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 // for effect V3 96 int32_t SetAudioEffectProperty(const AudioEffectPropertyArrayV3 &propertyArray, 97 const DeviceType& deviceType = DEVICE_TYPE_NONE) override; 98 int32_t GetAudioEffectProperty(AudioEffectPropertyArrayV3 &propertyArray, 99 const DeviceType& deviceType = DEVICE_TYPE_NONE) override; 100 101 void NotifyDeviceInfo(std::string networkId, bool connected) override; 102 103 int32_t CheckRemoteDeviceState(std::string networkId, DeviceRole deviceRole, bool isStartDevice) override; 104 105 sptr<IRemoteObject> CreateAudioProcess(const AudioProcessConfig &config, int32_t &errorCode) override; 106 107 // ISinkParameterCallback 108 void OnAudioSinkParamChange(const std::string &netWorkId, const AudioParamKey key, 109 const std::string &condition, const std::string &value) override; 110 111 // IAudioSourceCallback 112 void OnWakeupClose() override; 113 void OnAudioSourceParamChange(const std::string &netWorkId, const AudioParamKey key, 114 const std::string &condition, const std::string &value) override; 115 116 int32_t SetParameterCallback(const sptr<IRemoteObject>& object) override; 117 118 int32_t RegiestPolicyProvider(const sptr<IRemoteObject> &object) override; 119 120 int32_t SetWakeupSourceCallback(const sptr<IRemoteObject>& object) override; 121 122 void RequestThreadPriority(uint32_t tid, std::string bundleName) override; 123 124 int32_t UpdateSpatializationState(AudioSpatializationState spatializationState) override; 125 126 int32_t UpdateSpatialDeviceType(AudioSpatialDeviceType spatialDeviceType) override; 127 128 int32_t NotifyStreamVolumeChanged(AudioStreamType streamType, float volume) override; 129 130 int32_t SetSpatializationSceneType(AudioSpatializationSceneType spatializationSceneType) override; 131 132 int32_t ResetRouteForDisconnect(DeviceType type) override; 133 134 uint32_t GetEffectLatency(const std::string &sessionId) override; 135 136 void UpdateLatencyTimestamp(std::string ×tamp, bool isRenderer) override; 137 138 float GetMaxAmplitude(bool isOutputDevice, int32_t deviceType) override; 139 140 void OnCapturerState(bool isActive, int32_t num); 141 142 void ResetAudioEndpoint() override; 143 144 bool GetEffectOffloadEnabled() override; 145 146 // IAudioServerInnerCall 147 int32_t SetSinkRenderEmpty(const std::string &devceClass, int32_t durationUs) final; 148 149 int32_t SetSinkMuteForSwitchDevice(const std::string &devceClass, int32_t durationUs, bool mute) override; 150 151 void LoadHdiEffectModel() override; 152 153 void UpdateEffectBtOffloadSupported(const bool &isSupported) override; 154 155 void SetRotationToEffect(const uint32_t rotate) override; 156 157 void UpdateSessionConnectionState(const int32_t &sessionID, const int32_t &state) override; 158 159 void SetNonInterruptMute(const uint32_t sessionId, const bool muteFlag) override; 160 161 sptr<IRemoteObject> CreateIpcOfflineStream(int32_t &errorCode) override; 162 163 int32_t GetOfflineAudioEffectChains(std::vector<std::string> &effectChains) override; 164 165 int32_t GetStandbyStatus(uint32_t sessionId, bool &isStandby, int64_t &enterStandbyTime) override; 166 protected: 167 void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 168 169 private: 170 int32_t GetAudioEnhancePropertyArray(AudioEffectPropertyArrayV3 &propertyArray, 171 const DeviceType& deviceType); 172 int32_t GetAudioEffectPropertyArray(AudioEffectPropertyArrayV3 &propertyArray); 173 int32_t SetAudioEffectChainProperty(const AudioEffectPropertyArrayV3 &propertyArray); 174 int32_t SetAudioEnhanceChainProperty(const AudioEffectPropertyArrayV3 &propertyArray, 175 const DeviceType& deviceType); 176 bool VerifyClientPermission(const std::string &permissionName, 177 Security::AccessToken::AccessTokenID tokenId = Security::AccessToken::INVALID_TOKENID); 178 bool PermissionChecker(const AudioProcessConfig &config); 179 bool CheckPlaybackPermission(const AudioProcessConfig &config); 180 bool CheckRecorderPermission(const AudioProcessConfig &config); 181 bool CheckVoiceCallRecorderPermission(Security::AccessToken::AccessTokenID tokenId); 182 183 void ResetRecordConfig(AudioProcessConfig &config); 184 AudioProcessConfig ResetProcessConfig(const AudioProcessConfig &config); 185 bool CheckStreamInfoFormat(const AudioProcessConfig &config); 186 bool CheckRendererFormat(const AudioProcessConfig &config); 187 bool CheckRecorderFormat(const AudioProcessConfig &config); 188 bool CheckConfigFormat(const AudioProcessConfig &config); 189 int32_t GetHapBuildApiVersion(int32_t callerUid); 190 191 void AudioServerDied(pid_t pid); 192 void RegisterPolicyServerDeathRecipient(); 193 void RegisterAudioCapturerSourceCallback(); 194 int32_t SetIORoutes(std::vector<std::pair<DeviceType, DeviceFlag>> &activeDevices, 195 BluetoothOffloadState a2dpOffloadFlag); 196 int32_t SetIORoutes(DeviceType type, DeviceFlag flag, std::vector<DeviceType> deviceTypes, 197 BluetoothOffloadState a2dpOffloadFlag); 198 const std::string GetDPParameter(const std::string &condition); 199 const std::string GetUsbParameter(); 200 void WriteServiceStartupError(); 201 bool IsNormalIpcStream(const AudioProcessConfig &config) const; 202 void RecognizeAudioEffectType(const std::string &mainkey, const std::string &subkey, 203 const std::string &extraSceneType); 204 int32_t SetSystemVolumeToEffect(const AudioStreamType streamType, float volume); 205 const std::string GetBundleNameFromUid(int32_t uid); 206 207 bool IsFastBlocked(int32_t uid, PlayerType playerType); 208 209 void InitMaxRendererStreamCntPerUid(); 210 int32_t CheckParam(const AudioProcessConfig &config); 211 void SendRendererCreateErrorInfo(const StreamUsage &sreamUsage, 212 const int32_t &errorCode); 213 int32_t CheckMaxRendererInstances(); 214 sptr<IRemoteObject> CreateAudioStream(const AudioProcessConfig &config, int32_t callingUid); 215 private: 216 static constexpr int32_t MEDIA_SERVICE_UID = 1013; 217 static constexpr int32_t VASSISTANT_UID = 3001; 218 static constexpr int32_t MAX_VOLUME = 15; 219 static constexpr int32_t MIN_VOLUME = 0; 220 static uint32_t paDaemonTid_; 221 static std::unordered_map<int, float> AudioStreamVolumeMap; 222 static std::map<std::string, std::string> audioParameters; 223 static std::unordered_map<std::string, std::unordered_map<std::string, std::set<std::string>>> audioParameterKeys; 224 225 pthread_t m_paDaemonThread; 226 AudioScene audioScene_ = AUDIO_SCENE_DEFAULT; 227 228 // Capturer status flags: each capturer is represented by a single bit. 229 // 0 indicates the capturer has stopped; 1 indicates the capturer has started. 230 std::atomic<uint64_t> capturerStateFlag_ = 0; 231 232 std::shared_ptr<AudioParameterCallback> audioParamCb_; 233 std::mutex onCapturerStateCbMutex_; 234 std::shared_ptr<WakeUpSourceCallback> wakeupCallback_; 235 std::mutex audioParamCbMtx_; 236 std::mutex setWakeupCloseCallbackMutex_; 237 std::mutex audioParameterMutex_; 238 std::mutex audioSceneMutex_; 239 std::unique_ptr<AudioEffectServer> audioEffectServer_; 240 bool isFastControlled_ = true; 241 int32_t maxRendererStreamCntPerUid_ = 0; 242 std::mutex streamLifeCycleMutex_ {}; 243 }; 244 } // namespace AudioStandard 245 } // namespace OHOS 246 #endif // ST_AUDIO_SERVER_H 247