2021-01-31 15:58:57 +00:00
|
|
|
package serverrtmp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"strconv"
|
2021-03-19 12:13:38 +00:00
|
|
|
"sync/atomic"
|
2021-01-31 15:58:57 +00:00
|
|
|
|
|
|
|
"github.com/aler9/rtsp-simple-server/internal/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Parent is implemented by program.
|
|
|
|
type Parent interface {
|
|
|
|
Log(logger.Level, string, ...interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Server is a RTMP listener.
|
|
|
|
type Server struct {
|
2021-03-19 12:13:38 +00:00
|
|
|
parent Parent
|
|
|
|
|
|
|
|
l net.Listener
|
|
|
|
closed uint32
|
2021-01-31 20:42:27 +00:00
|
|
|
|
2021-04-03 10:08:07 +00:00
|
|
|
// out
|
2021-04-08 16:48:39 +00:00
|
|
|
accept chan net.Conn
|
2021-04-03 10:08:07 +00:00
|
|
|
done chan struct{}
|
2021-01-31 15:58:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New allocates a Server.
|
|
|
|
func New(
|
|
|
|
listenIP string,
|
|
|
|
port int,
|
|
|
|
parent Parent) (*Server, error) {
|
|
|
|
|
|
|
|
address := listenIP + ":" + strconv.FormatInt(int64(port), 10)
|
|
|
|
l, err := net.Listen("tcp", address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &Server{
|
2021-03-19 12:13:38 +00:00
|
|
|
parent: parent,
|
2021-01-31 20:42:27 +00:00
|
|
|
l: l,
|
2021-04-08 16:48:39 +00:00
|
|
|
accept: make(chan net.Conn),
|
2021-04-03 10:08:07 +00:00
|
|
|
done: make(chan struct{}),
|
2021-01-31 15:58:57 +00:00
|
|
|
}
|
|
|
|
|
2021-03-19 12:13:38 +00:00
|
|
|
s.log(logger.Info, "opened on %s", address)
|
2021-01-31 15:58:57 +00:00
|
|
|
|
|
|
|
go s.run()
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2021-03-19 12:13:38 +00:00
|
|
|
func (s *Server) log(level logger.Level, format string, args ...interface{}) {
|
|
|
|
s.parent.Log(level, "[RTMP listener] "+format, append([]interface{}{}, args...)...)
|
|
|
|
}
|
|
|
|
|
2021-01-31 15:58:57 +00:00
|
|
|
// Close closes a Server.
|
|
|
|
func (s *Server) Close() {
|
2021-01-31 20:42:27 +00:00
|
|
|
go func() {
|
|
|
|
for co := range s.accept {
|
2021-04-08 16:48:39 +00:00
|
|
|
co.Close()
|
2021-01-31 20:42:27 +00:00
|
|
|
}
|
|
|
|
}()
|
2021-03-19 12:13:38 +00:00
|
|
|
atomic.StoreUint32(&s.closed, 1)
|
2021-01-31 15:58:57 +00:00
|
|
|
s.l.Close()
|
2021-04-03 10:08:07 +00:00
|
|
|
<-s.done
|
2021-01-31 15:58:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) run() {
|
2021-04-03 10:08:07 +00:00
|
|
|
defer close(s.done)
|
2021-01-31 15:58:57 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
nconn, err := s.l.Accept()
|
|
|
|
if err != nil {
|
2021-03-19 12:13:38 +00:00
|
|
|
if atomic.LoadUint32(&s.closed) == 1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
s.log(logger.Warn, "ERR: %s", err)
|
|
|
|
continue
|
2021-01-31 15:58:57 +00:00
|
|
|
}
|
|
|
|
|
2021-04-08 16:48:39 +00:00
|
|
|
s.accept <- nconn
|
2021-01-31 15:58:57 +00:00
|
|
|
}
|
|
|
|
|
2021-04-03 10:08:07 +00:00
|
|
|
close(s.accept)
|
2021-01-31 20:42:27 +00:00
|
|
|
}
|
2021-01-31 15:58:57 +00:00
|
|
|
|
2021-01-31 20:42:27 +00:00
|
|
|
// Accept returns a channel to accept incoming connections.
|
2021-04-08 16:48:39 +00:00
|
|
|
func (s *Server) Accept() chan net.Conn {
|
2021-01-31 20:42:27 +00:00
|
|
|
return s.accept
|
2021-01-31 15:58:57 +00:00
|
|
|
}
|