Browse Source

rtmp: simplify Audio message structure (#2924)

pull/2925/head
Alessandro Ros 1 year ago committed by GitHub
parent
commit
f8a56aae91
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 19
      internal/protocols/rtmp/message/audio.go
  2. 4
      internal/protocols/rtmp/message/reader_test.go
  3. 10
      internal/protocols/rtmp/reader.go
  4. 18
      internal/protocols/rtmp/reader_test.go
  5. 13
      internal/protocols/rtmp/writer.go
  6. 2
      internal/protocols/rtmp/writer_test.go

19
internal/protocols/rtmp/message/audio.go

@ -35,12 +35,6 @@ const ( @@ -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 { @@ -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 { @@ -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 { @@ -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)

4
internal/protocols/rtmp/message/reader_test.go

@ -35,7 +35,7 @@ var readWriterCases = []struct { @@ -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 { @@ -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},
},

10
internal/protocols/rtmp/reader.go

@ -333,7 +333,7 @@ func tracksFromMetadata(conn *Conn, payload []interface{}) (format.Format, forma @@ -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 @@ -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 @@ -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
}(),
}
}

18
internal/protocols/rtmp/reader_test.go

@ -174,7 +174,7 @@ func TestReadTracks(t *testing.T) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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},
},
},

13
internal/protocols/rtmp/writer.go

@ -39,11 +39,8 @@ func audioRateIntToRTMP(v int) uint8 { @@ -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) @@ -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 { @@ -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 @@ -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,
})

2
internal/protocols/rtmp/writer_test.go

@ -91,7 +91,7 @@ func TestWriteTracks(t *testing.T) { @@ -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)

Loading…
Cancel
Save