From a8a754110fdcaabfe5fd42c2bf9fa8347bb17a7a Mon Sep 17 00:00:00 2001 From: pedroSG94 Date: Mon, 28 Oct 2024 11:21:28 +0100 Subject: [PATCH] fix RTSP only video/audio --- .../com/pedro/rtsp/rtcp/BaseSenderReport.kt | 20 +++++++++++------- .../com/pedro/rtsp/rtcp/SenderReportUdp.kt | 16 +++++++------- .../pedro/rtsp/rtp/sockets/BaseRtpSocket.kt | 20 +++++++++++------- .../pedro/rtsp/rtp/sockets/RtpSocketUdp.kt | 16 +++++++------- .../java/com/pedro/rtsp/rtsp/RtspClient.kt | 21 +++++++++++++++---- .../java/com/pedro/rtsp/rtsp/RtspSender.kt | 4 ++-- .../pedro/rtsp/rtsp/commands/CommandParser.kt | 4 ++-- .../rtsp/rtsp/commands/CommandsManager.kt | 8 +++---- 8 files changed, 65 insertions(+), 44 deletions(-) diff --git a/rtsp/src/main/java/com/pedro/rtsp/rtcp/BaseSenderReport.kt b/rtsp/src/main/java/com/pedro/rtsp/rtcp/BaseSenderReport.kt index 48014dc00..3b18dd4c3 100644 --- a/rtsp/src/main/java/com/pedro/rtsp/rtcp/BaseSenderReport.kt +++ b/rtsp/src/main/java/com/pedro/rtsp/rtcp/BaseSenderReport.kt @@ -44,18 +44,22 @@ abstract class BaseSenderReport internal constructor() { @JvmStatic fun getInstance( protocol: Protocol, host: String, - videoSourcePort: Int, audioSourcePort: Int, - videoServerPort: Int, audioServerPort: Int, + videoSourcePort: Int?, audioSourcePort: Int?, + videoServerPort: Int?, audioServerPort: Int?, ): BaseSenderReport { return if (protocol === Protocol.TCP) { SenderReportTcp() } else { - val videoSocket = UdpStreamSocket( - host, videoServerPort, videoSourcePort, receiveSize = RtpConstants.REPORT_PACKET_LENGTH - ) - val audioSocket = UdpStreamSocket( - host, audioServerPort, audioSourcePort, receiveSize = RtpConstants.REPORT_PACKET_LENGTH - ) + val videoSocket = if (videoServerPort != null) { + UdpStreamSocket( + host, videoServerPort, videoSourcePort, receiveSize = RtpConstants.REPORT_PACKET_LENGTH + ) + } else null + val audioSocket = if (audioServerPort != null) { + UdpStreamSocket( + host, audioServerPort, audioSourcePort, receiveSize = RtpConstants.REPORT_PACKET_LENGTH + ) + } else null SenderReportUdp(videoSocket, audioSocket) } } diff --git a/rtsp/src/main/java/com/pedro/rtsp/rtcp/SenderReportUdp.kt b/rtsp/src/main/java/com/pedro/rtsp/rtcp/SenderReportUdp.kt index 4f9125449..ab5b98630 100644 --- a/rtsp/src/main/java/com/pedro/rtsp/rtcp/SenderReportUdp.kt +++ b/rtsp/src/main/java/com/pedro/rtsp/rtcp/SenderReportUdp.kt @@ -25,14 +25,14 @@ import java.io.IOException * Created by pedro on 8/11/18. */ class SenderReportUdp( - private val videoSocket: UdpStreamSocket, - private val audioSocket: UdpStreamSocket, + private val videoSocket: UdpStreamSocket?, + private val audioSocket: UdpStreamSocket?, ) : BaseSenderReport() { @Throws(IOException::class) override suspend fun setSocket(socket: TcpStreamSocket) { - videoSocket.connect() - audioSocket.connect() + videoSocket?.connect() + audioSocket?.connect() } @Throws(IOException::class) @@ -41,16 +41,16 @@ class SenderReportUdp( } override suspend fun close() { - videoSocket.close() - audioSocket.close() + videoSocket?.close() + audioSocket?.close() } @Throws(IOException::class) private suspend fun sendReportUDP(buffer: ByteArray, isVideo: Boolean) { if (isVideo) { - videoSocket.writePacket(buffer) + videoSocket?.writePacket(buffer) } else { - audioSocket.writePacket(buffer) + audioSocket?.writePacket(buffer) } } } \ No newline at end of file diff --git a/rtsp/src/main/java/com/pedro/rtsp/rtp/sockets/BaseRtpSocket.kt b/rtsp/src/main/java/com/pedro/rtsp/rtp/sockets/BaseRtpSocket.kt index 41698d702..e62a72805 100644 --- a/rtsp/src/main/java/com/pedro/rtsp/rtp/sockets/BaseRtpSocket.kt +++ b/rtsp/src/main/java/com/pedro/rtsp/rtp/sockets/BaseRtpSocket.kt @@ -32,18 +32,22 @@ abstract class BaseRtpSocket { @JvmStatic fun getInstance( protocol: Protocol, host: String, - videoSourcePort: Int, audioSourcePort: Int, - videoServerPort: Int, audioServerPort: Int, + videoSourcePort: Int?, audioSourcePort: Int?, + videoServerPort: Int?, audioServerPort: Int?, ): BaseRtpSocket { return if (protocol === Protocol.TCP) { RtpSocketTcp() } else { - val videoSocket = UdpStreamSocket( - host, videoServerPort, videoSourcePort, receiveSize = RtpConstants.MTU - ) - val audioSocket = UdpStreamSocket( - host, audioServerPort, audioSourcePort, receiveSize = RtpConstants.MTU - ) + val videoSocket = if (videoServerPort != null) { + UdpStreamSocket( + host, videoServerPort, videoSourcePort, receiveSize = RtpConstants.REPORT_PACKET_LENGTH + ) + } else null + val audioSocket = if (audioServerPort != null) { + UdpStreamSocket( + host, audioServerPort, audioSourcePort, receiveSize = RtpConstants.REPORT_PACKET_LENGTH + ) + } else null RtpSocketUdp(videoSocket, audioSocket) } } diff --git a/rtsp/src/main/java/com/pedro/rtsp/rtp/sockets/RtpSocketUdp.kt b/rtsp/src/main/java/com/pedro/rtsp/rtp/sockets/RtpSocketUdp.kt index 57381eda0..c2454eb3e 100644 --- a/rtsp/src/main/java/com/pedro/rtsp/rtp/sockets/RtpSocketUdp.kt +++ b/rtsp/src/main/java/com/pedro/rtsp/rtp/sockets/RtpSocketUdp.kt @@ -25,14 +25,14 @@ import java.io.IOException * Created by pedro on 7/11/18. */ class RtpSocketUdp( - private val videoSocket: UdpStreamSocket, - private val audioSocket: UdpStreamSocket, + private val videoSocket: UdpStreamSocket?, + private val audioSocket: UdpStreamSocket?, ) : BaseRtpSocket() { @Throws(IOException::class) override suspend fun setSocket(socket: TcpStreamSocket) { - videoSocket.connect() - audioSocket.connect() + videoSocket?.connect() + audioSocket?.connect() } @Throws(IOException::class) @@ -43,16 +43,16 @@ class RtpSocketUdp( override suspend fun flush() { } override suspend fun close() { - videoSocket.close() - audioSocket.close() + videoSocket?.close() + audioSocket?.close() } @Throws(IOException::class) private suspend fun sendFrameUDP(rtpFrame: RtpFrame) { if (rtpFrame.isVideoFrame()) { - videoSocket.writePacket(rtpFrame.buffer) + videoSocket?.writePacket(rtpFrame.buffer) } else { - audioSocket.writePacket(rtpFrame.buffer) + audioSocket?.writePacket(rtpFrame.buffer) } } } \ No newline at end of file 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 abbd30881..b25688fcf 100644 --- a/rtsp/src/main/java/com/pedro/rtsp/rtsp/RtspClient.kt +++ b/rtsp/src/main/java/com/pedro/rtsp/rtsp/RtspClient.kt @@ -326,12 +326,25 @@ class RtspClient(private val connectChecker: ConnectChecker) { } return@launch } + val videoClientPorts = if (!commandsManager.videoDisabled) { + commandsManager.videoClientPorts + } else arrayOf(null, null) + val videoServerPorts = if (!commandsManager.videoDisabled) { + commandsManager.videoServerPorts + } else arrayOf(null, null) + val audioClientPorts = if (!commandsManager.audioDisabled) { + commandsManager.audioClientPorts + } else arrayOf(null, null) + val audioServerPorts = if (!commandsManager.audioDisabled) { + commandsManager.audioServerPorts + } else arrayOf(null, null) + rtspSender.setSocketsInfo(commandsManager.protocol, host, - commandsManager.videoClientPorts, - commandsManager.audioClientPorts, - commandsManager.videoServerPorts, - commandsManager.audioServerPorts + videoClientPorts, + audioClientPorts, + videoServerPorts, + audioServerPorts ) rtspSender.setSocket(socket) rtspSender.start() diff --git a/rtsp/src/main/java/com/pedro/rtsp/rtsp/RtspSender.kt b/rtsp/src/main/java/com/pedro/rtsp/rtsp/RtspSender.kt index 38dc69750..f24d9cc80 100644 --- a/rtsp/src/main/java/com/pedro/rtsp/rtsp/RtspSender.kt +++ b/rtsp/src/main/java/com/pedro/rtsp/rtsp/RtspSender.kt @@ -54,8 +54,8 @@ class RtspSender( @Throws(IOException::class) fun setSocketsInfo( protocol: Protocol, host: String, - videoSourcePorts: IntArray, audioSourcePorts: IntArray, - videoServerPorts: IntArray, audioServerPorts: IntArray, + videoSourcePorts: Array, audioSourcePorts: Array, + videoServerPorts: Array, audioServerPorts: Array, ) { rtpSocket = BaseRtpSocket.getInstance(protocol, host, videoSourcePorts[0], audioSourcePorts[0], videoServerPorts[0], audioServerPorts[0]) baseSenderReport = BaseSenderReport.getInstance(protocol, host, videoSourcePorts[1], audioSourcePorts[1], videoServerPorts[1], audioServerPorts[1]) diff --git a/rtsp/src/main/java/com/pedro/rtsp/rtsp/commands/CommandParser.kt b/rtsp/src/main/java/com/pedro/rtsp/rtsp/commands/CommandParser.kt index 30759bea6..25a485b9f 100644 --- a/rtsp/src/main/java/com/pedro/rtsp/rtsp/commands/CommandParser.kt +++ b/rtsp/src/main/java/com/pedro/rtsp/rtsp/commands/CommandParser.kt @@ -29,8 +29,8 @@ class CommandParser { private const val TAG = "CommandParser" } - fun loadServerPorts(command: Command, protocol: Protocol, audioClientPorts: IntArray, - videoClientPorts: IntArray, audioServerPorts: IntArray, videoServerPorts: IntArray): Boolean { + fun loadServerPorts(command: Command, protocol: Protocol, audioClientPorts: Array, + videoClientPorts: Array, audioServerPorts: Array, videoServerPorts: Array): Boolean { var isAudio = true if (command.method == Method.SETUP && protocol == Protocol.UDP) { val clientPattern = Pattern.compile("client_port=([0-9]+)-([0-9]+)") diff --git a/rtsp/src/main/java/com/pedro/rtsp/rtsp/commands/CommandsManager.kt b/rtsp/src/main/java/com/pedro/rtsp/rtsp/commands/CommandsManager.kt index 9835eb119..4bf6e76f1 100644 --- a/rtsp/src/main/java/com/pedro/rtsp/rtsp/commands/CommandsManager.kt +++ b/rtsp/src/main/java/com/pedro/rtsp/rtsp/commands/CommandsManager.kt @@ -68,10 +68,10 @@ open class CommandsManager { var videoCodec = VideoCodec.H264 var audioCodec = AudioCodec.AAC //For udp - val audioClientPorts = intArrayOf(5000, 5001) - val videoClientPorts = intArrayOf(5002, 5003) - val audioServerPorts = intArrayOf(5004, 5005) - val videoServerPorts = intArrayOf(5006, 5007) + val audioClientPorts = arrayOf(5000, 5001) + val videoClientPorts = arrayOf(5002, 5003) + val audioServerPorts = arrayOf(5004, 5005) + val videoServerPorts = arrayOf(5006, 5007) val spsString: String get() = sps?.getData()?.encodeToString() ?: ""