1 /*
2 * Copyright (c) 2021-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 "output/photo_output.h"
17
18 #include <mutex>
19 #include <securec.h>
20
21 #include "photo_output_callback.h"
22 #include "camera_error_code.h"
23 #include "camera_log.h"
24 #include "camera_manager.h"
25 #include "camera_output_capability.h"
26 #include "camera_util.h"
27 #include "capture_scene_const.h"
28 #include "input/camera_device.h"
29 #include "session/capture_session.h"
30 #include "picture_interface.h"
31 #include "task_manager.h"
32 #include "dp_utils.h"
33 #include "display/graphic/common/v1_0/cm_color_space.h"
34 #include "display/graphic/common/v2_1/cm_color_space.h"
35 #include <pixel_map.h>
36 using namespace std;
37
38 namespace OHOS {
39 namespace CameraStandard {
40 constexpr uint32_t CAPTURE_TIMEOUT = 1;
PhotoCaptureSetting()41 PhotoCaptureSetting::PhotoCaptureSetting()
42 {
43 int32_t items = 10;
44 int32_t dataLength = 100;
45 captureMetadataSetting_ = std::make_shared<Camera::CameraMetadata>(items, dataLength);
46 location_ = std::make_shared<Location>();
47 }
48
GetQuality()49 PhotoCaptureSetting::QualityLevel PhotoCaptureSetting::GetQuality()
50 {
51 QualityLevel quality = QUALITY_LEVEL_LOW;
52 camera_metadata_item_t item;
53
54 int ret = Camera::FindCameraMetadataItem(captureMetadataSetting_->get(), OHOS_JPEG_QUALITY, &item);
55 CHECK_RETURN_RET(ret != CAM_META_SUCCESS, QUALITY_LEVEL_MEDIUM);
56 if (item.data.u8[0] == OHOS_CAMERA_JPEG_LEVEL_HIGH) {
57 quality = QUALITY_LEVEL_HIGH;
58 } else if (item.data.u8[0] == OHOS_CAMERA_JPEG_LEVEL_MIDDLE) {
59 quality = QUALITY_LEVEL_MEDIUM;
60 }
61 return quality;
62 }
63
SetQuality(PhotoCaptureSetting::QualityLevel qualityLevel)64 void PhotoCaptureSetting::SetQuality(PhotoCaptureSetting::QualityLevel qualityLevel)
65 {
66 uint8_t quality = OHOS_CAMERA_JPEG_LEVEL_LOW;
67 if (qualityLevel == QUALITY_LEVEL_HIGH) {
68 quality = OHOS_CAMERA_JPEG_LEVEL_HIGH;
69 } else if (qualityLevel == QUALITY_LEVEL_MEDIUM) {
70 quality = OHOS_CAMERA_JPEG_LEVEL_MIDDLE;
71 }
72 bool status = AddOrUpdateMetadata(captureMetadataSetting_, OHOS_JPEG_QUALITY, &quality, 1);
73 CHECK_PRINT_ELOG(!status, "PhotoCaptureSetting::SetQuality Failed to set Quality");
74 }
75
GetRotation()76 PhotoCaptureSetting::RotationConfig PhotoCaptureSetting::GetRotation()
77 {
78 RotationConfig rotation;
79 camera_metadata_item_t item;
80
81 int ret = Camera::FindCameraMetadataItem(captureMetadataSetting_->get(), OHOS_JPEG_ORIENTATION, &item);
82 if (ret == CAM_META_SUCCESS) {
83 rotation = static_cast<RotationConfig>(item.data.i32[0]);
84 return rotation;
85 }
86 return RotationConfig::Rotation_0;
87 }
88
SetRotation(PhotoCaptureSetting::RotationConfig rotationValue)89 void PhotoCaptureSetting::SetRotation(PhotoCaptureSetting::RotationConfig rotationValue)
90 {
91 int32_t rotation = rotationValue;
92 bool status = AddOrUpdateMetadata(captureMetadataSetting_, OHOS_JPEG_ORIENTATION, &rotation, 1);
93 CHECK_PRINT_ELOG(!status, "PhotoCaptureSetting::SetRotation Failed to set Rotation");
94 return;
95 }
96
SetGpsLocation(double latitude,double longitude)97 void PhotoCaptureSetting::SetGpsLocation(double latitude, double longitude)
98 {
99 std::shared_ptr<Location> location = std::make_shared<Location>();
100 location->latitude = latitude;
101 location->longitude = longitude;
102 location->altitude = 0;
103 SetLocation(location);
104 }
105
SetLocation(std::shared_ptr<Location> & location)106 void PhotoCaptureSetting::SetLocation(std::shared_ptr<Location>& location)
107 {
108 CHECK_RETURN(location == nullptr);
109 std::lock_guard<std::mutex> lock(locationMutex_);
110 location_ = location;
111 std::vector<double> gpsCoordinates = {location->latitude, location->longitude, location->altitude};
112 MEDIA_DEBUG_LOG("PhotoCaptureSetting::SetLocation lat=%{private}f, long=%{private}f and alt=%{private}f",
113 location_->latitude, location_->longitude, location_->altitude);
114 bool status = AddOrUpdateMetadata(
115 captureMetadataSetting_, OHOS_JPEG_GPS_COORDINATES, gpsCoordinates.data(), gpsCoordinates.size());
116 CHECK_PRINT_ELOG(!status, "PhotoCaptureSetting::SetLocation Failed to set GPS co-ordinates");
117 }
118
GetLocation(std::shared_ptr<Location> & location)119 void PhotoCaptureSetting::GetLocation(std::shared_ptr<Location>& location)
120 {
121 // LCOV_EXCL_START
122 std::lock_guard<std::mutex> lock(locationMutex_);
123 location = location_;
124 MEDIA_DEBUG_LOG("PhotoCaptureSetting::GetLocation lat=%{private}f, long=%{private}f and alt=%{private}f",
125 location->latitude, location->longitude, location->altitude);
126 // LCOV_EXCL_STOP
127 }
128
129
SetMirror(bool enable)130 void PhotoCaptureSetting::SetMirror(bool enable)
131 {
132 uint8_t mirror = enable;
133 MEDIA_DEBUG_LOG("PhotoCaptureSetting::SetMirror value=%{public}d", enable);
134 bool status = AddOrUpdateMetadata(captureMetadataSetting_, OHOS_CONTROL_CAPTURE_MIRROR, &mirror, 1);
135 CHECK_PRINT_ELOG(!status, "PhotoCaptureSetting::SetMirror Failed to set mirroring in photo capture setting");
136 return;
137 }
138
GetMirror()139 bool PhotoCaptureSetting::GetMirror()
140 {
141 bool isMirror;
142 camera_metadata_item_t item;
143 int ret = Camera::FindCameraMetadataItem(captureMetadataSetting_->get(), OHOS_CONTROL_CAPTURE_MIRROR, &item);
144 if (ret == CAM_META_SUCCESS) {
145 isMirror = static_cast<bool>(item.data.u8[0]);
146 return isMirror;
147 }
148 return false;
149 }
150
GetCaptureMetadataSetting()151 std::shared_ptr<Camera::CameraMetadata> PhotoCaptureSetting::GetCaptureMetadataSetting()
152 {
153 return captureMetadataSetting_;
154 }
155
SetBurstCaptureState(uint8_t burstState)156 void PhotoCaptureSetting::SetBurstCaptureState(uint8_t burstState)
157 {
158 CAMERA_SYNC_TRACE;
159 MEDIA_INFO_LOG("SetBurstCaptureState");
160 bool status = AddOrUpdateMetadata(captureMetadataSetting_, OHOS_CONTROL_BURST_CAPTURE, &burstState, 1);
161 CHECK_PRINT_ELOG(!status, "PhotoCaptureSetting::SetBurstCaptureState Failed");
162 return;
163 }
164
OnCaptureStarted(const int32_t captureId)165 int32_t HStreamCaptureCallbackImpl::OnCaptureStarted(const int32_t captureId)
166 {
167 CAMERA_SYNC_TRACE;
168 auto photoOutput = GetPhotoOutput();
169 CHECK_RETURN_RET_ELOG(photoOutput == nullptr, CAMERA_OK,
170 "HStreamCaptureCallbackImpl::OnCaptureStarted photoOutput is nullptr");
171 auto callback = photoOutput->GetApplicationCallback();
172 CHECK_RETURN_RET_ELOG(callback == nullptr, CAMERA_OK,
173 "HStreamCaptureCallbackImpl::OnCaptureStarted callback is nullptr");
174
175 // LCOV_EXCL_START
176 sptr<CaptureSession> session = photoOutput->GetSession();
177 CHECK_RETURN_RET_ELOG(session == nullptr, CAMERA_OK,
178 "HStreamCaptureCallbackImpl::OnCaptureStarted session is nullptr");
179 switch (session->GetMode()) {
180 case SceneMode::HIGH_RES_PHOTO: {
181 auto inputDevice = session->GetInputDevice();
182 CHECK_RETURN_RET_ELOG(inputDevice == nullptr, CAMERA_OK,
183 "HStreamCaptureCallbackImpl::OnCaptureStarted inputDevice is nullptr");
184 sptr<CameraDevice> cameraObj = inputDevice->GetCameraDeviceInfo();
185 CHECK_RETURN_RET_ELOG(cameraObj == nullptr, CAMERA_OK,
186 "HStreamCaptureCallbackImpl::OnCaptureStarted cameraObj is nullptr");
187 std::shared_ptr<Camera::CameraMetadata> metadata = cameraObj->GetCachedMetadata();
188 CHECK_RETURN_RET_ELOG(metadata == nullptr, CAMERA_OK,
189 "HStreamCaptureCallbackImpl::OnCaptureStarted metadata is nullptr");
190 camera_metadata_item_t meta;
191 int32_t ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_CAPTURE_EXPECT_TIME, &meta);
192 const int32_t CONST_2 = 2;
193 if (ret == CAM_META_SUCCESS && meta.count >= CONST_2) {
194 callback->OnCaptureStarted(captureId, meta.data.ui32[1]);
195 } else if (meta.count) {
196 MEDIA_WARNING_LOG("Discarding OnCaptureStarted callback, mode:%{public}d. exposureTime is not found",
197 meta.data.ui32[0]);
198 } else {
199 MEDIA_WARNING_LOG("Discarding OnCaptureStarted callback, mode and exposureTime are not found");
200 }
201 break;
202 }
203 default:
204 callback->OnCaptureStarted(captureId);
205 break;
206 }
207 // LCOV_EXCL_STOP
208 return CAMERA_OK;
209 }
210
OnCaptureStarted(const int32_t captureId,uint32_t exposureTime)211 int32_t HStreamCaptureCallbackImpl::OnCaptureStarted(const int32_t captureId, uint32_t exposureTime)
212 {
213 CAMERA_SYNC_TRACE;
214 auto photoOutput = GetPhotoOutput();
215 CHECK_RETURN_RET_ELOG(photoOutput == nullptr, CAMERA_OK,
216 "HStreamCaptureCallbackImpl::OnCaptureStarted photoOutput is nullptr");
217 auto callback = photoOutput->GetApplicationCallback();
218 CHECK_RETURN_RET_ELOG(callback == nullptr, CAMERA_OK,
219 "HStreamCaptureCallbackImpl::OnCaptureStarted callback is nullptr");
220 // LCOV_EXCL_START
221 photoOutput->GetApplicationCallback()->OnCaptureStarted(captureId, exposureTime);
222 return CAMERA_OK;
223 // LCOV_EXCL_STOP
224 }
225
OnCaptureEnded(const int32_t captureId,const int32_t frameCount)226 int32_t HStreamCaptureCallbackImpl::OnCaptureEnded(const int32_t captureId, const int32_t frameCount)
227 {
228 CAMERA_SYNC_TRACE;
229 auto photoOutput = GetPhotoOutput();
230 CHECK_RETURN_RET_ELOG(photoOutput == nullptr, CAMERA_OK,
231 "HStreamCaptureCallbackImpl::OnCaptureEnded photoOutput is nullptr");
232 auto callback = photoOutput->GetApplicationCallback();
233 CHECK_RETURN_RET_ELOG(callback == nullptr, CAMERA_OK,
234 "HStreamCaptureCallbackImpl::OnCaptureEnded callback is nullptr");
235 // LCOV_EXCL_START
236 callback->OnCaptureEnded(captureId, frameCount);
237 auto timeStartIter = photoOutput->captureIdToCaptureInfoMap_.find(captureId);
238 if (timeStartIter != photoOutput->captureIdToCaptureInfoMap_.end()) {
239 DeferredProcessing::GetGlobalWatchdog().StopMonitor((timeStartIter->second).CaptureHandle);
240 }
241 return CAMERA_OK;
242 // LCOV_EXCL_STOP
243 }
244
OnCaptureError(const int32_t captureId,const int32_t errorCode)245 int32_t HStreamCaptureCallbackImpl::OnCaptureError(const int32_t captureId, const int32_t errorCode)
246 {
247 auto photoOutput = GetPhotoOutput();
248 CHECK_RETURN_RET_ELOG(photoOutput == nullptr, CAMERA_OK,
249 "HStreamCaptureCallbackImpl::OnCaptureError photoOutput is nullptr");
250 auto callback = photoOutput->GetApplicationCallback();
251 CHECK_RETURN_RET_ELOG(callback == nullptr, CAMERA_OK,
252 "HStreamCaptureCallbackImpl::OnCaptureError callback is nullptr");
253 // LCOV_EXCL_START
254 callback->OnCaptureError(captureId, errorCode);
255 return CAMERA_OK;
256 // LCOV_EXCL_STOP
257 }
258
OnFrameShutter(const int32_t captureId,const uint64_t timestamp)259 int32_t HStreamCaptureCallbackImpl::OnFrameShutter(const int32_t captureId, const uint64_t timestamp)
260 {
261 CAMERA_SYNC_TRACE;
262 auto photoOutput = GetPhotoOutput();
263 CHECK_RETURN_RET_ELOG(photoOutput == nullptr, CAMERA_OK,
264 "HStreamCaptureCallbackImpl::OnFrameShutter photoOutput is nullptr");
265 auto callback = photoOutput->GetApplicationCallback();
266 CHECK_RETURN_RET_ELOG(callback == nullptr, CAMERA_OK,
267 "HStreamCaptureCallbackImpl::OnFrameShutter callback is nullptr");
268 // LCOV_EXCL_START
269 callback->OnFrameShutter(captureId, timestamp);
270 return CAMERA_OK;
271 // LCOV_EXCL_STOP
272 }
273
OnFrameShutterEnd(const int32_t captureId,const uint64_t timestamp)274 int32_t HStreamCaptureCallbackImpl::OnFrameShutterEnd(const int32_t captureId, const uint64_t timestamp)
275 {
276 CAMERA_SYNC_TRACE;
277 auto photoOutput = GetPhotoOutput();
278 CHECK_RETURN_RET_ELOG(photoOutput == nullptr, CAMERA_OK,
279 "HStreamCaptureCallbackImpl::OnFrameShutterEnd photoOutput is nullptr");
280 auto callback = photoOutput->GetApplicationCallback();
281 CHECK_RETURN_RET_ELOG(callback == nullptr, CAMERA_OK,
282 "HStreamCaptureCallbackImpl::OnFrameShutterEnd callback is nullptr");
283 // LCOV_EXCL_START
284 callback->OnFrameShutterEnd(captureId, timestamp);
285 CHECK_RETURN_RET(!photoOutput->IsHasEnableOfflinePhoto(), CAMERA_OK);
286 uint32_t startCaptureHandle;
287 constexpr uint32_t delayMilli = 10 * 1000; // 10S 1000 is ms
288 MEDIA_INFO_LOG("offline GetGlobalWatchdog StartMonitor, captureId=%{public}d", captureId);
289 DeferredProcessing::GetGlobalWatchdog().StartMonitor(
290 startCaptureHandle, delayMilli, [captureId, photoOutput](uint32_t handle) {
291 MEDIA_INFO_LOG("offline Watchdog executed, handle: %{public}d, captureId= %{public}d",
292 static_cast<int>(handle), captureId);
293 CHECK_RETURN_ELOG(photoOutput == nullptr, "photoOutput is release");
294 bool canRelease =
295 photoOutput->IsHasSwitchOfflinePhoto() && (photoOutput->captureIdToCaptureInfoMap_).size() == 0;
296 CHECK_EXECUTE(canRelease, photoOutput->Release());
297 });
298 photoOutput->captureIdToCaptureInfoMap_[captureId].CaptureHandle = static_cast<int32_t>(startCaptureHandle);
299 photoOutput->captureIdToCaptureInfoMap_[captureId].timeStart = std::chrono::steady_clock::now();
300 return CAMERA_OK;
301 // LCOV_EXCL_STOP
302 }
303
OnCaptureReady(const int32_t captureId,const uint64_t timestamp)304 int32_t HStreamCaptureCallbackImpl::OnCaptureReady(const int32_t captureId, const uint64_t timestamp)
305 {
306 CAMERA_SYNC_TRACE;
307 auto photoOutput = GetPhotoOutput();
308 CHECK_RETURN_RET_ELOG(photoOutput == nullptr, CAMERA_OK,
309 "HStreamCaptureCallbackImpl::OnCaptureReady photoOutput is nullptr");
310 auto callback = photoOutput->GetApplicationCallback();
311 CHECK_RETURN_RET_ELOG(callback == nullptr, CAMERA_OK,
312 "HStreamCaptureCallbackImpl::OnCaptureReady callback is nullptr");
313 // LCOV_EXCL_START
314 callback->OnCaptureReady(captureId, timestamp);
315 return CAMERA_OK;
316 // LCOV_EXCL_STOP
317 }
318
OnOfflineDeliveryFinished(const int32_t captureId)319 int32_t HStreamCaptureCallbackImpl::OnOfflineDeliveryFinished(const int32_t captureId)
320 {
321 // LCOV_EXCL_START
322 CAMERA_SYNC_TRACE;
323 auto photoOutput = GetPhotoOutput();
324 CHECK_RETURN_RET_ELOG(photoOutput == nullptr, CAMERA_OK,
325 "HStreamCaptureCallbackImpl::OnOfflineDeliveryFinished photoOutput is nullptr");
326 auto callback = photoOutput->GetApplicationCallback();
327 CHECK_RETURN_RET_ELOG(callback == nullptr, CAMERA_OK,
328 "HStreamCaptureCallbackImpl::OnOfflineDeliveryFinished callback is nullptr");
329 callback->OnOfflineDeliveryFinished(captureId);
330 return CAMERA_OK;
331 // LCOV_EXCL_STOP
332 }
333
PhotoOutput(sptr<IBufferProducer> bufferProducer)334 PhotoOutput::PhotoOutput(sptr<IBufferProducer> bufferProducer)
335 : CaptureOutput(CAPTURE_OUTPUT_TYPE_PHOTO, StreamType::CAPTURE, bufferProducer, nullptr)
336 {
337 defaultCaptureSetting_ = nullptr;
338 }
339
PhotoOutput(sptr<IBufferProducer> bufferProducer,sptr<Surface> photoSurface)340 PhotoOutput::PhotoOutput(sptr<IBufferProducer> bufferProducer, sptr<Surface> photoSurface)
341 : CaptureOutput(CAPTURE_OUTPUT_TYPE_PHOTO, StreamType::CAPTURE, bufferProducer, nullptr)
342 {
343 // LCOV_EXCL_START
344 defaultCaptureSetting_ = nullptr;
345 photoSurface_ = photoSurface;
346 // LCOV_EXCL_STOP
347 }
348
PhotoOutput()349 PhotoOutput::PhotoOutput()
350 : CaptureOutput(CAPTURE_OUTPUT_TYPE_PHOTO, StreamType::CAPTURE, nullptr)
351 {
352 MEDIA_INFO_LOG("new PhotoOutput");
353 isSurfaceOnService_ = true;
354 }
355
~PhotoOutput()356 PhotoOutput::~PhotoOutput()
357 {
358 // LCOV_EXCL_START
359 MEDIA_DEBUG_LOG("Enter Into PhotoOutput::~PhotoOutput()");
360 defaultCaptureSetting_ = nullptr;
361 // LCOV_EXCL_STOP
362 }
363
SetNativeSurface(bool isNativeSurface)364 void PhotoOutput::SetNativeSurface(bool isNativeSurface)
365 {
366 MEDIA_INFO_LOG("Enter Into SetNativeSurface %{public}d", isNativeSurface);
367 isNativeSurface_ = isNativeSurface;
368 }
369
SetCallbackFlag(uint8_t callbackFlag)370 void PhotoOutput::SetCallbackFlag(uint8_t callbackFlag)
371 {
372 std::lock_guard<std::mutex> lock(callbackMutex_);
373 CHECK_RETURN_ELOG(!isNativeSurface_, "SetCallbackFlag when register imageReciver");
374 // LCOV_EXCL_START
375 bool beforeStatus = IsEnableDeferred();
376 callbackFlag_ = callbackFlag;
377 bool afterStatus = IsEnableDeferred();
378 // if session is commit or start, and isEnableDeferred is oppsite, need to restart session config
379 auto session = GetSession();
380 if (beforeStatus != afterStatus && session) {
381 FocusMode focusMode = session->GetFocusMode();
382 FlashMode flashMode = session->GetFlashMode();
383 MEDIA_INFO_LOG("session restart when callback status changed %{public}d, %{public}d", focusMode, flashMode);
384 if (session->IsSessionStarted()) {
385 session->BeginConfig();
386 session->CommitConfig();
387 session->LockForControl();
388 session->SetFocusMode(focusMode);
389 session->SetFlashMode(flashMode);
390 session->UnlockForControl();
391 session->Start();
392 } else if (session->IsSessionCommited()) {
393 MEDIA_INFO_LOG("session recommit when callback status changed");
394 session->BeginConfig();
395 session->CommitConfig();
396 session->LockForControl();
397 session->SetFocusMode(focusMode);
398 session->SetFlashMode(flashMode);
399 session->UnlockForControl();
400 }
401 }
402 // LCOV_EXCL_STOP
403 }
404
IsYuvOrHeifPhoto()405 bool PhotoOutput::IsYuvOrHeifPhoto()
406 {
407 CHECK_RETURN_RET(!GetPhotoProfile(), false);
408 bool ret = GetPhotoProfile()->GetCameraFormat() == CAMERA_FORMAT_YUV_420_SP;
409 MEDIA_INFO_LOG("IsYuvOrHeifPhoto res = %{public}d", ret);
410 return ret;
411 }
412
SetAuxiliaryPhotoHandle(uint32_t handle)413 void PhotoOutput::SetAuxiliaryPhotoHandle(uint32_t handle)
414 {
415 std::lock_guard<std::mutex> lock(watchDogHandleMutex_);
416 watchDogHandle_ = handle;
417 }
418
419
GetAuxiliaryPhotoHandle()420 uint32_t PhotoOutput::GetAuxiliaryPhotoHandle()
421 {
422 std::lock_guard<std::mutex> lock(watchDogHandleMutex_);
423 return watchDogHandle_;
424 }
425
426 template<typename T>
CastStream(sptr<IStreamCommon> streamCommon)427 sptr<T> CastStream(sptr<IStreamCommon> streamCommon)
428 {
429 CHECK_RETURN_RET(streamCommon == nullptr, nullptr);
430 return static_cast<T*>(streamCommon.GetRefPtr());
431 }
432
CreateMultiChannel()433 void PhotoOutput::CreateMultiChannel()
434 {
435 CAMERA_SYNC_TRACE;
436 auto streamCapturePtr = CastStream<IStreamCapture>(GetStream());
437 CHECK_RETURN_ELOG(
438 streamCapturePtr == nullptr, "PhotoOutput::CreateMultiChannel Failed!streamCapturePtr is nullptr");
439 // LCOV_EXCL_START
440 std::string retStr = "";
441 int32_t ret = 0;
442 if (gainmapSurface_ == nullptr) {
443 std::string bufferName = "gainmapImage";
444 gainmapSurface_ = Surface::CreateSurfaceAsConsumer(bufferName);
445 ret = streamCapturePtr->SetBufferProducerInfo(bufferName, gainmapSurface_->GetProducer());
446 retStr += (ret != CAMERA_OK ? bufferName + "," : retStr);
447 }
448 if (isDepthBufferSupported_ && deepSurface_ == nullptr) {
449 std::string bufferName = "deepImage";
450 deepSurface_ = Surface::CreateSurfaceAsConsumer(bufferName);
451 ret = streamCapturePtr->SetBufferProducerInfo(bufferName, deepSurface_->GetProducer());
452 retStr += (ret != CAMERA_OK ? bufferName + "," : retStr);
453 }
454 if (exifSurface_ == nullptr) {
455 std::string bufferName = "exifImage";
456 exifSurface_ = Surface::CreateSurfaceAsConsumer(bufferName);
457 ret = streamCapturePtr->SetBufferProducerInfo(bufferName, exifSurface_->GetProducer());
458 retStr += (ret != CAMERA_OK ? bufferName + "," : retStr);
459 }
460 if (debugSurface_ == nullptr) {
461 std::string bufferName = "debugImage";
462 debugSurface_ = Surface::CreateSurfaceAsConsumer(bufferName);
463 ret = streamCapturePtr->SetBufferProducerInfo(bufferName, debugSurface_->GetProducer());
464 retStr += (ret != CAMERA_OK ? bufferName + "," : retStr);
465 }
466 MEDIA_INFO_LOG("PhotoOutput::CreateMultiChannel! failed channel is = %{public}s", retStr.c_str());
467 // LCOV_EXCL_STOP
468 }
469
IsEnableDeferred()470 bool PhotoOutput::IsEnableDeferred()
471 {
472 CHECK_RETURN_RET(!isNativeSurface_, false);
473 bool isEnabled = (callbackFlag_ & CAPTURE_PHOTO_ASSET) != 0 || (callbackFlag_ & CAPTURE_PHOTO) == 0;
474 MEDIA_INFO_LOG("Enter Into PhotoOutput::IsEnableDeferred %{public}d", isEnabled);
475 return isEnabled;
476 }
477
SetCallback(std::shared_ptr<PhotoStateCallback> callback)478 void PhotoOutput::SetCallback(std::shared_ptr<PhotoStateCallback> callback)
479 {
480 std::lock_guard<std::mutex> lock(outputCallbackMutex_);
481 appCallback_ = callback;
482 if (appCallback_ != nullptr) {
483 if (cameraSvcCallback_ == nullptr) {
484 cameraSvcCallback_ = new (std::nothrow) HStreamCaptureCallbackImpl(this);
485 // LCOV_EXCL_START
486 if (cameraSvcCallback_ == nullptr) {
487 MEDIA_ERR_LOG("PhotoOutput::SetCallback new HStreamCaptureCallbackImpl Failed to register callback");
488 appCallback_ = nullptr;
489 return;
490 }
491 // LCOV_EXCL_STOP
492 }
493 auto itemStream = CastStream<IStreamCapture>(GetStream());
494 int32_t errorCode = CAMERA_OK;
495 if (itemStream) {
496 errorCode = itemStream->SetCallback(cameraSvcCallback_);
497 } else {
498 MEDIA_ERR_LOG("PhotoOutput::SetCallback() itemStream is nullptr");
499 }
500 // LCOV_EXCL_START
501 CHECK_RETURN(errorCode == CAMERA_OK);
502 MEDIA_ERR_LOG("PhotoOutput::SetCallback: Failed to register callback, errorCode: %{public}d", errorCode);
503 cameraSvcCallback_ = nullptr;
504 appCallback_ = nullptr;
505 // LCOV_EXCL_STOP
506 }
507 }
508
SetPhotoNativeConsumer()509 void PhotoOutput::SetPhotoNativeConsumer()
510 {
511 MEDIA_DEBUG_LOG("SetPhotoNativeConsumer E");
512 CHECK_RETURN_ELOG(photoSurface_ == nullptr, "SetPhotoNativeConsumer err, surface is null");
513 CHECK_RETURN(photoNativeConsumer_ != nullptr);
514 photoSurface_->UnregisterConsumerListener();
515 photoNativeConsumer_ = new (std::nothrow) PhotoNativeConsumer(wptr<PhotoOutput>(this));
516 SurfaceError ret = photoSurface_->RegisterConsumerListener((sptr<IBufferConsumerListener> &)photoNativeConsumer_);
517 CHECK_PRINT_ELOG(ret != SURFACE_ERROR_OK, "SetPhotoNativeConsumer failed:%{public}d", ret);
518 }
519
SetPhotoAvailableCallback(std::shared_ptr<PhotoAvailableCallback> callback)520 void PhotoOutput::SetPhotoAvailableCallback(std::shared_ptr<PhotoAvailableCallback> callback)
521 {
522 MEDIA_DEBUG_LOG("SetPhotoAvailableCallback E");
523 CHECK_RETURN_ELOG(callback == nullptr, "photo callback nullptr");
524 std::lock_guard<std::mutex> lock(outputCallbackMutex_);
525 appPhotoCallback_ = nullptr;
526 svcPhotoCallback_ = nullptr;
527 appPhotoCallback_ = callback;
528 if (isSurfaceOnService_) {
529 SetPhotoAvailableInSvc();
530 } else {
531 SetPhotoNativeConsumer();
532 }
533 }
534
SetPhotoAvailableInSvc()535 void PhotoOutput::SetPhotoAvailableInSvc()
536 {
537 MEDIA_DEBUG_LOG("SetPhotoAvailableInSvc E");
538 svcPhotoCallback_ = new (std::nothrow) HStreamCapturePhotoCallbackImpl(this);
539 CHECK_RETURN_ELOG(svcPhotoCallback_ == nullptr, "new photo svc callback err");
540 auto itemStream = CastStream<IStreamCapture>(GetStream());
541 int32_t errorCode = CAMERA_OK;
542 if (itemStream) {
543 errorCode = itemStream->SetPhotoAvailableCallback(svcPhotoCallback_);
544 } else {
545 reSetFlag_ = RESET_PHOTO;
546 MEDIA_ERR_LOG("SetPhotoAvailableCallback itemStream is nullptr");
547 }
548 if (errorCode != CAMERA_OK) {
549 MEDIA_ERR_LOG("SetPhotoAvailableCallback Failed to register callback, errorCode: %{public}d", errorCode);
550 svcPhotoCallback_ = nullptr;
551 appPhotoCallback_ = nullptr;
552 }
553 MEDIA_DEBUG_LOG("SetPhotoAvailableCallback X");
554 }
555
UnSetPhotoAvailableCallback()556 void PhotoOutput::UnSetPhotoAvailableCallback()
557 {
558 // LCOV_EXCL_START
559 MEDIA_DEBUG_LOG("UnSetPhotoAvailableCallback E");
560 std::lock_guard<std::mutex> lock(outputCallbackMutex_);
561 appPhotoCallback_ = nullptr;
562 svcPhotoCallback_ = nullptr;
563 auto itemStream = CastStream<IStreamCapture>(GetStream());
564 if (itemStream) {
565 itemStream->UnSetPhotoAvailableCallback();
566 }
567 // LCOV_EXCL_STOP
568 }
569
SetPhotoAssetAvailableCallback(std::shared_ptr<PhotoAssetAvailableCallback> callback)570 void PhotoOutput::SetPhotoAssetAvailableCallback(std::shared_ptr<PhotoAssetAvailableCallback> callback)
571 {
572 // LCOV_EXCL_START
573 MEDIA_DEBUG_LOG("SetPhotoAssetAvailableCallback E");
574 CHECK_RETURN_ELOG(callback == nullptr, "photoAsset callback nullptr");
575 std::lock_guard<std::mutex> lock(outputCallbackMutex_);
576 appPhotoAssetCallback_ = nullptr;
577 svcPhotoAssetCallback_ = nullptr;
578 appPhotoAssetCallback_ = callback;
579 if (isSurfaceOnService_) {
580 SetPhotoAssetAvailableInSvc();
581 } else {
582 SetPhotoNativeConsumer();
583 }
584 // LCOV_EXCL_STOP
585 }
586
SetPhotoAssetAvailableInSvc()587 void PhotoOutput::SetPhotoAssetAvailableInSvc()
588 {
589 MEDIA_DEBUG_LOG("SetPhotoAssetAvailableInSvc E");
590 svcPhotoAssetCallback_ = new (std::nothrow) HStreamCapturePhotoAssetCallbackImpl(this);
591 CHECK_RETURN_ELOG(svcPhotoAssetCallback_ == nullptr, "new photoAsset svc callback err");
592 auto itemStream = CastStream<IStreamCapture>(GetStream());
593 int32_t errorCode = CAMERA_OK;
594 if (itemStream) {
595 errorCode = itemStream->SetPhotoAssetAvailableCallback(svcPhotoAssetCallback_);
596 } else {
597 reSetFlag_ = RESET_PHOTO_ASSET;
598 MEDIA_ERR_LOG("SetPhotoAssetAvailableCallback itemStream is nullptr");
599 }
600 if (errorCode != CAMERA_OK) {
601 MEDIA_ERR_LOG("SetPhotoAssetAvailableCallback Failed to register callback, errorCode: %{public}d", errorCode);
602 svcPhotoAssetCallback_ = nullptr;
603 appPhotoAssetCallback_ = nullptr;
604 }
605 MEDIA_DEBUG_LOG("SetPhotoAssetAvailableCallback X");
606 }
607
UnSetPhotoAssetAvailableCallback()608 void PhotoOutput::UnSetPhotoAssetAvailableCallback()
609 {
610 // LCOV_EXCL_START
611 MEDIA_DEBUG_LOG("UnSetPhotoAssetAvailableCallback E");
612 std::lock_guard<std::mutex> lock(outputCallbackMutex_);
613 appPhotoAssetCallback_ = nullptr;
614 svcPhotoAssetCallback_ = nullptr;
615 auto itemStream = CastStream<IStreamCapture>(GetStream());
616 if (itemStream) {
617 itemStream->UnSetPhotoAssetAvailableCallback();
618 }
619 // LCOV_EXCL_STOP
620 }
621
ReSetSavedCallback()622 void PhotoOutput::ReSetSavedCallback()
623 {
624 MEDIA_DEBUG_LOG("ReSetSavedCallback E");
625 CHECK_RETURN_ELOG(!isSurfaceOnService_, "ReSetSavedCallback no need");
626 auto itemStream = CastStream<IStreamCapture>(GetStream());
627 CHECK_RETURN_ELOG(itemStream == nullptr, "ReSetSavedCallback faild");
628 int32_t errorCode = CAMERA_OK;
629 if (reSetFlag_ == RESET_PHOTO && appPhotoCallback_ && svcPhotoCallback_) {
630 errorCode = itemStream->SetPhotoAvailableCallback(svcPhotoCallback_);
631 } else if (reSetFlag_ == RESET_PHOTO_ASSET && appPhotoAssetCallback_ && svcPhotoAssetCallback_) {
632 errorCode = itemStream->SetPhotoAssetAvailableCallback(svcPhotoAssetCallback_);
633 }
634 reSetFlag_ = NO_NEED_RESET;
635 if (errorCode != CAMERA_OK) {
636 MEDIA_ERR_LOG("ReSetSavedCallback failed, errorCode: %{public}d", errorCode);
637 }
638 }
639
SetThumbnailCallback(std::shared_ptr<ThumbnailCallback> callback)640 void PhotoOutput::SetThumbnailCallback(std::shared_ptr<ThumbnailCallback> callback)
641 {
642 // LCOV_EXCL_START
643 MEDIA_DEBUG_LOG("SetThumbnailCallback E");
644 CHECK_RETURN_ELOG(callback == nullptr, "photoAsset callback nullptr");
645 std::lock_guard<std::mutex> lock(outputCallbackMutex_);
646 appThumbnailCallback_ = nullptr;
647 svcThumbnailCallback_ = nullptr;
648 appThumbnailCallback_ = callback;
649 svcThumbnailCallback_ = new (std::nothrow) HStreamCaptureThumbnailCallbackImpl(this);
650 CHECK_RETURN_ELOG(svcThumbnailCallback_ == nullptr, "new photoAsset svc callback err");
651 auto itemStream = CastStream<IStreamCapture>(GetStream());
652 int32_t errorCode = CAMERA_OK;
653 if (itemStream) {
654 errorCode = itemStream->SetThumbnailCallback(svcThumbnailCallback_);
655 } else {
656 MEDIA_ERR_LOG("SetThumbnailCallback itemStream is nullptr");
657 }
658 if (errorCode != CAMERA_OK) {
659 MEDIA_ERR_LOG("SetThumbnailCallback Failed to register callback, errorCode: %{public}d", errorCode);
660 svcThumbnailCallback_ = nullptr;
661 appThumbnailCallback_ = nullptr;
662 }
663 MEDIA_DEBUG_LOG("SetThumbnailCallback X");
664 // LCOV_EXCL_STOP
665 }
666
UnSetThumbnailAvailableCallback()667 void PhotoOutput::UnSetThumbnailAvailableCallback()
668 {
669 // LCOV_EXCL_START
670 MEDIA_DEBUG_LOG("UnSetThumbnailAvailableCallback E");
671 std::lock_guard<std::mutex> lock(outputCallbackMutex_);
672 appThumbnailCallback_ = nullptr;
673 svcThumbnailCallback_ = nullptr;
674 auto itemStream = CastStream<IStreamCapture>(GetStream());
675 if (itemStream) {
676 itemStream->UnSetThumbnailCallback();
677 }
678 // LCOV_EXCL_STOP
679 }
680
SetThumbnail(bool isEnabled)681 int32_t PhotoOutput::SetThumbnail(bool isEnabled)
682 {
683 CAMERA_SYNC_TRACE;
684 sptr<CameraDevice> cameraObj;
685 auto session = GetSession();
686 CHECK_RETURN_RET_ELOG(session == nullptr, SESSION_NOT_RUNNING,
687 "PhotoOutput SetThumbnail error!, session is nullptr");
688 // LCOV_EXCL_START
689 auto inputDevice = session->GetInputDevice();
690 CHECK_RETURN_RET_ELOG(inputDevice == nullptr, SESSION_NOT_RUNNING,
691 "PhotoOutput SetThumbnail error!, inputDevice is nullptr");
692 cameraObj = inputDevice->GetCameraDeviceInfo();
693 CHECK_RETURN_RET_ELOG(cameraObj == nullptr, SESSION_NOT_RUNNING,
694 "PhotoOutput SetThumbnail error!, cameraObj is nullptr");
695 auto streamCapturePtr = CastStream<IStreamCapture>(GetStream());
696 CHECK_RETURN_RET(streamCapturePtr == nullptr, SERVICE_FATL_ERROR);
697 return streamCapturePtr->SetThumbnail(isEnabled);
698 // LCOV_EXCL_STOP
699 }
700
EnableRawDelivery(bool enabled)701 int32_t PhotoOutput::EnableRawDelivery(bool enabled)
702 {
703 CAMERA_SYNC_TRACE;
704 MEDIA_DEBUG_LOG("enter into EnableRawDelivery");
705 auto session = GetSession();
706 CHECK_RETURN_RET_ELOG(session == nullptr, SESSION_NOT_RUNNING,
707 "PhotoOutput EnableRawDelivery error!, session is nullptr");
708 // LCOV_EXCL_START
709 auto streamCapturePtr = CastStream<IStreamCapture>(GetStream());
710 CHECK_RETURN_RET_ELOG(streamCapturePtr == nullptr, SERVICE_FATL_ERROR,
711 "PhotoOutput::EnableRawDelivery Failed to GetStream");
712 int32_t ret = CAMERA_OK;
713 if (session->EnableRawDelivery(enabled) == CameraErrorCode::SUCCESS) {
714 ret = streamCapturePtr->EnableRawDelivery(enabled);
715 CHECK_RETURN_RET_ELOG(ret != CAMERA_OK, SERVICE_FATL_ERROR,
716 "PhotoOutput::EnableRawDelivery session EnableRawDelivery Failed");
717 }
718 isRawImageDelivery_ = enabled;
719 return ret;
720 // LCOV_EXCL_STOP
721 }
722
EnableMovingPhoto(bool enabled)723 int32_t PhotoOutput::EnableMovingPhoto(bool enabled)
724 {
725 CAMERA_SYNC_TRACE;
726 int32_t ret = CAMERA_OK;
727 MEDIA_DEBUG_LOG("enter into EnableMovingPhoto");
728
729 auto streamCapturePtr = CastStream<IStreamCapture>(GetStream());
730 CHECK_RETURN_RET_ELOG(streamCapturePtr == nullptr, SERVICE_FATL_ERROR,
731 "PhotoOutput::EnableMovingPhoto Failed!streamCapturePtr is nullptr");
732 // LCOV_EXCL_START
733 ret = streamCapturePtr->EnableMovingPhoto(enabled);
734 CHECK_RETURN_RET_ELOG(ret != CAMERA_OK, SERVICE_FATL_ERROR,
735 "PhotoOutput::EnableMovingPhoto Failed");
736 return ret;
737 // LCOV_EXCL_STOP
738 }
739
GetApplicationCallback()740 std::shared_ptr<PhotoStateCallback> PhotoOutput::GetApplicationCallback()
741 {
742 // LCOV_EXCL_START
743 std::lock_guard<std::mutex> lock(outputCallbackMutex_);
744 return appCallback_;
745 // LCOV_EXCL_STOP
746 }
747
GetAppThumbnailCallback()748 std::shared_ptr<ThumbnailCallback> PhotoOutput::GetAppThumbnailCallback()
749 {
750 // LCOV_EXCL_START
751 std::lock_guard<std::mutex> lock(outputCallbackMutex_);
752 return appThumbnailCallback_;
753 // LCOV_EXCL_STOP
754 }
755
GetAppPhotoCallback()756 std::shared_ptr<PhotoAvailableCallback> PhotoOutput::GetAppPhotoCallback()
757 {
758 // LCOV_EXCL_START
759 std::lock_guard<std::mutex> lock(outputCallbackMutex_);
760 return appPhotoCallback_;
761 // LCOV_EXCL_STOP
762 }
763
GetAppPhotoAssetCallback()764 std::shared_ptr<PhotoAssetAvailableCallback> PhotoOutput::GetAppPhotoAssetCallback()
765 {
766 // LCOV_EXCL_START
767 std::lock_guard<std::mutex> lock(outputCallbackMutex_);
768 return appPhotoAssetCallback_;
769 // LCOV_EXCL_STOP
770 }
771
Capture(std::shared_ptr<PhotoCaptureSetting> photoCaptureSettings)772 int32_t PhotoOutput::Capture(std::shared_ptr<PhotoCaptureSetting> photoCaptureSettings)
773 {
774 std::lock_guard<std::mutex> lock(asyncOpMutex_);
775 auto session = GetSession();
776 CHECK_RETURN_RET_ELOG(session == nullptr || !session->IsSessionCommited(),
777 CameraErrorCode::SESSION_NOT_RUNNING, "PhotoOutput Failed to Capture with setting, session not commited");
778 // LCOV_EXCL_START
779 CHECK_RETURN_RET_ELOG(GetStream() == nullptr,
780 CameraErrorCode::SERVICE_FATL_ERROR, "PhotoOutput Failed to Capture with setting, GetStream is nullptr");
781 defaultCaptureSetting_ = photoCaptureSettings;
782 auto itemStream = CastStream<IStreamCapture>(GetStream());
783 int32_t errCode = CAMERA_UNKNOWN_ERROR;
784 CHECK_PRINT_ELOG(itemStream == nullptr, "PhotoOutput::Capture() itemStream is nullptr");
785 if (itemStream) {
786 MEDIA_INFO_LOG("Capture start");
787 session->EnableMovingPhotoMirror(photoCaptureSettings->GetMirror(), true);
788 errCode = itemStream->Capture(photoCaptureSettings->GetCaptureMetadataSetting());
789 MEDIA_INFO_LOG("Capture End");
790 }
791 CHECK_PRINT_ELOG(errCode != CAMERA_OK, "PhotoOutput Failed to Capture!, errCode: %{public}d", errCode);
792 return ServiceToCameraError(errCode);
793 // LCOV_EXCL_STOP
794 }
795
Capture()796 int32_t PhotoOutput::Capture()
797 {
798 std::lock_guard<std::mutex> lock(asyncOpMutex_);
799 auto session = GetSession();
800 CHECK_RETURN_RET_ELOG(session == nullptr || !session->IsSessionCommited(),
801 CameraErrorCode::SESSION_NOT_RUNNING, "PhotoOutput Failed to Capture, session not commited");
802 // LCOV_EXCL_START
803 CHECK_RETURN_RET_ELOG(GetStream() == nullptr, CameraErrorCode::SERVICE_FATL_ERROR,
804 "PhotoOutput Failed to Capture, GetStream is nullptr");
805 int32_t items = 0;
806 int32_t dataLength = 0;
807 std::shared_ptr<Camera::CameraMetadata> captureMetadataSetting =
808 std::make_shared<Camera::CameraMetadata>(items, dataLength);
809 auto itemStream = CastStream<IStreamCapture>(GetStream());
810 int32_t errCode = CAMERA_UNKNOWN_ERROR;
811 CHECK_PRINT_ELOG(itemStream == nullptr, "PhotoOutput::Capture() itemStream is nullptr");
812 if (itemStream) {
813 MEDIA_DEBUG_LOG("Capture start");
814 session->EnableMovingPhotoMirror(false, true);
815 errCode = itemStream->Capture(captureMetadataSetting);
816 MEDIA_DEBUG_LOG("Capture end");
817 }
818 CHECK_PRINT_ELOG(errCode != CAMERA_OK, "PhotoOutput Failed to Capture!, errCode: %{public}d", errCode);
819 return ServiceToCameraError(errCode);
820 // LCOV_EXCL_STOP
821 }
822
CancelCapture()823 int32_t PhotoOutput::CancelCapture()
824 {
825 std::lock_guard<std::mutex> lock(asyncOpMutex_);
826 auto session = GetSession();
827 CHECK_RETURN_RET_ELOG(session == nullptr || !session->IsSessionCommited(),
828 CameraErrorCode::SESSION_NOT_RUNNING, "PhotoOutput Failed to CancelCapture, session not commited");
829 // LCOV_EXCL_START
830 CHECK_RETURN_RET_ELOG(GetStream() == nullptr,
831 CameraErrorCode::SERVICE_FATL_ERROR, "PhotoOutput Failed to CancelCapture, GetStream is nullptr");
832 auto itemStream = CastStream<IStreamCapture>(GetStream());
833 int32_t errCode = CAMERA_UNKNOWN_ERROR;
834 CHECK_PRINT_ELOG(itemStream == nullptr, "PhotoOutput::CancelCapture() itemStream is nullptr");
835 if (itemStream) {
836 errCode = itemStream->CancelCapture();
837 }
838 CHECK_PRINT_ELOG(errCode != CAMERA_OK, "PhotoOutput Failed to CancelCapture, errCode: %{public}d", errCode);
839 return ServiceToCameraError(errCode);
840 // LCOV_EXCL_STOP
841 }
842
ConfirmCapture()843 int32_t PhotoOutput::ConfirmCapture()
844 {
845 std::lock_guard<std::mutex> lock(asyncOpMutex_);
846 auto session = GetSession();
847 CHECK_RETURN_RET_ELOG(session == nullptr || !session->IsSessionCommited(),
848 CameraErrorCode::SESSION_NOT_RUNNING, "PhotoOutput Failed to ConfirmCapture, session not commited");
849 // LCOV_EXCL_START
850 CHECK_RETURN_RET_ELOG(GetStream() == nullptr, CameraErrorCode::SERVICE_FATL_ERROR,
851 "PhotoOutput Failed to ConfirmCapture, GetStream is nullptr");
852 auto itemStream = CastStream<IStreamCapture>(GetStream());
853 int32_t errCode = CAMERA_UNKNOWN_ERROR;
854 if (itemStream) {
855 errCode = itemStream->ConfirmCapture();
856 } else {
857 MEDIA_ERR_LOG("PhotoOutput::ConfirmCapture() itemStream is nullptr");
858 }
859 CHECK_PRINT_ELOG(errCode != CAMERA_OK, "PhotoOutput Failed to ConfirmCapture, errCode: %{public}d", errCode);
860 return ServiceToCameraError(errCode);
861 // LCOV_EXCL_STOP
862 }
863
CreateStream()864 int32_t PhotoOutput::CreateStream()
865 {
866 auto stream = GetStream();
867 CHECK_RETURN_RET_ELOG(stream != nullptr, CameraErrorCode::OPERATION_NOT_ALLOWED,
868 "PhotoOutput::CreateStream stream is not null");
869 // LCOV_EXCL_START
870 sptr<IStreamCapture> streamPtr = nullptr;
871 auto photoProfile = GetPhotoProfile();
872 CHECK_RETURN_RET_ELOG(photoProfile == nullptr, CameraErrorCode::SERVICE_FATL_ERROR,
873 "PhotoOutput::CreateStream photoProfile is null");
874 int32_t res = CameraErrorCode::SUCCESS;
875 if (isSurfaceOnService_) {
876 res = CameraManager::GetInstance()->CreatePhotoOutputStream(streamPtr, *photoProfile);
877 } else {
878 auto producer = GetBufferProducer();
879 CHECK_RETURN_RET_ELOG(
880 !producer, CameraErrorCode::OPERATION_NOT_ALLOWED, "PhotoOutput::CreateStream producer is null");
881 res = CameraManager::GetInstance()->CreatePhotoOutputStream(streamPtr, *photoProfile, producer);
882 }
883 CHECK_PRINT_ELOG(res != CameraErrorCode::SUCCESS,
884 "PhotoOutput::CreateStream fail! error code :%{public}d", res);
885 SetStream(streamPtr);
886 CHECK_EXECUTE(isSurfaceOnService_, ReSetSavedCallback());
887 return res;
888 // LCOV_EXCL_STOP
889 }
890
Release()891 int32_t PhotoOutput::Release()
892 {
893 {
894 std::lock_guard<std::mutex> lock(outputCallbackMutex_);
895 cameraSvcCallback_ = nullptr;
896 appCallback_ = nullptr;
897 }
898 std::lock_guard<std::mutex> lock(asyncOpMutex_);
899 MEDIA_DEBUG_LOG("Enter Into PhotoOutput::Release");
900 CHECK_RETURN_RET_ELOG(GetStream() == nullptr, CameraErrorCode::SERVICE_FATL_ERROR,
901 "PhotoOutput Failed to Release!, GetStream is nullptr");
902 auto itemStream = CastStream<IStreamCapture>(GetStream());
903 int32_t errCode = CAMERA_UNKNOWN_ERROR;
904 if (itemStream) {
905 errCode = itemStream->Release();
906 } else {
907 MEDIA_ERR_LOG("PhotoOutput::Release() itemStream is nullptr");
908 }
909 CHECK_PRINT_ELOG(errCode != CAMERA_OK, "PhotoOutput Failed to release!, errCode: %{public}d", errCode);
910 defaultCaptureSetting_ = nullptr;
911 CaptureOutput::Release();
912 return ServiceToCameraError(errCode);
913 }
914
IsMirrorSupported()915 bool PhotoOutput::IsMirrorSupported()
916 {
917 auto session = GetSession();
918 CHECK_RETURN_RET_ELOG(session == nullptr, false,
919 "PhotoOutput IsMirrorSupported error!, session is nullptr");
920 // LCOV_EXCL_START
921 auto inputDevice = session->GetInputDevice();
922 CHECK_RETURN_RET_ELOG(inputDevice == nullptr, false,
923 "PhotoOutput IsMirrorSupported error!, inputDevice is nullptr");
924 sptr<CameraDevice> cameraObj = inputDevice->GetCameraDeviceInfo();
925 CHECK_RETURN_RET_ELOG(cameraObj == nullptr, false,
926 "PhotoOutput IsMirrorSupported error!, cameraObj is nullptr");
927 std::shared_ptr<Camera::CameraMetadata> metadata = cameraObj->GetCachedMetadata();
928 CHECK_RETURN_RET(metadata == nullptr, false);
929
930 camera_metadata_item_t item;
931 int32_t ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_CAPTURE_MIRROR_SUPPORTED, &item);
932 CHECK_RETURN_RET_ELOG(ret != CAM_META_SUCCESS, false,
933 "PhotoOutput Can not find OHOS_CONTROL_CAPTURE_MIRROR_SUPPORTED");
934 int step = 2;
935 const int32_t canMirrorVideoAndPhoto = 2;
936 const int32_t canMirrorPhotoOnly = 1;
937 bool isMirrorEnabled = false;
938 SceneMode currentSceneMode = session->GetMode();
939 for (int i = 0; i < static_cast<int>(item.count); i += step) {
940 MEDIA_DEBUG_LOG("mode u8[%{public}d]: %{public}d, u8[%{public}d], %{public}d",
941 i, item.data.u8[i], i + 1, item.data.u8[i + 1]);
942 if (currentSceneMode == static_cast<int>(item.data.u8[i])) {
943 isMirrorEnabled = (
944 item.data.u8[i + 1] == canMirrorPhotoOnly ||
945 item.data.u8[i + 1] == canMirrorVideoAndPhoto) ? true : false;
946 }
947 }
948 MEDIA_DEBUG_LOG("IsMirrorSupported isSupport: %{public}d", isMirrorEnabled);
949 return isMirrorEnabled;
950 // LCOV_EXCL_STOP
951 }
952
EnableMirror(bool isEnable)953 int32_t PhotoOutput::EnableMirror(bool isEnable)
954 {
955 MEDIA_INFO_LOG("PhotoOutput::EnableMirror enter, isEnable: %{public}d", isEnable);
956 auto session = GetSession();
957 CHECK_RETURN_RET_ELOG(session == nullptr, CameraErrorCode::SESSION_NOT_RUNNING,
958 "PhotoOutput EnableMirror error!, session is nullptr");
959
960 // LCOV_EXCL_START
961 int32_t ret = CAMERA_UNKNOWN_ERROR;
962 CHECK_RETURN_RET_ELOG(!(IsMirrorSupported()), ret,
963 "PhotoOutput EnableMirror error!, mirror is not supported");
964 auto isSessionConfiged = session->IsSessionCommited() || session->IsSessionStarted();
965 ret = session->EnableMovingPhotoMirror(isEnable, isSessionConfiged);
966 CHECK_RETURN_RET_ELOG(ret != CameraErrorCode::SUCCESS, ret,
967 "PhotoOutput EnableMirror error!, ret is not success");
968 return ret;
969 // LCOV_EXCL_STOP
970 }
971
IsQuickThumbnailSupported()972 int32_t PhotoOutput::IsQuickThumbnailSupported()
973 {
974 int32_t isQuickThumbnailEnabled = -1;
975 auto session = GetSession();
976 CHECK_RETURN_RET_ELOG(session == nullptr, SESSION_NOT_RUNNING,
977 "PhotoOutput IsQuickThumbnailSupported error!, session is nullptr");
978 // LCOV_EXCL_START
979 auto inputDevice = session->GetInputDevice();
980 CHECK_RETURN_RET_ELOG(inputDevice == nullptr, SESSION_NOT_RUNNING,
981 "PhotoOutput IsQuickThumbnailSupported error!, inputDevice is nullptr");
982 sptr<CameraDevice> cameraObj = inputDevice->GetCameraDeviceInfo();
983 CHECK_RETURN_RET_ELOG(cameraObj == nullptr, SESSION_NOT_RUNNING,
984 "PhotoOutput IsQuickThumbnailSupported error!, cameraObj is nullptr");
985 std::shared_ptr<Camera::CameraMetadata> metadata = cameraObj->GetCachedMetadata();
986 CHECK_RETURN_RET(metadata == nullptr, SESSION_NOT_RUNNING);
987 camera_metadata_item_t item;
988 int32_t ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_STREAM_QUICK_THUMBNAIL_AVAILABLE, &item);
989 if (ret == CAM_META_SUCCESS) {
990 isQuickThumbnailEnabled = (item.data.u8[0] == 1) ? 0 : -1;
991 }
992 const int32_t nightMode = 4;
993 if ((session->GetMode() == nightMode && (cameraObj->GetPosition() != CAMERA_POSITION_FRONT)) ||
994 session->GetMode() == LIGHT_PAINTING) {
995 isQuickThumbnailEnabled = -1;
996 }
997 return isQuickThumbnailEnabled;
998 // LCOV_EXCL_STOP
999 }
1000
IsRawDeliverySupported(bool & isRawDeliveryEnabled)1001 int32_t PhotoOutput::IsRawDeliverySupported(bool &isRawDeliveryEnabled)
1002 {
1003 MEDIA_DEBUG_LOG("enter into IsRawDeliverySupported");
1004 isRawDeliveryEnabled = false;
1005 auto session = GetSession();
1006 CHECK_RETURN_RET_ELOG(session == nullptr, SESSION_NOT_RUNNING,
1007 "PhotoOutput IsRawDeliverySupported error!, session is nullptr");
1008 // LCOV_EXCL_START
1009 const int32_t professionalPhotoMode = 11;
1010 if ((session->GetMode() == professionalPhotoMode)) {
1011 isRawDeliveryEnabled = true;
1012 }
1013 return CAMERA_OK;
1014 // LCOV_EXCL_STOP
1015 }
1016
DeferImageDeliveryFor(DeferredDeliveryImageType type)1017 int32_t PhotoOutput::DeferImageDeliveryFor(DeferredDeliveryImageType type)
1018 {
1019 MEDIA_INFO_LOG("PhotoOutput DeferImageDeliveryFor type:%{public}d!", type);
1020 CAMERA_SYNC_TRACE;
1021 sptr<CameraDevice> cameraObj;
1022 auto session = GetSession();
1023 CHECK_RETURN_RET_ELOG(session == nullptr, SESSION_NOT_RUNNING,
1024 "PhotoOutput DeferImageDeliveryFor error!, session is nullptr");
1025 // LCOV_EXCL_START
1026 auto inputDevice = session->GetInputDevice();
1027 CHECK_RETURN_RET_ELOG(inputDevice == nullptr, SESSION_NOT_RUNNING,
1028 "PhotoOutput DeferImageDeliveryFor error!, inputDevice is nullptr");
1029 cameraObj = inputDevice->GetCameraDeviceInfo();
1030 CHECK_RETURN_RET_ELOG(cameraObj == nullptr, SESSION_NOT_RUNNING,
1031 "PhotoOutput DeferImageDeliveryFor error!, cameraObj is nullptr");
1032 session->EnableDeferredType(type, true);
1033 session->SetUserId();
1034 return 0;
1035 // LCOV_EXCL_STOP
1036 }
1037
IsDeferredImageDeliverySupported(DeferredDeliveryImageType type)1038 int32_t PhotoOutput::IsDeferredImageDeliverySupported(DeferredDeliveryImageType type)
1039 {
1040 MEDIA_INFO_LOG("IsDeferredImageDeliverySupported type:%{public}d!", type);
1041 int32_t isSupported = -1;
1042 CHECK_RETURN_RET(type == DELIVERY_NONE, isSupported);
1043 sptr<CameraDevice> cameraObj;
1044 auto session = GetSession();
1045 CHECK_RETURN_RET_ELOG(session == nullptr, SESSION_NOT_RUNNING,
1046 "PhotoOutput IsDeferredImageDeliverySupported error!, session is nullptr");
1047 // LCOV_EXCL_START
1048 auto inputDevice = session->GetInputDevice();
1049 CHECK_RETURN_RET_ELOG(inputDevice == nullptr, SESSION_NOT_RUNNING,
1050 "PhotoOutput IsDeferredImageDeliverySupported error!, inputDevice is nullptr");
1051 cameraObj = inputDevice->GetCameraDeviceInfo();
1052 CHECK_RETURN_RET_ELOG(cameraObj == nullptr, SESSION_NOT_RUNNING,
1053 "PhotoOutput IsDeferredImageDeliverySupported error!, cameraObj is nullptr");
1054 int32_t curMode = session->GetMode();
1055 int32_t modeSupportType = cameraObj->modeDeferredType_[curMode];
1056 MEDIA_INFO_LOG("IsDeferredImageDeliverySupported curMode:%{public}d, modeSupportType:%{public}d",
1057 curMode, modeSupportType);
1058 if (modeSupportType == type) {
1059 isSupported = 0; // -1:not support 0:support
1060 }
1061 return isSupported;
1062 // LCOV_EXCL_STOP
1063 }
1064
IsDeferredImageDeliveryEnabled(DeferredDeliveryImageType type)1065 int32_t PhotoOutput::IsDeferredImageDeliveryEnabled(DeferredDeliveryImageType type)
1066 {
1067 MEDIA_INFO_LOG("PhotoOutput IsDeferredImageDeliveryEnabled type:%{public}d!", type);
1068 int32_t isEnabled = -1;
1069 auto session = GetSession();
1070 CHECK_RETURN_RET_ELOG(session == nullptr, SESSION_NOT_RUNNING,
1071 "PhotoOutput IsDeferredImageDeliveryEnabled error!, session is nullptr");
1072 // LCOV_EXCL_START
1073 auto inputDevice = session->GetInputDevice();
1074 CHECK_RETURN_RET_ELOG(inputDevice == nullptr, SESSION_NOT_RUNNING,
1075 "PhotoOutput IsDeferredImageDeliveryEnabled error!, inputDevice is nullptr");
1076 sptr<CameraDevice> cameraObj = inputDevice->GetCameraDeviceInfo();
1077 CHECK_RETURN_RET_ELOG(cameraObj == nullptr, SESSION_NOT_RUNNING,
1078 "PhotoOutput IsDeferredImageDeliveryEnabled error!, cameraObj is nullptr");
1079 isEnabled = session->IsImageDeferred() ? 0 : -1;
1080 return isEnabled;
1081 // LCOV_EXCL_STOP
1082 }
1083
IsAutoHighQualityPhotoSupported(int32_t & isAutoHighQualityPhotoSupported)1084 int32_t PhotoOutput::IsAutoHighQualityPhotoSupported(int32_t &isAutoHighQualityPhotoSupported)
1085 {
1086 MEDIA_INFO_LOG("PhotoOutput IsAutoHighQualityPhotoSupported is called");
1087 isAutoHighQualityPhotoSupported = -1;
1088 camera_metadata_item_t item;
1089 sptr<CameraDevice> cameraObj;
1090 auto session = GetSession();
1091 CHECK_RETURN_RET_ELOG(session == nullptr, SESSION_NOT_RUNNING,
1092 "PhotoOutput IsAutoHighQualityPhotoSupported error!, session is nullptr");
1093 // LCOV_EXCL_START
1094 auto inputDevice = session->GetInputDevice();
1095 CHECK_RETURN_RET_ELOG(inputDevice == nullptr, SESSION_NOT_RUNNING,
1096 "PhotoOutput IsAutoHighQualityPhotoSupported error!, inputDevice is nullptr");
1097 cameraObj = inputDevice->GetCameraDeviceInfo();
1098 CHECK_RETURN_RET_ELOG(cameraObj == nullptr, SESSION_NOT_RUNNING,
1099 "PhotoOutput IsAutoHighQualityPhotoSupported error!, cameraObj is nullptr");
1100 std::shared_ptr<Camera::CameraMetadata> metadata = cameraObj->GetCachedMetadata();
1101 CHECK_RETURN_RET(metadata == nullptr, SESSION_NOT_RUNNING);
1102 int32_t ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_HIGH_QUALITY_SUPPORT, &item);
1103 if (ret == CAM_META_SUCCESS) {
1104 isAutoHighQualityPhotoSupported = (item.data.u8[1] == 1) ? 0 : -1; // default mode
1105 }
1106
1107 int headLenPerMode = 2;
1108 SceneMode currentSceneMode = session->GetMode();
1109 for (int i = 0; i + 1 < static_cast<int>(item.count); i += headLenPerMode) {
1110 if (currentSceneMode == static_cast<int>(item.data.u8[i])) {
1111 isAutoHighQualityPhotoSupported = (item.data.u8[i + 1] == 1) ? 0 : -1;
1112 }
1113 }
1114 MEDIA_INFO_LOG("PhotoOutput IsAutoHighQualityPhotoSupported curMode:%{public}d, modeSupportType:%{public}d",
1115 currentSceneMode, isAutoHighQualityPhotoSupported);
1116 return CAMERA_OK;
1117 // LCOV_EXCL_STOP
1118 }
1119
EnableAutoHighQualityPhoto(bool enabled)1120 int32_t PhotoOutput::EnableAutoHighQualityPhoto(bool enabled)
1121 {
1122 MEDIA_DEBUG_LOG("PhotoOutput EnableAutoHighQualityPhoto");
1123 auto session = GetSession();
1124 CHECK_RETURN_RET_ELOG(session == nullptr, SESSION_NOT_RUNNING,
1125 "PhotoOutput EnableAutoHighQualityPhoto error!, session is nullptr");
1126 // LCOV_EXCL_START
1127 auto inputDevice = session->GetInputDevice();
1128 CHECK_RETURN_RET_ELOG(inputDevice == nullptr, SESSION_NOT_RUNNING,
1129 "PhotoOutput EnableAutoHighQualityPhoto error!, inputDevice is nullptr");
1130 int32_t isAutoHighQualityPhotoSupported;
1131 int32_t ret = IsAutoHighQualityPhotoSupported(isAutoHighQualityPhotoSupported);
1132 CHECK_RETURN_RET_ELOG(ret != CAMERA_OK, OPERATION_NOT_ALLOWED,
1133 "PhotoOutput EnableAutoHighQualityPhoto error");
1134 CHECK_RETURN_RET_ELOG(isAutoHighQualityPhotoSupported == -1, INVALID_ARGUMENT,
1135 "PhotoOutput EnableAutoHighQualityPhoto not supported");
1136 int32_t res = session->EnableAutoHighQualityPhoto(enabled);
1137 return res;
1138 // LCOV_EXCL_STOP
1139 }
1140
ProcessSnapshotDurationUpdates(int32_t snapshotDuration)1141 void PhotoOutput::ProcessSnapshotDurationUpdates(int32_t snapshotDuration) __attribute__((no_sanitize("cfi")))
1142 {
1143 std::lock_guard<std::mutex> lock(outputCallbackMutex_);
1144 if (appCallback_ != nullptr) {
1145 MEDIA_DEBUG_LOG("appCallback not nullptr");
1146 appCallback_->OnEstimatedCaptureDuration(snapshotDuration);
1147 }
1148 }
1149
GetDefaultCaptureSetting()1150 std::shared_ptr<PhotoCaptureSetting> PhotoOutput::GetDefaultCaptureSetting()
1151 {
1152 return defaultCaptureSetting_;
1153 }
1154
SetMovingPhotoVideoCodecType(int32_t videoCodecType)1155 int32_t PhotoOutput::SetMovingPhotoVideoCodecType(int32_t videoCodecType)
1156 {
1157 std::lock_guard<std::mutex> lock(asyncOpMutex_);
1158 MEDIA_DEBUG_LOG("Enter Into PhotoOutput::SetMovingPhotoVideoCodecType");
1159 CHECK_RETURN_RET_ELOG(GetStream() == nullptr, CameraErrorCode::SERVICE_FATL_ERROR,
1160 "PhotoOutput Failed to SetMovingPhotoVideoCodecType!, GetStream is nullptr");
1161 // LCOV_EXCL_START
1162 auto itemStream = CastStream<IStreamCapture>(GetStream());
1163 int32_t errCode = CAMERA_UNKNOWN_ERROR;
1164 CHECK_PRINT_ELOG(itemStream == nullptr, "PhotoOutput::SetMovingPhotoVideoCodecType() itemStream is nullptr");
1165 if (itemStream) {
1166 errCode = itemStream->SetMovingPhotoVideoCodecType(videoCodecType);
1167 }
1168 CHECK_PRINT_ELOG(errCode != CAMERA_OK, "PhotoOutput Failed to SetMovingPhotoVideoCodecType!, "
1169 "errCode: %{public}d", errCode);
1170 return ServiceToCameraError(errCode);
1171 // LCOV_EXCL_STOP
1172 }
1173
CameraServerDied(pid_t pid)1174 void PhotoOutput::CameraServerDied(pid_t pid)
1175 {
1176 MEDIA_ERR_LOG("camera server has died, pid:%{public}d!", pid);
1177 std::lock_guard<std::mutex> lock(outputCallbackMutex_);
1178 if (appCallback_ != nullptr) {
1179 MEDIA_DEBUG_LOG("appCallback not nullptr");
1180 int32_t serviceErrorType = ServiceToCameraError(CAMERA_INVALID_STATE);
1181 int32_t captureId = -1;
1182 appCallback_->OnCaptureError(captureId, serviceErrorType);
1183 }
1184 }
1185
IsAutoCloudImageEnhancementSupported(bool & isAutoCloudImageEnhancementSupported)1186 int32_t PhotoOutput::IsAutoCloudImageEnhancementSupported(bool &isAutoCloudImageEnhancementSupported)
1187 {
1188 MEDIA_INFO_LOG("PhotoOutput IsAutoCloudImageEnhancementSupported is called");
1189 auto session = GetSession();
1190 CHECK_RETURN_RET_ELOG(session == nullptr, SERVICE_FATL_ERROR,
1191 "PhotoOutput IsAutoCloudImageEnhancementSupported error!, session is nullptr");
1192 // LCOV_EXCL_START
1193 auto inputDevice = session->GetInputDevice();
1194 CHECK_RETURN_RET_ELOG(inputDevice == nullptr, SERVICE_FATL_ERROR,
1195 "PhotoOutput IsAutoCloudImageEnhancementSupported error!, inputDevice is nullptr");
1196 sptr<CameraDevice> cameraObj = inputDevice->GetCameraDeviceInfo();
1197 CHECK_RETURN_RET_ELOG(cameraObj == nullptr, SERVICE_FATL_ERROR,
1198 "PhotoOutput IsAutoCloudImageEnhancementSupported error!, cameraObj is nullptr");
1199 std::shared_ptr<Camera::CameraMetadata> metadata = cameraObj->GetCachedMetadata();
1200 CHECK_RETURN_RET(metadata == nullptr, SERVICE_FATL_ERROR);
1201 camera_metadata_item_t item;
1202 int32_t ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_AUTO_CLOUD_IMAGE_ENHANCE, &item);
1203 if (ret == CAM_META_SUCCESS) {
1204 if (item.count == 0) {
1205 MEDIA_WARNING_LOG("isAutoCloudImageEnhancementSupported item is nullptr");
1206 return CAMERA_OK;
1207 }
1208 SceneMode currentScenemode = session->GetMode();
1209 for (int i = 0; i < static_cast<int>(item.count);i++) {
1210 if (currentScenemode == static_cast<int>(item.data.i32[i])) {
1211 isAutoCloudImageEnhancementSupported = true;
1212 return CAMERA_OK;
1213 }
1214 }
1215 }
1216 MEDIA_INFO_LOG("Judge Auto Cloud Image Enhancement Supported result %{public}d",
1217 isAutoCloudImageEnhancementSupported);
1218 return CAMERA_OK;
1219 // LCOV_EXCL_STOP
1220 }
1221
EnableAutoCloudImageEnhancement(bool enabled)1222 int32_t PhotoOutput::EnableAutoCloudImageEnhancement(bool enabled)
1223 {
1224 MEDIA_DEBUG_LOG("PhotoOutput EnableAutoCloudImageEnhancement is called");
1225 auto session = GetSession();
1226 CHECK_RETURN_RET_ELOG(session == nullptr, SERVICE_FATL_ERROR,
1227 "PhotoOutput EnableAutoCloudImageEnhancement error!, session is nullptr");
1228 // LCOV_EXCL_START
1229 auto inputDevice = session->GetInputDevice();
1230 CHECK_RETURN_RET_ELOG(inputDevice == nullptr, SERVICE_FATL_ERROR,
1231 "PhotoOutput EnableAutoCloudImageEnhancement error!, inputDevice is nullptr");
1232 bool isAutoCloudImageEnhancementSupported = false;
1233 int32_t ret = IsAutoCloudImageEnhancementSupported(isAutoCloudImageEnhancementSupported);
1234 CHECK_RETURN_RET_ELOG(ret != CAM_META_SUCCESS, SERVICE_FATL_ERROR,
1235 "PhotoOutput EnableAutoCloudImageEnhancement error");
1236 CHECK_RETURN_RET_ELOG(isAutoCloudImageEnhancementSupported == false, SERVICE_FATL_ERROR,
1237 "PhotoOutput EnableAutoCloudImageEnhancement not supported");
1238 int32_t res = session->EnableAutoCloudImageEnhancement(enabled);
1239 return res;
1240 // LCOV_EXCL_STOP
1241 }
1242
IsDepthDataDeliverySupported()1243 bool PhotoOutput::IsDepthDataDeliverySupported()
1244 {
1245 CAMERA_SYNC_TRACE;
1246 MEDIA_INFO_LOG("Enter IsDepthDataDeliverySupported");
1247 return false;
1248 }
1249
EnableDepthDataDelivery(bool enabled)1250 int32_t PhotoOutput::EnableDepthDataDelivery(bool enabled)
1251 {
1252 CAMERA_SYNC_TRACE;
1253 MEDIA_INFO_LOG("Enter EnableDepthDataDelivery, enabled:%{public}d", enabled);
1254 return CameraErrorCode::SUCCESS;
1255 }
1256
GetPhotoRotation(int32_t imageRotation)1257 int32_t PhotoOutput::GetPhotoRotation(int32_t imageRotation)
1258 {
1259 MEDIA_DEBUG_LOG("PhotoOutput GetPhotoRotation is called");
1260 int32_t sensorOrientation = 0;
1261 CameraPosition cameraPosition;
1262 ImageRotation result = ImageRotation::ROTATION_0;
1263 sptr<CameraDevice> cameraObj;
1264 auto session = GetSession();
1265 CHECK_RETURN_RET_ELOG(session == nullptr, SERVICE_FATL_ERROR,
1266 "PhotoOutput GetPhotoRotation error!, session is nullptr");
1267 // LCOV_EXCL_START
1268 auto inputDevice = session->GetInputDevice();
1269 CHECK_RETURN_RET_ELOG(inputDevice == nullptr, SERVICE_FATL_ERROR,
1270 "PhotoOutput GetPhotoRotation error!, inputDevice is nullptr");
1271 cameraObj = inputDevice->GetCameraDeviceInfo();
1272 CHECK_RETURN_RET_ELOG(cameraObj == nullptr, SERVICE_FATL_ERROR,
1273 "PhotoOutput GetPhotoRotation error!, cameraObj is nullptr");
1274 cameraPosition = cameraObj->GetPosition();
1275 CHECK_RETURN_RET_ELOG(cameraPosition == CAMERA_POSITION_UNSPECIFIED, SERVICE_FATL_ERROR,
1276 "PhotoOutput GetPhotoRotation error!, cameraPosition is unspecified");
1277 sensorOrientation = static_cast<int32_t>(cameraObj->GetCameraOrientation());
1278 imageRotation = (imageRotation + ROTATION_45_DEGREES) / ROTATION_90_DEGREES * ROTATION_90_DEGREES;
1279 if (cameraPosition == CAMERA_POSITION_BACK) {
1280 result = (ImageRotation)((imageRotation + sensorOrientation) % CAPTURE_ROTATION_BASE);
1281 } else if (cameraPosition == CAMERA_POSITION_FRONT || cameraPosition == CAMERA_POSITION_FOLD_INNER) {
1282 result = (ImageRotation)((sensorOrientation - imageRotation + CAPTURE_ROTATION_BASE) % CAPTURE_ROTATION_BASE);
1283 }
1284 auto streamCapturePtr = CastStream<IStreamCapture>(GetStream());
1285 int32_t errCode = CAMERA_UNKNOWN_ERROR;
1286 CHECK_RETURN_RET_ELOG(streamCapturePtr == nullptr, CameraErrorCode::SERVICE_FATL_ERROR;,
1287 "PhotoOutput::SetCameraPhotoRotation() streamCapturePtr is nullptr");
1288 if (streamCapturePtr) {
1289 errCode = streamCapturePtr->SetCameraPhotoRotation(true);
1290 CHECK_RETURN_RET_ELOG(errCode != CAMERA_OK, SERVICE_FATL_ERROR,
1291 "Failed to SetCameraPhotoRotation!, errCode: %{public}d", errCode);
1292 }
1293 MEDIA_INFO_LOG("PhotoOutput GetPhotoRotation :result %{public}d, sensorOrientation:%{public}d",
1294 result, sensorOrientation);
1295 return result;
1296 // LCOV_EXCL_STOP
1297 }
1298
IsAutoAigcPhotoSupported(bool & isAutoAigcPhotoSupported)1299 int32_t PhotoOutput::IsAutoAigcPhotoSupported(bool& isAutoAigcPhotoSupported)
1300 {
1301 MEDIA_INFO_LOG("PhotoOutput::IsAutoAigcPhotoSupported enter");
1302 auto session = GetSession();
1303 CHECK_RETURN_RET_ELOG(session == nullptr, SERVICE_FATL_ERROR,
1304 "PhotoOutput::IsAutoAigcPhotoSupportederror, captureSession is nullptr");
1305
1306 // LCOV_EXCL_START
1307 auto inputDevice = session->GetInputDevice();
1308 CHECK_RETURN_RET_ELOG(inputDevice == nullptr, SERVICE_FATL_ERROR,
1309 "PhotoOutput::IsAutoAigcPhotoSupported, inputDevice is nullptr");
1310
1311 sptr<CameraDevice> cameraObj = inputDevice->GetCameraDeviceInfo();
1312 CHECK_RETURN_RET_ELOG(
1313 cameraObj == nullptr, SERVICE_FATL_ERROR, "PhotoOutput::IsAutoAigcPhotoSupported error, cameraObj is nullptr");
1314
1315 std::shared_ptr<Camera::CameraMetadata> metadata = cameraObj->GetMetadata();
1316 CHECK_RETURN_RET_ELOG(
1317 metadata == nullptr, SERVICE_FATL_ERROR, "PhotoOutput::IsAutoAigcPhotoSupported error, metadata is nullptr");
1318
1319 camera_metadata_item_t item;
1320 int32_t ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_AUTO_AIGC_PHOTO, &item);
1321 MEDIA_DEBUG_LOG("PhotoOutput::IsAutoAigcPhotoSupported ret: %{public}d", ret);
1322 if (ret == CAM_META_SUCCESS) {
1323 if (item.count == 0) {
1324 MEDIA_WARNING_LOG("PhotoOutput::IsAutoAigcPhotoSupported item is nullptr");
1325 return CAMERA_OK;
1326 }
1327 SceneMode currentSceneMode = session->GetMode();
1328 MEDIA_DEBUG_LOG(
1329 "PhotoOutput::IsAutoAigcPhotoSupported curMode: %{public}d", static_cast<int>(currentSceneMode));
1330 for (int i = 0; i < static_cast<int>(item.count); i++) {
1331 MEDIA_DEBUG_LOG(
1332 "PhotoOutput::IsAutoAigcPhotoSupported item data: %{public}d", static_cast<int>(item.data.u8[i]));
1333 if (currentSceneMode == static_cast<int>(item.data.u8[i])) {
1334 isAutoAigcPhotoSupported = true;
1335 return CAMERA_OK;
1336 }
1337 }
1338 }
1339 MEDIA_INFO_LOG("PhotoOutput::IsAutoAigcPhotoSupported result: %{public}d ", isAutoAigcPhotoSupported);
1340 return CAMERA_OK;
1341 // LCOV_EXCL_STOP
1342 }
1343
EnableAutoAigcPhoto(bool enabled)1344 int32_t PhotoOutput::EnableAutoAigcPhoto(bool enabled)
1345 {
1346 MEDIA_INFO_LOG("PhotoOutput::EnableAutoAigcPhoto enter, enabled: %{public}d", enabled);
1347 auto captureSession = GetSession();
1348 CHECK_RETURN_RET_ELOG(captureSession == nullptr, SESSION_NOT_RUNNING,
1349 "PhotoOutput::EnableAutoAigcPhoto error, captureSession is nullptr");
1350
1351 // LCOV_EXCL_START
1352 auto inputDevice = captureSession->GetInputDevice();
1353 CHECK_RETURN_RET_ELOG(inputDevice == nullptr, SESSION_NOT_RUNNING,
1354 "PhotoOutput::EnableAutoAigcPhoto error, inputDevice is nullptr");
1355
1356 bool isAutoAigcPhotoSupported = false;
1357 int32_t ret = IsAutoAigcPhotoSupported(isAutoAigcPhotoSupported);
1358 CHECK_RETURN_RET_ELOG(ret != CAMERA_OK, SERVICE_FATL_ERROR, "PhotoOutput::EnableAutoAigcPhoto error");
1359 CHECK_RETURN_RET_ELOG(
1360 !isAutoAigcPhotoSupported, PARAMETER_ERROR, "PhotoOutput::EnableAutoAigcPhoto not supported");
1361 int32_t res = captureSession->EnableAutoAigcPhoto(enabled);
1362 MEDIA_INFO_LOG("PhotoOutput::EnableAutoAigcPhoto result: %{public}d", res);
1363 return res;
1364 // LCOV_EXCL_STOP
1365 }
1366
GetPhotoSurface()1367 sptr<Surface> PhotoOutput::GetPhotoSurface()
1368 {
1369 return photoSurface_;
1370 }
1371
IsOfflineSupported()1372 bool PhotoOutput::IsOfflineSupported()
1373 {
1374 // LCOV_EXCL_START
1375 CAMERA_SYNC_TRACE;
1376 MEDIA_INFO_LOG("Enter IsOfflineSupported");
1377 bool isOfflineSupported = false;
1378 auto session = GetSession();
1379 CHECK_RETURN_RET_ELOG(
1380 session == nullptr, isOfflineSupported, "PhotoOutput IsOfflineSupported error!, session is nullptr");
1381 auto inputDevice = session->GetInputDevice();
1382 CHECK_RETURN_RET_ELOG(
1383 inputDevice == nullptr, isOfflineSupported, "PhotoOutput IsOfflineSupported error!, inputDevice is nullptr");
1384 sptr<CameraDevice> cameraObj = inputDevice->GetCameraDeviceInfo();
1385 CHECK_RETURN_RET_ELOG(
1386 cameraObj == nullptr, isOfflineSupported, "PhotoOutput IsOfflineSupported error!, cameraObj is nullptr");
1387 std::shared_ptr<Camera::CameraMetadata> metadata = cameraObj->GetMetadata();
1388 CHECK_RETURN_RET_ELOG(
1389 metadata == nullptr, isOfflineSupported, "PhotoOutput IsOfflineSupported error!, metadata is nullptr");
1390 camera_metadata_item_t item;
1391 int32_t ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_CHANGETO_OFFLINE_STREAM_OPEATOR, &item);
1392 if (ret == CAM_META_SUCCESS && item.count > 0) {
1393 isOfflineSupported = static_cast<bool>(item.data.u8[0]);
1394 MEDIA_INFO_LOG("PhotoOutput isOfflineSupported %{public}d", isOfflineSupported);
1395 return isOfflineSupported;
1396 }
1397 return isOfflineSupported;
1398 // LCOV_EXCL_STOP
1399 }
1400
EnableOfflinePhoto()1401 int32_t PhotoOutput::EnableOfflinePhoto()
1402 {
1403 // LCOV_EXCL_START
1404 CAMERA_SYNC_TRACE;
1405 MEDIA_INFO_LOG("PhotoOutput EnableOfflinePhoto");
1406 auto session = GetSession();
1407 CHECK_RETURN_RET_ELOG(
1408 session == nullptr, SESSION_NOT_RUNNING, "PhotoOutput EnableOfflinePhoto error!, session is nullptr");
1409 auto inputDevice = session->GetInputDevice();
1410 CHECK_RETURN_RET_ELOG(
1411 inputDevice == nullptr, SESSION_NOT_RUNNING, "PhotoOutput EnableOfflinePhoto error!, inputDevice is nullptr");
1412 bool isOfflineSupported = IsOfflineSupported();
1413 CHECK_RETURN_RET_ELOG(isOfflineSupported == false, OPERATION_NOT_ALLOWED,
1414 "PhotoOutput EnableOfflinePhoto error, isOfflineSupported is false");
1415 auto isSessionConfiged = session->IsSessionCommited();
1416 CHECK_RETURN_RET_ELOG(isSessionConfiged == false, OPERATION_NOT_ALLOWED,
1417 "PhotoOutput EnableOfflinePhoto error, isSessionConfiged is false");
1418 mIsHasEnableOfflinePhoto_ = true; // 管理offlinephotooutput
1419 auto streamCapturePtr = CastStream<IStreamCapture>(GetStream());
1420 int32_t errCode = CAMERA_UNKNOWN_ERROR;
1421 if (streamCapturePtr) {
1422 errCode = streamCapturePtr->EnableOfflinePhoto(true);
1423 CHECK_RETURN_RET_ELOG(
1424 errCode != CAMERA_OK, SERVICE_FATL_ERROR, "Failed to EnableOfflinePhoto! , errCode: %{public}d", errCode);
1425 } else {
1426 MEDIA_ERR_LOG("PhotoOutput::EnableOfflinePhoto() itemStream is nullptr");
1427 return CameraErrorCode::SERVICE_FATL_ERROR;
1428 }
1429 return CameraErrorCode::SUCCESS;
1430 // LCOV_EXCL_STOP
1431 }
1432
IsHasEnableOfflinePhoto()1433 bool PhotoOutput::IsHasEnableOfflinePhoto()
1434 {
1435 // LCOV_EXCL_START
1436 MEDIA_INFO_LOG("PhotoOutput::IsHasEnableOfflinePhoto %{public}d", mIsHasEnableOfflinePhoto_);
1437 return mIsHasEnableOfflinePhoto_;
1438 // LCOV_EXCL_STOP
1439 }
1440
SetSwitchOfflinePhotoOutput(bool isHasSwitched)1441 void PhotoOutput::SetSwitchOfflinePhotoOutput(bool isHasSwitched)
1442 {
1443 // LCOV_EXCL_START
1444 std::lock_guard<std::mutex> lock(offlineStatusMutex_);
1445 isHasSwitched_ = isHasSwitched;
1446 // LCOV_EXCL_STOP
1447 }
1448
IsHasSwitchOfflinePhoto()1449 bool PhotoOutput::IsHasSwitchOfflinePhoto()
1450 {
1451 std::lock_guard<std::mutex> lock(offlineStatusMutex_);
1452 return isHasSwitched_;
1453 }
1454
NotifyOfflinePhotoOutput(int32_t captureId)1455 void PhotoOutput::NotifyOfflinePhotoOutput(int32_t captureId)
1456 {
1457 // LCOV_EXCL_START
1458 auto timeStartIter = captureIdToCaptureInfoMap_.find(captureId);
1459 if (timeStartIter != captureIdToCaptureInfoMap_.end()) {
1460 auto timeEnd = std::chrono::steady_clock::now();
1461 uint32_t timeCost = static_cast<uint32_t>(std::chrono::duration<double>(timeEnd -
1462 (timeStartIter->second).timeStart).count());
1463 if (timeCost > CAPTURE_TIMEOUT) {
1464 MEDIA_INFO_LOG("OnCaptureEnded: capture ID: %{public}d timeCost is %{public}d)",
1465 captureId, timeCost);
1466 }
1467 captureIdToCaptureInfoMap_.erase(captureId);
1468 if (IsHasSwitchOfflinePhoto() && captureIdToCaptureInfoMap_.size() == 0) {
1469 MEDIA_INFO_LOG("OnCaptureEnded notify offline delivery finished with capture ID: %{public}d", captureId);
1470 auto callback = GetApplicationCallback();
1471 if (callback == nullptr) {
1472 MEDIA_INFO_LOG("PhotoOutput::NotifyOfflinePhotoOutput callback is nullptr");
1473 Release();
1474 return;
1475 }
1476 callback->OnOfflineDeliveryFinished(captureId);
1477 }
1478 }
1479 // LCOV_EXCL_STOP
1480 }
1481
CreateMediaLibrary(sptr<CameraPhotoProxy> photoProxy,std::string & uri,int32_t & cameraShotType,std::string & burstKey,int64_t timestamp)1482 void PhotoOutput::CreateMediaLibrary(sptr<CameraPhotoProxy> photoProxy, std::string &uri, int32_t &cameraShotType,
1483 std::string &burstKey, int64_t timestamp)
1484 {
1485 MEDIA_INFO_LOG("PhotoOutput::CreateMediaLibrary E");
1486 CAMERA_SYNC_TRACE;
1487 int32_t errorCode = CAMERA_OK;
1488 auto itemStream = CastStream<IStreamCapture>(GetStream());
1489 if (itemStream) {
1490 errorCode = itemStream->CreateMediaLibrary(photoProxy, uri, cameraShotType, burstKey, timestamp);
1491 CHECK_RETURN_ELOG(errorCode != CAMERA_OK, "Failed to createMediaLibrary, errorCode: %{public}d", errorCode);
1492 } else {
1493 MEDIA_ERR_LOG("PhotoOutput::CreateMediaLibrary itemStream is nullptr");
1494 }
1495 MEDIA_INFO_LOG("PhotoOutput::CreateMediaLibrary X");
1496 }
1497 } // namespace CameraStandard
1498 } // namespace OHOS
1499