golanggohlsrtmpwebrtcmedia-serverobs-studiortcprtmp-proxyrtmp-serverrtprtsprtsp-proxyrtsp-relayrtsp-serversrtstreamingwebrtc-proxy
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.
92 lines
1.6 KiB
92 lines
1.6 KiB
package serverplain |
|
|
|
import ( |
|
"strconv" |
|
"time" |
|
|
|
"github.com/aler9/gortsplib" |
|
|
|
"github.com/aler9/rtsp-simple-server/internal/logger" |
|
) |
|
|
|
// Parent is implemented by program. |
|
type Parent interface { |
|
Log(logger.Level, string, ...interface{}) |
|
} |
|
|
|
// Server is a TCP/RTSP listener. |
|
type Server struct { |
|
parent Parent |
|
|
|
srv *gortsplib.Server |
|
|
|
// out |
|
accept chan *gortsplib.ServerConn |
|
done chan struct{} |
|
} |
|
|
|
// New allocates a Server. |
|
func New(port int, |
|
readTimeout time.Duration, |
|
writeTimeout time.Duration, |
|
readBufferCount uint64, |
|
udpRTPListener *gortsplib.ServerUDPListener, |
|
udpRTCPListener *gortsplib.ServerUDPListener, |
|
parent Parent) (*Server, error) { |
|
|
|
conf := gortsplib.ServerConf{ |
|
ReadTimeout: readTimeout, |
|
WriteTimeout: writeTimeout, |
|
ReadBufferCount: readBufferCount, |
|
UDPRTPListener: udpRTPListener, |
|
UDPRTCPListener: udpRTCPListener, |
|
} |
|
|
|
srv, err := conf.Serve(":" + strconv.FormatInt(int64(port), 10)) |
|
if err != nil { |
|
return nil, err |
|
} |
|
|
|
s := &Server{ |
|
parent: parent, |
|
srv: srv, |
|
accept: make(chan *gortsplib.ServerConn), |
|
done: make(chan struct{}), |
|
} |
|
|
|
parent.Log(logger.Info, "[TCP/RTSP listener] opened on :%d", port) |
|
|
|
go s.run() |
|
return s, nil |
|
} |
|
|
|
// Close closes a Server. |
|
func (s *Server) Close() { |
|
go func() { |
|
for co := range s.accept { |
|
co.Close() |
|
} |
|
}() |
|
s.srv.Close() |
|
<-s.done |
|
} |
|
|
|
func (s *Server) run() { |
|
defer close(s.done) |
|
|
|
for { |
|
conn, err := s.srv.Accept() |
|
if err != nil { |
|
break |
|
} |
|
|
|
s.accept <- conn |
|
} |
|
|
|
close(s.accept) |
|
} |
|
|
|
// Accept returns a channel to accept incoming connections. |
|
func (s *Server) Accept() chan *gortsplib.ServerConn { |
|
return s.accept |
|
}
|
|
|