diff --git a/.gitignore b/.gitignore index 43bca47..b89c3d9 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,6 @@ ui/build .idea machinery/www yarn.lock -machinery/data/config/config.json +machinery/data/config +machinery/data/cloud +machinery/data/recordings \ No newline at end of file diff --git a/machinery/src/capture/main.go b/machinery/src/capture/main.go index 4ed1e75..557b404 100644 --- a/machinery/src/capture/main.go +++ b/machinery/src/capture/main.go @@ -52,6 +52,7 @@ func HandleRecordStream(recordingCursor *pubsub.QueueCursor, configuration *mode //for pkt := range packets { var cursorError error var pkt av.Packet + recordingStatus := "idle" for cursorError == nil { @@ -84,6 +85,8 @@ func HandleRecordStream(recordingCursor *pubsub.QueueCursor, configuration *mode myMuxer = nil runtime.GC() debug.FreeOSMemory() + + recordingStatus = "idle" } // If not yet started and a keyframe, let's make a recording @@ -151,6 +154,8 @@ func HandleRecordStream(recordingCursor *pubsub.QueueCursor, configuration *mode log.Log.Error(err.Error()) } + recordingStatus = "started" + } else if start { if err := myMuxer.WritePacket(pkt); err != nil { log.Log.Error(err.Error()) @@ -158,6 +163,36 @@ func HandleRecordStream(recordingCursor *pubsub.QueueCursor, configuration *mode } } + // We might have interrupted the recording while restarting the agent. + // If this happens we need to check to properly close the recording. + if cursorError != nil { + if recordingStatus == "started" { + // This will write the trailer a well. + if err := myMuxer.WriteTrailer(); err != nil { + log.Log.Error(err.Error()) + } + + log.Log.Info("HandleRecordStream: Recording finished: file save: " + name) + file.Close() + + // Check if need to convert to fragmented using bento + if config.Capture.Fragmented == "true" && config.Capture.FragmentedDuration > 0 { + utils.CreateFragmentedMP4(fullName, config.Capture.FragmentedDuration) + } + + // Create a symbol link. + fc, _ := os.Create("./data/cloud/" + name) + fc.Close() + + // Cleanup muxer + start = false + myMuxer = nil + runtime.GC() + debug.FreeOSMemory() + + recordingStatus = "idle" + } + } } else { log.Log.Info("HandleRecordStream: Start motion based recording ") diff --git a/machinery/src/components/Kerberos.go b/machinery/src/components/Kerberos.go index 765d6ff..98c3464 100644 --- a/machinery/src/components/Kerberos.go +++ b/machinery/src/components/Kerberos.go @@ -103,6 +103,7 @@ func RunAgent(configuration *models.Configuration, communication *models.Communi communication.HandleHeartBeat <- "stop" infile.Close() queue.Close() + DisconnectMQTT(mqttClient) } else { time.Sleep(time.Second * 2) diff --git a/machinery/src/components/MQTT.go b/machinery/src/components/MQTT.go index cebbf65..5a23a84 100644 --- a/machinery/src/components/MQTT.go +++ b/machinery/src/components/MQTT.go @@ -185,6 +185,6 @@ func MQTTListenerHandleONVIF(mqttClient mqtt.Client, hubKey string, configuratio }) } -func DisconnectMQTT(config models.Config, mqc mqtt.Client) { - mqc.Disconnect(1000) +func DisconnectMQTT(mqttClient mqtt.Client) { + mqttClient.Disconnect(1000) }