You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
EasyDarwin/rtsp/udp-server.go

285 lines
7.0 KiB

6 years ago
package rtsp
import (
"bytes"
"fmt"
"log"
6 years ago
"net"
"strconv"
"strings"
6 years ago
"time"
6 years ago
"github.com/penggy/EasyGoLib/utils"
)
type UDPServer struct {
*Session
*RTSPClient
6 years ago
APort int
AConn *net.UDPConn
AControlPort int
AControlConn *net.UDPConn
VPort int
VConn *net.UDPConn
VControlPort int
VControlConn *net.UDPConn
Stoped bool
}
6 years ago
func (s *UDPServer) AddInputBytes(bytes int) {
if s.Session != nil {
s.Session.InBytes += bytes
return
}
if s.RTSPClient != nil {
s.RTSPClient.InBytes += bytes
return
}
panic(fmt.Errorf("session and RTSPClient both nil"))
}
6 years ago
func (s *UDPServer) HandleRTP(pack *RTPPack) {
if s.Session != nil {
for _, v := range s.Session.RTPHandles {
v(pack)
}
return
}
if s.RTSPClient != nil {
for _, v := range s.RTSPClient.RTPHandles {
v(pack)
}
return
}
panic(fmt.Errorf("session and RTSPClient both nil"))
}
6 years ago
func (s *UDPServer) Logger() *log.Logger {
if s.Session != nil {
return s.Session.logger
}
if s.RTSPClient != nil {
return s.RTSPClient.logger
}
panic(fmt.Errorf("session and RTSPClient both nil"))
}
6 years ago
func (s *UDPServer) Stop() {
if s.Stoped {
return
}
s.Stoped = true
if s.AConn != nil {
s.AConn.Close()
s.AConn = nil
}
if s.AControlConn != nil {
s.AControlConn.Close()
s.AControlConn = nil
}
if s.VConn != nil {
s.VConn.Close()
s.VConn = nil
}
if s.VControlConn != nil {
s.VControlConn.Close()
s.VControlConn = nil
}
}
func (s *UDPServer) SetupAudio() (err error) {
4 years ago
var (
logger = s.Logger()
addr *net.UDPAddr
)
if addr, err = net.ResolveUDPAddr("udp", ":0"); err != nil {
6 years ago
return
}
4 years ago
if s.AConn, err = net.ListenUDP("udp", addr); err != nil {
6 years ago
return
}
networkBuffer := utils.Conf().Section("rtsp").Key("network_buffer").MustInt(1048576)
4 years ago
if err = s.AConn.SetReadBuffer(networkBuffer); err != nil {
logger.Printf("udp server audio conn set read buffer error, %v", err)
6 years ago
}
4 years ago
if err = s.AConn.SetWriteBuffer(networkBuffer); err != nil {
logger.Printf("udp server audio conn set write buffer error, %v", err)
6 years ago
}
la := s.AConn.LocalAddr().String()
strPort := la[strings.LastIndex(la, ":")+1:]
s.APort, err = strconv.Atoi(strPort)
if err != nil {
return
}
go func() {
bufUDP := make([]byte, UDP_BUF_SIZE)
logger.Printf("udp server start listen audio port[%d]", s.APort)
defer logger.Printf("udp server stop listen audio port[%d]", s.APort)
6 years ago
timer := time.Unix(0, 0)
6 years ago
for !s.Stoped {
if n, _, err := s.AConn.ReadFromUDP(bufUDP); err == nil {
6 years ago
elapsed := time.Now().Sub(timer)
if elapsed >= 30*time.Second {
logger.Printf("Package recv from AConn.len:%d\n", n)
timer = time.Now()
}
6 years ago
rtpBytes := make([]byte, n)
s.AddInputBytes(n)
6 years ago
copy(rtpBytes, bufUDP)
pack := &RTPPack{
Type: RTP_TYPE_AUDIO,
Buffer: bytes.NewBuffer(rtpBytes),
}
s.HandleRTP(pack)
6 years ago
} else {
logger.Println("udp server read audio pack error", err)
6 years ago
continue
}
}
}()
addr, err = net.ResolveUDPAddr("udp", ":0")
if err != nil {
return
}
s.AControlConn, err = net.ListenUDP("udp", addr)
if err != nil {
return
}
4 years ago
if err = s.AControlConn.SetReadBuffer(networkBuffer); err != nil {
logger.Printf("udp server audio control conn set read buffer error, %v", err)
6 years ago
}
4 years ago
if err = s.AControlConn.SetWriteBuffer(networkBuffer); err != nil {
logger.Printf("udp server audio control conn set write buffer error, %v", err)
6 years ago
}
la = s.AControlConn.LocalAddr().String()
strPort = la[strings.LastIndex(la, ":")+1:]
s.AControlPort, err = strconv.Atoi(strPort)
if err != nil {
return
}
go func() {
bufUDP := make([]byte, UDP_BUF_SIZE)
logger.Printf("udp server start listen audio control port[%d]", s.AControlPort)
defer logger.Printf("udp server stop listen audio control port[%d]", s.AControlPort)
6 years ago
for !s.Stoped {
if n, _, err := s.AControlConn.ReadFromUDP(bufUDP); err == nil {
6 years ago
//logger.Printf("Package recv from AControlConn.len:%d\n", n)
6 years ago
rtpBytes := make([]byte, n)
s.AddInputBytes(n)
6 years ago
copy(rtpBytes, bufUDP)
pack := &RTPPack{
Type: RTP_TYPE_AUDIOCONTROL,
Buffer: bytes.NewBuffer(rtpBytes),
}
s.HandleRTP(pack)
6 years ago
} else {
logger.Println("udp server read audio control pack error", err)
6 years ago
continue
}
}
}()
return
}
func (s *UDPServer) SetupVideo() (err error) {
4 years ago
var (
logger = s.Logger()
addr *net.UDPAddr
)
addr, err = net.ResolveUDPAddr("udp", ":0")
6 years ago
if err != nil {
return
}
s.VConn, err = net.ListenUDP("udp", addr)
if err != nil {
return
}
networkBuffer := utils.Conf().Section("rtsp").Key("network_buffer").MustInt(1048576)
4 years ago
if err = s.VConn.SetReadBuffer(networkBuffer); err != nil {
logger.Printf("udp server video conn set read buffer error, %v", err)
6 years ago
}
4 years ago
if err = s.VConn.SetWriteBuffer(networkBuffer); err != nil {
logger.Printf("udp server video conn set write buffer error, %v", err)
6 years ago
}
la := s.VConn.LocalAddr().String()
strPort := la[strings.LastIndex(la, ":")+1:]
s.VPort, err = strconv.Atoi(strPort)
if err != nil {
return
}
go func() {
bufUDP := make([]byte, UDP_BUF_SIZE)
logger.Printf("udp server start listen video port[%d]", s.VPort)
defer logger.Printf("udp server stop listen video port[%d]", s.VPort)
6 years ago
timer := time.Unix(0, 0)
6 years ago
for !s.Stoped {
4 years ago
var n int
if n, _, err = s.VConn.ReadFromUDP(bufUDP); err == nil {
6 years ago
elapsed := time.Now().Sub(timer)
if elapsed >= 30*time.Second {
logger.Printf("Package recv from VConn.len:%d\n", n)
timer = time.Now()
}
6 years ago
rtpBytes := make([]byte, n)
s.AddInputBytes(n)
6 years ago
copy(rtpBytes, bufUDP)
pack := &RTPPack{
Type: RTP_TYPE_VIDEO,
Buffer: bytes.NewBuffer(rtpBytes),
}
s.HandleRTP(pack)
6 years ago
} else {
logger.Println("udp server read video pack error", err)
6 years ago
continue
}
}
}()
addr, err = net.ResolveUDPAddr("udp", ":0")
if err != nil {
return
}
s.VControlConn, err = net.ListenUDP("udp", addr)
if err != nil {
return
}
4 years ago
if err = s.VControlConn.SetReadBuffer(networkBuffer); err != nil {
logger.Printf("udp server video control conn set read buffer error, %v", err)
6 years ago
}
4 years ago
if err = s.VControlConn.SetWriteBuffer(networkBuffer); err != nil {
logger.Printf("udp server video control conn set write buffer error, %v", err)
6 years ago
}
la = s.VControlConn.LocalAddr().String()
strPort = la[strings.LastIndex(la, ":")+1:]
s.VControlPort, err = strconv.Atoi(strPort)
if err != nil {
return
}
go func() {
bufUDP := make([]byte, UDP_BUF_SIZE)
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)
6 years ago
for !s.Stoped {
4 years ago
var n int
if n, _, err = s.VControlConn.ReadFromUDP(bufUDP); err == nil {
6 years ago
//logger.Printf("Package recv from VControlConn.len:%d\n", n)
6 years ago
rtpBytes := make([]byte, n)
s.AddInputBytes(n)
6 years ago
copy(rtpBytes, bufUDP)
pack := &RTPPack{
Type: RTP_TYPE_VIDEOCONTROL,
Buffer: bytes.NewBuffer(rtpBytes),
}
s.HandleRTP(pack)
6 years ago
} else {
logger.Println("udp server read video control pack error", err)
6 years ago
continue
}
}
}()
return
}