Skip to content

Commit ee85ff2

Browse files
committed
rtp: reorder packets before ingestion
1 parent 2d02ca0 commit ee85ff2

File tree

3 files changed

+75
-11
lines changed

3 files changed

+75
-11
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package rtp
2+
3+
import (
4+
"time"
5+
6+
"github.com/bluenviron/gortsplib/v5/pkg/format"
7+
"github.com/bluenviron/gortsplib/v5/pkg/rtpreceiver"
8+
"github.com/pion/rtcp"
9+
)
10+
11+
type rtpFormat struct {
12+
desc format.Format
13+
14+
rtpReceiver *rtpreceiver.Receiver
15+
}
16+
17+
func (f *rtpFormat) initialize() {
18+
f.rtpReceiver = &rtpreceiver.Receiver{
19+
ClockRate: f.desc.ClockRate(),
20+
UnrealiableTransport: true,
21+
Period: 10 * time.Second,
22+
WritePacketRTCP: func(_ rtcp.Packet) {
23+
},
24+
}
25+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package rtp
2+
3+
import "github.com/bluenviron/gortsplib/v5/pkg/description"
4+
5+
type rtpMedia struct {
6+
desc *description.Media
7+
}

internal/staticsources/rtp/source.go

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"time"
99

1010
"github.com/bluenviron/gortsplib/v5/pkg/description"
11-
"github.com/bluenviron/gortsplib/v5/pkg/format"
1211
"github.com/bluenviron/gortsplib/v5/pkg/rtptime"
1312
"github.com/bluenviron/gortsplib/v5/pkg/sdp"
1413
"github.com/bluenviron/mediamtx/internal/conf"
@@ -103,6 +102,22 @@ func (s *Source) Run(params defs.StaticSourceRunParams) error {
103102
}
104103

105104
func (s *Source) runReader(desc *description.Session, nc net.Conn) error {
105+
packetsLost := &counterdumper.CounterDumper{
106+
OnReport: func(val uint64) {
107+
s.Log(logger.Warn, "%d RTP %s lost",
108+
val,
109+
func() string {
110+
if val == 1 {
111+
return "packet"
112+
}
113+
return "packets"
114+
}())
115+
},
116+
}
117+
118+
packetsLost.Start()
119+
defer packetsLost.Stop()
120+
106121
decodeErrors := &counterdumper.CounterDumper{
107122
OnReport: func(val uint64) {
108123
s.Log(logger.Warn, "%d decode %s",
@@ -123,13 +138,22 @@ func (s *Source) runReader(desc *description.Session, nc net.Conn) error {
123138
timeDecoder := &rtptime.GlobalDecoder{}
124139
timeDecoder.Initialize()
125140

126-
mediasByPayloadType := make(map[uint8]*description.Media)
127-
formatsByPayloadType := make(map[uint8]format.Format)
141+
mediasByPayloadType := make(map[uint8]*rtpMedia)
142+
formatsByPayloadType := make(map[uint8]*rtpFormat)
128143

129-
for _, media := range desc.Medias {
130-
for _, forma := range media.Formats {
131-
mediasByPayloadType[forma.PayloadType()] = media
132-
formatsByPayloadType[forma.PayloadType()] = forma
144+
for _, descMedia := range desc.Medias {
145+
rtpMedia := &rtpMedia{
146+
desc: descMedia,
147+
}
148+
149+
for _, descFormat := range descMedia.Formats {
150+
rtpFormat := &rtpFormat{
151+
desc: descFormat,
152+
}
153+
rtpFormat.initialize()
154+
155+
mediasByPayloadType[descFormat.PayloadType()] = rtpMedia
156+
formatsByPayloadType[descFormat.PayloadType()] = rtpFormat
133157
}
134158
}
135159

@@ -173,12 +197,20 @@ func (s *Source) runReader(desc *description.Session, nc net.Conn) error {
173197

174198
forma := formatsByPayloadType[pkt.PayloadType]
175199

176-
pts, ok := timeDecoder.Decode(forma, &pkt)
177-
if !ok {
178-
continue
200+
pkts, lost := forma.rtpReceiver.ProcessPacket2(&pkt, time.Now(), forma.desc.PTSEqualsDTS(&pkt))
201+
202+
if lost != 0 {
203+
packetsLost.Add(lost)
179204
}
180205

181-
stream.WriteRTPPacket(media, forma, &pkt, time.Time{}, pts)
206+
for _, pkt := range pkts {
207+
pts, ok2 := timeDecoder.Decode(forma.desc, pkt)
208+
if !ok2 {
209+
continue
210+
}
211+
212+
stream.WriteRTPPacket(media.desc, forma.desc, pkt, time.Time{}, pts)
213+
}
182214
}
183215
}
184216

0 commit comments

Comments
 (0)