mediamtx/internal/serverhls/server.go

213 lines
4.1 KiB
Go
Raw Normal View History

2021-04-11 17:05:08 +00:00
package serverhls
import (
"context"
"io"
"net"
"net/http"
"strings"
"sync"
"time"
2021-04-11 17:05:08 +00:00
2021-04-27 17:29:43 +00:00
"github.com/aler9/rtsp-simple-server/internal/converterhls"
2021-04-11 17:05:08 +00:00
"github.com/aler9/rtsp-simple-server/internal/logger"
"github.com/aler9/rtsp-simple-server/internal/pathman"
"github.com/aler9/rtsp-simple-server/internal/stats"
2021-04-11 17:05:08 +00:00
)
// Parent is implemented by program.
type Parent interface {
Log(logger.Level, string, ...interface{})
}
// Server is an HLS server.
type Server struct {
hlsSegmentCount int
hlsSegmentDuration time.Duration
readBufferCount int
stats *stats.Stats
pathMan *pathman.PathManager
parent Parent
2021-04-27 17:29:43 +00:00
ln net.Listener
wg sync.WaitGroup
converters map[string]*converterhls.Converter
// in
2021-04-27 17:29:43 +00:00
request chan converterhls.Request
clientClose chan *converterhls.Converter
terminate chan struct{}
2021-04-11 17:05:08 +00:00
// out
done chan struct{}
2021-04-11 17:05:08 +00:00
}
// New allocates a Server.
func New(
address string,
hlsSegmentCount int,
hlsSegmentDuration time.Duration,
readBufferCount int,
stats *stats.Stats,
pathMan *pathman.PathManager,
2021-04-11 17:05:08 +00:00
parent Parent,
) (*Server, error) {
2021-04-11 17:05:08 +00:00
ln, err := net.Listen("tcp", address)
if err != nil {
return nil, err
}
s := &Server{
hlsSegmentCount: hlsSegmentCount,
hlsSegmentDuration: hlsSegmentDuration,
readBufferCount: readBufferCount,
stats: stats,
pathMan: pathMan,
parent: parent,
ln: ln,
2021-04-27 17:29:43 +00:00
converters: make(map[string]*converterhls.Converter),
request: make(chan converterhls.Request),
clientClose: make(chan *converterhls.Converter),
terminate: make(chan struct{}),
done: make(chan struct{}),
2021-04-11 17:05:08 +00:00
}
s.Log(logger.Info, "listener opened on "+address)
2021-04-11 17:05:08 +00:00
go s.run()
2021-04-11 17:05:08 +00:00
return s, nil
}
// Log is the main logging function.
func (s *Server) Log(level logger.Level, format string, args ...interface{}) {
s.parent.Log(level, "[HLS] "+format, append([]interface{}{}, args...)...)
2021-04-11 17:05:08 +00:00
}
// Close closes all the server resources.
func (s *Server) Close() {
close(s.terminate)
<-s.done
}
func (s *Server) run() {
defer close(s.done)
hs := &http.Server{Handler: s}
go hs.Serve(s.ln)
outer:
for {
select {
case req := <-s.request:
2021-04-27 17:29:43 +00:00
c, ok := s.converters[req.Path]
if !ok {
2021-04-27 17:29:43 +00:00
c = converterhls.New(
s.hlsSegmentCount,
s.hlsSegmentDuration,
s.readBufferCount,
&s.wg,
s.stats,
req.Path,
s.pathMan,
s)
2021-04-27 17:29:43 +00:00
s.converters[req.Path] = c
}
c.OnRequest(req)
case c := <-s.clientClose:
2021-04-27 17:29:43 +00:00
if c2, ok := s.converters[c.PathName()]; !ok || c2 != c {
continue
}
2021-04-27 17:29:43 +00:00
s.doConverterClose(c)
case <-s.terminate:
break outer
}
}
2021-04-11 17:05:08 +00:00
go func() {
for {
select {
case req, ok := <-s.request:
if !ok {
return
}
req.Res <- nil
case _, ok := <-s.clientClose:
if !ok {
return
}
}
2021-04-11 17:05:08 +00:00
}
}()
2021-04-27 17:29:43 +00:00
for _, c := range s.converters {
s.doConverterClose(c)
}
hs.Shutdown(context.Background())
2021-04-11 17:05:08 +00:00
close(s.request)
close(s.clientClose)
2021-04-11 17:05:08 +00:00
}
// ServeHTTP implements http.Handler.
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
2021-04-27 17:29:43 +00:00
s.Log(logger.Info, "[client %s] %s %s", r.RemoteAddr, r.Method, r.URL.Path)
2021-04-11 17:05:08 +00:00
// remove leading prefix
path := r.URL.Path[1:]
if path == "" || path == "favicon.ico" {
w.WriteHeader(http.StatusNotFound)
return
}
parts := strings.SplitN(path, "/", 2)
if len(parts) < 2 {
w.Header().Add("Location", parts[0]+"/")
w.WriteHeader(http.StatusMovedPermanently)
return
}
cres := make(chan io.Reader)
2021-04-27 17:29:43 +00:00
s.request <- converterhls.Request{
2021-04-11 17:05:08 +00:00
Path: parts[0],
Subpath: parts[1],
Req: r,
W: w,
Res: cres,
}
res := <-cres
if res != nil {
buf := make([]byte, 4096)
for {
n, err := res.Read(buf)
if err != nil {
return
}
_, err = w.Write(buf[:n])
if err != nil {
return
}
w.(http.Flusher).Flush()
}
2021-04-11 17:05:08 +00:00
}
}
2021-04-27 17:29:43 +00:00
func (s *Server) doConverterClose(c *converterhls.Converter) {
delete(s.converters, c.PathName())
c.Close()
}
2021-04-27 17:29:43 +00:00
// OnConverterClose is called by converterhls.Converter.
func (s *Server) OnConverterClose(c *converterhls.Converter) {
s.clientClose <- c
2021-04-11 17:05:08 +00:00
}