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 "hstream_repeat_stub.h"
17
18 #include "camera_log.h"
19 #include "camera_service_ipc_interface_code.h"
20 #include "camera_util.h"
21
22 namespace OHOS {
23 namespace CameraStandard {
24 static constexpr float SKETCH_RATIO_MAX_VALUE = 100.0f;
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)25 int HStreamRepeatStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
26 {
27 DisableJeMalloc();
28 int errCode = -1;
29
30 CHECK_ERROR_RETURN_RET(data.ReadInterfaceToken() != GetDescriptor(), errCode);
31 errCode = OperatePermissionCheck(code);
32 CHECK_ERROR_RETURN_RET(errCode != CAMERA_OK, errCode);
33 switch (code) {
34 case static_cast<uint32_t>(StreamRepeatInterfaceCode::CAMERA_START_VIDEO_RECORDING):
35 errCode = Start();
36 break;
37 case static_cast<uint32_t>(StreamRepeatInterfaceCode::CAMERA_STOP_VIDEO_RECORDING):
38 errCode = Stop();
39 break;
40 case static_cast<uint32_t>(StreamRepeatInterfaceCode::CAMERA_STREAM_REPEAT_SET_CALLBACK):
41 errCode = HStreamRepeatStub::HandleSetCallback(data);
42 break;
43 case static_cast<uint32_t>(StreamRepeatInterfaceCode::CAMERA_STREAM_REPEAT_RELEASE):
44 errCode = Release();
45 break;
46 case static_cast<uint32_t>(StreamRepeatInterfaceCode::CAMERA_ADD_DEFERRED_SURFACE):
47 errCode = HStreamRepeatStub::HandleAddDeferredSurface(data);
48 break;
49 case static_cast<uint32_t>(StreamRepeatInterfaceCode::CAMERA_FORK_SKETCH_STREAM_REPEAT):
50 errCode = HStreamRepeatStub::HandleForkSketchStreamRepeat(data, reply);
51 break;
52 case static_cast<uint32_t>(StreamRepeatInterfaceCode::CAMERA_REMOVE_SKETCH_STREAM_REPEAT):
53 errCode = RemoveSketchStreamRepeat();
54 break;
55 case static_cast<uint32_t>(StreamRepeatInterfaceCode::CAMERA_UPDATE_SKETCH_RATIO):
56 errCode = HandleUpdateSketchRatio(data);
57 break;
58 case static_cast<uint32_t>(StreamRepeatInterfaceCode::CAMERA_STREAM_FRAME_RANGE_SET):
59 errCode = HandleSetFrameRate(data);
60 break;
61 case static_cast<uint32_t>(StreamRepeatInterfaceCode::CAMERA_ENABLE_SECURE_STREAM):
62 errCode = EnableSecure(data.ReadBool());
63 break;
64 case static_cast<uint32_t>(StreamRepeatInterfaceCode::CAMERA_ENABLE_STREAM_MIRROR):
65 errCode = HandleSetMirror(data);
66 break;
67 case static_cast<uint32_t>(StreamRepeatInterfaceCode::CAMERA_ATTACH_META_SURFACE):
68 errCode = HandleAttachMetaSurface(data);
69 break;
70 case static_cast<uint32_t>(StreamRepeatInterfaceCode::CAMERA_PRIVIEW_ROTATION):
71 errCode = HandleSetCameraRotation(data);
72 break;
73 default:
74 MEDIA_ERR_LOG("HStreamRepeatStub request code %{public}u not handled", code);
75 errCode = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
76 break;
77 }
78
79 return errCode;
80 }
81
HandleSetCallback(MessageParcel & data)82 int32_t HStreamRepeatStub::HandleSetCallback(MessageParcel& data)
83 {
84 auto remoteObject = data.ReadRemoteObject();
85 CHECK_ERROR_RETURN_RET_LOG(remoteObject == nullptr, IPC_STUB_INVALID_DATA_ERR,
86 "HStreamRepeatStub HandleSetCallback StreamRepeatCallback is null");
87
88 auto callback = iface_cast<IStreamRepeatCallback>(remoteObject);
89 CHECK_ERROR_RETURN_RET_LOG(callback == nullptr, IPC_STUB_INVALID_DATA_ERR,
90 "HStreamRepeatStub HandleSetCallback callback is null");
91
92 return SetCallback(callback);
93 }
94
HandleAddDeferredSurface(MessageParcel & data)95 int32_t HStreamRepeatStub::HandleAddDeferredSurface(MessageParcel& data)
96 {
97 CHECK_ERROR_RETURN_RET(!CheckSystemApp(), CAMERA_NO_PERMISSION);
98
99 sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
100 CHECK_ERROR_RETURN_RET_LOG(remoteObj == nullptr, IPC_STUB_INVALID_DATA_ERR,
101 "HStreamRepeatStub HandleAddDeferredSurface BufferProducer is null");
102
103 sptr<OHOS::IBufferProducer> producer = iface_cast<OHOS::IBufferProducer>(remoteObj);
104 CHECK_ERROR_RETURN_RET_LOG(producer == nullptr, IPC_STUB_INVALID_DATA_ERR,
105 "HStreamRepeatStub HandleAddDeferredSurface producer is null");
106
107 int ret = AddDeferredSurface(producer);
108 CHECK_ERROR_PRINT_LOG(ret != ERR_NONE,
109 "HStreamRepeatStub::HandleAddDeferredSurface add deferred surface failed : %{public}d", ret);
110
111 return ret;
112 }
113
HandleForkSketchStreamRepeat(MessageParcel & data,MessageParcel & reply)114 int32_t HStreamRepeatStub::HandleForkSketchStreamRepeat(MessageParcel& data, MessageParcel& reply)
115 {
116 CHECK_ERROR_RETURN_RET(!CheckSystemApp(), CAMERA_NO_PERMISSION);
117
118 sptr<IStreamRepeat> sketchStream = nullptr;
119 int32_t width = data.ReadInt32();
120 int32_t height = data.ReadInt32();
121 float sketchRatio = data.ReadFloat();
122 int ret = ForkSketchStreamRepeat(width, height, sketchStream, sketchRatio);
123 CHECK_ERROR_RETURN_RET_LOG(ret != ERR_NONE, ret,
124 "HStreamRepeatStub::HandleForkSketchStreamRepeat failed : %{public}d", ret);
125 CHECK_ERROR_RETURN_RET_LOG(!reply.WriteRemoteObject(sketchStream->AsObject()), IPC_STUB_WRITE_PARCEL_ERR,
126 "HStreamRepeatStub HandleForkSketchStreamRepeat Write sketchStream obj failed");
127
128 return ret;
129 }
130
HandleUpdateSketchRatio(MessageParcel & data)131 int32_t HStreamRepeatStub::HandleUpdateSketchRatio(MessageParcel& data)
132 {
133 CHECK_ERROR_RETURN_RET(!CheckSystemApp(), CAMERA_NO_PERMISSION);
134 float sketchRatio = data.ReadFloat();
135 // SketchRatio value could be negative value
136 CHECK_ERROR_RETURN_RET_LOG(sketchRatio > SKETCH_RATIO_MAX_VALUE, IPC_STUB_INVALID_DATA_ERR,
137 "HStreamRepeatStub HandleUpdateSketchRatio sketchRatio value is illegal %{public}f", sketchRatio);
138 return UpdateSketchRatio(sketchRatio);
139 }
140
HandleSetFrameRate(MessageParcel & data)141 int32_t HStreamRepeatStub::HandleSetFrameRate(MessageParcel& data)
142 {
143 int32_t minFrameRate = data.ReadInt32();
144 int32_t maxFrameRate = data.ReadInt32();
145
146 int ret = SetFrameRate(minFrameRate, maxFrameRate);
147 CHECK_ERROR_PRINT_LOG(ret != ERR_NONE, "HStreamRepeatStub::HandleSetFrameRate failed : %{public}d", ret);
148 return ret;
149 }
150
HandleSetCameraRotation(MessageParcel & data)151 int32_t HStreamRepeatStub::HandleSetCameraRotation(MessageParcel& data)
152 {
153 bool isEnable = data.ReadBool();
154 int32_t rotation = data.ReadInt32();
155
156 int ret = SetCameraRotation(isEnable, rotation);
157 CHECK_ERROR_PRINT_LOG(ret != ERR_NONE, "HStreamRepeatStub::HandleSetFrameRate failed : %{public}d", ret);
158 return ret;
159 }
160
HandleSetMirror(MessageParcel & data)161 int32_t HStreamRepeatStub::HandleSetMirror(MessageParcel& data)
162 {
163 bool isEnable = data.ReadBool();
164
165 int ret = SetMirror(isEnable);
166 CHECK_ERROR_PRINT_LOG(ret != ERR_NONE, "HStreamRepeatStub::HandleSetMirror failed : %{public}d", ret);
167 return ret;
168 }
169
HandleAttachMetaSurface(MessageParcel & data)170 int32_t HStreamRepeatStub::HandleAttachMetaSurface(MessageParcel& data)
171 {
172 sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
173 int32_t videoMetaType = data.ReadInt32();
174
175 CHECK_AND_RETURN_RET_LOG(remoteObj != nullptr, IPC_STUB_INVALID_DATA_ERR,
176 "HStreamRepeatStub HandleAttachMetaSurface BufferProducer is null");
177
178 sptr<OHOS::IBufferProducer> producer = iface_cast<OHOS::IBufferProducer>(remoteObj);
179 CHECK_AND_RETURN_RET_LOG(producer != nullptr, IPC_STUB_INVALID_DATA_ERR,
180 "HStreamRepeatStub HandleAttachMetaSurface producer is null");
181 int errCode = AttachMetaSurface(producer, videoMetaType);
182 CHECK_ERROR_RETURN_RET_LOG(errCode != ERR_NONE, errCode,
183 "HStreamRepeatStub::HandleAttachMetaSurface add deferred surface failed : %{public}d", errCode);
184
185 return errCode;
186 }
187 } // namespace CameraStandard
188 } // namespace OHOS
189