2005-07-06 15:27:17 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
|
|
|
|
* 2005 Wim Taymans <wim@fluendo.com>
|
|
|
|
*
|
|
|
|
* gstbaseaudiosrc.c:
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "gstbaseaudiosrc.h"
|
|
|
|
|
2005-07-10 12:03:58 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_base_audio_src_debug);
|
|
|
|
#define GST_CAT_DEFAULT gst_base_audio_src_debug
|
2005-07-06 15:27:17 +00:00
|
|
|
|
|
|
|
/* BaseAudioSrc signals and args */
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
/* FILL ME */
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
2006-07-06 15:54:50 +00:00
|
|
|
#define DEFAULT_BUFFER_TIME ((200 * GST_MSECOND) / GST_USECOND)
|
|
|
|
#define DEFAULT_LATENCY_TIME ((10 * GST_MSECOND) / GST_USECOND)
|
|
|
|
|
2005-07-06 15:27:17 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_BUFFER_TIME,
|
|
|
|
PROP_LATENCY_TIME,
|
|
|
|
};
|
|
|
|
|
|
|
|
#define _do_init(bla) \
|
2005-07-10 12:03:58 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_base_audio_src_debug, "baseaudiosrc", 0, "baseaudiosrc element");
|
2005-07-06 15:27:17 +00:00
|
|
|
|
2005-07-10 12:03:58 +00:00
|
|
|
GST_BOILERPLATE_FULL (GstBaseAudioSrc, gst_base_audio_src, GstPushSrc,
|
2005-07-14 09:35:11 +00:00
|
|
|
GST_TYPE_PUSH_SRC, _do_init);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
2005-07-10 12:03:58 +00:00
|
|
|
static void gst_base_audio_src_set_property (GObject * object, guint prop_id,
|
2005-07-06 15:27:17 +00:00
|
|
|
const GValue * value, GParamSpec * pspec);
|
2005-07-10 12:03:58 +00:00
|
|
|
static void gst_base_audio_src_get_property (GObject * object, guint prop_id,
|
2005-07-06 15:27:17 +00:00
|
|
|
GValue * value, GParamSpec * pspec);
|
|
|
|
|
2005-07-10 12:03:58 +00:00
|
|
|
static void gst_base_audio_src_fixate (GstPad * pad, GstCaps * caps);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
2005-09-02 15:43:18 +00:00
|
|
|
static GstStateChangeReturn gst_base_audio_src_change_state (GstElement *
|
|
|
|
element, GstStateChange transition);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
2005-09-28 13:41:29 +00:00
|
|
|
static GstClock *gst_base_audio_src_provide_clock (GstElement * elem);
|
2006-07-06 15:54:50 +00:00
|
|
|
static gboolean gst_base_audio_src_set_clock (GstElement * elem,
|
|
|
|
GstClock * clock);
|
2005-07-10 12:03:58 +00:00
|
|
|
static GstClockTime gst_base_audio_src_get_time (GstClock * clock,
|
2005-07-06 15:27:17 +00:00
|
|
|
GstBaseAudioSrc * src);
|
|
|
|
|
2006-07-06 15:54:50 +00:00
|
|
|
static GstFlowReturn gst_base_audio_src_create (GstBaseSrc * bsrc,
|
|
|
|
guint64 offset, guint length, GstBuffer ** buf);
|
|
|
|
static gboolean gst_base_audio_src_check_get_range (GstBaseSrc * bsrc);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
2005-07-10 12:03:58 +00:00
|
|
|
static gboolean gst_base_audio_src_event (GstBaseSrc * bsrc, GstEvent * event);
|
|
|
|
static void gst_base_audio_src_get_times (GstBaseSrc * bsrc,
|
2005-07-06 15:27:17 +00:00
|
|
|
GstBuffer * buffer, GstClockTime * start, GstClockTime * end);
|
2005-07-10 12:03:58 +00:00
|
|
|
static gboolean gst_base_audio_src_setcaps (GstBaseSrc * bsrc, GstCaps * caps);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
2005-07-10 12:03:58 +00:00
|
|
|
//static guint gst_base_audio_src_signals[LAST_SIGNAL] = { 0 };
|
2005-07-06 15:27:17 +00:00
|
|
|
|
|
|
|
static void
|
2005-07-10 12:03:58 +00:00
|
|
|
gst_base_audio_src_base_init (gpointer g_class)
|
2005-07-06 15:27:17 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-07-10 12:03:58 +00:00
|
|
|
gst_base_audio_src_class_init (GstBaseAudioSrcClass * klass)
|
2005-07-06 15:27:17 +00:00
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstElementClass *gstelement_class;
|
|
|
|
GstBaseSrcClass *gstbasesrc_class;
|
|
|
|
GstPushSrcClass *gstpushsrc_class;
|
|
|
|
|
|
|
|
gobject_class = (GObjectClass *) klass;
|
|
|
|
gstelement_class = (GstElementClass *) klass;
|
|
|
|
gstbasesrc_class = (GstBaseSrcClass *) klass;
|
|
|
|
gstpushsrc_class = (GstPushSrcClass *) klass;
|
|
|
|
|
|
|
|
gobject_class->set_property =
|
2005-07-10 12:03:58 +00:00
|
|
|
GST_DEBUG_FUNCPTR (gst_base_audio_src_set_property);
|
2005-07-06 15:27:17 +00:00
|
|
|
gobject_class->get_property =
|
2005-07-10 12:03:58 +00:00
|
|
|
GST_DEBUG_FUNCPTR (gst_base_audio_src_get_property);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
2006-07-06 15:54:50 +00:00
|
|
|
g_object_class_install_property (gobject_class, PROP_BUFFER_TIME,
|
|
|
|
g_param_spec_int64 ("buffer-time", "Buffer Time",
|
|
|
|
"Size of audio buffer in microseconds", 1,
|
2005-12-20 12:00:26 +00:00
|
|
|
G_MAXINT64, DEFAULT_BUFFER_TIME, G_PARAM_READWRITE));
|
2006-07-06 15:54:50 +00:00
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_LATENCY_TIME,
|
|
|
|
g_param_spec_int64 ("latency-time", "Latency Time",
|
|
|
|
"Audio latency in microseconds", 1,
|
2005-12-20 12:00:26 +00:00
|
|
|
G_MAXINT64, DEFAULT_LATENCY_TIME, G_PARAM_READWRITE));
|
2005-07-06 15:27:17 +00:00
|
|
|
|
|
|
|
gstelement_class->change_state =
|
2005-07-10 12:03:58 +00:00
|
|
|
GST_DEBUG_FUNCPTR (gst_base_audio_src_change_state);
|
2005-09-28 13:41:29 +00:00
|
|
|
gstelement_class->provide_clock =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_base_audio_src_provide_clock);
|
2006-07-06 15:54:50 +00:00
|
|
|
gstelement_class->set_clock =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_base_audio_src_set_clock);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
2005-07-10 12:03:58 +00:00
|
|
|
gstbasesrc_class->set_caps = GST_DEBUG_FUNCPTR (gst_base_audio_src_setcaps);
|
|
|
|
gstbasesrc_class->event = GST_DEBUG_FUNCPTR (gst_base_audio_src_event);
|
|
|
|
gstbasesrc_class->get_times =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_base_audio_src_get_times);
|
2006-07-06 15:54:50 +00:00
|
|
|
gstbasesrc_class->create = GST_DEBUG_FUNCPTR (gst_base_audio_src_create);
|
|
|
|
gstbasesrc_class->check_get_range =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_base_audio_src_check_get_range);
|
2005-07-06 15:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-08-28 17:52:45 +00:00
|
|
|
gst_base_audio_src_init (GstBaseAudioSrc * baseaudiosrc,
|
|
|
|
GstBaseAudioSrcClass * g_class)
|
2005-07-06 15:27:17 +00:00
|
|
|
{
|
|
|
|
baseaudiosrc->buffer_time = DEFAULT_BUFFER_TIME;
|
|
|
|
baseaudiosrc->latency_time = DEFAULT_LATENCY_TIME;
|
2006-07-06 15:54:50 +00:00
|
|
|
/* reset blocksize we use latency time to calculate a more useful
|
|
|
|
* value based on negotiated format. */
|
|
|
|
GST_BASE_SRC (baseaudiosrc)->blocksize = 0;
|
2005-07-06 15:27:17 +00:00
|
|
|
|
|
|
|
baseaudiosrc->clock = gst_audio_clock_new ("clock",
|
2005-07-10 12:03:58 +00:00
|
|
|
(GstAudioClockGetTimeFunc) gst_base_audio_src_get_time, baseaudiosrc);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
|
|
|
gst_pad_set_fixatecaps_function (GST_BASE_SRC_PAD (baseaudiosrc),
|
2005-07-10 12:03:58 +00:00
|
|
|
gst_base_audio_src_fixate);
|
2006-01-25 09:27:01 +00:00
|
|
|
|
2006-07-06 15:54:50 +00:00
|
|
|
/* we are always a live source */
|
2006-04-28 14:37:46 +00:00
|
|
|
gst_base_src_set_live (GST_BASE_SRC (baseaudiosrc), TRUE);
|
2006-01-25 09:27:01 +00:00
|
|
|
gst_base_src_set_format (GST_BASE_SRC (baseaudiosrc), GST_FORMAT_TIME);
|
2005-07-06 15:27:17 +00:00
|
|
|
}
|
|
|
|
|
2006-07-06 15:54:50 +00:00
|
|
|
static gboolean
|
|
|
|
gst_base_audio_src_set_clock (GstElement * elem, GstClock * clock)
|
|
|
|
{
|
|
|
|
GstBaseAudioSrc *src;
|
|
|
|
|
|
|
|
src = GST_BASE_AUDIO_SRC (elem);
|
|
|
|
|
|
|
|
/* FIXME, we cannot slave to another clock yet, better fail
|
|
|
|
* than to give a bad user experience (tm). */
|
|
|
|
if (clock && clock != src->clock)
|
|
|
|
goto wrong_clock;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
wrong_clock:
|
|
|
|
{
|
2006-07-12 13:24:19 +00:00
|
|
|
/* no error message, this method is called with the parent
|
|
|
|
* lock helt.. sigh.. long live recursive locks.. */
|
|
|
|
GST_DEBUG_OBJECT (src, "Cannot operate with this clock.");
|
2006-07-06 15:54:50 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-06 15:27:17 +00:00
|
|
|
static GstClock *
|
2005-09-28 13:41:29 +00:00
|
|
|
gst_base_audio_src_provide_clock (GstElement * elem)
|
2005-07-06 15:27:17 +00:00
|
|
|
{
|
|
|
|
GstBaseAudioSrc *src;
|
2006-07-06 15:54:50 +00:00
|
|
|
GstClock *clock;
|
2005-07-06 15:27:17 +00:00
|
|
|
|
2005-07-10 12:03:58 +00:00
|
|
|
src = GST_BASE_AUDIO_SRC (elem);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
2006-07-06 15:54:50 +00:00
|
|
|
/* we have no ringbuffer (must be NULL state) */
|
|
|
|
if (src->ringbuffer == NULL)
|
|
|
|
goto wrong_state;
|
|
|
|
|
|
|
|
if (!gst_ring_buffer_is_acquired (src->ringbuffer))
|
|
|
|
goto wrong_state;
|
|
|
|
|
|
|
|
clock = GST_CLOCK_CAST (gst_object_ref (src->clock));
|
|
|
|
|
|
|
|
return clock;
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
wrong_state:
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (src, "ringbuffer not acquired");
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-07-06 15:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstClockTime
|
2005-07-10 12:03:58 +00:00
|
|
|
gst_base_audio_src_get_time (GstClock * clock, GstBaseAudioSrc * src)
|
2005-07-06 15:27:17 +00:00
|
|
|
{
|
|
|
|
guint64 samples;
|
|
|
|
GstClockTime result;
|
|
|
|
|
2006-07-06 15:54:50 +00:00
|
|
|
if (G_UNLIKELY (src->ringbuffer == NULL || src->ringbuffer->spec.rate == 0))
|
|
|
|
return GST_CLOCK_TIME_NONE;
|
2005-07-06 15:27:17 +00:00
|
|
|
|
2005-07-10 12:03:58 +00:00
|
|
|
samples = gst_ring_buffer_samples_done (src->ringbuffer);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
2006-01-25 09:27:01 +00:00
|
|
|
result = gst_util_uint64_scale_int (samples, GST_SECOND,
|
|
|
|
src->ringbuffer->spec.rate);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2006-07-06 15:54:50 +00:00
|
|
|
static gboolean
|
|
|
|
gst_base_audio_src_check_get_range (GstBaseSrc * bsrc)
|
|
|
|
{
|
|
|
|
/* we allow limited pull base operation of which the details
|
|
|
|
* will eventually exposed in an as of yet non-existing query.
|
|
|
|
* Basically pulling can be done on any number of bytes as long
|
|
|
|
* as the offset is -1 or sequentially increasing. */
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2005-07-06 15:27:17 +00:00
|
|
|
static void
|
2005-07-10 12:03:58 +00:00
|
|
|
gst_base_audio_src_set_property (GObject * object, guint prop_id,
|
2005-07-06 15:27:17 +00:00
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstBaseAudioSrc *src;
|
|
|
|
|
2005-07-10 12:03:58 +00:00
|
|
|
src = GST_BASE_AUDIO_SRC (object);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_BUFFER_TIME:
|
|
|
|
src->buffer_time = g_value_get_int64 (value);
|
|
|
|
break;
|
|
|
|
case PROP_LATENCY_TIME:
|
|
|
|
src->latency_time = g_value_get_int64 (value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-07-10 12:03:58 +00:00
|
|
|
gst_base_audio_src_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec)
|
2005-07-06 15:27:17 +00:00
|
|
|
{
|
|
|
|
GstBaseAudioSrc *src;
|
|
|
|
|
2005-07-10 12:03:58 +00:00
|
|
|
src = GST_BASE_AUDIO_SRC (object);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_BUFFER_TIME:
|
|
|
|
g_value_set_int64 (value, src->buffer_time);
|
|
|
|
break;
|
|
|
|
case PROP_LATENCY_TIME:
|
|
|
|
g_value_set_int64 (value, src->latency_time);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-07-10 12:03:58 +00:00
|
|
|
gst_base_audio_src_fixate (GstPad * pad, GstCaps * caps)
|
2005-07-06 15:27:17 +00:00
|
|
|
{
|
|
|
|
GstStructure *s;
|
|
|
|
|
|
|
|
s = gst_caps_get_structure (caps, 0);
|
|
|
|
|
2005-11-21 14:29:53 +00:00
|
|
|
gst_structure_fixate_field_nearest_int (s, "rate", 44100);
|
|
|
|
gst_structure_fixate_field_nearest_int (s, "channels", 2);
|
|
|
|
gst_structure_fixate_field_nearest_int (s, "depth", 16);
|
|
|
|
gst_structure_fixate_field_nearest_int (s, "width", 16);
|
2005-07-06 15:27:17 +00:00
|
|
|
gst_structure_set (s, "signed", G_TYPE_BOOLEAN, TRUE, NULL);
|
2005-08-23 13:29:17 +00:00
|
|
|
if (gst_structure_has_field (s, "endianness"))
|
2005-11-21 14:29:53 +00:00
|
|
|
gst_structure_fixate_field_nearest_int (s, "endianness", G_BYTE_ORDER);
|
2005-07-06 15:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2005-07-10 12:03:58 +00:00
|
|
|
gst_base_audio_src_setcaps (GstBaseSrc * bsrc, GstCaps * caps)
|
2005-07-06 15:27:17 +00:00
|
|
|
{
|
2005-07-10 12:03:58 +00:00
|
|
|
GstBaseAudioSrc *src = GST_BASE_AUDIO_SRC (bsrc);
|
2005-07-06 15:27:17 +00:00
|
|
|
GstRingBufferSpec *spec;
|
|
|
|
|
|
|
|
spec = &src->ringbuffer->spec;
|
|
|
|
|
|
|
|
spec->buffer_time = src->buffer_time;
|
|
|
|
spec->latency_time = src->latency_time;
|
|
|
|
|
2005-07-10 12:03:58 +00:00
|
|
|
if (!gst_ring_buffer_parse_caps (spec, caps))
|
2005-07-06 15:27:17 +00:00
|
|
|
goto parse_error;
|
|
|
|
|
|
|
|
/* calculate suggested segsize and segtotal */
|
|
|
|
spec->segsize =
|
|
|
|
spec->rate * spec->bytes_per_sample * spec->latency_time / GST_MSECOND;
|
|
|
|
spec->segtotal = spec->buffer_time / spec->latency_time;
|
|
|
|
|
|
|
|
GST_DEBUG ("release old ringbuffer");
|
|
|
|
|
2005-07-10 12:03:58 +00:00
|
|
|
gst_ring_buffer_release (src->ringbuffer);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
2005-07-10 12:03:58 +00:00
|
|
|
gst_ring_buffer_debug_spec_buff (spec);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
|
|
|
GST_DEBUG ("acquire new ringbuffer");
|
|
|
|
|
2005-07-10 12:03:58 +00:00
|
|
|
if (!gst_ring_buffer_acquire (src->ringbuffer, spec))
|
2005-07-06 15:27:17 +00:00
|
|
|
goto acquire_error;
|
|
|
|
|
|
|
|
/* calculate actual latency and buffer times */
|
|
|
|
spec->latency_time =
|
|
|
|
spec->segsize * GST_MSECOND / (spec->rate * spec->bytes_per_sample);
|
|
|
|
spec->buffer_time =
|
|
|
|
spec->segtotal * spec->segsize * GST_MSECOND / (spec->rate *
|
|
|
|
spec->bytes_per_sample);
|
|
|
|
|
2005-07-10 12:03:58 +00:00
|
|
|
gst_ring_buffer_debug_spec_buff (spec);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
parse_error:
|
|
|
|
{
|
|
|
|
GST_DEBUG ("could not parse caps");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
acquire_error:
|
|
|
|
{
|
|
|
|
GST_DEBUG ("could not acquire ringbuffer");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-07-10 12:03:58 +00:00
|
|
|
gst_base_audio_src_get_times (GstBaseSrc * bsrc, GstBuffer * buffer,
|
2005-07-06 15:27:17 +00:00
|
|
|
GstClockTime * start, GstClockTime * end)
|
|
|
|
{
|
|
|
|
/* ne need to sync to a clock here, we schedule the samples based
|
|
|
|
* on our own clock for the moment. FIXME, implement this when
|
|
|
|
* we are not using our own clock */
|
|
|
|
*start = GST_CLOCK_TIME_NONE;
|
|
|
|
*end = GST_CLOCK_TIME_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2005-07-10 12:03:58 +00:00
|
|
|
gst_base_audio_src_event (GstBaseSrc * bsrc, GstEvent * event)
|
2005-07-06 15:27:17 +00:00
|
|
|
{
|
2005-07-10 12:03:58 +00:00
|
|
|
GstBaseAudioSrc *src = GST_BASE_AUDIO_SRC (bsrc);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
2005-07-27 19:13:27 +00:00
|
|
|
case GST_EVENT_FLUSH_START:
|
|
|
|
gst_ring_buffer_pause (src->ringbuffer);
|
2005-10-06 15:15:04 +00:00
|
|
|
gst_ring_buffer_clear_all (src->ringbuffer);
|
2005-07-27 19:13:27 +00:00
|
|
|
break;
|
|
|
|
case GST_EVENT_FLUSH_STOP:
|
2005-10-06 15:15:04 +00:00
|
|
|
/* always resync on sample after a flush */
|
|
|
|
src->next_sample = -1;
|
|
|
|
gst_ring_buffer_clear_all (src->ringbuffer);
|
2005-07-06 15:27:17 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
2006-07-06 15:54:50 +00:00
|
|
|
gst_base_audio_src_create (GstBaseSrc * bsrc, guint64 offset, guint length,
|
|
|
|
GstBuffer ** outbuf)
|
2005-07-06 15:27:17 +00:00
|
|
|
{
|
2006-07-06 15:54:50 +00:00
|
|
|
GstBaseAudioSrc *src = GST_BASE_AUDIO_SRC (bsrc);
|
2005-07-06 15:27:17 +00:00
|
|
|
GstBuffer *buf;
|
|
|
|
guchar *data;
|
2006-07-06 15:54:50 +00:00
|
|
|
guint samples;
|
2005-07-06 15:27:17 +00:00
|
|
|
guint res;
|
2005-10-06 15:15:04 +00:00
|
|
|
guint64 sample;
|
2006-07-06 15:54:50 +00:00
|
|
|
gint bps;
|
2006-01-25 09:27:01 +00:00
|
|
|
GstRingBuffer *ringbuffer;
|
|
|
|
|
|
|
|
ringbuffer = src->ringbuffer;
|
2005-07-06 15:27:17 +00:00
|
|
|
|
2006-07-06 15:54:50 +00:00
|
|
|
if (G_UNLIKELY (!gst_ring_buffer_is_acquired (ringbuffer)))
|
2005-07-06 15:27:17 +00:00
|
|
|
goto wrong_state;
|
|
|
|
|
2006-07-06 15:54:50 +00:00
|
|
|
bps = ringbuffer->spec.bytes_per_sample;
|
2005-07-06 15:27:17 +00:00
|
|
|
|
2006-07-06 15:54:50 +00:00
|
|
|
if ((length == 0 && bsrc->blocksize == 0) || length == -1)
|
|
|
|
/* no length given, use the default segment size */
|
|
|
|
length = ringbuffer->spec.segsize;
|
|
|
|
else
|
|
|
|
/* make sure we round down to an integral number of samples */
|
|
|
|
length -= length % bps;
|
2005-07-06 15:27:17 +00:00
|
|
|
|
2006-07-06 15:54:50 +00:00
|
|
|
/* calculate the sequentially next sample we need to read */
|
|
|
|
sample = (src->next_sample != -1 ? src->next_sample : 0);
|
|
|
|
|
|
|
|
if (G_UNLIKELY (offset != -1)) {
|
|
|
|
/* if a specific offset was given it must be the next
|
|
|
|
* sequential offset we expect or we fail. */
|
|
|
|
if (offset / bps != sample)
|
|
|
|
goto wrong_offset;
|
2005-10-06 15:15:04 +00:00
|
|
|
}
|
|
|
|
|
2006-07-06 15:54:50 +00:00
|
|
|
/* get the number of samples to read */
|
|
|
|
samples = length / bps;
|
|
|
|
|
|
|
|
/* FIXME, using a bufferpool would be nice here */
|
|
|
|
buf = gst_buffer_new_and_alloc (length);
|
|
|
|
data = GST_BUFFER_DATA (buf);
|
2005-10-11 18:32:01 +00:00
|
|
|
|
2006-01-25 09:27:01 +00:00
|
|
|
res = gst_ring_buffer_read (ringbuffer, sample, data, samples);
|
2006-07-06 15:54:50 +00:00
|
|
|
if (G_UNLIKELY (res == -1))
|
2005-07-06 15:27:17 +00:00
|
|
|
goto stopped;
|
|
|
|
|
2006-07-06 15:54:50 +00:00
|
|
|
/* FIXME, we timestamp against our own clock, also handle the case
|
|
|
|
* where we are slaved to another clock. We currently refuse to accept
|
|
|
|
* any other clock than the one we provide, so this code is fine for
|
|
|
|
* now. */
|
2006-01-25 09:27:01 +00:00
|
|
|
GST_BUFFER_TIMESTAMP (buf) = gst_util_uint64_scale_int (sample,
|
|
|
|
GST_SECOND, ringbuffer->spec.rate);
|
2005-10-11 18:32:01 +00:00
|
|
|
src->next_sample = sample + samples;
|
2006-01-25 09:27:01 +00:00
|
|
|
GST_BUFFER_DURATION (buf) = gst_util_uint64_scale_int (src->next_sample,
|
|
|
|
GST_SECOND, ringbuffer->spec.rate) - GST_BUFFER_TIMESTAMP (buf);
|
2005-10-06 15:15:04 +00:00
|
|
|
|
2006-07-06 15:54:50 +00:00
|
|
|
gst_buffer_set_caps (buf, GST_PAD_CAPS (GST_BASE_SRC_PAD (bsrc)));
|
2005-07-06 15:27:17 +00:00
|
|
|
|
|
|
|
*outbuf = buf;
|
|
|
|
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
|
2006-07-06 15:54:50 +00:00
|
|
|
/* ERRORS */
|
2005-07-06 15:27:17 +00:00
|
|
|
wrong_state:
|
|
|
|
{
|
2006-07-06 15:54:50 +00:00
|
|
|
GST_DEBUG_OBJECT (src, "ringbuffer in wrong state");
|
2005-07-06 15:27:17 +00:00
|
|
|
return GST_FLOW_WRONG_STATE;
|
|
|
|
}
|
2006-07-06 15:54:50 +00:00
|
|
|
wrong_offset:
|
|
|
|
{
|
|
|
|
GST_ELEMENT_ERROR (src, RESOURCE, SEEK,
|
|
|
|
(NULL), ("resource can only be operated on sequentially but offset %"
|
|
|
|
G_GUINT64_FORMAT " was given", offset));
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
2005-07-06 15:27:17 +00:00
|
|
|
stopped:
|
|
|
|
{
|
|
|
|
gst_buffer_unref (buf);
|
2006-07-06 15:54:50 +00:00
|
|
|
GST_DEBUG_OBJECT (src, "ringbuffer stopped");
|
2005-07-06 15:27:17 +00:00
|
|
|
return GST_FLOW_WRONG_STATE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GstRingBuffer *
|
2005-07-10 12:03:58 +00:00
|
|
|
gst_base_audio_src_create_ringbuffer (GstBaseAudioSrc * src)
|
2005-07-06 15:27:17 +00:00
|
|
|
{
|
|
|
|
GstBaseAudioSrcClass *bclass;
|
|
|
|
GstRingBuffer *buffer = NULL;
|
|
|
|
|
2005-07-10 12:03:58 +00:00
|
|
|
bclass = GST_BASE_AUDIO_SRC_GET_CLASS (src);
|
2005-07-06 15:27:17 +00:00
|
|
|
if (bclass->create_ringbuffer)
|
|
|
|
buffer = bclass->create_ringbuffer (src);
|
|
|
|
|
2006-07-06 15:54:50 +00:00
|
|
|
if (G_LIKELY (buffer))
|
|
|
|
gst_object_set_parent (GST_OBJECT_CAST (buffer), GST_OBJECT_CAST (src));
|
2005-07-06 15:27:17 +00:00
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-07-10 12:03:58 +00:00
|
|
|
gst_base_audio_src_callback (GstRingBuffer * rbuf, guint8 * data, guint len,
|
2005-07-06 15:27:17 +00:00
|
|
|
gpointer user_data)
|
|
|
|
{
|
2005-07-10 12:03:58 +00:00
|
|
|
//GstBaseAudioSrc *src = GST_BASE_AUDIO_SRC (data);
|
2005-07-06 15:27:17 +00:00
|
|
|
}
|
|
|
|
|
2005-09-02 15:43:18 +00:00
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_base_audio_src_change_state (GstElement * element,
|
|
|
|
GstStateChange transition)
|
2005-07-06 15:27:17 +00:00
|
|
|
{
|
2005-09-02 15:43:18 +00:00
|
|
|
GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
|
2005-07-10 12:03:58 +00:00
|
|
|
GstBaseAudioSrc *src = GST_BASE_AUDIO_SRC (element);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
|
|
|
switch (transition) {
|
2005-09-02 15:43:18 +00:00
|
|
|
case GST_STATE_CHANGE_NULL_TO_READY:
|
2005-08-16 15:53:59 +00:00
|
|
|
if (src->ringbuffer == NULL) {
|
|
|
|
src->ringbuffer = gst_base_audio_src_create_ringbuffer (src);
|
|
|
|
gst_ring_buffer_set_callback (src->ringbuffer,
|
|
|
|
gst_base_audio_src_callback, src);
|
|
|
|
}
|
|
|
|
if (!gst_ring_buffer_open_device (src->ringbuffer))
|
2005-09-02 15:43:18 +00:00
|
|
|
return GST_STATE_CHANGE_FAILURE;
|
2005-10-06 15:15:04 +00:00
|
|
|
src->next_sample = 0;
|
2005-07-06 15:27:17 +00:00
|
|
|
break;
|
2005-09-02 15:43:18 +00:00
|
|
|
case GST_STATE_CHANGE_READY_TO_PAUSED:
|
2005-11-17 14:40:12 +00:00
|
|
|
gst_ring_buffer_set_flushing (src->ringbuffer, FALSE);
|
2005-07-06 15:27:17 +00:00
|
|
|
break;
|
2005-09-02 15:43:18 +00:00
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
|
2006-03-23 16:58:03 +00:00
|
|
|
gst_ring_buffer_may_start (src->ringbuffer, TRUE);
|
2005-07-06 15:27:17 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-09-02 15:43:18 +00:00
|
|
|
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
|
|
|
switch (transition) {
|
2005-09-02 15:43:18 +00:00
|
|
|
case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
|
2006-03-23 16:58:03 +00:00
|
|
|
gst_ring_buffer_may_start (src->ringbuffer, FALSE);
|
2005-07-10 12:03:58 +00:00
|
|
|
gst_ring_buffer_pause (src->ringbuffer);
|
2005-07-06 15:27:17 +00:00
|
|
|
break;
|
2005-09-02 15:43:18 +00:00
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_READY:
|
2005-11-17 14:40:12 +00:00
|
|
|
gst_ring_buffer_set_flushing (src->ringbuffer, TRUE);
|
2005-07-10 12:03:58 +00:00
|
|
|
gst_ring_buffer_release (src->ringbuffer);
|
2005-10-06 15:15:04 +00:00
|
|
|
src->next_sample = 0;
|
2005-07-06 15:27:17 +00:00
|
|
|
break;
|
2005-09-02 15:43:18 +00:00
|
|
|
case GST_STATE_CHANGE_READY_TO_NULL:
|
2005-08-16 15:53:59 +00:00
|
|
|
gst_ring_buffer_close_device (src->ringbuffer);
|
2006-03-23 16:29:58 +00:00
|
|
|
gst_object_unparent (GST_OBJECT_CAST (src->ringbuffer));
|
2005-08-16 15:53:59 +00:00
|
|
|
src->ringbuffer = NULL;
|
2005-07-06 15:27:17 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|