-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy pathserver_session_format.go
151 lines (126 loc) · 4 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package gortsplib
import (
"log"
"sync/atomic"
"time"
"github.com/pion/rtcp"
"github.com/pion/rtp"
"github.com/bluenviron/gortsplib/v4/internal/rtcpreceiver"
"github.com/bluenviron/gortsplib/v4/internal/rtplossdetector"
"github.com/bluenviron/gortsplib/v4/internal/rtpreorderer"
"github.com/bluenviron/gortsplib/v4/pkg/format"
"github.com/bluenviron/gortsplib/v4/pkg/liberrors"
)
type serverSessionFormat struct {
sm *serverSessionMedia
format format.Format
onPacketRTP OnPacketRTPFunc
udpReorderer *rtpreorderer.Reorderer
tcpLossDetector *rtplossdetector.LossDetector
rtcpReceiver *rtcpreceiver.RTCPReceiver
writePacketRTPInQueue func([]byte) error
rtpPacketsReceived *uint64
rtpPacketsSent *uint64
rtpPacketsLost *uint64
}
func (sf *serverSessionFormat) initialize() {
sf.rtpPacketsReceived = new(uint64)
sf.rtpPacketsSent = new(uint64)
sf.rtpPacketsLost = new(uint64)
}
func (sf *serverSessionFormat) start() {
switch *sf.sm.ss.setuppedTransport {
case TransportUDP, TransportUDPMulticast:
sf.writePacketRTPInQueue = sf.writePacketRTPInQueueUDP
default:
sf.writePacketRTPInQueue = sf.writePacketRTPInQueueTCP
}
if sf.sm.ss.state != ServerSessionStatePlay {
if *sf.sm.ss.setuppedTransport == TransportUDP || *sf.sm.ss.setuppedTransport == TransportUDPMulticast {
sf.udpReorderer = &rtpreorderer.Reorderer{}
sf.udpReorderer.Initialize()
} else {
sf.tcpLossDetector = &rtplossdetector.LossDetector{}
}
sf.rtcpReceiver = &rtcpreceiver.RTCPReceiver{
ClockRate: sf.format.ClockRate(),
Period: sf.sm.ss.s.receiverReportPeriod,
TimeNow: sf.sm.ss.s.timeNow,
WritePacketRTCP: func(pkt rtcp.Packet) {
if *sf.sm.ss.setuppedTransport == TransportUDP || *sf.sm.ss.setuppedTransport == TransportUDPMulticast {
sf.sm.ss.WritePacketRTCP(sf.sm.media, pkt) //nolint:errcheck
}
},
}
err := sf.rtcpReceiver.Initialize()
if err != nil {
panic(err)
}
}
}
func (sf *serverSessionFormat) stop() {
if sf.rtcpReceiver != nil {
sf.rtcpReceiver.Close()
sf.rtcpReceiver = nil
}
}
func (sf *serverSessionFormat) readPacketRTPUDP(pkt *rtp.Packet, now time.Time) {
packets, lost := sf.udpReorderer.Process(pkt)
if lost != 0 {
sf.onPacketRTPLost(lost)
// do not return
}
for _, pkt := range packets {
sf.handlePacketRTP(pkt, now)
}
}
func (sf *serverSessionFormat) readPacketRTPTCP(pkt *rtp.Packet) {
lost := sf.tcpLossDetector.Process(pkt)
if lost != 0 {
sf.onPacketRTPLost(lost)
// do not return
}
now := sf.sm.ss.s.timeNow()
sf.handlePacketRTP(pkt, now)
}
func (sf *serverSessionFormat) handlePacketRTP(pkt *rtp.Packet, now time.Time) {
err := sf.rtcpReceiver.ProcessPacketRTP(pkt, now, sf.format.PTSEqualsDTS(pkt))
if err != nil {
sf.sm.onPacketRTPDecodeError(err)
return
}
atomic.AddUint64(sf.rtpPacketsReceived, 1)
sf.onPacketRTP(pkt)
}
func (sf *serverSessionFormat) onPacketRTPLost(lost uint) {
atomic.AddUint64(sf.rtpPacketsLost, uint64(lost))
if h, ok := sf.sm.ss.s.Handler.(ServerHandlerOnPacketLost); ok {
h.OnPacketLost(&ServerHandlerOnPacketLostCtx{
Session: sf.sm.ss,
Error: liberrors.ErrServerRTPPacketsLost{Lost: lost},
})
} else {
log.Println(liberrors.ErrServerRTPPacketsLost{Lost: lost}.Error())
}
}
func (sf *serverSessionFormat) writePacketRTPInQueueUDP(payload []byte) error {
err := sf.sm.ss.s.udpRTPListener.write(payload, sf.sm.udpRTPWriteAddr)
if err != nil {
return err
}
atomic.AddUint64(sf.sm.bytesSent, uint64(len(payload)))
atomic.AddUint64(sf.rtpPacketsSent, 1)
return nil
}
func (sf *serverSessionFormat) writePacketRTPInQueueTCP(payload []byte) error {
sf.sm.ss.tcpFrame.Channel = sf.sm.tcpChannel
sf.sm.ss.tcpFrame.Payload = payload
sf.sm.ss.tcpConn.nconn.SetWriteDeadline(time.Now().Add(sf.sm.ss.s.WriteTimeout))
err := sf.sm.ss.tcpConn.conn.WriteInterleavedFrame(sf.sm.ss.tcpFrame, sf.sm.ss.tcpBuffer)
if err != nil {
return err
}
atomic.AddUint64(sf.sm.bytesSent, uint64(len(payload)))
atomic.AddUint64(sf.rtpPacketsSent, 1)
return nil
}