1/*
2 * Copyright 2014 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10package eu.siacs.conversations.services;
11
12import android.content.BroadcastReceiver;
13import android.content.Context;
14import android.content.Intent;
15import android.content.IntentFilter;
16import android.content.pm.PackageManager;
17import android.media.AudioDeviceInfo;
18import android.media.AudioManager;
19import android.media.ToneGenerator;
20import android.util.Log;
21
22import androidx.annotation.Nullable;
23import androidx.core.content.ContextCompat;
24
25import com.google.common.collect.ImmutableSet;
26
27import eu.siacs.conversations.Config;
28import eu.siacs.conversations.utils.AppRTCUtils;
29import eu.siacs.conversations.xmpp.jingle.JingleConnectionManager;
30
31import org.webrtc.ThreadUtils;
32
33import java.util.HashSet;
34import java.util.Set;
35import java.util.concurrent.ScheduledFuture;
36import java.util.concurrent.TimeUnit;
37
38/** AppRTCAudioManager manages all audio related parts of the AppRTC demo. */
39public class AppRTCAudioManager {
40
41 private final Context apprtcContext;
42 // Contains speakerphone setting: auto, true or false
43 // Handles all tasks related to Bluetooth headset devices.
44 private final AppRTCBluetoothManager bluetoothManager;
45 @Nullable private final AudioManager audioManager;
46 @Nullable private AudioManagerEvents audioManagerEvents;
47 private AudioManagerState amState;
48 private boolean savedIsSpeakerPhoneOn;
49 private boolean savedIsMicrophoneMute;
50 private boolean hasWiredHeadset;
51 // Default audio device; speaker phone for video calls or earpiece for audio
52 // only calls.
53 private CallIntegration.AudioDevice defaultAudioDevice;
54 // Contains the currently selected audio device.
55 // This device is changed automatically using a certain scheme where e.g.
56 // a wired headset "wins" over speaker phone. It is also possible for a
57 // user to explicitly select a device (and overrid any predefined scheme).
58 // See |userSelectedAudioDevice| for details.
59 private CallIntegration.AudioDevice selectedAudioDevice;
60 // Contains the user-selected audio device which overrides the predefined
61 // selection scheme.
62 // TODO(henrika): always set to AudioDevice.NONE today. Add support for
63 // explicit selection based on choice by userSelectedAudioDevice.
64 private CallIntegration.AudioDevice userSelectedAudioDevice;
65
66 // Contains a list of available audio devices. A Set collection is used to
67 // avoid duplicate elements.
68 private Set<CallIntegration.AudioDevice> audioDevices = new HashSet<>();
69 // Broadcast receiver for wired headset intent broadcasts.
70 private final BroadcastReceiver wiredHeadsetReceiver;
71 // Callback method for changes in audio focus.
72 @Nullable private AudioManager.OnAudioFocusChangeListener audioFocusChangeListener;
73 private ScheduledFuture<?> ringBackFuture;
74
75 public AppRTCAudioManager(final Context context) {
76 apprtcContext = context;
77 audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE));
78 bluetoothManager = AppRTCBluetoothManager.create(context, this);
79 wiredHeadsetReceiver = new WiredHeadsetReceiver();
80 amState = AudioManagerState.UNINITIALIZED;
81 // CallIntegration / Connection uses Earpiece as default too
82 if (hasEarpiece()) {
83 defaultAudioDevice = CallIntegration.AudioDevice.EARPIECE;
84 } else {
85 defaultAudioDevice = CallIntegration.AudioDevice.SPEAKER_PHONE;
86 }
87 Log.d(Config.LOGTAG, "defaultAudioDevice: " + defaultAudioDevice);
88 AppRTCUtils.logDeviceInfo(Config.LOGTAG);
89 }
90
91 public void setAudioManagerEvents(final AudioManagerEvents audioManagerEvents) {
92 this.audioManagerEvents = audioManagerEvents;
93 }
94
95 @SuppressWarnings("deprecation")
96 public void start() {
97 Log.d(Config.LOGTAG, AppRTCAudioManager.class.getName() + ".start()");
98 ThreadUtils.checkIsOnMainThread();
99 if (amState == AudioManagerState.RUNNING) {
100 Log.e(Config.LOGTAG, "AudioManager is already active");
101 return;
102 }
103 amState = AudioManagerState.RUNNING;
104 // Store current audio state so we can restore it when stop() is called.
105 savedIsSpeakerPhoneOn = audioManager.isSpeakerphoneOn();
106 savedIsMicrophoneMute = audioManager.isMicrophoneMute();
107 hasWiredHeadset = hasWiredHeadset();
108 // Create an AudioManager.OnAudioFocusChangeListener instance.
109 audioFocusChangeListener =
110 new AudioManager.OnAudioFocusChangeListener() {
111 // Called on the listener to notify if the audio focus for this listener has
112 // been changed.
113 // The |focusChange| value indicates whether the focus was gained, whether the
114 // focus was lost,
115 // and whether that loss is transient, or whether the new focus holder will hold
116 // it for an
117 // unknown amount of time.
118 // TODO(henrika): possibly extend support of handling audio-focus changes. Only
119 // contains
120 // logging for now.
121 @Override
122 public void onAudioFocusChange(final int focusChange) {
123 final String typeOfChange =
124 switch (focusChange) {
125 case AudioManager.AUDIOFOCUS_GAIN -> "AUDIOFOCUS_GAIN";
126 case AudioManager
127 .AUDIOFOCUS_GAIN_TRANSIENT -> "AUDIOFOCUS_GAIN_TRANSIENT";
128 case AudioManager
129 .AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE -> "AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE";
130 case AudioManager
131 .AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK -> "AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK";
132 case AudioManager.AUDIOFOCUS_LOSS -> "AUDIOFOCUS_LOSS";
133 case AudioManager
134 .AUDIOFOCUS_LOSS_TRANSIENT -> "AUDIOFOCUS_LOSS_TRANSIENT";
135 case AudioManager
136 .AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK -> "AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK";
137 default -> "AUDIOFOCUS_INVALID";
138 };
139 Log.d(Config.LOGTAG, "onAudioFocusChange: " + typeOfChange);
140 }
141 };
142 // Request audio playout focus (without ducking) and install listener for changes in focus.
143 int result =
144 audioManager.requestAudioFocus(
145 audioFocusChangeListener,
146 AudioManager.STREAM_VOICE_CALL,
147 AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
148 if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
149 Log.d(Config.LOGTAG, "Audio focus request granted for VOICE_CALL streams");
150 } else {
151 Log.e(Config.LOGTAG, "Audio focus request failed");
152 }
153 // Start by setting MODE_IN_COMMUNICATION as default audio mode. It is
154 // required to be in this mode when playout and/or recording starts for
155 // best possible VoIP performance.
156 audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
157 // Always disable microphone mute during a WebRTC call.
158 setMicrophoneMute(false);
159 // Set initial device states.
160 userSelectedAudioDevice = CallIntegration.AudioDevice.NONE;
161 selectedAudioDevice = CallIntegration.AudioDevice.NONE;
162 audioDevices.clear();
163 // Initialize and start Bluetooth if a BT device is available or initiate
164 // detection of new (enabled) BT devices.
165 bluetoothManager.start();
166 // Do initial selection of audio device. This setting can later be changed
167 // either by adding/removing a BT or wired headset or by covering/uncovering
168 // the proximity sensor.
169 updateAudioDeviceState();
170 // Register receiver for broadcast intents related to adding/removing a
171 // wired headset.
172 registerReceiver(wiredHeadsetReceiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG));
173 Log.d(Config.LOGTAG, "AudioManager started");
174 }
175
176 @SuppressWarnings("deprecation")
177 public void stop() {
178 Log.d(Config.LOGTAG, "appRtpAudioManager.stop()");
179 Log.d(Config.LOGTAG, AppRTCAudioManager.class.getName() + ".stop()");
180 ThreadUtils.checkIsOnMainThread();
181 if (amState != AudioManagerState.RUNNING) {
182 Log.e(Config.LOGTAG, "Trying to stop AudioManager in incorrect state: " + amState);
183 return;
184 }
185 amState = AudioManagerState.UNINITIALIZED;
186 unregisterReceiver(wiredHeadsetReceiver);
187 bluetoothManager.stop();
188 // Restore previously stored audio states.
189 setSpeakerphoneOn(savedIsSpeakerPhoneOn);
190 setMicrophoneMute(savedIsMicrophoneMute);
191 audioManager.setMode(AudioManager.MODE_NORMAL);
192 // Abandon audio focus. Gives the previous focus owner, if any, focus.
193 audioManager.abandonAudioFocus(audioFocusChangeListener);
194 audioFocusChangeListener = null;
195 audioManagerEvents = null;
196 Log.d(Config.LOGTAG, "appRtpAudioManager.stopped()");
197 }
198
199 /** Changes selection of the currently active audio device. */
200 private void setAudioDeviceInternal(final CallIntegration.AudioDevice device) {
201 Log.d(Config.LOGTAG, "setAudioDeviceInternal(device=" + device + ")");
202 AppRTCUtils.assertIsTrue(audioDevices.contains(device));
203 switch (device) {
204 case SPEAKER_PHONE -> setSpeakerphoneOn(true);
205 case EARPIECE, WIRED_HEADSET, BLUETOOTH -> setSpeakerphoneOn(false);
206 default -> Log.e(Config.LOGTAG, "Invalid audio device selection");
207 }
208 selectedAudioDevice = device;
209 }
210
211 /**
212 * Changes default audio device. TODO(henrika): add usage of this method in the AppRTCMobile
213 * client.
214 */
215 public void setDefaultAudioDevice(final CallIntegration.AudioDevice defaultDevice) {
216 ThreadUtils.checkIsOnMainThread();
217 switch (defaultDevice) {
218 case SPEAKER_PHONE -> defaultAudioDevice = defaultDevice;
219 case EARPIECE -> {
220 if (hasEarpiece()) {
221 defaultAudioDevice = defaultDevice;
222 } else {
223 defaultAudioDevice = CallIntegration.AudioDevice.SPEAKER_PHONE;
224 }
225 }
226 default -> Log.e(Config.LOGTAG, "Invalid default audio device selection");
227 }
228 Log.d(Config.LOGTAG, "setDefaultAudioDevice(device=" + defaultAudioDevice + ")");
229 updateAudioDeviceState();
230 }
231
232 /** Changes selection of the currently active audio device. */
233 public void selectAudioDevice(final CallIntegration.AudioDevice device) {
234 ThreadUtils.checkIsOnMainThread();
235 if (!audioDevices.contains(device)) {
236 Log.e(Config.LOGTAG, "Can not select " + device + " from available " + audioDevices);
237 }
238 userSelectedAudioDevice = device;
239 updateAudioDeviceState();
240 }
241
242 /** Returns current set of available/selectable audio devices. */
243 public Set<CallIntegration.AudioDevice> getAudioDevices() {
244 return ImmutableSet.copyOf(audioDevices);
245 }
246
247 /** Returns the currently selected audio device. */
248 public CallIntegration.AudioDevice getSelectedAudioDevice() {
249 return selectedAudioDevice;
250 }
251
252 /** Helper method for receiver registration. */
253 private void registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
254 apprtcContext.registerReceiver(receiver, filter);
255 }
256
257 /** Helper method for unregistration of an existing receiver. */
258 private void unregisterReceiver(BroadcastReceiver receiver) {
259 apprtcContext.unregisterReceiver(receiver);
260 }
261
262 /** Sets the speaker phone mode. */
263 private void setSpeakerphoneOn(boolean on) {
264 boolean wasOn = audioManager.isSpeakerphoneOn();
265 if (wasOn == on) {
266 return;
267 }
268 audioManager.setSpeakerphoneOn(on);
269 }
270
271 /** Sets the microphone mute state. */
272 private void setMicrophoneMute(boolean on) {
273 boolean wasMuted = audioManager.isMicrophoneMute();
274 if (wasMuted == on) {
275 return;
276 }
277 audioManager.setMicrophoneMute(on);
278 }
279
280 /** Gets the current earpiece state. */
281 private boolean hasEarpiece() {
282 return apprtcContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
283 }
284
285 /**
286 * Checks whether a wired headset is connected or not. This is not a valid indication that audio
287 * playback is actually over the wired headset as audio routing depends on other conditions. We
288 * only use it as an early indicator (during initialization) of an attached wired headset.
289 */
290 @Deprecated
291 private boolean hasWiredHeadset() {
292 final AudioDeviceInfo[] devices = audioManager.getDevices(AudioManager.GET_DEVICES_ALL);
293 for (AudioDeviceInfo device : devices) {
294 final int type = device.getType();
295 if (type == AudioDeviceInfo.TYPE_WIRED_HEADSET) {
296 Log.d(Config.LOGTAG, "hasWiredHeadset: found wired headset");
297 return true;
298 } else if (type == AudioDeviceInfo.TYPE_USB_DEVICE) {
299 Log.d(Config.LOGTAG, "hasWiredHeadset: found USB audio device");
300 return true;
301 }
302 }
303 return false;
304 }
305
306 /**
307 * Updates list of possible audio devices and make new device selection. TODO(henrika): add unit
308 * test to verify all state transitions.
309 */
310 public void updateAudioDeviceState() {
311 ThreadUtils.checkIsOnMainThread();
312 Log.d(
313 Config.LOGTAG,
314 "--- updateAudioDeviceState: "
315 + "wired headset="
316 + hasWiredHeadset
317 + ", "
318 + "BT state="
319 + bluetoothManager.getState());
320 Log.d(
321 Config.LOGTAG,
322 "Device status: "
323 + "available="
324 + audioDevices
325 + ", "
326 + "selected="
327 + selectedAudioDevice
328 + ", "
329 + "user selected="
330 + userSelectedAudioDevice);
331 // Check if any Bluetooth headset is connected. The internal BT state will
332 // change accordingly.
333 // TODO(henrika): perhaps wrap required state into BT manager.
334 if (bluetoothManager.getState() == AppRTCBluetoothManager.State.HEADSET_AVAILABLE
335 || bluetoothManager.getState() == AppRTCBluetoothManager.State.HEADSET_UNAVAILABLE
336 || bluetoothManager.getState() == AppRTCBluetoothManager.State.SCO_DISCONNECTING) {
337 bluetoothManager.updateDevice();
338 }
339 // Update the set of available audio devices.
340 Set<CallIntegration.AudioDevice> newAudioDevices = new HashSet<>();
341 if (bluetoothManager.getState() == AppRTCBluetoothManager.State.SCO_CONNECTED
342 || bluetoothManager.getState() == AppRTCBluetoothManager.State.SCO_CONNECTING
343 || bluetoothManager.getState() == AppRTCBluetoothManager.State.HEADSET_AVAILABLE) {
344 newAudioDevices.add(CallIntegration.AudioDevice.BLUETOOTH);
345 }
346 if (hasWiredHeadset) {
347 // If a wired headset is connected, then it is the only possible option.
348 newAudioDevices.add(CallIntegration.AudioDevice.WIRED_HEADSET);
349 } else {
350 // No wired headset, hence the audio-device list can contain speaker
351 // phone (on a tablet), or speaker phone and earpiece (on mobile phone).
352 newAudioDevices.add(CallIntegration.AudioDevice.SPEAKER_PHONE);
353 if (hasEarpiece()) {
354 newAudioDevices.add(CallIntegration.AudioDevice.EARPIECE);
355 }
356 }
357 // Store state which is set to true if the device list has changed.
358 boolean audioDeviceSetUpdated = !audioDevices.equals(newAudioDevices);
359 // Update the existing audio device set.
360 audioDevices = newAudioDevices;
361 // Correct user selected audio devices if needed.
362 if (bluetoothManager.getState() == AppRTCBluetoothManager.State.HEADSET_UNAVAILABLE
363 && userSelectedAudioDevice == CallIntegration.AudioDevice.BLUETOOTH) {
364 // If BT is not available, it can't be the user selection.
365 userSelectedAudioDevice = CallIntegration.AudioDevice.NONE;
366 }
367 if (hasWiredHeadset
368 && userSelectedAudioDevice == CallIntegration.AudioDevice.SPEAKER_PHONE) {
369 // If user selected speaker phone, but then plugged wired headset then make
370 // wired headset as user selected device.
371 userSelectedAudioDevice = CallIntegration.AudioDevice.WIRED_HEADSET;
372 }
373 if (!hasWiredHeadset
374 && userSelectedAudioDevice == CallIntegration.AudioDevice.WIRED_HEADSET) {
375 // If user selected wired headset, but then unplugged wired headset then make
376 // speaker phone as user selected device.
377 userSelectedAudioDevice = CallIntegration.AudioDevice.SPEAKER_PHONE;
378 }
379 // Need to start Bluetooth if it is available and user either selected it explicitly or
380 // user did not select any output device.
381 boolean needBluetoothAudioStart =
382 bluetoothManager.getState() == AppRTCBluetoothManager.State.HEADSET_AVAILABLE
383 && (userSelectedAudioDevice == CallIntegration.AudioDevice.NONE
384 || userSelectedAudioDevice
385 == CallIntegration.AudioDevice.BLUETOOTH);
386 // Need to stop Bluetooth audio if user selected different device and
387 // Bluetooth SCO connection is established or in the process.
388 boolean needBluetoothAudioStop =
389 (bluetoothManager.getState() == AppRTCBluetoothManager.State.SCO_CONNECTED
390 || bluetoothManager.getState()
391 == AppRTCBluetoothManager.State.SCO_CONNECTING)
392 && (userSelectedAudioDevice != CallIntegration.AudioDevice.NONE
393 && userSelectedAudioDevice
394 != CallIntegration.AudioDevice.BLUETOOTH);
395 if (bluetoothManager.getState() == AppRTCBluetoothManager.State.HEADSET_AVAILABLE
396 || bluetoothManager.getState() == AppRTCBluetoothManager.State.SCO_CONNECTING
397 || bluetoothManager.getState() == AppRTCBluetoothManager.State.SCO_CONNECTED) {
398 Log.d(
399 Config.LOGTAG,
400 "Need BT audio: start="
401 + needBluetoothAudioStart
402 + ", "
403 + "stop="
404 + needBluetoothAudioStop
405 + ", "
406 + "BT state="
407 + bluetoothManager.getState());
408 }
409 // Start or stop Bluetooth SCO connection given states set earlier.
410 if (needBluetoothAudioStop) {
411 bluetoothManager.stopScoAudio();
412 bluetoothManager.updateDevice();
413 }
414 if (needBluetoothAudioStart && !needBluetoothAudioStop) {
415 // Attempt to start Bluetooth SCO audio (takes a few second to start).
416 if (!bluetoothManager.startScoAudio()) {
417 // Remove BLUETOOTH from list of available devices since SCO failed.
418 audioDevices.remove(CallIntegration.AudioDevice.BLUETOOTH);
419 audioDeviceSetUpdated = true;
420 }
421 }
422 // Update selected audio device.
423 final CallIntegration.AudioDevice newAudioDevice;
424 if (bluetoothManager.getState() == AppRTCBluetoothManager.State.SCO_CONNECTED) {
425 // If a Bluetooth is connected, then it should be used as output audio
426 // device. Note that it is not sufficient that a headset is available;
427 // an active SCO channel must also be up and running.
428 newAudioDevice = CallIntegration.AudioDevice.BLUETOOTH;
429 } else if (hasWiredHeadset) {
430 // If a wired headset is connected, but Bluetooth is not, then wired headset is used as
431 // audio device.
432 newAudioDevice = CallIntegration.AudioDevice.WIRED_HEADSET;
433 } else {
434 // No wired headset and no Bluetooth, hence the audio-device list can contain speaker
435 // phone (on a tablet), or speaker phone and earpiece (on mobile phone).
436 // |defaultAudioDevice| contains either AudioDevice.SPEAKER_PHONE or
437 // AudioDevice.EARPIECE
438 // depending on the user's selection.
439 newAudioDevice = defaultAudioDevice;
440 }
441 // Switch to new device but only if there has been any changes.
442 if (newAudioDevice != selectedAudioDevice || audioDeviceSetUpdated) {
443 // Do the required device switch.
444 setAudioDeviceInternal(newAudioDevice);
445 Log.d(
446 Config.LOGTAG,
447 "New device status: "
448 + "available="
449 + audioDevices
450 + ", "
451 + "selected="
452 + newAudioDevice);
453 if (audioManagerEvents != null) {
454 // Notify a listening client that audio device has been changed.
455 audioManagerEvents.onAudioDeviceChanged(selectedAudioDevice, audioDevices);
456 }
457 }
458 Log.d(Config.LOGTAG, "--- updateAudioDeviceState done");
459 }
460
461 public void executeOnMain(final Runnable runnable) {
462 ContextCompat.getMainExecutor(apprtcContext).execute(runnable);
463 }
464
465 public void startRingBack() {
466 this.ringBackFuture =
467 JingleConnectionManager.SCHEDULED_EXECUTOR_SERVICE.scheduleAtFixedRate(
468 () -> {
469 final var toneGenerator =
470 new ToneGenerator(
471 AudioManager.STREAM_MUSIC,
472 CallIntegration.DEFAULT_TONE_VOLUME);
473 toneGenerator.startTone(ToneGenerator.TONE_CDMA_DIAL_TONE_LITE, 750);
474 },
475 0,
476 3,
477 TimeUnit.SECONDS);
478 }
479
480 public void stopRingBack() {
481 final var future = this.ringBackFuture;
482 if (future == null || future.isDone()) {
483 return;
484 }
485 future.cancel(true);
486 }
487
488 /** AudioManager state. */
489 public enum AudioManagerState {
490 UNINITIALIZED,
491 PREINITIALIZED,
492 RUNNING,
493 }
494
495 /** Selected audio device change event. */
496 public interface AudioManagerEvents {
497 // Callback fired once audio device is changed or list of available audio devices changed.
498 void onAudioDeviceChanged(
499 CallIntegration.AudioDevice selectedAudioDevice,
500 Set<CallIntegration.AudioDevice> availableAudioDevices);
501 }
502
503 /* Receiver which handles changes in wired headset availability. */
504 private class WiredHeadsetReceiver extends BroadcastReceiver {
505 private static final int STATE_UNPLUGGED = 0;
506 private static final int STATE_PLUGGED = 1;
507 private static final int HAS_NO_MIC = 0;
508 private static final int HAS_MIC = 1;
509
510 @Override
511 public void onReceive(Context context, Intent intent) {
512 int state = intent.getIntExtra("state", STATE_UNPLUGGED);
513 int microphone = intent.getIntExtra("microphone", HAS_NO_MIC);
514 String name = intent.getStringExtra("name");
515 Log.d(
516 Config.LOGTAG,
517 "WiredHeadsetReceiver.onReceive"
518 + AppRTCUtils.getThreadInfo()
519 + ": "
520 + "a="
521 + intent.getAction()
522 + ", s="
523 + (state == STATE_UNPLUGGED ? "unplugged" : "plugged")
524 + ", m="
525 + (microphone == HAS_MIC ? "mic" : "no mic")
526 + ", n="
527 + name
528 + ", sb="
529 + isInitialStickyBroadcast());
530 hasWiredHeadset = (state == STATE_PLUGGED);
531 updateAudioDeviceState();
532 }
533 }
534}