1 /*-*- Mode: C; c-basic-offset: 2 -*-*/
3 /* GStreamer pulseaudio plugin
5 * Copyright (c) 2004-2008 Lennart Poettering
8 * gst-pulse is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation; either version 2.1 of the
11 * License, or (at your option) any later version.
13 * gst-pulse is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with gst-pulse; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
25 * SECTION:element-pulsesink
28 * This element outputs audio to a
29 * <ulink href="http://www.pulseaudio.org">PulseAudio sound server</ulink>.
32 * <title>Example pipelines</title>
34 * gst-launch-1.0 -v filesrc location=sine.ogg ! oggdemux ! vorbisdec ! audioconvert ! audioresample ! pulsesink
35 * ]| Play an Ogg/Vorbis file.
37 * gst-launch-1.0 -v audiotestsrc ! audioconvert ! volume volume=0.4 ! pulsesink
38 * ]| Play a 440Hz sine wave.
40 * gst-launch-1.0 -v audiotestsrc ! pulsesink stream-properties="props,media.title=test"
41 * ]| Play a sine wave and set a stream property. The property can be checked
53 #include <gst/base/gstbasesink.h>
54 #include <gst/gsttaglist.h>
55 #include <gst/audio/audio.h>
56 #include <gst/gst-i18n-plugin.h>
58 #include <gst/pbutils/pbutils.h> /* only used for GST_PLUGINS_BASE_VERSION_* */
60 #include <gst/glib-compat-private.h>
62 #include "pulsesink.h"
63 #include "pulseutil.h"
65 GST_DEBUG_CATEGORY_EXTERN (pulse_debug);
66 #define GST_CAT_DEFAULT pulse_debug
68 #define DEFAULT_SERVER NULL
69 #define DEFAULT_DEVICE NULL
70 #define DEFAULT_DEVICE_NAME NULL
71 #define DEFAULT_VOLUME 1.0
72 #define DEFAULT_MUTE FALSE
73 #define MAX_VOLUME 10.0
84 PROP_STREAM_PROPERTIES,
88 #define GST_TYPE_PULSERING_BUFFER \
89 (gst_pulseringbuffer_get_type())
90 #define GST_PULSERING_BUFFER(obj) \
91 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_PULSERING_BUFFER,GstPulseRingBuffer))
92 #define GST_PULSERING_BUFFER_CLASS(klass) \
93 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_PULSERING_BUFFER,GstPulseRingBufferClass))
94 #define GST_PULSERING_BUFFER_GET_CLASS(obj) \
95 (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_PULSERING_BUFFER, GstPulseRingBufferClass))
96 #define GST_PULSERING_BUFFER_CAST(obj) \
97 ((GstPulseRingBuffer *)obj)
98 #define GST_IS_PULSERING_BUFFER(obj) \
99 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_PULSERING_BUFFER))
100 #define GST_IS_PULSERING_BUFFER_CLASS(klass)\
101 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_PULSERING_BUFFER))
103 typedef struct _GstPulseRingBuffer GstPulseRingBuffer;
104 typedef struct _GstPulseRingBufferClass GstPulseRingBufferClass;
106 typedef struct _GstPulseContext GstPulseContext;
108 /* Store the PA contexts in a hash table to allow easy sharing among
109 * multiple instances of the sink. Keys are $context_name@$server_name
110 * (strings) and values should be GstPulseContext pointers.
112 struct _GstPulseContext
115 GSList *ring_buffers;
118 static GHashTable *gst_pulse_shared_contexts = NULL;
120 /* use one static main-loop for all instances
121 * this is needed to make the context sharing work as the contexts are
122 * released when releasing their parent main-loop
124 static pa_threaded_mainloop *mainloop = NULL;
125 static guint mainloop_ref_ct = 0;
127 /* lock for access to shared resources */
128 static GMutex pa_shared_resource_mutex;
130 /* We keep a custom ringbuffer that is backed up by data allocated by
131 * pulseaudio. We must also overide the commit function to write into
132 * pulseaudio memory instead. */
133 struct _GstPulseRingBuffer
135 GstAudioRingBuffer object;
142 pa_stream *probe_stream;
144 pa_format_info *format;
155 gboolean in_commit:1;
158 struct _GstPulseRingBufferClass
160 GstAudioRingBufferClass parent_class;
163 static GType gst_pulseringbuffer_get_type (void);
164 static void gst_pulseringbuffer_finalize (GObject * object);
166 static GstAudioRingBufferClass *ring_parent_class = NULL;
168 static gboolean gst_pulseringbuffer_open_device (GstAudioRingBuffer * buf);
169 static gboolean gst_pulseringbuffer_close_device (GstAudioRingBuffer * buf);
170 static gboolean gst_pulseringbuffer_acquire (GstAudioRingBuffer * buf,
171 GstAudioRingBufferSpec * spec);
172 static gboolean gst_pulseringbuffer_release (GstAudioRingBuffer * buf);
173 static gboolean gst_pulseringbuffer_start (GstAudioRingBuffer * buf);
174 static gboolean gst_pulseringbuffer_pause (GstAudioRingBuffer * buf);
175 static gboolean gst_pulseringbuffer_stop (GstAudioRingBuffer * buf);
176 static void gst_pulseringbuffer_clear (GstAudioRingBuffer * buf);
177 static guint gst_pulseringbuffer_commit (GstAudioRingBuffer * buf,
178 guint64 * sample, guchar * data, gint in_samples, gint out_samples,
181 G_DEFINE_TYPE (GstPulseRingBuffer, gst_pulseringbuffer,
182 GST_TYPE_AUDIO_RING_BUFFER);
185 gst_pulsesink_init_contexts (void)
187 g_mutex_init (&pa_shared_resource_mutex);
188 gst_pulse_shared_contexts = g_hash_table_new_full (g_str_hash, g_str_equal,
193 gst_pulseringbuffer_class_init (GstPulseRingBufferClass * klass)
195 GObjectClass *gobject_class;
196 GstAudioRingBufferClass *gstringbuffer_class;
198 gobject_class = (GObjectClass *) klass;
199 gstringbuffer_class = (GstAudioRingBufferClass *) klass;
201 ring_parent_class = g_type_class_peek_parent (klass);
203 gobject_class->finalize = gst_pulseringbuffer_finalize;
205 gstringbuffer_class->open_device =
206 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_open_device);
207 gstringbuffer_class->close_device =
208 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_close_device);
209 gstringbuffer_class->acquire =
210 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_acquire);
211 gstringbuffer_class->release =
212 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_release);
213 gstringbuffer_class->start = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_start);
214 gstringbuffer_class->pause = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_pause);
215 gstringbuffer_class->resume = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_start);
216 gstringbuffer_class->stop = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_stop);
217 gstringbuffer_class->clear_all =
218 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_clear);
220 gstringbuffer_class->commit = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_commit);
224 gst_pulseringbuffer_init (GstPulseRingBuffer * pbuf)
226 pbuf->stream_name = NULL;
227 pbuf->context = NULL;
229 pbuf->probe_stream = NULL;
233 pbuf->is_pcm = FALSE;
237 pbuf->m_writable = 0;
239 pbuf->m_lastoffset = 0;
242 pbuf->in_commit = FALSE;
243 pbuf->paused = FALSE;
246 /* Call with mainloop lock held if wait == TRUE) */
248 gst_pulse_destroy_stream (pa_stream * stream, gboolean wait)
250 /* Make sure we don't get any further callbacks */
251 pa_stream_set_write_callback (stream, NULL, NULL);
252 pa_stream_set_underflow_callback (stream, NULL, NULL);
253 pa_stream_set_overflow_callback (stream, NULL, NULL);
255 pa_stream_disconnect (stream);
258 pa_threaded_mainloop_wait (mainloop);
260 pa_stream_set_state_callback (stream, NULL, NULL);
261 pa_stream_unref (stream);
265 gst_pulsering_destroy_stream (GstPulseRingBuffer * pbuf)
267 if (pbuf->probe_stream) {
268 gst_pulse_destroy_stream (pbuf->probe_stream, FALSE);
269 pbuf->probe_stream = NULL;
275 /* drop shm memory buffer */
276 pa_stream_cancel_write (pbuf->stream);
278 /* reset internal variables */
281 pbuf->m_writable = 0;
283 pbuf->m_lastoffset = 0;
286 pa_format_info_free (pbuf->format);
289 pbuf->is_pcm = FALSE;
292 pa_stream_disconnect (pbuf->stream);
294 /* Make sure we don't get any further callbacks */
295 pa_stream_set_state_callback (pbuf->stream, NULL, NULL);
296 pa_stream_set_write_callback (pbuf->stream, NULL, NULL);
297 pa_stream_set_underflow_callback (pbuf->stream, NULL, NULL);
298 pa_stream_set_overflow_callback (pbuf->stream, NULL, NULL);
300 pa_stream_unref (pbuf->stream);
304 g_free (pbuf->stream_name);
305 pbuf->stream_name = NULL;
309 gst_pulsering_destroy_context (GstPulseRingBuffer * pbuf)
311 g_mutex_lock (&pa_shared_resource_mutex);
313 GST_DEBUG_OBJECT (pbuf, "destroying ringbuffer %p", pbuf);
315 gst_pulsering_destroy_stream (pbuf);
318 pa_context_unref (pbuf->context);
319 pbuf->context = NULL;
322 if (pbuf->context_name) {
323 GstPulseContext *pctx;
325 pctx = g_hash_table_lookup (gst_pulse_shared_contexts, pbuf->context_name);
327 GST_DEBUG_OBJECT (pbuf, "releasing context with name %s, pbuf=%p, pctx=%p",
328 pbuf->context_name, pbuf, pctx);
331 pctx->ring_buffers = g_slist_remove (pctx->ring_buffers, pbuf);
332 if (pctx->ring_buffers == NULL) {
333 GST_DEBUG_OBJECT (pbuf,
334 "destroying final context with name %s, pbuf=%p, pctx=%p",
335 pbuf->context_name, pbuf, pctx);
337 pa_context_disconnect (pctx->context);
339 /* Make sure we don't get any further callbacks */
340 pa_context_set_state_callback (pctx->context, NULL, NULL);
341 pa_context_set_subscribe_callback (pctx->context, NULL, NULL);
343 g_hash_table_remove (gst_pulse_shared_contexts, pbuf->context_name);
345 pa_context_unref (pctx->context);
346 g_slice_free (GstPulseContext, pctx);
349 g_free (pbuf->context_name);
350 pbuf->context_name = NULL;
352 g_mutex_unlock (&pa_shared_resource_mutex);
356 gst_pulseringbuffer_finalize (GObject * object)
358 GstPulseRingBuffer *ringbuffer;
360 ringbuffer = GST_PULSERING_BUFFER_CAST (object);
362 gst_pulsering_destroy_context (ringbuffer);
363 G_OBJECT_CLASS (ring_parent_class)->finalize (object);
367 #define CONTEXT_OK(c) ((c) && PA_CONTEXT_IS_GOOD (pa_context_get_state ((c))))
368 #define STREAM_OK(s) ((s) && PA_STREAM_IS_GOOD (pa_stream_get_state ((s))))
371 gst_pulsering_is_dead (GstPulseSink * psink, GstPulseRingBuffer * pbuf,
372 gboolean check_stream)
374 if (!CONTEXT_OK (pbuf->context))
377 if (check_stream && !STREAM_OK (pbuf->stream))
384 const gchar *err_str =
385 pbuf->context ? pa_strerror (pa_context_errno (pbuf->context)) : NULL;
386 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED, ("Disconnected: %s",
393 gst_pulsering_context_state_cb (pa_context * c, void *userdata)
395 pa_context_state_t state;
396 pa_threaded_mainloop *mainloop = (pa_threaded_mainloop *) userdata;
398 state = pa_context_get_state (c);
400 GST_LOG ("got new context state %d", state);
403 case PA_CONTEXT_READY:
404 case PA_CONTEXT_TERMINATED:
405 case PA_CONTEXT_FAILED:
406 GST_LOG ("signaling");
407 pa_threaded_mainloop_signal (mainloop, 0);
410 case PA_CONTEXT_UNCONNECTED:
411 case PA_CONTEXT_CONNECTING:
412 case PA_CONTEXT_AUTHORIZING:
413 case PA_CONTEXT_SETTING_NAME:
419 gst_pulsering_context_subscribe_cb (pa_context * c,
420 pa_subscription_event_type_t t, uint32_t idx, void *userdata)
423 GstPulseContext *pctx = (GstPulseContext *) userdata;
426 if (t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT | PA_SUBSCRIPTION_EVENT_CHANGE) &&
427 t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT | PA_SUBSCRIPTION_EVENT_NEW))
430 for (walk = pctx->ring_buffers; walk; walk = g_slist_next (walk)) {
431 GstPulseRingBuffer *pbuf = (GstPulseRingBuffer *) walk->data;
432 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
434 GST_LOG_OBJECT (psink, "type %04x, idx %u", t, idx);
439 if (idx != pa_stream_get_index (pbuf->stream))
442 if (psink->device && pbuf->is_pcm &&
443 !g_str_equal (psink->device,
444 pa_stream_get_device_name (pbuf->stream))) {
445 /* Underlying sink changed. And this is not a passthrough stream. Let's
446 * see if someone upstream wants to try to renegotiate. */
449 g_free (psink->device);
450 psink->device = g_strdup (pa_stream_get_device_name (pbuf->stream));
452 GST_INFO_OBJECT (psink, "emitting sink-changed");
454 /* FIXME: send reconfigure event instead and let decodebin/playbin
455 * handle that. Also take care of ac3 alignment. See "pulse-format-lost" */
456 renego = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
457 gst_structure_new_empty ("pulse-sink-changed"));
459 if (!gst_pad_push_event (GST_BASE_SINK (psink)->sinkpad, renego))
460 GST_DEBUG_OBJECT (psink, "Emitted sink-changed - nobody was listening");
463 /* Actually this event is also triggered when other properties of
464 * the stream change that are unrelated to the volume. However it is
465 * probably cheaper to signal the change here and check for the
466 * volume when the GObject property is read instead of querying it always. */
468 /* inform streaming thread to notify */
469 g_atomic_int_compare_and_exchange (&psink->notify, 0, 1);
473 /* will be called when the device should be opened. In this case we will connect
474 * to the server. We should not try to open any streams in this state. */
476 gst_pulseringbuffer_open_device (GstAudioRingBuffer * buf)
479 GstPulseRingBuffer *pbuf;
480 GstPulseContext *pctx;
481 pa_mainloop_api *api;
482 gboolean need_unlock_shared;
484 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
485 pbuf = GST_PULSERING_BUFFER_CAST (buf);
487 g_assert (!pbuf->stream);
488 g_assert (psink->client_name);
491 pbuf->context_name = g_strdup_printf ("%s@%s", psink->client_name,
494 pbuf->context_name = g_strdup (psink->client_name);
496 pa_threaded_mainloop_lock (mainloop);
498 g_mutex_lock (&pa_shared_resource_mutex);
499 need_unlock_shared = TRUE;
501 pctx = g_hash_table_lookup (gst_pulse_shared_contexts, pbuf->context_name);
503 pctx = g_slice_new0 (GstPulseContext);
505 /* get the mainloop api and create a context */
506 GST_INFO_OBJECT (psink, "new context with name %s, pbuf=%p, pctx=%p",
507 pbuf->context_name, pbuf, pctx);
508 api = pa_threaded_mainloop_get_api (mainloop);
509 if (!(pctx->context = pa_context_new (api, pbuf->context_name)))
512 pctx->ring_buffers = g_slist_prepend (pctx->ring_buffers, pbuf);
513 g_hash_table_insert (gst_pulse_shared_contexts,
514 g_strdup (pbuf->context_name), (gpointer) pctx);
515 /* register some essential callbacks */
516 pa_context_set_state_callback (pctx->context,
517 gst_pulsering_context_state_cb, mainloop);
518 pa_context_set_subscribe_callback (pctx->context,
519 gst_pulsering_context_subscribe_cb, pctx);
521 /* try to connect to the server and wait for completion, we don't want to
522 * autospawn a deamon */
523 GST_LOG_OBJECT (psink, "connect to server %s",
524 GST_STR_NULL (psink->server));
525 if (pa_context_connect (pctx->context, psink->server,
526 PA_CONTEXT_NOAUTOSPAWN, NULL) < 0)
529 GST_INFO_OBJECT (psink,
530 "reusing shared context with name %s, pbuf=%p, pctx=%p",
531 pbuf->context_name, pbuf, pctx);
532 pctx->ring_buffers = g_slist_prepend (pctx->ring_buffers, pbuf);
535 g_mutex_unlock (&pa_shared_resource_mutex);
536 need_unlock_shared = FALSE;
538 /* context created or shared okay */
539 pbuf->context = pa_context_ref (pctx->context);
542 pa_context_state_t state;
544 state = pa_context_get_state (pbuf->context);
546 GST_LOG_OBJECT (psink, "context state is now %d", state);
548 if (!PA_CONTEXT_IS_GOOD (state))
551 if (state == PA_CONTEXT_READY)
554 /* Wait until the context is ready */
555 GST_LOG_OBJECT (psink, "waiting..");
556 pa_threaded_mainloop_wait (mainloop);
559 if (pa_context_get_server_protocol_version (pbuf->context) < 22) {
560 /* We need PulseAudio >= 1.0 on the server side for the extended API */
561 goto bad_server_version;
564 GST_LOG_OBJECT (psink, "opened the device");
566 pa_threaded_mainloop_unlock (mainloop);
573 if (need_unlock_shared)
574 g_mutex_unlock (&pa_shared_resource_mutex);
575 gst_pulsering_destroy_context (pbuf);
576 pa_threaded_mainloop_unlock (mainloop);
581 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
582 ("Failed to create context"), (NULL));
583 g_slice_free (GstPulseContext, pctx);
584 goto unlock_and_fail;
588 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED, ("Failed to connect: %s",
589 pa_strerror (pa_context_errno (pctx->context))), (NULL));
590 goto unlock_and_fail;
594 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED, ("PulseAudio server version "
595 "is too old."), (NULL));
596 goto unlock_and_fail;
600 /* close the device */
602 gst_pulseringbuffer_close_device (GstAudioRingBuffer * buf)
605 GstPulseRingBuffer *pbuf;
607 pbuf = GST_PULSERING_BUFFER_CAST (buf);
608 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
610 GST_LOG_OBJECT (psink, "closing device");
612 pa_threaded_mainloop_lock (mainloop);
613 gst_pulsering_destroy_context (pbuf);
614 pa_threaded_mainloop_unlock (mainloop);
616 GST_LOG_OBJECT (psink, "closed device");
622 gst_pulsering_stream_state_cb (pa_stream * s, void *userdata)
625 GstPulseRingBuffer *pbuf;
626 pa_stream_state_t state;
628 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
629 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
631 state = pa_stream_get_state (s);
632 GST_LOG_OBJECT (psink, "got new stream state %d", state);
635 case PA_STREAM_READY:
636 case PA_STREAM_FAILED:
637 case PA_STREAM_TERMINATED:
638 GST_LOG_OBJECT (psink, "signaling");
639 pa_threaded_mainloop_signal (mainloop, 0);
641 case PA_STREAM_UNCONNECTED:
642 case PA_STREAM_CREATING:
648 gst_pulsering_stream_request_cb (pa_stream * s, size_t length, void *userdata)
651 GstAudioRingBuffer *rbuf;
652 GstPulseRingBuffer *pbuf;
654 rbuf = GST_AUDIO_RING_BUFFER_CAST (userdata);
655 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
656 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
658 GST_LOG_OBJECT (psink, "got request for length %" G_GSIZE_FORMAT, length);
660 if (pbuf->in_commit && (length >= rbuf->spec.segsize)) {
661 /* only signal when we are waiting in the commit thread
662 * and got request for atleast a segment */
663 pa_threaded_mainloop_signal (mainloop, 0);
668 gst_pulsering_stream_underflow_cb (pa_stream * s, void *userdata)
671 GstPulseRingBuffer *pbuf;
673 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
674 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
676 GST_WARNING_OBJECT (psink, "Got underflow");
680 gst_pulsering_stream_overflow_cb (pa_stream * s, void *userdata)
683 GstPulseRingBuffer *pbuf;
685 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
686 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
688 GST_WARNING_OBJECT (psink, "Got overflow");
692 gst_pulsering_stream_latency_cb (pa_stream * s, void *userdata)
695 GstPulseRingBuffer *pbuf;
696 GstAudioRingBuffer *ringbuf;
697 const pa_timing_info *info;
700 info = pa_stream_get_timing_info (s);
702 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
703 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
704 ringbuf = GST_AUDIO_RING_BUFFER (pbuf);
707 GST_LOG_OBJECT (psink, "latency update (information unknown)");
711 if (!info->read_index_corrupt) {
712 /* Update segdone based on the read index. segdone is of segment
713 * granularity, while the read index is at byte granularity. We take the
714 * ceiling while converting the latter to the former since it is more
715 * conservative to report that we've read more than we have than to report
716 * less. One concern here is that latency updates happen every 100ms, which
717 * means segdone is not updated very often, but increasing the update
718 * frequency would mean more communication overhead. */
719 g_atomic_int_set (&ringbuf->segdone,
720 (int) gst_util_uint64_scale_ceil (info->read_index, 1,
721 ringbuf->spec.segsize));
724 sink_usec = info->configured_sink_usec;
726 GST_LOG_OBJECT (psink,
727 "latency_update, %" G_GUINT64_FORMAT ", %d:%" G_GINT64_FORMAT ", %d:%"
728 G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT,
729 GST_TIMEVAL_TO_TIME (info->timestamp), info->write_index_corrupt,
730 info->write_index, info->read_index_corrupt, info->read_index,
731 info->sink_usec, sink_usec);
735 gst_pulsering_stream_suspended_cb (pa_stream * p, void *userdata)
738 GstPulseRingBuffer *pbuf;
740 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
741 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
743 if (pa_stream_is_suspended (p))
744 GST_DEBUG_OBJECT (psink, "stream suspended");
746 GST_DEBUG_OBJECT (psink, "stream resumed");
750 gst_pulsering_stream_started_cb (pa_stream * p, void *userdata)
753 GstPulseRingBuffer *pbuf;
755 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
756 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
758 GST_DEBUG_OBJECT (psink, "stream started");
762 gst_pulsering_stream_event_cb (pa_stream * p, const char *name,
763 pa_proplist * pl, void *userdata)
766 GstPulseRingBuffer *pbuf;
768 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
769 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
771 if (!strcmp (name, PA_STREAM_EVENT_REQUEST_CORK)) {
772 /* the stream wants to PAUSE, post a message for the application. */
773 GST_DEBUG_OBJECT (psink, "got request for CORK");
774 gst_element_post_message (GST_ELEMENT_CAST (psink),
775 gst_message_new_request_state (GST_OBJECT_CAST (psink),
778 } else if (!strcmp (name, PA_STREAM_EVENT_REQUEST_UNCORK)) {
779 GST_DEBUG_OBJECT (psink, "got request for UNCORK");
780 gst_element_post_message (GST_ELEMENT_CAST (psink),
781 gst_message_new_request_state (GST_OBJECT_CAST (psink),
783 } else if (!strcmp (name, PA_STREAM_EVENT_FORMAT_LOST)) {
786 if (g_atomic_int_get (&psink->format_lost)) {
787 /* Duplicate event before we're done reconfiguring, discard */
791 GST_DEBUG_OBJECT (psink, "got FORMAT LOST");
792 g_atomic_int_set (&psink->format_lost, 1);
793 psink->format_lost_time = g_ascii_strtoull (pa_proplist_gets (pl,
794 "stream-time"), NULL, 0) * 1000;
796 g_free (psink->device);
797 psink->device = g_strdup (pa_proplist_gets (pl, "device"));
799 /* FIXME: send reconfigure event instead and let decodebin/playbin
800 * handle that. Also take care of ac3 alignment */
801 renego = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
802 gst_structure_new_empty ("pulse-format-lost"));
805 if (g_str_equal (gst_structure_get_name (st), "audio/x-eac3")) {
806 GstStructure *event_st = gst_structure_new ("ac3parse-set-alignment",
807 "alignment", G_TYPE_STRING, pbin->dbin ? "frame" : "iec61937", NULL);
809 if (!gst_pad_push_event (pbin->sinkpad,
810 gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, event_st)))
811 GST_WARNING_OBJECT (pbin->sinkpad, "Could not update alignment");
815 if (!gst_pad_push_event (GST_BASE_SINK (psink)->sinkpad, renego)) {
816 /* Nobody handled the format change - emit an error */
817 GST_ELEMENT_ERROR (psink, STREAM, FORMAT, ("Sink format changed"),
818 ("Sink format changed"));
821 GST_DEBUG_OBJECT (psink, "got unknown event %s", name);
825 /* Called with the mainloop locked */
827 gst_pulsering_wait_for_stream_ready (GstPulseSink * psink, pa_stream * stream)
829 pa_stream_state_t state;
832 state = pa_stream_get_state (stream);
834 GST_LOG_OBJECT (psink, "stream state is now %d", state);
836 if (!PA_STREAM_IS_GOOD (state))
839 if (state == PA_STREAM_READY)
842 /* Wait until the stream is ready */
843 pa_threaded_mainloop_wait (mainloop);
848 /* This method should create a new stream of the given @spec. No playback should
849 * start yet so we start in the corked state. */
851 gst_pulseringbuffer_acquire (GstAudioRingBuffer * buf,
852 GstAudioRingBufferSpec * spec)
855 GstPulseRingBuffer *pbuf;
856 pa_buffer_attr wanted;
857 const pa_buffer_attr *actual;
858 pa_channel_map channel_map;
859 pa_operation *o = NULL;
861 pa_cvolume *pv = NULL;
862 pa_stream_flags_t flags;
864 GstAudioClock *clock;
865 pa_format_info *formats[1];
866 #ifndef GST_DISABLE_GST_DEBUG
867 gchar print_buf[PA_FORMAT_INFO_SNPRINT_MAX];
870 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
871 pbuf = GST_PULSERING_BUFFER_CAST (buf);
873 GST_LOG_OBJECT (psink, "creating sample spec");
874 /* convert the gstreamer sample spec to the pulseaudio format */
875 if (!gst_pulse_fill_format_info (spec, &pbuf->format, &pbuf->channels))
877 pbuf->is_pcm = pa_format_info_is_pcm (pbuf->format);
879 pa_threaded_mainloop_lock (mainloop);
881 /* we need a context and a no stream */
882 g_assert (pbuf->context);
883 g_assert (!pbuf->stream);
885 /* if we have a probe, disconnect it first so that if we're creating a
886 * compressed stream, it doesn't get blocked by a PCM stream */
887 if (pbuf->probe_stream) {
888 gst_pulse_destroy_stream (pbuf->probe_stream, TRUE);
889 pbuf->probe_stream = NULL;
892 /* enable event notifications */
893 GST_LOG_OBJECT (psink, "subscribing to context events");
894 if (!(o = pa_context_subscribe (pbuf->context,
895 PA_SUBSCRIPTION_MASK_SINK_INPUT, NULL, NULL)))
896 goto subscribe_failed;
898 pa_operation_unref (o);
900 /* initialize the channel map */
901 if (pbuf->is_pcm && gst_pulse_gst_to_channel_map (&channel_map, spec))
902 pa_format_info_set_channel_map (pbuf->format, &channel_map);
904 /* find a good name for the stream */
905 if (psink->stream_name)
906 name = psink->stream_name;
908 name = "Playback Stream";
910 /* create a stream */
911 formats[0] = pbuf->format;
912 if (!(pbuf->stream = pa_stream_new_extended (pbuf->context, name, formats, 1,
916 /* install essential callbacks */
917 pa_stream_set_state_callback (pbuf->stream,
918 gst_pulsering_stream_state_cb, pbuf);
919 pa_stream_set_write_callback (pbuf->stream,
920 gst_pulsering_stream_request_cb, pbuf);
921 pa_stream_set_underflow_callback (pbuf->stream,
922 gst_pulsering_stream_underflow_cb, pbuf);
923 pa_stream_set_overflow_callback (pbuf->stream,
924 gst_pulsering_stream_overflow_cb, pbuf);
925 pa_stream_set_latency_update_callback (pbuf->stream,
926 gst_pulsering_stream_latency_cb, pbuf);
927 pa_stream_set_suspended_callback (pbuf->stream,
928 gst_pulsering_stream_suspended_cb, pbuf);
929 pa_stream_set_started_callback (pbuf->stream,
930 gst_pulsering_stream_started_cb, pbuf);
931 pa_stream_set_event_callback (pbuf->stream,
932 gst_pulsering_stream_event_cb, pbuf);
934 /* buffering requirements. When setting prebuf to 0, the stream will not pause
935 * when we cause an underrun, which causes time to continue. */
936 memset (&wanted, 0, sizeof (wanted));
937 wanted.tlength = spec->segtotal * spec->segsize;
938 wanted.maxlength = -1;
940 wanted.minreq = spec->segsize;
942 GST_INFO_OBJECT (psink, "tlength: %d", wanted.tlength);
943 GST_INFO_OBJECT (psink, "maxlength: %d", wanted.maxlength);
944 GST_INFO_OBJECT (psink, "prebuf: %d", wanted.prebuf);
945 GST_INFO_OBJECT (psink, "minreq: %d", wanted.minreq);
947 /* configure volume when we changed it, else we leave the default */
948 if (psink->volume_set) {
949 GST_LOG_OBJECT (psink, "have volume of %f", psink->volume);
952 gst_pulse_cvolume_from_linear (pv, pbuf->channels, psink->volume);
954 GST_DEBUG_OBJECT (psink, "passthrough stream, not setting volume");
961 /* construct the flags */
962 flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE |
963 PA_STREAM_ADJUST_LATENCY | PA_STREAM_START_CORKED;
965 if (psink->mute_set) {
967 flags |= PA_STREAM_START_MUTED;
969 flags |= PA_STREAM_START_UNMUTED;
972 /* we always start corked (see flags above) */
975 /* try to connect now */
976 GST_LOG_OBJECT (psink, "connect for playback to device %s",
977 GST_STR_NULL (psink->device));
978 if (pa_stream_connect_playback (pbuf->stream, psink->device,
979 &wanted, flags, pv, NULL) < 0)
982 /* our clock will now start from 0 again */
983 clock = GST_AUDIO_CLOCK (GST_AUDIO_BASE_SINK (psink)->provided_clock);
984 gst_audio_clock_reset (clock, 0);
986 if (!gst_pulsering_wait_for_stream_ready (psink, pbuf->stream))
989 g_free (psink->device);
990 psink->device = g_strdup (pa_stream_get_device_name (pbuf->stream));
992 #ifndef GST_DISABLE_GST_DEBUG
993 pa_format_info_snprint (print_buf, sizeof (print_buf),
994 pa_stream_get_format_info (pbuf->stream));
995 GST_INFO_OBJECT (psink, "negotiated to: %s", print_buf);
998 /* After we passed the volume off of to PA we never want to set it
999 again, since it is PA's job to save/restore volumes. */
1000 psink->volume_set = psink->mute_set = FALSE;
1002 GST_LOG_OBJECT (psink, "stream is acquired now");
1004 /* get the actual buffering properties now */
1005 actual = pa_stream_get_buffer_attr (pbuf->stream);
1007 GST_INFO_OBJECT (psink, "tlength: %d (wanted: %d)", actual->tlength,
1009 GST_INFO_OBJECT (psink, "maxlength: %d", actual->maxlength);
1010 GST_INFO_OBJECT (psink, "prebuf: %d", actual->prebuf);
1011 GST_INFO_OBJECT (psink, "minreq: %d (wanted %d)", actual->minreq,
1014 spec->segsize = actual->minreq;
1015 spec->segtotal = actual->tlength / spec->segsize;
1017 pa_threaded_mainloop_unlock (mainloop);
1024 gst_pulsering_destroy_stream (pbuf);
1025 pa_threaded_mainloop_unlock (mainloop);
1031 GST_ELEMENT_ERROR (psink, RESOURCE, SETTINGS,
1032 ("Invalid sample specification."), (NULL));
1037 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1038 ("pa_context_subscribe() failed: %s",
1039 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1040 goto unlock_and_fail;
1044 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1045 ("Failed to create stream: %s",
1046 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1047 goto unlock_and_fail;
1051 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1052 ("Failed to connect stream: %s",
1053 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1054 goto unlock_and_fail;
1058 /* free the stream that we acquired before */
1060 gst_pulseringbuffer_release (GstAudioRingBuffer * buf)
1062 GstPulseRingBuffer *pbuf;
1064 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1066 pa_threaded_mainloop_lock (mainloop);
1067 gst_pulsering_destroy_stream (pbuf);
1068 pa_threaded_mainloop_unlock (mainloop);
1071 GstPulseSink *psink;
1073 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1074 g_atomic_int_set (&psink->format_lost, FALSE);
1075 psink->format_lost_time = GST_CLOCK_TIME_NONE;
1082 gst_pulsering_success_cb (pa_stream * s, int success, void *userdata)
1084 pa_threaded_mainloop_signal (mainloop, 0);
1087 /* update the corked state of a stream, must be called with the mainloop
1090 gst_pulsering_set_corked (GstPulseRingBuffer * pbuf, gboolean corked,
1093 pa_operation *o = NULL;
1094 GstPulseSink *psink;
1095 gboolean res = FALSE;
1097 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1099 if (g_atomic_int_get (&psink->format_lost)) {
1100 /* Sink format changed, stream's gone so fake being paused */
1104 GST_DEBUG_OBJECT (psink, "setting corked state to %d", corked);
1105 if (pbuf->corked != corked) {
1106 if (!(o = pa_stream_cork (pbuf->stream, corked,
1107 gst_pulsering_success_cb, pbuf)))
1110 while (wait && pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1111 pa_threaded_mainloop_wait (mainloop);
1112 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
1115 pbuf->corked = corked;
1117 GST_DEBUG_OBJECT (psink, "skipping, already in requested state");
1123 pa_operation_unref (o);
1130 GST_DEBUG_OBJECT (psink, "the server is dead");
1135 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1136 ("pa_stream_cork() failed: %s",
1137 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1143 gst_pulseringbuffer_clear (GstAudioRingBuffer * buf)
1145 GstPulseSink *psink;
1146 GstPulseRingBuffer *pbuf;
1147 pa_operation *o = NULL;
1149 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1150 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1152 pa_threaded_mainloop_lock (mainloop);
1153 GST_DEBUG_OBJECT (psink, "clearing");
1155 /* don't wait for the flush to complete */
1156 if ((o = pa_stream_flush (pbuf->stream, NULL, pbuf)))
1157 pa_operation_unref (o);
1159 pa_threaded_mainloop_unlock (mainloop);
1162 /* called from pulse with the mainloop lock */
1164 mainloop_enter_defer_cb (pa_mainloop_api * api, void *userdata)
1166 GstPulseSink *pulsesink = GST_PULSESINK (userdata);
1167 GstMessage *message;
1170 GST_DEBUG_OBJECT (pulsesink, "posting ENTER stream status");
1171 message = gst_message_new_stream_status (GST_OBJECT (pulsesink),
1172 GST_STREAM_STATUS_TYPE_ENTER, GST_ELEMENT (pulsesink));
1173 g_value_init (&val, GST_TYPE_G_THREAD);
1174 g_value_set_boxed (&val, g_thread_self ());
1175 gst_message_set_stream_status_object (message, &val);
1176 g_value_unset (&val);
1178 gst_element_post_message (GST_ELEMENT (pulsesink), message);
1180 g_return_if_fail (pulsesink->defer_pending);
1181 pulsesink->defer_pending--;
1182 pa_threaded_mainloop_signal (mainloop, 0);
1185 /* start/resume playback ASAP, we don't uncork here but in the commit method */
1187 gst_pulseringbuffer_start (GstAudioRingBuffer * buf)
1189 GstPulseSink *psink;
1190 GstPulseRingBuffer *pbuf;
1192 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1193 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1195 pa_threaded_mainloop_lock (mainloop);
1197 GST_DEBUG_OBJECT (psink, "scheduling stream status");
1198 psink->defer_pending++;
1199 pa_mainloop_api_once (pa_threaded_mainloop_get_api (mainloop),
1200 mainloop_enter_defer_cb, psink);
1202 GST_DEBUG_OBJECT (psink, "starting");
1203 pbuf->paused = FALSE;
1205 /* EOS needs running clock */
1206 if (GST_BASE_SINK_CAST (psink)->eos ||
1207 g_atomic_int_get (&GST_AUDIO_BASE_SINK (psink)->eos_rendering))
1208 gst_pulsering_set_corked (pbuf, FALSE, FALSE);
1210 pa_threaded_mainloop_unlock (mainloop);
1215 /* pause/stop playback ASAP */
1217 gst_pulseringbuffer_pause (GstAudioRingBuffer * buf)
1219 GstPulseSink *psink;
1220 GstPulseRingBuffer *pbuf;
1223 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1224 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1226 pa_threaded_mainloop_lock (mainloop);
1227 GST_DEBUG_OBJECT (psink, "pausing and corking");
1228 /* make sure the commit method stops writing */
1229 pbuf->paused = TRUE;
1230 res = gst_pulsering_set_corked (pbuf, TRUE, TRUE);
1231 if (pbuf->in_commit) {
1232 /* we are waiting in a commit, signal */
1233 GST_DEBUG_OBJECT (psink, "signal commit");
1234 pa_threaded_mainloop_signal (mainloop, 0);
1236 pa_threaded_mainloop_unlock (mainloop);
1241 /* called from pulse with the mainloop lock */
1243 mainloop_leave_defer_cb (pa_mainloop_api * api, void *userdata)
1245 GstPulseSink *pulsesink = GST_PULSESINK (userdata);
1246 GstMessage *message;
1249 GST_DEBUG_OBJECT (pulsesink, "posting LEAVE stream status");
1250 message = gst_message_new_stream_status (GST_OBJECT (pulsesink),
1251 GST_STREAM_STATUS_TYPE_LEAVE, GST_ELEMENT (pulsesink));
1252 g_value_init (&val, GST_TYPE_G_THREAD);
1253 g_value_set_boxed (&val, g_thread_self ());
1254 gst_message_set_stream_status_object (message, &val);
1255 g_value_unset (&val);
1257 gst_element_post_message (GST_ELEMENT (pulsesink), message);
1259 g_return_if_fail (pulsesink->defer_pending);
1260 pulsesink->defer_pending--;
1261 pa_threaded_mainloop_signal (mainloop, 0);
1264 /* stop playback, we flush everything. */
1266 gst_pulseringbuffer_stop (GstAudioRingBuffer * buf)
1268 GstPulseSink *psink;
1269 GstPulseRingBuffer *pbuf;
1270 gboolean res = FALSE;
1271 pa_operation *o = NULL;
1273 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1274 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1276 pa_threaded_mainloop_lock (mainloop);
1278 pbuf->paused = TRUE;
1279 res = gst_pulsering_set_corked (pbuf, TRUE, TRUE);
1281 /* Inform anyone waiting in _commit() call that it shall wakeup */
1282 if (pbuf->in_commit) {
1283 GST_DEBUG_OBJECT (psink, "signal commit thread");
1284 pa_threaded_mainloop_signal (mainloop, 0);
1286 if (g_atomic_int_get (&psink->format_lost)) {
1287 /* Don't try to flush, the stream's probably gone by now */
1292 /* then try to flush, it's not fatal when this fails */
1293 GST_DEBUG_OBJECT (psink, "flushing");
1294 if ((o = pa_stream_flush (pbuf->stream, gst_pulsering_success_cb, pbuf))) {
1295 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1296 GST_DEBUG_OBJECT (psink, "wait for completion");
1297 pa_threaded_mainloop_wait (mainloop);
1298 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
1301 GST_DEBUG_OBJECT (psink, "flush completed");
1307 pa_operation_cancel (o);
1308 pa_operation_unref (o);
1311 GST_DEBUG_OBJECT (psink, "scheduling stream status");
1312 psink->defer_pending++;
1313 pa_mainloop_api_once (pa_threaded_mainloop_get_api (mainloop),
1314 mainloop_leave_defer_cb, psink);
1316 pa_threaded_mainloop_unlock (mainloop);
1323 GST_DEBUG_OBJECT (psink, "the server is dead");
1328 /* in_samples >= out_samples, rate > 1.0 */
1329 #define FWD_UP_SAMPLES(s,se,d,de) \
1331 guint8 *sb = s, *db = d; \
1332 while (s <= se && d < de) { \
1333 memcpy (d, s, bpf); \
1336 if ((*accum << 1) >= inr) { \
1341 in_samples -= (s - sb)/bpf; \
1342 out_samples -= (d - db)/bpf; \
1343 GST_DEBUG ("fwd_up end %d/%d",*accum,*toprocess); \
1346 /* out_samples > in_samples, for rates smaller than 1.0 */
1347 #define FWD_DOWN_SAMPLES(s,se,d,de) \
1349 guint8 *sb = s, *db = d; \
1350 while (s <= se && d < de) { \
1351 memcpy (d, s, bpf); \
1354 if ((*accum << 1) >= outr) { \
1359 in_samples -= (s - sb)/bpf; \
1360 out_samples -= (d - db)/bpf; \
1361 GST_DEBUG ("fwd_down end %d/%d",*accum,*toprocess); \
1364 #define REV_UP_SAMPLES(s,se,d,de) \
1366 guint8 *sb = se, *db = d; \
1367 while (s <= se && d < de) { \
1368 memcpy (d, se, bpf); \
1371 while (d < de && (*accum << 1) >= inr) { \
1376 in_samples -= (sb - se)/bpf; \
1377 out_samples -= (d - db)/bpf; \
1378 GST_DEBUG ("rev_up end %d/%d",*accum,*toprocess); \
1381 #define REV_DOWN_SAMPLES(s,se,d,de) \
1383 guint8 *sb = se, *db = d; \
1384 while (s <= se && d < de) { \
1385 memcpy (d, se, bpf); \
1388 while (s <= se && (*accum << 1) >= outr) { \
1393 in_samples -= (sb - se)/bpf; \
1394 out_samples -= (d - db)/bpf; \
1395 GST_DEBUG ("rev_down end %d/%d",*accum,*toprocess); \
1398 /* our custom commit function because we write into the buffer of pulseaudio
1399 * instead of keeping our own buffer */
1401 gst_pulseringbuffer_commit (GstAudioRingBuffer * buf, guint64 * sample,
1402 guchar * data, gint in_samples, gint out_samples, gint * accum)
1404 GstPulseSink *psink;
1405 GstPulseRingBuffer *pbuf;
1410 gint inr, outr, bpf;
1414 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1415 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1417 /* FIXME post message rather than using a signal (as mixer interface) */
1418 if (g_atomic_int_compare_and_exchange (&psink->notify, 1, 0)) {
1419 g_object_notify (G_OBJECT (psink), "volume");
1420 g_object_notify (G_OBJECT (psink), "mute");
1423 /* make sure the ringbuffer is started */
1424 if (G_UNLIKELY (g_atomic_int_get (&buf->state) !=
1425 GST_AUDIO_RING_BUFFER_STATE_STARTED)) {
1426 /* see if we are allowed to start it */
1427 if (G_UNLIKELY (g_atomic_int_get (&buf->may_start) == FALSE))
1430 GST_DEBUG_OBJECT (buf, "start!");
1431 if (!gst_audio_ring_buffer_start (buf))
1435 pa_threaded_mainloop_lock (mainloop);
1437 GST_DEBUG_OBJECT (psink, "entering commit");
1438 pbuf->in_commit = TRUE;
1440 bpf = GST_AUDIO_INFO_BPF (&buf->spec.info);
1441 bufsize = buf->spec.segsize * buf->spec.segtotal;
1443 /* our toy resampler for trick modes */
1444 reverse = out_samples < 0;
1445 out_samples = ABS (out_samples);
1447 if (in_samples >= out_samples)
1448 toprocess = &in_samples;
1450 toprocess = &out_samples;
1452 inr = in_samples - 1;
1453 outr = out_samples - 1;
1455 GST_DEBUG_OBJECT (psink, "in %d, out %d", inr, outr);
1457 /* data_end points to the last sample we have to write, not past it. This is
1458 * needed to properly handle reverse playback: it points to the last sample. */
1459 data_end = data + (bpf * inr);
1461 if (g_atomic_int_get (&psink->format_lost)) {
1462 /* Sink format changed, drop the data and hope upstream renegotiates */
1469 /* offset is in bytes */
1470 offset = *sample * bpf;
1472 while (*toprocess > 0) {
1476 GST_LOG_OBJECT (psink,
1477 "need to write %d samples at offset %" G_GINT64_FORMAT, *toprocess,
1480 if (offset != pbuf->m_lastoffset)
1481 GST_LOG_OBJECT (psink, "discontinuity, offset is %" G_GINT64_FORMAT ", "
1482 "last offset was %" G_GINT64_FORMAT, offset, pbuf->m_lastoffset);
1484 towrite = out_samples * bpf;
1486 /* Wait for at least segsize bytes to become available */
1487 if (towrite > buf->spec.segsize)
1488 towrite = buf->spec.segsize;
1490 if ((pbuf->m_writable < towrite) || (offset != pbuf->m_lastoffset)) {
1491 /* if no room left or discontinuity in offset,
1492 we need to flush data and get a new buffer */
1494 /* flush the buffer if possible */
1495 if ((pbuf->m_data != NULL) && (pbuf->m_towrite > 0)) {
1497 GST_LOG_OBJECT (psink,
1498 "flushing %u samples at offset %" G_GINT64_FORMAT,
1499 (guint) pbuf->m_towrite / bpf, pbuf->m_offset);
1501 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1502 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1506 pbuf->m_towrite = 0;
1507 pbuf->m_offset = offset; /* keep track of current offset */
1509 /* get a buffer to write in for now on */
1511 pbuf->m_writable = pa_stream_writable_size (pbuf->stream);
1513 if (g_atomic_int_get (&psink->format_lost)) {
1514 /* Sink format changed, give up and hope upstream renegotiates */
1518 if (pbuf->m_writable == (size_t) - 1)
1519 goto writable_size_failed;
1521 pbuf->m_writable /= bpf;
1522 pbuf->m_writable *= bpf; /* handle only complete samples */
1524 if (pbuf->m_writable >= towrite)
1527 /* see if we need to uncork because we have no free space */
1529 if (!gst_pulsering_set_corked (pbuf, FALSE, FALSE))
1533 /* we can't write segsize bytes, wait a bit */
1534 GST_LOG_OBJECT (psink, "waiting for free space");
1535 pa_threaded_mainloop_wait (mainloop);
1541 /* Recalculate what we can write in the next chunk */
1542 towrite = out_samples * bpf;
1543 if (pbuf->m_writable > towrite)
1544 pbuf->m_writable = towrite;
1546 GST_LOG_OBJECT (psink, "requesting %" G_GSIZE_FORMAT " bytes of "
1547 "shared memory", pbuf->m_writable);
1549 if (pa_stream_begin_write (pbuf->stream, &pbuf->m_data,
1550 &pbuf->m_writable) < 0) {
1551 GST_LOG_OBJECT (psink, "pa_stream_begin_write() failed");
1552 goto writable_size_failed;
1555 GST_LOG_OBJECT (psink, "got %" G_GSIZE_FORMAT " bytes of shared memory",
1560 if (towrite > pbuf->m_writable)
1561 towrite = pbuf->m_writable;
1562 avail = towrite / bpf;
1564 GST_LOG_OBJECT (psink, "writing %u samples at offset %" G_GUINT64_FORMAT,
1565 (guint) avail, offset);
1567 /* No trick modes for passthrough streams */
1568 if (G_UNLIKELY (!pbuf->is_pcm && (inr != outr || reverse))) {
1569 GST_WARNING_OBJECT (psink, "Passthrough stream can't run in trick mode");
1570 goto unlock_and_fail;
1573 if (G_LIKELY (inr == outr && !reverse)) {
1574 /* no rate conversion, simply write out the samples */
1575 /* copy the data into internal buffer */
1577 memcpy ((guint8 *) pbuf->m_data + pbuf->m_towrite, data, towrite);
1578 pbuf->m_towrite += towrite;
1579 pbuf->m_writable -= towrite;
1582 in_samples -= avail;
1583 out_samples -= avail;
1585 guint8 *dest, *d, *d_end;
1587 /* write into the PulseAudio shm buffer */
1588 dest = d = (guint8 *) pbuf->m_data + pbuf->m_towrite;
1589 d_end = d + towrite;
1593 /* forward speed up */
1594 FWD_UP_SAMPLES (data, data_end, d, d_end);
1596 /* forward slow down */
1597 FWD_DOWN_SAMPLES (data, data_end, d, d_end);
1600 /* reverse speed up */
1601 REV_UP_SAMPLES (data, data_end, d, d_end);
1603 /* reverse slow down */
1604 REV_DOWN_SAMPLES (data, data_end, d, d_end);
1606 /* see what we have left to write */
1607 towrite = (d - dest);
1608 pbuf->m_towrite += towrite;
1609 pbuf->m_writable -= towrite;
1611 avail = towrite / bpf;
1614 /* flush the buffer if it's full */
1615 if ((pbuf->m_data != NULL) && (pbuf->m_towrite > 0)
1616 && (pbuf->m_writable == 0)) {
1617 GST_LOG_OBJECT (psink, "flushing %u samples at offset %" G_GINT64_FORMAT,
1618 (guint) pbuf->m_towrite / bpf, pbuf->m_offset);
1620 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1621 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1624 pbuf->m_towrite = 0;
1625 pbuf->m_offset = offset + towrite; /* keep track of current offset */
1629 offset += avail * bpf;
1630 pbuf->m_lastoffset = offset;
1632 /* check if we need to uncork after writing the samples */
1634 const pa_timing_info *info;
1636 if ((info = pa_stream_get_timing_info (pbuf->stream))) {
1637 GST_LOG_OBJECT (psink,
1638 "read_index at %" G_GUINT64_FORMAT ", offset %" G_GINT64_FORMAT,
1639 info->read_index, offset);
1641 /* we uncork when the read_index is too far behind the offset we need
1643 if (info->read_index + bufsize <= offset) {
1644 if (!gst_pulsering_set_corked (pbuf, FALSE, FALSE))
1648 GST_LOG_OBJECT (psink, "no timing info available yet");
1654 /* we consumed all samples here */
1655 data = data_end + bpf;
1657 pbuf->in_commit = FALSE;
1658 pa_threaded_mainloop_unlock (mainloop);
1661 result = inr - ((data_end - data) / bpf);
1662 GST_LOG_OBJECT (psink, "wrote %d samples", result);
1669 pbuf->in_commit = FALSE;
1670 GST_LOG_OBJECT (psink, "we are reset");
1671 pa_threaded_mainloop_unlock (mainloop);
1676 GST_LOG_OBJECT (psink, "we can not start");
1681 GST_LOG_OBJECT (psink, "failed to start the ringbuffer");
1686 pbuf->in_commit = FALSE;
1687 GST_ERROR_OBJECT (psink, "uncork failed");
1688 pa_threaded_mainloop_unlock (mainloop);
1693 pbuf->in_commit = FALSE;
1694 GST_LOG_OBJECT (psink, "we are paused");
1695 pa_threaded_mainloop_unlock (mainloop);
1698 writable_size_failed:
1700 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1701 ("pa_stream_writable_size() failed: %s",
1702 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1703 goto unlock_and_fail;
1707 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1708 ("pa_stream_write() failed: %s",
1709 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1710 goto unlock_and_fail;
1714 /* write pending local samples, must be called with the mainloop lock */
1716 gst_pulsering_flush (GstPulseRingBuffer * pbuf)
1718 GstPulseSink *psink;
1720 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1721 GST_DEBUG_OBJECT (psink, "entering flush");
1723 /* flush the buffer if possible */
1724 if (pbuf->stream && (pbuf->m_data != NULL) && (pbuf->m_towrite > 0)) {
1725 #ifndef GST_DISABLE_GST_DEBUG
1728 bpf = (GST_AUDIO_RING_BUFFER_CAST (pbuf))->spec.info.bpf;
1729 GST_LOG_OBJECT (psink,
1730 "flushing %u samples at offset %" G_GINT64_FORMAT,
1731 (guint) pbuf->m_towrite / bpf, pbuf->m_offset);
1734 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1735 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1739 pbuf->m_towrite = 0;
1740 pbuf->m_offset += pbuf->m_towrite; /* keep track of current offset */
1749 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1750 ("pa_stream_write() failed: %s",
1751 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1756 static void gst_pulsesink_set_property (GObject * object, guint prop_id,
1757 const GValue * value, GParamSpec * pspec);
1758 static void gst_pulsesink_get_property (GObject * object, guint prop_id,
1759 GValue * value, GParamSpec * pspec);
1760 static void gst_pulsesink_finalize (GObject * object);
1762 static gboolean gst_pulsesink_event (GstBaseSink * sink, GstEvent * event);
1763 static gboolean gst_pulsesink_query (GstBaseSink * sink, GstQuery * query);
1765 static GstStateChangeReturn gst_pulsesink_change_state (GstElement * element,
1766 GstStateChange transition);
1768 static GstStaticPadTemplate pad_template = GST_STATIC_PAD_TEMPLATE ("sink",
1771 GST_STATIC_CAPS (PULSE_SINK_TEMPLATE_CAPS));
1773 #define gst_pulsesink_parent_class parent_class
1774 G_DEFINE_TYPE_WITH_CODE (GstPulseSink, gst_pulsesink, GST_TYPE_AUDIO_BASE_SINK,
1775 gst_pulsesink_init_contexts ();
1776 G_IMPLEMENT_INTERFACE (GST_TYPE_STREAM_VOLUME, NULL)
1779 static GstAudioRingBuffer *
1780 gst_pulsesink_create_ringbuffer (GstAudioBaseSink * sink)
1782 GstAudioRingBuffer *buffer;
1784 GST_DEBUG_OBJECT (sink, "creating ringbuffer");
1785 buffer = g_object_new (GST_TYPE_PULSERING_BUFFER, NULL);
1786 GST_DEBUG_OBJECT (sink, "created ringbuffer @%p", buffer);
1792 gst_pulsesink_payload (GstAudioBaseSink * sink, GstBuffer * buf)
1794 switch (sink->ringbuffer->spec.type) {
1795 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3:
1796 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_EAC3:
1797 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS:
1798 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_MPEG:
1800 /* FIXME: alloc memory from PA if possible */
1801 gint framesize = gst_audio_iec61937_frame_size (&sink->ringbuffer->spec);
1803 GstMapInfo inmap, outmap;
1809 out = gst_buffer_new_and_alloc (framesize);
1811 gst_buffer_map (buf, &inmap, GST_MAP_READ);
1812 gst_buffer_map (out, &outmap, GST_MAP_WRITE);
1814 res = gst_audio_iec61937_payload (inmap.data, inmap.size,
1815 outmap.data, outmap.size, &sink->ringbuffer->spec, G_BIG_ENDIAN);
1817 gst_buffer_unmap (buf, &inmap);
1818 gst_buffer_unmap (out, &outmap);
1821 gst_buffer_unref (out);
1825 gst_buffer_copy_into (out, buf, GST_BUFFER_COPY_METADATA, 0, -1);
1830 return gst_buffer_ref (buf);
1835 gst_pulsesink_class_init (GstPulseSinkClass * klass)
1837 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1838 GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
1839 GstBaseSinkClass *bc;
1840 GstAudioBaseSinkClass *gstaudiosink_class = GST_AUDIO_BASE_SINK_CLASS (klass);
1841 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1844 gobject_class->finalize = gst_pulsesink_finalize;
1845 gobject_class->set_property = gst_pulsesink_set_property;
1846 gobject_class->get_property = gst_pulsesink_get_property;
1848 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_pulsesink_event);
1849 gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_pulsesink_query);
1851 /* restore the original basesink pull methods */
1852 bc = g_type_class_peek (GST_TYPE_BASE_SINK);
1853 gstbasesink_class->activate_pull = GST_DEBUG_FUNCPTR (bc->activate_pull);
1855 gstelement_class->change_state =
1856 GST_DEBUG_FUNCPTR (gst_pulsesink_change_state);
1858 gstaudiosink_class->create_ringbuffer =
1859 GST_DEBUG_FUNCPTR (gst_pulsesink_create_ringbuffer);
1860 gstaudiosink_class->payload = GST_DEBUG_FUNCPTR (gst_pulsesink_payload);
1862 /* Overwrite GObject fields */
1863 g_object_class_install_property (gobject_class,
1865 g_param_spec_string ("server", "Server",
1866 "The PulseAudio server to connect to", DEFAULT_SERVER,
1867 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1869 g_object_class_install_property (gobject_class, PROP_DEVICE,
1870 g_param_spec_string ("device", "Device",
1871 "The PulseAudio sink device to connect to", DEFAULT_DEVICE,
1872 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1874 g_object_class_install_property (gobject_class,
1876 g_param_spec_string ("device-name", "Device name",
1877 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
1878 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1880 g_object_class_install_property (gobject_class,
1882 g_param_spec_double ("volume", "Volume",
1883 "Linear volume of this stream, 1.0=100%", 0.0, MAX_VOLUME,
1884 DEFAULT_VOLUME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1885 g_object_class_install_property (gobject_class,
1887 g_param_spec_boolean ("mute", "Mute",
1888 "Mute state of this stream", DEFAULT_MUTE,
1889 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1892 * GstPulseSink:client-name
1894 * The PulseAudio client name to use.
1896 clientname = gst_pulse_client_name ();
1897 g_object_class_install_property (gobject_class,
1899 g_param_spec_string ("client-name", "Client Name",
1900 "The PulseAudio client name to use", clientname,
1901 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1902 GST_PARAM_MUTABLE_READY));
1903 g_free (clientname);
1906 * GstPulseSink:stream-properties
1908 * List of pulseaudio stream properties. A list of defined properties can be
1909 * found in the <ulink url="http://0pointer.de/lennart/projects/pulseaudio/doxygen/proplist_8h.html">pulseaudio api docs</ulink>.
1911 * Below is an example for registering as a music application to pulseaudio.
1913 * GstStructure *props;
1915 * props = gst_structure_from_string ("props,media.role=music", NULL);
1916 * g_object_set (pulse, "stream-properties", props, NULL);
1917 * gst_structure_free
1922 g_object_class_install_property (gobject_class,
1923 PROP_STREAM_PROPERTIES,
1924 g_param_spec_boxed ("stream-properties", "stream properties",
1925 "list of pulseaudio stream properties",
1926 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1928 gst_element_class_set_static_metadata (gstelement_class,
1929 "PulseAudio Audio Sink",
1930 "Sink/Audio", "Plays audio to a PulseAudio server", "Lennart Poettering");
1931 gst_element_class_add_pad_template (gstelement_class,
1932 gst_static_pad_template_get (&pad_template));
1936 free_device_info (GstPulseDeviceInfo * device_info)
1940 g_free (device_info->description);
1942 for (l = g_list_first (device_info->formats); l; l = g_list_next (l))
1943 pa_format_info_free ((pa_format_info *) l->data);
1945 g_list_free (device_info->formats);
1948 /* Returns the current time of the sink ringbuffer. The timing_info is updated
1949 * on every data write/flush and every 100ms (PA_STREAM_AUTO_TIMING_UPDATE).
1952 gst_pulsesink_get_time (GstClock * clock, GstAudioBaseSink * sink)
1954 GstPulseSink *psink;
1955 GstPulseRingBuffer *pbuf;
1958 if (!sink->ringbuffer || !sink->ringbuffer->acquired)
1959 return GST_CLOCK_TIME_NONE;
1961 pbuf = GST_PULSERING_BUFFER_CAST (sink->ringbuffer);
1962 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1964 if (g_atomic_int_get (&psink->format_lost)) {
1965 /* Stream was lost in a format change, it'll get set up again once
1966 * upstream renegotiates */
1967 return psink->format_lost_time;
1970 pa_threaded_mainloop_lock (mainloop);
1971 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
1974 /* if we don't have enough data to get a timestamp, just return NONE, which
1975 * will return the last reported time */
1976 if (pa_stream_get_time (pbuf->stream, &time) < 0) {
1977 GST_DEBUG_OBJECT (psink, "could not get time");
1978 time = GST_CLOCK_TIME_NONE;
1981 pa_threaded_mainloop_unlock (mainloop);
1983 GST_LOG_OBJECT (psink, "current time is %" GST_TIME_FORMAT,
1984 GST_TIME_ARGS (time));
1991 GST_DEBUG_OBJECT (psink, "the server is dead");
1992 pa_threaded_mainloop_unlock (mainloop);
1994 return GST_CLOCK_TIME_NONE;
1999 gst_pulsesink_sink_info_cb (pa_context * c, const pa_sink_info * i, int eol,
2002 GstPulseDeviceInfo *device_info = (GstPulseDeviceInfo *) userdata;
2008 device_info->description = g_strdup (i->description);
2010 device_info->formats = NULL;
2011 for (j = 0; j < i->n_formats; j++)
2012 device_info->formats = g_list_prepend (device_info->formats,
2013 pa_format_info_copy (i->formats[j]));
2016 pa_threaded_mainloop_signal (mainloop, 0);
2020 gst_pulse_format_info_int_prop_to_value (pa_format_info * format,
2021 const char *key, GValue * value)
2028 if (pa_format_info_get_prop_int (format, key, &i) == 0) {
2029 g_value_init (value, G_TYPE_INT);
2030 g_value_set_int (value, i);
2032 } else if (pa_format_info_get_prop_int_array (format, key, &a, &n) == 0) {
2033 g_value_init (value, GST_TYPE_LIST);
2034 g_value_init (&v, G_TYPE_INT);
2036 for (i = 0; i < n; i++) {
2037 g_value_set_int (&v, a[i]);
2038 gst_value_list_append_value (value, &v);
2043 } else if (pa_format_info_get_prop_int_range (format, key, &min, &max) == 0) {
2044 g_value_init (value, GST_TYPE_INT_RANGE);
2045 gst_value_set_int_range (value, min, max);
2048 /* Property not available or is not an int type */
2056 gst_pulse_format_info_to_caps (pa_format_info * format)
2058 GstCaps *ret = NULL;
2062 switch (format->encoding) {
2063 case PA_ENCODING_PCM:{
2066 pa_format_info_to_sample_spec (format, &ss, NULL);
2068 if (pa_format_info_get_prop_string (format,
2069 PA_PROP_FORMAT_SAMPLE_FORMAT, &tmp)) {
2070 /* No specific sample format means any sample format */
2071 ret = gst_caps_from_string (_PULSE_SINK_CAPS_PCM);
2074 } else if (ss.format == PA_SAMPLE_ALAW) {
2075 ret = gst_caps_from_string (_PULSE_SINK_CAPS_ALAW);
2077 } else if (ss.format == PA_SAMPLE_ULAW) {
2078 ret = gst_caps_from_string (_PULSE_SINK_CAPS_MP3);
2081 /* Linear PCM format */
2082 const char *sformat =
2083 gst_pulse_sample_format_to_caps_format (ss.format);
2085 ret = gst_caps_from_string (_PULSE_SINK_CAPS_LINEAR);
2088 gst_caps_set_simple (ret, "format", G_TYPE_STRING, NULL);
2095 case PA_ENCODING_AC3_IEC61937:
2096 ret = gst_caps_from_string (_PULSE_SINK_CAPS_AC3);
2099 case PA_ENCODING_EAC3_IEC61937:
2100 ret = gst_caps_from_string (_PULSE_SINK_CAPS_EAC3);
2103 case PA_ENCODING_DTS_IEC61937:
2104 ret = gst_caps_from_string (_PULSE_SINK_CAPS_DTS);
2107 case PA_ENCODING_MPEG_IEC61937:
2108 ret = gst_caps_from_string (_PULSE_SINK_CAPS_MP3);
2112 GST_WARNING ("Found a PA format that we don't support yet");
2116 if (gst_pulse_format_info_int_prop_to_value (format, PA_PROP_FORMAT_RATE, &v))
2117 gst_caps_set_value (ret, "rate", &v);
2121 if (gst_pulse_format_info_int_prop_to_value (format, PA_PROP_FORMAT_CHANNELS,
2123 gst_caps_set_value (ret, "channels", &v);
2129 /* Call with mainloop lock held */
2131 gst_pulsesink_create_probe_stream (GstPulseSink * psink,
2132 GstPulseRingBuffer * pbuf, pa_format_info * format)
2134 pa_format_info *formats[1] = { format };
2136 pa_stream_flags_t flags;
2138 GST_LOG_OBJECT (psink, "Creating probe stream");
2140 if (!(stream = pa_stream_new_extended (pbuf->context, "pulsesink probe",
2141 formats, 1, psink->proplist)))
2144 /* construct the flags */
2145 flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE |
2146 PA_STREAM_ADJUST_LATENCY | PA_STREAM_START_CORKED;
2148 pa_stream_set_state_callback (stream, gst_pulsering_stream_state_cb, pbuf);
2150 if (pa_stream_connect_playback (stream, psink->device, NULL, flags, NULL,
2154 if (!gst_pulsering_wait_for_stream_ready (psink, stream))
2161 pa_stream_unref (stream);
2166 gst_pulsesink_query_getcaps (GstPulseSink * psink, GstCaps * filter)
2168 GstPulseRingBuffer *pbuf = NULL;
2169 GstPulseDeviceInfo device_info = { NULL, NULL };
2170 GstCaps *ret = NULL;
2172 pa_operation *o = NULL;
2175 GST_OBJECT_LOCK (psink);
2176 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2178 gst_object_ref (pbuf);
2179 GST_OBJECT_UNLOCK (psink);
2182 ret = gst_pad_get_pad_template_caps (GST_AUDIO_BASE_SINK_PAD (psink));
2186 GST_OBJECT_LOCK (pbuf);
2187 pa_threaded_mainloop_lock (mainloop);
2189 if (!pbuf->context) {
2190 ret = gst_pad_get_pad_template_caps (GST_AUDIO_BASE_SINK_PAD (psink));
2195 /* We're in PAUSED or higher */
2196 stream = pbuf->stream;
2198 } else if (pbuf->probe_stream) {
2199 /* We're not paused, but have a cached probe stream */
2200 stream = pbuf->probe_stream;
2203 /* We're not yet in PAUSED and still need to create a probe stream.
2205 * FIXME: PA doesn't accept "any" format. We fix something reasonable since
2206 * this is merely a probe. This should eventually be fixed in PA and
2207 * hard-coding the format should be dropped. */
2208 pa_format_info *format = pa_format_info_new ();
2209 format->encoding = PA_ENCODING_PCM;
2210 pa_format_info_set_sample_format (format, PA_SAMPLE_S16LE);
2211 pa_format_info_set_rate (format, GST_AUDIO_DEF_RATE);
2212 pa_format_info_set_channels (format, GST_AUDIO_DEF_CHANNELS);
2214 pbuf->probe_stream = gst_pulsesink_create_probe_stream (psink, pbuf,
2216 if (!pbuf->probe_stream) {
2217 GST_WARNING_OBJECT (psink, "Could not create probe stream");
2221 pa_format_info_free (format);
2223 stream = pbuf->probe_stream;
2226 ret = gst_caps_new_empty ();
2228 if (!(o = pa_context_get_sink_info_by_name (pbuf->context,
2229 pa_stream_get_device_name (stream), gst_pulsesink_sink_info_cb,
2233 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2234 pa_threaded_mainloop_wait (mainloop);
2235 if (gst_pulsering_is_dead (psink, pbuf, FALSE))
2239 for (i = g_list_first (device_info.formats); i; i = g_list_next (i)) {
2240 gst_caps_append (ret,
2241 gst_pulse_format_info_to_caps ((pa_format_info *) i->data));
2245 GstCaps *tmp = gst_caps_intersect_full (filter, ret,
2246 GST_CAPS_INTERSECT_FIRST);
2247 gst_caps_unref (ret);
2252 pa_threaded_mainloop_unlock (mainloop);
2253 /* FIXME: this could be freed after device_name is got */
2254 GST_OBJECT_UNLOCK (pbuf);
2257 free_device_info (&device_info);
2260 pa_operation_unref (o);
2263 gst_object_unref (pbuf);
2265 GST_DEBUG_OBJECT (psink, "caps %" GST_PTR_FORMAT, ret);
2271 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2272 ("pa_context_get_sink_input_info() failed: %s",
2273 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2279 gst_pulsesink_query_acceptcaps (GstPulseSink * psink, GstCaps * caps)
2281 GstPulseRingBuffer *pbuf = NULL;
2282 GstPulseDeviceInfo device_info = { NULL, NULL };
2285 gboolean ret = FALSE;
2287 GstAudioRingBufferSpec spec = { 0 };
2288 pa_operation *o = NULL;
2289 pa_channel_map channel_map;
2290 pa_format_info *format = NULL;
2293 pad_caps = gst_pad_get_pad_template_caps (GST_BASE_SINK_PAD (psink));
2294 ret = gst_caps_is_subset (caps, pad_caps);
2295 gst_caps_unref (pad_caps);
2297 GST_DEBUG_OBJECT (psink, "caps %" GST_PTR_FORMAT, caps);
2299 /* Template caps didn't match */
2303 /* If we've not got fixed caps, creating a stream might fail, so let's just
2304 * return from here with default acceptcaps behaviour */
2305 if (!gst_caps_is_fixed (caps))
2308 GST_OBJECT_LOCK (psink);
2309 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2311 gst_object_ref (pbuf);
2312 GST_OBJECT_UNLOCK (psink);
2314 /* We're still in NULL state */
2318 GST_OBJECT_LOCK (pbuf);
2319 pa_threaded_mainloop_lock (mainloop);
2321 if (pbuf->context == NULL)
2326 spec.latency_time = GST_AUDIO_BASE_SINK (psink)->latency_time;
2327 if (!gst_audio_ring_buffer_parse_caps (&spec, caps))
2330 if (!gst_pulse_fill_format_info (&spec, &format, &channels))
2333 /* Make sure input is framed (one frame per buffer) and can be payloaded */
2334 if (!pa_format_info_is_pcm (format)) {
2335 gboolean framed = FALSE, parsed = FALSE;
2336 st = gst_caps_get_structure (caps, 0);
2338 gst_structure_get_boolean (st, "framed", &framed);
2339 gst_structure_get_boolean (st, "parsed", &parsed);
2340 if ((!framed && !parsed) || gst_audio_iec61937_frame_size (&spec) <= 0)
2344 /* initialize the channel map */
2345 if (pa_format_info_is_pcm (format) &&
2346 gst_pulse_gst_to_channel_map (&channel_map, &spec))
2347 pa_format_info_set_channel_map (format, &channel_map);
2349 if (pbuf->stream || pbuf->probe_stream) {
2350 /* We're already in PAUSED or above, so just reuse this stream to query
2351 * sink formats and use those. */
2353 const char *device_name = pa_stream_get_device_name (pbuf->stream ?
2354 pbuf->stream : pbuf->probe_stream);
2356 if (!(o = pa_context_get_sink_info_by_name (pbuf->context, device_name,
2357 gst_pulsesink_sink_info_cb, &device_info)))
2360 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2361 pa_threaded_mainloop_wait (mainloop);
2362 if (gst_pulsering_is_dead (psink, pbuf, FALSE))
2366 for (i = g_list_first (device_info.formats); i; i = g_list_next (i)) {
2367 if (pa_format_info_is_compatible ((pa_format_info *) i->data, format)) {
2373 /* We're in READY, let's connect a stream to see if the format is
2374 * accepted by whatever sink we're routed to */
2375 pbuf->probe_stream = gst_pulsesink_create_probe_stream (psink, pbuf,
2377 if (pbuf->probe_stream)
2383 pa_format_info_free (format);
2386 pa_operation_unref (o);
2388 pa_threaded_mainloop_unlock (mainloop);
2389 GST_OBJECT_UNLOCK (pbuf);
2391 gst_caps_replace (&spec.caps, NULL);
2392 gst_object_unref (pbuf);
2400 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2401 ("pa_context_get_sink_input_info() failed: %s",
2402 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2408 gst_pulsesink_init (GstPulseSink * pulsesink)
2410 pulsesink->server = NULL;
2411 pulsesink->device = NULL;
2412 pulsesink->device_info.description = NULL;
2413 pulsesink->client_name = gst_pulse_client_name ();
2415 pulsesink->device_info.formats = NULL;
2417 pulsesink->volume = DEFAULT_VOLUME;
2418 pulsesink->volume_set = FALSE;
2420 pulsesink->mute = DEFAULT_MUTE;
2421 pulsesink->mute_set = FALSE;
2423 pulsesink->notify = 0;
2425 g_atomic_int_set (&pulsesink->format_lost, FALSE);
2426 pulsesink->format_lost_time = GST_CLOCK_TIME_NONE;
2428 pulsesink->properties = NULL;
2429 pulsesink->proplist = NULL;
2431 /* override with a custom clock */
2432 if (GST_AUDIO_BASE_SINK (pulsesink)->provided_clock)
2433 gst_object_unref (GST_AUDIO_BASE_SINK (pulsesink)->provided_clock);
2435 GST_AUDIO_BASE_SINK (pulsesink)->provided_clock =
2436 gst_audio_clock_new ("GstPulseSinkClock",
2437 (GstAudioClockGetTimeFunc) gst_pulsesink_get_time, pulsesink, NULL);
2439 /* TRUE for sinks, FALSE for sources */
2440 pulsesink->probe = gst_pulseprobe_new (G_OBJECT (pulsesink),
2441 G_OBJECT_GET_CLASS (pulsesink), PROP_DEVICE, pulsesink->device,
2446 gst_pulsesink_finalize (GObject * object)
2448 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2450 g_free (pulsesink->server);
2451 g_free (pulsesink->device);
2452 g_free (pulsesink->client_name);
2454 free_device_info (&pulsesink->device_info);
2456 if (pulsesink->properties)
2457 gst_structure_free (pulsesink->properties);
2458 if (pulsesink->proplist)
2459 pa_proplist_free (pulsesink->proplist);
2461 if (pulsesink->probe) {
2462 gst_pulseprobe_free (pulsesink->probe);
2463 pulsesink->probe = NULL;
2466 G_OBJECT_CLASS (parent_class)->finalize (object);
2470 gst_pulsesink_set_volume (GstPulseSink * psink, gdouble volume)
2473 pa_operation *o = NULL;
2474 GstPulseRingBuffer *pbuf;
2480 pa_threaded_mainloop_lock (mainloop);
2482 GST_DEBUG_OBJECT (psink, "setting volume to %f", volume);
2484 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2485 if (pbuf == NULL || pbuf->stream == NULL)
2488 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2492 gst_pulse_cvolume_from_linear (&v, pbuf->channels, volume);
2494 /* FIXME: this will eventually be superceded by checks to see if the volume
2495 * is readable/writable */
2498 if (!(o = pa_context_set_sink_input_volume (pbuf->context, idx,
2502 /* We don't really care about the result of this call */
2506 pa_operation_unref (o);
2508 pa_threaded_mainloop_unlock (mainloop);
2515 psink->volume = volume;
2516 psink->volume_set = TRUE;
2518 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2523 psink->volume = volume;
2524 psink->volume_set = TRUE;
2526 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2531 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2536 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2537 ("pa_stream_set_sink_input_volume() failed: %s",
2538 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2544 gst_pulsesink_set_mute (GstPulseSink * psink, gboolean mute)
2546 pa_operation *o = NULL;
2547 GstPulseRingBuffer *pbuf;
2553 pa_threaded_mainloop_lock (mainloop);
2555 GST_DEBUG_OBJECT (psink, "setting mute state to %d", mute);
2557 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2558 if (pbuf == NULL || pbuf->stream == NULL)
2561 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2564 if (!(o = pa_context_set_sink_input_mute (pbuf->context, idx,
2568 /* We don't really care about the result of this call */
2572 pa_operation_unref (o);
2574 pa_threaded_mainloop_unlock (mainloop);
2582 psink->mute_set = TRUE;
2584 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2590 psink->mute_set = TRUE;
2592 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2597 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2602 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2603 ("pa_stream_set_sink_input_mute() failed: %s",
2604 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2610 gst_pulsesink_sink_input_info_cb (pa_context * c, const pa_sink_input_info * i,
2611 int eol, void *userdata)
2613 GstPulseRingBuffer *pbuf;
2614 GstPulseSink *psink;
2616 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
2617 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
2625 /* If the index doesn't match our current stream,
2626 * it implies we just recreated the stream (caps change)
2628 if (i->index == pa_stream_get_index (pbuf->stream)) {
2629 psink->volume = pa_sw_volume_to_linear (pa_cvolume_max (&i->volume));
2630 psink->mute = i->mute;
2634 pa_threaded_mainloop_signal (mainloop, 0);
2638 gst_pulsesink_get_volume (GstPulseSink * psink)
2640 GstPulseRingBuffer *pbuf;
2641 pa_operation *o = NULL;
2642 gdouble v = DEFAULT_VOLUME;
2648 pa_threaded_mainloop_lock (mainloop);
2650 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2651 if (pbuf == NULL || pbuf->stream == NULL)
2654 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2657 if (!(o = pa_context_get_sink_input_info (pbuf->context, idx,
2658 gst_pulsesink_sink_input_info_cb, pbuf)))
2661 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2662 pa_threaded_mainloop_wait (mainloop);
2663 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
2671 pa_operation_unref (o);
2673 pa_threaded_mainloop_unlock (mainloop);
2675 if (v > MAX_VOLUME) {
2676 GST_WARNING_OBJECT (psink, "Clipped volume from %f to %f", v, MAX_VOLUME);
2686 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2691 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2696 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2701 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2702 ("pa_context_get_sink_input_info() failed: %s",
2703 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2709 gst_pulsesink_get_mute (GstPulseSink * psink)
2711 GstPulseRingBuffer *pbuf;
2712 pa_operation *o = NULL;
2714 gboolean mute = FALSE;
2719 pa_threaded_mainloop_lock (mainloop);
2722 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2723 if (pbuf == NULL || pbuf->stream == NULL)
2726 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2729 if (!(o = pa_context_get_sink_input_info (pbuf->context, idx,
2730 gst_pulsesink_sink_input_info_cb, pbuf)))
2733 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2734 pa_threaded_mainloop_wait (mainloop);
2735 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
2741 pa_operation_unref (o);
2743 pa_threaded_mainloop_unlock (mainloop);
2751 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2756 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2761 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2766 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2767 ("pa_context_get_sink_input_info() failed: %s",
2768 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2774 gst_pulsesink_device_description (GstPulseSink * psink)
2776 GstPulseRingBuffer *pbuf;
2777 pa_operation *o = NULL;
2783 pa_threaded_mainloop_lock (mainloop);
2784 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2788 free_device_info (&psink->device_info);
2789 if (!(o = pa_context_get_sink_info_by_name (pbuf->context,
2790 psink->device, gst_pulsesink_sink_info_cb, &psink->device_info)))
2793 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2794 pa_threaded_mainloop_wait (mainloop);
2795 if (gst_pulsering_is_dead (psink, pbuf, FALSE))
2801 pa_operation_unref (o);
2803 t = g_strdup (psink->device_info.description);
2804 pa_threaded_mainloop_unlock (mainloop);
2811 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2816 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2821 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2822 ("pa_context_get_sink_info_by_index() failed: %s",
2823 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2829 gst_pulsesink_set_property (GObject * object,
2830 guint prop_id, const GValue * value, GParamSpec * pspec)
2832 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2836 g_free (pulsesink->server);
2837 pulsesink->server = g_value_dup_string (value);
2838 if (pulsesink->probe)
2839 gst_pulseprobe_set_server (pulsesink->probe, pulsesink->server);
2842 g_free (pulsesink->device);
2843 pulsesink->device = g_value_dup_string (value);
2846 gst_pulsesink_set_volume (pulsesink, g_value_get_double (value));
2849 gst_pulsesink_set_mute (pulsesink, g_value_get_boolean (value));
2851 case PROP_CLIENT_NAME:
2852 g_free (pulsesink->client_name);
2853 if (!g_value_get_string (value)) {
2854 GST_WARNING_OBJECT (pulsesink,
2855 "Empty PulseAudio client name not allowed. Resetting to default value");
2856 pulsesink->client_name = gst_pulse_client_name ();
2858 pulsesink->client_name = g_value_dup_string (value);
2860 case PROP_STREAM_PROPERTIES:
2861 if (pulsesink->properties)
2862 gst_structure_free (pulsesink->properties);
2863 pulsesink->properties =
2864 gst_structure_copy (gst_value_get_structure (value));
2865 if (pulsesink->proplist)
2866 pa_proplist_free (pulsesink->proplist);
2867 pulsesink->proplist = gst_pulse_make_proplist (pulsesink->properties);
2870 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2876 gst_pulsesink_get_property (GObject * object,
2877 guint prop_id, GValue * value, GParamSpec * pspec)
2880 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2884 g_value_set_string (value, pulsesink->server);
2887 g_value_set_string (value, pulsesink->device);
2889 case PROP_DEVICE_NAME:
2890 g_value_take_string (value, gst_pulsesink_device_description (pulsesink));
2893 g_value_set_double (value, gst_pulsesink_get_volume (pulsesink));
2896 g_value_set_boolean (value, gst_pulsesink_get_mute (pulsesink));
2898 case PROP_CLIENT_NAME:
2899 g_value_set_string (value, pulsesink->client_name);
2901 case PROP_STREAM_PROPERTIES:
2902 gst_value_set_structure (value, pulsesink->properties);
2905 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2911 gst_pulsesink_change_title (GstPulseSink * psink, const gchar * t)
2913 pa_operation *o = NULL;
2914 GstPulseRingBuffer *pbuf;
2916 pa_threaded_mainloop_lock (mainloop);
2918 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2920 if (pbuf == NULL || pbuf->stream == NULL)
2923 g_free (pbuf->stream_name);
2924 pbuf->stream_name = g_strdup (t);
2926 if (!(o = pa_stream_set_name (pbuf->stream, pbuf->stream_name, NULL, NULL)))
2929 /* We're not interested if this operation failed or not */
2933 pa_operation_unref (o);
2934 pa_threaded_mainloop_unlock (mainloop);
2941 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2946 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2947 ("pa_stream_set_name() failed: %s",
2948 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2954 gst_pulsesink_change_props (GstPulseSink * psink, GstTagList * l)
2956 static const gchar *const map[] = {
2957 GST_TAG_TITLE, PA_PROP_MEDIA_TITLE,
2959 /* might get overriden in the next iteration by GST_TAG_ARTIST */
2960 GST_TAG_PERFORMER, PA_PROP_MEDIA_ARTIST,
2962 GST_TAG_ARTIST, PA_PROP_MEDIA_ARTIST,
2963 GST_TAG_LANGUAGE_CODE, PA_PROP_MEDIA_LANGUAGE,
2964 GST_TAG_LOCATION, PA_PROP_MEDIA_FILENAME,
2965 /* We might add more here later on ... */
2968 pa_proplist *pl = NULL;
2969 const gchar *const *t;
2970 gboolean empty = TRUE;
2971 pa_operation *o = NULL;
2972 GstPulseRingBuffer *pbuf;
2974 pl = pa_proplist_new ();
2976 for (t = map; *t; t += 2) {
2979 if (gst_tag_list_get_string (l, *t, &n)) {
2982 pa_proplist_sets (pl, *(t + 1), n);
2992 pa_threaded_mainloop_lock (mainloop);
2993 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2994 if (pbuf == NULL || pbuf->stream == NULL)
2997 /* We're not interested if this operation failed or not */
2998 if (!(o = pa_stream_proplist_update (pbuf->stream, PA_UPDATE_REPLACE,
3000 GST_DEBUG_OBJECT (psink, "pa_stream_proplist_update() failed");
3006 pa_operation_unref (o);
3008 pa_threaded_mainloop_unlock (mainloop);
3013 pa_proplist_free (pl);
3020 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
3026 gst_pulsesink_flush_ringbuffer (GstPulseSink * psink)
3028 GstPulseRingBuffer *pbuf;
3030 pa_threaded_mainloop_lock (mainloop);
3032 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
3034 if (pbuf == NULL || pbuf->stream == NULL)
3037 gst_pulsering_flush (pbuf);
3039 /* Uncork if we haven't already (happens when waiting to get enough data
3040 * to send out the first time) */
3042 gst_pulsering_set_corked (pbuf, FALSE, FALSE);
3044 /* We're not interested if this operation failed or not */
3046 pa_threaded_mainloop_unlock (mainloop);
3053 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
3059 gst_pulsesink_event (GstBaseSink * sink, GstEvent * event)
3061 GstPulseSink *pulsesink = GST_PULSESINK_CAST (sink);
3063 switch (GST_EVENT_TYPE (event)) {
3064 case GST_EVENT_TAG:{
3065 gchar *title = NULL, *artist = NULL, *location = NULL, *description =
3066 NULL, *t = NULL, *buf = NULL;
3069 gst_event_parse_tag (event, &l);
3071 gst_tag_list_get_string (l, GST_TAG_TITLE, &title);
3072 gst_tag_list_get_string (l, GST_TAG_ARTIST, &artist);
3073 gst_tag_list_get_string (l, GST_TAG_LOCATION, &location);
3074 gst_tag_list_get_string (l, GST_TAG_DESCRIPTION, &description);
3077 gst_tag_list_get_string (l, GST_TAG_PERFORMER, &artist);
3079 if (title && artist)
3080 /* TRANSLATORS: 'song title' by 'artist name' */
3081 t = buf = g_strdup_printf (_("'%s' by '%s'"), g_strstrip (title),
3082 g_strstrip (artist));
3084 t = g_strstrip (title);
3085 else if (description)
3086 t = g_strstrip (description);
3088 t = g_strstrip (location);
3091 gst_pulsesink_change_title (pulsesink, t);
3096 g_free (description);
3099 gst_pulsesink_change_props (pulsesink, l);
3103 case GST_EVENT_GAP:{
3104 GstClockTime timestamp, duration;
3106 gst_event_parse_gap (event, ×tamp, &duration);
3107 if (duration == GST_CLOCK_TIME_NONE)
3108 gst_pulsesink_flush_ringbuffer (pulsesink);
3112 gst_pulsesink_flush_ringbuffer (pulsesink);
3118 return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
3122 gst_pulsesink_query (GstBaseSink * sink, GstQuery * query)
3124 GstPulseSink *pulsesink = GST_PULSESINK_CAST (sink);
3127 switch (GST_QUERY_TYPE (query)) {
3128 case GST_QUERY_CAPS:
3130 GstCaps *caps, *filter;
3132 gst_query_parse_caps (query, &filter);
3133 caps = gst_pulsesink_query_getcaps (pulsesink, filter);
3136 gst_query_set_caps_result (query, caps);
3137 gst_caps_unref (caps);
3143 case GST_QUERY_ACCEPT_CAPS:
3147 gst_query_parse_accept_caps (query, &caps);
3148 ret = gst_pulsesink_query_acceptcaps (pulsesink, caps);
3149 gst_query_set_accept_caps_result (query, ret);
3154 ret = GST_BASE_SINK_CLASS (parent_class)->query (sink, query);
3161 gst_pulsesink_release_mainloop (GstPulseSink * psink)
3166 pa_threaded_mainloop_lock (mainloop);
3167 while (psink->defer_pending) {
3168 GST_DEBUG_OBJECT (psink, "waiting for stream status message emission");
3169 pa_threaded_mainloop_wait (mainloop);
3171 pa_threaded_mainloop_unlock (mainloop);
3173 g_mutex_lock (&pa_shared_resource_mutex);
3175 if (!mainloop_ref_ct) {
3176 GST_INFO_OBJECT (psink, "terminating pa main loop thread");
3177 pa_threaded_mainloop_stop (mainloop);
3178 pa_threaded_mainloop_free (mainloop);
3181 g_mutex_unlock (&pa_shared_resource_mutex);
3184 static GstStateChangeReturn
3185 gst_pulsesink_change_state (GstElement * element, GstStateChange transition)
3187 GstPulseSink *pulsesink = GST_PULSESINK (element);
3188 GstStateChangeReturn ret;
3190 switch (transition) {
3191 case GST_STATE_CHANGE_NULL_TO_READY:
3192 g_mutex_lock (&pa_shared_resource_mutex);
3193 if (!mainloop_ref_ct) {
3194 GST_INFO_OBJECT (element, "new pa main loop thread");
3195 if (!(mainloop = pa_threaded_mainloop_new ()))
3196 goto mainloop_failed;
3197 if (pa_threaded_mainloop_start (mainloop) < 0) {
3198 pa_threaded_mainloop_free (mainloop);
3199 goto mainloop_start_failed;
3201 mainloop_ref_ct = 1;
3202 g_mutex_unlock (&pa_shared_resource_mutex);
3204 GST_INFO_OBJECT (element, "reusing pa main loop thread");
3206 g_mutex_unlock (&pa_shared_resource_mutex);
3209 case GST_STATE_CHANGE_READY_TO_PAUSED:
3210 gst_element_post_message (element,
3211 gst_message_new_clock_provide (GST_OBJECT_CAST (element),
3212 GST_AUDIO_BASE_SINK (pulsesink)->provided_clock, TRUE));
3219 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3220 if (ret == GST_STATE_CHANGE_FAILURE)
3223 switch (transition) {
3224 case GST_STATE_CHANGE_PAUSED_TO_READY:
3225 /* format_lost is reset in release() in audiobasesink */
3226 gst_element_post_message (element,
3227 gst_message_new_clock_lost (GST_OBJECT_CAST (element),
3228 GST_AUDIO_BASE_SINK (pulsesink)->provided_clock));
3230 case GST_STATE_CHANGE_READY_TO_NULL:
3231 gst_pulsesink_release_mainloop (pulsesink);
3242 g_mutex_unlock (&pa_shared_resource_mutex);
3243 GST_ELEMENT_ERROR (pulsesink, RESOURCE, FAILED,
3244 ("pa_threaded_mainloop_new() failed"), (NULL));
3245 return GST_STATE_CHANGE_FAILURE;
3247 mainloop_start_failed:
3249 g_mutex_unlock (&pa_shared_resource_mutex);
3250 GST_ELEMENT_ERROR (pulsesink, RESOURCE, FAILED,
3251 ("pa_threaded_mainloop_start() failed"), (NULL));
3252 return GST_STATE_CHANGE_FAILURE;
3256 if (transition == GST_STATE_CHANGE_NULL_TO_READY) {
3257 /* Clear the PA mainloop if audiobasesink failed to open the ring_buffer */
3258 g_assert (mainloop);
3259 gst_pulsesink_release_mainloop (pulsesink);