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,37 +163,44 @@ 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,
channelConfiguration, channelConfiguration,
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
val readBuffer = ByteArray(byteBufferSize) }
val decodedBuffer = ShortArray(byteBufferSize * 2)
while (socket.isConnected && read != -1 && !Thread.currentThread().isInterrupted) { runCatching { block(audioTrack) }
read = inputStream.read(readBuffer) audioTrack.stop()
val decoded: Int = AudioCodecDefines.CODEC.decode(decodedBuffer, readBuffer, read, 0) }
if (decoded > 0) {
audioTrack.write(decodedBuffer, 0, decoded) private fun streamAudio(inputStream: InputStream, audioTrack: AudioTrack) {
val decodedBytes = ShortArray(decoded) val readBuffer = ByteArray(byteBufferSize)
System.arraycopy(decodedBuffer, 0, decodedBytes, 0, decoded) val decodedBuffer = ShortArray(byteBufferSize * 2)
volumeHistory.onAudioData(decodedBytes) while (!Thread.currentThread().isInterrupted) {
updateCallback?.invoke() val read = runCatching { inputStream.read(readBuffer) }
} 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) {
audioTrack.write(decodedBuffer, 0, decoded)
val decodedBytes = ShortArray(decoded)
System.arraycopy(decodedBuffer, 0, decodedBytes, 0, decoded)
volumeHistory.onAudioData(decodedBytes)
updateCallback?.invoke()
} }
} catch (e: Exception) {
Log.e(TAG, "Connection failed", e)
} finally {
audioTrack.stop()
socket.close()
} }
} }