mirror of
https://github.com/bluenviron/mediamtx.git
synced 2026-01-09 11:52:02 -08:00
simplify code
This commit is contained in:
parent
7e45b0e7c2
commit
9d608ef19d
2 changed files with 14 additions and 10 deletions
13
source.go
13
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()
|
||||
|
|
|
|||
11
utils.go
11
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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue