• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SERVICE_H
17 #define OHOS_AVSESSION_SERVICE_H
18 
19 #include <atomic>
20 #include <mutex>
21 #include <map>
22 #include <thread>
23 
24 #include "iremote_stub.h"
25 #include "system_ability.h"
26 #include "audio_info.h"
27 #include "avsession_service_stub.h"
28 #include "avsession_item.h"
29 #include "avcontroller_item.h"
30 #include "session_container.h"
31 #include "iclient_death.h"
32 #include "isession_listener.h"
33 #include "focus_session_strategy.h"
34 #include "background_audio_controller.h"
35 #include "ability_manager_adapter.h"
36 #include "device_manager.h"
37 #include "dm_device_info.h"
38 #include "audio_adapter.h"
39 #include "remote_session_command_process.h"
40 #include "i_avsession_service_listener.h"
41 #include "avqueue_info.h"
42 #include "migrate/migrate_avsession_server.h"
43 #include "migrate/migrate_avsession_proxy.h"
44 #include "account_manager_adapter.h"
45 #include "app_manager_adapter.h"
46 #include "avsession_dynamic_loader.h"
47 #include "avsession_errors.h"
48 #include "avsession_log.h"
49 #include "avsession_info.h"
50 #include "file_ex.h"
51 #include "iservice_registry.h"
52 #include "key_event_adapter.h"
53 #include "cJSON.h"
54 #include "session_stack.h"
55 #include "avsession_trace.h"
56 #include "avsession_dumper.h"
57 #include "command_send_limit.h"
58 #include "avsession_sysevent.h"
59 #include "json_utils.h"
60 #include "avsession_utils.h"
61 #include "avcontrol_command.h"
62 #include "avsession_event_handler.h"
63 #include "bundle_status_adapter.h"
64 #include "if_system_ability_manager.h"
65 #include "avsession_radar.h"
66 #include "system_ability_load_callback_stub.h"
67 
68 #include "common_event_manager.h"
69 #include "common_event_subscribe_info.h"
70 #include "common_event_subscriber.h"
71 #include "common_event_support.h"
72 #include "matching_skills.h"
73 
74 #include "avsession_users_manager.h"
75 
76 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
77 #include "av_router.h"
78 #include "collaboration_manager.h"
79 #endif
80 
81 namespace OHOS::AVSession {
82 class AVSessionDumper;
83 
84 class ClientDeathRecipient : public IRemoteObject::DeathRecipient {
85 public:
86     explicit ClientDeathRecipient(const std::function<void()>& callback);
87 
88     void OnRemoteDied(const wptr<IRemoteObject>& object) override;
89 
90 private:
91     std::function<void()> callback_;
92 };
93 
94 class AVSessionInitDMCallback : public OHOS::DistributedHardware::DmInitCallback {
95 public:
96     AVSessionInitDMCallback() = default;
97     ~AVSessionInitDMCallback() override = default;
OnRemoteDied()98     void OnRemoteDied() override {};
99 };
100 
101 class AVSessionSystemAbilityLoadCallback : public SystemAbilityLoadCallbackStub {
102 public:
103     explicit AVSessionSystemAbilityLoadCallback(AVSessionService *ptr);
104     virtual ~AVSessionSystemAbilityLoadCallback();
105 
106     void OnLoadSACompleteForRemote(const std::string& deviceId,
107         int32_t systemAbilityId, const sptr<IRemoteObject>& remoteObject);
108 
109 private:
110     AVSessionService *servicePtr_ = nullptr;
111 };
112 
113 class AVSessionDeviceStateCallback : public OHOS::DistributedHardware::DeviceStateCallback {
114 public:
115     explicit AVSessionDeviceStateCallback(AVSessionService *ptr);
116     virtual ~AVSessionDeviceStateCallback();
117 
118     void OnDeviceOnline(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo) override;
119     void OnDeviceReady(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo) override;
120     void OnDeviceOffline(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo) override;
121     void OnDeviceChanged(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo) override;
122 
123 private:
124     AVSessionService *servicePtr_ = nullptr;
125 };
126 
127 class EventSubscriber : public EventFwk::CommonEventSubscriber {
128 public:
129     EventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscriberInfo, AVSessionService *ptr);
130     virtual ~EventSubscriber() = default;
131     void OnReceiveEvent(const EventFwk::CommonEventData &eventData) override;
132 private:
133     AVSessionService *servicePtr_ = nullptr;
134 };
135 
136 class AVSessionService : public SystemAbility, public AVSessionServiceStub, public IAVSessionServiceListener {
137     DECLARE_SYSTEM_ABILITY(AVSessionService);
138 
139 public:
140     DISALLOW_COPY_AND_MOVE(AVSessionService);
141 
142     explicit AVSessionService(int32_t systemAbilityId, bool runOnCreate = true);
143 
144     ~AVSessionService() override;
145 
146     void OnDump() override;
147 
148     void OnStart() override;
149 
150     void OnStartProcess();
151 
152     void OnStop() override;
153 
154     int32_t OnIdle(const SystemAbilityOnDemandReason& idleReason) override;
155 
156     void OnActive(const SystemAbilityOnDemandReason& activeReason) override;
157 
158     void PullMigrateStub();
159 
160     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
161 
162     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
163 
164     sptr<IRemoteObject> CreateSessionInner(const std::string& tag, int32_t type,
165                                            const AppExecFwk::ElementName& elementName) override;
166 
167     int32_t CreateSessionInner(const std::string& tag, int32_t type, const AppExecFwk::ElementName& elementName,
168                                sptr<IRemoteObject>& object) override;
169 
170     int32_t CreateSessionInnerWithExtra(const std::string& tag, int32_t type, const std::string& extra,
171                                         const AppExecFwk::ElementName& elementName,
172                                         sptr<IRemoteObject>& object) override;
173 
174     int32_t GetAllSessionDescriptors(std::vector<AVSessionDescriptor>& descriptors) override;
175 
176     int32_t GetSessionDescriptorsBySessionId(const std::string& sessionId, AVSessionDescriptor& descriptor) override;
177 
178     int32_t GetColdStartSessionDescriptors(std::vector<AVSessionDescriptor>& descriptors);
179 
180     int32_t GetHistoricalSessionDescriptors(int32_t maxSize, std::vector<AVSessionDescriptor>& descriptors) override;
181 
182     int32_t GetHistoricalAVQueueInfos(int32_t maxSize, int32_t maxAppSize,
183                                       std::vector<AVQueueInfo>& avQueueInfos) override;
184 
185     int32_t StartAVPlayback(const std::string& bundleName, const std::string& assetId) override;
186 
187     int32_t StartAVPlayback(const std::string& bundleName, const std::string& assetId, const std::string& deviceId);
188 
189     int32_t HandleKeyEvent(const MMI::KeyEvent& keyEvent);
190 
191     int32_t CreateControllerInner(const std::string& sessionId, sptr<IRemoteObject>& object) override;
192 
193     int32_t CreateControllerInner(const std::string& sessionId, sptr<IRemoteObject>& object, pid_t pid);
194 
195 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
196     int32_t GetAVCastControllerInner(const std::string& sessionId, sptr<IRemoteObject>& object) override;
197 #endif
198 
199     int32_t RegisterSessionListener(const sptr<ISessionListener>& listener) override;
200 
201     int32_t RegisterSessionListenerForAllUsers(const sptr<ISessionListener>& listener) override;
202 
203     int32_t SendSystemAVKeyEvent(const MMI::KeyEvent& keyEvent) override;
204 
205     int32_t SendSystemAVKeyEvent(const MMI::KeyEvent& keyEvent, const AAFwk::Want &wantParam) override;
206 
207     int32_t SendSystemControlCommand(const AVControlCommand& command) override;
208 
209     int32_t RegisterClientDeathObserver(const sptr<IClientDeath>& observer) override;
210 
211     void OnClientDied(pid_t pid, pid_t uid);
212 
213     void HandleSessionRelease(std::string sessionId, bool continuePlay = false);
214 
215     void HandleDisableCast();
216 
217     void HandleSessionReleaseInner();
218 
219     void HandleCallStartEvent();
220 
221     void HandleControllerRelease(AVControllerItem& controller);
222 
223     std::int32_t Dump(std::int32_t fd, const std::vector<std::u16string>& args) override;
224 
225     int32_t CastAudio(const SessionToken& token,
226                       const std::vector<AudioStandard::AudioDeviceDescriptor>& sinkAudioDescriptors) override;
227 
228     int32_t CastAudioForAll(const std::vector<AudioStandard::AudioDeviceDescriptor>& sinkAudioDescriptors) override;
229 
NotifyAudioSessionCheckTrigger(const int32_t uid)230     void NotifyAudioSessionCheckTrigger(const int32_t uid)
231     {
232         return NotifyAudioSessionCheck(uid);
233     }
234 
235     void NotifyBackgroundReportCheck(const int32_t uid, const int32_t pid,
236         AudioStandard::StreamUsage streamUsage, AudioStandard::RendererState rendererState);
237 
238     void SuperLauncher(std::string deviceId, std::string serviceName,
239         std::string extraInfo, const std::string& state);
240 
241     void ReleaseSuperLauncher(std::string serviceName);
242 
243     void ConnectSuperLauncher(std::string deviceId, std::string serviceName);
244 
245     void SucceedSuperLauncher(std::string deviceId, std::string extraInfo);
246 
247 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
248     void ReleaseCastSession() override;
249 
250     void CreateSessionByCast(const int64_t castHandle) override;
251 
252     void NotifyDeviceAvailable(const OutputDeviceInfo& castOutputDeviceInfo) override;
253 
254     void NotifyDeviceLogEvent(const DeviceLogEventCode eventId, const int64_t param) override;
255 
256     void NotifyDeviceOffline(const std::string& deviceId) override;
257 
258     void NotifyDeviceStateChange(const DeviceState& deviceState) override;
259 
260     void NotifyMirrorToStreamCast();
261 
262     bool IsMirrorToStreamCastAllowed(sptr<AVSessionItem>& session);
263 
264     __attribute__((no_sanitize("cfi"))) int32_t MirrorToStreamCast(sptr<AVSessionItem>& session);
265 
266     void SetIsSupportMirrorToStream(bool isSupportMirrorToStream) override;
267 
268     void SplitExtraInfo(std::string info);
269 
270     int32_t StartCast(const SessionToken& sessionToken, const OutputDeviceInfo& outputDeviceInfo) override;
271 
272     int32_t StopCast(const SessionToken& sessionToken) override;
273 
274     int32_t checkEnableCast(bool enable) override;
275 
276     void setInCast(bool isInCast) override;
277 #endif
278 
279     int32_t Close(void) override;
280 
281     void AddAvQueueInfoToFile(AVSessionItem& session);
282 
283     std::string GetAVQueueDir(int32_t userId = 0);
284 
285     std::string GetAVSortDir(int32_t userId = 0);
286 
287     void HandleUserEvent(const std::string &type, const int &userId);
288 
289     void HandleRemoveMediaCardEvent();
290 
291     void HandleMediaCardStateChangeEvent(std::string isAppear);
292 
293     void RegisterBundleDeleteEventForHistory(int32_t userId = 0);
294 
295     void NotifyMigrateStop(const std::string &deviceId);
296 
297     void InitCastEngineService();
298 
299     bool ProcessTargetMigrate(bool isOnline, const OHOS::DistributedHardware::DmDeviceInfo& deviceInfo);
300 
301     int32_t GetDistributedSessionControllersInner(const DistributedSessionType& sessionType,
302         std::vector<sptr<IRemoteObject>>& sessionControllers) override;
303 
304     void NotifyRemoteDistributedSessionControllersChanged(
305         const std::vector<sptr<IRemoteObject>>& sessionControllers);
306 
307     void NotifyRemoteBundleChange(const std::string bundleName);
308 
309     bool CheckIfOtherAudioPlaying();
310 
311 private:
312     void NotifyProcessStatus(bool isStart);
313 
314     static SessionContainer& GetContainer();
315     static AVSessionUsersManager& GetUsersManager();
316 
317     std::string AllocSessionId();
318 
319     bool AbilityHasSession(pid_t pid);
320 
321     sptr<AVControllerItem> GetPresentController(pid_t pid, const std::string& sessionId);
322 
323     void NotifySessionCreate(const AVSessionDescriptor& descriptor);
324     void NotifySessionRelease(const AVSessionDescriptor& descriptor);
325     void NotifyTopSessionChanged(const AVSessionDescriptor& descriptor);
326     void NotifyAudioSessionCheck(const int32_t uid);
327     void NotifySystemUI(const AVSessionDescriptor* historyDescriptor, bool isActiveSession, bool addCapsule,
328                         bool isCapsuleUpdate);
329     void PublishEvent(int32_t mediaPlayState);
330 
331     void AddClientDeathObserver(pid_t pid, const sptr<IClientDeath>& observer,
332         const sptr<ClientDeathRecipient> recipient);
333     void RemoveClientDeathObserver(pid_t pid);
334 
335     void AddSessionListener(pid_t pid, const sptr<ISessionListener>& listener);
336     void AddSessionListenerForAllUsers(pid_t pid, const sptr<ISessionListener>& listener);
337     void RemoveSessionListener(pid_t pid);
338 
339     void AddInnerSessionListener(SessionListener* listener);
340     void RemoveInnerSessionListener(SessionListener* listener);
341 
342     void AddHistoricalRecordListener(HistoricalRecordListener* listener);
343     void RemoveHistoricalRecordListener(HistoricalRecordListener* listener);
344 
345     sptr<AVSessionItem> SelectSessionByUid(const AudioStandard::AudioRendererChangeInfo& info);
346 
347     std::function<bool(int32_t, int32_t)> GetAllowedPlaybackCallbackFunc();
348 
349     sptr<AVSessionItem> CreateSessionInner(const std::string& tag, int32_t type, bool thirdPartyApp,
350                                            const AppExecFwk::ElementName& elementName);
351 
352     int32_t CreateSessionInner(const std::string& tag, int32_t type, bool thirdPartyApp,
353                                const AppExecFwk::ElementName& elementName, sptr<AVSessionItem>& sessionItem);
354 
355     int32_t CreateSessionInnerWithExtra(const std::string& tag, int32_t type, const std::string& extraInfo,
356                                         bool thirdPartyApp, const AppExecFwk::ElementName& elementName,
357                                         sptr<AVSessionItem>& sessionItem);
358 
359     bool IsParamInvalid(const std::string& tag, int32_t type, const AppExecFwk::ElementName& elementName);
360 
361     void ServiceCallback(sptr<AVSessionItem>& sessionItem);
362 
363     sptr<AVSessionItem> CreateNewSessionWithExtra(const std::string& tag, int32_t type, const std::string& extraInfo,
364                                                   bool thirdPartyApp, const AppExecFwk::ElementName& elementName);
365 
366     sptr<AVSessionItem> CreateNewSession(const std::string& tag, int32_t type, bool thirdPartyApp,
367                                          const AppExecFwk::ElementName& elementName);
368 
369     sptr<AVControllerItem> CreateNewControllerForSession(pid_t pid, sptr<AVSessionItem>& session);
370 
371     int32_t CancelCastAudioForClientExit(pid_t pid, const sptr<AVSessionItem>& session);
372 
373     void ClearSessionForClientDiedNoLock(pid_t pid, bool continuePlay);
374 
375     void ClearControllerForClientDiedNoLock(pid_t pid);
376 
377     void InitKeyEvent();
378 
379     void InitAudio();
380 
381     void InitAMS();
382 
383     void InitDM();
384 
385     void InitBMS();
386 
387     void InitRadarBMS();
388 
389     void InitAccountMgr();
390 
391     void InitCommonEventService();
392 
393     void InitCollaboration();
394 
395     bool SelectFocusSession(const FocusSessionStrategy::FocusSessionChangeInfo& info);
396 
397     void RefreshFocusSessionSort(sptr<AVSessionItem> &session);
398 
399     void UpdateTopSession(const sptr<AVSessionItem>& newTopSession, int32_t userId = 0);
400 
401     sptr<AVSessionItem> GetOtherPlayingSession(int32_t userId, std::string bundleName);
402 
403     void HandleOtherSessionPlaying(sptr<AVSessionItem>& session);
404 
405     void HandleFocusSession(const FocusSessionStrategy::FocusSessionChangeInfo& info, bool isPlaying);
406 
407     __attribute__((no_sanitize("cfi"))) std::shared_ptr<RemoteSessionCommandProcess> GetService(
408         const std::string& deviceId);
409 
410     int32_t CastAudioProcess(const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors,
411                              const std::string& sourceSessionInfo,
412                              sptr<AVSessionItem>& session);
413 
414     int32_t CastAudioInner(const std::vector<AudioStandard::AudioDeviceDescriptor>& sinkAudioDescriptors,
415                            const std::string& sourceSessionInfo,
416                            const sptr<AVSessionItem>& session);
417 
418     int32_t CancelCastAudioInner(const std::vector<AudioStandard::AudioDeviceDescriptor>& sinkDevices,
419                                  const std::string& sourceSessionInfo,
420                                  const sptr<AVSessionItem>& session);
421 
422     int32_t RemoteCastAudioInner(const std::string& sourceSessionInfo, std::string& sinkSessionInfo);
423 
424     int32_t RemoteCancelCastAudioInner(const std::string& sessionInfo);
425 
426     int32_t ProcessCastAudioCommand(const RemoteServiceCommand command, const std::string& input,
427                                     std::string& output) override;
428 
429     int32_t CastAudioForNewSession(const sptr<AVSessionItem>& session);
430 
431     bool IsLocalDevice(const std::string& networkId);
432 
433     int32_t GetLocalNetworkId(std::string& networkId);
434 
435     int32_t GetTrustedDeviceName(const std::string& networkId, std::string& deviceName);
436 
437     int32_t GetTrustedDevicesInfo(std::vector<OHOS::DistributedHardware::DmDeviceInfo>& deviceList);
438 
439     int32_t SetBasicInfo(std::string& sessionInfo);
440 
441     void SetDeviceInfo(const std::vector<AudioStandard::AudioDeviceDescriptor>& castAudioDescriptors,
442                            sptr<AVSessionItem>& session);
443 
444     int32_t GetAudioDescriptor(const std::string deviceId,
445                                std::vector<AudioStandard::AudioDeviceDescriptor>& audioDeviceDescriptors);
446 
447     bool GetAudioDescriptorByDeviceId(
448         const std::vector<std::shared_ptr<AudioStandard::AudioDeviceDescriptor>>& descriptors,
449         const std::string& deviceId,
450         AudioStandard::AudioDeviceDescriptor& audioDescriptor);
451 
452     void GetDeviceInfo(const sptr<AVSessionItem>& session,
453                            const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors,
454                            std::vector<AudioStandard::AudioDeviceDescriptor>& castSinkDescriptors,
455                            std::vector<AudioStandard::AudioDeviceDescriptor>& cancelSinkDescriptors);
456 
457     int32_t SelectOutputDevice(const int32_t uid, const AudioStandard::AudioDeviceDescriptor& descriptor);
458 
459     int32_t StartDefaultAbilityByCall(std::string& sessionId);
460 
461     int32_t StartAbilityByCall(const std::string& sessionIdNeeded, std::string& sessionId);
462 
463     void HandleEventHandlerCallBack();
464 
465     AVControlCommand GetSessionProcCommand();
466 
467     bool IsHistoricalSession(const std::string& sessionId);
468 
469     void DeleteHistoricalRecord(const std::string& bundleName, int32_t userId = 0);
470 
471     void DeleteAVQueueInfoRecord(const std::string& bundleName, int32_t userId = 0);
472 
473     const cJSON* GetSubNode(const cJSON* nodeItem, const std::string& name);
474 
475     void SaveSessionInfoInFile(const std::string& sessionId, const std::string& sessionType,
476         const AppExecFwk::ElementName& elementName);
477 
478     bool CheckAndCreateDir(const std::string& filePath);
479 
480     bool CheckUserDirValid(int32_t userId = 0);
481 
482     bool LoadStringFromFileEx(const std::string& filePath, std::string& content);
483 
484     bool SaveStringToFileEx(const std::string& filePath, const std::string& content);
485 
486     bool CheckStringAndCleanFile(const std::string& filePath);
487 
488     void ClearClientResources(pid_t pid, bool continuePlay);
489 
490     bool SaveAvQueueInfo(std::string& oldContent, const std::string &bundleName,
491         const AVMetaData& meta, const int32_t userId);
492 
493     void DeleteAVQueueImage(cJSON* item);
494 
495     int32_t GetHistoricalSessionDescriptorsFromFile(std::vector<AVSessionDescriptor>& descriptors);
496 
497     void ReportStartCastBegin(std::string func, const OutputDeviceInfo& outputDeviceInfo, int32_t uid);
498 
499     void ReportStartCastEnd(std::string func, const OutputDeviceInfo &outputDeviceInfo, int32_t uid, int ret);
500 
501     void HandleAppStateChange(int uid, int state);
502 
503     void UpdateFrontSession(sptr<AVSessionItem>& sessionItem, bool isAdd);
504 
505     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> CreateWantAgent(
506         const AVSessionDescriptor* histroyDescriptor);
507 
508     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> CreateNftRemoveWant(int32_t uid);
509 
510     void DoMetadataImgClean(AVMetaData& data);
511 
512     void HandleSystemKeyColdStart(const AVControlCommand &command, const std::string deviceId = "");
513 
514     bool SubscribeCommonEvent();
515 
516     bool UnSubscribeCommonEvent();
517 
518     void ReportSessionInfo(const sptr <AVSessionItem>& session, int res);
519 
520     bool CheckAncoAudio();
521 
522     int32_t ConvertKeyCodeToCommand(int keyCode);
523 
524     void RemoveExpired(std::list<std::chrono::system_clock::time_point> &list,
525         const std::chrono::system_clock::time_point &now, int32_t time = 1);
526 
527     void LowQualityCheck(int32_t uid, int32_t pid, AudioStandard::StreamUsage streamUsage,
528         AudioStandard::RendererState rendererState);
529 
530     void PlayStateCheck(int32_t uid, AudioStandard::StreamUsage streamUsage,
531         AudioStandard::RendererState rState);
532 
533     std::shared_ptr<std::list<sptr<AVSessionItem>>> GetCurSessionListForFront(int32_t userId = 0);
534 
535     std::shared_ptr<std::list<sptr<AVSessionItem>>> GetCurKeyEventSessionList(int32_t userId = 0);
536 
537     void AddCapsuleServiceCallback(sptr<AVSessionItem>& sessionItem);
538 
539     void AddCastCapsuleServiceCallback(sptr<AVSessionItem>& sessionItem);
540 
541     bool VerifyNotification();
542 
543     void HandleChangeTopSession(int32_t infoUid, int32_t infoPid, int32_t userId);
544 
545     bool UpdateOrder(sptr<AVSessionItem>& sessionItem);
546 
547     bool IsTopSessionPlaying();
548 
549     bool NotifyFlowControl();
550 
551     int32_t GetLocalDeviceType();
552 
553     void DoTargetDevListenWithDM();
554 
555     void DoRemoteAVSessionLoad(std::string remoteDeviceId);
556 
557     void DoConnectProcessWithMigrate(const OHOS::DistributedHardware::DmDeviceInfo& deviceInfo);
558 
559     void DoConnectProcessWithMigrateServer(const OHOS::DistributedHardware::DmDeviceInfo& deviceInfo);
560 
561     void DoConnectProcessWithMigrateProxy(const OHOS::DistributedHardware::DmDeviceInfo& deviceInfo);
562 
563     void DoDisconnectProcessWithMigrate(const OHOS::DistributedHardware::DmDeviceInfo& deviceInfo);
564 
565     void DoDisconnectProcessWithMigrateServer(const OHOS::DistributedHardware::DmDeviceInfo& deviceInfo);
566 
567     void DoDisconnectProcessWithMigrateProxy(const OHOS::DistributedHardware::DmDeviceInfo& deviceInfo);
568 
569     int32_t DoHisMigrateServerTransform(std::string networkId);
570 
571     void UpdateLocalFrontSession(std::shared_ptr<std::list<sptr<AVSessionItem>>> sessionListForFront);
572 
573     void NotifyLocalFrontSessionChangeForMigrate(std::string localFrontSessionIdUpdate);
574 
575     bool CheckWhetherTargetDevIsNext(const OHOS::DistributedHardware::DmDeviceInfo& deviceInfo);
576 
577     void NotifyHistoricalRecordChange(const std::string& bundleName, int32_t userId);
578 
579     bool IsCapsuleNeeded();
580 
581     void AddKeyEventServiceCallback(sptr<AVSessionItem>& sessionItem);
582 
583     std::string GetLocalTitle();
584 
585     bool InsertSessionItemToCJSON(sptr<AVSessionItem> &session, cJSON* valuesArray);
586 
587     bool InsertSessionItemToCJSONAndPrint(const std::string& sessionId, const std::string& sessionType,
588         const AppExecFwk::ElementName& elementName, cJSON* valuesArray);
589 
590     void ProcessDescriptorsFromCJSON(std::vector<AVSessionDescriptor>& descriptors, cJSON* valueItem);
591 
592     void ProcessAvQueueInfosFromCJSON(std::vector<AVQueueInfo>& avQueueInfos, cJSON* valueItem);
593 
594     bool InsertAvQueueInfoToCJSONAndPrint(const std::string &bundleName,
595         const AVMetaData& meta, const int32_t userId, cJSON* valuesArray);
596 
597     bool GetDefaultAbilityElementNameFromCJSON(std::string& sortContent,
598         std::string& bundleName, std::string& abilityName);
599 
600     bool GetElementNameBySessionIdFromCJSON(std::string& sortContent, const std::string& sessionIdNeeded,
601         std::string& bundleName, std::string& abilityName);
602 
603     void DeleteAVQueueInfoRecordFromCJSON(std::string& sortContent, const std::string& bundleName, int32_t userId);
604 
605     bool FillFileWithEmptyContentEx(ofstream& fileWrite);
606 
607     void CheckAndUpdateAncoMediaSession(const AppExecFwk::ElementName& elementName);
608 
609     void UpdateSessionTimestamp(sptr<AVSessionItem> session);
610 
611     bool CheckSessionHandleKeyEvent(bool procCmd, AVControlCommand cmd, const MMI::KeyEvent& keyEvent,
612         sptr<AVSessionItem> session);
613 
614     bool IsAncoValid();
615 
616     std::string DoCJSONArrayTransformToString(cJSON* valueItem);
617 
618     void HandleTopSessionRelease(int32_t userId, sptr<AVSessionItem>& sessionItem);
619 
620 #ifdef ENABLE_AVSESSION_SYSEVENT_CONTROL
621     void ReportSessionState(const sptr<AVSessionItem>& session, SessionState state);
622     void ReportSessionControl(const std::string& bundleName, int32_t cmd);
623 #endif
624 
625     std::atomic<uint32_t> sessionSeqNum_ {};
626     std::atomic<bool> isMediaCardOpen_ = false;
627     std::atomic<bool> hasRemoveEvent_ = false;
628     std::atomic<bool> hasMediaCapsule_ = false;
629     std::atomic<bool> hasCardStateChangeStopTask_ = false;
630 
631     sptr<AVSessionItem> topSession_;
632     sptr<AVSessionItem> ancoSession_;
633     std::map<pid_t, std::list<sptr<AVControllerItem>>> controllers_;
634     std::map<pid_t, sptr<IClientDeath>> clientDeathObservers_;
635     std::map<pid_t, sptr<ClientDeathRecipient>> clientDeathRecipients_;
636     std::list<SessionListener*> innerSessionListeners_;
637     std::list<HistoricalRecordListener*> historicalRecordListeners_;
638     std::map<std::string, std::shared_ptr<AbilityManagerAdapter>> abilityManager_;
639     FocusSessionStrategy focusSessionStrategy_;
640     BackgroundAudioController backgroundAudioController_;
641     std::map<std::string, std::string> castAudioSessionMap_;
642     bool isAllSessionCast_ {};
643     std::string outputDeviceId_;
644     std::unique_ptr<AVSessionDumper> dumpHelper_ {};
645     friend class AVSessionDumper;
646     std::shared_ptr<MigrateAVSessionServer> migrateAVSession_;
647     std::shared_ptr<EventSubscriber> subscriber_;
648     bool isScreenOn_ = false;
649     bool isScreenLocked_ = true;
650     std::list<std::chrono::system_clock::time_point> flowControlPublishTimestampList_;
651     std::function<bool(int32_t, int32_t)> queryAllowedPlaybackCallbackFunc_;
652 
653     // The following locks are used in the defined order of priority
654     std::recursive_mutex sessionServiceLock_;
655 
656     std::recursive_mutex sessionFrontLock_;
657 
658     std::recursive_mutex sessionListenersLock_;
659 
660     std::recursive_mutex migrateListenersLock_;
661 
662     std::recursive_mutex sessionFileLock_;
663 
664     std::recursive_mutex avQueueFileLock_;
665 
666     std::recursive_mutex abilityManagerLock_;
667 
668     std::recursive_mutex screenStateLock_;
669 
670     std::recursive_mutex clientDeathLock_;
671 
672     std::recursive_mutex notifyLock_;
673 
674     // DMSDP related locks
675     std::recursive_mutex isAllSessionCastLock_;
676 
677     std::recursive_mutex outputDeviceIdLock_;
678 
679     std::recursive_mutex castAudioSessionMapLock_;
680 
681     std::recursive_mutex historicalRecordListenersLock_;
682 
683     std::recursive_mutex keyEventListLock_;
684 
685 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
686     std::pair<std::string, std::string> castServiceNameStatePair_;
687     const std::string deviceStateConnection = "CONNECT_SUCC";
688     const std::string deviceStateDisconnection = "IDLE";
689     const std::string seperator = ",";
690     int appState = -1;
691     std::atomic<bool> isSupportMirrorToStream_ = false;
692     std::string castDeviceId_ = "0";
693     std::string castDeviceName_ = " ";
694     int32_t castDeviceType_ = 0;
695     const int32_t beginAddPos = 3;
696     const int32_t endDecPos = 4;
697     const int32_t typeAddPos = 2;
698     std::recursive_mutex checkEnableCastLock_;
699 #endif
700 
701     static constexpr const char *SORT_FILE_NAME = "sortinfo";
702     static constexpr const char *DEFAULT_SESSION_ID = "default";
703     static constexpr const char *DEFAULT_BUNDLE_NAME = "com.example.himusicdemo";
704     static constexpr const char *DEFAULT_ABILITY_NAME = "MainAbility";
705     static constexpr const int32_t SYSTEMUI_LIVEVIEW_TYPECODE_MDEDIACONTROLLER = 2;
706     static constexpr const char *AVQUEUE_FILE_NAME = "avqueueinfo";
707     static constexpr const char *sessionCastState_ = "CAST_STATE";
708 
709     const std::string MEDIA_CONTROL_BUNDLENAME = "com.ohos.mediacontroller";
710     const std::string MEDIA_CONTROL_ABILITYNAME = "com.ohos.mediacontroller.avplayer.mainability";
711     const std::string MEDIA_CONTROL_STATE = "usual.event.MEDIA_CONTROL_STATE";
712     const std::string MEDIA_PLAY_STATE = "MediaPlaybackState";
713 
714     int32_t pressCount_ {};
715     int32_t maxHistoryNums_ = 10;
716     bool isFirstPress_ = true;
717     bool isInCast_ = false;
718     bool is2in1_ = false;
719 
720     void *migrateStubFuncHandle_ = nullptr;
721 
722     std::shared_ptr<AVSessionDeviceStateCallback> deviceStateCallback_ = nullptr;
723     int32_t localDeviceType_ = OHOS::DistributedHardware::DmDeviceType::DEVICE_TYPE_PHONE;
724     int32_t targetDeviceType_ = OHOS::DistributedHardware::DmDeviceType::DEVICE_TYPE_WATCH;
725     const std::string serviceName = "av_session";
726     std::string deviceIdForMigrate_ = "";
727     std::string localFrontSessionId_ = "";
728     bool isMigrateTargetFound_ = false;
729     std::map<std::string, std::shared_ptr<MigrateAVSessionServer>> migrateAVSessionServerMap_;
730     std::map<std::string, std::shared_ptr<SoftbusSession>> migrateAVSessionProxyMap_;
731 
732     const int32_t ONE_CLICK = 1;
733     const int32_t DOUBLE_CLICK = 2;
734     const int32_t THREE_CLICK = 3;
735     const int32_t unSetHistoryNum = 3;
736     const int32_t CLICK_TIMEOUT = 500;
737     const int32_t lowQualityTimeout = 1000;
738     const int32_t errorStateTimeout = 3 * 1000;
739     const int32_t defMaxHistoryNum = 10;
740     const int32_t maxFileLength = 32 * 1024 * 1024;
741     const int32_t maxAVQueueInfoLen = 99;
742     const int32_t allocSpace = 2;
743     const int32_t avSessionUid = 6700;
744     const int32_t ancoUid = 1041;
745     const int32_t audioBrokerUid = 5557;
746     const int32_t saType = 1;
747     const int32_t remoteMediaNone = 4;
748     const int32_t remoteMediaAlive = 3;
749     const int32_t mediaPlayStateTrue = 1;
750     const int32_t mediaPlayStateFalse = 0;
751     const uint32_t MAX_NOTIFICATION_NUM = 3;
752     const int32_t NOTIFICATION_CONTROL_TIME = 1000;
753     const int32_t cancelTimeout = 5000;
754     const uint8_t doRemoteLoadRetryTime = 5;
755     const int32_t defaultUserId = 100;
756 };
757 } // namespace OHOS::AVSession
758 #endif // OHOS_AVSESSION_SERVICE_H
759