mirror of
https://github.com/aler9/rtsp-simple-server
synced 2025-10-28 18:01:54 +08:00
rtmp: simplify Audio message structure (#2924)
This commit is contained in:
@@ -35,12 +35,6 @@ const (
|
|||||||
Depth16 = 1
|
Depth16 = 1
|
||||||
)
|
)
|
||||||
|
|
||||||
// audio channels
|
|
||||||
const (
|
|
||||||
ChannelsMono = 0
|
|
||||||
ChannelsStereo = 1
|
|
||||||
)
|
|
||||||
|
|
||||||
// AudioAACType is the AAC type of a Audio.
|
// AudioAACType is the AAC type of a Audio.
|
||||||
type AudioAACType uint8
|
type AudioAACType uint8
|
||||||
|
|
||||||
@@ -58,7 +52,7 @@ type Audio struct {
|
|||||||
Codec uint8
|
Codec uint8
|
||||||
Rate uint8
|
Rate uint8
|
||||||
Depth uint8
|
Depth uint8
|
||||||
Channels uint8
|
IsStereo bool
|
||||||
AACType AudioAACType // only for CodecMPEG4Audio
|
AACType AudioAACType // only for CodecMPEG4Audio
|
||||||
Payload []byte
|
Payload []byte
|
||||||
}
|
}
|
||||||
@@ -82,7 +76,10 @@ func (m *Audio) Unmarshal(raw *rawmessage.Message) error {
|
|||||||
|
|
||||||
m.Rate = (raw.Body[0] >> 2) & 0x03
|
m.Rate = (raw.Body[0] >> 2) & 0x03
|
||||||
m.Depth = (raw.Body[0] >> 1) & 0x01
|
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 {
|
if m.Codec == CodecMPEG4Audio {
|
||||||
m.AACType = AudioAACType(raw.Body[1])
|
m.AACType = AudioAACType(raw.Body[1])
|
||||||
@@ -114,7 +111,11 @@ func (m Audio) marshalBodySize() int {
|
|||||||
func (m Audio) Marshal() (*rawmessage.Message, error) {
|
func (m Audio) Marshal() (*rawmessage.Message, error) {
|
||||||
body := make([]byte, m.marshalBodySize())
|
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 {
|
if m.Codec == CodecMPEG4Audio {
|
||||||
body[1] = uint8(m.AACType)
|
body[1] = uint8(m.AACType)
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ var readWriterCases = []struct {
|
|||||||
Codec: CodecMPEG1Audio,
|
Codec: CodecMPEG1Audio,
|
||||||
Rate: Rate44100,
|
Rate: Rate44100,
|
||||||
Depth: Depth16,
|
Depth: Depth16,
|
||||||
Channels: ChannelsStereo,
|
IsStereo: true,
|
||||||
Payload: []byte{0x01, 0x02, 0x03, 0x04},
|
Payload: []byte{0x01, 0x02, 0x03, 0x04},
|
||||||
},
|
},
|
||||||
[]byte{
|
[]byte{
|
||||||
@@ -52,7 +52,7 @@ var readWriterCases = []struct {
|
|||||||
Codec: CodecMPEG4Audio,
|
Codec: CodecMPEG4Audio,
|
||||||
Rate: Rate44100,
|
Rate: Rate44100,
|
||||||
Depth: Depth16,
|
Depth: Depth16,
|
||||||
Channels: ChannelsStereo,
|
IsStereo: true,
|
||||||
AACType: AudioAACTypeAU,
|
AACType: AudioAACTypeAU,
|
||||||
Payload: []byte{0x5A, 0xC0, 0x77, 0x40},
|
Payload: []byte{0x5A, 0xC0, 0x77, 0x40},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -333,7 +333,7 @@ func tracksFromMetadata(conn *Conn, payload []interface{}) (format.Format, forma
|
|||||||
MULaw: false,
|
MULaw: false,
|
||||||
SampleRate: 8000,
|
SampleRate: 8000,
|
||||||
ChannelCount: func() int {
|
ChannelCount: func() int {
|
||||||
if msg.Channels == message.ChannelsStereo {
|
if msg.IsStereo {
|
||||||
return 2
|
return 2
|
||||||
}
|
}
|
||||||
return 1
|
return 1
|
||||||
@@ -346,7 +346,7 @@ func tracksFromMetadata(conn *Conn, payload []interface{}) (format.Format, forma
|
|||||||
MULaw: true,
|
MULaw: true,
|
||||||
SampleRate: 8000,
|
SampleRate: 8000,
|
||||||
ChannelCount: func() int {
|
ChannelCount: func() int {
|
||||||
if msg.Channels == message.ChannelsStereo {
|
if msg.IsStereo {
|
||||||
return 2
|
return 2
|
||||||
}
|
}
|
||||||
return 1
|
return 1
|
||||||
@@ -364,10 +364,10 @@ func tracksFromMetadata(conn *Conn, payload []interface{}) (format.Format, forma
|
|||||||
}(),
|
}(),
|
||||||
SampleRate: audioRateRTMPToInt(msg.Rate),
|
SampleRate: audioRateRTMPToInt(msg.Rate),
|
||||||
ChannelCount: func() int {
|
ChannelCount: func() int {
|
||||||
if msg.Channels == message.ChannelsMono {
|
if msg.IsStereo {
|
||||||
return 1
|
|
||||||
}
|
|
||||||
return 2
|
return 2
|
||||||
|
}
|
||||||
|
return 1
|
||||||
}(),
|
}(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -174,7 +174,7 @@ func TestReadTracks(t *testing.T) {
|
|||||||
Codec: message.CodecMPEG4Audio,
|
Codec: message.CodecMPEG4Audio,
|
||||||
Rate: message.Rate44100,
|
Rate: message.Rate44100,
|
||||||
Depth: message.Depth16,
|
Depth: message.Depth16,
|
||||||
Channels: message.ChannelsStereo,
|
IsStereo: true,
|
||||||
AACType: message.AudioAACTypeConfig,
|
AACType: message.AudioAACTypeConfig,
|
||||||
Payload: func() []byte {
|
Payload: func() []byte {
|
||||||
enc, err := mpeg4audio.Config{
|
enc, err := mpeg4audio.Config{
|
||||||
@@ -294,7 +294,7 @@ func TestReadTracks(t *testing.T) {
|
|||||||
Codec: message.CodecMPEG4Audio,
|
Codec: message.CodecMPEG4Audio,
|
||||||
Rate: message.Rate44100,
|
Rate: message.Rate44100,
|
||||||
Depth: message.Depth16,
|
Depth: message.Depth16,
|
||||||
Channels: message.ChannelsStereo,
|
IsStereo: true,
|
||||||
AACType: message.AudioAACTypeConfig,
|
AACType: message.AudioAACTypeConfig,
|
||||||
Payload: func() []byte {
|
Payload: func() []byte {
|
||||||
enc, err := mpeg4audio.Config{
|
enc, err := mpeg4audio.Config{
|
||||||
@@ -329,7 +329,7 @@ func TestReadTracks(t *testing.T) {
|
|||||||
Codec: message.CodecMPEG4Audio,
|
Codec: message.CodecMPEG4Audio,
|
||||||
Rate: message.Rate44100,
|
Rate: message.Rate44100,
|
||||||
Depth: message.Depth16,
|
Depth: message.Depth16,
|
||||||
Channels: message.ChannelsStereo,
|
IsStereo: true,
|
||||||
AACType: message.AudioAACTypeConfig,
|
AACType: message.AudioAACTypeConfig,
|
||||||
Payload: func() []byte {
|
Payload: func() []byte {
|
||||||
enc, err := mpeg4audio.Config{
|
enc, err := mpeg4audio.Config{
|
||||||
@@ -347,7 +347,7 @@ func TestReadTracks(t *testing.T) {
|
|||||||
Codec: message.CodecMPEG4Audio,
|
Codec: message.CodecMPEG4Audio,
|
||||||
Rate: message.Rate44100,
|
Rate: message.Rate44100,
|
||||||
Depth: message.Depth16,
|
Depth: message.Depth16,
|
||||||
Channels: message.ChannelsStereo,
|
IsStereo: true,
|
||||||
AACType: message.AudioAACTypeConfig,
|
AACType: message.AudioAACTypeConfig,
|
||||||
Payload: func() []byte {
|
Payload: func() []byte {
|
||||||
enc, err := mpeg4audio.Config{
|
enc, err := mpeg4audio.Config{
|
||||||
@@ -430,7 +430,7 @@ func TestReadTracks(t *testing.T) {
|
|||||||
Codec: message.CodecMPEG4Audio,
|
Codec: message.CodecMPEG4Audio,
|
||||||
Rate: message.Rate44100,
|
Rate: message.Rate44100,
|
||||||
Depth: message.Depth16,
|
Depth: message.Depth16,
|
||||||
Channels: message.ChannelsStereo,
|
IsStereo: true,
|
||||||
AACType: message.AudioAACTypeConfig,
|
AACType: message.AudioAACTypeConfig,
|
||||||
Payload: func() []byte {
|
Payload: func() []byte {
|
||||||
enc, err := mpeg4audio.Config{
|
enc, err := mpeg4audio.Config{
|
||||||
@@ -674,7 +674,7 @@ func TestReadTracks(t *testing.T) {
|
|||||||
Codec: 0xa,
|
Codec: 0xa,
|
||||||
Rate: 0x3,
|
Rate: 0x3,
|
||||||
Depth: 0x1,
|
Depth: 0x1,
|
||||||
Channels: 0x1,
|
IsStereo: true,
|
||||||
Payload: []uint8{0x11, 0x88},
|
Payload: []uint8{0x11, 0x88},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -813,7 +813,7 @@ func TestReadTracks(t *testing.T) {
|
|||||||
Codec: message.CodecPCMA,
|
Codec: message.CodecPCMA,
|
||||||
Rate: message.Rate5512,
|
Rate: message.Rate5512,
|
||||||
Depth: message.Depth16,
|
Depth: message.Depth16,
|
||||||
Channels: message.ChannelsMono,
|
IsStereo: false,
|
||||||
Payload: []byte{1, 2, 3, 4},
|
Payload: []byte{1, 2, 3, 4},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -848,7 +848,7 @@ func TestReadTracks(t *testing.T) {
|
|||||||
Codec: message.CodecPCMU,
|
Codec: message.CodecPCMU,
|
||||||
Rate: message.Rate5512,
|
Rate: message.Rate5512,
|
||||||
Depth: message.Depth16,
|
Depth: message.Depth16,
|
||||||
Channels: message.ChannelsMono,
|
IsStereo: false,
|
||||||
Payload: []byte{1, 2, 3, 4},
|
Payload: []byte{1, 2, 3, 4},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -882,7 +882,7 @@ func TestReadTracks(t *testing.T) {
|
|||||||
Codec: message.CodecLPCM,
|
Codec: message.CodecLPCM,
|
||||||
Rate: message.Rate44100,
|
Rate: message.Rate44100,
|
||||||
Depth: message.Depth16,
|
Depth: message.Depth16,
|
||||||
Channels: message.ChannelsStereo,
|
IsStereo: true,
|
||||||
Payload: []byte{1, 2, 3, 4},
|
Payload: []byte{1, 2, 3, 4},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -39,11 +39,8 @@ func audioRateIntToRTMP(v int) uint8 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func mpeg1AudioChannels(m mpeg1audio.ChannelMode) uint8 {
|
func mpeg1AudioChannels(m mpeg1audio.ChannelMode) bool {
|
||||||
if m == mpeg1audio.ChannelModeMono {
|
return m != mpeg1audio.ChannelModeMono
|
||||||
return message.ChannelsMono
|
|
||||||
}
|
|
||||||
return message.ChannelsStereo
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Writer is a wrapper around Conn that provides utilities to mux outgoing data.
|
// 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,
|
Codec: message.CodecMPEG4Audio,
|
||||||
Rate: message.Rate44100,
|
Rate: message.Rate44100,
|
||||||
Depth: message.Depth16,
|
Depth: message.Depth16,
|
||||||
Channels: message.ChannelsStereo,
|
IsStereo: true,
|
||||||
AACType: message.AudioAACTypeConfig,
|
AACType: message.AudioAACTypeConfig,
|
||||||
Payload: enc,
|
Payload: enc,
|
||||||
})
|
})
|
||||||
@@ -195,7 +192,7 @@ func (w *Writer) WriteMPEG4Audio(pts time.Duration, au []byte) error {
|
|||||||
Codec: message.CodecMPEG4Audio,
|
Codec: message.CodecMPEG4Audio,
|
||||||
Rate: message.Rate44100,
|
Rate: message.Rate44100,
|
||||||
Depth: message.Depth16,
|
Depth: message.Depth16,
|
||||||
Channels: message.ChannelsStereo,
|
IsStereo: true,
|
||||||
AACType: message.AudioAACTypeAU,
|
AACType: message.AudioAACTypeAU,
|
||||||
Payload: au,
|
Payload: au,
|
||||||
DTS: pts,
|
DTS: pts,
|
||||||
@@ -210,7 +207,7 @@ func (w *Writer) WriteMPEG1Audio(pts time.Duration, h *mpeg1audio.FrameHeader, f
|
|||||||
Codec: message.CodecMPEG1Audio,
|
Codec: message.CodecMPEG1Audio,
|
||||||
Rate: audioRateIntToRTMP(h.SampleRate),
|
Rate: audioRateIntToRTMP(h.SampleRate),
|
||||||
Depth: message.Depth16,
|
Depth: message.Depth16,
|
||||||
Channels: mpeg1AudioChannels(h.ChannelMode),
|
IsStereo: mpeg1AudioChannels(h.ChannelMode),
|
||||||
Payload: frame,
|
Payload: frame,
|
||||||
DTS: pts,
|
DTS: pts,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -91,7 +91,7 @@ func TestWriteTracks(t *testing.T) {
|
|||||||
Codec: message.CodecMPEG4Audio,
|
Codec: message.CodecMPEG4Audio,
|
||||||
Rate: message.Rate44100,
|
Rate: message.Rate44100,
|
||||||
Depth: message.Depth16,
|
Depth: message.Depth16,
|
||||||
Channels: message.ChannelsStereo,
|
IsStereo: true,
|
||||||
AACType: message.AudioAACTypeConfig,
|
AACType: message.AudioAACTypeConfig,
|
||||||
Payload: []byte{0x12, 0x10},
|
Payload: []byte{0x12, 0x10},
|
||||||
}, msg)
|
}, msg)
|
||||||
|
|||||||
Reference in New Issue
Block a user