From 77422e304a6e5194699e8fa75b40df9642e69f9b Mon Sep 17 00:00:00 2001 From: Mario Danic Date: Thu, 12 Mar 2020 15:12:51 +0100 Subject: [PATCH] Fix #748 Signed-off-by: Mario Danic --- .../talk/controllers/CallController.kt | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/app/src/main/java/com/nextcloud/talk/controllers/CallController.kt b/app/src/main/java/com/nextcloud/talk/controllers/CallController.kt index 78e3a1b82..9828a4e12 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/CallController.kt +++ b/app/src/main/java/com/nextcloud/talk/controllers/CallController.kt @@ -28,6 +28,7 @@ import android.content.res.Configuration import android.graphics.Color import android.media.AudioAttributes import android.media.MediaPlayer +import android.media.MediaRecorder import android.net.Uri import android.os.Build import android.os.Bundle @@ -90,6 +91,8 @@ import java.io.IOException import java.net.CookieManager import java.util.* import java.util.concurrent.TimeUnit +import kotlin.math.abs +import kotlin.math.log10 class CallController(args: Bundle) : BaseController() { @@ -198,6 +201,9 @@ class CallController(args: Bundle) : BaseController() { private var hasExternalSignalingServer: Boolean = false private val conversationPassword: String + private var recorder: MediaRecorder = MediaRecorder() + private val timer = Timer() + private val powerManagerUtils: PowerManagerUtils private var handler: Handler? = null @@ -783,6 +789,10 @@ class CallController(args: Bundle) : BaseController() { } } + sendDataChannelMessage(message) + } + + private fun sendDataChannelMessage(message: String) { if (isConnectionEstablished) { if (!hasMCU) { for (i in magicPeerConnectionWrapperList.indices) { @@ -801,6 +811,44 @@ class CallController(args: Bundle) : BaseController() { } } + private fun startListening() { + recorder.setAudioSource(MediaRecorder.AudioSource.MIC) + recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP) + recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB) + timer.scheduleAtFixedRate(RecorderTask(recorder), 0, 1000) + recorder.setOutputFile("/dev/null") + + try { + recorder.prepare() + recorder.start() + } catch (e: IllegalStateException) { + e.printStackTrace() + } catch (e: IOException) { + e.printStackTrace() + } + } + + inner class RecorderTask(private val recorder: MediaRecorder) : TimerTask() { + private var speaking = false + override fun run() { + if (isConnectionEstablished) { + val amplitude: Int = recorder.maxAmplitude + val amplitudeDb = 20 * log10(abs(amplitude).toDouble()) + if (amplitudeDb >= 50) { + if (!speaking) { + speaking = true + sendDataChannelMessage("speaking") + } + } else { + if (speaking) { + speaking = false + sendDataChannelMessage("stoppedSpeaking") + } + } + } + } + } + private fun animateCallControls( show: Boolean, startDelay: Long @@ -1412,6 +1460,10 @@ class CallController(args: Bundle) : BaseController() { endPeerConnection(magicPeerConnectionWrapperList[i].sessionId, false) } + timer.cancel() + recorder.stop() + recorder.release() + hangupNetworkCalls(shutDownView) } @@ -2285,6 +2337,7 @@ class CallController(args: Bundle) : BaseController() { private fun playCallingSound() { stopCallingSound() + startListening() val ringtoneUri = Uri.parse( "android.resource://" + applicationContext!!.packageName