mirror of
https://github.com/aler9/gortsplib
synced 2025-10-04 14:52:46 +08:00
rename ServerConf into Server
This commit is contained in:
129
server.go
129
server.go
@@ -1,6 +1,7 @@
|
||||
package gortsplib
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
@@ -23,87 +24,131 @@ func extractPort(address string) (int, error) {
|
||||
|
||||
// Server is a RTSP server.
|
||||
type Server struct {
|
||||
conf ServerConf
|
||||
// a TLS configuration to accept TLS (RTSPS) connections.
|
||||
TLSConfig *tls.Config
|
||||
|
||||
// a port to send and receive UDP/RTP packets.
|
||||
// If UDPRTPAddress and UDPRTCPAddress are != "", the server can accept and send UDP streams.
|
||||
UDPRTPAddress string
|
||||
|
||||
// a port to send and receive UDP/RTCP packets.
|
||||
// If UDPRTPAddress and UDPRTCPAddress are != "", the server can accept and send UDP streams.
|
||||
UDPRTCPAddress string
|
||||
|
||||
// timeout of read operations.
|
||||
// It defaults to 10 seconds
|
||||
ReadTimeout time.Duration
|
||||
|
||||
// timeout of write operations.
|
||||
// It defaults to 10 seconds
|
||||
WriteTimeout time.Duration
|
||||
|
||||
// read buffer count.
|
||||
// If greater than 1, allows to pass buffers to routines different than the one
|
||||
// that is reading frames.
|
||||
// It also allows to buffer routed frames and mitigate network fluctuations
|
||||
// that are particularly high when using UDP.
|
||||
// It defaults to 512
|
||||
ReadBufferCount int
|
||||
|
||||
// read buffer size.
|
||||
// This must be touched only when the server reports problems about buffer sizes.
|
||||
// It defaults to 2048.
|
||||
ReadBufferSize int
|
||||
|
||||
// function used to initialize the TCP listener.
|
||||
// It defaults to net.Listen
|
||||
Listen func(network string, address string) (net.Listener, error)
|
||||
|
||||
receiverReportPeriod time.Duration
|
||||
|
||||
tcpListener net.Listener
|
||||
udpRTPListener *serverUDPListener
|
||||
udpRTCPListener *serverUDPListener
|
||||
}
|
||||
|
||||
func newServer(conf ServerConf, address string) (*Server, error) {
|
||||
if conf.ReadTimeout == 0 {
|
||||
conf.ReadTimeout = 10 * time.Second
|
||||
// Serve starts listening on the given address.
|
||||
func (s *Server) Serve(address string) error {
|
||||
if s.ReadTimeout == 0 {
|
||||
s.ReadTimeout = 10 * time.Second
|
||||
}
|
||||
if conf.WriteTimeout == 0 {
|
||||
conf.WriteTimeout = 10 * time.Second
|
||||
if s.WriteTimeout == 0 {
|
||||
s.WriteTimeout = 10 * time.Second
|
||||
}
|
||||
if conf.ReadBufferCount == 0 {
|
||||
conf.ReadBufferCount = 512
|
||||
if s.ReadBufferCount == 0 {
|
||||
s.ReadBufferCount = 512
|
||||
}
|
||||
if conf.ReadBufferSize == 0 {
|
||||
conf.ReadBufferSize = 2048
|
||||
if s.ReadBufferSize == 0 {
|
||||
s.ReadBufferSize = 2048
|
||||
}
|
||||
|
||||
if conf.Listen == nil {
|
||||
conf.Listen = net.Listen
|
||||
if s.Listen == nil {
|
||||
s.Listen = net.Listen
|
||||
}
|
||||
|
||||
if conf.receiverReportPeriod == 0 {
|
||||
conf.receiverReportPeriod = 10 * time.Second
|
||||
if s.receiverReportPeriod == 0 {
|
||||
s.receiverReportPeriod = 10 * time.Second
|
||||
}
|
||||
|
||||
if conf.TLSConfig != nil && conf.UDPRTPAddress != "" {
|
||||
return nil, fmt.Errorf("TLS can't be used together with UDP")
|
||||
if s.TLSConfig != nil && s.UDPRTPAddress != "" {
|
||||
return fmt.Errorf("TLS can't be used together with UDP")
|
||||
}
|
||||
|
||||
if (conf.UDPRTPAddress != "" && conf.UDPRTCPAddress == "") ||
|
||||
(conf.UDPRTPAddress == "" && conf.UDPRTCPAddress != "") {
|
||||
return nil, fmt.Errorf("UDPRTPAddress and UDPRTCPAddress must be used together")
|
||||
if (s.UDPRTPAddress != "" && s.UDPRTCPAddress == "") ||
|
||||
(s.UDPRTPAddress == "" && s.UDPRTCPAddress != "") {
|
||||
return fmt.Errorf("UDPRTPAddress and UDPRTCPAddress must be used together")
|
||||
}
|
||||
|
||||
s := &Server{
|
||||
conf: conf,
|
||||
}
|
||||
|
||||
if conf.UDPRTPAddress != "" {
|
||||
rtpPort, err := extractPort(conf.UDPRTPAddress)
|
||||
if s.UDPRTPAddress != "" {
|
||||
rtpPort, err := extractPort(s.UDPRTPAddress)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
rtcpPort, err := extractPort(conf.UDPRTCPAddress)
|
||||
rtcpPort, err := extractPort(s.UDPRTCPAddress)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
if (rtpPort % 2) != 0 {
|
||||
return nil, fmt.Errorf("RTP port must be even")
|
||||
return fmt.Errorf("RTP port must be even")
|
||||
}
|
||||
|
||||
if rtcpPort != (rtpPort + 1) {
|
||||
return nil, fmt.Errorf("RTCP and RTP ports must be consecutive")
|
||||
return fmt.Errorf("RTCP and RTP ports must be consecutive")
|
||||
}
|
||||
|
||||
s.udpRTPListener, err = newServerUDPListener(conf, conf.UDPRTPAddress, StreamTypeRTP)
|
||||
s.udpRTPListener, err = newServerUDPListener(s, s.UDPRTPAddress, StreamTypeRTP)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
s.udpRTCPListener, err = newServerUDPListener(conf, conf.UDPRTCPAddress, StreamTypeRTCP)
|
||||
s.udpRTCPListener, err = newServerUDPListener(s, s.UDPRTCPAddress, StreamTypeRTCP)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
var err error
|
||||
s.tcpListener, err = conf.Listen("tcp", address)
|
||||
s.tcpListener, err = s.Listen("tcp", address)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Accept accepts a connection.
|
||||
func (s *Server) Accept() (*ServerConn, error) {
|
||||
nconn, err := s.tcpListener.Accept()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s, nil
|
||||
return newServerConn(s, nconn), nil
|
||||
}
|
||||
|
||||
// Close closes the server.
|
||||
// Close closes all the server resources.
|
||||
func (s *Server) Close() error {
|
||||
s.tcpListener.Close()
|
||||
|
||||
@@ -117,13 +162,3 @@ func (s *Server) Close() error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Accept accepts a connection.
|
||||
func (s *Server) Accept() (*ServerConn, error) {
|
||||
nconn, err := s.tcpListener.Accept()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return newServerConn(s.conf, s.udpRTPListener, s.udpRTCPListener, nconn), nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user