From f61e7ad2d16b64546b448498402b23e51dfac7e4 Mon Sep 17 00:00:00 2001 From: pedro Date: Thu, 21 Dec 2023 03:31:07 +0100 Subject: [PATCH] support g711a in rtmp --- README.md | 2 +- .../com/pedro/library/rtmp/RtmpCamera1.java | 2 +- .../com/pedro/library/rtmp/RtmpCamera2.java | 2 +- .../com/pedro/library/rtmp/RtmpDisplay.java | 2 +- .../com/pedro/library/rtmp/RtmpFromFile.java | 2 +- .../com/pedro/library/rtmp/RtmpOnlyAudio.java | 2 +- .../java/com/pedro/library/rtmp/RtmpStream.kt | 2 +- .../pedro/rtmp/flv/audio/packet/G711Packet.kt | 23 +++---------------- 8 files changed, 10 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index f4fa2af37..8ade78953 100644 --- a/README.md +++ b/README.md @@ -101,7 +101,7 @@ dependencies { - [X] Get upload bandwidth used. - [x] RTSP auth (adobe and llnw). -- [x] H264, AV1, H265 ([Using RTMP enhanced](https://github.com/veovera/enhanced-rtmp/tree/main)) and AAC support. +- [x] H264, AV1, H265 ([Using RTMP enhanced](https://github.com/veovera/enhanced-rtmp/tree/main)), AAC and G711 support. - [x] RTMPS (under TLS) - [x] RTMPT and RTMPTS (tunneled and tunneled under TLS) - [x] AMF0 diff --git a/library/src/main/java/com/pedro/library/rtmp/RtmpCamera1.java b/library/src/main/java/com/pedro/library/rtmp/RtmpCamera1.java index d9b7624ea..786313167 100644 --- a/library/src/main/java/com/pedro/library/rtmp/RtmpCamera1.java +++ b/library/src/main/java/com/pedro/library/rtmp/RtmpCamera1.java @@ -94,7 +94,7 @@ protected void setVideoCodecImp(VideoCodec codec) { @Override protected void setAudioCodecImp(AudioCodec codec) { - if (codec != AudioCodec.AAC) throw new IllegalArgumentException("Unsupported codec: " + codec.name()); + rtmpClient.setAudioCodec(codec); } @Override diff --git a/library/src/main/java/com/pedro/library/rtmp/RtmpCamera2.java b/library/src/main/java/com/pedro/library/rtmp/RtmpCamera2.java index 30b105606..55855601c 100644 --- a/library/src/main/java/com/pedro/library/rtmp/RtmpCamera2.java +++ b/library/src/main/java/com/pedro/library/rtmp/RtmpCamera2.java @@ -103,7 +103,7 @@ protected void setVideoCodecImp(VideoCodec codec) { @Override protected void setAudioCodecImp(AudioCodec codec) { - if (codec != AudioCodec.AAC) throw new IllegalArgumentException("Unsupported codec: " + codec.name()); + rtmpClient.setAudioCodec(codec); } @Override diff --git a/library/src/main/java/com/pedro/library/rtmp/RtmpDisplay.java b/library/src/main/java/com/pedro/library/rtmp/RtmpDisplay.java index e4b769506..2b0d37f71 100644 --- a/library/src/main/java/com/pedro/library/rtmp/RtmpDisplay.java +++ b/library/src/main/java/com/pedro/library/rtmp/RtmpDisplay.java @@ -63,7 +63,7 @@ protected void setVideoCodecImp(VideoCodec codec) { @Override protected void setAudioCodecImp(AudioCodec codec) { - if (codec != AudioCodec.AAC) throw new IllegalArgumentException("Unsupported codec: " + codec.name()); + rtmpClient.setAudioCodec(codec); } @Override diff --git a/library/src/main/java/com/pedro/library/rtmp/RtmpFromFile.java b/library/src/main/java/com/pedro/library/rtmp/RtmpFromFile.java index 153fb82bc..518342041 100644 --- a/library/src/main/java/com/pedro/library/rtmp/RtmpFromFile.java +++ b/library/src/main/java/com/pedro/library/rtmp/RtmpFromFile.java @@ -89,7 +89,7 @@ protected void setVideoCodecImp(VideoCodec codec) { @Override protected void setAudioCodecImp(AudioCodec codec) { - if (codec != AudioCodec.AAC) throw new IllegalArgumentException("Unsupported codec: " + codec.name()); + rtmpClient.setAudioCodec(codec); } @Override diff --git a/library/src/main/java/com/pedro/library/rtmp/RtmpOnlyAudio.java b/library/src/main/java/com/pedro/library/rtmp/RtmpOnlyAudio.java index 03485584b..fa8d08bff 100644 --- a/library/src/main/java/com/pedro/library/rtmp/RtmpOnlyAudio.java +++ b/library/src/main/java/com/pedro/library/rtmp/RtmpOnlyAudio.java @@ -51,7 +51,7 @@ public RtmpStreamClient getStreamClient() { @Override protected void setAudioCodecImp(AudioCodec codec) { - if (codec != AudioCodec.AAC) throw new IllegalArgumentException("Unsupported codec: " + codec.name()); + rtmpClient.setAudioCodec(codec); } @Override diff --git a/library/src/main/java/com/pedro/library/rtmp/RtmpStream.kt b/library/src/main/java/com/pedro/library/rtmp/RtmpStream.kt index 00a3cd902..309344869 100644 --- a/library/src/main/java/com/pedro/library/rtmp/RtmpStream.kt +++ b/library/src/main/java/com/pedro/library/rtmp/RtmpStream.kt @@ -60,7 +60,7 @@ class RtmpStream( } override fun setAudioCodecImp(codec: AudioCodec) { - require(codec == AudioCodec.AAC) { "Unsupported codec: ${codec.name}" } + rtmpClient.setAudioCodec(codec) } override fun audioInfo(sampleRate: Int, isStereo: Boolean) { 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 c52d88d14..c36b9c628 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 @@ -26,7 +26,6 @@ import com.pedro.rtmp.flv.audio.AudioObjectType 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 @@ -35,7 +34,7 @@ import kotlin.experimental.or */ class G711Packet: BasePacket() { - private val header = ByteArray(2) + private val header = ByteArray(1) //first time we need send audio config private var configSend = false @@ -43,12 +42,6 @@ class G711Packet: BasePacket() { private var isStereo = true //In microphone we are using always 16bits pcm encoding. Change me if needed private var audioSize = AudioSize.SND_16_BIT - //In encoder we are using always AAC LC. Change me if needed - private val objectType = AudioObjectType.AAC_LC - - enum class Type(val mark: Byte) { - SEQUENCE(0x00), RAW(0x01) - } fun sendAudioInfo(sampleRate: Int, isStereo: Boolean, audioSize: AudioSize = AudioSize.SND_16_BIT) { this.sampleRate = sampleRate @@ -76,18 +69,8 @@ class G711Packet: BasePacket() { } header[0] = header[0] or (soundRate.value shl 2).toByte() header[0] = header[0] or (AudioFormat.G711_A.value shl 4).toByte() - val buffer: ByteArray - if (!configSend) { - val config = AudioSpecificConfig(objectType.value, sampleRate, if (isStereo) 2 else 1) - buffer = ByteArray(config.size + header.size) - header[1] = Type.SEQUENCE.mark - config.write(buffer, header.size) - configSend = true - } else { - header[1] = Type.RAW.mark - buffer = ByteArray(fixedBuffer.remaining() + header.size) - fixedBuffer.get(buffer, header.size, fixedBuffer.remaining()) - } + 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.presentationTimeUs / 1000 callback(FlvPacket(buffer, ts, buffer.size, FlvType.AUDIO))