mirror of
https://github.com/aler9/gortsplib
synced 2025-10-07 08:01:14 +08:00
rename Udp into UDP and Tcp into TCP, like the Go standard library
This commit is contained in:
@@ -5,8 +5,8 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
// connClientUdpListener is a UDP listener created by SetupUDP() to receive UDP frames.
|
// connClientUDPListener is a UDP listener created by SetupUDP() to receive UDP frames.
|
||||||
type connClientUdpListener struct {
|
type connClientUDPListener struct {
|
||||||
c *ConnClient
|
c *ConnClient
|
||||||
pc net.PacketConn
|
pc net.PacketConn
|
||||||
trackId int
|
trackId int
|
||||||
@@ -15,13 +15,13 @@ type connClientUdpListener struct {
|
|||||||
publisherPort int
|
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))
|
pc, err := c.conf.ListenPacket("udp", ":"+strconv.FormatInt(int64(port), 10))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &connClientUdpListener{
|
return &connClientUDPListener{
|
||||||
c: c,
|
c: c,
|
||||||
pc: pc,
|
pc: pc,
|
||||||
trackId: trackId,
|
trackId: trackId,
|
||||||
@@ -29,12 +29,12 @@ func newConnClientUdpListener(c *ConnClient, port int, trackId int, streamType S
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *connClientUdpListener) close() {
|
func (l *connClientUDPListener) close() {
|
||||||
l.pc.Close()
|
l.pc.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read reads a frame from the publisher.
|
// Read reads a frame from the publisher.
|
||||||
func (l *connClientUdpListener) Read(buf []byte) (int, error) {
|
func (l *connClientUDPListener) Read(buf []byte) (int, error) {
|
||||||
for {
|
for {
|
||||||
n, addr, err := l.pc.ReadFrom(buf)
|
n, addr, err := l.pc.ReadFrom(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@@ -23,9 +23,9 @@ const (
|
|||||||
clientReadBufferSize = 4096
|
clientReadBufferSize = 4096
|
||||||
clientWriteBufferSize = 4096
|
clientWriteBufferSize = 4096
|
||||||
clientReceiverReportPeriod = 10 * time.Second
|
clientReceiverReportPeriod = 10 * time.Second
|
||||||
clientUdpCheckStreamPeriod = 5 * time.Second
|
clientUDPCheckStreamPeriod = 5 * time.Second
|
||||||
clientUdpKeepalivePeriod = 30 * time.Second
|
clientUDPKeepalivePeriod = 30 * time.Second
|
||||||
clientTcpReadBufferSize = 128 * 1024
|
clientTCPReadBufferSize = 128 * 1024
|
||||||
)
|
)
|
||||||
|
|
||||||
// Track is a track available in a certain URL.
|
// Track is a track available in a certain URL.
|
||||||
@@ -71,8 +71,8 @@ type ConnClient struct {
|
|||||||
streamUrl *url.URL
|
streamUrl *url.URL
|
||||||
streamProtocol *StreamProtocol
|
streamProtocol *StreamProtocol
|
||||||
rtcpReceivers map[int]*RtcpReceiver
|
rtcpReceivers map[int]*RtcpReceiver
|
||||||
rtpListeners map[int]*connClientUdpListener
|
rtpListeners map[int]*connClientUDPListener
|
||||||
rtcpListeners map[int]*connClientUdpListener
|
rtcpListeners map[int]*connClientUDPListener
|
||||||
|
|
||||||
receiverReportTerminate chan struct{}
|
receiverReportTerminate chan struct{}
|
||||||
receiverReportDone chan struct{}
|
receiverReportDone chan struct{}
|
||||||
@@ -104,8 +104,8 @@ func NewConnClient(conf ConnClientConf) (*ConnClient, error) {
|
|||||||
br: bufio.NewReaderSize(nconn, clientReadBufferSize),
|
br: bufio.NewReaderSize(nconn, clientReadBufferSize),
|
||||||
bw: bufio.NewWriterSize(nconn, clientWriteBufferSize),
|
bw: bufio.NewWriterSize(nconn, clientWriteBufferSize),
|
||||||
rtcpReceivers: make(map[int]*RtcpReceiver),
|
rtcpReceivers: make(map[int]*RtcpReceiver),
|
||||||
rtpListeners: make(map[int]*connClientUdpListener),
|
rtpListeners: make(map[int]*connClientUDPListener),
|
||||||
rtcpListeners: make(map[int]*connClientUdpListener),
|
rtcpListeners: make(map[int]*connClientUDPListener),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -390,18 +390,18 @@ func (c *ConnClient) setup(u *url.URL, track *Track, transport []string) (*Respo
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UdpReadFunc is a function used to read UDP packets.
|
// UDPReadFunc is a function used to read UDP packets.
|
||||||
type UdpReadFunc func([]byte) (int, error)
|
type UDPReadFunc func([]byte) (int, error)
|
||||||
|
|
||||||
// SetupUdp writes a SETUP request, that means that we want to read
|
// SetupUDP writes a SETUP request, that means that we want to read
|
||||||
// a given track with the UDP transport. It then reads a Response.
|
// a given track with the UDP transport. It then reads a Response.
|
||||||
func (c *ConnClient) SetupUdp(u *url.URL, track *Track, rtpPort int,
|
func (c *ConnClient) SetupUDP(u *url.URL, track *Track, rtpPort int,
|
||||||
rtcpPort int) (UdpReadFunc, UdpReadFunc, *Response, error) {
|
rtcpPort int) (UDPReadFunc, UDPReadFunc, *Response, error) {
|
||||||
if c.streamUrl != nil && *u != *c.streamUrl {
|
if c.streamUrl != nil && *u != *c.streamUrl {
|
||||||
fmt.Errorf("setup has already begun with another url")
|
fmt.Errorf("setup has already begun with another url")
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.streamProtocol != nil && *c.streamProtocol != StreamProtocolUdp {
|
if c.streamProtocol != nil && *c.streamProtocol != StreamProtocolUDP {
|
||||||
return nil, nil, nil, fmt.Errorf("cannot setup tracks with different protocols")
|
return nil, nil, nil, fmt.Errorf("cannot setup tracks with different protocols")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -409,12 +409,12 @@ func (c *ConnClient) SetupUdp(u *url.URL, track *Track, rtpPort int,
|
|||||||
return nil, nil, nil, fmt.Errorf("track has already been setup")
|
return nil, nil, nil, fmt.Errorf("track has already been setup")
|
||||||
}
|
}
|
||||||
|
|
||||||
rtpListener, err := newConnClientUdpListener(c, rtpPort, track.Id, StreamTypeRtp)
|
rtpListener, err := newConnClientUDPListener(c, rtpPort, track.Id, StreamTypeRtp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, nil, err
|
return nil, nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
rtcpListener, err := newConnClientUdpListener(c, rtcpPort, track.Id, StreamTypeRtcp)
|
rtcpListener, err := newConnClientUDPListener(c, rtcpPort, track.Id, StreamTypeRtcp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rtpListener.close()
|
rtpListener.close()
|
||||||
return nil, nil, nil, err
|
return nil, nil, nil, err
|
||||||
@@ -446,7 +446,7 @@ func (c *ConnClient) SetupUdp(u *url.URL, track *Track, rtpPort int,
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.streamUrl = u
|
c.streamUrl = u
|
||||||
streamProtocol := StreamProtocolUdp
|
streamProtocol := StreamProtocolUDP
|
||||||
c.streamProtocol = &streamProtocol
|
c.streamProtocol = &streamProtocol
|
||||||
c.rtcpReceivers[track.Id] = NewRtcpReceiver()
|
c.rtcpReceivers[track.Id] = NewRtcpReceiver()
|
||||||
|
|
||||||
@@ -461,14 +461,14 @@ func (c *ConnClient) SetupUdp(u *url.URL, track *Track, rtpPort int,
|
|||||||
return rtpListener.Read, rtcpListener.Read, res, nil
|
return rtpListener.Read, rtcpListener.Read, res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetupTcp writes a SETUP request, that means that we want to read
|
// SetupTCP writes a SETUP request, that means that we want to read
|
||||||
// a given track with the TCP transport. It then reads a Response.
|
// a given track with the TCP transport. It then reads a Response.
|
||||||
func (c *ConnClient) SetupTcp(u *url.URL, track *Track) (*Response, error) {
|
func (c *ConnClient) SetupTCP(u *url.URL, track *Track) (*Response, error) {
|
||||||
if c.streamUrl != nil && *u != *c.streamUrl {
|
if c.streamUrl != nil && *u != *c.streamUrl {
|
||||||
fmt.Errorf("setup has already begun with another url")
|
fmt.Errorf("setup has already begun with another url")
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.streamProtocol != nil && *c.streamProtocol != StreamProtocolTcp {
|
if c.streamProtocol != nil && *c.streamProtocol != StreamProtocolTCP {
|
||||||
return nil, fmt.Errorf("cannot setup tracks with different protocols")
|
return nil, fmt.Errorf("cannot setup tracks with different protocols")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -498,7 +498,7 @@ func (c *ConnClient) SetupTcp(u *url.URL, track *Track) (*Response, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.streamUrl = u
|
c.streamUrl = u
|
||||||
streamProtocol := StreamProtocolTcp
|
streamProtocol := StreamProtocolTCP
|
||||||
c.streamProtocol = &streamProtocol
|
c.streamProtocol = &streamProtocol
|
||||||
c.rtcpReceivers[track.Id] = NewRtcpReceiver()
|
c.rtcpReceivers[track.Id] = NewRtcpReceiver()
|
||||||
|
|
||||||
@@ -518,7 +518,7 @@ func (c *ConnClient) Play(u *url.URL) (*Response, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
res, err := func() (*Response, error) {
|
res, err := func() (*Response, error) {
|
||||||
if *c.streamProtocol == StreamProtocolUdp {
|
if *c.streamProtocol == StreamProtocolUDP {
|
||||||
res, err := c.Do(&Request{
|
res, err := c.Do(&Request{
|
||||||
Method: PLAY,
|
Method: PLAY,
|
||||||
Url: u,
|
Url: u,
|
||||||
@@ -540,7 +540,7 @@ func (c *ConnClient) Play(u *url.URL) (*Response, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
frame := &InterleavedFrame{
|
frame := &InterleavedFrame{
|
||||||
Content: make([]byte, 0, clientTcpReadBufferSize),
|
Content: make([]byte, 0, clientTCPReadBufferSize),
|
||||||
}
|
}
|
||||||
|
|
||||||
// v4lrtspserver sends frames before the response.
|
// v4lrtspserver sends frames before the response.
|
||||||
@@ -567,7 +567,7 @@ func (c *ConnClient) Play(u *url.URL) (*Response, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// open the firewall by sending packets to every channel
|
// open the firewall by sending packets to every channel
|
||||||
if *c.streamProtocol == StreamProtocolUdp {
|
if *c.streamProtocol == StreamProtocolUDP {
|
||||||
for trackId := range c.rtpListeners {
|
for trackId := range c.rtpListeners {
|
||||||
c.rtpListeners[trackId].pc.WriteTo(
|
c.rtpListeners[trackId].pc.WriteTo(
|
||||||
[]byte{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
[]byte{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
||||||
@@ -604,7 +604,7 @@ func (c *ConnClient) Play(u *url.URL) (*Response, error) {
|
|||||||
for trackId := range c.rtcpReceivers {
|
for trackId := range c.rtcpReceivers {
|
||||||
frame := c.rtcpReceivers[trackId].Report()
|
frame := c.rtcpReceivers[trackId].Report()
|
||||||
|
|
||||||
if *c.streamProtocol == StreamProtocolUdp {
|
if *c.streamProtocol == StreamProtocolUDP {
|
||||||
c.rtcpListeners[trackId].pc.WriteTo(frame, &net.UDPAddr{
|
c.rtcpListeners[trackId].pc.WriteTo(frame, &net.UDPAddr{
|
||||||
IP: c.nconn.RemoteAddr().(*net.TCPAddr).IP,
|
IP: c.nconn.RemoteAddr().(*net.TCPAddr).IP,
|
||||||
Zone: c.nconn.RemoteAddr().(*net.TCPAddr).Zone,
|
Zone: c.nconn.RemoteAddr().(*net.TCPAddr).Zone,
|
||||||
@@ -626,14 +626,14 @@ func (c *ConnClient) Play(u *url.URL) (*Response, error) {
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoopUdp must be called after SetupUDP() and Play(); it keeps
|
// LoopUDP must be called after SetupUDP() and Play(); it keeps
|
||||||
// the TCP connection open through keepalives, and returns when the TCP
|
// the TCP connection open through keepalives, and returns when the TCP
|
||||||
// connection closes.
|
// connection closes.
|
||||||
func (c *ConnClient) LoopUdp(u *url.URL) error {
|
func (c *ConnClient) LoopUDP(u *url.URL) error {
|
||||||
readDone := make(chan error)
|
readDone := make(chan error)
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
c.nconn.SetReadDeadline(time.Now().Add(clientUdpKeepalivePeriod + c.conf.ReadTimeout))
|
c.nconn.SetReadDeadline(time.Now().Add(clientUDPKeepalivePeriod + c.conf.ReadTimeout))
|
||||||
_, err := ReadResponse(c.br)
|
_, err := ReadResponse(c.br)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
readDone <- err
|
readDone <- err
|
||||||
@@ -642,10 +642,10 @@ func (c *ConnClient) LoopUdp(u *url.URL) error {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
keepaliveTicker := time.NewTicker(clientUdpKeepalivePeriod)
|
keepaliveTicker := time.NewTicker(clientUDPKeepalivePeriod)
|
||||||
defer keepaliveTicker.Stop()
|
defer keepaliveTicker.Stop()
|
||||||
|
|
||||||
checkStreamTicker := time.NewTicker(clientUdpCheckStreamPeriod)
|
checkStreamTicker := time.NewTicker(clientUDPCheckStreamPeriod)
|
||||||
defer checkStreamTicker.Stop()
|
defer checkStreamTicker.Stop()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
@@ -32,7 +32,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, track := range tracks {
|
for _, track := range tracks {
|
||||||
_, err := conn.SetupTcp(u, track)
|
_, err := conn.SetupTCP(u, track)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@@ -32,11 +32,11 @@ func main() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var rtpReads []gortsplib.UdpReadFunc
|
var rtpReads []gortsplib.UDPReadFunc
|
||||||
var rtcpReads []gortsplib.UdpReadFunc
|
var rtcpReads []gortsplib.UDPReadFunc
|
||||||
|
|
||||||
for _, track := range tracks {
|
for _, track := range tracks {
|
||||||
rtpRead, rtcpRead, _, 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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@@ -56,7 +56,7 @@ func main() {
|
|||||||
for trackId, rtpRead := range rtpReads {
|
for trackId, rtpRead := range rtpReads {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
|
|
||||||
go func(trackId int, rtpRead gortsplib.UdpReadFunc) {
|
go func(trackId int, rtpRead gortsplib.UDPReadFunc) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
|
||||||
buf := make([]byte, 2048)
|
buf := make([]byte, 2048)
|
||||||
@@ -75,7 +75,7 @@ func main() {
|
|||||||
for trackId, rtcpRead := range rtcpReads {
|
for trackId, rtcpRead := range rtcpReads {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
|
|
||||||
go func(trackId int, rtcpRead gortsplib.UdpReadFunc) {
|
go func(trackId int, rtcpRead gortsplib.UDPReadFunc) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
|
||||||
buf := make([]byte, 2048)
|
buf := make([]byte, 2048)
|
||||||
@@ -90,7 +90,7 @@ func main() {
|
|||||||
}(trackId, rtcpRead)
|
}(trackId, rtcpRead)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = conn.LoopUdp(u)
|
err = conn.LoopUDP(u)
|
||||||
conn.Close()
|
conn.Close()
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
fmt.Println("connection is closed (%s)", err)
|
fmt.Println("connection is closed (%s)", err)
|
||||||
|
@@ -27,8 +27,8 @@ func ReadHeaderTransport(v HeaderValue) (HeaderTransport, error) {
|
|||||||
return ht, nil
|
return ht, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsUdp check whether the header contains the UDP protocol.
|
// IsUDP check whether the header contains the UDP protocol.
|
||||||
func (ht HeaderTransport) IsUdp() bool {
|
func (ht HeaderTransport) IsUDP() bool {
|
||||||
if _, ok := ht["RTP/AVP"]; ok {
|
if _, ok := ht["RTP/AVP"]; ok {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@@ -38,8 +38,8 @@ func (ht HeaderTransport) IsUdp() bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsTcp check whether the header contains the TCP protocol.
|
// IsTCP check whether the header contains the TCP protocol.
|
||||||
func (ht HeaderTransport) IsTcp() bool {
|
func (ht HeaderTransport) IsTCP() bool {
|
||||||
_, ok := ht["RTP/AVP/TCP"]
|
_, ok := ht["RTP/AVP/TCP"]
|
||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
10
utils.go
10
utils.go
@@ -15,16 +15,16 @@ const (
|
|||||||
type StreamProtocol int
|
type StreamProtocol int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// StreamProtocolUdp means that the stream uses the UDP protocol
|
// StreamProtocolUDP means that the stream uses the UDP protocol
|
||||||
StreamProtocolUdp StreamProtocol = iota
|
StreamProtocolUDP StreamProtocol = iota
|
||||||
|
|
||||||
// StreamProtocolTcp means that the stream uses the TCP protocol
|
// StreamProtocolTCP means that the stream uses the TCP protocol
|
||||||
StreamProtocolTcp
|
StreamProtocolTCP
|
||||||
)
|
)
|
||||||
|
|
||||||
// String implements fmt.Stringer
|
// String implements fmt.Stringer
|
||||||
func (sp StreamProtocol) String() string {
|
func (sp StreamProtocol) String() string {
|
||||||
if sp == StreamProtocolUdp {
|
if sp == StreamProtocolUDP {
|
||||||
return "udp"
|
return "udp"
|
||||||
}
|
}
|
||||||
return "tcp"
|
return "tcp"
|
||||||
|
Reference in New Issue
Block a user