1 /*
2 * Copyright (C) 2021 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 "telephony_log_wrapper.h"
19
20 #include "audio_control_manager.h"
21 #include "inactive_device_state.h"
22 #include "bluetooth_device_state.h"
23 #include "earpiece_device_state.h"
24 #include "speaker_device_state.h"
25 #include "wired_headset_device_state.h"
26 #include "bluetooth_call_manager.h"
27
28 namespace OHOS {
29 namespace Telephony {
30 bool AudioDeviceManager::isBtScoDevEnable_ = false;
31 bool AudioDeviceManager::isSpeakerAvailable_ = true; // default available
32 bool AudioDeviceManager::isEarpieceAvailable_ = false;
33 bool AudioDeviceManager::isWiredHeadsetConnected_ = false;
34 bool AudioDeviceManager::isBtScoConnected_ = false;
35
AudioDeviceManager()36 AudioDeviceManager::AudioDeviceManager()
37 : audioDevice_(AudioDevice::DEVICE_UNKNOWN), currentAudioDevice_(nullptr), isAudioActivated_(false)
38 {}
39
~AudioDeviceManager()40 AudioDeviceManager::~AudioDeviceManager()
41 {
42 memberFuncMap_.clear();
43 }
44
Init()45 void AudioDeviceManager::Init()
46 {
47 memberFuncMap_[AudioEvent::ENABLE_DEVICE_EARPIECE] = &AudioDeviceManager::EnableEarpiece;
48 memberFuncMap_[AudioEvent::ENABLE_DEVICE_SPEAKER] = &AudioDeviceManager::EnableSpeaker;
49 memberFuncMap_[AudioEvent::ENABLE_DEVICE_WIRED_HEADSET] = &AudioDeviceManager::EnableWiredHeadset;
50 memberFuncMap_[AudioEvent::ENABLE_DEVICE_BLUETOOTH] = &AudioDeviceManager::EnableBtSco;
51 currentAudioDevice_ = std::make_unique<InactiveDeviceState>();
52 if (currentAudioDevice_ == nullptr) {
53 TELEPHONY_LOGE("current audio device nullptr");
54 }
55 }
56
InitAudioDevice()57 bool AudioDeviceManager::InitAudioDevice()
58 {
59 // when audio deactivate interrupt , reinit
60 // when external audio device connection state changed , reinit
61 auto device = DelayedSingleton<AudioControlManager>::GetInstance()->GetInitAudioDevice();
62 return SwitchDevice(device);
63 }
64
ProcessEvent(AudioEvent event)65 bool AudioDeviceManager::ProcessEvent(AudioEvent event)
66 {
67 if (currentAudioDevice_ == nullptr) {
68 TELEPHONY_LOGE("current audio device nullptr");
69 return false;
70 }
71 bool result = false;
72 switch (event) {
73 case AudioEvent::ENABLE_DEVICE_EARPIECE:
74 case AudioEvent::ENABLE_DEVICE_SPEAKER:
75 case AudioEvent::ENABLE_DEVICE_BLUETOOTH:
76 case AudioEvent::ENABLE_DEVICE_WIRED_HEADSET:
77 result = SwitchDevice(event);
78 break;
79 case AudioEvent::AUDIO_ACTIVATED:
80 case AudioEvent::AUDIO_RINGING:
81 if (!isAudioActivated_) {
82 isAudioActivated_ = true;
83 std::shared_ptr<BluetoothCallManager> bluetoothCallManager = std::make_shared<BluetoothCallManager>();
84 // Gets whether the device can be started from the configuration
85 if (bluetoothCallManager->IsBtAvailble()) {
86 return DelayedSingleton<BluetoothConnection>::GetInstance()->ConnectBtSco();
87 }
88 result = currentAudioDevice_->ProcessEvent(event);
89 }
90 break;
91 case AudioEvent::AUDIO_DEACTIVATED:
92 if (isAudioActivated_) {
93 isAudioActivated_ = false;
94 result = InitAudioDevice();
95 }
96 break;
97 case AudioEvent::BLUETOOTH_SCO_CONNECTED:
98 isBtScoConnected_ = true;
99 currentAudioDevice_ = std::make_unique<BluetoothDeviceState>();
100 if (currentAudioDevice_ == nullptr) {
101 return false;
102 }
103 result = currentAudioDevice_->ProcessEvent(event);
104 break;
105 case AudioEvent::BLUETOOTH_SCO_DISCONNECTED:
106 isBtScoConnected_ = false;
107 result = currentAudioDevice_->ProcessEvent(event);
108 break;
109 case AudioEvent::INIT_AUDIO_DEVICE:
110 result = InitAudioDevice();
111 break;
112 default:
113 break;
114 }
115 return result;
116 }
117
SwitchDevice(AudioEvent event)118 bool AudioDeviceManager::SwitchDevice(AudioEvent event)
119 {
120 auto itFunc = memberFuncMap_.find(event);
121 if (itFunc != memberFuncMap_.end() && itFunc->second != nullptr) {
122 auto memberFunc = itFunc->second;
123 return (this->*memberFunc)();
124 }
125 return false;
126 }
127
ConnectBtScoWithAddress(const std::string & bluetoothAddress)128 bool AudioDeviceManager::ConnectBtScoWithAddress(const std::string &bluetoothAddress)
129 {
130 std::shared_ptr<BluetoothCallManager> bluetoothCallManager = std::make_shared<BluetoothCallManager>();
131 if (bluetoothCallManager->ConnectBtSco(bluetoothAddress)) {
132 return true;
133 }
134 return false;
135 }
136
SwitchDevice(AudioDevice device)137 bool AudioDeviceManager::SwitchDevice(AudioDevice device)
138 {
139 bool result = false;
140 std::lock_guard<std::mutex> lock(mutex_);
141 switch (device) {
142 case AudioDevice::DEVICE_EARPIECE:
143 result = EnableEarpiece();
144 break;
145 case AudioDevice::DEVICE_SPEAKER:
146 result = EnableSpeaker();
147 break;
148 case AudioDevice::DEVICE_WIRED_HEADSET:
149 result = EnableWiredHeadset();
150 break;
151 case AudioDevice::DEVICE_BLUETOOTH_SCO:
152 result = EnableBtSco();
153 break;
154 case AudioDevice::DEVICE_DISABLE:
155 result = DisableAll();
156 break;
157 default:
158 break;
159 }
160 TELEPHONY_LOGI("switch device lock release");
161 return result;
162 }
163
EnableSpeaker()164 bool AudioDeviceManager::EnableSpeaker()
165 {
166 if (isSpeakerAvailable_ && DelayedSingleton<AudioProxy>::GetInstance()->SetSpeakerDevActive()) {
167 currentAudioDevice_ = std::make_unique<SpeakerDeviceState>();
168 if (currentAudioDevice_ == nullptr) {
169 TELEPHONY_LOGE("make_unique SpeakerDeviceState failed");
170 return false;
171 }
172 TELEPHONY_LOGI("speaker enabled , current audio device : speaker");
173 SetCurrentAudioDevice(AudioDevice::DEVICE_SPEAKER);
174 SetSpeakerDevEnable();
175 return true;
176 }
177 TELEPHONY_LOGI("enable speaker device failed");
178 return false;
179 }
180
EnableEarpiece()181 bool AudioDeviceManager::EnableEarpiece()
182 {
183 if (isEarpieceAvailable_ && DelayedSingleton<AudioProxy>::GetInstance()->SetEarpieceDevActive()) {
184 currentAudioDevice_ = std::make_unique<EarpieceDeviceState>();
185 if (currentAudioDevice_ == nullptr) {
186 TELEPHONY_LOGE("make_unique EarpieceDeviceState failed");
187 return false;
188 }
189 TELEPHONY_LOGI("earpiece enabled , current audio device : earpiece");
190 SetCurrentAudioDevice(AudioDevice::DEVICE_EARPIECE);
191 SetEarpieceDevEnable();
192 return true;
193 }
194 TELEPHONY_LOGI("enable earpiece device failed");
195 return false;
196 }
197
EnableWiredHeadset()198 bool AudioDeviceManager::EnableWiredHeadset()
199 {
200 if (isWiredHeadsetConnected_ && DelayedSingleton<AudioProxy>::GetInstance()->SetWiredHeadsetDevActive()) {
201 currentAudioDevice_ = std::make_unique<WiredHeadsetDeviceState>();
202 if (currentAudioDevice_ == nullptr) {
203 TELEPHONY_LOGE("make_unique WiredHeadsetDeviceState failed");
204 return false;
205 }
206 TELEPHONY_LOGI("wired headset enabled , current audio device : wired headset");
207 SetCurrentAudioDevice(AudioDevice::DEVICE_WIRED_HEADSET);
208 SetWiredHeadsetDevEnable();
209 return true;
210 }
211 TELEPHONY_LOGI("enable wired headset device failed");
212 return false;
213 }
214
EnableBtSco()215 bool AudioDeviceManager::EnableBtSco()
216 {
217 if (isBtScoConnected_ && DelayedSingleton<AudioProxy>::GetInstance()->SetBluetoothDevActive()) {
218 currentAudioDevice_ = std::make_unique<BluetoothDeviceState>();
219 if (currentAudioDevice_ == nullptr) {
220 TELEPHONY_LOGE("make_unique BluetoothDeviceState failed");
221 return false;
222 }
223 TELEPHONY_LOGI("bluetooth sco enabled , current audio device : bluetooth sco");
224 SetCurrentAudioDevice(AudioDevice::DEVICE_BLUETOOTH_SCO);
225 SetBtScoDevEnable();
226 return true;
227 }
228 TELEPHONY_LOGI("enable bluetooth sco device failed");
229 return false;
230 }
231
DisableAll()232 bool AudioDeviceManager::DisableAll()
233 {
234 isBtScoDevEnable_ = false;
235 isWiredHeadsetDevEnable_ = false;
236 isSpeakerDevEnable_ = false;
237 isEarpieceDevEnable_ = false;
238 currentAudioDevice_ = std::make_unique<InactiveDeviceState>();
239 if (currentAudioDevice_ == nullptr) {
240 TELEPHONY_LOGE("make_unique InactiveDeviceState failed");
241 return false;
242 }
243 TELEPHONY_LOGI("current audio device : all audio devices disabled");
244 return true;
245 }
246
SetCurrentAudioDevice(AudioDevice device)247 void AudioDeviceManager::SetCurrentAudioDevice(AudioDevice device)
248 {
249 if (audioDevice_ == AudioDevice::DEVICE_BLUETOOTH_SCO && audioDevice_ != device) {
250 DelayedSingleton<BluetoothConnection>::GetInstance()->SetBtScoState(SCO_STATE_DISCONNECTED);
251 } else if (audioDevice_ != AudioDevice::DEVICE_BLUETOOTH_SCO && device == AudioDevice::DEVICE_BLUETOOTH_SCO) {
252 DelayedSingleton<BluetoothConnection>::GetInstance()->SetBtScoState(SCO_STATE_CONNECTED);
253 }
254 audioDevice_ = device;
255 }
256
GetCurrentAudioDevice()257 AudioDevice AudioDeviceManager::GetCurrentAudioDevice()
258 {
259 return audioDevice_;
260 }
261
SetEarpieceDevEnable()262 void AudioDeviceManager::SetEarpieceDevEnable()
263 {
264 isEarpieceDevEnable_ = true;
265 isBtScoDevEnable_ = false;
266 isWiredHeadsetDevEnable_ = false;
267 isSpeakerDevEnable_ = false;
268 }
269
SetSpeakerDevEnable()270 void AudioDeviceManager::SetSpeakerDevEnable()
271 {
272 isSpeakerDevEnable_ = true;
273 isWiredHeadsetDevEnable_ = false;
274 isBtScoDevEnable_ = false;
275 isEarpieceDevEnable_ = false;
276 }
277
SetBtScoDevEnable()278 void AudioDeviceManager::SetBtScoDevEnable()
279 {
280 isBtScoDevEnable_ = true;
281 isWiredHeadsetDevEnable_ = false;
282 isSpeakerDevEnable_ = false;
283 isEarpieceDevEnable_ = false;
284 }
285
SetWiredHeadsetDevEnable()286 void AudioDeviceManager::SetWiredHeadsetDevEnable()
287 {
288 isWiredHeadsetDevEnable_ = true;
289 isBtScoDevEnable_ = false;
290 isSpeakerDevEnable_ = false;
291 isEarpieceDevEnable_ = false;
292 }
293
IsEarpieceDevEnable()294 bool AudioDeviceManager::IsEarpieceDevEnable()
295 {
296 return isEarpieceDevEnable_;
297 }
298
IsWiredHeadsetDevEnable()299 bool AudioDeviceManager::IsWiredHeadsetDevEnable()
300 {
301 return isWiredHeadsetDevEnable_;
302 }
303
IsSpeakerDevEnable()304 bool AudioDeviceManager::IsSpeakerDevEnable()
305 {
306 return isSpeakerDevEnable_;
307 }
308
IsBtScoDevEnable()309 bool AudioDeviceManager::IsBtScoDevEnable()
310 {
311 return isBtScoDevEnable_;
312 }
313
IsBtScoConnected()314 bool AudioDeviceManager::IsBtScoConnected()
315 {
316 return isBtScoConnected_;
317 }
318
SetBtScoAvailable(bool connected)319 void AudioDeviceManager::SetBtScoAvailable(bool connected)
320 {
321 isBtScoConnected_ = connected;
322 }
323
IsWiredHeadsetConnected()324 bool AudioDeviceManager::IsWiredHeadsetConnected()
325 {
326 return isWiredHeadsetConnected_;
327 }
328
SetWiredHeadsetAvailable(bool connected)329 void AudioDeviceManager::SetWiredHeadsetAvailable(bool connected)
330 {
331 isWiredHeadsetConnected_ = connected;
332 }
333
IsEarpieceAvailable()334 bool AudioDeviceManager::IsEarpieceAvailable()
335 {
336 return isEarpieceAvailable_;
337 }
338
IsSpeakerAvailable()339 bool AudioDeviceManager::IsSpeakerAvailable()
340 {
341 return isSpeakerAvailable_;
342 }
343
SetSpeakerAvailable(bool available)344 void AudioDeviceManager::SetSpeakerAvailable(bool available)
345 {
346 isSpeakerAvailable_ = available;
347 }
348 } // namespace Telephony
349 } // namespace OHOS