From df4874ab15e712336a1cdd08563bfed8c9a0f8c3 Mon Sep 17 00:00:00 2001 From: Juha Heinanen Date: Thu, 30 May 2019 14:46:48 +0300 Subject: [PATCH] - Don't set audio mode to .MODE_IN_COMMUNICATION and call is established, because on some devices it adds 3-4 sec delay to playing of audio. --- .../com/tutpro/baresip/BaresipService.kt | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt index 16d1eaf8..01f5daa1 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt @@ -56,6 +56,10 @@ class BaresipService: Service() { filesPath = filesDir.absolutePath am = getSystemService(Context.AUDIO_SERVICE) as AudioManager + // Setting this when call is established, causes on some devices 3-4 sec delay + // to hearing of audio + am.mode = AudioManager.MODE_IN_COMMUNICATION + val rtUri = RingtoneManager.getActualDefaultRingtoneUri(applicationContext, RingtoneManager.TYPE_RINGTONE) rt = RingtoneManager.getRingtone(applicationContext, rtUri) @@ -162,6 +166,8 @@ class BaresipService: Service() { if (Config.variable("dyn_dns")[0] == "yes") Config.remove("dns_server") + Log.d(LOG_TAG, "AudioManager mode is ${am.mode}") + } "Call Show", "Call Answer" -> { @@ -343,8 +349,6 @@ class BaresipService: Service() { updateStatusNotification() } "call ringing" -> { - am.mode = AudioManager.MODE_IN_COMMUNICATION - requestAudioFocus(AudioManager.STREAM_VOICE_CALL) } "call incoming" -> { val peerUri = Api.call_peeruri(callp) @@ -415,12 +419,8 @@ class BaresipService: Service() { CallHistory.add(CallHistory(aor, call.peerURI, call.dir, true)) CallHistory.save(filesPath) call.hasHistory = true - if (call.dir == "in") - stopRinging() - if (!isAudioFocused()) { - am.mode = AudioManager.MODE_IN_COMMUNICATION - requestAudioFocus(AudioManager.STREAM_VOICE_CALL) - } + if (call.dir == "in") stopRinging() + if (!isAudioFocused()) requestAudioFocus(AudioManager.STREAM_VOICE_CALL) if (callVolume != 0) { origCallVolume = am.getStreamVolume(am.mode) am.setStreamVolume(am.mode, @@ -500,8 +500,7 @@ class BaresipService: Service() { return } Log.d(LOG_TAG, "AoR $aor call $callp is closed") - if (call.status == "incoming") - stopRinging() + if (call.status == "incoming") stopRinging() calls.remove(call) if (!call.hasHistory) { CallHistory.add(CallHistory(aor, call.peerURI, call.dir, false)) @@ -515,7 +514,6 @@ class BaresipService: Service() { origCallVolume = -1 } Log.d(LOG_TAG, "Call volume of stream ${am.mode} is ${am.getStreamVolume(am.mode)}") - am.mode = AudioManager.MODE_NORMAL if (am.isSpeakerphoneOn) am.isSpeakerphoneOn = false speakerPhone = false } @@ -691,7 +689,7 @@ class BaresipService: Service() { } @TargetApi(26) if (am.requestAudioFocus(audioFocusRequest!!) == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { - Log.d(LOG_TAG, "Audio focus granted") + Log.d(LOG_TAG, "Audio focus granted for stream $streamType") } else { Log.d(LOG_TAG, "Audio focus denied") audioFocusRequest = null @@ -699,7 +697,7 @@ class BaresipService: Service() { } else { if (am.requestAudioFocus(null, streamType, AudioManager.AUDIOFOCUS_GAIN) == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { - Log.d(LOG_TAG, "Audio focus granted") + Log.d(LOG_TAG, "Audio focus granted for stream $streamType") audioFocused = true } else { Log.d(LOG_TAG, "Audio focus denied") @@ -751,6 +749,7 @@ class BaresipService: Service() { if (rt.isPlaying) rt.stop() } abandonAudioFocus() + am.mode = AudioManager.MODE_IN_COMMUNICATION } private fun cleanService() {