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      OutputDeviceInfo outputDeviceInfo;
177      int32_t deviceInfoSize;
178      CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfoSize), false, "write deviceInfoSize failed");
179      int32_t maxDeviceInfoSize = 1000; // A maximum of 1000 device change events can be processed at a time
180      CHECK_AND_RETURN_RET_LOG((deviceInfoSize >= 0) && (deviceInfoSize < maxDeviceInfoSize),
181          false, "deviceInfoSize is illegal");
182      for (int i = 0; i < deviceInfoSize; i++) {
183          DeviceInfo deviceInfo;
184          CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.castCategory_), false, "Read castCategory failed");
185          CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.deviceId_), false, "Read deviceId failed");
186          CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.deviceName_), false, "Read deviceName failed");
187          CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.deviceType_), false, "Read deviceType failed");
188          CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.ipAddress_), false, "Read ipAddress failed");
189          CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.manufacturer_), false, "Read manufacturer failed");
190          CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.modelName_), false, "Read modelName failed");
191          CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.providerId_), false, "Read providerId failed");
192          CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.supportedProtocols_), false,
193              "Read supportedProtocols failed");
194          CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.authenticationStatus_), false,
195              "Read authenticationStatus failed");
196          int32_t supportedDrmCapabilityLen = 0;
197          CHECK_AND_RETURN_RET_LOG(data.ReadInt32(supportedDrmCapabilityLen), false,
198              "read supportedDrmCapabilityLen failed");
199          std::vector<std::string> supportedDrmCapabilities;
200          int32_t maxSupportedDrmCapabilityLen = 10;
201          CHECK_AND_RETURN_RET_LOG((supportedDrmCapabilityLen >= 0) &&
202              (supportedDrmCapabilityLen <= maxSupportedDrmCapabilityLen), false, "supportedDrmCapabilityLen is illegal");
203          for (int j = 0; j < supportedDrmCapabilityLen; j++) {
204              std::string supportedDrmCapability;
205              CHECK_AND_RETURN_RET_LOG(data.ReadString(supportedDrmCapability), false,
206                  "read supportedDrmCapability failed");
207              supportedDrmCapabilities.emplace_back(supportedDrmCapability);
208          }
209          deviceInfo.supportedDrmCapabilities_ = supportedDrmCapabilities;
210          CHECK_AND_RETURN_RET_LOG(data.ReadBool(deviceInfo.isLegacy_), false, "Read isLegacy failed");
211          CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.mediumTypes_), false, "Read mediumTypes failed");
212          outputDeviceInfo.deviceInfos_.emplace_back(deviceInfo);
213      }
214  
215      OnOutputDeviceChange(connectionState, outputDeviceInfo);
216      return ERR_NONE;
217  }
218  
HandleOnCommonCommand(MessageParcel & data,MessageParcel & reply)219  int32_t AVSessionCallbackStub::HandleOnCommonCommand(MessageParcel& data, MessageParcel& reply)
220  {
221      AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnCommonCommand");
222      auto commonCommand = data.ReadString();
223      sptr commonArgs = data.ReadParcelable<AAFwk::WantParams>();
224      CHECK_AND_RETURN_RET_LOG(commonArgs != nullptr, ERR_NONE, "Read common args failed");
225      OnCommonCommand(commonCommand, *commonArgs);
226      return ERR_NONE;
227  }
228  
HandleOnSkipToQueueItem(MessageParcel & data,MessageParcel & reply)229  int32_t AVSessionCallbackStub::HandleOnSkipToQueueItem(MessageParcel& data, MessageParcel& reply)
230  {
231      AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnSkipToQueueItem");
232      int32_t itemId = -1;
233      CHECK_AND_RETURN_RET_LOG(data.ReadInt32(itemId), ERR_NONE, "read itemId failed");
234      OnSkipToQueueItem(itemId);
235      return ERR_NONE;
236  }
237  
HandleOnPlayFromAssetId(MessageParcel & data,MessageParcel & reply)238  int32_t AVSessionCallbackStub::HandleOnPlayFromAssetId(MessageParcel& data, MessageParcel& reply)
239  {
240      AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnPlayFromAssetId");
241      int64_t assetId = -1;
242      CHECK_AND_RETURN_RET_LOG(data.ReadInt64(assetId), ERR_NONE, "read time failed");
243      OnPlayFromAssetId(assetId);
244      return ERR_NONE;
245  }
246  
HandleOnCastDisplayChange(MessageParcel & data,MessageParcel & reply)247  int32_t AVSessionCallbackStub::HandleOnCastDisplayChange(MessageParcel& data, MessageParcel& reply)
248  {
249      AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnCastDisplayChange");
250      CastDisplayInfo castDisplayInfo;
251      int32_t displayState = -1;
252      CHECK_AND_RETURN_RET_LOG(data.ReadInt32(displayState), ERR_NONE, "read displayState failed");
253      castDisplayInfo.displayState = static_cast<CastDisplayState>(displayState);
254      uint64_t displayId = 0;
255      CHECK_AND_RETURN_RET_LOG(data.ReadUint64(displayId), ERR_NONE, "read displayId failed");
256      castDisplayInfo.displayId = displayId;
257      std::string name{};
258      CHECK_AND_RETURN_RET_LOG(data.ReadString(name), ERR_NONE, "read name failed");
259      castDisplayInfo.name = name;
260      int32_t width = -1;
261      CHECK_AND_RETURN_RET_LOG(data.ReadInt32(width), ERR_NONE, "read width failed");
262      castDisplayInfo.width = width;
263      int32_t height = -1;
264      CHECK_AND_RETURN_RET_LOG(data.ReadInt32(height), ERR_NONE, "read height failed");
265      castDisplayInfo.height = height;
266      OnCastDisplayChange(castDisplayInfo);
267      return ERR_NONE;
268  }
269  } // namespace OHOS::AVSession
270