1 /*-*- Mode: C; c-basic-offset: 2 -*-*/
3 /* GStreamer pulseaudio plugin
5 * Copyright (c) 2011 Intel Corporation
7 * 2011 Arun Raghavan <arun.raghavan@collabora.co.uk>
8 * 2011 Sebastian Dröge <sebastian.droege@collabora.co.uk>
10 * gst-pulse is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2.1 of the
13 * License, or (at your option) any later version.
15 * gst-pulse is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with gst-pulse; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 * SECTION:element-pulseaudiosink
28 * @see_also: pulsesink, pulsesrc, pulsemixer
30 * This element outputs audio to a
31 * <ulink href="http://www.pulseaudio.org">PulseAudio sound server</ulink> via
32 * the @pulsesink element. It transparently takes care of passing compressed
33 * format as-is if the sink supports it, decoding if necessary, and changes
34 * to supported formats at runtime.
37 * <title>Example pipelines</title>
39 * gst-launch -v filesrc location=sine.ogg ! oggdemux ! vorbisdec ! pulseaudiosink
40 * ]| Decode and play an Ogg/Vorbis file.
42 * gst-launch -v filesrc location=test.mp3 ! mp3parse ! pulseaudiosink stream-properties="props,media.title=test"
43 * ]| Play an MP3 file on a sink that supports decoding directly, plug in a
44 * decoder if/when required.
54 #include <gst/pbutils/pbutils.h>
55 #include <gst/gst-i18n-plugin.h>
56 #include <gst/glib-compat-private.h>
58 #include <gst/audio/gstaudioiec61937.h>
59 #include "pulsesink.h"
61 GST_DEBUG_CATEGORY (pulseaudiosink_debug);
62 #define GST_CAT_DEFAULT (pulseaudiosink_debug)
64 #define GST_PULSE_AUDIO_SINK_LOCK(obj) G_STMT_START { \
65 GST_LOG_OBJECT (obj, \
66 "locking from thread %p", \
68 g_mutex_lock (GST_PULSE_AUDIO_SINK_CAST(obj)->lock); \
69 GST_LOG_OBJECT (obj, \
70 "locked from thread %p", \
74 #define GST_PULSE_AUDIO_SINK_UNLOCK(obj) G_STMT_START { \
75 GST_LOG_OBJECT (obj, \
76 "unlocking from thread %p", \
78 g_mutex_unlock (GST_PULSE_AUDIO_SINK_CAST(obj)->lock); \
87 GstPad *sink_proxypad;
88 GstPadEventFunction sinkpad_old_eventfunc;
89 GstPadEventFunction proxypad_old_eventfunc;
104 GstBinClass parent_class;
107 } GstPulseAudioSinkClass;
109 static void gst_pulse_audio_sink_get_property (GObject * object, guint prop_id,
110 GValue * value, GParamSpec * pspec);
111 static void gst_pulse_audio_sink_set_property (GObject * object, guint prop_id,
112 const GValue * value, GParamSpec * pspec);
113 static void gst_pulse_audio_sink_dispose (GObject * object);
114 static gboolean gst_pulse_audio_sink_src_event (GstPad * pad, GstEvent * event);
115 static gboolean gst_pulse_audio_sink_sink_event (GstPad * pad,
117 static gboolean gst_pulse_audio_sink_sink_acceptcaps (GstPad * pad,
119 static gboolean gst_pulse_audio_sink_sink_setcaps (GstPad * pad,
121 static GstStateChangeReturn
122 gst_pulse_audio_sink_change_state (GstElement * element,
123 GstStateChange transition);
126 gst_pulse_audio_sink_do_init (GType type)
128 GST_DEBUG_CATEGORY_INIT (pulseaudiosink_debug, "pulseaudiosink", 0,
129 "Bin that wraps pulsesink for handling compressed formats");
132 GST_BOILERPLATE_FULL (GstPulseAudioSink, gst_pulse_audio_sink, GstBin,
133 GST_TYPE_BIN, gst_pulse_audio_sink_do_init);
135 static GstStaticPadTemplate sink_template =
136 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
137 GST_STATIC_CAPS (PULSE_SINK_TEMPLATE_CAPS));
140 gst_pulse_audio_sink_base_init (gpointer klass)
142 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
144 gst_element_class_add_static_pad_template (element_class, &sink_template);
146 gst_element_class_set_details_simple (element_class,
147 "Bin wrapping pulsesink", "Sink/Audio/Bin",
148 "Correctly handles sink changes when streaming compressed formats to "
149 "pulsesink", "Arun Raghavan <arun.raghavan@collabora.co.uk>");
153 param_spec_copy (GParamSpec * spec)
155 const char *name, *nick, *blurb;
158 name = g_param_spec_get_name (spec);
159 nick = g_param_spec_get_nick (spec);
160 blurb = g_param_spec_get_blurb (spec);
163 if (G_PARAM_SPEC_TYPE (spec) == G_TYPE_PARAM_BOOLEAN) {
164 return g_param_spec_boolean (name, nick, blurb,
165 G_PARAM_SPEC_BOOLEAN (spec)->default_value, flags);
168 if (G_PARAM_SPEC_TYPE (spec) == G_TYPE_PARAM_BOXED) {
169 return g_param_spec_boxed (name, nick, blurb, spec->value_type, flags);
172 if (G_PARAM_SPEC_TYPE (spec) == G_TYPE_PARAM_CHAR) {
173 GParamSpecChar *cspec = G_PARAM_SPEC_CHAR (spec);
174 return g_param_spec_char (name, nick, blurb, cspec->minimum,
175 cspec->maximum, cspec->default_value, flags);
178 if (G_PARAM_SPEC_TYPE (spec) == G_TYPE_PARAM_DOUBLE) {
179 GParamSpecDouble *dspec = G_PARAM_SPEC_DOUBLE (spec);
180 return g_param_spec_double (name, nick, blurb, dspec->minimum,
181 dspec->maximum, dspec->default_value, flags);
184 if (G_PARAM_SPEC_TYPE (spec) == G_TYPE_PARAM_ENUM) {
185 return g_param_spec_enum (name, nick, blurb, spec->value_type,
186 G_PARAM_SPEC_ENUM (spec)->default_value, flags);
189 if (G_PARAM_SPEC_TYPE (spec) == G_TYPE_PARAM_FLAGS) {
190 return g_param_spec_flags (name, nick, blurb, spec->value_type,
191 G_PARAM_SPEC_ENUM (spec)->default_value, flags);
194 if (G_PARAM_SPEC_TYPE (spec) == G_TYPE_PARAM_FLOAT) {
195 GParamSpecFloat *fspec = G_PARAM_SPEC_FLOAT (spec);
196 return g_param_spec_double (name, nick, blurb, fspec->minimum,
197 fspec->maximum, fspec->default_value, flags);
200 if (G_PARAM_SPEC_TYPE (spec) == G_TYPE_PARAM_GTYPE) {
201 return g_param_spec_gtype (name, nick, blurb,
202 G_PARAM_SPEC_GTYPE (spec)->is_a_type, flags);
205 if (G_PARAM_SPEC_TYPE (spec) == G_TYPE_PARAM_INT) {
206 GParamSpecInt *ispec = G_PARAM_SPEC_INT (spec);
207 return g_param_spec_int (name, nick, blurb, ispec->minimum,
208 ispec->maximum, ispec->default_value, flags);
211 if (G_PARAM_SPEC_TYPE (spec) == G_TYPE_PARAM_INT64) {
212 GParamSpecInt64 *ispec = G_PARAM_SPEC_INT64 (spec);
213 return g_param_spec_int64 (name, nick, blurb, ispec->minimum,
214 ispec->maximum, ispec->default_value, flags);
217 if (G_PARAM_SPEC_TYPE (spec) == G_TYPE_PARAM_LONG) {
218 GParamSpecLong *lspec = G_PARAM_SPEC_LONG (spec);
219 return g_param_spec_long (name, nick, blurb, lspec->minimum,
220 lspec->maximum, lspec->default_value, flags);
223 if (G_PARAM_SPEC_TYPE (spec) == G_TYPE_PARAM_OBJECT) {
224 return g_param_spec_object (name, nick, blurb, spec->value_type, flags);
227 if (G_PARAM_SPEC_TYPE (spec) == G_TYPE_PARAM_PARAM) {
228 return g_param_spec_param (name, nick, blurb, spec->value_type, flags);
231 if (G_PARAM_SPEC_TYPE (spec) == G_TYPE_PARAM_POINTER) {
232 return g_param_spec_pointer (name, nick, blurb, flags);
235 if (G_PARAM_SPEC_TYPE (spec) == G_TYPE_PARAM_STRING) {
236 return g_param_spec_string (name, nick, blurb,
237 G_PARAM_SPEC_STRING (spec)->default_value, flags);
240 if (G_PARAM_SPEC_TYPE (spec) == G_TYPE_PARAM_UCHAR) {
241 GParamSpecUChar *cspec = G_PARAM_SPEC_UCHAR (spec);
242 return g_param_spec_uchar (name, nick, blurb, cspec->minimum,
243 cspec->maximum, cspec->default_value, flags);
246 if (G_PARAM_SPEC_TYPE (spec) == G_TYPE_PARAM_UINT) {
247 GParamSpecUInt *ispec = G_PARAM_SPEC_UINT (spec);
248 return g_param_spec_uint (name, nick, blurb, ispec->minimum,
249 ispec->maximum, ispec->default_value, flags);
252 if (G_PARAM_SPEC_TYPE (spec) == G_TYPE_PARAM_UINT64) {
253 GParamSpecUInt64 *ispec = G_PARAM_SPEC_UINT64 (spec);
254 return g_param_spec_uint64 (name, nick, blurb, ispec->minimum,
255 ispec->maximum, ispec->default_value, flags);
258 if (G_PARAM_SPEC_TYPE (spec) == G_TYPE_PARAM_ULONG) {
259 GParamSpecULong *lspec = G_PARAM_SPEC_ULONG (spec);
260 return g_param_spec_ulong (name, nick, blurb, lspec->minimum,
261 lspec->maximum, lspec->default_value, flags);
264 if (G_PARAM_SPEC_TYPE (spec) == G_TYPE_PARAM_UNICHAR) {
265 return g_param_spec_unichar (name, nick, blurb,
266 G_PARAM_SPEC_UNICHAR (spec)->default_value, flags);
269 if (G_PARAM_SPEC_TYPE (spec) == G_TYPE_PARAM_VARIANT) {
270 GParamSpecVariant *vspec = G_PARAM_SPEC_VARIANT (spec);
271 return g_param_spec_variant (name, nick, blurb, vspec->type,
272 vspec->default_value, flags);
275 if (G_PARAM_SPEC_TYPE (spec) == GST_TYPE_PARAM_MINI_OBJECT) {
276 return gst_param_spec_mini_object (name, nick, blurb, spec->value_type,
280 g_warning ("Unknown param type %ld for '%s'",
281 (long) G_PARAM_SPEC_TYPE (spec), name);
282 g_assert_not_reached ();
286 gst_pulse_audio_sink_class_init (GstPulseAudioSinkClass * klass)
288 GObjectClass *gobject_class = (GObjectClass *) klass;
289 GstElementClass *element_class = (GstElementClass *) klass;
290 GstPulseSinkClass *psink_class =
291 GST_PULSESINK_CLASS (g_type_class_ref (GST_TYPE_PULSESINK));
295 gobject_class->get_property = gst_pulse_audio_sink_get_property;
296 gobject_class->set_property = gst_pulse_audio_sink_set_property;
297 gobject_class->dispose = gst_pulse_audio_sink_dispose;
298 element_class->change_state =
299 GST_DEBUG_FUNCPTR (gst_pulse_audio_sink_change_state);
301 /* Find out how many properties we already have */
302 specs = g_object_class_list_properties (gobject_class, &klass->n_prop_own);
305 /* Proxy pulsesink's properties */
306 specs = g_object_class_list_properties (G_OBJECT_CLASS (psink_class), &n);
307 for (i = 0, j = klass->n_prop_own; i < n; i++) {
308 if (g_object_class_find_property (gobject_class,
309 g_param_spec_get_name (specs[i]))) {
310 /* We already inherited this property from a parent, skip */
313 g_object_class_install_property (gobject_class, i + j + 1,
314 param_spec_copy (specs[i]));
318 klass->n_prop_total = i + j;
321 g_type_class_unref (psink_class);
325 get_proxypad (GstPad * sinkpad)
327 GstIterator *iter = NULL;
328 GstPad *proxypad = NULL;
330 iter = gst_pad_iterate_internal_links (sinkpad);
332 if (gst_iterator_next (iter, (gpointer) & proxypad) != GST_ITERATOR_OK)
334 gst_iterator_free (iter);
341 post_missing_element_message (GstPulseAudioSink * pbin, const gchar * name)
345 msg = gst_missing_element_message_new (GST_ELEMENT_CAST (pbin), name);
346 gst_element_post_message (GST_ELEMENT_CAST (pbin), msg);
350 notify_cb (GObject * selector, GParamSpec * pspec, GstPulseAudioSink * pbin)
352 g_object_notify (G_OBJECT (pbin), g_param_spec_get_name (pspec));
356 gst_pulse_audio_sink_init (GstPulseAudioSink * pbin,
357 GstPulseAudioSinkClass * klass)
359 GstPadTemplate *template;
365 pbin->lock = g_mutex_new ();
367 gst_segment_init (&pbin->segment, GST_FORMAT_UNDEFINED);
369 pbin->psink = GST_PULSESINK (gst_element_factory_make ("pulsesink",
370 "pulseaudiosink-sink"));
371 g_assert (pbin->psink != NULL);
373 if (!gst_bin_add (GST_BIN (pbin), GST_ELEMENT (pbin->psink))) {
374 GST_ERROR_OBJECT (pbin, "Failed to add pulsesink to bin");
378 pad = gst_element_get_static_pad (GST_ELEMENT (pbin->psink), "sink");
379 template = gst_static_pad_template_get (&sink_template);
380 pbin->sinkpad = gst_ghost_pad_new_from_template ("sink", pad, template);
381 gst_object_unref (template);
383 pbin->sinkpad_old_eventfunc = GST_PAD_EVENTFUNC (pbin->sinkpad);
384 gst_pad_set_event_function (pbin->sinkpad,
385 GST_DEBUG_FUNCPTR (gst_pulse_audio_sink_sink_event));
386 gst_pad_set_setcaps_function (pbin->sinkpad,
387 GST_DEBUG_FUNCPTR (gst_pulse_audio_sink_sink_setcaps));
388 gst_pad_set_acceptcaps_function (pbin->sinkpad,
389 GST_DEBUG_FUNCPTR (gst_pulse_audio_sink_sink_acceptcaps));
391 gst_element_add_pad (GST_ELEMENT (pbin), pbin->sinkpad);
393 if (!(pbin->sink_proxypad = get_proxypad (pbin->sinkpad)))
394 GST_ERROR_OBJECT (pbin, "Failed to get proxypad of srcpad");
396 pbin->proxypad_old_eventfunc = GST_PAD_EVENTFUNC (pbin->sink_proxypad);
397 gst_pad_set_event_function (pbin->sink_proxypad,
398 GST_DEBUG_FUNCPTR (gst_pulse_audio_sink_src_event));
401 /* Now proxy all the notify::* signals */
402 specs = g_object_class_list_properties (G_OBJECT_CLASS (klass), &i);
403 prop = g_string_sized_new (30);
405 for (i--; i >= klass->n_prop_own; i--) {
406 g_string_printf (prop, "notify::%s", g_param_spec_get_name (specs[i]));
407 g_signal_connect (pbin->psink, prop->str, G_CALLBACK (notify_cb), pbin);
410 g_string_free (prop, TRUE);
413 pbin->format_lost = FALSE;
417 gst_object_unref (pad);
423 gst_object_unref (pbin->psink);
428 gst_pulse_audio_sink_set_property (GObject * object, guint prop_id,
429 const GValue * value, GParamSpec * pspec)
431 GstPulseAudioSink *pbin = GST_PULSE_AUDIO_SINK (object);
432 GstPulseAudioSinkClass *klass =
433 GST_PULSE_AUDIO_SINK_CLASS (G_OBJECT_GET_CLASS (object));
435 g_return_if_fail (prop_id <= klass->n_prop_total);
437 g_object_set_property (G_OBJECT (pbin->psink), g_param_spec_get_name (pspec),
442 gst_pulse_audio_sink_get_property (GObject * object, guint prop_id,
443 GValue * value, GParamSpec * pspec)
445 GstPulseAudioSink *pbin = GST_PULSE_AUDIO_SINK (object);
446 GstPulseAudioSinkClass *klass =
447 GST_PULSE_AUDIO_SINK_CLASS (G_OBJECT_GET_CLASS (object));
449 g_return_if_fail (prop_id <= klass->n_prop_total);
451 g_object_get_property (G_OBJECT (pbin->psink), g_param_spec_get_name (pspec),
456 gst_pulse_audio_sink_free_dbin2 (GstPulseAudioSink * pbin)
458 g_signal_handler_disconnect (pbin->dbin2, pbin->pad_added_id);
459 gst_element_set_state (pbin->dbin2, GST_STATE_NULL);
461 gst_bin_remove (GST_BIN (pbin), pbin->dbin2);
467 gst_pulse_audio_sink_dispose (GObject * object)
469 GstPulseAudioSink *pbin = GST_PULSE_AUDIO_SINK (object);
472 g_mutex_free (pbin->lock);
476 if (pbin->sink_proxypad) {
477 gst_object_unref (pbin->sink_proxypad);
478 pbin->sink_proxypad = NULL;
482 g_signal_handler_disconnect (pbin->dbin2, pbin->pad_added_id);
486 pbin->sinkpad = NULL;
489 G_OBJECT_CLASS (parent_class)->dispose (object);
493 gst_pulse_audio_sink_update_sinkpad (GstPulseAudioSink * pbin, GstPad * sinkpad)
497 ret = gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (pbin->sinkpad), sinkpad);
500 GST_WARNING_OBJECT (pbin, "Could not update ghostpad target");
506 distribute_running_time (GstElement * element, const GstSegment * segment)
511 pad = gst_element_get_static_pad (element, "sink");
513 /* FIXME: Some decoders collect newsegments and send them out at once, making
514 * them lose accumulator events (and thus making dbin2_event_probe() hard to
515 * do right if we're sending these as well. We can get away with not sending
516 * these at the moment, but this should be fixed! */
518 if (segment->accum) {
519 event = gst_event_new_new_segment_full (FALSE, segment->rate,
520 segment->applied_rate, segment->format, 0, segment->accum, 0);
521 gst_pad_send_event (pad, event);
525 event = gst_event_new_new_segment_full (FALSE, segment->rate,
526 segment->applied_rate, segment->format,
527 segment->start, segment->stop, segment->time);
528 gst_pad_send_event (pad, event);
530 gst_object_unref (pad);
534 dbin2_event_probe (GstPad * pad, GstMiniObject * obj, gpointer data)
536 GstPulseAudioSink *pbin = GST_PULSE_AUDIO_SINK (data);
537 GstEvent *event = GST_EVENT (obj);
539 if (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT) {
540 GST_DEBUG_OBJECT (pbin, "Got newsegment - dropping");
541 gst_pad_remove_event_probe (pad, pbin->event_probe_id);
549 pad_added_cb (GstElement * dbin2, GstPad * pad, gpointer * data)
551 GstPulseAudioSink *pbin;
552 GstPad *sinkpad = NULL;
554 pbin = GST_PULSE_AUDIO_SINK (data);
555 sinkpad = gst_element_get_static_pad (GST_ELEMENT (pbin->psink), "sink");
557 GST_PULSE_AUDIO_SINK_LOCK (pbin);
558 if (gst_pad_link (pad, sinkpad) != GST_PAD_LINK_OK)
559 GST_ERROR_OBJECT (pbin, "Failed to link decodebin2 to pulsesink");
561 GST_DEBUG_OBJECT (pbin, "Linked new pad to pulsesink");
562 GST_PULSE_AUDIO_SINK_UNLOCK (pbin);
564 gst_object_unref (sinkpad);
567 /* Called with pbin lock held */
569 gst_pulse_audio_sink_add_dbin2 (GstPulseAudioSink * pbin)
571 GstPad *sinkpad = NULL;
573 g_assert (pbin->dbin2 == NULL);
575 pbin->dbin2 = gst_element_factory_make ("decodebin2", "pulseaudiosink-dbin2");
578 post_missing_element_message (pbin, "decodebin2");
579 GST_ELEMENT_WARNING (pbin, CORE, MISSING_PLUGIN,
580 (_("Missing element '%s' - check your GStreamer installation."),
581 "decodebin2"), ("audio playback might fail"));
585 if (!gst_bin_add (GST_BIN (pbin), pbin->dbin2)) {
586 GST_ERROR_OBJECT (pbin, "Failed to add decodebin2 to bin");
590 pbin->pad_added_id = g_signal_connect (pbin->dbin2, "pad-added",
591 G_CALLBACK (pad_added_cb), pbin);
593 if (!gst_element_sync_state_with_parent (pbin->dbin2)) {
594 GST_ERROR_OBJECT (pbin, "Failed to set decodebin2 to parent state");
598 /* Trap the newsegment events that we feed the decodebin and discard them */
599 sinkpad = gst_element_get_static_pad (GST_ELEMENT (pbin->psink), "sink");
600 pbin->event_probe_id = gst_pad_add_event_probe_full (sinkpad,
601 G_CALLBACK (dbin2_event_probe), gst_object_ref (pbin),
602 (GDestroyNotify) gst_object_unref);
603 gst_object_unref (sinkpad);
606 GST_DEBUG_OBJECT (pbin, "Distributing running time to decodebin");
607 distribute_running_time (pbin->dbin2, &pbin->segment);
609 sinkpad = gst_element_get_static_pad (pbin->dbin2, "sink");
611 gst_pulse_audio_sink_update_sinkpad (pbin, sinkpad);
615 gst_object_unref (sinkpad);
619 update_eac3_alignment (GstPulseAudioSink * pbin)
621 GstCaps *caps = gst_pad_peer_get_caps_reffed (pbin->sinkpad);
627 st = gst_caps_get_structure (caps, 0);
629 if (g_str_equal (gst_structure_get_name (st), "audio/x-eac3")) {
630 GstStructure *event_st = gst_structure_new ("ac3parse-set-alignment",
631 "alignment", G_TYPE_STRING, pbin->dbin2 ? "frame" : "iec61937", NULL);
633 if (!gst_pad_push_event (pbin->sinkpad,
634 gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, event_st)))
635 GST_WARNING_OBJECT (pbin->sinkpad, "Could not update alignment");
638 gst_caps_unref (caps);
642 proxypad_blocked_cb (GstPad * pad, gboolean blocked, gpointer data)
644 GstPulseAudioSink *pbin = GST_PULSE_AUDIO_SINK (data);
646 GstPad *sinkpad = NULL;
649 /* Unblocked, don't need to do anything */
650 GST_DEBUG_OBJECT (pbin, "unblocked");
654 GST_DEBUG_OBJECT (pbin, "blocked");
656 GST_PULSE_AUDIO_SINK_LOCK (pbin);
658 if (!pbin->format_lost) {
659 sinkpad = gst_element_get_static_pad (GST_ELEMENT (pbin->psink), "sink");
661 if (GST_PAD_CAPS (pbin->sinkpad)) {
662 /* See if we already got caps on our sinkpad */
663 caps = gst_caps_ref (GST_PAD_CAPS (pbin->sinkpad));
665 /* We haven't, so get caps from upstream */
666 caps = gst_pad_get_caps_reffed (pad);
669 if (gst_pad_accept_caps (sinkpad, caps)) {
671 GST_DEBUG_OBJECT (pbin, "Removing decodebin");
672 gst_pulse_audio_sink_free_dbin2 (pbin);
673 gst_pulse_audio_sink_update_sinkpad (pbin, sinkpad);
675 GST_DEBUG_OBJECT (pbin, "Doing nothing");
677 gst_caps_unref (caps);
678 gst_object_unref (sinkpad);
681 /* pulsesink doesn't accept the incoming caps, so add a decodebin
682 * (potentially after removing the existing once, since decodebin2 can't
685 /* Format lost, proceed to try plugging a decodebin */
686 pbin->format_lost = FALSE;
689 if (pbin->dbin2 != NULL) {
690 /* decodebin2 doesn't support reconfiguration, so throw this one away and
691 * create a new one. */
692 gst_pulse_audio_sink_free_dbin2 (pbin);
695 GST_DEBUG_OBJECT (pbin, "Adding decodebin");
696 gst_pulse_audio_sink_add_dbin2 (pbin);
699 update_eac3_alignment (pbin);
701 gst_pad_set_blocked_async_full (pad, FALSE, proxypad_blocked_cb,
702 gst_object_ref (pbin), (GDestroyNotify) gst_object_unref);
704 GST_PULSE_AUDIO_SINK_UNLOCK (pbin);
708 gst_pulse_audio_sink_src_event (GstPad * pad, GstEvent * event)
710 GstPulseAudioSink *pbin = NULL;
711 GstPad *ghostpad = NULL;
712 gboolean ret = FALSE;
714 ghostpad = GST_PAD_CAST (gst_pad_get_parent (pad));
715 if (G_UNLIKELY (!ghostpad)) {
716 GST_WARNING_OBJECT (pad, "Could not get ghostpad");
720 pbin = GST_PULSE_AUDIO_SINK (gst_pad_get_parent (ghostpad));
721 if (G_UNLIKELY (!pbin)) {
722 GST_WARNING_OBJECT (pad, "Could not get pulseaudiosink");
726 if (G_UNLIKELY (GST_EVENT_TYPE (event) == GST_EVENT_CUSTOM_UPSTREAM) &&
727 (gst_event_has_name (event, "pulse-format-lost") ||
728 gst_event_has_name (event, "pulse-sink-changed"))) {
729 g_return_val_if_fail (pad->mode != GST_ACTIVATE_PULL, FALSE);
731 GST_PULSE_AUDIO_SINK_LOCK (pbin);
732 if (gst_event_has_name (event, "pulse-format-lost"))
733 pbin->format_lost = TRUE;
735 if (!gst_pad_is_blocked (pad))
736 gst_pad_set_blocked_async_full (pad, TRUE, proxypad_blocked_cb,
737 gst_object_ref (pbin), (GDestroyNotify) gst_object_unref);
738 GST_PULSE_AUDIO_SINK_UNLOCK (pbin);
741 } else if (pbin->proxypad_old_eventfunc) {
742 ret = pbin->proxypad_old_eventfunc (pad, event);
748 gst_object_unref (ghostpad);
750 gst_object_unref (pbin);
752 gst_event_unref (event);
758 gst_pulse_audio_sink_sink_event (GstPad * pad, GstEvent * event)
760 GstPulseAudioSink *pbin = GST_PULSE_AUDIO_SINK (gst_pad_get_parent (pad));
763 ret = pbin->sinkpad_old_eventfunc (pad, gst_event_ref (event));
765 switch (GST_EVENT_TYPE (event)) {
766 case GST_EVENT_NEWSEGMENT:
770 gint64 start, stop, time;
773 GST_PULSE_AUDIO_SINK_LOCK (pbin);
774 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
775 &start, &stop, &time);
777 GST_DEBUG_OBJECT (pbin,
778 "newsegment: update %d, rate %g, arate %g, start %" GST_TIME_FORMAT
779 ", stop %" GST_TIME_FORMAT ", time %" GST_TIME_FORMAT,
780 update, rate, arate, GST_TIME_ARGS (start), GST_TIME_ARGS (stop),
781 GST_TIME_ARGS (time));
783 if (format == GST_FORMAT_TIME) {
784 /* Store the values for feeding to sub-elements */
785 gst_segment_set_newsegment_full (&pbin->segment, update,
786 rate, arate, format, start, stop, time);
788 GST_WARNING_OBJECT (pbin, "Got a non-TIME format segment");
789 gst_segment_init (&pbin->segment, GST_FORMAT_TIME);
791 GST_PULSE_AUDIO_SINK_UNLOCK (pbin);
796 case GST_EVENT_FLUSH_STOP:
797 GST_PULSE_AUDIO_SINK_LOCK (pbin);
798 gst_segment_init (&pbin->segment, GST_FORMAT_UNDEFINED);
799 GST_PULSE_AUDIO_SINK_UNLOCK (pbin);
806 gst_object_unref (pbin);
807 gst_event_unref (event);
812 /* The bin's acceptcaps should be exactly equivalent to a pulsesink that is
813 * connected to a sink that supports all the formats in template caps. This
814 * means that upstream will have to have everything possibly upto a parser
815 * plugged and we plugin a decoder whenever required. */
817 gst_pulse_audio_sink_sink_acceptcaps (GstPad * pad, GstCaps * caps)
819 GstPulseAudioSink *pbin = GST_PULSE_AUDIO_SINK (gst_pad_get_parent (pad));
820 GstRingBufferSpec spec = { 0 };
821 const GstStructure *st;
822 GstCaps *pad_caps = NULL;
823 gboolean ret = FALSE;
825 pad_caps = gst_pad_get_caps_reffed (pad);
826 if (!pad_caps || !gst_caps_can_intersect (pad_caps, caps))
829 /* If we've not got fixed caps, creating a stream might fail, so let's just
830 * return from here with default acceptcaps behaviour */
831 if (!gst_caps_is_fixed (caps))
834 spec.latency_time = GST_BASE_AUDIO_SINK (pbin->psink)->latency_time;
835 if (!gst_ring_buffer_parse_caps (&spec, caps))
838 /* Make sure non-raw input is framed (one frame per buffer) and can be
840 st = gst_caps_get_structure (caps, 0);
842 if (!g_str_has_prefix (gst_structure_get_name (st), "audio/x-raw")) {
843 gboolean framed = FALSE, parsed = FALSE;
845 gst_structure_get_boolean (st, "framed", &framed);
846 gst_structure_get_boolean (st, "parsed", &parsed);
847 if ((!framed && !parsed) || gst_audio_iec61937_frame_size (&spec) <= 0)
855 gst_caps_unref (pad_caps);
857 gst_object_unref (pbin);
863 gst_pulse_audio_sink_sink_setcaps (GstPad * pad, GstCaps * caps)
865 GstPulseAudioSink *pbin = GST_PULSE_AUDIO_SINK (gst_pad_get_parent (pad));
868 GST_PULSE_AUDIO_SINK_LOCK (pbin);
870 if (!gst_pad_is_blocked (pbin->sinkpad))
871 gst_pad_set_blocked_async_full (pbin->sink_proxypad, TRUE,
872 proxypad_blocked_cb, gst_object_ref (pbin),
873 (GDestroyNotify) gst_object_unref);
875 GST_PULSE_AUDIO_SINK_UNLOCK (pbin);
877 gst_object_unref (pbin);
882 static GstStateChangeReturn
883 gst_pulse_audio_sink_change_state (GstElement * element,
884 GstStateChange transition)
886 GstPulseAudioSink *pbin = GST_PULSE_AUDIO_SINK (element);
887 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
889 /* Nothing to do for upward transitions */
890 switch (transition) {
891 case GST_STATE_CHANGE_PAUSED_TO_READY:
892 GST_PULSE_AUDIO_SINK_LOCK (pbin);
893 if (gst_pad_is_blocked (pbin->sinkpad)) {
894 gst_pad_set_blocked_async_full (pbin->sink_proxypad, FALSE,
895 proxypad_blocked_cb, gst_object_ref (pbin),
896 (GDestroyNotify) gst_object_unref);
898 GST_PULSE_AUDIO_SINK_UNLOCK (pbin);
905 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
906 if (ret != GST_STATE_CHANGE_SUCCESS) {
907 GST_DEBUG_OBJECT (pbin, "Base class returned %d on state change", ret);
911 switch (transition) {
912 case GST_STATE_CHANGE_PAUSED_TO_READY:
913 GST_PULSE_AUDIO_SINK_LOCK (pbin);
914 gst_segment_init (&pbin->segment, GST_FORMAT_UNDEFINED);
917 GstPad *pad = gst_element_get_static_pad (GST_ELEMENT (pbin->psink),
920 gst_pulse_audio_sink_free_dbin2 (pbin);
921 gst_pulse_audio_sink_update_sinkpad (pbin, pad);
923 gst_object_unref (pad);
926 GST_PULSE_AUDIO_SINK_UNLOCK (pbin);
938 #endif /* HAVE_PULSE_1_0 */