Browse Source

simplify code

pull/97/head
aler9 5 years ago
parent
commit
9d608ef19d
  1. 13
      source.go
  2. 11
      utils.go

13
source.go

@ -2,8 +2,6 @@ package main @@ -2,8 +2,6 @@ package main
import (
"math/rand"
"net"
"os"
"sync"
"time"
@ -195,20 +193,15 @@ func (s *source) runUDP(conn *gortsplib.ConnClient) bool { @@ -195,20 +193,15 @@ func (s *source) runUDP(conn *gortsplib.ConnClient) bool {
for _, track := range s.tracks {
for {
// choose two consecutive ports in range 65536-10000
// choose two consecutive ports in range 65535-10000
// rtp must be even and rtcp odd
rtpPort := (rand.Intn((65535-10000)/2) * 2) + 10000
rtcpPort := rtpPort + 1
rtpRead, rtcpRead, _, err := conn.SetupUDP(s.confp.sourceUrl, track, rtpPort, rtcpPort)
if err != nil {
// retry if it's a bind error
if nerr, ok := err.(*net.OpError); ok {
if serr, ok := nerr.Err.(*os.SyscallError); ok {
if serr.Syscall == "bind" {
continue
}
}
if isBindError(err) {
continue // retry
}
conn.Close()

11
utils.go

@ -159,3 +159,14 @@ func startExternalCommand(cmdstr string, pathName string) (*exec.Cmd, error) { @@ -159,3 +159,14 @@ func startExternalCommand(cmdstr string, pathName string) (*exec.Cmd, error) {
return cmd, nil
}
func isBindError(err error) bool {
if nerr, ok := err.(*net.OpError); ok {
if serr, ok := nerr.Err.(*os.SyscallError); ok {
if serr.Syscall == "bind" {
return true
}
}
}
return false
}

Loading…
Cancel
Save