Merge remote-tracking branch 'origin/master' into 0.11
[platform/upstream/gstreamer.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 #ifdef HAVE_PULSE_1_0
47 #include <gst/interfaces/streamvolume.h>
48 #endif
49
50 #include "pulsesrc.h"
51 #include "pulseutil.h"
52 #include "pulsemixerctrl.h"
53
54 GST_DEBUG_CATEGORY_EXTERN (pulse_debug);
55 #define GST_CAT_DEFAULT pulse_debug
56
57 #define DEFAULT_SERVER            NULL
58 #define DEFAULT_DEVICE            NULL
59 #define DEFAULT_DEVICE_NAME       NULL
60
61 #ifdef HAVE_PULSE_1_0
62 #define DEFAULT_VOLUME          1.0
63 #define DEFAULT_MUTE            FALSE
64 #define MAX_VOLUME              10.0
65 #endif
66
67 enum
68 {
69   PROP_0,
70   PROP_SERVER,
71   PROP_DEVICE,
72   PROP_DEVICE_NAME,
73   PROP_CLIENT,
74   PROP_STREAM_PROPERTIES,
75   PROP_SOURCE_OUTPUT_INDEX,
76 #ifdef HAVE_PULSE_1_0
77   PROP_VOLUME,
78   PROP_MUTE,
79 #endif
80   PROP_LAST
81 };
82
83 static void gst_pulsesrc_destroy_stream (GstPulseSrc * pulsesrc);
84 static void gst_pulsesrc_destroy_context (GstPulseSrc * pulsesrc);
85
86 static void gst_pulsesrc_set_property (GObject * object, guint prop_id,
87     const GValue * value, GParamSpec * pspec);
88 static void gst_pulsesrc_get_property (GObject * object, guint prop_id,
89     GValue * value, GParamSpec * pspec);
90 static void gst_pulsesrc_finalize (GObject * object);
91
92 static gboolean gst_pulsesrc_set_corked (GstPulseSrc * psrc, gboolean corked,
93     gboolean wait);
94 static gboolean gst_pulsesrc_open (GstAudioSrc * asrc);
95
96 static gboolean gst_pulsesrc_close (GstAudioSrc * asrc);
97
98 static gboolean gst_pulsesrc_prepare (GstAudioSrc * asrc,
99     GstAudioRingBufferSpec * spec);
100
101 static gboolean gst_pulsesrc_unprepare (GstAudioSrc * asrc);
102
103 static guint gst_pulsesrc_read (GstAudioSrc * asrc, gpointer data,
104     guint length);
105 static guint gst_pulsesrc_delay (GstAudioSrc * asrc);
106
107 static void gst_pulsesrc_reset (GstAudioSrc * src);
108
109 static gboolean gst_pulsesrc_negotiate (GstBaseSrc * basesrc);
110
111 static GstStateChangeReturn gst_pulsesrc_change_state (GstElement *
112     element, GstStateChange transition);
113
114 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
115 # define FORMATS "{ S16LE, S16BE, F32LE, F32BE, S32LE, S32BE, U8 }"
116 #else
117 # define FORMATS "{ S16BE, S16LE, F32BE, F32LE, S32BE, S32LE, U8 }"
118 #endif
119
120 static GstStaticPadTemplate pad_template = GST_STATIC_PAD_TEMPLATE ("src",
121     GST_PAD_SRC,
122     GST_PAD_ALWAYS,
123     GST_STATIC_CAPS ("audio/x-raw, "
124         "format = (string) " FORMATS ", "
125         "rate = (int) [ 1, MAX ], "
126         "channels = (int) [ 1, 32 ];"
127         "audio/x-alaw, "
128         "rate = (int) [ 1, MAX], "
129         "channels = (int) [ 1, 32 ];"
130         "audio/x-mulaw, "
131         "rate = (int) [ 1, MAX], " "channels = (int) [ 1, 32 ]")
132     );
133
134
135 GST_IMPLEMENT_PULSEMIXER_CTRL_METHODS (GstPulseSrc, gst_pulsesrc);
136 GST_IMPLEMENT_PULSEPROBE_METHODS (GstPulseSrc, gst_pulsesrc);
137
138 #define gst_pulsesrc_parent_class parent_class
139 G_DEFINE_TYPE_WITH_CODE (GstPulseSrc, gst_pulsesrc, GST_TYPE_AUDIO_SRC,
140     G_IMPLEMENT_INTERFACE (GST_TYPE_MIXER, gst_pulsesrc_mixer_interface_init);
141     G_IMPLEMENT_INTERFACE (GST_TYPE_PROPERTY_PROBE,
142         gst_pulsesrc_property_probe_interface_init);
143     G_IMPLEMENT_INTERFACE (GST_TYPE_STREAM_VOLUME, NULL));
144
145 static void
146 gst_pulsesrc_class_init (GstPulseSrcClass * klass)
147 {
148   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
149   GstAudioSrcClass *gstaudiosrc_class = GST_AUDIO_SRC_CLASS (klass);
150   GstBaseSrcClass *gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
151   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
152   gchar *clientname;
153
154   gobject_class->finalize = gst_pulsesrc_finalize;
155   gobject_class->set_property = gst_pulsesrc_set_property;
156   gobject_class->get_property = gst_pulsesrc_get_property;
157
158   gstelement_class->change_state =
159       GST_DEBUG_FUNCPTR (gst_pulsesrc_change_state);
160
161   gstbasesrc_class->negotiate = GST_DEBUG_FUNCPTR (gst_pulsesrc_negotiate);
162
163   gstaudiosrc_class->open = GST_DEBUG_FUNCPTR (gst_pulsesrc_open);
164   gstaudiosrc_class->close = GST_DEBUG_FUNCPTR (gst_pulsesrc_close);
165   gstaudiosrc_class->prepare = GST_DEBUG_FUNCPTR (gst_pulsesrc_prepare);
166   gstaudiosrc_class->unprepare = GST_DEBUG_FUNCPTR (gst_pulsesrc_unprepare);
167   gstaudiosrc_class->read = GST_DEBUG_FUNCPTR (gst_pulsesrc_read);
168   gstaudiosrc_class->delay = GST_DEBUG_FUNCPTR (gst_pulsesrc_delay);
169   gstaudiosrc_class->reset = GST_DEBUG_FUNCPTR (gst_pulsesrc_reset);
170
171   /* Overwrite GObject fields */
172   g_object_class_install_property (gobject_class,
173       PROP_SERVER,
174       g_param_spec_string ("server", "Server",
175           "The PulseAudio server to connect to", DEFAULT_SERVER,
176           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
177
178   g_object_class_install_property (gobject_class, PROP_DEVICE,
179       g_param_spec_string ("device", "Device",
180           "The PulseAudio source device to connect to", DEFAULT_DEVICE,
181           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
182
183   g_object_class_install_property (gobject_class,
184       PROP_DEVICE_NAME,
185       g_param_spec_string ("device-name", "Device name",
186           "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
187           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
188
189   clientname = gst_pulse_client_name ();
190   /**
191    * GstPulseSrc:client
192    *
193    * The PulseAudio client name to use.
194    *
195    * Since: 0.10.27
196    */
197   g_object_class_install_property (gobject_class,
198       PROP_CLIENT,
199       g_param_spec_string ("client", "Client",
200           "The PulseAudio client_name_to_use", clientname,
201           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
202           GST_PARAM_MUTABLE_READY));
203   g_free (clientname);
204
205   /**
206    * GstPulseSrc:stream-properties
207    *
208    * List of pulseaudio stream properties. A list of defined properties can be
209    * found in the <ulink href="http://0pointer.de/lennart/projects/pulseaudio/doxygen/proplist_8h.html">pulseaudio api docs</ulink>.
210    *
211    * Below is an example for registering as a music application to pulseaudio.
212    * |[
213    * GstStructure *props;
214    *
215    * props = gst_structure_from_string ("props,media.role=music", NULL);
216    * g_object_set (pulse, "stream-properties", props, NULL);
217    * gst_structure_free (props);
218    * ]|
219    *
220    * Since: 0.10.26
221    */
222   g_object_class_install_property (gobject_class,
223       PROP_STREAM_PROPERTIES,
224       g_param_spec_boxed ("stream-properties", "stream properties",
225           "list of pulseaudio stream properties",
226           GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
227   /**
228    * GstPulseSrc:source-output-index
229    *
230    * The index of the PulseAudio source output corresponding to this element.
231    *
232    * Since: 0.10.31
233    */
234   g_object_class_install_property (gobject_class,
235       PROP_SOURCE_OUTPUT_INDEX,
236       g_param_spec_uint ("source-output-index", "source output index",
237           "The index of the PulseAudio source output corresponding to this "
238           "record stream", 0, G_MAXUINT, PA_INVALID_INDEX,
239           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
240
241   gst_element_class_set_details_simple (gstelement_class,
242       "PulseAudio Audio Source",
243       "Source/Audio",
244       "Captures audio from a PulseAudio server", "Lennart Poettering");
245   gst_element_class_add_pad_template (gstelement_class,
246       gst_static_pad_template_get (&pad_template));
247
248 #ifdef HAVE_PULSE_1_0
249   /**
250    * GstPulseSrc:volume
251    *
252    * The volume of the record stream. Only works when using PulseAudio 1.0 or
253    * later.
254    *
255    * Since: 0.10.36
256    */
257   g_object_class_install_property (gobject_class,
258       PROP_VOLUME, g_param_spec_double ("volume", "Volume",
259           "Linear volume of this stream, 1.0=100%",
260           0.0, MAX_VOLUME, DEFAULT_VOLUME,
261           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
262
263   /**
264    * GstPulseSrc:mute
265    *
266    * Whether the stream is muted or not. Only works when using PulseAudio 1.0
267    * or later.
268    *
269    * Since: 0.10.36
270    */
271   g_object_class_install_property (gobject_class,
272       PROP_MUTE, g_param_spec_boolean ("mute", "Mute",
273           "Mute state of this stream",
274           DEFAULT_MUTE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
275 #endif
276 }
277
278 static void
279 gst_pulsesrc_init (GstPulseSrc * pulsesrc)
280 {
281   pulsesrc->server = NULL;
282   pulsesrc->device = NULL;
283   pulsesrc->client_name = gst_pulse_client_name ();
284   pulsesrc->device_description = NULL;
285
286   pulsesrc->context = NULL;
287   pulsesrc->stream = NULL;
288   pulsesrc->stream_connected = FALSE;
289   pulsesrc->source_output_idx = PA_INVALID_INDEX;
290
291   pulsesrc->read_buffer = NULL;
292   pulsesrc->read_buffer_length = 0;
293
294   pa_sample_spec_init (&pulsesrc->sample_spec);
295
296   pulsesrc->operation_success = FALSE;
297   pulsesrc->paused = TRUE;
298   pulsesrc->in_read = FALSE;
299
300 #ifdef HAVE_PULSE_1_0
301   pulsesrc->volume = DEFAULT_VOLUME;
302   pulsesrc->volume_set = FALSE;
303
304   pulsesrc->mute = DEFAULT_MUTE;
305   pulsesrc->mute_set = FALSE;
306
307   pulsesrc->notify = 0;
308 #endif
309
310   pulsesrc->mixer = NULL;
311
312   pulsesrc->properties = NULL;
313   pulsesrc->proplist = NULL;
314
315   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 */
316
317   /* this should be the default but it isn't yet */
318   gst_audio_base_src_set_slave_method (GST_AUDIO_BASE_SRC (pulsesrc),
319       GST_AUDIO_BASE_SRC_SLAVE_SKEW);
320 }
321
322 static void
323 gst_pulsesrc_destroy_stream (GstPulseSrc * pulsesrc)
324 {
325   if (pulsesrc->stream) {
326     pa_stream_disconnect (pulsesrc->stream);
327     pa_stream_unref (pulsesrc->stream);
328     pulsesrc->stream = NULL;
329     pulsesrc->stream_connected = FALSE;
330     pulsesrc->source_output_idx = PA_INVALID_INDEX;
331     g_object_notify (G_OBJECT (pulsesrc), "source-output-index");
332   }
333
334   g_free (pulsesrc->device_description);
335   pulsesrc->device_description = NULL;
336 }
337
338 static void
339 gst_pulsesrc_destroy_context (GstPulseSrc * pulsesrc)
340 {
341
342   gst_pulsesrc_destroy_stream (pulsesrc);
343
344   if (pulsesrc->context) {
345     pa_context_disconnect (pulsesrc->context);
346
347     /* Make sure we don't get any further callbacks */
348     pa_context_set_state_callback (pulsesrc->context, NULL, NULL);
349 #ifdef HAVE_PULSE_1_0
350     pa_context_set_subscribe_callback (pulsesrc->context, NULL, NULL);
351 #endif
352
353     pa_context_unref (pulsesrc->context);
354
355     pulsesrc->context = NULL;
356   }
357 }
358
359 static void
360 gst_pulsesrc_finalize (GObject * object)
361 {
362   GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (object);
363
364   g_free (pulsesrc->server);
365   g_free (pulsesrc->device);
366   g_free (pulsesrc->client_name);
367
368   if (pulsesrc->properties)
369     gst_structure_free (pulsesrc->properties);
370   if (pulsesrc->proplist)
371     pa_proplist_free (pulsesrc->proplist);
372
373   if (pulsesrc->mixer) {
374     gst_pulsemixer_ctrl_free (pulsesrc->mixer);
375     pulsesrc->mixer = NULL;
376   }
377
378   if (pulsesrc->probe) {
379     gst_pulseprobe_free (pulsesrc->probe);
380     pulsesrc->probe = NULL;
381   }
382
383   G_OBJECT_CLASS (parent_class)->finalize (object);
384 }
385
386 #define CONTEXT_OK(c) ((c) && PA_CONTEXT_IS_GOOD (pa_context_get_state ((c))))
387 #define STREAM_OK(s) ((s) && PA_STREAM_IS_GOOD (pa_stream_get_state ((s))))
388
389 static gboolean
390 gst_pulsesrc_is_dead (GstPulseSrc * pulsesrc, gboolean check_stream)
391 {
392   if (!CONTEXT_OK (pulsesrc->context))
393     goto error;
394
395   if (check_stream && !STREAM_OK (pulsesrc->stream))
396     goto error;
397
398   return FALSE;
399
400 error:
401   {
402     const gchar *err_str = pulsesrc->context ?
403         pa_strerror (pa_context_errno (pulsesrc->context)) : NULL;
404     GST_ELEMENT_ERROR ((pulsesrc), RESOURCE, FAILED, ("Disconnected: %s",
405             err_str), (NULL));
406     return TRUE;
407   }
408 }
409
410 static void
411 gst_pulsesrc_source_info_cb (pa_context * c, const pa_source_info * i, int eol,
412     void *userdata)
413 {
414   GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
415
416   if (!i)
417     goto done;
418
419   g_free (pulsesrc->device_description);
420   pulsesrc->device_description = g_strdup (i->description);
421
422 done:
423   pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
424 }
425
426 static gchar *
427 gst_pulsesrc_device_description (GstPulseSrc * pulsesrc)
428 {
429   pa_operation *o = NULL;
430   gchar *t;
431
432   if (!pulsesrc->mainloop)
433     goto no_mainloop;
434
435   pa_threaded_mainloop_lock (pulsesrc->mainloop);
436
437   if (!(o = pa_context_get_source_info_by_name (pulsesrc->context,
438               pulsesrc->device, gst_pulsesrc_source_info_cb, pulsesrc))) {
439
440     GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
441         ("pa_stream_get_source_info() failed: %s",
442             pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
443     goto unlock;
444   }
445
446   while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
447
448     if (gst_pulsesrc_is_dead (pulsesrc, FALSE))
449       goto unlock;
450
451     pa_threaded_mainloop_wait (pulsesrc->mainloop);
452   }
453
454 unlock:
455
456   if (o)
457     pa_operation_unref (o);
458
459   t = g_strdup (pulsesrc->device_description);
460
461   pa_threaded_mainloop_unlock (pulsesrc->mainloop);
462
463   return t;
464
465 no_mainloop:
466   {
467     GST_DEBUG_OBJECT (pulsesrc, "have no mainloop");
468     return NULL;
469   }
470 }
471
472 #ifdef HAVE_PULSE_1_0
473 static void
474 gst_pulsesrc_source_output_info_cb (pa_context * c,
475     const pa_source_output_info * i, int eol, void *userdata)
476 {
477   GstPulseSrc *psrc;
478
479   psrc = GST_PULSESRC_CAST (userdata);
480
481   if (!i)
482     goto done;
483
484   /* If the index doesn't match our current stream,
485    * it implies we just recreated the stream (caps change)
486    */
487   if (i->index == psrc->source_output_idx) {
488     psrc->volume = pa_sw_volume_to_linear (pa_cvolume_max (&i->volume));
489     psrc->mute = i->mute;
490   }
491
492 done:
493   pa_threaded_mainloop_signal (psrc->mainloop, 0);
494 }
495
496 static gdouble
497 gst_pulsesrc_get_stream_volume (GstPulseSrc * pulsesrc)
498 {
499   pa_operation *o = NULL;
500   gdouble v;
501
502   if (!pulsesrc->mainloop)
503     goto no_mainloop;
504
505   if (pulsesrc->source_output_idx == PA_INVALID_INDEX)
506     goto no_index;
507
508   pa_threaded_mainloop_lock (pulsesrc->mainloop);
509
510   if (!(o = pa_context_get_source_output_info (pulsesrc->context,
511               pulsesrc->source_output_idx, gst_pulsesrc_source_output_info_cb,
512               pulsesrc)))
513     goto info_failed;
514
515   while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
516     pa_threaded_mainloop_wait (pulsesrc->mainloop);
517     if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
518       goto unlock;
519   }
520
521 unlock:
522   v = pulsesrc->volume;
523
524   if (o)
525     pa_operation_unref (o);
526
527   pa_threaded_mainloop_unlock (pulsesrc->mainloop);
528
529   if (v > MAX_VOLUME) {
530     GST_WARNING_OBJECT (pulsesrc, "Clipped volume from %f to %f", v,
531         MAX_VOLUME);
532     v = MAX_VOLUME;
533   }
534
535   return v;
536
537   /* ERRORS */
538 no_mainloop:
539   {
540     v = pulsesrc->volume;
541     GST_DEBUG_OBJECT (pulsesrc, "we have no mainloop");
542     return v;
543   }
544 no_index:
545   {
546     v = pulsesrc->volume;
547     GST_DEBUG_OBJECT (pulsesrc, "we don't have a stream index");
548     return v;
549   }
550 info_failed:
551   {
552     GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
553         ("pa_context_get_source_output_info() failed: %s",
554             pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
555     goto unlock;
556   }
557 }
558
559 static gboolean
560 gst_pulsesrc_get_stream_mute (GstPulseSrc * pulsesrc)
561 {
562   pa_operation *o = NULL;
563   gboolean mute;
564
565   if (!pulsesrc->mainloop)
566     goto no_mainloop;
567
568   if (pulsesrc->source_output_idx == PA_INVALID_INDEX)
569     goto no_index;
570
571   pa_threaded_mainloop_lock (pulsesrc->mainloop);
572
573   if (!(o = pa_context_get_source_output_info (pulsesrc->context,
574               pulsesrc->source_output_idx, gst_pulsesrc_source_output_info_cb,
575               pulsesrc)))
576     goto info_failed;
577
578   while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
579     pa_threaded_mainloop_wait (pulsesrc->mainloop);
580     if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
581       goto unlock;
582   }
583
584 unlock:
585   mute = pulsesrc->mute;
586
587   if (o)
588     pa_operation_unref (o);
589
590   pa_threaded_mainloop_unlock (pulsesrc->mainloop);
591
592   return mute;
593
594   /* ERRORS */
595 no_mainloop:
596   {
597     mute = pulsesrc->mute;
598     GST_DEBUG_OBJECT (pulsesrc, "we have no mainloop");
599     return mute;
600   }
601 no_index:
602   {
603     mute = pulsesrc->mute;
604     GST_DEBUG_OBJECT (pulsesrc, "we don't have a stream index");
605     return mute;
606   }
607 info_failed:
608   {
609     GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
610         ("pa_context_get_source_output_info() failed: %s",
611             pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
612     goto unlock;
613   }
614 }
615
616 static void
617 gst_pulsesrc_set_stream_volume (GstPulseSrc * pulsesrc, gdouble volume)
618 {
619   pa_cvolume v;
620   pa_operation *o = NULL;
621
622   if (!pulsesrc->mainloop)
623     goto no_mainloop;
624
625   if (!pulsesrc->source_output_idx)
626     goto no_index;
627
628   pa_threaded_mainloop_lock (pulsesrc->mainloop);
629
630   GST_DEBUG_OBJECT (pulsesrc, "setting volume to %f", volume);
631
632   gst_pulse_cvolume_from_linear (&v, pulsesrc->sample_spec.channels, volume);
633
634   if (!(o = pa_context_set_source_output_volume (pulsesrc->context,
635               pulsesrc->source_output_idx, &v, NULL, NULL)))
636     goto volume_failed;
637
638   /* We don't really care about the result of this call */
639 unlock:
640
641   if (o)
642     pa_operation_unref (o);
643
644   pa_threaded_mainloop_unlock (pulsesrc->mainloop);
645
646   return;
647
648   /* ERRORS */
649 no_mainloop:
650   {
651     pulsesrc->volume = volume;
652     pulsesrc->volume_set = TRUE;
653     GST_DEBUG_OBJECT (pulsesrc, "we have no mainloop");
654     return;
655   }
656 no_index:
657   {
658     pulsesrc->volume = volume;
659     pulsesrc->volume_set = TRUE;
660     GST_DEBUG_OBJECT (pulsesrc, "we don't have a stream index");
661     return;
662   }
663 volume_failed:
664   {
665     GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
666         ("pa_stream_set_source_output_volume() failed: %s",
667             pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
668     goto unlock;
669   }
670 }
671
672 static void
673 gst_pulsesrc_set_stream_mute (GstPulseSrc * pulsesrc, gboolean mute)
674 {
675   pa_operation *o = NULL;
676
677   if (!pulsesrc->mainloop)
678     goto no_mainloop;
679
680   if (!pulsesrc->source_output_idx)
681     goto no_index;
682
683   pa_threaded_mainloop_lock (pulsesrc->mainloop);
684
685   GST_DEBUG_OBJECT (pulsesrc, "setting mute state to %d", mute);
686
687   if (!(o = pa_context_set_source_output_mute (pulsesrc->context,
688               pulsesrc->source_output_idx, mute, NULL, NULL)))
689     goto mute_failed;
690
691   /* We don't really care about the result of this call */
692 unlock:
693
694   if (o)
695     pa_operation_unref (o);
696
697   pa_threaded_mainloop_unlock (pulsesrc->mainloop);
698
699   return;
700
701   /* ERRORS */
702 no_mainloop:
703   {
704     pulsesrc->mute = mute;
705     pulsesrc->mute_set = TRUE;
706     GST_DEBUG_OBJECT (pulsesrc, "we have no mainloop");
707     return;
708   }
709 no_index:
710   {
711     pulsesrc->mute = mute;
712     pulsesrc->mute_set = TRUE;
713     GST_DEBUG_OBJECT (pulsesrc, "we don't have a stream index");
714     return;
715   }
716 mute_failed:
717   {
718     GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
719         ("pa_stream_set_source_output_mute() failed: %s",
720             pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
721     goto unlock;
722   }
723 }
724 #endif
725
726 static void
727 gst_pulsesrc_set_property (GObject * object,
728     guint prop_id, const GValue * value, GParamSpec * pspec)
729 {
730
731   GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (object);
732
733   switch (prop_id) {
734     case PROP_SERVER:
735       g_free (pulsesrc->server);
736       pulsesrc->server = g_value_dup_string (value);
737       if (pulsesrc->probe)
738         gst_pulseprobe_set_server (pulsesrc->probe, pulsesrc->server);
739       break;
740     case PROP_DEVICE:
741       g_free (pulsesrc->device);
742       pulsesrc->device = g_value_dup_string (value);
743       break;
744     case PROP_CLIENT:
745       g_free (pulsesrc->client_name);
746       if (!g_value_get_string (value)) {
747         GST_WARNING_OBJECT (pulsesrc,
748             "Empty PulseAudio client name not allowed. Resetting to default value");
749         pulsesrc->client_name = gst_pulse_client_name ();
750       } else
751         pulsesrc->client_name = g_value_dup_string (value);
752       break;
753     case PROP_STREAM_PROPERTIES:
754       if (pulsesrc->properties)
755         gst_structure_free (pulsesrc->properties);
756       pulsesrc->properties =
757           gst_structure_copy (gst_value_get_structure (value));
758       if (pulsesrc->proplist)
759         pa_proplist_free (pulsesrc->proplist);
760       pulsesrc->proplist = gst_pulse_make_proplist (pulsesrc->properties);
761       break;
762 #ifdef HAVE_PULSE_1_0
763     case PROP_VOLUME:
764       gst_pulsesrc_set_stream_volume (pulsesrc, g_value_get_double (value));
765       break;
766     case PROP_MUTE:
767       gst_pulsesrc_set_stream_mute (pulsesrc, g_value_get_boolean (value));
768       break;
769 #endif
770     default:
771       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
772       break;
773   }
774 }
775
776 static void
777 gst_pulsesrc_get_property (GObject * object,
778     guint prop_id, GValue * value, GParamSpec * pspec)
779 {
780
781   GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (object);
782
783   switch (prop_id) {
784     case PROP_SERVER:
785       g_value_set_string (value, pulsesrc->server);
786       break;
787     case PROP_DEVICE:
788       g_value_set_string (value, pulsesrc->device);
789       break;
790     case PROP_DEVICE_NAME:
791       g_value_take_string (value, gst_pulsesrc_device_description (pulsesrc));
792       break;
793     case PROP_CLIENT:
794       g_value_set_string (value, pulsesrc->client_name);
795       break;
796     case PROP_STREAM_PROPERTIES:
797       gst_value_set_structure (value, pulsesrc->properties);
798       break;
799     case PROP_SOURCE_OUTPUT_INDEX:
800       g_value_set_uint (value, pulsesrc->source_output_idx);
801       break;
802 #ifdef HAVE_PULSE_1_0
803     case PROP_VOLUME:
804       g_value_set_double (value, gst_pulsesrc_get_stream_volume (pulsesrc));
805       break;
806     case PROP_MUTE:
807       g_value_set_boolean (value, gst_pulsesrc_get_stream_mute (pulsesrc));
808       break;
809 #endif
810     default:
811       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
812       break;
813   }
814 }
815
816 static void
817 gst_pulsesrc_context_state_cb (pa_context * c, void *userdata)
818 {
819   GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
820
821   switch (pa_context_get_state (c)) {
822     case PA_CONTEXT_READY:
823     case PA_CONTEXT_TERMINATED:
824     case PA_CONTEXT_FAILED:
825       pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
826       break;
827
828     case PA_CONTEXT_UNCONNECTED:
829     case PA_CONTEXT_CONNECTING:
830     case PA_CONTEXT_AUTHORIZING:
831     case PA_CONTEXT_SETTING_NAME:
832       break;
833   }
834 }
835
836 static void
837 gst_pulsesrc_stream_state_cb (pa_stream * s, void *userdata)
838 {
839   GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
840
841   switch (pa_stream_get_state (s)) {
842
843     case PA_STREAM_READY:
844     case PA_STREAM_FAILED:
845     case PA_STREAM_TERMINATED:
846       pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
847       break;
848
849     case PA_STREAM_UNCONNECTED:
850     case PA_STREAM_CREATING:
851       break;
852   }
853 }
854
855 static void
856 gst_pulsesrc_stream_request_cb (pa_stream * s, size_t length, void *userdata)
857 {
858   GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
859
860   GST_LOG_OBJECT (pulsesrc, "got request for length %" G_GSIZE_FORMAT, length);
861
862   if (pulsesrc->in_read) {
863     /* only signal when reading */
864     pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
865   }
866 }
867
868 static void
869 gst_pulsesrc_stream_latency_update_cb (pa_stream * s, void *userdata)
870 {
871   const pa_timing_info *info;
872   pa_usec_t source_usec;
873
874   info = pa_stream_get_timing_info (s);
875
876   if (!info) {
877     GST_LOG_OBJECT (GST_PULSESRC_CAST (userdata),
878         "latency update (information unknown)");
879     return;
880   }
881   source_usec = info->configured_source_usec;
882
883   GST_LOG_OBJECT (GST_PULSESRC_CAST (userdata),
884       "latency_update, %" G_GUINT64_FORMAT ", %d:%" G_GINT64_FORMAT ", %d:%"
885       G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT,
886       GST_TIMEVAL_TO_TIME (info->timestamp), info->write_index_corrupt,
887       info->write_index, info->read_index_corrupt, info->read_index,
888       info->source_usec, source_usec);
889 }
890
891 static void
892 gst_pulsesrc_stream_underflow_cb (pa_stream * s, void *userdata)
893 {
894   GST_WARNING_OBJECT (GST_PULSESRC_CAST (userdata), "Got underflow");
895 }
896
897 static void
898 gst_pulsesrc_stream_overflow_cb (pa_stream * s, void *userdata)
899 {
900   GST_WARNING_OBJECT (GST_PULSESRC_CAST (userdata), "Got overflow");
901 }
902
903 #ifdef HAVE_PULSE_1_0
904 static void
905 gst_pulsesrc_context_subscribe_cb (pa_context * c,
906     pa_subscription_event_type_t t, uint32_t idx, void *userdata)
907 {
908   GstPulseSrc *psrc = GST_PULSESRC (userdata);
909
910   if (t != (PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT | PA_SUBSCRIPTION_EVENT_CHANGE)
911       && t != (PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT | PA_SUBSCRIPTION_EVENT_NEW))
912     return;
913
914   if (idx != psrc->source_output_idx)
915     return;
916
917   /* Actually this event is also triggered when other properties of the stream
918    * change that are unrelated to the volume. However it is probably cheaper to
919    * signal the change here and check for the volume when the GObject property
920    * is read instead of querying it always. */
921
922   /* inform streaming thread to notify */
923   g_atomic_int_compare_and_exchange (&psrc->notify, 0, 1);
924 }
925 #endif
926
927 static gboolean
928 gst_pulsesrc_open (GstAudioSrc * asrc)
929 {
930   GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
931
932   pa_threaded_mainloop_lock (pulsesrc->mainloop);
933
934   g_assert (!pulsesrc->context);
935   g_assert (!pulsesrc->stream);
936
937   GST_DEBUG_OBJECT (pulsesrc, "opening device");
938
939   if (!(pulsesrc->context =
940           pa_context_new (pa_threaded_mainloop_get_api (pulsesrc->mainloop),
941               pulsesrc->client_name))) {
942     GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Failed to create context"),
943         (NULL));
944     goto unlock_and_fail;
945   }
946
947   pa_context_set_state_callback (pulsesrc->context,
948       gst_pulsesrc_context_state_cb, pulsesrc);
949 #ifdef HAVE_PULSE_1_0
950   pa_context_set_subscribe_callback (pulsesrc->context,
951       gst_pulsesrc_context_subscribe_cb, pulsesrc);
952 #endif
953
954   GST_DEBUG_OBJECT (pulsesrc, "connect to server %s",
955       GST_STR_NULL (pulsesrc->server));
956
957   if (pa_context_connect (pulsesrc->context, pulsesrc->server, 0, NULL) < 0) {
958     GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Failed to connect: %s",
959             pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
960     goto unlock_and_fail;
961   }
962
963   for (;;) {
964     pa_context_state_t state;
965
966     state = pa_context_get_state (pulsesrc->context);
967
968     if (!PA_CONTEXT_IS_GOOD (state)) {
969       GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Failed to connect: %s",
970               pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
971       goto unlock_and_fail;
972     }
973
974     if (state == PA_CONTEXT_READY)
975       break;
976
977     /* Wait until the context is ready */
978     pa_threaded_mainloop_wait (pulsesrc->mainloop);
979   }
980   GST_DEBUG_OBJECT (pulsesrc, "connected");
981
982   pa_threaded_mainloop_unlock (pulsesrc->mainloop);
983
984   return TRUE;
985
986   /* ERRORS */
987 unlock_and_fail:
988   {
989     gst_pulsesrc_destroy_context (pulsesrc);
990
991     pa_threaded_mainloop_unlock (pulsesrc->mainloop);
992
993     return FALSE;
994   }
995 }
996
997 static gboolean
998 gst_pulsesrc_close (GstAudioSrc * asrc)
999 {
1000   GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
1001
1002   pa_threaded_mainloop_lock (pulsesrc->mainloop);
1003   gst_pulsesrc_destroy_context (pulsesrc);
1004   pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1005
1006   return TRUE;
1007 }
1008
1009 static gboolean
1010 gst_pulsesrc_unprepare (GstAudioSrc * asrc)
1011 {
1012   GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
1013
1014   pa_threaded_mainloop_lock (pulsesrc->mainloop);
1015   gst_pulsesrc_destroy_stream (pulsesrc);
1016
1017   pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1018
1019   pulsesrc->read_buffer = NULL;
1020   pulsesrc->read_buffer_length = 0;
1021
1022   return TRUE;
1023 }
1024
1025 static guint
1026 gst_pulsesrc_read (GstAudioSrc * asrc, gpointer data, guint length)
1027 {
1028   GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
1029   size_t sum = 0;
1030
1031   pa_threaded_mainloop_lock (pulsesrc->mainloop);
1032   pulsesrc->in_read = TRUE;
1033
1034 #ifdef HAVE_PULSE_1_0
1035   if (g_atomic_int_compare_and_exchange (&pulsesrc->notify, 1, 0)) {
1036     g_object_notify (G_OBJECT (pulsesrc), "volume");
1037     g_object_notify (G_OBJECT (pulsesrc), "mute");
1038   }
1039 #endif
1040
1041   if (pulsesrc->paused)
1042     goto was_paused;
1043
1044   while (length > 0) {
1045     size_t l;
1046
1047     GST_LOG_OBJECT (pulsesrc, "reading %u bytes", length);
1048
1049     /*check if we have a leftover buffer */
1050     if (!pulsesrc->read_buffer) {
1051       for (;;) {
1052         if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
1053           goto unlock_and_fail;
1054
1055         /* read all available data, we keep a pointer to the data and the length
1056          * and take from it what we need. */
1057         if (pa_stream_peek (pulsesrc->stream, &pulsesrc->read_buffer,
1058                 &pulsesrc->read_buffer_length) < 0)
1059           goto peek_failed;
1060
1061         GST_LOG_OBJECT (pulsesrc, "have data of %" G_GSIZE_FORMAT " bytes",
1062             pulsesrc->read_buffer_length);
1063
1064         /* if we have data, process if */
1065         if (pulsesrc->read_buffer && pulsesrc->read_buffer_length)
1066           break;
1067
1068         /* now wait for more data to become available */
1069         GST_LOG_OBJECT (pulsesrc, "waiting for data");
1070         pa_threaded_mainloop_wait (pulsesrc->mainloop);
1071
1072         if (pulsesrc->paused)
1073           goto was_paused;
1074       }
1075     }
1076
1077     l = pulsesrc->read_buffer_length >
1078         length ? length : pulsesrc->read_buffer_length;
1079
1080     memcpy (data, pulsesrc->read_buffer, l);
1081
1082     pulsesrc->read_buffer = (const guint8 *) pulsesrc->read_buffer + l;
1083     pulsesrc->read_buffer_length -= l;
1084
1085     data = (guint8 *) data + l;
1086     length -= l;
1087     sum += l;
1088
1089     if (pulsesrc->read_buffer_length <= 0) {
1090       /* we copied all of the data, drop it now */
1091       if (pa_stream_drop (pulsesrc->stream) < 0)
1092         goto drop_failed;
1093
1094       /* reset pointer to data */
1095       pulsesrc->read_buffer = NULL;
1096       pulsesrc->read_buffer_length = 0;
1097     }
1098   }
1099
1100   pulsesrc->in_read = FALSE;
1101   pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1102
1103   return sum;
1104
1105   /* ERRORS */
1106 was_paused:
1107   {
1108     GST_LOG_OBJECT (pulsesrc, "we are paused");
1109     goto unlock_and_fail;
1110   }
1111 peek_failed:
1112   {
1113     GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1114         ("pa_stream_peek() failed: %s",
1115             pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1116     goto unlock_and_fail;
1117   }
1118 drop_failed:
1119   {
1120     GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1121         ("pa_stream_drop() failed: %s",
1122             pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1123     goto unlock_and_fail;
1124   }
1125 unlock_and_fail:
1126   {
1127     pulsesrc->in_read = FALSE;
1128     pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1129
1130     return (guint) - 1;
1131   }
1132 }
1133
1134 /* return the delay in samples */
1135 static guint
1136 gst_pulsesrc_delay (GstAudioSrc * asrc)
1137 {
1138   GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
1139   pa_usec_t t;
1140   int negative, res;
1141   guint result;
1142
1143   pa_threaded_mainloop_lock (pulsesrc->mainloop);
1144   if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
1145     goto server_dead;
1146
1147   /* get the latency, this can fail when we don't have a latency update yet.
1148    * We don't want to wait for latency updates here but we just return 0. */
1149   res = pa_stream_get_latency (pulsesrc->stream, &t, &negative);
1150
1151   pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1152
1153   if (res > 0) {
1154     GST_DEBUG_OBJECT (pulsesrc, "could not get latency");
1155     result = 0;
1156   } else {
1157     if (negative)
1158       result = 0;
1159     else
1160       result = (guint) ((t * pulsesrc->sample_spec.rate) / 1000000LL);
1161   }
1162   return result;
1163
1164   /* ERRORS */
1165 server_dead:
1166   {
1167     GST_DEBUG_OBJECT (pulsesrc, "the server is dead");
1168     pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1169     return 0;
1170   }
1171 }
1172
1173 static gboolean
1174 gst_pulsesrc_create_stream (GstPulseSrc * pulsesrc, GstCaps * caps)
1175 {
1176   pa_channel_map channel_map;
1177   GstStructure *s;
1178   gboolean need_channel_layout = FALSE;
1179   GstAudioRingBufferSpec spec;
1180   const gchar *name;
1181
1182   memset (&spec, 0, sizeof (GstAudioRingBufferSpec));
1183   spec.latency_time = GST_SECOND;
1184   if (!gst_audio_ring_buffer_parse_caps (&spec, caps))
1185     goto invalid_caps;
1186
1187   /* Keep the refcount of the caps at 1 to make them writable */
1188   gst_caps_unref (spec.caps);
1189
1190   if (!gst_pulse_fill_sample_spec (&spec, &pulsesrc->sample_spec))
1191     goto invalid_spec;
1192
1193   pa_threaded_mainloop_lock (pulsesrc->mainloop);
1194
1195   if (!pulsesrc->context)
1196     goto bad_context;
1197
1198   s = gst_caps_get_structure (caps, 0);
1199   if (!gst_structure_has_field (s, "channel-layout") ||
1200       !gst_pulse_gst_to_channel_map (&channel_map, &spec)) {
1201     if (spec.info.channels == 1)
1202       pa_channel_map_init_mono (&channel_map);
1203     else if (spec.info.channels == 2)
1204       pa_channel_map_init_stereo (&channel_map);
1205     else
1206       need_channel_layout = TRUE;
1207   }
1208
1209   name = "Record Stream";
1210   if (pulsesrc->proplist) {
1211     if (!(pulsesrc->stream = pa_stream_new_with_proplist (pulsesrc->context,
1212                 name, &pulsesrc->sample_spec,
1213                 (need_channel_layout) ? NULL : &channel_map,
1214                 pulsesrc->proplist)))
1215       goto create_failed;
1216
1217   } else if (!(pulsesrc->stream = pa_stream_new (pulsesrc->context,
1218               name, &pulsesrc->sample_spec,
1219               (need_channel_layout) ? NULL : &channel_map)))
1220     goto create_failed;
1221
1222   if (need_channel_layout) {
1223     const pa_channel_map *m = pa_stream_get_channel_map (pulsesrc->stream);
1224
1225     gst_pulse_channel_map_to_gst (m, &spec);
1226     caps = spec.caps;
1227   }
1228
1229   GST_DEBUG_OBJECT (pulsesrc, "Caps are %" GST_PTR_FORMAT, caps);
1230
1231   pa_stream_set_state_callback (pulsesrc->stream, gst_pulsesrc_stream_state_cb,
1232       pulsesrc);
1233   pa_stream_set_read_callback (pulsesrc->stream, gst_pulsesrc_stream_request_cb,
1234       pulsesrc);
1235   pa_stream_set_underflow_callback (pulsesrc->stream,
1236       gst_pulsesrc_stream_underflow_cb, pulsesrc);
1237   pa_stream_set_overflow_callback (pulsesrc->stream,
1238       gst_pulsesrc_stream_overflow_cb, pulsesrc);
1239   pa_stream_set_latency_update_callback (pulsesrc->stream,
1240       gst_pulsesrc_stream_latency_update_cb, pulsesrc);
1241
1242   pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1243
1244   return TRUE;
1245
1246   /* ERRORS */
1247 invalid_caps:
1248   {
1249     GST_ELEMENT_ERROR (pulsesrc, RESOURCE, SETTINGS,
1250         ("Can't parse caps."), (NULL));
1251     goto fail;
1252   }
1253 invalid_spec:
1254   {
1255     GST_ELEMENT_ERROR (pulsesrc, RESOURCE, SETTINGS,
1256         ("Invalid sample specification."), (NULL));
1257     goto fail;
1258   }
1259 bad_context:
1260   {
1261     GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Bad context"), (NULL));
1262     goto unlock_and_fail;
1263   }
1264 create_failed:
1265   {
1266     GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1267         ("Failed to create stream: %s",
1268             pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1269     goto unlock_and_fail;
1270   }
1271 unlock_and_fail:
1272   {
1273     gst_pulsesrc_destroy_stream (pulsesrc);
1274
1275     pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1276
1277   fail:
1278     return FALSE;
1279   }
1280 }
1281
1282 /* This is essentially gst_base_src_negotiate_default() but the caps
1283  * are guaranteed to have a channel layout for > 2 channels
1284  */
1285 static gboolean
1286 gst_pulsesrc_negotiate (GstBaseSrc * basesrc)
1287 {
1288   GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (basesrc);
1289   GstCaps *thiscaps;
1290   GstCaps *caps = NULL;
1291   GstCaps *peercaps = NULL;
1292   gboolean result = FALSE;
1293
1294   /* first see what is possible on our source pad */
1295   thiscaps = gst_pad_query_caps (GST_BASE_SRC_PAD (basesrc), NULL);
1296   GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
1297   /* nothing or anything is allowed, we're done */
1298   if (thiscaps == NULL || gst_caps_is_any (thiscaps))
1299     goto no_nego_needed;
1300
1301   /* get the peer caps */
1302   peercaps = gst_pad_peer_query_caps (GST_BASE_SRC_PAD (basesrc), NULL);
1303   GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
1304   if (peercaps) {
1305     /* get intersection */
1306     caps = gst_caps_intersect (thiscaps, peercaps);
1307     GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, caps);
1308     gst_caps_unref (thiscaps);
1309     gst_caps_unref (peercaps);
1310   } else {
1311     /* no peer, work with our own caps then */
1312     caps = thiscaps;
1313   }
1314   if (caps) {
1315     /* take first (and best, since they are sorted) possibility */
1316     caps = gst_caps_make_writable (caps);
1317     gst_caps_truncate (caps);
1318
1319     /* now fixate */
1320     if (!gst_caps_is_empty (caps)) {
1321       GST_BASE_SRC_CLASS (parent_class)->fixate (basesrc, caps);
1322       GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
1323
1324       if (gst_caps_is_any (caps)) {
1325         /* hmm, still anything, so element can do anything and
1326          * nego is not needed */
1327         result = TRUE;
1328       } else if (gst_caps_is_fixed (caps)) {
1329         /* yay, fixed caps, use those then */
1330         result = gst_pulsesrc_create_stream (pulsesrc, caps);
1331         if (result)
1332           result = gst_base_src_set_caps (basesrc, caps);
1333       }
1334     }
1335     gst_caps_unref (caps);
1336   }
1337   return result;
1338
1339 no_nego_needed:
1340   {
1341     GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
1342     if (thiscaps)
1343       gst_caps_unref (thiscaps);
1344     return TRUE;
1345   }
1346 }
1347
1348 static gboolean
1349 gst_pulsesrc_prepare (GstAudioSrc * asrc, GstAudioRingBufferSpec * spec)
1350 {
1351   pa_buffer_attr wanted;
1352   const pa_buffer_attr *actual;
1353   GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
1354   pa_stream_flags_t flags;
1355 #ifdef HAVE_PULSE_1_0
1356   pa_operation *o;
1357 #endif
1358
1359   pa_threaded_mainloop_lock (pulsesrc->mainloop);
1360
1361 #ifdef HAVE_PULSE_1_0
1362   /* enable event notifications */
1363   GST_LOG_OBJECT (pulsesrc, "subscribing to context events");
1364   if (!(o = pa_context_subscribe (pulsesrc->context,
1365               PA_SUBSCRIPTION_MASK_SINK_INPUT, NULL, NULL))) {
1366     GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1367         ("pa_context_subscribe() failed: %s",
1368             pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1369     goto unlock_and_fail;
1370   }
1371
1372   pa_operation_unref (o);
1373 #endif
1374
1375   wanted.maxlength = -1;
1376   wanted.tlength = -1;
1377   wanted.prebuf = 0;
1378   wanted.minreq = -1;
1379   wanted.fragsize = spec->segsize;
1380
1381   GST_INFO_OBJECT (pulsesrc, "maxlength: %d", wanted.maxlength);
1382   GST_INFO_OBJECT (pulsesrc, "tlength:   %d", wanted.tlength);
1383   GST_INFO_OBJECT (pulsesrc, "prebuf:    %d", wanted.prebuf);
1384   GST_INFO_OBJECT (pulsesrc, "minreq:    %d", wanted.minreq);
1385   GST_INFO_OBJECT (pulsesrc, "fragsize:  %d", wanted.fragsize);
1386
1387   flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE |
1388       PA_STREAM_NOT_MONOTONIC | PA_STREAM_ADJUST_LATENCY |
1389       PA_STREAM_START_CORKED;
1390
1391 #ifdef HAVE_PULSE_1_0
1392   if (pulsesrc->mute_set && pulsesrc->mute)
1393     flags |= PA_STREAM_START_MUTED;
1394 #endif
1395
1396   if (pa_stream_connect_record (pulsesrc->stream, pulsesrc->device, &wanted,
1397           flags) < 0) {
1398     goto connect_failed;
1399   }
1400
1401   pulsesrc->corked = TRUE;
1402
1403   for (;;) {
1404     pa_stream_state_t state;
1405
1406     state = pa_stream_get_state (pulsesrc->stream);
1407
1408     if (!PA_STREAM_IS_GOOD (state))
1409       goto stream_is_bad;
1410
1411     if (state == PA_STREAM_READY)
1412       break;
1413
1414     /* Wait until the stream is ready */
1415     pa_threaded_mainloop_wait (pulsesrc->mainloop);
1416   }
1417   pulsesrc->stream_connected = TRUE;
1418
1419   /* store the source output index so it can be accessed via a property */
1420   pulsesrc->source_output_idx = pa_stream_get_index (pulsesrc->stream);
1421   g_object_notify (G_OBJECT (pulsesrc), "source-output-index");
1422
1423 #ifdef HAVE_PULSE_1_0
1424   if (pulsesrc->volume_set) {
1425     gst_pulsesrc_set_stream_volume (pulsesrc, pulsesrc->volume);
1426     pulsesrc->volume_set = FALSE;
1427   }
1428 #endif
1429
1430   /* get the actual buffering properties now */
1431   actual = pa_stream_get_buffer_attr (pulsesrc->stream);
1432
1433   GST_INFO_OBJECT (pulsesrc, "maxlength: %d", actual->maxlength);
1434   GST_INFO_OBJECT (pulsesrc, "tlength:   %d (wanted: %d)",
1435       actual->tlength, wanted.tlength);
1436   GST_INFO_OBJECT (pulsesrc, "prebuf:    %d", actual->prebuf);
1437   GST_INFO_OBJECT (pulsesrc, "minreq:    %d (wanted %d)", actual->minreq,
1438       wanted.minreq);
1439   GST_INFO_OBJECT (pulsesrc, "fragsize:  %d (wanted %d)",
1440       actual->fragsize, wanted.fragsize);
1441
1442   if (actual->fragsize >= wanted.fragsize) {
1443     spec->segsize = actual->fragsize;
1444   } else {
1445     spec->segsize = actual->fragsize * (wanted.fragsize / actual->fragsize);
1446   }
1447   spec->segtotal = actual->maxlength / spec->segsize;
1448
1449   if (!pulsesrc->paused) {
1450     GST_DEBUG_OBJECT (pulsesrc, "uncorking because we are playing");
1451     gst_pulsesrc_set_corked (pulsesrc, FALSE, FALSE);
1452   }
1453   pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1454
1455   return TRUE;
1456
1457   /* ERRORS */
1458 connect_failed:
1459   {
1460     GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1461         ("Failed to connect stream: %s",
1462             pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1463     goto unlock_and_fail;
1464   }
1465 stream_is_bad:
1466   {
1467     GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1468         ("Failed to connect stream: %s",
1469             pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1470     goto unlock_and_fail;
1471   }
1472 unlock_and_fail:
1473   {
1474     gst_pulsesrc_destroy_stream (pulsesrc);
1475
1476     pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1477     return FALSE;
1478   }
1479 }
1480
1481 static void
1482 gst_pulsesrc_success_cb (pa_stream * s, int success, void *userdata)
1483 {
1484   GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
1485
1486   pulsesrc->operation_success = ! !success;
1487   pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
1488 }
1489
1490 static void
1491 gst_pulsesrc_reset (GstAudioSrc * asrc)
1492 {
1493   GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
1494   pa_operation *o = NULL;
1495
1496   pa_threaded_mainloop_lock (pulsesrc->mainloop);
1497   GST_DEBUG_OBJECT (pulsesrc, "reset");
1498
1499   if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
1500     goto unlock_and_fail;
1501
1502   if (!(o =
1503           pa_stream_flush (pulsesrc->stream, gst_pulsesrc_success_cb,
1504               pulsesrc))) {
1505     GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1506         ("pa_stream_flush() failed: %s",
1507             pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1508     goto unlock_and_fail;
1509   }
1510
1511   pulsesrc->paused = TRUE;
1512   /* Inform anyone waiting in _write() call that it shall wakeup */
1513   if (pulsesrc->in_read) {
1514     pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
1515   }
1516
1517   pulsesrc->operation_success = FALSE;
1518   while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1519
1520     if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
1521       goto unlock_and_fail;
1522
1523     pa_threaded_mainloop_wait (pulsesrc->mainloop);
1524   }
1525
1526   if (!pulsesrc->operation_success) {
1527     GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Flush failed: %s",
1528             pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1529     goto unlock_and_fail;
1530   }
1531
1532 unlock_and_fail:
1533
1534   if (o) {
1535     pa_operation_cancel (o);
1536     pa_operation_unref (o);
1537   }
1538
1539   pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1540 }
1541
1542 /* update the corked state of a stream, must be called with the mainloop
1543  * lock */
1544 static gboolean
1545 gst_pulsesrc_set_corked (GstPulseSrc * psrc, gboolean corked, gboolean wait)
1546 {
1547   pa_operation *o = NULL;
1548   gboolean res = FALSE;
1549
1550   GST_DEBUG_OBJECT (psrc, "setting corked state to %d", corked);
1551   if (!psrc->stream_connected)
1552     return TRUE;
1553
1554   if (psrc->corked != corked) {
1555     if (!(o = pa_stream_cork (psrc->stream, corked,
1556                 gst_pulsesrc_success_cb, psrc)))
1557       goto cork_failed;
1558
1559     while (wait && pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1560       pa_threaded_mainloop_wait (psrc->mainloop);
1561       if (gst_pulsesrc_is_dead (psrc, TRUE))
1562         goto server_dead;
1563     }
1564     psrc->corked = corked;
1565   } else {
1566     GST_DEBUG_OBJECT (psrc, "skipping, already in requested state");
1567   }
1568   res = TRUE;
1569
1570 cleanup:
1571   if (o)
1572     pa_operation_unref (o);
1573
1574   return res;
1575
1576   /* ERRORS */
1577 server_dead:
1578   {
1579     GST_DEBUG_OBJECT (psrc, "the server is dead");
1580     goto cleanup;
1581   }
1582 cork_failed:
1583   {
1584     GST_ELEMENT_ERROR (psrc, RESOURCE, FAILED,
1585         ("pa_stream_cork() failed: %s",
1586             pa_strerror (pa_context_errno (psrc->context))), (NULL));
1587     goto cleanup;
1588   }
1589 }
1590
1591 /* start/resume playback ASAP */
1592 static gboolean
1593 gst_pulsesrc_play (GstPulseSrc * psrc)
1594 {
1595   pa_threaded_mainloop_lock (psrc->mainloop);
1596   GST_DEBUG_OBJECT (psrc, "playing");
1597   psrc->paused = FALSE;
1598   gst_pulsesrc_set_corked (psrc, FALSE, FALSE);
1599   pa_threaded_mainloop_unlock (psrc->mainloop);
1600
1601   return TRUE;
1602 }
1603
1604 /* pause/stop playback ASAP */
1605 static gboolean
1606 gst_pulsesrc_pause (GstPulseSrc * psrc)
1607 {
1608   pa_threaded_mainloop_lock (psrc->mainloop);
1609   GST_DEBUG_OBJECT (psrc, "pausing");
1610   /* make sure the commit method stops writing */
1611   psrc->paused = TRUE;
1612   if (psrc->in_read) {
1613     /* we are waiting in a read, signal */
1614     GST_DEBUG_OBJECT (psrc, "signal read");
1615     pa_threaded_mainloop_signal (psrc->mainloop, 0);
1616   }
1617   pa_threaded_mainloop_unlock (psrc->mainloop);
1618
1619   return TRUE;
1620 }
1621
1622 static GstStateChangeReturn
1623 gst_pulsesrc_change_state (GstElement * element, GstStateChange transition)
1624 {
1625   GstStateChangeReturn ret;
1626   GstPulseSrc *this = GST_PULSESRC_CAST (element);
1627
1628   switch (transition) {
1629     case GST_STATE_CHANGE_NULL_TO_READY:
1630       this->mainloop = pa_threaded_mainloop_new ();
1631       g_assert (this->mainloop);
1632
1633       pa_threaded_mainloop_start (this->mainloop);
1634
1635       if (!this->mixer)
1636         this->mixer =
1637             gst_pulsemixer_ctrl_new (G_OBJECT (this), this->server,
1638             this->device, GST_PULSEMIXER_SOURCE);
1639       break;
1640     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1641       /* uncork and start recording */
1642       gst_pulsesrc_play (this);
1643       break;
1644     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1645       /* stop recording ASAP by corking */
1646       pa_threaded_mainloop_lock (this->mainloop);
1647       GST_DEBUG_OBJECT (this, "corking");
1648       gst_pulsesrc_set_corked (this, TRUE, FALSE);
1649       pa_threaded_mainloop_unlock (this->mainloop);
1650       break;
1651     default:
1652       break;
1653   }
1654
1655   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1656
1657   switch (transition) {
1658     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1659       /* now make sure we get out of the _read method */
1660       gst_pulsesrc_pause (this);
1661       break;
1662     case GST_STATE_CHANGE_READY_TO_NULL:
1663       if (this->mixer) {
1664         gst_pulsemixer_ctrl_free (this->mixer);
1665         this->mixer = NULL;
1666       }
1667
1668       if (this->mainloop)
1669         pa_threaded_mainloop_stop (this->mainloop);
1670
1671       gst_pulsesrc_destroy_context (this);
1672
1673       if (this->mainloop) {
1674         pa_threaded_mainloop_free (this->mainloop);
1675         this->mainloop = NULL;
1676       }
1677       break;
1678     default:
1679       break;
1680   }
1681
1682   return ret;
1683 }