forked from bluenviron/gortsplib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_reader.go
66 lines (53 loc) · 1.23 KB
/
client_reader.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
package gortsplib
import (
"time"
"github.com/bluenviron/gortsplib/v3/pkg/base"
)
type clientReader struct {
c *Client
closeErr chan error
}
func newClientReader(c *Client) *clientReader {
r := &clientReader{
c: c,
closeErr: make(chan error),
}
// for some reason, SetReadDeadline() must always be called in the same
// goroutine, otherwise Read() freezes.
// therefore, we disable the deadline and perform a check with a ticker.
r.c.nconn.SetReadDeadline(time.Time{})
go r.run()
return r
}
func (r *clientReader) close() {
r.c.nconn.SetReadDeadline(time.Now())
}
func (r *clientReader) run() {
r.c.readError <- r.runInner()
}
func (r *clientReader) runInner() error {
if *r.c.effectiveTransport == TransportUDP || *r.c.effectiveTransport == TransportUDPMulticast {
for {
res, err := r.c.conn.ReadResponse()
if err != nil {
return err
}
r.c.OnResponse(res)
}
} else {
for {
what, err := r.c.conn.ReadInterleavedFrameOrResponse()
if err != nil {
return err
}
switch what := what.(type) {
case *base.Response:
r.c.OnResponse(what)
case *base.InterleavedFrame:
if cb, ok := r.c.tcpCallbackByChannel[what.Channel]; ok {
cb(what.Payload)
}
}
}
}
}