2003-07-16 16:08:13 +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
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2003-11-07 12:47:02 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2003-07-16 16:08:13 +00:00
|
|
|
#include "audioclock.h"
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
static void gst_audio_clock_class_init (GstAudioClockClass * klass);
|
|
|
|
static void gst_audio_clock_init (GstAudioClock * clock);
|
2003-07-16 16:08:13 +00:00
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
static GstClockTime gst_audio_clock_get_internal_time (GstClock * clock);
|
2004-05-16 20:55:38 +00:00
|
|
|
static GstClockEntryStatus gst_audio_clock_id_wait_async (GstClock * clock,
|
2004-03-14 22:34:34 +00:00
|
|
|
GstClockEntry * entry);
|
|
|
|
static void gst_audio_clock_id_unschedule (GstClock * clock,
|
|
|
|
GstClockEntry * entry);
|
2003-07-16 16:08:13 +00:00
|
|
|
|
|
|
|
static GstSystemClockClass *parent_class = NULL;
|
2004-03-14 22:34:34 +00:00
|
|
|
|
2003-07-16 16:08:13 +00:00
|
|
|
/* static guint gst_audio_clock_signals[LAST_SIGNAL] = { 0 }; */
|
2004-03-14 22:34:34 +00:00
|
|
|
|
2003-07-16 16:08:13 +00:00
|
|
|
GType
|
|
|
|
gst_audio_clock_get_type (void)
|
2004-03-14 22:34:34 +00:00
|
|
|
{
|
2003-07-16 16:08:13 +00:00
|
|
|
static GType clock_type = 0;
|
2004-03-14 22:34:34 +00:00
|
|
|
|
2003-07-16 16:08:13 +00:00
|
|
|
if (!clock_type) {
|
|
|
|
static const GTypeInfo clock_info = {
|
|
|
|
sizeof (GstAudioClockClass),
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
(GClassInitFunc) gst_audio_clock_class_init,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
sizeof (GstAudioClock),
|
|
|
|
4,
|
|
|
|
(GInstanceInitFunc) gst_audio_clock_init,
|
|
|
|
NULL
|
|
|
|
};
|
2004-03-15 19:32:28 +00:00
|
|
|
|
2003-07-16 16:08:13 +00:00
|
|
|
clock_type = g_type_register_static (GST_TYPE_SYSTEM_CLOCK, "GstAudioClock",
|
2004-03-15 19:32:28 +00:00
|
|
|
&clock_info, 0);
|
2003-07-16 16:08:13 +00:00
|
|
|
}
|
|
|
|
return clock_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_audio_clock_class_init (GstAudioClockClass * klass)
|
2003-07-16 16:08:13 +00:00
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstObjectClass *gstobject_class;
|
|
|
|
GstClockClass *gstclock_class;
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
gobject_class = (GObjectClass *) klass;
|
|
|
|
gstobject_class = (GstObjectClass *) klass;
|
|
|
|
gstclock_class = (GstClockClass *) klass;
|
2003-07-16 16:08:13 +00:00
|
|
|
|
|
|
|
parent_class = g_type_class_ref (GST_TYPE_SYSTEM_CLOCK);
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
gstclock_class->get_internal_time = gst_audio_clock_get_internal_time;
|
|
|
|
gstclock_class->wait_async = gst_audio_clock_id_wait_async;
|
|
|
|
gstclock_class->unschedule = gst_audio_clock_id_unschedule;
|
2003-07-16 16:08:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_audio_clock_init (GstAudioClock * clock)
|
2003-07-16 16:08:13 +00:00
|
|
|
{
|
|
|
|
gst_object_set_name (GST_OBJECT (clock), "GstAudioClock");
|
|
|
|
|
|
|
|
clock->prev1 = 0;
|
|
|
|
clock->prev2 = 0;
|
|
|
|
}
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
GstClock *
|
|
|
|
gst_audio_clock_new (gchar * name, GstAudioClockGetTimeFunc func,
|
|
|
|
gpointer user_data)
|
2003-07-16 16:08:13 +00:00
|
|
|
{
|
2004-03-14 22:34:34 +00:00
|
|
|
GstAudioClock *aclock =
|
|
|
|
GST_AUDIO_CLOCK (g_object_new (GST_TYPE_AUDIO_CLOCK, NULL));
|
2003-07-16 16:08:13 +00:00
|
|
|
|
|
|
|
aclock->func = func;
|
|
|
|
aclock->user_data = user_data;
|
|
|
|
aclock->adjust = 0;
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
return (GstClock *) aclock;
|
2003-07-16 16:08:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_audio_clock_set_active (GstAudioClock * aclock, gboolean active)
|
2003-07-16 16:08:13 +00:00
|
|
|
{
|
2004-11-27 09:37:20 +00:00
|
|
|
GstClockTime audio_time, system_time;
|
2004-01-14 00:53:52 +00:00
|
|
|
GstClock *clock;
|
2004-11-27 09:37:20 +00:00
|
|
|
GTimeVal timeval;
|
2003-07-16 16:08:13 +00:00
|
|
|
|
2004-01-14 00:53:52 +00:00
|
|
|
g_return_if_fail (GST_IS_AUDIO_CLOCK (aclock));
|
|
|
|
clock = GST_CLOCK (aclock);
|
|
|
|
|
2004-11-27 09:37:20 +00:00
|
|
|
if (active == aclock->active) {
|
|
|
|
/* Nothing to do. */
|
|
|
|
return;
|
|
|
|
}
|
2003-07-16 16:08:13 +00:00
|
|
|
|
2004-11-27 09:37:20 +00:00
|
|
|
audio_time = aclock->func (clock, aclock->user_data);
|
2004-03-14 22:34:34 +00:00
|
|
|
|
2004-11-27 09:37:20 +00:00
|
|
|
g_get_current_time (&timeval);
|
|
|
|
system_time = GST_TIMEVAL_TO_TIME (timeval);
|
2004-03-14 22:34:34 +00:00
|
|
|
|
2004-11-27 09:37:20 +00:00
|
|
|
/* Set the new adjust value in such a way that there's no abrupt
|
|
|
|
discontinuity, i.e. if gst_audio_clock_get_internal_time is
|
|
|
|
invoked right before and right after (de)activating the clock,
|
|
|
|
the values returned will be close to each other, and the second
|
|
|
|
value will be greater than or equal than the first. */
|
|
|
|
if (active) {
|
|
|
|
aclock->adjust = aclock->adjust + system_time - audio_time;
|
|
|
|
} else {
|
|
|
|
aclock->adjust = aclock->adjust + audio_time - system_time;
|
2003-07-16 16:08:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
aclock->active = active;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstClockTime
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_audio_clock_get_internal_time (GstClock * clock)
|
2003-07-16 16:08:13 +00:00
|
|
|
{
|
|
|
|
GstAudioClock *aclock = GST_AUDIO_CLOCK (clock);
|
2004-03-14 22:34:34 +00:00
|
|
|
|
2003-07-16 16:08:13 +00:00
|
|
|
if (aclock->active) {
|
2004-01-14 00:53:52 +00:00
|
|
|
return aclock->func (clock, aclock->user_data) + aclock->adjust;
|
|
|
|
} else {
|
2003-07-16 16:08:13 +00:00
|
|
|
GTimeVal timeval;
|
2004-03-14 22:34:34 +00:00
|
|
|
|
2003-07-16 16:08:13 +00:00
|
|
|
g_get_current_time (&timeval);
|
2004-11-27 09:37:20 +00:00
|
|
|
return GST_TIMEVAL_TO_TIME (timeval) + aclock->adjust;
|
2003-07-16 16:08:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_audio_clock_update_time (GstAudioClock * aclock, GstClockTime time)
|
2003-07-16 16:08:13 +00:00
|
|
|
{
|
|
|
|
/* I don't know of a purpose in updating these; perhaps they can be removed */
|
|
|
|
aclock->prev2 = aclock->prev1;
|
|
|
|
aclock->prev1 = time;
|
|
|
|
|
|
|
|
/* FIXME: the wait_async subsystem should be made threadsafe, but I don't want
|
|
|
|
* to lock and unlock a mutex on every iteration... */
|
|
|
|
while (aclock->async_entries) {
|
2004-03-14 22:34:34 +00:00
|
|
|
GstClockEntry *entry = (GstClockEntry *) aclock->async_entries->data;
|
|
|
|
|
2003-07-16 16:08:13 +00:00
|
|
|
if (entry->time > time)
|
|
|
|
break;
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
entry->func ((GstClock *) aclock, time, entry, entry->user_data);
|
2003-07-16 16:08:13 +00:00
|
|
|
|
|
|
|
aclock->async_entries = g_slist_delete_link (aclock->async_entries,
|
2004-03-15 19:32:28 +00:00
|
|
|
aclock->async_entries);
|
2003-07-16 16:08:13 +00:00
|
|
|
/* do I need to free the entry? */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
2004-03-14 22:34:34 +00:00
|
|
|
compare_clock_entries (GstClockEntry * entry1, GstClockEntry * entry2)
|
2003-07-16 16:08:13 +00:00
|
|
|
{
|
|
|
|
return entry1->time - entry2->time;
|
|
|
|
}
|
|
|
|
|
2004-05-16 20:55:38 +00:00
|
|
|
static GstClockEntryStatus
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_audio_clock_id_wait_async (GstClock * clock, GstClockEntry * entry)
|
2003-07-16 16:08:13 +00:00
|
|
|
{
|
2004-03-14 22:34:34 +00:00
|
|
|
GstAudioClock *aclock = (GstAudioClock *) clock;
|
|
|
|
|
2003-07-16 16:08:13 +00:00
|
|
|
aclock->async_entries = g_slist_insert_sorted (aclock->async_entries,
|
2004-03-14 22:34:34 +00:00
|
|
|
entry, (GCompareFunc) compare_clock_entries);
|
2003-07-16 16:08:13 +00:00
|
|
|
|
|
|
|
/* is this the proper return val? */
|
|
|
|
return GST_CLOCK_EARLY;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_audio_clock_id_unschedule (GstClock * clock, GstClockEntry * entry)
|
2003-07-16 16:08:13 +00:00
|
|
|
{
|
2004-03-14 22:34:34 +00:00
|
|
|
GstAudioClock *aclock = (GstAudioClock *) clock;
|
|
|
|
|
|
|
|
aclock->async_entries = g_slist_remove (aclock->async_entries, entry);
|
2003-07-16 16:08:13 +00:00
|
|
|
}
|