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
Capture(CJPhotoCaptureSetting setting)204 int32_t CJPhotoOutput::Capture(CJPhotoCaptureSetting setting)
205 {
206 MEDIA_INFO_LOG("Capture is called");
207 if (photoOutput_ == nullptr) {
208 MEDIA_ERR_LOG("photooutput is nullptr.");
209 return CameraError::CAMERA_SERVICE_ERROR;
210 }
211 std::shared_ptr<PhotoCaptureSetting> capSettings = make_shared<PhotoCaptureSetting>();
212 if (capSettings == nullptr) {
213 return CameraError::CAMERA_SERVICE_ERROR;
214 }
215 capSettings->SetQuality(static_cast<PhotoCaptureSetting::QualityLevel>(setting.quality));
216 capSettings->SetRotation(static_cast<PhotoCaptureSetting::RotationConfig>(setting.rotation));
217 capSettings->SetMirror(setting.mirror);
218 auto location = make_shared<Location>();
219 location->latitude = setting.location.latitude;
220 location->longitude = setting.location.longitude;
221 location->altitude = setting.location.altitude;
222 capSettings->SetLocation(location);
223 return photoOutput_->Capture(capSettings);
224 }
225
IsMovingPhotoSupported(int32_t * errCode)226 bool CJPhotoOutput::IsMovingPhotoSupported(int32_t *errCode)
227 {
228 MEDIA_INFO_LOG("IsMovingPhotoSupported is called!");
229 if (photoOutput_ == nullptr) {
230 MEDIA_ERR_LOG("photooutput is nullptr.");
231 *errCode = CameraError::CAMERA_SERVICE_ERROR;
232 return false;
233 }
234 auto session = photoOutput_->GetSession();
235 if (session == nullptr) {
236 MEDIA_ERR_LOG("session is nullptr.");
237 *errCode = CameraError::CAMERA_SERVICE_ERROR;
238 return false;
239 }
240 bool isSupported = session->IsMovingPhotoSupported();
241 return isSupported;
242 }
243
EnableMovingPhoto(bool enabled)244 int32_t CJPhotoOutput::EnableMovingPhoto(bool enabled)
245 {
246 MEDIA_INFO_LOG("EnableMovingPhoto is called!");
247 if (photoOutput_ == nullptr) {
248 MEDIA_ERR_LOG("photooutput is nullptr.");
249 return CameraError::CAMERA_SERVICE_ERROR;
250 }
251 auto session = photoOutput_->GetSession();
252 if (session == nullptr) {
253 MEDIA_ERR_LOG("session is nullptr.");
254 return CameraError::CAMERA_SERVICE_ERROR;
255 }
256 session->LockForControl();
257 int32_t retCode = session->EnableMovingPhoto(enabled);
258 session->UnlockForControl();
259 return retCode;
260 }
261
IsMirrorSupported(int32_t * errCode)262 bool CJPhotoOutput::IsMirrorSupported(int32_t *errCode)
263 {
264 MEDIA_INFO_LOG("IsMirrorSupported is called!");
265 if (photoOutput_ == nullptr) {
266 MEDIA_ERR_LOG("photooutput is nullptr.");
267 *errCode = CameraError::CAMERA_SERVICE_ERROR;
268 return false;
269 }
270 bool isSupported = photoOutput_->IsMirrorSupported();
271 return isSupported;
272 }
273
EnableMirror(bool isMirror)274 int32_t CJPhotoOutput::EnableMirror(bool isMirror)
275 {
276 MEDIA_INFO_LOG("EnableMirror is called!");
277 if (photoOutput_ == nullptr) {
278 MEDIA_ERR_LOG("photooutput is nullptr.");
279 return CameraError::CAMERA_SERVICE_ERROR;
280 }
281 auto session = photoOutput_->GetSession();
282 if (session == nullptr) {
283 MEDIA_ERR_LOG("session is nullptr.");
284 return CameraError::CAMERA_SERVICE_ERROR;
285 }
286 auto isSessionConfiged = session->IsSessionCommited() || session->IsSessionStarted();
287 return session->EnableMovingPhotoMirror(isMirror, isSessionConfiged);
288 }
289
SetMovingPhotoVideoCodecType(int32_t codecType)290 int32_t CJPhotoOutput::SetMovingPhotoVideoCodecType(int32_t codecType)
291 {
292 MEDIA_DEBUG_LOG("SetMovingPhotoVideoCodecType is called");
293 if (photoOutput_ == nullptr) {
294 MEDIA_ERR_LOG("photooutput is nullptr.");
295 return CameraError::CAMERA_SERVICE_ERROR;
296 }
297 return photoOutput_->SetMovingPhotoVideoCodecType(codecType);
298 }
299
GetActiveProfile(int32_t * errCode)300 CJProfile CJPhotoOutput::GetActiveProfile(int32_t *errCode)
301 {
302 MEDIA_DEBUG_LOG("GetActiveProfile is called");
303 if (photoOutput_ == nullptr) {
304 MEDIA_ERR_LOG("photooutput is nullptr.");
305 *errCode = CameraError::CAMERA_SERVICE_ERROR;
306 return CJProfile{0};
307 }
308 auto profile = photoOutput_->GetPhotoProfile();
309 if (profile == nullptr) {
310 MEDIA_ERR_LOG("profile is nullptr.");
311 *errCode = CameraError::CAMERA_SERVICE_ERROR;
312 return CJProfile{0};
313 }
314 return CJProfile{
315 .format = profile->GetCameraFormat(), .width = profile->GetSize().width, .height = profile->GetSize().height};
316 }
317
GetPhotoRotation(int32_t deviceDegree,int32_t * errCode)318 int32_t CJPhotoOutput::GetPhotoRotation(int32_t deviceDegree, int32_t *errCode)
319 {
320 MEDIA_DEBUG_LOG("GetPhotoRotation is called");
321 if (photoOutput_ == nullptr) {
322 MEDIA_ERR_LOG("photooutput is nullptr.");
323 *errCode = CameraError::CAMERA_SERVICE_ERROR;
324 return -1;
325 }
326 int32_t retCode = photoOutput_->GetPhotoRotation(deviceDegree);
327 if (retCode == CameraError::CAMERA_SERVICE_ERROR) {
328 *errCode = CameraError::CAMERA_SERVICE_ERROR;
329 return -1;
330 }
331 return retCode;
332 }
333
OnCaptureStartWithInfo(int64_t callbackId)334 void CJPhotoOutput::OnCaptureStartWithInfo(int64_t callbackId)
335 {
336 if (photoOutputCallback_ == nullptr) {
337 photoOutputCallback_ = std::make_shared<CJPhotoOutputCallback>();
338 if (photoOutputCallback_ == nullptr || photoOutput_ == nullptr) {
339 return;
340 }
341 photoOutput_->SetCallback(photoOutputCallback_);
342 }
343 auto cFunc = reinterpret_cast<void (*)(CJCaptureStartInfo info)>(callbackId);
344 auto callback = [lambda = CJLambda::Create(cFunc)](const int32_t captureId, uint32_t exposureTime) -> void {
345 lambda(CJCaptureStartInfo{captureId, exposureTime});
346 };
347 auto callbackRef = std::make_shared<CallbackRef<const int32_t, uint32_t>>(callback, callbackId);
348
349 std::lock_guard<std::mutex> lock(photoOutputCallback_->captureStartedMutex);
350 photoOutputCallback_->captureStartedCallbackList.push_back(callbackRef);
351 }
352
OffCaptureStartWithInfo(int64_t callbackId)353 void CJPhotoOutput::OffCaptureStartWithInfo(int64_t callbackId)
354 {
355 if (photoOutputCallback_ == nullptr) {
356 return;
357 }
358 std::lock_guard<std::mutex> lock(photoOutputCallback_->captureStartedMutex);
359 for (auto it = photoOutputCallback_->captureStartedCallbackList.begin();
360 it != photoOutputCallback_->captureStartedCallbackList.end(); it++) {
361 if ((*it)->id == callbackId) {
362 photoOutputCallback_->captureStartedCallbackList.erase(it);
363 break;
364 }
365 }
366 }
367
OffAllCaptureStartWithInfo()368 void CJPhotoOutput::OffAllCaptureStartWithInfo()
369 {
370 if (photoOutputCallback_ == nullptr) {
371 return;
372 }
373 std::lock_guard<std::mutex> lock(photoOutputCallback_->captureStartedMutex);
374 photoOutputCallback_->captureStartedCallbackList.clear();
375 }
376
OnFrameShutter(int64_t callbackId)377 void CJPhotoOutput::OnFrameShutter(int64_t callbackId)
378 {
379 if (photoOutputCallback_ == nullptr) {
380 photoOutputCallback_ = std::make_shared<CJPhotoOutputCallback>();
381 if (photoOutputCallback_ == nullptr || photoOutput_ == nullptr) {
382 return;
383 }
384 photoOutput_->SetCallback(photoOutputCallback_);
385 }
386 auto cFunc = reinterpret_cast<void (*)(CJFrameShutterInfo info)>(callbackId);
387 auto callback = [lambda = CJLambda::Create(cFunc)](const int32_t captureId, const uint64_t timestamp) -> void {
388 lambda(CJFrameShutterInfo{captureId, timestamp});
389 };
390 auto callbackRef = std::make_shared<CallbackRef<const int32_t, const uint64_t>>(callback, callbackId);
391
392 std::lock_guard<std::mutex> lock(photoOutputCallback_->frameShutterMutex);
393 photoOutputCallback_->frameShutterCallbackList.push_back(callbackRef);
394 }
395
OffFrameShutter(int64_t callbackId)396 void CJPhotoOutput::OffFrameShutter(int64_t callbackId)
397 {
398 if (photoOutputCallback_ == nullptr) {
399 return;
400 }
401 std::lock_guard<std::mutex> lock(photoOutputCallback_->frameShutterMutex);
402 for (auto it = photoOutputCallback_->frameShutterCallbackList.begin();
403 it != photoOutputCallback_->frameShutterCallbackList.end(); it++) {
404 if ((*it)->id == callbackId) {
405 photoOutputCallback_->frameShutterCallbackList.erase(it);
406 break;
407 }
408 }
409 }
410
OffAllFrameShutter()411 void CJPhotoOutput::OffAllFrameShutter()
412 {
413 if (photoOutputCallback_ == nullptr) {
414 return;
415 }
416 std::lock_guard<std::mutex> lock(photoOutputCallback_->frameShutterMutex);
417 photoOutputCallback_->frameShutterCallbackList.clear();
418 }
419
OnCaptureEnd(int64_t callbackId)420 void CJPhotoOutput::OnCaptureEnd(int64_t callbackId)
421 {
422 if (photoOutputCallback_ == nullptr) {
423 photoOutputCallback_ = std::make_shared<CJPhotoOutputCallback>();
424 if (photoOutputCallback_ == nullptr || photoOutput_ == nullptr) {
425 return;
426 }
427 photoOutput_->SetCallback(photoOutputCallback_);
428 }
429 auto cFunc = reinterpret_cast<void (*)(CJCaptureEndInfo info)>(callbackId);
430 auto callback = [lambda = CJLambda::Create(cFunc)](const int32_t captureId, const int32_t frameCount) -> void {
431 lambda(CJCaptureEndInfo{captureId, frameCount});
432 };
433 auto callbackRef = std::make_shared<CallbackRef<const int32_t, const int32_t>>(callback, callbackId);
434
435 std::lock_guard<std::mutex> lock(photoOutputCallback_->captureEndedMutex);
436 photoOutputCallback_->captureEndedCallbackList.push_back(callbackRef);
437 }
438
OffCaptureEnd(int64_t callbackId)439 void CJPhotoOutput::OffCaptureEnd(int64_t callbackId)
440 {
441 if (photoOutputCallback_ == nullptr) {
442 return;
443 }
444 std::lock_guard<std::mutex> lock(photoOutputCallback_->captureEndedMutex);
445 for (auto it = photoOutputCallback_->captureEndedCallbackList.begin();
446 it != photoOutputCallback_->captureEndedCallbackList.end(); it++) {
447 if ((*it)->id == callbackId) {
448 photoOutputCallback_->captureEndedCallbackList.erase(it);
449 break;
450 }
451 }
452 }
453
OffAllCaptureEnd()454 void CJPhotoOutput::OffAllCaptureEnd()
455 {
456 if (photoOutputCallback_ == nullptr) {
457 return;
458 }
459 std::lock_guard<std::mutex> lock(photoOutputCallback_->captureEndedMutex);
460 photoOutputCallback_->captureEndedCallbackList.clear();
461 }
462
OnFrameShutterEnd(int64_t callbackId)463 void CJPhotoOutput::OnFrameShutterEnd(int64_t callbackId)
464 {
465 if (photoOutputCallback_ == nullptr) {
466 photoOutputCallback_ = std::make_shared<CJPhotoOutputCallback>();
467 if (photoOutputCallback_ == nullptr || photoOutput_ == nullptr) {
468 return;
469 }
470 photoOutput_->SetCallback(photoOutputCallback_);
471 }
472 auto cFunc = reinterpret_cast<void (*)(const int32_t id)>(callbackId);
473 auto callback = [lambda = CJLambda::Create(cFunc)](const int32_t captureId) -> void { lambda(captureId); };
474 auto callbackRef = std::make_shared<CallbackRef<const int32_t>>(callback, callbackId);
475
476 std::lock_guard<std::mutex> lock(photoOutputCallback_->frameShutterEndMutex);
477 photoOutputCallback_->frameShutterEndCallbackList.push_back(callbackRef);
478 }
479
OffFrameShutterEnd(int64_t callbackId)480 void CJPhotoOutput::OffFrameShutterEnd(int64_t callbackId)
481 {
482 if (photoOutputCallback_ == nullptr) {
483 return;
484 }
485 std::lock_guard<std::mutex> lock(photoOutputCallback_->frameShutterEndMutex);
486 for (auto it = photoOutputCallback_->frameShutterEndCallbackList.begin();
487 it != photoOutputCallback_->frameShutterEndCallbackList.end(); it++) {
488 if ((*it)->id == callbackId) {
489 photoOutputCallback_->frameShutterEndCallbackList.erase(it);
490 break;
491 }
492 }
493 }
494
OffAllFrameShutterEnd()495 void CJPhotoOutput::OffAllFrameShutterEnd()
496 {
497 if (photoOutputCallback_ == nullptr) {
498 return;
499 }
500 std::lock_guard<std::mutex> lock(photoOutputCallback_->frameShutterEndMutex);
501 photoOutputCallback_->frameShutterEndCallbackList.clear();
502 }
503
OnCaptureReady(int64_t callbackId)504 void CJPhotoOutput::OnCaptureReady(int64_t callbackId)
505 {
506 if (photoOutputCallback_ == nullptr) {
507 photoOutputCallback_ = std::make_shared<CJPhotoOutputCallback>();
508 if (photoOutputCallback_ == nullptr || photoOutput_ == nullptr) {
509 return;
510 }
511 photoOutput_->SetCallback(photoOutputCallback_);
512 }
513 auto cFunc = reinterpret_cast<void (*)()>(callbackId);
514 auto callback = [lambda = CJLambda::Create(cFunc)]() -> void { lambda(); };
515 auto callbackRef = std::make_shared<CallbackRef<>>(callback, callbackId);
516
517 std::lock_guard<std::mutex> lock(photoOutputCallback_->captureReadyMutex);
518 photoOutputCallback_->captureReadyCallbackList.push_back(callbackRef);
519 }
520
OffCaptureReady(int64_t callbackId)521 void CJPhotoOutput::OffCaptureReady(int64_t callbackId)
522 {
523 if (photoOutputCallback_ == nullptr) {
524 return;
525 }
526 std::lock_guard<std::mutex> lock(photoOutputCallback_->captureReadyMutex);
527 for (auto it = photoOutputCallback_->captureReadyCallbackList.begin();
528 it != photoOutputCallback_->captureReadyCallbackList.end(); it++) {
529 if ((*it)->id == callbackId) {
530 photoOutputCallback_->captureReadyCallbackList.erase(it);
531 break;
532 }
533 }
534 }
535
OffAllCaptureReady()536 void CJPhotoOutput::OffAllCaptureReady()
537 {
538 if (photoOutputCallback_ == nullptr) {
539 return;
540 }
541 std::lock_guard<std::mutex> lock(photoOutputCallback_->captureReadyMutex);
542 photoOutputCallback_->captureReadyCallbackList.clear();
543 }
544
OnEstimatedCaptureDuration(int64_t callbackId)545 void CJPhotoOutput::OnEstimatedCaptureDuration(int64_t callbackId)
546 {
547 if (photoOutputCallback_ == nullptr) {
548 photoOutputCallback_ = std::make_shared<CJPhotoOutputCallback>();
549 if (photoOutputCallback_ == nullptr || photoOutput_ == nullptr) {
550 return;
551 }
552 photoOutput_->SetCallback(photoOutputCallback_);
553 }
554 auto cFunc = reinterpret_cast<void (*)(const int32_t duration)>(callbackId);
555 auto callback = [lambda = CJLambda::Create(cFunc)](const int32_t duration) -> void { lambda(duration); };
556 auto callbackRef = std::make_shared<CallbackRef<const int32_t>>(callback, callbackId);
557
558 std::lock_guard<std::mutex> lock(photoOutputCallback_->estimatedCaptureDurationMutex);
559 photoOutputCallback_->estimatedCaptureDurationCallbackList.push_back(callbackRef);
560 }
561
OffEstimatedCaptureDuration(int64_t callbackId)562 void CJPhotoOutput::OffEstimatedCaptureDuration(int64_t callbackId)
563 {
564 if (photoOutputCallback_ == nullptr) {
565 return;
566 }
567 std::lock_guard<std::mutex> lock(photoOutputCallback_->estimatedCaptureDurationMutex);
568 for (auto it = photoOutputCallback_->estimatedCaptureDurationCallbackList.begin();
569 it != photoOutputCallback_->estimatedCaptureDurationCallbackList.end(); it++) {
570 if ((*it)->id == callbackId) {
571 photoOutputCallback_->estimatedCaptureDurationCallbackList.erase(it);
572 break;
573 }
574 }
575 }
576
OffAllEstimatedCaptureDuration()577 void CJPhotoOutput::OffAllEstimatedCaptureDuration()
578 {
579 if (photoOutputCallback_ == nullptr) {
580 return;
581 }
582 std::lock_guard<std::mutex> lock(photoOutputCallback_->estimatedCaptureDurationMutex);
583 photoOutputCallback_->estimatedCaptureDurationCallbackList.clear();
584 }
585
OnError(int64_t callbackId)586 void CJPhotoOutput::OnError(int64_t callbackId)
587 {
588 if (photoOutputCallback_ == nullptr) {
589 photoOutputCallback_ = std::make_shared<CJPhotoOutputCallback>();
590 if (photoOutputCallback_ == nullptr || photoOutput_ == nullptr) {
591 return;
592 }
593 photoOutput_->SetCallback(photoOutputCallback_);
594 }
595 auto cFunc = reinterpret_cast<void (*)(const int32_t errorCode)>(callbackId);
596 auto callback = [lambda = CJLambda::Create(cFunc)](const int32_t errorCode) -> void { lambda(errorCode); };
597 auto callbackRef = std::make_shared<CallbackRef<const int32_t>>(callback, callbackId);
598
599 std::lock_guard<std::mutex> lock(photoOutputCallback_->captureErrorMutex);
600 photoOutputCallback_->captureErrorCallbackList.push_back(callbackRef);
601 }
602
OffError(int64_t callbackId)603 void CJPhotoOutput::OffError(int64_t callbackId)
604 {
605 if (photoOutputCallback_ == nullptr) {
606 return;
607 }
608 std::lock_guard<std::mutex> lock(photoOutputCallback_->captureErrorMutex);
609 for (auto it = photoOutputCallback_->captureErrorCallbackList.begin();
610 it != photoOutputCallback_->captureErrorCallbackList.end(); it++) {
611 if ((*it)->id == callbackId) {
612 photoOutputCallback_->captureErrorCallbackList.erase(it);
613 break;
614 }
615 }
616 }
617
OffAllError()618 void CJPhotoOutput::OffAllError()
619 {
620 if (photoOutputCallback_ == nullptr) {
621 return;
622 }
623 std::lock_guard<std::mutex> lock(photoOutputCallback_->captureErrorMutex);
624 photoOutputCallback_->captureErrorCallbackList.clear();
625 }
626
627 } // namespace CameraStandard
628 } // namespace OHOS