1 /*
2 * Copyright (c) 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_metadata.h"
17
18 #include "camera_util.h"
19 #include "camera_log.h"
20 #include "metadata_utils.h"
21
22 namespace OHOS {
23 namespace CameraStandard {
HStreamMetadata(sptr<OHOS::IBufferProducer> producer,int32_t format)24 HStreamMetadata::HStreamMetadata(sptr<OHOS::IBufferProducer> producer, int32_t format)
25 : HStreamCommon(StreamType::METADATA, producer, format, 0, 0)
26 {}
27
~HStreamMetadata()28 HStreamMetadata::~HStreamMetadata()
29 {}
30
LinkInput(sptr<OHOS::HDI::Camera::V1_1::IStreamOperator> streamOperator,std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility,int32_t streamId)31 int32_t HStreamMetadata::LinkInput(sptr<OHOS::HDI::Camera::V1_1::IStreamOperator> streamOperator,
32 std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility, int32_t streamId)
33 {
34 if (streamOperator == nullptr || cameraAbility == nullptr) {
35 MEDIA_ERR_LOG("HStreamMetadata::LinkInput streamOperator is null");
36 return CAMERA_INVALID_ARG;
37 }
38 streamId_ = streamId;
39 streamOperator_ = streamOperator;
40 std::lock_guard<std::mutex> lock(cameraAbilityLock_);
41 cameraAbility_ = cameraAbility;
42 return CAMERA_OK;
43 }
44
SetStreamInfo(StreamInfo_V1_1 & streamInfo)45 void HStreamMetadata::SetStreamInfo(StreamInfo_V1_1 &streamInfo)
46 {
47 HStreamCommon::SetStreamInfo(streamInfo);
48 streamInfo.v1_0.intent_ = ANALYZE;
49 }
50
Start()51 int32_t HStreamMetadata::Start()
52 {
53 CAMERA_SYNC_TRACE;
54
55 if (streamOperator_ == nullptr) {
56 return CAMERA_INVALID_STATE;
57 }
58 if (curCaptureID_ != 0) {
59 MEDIA_ERR_LOG("HStreamMetadata::Start, Already started with captureID: %{public}d", curCaptureID_);
60 return CAMERA_INVALID_STATE;
61 }
62 int32_t ret = AllocateCaptureId(curCaptureID_);
63 if (ret != CAMERA_OK) {
64 MEDIA_ERR_LOG("HStreamMetadata::Start Failed to allocate a captureId");
65 return ret;
66 }
67 std::vector<uint8_t> ability;
68 {
69 std::lock_guard<std::mutex> lock(cameraAbilityLock_);
70 OHOS::Camera::MetadataUtils::ConvertMetadataToVec(cameraAbility_, ability);
71 }
72 CaptureInfo captureInfo;
73 captureInfo.streamIds_ = {streamId_};
74 captureInfo.captureSetting_ = ability;
75 captureInfo.enableShutterCallback_ = false;
76 MEDIA_INFO_LOG("HStreamMetadata::Start Starting with capture ID: %{public}d", curCaptureID_);
77 CamRetCode rc = (CamRetCode)(streamOperator_->Capture(curCaptureID_, captureInfo, true));
78 if (rc != HDI::Camera::V1_0::NO_ERROR) {
79 ReleaseCaptureId(curCaptureID_);
80 curCaptureID_ = 0;
81 MEDIA_ERR_LOG("HStreamMetadata::Start Failed with error Code:%{public}d", rc);
82 ret = HdiToServiceError(rc);
83 }
84 return ret;
85 }
86
Stop()87 int32_t HStreamMetadata::Stop()
88 {
89 CAMERA_SYNC_TRACE;
90
91 if (streamOperator_ == nullptr) {
92 return CAMERA_INVALID_STATE;
93 }
94 if (curCaptureID_ == 0) {
95 MEDIA_ERR_LOG("HStreamMetadata::Stop, Stream not started yet");
96 return CAMERA_INVALID_STATE;
97 }
98 int32_t ret = CAMERA_OK;
99 CamRetCode rc = (CamRetCode)(streamOperator_->CancelCapture(curCaptureID_));
100 if (rc != HDI::Camera::V1_0::NO_ERROR) {
101 MEDIA_ERR_LOG("HStreamMetadata::Stop Failed with errorCode:%{public}d, curCaptureID_: %{public}d",
102 rc, curCaptureID_);
103 ret = HdiToServiceError(rc);
104 }
105 ReleaseCaptureId(curCaptureID_);
106 curCaptureID_ = 0;
107 return ret;
108 }
109
Release()110 int32_t HStreamMetadata::Release()
111 {
112 return HStreamCommon::Release();
113 }
114
DumpStreamInfo(std::string & dumpString)115 void HStreamMetadata::DumpStreamInfo(std::string& dumpString)
116 {
117 dumpString += "metadata stream:\n";
118 HStreamCommon::DumpStreamInfo(dumpString);
119 }
120 } // namespace Standard
121 } // namespace OHOS
122