mirror of
https://github.com/aler9/gortsplib
synced 2025-10-05 15:16:51 +08:00
rename ClientConf into Client
This commit is contained in:
@@ -9,27 +9,26 @@ import (
|
|||||||
"github.com/aler9/gortsplib/pkg/headers"
|
"github.com/aler9/gortsplib/pkg/headers"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DefaultClientConf is the default ClientConf.
|
// DefaultClient is the default Client.
|
||||||
var DefaultClientConf = ClientConf{}
|
var DefaultClient = &Client{}
|
||||||
|
|
||||||
// Dial connects to a server.
|
// Dial connects to a server.
|
||||||
func Dial(scheme string, host string) (*ClientConn, error) {
|
func Dial(scheme string, host string) (*ClientConn, error) {
|
||||||
return DefaultClientConf.Dial(scheme, host)
|
return DefaultClient.Dial(scheme, host)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialRead connects to a server and starts reading all tracks.
|
// DialRead connects to a server and starts reading all tracks.
|
||||||
func DialRead(address string) (*ClientConn, error) {
|
func DialRead(address string) (*ClientConn, error) {
|
||||||
return DefaultClientConf.DialRead(address)
|
return DefaultClient.DialRead(address)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialPublish connects to a server and starts publishing the tracks.
|
// DialPublish connects to a server and starts publishing the tracks.
|
||||||
func DialPublish(address string, tracks Tracks) (*ClientConn, error) {
|
func DialPublish(address string, tracks Tracks) (*ClientConn, error) {
|
||||||
return DefaultClientConf.DialPublish(address, tracks)
|
return DefaultClient.DialPublish(address, tracks)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ClientConf allows to initialize a ClientConn.
|
// Client is a RTSP client.
|
||||||
// All fields are optional.
|
type Client struct {
|
||||||
type ClientConf struct {
|
|
||||||
// the stream protocol (UDP or TCP).
|
// the stream protocol (UDP or TCP).
|
||||||
// If nil, it is chosen automatically (first UDP, then, if it fails, TCP).
|
// If nil, it is chosen automatically (first UDP, then, if it fails, TCP).
|
||||||
// It defaults to nil.
|
// It defaults to nil.
|
||||||
@@ -91,12 +90,12 @@ type ClientConf struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Dial connects to a server.
|
// Dial connects to a server.
|
||||||
func (c ClientConf) Dial(scheme string, host string) (*ClientConn, error) {
|
func (c *Client) Dial(scheme string, host string) (*ClientConn, error) {
|
||||||
return newClientConn(c, scheme, host)
|
return newClientConn(c, scheme, host)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialRead connects to the address and starts reading all tracks.
|
// DialRead connects to the address and starts reading all tracks.
|
||||||
func (c ClientConf) DialRead(address string) (*ClientConn, error) {
|
func (c *Client) DialRead(address string) (*ClientConn, error) {
|
||||||
u, err := base.ParseURL(address)
|
u, err := base.ParseURL(address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -137,7 +136,7 @@ func (c ClientConf) DialRead(address string) (*ClientConn, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DialPublish connects to the address and starts publishing the tracks.
|
// DialPublish connects to the address and starts publishing the tracks.
|
||||||
func (c ClientConf) DialPublish(address string, tracks Tracks) (*ClientConn, error) {
|
func (c *Client) DialPublish(address string, tracks Tracks) (*ClientConn, error) {
|
||||||
u, err := base.ParseURL(address)
|
u, err := base.ParseURL(address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
@@ -71,7 +71,7 @@ func (s clientConnState) String() string {
|
|||||||
|
|
||||||
// ClientConn is a client-side RTSP connection.
|
// ClientConn is a client-side RTSP connection.
|
||||||
type ClientConn struct {
|
type ClientConn struct {
|
||||||
conf ClientConf
|
c *Client
|
||||||
nconn net.Conn
|
nconn net.Conn
|
||||||
isTLS bool
|
isTLS bool
|
||||||
br *bufio.Reader
|
br *bufio.Reader
|
||||||
@@ -103,42 +103,42 @@ type ClientConn struct {
|
|||||||
backgroundDone chan struct{}
|
backgroundDone chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newClientConn(conf ClientConf, scheme string, host string) (*ClientConn, error) {
|
func newClientConn(c *Client, scheme string, host string) (*ClientConn, error) {
|
||||||
if conf.TLSConfig == nil {
|
if c.TLSConfig == nil {
|
||||||
conf.TLSConfig = &tls.Config{InsecureSkipVerify: true}
|
c.TLSConfig = &tls.Config{InsecureSkipVerify: true}
|
||||||
}
|
}
|
||||||
if conf.ReadTimeout == 0 {
|
if c.ReadTimeout == 0 {
|
||||||
conf.ReadTimeout = 10 * time.Second
|
c.ReadTimeout = 10 * time.Second
|
||||||
}
|
}
|
||||||
if conf.InitialUDPReadTimeout == 0 {
|
if c.InitialUDPReadTimeout == 0 {
|
||||||
conf.InitialUDPReadTimeout = 3 * time.Second
|
c.InitialUDPReadTimeout = 3 * time.Second
|
||||||
}
|
}
|
||||||
if conf.WriteTimeout == 0 {
|
if c.WriteTimeout == 0 {
|
||||||
conf.WriteTimeout = 10 * time.Second
|
c.WriteTimeout = 10 * time.Second
|
||||||
}
|
}
|
||||||
if conf.ReadBufferCount == 0 {
|
if c.ReadBufferCount == 0 {
|
||||||
conf.ReadBufferCount = 1
|
c.ReadBufferCount = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
if conf.ReadBufferSize == 0 {
|
if c.ReadBufferSize == 0 {
|
||||||
conf.ReadBufferSize = 2048
|
c.ReadBufferSize = 2048
|
||||||
}
|
}
|
||||||
if conf.DialTimeout == nil {
|
if c.DialTimeout == nil {
|
||||||
conf.DialTimeout = net.DialTimeout
|
c.DialTimeout = net.DialTimeout
|
||||||
}
|
}
|
||||||
if conf.ListenPacket == nil {
|
if c.ListenPacket == nil {
|
||||||
conf.ListenPacket = net.ListenPacket
|
c.ListenPacket = net.ListenPacket
|
||||||
}
|
}
|
||||||
|
|
||||||
if conf.senderReportPeriod == 0 {
|
if c.senderReportPeriod == 0 {
|
||||||
conf.senderReportPeriod = 10 * time.Second
|
c.senderReportPeriod = 10 * time.Second
|
||||||
}
|
}
|
||||||
if conf.receiverReportPeriod == 0 {
|
if c.receiverReportPeriod == 0 {
|
||||||
conf.receiverReportPeriod = 10 * time.Second
|
c.receiverReportPeriod = 10 * time.Second
|
||||||
}
|
}
|
||||||
|
|
||||||
cc := &ClientConn{
|
cc := &ClientConn{
|
||||||
conf: conf,
|
c: c,
|
||||||
tracks: make(map[int]clientConnTrack),
|
tracks: make(map[int]clientConnTrack),
|
||||||
writeError: fmt.Errorf("not running"),
|
writeError: fmt.Errorf("not running"),
|
||||||
}
|
}
|
||||||
@@ -203,7 +203,7 @@ func (cc *ClientConn) connOpen(scheme string, host string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
v := StreamProtocolUDP
|
v := StreamProtocolUDP
|
||||||
if scheme == "rtsps" && cc.conf.StreamProtocol == &v {
|
if scheme == "rtsps" && cc.c.StreamProtocol == &v {
|
||||||
return fmt.Errorf("RTSPS can't be used with UDP")
|
return fmt.Errorf("RTSPS can't be used with UDP")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -211,14 +211,14 @@ func (cc *ClientConn) connOpen(scheme string, host string) error {
|
|||||||
host += ":554"
|
host += ":554"
|
||||||
}
|
}
|
||||||
|
|
||||||
nconn, err := cc.conf.DialTimeout("tcp", host, cc.conf.ReadTimeout)
|
nconn, err := cc.c.DialTimeout("tcp", host, cc.c.ReadTimeout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
conn := func() net.Conn {
|
conn := func() net.Conn {
|
||||||
if scheme == "rtsps" {
|
if scheme == "rtsps" {
|
||||||
return tls.Client(nconn, cc.conf.TLSConfig)
|
return tls.Client(nconn, cc.c.TLSConfig)
|
||||||
}
|
}
|
||||||
return nconn
|
return nconn
|
||||||
}()
|
}()
|
||||||
@@ -289,11 +289,11 @@ func (cc *ClientConn) Do(req *base.Request) (*base.Response, error) {
|
|||||||
// add user agent
|
// add user agent
|
||||||
req.Header["User-Agent"] = base.HeaderValue{"gortsplib"}
|
req.Header["User-Agent"] = base.HeaderValue{"gortsplib"}
|
||||||
|
|
||||||
if cc.conf.OnRequest != nil {
|
if cc.c.OnRequest != nil {
|
||||||
cc.conf.OnRequest(req)
|
cc.c.OnRequest(req)
|
||||||
}
|
}
|
||||||
|
|
||||||
cc.nconn.SetWriteDeadline(time.Now().Add(cc.conf.WriteTimeout))
|
cc.nconn.SetWriteDeadline(time.Now().Add(cc.c.WriteTimeout))
|
||||||
err := req.Write(cc.bw)
|
err := req.Write(cc.bw)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -304,7 +304,7 @@ func (cc *ClientConn) Do(req *base.Request) (*base.Response, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var res base.Response
|
var res base.Response
|
||||||
cc.nconn.SetReadDeadline(time.Now().Add(cc.conf.ReadTimeout))
|
cc.nconn.SetReadDeadline(time.Now().Add(cc.c.ReadTimeout))
|
||||||
|
|
||||||
if cc.tcpFrameBuffer != nil {
|
if cc.tcpFrameBuffer != nil {
|
||||||
// read the response and ignore interleaved frames in between;
|
// read the response and ignore interleaved frames in between;
|
||||||
@@ -322,8 +322,8 @@ func (cc *ClientConn) Do(req *base.Request) (*base.Response, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if cc.conf.OnResponse != nil {
|
if cc.c.OnResponse != nil {
|
||||||
cc.conf.OnResponse(&res)
|
cc.c.OnResponse(&res)
|
||||||
}
|
}
|
||||||
|
|
||||||
// get session from response
|
// get session from response
|
||||||
@@ -423,7 +423,7 @@ func (cc *ClientConn) Describe(u *base.URL) (Tracks, *base.Response, error) {
|
|||||||
|
|
||||||
if res.StatusCode != base.StatusOK {
|
if res.StatusCode != base.StatusOK {
|
||||||
// redirect
|
// redirect
|
||||||
if !cc.conf.RedirectDisable &&
|
if !cc.c.RedirectDisable &&
|
||||||
res.StatusCode >= base.StatusMovedPermanently &&
|
res.StatusCode >= base.StatusMovedPermanently &&
|
||||||
res.StatusCode <= base.StatusUseProxy &&
|
res.StatusCode <= base.StatusUseProxy &&
|
||||||
len(res.Header["Location"]) == 1 {
|
len(res.Header["Location"]) == 1 {
|
||||||
@@ -530,8 +530,8 @@ func (cc *ClientConn) Setup(mode headers.TransportMode, track *Track,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// protocol set by conf
|
// protocol set by conf
|
||||||
if cc.conf.StreamProtocol != nil {
|
if cc.c.StreamProtocol != nil {
|
||||||
return *cc.conf.StreamProtocol
|
return *cc.c.StreamProtocol
|
||||||
}
|
}
|
||||||
|
|
||||||
// try UDP
|
// try UDP
|
||||||
@@ -637,7 +637,7 @@ func (cc *ClientConn) Setup(mode headers.TransportMode, track *Track,
|
|||||||
// switch protocol automatically
|
// switch protocol automatically
|
||||||
if res.StatusCode == base.StatusUnsupportedTransport &&
|
if res.StatusCode == base.StatusUnsupportedTransport &&
|
||||||
cc.streamProtocol == nil &&
|
cc.streamProtocol == nil &&
|
||||||
cc.conf.StreamProtocol == nil {
|
cc.c.StreamProtocol == nil {
|
||||||
|
|
||||||
v := StreamProtocolTCP
|
v := StreamProtocolTCP
|
||||||
cc.streamProtocol = &v
|
cc.streamProtocol = &v
|
||||||
@@ -668,7 +668,7 @@ func (cc *ClientConn) Setup(mode headers.TransportMode, track *Track,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !cc.conf.AnyPortEnable {
|
if !cc.c.AnyPortEnable {
|
||||||
if thRes.ServerPorts == nil || (thRes.ServerPorts[0] == 0 && thRes.ServerPorts[1] == 0) {
|
if thRes.ServerPorts == nil || (thRes.ServerPorts[0] == 0 && thRes.ServerPorts[1] == 0) {
|
||||||
rtpListener.close()
|
rtpListener.close()
|
||||||
rtcpListener.close()
|
rtcpListener.close()
|
||||||
@@ -732,7 +732,7 @@ func (cc *ClientConn) Setup(mode headers.TransportMode, track *Track,
|
|||||||
|
|
||||||
if *cc.streamProtocol == StreamProtocolTCP &&
|
if *cc.streamProtocol == StreamProtocolTCP &&
|
||||||
cc.tcpFrameBuffer == nil {
|
cc.tcpFrameBuffer == nil {
|
||||||
cc.tcpFrameBuffer = multibuffer.New(uint64(cc.conf.ReadBufferCount), uint64(cc.conf.ReadBufferSize))
|
cc.tcpFrameBuffer = multibuffer.New(uint64(cc.c.ReadBufferCount), uint64(cc.c.ReadBufferSize))
|
||||||
}
|
}
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
@@ -798,7 +798,7 @@ func (cc *ClientConn) WriteFrame(trackID int, streamType StreamType, payload []b
|
|||||||
return cc.tracks[trackID].udpRTCPListener.write(payload)
|
return cc.tracks[trackID].udpRTCPListener.write(payload)
|
||||||
}
|
}
|
||||||
|
|
||||||
cc.nconn.SetWriteDeadline(now.Add(cc.conf.WriteTimeout))
|
cc.nconn.SetWriteDeadline(now.Add(cc.c.WriteTimeout))
|
||||||
frame := base.InterleavedFrame{
|
frame := base.InterleavedFrame{
|
||||||
TrackID: trackID,
|
TrackID: trackID,
|
||||||
StreamType: streamType,
|
StreamType: streamType,
|
||||||
@@ -865,7 +865,7 @@ func (cc *ClientConn) ReadFrames(onFrame func(int, StreamType, []byte)) chan err
|
|||||||
if *cc.streamProtocol == StreamProtocolUDP &&
|
if *cc.streamProtocol == StreamProtocolUDP &&
|
||||||
safeState == clientConnStatePlay {
|
safeState == clientConnStatePlay {
|
||||||
if _, ok := err.(liberrors.ErrClientNoUDPPacketsRecently); ok {
|
if _, ok := err.(liberrors.ErrClientNoUDPPacketsRecently); ok {
|
||||||
if cc.conf.StreamProtocol == nil {
|
if cc.c.StreamProtocol == nil {
|
||||||
prevBaseURL := cc.streamBaseURL
|
prevBaseURL := cc.streamBaseURL
|
||||||
oldUseGetParameter := cc.useGetParameter
|
oldUseGetParameter := cc.useGetParameter
|
||||||
prevTracks := cc.tracks
|
prevTracks := cc.tracks
|
||||||
|
@@ -116,7 +116,7 @@ func (cc *ClientConn) backgroundRecordUDP() error {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
reportTicker := time.NewTicker(cc.conf.senderReportPeriod)
|
reportTicker := time.NewTicker(cc.c.senderReportPeriod)
|
||||||
defer reportTicker.Stop()
|
defer reportTicker.Stop()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
@@ -161,7 +161,7 @@ func (cc *ClientConn) backgroundRecordTCP() error {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
reportTicker := time.NewTicker(cc.conf.senderReportPeriod)
|
reportTicker := time.NewTicker(cc.c.senderReportPeriod)
|
||||||
defer reportTicker.Stop()
|
defer reportTicker.Stop()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
@@ -163,7 +163,7 @@ func TestClientPublishSerial(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
conf := ClientConf{
|
c := &Client{
|
||||||
StreamProtocol: func() *StreamProtocol {
|
StreamProtocol: func() *StreamProtocol {
|
||||||
if proto == "udp" {
|
if proto == "udp" {
|
||||||
v := StreamProtocolUDP
|
v := StreamProtocolUDP
|
||||||
@@ -177,7 +177,7 @@ func TestClientPublishSerial(t *testing.T) {
|
|||||||
track, err := NewTrackH264(96, []byte("123456"), []byte("123456"))
|
track, err := NewTrackH264(96, []byte("123456"), []byte("123456"))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
conn, err := conf.DialPublish("rtsp://localhost:8554/teststream",
|
conn, err := c.DialPublish("rtsp://localhost:8554/teststream",
|
||||||
Tracks{track})
|
Tracks{track})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
@@ -303,7 +303,7 @@ func TestClientPublishParallel(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
conf := ClientConf{
|
c := &Client{
|
||||||
StreamProtocol: func() *StreamProtocol {
|
StreamProtocol: func() *StreamProtocol {
|
||||||
if proto == "udp" {
|
if proto == "udp" {
|
||||||
v := StreamProtocolUDP
|
v := StreamProtocolUDP
|
||||||
@@ -320,7 +320,7 @@ func TestClientPublishParallel(t *testing.T) {
|
|||||||
writerDone := make(chan struct{})
|
writerDone := make(chan struct{})
|
||||||
defer func() { <-writerDone }()
|
defer func() { <-writerDone }()
|
||||||
|
|
||||||
conn, err := conf.DialPublish("rtsp://localhost:8554/teststream",
|
conn, err := c.DialPublish("rtsp://localhost:8554/teststream",
|
||||||
Tracks{track})
|
Tracks{track})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
@@ -464,7 +464,7 @@ func TestClientPublishPauseSerial(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
conf := ClientConf{
|
c := &Client{
|
||||||
StreamProtocol: func() *StreamProtocol {
|
StreamProtocol: func() *StreamProtocol {
|
||||||
if proto == "udp" {
|
if proto == "udp" {
|
||||||
v := StreamProtocolUDP
|
v := StreamProtocolUDP
|
||||||
@@ -478,7 +478,7 @@ func TestClientPublishPauseSerial(t *testing.T) {
|
|||||||
track, err := NewTrackH264(96, []byte("123456"), []byte("123456"))
|
track, err := NewTrackH264(96, []byte("123456"), []byte("123456"))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
conn, err := conf.DialPublish("rtsp://localhost:8554/teststream",
|
conn, err := c.DialPublish("rtsp://localhost:8554/teststream",
|
||||||
Tracks{track})
|
Tracks{track})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
@@ -604,7 +604,7 @@ func TestClientPublishPauseParallel(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
conf := ClientConf{
|
c := &Client{
|
||||||
StreamProtocol: func() *StreamProtocol {
|
StreamProtocol: func() *StreamProtocol {
|
||||||
if proto == "udp" {
|
if proto == "udp" {
|
||||||
v := StreamProtocolUDP
|
v := StreamProtocolUDP
|
||||||
@@ -618,7 +618,7 @@ func TestClientPublishPauseParallel(t *testing.T) {
|
|||||||
track, err := NewTrackH264(96, []byte("123456"), []byte("123456"))
|
track, err := NewTrackH264(96, []byte("123456"), []byte("123456"))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
conn, err := conf.DialPublish("rtsp://localhost:8554/teststream",
|
conn, err := c.DialPublish("rtsp://localhost:8554/teststream",
|
||||||
Tracks{track})
|
Tracks{track})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
@@ -889,7 +889,7 @@ func TestClientPublishRTCPReport(t *testing.T) {
|
|||||||
}.Write(bconn.Writer)
|
}.Write(bconn.Writer)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
conf := ClientConf{
|
c := &Client{
|
||||||
StreamProtocol: func() *StreamProtocol {
|
StreamProtocol: func() *StreamProtocol {
|
||||||
v := StreamProtocolTCP
|
v := StreamProtocolTCP
|
||||||
return &v
|
return &v
|
||||||
@@ -900,7 +900,7 @@ func TestClientPublishRTCPReport(t *testing.T) {
|
|||||||
track, err := NewTrackH264(96, []byte("123456"), []byte("123456"))
|
track, err := NewTrackH264(96, []byte("123456"), []byte("123456"))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
conn, err := conf.DialPublish("rtsp://localhost:8554/teststream",
|
conn, err := c.DialPublish("rtsp://localhost:8554/teststream",
|
||||||
Tracks{track})
|
Tracks{track})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
@@ -89,14 +89,14 @@ func (cc *ClientConn) backgroundPlayUDP() error {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
reportTicker := time.NewTicker(cc.conf.receiverReportPeriod)
|
reportTicker := time.NewTicker(cc.c.receiverReportPeriod)
|
||||||
defer reportTicker.Stop()
|
defer reportTicker.Stop()
|
||||||
|
|
||||||
keepaliveTicker := time.NewTicker(clientConnUDPKeepalivePeriod)
|
keepaliveTicker := time.NewTicker(clientConnUDPKeepalivePeriod)
|
||||||
defer keepaliveTicker.Stop()
|
defer keepaliveTicker.Stop()
|
||||||
|
|
||||||
checkStreamInitial := true
|
checkStreamInitial := true
|
||||||
checkStreamTicker := time.NewTicker(cc.conf.InitialUDPReadTimeout)
|
checkStreamTicker := time.NewTicker(cc.c.InitialUDPReadTimeout)
|
||||||
defer func() {
|
defer func() {
|
||||||
checkStreamTicker.Stop()
|
checkStreamTicker.Stop()
|
||||||
}()
|
}()
|
||||||
@@ -166,12 +166,12 @@ func (cc *ClientConn) backgroundPlayUDP() error {
|
|||||||
now := time.Now()
|
now := time.Now()
|
||||||
for _, cct := range cc.tracks {
|
for _, cct := range cc.tracks {
|
||||||
lft := time.Unix(atomic.LoadInt64(cct.udpRTPListener.lastFrameTime), 0)
|
lft := time.Unix(atomic.LoadInt64(cct.udpRTPListener.lastFrameTime), 0)
|
||||||
if now.Sub(lft) < cc.conf.ReadTimeout {
|
if now.Sub(lft) < cc.c.ReadTimeout {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
lft = time.Unix(atomic.LoadInt64(cct.udpRTCPListener.lastFrameTime), 0)
|
lft = time.Unix(atomic.LoadInt64(cct.udpRTCPListener.lastFrameTime), 0)
|
||||||
if now.Sub(lft) < cc.conf.ReadTimeout {
|
if now.Sub(lft) < cc.c.ReadTimeout {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -222,7 +222,7 @@ func (cc *ClientConn) backgroundPlayTCP() error {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
reportTicker := time.NewTicker(cc.conf.receiverReportPeriod)
|
reportTicker := time.NewTicker(cc.c.receiverReportPeriod)
|
||||||
defer reportTicker.Stop()
|
defer reportTicker.Stop()
|
||||||
|
|
||||||
checkStreamTicker := time.NewTicker(clientConnCheckStreamPeriod)
|
checkStreamTicker := time.NewTicker(clientConnCheckStreamPeriod)
|
||||||
@@ -239,7 +239,7 @@ func (cc *ClientConn) backgroundPlayTCP() error {
|
|||||||
now := time.Now()
|
now := time.Now()
|
||||||
for trackID, cct := range cc.tracks {
|
for trackID, cct := range cc.tracks {
|
||||||
r := cct.rtcpReceiver.Report(now)
|
r := cct.rtcpReceiver.Report(now)
|
||||||
cc.nconn.SetWriteDeadline(time.Now().Add(cc.conf.WriteTimeout))
|
cc.nconn.SetWriteDeadline(time.Now().Add(cc.c.WriteTimeout))
|
||||||
frame := base.InterleavedFrame{
|
frame := base.InterleavedFrame{
|
||||||
TrackID: trackID,
|
TrackID: trackID,
|
||||||
StreamType: StreamTypeRTCP,
|
StreamType: StreamTypeRTCP,
|
||||||
@@ -252,7 +252,7 @@ func (cc *ClientConn) backgroundPlayTCP() error {
|
|||||||
inTimeout := func() bool {
|
inTimeout := func() bool {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
lft := time.Unix(atomic.LoadInt64(&lastFrameTime), 0)
|
lft := time.Unix(atomic.LoadInt64(&lastFrameTime), 0)
|
||||||
return now.Sub(lft) >= cc.conf.ReadTimeout
|
return now.Sub(lft) >= cc.c.ReadTimeout
|
||||||
}()
|
}()
|
||||||
if inTimeout {
|
if inTimeout {
|
||||||
cc.nconn.SetReadDeadline(time.Now())
|
cc.nconn.SetReadDeadline(time.Now())
|
||||||
|
@@ -357,7 +357,7 @@ func TestClientRead(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
conf := ClientConf{
|
c := &Client{
|
||||||
StreamProtocol: func() *StreamProtocol {
|
StreamProtocol: func() *StreamProtocol {
|
||||||
if ca.proto == "udp" {
|
if ca.proto == "udp" {
|
||||||
v := StreamProtocolUDP
|
v := StreamProtocolUDP
|
||||||
@@ -368,7 +368,7 @@ func TestClientRead(t *testing.T) {
|
|||||||
}(),
|
}(),
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, err := conf.DialRead(scheme + "://localhost:8554/teststream")
|
conn, err := c.DialRead(scheme + "://localhost:8554/teststream")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
done := conn.ReadFrames(func(id int, streamType StreamType, payload []byte) {
|
done := conn.ReadFrames(func(id int, streamType StreamType, payload []byte) {
|
||||||
@@ -593,11 +593,11 @@ func TestClientReadAnyPort(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}()
|
}()
|
||||||
|
|
||||||
conf := ClientConf{
|
c := &Client{
|
||||||
AnyPortEnable: true,
|
AnyPortEnable: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, err := conf.DialRead("rtsp://localhost:8554/teststream")
|
conn, err := c.DialRead("rtsp://localhost:8554/teststream")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
frameRecv := make(chan struct{})
|
frameRecv := make(chan struct{})
|
||||||
@@ -877,11 +877,11 @@ func TestClientReadAutomaticProtocol(t *testing.T) {
|
|||||||
conn.Close()
|
conn.Close()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
conf := ClientConf{
|
c := &Client{
|
||||||
ReadTimeout: 1 * time.Second,
|
ReadTimeout: 1 * time.Second,
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, err := conf.DialRead("rtsp://localhost:8554/teststream")
|
conn, err := c.DialRead("rtsp://localhost:8554/teststream")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
frameRecv := make(chan struct{})
|
frameRecv := make(chan struct{})
|
||||||
@@ -1213,7 +1213,7 @@ func TestClientReadPause(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
conf := ClientConf{
|
c := &Client{
|
||||||
StreamProtocol: func() *StreamProtocol {
|
StreamProtocol: func() *StreamProtocol {
|
||||||
if proto == "udp" {
|
if proto == "udp" {
|
||||||
v := StreamProtocolUDP
|
v := StreamProtocolUDP
|
||||||
@@ -1224,7 +1224,7 @@ func TestClientReadPause(t *testing.T) {
|
|||||||
}(),
|
}(),
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, err := conf.DialRead("rtsp://localhost:8554/teststream")
|
conn, err := c.DialRead("rtsp://localhost:8554/teststream")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
firstFrame := int32(0)
|
firstFrame := int32(0)
|
||||||
@@ -1398,7 +1398,7 @@ func TestClientReadRTCPReport(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
conf := ClientConf{
|
c := &Client{
|
||||||
StreamProtocol: func() *StreamProtocol {
|
StreamProtocol: func() *StreamProtocol {
|
||||||
v := StreamProtocolTCP
|
v := StreamProtocolTCP
|
||||||
return &v
|
return &v
|
||||||
@@ -1406,7 +1406,7 @@ func TestClientReadRTCPReport(t *testing.T) {
|
|||||||
receiverReportPeriod: 1 * time.Second,
|
receiverReportPeriod: 1 * time.Second,
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, err := conf.DialRead("rtsp://localhost:8554/teststream")
|
conn, err := c.DialRead("rtsp://localhost:8554/teststream")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
recv := 0
|
recv := 0
|
||||||
@@ -1552,7 +1552,7 @@ func TestClientReadErrorTimeout(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
conf := ClientConf{
|
c := &Client{
|
||||||
StreamProtocol: func() *StreamProtocol {
|
StreamProtocol: func() *StreamProtocol {
|
||||||
switch proto {
|
switch proto {
|
||||||
case "udp":
|
case "udp":
|
||||||
@@ -1569,7 +1569,7 @@ func TestClientReadErrorTimeout(t *testing.T) {
|
|||||||
ReadTimeout: 1 * time.Second,
|
ReadTimeout: 1 * time.Second,
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, err := conf.DialRead("rtsp://localhost:8554/teststream")
|
conn, err := c.DialRead("rtsp://localhost:8554/teststream")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
@@ -1694,14 +1694,14 @@ func TestClientReadIgnoreTCPInvalidTrack(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
conf := ClientConf{
|
c := &Client{
|
||||||
StreamProtocol: func() *StreamProtocol {
|
StreamProtocol: func() *StreamProtocol {
|
||||||
v := StreamProtocolTCP
|
v := StreamProtocolTCP
|
||||||
return &v
|
return &v
|
||||||
}(),
|
}(),
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, err := conf.DialRead("rtsp://localhost:8554/teststream")
|
conn, err := c.DialRead("rtsp://localhost:8554/teststream")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
|
@@ -30,7 +30,7 @@ type clientConnUDPListener struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func newClientConnUDPListener(cc *ClientConn, port int) (*clientConnUDPListener, error) {
|
func newClientConnUDPListener(cc *ClientConn, port int) (*clientConnUDPListener, error) {
|
||||||
pc, err := cc.conf.ListenPacket("udp", ":"+strconv.FormatInt(int64(port), 10))
|
pc, err := cc.c.ListenPacket("udp", ":"+strconv.FormatInt(int64(port), 10))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -43,7 +43,7 @@ func newClientConnUDPListener(cc *ClientConn, port int) (*clientConnUDPListener,
|
|||||||
return &clientConnUDPListener{
|
return &clientConnUDPListener{
|
||||||
cc: cc,
|
cc: cc,
|
||||||
pc: pc,
|
pc: pc,
|
||||||
frameBuffer: multibuffer.New(uint64(cc.conf.ReadBufferCount), uint64(cc.conf.ReadBufferSize)),
|
frameBuffer: multibuffer.New(uint64(cc.c.ReadBufferCount), uint64(cc.c.ReadBufferSize)),
|
||||||
lastFrameTime: func() *int64 {
|
lastFrameTime: func() *int64 {
|
||||||
v := int64(0)
|
v := int64(0)
|
||||||
return &v
|
return &v
|
||||||
@@ -114,7 +114,7 @@ func (l *clientConnUDPListener) run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *clientConnUDPListener) write(buf []byte) error {
|
func (l *clientConnUDPListener) write(buf []byte) error {
|
||||||
l.pc.SetWriteDeadline(time.Now().Add(l.cc.conf.WriteTimeout))
|
l.pc.SetWriteDeadline(time.Now().Add(l.cc.c.WriteTimeout))
|
||||||
_, err := l.pc.WriteTo(buf, &net.UDPAddr{
|
_, err := l.pc.WriteTo(buf, &net.UDPAddr{
|
||||||
IP: l.remoteIP,
|
IP: l.remoteIP,
|
||||||
Zone: l.remoteZone,
|
Zone: l.remoteZone,
|
||||||
|
@@ -41,8 +41,8 @@ func main() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ClientConf allows to set additional client options
|
// Client allows to set additional client options
|
||||||
conf := gortsplib.ClientConf{
|
c := &gortsplib.Client{
|
||||||
// the stream protocol (UDP or TCP). If nil, it is chosen automatically
|
// the stream protocol (UDP or TCP). If nil, it is chosen automatically
|
||||||
StreamProtocol: nil,
|
StreamProtocol: nil,
|
||||||
// timeout of read operations
|
// timeout of read operations
|
||||||
@@ -52,7 +52,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// connect to the server and start publishing the track
|
// connect to the server and start publishing the track
|
||||||
conn, err := conf.DialPublish("rtsp://localhost:8554/mystream",
|
conn, err := c.DialPublish("rtsp://localhost:8554/mystream",
|
||||||
gortsplib.Tracks{track})
|
gortsplib.Tracks{track})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
@@ -12,8 +12,8 @@ import (
|
|||||||
// 2. connect to a RTSP server and read all tracks on a path
|
// 2. connect to a RTSP server and read all tracks on a path
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// ClientConf allows to set additional client options
|
// Client allows to set additional client options
|
||||||
conf := gortsplib.ClientConf{
|
c := &gortsplib.Client{
|
||||||
// the stream protocol (UDP or TCP). If nil, it is chosen automatically
|
// the stream protocol (UDP or TCP). If nil, it is chosen automatically
|
||||||
StreamProtocol: nil,
|
StreamProtocol: nil,
|
||||||
// timeout of read operations
|
// timeout of read operations
|
||||||
@@ -23,7 +23,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// connect to the server and start reading all tracks
|
// connect to the server and start reading all tracks
|
||||||
conn, err := conf.DialRead("rtsp://localhost:8554/mystream")
|
conn, err := c.DialRead("rtsp://localhost:8554/mystream")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user