Wrap java exceptions in runCatching blocks

This commit is contained in:
Fabian Wiesel
2024-02-26 20:42:45 +01:00
parent c6c60d9dbd
commit 41506bc601

View File

@@ -33,7 +33,7 @@ import android.util.Log
import android.widget.Toast import android.widget.Toast
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.app.ServiceCompat import androidx.core.app.ServiceCompat
import java.io.IOException import java.io.InputStream
import java.net.Socket import java.net.Socket
class ListenService : Service() { class ListenService : Service() {
@@ -136,11 +136,19 @@ class ListenService : Service() {
private var updateCallback: (() -> Unit)? = null private var updateCallback: (() -> Unit)? = null
private fun doListen(address: String?, port: Int) { private fun doListen(address: String?, port: Int) {
val lt = Thread { val lt = Thread {
try { Socket(address, port).use { socket ->
val socket = Socket(address, port) val result = runCatching { socket.getInputStream() }
streamAudio(socket) if (result.isSuccess) {
} catch (e: IOException) { result.getOrNull()?.let {inputStream ->
Log.e(TAG, "Failed to stream audio", e) withAudioTrack { audioTrack ->
streamAudio(inputStream, audioTrack)
}
}
}
else {
val exception = result.exceptionOrNull()
Log.e(TAG, "Failed to stream audio", exception)
}
} }
if (!Thread.currentThread().isInterrupted) { if (!Thread.currentThread().isInterrupted) {
// If this thread has not been interrupted, likely something // If this thread has not been interrupted, likely something
@@ -155,8 +163,7 @@ class ListenService : Service() {
lt.start() lt.start()
} }
@Throws(IllegalArgumentException::class, IllegalStateException::class, IOException::class) private fun withAudioTrack(block: (AudioTrack) -> Unit): Unit {
private fun streamAudio(socket: Socket) {
Log.i(TAG, "Setting up stream") Log.i(TAG, "Setting up stream")
val audioTrack = AudioTrack(AudioManager.STREAM_MUSIC, val audioTrack = AudioTrack(AudioManager.STREAM_MUSIC,
frequency, frequency,
@@ -164,15 +171,29 @@ class ListenService : Service() {
audioEncoding, audioEncoding,
bufferSize, bufferSize,
AudioTrack.MODE_STREAM) AudioTrack.MODE_STREAM)
val inputStream = socket.getInputStream() val playback = runCatching { audioTrack.play() }
var read = 0 if (playback.isFailure) {
audioTrack.play() Log.e(TAG, "Failed to start output due to ", playback.exceptionOrNull())
try { return
}
runCatching { block(audioTrack) }
audioTrack.stop()
}
private fun streamAudio(inputStream: InputStream, audioTrack: AudioTrack) {
val readBuffer = ByteArray(byteBufferSize) val readBuffer = ByteArray(byteBufferSize)
val decodedBuffer = ShortArray(byteBufferSize * 2) val decodedBuffer = ShortArray(byteBufferSize * 2)
while (socket.isConnected && read != -1 && !Thread.currentThread().isInterrupted) { while (!Thread.currentThread().isInterrupted) {
read = inputStream.read(readBuffer) val read = runCatching { inputStream.read(readBuffer) }
val decoded: Int = AudioCodecDefines.CODEC.decode(decodedBuffer, readBuffer, read, 0) if (read.isFailure) {
return
}
val len = read.getOrDefault(-1)
if (len < 0) {
return
}
val decoded: Int = AudioCodecDefines.CODEC.decode(decodedBuffer, readBuffer, len, 0)
if (decoded > 0) { if (decoded > 0) {
audioTrack.write(decodedBuffer, 0, decoded) audioTrack.write(decodedBuffer, 0, decoded)
val decodedBytes = ShortArray(decoded) val decodedBytes = ShortArray(decoded)
@@ -181,12 +202,6 @@ class ListenService : Service() {
updateCallback?.invoke() updateCallback?.invoke()
} }
} }
} catch (e: Exception) {
Log.e(TAG, "Connection failed", e)
} finally {
audioTrack.stop()
socket.close()
}
} }
private fun playAlert() { private fun playAlert() {