upload tizen1.0 source
[framework/multimedia/gst-plugins-good0.10.git] / ext / pulse / pulsesrc.c
1 /*
2  *  GStreamer pulseaudio plugin
3  *
4  *  Copyright (c) 2004-2008 Lennart Poettering
5  *
6  *  gst-pulse is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU Lesser General Public License as
8  *  published by the Free Software Foundation; either version 2.1 of the
9  *  License, or (at your option) any later version.
10  *
11  *  gst-pulse is distributed in the hope that it will be useful, but
12  *  WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with gst-pulse; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  *  USA.
20  */
21
22 /**
23  * SECTION:element-pulsesrc
24  * @see_also: pulsesink, pulsemixer
25  *
26  * This element captures audio from a
27  * <ulink href="http://www.pulseaudio.org">PulseAudio sound server</ulink>.
28  *
29  * <refsect2>
30  * <title>Example pipelines</title>
31  * |[
32  * gst-launch -v pulsesrc ! audioconvert ! vorbisenc ! oggmux ! filesink location=alsasrc.ogg
33  * ]| Record from a sound card using pulseaudio and encode to Ogg/Vorbis.
34  * </refsect2>
35  */
36
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40
41 #include <string.h>
42 #include <stdio.h>
43
44 #include <gst/base/gstbasesrc.h>
45 #include <gst/gsttaglist.h>
46
47 #include "pulsesrc.h"
48 #include "pulseutil.h"
49 #include "pulsemixerctrl.h"
50
51 GST_DEBUG_CATEGORY_EXTERN (pulse_debug);
52 #define GST_CAT_DEFAULT pulse_debug
53
54 #define DEFAULT_SERVER            NULL
55 #define DEFAULT_DEVICE            NULL
56 #define DEFAULT_DEVICE_NAME       NULL
57
58 enum
59 {
60   PROP_0,
61   PROP_SERVER,
62   PROP_DEVICE,
63   PROP_DEVICE_NAME,
64   PROP_CLIENT,
65   PROP_STREAM_PROPERTIES,
66   PROP_LAST
67 };
68
69 static void gst_pulsesrc_destroy_stream (GstPulseSrc * pulsesrc);
70 static void gst_pulsesrc_destroy_context (GstPulseSrc * pulsesrc);
71
72 static void gst_pulsesrc_set_property (GObject * object, guint prop_id,
73     const GValue * value, GParamSpec * pspec);
74 static void gst_pulsesrc_get_property (GObject * object, guint prop_id,
75     GValue * value, GParamSpec * pspec);
76 static void gst_pulsesrc_finalize (GObject * object);
77
78 static gboolean gst_pulsesrc_open (GstAudioSrc * asrc);
79
80 static gboolean gst_pulsesrc_close (GstAudioSrc * asrc);
81
82 static gboolean gst_pulsesrc_prepare (GstAudioSrc * asrc,
83     GstRingBufferSpec * spec);
84
85 static gboolean gst_pulsesrc_unprepare (GstAudioSrc * asrc);
86
87 static guint gst_pulsesrc_read (GstAudioSrc * asrc, gpointer data,
88     guint length);
89 static guint gst_pulsesrc_delay (GstAudioSrc * asrc);
90
91 static void gst_pulsesrc_reset (GstAudioSrc * src);
92
93 static gboolean gst_pulsesrc_negotiate (GstBaseSrc * basesrc);
94
95 static GstStateChangeReturn gst_pulsesrc_change_state (GstElement *
96     element, GstStateChange transition);
97
98 static void gst_pulsesrc_init_interfaces (GType type);
99
100 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
101 # define ENDIANNESS   "LITTLE_ENDIAN, BIG_ENDIAN"
102 #else
103 # define ENDIANNESS   "BIG_ENDIAN, LITTLE_ENDIAN"
104 #endif
105
106 GST_IMPLEMENT_PULSEMIXER_CTRL_METHODS (GstPulseSrc, gst_pulsesrc);
107 GST_IMPLEMENT_PULSEPROBE_METHODS (GstPulseSrc, gst_pulsesrc);
108 GST_BOILERPLATE_FULL (GstPulseSrc, gst_pulsesrc, GstAudioSrc,
109     GST_TYPE_AUDIO_SRC, gst_pulsesrc_init_interfaces);
110
111 static gboolean
112 gst_pulsesrc_interface_supported (GstImplementsInterface *
113     iface, GType interface_type)
114 {
115   GstPulseSrc *this = GST_PULSESRC_CAST (iface);
116
117   if (interface_type == GST_TYPE_MIXER && this->mixer)
118     return TRUE;
119
120   if (interface_type == GST_TYPE_PROPERTY_PROBE && this->probe)
121     return TRUE;
122
123   return FALSE;
124 }
125
126 static void
127 gst_pulsesrc_implements_interface_init (GstImplementsInterfaceClass * klass)
128 {
129   klass->supported = gst_pulsesrc_interface_supported;
130 }
131
132 static void
133 gst_pulsesrc_init_interfaces (GType type)
134 {
135   static const GInterfaceInfo implements_iface_info = {
136     (GInterfaceInitFunc) gst_pulsesrc_implements_interface_init,
137     NULL,
138     NULL,
139   };
140   static const GInterfaceInfo mixer_iface_info = {
141     (GInterfaceInitFunc) gst_pulsesrc_mixer_interface_init,
142     NULL,
143     NULL,
144   };
145   static const GInterfaceInfo probe_iface_info = {
146     (GInterfaceInitFunc) gst_pulsesrc_property_probe_interface_init,
147     NULL,
148     NULL,
149   };
150
151   g_type_add_interface_static (type, GST_TYPE_IMPLEMENTS_INTERFACE,
152       &implements_iface_info);
153   g_type_add_interface_static (type, GST_TYPE_MIXER, &mixer_iface_info);
154   g_type_add_interface_static (type, GST_TYPE_PROPERTY_PROBE,
155       &probe_iface_info);
156 }
157
158 static void
159 gst_pulsesrc_base_init (gpointer g_class)
160 {
161
162   static GstStaticPadTemplate pad_template = GST_STATIC_PAD_TEMPLATE ("src",
163       GST_PAD_SRC,
164       GST_PAD_ALWAYS,
165       GST_STATIC_CAPS ("audio/x-raw-int, "
166           "endianness = (int) { " ENDIANNESS " }, "
167           "signed = (boolean) TRUE, "
168           "width = (int) 16, "
169           "depth = (int) 16, "
170           "rate = (int) [ 1, MAX ], "
171           "channels = (int) [ 1, 32 ];"
172           "audio/x-raw-float, "
173           "endianness = (int) { " ENDIANNESS " }, "
174           "width = (int) 32, "
175           "rate = (int) [ 1, MAX ], "
176           "channels = (int) [ 1, 32 ];"
177           "audio/x-raw-int, "
178           "endianness = (int) { " ENDIANNESS " }, "
179           "signed = (boolean) TRUE, "
180           "width = (int) 32, "
181           "depth = (int) 32, "
182           "rate = (int) [ 1, MAX ], "
183           "channels = (int) [ 1, 32 ];"
184           "audio/x-raw-int, "
185           "signed = (boolean) FALSE, "
186           "width = (int) 8, "
187           "depth = (int) 8, "
188           "rate = (int) [ 1, MAX ], "
189           "channels = (int) [ 1, 32 ];"
190           "audio/x-alaw, "
191           "rate = (int) [ 1, MAX], "
192           "channels = (int) [ 1, 32 ];"
193           "audio/x-mulaw, "
194           "rate = (int) [ 1, MAX], " "channels = (int) [ 1, 32 ]")
195       );
196
197   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
198
199   gst_element_class_set_details_simple (element_class,
200       "PulseAudio Audio Source",
201       "Source/Audio",
202       "Captures audio from a PulseAudio server", "Lennart Poettering");
203   gst_element_class_add_pad_template (element_class,
204       gst_static_pad_template_get (&pad_template));
205 }
206
207 static void
208 gst_pulsesrc_class_init (GstPulseSrcClass * klass)
209 {
210   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
211   GstAudioSrcClass *gstaudiosrc_class = GST_AUDIO_SRC_CLASS (klass);
212   GstBaseSrcClass *gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
213   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
214
215   gobject_class->finalize = gst_pulsesrc_finalize;
216   gobject_class->set_property = gst_pulsesrc_set_property;
217   gobject_class->get_property = gst_pulsesrc_get_property;
218
219   gstelement_class->change_state =
220       GST_DEBUG_FUNCPTR (gst_pulsesrc_change_state);
221
222   gstbasesrc_class->negotiate = GST_DEBUG_FUNCPTR (gst_pulsesrc_negotiate);
223
224   gstaudiosrc_class->open = GST_DEBUG_FUNCPTR (gst_pulsesrc_open);
225   gstaudiosrc_class->close = GST_DEBUG_FUNCPTR (gst_pulsesrc_close);
226   gstaudiosrc_class->prepare = GST_DEBUG_FUNCPTR (gst_pulsesrc_prepare);
227   gstaudiosrc_class->unprepare = GST_DEBUG_FUNCPTR (gst_pulsesrc_unprepare);
228   gstaudiosrc_class->read = GST_DEBUG_FUNCPTR (gst_pulsesrc_read);
229   gstaudiosrc_class->delay = GST_DEBUG_FUNCPTR (gst_pulsesrc_delay);
230   gstaudiosrc_class->reset = GST_DEBUG_FUNCPTR (gst_pulsesrc_reset);
231
232   /* Overwrite GObject fields */
233   g_object_class_install_property (gobject_class,
234       PROP_SERVER,
235       g_param_spec_string ("server", "Server",
236           "The PulseAudio server to connect to", DEFAULT_SERVER,
237           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
238
239   g_object_class_install_property (gobject_class, PROP_DEVICE,
240       g_param_spec_string ("device", "Device",
241           "The PulseAudio source device to connect to", DEFAULT_DEVICE,
242           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
243
244   g_object_class_install_property (gobject_class,
245       PROP_DEVICE_NAME,
246       g_param_spec_string ("device-name", "Device name",
247           "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
248           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
249
250   /**
251    * GstPulseSink:client
252    *
253    * The PulseAudio client name to use.
254    *
255    * Since: 0.10.27
256    */
257   g_object_class_install_property (gobject_class,
258       PROP_CLIENT,
259       g_param_spec_string ("client", "Client",
260           "The PulseAudio client_name_to_use", gst_pulse_client_name (),
261           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
262           GST_PARAM_MUTABLE_READY));
263
264   /**
265    * GstPulseSrc:stream-properties
266    *
267    * List of pulseaudio stream properties. A list of defined properties can be
268    * found in the <ulink href="http://0pointer.de/lennart/projects/pulseaudio/doxygen/proplist_8h.html">pulseaudio api docs</ulink>.
269    *
270    * Below is an example for registering as a music application to pulseaudio.
271    * |[
272    * GstStructure *props;
273    *
274    * props = gst_structure_from_string ("props,media.role=music", NULL);
275    * g_object_set (pulse, "stream-properties", props, NULL);
276    * gst_structure_free (props);
277    * ]|
278    *
279    * Since: 0.10.26
280    */
281   g_object_class_install_property (gobject_class,
282       PROP_STREAM_PROPERTIES,
283       g_param_spec_boxed ("stream-properties", "stream properties",
284           "list of pulseaudio stream properties",
285           GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
286 }
287
288 static void
289 gst_pulsesrc_init (GstPulseSrc * pulsesrc, GstPulseSrcClass * klass)
290 {
291   pulsesrc->server = NULL;
292   pulsesrc->device = NULL;
293   pulsesrc->client_name = gst_pulse_client_name ();
294   pulsesrc->device_description = NULL;
295
296   pulsesrc->context = NULL;
297   pulsesrc->stream = NULL;
298
299   pulsesrc->read_buffer = NULL;
300   pulsesrc->read_buffer_length = 0;
301
302 #ifdef HAVE_PULSE_0_9_13
303   pa_sample_spec_init (&pulsesrc->sample_spec);
304 #else
305   pulsesrc->sample_spec.format = PA_SAMPLE_INVALID;
306   pulsesrc->sample_spec.rate = 0;
307   pulsesrc->sample_spec.channels = 0;
308 #endif
309
310   pulsesrc->operation_success = FALSE;
311   pulsesrc->paused = FALSE;
312   pulsesrc->in_read = FALSE;
313
314   pulsesrc->mixer = NULL;
315
316   pulsesrc->properties = NULL;
317   pulsesrc->proplist = NULL;
318
319   pulsesrc->probe = gst_pulseprobe_new (G_OBJECT (pulsesrc), G_OBJECT_GET_CLASS (pulsesrc), PROP_DEVICE, pulsesrc->server, FALSE, TRUE);        /* FALSE for sinks, TRUE for sources */
320
321   /* this should be the default but it isn't yet */
322   gst_base_audio_src_set_slave_method (GST_BASE_AUDIO_SRC (pulsesrc),
323       GST_BASE_AUDIO_SRC_SLAVE_SKEW);
324 }
325
326 static void
327 gst_pulsesrc_destroy_stream (GstPulseSrc * pulsesrc)
328 {
329   if (pulsesrc->stream) {
330     pa_stream_disconnect (pulsesrc->stream);
331     pa_stream_unref (pulsesrc->stream);
332     pulsesrc->stream = NULL;
333   }
334
335   g_free (pulsesrc->device_description);
336   pulsesrc->device_description = NULL;
337 }
338
339 static void
340 gst_pulsesrc_destroy_context (GstPulseSrc * pulsesrc)
341 {
342
343   gst_pulsesrc_destroy_stream (pulsesrc);
344
345   if (pulsesrc->context) {
346     pa_context_disconnect (pulsesrc->context);
347     pa_context_unref (pulsesrc->context);
348     pulsesrc->context = NULL;
349   }
350 }
351
352 static void
353 gst_pulsesrc_finalize (GObject * object)
354 {
355   GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (object);
356
357   g_free (pulsesrc->server);
358   g_free (pulsesrc->device);
359   g_free (pulsesrc->client_name);
360
361   if (pulsesrc->properties)
362     gst_structure_free (pulsesrc->properties);
363   if (pulsesrc->proplist)
364     pa_proplist_free (pulsesrc->proplist);
365
366   if (pulsesrc->mixer) {
367     gst_pulsemixer_ctrl_free (pulsesrc->mixer);
368     pulsesrc->mixer = NULL;
369   }
370
371   if (pulsesrc->probe) {
372     gst_pulseprobe_free (pulsesrc->probe);
373     pulsesrc->probe = NULL;
374   }
375
376   G_OBJECT_CLASS (parent_class)->finalize (object);
377 }
378
379 #define CONTEXT_OK(c) ((c) && PA_CONTEXT_IS_GOOD (pa_context_get_state ((c))))
380 #define STREAM_OK(s) ((s) && PA_STREAM_IS_GOOD (pa_stream_get_state ((s))))
381
382 static gboolean
383 gst_pulsesrc_is_dead (GstPulseSrc * pulsesrc, gboolean check_stream)
384 {
385   if (!CONTEXT_OK (pulsesrc->context))
386     goto error;
387
388   if (check_stream && !STREAM_OK (pulsesrc->stream))
389     goto error;
390
391   return FALSE;
392
393 error:
394   {
395     const gchar *err_str = pulsesrc->context ?
396         pa_strerror (pa_context_errno (pulsesrc->context)) : NULL;
397     GST_ELEMENT_ERROR ((pulsesrc), RESOURCE, FAILED, ("Disconnected: %s",
398             err_str), (NULL));
399     return TRUE;
400   }
401 }
402
403 static void
404 gst_pulsesrc_source_info_cb (pa_context * c, const pa_source_info * i, int eol,
405     void *userdata)
406 {
407   GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
408
409   if (!i)
410     goto done;
411
412   g_free (pulsesrc->device_description);
413   pulsesrc->device_description = g_strdup (i->description);
414
415 done:
416   pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
417 }
418
419 static gchar *
420 gst_pulsesrc_device_description (GstPulseSrc * pulsesrc)
421 {
422   pa_operation *o = NULL;
423   gchar *t;
424
425   if (!pulsesrc->mainloop)
426     goto no_mainloop;
427
428   pa_threaded_mainloop_lock (pulsesrc->mainloop);
429
430   if (!(o = pa_context_get_source_info_by_name (pulsesrc->context,
431               pulsesrc->device, gst_pulsesrc_source_info_cb, pulsesrc))) {
432
433     GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
434         ("pa_stream_get_source_info() failed: %s",
435             pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
436     goto unlock;
437   }
438
439   while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
440
441     if (gst_pulsesrc_is_dead (pulsesrc, FALSE))
442       goto unlock;
443
444     pa_threaded_mainloop_wait (pulsesrc->mainloop);
445   }
446
447 unlock:
448
449   if (o)
450     pa_operation_unref (o);
451
452   t = g_strdup (pulsesrc->device_description);
453
454   pa_threaded_mainloop_unlock (pulsesrc->mainloop);
455
456   return t;
457
458 no_mainloop:
459   {
460     GST_DEBUG_OBJECT (pulsesrc, "have no mainloop");
461     return NULL;
462   }
463 }
464
465 static void
466 gst_pulsesrc_set_property (GObject * object,
467     guint prop_id, const GValue * value, GParamSpec * pspec)
468 {
469
470   GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (object);
471
472   switch (prop_id) {
473     case PROP_SERVER:
474       g_free (pulsesrc->server);
475       pulsesrc->server = g_value_dup_string (value);
476       if (pulsesrc->probe)
477         gst_pulseprobe_set_server (pulsesrc->probe, pulsesrc->server);
478       break;
479     case PROP_DEVICE:
480       g_free (pulsesrc->device);
481       pulsesrc->device = g_value_dup_string (value);
482       break;
483     case PROP_CLIENT:
484       g_free (pulsesrc->client_name);
485       if (!g_value_get_string (value)) {
486         GST_WARNING_OBJECT (pulsesrc,
487             "Empty PulseAudio client name not allowed. Resetting to default value");
488         pulsesrc->client_name = gst_pulse_client_name ();
489       } else
490         pulsesrc->client_name = g_value_dup_string (value);
491       break;
492     case PROP_STREAM_PROPERTIES:
493       if (pulsesrc->properties)
494         gst_structure_free (pulsesrc->properties);
495       pulsesrc->properties =
496           gst_structure_copy (gst_value_get_structure (value));
497       if (pulsesrc->proplist)
498         pa_proplist_free (pulsesrc->proplist);
499       pulsesrc->proplist = gst_pulse_make_proplist (pulsesrc->properties);
500       break;
501     default:
502       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
503       break;
504   }
505 }
506
507 static void
508 gst_pulsesrc_get_property (GObject * object,
509     guint prop_id, GValue * value, GParamSpec * pspec)
510 {
511
512   GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (object);
513
514   switch (prop_id) {
515     case PROP_SERVER:
516       g_value_set_string (value, pulsesrc->server);
517       break;
518     case PROP_DEVICE:
519       g_value_set_string (value, pulsesrc->device);
520       break;
521     case PROP_DEVICE_NAME:
522       g_value_take_string (value, gst_pulsesrc_device_description (pulsesrc));
523       break;
524     case PROP_CLIENT:
525       g_value_set_string (value, pulsesrc->client_name);
526       break;
527     case PROP_STREAM_PROPERTIES:
528       gst_value_set_structure (value, pulsesrc->properties);
529       break;
530     default:
531       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
532       break;
533   }
534 }
535
536 static void
537 gst_pulsesrc_context_state_cb (pa_context * c, void *userdata)
538 {
539   GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
540
541   switch (pa_context_get_state (c)) {
542     case PA_CONTEXT_READY:
543     case PA_CONTEXT_TERMINATED:
544     case PA_CONTEXT_FAILED:
545       pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
546       break;
547
548     case PA_CONTEXT_UNCONNECTED:
549     case PA_CONTEXT_CONNECTING:
550     case PA_CONTEXT_AUTHORIZING:
551     case PA_CONTEXT_SETTING_NAME:
552       break;
553   }
554 }
555
556 static void
557 gst_pulsesrc_stream_state_cb (pa_stream * s, void *userdata)
558 {
559   GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
560
561   switch (pa_stream_get_state (s)) {
562
563     case PA_STREAM_READY:
564     case PA_STREAM_FAILED:
565     case PA_STREAM_TERMINATED:
566       pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
567       break;
568
569     case PA_STREAM_UNCONNECTED:
570     case PA_STREAM_CREATING:
571       break;
572   }
573 }
574
575 static void
576 gst_pulsesrc_stream_request_cb (pa_stream * s, size_t length, void *userdata)
577 {
578   GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
579
580   GST_LOG_OBJECT (pulsesrc, "got request for length %" G_GSIZE_FORMAT, length);
581
582   if (pulsesrc->in_read) {
583     /* only signal when reading */
584     pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
585   }
586 }
587
588 static void
589 gst_pulsesrc_stream_latency_update_cb (pa_stream * s, void *userdata)
590 {
591   const pa_timing_info *info;
592   pa_usec_t source_usec;
593
594   info = pa_stream_get_timing_info (s);
595
596   if (!info) {
597     GST_LOG_OBJECT (GST_PULSESRC_CAST (userdata),
598         "latency update (information unknown)");
599     return;
600   }
601 #ifdef HAVE_PULSE_0_9_11
602   source_usec = info->configured_source_usec;
603 #else
604   source_usec = 0;
605 #endif
606
607   GST_LOG_OBJECT (GST_PULSESRC_CAST (userdata),
608       "latency_update, %" G_GUINT64_FORMAT ", %d:%" G_GINT64_FORMAT ", %d:%"
609       G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT,
610       GST_TIMEVAL_TO_TIME (info->timestamp), info->write_index_corrupt,
611       info->write_index, info->read_index_corrupt, info->read_index,
612       info->source_usec, source_usec);
613 }
614
615 static void
616 gst_pulsesrc_stream_underflow_cb (pa_stream * s, void *userdata)
617 {
618   GST_WARNING_OBJECT (GST_PULSESRC_CAST (userdata), "Got underflow");
619 }
620
621 static void
622 gst_pulsesrc_stream_overflow_cb (pa_stream * s, void *userdata)
623 {
624   GST_WARNING_OBJECT (GST_PULSESRC_CAST (userdata), "Got overflow");
625 }
626
627 static gboolean
628 gst_pulsesrc_open (GstAudioSrc * asrc)
629 {
630   GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
631
632   pa_threaded_mainloop_lock (pulsesrc->mainloop);
633
634   g_assert (!pulsesrc->context);
635   g_assert (!pulsesrc->stream);
636
637   GST_DEBUG_OBJECT (pulsesrc, "opening device");
638
639   if (!(pulsesrc->context =
640           pa_context_new (pa_threaded_mainloop_get_api (pulsesrc->mainloop),
641               pulsesrc->client_name))) {
642     GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Failed to create context"),
643         (NULL));
644     goto unlock_and_fail;
645   }
646
647   pa_context_set_state_callback (pulsesrc->context,
648       gst_pulsesrc_context_state_cb, pulsesrc);
649
650   GST_DEBUG_OBJECT (pulsesrc, "connect to server %s",
651       GST_STR_NULL (pulsesrc->server));
652
653   if (pa_context_connect (pulsesrc->context, pulsesrc->server, 0, NULL) < 0) {
654     GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Failed to connect: %s",
655             pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
656     goto unlock_and_fail;
657   }
658
659   for (;;) {
660     pa_context_state_t state;
661
662     state = pa_context_get_state (pulsesrc->context);
663
664     if (!PA_CONTEXT_IS_GOOD (state)) {
665       GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Failed to connect: %s",
666               pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
667       goto unlock_and_fail;
668     }
669
670     if (state == PA_CONTEXT_READY)
671       break;
672
673     /* Wait until the context is ready */
674     pa_threaded_mainloop_wait (pulsesrc->mainloop);
675   }
676   GST_DEBUG_OBJECT (pulsesrc, "connected");
677
678   pa_threaded_mainloop_unlock (pulsesrc->mainloop);
679
680   return TRUE;
681
682   /* ERRORS */
683 unlock_and_fail:
684   {
685     gst_pulsesrc_destroy_context (pulsesrc);
686
687     pa_threaded_mainloop_unlock (pulsesrc->mainloop);
688
689     return FALSE;
690   }
691 }
692
693 static gboolean
694 gst_pulsesrc_close (GstAudioSrc * asrc)
695 {
696   GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
697
698   pa_threaded_mainloop_lock (pulsesrc->mainloop);
699   gst_pulsesrc_destroy_context (pulsesrc);
700   pa_threaded_mainloop_unlock (pulsesrc->mainloop);
701
702   return TRUE;
703 }
704
705 static gboolean
706 gst_pulsesrc_unprepare (GstAudioSrc * asrc)
707 {
708   GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
709
710   pa_threaded_mainloop_lock (pulsesrc->mainloop);
711   gst_pulsesrc_destroy_stream (pulsesrc);
712
713   pa_threaded_mainloop_unlock (pulsesrc->mainloop);
714
715   pulsesrc->read_buffer = NULL;
716   pulsesrc->read_buffer_length = 0;
717
718   return TRUE;
719 }
720
721 static guint
722 gst_pulsesrc_read (GstAudioSrc * asrc, gpointer data, guint length)
723 {
724   GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
725   size_t sum = 0;
726
727   pa_threaded_mainloop_lock (pulsesrc->mainloop);
728   pulsesrc->in_read = TRUE;
729
730   if (pulsesrc->paused)
731     goto was_paused;
732
733   while (length > 0) {
734     size_t l;
735
736     GST_LOG_OBJECT (pulsesrc, "reading %u bytes", length);
737
738     /*check if we have a leftover buffer */
739     if (!pulsesrc->read_buffer) {
740       for (;;) {
741         if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
742           goto unlock_and_fail;
743
744         /* read all available data, we keep a pointer to the data and the length
745          * and take from it what we need. */
746         if (pa_stream_peek (pulsesrc->stream, &pulsesrc->read_buffer,
747                 &pulsesrc->read_buffer_length) < 0)
748           goto peek_failed;
749
750         GST_LOG_OBJECT (pulsesrc, "have data of %" G_GSIZE_FORMAT " bytes",
751             pulsesrc->read_buffer_length);
752
753         /* if we have data, process if */
754         if (pulsesrc->read_buffer && pulsesrc->read_buffer_length)
755           break;
756
757         /* now wait for more data to become available */
758         GST_LOG_OBJECT (pulsesrc, "waiting for data");
759         pa_threaded_mainloop_wait (pulsesrc->mainloop);
760
761         if (pulsesrc->paused)
762           goto was_paused;
763       }
764     }
765
766     l = pulsesrc->read_buffer_length >
767         length ? length : pulsesrc->read_buffer_length;
768
769     memcpy (data, pulsesrc->read_buffer, l);
770
771     pulsesrc->read_buffer = (const guint8 *) pulsesrc->read_buffer + l;
772     pulsesrc->read_buffer_length -= l;
773
774     data = (guint8 *) data + l;
775     length -= l;
776     sum += l;
777
778     if (pulsesrc->read_buffer_length <= 0) {
779       /* we copied all of the data, drop it now */
780       if (pa_stream_drop (pulsesrc->stream) < 0)
781         goto drop_failed;
782
783       /* reset pointer to data */
784       pulsesrc->read_buffer = NULL;
785       pulsesrc->read_buffer_length = 0;
786     }
787   }
788
789   pulsesrc->in_read = FALSE;
790   pa_threaded_mainloop_unlock (pulsesrc->mainloop);
791
792   return sum;
793
794   /* ERRORS */
795 was_paused:
796   {
797     GST_LOG_OBJECT (pulsesrc, "we are paused");
798     goto unlock_and_fail;
799   }
800 peek_failed:
801   {
802     GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
803         ("pa_stream_peek() failed: %s",
804             pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
805     goto unlock_and_fail;
806   }
807 drop_failed:
808   {
809     GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
810         ("pa_stream_drop() failed: %s",
811             pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
812     goto unlock_and_fail;
813   }
814 unlock_and_fail:
815   {
816     pulsesrc->in_read = FALSE;
817     pa_threaded_mainloop_unlock (pulsesrc->mainloop);
818
819     return (guint) - 1;
820   }
821 }
822
823 /* return the delay in samples */
824 static guint
825 gst_pulsesrc_delay (GstAudioSrc * asrc)
826 {
827   GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
828   pa_usec_t t;
829   int negative, res;
830   guint result;
831
832   pa_threaded_mainloop_lock (pulsesrc->mainloop);
833   if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
834     goto server_dead;
835
836   /* get the latency, this can fail when we don't have a latency update yet.
837    * We don't want to wait for latency updates here but we just return 0. */
838   res = pa_stream_get_latency (pulsesrc->stream, &t, &negative);
839
840   pa_threaded_mainloop_unlock (pulsesrc->mainloop);
841
842   if (res > 0) {
843     GST_DEBUG_OBJECT (pulsesrc, "could not get latency");
844     result = 0;
845   } else {
846     if (negative)
847       result = 0;
848     else
849       result = (guint) ((t * pulsesrc->sample_spec.rate) / 1000000LL);
850   }
851   return result;
852
853   /* ERRORS */
854 server_dead:
855   {
856     GST_DEBUG_OBJECT (pulsesrc, "the server is dead");
857     pa_threaded_mainloop_unlock (pulsesrc->mainloop);
858     return 0;
859   }
860 }
861
862 static gboolean
863 gst_pulsesrc_create_stream (GstPulseSrc * pulsesrc, GstCaps * caps)
864 {
865   pa_channel_map channel_map;
866   GstStructure *s;
867   gboolean need_channel_layout = FALSE;
868   GstRingBufferSpec spec;
869   const gchar *name;
870
871   memset (&spec, 0, sizeof (GstRingBufferSpec));
872   spec.latency_time = GST_SECOND;
873   if (!gst_ring_buffer_parse_caps (&spec, caps)) {
874     GST_ELEMENT_ERROR (pulsesrc, RESOURCE, SETTINGS,
875         ("Can't parse caps."), (NULL));
876     goto fail;
877   }
878   /* Keep the refcount of the caps at 1 to make them writable */
879   gst_caps_unref (spec.caps);
880
881   if (!gst_pulse_fill_sample_spec (&spec, &pulsesrc->sample_spec)) {
882     GST_ELEMENT_ERROR (pulsesrc, RESOURCE, SETTINGS,
883         ("Invalid sample specification."), (NULL));
884     goto fail;
885   }
886
887   pa_threaded_mainloop_lock (pulsesrc->mainloop);
888
889   if (!pulsesrc->context) {
890     GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Bad context"), (NULL));
891     goto unlock_and_fail;
892   }
893
894   s = gst_caps_get_structure (caps, 0);
895   if (!gst_structure_has_field (s, "channel-layout") ||
896       !gst_pulse_gst_to_channel_map (&channel_map, &spec)) {
897     if (spec.channels == 1)
898       pa_channel_map_init_mono (&channel_map);
899     else if (spec.channels == 2)
900       pa_channel_map_init_stereo (&channel_map);
901     else
902       need_channel_layout = TRUE;
903   }
904
905   name = "Record Stream";
906   if (pulsesrc->proplist) {
907     if (!(pulsesrc->stream = pa_stream_new_with_proplist (pulsesrc->context,
908                 name, &pulsesrc->sample_spec,
909                 (need_channel_layout) ? NULL : &channel_map,
910                 pulsesrc->proplist))) {
911       GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
912           ("Failed to create stream: %s",
913               pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
914       goto unlock_and_fail;
915     }
916   } else if (!(pulsesrc->stream = pa_stream_new (pulsesrc->context,
917               name, &pulsesrc->sample_spec,
918               (need_channel_layout) ? NULL : &channel_map))) {
919     GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
920         ("Failed to create stream: %s",
921             pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
922     goto unlock_and_fail;
923   }
924
925   if (need_channel_layout) {
926     const pa_channel_map *m = pa_stream_get_channel_map (pulsesrc->stream);
927
928     gst_pulse_channel_map_to_gst (m, &spec);
929     caps = spec.caps;
930   }
931
932   GST_DEBUG_OBJECT (pulsesrc, "Caps are %" GST_PTR_FORMAT, caps);
933
934   pa_stream_set_state_callback (pulsesrc->stream, gst_pulsesrc_stream_state_cb,
935       pulsesrc);
936   pa_stream_set_read_callback (pulsesrc->stream, gst_pulsesrc_stream_request_cb,
937       pulsesrc);
938   pa_stream_set_underflow_callback (pulsesrc->stream,
939       gst_pulsesrc_stream_underflow_cb, pulsesrc);
940   pa_stream_set_overflow_callback (pulsesrc->stream,
941       gst_pulsesrc_stream_overflow_cb, pulsesrc);
942   pa_stream_set_latency_update_callback (pulsesrc->stream,
943       gst_pulsesrc_stream_latency_update_cb, pulsesrc);
944
945   pa_threaded_mainloop_unlock (pulsesrc->mainloop);
946
947   return TRUE;
948
949 unlock_and_fail:
950   gst_pulsesrc_destroy_stream (pulsesrc);
951
952   pa_threaded_mainloop_unlock (pulsesrc->mainloop);
953
954 fail:
955   return FALSE;
956 }
957
958 /* This is essentially gst_base_src_negotiate_default() but the caps
959  * are guaranteed to have a channel layout for > 2 channels
960  */
961 static gboolean
962 gst_pulsesrc_negotiate (GstBaseSrc * basesrc)
963 {
964   GstCaps *thiscaps;
965   GstCaps *caps = NULL;
966   GstCaps *peercaps = NULL;
967   gboolean result = FALSE;
968
969   /* first see what is possible on our source pad */
970   thiscaps = gst_pad_get_caps_reffed (GST_BASE_SRC_PAD (basesrc));
971   GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
972   /* nothing or anything is allowed, we're done */
973   if (thiscaps == NULL || gst_caps_is_any (thiscaps))
974     goto no_nego_needed;
975
976   /* get the peer caps */
977   peercaps = gst_pad_peer_get_caps_reffed (GST_BASE_SRC_PAD (basesrc));
978   GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
979   if (peercaps) {
980     /* get intersection */
981     caps = gst_caps_intersect (thiscaps, peercaps);
982     GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, caps);
983     gst_caps_unref (thiscaps);
984     gst_caps_unref (peercaps);
985   } else {
986     /* no peer, work with our own caps then */
987     caps = thiscaps;
988   }
989   if (caps) {
990     /* take first (and best, since they are sorted) possibility */
991     caps = gst_caps_make_writable (caps);
992     gst_caps_truncate (caps);
993
994     /* now fixate */
995     if (!gst_caps_is_empty (caps)) {
996       gst_pad_fixate_caps (GST_BASE_SRC_PAD (basesrc), caps);
997       GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
998
999       if (gst_caps_is_any (caps)) {
1000         /* hmm, still anything, so element can do anything and
1001          * nego is not needed */
1002         result = TRUE;
1003       } else if (gst_caps_is_fixed (caps)) {
1004         /* yay, fixed caps, use those then */
1005         result = gst_pulsesrc_create_stream (GST_PULSESRC_CAST (basesrc), caps);
1006         if (result)
1007           result = gst_pad_set_caps (GST_BASE_SRC_PAD (basesrc), caps);
1008       }
1009     }
1010     gst_caps_unref (caps);
1011   }
1012   return result;
1013
1014 no_nego_needed:
1015   {
1016     GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
1017     if (thiscaps)
1018       gst_caps_unref (thiscaps);
1019     return TRUE;
1020   }
1021 }
1022
1023 static gboolean
1024 gst_pulsesrc_prepare (GstAudioSrc * asrc, GstRingBufferSpec * spec)
1025 {
1026   pa_buffer_attr wanted;
1027   const pa_buffer_attr *actual;
1028   GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
1029
1030   pa_threaded_mainloop_lock (pulsesrc->mainloop);
1031
1032   wanted.maxlength = -1;
1033   wanted.tlength = -1;
1034   wanted.prebuf = 0;
1035   wanted.minreq = -1;
1036   wanted.fragsize = spec->segsize;
1037
1038   GST_INFO_OBJECT (pulsesrc, "maxlength: %d", wanted.maxlength);
1039   GST_INFO_OBJECT (pulsesrc, "tlength:   %d", wanted.tlength);
1040   GST_INFO_OBJECT (pulsesrc, "prebuf:    %d", wanted.prebuf);
1041   GST_INFO_OBJECT (pulsesrc, "minreq:    %d", wanted.minreq);
1042   GST_INFO_OBJECT (pulsesrc, "fragsize:  %d", wanted.fragsize);
1043
1044   if (pa_stream_connect_record (pulsesrc->stream, pulsesrc->device, &wanted,
1045           PA_STREAM_INTERPOLATE_TIMING |
1046           PA_STREAM_AUTO_TIMING_UPDATE | PA_STREAM_NOT_MONOTONOUS |
1047 #ifdef HAVE_PULSE_0_9_11
1048           PA_STREAM_ADJUST_LATENCY |
1049 #endif
1050           PA_STREAM_START_CORKED) < 0) {
1051     GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1052         ("Failed to connect stream: %s",
1053             pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1054     goto unlock_and_fail;
1055   }
1056
1057   pulsesrc->corked = TRUE;
1058
1059   for (;;) {
1060     pa_stream_state_t state;
1061
1062     state = pa_stream_get_state (pulsesrc->stream);
1063
1064     if (!PA_STREAM_IS_GOOD (state)) {
1065       GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1066           ("Failed to connect stream: %s",
1067               pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1068       goto unlock_and_fail;
1069     }
1070
1071     if (state == PA_STREAM_READY)
1072       break;
1073
1074     /* Wait until the stream is ready */
1075     pa_threaded_mainloop_wait (pulsesrc->mainloop);
1076   }
1077
1078   /* get the actual buffering properties now */
1079   actual = pa_stream_get_buffer_attr (pulsesrc->stream);
1080
1081   GST_INFO_OBJECT (pulsesrc, "maxlength: %d", actual->maxlength);
1082   GST_INFO_OBJECT (pulsesrc, "tlength:   %d (wanted: %d)",
1083       actual->tlength, wanted.tlength);
1084   GST_INFO_OBJECT (pulsesrc, "prebuf:    %d", actual->prebuf);
1085   GST_INFO_OBJECT (pulsesrc, "minreq:    %d (wanted %d)", actual->minreq,
1086       wanted.minreq);
1087   GST_INFO_OBJECT (pulsesrc, "fragsize:  %d (wanted %d)",
1088       actual->fragsize, wanted.fragsize);
1089
1090   if (actual->fragsize >= wanted.fragsize) {
1091     spec->segsize = actual->fragsize;
1092   } else {
1093     spec->segsize = actual->fragsize * (wanted.fragsize / actual->fragsize);
1094   }
1095   spec->segtotal = actual->maxlength / spec->segsize;
1096
1097   pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1098
1099   return TRUE;
1100
1101 unlock_and_fail:
1102   {
1103     gst_pulsesrc_destroy_stream (pulsesrc);
1104
1105     pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1106     return FALSE;
1107   }
1108 }
1109
1110 static void
1111 gst_pulsesrc_success_cb (pa_stream * s, int success, void *userdata)
1112 {
1113   GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
1114
1115   pulsesrc->operation_success = ! !success;
1116   pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
1117 }
1118
1119 static void
1120 gst_pulsesrc_reset (GstAudioSrc * asrc)
1121 {
1122   GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
1123   pa_operation *o = NULL;
1124
1125   pa_threaded_mainloop_lock (pulsesrc->mainloop);
1126   GST_DEBUG_OBJECT (pulsesrc, "reset");
1127
1128   if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
1129     goto unlock_and_fail;
1130
1131   if (!(o =
1132           pa_stream_flush (pulsesrc->stream, gst_pulsesrc_success_cb,
1133               pulsesrc))) {
1134     GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1135         ("pa_stream_flush() failed: %s",
1136             pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1137     goto unlock_and_fail;
1138   }
1139
1140   pulsesrc->paused = TRUE;
1141   /* Inform anyone waiting in _write() call that it shall wakeup */
1142   if (pulsesrc->in_read) {
1143     pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
1144   }
1145
1146   pulsesrc->operation_success = FALSE;
1147   while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1148
1149     if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
1150       goto unlock_and_fail;
1151
1152     pa_threaded_mainloop_wait (pulsesrc->mainloop);
1153   }
1154
1155   if (!pulsesrc->operation_success) {
1156     GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Flush failed: %s",
1157             pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1158     goto unlock_and_fail;
1159   }
1160
1161 unlock_and_fail:
1162
1163   if (o) {
1164     pa_operation_cancel (o);
1165     pa_operation_unref (o);
1166   }
1167
1168   pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1169 }
1170
1171 /* update the corked state of a stream, must be called with the mainloop
1172  * lock */
1173 static gboolean
1174 gst_pulsesrc_set_corked (GstPulseSrc * psrc, gboolean corked, gboolean wait)
1175 {
1176   pa_operation *o = NULL;
1177   gboolean res = FALSE;
1178
1179   GST_DEBUG_OBJECT (psrc, "setting corked state to %d", corked);
1180   if (psrc->corked != corked) {
1181     if (!(o = pa_stream_cork (psrc->stream, corked,
1182                 gst_pulsesrc_success_cb, psrc)))
1183       goto cork_failed;
1184
1185     while (wait && pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1186       pa_threaded_mainloop_wait (psrc->mainloop);
1187       if (gst_pulsesrc_is_dead (psrc, TRUE))
1188         goto server_dead;
1189     }
1190     psrc->corked = corked;
1191   } else {
1192     GST_DEBUG_OBJECT (psrc, "skipping, already in requested state");
1193   }
1194   res = TRUE;
1195
1196 cleanup:
1197   if (o)
1198     pa_operation_unref (o);
1199
1200   return res;
1201
1202   /* ERRORS */
1203 server_dead:
1204   {
1205     GST_DEBUG_OBJECT (psrc, "the server is dead");
1206     goto cleanup;
1207   }
1208 cork_failed:
1209   {
1210     GST_ELEMENT_ERROR (psrc, RESOURCE, FAILED,
1211         ("pa_stream_cork() failed: %s",
1212             pa_strerror (pa_context_errno (psrc->context))), (NULL));
1213     goto cleanup;
1214   }
1215 }
1216
1217 /* start/resume playback ASAP */
1218 static gboolean
1219 gst_pulsesrc_play (GstPulseSrc * psrc)
1220 {
1221   pa_threaded_mainloop_lock (psrc->mainloop);
1222   GST_DEBUG_OBJECT (psrc, "playing");
1223   psrc->paused = FALSE;
1224   gst_pulsesrc_set_corked (psrc, FALSE, FALSE);
1225   pa_threaded_mainloop_unlock (psrc->mainloop);
1226
1227   return TRUE;
1228 }
1229
1230 /* pause/stop playback ASAP */
1231 static gboolean
1232 gst_pulsesrc_pause (GstPulseSrc * psrc)
1233 {
1234   pa_threaded_mainloop_lock (psrc->mainloop);
1235   GST_DEBUG_OBJECT (psrc, "pausing");
1236   /* make sure the commit method stops writing */
1237   psrc->paused = TRUE;
1238   if (psrc->in_read) {
1239     /* we are waiting in a read, signal */
1240     GST_DEBUG_OBJECT (psrc, "signal read");
1241     pa_threaded_mainloop_signal (psrc->mainloop, 0);
1242   }
1243   pa_threaded_mainloop_unlock (psrc->mainloop);
1244
1245   return TRUE;
1246 }
1247
1248 static GstStateChangeReturn
1249 gst_pulsesrc_change_state (GstElement * element, GstStateChange transition)
1250 {
1251   GstStateChangeReturn ret;
1252   GstPulseSrc *this = GST_PULSESRC_CAST (element);
1253
1254   switch (transition) {
1255     case GST_STATE_CHANGE_NULL_TO_READY:
1256       this->mainloop = pa_threaded_mainloop_new ();
1257       g_assert (this->mainloop);
1258
1259       pa_threaded_mainloop_start (this->mainloop);
1260
1261       if (!this->mixer)
1262         this->mixer =
1263             gst_pulsemixer_ctrl_new (G_OBJECT (this), this->server,
1264             this->device, GST_PULSEMIXER_SOURCE);
1265       break;
1266     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1267       /* uncork and start recording */
1268       gst_pulsesrc_play (this);
1269       break;
1270     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1271       /* stop recording ASAP by corking */
1272       pa_threaded_mainloop_lock (this->mainloop);
1273       GST_DEBUG_OBJECT (this, "corking");
1274       gst_pulsesrc_set_corked (this, TRUE, FALSE);
1275       pa_threaded_mainloop_unlock (this->mainloop);
1276       break;
1277     default:
1278       break;
1279   }
1280
1281   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1282
1283   switch (transition) {
1284     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1285       /* now make sure we get out of the _read method */
1286       gst_pulsesrc_pause (this);
1287       break;
1288     case GST_STATE_CHANGE_READY_TO_NULL:
1289       if (this->mixer) {
1290         gst_pulsemixer_ctrl_free (this->mixer);
1291         this->mixer = NULL;
1292       }
1293
1294       if (this->mainloop)
1295         pa_threaded_mainloop_stop (this->mainloop);
1296
1297       gst_pulsesrc_destroy_context (this);
1298
1299       if (this->mainloop) {
1300         pa_threaded_mainloop_free (this->mainloop);
1301         this->mainloop = NULL;
1302       }
1303       break;
1304     default:
1305       break;
1306   }
1307
1308   return ret;
1309 }