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     auto remote = Remote();
136     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
137     MessageParcel reply;
138     MessageOption option = { MessageOption::TF_ASYNC };
139     CHECK_AND_RETURN_LOG(remote->SendRequest(SESSION_CALLBACK_ON_FAST_FORWARD, data, reply, option) == 0,
140         "send request failed");
141 }
142 
OnRewind(int64_t time)143 void AVSessionCallbackProxy::OnRewind(int64_t time)
144 {
145     MessageParcel data;
146     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
147     CHECK_AND_RETURN_LOG(data.WriteInt64(time), "write time failed");
148     auto remote = Remote();
149     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
150     MessageParcel reply;
151     MessageOption option = { MessageOption::TF_ASYNC };
152     CHECK_AND_RETURN_LOG(remote->SendRequest(SESSION_CALLBACK_ON_REWIND, data, reply, option) == 0,
153         "send request failed");
154 }
155 
OnSeek(int64_t time)156 void AVSessionCallbackProxy::OnSeek(int64_t time)
157 {
158     MessageParcel data;
159     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
160     CHECK_AND_RETURN_LOG(data.WriteInt64(time), "write time failed");
161 
162     auto remote = Remote();
163     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
164     MessageParcel reply;
165     MessageOption option = { MessageOption::TF_ASYNC };
166     CHECK_AND_RETURN_LOG(remote->SendRequest(SESSION_CALLBACK_ON_SEEK, data, reply, option) == 0,
167         "OnSeek send request failed");
168 }
169 
OnSetSpeed(double speed)170 void AVSessionCallbackProxy::OnSetSpeed(double speed)
171 {
172     MessageParcel data;
173     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
174     CHECK_AND_RETURN_LOG(data.WriteDouble(speed), "write speed failed");
175 
176     auto remote = Remote();
177     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
178     MessageParcel reply;
179     MessageOption option = { MessageOption::TF_ASYNC };
180     CHECK_AND_RETURN_LOG(remote->SendRequest(SESSION_CALLBACK_ON_SET_SPEED, data, reply, option) == 0,
181         "OnSetSpeed send request failed");
182 }
183 
OnSetLoopMode(int32_t loopMode)184 void AVSessionCallbackProxy::OnSetLoopMode(int32_t loopMode)
185 {
186     MessageParcel data;
187     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
188     CHECK_AND_RETURN_LOG(data.WriteInt32(loopMode), "write loopMode failed");
189 
190     auto remote = Remote();
191     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
192     MessageParcel reply;
193     MessageOption option = { MessageOption::TF_ASYNC };
194     CHECK_AND_RETURN_LOG(remote->SendRequest(SESSION_CALLBACK_ON_SET_LOOPMODE, data, reply, option) == 0,
195         "send request failed");
196 }
197 
OnToggleFavorite(const std::string & mediaId)198 void AVSessionCallbackProxy::OnToggleFavorite(const std::string& mediaId)
199 {
200     MessageParcel data;
201     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
202     CHECK_AND_RETURN_LOG(data.WriteString(mediaId), "write mediaId failed");
203 
204     auto remote = Remote();
205     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
206     MessageParcel reply;
207     MessageOption option = { MessageOption::TF_ASYNC };
208     CHECK_AND_RETURN_LOG(remote->SendRequest(SESSION_CALLBACK_ON_TOGGLE_FAVORITE, data, reply, option) == 0,
209         "send request failed");
210 }
211 
OnMediaKeyEvent(const MMI::KeyEvent & keyEvent)212 void AVSessionCallbackProxy::OnMediaKeyEvent(const MMI::KeyEvent& keyEvent)
213 {
214     MessageParcel data;
215     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
216     CHECK_AND_RETURN_LOG(keyEvent.WriteToParcel(data), "write keyEvent failed");
217 
218     auto remote = Remote();
219     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
220     MessageParcel reply;
221     MessageOption option = { MessageOption::TF_ASYNC };
222     CHECK_AND_RETURN_LOG(remote->SendRequest(SESSION_CALLBACK_ON_MEDIA_KEY_EVENT, data, reply, option) == 0,
223         "send request failed");
224 }
225 
OnOutputDeviceChange(const int32_t connectionState,const OutputDeviceInfo & outputDeviceInfo)226 void AVSessionCallbackProxy::OnOutputDeviceChange(const int32_t connectionState,
227     const OutputDeviceInfo& outputDeviceInfo)
228 {
229     MessageParcel data;
230     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
231     CHECK_AND_RETURN_LOG(data.WriteInt32(connectionState), "write connectionState failed");
232 
233     int32_t deviceInfoSize = static_cast<int32_t>(outputDeviceInfo.deviceInfos_.size());
234     CHECK_AND_RETURN_LOG(data.WriteInt32(deviceInfoSize), "write deviceInfoSize failed");
235     for (DeviceInfo deviceInfo : outputDeviceInfo.deviceInfos_) {
236         CHECK_AND_RETURN_LOG(data.WriteInt32(deviceInfo.castCategory_), "write castCategory failed");
237         CHECK_AND_RETURN_LOG(data.WriteString(deviceInfo.deviceId_), "write deviceId failed");
238         CHECK_AND_RETURN_LOG(data.WriteString(deviceInfo.deviceName_), "write deviceName failed");
239         CHECK_AND_RETURN_LOG(data.WriteInt32(deviceInfo.deviceType_), "write deviceType failed");
240         CHECK_AND_RETURN_LOG(data.WriteString(deviceInfo.ipAddress_), "write ipAddress failed");
241         CHECK_AND_RETURN_LOG(data.WriteString(deviceInfo.manufacturer_), "write manufacturer failed");
242         CHECK_AND_RETURN_LOG(data.WriteString(deviceInfo.modelName_), "write modelName failed");
243         CHECK_AND_RETURN_LOG(data.WriteInt32(deviceInfo.providerId_), "write providerId failed");
244         CHECK_AND_RETURN_LOG(data.WriteInt32(deviceInfo.supportedProtocols_),
245             "write supportedProtocols failed");
246         CHECK_AND_RETURN_LOG(data.WriteInt32(deviceInfo.authenticationStatus_),
247             "write authenticationStatus failed");
248         CHECK_AND_RETURN_LOG(data.WriteInt32(deviceInfo.supportedDrmCapabilities_.size()),
249             "write supportedDrmCapabilities size failed");
250         for (auto supportedDrmCapability : deviceInfo.supportedDrmCapabilities_) {
251             CHECK_AND_RETURN_LOG(data.WriteString(supportedDrmCapability),
252                 "write supportedDrmCapability failed");
253         }
254         CHECK_AND_RETURN_LOG(data.WriteBool(deviceInfo.isLegacy_), "write isLegacy failed");
255         CHECK_AND_RETURN_LOG(data.WriteInt32(deviceInfo.mediumTypes_), "write mediumTypes failed");
256     }
257 
258     auto remote = Remote();
259     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
260     MessageParcel reply;
261     MessageOption option = { MessageOption::TF_ASYNC };
262     CHECK_AND_RETURN_LOG(remote->SendRequest(SESSION_CALLBACK_ON_OUTPUT_DEVICE_CHANGE, data, reply, option) == 0,
263                          "send request failed");
264     SLOGI("outputdevice change send connect state %{public}d", connectionState);
265 }
266 
OnCommonCommand(const std::string & commonCommand,const AAFwk::WantParams & commandArgs)267 void AVSessionCallbackProxy::OnCommonCommand(const std::string& commonCommand,
268     const AAFwk::WantParams& commandArgs)
269 {
270     MessageParcel parcel;
271     CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "Write interface token failed");
272     CHECK_AND_RETURN_LOG(parcel.WriteString(commonCommand), "Write event string failed");
273     CHECK_AND_RETURN_LOG(parcel.WriteParcelable(&commandArgs), "Write Want failed");
274     MessageParcel reply;
275     MessageOption option = { MessageOption::TF_ASYNC };
276     auto remote = Remote();
277     CHECK_AND_RETURN_LOG(remote != nullptr, "Get remote service failed");
278     CHECK_AND_RETURN_LOG(remote->SendRequest(SESSION_CALLBACK_ON_SEND_COMMON_COMMAND, parcel, reply, option) == 0,
279         "Send request failed");
280 }
281 
OnSkipToQueueItem(int32_t itemId)282 void AVSessionCallbackProxy::OnSkipToQueueItem(int32_t itemId)
283 {
284     MessageParcel data;
285     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
286     CHECK_AND_RETURN_LOG(data.WriteInt32(itemId), "write itemId failed");
287 
288     auto remote = Remote();
289     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
290     MessageParcel reply;
291     MessageOption option = { MessageOption::TF_ASYNC };
292     CHECK_AND_RETURN_LOG(remote->SendRequest(SESSION_CALLBACK_ON_SKIP_TO_QUEUE_ITEM, data, reply, option) == 0,
293         "send request failed");
294 }
295 
OnPlayFromAssetId(int64_t assetId)296 void AVSessionCallbackProxy::OnPlayFromAssetId(int64_t assetId)
297 {
298     MessageParcel data;
299     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
300     CHECK_AND_RETURN_LOG(data.WriteInt64(assetId), "write assetId failed");
301 
302     auto remote = Remote();
303     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
304     MessageParcel reply;
305     MessageOption option = { MessageOption::TF_ASYNC };
306     CHECK_AND_RETURN_LOG(remote->SendRequest(SESSION_CALLBACK_ON_PLAY_FROM_ASSETID, data, reply, option) == 0,
307         "OnPlayFromAssetId send request failed");
308 }
309 
OnCastDisplayChange(const CastDisplayInfo & castDisplayInfo)310 void AVSessionCallbackProxy::OnCastDisplayChange(const CastDisplayInfo& castDisplayInfo)
311 {
312     MessageParcel data;
313     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
314     CHECK_AND_RETURN_LOG(data.WriteInt32(static_cast<int32_t>(castDisplayInfo.displayState)),
315         "write displayState failed");
316     CHECK_AND_RETURN_LOG(data.WriteUint64(castDisplayInfo.displayId), "write displayId failed");
317     CHECK_AND_RETURN_LOG(data.WriteString(castDisplayInfo.name), "write name failed");
318     CHECK_AND_RETURN_LOG(data.WriteInt32(castDisplayInfo.width), "write width failed");
319     CHECK_AND_RETURN_LOG(data.WriteInt32(castDisplayInfo.height), "write height failed");
320 
321     auto remote = Remote();
322     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
323     MessageParcel reply;
324     MessageOption option = { MessageOption::TF_ASYNC };
325     CHECK_AND_RETURN_LOG(remote->SendRequest(SESSION_CALLBACK_ON_CAST_DISPLAY_CHANGE, data, reply, option) == 0,
326                          "OnCastDisplayChange send request failed");
327 }
328 } // namespace OHOS::AVSession
329