playback: accept durations written in seconds (#2979)

This commit is contained in:
Alessandro Ros 2024-02-03 16:05:39 +01:00 committed by GitHub
parent e6bf095a05
commit ff70f9022e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 4 deletions

View File

@ -1231,13 +1231,13 @@ Where:
* [mypath] is the path name
* [start_date] is the start date in RFC3339 format
* [duration] is the maximum duration of the recording in Golang format (example: 20s, 20h)
* [duration] is the maximum duration of the recording in seconds
* [format] must be fmp4
All parameters must be [url-encoded](https://www.urlencoder.org/). For instance:
```
http://localhost:9996/get?path=stream2&start=2024-01-14T16%3A33%3A17%2B00%3A00&duration=200s&format=fmp4
http://localhost:9996/get?path=stream2&start=2024-01-14T16%3A33%3A17%2B00%3A00&duration=200.5&format=fmp4
```
The resulting stream is natively compatible with any browser, therefore its URL can be directly inserted into a \<video> tag:

View File

@ -6,6 +6,7 @@ import (
"fmt"
"net"
"net/http"
"strconv"
"sync"
"time"
@ -22,6 +23,16 @@ const (
var errNoSegmentsFound = errors.New("no recording segments found for the given timestamp")
func parseDuration(raw string) (time.Duration, error) {
// seconds
if secs, err := strconv.ParseFloat(raw, 64); err == nil {
return time.Duration(secs * float64(time.Second)), nil
}
// deprecated, golang format
return time.ParseDuration(raw)
}
type listEntry struct {
Start time.Time `json:"start"`
Duration float64 `json:"duration"`
@ -172,7 +183,7 @@ func (p *Server) onGet(ctx *gin.Context) {
return
}
duration, err := time.ParseDuration(ctx.Query("duration"))
duration, err := parseDuration(ctx.Query("duration"))
if err != nil {
p.writeError(ctx, http.StatusBadRequest, fmt.Errorf("invalid duration: %w", err))
return

View File

@ -164,7 +164,7 @@ func TestServerGet(t *testing.T) {
v := url.Values{}
v.Set("path", "mypath")
v.Set("start", time.Date(2008, 11, 0o7, 11, 23, 1, 0, time.Local).Format(time.RFC3339))
v.Set("duration", "2s")
v.Set("duration", "2")
v.Set("format", "fmp4")
u := &url.URL{