mirror of
https://github.com/aler9/gortsplib
synced 2025-10-05 23:26:54 +08:00
add multicast.InterfaceForSource (#414)
This commit is contained in:
@@ -2,7 +2,6 @@ package gortsplib
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"fmt"
|
|
||||||
"math/big"
|
"math/big"
|
||||||
"net"
|
"net"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -25,35 +24,6 @@ func randInRange(max int) (int, error) {
|
|||||||
return int(n.Int64()), nil
|
return int(n.Int64()), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func findMulticastInterfaceForSource(ip net.IP) (*net.Interface, error) {
|
|
||||||
if ip.Equal(net.ParseIP("127.0.0.1")) {
|
|
||||||
return nil, fmt.Errorf("IP 127.0.0.1 can't be used as source of a multicast stream. Use the LAN IP of your PC")
|
|
||||||
}
|
|
||||||
|
|
||||||
intfs, err := net.Interfaces()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, intf := range intfs {
|
|
||||||
if (intf.Flags & net.FlagMulticast) == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
addrs, err := intf.Addrs()
|
|
||||||
if err == nil {
|
|
||||||
for _, addr := range addrs {
|
|
||||||
_, ipnet, err := net.ParseCIDR(addr.String())
|
|
||||||
if err == nil && ipnet.Contains(ip) {
|
|
||||||
return &intf, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, fmt.Errorf("found no interface that is multicast-capable and can communicate with IP %v", ip)
|
|
||||||
}
|
|
||||||
|
|
||||||
type clientUDPListener struct {
|
type clientUDPListener struct {
|
||||||
c *Client
|
c *Client
|
||||||
pc net.PacketConn
|
pc net.PacketConn
|
||||||
@@ -118,7 +88,7 @@ func newClientUDPListener(
|
|||||||
) (*clientUDPListener, error) {
|
) (*clientUDPListener, error) {
|
||||||
var pc packetConn
|
var pc packetConn
|
||||||
if multicastEnable {
|
if multicastEnable {
|
||||||
intf, err := findMulticastInterfaceForSource(multicastSourceIP)
|
intf, err := multicast.InterfaceForSource(multicastSourceIP)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@@ -2,6 +2,7 @@
|
|||||||
package multicast
|
package multicast
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -10,3 +11,33 @@ type Conn interface {
|
|||||||
net.PacketConn
|
net.PacketConn
|
||||||
SetReadBuffer(int) error
|
SetReadBuffer(int) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// InterfaceForSource returns a multicast-capable interface that can communicate with given IP.
|
||||||
|
func InterfaceForSource(ip net.IP) (*net.Interface, error) {
|
||||||
|
if ip.Equal(net.ParseIP("127.0.0.1")) {
|
||||||
|
return nil, fmt.Errorf("IP 127.0.0.1 can't be used as source of a multicast stream. Use the LAN IP of your PC")
|
||||||
|
}
|
||||||
|
|
||||||
|
intfs, err := net.Interfaces()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, intf := range intfs {
|
||||||
|
if (intf.Flags & net.FlagMulticast) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
addrs, err := intf.Addrs()
|
||||||
|
if err == nil {
|
||||||
|
for _, addr := range addrs {
|
||||||
|
_, ipnet, err := net.ParseCIDR(addr.String())
|
||||||
|
if err == nil && ipnet.Contains(ip) {
|
||||||
|
return &intf, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("found no interface that is multicast-capable and can communicate with IP %v", ip)
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user