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