From 19ab6265fc68d44f2056d31a5396f3d22626d121 Mon Sep 17 00:00:00 2001 From: pedro Date: Fri, 23 Feb 2024 01:42:46 +0100 Subject: [PATCH] code refactor --- .../streamer/openglexample/OpenGlSrtActivity.java | 2 -- .../java/com/pedro/library/base/Camera1Base.java | 2 +- .../com/pedro/srt/mpeg2ts/packets/AacPacket.kt | 4 ++-- .../com/pedro/srt/mpeg2ts/packets/OpusPacket.kt | 15 +-------------- srt/src/main/java/com/pedro/srt/srt/SrtSender.kt | 1 - .../main/java/com/pedro/srt/utils/Extensions.kt | 1 + 6 files changed, 5 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/pedro/streamer/openglexample/OpenGlSrtActivity.java b/app/src/main/java/com/pedro/streamer/openglexample/OpenGlSrtActivity.java index f157449d8..0d3ccf07c 100644 --- a/app/src/main/java/com/pedro/streamer/openglexample/OpenGlSrtActivity.java +++ b/app/src/main/java/com/pedro/streamer/openglexample/OpenGlSrtActivity.java @@ -38,7 +38,6 @@ import androidx.annotation.RequiresApi; import androidx.appcompat.app.AppCompatActivity; -import com.pedro.common.AudioCodec; import com.pedro.common.ConnectChecker; import com.pedro.encoder.input.gl.SpriteGestureController; import com.pedro.encoder.input.gl.render.filters.AnalogTVFilterRender; @@ -136,7 +135,6 @@ protected void onCreate(Bundle savedInstanceState) { etUrl = findViewById(R.id.et_rtp_url); etUrl.setHint(R.string.hint_srt); srtCamera1 = new SrtCamera1(openGlView, this); - srtCamera1.setAudioCodec(AudioCodec.OPUS); openGlView.getHolder().addCallback(this); openGlView.setOnTouchListener(this); } diff --git a/library/src/main/java/com/pedro/library/base/Camera1Base.java b/library/src/main/java/com/pedro/library/base/Camera1Base.java index 6dd7beafb..e2b58a758 100644 --- a/library/src/main/java/com/pedro/library/base/Camera1Base.java +++ b/library/src/main/java/com/pedro/library/base/Camera1Base.java @@ -361,7 +361,7 @@ public boolean prepareVideo() { * doesn't support any configuration seated or your device hasn't a AAC encoder). */ public boolean prepareAudio() { - return prepareAudio(64 * 1024, 48000, true, false, false); + return prepareAudio(64 * 1024, 32000, true, false, false); } /** 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 4f3525b7c..057087078 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 @@ -35,8 +35,8 @@ class AacPacket( ): BasePacket(psiManager, limitSize) { private val header = ByteArray(7) //ADTS header - var sampleRate = 44100 - var isStereo = true + private var sampleRate = 44100 + private var isStereo = true override fun createAndSendPacket( byteBuffer: ByteBuffer, 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 284c69427..1fb154327 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 @@ -35,9 +35,6 @@ class OpusPacket( psiManager: PsiManager, ): BasePacket(psiManager, limitSize) { - var sampleRate = 48000 - var isStereo = true - override fun createAndSendPacket( byteBuffer: ByteBuffer, info: MediaCodec.BufferInfo, @@ -68,17 +65,7 @@ class OpusPacket( callback(packets) } - override fun resetPacket(resetInfo: Boolean) { - if (resetInfo) { - sampleRate = 48000 - isStereo = true - } - } - - fun sendAudioInfo(sampleRate: Int, stereo: Boolean) { - this.sampleRate = sampleRate - this.isStereo = stereo - } + override fun resetPacket(resetInfo: Boolean) { } private fun createControlHeader(payloadLength: Int): ByteArray { val bytes = payloadLength.toByteArray() 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 937796f76..62811d5d4 100644 --- a/srt/src/main/java/com/pedro/srt/srt/SrtSender.kt +++ b/srt/src/main/java/com/pedro/srt/srt/SrtSender.kt @@ -117,7 +117,6 @@ class SrtSender( } AudioCodec.OPUS -> { audioPacket = OpusPacket(limitSize, psiManager) - (audioPacket as? OpusPacket)?.sendAudioInfo(sampleRate, isStereo) } AudioCodec.G711 -> { throw IllegalArgumentException("Unsupported codec: ${commandsManager.audioCodec.name}") diff --git a/srt/src/main/java/com/pedro/srt/utils/Extensions.kt b/srt/src/main/java/com/pedro/srt/utils/Extensions.kt index 2500cbcb3..ccc4b5256 100644 --- a/srt/src/main/java/com/pedro/srt/utils/Extensions.kt +++ b/srt/src/main/java/com/pedro/srt/utils/Extensions.kt @@ -66,6 +66,7 @@ fun InputStream.readUntil(byteArray: ByteArray) { if (result != -1) bytesRead += result } } + fun Int.toByteArray(): ByteArray { val bytes = mutableListOf() var remainingValue = this