diff --git a/machinery/src/capture/gortsplib.go b/machinery/src/capture/gortsplib.go index 14005e6..59ffc39 100644 --- a/machinery/src/capture/gortsplib.go +++ b/machinery/src/capture/gortsplib.go @@ -660,7 +660,10 @@ func (g *Golibrtsp) Start(ctx context.Context, streamType string, queue *packets g.Streams[g.VideoH264Index].GopSize = gopSize log.Log.Info(fmt.Sprintf("capture.golibrtsp.Start(%s): Keyframe interval=%d packets, Avg=%.1f, GOP=%.1fs, GOPSize=%d", streamType, keyframeInterval, avgInterval, gopDuration, gopSize)) - queue.SetMaxGopCount(gopSize) + preRecording := configuration.Config.Capture.PreRecording + if preRecording > 0 && int(gopDuration) > 0 { + queue.SetMaxGopCount(int(preRecording) / int(gopDuration)) + } } pkt.Data = pkt.Data[4:] @@ -801,7 +804,8 @@ func (g *Golibrtsp) Start(ctx context.Context, streamType string, queue *packets g.Streams[g.VideoH265Index].GopSize = gopSize log.Log.Info(fmt.Sprintf("capture.golibrtsp.Start(%s): Keyframe interval=%d packets, Avg=%.1f, GOP=%.1fs, GOPSize=%d", streamType, keyframeInterval, avgInterval, gopDuration, gopSize)) - queue.SetMaxGopCount(gopSize) + preRecording := configuration.Config.Capture.PreRecording + queue.SetMaxGopCount(int(preRecording) / int(gopDuration)) } queue.WritePacket(pkt) diff --git a/machinery/src/capture/main.go b/machinery/src/capture/main.go index 5396d0a..f96e7d5 100644 --- a/machinery/src/capture/main.go +++ b/machinery/src/capture/main.go @@ -419,9 +419,21 @@ func HandleRecordStream(queue *packets.Queue, configDirectory string, configurat // Taking into account FPS = GOP size (Keyfram interval) if preRecording > 0 { - gopRatio := int64(2) //configuration.Config.Capture.IPCamera.GOPSize - displayTime = startRecording - preRecording/gopRatio - 500*gopRatio - fmt.Println(lastRecordingTime) + streams, _ := rtspClient.GetStreams() + videoIdx := -1 + audioIdx := -1 + for i, stream := range streams { + if (stream.Name == "H264" || stream.Name == "H265") && videoIdx < 0 { + videoIdx = i + } else if stream.Name == "PCM_MULAW" && audioIdx < 0 { + audioIdx = i + } + } + //videoStream := streams[videoIdx] + //gopSize := videoStream.GopSize + //fps := videoStream.FPS + displayTime = startRecording //*(int64(gopSize)/int64(fps)) // we substract the pre-recording time in milliseconds, divided by the gop size and fps. + fmt.Println(lastRecordingTime, startRecording, preRecording, displayTime) // Might be that recordings are coming short after each other. // Therefore we do some math with the current time and the last recording time. @@ -498,7 +510,7 @@ func HandleRecordStream(queue *packets.Queue, configDirectory string, configurat var cursorError error var pkt packets.Packet var nextPkt packets.Packet - recordingCursor := queue.DelayedGopCount(int(config.Capture.PreRecording + 1)) + recordingCursor := queue.Oldest() if cursorError == nil { pkt, cursorError = recordingCursor.ReadPacket() diff --git a/machinery/src/components/Kerberos.go b/machinery/src/components/Kerberos.go index 498d412..9f4f531 100644 --- a/machinery/src/components/Kerberos.go +++ b/machinery/src/components/Kerberos.go @@ -254,7 +254,7 @@ func RunAgent(configDirectory string, configuration *models.Configuration, commu if subStreamEnabled && rtspSubClient != nil { subQueue = packets.NewQueue() communication.SubQueue = subQueue - subQueue.SetMaxGopCount(3) // GOP time frame is set to prerecording (we'll add 2 gops to leave some room). + subQueue.SetMaxGopCount(3) // GOP time frame is set to 3 for motion detection and livestreaming. subQueue.WriteHeader(videoSubStreams) go rtspSubClient.Start(ctx, "sub", subQueue, configuration, communication) diff --git a/machinery/src/models/Config.go b/machinery/src/models/Config.go index 9473d98..f21e69c 100644 --- a/machinery/src/models/Config.go +++ b/machinery/src/models/Config.go @@ -65,6 +65,7 @@ type Capture struct { Continuous string `json:"continuous,omitempty"` PostRecording int64 `json:"postrecording"` PreRecording int64 `json:"prerecording"` + GopSize int `json:"gopsize,omitempty" bson:"gopsize,omitempty"` // GOP size in seconds, used for pre-recording MaxLengthRecording int64 `json:"maxlengthrecording"` TranscodingWebRTC string `json:"transcodingwebrtc"` TranscodingResolution int64 `json:"transcodingresolution"`