mirror of
https://github.com/brunoherbelin/vimix.git
synced 2025-12-16 20:59:59 +01:00
Improved recording time acuracy
This commit is contained in:
@@ -215,13 +215,12 @@ void FrameGrabbing::grabFrame(FrameBuffer *frame_buffer, float dt)
|
|||||||
|
|
||||||
|
|
||||||
FrameGrabber::FrameGrabber(): finished_(false), expecting_finished_(false), active_(false), accept_buffer_(false),
|
FrameGrabber::FrameGrabber(): finished_(false), expecting_finished_(false), active_(false), accept_buffer_(false),
|
||||||
pipeline_(nullptr), src_(nullptr), caps_(nullptr), timestamp_(0)
|
pipeline_(nullptr), src_(nullptr), caps_(nullptr), timer_(nullptr), timestamp_(0)
|
||||||
{
|
{
|
||||||
// unique id
|
// unique id
|
||||||
id_ = BaseToolkit::uniqueId();
|
id_ = BaseToolkit::uniqueId();
|
||||||
// configure fix parameter
|
// configure fix parameter
|
||||||
frame_duration_ = gst_util_uint64_scale_int (1, GST_SECOND, 30); // 30 FPS
|
frame_duration_ = gst_util_uint64_scale_int (1, GST_SECOND, 30); // 30 FPS
|
||||||
timeframe_ = 2 * frame_duration_;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
FrameGrabber::~FrameGrabber()
|
FrameGrabber::~FrameGrabber()
|
||||||
@@ -332,31 +331,33 @@ void FrameGrabber::addFrame (GstBuffer *buffer, GstCaps *caps, float dt)
|
|||||||
// store a frame if recording is active
|
// store a frame if recording is active
|
||||||
else if (active_)
|
else if (active_)
|
||||||
{
|
{
|
||||||
// calculate dt in ns
|
// initialize timer
|
||||||
timeframe_ += gst_gdouble_to_guint64( dt * 1000000.f );
|
if (timer_ == nullptr) {
|
||||||
|
timer_ = gst_pipeline_get_clock ( GST_PIPELINE(pipeline_) );
|
||||||
|
timer_firstframe_ = gst_clock_get_time(timer_);
|
||||||
|
}
|
||||||
|
|
||||||
// if time is passed one frame duration (with 10% margin)
|
// time since begin
|
||||||
|
GstClockTime t = gst_clock_get_time(timer_) - timer_firstframe_;
|
||||||
|
t = (t / frame_duration_) * frame_duration_;
|
||||||
|
|
||||||
|
// if delta time is passed one frame duration (with a margin)
|
||||||
// and if the encoder accepts data
|
// and if the encoder accepts data
|
||||||
if ( timeframe_ > frame_duration_ - 3000000 && accept_buffer_) {
|
if ( (t - timestamp_) > (frame_duration_ - 3000) && accept_buffer_) {
|
||||||
|
|
||||||
// set timing of buffer
|
// set timing of buffer
|
||||||
buffer->pts = timestamp_;
|
buffer->pts = t;
|
||||||
buffer->duration = frame_duration_;
|
buffer->duration = t - timestamp_;
|
||||||
|
|
||||||
// increment ref counter to make sure the frame remains available
|
// increment ref counter to make sure the frame remains available
|
||||||
gst_buffer_ref(buffer);
|
gst_buffer_ref(buffer);
|
||||||
|
|
||||||
// push
|
// push frame
|
||||||
gst_app_src_push_buffer (src_, buffer);
|
gst_app_src_push_buffer (src_, buffer);
|
||||||
// NB: buffer will be unrefed by the appsrc
|
// NB: buffer will be unrefed by the appsrc
|
||||||
|
|
||||||
accept_buffer_ = false;
|
// keep timestamp
|
||||||
|
timestamp_ = t;
|
||||||
// next timestamp
|
|
||||||
timestamp_ += frame_duration_;
|
|
||||||
|
|
||||||
// restart frame counter
|
|
||||||
timeframe_ = 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -62,10 +62,13 @@ protected:
|
|||||||
GstElement *pipeline_;
|
GstElement *pipeline_;
|
||||||
GstAppSrc *src_;
|
GstAppSrc *src_;
|
||||||
GstCaps *caps_;
|
GstCaps *caps_;
|
||||||
GstClockTime timeframe_;
|
|
||||||
GstClockTime timestamp_;
|
GstClockTime timestamp_;
|
||||||
GstClockTime frame_duration_;
|
GstClockTime frame_duration_;
|
||||||
|
|
||||||
|
GstClockTime timer_firstframe_;
|
||||||
|
GstClock *timer_;
|
||||||
|
|
||||||
// gstreamer callbacks
|
// gstreamer callbacks
|
||||||
static void callback_need_data (GstAppSrc *, guint, gpointer user_data);
|
static void callback_need_data (GstAppSrc *, guint, gpointer user_data);
|
||||||
static void callback_enough_data (GstAppSrc *, gpointer user_data);
|
static void callback_enough_data (GstAppSrc *, gpointer user_data);
|
||||||
|
|||||||
Reference in New Issue
Block a user