• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 
16 #include "photo_output_impl.h"
17 #include "camera_error.h"
18 #include "camera_log.h"
19 #include "cj_lambda.h"
20 
21 namespace OHOS {
22 namespace CameraStandard {
23 
24 thread_local sptr<PhotoOutput> CJPhotoOutput::sPhotoOutput_ = nullptr;
25 
OnCaptureStarted(const int32_t captureId) const26 void CJPhotoOutputCallback::OnCaptureStarted(const int32_t captureId) const
27 {
28 }
29 
OnCaptureStarted(const int32_t captureId,uint32_t exposureTime) const30 void CJPhotoOutputCallback::OnCaptureStarted(const int32_t captureId, uint32_t exposureTime) const
31 {
32     std::lock_guard<std::mutex> lock(captureStartedMutex);
33     if (captureStartedCallbackList.size() == 0) {
34         return;
35     }
36     for (size_t i = 0; i < captureStartedCallbackList.size(); i++) {
37         if (captureStartedCallbackList[i] != nullptr) {
38             captureStartedCallbackList[i]->ref(captureId, exposureTime);
39         }
40     }
41 }
42 
OnCaptureEnded(const int32_t captureId,const int32_t frameCount) const43 void CJPhotoOutputCallback::OnCaptureEnded(const int32_t captureId, const int32_t frameCount) const
44 {
45     std::lock_guard<std::mutex> lock(captureEndedMutex);
46     if (captureEndedCallbackList.size() == 0) {
47         return;
48     }
49     for (size_t i = 0; i < captureEndedCallbackList.size(); i++) {
50         if (captureEndedCallbackList[i] != nullptr) {
51             captureEndedCallbackList[i]->ref(captureId, frameCount);
52         }
53     }
54 }
55 
OnFrameShutter(const int32_t captureId,const uint64_t timestamp) const56 void CJPhotoOutputCallback::OnFrameShutter(const int32_t captureId, const uint64_t timestamp) const
57 {
58     std::lock_guard<std::mutex> lock(frameShutterMutex);
59     if (frameShutterCallbackList.size() == 0) {
60         return;
61     }
62     for (size_t i = 0; i < frameShutterCallbackList.size(); i++) {
63         if (frameShutterCallbackList[i] != nullptr) {
64             frameShutterCallbackList[i]->ref(captureId, timestamp);
65         }
66     }
67 }
68 
OnFrameShutterEnd(const int32_t captureId,const uint64_t timestamp) const69 void CJPhotoOutputCallback::OnFrameShutterEnd(const int32_t captureId, const uint64_t timestamp) const
70 {
71     std::lock_guard<std::mutex> lock(frameShutterEndMutex);
72     if (frameShutterEndCallbackList.size() == 0) {
73         return;
74     }
75     for (size_t i = 0; i < frameShutterEndCallbackList.size(); i++) {
76         if (frameShutterEndCallbackList[i] != nullptr) {
77             frameShutterEndCallbackList[i]->ref(captureId);
78         }
79     }
80 }
81 
OnCaptureReady(const int32_t captureId,const uint64_t timestamp) const82 void CJPhotoOutputCallback::OnCaptureReady(const int32_t captureId, const uint64_t timestamp) const
83 {
84     std::lock_guard<std::mutex> lock(captureReadyMutex);
85     if (captureReadyCallbackList.size() == 0) {
86         return;
87     }
88     for (size_t i = 0; i < captureReadyCallbackList.size(); i++) {
89         if (captureReadyCallbackList[i] != nullptr) {
90             captureReadyCallbackList[i]->ref();
91         }
92     }
93 }
94 
OnCaptureError(const int32_t captureId,const int32_t errorCode) const95 void CJPhotoOutputCallback::OnCaptureError(const int32_t captureId, const int32_t errorCode) const
96 {
97     std::lock_guard<std::mutex> lock(captureErrorMutex);
98     if (captureErrorCallbackList.size() == 0) {
99         return;
100     }
101     for (size_t i = 0; i < captureErrorCallbackList.size(); i++) {
102         if (captureErrorCallbackList[i] != nullptr) {
103             captureErrorCallbackList[i]->ref(errorCode);
104         }
105     }
106 }
107 
OnEstimatedCaptureDuration(const int32_t duration) const108 void CJPhotoOutputCallback::OnEstimatedCaptureDuration(const int32_t duration) const
109 {
110     std::lock_guard<std::mutex> lock(estimatedCaptureDurationMutex);
111     if (estimatedCaptureDurationCallbackList.size() == 0) {
112         return;
113     }
114     for (size_t i = 0; i < estimatedCaptureDurationCallbackList.size(); i++) {
115         if (estimatedCaptureDurationCallbackList[i] != nullptr) {
116             estimatedCaptureDurationCallbackList[i]->ref(duration);
117         }
118     }
119 }
120 
OnOfflineDeliveryFinished(const int32_t captureId) const121 void CJPhotoOutputCallback::OnOfflineDeliveryFinished(const int32_t captureId) const
122 {
123     return;
124 }
125 
CJPhotoOutput()126 CJPhotoOutput::CJPhotoOutput()
127 {
128     photoOutput_ = sPhotoOutput_;
129     sPhotoOutput_ = nullptr;
130 }
131 
GetCameraOutput()132 sptr<CameraStandard::CaptureOutput> CJPhotoOutput::GetCameraOutput()
133 {
134     return photoOutput_;
135 }
136 
Release()137 int32_t CJPhotoOutput::Release()
138 {
139     if (photoOutput_ == nullptr) {
140         return CameraError::CAMERA_SERVICE_ERROR;
141     }
142     return photoOutput_->Release();
143 }
144 
CreatePhotoOutput()145 int32_t CJPhotoOutput::CreatePhotoOutput()
146 {
147     sptr<Surface> photoSurface;
148     MEDIA_INFO_LOG("create surface as consumer");
149     photoSurface = Surface::CreateSurfaceAsConsumer("photoOutput");
150     if (photoSurface == nullptr) {
151         MEDIA_ERR_LOG("failed to get surface");
152         return CameraError::CAMERA_SERVICE_ERROR;
153     }
154     sptr<IBufferProducer> surfaceProducer = photoSurface->GetProducer();
155     MEDIA_INFO_LOG("surface width: %{public}d, height: %{public}d", photoSurface->GetDefaultWidth(),
156                    photoSurface->GetDefaultHeight());
157     int retCode =
158         CameraManager::GetInstance()->CreatePhotoOutputWithoutProfile(surfaceProducer, &sPhotoOutput_, photoSurface);
159     if (sPhotoOutput_ == nullptr) {
160         MEDIA_ERR_LOG("failed to create CreatePhotoOutput");
161     }
162     return retCode;
163 }
164 
CreatePhotoOutputWithProfile(Profile & profile)165 int32_t CJPhotoOutput::CreatePhotoOutputWithProfile(Profile &profile)
166 {
167     MEDIA_DEBUG_LOG("CreatePhotoOutput is called, profile CameraFormat= %{public}d", profile.GetCameraFormat());
168     sptr<Surface> photoSurface;
169     MEDIA_INFO_LOG("create surface as consumer");
170     photoSurface = Surface::CreateSurfaceAsConsumer("photoOutput");
171     if (photoSurface == nullptr) {
172         MEDIA_ERR_LOG("failed to get surface");
173         return CameraError::CAMERA_SERVICE_ERROR;
174     }
175     photoSurface->SetUserData(CameraManager::surfaceFormat, std::to_string(profile.GetCameraFormat()));
176     sptr<IBufferProducer> surfaceProducer = photoSurface->GetProducer();
177     MEDIA_INFO_LOG("profile width: %{public}d, height: %{public}d, format = %{public}d, "
178                    "surface width: %{public}d, height: %{public}d",
179                    profile.GetSize().width, profile.GetSize().height, static_cast<int32_t>(profile.GetCameraFormat()),
180                    photoSurface->GetDefaultWidth(), photoSurface->GetDefaultHeight());
181     int retCode =
182         CameraManager::GetInstance()->CreatePhotoOutput(profile, surfaceProducer, &sPhotoOutput_, photoSurface);
183     if (sPhotoOutput_ == nullptr) {
184         MEDIA_ERR_LOG("failed to create CreatePhotoOutput");
185         return retCode;
186     }
187     sPhotoOutput_->SetNativeSurface(true);
188     if (sPhotoOutput_->IsYuvOrHeifPhoto()) {
189         sPhotoOutput_->CreateMultiChannel();
190     }
191     return retCode;
192 }
193 
Capture()194 int32_t CJPhotoOutput::Capture()
195 {
196     MEDIA_INFO_LOG("Capture is called");
197     if (photoOutput_ == nullptr) {
198         MEDIA_ERR_LOG("photooutput is nullptr.");
199         return CameraError::CAMERA_SERVICE_ERROR;
200     }
201     return photoOutput_->Capture();
202 }
203 
Convert2PhotoCaptureSetting(CJPhotoCaptureSetting & setting,bool isLocationNone)204 std::shared_ptr<PhotoCaptureSetting> Convert2PhotoCaptureSetting(CJPhotoCaptureSetting &setting, bool isLocationNone)
205 {
206     auto capSettings = make_shared<PhotoCaptureSetting>();
207     capSettings->SetQuality(static_cast<PhotoCaptureSetting::QualityLevel>(setting.quality));
208     capSettings->SetRotation(static_cast<PhotoCaptureSetting::RotationConfig>(setting.rotation));
209     capSettings->SetMirror(setting.mirror);
210     if (!isLocationNone) {
211         auto location = make_shared<Location>();
212         location->latitude = setting.location.latitude;
213         location->longitude = setting.location.longitude;
214         location->altitude = setting.location.altitude;
215         capSettings->SetLocation(location);
216     }
217     return capSettings;
218 }
219 
Capture(CJPhotoCaptureSetting & setting,bool isLocationNone)220 int32_t CJPhotoOutput::Capture(CJPhotoCaptureSetting &setting, bool isLocationNone)
221 {
222     MEDIA_INFO_LOG("Capture is called");
223     if (photoOutput_ == nullptr) {
224         MEDIA_ERR_LOG("photooutput is nullptr.");
225         return CameraError::CAMERA_SERVICE_ERROR;
226     }
227     auto capSettings = Convert2PhotoCaptureSetting(setting, isLocationNone);
228     return photoOutput_->Capture(capSettings);
229 }
230 
IsMovingPhotoSupported(int32_t * errCode)231 bool CJPhotoOutput::IsMovingPhotoSupported(int32_t *errCode)
232 {
233     MEDIA_INFO_LOG("IsMovingPhotoSupported is called!");
234     if (photoOutput_ == nullptr) {
235         MEDIA_ERR_LOG("photooutput is nullptr.");
236         *errCode = CameraError::CAMERA_SERVICE_ERROR;
237         return false;
238     }
239     auto session = photoOutput_->GetSession();
240     if (session == nullptr) {
241         MEDIA_ERR_LOG("session is nullptr.");
242         *errCode = CameraError::CAMERA_SERVICE_ERROR;
243         return false;
244     }
245     bool isSupported = session->IsMovingPhotoSupported();
246     return isSupported;
247 }
248 
EnableMovingPhoto(bool enabled)249 int32_t CJPhotoOutput::EnableMovingPhoto(bool enabled)
250 {
251     MEDIA_INFO_LOG("EnableMovingPhoto is called!");
252     if (photoOutput_ == nullptr) {
253         MEDIA_ERR_LOG("photooutput is nullptr.");
254         return CameraError::CAMERA_SERVICE_ERROR;
255     }
256     auto session = photoOutput_->GetSession();
257     if (session == nullptr) {
258         MEDIA_ERR_LOG("session is nullptr.");
259         return CameraError::CAMERA_SERVICE_ERROR;
260     }
261     session->LockForControl();
262     int32_t retCode = session->EnableMovingPhoto(enabled);
263     session->UnlockForControl();
264     return retCode;
265 }
266 
IsMirrorSupported(int32_t * errCode)267 bool CJPhotoOutput::IsMirrorSupported(int32_t *errCode)
268 {
269     MEDIA_INFO_LOG("IsMirrorSupported is called!");
270     if (photoOutput_ == nullptr) {
271         MEDIA_ERR_LOG("photooutput is nullptr.");
272         *errCode = CameraError::CAMERA_SERVICE_ERROR;
273         return false;
274     }
275     bool isSupported = photoOutput_->IsMirrorSupported();
276     return isSupported;
277 }
278 
EnableMirror(bool isMirror)279 int32_t CJPhotoOutput::EnableMirror(bool isMirror)
280 {
281     MEDIA_INFO_LOG("EnableMirror is called!");
282     if (photoOutput_ == nullptr) {
283         MEDIA_ERR_LOG("photooutput is nullptr.");
284         return CameraError::CAMERA_SERVICE_ERROR;
285     }
286     auto session = photoOutput_->GetSession();
287     if (session == nullptr) {
288         MEDIA_ERR_LOG("session is nullptr.");
289         return CameraError::CAMERA_SERVICE_ERROR;
290     }
291     auto isSessionConfiged = session->IsSessionCommited() || session->IsSessionStarted();
292     return session->EnableMovingPhotoMirror(isMirror, isSessionConfiged);
293 }
294 
SetMovingPhotoVideoCodecType(int32_t codecType)295 int32_t CJPhotoOutput::SetMovingPhotoVideoCodecType(int32_t codecType)
296 {
297     MEDIA_DEBUG_LOG("SetMovingPhotoVideoCodecType is called");
298     if (photoOutput_ == nullptr) {
299         MEDIA_ERR_LOG("photooutput is nullptr.");
300         return CameraError::CAMERA_SERVICE_ERROR;
301     }
302     return photoOutput_->SetMovingPhotoVideoCodecType(codecType);
303 }
304 
GetActiveProfile(int32_t * errCode)305 CJProfile CJPhotoOutput::GetActiveProfile(int32_t *errCode)
306 {
307     MEDIA_DEBUG_LOG("GetActiveProfile is called");
308     if (photoOutput_ == nullptr) {
309         MEDIA_ERR_LOG("photooutput is nullptr.");
310         *errCode = CameraError::CAMERA_SERVICE_ERROR;
311         return CJProfile{0};
312     }
313     auto profile = photoOutput_->GetPhotoProfile();
314     if (profile == nullptr) {
315         MEDIA_ERR_LOG("profile is nullptr.");
316         *errCode = CameraError::CAMERA_SERVICE_ERROR;
317         return CJProfile{0};
318     }
319     return CJProfile{
320         .format = profile->GetCameraFormat(), .width = profile->GetSize().width, .height = profile->GetSize().height};
321 }
322 
GetPhotoRotation(int32_t deviceDegree,int32_t * errCode)323 int32_t CJPhotoOutput::GetPhotoRotation(int32_t deviceDegree, int32_t *errCode)
324 {
325     MEDIA_DEBUG_LOG("GetPhotoRotation is called");
326     if (photoOutput_ == nullptr) {
327         MEDIA_ERR_LOG("photooutput is nullptr.");
328         *errCode = CameraError::CAMERA_SERVICE_ERROR;
329         return -1;
330     }
331     int32_t retCode = photoOutput_->GetPhotoRotation(deviceDegree);
332     if (retCode == CameraError::CAMERA_SERVICE_ERROR) {
333         *errCode = CameraError::CAMERA_SERVICE_ERROR;
334         return -1;
335     }
336     return retCode;
337 }
338 
OnCaptureStartWithInfo(int64_t callbackId)339 void CJPhotoOutput::OnCaptureStartWithInfo(int64_t callbackId)
340 {
341     if (photoOutputCallback_ == nullptr) {
342         photoOutputCallback_ = std::make_shared<CJPhotoOutputCallback>();
343         if (photoOutputCallback_ == nullptr || photoOutput_ == nullptr) {
344             return;
345         }
346         photoOutput_->SetCallback(photoOutputCallback_);
347     }
348     auto cFunc = reinterpret_cast<void (*)(CJCaptureStartInfo info)>(callbackId);
349     auto callback = [lambda = CJLambda::Create(cFunc)](const int32_t captureId, uint32_t exposureTime) -> void {
350         lambda(CJCaptureStartInfo{captureId, exposureTime});
351     };
352     auto callbackRef = std::make_shared<CallbackRef<const int32_t, uint32_t>>(callback, callbackId);
353 
354     std::lock_guard<std::mutex> lock(photoOutputCallback_->captureStartedMutex);
355     photoOutputCallback_->captureStartedCallbackList.push_back(callbackRef);
356 }
357 
OffCaptureStartWithInfo(int64_t callbackId)358 void CJPhotoOutput::OffCaptureStartWithInfo(int64_t callbackId)
359 {
360     if (photoOutputCallback_ == nullptr) {
361         return;
362     }
363     std::lock_guard<std::mutex> lock(photoOutputCallback_->captureStartedMutex);
364     for (auto it = photoOutputCallback_->captureStartedCallbackList.begin();
365         it != photoOutputCallback_->captureStartedCallbackList.end(); it++) {
366         if ((*it)->id == callbackId) {
367             photoOutputCallback_->captureStartedCallbackList.erase(it);
368             break;
369         }
370     }
371 }
372 
OffAllCaptureStartWithInfo()373 void CJPhotoOutput::OffAllCaptureStartWithInfo()
374 {
375     if (photoOutputCallback_ == nullptr) {
376         return;
377     }
378     std::lock_guard<std::mutex> lock(photoOutputCallback_->captureStartedMutex);
379     photoOutputCallback_->captureStartedCallbackList.clear();
380 }
381 
OnFrameShutter(int64_t callbackId)382 void CJPhotoOutput::OnFrameShutter(int64_t callbackId)
383 {
384     if (photoOutputCallback_ == nullptr) {
385         photoOutputCallback_ = std::make_shared<CJPhotoOutputCallback>();
386         if (photoOutputCallback_ == nullptr || photoOutput_ == nullptr) {
387             return;
388         }
389         photoOutput_->SetCallback(photoOutputCallback_);
390     }
391     auto cFunc = reinterpret_cast<void (*)(CJFrameShutterInfo info)>(callbackId);
392     auto callback = [lambda = CJLambda::Create(cFunc)](const int32_t captureId, const uint64_t timestamp) -> void {
393         lambda(CJFrameShutterInfo{captureId, timestamp});
394     };
395     auto callbackRef = std::make_shared<CallbackRef<const int32_t, const uint64_t>>(callback, callbackId);
396 
397     std::lock_guard<std::mutex> lock(photoOutputCallback_->frameShutterMutex);
398     photoOutputCallback_->frameShutterCallbackList.push_back(callbackRef);
399 }
400 
OffFrameShutter(int64_t callbackId)401 void CJPhotoOutput::OffFrameShutter(int64_t callbackId)
402 {
403     if (photoOutputCallback_ == nullptr) {
404         return;
405     }
406     std::lock_guard<std::mutex> lock(photoOutputCallback_->frameShutterMutex);
407     for (auto it = photoOutputCallback_->frameShutterCallbackList.begin();
408         it != photoOutputCallback_->frameShutterCallbackList.end(); it++) {
409         if ((*it)->id == callbackId) {
410             photoOutputCallback_->frameShutterCallbackList.erase(it);
411             break;
412         }
413     }
414 }
415 
OffAllFrameShutter()416 void CJPhotoOutput::OffAllFrameShutter()
417 {
418     if (photoOutputCallback_ == nullptr) {
419         return;
420     }
421     std::lock_guard<std::mutex> lock(photoOutputCallback_->frameShutterMutex);
422     photoOutputCallback_->frameShutterCallbackList.clear();
423 }
424 
OnCaptureEnd(int64_t callbackId)425 void CJPhotoOutput::OnCaptureEnd(int64_t callbackId)
426 {
427     if (photoOutputCallback_ == nullptr) {
428         photoOutputCallback_ = std::make_shared<CJPhotoOutputCallback>();
429         if (photoOutputCallback_ == nullptr || photoOutput_ == nullptr) {
430             return;
431         }
432         photoOutput_->SetCallback(photoOutputCallback_);
433     }
434     auto cFunc = reinterpret_cast<void (*)(CJCaptureEndInfo info)>(callbackId);
435     auto callback = [lambda = CJLambda::Create(cFunc)](const int32_t captureId, const int32_t frameCount) -> void {
436         lambda(CJCaptureEndInfo{captureId, frameCount});
437     };
438     auto callbackRef = std::make_shared<CallbackRef<const int32_t, const int32_t>>(callback, callbackId);
439 
440     std::lock_guard<std::mutex> lock(photoOutputCallback_->captureEndedMutex);
441     photoOutputCallback_->captureEndedCallbackList.push_back(callbackRef);
442 }
443 
OffCaptureEnd(int64_t callbackId)444 void CJPhotoOutput::OffCaptureEnd(int64_t callbackId)
445 {
446     if (photoOutputCallback_ == nullptr) {
447         return;
448     }
449     std::lock_guard<std::mutex> lock(photoOutputCallback_->captureEndedMutex);
450     for (auto it = photoOutputCallback_->captureEndedCallbackList.begin();
451         it != photoOutputCallback_->captureEndedCallbackList.end(); it++) {
452         if ((*it)->id == callbackId) {
453             photoOutputCallback_->captureEndedCallbackList.erase(it);
454             break;
455         }
456     }
457 }
458 
OffAllCaptureEnd()459 void CJPhotoOutput::OffAllCaptureEnd()
460 {
461     if (photoOutputCallback_ == nullptr) {
462         return;
463     }
464     std::lock_guard<std::mutex> lock(photoOutputCallback_->captureEndedMutex);
465     photoOutputCallback_->captureEndedCallbackList.clear();
466 }
467 
OnFrameShutterEnd(int64_t callbackId)468 void CJPhotoOutput::OnFrameShutterEnd(int64_t callbackId)
469 {
470     if (photoOutputCallback_ == nullptr) {
471         photoOutputCallback_ = std::make_shared<CJPhotoOutputCallback>();
472         if (photoOutputCallback_ == nullptr || photoOutput_ == nullptr) {
473             return;
474         }
475         photoOutput_->SetCallback(photoOutputCallback_);
476     }
477     auto cFunc = reinterpret_cast<void (*)(const int32_t id)>(callbackId);
478     auto callback = [lambda = CJLambda::Create(cFunc)](const int32_t captureId) -> void { lambda(captureId); };
479     auto callbackRef = std::make_shared<CallbackRef<const int32_t>>(callback, callbackId);
480 
481     std::lock_guard<std::mutex> lock(photoOutputCallback_->frameShutterEndMutex);
482     photoOutputCallback_->frameShutterEndCallbackList.push_back(callbackRef);
483 }
484 
OffFrameShutterEnd(int64_t callbackId)485 void CJPhotoOutput::OffFrameShutterEnd(int64_t callbackId)
486 {
487     if (photoOutputCallback_ == nullptr) {
488         return;
489     }
490     std::lock_guard<std::mutex> lock(photoOutputCallback_->frameShutterEndMutex);
491     for (auto it = photoOutputCallback_->frameShutterEndCallbackList.begin();
492         it != photoOutputCallback_->frameShutterEndCallbackList.end(); it++) {
493         if ((*it)->id == callbackId) {
494             photoOutputCallback_->frameShutterEndCallbackList.erase(it);
495             break;
496         }
497     }
498 }
499 
OffAllFrameShutterEnd()500 void CJPhotoOutput::OffAllFrameShutterEnd()
501 {
502     if (photoOutputCallback_ == nullptr) {
503         return;
504     }
505     std::lock_guard<std::mutex> lock(photoOutputCallback_->frameShutterEndMutex);
506     photoOutputCallback_->frameShutterEndCallbackList.clear();
507 }
508 
OnCaptureReady(int64_t callbackId)509 void CJPhotoOutput::OnCaptureReady(int64_t callbackId)
510 {
511     if (photoOutputCallback_ == nullptr) {
512         photoOutputCallback_ = std::make_shared<CJPhotoOutputCallback>();
513         if (photoOutputCallback_ == nullptr || photoOutput_ == nullptr) {
514             return;
515         }
516         photoOutput_->SetCallback(photoOutputCallback_);
517     }
518     auto cFunc = reinterpret_cast<void (*)()>(callbackId);
519     auto callback = [lambda = CJLambda::Create(cFunc)]() -> void { lambda(); };
520     auto callbackRef = std::make_shared<CallbackRef<>>(callback, callbackId);
521 
522     std::lock_guard<std::mutex> lock(photoOutputCallback_->captureReadyMutex);
523     photoOutputCallback_->captureReadyCallbackList.push_back(callbackRef);
524 }
525 
OffCaptureReady(int64_t callbackId)526 void CJPhotoOutput::OffCaptureReady(int64_t callbackId)
527 {
528     if (photoOutputCallback_ == nullptr) {
529         return;
530     }
531     std::lock_guard<std::mutex> lock(photoOutputCallback_->captureReadyMutex);
532     for (auto it = photoOutputCallback_->captureReadyCallbackList.begin();
533         it != photoOutputCallback_->captureReadyCallbackList.end(); it++) {
534         if ((*it)->id == callbackId) {
535             photoOutputCallback_->captureReadyCallbackList.erase(it);
536             break;
537         }
538     }
539 }
540 
OffAllCaptureReady()541 void CJPhotoOutput::OffAllCaptureReady()
542 {
543     if (photoOutputCallback_ == nullptr) {
544         return;
545     }
546     std::lock_guard<std::mutex> lock(photoOutputCallback_->captureReadyMutex);
547     photoOutputCallback_->captureReadyCallbackList.clear();
548 }
549 
OnEstimatedCaptureDuration(int64_t callbackId)550 void CJPhotoOutput::OnEstimatedCaptureDuration(int64_t callbackId)
551 {
552     if (photoOutputCallback_ == nullptr) {
553         photoOutputCallback_ = std::make_shared<CJPhotoOutputCallback>();
554         if (photoOutputCallback_ == nullptr || photoOutput_ == nullptr) {
555             return;
556         }
557         photoOutput_->SetCallback(photoOutputCallback_);
558     }
559     auto cFunc = reinterpret_cast<void (*)(const int32_t duration)>(callbackId);
560     auto callback = [lambda = CJLambda::Create(cFunc)](const int32_t duration) -> void { lambda(duration); };
561     auto callbackRef = std::make_shared<CallbackRef<const int32_t>>(callback, callbackId);
562 
563     std::lock_guard<std::mutex> lock(photoOutputCallback_->estimatedCaptureDurationMutex);
564     photoOutputCallback_->estimatedCaptureDurationCallbackList.push_back(callbackRef);
565 }
566 
OffEstimatedCaptureDuration(int64_t callbackId)567 void CJPhotoOutput::OffEstimatedCaptureDuration(int64_t callbackId)
568 {
569     if (photoOutputCallback_ == nullptr) {
570         return;
571     }
572     std::lock_guard<std::mutex> lock(photoOutputCallback_->estimatedCaptureDurationMutex);
573     for (auto it = photoOutputCallback_->estimatedCaptureDurationCallbackList.begin();
574         it != photoOutputCallback_->estimatedCaptureDurationCallbackList.end(); it++) {
575         if ((*it)->id == callbackId) {
576             photoOutputCallback_->estimatedCaptureDurationCallbackList.erase(it);
577             break;
578         }
579     }
580 }
581 
OffAllEstimatedCaptureDuration()582 void CJPhotoOutput::OffAllEstimatedCaptureDuration()
583 {
584     if (photoOutputCallback_ == nullptr) {
585         return;
586     }
587     std::lock_guard<std::mutex> lock(photoOutputCallback_->estimatedCaptureDurationMutex);
588     photoOutputCallback_->estimatedCaptureDurationCallbackList.clear();
589 }
590 
OnError(int64_t callbackId)591 void CJPhotoOutput::OnError(int64_t callbackId)
592 {
593     if (photoOutputCallback_ == nullptr) {
594         photoOutputCallback_ = std::make_shared<CJPhotoOutputCallback>();
595         if (photoOutputCallback_ == nullptr || photoOutput_ == nullptr) {
596             return;
597         }
598         photoOutput_->SetCallback(photoOutputCallback_);
599     }
600     auto cFunc = reinterpret_cast<void (*)(const int32_t errorCode)>(callbackId);
601     auto callback = [lambda = CJLambda::Create(cFunc)](const int32_t errorCode) -> void { lambda(errorCode); };
602     auto callbackRef = std::make_shared<CallbackRef<const int32_t>>(callback, callbackId);
603 
604     std::lock_guard<std::mutex> lock(photoOutputCallback_->captureErrorMutex);
605     photoOutputCallback_->captureErrorCallbackList.push_back(callbackRef);
606 }
607 
OffError(int64_t callbackId)608 void CJPhotoOutput::OffError(int64_t callbackId)
609 {
610     if (photoOutputCallback_ == nullptr) {
611         return;
612     }
613     std::lock_guard<std::mutex> lock(photoOutputCallback_->captureErrorMutex);
614     for (auto it = photoOutputCallback_->captureErrorCallbackList.begin();
615         it != photoOutputCallback_->captureErrorCallbackList.end(); it++) {
616         if ((*it)->id == callbackId) {
617             photoOutputCallback_->captureErrorCallbackList.erase(it);
618             break;
619         }
620     }
621 }
622 
OffAllError()623 void CJPhotoOutput::OffAllError()
624 {
625     if (photoOutputCallback_ == nullptr) {
626         return;
627     }
628     std::lock_guard<std::mutex> lock(photoOutputCallback_->captureErrorMutex);
629     photoOutputCallback_->captureErrorCallbackList.clear();
630 }
631 
632 } // namespace CameraStandard
633 } // namespace OHOS