mirror of
https://github.com/aler9/gortsplib
synced 2025-10-06 23:52:46 +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"
|
||||
)
|
||||
|
||||
// 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 {
|
||||
|
@@ -23,9 +23,9 @@ const (
|
||||
clientReadBufferSize = 4096
|
||||
clientWriteBufferSize = 4096
|
||||
clientReceiverReportPeriod = 10 * time.Second
|
||||
clientUdpCheckStreamPeriod = 5 * time.Second
|
||||
clientUdpKeepalivePeriod = 30 * time.Second
|
||||
clientTcpReadBufferSize = 128 * 1024
|
||||
clientUDPCheckStreamPeriod = 5 * time.Second
|
||||
clientUDPKeepalivePeriod = 30 * time.Second
|
||||
clientTCPReadBufferSize = 128 * 1024
|
||||
)
|
||||
|
||||
// Track is a track available in a certain URL.
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -390,18 +390,18 @@ 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)
|
||||
// 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
|
||||
// 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) (UdpReadFunc, UdpReadFunc, *Response, error) {
|
||||
func (c *ConnClient) SetupUDP(u *url.URL, track *Track, rtpPort int,
|
||||
rtcpPort int) (UDPReadFunc, UDPReadFunc, *Response, error) {
|
||||
if c.streamUrl != nil && *u != *c.streamUrl {
|
||||
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")
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
rtpListener, err := newConnClientUdpListener(c, rtpPort, track.Id, StreamTypeRtp)
|
||||
rtpListener, err := newConnClientUDPListener(c, rtpPort, track.Id, StreamTypeRtp)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
|
||||
rtcpListener, err := newConnClientUdpListener(c, rtcpPort, track.Id, StreamTypeRtcp)
|
||||
rtcpListener, err := newConnClientUDPListener(c, rtcpPort, track.Id, StreamTypeRtcp)
|
||||
if err != nil {
|
||||
rtpListener.close()
|
||||
return nil, nil, nil, err
|
||||
@@ -446,7 +446,7 @@ func (c *ConnClient) SetupUdp(u *url.URL, track *Track, rtpPort int,
|
||||
}
|
||||
|
||||
c.streamUrl = u
|
||||
streamProtocol := StreamProtocolUdp
|
||||
streamProtocol := StreamProtocolUDP
|
||||
c.streamProtocol = &streamProtocol
|
||||
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
|
||||
}
|
||||
|
||||
// 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.
|
||||
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 {
|
||||
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")
|
||||
}
|
||||
|
||||
@@ -498,7 +498,7 @@ func (c *ConnClient) SetupTcp(u *url.URL, track *Track) (*Response, error) {
|
||||
}
|
||||
|
||||
c.streamUrl = u
|
||||
streamProtocol := StreamProtocolTcp
|
||||
streamProtocol := StreamProtocolTCP
|
||||
c.streamProtocol = &streamProtocol
|
||||
c.rtcpReceivers[track.Id] = NewRtcpReceiver()
|
||||
|
||||
@@ -518,7 +518,7 @@ func (c *ConnClient) Play(u *url.URL) (*Response, error) {
|
||||
}
|
||||
|
||||
res, err := func() (*Response, error) {
|
||||
if *c.streamProtocol == StreamProtocolUdp {
|
||||
if *c.streamProtocol == StreamProtocolUDP {
|
||||
res, err := c.Do(&Request{
|
||||
Method: PLAY,
|
||||
Url: u,
|
||||
@@ -540,7 +540,7 @@ func (c *ConnClient) Play(u *url.URL) (*Response, error) {
|
||||
}
|
||||
|
||||
frame := &InterleavedFrame{
|
||||
Content: make([]byte, 0, clientTcpReadBufferSize),
|
||||
Content: make([]byte, 0, clientTCPReadBufferSize),
|
||||
}
|
||||
|
||||
// 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
|
||||
if *c.streamProtocol == StreamProtocolUdp {
|
||||
if *c.streamProtocol == StreamProtocolUDP {
|
||||
for trackId := range c.rtpListeners {
|
||||
c.rtpListeners[trackId].pc.WriteTo(
|
||||
[]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 {
|
||||
frame := c.rtcpReceivers[trackId].Report()
|
||||
|
||||
if *c.streamProtocol == StreamProtocolUdp {
|
||||
if *c.streamProtocol == StreamProtocolUDP {
|
||||
c.rtcpListeners[trackId].pc.WriteTo(frame, &net.UDPAddr{
|
||||
IP: c.nconn.RemoteAddr().(*net.TCPAddr).IP,
|
||||
Zone: c.nconn.RemoteAddr().(*net.TCPAddr).Zone,
|
||||
@@ -626,14 +626,14 @@ func (c *ConnClient) Play(u *url.URL) (*Response, error) {
|
||||
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
|
||||
// connection closes.
|
||||
func (c *ConnClient) LoopUdp(u *url.URL) error {
|
||||
func (c *ConnClient) LoopUDP(u *url.URL) error {
|
||||
readDone := make(chan error)
|
||||
go func() {
|
||||
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)
|
||||
if err != nil {
|
||||
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()
|
||||
|
||||
checkStreamTicker := time.NewTicker(clientUdpCheckStreamPeriod)
|
||||
checkStreamTicker := time.NewTicker(clientUDPCheckStreamPeriod)
|
||||
defer checkStreamTicker.Stop()
|
||||
|
||||
for {
|
||||
|
@@ -32,7 +32,7 @@ func main() {
|
||||
}
|
||||
|
||||
for _, track := range tracks {
|
||||
_, err := conn.SetupTcp(u, track)
|
||||
_, err := conn.SetupTCP(u, track)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@@ -32,11 +32,11 @@ func main() {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var rtpReads []gortsplib.UdpReadFunc
|
||||
var rtcpReads []gortsplib.UdpReadFunc
|
||||
var rtpReads []gortsplib.UDPReadFunc
|
||||
var rtcpReads []gortsplib.UDPReadFunc
|
||||
|
||||
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 {
|
||||
panic(err)
|
||||
}
|
||||
@@ -56,7 +56,7 @@ func main() {
|
||||
for trackId, rtpRead := range rtpReads {
|
||||
wg.Add(1)
|
||||
|
||||
go func(trackId int, rtpRead gortsplib.UdpReadFunc) {
|
||||
go func(trackId int, rtpRead gortsplib.UDPReadFunc) {
|
||||
defer wg.Done()
|
||||
|
||||
buf := make([]byte, 2048)
|
||||
@@ -75,7 +75,7 @@ func main() {
|
||||
for trackId, rtcpRead := range rtcpReads {
|
||||
wg.Add(1)
|
||||
|
||||
go func(trackId int, rtcpRead gortsplib.UdpReadFunc) {
|
||||
go func(trackId int, rtcpRead gortsplib.UDPReadFunc) {
|
||||
defer wg.Done()
|
||||
|
||||
buf := make([]byte, 2048)
|
||||
@@ -90,7 +90,7 @@ func main() {
|
||||
}(trackId, rtcpRead)
|
||||
}
|
||||
|
||||
err = conn.LoopUdp(u)
|
||||
err = conn.LoopUDP(u)
|
||||
conn.Close()
|
||||
wg.Wait()
|
||||
fmt.Println("connection is closed (%s)", err)
|
||||
|
@@ -27,8 +27,8 @@ func ReadHeaderTransport(v HeaderValue) (HeaderTransport, error) {
|
||||
return ht, nil
|
||||
}
|
||||
|
||||
// IsUdp check whether the header contains the UDP protocol.
|
||||
func (ht HeaderTransport) IsUdp() bool {
|
||||
// IsUDP check whether the header contains the UDP protocol.
|
||||
func (ht HeaderTransport) IsUDP() bool {
|
||||
if _, ok := ht["RTP/AVP"]; ok {
|
||||
return true
|
||||
}
|
||||
@@ -38,8 +38,8 @@ func (ht HeaderTransport) IsUdp() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// IsTcp check whether the header contains the TCP protocol.
|
||||
func (ht HeaderTransport) IsTcp() bool {
|
||||
// IsTCP check whether the header contains the TCP protocol.
|
||||
func (ht HeaderTransport) IsTCP() bool {
|
||||
_, ok := ht["RTP/AVP/TCP"]
|
||||
return ok
|
||||
}
|
||||
|
10
utils.go
10
utils.go
@@ -15,16 +15,16 @@ const (
|
||||
type StreamProtocol int
|
||||
|
||||
const (
|
||||
// StreamProtocolUdp means that the stream uses the UDP protocol
|
||||
StreamProtocolUdp StreamProtocol = iota
|
||||
// StreamProtocolUDP means that the stream uses the UDP protocol
|
||||
StreamProtocolUDP StreamProtocol = iota
|
||||
|
||||
// StreamProtocolTcp means that the stream uses the TCP protocol
|
||||
StreamProtocolTcp
|
||||
// StreamProtocolTCP means that the stream uses the TCP protocol
|
||||
StreamProtocolTCP
|
||||
)
|
||||
|
||||
// String implements fmt.Stringer
|
||||
func (sp StreamProtocol) String() string {
|
||||
if sp == StreamProtocolUdp {
|
||||
if sp == StreamProtocolUDP {
|
||||
return "udp"
|
||||
}
|
||||
return "tcp"
|
||||
|
Reference in New Issue
Block a user