• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <iomanip>
16 #include <mutex>
17 #include "moving_photo_proxy.h"
18 #include "utils/camera_log.h"
19 
20 namespace OHOS {
21 namespace CameraStandard {
22 typedef AvcodecTaskManagerIntf* (*CreateAVCodecTaskManagerIntf)();
23 typedef AudioCapturerSessionIntf* (*CreateAudioCapturerSessionIntf)();
24 typedef MovingPhotoVideoCacheIntf* (*CreateMovingPhotoVideoCacheIntf)();
25 
AvcodecTaskManagerProxy(std::shared_ptr<Dynamiclib> avcodecTaskManagerLib,sptr<AvcodecTaskManagerIntf> avcodecTaskManagerIntf)26 AvcodecTaskManagerProxy::AvcodecTaskManagerProxy(
27     std::shared_ptr<Dynamiclib> avcodecTaskManagerLib, sptr<AvcodecTaskManagerIntf> avcodecTaskManagerIntf)
28     : avcodecTaskManagerLib_(avcodecTaskManagerLib), avcodecTaskManagerIntf_(avcodecTaskManagerIntf)
29 {
30     MEDIA_DEBUG_LOG("AvcodecTaskManagerProxy constructor");
31     CHECK_RETURN_ELOG(avcodecTaskManagerLib_ == nullptr, "avcodecTaskManagerLib_ is nullptr");
32     CHECK_RETURN_ELOG(avcodecTaskManagerIntf_ == nullptr, "avcodecTaskManagerIntf_ is nullptr");
33 }
34 
~AvcodecTaskManagerProxy()35 AvcodecTaskManagerProxy::~AvcodecTaskManagerProxy()
36 {
37     MEDIA_DEBUG_LOG("AvcodecTaskManagerProxy destructor");
38 }
39 
CreateAvcodecTaskManagerProxy()40 sptr<AvcodecTaskManagerProxy> AvcodecTaskManagerProxy::CreateAvcodecTaskManagerProxy()
41 {
42     std::shared_ptr<Dynamiclib> dynamiclib = CameraDynamicLoader::GetDynamiclib(MOVING_PHOTO_SO);
43     CHECK_RETURN_RET_ELOG(dynamiclib == nullptr, nullptr, "Failed to load moving photo library");
44     CreateAVCodecTaskManagerIntf createAVCodecTaskManagerIntf =
45         (CreateAVCodecTaskManagerIntf)dynamiclib->GetFunction("createAVCodecTaskManagerIntf");
46     CHECK_RETURN_RET_ELOG(
47         createAVCodecTaskManagerIntf == nullptr, nullptr, "Failed to get createAVCodecTaskManagerIntf function");
48     AvcodecTaskManagerIntf* avcodecTaskManagerIntf = createAVCodecTaskManagerIntf();
49     CHECK_RETURN_RET_ELOG(
50         avcodecTaskManagerIntf == nullptr, nullptr, "Failed to create AvcodecTaskManagerIntf instance");
51     sptr<AvcodecTaskManagerProxy> aVCodecTaskManagerProxy =
52         new AvcodecTaskManagerProxy(dynamiclib, sptr<AvcodecTaskManagerIntf>(avcodecTaskManagerIntf));
53     return aVCodecTaskManagerProxy;
54 }
55 
CreateAvcodecTaskManager(sptr<AudioCapturerSessionIntf> audioCapturerSessionIntf,VideoCodecType type,int32_t colorSpace)56 int32_t AvcodecTaskManagerProxy::CreateAvcodecTaskManager(sptr<AudioCapturerSessionIntf> audioCapturerSessionIntf,
57     VideoCodecType type, int32_t colorSpace)
58 {
59     MEDIA_DEBUG_LOG("CreateAvcodecTaskManager start, type: %{public}d, colorSpace: %{public}d",
60         static_cast<int32_t>(type), colorSpace);
61     CHECK_RETURN_RET_ELOG(audioCapturerSessionIntf == nullptr, -1, "audioCapturerSessionIntf is nullptr");
62     CHECK_RETURN_RET_ELOG(avcodecTaskManagerIntf_ == nullptr, -1, "avcodecTaskManagerIntf_ is nullptr");
63     sptr<AudioCapturerSessionProxy> audioCapturerSessionProxy =
64         static_cast<AudioCapturerSessionProxy*>(audioCapturerSessionIntf.GetRefPtr());
65     CHECK_RETURN_RET_ELOG(audioCapturerSessionProxy == nullptr, -1, "audioCapturerSessionProxy is nullptr");
66     return avcodecTaskManagerIntf_->CreateAvcodecTaskManager(
67         audioCapturerSessionProxy->GetAudioCapturerSessionAdapter(), type, colorSpace);
68 }
69 
CreateAvcodecTaskManager(wptr<Surface> movingSurface,shared_ptr<Size> size,sptr<AudioCapturerSessionIntf> audioCapturerSessionIntf,VideoCodecType type,int32_t colorSpace)70 int32_t AvcodecTaskManagerProxy::CreateAvcodecTaskManager(wptr<Surface> movingSurface, shared_ptr<Size> size,
71     sptr<AudioCapturerSessionIntf> audioCapturerSessionIntf, VideoCodecType type, int32_t colorSpace)
72 {
73     MEDIA_DEBUG_LOG("CreateAvcodecTaskManager start, type: %{public}d, colorSpace: %{public}d",
74         static_cast<int32_t>(type), colorSpace);
75     CHECK_RETURN_RET_ELOG(audioCapturerSessionIntf == nullptr, -1, "audioCapturerSessionIntf is nullptr");
76     CHECK_RETURN_RET_ELOG(avcodecTaskManagerIntf_ == nullptr, -1, "avcodecTaskManagerIntf_ is nullptr");
77     sptr<AudioCapturerSessionProxy> audioCapturerSessionProxy =
78         static_cast<AudioCapturerSessionProxy*>(audioCapturerSessionIntf.GetRefPtr());
79     CHECK_RETURN_RET_ELOG(audioCapturerSessionProxy == nullptr, -1, "audioCapturerSessionProxy is nullptr");
80     return avcodecTaskManagerIntf_->CreateAvcodecTaskManager(movingSurface, size,
81         audioCapturerSessionProxy->GetAudioCapturerSessionAdapter(), type, colorSpace);
82 }
83 
SetVideoBufferDuration(uint32_t preBufferCount,uint32_t postBufferCount)84 void AvcodecTaskManagerProxy::SetVideoBufferDuration(uint32_t preBufferCount, uint32_t postBufferCount)
85 {
86     MEDIA_DEBUG_LOG("SetVideoBufferDuration start, preBufferCount: %{public}u, postBufferCount: %{public}u",
87         preBufferCount, postBufferCount);
88     CHECK_RETURN_ELOG(avcodecTaskManagerIntf_ == nullptr, "avcodecTaskManagerIntf_ is nullptr");
89     avcodecTaskManagerIntf_->SetVideoBufferDuration(preBufferCount, postBufferCount);
90 }
91 
SetVideoFd(int64_t timestamp,std::shared_ptr<PhotoAssetIntf> photoAssetProxy,int32_t captureId)92 void AvcodecTaskManagerProxy::SetVideoFd(int64_t timestamp, std::shared_ptr<PhotoAssetIntf> photoAssetProxy,
93     int32_t captureId)
94 {
95     MEDIA_DEBUG_LOG("SetVideoFd start, timestamp: %{public}" PRIu64 ", captureId: %{public}d",
96         timestamp, captureId);
97     CHECK_RETURN_ELOG(avcodecTaskManagerIntf_ == nullptr, "avcodecTaskManagerIntf_ is nullptr");
98     avcodecTaskManagerIntf_->SetVideoFd(timestamp, photoAssetProxy, captureId);
99 }
100 
SubmitTask(std::function<void ()> task)101 void AvcodecTaskManagerProxy::SubmitTask(std::function<void()> task)
102 {
103     MEDIA_DEBUG_LOG("SubmitTask start");
104     CHECK_RETURN_ELOG(avcodecTaskManagerIntf_ == nullptr, "avcodecTaskManagerIntf_ is nullptr");
105     avcodecTaskManagerIntf_->SubmitTask(task);
106 }
107 
EncodeVideoBuffer(sptr<FrameRecord> frameRecord,CacheCbFunc cacheCallback)108 void AvcodecTaskManagerProxy::EncodeVideoBuffer(sptr<FrameRecord> frameRecord, CacheCbFunc cacheCallback)
109 {
110     MEDIA_DEBUG_LOG("EncodeVideoBuffer start");
111     CHECK_RETURN_ELOG(avcodecTaskManagerIntf_ == nullptr, "avcodecTaskManagerIntf_ is nullptr");
112     avcodecTaskManagerIntf_->EncodeVideoBuffer(frameRecord, cacheCallback);
113 }
114 
DoMuxerVideo(std::vector<sptr<FrameRecord>> frameRecords,uint64_t taskName,int32_t rotation,int32_t captureId)115 void AvcodecTaskManagerProxy::DoMuxerVideo(std::vector<sptr<FrameRecord>> frameRecords, uint64_t taskName,
116     int32_t rotation, int32_t captureId)
117 {
118     MEDIA_DEBUG_LOG("DoMuxerVideo start, taskName: %{public}" PRIu64 ", rotation: %{public}d, captureId: %{public}d",
119         taskName, rotation, captureId);
120     CHECK_RETURN_ELOG(avcodecTaskManagerIntf_ == nullptr, "avcodecTaskManagerIntf_ is nullptr");
121     avcodecTaskManagerIntf_->DoMuxerVideo(frameRecords, taskName, rotation, captureId);
122 }
123 
isEmptyVideoFdMap()124 bool AvcodecTaskManagerProxy::isEmptyVideoFdMap()
125 {
126     MEDIA_DEBUG_LOG("isEmptyVideoFdMap start");
127     CHECK_RETURN_RET_ELOG(avcodecTaskManagerIntf_ == nullptr, true, "avcodecTaskManagerIntf_ is nullptr");
128     bool result = avcodecTaskManagerIntf_->isEmptyVideoFdMap();
129     MEDIA_DEBUG_LOG("isEmptyVideoFdMap success, result: %{public}d", result);
130     return result;
131 }
132 
TaskManagerInsertStartTime(int32_t captureId,int64_t startTimeStamp)133 bool AvcodecTaskManagerProxy::TaskManagerInsertStartTime(int32_t captureId, int64_t startTimeStamp)
134 {
135     MEDIA_DEBUG_LOG("TaskManagerInsertStartTime start, captureId: %{public}d, startTimeStamp: %{public}" PRIu64,
136         captureId, startTimeStamp);
137     CHECK_RETURN_RET_ELOG(avcodecTaskManagerIntf_ == nullptr, false, "avcodecTaskManagerIntf_ is nullptr");
138     return avcodecTaskManagerIntf_->TaskManagerInsertStartTime(captureId, startTimeStamp);
139 }
140 
TaskManagerInsertEndTime(int32_t captureId,int64_t endTimeStamp)141 bool AvcodecTaskManagerProxy::TaskManagerInsertEndTime(int32_t captureId, int64_t endTimeStamp)
142 {
143     MEDIA_DEBUG_LOG("TaskManagerInsertEndTime start, captureId: %{public}d, endTimeStamp: %{public}" PRIu64,
144         captureId, endTimeStamp);
145     CHECK_RETURN_RET_ELOG(avcodecTaskManagerIntf_ == nullptr, false, "avcodecTaskManagerIntf_ is nullptr");
146     return avcodecTaskManagerIntf_->TaskManagerInsertEndTime(captureId, endTimeStamp);
147 }
148 
GetTaskManagerAdapter() const149 sptr<AvcodecTaskManagerIntf> AvcodecTaskManagerProxy::GetTaskManagerAdapter() const
150 {
151     return avcodecTaskManagerIntf_;
152 }
153 
AudioCapturerSessionProxy(std::shared_ptr<Dynamiclib> audioCapturerSessionLib,sptr<AudioCapturerSessionIntf> audioCapturerSessionIntf)154 AudioCapturerSessionProxy::AudioCapturerSessionProxy(
155     std::shared_ptr<Dynamiclib> audioCapturerSessionLib, sptr<AudioCapturerSessionIntf> audioCapturerSessionIntf)
156     : audioCapturerSessionLib_(audioCapturerSessionLib), audioCapturerSessionIntf_(audioCapturerSessionIntf)
157 {
158     MEDIA_DEBUG_LOG("AudioCapturerSessionProxy constructor");
159     CHECK_RETURN_ELOG(audioCapturerSessionLib_ == nullptr, "audioCapturerSessionLib_ is nullptr");
160     CHECK_RETURN_ELOG(audioCapturerSessionIntf_ == nullptr, "audioCapturerSessionIntf_ is nullptr");
161 }
162 
~AudioCapturerSessionProxy()163 AudioCapturerSessionProxy::~AudioCapturerSessionProxy()
164 {
165     MEDIA_DEBUG_LOG("AudioCapturerSessionProxy destructor");
166 }
167 
CreateAudioCapturerSessionProxy()168 sptr<AudioCapturerSessionProxy> AudioCapturerSessionProxy::CreateAudioCapturerSessionProxy()
169 {
170     std::shared_ptr<Dynamiclib> dynamiclib = CameraDynamicLoader::GetDynamiclib(MOVING_PHOTO_SO);
171     CHECK_RETURN_RET_ELOG(dynamiclib == nullptr, nullptr, "Failed to load moving photo library");
172 
173     CreateAudioCapturerSessionIntf createAudioCapturerSessionIntf =
174         (CreateAudioCapturerSessionIntf)dynamiclib->GetFunction("createAudioCapturerSessionIntf");
175     CHECK_RETURN_RET_ELOG(
176         createAudioCapturerSessionIntf == nullptr, nullptr, "Failed to get createAudioCapturerSessionIntf function");
177 
178     AudioCapturerSessionIntf* audioCapturerSessionIntf = createAudioCapturerSessionIntf();
179     CHECK_RETURN_RET_ELOG(
180         audioCapturerSessionIntf == nullptr, nullptr, "Failed to create AudioCapturerSessionIntf instance");
181 
182     sptr<AudioCapturerSessionProxy> audioCapturerSessionProxy =
183         new AudioCapturerSessionProxy(dynamiclib, sptr<AudioCapturerSessionIntf>(audioCapturerSessionIntf));
184     return audioCapturerSessionProxy;
185 }
186 
CreateAudioSession()187 int32_t AudioCapturerSessionProxy::CreateAudioSession()
188 {
189     MEDIA_DEBUG_LOG("CreateAudioSession start");
190     CHECK_RETURN_RET_ELOG(audioCapturerSessionIntf_ == nullptr, -1, "audioCapturerSessionIntf_ is nullptr");
191     return audioCapturerSessionIntf_->CreateAudioSession();
192 }
193 
StartAudioCapture()194 bool AudioCapturerSessionProxy::StartAudioCapture()
195 {
196     MEDIA_DEBUG_LOG("StartAudioCapture start");
197     CHECK_RETURN_RET_ELOG(audioCapturerSessionIntf_ == nullptr, false, "audioCapturerSessionIntf_ is nullptr");
198     bool result = audioCapturerSessionIntf_->StartAudioCapture();
199     MEDIA_DEBUG_LOG("StartAudioCapture success, result: %{public}d", result);
200     return result;
201 }
202 
StopAudioCapture()203 void AudioCapturerSessionProxy::StopAudioCapture()
204 {
205     MEDIA_DEBUG_LOG("StopAudioCapture start");
206     std::lock_guard<std::mutex> lock(audioCaptureLock_);
207     CHECK_RETURN_ELOG(audioCapturerSessionIntf_ == nullptr, "audioCapturerSessionIntf_ is nullptr");
208     audioCapturerSessionIntf_->StopAudioCapture();
209 }
210 
GetAudioCapturerSessionAdapter() const211 sptr<AudioCapturerSessionIntf> AudioCapturerSessionProxy::GetAudioCapturerSessionAdapter() const
212 {
213     return audioCapturerSessionIntf_;
214 }
215 
MovingPhotoVideoCacheProxy(std::shared_ptr<Dynamiclib> movingPhotoVideoCacheLib,sptr<MovingPhotoVideoCacheIntf> movingPhotoVideoCacheIntf)216 MovingPhotoVideoCacheProxy::MovingPhotoVideoCacheProxy(
217     std::shared_ptr<Dynamiclib> movingPhotoVideoCacheLib, sptr<MovingPhotoVideoCacheIntf> movingPhotoVideoCacheIntf)
218     : movingPhotoVideoCacheLib_(movingPhotoVideoCacheLib), movingPhotoVideoCacheIntf_(movingPhotoVideoCacheIntf)
219 {
220     MEDIA_DEBUG_LOG("MovingPhotoVideoCacheProxy constructor");
221     CHECK_RETURN_ELOG(movingPhotoVideoCacheLib_ == nullptr, "movingPhotoVideoCacheLib_ is nullptr");
222     CHECK_RETURN_ELOG(movingPhotoVideoCacheIntf_ == nullptr, "movingPhotoVideoCacheIntf_ is nullptr");
223 }
224 
~MovingPhotoVideoCacheProxy()225 MovingPhotoVideoCacheProxy::~MovingPhotoVideoCacheProxy()
226 {
227     MEDIA_DEBUG_LOG("MovingPhotoVideoCacheProxy destructor");
228 }
229 
CreateMovingPhotoVideoCacheProxy()230 sptr<MovingPhotoVideoCacheProxy> MovingPhotoVideoCacheProxy::CreateMovingPhotoVideoCacheProxy()
231 {
232     std::shared_ptr<Dynamiclib> dynamiclib = CameraDynamicLoader::GetDynamiclib(MOVING_PHOTO_SO);
233     CHECK_RETURN_RET_ELOG(dynamiclib == nullptr, nullptr, "Failed to load moving photo library");
234 
235     CreateMovingPhotoVideoCacheIntf createMovingPhotoVideoCacheIntf =
236         (CreateMovingPhotoVideoCacheIntf)dynamiclib->GetFunction("createMovingPhotoVideoCacheIntf");
237     CHECK_RETURN_RET_ELOG(createMovingPhotoVideoCacheIntf == nullptr, nullptr,
238         "Failed to get createMovingPhotoVideoCacheIntf function");
239     MovingPhotoVideoCacheIntf* movingPhotoVideoCacheIntf = createMovingPhotoVideoCacheIntf();
240     CHECK_RETURN_RET_ELOG(
241         movingPhotoVideoCacheIntf == nullptr, nullptr, "Failed to create MovingPhotoVideoCacheIntf instance");
242     sptr<MovingPhotoVideoCacheProxy> movingPhotoVideoCacheProxy =
243         new MovingPhotoVideoCacheProxy(dynamiclib, sptr<MovingPhotoVideoCacheIntf>(movingPhotoVideoCacheIntf));
244     return movingPhotoVideoCacheProxy;
245 }
246 
CreateMovingPhotoVideoCache(sptr<AvcodecTaskManagerIntf> avcodecTaskManagerIntf)247 int32_t MovingPhotoVideoCacheProxy::CreateMovingPhotoVideoCache(sptr<AvcodecTaskManagerIntf> avcodecTaskManagerIntf)
248 {
249     MEDIA_DEBUG_LOG("CreateMovingPhotoVideoCache start");
250     CHECK_RETURN_RET_ELOG(avcodecTaskManagerIntf == nullptr, -1, "avcodecTaskManagerIntf is nullptr");
251     CHECK_RETURN_RET_ELOG(movingPhotoVideoCacheIntf_ == nullptr, -1, "movingPhotoVideoCacheIntf_ is nullptr");
252     sptr<AvcodecTaskManagerProxy> avcodecTaskManagerProxy =
253         static_cast<AvcodecTaskManagerProxy*>(avcodecTaskManagerIntf.GetRefPtr());
254     CHECK_RETURN_RET_ELOG(avcodecTaskManagerProxy == nullptr, -1, "avcodecTaskManagerProxy is nullptr");
255     return movingPhotoVideoCacheIntf_->CreateMovingPhotoVideoCache(avcodecTaskManagerProxy->GetTaskManagerAdapter());
256 }
257 
OnDrainFrameRecord(sptr<FrameRecord> frame)258 void MovingPhotoVideoCacheProxy::OnDrainFrameRecord(sptr<FrameRecord> frame)
259 {
260     MEDIA_DEBUG_LOG("OnDrainFrameRecord start");
261     CHECK_RETURN_ELOG(movingPhotoVideoCacheIntf_ == nullptr, "movingPhotoVideoCacheIntf_ is nullptr");
262     movingPhotoVideoCacheIntf_->OnDrainFrameRecord(frame);
263 }
264 
GetFrameCachedResult(std::vector<sptr<FrameRecord>> frameRecords,uint64_t taskName,int32_t rotation,int32_t captureId)265 void MovingPhotoVideoCacheProxy::GetFrameCachedResult(std::vector<sptr<FrameRecord>> frameRecords,
266     uint64_t taskName, int32_t rotation, int32_t captureId)
267 {
268     MEDIA_DEBUG_LOG("GetFrameCachedResult start");
269     CHECK_RETURN_ELOG(movingPhotoVideoCacheIntf_ == nullptr, "movingPhotoVideoCacheIntf_ is nullptr");
270     movingPhotoVideoCacheIntf_->GetFrameCachedResult(frameRecords, taskName, rotation, captureId);
271 }
272 } // namespace CameraStandard
273 } // namespace OHOS