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