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 #ifndef OHOS_AVCONTROLLER_ITEM_H 17 #define OHOS_AVCONTROLLER_ITEM_H 18 19 #include <mutex> 20 #include <string> 21 22 #include "avsession_item.h" 23 #include "avcontroller_callback_proxy.h" 24 #include "avsession_controller_stub.h" 25 #include "audio_info.h" 26 27 namespace OHOS::AVSession { 28 class AVControllerItem : public AVSessionControllerStub { 29 public: 30 AVControllerItem(pid_t pid, const sptr<AVSessionItem>& session, int32_t userId = DEFAULT_USER_ID); 31 32 ~AVControllerItem() override; 33 34 int32_t GetUserId() const; 35 36 int32_t GetAVCallState(AVCallState& avCallState) override; 37 38 int32_t GetAVCallMetaData(AVCallMetaData& avCallMetaData) override; 39 40 int32_t GetAVPlaybackState(AVPlaybackState& state) override; 41 42 int32_t GetAVMetaData(AVMetaData& data) override; 43 44 int32_t GetAVQueueItems(std::vector<AVQueueItem>& items) override; 45 46 int32_t GetAVQueueTitle(std::string& title) override; 47 48 int32_t SkipToQueueItem(int32_t& itemId) override; 49 50 int32_t GetExtras(AAFwk::WantParams& extras) override; 51 52 int32_t SendAVKeyEvent(const MMI::KeyEvent& keyEvent) override; 53 54 int32_t GetLaunchAbility(AbilityRuntime::WantAgent::WantAgent& ability) override; 55 56 int32_t GetValidCommands(std::vector<int32_t>& cmds) override; 57 58 int32_t IsSessionActive(bool& isActive) override; 59 60 int32_t SendControlCommand(const AVControlCommand& cmd) override; 61 62 int32_t SendCommonCommand(const std::string& commonCommand, const AAFwk::WantParams& commandArgs) override; 63 64 int32_t SetAVCallMetaFilter(const AVCallMetaData::AVCallMetaMaskType& filter) override; 65 66 int32_t SetAVCallStateFilter(const AVCallState::AVCallStateMaskType& filter) override; 67 68 int32_t SetMetaFilter(const AVMetaData::MetaMaskType& filter) override; 69 70 int32_t SetPlaybackFilter(const AVPlaybackState::PlaybackStateMaskType& filter) override; 71 72 int32_t Destroy() override; 73 74 std::string GetSessionId() override; 75 76 void HandleSessionDestroy(); 77 78 void HandleAVCallStateChange(const AVCallState& avCallState); 79 80 void HandleAVCallMetaDataChange(const AVCallMetaData& avCallMetaData); 81 82 void HandlePlaybackStateChange(const AVPlaybackState& state); 83 84 void HandleMetaDataChange(const AVMetaData& data); 85 86 void HandleActiveStateChange(bool isActive); 87 88 void HandleValidCommandChange(const std::vector<int32_t>& cmds); 89 90 void HandleOutputDeviceChange(const int32_t connectionState, const OutputDeviceInfo& outputDeviceInfo); 91 92 void HandleSetSessionEvent(const std::string& event, const AAFwk::WantParams& args); 93 94 void HandleQueueItemsChange(const std::vector<AVQueueItem>& items); 95 96 void HandleQueueTitleChange(const std::string& title); 97 98 void HandleExtrasChange(const AAFwk::WantParams& extras); 99 100 pid_t GetPid() const; 101 102 bool HasSession(const std::string& sessionId); 103 104 void SetServiceCallbackForRelease(const std::function<void(AVControllerItem&)>& callback); 105 106 int32_t RegisterAVControllerCallback(const std::shared_ptr<AVControllerCallback> &callback); 107 108 bool isFromSession_ = false; 109 protected: 110 int32_t RegisterCallbackInner(const sptr<IRemoteObject>& callback) override; 111 112 private: 113 static const int32_t DEFAULT_USER_ID = 100; 114 pid_t pid_; 115 sptr<AVSessionItem> session_; 116 std::string sessionId_; 117 int32_t userId_; 118 sptr<IAVControllerCallback> callback_; 119 std::shared_ptr<AVControllerCallback> innerCallback_; 120 AVCallMetaData::AVCallMetaMaskType avCallMetaMask_; 121 AVCallState::AVCallStateMaskType avCallStateMask_; 122 AVMetaData::MetaMaskType metaMask_; 123 AVPlaybackState::PlaybackStateMaskType playbackMask_; 124 std::function<void(AVControllerItem&)> serviceCallback_; 125 126 // The following locks are used in the defined order of priority 127 std::recursive_mutex callbackMutex_; 128 129 std::recursive_mutex serviceCallbackMutex_; 130 131 std::recursive_mutex avCallMetaMaskMutex_; 132 133 std::recursive_mutex avCallStateMaskMutex_; 134 135 std::recursive_mutex metaMaskMutex_; 136 137 std::recursive_mutex playbackMaskMutex_; 138 139 std::recursive_mutex sessionMutex_; 140 }; 141 } // namespace OHOS::AVSession 142 #endif // OHOS_AVCONTROLLER_ITEM_H 143