• 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 DeInit();
61     std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetDevices(DeviceFlag deviceFlag);
62     std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetPreferredOutputDeviceDescriptors(
63         AudioRendererInfo &rendererInfo, std::string networkId = LOCAL_NETWORK_ID);
64     std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetPreferredInputDeviceDescriptors(
65         AudioCapturerInfo &captureInfo, std::string networkId = LOCAL_NETWORK_ID);
66     int32_t UnexcludeOutputDevices(AudioDeviceUsage audioDevUsage,
67         std::vector<std::shared_ptr<AudioDeviceDescriptor>> &audioDeviceDescriptors);
68     std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetExcludedDevices(
69         AudioDeviceUsage audioDevUsage);
70     void UpdateSpatializationSupported(const std::string macAddress, const bool support);
71     /*****IDeviceStatusObserver*****/
72     void OnMicrophoneBlockedUpdate(DeviceType devType, DeviceBlockStatus status);
73     void OnDeviceStatusUpdated(DeviceType devType, bool isConnected,
74         const std::string &macAddress, const std::string &deviceName,
75         const AudioStreamInfo &streamInfo, DeviceRole role = DEVICE_ROLE_NONE, bool hasPair = false);
76     void OnDeviceStatusUpdated(AudioDeviceDescriptor &desc, bool isConnected);
77     void OnDeviceStatusUpdated(DStatusInfo statusInfo, bool isStop = false);
78     void OnPnpDeviceStatusUpdated(AudioDeviceDescriptor &desc, bool isConnected);
79     void OnDeviceConfigurationChanged(DeviceType deviceType,
80         const std::string &macAddress, const std::string &deviceName,
81         const AudioStreamInfo &streamInfo);
82     void OnForcedDeviceSelected(DeviceType devType, const std::string &macAddress);
83     void OnDeviceInfoUpdated(AudioDeviceDescriptor &desc, const DeviceInfoUpdateCommand command);
84     void UpdateAppVolume(int32_t appUid, int32_t volume);
85     /*****IDeviceStatusObserver*****/
86 private:
AudioDeviceLock()87     AudioDeviceLock() : audioPolicyManager_(AudioPolicyManagerFactory::GetAudioPolicyManager()),
88         streamCollector_(AudioStreamCollector::GetAudioStreamCollector()),
89         audioStateManager_(AudioStateManager::GetAudioStateManager()),
90         audioDeviceManager_(AudioDeviceManager::GetAudioDeviceManager()),
91         audioActiveDevice_(AudioActiveDevice::GetInstance()),
92         audioSceneManager_(AudioSceneManager::GetInstance()),
93         audioVolumeManager_(AudioVolumeManager::GetInstance()),
94         audioConnectedDevice_(AudioConnectedDevice::GetInstance()),
95         audioMicrophoneDescriptor_(AudioMicrophoneDescriptor::GetInstance()),
96         audioOffloadStream_(AudioOffloadStream::GetInstance()),
97         audioDeviceCommon_(AudioDeviceCommon::GetInstance()),
98         audioCapturerSession_(AudioCapturerSession::GetInstance()),
99         audioDeviceStatus_(AudioDeviceStatus::GetInstance()),
100         audioRecoveryDevice_(AudioRecoveryDevice::GetInstance()),
101         audioA2dpOffloadFlag_(AudioA2dpOffloadFlag::GetInstance()) {}
~AudioDeviceLock()102     ~AudioDeviceLock() {}
103 private:
104     IAudioPolicyInterface& audioPolicyManager_;
105     AudioStreamCollector& streamCollector_;
106     AudioStateManager &audioStateManager_;
107     AudioDeviceManager &audioDeviceManager_;
108     AudioActiveDevice& audioActiveDevice_;
109     AudioSceneManager& audioSceneManager_;
110     AudioVolumeManager& audioVolumeManager_;
111     AudioConnectedDevice& audioConnectedDevice_;
112     AudioMicrophoneDescriptor& audioMicrophoneDescriptor_;
113     AudioOffloadStream& audioOffloadStream_;
114     AudioDeviceCommon& audioDeviceCommon_;
115     AudioCapturerSession& audioCapturerSession_;
116     AudioDeviceStatus& audioDeviceStatus_;
117     AudioRecoveryDevice& audioRecoveryDevice_;
118     AudioA2dpOffloadFlag& audioA2dpOffloadFlag_;
119 
120     mutable std::shared_mutex deviceStatusUpdateSharedMutex_;
121     std::shared_ptr<AudioA2dpOffloadManager> audioA2dpOffloadManager_ = nullptr;
122 };
123 
124 }
125 }
126 
127 #endif
128