diff --git a/examples/server-tls/main.go b/examples/server-tls/main.go index 1166321b..60629fae 100644 --- a/examples/server-tls/main.go +++ b/examples/server-tls/main.go @@ -157,11 +157,12 @@ func main() { // configure server s := &gortsplib.Server{ - Handler: &serverHandler{}, - TLSConfig: &tls.Config{Certificates: []tls.Certificate{cert}}, + Handler: &serverHandler{}, + TLSConfig: &tls.Config{Certificates: []tls.Certificate{cert}}, + RTSPAddress: ":8554", } // start server and wait until a fatal error log.Printf("server is ready") - panic(s.StartAndWait(":8554")) + panic(s.StartAndWait()) } diff --git a/examples/server/main.go b/examples/server/main.go index 5b8c249d..5ecc84d2 100644 --- a/examples/server/main.go +++ b/examples/server/main.go @@ -149,6 +149,7 @@ func main() { // configure server s := &gortsplib.Server{ Handler: &serverHandler{}, + RTSPAddress: ":8554", UDPRTPAddress: ":8000", UDPRTCPAddress: ":8001", MulticastIPRange: "224.1.0.0/16", @@ -158,5 +159,5 @@ func main() { // start server and wait until a fatal error log.Printf("server is ready") - panic(s.StartAndWait(":8554")) + panic(s.StartAndWait()) } diff --git a/server.go b/server.go index 0d387bed..5b8aea43 100644 --- a/server.go +++ b/server.go @@ -80,6 +80,9 @@ type Server struct { // timeout of write operations. // It defaults to 10 seconds WriteTimeout time.Duration + // the RTSP address of the server, to accept connections and send and receive + // packets with the TCP transport. + RTSPAddress string // a TLS configuration to accept TLS (RTSPS) connections. TLSConfig *tls.Config // a port to send and receive RTP packets with the UDP transport. @@ -151,8 +154,8 @@ type Server struct { streamMulticastIP chan streamMulticastIPReq } -// Start starts listening on the given address. -func (s *Server) Start(address string) error { +// Start starts the server. +func (s *Server) Start() error { // RTSP parameters if s.ReadTimeout == 0 { s.ReadTimeout = 10 * time.Second @@ -191,6 +194,10 @@ func (s *Server) Start(address string) error { return fmt.Errorf("TLS can't be used with UDP-multicast") } + if s.RTSPAddress == "" { + return fmt.Errorf("RTSPAddress not provided") + } + if (s.UDPRTPAddress != "" && s.UDPRTCPAddress == "") || (s.UDPRTPAddress == "" && s.UDPRTCPAddress != "") { return fmt.Errorf("UDPRTPAddress and UDPRTCPAddress must be used together") @@ -276,7 +283,7 @@ func (s *Server) Start(address string) error { } var err error - s.tcpListener, err = s.Listen("tcp", address) + s.tcpListener, err = s.Listen("tcp", s.RTSPAddress) if err != nil { if s.udpRTPListener != nil { s.udpRTPListener.close() @@ -462,8 +469,8 @@ func (s *Server) run() { } // StartAndWait starts the server and waits until a fatal error. -func (s *Server) StartAndWait(address string) error { - err := s.Start(address) +func (s *Server) StartAndWait() error { + err := s.Start() if err != nil { return err } diff --git a/server_publish_test.go b/server_publish_test.go index 20b556a4..e52f58c1 100644 --- a/server_publish_test.go +++ b/server_publish_test.go @@ -159,9 +159,10 @@ func TestServerPublishErrorAnnounce(t *testing.T) { }, nil }, }, + RTSPAddress: "localhost:8554", } - err := s.Start("localhost:8554") + err := s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() @@ -246,9 +247,10 @@ func TestServerPublishSetupPath(t *testing.T) { }, nil, nil }, }, + RTSPAddress: "localhost:8554", } - err := s.Start("localhost:8554") + err := s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() @@ -342,9 +344,10 @@ func TestServerPublishErrorSetupDifferentPaths(t *testing.T) { }, nil, nil }, }, + RTSPAddress: "localhost:8554", } - err := s.Start("localhost:8554") + err := s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() @@ -425,9 +428,10 @@ func TestServerPublishErrorSetupTrackTwice(t *testing.T) { }, nil, nil }, }, + RTSPAddress: "localhost:8554", } - err := s.Start("localhost:8554") + err := s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() @@ -525,9 +529,10 @@ func TestServerPublishErrorRecordPartialTracks(t *testing.T) { }, nil }, }, + RTSPAddress: "localhost:8554", } - err := s.Start("localhost:8554") + err := s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() @@ -654,6 +659,7 @@ func TestServerPublish(t *testing.T) { ctx.Session.WritePacketRTCP(0, []byte{0x09, 0x0A, 0x0B, 0x0C}) }, }, + RTSPAddress: "localhost:8554", } switch transport { @@ -667,7 +673,7 @@ func TestServerPublish(t *testing.T) { s.TLSConfig = &tls.Config{Certificates: []tls.Certificate{cert}} } - err := s.Start("localhost:8554") + err := s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() @@ -967,9 +973,10 @@ func TestServerPublishErrorInvalidProtocol(t *testing.T) { }, UDPRTPAddress: "127.0.0.1:8000", UDPRTCPAddress: "127.0.0.1:8001", + RTSPAddress: "localhost:8554", } - err := s.Start("localhost:8554") + err := s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() @@ -1069,9 +1076,10 @@ func TestServerPublishRTCPReport(t *testing.T) { }, }, receiverReportPeriod: 1 * time.Second, + RTSPAddress: "localhost:8554", } - err := s.Start("localhost:8554") + err := s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() @@ -1224,6 +1232,7 @@ func TestServerPublishTimeout(t *testing.T) { }, }, ReadTimeout: 1 * time.Second, + RTSPAddress: "localhost:8554", } if transport == "udp" { @@ -1231,7 +1240,7 @@ func TestServerPublishTimeout(t *testing.T) { s.UDPRTCPAddress = "127.0.0.1:8001" } - err := s.Start("localhost:8554") + err := s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() @@ -1353,6 +1362,7 @@ func TestServerPublishWithoutTeardown(t *testing.T) { }, }, ReadTimeout: 1 * time.Second, + RTSPAddress: "localhost:8554", } if transport == "udp" { @@ -1360,7 +1370,7 @@ func TestServerPublishWithoutTeardown(t *testing.T) { s.UDPRTCPAddress = "127.0.0.1:8001" } - err := s.Start("localhost:8554") + err := s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() @@ -1474,9 +1484,10 @@ func TestServerPublishUDPChangeConn(t *testing.T) { }, UDPRTPAddress: "127.0.0.1:8000", UDPRTCPAddress: "127.0.0.1:8001", + RTSPAddress: "localhost:8554", } - err := s.Start("localhost:8554") + err := s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() diff --git a/server_read_test.go b/server_read_test.go index f69fa08e..b3e76ecd 100644 --- a/server_read_test.go +++ b/server_read_test.go @@ -105,9 +105,10 @@ func TestServerReadSetupPath(t *testing.T) { }, stream, nil }, }, + RTSPAddress: "localhost:8554", } - err = s.Start("localhost:8554") + err = s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() @@ -170,9 +171,10 @@ func TestServerReadSetupErrors(t *testing.T) { }, stream, nil }, }, + RTSPAddress: "localhost:8554", } - err = s.Start("localhost:8554") + err = s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() @@ -273,6 +275,8 @@ func TestServerRead(t *testing.T) { counter := uint64(0) + listenIP := multicastCapableIP(t) + s := &Server{ Handler: &testServerHandler{ onConnOpen: func(ctx *ServerHandlerOnConnOpenCtx) { @@ -319,6 +323,7 @@ func TestServerRead(t *testing.T) { }, nil }, }, + RTSPAddress: listenIP + ":8554", } switch transport { @@ -337,8 +342,7 @@ func TestServerRead(t *testing.T) { s.TLSConfig = &tls.Config{Certificates: []tls.Certificate{cert}} } - listenIP := multicastCapableIP(t) - err = s.Start(listenIP + ":8554") + err = s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() @@ -595,9 +599,10 @@ func TestServerReadNonStandardFrameSize(t *testing.T) { }, nil }, }, + RTSPAddress: "localhost:8554", } - err = s.Start("localhost:8554") + err = s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() @@ -767,9 +772,10 @@ func TestServerReadPlayPlay(t *testing.T) { }, UDPRTPAddress: "127.0.0.1:8000", UDPRTCPAddress: "127.0.0.1:8001", + RTSPAddress: "localhost:8554", } - err = s.Start("localhost:8554") + err = s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() @@ -875,9 +881,10 @@ func TestServerReadPlayPausePlay(t *testing.T) { }, nil }, }, + RTSPAddress: "localhost:8554", } - err = s.Start("localhost:8554") + err = s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() @@ -990,9 +997,10 @@ func TestServerReadPlayPausePause(t *testing.T) { }, nil }, }, + RTSPAddress: "localhost:8554", } - err = s.Start("localhost:8554") + err = s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() @@ -1100,12 +1108,12 @@ func TestServerReadTimeout(t *testing.T) { }, ReadTimeout: 1 * time.Second, closeSessionAfterNoRequestsFor: 1 * time.Second, + UDPRTPAddress: "127.0.0.1:8000", + UDPRTCPAddress: "127.0.0.1:8001", + RTSPAddress: "localhost:8554", } - s.UDPRTPAddress = "127.0.0.1:8000" - s.UDPRTCPAddress = "127.0.0.1:8001" - - err = s.Start("localhost:8554") + err = s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() @@ -1196,6 +1204,7 @@ func TestServerReadWithoutTeardown(t *testing.T) { }, ReadTimeout: 1 * time.Second, closeSessionAfterNoRequestsFor: 1 * time.Second, + RTSPAddress: "localhost:8554", } if transport == "udp" { @@ -1203,7 +1212,7 @@ func TestServerReadWithoutTeardown(t *testing.T) { s.UDPRTCPAddress = "127.0.0.1:8001" } - err = s.Start("localhost:8554") + err = s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() @@ -1288,9 +1297,10 @@ func TestServerReadUDPChangeConn(t *testing.T) { }, UDPRTPAddress: "127.0.0.1:8000", UDPRTCPAddress: "127.0.0.1:8001", + RTSPAddress: "localhost:8554", } - err = s.Start("localhost:8554") + err = s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() @@ -1380,9 +1390,10 @@ func TestServerReadErrorUDPSamePorts(t *testing.T) { }, UDPRTPAddress: "127.0.0.1:8000", UDPRTCPAddress: "127.0.0.1:8001", + RTSPAddress: "localhost:8554", } - err = s.Start("localhost:8554") + err = s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() @@ -1486,9 +1497,10 @@ func TestServerReadNonSetuppedPath(t *testing.T) { }, nil }, }, + RTSPAddress: "localhost:8554", } - err = s.Start("localhost:8554") + err = s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() @@ -1651,9 +1663,10 @@ func TestServerReadAdditionalInfos(t *testing.T) { }, nil }, }, + RTSPAddress: "localhost:8554", } - err = s.Start("localhost:8554") + err = s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() diff --git a/server_test.go b/server_test.go index 36e078c4..62abca10 100644 --- a/server_test.go +++ b/server_test.go @@ -416,6 +416,7 @@ func TestServerHighLevelPublishRead(t *testing.T) { } }, }, + RTSPAddress: "localhost:8554", } var proto string @@ -433,7 +434,7 @@ func TestServerHighLevelPublishRead(t *testing.T) { s.MulticastRTCPPort = 8003 } - err := s.Start("localhost:8554") + err := s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() @@ -531,10 +532,11 @@ func TestServerHighLevelPublishRead(t *testing.T) { func TestServerClose(t *testing.T) { s := &Server{ - Handler: &testServerHandler{}, + Handler: &testServerHandler{}, + RTSPAddress: "localhost:8554", } - err := s.Start("localhost:8554") + err := s.Start() require.NoError(t, err) defer s.Wait() s.Close() @@ -546,8 +548,9 @@ func TestServerErrorInvalidUDPPorts(t *testing.T) { s := &Server{ UDPRTPAddress: "127.0.0.1:8006", UDPRTCPAddress: "127.0.0.1:8009", + RTSPAddress: "localhost:8554", } - err := s.Start("localhost:8554") + err := s.Start() require.Error(t, err) }) @@ -555,8 +558,9 @@ func TestServerErrorInvalidUDPPorts(t *testing.T) { s := &Server{ UDPRTPAddress: "127.0.0.1:8003", UDPRTCPAddress: "127.0.0.1:8004", + RTSPAddress: "localhost:8554", } - err := s.Start("localhost:8554") + err := s.Start() require.Error(t, err) }) } @@ -574,9 +578,10 @@ func TestServerConnClose(t *testing.T) { close(connClosed) }, }, + RTSPAddress: "localhost:8554", } - err := s.Start("localhost:8554") + err := s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() @@ -589,8 +594,10 @@ func TestServerConnClose(t *testing.T) { } func TestServerCSeq(t *testing.T) { - s := &Server{} - err := s.Start("localhost:8554") + s := &Server{ + RTSPAddress: "localhost:8554", + } + err := s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() @@ -616,15 +623,16 @@ func TestServerCSeq(t *testing.T) { func TestServerErrorCSeqMissing(t *testing.T) { connClosed := make(chan struct{}) - h := &testServerHandler{ - onConnClose: func(ctx *ServerHandlerOnConnCloseCtx) { - require.EqualError(t, ctx.Error, "CSeq is missing") - close(connClosed) + s := &Server{ + Handler: &testServerHandler{ + onConnClose: func(ctx *ServerHandlerOnConnCloseCtx) { + require.EqualError(t, ctx.Error, "CSeq is missing") + close(connClosed) + }, }, + RTSPAddress: "localhost:8554", } - - s := &Server{Handler: h} - err := s.Start("localhost:8554") + err := s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() @@ -648,15 +656,16 @@ func TestServerErrorCSeqMissing(t *testing.T) { func TestServerErrorInvalidMethod(t *testing.T) { connClosed := make(chan struct{}) - h := &testServerHandler{ - onConnClose: func(ctx *ServerHandlerOnConnCloseCtx) { - require.EqualError(t, ctx.Error, "unhandled request (INVALID rtsp://localhost:8554/)") - close(connClosed) + s := &Server{ + Handler: &testServerHandler{ + onConnClose: func(ctx *ServerHandlerOnConnCloseCtx) { + require.EqualError(t, ctx.Error, "unhandled request (INVALID rtsp://localhost:8554/)") + close(connClosed) + }, }, + RTSPAddress: "localhost:8554", } - - s := &Server{Handler: h} - err := s.Start("localhost:8554") + err := s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() @@ -703,9 +712,10 @@ func TestServerErrorTCPTwoConnOneSession(t *testing.T) { }, nil }, }, + RTSPAddress: "localhost:8554", } - err = s.Start("localhost:8554") + err = s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() @@ -801,9 +811,10 @@ func TestServerErrorTCPOneConnTwoSessions(t *testing.T) { }, nil }, }, + RTSPAddress: "localhost:8554", } - err = s.Start("localhost:8554") + err = s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() @@ -887,9 +898,10 @@ func TestServerGetSetParameter(t *testing.T) { }, nil }, }, + RTSPAddress: "localhost:8554", } - err := s.Start("localhost:8554") + err := s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() @@ -959,9 +971,10 @@ func TestServerErrorInvalidSession(t *testing.T) { }, nil }, }, + RTSPAddress: "localhost:8554", } - err := s.Start("localhost:8554") + err := s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() @@ -1002,9 +1015,10 @@ func TestServerSessionClose(t *testing.T) { }, nil, nil }, }, + RTSPAddress: "localhost:8554", } - err := s.Start("localhost:8554") + err := s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() @@ -1057,9 +1071,10 @@ func TestServerSessionAutoClose(t *testing.T) { }, stream, nil }, }, + RTSPAddress: "localhost:8554", } - err = s.Start("localhost:8554") + err = s.Start() require.NoError(t, err) defer s.Wait() defer s.Close() @@ -1135,9 +1150,10 @@ func TestServerErrorInvalidPath(t *testing.T) { }, nil }, }, + RTSPAddress: "localhost:8554", } - err = s.Start("localhost:8554") + err = s.Start() require.NoError(t, err) defer s.Wait() defer s.Close()