From e26b67084e9769e1b941293ff6be37c870f6ea68 Mon Sep 17 00:00:00 2001 From: mkckr0 <26296627+mkckr0@users.noreply.github.com> Date: Sun, 29 Dec 2024 20:47:13 +0800 Subject: [PATCH] android-app: fix no sound on android api 23 #117 --- .../mkckr0/audio_share_app/service/AudioPlayer.kt | 1 + .../mkckr0/audio_share_app/service/NetClient.kt | 13 ++++++++++--- .../mkckr0/audio_share_app/service/NetworkIO.kt | 15 +++++++++++++-- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/android-app/app/src/main/java/io/github/mkckr0/audio_share_app/service/AudioPlayer.kt b/android-app/app/src/main/java/io/github/mkckr0/audio_share_app/service/AudioPlayer.kt index 683b2af..355a56d 100644 --- a/android-app/app/src/main/java/io/github/mkckr0/audio_share_app/service/AudioPlayer.kt +++ b/android-app/app/src/main/java/io/github/mkckr0/audio_share_app/service/AudioPlayer.kt @@ -286,6 +286,7 @@ class AudioPlayer(val context: Context) : SimpleBasePlayer(Looper.getMainLooper( } override suspend fun onReceiveAudioData(audioData: ByteBuffer) { +// Log.d(tag, "${audioData.remaining()}") audioTrack.write(audioData, audioData.remaining(), AudioTrack.WRITE_NON_BLOCKING) } diff --git a/android-app/app/src/main/java/io/github/mkckr0/audio_share_app/service/NetClient.kt b/android-app/app/src/main/java/io/github/mkckr0/audio_share_app/service/NetClient.kt index d279754..a63b33f 100644 --- a/android-app/app/src/main/java/io/github/mkckr0/audio_share_app/service/NetClient.kt +++ b/android-app/app/src/main/java/io/github/mkckr0/audio_share_app/service/NetClient.kt @@ -19,12 +19,15 @@ package io.github.mkckr0.audio_share_app.service import android.util.Log import io.github.mkckr0.audio_share_app.pb.Client.AudioFormat import io.ktor.network.selector.SelectorManager +import io.ktor.network.sockets.BoundDatagramSocket import io.ktor.network.sockets.ConnectedDatagramSocket import io.ktor.network.sockets.InetSocketAddress import io.ktor.network.sockets.Socket import io.ktor.network.sockets.aSocket import io.ktor.network.sockets.openReadChannel import io.ktor.network.sockets.openWriteChannel +import io.ktor.network.sockets.toJavaAddress +import io.ktor.util.network.address import kotlinx.coroutines.CoroutineExceptionHandler import kotlinx.coroutines.CoroutineName import kotlinx.coroutines.CoroutineScope @@ -63,7 +66,8 @@ class NetClient { private val selectorManager get() = _selectorManager!! private var _tcpSocket: Socket? = null private val tcpSocket get() = _tcpSocket!! - private var _udpSocket: ConnectedDatagramSocket? = null +// private var _udpSocket: ConnectedDatagramSocket? = null + private var _udpSocket: BoundDatagramSocket? = null private val udpSocket get() = _udpSocket!! private var _heartbeatLastTick = TimeSource.Monotonic.markNow() @@ -152,8 +156,10 @@ class NetClient { onPlaybackStarted() } +// _udpSocket = aSocket(selectorManager).udp() +// .connect(InetSocketAddress(host, port)) _udpSocket = aSocket(selectorManager).udp() - .connect(InetSocketAddress(host, port)) + .bind(InetSocketAddress(tcpSocket.localAddress.toJavaAddress().address, 0)) // heartbeat loop scope.launch { @@ -179,7 +185,8 @@ class NetClient { // audio data read loop scope.launch { - udpSocket.writeIntLE(id) + udpSocket.writeIntLE(id, InetSocketAddress(host, port)) +// udpSocket.writeIntLE(id) while (true) { val buf = udpSocket.readByteBuffer() _callback?.launch { diff --git a/android-app/app/src/main/java/io/github/mkckr0/audio_share_app/service/NetworkIO.kt b/android-app/app/src/main/java/io/github/mkckr0/audio_share_app/service/NetworkIO.kt index fc2d085..16bdeb9 100644 --- a/android-app/app/src/main/java/io/github/mkckr0/audio_share_app/service/NetworkIO.kt +++ b/android-app/app/src/main/java/io/github/mkckr0/audio_share_app/service/NetworkIO.kt @@ -18,8 +18,12 @@ package io.github.mkckr0.audio_share_app.service import io.github.mkckr0.audio_share_app.pb.Client.AudioFormat import io.github.mkckr0.audio_share_app.service.NetClient.CMD +import io.ktor.network.sockets.BoundDatagramSocket import io.ktor.network.sockets.ConnectedDatagramSocket import io.ktor.network.sockets.Datagram +import io.ktor.network.sockets.DatagramReadChannel +import io.ktor.network.sockets.DatagramWriteChannel +import io.ktor.network.sockets.SocketAddress import io.ktor.utils.io.ByteReadChannel import io.ktor.utils.io.ByteWriteChannel import io.ktor.utils.io.core.build @@ -62,6 +66,13 @@ suspend fun ConnectedDatagramSocket.writeIntLE(value: Int) { }.build(), remoteAddress)) } -suspend fun ConnectedDatagramSocket.readByteBuffer(): ByteBuffer { +suspend fun DatagramWriteChannel.writeIntLE(value: Int, address: SocketAddress) { + send(Datagram(Buffer().apply { + this.writeIntLe(value) + }.build(), address)) +} + +suspend fun DatagramReadChannel.readByteBuffer(): ByteBuffer { return ByteBuffer.wrap(receive().packet.readByteArray()) -} \ No newline at end of file +} +