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 <list> 21 #include <map> 22 #include <mutex> 23 #include <vector> 24 #include <unordered_map> 25 26 #include "parcel.h" 27 #include "audio_info.h" 28 #include "audio_interrupt_callback.h" 29 #include "audio_group_manager.h" 30 #include "audio_routing_manager.h" 31 32 namespace OHOS { 33 namespace AudioStandard { 34 35 class AudioDeviceDescriptor; 36 class AudioDeviceDescriptor : public Parcelable { 37 friend class AudioSystemManager; 38 public: 39 DeviceType getType(); 40 DeviceRole getRole(); 41 DeviceType deviceType_; 42 DeviceRole deviceRole_; 43 int32_t deviceId_; 44 int32_t channelMasks_; 45 int32_t channelIndexMasks_; 46 std::string deviceName_; 47 std::string macAddress_; 48 int32_t interruptGroupId_; 49 int32_t volumeGroupId_; 50 std::string networkId_; 51 std::string displayName_; 52 bool exceptionFlag_ = false; 53 DeviceStreamInfo audioStreamInfo_ = {}; 54 DeviceCategory deviceCategory_; 55 int64_t connectTimeStamp_; 56 std::shared_ptr<AudioDeviceDescriptor> pairDeviceDescriptor_; 57 ConnectState connectState_; 58 bool isScoRealConnected_ = false; 59 bool isEnable_ = true; 60 61 AudioDeviceDescriptor(); 62 AudioDeviceDescriptor(DeviceType type, DeviceRole role, int32_t interruptGroupId, int32_t volumeGroupId, 63 std::string networkId); 64 AudioDeviceDescriptor(DeviceType type, DeviceRole role); 65 AudioDeviceDescriptor(const AudioDeviceDescriptor &deviceDescriptor); 66 AudioDeviceDescriptor(const sptr<AudioDeviceDescriptor> &deviceDescriptor); 67 virtual ~AudioDeviceDescriptor(); 68 69 bool Marshalling(Parcel &parcel) const override; 70 static sptr<AudioDeviceDescriptor> Unmarshalling(Parcel &parcel); 71 void SetDeviceInfo(std::string deviceName, std::string macAddress); 72 void SetDeviceCapability(const DeviceStreamInfo &audioStreamInfo, int32_t channelMask, 73 int32_t channelIndexMasks = 0); 74 }; 75 76 struct DistributedRoutingInfo { 77 sptr<AudioDeviceDescriptor> descriptor; 78 CastType type; 79 }; 80 81 class InterruptGroupInfo; 82 class InterruptGroupInfo : public Parcelable { 83 friend class AudioSystemManager; 84 public: 85 int32_t interruptGroupId_; 86 int32_t mappingId_; 87 std::string groupName_; 88 std::string networkId_; 89 ConnectType connectType_; 90 InterruptGroupInfo(); 91 InterruptGroupInfo(int32_t interruptGroupId, int32_t mappingId, std::string groupName, std::string networkId, 92 ConnectType type); 93 virtual ~InterruptGroupInfo(); 94 bool Marshalling(Parcel &parcel) const override; 95 static sptr<InterruptGroupInfo> Unmarshalling(Parcel &parcel); 96 }; 97 98 class VolumeGroupInfo; 99 class VolumeGroupInfo : public Parcelable { 100 friend class AudioSystemManager; 101 public: 102 int32_t volumeGroupId_; 103 int32_t mappingId_; 104 std::string groupName_; 105 std::string networkId_; 106 ConnectType connectType_; 107 108 /** 109 * @brief Volume group info. 110 * 111 * @since 9 112 */ 113 VolumeGroupInfo(); 114 115 /** 116 * @brief Volume group info. 117 * 118 * @param volumeGroupId volumeGroupId 119 * @param mappingId mappingId 120 * @param groupName groupName 121 * @param networkId networkId 122 * @param type type 123 * @since 9 124 */ 125 VolumeGroupInfo(int32_t volumeGroupId, int32_t mappingId, std::string groupName, std::string networkId, 126 ConnectType type); 127 virtual ~VolumeGroupInfo(); 128 129 /** 130 * @brief Marshall. 131 * 132 * @since 8 133 * @return bool 134 */ 135 bool Marshalling(Parcel &parcel) const override; 136 137 /** 138 * @brief Unmarshall. 139 * 140 * @since 8 141 * @return Returns volume group info 142 */ 143 static sptr<VolumeGroupInfo> Unmarshalling(Parcel &parcel); 144 }; 145 146 /** 147 * Describes the device change type and device information. 148 * 149 * @since 7 150 */ 151 struct DeviceChangeAction { 152 DeviceChangeType type; 153 DeviceFlag flag; 154 std::vector<sptr<AudioDeviceDescriptor>> deviceDescriptors; 155 }; 156 157 /** 158 * @brief AudioRendererFilter is used for select speficed AudioRenderer. 159 */ 160 class AudioRendererFilter; 161 class AudioRendererFilter : public Parcelable { 162 friend class AudioSystemManager; 163 public: 164 AudioRendererFilter(); 165 virtual ~AudioRendererFilter(); 166 167 int32_t uid = -1; 168 AudioRendererInfo rendererInfo = {}; 169 AudioStreamType streamType = AudioStreamType::STREAM_DEFAULT; 170 int32_t streamId = -1; 171 172 bool Marshalling(Parcel &parcel) const override; 173 static sptr<AudioRendererFilter> Unmarshalling(Parcel &in); 174 }; 175 176 /** 177 * @brief AudioCapturerFilter is used for select speficed audiocapturer. 178 */ 179 class AudioCapturerFilter; 180 class AudioCapturerFilter : public Parcelable { 181 friend class AudioSystemManager; 182 public: 183 AudioCapturerFilter(); 184 virtual ~AudioCapturerFilter(); 185 186 int32_t uid = -1; 187 AudioCapturerInfo capturerInfo = {SOURCE_TYPE_INVALID, 0}; 188 189 bool Marshalling(Parcel &parcel) const override; 190 static sptr<AudioCapturerFilter> Unmarshalling(Parcel &in); 191 }; 192 193 // AudioManagerCallback OnInterrupt is added to handle compilation error in call manager 194 // Once call manager adapt to new interrupt APIs, this will be removed 195 class AudioManagerCallback { 196 public: 197 virtual ~AudioManagerCallback() = default; 198 /** 199 * Called when an interrupt is received. 200 * 201 * @param interruptAction Indicates the InterruptAction information needed by client. 202 * For details, refer InterruptAction struct in audio_info.h 203 */ 204 virtual void OnInterrupt(const InterruptAction &interruptAction) = 0; 205 std::mutex cbMutex_; 206 }; 207 208 class AudioManagerInterruptCallbackImpl : public AudioInterruptCallback { 209 public: 210 explicit AudioManagerInterruptCallbackImpl(); 211 virtual ~AudioManagerInterruptCallbackImpl(); 212 213 /** 214 * Called when an interrupt is received. 215 * 216 * @param interruptAction Indicates the InterruptAction information needed by client. 217 * For details, refer InterruptAction struct in audio_info.h 218 * @since 7 219 */ 220 void OnInterrupt(const InterruptEventInternal &interruptEvent) override; 221 void SaveCallback(const std::weak_ptr<AudioManagerCallback> &callback); 222 private: 223 std::weak_ptr<AudioManagerCallback> callback_; 224 std::shared_ptr<AudioManagerCallback> cb_; 225 }; 226 227 class AudioManagerDeviceChangeCallback { 228 public: 229 virtual ~AudioManagerDeviceChangeCallback() = default; 230 /** 231 * Called when an interrupt is received. 232 * 233 * @param deviceChangeAction Indicates the DeviceChangeAction information needed by client. 234 * For details, refer DeviceChangeAction struct 235 * @since 8 236 */ 237 virtual void OnDeviceChange(const DeviceChangeAction &deviceChangeAction) = 0; 238 }; 239 240 class AudioManagerAvailableDeviceChangeCallback { 241 public: 242 virtual ~AudioManagerAvailableDeviceChangeCallback() = default; 243 /** 244 * Called when an interrupt is received. 245 * 246 * @param deviceChangeAction Indicates the DeviceChangeAction information needed by client. 247 * For details, refer DeviceChangeAction struct 248 * @since 11 249 */ 250 virtual void OnAvailableDeviceChange(const AudioDeviceUsage usage, 251 const DeviceChangeAction &deviceChangeAction) = 0; 252 }; 253 254 class VolumeKeyEventCallback { 255 public: 256 virtual ~VolumeKeyEventCallback() = default; 257 /** 258 * @brief VolumeKeyEventCallback will be executed when hard volume key is pressed up/down 259 * 260 * @param volumeEvent the volume event info. 261 * @since 8 262 */ 263 virtual void OnVolumeKeyEvent(VolumeEvent volumeEvent) = 0; 264 }; 265 266 class AudioParameterCallback { 267 public: 268 virtual ~AudioParameterCallback() = default; 269 /** 270 * @brief AudioParameterCallback will be executed when parameter change. 271 * 272 * @param networkId networkId 273 * @param key Audio paramKey 274 * @param condition condition 275 * @param value value 276 * @since 9 277 */ 278 virtual void OnAudioParameterChange(const std::string networkId, const AudioParamKey key, 279 const std::string& condition, const std::string& value) = 0; 280 }; 281 282 class AudioCapturerSourceCallback { 283 public: 284 virtual ~AudioCapturerSourceCallback() = default; 285 virtual void OnCapturerState(bool isActive) = 0; 286 }; 287 288 class WakeUpSourceCloseCallback { 289 public: 290 virtual ~WakeUpSourceCloseCallback() = default; 291 virtual void OnWakeupClose() = 0; 292 }; 293 294 class WakeUpSourceCallback : public AudioCapturerSourceCallback, public WakeUpSourceCloseCallback { 295 public: 296 virtual ~WakeUpSourceCallback() = default; 297 virtual void OnCapturerState(bool isActive) = 0; 298 virtual void OnWakeupClose() = 0; 299 }; 300 301 class AudioPreferredOutputDeviceChangeCallback; 302 303 class AudioFocusInfoChangeCallback { 304 public: 305 virtual ~AudioFocusInfoChangeCallback() = default; 306 /** 307 * Called when focus info change. 308 * 309 * @param focusInfoList Indicates the focusInfoList information needed by client. 310 * For details, refer audioFocusInfoList_ struct in audio_policy_server.h 311 * @since 9 312 */ 313 virtual void OnAudioFocusInfoChange(const std::list<std::pair<AudioInterrupt, AudioFocuState>> &focusInfoList) = 0; 314 OnAudioFocusRequested(const AudioInterrupt &)315 virtual void OnAudioFocusRequested(const AudioInterrupt &) {} 316 OnAudioFocusAbandoned(const AudioInterrupt &)317 virtual void OnAudioFocusAbandoned(const AudioInterrupt &) {} 318 }; 319 320 class AudioFocusInfoChangeCallbackImpl : public AudioFocusInfoChangeCallback { 321 public: 322 explicit AudioFocusInfoChangeCallbackImpl(); 323 virtual ~AudioFocusInfoChangeCallbackImpl(); 324 325 /** 326 * Called when focus info change. 327 * 328 * @param focusInfoList Indicates the focusInfoList information needed by client. 329 * For details, refer audioFocusInfoList_ struct in audio_policy_server.h 330 * @since 9 331 */ 332 void OnAudioFocusInfoChange(const std::list<std::pair<AudioInterrupt, AudioFocuState>> &focusInfoList) override; 333 void OnAudioFocusRequested(const AudioInterrupt &requestFocus) override; 334 void OnAudioFocusAbandoned(const AudioInterrupt &abandonFocus) override; 335 void SaveCallback(const std::weak_ptr<AudioFocusInfoChangeCallback> &callback); 336 337 /** 338 * Cancel when focus info change. 339 * 340 * @since 9 341 */ 342 void RemoveCallback(const std::weak_ptr<AudioFocusInfoChangeCallback> &callback); 343 private: 344 std::list<std::weak_ptr<AudioFocusInfoChangeCallback>> callbackList_; 345 std::shared_ptr<AudioFocusInfoChangeCallback> cb_; 346 }; 347 348 class AudioDistributedRoutingRoleCallback { 349 public: 350 virtual ~AudioDistributedRoutingRoleCallback() = default; 351 352 /** 353 * Called when audio device descriptor change. 354 * 355 * @param descriptor Indicates the descriptor needed by client. 356 * For details, refer AudioDeviceDescriptor in audio_system_manager.h 357 * @since 9 358 */ 359 virtual void OnDistributedRoutingRoleChange(const AudioDeviceDescriptor *descriptor, const CastType type) = 0; 360 std::mutex cbMutex_; 361 }; 362 363 class AudioDistributedRoutingRoleCallbackImpl : public AudioDistributedRoutingRoleCallback { 364 public: 365 explicit AudioDistributedRoutingRoleCallbackImpl(); 366 virtual ~AudioDistributedRoutingRoleCallbackImpl(); 367 368 /** 369 * Called when audio device descriptor change. 370 * 371 * @param descriptor Indicates the descriptor needed by client. 372 * For details, refer AudioDeviceDescriptor in audio_system_manager.h 373 * @since 9 374 */ 375 void OnDistributedRoutingRoleChange(const AudioDeviceDescriptor *descriptor, const CastType type) override; 376 void SaveCallback(const std::shared_ptr<AudioDistributedRoutingRoleCallback> &callback); 377 void RemoveCallback(const std::shared_ptr<AudioDistributedRoutingRoleCallback> &callback); 378 private: 379 std::list<std::shared_ptr<AudioDistributedRoutingRoleCallback>> callbackList_; 380 std::shared_ptr<AudioDistributedRoutingRoleCallback> cb_; 381 }; 382 383 /** 384 * @brief The AudioSystemManager class is an abstract definition of audio manager. 385 * Provides a series of client/interfaces for audio management 386 */ 387 388 class AudioSystemManager { 389 public: 390 static AudioSystemManager *GetInstance(); 391 392 /** 393 * @brief Map volume to HDI. 394 * 395 * @param volume volume value. 396 * @return Returns current volume. 397 * @since 8 398 */ 399 static float MapVolumeToHDI(int32_t volume); 400 401 /** 402 * @brief Map volume from HDI. 403 * 404 * @param volume volume value. 405 * @return Returns current volume. 406 * @since 8 407 */ 408 static int32_t MapVolumeFromHDI(float volume); 409 410 /** 411 * @brief Get audio streamType. 412 * 413 * @param contentType Enumerates the audio content type. 414 * @param streamUsage Enumerates the stream usage. 415 * @return Returns Audio streamType. 416 * @since 8 417 */ 418 static AudioStreamType GetStreamType(ContentType contentType, StreamUsage streamUsage); 419 420 /** 421 * @brief Set the stream volume. 422 * 423 * @param volumeType Enumerates the audio volume type. 424 * @param volume The volume to be set for the current stream. 425 * @return Returns {@link SUCCESS} if volume is successfully set; returns an error code 426 * defined in {@link audio_errors.h} otherwise. 427 * @since 8 428 */ 429 int32_t SetVolume(AudioVolumeType volumeType, int32_t volume) const; 430 431 /** 432 * @brief Obtains the current stream volume. 433 * 434 * @param volumeType Enumerates the audio volume type. 435 * @return Returns current stream volume. 436 * @since 8 437 */ 438 int32_t GetVolume(AudioVolumeType volumeType) const; 439 440 /** 441 * @brief Set volume discount factor. 442 * 443 * @param streamId stream Unique identification. 444 * @param volume Adjustment percentage. 445 * @return Whether the operation is effective 446 * @since 9 447 */ 448 int32_t SetLowPowerVolume(int32_t streamId, float volume) const; 449 450 /** 451 * @brief get volume discount factor. 452 * 453 * @param streamId stream Unique identification. 454 * @return Returns current stream volume. 455 * @since 9 456 */ 457 float GetLowPowerVolume(int32_t streamId) const; 458 459 /** 460 * @brief get single stream volume. 461 * 462 * @param streamId stream Unique identification. 463 * @return Returns current stream volume. 464 * @since 9 465 */ 466 float GetSingleStreamVolume(int32_t streamId) const; 467 468 /** 469 * @brief get max stream volume. 470 * 471 * @param volumeType audio volume type. 472 * @return Returns current stream volume. 473 * @since 8 474 */ 475 int32_t GetMaxVolume(AudioVolumeType volumeType); 476 477 /** 478 * @brief get min stream volume. 479 * 480 * @param volumeType audio volume type. 481 * @return Returns current stream volume. 482 * @since 8 483 */ 484 int32_t GetMinVolume(AudioVolumeType volumeType); 485 486 /** 487 * @brief set stream mute. 488 * 489 * @param volumeType audio volume type. 490 * @param mute Specifies whether the stream is muted. 491 * @return Returns {@link SUCCESS} if the setting is successful; returns an error code defined 492 * in {@link audio_errors.h} otherwise. 493 * @since 8 494 */ 495 int32_t SetMute(AudioVolumeType volumeType, bool mute) const; 496 497 /** 498 * @brief is stream mute. 499 * 500 * @param volumeType audio volume type. 501 * @return Returns <b>true</b> if the rendering is successfully started; returns <b>false</b> otherwise. 502 * @since 8 503 */ 504 bool IsStreamMute(AudioVolumeType volumeType) const; 505 506 /** 507 * @brief Set global microphone mute state. 508 * 509 * @param mute Specifies whether the Microphone is muted. 510 * @return Returns {@link SUCCESS} if the setting is successful; returns an error code defined 511 * in {@link audio_errors.h} otherwise. 512 * @since 8 513 */ 514 int32_t SetMicrophoneMute(bool isMute); 515 516 /** 517 * @brief get global microphone mute state. 518 * 519 * @return Returns <b>true</b> if the rendering is successfully started; returns <b>false</b> otherwise. 520 * @since 9 521 */ 522 bool IsMicrophoneMute(API_VERSION api_v = API_7); 523 524 /** 525 * @brief Select output device. 526 * 527 * @param audioDeviceDescriptors Output device object. 528 * @return Returns {@link SUCCESS} if the setting is successful; returns an error code defined 529 * in {@link audio_errors.h} otherwise. 530 * @since 9 531 */ 532 int32_t SelectOutputDevice(std::vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors) const; 533 534 /** 535 * @brief Select input device. 536 * 537 * @param audioDeviceDescriptors Output device object. 538 * @return Returns {@link SUCCESS} if the setting is successful; returns an error code defined 539 * in {@link audio_errors.h} otherwise. 540 * @since 9 541 */ 542 int32_t SelectInputDevice(std::vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors) const; 543 544 /** 545 * @brief get selected device info. 546 * 547 * @param uid identifier. 548 * @param pid identifier. 549 * @param streamType audio stream type. 550 * @return Returns device info. 551 * @since 9 552 */ 553 std::string GetSelectedDeviceInfo(int32_t uid, int32_t pid, AudioStreamType streamType) const; 554 555 /** 556 * @brief Select the audio output device according to the filter conditions. 557 * 558 * @param audioRendererFilter filter conditions. 559 * @param audioDeviceDescriptors Output device object. 560 * @return Returns {@link SUCCESS} if the setting is successful; returns an error code defined 561 * in {@link audio_errors.h} otherwise. 562 * @since 9 563 */ 564 int32_t SelectOutputDevice(sptr<AudioRendererFilter> audioRendererFilter, 565 std::vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors) const; 566 567 /** 568 * @brief Select the audio input device according to the filter conditions. 569 * 570 * @param audioRendererFilter filter conditions. 571 * @param audioDeviceDescriptors Output device object. 572 * @return Returns {@link SUCCESS} if the setting is successful; returns an error code defined 573 * in {@link audio_errors.h} otherwise. 574 * @since 9 575 */ 576 int32_t SelectInputDevice(sptr<AudioCapturerFilter> audioCapturerFilter, 577 std::vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors) const; 578 579 /** 580 * @brief Get the list of audio devices. 581 * 582 * @param deviceFlag Flag of device type. 583 * @param GetAudioParameter Key of audio parameters to be obtained. 584 * @return Returns the device list is obtained. 585 * @since 9 586 */ 587 std::vector<sptr<AudioDeviceDescriptor>> GetDevices(DeviceFlag deviceFlag); 588 589 /** 590 * @brief Get audio parameter. 591 * 592 * @param key Key of audio parameters to be obtained. 593 * @return Returns the value of the obtained audio parameter 594 * @since 9 595 */ 596 const std::string GetAudioParameter(const std::string key); 597 598 /** 599 * @brief set audio parameter. 600 * 601 * @param key The key of the set audio parameter. 602 * @param value The value of the set audio parameter. 603 * @since 9 604 */ 605 void SetAudioParameter(const std::string &key, const std::string &value); 606 607 /** 608 * @brief Get audio parameter. 609 * 610 * @param mainKey Main key of audio parameters to be obtained. 611 * @param subKeys subKeys of audio parameters to be obtained. 612 * @return Returns the value of the obtained audio parameter 613 * @since 11 614 */ 615 const std::vector<std::pair<std::string, std::string>> GetExtraParameters(const std::string mainKey, 616 const std::vector<std::string> subKeys); 617 618 /** 619 * @brief Set audio parameters. 620 * 621 * @param key The main key of the set audio parameter. 622 * @param kvpairs The pairs with sub keys and values of the set audio parameter. 623 * @since 11 624 */ 625 void SetExtraParameters(const std::string &key, const std::vector<std::pair<std::string, std::string>> &kvpairs); 626 627 /** 628 * @brief Get transaction Id. 629 * 630 * @param deviceType device type. 631 * @param deviceRole device role. 632 * @return Returns transaction Id. 633 * @since 9 634 */ 635 uint64_t GetTransactionId(DeviceType deviceType, DeviceRole deviceRole); 636 637 /** 638 * @brief Set device active. 639 * 640 * @param deviceType device type. 641 * @param flag Device activation status. 642 * @return Returns {@link SUCCESS} if the setting is successful; returns an error code defined 643 * in {@link audio_errors.h} otherwise. 644 * @since 9 645 */ 646 int32_t SetDeviceActive(ActiveDeviceType deviceType, bool flag) const; 647 648 /** 649 * @brief get device active. 650 * 651 * @param deviceType device type. 652 * @return Returns <b>true</b> if the rendering is successfully started; returns <b>false</b> otherwise. 653 * @since 9 654 */ 655 bool IsDeviceActive(ActiveDeviceType deviceType) const; 656 657 /** 658 * @brief get active output device. 659 * 660 * @return Returns device type. 661 * @since 9 662 */ 663 DeviceType GetActiveOutputDevice(); 664 665 /** 666 * @brief get active input device. 667 * 668 * @return Returns device type. 669 * @since 9 670 */ 671 DeviceType GetActiveInputDevice(); 672 673 /** 674 * @brief Is stream active. 675 * 676 * @param volumeType audio volume type. 677 * @return Returns <b>true</b> if the rendering is successfully started; returns <b>false</b> otherwise. 678 * @since 9 679 */ 680 bool IsStreamActive(AudioVolumeType volumeType) const; 681 682 /** 683 * @brief Set ringer mode. 684 * 685 * @param ringMode audio ringer mode. 686 * @return Returns {@link SUCCESS} if the setting is successful; returns an error code defined 687 * in {@link audio_errors.h} otherwise. 688 * @since 8 689 */ 690 int32_t SetRingerMode(AudioRingerMode ringMode); 691 692 /** 693 * @brief Get ringer mode. 694 * 695 * @return Returns audio ringer mode. 696 * @since 8 697 */ 698 AudioRingerMode GetRingerMode(); 699 700 /** 701 * @brief Set audio scene. 702 * 703 * @param scene audio scene. 704 * @return Returns {@link SUCCESS} if the setting is successful; returns an error code defined 705 * in {@link audio_errors.h} otherwise. 706 * @since 8 707 */ 708 int32_t SetAudioScene(const AudioScene &scene); 709 710 /** 711 * @brief Get audio scene. 712 * 713 * @return Returns audio scene. 714 * @since 8 715 */ 716 AudioScene GetAudioScene() const; 717 718 /** 719 * @brief Registers the deviceChange callback listener. 720 * 721 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 722 * defined in {@link audio_errors.h} otherwise. 723 * @since 8 724 */ 725 int32_t SetDeviceChangeCallback(const DeviceFlag flag, const std::shared_ptr<AudioManagerDeviceChangeCallback> 726 &callback); 727 728 /** 729 * @brief Unregisters the deviceChange callback listener 730 * 731 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 732 * defined in {@link audio_errors.h} otherwise. 733 * @since 8 734 */ 735 int32_t UnsetDeviceChangeCallback(DeviceFlag flag = DeviceFlag::ALL_DEVICES_FLAG); 736 737 /** 738 * @brief Registers the ringerMode callback listener. 739 * 740 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 741 * defined in {@link audio_errors.h} otherwise. 742 * @since 8 743 */ 744 int32_t SetRingerModeCallback(const int32_t clientId, 745 const std::shared_ptr<AudioRingerModeCallback> &callback); 746 747 /** 748 * @brief Unregisters the VolumeKeyEvent callback listener 749 * 750 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 751 * defined in {@link audio_errors.h} otherwise. 752 * @since 8 753 */ 754 int32_t UnsetRingerModeCallback(const int32_t clientId) const; 755 756 /** 757 * @brief registers the volumeKeyEvent callback listener 758 * 759 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 760 * defined in {@link audio_errors.h} otherwise. 761 * @since 8 762 */ 763 int32_t RegisterVolumeKeyEventCallback(const int32_t clientPid, 764 const std::shared_ptr<VolumeKeyEventCallback> &callback, API_VERSION api_v = API_9); 765 766 /** 767 * @brief Unregisters the volumeKeyEvent callback listener 768 * 769 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 770 * defined in {@link audio_errors.h} otherwise. 771 * @since 8 772 */ 773 int32_t UnregisterVolumeKeyEventCallback(const int32_t clientPid); 774 775 /** 776 * @brief Set mono audio state 777 * 778 * @param monoState mono state 779 * @since 8 780 */ 781 void SetAudioMonoState(bool monoState); 782 783 /** 784 * @brief Set audio balance value 785 * 786 * @param balanceValue balance value 787 * @since 8 788 */ 789 void SetAudioBalanceValue(float balanceValue); 790 791 /** 792 * @brief Set system sound uri 793 * 794 * @param key the key of uri 795 * @param uri the value of uri 796 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 797 * defined in {@link audio_errors.h} otherwise. 798 * @since 10 799 */ 800 int32_t SetSystemSoundUri(const std::string &key, const std::string &uri); 801 802 /** 803 * @brief Get system sound uri 804 * 805 * @param key the key of uri 806 * @return Returns the value of uri for the key 807 * @since 10 808 */ 809 std::string GetSystemSoundUri(const std::string &key); 810 811 // Below APIs are added to handle compilation error in call manager 812 // Once call manager adapt to new interrupt APIs, this will be removed 813 814 /** 815 * @brief registers the audioManager callback listener 816 * 817 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 818 * defined in {@link audio_errors.h} otherwise. 819 * @since 8 820 */ 821 int32_t SetAudioManagerCallback(const AudioVolumeType streamType, 822 const std::shared_ptr<AudioManagerCallback> &callback); 823 824 /** 825 * @brief Unregisters the audioManager callback listener 826 * 827 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 828 * defined in {@link audio_errors.h} otherwise. 829 * @since 8 830 */ 831 int32_t UnsetAudioManagerCallback(const AudioVolumeType streamType) const; 832 833 /** 834 * @brief Activate audio Interrupt 835 * 836 * @param audioInterrupt audioInterrupt 837 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 838 * defined in {@link audio_errors.h} otherwise. 839 * @since 8 840 */ 841 int32_t ActivateAudioInterrupt(const AudioInterrupt &audioInterrupt); 842 843 /** 844 * @brief Deactivactivate audio Interrupt 845 * 846 * @param audioInterrupt audioInterrupt 847 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 848 * defined in {@link audio_errors.h} otherwise. 849 * @since 8 850 */ 851 int32_t DeactivateAudioInterrupt(const AudioInterrupt &audioInterrupt) const; 852 853 /** 854 * @brief registers the Interrupt callback listener 855 * 856 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 857 * defined in {@link audio_errors.h} otherwise. 858 * @since 8 859 */ 860 int32_t SetAudioManagerInterruptCallback(const std::shared_ptr<AudioManagerCallback> &callback); 861 862 /** 863 * @brief Unregisters the Interrupt callback listener 864 * 865 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 866 * defined in {@link audio_errors.h} otherwise. 867 * @since 8 868 */ 869 int32_t UnsetAudioManagerInterruptCallback(); 870 871 /** 872 * @brief Request audio focus 873 * 874 * @param audioInterrupt audioInterrupt 875 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 876 * defined in {@link audio_errors.h} otherwise. 877 * @since 8 878 */ 879 int32_t RequestAudioFocus(const AudioInterrupt &audioInterrupt); 880 881 /** 882 * @brief Abandon audio focus 883 * 884 * @param audioInterrupt audioInterrupt 885 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 886 * defined in {@link audio_errors.h} otherwise. 887 * @since 8 888 */ 889 int32_t AbandonAudioFocus(const AudioInterrupt &audioInterrupt); 890 891 /** 892 * @brief Reconfigure audio channel 893 * 894 * @param count count 895 * @param deviceType device type 896 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 897 * defined in {@link audio_errors.h} otherwise. 898 * @since 8 899 */ 900 int32_t ReconfigureAudioChannel(const uint32_t &count, DeviceType deviceType); 901 902 /** 903 * @brief Request independent interrupt 904 * 905 * @param focusType focus type 906 * @return Returns {@link SUCCESS} if the setting is successful; returns an error code defined 907 * in {@link audio_errors.h} otherwise. 908 * @since 8 909 */ 910 bool RequestIndependentInterrupt(FocusType focusType); 911 912 /** 913 * @brief Abandon independent interrupt 914 * 915 * @param focusType focus type 916 * @return Returns {@link SUCCESS} if the setting is successful; returns an error code defined 917 * in {@link audio_errors.h} otherwise. 918 * @since 8 919 */ 920 bool AbandonIndependentInterrupt(FocusType focusType); 921 922 /** 923 * @brief Get audio latency from Xml 924 * 925 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 926 * defined in {@link audio_errors.h} otherwise. 927 * @since 8 928 */ 929 int32_t GetAudioLatencyFromXml() const; 930 931 /** 932 * @brief Get audio sink from Xml 933 * 934 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 935 * defined in {@link audio_errors.h} otherwise. 936 * @since 8 937 */ 938 uint32_t GetSinkLatencyFromXml() const; 939 940 /** 941 * @brief Update stream state 942 * 943 * @param clientUid client Uid 944 * @param streamSetState streamSetState 945 * @param audioStreamType audioStreamType 946 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 947 * defined in {@link audio_errors.h} otherwise. 948 * @since 8 949 */ 950 int32_t UpdateStreamState(const int32_t clientUid, StreamSetState streamSetState, 951 AudioStreamType audioStreamType); 952 953 /** 954 * @brief Get Pin Value From Type 955 * 956 * @param deviceType deviceType 957 * @param deviceRole deviceRole 958 * @return Returns Enumerate AudioPin 959 * @since 8 960 */ 961 AudioPin GetPinValueFromType(DeviceType deviceType, DeviceRole deviceRole) const; 962 963 /** 964 * @brief Get type Value From Pin 965 * 966 * @param pin AudioPin 967 * @return Returns Enumerate DeviceType 968 * @since 8 969 */ 970 DeviceType GetTypeValueFromPin(AudioPin pin) const; 971 972 /** 973 * @brief Get volume groups 974 * 975 * @param networkId networkId 976 * @param info VolumeGroupInfo 977 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 978 * defined in {@link audio_errors.h} otherwise. 979 * @since 8 980 */ 981 int32_t GetVolumeGroups(std::string networkId, std::vector<sptr<VolumeGroupInfo>> &info); 982 983 /** 984 * @brief Get volume groups manager 985 * 986 * @param networkId networkId 987 * @return Returns AudioGroupManager 988 * @since 8 989 */ 990 std::shared_ptr<AudioGroupManager> GetGroupManager(int32_t groupId); 991 992 /** 993 * @brief Get active output deviceDescriptors 994 * 995 * @return Returns AudioDeviceDescriptor 996 * @since 8 997 */ 998 std::vector<sptr<AudioDeviceDescriptor>> GetActiveOutputDeviceDescriptors(); 999 1000 /** 1001 * @brief Get preferred input device deviceDescriptors 1002 * 1003 * @return Returns AudioDeviceDescriptor 1004 * @since 10 1005 */ 1006 int32_t GetPreferredInputDeviceDescriptors(); 1007 1008 /** 1009 * @brief Get audio focus info 1010 * 1011 * @return Returns success or not 1012 * @since 10 1013 */ 1014 int32_t GetAudioFocusInfoList(std::list<std::pair<AudioInterrupt, AudioFocuState>> &focusInfoList); 1015 1016 /** 1017 * @brief Register callback to listen audio focus info change event 1018 * 1019 * @return Returns success or not 1020 * @since 10 1021 */ 1022 int32_t RegisterFocusInfoChangeCallback(const std::shared_ptr<AudioFocusInfoChangeCallback> &callback); 1023 1024 /** 1025 * @brief Unregister callback to listen audio focus info change event 1026 * 1027 * @return Returns success or not 1028 * @since 10 1029 */ 1030 int32_t UnregisterFocusInfoChangeCallback( 1031 const std::shared_ptr<AudioFocusInfoChangeCallback> &callback = nullptr); 1032 1033 /** 1034 * @brief Ask audio native process to request thread priority for client 1035 * 1036 * @param tid Target thread id 1037 * @since 10 1038 */ 1039 void RequestThreadPriority(uint32_t tid); 1040 1041 int32_t SetAudioCapturerSourceCallback(const std::shared_ptr<AudioCapturerSourceCallback> &callback); 1042 1043 int32_t SetWakeUpSourceCloseCallback(const std::shared_ptr<WakeUpSourceCloseCallback> &callback); 1044 1045 int32_t OffloadDrain(); 1046 1047 int32_t GetCapturePresentationPosition(const std::string& deviceClass, uint64_t& frames, int64_t& timeSec, 1048 int64_t& timeNanoSec); 1049 1050 int32_t GetRenderPresentationPosition(const std::string& deviceClass, uint64_t& frames, int64_t& timeSec, 1051 int64_t& timeNanoSec); 1052 1053 int32_t OffloadGetPresentationPosition(uint64_t& frames, int64_t& timeSec, int64_t& timeNanoSec); 1054 1055 int32_t OffloadSetBufferSize(uint32_t sizeMs); 1056 1057 int32_t OffloadSetVolume(float volume); 1058 1059 /** 1060 * @brief Set whether or not absolute volume is supported for the specified Bluetooth device 1061 * 1062 * @return Returns success or not 1063 * @since 11 1064 */ 1065 int32_t SetDeviceAbsVolumeSupported(const std::string &macAddress, const bool support); 1066 1067 /** 1068 * @brief Set the absolute volume value for the specified Bluetooth device 1069 * 1070 * @return Returns success or not 1071 * @since 11 1072 */ 1073 int32_t SetA2dpDeviceVolume(const std::string &macAddress, const int32_t volume, const bool updateUi); 1074 /** 1075 * @brief Registers the availbale deviceChange callback listener. 1076 * 1077 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 1078 * defined in {@link audio_errors.h} otherwise. 1079 * @since 11 1080 */ 1081 int32_t SetAvailableDeviceChangeCallback(const AudioDeviceUsage usage, 1082 const std::shared_ptr<AudioManagerAvailableDeviceChangeCallback>& callback); 1083 /** 1084 * @brief UnRegisters the availbale deviceChange callback listener. 1085 * 1086 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 1087 * defined in {@link audio_errors.h} otherwise. 1088 * @since 11 1089 */ 1090 int32_t UnsetAvailableDeviceChangeCallback(AudioDeviceUsage usage); 1091 1092 /** 1093 * @brief Switch the output device accoring different cast type. 1094 * 1095 * @return Returns {@link SUCCESS} if device is successfully switched; returns an error code 1096 * defined in {@link audio_errors.h} otherwise. 1097 * @since 11 1098 */ 1099 int32_t ConfigDistributedRoutingRole(AudioDeviceDescriptor *desciptor, CastType type); 1100 1101 /** 1102 * @brief Registers the descriptor Change callback listener. 1103 * 1104 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 1105 * defined in {@link audio_errors.h} otherwise. 1106 * @since 11 1107 */ 1108 int32_t SetDistributedRoutingRoleCallback(const std::shared_ptr<AudioDistributedRoutingRoleCallback> &callback); 1109 1110 /** 1111 * @brief UnRegisters the descriptor Change callback callback listener. 1112 * 1113 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 1114 * defined in {@link audio_errors.h} otherwise. 1115 * @since 11 1116 */ 1117 int32_t UnsetDistributedRoutingRoleCallback(const std::shared_ptr<AudioDistributedRoutingRoleCallback> &callback); 1118 1119 /** 1120 * @brief Set device address. 1121 * 1122 * @param deviceType device type. 1123 * @param flag Device activation status. 1124 * @param address Device address 1125 * @return Returns {@link SUCCESS} if the setting is successful; returns an error code defined 1126 * in {@link audio_errors.h} otherwise. 1127 * @since 11 1128 */ 1129 int32_t SetCallDeviceActive(ActiveDeviceType deviceType, bool flag, std::string address) const; 1130 1131 static void AudioServerDied(pid_t pid); 1132 private: 1133 class WakeUpCallbackImpl : public WakeUpSourceCallback { 1134 public: WakeUpCallbackImpl(AudioSystemManager * audioSystemManager)1135 WakeUpCallbackImpl(AudioSystemManager *audioSystemManager) 1136 :audioSystemManager_(audioSystemManager) 1137 { 1138 } OnCapturerState(bool isActive)1139 void OnCapturerState(bool isActive) override 1140 { 1141 auto callback = audioSystemManager_ -> audioCapturerSourceCallback_; 1142 if (callback != nullptr) { 1143 callback -> OnCapturerState(isActive); 1144 } 1145 } OnWakeupClose()1146 void OnWakeupClose() override 1147 { 1148 auto callback = audioSystemManager_ -> audioWakeUpSourceCloseCallback_; 1149 if (callback != nullptr) { 1150 callback -> OnWakeupClose(); 1151 } 1152 } 1153 private: 1154 AudioSystemManager *audioSystemManager_; 1155 }; 1156 1157 static constexpr int32_t MAX_VOLUME_LEVEL = 15; 1158 static constexpr int32_t MIN_VOLUME_LEVEL = 0; 1159 static constexpr int32_t CONST_FACTOR = 100; 1160 static const std::map<std::pair<ContentType, StreamUsage>, AudioStreamType> streamTypeMap_; 1161 1162 AudioSystemManager(); 1163 virtual ~AudioSystemManager(); 1164 1165 static std::map<std::pair<ContentType, StreamUsage>, AudioStreamType> CreateStreamMap(); 1166 uint32_t GetCallingPid(); 1167 std::string GetSelfBundleName(); 1168 1169 int32_t RegisterWakeupSourceCallback(); 1170 1171 int32_t cbClientId_ = -1; 1172 int32_t volumeChangeClientPid_ = -1; 1173 AudioRingerMode ringModeBackup_ = RINGER_MODE_NORMAL; 1174 std::shared_ptr<AudioManagerDeviceChangeCallback> deviceChangeCallback_ = nullptr; 1175 std::shared_ptr<AudioInterruptCallback> audioInterruptCallback_ = nullptr; 1176 std::shared_ptr<AudioRingerModeCallback> ringerModeCallback_ = nullptr; 1177 std::shared_ptr<AudioFocusInfoChangeCallback> audioFocusInfoCallback_ = nullptr; 1178 std::shared_ptr<AudioDistributedRoutingRoleCallback> audioDistributedRoutingRoleCallback_ = nullptr; 1179 std::vector<std::shared_ptr<AudioGroupManager>> groupManagerMap_; 1180 std::mutex ringerModeCallbackMutex_; 1181 1182 std::shared_ptr<AudioCapturerSourceCallback> audioCapturerSourceCallback_ = nullptr; 1183 std::shared_ptr<WakeUpSourceCloseCallback> audioWakeUpSourceCloseCallback_ = nullptr; 1184 1185 std::shared_ptr<WakeUpCallbackImpl> remoteWakeUpCallback_; 1186 }; 1187 } // namespace AudioStandard 1188 } // namespace OHOS 1189 #endif // ST_AUDIO_SYSTEM_MANAGER_H 1190