• 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  * gstaudiobasesrc.c:
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:gstaudiobasesrc
25  * @title: GstAudioBaseSrc
26  * @short_description: Base class for audio sources
27  * @see_also: #GstAudioSrc, #GstAudioRingBuffer.
28  *
29  * This is the base class for audio sources. Subclasses need to implement the
30  * ::create_ringbuffer vmethod. This base class will then take care of
31  * reading samples from the ringbuffer, synchronisation and flushing.
32  */
33 
34 #ifdef HAVE_CONFIG_H
35 #  include "config.h"
36 #endif
37 
38 #include <string.h>
39 
40 #include <gst/audio/audio.h>
41 #include "gstaudiobasesrc.h"
42 
43 #include "gst/gst-i18n-plugin.h"
44 
45 GST_DEBUG_CATEGORY_STATIC (gst_audio_base_src_debug);
46 #define GST_CAT_DEFAULT gst_audio_base_src_debug
47 
48 struct _GstAudioBaseSrcPrivate
49 {
50   /* the clock slaving algorithm in use */
51   GstAudioBaseSrcSlaveMethod slave_method;
52 };
53 
54 /* BaseAudioSrc signals and args */
55 enum
56 {
57   /* FILL ME */
58   LAST_SIGNAL
59 };
60 
61 /* FIXME: 2.0, handle BUFFER_TIME and LATENCY in nanoseconds */
62 #define DEFAULT_BUFFER_TIME     ((200 * GST_MSECOND) / GST_USECOND)
63 #define DEFAULT_LATENCY_TIME    ((10 * GST_MSECOND) / GST_USECOND)
64 #define DEFAULT_ACTUAL_BUFFER_TIME     -1
65 #define DEFAULT_ACTUAL_LATENCY_TIME    -1
66 #define DEFAULT_PROVIDE_CLOCK   TRUE
67 #define DEFAULT_SLAVE_METHOD    GST_AUDIO_BASE_SRC_SLAVE_SKEW
68 
69 enum
70 {
71   PROP_0,
72   PROP_BUFFER_TIME,
73   PROP_LATENCY_TIME,
74   PROP_ACTUAL_BUFFER_TIME,
75   PROP_ACTUAL_LATENCY_TIME,
76   PROP_PROVIDE_CLOCK,
77   PROP_SLAVE_METHOD,
78   PROP_LAST
79 };
80 
81 static void
_do_init(GType type)82 _do_init (GType type)
83 {
84   GST_DEBUG_CATEGORY_INIT (gst_audio_base_src_debug, "audiobasesrc", 0,
85       "audiobasesrc element");
86 
87 #ifdef ENABLE_NLS
88   GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
89       LOCALEDIR);
90   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
91   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
92 #endif /* ENABLE_NLS */
93 }
94 
95 #define gst_audio_base_src_parent_class parent_class
96 G_DEFINE_TYPE_WITH_CODE (GstAudioBaseSrc, gst_audio_base_src, GST_TYPE_PUSH_SRC,
97     G_ADD_PRIVATE (GstAudioBaseSrc)
98     _do_init (g_define_type_id));
99 
100 static void gst_audio_base_src_set_property (GObject * object, guint prop_id,
101     const GValue * value, GParamSpec * pspec);
102 static void gst_audio_base_src_get_property (GObject * object, guint prop_id,
103     GValue * value, GParamSpec * pspec);
104 static void gst_audio_base_src_dispose (GObject * object);
105 
106 static GstStateChangeReturn gst_audio_base_src_change_state (GstElement *
107     element, GstStateChange transition);
108 static gboolean gst_audio_base_src_post_message (GstElement * element,
109     GstMessage * message);
110 static GstClock *gst_audio_base_src_provide_clock (GstElement * elem);
111 static GstClockTime gst_audio_base_src_get_time (GstClock * clock,
112     GstAudioBaseSrc * src);
113 
114 static GstFlowReturn gst_audio_base_src_create (GstBaseSrc * bsrc,
115     guint64 offset, guint length, GstBuffer ** buf);
116 
117 static gboolean gst_audio_base_src_event (GstBaseSrc * bsrc, GstEvent * event);
118 static void gst_audio_base_src_get_times (GstBaseSrc * bsrc,
119     GstBuffer * buffer, GstClockTime * start, GstClockTime * end);
120 static gboolean gst_audio_base_src_setcaps (GstBaseSrc * bsrc, GstCaps * caps);
121 static gboolean gst_audio_base_src_query (GstBaseSrc * bsrc, GstQuery * query);
122 static GstCaps *gst_audio_base_src_fixate (GstBaseSrc * bsrc, GstCaps * caps);
123 
124 /* static guint gst_audio_base_src_signals[LAST_SIGNAL] = { 0 }; */
125 
126 static void
gst_audio_base_src_class_init(GstAudioBaseSrcClass * klass)127 gst_audio_base_src_class_init (GstAudioBaseSrcClass * klass)
128 {
129   GObjectClass *gobject_class;
130   GstElementClass *gstelement_class;
131   GstBaseSrcClass *gstbasesrc_class;
132 
133   gobject_class = (GObjectClass *) klass;
134   gstelement_class = (GstElementClass *) klass;
135   gstbasesrc_class = (GstBaseSrcClass *) klass;
136 
137   gobject_class->set_property = gst_audio_base_src_set_property;
138   gobject_class->get_property = gst_audio_base_src_get_property;
139   gobject_class->dispose = gst_audio_base_src_dispose;
140 
141   /* FIXME: 2.0, handle BUFFER_TIME and LATENCY in nanoseconds */
142   g_object_class_install_property (gobject_class, PROP_BUFFER_TIME,
143       g_param_spec_int64 ("buffer-time", "Buffer Time",
144           "Size of audio buffer in microseconds. This is the maximum amount "
145           "of data that is buffered in the device and the maximum latency that "
146           "the source reports. This value might be ignored by the element if "
147           "necessary; see \"actual-buffer-time\"",
148           1, G_MAXINT64, DEFAULT_BUFFER_TIME,
149           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
150 
151   g_object_class_install_property (gobject_class, PROP_LATENCY_TIME,
152       g_param_spec_int64 ("latency-time", "Latency Time",
153           "The minimum amount of data to read in each iteration in "
154           "microseconds. This is the minimum latency that the source reports. "
155           "This value might be ignored by the element if necessary; see "
156           "\"actual-latency-time\"", 1, G_MAXINT64, DEFAULT_LATENCY_TIME,
157           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
158 
159   /**
160    * GstAudioBaseSrc:actual-buffer-time:
161    *
162    * Actual configured size of audio buffer in microseconds.
163    **/
164   g_object_class_install_property (gobject_class, PROP_ACTUAL_BUFFER_TIME,
165       g_param_spec_int64 ("actual-buffer-time", "Actual Buffer Time",
166           "Actual configured size of audio buffer in microseconds",
167           DEFAULT_ACTUAL_BUFFER_TIME, G_MAXINT64, DEFAULT_ACTUAL_BUFFER_TIME,
168           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
169 
170   /**
171    * GstAudioBaseSrc:actual-latency-time:
172    *
173    * Actual configured audio latency in microseconds.
174    **/
175   g_object_class_install_property (gobject_class, PROP_ACTUAL_LATENCY_TIME,
176       g_param_spec_int64 ("actual-latency-time", "Actual Latency Time",
177           "Actual configured audio latency in microseconds",
178           DEFAULT_ACTUAL_LATENCY_TIME, G_MAXINT64, DEFAULT_ACTUAL_LATENCY_TIME,
179           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
180 
181   g_object_class_install_property (gobject_class, PROP_PROVIDE_CLOCK,
182       g_param_spec_boolean ("provide-clock", "Provide Clock",
183           "Provide a clock to be used as the global pipeline clock",
184           DEFAULT_PROVIDE_CLOCK, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
185 
186   g_object_class_install_property (gobject_class, PROP_SLAVE_METHOD,
187       g_param_spec_enum ("slave-method", "Slave Method",
188           "Algorithm used to match the rate of the masterclock",
189           GST_TYPE_AUDIO_BASE_SRC_SLAVE_METHOD, DEFAULT_SLAVE_METHOD,
190           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
191 
192   gstelement_class->change_state =
193       GST_DEBUG_FUNCPTR (gst_audio_base_src_change_state);
194   gstelement_class->provide_clock =
195       GST_DEBUG_FUNCPTR (gst_audio_base_src_provide_clock);
196   gstelement_class->post_message =
197       GST_DEBUG_FUNCPTR (gst_audio_base_src_post_message);
198 
199   gstbasesrc_class->set_caps = GST_DEBUG_FUNCPTR (gst_audio_base_src_setcaps);
200   gstbasesrc_class->event = GST_DEBUG_FUNCPTR (gst_audio_base_src_event);
201   gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_audio_base_src_query);
202   gstbasesrc_class->get_times =
203       GST_DEBUG_FUNCPTR (gst_audio_base_src_get_times);
204   gstbasesrc_class->create = GST_DEBUG_FUNCPTR (gst_audio_base_src_create);
205   gstbasesrc_class->fixate = GST_DEBUG_FUNCPTR (gst_audio_base_src_fixate);
206 
207   /* ref class from a thread-safe context to work around missing bit of
208    * thread-safety in GObject */
209   g_type_class_ref (GST_TYPE_AUDIO_CLOCK);
210   g_type_class_ref (GST_TYPE_AUDIO_RING_BUFFER);
211 }
212 
213 static void
gst_audio_base_src_init(GstAudioBaseSrc * audiobasesrc)214 gst_audio_base_src_init (GstAudioBaseSrc * audiobasesrc)
215 {
216   audiobasesrc->priv = gst_audio_base_src_get_instance_private (audiobasesrc);
217 
218   audiobasesrc->buffer_time = DEFAULT_BUFFER_TIME;
219   audiobasesrc->latency_time = DEFAULT_LATENCY_TIME;
220   if (DEFAULT_PROVIDE_CLOCK)
221     GST_OBJECT_FLAG_SET (audiobasesrc, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
222   else
223     GST_OBJECT_FLAG_UNSET (audiobasesrc, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
224   audiobasesrc->priv->slave_method = DEFAULT_SLAVE_METHOD;
225   /* reset blocksize we use latency time to calculate a more useful
226    * value based on negotiated format. */
227   GST_BASE_SRC (audiobasesrc)->blocksize = 0;
228 
229   audiobasesrc->clock = gst_audio_clock_new ("GstAudioSrcClock",
230       (GstAudioClockGetTimeFunc) gst_audio_base_src_get_time, audiobasesrc,
231       NULL);
232 
233 
234   /* we are always a live source */
235   gst_base_src_set_live (GST_BASE_SRC (audiobasesrc), TRUE);
236   /* we operate in time */
237   gst_base_src_set_format (GST_BASE_SRC (audiobasesrc), GST_FORMAT_TIME);
238 }
239 
240 static void
gst_audio_base_src_dispose(GObject * object)241 gst_audio_base_src_dispose (GObject * object)
242 {
243   GstAudioBaseSrc *src;
244 
245   src = GST_AUDIO_BASE_SRC (object);
246 
247   GST_OBJECT_LOCK (src);
248   if (src->clock) {
249     gst_audio_clock_invalidate (GST_AUDIO_CLOCK (src->clock));
250     gst_object_unref (src->clock);
251     src->clock = NULL;
252   }
253 
254   if (src->ringbuffer) {
255     gst_object_unparent (GST_OBJECT_CAST (src->ringbuffer));
256     src->ringbuffer = NULL;
257   }
258   GST_OBJECT_UNLOCK (src);
259 
260   G_OBJECT_CLASS (parent_class)->dispose (object);
261 }
262 
263 static GstClock *
gst_audio_base_src_provide_clock(GstElement * elem)264 gst_audio_base_src_provide_clock (GstElement * elem)
265 {
266   GstAudioBaseSrc *src;
267   GstClock *clock;
268 
269   src = GST_AUDIO_BASE_SRC (elem);
270 
271   /* we have no ringbuffer (must be NULL state) */
272   if (src->ringbuffer == NULL)
273     goto wrong_state;
274 
275   if (gst_audio_ring_buffer_is_flushing (src->ringbuffer))
276     goto wrong_state;
277 
278   GST_OBJECT_LOCK (src);
279 
280   if (!GST_OBJECT_FLAG_IS_SET (src, GST_ELEMENT_FLAG_PROVIDE_CLOCK))
281     goto clock_disabled;
282 
283   clock = GST_CLOCK_CAST (gst_object_ref (src->clock));
284   GST_OBJECT_UNLOCK (src);
285 
286   return clock;
287 
288   /* ERRORS */
289 wrong_state:
290   {
291     GST_DEBUG_OBJECT (src, "ringbuffer is flushing");
292     return NULL;
293   }
294 clock_disabled:
295   {
296     GST_DEBUG_OBJECT (src, "clock provide disabled");
297     GST_OBJECT_UNLOCK (src);
298     return NULL;
299   }
300 }
301 
302 static GstClockTime
gst_audio_base_src_get_time(GstClock * clock,GstAudioBaseSrc * src)303 gst_audio_base_src_get_time (GstClock * clock, GstAudioBaseSrc * src)
304 {
305   guint64 raw, samples;
306   guint delay;
307   GstClockTime result;
308   GstAudioRingBuffer *ringbuffer;
309   gint rate;
310 
311   ringbuffer = src->ringbuffer;
312   if (!ringbuffer)
313     return GST_CLOCK_TIME_NONE;
314 
315   rate = ringbuffer->spec.info.rate;
316   if (rate == 0)
317     return GST_CLOCK_TIME_NONE;
318 
319   raw = samples = gst_audio_ring_buffer_samples_done (ringbuffer);
320 
321   /* the number of samples not yet processed, this is still queued in the
322    * device (not yet read for capture). */
323   delay = gst_audio_ring_buffer_delay (ringbuffer);
324 
325   samples += delay;
326 
327   result = gst_util_uint64_scale_int (samples, GST_SECOND, rate);
328 
329   GST_DEBUG_OBJECT (src,
330       "processed samples: raw %" G_GUINT64_FORMAT ", delay %u, real %"
331       G_GUINT64_FORMAT ", time %" GST_TIME_FORMAT, raw, delay, samples,
332       GST_TIME_ARGS (result));
333 
334   return result;
335 }
336 
337 /**
338  * gst_audio_base_src_set_provide_clock:
339  * @src: a #GstAudioBaseSrc
340  * @provide: new state
341  *
342  * Controls whether @src will provide a clock or not. If @provide is %TRUE,
343  * gst_element_provide_clock() will return a clock that reflects the datarate
344  * of @src. If @provide is %FALSE, gst_element_provide_clock() will return NULL.
345  */
346 void
gst_audio_base_src_set_provide_clock(GstAudioBaseSrc * src,gboolean provide)347 gst_audio_base_src_set_provide_clock (GstAudioBaseSrc * src, gboolean provide)
348 {
349   g_return_if_fail (GST_IS_AUDIO_BASE_SRC (src));
350 
351   GST_OBJECT_LOCK (src);
352   if (provide)
353     GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
354   else
355     GST_OBJECT_FLAG_UNSET (src, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
356   GST_OBJECT_UNLOCK (src);
357 }
358 
359 /**
360  * gst_audio_base_src_get_provide_clock:
361  * @src: a #GstAudioBaseSrc
362  *
363  * Queries whether @src will provide a clock or not. See also
364  * gst_audio_base_src_set_provide_clock.
365  *
366  * Returns: %TRUE if @src will provide a clock.
367  */
368 gboolean
gst_audio_base_src_get_provide_clock(GstAudioBaseSrc * src)369 gst_audio_base_src_get_provide_clock (GstAudioBaseSrc * src)
370 {
371   gboolean result;
372 
373   g_return_val_if_fail (GST_IS_AUDIO_BASE_SRC (src), FALSE);
374 
375   GST_OBJECT_LOCK (src);
376   result = GST_OBJECT_FLAG_IS_SET (src, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
377   GST_OBJECT_UNLOCK (src);
378 
379   return result;
380 }
381 
382 /**
383  * gst_audio_base_src_set_slave_method:
384  * @src: a #GstAudioBaseSrc
385  * @method: the new slave method
386  *
387  * Controls how clock slaving will be performed in @src.
388  */
389 void
gst_audio_base_src_set_slave_method(GstAudioBaseSrc * src,GstAudioBaseSrcSlaveMethod method)390 gst_audio_base_src_set_slave_method (GstAudioBaseSrc * src,
391     GstAudioBaseSrcSlaveMethod method)
392 {
393   g_return_if_fail (GST_IS_AUDIO_BASE_SRC (src));
394 
395   GST_OBJECT_LOCK (src);
396   src->priv->slave_method = method;
397   GST_OBJECT_UNLOCK (src);
398 }
399 
400 /**
401  * gst_audio_base_src_get_slave_method:
402  * @src: a #GstAudioBaseSrc
403  *
404  * Get the current slave method used by @src.
405  *
406  * Returns: The current slave method used by @src.
407  */
408 GstAudioBaseSrcSlaveMethod
gst_audio_base_src_get_slave_method(GstAudioBaseSrc * src)409 gst_audio_base_src_get_slave_method (GstAudioBaseSrc * src)
410 {
411   GstAudioBaseSrcSlaveMethod result;
412 
413   g_return_val_if_fail (GST_IS_AUDIO_BASE_SRC (src), -1);
414 
415   GST_OBJECT_LOCK (src);
416   result = src->priv->slave_method;
417   GST_OBJECT_UNLOCK (src);
418 
419   return result;
420 }
421 
422 static void
gst_audio_base_src_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)423 gst_audio_base_src_set_property (GObject * object, guint prop_id,
424     const GValue * value, GParamSpec * pspec)
425 {
426   GstAudioBaseSrc *src;
427 
428   src = GST_AUDIO_BASE_SRC (object);
429 
430   switch (prop_id) {
431     case PROP_BUFFER_TIME:
432       src->buffer_time = g_value_get_int64 (value);
433       break;
434     case PROP_LATENCY_TIME:
435       src->latency_time = g_value_get_int64 (value);
436       break;
437     case PROP_PROVIDE_CLOCK:
438       gst_audio_base_src_set_provide_clock (src, g_value_get_boolean (value));
439       break;
440     case PROP_SLAVE_METHOD:
441       gst_audio_base_src_set_slave_method (src, g_value_get_enum (value));
442       break;
443     default:
444       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
445       break;
446   }
447 }
448 
449 static void
gst_audio_base_src_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)450 gst_audio_base_src_get_property (GObject * object, guint prop_id,
451     GValue * value, GParamSpec * pspec)
452 {
453   GstAudioBaseSrc *src;
454 
455   src = GST_AUDIO_BASE_SRC (object);
456 
457   switch (prop_id) {
458     case PROP_BUFFER_TIME:
459       g_value_set_int64 (value, src->buffer_time);
460       break;
461     case PROP_LATENCY_TIME:
462       g_value_set_int64 (value, src->latency_time);
463       break;
464     case PROP_ACTUAL_BUFFER_TIME:
465       GST_OBJECT_LOCK (src);
466       if (src->ringbuffer && src->ringbuffer->acquired)
467         g_value_set_int64 (value, src->ringbuffer->spec.buffer_time);
468       else
469         g_value_set_int64 (value, DEFAULT_ACTUAL_BUFFER_TIME);
470       GST_OBJECT_UNLOCK (src);
471       break;
472     case PROP_ACTUAL_LATENCY_TIME:
473       GST_OBJECT_LOCK (src);
474       if (src->ringbuffer && src->ringbuffer->acquired)
475         g_value_set_int64 (value, src->ringbuffer->spec.latency_time);
476       else
477         g_value_set_int64 (value, DEFAULT_ACTUAL_LATENCY_TIME);
478       GST_OBJECT_UNLOCK (src);
479       break;
480     case PROP_PROVIDE_CLOCK:
481       g_value_set_boolean (value, gst_audio_base_src_get_provide_clock (src));
482       break;
483     case PROP_SLAVE_METHOD:
484       g_value_set_enum (value, gst_audio_base_src_get_slave_method (src));
485       break;
486     default:
487       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
488       break;
489   }
490 }
491 
492 static GstCaps *
gst_audio_base_src_fixate(GstBaseSrc * bsrc,GstCaps * caps)493 gst_audio_base_src_fixate (GstBaseSrc * bsrc, GstCaps * caps)
494 {
495   GstStructure *s;
496 
497   caps = gst_caps_make_writable (caps);
498 
499   s = gst_caps_get_structure (caps, 0);
500 
501   /* fields for all formats */
502   gst_structure_fixate_field_nearest_int (s, "rate", GST_AUDIO_DEF_RATE);
503   gst_structure_fixate_field_nearest_int (s, "channels",
504       GST_AUDIO_DEF_CHANNELS);
505   gst_structure_fixate_field_string (s, "format", GST_AUDIO_DEF_FORMAT);
506 
507   caps = GST_BASE_SRC_CLASS (parent_class)->fixate (bsrc, caps);
508 
509   return caps;
510 }
511 
512 static gboolean
gst_audio_base_src_setcaps(GstBaseSrc * bsrc,GstCaps * caps)513 gst_audio_base_src_setcaps (GstBaseSrc * bsrc, GstCaps * caps)
514 {
515   GstAudioBaseSrc *src = GST_AUDIO_BASE_SRC (bsrc);
516   GstAudioRingBufferSpec *spec;
517   gint bpf, rate;
518 
519   spec = &src->ringbuffer->spec;
520 
521   if (G_UNLIKELY (gst_audio_ring_buffer_is_acquired (src->ringbuffer)
522           && gst_caps_is_equal (spec->caps, caps))) {
523     GST_DEBUG_OBJECT (src,
524         "Ringbuffer caps haven't changed, skipping reconfiguration");
525     return TRUE;
526   }
527 
528   GST_DEBUG ("release old ringbuffer");
529   gst_audio_ring_buffer_release (src->ringbuffer);
530 
531   spec->buffer_time = src->buffer_time;
532   spec->latency_time = src->latency_time;
533 
534   GST_OBJECT_LOCK (src);
535   if (!gst_audio_ring_buffer_parse_caps (spec, caps)) {
536     GST_OBJECT_UNLOCK (src);
537     goto parse_error;
538   }
539 
540   bpf = GST_AUDIO_INFO_BPF (&spec->info);
541   rate = GST_AUDIO_INFO_RATE (&spec->info);
542 
543   /* calculate suggested segsize and segtotal */
544   spec->segsize = rate * bpf * spec->latency_time / GST_MSECOND;
545   /* Round to an integer number of samples */
546   spec->segsize -= spec->segsize % bpf;
547   spec->segtotal = spec->buffer_time / spec->latency_time;
548 
549   GST_OBJECT_UNLOCK (src);
550 
551   gst_audio_ring_buffer_debug_spec_buff (spec);
552 
553   GST_DEBUG ("acquire new ringbuffer");
554 
555   if (!gst_audio_ring_buffer_acquire (src->ringbuffer, spec))
556     goto acquire_error;
557 
558   /* calculate actual latency and buffer times */
559   spec->latency_time = spec->segsize * GST_MSECOND / (rate * bpf);
560   spec->buffer_time =
561       spec->segtotal * spec->segsize * GST_MSECOND / (rate * bpf);
562 
563   gst_audio_ring_buffer_debug_spec_buff (spec);
564 
565   g_object_notify (G_OBJECT (src), "actual-buffer-time");
566   g_object_notify (G_OBJECT (src), "actual-latency-time");
567 
568   gst_element_post_message (GST_ELEMENT_CAST (bsrc),
569       gst_message_new_latency (GST_OBJECT (bsrc)));
570 
571   return TRUE;
572 
573   /* ERRORS */
574 parse_error:
575   {
576     GST_DEBUG ("could not parse caps");
577     return FALSE;
578   }
579 acquire_error:
580   {
581     GST_DEBUG ("could not acquire ringbuffer");
582     return FALSE;
583   }
584 }
585 
586 static void
gst_audio_base_src_get_times(GstBaseSrc * bsrc,GstBuffer * buffer,GstClockTime * start,GstClockTime * end)587 gst_audio_base_src_get_times (GstBaseSrc * bsrc, GstBuffer * buffer,
588     GstClockTime * start, GstClockTime * end)
589 {
590   /* No need to sync to a clock here. We schedule the samples based
591    * on our own clock for the moment. */
592   *start = GST_CLOCK_TIME_NONE;
593   *end = GST_CLOCK_TIME_NONE;
594 }
595 
596 static gboolean
gst_audio_base_src_query(GstBaseSrc * bsrc,GstQuery * query)597 gst_audio_base_src_query (GstBaseSrc * bsrc, GstQuery * query)
598 {
599   GstAudioBaseSrc *src = GST_AUDIO_BASE_SRC (bsrc);
600   gboolean res = FALSE;
601 
602   switch (GST_QUERY_TYPE (query)) {
603     case GST_QUERY_LATENCY:
604     {
605       GstClockTime min_latency, max_latency;
606       GstAudioRingBufferSpec *spec;
607       gint bpf, rate;
608 
609       GST_OBJECT_LOCK (src);
610       if (G_UNLIKELY (src->ringbuffer == NULL
611               || src->ringbuffer->spec.info.rate == 0)) {
612         GST_OBJECT_UNLOCK (src);
613         goto done;
614       }
615 
616       spec = &src->ringbuffer->spec;
617       rate = GST_AUDIO_INFO_RATE (&spec->info);
618       bpf = GST_AUDIO_INFO_BPF (&spec->info);
619 
620       /* we have at least 1 segment of latency */
621       min_latency =
622           gst_util_uint64_scale_int (spec->segsize, GST_SECOND, rate * bpf);
623       /* we cannot delay more than the buffersize else we lose data */
624       max_latency =
625           gst_util_uint64_scale_int (spec->segtotal * spec->segsize, GST_SECOND,
626           rate * bpf);
627       GST_OBJECT_UNLOCK (src);
628 
629       GST_DEBUG_OBJECT (src,
630           "report latency min %" GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
631           GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
632 
633       /* we are always live, the min latency is 1 segment and the max latency is
634        * the complete buffer of segments. */
635       gst_query_set_latency (query, TRUE, min_latency, max_latency);
636 
637       res = TRUE;
638       break;
639     }
640     case GST_QUERY_SCHEDULING:
641     {
642       /* We allow limited pull base operation. Basically, pulling can be
643        * done on any number of bytes as long as the offset is -1 or
644        * sequentially increasing. */
645       gst_query_set_scheduling (query, GST_SCHEDULING_FLAG_SEQUENTIAL, 1, -1,
646           0);
647       gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
648       gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
649 
650       res = TRUE;
651       break;
652     }
653     default:
654       res = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
655       break;
656   }
657 done:
658   return res;
659 }
660 
661 static gboolean
gst_audio_base_src_event(GstBaseSrc * bsrc,GstEvent * event)662 gst_audio_base_src_event (GstBaseSrc * bsrc, GstEvent * event)
663 {
664   GstAudioBaseSrc *src = GST_AUDIO_BASE_SRC (bsrc);
665   gboolean res, forward;
666 
667   res = FALSE;
668   forward = TRUE;
669 
670   switch (GST_EVENT_TYPE (event)) {
671     case GST_EVENT_FLUSH_START:
672       GST_DEBUG_OBJECT (bsrc, "flush-start");
673       gst_audio_ring_buffer_pause (src->ringbuffer);
674       gst_audio_ring_buffer_clear_all (src->ringbuffer);
675       break;
676     case GST_EVENT_FLUSH_STOP:
677       GST_DEBUG_OBJECT (bsrc, "flush-stop");
678       /* always resync on sample after a flush */
679       src->next_sample = -1;
680       gst_audio_ring_buffer_clear_all (src->ringbuffer);
681       break;
682     case GST_EVENT_SEEK:
683       GST_DEBUG_OBJECT (bsrc, "refuse to seek");
684       forward = FALSE;
685       break;
686     default:
687       GST_DEBUG_OBJECT (bsrc, "forward event %p", event);
688       break;
689   }
690   if (forward)
691     res = GST_BASE_SRC_CLASS (parent_class)->event (bsrc, event);
692 
693   return res;
694 }
695 
696 /* Get the next offset in the ringbuffer for reading samples.
697  * If the next sample is too far away, this function will position itself to the
698  * next most recent sample, creating discontinuity */
699 static guint64
gst_audio_base_src_get_offset(GstAudioBaseSrc * src)700 gst_audio_base_src_get_offset (GstAudioBaseSrc * src)
701 {
702   guint64 sample;
703   gint readseg, segdone, segtotal, sps;
704   gint diff;
705 
706   /* assume we can append to the previous sample */
707   sample = src->next_sample;
708 
709   sps = src->ringbuffer->samples_per_seg;
710   segtotal = src->ringbuffer->spec.segtotal;
711 
712   /* get the currently processed segment */
713   segdone = g_atomic_int_get (&src->ringbuffer->segdone)
714       - src->ringbuffer->segbase;
715 
716   if (sample != -1) {
717     GST_DEBUG_OBJECT (src, "at segment %d and sample %" G_GUINT64_FORMAT,
718         segdone, sample);
719     /* figure out the segment and the offset inside the segment where
720      * the sample should be read from. */
721     readseg = sample / sps;
722 
723     /* See how far away it is from the read segment. Normally, segdone (where
724      * new data is written in the ringbuffer) is bigger than readseg
725      * (where we are reading). */
726     diff = segdone - readseg;
727     if (diff >= segtotal) {
728       GST_DEBUG_OBJECT (src, "dropped, align to segment %d", segdone);
729       /* sample would be dropped, position to next playable position */
730       sample = ((guint64) (segdone)) * sps;
731     }
732   } else {
733     /* no previous sample, go to the current position */
734     GST_DEBUG_OBJECT (src, "first sample, align to current %d", segdone);
735     sample = ((guint64) (segdone)) * sps;
736     readseg = segdone;
737   }
738 
739   GST_DEBUG_OBJECT (src,
740       "reading from %d, we are at %d, sample %" G_GUINT64_FORMAT, readseg,
741       segdone, sample);
742 
743   return sample;
744 }
745 
746 static GstFlowReturn
gst_audio_base_src_create(GstBaseSrc * bsrc,guint64 offset,guint length,GstBuffer ** outbuf)747 gst_audio_base_src_create (GstBaseSrc * bsrc, guint64 offset, guint length,
748     GstBuffer ** outbuf)
749 {
750   GstFlowReturn ret;
751   GstAudioBaseSrc *src = GST_AUDIO_BASE_SRC (bsrc);
752   GstBuffer *buf;
753   GstMapInfo info;
754   guint8 *ptr;
755   guint samples, total_samples;
756   guint64 sample;
757   gint bpf, rate;
758   GstAudioRingBuffer *ringbuffer;
759   GstAudioRingBufferSpec *spec;
760   guint read;
761   GstClockTime timestamp, duration;
762   GstClockTime rb_timestamp = GST_CLOCK_TIME_NONE;
763   GstClock *clock;
764   gboolean first;
765   gboolean first_sample = src->next_sample == -1;
766 
767   ringbuffer = src->ringbuffer;
768   spec = &ringbuffer->spec;
769 
770   if (G_UNLIKELY (!gst_audio_ring_buffer_is_acquired (ringbuffer)))
771     goto wrong_state;
772 
773   bpf = GST_AUDIO_INFO_BPF (&spec->info);
774   rate = GST_AUDIO_INFO_RATE (&spec->info);
775 
776   if ((length == 0 && bsrc->blocksize == 0) || length == -1)
777     /* no length given, use the default segment size */
778     length = spec->segsize;
779   else
780     /* make sure we round down to an integral number of samples */
781     length -= length % bpf;
782 
783   /* figure out the offset in the ringbuffer */
784   if (G_UNLIKELY (offset != -1)) {
785     sample = offset / bpf;
786     /* if a specific offset was given it must be the next sequential
787      * offset we expect or we fail for now. */
788     if (src->next_sample != -1 && sample != src->next_sample)
789       goto wrong_offset;
790   } else {
791     /* Calculate the sequentially-next sample we need to read. This can jump and
792      * create a DISCONT. */
793     sample = gst_audio_base_src_get_offset (src);
794   }
795 
796   GST_DEBUG_OBJECT (src, "reading from sample %" G_GUINT64_FORMAT " length %u",
797       sample, length);
798 
799   /* get the number of samples to read */
800   total_samples = samples = length / bpf;
801 
802   /* use the basesrc allocation code to use bufferpools or custom allocators */
803   ret = GST_BASE_SRC_CLASS (parent_class)->alloc (bsrc, offset, length, &buf);
804   if (G_UNLIKELY (ret != GST_FLOW_OK))
805     goto alloc_failed;
806 
807   gst_buffer_map (buf, &info, GST_MAP_WRITE);
808   ptr = info.data;
809   first = TRUE;
810   do {
811     GstClockTime tmp_ts = GST_CLOCK_TIME_NONE;
812 
813     read =
814         gst_audio_ring_buffer_read (ringbuffer, sample, ptr, samples, &tmp_ts);
815     if (first && GST_CLOCK_TIME_IS_VALID (tmp_ts)) {
816       first = FALSE;
817       rb_timestamp = tmp_ts;
818     }
819     GST_DEBUG_OBJECT (src, "read %u of %u", read, samples);
820     /* if we read all, we're done */
821     if (read == samples)
822       break;
823 
824     if (g_atomic_int_get (&ringbuffer->state) ==
825         GST_AUDIO_RING_BUFFER_STATE_ERROR)
826       goto got_error;
827 
828     /* else something interrupted us and we wait for playing again. */
829     GST_DEBUG_OBJECT (src, "wait playing");
830     if (gst_base_src_wait_playing (bsrc) != GST_FLOW_OK)
831       goto stopped;
832 
833     GST_DEBUG_OBJECT (src, "continue playing");
834 
835     /* read next samples */
836     sample += read;
837     samples -= read;
838     ptr += read * bpf;
839   } while (TRUE);
840   gst_buffer_unmap (buf, &info);
841 
842   /* mark discontinuity if needed */
843   if (G_UNLIKELY (sample != src->next_sample) && src->next_sample != -1) {
844     GST_WARNING_OBJECT (src,
845         "create DISCONT of %" G_GUINT64_FORMAT " samples at sample %"
846         G_GUINT64_FORMAT, sample - src->next_sample, sample);
847     GST_ELEMENT_WARNING (src, CORE, CLOCK,
848         (_("Can't record audio fast enough")),
849         ("Dropped %" G_GUINT64_FORMAT " samples. This is most likely because "
850             "downstream can't keep up and is consuming samples too slowly.",
851             sample - src->next_sample));
852     GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
853   }
854 
855   src->next_sample = sample + samples;
856 
857   /* get the normal timestamp to get the duration. */
858   timestamp = gst_util_uint64_scale_int (sample, GST_SECOND, rate);
859   duration = gst_util_uint64_scale_int (src->next_sample, GST_SECOND,
860       rate) - timestamp;
861 
862   GST_OBJECT_LOCK (src);
863   if (!(clock = GST_ELEMENT_CLOCK (src)))
864     goto no_sync;
865 
866   if (!GST_CLOCK_TIME_IS_VALID (rb_timestamp) && clock != src->clock) {
867     /* we are slaved, check how to handle this */
868     switch (src->priv->slave_method) {
869       case GST_AUDIO_BASE_SRC_SLAVE_RESAMPLE:
870         /* Not implemented, use skew algorithm. This algorithm should
871          * work on the readout pointer and produce more or less samples based
872          * on the clock drift */
873       case GST_AUDIO_BASE_SRC_SLAVE_SKEW:
874       {
875         GstClockTime running_time;
876         GstClockTime base_time;
877         GstClockTime current_time;
878         guint64 running_time_sample;
879         gint running_time_segment;
880         gint last_read_segment;
881         gint segment_skew;
882         gint sps;
883         gint segments_written;
884         gint last_written_segment;
885 
886         /* get the amount of segments written from the device by now */
887         segments_written = g_atomic_int_get (&ringbuffer->segdone);
888 
889         /* subtract the base to segments_written to get the number of the
890          * last written segment in the ringbuffer
891          * (one segment written = segment 0) */
892         last_written_segment = segments_written - ringbuffer->segbase - 1;
893 
894         /* samples per segment */
895         sps = ringbuffer->samples_per_seg;
896 
897         /* get the current time */
898         current_time = gst_clock_get_time (clock);
899 
900         /* get the basetime */
901         base_time = GST_ELEMENT_CAST (src)->base_time;
902 
903         /* get the running_time */
904         running_time = current_time - base_time;
905 
906         /* the running_time converted to a sample
907          * (relative to the ringbuffer) */
908         running_time_sample =
909             gst_util_uint64_scale_int (running_time, rate, GST_SECOND);
910 
911         /* the segmentnr corresponding to running_time, round down */
912         running_time_segment = running_time_sample / sps;
913 
914         /* the segment currently read from the ringbuffer */
915         last_read_segment = sample / sps;
916 
917         /* the skew we have between running_time and the ringbuffertime
918          * (last written to) */
919         segment_skew = running_time_segment - last_written_segment;
920 
921         GST_DEBUG_OBJECT (bsrc,
922             "\n running_time                                              = %"
923             GST_TIME_FORMAT
924             "\n timestamp                                                  = %"
925             GST_TIME_FORMAT
926             "\n running_time_segment                                       = %d"
927             "\n last_written_segment                                       = %d"
928             "\n segment_skew (running time segment - last_written_segment) = %d"
929             "\n last_read_segment                                          = %d",
930             GST_TIME_ARGS (running_time), GST_TIME_ARGS (timestamp),
931             running_time_segment, last_written_segment, segment_skew,
932             last_read_segment);
933 
934         /* Resync the ringbuffer if:
935          *
936          * 1. We are more than the length of the ringbuffer behind.
937          *    The length of the ringbuffer then gets to dictate
938          *    the threshold for what is considered "too late"
939          *
940          * 2. If this is our first buffer.
941          *    We know that we should catch up to running_time
942          *    the first time we are ran.
943          */
944         if ((segment_skew >= ringbuffer->spec.segtotal) ||
945             (last_read_segment == 0) || first_sample) {
946           gint new_read_segment;
947           gint segment_diff;
948           guint64 new_sample;
949 
950           /* the difference between running_time and the last written segment */
951           segment_diff = running_time_segment - last_written_segment;
952 
953           /* advance the ringbuffer */
954           gst_audio_ring_buffer_advance (ringbuffer, segment_diff);
955 
956           /* we move the  new read segment to the last known written segment */
957           new_read_segment =
958               g_atomic_int_get (&ringbuffer->segdone) - ringbuffer->segbase;
959 
960           /* we calculate the new sample value */
961           new_sample = ((guint64) new_read_segment) * sps;
962 
963           /* and get the relative time to this -> our new timestamp */
964           timestamp = gst_util_uint64_scale_int (new_sample, GST_SECOND, rate);
965 
966           /* we update the next sample accordingly */
967           src->next_sample = new_sample + samples;
968 
969           GST_DEBUG_OBJECT (bsrc,
970               "Timeshifted the ringbuffer with %d segments: "
971               "Updating the timestamp to %" GST_TIME_FORMAT ", "
972               "and src->next_sample to %" G_GUINT64_FORMAT, segment_diff,
973               GST_TIME_ARGS (timestamp), src->next_sample);
974         }
975         break;
976       }
977       case GST_AUDIO_BASE_SRC_SLAVE_RE_TIMESTAMP:
978       {
979         GstClockTime base_time, latency;
980 
981         /* We are slaved to another clock. Take running time of the pipeline
982          * clock and timestamp against it. Somebody else in the pipeline should
983          * figure out the clock drift. We keep the duration we calculated
984          * above. */
985         timestamp = gst_clock_get_time (clock);
986         base_time = GST_ELEMENT_CAST (src)->base_time;
987 
988         if (GST_CLOCK_DIFF (timestamp, base_time) < 0)
989           timestamp -= base_time;
990         else
991           timestamp = 0;
992 
993         /* subtract latency */
994         latency = gst_util_uint64_scale_int (total_samples, GST_SECOND, rate);
995         if (timestamp > latency)
996           timestamp -= latency;
997         else
998           timestamp = 0;
999       }
1000       case GST_AUDIO_BASE_SRC_SLAVE_NONE:
1001         break;
1002     }
1003   } else {
1004     GstClockTime base_time;
1005 
1006     if (GST_CLOCK_TIME_IS_VALID (rb_timestamp)) {
1007       /* the read method returned a timestamp so we use this instead */
1008       timestamp = rb_timestamp;
1009     } else {
1010       /* to get the timestamp against the clock we also need to add our
1011        * offset */
1012       timestamp = gst_audio_clock_adjust (GST_AUDIO_CLOCK (clock), timestamp);
1013     }
1014 
1015     /* we are not slaved, subtract base_time */
1016     base_time = GST_ELEMENT_CAST (src)->base_time;
1017 
1018     if (GST_CLOCK_DIFF (timestamp, base_time) < 0) {
1019       timestamp -= base_time;
1020       GST_LOG_OBJECT (src,
1021           "buffer timestamp %" GST_TIME_FORMAT " (base_time %" GST_TIME_FORMAT
1022           ")", GST_TIME_ARGS (timestamp), GST_TIME_ARGS (base_time));
1023     } else {
1024       GST_LOG_OBJECT (src,
1025           "buffer timestamp 0, ts %" GST_TIME_FORMAT " <= base_time %"
1026           GST_TIME_FORMAT, GST_TIME_ARGS (timestamp),
1027           GST_TIME_ARGS (base_time));
1028       timestamp = 0;
1029     }
1030   }
1031 
1032 no_sync:
1033   GST_OBJECT_UNLOCK (src);
1034 
1035   GST_BUFFER_PTS (buf) = timestamp;
1036   GST_BUFFER_DURATION (buf) = duration;
1037   GST_BUFFER_OFFSET (buf) = sample;
1038   GST_BUFFER_OFFSET_END (buf) = sample + samples;
1039 
1040   *outbuf = buf;
1041 
1042   GST_LOG_OBJECT (src, "Pushed buffer timestamp %" GST_TIME_FORMAT,
1043       GST_TIME_ARGS (GST_BUFFER_PTS (buf)));
1044 
1045   return GST_FLOW_OK;
1046 
1047   /* ERRORS */
1048 wrong_state:
1049   {
1050     GST_DEBUG_OBJECT (src, "ringbuffer in wrong state");
1051     return GST_FLOW_FLUSHING;
1052   }
1053 wrong_offset:
1054   {
1055     GST_ELEMENT_ERROR (src, RESOURCE, SEEK,
1056         (NULL), ("resource can only be operated on sequentially but offset %"
1057             G_GUINT64_FORMAT " was given", offset));
1058     return GST_FLOW_ERROR;
1059   }
1060 alloc_failed:
1061   {
1062     GST_DEBUG_OBJECT (src, "alloc failed: %s", gst_flow_get_name (ret));
1063     return ret;
1064   }
1065 stopped:
1066   {
1067     gst_buffer_unmap (buf, &info);
1068     gst_buffer_unref (buf);
1069     GST_DEBUG_OBJECT (src, "ringbuffer stopped");
1070     return GST_FLOW_FLUSHING;
1071   }
1072 got_error:
1073   {
1074     gst_buffer_unmap (buf, &info);
1075     gst_buffer_unref (buf);
1076     GST_DEBUG_OBJECT (src, "ringbuffer was in error state, bailing out");
1077     return GST_FLOW_ERROR;
1078   }
1079 }
1080 
1081 /**
1082  * gst_audio_base_src_create_ringbuffer:
1083  * @src: a #GstAudioBaseSrc.
1084  *
1085  * Create and return the #GstAudioRingBuffer for @src. This function will call
1086  * the ::create_ringbuffer vmethod and will set @src as the parent of the
1087  * returned buffer (see gst_object_set_parent()).
1088  *
1089  * Returns: (transfer none): The new ringbuffer of @src.
1090  */
1091 GstAudioRingBuffer *
gst_audio_base_src_create_ringbuffer(GstAudioBaseSrc * src)1092 gst_audio_base_src_create_ringbuffer (GstAudioBaseSrc * src)
1093 {
1094   GstAudioBaseSrcClass *bclass;
1095   GstAudioRingBuffer *buffer = NULL;
1096 
1097   bclass = GST_AUDIO_BASE_SRC_GET_CLASS (src);
1098   if (bclass->create_ringbuffer)
1099     buffer = bclass->create_ringbuffer (src);
1100 
1101   if (G_LIKELY (buffer))
1102     gst_object_set_parent (GST_OBJECT_CAST (buffer), GST_OBJECT_CAST (src));
1103 
1104   return buffer;
1105 }
1106 
1107 static GstStateChangeReturn
gst_audio_base_src_change_state(GstElement * element,GstStateChange transition)1108 gst_audio_base_src_change_state (GstElement * element,
1109     GstStateChange transition)
1110 {
1111   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1112   GstAudioBaseSrc *src = GST_AUDIO_BASE_SRC (element);
1113 
1114   switch (transition) {
1115     case GST_STATE_CHANGE_NULL_TO_READY:{
1116       GstAudioRingBuffer *rb;
1117 
1118       GST_DEBUG_OBJECT (src, "NULL->READY");
1119       gst_audio_clock_reset (GST_AUDIO_CLOCK (src->clock), 0);
1120       rb = gst_audio_base_src_create_ringbuffer (src);
1121       if (rb == NULL)
1122         goto create_failed;
1123 
1124       GST_OBJECT_LOCK (src);
1125       src->ringbuffer = rb;
1126       GST_OBJECT_UNLOCK (src);
1127 
1128       if (!gst_audio_ring_buffer_open_device (src->ringbuffer)) {
1129         GST_OBJECT_LOCK (src);
1130         gst_object_unparent (GST_OBJECT_CAST (src->ringbuffer));
1131         src->ringbuffer = NULL;
1132         GST_OBJECT_UNLOCK (src);
1133         goto open_failed;
1134       }
1135       break;
1136     }
1137     case GST_STATE_CHANGE_READY_TO_PAUSED:
1138       GST_DEBUG_OBJECT (src, "READY->PAUSED");
1139       src->next_sample = -1;
1140       gst_audio_ring_buffer_set_flushing (src->ringbuffer, FALSE);
1141       gst_audio_ring_buffer_may_start (src->ringbuffer, FALSE);
1142       /* Only post clock-provide messages if this is the clock that
1143        * we've created. If the subclass has overridden it the subclass
1144        * should post this messages whenever necessary */
1145       if (src->clock && GST_IS_AUDIO_CLOCK (src->clock) &&
1146           GST_AUDIO_CLOCK_CAST (src->clock)->func ==
1147           (GstAudioClockGetTimeFunc) gst_audio_base_src_get_time)
1148         gst_element_post_message (element,
1149             gst_message_new_clock_provide (GST_OBJECT_CAST (element),
1150                 src->clock, TRUE));
1151       break;
1152     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1153       GST_DEBUG_OBJECT (src, "PAUSED->PLAYING");
1154       gst_audio_ring_buffer_may_start (src->ringbuffer, TRUE);
1155       break;
1156     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1157       GST_DEBUG_OBJECT (src, "PLAYING->PAUSED");
1158       gst_audio_ring_buffer_may_start (src->ringbuffer, FALSE);
1159       gst_audio_ring_buffer_pause (src->ringbuffer);
1160       break;
1161     case GST_STATE_CHANGE_PAUSED_TO_READY:
1162       GST_DEBUG_OBJECT (src, "PAUSED->READY");
1163       /* Only post clock-lost messages if this is the clock that
1164        * we've created. If the subclass has overridden it the subclass
1165        * should post this messages whenever necessary */
1166       if (src->clock && GST_IS_AUDIO_CLOCK (src->clock) &&
1167           GST_AUDIO_CLOCK_CAST (src->clock)->func ==
1168           (GstAudioClockGetTimeFunc) gst_audio_base_src_get_time)
1169         gst_element_post_message (element,
1170             gst_message_new_clock_lost (GST_OBJECT_CAST (element), src->clock));
1171       gst_audio_ring_buffer_set_flushing (src->ringbuffer, TRUE);
1172       break;
1173     default:
1174       break;
1175   }
1176 
1177   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1178 
1179   switch (transition) {
1180     case GST_STATE_CHANGE_PAUSED_TO_READY:
1181       GST_DEBUG_OBJECT (src, "PAUSED->READY");
1182       gst_audio_ring_buffer_release (src->ringbuffer);
1183       break;
1184     case GST_STATE_CHANGE_READY_TO_NULL:
1185       GST_DEBUG_OBJECT (src, "READY->NULL");
1186       gst_audio_ring_buffer_close_device (src->ringbuffer);
1187       GST_OBJECT_LOCK (src);
1188       gst_object_unparent (GST_OBJECT_CAST (src->ringbuffer));
1189       src->ringbuffer = NULL;
1190       GST_OBJECT_UNLOCK (src);
1191       break;
1192     default:
1193       break;
1194   }
1195 
1196   return ret;
1197 
1198   /* ERRORS */
1199 create_failed:
1200   {
1201     /* subclass must post a meaningful error message */
1202     GST_DEBUG_OBJECT (src, "create failed");
1203     return GST_STATE_CHANGE_FAILURE;
1204   }
1205 open_failed:
1206   {
1207     /* subclass must post a meaningful error message */
1208     GST_DEBUG_OBJECT (src, "open failed");
1209     return GST_STATE_CHANGE_FAILURE;
1210   }
1211 
1212 }
1213 
1214 static gboolean
gst_audio_base_src_post_message(GstElement * element,GstMessage * message)1215 gst_audio_base_src_post_message (GstElement * element, GstMessage * message)
1216 {
1217   GstAudioBaseSrc *src = GST_AUDIO_BASE_SRC (element);
1218   gboolean ret;
1219 
1220   if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ERROR && src->ringbuffer) {
1221     GstAudioRingBuffer *ringbuffer;
1222 
1223     GST_INFO_OBJECT (element, "subclass posted error");
1224 
1225     ringbuffer = gst_object_ref (src->ringbuffer);
1226 
1227     /* post message first before signalling the error to the ringbuffer, to
1228      * make sure it ends up on the bus before the generic basesrc internal
1229      * flow error message */
1230     ret = GST_ELEMENT_CLASS (parent_class)->post_message (element, message);
1231 
1232     g_atomic_int_set (&ringbuffer->state, GST_AUDIO_RING_BUFFER_STATE_ERROR);
1233     GST_AUDIO_RING_BUFFER_SIGNAL (ringbuffer);
1234     gst_object_unref (ringbuffer);
1235   } else {
1236     ret = GST_ELEMENT_CLASS (parent_class)->post_message (element, message);
1237   }
1238   return ret;
1239 }
1240