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 {
40 std::lock_guard<std::mutex> lock(streamOperatorLock_);
41 streamOperator_ = streamOperator;
42 }
43 std::lock_guard<std::mutex> lock(cameraAbilityLock_);
44 cameraAbility_ = cameraAbility;
45 return CAMERA_OK;
46 }
47
SetStreamInfo(StreamInfo_V1_1 & streamInfo)48 void HStreamMetadata::SetStreamInfo(StreamInfo_V1_1 &streamInfo)
49 {
50 HStreamCommon::SetStreamInfo(streamInfo);
51 streamInfo.v1_0.intent_ = ANALYZE;
52 }
53
Start()54 int32_t HStreamMetadata::Start()
55 {
56 CAMERA_SYNC_TRACE;
57 {
58 std::lock_guard<std::mutex> lock(streamOperatorLock_);
59 if (streamOperator_ == nullptr) {
60 return CAMERA_INVALID_STATE;
61 }
62 }
63 if (curCaptureID_ != 0) {
64 MEDIA_ERR_LOG("HStreamMetadata::Start, Already started with captureID: %{public}d", curCaptureID_);
65 return CAMERA_INVALID_STATE;
66 }
67 int32_t ret = AllocateCaptureId(curCaptureID_);
68 if (ret != CAMERA_OK) {
69 MEDIA_ERR_LOG("HStreamMetadata::Start Failed to allocate a captureId");
70 return ret;
71 }
72 std::vector<uint8_t> ability;
73 {
74 std::lock_guard<std::mutex> lock(cameraAbilityLock_);
75 OHOS::Camera::MetadataUtils::ConvertMetadataToVec(cameraAbility_, ability);
76 }
77 CaptureInfo captureInfo;
78 captureInfo.streamIds_ = {streamId_};
79 captureInfo.captureSetting_ = ability;
80 captureInfo.enableShutterCallback_ = false;
81 MEDIA_INFO_LOG("HStreamMetadata::Start Starting with capture ID: %{public}d", curCaptureID_);
82 CamRetCode rc;
83 {
84 std::lock_guard<std::mutex> lock(streamOperatorLock_);
85 rc = (CamRetCode)(streamOperator_->Capture(curCaptureID_, captureInfo, true));
86 }
87 if (rc != HDI::Camera::V1_0::NO_ERROR) {
88 ReleaseCaptureId(curCaptureID_);
89 curCaptureID_ = 0;
90 MEDIA_ERR_LOG("HStreamMetadata::Start Failed with error Code:%{public}d", rc);
91 ret = HdiToServiceError(rc);
92 }
93 return ret;
94 }
95
Stop()96 int32_t HStreamMetadata::Stop()
97 {
98 CAMERA_SYNC_TRACE;
99 {
100 std::lock_guard<std::mutex> lock(streamOperatorLock_);
101 if (streamOperator_ == nullptr) {
102 return CAMERA_INVALID_STATE;
103 }
104 }
105 if (curCaptureID_ == 0) {
106 MEDIA_ERR_LOG("HStreamMetadata::Stop, Stream not started yet");
107 return CAMERA_INVALID_STATE;
108 }
109 int32_t ret = CAMERA_OK;
110 CamRetCode rc;
111 {
112 std::lock_guard<std::mutex> lock(streamOperatorLock_);
113 rc = (CamRetCode)(streamOperator_->CancelCapture(curCaptureID_));
114 }
115
116 if (rc != HDI::Camera::V1_0::NO_ERROR) {
117 MEDIA_ERR_LOG("HStreamMetadata::Stop Failed with errorCode:%{public}d, curCaptureID_: %{public}d",
118 rc, curCaptureID_);
119 ret = HdiToServiceError(rc);
120 }
121 ReleaseCaptureId(curCaptureID_);
122 curCaptureID_ = 0;
123 return ret;
124 }
125
Release()126 int32_t HStreamMetadata::Release()
127 {
128 return HStreamCommon::Release();
129 }
130
DumpStreamInfo(std::string & dumpString)131 void HStreamMetadata::DumpStreamInfo(std::string& dumpString)
132 {
133 dumpString += "metadata stream:\n";
134 HStreamCommon::DumpStreamInfo(dumpString);
135 }
136 } // namespace Standard
137 } // namespace OHOS
138