diff --git a/machinery/src/capture/gortsplib.go b/machinery/src/capture/gortsplib.go index ee5548d..c078474 100644 --- a/machinery/src/capture/gortsplib.go +++ b/machinery/src/capture/gortsplib.go @@ -13,6 +13,7 @@ import ( "errors" "fmt" "image" + "math" "net/url" "os" "reflect" @@ -779,6 +780,23 @@ func ptsToDuration(pts int64, clockRate int) time.Duration { time.Duration(pts%rate)*time.Second/time.Duration(rate) } +func preRecordingGOPCount(preRecording int64, gopDuration float64) (int, bool) { + if preRecording <= 0 || + gopDuration < 1 || + math.IsNaN(gopDuration) || + math.IsInf(gopDuration, 0) || + gopDuration >= float64(math.MaxInt64) { + return 0, false + } + + count := preRecording / int64(gopDuration) + maxInt := int64(^uint(0) >> 1) + if count >= maxInt { + return 0, false + } + return int(count) + 1, true +} + // Start the RTSP client, and start reading packets. func (g *Golibrtsp) Start(ctx context.Context, streamType string, queue *packets.Queue, configuration *models.Configuration, communication *models.Communication) (err error) { log.Debug("capture.golibrtsp.Start(): started") @@ -1061,8 +1079,8 @@ func (g *Golibrtsp) Start(ctx context.Context, streamType string, queue *packets }).Debug("RTSP keyframe interval observed") preRecording := configuration.Config.Capture.PreRecording - if preRecording > 0 && int(gopDuration) > 0 { - queue.SetMaxGopCount(int(preRecording)/int(gopDuration) + 1) + if maxGOPCount, ok := preRecordingGOPCount(preRecording, gopDuration); ok { + queue.SetMaxGopCount(maxGOPCount) } } @@ -1279,8 +1297,8 @@ func (g *Golibrtsp) Start(ctx context.Context, streamType string, queue *packets }).Debug("RTSP keyframe interval observed") preRecording := configuration.Config.Capture.PreRecording - if preRecording > 0 && int(gopDuration) > 0 { - queue.SetMaxGopCount(int(preRecording)/int(gopDuration) + 1) + if maxGOPCount, ok := preRecordingGOPCount(preRecording, gopDuration); ok { + queue.SetMaxGopCount(maxGOPCount) } } diff --git a/machinery/src/capture/gortsplib_test.go b/machinery/src/capture/gortsplib_test.go index cb6742e..09bdc70 100644 --- a/machinery/src/capture/gortsplib_test.go +++ b/machinery/src/capture/gortsplib_test.go @@ -5,6 +5,7 @@ import ( "context" "encoding/pem" "errors" + "math" "net/http/httptest" "os" "path/filepath" @@ -12,6 +13,37 @@ import ( "testing" ) +func TestPreRecordingGOPCount(t *testing.T) { + maxInt := int64(^uint(0) >> 1) + tests := []struct { + name string + preRecording int64 + gopDuration float64 + want int + wantOK bool + }{ + {name: "normal duration", preRecording: 10, gopDuration: 2.9, want: 6, wantOK: true}, + {name: "duration longer than buffer", preRecording: 1, gopDuration: 2, want: 1, wantOK: true}, + {name: "largest representable result", preRecording: maxInt - 1, gopDuration: 1, want: int(maxInt), wantOK: true}, + {name: "result exceeds int", preRecording: maxInt, gopDuration: 1, wantOK: false}, + {name: "non-positive pre-recording", preRecording: 0, gopDuration: 1, wantOK: false}, + {name: "sub-second GOP", preRecording: 10, gopDuration: 0.9, wantOK: false}, + {name: "NaN GOP", preRecording: 10, gopDuration: math.NaN(), wantOK: false}, + {name: "infinite GOP", preRecording: 10, gopDuration: math.Inf(1), wantOK: false}, + {name: "GOP exceeds int64", preRecording: 10, gopDuration: float64(math.MaxInt64), wantOK: false}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, ok := preRecordingGOPCount(tt.preRecording, tt.gopDuration) + if ok != tt.wantOK || got != tt.want { + t.Fatalf("preRecordingGOPCount(%d, %v) = (%d, %t), want (%d, %t)", + tt.preRecording, tt.gopDuration, got, ok, tt.want, tt.wantOK) + } + }) + } +} + func TestGolibrtspCloseBeforeClientStart(t *testing.T) { client := &Golibrtsp{} diff --git a/machinery/src/components/kerberos.go b/machinery/src/components/kerberos.go index 6a10c1c..c17c117 100644 --- a/machinery/src/components/kerberos.go +++ b/machinery/src/components/kerberos.go @@ -397,12 +397,13 @@ func RunAgent(parent context.Context, configDirectory string, configuration *mod run.SetMainQueue(queue) // Set the maximum GOP count, this is used to determine the pre-recording time. + const initialMaxGOPCount = 1 + queue.SetMaxGopCount(initialMaxGOPCount) // Adjusted after the GOP duration is observed. log.WithFields(runFields).WithFields(log.Fields{ "event": "packet_queue_configured", - "max_gop_count": int(config.Capture.PreRecording) + 1, + "max_gop_count": initialMaxGOPCount, "stream": "main", }).Debug("Packet queue configured") - queue.SetMaxGopCount(1) // We will adjust this later on, when we have the GOP size. queue.WriteHeader(videoStreams) if err := run.Activate(); err != nil { log.WithError(err).WithFields(runFields).WithField("event", "run_activation_failed").