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 
38 namespace OHOS {
39 namespace AudioStandard {
40 class AudioServer : public SystemAbility, public AudioManagerStub, public IAudioSinkCallback, IAudioSourceCallback,
41     public IAudioServerInnerCall {
42     DECLARE_SYSTEM_ABILITY(AudioServer);
43 public:
44     DISALLOW_COPY_AND_MOVE(AudioServer);
45     explicit AudioServer(int32_t systemAbilityId, bool runOnCreate = true);
46     virtual ~AudioServer() = default;
47     void OnDump() override;
48     void OnStart() override;
49     void OnStop() override;
50 
51     int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
52 
53     bool LoadAudioEffectLibraries(std::vector<Library> libraries, std::vector<Effect> effects,
54         std::vector<Effect>& successEffectList) override;
55     bool CreatePlaybackCapturerManager() override;
56     bool CreateEffectChainManager(std::vector<EffectChain> &effectChains,
57         const EffectChainManagerParam &effectParam, const EffectChainManagerParam &enhanceParam) override;
58     void SetOutputDeviceSink(int32_t deviceType, std::string &sinkName) override;
59     int32_t SetMicrophoneMute(bool isMute) override;
60     int32_t SetVoiceVolume(float volume) override;
61     int32_t OffloadSetVolume(float volume) override;
62     int32_t SetAudioScene(AudioScene audioScene, std::vector<DeviceType> &activeOutputDevices,
63         DeviceType activeInputDevice, BluetoothOffloadState a2dpOffloadFlag) override;
64     static void *paDaemonThread(void *arg);
65     int32_t SetExtraParameters(const std::string& key,
66         const std::vector<std::pair<std::string, std::string>>& kvpairs) override;
67     void SetAudioParameter(const std::string& key, const std::string& value) override;
68     void SetAudioParameter(const std::string& networkId, const AudioParamKey key, const std::string& condition,
69         const std::string& value) override;
70     bool CheckAndPrintStacktrace(const std::string &key);
71     int32_t GetExtraParameters(const std::string &mainKey, const std::vector<std::string> &subKeys,
72         std::vector<std::pair<std::string, std::string>> &result) override;
73     const std::string GetAudioParameter(const std::string &key) override;
74     const std::string GetAudioParameter(const std::string& networkId, const AudioParamKey key,
75         const std::string& condition) override;
76     uint64_t GetTransactionId(DeviceType deviceType, DeviceRole deviceRole) override;
77     int32_t UpdateActiveDeviceRoute(DeviceType type, DeviceFlag flag, BluetoothOffloadState a2dpOffloadFlag) override;
78     int32_t UpdateActiveDevicesRoute(std::vector<std::pair<DeviceType, DeviceFlag>> &activeDevices,
79         BluetoothOffloadState a2dpOffloadFlag) override;
80     int32_t UpdateDualToneState(bool enable, int32_t sessionId) override;
81     void SetAudioMonoState(bool audioMono) override;
82     void SetAudioBalanceValue(float audioBalance) override;
83     int32_t SuspendRenderSink(const std::string &sinkName) override;
84     int32_t RestoreRenderSink(const std::string &sinkName) override;
85 
86     int32_t SetAsrAecMode(AsrAecMode asrAecMode) override;
87     int32_t GetAsrAecMode(AsrAecMode &asrAecMode) override;
88     int32_t SetAsrNoiseSuppressionMode(AsrNoiseSuppressionMode asrNoiseSuppressionMode) override;
89     int32_t GetAsrNoiseSuppressionMode(AsrNoiseSuppressionMode &asrNoiseSuppressionMode) override;
90     int32_t SetAsrWhisperDetectionMode(AsrWhisperDetectionMode asrWhisperDetectionMode) override;
91     int32_t GetAsrWhisperDetectionMode(AsrWhisperDetectionMode &asrWhisperDetectionMode) override;
92     int32_t SetAsrVoiceControlMode(AsrVoiceControlMode asrVoiceControlMode, bool on) override;
93     int32_t SetAsrVoiceMuteMode(AsrVoiceMuteMode asrVoiceMuteMode, bool on) override;
94     int32_t IsWhispering() override;
95 
96     void NotifyDeviceInfo(std::string networkId, bool connected) override;
97 
98     int32_t CheckRemoteDeviceState(std::string networkId, DeviceRole deviceRole, bool isStartDevice) override;
99 
100     sptr<IRemoteObject> CreateAudioProcess(const AudioProcessConfig &config, int32_t &errorCode) override;
101 
102     // ISinkParameterCallback
103     void OnAudioSinkParamChange(const std::string &netWorkId, const AudioParamKey key,
104         const std::string &condition, const std::string &value) override;
105 
106     // IAudioSourceCallback
107     void OnWakeupClose() override;
108     void OnAudioSourceParamChange(const std::string &netWorkId, const AudioParamKey key,
109         const std::string &condition, const std::string &value) override;
110 
111     int32_t SetParameterCallback(const sptr<IRemoteObject>& object) override;
112 
113     int32_t RegiestPolicyProvider(const sptr<IRemoteObject> &object) override;
114 
115     int32_t SetWakeupSourceCallback(const sptr<IRemoteObject>& object) override;
116 
117     void RequestThreadPriority(uint32_t tid, std::string bundleName) override;
118 
119     int32_t SetSupportStreamUsage(std::vector<int32_t> usage) override;
120 
121     int32_t SetCaptureSilentState(bool state) override;
122 
123     int32_t UpdateSpatializationState(AudioSpatializationState spatializationState) override;
124 
125     int32_t UpdateSpatialDeviceType(AudioSpatialDeviceType spatialDeviceType) override;
126 
127     int32_t NotifyStreamVolumeChanged(AudioStreamType streamType, float volume) override;
128 
129     int32_t SetSpatializationSceneType(AudioSpatializationSceneType spatializationSceneType) override;
130 
131     int32_t ResetRouteForDisconnect(DeviceType type) override;
132 
133     uint32_t GetEffectLatency(const std::string &sessionId) override;
134 
135     void UpdateLatencyTimestamp(std::string &timestamp, bool isRenderer) override;
136 
137     float GetMaxAmplitude(bool isOutputDevice, int32_t deviceType) override;
138 
139     void OnCapturerState(bool isActive, int32_t num);
140 
141     void ResetAudioEndpoint() override;
142 
143     bool GetEffectOffloadEnabled() override;
144 
145     // IAudioServerInnerCall
146     int32_t SetSinkRenderEmpty(const std::string &devceClass, int32_t durationUs) final;
147 
148     int32_t SetSinkMuteForSwitchDevice(const std::string &devceClass, int32_t durationUs, bool mute) override;
149 
150     void LoadHdiEffectModel() override;
151 
152     void UpdateEffectBtOffloadSupported(const bool &isSupported) override;
153 
154     void SetRotationToEffect(const uint32_t rotate) override;
155 
156     void UpdateSessionConnectionState(const int32_t &sessionID, const int32_t &state) override;
157 
158     void SetNonInterruptMute(const uint32_t sessionId, const bool muteFlag) override;
159 
160     sptr<IRemoteObject> CreateIpcOfflineStream(int32_t &errorCode) override;
161 
162     int32_t GetOfflineAudioEffectChains(std::vector<std::string> &effectChains) override;
163 protected:
164     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
165 
166 private:
167     bool VerifyClientPermission(const std::string &permissionName,
168         Security::AccessToken::AccessTokenID tokenId = Security::AccessToken::INVALID_TOKENID);
169     bool PermissionChecker(const AudioProcessConfig &config);
170     bool CheckPlaybackPermission(const AudioProcessConfig &config);
171     bool CheckRecorderPermission(const AudioProcessConfig &config);
172     bool CheckVoiceCallRecorderPermission(Security::AccessToken::AccessTokenID tokenId);
173 
174     void ResetRecordConfig(AudioProcessConfig &config);
175     AudioProcessConfig ResetProcessConfig(const AudioProcessConfig &config);
176     bool CheckStreamInfoFormat(const AudioProcessConfig &config);
177     bool CheckRendererFormat(const AudioProcessConfig &config);
178     bool CheckRecorderFormat(const AudioProcessConfig &config);
179     bool CheckConfigFormat(const AudioProcessConfig &config);
180     int32_t GetHapBuildApiVersion(int32_t callerUid);
181 
182     void AudioServerDied(pid_t pid);
183     void RegisterPolicyServerDeathRecipient();
184     void RegisterAudioCapturerSourceCallback();
185     int32_t SetIORoutes(std::vector<std::pair<DeviceType, DeviceFlag>> &activeDevices,
186         BluetoothOffloadState a2dpOffloadFlag);
187     int32_t SetIORoutes(DeviceType type, DeviceFlag flag, std::vector<DeviceType> deviceTypes,
188         BluetoothOffloadState a2dpOffloadFlag);
189     const std::string GetDPParameter(const std::string &condition);
190     const std::string GetUsbParameter();
191     void WriteServiceStartupError();
192     bool IsNormalIpcStream(const AudioProcessConfig &config) const;
193     void RecognizeAudioEffectType(const std::string &mainkey, const std::string &subkey,
194         const std::string &extraSceneType);
195     int32_t SetSystemVolumeToEffect(const AudioStreamType streamType, float volume);
196     const std::string GetBundleNameFromUid(int32_t uid);
197     bool IsFastBlocked(int32_t uid);
198     void InitMaxRendererStreamCntPerUid();
199     int32_t CheckParam(const AudioProcessConfig &config);
200     void SendRendererCreateErrorInfo(const StreamUsage &sreamUsage,
201         const int32_t &errorCode);
202     int32_t CheckMaxRendererInstances();
203     sptr<IRemoteObject> CreateAudioStream(const AudioProcessConfig &config, int32_t callingUid);
204 private:
205     static constexpr int32_t MEDIA_SERVICE_UID = 1013;
206     static constexpr int32_t VASSISTANT_UID = 3001;
207     static constexpr int32_t MAX_VOLUME = 15;
208     static constexpr int32_t MIN_VOLUME = 0;
209     static uint32_t paDaemonTid_;
210     static std::unordered_map<int, float> AudioStreamVolumeMap;
211     static std::map<std::string, std::string> audioParameters;
212     static std::unordered_map<std::string, std::unordered_map<std::string, std::set<std::string>>> audioParameterKeys;
213 
214     pthread_t m_paDaemonThread;
215     AudioScene audioScene_ = AUDIO_SCENE_DEFAULT;
216 
217     // Capturer status flags: each capturer is represented by a single bit.
218     // 0 indicates the capturer has stopped; 1 indicates the capturer has started.
219     std::atomic<uint64_t> capturerStateFlag_ = 0;
220 
221     std::shared_ptr<AudioParameterCallback> audioParamCb_;
222     std::mutex onCapturerStateCbMutex_;
223     std::shared_ptr<WakeUpSourceCallback> wakeupCallback_;
224     std::mutex audioParamCbMtx_;
225     std::mutex setWakeupCloseCallbackMutex_;
226     std::mutex audioParameterMutex_;
227     std::mutex audioSceneMutex_;
228     std::unique_ptr<AudioEffectServer> audioEffectServer_;
229     bool isFastControlled_ = false;
230     int32_t maxRendererStreamCntPerUid_ = 0;
231     std::mutex streamLifeCycleMutex_ {};
232 };
233 } // namespace AudioStandard
234 } // namespace OHOS
235 #endif // ST_AUDIO_SERVER_H
236