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