Skip to content

Commit

Permalink
better way of doing prerecording + matching timestamp with prerecord …
Browse files Browse the repository at this point in the history
…time
  • Loading branch information
cedricve committed Feb 14, 2023
1 parent 9d70778 commit b4a8028
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
54 changes: 54 additions & 0 deletions machinery/src/capture/IPCamera.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,60 @@ func DecodeImage(frame *ffmpeg.VideoFrame, pkt av.Packet, decoder *ffmpeg.VideoD
return img, err
}

func GetStreamInsights(infile av.DemuxCloser, streams []av.CodecData) (int, int, int, int) {
var width, height, fps, gopsize int
for _, stream := range streams {
if stream.Type().IsAudio() {
//astream := stream.(av.AudioCodecData)
} else if stream.Type().IsVideo() {
vstream := stream.(av.VideoCodecData)
width = vstream.Width()
height = vstream.Height()
}
}

loop:
for timeout := time.After(1 * time.Second); ; {
var err error
if _, err = infile.ReadPacket(); err != nil { // sometimes this throws an end of file..
log.Log.Error("HandleStream: " + err.Error())
}
fps++
select {
case <-timeout:
break loop
default:
}
}

gopCounter := 0
start := false
for {
var pkt av.Packet
var err error
if pkt, err = infile.ReadPacket(); err != nil { // sometimes this throws an end of file..
log.Log.Error("HandleStream: " + err.Error())
}
// Could be that a decode is throwing errors.
if len(pkt.Data) > 0 {
if start {
gopCounter = gopCounter + 1
}

if pkt.IsKeyFrame {
if start == false {
start = true
} else {
gopsize = gopCounter
break
}
}
}
}

return width, height, fps, gopsize
}

func HandleStream(infile av.DemuxCloser, queue *pubsub.Queue, communication *models.Communication) { //, wg *sync.WaitGroup) {

log.Log.Debug("HandleStream: started")
Expand Down
8 changes: 7 additions & 1 deletion machinery/src/capture/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,12 @@ func HandleRecordStream(queue *pubsub.Queue, configuration *models.Configuration
startRecording = time.Now().Unix() // we mark the current time when the record started.
numberOfChanges := motion.NumberOfChanges

// If we have prerecording we will substract the number of seconds.
// Taking into account FPS = GOP size (Keyfram interval)
if config.Capture.PreRecording > 0 {
startRecording = startRecording - int64(config.Capture.PreRecording) + 1
}

// timestamp_microseconds_instanceName_regionCoordinates_numberOfChanges_token
// 1564859471_6-474162_oprit_577-283-727-375_1153_27.mp4
// - Timestamp
Expand Down Expand Up @@ -318,7 +324,7 @@ func HandleRecordStream(queue *pubsub.Queue, configuration *models.Configuration
var cursorError error
var pkt av.Packet
var nextPkt av.Packet
recordingCursor := queue.Oldest()
recordingCursor := queue.DelayedGopCount(int(config.Capture.PreRecording))

if cursorError == nil {
pkt, cursorError = recordingCursor.ReadPacket()
Expand Down
6 changes: 3 additions & 3 deletions machinery/src/components/Kerberos.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,14 @@ func RunAgent(configuration *models.Configuration, communication *models.Communi
// processed by the different consumers: motion detection, recording, etc.
queue = pubsub.NewQueue()
communication.Queue = queue
queue.SetMaxGopCount(int(config.Capture.PreRecording)) // GOP time frame is set to prerecording.
log.Log.Info("RunAgent: SetMaxGopCount was set with: " + strconv.Itoa(int(config.Capture.PreRecording)))
queue.SetMaxGopCount(int(config.Capture.PreRecording) + 1) // GOP time frame is set to prerecording (we'll add 2 gops to leave some room).
log.Log.Info("RunAgent: SetMaxGopCount was set with: " + strconv.Itoa(int(config.Capture.PreRecording)+1))
queue.WriteHeader(streams)

// We might have a substream, if so we'll create a seperate queue.
var subQueue *pubsub.Queue
if subStreamEnabled {
log.Log.Info("RunAgent: Creating sub stream queue with SetMaxGopCount set to " + strconv.Itoa(int(config.Capture.PreRecording)))
log.Log.Info("RunAgent: Creating sub stream queue with SetMaxGopCount set to " + strconv.Itoa(int(1)))
subQueue = pubsub.NewQueue()
subQueue.SetMaxGopCount(1)
subQueue.WriteHeader(subStreams)
Expand Down

0 comments on commit b4a8028

Please sign in to comment.