Skip to content

Commit

Permalink
support g711a in rtmp
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroSG94 committed Dec 21, 2023
1 parent 6b28f89 commit f61e7ad
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion library/src/main/java/com/pedro/library/rtmp/RtmpStream.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
23 changes: 3 additions & 20 deletions rtmp/src/main/java/com/pedro/rtmp/flv/audio/packet/G711Packet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -35,20 +34,14 @@ 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

private var sampleRate = 44100
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
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit f61e7ad

Please sign in to comment.