1 /* 2 * Copyright (c) 2023 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 AUDIO_SERVICE_H 17 #define AUDIO_SERVICE_H 18 19 #include <condition_variable> 20 #include <sstream> 21 #include <set> 22 #include <map> 23 #include <mutex> 24 #include <vector> 25 26 #include "audio_process_in_server.h" 27 #include "audio_endpoint.h" 28 #include "ipc_stream_in_server.h" 29 #include "playback_capturer_manager.h" 30 31 namespace OHOS { 32 namespace AudioStandard { 33 namespace { 34 enum InnerCapFilterPolicy : uint32_t { 35 POLICY_INVALID = 0, 36 POLICY_USAGES_ONLY, 37 POLICY_USAGES_AND_PIDS 38 }; 39 } // anonymous namespace 40 41 class AudioService : public ProcessReleaseCallback, public ICapturerFilterListener { 42 public: 43 static AudioService *GetInstance(); 44 ~AudioService(); 45 46 // override for ICapturerFilterListener 47 int32_t OnCapturerFilterChange(uint32_t sessionId, const AudioPlaybackCaptureConfig &newConfig) override; 48 int32_t OnCapturerFilterRemove(uint32_t sessionId) override; 49 50 int32_t GetStandbyStatus(uint32_t sessionId, bool &isStandby, int64_t &enterStandbyTime); 51 sptr<IpcStreamInServer> GetIpcStream(const AudioProcessConfig &config, int32_t &ret); 52 53 sptr<AudioProcessInServer> GetAudioProcess(const AudioProcessConfig &config); 54 // override for ProcessReleaseCallback, do release process work. 55 int32_t OnProcessRelease(IAudioProcessStream *process, bool isSwitchStream = false) override; 56 void ReleaseProcess(const std::string endpointName, const int32_t delayTime); 57 58 AudioDeviceDescriptor GetDeviceInfoForProcess(const AudioProcessConfig &config); 59 std::shared_ptr<AudioEndpoint> GetAudioEndpointForDevice(AudioDeviceDescriptor &deviceInfo, 60 const AudioProcessConfig &clientConfig, bool isVoipStream); 61 int32_t NotifyStreamVolumeChanged(AudioStreamType streamType, float volume); 62 63 int32_t LinkProcessToEndpoint(sptr<AudioProcessInServer> process, std::shared_ptr<AudioEndpoint> endpoint); 64 int32_t UnlinkProcessToEndpoint(sptr<AudioProcessInServer> process, std::shared_ptr<AudioEndpoint> endpoint); 65 void Dump(std::string &dumpString); 66 float GetMaxAmplitude(bool isOutputDevice); 67 void ResetAudioEndpoint(); 68 69 void RemoveRenderer(uint32_t sessionId); 70 void RemoveCapturer(uint32_t sessionId); 71 int32_t EnableDualToneList(uint32_t sessionId); 72 int32_t DisableDualToneList(uint32_t sessionId); 73 std::shared_ptr<RendererInServer> GetRendererBySessionID(const uint32_t &session); 74 void SetNonInterruptMute(const uint32_t SessionId, const bool muteFlag); 75 void UpdateMuteControlSet(uint32_t sessionId, bool muteFlag); 76 77 int32_t UpdateSourceType(SourceType sourceType); 78 void SetIncMaxRendererStreamCnt(AudioMode audioMode); 79 int32_t GetCurrentRendererStreamCnt(); 80 void SetDecMaxRendererStreamCnt(); 81 bool IsExceedingMaxStreamCntPerUid(int32_t callingUid, int32_t appUid, int32_t maxStreamCntPerUid); 82 void GetCreatedAudioStreamMostUid(int32_t &mostAppUid, int32_t &mostAppNum); 83 void CleanAppUseNumMap(int32_t appUid); 84 85 private: 86 AudioService(); 87 void DelayCallReleaseEndpoint(std::string endpointName, int32_t delayInMs); 88 89 void InsertRenderer(uint32_t sessionId, std::shared_ptr<RendererInServer> renderer); 90 void InsertCapturer(uint32_t sessionId, std::shared_ptr<CapturerInServer> capturer); 91 // for inner-capturer 92 void CheckInnerCapForRenderer(uint32_t sessionId, std::shared_ptr<RendererInServer> renderer); 93 void CheckInnerCapForProcess(sptr<AudioProcessInServer> process, std::shared_ptr<AudioEndpoint> endpoint); 94 void FilterAllFastProcess(); 95 InnerCapFilterPolicy GetInnerCapFilterPolicy(); 96 bool ShouldBeInnerCap(const AudioProcessConfig &rendererConfig); 97 bool ShouldBeDualTone(const AudioProcessConfig &config); 98 int32_t OnInitInnerCapList(); // for first InnerCap filter take effect. 99 int32_t OnUpdateInnerCapList(); // for some InnerCap filter has already take effect. 100 bool IsEndpointTypeVoip(const AudioProcessConfig &config, AudioDeviceDescriptor &deviceInfo); 101 void RemoveIdFromMuteControlSet(uint32_t sessionId); 102 void CheckRenderSessionMuteState(uint32_t sessionId, std::shared_ptr<RendererInServer> renderer); 103 void CheckCaptureSessionMuteState(uint32_t sessionId, std::shared_ptr<CapturerInServer> capturer); 104 void CheckFastSessionMuteState(uint32_t sessionId, sptr<AudioProcessInServer> process); 105 int32_t GetReleaseDelayTime(std::shared_ptr<AudioEndpoint> endpoint, bool isSwitchStream); 106 void ReLinkProcessToEndpoint(); 107 108 private: 109 std::mutex processListMutex_; 110 std::vector<std::pair<sptr<AudioProcessInServer>, std::shared_ptr<AudioEndpoint>>> linkedPairedList_; 111 112 std::mutex releaseEndpointMutex_; 113 std::condition_variable releaseEndpointCV_; 114 std::set<std::string> releasingEndpointSet_; 115 std::map<std::string, std::shared_ptr<AudioEndpoint>> endpointList_; 116 117 // for inner-capturer 118 PlaybackCapturerManager *innerCapturerMgr_ = nullptr; 119 uint32_t workingInnerCapId_ = 0; // invalid sessionId 120 uint32_t workingDualToneId_ = 0; // invalid sessionId 121 AudioPlaybackCaptureConfig workingConfig_; 122 123 std::mutex rendererMapMutex_; 124 std::mutex capturerMapMutex_; 125 std::vector<std::weak_ptr<RendererInServer>> filteredRendererMap_ = {}; 126 std::map<uint32_t, std::weak_ptr<RendererInServer>> allRendererMap_ = {}; 127 std::map<uint32_t, std::weak_ptr<CapturerInServer>> allCapturerMap_ = {}; 128 129 std::vector<std::weak_ptr<RendererInServer>> filteredDualToneRendererMap_ = {}; 130 131 std::mutex mutedSessionsMutex_; 132 std::set<uint32_t> mutedSessions_ = {}; 133 int32_t currentRendererStreamCnt_ = 0; 134 std::mutex streamLifeCycleMutex_ {}; 135 std::map<int32_t, std::int32_t> appUseNumMap_; 136 }; 137 } // namespace AudioStandard 138 } // namespace OHOS 139 #endif // AUDIO_SERVICE_H 140