1 /*
2  * Copyright (c) 2021-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 "hcapture_session_stub.h"
17 #include "camera_log.h"
18 #include "camera_server_photo_proxy.h"
19 #include "camera_util.h"
20 #include "camera_xcollie.h"
21 #include "ipc_skeleton.h"
22 #include "camera_service_ipc_interface_code.h"
23 #include "camera_photo_proxy.h"
24 #include "picture.h"
25 #include <memory>
26 
27 namespace OHOS {
28 namespace CameraStandard {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)29 int HCaptureSessionStub::OnRemoteRequest(
30     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
31 {
32     DisableJeMalloc();
33     int errCode = -1;
34 
35     CHECK_AND_RETURN_RET(data.ReadInterfaceToken() == GetDescriptor(), errCode);
36     errCode = OperatePermissionCheck(code);
37     CHECK_AND_RETURN_RET(errCode == CAMERA_OK, errCode);
38     switch (code) {
39         case static_cast<uint32_t>(CaptureSessionInterfaceCode::CAMERA_CAPTURE_SESSION_BEGIN_CONFIG):
40             errCode = BeginConfig();
41             break;
42         case static_cast<uint32_t>(CaptureSessionInterfaceCode::CAMERA_CAPTURE_SESSION_CAN_ADD_INPUT):
43             errCode = HCaptureSessionStub::HandleCanAddInput(data, reply);
44             break;
45         case static_cast<uint32_t>(CaptureSessionInterfaceCode::CAMERA_CAPTURE_SESSION_ADD_INPUT):
46             errCode = HCaptureSessionStub::HandleAddInput(data);
47             break;
48         case static_cast<uint32_t>(CaptureSessionInterfaceCode::CAMERA_CAPTURE_SESSION_ADD_OUTPUT):
49             errCode = HCaptureSessionStub::HandleAddOutput(data);
50             break;
51         case static_cast<uint32_t>(CaptureSessionInterfaceCode::CAMERA_CAPTURE_SESSION_REMOVE_INPUT):
52             errCode = HCaptureSessionStub::HandleRemoveInput(data);
53             break;
54         case static_cast<uint32_t>(CaptureSessionInterfaceCode::CAMERA_CAPTURE_SESSION_REMOVE_OUTPUT):
55             errCode = HCaptureSessionStub::HandleRemoveOutput(data);
56             break;
57         case static_cast<uint32_t>(CaptureSessionInterfaceCode::CAMERA_CAPTURE_SESSION_COMMIT_CONFIG):
58             errCode = CommitConfig();
59             break;
60         case static_cast<uint32_t>(CaptureSessionInterfaceCode::CAMERA_CAPTURE_SESSION_START):
61             errCode = Start();
62             break;
63         case static_cast<uint32_t>(CaptureSessionInterfaceCode::CAMERA_CAPTURE_SESSION_STOP):
64             errCode = Stop();
65             break;
66         case static_cast<uint32_t>(CaptureSessionInterfaceCode::CAMERA_CAPTURE_SESSION_RELEASE):
67             {
68                 CameraXCollie cameraXCollie("hcaptureSessionStub::Release");
69                 errCode = Release();
70             }
71             break;
72         case static_cast<uint32_t>(CaptureSessionInterfaceCode::CAMERA_CAPTURE_SESSION_SET_CALLBACK):
73             errCode = HandleSetCallback(data);
74             break;
75         case static_cast<uint32_t>(CaptureSessionInterfaceCode::CAMERA_CAPTURE_GET_SESSION_STATE):
76             errCode = HandleGetSessionState(reply);
77             break;
78         case static_cast<uint32_t>(CaptureSessionInterfaceCode::CAMERA_CAPTURE_SESSION_SET_SMOOTH_ZOOM):
79             errCode = HandleSetSmoothZoom(data, reply);
80             break;
81         case static_cast<uint32_t>(CaptureSessionInterfaceCode::CAMERA_CAPTURE_GET_ACTIVE_COLOR_SPACE):
82             errCode = HandleGetActiveColorSpace(reply);
83             break;
84         case static_cast<uint32_t>(CaptureSessionInterfaceCode::CAMERA_CAPTURE_SET_COLOR_SPACE):
85             errCode = HandleSetColorSpace(data);
86             break;
87         case static_cast<uint32_t>(CaptureSessionInterfaceCode::CAMERA_CAPTURE_SESSION_SET_FEATURE_MODE):
88             errCode = HandleSetFeatureMode(data);
89             break;
90         case static_cast<uint32_t>(CaptureSessionInterfaceCode::CAMERA_CAPTURE_SESSION_ENABLE_MOTION_PHOTO):
91             errCode = HandleEnableMovingPhoto(data);
92             break;
93         case static_cast<uint32_t>(CaptureSessionInterfaceCode::CAMERA_CAPTURE_SESSION_START_MOVING_PHOTO_CAPTURE):
94             errCode = HandleStartMovingPhotoCapture(data);
95             break;
96         case static_cast<uint32_t>(CaptureSessionInterfaceCode::CAMERA_CAPTURE_SESSION_CREATE_MEDIA_LIBRARY_MANAGER):
97             errCode = HandleCreateMediaLibrary(data, reply);
98             break;
99         case static_cast<uint32_t>(
100             CaptureSessionInterfaceCode::CAMERA_CAPTURE_SESSION_CREATE_MEDIA_LIBRARY_MANAGER_PICTURE):
101             errCode = HandleCreateMediaLibraryForPicture(data, reply);
102             break;
103         case static_cast<uint32_t>(CaptureSessionInterfaceCode::CAMERA_CAPTURE_SESSION_SET_PREVIEW_ROTATE):
104             {
105                 std::string deviceClass = data.ReadString();
106                 errCode = SetPreviewRotation(deviceClass);
107             }
108             break;
109         default:
110             MEDIA_ERR_LOG("HCaptureSessionStub request code %{public}u not handled", code);
111             errCode = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
112             break;
113     }
114 
115     return errCode;
116 }
117 
HandleAddInput(MessageParcel & data)118 int32_t HCaptureSessionStub::HandleAddInput(MessageParcel &data)
119 {
120     sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
121     CHECK_AND_RETURN_RET_LOG(remoteObj != nullptr, IPC_STUB_INVALID_DATA_ERR,
122                              "HCaptureSessionStub HandleAddInput CameraDevice is null");
123 
124     sptr<ICameraDeviceService> cameraDevice = iface_cast<ICameraDeviceService>(remoteObj);
125     CHECK_AND_RETURN_RET_LOG(cameraDevice != nullptr, IPC_STUB_INVALID_DATA_ERR,
126                              "HCaptureSessionStub HandleAddInput Device is null");
127     return AddInput(cameraDevice);
128 }
129 
HandleCanAddInput(MessageParcel & data,MessageParcel & reply)130 int HCaptureSessionStub::HandleCanAddInput(MessageParcel &data, MessageParcel &reply)
131 {
132     sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
133     CHECK_AND_RETURN_RET_LOG(remoteObj != nullptr, IPC_STUB_INVALID_DATA_ERR,
134                              "HCaptureSessionStub HandleAddInput CameraDevice is null");
135     sptr<ICameraDeviceService> cameraDevice = iface_cast<ICameraDeviceService>(remoteObj);
136     CHECK_AND_RETURN_RET_LOG(cameraDevice != nullptr, IPC_STUB_INVALID_DATA_ERR,
137                              "HCaptureSessionStub HandleCanAddInput CameraDevice is null");
138     bool result = false;
139     int32_t ret = CanAddInput(cameraDevice, result);
140     MEDIA_INFO_LOG("HandleCanAddInput ret: %{public}d, result: %{public}d", ret, result);
141     CHECK_AND_RETURN_RET_LOG(reply.WriteBool(result), IPC_STUB_WRITE_PARCEL_ERR,
142         "HCaptureSessionStub HandleCanAddInput Write result failed");
143     return ret;
144 }
145 
HandleRemoveInput(MessageParcel & data)146 int32_t HCaptureSessionStub::HandleRemoveInput(MessageParcel &data)
147 {
148     sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
149     CHECK_AND_RETURN_RET_LOG(remoteObj != nullptr, IPC_STUB_INVALID_DATA_ERR,
150                              "HCaptureSessionStub HandleRemoveInput CameraDevice is null");
151 
152     sptr<ICameraDeviceService> cameraDevice = iface_cast<ICameraDeviceService>(remoteObj);
153     CHECK_AND_RETURN_RET_LOG(cameraDevice != nullptr, IPC_STUB_INVALID_DATA_ERR,
154                              "HCaptureSessionStub HandleRemoveInput CameraDevice is null");
155     return RemoveInput(cameraDevice);
156 }
157 
HandleAddOutput(MessageParcel & data)158 int32_t HCaptureSessionStub::HandleAddOutput(MessageParcel &data)
159 {
160     StreamType streamType = static_cast<StreamType>(data.ReadUint32());
161     sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
162     CHECK_AND_RETURN_RET_LOG(remoteObj != nullptr, IPC_STUB_INVALID_DATA_ERR,
163                              "HCaptureSessionStub HandleAddOutput remoteObj is null");
164     sptr<IStreamCommon> stream = nullptr;
165     if (streamType == StreamType::CAPTURE) {
166         stream = iface_cast<IStreamCapture>(remoteObj);
167     } else if (streamType == StreamType::REPEAT) {
168         stream = iface_cast<IStreamRepeat>(remoteObj);
169     }  else if (streamType == StreamType::METADATA) {
170         stream = iface_cast<IStreamMetadata>(remoteObj);
171     } else if (streamType == StreamType::DEPTH) {
172         stream = iface_cast<IStreamDepthData>(remoteObj);
173     }
174 
175     return AddOutput(streamType, stream);
176 }
177 
HandleRemoveOutput(MessageParcel & data)178 int32_t HCaptureSessionStub::HandleRemoveOutput(MessageParcel &data)
179 {
180     StreamType streamType = static_cast<StreamType>(data.ReadUint32());
181     sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
182     CHECK_AND_RETURN_RET_LOG(remoteObj != nullptr, IPC_STUB_INVALID_DATA_ERR,
183                              "HCaptureSessionStub HandleRemoveOutput remoteObj is null");
184     sptr<IStreamCommon> stream = nullptr;
185     if (streamType == StreamType::CAPTURE) {
186         stream = iface_cast<IStreamCapture>(remoteObj);
187     } else if (streamType == StreamType::REPEAT) {
188         stream = iface_cast<IStreamRepeat>(remoteObj);
189     } else if (streamType == StreamType::METADATA) {
190         stream = iface_cast<IStreamMetadata>(remoteObj);
191     } else if (streamType == StreamType::DEPTH) {
192         stream = iface_cast<IStreamDepthData>(remoteObj);
193     }
194     return RemoveOutput(streamType, stream);
195 }
196 
HandleSetCallback(MessageParcel & data)197 int32_t HCaptureSessionStub::HandleSetCallback(MessageParcel &data)
198 {
199     auto remoteObject = data.ReadRemoteObject();
200     CHECK_AND_RETURN_RET_LOG(remoteObject != nullptr, IPC_STUB_INVALID_DATA_ERR,
201                              "HCaptureSessionStub HandleSetCallback CaptureSessionCallback is null");
202 
203     auto callback = iface_cast<ICaptureSessionCallback>(remoteObject);
204     CHECK_AND_RETURN_RET_LOG(callback != nullptr, IPC_STUB_INVALID_DATA_ERR,
205                              "HCaptureSessionStub HandleSetCallback callback is null");
206     return SetCallback(callback);
207 }
208 
HandleGetSessionState(MessageParcel & reply)209 int32_t HCaptureSessionStub::HandleGetSessionState(MessageParcel &reply)
210 {
211     CaptureSessionState sessionState;
212     int32_t ret = GetSessionState(sessionState);
213     CHECK_AND_RETURN_RET_LOG(reply.WriteUint32(static_cast<uint32_t>(sessionState)), IPC_STUB_WRITE_PARCEL_ERR,
214                              "HCaptureSessionStub HandleGetSesstionState Write sessionState failed");
215     return ret;
216 }
217 
HandleGetActiveColorSpace(MessageParcel & reply)218 int HCaptureSessionStub::HandleGetActiveColorSpace(MessageParcel &reply)
219 {
220     ColorSpace currColorSpace;
221     int32_t ret = GetActiveColorSpace(currColorSpace);
222     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(static_cast<int32_t>(currColorSpace)), IPC_STUB_WRITE_PARCEL_ERR,
223                              "HCaptureSessionStub HandleGetActiveColorSpace write colorSpace failed");
224     return ret;
225 }
226 
HandleSetColorSpace(MessageParcel & data)227 int HCaptureSessionStub::HandleSetColorSpace(MessageParcel &data)
228 {
229     ColorSpace colorSpace = static_cast<ColorSpace>(data.ReadInt32());
230     ColorSpace colorSpaceForCapture = static_cast<ColorSpace>(data.ReadInt32());
231     bool isNeedUpdate = data.ReadBool();
232     return SetColorSpace(colorSpace, colorSpaceForCapture, isNeedUpdate);
233 }
234 
HandleSetSmoothZoom(MessageParcel & data,MessageParcel & reply)235 int HCaptureSessionStub::HandleSetSmoothZoom(MessageParcel &data, MessageParcel &reply)
236 {
237     int smoothZoomType = static_cast<int32_t>(data.ReadUint32());
238     int operationMode = static_cast<int32_t>(data.ReadUint32());
239     float targetZoomRatio = data.ReadFloat();
240     float duration;
241     int32_t ret = SetSmoothZoom(smoothZoomType, operationMode, targetZoomRatio, duration);
242     CHECK_AND_RETURN_RET_LOG(reply.WriteFloat(duration), IPC_STUB_WRITE_PARCEL_ERR,
243                              "HCaptureSessionStub HandleSetSmoothZoom Write duration failed");
244     return ret;
245 }
246 
HandleSetFeatureMode(MessageParcel & data)247 int HCaptureSessionStub::HandleSetFeatureMode(MessageParcel &data)
248 {
249     CHECK_AND_RETURN_RET(CheckSystemApp(), CAMERA_NO_PERMISSION);
250     int featureMode = static_cast<int>(data.ReadUint32());
251     return SetFeatureMode(featureMode);
252 }
253 
HandleEnableMovingPhoto(MessageParcel & data)254 int32_t HCaptureSessionStub::HandleEnableMovingPhoto(MessageParcel &data)
255 {
256     bool isEnabled = data.ReadBool();
257     return EnableMovingPhoto(isEnabled);
258 }
259 
HandleStartMovingPhotoCapture(MessageParcel & data)260 int32_t HCaptureSessionStub::HandleStartMovingPhotoCapture(MessageParcel &data)
261 {
262     bool isMirror = data.ReadBool();
263     bool isConfig = data.ReadBool();
264     return EnableMovingPhotoMirror(isMirror, isConfig);
265 }
266 
HandleCreateMediaLibrary(MessageParcel & data,MessageParcel & reply)267 int32_t HCaptureSessionStub::HandleCreateMediaLibrary(MessageParcel& data, MessageParcel &reply)
268 {
269     sptr<CameraPhotoProxy> photoProxy = new CameraPhotoProxy();
270     photoProxy->ReadFromParcel(data);
271     int64_t timestamp = data.ReadInt64();
272     CHECK_AND_RETURN_RET_LOG(photoProxy != nullptr, IPC_STUB_INVALID_DATA_ERR,
273         "HCaptureSessionStub HandleCreateMediaLibrary photoProxy is null");
274     std::string uri;
275     int32_t cameraShotType = 0;
276     std::string burstKey;
277     int32_t ret = CreateMediaLibrary(photoProxy, uri, cameraShotType, burstKey, timestamp);
278     CHECK_AND_RETURN_RET_LOG(reply.WriteString(uri) && reply.WriteInt32(cameraShotType) && reply.WriteString(burstKey),
279         IPC_STUB_WRITE_PARCEL_ERR, "HCaptureSessionStub HandleCreateMediaLibrary Write uri and cameraShotType failed");
280     return ret;
281 }
282 
HandleCreateMediaLibraryForPicture(MessageParcel & data,MessageParcel & reply)283 int32_t HCaptureSessionStub::HandleCreateMediaLibraryForPicture(MessageParcel& data, MessageParcel &reply)
284 {
285     Picture *picturePtr = Media::Picture::Unmarshalling(data);
286     CHECK_AND_RETURN_RET_LOG(picturePtr != nullptr, IPC_STUB_INVALID_DATA_ERR,
287         "HCaptureSessionStub HandleCreateMediaLibrary picture is null");
288     std::unique_ptr<Media::Picture> picture(std::move(picturePtr));
289     sptr<CameraPhotoProxy> photoProxy = new CameraPhotoProxy();
290     photoProxy->ReadFromParcel(data);
291     CHECK_AND_RETURN_RET_LOG(photoProxy != nullptr, IPC_STUB_INVALID_DATA_ERR,
292         "HCaptureSessionStub HandleCreateMediaLibrary photoProxy is null");
293     int64_t timestamp = data.ReadInt64();
294     std::string uri;
295     int32_t cameraShotType = 0;
296     std::string burstKey;
297     int32_t ret = CreateMediaLibrary(std::move(picture), photoProxy, uri, cameraShotType, burstKey, timestamp);
298     CHECK_AND_RETURN_RET_LOG(reply.WriteString(uri) && reply.WriteInt32(cameraShotType) && reply.WriteString(burstKey),
299         IPC_STUB_WRITE_PARCEL_ERR, "HCaptureSessionStub HandleCreateMediaLibrary Write uri and cameraShotType failed");
300     return ret;
301 }
302 } // namespace CameraStandard
303 } // namespace OHOS
304