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 "avsession_display_interface.h"
26 #include "avsession_dynamic_loader.h"
27 #include "avcontrol_command.h"
28 #include "audio_info.h"
29 #include "avcast_control_command.h"
30 #include "system_ability_definition.h"
31 #include "collaboration_manager_utils.h"
32 
33 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
34 #include <condition_variable>
35 #include <chrono>
36 
37 #include "i_avcast_controller_proxy.h"
38 #include "avcast_controller_item.h"
39 #include "hw_cast_display_listener.h"
40 #endif
41 
42 namespace OHOS::AVSession {
43 class AVControllerItem;
44 class RemoteSessionSink;
45 class RemoteSessionSource;
46 class AVSessionItem : public AVSessionStub {
47 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
48 class CssListener : public IAVCastSessionStateListener {
49 public:
CssListener(AVSessionItem *ptr)50     explicit CssListener(AVSessionItem *ptr)
51     {
52         ptr_ = ptr;
53     }
54 
OnCastStateChange(int32_t castState, DeviceInfo deviceInfo)55     void OnCastStateChange(int32_t castState, DeviceInfo deviceInfo)
56     {
57         ptr_->OnCastStateChange(castState, deviceInfo);
58     }
59 
OnCastEventRecv(int32_t errorCode, std::string& errorMsg)60     void OnCastEventRecv(int32_t errorCode, std::string& errorMsg)
61     {
62         ptr_->OnCastEventRecv(errorCode, errorMsg);
63     }
64 
65     AVSessionItem *ptr_;
66 };
67 #endif
68 public:
69     explicit AVSessionItem(const AVSessionDescriptor& descriptor, int32_t userId = DEFAULT_USER_ID);
70 
71     ~AVSessionItem() override;
72 
73 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
74     void DealCastState(int32_t castState);
75 
76     void DealDisconnect(DeviceInfo deviceInfo);
77 
78     void DealCollaborationPublishState(int32_t castState, DeviceInfo deviceInfo);
79 
80     void OnCastStateChange(int32_t castState, DeviceInfo deviceInfo);
81 
82     void OnCastEventRecv(int32_t errorCode, std::string& errorMsg);
83 
84     void OnRemoveCastEngine();
85 
86     void ListenCollaborationRejectToStopCast();
87 #endif
88 
89     std::string GetSessionId() override;
90 
91     std::string GetSessionType() override;
92 
93     int32_t SetAVCallMetaData(const AVCallMetaData& avCallMetaData) override;
94 
95     int32_t SetAVCallState(const AVCallState& avCallState) override;
96 
97     int32_t GetAVMetaData(AVMetaData& meta) override;
98 
99     int32_t SetAVMetaData(const AVMetaData& meta) override;
100 
101     int32_t GetAVQueueItems(std::vector<AVQueueItem>& items) override;
102 
103     int32_t SetAVQueueItems(const std::vector<AVQueueItem>& items) override;
104 
105     int32_t GetAVQueueTitle(std::string& title) override;
106 
107     int32_t SetAVQueueTitle(const std::string& title) override;
108 
109     int32_t GetAVPlaybackState(AVPlaybackState& state) override;
110 
111     int32_t SetLaunchAbility(const AbilityRuntime::WantAgent::WantAgent& ability) override;
112 
113     int32_t GetExtras(AAFwk::WantParams& extras) override;
114 
115     int32_t SetExtras(const AAFwk::WantParams& extras) override;
116 
117     int32_t Activate() override;
118 
119     int32_t Deactivate() override;
120 
121     bool IsActive() override;
122 
123     int32_t Destroy() override;
124 
125     int32_t AddSupportCommand(int32_t cmd) override;
126 
127     int32_t DeleteSupportCommand(int32_t cmd) override;
128 
129     int32_t DestroyTask();
130 
131     AVSessionDescriptor GetDescriptor();
132 
133     int32_t SetAVPlaybackState(const AVPlaybackState& state) override;
134 
135     AVCallState GetAVCallState();
136 
137     AVCallMetaData GetAVCallMetaData();
138 
139     AVPlaybackState GetPlaybackState();
140 
141     AVMetaData GetMetaData();
142 
143     std::vector<AVQueueItem> GetQueueItems();
144 
145     std::string GetQueueTitle();
146 
147     AAFwk::WantParams GetExtras();
148 
149     std::vector<int32_t> GetSupportCommand();
150 
151     AbilityRuntime::WantAgent::WantAgent GetLaunchAbility();
152 
153     void HandleMediaKeyEvent(const MMI::KeyEvent& keyEvent);
154 
155     void HandleOutputDeviceChange(const int32_t connectionState, const OutputDeviceInfo& outputDeviceInfo);
156 
157     void HandleSkipToQueueItem(const int32_t& itemId);
158 
159     void ExecuteControllerCommand(const AVControlCommand& cmd);
160 
161     void ExecueCommonCommand(const std::string& commonCommand, const AAFwk::WantParams& commandArgs);
162 
163     int32_t AddController(pid_t pid, sptr<AVControllerItem>& controller);
164 
165     void SetPid(pid_t pid);
166 
167     void SetUid(pid_t uid);
168 
169     pid_t GetPid() const;
170 
171     pid_t GetUid() const;
172 
173     int32_t GetUserId() const;
174 
175     std::string GetAbilityName() const;
176 
177     std::string GetBundleName() const;
178 
179     void SetTop(bool top);
180 
181     std::shared_ptr<RemoteSessionSource> GetRemoteSource();
182 
183     void HandleControllerRelease(pid_t pid);
184 
185     void SetServiceCallbackForRelease(const std::function<void(AVSessionItem&)>& callback);
186 
187     void SetServiceCallbackForCallStart(const std::function<void(AVSessionItem&)>& callback);
188 
189     void SetOutputDevice(const OutputDeviceInfo& info);
190 
191     void GetOutputDevice(OutputDeviceInfo& info);
192 
193     int32_t CastAudioToRemote(const std::string& sourceDevice, const std::string& sinkDevice,
194                               const std::string& sinkCapability);
195 
196     int32_t SourceCancelCastAudio(const std::string& sinkDevice);
197 
198     int32_t CastAudioFromRemote(const std::string& sourceSessionId, const std::string& sourceDevice,
199                                 const std::string& sinkDevice, const std::string& sourceCapability);
200 
201     int32_t SinkCancelCastAudio();
202 
203     int32_t SetSessionEvent(const std::string& event, const AAFwk::WantParams& args) override;
204 
205     void SetServiceCallbackForAVQueueInfo(const std::function<void(AVSessionItem&)>& callback);
206 
207     void SetServiceCallbackForUpdateSession(const std::function<void(std::string, bool)>& callback);
208 
209 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
210     void InitializeCastCommands();
211 
212     void AddSessionCommandToCast(int32_t cmd);
213 
214     void RemoveSessionCommandFromCast(int32_t cmd);
215 
216     int32_t SessionCommandToCastCommand(int32_t cmd);
217 
218     int32_t RegisterListenerStreamToCast(const std::map<std::string, std::string>& serviceNameMapState,
219         DeviceInfo deviceInfo);
220 
221     int32_t AddSupportCastCommand(int32_t cmd);
222 
223     int32_t DeleteSupportCastCommand(int32_t cmd);
224 
225     void HandleCastValidCommandChange(std::vector<int32_t> &cmds);
226 
227     int32_t ReleaseCast() override;
228 
229     int32_t StartCast(const OutputDeviceInfo& outputDeviceInfo);
230 
231     int32_t CastAddToCollaboration(const OutputDeviceInfo& outputDeviceInfo);
232 
233     int32_t AddDevice(const int64_t castHandle, const OutputDeviceInfo& outputDeviceInfo);
234 
235     int32_t StopCast();
236 
237     sptr<IRemoteObject> GetAVCastControllerInner() override;
238 
239     void ReleaseAVCastControllerInner();
240 
241     void UpdateCastDeviceMap(DeviceInfo deviceInfo);
242 
243     void SetCastHandle(int64_t castHandle);
244 
245     void RegisterDeviceStateCallback();
246 
247     void UnRegisterDeviceStateCallback();
248 
249     void StopCastSession();
250 
251     int32_t StartCastDisplayListener() override;
252 
253     int32_t StopCastDisplayListener() override;
254 
255     void GetDisplayListener(sptr<IAVSessionCallback> callback);
256 
257     int32_t GetAllCastDisplays(std::vector<CastDisplayInfo>& castDisplays) override;
258 
259     void SetExtrasInner(AAFwk::IArray* list);
260 
261     void SetServiceCallbackForStream(const std::function<void(std::string)>& callback);
262 #endif
263 
264 protected:
265     int32_t RegisterCallbackInner(const sptr<IAVSessionCallback>& callback) override;
266     sptr<IRemoteObject> GetControllerInner() override;
267 
268 private:
269     bool HasAvQueueInfo();
270     void HandleOnAVCallAnswer(const AVControlCommand& cmd);
271     void HandleOnAVCallHangUp(const AVControlCommand& cmd);
272     void HandleOnAVCallToggleCallMute(const AVControlCommand& cmd);
273     void HandleOnPlay(const AVControlCommand& cmd);
274     void HandleOnPause(const AVControlCommand& cmd);
275     void HandleOnPlayOrPause(const AVControlCommand& cmd);
276     void HandleOnStop(const AVControlCommand& cmd);
277     void HandleOnPlayNext(const AVControlCommand& cmd);
278     void HandleOnPlayPrevious(const AVControlCommand& cmd);
279     void HandleOnFastForward(const AVControlCommand& cmd);
280     void HandleOnRewind(const AVControlCommand& cmd);
281     void HandleOnSeek(const AVControlCommand& cmd);
282     void HandleOnSetSpeed(const AVControlCommand& cmd);
283     void HandleOnSetLoopMode(const AVControlCommand& cmd);
284     void HandleOnToggleFavorite(const AVControlCommand& cmd);
285     void HandleOnPlayFromAssetId(const AVControlCommand& cmd);
286 
287     void ReportConnectFinish(const std::string func, const DeviceInfo &deviceInfo);
288     void ReportStopCastFinish(const std::string func, const DeviceInfo &deviceInfo);
289     void SaveLocalDeviceInfo();
290     int32_t ProcessFrontSession(const std::string& source);
291     void HandleFrontSession();
292     int32_t DoContinuousTaskRegister();
293     int32_t DoContinuousTaskUnregister();
294     AVSessionDisplayIntf* GetAVSessionDisplayIntf();
295     void ReportSetAVMetaDataInfo(const AVMetaData& meta);
296     std::string GetAnonymousDeviceId(std::string deviceId);
297     void ReportAVCastControllerInfo();
298     void InitAVCastControllerProxy();
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::map<pid_t, sptr<AVControllerItem>> controllers_;
346     AVCallMetaData avCallMetaData_;
347     AVCallState avCallState_;
348 
349     AVSessionDescriptor descriptor_;
350     int32_t userId_;
351     AVPlaybackState playbackState_;
352     AVMetaData metaData_;
353     std::vector<AVQueueItem> queueItems_;
354     std::string queueTitle_;
355     AbilityRuntime::WantAgent::WantAgent launchAbility_;
356     AAFwk::WantParams extras_;
357     std::vector<int32_t> supportedCmd_;
358     std::vector<int32_t> supportedCastCmds_;
359     sptr<IAVSessionCallback> callback_;
360     std::shared_ptr<AVSessionCallback> remoteCallback_;
361     std::function<void(AVSessionItem&)> serviceCallback_;
362     std::function<void(AVSessionItem&)> callStartCallback_;
363     friend class AVSessionDumper;
364 
365     std::shared_ptr<RemoteSessionSource> remoteSource_;
366     std::shared_ptr<RemoteSessionSink> remoteSink_;
367 
368     std::function<void(AVSessionItem&)> serviceCallbackForAddAVQueueInfo_;
369     std::function<void(std::string, bool)> serviceCallbackForUpdateSession_;
370     volatile bool isFirstAddToFront_ = true;
371     bool isMediaKeySupport = false;
372 
373     int32_t castConnectStateForDisconnect_ = 5;
374     int32_t castConnectStateForConnected_ = 6;
375     int32_t removeCmdStep_ = 1000;
376 
377     volatile bool isDestroyed_ = false;
378 
379     AVSessionDisplayIntf *avsessionDisaplayIntf_;
380     std::unique_ptr<AVSessionDynamicLoader> dynamicLoader_ {};
381 
382     static const int32_t DEFAULT_USER_ID = 100;
383 
384     // The following locks are used in the defined order of priority
385     std::recursive_mutex avsessionItemLock_;
386 
387     std::recursive_mutex controllersLock_;
388 
389     std::recursive_mutex callbackLock_;
390 
391     std::recursive_mutex destroyLock_;
392 
393     std::recursive_mutex displayListenerLock_;
394 
395     std::recursive_mutex remoteCallbackLock_;
396 
397     std::recursive_mutex remoteSourceLock_;
398 
399     std::recursive_mutex remoteSinkLock_;
400 
401 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
402     std::recursive_mutex castHandleLock_;
403     int64_t castHandle_ = 0;
404     std::string castHandleDeviceId_ = "-100";
405     const int32_t streamStateConnection = 6;
406     const int32_t virtualDeviceStateConnection = -6;
407     const std::string deviceStateConnection = "CONNECT_SUCC";
408     const int32_t firstStep = 1;
409     const int32_t secondStep = 2;
410     int32_t removeTimes = 0;
411     int32_t newCastState = -1;
412     int32_t counter_ = -1;
413     bool isUpdate = false;
414     std::map<std::string, std::string> castServiceNameMapState_;
415 
416     bool collaborationRejectFlag_ = false;
417     bool applyUserResultFlag_ = false;
418     bool applyResultFlag_ = false;
419     bool networkIdIsEmpty_ = false;
420     bool waitUserDecisionFlag_ = false;
421     bool mirrorToStreamFlag_ = false;
422     std::string collaborationNeedNetworkId_;
423     std::mutex collaborationApplyResultMutex_;
424     std::condition_variable connectWaitCallbackCond_;
425     const int32_t collaborationCallbackTimeOut_ = 10;
426     const int32_t collaborationUserCallbackTimeOut_ = 60;
427 
428     std::recursive_mutex castControllerProxyLock_;
429     std::shared_ptr<IAVCastControllerProxy> castControllerProxy_;
430     std::recursive_mutex castControllersLock_;
431     std::vector<std::shared_ptr<AVCastControllerItem>> castControllers_;
432     std::shared_ptr<CssListener> cssListener_;
433     std::shared_ptr<IAVCastSessionStateListener> iAVCastSessionStateListener_;
434     sptr<HwCastDisplayListener> displayListener_;
435     std::recursive_mutex displayListenerLock_;
436 
437     std::map<std::string, DeviceInfo> castDeviceInfoMap_;
438     std::function<void(std::string)> serviceCallbackForStream_;
439 #endif
440 };
441 } // namespace OHOS::AVSession
442 #endif // OHOS_AVSESSION_ITEM_H
443