• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2005 Wim Taymans <wim@fluendo.com>
4  *
5  * gstaudiosrc.c: simple audio src base class
6  *
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.
11  *
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.
16  *
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., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 /**
24  * SECTION:gstaudiosrc
25  * @title: GstAudioSrc
26  * @short_description: Simple base class for audio sources
27  * @see_also: #GstAudioBaseSrc, #GstAudioRingBuffer, #GstAudioSrc.
28  *
29  * This is the most simple base class for audio sources that only requires
30  * subclasses to implement a set of simple functions:
31  *
32  * * `open()` :Open the device.
33  * * `prepare()` :Configure the device with the specified format.
34  * * `read()` :Read samples from the device.
35  * * `reset()` :Unblock reads and flush the device.
36  * * `delay()` :Get the number of samples in the device but not yet read.
37  * * `unprepare()` :Undo operations done by prepare.
38  * * `close()` :Close the device.
39  *
40  * All scheduling of samples and timestamps is done in this base class
41  * together with #GstAudioBaseSrc using a default implementation of a
42  * #GstAudioRingBuffer that uses threads.
43  */
44 #ifdef HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47 
48 #include <string.h>
49 
50 #include <gst/audio/audio.h>
51 #include "gstaudiosrc.h"
52 #include "gstaudioutilsprivate.h"
53 
54 GST_DEBUG_CATEGORY_STATIC (gst_audio_src_debug);
55 #define GST_CAT_DEFAULT gst_audio_src_debug
56 
57 #define GST_TYPE_AUDIO_SRC_RING_BUFFER        \
58         (gst_audio_src_ring_buffer_get_type())
59 #define GST_AUDIO_SRC_RING_BUFFER(obj)        \
60         (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_AUDIO_SRC_RING_BUFFER,GstAudioSrcRingBuffer))
61 #define GST_AUDIO_SRC_RING_BUFFER_CLASS(klass) \
62         (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_AUDIO_SRC_RING_BUFFER,GstAudioSrcRingBufferClass))
63 #define GST_AUDIO_SRC_RING_BUFFER_GET_CLASS(obj) \
64         (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_AUDIO_SRC_RING_BUFFER, GstAudioSrcRingBufferClass))
65 #define GST_IS_AUDIO_SRC_RING_BUFFER(obj)     \
66         (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_AUDIO_SRC_RING_BUFFER))
67 #define GST_IS_AUDIO_SRC_RING_BUFFER_CLASS(klass)\
68         (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_AUDIO_SRC_RING_BUFFER))
69 
70 typedef struct _GstAudioSrcRingBuffer GstAudioSrcRingBuffer;
71 typedef struct _GstAudioSrcRingBufferClass GstAudioSrcRingBufferClass;
72 
73 #define GST_AUDIO_SRC_RING_BUFFER_GET_COND(buf) (&(((GstAudioSrcRingBuffer *)buf)->cond))
74 #define GST_AUDIO_SRC_RING_BUFFER_WAIT(buf)     (g_cond_wait (GST_AUDIO_SRC_RING_BUFFER_GET_COND (buf), GST_OBJECT_GET_LOCK (buf)))
75 #define GST_AUDIO_SRC_RING_BUFFER_SIGNAL(buf)   (g_cond_signal (GST_AUDIO_SRC_RING_BUFFER_GET_COND (buf)))
76 #define GST_AUDIO_SRC_RING_BUFFER_BROADCAST(buf)(g_cond_broadcast (GST_AUDIO_SRC_RING_BUFFER_GET_COND (buf)))
77 
78 struct _GstAudioSrcRingBuffer
79 {
80   GstAudioRingBuffer object;
81 
82   gboolean running;
83   gint queuedseg;
84 
85   GCond cond;
86 };
87 
88 struct _GstAudioSrcRingBufferClass
89 {
90   GstAudioRingBufferClass parent_class;
91 };
92 
93 static void gst_audio_src_ring_buffer_class_init (GstAudioSrcRingBufferClass *
94     klass);
95 static void gst_audio_src_ring_buffer_init (GstAudioSrcRingBuffer * ringbuffer,
96     GstAudioSrcRingBufferClass * klass);
97 static void gst_audio_src_ring_buffer_dispose (GObject * object);
98 static void gst_audio_src_ring_buffer_finalize (GObject * object);
99 
100 static GstAudioRingBufferClass *ring_parent_class = NULL;
101 
102 static gboolean gst_audio_src_ring_buffer_open_device (GstAudioRingBuffer *
103     buf);
104 static gboolean gst_audio_src_ring_buffer_close_device (GstAudioRingBuffer *
105     buf);
106 static gboolean gst_audio_src_ring_buffer_acquire (GstAudioRingBuffer * buf,
107     GstAudioRingBufferSpec * spec);
108 static gboolean gst_audio_src_ring_buffer_release (GstAudioRingBuffer * buf);
109 static gboolean gst_audio_src_ring_buffer_start (GstAudioRingBuffer * buf);
110 static gboolean gst_audio_src_ring_buffer_stop (GstAudioRingBuffer * buf);
111 static guint gst_audio_src_ring_buffer_delay (GstAudioRingBuffer * buf);
112 
113 /* ringbuffer abstract base class */
114 static GType
gst_audio_src_ring_buffer_get_type(void)115 gst_audio_src_ring_buffer_get_type (void)
116 {
117   static GType ringbuffer_type = 0;
118 
119   if (!ringbuffer_type) {
120     static const GTypeInfo ringbuffer_info = {
121       sizeof (GstAudioSrcRingBufferClass),
122       NULL,
123       NULL,
124       (GClassInitFunc) gst_audio_src_ring_buffer_class_init,
125       NULL,
126       NULL,
127       sizeof (GstAudioSrcRingBuffer),
128       0,
129       (GInstanceInitFunc) gst_audio_src_ring_buffer_init,
130       NULL
131     };
132 
133     ringbuffer_type =
134         g_type_register_static (GST_TYPE_AUDIO_RING_BUFFER,
135         "GstAudioSrcRingBuffer", &ringbuffer_info, 0);
136   }
137   return ringbuffer_type;
138 }
139 
140 static void
gst_audio_src_ring_buffer_class_init(GstAudioSrcRingBufferClass * klass)141 gst_audio_src_ring_buffer_class_init (GstAudioSrcRingBufferClass * klass)
142 {
143   GObjectClass *gobject_class;
144   GstAudioRingBufferClass *gstringbuffer_class;
145 
146   gobject_class = (GObjectClass *) klass;
147   gstringbuffer_class = (GstAudioRingBufferClass *) klass;
148 
149   ring_parent_class = g_type_class_peek_parent (klass);
150 
151   gobject_class->dispose = gst_audio_src_ring_buffer_dispose;
152   gobject_class->finalize = gst_audio_src_ring_buffer_finalize;
153 
154   gstringbuffer_class->open_device =
155       GST_DEBUG_FUNCPTR (gst_audio_src_ring_buffer_open_device);
156   gstringbuffer_class->close_device =
157       GST_DEBUG_FUNCPTR (gst_audio_src_ring_buffer_close_device);
158   gstringbuffer_class->acquire =
159       GST_DEBUG_FUNCPTR (gst_audio_src_ring_buffer_acquire);
160   gstringbuffer_class->release =
161       GST_DEBUG_FUNCPTR (gst_audio_src_ring_buffer_release);
162   gstringbuffer_class->start =
163       GST_DEBUG_FUNCPTR (gst_audio_src_ring_buffer_start);
164   gstringbuffer_class->resume =
165       GST_DEBUG_FUNCPTR (gst_audio_src_ring_buffer_start);
166   gstringbuffer_class->stop =
167       GST_DEBUG_FUNCPTR (gst_audio_src_ring_buffer_stop);
168 
169   gstringbuffer_class->delay =
170       GST_DEBUG_FUNCPTR (gst_audio_src_ring_buffer_delay);
171 }
172 
173 typedef guint (*ReadFunc)
174   (GstAudioSrc * src, gpointer data, guint length, GstClockTime * timestamp);
175 
176 /* this internal thread does nothing else but read samples from the audio device.
177  * It will read each segment in the ringbuffer and will update the play
178  * pointer.
179  * The start/stop methods control the thread.
180  */
181 static void
audioringbuffer_thread_func(GstAudioRingBuffer * buf)182 audioringbuffer_thread_func (GstAudioRingBuffer * buf)
183 {
184   GstAudioSrc *src;
185   GstAudioSrcClass *csrc;
186   GstAudioSrcRingBuffer *abuf = GST_AUDIO_SRC_RING_BUFFER (buf);
187   ReadFunc readfunc;
188   GstMessage *message;
189   GValue val = { 0 };
190   gpointer handle;
191 
192   src = GST_AUDIO_SRC (GST_OBJECT_PARENT (buf));
193   csrc = GST_AUDIO_SRC_GET_CLASS (src);
194 
195   GST_DEBUG_OBJECT (src, "enter thread");
196 
197   if ((readfunc = csrc->read) == NULL)
198     goto no_function;
199 
200   if (G_UNLIKELY (!__gst_audio_set_thread_priority (&handle)))
201     GST_WARNING_OBJECT (src, "failed to set thread priority");
202 
203   message = gst_message_new_stream_status (GST_OBJECT_CAST (buf),
204       GST_STREAM_STATUS_TYPE_ENTER, GST_ELEMENT_CAST (src));
205   g_value_init (&val, GST_TYPE_G_THREAD);
206   g_value_set_boxed (&val, g_thread_self ());
207   gst_message_set_stream_status_object (message, &val);
208   g_value_unset (&val);
209   GST_DEBUG_OBJECT (src, "posting ENTER stream status");
210   gst_element_post_message (GST_ELEMENT_CAST (src), message);
211 
212   while (TRUE) {
213     gint left, len;
214     guint8 *readptr;
215     gint readseg;
216     GstClockTime timestamp = GST_CLOCK_TIME_NONE;
217 
218     if (gst_audio_ring_buffer_prepare_read (buf, &readseg, &readptr, &len)) {
219       gint read;
220 
221       left = len;
222       do {
223         read = readfunc (src, readptr, left, &timestamp);
224         GST_LOG_OBJECT (src, "transferred %d bytes of %d to segment %d", read,
225             left, readseg);
226         if (read < 0 || read > left) {
227           GST_WARNING_OBJECT (src,
228               "error reading data %d (reason: %s), skipping segment", read,
229               g_strerror (errno));
230           break;
231         }
232         left -= read;
233         readptr += read;
234 
235       } while (left > 0 && g_atomic_int_get (&abuf->running));
236 
237       /* Update timestamp on buffer if required */
238       gst_audio_ring_buffer_set_timestamp (buf, readseg, timestamp);
239 
240       /* we read one segment */
241       gst_audio_ring_buffer_advance (buf, 1);
242     } else {
243       GST_OBJECT_LOCK (abuf);
244       if (!abuf->running)
245         goto stop_running;
246       if (G_UNLIKELY (g_atomic_int_get (&buf->state) ==
247               GST_AUDIO_RING_BUFFER_STATE_STARTED)) {
248         GST_OBJECT_UNLOCK (abuf);
249         continue;
250       }
251       GST_DEBUG_OBJECT (src, "signal wait");
252       GST_AUDIO_SRC_RING_BUFFER_SIGNAL (buf);
253       GST_DEBUG_OBJECT (src, "wait for action");
254       GST_AUDIO_SRC_RING_BUFFER_WAIT (buf);
255       GST_DEBUG_OBJECT (src, "got signal");
256       if (!abuf->running)
257         goto stop_running;
258       GST_DEBUG_OBJECT (src, "continue running");
259       GST_OBJECT_UNLOCK (abuf);
260     }
261   }
262 
263   /* Will never be reached */
264   g_assert_not_reached ();
265   return;
266 
267   /* ERROR */
268 no_function:
269   {
270     GST_DEBUG ("no write function, exit thread");
271     return;
272   }
273 stop_running:
274   {
275     GST_OBJECT_UNLOCK (abuf);
276     GST_DEBUG ("stop running, exit thread");
277     message = gst_message_new_stream_status (GST_OBJECT_CAST (buf),
278         GST_STREAM_STATUS_TYPE_LEAVE, GST_ELEMENT_CAST (src));
279     g_value_init (&val, GST_TYPE_G_THREAD);
280     g_value_set_boxed (&val, g_thread_self ());
281     gst_message_set_stream_status_object (message, &val);
282     g_value_unset (&val);
283     GST_DEBUG_OBJECT (src, "posting LEAVE stream status");
284     gst_element_post_message (GST_ELEMENT_CAST (src), message);
285 
286     if (G_UNLIKELY (!__gst_audio_restore_thread_priority (handle)))
287       GST_WARNING_OBJECT (src, "failed to restore thread priority");
288     return;
289   }
290 }
291 
292 static void
gst_audio_src_ring_buffer_init(GstAudioSrcRingBuffer * ringbuffer,GstAudioSrcRingBufferClass * g_class)293 gst_audio_src_ring_buffer_init (GstAudioSrcRingBuffer * ringbuffer,
294     GstAudioSrcRingBufferClass * g_class)
295 {
296   ringbuffer->running = FALSE;
297   ringbuffer->queuedseg = 0;
298 
299   g_cond_init (&ringbuffer->cond);
300 }
301 
302 static void
gst_audio_src_ring_buffer_dispose(GObject * object)303 gst_audio_src_ring_buffer_dispose (GObject * object)
304 {
305   GstAudioSrcRingBuffer *ringbuffer = GST_AUDIO_SRC_RING_BUFFER (object);
306 
307   g_cond_clear (&ringbuffer->cond);
308 
309   G_OBJECT_CLASS (ring_parent_class)->dispose (object);
310 }
311 
312 static void
gst_audio_src_ring_buffer_finalize(GObject * object)313 gst_audio_src_ring_buffer_finalize (GObject * object)
314 {
315   G_OBJECT_CLASS (ring_parent_class)->finalize (object);
316 }
317 
318 static gboolean
gst_audio_src_ring_buffer_open_device(GstAudioRingBuffer * buf)319 gst_audio_src_ring_buffer_open_device (GstAudioRingBuffer * buf)
320 {
321   GstAudioSrc *src;
322   GstAudioSrcClass *csrc;
323   gboolean result = TRUE;
324 
325   src = GST_AUDIO_SRC (GST_OBJECT_PARENT (buf));
326   csrc = GST_AUDIO_SRC_GET_CLASS (src);
327 
328   if (csrc->open)
329     result = csrc->open (src);
330 
331   if (!result)
332     goto could_not_open;
333 
334   return result;
335 
336 could_not_open:
337   {
338     return FALSE;
339   }
340 }
341 
342 static gboolean
gst_audio_src_ring_buffer_close_device(GstAudioRingBuffer * buf)343 gst_audio_src_ring_buffer_close_device (GstAudioRingBuffer * buf)
344 {
345   GstAudioSrc *src;
346   GstAudioSrcClass *csrc;
347   gboolean result = TRUE;
348 
349   src = GST_AUDIO_SRC (GST_OBJECT_PARENT (buf));
350   csrc = GST_AUDIO_SRC_GET_CLASS (src);
351 
352   if (csrc->close)
353     result = csrc->close (src);
354 
355   if (!result)
356     goto could_not_open;
357 
358   return result;
359 
360 could_not_open:
361   {
362     return FALSE;
363   }
364 }
365 
366 static gboolean
gst_audio_src_ring_buffer_acquire(GstAudioRingBuffer * buf,GstAudioRingBufferSpec * spec)367 gst_audio_src_ring_buffer_acquire (GstAudioRingBuffer * buf,
368     GstAudioRingBufferSpec * spec)
369 {
370   GstAudioSrc *src;
371   GstAudioSrcClass *csrc;
372   GstAudioSrcRingBuffer *abuf;
373   gboolean result = FALSE;
374 
375   src = GST_AUDIO_SRC (GST_OBJECT_PARENT (buf));
376   csrc = GST_AUDIO_SRC_GET_CLASS (src);
377 
378   if (csrc->prepare)
379     result = csrc->prepare (src, spec);
380 
381   if (!result)
382     goto could_not_open;
383 
384   buf->size = spec->segtotal * spec->segsize;
385   buf->memory = g_malloc (buf->size);
386   if (buf->spec.type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_RAW) {
387     gst_audio_format_info_fill_silence (buf->spec.info.finfo, buf->memory,
388         buf->size);
389   } else {
390     /* FIXME, non-raw formats get 0 as the empty sample */
391     memset (buf->memory, 0, buf->size);
392   }
393 
394   abuf = GST_AUDIO_SRC_RING_BUFFER (buf);
395   abuf->running = TRUE;
396 
397   /* FIXME: handle thread creation failure */
398   src->thread = g_thread_try_new ("audiosrc-ringbuffer",
399       (GThreadFunc) audioringbuffer_thread_func, buf, NULL);
400 
401   GST_AUDIO_SRC_RING_BUFFER_WAIT (buf);
402 
403   return result;
404 
405 could_not_open:
406   {
407     return FALSE;
408   }
409 }
410 
411 /* function is called with LOCK */
412 static gboolean
gst_audio_src_ring_buffer_release(GstAudioRingBuffer * buf)413 gst_audio_src_ring_buffer_release (GstAudioRingBuffer * buf)
414 {
415   GstAudioSrc *src;
416   GstAudioSrcClass *csrc;
417   GstAudioSrcRingBuffer *abuf;
418   gboolean result = FALSE;
419 
420   src = GST_AUDIO_SRC (GST_OBJECT_PARENT (buf));
421   csrc = GST_AUDIO_SRC_GET_CLASS (src);
422   abuf = GST_AUDIO_SRC_RING_BUFFER (buf);
423 
424   abuf->running = FALSE;
425   GST_AUDIO_SRC_RING_BUFFER_SIGNAL (buf);
426   GST_OBJECT_UNLOCK (buf);
427 
428   /* join the thread */
429   g_thread_join (src->thread);
430 
431   GST_OBJECT_LOCK (buf);
432 
433   /* free the buffer */
434   g_free (buf->memory);
435   buf->memory = NULL;
436 
437   if (csrc->unprepare)
438     result = csrc->unprepare (src);
439 
440   return result;
441 }
442 
443 static gboolean
gst_audio_src_ring_buffer_start(GstAudioRingBuffer * buf)444 gst_audio_src_ring_buffer_start (GstAudioRingBuffer * buf)
445 {
446   GST_DEBUG ("start, sending signal");
447   GST_AUDIO_SRC_RING_BUFFER_SIGNAL (buf);
448 
449   return TRUE;
450 }
451 
452 static gboolean
gst_audio_src_ring_buffer_stop(GstAudioRingBuffer * buf)453 gst_audio_src_ring_buffer_stop (GstAudioRingBuffer * buf)
454 {
455   GstAudioSrc *src;
456   GstAudioSrcClass *csrc;
457 
458   src = GST_AUDIO_SRC (GST_OBJECT_PARENT (buf));
459   csrc = GST_AUDIO_SRC_GET_CLASS (src);
460 
461   /* unblock any pending writes to the audio device */
462   if (csrc->reset) {
463     GST_DEBUG ("reset...");
464     csrc->reset (src);
465     GST_DEBUG ("reset done");
466   }
467 #if 0
468   GST_DEBUG ("stop, waiting...");
469   GST_AUDIO_SRC_RING_BUFFER_WAIT (buf);
470   GST_DEBUG ("stopped");
471 #endif
472 
473   return TRUE;
474 }
475 
476 static guint
gst_audio_src_ring_buffer_delay(GstAudioRingBuffer * buf)477 gst_audio_src_ring_buffer_delay (GstAudioRingBuffer * buf)
478 {
479   GstAudioSrc *src;
480   GstAudioSrcClass *csrc;
481   guint res = 0;
482 
483   src = GST_AUDIO_SRC (GST_OBJECT_PARENT (buf));
484   csrc = GST_AUDIO_SRC_GET_CLASS (src);
485 
486   if (csrc->delay)
487     res = csrc->delay (src);
488 
489   return res;
490 }
491 
492 /* AudioSrc signals and args */
493 enum
494 {
495   /* FILL ME */
496   LAST_SIGNAL
497 };
498 
499 enum
500 {
501   ARG_0,
502 };
503 
504 #define _do_init \
505     GST_DEBUG_CATEGORY_INIT (gst_audio_src_debug, "audiosrc", 0, "audiosrc element");
506 #define gst_audio_src_parent_class parent_class
507 G_DEFINE_TYPE_WITH_CODE (GstAudioSrc, gst_audio_src,
508     GST_TYPE_AUDIO_BASE_SRC, _do_init);
509 
510 static GstAudioRingBuffer *gst_audio_src_create_ringbuffer (GstAudioBaseSrc *
511     src);
512 
513 static void
gst_audio_src_class_init(GstAudioSrcClass * klass)514 gst_audio_src_class_init (GstAudioSrcClass * klass)
515 {
516   GstAudioBaseSrcClass *gstaudiobasesrc_class;
517 
518   gstaudiobasesrc_class = (GstAudioBaseSrcClass *) klass;
519 
520   gstaudiobasesrc_class->create_ringbuffer =
521       GST_DEBUG_FUNCPTR (gst_audio_src_create_ringbuffer);
522 
523   g_type_class_ref (GST_TYPE_AUDIO_SRC_RING_BUFFER);
524 }
525 
526 static void
gst_audio_src_init(GstAudioSrc * audiosrc)527 gst_audio_src_init (GstAudioSrc * audiosrc)
528 {
529 }
530 
531 static GstAudioRingBuffer *
gst_audio_src_create_ringbuffer(GstAudioBaseSrc * src)532 gst_audio_src_create_ringbuffer (GstAudioBaseSrc * src)
533 {
534   GstAudioRingBuffer *buffer;
535 
536   GST_DEBUG ("creating ringbuffer");
537   buffer = g_object_new (GST_TYPE_AUDIO_SRC_RING_BUFFER, NULL);
538   GST_DEBUG ("created ringbuffer @%p", buffer);
539 
540   return buffer;
541 }
542