mirror of
https://github.com/aler9/gortsplib
synced 2025-10-06 07:37:07 +08:00
improve performance
This commit is contained in:
@@ -80,12 +80,12 @@ type ConnClient struct {
|
|||||||
tcpFrameBuffer *multibuffer.MultiBuffer
|
tcpFrameBuffer *multibuffer.MultiBuffer
|
||||||
getParameterSupported bool
|
getParameterSupported bool
|
||||||
backgroundError error
|
backgroundError error
|
||||||
|
writeFrameMutex sync.RWMutex
|
||||||
|
writeFrameOpen bool
|
||||||
|
readCB func(int, StreamType, []byte, error)
|
||||||
|
|
||||||
backgroundTerminate chan struct{}
|
backgroundTerminate chan struct{}
|
||||||
backgroundDone chan struct{}
|
backgroundDone chan struct{}
|
||||||
readFrame chan base.InterleavedFrame
|
|
||||||
writeFrameMutex sync.RWMutex
|
|
||||||
writeFrameOpen bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close closes all the ConnClient resources.
|
// Close closes all the ConnClient resources.
|
||||||
|
@@ -61,7 +61,6 @@ func (c *ConnClient) Record() (*base.Response, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.state = connClientStateRecord
|
c.state = connClientStateRecord
|
||||||
|
|
||||||
c.writeFrameOpen = true
|
c.writeFrameOpen = true
|
||||||
c.backgroundTerminate = make(chan struct{})
|
c.backgroundTerminate = make(chan struct{})
|
||||||
c.backgroundDone = make(chan struct{})
|
c.backgroundDone = make(chan struct{})
|
||||||
|
@@ -30,42 +30,19 @@ func (c *ConnClient) Play() (*base.Response, error) {
|
|||||||
return nil, fmt.Errorf("bad status code: %d (%s)", res.StatusCode, res.StatusMessage)
|
return nil, fmt.Errorf("bad status code: %d (%s)", res.StatusCode, res.StatusMessage)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.state = connClientStatePlay
|
|
||||||
|
|
||||||
c.readFrame = make(chan base.InterleavedFrame)
|
|
||||||
c.backgroundTerminate = make(chan struct{})
|
|
||||||
c.backgroundDone = make(chan struct{})
|
|
||||||
|
|
||||||
if *c.streamProtocol == StreamProtocolUDP {
|
|
||||||
// open the firewall by sending packets to the counterpart
|
|
||||||
for trackId := range c.udpRtpListeners {
|
|
||||||
c.udpRtpListeners[trackId].write(
|
|
||||||
[]byte{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})
|
|
||||||
|
|
||||||
c.udpRtcpListeners[trackId].write(
|
|
||||||
[]byte{0x80, 0xc9, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00})
|
|
||||||
}
|
|
||||||
|
|
||||||
go c.backgroundPlayUDP()
|
|
||||||
} else {
|
|
||||||
go c.backgroundPlayTCP()
|
|
||||||
}
|
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConnClient) backgroundPlayUDP() {
|
func (c *ConnClient) backgroundPlayUDP() {
|
||||||
defer close(c.backgroundDone)
|
defer close(c.backgroundDone)
|
||||||
|
|
||||||
readFrame := c.readFrame
|
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
for trackId := range c.udpRtpListeners {
|
for trackId := range c.udpRtpListeners {
|
||||||
c.udpRtpListeners[trackId].stop()
|
c.udpRtpListeners[trackId].stop()
|
||||||
c.udpRtcpListeners[trackId].stop()
|
c.udpRtcpListeners[trackId].stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
close(readFrame)
|
c.readCB(0, 0, nil, c.backgroundError)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
for trackId := range c.udpRtpListeners {
|
for trackId := range c.udpRtpListeners {
|
||||||
@@ -100,10 +77,6 @@ func (c *ConnClient) backgroundPlayUDP() {
|
|||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-c.backgroundTerminate:
|
case <-c.backgroundTerminate:
|
||||||
go func() {
|
|
||||||
for range readFrame {
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
c.nconn.SetReadDeadline(time.Now())
|
c.nconn.SetReadDeadline(time.Now())
|
||||||
<-readerDone
|
<-readerDone
|
||||||
c.backgroundError = fmt.Errorf("terminated")
|
c.backgroundError = fmt.Errorf("terminated")
|
||||||
@@ -129,10 +102,6 @@ func (c *ConnClient) backgroundPlayUDP() {
|
|||||||
SkipResponse: true,
|
SkipResponse: true,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
go func() {
|
|
||||||
for range readFrame {
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
c.nconn.SetReadDeadline(time.Now())
|
c.nconn.SetReadDeadline(time.Now())
|
||||||
<-readerDone
|
<-readerDone
|
||||||
c.backgroundError = err
|
c.backgroundError = err
|
||||||
@@ -146,10 +115,6 @@ func (c *ConnClient) backgroundPlayUDP() {
|
|||||||
last := time.Unix(atomic.LoadInt64(lastUnix), 0)
|
last := time.Unix(atomic.LoadInt64(lastUnix), 0)
|
||||||
|
|
||||||
if now.Sub(last) >= c.d.ReadTimeout {
|
if now.Sub(last) >= c.d.ReadTimeout {
|
||||||
go func() {
|
|
||||||
for range readFrame {
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
c.nconn.SetReadDeadline(time.Now())
|
c.nconn.SetReadDeadline(time.Now())
|
||||||
<-readerDone
|
<-readerDone
|
||||||
c.backgroundError = fmt.Errorf("no packets received recently (maybe there's a firewall/NAT in between)")
|
c.backgroundError = fmt.Errorf("no packets received recently (maybe there's a firewall/NAT in between)")
|
||||||
@@ -158,10 +123,6 @@ func (c *ConnClient) backgroundPlayUDP() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case err := <-readerDone:
|
case err := <-readerDone:
|
||||||
go func() {
|
|
||||||
for range readFrame {
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
c.backgroundError = err
|
c.backgroundError = err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -171,11 +132,7 @@ func (c *ConnClient) backgroundPlayUDP() {
|
|||||||
func (c *ConnClient) backgroundPlayTCP() {
|
func (c *ConnClient) backgroundPlayTCP() {
|
||||||
defer close(c.backgroundDone)
|
defer close(c.backgroundDone)
|
||||||
|
|
||||||
readFrame := c.readFrame
|
defer c.readCB(0, 0, nil, c.backgroundError)
|
||||||
|
|
||||||
defer func() {
|
|
||||||
close(readFrame)
|
|
||||||
}()
|
|
||||||
|
|
||||||
readerDone := make(chan error)
|
readerDone := make(chan error)
|
||||||
go func() {
|
go func() {
|
||||||
@@ -192,7 +149,7 @@ func (c *ConnClient) backgroundPlayTCP() {
|
|||||||
|
|
||||||
c.rtcpReceivers[frame.TrackId].OnFrame(frame.StreamType, frame.Content)
|
c.rtcpReceivers[frame.TrackId].OnFrame(frame.StreamType, frame.Content)
|
||||||
|
|
||||||
readFrame <- frame
|
c.readCB(frame.TrackId, frame.StreamType, frame.Content, nil)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@@ -202,10 +159,6 @@ func (c *ConnClient) backgroundPlayTCP() {
|
|||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-c.backgroundTerminate:
|
case <-c.backgroundTerminate:
|
||||||
go func() {
|
|
||||||
for range readFrame {
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
c.nconn.SetReadDeadline(time.Now())
|
c.nconn.SetReadDeadline(time.Now())
|
||||||
<-readerDone
|
<-readerDone
|
||||||
c.backgroundError = fmt.Errorf("terminated")
|
c.backgroundError = fmt.Errorf("terminated")
|
||||||
@@ -224,23 +177,31 @@ func (c *ConnClient) backgroundPlayTCP() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case err := <-readerDone:
|
case err := <-readerDone:
|
||||||
go func() {
|
|
||||||
for range readFrame {
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
c.backgroundError = err
|
c.backgroundError = err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadFrame reads a frame.
|
// OnFrame sets a callback that is called when a frame is received.
|
||||||
// This can be used only after Play().
|
func (c *ConnClient) OnFrame(cb func(int, StreamType, []byte, error)) {
|
||||||
func (c *ConnClient) ReadFrame() (int, StreamType, []byte, error) {
|
c.state = connClientStatePlay
|
||||||
f, ok := <-c.readFrame
|
c.readCB = cb
|
||||||
if !ok {
|
c.backgroundTerminate = make(chan struct{})
|
||||||
return 0, 0, nil, c.backgroundError
|
c.backgroundDone = make(chan struct{})
|
||||||
}
|
|
||||||
|
|
||||||
return f.TrackId, f.StreamType, f.Content, nil
|
if *c.streamProtocol == StreamProtocolUDP {
|
||||||
|
// open the firewall by sending packets to the counterpart
|
||||||
|
for trackId := range c.udpRtpListeners {
|
||||||
|
c.udpRtpListeners[trackId].write(
|
||||||
|
[]byte{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})
|
||||||
|
|
||||||
|
c.udpRtcpListeners[trackId].write(
|
||||||
|
[]byte{0x80, 0xc9, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00})
|
||||||
|
}
|
||||||
|
|
||||||
|
go c.backgroundPlayUDP()
|
||||||
|
} else {
|
||||||
|
go c.backgroundPlayTCP()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -6,7 +6,6 @@ import (
|
|||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/aler9/gortsplib/pkg/base"
|
|
||||||
"github.com/aler9/gortsplib/pkg/multibuffer"
|
"github.com/aler9/gortsplib/pkg/multibuffer"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -33,7 +32,7 @@ func newConnClientUDPListener(c *ConnClient, port int) (*connClientUDPListener,
|
|||||||
return &connClientUDPListener{
|
return &connClientUDPListener{
|
||||||
c: c,
|
c: c,
|
||||||
pc: pc,
|
pc: pc,
|
||||||
udpFrameBuffer: multibuffer.New(c.d.ReadBufferCount+1, 2048),
|
udpFrameBuffer: multibuffer.New(c.d.ReadBufferCount, 2048),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,11 +75,7 @@ func (l *connClientUDPListener) run() {
|
|||||||
|
|
||||||
l.c.rtcpReceivers[l.trackId].OnFrame(l.streamType, buf[:n])
|
l.c.rtcpReceivers[l.trackId].OnFrame(l.streamType, buf[:n])
|
||||||
|
|
||||||
l.c.readFrame <- base.InterleavedFrame{
|
l.c.readCB(l.trackId, l.streamType, buf[:n], nil)
|
||||||
TrackId: l.trackId,
|
|
||||||
StreamType: l.streamType,
|
|
||||||
Content: buf[:n],
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -96,7 +96,7 @@ func (d Dialer) Dial(host string) (*ConnClient, error) {
|
|||||||
udpLastFrameTimes: make(map[int]*int64),
|
udpLastFrameTimes: make(map[int]*int64),
|
||||||
udpRtpListeners: make(map[int]*connClientUDPListener),
|
udpRtpListeners: make(map[int]*connClientUDPListener),
|
||||||
udpRtcpListeners: make(map[int]*connClientUDPListener),
|
udpRtcpListeners: make(map[int]*connClientUDPListener),
|
||||||
tcpFrameBuffer: multibuffer.New(d.ReadBufferCount+1, clientTCPFrameReadBufferSize),
|
tcpFrameBuffer: multibuffer.New(d.ReadBufferCount, clientTCPFrameReadBufferSize),
|
||||||
backgroundError: fmt.Errorf("not running"),
|
backgroundError: fmt.Errorf("not running"),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
193
dialer_test.go
193
dialer_test.go
@@ -5,6 +5,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -58,56 +59,6 @@ func (c *container) wait() int {
|
|||||||
return int(code)
|
return int(code)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDialRead(t *testing.T) {
|
|
||||||
for _, proto := range []string{
|
|
||||||
"udp",
|
|
||||||
"tcp",
|
|
||||||
} {
|
|
||||||
t.Run(proto, func(t *testing.T) {
|
|
||||||
cnt1, err := newContainer("rtsp-simple-server", "server", []string{"{}"})
|
|
||||||
require.NoError(t, err)
|
|
||||||
defer cnt1.close()
|
|
||||||
|
|
||||||
time.Sleep(1 * time.Second)
|
|
||||||
|
|
||||||
cnt2, err := newContainer("ffmpeg", "publish", []string{
|
|
||||||
"-re",
|
|
||||||
"-stream_loop", "-1",
|
|
||||||
"-i", "/emptyvideo.ts",
|
|
||||||
"-c", "copy",
|
|
||||||
"-f", "rtsp",
|
|
||||||
"-rtsp_transport", "udp",
|
|
||||||
"rtsp://localhost:8554/teststream",
|
|
||||||
})
|
|
||||||
require.NoError(t, err)
|
|
||||||
defer cnt2.close()
|
|
||||||
|
|
||||||
time.Sleep(1 * time.Second)
|
|
||||||
|
|
||||||
dialer := func() Dialer {
|
|
||||||
if proto == "udp" {
|
|
||||||
return Dialer{}
|
|
||||||
}
|
|
||||||
return Dialer{StreamProtocol: StreamProtocolTCP}
|
|
||||||
}()
|
|
||||||
|
|
||||||
conn, err := dialer.DialRead("rtsp://localhost:8554/teststream")
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
id, typ, _, err := conn.ReadFrame()
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
require.Equal(t, 0, id)
|
|
||||||
require.Equal(t, StreamTypeRtp, typ)
|
|
||||||
|
|
||||||
conn.Close()
|
|
||||||
|
|
||||||
_, _, _, err = conn.ReadFrame()
|
|
||||||
require.Error(t, err)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestDialReadParallel(t *testing.T) {
|
func TestDialReadParallel(t *testing.T) {
|
||||||
for _, proto := range []string{
|
for _, proto := range []string{
|
||||||
"udp",
|
"udp",
|
||||||
@@ -144,27 +95,35 @@ func TestDialReadParallel(t *testing.T) {
|
|||||||
conn, err := dialer.DialRead("rtsp://localhost:8554/teststream")
|
conn, err := dialer.DialRead("rtsp://localhost:8554/teststream")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
var firstFrame int32
|
||||||
|
frameRecv := make(chan struct{})
|
||||||
readerDone := make(chan struct{})
|
readerDone := make(chan struct{})
|
||||||
go func() {
|
conn.OnFrame(func(id int, typ StreamType, content []byte, err error) {
|
||||||
defer close(readerDone)
|
if err != nil {
|
||||||
|
close(readerDone)
|
||||||
for {
|
return
|
||||||
_, _, _, err := conn.ReadFrame()
|
|
||||||
if err != nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}()
|
|
||||||
|
|
||||||
time.Sleep(1 * time.Second)
|
if atomic.SwapInt32(&firstFrame, 1) == 0 {
|
||||||
|
close(frameRecv)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
<-frameRecv
|
||||||
conn.Close()
|
conn.Close()
|
||||||
<-readerDone
|
<-readerDone
|
||||||
|
|
||||||
|
readerDone = make(chan struct{})
|
||||||
|
conn.OnFrame(func(id int, typ StreamType, content []byte, err error) {
|
||||||
|
require.Error(t, err)
|
||||||
|
close(readerDone)
|
||||||
|
})
|
||||||
|
<-readerDone
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDialReadRedirect(t *testing.T) {
|
func TestDialReadRedirectParallel(t *testing.T) {
|
||||||
cnt1, err := newContainer("rtsp-simple-server", "server", []string{
|
cnt1, err := newContainer("rtsp-simple-server", "server", []string{
|
||||||
"paths:\n" +
|
"paths:\n" +
|
||||||
" path1:\n" +
|
" path1:\n" +
|
||||||
@@ -193,62 +152,24 @@ func TestDialReadRedirect(t *testing.T) {
|
|||||||
|
|
||||||
conn, err := DialRead("rtsp://localhost:8554/path1")
|
conn, err := DialRead("rtsp://localhost:8554/path1")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
_, _, _, err = conn.ReadFrame()
|
var firstFrame int32
|
||||||
require.NoError(t, err)
|
frameRecv := make(chan struct{})
|
||||||
}
|
readerDone := make(chan struct{})
|
||||||
|
conn.OnFrame(func(id int, typ StreamType, content []byte, err error) {
|
||||||
|
if err != nil {
|
||||||
|
close(readerDone)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func TestDialReadPause(t *testing.T) {
|
if atomic.SwapInt32(&firstFrame, 1) == 0 {
|
||||||
for _, proto := range []string{
|
close(frameRecv)
|
||||||
"udp",
|
}
|
||||||
"tcp",
|
})
|
||||||
} {
|
|
||||||
t.Run(proto, func(t *testing.T) {
|
|
||||||
cnt1, err := newContainer("rtsp-simple-server", "server", []string{"{}"})
|
|
||||||
require.NoError(t, err)
|
|
||||||
defer cnt1.close()
|
|
||||||
|
|
||||||
time.Sleep(1 * time.Second)
|
<-frameRecv
|
||||||
|
conn.Close()
|
||||||
cnt2, err := newContainer("ffmpeg", "publish", []string{
|
<-readerDone
|
||||||
"-re",
|
|
||||||
"-stream_loop", "-1",
|
|
||||||
"-i", "/emptyvideo.ts",
|
|
||||||
"-c", "copy",
|
|
||||||
"-f", "rtsp",
|
|
||||||
"-rtsp_transport", "udp",
|
|
||||||
"rtsp://localhost:8554/teststream",
|
|
||||||
})
|
|
||||||
require.NoError(t, err)
|
|
||||||
defer cnt2.close()
|
|
||||||
|
|
||||||
time.Sleep(1 * time.Second)
|
|
||||||
|
|
||||||
dialer := func() Dialer {
|
|
||||||
if proto == "udp" {
|
|
||||||
return Dialer{}
|
|
||||||
}
|
|
||||||
return Dialer{StreamProtocol: StreamProtocolTCP}
|
|
||||||
}()
|
|
||||||
|
|
||||||
conn, err := dialer.DialRead("rtsp://localhost:8554/teststream")
|
|
||||||
require.NoError(t, err)
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
_, _, _, err = conn.ReadFrame()
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
_, err = conn.Pause()
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
_, err = conn.Play()
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
_, _, _, err = conn.ReadFrame()
|
|
||||||
require.NoError(t, err)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDialReadPauseParallel(t *testing.T) {
|
func TestDialReadPauseParallel(t *testing.T) {
|
||||||
@@ -287,30 +208,50 @@ func TestDialReadPauseParallel(t *testing.T) {
|
|||||||
conn, err := dialer.DialRead("rtsp://localhost:8554/teststream")
|
conn, err := dialer.DialRead("rtsp://localhost:8554/teststream")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
firstFrame := int32(0)
|
||||||
|
frameRecv := make(chan struct{})
|
||||||
readerDone := make(chan struct{})
|
readerDone := make(chan struct{})
|
||||||
go func() {
|
conn.OnFrame(func(id int, typ StreamType, content []byte, err error) {
|
||||||
defer close(readerDone)
|
if err != nil {
|
||||||
|
close(readerDone)
|
||||||
for {
|
return
|
||||||
_, _, _, err := conn.ReadFrame()
|
|
||||||
if err != nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}()
|
|
||||||
|
|
||||||
time.Sleep(1 * time.Second)
|
if atomic.SwapInt32(&firstFrame, 1) == 0 {
|
||||||
|
close(frameRecv)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
<-frameRecv
|
||||||
_, err = conn.Pause()
|
_, err = conn.Pause()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
<-readerDone
|
<-readerDone
|
||||||
|
|
||||||
|
_, err = conn.Play()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
firstFrame = int32(0)
|
||||||
|
frameRecv = make(chan struct{})
|
||||||
|
readerDone = make(chan struct{})
|
||||||
|
conn.OnFrame(func(id int, typ StreamType, content []byte, err error) {
|
||||||
|
if err != nil {
|
||||||
|
close(readerDone)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if atomic.SwapInt32(&firstFrame, 1) == 0 {
|
||||||
|
close(frameRecv)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
<-frameRecv
|
||||||
conn.Close()
|
conn.Close()
|
||||||
|
<-readerDone
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDialPublish(t *testing.T) {
|
func TestDialPublishSerial(t *testing.T) {
|
||||||
for _, proto := range []string{
|
for _, proto := range []string{
|
||||||
"udp",
|
"udp",
|
||||||
"tcp",
|
"tcp",
|
||||||
@@ -476,7 +417,7 @@ func TestDialPublishParallel(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDialPublishPause(t *testing.T) {
|
func TestDialPublishPauseSerial(t *testing.T) {
|
||||||
for _, proto := range []string{
|
for _, proto := range []string{
|
||||||
"udp",
|
"udp",
|
||||||
"tcp",
|
"tcp",
|
||||||
|
@@ -22,15 +22,18 @@ func main() {
|
|||||||
}
|
}
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
|
readerDone := make(chan struct{})
|
||||||
|
defer func() { <-readerDone }()
|
||||||
|
|
||||||
// read frames
|
// read frames
|
||||||
for {
|
conn.OnFrame(func(id int, typ gortsplib.StreamType, buf []byte, err error) {
|
||||||
id, typ, buf, err := conn.ReadFrame()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("connection is closed (%s)\n", err)
|
fmt.Printf("ERR: %v\n", err)
|
||||||
break
|
close(readerDone)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("frame from track %d, type %v: %v\n",
|
fmt.Printf("frame from track %d, type %v: %v\n",
|
||||||
id, typ, buf)
|
id, typ, buf)
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
@@ -19,15 +19,18 @@ func main() {
|
|||||||
}
|
}
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
|
readerDone := make(chan struct{})
|
||||||
|
defer func() { <-readerDone }()
|
||||||
|
|
||||||
// read frames
|
// read frames
|
||||||
for {
|
conn.OnFrame(func(id int, typ gortsplib.StreamType, buf []byte, err error) {
|
||||||
id, typ, buf, err := conn.ReadFrame()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("connection is closed (%s)\n", err)
|
fmt.Printf("ERR: %v\n", err)
|
||||||
break
|
close(readerDone)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("frame from track %d, type %v: %v\n",
|
fmt.Printf("frame from track %d, type %v: %v\n",
|
||||||
id, typ, buf)
|
id, typ, buf)
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user