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_proxy.h"
17 
18 #include "camera_log.h"
19 #include "camera_service_ipc_interface_code.h"
20 
21 namespace OHOS {
22 namespace CameraStandard {
23 static constexpr float SKETCH_RATIO_MAX_VALUE = 100.0f;
HStreamRepeatProxy(const sptr<IRemoteObject> & impl)24 HStreamRepeatProxy::HStreamRepeatProxy(const sptr<IRemoteObject>& impl) : IRemoteProxy<IStreamRepeat>(impl) {}
25 
~HStreamRepeatProxy()26 HStreamRepeatProxy::~HStreamRepeatProxy()
27 {
28     MEDIA_INFO_LOG("~HStreamRepeatProxy is called");
29 }
30 
Start()31 int32_t HStreamRepeatProxy::Start()
32 {
33     MessageParcel data;
34     MessageParcel reply;
35     MessageOption option;
36 
37     data.WriteInterfaceToken(GetDescriptor());
38     int error = Remote()->SendRequest(
39         static_cast<uint32_t>(StreamRepeatInterfaceCode::CAMERA_START_VIDEO_RECORDING), data, reply, option);
40     if (error != ERR_NONE) {
41         MEDIA_ERR_LOG("HStreamRepeatProxy Start failed, error: %{public}d", error);
42     }
43 
44     return error;
45 }
46 
Stop()47 int32_t HStreamRepeatProxy::Stop()
48 {
49     MessageParcel data;
50     MessageParcel reply;
51     MessageOption option;
52 
53     data.WriteInterfaceToken(GetDescriptor());
54     int error = Remote()->SendRequest(
55         static_cast<uint32_t>(StreamRepeatInterfaceCode::CAMERA_STOP_VIDEO_RECORDING), data, reply, option);
56     if (error != ERR_NONE) {
57         MEDIA_ERR_LOG("HStreamRepeatProxy Stop failed, error: %{public}d", error);
58     }
59 
60     return error;
61 }
62 
Release()63 int32_t HStreamRepeatProxy::Release()
64 {
65     MessageParcel data;
66     MessageParcel reply;
67     MessageOption option;
68 
69     data.WriteInterfaceToken(GetDescriptor());
70     int error = Remote()->SendRequest(
71         static_cast<uint32_t>(StreamRepeatInterfaceCode::CAMERA_STREAM_REPEAT_RELEASE), data, reply, option);
72     if (error != ERR_NONE) {
73         MEDIA_ERR_LOG("HStreamRepeatProxy Stop failed, error: %{public}d", error);
74     }
75     return error;
76 }
77 
SetCallback(sptr<IStreamRepeatCallback> & callback)78 int32_t HStreamRepeatProxy::SetCallback(sptr<IStreamRepeatCallback>& callback)
79 {
80     MessageParcel data;
81     MessageParcel reply;
82     MessageOption option;
83 
84     if (callback == nullptr) {
85         MEDIA_ERR_LOG("HStreamRepeatProxy SetCallback callback is null");
86         return IPC_PROXY_ERR;
87     }
88 
89     data.WriteInterfaceToken(GetDescriptor());
90     data.WriteRemoteObject(callback->AsObject());
91 
92     int error = Remote()->SendRequest(
93         static_cast<uint32_t>(StreamRepeatInterfaceCode::CAMERA_STREAM_REPEAT_SET_CALLBACK), data, reply, option);
94     if (error != ERR_NONE) {
95         MEDIA_ERR_LOG("HStreamRepeatProxy SetCallback failed, error: %{public}d", error);
96     }
97 
98     return error;
99 }
100 
AddDeferredSurface(const sptr<OHOS::IBufferProducer> & producer)101 int32_t HStreamRepeatProxy::AddDeferredSurface(const sptr<OHOS::IBufferProducer>& producer)
102 {
103     MessageParcel data;
104     MessageParcel reply;
105     MessageOption option;
106 
107     if (producer == nullptr) {
108         MEDIA_ERR_LOG("HStreamRepeatProxy AddDeferredSurface producer is null");
109         return IPC_PROXY_ERR;
110     }
111 
112     data.WriteInterfaceToken(GetDescriptor());
113     data.WriteRemoteObject(producer->AsObject());
114     int error = Remote()->SendRequest(
115         static_cast<uint32_t>(StreamRepeatInterfaceCode::CAMERA_ADD_DEFERRED_SURFACE), data, reply, option);
116     if (error != ERR_NONE) {
117         MEDIA_ERR_LOG("HStreamRepeatProxy::AddDeferredSurface failed, error: %{public}d", error);
118         return error;
119     }
120 
121     return error;
122 }
123 
ForkSketchStreamRepeat(int32_t width,int32_t height,sptr<IStreamRepeat> & sketchStream,float sketchRatio)124 int32_t HStreamRepeatProxy::ForkSketchStreamRepeat(
125     int32_t width, int32_t height, sptr<IStreamRepeat>& sketchStream, float sketchRatio)
126 {
127     MessageParcel data;
128     MessageParcel reply;
129     MessageOption option;
130 
131     if (width <= 0 || height <= 0) {
132         MEDIA_ERR_LOG("HStreamRepeatProxy ForkSketchStreamRepeat producer is null or invalid size is set");
133         return IPC_PROXY_ERR;
134     }
135 
136     data.WriteInterfaceToken(GetDescriptor());
137     data.WriteInt32(width);
138     data.WriteInt32(height);
139     data.WriteFloat(sketchRatio);
140 
141     int error = Remote()->SendRequest(
142         static_cast<uint32_t>(StreamRepeatInterfaceCode::CAMERA_FORK_SKETCH_STREAM_REPEAT), data, reply, option);
143     auto remoteObject = reply.ReadRemoteObject();
144     if (remoteObject != nullptr) {
145         sketchStream = iface_cast<IStreamRepeat>(remoteObject);
146     } else {
147         MEDIA_ERR_LOG("HCameraServiceProxy ForkSketchStreamRepeat sketchStream is null");
148         error = IPC_PROXY_ERR;
149     }
150     return error;
151 }
152 
UpdateSketchRatio(float sketchRatio)153 int32_t HStreamRepeatProxy::UpdateSketchRatio(float sketchRatio)
154 {
155     MessageParcel data;
156     MessageParcel reply;
157     MessageOption option;
158 
159     // SketchRatio value could be negative value
160     if (sketchRatio > SKETCH_RATIO_MAX_VALUE) {
161         MEDIA_ERR_LOG("HStreamRepeatProxy UpdateSketchRatio value is illegal %{public}f", sketchRatio);
162         return IPC_PROXY_ERR;
163     }
164 
165     data.WriteInterfaceToken(GetDescriptor());
166     data.WriteFloat(sketchRatio);
167     int error = Remote()->SendRequest(
168         static_cast<uint32_t>(StreamRepeatInterfaceCode::CAMERA_UPDATE_SKETCH_RATIO), data, reply, option);
169     if (error != ERR_NONE) {
170         MEDIA_ERR_LOG("HStreamRepeatProxy UpdateSketchRatio failed, error: %{public}d", error);
171     }
172     return error;
173 }
174 
RemoveSketchStreamRepeat()175 int32_t HStreamRepeatProxy::RemoveSketchStreamRepeat()
176 {
177     MessageParcel data;
178     MessageParcel reply;
179     MessageOption option;
180 
181     data.WriteInterfaceToken(GetDescriptor());
182     int error = Remote()->SendRequest(
183         static_cast<uint32_t>(StreamRepeatInterfaceCode::CAMERA_REMOVE_SKETCH_STREAM_REPEAT), data, reply, option);
184     if (error != ERR_NONE) {
185         MEDIA_ERR_LOG("HStreamRepeatProxy Stop failed, error: %{public}d", error);
186     }
187     return error;
188 }
189 
SetFrameRate(int32_t minFrameRate,int32_t maxFrameRate)190 int32_t HStreamRepeatProxy::SetFrameRate(int32_t minFrameRate, int32_t maxFrameRate)
191 {
192     MessageParcel data;
193     MessageParcel reply;
194     MessageOption option;
195 
196     data.WriteInterfaceToken(GetDescriptor());
197     data.WriteInt32(minFrameRate);
198     data.WriteInt32(maxFrameRate);
199 
200     int error = Remote()->SendRequest(
201         static_cast<uint32_t>(StreamRepeatInterfaceCode::CAMERA_STREAM_FRAME_RANGE_SET), data, reply, option);
202     if (error != ERR_NONE) {
203         MEDIA_ERR_LOG("HStreamRepeatProxy SetFrameRate failed, error: %{public}d", error);
204     }
205     return error;
206 }
207 
SetCameraRotation(bool isEnable,int32_t rotation)208 int32_t HStreamRepeatProxy::SetCameraRotation(bool isEnable, int32_t rotation)
209 {
210     MessageParcel data;
211     MessageParcel reply;
212     MessageOption option;
213 
214     data.WriteInterfaceToken(GetDescriptor());
215     data.WriteBool(isEnable);
216     data.WriteInt32(rotation);
217 
218     int error = Remote()->SendRequest(
219         static_cast<uint32_t>(StreamRepeatInterfaceCode::CAMERA_PRIVIEW_ROTATION), data, reply, option);
220     if (error != ERR_NONE) {
221         MEDIA_ERR_LOG("HStreamRepeatProxy SetCameraRotation failed, error: %{public}d", error);
222     }
223     return error;
224 }
225 
SetCameraApi(uint32_t apiCompatibleVersion)226 int32_t HStreamRepeatProxy::SetCameraApi(uint32_t apiCompatibleVersion)
227 {
228     MessageParcel data;
229     MessageParcel reply;
230     MessageOption option;
231 
232     data.WriteInterfaceToken(GetDescriptor());
233     data.WriteUint32(apiCompatibleVersion);
234 
235     int error = Remote()->SendRequest(
236         static_cast<uint32_t>(StreamRepeatInterfaceCode::CAMERA_API_VERSION), data, reply, option);
237     if (error != ERR_NONE) {
238         MEDIA_ERR_LOG("HStreamRepeatProxy SetCameraApi failed, error: %{public}d", error);
239     }
240     return error;
241 }
242 
EnableSecure(bool isEnable)243 int32_t HStreamRepeatProxy::EnableSecure(bool isEnable)
244 {
245     MessageParcel data;
246     MessageParcel reply;
247     MessageOption option;
248 
249     data.WriteInterfaceToken(GetDescriptor());
250     data.WriteBool(isEnable);
251     int error = Remote()->SendRequest(
252         static_cast<uint32_t>(StreamRepeatInterfaceCode::CAMERA_ENABLE_SECURE_STREAM), data, reply, option);
253     if (error != ERR_NONE) {
254         MEDIA_ERR_LOG("HStreamRepeatProxy EnableSecure failed, error: %{public}d", error);
255     }
256     return error;
257 }
258 
SetMirror(bool isEnable)259 int32_t HStreamRepeatProxy::SetMirror(bool isEnable)
260 {
261     MessageParcel data;
262     MessageParcel reply;
263     MessageOption option;
264 
265     data.WriteInterfaceToken(GetDescriptor());
266     data.WriteBool(isEnable);
267     int error = Remote()->SendRequest(
268         static_cast<uint32_t>(StreamRepeatInterfaceCode::CAMERA_ENABLE_STREAM_MIRROR), data, reply, option);
269     if (error != ERR_NONE) {
270         MEDIA_ERR_LOG("HStreamRepeatProxy SetMirror failed, error: %{public}d", error);
271     }
272     return error;
273 }
274 
AttachMetaSurface(const sptr<OHOS::IBufferProducer> & producer,int32_t videoMetaType)275 int32_t HStreamRepeatProxy::AttachMetaSurface(const sptr<OHOS::IBufferProducer>& producer, int32_t videoMetaType)
276 {
277     MessageParcel data;
278     MessageParcel reply;
279     MessageOption option;
280 
281     CHECK_ERROR_RETURN_RET_LOG(producer == nullptr, IPC_PROXY_ERR,
282         "HStreamRepeatProxy AttachMetaSurface producer is null");
283 
284     data.WriteInterfaceToken(GetDescriptor());
285     data.WriteRemoteObject(producer->AsObject());
286     data.WriteInt32(videoMetaType);
287     int error = Remote()->SendRequest(
288         static_cast<uint32_t>(StreamRepeatInterfaceCode::CAMERA_ATTACH_META_SURFACE), data, reply, option);
289     CHECK_ERROR_RETURN_RET_LOG(error != ERR_NONE, error,
290         "HStreamRepeatProxy::AttachMetaSurface failed, error: %{public}d", error);
291 
292     return error;
293 }
294 } // namespace CameraStandard
295 } // namespace OHOS
296