diff --git a/pkg/dvrip/client.go b/pkg/dvrip/client.go index 9efb4934..77b060f4 100644 --- a/pkg/dvrip/client.go +++ b/pkg/dvrip/client.go @@ -5,7 +5,6 @@ import ( "crypto/md5" "encoding/base64" "encoding/binary" - "encoding/hex" "encoding/json" "errors" "fmt" @@ -336,25 +335,7 @@ func (c *Client) AddVideoTrack(mediaCode byte, payload []byte) { Name: streamer.CodecH264, ClockRate: 90000, PayloadType: streamer.PayloadTypeRAW, - FmtpLine: "packetization-mode=1", - } - - for { - size := 4 + int(binary.BigEndian.Uint32(payload)) - - switch h264.NALUType(payload) { - case h264.NALUTypeSPS: - codec.FmtpLine += ";profile-level-id=" + hex.EncodeToString(payload[5:8]) - codec.FmtpLine += ";sprop-parameter-sets=" + base64.StdEncoding.EncodeToString(payload[4:size]) - case h264.NALUTypePPS: - codec.FmtpLine += "," + base64.StdEncoding.EncodeToString(payload[4:size]) - } - - if size < len(payload) { - payload = payload[size:] - } else { - break - } + FmtpLine: h264.GetFmtpLine(payload), } case 0x03, 0x13: diff --git a/pkg/h264/helper.go b/pkg/h264/helper.go index 8aa6f92d..3fa012ec 100644 --- a/pkg/h264/helper.go +++ b/pkg/h264/helper.go @@ -104,3 +104,26 @@ func GetParameterSet(fmtp string) (sps, pps []byte) { return } + +// GetFmtpLine from SPS+PPS+IFrame in AVC format +func GetFmtpLine(avc []byte) string { + s := "packetization-mode=1" + + for { + size := 4 + int(binary.BigEndian.Uint32(avc)) + + switch NALUType(avc) { + case NALUTypeSPS: + s += ";profile-level-id=" + hex.EncodeToString(avc[5:8]) + s += ";sprop-parameter-sets=" + base64.StdEncoding.EncodeToString(avc[4:size]) + case NALUTypePPS: + s += "," + base64.StdEncoding.EncodeToString(avc[4:size]) + } + + if size < len(avc) { + avc = avc[size:] + } else { + return s + } + } +}