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 Jul 2, 2022
1 parent 45c1370 commit d5c5a43
Show file tree
Hide file tree
Showing 2 changed files with 66 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 @@ -124,8 +124,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 @@ -151,7 +158,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 @@ -179,7 +186,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
55 changes: 55 additions & 0 deletions internal/rtmp/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func TestReadTracks(t *testing.T) {
"standard",
"metadata without codec id",
"missing metadata",
"null metadata",
} {
t.Run(ca, func(t *testing.T) {
ln, err := net.Listen("tcp", "127.0.0.1:9121")
Expand Down Expand Up @@ -140,6 +141,15 @@ func TestReadTracks(t *testing.T) {
IndexLength: 3,
IndexDeltaLength: 3,
}, audioTrack)

case "null metadata":
require.Equal(t, &gortsplib.TrackH264{
PayloadType: 96,
SPS: sps,
PPS: pps,
}, videoTrack)

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

close(done)
Expand Down Expand Up @@ -501,6 +511,51 @@ func TestReadTracks(t *testing.T) {
Payload: enc,
})
require.NoError(t, err)

case "null metadata":
// C->S metadata
err = mrw.Write(&message.MsgDataAMF0{
ChunkStreamID: 4,
MessageStreamID: 1,
Payload: []interface{}{
"@setDataFrame",
"onMetaData",
nil,
},
})
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)
err = mrw.Write(&message.MsgVideo{
ChunkStreamID: 6,
MessageStreamID: 1,
IsKeyFrame: true,
H264Type: flvio.AVC_SEQHDR,
Payload: b[:n],
})
require.NoError(t, err)

// C->S H264 NALU
err = mrw.Write(&message.MsgVideo{
ChunkStreamID: 6,
MessageStreamID: 1,
IsKeyFrame: true,
H264Type: flvio.AVC_NALU,
Payload: []byte{0x01, 0x02, 0x03, 0x04},
DTS: 1000,
})
require.NoError(t, err)
}

<-done
Expand Down

0 comments on commit d5c5a43

Please sign in to comment.