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_SERVER_H
17 #define ST_AUDIO_SERVER_H
18 
19 #include <mutex>
20 #include <pthread.h>
21 #include <unordered_map>
22 
23 #include "accesstoken_kit.h"
24 #include "ipc_skeleton.h"
25 #include "iremote_stub.h"
26 #include "system_ability.h"
27 
28 #include "audio_manager_base.h"
29 #include "audio_server_death_recipient.h"
30 #include "audio_server_dump.h"
31 #include "audio_system_manager.h"
32 #include "audio_inner_call.h"
33 #include "i_audio_renderer_sink.h"
34 #include "i_audio_capturer_source.h"
35 #include "audio_effect_server.h"
36 #include "audio_asr.h"
37 #include "audio_utils.h"
38 #include "policy_handler.h"
39 
40 namespace OHOS {
41 namespace AudioStandard {
42 class AudioServer : public SystemAbility, public AudioManagerStub, public IAudioSinkCallback, IAudioSourceCallback,
43     public IAudioServerInnerCall {
44     DECLARE_SYSTEM_ABILITY(AudioServer);
45 public:
46     DISALLOW_COPY_AND_MOVE(AudioServer);
47     explicit AudioServer(int32_t systemAbilityId, bool runOnCreate = true);
48     virtual ~AudioServer() = default;
49     void OnDump() override;
50     void OnStart() override;
51     void OnStop() override;
52 
53     int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
54 
55     bool LoadAudioEffectLibraries(std::vector<Library> libraries, std::vector<Effect> effects,
56         std::vector<Effect>& successEffectList) override;
57     bool CreatePlaybackCapturerManager() override;
58     bool CreateEffectChainManager(std::vector<EffectChain> &effectChains,
59         const EffectChainManagerParam &effectParam, const EffectChainManagerParam &enhanceParam) override;
60     void SetOutputDeviceSink(int32_t deviceType, std::string &sinkName) override;
61     int32_t SetMicrophoneMute(bool isMute) override;
62     int32_t SetVoiceVolume(float volume) override;
63     int32_t OffloadSetVolume(float volume) override;
64     int32_t SetAudioScene(AudioScene audioScene, std::vector<DeviceType> &activeOutputDevices,
65         DeviceType activeInputDevice, BluetoothOffloadState a2dpOffloadFlag) override;
66     static void *paDaemonThread(void *arg);
67     int32_t SetExtraParameters(const std::string& key,
68         const std::vector<std::pair<std::string, std::string>>& kvpairs) override;
69     void SetAudioParameter(const std::string& key, const std::string& value) override;
70     void SetAudioParameter(const std::string& networkId, const AudioParamKey key, const std::string& condition,
71         const std::string& value) override;
72     int32_t GetExtraParameters(const std::string &mainKey, const std::vector<std::string> &subKeys,
73         std::vector<std::pair<std::string, std::string>> &result) override;
74     const std::string GetAudioParameter(const std::string &key) override;
75     const std::string GetAudioParameter(const std::string& networkId, const AudioParamKey key,
76         const std::string& condition) override;
77     uint64_t GetTransactionId(DeviceType deviceType, DeviceRole deviceRole) override;
78     int32_t UpdateActiveDeviceRoute(DeviceType type, DeviceFlag flag, BluetoothOffloadState a2dpOffloadFlag) override;
79     int32_t UpdateActiveDevicesRoute(std::vector<std::pair<DeviceType, DeviceFlag>> &activeDevices,
80         BluetoothOffloadState a2dpOffloadFlag, const std::string &deviceName = "") override;
81     int32_t UpdateDualToneState(bool enable, int32_t sessionId) override;
82     void SetAudioMonoState(bool audioMono) override;
83     void SetAudioBalanceValue(float audioBalance) override;
84     int32_t SuspendRenderSink(const std::string &sinkName) override;
85     int32_t RestoreRenderSink(const std::string &sinkName) override;
86 
87     int32_t SetAsrAecMode(AsrAecMode asrAecMode) override;
88     int32_t GetAsrAecMode(AsrAecMode &asrAecMode) override;
89     int32_t SetAsrNoiseSuppressionMode(AsrNoiseSuppressionMode asrNoiseSuppressionMode) override;
90     int32_t GetAsrNoiseSuppressionMode(AsrNoiseSuppressionMode &asrNoiseSuppressionMode) override;
91     int32_t SetAsrWhisperDetectionMode(AsrWhisperDetectionMode asrWhisperDetectionMode) override;
92     int32_t GetAsrWhisperDetectionMode(AsrWhisperDetectionMode &asrWhisperDetectionMode) override;
93     int32_t SetAsrVoiceControlMode(AsrVoiceControlMode asrVoiceControlMode, bool on) override;
94     int32_t SetAsrVoiceMuteMode(AsrVoiceMuteMode asrVoiceMuteMode, bool on) override;
95     int32_t IsWhispering() override;
96 
97     // for effect
98     int32_t SetAudioEffectProperty(const AudioEffectPropertyArray &propertyArray) override;
99     int32_t GetAudioEffectProperty(AudioEffectPropertyArray &propertyArray) override;
100     // for enhance
101     int32_t SetAudioEnhanceProperty(const AudioEnhancePropertyArray &propertyArray,
102         DeviceType deviceType = DEVICE_TYPE_NONE) override;
103     int32_t GetAudioEnhanceProperty(AudioEnhancePropertyArray &propertyArray,
104         DeviceType deviceType = DEVICE_TYPE_NONE) override;
105 
106     void NotifyDeviceInfo(std::string networkId, bool connected) override;
107 
108     int32_t CheckRemoteDeviceState(std::string networkId, DeviceRole deviceRole, bool isStartDevice) override;
109 
110     sptr<IRemoteObject> CreateAudioProcess(const AudioProcessConfig &config, int32_t &errorCode) override;
111 
112     // ISinkParameterCallback
113     void OnAudioSinkParamChange(const std::string &netWorkId, const AudioParamKey key,
114         const std::string &condition, const std::string &value) override;
115 
116     // IAudioSourceCallback
117     void OnWakeupClose() override;
118     void OnAudioSourceParamChange(const std::string &netWorkId, const AudioParamKey key,
119         const std::string &condition, const std::string &value) override;
120 
121     int32_t SetParameterCallback(const sptr<IRemoteObject>& object) override;
122 
123     int32_t RegiestPolicyProvider(const sptr<IRemoteObject> &object) override;
124 
125     int32_t SetWakeupSourceCallback(const sptr<IRemoteObject>& object) override;
126 
127     void RequestThreadPriority(uint32_t tid, std::string bundleName) override;
128 
129     int32_t SetSupportStreamUsage(std::vector<int32_t> usage) override;
130 
131     int32_t SetCaptureSilentState(bool state) 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, int32_t deviceType) 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 int32_t &sessionID, bool isOutput) 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 SetHibernateEndpointRelease(const bool &isHibernate) override;
177 protected:
178     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
179 
180 private:
181     bool VerifyClientPermission(const std::string &permissionName,
182         Security::AccessToken::AccessTokenID tokenId = Security::AccessToken::INVALID_TOKENID);
183     bool PermissionChecker(const AudioProcessConfig &config);
184     bool CheckPlaybackPermission(const AudioProcessConfig &config);
185     bool CheckRecorderPermission(const AudioProcessConfig &config);
186     bool CheckVoiceCallRecorderPermission(Security::AccessToken::AccessTokenID tokenId);
187 
188     void ResetRecordConfig(AudioProcessConfig &config);
189     AudioProcessConfig ResetProcessConfig(const AudioProcessConfig &config);
190     bool CheckStreamInfoFormat(const AudioProcessConfig &config);
191     bool CheckRendererFormat(const AudioProcessConfig &config);
192     bool CheckRecorderFormat(const AudioProcessConfig &config);
193     bool CheckConfigFormat(const AudioProcessConfig &config);
194     int32_t GetHapBuildApiVersion(int32_t callerUid);
195 
196     void NotifyProcessStatus(bool isStart);
197     void AudioServerDied(pid_t pid, pid_t uid);
198     void RegisterPolicyServerDeathRecipient();
199     void RegisterAudioCapturerSourceCallback();
200     int32_t SetIORoutes(std::vector<std::pair<DeviceType, DeviceFlag>> &activeDevices,
201         BluetoothOffloadState a2dpOffloadFlag, const std::string &deviceName = "");
202     int32_t SetIORoutes(DeviceType type, DeviceFlag flag, std::vector<DeviceType> deviceTypes,
203         BluetoothOffloadState a2dpOffloadFlag, const std::string &deviceName = "");
204     bool CheckAndPrintStacktrace(const std::string &key);
205     const std::string GetDPParameter(const std::string &condition);
206     const std::string GetUsbParameter();
207     void WriteServiceStartupError();
208     bool IsNormalIpcStream(const AudioProcessConfig &config) const;
209     void RecognizeAudioEffectType(const std::string &mainkey, const std::string &subkey,
210         const std::string &extraSceneType);
211     int32_t SetSystemVolumeToEffect(const AudioStreamType streamType, float volume);
212     const std::string GetBundleNameFromUid(int32_t uid);
213     bool IsFastBlocked(int32_t uid);
214     int32_t SetVolumeInfoForEnhanceChain(const AudioStreamType &streamType);
215     int32_t SetMicrophoneMuteForEnhanceChain(const bool &isMute);
216     void InitMaxRendererStreamCntPerUid();
217     int32_t CheckParam(const AudioProcessConfig &config);
218     void SendRendererCreateErrorInfo(const StreamUsage &sreamUsage,
219         const int32_t &errorCode);
220     int32_t CheckMaxRendererInstances();
221     bool SetPcmDumpParameter(const std::vector<std::pair<std::string, std::string>> &params);
222     bool GetPcmDumpParameter(const std::vector<std::string> &subKeys,
223         std::vector<std::pair<std::string, std::string>> &result);
224     sptr<IRemoteObject> CreateAudioStream(const AudioProcessConfig &config, int32_t callingUid);
225 private:
226     static constexpr int32_t MEDIA_SERVICE_UID = 1013;
227     static constexpr int32_t VASSISTANT_UID = 3001;
228     static constexpr int32_t MAX_VOLUME = 15;
229     static constexpr int32_t MIN_VOLUME = 0;
230     static uint32_t paDaemonTid_;
231     static std::unordered_map<int, float> AudioStreamVolumeMap;
232     static std::map<std::string, std::string> audioParameters;
233     static std::unordered_map<std::string, std::unordered_map<std::string, std::set<std::string>>> audioParameterKeys;
234 
235     pthread_t m_paDaemonThread;
236     AudioScene audioScene_ = AUDIO_SCENE_DEFAULT;
237 
238     // Capturer status flags: each capturer is represented by a single bit.
239     // 0 indicates the capturer has stopped; 1 indicates the capturer has started.
240     std::atomic<uint64_t> capturerStateFlag_ = 0;
241 
242     std::shared_ptr<AudioParameterCallback> audioParamCb_;
243     std::mutex onCapturerStateCbMutex_;
244     std::shared_ptr<WakeUpSourceCallback> wakeupCallback_;
245     std::mutex audioParamCbMtx_;
246     std::mutex setWakeupCloseCallbackMutex_;
247     std::mutex audioParameterMutex_;
248     std::mutex audioSceneMutex_;
249     std::unique_ptr<AudioEffectServer> audioEffectServer_;
250     bool isFastControlled_ = false;
251     int32_t maxRendererStreamCntPerUid_ = 0;
252     std::mutex streamLifeCycleMutex_ {};
253 };
254 } // namespace AudioStandard
255 } // namespace OHOS
256 #endif // ST_AUDIO_SERVER_H
257