• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
HandleOnSetTargetLoopMode(MessageParcel & data,MessageParcel & reply)149 int32_t AVSessionCallbackStub::HandleOnSetTargetLoopMode(MessageParcel& data, MessageParcel& reply)
150 {
151     AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnSetTargetLoopMode");
152     int32_t targetLoopMode = -1;
153     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(targetLoopMode), ERR_NONE, "read targetLoopMode failed");
154     OnSetTargetLoopMode(targetLoopMode);
155     return ERR_NONE;
156 }
157 
HandleOnToggleFavorite(MessageParcel & data,MessageParcel & reply)158 int32_t AVSessionCallbackStub::HandleOnToggleFavorite(MessageParcel& data, MessageParcel& reply)
159 {
160     AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnToggleFavorite");
161     std::string mediaId;
162     CHECK_AND_RETURN_RET_LOG(data.ReadString(mediaId), ERR_NONE, "read mediaId failed");
163     OnToggleFavorite(mediaId);
164     return ERR_NONE;
165 }
166 
HandleOnMediaKeyEvent(MessageParcel & data,MessageParcel & reply)167 int32_t AVSessionCallbackStub::HandleOnMediaKeyEvent(MessageParcel& data, MessageParcel& reply)
168 {
169     AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnMediaKeyEvent");
170     auto keyEvent = MMI::KeyEvent::Create();
171     if (keyEvent == nullptr) {
172         SLOGE("HandleOnMediaKeyEvent get key event null");
173         return ERR_NONE;
174     }
175     CHECK_AND_RETURN_RET_LOG((*keyEvent).ReadFromParcel(data), ERR_NONE, "read keyEvent failed");
176     OnMediaKeyEvent(*keyEvent);
177     return ERR_NONE;
178 }
179 
HandleOnOutputDeviceChange(MessageParcel & data,MessageParcel & reply)180 int32_t AVSessionCallbackStub::HandleOnOutputDeviceChange(MessageParcel& data, MessageParcel& reply)
181 {
182     AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnOutputDeviceChange");
183     int32_t connectionState;
184     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(connectionState), false, "write deviceInfoSize failed");
185 
186     OutputDeviceInfo outputDeviceInfo;
187     int32_t deviceInfoSize;
188     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfoSize), false, "write deviceInfoSize failed");
189     int32_t maxDeviceInfoSize = 1000; // A maximum of 1000 device change events can be processed at a time
190     CHECK_AND_RETURN_RET_LOG((deviceInfoSize >= 0) && (deviceInfoSize < maxDeviceInfoSize),
191         false, "deviceInfoSize is illegal");
192     for (int i = 0; i < deviceInfoSize; i++) {
193         DeviceInfo deviceInfo;
194         CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.castCategory_), false, "Read castCategory failed");
195         CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.deviceId_), false, "Read deviceId failed");
196         CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.deviceName_), false, "Read deviceName failed");
197         CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.deviceType_), false, "Read deviceType failed");
198         CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.ipAddress_), false, "Read ipAddress failed");
199         CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.networkId_), false, "Read networkId failed");
200         CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.manufacturer_), false, "Read manufacturer failed");
201         CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.modelName_), false, "Read modelName failed");
202         CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.providerId_), false, "Read providerId failed");
203         CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.supportedProtocols_), false,
204             "Read supportedProtocols failed");
205         CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.authenticationStatus_), false,
206             "Read authenticationStatus failed");
207         int32_t supportedDrmCapabilityLen = 0;
208         CHECK_AND_RETURN_RET_LOG(data.ReadInt32(supportedDrmCapabilityLen), false,
209             "read supportedDrmCapabilityLen failed");
210         std::vector<std::string> supportedDrmCapabilities;
211         int32_t maxSupportedDrmCapabilityLen = 10;
212         CHECK_AND_RETURN_RET_LOG((supportedDrmCapabilityLen >= 0) &&
213             (supportedDrmCapabilityLen <= maxSupportedDrmCapabilityLen), false, "supportedDrmCapabilityLen is illegal");
214         for (int j = 0; j < supportedDrmCapabilityLen; j++) {
215             std::string supportedDrmCapability;
216             CHECK_AND_RETURN_RET_LOG(data.ReadString(supportedDrmCapability), false,
217                 "read supportedDrmCapability failed");
218             supportedDrmCapabilities.emplace_back(supportedDrmCapability);
219         }
220         deviceInfo.supportedDrmCapabilities_ = supportedDrmCapabilities;
221         CHECK_AND_RETURN_RET_LOG(data.ReadBool(deviceInfo.isLegacy_), false, "Read isLegacy failed");
222         CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.mediumTypes_), false, "Read mediumTypes failed");
223         outputDeviceInfo.deviceInfos_.emplace_back(deviceInfo);
224     }
225 
226     OnOutputDeviceChange(connectionState, outputDeviceInfo);
227     return ERR_NONE;
228 }
229 
HandleOnCommonCommand(MessageParcel & data,MessageParcel & reply)230 int32_t AVSessionCallbackStub::HandleOnCommonCommand(MessageParcel& data, MessageParcel& reply)
231 {
232     AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnCommonCommand");
233     auto commonCommand = data.ReadString();
234     sptr commonArgs = data.ReadParcelable<AAFwk::WantParams>();
235     CHECK_AND_RETURN_RET_LOG(commonArgs != nullptr, ERR_NONE, "Read common args failed");
236     OnCommonCommand(commonCommand, *commonArgs);
237     return ERR_NONE;
238 }
239 
HandleOnSkipToQueueItem(MessageParcel & data,MessageParcel & reply)240 int32_t AVSessionCallbackStub::HandleOnSkipToQueueItem(MessageParcel& data, MessageParcel& reply)
241 {
242     AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnSkipToQueueItem");
243     int32_t itemId = -1;
244     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(itemId), ERR_NONE, "read itemId failed");
245     OnSkipToQueueItem(itemId);
246     return ERR_NONE;
247 }
248 
HandleOnPlayFromAssetId(MessageParcel & data,MessageParcel & reply)249 int32_t AVSessionCallbackStub::HandleOnPlayFromAssetId(MessageParcel& data, MessageParcel& reply)
250 {
251     AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnPlayFromAssetId");
252     int64_t assetId = -1;
253     CHECK_AND_RETURN_RET_LOG(data.ReadInt64(assetId), ERR_NONE, "read time failed");
254     OnPlayFromAssetId(assetId);
255     return ERR_NONE;
256 }
257 
HandleOnCastDisplayChange(MessageParcel & data,MessageParcel & reply)258 int32_t AVSessionCallbackStub::HandleOnCastDisplayChange(MessageParcel& data, MessageParcel& reply)
259 {
260     AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnCastDisplayChange");
261     CastDisplayInfo castDisplayInfo;
262     int32_t displayState = -1;
263     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(displayState), ERR_NONE, "read displayState failed");
264     castDisplayInfo.displayState = static_cast<CastDisplayState>(displayState);
265     uint64_t displayId = 0;
266     CHECK_AND_RETURN_RET_LOG(data.ReadUint64(displayId), ERR_NONE, "read displayId failed");
267     castDisplayInfo.displayId = displayId;
268     std::string name{};
269     CHECK_AND_RETURN_RET_LOG(data.ReadString(name), ERR_NONE, "read name failed");
270     castDisplayInfo.name = name;
271     int32_t width = -1;
272     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(width), ERR_NONE, "read width failed");
273     castDisplayInfo.width = width;
274     int32_t height = -1;
275     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(height), ERR_NONE, "read height failed");
276     castDisplayInfo.height = height;
277     OnCastDisplayChange(castDisplayInfo);
278     return ERR_NONE;
279 }
280 } // namespace OHOS::AVSession
281