Browse Source

修复bug

pull/205/head
mask-pp 4 years ago
parent
commit
360bc8e736
  1. 6
      rtsp/rtsp-client.go
  2. 24
      rtsp/rtsp-server.go
  3. 40
      rtsp/udp-server.go

6
rtsp/rtsp-client.go

@ -660,8 +660,10 @@ func (client *RTSPClient) Request(method string, headers map[string]string) (*Re
} }
func (client *RTSPClient) RequestNoResp(method string, headers map[string]string) (err error) { func (client *RTSPClient) RequestNoResp(method string, headers map[string]string) (err error) {
l, err := url.Parse(client.URL) var (
if err != nil { l *url.URL
)
if l, err = url.Parse(client.URL); err != nil {
return fmt.Errorf("Url parse error:%v", err) return fmt.Errorf("Url parse error:%v", err)
} }
l.User = nil l.User = nil

24
rtsp/rtsp-server.go

@ -41,13 +41,15 @@ func GetServer() *Server {
} }
func (server *Server) Start() (err error) { func (server *Server) Start() (err error) {
logger := server.logger var (
addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf(":%d", server.TCPPort)) logger = server.logger
if err != nil { addr *net.TCPAddr
listener *net.TCPListener
)
if addr, err = net.ResolveTCPAddr("tcp", fmt.Sprintf(":%d", server.TCPPort)); err != nil {
return return
} }
listener, err := net.ListenTCP("tcp", addr) if listener, err = net.ListenTCP("tcp", addr); err != nil {
if err != nil {
return return
} }
@ -57,7 +59,7 @@ func (server *Server) Start() (err error) {
ts_duration_second := utils.Conf().Section("rtsp").Key("ts_duration_second").MustInt(6) ts_duration_second := utils.Conf().Section("rtsp").Key("ts_duration_second").MustInt(6)
SaveStreamToLocal := false SaveStreamToLocal := false
if (len(ffmpeg) > 0) && localRecord > 0 && len(m3u8_dir_path) > 0 { if (len(ffmpeg) > 0) && localRecord > 0 && len(m3u8_dir_path) > 0 {
err := utils.EnsureDir(m3u8_dir_path) err = utils.EnsureDir(m3u8_dir_path)
if err != nil { if err != nil {
logger.Printf("Create m3u8_dir_path[%s] err:%v.", m3u8_dir_path, err) logger.Printf("Create m3u8_dir_path[%s] err:%v.", m3u8_dir_path, err)
} else { } else {
@ -151,16 +153,18 @@ func (server *Server) Start() (err error) {
logger.Println("rtsp server start on", server.TCPPort) logger.Println("rtsp server start on", server.TCPPort)
networkBuffer := utils.Conf().Section("rtsp").Key("network_buffer").MustInt(1048576) networkBuffer := utils.Conf().Section("rtsp").Key("network_buffer").MustInt(1048576)
for !server.Stoped { for !server.Stoped {
conn, err := server.TCPListener.Accept() var (
if err != nil { conn net.Conn
)
if conn, err = server.TCPListener.Accept(); err != nil {
logger.Println(err) logger.Println(err)
continue continue
} }
if tcpConn, ok := conn.(*net.TCPConn); ok { if tcpConn, ok := conn.(*net.TCPConn); ok {
if err := tcpConn.SetReadBuffer(networkBuffer); err != nil { if err = tcpConn.SetReadBuffer(networkBuffer); err != nil {
logger.Printf("rtsp server conn set read buffer error, %v", err) logger.Printf("rtsp server conn set read buffer error, %v", err)
} }
if err := tcpConn.SetWriteBuffer(networkBuffer); err != nil { if err = tcpConn.SetWriteBuffer(networkBuffer); err != nil {
logger.Printf("rtsp server conn set write buffer error, %v", err) logger.Printf("rtsp server conn set write buffer error, %v", err)
} }
} }

40
rtsp/udp-server.go

@ -91,20 +91,21 @@ func (s *UDPServer) Stop() {
} }
func (s *UDPServer) SetupAudio() (err error) { func (s *UDPServer) SetupAudio() (err error) {
logger := s.Logger() var (
addr, err := net.ResolveUDPAddr("udp", ":0") logger = s.Logger()
if err != nil { addr *net.UDPAddr
)
if addr, err = net.ResolveUDPAddr("udp", ":0"); err != nil {
return return
} }
s.AConn, err = net.ListenUDP("udp", addr) if s.AConn, err = net.ListenUDP("udp", addr); err != nil {
if err != nil {
return return
} }
networkBuffer := utils.Conf().Section("rtsp").Key("network_buffer").MustInt(1048576) networkBuffer := utils.Conf().Section("rtsp").Key("network_buffer").MustInt(1048576)
if err := s.AConn.SetReadBuffer(networkBuffer); err != nil { if err = s.AConn.SetReadBuffer(networkBuffer); err != nil {
logger.Printf("udp server audio conn set read buffer error, %v", err) logger.Printf("udp server audio conn set read buffer error, %v", err)
} }
if err := s.AConn.SetWriteBuffer(networkBuffer); err != nil { if err = s.AConn.SetWriteBuffer(networkBuffer); err != nil {
logger.Printf("udp server audio conn set write buffer error, %v", err) logger.Printf("udp server audio conn set write buffer error, %v", err)
} }
la := s.AConn.LocalAddr().String() la := s.AConn.LocalAddr().String()
@ -147,10 +148,10 @@ func (s *UDPServer) SetupAudio() (err error) {
if err != nil { if err != nil {
return return
} }
if err := s.AControlConn.SetReadBuffer(networkBuffer); err != nil { if err = s.AControlConn.SetReadBuffer(networkBuffer); err != nil {
logger.Printf("udp server audio control conn set read buffer error, %v", err) logger.Printf("udp server audio control conn set read buffer error, %v", err)
} }
if err := s.AControlConn.SetWriteBuffer(networkBuffer); err != nil { if err = s.AControlConn.SetWriteBuffer(networkBuffer); err != nil {
logger.Printf("udp server audio control conn set write buffer error, %v", err) logger.Printf("udp server audio control conn set write buffer error, %v", err)
} }
la = s.AControlConn.LocalAddr().String() la = s.AControlConn.LocalAddr().String()
@ -184,8 +185,11 @@ func (s *UDPServer) SetupAudio() (err error) {
} }
func (s *UDPServer) SetupVideo() (err error) { func (s *UDPServer) SetupVideo() (err error) {
logger := s.Logger() var (
addr, err := net.ResolveUDPAddr("udp", ":0") logger = s.Logger()
addr *net.UDPAddr
)
addr, err = net.ResolveUDPAddr("udp", ":0")
if err != nil { if err != nil {
return return
} }
@ -194,10 +198,10 @@ func (s *UDPServer) SetupVideo() (err error) {
return return
} }
networkBuffer := utils.Conf().Section("rtsp").Key("network_buffer").MustInt(1048576) networkBuffer := utils.Conf().Section("rtsp").Key("network_buffer").MustInt(1048576)
if err := s.VConn.SetReadBuffer(networkBuffer); err != nil { if err = s.VConn.SetReadBuffer(networkBuffer); err != nil {
logger.Printf("udp server video conn set read buffer error, %v", err) logger.Printf("udp server video conn set read buffer error, %v", err)
} }
if err := s.VConn.SetWriteBuffer(networkBuffer); err != nil { if err = s.VConn.SetWriteBuffer(networkBuffer); err != nil {
logger.Printf("udp server video conn set write buffer error, %v", err) logger.Printf("udp server video conn set write buffer error, %v", err)
} }
la := s.VConn.LocalAddr().String() la := s.VConn.LocalAddr().String()
@ -212,7 +216,8 @@ func (s *UDPServer) SetupVideo() (err error) {
defer logger.Printf("udp server stop listen video port[%d]", s.VPort) defer logger.Printf("udp server stop listen video port[%d]", s.VPort)
timer := time.Unix(0, 0) timer := time.Unix(0, 0)
for !s.Stoped { for !s.Stoped {
if n, _, err := s.VConn.ReadFromUDP(bufUDP); err == nil { var n int
if n, _, err = s.VConn.ReadFromUDP(bufUDP); err == nil {
elapsed := time.Now().Sub(timer) elapsed := time.Now().Sub(timer)
if elapsed >= 30*time.Second { if elapsed >= 30*time.Second {
logger.Printf("Package recv from VConn.len:%d\n", n) logger.Printf("Package recv from VConn.len:%d\n", n)
@ -241,10 +246,10 @@ func (s *UDPServer) SetupVideo() (err error) {
if err != nil { if err != nil {
return return
} }
if err := s.VControlConn.SetReadBuffer(networkBuffer); err != nil { if err = s.VControlConn.SetReadBuffer(networkBuffer); err != nil {
logger.Printf("udp server video control conn set read buffer error, %v", err) logger.Printf("udp server video control conn set read buffer error, %v", err)
} }
if err := s.VControlConn.SetWriteBuffer(networkBuffer); err != nil { if err = s.VControlConn.SetWriteBuffer(networkBuffer); err != nil {
logger.Printf("udp server video control conn set write buffer error, %v", err) logger.Printf("udp server video control conn set write buffer error, %v", err)
} }
la = s.VControlConn.LocalAddr().String() la = s.VControlConn.LocalAddr().String()
@ -258,7 +263,8 @@ func (s *UDPServer) SetupVideo() (err error) {
logger.Printf("udp server start listen video control port[%d]", s.VControlPort) logger.Printf("udp server start listen video control port[%d]", s.VControlPort)
defer logger.Printf("udp server stop listen video control port[%d]", s.VControlPort) defer logger.Printf("udp server stop listen video control port[%d]", s.VControlPort)
for !s.Stoped { for !s.Stoped {
if n, _, err := s.VControlConn.ReadFromUDP(bufUDP); err == nil { var n int
if n, _, err = s.VControlConn.ReadFromUDP(bufUDP); err == nil {
//logger.Printf("Package recv from VControlConn.len:%d\n", n) //logger.Printf("Package recv from VControlConn.len:%d\n", n)
rtpBytes := make([]byte, n) rtpBytes := make([]byte, n)
s.AddInputBytes(n) s.AddInputBytes(n)

Loading…
Cancel
Save