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 result = currentAudioDevice_->ProcessEvent(event);
84 }
85 break;
86 case AudioEvent::AUDIO_DEACTIVATED:
87 if (isAudioActivated_) {
88 isAudioActivated_ = false;
89 result = InitAudioDevice();
90 }
91 break;
92 case AudioEvent::BLUETOOTH_SCO_CONNECTED:
93 isBtScoConnected_ = true;
94 result = currentAudioDevice_->ProcessEvent(event);
95 break;
96 case AudioEvent::BLUETOOTH_SCO_DISCONNECTED:
97 isBtScoConnected_ = false;
98 result = currentAudioDevice_->ProcessEvent(event);
99 break;
100 case AudioEvent::INIT_AUDIO_DEVICE:
101 result = InitAudioDevice();
102 break;
103 default:
104 break;
105 }
106 return result;
107 }
108
SwitchDevice(AudioEvent event)109 bool AudioDeviceManager::SwitchDevice(AudioEvent event)
110 {
111 auto itFunc = memberFuncMap_.find(event);
112 if (itFunc != memberFuncMap_.end() && itFunc->second != nullptr) {
113 auto memberFunc = itFunc->second;
114 return (this->*memberFunc)();
115 }
116 return false;
117 }
118
SwitchDevice(AudioDevice device)119 bool AudioDeviceManager::SwitchDevice(AudioDevice device)
120 {
121 bool result = false;
122 std::lock_guard<std::mutex> lock(mutex_);
123 switch (device) {
124 case AudioDevice::DEVICE_EARPIECE:
125 result = EnableEarpiece();
126 break;
127 case AudioDevice::DEVICE_SPEAKER:
128 result = EnableSpeaker();
129 break;
130 case AudioDevice::DEVICE_WIRED_HEADSET:
131 result = EnableWiredHeadset();
132 break;
133 case AudioDevice::DEVICE_BLUETOOTH_SCO:
134 result = EnableBtSco();
135 break;
136 case AudioDevice::DEVICE_DISABLE:
137 result = DisableAll();
138 break;
139 default:
140 break;
141 }
142 TELEPHONY_LOGI("switch device lock release");
143 return result;
144 }
145
EnableSpeaker()146 bool AudioDeviceManager::EnableSpeaker()
147 {
148 if (isSpeakerAvailable_ && DelayedSingleton<AudioProxy>::GetInstance()->SetSpeakerDevActive()) {
149 currentAudioDevice_ = std::make_unique<SpeakerDeviceState>();
150 if (currentAudioDevice_ == nullptr) {
151 TELEPHONY_LOGE("make_unique SpeakerDeviceState failed");
152 return false;
153 }
154 TELEPHONY_LOGI("speaker enabled , current audio device : speaker");
155 SetCurrentAudioDevice(AudioDevice::DEVICE_SPEAKER);
156 SetSpeakerDevEnable();
157 return true;
158 }
159 TELEPHONY_LOGI("enable speaker device failed");
160 return false;
161 }
162
EnableEarpiece()163 bool AudioDeviceManager::EnableEarpiece()
164 {
165 if (isEarpieceAvailable_ && DelayedSingleton<AudioProxy>::GetInstance()->SetEarpieceDevActive()) {
166 currentAudioDevice_ = std::make_unique<EarpieceDeviceState>();
167 if (currentAudioDevice_ == nullptr) {
168 TELEPHONY_LOGE("make_unique EarpieceDeviceState failed");
169 return false;
170 }
171 TELEPHONY_LOGI("earpiece enabled , current audio device : earpiece");
172 SetCurrentAudioDevice(AudioDevice::DEVICE_EARPIECE);
173 SetEarpieceDevEnable();
174 return true;
175 }
176 TELEPHONY_LOGI("enable earpiece device failed");
177 return false;
178 }
179
EnableWiredHeadset()180 bool AudioDeviceManager::EnableWiredHeadset()
181 {
182 if (isWiredHeadsetConnected_ && DelayedSingleton<AudioProxy>::GetInstance()->SetWiredHeadsetDevActive()) {
183 currentAudioDevice_ = std::make_unique<WiredHeadsetDeviceState>();
184 if (currentAudioDevice_ == nullptr) {
185 TELEPHONY_LOGE("make_unique WiredHeadsetDeviceState failed");
186 return false;
187 }
188 TELEPHONY_LOGI("wired headset enabled , current audio device : wired headset");
189 SetCurrentAudioDevice(AudioDevice::DEVICE_WIRED_HEADSET);
190 SetWiredHeadsetDevEnable();
191 return true;
192 }
193 TELEPHONY_LOGI("enable wired headset device failed");
194 return false;
195 }
196
EnableBtSco()197 bool AudioDeviceManager::EnableBtSco()
198 {
199 if (isBtScoConnected_ && DelayedSingleton<AudioProxy>::GetInstance()->SetBluetoothDevActive() &&
200 DelayedSingleton<BluetoothCallManager>::GetInstance()->ConnectBtSco()) {
201 currentAudioDevice_ = std::make_unique<BluetoothDeviceState>();
202 if (currentAudioDevice_ == nullptr) {
203 TELEPHONY_LOGE("make_unique BluetoothDeviceState failed");
204 return false;
205 }
206 TELEPHONY_LOGI("bluetooth sco enabled , current audio device : bluetooth sco");
207 SetCurrentAudioDevice(AudioDevice::DEVICE_BLUETOOTH_SCO);
208 SetBtScoDevEnable();
209 return true;
210 }
211 TELEPHONY_LOGI("enable bluetooth sco device failed");
212 return false;
213 }
214
DisableAll()215 bool AudioDeviceManager::DisableAll()
216 {
217 isBtScoDevEnable_ = false;
218 isWiredHeadsetDevEnable_ = false;
219 isSpeakerDevEnable_ = false;
220 isEarpieceDevEnable_ = false;
221 currentAudioDevice_ = std::make_unique<InactiveDeviceState>();
222 if (currentAudioDevice_ == nullptr) {
223 TELEPHONY_LOGE("make_unique InactiveDeviceState failed");
224 return false;
225 }
226 TELEPHONY_LOGI("current audio device : all audio devices disabled");
227 return true;
228 }
229
SetCurrentAudioDevice(AudioDevice device)230 void AudioDeviceManager::SetCurrentAudioDevice(AudioDevice device)
231 {
232 if (audioDevice_ == AudioDevice::DEVICE_BLUETOOTH_SCO && audioDevice_ != device) {
233 BluetoothConnection::SetBtScoState(SCO_STATE_PENDING);
234 } else if (audioDevice_ != AudioDevice::DEVICE_BLUETOOTH_SCO && device == AudioDevice::DEVICE_BLUETOOTH_SCO) {
235 BluetoothConnection::SetBtScoState(SCO_STATE_CONNECTED);
236 }
237 audioDevice_ = device;
238 }
239
GetCurrentAudioDevice()240 AudioDevice AudioDeviceManager::GetCurrentAudioDevice()
241 {
242 return audioDevice_;
243 }
244
SetEarpieceDevEnable()245 void AudioDeviceManager::SetEarpieceDevEnable()
246 {
247 isEarpieceDevEnable_ = true;
248 isBtScoDevEnable_ = false;
249 isWiredHeadsetDevEnable_ = false;
250 isSpeakerDevEnable_ = false;
251 }
252
SetSpeakerDevEnable()253 void AudioDeviceManager::SetSpeakerDevEnable()
254 {
255 isSpeakerDevEnable_ = true;
256 isWiredHeadsetDevEnable_ = false;
257 isBtScoDevEnable_ = false;
258 isEarpieceDevEnable_ = false;
259 }
260
SetBtScoDevEnable()261 void AudioDeviceManager::SetBtScoDevEnable()
262 {
263 isBtScoDevEnable_ = true;
264 isWiredHeadsetDevEnable_ = false;
265 isSpeakerDevEnable_ = false;
266 isEarpieceDevEnable_ = false;
267 }
268
SetWiredHeadsetDevEnable()269 void AudioDeviceManager::SetWiredHeadsetDevEnable()
270 {
271 isWiredHeadsetDevEnable_ = true;
272 isBtScoDevEnable_ = false;
273 isSpeakerDevEnable_ = false;
274 isEarpieceDevEnable_ = false;
275 }
276
IsEarpieceDevEnable()277 bool AudioDeviceManager::IsEarpieceDevEnable()
278 {
279 return isEarpieceDevEnable_;
280 }
281
IsWiredHeadsetDevEnable()282 bool AudioDeviceManager::IsWiredHeadsetDevEnable()
283 {
284 return isWiredHeadsetDevEnable_;
285 }
286
IsSpeakerDevEnable()287 bool AudioDeviceManager::IsSpeakerDevEnable()
288 {
289 return isSpeakerDevEnable_;
290 }
291
IsBtScoDevEnable()292 bool AudioDeviceManager::IsBtScoDevEnable()
293 {
294 return isBtScoDevEnable_;
295 }
296
IsBtScoConnected()297 bool AudioDeviceManager::IsBtScoConnected()
298 {
299 return isBtScoConnected_;
300 }
301
SetBtScoAvailable(bool connected)302 void AudioDeviceManager::SetBtScoAvailable(bool connected)
303 {
304 isBtScoConnected_ = connected;
305 }
306
IsWiredHeadsetConnected()307 bool AudioDeviceManager::IsWiredHeadsetConnected()
308 {
309 return isWiredHeadsetConnected_;
310 }
311
SetWiredHeadsetAvailable(bool connected)312 void AudioDeviceManager::SetWiredHeadsetAvailable(bool connected)
313 {
314 isWiredHeadsetConnected_ = connected;
315 }
316
IsEarpieceAvailable()317 bool AudioDeviceManager::IsEarpieceAvailable()
318 {
319 return isEarpieceAvailable_;
320 }
321
IsSpeakerAvailable()322 bool AudioDeviceManager::IsSpeakerAvailable()
323 {
324 return isSpeakerAvailable_;
325 }
326
SetSpeakerAvailable(bool available)327 void AudioDeviceManager::SetSpeakerAvailable(bool available)
328 {
329 isSpeakerAvailable_ = available;
330 }
331 } // namespace Telephony
332 } // namespace OHOS