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