• 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 
ResetNearlinkAudioDevicesList()297 void AudioDeviceManager::ResetNearlinkAudioDevicesList()
298 {
299     std::unique_lock<std::mutex> lock(infoMutex_);
300     std::vector<AudioDevice>::iterator it = info_.audioDeviceList.begin();
301     bool hadNearlinkActived = false;
302     while (it != info_.audioDeviceList.end()) {
303         if (it->deviceType == AudioDeviceType::DEVICE_NEARLINK) {
304             hadNearlinkActived = true;
305             it = info_.audioDeviceList.erase(it);
306         } else {
307             ++it;
308         }
309     }
310     if (hadNearlinkActived) {
311         ReportAudioDeviceInfo();
312     }
313     lock.unlock();
314     if (audioDeviceType_ == AudioDeviceType::DEVICE_NEARLINK) {
315         TELEPHONY_LOGI("Nearlink SA removed, init audio device");
316         ProcessEvent(AudioEvent::INIT_AUDIO_DEVICE);
317     }
318     TELEPHONY_LOGI("ResetNearlinkAudioDevicesList success");
319 }
320 
ResetBtHearingAidDeviceList()321 void AudioDeviceManager::ResetBtHearingAidDeviceList()
322 {
323     std::unique_lock<std::mutex> lock(infoMutex_);
324     std::vector<AudioDevice>::iterator it = info_.audioDeviceList.begin();
325     bool hadBtHearingAidActived = false;
326     while (it != info_.audioDeviceList.end()) {
327         if (it->deviceType == AudioDeviceType::DEVICE_BLUETOOTH_HEARING_AID) {
328             hadBtHearingAidActived = true;
329             it = info_.audioDeviceList.erase(it);
330         } else {
331             ++it;
332         }
333     }
334     if (hadBtHearingAidActived) {
335         ReportAudioDeviceInfo();
336     }
337     lock.unlock();
338     if (audioDeviceType_ == AudioDeviceType::DEVICE_BLUETOOTH_HEARING_AID) {
339         TELEPHONY_LOGI("Bluetooth SA removed, init audio device");
340         ProcessEvent(AudioEvent::INIT_AUDIO_DEVICE);
341     }
342     TELEPHONY_LOGI("ResetBtHearingAidDeviceList success");
343 }
344 
InitAudioDevice()345 bool AudioDeviceManager::InitAudioDevice()
346 {
347     // when audio deactivate interrupt , reinit
348     // when external audio device connection state changed , reinit
349     auto device = DelayedSingleton<AudioControlManager>::GetInstance()->GetInitAudioDeviceType();
350     return SwitchDevice(device);
351 }
352 
ProcessEvent(AudioEvent event)353 bool AudioDeviceManager::ProcessEvent(AudioEvent event)
354 {
355     bool result = false;
356     switch (event) {
357         case AudioEvent::AUDIO_ACTIVATED:
358         case AudioEvent::AUDIO_RINGING:
359             if (!isAudioActivated_) {
360                 isAudioActivated_ = true;
361                 AudioDevice device = {
362                     .deviceType = AudioDeviceType::DEVICE_EARPIECE,
363                     .address = { 0 },
364                 };
365                 if (DelayedSingleton<AudioProxy>::GetInstance()->GetPreferredOutputAudioDevice(device) !=
366                     TELEPHONY_SUCCESS) {
367                     TELEPHONY_LOGE("current audio device nullptr");
368                     return false;
369                 }
370                 SetCurrentAudioDevice(device.deviceType);
371             }
372             break;
373         case AudioEvent::AUDIO_DEACTIVATED:
374             if (isAudioActivated_) {
375                 isAudioActivated_ = false;
376                 result = InitAudioDevice();
377             }
378             break;
379         case AudioEvent::INIT_AUDIO_DEVICE:
380             result = InitAudioDevice();
381             break;
382         case AudioEvent::WIRED_HEADSET_DISCONNECTED: {
383             if (!isAudioActivated_) {
384                 TELEPHONY_LOGE("call is not active, no need to connect sco");
385                 return false;
386             }
387             CheckAndSwitchDistributedAudioDevice();
388             break;
389         }
390         default:
391             break;
392     }
393     return result;
394 }
395 
SwitchDevice(AudioEvent event)396 bool AudioDeviceManager::SwitchDevice(AudioEvent event)
397 {
398     auto itFunc = memberFuncMap_.find(event);
399     if (itFunc != memberFuncMap_.end() && itFunc->second != nullptr) {
400         auto memberFunc = itFunc->second;
401         return memberFunc();
402     }
403     return false;
404 }
405 
SwitchDevice(AudioDeviceType device)406 bool AudioDeviceManager::SwitchDevice(AudioDeviceType device)
407 {
408     bool result = false;
409     std::lock_guard<std::mutex> lock(mutex_);
410     switch (device) {
411         case AudioDeviceType::DEVICE_EARPIECE:
412             result = EnableEarpiece();
413             break;
414         case AudioDeviceType::DEVICE_SPEAKER:
415             result = EnableSpeaker();
416             break;
417         case AudioDeviceType::DEVICE_WIRED_HEADSET:
418             result = EnableWiredHeadset();
419             break;
420         case AudioDeviceType::DEVICE_BLUETOOTH_SCO:
421             result = EnableBtSco();
422             break;
423         case AudioDeviceType::DEVICE_DISABLE:
424             result = DisableAll();
425             break;
426         case AudioDeviceType::DEVICE_NEARLINK:
427             result = EnableNearlink();
428             break;
429         case AudioDeviceType::DEVICE_BLUETOOTH_HEARING_AID:
430             result = EnableBtHearingAid();
431             break;
432         default:
433             break;
434     }
435     TELEPHONY_LOGI("switch device lock release");
436     return result;
437 }
438 
EnableSpeaker()439 bool AudioDeviceManager::EnableSpeaker()
440 {
441     if (isSpeakerAvailable_ && DelayedSingleton<AudioProxy>::GetInstance()->SetSpeakerDevActive(true)) {
442         TELEPHONY_LOGI("speaker enabled , current audio device : speaker");
443         SetCurrentAudioDevice(AudioDeviceType::DEVICE_SPEAKER);
444         return true;
445     }
446     TELEPHONY_LOGI("enable speaker device failed");
447     return false;
448 }
449 
EnableEarpiece()450 bool AudioDeviceManager::EnableEarpiece()
451 {
452     if (isEarpieceAvailable_ && DelayedSingleton<AudioProxy>::GetInstance()->SetEarpieceDevActive()) {
453         TELEPHONY_LOGI("earpiece enabled , current audio device : earpiece");
454         SetCurrentAudioDevice(AudioDeviceType::DEVICE_EARPIECE);
455         return true;
456     }
457     TELEPHONY_LOGI("enable earpiece device failed");
458     return false;
459 }
460 
EnableWiredHeadset()461 bool AudioDeviceManager::EnableWiredHeadset()
462 {
463     if (isWiredHeadsetConnected_ && DelayedSingleton<AudioProxy>::GetInstance()->SetWiredHeadsetDevActive()) {
464         TELEPHONY_LOGI("wired headset enabled , current audio device : wired headset");
465         SetCurrentAudioDevice(AudioDeviceType::DEVICE_WIRED_HEADSET);
466         return true;
467     }
468     TELEPHONY_LOGI("enable wired headset device failed");
469     return false;
470 }
471 
EnableBtSco()472 bool AudioDeviceManager::EnableBtSco()
473 {
474     if (IsBtActived()) {
475         TELEPHONY_LOGI("bluetooth sco enabled , current audio device : bluetooth sco");
476         SetCurrentAudioDevice(AudioDeviceType::DEVICE_BLUETOOTH_SCO);
477         return true;
478     }
479     TELEPHONY_LOGI("enable bluetooth sco device failed");
480     return false;
481 }
482 
EnableNearlink()483 bool AudioDeviceManager::EnableNearlink()
484 {
485     AudioDevice device;
486     if (IsNearlinkActived(device)) {
487         TELEPHONY_LOGI("nearlink enabled , current audio device : nearlink");
488         SetCurrentAudioDevice(AudioDeviceType::DEVICE_NEARLINK);
489         return true;
490     }
491     TELEPHONY_LOGI("enable nearlink device failed");
492     return false;
493 }
494 
EnableBtHearingAid()495 bool AudioDeviceManager::EnableBtHearingAid()
496 {
497     AudioDevice device;
498     if (IsBtHearingAidActived(device)) {
499         TELEPHONY_LOGI("bt hearing aid enabled , current audio device : bt hearing aid");
500         SetCurrentAudioDevice(AudioDeviceType::DEVICE_BLUETOOTH_HEARING_AID);
501         return true;
502     }
503     TELEPHONY_LOGI("enable bt hearing aid device failed");
504     return false;
505 }
506 
DisableAll()507 bool AudioDeviceManager::DisableAll()
508 {
509     audioDeviceType_ = AudioDeviceType::DEVICE_UNKNOWN;
510     isBtScoDevEnable_ = false;
511     isDCallDevEnable_ = false;
512     isWiredHeadsetDevEnable_ = false;
513     isSpeakerDevEnable_ = false;
514     isEarpieceDevEnable_ = false;
515     currentAudioDevice_ = std::make_unique<InactiveDeviceState>();
516     if (currentAudioDevice_ == nullptr) {
517         TELEPHONY_LOGE("make_unique InactiveDeviceState failed");
518         return false;
519     }
520     TELEPHONY_LOGI("current audio device : all audio devices disabled");
521     return true;
522 }
523 
SetCurrentAudioDevice(AudioDeviceType deviceType)524 void AudioDeviceManager::SetCurrentAudioDevice(AudioDeviceType deviceType)
525 {
526     TELEPHONY_LOGI("set current audio device, deviceType: %{public}d.", deviceType);
527     if (!IsDistributedAudioDeviceType(deviceType) && IsDistributedAudioDeviceType(audioDeviceType_)) {
528         DelayedSingleton<DistributedCallManager>::GetInstance()->SwitchOffDCallDeviceSync();
529     }
530     if (deviceType == AudioDeviceType::DEVICE_EARPIECE && CallObjectManager::HasSatelliteCallExist()) {
531         audioDeviceType_ = AudioDeviceType::DEVICE_SPEAKER;
532         AudioStandard::AudioSystemManager::GetInstance()->
533             SetDeviceActive(AudioStandard::DeviceType::DEVICE_TYPE_SPEAKER, true);
534         return;
535     }
536     AudioDevice device = {
537         .deviceType = deviceType,
538         .address = { 0 },
539     };
540     SetCurrentAudioDevice(device);
541 }
542 
SetVirtualCall(bool isVirtual)543 bool AudioDeviceManager::SetVirtualCall(bool isVirtual)
544 {
545     TELEPHONY_LOGI("Virtualcall SetVirtualCall: %{public}d.", isVirtual);
546     return AudioStandard::AudioSystemManager::GetInstance()->SetVirtualCall(isVirtual);
547 }
548 
SetCurrentAudioDevice(const AudioDevice & device)549 void AudioDeviceManager::SetCurrentAudioDevice(const AudioDevice &device)
550 {
551     AudioDeviceType deviceType = device.deviceType;
552     TELEPHONY_LOGI("set current audio device, audioDeviceType = %{public}d.", deviceType);
553     if (!IsDistributedAudioDeviceType(deviceType) && IsDistributedAudioDeviceType(audioDeviceType_)) {
554         DelayedSingleton<DistributedCallManager>::GetInstance()->SwitchOffDCallDeviceSync();
555     }
556     audioDeviceType_ = deviceType;
557     ReportAudioDeviceChange(device);
558 }
559 
CheckAndSwitchDistributedAudioDevice()560 bool AudioDeviceManager::CheckAndSwitchDistributedAudioDevice()
561 {
562     TELEPHONY_LOGI("check and switch distributed audio device.");
563     std::lock_guard<std::mutex> lock(infoMutex_);
564     DelayedSingleton<DistributedCallManager>::GetInstance()->SetCallState(true);
565     std::vector<AudioDevice>::iterator it = info_.audioDeviceList.begin();
566     while (it != info_.audioDeviceList.end()) {
567         if (it->deviceType == AudioDeviceType::DEVICE_DISTRIBUTED_AUTOMOTIVE &&
568             DelayedSingleton<DistributedCallManager>::GetInstance()->IsSelectVirtualModem()) {
569             DelayedSingleton<DistributedCallManager>::GetInstance()->SwitchOnDCallDeviceAsync(*it);
570             return true;
571         } else {
572             ++it;
573         }
574     }
575     return false;
576 }
577 
OnActivedCallDisconnected()578 void AudioDeviceManager::OnActivedCallDisconnected()
579 {
580     DelayedSingleton<DistributedCallManager>::GetInstance()->SetCallState(false);
581     DelayedSingleton<DistributedCallManager>::GetInstance()->DealDisconnectCall();
582 }
583 
ReportAudioDeviceChange(const AudioDevice & device)584 int32_t AudioDeviceManager::ReportAudioDeviceChange(const AudioDevice &device)
585 {
586     if (audioDeviceType_ == AudioDeviceType::DEVICE_UNKNOWN) {
587         audioDeviceType_ = DelayedSingleton<AudioControlManager>::GetInstance()->GetInitAudioDeviceType();
588         info_.currentAudioDevice.deviceType = audioDeviceType_;
589     } else {
590         info_.currentAudioDevice.deviceType = audioDeviceType_;
591     }
592     std::string address = device.address;
593     std::string deviceName = device.deviceName;
594     if (audioDeviceType_ == AudioDeviceType::DEVICE_BLUETOOTH_SCO) {
595         UpdateBtDevice(address, deviceName);
596     } else if (DelayedSingleton<DistributedCommunicationManager>::GetInstance()->IsDistributedDev(device)) {
597         TELEPHONY_LOGI("audio device is distributed communication dev");
598     } else if (IsDistributedAudioDeviceType(audioDeviceType_)) {
599         address = DelayedSingleton<DistributedCallManager>::GetInstance()->GetConnectedDCallDeviceAddr();
600     } else if (audioDeviceType_ == AudioDeviceType::DEVICE_NEARLINK) {
601         UpdateNearlinkDevice(address, deviceName);
602     } else if (audioDeviceType_ == AudioDeviceType::DEVICE_BLUETOOTH_HEARING_AID) {
603         UpdateBtHearingAidDevice(address, deviceName);
604     }
605     if (address.length() > kMaxAddressLen) {
606         TELEPHONY_LOGE("address is not too long");
607         return TELEPHONY_ERR_ARGUMENT_INVALID;
608     }
609     if (memset_s(info_.currentAudioDevice.address, kMaxAddressLen + 1, 0, kMaxAddressLen + 1) != EOK) {
610         TELEPHONY_LOGE("failed to memset_s currentAudioDevice.address");
611         return TELEPHONY_ERR_MEMSET_FAIL;
612     }
613     if (memcpy_s(info_.currentAudioDevice.address, kMaxAddressLen, address.c_str(), address.length()) != EOK) {
614         TELEPHONY_LOGE("memcpy_s address fail");
615         return TELEPHONY_ERR_MEMCPY_FAIL;
616     }
617     if (deviceName.length() > kMaxDeviceNameLen) {
618         TELEPHONY_LOGE("deviceName is too long");
619         return TELEPHONY_ERR_ARGUMENT_INVALID;
620     }
621     if (memset_s(info_.currentAudioDevice.deviceName, kMaxDeviceNameLen + 1, 0, kMaxDeviceNameLen + 1) != EOK) {
622         TELEPHONY_LOGE("failed to memset_s currentAudioDevice.deviceName");
623         return TELEPHONY_ERR_MEMSET_FAIL;
624     }
625     if (memcpy_s(info_.currentAudioDevice.deviceName, kMaxDeviceNameLen,
626         deviceName.c_str(), deviceName.length()) != EOK) {
627         TELEPHONY_LOGE("memcpy_s deviceName fail");
628         return TELEPHONY_ERR_MEMCPY_FAIL;
629     }
630     return ReportAudioDeviceInfo();
631 }
632 
UpdateBtDevice(std::string & address,std::string & deviceName)633 void AudioDeviceManager::UpdateBtDevice(std::string &address, std::string &deviceName)
634 {
635     if (!address.empty() && !deviceName.empty()) {
636         return;
637     }
638 
639     std::shared_ptr<AudioStandard::AudioDeviceDescriptor> activeBluetoothDevice =
640         AudioStandard::AudioRoutingManager::GetInstance()->GetActiveBluetoothDevice();
641     if (activeBluetoothDevice != nullptr && !activeBluetoothDevice->macAddress_.empty()) {
642         address = activeBluetoothDevice->macAddress_;
643         deviceName = activeBluetoothDevice->deviceName_;
644     }
645 }
646 
UpdateNearlinkDevice(std::string & address,std::string & deviceName)647 void AudioDeviceManager::UpdateNearlinkDevice(std::string &address, std::string &deviceName)
648 {
649     if (!address.empty() && !deviceName.empty()) {
650         return;
651     }
652 
653     AudioDevice device;
654     if (!IsNearlinkActived(device)) {
655         TELEPHONY_LOGE("Get active nearlink device failed.");
656         return;
657     }
658     address = device.address;
659     deviceName = device.deviceName;
660 }
661 
UpdateBtHearingAidDevice(std::string & address,std::string & deviceName)662 void AudioDeviceManager::UpdateBtHearingAidDevice(std::string &address, std::string &deviceName)
663 {
664     if (!address.empty() && !deviceName.empty()) {
665         return;
666     }
667 
668     AudioDevice device;
669     if (!IsBtHearingAidActived(device)) {
670         TELEPHONY_LOGE("Get active bt hearing aid device failed.");
671         return;
672     }
673     address = device.address;
674     deviceName = device.deviceName;
675 }
676 
ReportAudioDeviceInfo()677 int32_t AudioDeviceManager::ReportAudioDeviceInfo()
678 {
679     sptr<CallBase> liveCall = CallObjectManager::GetAudioLiveCall();
680     return ReportAudioDeviceInfo(liveCall);
681 }
682 
ConvertAddress()683 std::string AudioDeviceManager::ConvertAddress()
684 {
685     std::string addr = info_.currentAudioDevice.address;
686     if (info_.currentAudioDevice.deviceType == AudioDeviceType::DEVICE_BLUETOOTH_SCO ||
687         info_.currentAudioDevice.deviceType == AudioDeviceType::DEVICE_NEARLINK ||
688         info_.currentAudioDevice.deviceType == AudioDeviceType::DEVICE_BLUETOOTH_HEARING_AID) {
689         if (!addr.empty() && addr.length() > DEVICE_ADDR_LEN) {
690             return (addr.substr(0, ADDR_HEAD_VALID_LEN) + ":*:*:*:" +
691                 addr.substr(addr.length() - ADDR_TAIL_VALID_LEN));
692         }
693     } else {
694         addr = "";
695     }
696     return addr;
697 }
698 
ReportAudioDeviceInfo(sptr<CallBase> call)699 int32_t AudioDeviceManager::ReportAudioDeviceInfo(sptr<CallBase> call)
700 {
701     if (call != nullptr && (call->GetCallType() == CallType::TYPE_VOIP ||
702         call->GetCallType() == CallType::TYPE_BLUETOOTH)) {
703         info_.isMuted = call->IsMuted();
704     } else {
705         info_.isMuted = DelayedSingleton<AudioProxy>::GetInstance()->IsMicrophoneMute();
706     }
707     if (call != nullptr) {
708         info_.callId = call->GetCallID();
709     }
710     AudioDeviceType deviceType = info_.currentAudioDevice.deviceType;
711     if (call != nullptr && call->GetCallType() == CallType::TYPE_BLUETOOTH &&
712         (call->GetTelCallState() == TelCallState::CALL_STATUS_ACTIVE ||
713         call->GetTelCallState() == TelCallState::CALL_STATUS_DIALING ||
714         call->GetTelCallState() == TelCallState::CALL_STATUS_ALERTING)) {
715         bool state = DelayedSingleton<BluetoothCallConnection>::GetInstance()->GetBtCallScoConnected();
716         if (state) {
717             info_.currentAudioDevice.deviceType = AudioDeviceType::DEVICE_SPEAKER;
718         } else {
719             info_.currentAudioDevice.deviceType = AudioDeviceType::DEVICE_EARPIECE;
720         }
721     }
722     TELEPHONY_LOGI("report audio device info, currentAudioDeviceType:%{public}d, currentAddress:%{public}s, "
723         "mute:%{public}d, callId:%{public}d", info_.currentAudioDevice.deviceType, ConvertAddress().c_str(),
724         info_.isMuted, info_.callId);
725     int32_t ret = DelayedSingleton<CallAbilityReportProxy>::GetInstance()->ReportAudioDeviceChange(info_);
726     info_.currentAudioDevice.deviceType = deviceType;
727     return ret;
728 }
729 
GetCurrentAudioDevice()730 AudioDeviceType AudioDeviceManager::GetCurrentAudioDevice()
731 {
732     return audioDeviceType_;
733 }
734 
IsEarpieceDevEnable()735 bool AudioDeviceManager::IsEarpieceDevEnable()
736 {
737     return isEarpieceDevEnable_;
738 }
739 
IsWiredHeadsetDevEnable()740 bool AudioDeviceManager::IsWiredHeadsetDevEnable()
741 {
742     return isWiredHeadsetDevEnable_;
743 }
744 
IsSpeakerDevEnable()745 bool AudioDeviceManager::IsSpeakerDevEnable()
746 {
747     return isSpeakerDevEnable_;
748 }
749 
IsBtScoDevEnable()750 bool AudioDeviceManager::IsBtScoDevEnable()
751 {
752     return isBtScoDevEnable_;
753 }
754 
IsDCallDevEnable()755 bool AudioDeviceManager::IsDCallDevEnable()
756 {
757     return isDCallDevEnable_;
758 }
759 
IsBtScoConnected()760 bool AudioDeviceManager::IsBtScoConnected()
761 {
762     return isBtScoConnected_;
763 }
764 
IsBtActived()765 bool AudioDeviceManager::IsBtActived()
766 {
767     std::shared_ptr<AudioStandard::AudioDeviceDescriptor> activeBluetoothDevice =
768         AudioStandard::AudioRoutingManager::GetInstance()->GetActiveBluetoothDevice();
769     if (activeBluetoothDevice != nullptr && !activeBluetoothDevice->macAddress_.empty()) {
770         TELEPHONY_LOGI("has actived bt device");
771         return true;
772     }
773     return false;
774 }
775 
IsNearlinkActived(AudioDevice & device)776 bool AudioDeviceManager::IsNearlinkActived(AudioDevice &device)
777 {
778     if (DelayedSingleton<AudioProxy>::GetInstance()->GetPreferredOutputAudioDevice(device) != TELEPHONY_SUCCESS) {
779         return false;
780     }
781     if (device.deviceType != AudioDeviceType::DEVICE_NEARLINK || strlen(device.address) == 0) {
782         return false;
783     }
784     return true;
785 }
786 
IsBtHearingAidActived(AudioDevice & device)787 bool AudioDeviceManager::IsBtHearingAidActived(AudioDevice &device)
788 {
789     if (DelayedSingleton<AudioProxy>::GetInstance()->GetPreferredOutputAudioDevice(device) != TELEPHONY_SUCCESS) {
790         return false;
791     }
792     if (device.deviceType != AudioDeviceType::DEVICE_BLUETOOTH_HEARING_AID || strlen(device.address) == 0) {
793         return false;
794     }
795     return true;
796 }
797 
IsDistributedCallConnected()798 bool AudioDeviceManager::IsDistributedCallConnected()
799 {
800     return isDCallDevConnected_;
801 }
802 
IsWiredHeadsetConnected()803 bool AudioDeviceManager::IsWiredHeadsetConnected()
804 {
805     return isWiredHeadsetConnected_;
806 }
807 
IsEarpieceAvailable()808 bool AudioDeviceManager::IsEarpieceAvailable()
809 {
810     return isEarpieceAvailable_;
811 }
812 
IsSpeakerAvailable()813 bool AudioDeviceManager::IsSpeakerAvailable()
814 {
815     return isSpeakerAvailable_;
816 }
817 
IsDistributedAudioDeviceType(AudioDeviceType deviceType)818 bool AudioDeviceManager::IsDistributedAudioDeviceType(AudioDeviceType deviceType)
819 {
820     if (((deviceType == AudioDeviceType::DEVICE_DISTRIBUTED_AUTOMOTIVE) ||
821         (deviceType == AudioDeviceType::DEVICE_DISTRIBUTED_PHONE) ||
822         (deviceType == AudioDeviceType::DEVICE_DISTRIBUTED_PAD) ||
823         (deviceType == AudioDeviceType::DEVICE_DISTRIBUTED_PC))) {
824         return true;
825     }
826     return false;
827 }
828 
SetDeviceAvailable(AudioDeviceType deviceType,bool available)829 void AudioDeviceManager::SetDeviceAvailable(AudioDeviceType deviceType, bool available)
830 {
831     switch (deviceType) {
832         case AudioDeviceType::DEVICE_SPEAKER:
833             isSpeakerAvailable_ = available;
834             break;
835         case AudioDeviceType::DEVICE_EARPIECE:
836             isEarpieceAvailable_ = available;
837             break;
838         case AudioDeviceType::DEVICE_BLUETOOTH_SCO:
839             isBtScoConnected_ = available;
840             break;
841         case AudioDeviceType::DEVICE_WIRED_HEADSET:
842             isWiredHeadsetConnected_ = available;
843             break;
844         case AudioDeviceType::DEVICE_DISTRIBUTED_AUTOMOTIVE:
845         case AudioDeviceType::DEVICE_DISTRIBUTED_PHONE:
846         case AudioDeviceType::DEVICE_DISTRIBUTED_PAD:
847         case AudioDeviceType::DEVICE_DISTRIBUTED_PC:
848             isDCallDevConnected_ = available;
849             break;
850         default:
851             break;
852     }
853 }
854 } // namespace Telephony
855 } // namespace OHOS