1 /* 2 * Copyright (c) 2021-2022 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_SERVER_H 17 #define ST_AUDIO_POLICY_SERVER_H 18 19 #include <mutex> 20 #include <pthread.h> 21 22 #include "audio_interrupt_callback.h" 23 #include "audio_policy_manager_stub.h" 24 #include "audio_policy_service.h" 25 #include "audio_server_death_recipient.h" 26 #include "audio_session_callback.h" 27 #include "i_audio_volume_key_event_callback.h" 28 #include "iremote_stub.h" 29 #include "system_ability.h" 30 #include "audio_service_dump.h" 31 #include "audio_info.h" 32 #include "accesstoken_kit.h" 33 #include "perm_state_change_callback_customize.h" 34 #include "bundle_mgr_interface.h" 35 #include "system_ability_definition.h" 36 #include "iservice_registry.h" 37 #include "ipc_skeleton.h" 38 #include "bundle_mgr_proxy.h" 39 40 namespace OHOS { 41 namespace AudioStandard { 42 class AudioPolicyServer : public SystemAbility, public AudioPolicyManagerStub, public AudioSessionCallback { 43 DECLARE_SYSTEM_ABILITY(AudioPolicyServer); 44 public: 45 DISALLOW_COPY_AND_MOVE(AudioPolicyServer); 46 47 enum DeathRecipientId { 48 TRACKER_CLIENT = 0, 49 LISTENER_CLIENT 50 }; 51 52 explicit AudioPolicyServer(int32_t systemAbilityId, bool runOnCreate = true); 53 54 virtual ~AudioPolicyServer() = default; 55 56 void OnDump() override; 57 void OnStart() override; 58 void OnStop() override; 59 60 int32_t SetStreamVolume(AudioStreamType streamType, float volume) override; 61 62 float GetStreamVolume(AudioStreamType streamType) override; 63 64 int32_t SetLowPowerVolume(int32_t streamId, float volume) override; 65 66 float GetLowPowerVolume(int32_t streamId) override; 67 68 float GetSingleStreamVolume(int32_t streamId) override; 69 70 int32_t SetStreamMute(AudioStreamType streamType, bool mute) override; 71 72 bool GetStreamMute(AudioStreamType streamType) override; 73 74 bool IsStreamActive(AudioStreamType streamType) override; 75 76 int32_t SelectOutputDevice(sptr<AudioRendererFilter> audioRendererFilter, 77 std::vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors) override; 78 79 std::string GetSelectedDeviceInfo(int32_t uid, int32_t pid, AudioStreamType streamType) override; 80 81 int32_t SelectInputDevice(sptr<AudioCapturerFilter> audioCapturerFilter, 82 std::vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors) override; 83 84 std::vector<sptr<AudioDeviceDescriptor>> GetDevices(DeviceFlag deviceFlag) override; 85 86 int32_t SetDeviceActive(InternalDeviceType deviceType, bool active) override; 87 88 bool IsDeviceActive(InternalDeviceType deviceType) override; 89 90 InternalDeviceType GetActiveOutputDevice() override; 91 92 InternalDeviceType GetActiveInputDevice() override; 93 94 int32_t SetRingerMode(AudioRingerMode ringMode) override; 95 96 std::vector<int32_t> GetSupportedTones() override; 97 98 std::shared_ptr<ToneInfo> GetToneConfig(int32_t ltonetype) override; 99 100 AudioRingerMode GetRingerMode() override; 101 102 int32_t SetAudioScene(AudioScene audioScene) override; 103 104 int32_t SetMicrophoneMuteCommon(bool isMute, API_VERSION api_v); 105 106 int32_t SetMicrophoneMute(bool isMute) override; 107 108 int32_t SetMicrophoneMuteAudioConfig(bool isMute) override; 109 110 bool IsMicrophoneMute(API_VERSION api_v) override; 111 112 AudioScene GetAudioScene() override; 113 114 int32_t SetRingerModeCallback(const int32_t clientId, const sptr<IRemoteObject> &object) override; 115 116 int32_t UnsetRingerModeCallback(const int32_t clientId) override; 117 118 int32_t SetMicStateChangeCallback(const int32_t clientId, const sptr<IRemoteObject> &object) override; 119 120 int32_t SetDeviceChangeCallback(const int32_t clientId, const DeviceFlag flag, const sptr<IRemoteObject> &object) 121 override; 122 123 int32_t UnsetDeviceChangeCallback(const int32_t clientId) override; 124 125 int32_t SetAudioInterruptCallback(const uint32_t sessionID, const sptr<IRemoteObject> &object) override; 126 127 int32_t UnsetAudioInterruptCallback(const uint32_t sessionID) override; 128 129 int32_t ActivateAudioInterrupt(const AudioInterrupt &audioInterrupt) override; 130 131 int32_t DeactivateAudioInterrupt(const AudioInterrupt &audioInterrupt) override; 132 133 int32_t SetAudioManagerInterruptCallback(const uint32_t clientID, const sptr<IRemoteObject> &object) override; 134 135 int32_t UnsetAudioManagerInterruptCallback(const uint32_t clientID) override; 136 137 int32_t RequestAudioFocus(const uint32_t clientID, const AudioInterrupt &audioInterrupt) override; 138 139 int32_t AbandonAudioFocus(const uint32_t clientID, const AudioInterrupt &audioInterrupt) override; 140 141 AudioStreamType GetStreamInFocus() override; 142 143 int32_t GetSessionInfoInFocus(AudioInterrupt &audioInterrupt) override; 144 145 int32_t SetVolumeKeyEventCallback(const int32_t clientPid, const sptr<IRemoteObject> &object) override; 146 147 int32_t UnsetVolumeKeyEventCallback(const int32_t clientPid) override; 148 149 void OnSessionRemoved(const uint32_t sessionID) override; 150 151 int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override; 152 153 bool VerifyClientPermission(const std::string &permission, uint32_t appTokenId = 0, int32_t appUid = INVALID_UID, 154 bool privacyFlag = false, AudioPermissionState state = AUDIO_PERMISSION_START) override; 155 156 bool getUsingPemissionFromPrivacy(const std::string &permissionName, uint32_t appTokenId, 157 AudioPermissionState state = AUDIO_PERMISSION_START) override; 158 159 int32_t ReconfigureAudioChannel(const uint32_t &count, DeviceType deviceType) override; 160 161 int32_t GetAudioLatencyFromXml() override; 162 163 uint32_t GetSinkLatencyFromXml() override; 164 165 int32_t RegisterAudioRendererEventListener(int32_t clientUID, const sptr<IRemoteObject> &object) override; 166 167 int32_t UnregisterAudioRendererEventListener(int32_t clientUID) override; 168 169 int32_t RegisterAudioCapturerEventListener(int32_t clientUID, const sptr<IRemoteObject> &object) override; 170 171 int32_t UnregisterAudioCapturerEventListener(int32_t clientUID) override; 172 173 int32_t RegisterTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo, 174 const sptr<IRemoteObject> &object) override; 175 176 int32_t UpdateTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo) override; 177 178 int32_t GetCurrentRendererChangeInfos( 179 std::vector<std::unique_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos) override; 180 181 int32_t GetCurrentCapturerChangeInfos( 182 std::vector<std::unique_ptr<AudioCapturerChangeInfo>> &audioCapturerChangeInfos) override; 183 184 void RegisterClientDeathRecipient(const sptr<IRemoteObject> &object, DeathRecipientId id); 185 186 void RegisteredTrackerClientDied(int pid); 187 188 void RegisteredStreamListenerClientDied(int pid); 189 190 bool IsAudioRendererLowLatencySupported(const AudioStreamInfo &audioStreamInfo) override; 191 192 int32_t UpdateStreamState(const int32_t clientUid, StreamSetState streamSetState, 193 AudioStreamType audioStreamType) override; 194 195 std::vector<sptr<VolumeGroupInfo>> GetVolumeGroupInfos() override; 196 197 std::vector<sptr<AudioDeviceDescriptor>> GetActiveOutputDeviceDescriptors() override; 198 199 class RemoteParameterCallback : public AudioParameterCallback { 200 public: 201 RemoteParameterCallback(sptr<AudioPolicyServer> server); 202 // AudioParameterCallback 203 void OnAudioParameterChange(const std::string networkId, const AudioParamKey key, const std::string& condition, 204 const std::string& value) override; 205 private: 206 sptr<AudioPolicyServer> server_; 207 void VolumeOnChange(const std::string networkId, const std::string& condition); 208 void InterruptOnChange(const std::string networkId, const std::string& condition); 209 void StateOnChange(const std::string networkId, const std::string& condition, const std::string& value); 210 }; 211 std::shared_ptr<RemoteParameterCallback> remoteParameterCallback_; 212 213 class PerStateChangeCbCustomizeCallback : public Security::AccessToken::PermStateChangeCallbackCustomize { 214 public: PerStateChangeCbCustomizeCallback(const Security::AccessToken::PermStateChangeScope & scopeInfo,sptr<AudioPolicyServer> server)215 explicit PerStateChangeCbCustomizeCallback(const Security::AccessToken::PermStateChangeScope &scopeInfo, 216 sptr<AudioPolicyServer> server) : PermStateChangeCallbackCustomize(scopeInfo), 217 ready_(false), server_(server) {} ~PerStateChangeCbCustomizeCallback()218 ~PerStateChangeCbCustomizeCallback() {} 219 220 void PermStateChangeCallback(Security::AccessToken::PermStateChangeInfo& result); 221 int32_t getUidByBundleName(std::string bundle_name, int user_id); 222 223 bool ready_; 224 private: 225 sptr<AudioPolicyServer> server_; 226 }; 227 228 protected: 229 void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 230 231 void RegisterParamCallback(); 232 233 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 234 private: 235 void PrintOwnersLists(); 236 int32_t ProcessFocusEntry(const AudioInterrupt &incomingInterrupt); 237 bool ProcessCurActiveInterrupt(std::list<AudioInterrupt>::iterator &iterActive, const AudioInterrupt &incoming); 238 bool ProcessPendingInterrupt(std::list<AudioInterrupt>::iterator &iterPending, const AudioInterrupt &incoming); 239 void AddToCurActiveList(const AudioInterrupt &audioInterrupt); 240 void UnduckCurActiveList(const AudioInterrupt &exitingInterrupt); 241 void ResumeUnduckPendingList(const AudioInterrupt &exitingInterrupt); 242 void NotifyFocusGranted(const uint32_t clientID, const AudioInterrupt &audioInterrupt); 243 int32_t NotifyFocusAbandoned(const uint32_t clientID, const AudioInterrupt &audioInterrupt); 244 int32_t SetStreamVolume(AudioStreamType streamType, float volume, bool isUpdateUi); 245 void GetPolicyData(PolicyData &policyData); 246 void GetDeviceInfo(PolicyData &policyData); 247 void GetGroupInfo(PolicyData &policyData); 248 void SubscribeKeyEvents(); 249 void InitKVStore(); 250 void ConnectServiceAdapter(); 251 void RegisterBluetoothListener(); 252 void SubscribeAccessibilityConfigObserver(); 253 254 static float MapVolumeToHDI(int32_t volume); 255 static int32_t ConvertVolumeToInt(float volume); 256 257 AudioPolicyService& mPolicyService; 258 std::unordered_map<int32_t, std::shared_ptr<VolumeKeyEventCallback>> volumeChangeCbsMap_; 259 std::mutex ringerModeMutex_; 260 std::mutex interruptMutex_; 261 std::mutex volumeKeyEventMutex_; 262 std::mutex micStateChangeMutex_; 263 uint32_t clientOnFocus_; 264 std::unique_ptr<AudioInterrupt> focussedAudioInterruptInfo_; 265 266 std::unordered_map<uint32_t, std::shared_ptr<AudioInterruptCallback>> policyListenerCbsMap_; 267 std::unordered_map<uint32_t, std::shared_ptr<AudioInterruptCallback>> audioManagerListenerCbsMap_; 268 std::list<AudioInterrupt> curActiveOwnersList_; 269 std::list<AudioInterrupt> pendingOwnersList_; 270 std::unordered_map<AudioStreamType, int32_t> interruptPriorityMap_; 271 std::unordered_map<int32_t, std::shared_ptr<AudioRingerModeCallback>> ringerModeListenerCbsMap_; 272 std::unordered_map<int32_t, std::shared_ptr<AudioManagerMicStateChangeCallback>> micStateChangeListenerCbsMap_; 273 std::vector<pid_t> clientDiedListenerState_; 274 static constexpr int32_t MAX_VOLUME_LEVEL = 15; 275 static constexpr int32_t MIN_VOLUME_LEVEL = 0; 276 static constexpr int32_t CONST_FACTOR = 100; 277 static constexpr int32_t VOLUME_CHANGE_FACTOR = 1; 278 static constexpr int32_t FIRST_PRIORITY = 1; 279 static constexpr int32_t SECOND_PRIORITY = 2; 280 static constexpr int32_t THIRD_PRIORITY = 3; 281 static constexpr int32_t VOLUME_KEY_DURATION = 0; 282 static constexpr int32_t MEDIA_SERVICE_UID = 1013; 283 }; 284 } // namespace AudioStandard 285 } // namespace OHOS 286 #endif // ST_AUDIO_POLICY_SERVER_H 287