Skip to content

Commit

Permalink
do not reload decoder if same settings
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricve committed Feb 21, 2023
1 parent 56cebb6 commit d70a3ed
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
12 changes: 12 additions & 0 deletions machinery/src/capture/IPCamera.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 29 additions & 7 deletions machinery/src/components/Kerberos.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions machinery/src/models/Camera.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit d70a3ed

Please sign in to comment.