davidliu
Committed by GitHub

handle audio output changes automatically in sample apps (#100)

<?xml version="1.0" encoding="utf-8"?>
<manifest package="io.livekit.android.sample">
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.livekit.android.sample">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission
android:name="android.permission.BLUETOOTH"
android:maxSdkVersion="30" />
<uses-permission
android:name="android.permission.BLUETOOTH_ADMIN"
android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
</manifest>
\ No newline at end of file
... ...
... ... @@ -2,7 +2,10 @@ package io.livekit.android.sample
import android.app.Application
import android.content.Intent
import androidx.lifecycle.*
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.github.ajalt.timberkt.Timber
import io.livekit.android.LiveKit
import io.livekit.android.RoomOptions
... ... @@ -13,6 +16,7 @@ import io.livekit.android.room.participant.LocalParticipant
import io.livekit.android.room.participant.Participant
import io.livekit.android.room.participant.RemoteParticipant
import io.livekit.android.room.track.*
import io.livekit.android.sample.audio.AppRTCAudioManager
import io.livekit.android.util.flow
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.*
... ... @@ -76,7 +80,12 @@ class CallViewModel(
private val mutablePermissionAllowed = MutableStateFlow(true)
val permissionAllowed = mutablePermissionAllowed.hide()
private val audioManager = AppRTCAudioManager(application)
init {
audioManager.start(null)
viewModelScope.launch {
launch {
... ... @@ -199,6 +208,7 @@ class CallViewModel(
override fun onCleared() {
super.onCleared()
mutableRoom.value?.disconnect()
audioManager.stop()
}
fun setMicEnabled(enabled: Boolean) {
... ...
/*
* 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)
}
}
\ No newline at end of file
... ...
/*
* Copyright 2016 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.
*/
package io.livekit.android.sample.audio
import android.Manifest
import android.annotation.SuppressLint
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothHeadset
import android.bluetooth.BluetoothProfile
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.media.AudioManager
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.os.Process
import org.webrtc.ThreadUtils
import timber.log.Timber
/**
* AppRTCProximitySensor manages functions related to Bluetoth devices in the
* AppRTC demo.
*/
@Suppress("MemberVisibilityCanBePrivate")
@SuppressLint("BinaryOperationInTimber")
open class AppRTCBluetoothManager
constructor(context: Context, audioManager: AppRTCAudioManager) {
// Bluetooth connection state.
enum class State {
// Bluetooth is not available; no adapter or Bluetooth is off.
UNINITIALIZED, // Bluetooth error happened when trying to start Bluetooth.
ERROR, // Bluetooth proxy object for the Headset profile exists, but no connected headset devices,
// SCO is not started or disconnected.
HEADSET_UNAVAILABLE, // Bluetooth proxy object for the Headset profile connected, connected Bluetooth headset
// present, but SCO is not started or disconnected.
HEADSET_AVAILABLE, // Bluetooth audio SCO connection with remote device is closing.
SCO_DISCONNECTING, // Bluetooth audio SCO connection with remote device is initiated.
SCO_CONNECTING, // Bluetooth audio SCO connection with remote device is established.
SCO_CONNECTED
}
private val apprtcContext: Context
private val apprtcAudioManager: AppRTCAudioManager
private val audioManager: AudioManager?
private val handler: Handler
var scoConnectionAttempts = 0
private var bluetoothState: State
private val bluetoothServiceListener: BluetoothProfile.ServiceListener
private var bluetoothAdapter: BluetoothAdapter? = null
private var bluetoothHeadset: BluetoothHeadset? = null
private var bluetoothDevice: BluetoothDevice? = null
private val bluetoothHeadsetReceiver: BroadcastReceiver
// Runs when the Bluetooth timeout expires. We use that timeout after calling
// startScoAudio() or stopScoAudio() because we're not guaranteed to get a
// callback after those calls.
private val bluetoothTimeoutRunnable = Runnable { bluetoothTimeout() }
/**
* Implementation of an interface that notifies BluetoothProfile IPC clients when they have been
* connected to or disconnected from the service.
*/
private inner class BluetoothServiceListener : BluetoothProfile.ServiceListener {
// Called to notify the client when the proxy object has been connected to the service.
// Once we have the profile proxy object, we can use it to monitor the state of the
// connection and perform other operations that are relevant to the headset profile.
override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) {
if (profile != BluetoothProfile.HEADSET || bluetoothState == State.UNINITIALIZED) {
return
}
Timber.d("BluetoothServiceListener.onServiceConnected: BT state=$bluetoothState")
// Android only supports one connected Bluetooth Headset at a time.
bluetoothHeadset = proxy as BluetoothHeadset
updateAudioDeviceState()
Timber.d("onServiceConnected done: BT state=$bluetoothState")
}
/** Notifies the client when the proxy object has been disconnected from the service. */
override fun onServiceDisconnected(profile: Int) {
if (profile != BluetoothProfile.HEADSET || bluetoothState == State.UNINITIALIZED) {
return
}
Timber.d("BluetoothServiceListener.onServiceDisconnected: BT state=$bluetoothState")
stopScoAudio()
bluetoothHeadset = null
bluetoothDevice = null
bluetoothState = State.HEADSET_UNAVAILABLE
updateAudioDeviceState()
Timber.d("onServiceDisconnected done: BT state=$bluetoothState")
}
}
// Intent broadcast receiver which handles changes in Bluetooth device availability.
// Detects headset changes and Bluetooth SCO state changes.
private inner class BluetoothHeadsetBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (bluetoothState == State.UNINITIALIZED) {
return
}
val action = intent.action
// Change in connection state of the Headset profile. Note that the
// change does not tell us anything about whether we're streaming
// audio to BT over SCO. Typically received when user turns on a BT
// headset while audio is active using another audio device.
if (action == BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED) {
val state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_DISCONNECTED)
Timber.d(
"""BluetoothHeadsetBroadcastReceiver.onReceive: a=ACTION_CONNECTION_STATE_CHANGED, s=${
stateToString(
state
)
}, sb=$isInitialStickyBroadcast, BT state: $bluetoothState"""
)
if (state == BluetoothHeadset.STATE_CONNECTED) {
scoConnectionAttempts = 0
updateAudioDeviceState()
} else if (state == BluetoothHeadset.STATE_CONNECTING) {
// No action needed.
} else if (state == BluetoothHeadset.STATE_DISCONNECTING) {
// No action needed.
} else if (state == BluetoothHeadset.STATE_DISCONNECTED) {
// Bluetooth is probably powered off during the call.
stopScoAudio()
updateAudioDeviceState()
}
// Change in the audio (SCO) connection state of the Headset profile.
// Typically received after call to startScoAudio() has finalized.
} else if (action == BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED) {
val state = intent.getIntExtra(
BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_AUDIO_DISCONNECTED
)
Timber.d(
"BluetoothHeadsetBroadcastReceiver.onReceive: "
+ "a=ACTION_AUDIO_STATE_CHANGED, "
+ "s=" + stateToString(state) + ", "
+ "sb=" + isInitialStickyBroadcast + ", "
+ "BT state: " + bluetoothState
)
if (state == BluetoothHeadset.STATE_AUDIO_CONNECTED) {
cancelTimer()
if (bluetoothState == State.SCO_CONNECTING) {
Timber.d("+++ Bluetooth audio SCO is now connected")
bluetoothState = State.SCO_CONNECTED
scoConnectionAttempts = 0
updateAudioDeviceState()
} else {
Timber.w("Unexpected state BluetoothHeadset.STATE_AUDIO_CONNECTED")
}
} else if (state == BluetoothHeadset.STATE_AUDIO_CONNECTING) {
Timber.d("+++ Bluetooth audio SCO is now connecting...")
} else if (state == BluetoothHeadset.STATE_AUDIO_DISCONNECTED) {
Timber.d("+++ Bluetooth audio SCO is now disconnected")
if (isInitialStickyBroadcast) {
Timber.d("Ignore STATE_AUDIO_DISCONNECTED initial sticky broadcast.")
return
}
updateAudioDeviceState()
}
}
Timber.d("onReceive done: BT state=$bluetoothState")
}
}
/** Returns the internal state. */
val state: State
get() {
ThreadUtils.checkIsOnMainThread()
return bluetoothState
}
/**
* Activates components required to detect Bluetooth devices and to enable
* BT SCO (audio is routed via BT SCO) for the headset profile. The end
* state will be HEADSET_UNAVAILABLE but a state machine has started which
* will start a state change sequence where the final outcome depends on
* if/when the BT headset is enabled.
* Example of state change sequence when start() is called while BT device
* is connected and enabled:
* UNINITIALIZED --> HEADSET_UNAVAILABLE --> HEADSET_AVAILABLE -->
* SCO_CONNECTING --> SCO_CONNECTED <==> audio is now routed via BT SCO.
* Note that the AppRTCAudioManager is also involved in driving this state
* change.
*/
fun start() {
ThreadUtils.checkIsOnMainThread()
Timber.d("start")
if (!hasPermission(apprtcContext, Manifest.permission.BLUETOOTH) &&
!(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S &&
hasPermission(apprtcContext, Manifest.permission.BLUETOOTH_CONNECT))
) {
Timber.w("Process (pid=" + Process.myPid() + ") lacks BLUETOOTH permission")
return
}
if (bluetoothState != State.UNINITIALIZED) {
Timber.w("Invalid BT state")
return
}
bluetoothHeadset = null
bluetoothDevice = null
scoConnectionAttempts = 0
// Get a handle to the default local Bluetooth adapter.
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
if (bluetoothAdapter == null) {
Timber.w("Device does not support Bluetooth")
return
}
// Ensure that the device supports use of BT SCO audio for off call use cases.
if (!audioManager!!.isBluetoothScoAvailableOffCall) {
Timber.e("Bluetooth SCO audio is not available off call")
return
}
logBluetoothAdapterInfo(bluetoothAdapter!!)
// Establish a connection to the HEADSET profile (includes both Bluetooth Headset and
// Hands-Free) proxy object and install a listener.
if (!getBluetoothProfileProxy(
apprtcContext, bluetoothServiceListener, BluetoothProfile.HEADSET
)
) {
Timber.e("BluetoothAdapter.getProfileProxy(HEADSET) failed")
return
}
// Register receivers for BluetoothHeadset change notifications.
val bluetoothHeadsetFilter = IntentFilter()
// Register receiver for change in connection state of the Headset profile.
bluetoothHeadsetFilter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)
// Register receiver for change in audio connection state of the Headset profile.
bluetoothHeadsetFilter.addAction(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED)
registerReceiver(bluetoothHeadsetReceiver, bluetoothHeadsetFilter)
Timber.d(
"HEADSET profile state: "
+ stateToString(bluetoothAdapter!!.getProfileConnectionState(BluetoothProfile.HEADSET))
)
Timber.d("Bluetooth proxy for headset profile has started")
bluetoothState = State.HEADSET_UNAVAILABLE
Timber.d("start done: BT state=$bluetoothState")
}
/** Stops and closes all components related to Bluetooth audio. */
fun stop() {
ThreadUtils.checkIsOnMainThread()
Timber.d("stop: BT state=$bluetoothState")
if (bluetoothAdapter == null) {
return
}
// Stop BT SCO connection with remote device if needed.
stopScoAudio()
// Close down remaining BT resources.
if (bluetoothState == State.UNINITIALIZED) {
return
}
unregisterReceiver(bluetoothHeadsetReceiver)
cancelTimer()
if (bluetoothHeadset != null) {
bluetoothAdapter!!.closeProfileProxy(BluetoothProfile.HEADSET, bluetoothHeadset)
bluetoothHeadset = null
}
bluetoothAdapter = null
bluetoothDevice = null
bluetoothState = State.UNINITIALIZED
Timber.d("stop done: BT state=$bluetoothState")
}
/**
* Starts Bluetooth SCO connection with remote device.
* Note that the phone application always has the priority on the usage of the SCO connection
* for telephony. If this method is called while the phone is in call it will be ignored.
* Similarly, if a call is received or sent while an application is using the SCO connection,
* the connection will be lost for the application and NOT returned automatically when the call
* ends. Also note that: up to and including API version JELLY_BEAN_MR1, this method initiates a
* virtual voice call to the Bluetooth headset. After API version JELLY_BEAN_MR2 only a raw SCO
* audio connection is established.
* TODO(henrika): should we add support for virtual voice call to BT headset also for JBMR2 and
* higher. It might be required to initiates a virtual voice call since many devices do not
* accept SCO audio without a "call".
*/
fun startScoAudio(): Boolean {
ThreadUtils.checkIsOnMainThread()
Timber.d(
"startSco: BT state=" + bluetoothState + ", "
+ "attempts: " + scoConnectionAttempts + ", "
+ "SCO is on: " + isScoOn
)
if (scoConnectionAttempts >= MAX_SCO_CONNECTION_ATTEMPTS) {
Timber.e("BT SCO connection fails - no more attempts")
return false
}
if (bluetoothState != State.HEADSET_AVAILABLE) {
Timber.e("BT SCO connection fails - no headset available")
return false
}
// Start BT SCO channel and wait for ACTION_AUDIO_STATE_CHANGED.
Timber.d("Starting Bluetooth SCO and waits for ACTION_AUDIO_STATE_CHANGED...")
// The SCO connection establishment can take several seconds, hence we cannot rely on the
// connection to be available when the method returns but instead register to receive the
// intent ACTION_SCO_AUDIO_STATE_UPDATED and wait for the state to be SCO_AUDIO_STATE_CONNECTED.
bluetoothState = State.SCO_CONNECTING
audioManager!!.startBluetoothSco()
audioManager.isBluetoothScoOn = true
scoConnectionAttempts++
startTimer()
Timber.d(
"startScoAudio done: BT state=" + bluetoothState + ", "
+ "SCO is on: " + isScoOn
)
return true
}
/** Stops Bluetooth SCO connection with remote device. */
fun stopScoAudio() {
ThreadUtils.checkIsOnMainThread()
Timber.d(
"stopScoAudio: BT state=" + bluetoothState + ", "
+ "SCO is on: " + isScoOn
)
if (bluetoothState != State.SCO_CONNECTING && bluetoothState != State.SCO_CONNECTED) {
return
}
cancelTimer()
audioManager!!.stopBluetoothSco()
audioManager.isBluetoothScoOn = false
bluetoothState = State.SCO_DISCONNECTING
Timber.d(
"stopScoAudio done: BT state=" + bluetoothState + ", "
+ "SCO is on: " + isScoOn
)
}
/**
* Use the BluetoothHeadset proxy object (controls the Bluetooth Headset
* Service via IPC) to update the list of connected devices for the HEADSET
* profile. The internal state will change to HEADSET_UNAVAILABLE or to
* HEADSET_AVAILABLE and |bluetoothDevice| will be mapped to the connected
* device if available.
*/
fun updateDevice() {
if (bluetoothState == State.UNINITIALIZED || bluetoothHeadset == null) {
return
}
Timber.d("updateDevice")
// Get connected devices for the headset profile. Returns the set of
// devices which are in state STATE_CONNECTED. The BluetoothDevice class
// is just a thin wrapper for a Bluetooth hardware address.
val devices = bluetoothHeadset!!.connectedDevices
if (devices.isEmpty()) {
bluetoothDevice = null
bluetoothState = State.HEADSET_UNAVAILABLE
Timber.d("No connected bluetooth headset")
} else {
// Always use first device in list. Android only supports one device.
bluetoothDevice = devices[0]
bluetoothState = State.HEADSET_AVAILABLE
Timber.d(
"Connected bluetooth headset: "
+ "name=" + bluetoothDevice!!.name + ", "
+ "state=" + stateToString(bluetoothHeadset!!.getConnectionState(bluetoothDevice))
+ ", SCO audio=" + bluetoothHeadset!!.isAudioConnected(bluetoothDevice)
)
}
Timber.d("updateDevice done: BT state=$bluetoothState")
}
/**
* Stubs for test mocks.
*/
protected fun getAudioManager(context: Context): AudioManager? {
return context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
}
protected fun registerReceiver(receiver: BroadcastReceiver?, filter: IntentFilter?) {
apprtcContext.registerReceiver(receiver, filter)
}
protected fun unregisterReceiver(receiver: BroadcastReceiver?) {
apprtcContext.unregisterReceiver(receiver)
}
protected fun getBluetoothProfileProxy(
context: Context?, listener: BluetoothProfile.ServiceListener?, profile: Int
): Boolean {
return bluetoothAdapter!!.getProfileProxy(context, listener, profile)
}
protected fun hasPermission(context: Context?, permission: String?): Boolean {
return (apprtcContext.checkPermission(permission!!, Process.myPid(), Process.myUid())
== PackageManager.PERMISSION_GRANTED)
}
/** Logs the state of the local Bluetooth adapter. */
@SuppressLint("HardwareIds")
protected fun logBluetoothAdapterInfo(localAdapter: BluetoothAdapter) {
Timber.d(
"BluetoothAdapter: "
+ "enabled=" + localAdapter.isEnabled + ", "
+ "state=" + stateToString(localAdapter.state) + ", "
+ "name=" + localAdapter.name + ", "
+ "address=" + localAdapter.address
)
// Log the set of BluetoothDevice objects that are bonded (paired) to the local adapter.
val pairedDevices = localAdapter.bondedDevices
if (!pairedDevices.isEmpty()) {
Timber.d("paired devices:")
for (device in pairedDevices) {
Timber.d(" name=" + device.name + ", address=" + device.address)
}
}
}
/** Ensures that the audio manager updates its list of available audio devices. */
private fun updateAudioDeviceState() {
ThreadUtils.checkIsOnMainThread()
Timber.d("updateAudioDeviceState")
apprtcAudioManager.updateAudioDeviceState()
}
/** Starts timer which times out after BLUETOOTH_SCO_TIMEOUT_MS milliseconds. */
private fun startTimer() {
ThreadUtils.checkIsOnMainThread()
Timber.d("startTimer")
handler.postDelayed(bluetoothTimeoutRunnable, BLUETOOTH_SCO_TIMEOUT_MS.toLong())
}
/** Cancels any outstanding timer tasks. */
private fun cancelTimer() {
ThreadUtils.checkIsOnMainThread()
Timber.d("cancelTimer")
handler.removeCallbacks(bluetoothTimeoutRunnable)
}
/**
* Called when start of the BT SCO channel takes too long time. Usually
* happens when the BT device has been turned on during an ongoing call.
*/
private fun bluetoothTimeout() {
ThreadUtils.checkIsOnMainThread()
if (bluetoothState == State.UNINITIALIZED || bluetoothHeadset == null) {
return
}
Timber.d(
"bluetoothTimeout: BT state=" + bluetoothState + ", "
+ "attempts: " + scoConnectionAttempts + ", "
+ "SCO is on: " + isScoOn
)
if (bluetoothState != State.SCO_CONNECTING) {
return
}
// Bluetooth SCO should be connecting; check the latest result.
var scoConnected = false
val devices = bluetoothHeadset!!.connectedDevices
if (devices.size > 0) {
bluetoothDevice = devices[0]
if (bluetoothHeadset!!.isAudioConnected(bluetoothDevice)) {
Timber.d("SCO connected with " + bluetoothDevice!!.name)
scoConnected = true
} else {
Timber.d("SCO is not connected with " + bluetoothDevice!!.name)
}
}
if (scoConnected) {
// We thought BT had timed out, but it's actually on; updating state.
bluetoothState = State.SCO_CONNECTED
scoConnectionAttempts = 0
} else {
// Give up and "cancel" our request by calling stopBluetoothSco().
Timber.w("BT failed to connect after timeout")
stopScoAudio()
}
updateAudioDeviceState()
Timber.d("bluetoothTimeout done: BT state=$bluetoothState")
}
/** Checks whether audio uses Bluetooth SCO. */
private val isScoOn: Boolean
private get() = audioManager!!.isBluetoothScoOn
/** Converts BluetoothAdapter states into local string representations. */
private fun stateToString(state: Int): String {
return when (state) {
BluetoothAdapter.STATE_DISCONNECTED -> "DISCONNECTED"
BluetoothAdapter.STATE_CONNECTED -> "CONNECTED"
BluetoothAdapter.STATE_CONNECTING -> "CONNECTING"
BluetoothAdapter.STATE_DISCONNECTING -> "DISCONNECTING"
BluetoothAdapter.STATE_OFF -> "OFF"
BluetoothAdapter.STATE_ON -> "ON"
BluetoothAdapter.STATE_TURNING_OFF -> // Indicates the local Bluetooth adapter is turning off. Local clients should immediately
// attempt graceful disconnection of any remote links.
"TURNING_OFF"
BluetoothAdapter.STATE_TURNING_ON -> // Indicates the local Bluetooth adapter is turning on. However local clients should wait
// for STATE_ON before attempting to use the adapter.
"TURNING_ON"
else -> "INVALID"
}
}
companion object {
private const val TAG = "AppRTCBluetoothManager"
// Timeout interval for starting or stopping audio to a Bluetooth SCO device.
private const val BLUETOOTH_SCO_TIMEOUT_MS = 4000
// Maximum number of SCO connection attempts.
private const val MAX_SCO_CONNECTION_ATTEMPTS = 2
/** Construction. */
@JvmStatic
fun create(context: Context, audioManager: AppRTCAudioManager): AppRTCBluetoothManager {
Timber.d("create" + AppRTCUtils.threadInfo)
return AppRTCBluetoothManager(context, audioManager)
}
}
init {
Timber.d("ctor")
ThreadUtils.checkIsOnMainThread()
apprtcContext = context
apprtcAudioManager = audioManager
this.audioManager = getAudioManager(context)
bluetoothState = State.UNINITIALIZED
bluetoothServiceListener = BluetoothServiceListener()
bluetoothHeadsetReceiver = BluetoothHeadsetBroadcastReceiver()
handler = Handler(Looper.getMainLooper())
}
}
\ No newline at end of file
... ...
/*
* 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.
*/
package io.livekit.android.sample.audio
import android.annotation.SuppressLint
import android.content.Context
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.os.Build
import org.webrtc.ThreadUtils
import timber.log.Timber
/**
* AppRTCProximitySensor manages functions related to the proximity sensor in
* the AppRTC demo.
* On most device, the proximity sensor is implemented as a boolean-sensor.
* It returns just two values "NEAR" or "FAR". Thresholding is done on the LUX
* value i.e. the LUX value of the light sensor is compared with a threshold.
* A LUX-value more than the threshold means the proximity sensor returns "FAR".
* Anything less than the threshold value and the sensor returns "NEAR".
*/
class AppRTCProximitySensor
private constructor(context: Context, sensorStateListener: Runnable) : SensorEventListener {
// This class should be created, started and stopped on one thread
// (e.g. the main thread). We use |nonThreadSafe| to ensure that this is
// the case. Only active when |DEBUG| is set to true.
private val threadChecker = ThreadUtils.ThreadChecker()
private val onSensorStateListener: Runnable?
private val sensorManager: SensorManager
private var proximitySensor: Sensor? = null
private var lastStateReportIsNear = false
init {
Timber.d("AppRTCProximitySensor ${AppRTCUtils.threadInfo}")
onSensorStateListener = sensorStateListener
sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager
}
/**
* Activate the proximity sensor. Also do initialization if called for the
* first time.
*/
fun start(): Boolean {
threadChecker.checkIsOnValidThread()
Timber.d("start ${AppRTCUtils.threadInfo}")
if (!initDefaultSensor()) {
// Proximity sensor is not supported on this device.
return false
}
sensorManager.registerListener(this, proximitySensor, SensorManager.SENSOR_DELAY_NORMAL)
return true
}
/** Deactivate the proximity sensor. */
fun stop() {
threadChecker.checkIsOnValidThread()
Timber.d("stop ${AppRTCUtils.threadInfo}")
if (proximitySensor == null) {
return
}
sensorManager.unregisterListener(this, proximitySensor)
}
/** Getter for last reported state. Set to true if "near" is reported. */
fun sensorReportsNearState(): Boolean {
threadChecker.checkIsOnValidThread()
return lastStateReportIsNear
}
override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {
threadChecker.checkIsOnValidThread()
AppRTCUtils.assertIsTrue(sensor.type == Sensor.TYPE_PROXIMITY)
if (accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) {
Timber.e("The values returned by this sensor cannot be trusted")
}
}
override fun onSensorChanged(event: SensorEvent) {
threadChecker.checkIsOnValidThread()
AppRTCUtils.assertIsTrue(event.sensor.type == Sensor.TYPE_PROXIMITY)
// As a best practice; do as little as possible within this method and
// avoid blocking.
val distanceInCentimeters = event.values[0]
lastStateReportIsNear = if (distanceInCentimeters < proximitySensor!!.maximumRange) {
Timber.d("Proximity sensor => NEAR state")
true
} else {
Timber.d("Proximity sensor => FAR state")
false
}
// Report about new state to listening client. Client can then call
// sensorReportsNearState() to query the current state (NEAR or FAR).
onSensorStateListener?.run()
Timber.d(
"onSensorChanged ${AppRTCUtils.threadInfo}: accuracy=${event.accuracy}, timestamp=${event.timestamp}, distance=${event.values[0]}"
)
}
/**
* Get default proximity sensor if it exists. Tablet devices (e.g. Nexus 7)
* does not support this type of sensor and false will be returned in such
* cases.
*/
private fun initDefaultSensor(): Boolean {
if (proximitySensor != null) {
return true
}
proximitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY)
if (proximitySensor == null) {
return false
}
logProximitySensorInfo()
return true
}
/** Helper method for logging information about the proximity sensor. */
@SuppressLint("ObsoleteSdkInt")
private fun logProximitySensorInfo() {
if (proximitySensor == null) {
return
}
val info = StringBuilder("Proximity sensor: ")
info.append("name=").append(proximitySensor!!.name)
info.append(", vendor: ").append(proximitySensor!!.vendor)
info.append(", power: ").append(proximitySensor!!.power)
info.append(", resolution: ").append(proximitySensor!!.resolution)
info.append(", max range: ").append(proximitySensor!!.maximumRange)
info.append(", min delay: ").append(proximitySensor!!.minDelay)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
// Added in API level 20.
info.append(", type: ").append(proximitySensor!!.stringType)
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// Added in API level 21.
info.append(", max delay: ").append(proximitySensor!!.maxDelay)
info.append(", reporting mode: ").append(proximitySensor!!.reportingMode)
info.append(", isWakeUpSensor: ").append(proximitySensor!!.isWakeUpSensor)
}
Timber.d(info.toString())
}
companion object {
/** Construction */
@JvmStatic
fun create(context: Context, sensorStateListener: Runnable): AppRTCProximitySensor {
return AppRTCProximitySensor(context, sensorStateListener)
}
}
}
\ No newline at end of file
... ...
/*
* 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.
*/
package io.livekit.android.sample.audio
import android.annotation.SuppressLint
import android.os.Build
import timber.log.Timber
/**
* AppRTCUtils provides helper functions for managing thread safety.
*/
object AppRTCUtils {
/** Helper method which throws an exception when an assertion has failed. */
fun assertIsTrue(condition: Boolean) {
if (!condition) {
throw AssertionError("Expected condition to be true")
}
}
/** Helper method for building a string of thread information. */
val threadInfo: String
get() = ("@[name=" + Thread.currentThread().name + ", id=" + Thread.currentThread().id
+ "]")
/** Information about the current build, taken from system properties. */
@SuppressLint("BinaryOperationInTimber")
fun logDeviceInfo(tag: String?) {
Timber.tag(tag).d(
"Android SDK: " + Build.VERSION.SDK_INT + ", "
+ "Release: " + Build.VERSION.RELEASE + ", "
+ "Brand: " + Build.BRAND + ", "
+ "Device: " + Build.DEVICE + ", "
+ "Id: " + Build.ID + ", "
+ "Hardware: " + Build.HARDWARE + ", "
+ "Manufacturer: " + Build.MANUFACTURER + ", "
+ "Model: " + Build.MODEL + ", "
+ "Product: " + Build.PRODUCT
)
}
}
\ No newline at end of file
... ...
package io.livekit.android.sample.util
import android.Manifest
import android.content.pm.PackageManager
import android.os.Build
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat
fun ComponentActivity.requestNeededPermissions() {
val requestPermissionLauncher =
registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions()
) { grants ->
for (grant in grants.entries) {
if (!grant.value) {
Toast.makeText(
this,
"Missing permission: ${grant.key}",
Toast.LENGTH_SHORT
)
.show()
}
}
}
val neededPermissions = listOf(Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA)
.let { perms ->
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
perms + listOf(Manifest.permission.BLUETOOTH_CONNECT)
} else {
perms
}
}
.filter {
ContextCompat.checkSelfPermission(
this,
it
) == PackageManager.PERMISSION_DENIED
}
.toTypedArray()
if (neededPermissions.isNotEmpty()) {
requestPermissionLauncher.launch(neededPermissions)
}
}
\ No newline at end of file
... ...
... ... @@ -2,10 +2,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.livekit.android.composesample">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<application
android:allowBackup="true"
android:name=".SampleApplication"
... ...
package io.livekit.android.composesample
import android.app.Activity
import android.media.AudioManager
import android.media.projection.MediaProjectionManager
import android.os.Bundle
import android.os.Parcelable
... ... @@ -25,19 +24,15 @@ import androidx.compose.ui.unit.dp
import androidx.constraintlayout.compose.ConstraintLayout
import androidx.constraintlayout.compose.Dimension
import androidx.lifecycle.lifecycleScope
import com.github.ajalt.timberkt.Timber
import com.google.accompanist.pager.ExperimentalPagerApi
import io.livekit.android.composesample.ui.DebugMenuDialog
import io.livekit.android.composesample.ui.theme.AppTheme
import io.livekit.android.room.Room
import io.livekit.android.room.participant.Participant
import io.livekit.android.sample.CallViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlinx.parcelize.Parcelize
@OptIn(ExperimentalPagerApi::class)
class CallActivity : AppCompatActivity() {
private val viewModel: CallViewModel by viewModelByFactory {
... ... @@ -45,10 +40,6 @@ class CallActivity : AppCompatActivity() {
?: throw NullPointerException("args is null!")
CallViewModel(args.url, args.token, application)
}
private val focusChangeListener = AudioManager.OnAudioFocusChangeListener {}
private var previousSpeakerphoneOn = true
private var previousMicrophoneMute = false
private val screenCaptureIntentLauncher =
registerForActivityResult(
... ... @@ -67,26 +58,6 @@ class CallActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Obtain audio focus.
val audioManager = getSystemService(AUDIO_SERVICE) as AudioManager
with(audioManager) {
previousSpeakerphoneOn = isSpeakerphoneOn
previousMicrophoneMute = isMicrophoneMute
isSpeakerphoneOn = true
isMicrophoneMute = false
mode = AudioManager.MODE_IN_COMMUNICATION
}
val result = audioManager.requestAudioFocus(
focusChangeListener,
AudioManager.STREAM_VOICE_CALL,
AudioManager.AUDIOFOCUS_GAIN,
)
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
Timber.v { "Audio focus request granted for VOICE_CALL streams" }
} else {
Timber.v { "Audio focus request failed" }
}
// Setup compose view.
setContent {
val room by viewModel.room.collectAsState()
... ... @@ -451,19 +422,6 @@ class CallActivity : AppCompatActivity() {
}
}
override fun onDestroy() {
super.onDestroy()
// release audio focus and revert audio settings.
val audioManager = getSystemService(AUDIO_SERVICE) as AudioManager
with(audioManager) {
isSpeakerphoneOn = previousSpeakerphoneOn
isMicrophoneMute = previousMicrophoneMute
abandonAudioFocus(focusChangeListener)
mode = AudioManager.MODE_NORMAL
}
}
companion object {
const val KEY_ARGS = "args"
}
... ...
package io.livekit.android.composesample
import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
... ... @@ -20,10 +17,10 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.core.content.ContextCompat
import com.google.accompanist.pager.ExperimentalPagerApi
import io.livekit.android.composesample.ui.theme.AppTheme
import io.livekit.android.sample.MainViewModel
import io.livekit.android.sample.util.requestNeededPermissions
@ExperimentalPagerApi
class MainActivity : ComponentActivity() {
... ... @@ -32,7 +29,7 @@ class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requestPermissions()
requestNeededPermissions()
setContent {
MainContent(
defaultUrl = viewModel.getSavedUrl(),
... ... @@ -145,34 +142,4 @@ class MainActivity : ComponentActivity() {
}
}
}
private fun requestPermissions() {
val requestPermissionLauncher =
registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions()
) { grants ->
for (grant in grants.entries) {
if (!grant.value) {
Toast.makeText(
this,
"Missing permission: ${grant.key}",
Toast.LENGTH_SHORT
)
.show()
}
}
}
val neededPermissions = listOf(Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA)
.filter {
ContextCompat.checkSelfPermission(
this,
it
) == PackageManager.PERMISSION_DENIED
}
.toTypedArray()
if (neededPermissions.isNotEmpty()) {
requestPermissionLauncher.launch(neededPermissions)
}
}
}
... ...
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.livekit.android.sample">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<application
android:name=".SampleApplication"
android:networkSecurityConfig="@xml/network_security_config"
... ...
package io.livekit.android.sample
import android.app.Activity
import android.media.AudioManager
import android.media.projection.MediaProjectionManager
import android.os.Bundle
import android.os.Parcelable
... ... @@ -13,7 +12,6 @@ import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.github.ajalt.timberkt.Timber
import com.xwray.groupie.GroupieAdapter
import io.livekit.android.room.track.Track
import io.livekit.android.room.track.VideoTrack
... ... @@ -30,11 +28,6 @@ class CallActivity : AppCompatActivity() {
CallViewModel(args.url, args.token, application)
}
lateinit var binding: CallActivityBinding
val focusChangeListener = AudioManager.OnAudioFocusChangeListener {}
private var previousSpeakerphoneOn = true
private var previousMicrophoneMute = false
private val screenCaptureIntentLauncher =
registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
... ... @@ -180,26 +173,6 @@ class CallActivity : AppCompatActivity() {
}
binding.exit.setOnClickListener { finish() }
// Grab audio focus for video call
val audioManager = getSystemService(AUDIO_SERVICE) as AudioManager
with(audioManager) {
previousSpeakerphoneOn = isSpeakerphoneOn
previousMicrophoneMute = isMicrophoneMute
isSpeakerphoneOn = true
isMicrophoneMute = false
mode = AudioManager.MODE_IN_COMMUNICATION
}
val result = audioManager.requestAudioFocus(
focusChangeListener,
AudioManager.STREAM_VOICE_CALL,
AudioManager.AUDIOFOCUS_GAIN,
)
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
Timber.v { "Audio focus request granted for VOICE_CALL streams" }
} else {
Timber.v { "Audio focus request failed" }
}
}
override fun onResume() {
... ... @@ -231,15 +204,6 @@ class CallActivity : AppCompatActivity() {
// Release video views
binding.speakerVideoView.release()
// Undo audio mode changes
val audioManager = getSystemService(AUDIO_SERVICE) as AudioManager
with(audioManager) {
isSpeakerphoneOn = previousSpeakerphoneOn
isMicrophoneMute = previousMicrophoneMute
abandonAudioFocus(focusChangeListener)
mode = AudioManager.MODE_NORMAL
}
}
companion object {
... ...
... ... @@ -3,6 +3,7 @@ package io.livekit.android.sample
import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.text.SpannableStringBuilder
import android.widget.Toast
... ... @@ -11,6 +12,7 @@ import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import io.livekit.android.sample.databinding.MainActivityBinding
import io.livekit.android.sample.util.requestNeededPermissions
class MainActivity : AppCompatActivity() {
... ... @@ -67,36 +69,6 @@ class MainActivity : AppCompatActivity() {
setContentView(binding.root)
requestPermissions()
}
private fun requestPermissions() {
val requestPermissionLauncher =
registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions()
) { grants ->
for (grant in grants.entries) {
if (!grant.value) {
Toast.makeText(
this,
"Missing permission: ${grant.key}",
Toast.LENGTH_SHORT
)
.show()
}
}
}
val neededPermissions = listOf(Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA)
.filter {
ContextCompat.checkSelfPermission(
this,
it
) == PackageManager.PERMISSION_DENIED
}
.toTypedArray()
if (neededPermissions.isNotEmpty()) {
requestPermissionLauncher.launch(neededPermissions)
}
requestNeededPermissions()
}
}
\ No newline at end of file
... ...