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         case static_cast<uint32_t>(StreamRepeatInterfaceCode::CAMERA_API_VERSION):
74             errCode = HandleSetCameraApi(data);
75             break;
76         default:
77             MEDIA_ERR_LOG("HStreamRepeatStub request code %{public}u not handled", code);
78             errCode = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
79             break;
80     }
81 
82     return errCode;
83 }
84 
HandleSetCallback(MessageParcel & data)85 int32_t HStreamRepeatStub::HandleSetCallback(MessageParcel& data)
86 {
87     auto remoteObject = data.ReadRemoteObject();
88     CHECK_ERROR_RETURN_RET_LOG(remoteObject == nullptr, IPC_STUB_INVALID_DATA_ERR,
89         "HStreamRepeatStub HandleSetCallback StreamRepeatCallback is null");
90 
91     auto callback = iface_cast<IStreamRepeatCallback>(remoteObject);
92     CHECK_ERROR_RETURN_RET_LOG(callback == nullptr, IPC_STUB_INVALID_DATA_ERR,
93         "HStreamRepeatStub HandleSetCallback callback is null");
94 
95     return SetCallback(callback);
96 }
97 
HandleAddDeferredSurface(MessageParcel & data)98 int32_t HStreamRepeatStub::HandleAddDeferredSurface(MessageParcel& data)
99 {
100     CHECK_ERROR_RETURN_RET(!CheckSystemApp(), CAMERA_NO_PERMISSION);
101 
102     sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
103     CHECK_ERROR_RETURN_RET_LOG(remoteObj == nullptr, IPC_STUB_INVALID_DATA_ERR,
104         "HStreamRepeatStub HandleAddDeferredSurface BufferProducer is null");
105 
106     sptr<OHOS::IBufferProducer> producer = iface_cast<OHOS::IBufferProducer>(remoteObj);
107     CHECK_ERROR_RETURN_RET_LOG(producer == nullptr, IPC_STUB_INVALID_DATA_ERR,
108         "HStreamRepeatStub HandleAddDeferredSurface producer is null");
109 
110     int ret = AddDeferredSurface(producer);
111     CHECK_ERROR_PRINT_LOG(ret != ERR_NONE,
112         "HStreamRepeatStub::HandleAddDeferredSurface add deferred surface failed : %{public}d", ret);
113 
114     return ret;
115 }
116 
HandleForkSketchStreamRepeat(MessageParcel & data,MessageParcel & reply)117 int32_t HStreamRepeatStub::HandleForkSketchStreamRepeat(MessageParcel& data, MessageParcel& reply)
118 {
119     CHECK_ERROR_RETURN_RET(!CheckSystemApp(), CAMERA_NO_PERMISSION);
120 
121     sptr<IStreamRepeat> sketchStream = nullptr;
122     int32_t width = data.ReadInt32();
123     int32_t height = data.ReadInt32();
124     float sketchRatio = data.ReadFloat();
125     int ret = ForkSketchStreamRepeat(width, height, sketchStream, sketchRatio);
126     CHECK_ERROR_RETURN_RET_LOG(ret != ERR_NONE, ret,
127         "HStreamRepeatStub::HandleForkSketchStreamRepeat failed : %{public}d", ret);
128     CHECK_ERROR_RETURN_RET_LOG(!reply.WriteRemoteObject(sketchStream->AsObject()), IPC_STUB_WRITE_PARCEL_ERR,
129         "HStreamRepeatStub HandleForkSketchStreamRepeat Write sketchStream obj failed");
130 
131     return ret;
132 }
133 
HandleUpdateSketchRatio(MessageParcel & data)134 int32_t HStreamRepeatStub::HandleUpdateSketchRatio(MessageParcel& data)
135 {
136     CHECK_ERROR_RETURN_RET(!CheckSystemApp(), CAMERA_NO_PERMISSION);
137     float sketchRatio = data.ReadFloat();
138     // SketchRatio value could be negative value
139     CHECK_ERROR_RETURN_RET_LOG(sketchRatio > SKETCH_RATIO_MAX_VALUE, IPC_STUB_INVALID_DATA_ERR,
140         "HStreamRepeatStub HandleUpdateSketchRatio sketchRatio value is illegal %{public}f", sketchRatio);
141     return UpdateSketchRatio(sketchRatio);
142 }
143 
HandleSetFrameRate(MessageParcel & data)144 int32_t HStreamRepeatStub::HandleSetFrameRate(MessageParcel& data)
145 {
146     int32_t minFrameRate = data.ReadInt32();
147     int32_t maxFrameRate = data.ReadInt32();
148 
149     int ret = SetFrameRate(minFrameRate, maxFrameRate);
150     CHECK_ERROR_PRINT_LOG(ret != ERR_NONE, "HStreamRepeatStub::HandleSetFrameRate failed : %{public}d", ret);
151     return ret;
152 }
153 
HandleSetCameraRotation(MessageParcel & data)154 int32_t HStreamRepeatStub::HandleSetCameraRotation(MessageParcel& data)
155 {
156     bool isEnable = data.ReadBool();
157     int32_t rotation = data.ReadInt32();
158 
159     int ret = SetCameraRotation(isEnable, rotation);
160     CHECK_ERROR_PRINT_LOG(ret != ERR_NONE, "HStreamRepeatStub::SetCameraRotation failed : %{public}d", ret);
161     return ret;
162 }
163 
HandleSetCameraApi(MessageParcel & data)164 int32_t HStreamRepeatStub::HandleSetCameraApi(MessageParcel& data)
165 {
166     uint32_t apiCompatibleVersion = data.ReadUint32();
167 
168     int ret = SetCameraApi(apiCompatibleVersion);
169     CHECK_ERROR_PRINT_LOG(ret != ERR_NONE, "HStreamRepeatStub::SetCameraApi failed : %{public}d", ret);
170     return ret;
171 }
172 
HandleSetMirror(MessageParcel & data)173 int32_t HStreamRepeatStub::HandleSetMirror(MessageParcel& data)
174 {
175     bool isEnable = data.ReadBool();
176 
177     int ret = SetMirror(isEnable);
178     CHECK_ERROR_PRINT_LOG(ret != ERR_NONE, "HStreamRepeatStub::HandleSetMirror failed : %{public}d", ret);
179     return ret;
180 }
181 
HandleAttachMetaSurface(MessageParcel & data)182 int32_t HStreamRepeatStub::HandleAttachMetaSurface(MessageParcel& data)
183 {
184     sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
185     int32_t videoMetaType = data.ReadInt32();
186 
187     CHECK_AND_RETURN_RET_LOG(remoteObj != nullptr, IPC_STUB_INVALID_DATA_ERR,
188         "HStreamRepeatStub HandleAttachMetaSurface BufferProducer is null");
189 
190     sptr<OHOS::IBufferProducer> producer = iface_cast<OHOS::IBufferProducer>(remoteObj);
191     CHECK_AND_RETURN_RET_LOG(producer != nullptr, IPC_STUB_INVALID_DATA_ERR,
192                              "HStreamRepeatStub HandleAttachMetaSurface producer is null");
193     int errCode = AttachMetaSurface(producer, videoMetaType);
194     CHECK_ERROR_RETURN_RET_LOG(errCode != ERR_NONE, errCode,
195         "HStreamRepeatStub::HandleAttachMetaSurface add deferred surface failed : %{public}d", errCode);
196 
197     return errCode;
198 }
199 } // namespace CameraStandard
200 } // namespace OHOS
201