2019-12-24 06:38:42 +03:00
|
|
|
/*
|
|
|
|
DESCRIPTION
|
|
|
|
A motion filter that has a variable frame rate. When motion is detected,
|
|
|
|
the filter sends all frames and when it is not, the filter sends frames
|
|
|
|
at a reduced rate, as set by a parameter.
|
|
|
|
|
|
|
|
AUTHORS
|
|
|
|
Scott Barnard <scott@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
2019-12-31 03:24:13 +03:00
|
|
|
vfps.go is Copyright (C) 2019 the Australian Ocean Lab (AusOcean)
|
2019-12-24 06:38:42 +03:00
|
|
|
|
|
|
|
It is free software: you can redistribute it and/or modify them
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
in gpl.txt. If not, see http://www.gnu.org/licenses.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package filter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2020-02-03 09:09:25 +03:00
|
|
|
// VariableFPS is a filter that has a variable frame rate. When motion is
|
2019-12-24 06:38:42 +03:00
|
|
|
// detected, the filter sends all frames and when it is not, the filter
|
|
|
|
// sends frames at a reduced framerate.
|
2020-02-03 02:44:33 +03:00
|
|
|
type VariableFPS struct {
|
2019-12-31 03:24:13 +03:00
|
|
|
filter Filter
|
|
|
|
dst io.WriteCloser
|
|
|
|
frames uint
|
|
|
|
count uint
|
2019-12-24 06:38:42 +03:00
|
|
|
}
|
|
|
|
|
2020-02-03 09:14:38 +03:00
|
|
|
// NewVariableFPS returns a pointer to a new VariableFPS filter struct.
|
2020-05-19 09:02:02 +03:00
|
|
|
func NewVariableFPS(dst io.WriteCloser, minFPS uint, filter Filter) *VariableFPS {
|
2019-12-27 06:21:50 +03:00
|
|
|
frames := uint(25 / minFPS)
|
2020-02-03 02:44:33 +03:00
|
|
|
return &VariableFPS{filter, dst, frames, 0}
|
2019-12-24 06:38:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implements io.Writer.
|
|
|
|
// Write applies the motion filter to the video stream. Frames are sent
|
|
|
|
// at a reduced frame rate, except when motion is detected, then all frames
|
|
|
|
// with motion are sent.
|
2020-02-03 02:44:33 +03:00
|
|
|
func (v *VariableFPS) Write(f []byte) (int, error) {
|
2019-12-27 06:21:50 +03:00
|
|
|
v.count = (v.count + 1) % v.frames
|
|
|
|
|
|
|
|
if v.count == 0 {
|
2019-12-24 06:38:42 +03:00
|
|
|
return v.dst.Write(f)
|
|
|
|
}
|
|
|
|
|
2019-12-31 03:24:13 +03:00
|
|
|
return v.filter.Write(f)
|
2019-12-24 06:38:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implements io.Closer.
|
|
|
|
// Close calls the motion filter's Close method.
|
2020-02-03 02:44:33 +03:00
|
|
|
func (v *VariableFPS) Close() error {
|
2019-12-31 03:24:13 +03:00
|
|
|
return v.filter.Close()
|
2019-12-24 06:38:42 +03:00
|
|
|
}
|