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