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>
|
|
|
|
*
|
|
|
|
* gstaudiosrc.c: simple audio src base class
|
|
|
|
*
|
|
|
|
* 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-07-06 15:27:17 +00:00
|
|
|
*/
|
|
|
|
|
2006-09-27 11:05:08 +00:00
|
|
|
/**
|
|
|
|
* SECTION:gstaudiosrc
|
|
|
|
* @short_description: Simple base class for audio sources
|
2011-11-11 10:52:47 +00:00
|
|
|
* @see_also: #GstAudioBaseSrc, #GstAudioRingBuffer, #GstAudioSrc.
|
2006-09-27 11:05:08 +00:00
|
|
|
*
|
|
|
|
* This is the most simple base class for audio sources that only requires
|
|
|
|
* subclasses to implement a set of simple functions:
|
|
|
|
*
|
|
|
|
* <variablelist>
|
|
|
|
* <varlistentry>
|
|
|
|
* <term>open()</term>
|
|
|
|
* <listitem><para>Open the device.</para></listitem>
|
|
|
|
* </varlistentry>
|
|
|
|
* <varlistentry>
|
|
|
|
* <term>prepare()</term>
|
|
|
|
* <listitem><para>Configure the device with the specified format.</para></listitem>
|
|
|
|
* </varlistentry>
|
|
|
|
* <varlistentry>
|
|
|
|
* <term>read()</term>
|
|
|
|
* <listitem><para>Read samples from the device.</para></listitem>
|
|
|
|
* </varlistentry>
|
|
|
|
* <varlistentry>
|
|
|
|
* <term>reset()</term>
|
|
|
|
* <listitem><para>Unblock reads and flush the device.</para></listitem>
|
|
|
|
* </varlistentry>
|
|
|
|
* <varlistentry>
|
|
|
|
* <term>delay()</term>
|
|
|
|
* <listitem><para>Get the number of samples in the device but not yet read.
|
|
|
|
* </para></listitem>
|
|
|
|
* </varlistentry>
|
|
|
|
* <varlistentry>
|
|
|
|
* <term>unprepare()</term>
|
|
|
|
* <listitem><para>Undo operations done by prepare.</para></listitem>
|
|
|
|
* </varlistentry>
|
|
|
|
* <varlistentry>
|
|
|
|
* <term>close()</term>
|
|
|
|
* <listitem><para>Close the device.</para></listitem>
|
|
|
|
* </varlistentry>
|
|
|
|
* </variablelist>
|
|
|
|
*
|
|
|
|
* All scheduling of samples and timestamps is done in this base class
|
2011-11-11 10:52:47 +00:00
|
|
|
* together with #GstAudioBaseSrc using a default implementation of a
|
2011-11-11 10:21:41 +00:00
|
|
|
* #GstAudioRingBuffer that uses threads.
|
2006-09-27 11:05:08 +00:00
|
|
|
*/
|
|
|
|
|
2005-07-06 15:27:17 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2012-12-12 17:13:10 +00:00
|
|
|
#include <gst/audio/audio.h>
|
2005-07-06 15:27:17 +00:00
|
|
|
#include "gstaudiosrc.h"
|
|
|
|
|
2005-07-10 12:03:58 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_audio_src_debug);
|
|
|
|
#define GST_CAT_DEFAULT gst_audio_src_debug
|
2005-07-06 15:27:17 +00:00
|
|
|
|
2011-11-11 09:54:39 +00:00
|
|
|
#define GST_TYPE_AUDIO_SRC_RING_BUFFER \
|
|
|
|
(gst_audio_src_ring_buffer_get_type())
|
|
|
|
#define GST_AUDIO_SRC_RING_BUFFER(obj) \
|
|
|
|
(G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_AUDIO_SRC_RING_BUFFER,GstAudioSrcRingBuffer))
|
|
|
|
#define GST_AUDIO_SRC_RING_BUFFER_CLASS(klass) \
|
|
|
|
(G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_AUDIO_SRC_RING_BUFFER,GstAudioSrcRingBufferClass))
|
|
|
|
#define GST_AUDIO_SRC_RING_BUFFER_GET_CLASS(obj) \
|
|
|
|
(G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_AUDIO_SRC_RING_BUFFER, GstAudioSrcRingBufferClass))
|
|
|
|
#define GST_IS_AUDIO_SRC_RING_BUFFER(obj) \
|
|
|
|
(G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_AUDIO_SRC_RING_BUFFER))
|
|
|
|
#define GST_IS_AUDIO_SRC_RING_BUFFER_CLASS(klass)\
|
|
|
|
(G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_AUDIO_SRC_RING_BUFFER))
|
|
|
|
|
|
|
|
typedef struct _GstAudioSrcRingBuffer GstAudioSrcRingBuffer;
|
|
|
|
typedef struct _GstAudioSrcRingBufferClass GstAudioSrcRingBufferClass;
|
|
|
|
|
2012-09-09 23:34:47 +00:00
|
|
|
#define GST_AUDIO_SRC_RING_BUFFER_GET_COND(buf) (&(((GstAudioSrcRingBuffer *)buf)->cond))
|
2011-11-11 09:54:39 +00:00
|
|
|
#define GST_AUDIO_SRC_RING_BUFFER_WAIT(buf) (g_cond_wait (GST_AUDIO_SRC_RING_BUFFER_GET_COND (buf), GST_OBJECT_GET_LOCK (buf)))
|
|
|
|
#define GST_AUDIO_SRC_RING_BUFFER_SIGNAL(buf) (g_cond_signal (GST_AUDIO_SRC_RING_BUFFER_GET_COND (buf)))
|
|
|
|
#define GST_AUDIO_SRC_RING_BUFFER_BROADCAST(buf)(g_cond_broadcast (GST_AUDIO_SRC_RING_BUFFER_GET_COND (buf)))
|
|
|
|
|
|
|
|
struct _GstAudioSrcRingBuffer
|
2005-07-06 15:27:17 +00:00
|
|
|
{
|
2011-11-11 10:21:41 +00:00
|
|
|
GstAudioRingBuffer object;
|
2005-07-06 15:27:17 +00:00
|
|
|
|
|
|
|
gboolean running;
|
|
|
|
gint queuedseg;
|
|
|
|
|
2012-09-09 23:34:47 +00:00
|
|
|
GCond cond;
|
2005-07-06 15:27:17 +00:00
|
|
|
};
|
|
|
|
|
2011-11-11 09:54:39 +00:00
|
|
|
struct _GstAudioSrcRingBufferClass
|
2005-07-06 15:27:17 +00:00
|
|
|
{
|
2011-11-11 10:21:41 +00:00
|
|
|
GstAudioRingBufferClass parent_class;
|
2005-07-06 15:27:17 +00:00
|
|
|
};
|
|
|
|
|
2011-11-11 09:54:39 +00:00
|
|
|
static void gst_audio_src_ring_buffer_class_init (GstAudioSrcRingBufferClass *
|
|
|
|
klass);
|
|
|
|
static void gst_audio_src_ring_buffer_init (GstAudioSrcRingBuffer * ringbuffer,
|
|
|
|
GstAudioSrcRingBufferClass * klass);
|
|
|
|
static void gst_audio_src_ring_buffer_dispose (GObject * object);
|
|
|
|
static void gst_audio_src_ring_buffer_finalize (GObject * object);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
2011-11-11 10:21:41 +00:00
|
|
|
static GstAudioRingBufferClass *ring_parent_class = NULL;
|
2005-07-06 15:27:17 +00:00
|
|
|
|
2011-11-11 10:21:41 +00:00
|
|
|
static gboolean gst_audio_src_ring_buffer_open_device (GstAudioRingBuffer *
|
|
|
|
buf);
|
|
|
|
static gboolean gst_audio_src_ring_buffer_close_device (GstAudioRingBuffer *
|
|
|
|
buf);
|
|
|
|
static gboolean gst_audio_src_ring_buffer_acquire (GstAudioRingBuffer * buf,
|
|
|
|
GstAudioRingBufferSpec * spec);
|
|
|
|
static gboolean gst_audio_src_ring_buffer_release (GstAudioRingBuffer * buf);
|
|
|
|
static gboolean gst_audio_src_ring_buffer_start (GstAudioRingBuffer * buf);
|
|
|
|
static gboolean gst_audio_src_ring_buffer_stop (GstAudioRingBuffer * buf);
|
|
|
|
static guint gst_audio_src_ring_buffer_delay (GstAudioRingBuffer * buf);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
|
|
|
/* ringbuffer abstract base class */
|
|
|
|
static GType
|
2011-11-11 09:54:39 +00:00
|
|
|
gst_audio_src_ring_buffer_get_type (void)
|
2005-07-06 15:27:17 +00:00
|
|
|
{
|
|
|
|
static GType ringbuffer_type = 0;
|
|
|
|
|
|
|
|
if (!ringbuffer_type) {
|
|
|
|
static const GTypeInfo ringbuffer_info = {
|
2011-11-11 09:54:39 +00:00
|
|
|
sizeof (GstAudioSrcRingBufferClass),
|
2005-07-06 15:27:17 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
2011-11-11 09:54:39 +00:00
|
|
|
(GClassInitFunc) gst_audio_src_ring_buffer_class_init,
|
2005-07-06 15:27:17 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
2011-11-11 09:54:39 +00:00
|
|
|
sizeof (GstAudioSrcRingBuffer),
|
2005-07-06 15:27:17 +00:00
|
|
|
0,
|
2011-11-11 09:54:39 +00:00
|
|
|
(GInstanceInitFunc) gst_audio_src_ring_buffer_init,
|
2005-07-06 15:27:17 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
ringbuffer_type =
|
2011-11-11 10:21:41 +00:00
|
|
|
g_type_register_static (GST_TYPE_AUDIO_RING_BUFFER,
|
|
|
|
"GstAudioSrcRingBuffer", &ringbuffer_info, 0);
|
2005-07-06 15:27:17 +00:00
|
|
|
}
|
|
|
|
return ringbuffer_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-11-11 09:54:39 +00:00
|
|
|
gst_audio_src_ring_buffer_class_init (GstAudioSrcRingBufferClass * klass)
|
2005-07-06 15:27:17 +00:00
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
2011-11-11 10:21:41 +00:00
|
|
|
GstAudioRingBufferClass *gstringbuffer_class;
|
2005-07-06 15:27:17 +00:00
|
|
|
|
|
|
|
gobject_class = (GObjectClass *) klass;
|
2011-11-11 10:21:41 +00:00
|
|
|
gstringbuffer_class = (GstAudioRingBufferClass *) klass;
|
2005-07-06 15:27:17 +00:00
|
|
|
|
2006-04-08 21:02:53 +00:00
|
|
|
ring_parent_class = g_type_class_peek_parent (klass);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
2011-11-11 09:54:39 +00:00
|
|
|
gobject_class->dispose = gst_audio_src_ring_buffer_dispose;
|
|
|
|
gobject_class->finalize = gst_audio_src_ring_buffer_finalize;
|
2005-07-06 15:27:17 +00:00
|
|
|
|
2005-08-22 15:11:31 +00:00
|
|
|
gstringbuffer_class->open_device =
|
2011-11-11 09:54:39 +00:00
|
|
|
GST_DEBUG_FUNCPTR (gst_audio_src_ring_buffer_open_device);
|
2005-08-22 15:11:31 +00:00
|
|
|
gstringbuffer_class->close_device =
|
2011-11-11 09:54:39 +00:00
|
|
|
GST_DEBUG_FUNCPTR (gst_audio_src_ring_buffer_close_device);
|
2005-07-06 15:27:17 +00:00
|
|
|
gstringbuffer_class->acquire =
|
2011-11-11 09:54:39 +00:00
|
|
|
GST_DEBUG_FUNCPTR (gst_audio_src_ring_buffer_acquire);
|
2005-07-06 15:27:17 +00:00
|
|
|
gstringbuffer_class->release =
|
2011-11-11 09:54:39 +00:00
|
|
|
GST_DEBUG_FUNCPTR (gst_audio_src_ring_buffer_release);
|
|
|
|
gstringbuffer_class->start =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_audio_src_ring_buffer_start);
|
|
|
|
gstringbuffer_class->resume =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_audio_src_ring_buffer_start);
|
|
|
|
gstringbuffer_class->stop =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_audio_src_ring_buffer_stop);
|
|
|
|
|
|
|
|
gstringbuffer_class->delay =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_audio_src_ring_buffer_delay);
|
2005-07-06 15:27:17 +00:00
|
|
|
}
|
|
|
|
|
2012-09-10 09:26:38 +00:00
|
|
|
typedef guint (*ReadFunc)
|
|
|
|
(GstAudioSrc * src, gpointer data, guint length, GstClockTime * timestamp);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
|
|
|
/* this internal thread does nothing else but read samples from the audio device.
|
|
|
|
* It will read each segment in the ringbuffer and will update the play
|
2013-12-20 21:54:39 +00:00
|
|
|
* pointer.
|
2005-07-06 15:27:17 +00:00
|
|
|
* The start/stop methods control the thread.
|
|
|
|
*/
|
|
|
|
static void
|
2011-11-11 10:21:41 +00:00
|
|
|
audioringbuffer_thread_func (GstAudioRingBuffer * buf)
|
2005-07-06 15:27:17 +00:00
|
|
|
{
|
|
|
|
GstAudioSrc *src;
|
|
|
|
GstAudioSrcClass *csrc;
|
2011-11-11 09:54:39 +00:00
|
|
|
GstAudioSrcRingBuffer *abuf = GST_AUDIO_SRC_RING_BUFFER (buf);
|
2005-07-06 15:27:17 +00:00
|
|
|
ReadFunc readfunc;
|
2009-07-03 08:41:05 +00:00
|
|
|
GstMessage *message;
|
|
|
|
GValue val = { 0 };
|
2005-07-06 15:27:17 +00:00
|
|
|
|
2005-07-10 12:03:58 +00:00
|
|
|
src = GST_AUDIO_SRC (GST_OBJECT_PARENT (buf));
|
|
|
|
csrc = GST_AUDIO_SRC_GET_CLASS (src);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
2007-03-01 17:01:43 +00:00
|
|
|
GST_DEBUG_OBJECT (src, "enter thread");
|
2005-07-06 15:27:17 +00:00
|
|
|
|
2012-09-10 09:26:38 +00:00
|
|
|
if ((readfunc = csrc->read) == NULL)
|
2005-07-06 15:27:17 +00:00
|
|
|
goto no_function;
|
|
|
|
|
2009-07-03 08:41:05 +00:00
|
|
|
message = gst_message_new_stream_status (GST_OBJECT_CAST (buf),
|
|
|
|
GST_STREAM_STATUS_TYPE_ENTER, GST_ELEMENT_CAST (src));
|
2012-11-12 10:45:15 +00:00
|
|
|
g_value_init (&val, GST_TYPE_G_THREAD);
|
|
|
|
g_value_set_boxed (&val, src->thread);
|
2009-07-03 08:41:05 +00:00
|
|
|
gst_message_set_stream_status_object (message, &val);
|
2012-11-12 10:45:15 +00:00
|
|
|
g_value_unset (&val);
|
2009-07-03 08:41:05 +00:00
|
|
|
GST_DEBUG_OBJECT (src, "posting ENTER stream status");
|
|
|
|
gst_element_post_message (GST_ELEMENT_CAST (src), message);
|
|
|
|
|
2005-07-06 15:27:17 +00:00
|
|
|
while (TRUE) {
|
|
|
|
gint left, len;
|
|
|
|
guint8 *readptr;
|
|
|
|
gint readseg;
|
2012-09-10 09:26:38 +00:00
|
|
|
GstClockTime timestamp = GST_CLOCK_TIME_NONE;
|
2005-07-06 15:27:17 +00:00
|
|
|
|
2011-11-11 10:21:41 +00:00
|
|
|
if (gst_audio_ring_buffer_prepare_read (buf, &readseg, &readptr, &len)) {
|
2009-06-17 11:17:30 +00:00
|
|
|
gint read;
|
2005-07-06 15:27:17 +00:00
|
|
|
|
|
|
|
left = len;
|
|
|
|
do {
|
2012-09-10 09:26:38 +00:00
|
|
|
read = readfunc (src, readptr, left, ×tamp);
|
2007-03-01 17:01:43 +00:00
|
|
|
GST_LOG_OBJECT (src, "transfered %d bytes of %d to segment %d", read,
|
|
|
|
left, readseg);
|
2005-07-06 15:27:17 +00:00
|
|
|
if (read < 0 || read > left) {
|
2007-03-01 17:01:43 +00:00
|
|
|
GST_WARNING_OBJECT (src,
|
2009-06-17 11:17:30 +00:00
|
|
|
"error reading data %d (reason: %s), skipping segment", read,
|
2006-11-06 18:24:59 +00:00
|
|
|
g_strerror (errno));
|
2005-07-06 15:27:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
left -= read;
|
2009-06-17 11:17:30 +00:00
|
|
|
readptr += read;
|
2005-07-06 15:27:17 +00:00
|
|
|
} while (left > 0);
|
|
|
|
|
2012-09-10 09:26:38 +00:00
|
|
|
/* Update timestamp on buffer if required */
|
|
|
|
gst_audio_ring_buffer_set_timestamp (buf, readseg, timestamp);
|
|
|
|
|
2005-07-06 15:27:17 +00:00
|
|
|
/* we read one segment */
|
2011-11-11 10:21:41 +00:00
|
|
|
gst_audio_ring_buffer_advance (buf, 1);
|
2005-07-06 15:27:17 +00:00
|
|
|
} else {
|
2005-11-21 16:35:24 +00:00
|
|
|
GST_OBJECT_LOCK (abuf);
|
2005-07-06 15:27:17 +00:00
|
|
|
if (!abuf->running)
|
|
|
|
goto stop_running;
|
2011-11-10 15:00:28 +00:00
|
|
|
if (G_UNLIKELY (g_atomic_int_get (&buf->state) ==
|
2011-11-11 10:21:41 +00:00
|
|
|
GST_AUDIO_RING_BUFFER_STATE_STARTED)) {
|
2011-11-10 15:00:28 +00:00
|
|
|
GST_OBJECT_UNLOCK (abuf);
|
|
|
|
continue;
|
|
|
|
}
|
2007-03-01 17:01:43 +00:00
|
|
|
GST_DEBUG_OBJECT (src, "signal wait");
|
2011-11-11 09:54:39 +00:00
|
|
|
GST_AUDIO_SRC_RING_BUFFER_SIGNAL (buf);
|
2007-03-01 17:01:43 +00:00
|
|
|
GST_DEBUG_OBJECT (src, "wait for action");
|
2011-11-11 09:54:39 +00:00
|
|
|
GST_AUDIO_SRC_RING_BUFFER_WAIT (buf);
|
2007-03-01 17:01:43 +00:00
|
|
|
GST_DEBUG_OBJECT (src, "got signal");
|
2005-07-06 15:27:17 +00:00
|
|
|
if (!abuf->running)
|
|
|
|
goto stop_running;
|
2007-03-01 17:01:43 +00:00
|
|
|
GST_DEBUG_OBJECT (src, "continue running");
|
2005-11-21 16:35:24 +00:00
|
|
|
GST_OBJECT_UNLOCK (abuf);
|
2005-07-06 15:27:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-04 15:02:20 +00:00
|
|
|
/* Will never be reached */
|
2009-07-03 08:41:05 +00:00
|
|
|
g_assert_not_reached ();
|
2005-07-06 15:27:17 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* ERROR */
|
|
|
|
no_function:
|
|
|
|
{
|
|
|
|
GST_DEBUG ("no write function, exit thread");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
stop_running:
|
|
|
|
{
|
2005-11-21 16:35:24 +00:00
|
|
|
GST_OBJECT_UNLOCK (abuf);
|
2005-07-06 15:27:17 +00:00
|
|
|
GST_DEBUG ("stop running, exit thread");
|
2009-07-03 08:41:05 +00:00
|
|
|
message = gst_message_new_stream_status (GST_OBJECT_CAST (buf),
|
|
|
|
GST_STREAM_STATUS_TYPE_LEAVE, GST_ELEMENT_CAST (src));
|
2012-11-12 10:45:15 +00:00
|
|
|
g_value_init (&val, GST_TYPE_G_THREAD);
|
|
|
|
g_value_set_boxed (&val, src->thread);
|
2009-07-03 08:41:05 +00:00
|
|
|
gst_message_set_stream_status_object (message, &val);
|
2012-11-12 10:45:15 +00:00
|
|
|
g_value_unset (&val);
|
2009-07-03 08:41:05 +00:00
|
|
|
GST_DEBUG_OBJECT (src, "posting LEAVE stream status");
|
|
|
|
gst_element_post_message (GST_ELEMENT_CAST (src), message);
|
2005-07-06 15:27:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-11-11 09:54:39 +00:00
|
|
|
gst_audio_src_ring_buffer_init (GstAudioSrcRingBuffer * ringbuffer,
|
|
|
|
GstAudioSrcRingBufferClass * g_class)
|
2005-07-06 15:27:17 +00:00
|
|
|
{
|
|
|
|
ringbuffer->running = FALSE;
|
|
|
|
ringbuffer->queuedseg = 0;
|
|
|
|
|
2012-09-09 23:34:47 +00:00
|
|
|
g_cond_init (&ringbuffer->cond);
|
2005-07-06 15:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-11-11 09:54:39 +00:00
|
|
|
gst_audio_src_ring_buffer_dispose (GObject * object)
|
2005-07-06 15:27:17 +00:00
|
|
|
{
|
2011-11-11 09:54:39 +00:00
|
|
|
GstAudioSrcRingBuffer *ringbuffer = GST_AUDIO_SRC_RING_BUFFER (object);
|
2006-01-10 12:25:59 +00:00
|
|
|
|
2012-09-09 23:34:47 +00:00
|
|
|
g_cond_clear (&ringbuffer->cond);
|
2006-01-10 12:25:59 +00:00
|
|
|
|
2005-07-06 15:27:17 +00:00
|
|
|
G_OBJECT_CLASS (ring_parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-11-11 09:54:39 +00:00
|
|
|
gst_audio_src_ring_buffer_finalize (GObject * object)
|
2005-07-06 15:27:17 +00:00
|
|
|
{
|
|
|
|
G_OBJECT_CLASS (ring_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
2005-08-22 15:11:31 +00:00
|
|
|
static gboolean
|
2011-11-11 10:21:41 +00:00
|
|
|
gst_audio_src_ring_buffer_open_device (GstAudioRingBuffer * buf)
|
2005-08-22 15:11:31 +00:00
|
|
|
{
|
|
|
|
GstAudioSrc *src;
|
|
|
|
GstAudioSrcClass *csrc;
|
|
|
|
gboolean result = TRUE;
|
|
|
|
|
|
|
|
src = GST_AUDIO_SRC (GST_OBJECT_PARENT (buf));
|
|
|
|
csrc = GST_AUDIO_SRC_GET_CLASS (src);
|
|
|
|
|
|
|
|
if (csrc->open)
|
|
|
|
result = csrc->open (src);
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
goto could_not_open;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
could_not_open:
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2011-11-11 10:21:41 +00:00
|
|
|
gst_audio_src_ring_buffer_close_device (GstAudioRingBuffer * buf)
|
2005-08-22 15:11:31 +00:00
|
|
|
{
|
|
|
|
GstAudioSrc *src;
|
|
|
|
GstAudioSrcClass *csrc;
|
|
|
|
gboolean result = TRUE;
|
|
|
|
|
|
|
|
src = GST_AUDIO_SRC (GST_OBJECT_PARENT (buf));
|
|
|
|
csrc = GST_AUDIO_SRC_GET_CLASS (src);
|
|
|
|
|
|
|
|
if (csrc->close)
|
|
|
|
result = csrc->close (src);
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
goto could_not_open;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
could_not_open:
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-06 15:27:17 +00:00
|
|
|
static gboolean
|
2011-11-11 10:21:41 +00:00
|
|
|
gst_audio_src_ring_buffer_acquire (GstAudioRingBuffer * buf,
|
|
|
|
GstAudioRingBufferSpec * spec)
|
2005-07-06 15:27:17 +00:00
|
|
|
{
|
|
|
|
GstAudioSrc *src;
|
|
|
|
GstAudioSrcClass *csrc;
|
2011-11-11 09:54:39 +00:00
|
|
|
GstAudioSrcRingBuffer *abuf;
|
2005-07-06 15:27:17 +00:00
|
|
|
gboolean result = FALSE;
|
|
|
|
|
2005-07-10 12:03:58 +00:00
|
|
|
src = GST_AUDIO_SRC (GST_OBJECT_PARENT (buf));
|
|
|
|
csrc = GST_AUDIO_SRC_GET_CLASS (src);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
2005-08-22 15:11:31 +00:00
|
|
|
if (csrc->prepare)
|
|
|
|
result = csrc->prepare (src, spec);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
|
|
|
if (!result)
|
|
|
|
goto could_not_open;
|
|
|
|
|
2011-03-27 11:55:15 +00:00
|
|
|
buf->size = spec->segtotal * spec->segsize;
|
2015-01-21 08:37:30 +00:00
|
|
|
buf->memory = g_malloc (buf->size);
|
|
|
|
if (buf->spec.type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_RAW) {
|
|
|
|
gst_audio_format_fill_silence (buf->spec.info.finfo, buf->memory,
|
|
|
|
buf->size);
|
|
|
|
} else {
|
|
|
|
/* FIXME, non-raw formats get 0 as the empty sample */
|
|
|
|
memset (buf->memory, 0, buf->size);
|
|
|
|
}
|
2005-07-06 15:27:17 +00:00
|
|
|
|
2011-11-11 09:54:39 +00:00
|
|
|
abuf = GST_AUDIO_SRC_RING_BUFFER (buf);
|
2005-07-06 15:27:17 +00:00
|
|
|
abuf->running = TRUE;
|
|
|
|
|
2011-12-04 16:43:38 +00:00
|
|
|
/* FIXME: handle thread creation failure */
|
|
|
|
src->thread = g_thread_try_new ("audiosrc-ringbuffer",
|
|
|
|
(GThreadFunc) audioringbuffer_thread_func, buf, NULL);
|
|
|
|
|
2012-01-27 17:25:38 +00:00
|
|
|
GST_AUDIO_SRC_RING_BUFFER_WAIT (buf);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
could_not_open:
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* function is called with LOCK */
|
|
|
|
static gboolean
|
2011-11-11 10:21:41 +00:00
|
|
|
gst_audio_src_ring_buffer_release (GstAudioRingBuffer * buf)
|
2005-07-06 15:27:17 +00:00
|
|
|
{
|
|
|
|
GstAudioSrc *src;
|
|
|
|
GstAudioSrcClass *csrc;
|
2011-11-11 09:54:39 +00:00
|
|
|
GstAudioSrcRingBuffer *abuf;
|
2005-07-06 15:27:17 +00:00
|
|
|
gboolean result = FALSE;
|
|
|
|
|
2005-07-10 12:03:58 +00:00
|
|
|
src = GST_AUDIO_SRC (GST_OBJECT_PARENT (buf));
|
|
|
|
csrc = GST_AUDIO_SRC_GET_CLASS (src);
|
2011-11-11 09:54:39 +00:00
|
|
|
abuf = GST_AUDIO_SRC_RING_BUFFER (buf);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
|
|
|
abuf->running = FALSE;
|
2011-11-11 09:54:39 +00:00
|
|
|
GST_AUDIO_SRC_RING_BUFFER_SIGNAL (buf);
|
2005-11-21 16:35:24 +00:00
|
|
|
GST_OBJECT_UNLOCK (buf);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
|
|
|
/* join the thread */
|
|
|
|
g_thread_join (src->thread);
|
|
|
|
|
2005-11-21 16:35:24 +00:00
|
|
|
GST_OBJECT_LOCK (buf);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
|
|
|
/* free the buffer */
|
2011-03-27 11:55:15 +00:00
|
|
|
g_free (buf->memory);
|
|
|
|
buf->memory = NULL;
|
2005-07-06 15:27:17 +00:00
|
|
|
|
2005-08-22 15:11:31 +00:00
|
|
|
if (csrc->unprepare)
|
|
|
|
result = csrc->unprepare (src);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-08-08 16:51:10 +00:00
|
|
|
static gboolean
|
2011-11-11 10:21:41 +00:00
|
|
|
gst_audio_src_ring_buffer_start (GstAudioRingBuffer * buf)
|
2005-07-06 15:27:17 +00:00
|
|
|
{
|
|
|
|
GST_DEBUG ("start, sending signal");
|
2011-11-11 09:54:39 +00:00
|
|
|
GST_AUDIO_SRC_RING_BUFFER_SIGNAL (buf);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2011-11-11 10:21:41 +00:00
|
|
|
gst_audio_src_ring_buffer_stop (GstAudioRingBuffer * buf)
|
2005-07-06 15:27:17 +00:00
|
|
|
{
|
|
|
|
GstAudioSrc *src;
|
|
|
|
GstAudioSrcClass *csrc;
|
|
|
|
|
2005-07-10 12:03:58 +00:00
|
|
|
src = GST_AUDIO_SRC (GST_OBJECT_PARENT (buf));
|
|
|
|
csrc = GST_AUDIO_SRC_GET_CLASS (src);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
|
|
|
/* unblock any pending writes to the audio device */
|
|
|
|
if (csrc->reset) {
|
|
|
|
GST_DEBUG ("reset...");
|
|
|
|
csrc->reset (src);
|
|
|
|
GST_DEBUG ("reset done");
|
|
|
|
}
|
2008-09-04 16:25:06 +00:00
|
|
|
#if 0
|
2005-07-06 15:27:17 +00:00
|
|
|
GST_DEBUG ("stop, waiting...");
|
2011-11-11 09:54:39 +00:00
|
|
|
GST_AUDIO_SRC_RING_BUFFER_WAIT (buf);
|
2005-07-06 15:27:17 +00:00
|
|
|
GST_DEBUG ("stoped");
|
2008-09-04 16:25:06 +00:00
|
|
|
#endif
|
2005-07-06 15:27:17 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static guint
|
2011-11-11 10:21:41 +00:00
|
|
|
gst_audio_src_ring_buffer_delay (GstAudioRingBuffer * buf)
|
2005-07-06 15:27:17 +00:00
|
|
|
{
|
|
|
|
GstAudioSrc *src;
|
|
|
|
GstAudioSrcClass *csrc;
|
|
|
|
guint res = 0;
|
|
|
|
|
2005-07-10 12:03:58 +00:00
|
|
|
src = GST_AUDIO_SRC (GST_OBJECT_PARENT (buf));
|
|
|
|
csrc = GST_AUDIO_SRC_GET_CLASS (src);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
|
|
|
if (csrc->delay)
|
|
|
|
res = csrc->delay (src);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* AudioSrc signals and args */
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
/* FILL ME */
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
ARG_0,
|
|
|
|
};
|
|
|
|
|
2011-04-19 08:52:00 +00:00
|
|
|
#define _do_init \
|
2005-07-10 12:03:58 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_audio_src_debug, "audiosrc", 0, "audiosrc element");
|
2011-04-19 08:52:00 +00:00
|
|
|
#define gst_audio_src_parent_class parent_class
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (GstAudioSrc, gst_audio_src,
|
2011-11-11 10:52:47 +00:00
|
|
|
GST_TYPE_AUDIO_BASE_SRC, _do_init);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
2011-11-11 10:52:47 +00:00
|
|
|
static GstAudioRingBuffer *gst_audio_src_create_ringbuffer (GstAudioBaseSrc *
|
2011-11-11 10:21:41 +00:00
|
|
|
src);
|
2005-07-06 15:27:17 +00:00
|
|
|
|
|
|
|
static void
|
2005-07-10 12:03:58 +00:00
|
|
|
gst_audio_src_class_init (GstAudioSrcClass * klass)
|
2005-07-06 15:27:17 +00:00
|
|
|
{
|
2011-11-11 11:00:52 +00:00
|
|
|
GstAudioBaseSrcClass *gstaudiobasesrc_class;
|
2005-07-06 15:27:17 +00:00
|
|
|
|
2011-11-11 11:00:52 +00:00
|
|
|
gstaudiobasesrc_class = (GstAudioBaseSrcClass *) klass;
|
2005-07-06 15:27:17 +00:00
|
|
|
|
2011-11-11 11:00:52 +00:00
|
|
|
gstaudiobasesrc_class->create_ringbuffer =
|
2005-07-10 12:03:58 +00:00
|
|
|
GST_DEBUG_FUNCPTR (gst_audio_src_create_ringbuffer);
|
2009-06-16 13:05:04 +00:00
|
|
|
|
2011-11-11 09:54:39 +00:00
|
|
|
g_type_class_ref (GST_TYPE_AUDIO_SRC_RING_BUFFER);
|
2005-07-06 15:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-04-19 08:52:00 +00:00
|
|
|
gst_audio_src_init (GstAudioSrc * audiosrc)
|
2005-07-06 15:27:17 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-11-11 10:21:41 +00:00
|
|
|
static GstAudioRingBuffer *
|
2011-11-11 10:52:47 +00:00
|
|
|
gst_audio_src_create_ringbuffer (GstAudioBaseSrc * src)
|
2005-07-06 15:27:17 +00:00
|
|
|
{
|
2011-11-11 10:21:41 +00:00
|
|
|
GstAudioRingBuffer *buffer;
|
2005-07-06 15:27:17 +00:00
|
|
|
|
|
|
|
GST_DEBUG ("creating ringbuffer");
|
2011-11-11 09:54:39 +00:00
|
|
|
buffer = g_object_new (GST_TYPE_AUDIO_SRC_RING_BUFFER, NULL);
|
2005-07-06 15:27:17 +00:00
|
|
|
GST_DEBUG ("created ringbuffer @%p", buffer);
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|