Skip to content

Commit

Permalink
use no buffered socket and flush withcontext
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroSG94 committed Sep 18, 2024
1 parent dd4ded4 commit 3a3386c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ abstract class RtmpSocket {

abstract fun getOutStream(): OutputStream
abstract fun getInputStream(): InputStream
abstract fun flush(isPacket: Boolean = false)
abstract suspend fun flush(isPacket: Boolean = false)
abstract fun connect()
abstract fun close()
abstract fun isConnected(): Boolean
Expand Down
8 changes: 5 additions & 3 deletions rtmp/src/main/java/com/pedro/rtmp/utils/socket/TcpSocket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.pedro.rtmp.utils.socket

import com.pedro.common.TLSSocketFactory
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.IOException
Expand All @@ -40,13 +42,13 @@ class TcpSocket(

private var socket: Socket = Socket()
private var input = ByteArrayInputStream(byteArrayOf()).buffered()
private var output = ByteArrayOutputStream().buffered()
private var output: OutputStream = ByteArrayOutputStream()

override fun getOutStream(): OutputStream = output

override fun getInputStream(): InputStream = input

override fun flush(isPacket: Boolean) {
override suspend fun flush(isPacket: Boolean) = withContext(Dispatchers.IO) {
getOutStream().flush()
}

Expand All @@ -63,7 +65,7 @@ class TcpSocket(
val socketAddress: SocketAddress = InetSocketAddress(host, port)
socket.connect(socketAddress, timeout)
}
output = socket.getOutputStream().buffered()
output = socket.getOutputStream()
input = socket.getInputStream().buffered()
socket.soTimeout = timeout
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package com.pedro.rtmp.utils.socket

import android.util.Log
import com.pedro.common.TimeUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.*
import java.net.HttpURLConnection
import java.net.SocketTimeoutException
Expand Down Expand Up @@ -63,13 +65,13 @@ class TcpTunneledSocket(private val host: String, private val port: Int, private
return input
}

override fun flush(isPacket: Boolean) {
override suspend fun flush(isPacket: Boolean) = withContext(Dispatchers.IO) {
synchronized(sync) {
if (isPacket && storedPackets < maxStoredPackets) {
storedPackets++
return
return@withContext
}
if (!connected) return
if (!connected) return@withContext
val i = index.addAndGet(1)
val bytes = output.toByteArray()
output.reset()
Expand Down

0 comments on commit 3a3386c

Please sign in to comment.