• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 
16 #ifndef ST_AUDIO_SERVER_H
17 #define ST_AUDIO_SERVER_H
18 
19 #include <mutex>
20 #include <condition_variable>
21 #include <pthread.h>
22 #include <unordered_map>
23 
24 #include "accesstoken_kit.h"
25 #include "ipc_skeleton.h"
26 #include "iremote_stub.h"
27 #include "system_ability.h"
28 
29 #include "audio_manager_base.h"
30 #include "audio_server_death_recipient.h"
31 #include "audio_server_dump.h"
32 #include "audio_system_manager.h"
33 #include "audio_inner_call.h"
34 #include "common/hdi_adapter_info.h"
35 #include "sink/i_audio_render_sink.h"
36 #include "source/i_audio_capture_source.h"
37 #include "audio_effect_server.h"
38 #include "audio_asr.h"
39 #include "policy_handler.h"
40 
41 namespace OHOS {
42 namespace AudioStandard {
43 class AudioServer : public SystemAbility, public AudioManagerStub, public IAudioSinkCallback, IAudioSourceCallback,
44     public IAudioServerInnerCall {
45     DECLARE_SYSTEM_ABILITY(AudioServer);
46 public:
47     DISALLOW_COPY_AND_MOVE(AudioServer);
48     explicit AudioServer(int32_t systemAbilityId, bool runOnCreate = true);
49     virtual ~AudioServer() = default;
50     void OnDump() override;
51     void OnStart() override;
52     void OnStop() override;
53 
54     int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
55 
56     bool LoadAudioEffectLibraries(std::vector<Library> libraries, std::vector<Effect> effects,
57         std::vector<Effect>& successEffectList) override;
58     bool CreatePlaybackCapturerManager() override;
59     bool CreateEffectChainManager(std::vector<EffectChain> &effectChains,
60         const EffectChainManagerParam &effectParam, const EffectChainManagerParam &enhanceParam) override;
61     void SetOutputDeviceSink(int32_t deviceType, std::string &sinkName) override;
62     int32_t SetMicrophoneMute(bool isMute) override;
63     int32_t SetVoiceVolume(float volume) override;
64     int32_t OffloadSetVolume(float volume) override;
65     int32_t SetAudioScene(AudioScene audioScene, std::vector<DeviceType> &activeOutputDevices,
66         DeviceType activeInputDevice, BluetoothOffloadState a2dpOffloadFlag) override;
67     static void *paDaemonThread(void *arg);
68     int32_t SetExtraParameters(const std::string& key,
69         const std::vector<std::pair<std::string, std::string>>& kvpairs) override;
70     void SetAudioParameter(const std::string& key, const std::string& value) override;
71     void SetAudioParameter(const std::string& networkId, const AudioParamKey key, const std::string& condition,
72         const std::string& value) override;
73     int32_t GetExtraParameters(const std::string &mainKey, const std::vector<std::string> &subKeys,
74         std::vector<std::pair<std::string, std::string>> &result) override;
75     const std::string GetAudioParameter(const std::string &key) override;
76     const std::string GetAudioParameter(const std::string& networkId, const AudioParamKey key,
77         const std::string& condition) override;
78     uint64_t GetTransactionId(DeviceType deviceType, DeviceRole deviceRole) override;
79     int32_t UpdateActiveDeviceRoute(DeviceType type, DeviceFlag flag, BluetoothOffloadState a2dpOffloadFlag) override;
80     int32_t UpdateActiveDevicesRoute(std::vector<std::pair<DeviceType, DeviceFlag>> &activeDevices,
81         BluetoothOffloadState a2dpOffloadFlag, const std::string &deviceName = "") override;
82     int32_t UpdateDualToneState(bool enable, int32_t sessionId) override;
83     void SetAudioMonoState(bool audioMono) override;
84     void SetAudioBalanceValue(float audioBalance) override;
85     int32_t SuspendRenderSink(const std::string &sinkName) override;
86     int32_t RestoreRenderSink(const std::string &sinkName) override;
87 
88     int32_t SetAsrAecMode(AsrAecMode asrAecMode) override;
89     int32_t GetAsrAecMode(AsrAecMode &asrAecMode) override;
90     int32_t SetAsrNoiseSuppressionMode(AsrNoiseSuppressionMode asrNoiseSuppressionMode) override;
91     int32_t GetAsrNoiseSuppressionMode(AsrNoiseSuppressionMode &asrNoiseSuppressionMode) override;
92     int32_t SetAsrWhisperDetectionMode(AsrWhisperDetectionMode asrWhisperDetectionMode) override;
93     int32_t GetAsrWhisperDetectionMode(AsrWhisperDetectionMode &asrWhisperDetectionMode) override;
94     int32_t SetAsrVoiceControlMode(AsrVoiceControlMode asrVoiceControlMode, bool on) override;
95     int32_t SetAsrVoiceMuteMode(AsrVoiceMuteMode asrVoiceMuteMode, bool on) override;
96     int32_t IsWhispering() override;
97     // for effect V3
98     int32_t SetAudioEffectProperty(const AudioEffectPropertyArrayV3 &propertyArray,
99         const DeviceType& deviceType = DEVICE_TYPE_NONE) override;
100     int32_t GetAudioEffectProperty(AudioEffectPropertyArrayV3 &propertyArray,
101         const DeviceType& deviceType = DEVICE_TYPE_NONE) override;
102     // for effect
103     int32_t SetAudioEffectProperty(const AudioEffectPropertyArray &propertyArray) override;
104     int32_t GetAudioEffectProperty(AudioEffectPropertyArray &propertyArray) override;
105     // for enhance
106     int32_t SetAudioEnhanceProperty(const AudioEnhancePropertyArray &propertyArray,
107         DeviceType deviceType = DEVICE_TYPE_NONE) override;
108     int32_t GetAudioEnhanceProperty(AudioEnhancePropertyArray &propertyArray,
109         DeviceType deviceType = DEVICE_TYPE_NONE) override;
110 
111     void NotifyDeviceInfo(std::string networkId, bool connected) override;
112 
113     int32_t CheckRemoteDeviceState(std::string networkId, DeviceRole deviceRole, bool isStartDevice) override;
114 
115     sptr<IRemoteObject> CreateAudioProcess(const AudioProcessConfig &config, int32_t &errorCode,
116         const AudioPlaybackCaptureConfig &filterConfig = AudioPlaybackCaptureConfig()) override;
117 
118     // ISinkParameterCallback
119     void OnRenderSinkParamChange(const std::string &networkId, const AudioParamKey key,
120         const std::string &condition, const std::string &value) override;
121 
122     // IAudioSourceCallback
123     void OnWakeupClose() override;
124     void OnCaptureSourceParamChange(const std::string &networkId, const AudioParamKey key,
125         const std::string &condition, const std::string &value) override;
126 
127     int32_t SetParameterCallback(const sptr<IRemoteObject>& object) override;
128 
129     int32_t RegiestPolicyProvider(const sptr<IRemoteObject> &object) override;
130 
131     int32_t SetWakeupSourceCallback(const sptr<IRemoteObject>& object) override;
132 
133     int32_t UpdateSpatializationState(AudioSpatializationState spatializationState) override;
134 
135     int32_t UpdateSpatialDeviceType(AudioSpatialDeviceType spatialDeviceType) override;
136 
137     int32_t NotifyStreamVolumeChanged(AudioStreamType streamType, float volume) override;
138 
139     int32_t SetSpatializationSceneType(AudioSpatializationSceneType spatializationSceneType) override;
140 
141     int32_t ResetRouteForDisconnect(DeviceType type) override;
142 
143     uint32_t GetEffectLatency(const std::string &sessionId) override;
144 
145     float GetMaxAmplitude(bool isOutputDevice, std::string deviceClass, SourceType sourceType) override;
146 
147     void ResetAudioEndpoint() override;
148 
149     void UpdateLatencyTimestamp(std::string &timestamp, bool isRenderer) override;
150 
151     bool GetEffectOffloadEnabled() override;
152 
153     void OnCapturerState(bool isActive, int32_t num);
154 
155     // IAudioServerInnerCall
156     int32_t SetSinkRenderEmpty(const std::string &devceClass, int32_t durationUs) final;
157 
158     int32_t SetSinkMuteForSwitchDevice(const std::string &devceClass, int32_t durationUs, bool mute) override;
159 
160     void LoadHdiEffectModel() override;
161 
162     void UpdateEffectBtOffloadSupported(const bool &isSupported) override;
163 
164     void RestoreSession(const uint32_t &sessionID, RestoreInfo restoreInfo) override;
165 
166     void SetRotationToEffect(const uint32_t rotate) override;
167 
168     void UpdateSessionConnectionState(const int32_t &sessionID, const int32_t &state) override;
169 
170     void SetNonInterruptMute(const uint32_t sessionId, const bool muteFlag) override;
171 
172     int32_t SetOffloadMode(uint32_t sessionId, int32_t state, bool isAppBack) override;
173 
174     int32_t UnsetOffloadMode(uint32_t sessionId) override;
175 
176     void OnRenderSinkStateChange(uint32_t sinkId, bool started) override;
177 
178     void CheckHibernateState(bool hibernate) override;
179 
180     sptr<IRemoteObject> CreateIpcOfflineStream(int32_t &errorCode) override;
181 
182     int32_t GetOfflineAudioEffectChains(std::vector<std::string> &effectChains) override;
183 
184     int32_t GetStandbyStatus(uint32_t sessionId, bool &isStandby, int64_t &enterStandbyTime) override;
185 
186     void NotifySettingsDataReady() override;
187 
188     int32_t GenerateSessionId(uint32_t &sessionId) override;
189 
190     void NotifyAccountsChanged() override;
191 
192     void GetAllSinkInputs(std::vector<SinkInput> &sinkInputs) override;
193 
194     void SetDefaultAdapterEnable(bool isEnable) override;
195 
196     void NotifyAudioPolicyReady() override;
197 #ifdef HAS_FEATURE_INNERCAPTURER
198     int32_t SetInnerCapLimit(uint32_t innerCapLimit) override;
199     int32_t CheckCaptureLimit(const AudioPlaybackCaptureConfig &config, int32_t &innerCapId) override;
200     int32_t ReleaseCaptureLimit(int32_t innerCapId) override;
201 #endif
202 
203     int32_t LoadHdiAdapter(uint32_t devMgrType, const std::string &adapterName) override;
204     void UnloadHdiAdapter(uint32_t devMgrType, const std::string &adapterName, bool force) override;
205     void SetDeviceConnectedFlag(bool flag) override;
206 protected:
207     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
208 
209 private:
210 #ifdef HAS_FEATURE_INNERCAPTURER
211     bool HandleCheckCaptureLimit(AudioProcessConfig &resetConfig,
212         const AudioPlaybackCaptureConfig &filterConfig);
213     int32_t InnerCheckCaptureLimit(const AudioPlaybackCaptureConfig &config, int32_t &innerCapId);
214 #endif
215     int32_t GetAudioEnhancePropertyArray(AudioEffectPropertyArrayV3 &propertyArray,
216         const DeviceType& deviceType);
217     int32_t GetAudioEffectPropertyArray(AudioEffectPropertyArrayV3 &propertyArray);
218     int32_t SetAudioEffectChainProperty(const AudioEffectPropertyArrayV3 &propertyArray);
219     int32_t SetAudioEnhanceChainProperty(const AudioEffectPropertyArrayV3 &propertyArray,
220         const DeviceType& deviceType);
221 
222     void SetA2dpAudioParameter(const std::string &renderValue);
223     bool VerifyClientPermission(const std::string &permissionName,
224         Security::AccessToken::AccessTokenID tokenId = Security::AccessToken::INVALID_TOKENID);
225     bool PermissionChecker(const AudioProcessConfig &config);
226     bool CheckPlaybackPermission(const AudioProcessConfig &config);
227     int32_t CheckInnerRecorderPermission(const AudioProcessConfig &config);
228     bool CheckRecorderPermission(const AudioProcessConfig &config);
229     bool HandleCheckRecorderBackgroundCapture(const AudioProcessConfig &config);
230     bool CheckVoiceCallRecorderPermission(Security::AccessToken::AccessTokenID tokenId);
231 
232     void ResetRecordConfig(AudioProcessConfig &config);
233     AudioProcessConfig ResetProcessConfig(const AudioProcessConfig &config);
234     bool CheckStreamInfoFormat(const AudioProcessConfig &config);
235     bool CheckRendererFormat(const AudioProcessConfig &config);
236     bool CheckRecorderFormat(const AudioProcessConfig &config);
237     bool CheckConfigFormat(const AudioProcessConfig &config);
238     int32_t GetHapBuildApiVersion(int32_t callerUid);
239 
240     void AudioServerDied(pid_t pid, pid_t uid);
241     void RegisterPolicyServerDeathRecipient();
242     void RegisterAudioCapturerSourceCallback();
243     void RegisterAudioRendererSinkCallback();
244 
245     int32_t SetIORoutes(std::vector<std::pair<DeviceType, DeviceFlag>> &activeDevices,
246         BluetoothOffloadState a2dpOffloadFlag, const std::string &deviceName = "");
247     int32_t SetIORoutes(DeviceType type, DeviceFlag flag, std::vector<DeviceType> deviceTypes,
248         BluetoothOffloadState a2dpOffloadFlag, const std::string &deviceName = "");
249     bool CheckAndPrintStacktrace(const std::string &key);
250     const std::string GetDPParameter(const std::string &condition);
251     const std::string GetUsbParameter(const std::string &condition);
252     void WriteServiceStartupError();
253     void ParseAudioParameter();
254     void CacheExtraParameters(const std::string& key,
255         const std::vector<std::pair<std::string, std::string>>& kvpairs);
256     bool IsNormalIpcStream(const AudioProcessConfig &config) const;
257     void RecognizeAudioEffectType(const std::string &mainkey, const std::string &subkey,
258         const std::string &extraSceneType);
259     int32_t SetSystemVolumeToEffect(const AudioStreamType streamType, float volume);
260     const std::string GetBundleNameFromUid(int32_t uid);
261     bool IsFastBlocked(int32_t uid, PlayerType playerType);
262     int32_t SetVolumeInfoForEnhanceChain(const AudioStreamType &streamType);
263     int32_t SetMicrophoneMuteForEnhanceChain(const bool &isMute);
264     void InitMaxRendererStreamCntPerUid();
265     int32_t CheckParam(const AudioProcessConfig &config);
266     void SendRendererCreateErrorInfo(const StreamUsage &sreamUsage,
267         const int32_t &errorCode);
268     int32_t CheckMaxRendererInstances();
269     bool SetPcmDumpParameter(const std::vector<std::pair<std::string, std::string>> &params);
270     bool GetPcmDumpParameter(const std::vector<std::string> &subKeys,
271         std::vector<std::pair<std::string, std::string>> &result);
272     sptr<IRemoteObject> CreateAudioStream(const AudioProcessConfig &config, int32_t callingUid);
273     int32_t SetAsrVoiceSuppressionControlMode(const AudioParamKey paramKey, AsrVoiceControlMode asrVoiceControlMode,
274         bool on, int32_t modifyVolume);
275     int32_t CheckAndWaitAudioPolicyReady();
276 private:
277     static constexpr int32_t MEDIA_SERVICE_UID = 1013;
278     static constexpr int32_t VASSISTANT_UID = 3001;
279     static constexpr int32_t MAX_VOLUME = 15;
280     static constexpr int32_t MIN_VOLUME = 0;
281     static constexpr int32_t ROOT_UID = 0;
282     static uint32_t paDaemonTid_;
283     static std::unordered_map<int, float> AudioStreamVolumeMap;
284     static std::map<std::string, std::string> audioParameters;
285     static std::unordered_map<std::string, std::unordered_map<std::string, std::set<std::string>>> audioParameterKeys;
286 
287     pthread_t m_paDaemonThread;
288     AudioScene audioScene_ = AUDIO_SCENE_DEFAULT;
289 
290     // Capturer status flags: each capturer is represented by a single bit.
291     // 0 indicates the capturer has stopped; 1 indicates the capturer has started.
292     std::atomic<uint64_t> capturerStateFlag_ = 0;
293 
294     std::shared_ptr<AudioParameterCallback> audioParamCb_;
295     std::mutex onCapturerStateCbMutex_;
296     std::shared_ptr<WakeUpSourceCallback> wakeupCallback_;
297     std::mutex audioParamCbMtx_;
298     std::mutex setWakeupCloseCallbackMutex_;
299     std::mutex audioParameterMutex_;
300     std::mutex audioSceneMutex_;
301     std::unique_ptr<AudioEffectServer> audioEffectServer_;
302 
303     std::shared_mutex audioParameterKeyMutex_;
304     bool isAudioParameterParsed_ = false;
305     std::vector<std::pair<std::string,
306         std::vector<std::pair<std::string, std::string>>>> audioExtraParameterCacheVector_;
307 
308     bool isFastControlled_ = false;
309     int32_t maxRendererStreamCntPerUid_ = 0;
310     std::mutex streamLifeCycleMutex_ {};
311     // Temporary resolution to avoid pcm driver problem
312     std::map<std::string, std::string> usbInfoMap_;
313 
314     std::atomic<bool> isAudioPolicyReady_ = false;
315     std::mutex isAudioPolicyReadyMutex_;
316     std::condition_variable isAudioPolicyReadyCv_;
317 
318     int32_t waitCreateStreamInServerCount_ = 0;
319 };
320 } // namespace AudioStandard
321 } // namespace OHOS
322 #endif // ST_AUDIO_SERVER_H
323