1 /* 2 * Copyright (c) 2022-2025 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_STREAM_MANAGER_H 17 #define ST_AUDIO_STREAM_MANAGER_H 18 19 #include <iostream> 20 #include <map> 21 #include "audio_effect.h" 22 #include "audio_system_manager.h" 23 #include "audio_stream_change_info.h" 24 25 namespace OHOS { 26 namespace AudioStandard { 27 class DeviceChangeWithInfoCallback { 28 public: 29 virtual ~DeviceChangeWithInfoCallback() = default; 30 31 virtual void OnDeviceChangeWithInfo(const uint32_t sessionId, const AudioDeviceDescriptor &deviceInfo, 32 const AudioStreamDeviceChangeReasonExt reason) = 0; 33 34 virtual void OnRecreateStreamEvent(const uint32_t sessionId, const int32_t streamFlag, 35 const AudioStreamDeviceChangeReasonExt reason) = 0; 36 }; 37 38 class AudioFormatUnsupportedErrorCallback { 39 public: 40 virtual ~AudioFormatUnsupportedErrorCallback() = default; 41 42 /** 43 * Called when format unsupported error occurs 44 * 45 * @param errorCode ErrorCode information. 46 */ 47 virtual void OnFormatUnsupportedError(const AudioErrors &errorCode) = 0; 48 }; 49 50 class AudioClientTracker { 51 public: 52 virtual ~AudioClientTracker() = default; 53 54 /** 55 * Mute Stream was controlled by system application 56 * 57 * @param streamSetStateEventInternal Contains the set even information. 58 */ 59 virtual void MuteStreamImpl(const StreamSetStateEventInternal &streamSetStateEventInternal) = 0; 60 61 /** 62 * Unmute Stream was controlled by system application 63 * 64 * @param streamSetStateEventInternal Contains the set even information. 65 */ 66 virtual void UnmuteStreamImpl(const StreamSetStateEventInternal &streamSetStateEventInternal) = 0; 67 68 /** 69 * Paused Stream was controlled by system application 70 * 71 * @param streamSetStateEventInternal Contains the set even information. 72 */ 73 virtual void PausedStreamImpl(const StreamSetStateEventInternal &streamSetStateEventInternal) = 0; 74 75 /** 76 * Resumed Stream was controlled by system application 77 * 78 * @param streamSetStateEventInternal Contains the set even information. 79 */ 80 virtual void ResumeStreamImpl(const StreamSetStateEventInternal &streamSetStateEventInternal) = 0; 81 82 /** 83 * Set low power volume was controlled by system application 84 * 85 * @param volume volume value. 86 */ 87 virtual void SetLowPowerVolumeImpl(float volume) = 0; 88 89 /** 90 * Get low power volume was controlled by system application 91 * 92 * @param volume volume value. 93 */ 94 virtual void GetLowPowerVolumeImpl(float &volume) = 0; 95 96 /** 97 * Set Stream into a specified Offload state 98 * 99 * @param state power state. 100 * @param isAppBack app state. 101 */ 102 virtual void SetOffloadModeImpl(int32_t state, bool isAppBack) = 0; 103 104 /** 105 * Unset Stream out of Offload state 106 * 107 */ 108 virtual void UnsetOffloadModeImpl() = 0; 109 110 /** 111 * Get single stream was controlled by system application 112 * 113 * @param volume volume value. 114 */ 115 virtual void GetSingleStreamVolumeImpl(float &volume) = 0; 116 }; 117 118 class AudioStreamManager { 119 public: 120 AudioStreamManager() = default; 121 virtual ~AudioStreamManager() = default; 122 123 static AudioStreamManager *GetInstance(); 124 125 /** 126 * @brief Registers the renderer event callback listener. 127 * 128 * @param clientPid client PID 129 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 130 * defined in {@link audio_errors.h} otherwise. 131 * @since 9 132 * @deprecated since 12 133 */ 134 int32_t RegisterAudioRendererEventListener(const int32_t clientPid, 135 const std::shared_ptr<AudioRendererStateChangeCallback> &callback); 136 137 /** 138 * @brief Unregisters the renderer event callback listener. 139 * 140 * @param clientPid client PID 141 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 142 * defined in {@link audio_errors.h} otherwise. 143 * @since 9 144 * @deprecated since 12 145 */ 146 int32_t UnregisterAudioRendererEventListener(const int32_t clientPid); 147 148 /** 149 * @brief Registers the renderer event callback listener. 150 * 151 * @param callback 152 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 153 * defined in {@link audio_errors.h} otherwise. 154 * @since 12 155 */ 156 int32_t RegisterAudioRendererEventListener(const std::shared_ptr<AudioRendererStateChangeCallback> &callback); 157 158 /** 159 * @brief Unregisters the renderer event callback listener. 160 * 161 * @param callback need to unregister. 162 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 163 * defined in {@link audio_errors.h} otherwise. 164 * @since 12 165 */ 166 int32_t UnregisterAudioRendererEventListener(const std::shared_ptr<AudioRendererStateChangeCallback> &callback); 167 168 /** 169 * @brief Registers the capturer event callback listener. 170 * 171 * @param clientPid client PID 172 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 173 * defined in {@link audio_errors.h} otherwise. 174 * @since 9 175 */ 176 int32_t RegisterAudioCapturerEventListener(const int32_t clientPid, 177 const std::shared_ptr<AudioCapturerStateChangeCallback> &callback); 178 179 /** 180 * @brief Unregisters the capturer event callback listener. 181 * 182 * @param clientPid client PID 183 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 184 * defined in {@link audio_errors.h} otherwise. 185 * @since 9 186 */ 187 int32_t UnregisterAudioCapturerEventListener(const int32_t clientPid); 188 189 /** 190 * @brief Get current renderer change Infos. 191 * 192 * @param audioRendererChangeInfos audioRendererChangeInfos 193 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 194 * defined in {@link audio_errors.h} otherwise. 195 * @since 9 196 */ 197 int32_t GetCurrentRendererChangeInfos( 198 std::vector<std::shared_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos); 199 200 /** 201 * @brief Get current capturer change Infos. 202 * 203 * @param audioRendererChangeInfos audioRendererChangeInfos 204 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 205 * defined in {@link audio_errors.h} otherwise. 206 * @since 9 207 */ 208 int32_t GetCurrentCapturerChangeInfos( 209 std::vector<std::shared_ptr<AudioCapturerChangeInfo>> &audioCapturerChangeInfos); 210 211 /** 212 * @brief Get Audio Effect Infos. 213 * 214 * @param AudioSceneEffectInfo AudioSceneEffectInfo 215 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 216 * defined in {@link audio_errors.h} otherwise. 217 * @since 9 218 */ 219 int32_t GetEffectInfoArray(AudioSceneEffectInfo &audioSceneEffectInfo, StreamUsage streamUsage); 220 221 /** 222 * @brief Get Audio render Effect param. 223 * 224 * @param AudioSceneEffectInfo AudioSceneEffectInfo 225 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 226 * defined in {@link audio_errors.h} otherwise. 227 * @since 13 228 */ 229 int32_t GetSupportedAudioEffectProperty(AudioEffectPropertyArray &propertyArray); 230 231 /** 232 * @brief Get Audio Capture Effect param. 233 * 234 * @param AudioSceneEffectInfo AudioSceneEffectInfo 235 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 236 * defined in {@link audio_errors.h} otherwise. 237 * @since 13 238 */ 239 int32_t GetSupportedAudioEnhanceProperty(AudioEnhancePropertyArray &propertyArray); 240 241 /** 242 * @brief Sets the audio effect Param. 243 * 244 * * @param effectParam The audio effect Param at which the stream needs to be rendered. 245 * @return Returns {@link SUCCESS} if audio effect Param is successfully set; returns an error code 246 * defined in {@link audio_errors.h} otherwise. 247 * @since 13 248 */ 249 int32_t SetAudioEffectProperty(const AudioEffectPropertyArray &propertyArray); 250 251 /** 252 * @brief Gets the audio effect Param. 253 * 254 * * @param effectParam The audio effect moParamde at which the stream needs to be rendered. 255 * @return Returns {@link SUCCESS} if audio effect Param is successfully set; returns an error code 256 * defined in {@link audio_errors.h} otherwise. 257 * @since 13 258 */ 259 int32_t GetAudioEffectProperty(AudioEffectPropertyArray &propertyArray); 260 261 /** 262 * @brief Sets the audio effect Param. 263 * 264 * * @param effectParam The audio effect Param at which the stream needs to be rendered. 265 * @return Returns {@link SUCCESS} if audio effect Param is successfully set; returns an error code 266 * defined in {@link audio_errors.h} otherwise. 267 * @since 13 268 */ 269 int32_t SetAudioEnhanceProperty(const AudioEnhancePropertyArray &propertyArray); 270 271 /** 272 * @brief Gets the audio effect Param. 273 * 274 * * @param effectParam The audio effect moParamde at which the stream needs to be rendered. 275 * @return Returns {@link SUCCESS} if audio effect Param is successfully set; returns an error code 276 * defined in {@link audio_errors.h} otherwise. 277 * @since 13 278 */ 279 int32_t GetAudioEnhanceProperty(AudioEnhancePropertyArray &propertyArray); 280 281 /** 282 * @brief Is stream active. 283 * 284 * @param volumeType audio volume type. 285 * @return Returns <b>true</b> if the rendering is successfully started; returns <b>false</b> otherwise. 286 * @since 9 287 */ 288 bool IsStreamActive(AudioVolumeType volumeType) const; 289 290 /** 291 * @brief Is stream active. 292 * 293 * @param streamUsage stream usage. 294 * @return Returns <b>true</b> if the stream is active; returns <b>false</b> otherwise. 295 * @since 20 296 */ 297 bool IsStreamActiveByStreamUsage(StreamUsage streamUsage) const; 298 299 /** 300 * @brief Is fast playback supported. 301 * 302 * @param streamInfo audio stream info. 303 * @param usage StreamUsage. 304 * @return Returns <b>true</b> if the stream is support fast playback; returns <b>false</b> otherwise. 305 * @since 20 306 */ 307 bool IsFastPlaybackSupported(AudioStreamInfo &streamInfo, StreamUsage usage); 308 309 /** 310 * @brief Is fast recording supported. 311 * 312 * @param streamInfo audio stream info. 313 * @param source SourceType. 314 * @return Returns <b>true</b> if the stream is support fast recording; returns <b>false</b> otherwise. 315 * @since 20 316 */ 317 bool IsFastRecordingSupported(AudioStreamInfo &streamInfo, SourceType source); 318 319 /** 320 * @brief Gets sampling rate for hardware output. 321 * 322 * @param AudioDeviceDescriptor Target output device. 323 * @return The sampling rate for output. 324 * @since 11 325 */ 326 int32_t GetHardwareOutputSamplingRate(std::shared_ptr<AudioDeviceDescriptor> &desc); 327 328 /** 329 * @brief Judges whether the playback is supported by the renderer. 330 * 331 * @param streamInfo AudioStreamInfo 332 * @param streamUsage StreamUsage 333 * @return Returns direct playback mode. 334 * @since 19 335 */ 336 DirectPlaybackMode GetDirectPlaybackSupport(const AudioStreamInfo &streamInfo, const StreamUsage &streamUsage); 337 338 /** 339 * @brief Sets format unsupported error callback. 340 * 341 * @param callback The format unsupported error callback. 342 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 343 * defined in {@link audio_errors.h} otherwise. 344 * @since 19 345 */ 346 int32_t SetAudioFormatUnsupportedErrorCallback( 347 const std::shared_ptr<AudioFormatUnsupportedErrorCallback> &callback); 348 349 /** 350 * @brief Unsets format unsupported error callback. 351 * 352 * @return Returns {@link SUCCESS} if callback unregistration is successful; returns an error code 353 * defined in {@link audio_errors.h} otherwise. 354 * @since 19 355 */ 356 int32_t UnsetAudioFormatUnsupportedErrorCallback(); 357 358 /** 359 * Query whether Acoustic Echo Canceler is supported on input SourceType. 360 * @param { SourceType } sourceType - Audio source type. 361 * @returns { bool } Promise used to return the support status of Acoustic Echo Canceler. 362 * The value true means that Acoustic Echo Canceler is supported, and false means the opposite. 363 * @since 20 364 */ 365 bool IsAcousticEchoCancelerSupported(SourceType sourceType); 366 367 /** 368 * @brief Force Stop the audio stream. 369 * 370 * @return Returns {@link SUCCESS} if the operation is successful; returns an error code 371 * defined in {@link audio_errors.h} otherwise. 372 * @since 20 373 */ 374 int32_t ForceStopAudioStream(StopAudioType audioType); 375 376 /** 377 * Checks whether it is possible to obtain the recording focus. 378 * @param { AudioCapturerInfo } AudioCapturerInfo. 379 * @returns { bool } Indicating whether obtaining the recording focus is possible. 380 * 381 * @since 20 382 */ 383 bool IsCapturerFocusAvailable(const AudioCapturerInfo &capturerInfo); 384 385 /** 386 * Checks whether the audio loopback is supported. 387 * @param { AudioLoopbackMode } mode - The audio loopback mode. 388 * @returns { bool } The value true means that the audio loopback is supported, 389 * and false means the opposite. 390 * @since 20 391 */ 392 bool IsAudioLoopbackSupported(AudioLoopbackMode mode); 393 private: 394 std::mutex rendererStateChangeCallbacksMutex_; 395 std::vector<std::shared_ptr<AudioRendererStateChangeCallback>> rendererStateChangeCallbacks_; 396 }; 397 } // namespace AudioStandard 398 } // namespace OHOS 399 #endif // ST_AUDIO_STREAM_MANAGER_H 400