2024-04-06 18:01:23 +00:00
|
|
|
package playback
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2024-04-08 21:56:27 +00:00
|
|
|
"github.com/bluenviron/mediacommon/pkg/formats/fmp4"
|
2024-04-06 18:01:23 +00:00
|
|
|
"github.com/bluenviron/mediamtx/internal/conf"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
type listEntryDuration time.Duration
|
|
|
|
|
|
|
|
func (d listEntryDuration) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(time.Duration(d).Seconds())
|
|
|
|
}
|
|
|
|
|
|
|
|
type listEntry struct {
|
|
|
|
Start time.Time `json:"start"`
|
|
|
|
Duration listEntryDuration `json:"duration"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func computeDurationAndConcatenate(recordFormat conf.RecordFormat, segments []*Segment) ([]listEntry, error) {
|
|
|
|
if recordFormat == conf.RecordFormatFMP4 {
|
|
|
|
out := []listEntry{}
|
2024-04-08 21:56:27 +00:00
|
|
|
var prevInit *fmp4.Init
|
2024-04-06 18:01:23 +00:00
|
|
|
|
|
|
|
for _, seg := range segments {
|
|
|
|
err := func() error {
|
|
|
|
f, err := os.Open(seg.Fpath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2024-04-07 12:23:02 +00:00
|
|
|
init, err := segmentFMP4ReadInit(f)
|
2024-04-06 18:01:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = f.Seek(0, io.SeekStart)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-08 21:56:27 +00:00
|
|
|
maxDuration, err := segmentFMP4ReadMaxDuration(f, init)
|
2024-04-06 18:01:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-07 12:23:02 +00:00
|
|
|
if len(out) != 0 && segmentFMP4CanBeConcatenated(
|
2024-04-06 18:01:23 +00:00
|
|
|
prevInit,
|
|
|
|
out[len(out)-1].Start.Add(time.Duration(out[len(out)-1].Duration)),
|
|
|
|
init,
|
|
|
|
seg.Start) {
|
|
|
|
prevStart := out[len(out)-1].Start
|
2024-04-07 10:48:47 +00:00
|
|
|
curEnd := seg.Start.Add(maxDuration)
|
2024-04-06 18:01:23 +00:00
|
|
|
out[len(out)-1].Duration = listEntryDuration(curEnd.Sub(prevStart))
|
|
|
|
} else {
|
|
|
|
out = append(out, listEntry{
|
|
|
|
Start: seg.Start,
|
2024-04-07 10:48:47 +00:00
|
|
|
Duration: listEntryDuration(maxDuration),
|
2024-04-06 18:01:23 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
prevInit = init
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("MPEG-TS format is not supported yet")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Server) onList(ctx *gin.Context) {
|
|
|
|
pathName := ctx.Query("path")
|
|
|
|
|
|
|
|
if !p.doAuth(ctx, pathName) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pathConf, err := p.safeFindPathConf(pathName)
|
|
|
|
if err != nil {
|
|
|
|
p.writeError(ctx, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
segments, err := FindSegments(pathConf, pathName)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, errNoSegmentsFound) {
|
|
|
|
p.writeError(ctx, http.StatusNotFound, err)
|
|
|
|
} else {
|
|
|
|
p.writeError(ctx, http.StatusBadRequest, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := computeDurationAndConcatenate(pathConf.RecordFormat, segments)
|
|
|
|
if err != nil {
|
|
|
|
p.writeError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, out)
|
|
|
|
}
|