• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_AVSESSION_ITEM_H
17 #define OHOS_AVSESSION_ITEM_H
18 
19 #include <string>
20 #include <map>
21 
22 #include "avsession_stub.h"
23 #include "avsession_callback_proxy.h"
24 #include "avcontrol_command.h"
25 #include "audio_info.h"
26 
27 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
28 #include "i_avcast_controller_proxy.h"
29 #include "avcast_controller_item.h"
30 #endif
31 
32 namespace OHOS::AVSession {
33 class AVControllerItem;
34 class RemoteSessionSink;
35 class RemoteSessionSource;
36 class AVSessionItem : public AVSessionStub {
37 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
38 class CssListener : public IAVCastSessionStateListener {
39 public:
CssListener(AVSessionItem * ptr)40     explicit CssListener(AVSessionItem *ptr)
41     {
42         ptr_ = ptr;
43     }
44 
OnCastStateChange(int32_t castState,DeviceInfo deviceInfo)45     void OnCastStateChange(int32_t castState, DeviceInfo deviceInfo)
46     {
47         ptr_->OnCastStateChange(castState, deviceInfo);
48     }
49 
50     AVSessionItem *ptr_;
51 };
52 #endif
53 public:
54     explicit AVSessionItem(const AVSessionDescriptor& descriptor);
55 
56     ~AVSessionItem() override;
57 
58 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
59     bool IsCastSinkSession(int32_t castState);
60 
61     void OnCastStateChange(int32_t castState, DeviceInfo deviceInfo);
62 #endif
63 
64     std::string GetSessionId() override;
65 
66     std::string GetSessionType() override;
67 
68     int32_t GetAVMetaData(AVMetaData& meta) override;
69 
70     int32_t SetAVMetaData(const AVMetaData& meta) override;
71 
72     int32_t GetAVQueueItems(std::vector<AVQueueItem>& items) override;
73 
74     int32_t SetAVQueueItems(const std::vector<AVQueueItem>& items) override;
75 
76     int32_t GetAVQueueTitle(std::string& title) override;
77 
78     int32_t SetAVQueueTitle(const std::string& title) override;
79 
80     int32_t GetAVPlaybackState(AVPlaybackState& state) override;
81 
82     int32_t SetLaunchAbility(const AbilityRuntime::WantAgent::WantAgent& ability) override;
83 
84     int32_t GetExtras(AAFwk::WantParams& extras) override;
85 
86     int32_t SetExtras(const AAFwk::WantParams& extras) override;
87 
88     int32_t Activate() override;
89 
90     int32_t Deactivate() override;
91 
92     bool IsActive() override;
93 
94     int32_t Destroy() override;
95 
96     int32_t AddSupportCommand(int32_t cmd) override;
97 
98     int32_t DeleteSupportCommand(int32_t cmd) override;
99 
100     AVSessionDescriptor GetDescriptor();
101 
102     int32_t SetAVPlaybackState(const AVPlaybackState& state) override;
103 
104     AVPlaybackState GetPlaybackState();
105 
106     AVMetaData GetMetaData();
107 
108     std::vector<AVQueueItem> GetQueueItems();
109 
110     std::string GetQueueTitle();
111 
112     AAFwk::WantParams GetExtras();
113 
114     std::vector<int32_t> GetSupportCommand();
115 
116     AbilityRuntime::WantAgent::WantAgent GetLaunchAbility();
117 
118     void HandleMediaKeyEvent(const MMI::KeyEvent& keyEvent);
119 
120     void HandleOutputDeviceChange(const int32_t connectionState, const OutputDeviceInfo& outputDeviceInfo);
121 
122     void HandleSkipToQueueItem(const int32_t& itemId);
123 
124     void ExecuteControllerCommand(const AVControlCommand& cmd);
125 
126     void ExecueCommonCommand(const std::string& commonCommand, const AAFwk::WantParams& commandArgs);
127 
128     int32_t AddController(pid_t pid, sptr<AVControllerItem>& controller);
129 
130     void SetPid(pid_t pid);
131 
132     void SetUid(pid_t uid);
133 
134     pid_t GetPid() const;
135 
136     pid_t GetUid() const;
137 
138     std::string GetAbilityName() const;
139 
140     std::string GetBundleName() const;
141 
142     void SetTop(bool top);
143 
144     std::shared_ptr<RemoteSessionSource> GetRemoteSource();
145 
146     void HandleControllerRelease(pid_t pid);
147 
148     void SetServiceCallbackForRelease(const std::function<void(AVSessionItem&)>& callback);
149 
150     void SetOutputDevice(const OutputDeviceInfo& info);
151 
152     void GetOutputDevice(OutputDeviceInfo& info);
153 
154     int32_t CastAudioToRemote(const std::string& sourceDevice, const std::string& sinkDevice,
155                               const std::string& sinkCapability);
156 
157     int32_t SourceCancelCastAudio(const std::string& sinkDevice);
158 
159     int32_t CastAudioFromRemote(const std::string& sourceSessionId, const std::string& sourceDevice,
160                                 const std::string& sinkDevice, const std::string& sourceCapability);
161 
162     int32_t SinkCancelCastAudio();
163 
164     int32_t SetSessionEvent(const std::string& event, const AAFwk::WantParams& args) override;
165 
166 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
167     int32_t ReleaseCast() override;
168 
169     int32_t StartCast(const OutputDeviceInfo& outputDeviceInfo);
170 
171     int32_t AddDevice(const int64_t castHandle, const OutputDeviceInfo& outputDeviceInfo);
172 
173     int32_t StopCast();
174 
175     sptr<IRemoteObject> GetAVCastControllerInner() override;
176 
177     void ReleaseAVCastControllerInner();
178 
179     void UpdateCastDeviceMap(DeviceInfo deviceInfo);
180 
181     void SetCastHandle(int64_t castHandle);
182 
183     void RegisterDeviceStateCallback();
184 
185     void UnRegisterDeviceStateCallback();
186 
187     void StopCastSession();
188 #endif
189 
190 protected:
191     int32_t RegisterCallbackInner(const sptr<IAVSessionCallback>& callback) override;
192     sptr<IRemoteObject> GetControllerInner() override;
193 
194 private:
195     void HandleOnPlay(const AVControlCommand& cmd);
196     void HandleOnPause(const AVControlCommand& cmd);
197     void HandleOnStop(const AVControlCommand& cmd);
198     void HandleOnPlayNext(const AVControlCommand& cmd);
199     void HandleOnPlayPrevious(const AVControlCommand& cmd);
200     void HandleOnFastForward(const AVControlCommand& cmd);
201     void HandleOnRewind(const AVControlCommand& cmd);
202     void HandleOnSeek(const AVControlCommand& cmd);
203     void HandleOnSetSpeed(const AVControlCommand& cmd);
204     void HandleOnSetLoopMode(const AVControlCommand& cmd);
205     void HandleOnToggleFavorite(const AVControlCommand& cmd);
206 
207     using HandlerFuncType = void(AVSessionItem::*)(const AVControlCommand&);
208     static inline HandlerFuncType cmdHandlers[] = {
209         &AVSessionItem::HandleOnPlay,
210         &AVSessionItem::HandleOnPause,
211         &AVSessionItem::HandleOnStop,
212         &AVSessionItem::HandleOnPlayNext,
213         &AVSessionItem::HandleOnPlayPrevious,
214         &AVSessionItem::HandleOnFastForward,
215         &AVSessionItem::HandleOnRewind,
216         &AVSessionItem::HandleOnSeek,
217         &AVSessionItem::HandleOnSetSpeed,
218         &AVSessionItem::HandleOnSetLoopMode,
219         &AVSessionItem::HandleOnToggleFavorite,
220     };
221 
222     std::recursive_mutex controllersLock_;
223     std::map<pid_t, sptr<AVControllerItem>> controllers_;
224     AVSessionDescriptor descriptor_;
225     AVPlaybackState playbackState_;
226     AVMetaData metaData_;
227     std::recursive_mutex queueItemsLock_;
228     std::vector<AVQueueItem> queueItems_;
229     std::string queueTitle_;
230     AbilityRuntime::WantAgent::WantAgent launchAbility_;
231     AAFwk::WantParams extras_;
232     std::vector<int32_t> supportedCmd_;
233     std::recursive_mutex callbackLock_;
234     sptr<IAVSessionCallback> callback_;
235     std::recursive_mutex remoteCallbackLock_;
236     std::shared_ptr<AVSessionCallback> remoteCallback_;
237     std::function<void(AVSessionItem&)> serviceCallback_;
238     friend class AVSessionDumper;
239 
240     std::recursive_mutex remoteSourceLock_;
241     std::shared_ptr<RemoteSessionSource> remoteSource_;
242     std::recursive_mutex remoteSinkLock_;
243     std::shared_ptr<RemoteSessionSink> remoteSink_;
244 
245 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
246     std::recursive_mutex castHandleLock_;
247     int64_t castHandle_ = 0;
248 
249     std::recursive_mutex castControllerProxyLock_;
250     std::shared_ptr<IAVCastControllerProxy> castControllerProxy_;
251     std::vector<std::shared_ptr<AVCastControllerItem>> castControllers_;
252     std::shared_ptr<CssListener> cssListener_;
253     std::shared_ptr<IAVCastSessionStateListener> iAVCastSessionStateListener_;
254 
255     std::map<std::string, DeviceInfo> castDeviceInfoMap_;
256 #endif
257 };
258 } // namespace OHOS::AVSession
259 #endif // OHOS_AVSESSION_ITEM_H