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_util.h"
19 #include "ipc_skeleton.h"
20 #include "remote_request_code.h"
21
22 namespace OHOS {
23 namespace CameraStandard {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)24 int HCaptureSessionStub::OnRemoteRequest(
25 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
26 {
27 DisableJeMalloc();
28 int errCode = -1;
29
30 if (data.ReadInterfaceToken() != GetDescriptor()) {
31 return errCode;
32 }
33 switch (code) {
34 case CAMERA_CAPTURE_SESSION_BEGIN_CONFIG:
35 errCode = BeginConfig();
36 break;
37 case CAMERA_CAPTURE_SESSION_ADD_INPUT:
38 errCode = HCaptureSessionStub::HandleAddInput(data);
39 break;
40 case CAMERA_CAPTURE_SESSION_ADD_OUTPUT:
41 errCode = HCaptureSessionStub::HandleAddOutput(data);
42 break;
43 case CAMERA_CAPTURE_SESSION_REMOVE_INPUT:
44 errCode = HCaptureSessionStub::HandleRemoveInput(data);
45 break;
46 case CAMERA_CAPTURE_SESSION_REMOVE_OUTPUT:
47 errCode = HCaptureSessionStub::HandleRemoveOutput(data);
48 break;
49 case CAMERA_CAPTURE_SESSION_COMMIT_CONFIG:
50 errCode = CommitConfig();
51 break;
52 case CAMERA_CAPTURE_SESSION_START:
53 errCode = Start();
54 break;
55 case CAMERA_CAPTURE_SESSION_STOP:
56 errCode = Stop();
57 break;
58 case CAMERA_CAPTURE_SESSION_RELEASE: {
59 pid_t pid = IPCSkeleton::GetCallingPid();
60 errCode = Release(pid);
61 }
62 break;
63 case CAMERA_CAPTURE_SESSION_SET_CALLBACK:
64 errCode = HandleSetCallback(data);
65 break;
66 case CAMERA_CAPTURE_GET_SESSION_STATE:
67 errCode = HandleGetSesstionState(reply);
68 break;
69 default:
70 MEDIA_ERR_LOG("HCaptureSessionStub request code %{public}u not handled", code);
71 errCode = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
72 break;
73 }
74
75 return errCode;
76 }
77
HandleAddInput(MessageParcel & data)78 int HCaptureSessionStub::HandleAddInput(MessageParcel &data)
79 {
80 sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
81 if (remoteObj == nullptr) {
82 MEDIA_ERR_LOG("HCaptureSessionStub HandleAddInput CameraDevice is null");
83 return IPC_STUB_INVALID_DATA_ERR;
84 }
85
86 sptr<ICameraDeviceService> cameraDevice = iface_cast<ICameraDeviceService>(remoteObj);
87
88 return AddInput(cameraDevice);
89 }
90
HandleRemoveInput(MessageParcel & data)91 int HCaptureSessionStub::HandleRemoveInput(MessageParcel &data)
92 {
93 sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
94 if (remoteObj == nullptr) {
95 MEDIA_ERR_LOG("HCaptureSessionStub HandleRemoveInput CameraDevice is null");
96 return IPC_STUB_INVALID_DATA_ERR;
97 }
98
99 sptr<ICameraDeviceService> cameraDevice = iface_cast<ICameraDeviceService>(remoteObj);
100
101 return RemoveInput(cameraDevice);
102 }
103
HandleAddOutput(MessageParcel & data)104 int HCaptureSessionStub::HandleAddOutput(MessageParcel &data)
105 {
106 StreamType streamType = static_cast<StreamType>(data.ReadUint32());
107 sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
108 if (remoteObj == nullptr) {
109 MEDIA_ERR_LOG("HCaptureSessionStub HandleAddOutput remoteObj is null");
110 return IPC_STUB_INVALID_DATA_ERR;
111 }
112
113 sptr<IStreamCommon> stream = iface_cast<IStreamCommon>(remoteObj);
114
115 return AddOutput(streamType, stream);
116 }
117
HandleRemoveOutput(MessageParcel & data)118 int HCaptureSessionStub::HandleRemoveOutput(MessageParcel &data)
119 {
120 StreamType streamType = static_cast<StreamType>(data.ReadUint32());
121 sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
122 if (remoteObj == nullptr) {
123 MEDIA_ERR_LOG("HCaptureSessionStub HandleRemoveOutput remoteObj is null");
124 return IPC_STUB_INVALID_DATA_ERR;
125 }
126
127 sptr<IStreamCommon> stream = iface_cast<IStreamCommon>(remoteObj);
128
129 return RemoveOutput(streamType, stream);
130 }
131
HandleSetCallback(MessageParcel & data)132 int HCaptureSessionStub::HandleSetCallback(MessageParcel &data)
133 {
134 auto remoteObject = data.ReadRemoteObject();
135 if (remoteObject == nullptr) {
136 MEDIA_ERR_LOG("HCaptureSessionStub HandleSetCallback CaptureSessionCallback is null");
137 return IPC_STUB_INVALID_DATA_ERR;
138 }
139
140 auto callback = iface_cast<ICaptureSessionCallback>(remoteObject);
141
142 return SetCallback(callback);
143 }
144
HandleGetSesstionState(MessageParcel & reply)145 int HCaptureSessionStub::HandleGetSesstionState(MessageParcel &reply)
146 {
147 CaptureSessionState sessionState;
148 int32_t ret = GetSessionState(sessionState);
149 if (!reply.WriteUint32(static_cast<uint32_t>(sessionState))) {
150 MEDIA_ERR_LOG("HCaptureSessionStub HandleGetSesstionState Write sessionState failed");
151 return IPC_STUB_WRITE_PARCEL_ERR;
152 }
153 return ret;
154 }
155 } // namespace CameraStandard
156 } // namespace OHOS
157