From 32b28c5bb103c7c186f1321ed138e00c01c85674 Mon Sep 17 00:00:00 2001 From: pedro Date: Wed, 8 Nov 2023 22:28:55 +0100 Subject: [PATCH] fixing mpegpacketizer --- .../com/pedro/srt/mpeg2ts/MpegTsPacketizer.kt | 15 ++++++++------- .../com/pedro/srt/mpeg2ts/packets/BasePacket.kt | 2 +- .../com/pedro/srt/mpeg2ts/packets/H26XPacket.kt | 2 +- srt/src/main/java/com/pedro/srt/srt/SrtSender.kt | 4 ++-- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/srt/src/main/java/com/pedro/srt/mpeg2ts/MpegTsPacketizer.kt b/srt/src/main/java/com/pedro/srt/mpeg2ts/MpegTsPacketizer.kt index 7d9d06ec8..e99d2f78a 100644 --- a/srt/src/main/java/com/pedro/srt/mpeg2ts/MpegTsPacketizer.kt +++ b/srt/src/main/java/com/pedro/srt/mpeg2ts/MpegTsPacketizer.kt @@ -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 @@ -26,7 +27,7 @@ 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 @@ -63,25 +64,25 @@ class MpegTsPacketizer { when (mpegTsPayload) { is Psi -> { writeHeader(buffer, true, mpegTsPayload.pid, AdaptationFieldControl.PAYLOAD, continuity) - val psi = mpegTsPayload - psi.write(buffer) + 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 diff --git a/srt/src/main/java/com/pedro/srt/mpeg2ts/packets/BasePacket.kt b/srt/src/main/java/com/pedro/srt/mpeg2ts/packets/BasePacket.kt index e55433bab..b8efcb05d 100644 --- a/srt/src/main/java/com/pedro/srt/mpeg2ts/packets/BasePacket.kt +++ b/srt/src/main/java/com/pedro/srt/mpeg2ts/packets/BasePacket.kt @@ -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( diff --git a/srt/src/main/java/com/pedro/srt/mpeg2ts/packets/H26XPacket.kt b/srt/src/main/java/com/pedro/srt/mpeg2ts/packets/H26XPacket.kt index fe47671e7..24ffea79e 100644 --- a/srt/src/main/java/com/pedro/srt/mpeg2ts/packets/H26XPacket.kt +++ b/srt/src/main/java/com/pedro/srt/mpeg2ts/packets/H26XPacket.kt @@ -77,7 +77,7 @@ class H26XPacket( return } } - + if (!isKeyFrame && !configSend) return //start with keyframe byteBuffer.rewind() val validBuffer = fixHeader(byteBuffer, isKeyFrame) val payload = ByteArray(validBuffer.remaining()) diff --git a/srt/src/main/java/com/pedro/srt/srt/SrtSender.kt b/srt/src/main/java/com/pedro/srt/srt/SrtSender.kt index 86d9812fd..23dab0f6a 100644 --- a/srt/src/main/java/com/pedro/srt/srt/SrtSender.kt +++ b/srt/src/main/java/com/pedro/srt/srt/SrtSender.kt @@ -59,12 +59,12 @@ class SrtSender( private val service = Mpeg2TsService() - private var psiManager = PsiManager(service).apply { + private val psiManager = PsiManager(service).apply { upgradePatVersion() upgradeSdtVersion() } - private val mpegTsPacketizer = MpegTsPacketizer() + private val mpegTsPacketizer = MpegTsPacketizer(psiManager) private val aacPacket = AacPacket(commandsManager.MTU - SrtPacket.headerSize, psiManager) private val h26XPacket = H26XPacket(commandsManager.MTU - SrtPacket.headerSize, psiManager)