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_SYSTEM_MANAGER_H 17 #define ST_AUDIO_SYSTEM_MANAGER_H 18 19 #include <cstdlib> 20 #include <map> 21 22 #include "parcel.h" 23 #include "audio_info.h" 24 #include "audio_interrupt_callback.h" 25 26 namespace OHOS { 27 namespace AudioStandard { 28 class AudioDeviceDescriptor; 29 class AudioDeviceDescriptor : public Parcelable { 30 friend class AudioSystemManager; 31 public: 32 DeviceType getType(); 33 DeviceRole getRole(); 34 DeviceType deviceType_; 35 DeviceRole deviceRole_; 36 AudioDeviceDescriptor(); 37 AudioDeviceDescriptor(DeviceType type, DeviceRole role); 38 virtual ~AudioDeviceDescriptor(); 39 bool Marshalling(Parcel &parcel) const override; 40 static sptr<AudioDeviceDescriptor> Unmarshalling(Parcel &parcel); 41 }; 42 43 struct DeviceChangeAction { 44 DeviceChangeType type; 45 std::vector<sptr<AudioDeviceDescriptor>> deviceDescriptors; 46 }; 47 48 // AudioManagerCallback OnInterrupt is added to handle compilation error in call manager 49 // Once call manager adapt to new interrupt APIs, this will be rmeoved 50 class AudioManagerCallback { 51 public: 52 virtual ~AudioManagerCallback() = default; 53 /** 54 * Called when an interrupt is received. 55 * 56 * @param interruptAction Indicates the InterruptAction information needed by client. 57 * For details, refer InterruptAction struct in audio_info.h 58 */ 59 virtual void OnInterrupt(const InterruptAction &interruptAction) = 0; 60 }; 61 62 class AudioManagerInterruptCallbackImpl : public AudioInterruptCallback { 63 public: 64 explicit AudioManagerInterruptCallbackImpl(); 65 virtual ~AudioManagerInterruptCallbackImpl(); 66 67 void OnInterrupt(const InterruptEventInternal &interruptEvent) override; 68 void SaveCallback(const std::weak_ptr<AudioManagerCallback> &callback); 69 private: 70 std::weak_ptr<AudioManagerCallback> callback_; 71 std::shared_ptr<AudioManagerCallback> cb_; 72 }; 73 74 class AudioManagerDeviceChangeCallback { 75 public: 76 virtual ~AudioManagerDeviceChangeCallback() = default; 77 /** 78 * Called when an interrupt is received. 79 * 80 * @param deviceChangeAction Indicates the DeviceChangeAction information needed by client. 81 * For details, refer DeviceChangeAction struct 82 */ 83 virtual void OnDeviceChange(const DeviceChangeAction &deviceChangeAction) = 0; 84 }; 85 86 class VolumeKeyEventCallback { 87 public: 88 virtual ~VolumeKeyEventCallback() = default; 89 /** 90 * @brief VolumeKeyEventCallback will be executed when hard volume key is pressed up/down 91 * 92 * @param streamType the stream type for which volume must be updated. 93 * @param volumeLevel the volume level to be updated. 94 * @param isUpdateUi whether to update volume level in UI. 95 **/ 96 virtual void OnVolumeKeyEvent(AudioStreamType streamType, int32_t volumeLevel, bool isUpdateUi) = 0; 97 }; 98 99 class AudioRingerModeCallback { 100 public: 101 virtual ~AudioRingerModeCallback() = default; 102 /** 103 * Called when ringer mode is updated. 104 * 105 * @param ringerMode Indicates the updated ringer mode value. 106 * For details, refer RingerMode enum in audio_info.h 107 */ 108 virtual void OnRingerModeUpdated(const AudioRingerMode &ringerMode) = 0; 109 }; 110 111 /** 112 * @brief The AudioSystemManager class is an abstract definition of audio manager. 113 * Provides a series of client/interfaces for audio management 114 */ 115 116 class AudioSystemManager { 117 public: 118 enum AudioVolumeType { 119 /** 120 * Indicates audio streams default. 121 */ 122 STREAM_DEFAULT = -1, 123 /** 124 * Indicates audio streams of voices in calls. 125 */ 126 STREAM_VOICE_CALL = 0, 127 /** 128 * Indicates audio streams for music playback. 129 */ 130 STREAM_MUSIC = 1, 131 /** 132 * Indicates audio streams for ringtones. 133 */ 134 STREAM_RING = 2, 135 /** 136 * Indicates audio streams media. 137 */ 138 STREAM_MEDIA = 3, 139 /** 140 * Indicates Audio streams for voice assistant 141 */ 142 STREAM_VOICE_ASSISTANT = 4, 143 /** 144 * Indicates audio streams for system sounds. 145 */ 146 STREAM_SYSTEM = 5, 147 /** 148 * Indicates audio streams for alarms. 149 */ 150 STREAM_ALARM = 6, 151 /** 152 * Indicates audio streams for notifications. 153 */ 154 STREAM_NOTIFICATION = 7, 155 /** 156 * Indicates audio streams for voice calls routed through a connected Bluetooth device. 157 */ 158 STREAM_BLUETOOTH_SCO = 8, 159 /** 160 * Indicates audio streams for enforced audible. 161 */ 162 STREAM_ENFORCED_AUDIBLE = 9, 163 /** 164 * Indicates audio streams for dual-tone multi-frequency (DTMF) tones. 165 */ 166 STREAM_DTMF = 10, 167 /** 168 * Indicates audio streams exclusively transmitted through the speaker (text-to-speech) of a device. 169 */ 170 STREAM_TTS = 11, 171 /** 172 * Indicates audio streams used for prompts in terms of accessibility. 173 */ 174 STREAM_ACCESSIBILITY = 12 175 }; 176 177 static AudioSystemManager *GetInstance(); 178 static float MapVolumeToHDI(int32_t volume); 179 static int32_t MapVolumeFromHDI(float volume); 180 static AudioStreamType GetStreamType(ContentType contentType, StreamUsage streamUsage); 181 int32_t SetVolume(AudioSystemManager::AudioVolumeType volumeType, int32_t volume) const; 182 int32_t GetVolume(AudioSystemManager::AudioVolumeType volumeType) const; 183 int32_t GetMaxVolume(AudioSystemManager::AudioVolumeType volumeType) const; 184 int32_t GetMinVolume(AudioSystemManager::AudioVolumeType volumeType) const; 185 int32_t SetMute(AudioSystemManager::AudioVolumeType volumeType, bool mute) const; 186 bool IsStreamMute(AudioSystemManager::AudioVolumeType volumeType) const; 187 int32_t SetMicrophoneMute(bool isMute) const; 188 bool IsMicrophoneMute(void) const; 189 std::vector<sptr<AudioDeviceDescriptor>> GetDevices(DeviceFlag deviceFlag); 190 const std::string GetAudioParameter(const std::string key) const; 191 void SetAudioParameter(const std::string &key, const std::string &value) const; 192 const char *RetrieveCookie(int32_t &size) const; 193 int32_t SetDeviceActive(ActiveDeviceType deviceType, bool flag) const; 194 bool IsDeviceActive(ActiveDeviceType deviceType) const; 195 bool IsStreamActive(AudioSystemManager::AudioVolumeType volumeType) const; 196 bool SetRingerMode(AudioRingerMode ringMode) const; 197 AudioRingerMode GetRingerMode() const; 198 int32_t SetAudioScene(const AudioScene &scene); 199 AudioScene GetAudioScene() const; 200 int32_t SetDeviceChangeCallback(const std::shared_ptr<AudioManagerDeviceChangeCallback> &callback); 201 int32_t UnsetDeviceChangeCallback(); 202 int32_t SetRingerModeCallback(const int32_t clientId, 203 const std::shared_ptr<AudioRingerModeCallback> &callback); 204 int32_t UnsetRingerModeCallback(const int32_t clientId) const; 205 int32_t RegisterVolumeKeyEventCallback(const int32_t clientPid, 206 const std::shared_ptr<VolumeKeyEventCallback> &callback); 207 int32_t UnregisterVolumeKeyEventCallback(const int32_t clientPid); 208 209 // Below APIs are added to handle compilation error in call manager 210 // Once call manager adapt to new interrupt APIs, this will be rmeoved 211 int32_t SetAudioManagerCallback(const AudioSystemManager::AudioVolumeType streamType, 212 const std::shared_ptr<AudioManagerCallback> &callback); 213 int32_t UnsetAudioManagerCallback(const AudioSystemManager::AudioVolumeType streamType) const; 214 int32_t ActivateAudioInterrupt(const AudioInterrupt &audioInterrupt); 215 int32_t DeactivateAudioInterrupt(const AudioInterrupt &audioInterrupt) const; 216 int32_t SetAudioManagerInterruptCallback(const std::shared_ptr<AudioManagerCallback> &callback); 217 int32_t UnsetAudioManagerInterruptCallback(); 218 int32_t RequestAudioFocus(const AudioInterrupt &audioInterrupt); 219 int32_t AbandonAudioFocus(const AudioInterrupt &audioInterrupt); 220 private: 221 AudioSystemManager(); 222 virtual ~AudioSystemManager(); 223 void init(); 224 static constexpr int32_t MAX_VOLUME_LEVEL = 15; 225 static constexpr int32_t MIN_VOLUME_LEVEL = 0; 226 static constexpr int32_t CONST_FACTOR = 100; 227 static const std::map<std::pair<ContentType, StreamUsage>, AudioStreamType> streamTypeMap_; 228 static std::map<std::pair<ContentType, StreamUsage>, AudioStreamType> CreateStreamMap(); 229 230 int32_t cbClientId_ = -1; 231 int32_t volumeChangeClientPid_ = -1; 232 std::shared_ptr<AudioManagerDeviceChangeCallback> deviceChangeCallback_ = nullptr; 233 std::shared_ptr<AudioInterruptCallback> audioInterruptCallback_ = nullptr; 234 235 uint32_t GetCallingPid(); 236 }; 237 } // namespace AudioStandard 238 } // namespace OHOS 239 #endif // ST_AUDIO_SYSTEM_MANAGER_H 240