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_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 < SESSION_CALLBACK_MAX) {
31 return (this->*handlers[code])(data, reply);
32 }
33 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
34 }
35
CheckInterfaceToken(MessageParcel & data)36 bool AVSessionCallbackStub::CheckInterfaceToken(MessageParcel& data)
37 {
38 auto localDescriptor = IAVSessionCallback::GetDescriptor();
39 auto remoteDescriptor = data.ReadInterfaceToken();
40 if (remoteDescriptor != localDescriptor) {
41 SLOGI("interface token is not equal");
42 return false;
43 }
44 return true;
45 }
46
HandleOnAVCallAnswer(MessageParcel & data,MessageParcel & reply)47 int32_t AVSessionCallbackStub::HandleOnAVCallAnswer(MessageParcel& data, MessageParcel& reply)
48 {
49 AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnPlay");
50 OnAVCallAnswer();
51 return ERR_NONE;
52 }
53
HandleOnAVCallHangUp(MessageParcel & data,MessageParcel & reply)54 int32_t AVSessionCallbackStub::HandleOnAVCallHangUp(MessageParcel& data, MessageParcel& reply)
55 {
56 AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnPlay");
57 OnAVCallHangUp();
58 return ERR_NONE;
59 }
60
HandleOnAVCallToggleCallMute(MessageParcel & data,MessageParcel & reply)61 int32_t AVSessionCallbackStub::HandleOnAVCallToggleCallMute(MessageParcel& data, MessageParcel& reply)
62 {
63 AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnPlay");
64 OnAVCallToggleCallMute();
65 return ERR_NONE;
66 }
67
HandleOnPlay(MessageParcel & data,MessageParcel & reply)68 int32_t AVSessionCallbackStub::HandleOnPlay(MessageParcel& data, MessageParcel& reply)
69 {
70 AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnPlay");
71 OnPlay();
72 return ERR_NONE;
73 }
74
HandleOnPause(MessageParcel & data,MessageParcel & reply)75 int32_t AVSessionCallbackStub::HandleOnPause(MessageParcel& data, MessageParcel& reply)
76 {
77 AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnPause");
78 OnPause();
79 return ERR_NONE;
80 }
81
HandleOnStop(MessageParcel & data,MessageParcel & reply)82 int32_t AVSessionCallbackStub::HandleOnStop(MessageParcel& data, MessageParcel& reply)
83 {
84 AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnStop");
85 OnStop();
86 return ERR_NONE;
87 }
88
HandleOnPlayNext(MessageParcel & data,MessageParcel & reply)89 int32_t AVSessionCallbackStub::HandleOnPlayNext(MessageParcel& data, MessageParcel& reply)
90 {
91 AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnPlayNext");
92 OnPlayNext();
93 return ERR_NONE;
94 }
95
HandleOnPlayPrevious(MessageParcel & data,MessageParcel & reply)96 int32_t AVSessionCallbackStub::HandleOnPlayPrevious(MessageParcel& data, MessageParcel& reply)
97 {
98 AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnPlayPrevious");
99 OnPlayPrevious();
100 return ERR_NONE;
101 }
102
HandleOnFastForward(MessageParcel & data,MessageParcel & reply)103 int32_t AVSessionCallbackStub::HandleOnFastForward(MessageParcel& data, MessageParcel& reply)
104 {
105 AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnFastForward");
106 int32_t time = -1;
107 CHECK_AND_RETURN_RET_LOG(data.ReadInt32(time), ERR_NONE, "read time failed");
108 OnFastForward(time);
109 return ERR_NONE;
110 }
111
HandleOnRewind(MessageParcel & data,MessageParcel & reply)112 int32_t AVSessionCallbackStub::HandleOnRewind(MessageParcel& data, MessageParcel& reply)
113 {
114 AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnRewind");
115 int32_t time = -1;
116 CHECK_AND_RETURN_RET_LOG(data.ReadInt32(time), ERR_NONE, "read time failed");
117 OnRewind(time);
118 return ERR_NONE;
119 }
120
HandleOnSeek(MessageParcel & data,MessageParcel & reply)121 int32_t AVSessionCallbackStub::HandleOnSeek(MessageParcel& data, MessageParcel& reply)
122 {
123 AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnSeek");
124 int32_t time = -1;
125 CHECK_AND_RETURN_RET_LOG(data.ReadInt32(time), ERR_NONE, "read time failed");
126 OnSeek(time);
127 return ERR_NONE;
128 }
129
HandleOnSetSpeed(MessageParcel & data,MessageParcel & reply)130 int32_t AVSessionCallbackStub::HandleOnSetSpeed(MessageParcel& data, MessageParcel& reply)
131 {
132 AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnSetSpeed");
133 double speed = 0.0;
134 CHECK_AND_RETURN_RET_LOG(data.ReadDouble(speed), ERR_NONE, "read speed failed");
135 OnSetSpeed(speed);
136 return ERR_NONE;
137 }
138
HandleOnSetLoopMode(MessageParcel & data,MessageParcel & reply)139 int32_t AVSessionCallbackStub::HandleOnSetLoopMode(MessageParcel& data, MessageParcel& reply)
140 {
141 AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnSetLoopMode");
142 int32_t loopMode = -1;
143 CHECK_AND_RETURN_RET_LOG(data.ReadInt32(loopMode), ERR_NONE, "read loopMode failed");
144 OnSetLoopMode(loopMode);
145 return ERR_NONE;
146 }
147
HandleOnToggleFavorite(MessageParcel & data,MessageParcel & reply)148 int32_t AVSessionCallbackStub::HandleOnToggleFavorite(MessageParcel& data, MessageParcel& reply)
149 {
150 AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnToggleFavorite");
151 std::string mediaId;
152 CHECK_AND_RETURN_RET_LOG(data.ReadString(mediaId), ERR_NONE, "read mediaId failed");
153 OnToggleFavorite(mediaId);
154 return ERR_NONE;
155 }
156
HandleOnMediaKeyEvent(MessageParcel & data,MessageParcel & reply)157 int32_t AVSessionCallbackStub::HandleOnMediaKeyEvent(MessageParcel& data, MessageParcel& reply)
158 {
159 AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnMediaKeyEvent");
160 auto keyEvent = MMI::KeyEvent::Create();
161 CHECK_AND_RETURN_RET_LOG((*keyEvent).ReadFromParcel(data), ERR_NONE, "read keyEvent failed");
162 OnMediaKeyEvent(*keyEvent);
163 return ERR_NONE;
164 }
165
HandleOnOutputDeviceChange(MessageParcel & data,MessageParcel & reply)166 int32_t AVSessionCallbackStub::HandleOnOutputDeviceChange(MessageParcel& data, MessageParcel& reply)
167 {
168 AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnOutputDeviceChange");
169 int32_t connectionState;
170 CHECK_AND_RETURN_RET_LOG(data.ReadInt32(connectionState), false, "write deviceInfoSize failed");
171 OutputDeviceInfo outputDeviceInfo;
172 int32_t deviceInfoSize;
173 CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfoSize), false, "write deviceInfoSize failed");
174 int32_t maxDeviceInfoSize = 1000; // A maximum of 1000 device change events can be processed at a time
175 CHECK_AND_RETURN_RET_LOG((deviceInfoSize >= 0) && (deviceInfoSize < maxDeviceInfoSize),
176 false, "deviceInfoSize is illegal");
177 for (int i = 0; i < deviceInfoSize; i++) {
178 DeviceInfo deviceInfo;
179 CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.castCategory_), false, "Read castCategory failed");
180 CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.deviceId_), false, "Read deviceId failed");
181 CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.deviceName_), false, "Read deviceName failed");
182 CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.deviceType_), false, "Read deviceType failed");
183 CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.ipAddress_), false, "Read ipAddress failed");
184 CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.providerId_), false, "Read providerId failed");
185 CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.supportedProtocols_), false,
186 "Read supportedProtocols failed");
187 CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.authenticationStatus_), false,
188 "Read authenticationStatus failed");
189 outputDeviceInfo.deviceInfos_.emplace_back(deviceInfo);
190 }
191
192 OnOutputDeviceChange(connectionState, outputDeviceInfo);
193 return ERR_NONE;
194 }
195
HandleOnCommonCommand(MessageParcel & data,MessageParcel & reply)196 int32_t AVSessionCallbackStub::HandleOnCommonCommand(MessageParcel& data, MessageParcel& reply)
197 {
198 AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnCommonCommand");
199 auto commonCommand = data.ReadString();
200 sptr commonArgs = data.ReadParcelable<AAFwk::WantParams>();
201 CHECK_AND_RETURN_RET_LOG(commonArgs != nullptr, ERR_NONE, "Read common args failed");
202 OnCommonCommand(commonCommand, *commonArgs);
203 return ERR_NONE;
204 }
205
HandleOnSkipToQueueItem(MessageParcel & data,MessageParcel & reply)206 int32_t AVSessionCallbackStub::HandleOnSkipToQueueItem(MessageParcel& data, MessageParcel& reply)
207 {
208 AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnSkipToQueueItem");
209 int32_t itemId = -1;
210 CHECK_AND_RETURN_RET_LOG(data.ReadInt32(itemId), ERR_NONE, "read itemId failed");
211 OnSkipToQueueItem(itemId);
212 return ERR_NONE;
213 }
214
HandleOnPlayFromAssetId(MessageParcel & data,MessageParcel & reply)215 int32_t AVSessionCallbackStub::HandleOnPlayFromAssetId(MessageParcel& data, MessageParcel& reply)
216 {
217 AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnPlayFromAssetId");
218 int64_t assetId = -1;
219 CHECK_AND_RETURN_RET_LOG(data.ReadInt64(assetId), ERR_NONE, "read time failed");
220 OnPlayFromAssetId(assetId);
221 return ERR_NONE;
222 }
223 } // namespace OHOS::AVSession
224