Skip to content

Commit

Permalink
Support parse listen endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Sep 5, 2024
1 parent 91fc387 commit 0556fe7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
17 changes: 10 additions & 7 deletions proxy/rtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,12 @@ func (v *rtcServer) proxyApiToBackend(

// Replace the WebRTC UDP port in answer.
localSDPAnswer := string(b)
for _, port := range backend.RTC {
for _, endpoint := range backend.RTC {
_, _, port, err := parseListenEndpoint(endpoint)
if err != nil {
return errors.Wrapf(err, "parse endpoint %v", endpoint)
}

from := fmt.Sprintf(" %v typ host", port)
to := fmt.Sprintf(" %v typ host", envWebRTCServer())
localSDPAnswer = strings.Replace(localSDPAnswer, from, to, -1)
Expand Down Expand Up @@ -425,16 +430,14 @@ func (v *RTCConnection) connectBackend(ctx context.Context) error {
return errors.Errorf("no udp server")
}

var udpPort int
if iv, err := strconv.ParseInt(backend.RTC[0], 10, 64); err != nil {
return errors.Wrapf(err, "parse udp port %v", backend.RTC[0])
} else {
udpPort = int(iv)
_, _, udpPort, err := parseListenEndpoint(backend.RTC[0])
if err != nil {
return errors.Wrapf(err, "parse endpoint %v", backend.RTC[0])
}

// Connect to backend SRS server via UDP client.
// TODO: FIXME: Support close the connection when timeout or DTLS alert.
backendAddr := net.UDPAddr{IP: net.ParseIP(backend.IP), Port: udpPort}
backendAddr := net.UDPAddr{IP: net.ParseIP(backend.IP), Port: int(udpPort)}
if backendUDP, err := net.DialUDP("udp", nil, &backendAddr); err != nil {
return errors.Wrapf(err, "dial udp to %v", backendAddr)
} else {
Expand Down
27 changes: 27 additions & 0 deletions proxy/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"path"
"reflect"
"regexp"
"strconv"
"strings"
"syscall"
"time"
Expand Down Expand Up @@ -247,3 +248,29 @@ func parseSRTStreamID(sid string) (host, resource string, err error) {

return host, resource, nil
}

// parseListenEndpoint parse the listen endpoint as:
// port The tcp listen port, like 1935.
// protocol://ip:port The listen endpoint, like tcp://:1935 or tcp://0.0.0.0:1935
func parseListenEndpoint(ep string) (protocol string, ip net.IP, port uint16, err error) {
// If no colon in ep, it's port in string.
if !strings.Contains(ep, ":") {
if p, err := strconv.Atoi(ep); err != nil {
return "", nil, 0, errors.Wrapf(err, "parse port %v", ep)
} else {
return "tcp", nil, uint16(p), nil
}
}

// Must be protocol://ip:port schema.
parts := strings.Split(ep, ":")
if len(parts) != 3 {
return "", nil, 0, errors.Errorf("invalid endpoint %v", ep)
}

if p, err := strconv.Atoi(parts[2]); err != nil {
return "", nil, 0, errors.Wrapf(err, "parse port %v", parts[2])
} else {
return parts[0], net.ParseIP(parts[1]), uint16(p), nil
}
}

0 comments on commit 0556fe7

Please sign in to comment.