gstreamer/gst-libs/gst/audio/gstaudioclock.c

239 lines
6.3 KiB
C
Raw Normal View History

/* 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.
*/
/**
* SECTION:gstaudioclock
* @short_description: Helper object for implementing audio clocks
* @see_also: #GstBaseAudioSink, #GstSystemClock
*
* #GstAudioClock makes it easy for elements to implement a #GstClock, they
* simply need to provide a function that returns the current clock time.
*
* This object is internally used to implement the clock in #GstBaseAudioSink.
*
* Last reviewed on 2006-09-27 (0.10.12)
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gstaudioclock.h"
GST_DEBUG_CATEGORY_STATIC (gst_audio_clock_debug);
#define GST_CAT_DEFAULT gst_audio_clock_debug
static void gst_audio_clock_class_init (GstAudioClockClass * klass);
static void gst_audio_clock_init (GstAudioClock * clock);
static GstClockTime gst_audio_clock_get_internal_time (GstClock * clock);
static GstSystemClockClass *parent_class = NULL;
/* static guint gst_audio_clock_signals[LAST_SIGNAL] = { 0 }; */
GType
gst_audio_clock_get_type (void)
{
static GType clock_type = 0;
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
};
clock_type = g_type_register_static (GST_TYPE_SYSTEM_CLOCK, "GstAudioClock",
&clock_info, 0);
}
return clock_type;
}
static void
gst_audio_clock_class_init (GstAudioClockClass * klass)
{
GstClockClass *gstclock_class;
gstclock_class = (GstClockClass *) klass;
Fix #337365 (g_type_class_ref <-> g_type_class_peek_parent) Original commit message from CVS: * ext/alsa/gstalsamixeroptions.c: (gst_alsa_mixer_options_class_init): * ext/alsa/gstalsamixertrack.c: (gst_alsa_mixer_track_class_init): * ext/ogg/gstoggdemux.c: (gst_ogg_pad_class_init): * ext/ogg/gstoggmux.c: (gst_ogg_mux_class_init): * ext/ogg/gstoggparse.c: (gst_ogg_parse_class_init): * gst-libs/gst/audio/gstaudioclock.c: (gst_audio_clock_class_init): * gst-libs/gst/audio/gstaudiofilter.c: (gst_audio_filter_class_init): * gst-libs/gst/audio/gstaudiosink.c: (gst_audioringbuffer_class_init): * gst-libs/gst/audio/gstaudiosrc.c: (gst_audioringbuffer_class_init): * gst-libs/gst/audio/gstringbuffer.c: (gst_ring_buffer_class_init): * gst-libs/gst/interfaces/colorbalancechannel.c: (gst_color_balance_channel_class_init): * gst-libs/gst/interfaces/mixeroptions.c: (gst_mixer_options_class_init): * gst-libs/gst/interfaces/mixertrack.c: (gst_mixer_track_class_init): * gst-libs/gst/interfaces/tunerchannel.c: (gst_tuner_channel_class_init): * gst-libs/gst/interfaces/tunernorm.c: (gst_tuner_norm_class_init): * gst-libs/gst/netbuffer/gstnetbuffer.c: (gst_netbuffer_class_init): * gst-libs/gst/rtp/gstbasertppayload.c: (gst_basertppayload_class_init): * gst/playback/gstdecodebin.c: (gst_decode_bin_class_init): * gst/playback/gstplaybasebin.c: (gst_play_base_bin_class_init): * gst/playback/gstplaybin.c: (gst_play_bin_class_init): * gst/playback/gststreaminfo.c: (gst_stream_info_class_init): * gst/playback/gststreamselector.c: (gst_stream_selector_class_init): * gst/subparse/gstsubparse.c: (gst_sub_parse_class_init): * gst/tcp/gsttcpclientsink.c: (gst_tcp_client_sink_class_init): * sys/v4l/gstv4lcolorbalance.c: (gst_v4l_color_balance_channel_class_init): * sys/v4l/gstv4ljpegsrc.c: (gst_v4ljpegsrc_class_init): * sys/v4l/gstv4lmjpegsink.c: (gst_v4lmjpegsink_class_init): * sys/v4l/gstv4lmjpegsrc.c: (gst_v4lmjpegsrc_class_init): * sys/v4l/gstv4ltuner.c: (gst_v4l_tuner_channel_class_init), (gst_v4l_tuner_norm_class_init): * sys/ximage/ximagesink.c: (gst_ximagesink_class_init): * sys/xvimage/xvimagesink.c: (gst_xvimagesink_class_init): * tests/old/testsuite/alsa/sinesrc.c: (sinesrc_class_init): Fix #337365 (g_type_class_ref <-> g_type_class_peek_parent)
2006-04-08 21:02:53 +00:00
parent_class = g_type_class_peek_parent (klass);
gstclock_class->get_internal_time = gst_audio_clock_get_internal_time;
GST_DEBUG_CATEGORY_INIT (gst_audio_clock_debug, "audioclock", 0,
"audioclock");
}
static void
gst_audio_clock_init (GstAudioClock * clock)
{
2009-09-09 16:24:15 +00:00
GST_DEBUG_OBJECT (clock, "init");
clock->last_time = 0;
clock->abidata.ABI.time_offset = 0;
GST_OBJECT_FLAG_SET (clock, GST_CLOCK_FLAG_CAN_SET_MASTER);
}
/**
* gst_audio_clock_new:
* @name: the name of the clock
* @func: a function
* @user_data: 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.
*
* Returns: a new #GstAudioClock casted to a #GstClock.
*/
GstClock *
gst_audio_clock_new (const gchar * name, GstAudioClockGetTimeFunc func,
gpointer user_data)
{
GstAudioClock *aclock =
GST_AUDIO_CLOCK (g_object_new (GST_TYPE_AUDIO_CLOCK, "name", name, NULL));
aclock->func = func;
aclock->user_data = user_data;
return (GstClock *) aclock;
}
/**
* 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)
{
GstClockTimeDiff time_offset;
if (clock->last_time >= time)
time_offset = clock->last_time - time;
else
time_offset = -(time - clock->last_time);
clock->abidata.ABI.time_offset = time_offset;
GST_DEBUG_OBJECT (clock,
2009-09-09 16:24:15 +00:00
"reset clock to %" GST_TIME_FORMAT ", last %" GST_TIME_FORMAT ", offset %"
GST_TIME_FORMAT, GST_TIME_ARGS (time), GST_TIME_ARGS (clock->last_time),
GST_TIME_ARGS (time_offset));
}
static GstClockTime
gst_audio_clock_get_internal_time (GstClock * clock)
{
GstAudioClock *aclock;
GstClockTime result;
aclock = GST_AUDIO_CLOCK_CAST (clock);
result = aclock->func (clock, aclock->user_data);
if (result == GST_CLOCK_TIME_NONE) {
result = aclock->last_time;
} else {
result += aclock->abidata.ABI.time_offset;
/* clock must be increasing */
if (aclock->last_time < result)
aclock->last_time = result;
else
result = aclock->last_time;
}
GST_DEBUG_OBJECT (clock,
"result %" GST_TIME_FORMAT ", last_time %" GST_TIME_FORMAT,
GST_TIME_ARGS (result), GST_TIME_ARGS (aclock->last_time));
return result;
}
/**
* gst_audio_clock_get_time:
* @clock: a #GstAudioClock
*
* Report the time as returned by the #GstAudioClockGetTimeFunc without applying
* any offsets.
*
* Returns: the time as reported by the time function of the audio clock
*
* Since: 0.10.23
*/
GstClockTime
gst_audio_clock_get_time (GstClock * clock)
{
GstAudioClock *aclock;
GstClockTime result;
aclock = GST_AUDIO_CLOCK_CAST (clock);
result = aclock->func (clock, aclock->user_data);
if (result == GST_CLOCK_TIME_NONE) {
2009-09-09 16:24:15 +00:00
GST_DEBUG_OBJECT (clock, "no time, reuse last");
result = aclock->last_time - aclock->abidata.ABI.time_offset;
}
2009-09-09 16:24:15 +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));
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.
*
* Since: 0.10.23
*/
GstClockTime
gst_audio_clock_adjust (GstClock * clock, GstClockTime time)
{
GstAudioClock *aclock;
GstClockTime result;
aclock = GST_AUDIO_CLOCK_CAST (clock);
result = time + aclock->abidata.ABI.time_offset;
return result;
}