Skip to content

Commit

Permalink
send mediaframe directly to packetizer
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroSG94 committed Oct 1, 2024
1 parent cdfeed3 commit da4736f
Show file tree
Hide file tree
Showing 32 changed files with 90 additions and 161 deletions.
3 changes: 1 addition & 2 deletions rtmp/src/main/java/com/pedro/rtmp/flv/BasePacket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
package com.pedro.rtmp.flv

import com.pedro.common.frame.MediaFrame
import java.nio.ByteBuffer

/**
* Created by pedro on 21/12/23.
*/
abstract class BasePacket {

abstract suspend fun createFlvPacket(byteBuffer: ByteBuffer, info: MediaFrame.Info, callback: suspend (FlvPacket) -> Unit)
abstract suspend fun createFlvPacket(mediaFrame: MediaFrame, callback: suspend (FlvPacket) -> Unit)
abstract fun reset(resetInfo: Boolean = true)
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import com.pedro.rtmp.flv.audio.AudioSize
import com.pedro.rtmp.flv.audio.AudioSoundRate
import com.pedro.rtmp.flv.audio.AudioSoundType
import com.pedro.rtmp.flv.audio.config.AudioSpecificConfig
import java.nio.ByteBuffer
import kotlin.experimental.or

/**
Expand Down Expand Up @@ -57,11 +56,10 @@ class AacPacket: BasePacket() {
}

override suspend fun createFlvPacket(
byteBuffer: ByteBuffer,
info: MediaFrame.Info,
mediaFrame: MediaFrame,
callback: suspend (FlvPacket) -> Unit
) {
val fixedBuffer = byteBuffer.removeInfo(info)
val fixedBuffer = mediaFrame.data.removeInfo(mediaFrame.info)
//header is 2 bytes length
//4 bits sound format, 2 bits sound rate, 1 bit sound size, 1 bit sound type
//8 bits sound data (always 10 because we are using aac)
Expand All @@ -87,7 +85,7 @@ class AacPacket: BasePacket() {
fixedBuffer.get(buffer, header.size, fixedBuffer.remaining())
}
System.arraycopy(header, 0, buffer, 0, header.size)
val ts = info.timestamp / 1000
val ts = mediaFrame.info.timestamp / 1000
callback(FlvPacket(buffer, ts, buffer.size, FlvType.AUDIO))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import com.pedro.rtmp.flv.audio.AudioFormat
import com.pedro.rtmp.flv.audio.AudioSize
import com.pedro.rtmp.flv.audio.AudioSoundRate
import com.pedro.rtmp.flv.audio.AudioSoundType
import java.nio.ByteBuffer
import kotlin.experimental.or

/**
Expand All @@ -42,11 +41,10 @@ class G711Packet: BasePacket() {
}

override suspend fun createFlvPacket(
byteBuffer: ByteBuffer,
info: MediaFrame.Info,
mediaFrame: MediaFrame,
callback: suspend (FlvPacket) -> Unit
) {
val fixedBuffer = byteBuffer.removeInfo(info)
val fixedBuffer = mediaFrame.data.removeInfo(mediaFrame.info)
//header is 1 byte length
//4 bits sound format, 2 bits sound rate, 1 bit sound size, 1 bit sound type
//sound rate should be ignored because G711 only support 8k so we are using 5_5k by default
Expand All @@ -55,7 +53,7 @@ class G711Packet: BasePacket() {
val buffer = ByteArray(fixedBuffer.remaining() + header.size)
fixedBuffer.get(buffer, header.size, fixedBuffer.remaining())
System.arraycopy(header, 0, buffer, 0, header.size)
val ts = info.timestamp / 1000
val ts = mediaFrame.info.timestamp / 1000
callback(FlvPacket(buffer, ts, buffer.size, FlvType.AUDIO))
}

Expand Down
10 changes: 4 additions & 6 deletions rtmp/src/main/java/com/pedro/rtmp/flv/video/packet/Av1Packet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import android.util.Log
import com.pedro.common.av1.Av1Parser
import com.pedro.common.av1.ObuType
import com.pedro.common.frame.MediaFrame
import com.pedro.common.isKeyframe
import com.pedro.common.removeInfo
import com.pedro.common.toByteArray
import com.pedro.rtmp.flv.BasePacket
Expand Down Expand Up @@ -51,12 +50,11 @@ class Av1Packet: BasePacket() {
}

override suspend fun createFlvPacket(
byteBuffer: ByteBuffer,
info: MediaFrame.Info,
mediaFrame: MediaFrame,
callback: suspend (FlvPacket) -> Unit
) {
var fixedBuffer = byteBuffer.duplicate().removeInfo(info)
val ts = info.timestamp / 1000
var fixedBuffer = mediaFrame.data.duplicate().removeInfo(mediaFrame.info)
val ts = mediaFrame.info.timestamp / 1000

//header is 8 bytes length:
//mark first byte as extended header (0b10000000)
Expand Down Expand Up @@ -96,7 +94,7 @@ class Av1Packet: BasePacket() {
val size = fixedBuffer.remaining()
buffer = ByteArray(header.size + size)

val nalType = if (info.isKeyFrame) VideoDataType.KEYFRAME.value else VideoDataType.INTER_FRAME.value
val nalType = if (mediaFrame.info.isKeyFrame) VideoDataType.KEYFRAME.value else VideoDataType.INTER_FRAME.value
header[0] = (0b10000000 or (nalType shl 4) or FourCCPacketType.CODED_FRAMES.value).toByte()
fixedBuffer.get(buffer, header.size, size)

Expand Down
10 changes: 4 additions & 6 deletions rtmp/src/main/java/com/pedro/rtmp/flv/video/packet/H264Packet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package com.pedro.rtmp.flv.video.packet

import android.util.Log
import com.pedro.common.frame.MediaFrame
import com.pedro.common.isKeyframe
import com.pedro.common.removeInfo
import com.pedro.rtmp.flv.BasePacket
import com.pedro.rtmp.flv.FlvPacket
Expand Down Expand Up @@ -65,12 +64,11 @@ class H264Packet: BasePacket() {
}

override suspend fun createFlvPacket(
byteBuffer: ByteBuffer,
info: MediaFrame.Info,
mediaFrame: MediaFrame,
callback: suspend (FlvPacket) -> Unit
) {
val fixedBuffer = byteBuffer.removeInfo(info)
val ts = info.timestamp / 1000
val fixedBuffer = mediaFrame.data.removeInfo(mediaFrame.info)
val ts = mediaFrame.info.timestamp / 1000
//header is 5 bytes length:
//4 bits FrameType, 4 bits CodecID
//1 byte AVCPacketType
Expand Down Expand Up @@ -109,7 +107,7 @@ class H264Packet: BasePacket() {

val type: Int = (validBuffer.get(0) and 0x1F).toInt()
var nalType = VideoDataType.INTER_FRAME.value
if (type == VideoNalType.IDR.value || info.isKeyFrame) {
if (type == VideoNalType.IDR.value || mediaFrame.info.isKeyFrame) {
nalType = VideoDataType.KEYFRAME.value
} else if (type == VideoNalType.SPS.value || type == VideoNalType.PPS.value) {
// we don't need send it because we already do it in video config
Expand Down
10 changes: 4 additions & 6 deletions rtmp/src/main/java/com/pedro/rtmp/flv/video/packet/H265Packet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package com.pedro.rtmp.flv.video.packet

import android.util.Log
import com.pedro.common.frame.MediaFrame
import com.pedro.common.isKeyframe
import com.pedro.common.removeInfo
import com.pedro.rtmp.flv.BasePacket
import com.pedro.rtmp.flv.FlvPacket
Expand Down Expand Up @@ -65,12 +64,11 @@ class H265Packet: BasePacket() {
}

override suspend fun createFlvPacket(
byteBuffer: ByteBuffer,
info: MediaFrame.Info,
mediaFrame: MediaFrame,
callback: suspend (FlvPacket) -> Unit
) {
val fixedBuffer = byteBuffer.removeInfo(info)
val ts = info.timestamp / 1000
val fixedBuffer = mediaFrame.data.removeInfo(mediaFrame.info)
val ts = mediaFrame.info.timestamp / 1000
//header is 8 bytes length:
//mark first byte as extended header (0b10000000)
//4 bits data type, 4 bits packet type
Expand Down Expand Up @@ -117,7 +115,7 @@ class H265Packet: BasePacket() {

val type: Int = validBuffer.get(0).toInt().shr(1 and 0x3f)
var nalType = VideoDataType.INTER_FRAME.value
if (type == VideoNalType.IDR_N_LP.value || type == VideoNalType.IDR_W_DLP.value || info.isKeyFrame) {
if (type == VideoNalType.IDR_N_LP.value || type == VideoNalType.IDR_W_DLP.value || mediaFrame.info.isKeyFrame) {
nalType = VideoDataType.KEYFRAME.value
} else if (type == VideoNalType.HEVC_VPS.value || type == VideoNalType.HEVC_SPS.value || type == VideoNalType.HEVC_PPS.value) {
// we don't need send it because we already do it in video config
Expand Down
12 changes: 2 additions & 10 deletions rtmp/src/main/java/com/pedro/rtmp/rtmp/RtmpSender.kt
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,8 @@ class RtmpSender(
private suspend fun getFlvPacket(mediaFrame: MediaFrame?, callback: suspend (FlvPacket) -> Unit) {
if (mediaFrame == null) return
when (mediaFrame.type) {
MediaFrame.Type.VIDEO -> {
videoPacket.createFlvPacket(mediaFrame.data, mediaFrame.info) {
callback(it)
}
}
MediaFrame.Type.AUDIO -> {
audioPacket.createFlvPacket(mediaFrame.data, mediaFrame.info) {
callback(it)
}
}
MediaFrame.Type.VIDEO -> videoPacket.createFlvPacket(mediaFrame) { callback(it) }
MediaFrame.Type.AUDIO -> audioPacket.createFlvPacket(mediaFrame) { callback(it) }
}
}
}
5 changes: 3 additions & 2 deletions rtmp/src/test/java/com/pedro/rtmp/flv/audio/AacPacketTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ class AacPacketTest {
val timestamp = 123456789L
val buffer = ByteArray(256) { 0x00 }
val info = MediaFrame.Info(0, buffer.size, timestamp, false)
val mediaFrame = MediaFrame(ByteBuffer.wrap(buffer), info, MediaFrame.Type.AUDIO)
val aacPacket = AacPacket()
aacPacket.sendAudioInfo(32000, true)
aacPacket.createFlvPacket(ByteBuffer.wrap(buffer), info) { flvPacket ->
aacPacket.createFlvPacket(mediaFrame) { flvPacket ->
assertEquals(FlvType.AUDIO, flvPacket.type)
assertEquals((-81).toByte(), flvPacket.buffer[0])
assertEquals(AacPacket.Type.SEQUENCE.mark, flvPacket.buffer[1])
}
aacPacket.createFlvPacket(ByteBuffer.wrap(buffer), info) { flvPacket ->
aacPacket.createFlvPacket(mediaFrame) { flvPacket ->
assertEquals(FlvType.AUDIO, flvPacket.type)
assertEquals((-81).toByte(), flvPacket.buffer[0])
assertEquals(AacPacket.Type.RAW.mark, flvPacket.buffer[1])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ class G711PacketTest {
val timestamp = 123456789L
val buffer = ByteArray(256) { 0x00 }
val info = MediaFrame.Info(0, buffer.size, timestamp, false)
val mediaFrame = MediaFrame(ByteBuffer.wrap(buffer), info, MediaFrame.Type.AUDIO)
val g711Packet = G711Packet()
g711Packet.sendAudioInfo()
g711Packet.createFlvPacket(ByteBuffer.wrap(buffer), info) { flvPacket ->
g711Packet.createFlvPacket(mediaFrame) { flvPacket ->
assertEquals(FlvType.AUDIO, flvPacket.type)
assertEquals(0x72, flvPacket.buffer[0])
assertEquals(buffer.size + 1, flvPacket.length)
Expand Down
4 changes: 2 additions & 2 deletions rtmp/src/test/java/com/pedro/rtmp/flv/video/Av1PacketTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ class Av1PacketTest {
val expectedFlvPacket = byteArrayOf(-111, 97, 118, 48, 49, 10, 13, 0, 0, 0, 36, 79, 126, 127, 0, 104, -125, 0, -125, 2)

val info = MediaFrame.Info(0, av1data.size, timestamp, true)

val mediaFrame = MediaFrame(ByteBuffer.wrap(av1data), info, MediaFrame.Type.VIDEO)
val frames = mutableListOf<FlvPacket>()
val av1Packet = Av1Packet()
av1Packet.sendVideoInfo(ByteBuffer.wrap(av1data))
av1Packet.createFlvPacket(ByteBuffer.wrap(av1data), info) { flvPacket ->
av1Packet.createFlvPacket(mediaFrame) { flvPacket ->
assertEquals(FlvType.VIDEO, flvPacket.type)
frames.add(flvPacket)
}
Expand Down
4 changes: 2 additions & 2 deletions rtmp/src/test/java/com/pedro/rtmp/flv/video/H264PacketTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ class H264PacketTest {
val h264Packet = H264Packet()
val sps = byteArrayOf(103, 100, 0, 30, -84, -76, 15, 2, -115, 53, 2, 2, 2, 7, -117, 23, 8)
val pps = byteArrayOf(104, -18, 13, -117)

val mediaFrame = MediaFrame(ByteBuffer.wrap(fakeH264), info, MediaFrame.Type.VIDEO)
h264Packet.sendVideoInfo(ByteBuffer.wrap(sps), ByteBuffer.wrap(pps))
val frames = mutableListOf<FlvPacket>()
h264Packet.createFlvPacket(ByteBuffer.wrap(fakeH264), info) { flvPacket ->
h264Packet.createFlvPacket(mediaFrame) { flvPacket ->
assertEquals(FlvType.VIDEO, flvPacket.type)
frames.add(flvPacket)
}
Expand Down
8 changes: 4 additions & 4 deletions rtmp/src/test/java/com/pedro/rtmp/flv/video/H265PacketTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,20 @@ class H265PacketTest {
fun `GIVEN a h265 buffer WHEN call create a h265 packet 1 time THEN return config and expected buffer`() = runTest {
val timestamp = 123456789L
val header = byteArrayOf(0x00, 0x00, 0x00, 0x01, 0x05)
val fakeH264 = header.plus(ByteArray(300) { 0x00 })
val fakeH265 = header.plus(ByteArray(300) { 0x00 })
val expectedConfig = byteArrayOf(-112, 104, 118, 99, 49, 1, 1, 96, 0, 0, 0, 0, 0, 0, 0, 0, 0, -103, -16, 0, -4, -3, -8, -8, 0, 0, 3, 3, -96, 0, 1, 0, 24, 64, 1, 12, 1, -1, -1, 1, 96, 0, 0, 3, 0, 0, 3, 0, 0, 3, 0, 0, 3, 0, -103, 44, 9, -95, 0, 1, 0, 35, 66, 1, 1, 1, 96, 0, 0, 3, 0, 0, 3, 0, 0, 3, 0, 0, 3, 0, -103, -96, 15, 8, 2, -127, 104, -76, -82, -55, 46, -26, -96, -64, -64, -64, 16, -94, 0, 1, 0, 8, 68, 1, -64, 102, 124, 12, -58, 64)
val expectedFlvPacket = byteArrayOf(-111, 104, 118, 99, 49, 0, 0, 0, 0, 0, 1, 45, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)

val info = MediaFrame.Info(0, fakeH264.size, timestamp, true)
val info = MediaFrame.Info(0, fakeH265.size, timestamp, true)
val h265Packet = H265Packet()
val vps = byteArrayOf(64, 1, 12, 1, -1, -1, 1, 96, 0, 0, 3, 0, 0, 3, 0, 0, 3, 0, 0, 3, 0, -103, 44, 9)
val sps = byteArrayOf(66, 1, 1, 1, 96, 0, 0, 3, 0, 0, 3, 0, 0, 3, 0, 0, 3, 0, -103, -96, 15, 8, 2, -127, 104, -76, -82, -55, 46, -26, -96, -64, -64, -64, 16)
val pps = byteArrayOf(68, 1, -64, 102, 124, 12, -58, 64)

h265Packet.sendVideoInfo(ByteBuffer.wrap(sps), ByteBuffer.wrap(pps), ByteBuffer.wrap(vps))

val mediaFrame = MediaFrame(ByteBuffer.wrap(fakeH265), info, MediaFrame.Type.VIDEO)
val frames = mutableListOf<FlvPacket>()
h265Packet.createFlvPacket(ByteBuffer.wrap(fakeH264), info) { flvPacket ->
h265Packet.createFlvPacket(mediaFrame) { flvPacket ->
assertEquals(FlvType.VIDEO, flvPacket.type)
frames.add(flvPacket)
}
Expand Down
8 changes: 3 additions & 5 deletions rtsp/src/main/java/com/pedro/rtsp/rtp/packets/AacPacket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import com.pedro.common.frame.MediaFrame
import com.pedro.common.removeInfo
import com.pedro.rtsp.rtsp.RtpFrame
import com.pedro.rtsp.utils.RtpConstants
import java.nio.ByteBuffer
import kotlin.experimental.and
import kotlin.experimental.or

Expand All @@ -43,14 +42,13 @@ class AacPacket: BasePacket(
}

override suspend fun createAndSendPacket(
byteBuffer: ByteBuffer,
bufferInfo: MediaFrame.Info,
mediaFrame: MediaFrame,
callback: suspend (List<RtpFrame>) -> Unit
) {
val fixedBuffer = byteBuffer.removeInfo(bufferInfo)
val fixedBuffer = mediaFrame.data.removeInfo(mediaFrame.info)
val length = fixedBuffer.remaining()
val maxPayload = maxPacketSize - (RtpConstants.RTP_HEADER_LENGTH + 4)
val ts = bufferInfo.timestamp * 1000
val ts = mediaFrame.info.timestamp * 1000
var sum = 0
val frames = mutableListOf<RtpFrame>()
while (sum < length) {
Expand Down
10 changes: 4 additions & 6 deletions rtsp/src/main/java/com/pedro/rtsp/rtp/packets/Av1Packet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package com.pedro.rtsp.rtp.packets
import com.pedro.common.av1.Av1Parser
import com.pedro.common.av1.ObuType
import com.pedro.common.frame.MediaFrame
import com.pedro.common.isKeyframe
import com.pedro.common.removeInfo
import com.pedro.common.toByteArray
import com.pedro.rtsp.rtsp.RtpFrame
Expand Down Expand Up @@ -50,18 +49,17 @@ class Av1Packet: BasePacket(
}

override suspend fun createAndSendPacket(
byteBuffer: ByteBuffer,
bufferInfo: MediaFrame.Info,
mediaFrame: MediaFrame,
callback: suspend (List<RtpFrame>) -> Unit
) {
var fixedBuffer = byteBuffer.removeInfo(bufferInfo)
var fixedBuffer = mediaFrame.data.removeInfo(mediaFrame.info)
//remove temporal delimitered OBU if found on start
if (parser.getObuType(fixedBuffer.get(0)) == ObuType.TEMPORAL_DELIMITER) {
fixedBuffer.position(2)
fixedBuffer = fixedBuffer.slice()
}
val obuList = parser.getObus(fixedBuffer.duplicate().toByteArray())
val ts = bufferInfo.timestamp * 1000L
val ts = mediaFrame.info.timestamp * 1000L
if (obuList.isEmpty()) return
var data = byteArrayOf()
obuList.forEachIndexed { index, obu ->
Expand Down Expand Up @@ -94,7 +92,7 @@ class Av1Packet: BasePacket(
markPacket(buffer) //mark end frame
}
val oSize = if (isFirstPacket) obuList.size else 1
buffer[RtpConstants.RTP_HEADER_LENGTH] = generateAv1AggregationHeader(bufferInfo.isKeyFrame, isFirstPacket, isLastPacket, oSize)
buffer[RtpConstants.RTP_HEADER_LENGTH] = generateAv1AggregationHeader(mediaFrame.info.isKeyFrame, isFirstPacket, isLastPacket, oSize)
updateSeq(buffer)
val rtpFrame = RtpFrame(buffer, rtpTs, buffer.size, channelIdentifier)
frames.add(rtpFrame)
Expand Down
4 changes: 1 addition & 3 deletions rtsp/src/main/java/com/pedro/rtsp/rtp/packets/BasePacket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import com.pedro.common.frame.MediaFrame
import com.pedro.rtsp.rtsp.RtpFrame
import com.pedro.rtsp.utils.RtpConstants
import com.pedro.rtsp.utils.setLong
import java.nio.ByteBuffer
import kotlin.experimental.and
import kotlin.experimental.or

Expand All @@ -36,8 +35,7 @@ abstract class BasePacket(private var clock: Long, private val payloadType: Int)
protected val TAG = "BasePacket"

abstract suspend fun createAndSendPacket(
byteBuffer: ByteBuffer,
bufferInfo: MediaFrame.Info,
mediaFrame: MediaFrame,
callback: suspend (List<RtpFrame>) -> Unit
)

Expand Down
10 changes: 4 additions & 6 deletions rtsp/src/main/java/com/pedro/rtsp/rtp/packets/G711Packet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package com.pedro.rtsp.rtp.packets
import com.pedro.common.frame.MediaFrame
import com.pedro.rtsp.rtsp.RtpFrame
import com.pedro.rtsp.utils.RtpConstants
import java.nio.ByteBuffer

/**
*
Expand All @@ -40,19 +39,18 @@ class G711Packet: BasePacket(
}

override suspend fun createAndSendPacket(
byteBuffer: ByteBuffer,
bufferInfo: MediaFrame.Info,
mediaFrame: MediaFrame,
callback: suspend (List<RtpFrame>) -> Unit
) {
val length = bufferInfo.size - byteBuffer.position()
val length = mediaFrame.info.size - mediaFrame.data.position()
val maxPayload = maxPacketSize - RtpConstants.RTP_HEADER_LENGTH
val ts = bufferInfo.timestamp * 1000
val ts = mediaFrame.info.timestamp * 1000
var sum = 0
val frames = mutableListOf<RtpFrame>()
while (sum < length) {
val size = if (length - sum < maxPayload) length - sum else maxPayload
val buffer = getBuffer(size + RtpConstants.RTP_HEADER_LENGTH)
byteBuffer.get(buffer, RtpConstants.RTP_HEADER_LENGTH, size)
mediaFrame.data.get(buffer, RtpConstants.RTP_HEADER_LENGTH, size)
markPacket(buffer)
val rtpTs = updateTimeStamp(buffer, ts)
updateSeq(buffer)
Expand Down
Loading

0 comments on commit da4736f

Please sign in to comment.