forked from bluenviron/gortsplib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn-client.go
629 lines (534 loc) · 15.1 KB
/
conn-client.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/*
Package gortsplib is a RTSP 1.0 library for the Go programming language,
written for rtsp-simple-server.
Examples are available at https://github.com/aler9/gortsplib/tree/master/examples
*/
package gortsplib
import (
"bufio"
"fmt"
"net"
"net/url"
"strconv"
"strings"
"time"
"github.com/aler9/sdp/v3"
)
const (
clientReadBufferSize = 4096
clientWriteBufferSize = 4096
clientReceiverReportPeriod = 10 * time.Second
clientUdpCheckStreamPeriod = 5 * time.Second
clientUdpKeepalivePeriod = 30 * time.Second
)
// Track is a track available in a certain URL.
type Track struct {
// track id
Id int
// track codec and info in SDP format
Media *sdp.MediaDescription
}
// ConnClientConf allows to configure a ConnClient.
type ConnClientConf struct {
// target address in format hostname:port
Host string
// (optional) timeout for read requests.
// It defaults to 10 seconds
ReadTimeout time.Duration
// (optional) timeout for write requests.
// It defaults to 5 seconds
WriteTimeout time.Duration
// (optional) function used to initialize the TCP client.
// It defaults to net.DialTimeout
DialTimeout func(network, address string, timeout time.Duration) (net.Conn, error)
// (optional) function used to initialize UDP listeners.
// It defaults to net.ListenPacket
ListenPacket func(network, address string) (net.PacketConn, error)
}
// ConnClient is a client-side RTSP connection.
type ConnClient struct {
conf ConnClientConf
nconn net.Conn
br *bufio.Reader
bw *bufio.Writer
session string
cseq int
auth *authClient
streamProtocol *StreamProtocol
rtcpReceivers map[int]*RtcpReceiver
rtpListeners map[int]*ConnClientUdpListener
rtcpListeners map[int]*ConnClientUdpListener
receiverReportTerminate chan struct{}
receiverReportDone chan struct{}
}
// NewConnClient allocates a ConnClient. See ConnClientConf for the options.
func NewConnClient(conf ConnClientConf) (*ConnClient, error) {
if conf.ReadTimeout == time.Duration(0) {
conf.ReadTimeout = 10 * time.Second
}
if conf.WriteTimeout == time.Duration(0) {
conf.WriteTimeout = 5 * time.Second
}
if conf.DialTimeout == nil {
conf.DialTimeout = net.DialTimeout
}
if conf.ListenPacket == nil {
conf.ListenPacket = net.ListenPacket
}
nconn, err := conf.DialTimeout("tcp", conf.Host, conf.ReadTimeout)
if err != nil {
return nil, err
}
return &ConnClient{
conf: conf,
nconn: nconn,
br: bufio.NewReaderSize(nconn, clientReadBufferSize),
bw: bufio.NewWriterSize(nconn, clientWriteBufferSize),
rtcpReceivers: make(map[int]*RtcpReceiver),
rtpListeners: make(map[int]*ConnClientUdpListener),
rtcpListeners: make(map[int]*ConnClientUdpListener),
}, nil
}
// Close closes all the ConnClient resources.
func (c *ConnClient) Close() error {
if c.receiverReportTerminate != nil {
close(c.receiverReportTerminate)
<-c.receiverReportDone
}
for _, rr := range c.rtcpReceivers {
rr.Close()
}
for _, l := range c.rtpListeners {
l.Close()
}
for _, l := range c.rtcpListeners {
l.Close()
}
return c.nconn.Close()
}
// NetConn returns the underlying net.Conn.
func (c *ConnClient) NetConn() net.Conn {
return c.nconn
}
// ReadFrame reads an InterleavedFrame.
func (c *ConnClient) ReadFrame(frame *InterleavedFrame) error {
c.nconn.SetReadDeadline(time.Now().Add(c.conf.ReadTimeout))
err := frame.Read(c.br)
if err != nil {
return err
}
c.rtcpReceivers[frame.TrackId].OnFrame(frame.StreamType, frame.Content)
return nil
}
// ReadFrameOrResponse reads an InterleavedFrame or a Response.
func (c *ConnClient) ReadFrameOrResponse(frame *InterleavedFrame) (interface{}, error) {
c.nconn.SetReadDeadline(time.Now().Add(c.conf.ReadTimeout))
b, err := c.br.ReadByte()
if err != nil {
return nil, err
}
c.br.UnreadByte()
if b == interleavedFrameMagicByte {
err := frame.Read(c.br)
if err != nil {
return nil, err
}
return frame, err
}
return ReadResponse(c.br)
}
// Do writes a Request and reads a Response.
func (c *ConnClient) Do(req *Request) (*Response, error) {
if req.Header == nil {
req.Header = make(Header)
}
// insert session
if c.session != "" {
req.Header["Session"] = HeaderValue{c.session}
}
// insert auth
if c.auth != nil {
// remove credentials
u := &url.URL{
Scheme: req.Url.Scheme,
Host: req.Url.Host,
Path: req.Url.Path,
RawQuery: req.Url.RawQuery,
}
req.Header["Authorization"] = c.auth.GenerateHeader(req.Method, u)
}
// insert cseq
c.cseq += 1
req.Header["CSeq"] = HeaderValue{strconv.FormatInt(int64(c.cseq), 10)}
c.nconn.SetWriteDeadline(time.Now().Add(c.conf.WriteTimeout))
err := req.Write(c.bw)
if err != nil {
return nil, err
}
if req.SkipResponse {
return nil, nil
}
c.nconn.SetReadDeadline(time.Now().Add(c.conf.ReadTimeout))
res, err := ReadResponse(c.br)
if err != nil {
return nil, err
}
// get session from response
if v, ok := res.Header["Session"]; ok {
sx, err := ReadHeaderSession(v)
if err != nil {
return nil, fmt.Errorf("unable to parse session header: %s", err)
}
c.session = sx.Session
}
// setup authentication
if res.StatusCode == StatusUnauthorized && req.Url.User != nil && c.auth == nil {
pass, _ := req.Url.User.Password()
auth, err := newAuthClient(res.Header["WWW-Authenticate"], req.Url.User.Username(), pass)
if err != nil {
return nil, fmt.Errorf("unable to setup authentication: %s", err)
}
c.auth = auth
// send request again
return c.Do(req)
}
return res, nil
}
// WriteFrame writes an InterleavedFrame.
func (c *ConnClient) WriteFrame(frame *InterleavedFrame) error {
c.nconn.SetWriteDeadline(time.Now().Add(c.conf.WriteTimeout))
return frame.Write(c.bw)
}
// Options writes an OPTIONS request and reads a response, that contains
// the methods allowed by the server. Since this method is not implemented by
// every RTSP server, the function does not fail if the returned code is StatusNotFound.
func (c *ConnClient) Options(u *url.URL) (*Response, error) {
res, err := c.Do(&Request{
Method: OPTIONS,
// strip path
Url: &url.URL{
Scheme: "rtsp",
Host: u.Host,
User: u.User,
Path: "/",
},
})
if err != nil {
return nil, err
}
if res.StatusCode != StatusOK && res.StatusCode != StatusNotFound {
return nil, fmt.Errorf("OPTIONS: bad status code: %d (%s)", res.StatusCode, res.StatusMessage)
}
return res, nil
}
// Describe writes a DESCRIBE request, that means that we want to obtain the SDP
// document that describes the tracks available in the given URL. It then
// reads a Response.
func (c *ConnClient) Describe(u *url.URL) ([]*Track, *Response, error) {
res, err := c.Do(&Request{
Method: DESCRIBE,
Url: u,
})
if err != nil {
return nil, nil, err
}
if res.StatusCode != StatusOK {
return nil, nil, fmt.Errorf("DESCRIBE: bad status code: %d (%s)", res.StatusCode, res.StatusMessage)
}
contentType, ok := res.Header["Content-Type"]
if !ok || len(contentType) != 1 {
return nil, nil, fmt.Errorf("DESCRIBE: Content-Type not provided")
}
if contentType[0] != "application/sdp" {
return nil, nil, fmt.Errorf("DESCRIBE: wrong Content-Type, expected application/sdp")
}
sdpd := &sdp.SessionDescription{}
err = sdpd.Unmarshal(res.Content)
if err != nil {
return nil, nil, err
}
tracks := make([]*Track, len(sdpd.MediaDescriptions))
for i, media := range sdpd.MediaDescriptions {
tracks[i] = &Track{
Id: i,
Media: media,
}
}
return tracks, res, nil
}
func (c *ConnClient) setup(u *url.URL, media *sdp.MediaDescription, transport []string) (*Response, error) {
// build an URL with the control attribute from media
u = func() *url.URL {
control := func() string {
for _, attr := range media.Attributes {
if attr.Key == "control" {
return attr.Value
}
}
return ""
}()
// no control attribute, use original URL
if control == "" {
return u
}
// control attribute with absolute path
if strings.HasPrefix(control, "rtsp://") {
newu, err := url.Parse(control)
if err != nil {
return u
}
return &url.URL{
Scheme: "rtsp",
Host: u.Host,
User: u.User,
Path: newu.Path,
RawQuery: newu.RawQuery,
}
}
// control attribute with relative path
return &url.URL{
Scheme: "rtsp",
Host: u.Host,
User: u.User,
Path: func() string {
ret := u.Path
if len(ret) == 0 || ret[len(ret)-1] != '/' {
ret += "/"
}
ret += control
return ret
}(),
RawQuery: u.RawQuery,
}
}()
res, err := c.Do(&Request{
Method: SETUP,
Url: u,
Header: Header{
"Transport": HeaderValue{strings.Join(transport, ";")},
},
})
if err != nil {
return nil, err
}
if res.StatusCode != StatusOK {
return nil, fmt.Errorf("SETUP: bad status code: %d (%s)", res.StatusCode, res.StatusMessage)
}
return res, nil
}
// SetupUdp writes a SETUP request, that means that we want to read
// a given track with the UDP transport. It then reads a Response.
func (c *ConnClient) SetupUdp(u *url.URL, track *Track, rtpPort int,
rtcpPort int) (*ConnClientUdpListener, *ConnClientUdpListener, *Response, error) {
if c.streamProtocol != nil && *c.streamProtocol != StreamProtocolUdp {
return nil, nil, nil, fmt.Errorf("cannot setup tracks with different protocols")
}
if _, ok := c.rtcpReceivers[track.Id]; ok {
return nil, nil, nil, fmt.Errorf("track has already been setup")
}
rtpListener, err := newConnClientUdpListener(c, rtpPort, track.Id, StreamTypeRtp)
if err != nil {
return nil, nil, nil, err
}
rtcpListener, err := newConnClientUdpListener(c, rtcpPort, track.Id, StreamTypeRtcp)
if err != nil {
rtpListener.Close()
return nil, nil, nil, err
}
res, err := c.setup(u, track.Media, []string{
"RTP/AVP/UDP",
"unicast",
fmt.Sprintf("client_port=%d-%d", rtpPort, rtcpPort),
})
if err != nil {
rtpListener.Close()
rtcpListener.Close()
return nil, nil, nil, err
}
th, err := ReadHeaderTransport(res.Header["Transport"])
if err != nil {
rtpListener.Close()
rtcpListener.Close()
return nil, nil, nil, fmt.Errorf("SETUP: transport header: %s", err)
}
rtpServerPort, rtcpServerPort := th.Ports("server_port")
if rtpServerPort == 0 {
rtpListener.Close()
rtcpListener.Close()
return nil, nil, nil, fmt.Errorf("SETUP: server ports not provided")
}
c.rtcpReceivers[track.Id] = NewRtcpReceiver()
streamProtocol := StreamProtocolUdp
c.streamProtocol = &streamProtocol
rtpListener.publisherIp = c.nconn.RemoteAddr().(*net.TCPAddr).IP
rtpListener.publisherPort = rtpServerPort
c.rtpListeners[track.Id] = rtpListener
rtcpListener.publisherIp = c.nconn.RemoteAddr().(*net.TCPAddr).IP
rtcpListener.publisherPort = rtcpServerPort
c.rtcpListeners[track.Id] = rtcpListener
return rtpListener, rtcpListener, res, nil
}
// SetupTcp writes a SETUP request, that means that we want to read
// a given track with the TCP transport. It then reads a Response.
func (c *ConnClient) SetupTcp(u *url.URL, track *Track) (*Response, error) {
if c.streamProtocol != nil && *c.streamProtocol != StreamProtocolTcp {
return nil, fmt.Errorf("cannot setup tracks with different protocols")
}
if _, ok := c.rtcpReceivers[track.Id]; ok {
return nil, fmt.Errorf("track has already been setup")
}
interleaved := fmt.Sprintf("interleaved=%d-%d", (track.Id * 2), (track.Id*2)+1)
res, err := c.setup(u, track.Media, []string{
"RTP/AVP/TCP",
"unicast",
interleaved,
})
if err != nil {
return nil, err
}
th, err := ReadHeaderTransport(res.Header["Transport"])
if err != nil {
return nil, fmt.Errorf("SETUP: transport header: %s", err)
}
_, ok := th[interleaved]
if !ok {
return nil, fmt.Errorf("SETUP: transport header does not contain '%s' (%s)",
interleaved, res.Header["Transport"])
}
c.rtcpReceivers[track.Id] = NewRtcpReceiver()
streamProtocol := StreamProtocolTcp
c.streamProtocol = &streamProtocol
return res, nil
}
// Play must be called after SetupUDP() or SetupTCP(), and writes a PLAY request,
// that means that we want to start the stream. It then reads a Response.
func (c *ConnClient) Play(u *url.URL) (*Response, error) {
_, err := c.Do(&Request{
Method: PLAY,
Url: u,
SkipResponse: true,
})
if err != nil {
return nil, err
}
res, err := func() (*Response, error) {
frame := &InterleavedFrame{
Content: make([]byte, 0, 512*1024),
}
// v4lrtspserver sends frames before the response.
// ignore them and wait for the response.
for {
frame.Content = frame.Content[:cap(frame.Content)]
recv, err := c.ReadFrameOrResponse(frame)
if err != nil {
return nil, err
}
if res, ok := recv.(*Response); ok {
if res.StatusCode != StatusOK {
return nil, fmt.Errorf("bad status code: %d (%s)", res.StatusCode, res.StatusMessage)
}
return res, nil
}
}
}()
if err != nil {
return nil, err
}
c.receiverReportTerminate = make(chan struct{})
c.receiverReportDone = make(chan struct{})
receiverReportTicker := time.NewTicker(clientReceiverReportPeriod)
go func() {
defer close(c.receiverReportDone)
defer receiverReportTicker.Stop()
for {
select {
case <-c.receiverReportTerminate:
return
case <-receiverReportTicker.C:
for trackId := range c.rtcpReceivers {
frame := c.rtcpReceivers[trackId].Report()
if *c.streamProtocol == StreamProtocolUdp {
c.rtcpListeners[trackId].pc.WriteTo(frame, &net.UDPAddr{
IP: c.nconn.RemoteAddr().(*net.TCPAddr).IP,
Zone: c.nconn.RemoteAddr().(*net.TCPAddr).Zone,
Port: c.rtcpListeners[trackId].publisherPort,
})
} else {
c.WriteFrame(&InterleavedFrame{
TrackId: trackId,
StreamType: StreamTypeRtcp,
Content: frame,
})
}
}
}
}
}()
return res, nil
}
// LoopUDP must be called after SetupUDP() and Play(); it keeps
// the TCP connection open through keepalives, and returns when the TCP
// connection closes.
func (c *ConnClient) LoopUDP(u *url.URL) error {
// do a first keepalive before start reading
_, err := c.Do(&Request{
Method: OPTIONS,
Url: &url.URL{
Scheme: "rtsp",
Host: u.Host,
User: u.User,
Path: "/",
},
SkipResponse: true,
})
if err != nil {
c.nconn.Close()
return err
}
readDone := make(chan error)
go func() {
for {
c.nconn.SetReadDeadline(time.Now().Add(clientUdpKeepalivePeriod))
_, err := ReadResponse(c.br)
if err != nil {
readDone <- err
return
}
}
}()
keepaliveTicker := time.NewTicker(clientUdpKeepalivePeriod)
defer keepaliveTicker.Stop()
checkStreamTicker := time.NewTicker(clientUdpCheckStreamPeriod)
defer checkStreamTicker.Stop()
for {
select {
case err := <-readDone:
c.nconn.Close()
return err
case <-keepaliveTicker.C:
_, err := c.Do(&Request{
Method: OPTIONS,
Url: &url.URL{
Scheme: "rtsp",
Host: u.Host,
User: u.User,
Path: "/",
},
SkipResponse: true,
})
if err != nil {
c.nconn.Close()
<-readDone
return err
}
case <-checkStreamTicker.C:
for trackId := range c.rtcpReceivers {
if time.Since(c.rtcpReceivers[trackId].LastFrameTime()) >= c.conf.ReadTimeout {
c.nconn.Close()
<-readDone
return fmt.Errorf("stream is dead")
}
}
}
}
}