Skip to content

Commit

Permalink
fix RTMP publishing with Dahua cameras (#769)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Apr 18, 2022
1 parent d5a1b8c commit 56cb725
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
15 changes: 11 additions & 4 deletions internal/rtmp/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,15 @@ func (c *Conn) readTracksFromMetadata(pkt av.Packet) (*gortsplib.TrackH264, *gor
return nil, nil, fmt.Errorf("invalid metadata")
}

md, ok := arr[0].(flvio.AMFMap)
if !ok {
var md flvio.AMFMap
switch tt := arr[0].(type) {
case flvio.AMFMap:
md = tt

case nil:
return nil, nil, errEmptyMetadata

default:
return nil, nil, fmt.Errorf("invalid metadata")
}

Expand All @@ -130,7 +137,7 @@ func (c *Conn) readTracksFromMetadata(pkt av.Packet) (*gortsplib.TrackH264, *gor
}
}

return false, fmt.Errorf("unsupported video codec %v", v)
return false, fmt.Errorf("unsupported video codec: %v", v)
}()
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -158,7 +165,7 @@ func (c *Conn) readTracksFromMetadata(pkt av.Packet) (*gortsplib.TrackH264, *gor
}
}

return false, fmt.Errorf("unsupported audio codec %v", v)
return false, fmt.Errorf("unsupported audio codec: %v", v)
}()
if err != nil {
return nil, nil, err
Expand Down
46 changes: 46 additions & 0 deletions internal/rtmp/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ func TestReadTracks(t *testing.T) {
for _, ca := range []string{
"standard",
"metadata without codec id",
"null metadata",
"no metadata",
} {
t.Run(ca, func(t *testing.T) {
Expand Down Expand Up @@ -289,6 +290,13 @@ func TestReadTracks(t *testing.T) {

require.Equal(t, (*gortsplib.TrackAAC)(nil), audioTrack)

case "null metadata":
videoTrack2, err := gortsplib.NewTrackH264(96, sps, pps, nil)
require.NoError(t, err)
require.Equal(t, videoTrack2, videoTrack)

require.Equal(t, (*gortsplib.TrackAAC)(nil), audioTrack)

case "no metadata":
videoTrack2, err := gortsplib.NewTrackH264(96, sps, pps, nil)
require.NoError(t, err)
Expand Down Expand Up @@ -619,6 +627,44 @@ func TestReadTracks(t *testing.T) {
}.write(conn)
require.NoError(t, err)

case "null metadata":
// C->S metadata
byts = flvio.FillAMF0ValsMalloc([]interface{}{
"@setDataFrame",
"onMetaData",
nil,
})
err = chunk0{
chunkStreamID: 4,
typ: 0x12,
streamID: 1,
bodyLen: uint32(len(byts)),
body: byts,
}.write(conn)
require.NoError(t, err)

// C->S H264 decoder config
codec := nh264.Codec{
SPS: map[int][]byte{
0: sps,
},
PPS: map[int][]byte{
0: pps,
},
}
b := make([]byte, 128)
var n int
codec.ToConfig(b, &n)
body := append([]byte{flvio.FRAME_KEY<<4 | flvio.VIDEO_H264, 0, 0, 0, 0}, b[:n]...)
err = chunk0{
chunkStreamID: 6,
typ: flvio.TAG_VIDEO,
streamID: 1,
bodyLen: uint32(len(body)),
body: body,
}.write(conn)
require.NoError(t, err)

case "no metadata":
// C->S H264 decoder config
codec := nh264.Codec{
Expand Down

0 comments on commit 56cb725

Please sign in to comment.