simplify code

This commit is contained in:
aler9 2020-09-15 10:40:32 +02:00
parent 7e45b0e7c2
commit 9d608ef19d
2 changed files with 14 additions and 10 deletions

View file

@ -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 {
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()

View file

@ -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
}