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     int32_t DestroyWithoutReply();
75 
76     std::string GetSessionId() override;
77 
78     void HandleSessionDestroy();
79 
80     void HandleAVCallStateChange(const AVCallState& avCallState);
81 
82     void HandleAVCallMetaDataChange(const AVCallMetaData& avCallMetaData);
83 
84     void HandlePlaybackStateChange(const AVPlaybackState& state);
85 
86     void HandleMetaDataChange(const AVMetaData& data);
87 
88     void HandleActiveStateChange(bool isActive);
89 
90     void HandleValidCommandChange(const std::vector<int32_t>& cmds);
91 
92     void HandleOutputDeviceChange(const int32_t connectionState, const OutputDeviceInfo& outputDeviceInfo);
93 
94     void HandleSetSessionEvent(const std::string& event, const AAFwk::WantParams& args);
95 
96     void HandleQueueItemsChange(const std::vector<AVQueueItem>& items);
97 
98     void HandleQueueTitleChange(const std::string& title);
99 
100     void HandleExtrasChange(const AAFwk::WantParams& extras);
101 
102     pid_t GetPid() const;
103 
104     bool HasSession(const std::string& sessionId);
105 
106     void SetServiceCallbackForRelease(const std::function<void(AVControllerItem&)>& callback);
107 
108     int32_t RegisterAVControllerCallback(const std::shared_ptr<AVControllerCallback> &callback);
109 
110     bool isFromSession_ = false;
111 protected:
112     int32_t RegisterCallbackInner(const sptr<IRemoteObject>& callback) override;
113 
114 private:
115     pid_t pid_;
116     sptr<AVSessionItem> session_;
117     std::string sessionId_;
118     int32_t userId_;
119     std::recursive_mutex sessionMutex_;
120     std::recursive_mutex callbackMutex_;
121     sptr<IAVControllerCallback> callback_;
122     std::shared_ptr<AVControllerCallback> innerCallback_;
123     std::recursive_mutex avCallMetaMaskMutex_;
124     AVCallMetaData::AVCallMetaMaskType avCallMetaMask_;
125     std::recursive_mutex avCallStateMaskMutex_;
126     AVCallState::AVCallStateMaskType avCallStateMask_;
127     std::recursive_mutex metaMaskMutex_;
128     AVMetaData::MetaMaskType metaMask_;
129     std::recursive_mutex playbackMaskMutex_;
130     AVPlaybackState::PlaybackStateMaskType playbackMask_;
131     std::recursive_mutex serviceCallbackMutex_;
132     std::function<void(AVControllerItem&)> serviceCallback_;
133 
134     static const int32_t DEFAULT_USER_ID = 100;
135 };
136 } // namespace OHOS::AVSession
137 #endif // OHOS_AVCONTROLLER_ITEM_H
138