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_log.h"
19 #include "camera_service_ipc_interface_code.h"
20 #include "camera_util.h"
21 #include "hstream_common.h"
22 #include "ipc_skeleton.h"
23 #include "metadata_utils.h"
24
25 namespace OHOS {
26 namespace CameraStandard {
27 using namespace OHOS::HDI::Camera::V1_0;
HStreamMetadata(sptr<OHOS::IBufferProducer> producer,int32_t format)28 HStreamMetadata::HStreamMetadata(sptr<OHOS::IBufferProducer> producer, int32_t format)
29 : HStreamCommon(StreamType::METADATA, producer, format, producer->GetDefaultWidth(), producer->GetDefaultHeight())
30 {}
31
~HStreamMetadata()32 HStreamMetadata::~HStreamMetadata()
33 {}
34
LinkInput(sptr<OHOS::HDI::Camera::V1_0::IStreamOperator> streamOperator,std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility)35 int32_t HStreamMetadata::LinkInput(sptr<OHOS::HDI::Camera::V1_0::IStreamOperator> streamOperator,
36 std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility)
37 {
38 if (streamOperator == nullptr || cameraAbility == nullptr) {
39 MEDIA_ERR_LOG("HStreamMetadata::LinkInput streamOperator is null");
40 return CAMERA_INVALID_ARG;
41 }
42 SetStreamOperator(streamOperator);
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 auto streamOperator = GetStreamOperator();
58 if (streamOperator == nullptr) {
59 return CAMERA_INVALID_STATE;
60 }
61 auto preparedCaptureId = GetPreparedCaptureId();
62 if (preparedCaptureId != CAPTURE_ID_UNSET) {
63 MEDIA_ERR_LOG("HStreamMetadata::Start, Already started with captureID: %{public}d", preparedCaptureId);
64 return CAMERA_INVALID_STATE;
65 }
66 int32_t ret = PrepareCaptureId();
67 preparedCaptureId = GetPreparedCaptureId();
68 if (ret != CAMERA_OK || preparedCaptureId == CAPTURE_ID_UNSET) {
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_ = { GetStreamId() };
79 captureInfo.captureSetting_ = ability;
80 captureInfo.enableShutterCallback_ = false;
81 MEDIA_INFO_LOG("HStreamMetadata::Start Starting with capture ID: %{public}d", preparedCaptureId);
82 CamRetCode rc = (CamRetCode)(streamOperator->Capture(preparedCaptureId, captureInfo, true));
83 if (rc != HDI::Camera::V1_0::NO_ERROR) {
84 ResetCaptureId();
85 MEDIA_ERR_LOG("HStreamMetadata::Start Failed with error Code:%{public}d", rc);
86 ret = HdiToServiceError(rc);
87 }
88 return ret;
89 }
90
Stop()91 int32_t HStreamMetadata::Stop()
92 {
93 CAMERA_SYNC_TRACE;
94 auto streamOperator = GetStreamOperator();
95 if (streamOperator == nullptr) {
96 return CAMERA_INVALID_STATE;
97 }
98 auto preparedCaptureId = GetPreparedCaptureId();
99 if (preparedCaptureId == CAPTURE_ID_UNSET) {
100 MEDIA_ERR_LOG("HStreamMetadata::Stop, Stream not started yet");
101 return CAMERA_INVALID_STATE;
102 }
103 int32_t ret = StopStream();
104 if (ret != CAMERA_OK) {
105 MEDIA_ERR_LOG("HStreamMetadata::Stop Failed with errorCode:%{public}d, curCaptureID_: %{public}d", ret,
106 preparedCaptureId);
107 }
108 return ret;
109 }
110
Release()111 int32_t HStreamMetadata::Release()
112 {
113 return ReleaseStream(false);
114 }
115
ReleaseStream(bool isDelay)116 int32_t HStreamMetadata::ReleaseStream(bool isDelay)
117 {
118 return HStreamCommon::ReleaseStream(isDelay);
119 }
120
DumpStreamInfo(std::string & dumpString)121 void HStreamMetadata::DumpStreamInfo(std::string& dumpString)
122 {
123 dumpString += "metadata stream:\n";
124 HStreamCommon::DumpStreamInfo(dumpString);
125 }
126
OperatePermissionCheck(uint32_t interfaceCode)127 int32_t HStreamMetadata::OperatePermissionCheck(uint32_t interfaceCode)
128 {
129 switch (static_cast<StreamMetadataInterfaceCode>(interfaceCode)) {
130 case StreamMetadataInterfaceCode::CAMERA_STREAM_META_START: {
131 auto callerToken = IPCSkeleton::GetCallingTokenID();
132 if (callerToken_ != callerToken) {
133 MEDIA_ERR_LOG("HStreamMetadata::OperatePermissionCheck fail, callerToken_ is : %{public}d, now token "
134 "is %{public}d",
135 callerToken_, callerToken);
136 return CAMERA_OPERATION_NOT_ALLOWED;
137 }
138 break;
139 }
140 default:
141 break;
142 }
143 return CAMERA_OK;
144 }
145 } // namespace Standard
146 } // namespace OHOS
147