Skip to content

Commit

Permalink
Merge pull request #1336 from pedroSG94/fix/srt-packets
Browse files Browse the repository at this point in the history
Fix/srt packets
  • Loading branch information
pedroSG94 authored Nov 13, 2023
2 parents 03f4405 + 2981a93 commit 85b3982
Show file tree
Hide file tree
Showing 26 changed files with 88 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ protected void startStreamRtp(String url) {
rtmpClient.setVideoResolution(videoEncoder.getWidth(), videoEncoder.getHeight());
}
rtmpClient.setFps(videoEncoder.getFps());
rtmpClient.setOnlyVideo(!audioInitialized);
rtmpClient.connect(url);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ protected void startStreamRtp(String url) {
rtmpClient.setVideoResolution(videoEncoder.getWidth(), videoEncoder.getHeight());
}
rtmpClient.setFps(videoEncoder.getFps());
rtmpClient.setOnlyVideo(!audioInitialized);
rtmpClient.connect(url);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public class RtmpOnlyAudio extends OnlyAudioBase {
public RtmpOnlyAudio(ConnectCheckerRtmp connectChecker) {
super();
rtmpClient = new RtmpClient(connectChecker);
rtmpClient.setOnlyAudio(true);
streamClient = new RtmpStreamClient(rtmpClient, null);
streamClient.setOnlyAudio(true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ protected void prepareAudioRtp(boolean isStereo, int sampleRate) {

@Override
protected void startStreamRtp(String url) {
rtspClient.setOnlyVideo(!audioInitialized);
rtspClient.connect(url);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ protected void prepareAudioRtp(boolean isStereo, int sampleRate) {

@Override
protected void startStreamRtp(String url) {
rtspClient.setOnlyVideo(!audioInitialized);
rtspClient.connect(url);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public class RtspOnlyAudio extends OnlyAudioBase {
public RtspOnlyAudio(ConnectCheckerRtsp connectCheckerRtsp) {
super();
rtspClient = new RtspClient(connectCheckerRtsp);
rtspClient.setOnlyAudio(true);
streamClient = new RtspStreamClient(rtspClient, null);
streamClient.setOnlyAudio(true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ protected void prepareAudioRtp(boolean isStereo, int sampleRate) {

@Override
protected void startStreamRtp(String url) {
srtClient.setOnlyVideo(!audioInitialized);
srtClient.connect(url);
requestKeyFrame();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ protected void prepareAudioRtp(boolean isStereo, int sampleRate) {

@Override
protected void startStreamRtp(String url) {
srtClient.setOnlyVideo(!audioInitialized);
srtClient.connect(url);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public class SrtOnlyAudio extends OnlyAudioBase {
public SrtOnlyAudio(ConnectCheckerSrt connectChecker) {
super();
srtClient = new SrtClient(connectChecker);
srtClient.setOnlyAudio(true);
streamClient = new SrtStreamClient(srtClient, null);
streamClient.setOnlyAudio(true);
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion srt/src/main/java/com/pedro/srt/mpeg2ts/MpegTsPacket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ import com.pedro.srt.srt.packets.data.PacketPosition
data class MpegTsPacket(
val buffer: ByteArray,
val type: MpegType,
val packetPosition: PacketPosition
val packetPosition: PacketPosition,
val isKey: Boolean
)
24 changes: 14 additions & 10 deletions srt/src/main/java/com/pedro/srt/mpeg2ts/MpegTsPacketizer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.pedro.srt.mpeg2ts

import com.pedro.srt.mpeg2ts.psi.Psi
import com.pedro.srt.mpeg2ts.psi.PsiManager
import com.pedro.srt.utils.TimeUtils
import com.pedro.srt.utils.toByteArray
import com.pedro.srt.utils.toInt
Expand All @@ -26,13 +27,14 @@ import java.nio.ByteBuffer
* Created by pedro on 28/8/23.
*
*/
class MpegTsPacketizer {
class MpegTsPacketizer(private val psiManager: PsiManager) {

companion object {
const val packetSize = 188
}

private var pesContinuity = 0
private var psiContinuity = 0

//4 bytes header
private fun writeHeader(buffer: ByteBuffer, startIndicator: Boolean, pid: Int, adaptationFieldControl: AdaptationFieldControl, continuity: Int) {
Expand All @@ -53,35 +55,36 @@ class MpegTsPacketizer {
/**
* return a list of mpeg2ts packets
*/
fun write(payload: List<MpegTsPayload>): List<ByteArray> {
fun write(payload: List<MpegTsPayload>, increasePsiContinuity: Boolean = false): List<ByteArray> {
val packets = mutableListOf<ByteArray>()
if (increasePsiContinuity) psiContinuity = (psiContinuity + 1) and 0xF

payload.forEachIndexed { index, mpegTsPayload ->
var buffer = ByteBuffer.allocate(packetSize)
var isFirstPacket = index == 0
val continuity = 0 and 0xF

when (mpegTsPayload) {
is Psi -> {
writeHeader(buffer, true, mpegTsPayload.pid, AdaptationFieldControl.PAYLOAD, continuity)
val psi = mpegTsPayload
psi.write(buffer)
writeHeader(buffer, true, mpegTsPayload.pid, AdaptationFieldControl.PAYLOAD, psiContinuity)
mpegTsPayload.write(buffer)
val stuffingSize = buffer.remaining()
writeStuffingBytes(buffer, stuffingSize, false)
packets.add(buffer.toByteArray())
}
is Pes -> {
val pes = mpegTsPayload
var adaptationFieldControl = AdaptationFieldControl.ADAPTATION_PAYLOAD
writeHeader(buffer, true, mpegTsPayload.pid, adaptationFieldControl, pesContinuity)
val isAudio = psiManager.getAudioPid().toInt() == mpegTsPayload.pid
val pcr = if (isAudio) null else TimeUtils.getCurrentTimeMicro()
val adaptationField = AdaptationField(
discontinuityIndicator = false,
randomAccessIndicator = mpegTsPayload.isKeyFrame, //only video can be true
pcr = TimeUtils.getCurrentTimeMicro()
pcr = pcr
)
buffer.put(adaptationField.getData())
pes.writeHeader(buffer)
mpegTsPayload.writeHeader(buffer)

val data = pes.bufferData
val data = mpegTsPayload.bufferData
while (data.hasRemaining()) {
if (isFirstPacket) {
isFirstPacket = false
Expand Down Expand Up @@ -128,5 +131,6 @@ class MpegTsPacketizer {

fun reset() {
pesContinuity = 0
psiContinuity = 0
}
}
12 changes: 2 additions & 10 deletions srt/src/main/java/com/pedro/srt/mpeg2ts/packets/AacPacket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,8 @@ class AacPacket(
chunks.forEach {
buffer.put(it)
}
val packetPosition = if (index == 0 && chunked.size == 1) {
PacketPosition.SINGLE
} else if (index == 0) {
PacketPosition.FIRST
} else if (index == chunked.size - 1) {
PacketPosition.LAST
} else {
PacketPosition.MIDDLE
}
packets.add(MpegTsPacket(buffer.array(), MpegType.AUDIO, packetPosition))
val packetPosition = PacketPosition.SINGLE
packets.add(MpegTsPacket(buffer.array(), MpegType.AUDIO, packetPosition, false))
}
callback(packets)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ abstract class BasePacket(
private var limitSize: Int,
) {

protected val mpegTsPacketizer = MpegTsPacketizer()
protected val mpegTsPacketizer = MpegTsPacketizer(psiManager)
protected var chunkSize = limitSize / MpegTsPacketizer.packetSize //max number of ts packets per srtpacket

abstract fun createAndSendPacket(
Expand Down
28 changes: 14 additions & 14 deletions srt/src/main/java/com/pedro/srt/mpeg2ts/packets/H26XPacket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ class H26XPacket(
return
}
}

byteBuffer.rewind()
val validBuffer = fixHeader(byteBuffer, isKeyFrame)
val payload = ByteArray(validBuffer.remaining())
Expand All @@ -93,16 +92,8 @@ class H26XPacket(
chunks.forEach {
buffer.put(it)
}
val packetPosition = if (index == 0 && chunked.size == 1) {
PacketPosition.SINGLE
} else if (index == 0) {
PacketPosition.FIRST
} else if (index == chunked.size - 1) {
PacketPosition.LAST
} else {
PacketPosition.MIDDLE
}
packets.add(MpegTsPacket(buffer.array(), MpegType.VIDEO, packetPosition))
val packetPosition = PacketPosition.SINGLE
packets.add(MpegTsPacket(buffer.array(), MpegType.VIDEO, packetPosition, isKeyFrame))
}
callback(packets)
}
Expand Down Expand Up @@ -140,13 +131,22 @@ class H26XPacket(
bufferWithPrefix.put(noHeaderBuffer)
noHeaderBuffer = bufferWithPrefix
}
return if (isKeyFrame && !configSend) { //add video info to first keyframe
return if (isKeyFrame) { //add video info to first keyframe
val vps = this.vps ?: byteArrayOf()
val sps = this.sps ?: byteArrayOf()
val pps = this.pps ?: byteArrayOf()
val keyExtraSize = vps.size + sps.size + pps.size
val validBuffer = ByteBuffer.allocate(noHeaderBuffer.remaining() + keyExtraSize)
val audSize = if (codec == Codec.AVC) 6 else 7
val videoHeader = vps.plus(sps).plus(pps)
val validBuffer = ByteBuffer.allocate(audSize + videoHeader.size + noHeaderBuffer.remaining())
validBuffer.putInt(0x00000001)
if (codec == Codec.AVC) {
validBuffer.put(0x09.toByte())
validBuffer.put(0xf0.toByte())
} else {
validBuffer.put(0x46.toByte())
validBuffer.put(0x01.toByte())
validBuffer.put(0x50.toByte())
}
validBuffer.put(videoHeader)
validBuffer.put(noHeaderBuffer.toByteArray())
validBuffer.rewind()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ data class Mpeg2TsService(
tracks.add(Track(codec, pid))
if (pcrPid == null) pcrPid = pid
else if (codec != Codec.AAC) pcrPid = pid
}

fun generatePmt() {
if (pmt == null) {
pmt = Pmt(
Pid.generatePID().toInt(),
Expand Down
16 changes: 3 additions & 13 deletions srt/src/main/java/com/pedro/srt/srt/CommandsManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class CommandsManager {
var startTS = 0L //microSeconds
var audioDisabled = false
var videoDisabled = false
var host = ""
//Avoid write a packet in middle of other.
private val writeSync = Mutex(locked = false)

Expand All @@ -63,7 +64,7 @@ class CommandsManager {
suspend fun writeHandshake(socket: SrtSocket?, handshake: Handshake = Handshake()) {
writeSync.withLock {
handshake.initialPacketSequence = sequenceNumber
handshake.ipAddress = getIPAddress()
handshake.ipAddress = host
handshake.write(getTs(), 0)
Log.i(TAG, handshake.toString())
socket?.write(handshake)
Expand Down Expand Up @@ -144,25 +145,14 @@ class CommandsManager {
MTU = Constants.MTU
socketId = 0
startTS = 0L
host = ""
packetHandlingQueue.clear()
}

private fun generateInitialSequence(): Int {
return Random.nextInt(0, Int.MAX_VALUE)
}

private fun getIPAddress(): String {
val interfaces: List<NetworkInterface> = NetworkInterface.getNetworkInterfaces().toList()
val vpnInterfaces = interfaces.filter { it.displayName.contains("tun") }
val address: String by lazy { interfaces.findAddress().firstOrNull() ?: "0.0.0.0" }
return if (vpnInterfaces.isNotEmpty()) {
val vpnAddresses = vpnInterfaces.findAddress()
vpnAddresses.firstOrNull() ?: address
} else {
address
}
}

private fun List<NetworkInterface>.findAddress(): List<String?> = this.asSequence()
.map { addresses -> addresses.inetAddresses.asSequence() }
.flatten()
Expand Down
1 change: 1 addition & 0 deletions srt/src/main/java/com/pedro/srt/srt/SrtClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ class SrtClient(private val connectCheckerSrt: ConnectCheckerSrt) {
val streamName =
if (srtMatcher.group(4).isNullOrEmpty()) "" else "/" + srtMatcher.group(4)
val path = "${srtMatcher.group(3)}$streamName".trim()
commandsManager.host = host

val error = runCatching {
socket = SrtSocket(host, port)
Expand Down
Loading

0 comments on commit 85b3982

Please sign in to comment.