Skip to content

Commit

Permalink
fix RTSP only video/audio
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroSG94 committed Oct 28, 2024
1 parent eca5994 commit a8a7541
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 44 deletions.
20 changes: 12 additions & 8 deletions rtsp/src/main/java/com/pedro/rtsp/rtcp/BaseSenderReport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
16 changes: 8 additions & 8 deletions rtsp/src/main/java/com/pedro/rtsp/rtcp/SenderReportUdp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
}
}
20 changes: 12 additions & 8 deletions rtsp/src/main/java/com/pedro/rtsp/rtp/sockets/BaseRtpSocket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
16 changes: 8 additions & 8 deletions rtsp/src/main/java/com/pedro/rtsp/rtp/sockets/RtpSocketUdp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
}
}
21 changes: 17 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 @@ -326,12 +326,25 @@ class RtspClient(private val connectChecker: ConnectChecker) {
}
return@launch
}
val videoClientPorts = if (!commandsManager.videoDisabled) {
commandsManager.videoClientPorts
} else arrayOf<Int?>(null, null)
val videoServerPorts = if (!commandsManager.videoDisabled) {
commandsManager.videoServerPorts
} else arrayOf<Int?>(null, null)
val audioClientPorts = if (!commandsManager.audioDisabled) {
commandsManager.audioClientPorts
} else arrayOf<Int?>(null, null)
val audioServerPorts = if (!commandsManager.audioDisabled) {
commandsManager.audioServerPorts
} else arrayOf<Int?>(null, null)

rtspSender.setSocketsInfo(commandsManager.protocol,
host,
commandsManager.videoClientPorts,
commandsManager.audioClientPorts,
commandsManager.videoServerPorts,
commandsManager.audioServerPorts
videoClientPorts,
audioClientPorts,
videoServerPorts,
audioServerPorts
)
rtspSender.setSocket(socket)
rtspSender.start()
Expand Down
4 changes: 2 additions & 2 deletions rtsp/src/main/java/com/pedro/rtsp/rtsp/RtspSender.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int?>, audioSourcePorts: Array<Int?>,
videoServerPorts: Array<Int?>, audioServerPorts: Array<Int?>,
) {
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])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int?>,
videoClientPorts: Array<Int?>, audioServerPorts: Array<Int?>, videoServerPorts: Array<Int?>): Boolean {
var isAudio = true
if (command.method == Method.SETUP && protocol == Protocol.UDP) {
val clientPattern = Pattern.compile("client_port=([0-9]+)-([0-9]+)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int?>(5000, 5001)
val videoClientPorts = arrayOf<Int?>(5002, 5003)
val audioServerPorts = arrayOf<Int?>(5004, 5005)
val videoServerPorts = arrayOf<Int?>(5006, 5007)

val spsString: String
get() = sps?.getData()?.encodeToString() ?: ""
Expand Down

0 comments on commit a8a7541

Please sign in to comment.