180922886Sopenharmony_ci/*
280922886Sopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
380922886Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
480922886Sopenharmony_ci * you may not use this file except in compliance with the License.
580922886Sopenharmony_ci * You may obtain a copy of the License at
680922886Sopenharmony_ci *
780922886Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
880922886Sopenharmony_ci *
980922886Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1080922886Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1180922886Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1280922886Sopenharmony_ci * See the License for the specific language governing permissions and
1380922886Sopenharmony_ci * limitations under the License.
1480922886Sopenharmony_ci */
1580922886Sopenharmony_ci
1680922886Sopenharmony_ci#ifndef OHOS_AVSESSION_SERVICE_H
1780922886Sopenharmony_ci#define OHOS_AVSESSION_SERVICE_H
1880922886Sopenharmony_ci
1980922886Sopenharmony_ci#include <atomic>
2080922886Sopenharmony_ci#include <mutex>
2180922886Sopenharmony_ci#include <map>
2280922886Sopenharmony_ci
2380922886Sopenharmony_ci#include "iremote_stub.h"
2480922886Sopenharmony_ci#include "system_ability.h"
2580922886Sopenharmony_ci#include "audio_info.h"
2680922886Sopenharmony_ci#include "avsession_service_stub.h"
2780922886Sopenharmony_ci#include "avsession_item.h"
2880922886Sopenharmony_ci#include "avcontroller_item.h"
2980922886Sopenharmony_ci#include "session_container.h"
3080922886Sopenharmony_ci#include "iclient_death.h"
3180922886Sopenharmony_ci#include "isession_listener.h"
3280922886Sopenharmony_ci#include "focus_session_strategy.h"
3380922886Sopenharmony_ci#include "background_audio_controller.h"
3480922886Sopenharmony_ci#include "ability_manager_adapter.h"
3580922886Sopenharmony_ci#include "device_manager.h"
3680922886Sopenharmony_ci#include "dm_device_info.h"
3780922886Sopenharmony_ci#include "audio_adapter.h"
3880922886Sopenharmony_ci#include "remote_session_command_process.h"
3980922886Sopenharmony_ci#include "i_avsession_service_listener.h"
4080922886Sopenharmony_ci#include "avqueue_info.h"
4180922886Sopenharmony_ci#include "migrate/migrate_avsession_server.h"
4280922886Sopenharmony_ci
4380922886Sopenharmony_ci#include "common_event_manager.h"
4480922886Sopenharmony_ci#include "common_event_subscribe_info.h"
4580922886Sopenharmony_ci#include "common_event_subscriber.h"
4680922886Sopenharmony_ci#include "common_event_support.h"
4780922886Sopenharmony_ci#include "matching_skills.h"
4880922886Sopenharmony_ci
4980922886Sopenharmony_ci#include "avsession_users_manager.h"
5080922886Sopenharmony_ci
5180922886Sopenharmony_ci#ifdef BLUETOOTH_ENABLE
5280922886Sopenharmony_ci#include "bluetooth_host.h"
5380922886Sopenharmony_ci#endif
5480922886Sopenharmony_ci
5580922886Sopenharmony_cinamespace OHOS::AVSession {
5680922886Sopenharmony_ciclass AVSessionDumper;
5780922886Sopenharmony_ci
5880922886Sopenharmony_ciclass ClientDeathRecipient : public IRemoteObject::DeathRecipient {
5980922886Sopenharmony_cipublic:
6080922886Sopenharmony_ci    explicit ClientDeathRecipient(const std::function<void()>& callback);
6180922886Sopenharmony_ci
6280922886Sopenharmony_ci    void OnRemoteDied(const wptr<IRemoteObject>& object) override;
6380922886Sopenharmony_ci
6480922886Sopenharmony_ciprivate:
6580922886Sopenharmony_ci    std::function<void()> callback_;
6680922886Sopenharmony_ci};
6780922886Sopenharmony_ci
6880922886Sopenharmony_ciclass AVSessionInitDMCallback : public OHOS::DistributedHardware::DmInitCallback {
6980922886Sopenharmony_cipublic:
7080922886Sopenharmony_ci    AVSessionInitDMCallback() = default;
7180922886Sopenharmony_ci    ~AVSessionInitDMCallback() override = default;
7280922886Sopenharmony_ci    void OnRemoteDied() override {};
7380922886Sopenharmony_ci};
7480922886Sopenharmony_ci
7580922886Sopenharmony_ci#ifdef BLUETOOTH_ENABLE
7680922886Sopenharmony_ciclass DetectBluetoothHostObserver : public OHOS::Bluetooth::BluetoothHostObserver {
7780922886Sopenharmony_cipublic:
7880922886Sopenharmony_ci    DetectBluetoothHostObserver(AVSessionService *ptr);
7980922886Sopenharmony_ci    virtual ~DetectBluetoothHostObserver() = default;
8080922886Sopenharmony_ci    void OnStateChanged(const int transport, const int status) override;
8180922886Sopenharmony_ci    void OnDiscoveryStateChanged(int status) override
8280922886Sopenharmony_ci    {
8380922886Sopenharmony_ci        return;
8480922886Sopenharmony_ci    }
8580922886Sopenharmony_ci
8680922886Sopenharmony_ci    void OnDiscoveryResult(const OHOS::Bluetooth::BluetoothRemoteDevice &device, int rssi,
8780922886Sopenharmony_ci        const std::string deviceName, int deviceClass) override
8880922886Sopenharmony_ci    {
8980922886Sopenharmony_ci        return;
9080922886Sopenharmony_ci    }
9180922886Sopenharmony_ci
9280922886Sopenharmony_ci    void OnPairRequested(const OHOS::Bluetooth::BluetoothRemoteDevice &device) override
9380922886Sopenharmony_ci    {
9480922886Sopenharmony_ci        return;
9580922886Sopenharmony_ci    }
9680922886Sopenharmony_ci
9780922886Sopenharmony_ci    void OnPairConfirmed(const OHOS::Bluetooth::BluetoothRemoteDevice &device, int reqType, int number) override
9880922886Sopenharmony_ci    {
9980922886Sopenharmony_ci        return;
10080922886Sopenharmony_ci    }
10180922886Sopenharmony_ci
10280922886Sopenharmony_ci    void OnScanModeChanged(int mode) override
10380922886Sopenharmony_ci    {
10480922886Sopenharmony_ci        return;
10580922886Sopenharmony_ci    }
10680922886Sopenharmony_ci
10780922886Sopenharmony_ci    void OnDeviceNameChanged(const std::string &deviceName) override
10880922886Sopenharmony_ci    {
10980922886Sopenharmony_ci        return;
11080922886Sopenharmony_ci    }
11180922886Sopenharmony_ci
11280922886Sopenharmony_ci    void OnDeviceAddrChanged(const std::string &address) override
11380922886Sopenharmony_ci    {
11480922886Sopenharmony_ci        return;
11580922886Sopenharmony_ci    }
11680922886Sopenharmony_ci
11780922886Sopenharmony_ciprivate:
11880922886Sopenharmony_ci    bool is2in1_ = false;
11980922886Sopenharmony_ci    bool lastEnabled_ = false;
12080922886Sopenharmony_ci    AVSessionService *servicePtr_ = nullptr;
12180922886Sopenharmony_ci};
12280922886Sopenharmony_ci#endif
12380922886Sopenharmony_ci
12480922886Sopenharmony_ciclass EventSubscriber : public EventFwk::CommonEventSubscriber {
12580922886Sopenharmony_cipublic:
12680922886Sopenharmony_ci    EventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscriberInfo, AVSessionService *ptr);
12780922886Sopenharmony_ci    virtual ~EventSubscriber() = default;
12880922886Sopenharmony_ci    void OnReceiveEvent(const EventFwk::CommonEventData &eventData) override;
12980922886Sopenharmony_ciprivate:
13080922886Sopenharmony_ci    AVSessionService *servicePtr_ = nullptr;
13180922886Sopenharmony_ci};
13280922886Sopenharmony_ci
13380922886Sopenharmony_ciclass AVSessionService : public SystemAbility, public AVSessionServiceStub, public IAVSessionServiceListener {
13480922886Sopenharmony_ci    DECLARE_SYSTEM_ABILITY(AVSessionService);
13580922886Sopenharmony_ci
13680922886Sopenharmony_cipublic:
13780922886Sopenharmony_ci    DISALLOW_COPY_AND_MOVE(AVSessionService);
13880922886Sopenharmony_ci
13980922886Sopenharmony_ci    explicit AVSessionService(int32_t systemAbilityId, bool runOnCreate = true);
14080922886Sopenharmony_ci
14180922886Sopenharmony_ci    ~AVSessionService() override;
14280922886Sopenharmony_ci
14380922886Sopenharmony_ci    void OnDump() override;
14480922886Sopenharmony_ci
14580922886Sopenharmony_ci    void OnStart() override;
14680922886Sopenharmony_ci
14780922886Sopenharmony_ci    void OnStop() override;
14880922886Sopenharmony_ci
14980922886Sopenharmony_ci    void PullMigrateStub();
15080922886Sopenharmony_ci
15180922886Sopenharmony_ci    void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
15280922886Sopenharmony_ci
15380922886Sopenharmony_ci    void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
15480922886Sopenharmony_ci
15580922886Sopenharmony_ci    sptr<IRemoteObject> CreateSessionInner(const std::string& tag, int32_t type,
15680922886Sopenharmony_ci                                           const AppExecFwk::ElementName& elementName) override;
15780922886Sopenharmony_ci
15880922886Sopenharmony_ci    int32_t CreateSessionInner(const std::string& tag, int32_t type, const AppExecFwk::ElementName& elementName,
15980922886Sopenharmony_ci                               sptr<IRemoteObject>& object) override;
16080922886Sopenharmony_ci
16180922886Sopenharmony_ci    int32_t GetAllSessionDescriptors(std::vector<AVSessionDescriptor>& descriptors) override;
16280922886Sopenharmony_ci
16380922886Sopenharmony_ci    int32_t GetSessionDescriptorsBySessionId(const std::string& sessionId, AVSessionDescriptor& descriptor) override;
16480922886Sopenharmony_ci
16580922886Sopenharmony_ci    int32_t GetHistoricalSessionDescriptors(int32_t maxSize, std::vector<AVSessionDescriptor>& descriptors) override;
16680922886Sopenharmony_ci
16780922886Sopenharmony_ci    int32_t GetHistoricalAVQueueInfos(int32_t maxSize, int32_t maxAppSize,
16880922886Sopenharmony_ci                                      std::vector<AVQueueInfo>& avQueueInfos) override;
16980922886Sopenharmony_ci
17080922886Sopenharmony_ci    int32_t StartAVPlayback(const std::string& bundleName, const std::string& assetId) override;
17180922886Sopenharmony_ci
17280922886Sopenharmony_ci    int32_t CreateControllerInner(const std::string& sessionId, sptr<IRemoteObject>& object) override;
17380922886Sopenharmony_ci
17480922886Sopenharmony_ci    bool IsAudioPlaybackAllowed(const int32_t uid, const int32_t pid) override;
17580922886Sopenharmony_ci
17680922886Sopenharmony_ci#ifdef CASTPLUS_CAST_ENGINE_ENABLE
17780922886Sopenharmony_ci    int32_t GetAVCastControllerInner(const std::string& sessionId, sptr<IRemoteObject>& object) override;
17880922886Sopenharmony_ci#endif
17980922886Sopenharmony_ci
18080922886Sopenharmony_ci    int32_t RegisterSessionListener(const sptr<ISessionListener>& listener) override;
18180922886Sopenharmony_ci
18280922886Sopenharmony_ci    int32_t RegisterSessionListenerForAllUsers(const sptr<ISessionListener>& listener) override;
18380922886Sopenharmony_ci
18480922886Sopenharmony_ci    int32_t SendSystemAVKeyEvent(const MMI::KeyEvent& keyEvent) override;
18580922886Sopenharmony_ci
18680922886Sopenharmony_ci    int32_t SendSystemControlCommand(const AVControlCommand& command) override;
18780922886Sopenharmony_ci
18880922886Sopenharmony_ci    int32_t RegisterClientDeathObserver(const sptr<IClientDeath>& observer) override;
18980922886Sopenharmony_ci
19080922886Sopenharmony_ci    void OnClientDied(pid_t pid);
19180922886Sopenharmony_ci
19280922886Sopenharmony_ci    void HandleSessionRelease(std::string sessionId);
19380922886Sopenharmony_ci
19480922886Sopenharmony_ci    void HandleCallStartEvent();
19580922886Sopenharmony_ci
19680922886Sopenharmony_ci#ifdef CASTPLUS_CAST_ENGINE_ENABLE
19780922886Sopenharmony_ci    __attribute__((no_sanitize("cfi"))) int32_t MirrorToStreamCast(sptr<AVSessionItem>& session);
19880922886Sopenharmony_ci
19980922886Sopenharmony_ci    void SplitExtraInfo(std::string info);
20080922886Sopenharmony_ci#endif
20180922886Sopenharmony_ci
20280922886Sopenharmony_ci    void HandleControllerRelease(AVControllerItem& controller);
20380922886Sopenharmony_ci
20480922886Sopenharmony_ci    std::int32_t Dump(std::int32_t fd, const std::vector<std::u16string>& args) override;
20580922886Sopenharmony_ci
20680922886Sopenharmony_ci    int32_t CastAudio(const SessionToken& token,
20780922886Sopenharmony_ci                      const std::vector<AudioStandard::AudioDeviceDescriptor>& sinkAudioDescriptors) override;
20880922886Sopenharmony_ci
20980922886Sopenharmony_ci    int32_t CastAudioForAll(const std::vector<AudioStandard::AudioDeviceDescriptor>& sinkAudioDescriptors) override;
21080922886Sopenharmony_ci
21180922886Sopenharmony_ci    void NotifyAudioSessionCheckTrigger(const int32_t uid)
21280922886Sopenharmony_ci    {
21380922886Sopenharmony_ci        return NotifyAudioSessionCheck(uid);
21480922886Sopenharmony_ci    }
21580922886Sopenharmony_ci
21680922886Sopenharmony_ci    void SuperLauncher(std::string deviceId, std::string serviceName,
21780922886Sopenharmony_ci        std::string extraInfo, const std::string& state);
21880922886Sopenharmony_ci
21980922886Sopenharmony_ci#ifdef CASTPLUS_CAST_ENGINE_ENABLE
22080922886Sopenharmony_ci    void ReleaseCastSession() override;
22180922886Sopenharmony_ci
22280922886Sopenharmony_ci    void CreateSessionByCast(const int64_t castHandle) override;
22380922886Sopenharmony_ci
22480922886Sopenharmony_ci    void NotifyDeviceAvailable(const OutputDeviceInfo& castOutputDeviceInfo) override;
22580922886Sopenharmony_ci
22680922886Sopenharmony_ci    void NotifyDeviceLogEvent(const DeviceLogEventCode eventId, const int64_t param) override;
22780922886Sopenharmony_ci
22880922886Sopenharmony_ci    void NotifyDeviceOffline(const std::string& deviceId) override;
22980922886Sopenharmony_ci
23080922886Sopenharmony_ci    void NotifyMirrorToStreamCast();
23180922886Sopenharmony_ci
23280922886Sopenharmony_ci    int32_t StartCast(const SessionToken& sessionToken, const OutputDeviceInfo& outputDeviceInfo) override;
23380922886Sopenharmony_ci
23480922886Sopenharmony_ci    int32_t StopCast(const SessionToken& sessionToken) override;
23580922886Sopenharmony_ci
23680922886Sopenharmony_ci    int32_t checkEnableCast(bool enable) override;
23780922886Sopenharmony_ci#endif
23880922886Sopenharmony_ci
23980922886Sopenharmony_ci    int32_t Close(void) override;
24080922886Sopenharmony_ci
24180922886Sopenharmony_ci    void AddAvQueueInfoToFile(AVSessionItem& session);
24280922886Sopenharmony_ci
24380922886Sopenharmony_ci    void SetScreenOn(bool on);
24480922886Sopenharmony_ci
24580922886Sopenharmony_ci    bool GetScreenOn();
24680922886Sopenharmony_ci
24780922886Sopenharmony_ci    void SetScreenLocked(bool isLocked);
24880922886Sopenharmony_ci
24980922886Sopenharmony_ci    bool GetScreenLocked();
25080922886Sopenharmony_ci
25180922886Sopenharmony_ci    std::string GetAVQueueDir(int32_t userId = 0);
25280922886Sopenharmony_ci
25380922886Sopenharmony_ci    std::string GetAVSortDir(int32_t userId = 0);
25480922886Sopenharmony_ci
25580922886Sopenharmony_ci    void HandleUserEvent(const std::string &type, const int &userId);
25680922886Sopenharmony_ci
25780922886Sopenharmony_ci    void HandleScreenStatusChange(std::string event);
25880922886Sopenharmony_ci
25980922886Sopenharmony_ci    void RegisterBundleDeleteEventForHistory(int32_t userId = 0);
26080922886Sopenharmony_ci
26180922886Sopenharmony_ciprivate:
26280922886Sopenharmony_ci    void CheckBrEnable();
26380922886Sopenharmony_ci
26480922886Sopenharmony_ci    void NotifyProcessStatus(bool isStart);
26580922886Sopenharmony_ci
26680922886Sopenharmony_ci    static SessionContainer& GetContainer();
26780922886Sopenharmony_ci    static AVSessionUsersManager& GetUsersManager();
26880922886Sopenharmony_ci
26980922886Sopenharmony_ci    std::string AllocSessionId();
27080922886Sopenharmony_ci
27180922886Sopenharmony_ci    bool AbilityHasSession(pid_t pid);
27280922886Sopenharmony_ci
27380922886Sopenharmony_ci    sptr<AVControllerItem> GetPresentController(pid_t pid, const std::string& sessionId);
27480922886Sopenharmony_ci
27580922886Sopenharmony_ci    void NotifySessionCreate(const AVSessionDescriptor& descriptor);
27680922886Sopenharmony_ci    void NotifySessionRelease(const AVSessionDescriptor& descriptor);
27780922886Sopenharmony_ci    void NotifyTopSessionChanged(const AVSessionDescriptor& descriptor);
27880922886Sopenharmony_ci    void NotifyAudioSessionCheck(const int32_t uid);
27980922886Sopenharmony_ci    void NotifySystemUI(const AVSessionDescriptor* historyDescriptor, bool isActiveSession);
28080922886Sopenharmony_ci    void NotifyDeviceChange(const DeviceChangeAction& deviceChangeAction);
28180922886Sopenharmony_ci
28280922886Sopenharmony_ci    void AddClientDeathObserver(pid_t pid, const sptr<IClientDeath>& observer,
28380922886Sopenharmony_ci        const sptr<ClientDeathRecipient> recipient);
28480922886Sopenharmony_ci    void RemoveClientDeathObserver(pid_t pid);
28580922886Sopenharmony_ci
28680922886Sopenharmony_ci    void AddSessionListener(pid_t pid, const sptr<ISessionListener>& listener);
28780922886Sopenharmony_ci    void AddSessionListenerForAllUsers(pid_t pid, const sptr<ISessionListener>& listener);
28880922886Sopenharmony_ci    void RemoveSessionListener(pid_t pid);
28980922886Sopenharmony_ci
29080922886Sopenharmony_ci    void AddInnerSessionListener(SessionListener* listener);
29180922886Sopenharmony_ci    void RemoveInnerSessionListener(SessionListener* listener);
29280922886Sopenharmony_ci
29380922886Sopenharmony_ci    sptr<AVSessionItem> SelectSessionByUid(const AudioStandard::AudioRendererChangeInfo& info);
29480922886Sopenharmony_ci
29580922886Sopenharmony_ci    void OutputDeviceChangeListener(const AudioRendererChangeInfos& infos);
29680922886Sopenharmony_ci
29780922886Sopenharmony_ci    sptr<AVSessionItem> CreateSessionInner(const std::string& tag, int32_t type, bool thirdPartyApp,
29880922886Sopenharmony_ci                                           const AppExecFwk::ElementName& elementName);
29980922886Sopenharmony_ci
30080922886Sopenharmony_ci    int32_t CreateSessionInner(const std::string& tag, int32_t type, bool thirdPartyApp,
30180922886Sopenharmony_ci                               const AppExecFwk::ElementName& elementName, sptr<AVSessionItem>& sessionItem);
30280922886Sopenharmony_ci
30380922886Sopenharmony_ci    bool IsParamInvalid(const std::string& tag, int32_t type, const AppExecFwk::ElementName& elementName);
30480922886Sopenharmony_ci
30580922886Sopenharmony_ci    void ServiceCallback(sptr<AVSessionItem>& sessionItem);
30680922886Sopenharmony_ci
30780922886Sopenharmony_ci    sptr<AVSessionItem> CreateNewSession(const std::string& tag, int32_t type, bool thirdPartyApp,
30880922886Sopenharmony_ci                                         const AppExecFwk::ElementName& elementName);
30980922886Sopenharmony_ci
31080922886Sopenharmony_ci    sptr<AVControllerItem> CreateNewControllerForSession(pid_t pid, sptr<AVSessionItem>& session);
31180922886Sopenharmony_ci
31280922886Sopenharmony_ci    int32_t CancelCastAudioForClientExit(pid_t pid, const sptr<AVSessionItem>& session);
31380922886Sopenharmony_ci
31480922886Sopenharmony_ci    void ClearSessionForClientDiedNoLock(pid_t pid);
31580922886Sopenharmony_ci
31680922886Sopenharmony_ci    void ClearControllerForClientDiedNoLock(pid_t pid);
31780922886Sopenharmony_ci
31880922886Sopenharmony_ci    void InitKeyEvent();
31980922886Sopenharmony_ci
32080922886Sopenharmony_ci    void InitAudio();
32180922886Sopenharmony_ci
32280922886Sopenharmony_ci    void InitAMS();
32380922886Sopenharmony_ci
32480922886Sopenharmony_ci    void InitDM();
32580922886Sopenharmony_ci
32680922886Sopenharmony_ci    void InitBMS();
32780922886Sopenharmony_ci
32880922886Sopenharmony_ci    void InitRadarBMS();
32980922886Sopenharmony_ci
33080922886Sopenharmony_ci    void InitAccountMgr();
33180922886Sopenharmony_ci
33280922886Sopenharmony_ci    void InitCommonEventService();
33380922886Sopenharmony_ci
33480922886Sopenharmony_ci    bool SelectFocusSession(const FocusSessionStrategy::FocusSessionChangeInfo& info);
33580922886Sopenharmony_ci
33680922886Sopenharmony_ci    void RefreshFocusSessionSort(sptr<AVSessionItem> &session);
33780922886Sopenharmony_ci
33880922886Sopenharmony_ci    void UpdateTopSession(const sptr<AVSessionItem>& newTopSession);
33980922886Sopenharmony_ci
34080922886Sopenharmony_ci    void HandleFocusSession(const FocusSessionStrategy::FocusSessionChangeInfo& info);
34180922886Sopenharmony_ci
34280922886Sopenharmony_ci    void HandleDeviceChange(const AudioStandard::DeviceChangeAction& deviceChangeAction);
34380922886Sopenharmony_ci
34480922886Sopenharmony_ci    __attribute__((no_sanitize("cfi"))) std::shared_ptr<RemoteSessionCommandProcess> GetService(
34580922886Sopenharmony_ci        const std::string& deviceId);
34680922886Sopenharmony_ci
34780922886Sopenharmony_ci    int32_t CastAudioProcess(const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors,
34880922886Sopenharmony_ci                             const std::string& sourceSessionInfo,
34980922886Sopenharmony_ci                             sptr<AVSessionItem>& session);
35080922886Sopenharmony_ci
35180922886Sopenharmony_ci    int32_t CastAudioInner(const std::vector<AudioStandard::AudioDeviceDescriptor>& sinkAudioDescriptors,
35280922886Sopenharmony_ci                           const std::string& sourceSessionInfo,
35380922886Sopenharmony_ci                           const sptr<AVSessionItem>& session);
35480922886Sopenharmony_ci
35580922886Sopenharmony_ci    int32_t CancelCastAudioInner(const std::vector<AudioStandard::AudioDeviceDescriptor>& sinkDevices,
35680922886Sopenharmony_ci                                 const std::string& sourceSessionInfo,
35780922886Sopenharmony_ci                                 const sptr<AVSessionItem>& session);
35880922886Sopenharmony_ci
35980922886Sopenharmony_ci    int32_t RemoteCastAudioInner(const std::string& sourceSessionInfo, std::string& sinkSessionInfo);
36080922886Sopenharmony_ci
36180922886Sopenharmony_ci    int32_t RemoteCancelCastAudioInner(const std::string& sessionInfo);
36280922886Sopenharmony_ci
36380922886Sopenharmony_ci    int32_t ProcessCastAudioCommand(const RemoteServiceCommand command, const std::string& input,
36480922886Sopenharmony_ci                                    std::string& output) override;
36580922886Sopenharmony_ci
36680922886Sopenharmony_ci    int32_t CastAudioForNewSession(const sptr<AVSessionItem>& session);
36780922886Sopenharmony_ci
36880922886Sopenharmony_ci    bool IsLocalDevice(const std::string& networkId);
36980922886Sopenharmony_ci
37080922886Sopenharmony_ci    int32_t GetLocalNetworkId(std::string& networkId);
37180922886Sopenharmony_ci
37280922886Sopenharmony_ci    int32_t GetTrustedDeviceName(const std::string& networkId, std::string& deviceName);
37380922886Sopenharmony_ci
37480922886Sopenharmony_ci    int32_t GetTrustedDevicesInfo(std::vector<OHOS::DistributedHardware::DmDeviceInfo>& deviceList);
37580922886Sopenharmony_ci
37680922886Sopenharmony_ci    int32_t SetBasicInfo(std::string& sessionInfo);
37780922886Sopenharmony_ci
37880922886Sopenharmony_ci    void SetDeviceInfo(const std::vector<AudioStandard::AudioDeviceDescriptor>& castAudioDescriptors,
37980922886Sopenharmony_ci                           sptr<AVSessionItem>& session);
38080922886Sopenharmony_ci
38180922886Sopenharmony_ci    int32_t GetAudioDescriptor(const std::string deviceId,
38280922886Sopenharmony_ci                               std::vector<AudioStandard::AudioDeviceDescriptor>& audioDeviceDescriptors);
38380922886Sopenharmony_ci
38480922886Sopenharmony_ci    bool GetAudioDescriptorByDeviceId(const std::vector<sptr<AudioStandard::AudioDeviceDescriptor>>& descriptors,
38580922886Sopenharmony_ci                                      const std::string& deviceId,
38680922886Sopenharmony_ci                                      AudioStandard::AudioDeviceDescriptor& audioDescriptor);
38780922886Sopenharmony_ci
38880922886Sopenharmony_ci    void GetDeviceInfo(const sptr<AVSessionItem>& session,
38980922886Sopenharmony_ci                           const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors,
39080922886Sopenharmony_ci                           std::vector<AudioStandard::AudioDeviceDescriptor>& castSinkDescriptors,
39180922886Sopenharmony_ci                           std::vector<AudioStandard::AudioDeviceDescriptor>& cancelSinkDescriptors);
39280922886Sopenharmony_ci
39380922886Sopenharmony_ci    int32_t SelectOutputDevice(const int32_t uid, const AudioStandard::AudioDeviceDescriptor& descriptor);
39480922886Sopenharmony_ci
39580922886Sopenharmony_ci    int32_t StartDefaultAbilityByCall(std::string& sessionId);
39680922886Sopenharmony_ci
39780922886Sopenharmony_ci    int32_t StartAbilityByCall(const std::string& sessionIdNeeded, std::string& sessionId);
39880922886Sopenharmony_ci
39980922886Sopenharmony_ci    void HandleEventHandlerCallBack();
40080922886Sopenharmony_ci
40180922886Sopenharmony_ci    bool IsHistoricalSession(const std::string& sessionId);
40280922886Sopenharmony_ci
40380922886Sopenharmony_ci    void DeleteHistoricalRecord(const std::string& bundleName, int32_t userId = 0);
40480922886Sopenharmony_ci
40580922886Sopenharmony_ci    void DeleteAVQueueInfoRecord(const std::string& bundleName, int32_t userId = 0);
40680922886Sopenharmony_ci
40780922886Sopenharmony_ci    const nlohmann::json& GetSubNode(const nlohmann::json& node, const std::string& name);
40880922886Sopenharmony_ci
40980922886Sopenharmony_ci    void SaveSessionInfoInFile(const std::string& sessionId, const std::string& sessionType,
41080922886Sopenharmony_ci        const AppExecFwk::ElementName& elementName);
41180922886Sopenharmony_ci
41280922886Sopenharmony_ci    bool CheckAndCreateDir(const std::string& filePath);
41380922886Sopenharmony_ci
41480922886Sopenharmony_ci    bool CheckUserDirValid(int32_t userId = 0);
41580922886Sopenharmony_ci
41680922886Sopenharmony_ci    bool LoadStringFromFileEx(const std::string& filePath, std::string& content);
41780922886Sopenharmony_ci
41880922886Sopenharmony_ci    bool SaveStringToFileEx(const std::string& filePath, const std::string& content);
41980922886Sopenharmony_ci
42080922886Sopenharmony_ci    bool CheckStringAndCleanFile(const std::string& filePath);
42180922886Sopenharmony_ci
42280922886Sopenharmony_ci    void ClearClientResources(pid_t pid);
42380922886Sopenharmony_ci
42480922886Sopenharmony_ci    bool SaveAvQueueInfo(std::string& oldContent, const std::string &bundleName,
42580922886Sopenharmony_ci        const AVMetaData& meta, const int32_t userId);
42680922886Sopenharmony_ci
42780922886Sopenharmony_ci    int32_t GetHistoricalSessionDescriptorsFromFile(std::vector<AVSessionDescriptor>& descriptors);
42880922886Sopenharmony_ci
42980922886Sopenharmony_ci    void ReportStartCastBegin(std::string func, const OutputDeviceInfo& outputDeviceInfo, int32_t uid);
43080922886Sopenharmony_ci
43180922886Sopenharmony_ci    void ReportStartCastEnd(std::string func, const OutputDeviceInfo &outputDeviceInfo, int32_t uid, int ret);
43280922886Sopenharmony_ci
43380922886Sopenharmony_ci    void HandleAppStateChange(int uid, int state);
43480922886Sopenharmony_ci
43580922886Sopenharmony_ci    void UpdateFrontSession(sptr<AVSessionItem>& sessionItem, bool isAdd);
43680922886Sopenharmony_ci
43780922886Sopenharmony_ci    std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> CreateWantAgent(
43880922886Sopenharmony_ci        const AVSessionDescriptor* histroyDescriptor);
43980922886Sopenharmony_ci
44080922886Sopenharmony_ci    void DoMetadataImgClean(AVMetaData& data);
44180922886Sopenharmony_ci
44280922886Sopenharmony_ci    void HandleSystemKeyColdStart(const AVControlCommand &command);
44380922886Sopenharmony_ci
44480922886Sopenharmony_ci    bool SubscribeCommonEvent();
44580922886Sopenharmony_ci
44680922886Sopenharmony_ci    bool UnSubscribeCommonEvent();
44780922886Sopenharmony_ci
44880922886Sopenharmony_ci    void ReportSessionInfo(const sptr <AVSessionItem>& session, int res);
44980922886Sopenharmony_ci
45080922886Sopenharmony_ci    bool CheckAncoAudio();
45180922886Sopenharmony_ci
45280922886Sopenharmony_ci    int32_t ConvertKeyCodeToCommand(int keyCode);
45380922886Sopenharmony_ci
45480922886Sopenharmony_ci    void RemoveExpired(std::list<std::chrono::system_clock::time_point> &list,
45580922886Sopenharmony_ci        const std::chrono::system_clock::time_point &now, int32_t time = 1);
45680922886Sopenharmony_ci
45780922886Sopenharmony_ci    std::shared_ptr<std::list<sptr<AVSessionItem>>> GetCurSessionListForFront();
45880922886Sopenharmony_ci
45980922886Sopenharmony_ci    std::atomic<uint32_t> sessionSeqNum_ {};
46080922886Sopenharmony_ci
46180922886Sopenharmony_ci    sptr<AVSessionItem> topSession_;
46280922886Sopenharmony_ci    std::map<pid_t, std::list<sptr<AVControllerItem>>> controllers_;
46380922886Sopenharmony_ci    std::map<pid_t, sptr<IClientDeath>> clientDeathObservers_;
46480922886Sopenharmony_ci    std::map<pid_t, sptr<ClientDeathRecipient>> clientDeathRecipients_;
46580922886Sopenharmony_ci    std::list<SessionListener*> innerSessionListeners_;
46680922886Sopenharmony_ci    std::map<std::string, std::shared_ptr<AbilityManagerAdapter>> abilityManager_;
46780922886Sopenharmony_ci    FocusSessionStrategy focusSessionStrategy_;
46880922886Sopenharmony_ci    BackgroundAudioController backgroundAudioController_;
46980922886Sopenharmony_ci    std::map<std::string, std::string> castAudioSessionMap_;
47080922886Sopenharmony_ci    bool isAllSessionCast_ {};
47180922886Sopenharmony_ci    std::string outputDeviceId_;
47280922886Sopenharmony_ci    std::unique_ptr<AVSessionDumper> dumpHelper_ {};
47380922886Sopenharmony_ci    friend class AVSessionDumper;
47480922886Sopenharmony_ci    std::shared_ptr<MigrateAVSessionServer> migrateAVSession_;
47580922886Sopenharmony_ci    std::shared_ptr<EventSubscriber> subscriber_;
47680922886Sopenharmony_ci    bool isScreenOn_ = false;
47780922886Sopenharmony_ci    bool isScreenLocked_ = true;
47880922886Sopenharmony_ci    std::list<std::chrono::system_clock::time_point> flowControlPublishTimestampList_;
47980922886Sopenharmony_ci
48080922886Sopenharmony_ci    // The following locks are used in the defined order of priority
48180922886Sopenharmony_ci    std::recursive_mutex sessionServiceLock_;
48280922886Sopenharmony_ci
48380922886Sopenharmony_ci    std::recursive_mutex sessionFrontLock_;
48480922886Sopenharmony_ci
48580922886Sopenharmony_ci    std::recursive_mutex sessionListenersLock_;
48680922886Sopenharmony_ci
48780922886Sopenharmony_ci    std::recursive_mutex migrateListenersLock_;
48880922886Sopenharmony_ci
48980922886Sopenharmony_ci    std::recursive_mutex sessionFileLock_;
49080922886Sopenharmony_ci
49180922886Sopenharmony_ci    std::recursive_mutex avQueueFileLock_;
49280922886Sopenharmony_ci
49380922886Sopenharmony_ci    std::recursive_mutex abilityManagerLock_;
49480922886Sopenharmony_ci
49580922886Sopenharmony_ci    std::recursive_mutex screenStateLock_;
49680922886Sopenharmony_ci
49780922886Sopenharmony_ci    std::recursive_mutex clientDeathLock_;
49880922886Sopenharmony_ci
49980922886Sopenharmony_ci    // DMSDP related locks
50080922886Sopenharmony_ci    std::recursive_mutex isAllSessionCastLock_;
50180922886Sopenharmony_ci
50280922886Sopenharmony_ci    std::recursive_mutex outputDeviceIdLock_;
50380922886Sopenharmony_ci
50480922886Sopenharmony_ci    std::recursive_mutex castAudioSessionMapLock_;
50580922886Sopenharmony_ci
50680922886Sopenharmony_ci#ifdef BLUETOOTH_ENABLE
50780922886Sopenharmony_ci    OHOS::Bluetooth::BluetoothHost *bluetoothHost_ = nullptr;
50880922886Sopenharmony_ci    std::shared_ptr<DetectBluetoothHostObserver> bluetoothObserver;
50980922886Sopenharmony_ci#endif
51080922886Sopenharmony_ci
51180922886Sopenharmony_ci#ifdef CASTPLUS_CAST_ENGINE_ENABLE
51280922886Sopenharmony_ci    std::map<std::string, std::string> castServiceNameMapState_;
51380922886Sopenharmony_ci    const std::string deviceStateConnection = "CONNECT_SUCC";
51480922886Sopenharmony_ci    const std::string seperator = ",";
51580922886Sopenharmony_ci    int appState = -1;
51680922886Sopenharmony_ci    bool isSupportMirrorToStream_ = false;
51780922886Sopenharmony_ci    std::string castDeviceId_ = "0";
51880922886Sopenharmony_ci    std::string castDeviceName_ = " ";
51980922886Sopenharmony_ci    int32_t castDeviceType_ = 0;
52080922886Sopenharmony_ci    const int32_t beginAddPos = 3;
52180922886Sopenharmony_ci    const int32_t endDecPos = 4;
52280922886Sopenharmony_ci    const int32_t typeAddPos = 2;
52380922886Sopenharmony_ci#endif
52480922886Sopenharmony_ci
52580922886Sopenharmony_ci    static constexpr const char *SORT_FILE_NAME = "sortinfo";
52680922886Sopenharmony_ci    static constexpr const char *DEFAULT_SESSION_ID = "default";
52780922886Sopenharmony_ci    static constexpr const char *DEFAULT_BUNDLE_NAME = "com.example.himusicdemo";
52880922886Sopenharmony_ci    static constexpr const char *DEFAULT_ABILITY_NAME = "MainAbility";
52980922886Sopenharmony_ci    static constexpr const int32_t SYSTEMUI_LIVEVIEW_TYPECODE_MDEDIACONTROLLER = 2;
53080922886Sopenharmony_ci    static constexpr const char *AVQUEUE_FILE_NAME = "avqueueinfo";
53180922886Sopenharmony_ci
53280922886Sopenharmony_ci    const std::string MEDIA_CONTROL_BUNDLENAME = "com.ohos.mediacontroller";
53380922886Sopenharmony_ci    const std::string MEDIA_CONTROL_ABILITYNAME = "com.ohos.mediacontroller.avplayer.mainability";
53480922886Sopenharmony_ci
53580922886Sopenharmony_ci    int32_t pressCount_ {};
53680922886Sopenharmony_ci    int32_t maxHistoryNums_ = 10;
53780922886Sopenharmony_ci    int uidForAppStateChange_ = 0;
53880922886Sopenharmony_ci    bool isFirstPress_ = true;
53980922886Sopenharmony_ci    bool isSourceInCast_ = false;
54080922886Sopenharmony_ci    bool isInCast_ = false;
54180922886Sopenharmony_ci    bool is2in1_ = false;
54280922886Sopenharmony_ci
54380922886Sopenharmony_ci    void *migrateStubFuncHandle_ = nullptr;
54480922886Sopenharmony_ci
54580922886Sopenharmony_ci    const int32_t ONE_CLICK = 1;
54680922886Sopenharmony_ci    const int32_t DOUBLE_CLICK = 2;
54780922886Sopenharmony_ci    const int32_t THREE_CLICK = 3;
54880922886Sopenharmony_ci    const int32_t unSetHistoryNum = 3;
54980922886Sopenharmony_ci    const int32_t CLICK_TIMEOUT = 500;
55080922886Sopenharmony_ci    const int32_t defMaxHistoryNum = 10;
55180922886Sopenharmony_ci    const int32_t maxFileLength = 32 * 1024 * 1024;
55280922886Sopenharmony_ci    const int32_t maxAVQueueInfoLen = 99;
55380922886Sopenharmony_ci    const int32_t allocSpace = 2;
55480922886Sopenharmony_ci    const int32_t avSessionUid = 6700;
55580922886Sopenharmony_ci    const int32_t ancoUid = 1041;
55680922886Sopenharmony_ci    const int32_t saType = 1;
55780922886Sopenharmony_ci    const int32_t MAX_NOTIFICATION_NUM = 3;
55880922886Sopenharmony_ci    const int32_t NOTIFICATION_CONTROL_TIME = 1000;
55980922886Sopenharmony_ci};
56080922886Sopenharmony_ci} // namespace OHOS::AVSession
56180922886Sopenharmony_ci#endif // OHOS_AVSESSION_SERVICE_H
562