Skip to content

Commit

Permalink
rtmp: fix panic when publishing audio-only streams (#1459) (#1502)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 authored Feb 22, 2023
1 parent 18a184c commit c7938eb
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
6 changes: 3 additions & 3 deletions internal/rtmp/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ func (c *Conn) WriteMessage(msg message.Message) error {
return c.mrw.Write(msg)
}

func trackFromH264DecoderConfig(data []byte) (*format.H264, error) {
func trackFromH264DecoderConfig(data []byte) (format.Format, error) {
var conf h264conf.Conf
err := conf.Unmarshal(data)
if err != nil {
Expand Down Expand Up @@ -766,9 +766,9 @@ func (c *Conn) readTracksFromMetadata(payload []interface{}) (format.Format, *fo
}
}

func (c *Conn) readTracksFromMessages(msg message.Message) (*format.H264, *format.MPEG4Audio, error) {
func (c *Conn) readTracksFromMessages(msg message.Message) (format.Format, *format.MPEG4Audio, error) {
var startTime *time.Duration
var videoTrack *format.H264
var videoTrack format.Format
var audioTrack *format.MPEG4Audio

// analyze 1 second of packets
Expand Down
59 changes: 57 additions & 2 deletions internal/rtmp/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net"
"net/url"
"testing"
"time"

"github.com/aler9/gortsplib/v2/pkg/codecs/h264"
"github.com/aler9/gortsplib/v2/pkg/codecs/mpeg4audio"
Expand Down Expand Up @@ -537,7 +538,7 @@ func TestReadTracks(t *testing.T) {
},
},
{
"missing metadata",
"missing metadata, video+audio",
&format.H264{
PayloadTyp: 96,
SPS: sps,
Expand All @@ -556,6 +557,21 @@ func TestReadTracks(t *testing.T) {
IndexDeltaLength: 3,
},
},
{
"missing metadata, audio",
nil,
&format.MPEG4Audio{
PayloadTyp: 96,
Config: &mpeg4audio.Config{
Type: 2,
SampleRate: 44100,
ChannelCount: 2,
},
SizeLength: 13,
IndexLength: 3,
IndexDeltaLength: 3,
},
},
{
"obs studio h265",
&format.H265{
Expand Down Expand Up @@ -796,6 +812,7 @@ func TestReadTracks(t *testing.T) {
SPS: sps,
PPS: pps,
}.Marshal()

err = mrw.Write(&message.MsgVideo{
ChunkStreamID: message.MsgVideoChunkStreamID,
MessageStreamID: 0x1000000,
Expand All @@ -811,6 +828,7 @@ func TestReadTracks(t *testing.T) {
ChannelCount: 2,
}.Marshal()
require.NoError(t, err)

err = mrw.Write(&message.MsgAudio{
ChunkStreamID: message.MsgAudioChunkStreamID,
MessageStreamID: 0x1000000,
Expand Down Expand Up @@ -855,6 +873,7 @@ func TestReadTracks(t *testing.T) {
SPS: sps,
PPS: pps,
}.Marshal()

err = mrw.Write(&message.MsgVideo{
ChunkStreamID: message.MsgVideoChunkStreamID,
MessageStreamID: 0x1000000,
Expand Down Expand Up @@ -893,6 +912,7 @@ func TestReadTracks(t *testing.T) {
SPS: sps,
PPS: pps,
}.Marshal()

err = mrw.Write(&message.MsgVideo{
ChunkStreamID: message.MsgVideoChunkStreamID,
MessageStreamID: 0x1000000,
Expand All @@ -908,6 +928,7 @@ func TestReadTracks(t *testing.T) {
ChannelCount: 2,
}.Marshal()
require.NoError(t, err)

err = mrw.Write(&message.MsgAudio{
ChunkStreamID: message.MsgAudioChunkStreamID,
MessageStreamID: 0x1000000,
Expand All @@ -919,11 +940,12 @@ func TestReadTracks(t *testing.T) {
})
require.NoError(t, err)

case "missing metadata":
case "missing metadata, video+audio":
buf, _ := h264conf.Conf{
SPS: sps,
PPS: pps,
}.Marshal()

err = mrw.Write(&message.MsgVideo{
ChunkStreamID: message.MsgVideoChunkStreamID,
MessageStreamID: 0x1000000,
Expand All @@ -939,6 +961,7 @@ func TestReadTracks(t *testing.T) {
ChannelCount: 2,
}.Marshal()
require.NoError(t, err)

err = mrw.Write(&message.MsgAudio{
ChunkStreamID: message.MsgAudioChunkStreamID,
MessageStreamID: 0x1000000,
Expand All @@ -950,6 +973,37 @@ func TestReadTracks(t *testing.T) {
})
require.NoError(t, err)

case "missing metadata, audio":
enc, err := mpeg4audio.Config{
Type: 2,
SampleRate: 44100,
ChannelCount: 2,
}.Marshal()
require.NoError(t, err)

err = mrw.Write(&message.MsgAudio{
ChunkStreamID: message.MsgAudioChunkStreamID,
MessageStreamID: 0x1000000,
Rate: flvio.SOUND_44Khz,
Depth: flvio.SOUND_16BIT,
Channels: flvio.SOUND_STEREO,
AACType: flvio.AAC_SEQHDR,
Payload: enc,
})
require.NoError(t, err)

err = mrw.Write(&message.MsgAudio{
ChunkStreamID: message.MsgAudioChunkStreamID,
MessageStreamID: 0x1000000,
Rate: flvio.SOUND_44Khz,
Depth: flvio.SOUND_16BIT,
Channels: flvio.SOUND_STEREO,
AACType: flvio.AAC_SEQHDR,
Payload: enc,
DTS: 1 * time.Second,
})
require.NoError(t, err)

case "obs studio h265":
err = mrw.Write(&message.MsgDataAMF0{
ChunkStreamID: 4,
Expand Down Expand Up @@ -1017,6 +1071,7 @@ func TestReadTracks(t *testing.T) {
ChannelCount: 2,
}.Marshal()
require.NoError(t, err)

err = mrw.Write(&message.MsgAudio{
ChunkStreamID: message.MsgAudioChunkStreamID,
MessageStreamID: 0x1000000,
Expand Down

0 comments on commit c7938eb

Please sign in to comment.