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