From d70a3ed3433d7f5edec118e6c0c943a3500730bb Mon Sep 17 00:00:00 2001 From: Cedric Verstraeten Date: Tue, 21 Feb 2023 22:44:56 +0100 Subject: [PATCH] do not reload decoder if same settings --- machinery/src/capture/IPCamera.go | 12 ++++++++++ machinery/src/components/Kerberos.go | 36 ++++++++++++++++++++++------ machinery/src/models/Camera.go | 14 +++++++++++ 3 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 machinery/src/models/Camera.go diff --git a/machinery/src/capture/IPCamera.go b/machinery/src/capture/IPCamera.go index 587ac86..fb39cb1 100644 --- a/machinery/src/capture/IPCamera.go +++ b/machinery/src/capture/IPCamera.go @@ -25,6 +25,18 @@ func OpenRTSP(url string) (av.DemuxCloser, []av.CodecData, error) { return nil, []av.CodecData{}, err } +func GetVideoStream(streams []av.CodecData) (av.CodecData, error) { + var videoStream av.CodecData + for _, stream := range streams { + if stream.Type().IsAudio() { + //astream := stream.(av.AudioCodecData) + } else if stream.Type().IsVideo() { + videoStream = stream + } + } + return videoStream, nil +} + func GetVideoDecoder(decoder *ffmpeg.VideoDecoder, streams []av.CodecData) { // Load video codec var vstream av.VideoCodecData diff --git a/machinery/src/components/Kerberos.go b/machinery/src/components/Kerberos.go index 0aeba90..9b67b03 100644 --- a/machinery/src/components/Kerberos.go +++ b/machinery/src/components/Kerberos.go @@ -61,13 +61,14 @@ func Bootstrap(configuration *models.Configuration, communication *models.Commun // Create some global variables decoder := &ffmpeg.VideoDecoder{} subDecoder := &ffmpeg.VideoDecoder{} + cameraSettings := &models.Camera{} // Run the agent and fire up all the other // goroutines which do image capture, motion detection, onvif, etc. for { // This will blocking until receiving a signal to be restarted, reconfigured, stopped, etc. - status := RunAgent(configuration, communication, uptimeStart, decoder, subDecoder) + status := RunAgent(configuration, communication, uptimeStart, cameraSettings, decoder, subDecoder) if status == "stop" { break } @@ -77,7 +78,7 @@ func Bootstrap(configuration *models.Configuration, communication *models.Commun log.Log.Debug("Bootstrap: finished") } -func RunAgent(configuration *models.Configuration, communication *models.Communication, uptimeStart time.Time, decoder *ffmpeg.VideoDecoder, subDecoder *ffmpeg.VideoDecoder) string { +func RunAgent(configuration *models.Configuration, communication *models.Communication, uptimeStart time.Time, cameraSettings *models.Camera, decoder *ffmpeg.VideoDecoder, subDecoder *ffmpeg.VideoDecoder) string { log.Log.Debug("RunAgent: started") config := configuration.Config @@ -113,12 +114,33 @@ func RunAgent(configuration *models.Configuration, communication *models.Communi } } - // At some routines we will need to decode the image. - // Make sure its properly locked as we only have a single decoder. - capture.GetVideoDecoder(decoder, streams) + // We will initialise the camera settings object + // so we can check if the camera settings have changed, and we need + // to reload the decoders. + videoStream, _ := capture.GetVideoStream(streams) + num, denum := videoStream.(av.VideoCodecData).Framerate() + width := videoStream.(av.VideoCodecData).Width() + height := videoStream.(av.VideoCodecData).Height() + + if cameraSettings.RTSP != rtspUrl || cameraSettings.SubRTSP != subRtspUrl || cameraSettings.Width != width || cameraSettings.Height != height || cameraSettings.Num != num || cameraSettings.Denum != denum || cameraSettings.Codec != videoStream.(av.VideoCodecData).Type() { + // At some routines we will need to decode the image. + // Make sure its properly locked as we only have a single decoder. + log.Log.Info("RunAgent: camera settings changed, reloading decoder") + capture.GetVideoDecoder(decoder, streams) + if subStreamEnabled { + capture.GetVideoDecoder(subDecoder, subStreams) + } - if subStreamEnabled { - capture.GetVideoDecoder(subDecoder, subStreams) + cameraSettings.RTSP = rtspUrl + cameraSettings.SubRTSP = subRtspUrl + cameraSettings.Width = width + cameraSettings.Height = height + cameraSettings.Framerate = float64(num) / float64(denum) + cameraSettings.Num = num + cameraSettings.Denum = denum + cameraSettings.Codec = videoStream.(av.VideoCodecData).Type() + } else { + log.Log.Info("RunAgent: camera settings did not change, keeping decoder") } communication.Decoder = decoder diff --git a/machinery/src/models/Camera.go b/machinery/src/models/Camera.go new file mode 100644 index 0000000..a7c2fbd --- /dev/null +++ b/machinery/src/models/Camera.go @@ -0,0 +1,14 @@ +package models + +import "github.com/kerberos-io/joy4/av" + +type Camera struct { + Width int + Height int + Num int + Denum int + Framerate float64 + RTSP string + SubRTSP string + Codec av.CodecType +}