Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make CallService sounds thread-safe #1201

Merged
merged 2 commits into from
Oct 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,9 @@ internal open class CallService : Service() {

if (trigger == TRIGGER_INCOMING_CALL) {
updateRingingCall(streamVideo, intentCallId, RingingState.Incoming())
if (mediaPlayer == null) mediaPlayer = MediaPlayer()
instantiateMediaPlayer()
} else if (trigger == TRIGGER_OUTGOING_CALL) {
if (mediaPlayer == null) mediaPlayer = MediaPlayer()
instantiateMediaPlayer()
}
observeCall(intentCallId, streamVideo)
registerToggleCameraBroadcastReceiver()
Expand Down Expand Up @@ -424,6 +424,12 @@ internal open class CallService : Service() {
}
}

private fun instantiateMediaPlayer() {
synchronized(this) {
if (mediaPlayer == null) mediaPlayer = MediaPlayer()
}
}

private fun observeCall(callId: StreamCallId, streamVideo: StreamVideoImpl) {
observeRingingState(callId, streamVideo)
observeCallEvents(callId, streamVideo)
Expand Down Expand Up @@ -480,16 +486,18 @@ internal open class CallService : Service() {

private fun playCallSound(soundUri: Uri?) {
try {
requestAudioFocus(
context = applicationContext,
onGranted = {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
playWithRingtone(soundUri)
} else {
playWithMediaPlayer(soundUri)
}
},
)
synchronized(this) {
requestAudioFocus(
context = applicationContext,
onGranted = {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
playWithRingtone(soundUri)
} else {
playWithMediaPlayer(soundUri)
}
},
)
}
} catch (e: Exception) {
logger.d { "[Sounds] Error playing call sound: ${e.message}" }
}
Expand Down Expand Up @@ -564,18 +572,20 @@ internal open class CallService : Service() {
}

private fun stopCallSound() {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
logger.d { "[Sounds] Stopping Ringtone sound" }
if (ringtone?.isPlaying == true) ringtone?.stop()
} else {
logger.d { "[Sounds] Stopping MediaPlayer sound" }
if (mediaPlayer?.isPlaying == true) mediaPlayer?.stop()
synchronized(this) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
logger.d { "[Sounds] Stopping Ringtone sound" }
if (ringtone?.isPlaying == true) ringtone?.stop()
} else {
logger.d { "[Sounds] Stopping MediaPlayer sound" }
if (mediaPlayer?.isPlaying == true) mediaPlayer?.stop()
}
} catch (e: Exception) {
logger.d { "[Sounds] Error stopping call sound: ${e.message}" }
} finally {
abandonAudioFocus()
}
} catch (e: Exception) {
logger.d { "[Sounds] Error stopping call sound: ${e.message}" }
} finally {
abandonAudioFocus()
}
}

Expand Down Expand Up @@ -675,17 +685,6 @@ internal open class CallService : Service() {
}
}

private fun unregisterToggleCameraBroadcastReceiver() {
if (isToggleCameraBroadcastReceiverRegistered) {
try {
unregisterReceiver(toggleCameraBroadcastReceiver)
isToggleCameraBroadcastReceiverRegistered = false
} catch (e: Exception) {
logger.d { "Unable to unregister ToggleCameraBroadcastReceiver." }
}
}
}

override fun onTimeout(startId: Int) {
super.onTimeout(startId)
logger.w { "Timeout received from the system, service will stop." }
Expand Down Expand Up @@ -775,17 +774,30 @@ internal open class CallService : Service() {
stopSelf()
}

private fun unregisterToggleCameraBroadcastReceiver() {
if (isToggleCameraBroadcastReceiverRegistered) {
try {
unregisterReceiver(toggleCameraBroadcastReceiver)
isToggleCameraBroadcastReceiverRegistered = false
} catch (e: Exception) {
logger.d { "Unable to unregister ToggleCameraBroadcastReceiver." }
}
}
}

private fun cleanAudioResources() {
logger.d { "[Sounds] Cleaning audio resources" }
synchronized(this) {
logger.d { "[Sounds] Cleaning audio resources" }

if (ringtone?.isPlaying == true) ringtone?.stop()
ringtone = null
if (ringtone?.isPlaying == true) ringtone?.stop()
ringtone = null

mediaPlayer?.release()
mediaPlayer = null
mediaPlayer?.release()
mediaPlayer = null

audioManager = null
audioFocusRequest = null
audioManager = null
audioFocusRequest = null
}
}

// This service does not return a Binder
Expand Down
Loading