insert TrackId and StreamType directly into InterleavedFrame

This commit is contained in:
aler9
2020-07-13 08:35:54 +02:00
parent df2da058e0
commit d675bad299
4 changed files with 43 additions and 24 deletions

View File

@@ -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)
}
}

3
go.mod
View File

@@ -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
)

6
go.sum
View File

@@ -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=

View File

@@ -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
}