• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright (c) 2021-2025 Huawei Device Co., Ltd.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef LOG_TAG
17 #define LOG_TAG "AudioActiveDevice"
18 #endif
19 
20 #include "audio_active_device.h"
21 #include <ability_manager_client.h>
22 #include "iservice_registry.h"
23 #include "parameter.h"
24 #include "parameters.h"
25 #include "audio_policy_log.h"
26 #include "audio_inner_call.h"
27 #include "media_monitor_manager.h"
28 
29 #ifdef BLUETOOTH_ENABLE
30 #include "audio_server_death_recipient.h"
31 #include "audio_bluetooth_manager.h"
32 #include "bluetooth_device_manager.h"
33 #endif
34 
35 #include "audio_policy_utils.h"
36 #include "audio_server_proxy.h"
37 #include "sle_audio_device_manager.h"
38 #include "audio_pipe_manager.h"
39 
40 namespace OHOS {
41 namespace AudioStandard {
42 
43 #ifdef BLUETOOTH_ENABLE
44 const uint32_t USER_NOT_SELECT_BT = 1;
45 const uint32_t USER_SELECT_BT = 2;
46 #endif
47 
GetActiveA2dpDeviceStreamInfo(DeviceType deviceType,AudioStreamInfo & streamInfo)48 bool AudioActiveDevice::GetActiveA2dpDeviceStreamInfo(DeviceType deviceType, AudioStreamInfo &streamInfo)
49 {
50     if (deviceType == DEVICE_TYPE_BLUETOOTH_A2DP) {
51         A2dpDeviceConfigInfo info;
52         if (audioA2dpDevice_.GetA2dpDeviceInfo(activeBTDevice_, info)) {
53             streamInfo.samplingRate = *info.streamInfo.samplingRate.rbegin();
54             streamInfo.format = info.streamInfo.format;
55             streamInfo.channels = *info.streamInfo.GetChannels().rbegin();
56             return true;
57         }
58     } else if (deviceType == DEVICE_TYPE_BLUETOOTH_A2DP_IN) {
59         A2dpDeviceConfigInfo info;
60         if (audioA2dpDevice_.GetA2dpInDeviceInfo(activeBTInDevice_, info)) {
61             streamInfo.samplingRate = *info.streamInfo.samplingRate.rbegin();
62             streamInfo.format = info.streamInfo.format;
63             streamInfo.channels = *info.streamInfo.GetChannels().rbegin();
64             return true;
65         }
66     }
67     return false;
68 }
69 
GetActiveBtDeviceMac()70 std::string AudioActiveDevice::GetActiveBtDeviceMac()
71 {
72     return activeBTDevice_;
73 }
74 
SetActiveBtDeviceMac(const std::string macAddress)75 void AudioActiveDevice::SetActiveBtDeviceMac(const std::string macAddress)
76 {
77     activeBTDevice_ = macAddress;
78 }
79 
SetActiveBtInDeviceMac(const std::string macAddress)80 void AudioActiveDevice::SetActiveBtInDeviceMac(const std::string macAddress)
81 {
82     activeBTInDevice_ = macAddress;
83 }
84 
IsDirectSupportedDevice()85 bool AudioActiveDevice::IsDirectSupportedDevice()
86 {
87     DeviceType dev = GetCurrentOutputDeviceType();
88     return dev == DEVICE_TYPE_WIRED_HEADSET || dev == DEVICE_TYPE_USB_HEADSET;
89 }
90 
CheckActiveOutputDeviceSupportOffload()91 bool AudioActiveDevice::CheckActiveOutputDeviceSupportOffload()
92 {
93     DeviceType dev = GetCurrentOutputDeviceType();
94     if (GetCurrentOutputDeviceNetworkId() != LOCAL_NETWORK_ID || dev == DEVICE_TYPE_REMOTE_CAST) {
95         return false;
96     }
97 
98     return dev == DEVICE_TYPE_SPEAKER ||
99         (dev == DEVICE_TYPE_BLUETOOTH_A2DP && audioA2dpOffloadFlag_.GetA2dpOffloadFlag() == A2DP_OFFLOAD) ||
100         dev == DEVICE_TYPE_USB_HEADSET;
101 }
102 
SetCurrentInputDevice(const AudioDeviceDescriptor & desc)103 void AudioActiveDevice::SetCurrentInputDevice(const AudioDeviceDescriptor &desc)
104 {
105     std::lock_guard<std::mutex> lock(curInputDevice_);
106     AUDIO_INFO_LOG("Set as type: %{public}d id: %{public}d", desc.deviceType_, desc.deviceId_);
107     currentActiveInputDevice_ = AudioDeviceDescriptor(desc);
108 }
109 
GetCurrentInputDevice()110 const AudioDeviceDescriptor AudioActiveDevice::GetCurrentInputDevice()
111 {
112     std::lock_guard<std::mutex> lock(curInputDevice_);
113     return currentActiveInputDevice_;
114 }
115 
116 
GetCurrentInputDeviceType()117 DeviceType AudioActiveDevice::GetCurrentInputDeviceType()
118 {
119     std::lock_guard<std::mutex> lock(curInputDevice_);
120     return currentActiveInputDevice_.deviceType_;
121 }
122 
GetCurrentInputDeviceMacAddr()123 std::string AudioActiveDevice::GetCurrentInputDeviceMacAddr()
124 {
125     std::lock_guard<std::mutex> lock(curInputDevice_);
126     return currentActiveDevice_.macAddress_;
127 }
128 
SetCurrentOutputDevice(const AudioDeviceDescriptor & desc)129 void AudioActiveDevice::SetCurrentOutputDevice(const AudioDeviceDescriptor &desc)
130 {
131     std::lock_guard<std::mutex> lock(curOutputDevice_);
132     AUDIO_INFO_LOG("Set as type: %{public}d id: %{public}d", desc.deviceType_, desc.deviceId_);
133     currentActiveDevice_ = AudioDeviceDescriptor(desc);
134 }
135 
GetCurrentOutputDevice()136 const AudioDeviceDescriptor AudioActiveDevice::GetCurrentOutputDevice()
137 {
138     std::lock_guard<std::mutex> lock(curOutputDevice_);
139     return currentActiveDevice_;
140 }
141 
GetCurrentOutputDeviceType()142 DeviceType AudioActiveDevice::GetCurrentOutputDeviceType()
143 {
144     std::lock_guard<std::mutex> lock(curOutputDevice_);
145     return currentActiveDevice_.deviceType_;
146 }
147 
GetCurrentOutputDeviceCategory()148 DeviceCategory AudioActiveDevice::GetCurrentOutputDeviceCategory()
149 {
150     std::lock_guard<std::mutex> lock(curOutputDevice_);
151     return currentActiveDevice_.deviceCategory_;
152 }
153 
GetCurrentOutputDeviceNetworkId()154 std::string AudioActiveDevice::GetCurrentOutputDeviceNetworkId()
155 {
156     std::lock_guard<std::mutex> lock(curOutputDevice_);
157     return currentActiveDevice_.networkId_;
158 }
159 
GetCurrentOutputDeviceMacAddr()160 std::string AudioActiveDevice::GetCurrentOutputDeviceMacAddr()
161 {
162     std::lock_guard<std::mutex> lock(curOutputDevice_);
163     return currentActiveDevice_.macAddress_;
164 }
165 
GetMaxAmplitude(const int32_t deviceId,AudioInterrupt audioInterrupt)166 float AudioActiveDevice::GetMaxAmplitude(const int32_t deviceId, AudioInterrupt audioInterrupt)
167 {
168     AudioDeviceDescriptor descriptor = GetCurrentOutputDevice();
169     if (deviceId == descriptor.deviceId_) {
170         uint32_t sessionId = audioInterrupt.streamId;
171         std::string sinkName = AudioPolicyUtils::GetInstance().GetSinkName(descriptor, static_cast<int32_t>(sessionId));
172         std::string deviceClass = AudioPolicyUtils::GetInstance().GetOutputDeviceClassBySinkPortName(sinkName);
173         return AudioServerProxy::GetInstance().GetMaxAmplitudeProxy(true, deviceClass);
174     }
175 
176     descriptor = GetCurrentInputDevice();
177     if (deviceId == descriptor.deviceId_) {
178         std::string sourceName = AudioPolicyUtils::GetInstance().GetSourcePortName(GetCurrentInputDeviceType());
179         std::string deviceClass = AudioPolicyUtils::GetInstance().GetInputDeviceClassBySourcePortName(sourceName);
180         return AudioServerProxy::GetInstance().GetMaxAmplitudeProxy(false, deviceClass,
181             audioInterrupt.audioFocusType.sourceType);
182     }
183 
184     return 0;
185 }
186 
NotifyUserSelectionEventToBt(std::shared_ptr<AudioDeviceDescriptor> audioDeviceDescriptor,StreamUsage streamUsage)187 void AudioActiveDevice::NotifyUserSelectionEventToBt(std::shared_ptr<AudioDeviceDescriptor> audioDeviceDescriptor,
188     StreamUsage streamUsage)
189 {
190     Trace trace("AudioActiveDevice::NotifyUserSelectionEventToBt");
191     CHECK_AND_RETURN_LOG(audioDeviceDescriptor != nullptr, "audioDeviceDescriptor is nullptr");
192 #ifdef BLUETOOTH_ENABLE
193     auto currentOutputDevice = std::make_shared<AudioDeviceDescriptor>(GetCurrentOutputDevice());
194     CHECK_AND_RETURN_LOG(currentOutputDevice != nullptr, "currentOutputDevice is nullptr");
195 
196     bool isSameDevice = audioDeviceDescriptor->IsSameDeviceDesc(*currentOutputDevice);
197     NotifyUserDisSelectionEventToBt(currentOutputDevice, isSameDevice);
198 
199     if (audioDeviceDescriptor->deviceType_ == DEVICE_TYPE_BLUETOOTH_SCO ||
200         audioDeviceDescriptor->deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP) {
201         Bluetooth::SendUserSelectionEvent(audioDeviceDescriptor->deviceType_,
202             audioDeviceDescriptor->macAddress_, USER_SELECT_BT);
203     }
204     if (audioDeviceDescriptor->deviceType_ == DEVICE_TYPE_NEARLINK) {
205         SleAudioDeviceManager::GetInstance().SendUserSelection(*audioDeviceDescriptor,
206             streamUsage);
207     }
208 #endif
209 }
210 
NotifyUserDisSelectionEventToBt(std::shared_ptr<AudioDeviceDescriptor> audioDeviceDescriptor,bool isSameDevice)211 void AudioActiveDevice::NotifyUserDisSelectionEventToBt(std::shared_ptr<AudioDeviceDescriptor> audioDeviceDescriptor,
212     bool isSameDevice)
213 {
214     CHECK_AND_RETURN_LOG(!isSameDevice, "isSameDevice is true, do not notify");
215     AUDIO_INFO_LOG("UserDisSelection start");
216     CHECK_AND_RETURN_LOG(audioDeviceDescriptor != nullptr, "deviceDesc is nullptr");
217 #ifdef BLUETOOTH_ENABLE
218     Bluetooth::SendUserSelectionEvent(
219         audioDeviceDescriptor->deviceType_, audioDeviceDescriptor->macAddress_, USER_NOT_SELECT_BT);
220     if (audioDeviceDescriptor->deviceType_ == DEVICE_TYPE_BLUETOOTH_SCO &&
221         audioDeviceDescriptor->IsSameDeviceDesc(GetCurrentOutputDevice())) {
222         Bluetooth::AudioHfpManager::DisconnectSco();
223     }
224 #endif
225     SleAudioDeviceManager::GetInstance().SetActiveDevice(audioDeviceDescriptor, STREAM_USAGE_INVALID);
226     SleAudioDeviceManager::GetInstance().SendUserSelection(*audioDeviceDescriptor, STREAM_USAGE_INVALID);
227 }
228 
NotifyUserSelectionEventForInput(std::shared_ptr<AudioDeviceDescriptor> audioDeviceDescriptor,SourceType sourceType)229 void AudioActiveDevice::NotifyUserSelectionEventForInput(std::shared_ptr<AudioDeviceDescriptor> audioDeviceDescriptor,
230     SourceType sourceType)
231 {
232     CHECK_AND_RETURN_LOG(audioDeviceDescriptor != nullptr, "audioDeviceDescriptor is nullptr");
233 #ifdef BLUETOOTH_ENABLE
234     auto curInputDevice = std::make_shared<AudioDeviceDescriptor>(GetCurrentInputDevice());
235     CHECK_AND_RETURN_LOG(curInputDevice != nullptr, "curInputDevice is nullptr");
236 
237     bool isSameDevice = audioDeviceDescriptor->IsSameDeviceDesc(*curInputDevice);
238     NotifyUserDisSelectionEventToBt(curInputDevice, isSameDevice);
239 
240     if (audioDeviceDescriptor->deviceType_ == DEVICE_TYPE_BLUETOOTH_SCO ||
241         audioDeviceDescriptor->deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP_IN) {
242         Bluetooth::SendUserSelectionEvent(audioDeviceDescriptor->deviceType_,
243             audioDeviceDescriptor->macAddress_, USER_SELECT_BT);
244     }
245     if (audioDeviceDescriptor->deviceType_ == DEVICE_TYPE_NEARLINK_IN) {
246         SleAudioDeviceManager::GetInstance().SendUserSelection(*audioDeviceDescriptor,
247             sourceType);
248     }
249 #endif
250 }
251 
WriteOutputRouteChangeEvent(std::shared_ptr<AudioDeviceDescriptor> & desc,const AudioStreamDeviceChangeReason reason)252 void AudioActiveDevice::WriteOutputRouteChangeEvent(std::shared_ptr<AudioDeviceDescriptor> &desc,
253     const AudioStreamDeviceChangeReason reason)
254 {
255     int64_t timeStamp = AudioPolicyUtils::GetInstance().GetCurrentTimeMS();
256     std::shared_ptr<Media::MediaMonitor::EventBean> bean = std::make_shared<Media::MediaMonitor::EventBean>(
257         Media::MediaMonitor::AUDIO, Media::MediaMonitor::AUDIO_ROUTE_CHANGE,
258         Media::MediaMonitor::BEHAVIOR_EVENT);
259     DeviceType curOutputDeviceType = GetCurrentOutputDeviceType();
260     bean->Add("REASON", static_cast<int32_t>(reason));
261     bean->Add("TIMESTAMP", static_cast<uint64_t>(timeStamp));
262     bean->Add("DEVICE_TYPE_BEFORE_CHANGE", curOutputDeviceType);
263     bean->Add("DEVICE_TYPE_AFTER_CHANGE", desc->deviceType_);
264     Media::MediaMonitor::MediaMonitorManager::GetInstance().WriteLogMsg(bean);
265 }
266 
UpdateDevice(std::shared_ptr<AudioDeviceDescriptor> & desc,const AudioStreamDeviceChangeReasonExt reason,const std::shared_ptr<AudioRendererChangeInfo> & rendererChangeInfo)267 bool AudioActiveDevice::UpdateDevice(std::shared_ptr<AudioDeviceDescriptor> &desc,
268     const AudioStreamDeviceChangeReasonExt reason, const std::shared_ptr<AudioRendererChangeInfo> &rendererChangeInfo)
269 {
270     std::shared_ptr<AudioDeviceDescriptor> preferredDesc =
271         audioAffinityManager_.GetRendererDevice(rendererChangeInfo->clientUID);
272     AudioDeviceDescriptor tmpOutputDeviceDesc = GetCurrentOutputDevice();
273     if (((preferredDesc->deviceType_ != DEVICE_TYPE_NONE) && !desc->IsSameDeviceInfo(tmpOutputDeviceDesc)
274         && desc->deviceType_ != preferredDesc->deviceType_)
275         || ((preferredDesc->deviceType_ == DEVICE_TYPE_NONE) && !desc->IsSameDeviceInfo(tmpOutputDeviceDesc))) {
276         WriteOutputRouteChangeEvent(desc, reason);
277         SetCurrentOutputDevice(*desc);
278         AUDIO_DEBUG_LOG("currentActiveDevice update %{public}d", GetCurrentOutputDeviceType());
279         return true;
280     }
281     return false;
282 }
283 
HandleActiveBt(DeviceType deviceType,std::string macAddress)284 void AudioActiveDevice::HandleActiveBt(DeviceType deviceType, std::string macAddress)
285 {
286     if (GetCurrentOutputDeviceType() == DEVICE_TYPE_BLUETOOTH_SCO &&
287         deviceType != DEVICE_TYPE_BLUETOOTH_SCO) {
288         Bluetooth::SendUserSelectionEvent(DEVICE_TYPE_BLUETOOTH_SCO,
289             GetCurrentOutputDeviceMacAddr(), USER_NOT_SELECT_BT);
290         Bluetooth::AudioHfpManager::DisconnectSco();
291     }
292     if (GetCurrentOutputDeviceType() != DEVICE_TYPE_BLUETOOTH_SCO &&
293         deviceType == DEVICE_TYPE_BLUETOOTH_SCO) {
294         Bluetooth::SendUserSelectionEvent(DEVICE_TYPE_BLUETOOTH_SCO,
295             macAddress, USER_SELECT_BT);
296     }
297     if (GetCurrentOutputDeviceType() == DEVICE_TYPE_NEARLINK &&
298         deviceType != DEVICE_TYPE_NEARLINK) {
299         SleAudioDeviceManager::GetInstance().SetActiveDevice(GetCurrentOutputDevice(), STREAM_USAGE_INVALID);
300         SleAudioDeviceManager::GetInstance().SendUserSelection(GetCurrentOutputDevice(), STREAM_USAGE_INVALID);
301     }
302     if (deviceType == DEVICE_TYPE_NEARLINK) {
303         SleAudioDeviceManager::GetInstance().SendUserSelection(GetCurrentOutputDevice(),
304             STREAM_USAGE_VOICE_COMMUNICATION);
305     }
306 }
307 
HandleNegtiveBt(DeviceType deviceType)308 void AudioActiveDevice::HandleNegtiveBt(DeviceType deviceType)
309 {
310     if (GetCurrentOutputDeviceType() == DEVICE_TYPE_BLUETOOTH_SCO &&
311         deviceType == DEVICE_TYPE_BLUETOOTH_SCO) {
312         Bluetooth::SendUserSelectionEvent(DEVICE_TYPE_BLUETOOTH_SCO,
313             GetCurrentOutputDeviceMacAddr(), USER_NOT_SELECT_BT);
314         Bluetooth::AudioHfpManager::DisconnectSco();
315     }
316     if (GetCurrentOutputDeviceType() == DEVICE_TYPE_NEARLINK &&
317         deviceType == DEVICE_TYPE_NEARLINK) {
318         SleAudioDeviceManager::GetInstance().SetActiveDevice(GetCurrentOutputDevice(), STREAM_USAGE_INVALID);
319         SleAudioDeviceManager::GetInstance().SendUserSelection(GetCurrentOutputDevice(), STREAM_USAGE_INVALID);
320     }
321 }
322 
IsDeviceActive(DeviceType deviceType)323 bool AudioActiveDevice::IsDeviceActive(DeviceType deviceType)
324 {
325     AUDIO_DEBUG_LOG("type [%{public}d]", deviceType);
326     CHECK_AND_RETURN_RET(GetCurrentOutputDeviceNetworkId() == LOCAL_NETWORK_ID, false);
327     return GetCurrentOutputDeviceType() == deviceType;
328 }
329 
SetDeviceActive(DeviceType deviceType,bool active,const int32_t uid)330 int32_t AudioActiveDevice::SetDeviceActive(DeviceType deviceType, bool active, const int32_t uid)
331 {
332     CHECK_AND_RETURN_RET_LOG(deviceType != DEVICE_TYPE_NONE, ERR_DEVICE_NOT_SUPPORTED, "Invalid device");
333 
334     // Activate new device if its already connected
335     auto isPresent = [&deviceType] (const std::shared_ptr<AudioDeviceDescriptor> &desc) {
336         CHECK_AND_RETURN_RET_LOG(desc != nullptr, false, "SetDeviceActive::Invalid device descriptor");
337         return ((deviceType == desc->deviceType_) || (deviceType == DEVICE_TYPE_FILE_SINK));
338     };
339 
340     std::vector<std::shared_ptr<AudioDeviceDescriptor>> callDevices
341         = AudioPolicyUtils::GetInstance().GetAvailableDevicesInner(CALL_OUTPUT_DEVICES);
342     std::vector<std::shared_ptr<AudioDeviceDescriptor>> deviceList = {};
343     for (const auto &desc : callDevices) {
344         std::shared_ptr<AudioDeviceDescriptor> devDesc = std::make_shared<AudioDeviceDescriptor>(*desc);
345         deviceList.push_back(devDesc);
346     }
347 
348     auto itr = std::find_if(deviceList.begin(), deviceList.end(), isPresent);
349     CHECK_AND_RETURN_RET_LOG(itr != deviceList.end(), ERR_OPERATION_FAILED,
350         "Requested device not available %{public}d ", deviceType);
351     if (!active) {
352         AudioPolicyUtils::GetInstance().SetPreferredDevice(AUDIO_CALL_RENDER,
353             std::make_shared<AudioDeviceDescriptor>(), uid, "SetDeviceActive");
354 #ifdef BLUETOOTH_ENABLE
355         HandleNegtiveBt(deviceType);
356 #endif
357     } else {
358         AudioPolicyUtils::GetInstance().SetPreferredDevice(AUDIO_CALL_RENDER, *itr, uid, "SetDeviceActive");
359 #ifdef BLUETOOTH_ENABLE
360         HandleActiveBt(deviceType, (*itr)->macAddress_);
361 #endif
362     }
363     return SUCCESS;
364 }
365 
SetCallDeviceActive(DeviceType deviceType,bool active,std::string address,const int32_t uid)366 int32_t AudioActiveDevice::SetCallDeviceActive(DeviceType deviceType, bool active, std::string address,
367     const int32_t uid)
368 {
369     // Activate new device if its already connected
370     auto isPresent = [&deviceType, &address] (const std::shared_ptr<AudioDeviceDescriptor> &desc) {
371         CHECK_AND_RETURN_RET_LOG(desc != nullptr, false, "Invalid device descriptor");
372         return ((deviceType == desc->deviceType_) && (address == desc->macAddress_));
373     };
374     std::vector<std::shared_ptr<AudioDeviceDescriptor>> callDevices
375         = AudioPolicyUtils::GetInstance().GetAvailableDevicesInner(CALL_OUTPUT_DEVICES);
376 
377     auto itr = std::find_if(callDevices.begin(), callDevices.end(), isPresent);
378     CHECK_AND_RETURN_RET_LOG(itr != callDevices.end(), ERR_OPERATION_FAILED,
379         "Requested device not available %{public}d ", deviceType);
380     if (active) {
381         if (deviceType == DEVICE_TYPE_BLUETOOTH_SCO) {
382             (*itr)->isEnable_ = true;
383             audioDeviceManager_.UpdateDevicesListInfo(std::make_shared<AudioDeviceDescriptor>(**itr), ENABLE_UPDATE);
384             AudioPolicyUtils::GetInstance().ClearScoDeviceSuspendState(address);
385         }
386         AudioPolicyUtils::GetInstance().SetPreferredDevice(AUDIO_CALL_RENDER,
387             std::make_shared<AudioDeviceDescriptor>(**itr), uid, "SetCallDeviceActive");
388 #ifdef BLUETOOTH_ENABLE
389         HandleActiveBt(deviceType, (*itr)->macAddress_);
390 #endif
391     } else {
392         AudioPolicyUtils::GetInstance().SetPreferredDevice(AUDIO_CALL_RENDER,
393             std::make_shared<AudioDeviceDescriptor>(), uid, "SetCallDeviceActive");
394 #ifdef BLUETOOTH_ENABLE
395         HandleNegtiveBt(deviceType);
396 #endif
397     }
398     return SUCCESS;
399 }
400 
UpdateActiveDeviceRoute(DeviceType deviceType,DeviceFlag deviceFlag,const std::string & deviceName,std::string networkId)401 void AudioActiveDevice::UpdateActiveDeviceRoute(DeviceType deviceType, DeviceFlag deviceFlag,
402     const std::string &deviceName, std::string networkId)
403 {
404     Trace trace("KeyAction AudioActiveDevice::UpdateActiveDeviceRoute DeviceType:" + std::to_string(deviceType));
405     CHECK_AND_RETURN_LOG(networkId == LOCAL_NETWORK_ID, "distributed device, do not update route");
406     AUDIO_INFO_LOG("[PipeExecInfo] Active route with type[%{public}d]", deviceType);
407     std::vector<std::pair<DeviceType, DeviceFlag>> activeDevices;
408     activeDevices.push_back(make_pair(deviceType, deviceFlag));
409     UpdateActiveDevicesRoute(activeDevices, deviceName);
410 }
411 
UpdateActiveDevicesRoute(std::vector<std::pair<DeviceType,DeviceFlag>> & activeDevices,const std::string & deviceName)412 void AudioActiveDevice::UpdateActiveDevicesRoute(std::vector<std::pair<DeviceType, DeviceFlag>>
413     &activeDevices, const std::string &deviceName)
414 {
415     CHECK_AND_RETURN_LOG(!activeDevices.empty(), "activeDevices is empty.");
416 
417     std::string deviceTypesInfo = "";
418     for (size_t i = 0; i < activeDevices.size(); i++) {
419         deviceTypesInfo = deviceTypesInfo + " " + std::to_string(activeDevices[i].first);
420     }
421     AUDIO_INFO_LOG("[PipeExecInfo] Active route with types[%{public}s]", deviceTypesInfo.c_str());
422 
423     Trace trace("AudioActiveDevice::UpdateActiveDevicesRoute DeviceTypes:" + deviceTypesInfo);
424     auto ret = AudioServerProxy::GetInstance().UpdateActiveDevicesRouteProxy(activeDevices,
425         audioA2dpOffloadFlag_.GetA2dpOffloadFlag(), deviceName);
426     CHECK_AND_RETURN_LOG(ret == SUCCESS, "Failed to update the route for %{public}s", deviceTypesInfo.c_str());
427 }
428 
IsDeviceInVector(std::shared_ptr<AudioDeviceDescriptor> desc,std::vector<std::shared_ptr<AudioDeviceDescriptor>> descs)429 bool AudioActiveDevice::IsDeviceInVector(std::shared_ptr<AudioDeviceDescriptor> desc,
430     std::vector<std::shared_ptr<AudioDeviceDescriptor>> descs)
431 {
432     for (auto &it : descs) {
433         CHECK_AND_RETURN_RET(!it->IsSameDeviceDesc(desc), true);
434     }
435     return false;
436 }
437 
UpdateStreamDeviceMap(std::string source)438 void AudioActiveDevice::UpdateStreamDeviceMap(std::string source)
439 {
440     AUDIO_INFO_LOG("update for %{public}s", source.c_str());
441     std::vector<std::shared_ptr<AudioStreamDescriptor>> descs =
442         AudioPipeManager::GetPipeManager()->GetAllOutputStreamDescs();
443     activeOutputDevices_.clear();
444     for (auto &desc : descs) {
445         CHECK_AND_CONTINUE(desc != nullptr);
446         AUDIO_INFO_LOG("session: %{public}d, calleruid: %{public}d, appuid: %{public}d " \
447             "usage:%{public}d devices:%{public}s",
448             desc->sessionId_, desc->callerUid_, desc->appInfo_.appUid,
449             desc->rendererInfo_.streamUsage, desc->GetNewDevicesInfo().c_str());
450         AudioStreamType streamType = VolumeUtils::GetVolumeTypeFromStreamUsage(desc->rendererInfo_.streamUsage);
451         streamTypeDeviceMap_[streamType] = desc->newDeviceDescs_.back();
452         streamUsageDeviceMap_[desc->rendererInfo_.streamUsage] = desc->newDeviceDescs_.front();
453         for (const auto &device : desc->newDeviceDescs_) {
454             CHECK_AND_CONTINUE(!IsDeviceInVector(device, activeOutputDevices_));
455             activeOutputDevices_.push_back(device);
456         }
457     }
458 
459     for (auto &pair : streamTypeDeviceMap_) {
460         CHECK_AND_CONTINUE(!IsDeviceInVector(pair.second, activeOutputDevices_));
461         streamTypeDeviceMap_[pair.first] = nullptr;
462     }
463 
464     for (auto &pair : streamUsageDeviceMap_) {
465         CHECK_AND_CONTINUE(!IsDeviceInVector(pair.second, activeOutputDevices_));
466         streamUsageDeviceMap_[pair.first] = nullptr;
467     }
468 }
469 }
470 }
471