Skip to content

Commit

Permalink
android-app: fix no sound on android api 23 #117
Browse files Browse the repository at this point in the history
  • Loading branch information
mkckr0 committed Dec 29, 2024
1 parent 9a5fca1 commit e26b670
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
}
}

0 comments on commit e26b670

Please sign in to comment.