From f8a56aae914045c59c8323a2c5841577febd1a8e Mon Sep 17 00:00:00 2001 From: Alessandro Ros Date: Sun, 21 Jan 2024 16:29:13 +0100 Subject: [PATCH] rtmp: simplify Audio message structure (#2924) --- internal/protocols/rtmp/message/audio.go | 19 ++++++++++--------- .../protocols/rtmp/message/reader_test.go | 4 ++-- internal/protocols/rtmp/reader.go | 10 +++++----- internal/protocols/rtmp/reader_test.go | 18 +++++++++--------- internal/protocols/rtmp/writer.go | 13 +++++-------- internal/protocols/rtmp/writer_test.go | 2 +- 6 files changed, 32 insertions(+), 34 deletions(-) diff --git a/internal/protocols/rtmp/message/audio.go b/internal/protocols/rtmp/message/audio.go index 06568983..2ae4a6d4 100644 --- a/internal/protocols/rtmp/message/audio.go +++ b/internal/protocols/rtmp/message/audio.go @@ -35,12 +35,6 @@ const ( Depth16 = 1 ) -// audio channels -const ( - ChannelsMono = 0 - ChannelsStereo = 1 -) - // AudioAACType is the AAC type of a Audio. type AudioAACType uint8 @@ -58,7 +52,7 @@ type Audio struct { Codec uint8 Rate uint8 Depth uint8 - Channels uint8 + IsStereo bool AACType AudioAACType // only for CodecMPEG4Audio Payload []byte } @@ -82,7 +76,10 @@ func (m *Audio) Unmarshal(raw *rawmessage.Message) error { m.Rate = (raw.Body[0] >> 2) & 0x03 m.Depth = (raw.Body[0] >> 1) & 0x01 - m.Channels = raw.Body[0] & 0x01 + + if (raw.Body[0] & 0x01) != 0 { + m.IsStereo = true + } if m.Codec == CodecMPEG4Audio { m.AACType = AudioAACType(raw.Body[1]) @@ -114,7 +111,11 @@ func (m Audio) marshalBodySize() int { func (m Audio) Marshal() (*rawmessage.Message, error) { body := make([]byte, m.marshalBodySize()) - body[0] = m.Codec<<4 | m.Rate<<2 | m.Depth<<1 | m.Channels + body[0] = m.Codec<<4 | m.Rate<<2 | m.Depth<<1 + + if m.IsStereo { + body[0] |= 1 + } if m.Codec == CodecMPEG4Audio { body[1] = uint8(m.AACType) diff --git a/internal/protocols/rtmp/message/reader_test.go b/internal/protocols/rtmp/message/reader_test.go index a6d16fab..dcda6bd3 100644 --- a/internal/protocols/rtmp/message/reader_test.go +++ b/internal/protocols/rtmp/message/reader_test.go @@ -35,7 +35,7 @@ var readWriterCases = []struct { Codec: CodecMPEG1Audio, Rate: Rate44100, Depth: Depth16, - Channels: ChannelsStereo, + IsStereo: true, Payload: []byte{0x01, 0x02, 0x03, 0x04}, }, []byte{ @@ -52,7 +52,7 @@ var readWriterCases = []struct { Codec: CodecMPEG4Audio, Rate: Rate44100, Depth: Depth16, - Channels: ChannelsStereo, + IsStereo: true, AACType: AudioAACTypeAU, Payload: []byte{0x5A, 0xC0, 0x77, 0x40}, }, diff --git a/internal/protocols/rtmp/reader.go b/internal/protocols/rtmp/reader.go index 6daf3b79..ff3da74f 100644 --- a/internal/protocols/rtmp/reader.go +++ b/internal/protocols/rtmp/reader.go @@ -333,7 +333,7 @@ func tracksFromMetadata(conn *Conn, payload []interface{}) (format.Format, forma MULaw: false, SampleRate: 8000, ChannelCount: func() int { - if msg.Channels == message.ChannelsStereo { + if msg.IsStereo { return 2 } return 1 @@ -346,7 +346,7 @@ func tracksFromMetadata(conn *Conn, payload []interface{}) (format.Format, forma MULaw: true, SampleRate: 8000, ChannelCount: func() int { - if msg.Channels == message.ChannelsStereo { + if msg.IsStereo { return 2 } return 1 @@ -364,10 +364,10 @@ func tracksFromMetadata(conn *Conn, payload []interface{}) (format.Format, forma }(), SampleRate: audioRateRTMPToInt(msg.Rate), ChannelCount: func() int { - if msg.Channels == message.ChannelsMono { - return 1 + if msg.IsStereo { + return 2 } - return 2 + return 1 }(), } } diff --git a/internal/protocols/rtmp/reader_test.go b/internal/protocols/rtmp/reader_test.go index af39979d..e6ff927c 100644 --- a/internal/protocols/rtmp/reader_test.go +++ b/internal/protocols/rtmp/reader_test.go @@ -174,7 +174,7 @@ func TestReadTracks(t *testing.T) { Codec: message.CodecMPEG4Audio, Rate: message.Rate44100, Depth: message.Depth16, - Channels: message.ChannelsStereo, + IsStereo: true, AACType: message.AudioAACTypeConfig, Payload: func() []byte { enc, err := mpeg4audio.Config{ @@ -294,7 +294,7 @@ func TestReadTracks(t *testing.T) { Codec: message.CodecMPEG4Audio, Rate: message.Rate44100, Depth: message.Depth16, - Channels: message.ChannelsStereo, + IsStereo: true, AACType: message.AudioAACTypeConfig, Payload: func() []byte { enc, err := mpeg4audio.Config{ @@ -329,7 +329,7 @@ func TestReadTracks(t *testing.T) { Codec: message.CodecMPEG4Audio, Rate: message.Rate44100, Depth: message.Depth16, - Channels: message.ChannelsStereo, + IsStereo: true, AACType: message.AudioAACTypeConfig, Payload: func() []byte { enc, err := mpeg4audio.Config{ @@ -347,7 +347,7 @@ func TestReadTracks(t *testing.T) { Codec: message.CodecMPEG4Audio, Rate: message.Rate44100, Depth: message.Depth16, - Channels: message.ChannelsStereo, + IsStereo: true, AACType: message.AudioAACTypeConfig, Payload: func() []byte { enc, err := mpeg4audio.Config{ @@ -430,7 +430,7 @@ func TestReadTracks(t *testing.T) { Codec: message.CodecMPEG4Audio, Rate: message.Rate44100, Depth: message.Depth16, - Channels: message.ChannelsStereo, + IsStereo: true, AACType: message.AudioAACTypeConfig, Payload: func() []byte { enc, err := mpeg4audio.Config{ @@ -674,7 +674,7 @@ func TestReadTracks(t *testing.T) { Codec: 0xa, Rate: 0x3, Depth: 0x1, - Channels: 0x1, + IsStereo: true, Payload: []uint8{0x11, 0x88}, }, }, @@ -813,7 +813,7 @@ func TestReadTracks(t *testing.T) { Codec: message.CodecPCMA, Rate: message.Rate5512, Depth: message.Depth16, - Channels: message.ChannelsMono, + IsStereo: false, Payload: []byte{1, 2, 3, 4}, }, }, @@ -848,7 +848,7 @@ func TestReadTracks(t *testing.T) { Codec: message.CodecPCMU, Rate: message.Rate5512, Depth: message.Depth16, - Channels: message.ChannelsMono, + IsStereo: false, Payload: []byte{1, 2, 3, 4}, }, }, @@ -882,7 +882,7 @@ func TestReadTracks(t *testing.T) { Codec: message.CodecLPCM, Rate: message.Rate44100, Depth: message.Depth16, - Channels: message.ChannelsStereo, + IsStereo: true, Payload: []byte{1, 2, 3, 4}, }, }, diff --git a/internal/protocols/rtmp/writer.go b/internal/protocols/rtmp/writer.go index 10df62c9..8ff2af1b 100644 --- a/internal/protocols/rtmp/writer.go +++ b/internal/protocols/rtmp/writer.go @@ -39,11 +39,8 @@ func audioRateIntToRTMP(v int) uint8 { } } -func mpeg1AudioChannels(m mpeg1audio.ChannelMode) uint8 { - if m == mpeg1audio.ChannelModeMono { - return message.ChannelsMono - } - return message.ChannelsStereo +func mpeg1AudioChannels(m mpeg1audio.ChannelMode) bool { + return m != mpeg1audio.ChannelModeMono } // Writer is a wrapper around Conn that provides utilities to mux outgoing data. @@ -156,7 +153,7 @@ func (w *Writer) writeTracks(videoTrack format.Format, audioTrack format.Format) Codec: message.CodecMPEG4Audio, Rate: message.Rate44100, Depth: message.Depth16, - Channels: message.ChannelsStereo, + IsStereo: true, AACType: message.AudioAACTypeConfig, Payload: enc, }) @@ -195,7 +192,7 @@ func (w *Writer) WriteMPEG4Audio(pts time.Duration, au []byte) error { Codec: message.CodecMPEG4Audio, Rate: message.Rate44100, Depth: message.Depth16, - Channels: message.ChannelsStereo, + IsStereo: true, AACType: message.AudioAACTypeAU, Payload: au, DTS: pts, @@ -210,7 +207,7 @@ func (w *Writer) WriteMPEG1Audio(pts time.Duration, h *mpeg1audio.FrameHeader, f Codec: message.CodecMPEG1Audio, Rate: audioRateIntToRTMP(h.SampleRate), Depth: message.Depth16, - Channels: mpeg1AudioChannels(h.ChannelMode), + IsStereo: mpeg1AudioChannels(h.ChannelMode), Payload: frame, DTS: pts, }) diff --git a/internal/protocols/rtmp/writer_test.go b/internal/protocols/rtmp/writer_test.go index dfb03f29..00fbdd38 100644 --- a/internal/protocols/rtmp/writer_test.go +++ b/internal/protocols/rtmp/writer_test.go @@ -91,7 +91,7 @@ func TestWriteTracks(t *testing.T) { Codec: message.CodecMPEG4Audio, Rate: message.Rate44100, Depth: message.Depth16, - Channels: message.ChannelsStereo, + IsStereo: true, AACType: message.AudioAACTypeConfig, Payload: []byte{0x12, 0x10}, }, msg)