From 9d608ef19d094d57b8799b4b4d0391cd1adc123b Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Tue, 15 Sep 2020 10:40:32 +0200 Subject: [PATCH] simplify code --- source.go | 13 +++---------- utils.go | 11 +++++++++++ 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/source.go b/source.go index 9903941c..baf2dfea 100644 --- a/source.go +++ b/source.go @@ -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() diff --git a/utils.go b/utils.go index 1397b2c2..0db857ed 100644 --- a/utils.go +++ b/utils.go @@ -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 +}