From d84ceeb67ec0d66bc9223bff69f0a56b0acb47ce Mon Sep 17 00:00:00 2001 From: Jae-Sung Lee Date: Tue, 11 Oct 2022 04:06:22 +0000 Subject: [PATCH] Add RTSPS listen --- conf/lalserver.conf.json | 4 ++++ pkg/base/url.go | 5 ++++- pkg/logic/config.go | 4 ++++ pkg/logic/group__.go | 5 +++-- pkg/logic/server_manager__.go | 23 +++++++++++++++++++++++ pkg/rtsp/server.go | 14 ++++++++++++++ 6 files changed, 52 insertions(+), 3 deletions(-) diff --git a/conf/lalserver.conf.json b/conf/lalserver.conf.json index 7c67eb5..b2eaaa1 100644 --- a/conf/lalserver.conf.json +++ b/conf/lalserver.conf.json @@ -45,6 +45,10 @@ "rtsp": { "enable": true, "addr": ":5544", + "rtsps_enable": true, + "rtsps_addr": ":5322", + "rtsps_cert_file": "./conf/cert.pem", + "rtsps_key_file": "./conf/key.pem", "out_wait_key_frame_flag": true, "auth_enable": false, "auth_method": 1, diff --git a/pkg/base/url.go b/pkg/base/url.go index a463e0d..9d0f63d 100644 --- a/pkg/base/url.go +++ b/pkg/base/url.go @@ -27,6 +27,7 @@ const ( DefaultHttpsPort = 443 DefaultRtspPort = 554 DefaultRtmpsPort = 443 + DefaultRtspsPort = 322 ) type UrlPathContext struct { @@ -112,6 +113,8 @@ func ParseUrl(rawUrl string, defaultPort int) (ctx UrlContext, err error) { defaultPort = DefaultRtspPort case "rtmps": defaultPort = DefaultRtmpsPort + case "rtsps": + defaultPort = DefaultRtspsPort } } @@ -203,7 +206,7 @@ func ParseRtspUrl(rawUrl string) (ctx UrlContext, err error) { return } // 注意,存在一种情况,使用rtsp pull session,直接拉取没有url path的流,所以不检查ctx.Path - if ctx.Scheme != "rtsp" || ctx.Host == "" { + if (ctx.Scheme != "rtsp" && ctx.Scheme != "rtsps") || ctx.Host == "" { return ctx, fmt.Errorf("%w. url=%s", ErrInvalidUrl, rawUrl) } diff --git a/pkg/logic/config.go b/pkg/logic/config.go index b9152e5..f08403b 100644 --- a/pkg/logic/config.go +++ b/pkg/logic/config.go @@ -89,6 +89,10 @@ type HlsConfig struct { type RtspConfig struct { Enable bool `json:"enable"` Addr string `json:"addr"` + RtspsEnable bool `json:"rtsps_enable"` + RtspsAddr string `json:"rtsps_addr"` + RtspsCertFile string `json:"rtsps_cert_file"` + RtspsKeyFile string `json:"rtsps_key_file"` OutWaitKeyFrameFlag bool `json:"out_wait_key_frame_flag"` rtsp.ServerAuthConfig } diff --git a/pkg/logic/group__.go b/pkg/logic/group__.go index 9918759..7eee083 100644 --- a/pkg/logic/group__.go +++ b/pkg/logic/group__.go @@ -10,10 +10,11 @@ package logic import ( "encoding/json" - "github.com/q191201771/lal/pkg/gb28181" "strings" "sync" + "github.com/q191201771/lal/pkg/gb28181" + "github.com/q191201771/lal/pkg/base" "github.com/q191201771/lal/pkg/hls" "github.com/q191201771/lal/pkg/httpflv" @@ -574,7 +575,7 @@ func (group *Group) inSessionUniqueKey() string { } func (group *Group) shouldStartRtspRemuxer() bool { - return group.config.RtspConfig.Enable + return group.config.RtspConfig.Enable || group.config.RtspConfig.RtspsEnable } func (group *Group) shouldStartMpegtsRemuxer() bool { diff --git a/pkg/logic/server_manager__.go b/pkg/logic/server_manager__.go index b8447f5..127f71b 100644 --- a/pkg/logic/server_manager__.go +++ b/pkg/logic/server_manager__.go @@ -48,6 +48,7 @@ type ServerManager struct { rtmpServer *rtmp.Server rtmpsServer *rtmp.Server rtspServer *rtsp.Server + rtspsServer *rtsp.Server httpApiServer *HttpApiServer pprofServer *http.Server exitChan chan struct{} @@ -130,6 +131,9 @@ Doc: %s if sm.config.RtspConfig.Enable { sm.rtspServer = rtsp.NewServer(sm.config.RtspConfig.Addr, sm, sm.config.RtspConfig.ServerAuthConfig) } + if sm.config.RtspConfig.RtspsEnable { + sm.rtspsServer = rtsp.NewServer(sm.config.RtspConfig.RtspsAddr, sm, sm.config.RtspConfig.ServerAuthConfig) + } if sm.config.HttpApiConfig.Enable { sm.httpApiServer = NewHttpApiServer(sm.config.HttpApiConfig.Addr, sm) } @@ -246,6 +250,17 @@ func (sm *ServerManager) RunLoop() error { }() } + if sm.rtspsServer != nil { + if err := sm.rtspsServer.ListenWithTLS(sm.config.RtspConfig.RtspsCertFile, sm.config.RtspConfig.RtspsKeyFile); err != nil { + return err + } + go func() { + if err := sm.rtspsServer.RunLoop(); err != nil { + Log.Error(err) + } + }() + } + if sm.httpApiServer != nil { if err := sm.httpApiServer.Listen(); err != nil { return err @@ -323,10 +338,18 @@ func (sm *ServerManager) Dispose() { sm.rtmpServer.Dispose() } + if sm.rtmpsServer != nil { + sm.rtmpsServer.Dispose() + } + if sm.rtspServer != nil { sm.rtspServer.Dispose() } + if sm.rtspsServer != nil { + sm.rtspsServer.Dispose() + } + if sm.httpServerManager != nil { sm.httpServerManager.Dispose() } diff --git a/pkg/rtsp/server.go b/pkg/rtsp/server.go index fa0a117..3fac360 100644 --- a/pkg/rtsp/server.go +++ b/pkg/rtsp/server.go @@ -9,6 +9,7 @@ package rtsp import ( + "crypto/tls" "net" ) @@ -81,6 +82,19 @@ func (s *Server) Listen() (err error) { return } +func (s *Server) ListenWithTLS(certFile, keyFile string) (err error) { + cert, err := tls.LoadX509KeyPair(certFile, keyFile) + if err != nil { + return + } + tlsConfig := &tls.Config{Certificates: []tls.Certificate{cert}} + if s.ln, err = tls.Listen("tcp", s.addr, tlsConfig); err != nil { + return + } + Log.Infof("start rtsps server listen. addr=%s", s.addr) + return +} + func (s *Server) RunLoop() error { for { conn, err := s.ln.Accept()