diff --git a/rtmp/src/main/java/com/pedro/rtmp/flv/BasePacket.kt b/rtmp/src/main/java/com/pedro/rtmp/flv/BasePacket.kt index c50412a68..84d3e5c1f 100644 --- a/rtmp/src/main/java/com/pedro/rtmp/flv/BasePacket.kt +++ b/rtmp/src/main/java/com/pedro/rtmp/flv/BasePacket.kt @@ -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) } \ No newline at end of file diff --git a/rtmp/src/main/java/com/pedro/rtmp/flv/audio/packet/AacPacket.kt b/rtmp/src/main/java/com/pedro/rtmp/flv/audio/packet/AacPacket.kt index 320d17939..74c774a0d 100644 --- a/rtmp/src/main/java/com/pedro/rtmp/flv/audio/packet/AacPacket.kt +++ b/rtmp/src/main/java/com/pedro/rtmp/flv/audio/packet/AacPacket.kt @@ -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 /** @@ -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) @@ -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)) } diff --git a/rtmp/src/main/java/com/pedro/rtmp/flv/audio/packet/G711Packet.kt b/rtmp/src/main/java/com/pedro/rtmp/flv/audio/packet/G711Packet.kt index 88dd82d8c..0141410e2 100644 --- a/rtmp/src/main/java/com/pedro/rtmp/flv/audio/packet/G711Packet.kt +++ b/rtmp/src/main/java/com/pedro/rtmp/flv/audio/packet/G711Packet.kt @@ -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 /** @@ -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 @@ -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)) } diff --git a/rtmp/src/main/java/com/pedro/rtmp/flv/video/packet/Av1Packet.kt b/rtmp/src/main/java/com/pedro/rtmp/flv/video/packet/Av1Packet.kt index 1c122ef81..28df900d4 100644 --- a/rtmp/src/main/java/com/pedro/rtmp/flv/video/packet/Av1Packet.kt +++ b/rtmp/src/main/java/com/pedro/rtmp/flv/video/packet/Av1Packet.kt @@ -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 @@ -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) @@ -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) diff --git a/rtmp/src/main/java/com/pedro/rtmp/flv/video/packet/H264Packet.kt b/rtmp/src/main/java/com/pedro/rtmp/flv/video/packet/H264Packet.kt index ac6359bd3..eee383d77 100644 --- a/rtmp/src/main/java/com/pedro/rtmp/flv/video/packet/H264Packet.kt +++ b/rtmp/src/main/java/com/pedro/rtmp/flv/video/packet/H264Packet.kt @@ -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 @@ -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 @@ -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 diff --git a/rtmp/src/main/java/com/pedro/rtmp/flv/video/packet/H265Packet.kt b/rtmp/src/main/java/com/pedro/rtmp/flv/video/packet/H265Packet.kt index 61c440842..d2db95a40 100644 --- a/rtmp/src/main/java/com/pedro/rtmp/flv/video/packet/H265Packet.kt +++ b/rtmp/src/main/java/com/pedro/rtmp/flv/video/packet/H265Packet.kt @@ -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 @@ -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 @@ -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 diff --git a/rtmp/src/main/java/com/pedro/rtmp/rtmp/RtmpSender.kt b/rtmp/src/main/java/com/pedro/rtmp/rtmp/RtmpSender.kt index 044d0967b..6565dae75 100644 --- a/rtmp/src/main/java/com/pedro/rtmp/rtmp/RtmpSender.kt +++ b/rtmp/src/main/java/com/pedro/rtmp/rtmp/RtmpSender.kt @@ -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) } } } } \ No newline at end of file diff --git a/rtmp/src/test/java/com/pedro/rtmp/flv/audio/AacPacketTest.kt b/rtmp/src/test/java/com/pedro/rtmp/flv/audio/AacPacketTest.kt index 8c40ba093..d6ac2ce61 100644 --- a/rtmp/src/test/java/com/pedro/rtmp/flv/audio/AacPacketTest.kt +++ b/rtmp/src/test/java/com/pedro/rtmp/flv/audio/AacPacketTest.kt @@ -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]) diff --git a/rtmp/src/test/java/com/pedro/rtmp/flv/audio/G711PacketTest.kt b/rtmp/src/test/java/com/pedro/rtmp/flv/audio/G711PacketTest.kt index 6b2ad6656..b0a501169 100644 --- a/rtmp/src/test/java/com/pedro/rtmp/flv/audio/G711PacketTest.kt +++ b/rtmp/src/test/java/com/pedro/rtmp/flv/audio/G711PacketTest.kt @@ -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) diff --git a/rtmp/src/test/java/com/pedro/rtmp/flv/video/Av1PacketTest.kt b/rtmp/src/test/java/com/pedro/rtmp/flv/video/Av1PacketTest.kt index ef8cce402..222782deb 100644 --- a/rtmp/src/test/java/com/pedro/rtmp/flv/video/Av1PacketTest.kt +++ b/rtmp/src/test/java/com/pedro/rtmp/flv/video/Av1PacketTest.kt @@ -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() 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) } diff --git a/rtmp/src/test/java/com/pedro/rtmp/flv/video/H264PacketTest.kt b/rtmp/src/test/java/com/pedro/rtmp/flv/video/H264PacketTest.kt index 1a9bbb4e1..6c2466c94 100644 --- a/rtmp/src/test/java/com/pedro/rtmp/flv/video/H264PacketTest.kt +++ b/rtmp/src/test/java/com/pedro/rtmp/flv/video/H264PacketTest.kt @@ -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() - h264Packet.createFlvPacket(ByteBuffer.wrap(fakeH264), info) { flvPacket -> + h264Packet.createFlvPacket(mediaFrame) { flvPacket -> assertEquals(FlvType.VIDEO, flvPacket.type) frames.add(flvPacket) } diff --git a/rtmp/src/test/java/com/pedro/rtmp/flv/video/H265PacketTest.kt b/rtmp/src/test/java/com/pedro/rtmp/flv/video/H265PacketTest.kt index 7c4b9a44a..46ded584c 100644 --- a/rtmp/src/test/java/com/pedro/rtmp/flv/video/H265PacketTest.kt +++ b/rtmp/src/test/java/com/pedro/rtmp/flv/video/H265PacketTest.kt @@ -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() - h265Packet.createFlvPacket(ByteBuffer.wrap(fakeH264), info) { flvPacket -> + h265Packet.createFlvPacket(mediaFrame) { flvPacket -> assertEquals(FlvType.VIDEO, flvPacket.type) frames.add(flvPacket) } diff --git a/rtsp/src/main/java/com/pedro/rtsp/rtp/packets/AacPacket.kt b/rtsp/src/main/java/com/pedro/rtsp/rtp/packets/AacPacket.kt index caa2170b9..1529b5d06 100644 --- a/rtsp/src/main/java/com/pedro/rtsp/rtp/packets/AacPacket.kt +++ b/rtsp/src/main/java/com/pedro/rtsp/rtp/packets/AacPacket.kt @@ -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 @@ -43,14 +42,13 @@ class AacPacket: BasePacket( } override suspend fun createAndSendPacket( - byteBuffer: ByteBuffer, - bufferInfo: MediaFrame.Info, + mediaFrame: MediaFrame, callback: suspend (List) -> 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() while (sum < length) { diff --git a/rtsp/src/main/java/com/pedro/rtsp/rtp/packets/Av1Packet.kt b/rtsp/src/main/java/com/pedro/rtsp/rtp/packets/Av1Packet.kt index 977b9a78c..9a5e42c34 100644 --- a/rtsp/src/main/java/com/pedro/rtsp/rtp/packets/Av1Packet.kt +++ b/rtsp/src/main/java/com/pedro/rtsp/rtp/packets/Av1Packet.kt @@ -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 @@ -50,18 +49,17 @@ class Av1Packet: BasePacket( } override suspend fun createAndSendPacket( - byteBuffer: ByteBuffer, - bufferInfo: MediaFrame.Info, + mediaFrame: MediaFrame, callback: suspend (List) -> 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 -> @@ -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) diff --git a/rtsp/src/main/java/com/pedro/rtsp/rtp/packets/BasePacket.kt b/rtsp/src/main/java/com/pedro/rtsp/rtp/packets/BasePacket.kt index 1369612d4..eea19803c 100644 --- a/rtsp/src/main/java/com/pedro/rtsp/rtp/packets/BasePacket.kt +++ b/rtsp/src/main/java/com/pedro/rtsp/rtp/packets/BasePacket.kt @@ -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 @@ -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) -> Unit ) diff --git a/rtsp/src/main/java/com/pedro/rtsp/rtp/packets/G711Packet.kt b/rtsp/src/main/java/com/pedro/rtsp/rtp/packets/G711Packet.kt index e58980eab..7bf6d3e4a 100644 --- a/rtsp/src/main/java/com/pedro/rtsp/rtp/packets/G711Packet.kt +++ b/rtsp/src/main/java/com/pedro/rtsp/rtp/packets/G711Packet.kt @@ -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 /** * @@ -40,19 +39,18 @@ class G711Packet: BasePacket( } override suspend fun createAndSendPacket( - byteBuffer: ByteBuffer, - bufferInfo: MediaFrame.Info, + mediaFrame: MediaFrame, callback: suspend (List) -> 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() 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) diff --git a/rtsp/src/main/java/com/pedro/rtsp/rtp/packets/H264Packet.kt b/rtsp/src/main/java/com/pedro/rtsp/rtp/packets/H264Packet.kt index 65e71059a..b88fca591 100644 --- a/rtsp/src/main/java/com/pedro/rtsp/rtp/packets/H264Packet.kt +++ b/rtsp/src/main/java/com/pedro/rtsp/rtp/packets/H264Packet.kt @@ -18,7 +18,6 @@ package com.pedro.rtsp.rtp.packets import android.util.Log 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 @@ -50,22 +49,21 @@ class H264Packet: BasePacket(RtpConstants.clockVideoFrequency, } override suspend fun createAndSendPacket( - byteBuffer: ByteBuffer, - bufferInfo: MediaFrame.Info, + mediaFrame: MediaFrame, callback: suspend (List) -> Unit ) { - val fixedBuffer = byteBuffer.removeInfo(bufferInfo) + val fixedBuffer = mediaFrame.data.removeInfo(mediaFrame.info) // We read a NAL units from ByteBuffer and we send them // NAL units are preceded with 0x00000001 val header = ByteArray(getHeaderSize(fixedBuffer) + 1) if (header.size == 1) return //invalid buffer or waiting for sps/pps fixedBuffer.rewind() fixedBuffer.get(header, 0, header.size) - val ts = bufferInfo.timestamp * 1000L + val ts = mediaFrame.info.timestamp * 1000L val naluLength = fixedBuffer.remaining() val type: Int = (header[header.size - 1] and 0x1F).toInt() val frames = mutableListOf() - if (type == RtpConstants.IDR || bufferInfo.isKeyFrame) { + if (type == RtpConstants.IDR || mediaFrame.info.isKeyFrame) { stapA?.let { val buffer = getBuffer(it.size + RtpConstants.RTP_HEADER_LENGTH) val rtpTs = updateTimeStamp(buffer, ts) diff --git a/rtsp/src/main/java/com/pedro/rtsp/rtp/packets/H265Packet.kt b/rtsp/src/main/java/com/pedro/rtsp/rtp/packets/H265Packet.kt index 360c9fcc9..2e2894dab 100644 --- a/rtsp/src/main/java/com/pedro/rtsp/rtp/packets/H265Packet.kt +++ b/rtsp/src/main/java/com/pedro/rtsp/rtp/packets/H265Packet.kt @@ -21,7 +21,6 @@ import com.pedro.common.removeInfo import com.pedro.rtsp.rtsp.RtpFrame import com.pedro.rtsp.utils.RtpConstants import com.pedro.rtsp.utils.getVideoStartCodeSize -import java.nio.ByteBuffer import kotlin.experimental.and /** @@ -39,17 +38,16 @@ class H265Packet: BasePacket( } override suspend fun createAndSendPacket( - byteBuffer: ByteBuffer, - bufferInfo: MediaFrame.Info, + mediaFrame: MediaFrame, callback: suspend (List) -> Unit ) { - val fixedBuffer = byteBuffer.removeInfo(bufferInfo) + val fixedBuffer = mediaFrame.data.removeInfo(mediaFrame.info) // We read a NAL units from ByteBuffer and we send them // NAL units are preceded with 0x00000001 val header = ByteArray(fixedBuffer.getVideoStartCodeSize() + 2) if (header.size == 2) return //invalid buffer or waiting for sps/pps/vps fixedBuffer.get(header, 0, header.size) - val ts = bufferInfo.timestamp * 1000L + val ts = mediaFrame.info.timestamp * 1000L val naluLength = fixedBuffer.remaining() val type: Int = header[header.size - 2].toInt().shr(1 and 0x3f) val frames = mutableListOf() diff --git a/rtsp/src/main/java/com/pedro/rtsp/rtp/packets/OpusPacket.kt b/rtsp/src/main/java/com/pedro/rtsp/rtp/packets/OpusPacket.kt index 0ffe79ab2..063ee85b6 100644 --- a/rtsp/src/main/java/com/pedro/rtsp/rtp/packets/OpusPacket.kt +++ b/rtsp/src/main/java/com/pedro/rtsp/rtp/packets/OpusPacket.kt @@ -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 /** * Created by pedro on 8/2/24. @@ -40,19 +39,18 @@ class OpusPacket: BasePacket( } override suspend fun createAndSendPacket( - byteBuffer: ByteBuffer, - bufferInfo: MediaFrame.Info, + mediaFrame: MediaFrame, callback: suspend (List) -> 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() 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) diff --git a/rtsp/src/main/java/com/pedro/rtsp/rtsp/RtspSender.kt b/rtsp/src/main/java/com/pedro/rtsp/rtsp/RtspSender.kt index 5265be4ad..38dc69750 100644 --- a/rtsp/src/main/java/com/pedro/rtsp/rtsp/RtspSender.kt +++ b/rtsp/src/main/java/com/pedro/rtsp/rtsp/RtspSender.kt @@ -150,16 +150,8 @@ class RtspSender( private suspend fun getRtpPackets(mediaFrame: MediaFrame?, callback: suspend (List) -> Unit) { if (mediaFrame == null) return when (mediaFrame.type) { - MediaFrame.Type.VIDEO -> { - videoPacket.createAndSendPacket(mediaFrame.data, mediaFrame.info) { packets -> - callback(packets) - } - } - MediaFrame.Type.AUDIO -> { - audioPacket.createAndSendPacket(mediaFrame.data, mediaFrame.info) { packets -> - callback(packets) - } - } + MediaFrame.Type.VIDEO -> videoPacket.createAndSendPacket(mediaFrame) { callback(it) } + MediaFrame.Type.AUDIO -> audioPacket.createAndSendPacket(mediaFrame) { callback(it) } } } } \ No newline at end of file diff --git a/rtsp/src/test/java/com/pedro/rtsp/rtp/AacPacketTest.kt b/rtsp/src/test/java/com/pedro/rtsp/rtp/AacPacketTest.kt index ebff9d0c6..7a57c17c7 100644 --- a/rtsp/src/test/java/com/pedro/rtsp/rtp/AacPacketTest.kt +++ b/rtsp/src/test/java/com/pedro/rtsp/rtp/AacPacketTest.kt @@ -36,12 +36,11 @@ class AacPacketTest { val fakeAac = ByteArray(300) { 0x00 } val info = MediaFrame.Info(0, fakeAac.size, timestamp, false) + val mediaFrame = MediaFrame(ByteBuffer.wrap(fakeAac), info, MediaFrame.Type.AUDIO) val aacPacket = AacPacket().apply { setAudioInfo(44100) } aacPacket.setSSRC(123456789) val frames = mutableListOf() - aacPacket.createAndSendPacket(ByteBuffer.wrap(fakeAac), info) { - frames.addAll(it) - } + aacPacket.createAndSendPacket(mediaFrame) { frames.addAll(it) } val expectedRtp = byteArrayOf(-128, -31, 0, 1, 0, 83, 19, 92, 7, 91, -51, 21, 0, 16, 9, 96).plus(fakeAac) val expectedTimeStamp = 5444444L diff --git a/rtsp/src/test/java/com/pedro/rtsp/rtp/Av1PacketTest.kt b/rtsp/src/test/java/com/pedro/rtsp/rtp/Av1PacketTest.kt index 92737f1f4..caed8b5d4 100644 --- a/rtsp/src/test/java/com/pedro/rtsp/rtp/Av1PacketTest.kt +++ b/rtsp/src/test/java/com/pedro/rtsp/rtp/Av1PacketTest.kt @@ -36,13 +36,11 @@ class Av1PacketTest { val av1data = byteArrayOf(0x0a, 0x0d, 0x00, 0x00, 0x00, 0x24, 0x4f, 0x7e, 0x7f, 0x00, 0x68, 0x83.toByte(), 0x00, 0x83.toByte(), 0x02) val info = MediaFrame.Info(0, av1data.size, timestamp, true) - + val mediaFrame = MediaFrame(ByteBuffer.wrap(av1data), info, MediaFrame.Type.VIDEO) val frames = mutableListOf() val av1Packet = Av1Packet() av1Packet.setSSRC(123456789) - av1Packet.createAndSendPacket(ByteBuffer.wrap(av1data), info) { rtpFrames -> - frames.addAll(rtpFrames) - } + av1Packet.createAndSendPacket(mediaFrame) { frames.addAll(it) } val expectedRtp = byteArrayOf(-128, -32, 0, 1, 0, -87, -118, -57, 7, 91, -51, 21, 24).plus(av1data) val expectedTimeStamp = 11111111L diff --git a/rtsp/src/test/java/com/pedro/rtsp/rtp/G711PacketTest.kt b/rtsp/src/test/java/com/pedro/rtsp/rtp/G711PacketTest.kt index 993f5038c..586d0094e 100644 --- a/rtsp/src/test/java/com/pedro/rtsp/rtp/G711PacketTest.kt +++ b/rtsp/src/test/java/com/pedro/rtsp/rtp/G711PacketTest.kt @@ -36,12 +36,11 @@ class G711PacketTest { val fakeG711 = ByteArray(30) { 0x05 } val info = MediaFrame.Info(0, fakeG711.size, timestamp, false) + val mediaFrame = MediaFrame(ByteBuffer.wrap(fakeG711), info, MediaFrame.Type.AUDIO) val g711Packet = G711Packet().apply { setAudioInfo(8000) } g711Packet.setSSRC(123456789) val frames = mutableListOf() - g711Packet.createAndSendPacket(ByteBuffer.wrap(fakeG711), info) { - frames.addAll(it) - } + g711Packet.createAndSendPacket(mediaFrame) { frames.addAll(it) } val expectedRtp = byteArrayOf(-128, -120, 0, 1, 0, 15, 18, 6, 7, 91, -51, 21).plus(fakeG711) val expectedTimeStamp = 987654L diff --git a/rtsp/src/test/java/com/pedro/rtsp/rtp/H264PacketTest.kt b/rtsp/src/test/java/com/pedro/rtsp/rtp/H264PacketTest.kt index 62854d23b..0231f5d92 100644 --- a/rtsp/src/test/java/com/pedro/rtsp/rtp/H264PacketTest.kt +++ b/rtsp/src/test/java/com/pedro/rtsp/rtp/H264PacketTest.kt @@ -41,12 +41,11 @@ class H264PacketTest { val info = MediaFrame.Info(0, fakeH264.size, timestamp, true) val fakeSps = byteArrayOf(0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04) val fakePps = byteArrayOf(0x00, 0x00, 0x00, 0x01, 0x0A, 0x0B, 0x0C) + val mediaFrame = MediaFrame(ByteBuffer.wrap(fakeH264), info, MediaFrame.Type.VIDEO) val h264Packet = H264Packet().apply { sendVideoInfo(ByteBuffer.wrap(fakeSps), ByteBuffer.wrap(fakePps)) } h264Packet.setSSRC(123456789) val frames = mutableListOf() - h264Packet.createAndSendPacket(ByteBuffer.wrap(fakeH264), info) { - frames.addAll(it) - } + h264Packet.createAndSendPacket(mediaFrame) { frames.addAll(it) } val expectedRtp = byteArrayOf(-128, -32, 0, 2, 0, -87, -118, -57, 7, 91, -51, 21, 5).plus(fakeH264.copyOfRange(header.size, fakeH264.size)) val expectedStapA = byteArrayOf(-128, -32, 0, 1, 0, -87, -118, -57, 7, 91, -51, 21, 24, 0, 7, 0, 0, 0, 1, 2, 3, 4, 0, 7, 0, 0, 0, 1, 10, 11, 12) @@ -70,12 +69,11 @@ class H264PacketTest { val info = MediaFrame.Info(0, fakeH264.size, timestamp, true) val fakeSps = byteArrayOf(0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04) val fakePps = byteArrayOf(0x00, 0x00, 0x00, 0x01, 0x0A, 0x0B, 0x0C) + val mediaFrame = MediaFrame(ByteBuffer.wrap(fakeH264), info, MediaFrame.Type.VIDEO) val h264Packet = H264Packet().apply { sendVideoInfo(ByteBuffer.wrap(fakeSps), ByteBuffer.wrap(fakePps)) } h264Packet.setSSRC(123456789) val frames = mutableListOf() - h264Packet.createAndSendPacket(ByteBuffer.wrap(fakeH264), info) { - frames.addAll(it) - } + h264Packet.createAndSendPacket(mediaFrame) { frames.addAll(it) } val packet1Size = RtpConstants.MTU - 28 - RtpConstants.RTP_HEADER_LENGTH - 2 val chunk1 = fakeH264.copyOfRange(header.size, header.size + packet1Size) diff --git a/rtsp/src/test/java/com/pedro/rtsp/rtp/H265PacketTest.kt b/rtsp/src/test/java/com/pedro/rtsp/rtp/H265PacketTest.kt index 32e46ea5e..3e40a2719 100644 --- a/rtsp/src/test/java/com/pedro/rtsp/rtp/H265PacketTest.kt +++ b/rtsp/src/test/java/com/pedro/rtsp/rtp/H265PacketTest.kt @@ -39,13 +39,11 @@ class H265PacketTest { val fakeH265 = header.plus(ByteArray(300) { 0x00 }) val info = MediaFrame.Info(0, fakeH265.size, timestamp, true) - + val mediaFrame = MediaFrame(ByteBuffer.wrap(fakeH265), info, MediaFrame.Type.VIDEO) val h265Packet = H265Packet() h265Packet.setSSRC(123456789) val frames = mutableListOf() - h265Packet.createAndSendPacket(ByteBuffer.wrap(fakeH265), info) { - frames.addAll(it) - } + h265Packet.createAndSendPacket(mediaFrame) { frames.addAll(it) } val expectedRtp = byteArrayOf(-128, -32, 0, 1, 0, -87, -118, -57, 7, 91, -51, 21, 5, 0).plus(fakeH265.copyOfRange(header.size, fakeH265.size)) val expectedTimeStamp = 11111111L @@ -64,12 +62,11 @@ class H265PacketTest { val fakeH265 = header.plus(ByteArray(2500) { 0x00 }) val info = MediaFrame.Info(0, fakeH265.size, timestamp, true) + val mediaFrame = MediaFrame(ByteBuffer.wrap(fakeH265), info, MediaFrame.Type.VIDEO) val h265Packet = H265Packet() h265Packet.setSSRC(123456789) val frames = mutableListOf() - h265Packet.createAndSendPacket(ByteBuffer.wrap(fakeH265), info) { - frames.addAll(it) - } + h265Packet.createAndSendPacket(mediaFrame) { frames.addAll(it) } val packet1Size = RtpConstants.MTU - 28 - RtpConstants.RTP_HEADER_LENGTH - 3 val chunk1 = fakeH265.copyOfRange(header.size, header.size + packet1Size) diff --git a/rtsp/src/test/java/com/pedro/rtsp/rtp/OpusPacketTest.kt b/rtsp/src/test/java/com/pedro/rtsp/rtp/OpusPacketTest.kt index 185f95035..4fd1acb3c 100644 --- a/rtsp/src/test/java/com/pedro/rtsp/rtp/OpusPacketTest.kt +++ b/rtsp/src/test/java/com/pedro/rtsp/rtp/OpusPacketTest.kt @@ -36,12 +36,11 @@ class OpusPacketTest { val fakeOpus = ByteArray(30) { 0x05 } val info = MediaFrame.Info(0, fakeOpus.size, timestamp, false) + val mediaFrame = MediaFrame(ByteBuffer.wrap(fakeOpus), info, MediaFrame.Type.AUDIO) val opusPacket = OpusPacket().apply { setAudioInfo(8000) } opusPacket.setSSRC(123456789) val frames = mutableListOf() - opusPacket.createAndSendPacket(ByteBuffer.wrap(fakeOpus), info) { - frames.addAll(it) - } + opusPacket.createAndSendPacket(mediaFrame) { frames.addAll(it) } val expectedRtp = byteArrayOf(-128, -31, 0, 1, 0, 15, 18, 6, 7, 91, -51, 21).plus(fakeOpus) val expectedTimeStamp = 987654L diff --git a/srt/src/main/java/com/pedro/srt/mpeg2ts/packets/AacPacket.kt b/srt/src/main/java/com/pedro/srt/mpeg2ts/packets/AacPacket.kt index e5b3aee81..4b8c6c42b 100644 --- a/srt/src/main/java/com/pedro/srt/mpeg2ts/packets/AacPacket.kt +++ b/srt/src/main/java/com/pedro/srt/mpeg2ts/packets/AacPacket.kt @@ -39,11 +39,10 @@ class AacPacket( private var isStereo = true override suspend fun createAndSendPacket( - byteBuffer: ByteBuffer, - info: MediaFrame.Info, + mediaFrame: MediaFrame, callback: suspend (List) -> Unit ) { - val fixedBuffer = byteBuffer.removeInfo(info) + val fixedBuffer = mediaFrame.data.removeInfo(mediaFrame.info) val length = fixedBuffer.remaining() if (length < 0) return @@ -51,7 +50,7 @@ class AacPacket( writeAdts(payload, payload.size, 0) fixedBuffer.get(payload, header.size, length) - val pes = Pes(psiManager.getAudioPid().toInt(), false, PesType.AUDIO, info.timestamp, ByteBuffer.wrap(payload)) + val pes = Pes(psiManager.getAudioPid().toInt(), false, PesType.AUDIO, mediaFrame.info.timestamp, ByteBuffer.wrap(payload)) val mpeg2tsPackets = mpegTsPacketizer.write(listOf(pes)) val chunked = mpeg2tsPackets.chunked(chunkSize) val packets = mutableListOf() 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 c527a24d0..18f8eda7f 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 @@ -20,7 +20,6 @@ import com.pedro.common.frame.MediaFrame import com.pedro.srt.mpeg2ts.MpegTsPacket import com.pedro.srt.mpeg2ts.MpegTsPacketizer import com.pedro.srt.mpeg2ts.psi.PsiManager -import java.nio.ByteBuffer /** * Created by pedro on 20/8/23. @@ -51,8 +50,7 @@ abstract class BasePacket( protected var chunkSize = limitSize / MpegTsPacketizer.packetSize //max number of ts packets per srtpacket abstract suspend fun createAndSendPacket( - byteBuffer: ByteBuffer, - info: MediaFrame.Info, + mediaFrame: MediaFrame, callback: suspend (List) -> Unit ) 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 ecee92f69..dad50af9f 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 @@ -18,7 +18,6 @@ package com.pedro.srt.mpeg2ts.packets import android.util.Log import com.pedro.common.frame.MediaFrame -import com.pedro.common.isKeyframe import com.pedro.common.removeInfo import com.pedro.common.toByteArray import com.pedro.srt.mpeg2ts.Codec @@ -50,14 +49,13 @@ class H26XPacket( private var configSend = false override suspend fun createAndSendPacket( - byteBuffer: ByteBuffer, - info: MediaFrame.Info, + mediaFrame: MediaFrame, callback: suspend (List) -> Unit ) { - val fixedBuffer = byteBuffer.removeInfo(info) + val fixedBuffer = mediaFrame.data.removeInfo(mediaFrame.info) val length = fixedBuffer.remaining() if (length < 0) return - val isKeyFrame = info.isKeyFrame + val isKeyFrame = mediaFrame.info.isKeyFrame if (codec == Codec.HEVC) { val sps = this.sps @@ -79,7 +77,7 @@ class H26XPacket( val payload = ByteArray(validBuffer.remaining()) validBuffer.get(payload, 0, validBuffer.remaining()) - val pes = Pes(psiManager.getVideoPid().toInt(), isKeyFrame, PesType.VIDEO, info.timestamp, ByteBuffer.wrap(payload)) + val pes = Pes(psiManager.getVideoPid().toInt(), isKeyFrame, PesType.VIDEO, mediaFrame.info.timestamp, ByteBuffer.wrap(payload)) val mpeg2tsPackets = mpegTsPacketizer.write(listOf(pes)) val chunked = mpeg2tsPackets.chunked(chunkSize) val packets = mutableListOf() diff --git a/srt/src/main/java/com/pedro/srt/mpeg2ts/packets/OpusPacket.kt b/srt/src/main/java/com/pedro/srt/mpeg2ts/packets/OpusPacket.kt index da1fbe4f2..50cdb4ae6 100644 --- a/srt/src/main/java/com/pedro/srt/mpeg2ts/packets/OpusPacket.kt +++ b/srt/src/main/java/com/pedro/srt/mpeg2ts/packets/OpusPacket.kt @@ -36,11 +36,10 @@ class OpusPacket( ): BasePacket(psiManager, limitSize) { override suspend fun createAndSendPacket( - byteBuffer: ByteBuffer, - info: MediaFrame.Info, + mediaFrame: MediaFrame, callback: suspend (List) -> Unit ) { - val fixedBuffer = byteBuffer.removeInfo(info) + val fixedBuffer = mediaFrame.data.removeInfo(mediaFrame.info) val length = fixedBuffer.remaining() if (length < 0) return @@ -49,7 +48,7 @@ class OpusPacket( fixedBuffer.get(payload, header.size, length) System.arraycopy(header, 0, payload, 0, header.size) - val pes = Pes(psiManager.getAudioPid().toInt(), true, PesType.PRIVATE_STREAM_1, info.timestamp, ByteBuffer.wrap(payload)) + val pes = Pes(psiManager.getAudioPid().toInt(), true, PesType.PRIVATE_STREAM_1, mediaFrame.info.timestamp, ByteBuffer.wrap(payload)) val mpeg2tsPackets = mpegTsPacketizer.write(listOf(pes)) val chunked = mpeg2tsPackets.chunked(chunkSize) val packets = mutableListOf() 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 6a10adb6d..26a0339ae 100644 --- a/srt/src/main/java/com/pedro/srt/srt/SrtSender.kt +++ b/srt/src/main/java/com/pedro/srt/srt/SrtSender.kt @@ -140,16 +140,8 @@ class SrtSender( private suspend fun getMpegTsPackets(mediaFrame: MediaFrame?, callback: suspend (List) -> Unit) { if (mediaFrame == null) return when (mediaFrame.type) { - MediaFrame.Type.VIDEO -> { - videoPacket.createAndSendPacket(mediaFrame.data, mediaFrame.info) { packets -> - callback(packets) - } - } - MediaFrame.Type.AUDIO -> { - audioPacket.createAndSendPacket(mediaFrame.data, mediaFrame.info) { packets -> - callback(packets) - } - } + MediaFrame.Type.VIDEO -> videoPacket.createAndSendPacket(mediaFrame) { callback(it) } + MediaFrame.Type.AUDIO -> audioPacket.createAndSendPacket(mediaFrame) { callback(it) } } } } \ No newline at end of file diff --git a/udp/src/main/java/com/pedro/udp/UdpSender.kt b/udp/src/main/java/com/pedro/udp/UdpSender.kt index 23c696f2f..d7c9bf870 100644 --- a/udp/src/main/java/com/pedro/udp/UdpSender.kt +++ b/udp/src/main/java/com/pedro/udp/UdpSender.kt @@ -140,16 +140,8 @@ class UdpSender( private suspend fun getMpegTsPackets(mediaFrame: MediaFrame?, callback: suspend (List) -> Unit) { if (mediaFrame == null) return when (mediaFrame.type) { - MediaFrame.Type.VIDEO -> { - videoPacket.createAndSendPacket(mediaFrame.data, mediaFrame.info) { packets -> - callback(packets) - } - } - MediaFrame.Type.AUDIO -> { - audioPacket.createAndSendPacket(mediaFrame.data, mediaFrame.info) { packets -> - callback(packets) - } - } + MediaFrame.Type.VIDEO -> videoPacket.createAndSendPacket(mediaFrame) { callback(it) } + MediaFrame.Type.AUDIO -> audioPacket.createAndSendPacket(mediaFrame) { callback(it) } } } } \ No newline at end of file