Skip to content

Commit

Permalink
fixing mpegpacketizer
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroSG94 committed Nov 8, 2023
1 parent 5327b3b commit 32b28c5
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
15 changes: 8 additions & 7 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,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
Expand Down Expand Up @@ -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
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
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
4 changes: 2 additions & 2 deletions srt/src/main/java/com/pedro/srt/srt/SrtSender.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 32b28c5

Please sign in to comment.