/* 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 LICENSE vfps.go is Copyright (C) 2019 the Australian Ocean Lab (AusOcean) 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" ) // VariableFPSFilter is a 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 framerate. type VariableFPSFilter struct { filter Filter dst io.WriteCloser frames uint count uint } // NewVariableFPSFilter returns a pointer to a new VariableFPSFilter struct. func NewVariableFPSFilter(dst io.WriteCloser, minFPS float64, filter Filter) *VariableFPSFilter { frames := uint(25 / minFPS) return &VariableFPSFilter{filter, dst, frames, 0} } // 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. func (v *VariableFPSFilter) Write(f []byte) (int, error) { v.count = (v.count + 1) % v.frames if v.count == 0 { return v.dst.Write(f) } return v.filter.Write(f) } // Implements io.Closer. // Close calls the motion filter's Close method. func (v *VariableFPSFilter) Close() error { return v.filter.Close() }