diff --git a/common/src/main/java/com/pedro/common/base/BaseSender.kt b/common/src/main/java/com/pedro/common/base/BaseSender.kt index 7aa44e7e2..1c4cb8be6 100644 --- a/common/src/main/java/com/pedro/common/base/BaseSender.kt +++ b/common/src/main/java/com/pedro/common/base/BaseSender.kt @@ -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, @@ -119,7 +121,7 @@ abstract class BaseSender( } } - suspend fun stop(clear: Boolean = true) { + suspend fun stop(clear: Boolean = true, unlockNeeded: suspend () -> Unit = {}) { running = false stopImp(clear) resetSentAudioFrames() @@ -127,7 +129,11 @@ abstract class BaseSender( 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() diff --git a/rtmp/src/main/java/com/pedro/rtmp/rtmp/RtmpClient.kt b/rtmp/src/main/java/com/pedro/rtmp/rtmp/RtmpClient.kt index 30d11b879..43f3d30ce 100644 --- a/rtmp/src/main/java/com/pedro/rtmp/rtmp/RtmpClient.kt +++ b/rtmp/src/main/java/com/pedro/rtmp/rtmp/RtmpClient.kt @@ -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 @@ -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) } diff --git a/rtsp/src/main/java/com/pedro/rtsp/rtsp/RtspClient.kt b/rtsp/src/main/java/com/pedro/rtsp/rtsp/RtspClient.kt index 67720bddb..1ec0ae001 100644 --- a/rtsp/src/main/java/com/pedro/rtsp/rtsp/RtspClient.kt +++ b/rtsp/src/main/java/com/pedro/rtsp/rtsp/RtspClient.kt @@ -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) }