• 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 
22 #include "camera_log.h"
23 #include "camera_server_photo_proxy.h"
24 #include "camera_service_ipc_interface_code.h"
25 #include "camera_util.h"
26 #include "camera/v1_4/types.h"
27 #include "hstream_common.h"
28 #include "ipc_skeleton.h"
29 #include "metadata_utils.h"
30 #include "camera_report_uitls.h"
31 #include "photo_asset_interface.h"
32 #include "photo_asset_proxy.h"
33 #include "camera_report_dfx_uitls.h"
34 #include "bms_adapter.h"
35 #include "picture_interface.h"
36 #include "hstream_operator_manager.h"
37 #include "hstream_operator.h"
38 #include "display/graphic/common/v1_0/cm_color_space.h"
39 
40 namespace OHOS {
41 namespace CameraStandard {
42 using namespace OHOS::HDI::Camera::V1_0;
43 static const int32_t CAPTURE_ROTATE_360 = 360;
44 static const std::string BURST_UUID_BEGIN = "";
45 static std::string g_currentBurstUuid = BURST_UUID_BEGIN;
46 
GenerateBurstUuid()47 static std::string GenerateBurstUuid()
48 {
49     MEDIA_INFO_LOG("HStreamCapture::GenerateBurstUuid");
50     uuid_t uuid;
51     char str[37] = {}; // UUIDs are 36 characters plus the null terminator
52     uuid_generate(uuid);
53     uuid_unparse(uuid, str); // Convert the UUID to a string
54     std::string burstUuid(str);
55     g_currentBurstUuid = burstUuid;
56     return burstUuid;
57 }
58 
HStreamCapture(sptr<OHOS::IBufferProducer> producer,int32_t format,int32_t width,int32_t height)59 HStreamCapture::HStreamCapture(sptr<OHOS::IBufferProducer> producer, int32_t format, int32_t width, int32_t height)
60     : HStreamCommon(StreamType::CAPTURE, producer, format, width, height)
61 {
62     MEDIA_INFO_LOG(
63         "HStreamCapture::HStreamCapture construct, format:%{public}d size:%{public}dx%{public}d streamId:%{public}d",
64         format, width, height, GetFwkStreamId());
65     thumbnailSwitch_ = 0;
66     rawDeliverySwitch_ = 0;
67     modeName_ = 0;
68     deferredPhotoSwitch_ = 0;
69     deferredVideoSwitch_ = 0;
70     burstNum_ = 0;
71     movingPhotoSwitch_ = 0;
72 }
73 
~HStreamCapture()74 HStreamCapture::~HStreamCapture()
75 {
76     photoAssetProxy_.Release();
77     rotationMap_.Clear();
78     MEDIA_INFO_LOG(
79         "HStreamCapture::~HStreamCapture deconstruct, format:%{public}d size:%{public}dx%{public}d streamId:%{public}d",
80         format_, width_, height_, GetFwkStreamId());
81 }
82 
LinkInput(wptr<HDI::Camera::V1_0::IStreamOperator> streamOperator,std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility)83 int32_t HStreamCapture::LinkInput(wptr<HDI::Camera::V1_0::IStreamOperator> streamOperator,
84     std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility)
85 {
86     MEDIA_INFO_LOG("HStreamCapture::LinkInput streamId:%{public}d", GetFwkStreamId());
87     return HStreamCommon::LinkInput(streamOperator, cameraAbility);
88 }
89 
FillingPictureExtendStreamInfos(StreamInfo_V1_1 & streamInfo,int32_t format)90 void HStreamCapture::FillingPictureExtendStreamInfos(StreamInfo_V1_1 &streamInfo, int32_t format)
91 {
92     HDI::Camera::V1_1::ExtendedStreamInfo gainmapExtendedStreamInfo = {
93         .type = static_cast<HDI::Camera::V1_1::ExtendedStreamInfoType>(HDI::Camera::V1_3::EXTENDED_STREAM_INFO_GAINMAP),
94         .width = width_,
95         .height = height_,
96         .format = format, // HDR:NV21 P3:NV21
97         .dataspace = dataSpace_, // HDR:BT2020_HLG_FULL P3:P3_FULL
98         .bufferQueue = gainmapBufferQueue_,
99     };
100     HDI::Camera::V1_1::ExtendedStreamInfo deepExtendedStreamInfo = {
101         .type = static_cast<HDI::Camera::V1_1::ExtendedStreamInfoType>(HDI::Camera::V1_3::EXTENDED_STREAM_INFO_DEPTH),
102         .width = width_,
103         .height = height_,
104         .format = format,
105         .bufferQueue = deepBufferQueue_,
106     };
107     HDI::Camera::V1_1::ExtendedStreamInfo exifExtendedStreamInfo = {
108         .type = static_cast<HDI::Camera::V1_1::ExtendedStreamInfoType>(HDI::Camera::V1_3::EXTENDED_STREAM_INFO_EXIF),
109         .width = width_,
110         .height = height_,
111         .format = format,
112         .bufferQueue = exifBufferQueue_,
113     };
114     HDI::Camera::V1_1::ExtendedStreamInfo debugExtendedStreamInfo = {
115         .type =
116             static_cast<HDI::Camera::V1_1::ExtendedStreamInfoType>(HDI::Camera::V1_3::EXTENDED_STREAM_INFO_MAKER_INFO),
117         .width = width_,
118         .height = height_,
119         .format = format,
120         .bufferQueue = debugBufferQueue_,
121     };
122     std::vector<HDI::Camera::V1_1::ExtendedStreamInfo> extendedStreams = { gainmapExtendedStreamInfo,
123         deepExtendedStreamInfo, exifExtendedStreamInfo, debugExtendedStreamInfo };
124     streamInfo.extendedStreamInfos.insert(streamInfo.extendedStreamInfos.end(),
125         extendedStreams.begin(), extendedStreams.end());
126 }
127 
FillingRawAndThumbnailStreamInfo(StreamInfo_V1_1 & streamInfo)128 void HStreamCapture::FillingRawAndThumbnailStreamInfo(StreamInfo_V1_1 &streamInfo)
129 {
130     if (rawDeliverySwitch_ && format_ != OHOS_CAMERA_FORMAT_DNG_XDRAW) {
131         MEDIA_INFO_LOG("HStreamCapture::SetStreamInfo Set DNG info, streamId:%{public}d", GetFwkStreamId());
132         HDI::Camera::V1_1::ExtendedStreamInfo extendedStreamInfo = {
133             .type = static_cast<HDI::Camera::V1_1::ExtendedStreamInfoType>(HDI::Camera::V1_3::EXTENDED_STREAM_INFO_RAW),
134             .width = width_,
135             .height = height_,
136             .format = streamInfo.v1_0.format_,
137             .dataspace = 0,
138             .bufferQueue = rawBufferQueue_,
139         };
140         streamInfo.extendedStreamInfos.push_back(extendedStreamInfo);
141     }
142     if (thumbnailSwitch_) {
143         MEDIA_DEBUG_LOG("HStreamCapture::SetStreamInfo Set thumbnail info, dataspace:%{public}d", dataSpace_);
144         int32_t pixelFormat = GRAPHIC_PIXEL_FMT_YCRCB_420_SP;
145         pixelFormat = dataSpace_ == CM_BT2020_HLG_FULL ? GRAPHIC_PIXEL_FMT_YCRCB_P010 : pixelFormat;
146         HDI::Camera::V1_1::ExtendedStreamInfo extendedStreamInfo = {
147             .type = HDI::Camera::V1_1::EXTENDED_STREAM_INFO_QUICK_THUMBNAIL,
148             .width = 0,
149             .height = 0,
150             .format = pixelFormat, // HDR: YCRCB_P010 P3: nv21
151             .dataspace = dataSpace_, // HDR: BT2020_HLG_FULL P3: P3
152             .bufferQueue = thumbnailBufferQueue_,
153         };
154         streamInfo.extendedStreamInfos.push_back(extendedStreamInfo);
155     }
156 }
157 
SetStreamInfo(StreamInfo_V1_1 & streamInfo)158 void HStreamCapture::SetStreamInfo(StreamInfo_V1_1 &streamInfo)
159 {
160     HStreamCommon::SetStreamInfo(streamInfo);
161     MEDIA_INFO_LOG("HStreamCapture::SetStreamInfo streamId:%{public}d format:%{public}d", GetFwkStreamId(), format_);
162     streamInfo.v1_0.intent_ = STILL_CAPTURE;
163     if (format_ == OHOS_CAMERA_FORMAT_HEIC) {
164         streamInfo.v1_0.encodeType_ =
165             static_cast<HDI::Camera::V1_0::EncodeType>(HDI::Camera::V1_3::ENCODE_TYPE_HEIC);
166         streamInfo.v1_0.format_ = GRAPHIC_PIXEL_FMT_BLOB;
167     } else if (format_ == OHOS_CAMERA_FORMAT_YCRCB_420_SP) { // NV21
168         streamInfo.v1_0.encodeType_ = ENCODE_TYPE_NULL;
169         streamInfo.v1_0.format_ = GRAPHIC_PIXEL_FMT_YCRCB_420_SP; // NV21
170         if (GetMode() != static_cast<int32_t>(HDI::Camera::V1_3::OperationMode::TIMELAPSE_PHOTO)) {
171             FillingPictureExtendStreamInfos(streamInfo, GRAPHIC_PIXEL_FMT_YCRCB_420_SP);
172         }
173         if (dataSpace_ == CM_BT2020_HLG_FULL || dataSpace_ == CM_BT2020_HLG_LIMIT) {
174             streamInfo.v1_0.dataspace_ = CM_P3_FULL; // HDR photo need P3 for captureStream
175         } else if (dataSpace_ == CM_BT709_LIMIT) {
176             streamInfo.v1_0.dataspace_ = CM_SRGB_FULL; // video session need SRGB for captureStream
177         }
178     } else if (format_ == OHOS_CAMERA_FORMAT_DNG_XDRAW) {
179         streamInfo.v1_0.encodeType_ =
180             static_cast<HDI::Camera::V1_0::EncodeType>(HDI::Camera::V1_4::ENCODE_TYPE_DNG_XDRAW);
181     } else {
182         streamInfo.v1_0.encodeType_ = ENCODE_TYPE_JPEG;
183     }
184     FillingRawAndThumbnailStreamInfo(streamInfo);
185 }
186 
SetThumbnail(bool isEnabled,const sptr<OHOS::IBufferProducer> & producer)187 int32_t HStreamCapture::SetThumbnail(bool isEnabled, const sptr<OHOS::IBufferProducer> &producer)
188 {
189     if (isEnabled && producer != nullptr) {
190         thumbnailSwitch_ = 1;
191         thumbnailBufferQueue_ = new BufferProducerSequenceable(producer);
192     } else {
193         thumbnailSwitch_ = 0;
194         thumbnailBufferQueue_ = nullptr;
195     }
196     return CAMERA_OK;
197 }
198 
EnableRawDelivery(bool enabled)199 int32_t HStreamCapture::EnableRawDelivery(bool enabled)
200 {
201     if (enabled) {
202         rawDeliverySwitch_ = 1;
203     } else {
204         rawDeliverySwitch_ = 0;
205     }
206     return CAMERA_OK;
207 }
208 
209 // LCOV_EXCL_START
EnableMovingPhoto(bool enabled)210 int32_t HStreamCapture::EnableMovingPhoto(bool enabled)
211 {
212     if (enabled) {
213         movingPhotoSwitch_ = 1;
214     } else {
215         movingPhotoSwitch_ = 0;
216     }
217     return CAMERA_OK;
218 }
219 // LCOV_EXCL_STOP
220 
SetBufferProducerInfo(const std::string bufName,const sptr<OHOS::IBufferProducer> & producer)221 int32_t HStreamCapture::SetBufferProducerInfo(const std::string bufName, const sptr<OHOS::IBufferProducer> &producer)
222 {
223     std::string resStr = "";
224     if (bufName == "rawImage") {
225         if (producer != nullptr) {
226             rawBufferQueue_ = new BufferProducerSequenceable(producer);
227         } else {
228             rawBufferQueue_ = nullptr;
229             resStr += bufName + ",";
230         }
231     }
232     if (bufName == "gainmapImage") {
233         if (producer != nullptr) {
234             gainmapBufferQueue_ = new BufferProducerSequenceable(producer);
235         } else {
236             gainmapBufferQueue_ = nullptr;
237             resStr += bufName + ",";
238         }
239     }
240     if (bufName == "deepImage") {
241         if (producer != nullptr) {
242             deepBufferQueue_ = new BufferProducerSequenceable(producer);
243         } else {
244             deepBufferQueue_ = nullptr;
245             resStr += bufName + ",";
246         }
247     }
248     if (bufName == "exifImage") {
249         if (producer != nullptr) {
250             exifBufferQueue_ = new BufferProducerSequenceable(producer);
251         } else {
252             exifBufferQueue_ = nullptr;
253             resStr += bufName + ",";
254         }
255     }
256     if (bufName == "debugImage") {
257         if (producer != nullptr) {
258             debugBufferQueue_ = new BufferProducerSequenceable(producer);
259         } else {
260             debugBufferQueue_ = nullptr;
261             resStr += bufName + ",";
262         }
263     }
264     MEDIA_INFO_LOG("HStreamCapture::SetBufferProducerInfo bufferQueue whether is nullptr: %{public}s", resStr.c_str());
265     return CAMERA_OK;
266 }
267 
DeferImageDeliveryFor(int32_t type)268 int32_t HStreamCapture::DeferImageDeliveryFor(int32_t type)
269 {
270     MEDIA_INFO_LOG("HStreamCapture::DeferImageDeliveryFor type: %{public}d", type);
271     if (type == OHOS::HDI::Camera::V1_2::STILL_IMAGE) {
272         MEDIA_INFO_LOG("HStreamCapture STILL_IMAGE");
273         deferredPhotoSwitch_ = 1;
274     } else if (type == OHOS::HDI::Camera::V1_2::MOVING_IMAGE) {
275         MEDIA_INFO_LOG("HStreamCapture MOVING_IMAGE");
276         deferredVideoSwitch_ = 1;
277     } else {
278         MEDIA_INFO_LOG("HStreamCapture NONE");
279         deferredPhotoSwitch_ = 0;
280         deferredVideoSwitch_ = 0;
281     }
282     return CAMERA_OK;
283 }
284 
PrepareBurst(int32_t captureId)285 int32_t HStreamCapture::PrepareBurst(int32_t captureId)
286 {
287     MEDIA_INFO_LOG("HStreamCapture::PrepareBurst captureId:%{public}d", captureId);
288     isBursting_ = true;
289     std::lock_guard<std::mutex> lock(burstLock_);
290     curBurstKey_ = GenerateBurstUuid();
291     burstkeyMap_.emplace(captureId, curBurstKey_);
292     std::vector<std::string> imageList = {};
293     burstImagesMap_.emplace(captureId, imageList);
294     burstNumMap_.emplace(captureId, 0);
295     burstNum_ = 0;
296     return CAMERA_OK;
297 }
298 
ResetBurst()299 void HStreamCapture::ResetBurst()
300 {
301     MEDIA_INFO_LOG("HStreamCapture::ResetBurst");
302     curBurstKey_ = BURST_UUID_UNSET;
303     isBursting_ = false;
304 }
305 
ResetBurstKey(int32_t captureId)306 void HStreamCapture::ResetBurstKey(int32_t captureId)
307 {
308     if (burstkeyMap_.erase(captureId) > 0 &&
309         burstImagesMap_.erase(captureId) > 0 &&
310         burstNumMap_.erase(captureId) > 0) {
311         MEDIA_INFO_LOG("HStreamCapture::ResetBurstKey captureId:%{public}d", captureId);
312     } else {
313         MEDIA_DEBUG_LOG("HStreamCapture::ResetBurstKey captureId not found");
314     }
315 }
316 
GetBurstKey(int32_t captureId) const317 std::string HStreamCapture::GetBurstKey(int32_t captureId) const
318 {
319     MEDIA_DEBUG_LOG("HStreamCapture::GetBurstKey captureId:%{public}d", captureId);
320     std::string burstKey = BURST_UUID_UNSET;
321     std::lock_guard<std::mutex> lock(burstLock_);
322     auto iter = burstkeyMap_.find(captureId);
323     if (iter != burstkeyMap_.end()) {
324         burstKey = iter->second;
325         MEDIA_DEBUG_LOG("HStreamCapture::GetBurstKey %{public}s", burstKey.c_str());
326     } else {
327         MEDIA_DEBUG_LOG("HStreamCapture::GetBurstKey not found");
328     }
329     return burstKey;
330 }
331 
IsBurstCapture(int32_t captureId) const332 bool HStreamCapture::IsBurstCapture(int32_t captureId) const
333 {
334     MEDIA_DEBUG_LOG("HStreamCapture::captureId:%{public}d", captureId);
335     auto iter = burstkeyMap_.find(captureId);
336     return iter != burstkeyMap_.end();
337 }
338 
IsBurstCover(int32_t captureId) const339 bool HStreamCapture::IsBurstCover(int32_t captureId) const
340 {
341     MEDIA_DEBUG_LOG("HStreamCapture::IsBurstCover for captureId: %d", captureId);
342     std::lock_guard<std::mutex> lock(burstLock_);
343     auto iter = burstImagesMap_.find(captureId);
344     return (iter != burstImagesMap_.end()) ? (iter->second.size() == 1) : false;
345 }
346 
GetCurBurstSeq(int32_t captureId) const347 int32_t HStreamCapture::GetCurBurstSeq(int32_t captureId) const
348 {
349     MEDIA_DEBUG_LOG("HStreamCapture::GetCurBurstSeq for captureId: %d", captureId);
350     std::lock_guard<std::mutex> lock(burstLock_);
351     auto iter = burstImagesMap_.find(captureId);
352     CHECK_ERROR_RETURN_RET(iter != burstImagesMap_.end(), iter->second.size());
353     return -1;
354 }
355 
SetBurstImages(int32_t captureId,std::string imageId)356 void HStreamCapture::SetBurstImages(int32_t captureId, std::string imageId)
357 {
358     MEDIA_DEBUG_LOG("HStreamCapture::SetBurstImages captureId:%{public}d imageId:%{public}s",
359         captureId, imageId.c_str());
360     std::lock_guard<std::mutex> lock(burstLock_);
361     auto iter = burstImagesMap_.find(captureId);
362     if (iter != burstImagesMap_.end()) {
363         iter->second.emplace_back(imageId);
364         MEDIA_DEBUG_LOG("HStreamCapture::SetBurstImages success");
365     } else {
366         MEDIA_ERR_LOG("HStreamCapture::SetBurstImages error");
367     }
368 }
369 
CheckResetBurstKey(int32_t captureId)370 void HStreamCapture::CheckResetBurstKey(int32_t captureId)
371 {
372     MEDIA_DEBUG_LOG("HStreamCapture::CheckResetBurstKey captureId:%{public}d", captureId);
373     std::lock_guard<std::mutex> lock(burstLock_);
374     auto numIter = burstNumMap_.find(captureId);
375     auto imageIter = burstImagesMap_.find(captureId);
376     if (numIter != burstNumMap_.end() && imageIter != burstImagesMap_.end()) {
377         // LCOV_EXCL_START
378         int32_t burstSum = numIter->second;
379         size_t curBurstSum = imageIter->second.size();
380         MEDIA_DEBUG_LOG("CheckResetBurstKey: burstSum=%d, curBurstSum=%zu", burstSum, curBurstSum);
381         CHECK_EXECUTE(static_cast<size_t>(burstSum) == curBurstSum, ResetBurstKey(captureId));
382         // LCOV_EXCL_STOP
383     } else {
384         MEDIA_DEBUG_LOG("CheckResetBurstKey: captureId %d not found in one or both maps", captureId);
385     }
386 }
387 
388 
CheckBurstCapture(const std::shared_ptr<OHOS::Camera::CameraMetadata> & captureSettings,const int32_t & preparedCaptureId)389 int32_t HStreamCapture::CheckBurstCapture(const std::shared_ptr<OHOS::Camera::CameraMetadata>& captureSettings,
390                                           const int32_t &preparedCaptureId)
391 {
392     MEDIA_INFO_LOG("CheckBurstCapture start!");
393     camera_metadata_item_t item;
394     CHECK_ERROR_RETURN_RET_LOG(captureSettings == nullptr, CAMERA_INVALID_STATE, "captureSettings is nullptr");
395     int32_t result = OHOS::Camera::FindCameraMetadataItem(captureSettings->get(), OHOS_CONTROL_BURST_CAPTURE, &item);
396     if (result == CAM_META_SUCCESS && item.count > 0) {
397     // LCOV_EXCL_START
398         CameraBurstCaptureEnum burstState = static_cast<CameraBurstCaptureEnum>(item.data.u8[0]);
399         MEDIA_INFO_LOG("CheckBurstCapture get burstState:%{public}d", item.data.u8[0]);
400         if (burstState) {
401             std::string burstUuid = GetBurstKey(preparedCaptureId);
402             CHECK_ERROR_RETURN_RET_LOG(burstUuid != BURST_UUID_UNSET || isBursting_, CAMERA_INVALID_STATE,
403                 "CheckBurstCapture faild!");
404             PrepareBurst(preparedCaptureId);
405             MEDIA_INFO_LOG("CheckBurstCapture ready!");
406         }
407     }
408     // LCOV_EXCL_STOP
409     return CAM_META_SUCCESS;
410 }
411 
Insert(const int32_t & key,const std::shared_ptr<PhotoAssetIntf> & value)412 void ConcurrentMap::Insert(const int32_t& key, const std::shared_ptr<PhotoAssetIntf>& value)
413 {
414     std::lock_guard<std::mutex> lock(map_mutex_);
415     map_[key] = value;
416     step_[key] = 1;
417     cv_[key].notify_all();
418 }
419 
Get(const int32_t & key)420 std::shared_ptr<PhotoAssetIntf> ConcurrentMap::Get(const int32_t& key)
421 {
422     std::lock_guard<std::mutex> lock(map_mutex_);
423     return map_[key];
424 }
425 
GetCv(const int32_t & key)426 std::condition_variable& ConcurrentMap::GetCv(const int32_t& key)
427 {
428     std::lock_guard<std::mutex> lock(map_mutex_);
429     return cv_[key];
430 }
431 
GetMutex(const int32_t & key)432 std::mutex& ConcurrentMap::GetMutex(const int32_t& key)
433 {
434     std::lock_guard<std::mutex> lock(map_mutex_);
435     return mutexes_[key];
436 }
437 
ReadyToUnlock(const int32_t & key,const int32_t & step,const int32_t & mode)438 bool ConcurrentMap::ReadyToUnlock(const int32_t& key, const int32_t& step, const int32_t& mode)
439 {
440     std::lock_guard<std::mutex> lock(map_mutex_);
441     if (mode == static_cast<int32_t>(HDI::Camera::V1_3::OperationMode::CAPTURE) ||
442       mode == static_cast<int32_t>(HDI::Camera::V1_3::OperationMode::QUICK_SHOT_PHOTO) ||
443       mode == static_cast<int32_t>(HDI::Camera::V1_3::OperationMode::PORTRAIT)) {
444         return step_.count(key) > 0 && step_[key] == step;
445     } else {
446         return map_.count(key) > 0;
447     }
448 }
449 
450 // LCOV_EXCL_START
IncreaseCaptureStep(const int32_t & key)451 void ConcurrentMap::IncreaseCaptureStep(const int32_t& key)
452 {
453     std::lock_guard<std::mutex> lock(map_mutex_);
454     step_[key] = step_[key] + 1;
455     cv_[key].notify_all();
456 }
457 // LCOV_EXCL_STOP
458 
Erase(const int32_t & key)459 void ConcurrentMap::Erase(const int32_t& key)
460 {
461     std::lock_guard<std::mutex> lock(map_mutex_);
462     mutexes_.erase(key);
463     map_.erase(key);
464     cv_.erase(key);
465     step_.erase(key);
466 }
467 
Release()468 void ConcurrentMap::Release()
469 {
470     std::lock_guard<std::mutex> lock(map_mutex_);
471     map_.clear();
472     mutexes_.clear();
473     cv_.clear();
474     step_.clear();
475 }
476 
CreateMediaLibraryPhotoAssetProxy(int32_t captureId)477 int32_t HStreamCapture::CreateMediaLibraryPhotoAssetProxy(int32_t captureId)
478 {
479     CAMERA_SYNC_TRACE;
480     MEDIA_DEBUG_LOG("CreateMediaLibraryPhotoAssetProxy E captureId:%{public}d", captureId);
481     constexpr int32_t imageShotType = 0;
482     constexpr int32_t movingPhotoShotType = 2;
483     constexpr int32_t burstShotType = 3;
484     int32_t cameraShotType = imageShotType;
485     if (movingPhotoSwitch_) {
486         cameraShotType = movingPhotoShotType;
487     } else if (isBursting_) {
488         cameraShotType = burstShotType;
489     }
490     auto photoAssetProxy = PhotoAssetProxy::GetPhotoAssetProxy(cameraShotType, IPCSkeleton::GetCallingUid());
491     CHECK_ERROR_RETURN_RET_LOG(photoAssetProxy == nullptr, CAMERA_ALLOC_ERROR,
492         "HStreamCapture::CreateMediaLibraryPhotoAssetProxy get photoAssetProxy fail");
493     photoAssetProxy_.Insert(captureId, photoAssetProxy);
494     MEDIA_DEBUG_LOG("CreateMediaLibraryPhotoAssetProxy X captureId:%{public}d", captureId);
495     return CAMERA_OK;
496 }
497 
GetPhotoAssetInstance(int32_t captureId)498 std::shared_ptr<PhotoAssetIntf> HStreamCapture::GetPhotoAssetInstance(int32_t captureId)
499 {
500     CAMERA_SYNC_TRACE;
501     const int32_t getPhotoAssetStep = 2;
502     std::unique_lock<std::mutex> lock(photoAssetProxy_.GetMutex(captureId));
503     photoAssetProxy_.GetCv(captureId).wait(lock,
504         [&] { return photoAssetProxy_.ReadyToUnlock(captureId, getPhotoAssetStep, GetMode()); });
505     std::shared_ptr<PhotoAssetIntf> proxy = photoAssetProxy_.Get(captureId);
506     photoAssetProxy_.Erase(captureId);
507     return proxy;
508 }
509 
GetAddPhotoProxyEnabled()510 bool HStreamCapture::GetAddPhotoProxyEnabled()
511 {
512     return thumbnailSwitch_;
513 }
514 
AcquireBufferToPrepareProxy(int32_t captureId)515 int32_t HStreamCapture::AcquireBufferToPrepareProxy(int32_t captureId)
516 {
517     MEDIA_DEBUG_LOG("HStreamCapture::AcquireBufferToPrepareProxy start");
518     CameraReportDfxUtils::GetInstance()->SetFirstBufferEndInfo(captureId);
519     CameraReportDfxUtils::GetInstance()->SetPrepareProxyStartInfo(captureId);
520     MEDIA_DEBUG_LOG("HStreamCapture::AcquireBufferToPrepareProxy end");
521     return CAMERA_OK;
522 }
523 
Capture(const std::shared_ptr<OHOS::Camera::CameraMetadata> & captureSettings)524 int32_t HStreamCapture::Capture(const std::shared_ptr<OHOS::Camera::CameraMetadata>& captureSettings)
525 {
526     CAMERA_SYNC_TRACE;
527     MEDIA_INFO_LOG("HStreamCapture::Capture Entry, streamId:%{public}d", GetFwkStreamId());
528     auto streamOperator = GetStreamOperator();
529     CHECK_ERROR_RETURN_RET(streamOperator == nullptr, CAMERA_INVALID_STATE);
530     // LCOV_EXCL_START
531     CHECK_ERROR_RETURN_RET_LOG(isCaptureReady_ == false, CAMERA_OPERATION_NOT_ALLOWED,
532         "HStreamCapture::Capture failed due to capture not ready");
533     auto preparedCaptureId = GetPreparedCaptureId();
534     CHECK_ERROR_RETURN_RET_LOG(preparedCaptureId != CAPTURE_ID_UNSET, CAMERA_INVALID_STATE,
535         "HStreamCapture::Capture, Already started with captureID: %{public}d", preparedCaptureId);
536     int32_t ret = PrepareCaptureId();
537     preparedCaptureId = GetPreparedCaptureId();
538     CHECK_ERROR_RETURN_RET_LOG(ret != CAMERA_OK || preparedCaptureId == CAPTURE_ID_UNSET, ret,
539         "HStreamCapture::Capture Failed to allocate a captureId");
540     ret = CheckBurstCapture(captureSettings, preparedCaptureId);
541     CHECK_ERROR_RETURN_RET_LOG(ret != CAMERA_OK, ret, "HStreamCapture::Capture Failed with burst state error");
542 
543     CaptureDfxInfo captureDfxInfo;
544     captureDfxInfo.captureId = preparedCaptureId;
545     captureDfxInfo.isSystemApp = CheckSystemApp();
546     captureDfxInfo.bundleName = BmsAdapter::GetInstance()->GetBundleName(IPCSkeleton::GetCallingUid());
547     CameraReportDfxUtils::GetInstance()->SetFirstBufferStartInfo(captureDfxInfo);
548 
549     CaptureInfo captureInfoPhoto;
550     captureInfoPhoto.streamIds_ = { GetHdiStreamId() };
551     ProcessCaptureInfoPhoto(captureInfoPhoto, captureSettings, preparedCaptureId);
552 
553     auto callingTokenId = IPCSkeleton::GetCallingTokenID();
554     const std::string permissionName = "ohos.permission.CAMERA";
555     AddCameraPermissionUsedRecord(callingTokenId, permissionName);
556 
557     // report capture performance dfx
558     std::shared_ptr<OHOS::Camera::CameraMetadata> captureMetadataSetting_ = nullptr;
559     OHOS::Camera::MetadataUtils::ConvertVecToMetadata(captureInfoPhoto.captureSetting_, captureMetadataSetting_);
560     if (captureMetadataSetting_ == nullptr) {
561         captureMetadataSetting_ = std::make_shared<OHOS::Camera::CameraMetadata>(0, 0);
562     }
563     DfxCaptureInfo captureInfo;
564     captureInfo.captureId = preparedCaptureId;
565     captureInfo.caller = CameraReportUtils::GetCallerInfo();
566     CameraReportUtils::GetInstance().SetCapturePerfStartInfo(captureInfo);
567     MEDIA_INFO_LOG("HStreamCapture::Capture Starting photo capture with capture ID: %{public}d", preparedCaptureId);
568     HStreamCommon::PrintCaptureDebugLog(captureMetadataSetting_);
569     CamRetCode rc = (CamRetCode)(streamOperator->Capture(preparedCaptureId, captureInfoPhoto, isBursting_));
570     if (rc != HDI::Camera::V1_0::NO_ERROR) {
571         ResetCaptureId();
572         captureIdForConfirmCapture_ = CAPTURE_ID_UNSET;
573         MEDIA_ERR_LOG("HStreamCapture::Capture failed with error Code: %{public}d", rc);
574         CameraReportUtils::ReportCameraError(
575             "HStreamCapture::Capture", rc, true, CameraReportUtils::GetCallerInfo());
576         ret = HdiToServiceError(rc);
577     }
578     camera_metadata_item_t item;
579     camera_position_enum_t cameraPosition = OHOS_CAMERA_POSITION_FRONT;
580     {
581         std::lock_guard<std::mutex> lock(cameraAbilityLock_);
582         CHECK_ERROR_RETURN_RET_LOG(cameraAbility_ == nullptr, CAMERA_INVALID_STATE,
583             "HStreamCapture::cameraAbility_ is null");
584         int32_t result = OHOS::Camera::FindCameraMetadataItem(cameraAbility_->get(), OHOS_ABILITY_CAMERA_POSITION,
585                                                               &item);
586         if (result == CAM_META_SUCCESS && item.count > 0) {
587             cameraPosition = static_cast<camera_position_enum_t>(item.data.u8[0]);
588         }
589     }
590 
591     bool isNightMode = (GetMode() == static_cast<int32_t>(HDI::Camera::V1_3::OperationMode::NIGHT));
592     CHECK_ERROR_RETURN_RET(isNightMode && cameraPosition == OHOS_CAMERA_POSITION_BACK, ret);
593     ResetCaptureId();
594 
595     uint32_t major;
596     uint32_t minor;
597     streamOperator->GetVersion(major, minor);
598     MEDIA_INFO_LOG("streamOperator GetVersion major:%{public}d, minor:%{public}d", major, minor);
599     // intercept when streamOperatorCallback support onCaptureReady
600     if (major >= HDI_VERSION_1 && minor >= HDI_VERSION_2 && !isBursting_) {
601         MEDIA_INFO_LOG("HStreamCapture::Capture set capture not ready");
602         isCaptureReady_ = false;
603     }
604     if (!isBursting_) {
605         MEDIA_DEBUG_LOG("HStreamCapture::Capture CreateMediaLibraryPhotoAssetProxy E");
606         CHECK_ERROR_PRINT_LOG(CreateMediaLibraryPhotoAssetProxy(preparedCaptureId) != CAMERA_OK,
607             "HStreamCapture::Capture Failed with CreateMediaLibraryPhotoAssetProxy");
608         MEDIA_DEBUG_LOG("HStreamCapture::Capture CreateMediaLibraryPhotoAssetProxy X");
609     }
610     return ret;
611     // LCOV_EXCL_STOP
612 }
613 
ProcessCaptureInfoPhoto(CaptureInfo & captureInfoPhoto,const std::shared_ptr<OHOS::Camera::CameraMetadata> & captureSettings,int32_t captureId)614 void HStreamCapture::ProcessCaptureInfoPhoto(CaptureInfo& captureInfoPhoto,
615     const std::shared_ptr<OHOS::Camera::CameraMetadata>& captureSettings, int32_t captureId)
616 {
617     if (!OHOS::Camera::GetCameraMetadataItemCount(captureSettings->get())) {
618         std::lock_guard<std::mutex> lock(cameraAbilityLock_);
619         OHOS::Camera::MetadataUtils::ConvertMetadataToVec(cameraAbility_, captureInfoPhoto.captureSetting_);
620     } else {
621         OHOS::Camera::MetadataUtils::ConvertMetadataToVec(captureSettings, captureInfoPhoto.captureSetting_);
622     }
623     captureInfoPhoto.enableShutterCallback_ = true;
624     std::shared_ptr<OHOS::Camera::CameraMetadata> captureMetadataSetting_ = nullptr;
625     OHOS::Camera::MetadataUtils::ConvertVecToMetadata(captureInfoPhoto.captureSetting_, captureMetadataSetting_);
626     if (captureMetadataSetting_ != nullptr) {
627         // convert rotation with application set rotation
628         SetRotation(captureMetadataSetting_, captureId);
629 
630         // update settings
631         std::vector<uint8_t> finalSetting;
632         OHOS::Camera::MetadataUtils::ConvertMetadataToVec(captureMetadataSetting_, finalSetting);
633         captureInfoPhoto.captureSetting_ = finalSetting;
634     }
635 }
636 
SetRotation(const std::shared_ptr<OHOS::Camera::CameraMetadata> & captureMetadataSetting_,int32_t captureId)637 void HStreamCapture::SetRotation(const std::shared_ptr<OHOS::Camera::CameraMetadata> &captureMetadataSetting_,
638     int32_t captureId)
639 {
640     // set orientation for capture
641     // sensor orientation, counter-clockwise rotation
642     int32_t sensorOrientation = 0;
643     int result;
644     camera_metadata_item_t item;
645     camera_position_enum_t cameraPosition = OHOS_CAMERA_POSITION_BACK;
646     {
647         std::lock_guard<std::mutex> lock(cameraAbilityLock_);
648         CHECK_ERROR_RETURN(cameraAbility_ == nullptr);
649         // LCOV_EXCL_START
650         result = OHOS::Camera::FindCameraMetadataItem(cameraAbility_->get(), OHOS_SENSOR_ORIENTATION, &item);
651         if (result == CAM_META_SUCCESS && item.count > 0) {
652             sensorOrientation = item.data.i32[0];
653         }
654         MEDIA_INFO_LOG("set rotation sensor orientation %{public}d", sensorOrientation);
655 
656         result = OHOS::Camera::FindCameraMetadataItem(cameraAbility_->get(), OHOS_ABILITY_CAMERA_POSITION, &item);
657         if (result == CAM_META_SUCCESS && item.count > 0) {
658             cameraPosition = static_cast<camera_position_enum_t>(item.data.u8[0]);
659         }
660         MEDIA_INFO_LOG("set rotation camera position %{public}d", cameraPosition);
661     }
662 
663     // rotation from application
664     int32_t rotationValue = 0;
665     int32_t rotation = 0;
666     result = OHOS::Camera::FindCameraMetadataItem(captureMetadataSetting_->get(), OHOS_JPEG_ORIENTATION, &item);
667     if (result == CAM_META_SUCCESS && item.count > 0) {
668         rotationValue = item.data.i32[0];
669     }
670     MEDIA_INFO_LOG("set rotation app rotationValue %{public}d", rotationValue); // 0 270 270+270=180
671     // real rotation
672     if (enableCameraPhotoRotation_) {
673         rotation = rotationValue;
674     } else {
675         rotation = sensorOrientation + rotationValue;
676         if (rotation >= CAPTURE_ROTATE_360) {
677             rotation = rotation - CAPTURE_ROTATE_360;
678         }
679     }
680     auto hStreamOperator = hStreamOperator_.promote();
681     if (hStreamOperator) {
682         hStreamOperator->SetSensorRotation(rotation, sensorOrientation, cameraPosition);
683     }
684     {
685         uint8_t connectType = 0;
686         std::lock_guard<std::mutex> lock(cameraAbilityLock_);
687         CHECK_ERROR_RETURN(cameraAbility_ == nullptr);
688         int ret = OHOS::Camera::FindCameraMetadataItem(
689             cameraAbility_->get(), OHOS_ABILITY_CAMERA_CONNECTION_TYPE, &item);
690         if (ret == CAM_META_SUCCESS && item.count > 0) {
691             connectType = item.data.u8[0];
692         }
693         if (connectType == OHOS_CAMERA_CONNECTION_TYPE_REMOTE) {
694             rotation = rotationValue;
695         }
696         MEDIA_INFO_LOG("set rotation camera real rotation %{public}d", rotation);
697     }
698 
699     bool status = false;
700     if (result == CAM_META_ITEM_NOT_FOUND) {
701         status = captureMetadataSetting_->addEntry(OHOS_JPEG_ORIENTATION, &rotation, 1);
702     } else if (result == CAM_META_SUCCESS) {
703         status = captureMetadataSetting_->updateEntry(OHOS_JPEG_ORIENTATION, &rotation, 1);
704     }
705     rotationMap_.EnsureInsert(captureId, rotation);
706     result = OHOS::Camera::FindCameraMetadataItem(captureMetadataSetting_->get(), OHOS_JPEG_ORIENTATION, &item);
707     CHECK_ERROR_PRINT_LOG(result != CAM_META_SUCCESS, "set rotation Failed to find OHOS_JPEG_ORIENTATION tag");
708     CHECK_ERROR_PRINT_LOG(!status, "set rotation Failed to set Rotation");
709     // LCOV_EXCL_STOP
710 }
711 
CancelCapture()712 int32_t HStreamCapture::CancelCapture()
713 {
714     CAMERA_SYNC_TRACE;
715     // Cancel capture dummy till continuous/burst mode is supported
716     StopStream();
717     return CAMERA_OK;
718 }
719 
SetMode(int32_t modeName)720 void HStreamCapture::SetMode(int32_t modeName)
721 {
722     modeName_ = modeName;
723     MEDIA_DEBUG_LOG("HStreamCapture SetMode modeName = %{public}d", modeName);
724 }
725 
GetMode()726 int32_t HStreamCapture::GetMode()
727 {
728     MEDIA_INFO_LOG("HStreamCapture GetMode modeName = %{public}d", modeName_);
729     return modeName_;
730 }
731 
ConfirmCapture()732 int32_t HStreamCapture::ConfirmCapture()
733 {
734     CAMERA_SYNC_TRACE;
735     auto streamOperator = GetStreamOperator();
736     CHECK_ERROR_RETURN_RET(streamOperator == nullptr, CAMERA_INVALID_STATE);
737     // LCOV_EXCL_START
738     int32_t ret = 0;
739 
740     // end burst capture
741     if (isBursting_) {
742         MEDIA_INFO_LOG("HStreamCapture::ConfirmCapture when burst capture");
743         std::vector<uint8_t> settingVector;
744         std::shared_ptr<OHOS::Camera::CameraMetadata> burstCaptureSettings = nullptr;
745         OHOS::Camera::MetadataUtils::ConvertMetadataToVec(cameraAbility_, settingVector);
746         OHOS::Camera::MetadataUtils::ConvertVecToMetadata(settingVector, burstCaptureSettings);
747         if (burstCaptureSettings == nullptr) {
748             burstCaptureSettings = std::make_shared<OHOS::Camera::CameraMetadata>(0, 0);
749         }
750         EndBurstCapture(burstCaptureSettings);
751         ret = Capture(burstCaptureSettings);
752         CHECK_ERROR_PRINT_LOG(ret != CAMERA_OK, "HStreamCapture::ConfirmCapture end burst faild!");
753         return ret;
754     }
755 
756     auto preparedCaptureId = captureIdForConfirmCapture_;
757     MEDIA_INFO_LOG("HStreamCapture::ConfirmCapture with capture ID: %{public}d", preparedCaptureId);
758     sptr<HDI::Camera::V1_2::IStreamOperator> streamOperatorV1_2 =
759         OHOS::HDI::Camera::V1_2::IStreamOperator::CastFrom(streamOperator);
760     CHECK_ERROR_RETURN_RET_LOG(streamOperatorV1_2 == nullptr, CAMERA_UNKNOWN_ERROR,
761         "HStreamCapture::ConfirmCapture streamOperatorV1_2 castFrom failed!");
762     OHOS::HDI::Camera::V1_2::CamRetCode rc =
763         (HDI::Camera::V1_2::CamRetCode)(streamOperatorV1_2->ConfirmCapture(preparedCaptureId));
764     if (rc != HDI::Camera::V1_2::NO_ERROR) {
765         MEDIA_ERR_LOG("HStreamCapture::ConfirmCapture failed with error Code: %{public}d", rc);
766         ret = HdiToServiceErrorV1_2(rc);
767     }
768     ResetCaptureId();
769     captureIdForConfirmCapture_ = CAPTURE_ID_UNSET;
770     return ret;
771     // LCOV_EXCL_STOP
772 }
773 
EndBurstCapture(const std::shared_ptr<OHOS::Camera::CameraMetadata> & captureMetadataSetting)774 void HStreamCapture::EndBurstCapture(const std::shared_ptr<OHOS::Camera::CameraMetadata>& captureMetadataSetting)
775 {
776     CHECK_ERROR_RETURN(captureMetadataSetting == nullptr);
777     MEDIA_INFO_LOG("HStreamCapture::EndBurstCapture");
778     camera_metadata_item_t item;
779     bool status = false;
780     int result = OHOS::Camera::FindCameraMetadataItem(captureMetadataSetting->get(), OHOS_CONTROL_BURST_CAPTURE, &item);
781     uint8_t burstState = 0;
782     if (result == CAM_META_ITEM_NOT_FOUND) {
783         status = captureMetadataSetting->addEntry(OHOS_CONTROL_BURST_CAPTURE, &burstState, 1);
784     } else if (result == CAM_META_SUCCESS) {
785         status = captureMetadataSetting->updateEntry(OHOS_CONTROL_BURST_CAPTURE, &burstState, 1);
786     }
787 
788     CHECK_ERROR_PRINT_LOG(!status, "HStreamCapture::EndBurstCapture Failed");
789 }
790 
Release()791 int32_t HStreamCapture::Release()
792 {
793     return ReleaseStream(false);
794 }
795 
ReleaseStream(bool isDelay)796 int32_t HStreamCapture::ReleaseStream(bool isDelay)
797 {
798     {
799         std::lock_guard<std::mutex> lock(callbackLock_);
800         streamCaptureCallback_ = nullptr;
801     }
802     int32_t errorCode = HStreamCommon::ReleaseStream(isDelay);
803     auto hStreamOperatorSptr_ = hStreamOperator_.promote();
804     if (hStreamOperatorSptr_ && mSwitchToOfflinePhoto_) {
805         hStreamOperatorSptr_->Release();
806     }
807     std::lock_guard<std::mutex> lock(streamOperatorLock_);
808     if (streamOperatorOffline_ != nullptr) {
809         streamOperatorOffline_ = nullptr;
810     }
811     mSwitchToOfflinePhoto_ = false;
812     return errorCode;
813 }
814 
SetCallback(sptr<IStreamCaptureCallback> & callback)815 int32_t HStreamCapture::SetCallback(sptr<IStreamCaptureCallback> &callback)
816 {
817     CHECK_ERROR_RETURN_RET_LOG(callback == nullptr, CAMERA_INVALID_ARG, "HStreamCapture::SetCallback input is null");
818     std::lock_guard<std::mutex> lock(callbackLock_);
819     streamCaptureCallback_ = callback;
820     return CAMERA_OK;
821 }
822 
823 // LCOV_EXCL_START
UnSetCallback()824 int32_t HStreamCapture::UnSetCallback()
825 {
826     std::lock_guard<std::mutex> lock(callbackLock_);
827     streamCaptureCallback_ = nullptr;
828     return CAMERA_OK;
829 }
830 // LCOV_EXCL_STOP
831 
OnCaptureStarted(int32_t captureId)832 int32_t HStreamCapture::OnCaptureStarted(int32_t captureId)
833 {
834     CAMERA_SYNC_TRACE;
835     std::lock_guard<std::mutex> lock(callbackLock_);
836     CHECK_EXECUTE(streamCaptureCallback_ != nullptr, streamCaptureCallback_->OnCaptureStarted(captureId));
837     return CAMERA_OK;
838 }
839 
OnCaptureStarted(int32_t captureId,uint32_t exposureTime)840 int32_t HStreamCapture::OnCaptureStarted(int32_t captureId, uint32_t exposureTime)
841 {
842     CAMERA_SYNC_TRACE;
843     std::lock_guard<std::mutex> lock(callbackLock_);
844     CHECK_EXECUTE(streamCaptureCallback_ != nullptr,
845         streamCaptureCallback_->OnCaptureStarted(captureId, exposureTime));
846     return CAMERA_OK;
847 }
848 
OnCaptureEnded(int32_t captureId,int32_t frameCount)849 int32_t HStreamCapture::OnCaptureEnded(int32_t captureId, int32_t frameCount)
850 {
851     CAMERA_SYNC_TRACE;
852     std::lock_guard<std::mutex> lock(callbackLock_);
853     CHECK_EXECUTE(streamCaptureCallback_ != nullptr, streamCaptureCallback_->OnCaptureEnded(captureId, frameCount));
854     MEDIA_INFO_LOG("HStreamCapture::Capture, notify OnCaptureEnded with capture ID: %{public}d", captureId);
855     int32_t offlineOutputCnt = mSwitchToOfflinePhoto_ ?
856         HStreamOperatorManager::GetInstance()->GetOfflineOutputSize() : 0;
857     CameraReportUtils::GetInstance().SetCapturePerfEndInfo(captureId, mSwitchToOfflinePhoto_, offlineOutputCnt);
858     auto preparedCaptureId = GetPreparedCaptureId();
859     if (preparedCaptureId != CAPTURE_ID_UNSET) {
860         MEDIA_INFO_LOG("HStreamCapture::OnCaptureEnded capturId = %{public}d already used, need release",
861                        preparedCaptureId);
862         ResetCaptureId();
863         captureIdForConfirmCapture_ = CAPTURE_ID_UNSET;
864     }
865     return CAMERA_OK;
866 }
867 
OnCaptureError(int32_t captureId,int32_t errorCode)868 int32_t HStreamCapture::OnCaptureError(int32_t captureId, int32_t errorCode)
869 {
870     std::lock_guard<std::mutex> lock(callbackLock_);
871     if (streamCaptureCallback_ != nullptr) {
872         // LCOV_EXCL_START
873         int32_t captureErrorCode;
874         if (errorCode == BUFFER_LOST) {
875             captureErrorCode = CAMERA_STREAM_BUFFER_LOST;
876         } else {
877             captureErrorCode = CAMERA_UNKNOWN_ERROR;
878         }
879         CAMERA_SYSEVENT_FAULT(CreateMsg("Photo OnCaptureError! captureId:%d & "
880                                         "errorCode:%{public}d", captureId, captureErrorCode));
881         streamCaptureCallback_->OnCaptureError(captureId, captureErrorCode);
882         // LCOV_EXCL_STOP
883     }
884     auto preparedCaptureId = GetPreparedCaptureId();
885     if (preparedCaptureId != CAPTURE_ID_UNSET) {
886         // LCOV_EXCL_START
887         MEDIA_INFO_LOG("HStreamCapture::OnCaptureError capturId = %{public}d already used, need release",
888                        preparedCaptureId);
889         ResetCaptureId();
890         captureIdForConfirmCapture_ = CAPTURE_ID_UNSET;
891         // LCOV_EXCL_STOP
892     }
893     return CAMERA_OK;
894 }
895 
OnFrameShutter(int32_t captureId,uint64_t timestamp)896 int32_t HStreamCapture::OnFrameShutter(int32_t captureId, uint64_t timestamp)
897 {
898     CAMERA_SYNC_TRACE;
899     std::lock_guard<std::mutex> lock(callbackLock_);
900     CHECK_EXECUTE(streamCaptureCallback_ != nullptr,
901         streamCaptureCallback_->OnFrameShutter(captureId, timestamp));
902     return CAMERA_OK;
903 }
904 
OnFrameShutterEnd(int32_t captureId,uint64_t timestamp)905 int32_t HStreamCapture::OnFrameShutterEnd(int32_t captureId, uint64_t timestamp)
906 {
907     CAMERA_SYNC_TRACE;
908     std::lock_guard<std::mutex> lock(callbackLock_);
909     CHECK_EXECUTE(streamCaptureCallback_ != nullptr,
910         streamCaptureCallback_->OnFrameShutterEnd(captureId, timestamp));
911     if (isBursting_) {
912         burstNum_++;
913         MEDIA_DEBUG_LOG("HStreamCapture::OnFrameShutterEnd burstNum:%{public}d", burstNum_);
914     }
915     return CAMERA_OK;
916 }
917 
918 
OnCaptureReady(int32_t captureId,uint64_t timestamp)919 int32_t HStreamCapture::OnCaptureReady(int32_t captureId, uint64_t timestamp)
920 {
921     CAMERA_SYNC_TRACE;
922     std::lock_guard<std::mutex> lock(callbackLock_);
923     MEDIA_INFO_LOG("HStreamCapture::Capture, notify OnCaptureReady with capture ID: %{public}d", captureId);
924     isCaptureReady_ = true;
925     CHECK_EXECUTE(streamCaptureCallback_ != nullptr,
926         streamCaptureCallback_->OnCaptureReady(captureId, timestamp));
927     std::lock_guard<std::mutex> burstLock(burstLock_);
928     if (IsBurstCapture(captureId)) {
929         burstNumMap_[captureId] = burstNum_;
930         ResetBurst();
931     }
932     return CAMERA_OK;
933 }
934 
EnableOfflinePhoto(bool isEnable)935 int32_t HStreamCapture::EnableOfflinePhoto(bool isEnable)
936 {
937     mEnableOfflinePhoto_ = isEnable;
938     return CAMERA_OK;
939 }
940 
IsHasEnableOfflinePhoto()941 bool HStreamCapture::IsHasEnableOfflinePhoto()
942 {
943     return mEnableOfflinePhoto_;
944 }
945 
SwitchToOffline()946 void HStreamCapture::SwitchToOffline()
947 {
948     mSwitchToOfflinePhoto_ = true;
949     streamOperatorOffline_ = GetStreamOperator();
950 }
951 
IsHasSwitchToOffline()952 bool HStreamCapture::IsHasSwitchToOffline()
953 {
954     return mSwitchToOfflinePhoto_;
955 }
956 
DumpStreamInfo(CameraInfoDumper & infoDumper)957 void HStreamCapture::DumpStreamInfo(CameraInfoDumper& infoDumper)
958 {
959     infoDumper.Title("capture stream");
960     infoDumper.Msg("ThumbnailSwitch:[" + std::to_string(thumbnailSwitch_) + "]");
961     infoDumper.Msg("RawDeliverSwitch:[" + std::to_string(rawDeliverySwitch_) + "]");
962     if (thumbnailBufferQueue_) {
963         infoDumper.Msg(
964             "ThumbnailBuffer producer Id:[" + std::to_string(thumbnailBufferQueue_->producer_->GetUniqueId()) + "]");
965     }
966     HStreamCommon::DumpStreamInfo(infoDumper);
967 }
968 
OperatePermissionCheck(uint32_t interfaceCode)969 int32_t HStreamCapture::OperatePermissionCheck(uint32_t interfaceCode)
970 {
971     switch (static_cast<StreamCaptureInterfaceCode>(interfaceCode)) {
972         case CAMERA_STREAM_CAPTURE_START: {
973             auto callerToken = IPCSkeleton::GetCallingTokenID();
974             CHECK_ERROR_RETURN_RET_LOG(callerToken_ != callerToken, CAMERA_OPERATION_NOT_ALLOWED,
975                 "HStreamCapture::OperatePermissionCheck fail, callerToken_ is : %{public}d, now token "
976                 "is %{public}d", callerToken_, callerToken);
977             break;
978         }
979         default:
980             break;
981     }
982     return CAMERA_OK;
983 }
984 
IsDeferredPhotoEnabled()985 int32_t HStreamCapture::IsDeferredPhotoEnabled()
986 {
987     MEDIA_INFO_LOG("HStreamCapture IsDeferredPhotoEnabled  deferredPhotoSwitch_: %{public}d", deferredPhotoSwitch_);
988     CHECK_ERROR_RETURN_RET(deferredPhotoSwitch_ == 1, 1);
989     MEDIA_INFO_LOG("HStreamCapture IsDeferredPhotoEnabled return 0");
990     return 0;
991 }
992 
IsDeferredVideoEnabled()993 int32_t HStreamCapture::IsDeferredVideoEnabled()
994 {
995     MEDIA_INFO_LOG("HStreamCapture IsDeferredVideoEnabled  deferredVideoSwitch_: %{public}d", deferredVideoSwitch_);
996     CHECK_ERROR_RETURN_RET(deferredVideoSwitch_ == 1, 1);
997     return 0;
998 }
999 
GetMovingPhotoVideoCodecType()1000 int32_t HStreamCapture::GetMovingPhotoVideoCodecType()
1001 {
1002     MEDIA_INFO_LOG("HStreamCapture GetMovingPhotoVideoCodecType videoCodecType_: %{public}d", videoCodecType_);
1003     return videoCodecType_;
1004 }
1005 
SetMovingPhotoVideoCodecType(int32_t videoCodecType)1006 int32_t HStreamCapture::SetMovingPhotoVideoCodecType(int32_t videoCodecType)
1007 {
1008     MEDIA_INFO_LOG("HStreamCapture SetMovingPhotoVideoCodecType videoCodecType_: %{public}d", videoCodecType);
1009     videoCodecType_ = videoCodecType;
1010     return 0;
1011 }
1012 
SetCameraPhotoRotation(bool isEnable)1013 int32_t HStreamCapture::SetCameraPhotoRotation(bool isEnable)
1014 {
1015     enableCameraPhotoRotation_ = isEnable;
1016     return 0;
1017 }
1018 
SetCameraPhotoProxyInfo(sptr<CameraServerPhotoProxy> cameraPhotoProxy)1019 void HStreamCapture::SetCameraPhotoProxyInfo(sptr<CameraServerPhotoProxy> cameraPhotoProxy)
1020 {
1021     MEDIA_INFO_LOG("SetCameraPhotoProxyInfo get captureStream");
1022     cameraPhotoProxy->SetDisplayName(CreateDisplayName());
1023     cameraPhotoProxy->SetShootingMode(GetMode());
1024     MEDIA_INFO_LOG("SetCameraPhotoProxyInfo quality:%{public}d, format:%{public}d",
1025         cameraPhotoProxy->GetPhotoQuality(), cameraPhotoProxy->GetFormat());
1026 }
1027 
1028 // LCOV_EXCL_START
UpdateMediaLibraryPhotoAssetProxy(sptr<CameraPhotoProxy> photoProxy)1029 int32_t HStreamCapture::UpdateMediaLibraryPhotoAssetProxy(sptr<CameraPhotoProxy> photoProxy)
1030 {
1031     CAMERA_SYNC_TRACE;
1032     if (isBursting_ || (GetMode() == static_cast<int32_t>(HDI::Camera::V1_3::OperationMode::PROFESSIONAL_PHOTO))) {
1033         return CAMERA_UNSUPPORTED;
1034     }
1035     {
1036         const int32_t updateMediaLibraryStep = 1;
1037         std::unique_lock<std::mutex> lock(photoAssetProxy_.GetMutex(photoProxy->captureId_));
1038         photoAssetProxy_.GetCv(photoProxy->captureId_).wait(lock,
1039             [&] { return photoAssetProxy_.ReadyToUnlock(photoProxy->captureId_, updateMediaLibraryStep, GetMode()); });
1040         std::shared_ptr<PhotoAssetIntf> photoAssetProxy = photoAssetProxy_.Get(photoProxy->captureId_);
1041         CHECK_ERROR_RETURN_RET_LOG(photoAssetProxy == nullptr, CAMERA_UNKNOWN_ERROR,
1042             "HStreamCapture UpdateMediaLibraryPhotoAssetProxy failed");
1043         MEDIA_DEBUG_LOG("HStreamCapture UpdateMediaLibraryPhotoAssetProxy E captureId(%{public}d)",
1044             photoProxy->captureId_);
1045         MessageParcel data;
1046         photoProxy->WriteToParcel(data);
1047         photoProxy->CameraFreeBufferHandle();
1048         sptr<CameraServerPhotoProxy> cameraPhotoProxy = new CameraServerPhotoProxy();
1049         cameraPhotoProxy->ReadFromParcel(data);
1050         SetCameraPhotoProxyInfo(cameraPhotoProxy);
1051         MEDIA_DEBUG_LOG("HStreamCapture AddPhotoProxy E");
1052         photoAssetProxy->AddPhotoProxy(cameraPhotoProxy);
1053         MEDIA_DEBUG_LOG("HStreamCapture AddPhotoProxy X");
1054     }
1055     photoAssetProxy_.IncreaseCaptureStep(photoProxy->captureId_);
1056     MEDIA_DEBUG_LOG("HStreamCapture UpdateMediaLibraryPhotoAssetProxy X captureId(%{public}d)", photoProxy->captureId_);
1057     return CAMERA_OK;
1058 }
1059 // LCOV_EXCL_STOP
1060 
SetStreamOperator(wptr<HStreamOperator> hStreamOperator)1061 void HStreamCapture::SetStreamOperator(wptr<HStreamOperator> hStreamOperator)
1062 {
1063     hStreamOperator_ = hStreamOperator;
1064 }
1065 
1066 // LCOV_EXCL_START
CreateMediaLibrary(std::shared_ptr<PictureIntf> picture,sptr<CameraPhotoProxy> & photoProxy,std::string & uri,int32_t & cameraShotType,std::string & burstKey,int64_t timestamp)1067 int32_t HStreamCapture::CreateMediaLibrary(std::shared_ptr<PictureIntf> picture, sptr<CameraPhotoProxy>& photoProxy,
1068     std::string& uri, int32_t& cameraShotType, std::string& burstKey, int64_t timestamp)
1069 {
1070     auto hStreamOperatorSptr_ = hStreamOperator_.promote();
1071     if (hStreamOperatorSptr_) {
1072         hStreamOperatorSptr_->CreateMediaLibrary(picture, photoProxy,
1073             uri, cameraShotType, burstKey, timestamp);
1074     }
1075     return CAMERA_OK;
1076 }
1077 
CreateMediaLibrary(sptr<CameraPhotoProxy> & photoProxy,std::string & uri,int32_t & cameraShotType,std::string & burstKey,int64_t timestamp)1078 int32_t HStreamCapture::CreateMediaLibrary(sptr<CameraPhotoProxy>& photoProxy, std::string& uri,
1079     int32_t& cameraShotType, std::string& burstKey, int64_t timestamp)
1080 {
1081     auto hStreamOperatorSptr_ = hStreamOperator_.promote();
1082     if (hStreamOperatorSptr_) {
1083         hStreamOperatorSptr_->CreateMediaLibrary(photoProxy, uri, cameraShotType, burstKey, timestamp);
1084     }
1085     return CAMERA_OK;
1086 }
1087 // LCOV_EXCL_STOP
1088 } // namespace CameraStandard
1089 } // namespace OHOS
1090