Browse Source

playback: accept durations written in seconds (#2979)

pull/2980/head
Alessandro Ros 1 year ago committed by GitHub
parent
commit
ff70f9022e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 4
      README.md
  2. 13
      internal/playback/server.go
  3. 2
      internal/playback/server_test.go

4
README.md

@ -1231,13 +1231,13 @@ Where:
* [mypath] is the path name * [mypath] is the path name
* [start_date] is the start date in RFC3339 format * [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 * [format] must be fmp4
All parameters must be [url-encoded](https://www.urlencoder.org/). For instance: 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: The resulting stream is natively compatible with any browser, therefore its URL can be directly inserted into a \<video> tag:

13
internal/playback/server.go

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

2
internal/playback/server_test.go

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

Loading…
Cancel
Save