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 <mutex>
19 #include <uuid.h>
20
21 #include "camera_log.h"
22 #include "camera_service_ipc_interface_code.h"
23 #include "camera_util.h"
24 #include "hstream_common.h"
25 #include "ipc_skeleton.h"
26 #include "metadata_utils.h"
27 #include "camera_report_uitls.h"
28
29 namespace OHOS {
30 namespace CameraStandard {
31 using namespace OHOS::HDI::Camera::V1_0;
32 static const int32_t CAPTURE_ROTATE_360 = 360;
33 static const std::string BURST_UUID_BEGIN = "";
34 static std::string g_currentBurstUuid = BURST_UUID_BEGIN;
35
GenerateBurstUuid()36 static std::string GenerateBurstUuid()
37 {
38 MEDIA_INFO_LOG("HStreamCapture::GenerateBurstUuid");
39 uuid_t uuid;
40 char str[37] = {}; // UUIDs are 36 characters plus the null terminator
41 uuid_generate(uuid);
42 uuid_unparse(uuid, str); // Convert the UUID to a string
43 std::string burstUuid(str);
44 g_currentBurstUuid = burstUuid;
45 return burstUuid;
46 }
47
HStreamCapture(sptr<OHOS::IBufferProducer> producer, int32_t format, int32_t width, int32_t height)48 HStreamCapture::HStreamCapture(sptr<OHOS::IBufferProducer> producer, int32_t format, int32_t width, int32_t height)
49 : HStreamCommon(StreamType::CAPTURE, producer, format, width, height)
50 {
51 MEDIA_INFO_LOG(
52 "HStreamCapture::HStreamCapture construct, format:%{public}d size:%{public}dx%{public}d streamId:%{public}d",
53 format, width, height, GetFwkStreamId());
54 thumbnailSwitch_ = 0;
55 rawDeliverySwitch_ = 0;
56 modeName_ = 0;
57 deferredPhotoSwitch_ = 0;
58 deferredVideoSwitch_ = 0;
59 burstNum_ = 0;
60 }
61
~HStreamCapture()62 HStreamCapture::~HStreamCapture()
63 {
64 rotationMap_.Clear();
65 MEDIA_INFO_LOG(
66 "HStreamCapture::~HStreamCapture deconstruct, format:%{public}d size:%{public}dx%{public}d streamId:%{public}d",
67 format_, width_, height_, GetFwkStreamId());
68 }
69
LinkInput(sptr<HDI::Camera::V1_0::IStreamOperator> streamOperator, std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility)70 int32_t HStreamCapture::LinkInput(sptr<HDI::Camera::V1_0::IStreamOperator> streamOperator,
71 std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility)
72 {
73 MEDIA_INFO_LOG("HStreamCapture::LinkInput streamId:%{public}d", GetFwkStreamId());
74 return HStreamCommon::LinkInput(streamOperator, cameraAbility);
75 }
76
FullfillPictureExtendStreamInfos(StreamInfo_V1_1 &streamInfo, int32_t format)77 void HStreamCapture::FullfillPictureExtendStreamInfos(StreamInfo_V1_1 &streamInfo, int32_t format)
78 {
79 HDI::Camera::V1_1::ExtendedStreamInfo gainmapExtendedStreamInfo = {
80 .type = static_cast<HDI::Camera::V1_1::ExtendedStreamInfoType>(HDI::Camera::V1_3::EXTENDED_STREAM_INFO_GAINMAP),
81 .width = width_,
82 .height = height_,
83 .format = format,
84 .bufferQueue = gainmapBufferQueue_,
85 };
86 HDI::Camera::V1_1::ExtendedStreamInfo deepExtendedStreamInfo = {
87 .type = static_cast<HDI::Camera::V1_1::ExtendedStreamInfoType>(HDI::Camera::V1_3::EXTENDED_STREAM_INFO_DEPTH),
88 .width = width_,
89 .height = height_,
90 .format = format,
91 .bufferQueue = deepBufferQueue_,
92 };
93 HDI::Camera::V1_1::ExtendedStreamInfo exifExtendedStreamInfo = {
94 .type = static_cast<HDI::Camera::V1_1::ExtendedStreamInfoType>(HDI::Camera::V1_3::EXTENDED_STREAM_INFO_EXIF),
95 .width = width_,
96 .height = height_,
97 .format = format,
98 .bufferQueue = exifBufferQueue_,
99 };
100 HDI::Camera::V1_1::ExtendedStreamInfo debugExtendedStreamInfo = {
101 .type =
102 static_cast<HDI::Camera::V1_1::ExtendedStreamInfoType>(HDI::Camera::V1_3::EXTENDED_STREAM_INFO_MAKER_INFO),
103 .width = width_,
104 .height = height_,
105 .format = format,
106 .bufferQueue = debugBufferQueue_,
107 };
108 std::vector<HDI::Camera::V1_1::ExtendedStreamInfo> extendedStreams = { gainmapExtendedStreamInfo,
109 deepExtendedStreamInfo, exifExtendedStreamInfo, debugExtendedStreamInfo };
110 streamInfo.extendedStreamInfos.insert(streamInfo.extendedStreamInfos.end(),
111 extendedStreams.begin(), extendedStreams.end());
112 }
113
SetStreamInfo(StreamInfo_V1_1 &streamInfo)114 void HStreamCapture::SetStreamInfo(StreamInfo_V1_1 &streamInfo)
115 {
116 HStreamCommon::SetStreamInfo(streamInfo);
117 streamInfo.v1_0.intent_ = STILL_CAPTURE;
118 if (format_ == OHOS_CAMERA_FORMAT_HEIC) {
119 streamInfo.v1_0.encodeType_ =
120 static_cast<HDI::Camera::V1_0::EncodeType>(HDI::Camera::V1_3::ENCODE_TYPE_HEIC);
121 streamInfo.v1_0.format_ = GRAPHIC_PIXEL_FMT_BLOB;
122 } else if (format_ == OHOS_CAMERA_FORMAT_YCRCB_420_SP) { // NV21
123 streamInfo.v1_0.encodeType_ = ENCODE_TYPE_NULL;
124 if (GetMode() == static_cast<int32_t>(HDI::Camera::V1_3::OperationMode::TIMELAPSE_PHOTO)) {
125 streamInfo.v1_0.format_ = GRAPHIC_PIXEL_FMT_YCRCB_420_SP; // NV21
126 } else {
127 streamInfo.v1_0.format_ = GRAPHIC_PIXEL_FMT_YCBCR_420_SP; // NV12
128 FullfillPictureExtendStreamInfos(streamInfo, GRAPHIC_PIXEL_FMT_YCBCR_420_SP);
129 }
130 } else {
131 streamInfo.v1_0.encodeType_ = ENCODE_TYPE_JPEG;
132 }
133 if (rawDeliverySwitch_) {
134 MEDIA_INFO_LOG("HStreamCapture::SetStreamInfo Set DNG info, streamId:%{public}d", GetFwkStreamId());
135 HDI::Camera::V1_1::ExtendedStreamInfo extendedStreamInfo = {
136 .type =
137 static_cast<HDI::Camera::V1_1::ExtendedStreamInfoType>(HDI::Camera::V1_3::EXTENDED_STREAM_INFO_RAW),
138 .width = width_,
139 .height = height_,
140 .format = streamInfo.v1_0.format_,
141 .dataspace = 0,
142 .bufferQueue = rawBufferQueue_,
143 };
144 streamInfo.extendedStreamInfos.push_back(extendedStreamInfo);
145 }
146 if (thumbnailSwitch_) {
147 HDI::Camera::V1_1::ExtendedStreamInfo extendedStreamInfo = {
148 .type = HDI::Camera::V1_1::EXTENDED_STREAM_INFO_QUICK_THUMBNAIL,
149 .width = 0,
150 .height = 0,
151 .format = 0,
152 .dataspace = 0,
153 .bufferQueue = thumbnailBufferQueue_,
154 };
155 streamInfo.extendedStreamInfos.push_back(extendedStreamInfo);
156 }
157 }
158
SetThumbnail(bool isEnabled, const sptr<OHOS::IBufferProducer> &producer)159 int32_t HStreamCapture::SetThumbnail(bool isEnabled, const sptr<OHOS::IBufferProducer> &producer)
160 {
161 if (isEnabled && producer != nullptr) {
162 thumbnailSwitch_ = 1;
163 thumbnailBufferQueue_ = new BufferProducerSequenceable(producer);
164 } else {
165 thumbnailSwitch_ = 0;
166 thumbnailBufferQueue_ = nullptr;
167 }
168 return CAMERA_OK;
169 }
170
EnableRawDelivery(bool enabled)171 int32_t HStreamCapture::EnableRawDelivery(bool enabled)
172 {
173 if (enabled) {
174 rawDeliverySwitch_ = 1;
175 } else {
176 rawDeliverySwitch_ = 0;
177 }
178 return CAMERA_OK;
179 }
180
SetBufferProducerInfo(const std::string bufName, const sptr<OHOS::IBufferProducer> &producer)181 int32_t HStreamCapture::SetBufferProducerInfo(const std::string bufName, const sptr<OHOS::IBufferProducer> &producer)
182 {
183 std::string resStr = "";
184 if (bufName == "rawImage") {
185 if (producer != nullptr) {
186 rawBufferQueue_ = new BufferProducerSequenceable(producer);
187 } else {
188 rawBufferQueue_ = nullptr;
189 resStr += bufName + ",";
190 }
191 }
192 if (bufName == "gainmapImage") {
193 if (producer != nullptr) {
194 gainmapBufferQueue_ = new BufferProducerSequenceable(producer);
195 } else {
196 gainmapBufferQueue_ = nullptr;
197 resStr += bufName + ",";
198 }
199 }
200 if (bufName == "deepImage") {
201 if (producer != nullptr) {
202 deepBufferQueue_ = new BufferProducerSequenceable(producer);
203 } else {
204 deepBufferQueue_ = nullptr;
205 resStr += bufName + ",";
206 }
207 }
208 if (bufName == "exifImage") {
209 if (producer != nullptr) {
210 exifBufferQueue_ = new BufferProducerSequenceable(producer);
211 } else {
212 exifBufferQueue_ = nullptr;
213 resStr += bufName + ",";
214 }
215 }
216 if (bufName == "debugImage") {
217 if (producer != nullptr) {
218 debugBufferQueue_ = new BufferProducerSequenceable(producer);
219 } else {
220 debugBufferQueue_ = nullptr;
221 resStr += bufName + ",";
222 }
223 }
224 MEDIA_INFO_LOG("HStreamCapture::SetBufferProducerInfo bufferQueue whether is nullptr: %{public}s", resStr.c_str());
225 return CAMERA_OK;
226 }
227
DeferImageDeliveryFor(int32_t type)228 int32_t HStreamCapture::DeferImageDeliveryFor(int32_t type)
229 {
230 MEDIA_INFO_LOG("HStreamCapture::DeferImageDeliveryFor type: %{public}d", type);
231 if (type == OHOS::HDI::Camera::V1_2::STILL_IMAGE) {
232 MEDIA_INFO_LOG("HStreamCapture STILL_IMAGE");
233 deferredPhotoSwitch_ = 1;
234 } else if (type == OHOS::HDI::Camera::V1_2::MOVING_IMAGE) {
235 MEDIA_INFO_LOG("HStreamCapture MOVING_IMAGE");
236 deferredVideoSwitch_ = 1;
237 } else {
238 MEDIA_INFO_LOG("HStreamCapture NONE");
239 deferredPhotoSwitch_ = 0;
240 deferredVideoSwitch_ = 0;
241 }
242 return CAMERA_OK;
243 }
244
PrepareBurst(int32_t captureId)245 int32_t HStreamCapture::PrepareBurst(int32_t captureId)
246 {
247 MEDIA_INFO_LOG("HStreamCapture::PrepareBurst captureId:%{public}d", captureId);
248 isBursting_ = true;
249 std::lock_guard<std::mutex> lock(burstLock_);
250 curBurstKey_ = GenerateBurstUuid();
251 burstkeyMap_.emplace(captureId, curBurstKey_);
252 std::vector<std::string> imageList = {};
253 burstImagesMap_.emplace(captureId, imageList);
254 burstNumMap_.emplace(captureId, 0);
255 burstNum_ = 0;
256 return CAMERA_OK;
257 }
258
ResetBurst()259 void HStreamCapture::ResetBurst()
260 {
261 MEDIA_INFO_LOG("HStreamCapture::ResetBurst");
262 curBurstKey_ = BURST_UUID_UNSET;
263 isBursting_ = false;
264 }
265
ResetBurstKey(int32_t captureId)266 void HStreamCapture::ResetBurstKey(int32_t captureId)
267 {
268 if (burstkeyMap_.erase(captureId) > 0 &&
269 burstImagesMap_.erase(captureId) > 0 &&
270 burstNumMap_.erase(captureId) > 0) {
271 MEDIA_INFO_LOG("HStreamCapture::ResetBurstKey captureId:%{public}d", captureId);
272 } else {
273 MEDIA_DEBUG_LOG("HStreamCapture::ResetBurstKey captureId not found");
274 }
275 }
276
GetBurstKey(int32_t captureId) const277 std::string HStreamCapture::GetBurstKey(int32_t captureId) const
278 {
279 MEDIA_DEBUG_LOG("HStreamCapture::GetBurstKey captureId:%{public}d", captureId);
280 std::string burstKey = BURST_UUID_UNSET;
281 std::lock_guard<std::mutex> lock(burstLock_);
282 auto iter = burstkeyMap_.find(captureId);
283 if (iter != burstkeyMap_.end()) {
284 burstKey = iter->second;
285 MEDIA_DEBUG_LOG("HStreamCapture::GetBurstKey %{public}s", burstKey.c_str());
286 } else {
287 MEDIA_DEBUG_LOG("HStreamCapture::GetBurstKey not found");
288 }
289 return burstKey;
290 }
291
IsBurstCapture(int32_t captureId) const292 bool HStreamCapture::IsBurstCapture(int32_t captureId) const
293 {
294 MEDIA_DEBUG_LOG("HStreamCapture::captureId:%{public}d", captureId);
295 auto iter = burstkeyMap_.find(captureId);
296 return iter != burstkeyMap_.end();
297 }
298
IsBurstCover(int32_t captureId) const299 bool HStreamCapture::IsBurstCover(int32_t captureId) const
300 {
301 MEDIA_DEBUG_LOG("HStreamCapture::IsBurstCover for captureId: %d", captureId);
302 std::lock_guard<std::mutex> lock(burstLock_);
303 auto iter = burstImagesMap_.find(captureId);
304 return (iter != burstImagesMap_.end()) ? (iter->second.size() == 1) : false;
305 }
306
GetCurBurstSeq(int32_t captureId) const307 int32_t HStreamCapture::GetCurBurstSeq(int32_t captureId) const
308 {
309 MEDIA_DEBUG_LOG("HStreamCapture::GetCurBurstSeq for captureId: %d", captureId);
310 std::lock_guard<std::mutex> lock(burstLock_);
311 auto iter = burstImagesMap_.find(captureId);
312 if (iter != burstImagesMap_.end()) {
313 return iter->second.size();
314 }
315 return -1;
316 }
317
SetBurstImages(int32_t captureId, std::string imageId)318 void HStreamCapture::SetBurstImages(int32_t captureId, std::string imageId)
319 {
320 MEDIA_DEBUG_LOG("HStreamCapture::SetBurstImages captureId:%{public}d imageId:%{public}s",
321 captureId, imageId.c_str());
322 std::lock_guard<std::mutex> lock(burstLock_);
323 auto iter = burstImagesMap_.find(captureId);
324 if (iter != burstImagesMap_.end()) {
325 iter->second.emplace_back(imageId);
326 MEDIA_DEBUG_LOG("HStreamCapture::SetBurstImages success");
327 } else {
328 MEDIA_ERR_LOG("HStreamCapture::SetBurstImages error");
329 }
330 }
331
CheckResetBurstKey(int32_t captureId)332 void HStreamCapture::CheckResetBurstKey(int32_t captureId)
333 {
334 MEDIA_DEBUG_LOG("HStreamCapture::CheckResetBurstKey captureId:%{public}d", captureId);
335 std::lock_guard<std::mutex> lock(burstLock_);
336 auto numIter = burstNumMap_.find(captureId);
337 auto imageIter = burstImagesMap_.find(captureId);
338 if (numIter != burstNumMap_.end() && imageIter != burstImagesMap_.end()) {
339 int32_t burstSum = numIter->second;
340 size_t curBurstSum = imageIter->second.size();
341 MEDIA_DEBUG_LOG("CheckResetBurstKey: burstSum=%d, curBurstSum=%zu", burstSum, curBurstSum);
342 if (static_cast<size_t>(burstSum) == curBurstSum) {
343 ResetBurstKey(captureId);
344 }
345 } else {
346 MEDIA_DEBUG_LOG("CheckResetBurstKey: captureId %d not found in one or both maps", captureId);
347 }
348 }
349
350
CheckBurstCapture(const std::shared_ptr<OHOS::Camera::CameraMetadata>& captureSettings, const int32_t &preparedCaptureId)351 int32_t HStreamCapture::CheckBurstCapture(const std::shared_ptr<OHOS::Camera::CameraMetadata>& captureSettings,
352 const int32_t &preparedCaptureId)
353 {
354 MEDIA_INFO_LOG("CheckBurstCapture start!");
355 camera_metadata_item_t item;
356 if (captureSettings == nullptr) {
357 MEDIA_ERR_LOG("captureSettings is nullptr");
358 return CAMERA_INVALID_STATE;
359 }
360 int32_t result = OHOS::Camera::FindCameraMetadataItem(captureSettings->get(), OHOS_CONTROL_BURST_CAPTURE, &item);
361 if (result == CAM_META_SUCCESS && item.count > 0) {
362 CameraBurstCaptureEnum burstState = static_cast<CameraBurstCaptureEnum>(item.data.u8[0]);
363 MEDIA_INFO_LOG("CheckBurstCapture get burstState:%{public}d", item.data.u8[0]);
364 if (burstState) {
365 std::string burstUuid = GetBurstKey(preparedCaptureId);
366 if (burstUuid != BURST_UUID_UNSET || isBursting_) {
367 MEDIA_ERR_LOG("CheckBurstCapture faild!");
368 return CAMERA_INVALID_STATE;
369 }
370 PrepareBurst(preparedCaptureId);
371 MEDIA_INFO_LOG("CheckBurstCapture ready!");
372 }
373 }
374 return CAM_META_SUCCESS;
375 }
376
Capture(const std::shared_ptr<OHOS::Camera::CameraMetadata>& captureSettings)377 int32_t HStreamCapture::Capture(const std::shared_ptr<OHOS::Camera::CameraMetadata>& captureSettings)
378 {
379 CAMERA_SYNC_TRACE;
380 MEDIA_INFO_LOG("HStreamCapture::Capture Entry, streamId:%{public}d", GetFwkStreamId());
381 auto streamOperator = GetStreamOperator();
382 CHECK_ERROR_RETURN_RET(streamOperator == nullptr, CAMERA_INVALID_STATE);
383 CHECK_ERROR_RETURN_RET_LOG(isCaptureReady_ == false, CAMERA_OPERATION_NOT_ALLOWED,
384 "HStreamCapture::Capture failed due to capture not ready");
385 auto preparedCaptureId = GetPreparedCaptureId();
386 CHECK_ERROR_RETURN_RET_LOG(preparedCaptureId != CAPTURE_ID_UNSET, CAMERA_INVALID_STATE,
387 "HStreamCapture::Capture, Already started with captureID: %{public}d", preparedCaptureId);
388 int32_t ret = PrepareCaptureId();
389 preparedCaptureId = GetPreparedCaptureId();
390 CHECK_ERROR_RETURN_RET_LOG(ret != CAMERA_OK || preparedCaptureId == CAPTURE_ID_UNSET, ret,
391 "HStreamCapture::Capture Failed to allocate a captureId");
392 ret = CheckBurstCapture(captureSettings, preparedCaptureId);
393 CHECK_ERROR_RETURN_RET_LOG(ret != CAMERA_OK, ret, "HStreamCapture::Capture Failed with burst state error");
394
395 CaptureInfo captureInfoPhoto;
396 captureInfoPhoto.streamIds_ = { GetHdiStreamId() };
397 ProcessCaptureInfoPhoto(captureInfoPhoto, captureSettings, preparedCaptureId);
398
399 auto callingTokenId = IPCSkeleton::GetCallingTokenID();
400 const std::string permissionName = "ohos.permission.CAMERA";
401 AddCameraPermissionUsedRecord(callingTokenId, permissionName);
402
403 // report capture performance dfx
404 std::shared_ptr<OHOS::Camera::CameraMetadata> captureMetadataSetting_ = nullptr;
405 OHOS::Camera::MetadataUtils::ConvertVecToMetadata(captureInfoPhoto.captureSetting_, captureMetadataSetting_);
406 DfxCaptureInfo captureInfo;
407 captureInfo.captureId = preparedCaptureId;
408 captureInfo.caller = CameraReportUtils::GetCallerInfo();
409 CameraReportUtils::GetInstance().SetCapturePerfStartInfo(captureInfo);
410 MEDIA_INFO_LOG("HStreamCapture::Capture Starting photo capture with capture ID: %{public}d", preparedCaptureId);
411 HStreamCommon::PrintCaptureDebugLog(captureMetadataSetting_);
412 CamRetCode rc = (CamRetCode)(streamOperator->Capture(preparedCaptureId, captureInfoPhoto, isBursting_));
413 if (rc != HDI::Camera::V1_0::NO_ERROR) {
414 ResetCaptureId();
415 captureIdForConfirmCapture_ = CAPTURE_ID_UNSET;
416 MEDIA_ERR_LOG("HStreamCapture::Capture failed with error Code: %{public}d", rc);
417 CameraReportUtils::ReportCameraError(
418 "HStreamCapture::Capture", rc, true, CameraReportUtils::GetCallerInfo());
419 ret = HdiToServiceError(rc);
420 }
421 camera_metadata_item_t item;
422 camera_position_enum_t cameraPosition = OHOS_CAMERA_POSITION_FRONT;
423 {
424 std::lock_guard<std::mutex> lock(cameraAbilityLock_);
425 if (cameraAbility_ == nullptr) {
426 MEDIA_ERR_LOG("HStreamCapture::cameraAbility_ is null");
427 return CAMERA_INVALID_STATE;
428 }
429 int32_t result = OHOS::Camera::FindCameraMetadataItem(cameraAbility_->get(), OHOS_ABILITY_CAMERA_POSITION,
430 &item);
431 if (result == CAM_META_SUCCESS && item.count > 0) {
432 cameraPosition = static_cast<camera_position_enum_t>(item.data.u8[0]);
433 }
434 }
435
436 int32_t NightMode = 4;
437 if (GetMode() == NightMode && cameraPosition == OHOS_CAMERA_POSITION_BACK) {
438 return ret;
439 }
440 ResetCaptureId();
441
442 uint32_t major;
443 uint32_t minor;
444 streamOperator->GetVersion(major, minor);
445 MEDIA_INFO_LOG("streamOperator GetVersion major:%{public}d, minor:%{public}d", major, minor);
446 // intercept when streamOperatorCallback support onCaptureReady
447 if (major >= HDI_VERSION_1 && minor >= HDI_VERSION_2 && !isBursting_) {
448 MEDIA_INFO_LOG("HStreamCapture::Capture set capture not ready");
449 isCaptureReady_ = false;
450 }
451 return ret;
452 }
453
ProcessCaptureInfoPhoto(CaptureInfo& captureInfoPhoto, const std::shared_ptr<OHOS::Camera::CameraMetadata>& captureSettings, int32_t captureId)454 void HStreamCapture::ProcessCaptureInfoPhoto(CaptureInfo& captureInfoPhoto,
455 const std::shared_ptr<OHOS::Camera::CameraMetadata>& captureSettings, int32_t captureId)
456 {
457 if (!OHOS::Camera::GetCameraMetadataItemCount(captureSettings->get())) {
458 std::lock_guard<std::mutex> lock(cameraAbilityLock_);
459 OHOS::Camera::MetadataUtils::ConvertMetadataToVec(cameraAbility_, captureInfoPhoto.captureSetting_);
460 } else {
461 OHOS::Camera::MetadataUtils::ConvertMetadataToVec(captureSettings, captureInfoPhoto.captureSetting_);
462 }
463 captureInfoPhoto.enableShutterCallback_ = true;
464 std::shared_ptr<OHOS::Camera::CameraMetadata> captureMetadataSetting_ = nullptr;
465 OHOS::Camera::MetadataUtils::ConvertVecToMetadata(captureInfoPhoto.captureSetting_, captureMetadataSetting_);
466 if (captureMetadataSetting_ != nullptr) {
467 // convert rotation with application set rotation
468 SetRotation(captureMetadataSetting_, captureId);
469
470 // update settings
471 std::vector<uint8_t> finalSetting;
472 OHOS::Camera::MetadataUtils::ConvertMetadataToVec(captureMetadataSetting_, finalSetting);
473 captureInfoPhoto.captureSetting_ = finalSetting;
474 }
475 }
476
SetRotation(const std::shared_ptr<OHOS::Camera::CameraMetadata> &captureMetadataSetting_, int32_t captureId)477 void HStreamCapture::SetRotation(const std::shared_ptr<OHOS::Camera::CameraMetadata> &captureMetadataSetting_,
478 int32_t captureId)
479 {
480 // set orientation for capture
481 // sensor orientation, counter-clockwise rotation
482 int32_t sensorOrientation = 0;
483 int result;
484 camera_metadata_item_t item;
485 {
486 std::lock_guard<std::mutex> lock(cameraAbilityLock_);
487 if (cameraAbility_ == nullptr) {
488 return;
489 }
490 result = OHOS::Camera::FindCameraMetadataItem(cameraAbility_->get(), OHOS_SENSOR_ORIENTATION, &item);
491 if (result == CAM_META_SUCCESS && item.count > 0) {
492 sensorOrientation = item.data.i32[0];
493 }
494 MEDIA_INFO_LOG("set rotation sensor orientation %{public}d", sensorOrientation);
495
496 camera_position_enum_t cameraPosition = OHOS_CAMERA_POSITION_BACK;
497 result = OHOS::Camera::FindCameraMetadataItem(cameraAbility_->get(), OHOS_ABILITY_CAMERA_POSITION, &item);
498 if (result == CAM_META_SUCCESS && item.count > 0) {
499 cameraPosition = static_cast<camera_position_enum_t>(item.data.u8[0]);
500 }
501 MEDIA_INFO_LOG("set rotation camera position %{public}d", cameraPosition);
502 }
503
504 // rotation from application
505 int32_t rotationValue = 0;
506 result = OHOS::Camera::FindCameraMetadataItem(captureMetadataSetting_->get(), OHOS_JPEG_ORIENTATION, &item);
507 if (result == CAM_META_SUCCESS && item.count > 0) {
508 rotationValue = item.data.i32[0];
509 }
510 MEDIA_INFO_LOG("set rotation app rotationValue %{public}d", rotationValue);
511 rotationMap_.EnsureInsert(captureId, rotationValue);
512 // real rotation
513 int32_t rotation = sensorOrientation + rotationValue;
514 if (rotation >= CAPTURE_ROTATE_360) {
515 rotation = rotation - CAPTURE_ROTATE_360;
516 }
517 {
518 uint8_t connectType = 0;
519 std::lock_guard<std::mutex> lock(cameraAbilityLock_);
520 if (cameraAbility_ == nullptr) {
521 return;
522 }
523 int ret = OHOS::Camera::FindCameraMetadataItem(
524 cameraAbility_->get(), OHOS_ABILITY_CAMERA_CONNECTION_TYPE, &item);
525 if (ret == CAM_META_SUCCESS && item.count > 0) {
526 connectType = item.data.u8[0];
527 }
528 if (connectType == OHOS_CAMERA_CONNECTION_TYPE_REMOTE) {
529 rotation = rotationValue;
530 }
531 MEDIA_INFO_LOG("set rotation camera real rotation %{public}d", rotation);
532 }
533
534 bool status = false;
535 if (result == CAM_META_ITEM_NOT_FOUND) {
536 status = captureMetadataSetting_->addEntry(OHOS_JPEG_ORIENTATION, &rotation, 1);
537 } else if (result == CAM_META_SUCCESS) {
538 status = captureMetadataSetting_->updateEntry(OHOS_JPEG_ORIENTATION, &rotation, 1);
539 }
540 result = OHOS::Camera::FindCameraMetadataItem(captureMetadataSetting_->get(), OHOS_JPEG_ORIENTATION, &item);
541 if (result != CAM_META_SUCCESS) {
542 MEDIA_ERR_LOG("set rotation Failed to find OHOS_JPEG_ORIENTATION tag");
543 }
544 CHECK_ERROR_PRINT_LOG(!status, "set rotation Failed to set Rotation");
545 }
546
CancelCapture()547 int32_t HStreamCapture::CancelCapture()
548 {
549 CAMERA_SYNC_TRACE;
550 // Cancel capture is dummy till continuous/burst mode is supported
551 StopStream();
552 return CAMERA_OK;
553 }
554
SetMode(int32_t modeName)555 void HStreamCapture::SetMode(int32_t modeName)
556 {
557 modeName_ = modeName;
558 MEDIA_INFO_LOG("HStreamCapture SetMode modeName = %{public}d", modeName);
559 }
560
GetMode()561 int32_t HStreamCapture::GetMode()
562 {
563 MEDIA_INFO_LOG("HStreamCapture GetMode modeName = %{public}d", modeName_);
564 return modeName_;
565 }
566
ConfirmCapture()567 int32_t HStreamCapture::ConfirmCapture()
568 {
569 CAMERA_SYNC_TRACE;
570 auto streamOperator = GetStreamOperator();
571 CHECK_ERROR_RETURN_RET(streamOperator == nullptr, CAMERA_INVALID_STATE);
572 int32_t ret = 0;
573
574 // end burst capture
575 if (isBursting_) {
576 MEDIA_INFO_LOG("HStreamCapture::ConfirmCapture when burst capture");
577 std::vector<uint8_t> settingVector;
578 std::shared_ptr<OHOS::Camera::CameraMetadata> burstCaptureSettings = nullptr;
579 OHOS::Camera::MetadataUtils::ConvertMetadataToVec(cameraAbility_, settingVector);
580 OHOS::Camera::MetadataUtils::ConvertVecToMetadata(settingVector, burstCaptureSettings);
581 EndBurstCapture(burstCaptureSettings);
582 ret = Capture(burstCaptureSettings);
583 if (ret != CAMERA_OK) {
584 MEDIA_ERR_LOG("HStreamCapture::ConfirmCapture end burst faild!");
585 }
586 return ret;
587 }
588
589 auto preparedCaptureId = captureIdForConfirmCapture_;
590 MEDIA_INFO_LOG("HStreamCapture::ConfirmCapture with capture ID: %{public}d", preparedCaptureId);
591 sptr<HDI::Camera::V1_2::IStreamOperator> streamOperatorV1_2 =
592 OHOS::HDI::Camera::V1_2::IStreamOperator::CastFrom(streamOperator);
593 CHECK_ERROR_RETURN_RET_LOG(streamOperatorV1_2 == nullptr, CAMERA_UNKNOWN_ERROR,
594 "HStreamCapture::ConfirmCapture streamOperatorV1_2 castFrom failed!");
595 OHOS::HDI::Camera::V1_2::CamRetCode rc =
596 (HDI::Camera::V1_2::CamRetCode)(streamOperatorV1_2->ConfirmCapture(preparedCaptureId));
597 if (rc != HDI::Camera::V1_2::NO_ERROR) {
598 MEDIA_ERR_LOG("HStreamCapture::ConfirmCapture failed with error Code: %{public}d", rc);
599 ret = HdiToServiceErrorV1_2(rc);
600 }
601 ResetCaptureId();
602 captureIdForConfirmCapture_ = CAPTURE_ID_UNSET;
603 return ret;
604 }
605
EndBurstCapture(const std::shared_ptr<OHOS::Camera::CameraMetadata>& captureMetadataSetting)606 void HStreamCapture::EndBurstCapture(const std::shared_ptr<OHOS::Camera::CameraMetadata>& captureMetadataSetting)
607 {
608 CHECK_ERROR_RETURN(captureMetadataSetting == nullptr);
609 MEDIA_INFO_LOG("HStreamCapture::EndBurstCapture");
610 camera_metadata_item_t item;
611 bool status = false;
612 int result = OHOS::Camera::FindCameraMetadataItem(captureMetadataSetting->get(), OHOS_CONTROL_BURST_CAPTURE, &item);
613 uint8_t burstState = 0;
614 if (result == CAM_META_ITEM_NOT_FOUND) {
615 status = captureMetadataSetting->addEntry(OHOS_CONTROL_BURST_CAPTURE, &burstState, 1);
616 } else if (result == CAM_META_SUCCESS) {
617 status = captureMetadataSetting->updateEntry(OHOS_CONTROL_BURST_CAPTURE, &burstState, 1);
618 }
619
620 if (!status) {
621 MEDIA_ERR_LOG("HStreamCapture::EndBurstCapture Failed");
622 }
623 }
624
Release()625 int32_t HStreamCapture::Release()
626 {
627 return ReleaseStream(false);
628 }
629
ReleaseStream(bool isDelay)630 int32_t HStreamCapture::ReleaseStream(bool isDelay)
631 {
632 {
633 std::lock_guard<std::mutex> lock(callbackLock_);
634 streamCaptureCallback_ = nullptr;
635 }
636 return HStreamCommon::ReleaseStream(isDelay);
637 }
638
SetCallback(sptr<IStreamCaptureCallback> &callback)639 int32_t HStreamCapture::SetCallback(sptr<IStreamCaptureCallback> &callback)
640 {
641 CHECK_ERROR_RETURN_RET_LOG(callback == nullptr, CAMERA_INVALID_ARG, "HStreamCapture::SetCallback input is null");
642 std::lock_guard<std::mutex> lock(callbackLock_);
643 streamCaptureCallback_ = callback;
644 return CAMERA_OK;
645 }
646
OnCaptureStarted(int32_t captureId)647 int32_t HStreamCapture::OnCaptureStarted(int32_t captureId)
648 {
649 CAMERA_SYNC_TRACE;
650 std::lock_guard<std::mutex> lock(callbackLock_);
651 if (streamCaptureCallback_ != nullptr) {
652 streamCaptureCallback_->OnCaptureStarted(captureId);
653 }
654 return CAMERA_OK;
655 }
656
OnCaptureStarted(int32_t captureId, uint32_t exposureTime)657 int32_t HStreamCapture::OnCaptureStarted(int32_t captureId, uint32_t exposureTime)
658 {
659 CAMERA_SYNC_TRACE;
660 std::lock_guard<std::mutex> lock(callbackLock_);
661 if (streamCaptureCallback_ != nullptr) {
662 streamCaptureCallback_->OnCaptureStarted(captureId, exposureTime);
663 }
664 return CAMERA_OK;
665 }
666
OnCaptureEnded(int32_t captureId, int32_t frameCount)667 int32_t HStreamCapture::OnCaptureEnded(int32_t captureId, int32_t frameCount)
668 {
669 CAMERA_SYNC_TRACE;
670 std::lock_guard<std::mutex> lock(callbackLock_);
671 if (streamCaptureCallback_ != nullptr) {
672 streamCaptureCallback_->OnCaptureEnded(captureId, frameCount);
673 }
674 CameraReportUtils::GetInstance().SetCapturePerfEndInfo(captureId);
675 auto preparedCaptureId = GetPreparedCaptureId();
676 if (preparedCaptureId != CAPTURE_ID_UNSET) {
677 MEDIA_INFO_LOG("HStreamCapture::OnCaptureEnded capturId = %{public}d already used, need release",
678 preparedCaptureId);
679 ResetCaptureId();
680 captureIdForConfirmCapture_ = CAPTURE_ID_UNSET;
681 }
682 return CAMERA_OK;
683 }
684
OnCaptureError(int32_t captureId, int32_t errorCode)685 int32_t HStreamCapture::OnCaptureError(int32_t captureId, int32_t errorCode)
686 {
687 std::lock_guard<std::mutex> lock(callbackLock_);
688 if (streamCaptureCallback_ != nullptr) {
689 int32_t captureErrorCode;
690 if (errorCode == BUFFER_LOST) {
691 captureErrorCode = CAMERA_STREAM_BUFFER_LOST;
692 } else {
693 captureErrorCode = CAMERA_UNKNOWN_ERROR;
694 }
695 CAMERA_SYSEVENT_FAULT(CreateMsg("Photo OnCaptureError! captureId:%d & "
696 "errorCode:%{public}d", captureId, captureErrorCode));
697 streamCaptureCallback_->OnCaptureError(captureId, captureErrorCode);
698 }
699 auto preparedCaptureId = GetPreparedCaptureId();
700 if (preparedCaptureId != CAPTURE_ID_UNSET) {
701 MEDIA_INFO_LOG("HStreamCapture::OnCaptureError capturId = %{public}d already used, need release",
702 preparedCaptureId);
703 ResetCaptureId();
704 captureIdForConfirmCapture_ = CAPTURE_ID_UNSET;
705 }
706 return CAMERA_OK;
707 }
708
OnFrameShutter(int32_t captureId, uint64_t timestamp)709 int32_t HStreamCapture::OnFrameShutter(int32_t captureId, uint64_t timestamp)
710 {
711 CAMERA_SYNC_TRACE;
712 std::lock_guard<std::mutex> lock(callbackLock_);
713 if (streamCaptureCallback_ != nullptr) {
714 streamCaptureCallback_->OnFrameShutter(captureId, timestamp);
715 }
716 return CAMERA_OK;
717 }
718
OnFrameShutterEnd(int32_t captureId, uint64_t timestamp)719 int32_t HStreamCapture::OnFrameShutterEnd(int32_t captureId, uint64_t timestamp)
720 {
721 CAMERA_SYNC_TRACE;
722 std::lock_guard<std::mutex> lock(callbackLock_);
723 if (streamCaptureCallback_ != nullptr) {
724 streamCaptureCallback_->OnFrameShutterEnd(captureId, timestamp);
725 }
726 if (isBursting_) {
727 burstNum_++;
728 MEDIA_DEBUG_LOG("HStreamCapture::OnFrameShutterEnd burstNum:%{public}d", burstNum_);
729 }
730 return CAMERA_OK;
731 }
732
733
OnCaptureReady(int32_t captureId, uint64_t timestamp)734 int32_t HStreamCapture::OnCaptureReady(int32_t captureId, uint64_t timestamp)
735 {
736 CAMERA_SYNC_TRACE;
737 std::lock_guard<std::mutex> lock(callbackLock_);
738 MEDIA_INFO_LOG("HStreamCapture::Capture, notify OnCaptureReady with capture ID: %{public}d", captureId);
739 isCaptureReady_ = true;
740 if (streamCaptureCallback_ != nullptr) {
741 streamCaptureCallback_->OnCaptureReady(captureId, timestamp);
742 }
743 std::lock_guard<std::mutex> burstLock(burstLock_);
744 if (IsBurstCapture(captureId)) {
745 burstNumMap_[captureId] = burstNum_;
746 ResetBurst();
747 }
748 return CAMERA_OK;
749 }
750
DumpStreamInfo(CameraInfoDumper& infoDumper)751 void HStreamCapture::DumpStreamInfo(CameraInfoDumper& infoDumper)
752 {
753 infoDumper.Title("capture stream");
754 infoDumper.Msg("ThumbnailSwitch:[" + std::to_string(thumbnailSwitch_) + "]");
755 infoDumper.Msg("RawDeliverSwitch:[" + std::to_string(rawDeliverySwitch_) + "]");
756 if (thumbnailBufferQueue_) {
757 infoDumper.Msg(
758 "ThumbnailBuffer producer Id:[" + std::to_string(thumbnailBufferQueue_->producer_->GetUniqueId()) + "]");
759 }
760 HStreamCommon::DumpStreamInfo(infoDumper);
761 }
762
OperatePermissionCheck(uint32_t interfaceCode)763 int32_t HStreamCapture::OperatePermissionCheck(uint32_t interfaceCode)
764 {
765 switch (static_cast<StreamCaptureInterfaceCode>(interfaceCode)) {
766 case CAMERA_STREAM_CAPTURE_START: {
767 auto callerToken = IPCSkeleton::GetCallingTokenID();
768 if (callerToken_ != callerToken) {
769 MEDIA_ERR_LOG("HStreamCapture::OperatePermissionCheck fail, callerToken_ is : %{public}d, now token "
770 "is %{public}d",
771 callerToken_, callerToken);
772 return CAMERA_OPERATION_NOT_ALLOWED;
773 }
774 break;
775 }
776 default:
777 break;
778 }
779 return CAMERA_OK;
780 }
781
IsDeferredPhotoEnabled()782 int32_t HStreamCapture::IsDeferredPhotoEnabled()
783 {
784 MEDIA_INFO_LOG("HStreamCapture IsDeferredPhotoEnabled deferredPhotoSwitch_: %{public}d", deferredPhotoSwitch_);
785 if (deferredPhotoSwitch_ == 1) {
786 return 1;
787 }
788 MEDIA_INFO_LOG("HStreamCapture IsDeferredPhotoEnabled return 0");
789 return 0;
790 }
791
IsDeferredVideoEnabled()792 int32_t HStreamCapture::IsDeferredVideoEnabled()
793 {
794 MEDIA_INFO_LOG("HStreamCapture IsDeferredVideoEnabled deferredVideoSwitch_: %{public}d", deferredVideoSwitch_);
795 if (deferredVideoSwitch_ == 1) {
796 return 1;
797 }
798 return 0;
799 }
800
GetMovingPhotoVideoCodecType()801 int32_t HStreamCapture::GetMovingPhotoVideoCodecType()
802 {
803 MEDIA_INFO_LOG("HStreamCapture GetMovingPhotoVideoCodecType videoCodecType_: %{public}d", videoCodecType_);
804 return videoCodecType_;
805 }
806
SetMovingPhotoVideoCodecType(int32_t videoCodecType)807 int32_t HStreamCapture::SetMovingPhotoVideoCodecType(int32_t videoCodecType)
808 {
809 MEDIA_INFO_LOG("HStreamCapture SetMovingPhotoVideoCodecType videoCodecType_: %{public}d", videoCodecType);
810 videoCodecType_ = videoCodecType;
811 return 0;
812 }
813 } // namespace CameraStandard
814 } // namespace OHOS
815