• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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_MANAGER_H
17 #define ST_AUDIO_POLICY_MANAGER_H
18 
19 #include <cstdint>
20 #include "audio_client_tracker_callback_stub.h"
21 #include "audio_effect.h"
22 #include "audio_concurrency_callback.h"
23 #include "audio_concurrency_state_listener_stub.h"
24 #include "audio_interrupt_callback.h"
25 #include "audio_policy_base.h"
26 #include "audio_policy_manager_listener_stub.h"
27 #include "audio_policy_client_stub_impl.h"
28 #include "audio_routing_manager.h"
29 #include "audio_routing_manager_listener_stub.h"
30 #include "audio_system_manager.h"
31 #include "i_standard_client_tracker.h"
32 #include "audio_log.h"
33 #include "microphone_descriptor.h"
34 #include "audio_spatialization_manager.h"
35 #include "audio_spatialization_state_change_listener_stub.h"
36 #include "i_standard_spatialization_state_change_listener.h"
37 
38 namespace OHOS {
39 namespace AudioStandard {
40 using InternalDeviceType = DeviceType;
41 using InternalAudioCapturerOptions = AudioCapturerOptions;
42 
43 struct CallbackChangeInfo {
44     std::mutex mutex;
45     bool isEnable = false;
46 };
47 
48 class AudioPolicyManager {
49 public:
50     static AudioPolicyManager& GetInstance();
51 
52     int32_t GetMaxVolumeLevel(AudioVolumeType volumeType);
53 
54     int32_t GetMinVolumeLevel(AudioVolumeType volumeType);
55 
56     int32_t SetSystemVolumeLevel(AudioVolumeType volumeType, int32_t volumeLevel, bool isLegacy = false,
57         int32_t volumeFlag = 0);
58 
59     AudioStreamType GetSystemActiveVolumeType(const int32_t clientUid);
60 
61     int32_t GetSystemVolumeLevel(AudioVolumeType volumeType);
62 
63     int32_t SetLowPowerVolume(int32_t streamId, float volume);
64 
65     float GetLowPowerVolume(int32_t streamId);
66 
67     float GetSingleStreamVolume(int32_t streamId);
68 
69     int32_t SetStreamMute(AudioVolumeType volumeType, bool mute, bool isLegacy = false);
70 
71     bool GetStreamMute(AudioVolumeType volumeType);
72 
73     bool IsStreamActive(AudioVolumeType volumeType);
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     std::vector<sptr<AudioDeviceDescriptor>> GetDevicesInner(DeviceFlag deviceFlag);
86 
87     int32_t SetDeviceActive(InternalDeviceType deviceType, bool active);
88 
89     bool IsDeviceActive(InternalDeviceType deviceType);
90 
91     DeviceType GetActiveOutputDevice();
92 
93     DeviceType GetActiveInputDevice();
94 
95     int32_t SetRingerModeLegacy(AudioRingerMode ringMode);
96 
97     int32_t SetRingerMode(AudioRingerMode ringMode);
98 
99 #ifdef FEATURE_DTMF_TONE
100     std::vector<int32_t> GetSupportedTones();
101 
102     std::shared_ptr<ToneInfo> GetToneConfig(int32_t ltonetype);
103 #endif
104 
105     AudioRingerMode GetRingerMode();
106 
107     int32_t SetAudioScene(AudioScene scene);
108 
109     int32_t SetMicrophoneMute(bool isMute);
110 
111     int32_t SetMicrophoneMuteAudioConfig(bool isMute);
112 
113     int32_t SetMicrophoneMutePersistent(const bool isMute, const PolicyType type);
114 
115     bool GetPersistentMicMuteState();
116 
117     bool IsMicrophoneMuteLegacy();
118 
119     bool IsMicrophoneMute();
120 
121     AudioScene GetAudioScene();
122 
123     int32_t SetDeviceChangeCallback(const int32_t clientId, const DeviceFlag flag,
124         const std::shared_ptr<AudioManagerDeviceChangeCallback> &callback);
125 
126     int32_t UnsetDeviceChangeCallback(const int32_t clientId, DeviceFlag flag,
127         std::shared_ptr<AudioManagerDeviceChangeCallback> &cb);
128 
129     int32_t SetRingerModeCallback(const int32_t clientId,
130         const std::shared_ptr<AudioRingerModeCallback> &callback, API_VERSION api_v = API_9);
131 
132     int32_t UnsetRingerModeCallback(const int32_t clientId);
133 
134     int32_t UnsetRingerModeCallback(const int32_t clientId,
135         const std::shared_ptr<AudioRingerModeCallback> &callback);
136 
137     int32_t SetMicStateChangeCallback(const int32_t clientId,
138         const std::shared_ptr<AudioManagerMicStateChangeCallback> &callback);
139 
140     int32_t UnsetMicStateChangeCallback(const std::shared_ptr<AudioManagerMicStateChangeCallback> &callback);
141 
142     int32_t SetAudioInterruptCallback(const uint32_t sessionID,
143         const std::shared_ptr<AudioInterruptCallback> &callback, uint32_t clientUid, const int32_t zoneID = 0);
144 
145     int32_t UnsetAudioInterruptCallback(const uint32_t sessionID, const int32_t zoneID = 0);
146 
147     int32_t ActivateAudioInterrupt(
148         const AudioInterrupt &audioInterrupt, const int32_t zoneID = 0, const bool isUpdatedAudioStrategy = false);
149 
150     int32_t DeactivateAudioInterrupt(const AudioInterrupt &audioInterrupt, const int32_t zoneID = 0);
151 
152     int32_t SetQueryClientTypeCallback(const std::shared_ptr<AudioQueryClientTypeCallback> &callback);
153 
154     int32_t SetAudioManagerInterruptCallback(const int32_t clientId,
155         const std::shared_ptr<AudioInterruptCallback> &callback);
156 
157     int32_t UnsetAudioManagerInterruptCallback(const int32_t clientId);
158 
159     int32_t RequestAudioFocus(const int32_t clientId, const AudioInterrupt &audioInterrupt);
160 
161     int32_t AbandonAudioFocus(const int32_t clientId, const AudioInterrupt &audioInterrupt);
162 
163     AudioStreamType GetStreamInFocus(const int32_t zoneID = 0);
164 
165     int32_t GetSessionInfoInFocus(AudioInterrupt &audioInterrupt, const int32_t zoneID = 0);
166 
167     int32_t ActivateAudioSession(const AudioSessionStrategy &strategy);
168 
169     int32_t DeactivateAudioSession();
170 
171     bool IsAudioSessionActivated();
172 
173     int32_t SetAudioSessionCallback(const std::shared_ptr<AudioSessionCallback> &audioSessionCallback);
174 
175     int32_t UnsetAudioSessionCallback();
176 
177     int32_t UnsetAudioSessionCallback(const std::shared_ptr<AudioSessionCallback> &audioSessionCallback);
178 
179     int32_t SetVolumeKeyEventCallback(const int32_t clientPid,
180         const std::shared_ptr<VolumeKeyEventCallback> &callback, API_VERSION api_v = API_9);
181 
182     int32_t UnsetVolumeKeyEventCallback(const std::shared_ptr<VolumeKeyEventCallback> &callback);
183 
184     bool CheckRecordingCreate(uint32_t appTokenId, uint64_t appFullTokenId, int32_t appUid,
185         SourceType sourceType = SOURCE_TYPE_MIC);
186 
187     bool CheckRecordingStateChange(uint32_t appTokenId, uint64_t appFullTokenId, int32_t appUid,
188         AudioPermissionState state);
189 
190     int32_t ReconfigureAudioChannel(const uint32_t &count, DeviceType deviceType);
191 
192     int32_t GetAudioLatencyFromXml();
193 
194     uint32_t GetSinkLatencyFromXml();
195 
196     int32_t GetPreferredOutputStreamType(AudioRendererInfo &rendererInfo);
197 
198     int32_t GetPreferredInputStreamType(AudioCapturerInfo &capturerInfo);
199 
200     int32_t RegisterAudioRendererEventListener(const std::shared_ptr<AudioRendererStateChangeCallback> &callback);
201 
202     int32_t UnregisterAudioRendererEventListener(
203         const std::vector<std::shared_ptr<AudioRendererStateChangeCallback>> &callbacks);
204 
205     int32_t UnregisterAudioRendererEventListener(
206         const std::shared_ptr<AudioRendererStateChangeCallback> &callback);
207 
208     int32_t RegisterAudioCapturerEventListener(const int32_t clientPid,
209         const std::shared_ptr<AudioCapturerStateChangeCallback> &callback);
210 
211     int32_t UnregisterAudioCapturerEventListener(const int32_t clientPid);
212 
213     int32_t RegisterDeviceChangeWithInfoCallback(
214         const uint32_t sessionID, const std::weak_ptr<DeviceChangeWithInfoCallback> &callback);
215 
216     int32_t UnregisterDeviceChangeWithInfoCallback(const uint32_t sessionID);
217 
218     int32_t RegisterTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo,
219         const std::shared_ptr<AudioClientTracker> &clientTrackerObj);
220 
221     int32_t UpdateTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo);
222 
223     int32_t GetCurrentRendererChangeInfos(
224         std::vector<std::unique_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos);
225 
226     int32_t GetCurrentCapturerChangeInfos(
227         std::vector<std::unique_ptr<AudioCapturerChangeInfo>> &audioCapturerChangeInfos);
228 
229     int32_t UpdateStreamState(const int32_t clientUid, StreamSetState streamSetState,
230                                     StreamUsage streamUsage);
231 
232     int32_t GetVolumeGroupInfos(std::string networkId, std::vector<sptr<VolumeGroupInfo>> &infos);
233 
234     int32_t GetNetworkIdByGroupId(int32_t groupId, std::string &networkId);
235 
236     bool IsAudioRendererLowLatencySupported(const AudioStreamInfo &audioStreamInfo);
237 
238     std::vector<sptr<AudioDeviceDescriptor>> GetPreferredOutputDeviceDescriptors(AudioRendererInfo &rendererInfo);
239 
240     std::vector<sptr<AudioDeviceDescriptor>> GetPreferredInputDeviceDescriptors(AudioCapturerInfo &captureInfo);
241 
242     int32_t SetPreferredOutputDeviceChangeCallback(const int32_t clientId,
243         const std::shared_ptr<AudioPreferredOutputDeviceChangeCallback> &callback);
244 
245     int32_t SetPreferredInputDeviceChangeCallback(
246         const std::shared_ptr<AudioPreferredInputDeviceChangeCallback> &callback);
247 
248     int32_t UnsetPreferredOutputDeviceChangeCallback(const int32_t clientId);
249 
250     int32_t UnsetPreferredInputDeviceChangeCallback();
251 
252     int32_t GetAudioFocusInfoList(std::list<std::pair<AudioInterrupt, AudioFocuState>> &focusInfoList,
253         const int32_t zoneID = 0);
254 
255     int32_t RegisterFocusInfoChangeCallback(const int32_t clientId,
256         const std::shared_ptr<AudioFocusInfoChangeCallback> &callback);
257 
258     int32_t UnregisterFocusInfoChangeCallback(const int32_t clientId);
259 
260     static void AudioPolicyServerDied(pid_t pid);
261 
262     int32_t SetSystemSoundUri(const std::string &key, const std::string &uri);
263 
264     std::string GetSystemSoundUri(const std::string &key);
265 
266     float GetMinStreamVolume(void);
267 
268     float GetMaxStreamVolume(void);
269     int32_t RegisterAudioPolicyServerDiedCb(const int32_t clientPid,
270         const std::shared_ptr<AudioRendererPolicyServiceDiedCallback> &callback);
271     int32_t UnregisterAudioPolicyServerDiedCb(const int32_t clientPid);
272 
273     int32_t RegisterAudioStreamPolicyServerDiedCb(
274         const std::shared_ptr<AudioStreamPolicyServiceDiedCallback> &callback);
275     int32_t UnregisterAudioStreamPolicyServerDiedCb(
276         const std::shared_ptr<AudioStreamPolicyServiceDiedCallback> &callback);
277 
278     bool IsVolumeUnadjustable();
279 
280     int32_t AdjustVolumeByStep(VolumeAdjustType adjustType);
281 
282     int32_t AdjustSystemVolumeByStep(AudioVolumeType volumeType, VolumeAdjustType adjustType);
283 
284     float GetSystemVolumeInDb(AudioVolumeType volumeType, int32_t volumeLevel, DeviceType deviceType);
285 
286     int32_t CheckMaxRendererInstances();
287 
288     int32_t QueryEffectSceneMode(SupportedEffectConfig &supportedEffectConfig);
289 
290     int32_t SetPlaybackCapturerFilterInfos(const AudioPlaybackCaptureConfig &config, uint32_t appTokenId);
291 
292     int32_t SetCaptureSilentState(bool state);
293 
294     int32_t GetHardwareOutputSamplingRate(const sptr<AudioDeviceDescriptor> &desc);
295 
296     void RecoverAudioPolicyCallbackClient();
297 
298     std::vector<sptr<MicrophoneDescriptor>> GetAudioCapturerMicrophoneDescriptors(int32_t sessionID);
299 
300     std::vector<sptr<MicrophoneDescriptor>> GetAvailableMicrophones();
301 
302     int32_t SetDeviceAbsVolumeSupported(const std::string &macAddress, const bool support);
303 
304     bool IsAbsVolumeScene();
305 
306     bool IsVgsVolumeSupported();
307 
308     int32_t SetA2dpDeviceVolume(const std::string &macAddress, const int32_t volume, const bool updateUi);
309 
310     std::vector<std::unique_ptr<AudioDeviceDescriptor>> GetAvailableDevices(AudioDeviceUsage usage);
311 
312     int32_t SetAvailableDeviceChangeCallback(const int32_t clientId, const AudioDeviceUsage usage,
313         const std::shared_ptr<AudioManagerAvailableDeviceChangeCallback>& callback);
314 
315     int32_t UnsetAvailableDeviceChangeCallback(const int32_t clientId, AudioDeviceUsage usage);
316 
317     bool IsSpatializationEnabled();
318 
319     bool IsSpatializationEnabled(const std::string address);
320 
321     int32_t SetSpatializationEnabled(const bool enable);
322 
323     int32_t SetSpatializationEnabled(const sptr<AudioDeviceDescriptor> &selectedAudioDevice, const bool enable);
324 
325     bool IsHeadTrackingEnabled();
326 
327     bool IsHeadTrackingEnabled(const std::string address);
328 
329     int32_t SetHeadTrackingEnabled(const bool enable);
330 
331     int32_t SetHeadTrackingEnabled(const sptr<AudioDeviceDescriptor> &selectedAudioDevice, const bool enable);
332 
333     int32_t RegisterSpatializationEnabledEventListener(
334         const std::shared_ptr<AudioSpatializationEnabledChangeCallback> &callback);
335 
336     int32_t RegisterHeadTrackingEnabledEventListener(
337         const std::shared_ptr<AudioHeadTrackingEnabledChangeCallback> &callback);
338 
339     int32_t UnregisterSpatializationEnabledEventListener();
340 
341     int32_t UnregisterHeadTrackingEnabledEventListener();
342 
343     AudioSpatializationState GetSpatializationState(const StreamUsage streamUsage);
344 
345     bool IsSpatializationSupported();
346 
347     bool IsSpatializationSupportedForDevice(const std::string address);
348 
349     bool IsHeadTrackingSupported();
350 
351     bool IsHeadTrackingSupportedForDevice(const std::string address);
352 
353     int32_t UpdateSpatialDeviceState(const AudioSpatialDeviceState audioSpatialDeviceState);
354 
355     int32_t RegisterSpatializationStateEventListener(const uint32_t sessionID, const StreamUsage streamUsage,
356         const std::shared_ptr<AudioSpatializationStateChangeCallback> &callback);
357 
358     int32_t ConfigDistributedRoutingRole(sptr<AudioDeviceDescriptor> descriptor, CastType type);
359 
360     int32_t SetDistributedRoutingRoleCallback(const std::shared_ptr<AudioDistributedRoutingRoleCallback> &callback);
361 
362     int32_t UnsetDistributedRoutingRoleCallback();
363 
364     int32_t UnregisterSpatializationStateEventListener(const uint32_t sessionID);
365 
366     int32_t CreateAudioInterruptZone(const std::set<int32_t> &pids, const int32_t zoneID);
367 
368     int32_t AddAudioInterruptZonePids(const std::set<int32_t> &pids, const int32_t zoneID);
369 
370     int32_t RemoveAudioInterruptZonePids(const std::set<int32_t> &pids, const int32_t zoneID);
371 
372     int32_t ReleaseAudioInterruptZone(const int32_t zoneID);
373 
374     int32_t SetCallDeviceActive(InternalDeviceType deviceType, bool active, std::string address);
375 
376     std::unique_ptr<AudioDeviceDescriptor> GetActiveBluetoothDevice();
377 
378     int32_t NotifyCapturerAdded(AudioCapturerInfo capturerInfo, AudioStreamInfo streamInfo, uint32_t sessionId);
379 
380     ConverterConfig GetConverterConfig();
381 
382     void FetchOutputDeviceForTrack(AudioStreamChangeInfo &streamChangeInfo,
383         const AudioStreamDeviceChangeReasonExt reason);
384 
385     void FetchInputDeviceForTrack(AudioStreamChangeInfo &streamChangeInfo);
386 
387     bool IsHighResolutionExist();
388 
389     int32_t SetHighResolutionExist(bool highResExist);
390 
391     AudioSpatializationSceneType GetSpatializationSceneType();
392 
393     int32_t SetSpatializationSceneType(const AudioSpatializationSceneType spatializationSceneType);
394 
395     float GetMaxAmplitude(const int32_t deviceId);
396 
397     int32_t DisableSafeMediaVolume();
398 
399     bool IsHeadTrackingDataRequested(const std::string &macAddress);
400 
401     int32_t RegisterHeadTrackingDataRequestedEventListener(const std::string &macAddress,
402         const std::shared_ptr<HeadTrackingDataRequestedChangeCallback> &callback);
403 
404     int32_t UnregisterHeadTrackingDataRequestedEventListener(const std::string &macAddress);
405 
406     int32_t SetAudioDeviceRefinerCallback(const std::shared_ptr<AudioDeviceRefiner> &callback);
407 
408     int32_t UnsetAudioDeviceRefinerCallback();
409 
410     int32_t TriggerFetchDevice(AudioStreamDeviceChangeReasonExt reason);
411 
412     int32_t MoveToNewPipe(const uint32_t sessionId, const AudioPipeType pipeType);
413 
414     int32_t SetAudioConcurrencyCallback(const uint32_t sessionID,
415         const std::shared_ptr<AudioConcurrencyCallback> &callback);
416 
417     int32_t UnsetAudioConcurrencyCallback(const uint32_t sessionID);
418 
419     int32_t ActivateAudioConcurrency(const AudioPipeType &pipeType);
420 
421     int32_t InjectInterruption(const std::string networkId, InterruptEvent &event);
422 
423     int32_t SetMicrophoneBlockedCallback(const int32_t clientId,
424         const std::shared_ptr<AudioManagerMicrophoneBlockedCallback> &callback);
425 
426     int32_t UnsetMicrophoneBlockedCallback(const int32_t clientId,
427         const std::shared_ptr<AudioManagerMicrophoneBlockedCallback> &callback);
428 
429     int32_t SetDefaultOutputDevice(const DeviceType deviceType, const uint32_t sessionID,
430         const StreamUsage streamUsage, bool isRunning);
431 
432     int32_t LoadSplitModule(const std::string &splitArgs, const std::string &networkId);
433 
434     int32_t SetVoiceRingtoneMute(bool isMute);
435 private:
AudioPolicyManager()436     AudioPolicyManager() {}
~AudioPolicyManager()437     ~AudioPolicyManager() {}
438 
439     int32_t RegisterPolicyCallbackClientFunc(const sptr<IAudioPolicy> &gsp);
440     int32_t SetClientCallbacksEnable(const CallbackChange &callbackchange, const bool &enable);
441 
442     std::mutex listenerStubMutex_;
443     std::mutex registerCallbackMutex_;
444     std::mutex stateChangelistenerStubMutex_;
445     std::mutex clientTrackerStubMutex_;
446     sptr<AudioPolicyClientStubImpl> audioPolicyClientStubCB_;
447     std::atomic<bool> isAudioPolicyClientRegisted_ = false;
448 
449     static std::unordered_map<int32_t, std::weak_ptr<AudioRendererPolicyServiceDiedCallback>> rendererCBMap_;
450     static std::vector<std::weak_ptr<AudioStreamPolicyServiceDiedCallback>> audioStreamCBMap_;
451 
452     bool isAudioRendererEventListenerRegistered = false;
453     bool isAudioCapturerEventListenerRegistered = false;
454 
455     std::array<CallbackChangeInfo, CALLBACK_MAX> callbackChangeInfos_ = {};
456 };
457 } // namespce AudioStandard
458 } // namespace OHOS
459 
460 #endif // ST_AUDIO_POLICY_MANAGER_H
461