• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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 #ifndef ST_AUDIO_DEVICE_LOCK_H
16 #define ST_AUDIO_DEVICE_LOCK_H
17 
18 #include <bitset>
19 #include <list>
20 #include <string>
21 #include <unordered_map>
22 #include <unordered_set>
23 #include <mutex>
24 #include "singleton.h"
25 #include "audio_group_handle.h"
26 #include "audio_manager_base.h"
27 #include "audio_module_info.h"
28 #include "audio_volume_config.h"
29 #include "audio_errors.h"
30 #include "microphone_descriptor.h"
31 #include "audio_system_manager.h"
32 
33 #include "audio_policy_manager_factory.h"
34 #include "audio_device_manager.h"
35 #include "audio_stream_collector.h"
36 
37 #include "audio_active_device.h"
38 #include "audio_scene_manager.h"
39 #include "audio_volume_manager.h"
40 #include "audio_connected_device.h"
41 #include "audio_microphone_descriptor.h"
42 #include "audio_offload_stream.h"
43 #include "audio_device_common.h"
44 #include "audio_capturer_session.h"
45 #include "audio_device_status.h"
46 #include "audio_recovery_device.h"
47 #include "audio_a2dp_offload_flag.h"
48 #include "audio_a2dp_offload_manager.h"
49 
50 namespace OHOS {
51 namespace AudioStandard {
52 
53 class AudioDeviceLock {
54 public:
GetInstance()55     static AudioDeviceLock& GetInstance()
56     {
57         static AudioDeviceLock instance;
58         return instance;
59     }
60     void Init(std::shared_ptr<AudioA2dpOffloadManager> audioA2dpOffloadManager);
61     int32_t SetAudioScene(AudioScene audioScene, const int32_t uid = INVALID_UID, const int32_t pid = INVALID_PID);
62     void DeInit();
63     bool IsArmUsbDevice(const AudioDeviceDescriptor &desc);
64     std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetDevices(DeviceFlag deviceFlag);
65     int32_t SetDeviceActive(DeviceType deviceType, bool active, const int32_t uid = INVALID_UID);
66     std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetPreferredOutputDeviceDescriptors(
67         AudioRendererInfo &rendererInfo, std::string networkId = LOCAL_NETWORK_ID);
68     std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetPreferredInputDeviceDescriptors(
69         AudioCapturerInfo &captureInfo, std::string networkId = LOCAL_NETWORK_ID);
70     std::shared_ptr<AudioDeviceDescriptor> GetActiveBluetoothDevice();
71     int32_t SetCallDeviceActive(DeviceType deviceType, bool active, std::string address,
72         const int32_t uid = INVALID_UID);
73     std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetAvailableDevices(AudioDeviceUsage usage);
74 
75     void FetchOutputDeviceForTrack(AudioStreamChangeInfo &streamChangeInfo,
76         const AudioStreamDeviceChangeReasonExt reason);
77 
78     void FetchInputDeviceForTrack(AudioStreamChangeInfo &streamChangeInfo);
79     int32_t RegisterTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo,
80         const sptr<IRemoteObject> &object, const int32_t apiVersion);
81     int32_t UpdateTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo);
82     void RegisteredTrackerClientDied(pid_t uid);
83     int32_t GetCurrentRendererChangeInfos(vector<shared_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos,
84         bool hasBTPermission, bool hasSystemPermission);
85     std::vector<sptr<MicrophoneDescriptor>> GetAvailableMicrophones();
86     std::vector<sptr<MicrophoneDescriptor>> GetAudioCapturerMicrophoneDescriptors(int32_t sessionId);
87     void OnReceiveBluetoothEvent(const std::string macAddress, const std::string deviceName);
88     void UpdateSessionConnectionState(const int32_t &sessionID, const int32_t &state);
89     int32_t SelectOutputDevice(sptr<AudioRendererFilter> audioRendererFilter,
90         std::vector<std::shared_ptr<AudioDeviceDescriptor>> audioDeviceDescriptors);
91     int32_t SelectInputDevice(sptr<AudioCapturerFilter> audioCapturerFilter,
92         std::vector<std::shared_ptr<AudioDeviceDescriptor>> audioDeviceDescriptors);
93     int32_t ExcludeOutputDevices(AudioDeviceUsage audioDevUsage,
94         std::vector<std::shared_ptr<AudioDeviceDescriptor>> &audioDeviceDescriptors);
95     int32_t UnexcludeOutputDevices(AudioDeviceUsage audioDevUsage,
96         std::vector<std::shared_ptr<AudioDeviceDescriptor>> &audioDeviceDescriptors);
97     std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetExcludedDevices(
98         AudioDeviceUsage audioDevUsage);
99     void NotifyRemoteRenderState(std::string networkId, std::string condition, std::string value);
100     int32_t OnCapturerSessionAdded(uint64_t sessionID, SessionInfo sessionInfo, AudioStreamInfo streamInfo);
101     void OnCapturerSessionRemoved(uint64_t sessionID);
102     int32_t TriggerFetchDevice(AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN);
103     void SetDisplayName(const std::string &deviceName, bool isLocalDevice);
104     void SetDmDeviceType(const uint16_t dmDeviceType);
105     void UpdateSpatializationSupported(const std::string macAddress, const bool support);
106     std::vector<sptr<VolumeGroupInfo>> GetVolumeGroupInfos();
107     /*****IDeviceStatusObserver*****/
108     void OnMicrophoneBlockedUpdate(DeviceType devType, DeviceBlockStatus status);
109     void OnBlockedStatusUpdated(DeviceType devType, DeviceBlockStatus status);
110     void OnDeviceStatusUpdated(DeviceType devType, bool isConnected,
111         const std::string &macAddress, const std::string &deviceName,
112         const AudioStreamInfo &streamInfo, DeviceRole role = DEVICE_ROLE_NONE, bool hasPair = false);
113     void OnDeviceStatusUpdated(AudioDeviceDescriptor &desc, bool isConnected);
114     void OnDeviceStatusUpdated(DStatusInfo statusInfo, bool isStop = false);
115     void OnPnpDeviceStatusUpdated(AudioDeviceDescriptor &desc, bool isConnected);
116     void OnDeviceConfigurationChanged(DeviceType deviceType,
117         const std::string &macAddress, const std::string &deviceName,
118         const AudioStreamInfo &streamInfo);
119     int32_t OnServiceConnected(AudioServiceIndex serviceIndex);
120     void OnServiceDisconnected(AudioServiceIndex serviceIndex);
121     void OnForcedDeviceSelected(DeviceType devType, const std::string &macAddress);
122     void OnDeviceInfoUpdated(AudioDeviceDescriptor &desc, const DeviceInfoUpdateCommand command);
123     void UpdateAppVolume(int32_t appUid, int32_t volume);
124     /*****IDeviceStatusObserver*****/
125 private:
AudioDeviceLock()126     AudioDeviceLock() : audioPolicyManager_(AudioPolicyManagerFactory::GetAudioPolicyManager()),
127         streamCollector_(AudioStreamCollector::GetAudioStreamCollector()),
128         audioStateManager_(AudioStateManager::GetAudioStateManager()),
129         audioDeviceManager_(AudioDeviceManager::GetAudioDeviceManager()),
130         audioActiveDevice_(AudioActiveDevice::GetInstance()),
131         audioSceneManager_(AudioSceneManager::GetInstance()),
132         audioVolumeManager_(AudioVolumeManager::GetInstance()),
133         audioConnectedDevice_(AudioConnectedDevice::GetInstance()),
134         audioMicrophoneDescriptor_(AudioMicrophoneDescriptor::GetInstance()),
135         audioOffloadStream_(AudioOffloadStream::GetInstance()),
136         audioDeviceCommon_(AudioDeviceCommon::GetInstance()),
137         audioCapturerSession_(AudioCapturerSession::GetInstance()),
138         audioDeviceStatus_(AudioDeviceStatus::GetInstance()),
139         audioRecoveryDevice_(AudioRecoveryDevice::GetInstance()),
140         audioA2dpOffloadFlag_(AudioA2dpOffloadFlag::GetInstance()) {}
~AudioDeviceLock()141     ~AudioDeviceLock() {}
142     void UpdateDefaultOutputDeviceWhenStopping(int32_t uid);
143     void SendA2dpConnectedWhileRunning(const RendererState &rendererState, const uint32_t &sessionId);
144     void UpdateTrackerDeviceChange(const vector<std::shared_ptr<AudioDeviceDescriptor>> &desc);
145     void HandleAudioCaptureState(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo);
146 private:
147     IAudioPolicyInterface& audioPolicyManager_;
148     AudioStreamCollector& streamCollector_;
149     AudioStateManager &audioStateManager_;
150     AudioDeviceManager &audioDeviceManager_;
151     AudioActiveDevice& audioActiveDevice_;
152     AudioSceneManager& audioSceneManager_;
153     AudioVolumeManager& audioVolumeManager_;
154     AudioConnectedDevice& audioConnectedDevice_;
155     AudioMicrophoneDescriptor& audioMicrophoneDescriptor_;
156     AudioOffloadStream& audioOffloadStream_;
157     AudioDeviceCommon& audioDeviceCommon_;
158     AudioCapturerSession& audioCapturerSession_;
159     AudioDeviceStatus& audioDeviceStatus_;
160     AudioRecoveryDevice& audioRecoveryDevice_;
161     AudioA2dpOffloadFlag& audioA2dpOffloadFlag_;
162 
163     mutable std::shared_mutex deviceStatusUpdateSharedMutex_;
164     std::shared_ptr<AudioA2dpOffloadManager> audioA2dpOffloadManager_ = nullptr;
165 };
166 
167 }
168 }
169 
170 #endif
171