1 /*
2  * Copyright (c) 2022-2023 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 "avsession_callback_proxy.h"
17 #include "avsession_log.h"
18 
19 namespace OHOS::AVSession {
AVSessionCallbackProxy(const sptr<IRemoteObject>& impl)20 AVSessionCallbackProxy::AVSessionCallbackProxy(const sptr<IRemoteObject>& impl)
21     : IRemoteProxy<IAVSessionCallback>(impl)
22 {
23     SLOGD("construct");
24 }
25 
OnAVCallAnswer()26 void AVSessionCallbackProxy::OnAVCallAnswer()
27 {
28     MessageParcel data;
29     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
30 
31     auto remote = Remote();
32     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
33     MessageParcel reply;
34     MessageOption option = { MessageOption::TF_ASYNC };
35     CHECK_AND_RETURN_LOG(remote->SendRequest(SESSION_CALLBACK_ON_AVCALL_ANSWER, data, reply, option) == 0,
36         "send request failed");
37 }
38 
OnAVCallHangUp()39 void AVSessionCallbackProxy::OnAVCallHangUp()
40 {
41     MessageParcel data;
42     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
43 
44     auto remote = Remote();
45     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
46     MessageParcel reply;
47     MessageOption option = { MessageOption::TF_ASYNC };
48     CHECK_AND_RETURN_LOG(remote->SendRequest(SESSION_CALLBACK_ON_AVCALL_HANGUP, data, reply, option) == 0,
49         "send request failed");
50 }
51 
OnAVCallToggleCallMute()52 void AVSessionCallbackProxy::OnAVCallToggleCallMute()
53 {
54     MessageParcel data;
55     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
56 
57     auto remote = Remote();
58     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
59     MessageParcel reply;
60     MessageOption option = { MessageOption::TF_ASYNC };
61     CHECK_AND_RETURN_LOG(remote->SendRequest(SESSION_CALLBACK_ON_AVCALL_TOGGLE_CALL_MUTE, data, reply, option) == 0,
62         "send request failed");
63 }
64 
OnPlay()65 void AVSessionCallbackProxy::OnPlay()
66 {
67     MessageParcel data;
68     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
69 
70     auto remote = Remote();
71     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
72     MessageParcel reply;
73     MessageOption option = { MessageOption::TF_ASYNC };
74     CHECK_AND_RETURN_LOG(remote->SendRequest(SESSION_CALLBACK_ON_PLAY, data, reply, option) == 0,
75         "send request failed");
76 }
77 
OnPause()78 void AVSessionCallbackProxy::OnPause()
79 {
80     MessageParcel data;
81     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
82 
83     auto remote = Remote();
84     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
85     MessageParcel reply;
86     MessageOption option = { MessageOption::TF_ASYNC };
87     CHECK_AND_RETURN_LOG(remote->SendRequest(SESSION_CALLBACK_ON_PAUSE, data, reply, option) == 0,
88         "send request failed");
89 }
90 
OnStop()91 void AVSessionCallbackProxy::OnStop()
92 {
93     MessageParcel data;
94     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
95 
96     auto remote = Remote();
97     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
98     MessageParcel reply;
99     MessageOption option = { MessageOption::TF_ASYNC };
100     CHECK_AND_RETURN_LOG(remote->SendRequest(SESSION_CALLBACK_ON_STOP, data, reply, option) == 0,
101         "send request failed");
102 }
103 
OnPlayNext()104 void AVSessionCallbackProxy::OnPlayNext()
105 {
106     MessageParcel data;
107     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
108 
109     auto remote = Remote();
110     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
111     MessageParcel reply;
112     MessageOption option = { MessageOption::TF_ASYNC };
113     CHECK_AND_RETURN_LOG(remote->SendRequest(SESSION_CALLBACK_ON_PLAY_NEXT, data, reply, option) == 0,
114         "send request failed");
115 }
116 
OnPlayPrevious()117 void AVSessionCallbackProxy::OnPlayPrevious()
118 {
119     MessageParcel data;
120     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
121 
122     auto remote = Remote();
123     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
124     MessageParcel reply;
125     MessageOption option = { MessageOption::TF_ASYNC };
126     CHECK_AND_RETURN_LOG(remote->SendRequest(SESSION_CALLBACK_ON_PLAY_PREVIOUS, data, reply, option) == 0,
127         "send request failed");
128 }
129 
OnFastForward(int64_t time)130 void AVSessionCallbackProxy::OnFastForward(int64_t time)
131 {
132     MessageParcel data;
133     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
134     CHECK_AND_RETURN_LOG(data.WriteInt64(time), "write time failed");
135 
136     auto remote = Remote();
137     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
138     MessageParcel reply;
139     MessageOption option = { MessageOption::TF_ASYNC };
140     CHECK_AND_RETURN_LOG(remote->SendRequest(SESSION_CALLBACK_ON_FAST_FORWARD, data, reply, option) == 0,
141         "send request failed");
142 }
143 
OnRewind(int64_t time)144 void AVSessionCallbackProxy::OnRewind(int64_t time)
145 {
146     MessageParcel data;
147     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
148     CHECK_AND_RETURN_LOG(data.WriteInt64(time), "write time failed");
149 
150     auto remote = Remote();
151     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
152     MessageParcel reply;
153     MessageOption option = { MessageOption::TF_ASYNC };
154     CHECK_AND_RETURN_LOG(remote->SendRequest(SESSION_CALLBACK_ON_REWIND, data, reply, option) == 0,
155         "send request failed");
156 }
157 
OnSeek(int64_t time)158 void AVSessionCallbackProxy::OnSeek(int64_t time)
159 {
160     MessageParcel data;
161     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
162     CHECK_AND_RETURN_LOG(data.WriteInt64(time), "write time failed");
163 
164     auto remote = Remote();
165     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
166     MessageParcel reply;
167     MessageOption option = { MessageOption::TF_ASYNC };
168     CHECK_AND_RETURN_LOG(remote->SendRequest(SESSION_CALLBACK_ON_SEEK, data, reply, option) == 0,
169         "OnSeek send request failed");
170 }
171 
OnSetSpeed(double speed)172 void AVSessionCallbackProxy::OnSetSpeed(double speed)
173 {
174     MessageParcel data;
175     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
176     CHECK_AND_RETURN_LOG(data.WriteDouble(speed), "write speed failed");
177 
178     auto remote = Remote();
179     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
180     MessageParcel reply;
181     MessageOption option = { MessageOption::TF_ASYNC };
182     CHECK_AND_RETURN_LOG(remote->SendRequest(SESSION_CALLBACK_ON_SET_SPEED, data, reply, option) == 0,
183         "OnSetSpeed send request failed");
184 }
185 
OnSetLoopMode(int32_t loopMode)186 void AVSessionCallbackProxy::OnSetLoopMode(int32_t loopMode)
187 {
188     MessageParcel data;
189     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
190     CHECK_AND_RETURN_LOG(data.WriteInt32(loopMode), "write loopMode failed");
191 
192     auto remote = Remote();
193     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
194     MessageParcel reply;
195     MessageOption option = { MessageOption::TF_ASYNC };
196     CHECK_AND_RETURN_LOG(remote->SendRequest(SESSION_CALLBACK_ON_SET_LOOPMODE, data, reply, option) == 0,
197         "send request failed");
198 }
199 
OnToggleFavorite(const std::string& mediaId)200 void AVSessionCallbackProxy::OnToggleFavorite(const std::string& mediaId)
201 {
202     MessageParcel data;
203     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
204     CHECK_AND_RETURN_LOG(data.WriteString(mediaId), "write mediaId failed");
205 
206     auto remote = Remote();
207     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
208     MessageParcel reply;
209     MessageOption option = { MessageOption::TF_ASYNC };
210     CHECK_AND_RETURN_LOG(remote->SendRequest(SESSION_CALLBACK_ON_TOGGLE_FAVORITE, data, reply, option) == 0,
211         "send request failed");
212 }
213 
OnMediaKeyEvent(const MMI::KeyEvent& keyEvent)214 void AVSessionCallbackProxy::OnMediaKeyEvent(const MMI::KeyEvent& keyEvent)
215 {
216     MessageParcel data;
217     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
218     CHECK_AND_RETURN_LOG(keyEvent.WriteToParcel(data), "write keyEvent failed");
219 
220     auto remote = Remote();
221     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
222     MessageParcel reply;
223     MessageOption option = { MessageOption::TF_ASYNC };
224     CHECK_AND_RETURN_LOG(remote->SendRequest(SESSION_CALLBACK_ON_MEDIA_KEY_EVENT, data, reply, option) == 0,
225         "send request failed");
226 }
227 
OnOutputDeviceChange(const int32_t connectionState, const OutputDeviceInfo& outputDeviceInfo)228 void AVSessionCallbackProxy::OnOutputDeviceChange(const int32_t connectionState,
229     const OutputDeviceInfo& outputDeviceInfo)
230 {
231     MessageParcel data;
232     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
233     CHECK_AND_RETURN_LOG(data.WriteInt32(connectionState), "write connectionState failed");
234 
235     int32_t deviceInfoSize = static_cast<int32_t>(outputDeviceInfo.deviceInfos_.size());
236     CHECK_AND_RETURN_LOG(data.WriteInt32(deviceInfoSize), "write deviceInfoSize failed");
237     for (DeviceInfo deviceInfo : outputDeviceInfo.deviceInfos_) {
238         CHECK_AND_RETURN_LOG(data.WriteInt32(deviceInfo.castCategory_), "write castCategory failed");
239         CHECK_AND_RETURN_LOG(data.WriteString(deviceInfo.deviceId_), "write deviceId failed");
240         CHECK_AND_RETURN_LOG(data.WriteString(deviceInfo.deviceName_), "write deviceName failed");
241         CHECK_AND_RETURN_LOG(data.WriteInt32(deviceInfo.deviceType_), "write deviceType failed");
242         CHECK_AND_RETURN_LOG(data.WriteString(deviceInfo.ipAddress_), "write ipAddress failed");
243         CHECK_AND_RETURN_LOG(data.WriteString(deviceInfo.manufacturer_), "write manufacturer failed");
244         CHECK_AND_RETURN_LOG(data.WriteString(deviceInfo.modelName_), "write modelName failed");
245         CHECK_AND_RETURN_LOG(data.WriteInt32(deviceInfo.providerId_), "write providerId failed");
246         CHECK_AND_RETURN_LOG(data.WriteInt32(deviceInfo.supportedProtocols_),
247             "write supportedProtocols failed");
248         CHECK_AND_RETURN_LOG(data.WriteInt32(deviceInfo.authenticationStatus_),
249             "write authenticationStatus failed");
250         CHECK_AND_RETURN_LOG(data.WriteInt32(deviceInfo.supportedDrmCapabilities_.size()),
251             "write supportedDrmCapabilities size failed");
252         for (auto supportedDrmCapability : deviceInfo.supportedDrmCapabilities_) {
253             CHECK_AND_RETURN_LOG(data.WriteString(supportedDrmCapability),
254                 "write supportedDrmCapability failed");
255         }
256         CHECK_AND_RETURN_LOG(data.WriteBool(deviceInfo.isLegacy_), "write isLegacy failed");
257         CHECK_AND_RETURN_LOG(data.WriteInt32(deviceInfo.mediumTypes_), "write mediumTypes failed");
258     }
259 
260     auto remote = Remote();
261     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
262     MessageParcel reply;
263     MessageOption option = { MessageOption::TF_ASYNC };
264     CHECK_AND_RETURN_LOG(remote->SendRequest(SESSION_CALLBACK_ON_OUTPUT_DEVICE_CHANGE, data, reply, option) == 0,
265                          "send request failed");
266     SLOGI("outputdevice change send connect state %{public}d", connectionState);
267 }
268 
OnCommonCommand(const std::string& commonCommand, const AAFwk::WantParams& commandArgs)269 void AVSessionCallbackProxy::OnCommonCommand(const std::string& commonCommand,
270     const AAFwk::WantParams& commandArgs)
271 {
272     MessageParcel parcel;
273     CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "Write interface token failed");
274     CHECK_AND_RETURN_LOG(parcel.WriteString(commonCommand), "Write event string failed");
275     CHECK_AND_RETURN_LOG(parcel.WriteParcelable(&commandArgs), "Write Want failed");
276     MessageParcel reply;
277     MessageOption option = { MessageOption::TF_ASYNC };
278     auto remote = Remote();
279     CHECK_AND_RETURN_LOG(remote != nullptr, "Get remote service failed");
280     CHECK_AND_RETURN_LOG(remote->SendRequest(SESSION_CALLBACK_ON_SEND_COMMON_COMMAND, parcel, reply, option) == 0,
281         "Send request failed");
282 }
283 
OnSkipToQueueItem(int32_t itemId)284 void AVSessionCallbackProxy::OnSkipToQueueItem(int32_t itemId)
285 {
286     MessageParcel data;
287     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
288     CHECK_AND_RETURN_LOG(data.WriteInt32(itemId), "write itemId failed");
289 
290     auto remote = Remote();
291     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
292     MessageParcel reply;
293     MessageOption option = { MessageOption::TF_ASYNC };
294     CHECK_AND_RETURN_LOG(remote->SendRequest(SESSION_CALLBACK_ON_SKIP_TO_QUEUE_ITEM, data, reply, option) == 0,
295         "send request failed");
296 }
297 
OnPlayFromAssetId(int64_t assetId)298 void AVSessionCallbackProxy::OnPlayFromAssetId(int64_t assetId)
299 {
300     MessageParcel data;
301     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
302     CHECK_AND_RETURN_LOG(data.WriteInt64(assetId), "write assetId failed");
303 
304     auto remote = Remote();
305     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
306     MessageParcel reply;
307     MessageOption option = { MessageOption::TF_ASYNC };
308     CHECK_AND_RETURN_LOG(remote->SendRequest(SESSION_CALLBACK_ON_PLAY_FROM_ASSETID, data, reply, option) == 0,
309         "OnPlayFromAssetId send request failed");
310 }
311 
OnCastDisplayChange(const CastDisplayInfo& castDisplayInfo)312 void AVSessionCallbackProxy::OnCastDisplayChange(const CastDisplayInfo& castDisplayInfo)
313 {
314     MessageParcel data;
315     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
316     CHECK_AND_RETURN_LOG(data.WriteInt32(static_cast<int32_t>(castDisplayInfo.displayState)),
317         "write displayState failed");
318     CHECK_AND_RETURN_LOG(data.WriteUint64(castDisplayInfo.displayId), "write displayId failed");
319     CHECK_AND_RETURN_LOG(data.WriteString(castDisplayInfo.name), "write name failed");
320     CHECK_AND_RETURN_LOG(data.WriteInt32(castDisplayInfo.width), "write width failed");
321     CHECK_AND_RETURN_LOG(data.WriteInt32(castDisplayInfo.height), "write height failed");
322 
323     auto remote = Remote();
324     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
325     MessageParcel reply;
326     MessageOption option = { MessageOption::TF_ASYNC };
327     CHECK_AND_RETURN_LOG(remote->SendRequest(SESSION_CALLBACK_ON_CAST_DISPLAY_CHANGE, data, reply, option) == 0,
328                          "OnCastDisplayChange send request failed");
329 }
330 } // namespace OHOS::AVSession
331