forked from bluenviron/gortsplib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_session_format.go
92 lines (81 loc) · 2.16 KB
/
server_session_format.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package gortsplib
import (
"fmt"
"time"
"github.com/pion/rtcp"
"github.com/pion/rtp"
"github.com/bluenviron/gortsplib/v3/pkg/formats"
"github.com/bluenviron/gortsplib/v3/pkg/rtcpreceiver"
"github.com/bluenviron/gortsplib/v3/pkg/rtplossdetector"
"github.com/bluenviron/gortsplib/v3/pkg/rtpreorderer"
)
type serverSessionFormat struct {
sm *serverSessionMedia
format formats.Format
udpReorderer *rtpreorderer.Reorderer
tcpLossDetector *rtplossdetector.LossDetector
udpRTCPReceiver *rtcpreceiver.RTCPReceiver
onPacketRTP func(*rtp.Packet)
}
func newServerSessionFormat(sm *serverSessionMedia, forma formats.Format) *serverSessionFormat {
return &serverSessionFormat{
sm: sm,
format: forma,
onPacketRTP: func(*rtp.Packet) {},
}
}
func (sf *serverSessionFormat) start() {
if sf.sm.ss.state != ServerSessionStatePlay {
if *sf.sm.ss.setuppedTransport == TransportUDP || *sf.sm.ss.setuppedTransport == TransportUDPMulticast {
sf.udpReorderer = rtpreorderer.New()
sf.udpRTCPReceiver = rtcpreceiver.New(
sf.sm.ss.s.udpReceiverReportPeriod,
nil,
sf.format.ClockRate(),
func(pkt rtcp.Packet) {
sf.sm.ss.WritePacketRTCP(sf.sm.media, pkt)
})
} else {
sf.tcpLossDetector = rtplossdetector.New()
}
}
}
func (sf *serverSessionFormat) stop() {
if sf.udpRTCPReceiver != nil {
sf.udpRTCPReceiver.Close()
sf.udpRTCPReceiver = nil
}
}
func (sf *serverSessionFormat) readRTPUDP(pkt *rtp.Packet, now time.Time) {
packets, lost := sf.udpReorderer.Process(pkt)
if lost != 0 {
sf.sm.ss.onPacketLost(fmt.Errorf("%d RTP %s lost",
lost,
func() string {
if lost == 1 {
return "packet"
}
return "packets"
}()))
// do not return
}
for _, pkt := range packets {
sf.udpRTCPReceiver.ProcessPacket(pkt, now, sf.format.PTSEqualsDTS(pkt))
sf.onPacketRTP(pkt)
}
}
func (sf *serverSessionFormat) readRTPTCP(pkt *rtp.Packet) {
lost := sf.tcpLossDetector.Process(pkt)
if lost != 0 {
sf.sm.ss.onPacketLost(fmt.Errorf("%d RTP %s lost",
lost,
func() string {
if lost == 1 {
return "packet"
}
return "packets"
}()))
// do not return
}
sf.onPacketRTP(pkt)
}