1 /* 2 * Copyright (c) 2021-2022 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 ST_AUDIO_POLICY_SERVICE_H 17 #define ST_AUDIO_POLICY_SERVICE_H 18 19 #include <bitset> 20 #include <list> 21 #include <string> 22 #include <unordered_map> 23 #include <mutex> 24 #include "audio_group_handle.h" 25 #include "audio_info.h" 26 #include "audio_manager_base.h" 27 #include "audio_policy_manager_factory.h" 28 #include "audio_stream_collector.h" 29 #include "audio_tone_parser.h" 30 31 #include "accessibility_config_listener.h" 32 #include "device_status_listener.h" 33 #include "iaudio_policy_interface.h" 34 #include "iport_observer.h" 35 #include "parser_factory.h" 36 37 namespace OHOS { 38 namespace AudioStandard { 39 class AudioPolicyService : public IPortObserver, public IDeviceStatusObserver, 40 public IAudioAccessibilityConfigObserver { 41 public: GetAudioPolicyService()42 static AudioPolicyService& GetAudioPolicyService() 43 { 44 static AudioPolicyService audioPolicyService; 45 return audioPolicyService; 46 } 47 48 bool Init(void); 49 void Deinit(void); 50 void InitKVStore(); 51 bool ConnectServiceAdapter(); 52 53 const sptr<IStandardAudioService> GetAudioPolicyServiceProxy(); 54 55 int32_t SetStreamVolume(AudioStreamType streamType, float volume); 56 57 float GetStreamVolume(AudioStreamType streamType) const; 58 59 int32_t SetLowPowerVolume(int32_t streamId, float volume) const; 60 61 float GetLowPowerVolume(int32_t streamId) const; 62 63 float GetSingleStreamVolume(int32_t streamId) const; 64 65 int32_t SetStreamMute(AudioStreamType streamType, bool mute) const; 66 67 int32_t SetSourceOutputStreamMute(int32_t uid, bool setMute) const; 68 69 bool GetStreamMute(AudioStreamType streamType) const; 70 71 bool IsStreamActive(AudioStreamType streamType) const; 72 73 void NotifyRemoteRenderState(std::string networkId, std::string condition, std::string value); 74 75 int32_t SelectOutputDevice(sptr<AudioRendererFilter> audioRendererFilter, 76 std::vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors); 77 78 std::string GetSelectedDeviceInfo(int32_t uid, int32_t pid, AudioStreamType streamType); 79 80 int32_t SelectInputDevice(sptr<AudioCapturerFilter> audioCapturerFilter, 81 std::vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors); 82 83 std::vector<sptr<AudioDeviceDescriptor>> GetDevices(DeviceFlag deviceFlag); 84 85 int32_t SetDeviceActive(InternalDeviceType deviceType, bool active); 86 87 bool IsDeviceActive(InternalDeviceType deviceType) const; 88 89 DeviceType GetActiveOutputDevice() const; 90 91 DeviceType GetActiveInputDevice() const; 92 93 int32_t SetRingerMode(AudioRingerMode ringMode); 94 95 bool IsAudioInterruptEnabled() const; 96 GetAudioFocusMap()97 auto& GetAudioFocusMap() const 98 { 99 return focusMap_; 100 } 101 102 AudioRingerMode GetRingerMode() const; 103 104 int32_t SetMicrophoneMute(bool isMute); 105 106 bool IsMicrophoneMute(); 107 108 int32_t SetAudioScene(AudioScene audioScene); 109 110 AudioScene GetAudioScene() const; 111 112 int32_t GetAudioLatencyFromXml() const; 113 114 uint32_t GetSinkLatencyFromXml() const; 115 116 // Parser callbacks 117 void OnXmlParsingCompleted(const std::unordered_map<ClassType, std::list<AudioModuleInfo>> &xmldata); 118 119 void OnVolumeGroupParsed(std::unordered_map<std::string, std::string>& volumeGroupData); 120 121 void OnInterruptGroupParsed(std::unordered_map<std::string, std::string>& interruptGroupData); 122 123 void OnAudioInterruptEnable(bool enable); 124 125 void OnUpdateRouteSupport(bool isSupported); 126 127 std::vector<int32_t> GetSupportedTones(); 128 129 std::shared_ptr<ToneInfo> GetToneConfig(int32_t ltonetype); 130 131 void OnDeviceStatusUpdated(DeviceType devType, bool isConnected, 132 const std::string &macAddress, const std::string &deviceName, 133 const AudioStreamInfo &streamInfo); 134 135 void OnDeviceConfigurationChanged(DeviceType deviceType, 136 const std::string &macAddress, const std::string &deviceName, 137 const AudioStreamInfo &streamInfo); 138 139 void OnDeviceStatusUpdated(DStatusInfo statusInfo); 140 141 void OnServiceConnected(AudioServiceIndex serviceIndex); 142 143 void OnServiceDisconnected(AudioServiceIndex serviceIndex); 144 145 void OnMonoAudioConfigChanged(bool audioMono); 146 147 void OnAudioBalanceChanged(float audioBalance); 148 149 int32_t SetAudioSessionCallback(AudioSessionCallback *callback); 150 151 int32_t SetDeviceChangeCallback(const int32_t clientId, const DeviceFlag flag, const sptr<IRemoteObject> &object); 152 153 int32_t UnsetDeviceChangeCallback(const int32_t clientId); 154 155 int32_t RegisterAudioRendererEventListener(int32_t clientUID, const sptr<IRemoteObject> &object, 156 bool hasBTPermission); 157 158 int32_t UnregisterAudioRendererEventListener(int32_t clientUID); 159 160 int32_t RegisterAudioCapturerEventListener(int32_t clientUID, const sptr<IRemoteObject> &object, 161 bool hasBTPermission); 162 163 int32_t UnregisterAudioCapturerEventListener(int32_t clientUID); 164 165 int32_t RegisterTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo, 166 const sptr<IRemoteObject> &object); 167 168 int32_t UpdateTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo); 169 170 int32_t GetCurrentRendererChangeInfos(vector<unique_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos, 171 bool hasBTPermission); 172 173 int32_t GetCurrentCapturerChangeInfos(vector<unique_ptr<AudioCapturerChangeInfo>> &audioCapturerChangeInfos, 174 bool hasBTPermission); 175 176 void RegisteredTrackerClientDied(pid_t pid); 177 178 void RegisteredStreamListenerClientDied(pid_t pid); 179 180 int32_t ReconfigureAudioChannel(const uint32_t &count, DeviceType deviceType); 181 182 void OnAudioLatencyParsed(uint64_t latency); 183 184 void OnSinkLatencyParsed(uint32_t latency); 185 186 int32_t UpdateStreamState(int32_t clientUid, StreamSetStateEventInternal &streamSetStateEventInternal); 187 188 DeviceType GetDeviceTypeFromPin(AudioPin pin); 189 190 std::vector<sptr<VolumeGroupInfo>> GetVolumeGroupInfos(); 191 192 void SetParameterCallback(const std::shared_ptr<AudioParameterCallback>& callback); 193 194 void RegisterBluetoothListener(); 195 196 void UnregisterBluetoothListener(); 197 198 void SubscribeAccessibilityConfigObserver(); 199 200 std::vector<sptr<AudioDeviceDescriptor>> GetActiveOutputDeviceDescriptors(); 201 202 private: AudioPolicyService()203 AudioPolicyService() 204 : audioPolicyManager_(AudioPolicyManagerFactory::GetAudioPolicyManager()), 205 configParser_(ParserFactory::GetInstance().CreateParser(*this)), 206 streamCollector_(AudioStreamCollector::GetAudioStreamCollector()) 207 { 208 accessibilityConfigListener_ = std::make_shared<AccessibilityConfigListener>(*this); 209 deviceStatusListener_ = std::make_unique<DeviceStatusListener>(*this); 210 } 211 212 ~AudioPolicyService(); 213 214 std::string GetPortName(InternalDeviceType deviceType); 215 216 int32_t RememberRoutingInfo(sptr<AudioRendererFilter> audioRendererFilter, 217 sptr<AudioDeviceDescriptor> deviceDescriptor); 218 219 int32_t MoveToLocalOutputDevice(std::vector<SinkInput> sinkInputIds, 220 sptr<AudioDeviceDescriptor> localDeviceDescriptor); 221 222 int32_t MoveToRemoteOutputDevice(std::vector<SinkInput> sinkInputIds, 223 sptr<AudioDeviceDescriptor> remoteDeviceDescriptor); 224 225 int32_t MoveToLocalInputDevice(std::vector<uint32_t> sourceOutputIds, 226 sptr<AudioDeviceDescriptor> localDeviceDescriptor); 227 228 int32_t MoveToRemoteInputDevice(std::vector<uint32_t> sourceOutputIds, 229 sptr<AudioDeviceDescriptor> remoteDeviceDescriptor); 230 231 AudioModuleInfo ConstructRemoteAudioModuleInfo(std::string networkId, 232 DeviceRole deviceRole, DeviceType deviceType); 233 234 AudioIOHandle GetAudioIOHandle(InternalDeviceType deviceType); 235 236 int32_t OpenRemoteAudioDevice(std::string networkId, DeviceRole deviceRole, DeviceType deviceType, 237 sptr<AudioDeviceDescriptor> remoteDeviceDescriptor); 238 239 InternalDeviceType GetDeviceType(const std::string &deviceName); 240 241 std::string GetGroupName(const std::string& deviceName, const GroupType type); 242 243 InternalDeviceType GetCurrentActiveDevice(DeviceRole role) const; 244 245 DeviceRole GetDeviceRole(DeviceType deviceType) const; 246 247 DeviceRole GetDeviceRole(const std::string &role); 248 249 int32_t SelectNewDevice(DeviceRole deviceRole, DeviceType deviceType); 250 251 int32_t ActivateNewDevice(DeviceType deviceType, bool isSceneActivation); 252 253 DeviceRole GetDeviceRole(AudioPin pin) const; 254 255 int32_t ActivateNewDevice(std::string networkId, DeviceType deviceType, bool isRemote); 256 257 DeviceType FetchHighPriorityDevice(bool isOutputDevice); 258 259 void UpdateConnectedDevices(const AudioDeviceDescriptor& deviceDescriptor, 260 std::vector<sptr<AudioDeviceDescriptor>>& desc, bool status); 261 262 void TriggerDeviceChangedCallback(const std::vector<sptr<AudioDeviceDescriptor>> &devChangeDesc, bool connection); 263 264 std::vector<sptr<AudioDeviceDescriptor>> DeviceFilterByFlag(DeviceFlag flag, 265 const std::vector<sptr<AudioDeviceDescriptor>>& desc); 266 267 void WriteDeviceChangedSysEvents(const std::vector<sptr<AudioDeviceDescriptor>> &desc, bool isConnected); 268 269 bool GetActiveDeviceStreamInfo(DeviceType deviceType, AudioStreamInfo &streamInfo); 270 271 bool IsConfigurationUpdated(DeviceType deviceType, const AudioStreamInfo &streamInfo); 272 273 void UpdateInputDeviceInfo(DeviceType deviceType); 274 275 void UpdateStreamChangeDeviceInfo(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo); 276 277 void UpdateTrackerDeviceChange(const vector<sptr<AudioDeviceDescriptor>> &desc); 278 279 void UpdateGroupInfo(GroupType type, std::string groupName, int32_t& groupId, std::string networkId, 280 bool connected, int32_t mappingId); 281 282 void AddAudioDevice(AudioModuleInfo& moduleInfo, InternalDeviceType devType); 283 284 std::vector<sptr<AudioDeviceDescriptor>> GetDevicesForGroup(GroupType type, int32_t groupId); 285 286 bool interruptEnabled_ = true; 287 bool isUpdateRouteSupported_ = true; 288 bool isCurrentRemoteRenderer = false; 289 bool remoteCapturerSwitch = false; 290 bool isOpenRemoteDevice = false; 291 bool isBtListenerRegistered = false; 292 const int32_t G_UNKNOWN_PID = -1; 293 int32_t dAudioClientUid = 3055; 294 int32_t switchVolumeDelay_ = 500000; // us 295 uint64_t audioLatencyInMsec_ = 50; 296 uint32_t sinkLatencyInMsec_ {0}; 297 std::bitset<MIN_SERVICE_COUNT> serviceFlag_; 298 std::mutex serviceFlagMutex_; 299 DeviceType currentActiveDevice_ = DEVICE_TYPE_NONE; 300 DeviceType activeInputDevice_ = DEVICE_TYPE_NONE; 301 std::unordered_map<int32_t, std::pair<std::string, int32_t>> routerMap_; 302 IAudioPolicyInterface& audioPolicyManager_; 303 Parser& configParser_; 304 std::unordered_map<int32_t, std::shared_ptr<ToneInfo>> toneDescriptorMap; 305 AudioStreamCollector& streamCollector_; 306 std::shared_ptr<AccessibilityConfigListener> accessibilityConfigListener_; 307 std::unique_ptr<DeviceStatusListener> deviceStatusListener_; 308 std::vector<sptr<AudioDeviceDescriptor>> connectedDevices_; 309 std::unordered_map<std::string, AudioStreamInfo> connectedA2dpDeviceMap_; 310 std::string activeBTDevice_; 311 312 std::unordered_map<int32_t, std::pair<DeviceFlag, sptr<IStandardAudioPolicyManagerListener>>> 313 deviceChangeCallbackMap_; 314 AudioScene audioScene_ = AUDIO_SCENE_DEFAULT; 315 std::map<std::pair<AudioStreamType, AudioStreamType>, AudioFocusEntry> focusMap_ = {}; 316 std::unordered_map<ClassType, std::list<AudioModuleInfo>> deviceClassInfo_ = {}; 317 std::unordered_map<std::string, AudioIOHandle> IOHandles_ = {}; 318 std::vector<DeviceType> ioDeviceList = { 319 DEVICE_TYPE_BLUETOOTH_A2DP, 320 DEVICE_TYPE_BLUETOOTH_SCO, 321 DEVICE_TYPE_USB_HEADSET, 322 DEVICE_TYPE_WIRED_HEADSET 323 }; 324 std::vector<DeviceType> outputPriorityList_ = { 325 DEVICE_TYPE_BLUETOOTH_SCO, 326 DEVICE_TYPE_BLUETOOTH_A2DP, 327 DEVICE_TYPE_USB_HEADSET, 328 DEVICE_TYPE_WIRED_HEADSET, 329 DEVICE_TYPE_SPEAKER 330 }; 331 std::vector<DeviceType> inputPriorityList_ = { 332 DEVICE_TYPE_BLUETOOTH_SCO, 333 DEVICE_TYPE_BLUETOOTH_A2DP, 334 DEVICE_TYPE_USB_HEADSET, 335 DEVICE_TYPE_WIRED_HEADSET, 336 DEVICE_TYPE_MIC 337 }; 338 339 std::vector<sptr<VolumeGroupInfo>> volumeGroups_; 340 std::vector<sptr<InterruptGroupInfo>> interruptGroups_; 341 std::unordered_map<std::string, std::string> volumeGroupData_; 342 std::unordered_map<std::string, std::string> interruptGroupData_; 343 }; 344 } // namespace AudioStandard 345 } // namespace OHOS 346 347 #endif // ST_AUDIO_POLICY_SERVICE_H 348