2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2005 Wim Taymans <wim@fluendo.com>
5 * gstaudiosrc.c: simple audio src base class
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
25 * @short_description: Simple base class for audio sources
26 * @see_also: #GstBaseAudioSrc, #GstRingBuffer, #GstAudioSrc.
28 * This is the most simple base class for audio sources that only requires
29 * subclasses to implement a set of simple functions:
34 * <listitem><para>Open the device.</para></listitem>
37 * <term>prepare()</term>
38 * <listitem><para>Configure the device with the specified format.</para></listitem>
42 * <listitem><para>Read samples from the device.</para></listitem>
45 * <term>reset()</term>
46 * <listitem><para>Unblock reads and flush the device.</para></listitem>
49 * <term>delay()</term>
50 * <listitem><para>Get the number of samples in the device but not yet read.
54 * <term>unprepare()</term>
55 * <listitem><para>Undo operations done by prepare.</para></listitem>
58 * <term>close()</term>
59 * <listitem><para>Close the device.</para></listitem>
63 * All scheduling of samples and timestamps is done in this base class
64 * together with #GstBaseAudioSrc using a default implementation of a
65 * #GstRingBuffer that uses threads.
67 * Last reviewed on 2006-09-27 (0.10.12)
72 #include "gstaudiosrc.h"
74 GST_DEBUG_CATEGORY_STATIC (gst_audio_src_debug);
75 #define GST_CAT_DEFAULT gst_audio_src_debug
77 #define GST_TYPE_AUDIORING_BUFFER \
78 (gst_audioringbuffer_get_type())
79 #define GST_AUDIORING_BUFFER(obj) \
80 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_AUDIORING_BUFFER,GstAudioRingBuffer))
81 #define GST_AUDIORING_BUFFER_CLASS(klass) \
82 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_AUDIORING_BUFFER,GstAudioRingBufferClass))
83 #define GST_AUDIORING_BUFFER_GET_CLASS(obj) \
84 (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_AUDIORING_BUFFER, GstAudioRingBufferClass))
85 #define GST_IS_AUDIORING_BUFFER(obj) \
86 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_AUDIORING_BUFFER))
87 #define GST_IS_AUDIORING_BUFFER_CLASS(klass)\
88 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_AUDIORING_BUFFER))
90 typedef struct _GstAudioRingBuffer GstAudioRingBuffer;
91 typedef struct _GstAudioRingBufferClass GstAudioRingBufferClass;
93 #define GST_AUDIORING_BUFFER_GET_COND(buf) (((GstAudioRingBuffer *)buf)->cond)
94 #define GST_AUDIORING_BUFFER_WAIT(buf) (g_cond_wait (GST_AUDIORING_BUFFER_GET_COND (buf), GST_OBJECT_GET_LOCK (buf)))
95 #define GST_AUDIORING_BUFFER_SIGNAL(buf) (g_cond_signal (GST_AUDIORING_BUFFER_GET_COND (buf)))
96 #define GST_AUDIORING_BUFFER_BROADCAST(buf)(g_cond_broadcast (GST_AUDIORING_BUFFER_GET_COND (buf)))
98 struct _GstAudioRingBuffer
100 GstRingBuffer object;
108 struct _GstAudioRingBufferClass
110 GstRingBufferClass parent_class;
113 static void gst_audioringbuffer_class_init (GstAudioRingBufferClass * klass);
114 static void gst_audioringbuffer_init (GstAudioRingBuffer * ringbuffer,
115 GstAudioRingBufferClass * klass);
116 static void gst_audioringbuffer_dispose (GObject * object);
117 static void gst_audioringbuffer_finalize (GObject * object);
119 static GstRingBufferClass *ring_parent_class = NULL;
121 static gboolean gst_audioringbuffer_open_device (GstRingBuffer * buf);
122 static gboolean gst_audioringbuffer_close_device (GstRingBuffer * buf);
123 static gboolean gst_audioringbuffer_acquire (GstRingBuffer * buf,
124 GstRingBufferSpec * spec);
125 static gboolean gst_audioringbuffer_release (GstRingBuffer * buf);
126 static gboolean gst_audioringbuffer_start (GstRingBuffer * buf);
127 static gboolean gst_audioringbuffer_stop (GstRingBuffer * buf);
128 static guint gst_audioringbuffer_delay (GstRingBuffer * buf);
130 /* ringbuffer abstract base class */
132 gst_audioringbuffer_get_type (void)
134 static GType ringbuffer_type = 0;
136 if (!ringbuffer_type) {
137 static const GTypeInfo ringbuffer_info = {
138 sizeof (GstAudioRingBufferClass),
141 (GClassInitFunc) gst_audioringbuffer_class_init,
144 sizeof (GstAudioRingBuffer),
146 (GInstanceInitFunc) gst_audioringbuffer_init,
151 g_type_register_static (GST_TYPE_RING_BUFFER, "GstAudioSrcRingBuffer",
152 &ringbuffer_info, 0);
154 return ringbuffer_type;
158 gst_audioringbuffer_class_init (GstAudioRingBufferClass * klass)
160 GObjectClass *gobject_class;
161 GstRingBufferClass *gstringbuffer_class;
163 gobject_class = (GObjectClass *) klass;
164 gstringbuffer_class = (GstRingBufferClass *) klass;
166 ring_parent_class = g_type_class_peek_parent (klass);
168 gobject_class->dispose = gst_audioringbuffer_dispose;
169 gobject_class->finalize = gst_audioringbuffer_finalize;
171 gstringbuffer_class->open_device =
172 GST_DEBUG_FUNCPTR (gst_audioringbuffer_open_device);
173 gstringbuffer_class->close_device =
174 GST_DEBUG_FUNCPTR (gst_audioringbuffer_close_device);
175 gstringbuffer_class->acquire =
176 GST_DEBUG_FUNCPTR (gst_audioringbuffer_acquire);
177 gstringbuffer_class->release =
178 GST_DEBUG_FUNCPTR (gst_audioringbuffer_release);
179 gstringbuffer_class->start = GST_DEBUG_FUNCPTR (gst_audioringbuffer_start);
180 gstringbuffer_class->resume = GST_DEBUG_FUNCPTR (gst_audioringbuffer_start);
181 gstringbuffer_class->stop = GST_DEBUG_FUNCPTR (gst_audioringbuffer_stop);
183 gstringbuffer_class->delay = GST_DEBUG_FUNCPTR (gst_audioringbuffer_delay);
186 typedef guint (*ReadFunc) (GstAudioSrc * src, gpointer data, guint length);
188 /* this internal thread does nothing else but read samples from the audio device.
189 * It will read each segment in the ringbuffer and will update the play
191 * The start/stop methods control the thread.
194 audioringbuffer_thread_func (GstRingBuffer * buf)
197 GstAudioSrcClass *csrc;
198 GstAudioRingBuffer *abuf = GST_AUDIORING_BUFFER (buf);
203 src = GST_AUDIO_SRC (GST_OBJECT_PARENT (buf));
204 csrc = GST_AUDIO_SRC_GET_CLASS (src);
206 GST_DEBUG_OBJECT (src, "enter thread");
208 readfunc = csrc->read;
209 if (readfunc == NULL)
212 /* FIXME: maybe we should at least use a custom pointer type here? */
213 g_value_init (&val, G_TYPE_POINTER);
214 g_value_set_pointer (&val, src->thread);
215 message = gst_message_new_stream_status (GST_OBJECT_CAST (buf),
216 GST_STREAM_STATUS_TYPE_ENTER, GST_ELEMENT_CAST (src));
217 gst_message_set_stream_status_object (message, &val);
218 GST_DEBUG_OBJECT (src, "posting ENTER stream status");
219 gst_element_post_message (GST_ELEMENT_CAST (src), message);
226 if (gst_ring_buffer_prepare_read (buf, &readseg, &readptr, &len)) {
231 read = readfunc (src, readptr, left);
232 GST_LOG_OBJECT (src, "transfered %d bytes of %d to segment %d", read,
234 if (read < 0 || read > left) {
235 GST_WARNING_OBJECT (src,
236 "error reading data %d (reason: %s), skipping segment", read,
244 /* we read one segment */
245 gst_ring_buffer_advance (buf, 1);
247 GST_OBJECT_LOCK (abuf);
250 GST_DEBUG_OBJECT (src, "signal wait");
251 GST_AUDIORING_BUFFER_SIGNAL (buf);
252 GST_DEBUG_OBJECT (src, "wait for action");
253 GST_AUDIORING_BUFFER_WAIT (buf);
254 GST_DEBUG_OBJECT (src, "got signal");
257 GST_DEBUG_OBJECT (src, "continue running");
258 GST_OBJECT_UNLOCK (abuf);
262 /* Will never be reached */
263 g_assert_not_reached ();
269 GST_DEBUG ("no write function, exit thread");
274 GST_OBJECT_UNLOCK (abuf);
275 GST_DEBUG ("stop running, exit thread");
276 message = gst_message_new_stream_status (GST_OBJECT_CAST (buf),
277 GST_STREAM_STATUS_TYPE_LEAVE, GST_ELEMENT_CAST (src));
278 gst_message_set_stream_status_object (message, &val);
279 GST_DEBUG_OBJECT (src, "posting LEAVE stream status");
280 gst_element_post_message (GST_ELEMENT_CAST (src), message);
286 gst_audioringbuffer_init (GstAudioRingBuffer * ringbuffer,
287 GstAudioRingBufferClass * g_class)
289 ringbuffer->running = FALSE;
290 ringbuffer->queuedseg = 0;
292 ringbuffer->cond = g_cond_new ();
296 gst_audioringbuffer_dispose (GObject * object)
298 GstAudioRingBuffer *ringbuffer = GST_AUDIORING_BUFFER (object);
300 if (ringbuffer->cond) {
301 g_cond_free (ringbuffer->cond);
302 ringbuffer->cond = NULL;
305 G_OBJECT_CLASS (ring_parent_class)->dispose (object);
309 gst_audioringbuffer_finalize (GObject * object)
311 G_OBJECT_CLASS (ring_parent_class)->finalize (object);
315 gst_audioringbuffer_open_device (GstRingBuffer * buf)
318 GstAudioSrcClass *csrc;
319 gboolean result = TRUE;
321 src = GST_AUDIO_SRC (GST_OBJECT_PARENT (buf));
322 csrc = GST_AUDIO_SRC_GET_CLASS (src);
325 result = csrc->open (src);
339 gst_audioringbuffer_close_device (GstRingBuffer * buf)
342 GstAudioSrcClass *csrc;
343 gboolean result = TRUE;
345 src = GST_AUDIO_SRC (GST_OBJECT_PARENT (buf));
346 csrc = GST_AUDIO_SRC_GET_CLASS (src);
349 result = csrc->close (src);
363 gst_audioringbuffer_acquire (GstRingBuffer * buf, GstRingBufferSpec * spec)
366 GstAudioSrcClass *csrc;
367 GstAudioRingBuffer *abuf;
368 gboolean result = FALSE;
370 src = GST_AUDIO_SRC (GST_OBJECT_PARENT (buf));
371 csrc = GST_AUDIO_SRC_GET_CLASS (src);
374 result = csrc->prepare (src, spec);
379 buf->size = spec->segtotal * spec->segsize;
380 buf->memory = g_malloc0 (buf->size);
382 abuf = GST_AUDIORING_BUFFER (buf);
383 abuf->running = TRUE;
386 g_thread_create ((GThreadFunc) audioringbuffer_thread_func, buf, TRUE,
388 GST_AUDIORING_BUFFER_WAIT (buf);
398 /* function is called with LOCK */
400 gst_audioringbuffer_release (GstRingBuffer * buf)
403 GstAudioSrcClass *csrc;
404 GstAudioRingBuffer *abuf;
405 gboolean result = FALSE;
407 src = GST_AUDIO_SRC (GST_OBJECT_PARENT (buf));
408 csrc = GST_AUDIO_SRC_GET_CLASS (src);
409 abuf = GST_AUDIORING_BUFFER (buf);
411 abuf->running = FALSE;
412 GST_AUDIORING_BUFFER_SIGNAL (buf);
413 GST_OBJECT_UNLOCK (buf);
415 /* join the thread */
416 g_thread_join (src->thread);
418 GST_OBJECT_LOCK (buf);
420 /* free the buffer */
421 g_free (buf->memory);
425 result = csrc->unprepare (src);
431 gst_audioringbuffer_start (GstRingBuffer * buf)
433 GST_DEBUG ("start, sending signal");
434 GST_AUDIORING_BUFFER_SIGNAL (buf);
440 gst_audioringbuffer_stop (GstRingBuffer * buf)
443 GstAudioSrcClass *csrc;
445 src = GST_AUDIO_SRC (GST_OBJECT_PARENT (buf));
446 csrc = GST_AUDIO_SRC_GET_CLASS (src);
448 /* unblock any pending writes to the audio device */
450 GST_DEBUG ("reset...");
452 GST_DEBUG ("reset done");
455 GST_DEBUG ("stop, waiting...");
456 GST_AUDIORING_BUFFER_WAIT (buf);
457 GST_DEBUG ("stoped");
464 gst_audioringbuffer_delay (GstRingBuffer * buf)
467 GstAudioSrcClass *csrc;
470 src = GST_AUDIO_SRC (GST_OBJECT_PARENT (buf));
471 csrc = GST_AUDIO_SRC_GET_CLASS (src);
474 res = csrc->delay (src);
479 /* AudioSrc signals and args */
491 #define _do_init(bla) \
492 GST_DEBUG_CATEGORY_INIT (gst_audio_src_debug, "audiosrc", 0, "audiosrc element");
494 GST_BOILERPLATE_FULL (GstAudioSrc, gst_audio_src, GstBaseAudioSrc,
495 GST_TYPE_BASE_AUDIO_SRC, _do_init);
497 static GstRingBuffer *gst_audio_src_create_ringbuffer (GstBaseAudioSrc * src);
500 gst_audio_src_base_init (gpointer g_class)
505 gst_audio_src_class_init (GstAudioSrcClass * klass)
507 GstBaseAudioSrcClass *gstbaseaudiosrc_class;
509 gstbaseaudiosrc_class = (GstBaseAudioSrcClass *) klass;
511 gstbaseaudiosrc_class->create_ringbuffer =
512 GST_DEBUG_FUNCPTR (gst_audio_src_create_ringbuffer);
514 g_type_class_ref (GST_TYPE_AUDIORING_BUFFER);
518 gst_audio_src_init (GstAudioSrc * audiosrc, GstAudioSrcClass * g_class)
522 static GstRingBuffer *
523 gst_audio_src_create_ringbuffer (GstBaseAudioSrc * src)
525 GstRingBuffer *buffer;
527 GST_DEBUG ("creating ringbuffer");
528 buffer = g_object_new (GST_TYPE_AUDIORING_BUFFER, NULL);
529 GST_DEBUG ("created ringbuffer @%p", buffer);