forked from pion/webrtc
/
rtpsender.go
197 lines (164 loc) · 4.29 KB
/
rtpsender.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
185
186
187
188
189
190
191
192
193
194
195
196
197
// +build !js
package webrtc
import (
"fmt"
"io"
"sync"
"github.com/pion/rtcp"
"github.com/pion/rtp"
"github.com/pion/srtp"
)
// RTPSender allows an application to control how a given Track is encoded and transmitted to a remote peer
type RTPSender struct {
track *Track
rtcpReadStream *srtp.ReadStreamSRTCP
transport *DTLSTransport
// TODO(sgotti) remove this when in future we'll avoid replacing
// a transceiver sender since we can just check the
// transceiver negotiation status
negotiated bool
// A reference to the associated api object
api *API
mu sync.RWMutex
sendCalled, stopCalled chan interface{}
}
// NewRTPSender constructs a new RTPSender
func (api *API) NewRTPSender(track *Track, transport *DTLSTransport) (*RTPSender, error) {
if track == nil {
return nil, fmt.Errorf("Track must not be nil")
} else if transport == nil {
return nil, fmt.Errorf("DTLSTransport must not be nil")
}
track.mu.Lock()
defer track.mu.Unlock()
if track.receiver != nil {
return nil, fmt.Errorf("RTPSender can not be constructed with remote track")
}
track.totalSenderCount++
return &RTPSender{
track: track,
transport: transport,
api: api,
sendCalled: make(chan interface{}),
stopCalled: make(chan interface{}),
}, nil
}
func (r *RTPSender) isNegotiated() bool {
r.mu.RLock()
defer r.mu.RUnlock()
return r.negotiated
}
func (r *RTPSender) setNegotiated() {
r.mu.Lock()
defer r.mu.Unlock()
r.negotiated = true
}
// Transport returns the currently-configured *DTLSTransport or nil
// if one has not yet been configured
func (r *RTPSender) Transport() *DTLSTransport {
r.mu.RLock()
defer r.mu.RUnlock()
return r.transport
}
// Track returns the RTCRtpTransceiver track, or nil
func (r *RTPSender) Track() *Track {
r.mu.RLock()
defer r.mu.RUnlock()
return r.track
}
// Send Attempts to set the parameters controlling the sending of media.
func (r *RTPSender) Send(parameters RTPSendParameters) error {
r.mu.Lock()
defer r.mu.Unlock()
if r.hasSent() {
return fmt.Errorf("Send has already been called")
}
srtcpSession, err := r.transport.getSRTCPSession()
if err != nil {
return err
}
r.rtcpReadStream, err = srtcpSession.OpenReadStream(parameters.Encodings.SSRC)
if err != nil {
return err
}
r.track.mu.Lock()
r.track.activeSenders = append(r.track.activeSenders, r)
r.track.mu.Unlock()
close(r.sendCalled)
return nil
}
// Stop irreversibly stops the RTPSender
func (r *RTPSender) Stop() error {
r.mu.Lock()
defer r.mu.Unlock()
select {
case <-r.stopCalled:
return nil
default:
}
r.track.mu.Lock()
defer r.track.mu.Unlock()
filtered := []*RTPSender{}
for _, s := range r.track.activeSenders {
if s != r {
filtered = append(filtered, s)
} else {
r.track.totalSenderCount--
}
}
r.track.activeSenders = filtered
close(r.stopCalled)
if r.hasSent() {
return r.rtcpReadStream.Close()
}
return nil
}
// Read reads incoming RTCP for this RTPReceiver
func (r *RTPSender) Read(b []byte) (n int, err error) {
select {
case <-r.sendCalled:
return r.rtcpReadStream.Read(b)
case <-r.stopCalled:
return 0, io.ErrClosedPipe
}
}
// ReadRTCP is a convenience method that wraps Read and unmarshals for you
func (r *RTPSender) ReadRTCP() ([]rtcp.Packet, error) {
b := make([]byte, receiveMTU)
i, err := r.Read(b)
if err != nil {
return nil, err
}
return rtcp.Unmarshal(b[:i])
}
// SendRTP sends a RTP packet on this RTPSender
//
// You should use Track instead to send packets. This is exposed because pion/webrtc currently
// provides no way for users to send RTP packets directly. This is makes users unable to send
// retransmissions to a single RTPSender. in /v3 this will go away, only use this API if you really
// need it.
func (r *RTPSender) SendRTP(header *rtp.Header, payload []byte) (int, error) {
select {
case <-r.stopCalled:
return 0, fmt.Errorf("RTPSender has been stopped")
case <-r.sendCalled:
srtpSession, err := r.transport.getSRTPSession()
if err != nil {
return 0, err
}
writeStream, err := srtpSession.OpenWriteStream()
if err != nil {
return 0, err
}
return writeStream.WriteRTP(header, payload)
}
}
// hasSent tells if data has been ever sent for this instance
func (r *RTPSender) hasSent() bool {
select {
case <-r.sendCalled:
return true
default:
return false
}
}