1 /* 2 * Copyright (c) 2023-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 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 #ifdef SUPPORT_LOW_LATENCY 27 #include "audio_process_in_server.h" 28 #include "audio_endpoint.h" 29 #endif 30 31 #include "i_audio_process_stream.h" 32 #include "i_audio_process.h" 33 #include "audio_info.h" 34 #include "audio_device_descriptor.h" 35 #include "ipc_stream_in_server.h" 36 #include "playback_capturer_filter_listener.h" 37 38 namespace OHOS { 39 namespace AudioStandard { 40 namespace { 41 enum InnerCapFilterPolicy : uint32_t { 42 POLICY_INVALID = 0, 43 POLICY_USAGES_ONLY, 44 POLICY_USAGES_AND_PIDS 45 }; 46 } // anonymous namespace 47 48 #ifdef SUPPORT_LOW_LATENCY 49 class AudioService : public ProcessReleaseCallback, public ICapturerFilterListener 50 #else 51 class AudioService : public ICapturerFilterListener 52 #endif 53 { 54 public: 55 static AudioService *GetInstance(); 56 ~AudioService(); 57 58 // override for ICapturerFilterListener 59 int32_t OnCapturerFilterChange(uint32_t sessionId, const AudioPlaybackCaptureConfig &newConfig, 60 int32_t innerCapId) override; 61 int32_t OnCapturerFilterRemove(uint32_t sessionId, int32_t innerCapId) override; 62 63 int32_t GetStandbyStatus(uint32_t sessionId, bool &isStandby, int64_t &enterStandbyTime); 64 sptr<IpcStreamInServer> GetIpcStream(const AudioProcessConfig &config, int32_t &ret); 65 int32_t NotifyStreamVolumeChanged(AudioStreamType streamType, float volume); 66 67 #ifdef SUPPORT_LOW_LATENCY 68 sptr<AudioProcessInServer> GetAudioProcess(const AudioProcessConfig &config); 69 // override for ProcessReleaseCallback, do release process work. 70 int32_t OnProcessRelease(IAudioProcessStream *process, bool isSwitchStream = false) override; 71 void ReleaseProcess(const std::string endpointName, const int32_t delayTime); 72 73 AudioDeviceDescriptor GetDeviceInfoForProcess(const AudioProcessConfig &config); 74 std::shared_ptr<AudioEndpoint> GetAudioEndpointForDevice(AudioDeviceDescriptor &deviceInfo, 75 const AudioProcessConfig &clientConfig, bool isVoipStream); 76 77 int32_t LinkProcessToEndpoint(sptr<AudioProcessInServer> process, std::shared_ptr<AudioEndpoint> endpoint); 78 int32_t UnlinkProcessToEndpoint(sptr<AudioProcessInServer> process, std::shared_ptr<AudioEndpoint> endpoint); 79 void ResetAudioEndpoint(); 80 #endif 81 82 void Dump(std::string &dumpString); 83 float GetMaxAmplitude(bool isOutputDevice); 84 85 void RemoveRenderer(uint32_t sessionId); 86 void RemoveCapturer(uint32_t sessionId); 87 int32_t EnableDualToneList(uint32_t sessionId); 88 int32_t DisableDualToneList(uint32_t sessionId); 89 int32_t SetOffloadMode(uint32_t sessionId, int32_t state, bool isAppBack); 90 int32_t UnsetOffloadMode(uint32_t sessionId); 91 void UpdateAudioSinkState(uint32_t sinkId, bool started); 92 void CheckHibernateState(bool onHibernate); 93 bool GetHibernateState(); 94 std::shared_ptr<RendererInServer> GetRendererBySessionID(const uint32_t &session); 95 std::shared_ptr<CapturerInServer> GetCapturerBySessionID(const uint32_t &session); 96 void SetNonInterruptMute(const uint32_t SessionId, const bool muteFlag); 97 void SetNonInterruptMuteForProcess(const uint32_t SessionId, const bool muteFlag); 98 void UpdateMuteControlSet(uint32_t sessionId, bool muteFlag); 99 int32_t UpdateSourceType(SourceType sourceType); 100 void SetIncMaxRendererStreamCnt(AudioMode audioMode); 101 int32_t GetCurrentRendererStreamCnt(); 102 void SetDecMaxRendererStreamCnt(); 103 bool IsExceedingMaxStreamCntPerUid(int32_t callingUid, int32_t appUid, int32_t maxStreamCntPerUid); 104 void GetCreatedAudioStreamMostUid(int32_t &mostAppUid, int32_t &mostAppNum); 105 void CleanAppUseNumMap(int32_t appUid); 106 bool HasBluetoothEndpoint(); 107 void GetAllSinkInputs(std::vector<SinkInput> &sinkInputs); 108 void SetDefaultAdapterEnable(bool isEnable); 109 bool GetDefaultAdapterEnable(); 110 RestoreStatus RestoreSession(uint32_t sessionId, RestoreInfo restoreInfo); 111 #ifdef HAS_FEATURE_INNERCAPTURER 112 int32_t UnloadModernInnerCapSink(int32_t innerCapId); 113 #endif 114 115 private: 116 AudioService(); 117 void DelayCallReleaseEndpoint(std::string endpointName, int32_t delayInMs); 118 119 void InsertRenderer(uint32_t sessionId, std::shared_ptr<RendererInServer> renderer); 120 void InsertCapturer(uint32_t sessionId, std::shared_ptr<CapturerInServer> capturer); 121 #ifdef HAS_FEATURE_INNERCAPTURER 122 // for inner-capturer 123 void CheckInnerCapForRenderer(uint32_t sessionId, std::shared_ptr<RendererInServer> renderer); 124 #ifdef SUPPORT_LOW_LATENCY 125 void CheckInnerCapForProcess(sptr<AudioProcessInServer> process, std::shared_ptr<AudioEndpoint> endpoint); 126 void FilterAllFastProcess(); 127 int32_t CheckDisableFastInner(std::shared_ptr<AudioEndpoint> endpoint); 128 int32_t HandleFastCapture(std::set<int32_t> captureIds, sptr<AudioProcessInServer> audioProcessInServer, 129 std::shared_ptr<AudioEndpoint> audioEndpoint); 130 131 void CheckFastSessionMuteState(uint32_t sessionId, sptr<AudioProcessInServer> process); 132 int32_t GetReleaseDelayTime(std::shared_ptr<AudioEndpoint> endpoint, bool isSwitchStream); 133 #endif 134 InnerCapFilterPolicy GetInnerCapFilterPolicy(int32_t innerCapId); 135 bool ShouldBeInnerCap(const AudioProcessConfig &rendererConfig, int32_t innerCapId); 136 bool ShouldBeInnerCap(const AudioProcessConfig &rendererConfig, std::set<int32_t> &beCapIds); 137 bool CheckShouldCap(const AudioProcessConfig &rendererConfig, int32_t innerCapId); 138 #endif 139 bool ShouldBeDualTone(const AudioProcessConfig &config); 140 #ifdef HAS_FEATURE_INNERCAPTURER 141 int32_t OnInitInnerCapList(int32_t innerCapId); // for first InnerCap filter take effect. 142 int32_t OnUpdateInnerCapList(int32_t innerCapId); // for some InnerCap filter has already take effect. 143 #endif 144 bool IsEndpointTypeVoip(const AudioProcessConfig &config, AudioDeviceDescriptor &deviceInfo); 145 void RemoveIdFromMuteControlSet(uint32_t sessionId); 146 void CheckRenderSessionMuteState(uint32_t sessionId, std::shared_ptr<RendererInServer> renderer); 147 void CheckCaptureSessionMuteState(uint32_t sessionId, std::shared_ptr<CapturerInServer> capturer); 148 void ReLinkProcessToEndpoint(); 149 void AddFilteredRender(int32_t innerCapId, std::shared_ptr<RendererInServer> renderer); 150 bool IsMuteSwitchStream(uint32_t sessionId); 151 152 private: 153 std::mutex processListMutex_; 154 std::mutex releaseEndpointMutex_; 155 std::condition_variable releaseEndpointCV_; 156 std::set<std::string> releasingEndpointSet_; 157 158 #ifdef SUPPORT_LOW_LATENCY 159 std::vector<std::pair<sptr<AudioProcessInServer>, std::shared_ptr<AudioEndpoint>>> linkedPairedList_; 160 std::map<std::string, std::shared_ptr<AudioEndpoint>> endpointList_; 161 #endif 162 163 // for inner-capturer 164 bool isRegisterCapturerFilterListened_ = false; 165 bool isDefaultAdapterEnable_ = false; 166 uint32_t workingDualToneId_ = 0; // invalid sessionId 167 AudioPlaybackCaptureConfig workingConfig_; 168 std::unordered_map<int32_t, AudioPlaybackCaptureConfig> workingConfigs_; 169 170 std::mutex rendererMapMutex_; 171 std::mutex capturerMapMutex_; 172 std::mutex muteSwitchStreamSetMutex_; 173 std::unordered_map<int32_t, std::vector<std::weak_ptr<RendererInServer>>> filteredRendererMap_ = {}; 174 std::map<uint32_t, std::weak_ptr<RendererInServer>> allRendererMap_ = {}; 175 std::map<uint32_t, std::weak_ptr<CapturerInServer>> allCapturerMap_ = {}; 176 177 std::vector<std::weak_ptr<RendererInServer>> filteredDualToneRendererMap_ = {}; 178 179 std::mutex mutedSessionsMutex_; 180 std::set<uint32_t> mutedSessions_ = {}; 181 int32_t currentRendererStreamCnt_ = 0; 182 std::mutex streamLifeCycleMutex_ {}; 183 std::map<int32_t, std::int32_t> appUseNumMap_; 184 std::set<uint32_t> muteSwitchStreams_ = {}; 185 std::mutex allRunningSinksMutex_; 186 std::condition_variable allRunningSinksCV_; 187 std::set<uint32_t> allRunningSinks_; 188 bool onHibernate_ = false; 189 }; 190 } // namespace AudioStandard 191 } // namespace OHOS 192 #endif // AUDIO_SERVICE_H 193