Skip to content

Commit

Permalink
fix send ack if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroSG94 committed Oct 11, 2023
1 parent eca8783 commit 1a19e74
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
20 changes: 20 additions & 0 deletions rtmp/src/main/java/com/pedro/rtmp/rtmp/CommandsManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ abstract class CommandsManager {
var readChunkSize = RtmpConfig.DEFAULT_CHUNK_SIZE
var audioDisabled = false
var videoDisabled = false
private var bytesRead = 0
private var acknowledgementSequence = 0

protected var width = 640
protected var height = 480
Expand Down Expand Up @@ -126,6 +128,7 @@ abstract class CommandsManager {
val message = RtmpMessage.getRtmpMessage(input, readChunkSize, sessionHistory)
sessionHistory.setReadHeader(message.header)
Log.i(TAG, "read $message")
bytesRead += message.header.getPacketLength()
return message
}

Expand Down Expand Up @@ -178,6 +181,21 @@ abstract class CommandsManager {
}
}

suspend fun checkAndSendAcknowledgement(socket: RtmpSocket) {
writeSync.withLock {
if (bytesRead >= RtmpConfig.acknowledgementWindowSize) {
acknowledgementSequence += bytesRead
bytesRead -= RtmpConfig.acknowledgementWindowSize
val output = socket.getOutStream()
val acknowledgement = Acknowledgement(acknowledgementSequence)
acknowledgement.writeHeader(output)
acknowledgement.writeBody(output)
output.flush()
Log.i(TAG, "send $acknowledgement")
}
}
}

@Throws(IOException::class)
suspend fun sendVideoPacket(flvPacket: FlvPacket, socket: RtmpSocket): Int {
writeSync.withLock {
Expand Down Expand Up @@ -221,5 +239,7 @@ abstract class CommandsManager {
commandId = 0
readChunkSize = RtmpConfig.DEFAULT_CHUNK_SIZE
sessionHistory.reset()
acknowledgementSequence = 0
bytesRead = 0
}
}
1 change: 1 addition & 0 deletions rtmp/src/main/java/com/pedro/rtmp/rtmp/RtmpClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ class RtmpClient(private val connectCheckerRtmp: ConnectCheckerRtmp) {
var socket = this.socket ?: throw IOException("Invalid socket, Connection failed")

val message = commandsManager.readMessageResponse(socket)
commandsManager.checkAndSendAcknowledgement(socket)
when (message.getType()) {
MessageType.SET_CHUNK_SIZE -> {
val setChunkSize = message as SetChunkSize
Expand Down
2 changes: 1 addition & 1 deletion rtmp/src/main/java/com/pedro/rtmp/utils/RtmpConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ package com.pedro.rtmp.utils
object RtmpConfig {
const val DEFAULT_CHUNK_SIZE = 128
var writeChunkSize = DEFAULT_CHUNK_SIZE
var acknowledgementWindowSize = 0
var acknowledgementWindowSize = Int.MAX_VALUE
}

0 comments on commit 1a19e74

Please sign in to comment.