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 #ifndef OHOS_AVSESSION_ITEM_H 17 #define OHOS_AVSESSION_ITEM_H 18 19 #include <dlfcn.h> 20 #include <string> 21 #include <map> 22 23 #include "avsession_stub.h" 24 #include "avsession_callback_proxy.h" 25 #include "avcontrol_command.h" 26 #include "audio_info.h" 27 #include "avcast_control_command.h" 28 #include "system_ability_definition.h" 29 #include "collaboration_manager_utils.h" 30 31 #ifdef CASTPLUS_CAST_ENGINE_ENABLE 32 #include <condition_variable> 33 #include <chrono> 34 35 #include "i_avcast_controller_proxy.h" 36 #include "avcast_controller_item.h" 37 #include "hw_cast_display_listener.h" 38 #endif 39 40 namespace OHOS::AVSession { 41 class AVControllerItem; 42 class RemoteSessionSink; 43 class RemoteSessionSource; 44 class AVSessionItem : public AVSessionStub { 45 #ifdef CASTPLUS_CAST_ENGINE_ENABLE 46 class CssListener : public IAVRouterListener { 47 public: CssListener(AVSessionItem * ptr)48 explicit CssListener(AVSessionItem *ptr) 49 { 50 ptr_ = ptr; 51 } 52 OnCastStateChange(int32_t castState,DeviceInfo deviceInfo,bool isNeedRemove)53 void OnCastStateChange(int32_t castState, DeviceInfo deviceInfo, bool isNeedRemove) 54 { 55 ptr_->OnCastStateChange(castState, deviceInfo, isNeedRemove); 56 } 57 OnCastEventRecv(int32_t errorCode,std::string & errorMsg)58 void OnCastEventRecv(int32_t errorCode, std::string& errorMsg) 59 { 60 ptr_->OnCastEventRecv(errorCode, errorMsg); 61 } 62 63 AVSessionItem *ptr_; 64 }; 65 #endif 66 public: 67 explicit AVSessionItem(const AVSessionDescriptor& descriptor, int32_t userId = DEFAULT_USER_ID); 68 69 ~AVSessionItem() override; 70 71 #ifdef CASTPLUS_CAST_ENGINE_ENABLE 72 void DealCastState(int32_t castState); 73 74 void DealDisconnect(DeviceInfo deviceInfo, bool isNeedRemove); 75 76 void DealCollaborationPublishState(int32_t castState, DeviceInfo deviceInfo); 77 78 void DealLocalState(int32_t castState); 79 80 void OnCastStateChange(int32_t castState, DeviceInfo deviceInfo, bool isNeedRemove); 81 82 void OnCastEventRecv(int32_t errorCode, std::string& errorMsg); 83 84 void ListenCollaborationApplyResult(); 85 86 void ListenCollaborationOnStop(); 87 #endif 88 89 std::string GetSessionId() override; 90 91 std::string GetSessionType() override; 92 93 int32_t SetAVCallMetaData(const AVCallMetaData& avCallMetaData) override; 94 95 int32_t SetAVCallState(const AVCallState& avCallState) override; 96 97 int32_t GetAVMetaData(AVMetaData& meta) override; 98 99 int32_t SetAVMetaData(const AVMetaData& meta) override; 100 101 int32_t GetAVQueueItems(std::vector<AVQueueItem>& items) override; 102 103 int32_t SetAVQueueItems(const std::vector<AVQueueItem>& items) override; 104 105 int32_t GetAVQueueTitle(std::string& title) override; 106 107 int32_t SetAVQueueTitle(const std::string& title) override; 108 109 int32_t GetAVPlaybackState(AVPlaybackState& state) override; 110 111 int32_t SetLaunchAbility(const AbilityRuntime::WantAgent::WantAgent& ability) override; 112 113 int32_t GetExtras(AAFwk::WantParams& extras) override; 114 115 int32_t SetExtras(const AAFwk::WantParams& extras) override; 116 117 int32_t Activate() override; 118 119 int32_t Deactivate() override; 120 121 bool IsActive() override; 122 123 int32_t Destroy() override; 124 125 int32_t AddSupportCommand(int32_t cmd) override; 126 127 int32_t DeleteSupportCommand(int32_t cmd) override; 128 129 int32_t DestroyTask(bool continuePlay = false); 130 131 AVSessionDescriptor GetDescriptor(); 132 133 int32_t SetAVPlaybackState(const AVPlaybackState& state) override; 134 135 AVCallState GetAVCallState(); 136 137 AVCallMetaData GetAVCallMetaData(); 138 139 AVPlaybackState GetPlaybackState(); 140 141 AVMetaData GetMetaDataWithoutImg(); 142 143 AVMetaData GetMetaData(); 144 145 std::vector<AVQueueItem> GetQueueItems(); 146 147 std::string GetQueueTitle(); 148 149 AAFwk::WantParams GetExtras(); 150 151 void KeyEventExtras(AAFwk::IArray* list); 152 153 void NotificationExtras(AAFwk::IArray* list); 154 155 bool IsNotShowNotification(); 156 157 std::vector<int32_t> GetSupportCommand(); 158 159 AbilityRuntime::WantAgent::WantAgent GetLaunchAbility(); 160 161 void HandleMediaKeyEvent(const MMI::KeyEvent& keyEvent); 162 163 void HandleOutputDeviceChange(const int32_t connectionState, const OutputDeviceInfo& outputDeviceInfo); 164 165 void HandleSkipToQueueItem(const int32_t& itemId); 166 167 void ExecuteControllerCommand(const AVControlCommand& cmd); 168 169 void ExecueCommonCommand(const std::string& commonCommand, const AAFwk::WantParams& commandArgs); 170 171 int32_t AddController(pid_t pid, sptr<AVControllerItem>& controller); 172 173 void SetPid(pid_t pid); 174 175 void SetUid(pid_t uid); 176 177 pid_t GetPid() const; 178 179 pid_t GetUid() const; 180 181 int32_t GetUserId() const; 182 183 std::string GetAbilityName() const; 184 185 std::string GetBundleName() const; 186 187 void SetTop(bool top); 188 189 std::shared_ptr<RemoteSessionSource> GetRemoteSource(); 190 191 void HandleControllerRelease(pid_t pid); 192 193 void SetServiceCallbackForRelease(const std::function<void(AVSessionItem&)>& callback); 194 195 void SetServiceCallbackForCallStart(const std::function<void(AVSessionItem&)>& callback); 196 197 void SetServiceCallbackForKeyEvent(const std::function<void(std::string)>& callback); 198 199 void SetOutputDevice(const OutputDeviceInfo& info); 200 201 void GetOutputDevice(OutputDeviceInfo& info); 202 203 int32_t CastAudioToRemote(const std::string& sourceDevice, const std::string& sinkDevice, 204 const std::string& sinkCapability); 205 206 int32_t SourceCancelCastAudio(const std::string& sinkDevice); 207 208 int32_t CastAudioFromRemote(const std::string& sourceSessionId, const std::string& sourceDevice, 209 const std::string& sinkDevice, const std::string& sourceCapability); 210 211 int32_t SinkCancelCastAudio(); 212 213 int32_t SetSessionEvent(const std::string& event, const AAFwk::WantParams& args) override; 214 215 void SetServiceCallbackForAVQueueInfo(const std::function<void(AVSessionItem&)>& callback); 216 217 void SetServiceCallbackForUpdateSession(const std::function<void(std::string, bool)>& callback); 218 219 void SetServiceCallbackForNtfCapsule(const std::function<void(std::string, bool)>& callback); 220 221 bool IsCasting(); 222 223 void GetCurrentCastItem(AVQueueItem& item); 224 225 AVPlaybackState GetCastAVPlaybackState(); 226 227 void SendControlCommandToCast(AVCastControlCommand cmd); 228 229 void SetServiceCallbackForUpdateExtras(const std::function<void(std::string)>& callback); 230 231 void RegisterAVSessionCallback(std::shared_ptr<AVSessionCallback> callbackOfMigrate); 232 233 void SetSupportCommand(std::vector<int32_t> cmds); 234 235 #ifdef CASTPLUS_CAST_ENGINE_ENABLE 236 int32_t RegisterListenerStreamToCast(const std::pair<std::string, std::string>& serviceNameStatePair, 237 DeviceInfo deviceInfo); 238 239 void InitializeCastCommands(); 240 241 void AddSessionCommandToCast(int32_t cmd); 242 243 void RemoveSessionCommandFromCast(int32_t cmd); 244 245 int32_t SessionCommandToCastCommand(int32_t cmd); 246 247 int32_t AddSupportCastCommand(int32_t cmd); 248 249 int32_t DeleteSupportCastCommand(int32_t cmd); 250 251 void HandleCastValidCommandChange(const std::vector<int32_t> &cmds); 252 253 int32_t ReleaseCast(bool continuePlay = false) override; 254 255 int32_t StartCast(const OutputDeviceInfo& outputDeviceInfo); 256 257 int32_t SubStartCast(const OutputDeviceInfo& outputDeviceInfo); 258 259 int32_t CastAddToCollaboration(const OutputDeviceInfo& outputDeviceInfo); 260 261 int32_t AddDevice(const int64_t castHandle, const OutputDeviceInfo& outputDeviceInfo); 262 263 int32_t StopCast(bool continuePlay = false); 264 265 void dealValidCallback(int32_t cmd, std::vector<int32_t>& supportedCastCmds); 266 267 sptr<IRemoteObject> GetAVCastControllerInner() override; 268 269 void ReleaseAVCastControllerInner(); 270 271 void UpdateCastDeviceMap(DeviceInfo deviceInfo); 272 273 void SetCastHandle(int64_t castHandle); 274 275 void RegisterDeviceStateCallback(); 276 277 void UnRegisterDeviceStateCallback(); 278 279 void StopCastSession(); 280 281 int32_t StartCastDisplayListener() override; 282 283 int32_t StopCastDisplayListener() override; 284 285 void GetDisplayListener(sptr<IAVSessionCallback> callback); 286 287 int32_t GetAllCastDisplays(std::vector<CastDisplayInfo>& castDisplays) override; 288 289 void SetExtrasInner(AAFwk::IArray* list); 290 291 void SetServiceCallbackForStream(const std::function<void(std::string)>& callback); 292 293 void SetServiceCallbackForCastNtfCapsule(const std::function<void(std::string, bool, bool)>& callback); 294 #endif 295 296 protected: 297 int32_t RegisterCallbackInner(const sptr<IAVSessionCallback>& callback) override; 298 sptr<IRemoteObject> GetControllerInner() override; 299 300 private: 301 bool HasAvQueueInfo(); 302 void HandleOnAVCallAnswer(const AVControlCommand& cmd); 303 void HandleOnAVCallHangUp(const AVControlCommand& cmd); 304 void HandleOnAVCallToggleCallMute(const AVControlCommand& cmd); 305 void HandleOnPlay(const AVControlCommand& cmd); 306 void HandleOnPause(const AVControlCommand& cmd); 307 void HandleOnPlayOrPause(const AVControlCommand& cmd); 308 void HandleOnStop(const AVControlCommand& cmd); 309 void HandleOnPlayNext(const AVControlCommand& cmd); 310 void HandleOnPlayPrevious(const AVControlCommand& cmd); 311 void HandleOnFastForward(const AVControlCommand& cmd); 312 void HandleOnRewind(const AVControlCommand& cmd); 313 void HandleOnSeek(const AVControlCommand& cmd); 314 void HandleOnSetSpeed(const AVControlCommand& cmd); 315 void HandleOnSetLoopMode(const AVControlCommand& cmd); 316 void HandleOnSetTargetLoopMode(const AVControlCommand& cmd); 317 void HandleOnToggleFavorite(const AVControlCommand& cmd); 318 void HandleOnPlayFromAssetId(const AVControlCommand& cmd); 319 320 void ReportConnectFinish(const std::string func, const DeviceInfo &deviceInfo); 321 void ReportStopCastFinish(const std::string func, const DeviceInfo &deviceInfo); 322 void SaveLocalDeviceInfo(); 323 int32_t ProcessFrontSession(const std::string& source); 324 void HandleFrontSession(); 325 int32_t DoContinuousTaskRegister(); 326 int32_t DoContinuousTaskUnregister(); 327 void ReportSetAVMetaDataInfo(const AVMetaData& meta); 328 std::string GetAnonymousDeviceId(std::string deviceId); 329 void ReportAVCastControllerInfo(); 330 void InitAVCastControllerProxy(); 331 bool CheckTitleChange(const AVMetaData& meta); 332 333 using HandlerFuncType = std::function<void(const AVControlCommand&)>; 334 std::map<uint32_t, HandlerFuncType> cmdHandlers = { 335 {AVControlCommand::SESSION_CMD_PLAY, 336 [this](const AVControlCommand& cmd) { HandleOnPlay(cmd); }}, 337 {AVControlCommand::SESSION_CMD_PAUSE, 338 [this](const AVControlCommand& cmd) { HandleOnPause(cmd); }}, 339 {AVControlCommand::SESSION_CMD_STOP, 340 [this](const AVControlCommand& cmd) { HandleOnStop(cmd); }}, 341 {AVControlCommand::SESSION_CMD_PLAY_NEXT, 342 [this](const AVControlCommand& cmd) { HandleOnPlayNext(cmd); }}, 343 {AVControlCommand::SESSION_CMD_PLAY_PREVIOUS, 344 [this](const AVControlCommand& cmd) { HandleOnPlayPrevious(cmd); }}, 345 {AVControlCommand::SESSION_CMD_FAST_FORWARD, 346 [this](const AVControlCommand& cmd) { HandleOnFastForward(cmd); }}, 347 {AVControlCommand::SESSION_CMD_REWIND, 348 [this](const AVControlCommand& cmd) { HandleOnRewind(cmd); }}, 349 {AVControlCommand::SESSION_CMD_SEEK, 350 [this](const AVControlCommand& cmd) { HandleOnSeek(cmd); }}, 351 {AVControlCommand::SESSION_CMD_SET_SPEED, 352 [this](const AVControlCommand& cmd) { HandleOnSetSpeed(cmd); }}, 353 {AVControlCommand::SESSION_CMD_SET_LOOP_MODE, 354 [this](const AVControlCommand& cmd) { HandleOnSetLoopMode(cmd); }}, 355 {AVControlCommand::SESSION_CMD_SET_TARGET_LOOP_MODE, 356 [this](const AVControlCommand& cmd) { HandleOnSetTargetLoopMode(cmd); }}, 357 {AVControlCommand::SESSION_CMD_TOGGLE_FAVORITE, 358 [this](const AVControlCommand& cmd) { HandleOnToggleFavorite(cmd); }}, 359 {AVControlCommand::SESSION_CMD_PLAY_FROM_ASSETID, 360 [this](const AVControlCommand& cmd) { HandleOnPlayFromAssetId(cmd); }}, 361 {AVControlCommand::SESSION_CMD_AVCALL_ANSWER, 362 [this](const AVControlCommand& cmd) { HandleOnAVCallAnswer(cmd); }}, 363 {AVControlCommand::SESSION_CMD_AVCALL_HANG_UP, 364 [this](const AVControlCommand& cmd) { HandleOnAVCallHangUp(cmd); }}, 365 {AVControlCommand::SESSION_CMD_AVCALL_TOGGLE_CALL_MUTE, 366 [this](const AVControlCommand& cmd) { HandleOnAVCallToggleCallMute(cmd); }} 367 }; 368 369 std::map<int32_t, HandlerFuncType> keyEventCaller_ = { 370 {MMI::KeyEvent::KEYCODE_MEDIA_PLAY, [this](const AVControlCommand& cmd) { HandleOnPlay(cmd); }}, 371 {MMI::KeyEvent::KEYCODE_MEDIA_PAUSE, [this](const AVControlCommand& cmd) { HandleOnPause(cmd); }}, 372 {MMI::KeyEvent::KEYCODE_MEDIA_PLAY_PAUSE, [this](const AVControlCommand& cmd) { HandleOnPlayOrPause(cmd); }}, 373 {MMI::KeyEvent::KEYCODE_MEDIA_STOP, [this](const AVControlCommand& cmd) { HandleOnPause(cmd); }}, 374 {MMI::KeyEvent::KEYCODE_MEDIA_NEXT, [this](const AVControlCommand& cmd) { HandleOnPlayNext(cmd); }}, 375 {MMI::KeyEvent::KEYCODE_MEDIA_PREVIOUS, [this](const AVControlCommand& cmd) { HandleOnPlayPrevious(cmd); }}, 376 {MMI::KeyEvent::KEYCODE_MEDIA_REWIND, [this](const AVControlCommand& cmd) { HandleOnRewind(cmd); }}, 377 {MMI::KeyEvent::KEYCODE_MEDIA_FAST_FORWARD, [this](const AVControlCommand& cmd) { HandleOnFastForward(cmd); }} 378 }; 379 380 std::map<pid_t, sptr<AVControllerItem>> controllers_; 381 AVCallMetaData avCallMetaData_; 382 AVCallState avCallState_; 383 384 AVSessionDescriptor descriptor_; 385 int32_t userId_; 386 AVPlaybackState playbackState_; 387 AVMetaData metaData_; 388 std::vector<AVQueueItem> queueItems_; 389 std::string queueTitle_; 390 AbilityRuntime::WantAgent::WantAgent launchAbility_; 391 AAFwk::WantParams extras_; 392 std::vector<int32_t> supportedCmd_; 393 std::vector<int32_t> supportedCastCmds_; 394 sptr<IAVSessionCallback> callback_; 395 std::shared_ptr<AVSessionCallback> callbackForMigrate_; 396 std::function<void(AVSessionItem&)> serviceCallback_; 397 std::function<void(AVSessionItem&)> callStartCallback_; 398 friend class AVSessionDumper; 399 400 std::shared_ptr<RemoteSessionSource> remoteSource_; 401 std::shared_ptr<RemoteSessionSink> remoteSink_; 402 403 std::function<void(AVSessionItem&)> serviceCallbackForAddAVQueueInfo_; 404 std::function<void(std::string, bool)> serviceCallbackForUpdateSession_; 405 std::function<void(std::string)> serviceCallbackForKeyEvent_; 406 std::function<void(std::string)> updateExtrasCallback_; 407 std::function<void(std::string, bool)> serviceCallbackForNtf_; 408 volatile bool isFirstAddToFront_ = true; 409 bool isMediaKeySupport = false; 410 bool isNotShowNotification_ = false; 411 bool isMediaChange_ = true; 412 413 int32_t disconnectStateFromCast_ = 5; 414 int32_t connectStateFromCast_ = 6; 415 int32_t castDisconnectStateInAVSession_ = 6; 416 int32_t removeCmdStep_ = 1000; 417 418 volatile bool isDestroyed_ = false; 419 420 static const int32_t DEFAULT_USER_ID = 100; 421 422 static constexpr const char *defaultBundleName = "com.example.himusicdemo"; 423 424 // The following locks are used in the defined order of priority 425 std::recursive_mutex avsessionItemLock_; 426 427 std::recursive_mutex controllersLock_; 428 429 std::recursive_mutex callbackLock_; 430 431 std::recursive_mutex destroyLock_; 432 433 std::recursive_mutex remoteCallbackLock_; 434 435 std::recursive_mutex remoteSourceLock_; 436 437 std::recursive_mutex remoteSinkLock_; 438 439 std::shared_ptr<bool> isAlivePtr_; 440 441 std::recursive_mutex isAliveLock_; 442 443 #ifdef CASTPLUS_CAST_ENGINE_ENABLE 444 std::recursive_mutex castLock_; 445 int64_t castHandle_ = 0; 446 std::string castHandleDeviceId_ = "-100"; 447 const int32_t streamStateConnection = 6; 448 const std::string deviceStateConnection = "CONNECT_SUCC"; 449 int32_t newCastState = -1; 450 std::pair<std::string, std::string> castserviceNameStatePair_; 451 452 bool collaborationRejectFlag_ = false; 453 bool applyUserResultFlag_ = false; 454 bool applyResultFlag_ = false; 455 bool waitUserDecisionFlag_ = false; 456 bool mirrorToStreamFlag_ = false; 457 std::string collaborationNeedNetworkId_; 458 std::mutex collaborationApplyResultMutex_; 459 std::condition_variable connectWaitCallbackCond_; 460 const int32_t collaborationCallbackTimeOut_ = 10; 461 const int32_t collaborationUserCallbackTimeOut_ = 60; 462 463 std::recursive_mutex castControllerProxyLock_; 464 std::shared_ptr<IAVCastControllerProxy> castControllerProxy_; 465 std::recursive_mutex castControllersLock_; 466 std::vector<std::shared_ptr<AVCastControllerItem>> castControllers_; 467 std::shared_ptr<CssListener> cssListener_; 468 sptr<HwCastDisplayListener> displayListener_; 469 std::recursive_mutex displayListenerLock_; 470 std::shared_ptr<IAVRouterListener> iAVRouterListener_; 471 std::recursive_mutex mirrorToStreamLock_; 472 473 std::map<std::string, DeviceInfo> castDeviceInfoMap_; 474 std::function<void(std::string)> serviceCallbackForStream_; 475 bool isSwitchNewDevice_ = false; 476 OutputDeviceInfo newOutputDeviceInfo_; 477 bool isFirstCallback_ = true; 478 const int32_t SWITCH_WAIT_TIME = 300; 479 std::function<void(std::string, bool, bool)> serviceCallbackForCastNtf_; 480 #endif 481 }; 482 } // namespace OHOS::AVSession 483 #endif // OHOS_AVSESSION_ITEM_H 484