-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_session.go
317 lines (263 loc) · 6.36 KB
/
send_session.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package main
import (
"fmt"
"io"
"os"
"sync"
"github.com/pion/webrtc/v2"
"github.com/sirupsen/logrus"
)
type sendSession struct {
sess inSession
stream io.Reader
initialized bool
dataChannel *webrtc.DataChannel
dataBuff []byte
msgToBeSent []outputMsg
stopSending chan struct{}
output chan outputMsg
doneCheckLock sync.Mutex
doneCheck bool
readingStats *stats
a *app
}
type inSession struct {
Done chan struct{}
NetworkStats *stats
sdpInput io.Reader
sdpOutput io.Writer
peerConnection *webrtc.PeerConnection
onCompletion compHandler
stunServers []string
}
type outputMsg struct {
n int
buff []byte
}
func newSendSession(c sendConfig) *sendSession {
s := &sendSession{
sess: inSession{
sdpInput: c.SDPProvider,
sdpOutput: c.SDPOutput,
Done: make(chan struct{}),
NetworkStats: newStats(),
stunServers: []string{fmt.Sprintf("stun:%s", c.STUN)},
onCompletion: func() {},
},
stream: c.Stream,
initialized: false,
dataBuff: make([]byte, senderBuffSize),
stopSending: make(chan struct{}, 1),
output: make(chan outputMsg, senderBuffSize*10),
doneCheck: false,
readingStats: newStats(),
}
if s.sess.sdpInput == nil {
s.sess.sdpInput = os.Stdin
}
if s.sess.sdpOutput == nil {
s.sess.sdpOutput = os.Stdout
}
return s
}
func (s *sendSession) onConnectionStateChange() func(webrtc.ICEConnectionState) {
return func(connState webrtc.ICEConnectionState) {
logrus.Infof("ICE Connection State has changed: %s\n", connState.String())
if connState == webrtc.ICEConnectionStateDisconnected {
s.stopSending <- struct{}{}
}
}
}
func (s *inSession) createConnection(changeFunction func(webrtc.ICEConnectionState)) error {
config := webrtc.Configuration{
ICEServers: []webrtc.ICEServer{
{
URLs: s.stunServers,
},
},
}
pc, err := webrtc.NewPeerConnection(config)
if err != nil {
return err
}
s.peerConnection = pc
pc.OnICEConnectionStateChange(changeFunction)
return nil
}
func (s *inSession) createDataChannel(c *webrtc.DataChannelInit) (*webrtc.DataChannel, error) {
return s.peerConnection.CreateDataChannel("data", c)
}
func (s *sendSession) close(call bool) {
if !call {
s.dataChannel.Close()
}
s.doneCheckLock.Lock()
if s.doneCheck {
s.doneCheckLock.Unlock()
return
}
s.doneCheck = true
s.doneCheckLock.Unlock()
s.dumpStats()
close(s.sess.Done)
}
// TODO: string implementation for below
func (s *sendSession) dumpStats() {
fmt.Printf(`
Disk : %v
Network : %v
`, s.readingStats, s.sess.NetworkStats)
}
func (s *sendSession) onBufferedAmountLow() func() {
return func() {
d := <-s.output
if d.n != 0 {
s.msgToBeSent = append(s.msgToBeSent, d)
} else if len(s.msgToBeSent) == 0 && s.dataChannel.BufferedAmount() == 0 {
s.sess.NetworkStats.stop()
s.close(false)
return
}
speed := s.sess.NetworkStats.bandwidth()
fmt.Printf("%.2f MB/s\r", speed)
for len(s.msgToBeSent) != 0 {
cur := s.msgToBeSent[0]
if err := s.dataChannel.Send(cur.buff); err != nil {
logrus.Errorf("Error, cannot send to client: %v\n", err)
return
}
s.sess.NetworkStats.addBytes(uint64(cur.n))
s.msgToBeSent = s.msgToBeSent[1:]
}
}
}
func (s *sendSession) writeToNetwork() {
s.dataChannel.OnBufferedAmountLow(s.onBufferedAmountLow())
<-s.stopSending
s.dataChannel.OnBufferedAmountLow(nil)
s.sess.NetworkStats.pause()
logrus.Infof("Pausing network I/O... (remaining at least %v packets)\n", len(s.output))
}
func (s *sendSession) onOpenHandler() func() {
return func() {
s.sess.NetworkStats.start()
logrus.Info("start sending data ...")
defer logrus.Info("stop sending data ...")
s.writeToNetwork()
}
}
func (s *sendSession) onCloseHandler() func() {
return func() {
s.close(true)
}
}
func (s *sendSession) createDataChannel() error {
o := true
mxPacketLifeTime := uint16(10000)
dChannel, err := s.sess.createDataChannel(&webrtc.DataChannelInit{
Ordered: &o,
MaxPacketLifeTime: &mxPacketLifeTime,
})
if err != nil {
return err
}
s.dataChannel = dChannel
s.dataChannel.OnBufferedAmountLow(s.onBufferedAmountLow())
s.dataChannel.SetBufferedAmountLowThreshold(bufferThreshold)
s.dataChannel.OnOpen(s.onOpenHandler())
s.dataChannel.OnClose(s.onCloseHandler())
return nil
}
func (s *inSession) createOffer() error {
ans, err := s.peerConnection.CreateOffer(nil)
if err != nil {
return err
}
return s.createSessionDescription(ans)
}
func (s *inSession) createSessionDescription(d webrtc.SessionDescription) error {
if err := s.peerConnection.SetLocalDescription(d); err != nil {
return err
}
d.SDP = stripSDP(d.SDP)
res, err := encode(d)
if err != nil {
return err
}
return os.WriteFile(local_file, []byte(res), 0644)
}
func (s *sendSession) initialize() error {
if s.initialized {
return nil
}
if err := s.sess.createConnection(s.onConnectionStateChange()); err != nil {
logrus.Errorln(err)
return err
}
if err := s.createDataChannel(); err != nil {
logrus.Errorln(err)
return err
}
if err := s.sess.createOffer(); err != nil {
logrus.Errorln(err)
return err
}
s.initialized = true
return nil
}
func (s *sendSession) readFile() {
logrus.Info("start reading data ...")
s.readingStats.start()
defer func() {
s.readingStats.pause()
logrus.Info("reading data paused ...")
close(s.output)
}()
for {
s.dataBuff = s.dataBuff[:cap(s.dataBuff)]
n, err := s.stream.Read(s.dataBuff)
if err != nil {
switch {
case err == io.EOF:
s.readingStats.stop()
logrus.Debugf("Got EOF after %v bytes!\n", s.readingStats.bytes())
default:
logrus.Errorf("reading error: %v", err)
}
return
}
s.dataBuff = s.dataBuff[:n]
s.readingStats.addBytes(uint64(n))
s.output <- outputMsg{
n: n,
buff: append([]byte(nil), s.dataBuff...),
}
}
}
func (s *inSession) readSDP() error {
var sdp webrtc.SessionDescription
for {
enc, err := readStream(s.sdpInput)
if err == nil {
if err := decode(enc, &sdp); err == nil {
break
}
}
logrus.Println("Invalid SDP, try again ...")
}
return s.peerConnection.SetRemoteDescription(sdp)
}
// TODO
func (s *sendSession) start() error {
if err := s.initialize(); err != nil {
return err
}
go s.readFile()
if err := s.sess.readSDP(); err != nil {
logrus.Errorln(err)
return err
}
<-s.sess.Done
s.sess.onCompletion()
return nil
}