Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions common/src/main/java/com/pedro/common/base/BaseSender.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.runInterruptible
import kotlinx.coroutines.withTimeoutOrNull
import java.nio.ByteBuffer
import java.util.concurrent.atomic.AtomicLong
import kotlin.time.Duration.Companion.milliseconds

abstract class BaseSender(
protected val connectChecker: ConnectChecker,
Expand Down Expand Up @@ -119,15 +121,19 @@ abstract class BaseSender(
}
}

suspend fun stop(clear: Boolean = true) {
suspend fun stop(clear: Boolean = true, unlockNeeded: suspend () -> Unit = {}) {
running = false
stopImp(clear)
resetSentAudioFrames()
resetSentVideoFrames()
resetDroppedAudioFrames()
resetDroppedVideoFrames()
resetBytesSend()
job?.cancelAndJoin()
val stopped = withTimeoutOrNull(1000.milliseconds) { job?.cancelAndJoin() } != null
if (!stopped) {
unlockNeeded()
withTimeoutOrNull(1000.milliseconds) { job?.cancelAndJoin() }
}
job = null
queue.clear { bufferPool.release(it.data) }
bufferPool.clear()
Expand Down
4 changes: 2 additions & 2 deletions rtmp/src/main/java/com/pedro/rtmp/rtmp/RtmpClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ import com.pedro.common.validMessage
import com.pedro.rtmp.rtmp.message.Abort
import com.pedro.rtmp.rtmp.message.Acknowledgement
import com.pedro.rtmp.rtmp.message.Aggregate
import com.pedro.rtmp.rtmp.message.Command
import com.pedro.rtmp.rtmp.message.MessageType
import com.pedro.rtmp.rtmp.message.SetChunkSize
import com.pedro.rtmp.rtmp.message.SetPeerBandwidth
import com.pedro.rtmp.rtmp.message.WindowAcknowledgementSize
import com.pedro.rtmp.rtmp.message.Command
import com.pedro.rtmp.rtmp.message.control.Type
import com.pedro.rtmp.rtmp.message.control.UserControl
import com.pedro.rtmp.utils.AuthUtil
Expand Down Expand Up @@ -566,7 +566,7 @@ class RtmpClient(private val connectChecker: ConnectChecker) {
}

private suspend fun disconnect(clear: Boolean) {
if (isStreaming) rtmpSender.stop(clear)
if (isStreaming) rtmpSender.stop(clear, unlockNeeded = { socket?.close() })
runCatching {
withTimeoutOrNull(100.milliseconds) {
socket?.let { commandsManager.sendClose(it) }
Expand Down
8 changes: 4 additions & 4 deletions rtsp/src/main/java/com/pedro/rtsp/rtsp/RtspClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -426,16 +426,16 @@ class RtspClient(private val connectChecker: ConnectChecker) {
}

private suspend fun disconnect(clear: Boolean) {
if (isStreaming) rtspSender.stop()
if (isStreaming) rtspSender.stop(unlockNeeded = { socket?.close() })
val error = runCatching {
withTimeoutOrNull(100.milliseconds) {
socket?.write(commandsManager.createTeardown())
socket?.flush()
Log.i(TAG, "write teardown success")
}
socket?.close()
socket = null
Log.i(TAG, "write teardown success")
}.exceptionOrNull()
socket?.close()
socket = null
if (error != null) {
Log.e(TAG, "disconnect error", error)
}
Expand Down
Loading