1 /*
2  * Copyright (c) 2022-2024 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_stub.h"
17 #include "avsession_errors.h"
18 #include "iavsession_callback.h"
19 #include "key_event.h"
20 #include "avsession_log.h"
21 #include "avsession_trace.h"
22 
23 namespace OHOS::AVSession {
OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)24 int32_t AVSessionCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply,
25     MessageOption& option)
26 {
27     if (!CheckInterfaceToken(data)) {
28         return AVSESSION_ERROR;
29     }
30     if (code >= static_cast<uint32_t>(IAVSessionCallback::SESSION_CALLBACK_ON_PLAY)
31         && code < static_cast<uint32_t>(IAVSessionCallback::SESSION_CALLBACK_MAX)) {
32         return handlers[code](data, reply);
33     }
34     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
35 }
36 
CheckInterfaceToken(MessageParcel& data)37 bool AVSessionCallbackStub::CheckInterfaceToken(MessageParcel& data)
38 {
39     auto localDescriptor = IAVSessionCallback::GetDescriptor();
40     auto remoteDescriptor = data.ReadInterfaceToken();
41     if (remoteDescriptor != localDescriptor) {
42         SLOGI("interface token is not equal");
43         return false;
44     }
45     return true;
46 }
47 
HandleOnAVCallAnswer(MessageParcel& data, MessageParcel& reply)48 int32_t AVSessionCallbackStub::HandleOnAVCallAnswer(MessageParcel& data, MessageParcel& reply)
49 {
50     AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnPlay");
51     OnAVCallAnswer();
52     return ERR_NONE;
53 }
54 
HandleOnAVCallHangUp(MessageParcel& data, MessageParcel& reply)55 int32_t AVSessionCallbackStub::HandleOnAVCallHangUp(MessageParcel& data, MessageParcel& reply)
56 {
57     AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnPlay");
58     OnAVCallHangUp();
59     return ERR_NONE;
60 }
61 
HandleOnAVCallToggleCallMute(MessageParcel& data, MessageParcel& reply)62 int32_t AVSessionCallbackStub::HandleOnAVCallToggleCallMute(MessageParcel& data, MessageParcel& reply)
63 {
64     AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnPlay");
65     OnAVCallToggleCallMute();
66     return ERR_NONE;
67 }
68 
HandleOnPlay(MessageParcel& data, MessageParcel& reply)69 int32_t AVSessionCallbackStub::HandleOnPlay(MessageParcel& data, MessageParcel& reply)
70 {
71     AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnPlay");
72     OnPlay();
73     return ERR_NONE;
74 }
75 
HandleOnPause(MessageParcel& data, MessageParcel& reply)76 int32_t AVSessionCallbackStub::HandleOnPause(MessageParcel& data, MessageParcel& reply)
77 {
78     AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnPause");
79     OnPause();
80     return ERR_NONE;
81 }
82 
HandleOnStop(MessageParcel& data, MessageParcel& reply)83 int32_t AVSessionCallbackStub::HandleOnStop(MessageParcel& data, MessageParcel& reply)
84 {
85     AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnStop");
86     OnStop();
87     return ERR_NONE;
88 }
89 
HandleOnPlayNext(MessageParcel& data, MessageParcel& reply)90 int32_t AVSessionCallbackStub::HandleOnPlayNext(MessageParcel& data, MessageParcel& reply)
91 {
92     AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnPlayNext");
93     OnPlayNext();
94     return ERR_NONE;
95 }
96 
HandleOnPlayPrevious(MessageParcel& data, MessageParcel& reply)97 int32_t AVSessionCallbackStub::HandleOnPlayPrevious(MessageParcel& data, MessageParcel& reply)
98 {
99     AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnPlayPrevious");
100     OnPlayPrevious();
101     return ERR_NONE;
102 }
103 
HandleOnFastForward(MessageParcel& data, MessageParcel& reply)104 int32_t AVSessionCallbackStub::HandleOnFastForward(MessageParcel& data, MessageParcel& reply)
105 {
106     AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnFastForward");
107     int32_t time = -1;
108     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(time), ERR_NONE, "read time failed");
109     OnFastForward(time);
110     return ERR_NONE;
111 }
112 
HandleOnRewind(MessageParcel& data, MessageParcel& reply)113 int32_t AVSessionCallbackStub::HandleOnRewind(MessageParcel& data, MessageParcel& reply)
114 {
115     AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnRewind");
116     int32_t time = -1;
117     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(time), ERR_NONE, "read time failed");
118     OnRewind(time);
119     return ERR_NONE;
120 }
121 
HandleOnSeek(MessageParcel& data, MessageParcel& reply)122 int32_t AVSessionCallbackStub::HandleOnSeek(MessageParcel& data, MessageParcel& reply)
123 {
124     AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnSeek");
125     int32_t time = -1;
126     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(time), ERR_NONE, "read time failed");
127     OnSeek(time);
128     return ERR_NONE;
129 }
130 
HandleOnSetSpeed(MessageParcel& data, MessageParcel& reply)131 int32_t AVSessionCallbackStub::HandleOnSetSpeed(MessageParcel& data, MessageParcel& reply)
132 {
133     AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnSetSpeed");
134     double speed = 0.0;
135     CHECK_AND_RETURN_RET_LOG(data.ReadDouble(speed), ERR_NONE, "read speed failed");
136     OnSetSpeed(speed);
137     return ERR_NONE;
138 }
139 
HandleOnSetLoopMode(MessageParcel& data, MessageParcel& reply)140 int32_t AVSessionCallbackStub::HandleOnSetLoopMode(MessageParcel& data, MessageParcel& reply)
141 {
142     AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnSetLoopMode");
143     int32_t loopMode = -1;
144     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(loopMode), ERR_NONE, "read loopMode failed");
145     OnSetLoopMode(loopMode);
146     return ERR_NONE;
147 }
148 
HandleOnToggleFavorite(MessageParcel& data, MessageParcel& reply)149 int32_t AVSessionCallbackStub::HandleOnToggleFavorite(MessageParcel& data, MessageParcel& reply)
150 {
151     AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnToggleFavorite");
152     std::string mediaId;
153     CHECK_AND_RETURN_RET_LOG(data.ReadString(mediaId), ERR_NONE, "read mediaId failed");
154     OnToggleFavorite(mediaId);
155     return ERR_NONE;
156 }
157 
HandleOnMediaKeyEvent(MessageParcel& data, MessageParcel& reply)158 int32_t AVSessionCallbackStub::HandleOnMediaKeyEvent(MessageParcel& data, MessageParcel& reply)
159 {
160     AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnMediaKeyEvent");
161     auto keyEvent = MMI::KeyEvent::Create();
162     if (keyEvent == nullptr) {
163         SLOGE("HandleOnMediaKeyEvent get key event null");
164         return ERR_NONE;
165     }
166     CHECK_AND_RETURN_RET_LOG((*keyEvent).ReadFromParcel(data), ERR_NONE, "read keyEvent failed");
167     OnMediaKeyEvent(*keyEvent);
168     return ERR_NONE;
169 }
170 
HandleOnOutputDeviceChange(MessageParcel& data, MessageParcel& reply)171 int32_t AVSessionCallbackStub::HandleOnOutputDeviceChange(MessageParcel& data, MessageParcel& reply)
172 {
173     AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnOutputDeviceChange");
174     int32_t connectionState;
175     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(connectionState), false, "write deviceInfoSize failed");
176 
177     OutputDeviceInfo outputDeviceInfo;
178     int32_t deviceInfoSize;
179     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfoSize), false, "write deviceInfoSize failed");
180     int32_t maxDeviceInfoSize = 1000; // A maximum of 1000 device change events can be processed at a time
181     CHECK_AND_RETURN_RET_LOG((deviceInfoSize >= 0) && (deviceInfoSize < maxDeviceInfoSize),
182         false, "deviceInfoSize is illegal");
183     for (int i = 0; i < deviceInfoSize; i++) {
184         DeviceInfo deviceInfo;
185         CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.castCategory_), false, "Read castCategory failed");
186         CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.deviceId_), false, "Read deviceId failed");
187         CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.deviceName_), false, "Read deviceName failed");
188         CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.deviceType_), false, "Read deviceType failed");
189         CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.ipAddress_), false, "Read ipAddress failed");
190         CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.manufacturer_), false, "Read manufacturer failed");
191         CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.modelName_), false, "Read modelName failed");
192         CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.providerId_), false, "Read providerId failed");
193         CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.supportedProtocols_), false,
194             "Read supportedProtocols failed");
195         CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.authenticationStatus_), false,
196             "Read authenticationStatus failed");
197         int32_t supportedDrmCapabilityLen = 0;
198         CHECK_AND_RETURN_RET_LOG(data.ReadInt32(supportedDrmCapabilityLen), false,
199             "read supportedDrmCapabilityLen failed");
200         std::vector<std::string> supportedDrmCapabilities;
201         int32_t maxSupportedDrmCapabilityLen = 10;
202         CHECK_AND_RETURN_RET_LOG((supportedDrmCapabilityLen >= 0) &&
203             (supportedDrmCapabilityLen <= maxSupportedDrmCapabilityLen), false, "supportedDrmCapabilityLen is illegal");
204         for (int j = 0; j < supportedDrmCapabilityLen; j++) {
205             std::string supportedDrmCapability;
206             CHECK_AND_RETURN_RET_LOG(data.ReadString(supportedDrmCapability), false,
207                 "read supportedDrmCapability failed");
208             supportedDrmCapabilities.emplace_back(supportedDrmCapability);
209         }
210         deviceInfo.supportedDrmCapabilities_ = supportedDrmCapabilities;
211         CHECK_AND_RETURN_RET_LOG(data.ReadBool(deviceInfo.isLegacy_), false, "Read isLegacy failed");
212         CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.mediumTypes_), false, "Read mediumTypes failed");
213         outputDeviceInfo.deviceInfos_.emplace_back(deviceInfo);
214     }
215 
216     OnOutputDeviceChange(connectionState, outputDeviceInfo);
217     return ERR_NONE;
218 }
219 
HandleOnCommonCommand(MessageParcel& data, MessageParcel& reply)220 int32_t AVSessionCallbackStub::HandleOnCommonCommand(MessageParcel& data, MessageParcel& reply)
221 {
222     AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnCommonCommand");
223     auto commonCommand = data.ReadString();
224     sptr commonArgs = data.ReadParcelable<AAFwk::WantParams>();
225     CHECK_AND_RETURN_RET_LOG(commonArgs != nullptr, ERR_NONE, "Read common args failed");
226     OnCommonCommand(commonCommand, *commonArgs);
227     return ERR_NONE;
228 }
229 
HandleOnSkipToQueueItem(MessageParcel& data, MessageParcel& reply)230 int32_t AVSessionCallbackStub::HandleOnSkipToQueueItem(MessageParcel& data, MessageParcel& reply)
231 {
232     AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnSkipToQueueItem");
233     int32_t itemId = -1;
234     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(itemId), ERR_NONE, "read itemId failed");
235     OnSkipToQueueItem(itemId);
236     return ERR_NONE;
237 }
238 
HandleOnPlayFromAssetId(MessageParcel& data, MessageParcel& reply)239 int32_t AVSessionCallbackStub::HandleOnPlayFromAssetId(MessageParcel& data, MessageParcel& reply)
240 {
241     AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnPlayFromAssetId");
242     int64_t assetId = -1;
243     CHECK_AND_RETURN_RET_LOG(data.ReadInt64(assetId), ERR_NONE, "read time failed");
244     OnPlayFromAssetId(assetId);
245     return ERR_NONE;
246 }
247 
HandleOnCastDisplayChange(MessageParcel& data, MessageParcel& reply)248 int32_t AVSessionCallbackStub::HandleOnCastDisplayChange(MessageParcel& data, MessageParcel& reply)
249 {
250     AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnCastDisplayChange");
251     CastDisplayInfo castDisplayInfo;
252     int32_t displayState = -1;
253     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(displayState), ERR_NONE, "read displayState failed");
254     castDisplayInfo.displayState = static_cast<CastDisplayState>(displayState);
255     uint64_t displayId = 0;
256     CHECK_AND_RETURN_RET_LOG(data.ReadUint64(displayId), ERR_NONE, "read displayId failed");
257     castDisplayInfo.displayId = displayId;
258     std::string name{};
259     CHECK_AND_RETURN_RET_LOG(data.ReadString(name), ERR_NONE, "read name failed");
260     castDisplayInfo.name = name;
261     int32_t width = -1;
262     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(width), ERR_NONE, "read width failed");
263     castDisplayInfo.width = width;
264     int32_t height = -1;
265     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(height), ERR_NONE, "read height failed");
266     castDisplayInfo.height = height;
267     OnCastDisplayChange(castDisplayInfo);
268     return ERR_NONE;
269 }
270 } // namespace OHOS::AVSession
271