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 IAVCastSessionStateListener { 46 public: CssListener(AVSessionItem * ptr)47 explicit CssListener(AVSessionItem *ptr) 48 { 49 ptr_ = ptr; 50 } 51 OnCastStateChange(int32_t castState,DeviceInfo deviceInfo)52 void OnCastStateChange(int32_t castState, DeviceInfo deviceInfo) 53 { 54 ptr_->OnCastStateChange(castState, deviceInfo); 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); 76 77 void DealCollaborationPublishState(int32_t castState, DeviceInfo deviceInfo); 78 79 void OnCastStateChange(int32_t castState, DeviceInfo deviceInfo); 80 81 void OnCastEventRecv(int32_t errorCode, std::string& errorMsg); 82 83 void OnRemoveCastEngine(); 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(); 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() override; 229 230 int32_t StartCast(const OutputDeviceInfo& outputDeviceInfo); 231 232 int32_t CastAddToCollaboration(const OutputDeviceInfo& outputDeviceInfo); 233 234 int32_t AddDevice(const int64_t castHandle, const OutputDeviceInfo& outputDeviceInfo); 235 236 int32_t StopCast(); 237 238 sptr<IRemoteObject> GetAVCastControllerInner() override; 239 240 void ReleaseAVCastControllerInner(); 241 242 void UpdateCastDeviceMap(DeviceInfo deviceInfo); 243 244 void SetCastHandle(int64_t castHandle); 245 246 void RegisterDeviceStateCallback(); 247 248 void UnRegisterDeviceStateCallback(); 249 250 void StopCastSession(); 251 252 int32_t StartCastDisplayListener() override; 253 254 int32_t StopCastDisplayListener() override; 255 256 void GetDisplayListener(sptr<IAVSessionCallback> callback); 257 258 int32_t GetAllCastDisplays(std::vector<CastDisplayInfo>& castDisplays) override; 259 260 void SetExtrasInner(AAFwk::IArray* list); 261 262 void SetServiceCallbackForStream(const std::function<void(std::string)>& callback); 263 #endif 264 265 protected: 266 int32_t RegisterCallbackInner(const sptr<IAVSessionCallback>& callback) override; 267 sptr<IRemoteObject> GetControllerInner() override; 268 269 private: 270 bool HasAvQueueInfo(); 271 void HandleOnAVCallAnswer(const AVControlCommand& cmd); 272 void HandleOnAVCallHangUp(const AVControlCommand& cmd); 273 void HandleOnAVCallToggleCallMute(const AVControlCommand& cmd); 274 void HandleOnPlay(const AVControlCommand& cmd); 275 void HandleOnPause(const AVControlCommand& cmd); 276 void HandleOnPlayOrPause(const AVControlCommand& cmd); 277 void HandleOnStop(const AVControlCommand& cmd); 278 void HandleOnPlayNext(const AVControlCommand& cmd); 279 void HandleOnPlayPrevious(const AVControlCommand& cmd); 280 void HandleOnFastForward(const AVControlCommand& cmd); 281 void HandleOnRewind(const AVControlCommand& cmd); 282 void HandleOnSeek(const AVControlCommand& cmd); 283 void HandleOnSetSpeed(const AVControlCommand& cmd); 284 void HandleOnSetLoopMode(const AVControlCommand& cmd); 285 void HandleOnToggleFavorite(const AVControlCommand& cmd); 286 void HandleOnPlayFromAssetId(const AVControlCommand& cmd); 287 288 void ReportConnectFinish(const std::string func, const DeviceInfo &deviceInfo); 289 void ReportStopCastFinish(const std::string func, const DeviceInfo &deviceInfo); 290 void SaveLocalDeviceInfo(); 291 int32_t ProcessFrontSession(const std::string& source); 292 void HandleFrontSession(); 293 int32_t doContinuousTaskRegister(); 294 int32_t doContinuousTaskUnregister(); 295 void ReportSetAVMetaDataInfo(const AVMetaData& meta); 296 std::string GetAnonymousDeviceId(std::string deviceId); 297 void ReportAVCastControllerInfo(); 298 void GetAVCastControllerProxy(); 299 300 using HandlerFuncType = std::function<void(const AVControlCommand&)>; 301 std::map<uint32_t, HandlerFuncType> cmdHandlers = { 302 {AVControlCommand::SESSION_CMD_PLAY, 303 [this](const AVControlCommand& cmd) { HandleOnPlay(cmd); }}, 304 {AVControlCommand::SESSION_CMD_PAUSE, 305 [this](const AVControlCommand& cmd) { HandleOnPause(cmd); }}, 306 {AVControlCommand::SESSION_CMD_STOP, 307 [this](const AVControlCommand& cmd) { HandleOnStop(cmd); }}, 308 {AVControlCommand::SESSION_CMD_PLAY_NEXT, 309 [this](const AVControlCommand& cmd) { HandleOnPlayNext(cmd); }}, 310 {AVControlCommand::SESSION_CMD_PLAY_PREVIOUS, 311 [this](const AVControlCommand& cmd) { HandleOnPlayPrevious(cmd); }}, 312 {AVControlCommand::SESSION_CMD_FAST_FORWARD, 313 [this](const AVControlCommand& cmd) { HandleOnFastForward(cmd); }}, 314 {AVControlCommand::SESSION_CMD_REWIND, 315 [this](const AVControlCommand& cmd) { HandleOnRewind(cmd); }}, 316 {AVControlCommand::SESSION_CMD_SEEK, 317 [this](const AVControlCommand& cmd) { HandleOnSeek(cmd); }}, 318 {AVControlCommand::SESSION_CMD_SET_SPEED, 319 [this](const AVControlCommand& cmd) { HandleOnSetSpeed(cmd); }}, 320 {AVControlCommand::SESSION_CMD_SET_LOOP_MODE, 321 [this](const AVControlCommand& cmd) { HandleOnSetLoopMode(cmd); }}, 322 {AVControlCommand::SESSION_CMD_TOGGLE_FAVORITE, 323 [this](const AVControlCommand& cmd) { HandleOnToggleFavorite(cmd); }}, 324 {AVControlCommand::SESSION_CMD_PLAY_FROM_ASSETID, 325 [this](const AVControlCommand& cmd) { HandleOnPlayFromAssetId(cmd); }}, 326 {AVControlCommand::SESSION_CMD_AVCALL_ANSWER, 327 [this](const AVControlCommand& cmd) { HandleOnAVCallAnswer(cmd); }}, 328 {AVControlCommand::SESSION_CMD_AVCALL_HANG_UP, 329 [this](const AVControlCommand& cmd) { HandleOnAVCallHangUp(cmd); }}, 330 {AVControlCommand::SESSION_CMD_AVCALL_TOGGLE_CALL_MUTE, 331 [this](const AVControlCommand& cmd) { HandleOnAVCallToggleCallMute(cmd); }} 332 }; 333 334 std::map<int32_t, HandlerFuncType> keyEventCaller_ = { 335 {MMI::KeyEvent::KEYCODE_MEDIA_PLAY, [this](const AVControlCommand& cmd) { HandleOnPlay(cmd); }}, 336 {MMI::KeyEvent::KEYCODE_MEDIA_PAUSE, [this](const AVControlCommand& cmd) { HandleOnPause(cmd); }}, 337 {MMI::KeyEvent::KEYCODE_MEDIA_PLAY_PAUSE, [this](const AVControlCommand& cmd) { HandleOnPlayOrPause(cmd); }}, 338 {MMI::KeyEvent::KEYCODE_MEDIA_STOP, [this](const AVControlCommand& cmd) { HandleOnPause(cmd); }}, 339 {MMI::KeyEvent::KEYCODE_MEDIA_NEXT, [this](const AVControlCommand& cmd) { HandleOnPlayNext(cmd); }}, 340 {MMI::KeyEvent::KEYCODE_MEDIA_PREVIOUS, [this](const AVControlCommand& cmd) { HandleOnPlayPrevious(cmd); }}, 341 {MMI::KeyEvent::KEYCODE_MEDIA_REWIND, [this](const AVControlCommand& cmd) { HandleOnRewind(cmd); }}, 342 {MMI::KeyEvent::KEYCODE_MEDIA_FAST_FORWARD, [this](const AVControlCommand& cmd) { HandleOnFastForward(cmd); }} 343 }; 344 345 std::recursive_mutex controllersLock_; 346 std::map<pid_t, sptr<AVControllerItem>> controllers_; 347 AVCallMetaData avCallMetaData_; 348 AVCallState avCallState_; 349 350 AVSessionDescriptor descriptor_; 351 int32_t userId_; 352 AVPlaybackState playbackState_; 353 AVMetaData metaData_; 354 std::recursive_mutex queueItemsLock_; 355 std::vector<AVQueueItem> queueItems_; 356 std::string queueTitle_; 357 AbilityRuntime::WantAgent::WantAgent launchAbility_; 358 AAFwk::WantParams extras_; 359 std::vector<int32_t> supportedCmd_; 360 std::vector<int32_t> supportedCastCmds_; 361 std::recursive_mutex callbackLock_; 362 sptr<IAVSessionCallback> callback_; 363 std::recursive_mutex remoteCallbackLock_; 364 std::shared_ptr<AVSessionCallback> remoteCallback_; 365 std::function<void(AVSessionItem&)> serviceCallback_; 366 std::function<void(AVSessionItem&)> callStartCallback_; 367 friend class AVSessionDumper; 368 369 std::recursive_mutex remoteSourceLock_; 370 std::shared_ptr<RemoteSessionSource> remoteSource_; 371 std::recursive_mutex remoteSinkLock_; 372 std::shared_ptr<RemoteSessionSink> remoteSink_; 373 std::recursive_mutex wantParamLock_; 374 375 std::function<void(AVSessionItem&)> serviceCallbackForAddAVQueueInfo_; 376 std::function<void(std::string, bool)> serviceCallbackForUpdateSession_; 377 volatile bool isFirstAddToFront_ = true; 378 bool isMediaKeySupport = false; 379 380 int32_t castConnectStateForDisconnect_ = 5; 381 int32_t castConnectStateForConnected_ = 6; 382 int32_t removeCmdStep_ = 1000; 383 384 std::recursive_mutex destroyLock_; 385 volatile bool isDestroyed_ = false; 386 387 std::recursive_mutex metaDataLock_; 388 389 static const int32_t DEFAULT_USER_ID = 100; 390 std::recursive_mutex cmdsLock_; 391 392 #ifdef CASTPLUS_CAST_ENGINE_ENABLE 393 std::recursive_mutex castHandleLock_; 394 int64_t castHandle_ = 0; 395 std::string castHandleDeviceId_ = "-100"; 396 const int32_t streamStateConnection = 6; 397 const int32_t virtualDeviceStateConnection = -6; 398 const std::string deviceStateConnection = "CONNECT_SUCC"; 399 const int32_t firstStep = 1; 400 const int32_t secondStep = 2; 401 int32_t removeTimes = 0; 402 int32_t newCastState = -1; 403 int32_t counter_ = -1; 404 bool isUpdate = false; 405 std::map<std::string, std::string> castServiceNameMapState_; 406 407 bool collaborationRejectFlag_ = false; 408 bool applyUserResultFlag_ = false; 409 bool applyResultFlag_ = false; 410 bool networkIdIsEmpty_ = false; 411 bool waitUserDecisionFlag_ = false; 412 bool mirrorToStreamFlag_ = false; 413 std::string collaborationNeedNetworkId_; 414 std::mutex collaborationApplyResultMutex_; 415 std::condition_variable connectWaitCallbackCond_; 416 const int32_t collaborationCallbackTimeOut_ = 10; 417 const int32_t collaborationUserCallbackTimeOut_ = 60; 418 419 std::recursive_mutex castControllerProxyLock_; 420 std::shared_ptr<IAVCastControllerProxy> castControllerProxy_; 421 std::recursive_mutex castControllersLock_; 422 std::vector<std::shared_ptr<AVCastControllerItem>> castControllers_; 423 std::shared_ptr<CssListener> cssListener_; 424 std::shared_ptr<IAVCastSessionStateListener> iAVCastSessionStateListener_; 425 sptr<HwCastDisplayListener> displayListener_; 426 std::recursive_mutex displayListenerLock_; 427 std::recursive_mutex mirrorToStreamLock_; 428 429 std::map<std::string, DeviceInfo> castDeviceInfoMap_; 430 std::function<void(std::string)> serviceCallbackForStream_; 431 #endif 432 }; 433 } // namespace OHOS::AVSession 434 #endif // OHOS_AVSESSION_ITEM_H 435