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