unexport connClientUdpListener

This commit is contained in:
aler9
2020-08-29 11:53:47 +02:00
parent 5998615a9c
commit 1bc3b627fb
4 changed files with 38 additions and 32 deletions

View File

@@ -7,6 +7,10 @@
RTSP 1.0 library for the Go programming language, written for [rtsp-simple-server](https://github.com/aler9/rtsp-simple-server).
Features:
* Provides primitives, a class for building clients (`ConnClient`) and a class for building servers (`ConnServer`)
* Supports TCP and UDP streaming protocols
## Examples
* [client-tcp](examples/client-tcp.go)

View File

@@ -5,8 +5,8 @@ import (
"strconv"
)
// ConnClientUdpListener is a UDP listener created by SetupUDP() to receive UDP frames.
type ConnClientUdpListener struct {
// connClientUdpListener is a UDP listener created by SetupUDP() to receive UDP frames.
type connClientUdpListener struct {
c *ConnClient
pc net.PacketConn
trackId int
@@ -15,13 +15,13 @@ type ConnClientUdpListener struct {
publisherPort int
}
func newConnClientUdpListener(c *ConnClient, port int, trackId int, streamType StreamType) (*ConnClientUdpListener, error) {
func newConnClientUdpListener(c *ConnClient, port int, trackId int, streamType StreamType) (*connClientUdpListener, error) {
pc, err := c.conf.ListenPacket("udp", ":"+strconv.FormatInt(int64(port), 10))
if err != nil {
return nil, err
}
return &ConnClientUdpListener{
return &connClientUdpListener{
c: c,
pc: pc,
trackId: trackId,
@@ -29,12 +29,12 @@ func newConnClientUdpListener(c *ConnClient, port int, trackId int, streamType S
}, nil
}
func (l *ConnClientUdpListener) close() {
func (l *connClientUdpListener) close() {
l.pc.Close()
}
// Read reads a frame from the publisher.
func (l *ConnClientUdpListener) Read(buf []byte) (int, error) {
func (l *connClientUdpListener) Read(buf []byte) (int, error) {
for {
n, addr, err := l.pc.ReadFrom(buf)
if err != nil {

View File

@@ -71,8 +71,8 @@ type ConnClient struct {
streamUrl *url.URL
streamProtocol *StreamProtocol
rtcpReceivers map[int]*RtcpReceiver
rtpListeners map[int]*ConnClientUdpListener
rtcpListeners map[int]*ConnClientUdpListener
rtpListeners map[int]*connClientUdpListener
rtcpListeners map[int]*connClientUdpListener
receiverReportTerminate chan struct{}
receiverReportDone chan struct{}
@@ -104,8 +104,8 @@ func NewConnClient(conf ConnClientConf) (*ConnClient, error) {
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),
rtpListeners: make(map[int]*connClientUdpListener),
rtcpListeners: make(map[int]*connClientUdpListener),
}, nil
}
@@ -387,10 +387,13 @@ func (c *ConnClient) setup(u *url.URL, track *Track, transport []string) (*Respo
return res, nil
}
// UdpReadFunc is a function used to read UDP packets.
type UdpReadFunc func([]byte) (int, error)
// 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) {
rtcpPort int) (UdpReadFunc, UdpReadFunc, *Response, error) {
if c.streamUrl != nil && *u != *c.streamUrl {
fmt.Errorf("setup has already begun with another url")
}
@@ -452,7 +455,7 @@ func (c *ConnClient) SetupUdp(u *url.URL, track *Track, rtpPort int,
rtcpListener.publisherPort = rtcpServerPort
c.rtcpListeners[track.Id] = rtcpListener
return rtpListener, rtcpListener, res, nil
return rtpListener.Read, rtcpListener.Read, res, nil
}
// SetupTcp writes a SETUP request, that means that we want to read

View File

@@ -32,22 +32,17 @@ func main() {
panic(err)
}
type trackListenerPair struct {
rtpl *gortsplib.ConnClientUdpListener
rtcpl *gortsplib.ConnClientUdpListener
}
var listeners []*trackListenerPair
var rtpReads []gortsplib.UdpReadFunc
var rtcpReads []gortsplib.UdpReadFunc
for _, track := range tracks {
rtpl, rtcpl, _, err := conn.SetupUdp(u, track, 9000+track.Id*2, 9001+track.Id*2)
rtpRead, rtcpRead, _, err := conn.SetupUdp(u, track, 9000+track.Id*2, 9001+track.Id*2)
if err != nil {
panic(err)
}
listeners = append(listeners, &trackListenerPair{
rtpl: rtpl,
rtcpl: rtcpl,
})
rtpReads = append(rtpReads, rtpRead)
rtcpReads = append(rtcpReads, rtcpRead)
}
_, err = conn.Play(u)
@@ -57,38 +52,42 @@ func main() {
var wg sync.WaitGroup
for trackId, lp := range listeners {
wg.Add(2)
// receive RTP frames
for trackId, rtpRead := range rtpReads {
wg.Add(1)
// receive RTP frames
go func(trackId int, l *gortsplib.ConnClientUdpListener) {
go func(trackId int, rtpRead gortsplib.UdpReadFunc) {
defer wg.Done()
buf := make([]byte, 2048)
for {
n, err := l.Read(buf)
n, err := rtpRead(buf)
if err != nil {
break
}
fmt.Printf("frame from track %d, type RTP: %v\n", trackId, buf[:n])
}
}(trackId, lp.rtpl)
}(trackId, rtpRead)
}
// receive RTCP frames
go func(trackId int, l *gortsplib.ConnClientUdpListener) {
// receive RTCP frames
for trackId, rtcpRead := range rtcpReads {
wg.Add(1)
go func(trackId int, rtcpRead gortsplib.UdpReadFunc) {
defer wg.Done()
buf := make([]byte, 2048)
for {
n, err := l.Read(buf)
n, err := rtcpRead(buf)
if err != nil {
break
}
fmt.Printf("frame from track %d, type RTCP: %v\n", trackId, buf[:n])
}
}(trackId, lp.rtcpl)
}(trackId, rtcpRead)
}
err = conn.LoopUdp(u)