mirror of
https://github.com/aler9/gortsplib
synced 2025-10-16 12:10:48 +08:00
client: slightly improve performance
This commit is contained in:
54
client.go
54
client.go
@@ -56,6 +56,8 @@ type clientTrack struct {
|
|||||||
udpRTPListener *clientUDPListener
|
udpRTPListener *clientUDPListener
|
||||||
udpRTCPListener *clientUDPListener
|
udpRTCPListener *clientUDPListener
|
||||||
tcpChannel int
|
tcpChannel int
|
||||||
|
tcpRTPFrame *base.InterleavedFrame
|
||||||
|
tcpRTCPFrame *base.InterleavedFrame
|
||||||
rtcpReceiver *rtcpreceiver.RTCPReceiver
|
rtcpReceiver *rtcpreceiver.RTCPReceiver
|
||||||
rtcpSender *rtcpsender.RTCPSender
|
rtcpSender *rtcpsender.RTCPSender
|
||||||
}
|
}
|
||||||
@@ -1424,7 +1426,6 @@ func (c *Client) doSetup(
|
|||||||
switch proto {
|
switch proto {
|
||||||
case TransportUDP:
|
case TransportUDP:
|
||||||
rtpListener.remoteReadIP = c.nconn.RemoteAddr().(*net.TCPAddr).IP
|
rtpListener.remoteReadIP = c.nconn.RemoteAddr().(*net.TCPAddr).IP
|
||||||
rtpListener.remoteWriteIP = c.nconn.RemoteAddr().(*net.TCPAddr).IP
|
|
||||||
rtpListener.remoteZone = c.nconn.RemoteAddr().(*net.TCPAddr).Zone
|
rtpListener.remoteZone = c.nconn.RemoteAddr().(*net.TCPAddr).Zone
|
||||||
if thRes.ServerPorts != nil {
|
if thRes.ServerPorts != nil {
|
||||||
rtpListener.remotePort = thRes.ServerPorts[0]
|
rtpListener.remotePort = thRes.ServerPorts[0]
|
||||||
@@ -1433,8 +1434,13 @@ func (c *Client) doSetup(
|
|||||||
rtpListener.isRTP = true
|
rtpListener.isRTP = true
|
||||||
cct.udpRTPListener = rtpListener
|
cct.udpRTPListener = rtpListener
|
||||||
|
|
||||||
|
rtpListener.remoteWriteAddr = &net.UDPAddr{
|
||||||
|
IP: c.nconn.RemoteAddr().(*net.TCPAddr).IP,
|
||||||
|
Zone: rtpListener.remoteZone,
|
||||||
|
Port: rtpListener.remotePort,
|
||||||
|
}
|
||||||
|
|
||||||
rtcpListener.remoteReadIP = c.nconn.RemoteAddr().(*net.TCPAddr).IP
|
rtcpListener.remoteReadIP = c.nconn.RemoteAddr().(*net.TCPAddr).IP
|
||||||
rtcpListener.remoteWriteIP = c.nconn.RemoteAddr().(*net.TCPAddr).IP
|
|
||||||
rtcpListener.remoteZone = c.nconn.RemoteAddr().(*net.TCPAddr).Zone
|
rtcpListener.remoteZone = c.nconn.RemoteAddr().(*net.TCPAddr).Zone
|
||||||
if thRes.ServerPorts != nil {
|
if thRes.ServerPorts != nil {
|
||||||
rtcpListener.remotePort = thRes.ServerPorts[1]
|
rtcpListener.remotePort = thRes.ServerPorts[1]
|
||||||
@@ -1443,23 +1449,39 @@ func (c *Client) doSetup(
|
|||||||
rtcpListener.isRTP = false
|
rtcpListener.isRTP = false
|
||||||
cct.udpRTCPListener = rtcpListener
|
cct.udpRTCPListener = rtcpListener
|
||||||
|
|
||||||
|
rtcpListener.remoteWriteAddr = &net.UDPAddr{
|
||||||
|
IP: c.nconn.RemoteAddr().(*net.TCPAddr).IP,
|
||||||
|
Zone: rtcpListener.remoteZone,
|
||||||
|
Port: rtcpListener.remotePort,
|
||||||
|
}
|
||||||
|
|
||||||
case TransportUDPMulticast:
|
case TransportUDPMulticast:
|
||||||
rtpListener.remoteReadIP = c.nconn.RemoteAddr().(*net.TCPAddr).IP
|
rtpListener.remoteReadIP = c.nconn.RemoteAddr().(*net.TCPAddr).IP
|
||||||
rtpListener.remoteWriteIP = *thRes.Destination
|
|
||||||
rtpListener.remoteZone = ""
|
rtpListener.remoteZone = ""
|
||||||
rtpListener.remotePort = thRes.Ports[0]
|
rtpListener.remotePort = thRes.Ports[0]
|
||||||
rtpListener.trackID = trackID
|
rtpListener.trackID = trackID
|
||||||
rtpListener.isRTP = true
|
rtpListener.isRTP = true
|
||||||
cct.udpRTPListener = rtpListener
|
cct.udpRTPListener = rtpListener
|
||||||
|
|
||||||
|
rtpListener.remoteWriteAddr = &net.UDPAddr{
|
||||||
|
IP: *thRes.Destination,
|
||||||
|
Zone: rtpListener.remoteZone,
|
||||||
|
Port: rtpListener.remotePort,
|
||||||
|
}
|
||||||
|
|
||||||
rtcpListener.remoteReadIP = c.nconn.RemoteAddr().(*net.TCPAddr).IP
|
rtcpListener.remoteReadIP = c.nconn.RemoteAddr().(*net.TCPAddr).IP
|
||||||
rtcpListener.remoteWriteIP = *thRes.Destination
|
|
||||||
rtcpListener.remoteZone = ""
|
rtcpListener.remoteZone = ""
|
||||||
rtcpListener.remotePort = thRes.Ports[1]
|
rtcpListener.remotePort = thRes.Ports[1]
|
||||||
rtcpListener.trackID = trackID
|
rtcpListener.trackID = trackID
|
||||||
rtcpListener.isRTP = false
|
rtcpListener.isRTP = false
|
||||||
cct.udpRTCPListener = rtcpListener
|
cct.udpRTCPListener = rtcpListener
|
||||||
|
|
||||||
|
rtcpListener.remoteWriteAddr = &net.UDPAddr{
|
||||||
|
IP: *thRes.Destination,
|
||||||
|
Zone: rtcpListener.remoteZone,
|
||||||
|
Port: rtcpListener.remotePort,
|
||||||
|
}
|
||||||
|
|
||||||
case TransportTCP:
|
case TransportTCP:
|
||||||
if c.tcpReadBuffer == nil {
|
if c.tcpReadBuffer == nil {
|
||||||
c.tcpReadBuffer = multibuffer.New(uint64(c.ReadBufferCount), uint64(c.ReadBufferSize))
|
c.tcpReadBuffer = multibuffer.New(uint64(c.ReadBufferCount), uint64(c.ReadBufferSize))
|
||||||
@@ -1472,6 +1494,14 @@ func (c *Client) doSetup(
|
|||||||
c.tracksByChannel[thRes.InterleavedIDs[0]] = trackID
|
c.tracksByChannel[thRes.InterleavedIDs[0]] = trackID
|
||||||
|
|
||||||
cct.tcpChannel = thRes.InterleavedIDs[0]
|
cct.tcpChannel = thRes.InterleavedIDs[0]
|
||||||
|
|
||||||
|
cct.tcpRTPFrame = &base.InterleavedFrame{
|
||||||
|
Channel: cct.tcpChannel,
|
||||||
|
}
|
||||||
|
|
||||||
|
cct.tcpRTCPFrame = &base.InterleavedFrame{
|
||||||
|
Channel: cct.tcpChannel + 1,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.tracks == nil {
|
if c.tracks == nil {
|
||||||
@@ -1763,17 +1793,15 @@ func (c *Client) WritePacketRTP(trackID int, payload []byte) error {
|
|||||||
return c.tracks[trackID].udpRTPListener.write(payload)
|
return c.tracks[trackID].udpRTPListener.write(payload)
|
||||||
|
|
||||||
default: // TCP
|
default: // TCP
|
||||||
channel := c.tracks[trackID].tcpChannel
|
f := c.tracks[trackID].tcpRTPFrame
|
||||||
|
|
||||||
// a mutex is needed here since bufio.Writer is not thread safe.
|
// a mutex is needed here since bufio.Writer is not thread safe.
|
||||||
c.tcpWriteMutex.Lock()
|
c.tcpWriteMutex.Lock()
|
||||||
defer c.tcpWriteMutex.Unlock()
|
defer c.tcpWriteMutex.Unlock()
|
||||||
|
|
||||||
c.nconn.SetWriteDeadline(now.Add(c.WriteTimeout))
|
c.nconn.SetWriteDeadline(now.Add(c.WriteTimeout))
|
||||||
return base.InterleavedFrame{
|
f.Payload = payload
|
||||||
Channel: channel,
|
return f.Write(c.bw)
|
||||||
Payload: payload,
|
|
||||||
}.Write(c.bw)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1798,16 +1826,14 @@ func (c *Client) WritePacketRTCP(trackID int, payload []byte) error {
|
|||||||
return c.tracks[trackID].udpRTCPListener.write(payload)
|
return c.tracks[trackID].udpRTCPListener.write(payload)
|
||||||
|
|
||||||
default: // TCP
|
default: // TCP
|
||||||
channel := c.tracks[trackID].tcpChannel + 1
|
f := c.tracks[trackID].tcpRTCPFrame
|
||||||
|
|
||||||
// a mutex is needed here since bufio.Writer is not thread safe.
|
// a mutex is needed here since bufio.Writer is not thread safe.
|
||||||
c.tcpWriteMutex.Lock()
|
c.tcpWriteMutex.Lock()
|
||||||
defer c.tcpWriteMutex.Unlock()
|
defer c.tcpWriteMutex.Unlock()
|
||||||
|
|
||||||
c.nconn.SetWriteDeadline(now.Add(c.WriteTimeout))
|
c.nconn.SetWriteDeadline(now.Add(c.WriteTimeout))
|
||||||
return base.InterleavedFrame{
|
f.Payload = payload
|
||||||
Channel: channel,
|
return f.Write(c.bw)
|
||||||
Payload: payload,
|
|
||||||
}.Write(c.bw)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -26,9 +26,9 @@ type clientUDPListener struct {
|
|||||||
c *Client
|
c *Client
|
||||||
pc *net.UDPConn
|
pc *net.UDPConn
|
||||||
remoteReadIP net.IP
|
remoteReadIP net.IP
|
||||||
remoteWriteIP net.IP
|
|
||||||
remoteZone string
|
remoteZone string
|
||||||
remotePort int
|
remotePort int
|
||||||
|
remoteWriteAddr *net.UDPAddr
|
||||||
trackID int
|
trackID int
|
||||||
isRTP bool
|
isRTP bool
|
||||||
running bool
|
running bool
|
||||||
@@ -192,10 +192,6 @@ func (l *clientUDPListener) write(buf []byte) error {
|
|||||||
// https://github.com/golang/go/issues/27203#issuecomment-534386117
|
// https://github.com/golang/go/issues/27203#issuecomment-534386117
|
||||||
|
|
||||||
l.pc.SetWriteDeadline(time.Now().Add(l.c.WriteTimeout))
|
l.pc.SetWriteDeadline(time.Now().Add(l.c.WriteTimeout))
|
||||||
_, err := l.pc.WriteTo(buf, &net.UDPAddr{
|
_, err := l.pc.WriteTo(buf, l.remoteWriteAddr)
|
||||||
IP: l.remoteWriteIP,
|
|
||||||
Zone: l.remoteZone,
|
|
||||||
Port: l.remotePort,
|
|
||||||
})
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user