forked from pion/webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtcrtpreceiver.go
184 lines (152 loc) · 3.99 KB
/
rtcrtpreceiver.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package webrtc
import (
"fmt"
"sync"
"github.com/pions/rtcp"
"github.com/pions/rtp"
"github.com/pions/srtp"
)
// RTCRtpReceiver allows an application to inspect the receipt of a RTCTrack
type RTCRtpReceiver struct {
kind RTCRtpCodecType
transport *RTCDtlsTransport
hasRecv chan bool
Track *RTCTrack
closed bool
mu sync.Mutex
rtpOut chan *rtp.Packet
rtpReadStream *srtp.ReadStreamSRTP
rtpOutDone chan bool
rtcpOut chan rtcp.Packet
rtcpReadStream *srtp.ReadStreamSRTCP
rtcpOutDone chan bool
}
// NewRTCRtpReceiver constructs a new RTCRtpReceiver
func NewRTCRtpReceiver(kind RTCRtpCodecType, transport *RTCDtlsTransport) *RTCRtpReceiver {
return &RTCRtpReceiver{
kind: kind,
transport: transport,
rtpOut: make(chan *rtp.Packet, 15),
rtpOutDone: make(chan bool),
rtcpOut: make(chan rtcp.Packet, 15),
rtcpOutDone: make(chan bool),
hasRecv: make(chan bool),
}
}
// Receive blocks until the RTCTrack is available
func (r *RTCRtpReceiver) Receive(parameters RTCRtpReceiveParameters) chan bool {
// TODO atomic only allow this to fire once
r.Track = &RTCTrack{
Kind: r.kind,
Ssrc: parameters.encodings.SSRC,
Packets: r.rtpOut,
RTCPPackets: r.rtcpOut,
}
// RTP ReadLoop
go func() {
payloadSet := false
defer func() {
if !payloadSet {
close(r.hasRecv)
}
close(r.rtpOut)
close(r.rtpOutDone)
}()
srtpSession, err := r.transport.getSRTPSession()
if err != nil {
pcLog.Warnf("Failed to open SRTPSession, RTCTrack done for: %v %d \n", err, parameters.encodings.SSRC)
return
}
readStream, err := srtpSession.OpenReadStream(parameters.encodings.SSRC)
if err != nil {
pcLog.Warnf("Failed to open RTCP ReadStream, RTCTrack done for: %v %d \n", err, parameters.encodings.SSRC)
return
}
r.mu.Lock()
r.rtpReadStream = readStream
r.mu.Unlock()
readBuf := make([]byte, receiveMTU)
for {
rtpLen, err := readStream.Read(readBuf)
if err != nil {
pcLog.Warnf("Failed to read, RTCTrack done for: %v %d \n", err, parameters.encodings.SSRC)
return
}
var rtpPacket rtp.Packet
if err = rtpPacket.Unmarshal(append([]byte{}, readBuf[:rtpLen]...)); err != nil {
pcLog.Warnf("Failed to unmarshal RTP packet, discarding: %v \n", err)
continue
}
if !payloadSet {
r.Track.PayloadType = rtpPacket.PayloadType
payloadSet = true
close(r.hasRecv)
}
select {
case r.rtpOut <- &rtpPacket:
default:
}
}
}()
// RTCP ReadLoop
go func() {
defer func() {
close(r.rtcpOut)
close(r.rtcpOutDone)
}()
srtcpSession, err := r.transport.getSRTCPSession()
if err != nil {
pcLog.Warnf("Failed to open SRTCPSession, RTCTrack done for: %v %d \n", err, parameters.encodings.SSRC)
return
}
readStream, err := srtcpSession.OpenReadStream(parameters.encodings.SSRC)
if err != nil {
pcLog.Warnf("Failed to open RTCP ReadStream, RTCTrack done for: %v %d \n", err, parameters.encodings.SSRC)
return
}
r.mu.Lock()
r.rtcpReadStream = readStream
r.mu.Unlock()
readBuf := make([]byte, receiveMTU)
for {
rtcpLen, err := readStream.Read(readBuf)
if err != nil {
pcLog.Warnf("Failed to read, RTCTrack done for: %v %d \n", err, parameters.encodings.SSRC)
return
}
rtcpPacket, _, err := rtcp.Unmarshal(append([]byte{}, readBuf[:rtcpLen]...))
if err != nil {
pcLog.Warnf("Failed to unmarshal RTCP packet, discarding: %v \n", err)
continue
}
select {
case r.rtcpOut <- rtcpPacket:
default:
}
}
}()
return r.hasRecv
}
// Stop irreversibly stops the RTCRtpReceiver
func (r *RTCRtpReceiver) Stop() error {
r.mu.Lock()
defer r.mu.Unlock()
if r.closed {
return fmt.Errorf("RTCRtpReceiver has already been closed")
}
select {
case <-r.hasRecv:
default:
return fmt.Errorf("RTCRtpReceiver has not been started")
}
if err := r.rtcpReadStream.Close(); err != nil {
return err
}
if err := r.rtpReadStream.Close(); err != nil {
return err
}
<-r.rtcpOutDone
<-r.rtpOutDone
r.closed = true
return nil
}