• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "hstream_capture.h"
17 #include <cstdint>
18 #include <memory>
19 #include <mutex>
20 #include <uuid.h>
21 #include <iomanip>
22 
23 #include "camera_log.h"
24 #include "camera_server_photo_proxy.h"
25 #include "camera_util.h"
26 #include "camera/v1_4/types.h"
27 #include "datetime_ex.h"
28 #include "hstream_common.h"
29 #include "ipc_skeleton.h"
30 #include "metadata_utils.h"
31 #include "camera_report_uitls.h"
32 #include "photo_asset_interface.h"
33 #include "photo_asset_proxy.h"
34 #include "camera_report_dfx_uitls.h"
35 #include "bms_adapter.h"
36 #include "picture_interface.h"
37 #include "hstream_operator_manager.h"
38 #include "hstream_operator.h"
39 #include "display/graphic/common/v2_1/cm_color_space.h"
40 #include "picture_proxy.h"
41 #include "task_manager.h"
42 #ifdef HOOK_CAMERA_OPERATOR
43 #include "camera_rotate_plugin.h"
44 #endif
45 #include "camera_buffer_manager/photo_buffer_consumer.h"
46 #include "camera_buffer_manager/photo_asset_buffer_consumer.h"
47 #include "camera_buffer_manager/photo_asset_auxiliary_consumer.h"
48 #include "camera_buffer_manager/thumbnail_buffer_consumer.h"
49 #include "camera_buffer_manager/picture_assembler.h"
50 #include "image_receiver.h"
51 #ifdef MEMMGR_OVERRID
52 #include "mem_mgr_client.h"
53 #include "mem_mgr_constant.h"
54 #endif
55 
56 namespace OHOS {
57 namespace CameraStandard {
58 using namespace OHOS::HDI::Camera::V1_0;
59 using namespace OHOS::HDI::Display::Graphic::Common::V2_1;
60 using CM_ColorSpaceType_V2_1 = OHOS::HDI::Display::Graphic::Common::V2_1::CM_ColorSpaceType;
61 static const int32_t CAPTURE_ROTATE_360 = 360;
62 static const std::string BURST_UUID_BEGIN = "";
63 static std::string g_currentBurstUuid = BURST_UUID_BEGIN;
64 static const uint32_t TASKMANAGER_ONE = 1;
65 static const uint32_t TASKMANAGER_FOUR = 4;
66 
GenerateBurstUuid()67 static std::string GenerateBurstUuid()
68 {
69     MEDIA_INFO_LOG("HStreamCapture::GenerateBurstUuid");
70     uuid_t uuid;
71     char str[37] = {}; // UUIDs are 36 characters plus the null terminator
72     uuid_generate(uuid);
73     uuid_unparse(uuid, str); // Convert the UUID to a string
74     std::string burstUuid(str);
75     g_currentBurstUuid = burstUuid;
76     return burstUuid;
77 }
78 
HStreamCapture(sptr<OHOS::IBufferProducer> producer,int32_t format,int32_t width,int32_t height)79 HStreamCapture::HStreamCapture(sptr<OHOS::IBufferProducer> producer, int32_t format, int32_t width, int32_t height)
80     : HStreamCommon(StreamType::CAPTURE, producer, format, width, height)
81 {
82     MEDIA_INFO_LOG(
83         "HStreamCapture::HStreamCapture construct, format:%{public}d size:%{public}dx%{public}d streamId:%{public}d",
84         format, width, height, GetFwkStreamId());
85     thumbnailSwitch_ = 0;
86     rawDeliverySwitch_ = 0;
87     modeName_ = 0;
88     deferredPhotoSwitch_ = 0;
89     deferredVideoSwitch_ = 0;
90     burstNum_ = 0;
91     movingPhotoSwitch_ = 0;
92 }
93 
HStreamCapture(int32_t format,int32_t width,int32_t height)94 HStreamCapture::HStreamCapture(int32_t format, int32_t width, int32_t height)
95     : HStreamCommon(StreamType::CAPTURE, format, width, height)
96 {
97     CAMERA_SYNC_TRACE;
98     MEDIA_INFO_LOG(
99         "HStreamCapture::HStreamCapture new E, format:%{public}d size:%{public}dx%{public}d streamId:%{public}d",
100         format, width, height, GetFwkStreamId());
101     thumbnailSwitch_ = 0;
102     rawDeliverySwitch_ = 0;
103     modeName_ = 0;
104     deferredPhotoSwitch_ = 0;
105     deferredVideoSwitch_ = 0;
106     burstNum_ = 0;
107     movingPhotoSwitch_ = 0;
108     isYuvCapture_ = format == OHOS_CAMERA_FORMAT_YCRCB_420_SP;
109     CreateCaptureSurface();
110 }
111 
CreateCaptureSurface()112 void HStreamCapture::CreateCaptureSurface()
113 {
114     CAMERA_SYNC_TRACE;
115     MEDIA_INFO_LOG("CreateCaptureSurface E");
116     if (surfaceId_ == "") {
117         surface_ = Surface::CreateSurfaceAsConsumer("photoOutput");
118         MEDIA_DEBUG_LOG("create photoOutput surface");
119     } else {
120         surface_ = OHOS::Media::ImageReceiver::getSurfaceById(surfaceId_);
121         MEDIA_DEBUG_LOG("get photoOutput surface by surfaceId:%{public}s", surfaceId_.c_str());
122     }
123     CHECK_RETURN_ELOG(surface_ == nullptr, "surface is null");
124     // expand yuv auxiliary surfaces
125     CHECK_EXECUTE(isYuvCapture_, CreateAuxiliarySurfaces());
126 }
127 
CreateAuxiliarySurfaces()128 void HStreamCapture::CreateAuxiliarySurfaces()
129 {
130     CAMERA_SYNC_TRACE;
131     MEDIA_INFO_LOG("CreateAuxiliarySurfaces E");
132     CHECK_RETURN_ELOG(pictureAssembler_ != nullptr, "pictureAssembler has been set");
133     pictureAssembler_ = new (std::nothrow) PictureAssembler(this);
134     CHECK_RETURN_ELOG(pictureAssembler_ == nullptr, "create pictureAssembler faild");
135 
136     std::string retStr = "";
137     int32_t ret = 0;
138     if (gainmapSurface_ == nullptr) {
139         std::string bufferName = "gainmapImage";
140         gainmapSurface_ = Surface::CreateSurfaceAsConsumer(bufferName);
141         MEDIA_INFO_LOG("CreateAuxiliarySurfaces 1 surfaceId: %{public}" PRIu64, gainmapSurface_->GetUniqueId());
142         ret = SetBufferProducerInfo(bufferName, gainmapSurface_->GetProducer());
143         retStr += (ret != CAMERA_OK ? bufferName + "," : retStr);
144     }
145     if (deepSurface_ == nullptr) {
146         std::string bufferName = "deepImage";
147         deepSurface_ = Surface::CreateSurfaceAsConsumer(bufferName);
148         MEDIA_INFO_LOG("CreateAuxiliarySurfaces 2 surfaceId: %{public}" PRIu64, deepSurface_->GetUniqueId());
149         ret = SetBufferProducerInfo(bufferName, deepSurface_->GetProducer());
150         retStr += (ret != CAMERA_OK ? bufferName + "," : retStr);
151     }
152     if (exifSurface_ == nullptr) {
153         std::string bufferName = "exifImage";
154         exifSurface_ = Surface::CreateSurfaceAsConsumer(bufferName);
155         MEDIA_INFO_LOG("CreateAuxiliarySurfaces 3 surfaceId: %{public}" PRIu64, exifSurface_->GetUniqueId());
156         ret = SetBufferProducerInfo(bufferName, exifSurface_->GetProducer());
157         retStr += (ret != CAMERA_OK ? bufferName + "," : retStr);
158     }
159     if (debugSurface_ == nullptr) {
160         std::string bufferName = "debugImage";
161         debugSurface_ = Surface::CreateSurfaceAsConsumer(bufferName);
162         MEDIA_INFO_LOG("CreateAuxiliarySurfaces 4 surfaceId: %{public}" PRIu64, debugSurface_->GetUniqueId());
163         ret = SetBufferProducerInfo(bufferName, debugSurface_->GetProducer());
164         retStr += (ret != CAMERA_OK ? bufferName + "," : retStr);
165     }
166     MEDIA_INFO_LOG("CreateAuxiliarySurfaces X, res:%{public}s", retStr.c_str());
167 }
168 
~HStreamCapture()169 HStreamCapture::~HStreamCapture()
170 {
171     if (photoTask_ != nullptr) {
172         photoTask_->CancelAllTasks();
173         photoTask_ = nullptr;
174     }
175     if (photoSubTask_ != nullptr) {
176         photoSubTask_->CancelAllTasks();
177         photoSubTask_ = nullptr;
178     }
179     if (thumbnailTask_ != nullptr) {
180         thumbnailTask_->CancelAllTasks();
181         thumbnailTask_ = nullptr;
182     }
183     photoAssetProxy_.Release();
184     rotationMap_.Clear();
185     MEDIA_INFO_LOG(
186         "HStreamCapture::~HStreamCapture deconstruct, format:%{public}d size:%{public}dx%{public}d streamId:%{public}d",
187         format_, width_, height_, GetFwkStreamId());
188 }
189 
LinkInput(wptr<HDI::Camera::V1_0::IStreamOperator> streamOperator,std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility)190 int32_t HStreamCapture::LinkInput(wptr<HDI::Camera::V1_0::IStreamOperator> streamOperator,
191     std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility)
192 {
193     MEDIA_INFO_LOG("HStreamCapture::LinkInput streamId:%{public}d", GetFwkStreamId());
194     return HStreamCommon::LinkInput(streamOperator, cameraAbility);
195 }
196 
FillingPictureExtendStreamInfos(StreamInfo_V1_1 & streamInfo,int32_t format)197 void HStreamCapture::FillingPictureExtendStreamInfos(StreamInfo_V1_1 &streamInfo, int32_t format)
198 {
199     HDI::Camera::V1_1::ExtendedStreamInfo gainmapExtendedStreamInfo = {
200         .type = static_cast<HDI::Camera::V1_1::ExtendedStreamInfoType>(HDI::Camera::V1_3::EXTENDED_STREAM_INFO_GAINMAP),
201         .width = width_,
202         .height = height_,
203         .format = format, // HDR:NV21 P3:NV21
204         .dataspace = dataSpace_, // HDR:BT2020_HLG_FULL P3:P3_FULL
205         .bufferQueue = gainmapBufferQueue_,
206     };
207     HDI::Camera::V1_1::ExtendedStreamInfo deepExtendedStreamInfo = {
208         .type = static_cast<HDI::Camera::V1_1::ExtendedStreamInfoType>(HDI::Camera::V1_3::EXTENDED_STREAM_INFO_DEPTH),
209         .width = width_,
210         .height = height_,
211         .format = format,
212         .bufferQueue = deepBufferQueue_,
213     };
214     HDI::Camera::V1_1::ExtendedStreamInfo exifExtendedStreamInfo = {
215         .type = static_cast<HDI::Camera::V1_1::ExtendedStreamInfoType>(HDI::Camera::V1_3::EXTENDED_STREAM_INFO_EXIF),
216         .width = width_,
217         .height = height_,
218         .format = format,
219         .bufferQueue = exifBufferQueue_,
220     };
221     HDI::Camera::V1_1::ExtendedStreamInfo debugExtendedStreamInfo = {
222         .type =
223             static_cast<HDI::Camera::V1_1::ExtendedStreamInfoType>(HDI::Camera::V1_3::EXTENDED_STREAM_INFO_MAKER_INFO),
224         .width = width_,
225         .height = height_,
226         .format = format,
227         .bufferQueue = debugBufferQueue_,
228     };
229     std::vector<HDI::Camera::V1_1::ExtendedStreamInfo> extendedStreams = { gainmapExtendedStreamInfo,
230         deepExtendedStreamInfo, exifExtendedStreamInfo, debugExtendedStreamInfo };
231     streamInfo.extendedStreamInfos.insert(streamInfo.extendedStreamInfos.end(),
232         extendedStreams.begin(), extendedStreams.end());
233 }
234 
FillingRawAndThumbnailStreamInfo(StreamInfo_V1_1 & streamInfo)235 void HStreamCapture::FillingRawAndThumbnailStreamInfo(StreamInfo_V1_1 &streamInfo)
236 {
237     if (rawDeliverySwitch_ && format_ != OHOS_CAMERA_FORMAT_DNG_XDRAW) {
238         MEDIA_INFO_LOG("HStreamCapture::SetStreamInfo Set DNG info, streamId:%{public}d", GetFwkStreamId());
239         HDI::Camera::V1_1::ExtendedStreamInfo extendedStreamInfo = {
240             .type = static_cast<HDI::Camera::V1_1::ExtendedStreamInfoType>(HDI::Camera::V1_3::EXTENDED_STREAM_INFO_RAW),
241             .width = width_,
242             .height = height_,
243             .format = streamInfo.v1_0.format_,
244             .dataspace = 0,
245             .bufferQueue = rawBufferQueue_,
246         };
247         streamInfo.extendedStreamInfos.push_back(extendedStreamInfo);
248     }
249     if (thumbnailSwitch_) {
250         MEDIA_DEBUG_LOG("HStreamCapture::SetStreamInfo Set thumbnail info, dataspace:%{public}d", dataSpace_);
251         int32_t pixelFormat = GRAPHIC_PIXEL_FMT_YCRCB_420_SP;
252         pixelFormat = dataSpace_ == CM_BT2020_HLG_FULL ? GRAPHIC_PIXEL_FMT_YCRCB_P010 : pixelFormat;
253         HDI::Camera::V1_1::ExtendedStreamInfo extendedStreamInfo = {
254             .type = HDI::Camera::V1_1::EXTENDED_STREAM_INFO_QUICK_THUMBNAIL,
255             .width = 0,
256             .height = 0,
257             .format = pixelFormat, // HDR: YCRCB_P010 P3: nv21
258             .dataspace = dataSpace_, // HDR: BT2020_HLG_FULL P3: P3
259             .bufferQueue = thumbnailBufferQueue_,
260         };
261         streamInfo.extendedStreamInfos.push_back(extendedStreamInfo);
262     }
263 }
264 
SetDataSpaceForCapture(StreamInfo_V1_1 & streamInfo)265 void HStreamCapture::SetDataSpaceForCapture(StreamInfo_V1_1 &streamInfo)
266 {
267     // LCOV_EXCL_START
268     switch (streamInfo.v1_0.dataspace_) {
269         case CM_ColorSpaceType_V2_1::CM_BT2020_HLG_FULL:
270             //  HDR Video Session need P3 for captureStream
271             streamInfo.v1_0.dataspace_ =  CM_ColorSpaceType_V2_1::CM_P3_FULL;
272             break;
273         case CM_ColorSpaceType_V2_1::CM_BT2020_HLG_LIMIT:
274             // HDR Video Session need P3 for captureStream
275             streamInfo.v1_0.dataspace_ =  CM_ColorSpaceType_V2_1::CM_P3_FULL;
276             break;
277         case CM_ColorSpaceType_V2_1::CM_BT709_LIMIT:
278             // SDR Video Session need SRGB for captureStream
279             streamInfo.v1_0.dataspace_ = CM_ColorSpaceType_V2_1::CM_SRGB_FULL;
280             break;
281         default:
282             streamInfo.v1_0.dataspace_ = CM_ColorSpaceType_V2_1::CM_SRGB_FULL;
283             break;
284     }
285     // LCOV_EXCL_STOP
286     MEDIA_DEBUG_LOG("HStreamCapture::SetDataSpaceForCapture current HDI dataSpace: %{public}d",
287         streamInfo.v1_0.dataspace_);
288 }
289 
SetStreamInfo(StreamInfo_V1_1 & streamInfo)290 void HStreamCapture::SetStreamInfo(StreamInfo_V1_1 &streamInfo)
291 {
292     HStreamCommon::SetStreamInfo(streamInfo);
293     MEDIA_INFO_LOG("HStreamCapture::SetStreamInfo streamId:%{public}d format:%{public}d", GetFwkStreamId(), format_);
294     streamInfo.v1_0.intent_ = STILL_CAPTURE;
295 
296     // 录像抓拍场景下添加拍照流的色域信息转换
297     SetDataSpaceForCapture(streamInfo);
298 
299     if (format_ == OHOS_CAMERA_FORMAT_HEIC) {
300         streamInfo.v1_0.encodeType_ =
301             static_cast<HDI::Camera::V1_0::EncodeType>(HDI::Camera::V1_3::ENCODE_TYPE_HEIC);
302         streamInfo.v1_0.format_ = GRAPHIC_PIXEL_FMT_BLOB;
303     } else if (format_ == OHOS_CAMERA_FORMAT_YCRCB_420_SP) { // NV21
304         streamInfo.v1_0.encodeType_ = ENCODE_TYPE_NULL;
305         streamInfo.v1_0.format_ = GRAPHIC_PIXEL_FMT_YCRCB_420_SP; // NV21
306         if (GetMode() != static_cast<int32_t>(HDI::Camera::V1_3::OperationMode::TIMELAPSE_PHOTO)) {
307             FillingPictureExtendStreamInfos(streamInfo, GRAPHIC_PIXEL_FMT_YCRCB_420_SP);
308         }
309     } else if (format_ == OHOS_CAMERA_FORMAT_DNG_XDRAW) {
310         streamInfo.v1_0.encodeType_ =
311             static_cast<HDI::Camera::V1_0::EncodeType>(HDI::Camera::V1_4::ENCODE_TYPE_DNG_XDRAW);
312     } else {
313         streamInfo.v1_0.encodeType_ = ENCODE_TYPE_JPEG;
314     }
315     FillingRawAndThumbnailStreamInfo(streamInfo);
316 }
317 
SetThumbnail(bool isEnabled)318 int32_t HStreamCapture::SetThumbnail(bool isEnabled)
319 {
320     if (isEnabled) {
321         thumbnailSwitch_ = 1;
322         thumbnailSurface_ = nullptr;
323         thumbnailSurface_ = Surface::CreateSurfaceAsConsumer("quickThumbnail");
324         CHECK_RETURN_RET_ELOG(thumbnailSurface_ == nullptr, CAMERA_OK, "thumbnail surface create faild");
325         thumbnailBufferQueue_ = new BufferProducerSequenceable(thumbnailSurface_->GetProducer());
326     } else {
327         thumbnailSwitch_ = 0;
328         thumbnailSurface_ = nullptr;
329         thumbnailBufferQueue_ = nullptr;
330     }
331     return CAMERA_OK;
332 }
333 
EnableRawDelivery(bool enabled)334 int32_t HStreamCapture::EnableRawDelivery(bool enabled)
335 {
336     MEDIA_INFO_LOG("EnableRawDelivery E,enabled:%{public}d", enabled);
337     int32_t ret = CAMERA_OK;
338     if (enabled) {
339         rawDeliverySwitch_ = 1;
340         rawSurface_ = nullptr;
341         std::string bufferName = "rawImage";
342         rawSurface_ = Surface::CreateSurfaceAsConsumer(bufferName);
343         CHECK_RETURN_RET_ELOG(rawSurface_ == nullptr, CAMERA_OK, "raw surface create faild");
344         ret = SetBufferProducerInfo(bufferName, rawSurface_->GetProducer());
345         SetRawCallback();
346     } else {
347         rawDeliverySwitch_ = 0;
348         rawSurface_ = nullptr;
349     }
350     return ret;
351 }
352 
353 // LCOV_EXCL_START
EnableMovingPhoto(bool enabled)354 int32_t HStreamCapture::EnableMovingPhoto(bool enabled)
355 {
356     if (enabled) {
357         movingPhotoSwitch_ = 1;
358     } else {
359         movingPhotoSwitch_ = 0;
360     }
361     return CAMERA_OK;
362 }
363 // LCOV_EXCL_STOP
364 
SetBufferProducerInfo(const std::string & bufName,const sptr<OHOS::IBufferProducer> & producer)365 int32_t HStreamCapture::SetBufferProducerInfo(const std::string& bufName, const sptr<OHOS::IBufferProducer> &producer)
366 {
367     std::string resStr = "";
368     if (bufName == "rawImage") {
369         if (producer != nullptr) {
370             rawBufferQueue_ = new BufferProducerSequenceable(producer);
371         } else {
372             rawBufferQueue_ = nullptr;
373             resStr += bufName + ",";
374         }
375     }
376     if (bufName == "gainmapImage") {
377         if (producer != nullptr) {
378             gainmapBufferQueue_ = new BufferProducerSequenceable(producer);
379         } else {
380             gainmapBufferQueue_ = nullptr;
381             resStr += bufName + ",";
382         }
383     }
384     if (bufName == "deepImage") {
385         if (producer != nullptr) {
386             deepBufferQueue_ = new BufferProducerSequenceable(producer);
387         } else {
388             deepBufferQueue_ = nullptr;
389             resStr += bufName + ",";
390         }
391     }
392     if (bufName == "exifImage") {
393         if (producer != nullptr) {
394             exifBufferQueue_ = new BufferProducerSequenceable(producer);
395         } else {
396             exifBufferQueue_ = nullptr;
397             resStr += bufName + ",";
398         }
399     }
400     if (bufName == "debugImage") {
401         if (producer != nullptr) {
402             debugBufferQueue_ = new BufferProducerSequenceable(producer);
403         } else {
404             debugBufferQueue_ = nullptr;
405             resStr += bufName + ",";
406         }
407     }
408     MEDIA_INFO_LOG("HStreamCapture::SetBufferProducerInfo bufferQueue whether is nullptr: %{public}s", resStr.c_str());
409     return CAMERA_OK;
410 }
411 
DeferImageDeliveryFor(int32_t type)412 int32_t HStreamCapture::DeferImageDeliveryFor(int32_t type)
413 {
414     MEDIA_INFO_LOG("HStreamCapture::DeferImageDeliveryFor type: %{public}d", type);
415     if (type == OHOS::HDI::Camera::V1_2::STILL_IMAGE) {
416         MEDIA_INFO_LOG("HStreamCapture STILL_IMAGE");
417         deferredPhotoSwitch_ = 1;
418     } else if (type == OHOS::HDI::Camera::V1_2::MOVING_IMAGE) {
419         MEDIA_INFO_LOG("HStreamCapture MOVING_IMAGE");
420         deferredVideoSwitch_ = 1;
421     } else {
422         MEDIA_INFO_LOG("HStreamCapture NONE");
423         deferredPhotoSwitch_ = 0;
424         deferredVideoSwitch_ = 0;
425     }
426     return CAMERA_OK;
427 }
428 
PrepareBurst(int32_t captureId)429 int32_t HStreamCapture::PrepareBurst(int32_t captureId)
430 {
431     MEDIA_INFO_LOG("HStreamCapture::PrepareBurst captureId:%{public}d", captureId);
432     isBursting_ = true;
433     std::lock_guard<std::mutex> lock(burstLock_);
434     curBurstKey_ = GenerateBurstUuid();
435     burstkeyMap_.emplace(captureId, curBurstKey_);
436     std::vector<std::string> imageList = {};
437     burstImagesMap_.emplace(captureId, imageList);
438     burstNumMap_.emplace(captureId, 0);
439     burstNum_ = 0;
440     return CAMERA_OK;
441 }
442 
ResetBurst()443 void HStreamCapture::ResetBurst()
444 {
445     MEDIA_INFO_LOG("HStreamCapture::ResetBurst");
446     curBurstKey_ = BURST_UUID_UNSET;
447     isBursting_ = false;
448 }
449 
ResetBurstKey(int32_t captureId)450 void HStreamCapture::ResetBurstKey(int32_t captureId)
451 {
452     if (burstkeyMap_.erase(captureId) > 0 &&
453         burstImagesMap_.erase(captureId) > 0 &&
454         burstNumMap_.erase(captureId) > 0) {
455         MEDIA_INFO_LOG("HStreamCapture::ResetBurstKey captureId:%{public}d", captureId);
456     } else {
457         MEDIA_DEBUG_LOG("HStreamCapture::ResetBurstKey captureId not found");
458     }
459 }
460 
GetBurstKey(int32_t captureId) const461 std::string HStreamCapture::GetBurstKey(int32_t captureId) const
462 {
463     MEDIA_DEBUG_LOG("HStreamCapture::GetBurstKey captureId:%{public}d", captureId);
464     std::string burstKey = BURST_UUID_UNSET;
465     std::lock_guard<std::mutex> lock(burstLock_);
466     auto iter = burstkeyMap_.find(captureId);
467     if (iter != burstkeyMap_.end()) {
468         burstKey = iter->second;
469         MEDIA_DEBUG_LOG("HStreamCapture::GetBurstKey %{public}s", burstKey.c_str());
470     } else {
471         MEDIA_DEBUG_LOG("HStreamCapture::GetBurstKey not found");
472     }
473     return burstKey;
474 }
475 
IsBurstCapture(int32_t captureId) const476 bool HStreamCapture::IsBurstCapture(int32_t captureId) const
477 {
478     MEDIA_DEBUG_LOG("HStreamCapture::captureId:%{public}d", captureId);
479     auto iter = burstkeyMap_.find(captureId);
480     return iter != burstkeyMap_.end();
481 }
482 
IsBurstCover(int32_t captureId) const483 bool HStreamCapture::IsBurstCover(int32_t captureId) const
484 {
485     MEDIA_DEBUG_LOG("HStreamCapture::IsBurstCover for captureId: %d", captureId);
486     std::lock_guard<std::mutex> lock(burstLock_);
487     auto iter = burstImagesMap_.find(captureId);
488     return (iter != burstImagesMap_.end()) ? (iter->second.size() == 1) : false;
489 }
490 
GetCurBurstSeq(int32_t captureId) const491 int32_t HStreamCapture::GetCurBurstSeq(int32_t captureId) const
492 {
493     MEDIA_DEBUG_LOG("HStreamCapture::GetCurBurstSeq for captureId: %d", captureId);
494     std::lock_guard<std::mutex> lock(burstLock_);
495     auto iter = burstImagesMap_.find(captureId);
496     CHECK_RETURN_RET(iter != burstImagesMap_.end(), iter->second.size());
497     return -1;
498 }
499 
SetBurstImages(int32_t captureId,std::string imageId)500 void HStreamCapture::SetBurstImages(int32_t captureId, std::string imageId)
501 {
502     MEDIA_DEBUG_LOG("HStreamCapture::SetBurstImages captureId:%{public}d imageId:%{public}s",
503         captureId, imageId.c_str());
504     std::lock_guard<std::mutex> lock(burstLock_);
505     auto iter = burstImagesMap_.find(captureId);
506     if (iter != burstImagesMap_.end()) {
507         iter->second.emplace_back(imageId);
508         MEDIA_DEBUG_LOG("HStreamCapture::SetBurstImages success");
509     } else {
510         MEDIA_ERR_LOG("HStreamCapture::SetBurstImages error");
511     }
512 }
513 
CheckResetBurstKey(int32_t captureId)514 void HStreamCapture::CheckResetBurstKey(int32_t captureId)
515 {
516     MEDIA_DEBUG_LOG("HStreamCapture::CheckResetBurstKey captureId:%{public}d", captureId);
517     std::lock_guard<std::mutex> lock(burstLock_);
518     auto numIter = burstNumMap_.find(captureId);
519     auto imageIter = burstImagesMap_.find(captureId);
520     if (numIter != burstNumMap_.end() && imageIter != burstImagesMap_.end()) {
521         // LCOV_EXCL_START
522         int32_t burstSum = numIter->second;
523         size_t curBurstSum = imageIter->second.size();
524         MEDIA_DEBUG_LOG("CheckResetBurstKey: burstSum=%d, curBurstSum=%zu", burstSum, curBurstSum);
525         CHECK_EXECUTE(static_cast<size_t>(burstSum) == curBurstSum, ResetBurstKey(captureId));
526         // LCOV_EXCL_STOP
527     } else {
528         MEDIA_DEBUG_LOG("CheckResetBurstKey: captureId %d not found in one or both maps", captureId);
529     }
530 }
531 
532 
CheckBurstCapture(const std::shared_ptr<OHOS::Camera::CameraMetadata> & captureSettings,const int32_t & preparedCaptureId)533 int32_t HStreamCapture::CheckBurstCapture(const std::shared_ptr<OHOS::Camera::CameraMetadata>& captureSettings,
534                                           const int32_t &preparedCaptureId)
535 {
536     MEDIA_INFO_LOG("CheckBurstCapture start!");
537     camera_metadata_item_t item;
538     CHECK_RETURN_RET_ELOG(captureSettings == nullptr, CAMERA_INVALID_STATE, "captureSettings is nullptr");
539     int32_t result = OHOS::Camera::FindCameraMetadataItem(captureSettings->get(), OHOS_CONTROL_BURST_CAPTURE, &item);
540     if (result == CAM_META_SUCCESS && item.count > 0) {
541     // LCOV_EXCL_START
542         CameraBurstCaptureEnum burstState = static_cast<CameraBurstCaptureEnum>(item.data.u8[0]);
543         MEDIA_INFO_LOG("CheckBurstCapture get burstState:%{public}d", item.data.u8[0]);
544         if (burstState) {
545             std::string burstUuid = GetBurstKey(preparedCaptureId);
546             CHECK_RETURN_RET_ELOG(burstUuid != BURST_UUID_UNSET || isBursting_, CAMERA_INVALID_STATE,
547                 "CheckBurstCapture faild!");
548             PrepareBurst(preparedCaptureId);
549             MEDIA_INFO_LOG("CheckBurstCapture ready!");
550         }
551     }
552     // LCOV_EXCL_STOP
553     return CAM_META_SUCCESS;
554 }
555 
Insert(const int32_t & key,const std::shared_ptr<PhotoAssetIntf> & value)556 void ConcurrentMap::Insert(const int32_t& key, const std::shared_ptr<PhotoAssetIntf>& value)
557 {
558     std::lock_guard<std::mutex> lock(map_mutex_);
559     map_[key] = value;
560     step_[key] = 1;
561     if (!cv_.count(key)) {
562         cv_[key] = std::make_shared<std::condition_variable>();
563     }
564     if (!mutexes_.count(key)) {
565         mutexes_[key] = std::make_shared<std::mutex>();
566     }
567     cv_[key]->notify_all();
568 }
569 
Get(const int32_t & key)570 std::shared_ptr<PhotoAssetIntf> ConcurrentMap::Get(const int32_t& key)
571 {
572     std::lock_guard<std::mutex> lock(map_mutex_);
573     auto it = map_.find(key);
574     return it != map_.end() ? it->second : nullptr;
575 }
576 
WaitForUnlock(const int32_t & key,const int32_t & step,const int32_t & mode,const std::chrono::seconds & timeout)577 bool ConcurrentMap::WaitForUnlock(const int32_t& key, const int32_t& step, const int32_t& mode,
578     const std::chrono::seconds& timeout)
579 {
580     std::shared_ptr<std::mutex> keyMutexPtr;
581     std::shared_ptr<std::condition_variable> cvPtr;
582     {
583         std::lock_guard<std::mutex> lock(map_mutex_);
584         if (!cv_.count(key)) {
585             cv_[key] = std::make_shared<std::condition_variable>();
586         }
587         if (!mutexes_.count(key)) {
588             mutexes_[key] = std::make_shared<std::mutex>();
589         }
590         keyMutexPtr = mutexes_[key];
591         cvPtr = cv_[key];
592     }
593 
594     std::unique_lock<std::mutex> lock(*keyMutexPtr);
595     return cvPtr->wait_for(lock, timeout, [&] {
596         return ReadyToUnlock(key, step, mode);
597     });
598 }
599 
ReadyToUnlock(const int32_t & key,const int32_t & step,const int32_t & mode)600 bool ConcurrentMap::ReadyToUnlock(const int32_t& key, const int32_t& step, const int32_t& mode)
601 {
602     std::lock_guard<std::mutex> lock(map_mutex_);
603     if (mode == static_cast<int32_t>(HDI::Camera::V1_3::OperationMode::CAPTURE) ||
604       mode == static_cast<int32_t>(HDI::Camera::V1_3::OperationMode::QUICK_SHOT_PHOTO) ||
605       mode == static_cast<int32_t>(HDI::Camera::V1_3::OperationMode::PORTRAIT) ||
606       mode == static_cast<int32_t>(HDI::Camera::V1_3::OperationMode::CAPTURE_MACRO)) {
607         return step_.count(key) > 0 && step_[key] == step;
608     } else {
609         return map_.count(key) > 0;
610     }
611 }
612 
613 // LCOV_EXCL_START
IncreaseCaptureStep(const int32_t & key)614 void ConcurrentMap::IncreaseCaptureStep(const int32_t& key)
615 {
616     std::lock_guard<std::mutex> lock(map_mutex_);
617     if (key < 0 || key > INT32_MAX) {
618         return;
619     }
620     if (step_.count(key) == 0) {
621         step_[key] = 1;
622     } else {
623         step_[key] = step_[key] + 1;
624     }
625     CHECK_RETURN(!cv_.count(key));
626     cv_[key]->notify_all();
627 }
628 // LCOV_EXCL_STOP
629 
Erase(const int32_t & key)630 void ConcurrentMap::Erase(const int32_t& key)
631 {
632     std::lock_guard<std::mutex> lock(map_mutex_);
633     mutexes_.erase(key);
634     map_.erase(key);
635     cv_.erase(key);
636     step_.erase(key);
637 }
638 
Release()639 void ConcurrentMap::Release()
640 {
641     std::lock_guard<std::mutex> lock(map_mutex_);
642     map_.clear();
643     mutexes_.clear();
644     cv_.clear();
645     step_.clear();
646 }
647 
CreateMediaLibraryPhotoAssetProxy(int32_t captureId)648 int32_t HStreamCapture::CreateMediaLibraryPhotoAssetProxy(int32_t captureId)
649 {
650     CAMERA_SYNC_TRACE;
651     MEDIA_DEBUG_LOG("CreateMediaLibraryPhotoAssetProxy E captureId:%{public}d", captureId);
652     constexpr int32_t imageShotType = 0;
653     constexpr int32_t movingPhotoShotType = 2;
654     constexpr int32_t burstShotType = 3;
655     int32_t cameraShotType = imageShotType;
656     if (movingPhotoSwitch_) {
657         cameraShotType = movingPhotoShotType;
658     } else if (isBursting_) {
659         cameraShotType = burstShotType;
660     }
661     auto photoAssetProxy = PhotoAssetProxy::GetPhotoAssetProxy(
662         cameraShotType, IPCSkeleton::GetCallingUid(), IPCSkeleton::GetCallingTokenID());
663     CHECK_RETURN_RET_ELOG(photoAssetProxy == nullptr, CAMERA_ALLOC_ERROR,
664         "HStreamCapture::CreateMediaLibraryPhotoAssetProxy get photoAssetProxy fail");
665     photoAssetProxy_.Insert(captureId, photoAssetProxy);
666     MEDIA_DEBUG_LOG("CreateMediaLibraryPhotoAssetProxy X captureId:%{public}d", captureId);
667     return CAMERA_OK;
668 }
669 
GetPhotoAssetInstance(int32_t captureId)670 std::shared_ptr<PhotoAssetIntf> HStreamCapture::GetPhotoAssetInstance(int32_t captureId)
671 {
672     CAMERA_SYNC_TRACE;
673     const int32_t getPhotoAssetStep = 2;
674     if (!photoAssetProxy_.WaitForUnlock(captureId, getPhotoAssetStep, GetMode(), std::chrono::seconds(1))) {
675         MEDIA_ERR_LOG("GetPhotoAsset faild wait timeout, captureId:%{public}d", captureId);
676         return nullptr;
677     }
678     std::shared_ptr<PhotoAssetIntf> proxy = photoAssetProxy_.Get(captureId);
679     photoAssetProxy_.Erase(captureId);
680     return proxy;
681 }
682 
GetAddPhotoProxyEnabled()683 bool HStreamCapture::GetAddPhotoProxyEnabled()
684 {
685     return thumbnailSwitch_;
686 }
687 
AcquireBufferToPrepareProxy(int32_t captureId)688 int32_t HStreamCapture::AcquireBufferToPrepareProxy(int32_t captureId)
689 {
690     MEDIA_DEBUG_LOG("HStreamCapture::AcquireBufferToPrepareProxy start");
691     CameraReportDfxUtils::GetInstance()->SetFirstBufferEndInfo(captureId);
692     CameraReportDfxUtils::GetInstance()->SetPrepareProxyStartInfo(captureId);
693     MEDIA_DEBUG_LOG("HStreamCapture::AcquireBufferToPrepareProxy end");
694     return CAMERA_OK;
695 }
696 
Capture(const std::shared_ptr<OHOS::Camera::CameraMetadata> & captureSettings)697 int32_t HStreamCapture::Capture(const std::shared_ptr<OHOS::Camera::CameraMetadata>& captureSettings)
698 {
699     CAMERA_SYNC_TRACE;
700     MEDIA_INFO_LOG("HStreamCapture::Capture Entry, streamId:%{public}d", GetFwkStreamId());
701     auto streamOperator = GetStreamOperator();
702     CHECK_RETURN_RET(streamOperator == nullptr, CAMERA_INVALID_STATE);
703     // LCOV_EXCL_START
704     CHECK_RETURN_RET_ELOG(isCaptureReady_ == false, CAMERA_OPERATION_NOT_ALLOWED,
705         "HStreamCapture::Capture failed due to capture not ready");
706     auto preparedCaptureId = GetPreparedCaptureId();
707     CHECK_RETURN_RET_ELOG(preparedCaptureId != CAPTURE_ID_UNSET, CAMERA_INVALID_STATE,
708         "HStreamCapture::Capture, Already started with captureID: %{public}d", preparedCaptureId);
709     int32_t ret = PrepareCaptureId();
710     preparedCaptureId = GetPreparedCaptureId();
711     CHECK_RETURN_RET_ELOG(ret != CAMERA_OK || preparedCaptureId == CAPTURE_ID_UNSET, ret,
712         "HStreamCapture::Capture Failed to allocate a captureId");
713     ret = CheckBurstCapture(captureSettings, preparedCaptureId);
714     CHECK_RETURN_RET_ELOG(ret != CAMERA_OK, ret, "HStreamCapture::Capture Failed with burst state error");
715 
716     CaptureDfxInfo captureDfxInfo;
717     captureDfxInfo.captureId = preparedCaptureId;
718     captureDfxInfo.isSystemApp = CheckSystemApp();
719     captureDfxInfo.bundleName = BmsAdapter::GetInstance()->GetBundleName(IPCSkeleton::GetCallingUid());
720     CameraReportDfxUtils::GetInstance()->SetFirstBufferStartInfo(captureDfxInfo);
721 
722     CaptureInfo captureInfoPhoto;
723     captureInfoPhoto.streamIds_ = { GetHdiStreamId() };
724     ProcessCaptureInfoPhoto(captureInfoPhoto, captureSettings, preparedCaptureId);
725     auto callingTokenId = IPCSkeleton::GetCallingTokenID();
726     const std::string permissionName = "ohos.permission.CAMERA";
727     AddCameraPermissionUsedRecord(callingTokenId, permissionName);
728 
729     // report capture performance dfx
730     std::shared_ptr<OHOS::Camera::CameraMetadata> captureMetadataSetting_ = nullptr;
731     OHOS::Camera::MetadataUtils::ConvertVecToMetadata(captureInfoPhoto.captureSetting_, captureMetadataSetting_);
732     if (captureMetadataSetting_ == nullptr) {
733         captureMetadataSetting_ = std::make_shared<OHOS::Camera::CameraMetadata>(0, 0);
734     }
735     DfxCaptureInfo captureInfo;
736     captureInfo.captureId = preparedCaptureId;
737     captureInfo.caller = CameraReportUtils::GetCallerInfo();
738     CameraReportUtils::GetInstance().SetCapturePerfStartInfo(captureInfo);
739     MEDIA_INFO_LOG("HStreamCapture::Capture Starting photo capture with capture ID: %{public}d", preparedCaptureId);
740     HStreamCommon::PrintCaptureDebugLog(captureMetadataSetting_);
741     CamRetCode rc = (CamRetCode)(streamOperator->Capture(preparedCaptureId, captureInfoPhoto, isBursting_));
742     if (rc != HDI::Camera::V1_0::NO_ERROR) {
743         ResetCaptureId();
744         captureIdForConfirmCapture_ = CAPTURE_ID_UNSET;
745         MEDIA_ERR_LOG("HStreamCapture::Capture failed with error Code: %{public}d", rc);
746         camera_metadata_item_t item;
747         uint8_t connectionType = 0;
748         if (captureSettings != nullptr) {
749             ret = OHOS::Camera::FindCameraMetadataItem(
750                 captureSettings->get(), OHOS_ABILITY_CAMERA_CONNECTION_TYPE, &item);
751             if (ret == CAM_META_SUCCESS && item.count > 0) {
752                 connectionType = item.data.u8[0];
753             }
754         }
755         CameraReportUtils::ReportCameraErrorForUsb(
756             "HStreamCapture::Capture", rc, true, std::to_string(connectionType), CameraReportUtils::GetCallerInfo());
757         ret = HdiToServiceError(rc);
758     }
759     camera_metadata_item_t item;
760     camera_position_enum_t cameraPosition = OHOS_CAMERA_POSITION_FRONT;
761     {
762         std::lock_guard<std::mutex> lock(cameraAbilityLock_);
763         CHECK_RETURN_RET_ELOG(cameraAbility_ == nullptr, CAMERA_INVALID_STATE,
764             "HStreamCapture::cameraAbility_ is null");
765         int32_t result = OHOS::Camera::FindCameraMetadataItem(cameraAbility_->get(), OHOS_ABILITY_CAMERA_POSITION,
766                                                               &item);
767         if (result == CAM_META_SUCCESS && item.count > 0) {
768             cameraPosition = static_cast<camera_position_enum_t>(item.data.u8[0]);
769         }
770     }
771 
772     bool isNightMode = (GetMode() == static_cast<int32_t>(HDI::Camera::V1_3::OperationMode::NIGHT));
773     CHECK_RETURN_RET(isNightMode && cameraPosition == OHOS_CAMERA_POSITION_BACK, ret);
774     ResetCaptureId();
775 
776     uint32_t major;
777     uint32_t minor;
778     streamOperator->GetVersion(major, minor);
779     MEDIA_INFO_LOG("streamOperator GetVersion major:%{public}d, minor:%{public}d", major, minor);
780     // intercept when streamOperatorCallback support onCaptureReady
781     if (GetVersionId(major, minor) >= HDI_VERSION_ID_1_2 && !isBursting_) {
782         MEDIA_INFO_LOG("HStreamCapture::Capture set capture not ready");
783         isCaptureReady_ = false;
784     }
785     if (!isBursting_) {
786         MEDIA_DEBUG_LOG("HStreamCapture::Capture CreateMediaLibraryPhotoAssetProxy E");
787         CHECK_PRINT_ELOG(CreateMediaLibraryPhotoAssetProxy(preparedCaptureId) != CAMERA_OK,
788             "HStreamCapture::Capture Failed with CreateMediaLibraryPhotoAssetProxy");
789         MEDIA_DEBUG_LOG("HStreamCapture::Capture CreateMediaLibraryPhotoAssetProxy X");
790     }
791     return ret;
792     // LCOV_EXCL_STOP
793 }
794 
ProcessCaptureInfoPhoto(CaptureInfo & captureInfoPhoto,const std::shared_ptr<OHOS::Camera::CameraMetadata> & captureSettings,int32_t captureId)795 void HStreamCapture::ProcessCaptureInfoPhoto(CaptureInfo& captureInfoPhoto,
796     const std::shared_ptr<OHOS::Camera::CameraMetadata>& captureSettings, int32_t captureId)
797 {
798     if (!OHOS::Camera::GetCameraMetadataItemCount(captureSettings->get())) {
799         std::lock_guard<std::mutex> lock(cameraAbilityLock_);
800         OHOS::Camera::MetadataUtils::ConvertMetadataToVec(cameraAbility_, captureInfoPhoto.captureSetting_);
801     } else {
802         OHOS::Camera::MetadataUtils::ConvertMetadataToVec(captureSettings, captureInfoPhoto.captureSetting_);
803     }
804     captureInfoPhoto.enableShutterCallback_ = true;
805     std::shared_ptr<OHOS::Camera::CameraMetadata> captureMetadataSetting_ = nullptr;
806     OHOS::Camera::MetadataUtils::ConvertVecToMetadata(captureInfoPhoto.captureSetting_, captureMetadataSetting_);
807     if (captureMetadataSetting_ != nullptr) {
808         // convert rotation with application set rotation
809         SetRotation(captureMetadataSetting_, captureId);
810 
811         // update settings
812         std::vector<uint8_t> finalSetting;
813         OHOS::Camera::MetadataUtils::ConvertMetadataToVec(captureMetadataSetting_, finalSetting);
814         captureInfoPhoto.captureSetting_ = finalSetting;
815     }
816     GetLocation(captureMetadataSetting_);
817 }
818 
SetRotation(const std::shared_ptr<OHOS::Camera::CameraMetadata> & captureMetadataSetting_,int32_t captureId)819 void HStreamCapture::SetRotation(const std::shared_ptr<OHOS::Camera::CameraMetadata> &captureMetadataSetting_,
820     int32_t captureId)
821 {
822     // set orientation for capture
823     // sensor orientation, counter-clockwise rotation
824     int32_t sensorOrientation = 0;
825     int result;
826     camera_metadata_item_t item;
827     camera_position_enum_t cameraPosition = OHOS_CAMERA_POSITION_BACK;
828     {
829         std::lock_guard<std::mutex> lock(cameraAbilityLock_);
830         CHECK_RETURN(cameraAbility_ == nullptr);
831         // LCOV_EXCL_START
832         result = OHOS::Camera::FindCameraMetadataItem(cameraAbility_->get(), OHOS_SENSOR_ORIENTATION, &item);
833         if (result == CAM_META_SUCCESS && item.count > 0) {
834             sensorOrientation = item.data.i32[0];
835         }
836         MEDIA_INFO_LOG("set rotation sensor orientation %{public}d", sensorOrientation);
837 
838         result = OHOS::Camera::FindCameraMetadataItem(cameraAbility_->get(), OHOS_ABILITY_CAMERA_POSITION, &item);
839         if (result == CAM_META_SUCCESS && item.count > 0) {
840             cameraPosition = static_cast<camera_position_enum_t>(item.data.u8[0]);
841         }
842         MEDIA_INFO_LOG("set rotation camera position %{public}d", cameraPosition);
843     }
844 
845     // rotation from application
846     int32_t rotationValue = 0;
847     int32_t rotation = 0;
848     result = OHOS::Camera::FindCameraMetadataItem(captureMetadataSetting_->get(), OHOS_JPEG_ORIENTATION, &item);
849     if (result == CAM_META_SUCCESS && item.count > 0) {
850         rotationValue = item.data.i32[0];
851     }
852     MEDIA_INFO_LOG("set rotation app rotationValue %{public}d", rotationValue); // 0 270 270+270=180
853     // real rotation
854     if (enableCameraPhotoRotation_) {
855         rotation = rotationValue;
856     } else {
857         rotation = sensorOrientation + rotationValue;
858         if (rotation >= CAPTURE_ROTATE_360) {
859             rotation = rotation - CAPTURE_ROTATE_360;
860         }
861     }
862     auto hStreamOperator = hStreamOperator_.promote();
863     if (hStreamOperator) {
864         hStreamOperator->SetSensorRotation(rotation, sensorOrientation, cameraPosition);
865     }
866     {
867         uint8_t connectType = 0;
868         std::lock_guard<std::mutex> lock(cameraAbilityLock_);
869         CHECK_RETURN(cameraAbility_ == nullptr);
870         int ret = OHOS::Camera::FindCameraMetadataItem(
871             cameraAbility_->get(), OHOS_ABILITY_CAMERA_CONNECTION_TYPE, &item);
872         if (ret == CAM_META_SUCCESS && item.count > 0) {
873             connectType = item.data.u8[0];
874         }
875         if (connectType == OHOS_CAMERA_CONNECTION_TYPE_REMOTE) {
876             rotation = rotationValue;
877         }
878         MEDIA_INFO_LOG("set rotation camera real rotation %{public}d", rotation);
879     }
880     UpdateJpegBasicInfo(captureMetadataSetting_, rotation);
881     CHECK_EXECUTE(hStreamOperator, hStreamOperator->UpdateOrientationBaseGravity(rotation, sensorOrientation,
882         cameraPosition, rotation));
883     bool status = false;
884     if (result == CAM_META_ITEM_NOT_FOUND) {
885         status = captureMetadataSetting_->addEntry(OHOS_JPEG_ORIENTATION, &rotation, 1);
886     } else if (result == CAM_META_SUCCESS) {
887         status = captureMetadataSetting_->updateEntry(OHOS_JPEG_ORIENTATION, &rotation, 1);
888     }
889     rotationMap_.EnsureInsert(captureId, rotation);
890     result = OHOS::Camera::FindCameraMetadataItem(captureMetadataSetting_->get(), OHOS_JPEG_ORIENTATION, &item);
891     CHECK_PRINT_ELOG(result != CAM_META_SUCCESS, "set rotation Failed to find OHOS_JPEG_ORIENTATION tag");
892     CHECK_PRINT_ELOG(!status, "set rotation Failed to set Rotation");
893     // LCOV_EXCL_STOP
894 }
895 
UpdateJpegBasicInfo(const std::shared_ptr<OHOS::Camera::CameraMetadata> & captureMetadataSetting,int32_t & rotation)896 void HStreamCapture::UpdateJpegBasicInfo(const std::shared_ptr<OHOS::Camera::CameraMetadata> &captureMetadataSetting,
897     int32_t& rotation)
898 {
899 #ifdef HOOK_CAMERA_OPERATOR
900     bool isMirror = false;
901     if (!CameraRotatePlugin::GetInstance()->HookCaptureStreamStart(GetBasicInfo(), rotation, isMirror)) {
902         MEDIA_ERR_LOG("HStreamRepeat::HookCaptureStreamStart is failed %{public}d", isMirror);
903         return;
904     }
905     bool status = false;
906     camera_metadata_item_t item;
907     int result = OHOS::Camera::FindCameraMetadataItem(captureMetadataSetting->get(),
908         OHOS_CONTROL_CAPTURE_MIRROR, &item);
909     if (result == CAM_META_ITEM_NOT_FOUND) {
910         status = captureMetadataSetting->addEntry(OHOS_CONTROL_CAPTURE_MIRROR, &isMirror, 1);
911     } else if (result == CAM_META_SUCCESS) {
912         status = captureMetadataSetting->updateEntry(OHOS_CONTROL_CAPTURE_MIRROR, &isMirror, 1);
913     }
914     CHECK_PRINT_ELOG(!status, "HStreamCapture::UpdateJpegBasicInfo Failed to set mirror");
915 #endif
916 }
917 
CancelCapture()918 int32_t HStreamCapture::CancelCapture()
919 {
920     CAMERA_SYNC_TRACE;
921     // Cancel capture dummy till continuous/burst mode is supported
922     StopStream();
923     return CAMERA_OK;
924 }
925 
SetMode(int32_t modeName)926 void HStreamCapture::SetMode(int32_t modeName)
927 {
928     modeName_ = modeName;
929     MEDIA_DEBUG_LOG("HStreamCapture SetMode modeName = %{public}d", modeName);
930 }
931 
GetMode()932 int32_t HStreamCapture::GetMode()
933 {
934     MEDIA_INFO_LOG("HStreamCapture GetMode modeName = %{public}d", modeName_);
935     return modeName_;
936 }
937 
ConfirmCapture()938 int32_t HStreamCapture::ConfirmCapture()
939 {
940     CAMERA_SYNC_TRACE;
941     auto streamOperator = GetStreamOperator();
942     CHECK_RETURN_RET(streamOperator == nullptr, CAMERA_INVALID_STATE);
943     // LCOV_EXCL_START
944     int32_t ret = 0;
945 
946     // end burst capture
947     if (isBursting_) {
948         MEDIA_INFO_LOG("HStreamCapture::ConfirmCapture when burst capture");
949         std::vector<uint8_t> settingVector;
950         std::shared_ptr<OHOS::Camera::CameraMetadata> burstCaptureSettings = nullptr;
951         OHOS::Camera::MetadataUtils::ConvertMetadataToVec(cameraAbility_, settingVector);
952         OHOS::Camera::MetadataUtils::ConvertVecToMetadata(settingVector, burstCaptureSettings);
953         if (burstCaptureSettings == nullptr) {
954             burstCaptureSettings = std::make_shared<OHOS::Camera::CameraMetadata>(0, 0);
955         }
956         EndBurstCapture(burstCaptureSettings);
957         ret = Capture(burstCaptureSettings);
958         CHECK_PRINT_ELOG(ret != CAMERA_OK, "HStreamCapture::ConfirmCapture end burst faild!");
959         return ret;
960     }
961 
962     auto preparedCaptureId = captureIdForConfirmCapture_;
963     MEDIA_INFO_LOG("HStreamCapture::ConfirmCapture with capture ID: %{public}d", preparedCaptureId);
964     sptr<HDI::Camera::V1_2::IStreamOperator> streamOperatorV1_2 =
965         OHOS::HDI::Camera::V1_2::IStreamOperator::CastFrom(streamOperator);
966     CHECK_RETURN_RET_ELOG(streamOperatorV1_2 == nullptr, CAMERA_UNKNOWN_ERROR,
967         "HStreamCapture::ConfirmCapture streamOperatorV1_2 castFrom failed!");
968     OHOS::HDI::Camera::V1_2::CamRetCode rc =
969         (HDI::Camera::V1_2::CamRetCode)(streamOperatorV1_2->ConfirmCapture(preparedCaptureId));
970     if (rc != HDI::Camera::V1_2::NO_ERROR) {
971         MEDIA_ERR_LOG("HStreamCapture::ConfirmCapture failed with error Code: %{public}d", rc);
972         ret = HdiToServiceErrorV1_2(rc);
973     }
974     ResetCaptureId();
975     captureIdForConfirmCapture_ = CAPTURE_ID_UNSET;
976     return ret;
977     // LCOV_EXCL_STOP
978 }
979 
EndBurstCapture(const std::shared_ptr<OHOS::Camera::CameraMetadata> & captureMetadataSetting)980 void HStreamCapture::EndBurstCapture(const std::shared_ptr<OHOS::Camera::CameraMetadata>& captureMetadataSetting)
981 {
982     CHECK_RETURN(captureMetadataSetting == nullptr);
983     MEDIA_INFO_LOG("HStreamCapture::EndBurstCapture");
984     camera_metadata_item_t item;
985     bool status = false;
986     int result = OHOS::Camera::FindCameraMetadataItem(captureMetadataSetting->get(), OHOS_CONTROL_BURST_CAPTURE, &item);
987     uint8_t burstState = 0;
988     if (result == CAM_META_ITEM_NOT_FOUND) {
989         status = captureMetadataSetting->addEntry(OHOS_CONTROL_BURST_CAPTURE, &burstState, 1);
990     } else if (result == CAM_META_SUCCESS) {
991         status = captureMetadataSetting->updateEntry(OHOS_CONTROL_BURST_CAPTURE, &burstState, 1);
992     }
993 
994     CHECK_PRINT_ELOG(!status, "HStreamCapture::EndBurstCapture Failed");
995 }
996 
Release()997 int32_t HStreamCapture::Release()
998 {
999     return ReleaseStream(false);
1000 }
1001 
ReleaseStream(bool isDelay)1002 int32_t HStreamCapture::ReleaseStream(bool isDelay)
1003 {
1004     {
1005         std::lock_guard<std::mutex> lock(callbackLock_);
1006         streamCaptureCallback_ = nullptr;
1007     }
1008     int32_t errorCode = HStreamCommon::ReleaseStream(isDelay);
1009     auto hStreamOperatorSptr_ = hStreamOperator_.promote();
1010     if (hStreamOperatorSptr_ && mSwitchToOfflinePhoto_) {
1011         hStreamOperatorSptr_->Release();
1012         streamOperatorOffline_ = nullptr;
1013     }
1014     mSwitchToOfflinePhoto_ = false;
1015     return errorCode;
1016 }
1017 
SetCallback(const sptr<IStreamCaptureCallback> & callback)1018 int32_t HStreamCapture::SetCallback(const sptr<IStreamCaptureCallback> &callback)
1019 {
1020     CHECK_RETURN_RET_ELOG(callback == nullptr, CAMERA_INVALID_ARG, "HStreamCapture::SetCallback input is null");
1021     std::lock_guard<std::mutex> lock(callbackLock_);
1022     MEDIA_DEBUG_LOG("HStreamCapture::SetCallback");
1023     streamCaptureCallback_ = callback;
1024     return CAMERA_OK;
1025 }
1026 
SetPhotoAvailableCallback(const sptr<IStreamCapturePhotoCallback> & callback)1027 int32_t HStreamCapture::SetPhotoAvailableCallback(const sptr<IStreamCapturePhotoCallback> &callback)
1028 {
1029     MEDIA_INFO_LOG("HSetPhotoAvailableCallback E");
1030     CHECK_RETURN_RET_ELOG(
1031         surface_ == nullptr, CAMERA_INVALID_ARG, "HSetPhotoAvailableCallback surface is null");
1032     CHECK_RETURN_RET_ELOG(
1033         callback == nullptr, CAMERA_INVALID_ARG, "HSetPhotoAvailableCallback callback is null");
1034     std::lock_guard<std::mutex> lock(photoCallbackLock_);
1035     photoAvaiableCallback_ = callback;
1036     CHECK_RETURN_RET_ELOG(photoAssetListener_ != nullptr, CAMERA_OK, "wait to set raw callback");
1037     photoListener_ = nullptr;
1038     photoListener_ = new (std::nothrow) PhotoBufferConsumer(this, false);
1039     surface_->UnregisterConsumerListener();
1040     SurfaceError ret = surface_->RegisterConsumerListener((sptr<IBufferConsumerListener> &)photoListener_);
1041     CHECK_EXECUTE(photoTask_ == nullptr, InitCaptureThread());
1042     photoSubTask_ = nullptr;
1043     CHECK_PRINT_ELOG(ret != SURFACE_ERROR_OK, "register photoConsume failed:%{public}d", ret);
1044     return CAMERA_OK;
1045 }
1046 
UnSetPhotoAvailableCallback()1047 int32_t HStreamCapture::UnSetPhotoAvailableCallback()
1048 {
1049     MEDIA_INFO_LOG("HUnSetPhotoAvailableCallback E");
1050     std::lock_guard<std::mutex> lock(photoCallbackLock_);
1051     photoAvaiableCallback_ = nullptr;
1052     photoListener_ = nullptr;
1053     return CAMERA_OK;
1054 }
1055 
SetRawCallback()1056 void HStreamCapture::SetRawCallback()
1057 {
1058     MEDIA_INFO_LOG("HStreamCapture::SetRawCallback E");
1059     CHECK_RETURN_ELOG(photoAvaiableCallback_ == nullptr, "SetRawCallback callback is null");
1060     CHECK_RETURN_ELOG(rawSurface_ == nullptr, "HStreamCapture::SetRawCallback callback is null");
1061     photoListener_ = nullptr;
1062     photoListener_ = new (std::nothrow) PhotoBufferConsumer(this, true);
1063     rawSurface_->UnregisterConsumerListener();
1064     SurfaceError ret = rawSurface_->RegisterConsumerListener((sptr<IBufferConsumerListener> &)photoListener_);
1065     CHECK_EXECUTE(photoTask_ == nullptr, InitCaptureThread());
1066     CHECK_PRINT_ELOG(ret != SURFACE_ERROR_OK, "register rawConsumer failed:%{public}d", ret);
1067     return;
1068 }
1069 
SetPhotoAssetAvailableCallback(const sptr<IStreamCapturePhotoAssetCallback> & callback)1070 int32_t HStreamCapture::SetPhotoAssetAvailableCallback(const sptr<IStreamCapturePhotoAssetCallback> &callback)
1071 {
1072     MEDIA_INFO_LOG("HSetPhotoAssetAvailableCallback E, isYuv:%{public}d", isYuvCapture_);
1073     CHECK_RETURN_RET_ELOG(
1074         surface_ == nullptr, CAMERA_INVALID_ARG, "HStreamCapture::SetPhotoAssetAvailableCallback surface is null");
1075     CHECK_RETURN_RET_ELOG(
1076         callback == nullptr, CAMERA_INVALID_ARG, "HStreamCapture::SetPhotoAssetAvailableCallback callback is null");
1077     std::lock_guard<std::mutex> lock(photoCallbackLock_);
1078     photoAssetAvaiableCallback_ = callback;
1079     // register photoAsset surface buffer consumer
1080     if (photoAssetListener_ == nullptr) {
1081         photoAssetListener_ = new (std::nothrow) PhotoAssetBufferConsumer(this);
1082     }
1083     surface_->UnregisterConsumerListener();
1084     SurfaceError ret = surface_->RegisterConsumerListener((sptr<IBufferConsumerListener> &)photoAssetListener_);
1085     CHECK_EXECUTE(photoTask_ == nullptr, InitCaptureThread());
1086     CHECK_PRINT_ELOG(ret != SURFACE_ERROR_OK, "registerConsumerListener failed:%{public}d", ret);
1087     // register auxiliary buffer consumer
1088     CHECK_EXECUTE(isYuvCapture_, RegisterAuxiliaryConsumers());
1089     return CAMERA_OK;
1090 }
1091 
UnSetPhotoAssetAvailableCallback()1092 int32_t HStreamCapture::UnSetPhotoAssetAvailableCallback()
1093 {
1094     MEDIA_INFO_LOG("HUnSetPhotoAssetAvailableCallback E");
1095     std::lock_guard<std::mutex> lock(photoCallbackLock_);
1096     photoAssetAvaiableCallback_ = nullptr;
1097     photoAssetListener_ = nullptr;
1098     return CAMERA_OK;
1099 }
1100 
RequireMemorySize(int32_t requiredMemSizeKB)1101 int32_t HStreamCapture::RequireMemorySize(int32_t requiredMemSizeKB)
1102 {
1103     #ifdef MEMMGR_OVERRID
1104     int32_t pid = getpid();
1105     const std::string reason = "HW_CAMERA_TO_PHOTO";
1106     std::string clientName = SYSTEM_CAMERA;
1107     int32_t ret = Memory::MemMgrClient::GetInstance().RequireBigMem(pid, reason, requiredMemSizeKB, clientName);
1108     MEDIA_INFO_LOG("HCameraDevice::RequireMemory reason:%{public}s, clientName:%{public}s, ret:%{public}d",
1109         reason.c_str(), clientName.c_str(), ret);
1110     if (ret == 0) {
1111         return CAMERA_OK;
1112     }
1113     #endif
1114     return CAMERA_UNKNOWN_ERROR;
1115 }
1116 
SetThumbnailCallback(const sptr<IStreamCaptureThumbnailCallback> & callback)1117 int32_t HStreamCapture::SetThumbnailCallback(const sptr<IStreamCaptureThumbnailCallback> &callback)
1118 {
1119     MEDIA_INFO_LOG("HSetThumbnailCallback E");
1120     CHECK_RETURN_RET_ELOG(
1121         thumbnailSurface_ == nullptr, CAMERA_INVALID_ARG, "HStreamCapture::SetThumbnailCallback surface is null");
1122     CHECK_RETURN_RET_ELOG(
1123         callback == nullptr, CAMERA_INVALID_ARG, "HStreamCapture::SetThumbnailCallback callback is null");
1124     std::lock_guard<std::mutex> lock(thumbnailCallbackLock_);
1125     thumbnailAvaiableCallback_ = callback;
1126     // register thumbnail buffer consumer
1127     if (thumbnailListener_ == nullptr) {
1128         thumbnailListener_ = new (std::nothrow) ThumbnailBufferConsumer(this);
1129     }
1130     thumbnailSurface_->UnregisterConsumerListener();
1131     MEDIA_INFO_LOG("SetThumbnailCallback GetUniqueId: %{public}" PRIu64, thumbnailSurface_->GetUniqueId());
1132     SurfaceError ret = thumbnailSurface_->RegisterConsumerListener(
1133         (sptr<IBufferConsumerListener> &)thumbnailListener_);
1134     CHECK_EXECUTE(thumbnailTask_ == nullptr, InitCaptureThread());
1135     CHECK_PRINT_ELOG(ret != SURFACE_ERROR_OK, "registerConsumerListener failed:%{public}d", ret);
1136     return CAMERA_OK;
1137 }
1138 
UnSetThumbnailCallback()1139 int32_t HStreamCapture::UnSetThumbnailCallback()
1140 {
1141     MEDIA_INFO_LOG("HUnSetThumbnailCallback E");
1142     std::lock_guard<std::mutex> lock(thumbnailCallbackLock_);
1143     thumbnailAvaiableCallback_ = nullptr;
1144     thumbnailListener_ = nullptr;
1145     if (thumbnailSurface_) {
1146         thumbnailSurface_->UnregisterConsumerListener();
1147     }
1148     return CAMERA_OK;
1149 }
1150 
InitCaptureThread()1151 void HStreamCapture::InitCaptureThread()
1152 {
1153     MEDIA_INFO_LOG("HStreamCapture::InitCaptureThread E");
1154     if (photoTask_ == nullptr) {
1155         photoTask_ = std::make_shared<DeferredProcessing::TaskManager>("photoTask", TASKMANAGER_ONE, false);
1156     }
1157     if (isYuvCapture_ && photoSubTask_ == nullptr) {
1158         photoSubTask_ = std::make_shared<DeferredProcessing::TaskManager>("photoSubTask", TASKMANAGER_FOUR, false);
1159     }
1160     if (isYuvCapture_ && thumbnailTask_ == nullptr) {
1161         thumbnailTask_ = std::make_shared<DeferredProcessing::TaskManager>("thumbnailTask", TASKMANAGER_ONE, false);
1162     }
1163 }
1164 
RegisterAuxiliaryConsumers()1165 void HStreamCapture::RegisterAuxiliaryConsumers()
1166 {
1167     MEDIA_INFO_LOG("RegisterAuxiliaryConsumers E");
1168     CHECK_RETURN_ELOG(pictureAssembler_ == nullptr, "pictureAssembler is null");
1169     pictureAssembler_->RegisterAuxiliaryConsumers();
1170 }
1171 
1172 // LCOV_EXCL_START
UnSetCallback()1173 int32_t HStreamCapture::UnSetCallback()
1174 {
1175     std::lock_guard<std::mutex> lock(callbackLock_);
1176     streamCaptureCallback_ = nullptr;
1177     return CAMERA_OK;
1178 }
1179 // LCOV_EXCL_STOP
1180 
OnCaptureStarted(int32_t captureId)1181 int32_t HStreamCapture::OnCaptureStarted(int32_t captureId)
1182 {
1183     CAMERA_SYNC_TRACE;
1184     std::lock_guard<std::mutex> lock(callbackLock_);
1185     CHECK_EXECUTE(streamCaptureCallback_ != nullptr, streamCaptureCallback_->OnCaptureStarted(captureId));
1186     return CAMERA_OK;
1187 }
1188 
OnCaptureStarted(int32_t captureId,uint32_t exposureTime)1189 int32_t HStreamCapture::OnCaptureStarted(int32_t captureId, uint32_t exposureTime)
1190 {
1191     CAMERA_SYNC_TRACE;
1192     std::lock_guard<std::mutex> lock(callbackLock_);
1193     CHECK_EXECUTE(streamCaptureCallback_ != nullptr,
1194         streamCaptureCallback_->OnCaptureStarted(captureId, exposureTime));
1195     return CAMERA_OK;
1196 }
1197 
OnCaptureEnded(int32_t captureId,int32_t frameCount)1198 int32_t HStreamCapture::OnCaptureEnded(int32_t captureId, int32_t frameCount)
1199 {
1200     CAMERA_SYNC_TRACE;
1201     std::lock_guard<std::mutex> lock(callbackLock_);
1202     CHECK_EXECUTE(streamCaptureCallback_ != nullptr, streamCaptureCallback_->OnCaptureEnded(captureId, frameCount));
1203     MEDIA_INFO_LOG("HStreamCapture::Capture, notify OnCaptureEnded with capture ID: %{public}d", captureId);
1204     int32_t offlineOutputCnt = mSwitchToOfflinePhoto_ ?
1205         HStreamOperatorManager::GetInstance()->GetOfflineOutputSize() : 0;
1206     CameraReportUtils::GetInstance().SetCapturePerfEndInfo(captureId, mSwitchToOfflinePhoto_, offlineOutputCnt);
1207     auto preparedCaptureId = GetPreparedCaptureId();
1208     if (preparedCaptureId != CAPTURE_ID_UNSET) {
1209         MEDIA_INFO_LOG("HStreamCapture::OnCaptureEnded capturId = %{public}d already used, need release",
1210                        preparedCaptureId);
1211         ResetCaptureId();
1212         captureIdForConfirmCapture_ = CAPTURE_ID_UNSET;
1213     }
1214     return CAMERA_OK;
1215 }
1216 
OnCaptureError(int32_t captureId,int32_t errorCode)1217 int32_t HStreamCapture::OnCaptureError(int32_t captureId, int32_t errorCode)
1218 {
1219     std::lock_guard<std::mutex> lock(callbackLock_);
1220     if (streamCaptureCallback_ != nullptr) {
1221         // LCOV_EXCL_START
1222         int32_t captureErrorCode;
1223         if (errorCode == BUFFER_LOST) {
1224             captureErrorCode = CAMERA_STREAM_BUFFER_LOST;
1225         } else {
1226             captureErrorCode = CAMERA_UNKNOWN_ERROR;
1227         }
1228         CAMERA_SYSEVENT_FAULT(CreateMsg("Photo OnCaptureError! captureId:%d & "
1229                                         "errorCode:%{public}d", captureId, captureErrorCode));
1230         streamCaptureCallback_->OnCaptureError(captureId, captureErrorCode);
1231         // LCOV_EXCL_STOP
1232     }
1233     auto preparedCaptureId = GetPreparedCaptureId();
1234     if (preparedCaptureId != CAPTURE_ID_UNSET) {
1235         // LCOV_EXCL_START
1236         MEDIA_INFO_LOG("HStreamCapture::OnCaptureError capturId = %{public}d already used, need release",
1237                        preparedCaptureId);
1238         ResetCaptureId();
1239         captureIdForConfirmCapture_ = CAPTURE_ID_UNSET;
1240         // LCOV_EXCL_STOP
1241     }
1242     return CAMERA_OK;
1243 }
1244 
OnFrameShutter(int32_t captureId,uint64_t timestamp)1245 int32_t HStreamCapture::OnFrameShutter(int32_t captureId, uint64_t timestamp)
1246 {
1247     CAMERA_SYNC_TRACE;
1248     std::lock_guard<std::mutex> lock(callbackLock_);
1249     CHECK_EXECUTE(streamCaptureCallback_ != nullptr,
1250         streamCaptureCallback_->OnFrameShutter(captureId, timestamp));
1251     return CAMERA_OK;
1252 }
1253 
OnFrameShutterEnd(int32_t captureId,uint64_t timestamp)1254 int32_t HStreamCapture::OnFrameShutterEnd(int32_t captureId, uint64_t timestamp)
1255 {
1256     CAMERA_SYNC_TRACE;
1257     std::lock_guard<std::mutex> lock(callbackLock_);
1258     CHECK_EXECUTE(streamCaptureCallback_ != nullptr,
1259         streamCaptureCallback_->OnFrameShutterEnd(captureId, timestamp));
1260     if (isBursting_) {
1261         burstNum_++;
1262         MEDIA_DEBUG_LOG("HStreamCapture::OnFrameShutterEnd burstNum:%{public}d", burstNum_);
1263     }
1264     return CAMERA_OK;
1265 }
1266 
1267 
OnCaptureReady(int32_t captureId,uint64_t timestamp)1268 int32_t HStreamCapture::OnCaptureReady(int32_t captureId, uint64_t timestamp)
1269 {
1270     CAMERA_SYNC_TRACE;
1271     std::lock_guard<std::mutex> lock(callbackLock_);
1272     MEDIA_INFO_LOG("HStreamCapture::Capture, notify OnCaptureReady with capture ID: %{public}d", captureId);
1273     isCaptureReady_ = true;
1274     CHECK_EXECUTE(streamCaptureCallback_ != nullptr,
1275         streamCaptureCallback_->OnCaptureReady(captureId, timestamp));
1276     std::lock_guard<std::mutex> burstLock(burstLock_);
1277     if (IsBurstCapture(captureId)) {
1278         burstNumMap_[captureId] = burstNum_;
1279         ResetBurst();
1280     }
1281     return CAMERA_OK;
1282 }
1283 
OnPhotoAvailable(sptr<SurfaceBuffer> surfaceBuffer,const int64_t timestamp,bool isRaw)1284 int32_t HStreamCapture::OnPhotoAvailable(sptr<SurfaceBuffer> surfaceBuffer, const int64_t timestamp, bool isRaw)
1285 {
1286     CAMERA_SYNC_TRACE;
1287     MEDIA_INFO_LOG("HStreamCapture::OnPhotoAvailable is called!");
1288     std::lock_guard<std::mutex> lock(photoCallbackLock_);
1289     if (photoAvaiableCallback_ != nullptr) {
1290         photoAvaiableCallback_->OnPhotoAvailable(surfaceBuffer, timestamp, isRaw);
1291     }
1292     return CAMERA_OK;
1293 }
1294 
OnPhotoAssetAvailable(const int32_t captureId,const std::string & uri,int32_t cameraShotType,const std::string & burstKey)1295 int32_t HStreamCapture::OnPhotoAssetAvailable(
1296     const int32_t captureId, const std::string &uri, int32_t cameraShotType, const std::string &burstKey)
1297 {
1298     CAMERA_SYNC_TRACE;
1299     MEDIA_INFO_LOG("HStreamCapture::OnPhotoAssetAvailable is called!");
1300     std::lock_guard<std::mutex> lock(photoCallbackLock_);
1301     if (photoAssetAvaiableCallback_ != nullptr) {
1302         photoAssetAvaiableCallback_->OnPhotoAssetAvailable(captureId, uri, cameraShotType, burstKey);
1303     }
1304     return CAMERA_OK;
1305 }
1306 
OnThumbnailAvailable(sptr<SurfaceBuffer> surfaceBuffer,const int64_t timestamp)1307 int32_t HStreamCapture::OnThumbnailAvailable(sptr<SurfaceBuffer> surfaceBuffer, const int64_t timestamp)
1308 {
1309     CAMERA_SYNC_TRACE;
1310     MEDIA_INFO_LOG("HStreamCapture::OnThumbnailAvailable is called!");
1311     std::lock_guard<std::mutex> lock(thumbnailCallbackLock_);
1312     if (thumbnailAvaiableCallback_ != nullptr) {
1313         thumbnailAvaiableCallback_->OnThumbnailAvailable(surfaceBuffer, timestamp);
1314     }
1315     return CAMERA_OK;
1316 }
1317 
EnableOfflinePhoto(bool isEnable)1318 int32_t HStreamCapture::EnableOfflinePhoto(bool isEnable)
1319 {
1320     mEnableOfflinePhoto_ = isEnable;
1321     return CAMERA_OK;
1322 }
1323 
IsHasEnableOfflinePhoto()1324 bool HStreamCapture::IsHasEnableOfflinePhoto()
1325 {
1326     return mEnableOfflinePhoto_;
1327 }
1328 
SwitchToOffline()1329 void HStreamCapture::SwitchToOffline()
1330 {
1331     mSwitchToOfflinePhoto_ = true;
1332     streamOperatorOffline_ = GetStreamOperator();
1333 }
1334 
IsHasSwitchToOffline()1335 bool HStreamCapture::IsHasSwitchToOffline()
1336 {
1337     return mSwitchToOfflinePhoto_;
1338 }
1339 
DumpStreamInfo(CameraInfoDumper & infoDumper)1340 void HStreamCapture::DumpStreamInfo(CameraInfoDumper& infoDumper)
1341 {
1342     infoDumper.Title("capture stream");
1343     infoDumper.Msg("ThumbnailSwitch:[" + std::to_string(thumbnailSwitch_) + "]");
1344     infoDumper.Msg("RawDeliverSwitch:[" + std::to_string(rawDeliverySwitch_) + "]");
1345     if (thumbnailBufferQueue_) {
1346         infoDumper.Msg(
1347             "ThumbnailBuffer producer Id:[" + std::to_string(thumbnailBufferQueue_->producer_->GetUniqueId()) + "]");
1348     }
1349     HStreamCommon::DumpStreamInfo(infoDumper);
1350 }
1351 
OperatePermissionCheck(uint32_t interfaceCode)1352 int32_t HStreamCapture::OperatePermissionCheck(uint32_t interfaceCode)
1353 {
1354     switch (static_cast<IStreamCaptureIpcCode>(interfaceCode)) {
1355         case IStreamCaptureIpcCode::COMMAND_CAPTURE: {
1356             auto callerToken = IPCSkeleton::GetCallingTokenID();
1357             CHECK_RETURN_RET_ELOG(callerToken_ != callerToken, CAMERA_OPERATION_NOT_ALLOWED,
1358                 "HStreamCapture::OperatePermissionCheck fail, callerToken_ is : %{public}d, now token "
1359                 "is %{public}d", callerToken_, callerToken);
1360             break;
1361         }
1362         default:
1363             break;
1364     }
1365     return CAMERA_OK;
1366 }
1367 
CallbackEnter(uint32_t code)1368 int32_t HStreamCapture::CallbackEnter([[maybe_unused]] uint32_t code)
1369 {
1370     MEDIA_DEBUG_LOG("start, code:%{public}u", code);
1371     DisableJeMalloc();
1372     int32_t errCode = OperatePermissionCheck(code);
1373     CHECK_RETURN_RET_ELOG(errCode != CAMERA_OK, errCode, "HStreamCapture::OperatePermissionCheck fail");
1374     switch (static_cast<IStreamCaptureIpcCode>(code)) {
1375         case IStreamCaptureIpcCode::COMMAND_SET_THUMBNAIL:
1376         case IStreamCaptureIpcCode::COMMAND_ENABLE_RAW_DELIVERY:
1377         case IStreamCaptureIpcCode::COMMAND_DEFER_IMAGE_DELIVERY_FOR:
1378         case IStreamCaptureIpcCode::COMMAND_CONFIRM_CAPTURE:
1379         case IStreamCaptureIpcCode::COMMAND_ENABLE_OFFLINE_PHOTO : {
1380             CHECK_RETURN_RET_ELOG(!CheckSystemApp(), CAMERA_NO_PERMISSION, "HStreamCapture::CheckSystemApp fail");
1381             break;
1382         }
1383         case IStreamCaptureIpcCode::COMMAND_ENABLE_MOVING_PHOTO: {
1384             uint32_t callerToken = IPCSkeleton::GetCallingTokenID();
1385             int32_t errCode = CheckPermission(OHOS_PERMISSION_MICROPHONE, callerToken);
1386             CHECK_RETURN_RET_ELOG(errCode != CAMERA_OK, CAMERA_NO_PERMISSION, "check microphone permission failed.");
1387             break;
1388         }
1389         default:
1390             break;
1391     }
1392     return CAMERA_OK;
1393 }
1394 
CallbackExit(uint32_t code,int32_t result)1395 int32_t HStreamCapture::CallbackExit([[maybe_unused]] uint32_t code, [[maybe_unused]] int32_t result)
1396 {
1397     MEDIA_DEBUG_LOG("leave, code:%{public}u, result:%{public}d", code, result);
1398     return CAMERA_OK;
1399 }
1400 
IsDeferredPhotoEnabled()1401 int32_t HStreamCapture::IsDeferredPhotoEnabled()
1402 {
1403     MEDIA_INFO_LOG("HStreamCapture IsDeferredPhotoEnabled  deferredPhotoSwitch_: %{public}d", deferredPhotoSwitch_);
1404     CHECK_RETURN_RET(deferredPhotoSwitch_ == 1, 1);
1405     MEDIA_INFO_LOG("HStreamCapture IsDeferredPhotoEnabled return 0");
1406     return 0;
1407 }
1408 
IsDeferredVideoEnabled()1409 int32_t HStreamCapture::IsDeferredVideoEnabled()
1410 {
1411     MEDIA_INFO_LOG("HStreamCapture IsDeferredVideoEnabled  deferredVideoSwitch_: %{public}d", deferredVideoSwitch_);
1412     CHECK_RETURN_RET(deferredVideoSwitch_ == 1, 1);
1413     return 0;
1414 }
1415 
GetMovingPhotoVideoCodecType()1416 int32_t HStreamCapture::GetMovingPhotoVideoCodecType()
1417 {
1418     MEDIA_INFO_LOG("HStreamCapture GetMovingPhotoVideoCodecType videoCodecType_: %{public}d", videoCodecType_);
1419     return videoCodecType_;
1420 }
1421 
SetMovingPhotoVideoCodecType(int32_t videoCodecType)1422 int32_t HStreamCapture::SetMovingPhotoVideoCodecType(int32_t videoCodecType)
1423 {
1424     MEDIA_INFO_LOG("HStreamCapture SetMovingPhotoVideoCodecType videoCodecType_: %{public}d", videoCodecType);
1425     videoCodecType_ = videoCodecType;
1426     return 0;
1427 }
1428 
SetCameraPhotoRotation(bool isEnable)1429 int32_t HStreamCapture::SetCameraPhotoRotation(bool isEnable)
1430 {
1431     enableCameraPhotoRotation_ = isEnable;
1432     return 0;
1433 }
1434 
1435 // LCOV_EXCL_START
GetLocation(const std::shared_ptr<OHOS::Camera::CameraMetadata> & captureMetadataSetting)1436 void HStreamCapture::GetLocation(const std::shared_ptr<OHOS::Camera::CameraMetadata> &captureMetadataSetting)
1437 {
1438     MEDIA_INFO_LOG("GetLocation E");
1439     camera_metadata_item_t item;
1440     const int32_t targetCount = 2;
1441     const int32_t latIndex = 0;
1442     const int32_t lonIndex = 1;
1443     const int32_t altIndex = 2;
1444     int result = OHOS::Camera::FindCameraMetadataItem(captureMetadataSetting->get(), OHOS_JPEG_GPS_COORDINATES, &item);
1445     if (result == CAM_META_SUCCESS && item.count > targetCount) {
1446         latitude_ = item.data.d[latIndex];
1447         longitude_ = item.data.d[lonIndex];
1448         altitude_ = item.data.d[altIndex];
1449     }
1450 }
1451 
SetCameraPhotoProxyInfo(sptr<CameraServerPhotoProxy> cameraPhotoProxy)1452 void HStreamCapture::SetCameraPhotoProxyInfo(sptr<CameraServerPhotoProxy> cameraPhotoProxy)
1453 {
1454     MEDIA_INFO_LOG("SetCameraPhotoProxyInfo get captureStream");
1455     cameraPhotoProxy->SetDisplayName(CreateDisplayName(format_ == OHOS_CAMERA_FORMAT_HEIC ? suffixHeif : suffixJpeg));
1456     cameraPhotoProxy->SetShootingMode(GetMode());
1457     MEDIA_INFO_LOG("SetCameraPhotoProxyInfo quality:%{public}d, format:%{public}d",
1458         cameraPhotoProxy->GetPhotoQuality(), cameraPhotoProxy->GetFormat());
1459     auto hStreamOperatorSptr_ = hStreamOperator_.promote();
1460     CHECK_RETURN(hStreamOperatorSptr_ == nullptr);
1461 
1462     camera_metadata_item_t item;
1463     if (hStreamOperatorSptr_->GetDeviceAbilityByMeta(OHOS_ABILITY_MOVING_PHOTO_MICRO_VIDEO_ENHANCE, &item)) {
1464         uint8_t status = item.data.u8[0];
1465         cameraPhotoProxy->SetStageVideoTaskStatus(status);
1466     }
1467 }
1468 
UpdateMediaLibraryPhotoAssetProxy(sptr<CameraServerPhotoProxy> cameraPhotoProxy)1469 int32_t HStreamCapture::UpdateMediaLibraryPhotoAssetProxy(sptr<CameraServerPhotoProxy> cameraPhotoProxy)
1470 {
1471     CAMERA_SYNC_TRACE;
1472     CHECK_RETURN_RET(
1473         isBursting_ || (GetMode() == static_cast<int32_t>(HDI::Camera::V1_3::OperationMode::PROFESSIONAL_PHOTO)),
1474         CAMERA_UNSUPPORTED);
1475     const int32_t updateMediaLibraryStep = 1;
1476     if (!photoAssetProxy_.WaitForUnlock(cameraPhotoProxy->GetCaptureId(), updateMediaLibraryStep, GetMode(),
1477                                         std::chrono::seconds(1))) {
1478         return CAMERA_UNKNOWN_ERROR;
1479     }
1480     std::shared_ptr<PhotoAssetIntf> photoAssetProxy = photoAssetProxy_.Get(cameraPhotoProxy->GetCaptureId());
1481     CHECK_RETURN_RET_ELOG(
1482         photoAssetProxy == nullptr, CAMERA_UNKNOWN_ERROR, "HStreamCapture UpdateMediaLibraryPhotoAssetProxy failed");
1483     MEDIA_DEBUG_LOG(
1484         "HStreamCapture UpdateMediaLibraryPhotoAssetProxy E captureId(%{public}d)", cameraPhotoProxy->GetCaptureId());
1485     SetCameraPhotoProxyInfo(cameraPhotoProxy);
1486     MEDIA_DEBUG_LOG("HStreamCapture AddPhotoProxy E");
1487     photoAssetProxy->AddPhotoProxy(cameraPhotoProxy);
1488     MEDIA_DEBUG_LOG("HStreamCapture AddPhotoProxy X");
1489     photoAssetProxy_.IncreaseCaptureStep(cameraPhotoProxy->GetCaptureId());
1490     MEDIA_DEBUG_LOG(
1491         "HStreamCapture UpdateMediaLibraryPhotoAssetProxy X captureId(%{public}d)", cameraPhotoProxy->GetCaptureId());
1492     return CAMERA_OK;
1493 }
1494 
SetStreamOperator(wptr<HStreamOperator> hStreamOperator)1495 void HStreamCapture::SetStreamOperator(wptr<HStreamOperator> hStreamOperator)
1496 {
1497     hStreamOperator_ = hStreamOperator;
1498 }
1499 
CreateMediaLibrary(std::shared_ptr<PictureIntf> picture,sptr<CameraServerPhotoProxy> & cameraPhotoProxy,std::string & uri,int32_t & cameraShotType,std::string & burstKey,int64_t timestamp)1500 int32_t HStreamCapture::CreateMediaLibrary(std::shared_ptr<PictureIntf> picture,
1501     sptr<CameraServerPhotoProxy> &cameraPhotoProxy, std::string &uri, int32_t &cameraShotType, std::string &burstKey,
1502     int64_t timestamp)
1503 {
1504     auto hStreamOperatorSptr_ = hStreamOperator_.promote();
1505     if (hStreamOperatorSptr_) {
1506         CHECK_RETURN_RET_ELOG(
1507             cameraPhotoProxy == nullptr, CAMERA_UNKNOWN_ERROR, "CreateMediaLibrary with null PhotoProxy");
1508         cameraPhotoProxy->SetLatitude(latitude_);
1509         cameraPhotoProxy->SetLongitude(longitude_);
1510         hStreamOperatorSptr_->CreateMediaLibrary(picture, cameraPhotoProxy, uri, cameraShotType, burstKey, timestamp);
1511     }
1512     return CAMERA_OK;
1513 }
1514 
CreateMediaLibrary(sptr<CameraServerPhotoProxy> & cameraPhotoProxy,std::string & uri,int32_t & cameraShotType,std::string & burstKey,int64_t timestamp)1515 int32_t HStreamCapture::CreateMediaLibrary(sptr<CameraServerPhotoProxy> &cameraPhotoProxy, std::string &uri,
1516     int32_t &cameraShotType, std::string &burstKey, int64_t timestamp)
1517 {
1518     auto hStreamOperatorSptr_ = hStreamOperator_.promote();
1519     if (hStreamOperatorSptr_) {
1520         CHECK_RETURN_RET_ELOG(
1521             cameraPhotoProxy == nullptr, CAMERA_UNKNOWN_ERROR, "CreateMediaLibrary with null PhotoProxy");
1522         cameraPhotoProxy->SetLatitude(latitude_);
1523         cameraPhotoProxy->SetLongitude(longitude_);
1524         hStreamOperatorSptr_->CreateMediaLibrary(cameraPhotoProxy, uri, cameraShotType, burstKey, timestamp);
1525     }
1526     return CAMERA_OK;
1527 }
1528 
CreateMediaLibrary(const sptr<CameraPhotoProxy> & photoProxy,std::string & uri,int32_t & cameraShotType,std::string & burstKey,int64_t timestamp)1529 int32_t HStreamCapture::CreateMediaLibrary(const sptr<CameraPhotoProxy> &photoProxy, std::string &uri,
1530     int32_t &cameraShotType, std::string &burstKey, int64_t timestamp)
1531 {
1532     MessageParcel data;
1533     photoProxy->WriteToParcel(data);
1534     photoProxy->CameraFreeBufferHandle();
1535     sptr<CameraServerPhotoProxy> cameraPhotoProxy = new CameraServerPhotoProxy();
1536     cameraPhotoProxy->ReadFromParcel(data);
1537     auto hStreamOperatorSptr_ = hStreamOperator_.promote();
1538     CHECK_RETURN_RET_ELOG(!hStreamOperatorSptr_, CAMERA_UNKNOWN_ERROR, "CreateMediaLibrary with null operator");
1539     CHECK_RETURN_RET_ELOG(!cameraPhotoProxy, CAMERA_UNKNOWN_ERROR, "CreateMediaLibrary with null photoProxy");
1540     cameraPhotoProxy->SetLatitude(latitude_);
1541     cameraPhotoProxy->SetLongitude(longitude_);
1542     hStreamOperatorSptr_->CreateMediaLibrary(cameraPhotoProxy, uri, cameraShotType, burstKey, timestamp);
1543     return CAMERA_OK;
1544 }
1545 // LCOV_EXCL_STOP
1546 } // namespace CameraStandard
1547 } // namespace OHOS
1548