• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2024 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 #include "audio_device_manager.h"
17 
18 #include "audio_control_manager.h"
19 #include "bluetooth_call_manager.h"
20 #include "bluetooth_device_state.h"
21 #include "call_ability_report_proxy.h"
22 #include "call_object_manager.h"
23 #include "earpiece_device_state.h"
24 #include "inactive_device_state.h"
25 #include "speaker_device_state.h"
26 #include "telephony_log_wrapper.h"
27 #include "wired_headset_device_state.h"
28 #include "distributed_call_manager.h"
29 #include "audio_system_manager.h"
30 #include "audio_device_info.h"
31 #include "distributed_communication_manager.h"
32 #include "bluetooth_call_connection.h"
33 
34 namespace OHOS {
35 namespace Telephony {
36 using namespace AudioStandard;
37 
38 constexpr int32_t DEVICE_ADDR_LEN = 7;
39 constexpr int32_t ADDR_HEAD_VALID_LEN = 5;
40 constexpr int32_t ADDR_TAIL_VALID_LEN = 2;
41 bool AudioDeviceManager::isBtScoDevEnable_ = false;
42 bool AudioDeviceManager::isDCallDevEnable_ = false;
43 bool AudioDeviceManager::isSpeakerAvailable_ = true; // default available
44 bool AudioDeviceManager::isEarpieceAvailable_ = true;
45 bool AudioDeviceManager::isUpdateEarpieceDevice_ = false;
46 bool AudioDeviceManager::isWiredHeadsetConnected_ = false;
47 bool AudioDeviceManager::isBtScoConnected_ = false;
48 bool AudioDeviceManager::isDCallDevConnected_ = false;
49 
AudioDeviceManager()50 AudioDeviceManager::AudioDeviceManager()
51     : audioDeviceType_(AudioDeviceType::DEVICE_UNKNOWN), currentAudioDevice_(nullptr), isAudioActivated_(false)
52 {}
53 
~AudioDeviceManager()54 AudioDeviceManager::~AudioDeviceManager()
55 {
56     memberFuncMap_.clear();
57 }
58 
Init()59 void AudioDeviceManager::Init()
60 {
61     memberFuncMap_[AudioEvent::ENABLE_DEVICE_EARPIECE] = [this]() { return EnableEarpiece(); };
62     memberFuncMap_[AudioEvent::ENABLE_DEVICE_SPEAKER] = [this]() { return EnableSpeaker(); };
63     memberFuncMap_[AudioEvent::ENABLE_DEVICE_WIRED_HEADSET] = [this]() { return EnableWiredHeadset(); };
64     memberFuncMap_[AudioEvent::ENABLE_DEVICE_BLUETOOTH] = [this]() { return EnableBtSco(); };
65     currentAudioDevice_ = std::make_unique<InactiveDeviceState>();
66     if (currentAudioDevice_ == nullptr) {
67         TELEPHONY_LOGE("current audio device nullptr");
68     }
69     if (memset_s(&info_, sizeof(AudioDeviceInfo), 0, sizeof(AudioDeviceInfo)) != EOK) {
70         TELEPHONY_LOGE("memset_s address fail");
71         return;
72     }
73     AudioDevice speaker = {
74         .deviceType = AudioDeviceType::DEVICE_SPEAKER,
75         .address = { 0 },
76     };
77     info_.audioDeviceList.push_back(speaker);
78     AudioDevice earpiece = {
79         .deviceType = AudioDeviceType::DEVICE_EARPIECE,
80         .address = { 0 },
81     };
82     info_.audioDeviceList.push_back(earpiece);
83 }
84 
IsSupportEarpiece()85 bool AudioDeviceManager::IsSupportEarpiece()
86 {
87     std::vector<std::shared_ptr<AudioDeviceDescriptor>> audioDeviceList =
88         AudioStandard::AudioRoutingManager::GetInstance()->GetAvailableDevices(AudioDeviceUsage::CALL_OUTPUT_DEVICES);
89     for (auto& audioDevice : audioDeviceList) {
90         TELEPHONY_LOGI("available deviceType : %{public}d", audioDevice->deviceType_);
91         if (audioDevice->deviceType_ == AudioStandard::DeviceType::DEVICE_TYPE_EARPIECE) {
92             return true;
93         }
94     }
95     return false;
96 }
97 
UpdateEarpieceDevice()98 void AudioDeviceManager::UpdateEarpieceDevice()
99 {
100     if (isUpdateEarpieceDevice_ || IsSupportEarpiece()) {
101         isUpdateEarpieceDevice_ = true;
102         return;
103     }
104     isUpdateEarpieceDevice_ = true;
105     std::lock_guard<std::mutex> lock(infoMutex_);
106     std::vector<AudioDevice>::iterator it = info_.audioDeviceList.begin();
107     while (it != info_.audioDeviceList.end()) {
108         if (it->deviceType == AudioDeviceType::DEVICE_EARPIECE) {
109             it = info_.audioDeviceList.erase(it);
110             TELEPHONY_LOGI("not support Earpice, remove Earpice device success");
111             return;
112         } else {
113             ++it;
114         }
115     }
116 }
117 
UpdateBluetoothDeviceName(const std::string & macAddress,const std::string & deviceName)118 void AudioDeviceManager::UpdateBluetoothDeviceName(const std::string &macAddress, const std::string &deviceName)
119 {
120     std::lock_guard<std::mutex> lock(infoMutex_);
121     std::vector<AudioDevice>::iterator it = info_.audioDeviceList.begin();
122     while (it != info_.audioDeviceList.end()) {
123         if (it->address == macAddress && it->deviceType == AudioDeviceType::DEVICE_BLUETOOTH_SCO) {
124             if (deviceName.length() > kMaxDeviceNameLen) {
125                 TELEPHONY_LOGE("deviceName is too long");
126                 return;
127             }
128             if (memset_s(it->deviceName, sizeof(it->deviceName), 0, sizeof(it->deviceName)) != EOK) {
129                 TELEPHONY_LOGE("memset_s fail");
130                 return;
131             }
132             if (memcpy_s(it->deviceName, kMaxDeviceNameLen, deviceName.c_str(), deviceName.length()) != EOK) {
133                 TELEPHONY_LOGE("memcpy_s deviceName fail");
134                 return;
135             }
136             TELEPHONY_LOGI("UpdateBluetoothDeviceName");
137             ReportAudioDeviceInfo();
138             return;
139         }
140         ++it;
141     }
142 }
143 
AddAudioDeviceList(const std::string & address,AudioDeviceType deviceType,const std::string & deviceName)144 void AudioDeviceManager::AddAudioDeviceList(const std::string &address, AudioDeviceType deviceType,
145     const std::string &deviceName)
146 {
147     std::lock_guard<std::mutex> lock(infoMutex_);
148     std::vector<AudioDevice>::iterator it = info_.audioDeviceList.begin();
149     while (it != info_.audioDeviceList.end()) {
150         if (it->address == address && it->deviceType == deviceType) {
151             TELEPHONY_LOGI("device is already existenced");
152             return;
153         }
154         if (deviceType == AudioDeviceType::DEVICE_WIRED_HEADSET && it->deviceType == AudioDeviceType::DEVICE_EARPIECE) {
155             it = info_.audioDeviceList.erase(it);
156             TELEPHONY_LOGI("remove Earpiece device success");
157         } else {
158             ++it;
159         }
160     }
161     AudioDevice audioDevice;
162     if (memset_s(&audioDevice, sizeof(AudioDevice), 0, sizeof(AudioDevice)) != EOK) {
163         TELEPHONY_LOGE("memset_s fail");
164         return;
165     }
166     audioDevice.deviceType = deviceType;
167     if (address.length() > kMaxAddressLen) {
168         TELEPHONY_LOGE("address is too long");
169         return;
170     }
171     if (memcpy_s(audioDevice.address, kMaxAddressLen, address.c_str(), address.length()) != EOK) {
172         TELEPHONY_LOGE("memcpy_s address fail");
173         return;
174     }
175     if (deviceName.length() > kMaxDeviceNameLen) {
176         TELEPHONY_LOGE("deviceName is too long");
177         return;
178     }
179     if (memcpy_s(audioDevice.deviceName, kMaxDeviceNameLen, deviceName.c_str(), deviceName.length()) != EOK) {
180         TELEPHONY_LOGE("memcpy_s deviceName fail");
181         return;
182     }
183     info_.audioDeviceList.push_back(audioDevice);
184     if (deviceType == AudioDeviceType::DEVICE_WIRED_HEADSET) {
185         SetDeviceAvailable(AudioDeviceType::DEVICE_WIRED_HEADSET, true);
186     }
187     if (deviceType == AudioDeviceType::DEVICE_BLUETOOTH_SCO) {
188         SetDeviceAvailable(AudioDeviceType::DEVICE_BLUETOOTH_SCO, true);
189     }
190     if (IsDistributedAudioDeviceType(deviceType)) {
191         SetDeviceAvailable(deviceType, true);
192     }
193     ReportAudioDeviceInfo();
194     TELEPHONY_LOGI("AddAudioDeviceList success");
195 }
196 
RemoveAudioDeviceList(const std::string & address,AudioDeviceType deviceType)197 void AudioDeviceManager::RemoveAudioDeviceList(const std::string &address, AudioDeviceType deviceType)
198 {
199     std::lock_guard<std::mutex> lock(infoMutex_);
200     bool needAddEarpiece = true;
201     std::vector<AudioDevice>::iterator it = info_.audioDeviceList.begin();
202     while (it != info_.audioDeviceList.end()) {
203         if (it->deviceType == AudioDeviceType::DEVICE_EARPIECE) {
204             needAddEarpiece = false;
205         }
206         if (it->address == address && it->deviceType == deviceType) {
207             it = info_.audioDeviceList.erase(it);
208         } else {
209             ++it;
210         }
211     }
212 
213     bool wiredHeadsetExist = false;
214     bool blueToothScoExist = false;
215     for (auto &elem : info_.audioDeviceList) {
216         if (elem.deviceType == AudioDeviceType::DEVICE_WIRED_HEADSET) {
217             wiredHeadsetExist = true;
218         }
219         if (elem.deviceType == AudioDeviceType::DEVICE_BLUETOOTH_SCO) {
220             blueToothScoExist = true;
221         }
222     }
223     if (deviceType == AudioDeviceType::DEVICE_WIRED_HEADSET && !wiredHeadsetExist) {
224         SetDeviceAvailable(AudioDeviceType::DEVICE_WIRED_HEADSET, false);
225     }
226     if (deviceType == AudioDeviceType::DEVICE_BLUETOOTH_SCO && !blueToothScoExist) {
227         SetDeviceAvailable(AudioDeviceType::DEVICE_BLUETOOTH_SCO, false);
228     }
229     if (IsDistributedAudioDeviceType(deviceType)) {
230         SetDeviceAvailable(deviceType, false);
231     }
232     if (needAddEarpiece && deviceType == AudioDeviceType::DEVICE_WIRED_HEADSET && !wiredHeadsetExist) {
233         AddEarpiece();
234     }
235     sptr<CallBase> liveCall = CallObjectManager::GetAudioLiveCall();
236     if (liveCall != nullptr && (liveCall->GetVideoStateType() == VideoStateType::TYPE_VIDEO ||
237         liveCall->GetCallType() == CallType::TYPE_SATELLITE)) {
238         DelayedSingleton<AudioControlManager>::GetInstance()->UpdateDeviceTypeForVideoOrSatelliteCall();
239     }
240     ReportAudioDeviceInfo();
241     TELEPHONY_LOGI("RemoveAudioDeviceList success");
242 }
243 
AddEarpiece()244 void AudioDeviceManager::AddEarpiece()
245 {
246     if (!IsSupportEarpiece()) {
247         TELEPHONY_LOGI("not support Earpiece device");
248         return;
249     }
250     AudioDevice audioDevice = {
251         .deviceType = AudioDeviceType::DEVICE_EARPIECE,
252         .address = { 0 },
253     };
254     info_.audioDeviceList.push_back(audioDevice);
255     TELEPHONY_LOGI("add Earpiece device success");
256 }
257 
ResetBtAudioDevicesList()258 void AudioDeviceManager::ResetBtAudioDevicesList()
259 {
260     std::lock_guard<std::mutex> lock(infoMutex_);
261     std::vector<AudioDevice>::iterator it = info_.audioDeviceList.begin();
262     bool hadBtActived = false;
263     while (it != info_.audioDeviceList.end()) {
264         if (it->deviceType == AudioDeviceType::DEVICE_BLUETOOTH_SCO) {
265             hadBtActived = true;
266             it = info_.audioDeviceList.erase(it);
267         } else {
268             ++it;
269         }
270     }
271     SetDeviceAvailable(AudioDeviceType::DEVICE_BLUETOOTH_SCO, false);
272     if (hadBtActived) {
273         ReportAudioDeviceInfo();
274     }
275     TELEPHONY_LOGI("ResetBtAudioDevicesList success");
276 }
277 
ResetDistributedCallDevicesList()278 void AudioDeviceManager::ResetDistributedCallDevicesList()
279 {
280     std::lock_guard<std::mutex> lock(infoMutex_);
281     std::vector<AudioDevice>::iterator it = info_.audioDeviceList.begin();
282     while (it != info_.audioDeviceList.end()) {
283         if (IsDistributedAudioDeviceType(it->deviceType)) {
284             it = info_.audioDeviceList.erase(it);
285         } else {
286             ++it;
287         }
288     }
289     SetDeviceAvailable(AudioDeviceType::DEVICE_DISTRIBUTED_AUTOMOTIVE, false);
290     SetDeviceAvailable(AudioDeviceType::DEVICE_DISTRIBUTED_PHONE, false);
291     SetDeviceAvailable(AudioDeviceType::DEVICE_DISTRIBUTED_PAD, false);
292     SetDeviceAvailable(AudioDeviceType::DEVICE_DISTRIBUTED_PC, false);
293     DelayedSingleton<CallAbilityReportProxy>::GetInstance()->ReportAudioDeviceChange(info_);
294     TELEPHONY_LOGI("Reset Distributed Audio Devices List success");
295 }
296 
InitAudioDevice()297 bool AudioDeviceManager::InitAudioDevice()
298 {
299     // when audio deactivate interrupt , reinit
300     // when external audio device connection state changed , reinit
301     auto device = DelayedSingleton<AudioControlManager>::GetInstance()->GetInitAudioDeviceType();
302     return SwitchDevice(device);
303 }
304 
ProcessEvent(AudioEvent event)305 bool AudioDeviceManager::ProcessEvent(AudioEvent event)
306 {
307     bool result = false;
308     switch (event) {
309         case AudioEvent::AUDIO_ACTIVATED:
310         case AudioEvent::AUDIO_RINGING:
311             if (!isAudioActivated_) {
312                 isAudioActivated_ = true;
313                 AudioDevice device = {
314                     .deviceType = AudioDeviceType::DEVICE_EARPIECE,
315                     .address = { 0 },
316                 };
317                 if (DelayedSingleton<AudioProxy>::GetInstance()->GetPreferredOutputAudioDevice(device) !=
318                     TELEPHONY_SUCCESS) {
319                     TELEPHONY_LOGE("current audio device nullptr");
320                     return false;
321                 }
322                 SetCurrentAudioDevice(device.deviceType);
323             }
324             break;
325         case AudioEvent::AUDIO_DEACTIVATED:
326             if (isAudioActivated_) {
327                 isAudioActivated_ = false;
328                 result = InitAudioDevice();
329             }
330             break;
331         case AudioEvent::INIT_AUDIO_DEVICE:
332             result = InitAudioDevice();
333             break;
334         case AudioEvent::WIRED_HEADSET_DISCONNECTED: {
335             if (!isAudioActivated_) {
336                 TELEPHONY_LOGE("call is not active, no need to connect sco");
337                 return false;
338             }
339             break;
340         }
341         default:
342             break;
343     }
344     return result;
345 }
346 
SwitchDevice(AudioEvent event)347 bool AudioDeviceManager::SwitchDevice(AudioEvent event)
348 {
349     auto itFunc = memberFuncMap_.find(event);
350     if (itFunc != memberFuncMap_.end() && itFunc->second != nullptr) {
351         auto memberFunc = itFunc->second;
352         return memberFunc();
353     }
354     return false;
355 }
356 
SwitchDevice(AudioDeviceType device)357 bool AudioDeviceManager::SwitchDevice(AudioDeviceType device)
358 {
359     bool result = false;
360     std::lock_guard<std::mutex> lock(mutex_);
361     switch (device) {
362         case AudioDeviceType::DEVICE_EARPIECE:
363             result = EnableEarpiece();
364             break;
365         case AudioDeviceType::DEVICE_SPEAKER:
366             result = EnableSpeaker();
367             break;
368         case AudioDeviceType::DEVICE_WIRED_HEADSET:
369             result = EnableWiredHeadset();
370             break;
371         case AudioDeviceType::DEVICE_BLUETOOTH_SCO:
372             result = EnableBtSco();
373             break;
374         case AudioDeviceType::DEVICE_DISABLE:
375             result = DisableAll();
376             break;
377         default:
378             break;
379     }
380     TELEPHONY_LOGI("switch device lock release");
381     return result;
382 }
383 
EnableSpeaker()384 bool AudioDeviceManager::EnableSpeaker()
385 {
386     if (isSpeakerAvailable_ && DelayedSingleton<AudioProxy>::GetInstance()->SetSpeakerDevActive(true)) {
387         TELEPHONY_LOGI("speaker enabled , current audio device : speaker");
388         SetCurrentAudioDevice(AudioDeviceType::DEVICE_SPEAKER);
389         return true;
390     }
391     TELEPHONY_LOGI("enable speaker device failed");
392     return false;
393 }
394 
EnableEarpiece()395 bool AudioDeviceManager::EnableEarpiece()
396 {
397     if (isEarpieceAvailable_ && DelayedSingleton<AudioProxy>::GetInstance()->SetEarpieceDevActive()) {
398         TELEPHONY_LOGI("earpiece enabled , current audio device : earpiece");
399         SetCurrentAudioDevice(AudioDeviceType::DEVICE_EARPIECE);
400         return true;
401     }
402     TELEPHONY_LOGI("enable earpiece device failed");
403     return false;
404 }
405 
EnableWiredHeadset()406 bool AudioDeviceManager::EnableWiredHeadset()
407 {
408     if (isWiredHeadsetConnected_ && DelayedSingleton<AudioProxy>::GetInstance()->SetWiredHeadsetDevActive()) {
409         TELEPHONY_LOGI("wired headset enabled , current audio device : wired headset");
410         SetCurrentAudioDevice(AudioDeviceType::DEVICE_WIRED_HEADSET);
411         return true;
412     }
413     TELEPHONY_LOGI("enable wired headset device failed");
414     return false;
415 }
416 
EnableBtSco()417 bool AudioDeviceManager::EnableBtSco()
418 {
419     if (IsBtActived()) {
420         TELEPHONY_LOGI("bluetooth sco enabled , current audio device : bluetooth sco");
421         SetCurrentAudioDevice(AudioDeviceType::DEVICE_BLUETOOTH_SCO);
422         return true;
423     }
424     TELEPHONY_LOGI("enable bluetooth sco device failed");
425     return false;
426 }
427 
DisableAll()428 bool AudioDeviceManager::DisableAll()
429 {
430     audioDeviceType_ = AudioDeviceType::DEVICE_UNKNOWN;
431     isBtScoDevEnable_ = false;
432     isDCallDevEnable_ = false;
433     isWiredHeadsetDevEnable_ = false;
434     isSpeakerDevEnable_ = false;
435     isEarpieceDevEnable_ = false;
436     currentAudioDevice_ = std::make_unique<InactiveDeviceState>();
437     if (currentAudioDevice_ == nullptr) {
438         TELEPHONY_LOGE("make_unique InactiveDeviceState failed");
439         return false;
440     }
441     TELEPHONY_LOGI("current audio device : all audio devices disabled");
442     return true;
443 }
444 
SetCurrentAudioDevice(AudioDeviceType deviceType)445 void AudioDeviceManager::SetCurrentAudioDevice(AudioDeviceType deviceType)
446 {
447     TELEPHONY_LOGI("set current audio device, deviceType: %{public}d.", deviceType);
448     if (!IsDistributedAudioDeviceType(deviceType) && IsDistributedAudioDeviceType(audioDeviceType_)) {
449         DelayedSingleton<DistributedCallManager>::GetInstance()->SwitchOffDCallDeviceSync();
450     }
451     if (deviceType == AudioDeviceType::DEVICE_EARPIECE && CallObjectManager::HasSatelliteCallExist()) {
452         audioDeviceType_ = AudioDeviceType::DEVICE_SPEAKER;
453         AudioStandard::AudioSystemManager::GetInstance()->
454             SetDeviceActive(AudioStandard::DeviceType::DEVICE_TYPE_SPEAKER, true);
455         return;
456     }
457     AudioDevice device = {
458         .deviceType = deviceType,
459         .address = { 0 },
460     };
461     SetCurrentAudioDevice(device);
462 }
463 
SetVirtualCall(bool isVirtual)464 bool AudioDeviceManager::SetVirtualCall(bool isVirtual)
465 {
466     TELEPHONY_LOGI("Virtualcall SetVirtualCall: %{public}d.", isVirtual);
467     return AudioStandard::AudioSystemManager::GetInstance()->SetVirtualCall(isVirtual);
468 }
469 
SetCurrentAudioDevice(const AudioDevice & device)470 void AudioDeviceManager::SetCurrentAudioDevice(const AudioDevice &device)
471 {
472     AudioDeviceType deviceType = device.deviceType;
473     TELEPHONY_LOGI("set current audio device, audioDeviceType = %{public}d.", deviceType);
474     if (!IsDistributedAudioDeviceType(deviceType) && IsDistributedAudioDeviceType(audioDeviceType_)) {
475         DelayedSingleton<DistributedCallManager>::GetInstance()->SwitchOffDCallDeviceSync();
476     }
477     audioDeviceType_ = deviceType;
478     ReportAudioDeviceChange(device);
479 }
480 
CheckAndSwitchDistributedAudioDevice()481 bool AudioDeviceManager::CheckAndSwitchDistributedAudioDevice()
482 {
483     TELEPHONY_LOGI("check and switch distributed audio device.");
484     std::lock_guard<std::mutex> lock(infoMutex_);
485     DelayedSingleton<DistributedCallManager>::GetInstance()->SetCallState(true);
486     std::vector<AudioDevice>::iterator it = info_.audioDeviceList.begin();
487     while (it != info_.audioDeviceList.end()) {
488         if (it->deviceType == AudioDeviceType::DEVICE_DISTRIBUTED_AUTOMOTIVE &&
489             DelayedSingleton<DistributedCallManager>::GetInstance()->IsSelectVirtualModem()) {
490             DelayedSingleton<DistributedCallManager>::GetInstance()->SwitchOnDCallDeviceAsync(*it);
491             return true;
492         } else {
493             ++it;
494         }
495     }
496     return false;
497 }
498 
OnActivedCallDisconnected()499 void AudioDeviceManager::OnActivedCallDisconnected()
500 {
501     DelayedSingleton<DistributedCallManager>::GetInstance()->SetCallState(false);
502     DelayedSingleton<DistributedCallManager>::GetInstance()->DealDisconnectCall();
503 }
504 
ReportAudioDeviceChange(const AudioDevice & device)505 int32_t AudioDeviceManager::ReportAudioDeviceChange(const AudioDevice &device)
506 {
507     if (audioDeviceType_ == AudioDeviceType::DEVICE_UNKNOWN) {
508         audioDeviceType_ = DelayedSingleton<AudioControlManager>::GetInstance()->GetInitAudioDeviceType();
509         info_.currentAudioDevice.deviceType = audioDeviceType_;
510     } else {
511         info_.currentAudioDevice.deviceType = audioDeviceType_;
512     }
513     std::string address = device.address;
514     std::string deviceName = device.deviceName;
515     if (audioDeviceType_ == AudioDeviceType::DEVICE_BLUETOOTH_SCO) {
516         if (address.empty() || deviceName.empty()) {
517             std::shared_ptr<AudioStandard::AudioDeviceDescriptor> activeBluetoothDevice =
518                 AudioStandard::AudioRoutingManager::GetInstance()->GetActiveBluetoothDevice();
519             if (activeBluetoothDevice != nullptr && !activeBluetoothDevice->macAddress_.empty()) {
520                 address = activeBluetoothDevice->macAddress_;
521                 deviceName = activeBluetoothDevice->deviceName_;
522             }
523         }
524     } else if (DelayedSingleton<DistributedCommunicationManager>::GetInstance()->IsDistributedDev(device)) {
525         TELEPHONY_LOGI("audio device is distributed communication dev");
526     } else if (IsDistributedAudioDeviceType(audioDeviceType_)) {
527         address = DelayedSingleton<DistributedCallManager>::GetInstance()->GetConnectedDCallDeviceAddr();
528     }
529     if (address.length() > kMaxAddressLen) {
530         TELEPHONY_LOGE("address is not too long");
531         return TELEPHONY_ERR_ARGUMENT_INVALID;
532     }
533     if (memset_s(info_.currentAudioDevice.address, kMaxAddressLen + 1, 0, kMaxAddressLen + 1) != EOK) {
534         TELEPHONY_LOGE("failed to memset_s currentAudioDevice.address");
535         return TELEPHONY_ERR_MEMSET_FAIL;
536     }
537     if (memcpy_s(info_.currentAudioDevice.address, kMaxAddressLen, address.c_str(), address.length()) != EOK) {
538         TELEPHONY_LOGE("memcpy_s address fail");
539         return TELEPHONY_ERR_MEMCPY_FAIL;
540     }
541     if (deviceName.length() > kMaxDeviceNameLen) {
542         TELEPHONY_LOGE("deviceName is too long");
543         return TELEPHONY_ERR_ARGUMENT_INVALID;
544     }
545     if (memset_s(info_.currentAudioDevice.deviceName, kMaxDeviceNameLen + 1, 0, kMaxDeviceNameLen + 1) != EOK) {
546         TELEPHONY_LOGE("failed to memset_s currentAudioDevice.deviceName");
547         return TELEPHONY_ERR_MEMSET_FAIL;
548     }
549     if (memcpy_s(info_.currentAudioDevice.deviceName, kMaxDeviceNameLen,
550         deviceName.c_str(), deviceName.length()) != EOK) {
551         TELEPHONY_LOGE("memcpy_s deviceName fail");
552         return TELEPHONY_ERR_MEMCPY_FAIL;
553     }
554     return ReportAudioDeviceInfo();
555 }
556 
ReportAudioDeviceInfo()557 int32_t AudioDeviceManager::ReportAudioDeviceInfo()
558 {
559     sptr<CallBase> liveCall = CallObjectManager::GetAudioLiveCall();
560     return ReportAudioDeviceInfo(liveCall);
561 }
562 
ConvertAddress()563 std::string AudioDeviceManager::ConvertAddress()
564 {
565     std::string addr = info_.currentAudioDevice.address;
566     if (info_.currentAudioDevice.deviceType == AudioDeviceType::DEVICE_BLUETOOTH_SCO) {
567         if (!addr.empty() && addr.length() > DEVICE_ADDR_LEN) {
568             return (addr.substr(0, ADDR_HEAD_VALID_LEN) + ":*:*:*:" +
569                 addr.substr(addr.length() - ADDR_TAIL_VALID_LEN));
570         }
571     } else {
572         addr = "";
573     }
574     return addr;
575 }
576 
ReportAudioDeviceInfo(sptr<CallBase> call)577 int32_t AudioDeviceManager::ReportAudioDeviceInfo(sptr<CallBase> call)
578 {
579     if (call != nullptr && call->GetCallType() == CallType::TYPE_VOIP) {
580         info_.isMuted = call->IsMuted();
581     } else {
582         info_.isMuted = DelayedSingleton<AudioProxy>::GetInstance()->IsMicrophoneMute();
583     }
584     if (call != nullptr) {
585         info_.callId = call->GetCallID();
586     }
587     AudioDeviceType deviceType = info_.currentAudioDevice.deviceType;
588     if (call != nullptr && call->GetCallType() == CallType::TYPE_BLUETOOTH &&
589         (call->GetTelCallState() == TelCallState::CALL_STATUS_ACTIVE ||
590         call->GetTelCallState() == TelCallState::CALL_STATUS_DIALING ||
591         call->GetTelCallState() == TelCallState::CALL_STATUS_ALERTING)) {
592         bool state = DelayedSingleton<BluetoothCallConnection>::GetInstance()->GetBtCallScoConnected();
593         if (state) {
594             info_.currentAudioDevice.deviceType = AudioDeviceType::DEVICE_SPEAKER;
595         } else {
596             info_.currentAudioDevice.deviceType = AudioDeviceType::DEVICE_EARPIECE;
597         }
598     }
599     TELEPHONY_LOGI("report audio device info, currentAudioDeviceType:%{public}d, currentAddress:%{public}s, "
600         "mute:%{public}d, callId:%{public}d", info_.currentAudioDevice.deviceType, ConvertAddress().c_str(),
601         info_.isMuted, info_.callId);
602     int32_t ret = DelayedSingleton<CallAbilityReportProxy>::GetInstance()->ReportAudioDeviceChange(info_);
603     info_.currentAudioDevice.deviceType = deviceType;
604     return ret;
605 }
606 
GetCurrentAudioDevice()607 AudioDeviceType AudioDeviceManager::GetCurrentAudioDevice()
608 {
609     return audioDeviceType_;
610 }
611 
IsEarpieceDevEnable()612 bool AudioDeviceManager::IsEarpieceDevEnable()
613 {
614     return isEarpieceDevEnable_;
615 }
616 
IsWiredHeadsetDevEnable()617 bool AudioDeviceManager::IsWiredHeadsetDevEnable()
618 {
619     return isWiredHeadsetDevEnable_;
620 }
621 
IsSpeakerDevEnable()622 bool AudioDeviceManager::IsSpeakerDevEnable()
623 {
624     return isSpeakerDevEnable_;
625 }
626 
IsBtScoDevEnable()627 bool AudioDeviceManager::IsBtScoDevEnable()
628 {
629     return isBtScoDevEnable_;
630 }
631 
IsDCallDevEnable()632 bool AudioDeviceManager::IsDCallDevEnable()
633 {
634     return isDCallDevEnable_;
635 }
636 
IsBtScoConnected()637 bool AudioDeviceManager::IsBtScoConnected()
638 {
639     return isBtScoConnected_;
640 }
641 
IsBtActived()642 bool AudioDeviceManager::IsBtActived()
643 {
644     std::shared_ptr<AudioStandard::AudioDeviceDescriptor> activeBluetoothDevice =
645         AudioStandard::AudioRoutingManager::GetInstance()->GetActiveBluetoothDevice();
646     if (activeBluetoothDevice != nullptr && !activeBluetoothDevice->macAddress_.empty()) {
647         TELEPHONY_LOGI("has actived bt device");
648         return true;
649     }
650     return false;
651 }
652 
IsDistributedCallConnected()653 bool AudioDeviceManager::IsDistributedCallConnected()
654 {
655     return isDCallDevConnected_;
656 }
657 
IsWiredHeadsetConnected()658 bool AudioDeviceManager::IsWiredHeadsetConnected()
659 {
660     return isWiredHeadsetConnected_;
661 }
662 
IsEarpieceAvailable()663 bool AudioDeviceManager::IsEarpieceAvailable()
664 {
665     return isEarpieceAvailable_;
666 }
667 
IsSpeakerAvailable()668 bool AudioDeviceManager::IsSpeakerAvailable()
669 {
670     return isSpeakerAvailable_;
671 }
672 
IsDistributedAudioDeviceType(AudioDeviceType deviceType)673 bool AudioDeviceManager::IsDistributedAudioDeviceType(AudioDeviceType deviceType)
674 {
675     if (((deviceType == AudioDeviceType::DEVICE_DISTRIBUTED_AUTOMOTIVE) ||
676         (deviceType == AudioDeviceType::DEVICE_DISTRIBUTED_PHONE) ||
677         (deviceType == AudioDeviceType::DEVICE_DISTRIBUTED_PAD) ||
678         (deviceType == AudioDeviceType::DEVICE_DISTRIBUTED_PC))) {
679         return true;
680     }
681     return false;
682 }
683 
SetDeviceAvailable(AudioDeviceType deviceType,bool available)684 void AudioDeviceManager::SetDeviceAvailable(AudioDeviceType deviceType, bool available)
685 {
686     switch (deviceType) {
687         case AudioDeviceType::DEVICE_SPEAKER:
688             isSpeakerAvailable_ = available;
689             break;
690         case AudioDeviceType::DEVICE_EARPIECE:
691             isEarpieceAvailable_ = available;
692             break;
693         case AudioDeviceType::DEVICE_BLUETOOTH_SCO:
694             isBtScoConnected_ = available;
695             break;
696         case AudioDeviceType::DEVICE_WIRED_HEADSET:
697             isWiredHeadsetConnected_ = available;
698             break;
699         case AudioDeviceType::DEVICE_DISTRIBUTED_AUTOMOTIVE:
700         case AudioDeviceType::DEVICE_DISTRIBUTED_PHONE:
701         case AudioDeviceType::DEVICE_DISTRIBUTED_PAD:
702         case AudioDeviceType::DEVICE_DISTRIBUTED_PC:
703             isDCallDevConnected_ = available;
704             break;
705         default:
706             break;
707     }
708 }
709 } // namespace Telephony
710 } // namespace OHOS