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 "camera_error_code.h"
22 #include "camera_log.h"
23 #include "camera_manager.h"
24 #include "camera_output_capability.h"
25 #include "camera_util.h"
26 #include "capture_scene_const.h"
27 #include "hstream_capture_callback_stub.h"
28 #include "input/camera_device.h"
29 #include "metadata_common_utils.h"
30 #include "session/capture_session.h"
31 #include "session/night_session.h"
32 #include "camera_report_dfx_uitls.h"
33 #include "picture.h"
34 using namespace std;
35
36 namespace OHOS {
37 namespace CameraStandard {
PhotoCaptureSetting()38 PhotoCaptureSetting::PhotoCaptureSetting()
39 {
40 int32_t items = 10;
41 int32_t dataLength = 100;
42 captureMetadataSetting_ = std::make_shared<Camera::CameraMetadata>(items, dataLength);
43 location_ = std::make_shared<Location>();
44 }
45
GetQuality()46 PhotoCaptureSetting::QualityLevel PhotoCaptureSetting::GetQuality()
47 {
48 QualityLevel quality = QUALITY_LEVEL_LOW;
49 camera_metadata_item_t item;
50
51 int ret = Camera::FindCameraMetadataItem(captureMetadataSetting_->get(), OHOS_JPEG_QUALITY, &item);
52 if (ret != CAM_META_SUCCESS) {
53 return QUALITY_LEVEL_MEDIUM;
54 }
55 if (item.data.u8[0] == OHOS_CAMERA_JPEG_LEVEL_HIGH) {
56 quality = QUALITY_LEVEL_HIGH;
57 } else if (item.data.u8[0] == OHOS_CAMERA_JPEG_LEVEL_MIDDLE) {
58 quality = QUALITY_LEVEL_MEDIUM;
59 }
60 return quality;
61 }
62
SetQuality(PhotoCaptureSetting::QualityLevel qualityLevel)63 void PhotoCaptureSetting::SetQuality(PhotoCaptureSetting::QualityLevel qualityLevel)
64 {
65 bool status = false;
66 camera_metadata_item_t item;
67 uint8_t quality = OHOS_CAMERA_JPEG_LEVEL_LOW;
68
69 if (qualityLevel == QUALITY_LEVEL_HIGH) {
70 quality = OHOS_CAMERA_JPEG_LEVEL_HIGH;
71 } else if (qualityLevel == QUALITY_LEVEL_MEDIUM) {
72 quality = OHOS_CAMERA_JPEG_LEVEL_MIDDLE;
73 }
74 int ret = Camera::FindCameraMetadataItem(captureMetadataSetting_->get(), OHOS_JPEG_QUALITY, &item);
75 if (ret == CAM_META_ITEM_NOT_FOUND) {
76 status = captureMetadataSetting_->addEntry(OHOS_JPEG_QUALITY, &quality, 1);
77 } else if (ret == CAM_META_SUCCESS) {
78 status = captureMetadataSetting_->updateEntry(OHOS_JPEG_QUALITY, &quality, 1);
79 }
80 CHECK_ERROR_PRINT_LOG(!status, "PhotoCaptureSetting::SetQuality Failed to set Quality");
81 }
82
GetRotation()83 PhotoCaptureSetting::RotationConfig PhotoCaptureSetting::GetRotation()
84 {
85 RotationConfig rotation;
86 camera_metadata_item_t item;
87
88 int ret = Camera::FindCameraMetadataItem(captureMetadataSetting_->get(), OHOS_JPEG_ORIENTATION, &item);
89 if (ret == CAM_META_SUCCESS) {
90 rotation = static_cast<RotationConfig>(item.data.i32[0]);
91 return rotation;
92 }
93 return RotationConfig::Rotation_0;
94 }
95
SetRotation(PhotoCaptureSetting::RotationConfig rotationValue)96 void PhotoCaptureSetting::SetRotation(PhotoCaptureSetting::RotationConfig rotationValue)
97 {
98 bool status = false;
99 camera_metadata_item_t item;
100 int32_t rotation = rotationValue;
101
102 int ret = Camera::FindCameraMetadataItem(captureMetadataSetting_->get(), OHOS_JPEG_ORIENTATION, &item);
103 if (ret == CAM_META_ITEM_NOT_FOUND) {
104 status = captureMetadataSetting_->addEntry(OHOS_JPEG_ORIENTATION, &rotation, 1);
105 } else if (ret == CAM_META_SUCCESS) {
106 status = captureMetadataSetting_->updateEntry(OHOS_JPEG_ORIENTATION, &rotation, 1);
107 }
108 CHECK_ERROR_PRINT_LOG(!status, "PhotoCaptureSetting::SetRotation Failed to set Rotation");
109 return;
110 }
111
SetGpsLocation(double latitude,double longitude)112 void PhotoCaptureSetting::SetGpsLocation(double latitude, double longitude)
113 {
114 std::shared_ptr<Location> location = std::make_shared<Location>();
115 location->latitude = latitude;
116 location->longitude = longitude;
117 location->altitude = 0;
118 SetLocation(location);
119 }
120
SetLocation(std::shared_ptr<Location> & location)121 void PhotoCaptureSetting::SetLocation(std::shared_ptr<Location>& location)
122 {
123 CHECK_ERROR_RETURN(location == nullptr);
124 std::lock_guard<std::mutex> lock(locationMutex_);
125 location_ = location;
126 double gpsCoordinates[3] = {location->latitude, location->longitude, location->altitude};
127 bool status = false;
128 camera_metadata_item_t item;
129
130 MEDIA_DEBUG_LOG("PhotoCaptureSetting::SetLocation lat=%{public}f, long=%{public}f and alt=%{public}f",
131 location_->latitude, location_->longitude, location_->altitude);
132 int ret = Camera::FindCameraMetadataItem(captureMetadataSetting_->get(), OHOS_JPEG_GPS_COORDINATES, &item);
133 if (ret == CAM_META_ITEM_NOT_FOUND) {
134 status = captureMetadataSetting_->addEntry(
135 OHOS_JPEG_GPS_COORDINATES, gpsCoordinates, sizeof(gpsCoordinates) / sizeof(gpsCoordinates[0]));
136 } else if (ret == CAM_META_SUCCESS) {
137 status = captureMetadataSetting_->updateEntry(
138 OHOS_JPEG_GPS_COORDINATES, gpsCoordinates, sizeof(gpsCoordinates) / sizeof(gpsCoordinates[0]));
139 }
140 CHECK_ERROR_PRINT_LOG(!status, "PhotoCaptureSetting::SetLocation Failed to set GPS co-ordinates");
141 }
142
GetLocation(std::shared_ptr<Location> & location)143 void PhotoCaptureSetting::GetLocation(std::shared_ptr<Location>& location)
144 {
145 std::lock_guard<std::mutex> lock(locationMutex_);
146 location = location_;
147 MEDIA_DEBUG_LOG("PhotoCaptureSetting::GetLocation lat=%{public}f, long=%{public}f and alt=%{public}f",
148 location->latitude, location->longitude, location->altitude);
149 }
150
151
SetMirror(bool enable)152 void PhotoCaptureSetting::SetMirror(bool enable)
153 {
154 bool status = false;
155 camera_metadata_item_t item;
156 uint8_t mirror = enable;
157
158 MEDIA_DEBUG_LOG("PhotoCaptureSetting::SetMirror value=%{public}d", enable);
159 int ret = Camera::FindCameraMetadataItem(captureMetadataSetting_->get(), OHOS_CONTROL_CAPTURE_MIRROR, &item);
160 if (ret == CAM_META_ITEM_NOT_FOUND) {
161 status = captureMetadataSetting_->addEntry(OHOS_CONTROL_CAPTURE_MIRROR, &mirror, 1);
162 } else if (ret == CAM_META_SUCCESS) {
163 status = captureMetadataSetting_->updateEntry(OHOS_CONTROL_CAPTURE_MIRROR, &mirror, 1);
164 }
165 CHECK_ERROR_PRINT_LOG(!status, "PhotoCaptureSetting::SetMirror Failed to set mirroring in photo capture setting");
166 return;
167 }
168
GetMirror()169 bool PhotoCaptureSetting::GetMirror()
170 {
171 bool isMirror;
172 camera_metadata_item_t item;
173 int ret = Camera::FindCameraMetadataItem(captureMetadataSetting_->get(), OHOS_CONTROL_CAPTURE_MIRROR, &item);
174 if (ret == CAM_META_SUCCESS) {
175 isMirror = static_cast<bool>(item.data.u8[0]);
176 return isMirror;
177 }
178 return false;
179 }
180
GetCaptureMetadataSetting()181 std::shared_ptr<Camera::CameraMetadata> PhotoCaptureSetting::GetCaptureMetadataSetting()
182 {
183 return captureMetadataSetting_;
184 }
185
SetBurstCaptureState(uint8_t burstState)186 void PhotoCaptureSetting::SetBurstCaptureState(uint8_t burstState)
187 {
188 CAMERA_SYNC_TRACE;
189 MEDIA_INFO_LOG("SetBurstCaptureState");
190 bool status = false;
191 camera_metadata_item_t item;
192 int ret = Camera::FindCameraMetadataItem(captureMetadataSetting_->get(), OHOS_CONTROL_BURST_CAPTURE, &item);
193 if (ret == CAM_META_ITEM_NOT_FOUND) {
194 status = captureMetadataSetting_->addEntry(OHOS_CONTROL_BURST_CAPTURE, &burstState, 1);
195 } else if (ret == CAM_META_SUCCESS) {
196 status = captureMetadataSetting_->updateEntry(OHOS_CONTROL_BURST_CAPTURE, &burstState, 1);
197 }
198 if (!status) {
199 MEDIA_ERR_LOG("PhotoCaptureSetting::SetBurstCaptureState Failed");
200 }
201 return;
202 }
203
OnCaptureStarted(const int32_t captureId)204 int32_t HStreamCaptureCallbackImpl::OnCaptureStarted(const int32_t captureId)
205 {
206 CAMERA_SYNC_TRACE;
207 auto photoOutput = GetPhotoOutput();
208 CHECK_ERROR_RETURN_RET_LOG(photoOutput == nullptr, CAMERA_OK,
209 "HStreamCaptureCallbackImpl::OnCaptureStarted photoOutput is nullptr");
210 auto callback = photoOutput->GetApplicationCallback();
211 CHECK_ERROR_RETURN_RET_LOG(callback == nullptr, CAMERA_OK,
212 "HStreamCaptureCallbackImpl::OnCaptureStarted callback is nullptr");
213
214 sptr<CaptureSession> session = photoOutput->GetSession();
215 switch (session->GetMode()) {
216 case SceneMode::HIGH_RES_PHOTO: {
217 auto inputDevice = session->GetInputDevice();
218 CHECK_ERROR_RETURN_RET_LOG(inputDevice == nullptr, CAMERA_OK,
219 "HStreamCaptureCallbackImpl::OnCaptureStarted inputDevice is nullptr");
220 sptr<CameraDevice> cameraObj = inputDevice->GetCameraDeviceInfo();
221 std::shared_ptr<Camera::CameraMetadata> metadata = cameraObj->GetMetadata();
222 camera_metadata_item_t meta;
223 int32_t ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_CAPTURE_EXPECT_TIME, &meta);
224 if (ret == CAM_META_SUCCESS) {
225 photoOutput->GetApplicationCallback()->OnCaptureStarted(captureId, meta.data.ui32[1]);
226 } else {
227 MEDIA_WARNING_LOG("Discarding OnCaptureStarted callback, mode:%{public}d."
228 "exposureTime is not found",
229 meta.data.ui32[0]);
230 }
231 break;
232 }
233 default:
234 photoOutput->GetApplicationCallback()->OnCaptureStarted(captureId);
235 break;
236 }
237 return CAMERA_OK;
238 }
239
OnCaptureStarted(const int32_t captureId,uint32_t exposureTime)240 int32_t HStreamCaptureCallbackImpl::OnCaptureStarted(const int32_t captureId, uint32_t exposureTime)
241 {
242 CAMERA_SYNC_TRACE;
243 auto photoOutput = GetPhotoOutput();
244 CHECK_ERROR_RETURN_RET_LOG(photoOutput == nullptr, CAMERA_OK,
245 "HStreamCaptureCallbackImpl::OnCaptureStarted photoOutput is nullptr");
246 auto callback = photoOutput->GetApplicationCallback();
247 CHECK_ERROR_RETURN_RET_LOG(callback == nullptr, CAMERA_OK,
248 "HStreamCaptureCallbackImpl::OnCaptureStarted callback is nullptr");
249 photoOutput->GetApplicationCallback()->OnCaptureStarted(captureId, exposureTime);
250 return CAMERA_OK;
251 }
252
OnCaptureEnded(const int32_t captureId,const int32_t frameCount)253 int32_t HStreamCaptureCallbackImpl::OnCaptureEnded(const int32_t captureId, const int32_t frameCount)
254 {
255 CAMERA_SYNC_TRACE;
256 auto photoOutput = GetPhotoOutput();
257 CHECK_ERROR_RETURN_RET_LOG(photoOutput == nullptr, CAMERA_OK,
258 "HStreamCaptureCallbackImpl::OnCaptureEnded photoOutput is nullptr");
259 auto callback = photoOutput->GetApplicationCallback();
260 CHECK_ERROR_RETURN_RET_LOG(callback == nullptr, CAMERA_OK,
261 "HStreamCaptureCallbackImpl::OnCaptureEnded callback is nullptr");
262 callback->OnCaptureEnded(captureId, frameCount);
263 return CAMERA_OK;
264 }
265
OnCaptureError(const int32_t captureId,const int32_t errorCode)266 int32_t HStreamCaptureCallbackImpl::OnCaptureError(const int32_t captureId, const int32_t errorCode)
267 {
268 auto photoOutput = GetPhotoOutput();
269 CHECK_ERROR_RETURN_RET_LOG(photoOutput == nullptr, CAMERA_OK,
270 "HStreamCaptureCallbackImpl::OnCaptureError photoOutput is nullptr");
271 auto callback = photoOutput->GetApplicationCallback();
272 CHECK_ERROR_RETURN_RET_LOG(callback == nullptr, CAMERA_OK,
273 "HStreamCaptureCallbackImpl::OnCaptureError callback is nullptr");
274 callback->OnCaptureError(captureId, errorCode);
275 return CAMERA_OK;
276 }
277
OnFrameShutter(const int32_t captureId,const uint64_t timestamp)278 int32_t HStreamCaptureCallbackImpl::OnFrameShutter(const int32_t captureId, const uint64_t timestamp)
279 {
280 CAMERA_SYNC_TRACE;
281 auto photoOutput = GetPhotoOutput();
282 CHECK_ERROR_RETURN_RET_LOG(photoOutput == nullptr, CAMERA_OK,
283 "HStreamCaptureCallbackImpl::OnFrameShutter photoOutput is nullptr");
284 auto callback = photoOutput->GetApplicationCallback();
285 CHECK_ERROR_RETURN_RET_LOG(callback == nullptr, CAMERA_OK,
286 "HStreamCaptureCallbackImpl::OnFrameShutter callback is nullptr");
287 callback->OnFrameShutter(captureId, timestamp);
288 return CAMERA_OK;
289 }
290
OnFrameShutterEnd(const int32_t captureId,const uint64_t timestamp)291 int32_t HStreamCaptureCallbackImpl::OnFrameShutterEnd(const int32_t captureId, const uint64_t timestamp)
292 {
293 CAMERA_SYNC_TRACE;
294 auto photoOutput = GetPhotoOutput();
295 CHECK_ERROR_RETURN_RET_LOG(photoOutput == nullptr, CAMERA_OK,
296 "HStreamCaptureCallbackImpl::OnFrameShutterEnd photoOutput is nullptr");
297 auto callback = photoOutput->GetApplicationCallback();
298 CHECK_ERROR_RETURN_RET_LOG(callback == nullptr, CAMERA_OK,
299 "HStreamCaptureCallbackImpl::OnFrameShutterEnd callback is nullptr");
300 callback->OnFrameShutterEnd(captureId, timestamp);
301 return CAMERA_OK;
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_ERROR_RETURN_RET_LOG(photoOutput == nullptr, CAMERA_OK,
309 "HStreamCaptureCallbackImpl::OnCaptureReady photoOutput is nullptr");
310 auto callback = photoOutput->GetApplicationCallback();
311 CHECK_ERROR_RETURN_RET_LOG(callback == nullptr, CAMERA_OK,
312 "HStreamCaptureCallbackImpl::OnCaptureReady callback is nullptr");
313 callback->OnCaptureReady(captureId, timestamp);
314 return CAMERA_OK;
315 }
316
PhotoOutput(sptr<IBufferProducer> bufferProducer)317 PhotoOutput::PhotoOutput(sptr<IBufferProducer> bufferProducer)
318 : CaptureOutput(CAPTURE_OUTPUT_TYPE_PHOTO, StreamType::CAPTURE, bufferProducer, nullptr)
319 {
320 defaultCaptureSetting_ = nullptr;
321 photoProxy_ = nullptr;
322 watchDogHandle_ = 0;
323 }
324
~PhotoOutput()325 PhotoOutput::~PhotoOutput()
326 {
327 MEDIA_DEBUG_LOG("Enter Into PhotoOutput::~PhotoOutput()");
328 defaultCaptureSetting_ = nullptr;
329 }
330
SetNativeSurface(bool isNativeSurface)331 void PhotoOutput::SetNativeSurface(bool isNativeSurface)
332 {
333 MEDIA_INFO_LOG("Enter Into SetNativeSurface %{public}d", isNativeSurface);
334 isNativeSurface_ = isNativeSurface;
335 }
336
SetCallbackFlag(uint8_t callbackFlag)337 void PhotoOutput::SetCallbackFlag(uint8_t callbackFlag)
338 {
339 std::lock_guard<std::mutex> lock(callbackMutex_);
340 CHECK_ERROR_RETURN_LOG(!isNativeSurface_, "SetCallbackFlag when register imageReciver");
341 bool beforeStatus = IsEnableDeferred();
342 callbackFlag_ = callbackFlag;
343 bool afterStatus = IsEnableDeferred();
344 // if session is commit or start, and isEnableDeferred is oppsite, need to restart session config
345 auto session = GetSession();
346 if (beforeStatus != afterStatus && session) {
347 if (session->IsSessionStarted()) {
348 FocusMode focusMode = session->GetFocusMode();
349 MEDIA_INFO_LOG("session restart when callback status changed %d", focusMode);
350 session->BeginConfig();
351 session->CommitConfig();
352 session->LockForControl();
353 session->SetFocusMode(focusMode);
354 session->UnlockForControl();
355 session->Start();
356 } else if (session->IsSessionCommited()) {
357 FocusMode focusMode = session->GetFocusMode();
358 MEDIA_INFO_LOG("session recommit when callback status changed %d", focusMode);
359 session->BeginConfig();
360 session->CommitConfig();
361 session->LockForControl();
362 session->SetFocusMode(focusMode);
363 session->UnlockForControl();
364 }
365 }
366 }
367
IsYuvOrHeifPhoto()368 bool PhotoOutput::IsYuvOrHeifPhoto()
369 {
370 if (!GetPhotoProfile()) {
371 return false;
372 }
373 bool ret = GetPhotoProfile()->GetCameraFormat() == CAMERA_FORMAT_YUV_420_SP;
374 MEDIA_INFO_LOG("IsYuvOrHeifPhoto res = %{public}d", ret);
375 return ret;
376 }
377
SetAuxiliaryPhotoHandle(uint32_t handle)378 void PhotoOutput::SetAuxiliaryPhotoHandle(uint32_t handle)
379 {
380 std::lock_guard<std::mutex> lock(watchDogHandleMutex_);
381 watchDogHandle_ = handle;
382 }
383
384
GetAuxiliaryPhotoHandle()385 uint32_t PhotoOutput::GetAuxiliaryPhotoHandle()
386 {
387 std::lock_guard<std::mutex> lock(watchDogHandleMutex_);
388 return watchDogHandle_;
389 }
390
CreateMultiChannel()391 void PhotoOutput::CreateMultiChannel()
392 {
393 CAMERA_SYNC_TRACE;
394 auto streamCapturePtr = static_cast<IStreamCapture*>(GetStream().GetRefPtr());
395 if (streamCapturePtr == nullptr) {
396 MEDIA_ERR_LOG("PhotoOutput::CreateMultiChannel Failed!streamCapturePtr is nullptr");
397 return;
398 }
399 std::string retStr = "";
400 int32_t ret = 0;
401 if (gainmapSurface_ == nullptr) {
402 std::string bufferName = "gainmapImage";
403 gainmapSurface_ = Surface::CreateSurfaceAsConsumer(bufferName);
404 ret = streamCapturePtr->SetBufferProducerInfo(bufferName, gainmapSurface_->GetProducer());
405 retStr += (ret != CAMERA_OK ? bufferName + "," : retStr);
406 }
407 if (deepSurface_ == nullptr) {
408 std::string bufferName = "deepImage";
409 deepSurface_ = Surface::CreateSurfaceAsConsumer(bufferName);
410 ret = streamCapturePtr->SetBufferProducerInfo(bufferName, deepSurface_->GetProducer());
411 retStr += (ret != CAMERA_OK ? bufferName + "," : retStr);
412 }
413 if (exifSurface_ == nullptr) {
414 std::string bufferName = "exifImage";
415 exifSurface_ = Surface::CreateSurfaceAsConsumer(bufferName);
416 ret = streamCapturePtr->SetBufferProducerInfo(bufferName, exifSurface_->GetProducer());
417 retStr += (ret != CAMERA_OK ? bufferName + "," : retStr);
418 }
419 if (debugSurface_ == nullptr) {
420 std::string bufferName = "debugImage";
421 debugSurface_ = Surface::CreateSurfaceAsConsumer(bufferName);
422 ret = streamCapturePtr->SetBufferProducerInfo(bufferName, debugSurface_->GetProducer());
423 retStr += (ret != CAMERA_OK ? bufferName + "," : retStr);
424 }
425 MEDIA_INFO_LOG("PhotoOutput::CreateMultiChannel! failed channel is = %{public}s", retStr.c_str());
426 }
427
IsEnableDeferred()428 bool PhotoOutput::IsEnableDeferred()
429 {
430 CHECK_ERROR_RETURN_RET(!isNativeSurface_, false);
431 bool isEnabled = (callbackFlag_ & CAPTURE_PHOTO_ASSET) != 0 || (callbackFlag_ & CAPTURE_PHOTO) == 0;
432 MEDIA_INFO_LOG("Enter Into PhotoOutput::IsEnableDeferred %{public}d", isEnabled);
433 return isEnabled;
434 }
435
SetCallback(std::shared_ptr<PhotoStateCallback> callback)436 void PhotoOutput::SetCallback(std::shared_ptr<PhotoStateCallback> callback)
437 {
438 std::lock_guard<std::mutex> lock(outputCallbackMutex_);
439 appCallback_ = callback;
440 if (appCallback_ != nullptr) {
441 if (cameraSvcCallback_ == nullptr) {
442 cameraSvcCallback_ = new (std::nothrow) HStreamCaptureCallbackImpl(this);
443 if (cameraSvcCallback_ == nullptr) {
444 MEDIA_ERR_LOG("PhotoOutput::SetCallback new HStreamCaptureCallbackImpl Failed to register callback");
445 appCallback_ = nullptr;
446 return;
447 }
448 }
449 auto itemStream = static_cast<IStreamCapture*>(GetStream().GetRefPtr());
450 int32_t errorCode = CAMERA_OK;
451 if (itemStream) {
452 errorCode = itemStream->SetCallback(cameraSvcCallback_);
453 } else {
454 MEDIA_ERR_LOG("PhotoOutput::SetCallback() itemStream is nullptr");
455 }
456 if (errorCode != CAMERA_OK) {
457 MEDIA_ERR_LOG("PhotoOutput::SetCallback: Failed to register callback, errorCode: %{public}d", errorCode);
458 cameraSvcCallback_ = nullptr;
459 appCallback_ = nullptr;
460 }
461 }
462 }
463
SetThumbnailListener(sptr<IBufferConsumerListener> & listener)464 void PhotoOutput::SetThumbnailListener(sptr<IBufferConsumerListener>& listener)
465 {
466 if (thumbnailSurface_) {
467 SurfaceError ret = thumbnailSurface_->RegisterConsumerListener(listener);
468 CHECK_ERROR_PRINT_LOG(ret != SURFACE_ERROR_OK,
469 "PhotoOutput::SetThumbnailListener Surface consumer listener registration failed");
470 } else {
471 MEDIA_ERR_LOG("PhotoOutput SetThumbnailListener surface is null");
472 }
473 }
474
SetThumbnail(bool isEnabled)475 int32_t PhotoOutput::SetThumbnail(bool isEnabled)
476 {
477 CAMERA_SYNC_TRACE;
478 sptr<CameraDevice> cameraObj;
479 auto session = GetSession();
480 CHECK_ERROR_RETURN_RET_LOG(session == nullptr, SESSION_NOT_RUNNING,
481 "PhotoOutput SetThumbnail error!, session is nullptr");
482 auto inputDevice = session->GetInputDevice();
483 CHECK_ERROR_RETURN_RET_LOG(inputDevice == nullptr, SESSION_NOT_RUNNING,
484 "PhotoOutput SetThumbnail error!, inputDevice is nullptr");
485 cameraObj = inputDevice->GetCameraDeviceInfo();
486 CHECK_ERROR_RETURN_RET_LOG(cameraObj == nullptr, SESSION_NOT_RUNNING,
487 "PhotoOutput SetThumbnail error!, cameraObj is nullptr");
488 !thumbnailSurface_ && (thumbnailSurface_ = Surface::CreateSurfaceAsConsumer("quickThumbnail"));
489 CHECK_ERROR_RETURN_RET_LOG(thumbnailSurface_ == nullptr, SERVICE_FATL_ERROR,
490 "PhotoOutput SetThumbnail Failed to create surface");
491 auto streamCapturePtr = static_cast<IStreamCapture*>(GetStream().GetRefPtr());
492 CHECK_ERROR_RETURN_RET(streamCapturePtr == nullptr, SERVICE_FATL_ERROR);
493 return streamCapturePtr->SetThumbnail(isEnabled, thumbnailSurface_->GetProducer());
494 }
495
EnableRawDelivery(bool enabled)496 int32_t PhotoOutput::EnableRawDelivery(bool enabled)
497 {
498 CAMERA_SYNC_TRACE;
499 auto session = GetSession();
500 CHECK_ERROR_RETURN_RET_LOG(session == nullptr, SESSION_NOT_RUNNING,
501 "PhotoOutput EnableRawDelivery error!, session is nullptr");
502 auto streamCapturePtr = static_cast<IStreamCapture*>(GetStream().GetRefPtr());
503 CHECK_ERROR_RETURN_RET_LOG(streamCapturePtr == nullptr, SERVICE_FATL_ERROR,
504 "PhotoOutput::EnableRawDelivery Failed to GetStream");
505 int32_t ret = CAMERA_OK;
506 if (rawPhotoSurface_ == nullptr) {
507 std::string bufferName = "rawImage";
508 rawPhotoSurface_ = Surface::CreateSurfaceAsConsumer(bufferName);
509 ret = streamCapturePtr->SetBufferProducerInfo(bufferName, rawPhotoSurface_->GetProducer());
510 CHECK_ERROR_RETURN_RET_LOG(ret != CAMERA_OK, SERVICE_FATL_ERROR,
511 "PhotoOutput::EnableRawDelivery Failed to SetBufferProducerInfo");
512 }
513 if (session->EnableRawDelivery(enabled) == CameraErrorCode::SUCCESS) {
514 ret = streamCapturePtr->EnableRawDelivery(enabled);
515 CHECK_ERROR_RETURN_RET_LOG(ret != CAMERA_OK, SERVICE_FATL_ERROR,
516 "PhotoOutput::EnableRawDelivery session EnableRawDelivery Failed");
517 }
518 isRawImageDelivery_ = enabled;
519 return ret;
520 }
521
SetRawPhotoInfo(sptr<Surface> & surface)522 int32_t PhotoOutput::SetRawPhotoInfo(sptr<Surface> &surface)
523 {
524 CAMERA_SYNC_TRACE;
525 auto streamCapturePtr = static_cast<IStreamCapture*>(GetStream().GetRefPtr());
526 CHECK_ERROR_RETURN_RET_LOG(streamCapturePtr == nullptr, SERVICE_FATL_ERROR,
527 "PhotoOutput::SetRawPhotoInfo Failed to create surface");
528 rawPhotoSurface_ = surface;
529 return streamCapturePtr->SetBufferProducerInfo("rawImage", rawPhotoSurface_->GetProducer());
530 }
531
GetApplicationCallback()532 std::shared_ptr<PhotoStateCallback> PhotoOutput::GetApplicationCallback()
533 {
534 std::lock_guard<std::mutex> lock(outputCallbackMutex_);
535 return appCallback_;
536 }
537
Capture(std::shared_ptr<PhotoCaptureSetting> photoCaptureSettings)538 int32_t PhotoOutput::Capture(std::shared_ptr<PhotoCaptureSetting> photoCaptureSettings)
539 {
540 CameraReportDfxUtils::GetInstance()->SetFirstBufferStartInfo();
541 std::lock_guard<std::mutex> lock(asyncOpMutex_);
542 auto session = GetSession();
543 CHECK_ERROR_RETURN_RET_LOG(session == nullptr || !session->IsSessionCommited(),
544 CameraErrorCode::SESSION_NOT_RUNNING, "PhotoOutput Failed to Capture with setting, session not commited");
545 CHECK_ERROR_RETURN_RET_LOG(GetStream() == nullptr,
546 CameraErrorCode::SERVICE_FATL_ERROR, "PhotoOutput Failed to Capture with setting, GetStream is nullptr");
547 defaultCaptureSetting_ = photoCaptureSettings;
548 auto itemStream = static_cast<IStreamCapture*>(GetStream().GetRefPtr());
549 int32_t errCode = CAMERA_UNKNOWN_ERROR;
550 if (itemStream) {
551 MEDIA_DEBUG_LOG("Capture start");
552 session->EnableMovingPhotoMirror(photoCaptureSettings->GetMirror());
553 errCode = itemStream->Capture(photoCaptureSettings->GetCaptureMetadataSetting());
554 MEDIA_DEBUG_LOG("Capture End");
555 } else {
556 MEDIA_ERR_LOG("PhotoOutput::Capture() itemStream is nullptr");
557 }
558 CHECK_ERROR_PRINT_LOG(errCode != CAMERA_OK, "PhotoOutput Failed to Capture!, errCode: %{public}d", errCode);
559 return ServiceToCameraError(errCode);
560 }
561
Capture()562 int32_t PhotoOutput::Capture()
563 {
564 std::lock_guard<std::mutex> lock(asyncOpMutex_);
565 auto session = GetSession();
566 CHECK_ERROR_RETURN_RET_LOG(session == nullptr || !session->IsSessionCommited(),
567 CameraErrorCode::SESSION_NOT_RUNNING, "PhotoOutput Failed to Capture, session not commited");
568 CHECK_ERROR_RETURN_RET_LOG(GetStream() == nullptr, CameraErrorCode::SERVICE_FATL_ERROR,
569 "PhotoOutput Failed to Capture, GetStream is nullptr");
570 int32_t items = 0;
571 int32_t dataLength = 0;
572 std::shared_ptr<Camera::CameraMetadata> captureMetadataSetting =
573 std::make_shared<Camera::CameraMetadata>(items, dataLength);
574 auto itemStream = static_cast<IStreamCapture*>(GetStream().GetRefPtr());
575 int32_t errCode = CAMERA_UNKNOWN_ERROR;
576 if (itemStream) {
577 MEDIA_DEBUG_LOG("Capture start");
578 session->EnableMovingPhotoMirror(false);
579 errCode = itemStream->Capture(captureMetadataSetting);
580 MEDIA_DEBUG_LOG("Capture end");
581 } else {
582 MEDIA_ERR_LOG("PhotoOutput::Capture() itemStream is nullptr");
583 }
584 CHECK_ERROR_PRINT_LOG(errCode != CAMERA_OK, "PhotoOutput Failed to Capture!, errCode: %{public}d", errCode);
585 return ServiceToCameraError(errCode);
586 }
587
CancelCapture()588 int32_t PhotoOutput::CancelCapture()
589 {
590 std::lock_guard<std::mutex> lock(asyncOpMutex_);
591 auto session = GetSession();
592 CHECK_ERROR_RETURN_RET_LOG(session == nullptr || !session->IsSessionCommited(),
593 CameraErrorCode::SESSION_NOT_RUNNING, "PhotoOutput Failed to CancelCapture, session not commited");
594 CHECK_ERROR_RETURN_RET_LOG(GetStream() == nullptr,
595 CameraErrorCode::SERVICE_FATL_ERROR, "PhotoOutput Failed to CancelCapture, GetStream is nullptr");
596 auto itemStream = static_cast<IStreamCapture*>(GetStream().GetRefPtr());
597 int32_t errCode = CAMERA_UNKNOWN_ERROR;
598 if (itemStream) {
599 errCode = itemStream->CancelCapture();
600 } else {
601 MEDIA_ERR_LOG("PhotoOutput::CancelCapture() itemStream is nullptr");
602 }
603 CHECK_ERROR_PRINT_LOG(errCode != CAMERA_OK, "PhotoOutput Failed to CancelCapture, errCode: %{public}d", errCode);
604 return ServiceToCameraError(errCode);
605 }
606
ConfirmCapture()607 int32_t PhotoOutput::ConfirmCapture()
608 {
609 std::lock_guard<std::mutex> lock(asyncOpMutex_);
610 auto session = GetSession();
611 CHECK_ERROR_RETURN_RET_LOG(session == nullptr || !session->IsSessionCommited(),
612 CameraErrorCode::SESSION_NOT_RUNNING, "PhotoOutput Failed to ConfirmCapture, session not commited");
613 CHECK_ERROR_RETURN_RET_LOG(GetStream() == nullptr, CameraErrorCode::SERVICE_FATL_ERROR,
614 "PhotoOutput Failed to ConfirmCapture, GetStream is nullptr");
615 auto itemStream = static_cast<IStreamCapture*>(GetStream().GetRefPtr());
616 int32_t errCode = CAMERA_UNKNOWN_ERROR;
617 if (itemStream) {
618 errCode = itemStream->ConfirmCapture();
619 } else {
620 MEDIA_ERR_LOG("PhotoOutput::ConfirmCapture() itemStream is nullptr");
621 }
622 CHECK_ERROR_PRINT_LOG(errCode != CAMERA_OK, "PhotoOutput Failed to ConfirmCapture, errCode: %{public}d", errCode);
623 return ServiceToCameraError(errCode);
624 }
625
CreateStream()626 int32_t PhotoOutput::CreateStream()
627 {
628 auto stream = GetStream();
629 CHECK_ERROR_RETURN_RET_LOG(stream != nullptr, CameraErrorCode::OPERATION_NOT_ALLOWED,
630 "PhotoOutput::CreateStream stream is not null");
631 auto producer = GetBufferProducer();
632 CHECK_ERROR_RETURN_RET_LOG(producer == nullptr, CameraErrorCode::OPERATION_NOT_ALLOWED,
633 "PhotoOutput::CreateStream producer is null");
634 sptr<IStreamCapture> streamPtr = nullptr;
635 auto photoProfile = GetPhotoProfile();
636 CHECK_ERROR_RETURN_RET_LOG(photoProfile == nullptr, CameraErrorCode::SERVICE_FATL_ERROR,
637 "PhotoOutput::CreateStream photoProfile is null");
638
639 int32_t res = CameraManager::GetInstance()->CreatePhotoOutputStream(streamPtr, *photoProfile, GetBufferProducer());
640 CHECK_ERROR_PRINT_LOG(res != CameraErrorCode::SUCCESS,
641 "PhotoOutput::CreateStream fail! error code :%{public}d", res);
642 SetStream(streamPtr);
643 return res;
644 }
645
Release()646 int32_t PhotoOutput::Release()
647 {
648 {
649 std::lock_guard<std::mutex> lock(outputCallbackMutex_);
650 cameraSvcCallback_ = nullptr;
651 appCallback_ = nullptr;
652 }
653 std::lock_guard<std::mutex> lock(asyncOpMutex_);
654 MEDIA_DEBUG_LOG("Enter Into PhotoOutput::Release");
655 CHECK_ERROR_RETURN_RET_LOG(GetStream() == nullptr, CameraErrorCode::SERVICE_FATL_ERROR,
656 "PhotoOutput Failed to Release!, GetStream is nullptr");
657 auto itemStream = static_cast<IStreamCapture*>(GetStream().GetRefPtr());
658 int32_t errCode = CAMERA_UNKNOWN_ERROR;
659 if (itemStream) {
660 errCode = itemStream->Release();
661 } else {
662 MEDIA_ERR_LOG("PhotoOutput::Release() itemStream is nullptr");
663 }
664 CHECK_ERROR_PRINT_LOG(errCode != CAMERA_OK, "PhotoOutput Failed to release!, errCode: %{public}d", errCode);
665 defaultCaptureSetting_ = nullptr;
666 CaptureOutput::Release();
667 return ServiceToCameraError(errCode);
668 }
669
IsMirrorSupported()670 bool PhotoOutput::IsMirrorSupported()
671 {
672 auto session = GetSession();
673 CHECK_ERROR_RETURN_RET_LOG(session == nullptr, false,
674 "PhotoOutput IsMirrorSupported error!, session is nullptr");
675 auto inputDevice = session->GetInputDevice();
676 CHECK_ERROR_RETURN_RET_LOG(inputDevice == nullptr, false,
677 "PhotoOutput IsMirrorSupported error!, inputDevice is nullptr");
678 sptr<CameraDevice> cameraObj = inputDevice->GetCameraDeviceInfo();
679 CHECK_ERROR_RETURN_RET_LOG(cameraObj == nullptr, false,
680 "PhotoOutput IsMirrorSupported error!, cameraObj is nullptr");
681 std::shared_ptr<Camera::CameraMetadata> metadata = cameraObj->GetMetadata();
682 CHECK_ERROR_RETURN_RET(metadata == nullptr, false);
683
684 camera_metadata_item_t item;
685 int32_t ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_CONTROL_CAPTURE_MIRROR_SUPPORTED, &item);
686 CHECK_ERROR_RETURN_RET_LOG(ret != CAM_META_SUCCESS, false,
687 "PhotoOutput Can not find OHOS_CONTROL_CAPTURE_MIRROR_SUPPORTED");
688 int step = 2;
689 const int32_t canMirrorVideoAndPhoto = 2;
690 const int32_t canMirrorPhotoOnly = 1;
691 bool isMirrorEnabled = false;
692 SceneMode currentSceneMode = session->GetMode();
693 for (int i = 0; i < static_cast<int>(item.count); i += step) {
694 MEDIA_DEBUG_LOG("mode u8[%{public}d]: %{public}d, u8[%{public}d], %{public}d",
695 i, item.data.u8[i], i + 1, item.data.u8[i + 1]);
696 if (currentSceneMode == static_cast<int>(item.data.u8[i])) {
697 isMirrorEnabled = (
698 item.data.u8[i + 1] == canMirrorPhotoOnly ||
699 item.data.u8[i + 1] == canMirrorVideoAndPhoto) ? true : false;
700 }
701 }
702 MEDIA_DEBUG_LOG("IsMirrorSupported isSupport: %{public}d", isMirrorEnabled);
703 return isMirrorEnabled;
704 }
705
EnableMirror(bool isEnable)706 int32_t PhotoOutput::EnableMirror(bool isEnable)
707 {
708 auto session = GetSession();
709 CHECK_ERROR_RETURN_RET_LOG(session == nullptr, CameraErrorCode::SESSION_NOT_RUNNING,
710 "PhotoOutput EnableMirror error!, session is nullptr");
711
712 int32_t ret = CAMERA_UNKNOWN_ERROR;
713 if (IsMirrorSupported()) {
714 ret = session->EnableMovingPhotoMirror(isEnable);
715 CHECK_ERROR_RETURN_RET_LOG(ret != CameraErrorCode::SUCCESS, ret,
716 "PhotoOutput EnableMirror error!, ret is not success");
717 } else {
718 MEDIA_ERR_LOG("PhotoOutput EnableMirror error!, mirror is not supported");
719 }
720 return ret;
721 }
722
IsQuickThumbnailSupported()723 int32_t PhotoOutput::IsQuickThumbnailSupported()
724 {
725 int32_t isQuickThumbnailEnabled = -1;
726 auto session = GetSession();
727 CHECK_ERROR_RETURN_RET_LOG(session == nullptr, SESSION_NOT_RUNNING,
728 "PhotoOutput IsQuickThumbnailSupported error!, session is nullptr");
729 auto inputDevice = session->GetInputDevice();
730 CHECK_ERROR_RETURN_RET_LOG(inputDevice == nullptr, SESSION_NOT_RUNNING,
731 "PhotoOutput IsQuickThumbnailSupported error!, inputDevice is nullptr");
732 sptr<CameraDevice> cameraObj = inputDevice->GetCameraDeviceInfo();
733 CHECK_ERROR_RETURN_RET_LOG(cameraObj == nullptr, SESSION_NOT_RUNNING,
734 "PhotoOutput IsQuickThumbnailSupported error!, cameraObj is nullptr");
735 std::shared_ptr<Camera::CameraMetadata> metadata = cameraObj->GetMetadata();
736 CHECK_ERROR_RETURN_RET(metadata == nullptr, SESSION_NOT_RUNNING);
737 camera_metadata_item_t item;
738 int32_t ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_STREAM_QUICK_THUMBNAIL_AVAILABLE, &item);
739 if (ret == CAM_META_SUCCESS) {
740 isQuickThumbnailEnabled = (item.data.u8[0] == 1) ? 0 : -1;
741 }
742 const int32_t nightMode = 4;
743 if ((session->GetMode() == nightMode && (cameraObj->GetPosition() != CAMERA_POSITION_FRONT)) ||
744 session->GetMode() == LIGHT_PAINTING) {
745 isQuickThumbnailEnabled = -1;
746 }
747 return isQuickThumbnailEnabled;
748 }
749
IsRawDeliverySupported()750 int32_t PhotoOutput::IsRawDeliverySupported()
751 {
752 int32_t isRawDevliveryEnabled = -1;
753 auto session = GetSession();
754 CHECK_ERROR_RETURN_RET_LOG(session == nullptr, SESSION_NOT_RUNNING,
755 "PhotoOutput IsRawDeliverySupported error!, session is nullptr");
756 const int32_t professionalPhotoMode = 11;
757 if ((session->GetMode() == professionalPhotoMode)) {
758 isRawDevliveryEnabled = 1;
759 }
760 return isRawDevliveryEnabled;
761 }
762
DeferImageDeliveryFor(DeferredDeliveryImageType type)763 int32_t PhotoOutput::DeferImageDeliveryFor(DeferredDeliveryImageType type)
764 {
765 MEDIA_INFO_LOG("PhotoOutput DeferImageDeliveryFor type:%{public}d!", type);
766 CAMERA_SYNC_TRACE;
767 sptr<CameraDevice> cameraObj;
768 auto session = GetSession();
769 CHECK_ERROR_RETURN_RET_LOG(session == nullptr, SESSION_NOT_RUNNING,
770 "PhotoOutput DeferImageDeliveryFor error!, session is nullptr");
771 auto inputDevice = session->GetInputDevice();
772 CHECK_ERROR_RETURN_RET_LOG(inputDevice == nullptr, SESSION_NOT_RUNNING,
773 "PhotoOutput DeferImageDeliveryFor error!, inputDevice is nullptr");
774 cameraObj = inputDevice->GetCameraDeviceInfo();
775 CHECK_ERROR_RETURN_RET_LOG(cameraObj == nullptr, SESSION_NOT_RUNNING,
776 "PhotoOutput DeferImageDeliveryFor error!, cameraObj is nullptr");
777 session->EnableDeferredType(type, true);
778 session->SetUserId();
779 return 0;
780 }
781
IsDeferredImageDeliverySupported(DeferredDeliveryImageType type)782 int32_t PhotoOutput::IsDeferredImageDeliverySupported(DeferredDeliveryImageType type)
783 {
784 MEDIA_INFO_LOG("IsDeferredImageDeliverySupported type:%{public}d!", type);
785 int32_t isSupported = -1;
786 if (type == DELIVERY_NONE) {
787 return isSupported;
788 }
789 sptr<CameraDevice> cameraObj;
790 auto session = GetSession();
791 CHECK_ERROR_RETURN_RET_LOG(session == nullptr, SESSION_NOT_RUNNING,
792 "PhotoOutput IsDeferredImageDeliverySupported error!, session is nullptr");
793 auto inputDevice = session->GetInputDevice();
794 CHECK_ERROR_RETURN_RET_LOG(inputDevice == nullptr, SESSION_NOT_RUNNING,
795 "PhotoOutput IsDeferredImageDeliverySupported error!, inputDevice is nullptr");
796 cameraObj = inputDevice->GetCameraDeviceInfo();
797 CHECK_ERROR_RETURN_RET_LOG(cameraObj == nullptr, SESSION_NOT_RUNNING,
798 "PhotoOutput IsDeferredImageDeliverySupported error!, cameraObj is nullptr");
799 int32_t curMode = session->GetMode();
800 int32_t modeSupportType = cameraObj->modeDeferredType_[curMode];
801 MEDIA_INFO_LOG("IsDeferredImageDeliverySupported curMode:%{public}d, modeSupportType:%{public}d",
802 curMode, modeSupportType);
803 if (modeSupportType == type) {
804 isSupported = 0;
805 }
806 return isSupported;
807 }
808
IsDeferredImageDeliveryEnabled(DeferredDeliveryImageType type)809 int32_t PhotoOutput::IsDeferredImageDeliveryEnabled(DeferredDeliveryImageType type)
810 {
811 MEDIA_INFO_LOG("PhotoOutput IsDeferredImageDeliveryEnabled type:%{public}d!", type);
812 int32_t isEnabled = -1;
813 auto session = GetSession();
814 CHECK_ERROR_RETURN_RET_LOG(session == nullptr, SESSION_NOT_RUNNING,
815 "PhotoOutput IsDeferredImageDeliveryEnabled error!, session is nullptr");
816 auto inputDevice = session->GetInputDevice();
817 CHECK_ERROR_RETURN_RET_LOG(inputDevice == nullptr, SESSION_NOT_RUNNING,
818 "PhotoOutput IsDeferredImageDeliveryEnabled error!, inputDevice is nullptr");
819 sptr<CameraDevice> cameraObj = inputDevice->GetCameraDeviceInfo();
820 CHECK_ERROR_RETURN_RET_LOG(cameraObj == nullptr, SESSION_NOT_RUNNING,
821 "PhotoOutput IsDeferredImageDeliveryEnabled error!, cameraObj is nullptr");
822 isEnabled = session->IsImageDeferred() ? 0 : -1;
823 return isEnabled;
824 }
825
IsAutoHighQualityPhotoSupported(int32_t & isAutoHighQualityPhotoSupported)826 int32_t PhotoOutput::IsAutoHighQualityPhotoSupported(int32_t &isAutoHighQualityPhotoSupported)
827 {
828 MEDIA_INFO_LOG("PhotoOutput IsAutoHighQualityPhotoSupported is called");
829 isAutoHighQualityPhotoSupported = -1;
830 camera_metadata_item_t item;
831 sptr<CameraDevice> cameraObj;
832 auto session = GetSession();
833 CHECK_ERROR_RETURN_RET_LOG(session == nullptr, SESSION_NOT_RUNNING,
834 "PhotoOutput IsAutoHighQualityPhotoSupported error!, session is nullptr");
835 auto inputDevice = session->GetInputDevice();
836 CHECK_ERROR_RETURN_RET_LOG(inputDevice == nullptr, SESSION_NOT_RUNNING,
837 "PhotoOutput IsAutoHighQualityPhotoSupported error!, inputDevice is nullptr");
838 cameraObj = inputDevice->GetCameraDeviceInfo();
839 CHECK_ERROR_RETURN_RET_LOG(cameraObj == nullptr, SESSION_NOT_RUNNING,
840 "PhotoOutput IsAutoHighQualityPhotoSupported error!, cameraObj is nullptr");
841 std::shared_ptr<Camera::CameraMetadata> metadata = cameraObj->GetMetadata();
842 CHECK_ERROR_RETURN_RET(metadata == nullptr, SESSION_NOT_RUNNING);
843 int32_t ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_HIGH_QUALITY_SUPPORT, &item);
844 if (ret == CAM_META_SUCCESS) {
845 isAutoHighQualityPhotoSupported = (item.data.u8[1] == 1) ? 0 : -1; // default mode
846 }
847
848 int headLenPerMode = 2;
849 SceneMode currentSceneMode = session->GetMode();
850 for (int i = 0; i < static_cast<int>(item.count); i += headLenPerMode) {
851 if (currentSceneMode == static_cast<int>(item.data.u8[i])) {
852 isAutoHighQualityPhotoSupported = (item.data.u8[i + 1] == 1) ? 0 : -1;
853 }
854 }
855 MEDIA_INFO_LOG("PhotoOutput IsAutoHighQualityPhotoSupported curMode:%{public}d, modeSupportType:%{public}d",
856 currentSceneMode, isAutoHighQualityPhotoSupported);
857 return CAMERA_OK;
858 }
859
EnableAutoHighQualityPhoto(bool enabled)860 int32_t PhotoOutput::EnableAutoHighQualityPhoto(bool enabled)
861 {
862 MEDIA_DEBUG_LOG("PhotoOutput EnableAutoHighQualityPhoto");
863 auto session = GetSession();
864 CHECK_ERROR_RETURN_RET_LOG(session == nullptr, SESSION_NOT_RUNNING,
865 "PhotoOutput EnableAutoHighQualityPhoto error!, session is nullptr");
866 auto inputDevice = session->GetInputDevice();
867 CHECK_ERROR_RETURN_RET_LOG(inputDevice == nullptr, SESSION_NOT_RUNNING,
868 "PhotoOutput EnableAutoHighQualityPhoto error!, inputDevice is nullptr");
869 int32_t isAutoHighQualityPhotoSupported;
870 int32_t ret = IsAutoHighQualityPhotoSupported(isAutoHighQualityPhotoSupported);
871 CHECK_ERROR_RETURN_RET_LOG(ret != CAMERA_OK, OPERATION_NOT_ALLOWED,
872 "PhotoOutput EnableAutoHighQualityPhoto error");
873 CHECK_ERROR_RETURN_RET_LOG(isAutoHighQualityPhotoSupported == -1, INVALID_ARGUMENT,
874 "PhotoOutput EnableAutoHighQualityPhoto not supported");
875 int32_t res = session->EnableAutoHighQualityPhoto(enabled);
876 return res;
877 }
878
ProcessSnapshotDurationUpdates(int32_t snapshotDuration)879 void PhotoOutput::ProcessSnapshotDurationUpdates(int32_t snapshotDuration) __attribute__((no_sanitize("cfi")))
880 {
881 std::lock_guard<std::mutex> lock(outputCallbackMutex_);
882 if (appCallback_ != nullptr) {
883 MEDIA_DEBUG_LOG("appCallback not nullptr");
884 appCallback_->OnEstimatedCaptureDuration(snapshotDuration);
885 }
886 }
887
GetDefaultCaptureSetting()888 std::shared_ptr<PhotoCaptureSetting> PhotoOutput::GetDefaultCaptureSetting()
889 {
890 return defaultCaptureSetting_;
891 }
892
SetMovingPhotoVideoCodecType(int32_t videoCodecType)893 int32_t PhotoOutput::SetMovingPhotoVideoCodecType(int32_t videoCodecType)
894 {
895 std::lock_guard<std::mutex> lock(asyncOpMutex_);
896 MEDIA_DEBUG_LOG("Enter Into PhotoOutput::SetMovingPhotoVideoCodecType");
897 CHECK_ERROR_RETURN_RET_LOG(GetStream() == nullptr, CameraErrorCode::SERVICE_FATL_ERROR,
898 "PhotoOutput Failed to SetMovingPhotoVideoCodecType!, GetStream is nullptr");
899 auto itemStream = static_cast<IStreamCapture*>(GetStream().GetRefPtr());
900 int32_t errCode = CAMERA_UNKNOWN_ERROR;
901 if (itemStream) {
902 errCode = itemStream->SetMovingPhotoVideoCodecType(videoCodecType);
903 } else {
904 MEDIA_ERR_LOG("PhotoOutput::SetMovingPhotoVideoCodecType() itemStream is nullptr");
905 }
906 CHECK_ERROR_PRINT_LOG(errCode != CAMERA_OK, "PhotoOutput Failed to SetMovingPhotoVideoCodecType!, "
907 "errCode: %{public}d", errCode);
908 return ServiceToCameraError(errCode);
909 }
910
CameraServerDied(pid_t pid)911 void PhotoOutput::CameraServerDied(pid_t pid)
912 {
913 MEDIA_ERR_LOG("camera server has died, pid:%{public}d!", pid);
914 std::lock_guard<std::mutex> lock(outputCallbackMutex_);
915 if (appCallback_ != nullptr) {
916 MEDIA_DEBUG_LOG("appCallback not nullptr");
917 int32_t serviceErrorType = ServiceToCameraError(CAMERA_INVALID_STATE);
918 int32_t captureId = -1;
919 appCallback_->OnCaptureError(captureId, serviceErrorType);
920 }
921 }
922
GetPhotoRotation(int32_t imageRotation)923 int32_t PhotoOutput::GetPhotoRotation(int32_t imageRotation)
924 {
925 MEDIA_DEBUG_LOG("PhotoOutput GetPhotoRotation is called");
926 int32_t sensorOrientation = 0;
927 CameraPosition cameraPosition;
928 camera_metadata_item_t item;
929 ImageRotation result = ImageRotation::ROTATION_0;
930 sptr<CameraDevice> cameraObj;
931 auto session = GetSession();
932 CHECK_ERROR_RETURN_RET_LOG(session == nullptr, SERVICE_FATL_ERROR,
933 "PhotoOutput GetPhotoRotation error!, session is nullptr");
934 auto inputDevice = session->GetInputDevice();
935 CHECK_ERROR_RETURN_RET_LOG(inputDevice == nullptr, SERVICE_FATL_ERROR,
936 "PhotoOutput GetPhotoRotation error!, inputDevice is nullptr");
937 cameraObj = inputDevice->GetCameraDeviceInfo();
938 CHECK_ERROR_RETURN_RET_LOG(cameraObj == nullptr, SERVICE_FATL_ERROR,
939 "PhotoOutput GetPhotoRotation error!, cameraObj is nullptr");
940 cameraPosition = cameraObj->GetPosition();
941 CHECK_ERROR_RETURN_RET_LOG(cameraPosition == CAMERA_POSITION_UNSPECIFIED, SERVICE_FATL_ERROR,
942 "PhotoOutput GetPhotoRotation error!, cameraPosition is unspecified");
943 std::shared_ptr<Camera::CameraMetadata> metadata = cameraObj->GetMetadata();
944 CHECK_ERROR_RETURN_RET(metadata == nullptr, SERVICE_FATL_ERROR);
945 int32_t ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_SENSOR_ORIENTATION, &item);
946 CHECK_ERROR_RETURN_RET_LOG(ret != CAM_META_SUCCESS, SERVICE_FATL_ERROR,
947 "PhotoOutput Can not find OHOS_SENSOR_ORIENTATION");
948 sensorOrientation = item.data.i32[0];
949 imageRotation = (imageRotation + ROTATION_45_DEGREES) / ROTATION_90_DEGREES * ROTATION_90_DEGREES;
950 if (cameraPosition == CAMERA_POSITION_BACK) {
951 result = (ImageRotation)((imageRotation + sensorOrientation) % CAPTURE_ROTATION_BASE);
952 } else if (cameraPosition == CAMERA_POSITION_FRONT || cameraPosition == CAMERA_POSITION_FOLD_INNER) {
953 result = (ImageRotation)((sensorOrientation - imageRotation + CAPTURE_ROTATION_BASE) % CAPTURE_ROTATION_BASE);
954 }
955 MEDIA_INFO_LOG("PhotoOutput GetPhotoRotation :result %{public}d, sensorOrientation:%{public}d",
956 result, sensorOrientation);
957 return result;
958 }
959
IsAutoCloudImageEnhancementSupported(bool & isAutoCloudImageEnhancementSupported)960 int32_t PhotoOutput::IsAutoCloudImageEnhancementSupported(bool &isAutoCloudImageEnhancementSupported)
961 {
962 MEDIA_INFO_LOG("PhotoOutput IsAutoCloudImageEnhancementSupported is called");
963 auto session = GetSession();
964 if (session == nullptr) {
965 return SERVICE_FATL_ERROR;
966 }
967 auto inputDevice = session->GetInputDevice();
968 if (inputDevice == nullptr) {
969 MEDIA_ERR_LOG("PhotoOutput IsAutoCloudImageEnhancementSupported error!, inputDevice is nullptr");
970 return SERVICE_FATL_ERROR;
971 }
972 sptr<CameraDevice> cameraObj = inputDevice->GetCameraDeviceInfo();
973 if (cameraObj == nullptr) {
974 MEDIA_ERR_LOG("PhotoOutput IsAutoCloudImageEnhancementSupported error!, cameraObj is nullptr");
975 return SERVICE_FATL_ERROR;
976 }
977 std::shared_ptr<Camera::CameraMetadata> metadata = cameraObj->GetMetadata();
978 if (metadata == nullptr) {
979 return SERVICE_FATL_ERROR;
980 }
981 camera_metadata_item_t item;
982 int32_t ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_AUTO_CLOUD_IMAGE_ENHANCE, &item);
983 if (ret == CAM_META_SUCCESS) {
984 if (item.count == 0) {
985 MEDIA_WARNING_LOG("IsAutoCloudImageEnhancementNotSupported item is nullptr");
986 return CAMERA_OK;
987 }
988 SceneMode currentSceneMode = session->GetMode();
989 for (int i = 0; i < static_cast<int>(item.count); i++) {
990 if (currentSceneMode == static_cast<int>(item.data.i32[i])) {
991 isAutoCloudImageEnhancementSupported = true;
992 return CAMERA_OK;
993 }
994 }
995 }
996 MEDIA_DEBUG_LOG("Judge Auto Cloud Image Enhancement Supported result:%{public}d ",
997 isAutoCloudImageEnhancementSupported);
998 return CAMERA_OK;
999 }
1000
EnableAutoCloudImageEnhancement(bool enabled)1001 int32_t PhotoOutput::EnableAutoCloudImageEnhancement(bool enabled)
1002 {
1003 MEDIA_DEBUG_LOG("PhotoOutput EnableAutoCloudImageEnhancement");
1004 auto captureSession = GetSession();
1005 if (captureSession == nullptr) {
1006 MEDIA_ERR_LOG("PhotoOutput IsAutoHighQualityPhotoSupported error!, captureSession is nullptr");
1007 return SERVICE_FATL_ERROR;
1008 }
1009 auto inputDevice = captureSession->GetInputDevice();
1010 if (inputDevice == nullptr) {
1011 MEDIA_ERR_LOG("PhotoOutput IsAutoHighQualityPhotoSupported error!, inputDevice is nullptr");
1012 return SERVICE_FATL_ERROR;
1013 }
1014 bool isAutoCloudImageEnhancementSupported = false;
1015 int32_t ret = IsAutoCloudImageEnhancementSupported(isAutoCloudImageEnhancementSupported);
1016 CHECK_ERROR_RETURN_RET_LOG(ret != CAMERA_OK, SERVICE_FATL_ERROR,
1017 "PhotoOutput EnableAutoCloudImageEnhancement error");
1018 CHECK_ERROR_RETURN_RET_LOG(isAutoCloudImageEnhancementSupported == false, INVALID_ARGUMENT,
1019 "PhotoOutput EnableAutoCloudImageEnhancement not supported");
1020 int32_t res = captureSession->EnableAutoCloudImageEnhancement(enabled);
1021 return res;
1022 }
1023
IsDepthDataDeliverySupported()1024 bool PhotoOutput::IsDepthDataDeliverySupported()
1025 {
1026 CAMERA_SYNC_TRACE;
1027 MEDIA_INFO_LOG("Enter IsDepthDataDeliverySupported");
1028 return false;
1029 }
1030
EnableDepthDataDelivery(bool enabled)1031 int32_t PhotoOutput::EnableDepthDataDelivery(bool enabled)
1032 {
1033 CAMERA_SYNC_TRACE;
1034 MEDIA_INFO_LOG("Enter EnableDepthDataDelivery, enabled:%{public}d", enabled);
1035 return CameraErrorCode::SUCCESS;
1036 }
1037 } // namespace CameraStandard
1038 } // namespace OHOS
1039