2021-07-24 13:55:42 +00:00
|
|
|
package core
|
2021-04-11 17:05:08 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-05-09 14:57:26 +00:00
|
|
|
"context"
|
2021-10-27 17:49:57 +00:00
|
|
|
"errors"
|
2021-04-11 17:05:08 +00:00
|
|
|
"fmt"
|
2021-04-27 17:04:05 +00:00
|
|
|
"io"
|
2021-04-11 17:05:08 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/aler9/gortsplib"
|
|
|
|
"github.com/aler9/gortsplib/pkg/ringbuffer"
|
|
|
|
"github.com/aler9/gortsplib/pkg/rtpaac"
|
|
|
|
"github.com/aler9/gortsplib/pkg/rtph264"
|
2021-05-14 21:19:46 +00:00
|
|
|
"github.com/pion/rtp"
|
2021-04-11 17:05:08 +00:00
|
|
|
|
2021-09-26 21:06:40 +00:00
|
|
|
"github.com/aler9/rtsp-simple-server/internal/conf"
|
2021-07-24 13:55:42 +00:00
|
|
|
"github.com/aler9/rtsp-simple-server/internal/hls"
|
2021-04-11 17:05:08 +00:00
|
|
|
"github.com/aler9/rtsp-simple-server/internal/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
closeCheckPeriod = 1 * time.Second
|
|
|
|
closeAfterInactivity = 60 * time.Second
|
|
|
|
)
|
|
|
|
|
|
|
|
const index = `<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<style>
|
2021-08-23 10:33:09 +00:00
|
|
|
html, body {
|
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
|
|
|
height: 100%;
|
2021-08-14 14:51:51 +00:00
|
|
|
}
|
2021-04-11 17:05:08 +00:00
|
|
|
#video {
|
2021-08-14 14:51:51 +00:00
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
2021-04-11 17:05:08 +00:00
|
|
|
background: black;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
|
2021-08-23 10:33:09 +00:00
|
|
|
<video id="video" muted controls autoplay></video>
|
2021-08-14 14:51:51 +00:00
|
|
|
|
2021-04-11 17:05:08 +00:00
|
|
|
<script src="https://cdn.jsdelivr.net/npm/hls.js@1.0.0"></script>
|
2021-08-14 14:51:51 +00:00
|
|
|
|
2021-04-11 17:05:08 +00:00
|
|
|
<script>
|
|
|
|
|
|
|
|
const create = () => {
|
|
|
|
const video = document.getElementById('video');
|
|
|
|
|
2021-08-14 14:30:41 +00:00
|
|
|
if (video.canPlayType('application/vnd.apple.mpegurl')) {
|
2021-08-23 10:26:06 +00:00
|
|
|
// since it's not possible to detect timeout errors in iOS,
|
|
|
|
// wait for the playlist to be available before starting the stream
|
|
|
|
fetch('stream.m3u8')
|
|
|
|
.then(() => {
|
|
|
|
video.src = 'index.m3u8';
|
|
|
|
video.play();
|
|
|
|
});
|
|
|
|
|
2021-08-14 14:30:41 +00:00
|
|
|
} else {
|
|
|
|
const hls = new Hls({
|
|
|
|
progressive: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
hls.on(Hls.Events.ERROR, (evt, data) => {
|
|
|
|
if (data.fatal) {
|
|
|
|
hls.destroy();
|
|
|
|
|
2021-08-23 10:26:06 +00:00
|
|
|
setTimeout(create, 2000);
|
2021-08-14 14:30:41 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-08-16 16:07:10 +00:00
|
|
|
hls.loadSource('index.m3u8');
|
2021-08-14 14:30:41 +00:00
|
|
|
hls.attachMedia(video);
|
|
|
|
|
|
|
|
video.play();
|
|
|
|
}
|
2021-08-23 10:26:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
window.addEventListener('DOMContentLoaded', create);
|
2021-04-11 17:05:08 +00:00
|
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`
|
|
|
|
|
2021-10-17 14:49:49 +00:00
|
|
|
type hlsMuxerResponse struct {
|
|
|
|
Status int
|
|
|
|
Header map[string]string
|
|
|
|
Body io.Reader
|
|
|
|
}
|
|
|
|
|
2021-08-18 13:49:12 +00:00
|
|
|
type hlsMuxerRequest struct {
|
2021-07-04 14:03:24 +00:00
|
|
|
Dir string
|
|
|
|
File string
|
|
|
|
Req *http.Request
|
2021-10-17 14:49:49 +00:00
|
|
|
Res chan hlsMuxerResponse
|
2021-04-27 17:04:05 +00:00
|
|
|
}
|
|
|
|
|
2021-08-18 13:49:12 +00:00
|
|
|
type hlsMuxerTrackIDPayloadPair struct {
|
2021-04-11 17:05:08 +00:00
|
|
|
trackID int
|
|
|
|
buf []byte
|
|
|
|
}
|
|
|
|
|
2021-08-18 13:49:12 +00:00
|
|
|
type hlsMuxerPathManager interface {
|
2021-10-27 19:01:00 +00:00
|
|
|
onReaderSetupPlay(req pathReaderSetupPlayReq) pathReaderSetupPlayRes
|
2021-08-11 10:45:53 +00:00
|
|
|
}
|
|
|
|
|
2021-08-18 13:49:12 +00:00
|
|
|
type hlsMuxerParent interface {
|
2021-10-27 19:01:00 +00:00
|
|
|
log(logger.Level, string, ...interface{})
|
|
|
|
onMuxerClose(*hlsMuxer)
|
2021-04-11 17:05:08 +00:00
|
|
|
}
|
|
|
|
|
2021-08-18 13:49:12 +00:00
|
|
|
type hlsMuxer struct {
|
2021-07-29 14:56:40 +00:00
|
|
|
hlsAlwaysRemux bool
|
2021-04-11 17:05:08 +00:00
|
|
|
hlsSegmentCount int
|
2021-09-26 21:06:40 +00:00
|
|
|
hlsSegmentDuration conf.StringDuration
|
2021-04-11 17:05:08 +00:00
|
|
|
readBufferCount int
|
|
|
|
wg *sync.WaitGroup
|
|
|
|
pathName string
|
2021-08-18 13:49:12 +00:00
|
|
|
pathManager hlsMuxerPathManager
|
|
|
|
parent hlsMuxerParent
|
2021-07-24 13:55:42 +00:00
|
|
|
|
2021-07-24 16:31:54 +00:00
|
|
|
ctx context.Context
|
|
|
|
ctxCancel func()
|
2021-07-31 16:25:47 +00:00
|
|
|
path *path
|
2021-07-24 16:31:54 +00:00
|
|
|
ringBuffer *ringbuffer.RingBuffer
|
|
|
|
lastRequestTime *int64
|
|
|
|
muxer *hls.Muxer
|
2021-08-18 13:49:12 +00:00
|
|
|
requests []hlsMuxerRequest
|
2021-04-11 17:05:08 +00:00
|
|
|
|
|
|
|
// in
|
2021-08-18 13:49:12 +00:00
|
|
|
request chan hlsMuxerRequest
|
2021-04-11 17:05:08 +00:00
|
|
|
}
|
|
|
|
|
2021-08-18 13:49:12 +00:00
|
|
|
func newHLSMuxer(
|
2021-07-24 13:55:42 +00:00
|
|
|
parentCtx context.Context,
|
2021-07-29 14:56:40 +00:00
|
|
|
hlsAlwaysRemux bool,
|
2021-04-11 17:05:08 +00:00
|
|
|
hlsSegmentCount int,
|
2021-09-26 21:06:40 +00:00
|
|
|
hlsSegmentDuration conf.StringDuration,
|
2021-04-11 17:05:08 +00:00
|
|
|
readBufferCount int,
|
|
|
|
wg *sync.WaitGroup,
|
|
|
|
pathName string,
|
2021-08-18 13:49:12 +00:00
|
|
|
pathManager hlsMuxerPathManager,
|
|
|
|
parent hlsMuxerParent) *hlsMuxer {
|
2021-07-24 13:55:42 +00:00
|
|
|
ctx, ctxCancel := context.WithCancel(parentCtx)
|
2021-05-10 19:32:59 +00:00
|
|
|
|
2021-08-18 13:49:12 +00:00
|
|
|
r := &hlsMuxer{
|
2021-07-29 14:56:40 +00:00
|
|
|
hlsAlwaysRemux: hlsAlwaysRemux,
|
2021-04-11 17:05:08 +00:00
|
|
|
hlsSegmentCount: hlsSegmentCount,
|
|
|
|
hlsSegmentDuration: hlsSegmentDuration,
|
|
|
|
readBufferCount: readBufferCount,
|
|
|
|
wg: wg,
|
|
|
|
pathName: pathName,
|
2021-07-30 12:49:09 +00:00
|
|
|
pathManager: pathManager,
|
2021-04-11 17:05:08 +00:00
|
|
|
parent: parent,
|
2021-05-10 19:32:59 +00:00
|
|
|
ctx: ctx,
|
|
|
|
ctxCancel: ctxCancel,
|
2021-07-24 16:31:54 +00:00
|
|
|
lastRequestTime: func() *int64 {
|
2021-07-03 10:19:03 +00:00
|
|
|
v := time.Now().Unix()
|
|
|
|
return &v
|
|
|
|
}(),
|
2021-08-18 13:49:12 +00:00
|
|
|
request: make(chan hlsMuxerRequest),
|
2021-04-11 17:05:08 +00:00
|
|
|
}
|
|
|
|
|
2021-07-29 14:56:40 +00:00
|
|
|
r.log(logger.Info, "created")
|
2021-04-11 17:05:08 +00:00
|
|
|
|
2021-07-29 14:56:40 +00:00
|
|
|
r.wg.Add(1)
|
|
|
|
go r.run()
|
2021-04-11 17:05:08 +00:00
|
|
|
|
2021-07-29 14:56:40 +00:00
|
|
|
return r
|
2021-04-11 17:05:08 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 19:01:00 +00:00
|
|
|
func (r *hlsMuxer) close() {
|
2021-07-29 14:56:40 +00:00
|
|
|
r.ctxCancel()
|
2021-04-27 11:43:15 +00:00
|
|
|
}
|
|
|
|
|
2021-08-18 13:49:12 +00:00
|
|
|
func (r *hlsMuxer) log(level logger.Level, format string, args ...interface{}) {
|
2021-10-27 19:01:00 +00:00
|
|
|
r.parent.log(level, "[muxer %s] "+format, append([]interface{}{r.pathName}, args...)...)
|
2021-04-11 17:05:08 +00:00
|
|
|
}
|
|
|
|
|
2021-07-31 18:46:06 +00:00
|
|
|
// PathName returns the path name.
|
2021-08-18 13:49:12 +00:00
|
|
|
func (r *hlsMuxer) PathName() string {
|
2021-07-29 14:56:40 +00:00
|
|
|
return r.pathName
|
2021-04-11 17:05:08 +00:00
|
|
|
}
|
|
|
|
|
2021-08-18 13:49:12 +00:00
|
|
|
func (r *hlsMuxer) run() {
|
2021-07-29 14:56:40 +00:00
|
|
|
defer r.wg.Done()
|
2021-04-11 17:05:08 +00:00
|
|
|
|
2021-08-18 13:49:12 +00:00
|
|
|
innerCtx, innerCtxCancel := context.WithCancel(context.Background())
|
|
|
|
innerReady := make(chan struct{})
|
|
|
|
innerErr := make(chan error)
|
2021-05-09 14:57:26 +00:00
|
|
|
go func() {
|
2021-08-18 13:49:12 +00:00
|
|
|
innerErr <- r.runInner(innerCtx, innerReady)
|
2021-05-09 14:57:26 +00:00
|
|
|
}()
|
|
|
|
|
2021-08-10 13:00:07 +00:00
|
|
|
isReady := false
|
2021-05-09 14:57:26 +00:00
|
|
|
|
2021-10-27 17:49:57 +00:00
|
|
|
err := func() error {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-r.ctx.Done():
|
|
|
|
innerCtxCancel()
|
|
|
|
<-innerErr
|
|
|
|
return errors.New("terminated")
|
|
|
|
|
|
|
|
case req := <-r.request:
|
|
|
|
if isReady {
|
|
|
|
req.Res <- r.handleRequest(req)
|
|
|
|
} else {
|
|
|
|
r.requests = append(r.requests, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-innerReady:
|
|
|
|
isReady = true
|
|
|
|
for _, req := range r.requests {
|
|
|
|
req.Res <- r.handleRequest(req)
|
|
|
|
}
|
|
|
|
r.requests = nil
|
|
|
|
|
|
|
|
case err := <-innerErr:
|
|
|
|
innerCtxCancel()
|
|
|
|
return err
|
2021-08-10 13:00:07 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-27 17:49:57 +00:00
|
|
|
}()
|
2021-05-09 14:57:26 +00:00
|
|
|
|
2021-07-29 14:56:40 +00:00
|
|
|
r.ctxCancel()
|
2021-05-09 14:57:26 +00:00
|
|
|
|
2021-08-14 09:15:59 +00:00
|
|
|
for _, req := range r.requests {
|
2021-10-17 14:49:49 +00:00
|
|
|
req.Res <- hlsMuxerResponse{Status: http.StatusNotFound}
|
2021-08-14 09:15:59 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 19:01:00 +00:00
|
|
|
r.parent.onMuxerClose(r)
|
2021-10-27 17:49:57 +00:00
|
|
|
|
|
|
|
r.log(logger.Info, "destroyed (%v)", err)
|
2021-05-09 14:57:26 +00:00
|
|
|
}
|
|
|
|
|
2021-08-18 13:49:12 +00:00
|
|
|
func (r *hlsMuxer) runInner(innerCtx context.Context, innerReady chan struct{}) error {
|
2021-10-27 19:01:00 +00:00
|
|
|
res := r.pathManager.onReaderSetupPlay(pathReaderSetupPlayReq{
|
2021-07-29 14:56:40 +00:00
|
|
|
Author: r,
|
|
|
|
PathName: r.pathName,
|
2021-05-09 14:57:26 +00:00
|
|
|
IP: nil,
|
|
|
|
ValidateCredentials: nil,
|
|
|
|
})
|
|
|
|
if res.Err != nil {
|
|
|
|
return res.Err
|
|
|
|
}
|
|
|
|
|
2021-07-29 14:56:40 +00:00
|
|
|
r.path = res.Path
|
2021-07-31 18:46:06 +00:00
|
|
|
|
|
|
|
defer func() {
|
2021-10-27 19:01:00 +00:00
|
|
|
r.path.onReaderRemove(pathReaderRemoveReq{Author: r})
|
2021-07-31 18:46:06 +00:00
|
|
|
}()
|
|
|
|
|
2021-04-11 17:05:08 +00:00
|
|
|
var videoTrack *gortsplib.Track
|
2021-06-26 11:46:31 +00:00
|
|
|
videoTrackID := -1
|
2021-04-11 17:05:08 +00:00
|
|
|
var h264Decoder *rtph264.Decoder
|
|
|
|
var audioTrack *gortsplib.Track
|
2021-06-26 11:46:31 +00:00
|
|
|
audioTrackID := -1
|
2021-04-11 17:05:08 +00:00
|
|
|
var aacDecoder *rtpaac.Decoder
|
|
|
|
|
2021-08-10 16:34:10 +00:00
|
|
|
for i, t := range res.Stream.tracks() {
|
2021-05-09 14:57:26 +00:00
|
|
|
if t.IsH264() {
|
|
|
|
if videoTrack != nil {
|
|
|
|
return fmt.Errorf("can't read track %d with HLS: too many tracks", i+1)
|
|
|
|
}
|
2021-06-26 11:46:31 +00:00
|
|
|
|
2021-05-09 14:57:26 +00:00
|
|
|
videoTrack = t
|
2021-06-26 11:46:31 +00:00
|
|
|
videoTrackID = i
|
2021-04-11 17:05:08 +00:00
|
|
|
|
2021-05-09 14:57:26 +00:00
|
|
|
h264Decoder = rtph264.NewDecoder()
|
2021-04-11 17:05:08 +00:00
|
|
|
|
2021-05-09 14:57:26 +00:00
|
|
|
} else if t.IsAAC() {
|
|
|
|
if audioTrack != nil {
|
|
|
|
return fmt.Errorf("can't read track %d with HLS: too many tracks", i+1)
|
2021-04-11 17:05:08 +00:00
|
|
|
}
|
2021-06-26 11:46:31 +00:00
|
|
|
|
2021-05-09 14:57:26 +00:00
|
|
|
audioTrack = t
|
2021-06-26 11:46:31 +00:00
|
|
|
audioTrackID = i
|
2021-04-11 17:05:08 +00:00
|
|
|
|
2021-08-25 17:51:59 +00:00
|
|
|
conf, err := t.ExtractConfigAAC()
|
2021-05-09 14:57:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-11 17:05:08 +00:00
|
|
|
|
2021-08-25 17:51:59 +00:00
|
|
|
aacDecoder = rtpaac.NewDecoder(conf.SampleRate)
|
2021-04-11 17:05:08 +00:00
|
|
|
}
|
2021-05-09 14:57:26 +00:00
|
|
|
}
|
2021-04-11 17:05:08 +00:00
|
|
|
|
2021-05-09 14:57:26 +00:00
|
|
|
if videoTrack == nil && audioTrack == nil {
|
2021-08-13 12:16:34 +00:00
|
|
|
return fmt.Errorf("the stream doesn't contain an H264 track or an AAC track")
|
2021-04-11 17:05:08 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 16:31:54 +00:00
|
|
|
var err error
|
2021-07-29 14:56:40 +00:00
|
|
|
r.muxer, err = hls.NewMuxer(
|
|
|
|
r.hlsSegmentCount,
|
2021-09-26 21:06:40 +00:00
|
|
|
time.Duration(r.hlsSegmentDuration),
|
2021-07-24 16:31:54 +00:00
|
|
|
videoTrack,
|
|
|
|
audioTrack,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-29 14:56:40 +00:00
|
|
|
defer r.muxer.Close()
|
2021-04-11 17:05:08 +00:00
|
|
|
|
2021-08-18 13:49:12 +00:00
|
|
|
innerReady <- struct{}{}
|
2021-04-11 17:05:08 +00:00
|
|
|
|
2021-07-29 14:56:40 +00:00
|
|
|
r.ringBuffer = ringbuffer.New(uint64(r.readBufferCount))
|
2021-04-11 17:05:08 +00:00
|
|
|
|
2021-10-27 19:01:00 +00:00
|
|
|
r.path.onReaderPlay(pathReaderPlayReq{Author: r})
|
2021-04-11 17:05:08 +00:00
|
|
|
|
|
|
|
writerDone := make(chan error)
|
|
|
|
go func() {
|
|
|
|
writerDone <- func() error {
|
|
|
|
for {
|
2021-07-29 14:56:40 +00:00
|
|
|
data, ok := r.ringBuffer.Pull()
|
2021-04-11 17:05:08 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("terminated")
|
|
|
|
}
|
2021-08-18 13:49:12 +00:00
|
|
|
pair := data.(hlsMuxerTrackIDPayloadPair)
|
2021-04-11 17:05:08 +00:00
|
|
|
|
2021-06-26 11:46:31 +00:00
|
|
|
if videoTrack != nil && pair.trackID == videoTrackID {
|
2021-05-14 21:19:46 +00:00
|
|
|
var pkt rtp.Packet
|
|
|
|
err := pkt.Unmarshal(pair.buf)
|
|
|
|
if err != nil {
|
2021-07-29 14:56:40 +00:00
|
|
|
r.log(logger.Warn, "unable to decode RTP packet: %v", err)
|
2021-05-14 21:19:46 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-09-28 13:44:59 +00:00
|
|
|
nalus, pts, err := h264Decoder.DecodeUntilMarker(&pkt)
|
2021-04-11 17:05:08 +00:00
|
|
|
if err != nil {
|
2021-09-28 13:44:59 +00:00
|
|
|
if err != rtph264.ErrMorePacketsNeeded &&
|
|
|
|
err != rtph264.ErrNonStartingPacketAndNoPrevious {
|
2021-07-29 14:56:40 +00:00
|
|
|
r.log(logger.Warn, "unable to decode video track: %v", err)
|
2021-04-11 17:05:08 +00:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-09-28 13:44:59 +00:00
|
|
|
err = r.muxer.WriteH264(pts, nalus)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-04-11 17:05:08 +00:00
|
|
|
}
|
|
|
|
|
2021-06-26 11:46:31 +00:00
|
|
|
} else if audioTrack != nil && pair.trackID == audioTrackID {
|
2021-07-24 16:31:54 +00:00
|
|
|
var pkt rtp.Packet
|
|
|
|
err := pkt.Unmarshal(pair.buf)
|
|
|
|
if err != nil {
|
2021-07-29 14:56:40 +00:00
|
|
|
r.log(logger.Warn, "unable to decode RTP packet: %v", err)
|
2021-07-24 16:31:54 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-09-28 13:44:59 +00:00
|
|
|
aus, pts, err := aacDecoder.Decode(&pkt)
|
2021-04-11 17:05:08 +00:00
|
|
|
if err != nil {
|
|
|
|
if err != rtpaac.ErrMorePacketsNeeded {
|
2021-07-29 14:56:40 +00:00
|
|
|
r.log(logger.Warn, "unable to decode audio track: %v", err)
|
2021-04-11 17:05:08 +00:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-07-29 14:56:40 +00:00
|
|
|
err = r.muxer.WriteAAC(pts, aus)
|
2021-06-19 11:51:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-04-11 17:05:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}()
|
|
|
|
|
|
|
|
closeCheckTicker := time.NewTicker(closeCheckPeriod)
|
|
|
|
defer closeCheckTicker.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-closeCheckTicker.C:
|
2021-07-29 14:56:40 +00:00
|
|
|
t := time.Unix(atomic.LoadInt64(r.lastRequestTime), 0)
|
|
|
|
if !r.hlsAlwaysRemux && time.Since(t) >= closeAfterInactivity {
|
|
|
|
r.ringBuffer.Close()
|
2021-04-11 17:05:08 +00:00
|
|
|
<-writerDone
|
2021-05-09 15:22:24 +00:00
|
|
|
return nil
|
2021-04-11 17:05:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case err := <-writerDone:
|
2021-05-09 14:57:26 +00:00
|
|
|
return err
|
2021-04-11 17:05:08 +00:00
|
|
|
|
2021-08-18 13:49:12 +00:00
|
|
|
case <-innerCtx.Done():
|
2021-07-29 14:56:40 +00:00
|
|
|
r.ringBuffer.Close()
|
2021-05-11 15:20:32 +00:00
|
|
|
<-writerDone
|
2021-05-09 15:22:24 +00:00
|
|
|
return nil
|
2021-04-11 17:05:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-17 14:49:49 +00:00
|
|
|
func (r *hlsMuxer) handleRequest(req hlsMuxerRequest) hlsMuxerResponse {
|
2021-08-10 13:00:07 +00:00
|
|
|
atomic.StoreInt64(r.lastRequestTime, time.Now().Unix())
|
2021-04-11 17:05:08 +00:00
|
|
|
|
2021-08-10 13:00:07 +00:00
|
|
|
conf := r.path.Conf()
|
2021-04-11 17:05:08 +00:00
|
|
|
|
2021-09-27 08:36:28 +00:00
|
|
|
if conf.ReadIPs != nil {
|
2021-08-10 13:00:07 +00:00
|
|
|
tmp, _, _ := net.SplitHostPort(req.Req.RemoteAddr)
|
|
|
|
ip := net.ParseIP(tmp)
|
2021-09-27 08:36:28 +00:00
|
|
|
if !ipEqualOrInRange(ip, conf.ReadIPs) {
|
2021-08-10 13:00:07 +00:00
|
|
|
r.log(logger.Info, "ERR: ip '%s' not allowed", ip)
|
2021-10-17 14:49:49 +00:00
|
|
|
return hlsMuxerResponse{Status: http.StatusUnauthorized}
|
2021-08-10 13:00:07 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-11 17:05:08 +00:00
|
|
|
|
2021-08-10 13:00:07 +00:00
|
|
|
if conf.ReadUser != "" {
|
|
|
|
user, pass, ok := req.Req.BasicAuth()
|
2021-09-27 13:45:51 +00:00
|
|
|
if !ok || user != string(conf.ReadUser) || pass != string(conf.ReadPass) {
|
2021-10-17 14:49:49 +00:00
|
|
|
return hlsMuxerResponse{
|
|
|
|
Status: http.StatusUnauthorized,
|
|
|
|
Header: map[string]string{
|
|
|
|
"WWW-Authenticate": `Basic realm="rtsp-simple-server"`,
|
|
|
|
},
|
|
|
|
}
|
2021-08-10 13:00:07 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-11 17:05:08 +00:00
|
|
|
|
2021-08-10 13:00:07 +00:00
|
|
|
switch {
|
2021-08-16 16:07:10 +00:00
|
|
|
case req.File == "index.m3u8":
|
2021-10-17 14:49:49 +00:00
|
|
|
return hlsMuxerResponse{
|
|
|
|
Status: http.StatusOK,
|
|
|
|
Header: map[string]string{
|
|
|
|
"Content-Type": `application/x-mpegURL`,
|
|
|
|
},
|
|
|
|
Body: r.muxer.PrimaryPlaylist(),
|
|
|
|
}
|
2021-08-16 16:07:10 +00:00
|
|
|
|
2021-08-10 13:00:07 +00:00
|
|
|
case req.File == "stream.m3u8":
|
2021-10-17 14:49:49 +00:00
|
|
|
return hlsMuxerResponse{
|
|
|
|
Status: http.StatusOK,
|
|
|
|
Header: map[string]string{
|
|
|
|
"Content-Type": `application/x-mpegURL`,
|
|
|
|
},
|
|
|
|
Body: r.muxer.StreamPlaylist(),
|
|
|
|
}
|
2021-05-09 14:57:26 +00:00
|
|
|
|
2021-08-10 13:00:07 +00:00
|
|
|
case strings.HasSuffix(req.File, ".ts"):
|
2021-08-23 10:26:06 +00:00
|
|
|
r := r.muxer.Segment(req.File)
|
2021-08-10 13:00:07 +00:00
|
|
|
if r == nil {
|
2021-10-17 14:49:49 +00:00
|
|
|
return hlsMuxerResponse{Status: http.StatusNotFound}
|
2021-08-10 13:00:07 +00:00
|
|
|
}
|
2021-04-11 17:05:08 +00:00
|
|
|
|
2021-10-17 14:49:49 +00:00
|
|
|
return hlsMuxerResponse{
|
|
|
|
Status: http.StatusOK,
|
|
|
|
Header: map[string]string{
|
|
|
|
"Content-Type": `video/MP2T`,
|
|
|
|
},
|
|
|
|
Body: r,
|
|
|
|
}
|
2021-04-11 17:05:08 +00:00
|
|
|
|
2021-08-10 13:00:07 +00:00
|
|
|
case req.File == "":
|
2021-10-17 14:49:49 +00:00
|
|
|
return hlsMuxerResponse{
|
|
|
|
Status: http.StatusOK,
|
|
|
|
Header: map[string]string{
|
|
|
|
"Content-Type": `text/html`,
|
|
|
|
},
|
|
|
|
Body: bytes.NewReader([]byte(index)),
|
|
|
|
}
|
2021-04-11 17:05:08 +00:00
|
|
|
|
2021-08-10 13:00:07 +00:00
|
|
|
default:
|
2021-10-17 14:49:49 +00:00
|
|
|
return hlsMuxerResponse{Status: http.StatusNotFound}
|
2021-04-11 17:05:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-27 19:01:00 +00:00
|
|
|
// onRequest is called by hlsserver.Server (forwarded from ServeHTTP).
|
|
|
|
func (r *hlsMuxer) onRequest(req hlsMuxerRequest) {
|
2021-05-10 19:32:59 +00:00
|
|
|
select {
|
2021-07-29 14:56:40 +00:00
|
|
|
case r.request <- req:
|
|
|
|
case <-r.ctx.Done():
|
2021-10-17 14:49:49 +00:00
|
|
|
req.Res <- hlsMuxerResponse{Status: http.StatusNotFound}
|
2021-05-10 19:32:59 +00:00
|
|
|
}
|
2021-04-11 17:05:08 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 19:01:00 +00:00
|
|
|
// onReaderAccepted implements reader.
|
|
|
|
func (r *hlsMuxer) onReaderAccepted() {
|
2021-08-18 13:49:12 +00:00
|
|
|
r.log(logger.Info, "is converting into HLS")
|
2021-07-29 14:56:40 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 19:01:00 +00:00
|
|
|
// onReaderFrame implements reader.
|
|
|
|
func (r *hlsMuxer) onReaderFrame(trackID int, streamType gortsplib.StreamType, payload []byte) {
|
2021-04-11 17:05:08 +00:00
|
|
|
if streamType == gortsplib.StreamTypeRTP {
|
2021-08-18 13:49:12 +00:00
|
|
|
r.ringBuffer.Push(hlsMuxerTrackIDPayloadPair{trackID, payload})
|
2021-04-11 17:05:08 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-04 16:13:49 +00:00
|
|
|
|
2021-10-27 19:01:00 +00:00
|
|
|
// onReaderAPIDescribe implements reader.
|
|
|
|
func (r *hlsMuxer) onReaderAPIDescribe() interface{} {
|
2021-07-04 16:13:49 +00:00
|
|
|
return struct {
|
|
|
|
Type string `json:"type"`
|
2021-08-18 13:49:12 +00:00
|
|
|
}{"hlsMuxer"}
|
2021-07-04 16:13:49 +00:00
|
|
|
}
|