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