• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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_MANAGER_H
16 #define ST_AUDIO_DEVICE_MANAGER_H
17 
18 #include <list>
19 #include <string>
20 #include <memory>
21 #include <vector>
22 #include <unordered_map>
23 #include "audio_device_info.h"
24 #include "audio_system_manager.h"
25 
26 namespace OHOS {
27 namespace AudioStandard {
28 using namespace std;
29 
30 constexpr int32_t NEED_TO_FETCH = 1;
31 
32 typedef function<bool(const std::shared_ptr<AudioDeviceDescriptor> &desc)> IsPresentFunc;
33 class AudioDeviceManager {
34 public:
GetAudioDeviceManager()35     static AudioDeviceManager& GetAudioDeviceManager()
36     {
37         static AudioDeviceManager audioDeviceManager;
38         return audioDeviceManager;
39     }
40 
41     void AddNewDevice(const std::shared_ptr<AudioDeviceDescriptor> &devDesc);
42     void RemoveNewDevice(const std::shared_ptr<AudioDeviceDescriptor> &devDesc);
43     void OnXmlParsingCompleted(const unordered_map<AudioDevicePrivacyType, list<DevicePrivacyInfo>> &xmlData);
44     int32_t GetDeviceUsageFromType(const DeviceType devType) const;
45     void ParseDeviceXml();
46     AudioStreamDeviceChangeReasonExt UpdateDevicesListInfo(
47         const std::shared_ptr<AudioDeviceDescriptor> &deviceDescriptor, const DeviceInfoUpdateCommand updateCommand);
48 
49     vector<shared_ptr<AudioDeviceDescriptor>> GetRemoteRenderDevices();
50     vector<shared_ptr<AudioDeviceDescriptor>> GetRemoteCaptureDevices();
51     vector<shared_ptr<AudioDeviceDescriptor>> GetCommRenderPrivacyDevices();
52     vector<shared_ptr<AudioDeviceDescriptor>> GetCommRenderPublicDevices();
53     vector<shared_ptr<AudioDeviceDescriptor>> GetCommRenderBTCarDevices();
54     vector<shared_ptr<AudioDeviceDescriptor>> GetCommCapturePrivacyDevices();
55     vector<shared_ptr<AudioDeviceDescriptor>> GetCommCapturePublicDevices();
56     vector<shared_ptr<AudioDeviceDescriptor>> GetMediaRenderPrivacyDevices();
57     vector<shared_ptr<AudioDeviceDescriptor>> GetMediaRenderPublicDevices();
58     vector<shared_ptr<AudioDeviceDescriptor>> GetMediaCapturePrivacyDevices();
59     vector<shared_ptr<AudioDeviceDescriptor>> GetMediaCapturePublicDevices();
60     vector<shared_ptr<AudioDeviceDescriptor>> GetCapturePrivacyDevices();
61     vector<shared_ptr<AudioDeviceDescriptor>> GetCapturePublicDevices();
62     vector<shared_ptr<AudioDeviceDescriptor>> GetRecongnitionCapturePrivacyDevices();
63     shared_ptr<AudioDeviceDescriptor> GetCommRenderDefaultDevice(StreamUsage streamUsage);
64     shared_ptr<AudioDeviceDescriptor> GetRenderDefaultDevice();
65     shared_ptr<AudioDeviceDescriptor> GetCaptureDefaultDevice();
66     shared_ptr<AudioDeviceDescriptor> FindConnectedDeviceById(const int32_t deviceId);
67     unordered_map<AudioDevicePrivacyType, list<DevicePrivacyInfo>> GetDevicePrivacyMaps();
68     vector<shared_ptr<AudioDeviceDescriptor>> GetAvailableDevicesByUsage(AudioDeviceUsage usage);
69     void GetAvailableDevicesWithUsage(const AudioDeviceUsage usage,
70         const list<DevicePrivacyInfo> &deviceInfos, const std::shared_ptr<AudioDeviceDescriptor> &dev,
71         std::vector<shared_ptr<AudioDeviceDescriptor>> &audioDeviceDescriptors);
72     vector<shared_ptr<AudioDeviceDescriptor>> GetAvailableBluetoothDevice(DeviceType devType,
73         const std::string &macAddress);
74     bool GetScoState();
75     void UpdateEarpieceStatus(const bool hasEarPiece);
76     vector<shared_ptr<AudioDeviceDescriptor>> GetDevicesByFilter(DeviceType devType, DeviceRole devRole,
77         const string &macAddress, const string &networkId, ConnectState connectState);
78     DeviceUsage GetDeviceUsage(const AudioDeviceDescriptor &desc);
79     std::string GetConnDevicesStr();
80     bool IsArmUsbDevice(const AudioDeviceDescriptor &desc);
81     void OnReceiveBluetoothEvent(const std::string macAddress, const std::string deviceName);
82     bool IsDeviceConnected(std::shared_ptr<AudioDeviceDescriptor> &audioDeviceDescriptors);
83     bool IsConnectedDevices(const std::shared_ptr<AudioDeviceDescriptor> &devDesc);
84     bool IsVirtualConnectedDevice(const std::shared_ptr<AudioDeviceDescriptor> &selectedDesc);
85     int32_t UpdateDeviceDescDeviceId(std::shared_ptr<AudioDeviceDescriptor> &deviceDescriptor);
86     int32_t SetDefaultOutputDevice(const DeviceType deviceType, const uint32_t sessionID,
87         const StreamUsage streamUsage, bool isRunning);
88     int32_t UpdateDefaultOutputDeviceWhenStarting(const uint32_t sessionID);
89     int32_t UpdateDefaultOutputDeviceWhenStopping(const uint32_t sessionID);
90     int32_t RemoveSelectedDefaultOutputDevice(const uint32_t sessionID);
91     shared_ptr<AudioDeviceDescriptor> GetSelectedMediaRenderDevice();
92     shared_ptr<AudioDeviceDescriptor> GetSelectedCallRenderDevice();
93     int32_t SetDeviceVolumeBehavior(const std::string &networkId, DeviceType deviceType, VolumeBehavior volumeBehavior);
94     VolumeBehavior GetDeviceVolumeBehavior(const std::string &networkId, DeviceType deviceType);
95     int32_t SetInputDevice(const DeviceType deviceType, const uint32_t sessionID,
96         const SourceType sourceType, bool isRunning);
97     int32_t RemoveSelectedInputDevice(const uint32_t sessionID);
98     shared_ptr<AudioDeviceDescriptor> GetSelectedCaptureDevice(const uint32_t sessionID);
99     void Dump(std::string &dumpString);
100     void UpdateVirtualDevices(const std::shared_ptr<AudioDeviceDescriptor> &devDesc, bool isConnected);
101     void GetAllConnectedDeviceByType(std::string networkId, DeviceType deviceType,
102         std::string macAddress, DeviceRole deviceRole, std::vector<std::shared_ptr<AudioDeviceDescriptor>> &descForCb);
103     bool IsSessionSetDefaultDevice(uint32_t sessionId);
104     bool ExistsByType(DeviceType devType) const;
105     bool ExistsByTypeAndAddress(DeviceType devType, const string &address) const;
106     bool ExistSameRemoteDeviceByMacAddress(std::shared_ptr<AudioDeviceDescriptor> desc);
107     shared_ptr<AudioDeviceDescriptor> GetActiveScoDevice(std::string scoMac, DeviceRole role);
108 
109 private:
110     AudioDeviceManager();
~AudioDeviceManager()111     ~AudioDeviceManager() {};
112     bool DeviceAttrMatch(const shared_ptr<AudioDeviceDescriptor> &devDesc, AudioDevicePrivacyType &privacyType,
113         DeviceRole &devRole, DeviceUsage &devUsage);
114 
115     void FillArrayWhenDeviceAttrMatch(const shared_ptr<AudioDeviceDescriptor> &devDesc,
116         AudioDevicePrivacyType privacyType, DeviceRole devRole, DeviceUsage devUsage, string logName,
117         vector<shared_ptr<AudioDeviceDescriptor>> &descArray);
118 
119     void RemoveMatchDeviceInArray(const AudioDeviceDescriptor &devDesc, string logName,
120         vector<shared_ptr<AudioDeviceDescriptor>> &descArray);
121 
122     void MakePairedDeviceDescriptor(const shared_ptr<AudioDeviceDescriptor> &devDesc);
123     void MakePairedDefaultDeviceDescriptor(const shared_ptr<AudioDeviceDescriptor> &devDesc, DeviceRole devRole);
124     void MakePairedDefaultDeviceImpl(const shared_ptr<AudioDeviceDescriptor> &devDesc,
125         const shared_ptr<AudioDeviceDescriptor> &connectedDesc);
126     void UpdateConnectedDevices(const shared_ptr<AudioDeviceDescriptor> &devDesc, bool isConnected);
127     void AddConnectedDevices(const shared_ptr<AudioDeviceDescriptor> &devDesc);
128     void RemoveConnectedDevices(const shared_ptr<AudioDeviceDescriptor> &devDesc);
129     void AddRemoteRenderDev(const shared_ptr<AudioDeviceDescriptor> &devDesc);
130     void AddRemoteCaptureDev(const shared_ptr<AudioDeviceDescriptor> &devDesc);
131     void AddDefaultDevices(const std::shared_ptr<AudioDeviceDescriptor> &devDesc);
132 
133     bool IsVirtualDevicesExist(const shared_ptr<AudioDeviceDescriptor> &devDesc);
134 
135     void UpdateDeviceInfo(shared_ptr<AudioDeviceDescriptor> &deviceDesc);
136     void AddCommunicationDevices(const shared_ptr<AudioDeviceDescriptor> &devDesc);
137     void AddMediaDevices(const shared_ptr<AudioDeviceDescriptor> &devDesc);
138     void AddCaptureDevices(const shared_ptr<AudioDeviceDescriptor> &devDesc);
139     void HandleScoWithDefaultCategory(const shared_ptr<AudioDeviceDescriptor> &devDesc);
140     bool IsExistedDevice(const std::shared_ptr<AudioDeviceDescriptor> &device,
141         const vector<shared_ptr<AudioDeviceDescriptor>> &audioDeviceDescriptors);
142     void AddAvailableDevicesByUsage(const AudioDeviceUsage usage,
143         const DevicePrivacyInfo &deviceInfo, const std::shared_ptr<AudioDeviceDescriptor> &dev,
144         std::vector<shared_ptr<AudioDeviceDescriptor>> &audioDeviceDescriptors);
145     void GetDefaultAvailableDevicesByUsage(AudioDeviceUsage usage,
146         vector<shared_ptr<AudioDeviceDescriptor>> &audioDeviceDescriptors);
147     void GetRemoteAvailableDevicesByUsage(AudioDeviceUsage usage,
148         std::vector<std::shared_ptr<AudioDeviceDescriptor>> &audioDeviceDescriptors);
149     void ReorderAudioDevices(std::vector<std::shared_ptr<AudioDeviceDescriptor>> &audioDeviceDescriptors,
150         const std::string &remoteInfoNetworkId, DeviceType remoteInfoDeviceType);
151     bool UpdateExistDeviceDescriptor(const std::shared_ptr<AudioDeviceDescriptor> &deviceDescriptor);
152 
153     void AddBtToOtherList(const shared_ptr<AudioDeviceDescriptor> &devDesc);
154     void RemoveBtFromOtherList(const AudioDeviceDescriptor &devDesc);
155     void RemoveRemoteDevices(const AudioDeviceDescriptor &devDesc);
156     void RemoveCommunicationDevices(const AudioDeviceDescriptor &devDesc);
157     void RemoveMediaDevices(const AudioDeviceDescriptor &devDesc);
158     void RemoveCaptureDevices(const AudioDeviceDescriptor &devDesc);
159     bool UpdateConnectState(const shared_ptr<AudioDeviceDescriptor> &devDesc);
160     bool UpdateDeviceCategory(const std::shared_ptr<AudioDeviceDescriptor> &deviceDescriptor);
161     bool UpdateEnableState(const shared_ptr<AudioDeviceDescriptor> &deviceDescriptor);
162     bool UpdateExceptionFlag(const shared_ptr<AudioDeviceDescriptor> &deviceDescriptor);
163     AudioStreamDeviceChangeReasonExt UpdateDeviceUsage(const shared_ptr<AudioDeviceDescriptor> &deviceDesc);
164 
165     void RemoveVirtualConnectedDevice(const shared_ptr<AudioDeviceDescriptor> &devDesc);
166 
167     list<DevicePrivacyInfo> privacyDeviceList_;
168     list<DevicePrivacyInfo> publicDeviceList_;
169 
170     vector<shared_ptr<AudioDeviceDescriptor>> remoteRenderDevices_;
171     vector<shared_ptr<AudioDeviceDescriptor>> remoteCaptureDevices_;
172     vector<shared_ptr<AudioDeviceDescriptor>> commRenderPrivacyDevices_;
173     vector<shared_ptr<AudioDeviceDescriptor>> commRenderPublicDevices_;
174     vector<shared_ptr<AudioDeviceDescriptor>> commCapturePrivacyDevices_;
175     vector<shared_ptr<AudioDeviceDescriptor>> commCapturePublicDevices_;
176     vector<shared_ptr<AudioDeviceDescriptor>> mediaRenderPrivacyDevices_;
177     vector<shared_ptr<AudioDeviceDescriptor>> mediaRenderPublicDevices_;
178     vector<shared_ptr<AudioDeviceDescriptor>> mediaCapturePrivacyDevices_;
179     vector<shared_ptr<AudioDeviceDescriptor>> mediaCapturePublicDevices_;
180     vector<shared_ptr<AudioDeviceDescriptor>> capturePrivacyDevices_;
181     vector<shared_ptr<AudioDeviceDescriptor>> capturePublicDevices_;
182     vector<shared_ptr<AudioDeviceDescriptor>> connectedDevices_;
183     vector<shared_ptr<AudioDeviceDescriptor>> reconCapturePrivacyDevices_;
184     vector<shared_ptr<AudioDeviceDescriptor>> virtualDevices_;
185     unordered_map<AudioDevicePrivacyType, list<DevicePrivacyInfo>> devicePrivacyMaps_ = {};
186     std::shared_ptr<AudioDeviceDescriptor> earpiece_ = nullptr;
187     std::shared_ptr<AudioDeviceDescriptor> speaker_ = nullptr;
188     std::shared_ptr<AudioDeviceDescriptor> defalutMic_ = nullptr;
189     bool hasEarpiece_ = false;
190     unordered_map<uint32_t, std::pair<DeviceType, StreamUsage>> selectedDefaultOutputDeviceInfo_;
191     vector<std::pair<uint32_t, DeviceType>> mediaDefaultOutputDevices_;
192     vector<std::pair<uint32_t, DeviceType>> callDefaultOutputDevices_;
193     DeviceType selectedMediaDefaultOutputDevice_ = DEVICE_TYPE_DEFAULT;
194     DeviceType selectedCallDefaultOutputDevice_ = DEVICE_TYPE_DEFAULT;
195     std::mutex selectDefaultOutputDeviceMutex_;
196     std::mutex currentActiveDevicesMutex_;
197     unordered_map<uint32_t, std::pair<DeviceType, SourceType>> selectedInputDeviceInfo_;
198     std::mutex selectInputDeviceMutex_;
199     std::string remoteInfoNetworkId_ = "";
200     DeviceType remoteInfoDeviceType_ = DEVICE_TYPE_DEFAULT;
201     std::mutex virtualDevicesMutex_;
202     std::mutex descArrayMutex_;
203 };
204 } // namespace AudioStandard
205 } // namespace OHOS
206 #endif //ST_AUDIO_DEVICE_MANAGER_H
207