diff --git a/examples/client-tcp.go b/examples/client-tcp.go index 75d7156e..ca3f832e 100644 --- a/examples/client-tcp.go +++ b/examples/client-tcp.go @@ -58,7 +58,7 @@ func main() { panic(err) } - trackId, streamType := gortsplib.ConvChannelToTrackIdAndStreamType(frame.Channel) - fmt.Printf("packet from track %d, type %v: %v\n", trackId, streamType, frame.Content) + fmt.Printf("packet from track %d, type %v: %v\n", + frame.TrackId, frame.StreamType, frame.Content) } } diff --git a/go.mod b/go.mod index c96e2489..03eddcf8 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,8 @@ module github.com/aler9/gortsplib go 1.13 require ( + github.com/pion/rtcp v1.2.3 github.com/pion/sdp v1.3.0 github.com/pkg/errors v0.9.1 // indirect - github.com/stretchr/testify v1.4.0 + github.com/stretchr/testify v1.5.1 ) diff --git a/go.sum b/go.sum index 7a305f4e..75502cf5 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pion/rtcp v1.2.3 h1:2wrhKnqgSz91Q5nzYTO07mQXztYPtxL8a0XOss4rJqA= +github.com/pion/rtcp v1.2.3/go.mod h1:zGhIv0RPRF0Z1Wiij22pUt5W/c9fevqSzT4jje/oK7I= github.com/pion/sdp v1.3.0 h1:21lpgEILHyolpsIrbCBagZaAPj4o057cFjzaFebkVOs= github.com/pion/sdp v1.3.0/go.mod h1:ceA2lTyftydQTuCIbUNoH77aAt6CiQJaRpssA4Gee8I= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= @@ -7,8 +9,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= -github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= diff --git a/interleaved-frame.go b/interleaved-frame.go index e31d3c7d..45adab1c 100644 --- a/interleaved-frame.go +++ b/interleaved-frame.go @@ -22,26 +22,27 @@ const ( StreamTypeRtcp ) -// ConvChannelToTrackIdAndStreamType converts a channel into a track id and a streamType. -func ConvChannelToTrackIdAndStreamType(channel uint8) (int, StreamType) { - if (channel % 2) == 0 { - return int(channel / 2), StreamTypeRtp +func (st StreamType) String() string { + switch st { + case StreamTypeRtp: + return "RTP" + + case StreamTypeRtcp: + return "RTCP" } - return int((channel - 1) / 2), StreamTypeRtcp + return "UNKNOWN" } -// ConvTrackIdAndStreamTypeToChannel converts a track id and a streamType into a channel. -func ConvTrackIdAndStreamTypeToChannel(trackId int, StreamType StreamType) uint8 { - if StreamType == StreamTypeRtp { - return uint8(trackId * 2) - } - return uint8((trackId * 2) + 1) -} - -// InterleavedFrame is a structure that allows to send and receive binary data -// within RTSP connections. It is usually deployed to send RTP and RTCP packets via TCP. +// InterleavedFrame is an object that allows to send and receive binary data +// within RTSP connections. It is used to send RTP and RTCP packets via TCP. type InterleavedFrame struct { - Channel uint8 + // track id + TrackId int + + // stream type + StreamType StreamType + + // frame content Content []byte } @@ -62,19 +63,34 @@ func (f *InterleavedFrame) read(r io.Reader) error { framelen, len(f.Content)) } - f.Channel = header[1] + // convert channel into TrackId and StreamType + channel := header[1] + f.TrackId, f.StreamType = func() (int, StreamType) { + if (channel % 2) == 0 { + return int(channel / 2), StreamTypeRtp + } + return int((channel - 1) / 2), StreamTypeRtcp + }() + f.Content = f.Content[:framelen] _, err = io.ReadFull(r, f.Content) if err != nil { return err } - return nil } func (f *InterleavedFrame) write(bw *bufio.Writer) error { - _, err := bw.Write([]byte{0x24, f.Channel}) + // convert TrackId and StreamType into channel + channel := func() uint8 { + if f.StreamType == StreamTypeRtp { + return uint8(f.TrackId * 2) + } + return uint8((f.TrackId * 2) + 1) + }() + + _, err := bw.Write([]byte{0x24, channel}) if err != nil { return err }