1 /*
2 * Copyright (c) 2021 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 "stream_operator_proxy.h"
17 #include <hdf_base.h>
18 #include <hdf_log.h>
19 #include <message_parcel.h>
20 #include "istream_operator_callback.h"
21 #include "ioffline_stream_operator.h"
22 #include "camera_metadata_info.h"
23 #include "utils_data_stub.h"
24 #include "metadata_utils.h"
25
26 namespace OHOS::Camera {
IsStreamsSupported(OperationMode mode,const std::shared_ptr<CameraMetadata> & modeSetting,const std::vector<std::shared_ptr<StreamInfo>> & pInfo,StreamSupportType & pType)27 CamRetCode StreamOperatorProxy::IsStreamsSupported(
28 OperationMode mode,
29 const std::shared_ptr<CameraMetadata> &modeSetting,
30 const std::vector<std::shared_ptr<StreamInfo>> &pInfo,
31 StreamSupportType &pType)
32 {
33 MessageParcel data;
34 MessageParcel reply;
35 MessageOption option;
36
37 if (!data.WriteInterfaceToken(StreamOperatorProxy::GetDescriptor())) {
38 HDF_LOGE("%{public}s: write interface descriptor failed.", __func__);
39 return INVALID_ARGUMENT;
40 }
41
42 // stub operation mode
43 if (!data.WriteInt32(mode)) {
44 HDF_LOGE("%s: write operation mode failed", __func__);
45 return INVALID_ARGUMENT;
46 }
47
48 bool nullFlag = (modeSetting != nullptr);
49 if (!data.WriteBool(nullFlag)) {
50 HDF_LOGE("%s: write mode nullflag failed", __func__);
51 return INVALID_ARGUMENT;
52 }
53
54 // stub metadata
55 if (nullFlag && !MetadataUtils::EncodeCameraMetadata(modeSetting, data)) {
56 HDF_LOGE("%s: write metadata failed", __func__);
57 return INVALID_ARGUMENT;
58 }
59
60 size_t count = pInfo.size();
61 if (!data.WriteInt32(static_cast<int32_t>(count))) {
62 HDF_LOGE("%s: write pInfo count failed", __func__);
63 return INVALID_ARGUMENT;
64 }
65
66 bool bRet = true;
67 for (size_t i = 0; i < count; i++) {
68 std::shared_ptr<StreamInfo> streamInfo = pInfo.at(i);
69 bRet = UtilsDataStub::EncodeStreamInfo(streamInfo, data);
70 if (!bRet) {
71 HDF_LOGE("%s: write streamInfo failed. index = %zu", __func__, i);
72 return INVALID_ARGUMENT;
73 }
74 }
75
76 int32_t ret = Remote()->SendRequest(
77 CMD_STREAM_OPERATOR_IS_STREAMS_SUPPORTED, data, reply, option);
78 if (ret != HDF_SUCCESS) {
79 HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ret);
80 return INVALID_ARGUMENT;
81 }
82
83 CamRetCode retCode = static_cast<CamRetCode>(reply.ReadInt32());
84 pType = static_cast<StreamSupportType>(reply.ReadInt32());
85
86 return retCode;
87 }
88
CreateStreams(const std::vector<std::shared_ptr<StreamInfo>> & streamInfos)89 CamRetCode StreamOperatorProxy::CreateStreams(
90 const std::vector<std::shared_ptr<StreamInfo>> &streamInfos)
91 {
92 MessageParcel data;
93 MessageParcel reply;
94 MessageOption option;
95
96 if (!data.WriteInterfaceToken(StreamOperatorProxy::GetDescriptor())) {
97 HDF_LOGE("%{public}s: write interface descriptor failed.", __func__);
98 return INVALID_ARGUMENT;
99 }
100
101 size_t count = streamInfos.size();
102 if (!data.WriteInt32(static_cast<int32_t>(count))) {
103 HDF_LOGE("%s: write streamInfos count failed", __func__);
104 return INVALID_ARGUMENT;
105 }
106
107 bool bRet = true;
108 for (size_t i = 0; i < count; i++) {
109 std::shared_ptr<StreamInfo> streamInfo = streamInfos.at(i);
110 bRet = UtilsDataStub::EncodeStreamInfo(streamInfo, data);
111 if (!bRet) {
112 HDF_LOGE("%s: write streamInfo failed. index = %zu", __func__, i);
113 return INVALID_ARGUMENT;
114 }
115 }
116
117 int32_t ret = Remote()->SendRequest(
118 CMD_STREAM_OPERATOR_CREATE_STREAMS, data, reply, option);
119 if (ret != HDF_SUCCESS) {
120 HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ret);
121 return INVALID_ARGUMENT;
122 }
123
124 return static_cast<CamRetCode>(reply.ReadInt32());
125 }
126
ReleaseStreams(const std::vector<int> & streamIds)127 CamRetCode StreamOperatorProxy::ReleaseStreams(const std::vector<int> &streamIds)
128 {
129 MessageParcel data;
130 MessageParcel reply;
131 MessageOption option;
132
133 if (!data.WriteInterfaceToken(StreamOperatorProxy::GetDescriptor())) {
134 HDF_LOGE("%{public}s: write interface descriptor failed.", __func__);
135 return INVALID_ARGUMENT;
136 }
137
138 std::vector<int32_t> pxyStreamIds = streamIds;
139 if (!data.WriteInt32Vector(pxyStreamIds)) {
140 HDF_LOGE("%s: write streamIds failed", __func__);
141 return INVALID_ARGUMENT;
142 }
143
144 int32_t ret = Remote()->SendRequest(
145 CMD_STREAM_OPERATOR_RELEASE_STREAMS, data, reply, option);
146 if (ret != HDF_SUCCESS) {
147 HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ret);
148 return INVALID_ARGUMENT;
149 }
150
151 return static_cast<CamRetCode>(reply.ReadInt32());
152 }
153
CommitStreams(OperationMode mode,const std::shared_ptr<CameraMetadata> & modeSetting)154 CamRetCode StreamOperatorProxy::CommitStreams(OperationMode mode,
155 const std::shared_ptr<CameraMetadata> &modeSetting)
156 {
157 MessageParcel data;
158 MessageParcel reply;
159 MessageOption option;
160
161 if (!data.WriteInterfaceToken(StreamOperatorProxy::GetDescriptor())) {
162 HDF_LOGE("%{public}s: write interface descriptor failed.", __func__);
163 return INVALID_ARGUMENT;
164 }
165
166 if (!data.WriteInt32(mode)) {
167 HDF_LOGE("%s: write operation mode failed", __func__);
168 return INVALID_ARGUMENT;
169 }
170
171 bool bRet = MetadataUtils::EncodeCameraMetadata(modeSetting, data);
172 if (!bRet) {
173 HDF_LOGE("%s: write metadata failed", __func__);
174 return INVALID_ARGUMENT;
175 }
176
177 int32_t ret = Remote()->SendRequest(
178 CMD_STREAM_OPERATOR_COMMIT_STREAMS, data, reply, option);
179 if (ret != HDF_SUCCESS) {
180 HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ret);
181 return INVALID_ARGUMENT;
182 }
183
184 return static_cast<CamRetCode>(reply.ReadInt32());
185 }
186
GetStreamAttributes(std::vector<std::shared_ptr<StreamAttribute>> & attributes)187 CamRetCode StreamOperatorProxy::GetStreamAttributes(
188 std::vector<std::shared_ptr<StreamAttribute>> &attributes)
189 {
190 MessageParcel data;
191 MessageParcel reply;
192 MessageOption option;
193
194 if (!data.WriteInterfaceToken(StreamOperatorProxy::GetDescriptor())) {
195 HDF_LOGE("%{public}s: write interface descriptor failed.", __func__);
196 return INVALID_ARGUMENT;
197 }
198
199 int32_t ret = Remote()->SendRequest(
200 CMD_STREAM_OPERATOR_GET_STREAM_ATTRIBUTES, data, reply, option);
201 if (ret != HDF_SUCCESS) {
202 HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ret);
203 return INVALID_ARGUMENT;
204 }
205
206 int32_t retCode = reply.ReadInt32();
207 int32_t count = reply.ReadInt32();
208 for (int i = 0; i < count; i++) {
209 const uint8_t *buffer = data.ReadBuffer(sizeof(StreamAttribute));
210 std::shared_ptr<StreamAttribute> attribute =
211 std::shared_ptr<StreamAttribute>(
212 reinterpret_cast<StreamAttribute*>(
213 const_cast<uint8_t *>(buffer)));
214 attributes.push_back(attribute);
215 }
216
217 return static_cast<CamRetCode>(retCode);
218 }
219
AttachBufferQueue(int streamId,const OHOS::sptr<OHOS::IBufferProducer> & producer)220 CamRetCode StreamOperatorProxy::AttachBufferQueue(int streamId, const OHOS::sptr<OHOS::IBufferProducer> &producer)
221 {
222 MessageParcel data;
223 MessageParcel reply;
224 MessageOption option;
225
226 if (!data.WriteInterfaceToken(StreamOperatorProxy::GetDescriptor())) {
227 HDF_LOGE("%{public}s: write interface descriptor failed.", __func__);
228 return INVALID_ARGUMENT;
229 }
230
231 if (producer == nullptr) {
232 HDF_LOGE("%s: producer is NULL", __func__);
233 return INVALID_ARGUMENT;
234 }
235
236 if (!data.WriteInt32(static_cast<int32_t>(streamId))) {
237 HDF_LOGE("%s: write streamId failed", __func__);
238 return INVALID_ARGUMENT;
239 }
240
241 if (!data.WriteRemoteObject(producer->AsObject())) {
242 HDF_LOGE("%s: write buffer producer failed", __func__);
243 return INVALID_ARGUMENT;
244 }
245
246 int32_t ret = Remote()->SendRequest(
247 CMD_STREAM_OPERATOR_ATTACH_BUFFER_QUEUE, data, reply, option);
248 if (ret != HDF_SUCCESS) {
249 HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ret);
250 return INVALID_ARGUMENT;
251 }
252
253 return static_cast<CamRetCode>(reply.ReadInt32());
254 }
255
DetachBufferQueue(int streamId)256 CamRetCode StreamOperatorProxy::DetachBufferQueue(int streamId)
257 {
258 MessageParcel data;
259 MessageParcel reply;
260 MessageOption option;
261
262 if (!data.WriteInterfaceToken(StreamOperatorProxy::GetDescriptor())) {
263 HDF_LOGE("%{public}s: write interface descriptor failed.", __func__);
264 return INVALID_ARGUMENT;
265 }
266
267 if (!data.WriteInt32(static_cast<int32_t>(streamId))) {
268 HDF_LOGE("%s: write streamId failed", __func__);
269 return INVALID_ARGUMENT;
270 }
271
272 int32_t ret = Remote()->SendRequest(
273 CMD_STREAM_OPERATOR_DETACH_BUFFER_QUEUE, data, reply, option);
274 if (ret != HDF_SUCCESS) {
275 HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ret);
276 return INVALID_ARGUMENT;
277 }
278
279 return static_cast<CamRetCode>(reply.ReadInt32());
280 }
281
Capture(int captureId,const std::shared_ptr<CaptureInfo> & pInfo,bool isStreaming)282 CamRetCode StreamOperatorProxy::Capture(int captureId,
283 const std::shared_ptr<CaptureInfo> &pInfo, bool isStreaming)
284 {
285 MessageParcel data;
286 MessageParcel reply;
287 MessageOption option;
288
289 if (!data.WriteInterfaceToken(StreamOperatorProxy::GetDescriptor())) {
290 HDF_LOGE("%{public}s: write interface descriptor failed.", __func__);
291 return INVALID_ARGUMENT;
292 }
293
294 if (pInfo == nullptr) {
295 return INVALID_ARGUMENT;
296 }
297
298 // stub captureId
299 if (!data.WriteInt32(static_cast<int32_t>(captureId))) {
300 HDF_LOGE("%s: write captureId failed", __func__);
301 return INVALID_ARGUMENT;
302 }
303
304 // stub CaptureInfo
305 std::vector<int32_t> pxyStreamIds = pInfo->streamIds_;
306 if (!data.WriteInt32Vector(pxyStreamIds)) {
307 HDF_LOGE("%s: write streamIds failed", __func__);
308 return INVALID_ARGUMENT;
309 }
310
311 bool bRet = MetadataUtils::EncodeCameraMetadata(pInfo->captureSetting_, data);
312 if (!bRet) {
313 HDF_LOGE("%s: write metadata failed", __func__);
314 return INVALID_ARGUMENT;
315 }
316
317 if (!data.WriteBool(pInfo->enableShutterCallback_)) {
318 HDF_LOGE("%s: write enableShutterCallback_ failed", __func__);
319 return INVALID_ARGUMENT;
320 }
321
322 // stub isStreaming
323 if (!data.WriteBool(isStreaming)) {
324 HDF_LOGE("%s: write isStreaming failed", __func__);
325 return INVALID_ARGUMENT;
326 }
327
328 int32_t ret = Remote()->SendRequest(
329 CMD_STREAM_OPERATOR_CAPTURE, data, reply, option);
330 if (ret != HDF_SUCCESS) {
331 HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ret);
332 return INVALID_ARGUMENT;
333 }
334
335 return static_cast<CamRetCode>(reply.ReadInt32());
336 }
337
CancelCapture(int captureId)338 CamRetCode StreamOperatorProxy::CancelCapture(int captureId)
339 {
340 MessageParcel data;
341 MessageParcel reply;
342 MessageOption option;
343
344 if (!data.WriteInterfaceToken(StreamOperatorProxy::GetDescriptor())) {
345 HDF_LOGE("%{public}s: write interface descriptor failed.", __func__);
346 return INVALID_ARGUMENT;
347 }
348
349 if (!data.WriteInt32(static_cast<int32_t>(captureId))) {
350 HDF_LOGE("%s: write captureId failed", __func__);
351 return INVALID_ARGUMENT;
352 }
353
354 int32_t ret = Remote()->SendRequest(
355 CMD_STREAM_OPERATOR_CANCEL_CAPTURE, data, reply, option);
356 if (ret != HDF_SUCCESS) {
357 HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ret);
358 return INVALID_ARGUMENT;
359 }
360
361 return static_cast<CamRetCode>(reply.ReadInt32());
362 }
363
ChangeToOfflineStream(const std::vector<int> & streamIds,OHOS::sptr<IStreamOperatorCallback> & callback,OHOS::sptr<IOfflineStreamOperator> & offlineOperator)364 CamRetCode StreamOperatorProxy::ChangeToOfflineStream(
365 const std::vector<int> &streamIds,
366 OHOS::sptr<IStreamOperatorCallback> &callback,
367 OHOS::sptr<IOfflineStreamOperator> &offlineOperator)
368 {
369 MessageParcel data;
370 MessageParcel reply;
371 MessageOption option;
372
373 if (!data.WriteInterfaceToken(StreamOperatorProxy::GetDescriptor())) {
374 HDF_LOGE("%{public}s: write interface descriptor failed.", __func__);
375 return INVALID_ARGUMENT;
376 }
377
378 if (callback == nullptr) {
379 return INVALID_ARGUMENT;
380 }
381
382 std::vector<int32_t> pxyStreamIds = streamIds;
383 if (!data.WriteInt32Vector(pxyStreamIds)) {
384 HDF_LOGE("%s: write streamIds failed", __func__);
385 return INVALID_ARGUMENT;
386 }
387
388 if (!data.WriteRemoteObject(callback->AsObject())) {
389 HDF_LOGE("%s: write offline stream operator callback failed", __func__);
390 return INVALID_ARGUMENT;
391 }
392
393 int32_t ret = Remote()->SendRequest(
394 CMD_STREAM_OPERATOR_CHANGE_TO_OFFLINE_STREAM, data, reply, option);
395 if (ret != HDF_SUCCESS) {
396 HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ret);
397 return INVALID_ARGUMENT;
398 }
399
400 CamRetCode retCode = static_cast<CamRetCode>(reply.ReadInt32());
401 sptr<IRemoteObject> remoteObj = reply.ReadRemoteObject();
402 offlineOperator = OHOS::iface_cast<IOfflineStreamOperator>(remoteObj);
403
404 return retCode;
405 }
406 }