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