mirror of
https://github.com/aler9/gortsplib
synced 2025-10-23 23:23:12 +08:00
client: allow bidirectional communication with multicast
This commit is contained in:
@@ -354,13 +354,15 @@ func TestClientRead(t *testing.T) {
|
|||||||
|
|
||||||
// client -> server (RTCP)
|
// client -> server (RTCP)
|
||||||
switch proto {
|
switch proto {
|
||||||
case "udp":
|
case "udp", "multicast":
|
||||||
// skip firewall opening
|
if proto == "udp" {
|
||||||
buf := make([]byte, 2048)
|
// skip firewall opening
|
||||||
_, _, err := l2.ReadFrom(buf)
|
buf := make([]byte, 2048)
|
||||||
require.NoError(t, err)
|
_, _, err := l2.ReadFrom(buf)
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
buf = make([]byte, 2048)
|
buf := make([]byte, 2048)
|
||||||
n, _, err := l2.ReadFrom(buf)
|
n, _, err := l2.ReadFrom(buf)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, []byte{0x05, 0x06, 0x07, 0x08}, buf[:n])
|
require.Equal(t, []byte{0x05, 0x06, 0x07, 0x08}, buf[:n])
|
||||||
@@ -414,20 +416,19 @@ func TestClientRead(t *testing.T) {
|
|||||||
defer close(done)
|
defer close(done)
|
||||||
conn.ReadFrames(func(id int, streamType StreamType, payload []byte) {
|
conn.ReadFrames(func(id int, streamType StreamType, payload []byte) {
|
||||||
// skip multicast loopback
|
// skip multicast loopback
|
||||||
if proto == "multicast" && atomic.AddUint64(&counter, 1) <= 2 {
|
if proto == "multicast" {
|
||||||
return
|
add := atomic.AddUint64(&counter, 1)
|
||||||
|
if add >= 2 {
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
|
|
||||||
if proto != "multicast" {
|
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)
|
|
||||||
} else {
|
|
||||||
close(frameRecv)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
@@ -475,13 +475,15 @@ func (cc *ClientConn) runBackground() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (cc *ClientConn) runBackgroundPlayUDP() error {
|
func (cc *ClientConn) runBackgroundPlayUDP() error {
|
||||||
// open the firewall by sending packets to the counterpart
|
if *cc.protocol == ClientProtocolUDP {
|
||||||
for _, cct := range cc.tracks {
|
// open the firewall by sending packets to the counterpart
|
||||||
cct.udpRTPListener.write(
|
for _, cct := range cc.tracks {
|
||||||
[]byte{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})
|
cct.udpRTPListener.write(
|
||||||
|
[]byte{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})
|
||||||
|
|
||||||
cct.udpRTCPListener.write(
|
cct.udpRTCPListener.write(
|
||||||
[]byte{0x80, 0xc9, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00})
|
[]byte{0x80, 0xc9, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, cct := range cc.tracks {
|
for _, cct := range cc.tracks {
|
||||||
@@ -1379,7 +1381,8 @@ func (cc *ClientConn) doSetup(
|
|||||||
|
|
||||||
switch proto {
|
switch proto {
|
||||||
case ClientProtocolUDP:
|
case ClientProtocolUDP:
|
||||||
rtpListener.remoteIP = cc.nconn.RemoteAddr().(*net.TCPAddr).IP
|
rtpListener.remoteReadIP = cc.nconn.RemoteAddr().(*net.TCPAddr).IP
|
||||||
|
rtpListener.remoteWriteIP = cc.nconn.RemoteAddr().(*net.TCPAddr).IP
|
||||||
rtpListener.remoteZone = cc.nconn.RemoteAddr().(*net.TCPAddr).Zone
|
rtpListener.remoteZone = cc.nconn.RemoteAddr().(*net.TCPAddr).Zone
|
||||||
if thRes.ServerPorts != nil {
|
if thRes.ServerPorts != nil {
|
||||||
rtpListener.remotePort = thRes.ServerPorts[0]
|
rtpListener.remotePort = thRes.ServerPorts[0]
|
||||||
@@ -1388,7 +1391,8 @@ func (cc *ClientConn) doSetup(
|
|||||||
rtpListener.streamType = StreamTypeRTP
|
rtpListener.streamType = StreamTypeRTP
|
||||||
cct.udpRTPListener = rtpListener
|
cct.udpRTPListener = rtpListener
|
||||||
|
|
||||||
rtcpListener.remoteIP = cc.nconn.RemoteAddr().(*net.TCPAddr).IP
|
rtcpListener.remoteReadIP = cc.nconn.RemoteAddr().(*net.TCPAddr).IP
|
||||||
|
rtcpListener.remoteWriteIP = cc.nconn.RemoteAddr().(*net.TCPAddr).IP
|
||||||
rtcpListener.remoteZone = cc.nconn.RemoteAddr().(*net.TCPAddr).Zone
|
rtcpListener.remoteZone = cc.nconn.RemoteAddr().(*net.TCPAddr).Zone
|
||||||
if thRes.ServerPorts != nil {
|
if thRes.ServerPorts != nil {
|
||||||
rtcpListener.remotePort = thRes.ServerPorts[1]
|
rtcpListener.remotePort = thRes.ServerPorts[1]
|
||||||
@@ -1398,14 +1402,16 @@ func (cc *ClientConn) doSetup(
|
|||||||
cct.udpRTCPListener = rtcpListener
|
cct.udpRTCPListener = rtcpListener
|
||||||
|
|
||||||
case ClientProtocolMulticast:
|
case ClientProtocolMulticast:
|
||||||
rtpListener.remoteIP = cc.nconn.RemoteAddr().(*net.TCPAddr).IP
|
rtpListener.remoteReadIP = cc.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.streamType = StreamTypeRTP
|
rtpListener.streamType = StreamTypeRTP
|
||||||
cct.udpRTPListener = rtpListener
|
cct.udpRTPListener = rtpListener
|
||||||
|
|
||||||
rtcpListener.remoteIP = cc.nconn.RemoteAddr().(*net.TCPAddr).IP
|
rtcpListener.remoteReadIP = cc.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
|
||||||
|
@@ -21,7 +21,8 @@ const (
|
|||||||
type clientConnUDPListener struct {
|
type clientConnUDPListener struct {
|
||||||
cc *ClientConn
|
cc *ClientConn
|
||||||
pc *net.UDPConn
|
pc *net.UDPConn
|
||||||
remoteIP net.IP
|
remoteReadIP net.IP
|
||||||
|
remoteWriteIP net.IP
|
||||||
remoteZone string
|
remoteZone string
|
||||||
remotePort int
|
remotePort int
|
||||||
trackID int
|
trackID int
|
||||||
@@ -31,6 +32,7 @@ type clientConnUDPListener struct {
|
|||||||
lastFrameTime *int64
|
lastFrameTime *int64
|
||||||
writeMutex sync.Mutex
|
writeMutex sync.Mutex
|
||||||
|
|
||||||
|
// out
|
||||||
done chan struct{}
|
done chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -148,7 +150,7 @@ func (l *clientConnUDPListener) run() {
|
|||||||
|
|
||||||
uaddr := addr.(*net.UDPAddr)
|
uaddr := addr.(*net.UDPAddr)
|
||||||
|
|
||||||
if !l.remoteIP.Equal(uaddr.IP) || (!isAnyPort(l.remotePort) && l.remotePort != uaddr.Port) {
|
if !l.remoteReadIP.Equal(uaddr.IP) || (!isAnyPort(l.remotePort) && l.remotePort != uaddr.Port) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,7 +169,7 @@ func (l *clientConnUDPListener) run() {
|
|||||||
|
|
||||||
uaddr := addr.(*net.UDPAddr)
|
uaddr := addr.(*net.UDPAddr)
|
||||||
|
|
||||||
if !l.remoteIP.Equal(uaddr.IP) || (!isAnyPort(l.remotePort) && l.remotePort != uaddr.Port) {
|
if !l.remoteReadIP.Equal(uaddr.IP) || (!isAnyPort(l.remotePort) && l.remotePort != uaddr.Port) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,7 +186,7 @@ func (l *clientConnUDPListener) write(buf []byte) error {
|
|||||||
|
|
||||||
l.pc.SetWriteDeadline(time.Now().Add(l.cc.c.WriteTimeout))
|
l.pc.SetWriteDeadline(time.Now().Add(l.cc.c.WriteTimeout))
|
||||||
_, err := l.pc.WriteTo(buf, &net.UDPAddr{
|
_, err := l.pc.WriteTo(buf, &net.UDPAddr{
|
||||||
IP: l.remoteIP,
|
IP: l.remoteWriteIP,
|
||||||
Zone: l.remoteZone,
|
Zone: l.remoteZone,
|
||||||
Port: l.remotePort,
|
Port: l.remotePort,
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user