1 /* 2 * Copyright (c) 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 #ifndef AUDIO_CORE_SERVICE_H 16 #define AUDIO_CORE_SERVICE_H 17 #include <mutex> 18 19 #include "audio_policy_server_handler.h" 20 #include "i_core_service_provider.h" 21 #include "idevice_status_observer.h" 22 #include "audio_stream_descriptor.h" 23 #include "audio_device_descriptor.h" 24 #include "audio_info.h" 25 #include "microphone_descriptor.h" 26 #include "audio_stream_change_info.h" 27 #include "audio_active_device.h" 28 #include "audio_scene_manager.h" 29 #include "audio_volume_manager.h" 30 #include "audio_capturer_session.h" 31 #include "audio_device_manager.h" 32 #include "audio_connected_device.h" 33 #include "audio_device_status.h" 34 #include "audio_effect_service.h" 35 #include "audio_microphone_descriptor.h" 36 #include "audio_recovery_device.h" 37 #include "device_status_listener.h" 38 #include "core_service_provider_stub.h" 39 #include "audio_pipe_info.h" 40 #include "audio_service_enum.h" 41 #include "audio_pipe_manager.h" 42 #include "audio_session_service.h" 43 #include "audio_pipe_selector.h" 44 #include "audio_policy_config_manager.h" 45 #include "audio_core_service_utils.h" 46 #include "sle_audio_device_manager.h" 47 #include "audio_event_utils.h" 48 #include "audio_stream_id_allocator.h" 49 #include "i_hpae_soft_link.h" 50 namespace OHOS { 51 namespace AudioStandard { 52 enum OffloadType { 53 LOCAL_OFFLOAD, 54 REMOTE_OFFLOAD, 55 OFFLOAD_TYPE_NUM, 56 }; 57 58 static constexpr uint32_t CONCURRENT_CAPTURE_DFX_THRESHOLD = 2; 59 static constexpr uint32_t CONCURRENT_CAPTURE_DFX_MSG_ARRAY_MAX = 5; 60 static constexpr uint32_t CONCURRENT_CAPTURE_DFX_HDI_SEGMENTS = 2; 61 struct ConcurrentCaptureDfxResult { 62 std::vector<std::string> existingAppName{}; 63 std::vector<uint8_t> existingAppState{}; 64 std::vector<uint8_t> existingSourceType{}; 65 std::vector<uint8_t> existingCaptureState{}; 66 std::vector<uint32_t> existingCreateDuration{}; 67 std::vector<uint32_t> existingStartDuration{}; 68 std::vector<bool> existingFastFlag{}; 69 uint8_t hdiSourceType{}; 70 std::string hdiSourceAlg{}; 71 uint8_t deviceType{}; 72 }; 73 74 class AudioA2dpOffloadManager; 75 class AudioCoreService : public enable_shared_from_this<AudioCoreService> { 76 public: 77 class EventEntry : public ICoreServiceProvider, public IDeviceStatusObserver { 78 public: 79 EventEntry(std::shared_ptr<AudioCoreService> coreService); 80 void RegistCoreService(); 81 82 // Stream operations 83 int32_t CreateRendererClient( 84 std::shared_ptr<AudioStreamDescriptor> streamDesc, uint32_t &flag, uint32_t &sessionId, 85 std::string &networkId); 86 int32_t CreateCapturerClient( 87 std::shared_ptr<AudioStreamDescriptor> streamDesc, uint32_t &flag, uint32_t &sessionId); 88 89 // ICoreServiceProvider 90 int32_t UpdateSessionOperation(uint32_t sessionId, SessionOperation operation, 91 SessionOperationMsg opMsg = SESSION_OP_MSG_DEFAULT) override; 92 int32_t ReloadCaptureSession(uint32_t sessionId, SessionOperation operation) override; 93 int32_t SetDefaultOutputDevice(const DeviceType deviceType, const uint32_t sessionId, 94 const StreamUsage streamUsage, bool isRunning, bool skipForce = false) override; 95 std::string GetAdapterNameBySessionId(uint32_t sessionId) override; 96 int32_t GetProcessDeviceInfoBySessionId(uint32_t sessionId, AudioDeviceDescriptor &deviceInfo, 97 AudioStreamInfo &streamInfo, bool isReloadProcess = false) override; 98 uint32_t GenerateSessionId() override; 99 int32_t LoadSplitModule(const std::string &splitArgs, const std::string &networkId); 100 101 // IDeviceStatusObserver 102 void OnDeviceInfoUpdated(AudioDeviceDescriptor &desc, const DeviceInfoUpdateCommand command) override; 103 void OnDeviceStatusUpdated(DeviceType devType, bool isConnected, 104 const std::string &macAddress, const std::string &deviceName, 105 const AudioStreamInfo &streamInfo, DeviceRole role = DEVICE_ROLE_NONE, bool hasPair = false) override; 106 void OnDeviceStatusUpdated(AudioDeviceDescriptor &desc, bool isConnected) override; 107 void OnDeviceStatusUpdated(DStatusInfo statusInfo, bool isStop = false) override; 108 void OnMicrophoneBlockedUpdate(DeviceType devType, DeviceBlockStatus status) override; 109 void OnPnpDeviceStatusUpdated(AudioDeviceDescriptor &desc, bool isConnected) override; 110 void OnDeviceConfigurationChanged(DeviceType deviceType, const std::string &macAddress, 111 const std::string &deviceName, const AudioStreamInfo &streamInfo) override; 112 void OnServiceConnected(AudioServiceIndex serviceIndex) override; 113 void OnServiceDisconnected(AudioServiceIndex serviceIndex) override; 114 void OnForcedDeviceSelected(DeviceType devType, const std::string &macAddress) override; 115 116 // Functions related to assignment operations - device related 117 int32_t SetAudioScene(AudioScene audioScene, const int32_t uid = INVALID_UID, const int32_t pid = INVALID_PID); 118 std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetDevices(DeviceFlag deviceFlag); 119 int32_t SetDeviceActive(InternalDeviceType deviceType, bool active, const int32_t uid = INVALID_UID); 120 int32_t SetInputDevice(const DeviceType deviceType, const uint32_t sessionID, 121 const SourceType sourceType, bool isRunning); 122 int32_t SetCallDeviceActive(InternalDeviceType deviceType, bool active, std::string address, 123 const int32_t uid = INVALID_UID); 124 int32_t RegisterTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo, 125 const sptr<IRemoteObject> &object, const int32_t apiVersion); 126 int32_t UpdateTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo); 127 void RegisteredTrackerClientDied(pid_t uid, pid_t pid); 128 bool ConnectServiceAdapter(); 129 void OnReceiveBluetoothEvent(const std::string macAddress, const std::string deviceName); 130 int32_t SelectOutputDevice(sptr<AudioRendererFilter> audioRendererFilter, 131 std::vector<std::shared_ptr<AudioDeviceDescriptor>> selectedDesc); 132 int32_t SelectInputDevice(sptr<AudioCapturerFilter> audioCapturerFilter, 133 std::vector<std::shared_ptr<AudioDeviceDescriptor>> selectedDesc); 134 void NotifyRemoteRenderState(std::string networkId, std::string condition, std::string value); 135 int32_t OnCapturerSessionAdded(uint64_t sessionID, SessionInfo sessionInfo, AudioStreamInfo streamInfo); 136 void CloseWakeUpAudioCapturer(); 137 void OnCapturerSessionRemoved(uint64_t sessionID); 138 int32_t TriggerFetchDevice(AudioStreamDeviceChangeReasonExt reason); 139 void FetchOutputDeviceForTrack(AudioStreamChangeInfo &streamChangeInfo, 140 const AudioStreamDeviceChangeReasonExt reason); 141 void FetchInputDeviceForTrack(AudioStreamChangeInfo &streamChangeInfo); 142 int32_t ExcludeOutputDevices(AudioDeviceUsage audioDevUsage, 143 std::vector<std::shared_ptr<AudioDeviceDescriptor>> &audioDeviceDescriptors); 144 int32_t UnexcludeOutputDevices(AudioDeviceUsage audioDevUsage, 145 std::vector<std::shared_ptr<AudioDeviceDescriptor>> &audioDeviceDescriptors); 146 int32_t SetSessionDefaultOutputDevice(const int32_t callerPid, const DeviceType &deviceType); 147 148 // Functions related to get operations - device related 149 bool IsArmUsbDevice(const AudioDeviceDescriptor &deviceDesc); 150 std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetPreferredOutputDeviceDescriptors( 151 AudioRendererInfo &rendererInfo, std::string networkId = LOCAL_NETWORK_ID); 152 std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetPreferredInputDeviceDescriptors( 153 AudioCapturerInfo &captureInfo, std::string networkId = LOCAL_NETWORK_ID); 154 std::shared_ptr<AudioDeviceDescriptor> GetActiveBluetoothDevice(); 155 std::vector<shared_ptr<AudioDeviceDescriptor>> GetAvailableDevices(AudioDeviceUsage usage); 156 std::vector<sptr<MicrophoneDescriptor>> GetAvailableMicrophones(); 157 std::vector<sptr<MicrophoneDescriptor>> GetAudioCapturerMicrophoneDescriptors(int32_t sessionId); 158 int32_t GetCurrentRendererChangeInfos(vector<shared_ptr<AudioRendererChangeInfo>> 159 &audioRendererChangeInfos, bool hasBTPermission, bool hasSystemPermission); 160 int32_t GetCurrentCapturerChangeInfos(vector<shared_ptr<AudioCapturerChangeInfo>> &audioCapturerChangeInfos, 161 bool hasBTPermission, bool hasSystemPermission); 162 std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetExcludedDevices(AudioDeviceUsage audioDevUsage); 163 int32_t FetchOutputDeviceAndRoute(std::string caller, 164 const AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN); 165 int32_t GetPreferredOutputStreamType(AudioRendererInfo &rendererInfo, const std::string &bundleName); 166 int32_t GetSessionDefaultOutputDevice(const int32_t callerPid, DeviceType &deviceType); 167 int32_t GetPreferredInputStreamType(AudioCapturerInfo &capturerInfo); 168 std::vector<sptr<VolumeGroupInfo>> GetVolumeGroupInfos(); 169 int32_t SetWakeUpAudioCapturerFromAudioServer(const AudioProcessConfig &config) override; 170 int32_t ReleaseOffloadPipe(AudioIOHandle id, uint32_t paIndex, OffloadType type); 171 private: 172 std::shared_ptr<AudioCoreService> coreService_; 173 std::shared_mutex eventMutex_; 174 }; 175 176 // Ctor & dtor 177 AudioCoreService(); 178 ~AudioCoreService(); 179 180 // Called by AudioPolicyServer 181 static std::shared_ptr<AudioCoreService> GetCoreService(); 182 void Init(); 183 void DeInit(); 184 void SetCallbackHandler(std::shared_ptr<AudioPolicyServerHandler> handler); 185 std::shared_ptr<EventEntry> GetEventEntry(); 186 bool IsStreamBelongToUid(const uid_t uid, const uint32_t sessionId); 187 void DumpPipeManager(std::string &dumpString); 188 void DumpSelectHistory(std::string &dumpString); 189 void SetAudioRouteCallback(uint32_t sessionId, const sptr<IRemoteObject> &object); 190 void UnsetAudioRouteCallback(uint32_t sessionId); 191 192 // Called by EventEntry - with lock 193 // Stream operations 194 int32_t CreateRendererClient( 195 std::shared_ptr<AudioStreamDescriptor> streamDesc, uint32_t &audioFlag, uint32_t &sessionId, 196 std::string &networkId); 197 int32_t CreateCapturerClient( 198 std::shared_ptr<AudioStreamDescriptor> streamDesc, uint32_t &audioFlag, uint32_t &sessionId); 199 int32_t StartClient(uint32_t sessionId); 200 int32_t PauseClient(uint32_t sessionId); 201 int32_t StopClient(uint32_t sessionId); 202 int32_t ReleaseClient(uint32_t sessionId, SessionOperationMsg opMsg = SESSION_OP_MSG_DEFAULT); 203 204 // ICoreServiceProvider from EventEntry 205 int32_t SetDefaultOutputDevice( 206 const DeviceType deviceType, const uint32_t sessionID, const StreamUsage streamUsage, bool isRunning, 207 bool skipForce = false); 208 std::string GetAdapterNameBySessionId(uint32_t sessionId); 209 int32_t GetProcessDeviceInfoBySessionId(uint32_t sessionId, AudioDeviceDescriptor &deviceInfo, 210 AudioStreamInfo &streamInfo); 211 uint32_t GenerateSessionId(); 212 int32_t LoadSplitModule(const std::string &splitArgs, const std::string &networkId); 213 214 // IDeviceStatusObserver from EventEntry 215 void OnDeviceInfoUpdated(AudioDeviceDescriptor &desc, const DeviceInfoUpdateCommand command); 216 void OnDeviceStatusUpdated(DeviceType devType, bool isConnected, 217 const std::string &macAddress, const std::string &deviceName, 218 const AudioStreamInfo &streamInfo, DeviceRole role = DEVICE_ROLE_NONE, bool hasPair = false); 219 void OnDeviceStatusUpdated(AudioDeviceDescriptor &desc, bool isConnected); 220 void OnDeviceStatusUpdated(DStatusInfo statusInfo, bool isStop = false); 221 void OnMicrophoneBlockedUpdate(DeviceType devType, DeviceBlockStatus status); 222 void OnPnpDeviceStatusUpdated(AudioDeviceDescriptor &desc, bool isConnected); 223 void OnDeviceConfigurationChanged(DeviceType deviceType, const std::string &macAddress, 224 const std::string &deviceName, const AudioStreamInfo &streamInfo); 225 int32_t OnServiceConnected(AudioServiceIndex serviceIndex); 226 void OnForcedDeviceSelected(DeviceType devType, const std::string &macAddress); 227 228 // Functions related to assignment operations - device related 229 int32_t SetAudioScene(AudioScene audioScene, const int32_t uid = INVALID_UID, const int32_t pid = INVALID_PID); 230 bool IsArmUsbDevice(const AudioDeviceDescriptor &deviceDesc); 231 int32_t SetDeviceActive(InternalDeviceType deviceType, bool active, const int32_t uid = INVALID_UID); 232 int32_t SetInputDevice(const DeviceType deviceType, const uint32_t sessionID, 233 const SourceType sourceType, bool isRunning); 234 int32_t SetCallDeviceActive(InternalDeviceType deviceType, bool active, std::string address, 235 const int32_t uid = INVALID_UID); 236 int32_t RegisterTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo, 237 const sptr<IRemoteObject> &object, const int32_t apiVersion); 238 int32_t UpdateTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo); 239 void RegisteredTrackerClientDied(pid_t uid, pid_t pid); 240 bool ConnectServiceAdapter(); 241 void OnReceiveBluetoothEvent(const std::string macAddress, const std::string deviceName); 242 int32_t SelectOutputDevice(sptr<AudioRendererFilter> audioRendererFilter, 243 std::vector<std::shared_ptr<AudioDeviceDescriptor>> selectedDesc); 244 void NotifyDistributedOutputChange(const AudioDeviceDescriptor &deviceDesc); 245 int32_t SelectInputDevice(sptr<AudioCapturerFilter> audioCapturerFilter, 246 std::vector<std::shared_ptr<AudioDeviceDescriptor>> selectedDesc); 247 void NotifyRemoteRenderState(std::string networkId, std::string condition, std::string value); 248 int32_t OnCapturerSessionAdded(uint64_t sessionID, SessionInfo sessionInfo, AudioStreamInfo streamInfo); 249 void CloseWakeUpAudioCapturer(); 250 int32_t SetWakeUpAudioCapturerFromAudioServer(const AudioProcessConfig &config); 251 void OnCapturerSessionRemoved(uint64_t sessionID); 252 int32_t TriggerFetchDevice(AudioStreamDeviceChangeReasonExt reason); 253 void FetchOutputDeviceForTrack(AudioStreamChangeInfo &streamChangeInfo, 254 const AudioStreamDeviceChangeReasonExt reason); 255 void FetchInputDeviceForTrack(AudioStreamChangeInfo &streamChangeInfo); 256 int32_t ExcludeOutputDevices(AudioDeviceUsage audioDevUsage, 257 std::vector<std::shared_ptr<AudioDeviceDescriptor>> &audioDeviceDescriptors); 258 int32_t UnexcludeOutputDevices(AudioDeviceUsage audioDevUsage, 259 std::vector<std::shared_ptr<AudioDeviceDescriptor>> &audioDeviceDescriptors); 260 int32_t SetSessionDefaultOutputDevice(const int32_t callerPid, const DeviceType &deviceType); 261 262 // Functions related to get operations - device related 263 std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetDevices(DeviceFlag deviceFlag); 264 std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetPreferredOutputDeviceDescInner( 265 AudioRendererInfo &rendererInfo, std::string networkId); 266 std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetPreferredInputDeviceDescInner( 267 AudioCapturerInfo &captureInfo, std::string networkId); 268 std::shared_ptr<AudioDeviceDescriptor> GetActiveBluetoothDevice(); 269 std::vector<shared_ptr<AudioDeviceDescriptor>> GetAvailableDevices(AudioDeviceUsage usage); 270 std::vector<sptr<MicrophoneDescriptor>> GetAvailableMicrophones(); 271 std::vector<sptr<MicrophoneDescriptor>> GetAudioCapturerMicrophoneDescriptors(int32_t sessionId); 272 int32_t GetCurrentRendererChangeInfos(vector<shared_ptr<AudioRendererChangeInfo>> 273 &audioRendererChangeInfos, bool hasBTPermission, bool hasSystemPermission); 274 int32_t GetCurrentCapturerChangeInfos(vector<shared_ptr<AudioCapturerChangeInfo>> &audioCapturerChangeInfos, 275 bool hasBTPermission, bool hasSystemPermission); 276 std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetExcludedDevices(AudioDeviceUsage audioDevUsage); 277 int32_t GetPreferredOutputStreamType(AudioRendererInfo &rendererInfo, const std::string &bundleName); 278 int32_t GetSessionDefaultOutputDevice(const int32_t callerPid, DeviceType &deviceType); 279 int32_t GetPreferredInputStreamType(AudioCapturerInfo &capturerInfo); 280 bool GetVolumeGroupInfos(std::vector<sptr<VolumeGroupInfo>> &infos); 281 DirectPlaybackMode GetDirectPlaybackSupport(const AudioStreamInfo &streamInfo, const StreamUsage &streamUsage); 282 283 // Called by Others - without lock 284 int32_t SetAudioDeviceAnahsCallback(const sptr<IRemoteObject> &object); 285 int32_t UnsetAudioDeviceAnahsCallback(); 286 void OnUpdateAnahsSupport(std::string anahsShowType); 287 void RegisterBluetoothListener(); 288 void UnregisterBluetoothListener(); 289 void ConfigDistributedRoutingRole(const std::shared_ptr<AudioDeviceDescriptor> descriptor, CastType type); 290 int32_t SetRingerMode(AudioRingerMode ringMode); 291 int32_t FetchOutputDeviceAndRoute(std::string caller, 292 const AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN); 293 int32_t FetchInputDeviceAndRoute(std::string caller); 294 void SetAudioServerProxy(); 295 bool GetDisableFastStreamParam(); 296 bool IsFastAllowed(std::string &bundleName); 297 void UpdateStreamPropInfo(const std::string &adapterName, const std::string &pipeName, 298 const std::list<DeviceStreamInfo> &deviceStreamInfo, const std::list<std::string> &supportDevices); 299 void ClearStreamPropInfo(const std::string &adapterName, const std::string &pipeName); 300 uint32_t GetStreamPropInfoSize(const std::string &adapterName, const std::string &pipeName); 301 int32_t CaptureConcurrentCheck(uint32_t sessionId); 302 303 private: 304 static std::string GetEncryptAddr(const std::string &addr); 305 int32_t FetchRendererPipesAndExecute(std::vector<std::shared_ptr<AudioStreamDescriptor>> &streamDescs, 306 const AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN); 307 int32_t FetchCapturerPipesAndExecute(std::vector<std::shared_ptr<AudioStreamDescriptor>> &streamDescs); 308 int32_t ScoInputDeviceFetchedForRecongnition( 309 bool handleFlag, const std::string &address, ConnectState connectState); 310 void BluetoothScoFetch(std::shared_ptr<AudioStreamDescriptor> streamDesc); 311 void CheckModemScene(std::vector<std::shared_ptr<AudioDeviceDescriptor>> &descs, 312 const AudioStreamDeviceChangeReasonExt reason); 313 int32_t UpdateModemRoute(std::vector<std::shared_ptr<AudioDeviceDescriptor>> &descs); 314 void HandleAudioCaptureState(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo); 315 void UpdateDefaultOutputDeviceWhenStopping(int32_t uid); 316 void UpdateInputDeviceWhenStopping(int32_t uid); 317 int32_t BluetoothDeviceFetchOutputHandle(shared_ptr<AudioStreamDescriptor> &desc, 318 const AudioStreamDeviceChangeReasonExt reason, std::string encryptMacAddr); 319 int32_t ActivateA2dpDeviceWhenDescEnabled(shared_ptr<AudioDeviceDescriptor> desc, 320 const AudioStreamDeviceChangeReasonExt reason); 321 int32_t ActivateA2dpDevice(std::shared_ptr<AudioDeviceDescriptor> desc, 322 const AudioStreamDeviceChangeReasonExt reason); 323 int32_t SwitchActiveA2dpDevice(std::shared_ptr<AudioDeviceDescriptor> deviceDescriptor); 324 int32_t ActivateNearlinkDevice(const std::shared_ptr<AudioStreamDescriptor> &streamDesc, 325 const AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReasonExt::ExtEnum::UNKNOWN); 326 int32_t LoadA2dpModule(DeviceType deviceType, const AudioStreamInfo &audioStreamInfo, 327 std::string networkId, std::string sinkName, SourceType sourceType); 328 int32_t ReloadA2dpAudioPort(AudioModuleInfo &moduleInfo, DeviceType deviceType, 329 const AudioStreamInfo& audioStreamInfo, std::string networkId, std::string sinkName, 330 SourceType sourceType); 331 AudioIOHandle ReloadOrOpenAudioPort(int32_t engineFlag, AudioModuleInfo &moduleInfo, 332 uint32_t &paIndex); 333 void GetA2dpModuleInfo(AudioModuleInfo &moduleInfo, const AudioStreamInfo& audioStreamInfo, 334 SourceType sourceType); 335 void RecordSelectDevice(const std::string &history); 336 bool IsSameDevice(shared_ptr<AudioDeviceDescriptor> &desc, const AudioDeviceDescriptor &deviceInfo); 337 int32_t SwitchActiveHearingAidDevice(std::shared_ptr<AudioDeviceDescriptor> deviceDescriptor); 338 int32_t LoadHearingAidModule(DeviceType deviceType, const AudioStreamInfo &audioStreamInfo, 339 std::string networkId, std::string sinkName, SourceType sourceType); 340 #ifdef BLUETOOTH_ENABLE 341 void RegisterBluetoothDeathCallback(); 342 static void BluetoothServiceCrashedCallback(pid_t pid, pid_t uid); 343 #endif 344 int32_t FetchDeviceAndRoute(std::string caller, 345 const AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN); 346 int32_t FetchRendererPipeAndExecute(std::shared_ptr<AudioStreamDescriptor> streamDesc, 347 uint32_t &sessionId, uint32_t &audioFlag, 348 const AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReasonExt::ExtEnum::UNKNOWN); 349 void ProcessOutputPipeNew(std::shared_ptr<AudioPipeInfo> pipeInfo, uint32_t &flag, 350 const AudioStreamDeviceChangeReasonExt reason); 351 void ProcessOutputPipeUpdate(std::shared_ptr<AudioPipeInfo> pipeInfo, uint32_t &flag, 352 const AudioStreamDeviceChangeReasonExt reason); 353 int32_t FetchCapturerPipeAndExecute( 354 std::shared_ptr<AudioStreamDescriptor> streamDesc, uint32_t &audioFlag, uint32_t &sessionId); 355 void ProcessInputPipeNew(std::shared_ptr<AudioPipeInfo> pipeInfo, uint32_t &flag); 356 void ProcessInputPipeUpdate(std::shared_ptr<AudioPipeInfo> pipeInfo, uint32_t &flag); 357 void RemoveUnusedPipe(); 358 void RemoveUnusedRecordPipe(); 359 void MoveStreamSink(std::shared_ptr<AudioStreamDescriptor> streamDesc, 360 std::shared_ptr<AudioPipeInfo> pipeInfo, const AudioStreamDeviceChangeReasonExt reason); 361 void MoveToNewOutputDevice(std::shared_ptr<AudioStreamDescriptor> streamDesc, 362 std::shared_ptr<AudioPipeInfo> pipeInfo, 363 const AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN); 364 int32_t MoveToRemoteOutputDevice( 365 std::vector<SinkInput> sinkInputIds, std::shared_ptr<AudioPipeInfo> pipeInfo, 366 std::shared_ptr<AudioDeviceDescriptor> remoteDeviceDescriptor); 367 void MoveStreamSource(std::shared_ptr<AudioStreamDescriptor> streamDesc); 368 void MoveToNewInputDevice(std::shared_ptr<AudioStreamDescriptor> streamDesc); 369 int32_t MoveToLocalInputDevice( 370 std::vector<SourceOutput> sourceOutputs, std::shared_ptr<AudioDeviceDescriptor> localDeviceDescriptor); 371 int32_t MoveToRemoteInputDevice( 372 std::vector<SourceOutput> sourceInputs, std::shared_ptr<AudioDeviceDescriptor> remoteDeviceDescriptor); 373 int32_t OpenRemoteAudioDevice(std::string networkId, DeviceRole deviceRole, DeviceType deviceType, 374 std::shared_ptr<AudioDeviceDescriptor> remoteDeviceDescriptor); 375 bool SelectRingerOrAlarmDevices(std::shared_ptr<AudioStreamDescriptor> streamDesc); 376 void UpdateDualToneState(const bool &enable, const int32_t &sessionId); 377 int32_t MoveToLocalOutputDevice(std::vector<SinkInput> sinkInputIds, 378 std::shared_ptr<AudioPipeInfo> pipeInfo, std::shared_ptr<AudioDeviceDescriptor> localDeviceDescriptor); 379 bool HasLowLatencyCapability(DeviceType deviceType, bool isRemote); 380 void TriggerRecreateRendererStreamCallback(shared_ptr<AudioStreamDescriptor> &streamDesc, 381 const AudioStreamDeviceChangeReasonExt reason); 382 void TriggerRecreateCapturerStreamCallback(shared_ptr<AudioStreamDescriptor> &streamDesc); 383 CapturerState HandleStreamStatusToCapturerState(AudioStreamStatus status); 384 uint32_t OpenNewAudioPortAndRoute(std::shared_ptr<AudioPipeInfo> pipeInfo, uint32_t &paIndex); 385 static int32_t GetRealUid(std::shared_ptr<AudioStreamDescriptor> streamDesc); 386 static int32_t GetRealPid(std::shared_ptr<AudioStreamDescriptor> streamDesc); 387 static void UpdateRendererInfoWhenNoPermission(const shared_ptr<AudioRendererChangeInfo> &audioRendererChangeInfos, 388 bool hasSystemPermission); 389 static void UpdateCapturerInfoWhenNoPermission(const shared_ptr<AudioCapturerChangeInfo> &audioCapturerChangeInfos, 390 bool hasSystemPermission); 391 void SendA2dpConnectedWhileRunning(const RendererState &rendererState, const uint32_t &sessionId); 392 void UpdateSessionConnectionState(const int32_t &sessionID, const int32_t &state); 393 void UpdateTrackerDeviceChange(const vector<std::shared_ptr<AudioDeviceDescriptor>> &desc); 394 bool IsForcedNormal(std::shared_ptr<AudioStreamDescriptor> &streamDesc); 395 void UpdatePlaybackStreamFlag(std::shared_ptr<AudioStreamDescriptor> &streamDesc, bool isCreateProcess); 396 AudioFlag SetFlagForSpecialStream(std::shared_ptr<AudioStreamDescriptor> &streamDesc, bool isCreateProcess); 397 void UpdateRecordStreamFlag(std::shared_ptr<AudioStreamDescriptor> streamDesc); 398 std::vector<SourceOutput> FilterSourceOutputs(int32_t sessionId); 399 std::vector<SourceOutput> GetSourceOutputs(); 400 void UpdateOutputRoute(std::shared_ptr<AudioStreamDescriptor> streamDesc); 401 void OnPreferredOutputDeviceUpdated(const AudioDeviceDescriptor &deviceDescriptor, 402 const AudioStreamDeviceChangeReason reason); 403 void OnPreferredInputDeviceUpdated(DeviceType deviceType, std::string networkId); 404 bool IsRingerOrAlarmerDualDevicesRange(const InternalDeviceType &deviceType); 405 bool GetFastControlParam(); 406 void StoreDistributedRoutingRoleInfo(const std::shared_ptr<AudioDeviceDescriptor> descriptor, CastType type); 407 int32_t GetSystemVolumeLevel(AudioStreamType streamType); 408 float GetSystemVolumeInDb(AudioVolumeType volumeType, int32_t volumeLevel, DeviceType deviceType) const; 409 bool IsStreamSupportLowpower(std::shared_ptr<AudioStreamDescriptor> streamDesc); 410 bool IsStreamSupportDirect(std::shared_ptr<AudioStreamDescriptor> streamDesc); 411 bool IsStreamSupportMultiChannel(std::shared_ptr<AudioStreamDescriptor> streamDesc); 412 bool IsNewDevicePlaybackSupported(std::shared_ptr<AudioStreamDescriptor> streamDesc); 413 bool IsDeviceSwitching(const AudioStreamDeviceChangeReasonExt reason); 414 415 void AddSessionId(const uint32_t sessionId); 416 void DeleteSessionId(const uint32_t sessionId); 417 418 bool IsPaRoute(uint32_t routeFlag); 419 bool RecoverFetchedDescs(const std::vector<std::shared_ptr<AudioStreamDescriptor>> &streamDescs); 420 int32_t HandleScoOutputDeviceFetched( 421 shared_ptr<AudioDeviceDescriptor> &desc, const AudioStreamDeviceChangeReasonExt reason); 422 int32_t HandleScoOutputDeviceFetched( 423 shared_ptr<AudioStreamDescriptor> &streamDesc, const AudioStreamDeviceChangeReasonExt reason); 424 int32_t HandleFetchOutputWhenNoRunningStream(const AudioStreamDeviceChangeReasonExt reason); 425 int32_t HandleFetchInputWhenNoRunningStream(); 426 bool UpdateOutputDevice(std::shared_ptr<AudioDeviceDescriptor> &desc, int32_t uid, 427 const AudioStreamDeviceChangeReasonExt reason); 428 bool UpdateInputDevice(std::shared_ptr<AudioDeviceDescriptor> &desc, int32_t uid, 429 const AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN); 430 void WriteOutputRouteChangeEvent(std::shared_ptr<AudioDeviceDescriptor> &desc, 431 const AudioStreamDeviceChangeReason reason); 432 void WriteInputRouteChangeEvent(std::shared_ptr<AudioDeviceDescriptor> &desc, 433 const AudioStreamDeviceChangeReason reason); 434 int32_t HandleDeviceChangeForFetchOutputDevice(std::shared_ptr<AudioStreamDescriptor> &streamDesc, 435 const AudioStreamDeviceChangeReasonExt reason); 436 int32_t HandleDeviceChangeForFetchInputDevice(std::shared_ptr<AudioStreamDescriptor> &streamDesc); 437 bool NeedRehandleA2DPDevice(std::shared_ptr<AudioDeviceDescriptor> &desc); 438 void UpdateTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo, RendererState rendererState); 439 void HandleCommonSourceOpened(std::shared_ptr<AudioPipeInfo> &pipeInfo); 440 void DelayReleaseOffloadPipe(AudioIOHandle id, uint32_t paIndex, OffloadType type); 441 int32_t ReleaseOffloadPipe(AudioIOHandle id, uint32_t paIndex, OffloadType type); 442 void CheckOffloadStream(AudioStreamChangeInfo &streamChangeInfo); 443 void ReConfigOffloadStatus(uint32_t sessionId, std::shared_ptr<AudioPipeInfo> &pipeInfo, std::string &oldSinkName); 444 void PrepareMoveAttrs(std::shared_ptr<AudioStreamDescriptor> &streamDesc, DeviceType &oldDeviceType, 445 bool &isNeedTriggerCallback, std::string &oldSinkName, const AudioStreamDeviceChangeReasonExt reason); 446 void MuteSinkPortForSwitchDevice(std::shared_ptr<AudioStreamDescriptor> &streamDesc, 447 const AudioStreamDeviceChangeReasonExt reason); 448 void CheckAndSleepBeforeRingDualDeviceSet(std::shared_ptr<AudioStreamDescriptor> &streamDesc, 449 const AudioStreamDeviceChangeReasonExt reason); 450 void SleepForSwitchDevice(std::shared_ptr<AudioStreamDescriptor> &streamDesc, 451 const AudioStreamDeviceChangeReasonExt reason); 452 bool IsHeadsetToSpkOrEp(const std::shared_ptr<AudioDeviceDescriptor> &oldDesc, 453 const std::shared_ptr<AudioDeviceDescriptor> &newDesc); 454 bool IsSceneRequireMuteAndSleep(); 455 void SetVoiceCallMuteForSwitchDevice(); 456 void MuteSinkPort(const std::string &oldSinkName, const std::string &newSinkName, 457 AudioStreamDeviceChangeReasonExt reason); 458 void MutePrimaryOrOffloadSink(const std::string &sinkName, int64_t muteTime); 459 void MuteSinkPortLogic(const std::string &oldSinkName, const std::string &newSinkName, 460 AudioStreamDeviceChangeReasonExt reason); 461 int32_t ActivateOutputDevice(std::shared_ptr<AudioStreamDescriptor> &streamDesc, 462 const AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReasonExt::ExtEnum::UNKNOWN); 463 int32_t ActivateInputDevice(std::shared_ptr<AudioStreamDescriptor> &streamDesc); 464 void OnAudioSceneChange(const AudioScene& audioScene); 465 bool HandleOutputStreamInRunning(std::shared_ptr<AudioStreamDescriptor> &streamDesc, 466 AudioStreamDeviceChangeReasonExt reason); 467 bool HandleInputStreamInRunning(std::shared_ptr<AudioStreamDescriptor> &streamDesc); 468 void HandleDualStartClient(std::vector<std::pair<DeviceType, DeviceFlag>> &activeDevices, 469 std::shared_ptr<AudioStreamDescriptor> &streamDesc); 470 void HandlePlaybackStreamInA2dp(std::shared_ptr<AudioStreamDescriptor> &streamDesc, bool isCreateProcess); 471 bool IsNoRunningStream(std::vector<std::shared_ptr<AudioStreamDescriptor>> outputStreamDescs); 472 void UpdateActiveDeviceAndVolumeBeforeMoveSession(std::vector<std::shared_ptr<AudioStreamDescriptor>> &streamDesc, 473 const AudioStreamDeviceChangeReasonExt reason); 474 void CheckAndSetCurrentOutputDevice(std::shared_ptr<AudioDeviceDescriptor> &desc, int32_t sessionId); 475 void CheckAndSetCurrentInputDevice(std::shared_ptr<AudioDeviceDescriptor> &desc); 476 void ClearRingMuteWhenCallStart(bool pre, bool after); 477 void CheckForRemoteDeviceState(std::shared_ptr<AudioDeviceDescriptor> desc); 478 void UpdateRemoteOffloadModuleName(std::shared_ptr<AudioPipeInfo> pipeInfo, std::string &moduleName); 479 void UpdateOffloadState(std::shared_ptr<AudioPipeInfo> pipeInfo); 480 void NotifyRouteUpdate(const std::vector<std::shared_ptr<AudioStreamDescriptor>> &streamDescs); 481 void ResetNearlinkDeviceState(const std::shared_ptr<AudioDeviceDescriptor> &deviceDesc); 482 void WriteCapturerConcurrentEvent(const std::unique_ptr<ConcurrentCaptureDfxResult> &result); 483 void LogCapturerConcurrentResult(const std::unique_ptr<ConcurrentCaptureDfxResult> &result); 484 bool WriteCapturerConcurrentMsg(std::shared_ptr<AudioStreamDescriptor> streamDesc, 485 const std::unique_ptr<ConcurrentCaptureDfxResult> &result); 486 // for collaboration 487 void UpdateRouteForCollaboration(InternalDeviceType deviceType); 488 void CheckAndUpdateHearingAidCall(const DeviceType deviceType); 489 void CheckModuleForHearingAid(uint32_t &paIndex); 490 void CheckCloseHearingAidCall(const bool isModemCallRunning, const DeviceType type); 491 void CheckOpenHearingAidCall(const bool isModemCallRunning, const DeviceType type); 492 493 private: 494 std::shared_ptr<EventEntry> eventEntry_; 495 std::shared_ptr<AudioPolicyServerHandler> audioPolicyServerHandler_ = nullptr; 496 AudioActiveDevice& audioActiveDevice_; 497 AudioSceneManager& audioSceneManager_; 498 AudioVolumeManager& audioVolumeManager_; 499 AudioCapturerSession& audioCapturerSession_; 500 AudioDeviceManager &audioDeviceManager_; 501 AudioConnectedDevice& audioConnectedDevice_; 502 AudioDeviceStatus& audioDeviceStatus_; 503 AudioEffectService& audioEffectService_; 504 AudioMicrophoneDescriptor& audioMicrophoneDescriptor_; 505 AudioRecoveryDevice& audioRecoveryDevice_; 506 AudioRouterCenter& audioRouterCenter_; 507 AudioStreamCollector& streamCollector_; 508 AudioStateManager &audioStateManager_; 509 AudioDeviceCommon& audioDeviceCommon_; 510 AudioOffloadStream& audioOffloadStream_; 511 AudioA2dpOffloadFlag& audioA2dpOffloadFlag_; 512 IAudioPolicyInterface& audioPolicyManager_; 513 AudioRouteMap& audioRouteMap_; 514 AudioIOHandleMap& audioIOHandleMap_; 515 AudioA2dpDevice& audioA2dpDevice_; 516 AudioEcManager& audioEcManager_; 517 AudioPolicyConfigManager& policyConfigMananger_; 518 AudioAffinityManager &audioAffinityManager_; 519 SleAudioDeviceManager &sleAudioDeviceManager_; 520 std::shared_ptr<AudioPipeSelector> audioPipeSelector_; 521 std::shared_ptr<AudioSessionService> audioSessionService_ = nullptr; 522 523 std::shared_ptr<AudioA2dpOffloadManager> audioA2dpOffloadManager_ = nullptr; 524 std::shared_ptr<DeviceStatusListener> deviceStatusListener_; 525 std::shared_ptr<AudioPipeManager> pipeManager_ = nullptr; 526 527 bool hearingAidCallFlag_ = false; 528 std::shared_ptr<HPAE::IHpaeSoftLink> softLink_ = nullptr; 529 530 // select device history 531 std::mutex hisQueueMutex_; 532 std::deque<std::string> selectDeviceHistory_; 533 534 // dual tone for same sinks 535 std::vector<std::pair<AudioStreamType, StreamUsage>> streamsWhenRingDualOnPrimarySpeaker_; 536 bool isRingDualToneOnPrimarySpeaker_ = false; 537 538 // Save the relationship of uid and session id. 539 std::map<uint32_t, uid_t> sessionIdMap_; 540 std::mutex sessionIdMutex_; 541 542 std::unordered_map<std::string, DeviceType> spatialDeviceMap_; 543 static bool isBtListenerRegistered; 544 static bool isBtCrashed; 545 static constexpr int32_t MIN_SERVICE_COUNT = 2; 546 std::bitset<MIN_SERVICE_COUNT> serviceFlag_; 547 bool isCurrentRemoteRenderer_ = false; 548 bool isOpenRemoteDevice = false; 549 int32_t enableDualHalToneSessionId_ = -1; 550 bool enableDualHalToneState_ = false; 551 int32_t shouldUpdateDeviceDueToDualTone_ = false; 552 bool isFastControlled_ = true; 553 std::mutex serviceFlagMutex_; 554 555 // offload delay release 556 std::atomic<bool> isOffloadOpened_[OFFLOAD_TYPE_NUM] = {}; 557 std::condition_variable offloadCloseCondition_[OFFLOAD_TYPE_NUM]; 558 std::mutex offloadCloseMutex_; 559 std::mutex offloadReOpenMutex_; 560 561 // route update callback 562 std::unordered_map<uint32_t, sptr<IStandardAudioPolicyManagerListener>> routeUpdateCallback_; 563 std::mutex routeUpdateCallbackMutex_; 564 565 DistributedRoutingInfo distributedRoutingInfo_ = { 566 .descriptor = nullptr, 567 .type = CAST_TYPE_NULL 568 }; 569 }; 570 } 571 } 572 #endif // AUDIO_CORE_SERVICE_H 573