2005-04-28 16:15:42 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
|
|
|
|
* 2000 Wim Taymans <wtay@chello.be>
|
|
|
|
*
|
|
|
|
* audioclock.c: Clock for use by audio plugins
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
2012-11-03 23:05:09 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2005-04-28 16:15:42 +00:00
|
|
|
*/
|
|
|
|
|
2006-09-27 11:05:08 +00:00
|
|
|
/**
|
|
|
|
* SECTION:gstaudioclock
|
2017-01-23 19:36:11 +00:00
|
|
|
* @title: GstAudioClock
|
2006-09-27 11:05:08 +00:00
|
|
|
* @short_description: Helper object for implementing audio clocks
|
2011-11-11 10:52:47 +00:00
|
|
|
* @see_also: #GstAudioBaseSink, #GstSystemClock
|
2006-09-27 11:05:08 +00:00
|
|
|
*
|
|
|
|
* #GstAudioClock makes it easy for elements to implement a #GstClock, they
|
|
|
|
* simply need to provide a function that returns the current clock time.
|
|
|
|
*
|
2011-11-11 10:52:47 +00:00
|
|
|
* This object is internally used to implement the clock in #GstAudioBaseSink.
|
2006-09-27 11:05:08 +00:00
|
|
|
*/
|
|
|
|
|
2005-04-28 16:15:42 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "gstaudioclock.h"
|
|
|
|
|
2009-04-14 11:08:52 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_audio_clock_debug);
|
|
|
|
#define GST_CAT_DEFAULT gst_audio_clock_debug
|
|
|
|
|
2010-07-08 06:32:22 +00:00
|
|
|
static void gst_audio_clock_dispose (GObject * object);
|
|
|
|
|
2005-04-28 16:15:42 +00:00
|
|
|
static GstClockTime gst_audio_clock_get_internal_time (GstClock * clock);
|
|
|
|
|
2014-12-13 15:01:44 +00:00
|
|
|
#define parent_class gst_audio_clock_parent_class
|
|
|
|
G_DEFINE_TYPE (GstAudioClock, gst_audio_clock, GST_TYPE_SYSTEM_CLOCK);
|
2005-04-28 16:15:42 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gst_audio_clock_class_init (GstAudioClockClass * klass)
|
|
|
|
{
|
|
|
|
GstClockClass *gstclock_class;
|
2010-07-08 06:32:22 +00:00
|
|
|
GObjectClass *gobject_class;
|
2005-04-28 16:15:42 +00:00
|
|
|
|
2010-07-08 06:32:22 +00:00
|
|
|
gobject_class = (GObjectClass *) klass;
|
2005-04-28 16:15:42 +00:00
|
|
|
gstclock_class = (GstClockClass *) klass;
|
|
|
|
|
2010-07-08 06:32:22 +00:00
|
|
|
gobject_class->dispose = gst_audio_clock_dispose;
|
2005-04-28 16:15:42 +00:00
|
|
|
gstclock_class->get_internal_time = gst_audio_clock_get_internal_time;
|
2009-04-14 11:08:52 +00:00
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_audio_clock_debug, "audioclock", 0,
|
|
|
|
"audioclock");
|
2005-04-28 16:15:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_audio_clock_init (GstAudioClock * clock)
|
|
|
|
{
|
2009-09-09 16:24:15 +00:00
|
|
|
GST_DEBUG_OBJECT (clock, "init");
|
2005-07-20 09:08:05 +00:00
|
|
|
clock->last_time = 0;
|
2011-06-21 16:17:59 +00:00
|
|
|
clock->time_offset = 0;
|
gst-libs/gst/audio/gstaudioclock.c: This clock can be slaved to a master clock now.
Original commit message from CVS:
* gst-libs/gst/audio/gstaudioclock.c: (gst_audio_clock_init):
This clock can be slaved to a master clock now.
* gst-libs/gst/audio/gstbaseaudiosink.c:
(gst_base_audio_sink_class_init), (gst_base_audio_sink_init),
(gst_base_audio_sink_dispose), (gst_base_audio_sink_provide_clock),
(gst_base_audio_sink_set_clock),
(gst_base_audio_sink_set_property),
(gst_base_audio_sink_get_property), (gst_base_audio_sink_preroll),
(gst_base_audio_sink_render), (gst_base_audio_sink_change_state):
* gst-libs/gst/audio/gstbaseaudiosink.h:
Handle slaving the internal clock to the clock selected in the
pipeline.
Add property to make the basesink not provide a clock.
* gst-libs/gst/rtp/gstbasertpdepayload.c:
(gst_base_rtp_depayload_class_init), (gst_base_rtp_depayload_init),
(gst_base_rtp_depayload_wait):
* gst-libs/gst/rtp/gstbasertpdepayload.h:
We can use the clock in GstElement, no need to store it ourselves.
2005-11-22 18:32:09 +00:00
|
|
|
GST_OBJECT_FLAG_SET (clock, GST_CLOCK_FLAG_CAN_SET_MASTER);
|
2005-04-28 16:15:42 +00:00
|
|
|
}
|
|
|
|
|
2010-07-08 06:32:22 +00:00
|
|
|
static void
|
|
|
|
gst_audio_clock_dispose (GObject * object)
|
|
|
|
{
|
|
|
|
GstAudioClock *clock = GST_AUDIO_CLOCK (object);
|
|
|
|
|
2011-06-21 16:17:59 +00:00
|
|
|
if (clock->destroy_notify && clock->user_data)
|
|
|
|
clock->destroy_notify (clock->user_data);
|
|
|
|
clock->destroy_notify = NULL;
|
2010-07-08 06:32:22 +00:00
|
|
|
clock->user_data = NULL;
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
2006-09-27 11:05:08 +00:00
|
|
|
/**
|
|
|
|
* gst_audio_clock_new:
|
|
|
|
* @name: the name of the clock
|
|
|
|
* @func: a function
|
|
|
|
* @user_data: user data
|
2010-07-08 06:32:22 +00:00
|
|
|
* @destroy_notify: #GDestroyNotify for @user_data
|
|
|
|
*
|
|
|
|
* Create a new #GstAudioClock instance. Whenever the clock time should be
|
|
|
|
* calculated it will call @func with @user_data. When @func returns
|
|
|
|
* #GST_CLOCK_TIME_NONE, the clock will return the last reported time.
|
|
|
|
*
|
2017-05-15 11:20:32 +00:00
|
|
|
* Returns: (transfer full): a new #GstAudioClock casted to a #GstClock.
|
2010-07-08 06:32:22 +00:00
|
|
|
*/
|
|
|
|
GstClock *
|
2011-11-10 12:50:08 +00:00
|
|
|
gst_audio_clock_new (const gchar * name, GstAudioClockGetTimeFunc func,
|
2010-07-08 06:32:22 +00:00
|
|
|
gpointer user_data, GDestroyNotify destroy_notify)
|
|
|
|
{
|
|
|
|
GstAudioClock *aclock =
|
2012-12-20 15:42:42 +00:00
|
|
|
GST_AUDIO_CLOCK (g_object_new (GST_TYPE_AUDIO_CLOCK, "name", name,
|
|
|
|
"clock-type", GST_CLOCK_TYPE_OTHER, NULL));
|
2010-07-08 06:32:22 +00:00
|
|
|
|
|
|
|
aclock->func = func;
|
|
|
|
aclock->user_data = user_data;
|
2011-06-21 16:17:59 +00:00
|
|
|
aclock->destroy_notify = destroy_notify;
|
2010-07-08 06:32:22 +00:00
|
|
|
|
2017-05-15 11:20:32 +00:00
|
|
|
gst_object_ref_sink (aclock);
|
|
|
|
|
2010-07-08 06:32:22 +00:00
|
|
|
return (GstClock *) aclock;
|
|
|
|
}
|
|
|
|
|
2008-05-27 16:20:17 +00:00
|
|
|
/**
|
|
|
|
* gst_audio_clock_reset:
|
|
|
|
* @clock: a #GstAudioClock
|
|
|
|
* @time: a #GstClockTime
|
|
|
|
*
|
|
|
|
* Inform @clock that future calls to #GstAudioClockGetTimeFunc will return values
|
|
|
|
* starting from @time. The clock will update an internal offset to make sure that
|
|
|
|
* future calls to internal_time will return an increasing result as required by
|
|
|
|
* the #GstClock object.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_audio_clock_reset (GstAudioClock * clock, GstClockTime time)
|
|
|
|
{
|
2009-04-14 11:08:52 +00:00
|
|
|
GstClockTimeDiff time_offset;
|
|
|
|
|
2008-05-27 16:20:17 +00:00
|
|
|
if (clock->last_time >= time)
|
2009-04-14 11:08:52 +00:00
|
|
|
time_offset = clock->last_time - time;
|
2008-05-27 16:20:17 +00:00
|
|
|
else
|
2009-04-14 11:08:52 +00:00
|
|
|
time_offset = -(time - clock->last_time);
|
|
|
|
|
2011-06-21 16:17:59 +00:00
|
|
|
clock->time_offset = time_offset;
|
2009-04-14 11:08:52 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (clock,
|
2016-07-20 10:17:57 +00:00
|
|
|
"reset clock to %" GST_TIME_FORMAT ", last %" GST_TIME_FORMAT
|
|
|
|
", offset %" GST_STIME_FORMAT, GST_TIME_ARGS (time),
|
2015-11-03 14:08:25 +00:00
|
|
|
GST_TIME_ARGS (clock->last_time), GST_STIME_ARGS (time_offset));
|
2008-05-27 16:20:17 +00:00
|
|
|
}
|
|
|
|
|
2010-09-07 16:06:27 +00:00
|
|
|
static GstClockTime
|
|
|
|
gst_audio_clock_func_invalid (GstClock * clock, gpointer user_data)
|
|
|
|
{
|
|
|
|
return GST_CLOCK_TIME_NONE;
|
|
|
|
}
|
|
|
|
|
2005-04-28 16:15:42 +00:00
|
|
|
static GstClockTime
|
|
|
|
gst_audio_clock_get_internal_time (GstClock * clock)
|
|
|
|
{
|
2008-05-27 16:20:17 +00:00
|
|
|
GstAudioClock *aclock;
|
2005-07-20 09:08:05 +00:00
|
|
|
GstClockTime result;
|
|
|
|
|
2009-04-14 11:08:52 +00:00
|
|
|
aclock = GST_AUDIO_CLOCK_CAST (clock);
|
2008-05-27 16:20:17 +00:00
|
|
|
|
2005-07-20 09:08:05 +00:00
|
|
|
result = aclock->func (clock, aclock->user_data);
|
2009-04-17 08:54:31 +00:00
|
|
|
if (result == GST_CLOCK_TIME_NONE) {
|
2005-07-20 09:08:05 +00:00
|
|
|
result = aclock->last_time;
|
2009-04-17 08:54:31 +00:00
|
|
|
} else {
|
2011-06-21 16:17:59 +00:00
|
|
|
result += aclock->time_offset;
|
2009-04-17 08:54:31 +00:00
|
|
|
/* clock must be increasing */
|
|
|
|
if (aclock->last_time < result)
|
|
|
|
aclock->last_time = result;
|
|
|
|
else
|
|
|
|
result = aclock->last_time;
|
2008-05-27 16:20:17 +00:00
|
|
|
}
|
2009-04-17 08:54:31 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (clock,
|
|
|
|
"result %" GST_TIME_FORMAT ", last_time %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (result), GST_TIME_ARGS (aclock->last_time));
|
|
|
|
|
2005-07-20 09:08:05 +00:00
|
|
|
return result;
|
2005-04-28 16:15:42 +00:00
|
|
|
}
|
2009-04-14 11:08:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_audio_clock_get_time:
|
|
|
|
* @clock: a #GstAudioClock
|
|
|
|
*
|
|
|
|
* Report the time as returned by the #GstAudioClockGetTimeFunc without applying
|
2009-10-06 12:08:48 +00:00
|
|
|
* any offsets.
|
2009-04-14 11:08:52 +00:00
|
|
|
*
|
|
|
|
* Returns: the time as reported by the time function of the audio clock
|
|
|
|
*/
|
|
|
|
GstClockTime
|
2015-10-15 10:52:27 +00:00
|
|
|
gst_audio_clock_get_time (GstAudioClock * clock)
|
2009-04-14 11:08:52 +00:00
|
|
|
{
|
|
|
|
GstClockTime result;
|
|
|
|
|
2015-10-15 10:52:27 +00:00
|
|
|
result = clock->func (GST_CLOCK_CAST (clock), clock->user_data);
|
2009-04-22 16:35:40 +00:00
|
|
|
if (result == GST_CLOCK_TIME_NONE) {
|
2009-09-09 16:24:15 +00:00
|
|
|
GST_DEBUG_OBJECT (clock, "no time, reuse last");
|
2015-10-15 10:52:27 +00:00
|
|
|
result = clock->last_time - clock->time_offset;
|
2009-04-22 16:35:40 +00:00
|
|
|
}
|
2009-04-14 11:08:52 +00:00
|
|
|
|
2009-09-09 16:24:15 +00:00
|
|
|
GST_DEBUG_OBJECT (clock,
|
|
|
|
"result %" GST_TIME_FORMAT ", last_time %" GST_TIME_FORMAT,
|
2015-10-15 10:52:27 +00:00
|
|
|
GST_TIME_ARGS (result), GST_TIME_ARGS (clock->last_time));
|
2009-09-09 16:24:15 +00:00
|
|
|
|
2009-04-14 11:08:52 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_audio_clock_adjust:
|
|
|
|
* @clock: a #GstAudioClock
|
|
|
|
* @time: a #GstClockTime
|
|
|
|
*
|
|
|
|
* Adjust @time with the internal offset of the audio clock.
|
|
|
|
*
|
|
|
|
* Returns: @time adjusted with the internal offset.
|
|
|
|
*/
|
|
|
|
GstClockTime
|
2015-10-15 10:52:27 +00:00
|
|
|
gst_audio_clock_adjust (GstAudioClock * clock, GstClockTime time)
|
2009-04-14 11:08:52 +00:00
|
|
|
{
|
|
|
|
GstClockTime result;
|
|
|
|
|
2015-10-15 10:52:27 +00:00
|
|
|
result = time + clock->time_offset;
|
2009-04-14 11:08:52 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2010-09-07 16:06:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_audio_clock_invalidate:
|
|
|
|
* @clock: a #GstAudioClock
|
|
|
|
*
|
|
|
|
* Invalidate the clock function. Call this function when the provided
|
|
|
|
* #GstAudioClockGetTimeFunc cannot be called anymore, for example, when the
|
|
|
|
* user_data becomes invalid.
|
|
|
|
*
|
|
|
|
* After calling this function, @clock will return the last returned time for
|
|
|
|
* the rest of its lifetime.
|
|
|
|
*/
|
|
|
|
void
|
2015-10-15 10:52:27 +00:00
|
|
|
gst_audio_clock_invalidate (GstAudioClock * clock)
|
2010-09-07 16:06:27 +00:00
|
|
|
{
|
2015-10-15 10:52:27 +00:00
|
|
|
clock->func = gst_audio_clock_func_invalid;
|
2010-09-07 16:06:27 +00:00
|
|
|
}
|