client: remove channel from ReadFrames()

This commit is contained in:
aler9
2021-05-10 17:37:27 +02:00
parent 510bcfe2d7
commit 5eb82448a7
8 changed files with 104 additions and 65 deletions

View File

@@ -181,12 +181,16 @@ func TestClientPublishSerial(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
recvDone := make(chan struct{}) recvDone := make(chan struct{})
done := conn.ReadFrames(func(trackID int, streamType StreamType, payload []byte) { done := make(chan struct{})
go func() {
defer close(done)
conn.ReadFrames(func(trackID int, streamType StreamType, payload []byte) {
require.Equal(t, 0, trackID) require.Equal(t, 0, trackID)
require.Equal(t, StreamTypeRTCP, streamType) require.Equal(t, StreamTypeRTCP, streamType)
require.Equal(t, []byte{0x05, 0x06, 0x07, 0x08}, payload) require.Equal(t, []byte{0x05, 0x06, 0x07, 0x08}, payload)
close(recvDone) close(recvDone)
}) })
}()
err = conn.WriteFrame(track.ID, StreamTypeRTP, err = conn.WriteFrame(track.ID, StreamTypeRTP,
[]byte{0x01, 0x02, 0x03, 0x04}) []byte{0x01, 0x02, 0x03, 0x04})

View File

@@ -359,7 +359,10 @@ func TestClientRead(t *testing.T) {
conn, err := c.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 := make(chan struct{})
go func() {
defer close(done)
conn.ReadFrames(func(id int, streamType StreamType, payload []byte) {
require.Equal(t, 0, id) require.Equal(t, 0, id)
require.Equal(t, StreamTypeRTP, streamType) require.Equal(t, StreamTypeRTP, streamType)
require.Equal(t, []byte{0x01, 0x02, 0x03, 0x04}, payload) require.Equal(t, []byte{0x01, 0x02, 0x03, 0x04}, payload)
@@ -367,12 +370,13 @@ func TestClientRead(t *testing.T) {
err = conn.WriteFrame(0, StreamTypeRTCP, []byte{0x05, 0x06, 0x07, 0x08}) err = conn.WriteFrame(0, StreamTypeRTCP, []byte{0x05, 0x06, 0x07, 0x08})
require.NoError(t, err) require.NoError(t, err)
}) })
}()
<-frameRecv <-frameRecv
conn.Close() conn.Close()
<-done <-done
<-conn.ReadFrames(func(id int, typ StreamType, payload []byte) { conn.ReadFrames(func(id int, typ StreamType, payload []byte) {
}) })
}) })
} }
@@ -590,9 +594,13 @@ func TestClientReadAnyPort(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
frameRecv := make(chan struct{}) frameRecv := make(chan struct{})
done := conn.ReadFrames(func(id int, typ StreamType, payload []byte) { done := make(chan struct{})
go func() {
defer close(done)
conn.ReadFrames(func(id int, typ StreamType, payload []byte) {
close(frameRecv) close(frameRecv)
}) })
}()
<-frameRecv <-frameRecv
conn.Close() conn.Close()
@@ -704,9 +712,13 @@ func TestClientReadAutomaticProtocol(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
frameRecv := make(chan struct{}) frameRecv := make(chan struct{})
done := conn.ReadFrames(func(id int, typ StreamType, payload []byte) { done := make(chan struct{})
go func() {
defer close(done)
conn.ReadFrames(func(id int, typ StreamType, payload []byte) {
close(frameRecv) close(frameRecv)
}) })
}()
<-frameRecv <-frameRecv
conn.Close() conn.Close()
@@ -872,9 +884,13 @@ func TestClientReadAutomaticProtocol(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
frameRecv := make(chan struct{}) frameRecv := make(chan struct{})
done := conn.ReadFrames(func(id int, typ StreamType, payload []byte) { done := make(chan struct{})
go func() {
defer close(done)
conn.ReadFrames(func(id int, typ StreamType, payload []byte) {
close(frameRecv) close(frameRecv)
}) })
}()
<-frameRecv <-frameRecv
conn.Close() conn.Close()
@@ -1013,9 +1029,13 @@ func TestClientReadRedirect(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
frameRecv := make(chan struct{}) frameRecv := make(chan struct{})
done := conn.ReadFrames(func(id int, typ StreamType, payload []byte) { done := make(chan struct{})
go func() {
defer close(done)
conn.ReadFrames(func(id int, typ StreamType, payload []byte) {
close(frameRecv) close(frameRecv)
}) })
}()
<-frameRecv <-frameRecv
conn.Close() conn.Close()
@@ -1214,18 +1234,22 @@ func TestClientReadPause(t *testing.T) {
firstFrame := int32(0) firstFrame := int32(0)
frameRecv := make(chan struct{}) frameRecv := make(chan struct{})
done := conn.ReadFrames(func(id int, typ StreamType, payload []byte) { done := make(chan struct{})
go func() {
defer close(done)
conn.ReadFrames(func(id int, typ StreamType, payload []byte) {
if atomic.SwapInt32(&firstFrame, 1) == 0 { if atomic.SwapInt32(&firstFrame, 1) == 0 {
close(frameRecv) close(frameRecv)
} }
}) })
}()
<-frameRecv <-frameRecv
_, err = conn.Pause() _, err = conn.Pause()
require.NoError(t, err) require.NoError(t, err)
<-done <-done
<-conn.ReadFrames(func(id int, typ StreamType, payload []byte) { conn.ReadFrames(func(id int, typ StreamType, payload []byte) {
}) })
_, err = conn.Play() _, err = conn.Play()
@@ -1233,11 +1257,15 @@ func TestClientReadPause(t *testing.T) {
firstFrame = int32(0) firstFrame = int32(0)
frameRecv = make(chan struct{}) frameRecv = make(chan struct{})
done = conn.ReadFrames(func(id int, typ StreamType, payload []byte) { done = make(chan struct{})
go func() {
defer close(done)
conn.ReadFrames(func(id int, typ StreamType, payload []byte) {
if atomic.SwapInt32(&firstFrame, 1) == 0 { if atomic.SwapInt32(&firstFrame, 1) == 0 {
close(frameRecv) close(frameRecv)
} }
}) })
}()
<-frameRecv <-frameRecv
conn.Close() conn.Close()
@@ -1398,12 +1426,16 @@ func TestClientReadRTCPReport(t *testing.T) {
recv := 0 recv := 0
recvDone := make(chan struct{}) recvDone := make(chan struct{})
done := conn.ReadFrames(func(id int, typ StreamType, payload []byte) { done := make(chan struct{})
go func() {
defer close(done)
conn.ReadFrames(func(id int, typ StreamType, payload []byte) {
recv++ recv++
if recv >= 3 { if recv >= 3 {
close(recvDone) close(recvDone)
} }
}) })
}()
time.Sleep(1300 * time.Millisecond) time.Sleep(1300 * time.Millisecond)
@@ -1559,7 +1591,7 @@ func TestClientReadErrorTimeout(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
defer conn.Close() defer conn.Close()
err = <-conn.ReadFrames(func(trackID int, streamType StreamType, payload []byte) { err = conn.ReadFrames(func(trackID int, streamType StreamType, payload []byte) {
}) })
switch proto { switch proto {
@@ -1688,13 +1720,17 @@ func TestClientReadIgnoreTCPInvalidTrack(t *testing.T) {
conn, err := c.DialRead("rtsp://localhost:8554/teststream") conn, err := c.DialRead("rtsp://localhost:8554/teststream")
require.NoError(t, err) require.NoError(t, err)
defer conn.Close()
recv := make(chan struct{}) recv := make(chan struct{})
done := make(chan struct{})
go func() {
defer close(done)
conn.ReadFrames(func(trackID int, streamType StreamType, payload []byte) { conn.ReadFrames(func(trackID int, streamType StreamType, payload []byte) {
close(recv) close(recv)
}) })
require.NoError(t, err) }()
<-recv <-recv
conn.Close()
<-done
} }

View File

@@ -1164,7 +1164,7 @@ func (cc *ClientConn) doSetup(
} }
proto := func() StreamProtocol { proto := func() StreamProtocol {
// protocol set by previous Setup() or ReadFrames() // protocol set by previous Setup() or switchProtocolIfTimeout()
if cc.streamProtocol != nil { if cc.streamProtocol != nil {
return *cc.streamProtocol return *cc.streamProtocol
} }
@@ -1566,8 +1566,7 @@ func (cc *ClientConn) Pause() (*base.Response, error) {
} }
// ReadFrames starts reading frames. // ReadFrames starts reading frames.
// it returns a channel that is written when the reading stops. func (cc *ClientConn) ReadFrames(onFrame func(int, StreamType, []byte)) error {
func (cc *ClientConn) ReadFrames(onFrame func(int, StreamType, []byte)) chan error {
cc.readCBMutex.Lock() cc.readCBMutex.Lock()
cc.readCB = onFrame cc.readCB = onFrame
cc.readCBMutex.Unlock() cc.readCBMutex.Unlock()
@@ -1578,12 +1577,8 @@ func (cc *ClientConn) ReadFrames(onFrame func(int, StreamType, []byte)) chan err
cc.readCBSet = nil cc.readCBSet = nil
} }
ch := make(chan error, 1)
go func() {
<-cc.backgroundDone <-cc.backgroundDone
ch <- cc.backgroundErr return cc.backgroundErr
}()
return ch
} }
// WriteFrame writes a frame. // WriteFrame writes a frame.

View File

@@ -38,7 +38,7 @@ func main() {
dec := rtph264.NewDecoder() dec := rtph264.NewDecoder()
// read RTP frames // read RTP frames
err = <-conn.ReadFrames(func(trackID int, typ gortsplib.StreamType, buf []byte) { err = conn.ReadFrames(func(trackID int, typ gortsplib.StreamType, buf []byte) {
if trackID == h264Track { if trackID == h264Track {
// convert RTP frames into H264 NALUs // convert RTP frames into H264 NALUs
nalus, _, err := dec.Decode(buf) nalus, _, err := dec.Decode(buf)

View File

@@ -30,7 +30,7 @@ func main() {
defer conn.Close() defer conn.Close()
// read RTP frames // read RTP frames
err = <-conn.ReadFrames(func(trackID int, typ gortsplib.StreamType, buf []byte) { err = conn.ReadFrames(func(trackID int, typ gortsplib.StreamType, buf []byte) {
fmt.Printf("frame from track %d, type %v, size %d\n", trackID, typ, len(buf)) fmt.Printf("frame from track %d, type %v, size %d\n", trackID, typ, len(buf))
}) })
panic(err) panic(err)

View File

@@ -52,7 +52,7 @@ func main() {
} }
// read RTP frames // read RTP frames
err = <-conn.ReadFrames(func(trackID int, typ gortsplib.StreamType, buf []byte) { err = conn.ReadFrames(func(trackID int, typ gortsplib.StreamType, buf []byte) {
fmt.Printf("frame from track %d, type %v, size %d\n", trackID, typ, len(buf)) fmt.Printf("frame from track %d, type %v, size %d\n", trackID, typ, len(buf))
}) })
panic(err) panic(err)

View File

@@ -23,9 +23,13 @@ func main() {
for { for {
// read RTP frames // read RTP frames
done := conn.ReadFrames(func(trackID int, typ gortsplib.StreamType, buf []byte) { done := make(chan struct{})
go func() {
defer close(done)
conn.ReadFrames(func(trackID int, typ gortsplib.StreamType, buf []byte) {
fmt.Printf("frame from track %d, type %v, size %d\n", trackID, typ, len(buf)) fmt.Printf("frame from track %d, type %v, size %d\n", trackID, typ, len(buf))
}) })
}()
// wait // wait
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)

View File

@@ -18,7 +18,7 @@ func main() {
defer conn.Close() defer conn.Close()
// read RTP frames // read RTP frames
err = <-conn.ReadFrames(func(trackID int, typ gortsplib.StreamType, buf []byte) { err = conn.ReadFrames(func(trackID int, typ gortsplib.StreamType, buf []byte) {
fmt.Printf("frame from track %d, type %v, size %d\n", trackID, typ, len(buf)) fmt.Printf("frame from track %d, type %v, size %d\n", trackID, typ, len(buf))
}) })
panic(err) panic(err)