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>
57 #include <gst/audio/gstaudioiec61937.h>
58 #include "pulsesink.h"
60 GST_DEBUG_CATEGORY (pulseaudiosink_debug);
61 #define GST_CAT_DEFAULT (pulseaudiosink_debug)
63 #define GST_PULSE_AUDIO_SINK_LOCK(obj) G_STMT_START { \
64 GST_LOG_OBJECT (obj, \
65 "locking from thread %p", \
67 g_mutex_lock (GST_PULSE_AUDIO_SINK_CAST(obj)->lock); \
68 GST_LOG_OBJECT (obj, \
69 "locked from thread %p", \
73 #define GST_PULSE_AUDIO_SINK_UNLOCK(obj) G_STMT_START { \
74 GST_LOG_OBJECT (obj, \
75 "unlocking from thread %p", \
77 g_mutex_unlock (GST_PULSE_AUDIO_SINK_CAST(obj)->lock); \
86 GstPad *sink_proxypad;
87 GstPadEventFunction sinkpad_old_eventfunc;
88 GstPadEventFunction proxypad_old_eventfunc;
103 GstBinClass parent_class;
106 } GstPulseAudioSinkClass;
108 static void gst_pulse_audio_sink_get_property (GObject * object, guint prop_id,
109 GValue * value, GParamSpec * pspec);
110 static void gst_pulse_audio_sink_set_property (GObject * object, guint prop_id,
111 const GValue * value, GParamSpec * pspec);
112 static void gst_pulse_audio_sink_dispose (GObject * object);
113 static gboolean gst_pulse_audio_sink_src_event (GstPad * pad, GstEvent * event);
114 static gboolean gst_pulse_audio_sink_sink_event (GstPad * pad,
116 static gboolean gst_pulse_audio_sink_sink_acceptcaps (GstPad * pad,
118 static gboolean gst_pulse_audio_sink_sink_setcaps (GstPad * pad,
120 static GstStateChangeReturn
121 gst_pulse_audio_sink_change_state (GstElement * element,
122 GstStateChange transition);
125 gst_pulse_audio_sink_do_init (GType type)
127 GST_DEBUG_CATEGORY_INIT (pulseaudiosink_debug, "pulseaudiosink", 0,
128 "Bin that wraps pulsesink for handling compressed formats");
131 GST_BOILERPLATE_FULL (GstPulseAudioSink, gst_pulse_audio_sink, GstBin,
132 GST_TYPE_BIN, gst_pulse_audio_sink_do_init);
134 static GstStaticPadTemplate sink_template =
135 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
136 GST_STATIC_CAPS (PULSE_SINK_TEMPLATE_CAPS));
139 gst_pulse_audio_sink_base_init (gpointer klass)
141 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
143 gst_element_class_add_static_pad_template (element_class,
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)
364 pbin->lock = g_mutex_new ();
366 gst_segment_init (&pbin->segment, GST_FORMAT_UNDEFINED);
368 pbin->psink = GST_PULSESINK (gst_element_factory_make ("pulsesink",
369 "pulseaudiosink-sink"));
370 g_assert (pbin->psink != NULL);
372 if (!gst_bin_add (GST_BIN (pbin), GST_ELEMENT (pbin->psink))) {
373 GST_ERROR_OBJECT (pbin, "Failed to add pulsesink to bin");
377 pad = gst_element_get_static_pad (GST_ELEMENT (pbin->psink), "sink");
378 pbin->sinkpad = gst_ghost_pad_new_from_template ("sink", pad,
379 gst_static_pad_template_get (&sink_template));
381 pbin->sinkpad_old_eventfunc = GST_PAD_EVENTFUNC (pbin->sinkpad);
382 gst_pad_set_event_function (pbin->sinkpad,
383 GST_DEBUG_FUNCPTR (gst_pulse_audio_sink_sink_event));
384 gst_pad_set_setcaps_function (pbin->sinkpad,
385 GST_DEBUG_FUNCPTR (gst_pulse_audio_sink_sink_setcaps));
386 gst_pad_set_acceptcaps_function (pbin->sinkpad,
387 GST_DEBUG_FUNCPTR (gst_pulse_audio_sink_sink_acceptcaps));
389 gst_element_add_pad (GST_ELEMENT (pbin), pbin->sinkpad);
391 if (!(pbin->sink_proxypad = get_proxypad (pbin->sinkpad)))
392 GST_ERROR_OBJECT (pbin, "Failed to get proxypad of srcpad");
394 pbin->proxypad_old_eventfunc = GST_PAD_EVENTFUNC (pbin->sink_proxypad);
395 gst_pad_set_event_function (pbin->sink_proxypad,
396 GST_DEBUG_FUNCPTR (gst_pulse_audio_sink_src_event));
399 /* Now proxy all the notify::* signals */
400 specs = g_object_class_list_properties (G_OBJECT_CLASS (klass), &i);
401 prop = g_string_sized_new (30);
403 for (i--; i >= klass->n_prop_own; i--) {
404 g_string_printf (prop, "notify::%s", g_param_spec_get_name (specs[i]));
405 g_signal_connect (pbin->psink, prop->str, G_CALLBACK (notify_cb), pbin);
408 g_string_free (prop, TRUE);
411 pbin->format_lost = FALSE;
415 gst_object_unref (pad);
421 gst_object_unref (pbin->psink);
426 gst_pulse_audio_sink_set_property (GObject * object, guint prop_id,
427 const GValue * value, GParamSpec * pspec)
429 GstPulseAudioSink *pbin = GST_PULSE_AUDIO_SINK (object);
430 GstPulseAudioSinkClass *klass =
431 GST_PULSE_AUDIO_SINK_CLASS (G_OBJECT_GET_CLASS (object));
433 g_return_if_fail (prop_id <= klass->n_prop_total);
435 g_object_set_property (G_OBJECT (pbin->psink), g_param_spec_get_name (pspec),
440 gst_pulse_audio_sink_get_property (GObject * object, guint prop_id,
441 GValue * value, GParamSpec * pspec)
443 GstPulseAudioSink *pbin = GST_PULSE_AUDIO_SINK (object);
444 GstPulseAudioSinkClass *klass =
445 GST_PULSE_AUDIO_SINK_CLASS (G_OBJECT_GET_CLASS (object));
447 g_return_if_fail (prop_id <= klass->n_prop_total);
449 g_object_get_property (G_OBJECT (pbin->psink), g_param_spec_get_name (pspec),
454 gst_pulse_audio_sink_free_dbin2 (GstPulseAudioSink * pbin)
456 g_signal_handler_disconnect (pbin->dbin2, pbin->pad_added_id);
457 gst_element_set_state (pbin->dbin2, GST_STATE_NULL);
459 gst_bin_remove (GST_BIN (pbin), pbin->dbin2);
465 gst_pulse_audio_sink_dispose (GObject * object)
467 GstPulseAudioSink *pbin = GST_PULSE_AUDIO_SINK (object);
470 g_mutex_free (pbin->lock);
474 if (pbin->sink_proxypad) {
475 gst_object_unref (pbin->sink_proxypad);
476 pbin->sink_proxypad = NULL;
480 g_signal_handler_disconnect (pbin->dbin2, pbin->pad_added_id);
484 pbin->sinkpad = NULL;
487 G_OBJECT_CLASS (parent_class)->dispose (object);
491 gst_pulse_audio_sink_update_sinkpad (GstPulseAudioSink * pbin, GstPad * sinkpad)
495 ret = gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (pbin->sinkpad), sinkpad);
498 GST_WARNING_OBJECT (pbin, "Could not update ghostpad target");
504 distribute_running_time (GstElement * element, const GstSegment * segment)
509 pad = gst_element_get_static_pad (element, "sink");
511 /* FIXME: Some decoders collect newsegments and send them out at once, making
512 * them lose accumulator events (and thus making dbin2_event_probe() hard to
513 * do right if we're sending these as well. We can get away with not sending
514 * these at the moment, but this should be fixed! */
516 if (segment->accum) {
517 event = gst_event_new_new_segment_full (FALSE, segment->rate,
518 segment->applied_rate, segment->format, 0, segment->accum, 0);
519 gst_pad_send_event (pad, event);
523 event = gst_event_new_new_segment_full (FALSE, segment->rate,
524 segment->applied_rate, segment->format,
525 segment->start, segment->stop, segment->time);
526 gst_pad_send_event (pad, event);
528 gst_object_unref (pad);
532 dbin2_event_probe (GstPad * pad, GstMiniObject * obj, gpointer data)
534 GstPulseAudioSink *pbin = GST_PULSE_AUDIO_SINK (data);
535 GstEvent *event = GST_EVENT (obj);
537 if (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT) {
538 GST_DEBUG_OBJECT (pbin, "Got newsegment - dropping");
539 gst_pad_remove_event_probe (pad, pbin->event_probe_id);
547 pad_added_cb (GstElement * dbin2, GstPad * pad, gpointer * data)
549 GstPulseAudioSink *pbin;
550 GstPad *sinkpad = NULL;
552 pbin = GST_PULSE_AUDIO_SINK (data);
553 sinkpad = gst_element_get_static_pad (GST_ELEMENT (pbin->psink), "sink");
555 GST_PULSE_AUDIO_SINK_LOCK (pbin);
556 if (gst_pad_link (pad, sinkpad) != GST_PAD_LINK_OK)
557 GST_ERROR_OBJECT (pbin, "Failed to link decodebin2 to pulsesink");
559 GST_DEBUG_OBJECT (pbin, "Linked new pad to pulsesink");
560 GST_PULSE_AUDIO_SINK_UNLOCK (pbin);
562 gst_object_unref (sinkpad);
565 /* Called with pbin lock held */
567 gst_pulse_audio_sink_add_dbin2 (GstPulseAudioSink * pbin)
569 GstPad *sinkpad = NULL;
571 g_assert (pbin->dbin2 == NULL);
573 pbin->dbin2 = gst_element_factory_make ("decodebin2", "pulseaudiosink-dbin2");
576 post_missing_element_message (pbin, "decodebin2");
577 GST_ELEMENT_WARNING (pbin, CORE, MISSING_PLUGIN,
578 (_("Missing element '%s' - check your GStreamer installation."),
579 "decodebin2"), ("audio playback might fail"));
583 if (!gst_bin_add (GST_BIN (pbin), pbin->dbin2)) {
584 GST_ERROR_OBJECT (pbin, "Failed to add decodebin2 to bin");
588 pbin->pad_added_id = g_signal_connect (pbin->dbin2, "pad-added",
589 G_CALLBACK (pad_added_cb), pbin);
591 if (!gst_element_sync_state_with_parent (pbin->dbin2)) {
592 GST_ERROR_OBJECT (pbin, "Failed to set decodebin2 to parent state");
596 /* Trap the newsegment events that we feed the decodebin and discard them */
597 sinkpad = gst_element_get_static_pad (GST_ELEMENT (pbin->psink), "sink");
598 pbin->event_probe_id = gst_pad_add_event_probe_full (sinkpad,
599 G_CALLBACK (dbin2_event_probe), gst_object_ref (pbin),
600 (GDestroyNotify) gst_object_unref);
601 gst_object_unref (sinkpad);
604 GST_DEBUG_OBJECT (pbin, "Distributing running time to decodebin");
605 distribute_running_time (pbin->dbin2, &pbin->segment);
607 sinkpad = gst_element_get_static_pad (pbin->dbin2, "sink");
609 gst_pulse_audio_sink_update_sinkpad (pbin, sinkpad);
613 gst_object_unref (sinkpad);
617 update_eac3_alignment (GstPulseAudioSink * pbin)
619 GstCaps *caps = gst_pad_peer_get_caps_reffed (pbin->sinkpad);
625 st = gst_caps_get_structure (caps, 0);
627 if (g_str_equal (gst_structure_get_name (st), "audio/x-eac3")) {
628 GstStructure *event_st = gst_structure_new ("ac3parse-set-alignment",
629 "alignment", G_TYPE_STRING, pbin->dbin2 ? "frame" : "iec61937", NULL);
631 if (!gst_pad_push_event (pbin->sinkpad,
632 gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, event_st)))
633 GST_WARNING_OBJECT (pbin->sinkpad, "Could not update alignment");
636 gst_caps_unref (caps);
640 proxypad_blocked_cb (GstPad * pad, gboolean blocked, gpointer data)
642 GstPulseAudioSink *pbin = GST_PULSE_AUDIO_SINK (data);
644 GstPad *sinkpad = NULL;
647 /* Unblocked, don't need to do anything */
648 GST_DEBUG_OBJECT (pbin, "unblocked");
652 GST_DEBUG_OBJECT (pbin, "blocked");
654 GST_PULSE_AUDIO_SINK_LOCK (pbin);
656 if (!pbin->format_lost) {
657 sinkpad = gst_element_get_static_pad (GST_ELEMENT (pbin->psink), "sink");
659 if (GST_PAD_CAPS (pbin->sinkpad)) {
660 /* See if we already got caps on our sinkpad */
661 caps = gst_caps_ref (GST_PAD_CAPS (pbin->sinkpad));
663 /* We haven't, so get caps from upstream */
664 caps = gst_pad_get_caps_reffed (pad);
667 if (gst_pad_accept_caps (sinkpad, caps)) {
669 GST_DEBUG_OBJECT (pbin, "Removing decodebin");
670 gst_pulse_audio_sink_free_dbin2 (pbin);
671 gst_pulse_audio_sink_update_sinkpad (pbin, sinkpad);
673 GST_DEBUG_OBJECT (pbin, "Doing nothing");
675 gst_caps_unref (caps);
676 gst_object_unref (sinkpad);
679 /* pulsesink doesn't accept the incoming caps, so add a decodebin
680 * (potentially after removing the existing once, since decodebin2 can't
683 /* Format lost, proceed to try plugging a decodebin */
684 pbin->format_lost = FALSE;
687 if (pbin->dbin2 != NULL) {
688 /* decodebin2 doesn't support reconfiguration, so throw this one away and
689 * create a new one. */
690 gst_pulse_audio_sink_free_dbin2 (pbin);
693 GST_DEBUG_OBJECT (pbin, "Adding decodebin");
694 gst_pulse_audio_sink_add_dbin2 (pbin);
697 update_eac3_alignment (pbin);
699 gst_pad_set_blocked_async_full (pad, FALSE, proxypad_blocked_cb,
700 gst_object_ref (pbin), (GDestroyNotify) gst_object_unref);
702 GST_PULSE_AUDIO_SINK_UNLOCK (pbin);
706 gst_pulse_audio_sink_src_event (GstPad * pad, GstEvent * event)
708 GstPulseAudioSink *pbin = NULL;
709 GstPad *ghostpad = NULL;
710 gboolean ret = FALSE;
712 ghostpad = GST_PAD_CAST (gst_pad_get_parent (pad));
713 if (G_UNLIKELY (!ghostpad)) {
714 GST_WARNING_OBJECT (pad, "Could not get ghostpad");
718 pbin = GST_PULSE_AUDIO_SINK (gst_pad_get_parent (ghostpad));
719 if (G_UNLIKELY (!pbin)) {
720 GST_WARNING_OBJECT (pad, "Could not get pulseaudiosink");
724 if (G_UNLIKELY (GST_EVENT_TYPE (event) == GST_EVENT_CUSTOM_UPSTREAM) &&
725 (gst_event_has_name (event, "pulse-format-lost") ||
726 gst_event_has_name (event, "pulse-sink-changed"))) {
727 g_return_val_if_fail (pad->mode != GST_ACTIVATE_PULL, FALSE);
729 GST_PULSE_AUDIO_SINK_LOCK (pbin);
730 if (gst_event_has_name (event, "pulse-format-lost"))
731 pbin->format_lost = TRUE;
733 if (!gst_pad_is_blocked (pad))
734 gst_pad_set_blocked_async_full (pad, TRUE, proxypad_blocked_cb,
735 gst_object_ref (pbin), (GDestroyNotify) gst_object_unref);
736 GST_PULSE_AUDIO_SINK_UNLOCK (pbin);
739 } else if (pbin->proxypad_old_eventfunc) {
740 ret = pbin->proxypad_old_eventfunc (pad, event);
746 gst_object_unref (ghostpad);
748 gst_object_unref (pbin);
750 gst_event_unref (event);
756 gst_pulse_audio_sink_sink_event (GstPad * pad, GstEvent * event)
758 GstPulseAudioSink *pbin = GST_PULSE_AUDIO_SINK (gst_pad_get_parent (pad));
761 ret = pbin->sinkpad_old_eventfunc (pad, gst_event_ref (event));
763 switch (GST_EVENT_TYPE (event)) {
764 case GST_EVENT_NEWSEGMENT:
768 gint64 start, stop, time;
771 GST_PULSE_AUDIO_SINK_LOCK (pbin);
772 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
773 &start, &stop, &time);
775 GST_DEBUG_OBJECT (pbin,
776 "newsegment: update %d, rate %g, arate %g, start %" GST_TIME_FORMAT
777 ", stop %" GST_TIME_FORMAT ", time %" GST_TIME_FORMAT,
778 update, rate, arate, GST_TIME_ARGS (start), GST_TIME_ARGS (stop),
779 GST_TIME_ARGS (time));
781 if (format == GST_FORMAT_TIME) {
782 /* Store the values for feeding to sub-elements */
783 gst_segment_set_newsegment_full (&pbin->segment, update,
784 rate, arate, format, start, stop, time);
786 GST_WARNING_OBJECT (pbin, "Got a non-TIME format segment");
787 gst_segment_init (&pbin->segment, GST_FORMAT_TIME);
789 GST_PULSE_AUDIO_SINK_UNLOCK (pbin);
794 case GST_EVENT_FLUSH_STOP:
795 GST_PULSE_AUDIO_SINK_LOCK (pbin);
796 gst_segment_init (&pbin->segment, GST_FORMAT_UNDEFINED);
797 GST_PULSE_AUDIO_SINK_UNLOCK (pbin);
804 gst_object_unref (pbin);
805 gst_event_unref (event);
810 /* The bin's acceptcaps should be exactly equivalent to a pulsesink that is
811 * connected to a sink that supports all the formats in template caps. This
812 * means that upstream will have to have everything possibly upto a parser
813 * plugged and we plugin a decoder whenever required. */
815 gst_pulse_audio_sink_sink_acceptcaps (GstPad * pad, GstCaps * caps)
817 GstPulseAudioSink *pbin = GST_PULSE_AUDIO_SINK (gst_pad_get_parent (pad));
818 GstRingBufferSpec spec = { 0 };
819 const GstStructure *st;
820 GstCaps *pad_caps = NULL;
821 gboolean ret = FALSE;
823 pad_caps = gst_pad_get_caps_reffed (pad);
824 if (!pad_caps || !gst_caps_can_intersect (pad_caps, caps))
827 /* If we've not got fixed caps, creating a stream might fail, so let's just
828 * return from here with default acceptcaps behaviour */
829 if (!gst_caps_is_fixed (caps))
832 spec.latency_time = GST_BASE_AUDIO_SINK (pbin->psink)->latency_time;
833 if (!gst_ring_buffer_parse_caps (&spec, caps))
836 /* Make sure non-raw input is framed (one frame per buffer) and can be
838 st = gst_caps_get_structure (caps, 0);
840 if (!g_str_has_prefix (gst_structure_get_name (st), "audio/x-raw")) {
841 gboolean framed = FALSE, parsed = FALSE;
843 gst_structure_get_boolean (st, "framed", &framed);
844 gst_structure_get_boolean (st, "parsed", &parsed);
845 if ((!framed && !parsed) || gst_audio_iec61937_frame_size (&spec) <= 0)
853 gst_caps_unref (pad_caps);
855 gst_object_unref (pbin);
861 gst_pulse_audio_sink_sink_setcaps (GstPad * pad, GstCaps * caps)
863 GstPulseAudioSink *pbin = GST_PULSE_AUDIO_SINK (gst_pad_get_parent (pad));
866 GST_PULSE_AUDIO_SINK_LOCK (pbin);
868 if (!gst_pad_is_blocked (pbin->sinkpad))
869 gst_pad_set_blocked_async_full (pbin->sink_proxypad, TRUE,
870 proxypad_blocked_cb, gst_object_ref (pbin),
871 (GDestroyNotify) gst_object_unref);
873 GST_PULSE_AUDIO_SINK_UNLOCK (pbin);
875 gst_caps_unref (caps);
876 gst_object_unref (pbin);
881 static GstStateChangeReturn
882 gst_pulse_audio_sink_change_state (GstElement * element,
883 GstStateChange transition)
885 GstPulseAudioSink *pbin = GST_PULSE_AUDIO_SINK (element);
886 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
888 /* Nothing to do for upward transitions */
889 switch (transition) {
890 case GST_STATE_CHANGE_PAUSED_TO_READY:
891 GST_PULSE_AUDIO_SINK_LOCK (pbin);
892 if (gst_pad_is_blocked (pbin->sinkpad)) {
893 gst_pad_set_blocked_async_full (pbin->sink_proxypad, FALSE,
894 proxypad_blocked_cb, gst_object_ref (pbin),
895 (GDestroyNotify) gst_object_unref);
897 GST_PULSE_AUDIO_SINK_UNLOCK (pbin);
904 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
905 if (ret != GST_STATE_CHANGE_SUCCESS) {
906 GST_DEBUG_OBJECT (pbin, "Base class returned %d on state change", ret);
910 switch (transition) {
911 case GST_STATE_CHANGE_PAUSED_TO_READY:
912 GST_PULSE_AUDIO_SINK_LOCK (pbin);
913 gst_segment_init (&pbin->segment, GST_FORMAT_UNDEFINED);
916 GstPad *pad = gst_element_get_static_pad (GST_ELEMENT (pbin->psink),
919 gst_pulse_audio_sink_free_dbin2 (pbin);
920 gst_pulse_audio_sink_update_sinkpad (pbin, pad);
922 gst_object_unref (pad);
925 GST_PULSE_AUDIO_SINK_UNLOCK (pbin);
937 #endif /* HAVE_PULSE_1_0 */