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_CURRENT_DEVICE NULL
71 #define DEFAULT_DEVICE_NAME NULL
72 #define DEFAULT_VOLUME 1.0
73 #define DEFAULT_MUTE FALSE
74 #define MAX_VOLUME 10.0
86 PROP_STREAM_PROPERTIES,
90 #define GST_TYPE_PULSERING_BUFFER \
91 (gst_pulseringbuffer_get_type())
92 #define GST_PULSERING_BUFFER(obj) \
93 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_PULSERING_BUFFER,GstPulseRingBuffer))
94 #define GST_PULSERING_BUFFER_CLASS(klass) \
95 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_PULSERING_BUFFER,GstPulseRingBufferClass))
96 #define GST_PULSERING_BUFFER_GET_CLASS(obj) \
97 (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_PULSERING_BUFFER, GstPulseRingBufferClass))
98 #define GST_PULSERING_BUFFER_CAST(obj) \
99 ((GstPulseRingBuffer *)obj)
100 #define GST_IS_PULSERING_BUFFER(obj) \
101 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_PULSERING_BUFFER))
102 #define GST_IS_PULSERING_BUFFER_CLASS(klass)\
103 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_PULSERING_BUFFER))
105 typedef struct _GstPulseRingBuffer GstPulseRingBuffer;
106 typedef struct _GstPulseRingBufferClass GstPulseRingBufferClass;
108 typedef struct _GstPulseContext GstPulseContext;
110 /* A note on threading.
112 * We use a pa_threaded_mainloop to interact with the PulseAudio server. This
113 * starts up a separate thread that runs a mainloop to carry back events,
114 * messages and timing updates from the PulseAudio server.
116 * In most cases, the PulseAudio API we use communicates with the server and
117 * processes replies asynchronously. Operations on PA objects that result in
118 * such communication are protected with a pa_threaded_mainloop_lock() and
119 * pa_threaded_mainloop_unlock(). These guarantee mutual exclusion with the
120 * mainloop thread -- when an iteration of the mainloop thread begins, it first
121 * tries to acquire this lock, and cannot do so if our code also holds that
124 * When we need to complete an operation synchronously, we use
125 * pa_threaded_mainloop_wait() and pa_threaded_mainloop_signal(). These work
126 * much as pthread conditionals do. pa_threaded_mainloop_wait() is called with
127 * the mainloop lock held. It releases the lock (thereby allowing the mainloop
128 * to execute), and waits till one of our callbacks to be executed by the
129 * mainloop thread calls pa_threaded_mainloop_signal(). At the end of the
130 * mainloop iteration, the pa_threaded_mainloop_wait() will reacquire the
131 * mainloop lock and return control to the caller.
134 /* Store the PA contexts in a hash table to allow easy sharing among
135 * multiple instances of the sink. Keys are $context_name@$server_name
136 * (strings) and values should be GstPulseContext pointers.
138 struct _GstPulseContext
141 GSList *ring_buffers;
144 static GHashTable *gst_pulse_shared_contexts = NULL;
146 /* use one static main-loop for all instances
147 * this is needed to make the context sharing work as the contexts are
148 * released when releasing their parent main-loop
150 static pa_threaded_mainloop *mainloop = NULL;
151 static guint mainloop_ref_ct = 0;
153 /* lock for access to shared resources */
154 static GMutex pa_shared_resource_mutex;
156 /* We keep a custom ringbuffer that is backed up by data allocated by
157 * pulseaudio. We must also overide the commit function to write into
158 * pulseaudio memory instead. */
159 struct _GstPulseRingBuffer
161 GstAudioRingBuffer object;
168 pa_stream *probe_stream;
170 pa_format_info *format;
181 gboolean in_commit:1;
184 struct _GstPulseRingBufferClass
186 GstAudioRingBufferClass parent_class;
189 static GType gst_pulseringbuffer_get_type (void);
190 static void gst_pulseringbuffer_finalize (GObject * object);
192 static GstAudioRingBufferClass *ring_parent_class = NULL;
194 static gboolean gst_pulseringbuffer_open_device (GstAudioRingBuffer * buf);
195 static gboolean gst_pulseringbuffer_close_device (GstAudioRingBuffer * buf);
196 static gboolean gst_pulseringbuffer_acquire (GstAudioRingBuffer * buf,
197 GstAudioRingBufferSpec * spec);
198 static gboolean gst_pulseringbuffer_release (GstAudioRingBuffer * buf);
199 static gboolean gst_pulseringbuffer_start (GstAudioRingBuffer * buf);
200 static gboolean gst_pulseringbuffer_pause (GstAudioRingBuffer * buf);
201 static gboolean gst_pulseringbuffer_stop (GstAudioRingBuffer * buf);
202 static void gst_pulseringbuffer_clear (GstAudioRingBuffer * buf);
203 static guint gst_pulseringbuffer_commit (GstAudioRingBuffer * buf,
204 guint64 * sample, guchar * data, gint in_samples, gint out_samples,
207 G_DEFINE_TYPE (GstPulseRingBuffer, gst_pulseringbuffer,
208 GST_TYPE_AUDIO_RING_BUFFER);
211 gst_pulsesink_init_contexts (void)
213 g_mutex_init (&pa_shared_resource_mutex);
214 gst_pulse_shared_contexts = g_hash_table_new_full (g_str_hash, g_str_equal,
219 gst_pulseringbuffer_class_init (GstPulseRingBufferClass * klass)
221 GObjectClass *gobject_class;
222 GstAudioRingBufferClass *gstringbuffer_class;
224 gobject_class = (GObjectClass *) klass;
225 gstringbuffer_class = (GstAudioRingBufferClass *) klass;
227 ring_parent_class = g_type_class_peek_parent (klass);
229 gobject_class->finalize = gst_pulseringbuffer_finalize;
231 gstringbuffer_class->open_device =
232 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_open_device);
233 gstringbuffer_class->close_device =
234 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_close_device);
235 gstringbuffer_class->acquire =
236 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_acquire);
237 gstringbuffer_class->release =
238 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_release);
239 gstringbuffer_class->start = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_start);
240 gstringbuffer_class->pause = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_pause);
241 gstringbuffer_class->resume = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_start);
242 gstringbuffer_class->stop = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_stop);
243 gstringbuffer_class->clear_all =
244 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_clear);
246 gstringbuffer_class->commit = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_commit);
250 gst_pulseringbuffer_init (GstPulseRingBuffer * pbuf)
252 pbuf->stream_name = NULL;
253 pbuf->context = NULL;
255 pbuf->probe_stream = NULL;
259 pbuf->is_pcm = FALSE;
263 pbuf->m_writable = 0;
265 pbuf->m_lastoffset = 0;
268 pbuf->in_commit = FALSE;
269 pbuf->paused = FALSE;
272 /* Call with mainloop lock held if wait == TRUE) */
274 gst_pulse_destroy_stream (pa_stream * stream, gboolean wait)
276 /* Make sure we don't get any further callbacks */
277 pa_stream_set_write_callback (stream, NULL, NULL);
278 pa_stream_set_underflow_callback (stream, NULL, NULL);
279 pa_stream_set_overflow_callback (stream, NULL, NULL);
281 pa_stream_disconnect (stream);
284 pa_threaded_mainloop_wait (mainloop);
286 pa_stream_set_state_callback (stream, NULL, NULL);
287 pa_stream_unref (stream);
291 gst_pulsering_destroy_stream (GstPulseRingBuffer * pbuf)
293 if (pbuf->probe_stream) {
294 gst_pulse_destroy_stream (pbuf->probe_stream, FALSE);
295 pbuf->probe_stream = NULL;
301 /* drop shm memory buffer */
302 pa_stream_cancel_write (pbuf->stream);
304 /* reset internal variables */
307 pbuf->m_writable = 0;
309 pbuf->m_lastoffset = 0;
312 pa_format_info_free (pbuf->format);
315 pbuf->is_pcm = FALSE;
318 pa_stream_disconnect (pbuf->stream);
320 /* Make sure we don't get any further callbacks */
321 pa_stream_set_state_callback (pbuf->stream, NULL, NULL);
322 pa_stream_set_write_callback (pbuf->stream, NULL, NULL);
323 pa_stream_set_underflow_callback (pbuf->stream, NULL, NULL);
324 pa_stream_set_overflow_callback (pbuf->stream, NULL, NULL);
326 pa_stream_unref (pbuf->stream);
330 g_free (pbuf->stream_name);
331 pbuf->stream_name = NULL;
335 gst_pulsering_destroy_context (GstPulseRingBuffer * pbuf)
337 g_mutex_lock (&pa_shared_resource_mutex);
339 GST_DEBUG_OBJECT (pbuf, "destroying ringbuffer %p", pbuf);
341 gst_pulsering_destroy_stream (pbuf);
344 pa_context_unref (pbuf->context);
345 pbuf->context = NULL;
348 if (pbuf->context_name) {
349 GstPulseContext *pctx;
351 pctx = g_hash_table_lookup (gst_pulse_shared_contexts, pbuf->context_name);
353 GST_DEBUG_OBJECT (pbuf, "releasing context with name %s, pbuf=%p, pctx=%p",
354 pbuf->context_name, pbuf, pctx);
357 pctx->ring_buffers = g_slist_remove (pctx->ring_buffers, pbuf);
358 if (pctx->ring_buffers == NULL) {
359 GST_DEBUG_OBJECT (pbuf,
360 "destroying final context with name %s, pbuf=%p, pctx=%p",
361 pbuf->context_name, pbuf, pctx);
363 pa_context_disconnect (pctx->context);
365 /* Make sure we don't get any further callbacks */
366 pa_context_set_state_callback (pctx->context, NULL, NULL);
367 pa_context_set_subscribe_callback (pctx->context, NULL, NULL);
369 g_hash_table_remove (gst_pulse_shared_contexts, pbuf->context_name);
371 pa_context_unref (pctx->context);
372 g_slice_free (GstPulseContext, pctx);
375 g_free (pbuf->context_name);
376 pbuf->context_name = NULL;
378 g_mutex_unlock (&pa_shared_resource_mutex);
382 gst_pulseringbuffer_finalize (GObject * object)
384 GstPulseRingBuffer *ringbuffer;
386 ringbuffer = GST_PULSERING_BUFFER_CAST (object);
388 gst_pulsering_destroy_context (ringbuffer);
389 G_OBJECT_CLASS (ring_parent_class)->finalize (object);
393 #define CONTEXT_OK(c) ((c) && PA_CONTEXT_IS_GOOD (pa_context_get_state ((c))))
394 #define STREAM_OK(s) ((s) && PA_STREAM_IS_GOOD (pa_stream_get_state ((s))))
397 gst_pulsering_is_dead (GstPulseSink * psink, GstPulseRingBuffer * pbuf,
398 gboolean check_stream)
400 if (!CONTEXT_OK (pbuf->context))
403 if (check_stream && !STREAM_OK (pbuf->stream))
410 const gchar *err_str =
411 pbuf->context ? pa_strerror (pa_context_errno (pbuf->context)) : NULL;
412 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED, ("Disconnected: %s",
419 gst_pulsering_context_state_cb (pa_context * c, void *userdata)
421 pa_context_state_t state;
422 pa_threaded_mainloop *mainloop = (pa_threaded_mainloop *) userdata;
424 state = pa_context_get_state (c);
426 GST_LOG ("got new context state %d", state);
429 case PA_CONTEXT_READY:
430 case PA_CONTEXT_TERMINATED:
431 case PA_CONTEXT_FAILED:
432 GST_LOG ("signaling");
433 pa_threaded_mainloop_signal (mainloop, 0);
436 case PA_CONTEXT_UNCONNECTED:
437 case PA_CONTEXT_CONNECTING:
438 case PA_CONTEXT_AUTHORIZING:
439 case PA_CONTEXT_SETTING_NAME:
445 gst_pulsering_context_subscribe_cb (pa_context * c,
446 pa_subscription_event_type_t t, uint32_t idx, void *userdata)
449 GstPulseContext *pctx = (GstPulseContext *) userdata;
452 if (t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT | PA_SUBSCRIPTION_EVENT_CHANGE) &&
453 t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT | PA_SUBSCRIPTION_EVENT_NEW))
456 for (walk = pctx->ring_buffers; walk; walk = g_slist_next (walk)) {
457 GstPulseRingBuffer *pbuf = (GstPulseRingBuffer *) walk->data;
458 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
460 GST_LOG_OBJECT (psink, "type %04x, idx %u", t, idx);
465 if (idx != pa_stream_get_index (pbuf->stream))
468 if (psink->device && pbuf->is_pcm &&
469 !g_str_equal (psink->device,
470 pa_stream_get_device_name (pbuf->stream))) {
471 /* Underlying sink changed. And this is not a passthrough stream. Let's
472 * see if someone upstream wants to try to renegotiate. */
475 g_free (psink->device);
476 psink->device = g_strdup (pa_stream_get_device_name (pbuf->stream));
478 GST_INFO_OBJECT (psink, "emitting sink-changed");
480 /* FIXME: send reconfigure event instead and let decodebin/playbin
481 * handle that. Also take care of ac3 alignment. See "pulse-format-lost" */
482 renego = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
483 gst_structure_new_empty ("pulse-sink-changed"));
485 if (!gst_pad_push_event (GST_BASE_SINK (psink)->sinkpad, renego))
486 GST_DEBUG_OBJECT (psink, "Emitted sink-changed - nobody was listening");
489 /* Actually this event is also triggered when other properties of
490 * the stream change that are unrelated to the volume. However it is
491 * probably cheaper to signal the change here and check for the
492 * volume when the GObject property is read instead of querying it always. */
494 /* inform streaming thread to notify */
495 g_atomic_int_compare_and_exchange (&psink->notify, 0, 1);
499 /* will be called when the device should be opened. In this case we will connect
500 * to the server. We should not try to open any streams in this state. */
502 gst_pulseringbuffer_open_device (GstAudioRingBuffer * buf)
505 GstPulseRingBuffer *pbuf;
506 GstPulseContext *pctx;
507 pa_mainloop_api *api;
508 gboolean need_unlock_shared;
510 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
511 pbuf = GST_PULSERING_BUFFER_CAST (buf);
513 g_assert (!pbuf->stream);
514 g_assert (psink->client_name);
517 pbuf->context_name = g_strdup_printf ("%s@%s", psink->client_name,
520 pbuf->context_name = g_strdup (psink->client_name);
522 pa_threaded_mainloop_lock (mainloop);
524 g_mutex_lock (&pa_shared_resource_mutex);
525 need_unlock_shared = TRUE;
527 pctx = g_hash_table_lookup (gst_pulse_shared_contexts, pbuf->context_name);
529 pctx = g_slice_new0 (GstPulseContext);
531 /* get the mainloop api and create a context */
532 GST_INFO_OBJECT (psink, "new context with name %s, pbuf=%p, pctx=%p",
533 pbuf->context_name, pbuf, pctx);
534 api = pa_threaded_mainloop_get_api (mainloop);
535 if (!(pctx->context = pa_context_new (api, pbuf->context_name)))
538 pctx->ring_buffers = g_slist_prepend (pctx->ring_buffers, pbuf);
539 g_hash_table_insert (gst_pulse_shared_contexts,
540 g_strdup (pbuf->context_name), (gpointer) pctx);
541 /* register some essential callbacks */
542 pa_context_set_state_callback (pctx->context,
543 gst_pulsering_context_state_cb, mainloop);
544 pa_context_set_subscribe_callback (pctx->context,
545 gst_pulsering_context_subscribe_cb, pctx);
547 /* try to connect to the server and wait for completion, we don't want to
548 * autospawn a deamon */
549 GST_LOG_OBJECT (psink, "connect to server %s",
550 GST_STR_NULL (psink->server));
551 if (pa_context_connect (pctx->context, psink->server,
552 PA_CONTEXT_NOAUTOSPAWN, NULL) < 0)
555 GST_INFO_OBJECT (psink,
556 "reusing shared context with name %s, pbuf=%p, pctx=%p",
557 pbuf->context_name, pbuf, pctx);
558 pctx->ring_buffers = g_slist_prepend (pctx->ring_buffers, pbuf);
561 g_mutex_unlock (&pa_shared_resource_mutex);
562 need_unlock_shared = FALSE;
564 /* context created or shared okay */
565 pbuf->context = pa_context_ref (pctx->context);
568 pa_context_state_t state;
570 state = pa_context_get_state (pbuf->context);
572 GST_LOG_OBJECT (psink, "context state is now %d", state);
574 if (!PA_CONTEXT_IS_GOOD (state))
577 if (state == PA_CONTEXT_READY)
580 /* Wait until the context is ready */
581 GST_LOG_OBJECT (psink, "waiting..");
582 pa_threaded_mainloop_wait (mainloop);
585 if (pa_context_get_server_protocol_version (pbuf->context) < 22) {
586 /* We need PulseAudio >= 1.0 on the server side for the extended API */
587 goto bad_server_version;
590 GST_LOG_OBJECT (psink, "opened the device");
592 pa_threaded_mainloop_unlock (mainloop);
599 if (need_unlock_shared)
600 g_mutex_unlock (&pa_shared_resource_mutex);
601 gst_pulsering_destroy_context (pbuf);
602 pa_threaded_mainloop_unlock (mainloop);
607 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
608 ("Failed to create context"), (NULL));
609 g_slice_free (GstPulseContext, pctx);
610 goto unlock_and_fail;
614 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED, ("Failed to connect: %s",
615 pa_strerror (pa_context_errno (pctx->context))), (NULL));
616 goto unlock_and_fail;
620 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED, ("PulseAudio server version "
621 "is too old."), (NULL));
622 goto unlock_and_fail;
626 /* close the device */
628 gst_pulseringbuffer_close_device (GstAudioRingBuffer * buf)
631 GstPulseRingBuffer *pbuf;
633 pbuf = GST_PULSERING_BUFFER_CAST (buf);
634 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
636 GST_LOG_OBJECT (psink, "closing device");
638 pa_threaded_mainloop_lock (mainloop);
639 gst_pulsering_destroy_context (pbuf);
640 pa_threaded_mainloop_unlock (mainloop);
642 GST_LOG_OBJECT (psink, "closed device");
648 gst_pulsering_stream_state_cb (pa_stream * s, void *userdata)
651 GstPulseRingBuffer *pbuf;
652 pa_stream_state_t state;
654 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
655 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
657 state = pa_stream_get_state (s);
658 GST_LOG_OBJECT (psink, "got new stream state %d", state);
661 case PA_STREAM_READY:
662 case PA_STREAM_FAILED:
663 case PA_STREAM_TERMINATED:
664 GST_LOG_OBJECT (psink, "signaling");
665 pa_threaded_mainloop_signal (mainloop, 0);
667 case PA_STREAM_UNCONNECTED:
668 case PA_STREAM_CREATING:
674 gst_pulsering_stream_request_cb (pa_stream * s, size_t length, void *userdata)
677 GstAudioRingBuffer *rbuf;
678 GstPulseRingBuffer *pbuf;
680 rbuf = GST_AUDIO_RING_BUFFER_CAST (userdata);
681 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
682 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
684 GST_LOG_OBJECT (psink, "got request for length %" G_GSIZE_FORMAT, length);
686 if (pbuf->in_commit && (length >= rbuf->spec.segsize)) {
687 /* only signal when we are waiting in the commit thread
688 * and got request for atleast a segment */
689 pa_threaded_mainloop_signal (mainloop, 0);
694 gst_pulsering_stream_underflow_cb (pa_stream * s, void *userdata)
697 GstPulseRingBuffer *pbuf;
699 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
700 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
702 GST_WARNING_OBJECT (psink, "Got underflow");
706 gst_pulsering_stream_overflow_cb (pa_stream * s, void *userdata)
709 GstPulseRingBuffer *pbuf;
711 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
712 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
714 GST_WARNING_OBJECT (psink, "Got overflow");
718 gst_pulsering_stream_latency_cb (pa_stream * s, void *userdata)
721 GstPulseRingBuffer *pbuf;
722 GstAudioRingBuffer *ringbuf;
723 const pa_timing_info *info;
726 info = pa_stream_get_timing_info (s);
728 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
729 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
730 ringbuf = GST_AUDIO_RING_BUFFER (pbuf);
733 GST_LOG_OBJECT (psink, "latency update (information unknown)");
737 if (!info->read_index_corrupt) {
738 /* Update segdone based on the read index. segdone is of segment
739 * granularity, while the read index is at byte granularity. We take the
740 * ceiling while converting the latter to the former since it is more
741 * conservative to report that we've read more than we have than to report
742 * less. One concern here is that latency updates happen every 100ms, which
743 * means segdone is not updated very often, but increasing the update
744 * frequency would mean more communication overhead. */
745 g_atomic_int_set (&ringbuf->segdone,
746 (int) gst_util_uint64_scale_ceil (info->read_index, 1,
747 ringbuf->spec.segsize));
750 sink_usec = info->configured_sink_usec;
752 GST_LOG_OBJECT (psink,
753 "latency_update, %" G_GUINT64_FORMAT ", %d:%" G_GINT64_FORMAT ", %d:%"
754 G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT,
755 GST_TIMEVAL_TO_TIME (info->timestamp), info->write_index_corrupt,
756 info->write_index, info->read_index_corrupt, info->read_index,
757 info->sink_usec, sink_usec);
761 gst_pulsering_stream_suspended_cb (pa_stream * p, void *userdata)
764 GstPulseRingBuffer *pbuf;
766 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
767 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
769 if (pa_stream_is_suspended (p))
770 GST_DEBUG_OBJECT (psink, "stream suspended");
772 GST_DEBUG_OBJECT (psink, "stream resumed");
776 gst_pulsering_stream_started_cb (pa_stream * p, void *userdata)
779 GstPulseRingBuffer *pbuf;
781 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
782 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
784 GST_DEBUG_OBJECT (psink, "stream started");
788 gst_pulsering_stream_event_cb (pa_stream * p, const char *name,
789 pa_proplist * pl, void *userdata)
792 GstPulseRingBuffer *pbuf;
794 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
795 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
797 if (!strcmp (name, PA_STREAM_EVENT_REQUEST_CORK)) {
798 /* the stream wants to PAUSE, post a message for the application. */
799 GST_DEBUG_OBJECT (psink, "got request for CORK");
800 gst_element_post_message (GST_ELEMENT_CAST (psink),
801 gst_message_new_request_state (GST_OBJECT_CAST (psink),
804 } else if (!strcmp (name, PA_STREAM_EVENT_REQUEST_UNCORK)) {
805 GST_DEBUG_OBJECT (psink, "got request for UNCORK");
806 gst_element_post_message (GST_ELEMENT_CAST (psink),
807 gst_message_new_request_state (GST_OBJECT_CAST (psink),
809 } else if (!strcmp (name, PA_STREAM_EVENT_FORMAT_LOST)) {
812 if (g_atomic_int_get (&psink->format_lost)) {
813 /* Duplicate event before we're done reconfiguring, discard */
817 GST_DEBUG_OBJECT (psink, "got FORMAT LOST");
818 g_atomic_int_set (&psink->format_lost, 1);
819 psink->format_lost_time = g_ascii_strtoull (pa_proplist_gets (pl,
820 "stream-time"), NULL, 0) * 1000;
822 g_free (psink->device);
823 psink->device = g_strdup (pa_proplist_gets (pl, "device"));
825 /* FIXME: send reconfigure event instead and let decodebin/playbin
826 * handle that. Also take care of ac3 alignment */
827 renego = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
828 gst_structure_new_empty ("pulse-format-lost"));
831 if (g_str_equal (gst_structure_get_name (st), "audio/x-eac3")) {
832 GstStructure *event_st = gst_structure_new ("ac3parse-set-alignment",
833 "alignment", G_TYPE_STRING, pbin->dbin ? "frame" : "iec61937", NULL);
835 if (!gst_pad_push_event (pbin->sinkpad,
836 gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, event_st)))
837 GST_WARNING_OBJECT (pbin->sinkpad, "Could not update alignment");
841 if (!gst_pad_push_event (GST_BASE_SINK (psink)->sinkpad, renego)) {
842 /* Nobody handled the format change - emit an error */
843 GST_ELEMENT_ERROR (psink, STREAM, FORMAT, ("Sink format changed"),
844 ("Sink format changed"));
847 GST_DEBUG_OBJECT (psink, "got unknown event %s", name);
851 /* Called with the mainloop locked */
853 gst_pulsering_wait_for_stream_ready (GstPulseSink * psink, pa_stream * stream)
855 pa_stream_state_t state;
858 state = pa_stream_get_state (stream);
860 GST_LOG_OBJECT (psink, "stream state is now %d", state);
862 if (!PA_STREAM_IS_GOOD (state))
865 if (state == PA_STREAM_READY)
868 /* Wait until the stream is ready */
869 pa_threaded_mainloop_wait (mainloop);
874 /* This method should create a new stream of the given @spec. No playback should
875 * start yet so we start in the corked state. */
877 gst_pulseringbuffer_acquire (GstAudioRingBuffer * buf,
878 GstAudioRingBufferSpec * spec)
881 GstPulseRingBuffer *pbuf;
882 pa_buffer_attr wanted;
883 const pa_buffer_attr *actual;
884 pa_channel_map channel_map;
885 pa_operation *o = NULL;
887 pa_cvolume *pv = NULL;
888 pa_stream_flags_t flags;
890 GstAudioClock *clock;
891 pa_format_info *formats[1];
892 #ifndef GST_DISABLE_GST_DEBUG
893 gchar print_buf[PA_FORMAT_INFO_SNPRINT_MAX];
896 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
897 pbuf = GST_PULSERING_BUFFER_CAST (buf);
899 GST_LOG_OBJECT (psink, "creating sample spec");
900 /* convert the gstreamer sample spec to the pulseaudio format */
901 if (!gst_pulse_fill_format_info (spec, &pbuf->format, &pbuf->channels))
903 pbuf->is_pcm = pa_format_info_is_pcm (pbuf->format);
905 pa_threaded_mainloop_lock (mainloop);
907 /* we need a context and a no stream */
908 g_assert (pbuf->context);
909 g_assert (!pbuf->stream);
911 /* if we have a probe, disconnect it first so that if we're creating a
912 * compressed stream, it doesn't get blocked by a PCM stream */
913 if (pbuf->probe_stream) {
914 gst_pulse_destroy_stream (pbuf->probe_stream, TRUE);
915 pbuf->probe_stream = NULL;
918 /* enable event notifications */
919 GST_LOG_OBJECT (psink, "subscribing to context events");
920 if (!(o = pa_context_subscribe (pbuf->context,
921 PA_SUBSCRIPTION_MASK_SINK_INPUT, NULL, NULL)))
922 goto subscribe_failed;
924 pa_operation_unref (o);
926 /* initialize the channel map */
927 if (pbuf->is_pcm && gst_pulse_gst_to_channel_map (&channel_map, spec))
928 pa_format_info_set_channel_map (pbuf->format, &channel_map);
930 /* find a good name for the stream */
931 if (psink->stream_name)
932 name = psink->stream_name;
934 name = "Playback Stream";
936 /* create a stream */
937 formats[0] = pbuf->format;
938 if (!(pbuf->stream = pa_stream_new_extended (pbuf->context, name, formats, 1,
942 /* install essential callbacks */
943 pa_stream_set_state_callback (pbuf->stream,
944 gst_pulsering_stream_state_cb, pbuf);
945 pa_stream_set_write_callback (pbuf->stream,
946 gst_pulsering_stream_request_cb, pbuf);
947 pa_stream_set_underflow_callback (pbuf->stream,
948 gst_pulsering_stream_underflow_cb, pbuf);
949 pa_stream_set_overflow_callback (pbuf->stream,
950 gst_pulsering_stream_overflow_cb, pbuf);
951 pa_stream_set_latency_update_callback (pbuf->stream,
952 gst_pulsering_stream_latency_cb, pbuf);
953 pa_stream_set_suspended_callback (pbuf->stream,
954 gst_pulsering_stream_suspended_cb, pbuf);
955 pa_stream_set_started_callback (pbuf->stream,
956 gst_pulsering_stream_started_cb, pbuf);
957 pa_stream_set_event_callback (pbuf->stream,
958 gst_pulsering_stream_event_cb, pbuf);
960 /* buffering requirements. When setting prebuf to 0, the stream will not pause
961 * when we cause an underrun, which causes time to continue. */
962 memset (&wanted, 0, sizeof (wanted));
963 wanted.tlength = spec->segtotal * spec->segsize;
964 wanted.maxlength = -1;
966 wanted.minreq = spec->segsize;
968 GST_INFO_OBJECT (psink, "tlength: %d", wanted.tlength);
969 GST_INFO_OBJECT (psink, "maxlength: %d", wanted.maxlength);
970 GST_INFO_OBJECT (psink, "prebuf: %d", wanted.prebuf);
971 GST_INFO_OBJECT (psink, "minreq: %d", wanted.minreq);
973 /* configure volume when we changed it, else we leave the default */
974 if (psink->volume_set) {
975 GST_LOG_OBJECT (psink, "have volume of %f", psink->volume);
978 gst_pulse_cvolume_from_linear (pv, pbuf->channels, psink->volume);
980 GST_DEBUG_OBJECT (psink, "passthrough stream, not setting volume");
987 /* construct the flags */
988 flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE |
989 PA_STREAM_ADJUST_LATENCY | PA_STREAM_START_CORKED;
991 if (psink->mute_set) {
993 flags |= PA_STREAM_START_MUTED;
995 flags |= PA_STREAM_START_UNMUTED;
998 /* we always start corked (see flags above) */
1001 /* try to connect now */
1002 GST_LOG_OBJECT (psink, "connect for playback to device %s",
1003 GST_STR_NULL (psink->device));
1004 if (pa_stream_connect_playback (pbuf->stream, psink->device,
1005 &wanted, flags, pv, NULL) < 0)
1006 goto connect_failed;
1008 /* our clock will now start from 0 again */
1009 clock = GST_AUDIO_CLOCK (GST_AUDIO_BASE_SINK (psink)->provided_clock);
1010 gst_audio_clock_reset (clock, 0);
1012 if (!gst_pulsering_wait_for_stream_ready (psink, pbuf->stream))
1013 goto connect_failed;
1015 g_free (psink->device);
1016 psink->device = g_strdup (pa_stream_get_device_name (pbuf->stream));
1018 #ifndef GST_DISABLE_GST_DEBUG
1019 pa_format_info_snprint (print_buf, sizeof (print_buf),
1020 pa_stream_get_format_info (pbuf->stream));
1021 GST_INFO_OBJECT (psink, "negotiated to: %s", print_buf);
1024 /* After we passed the volume off of to PA we never want to set it
1025 again, since it is PA's job to save/restore volumes. */
1026 psink->volume_set = psink->mute_set = FALSE;
1028 GST_LOG_OBJECT (psink, "stream is acquired now");
1030 /* get the actual buffering properties now */
1031 actual = pa_stream_get_buffer_attr (pbuf->stream);
1033 GST_INFO_OBJECT (psink, "tlength: %d (wanted: %d)", actual->tlength,
1035 GST_INFO_OBJECT (psink, "maxlength: %d", actual->maxlength);
1036 GST_INFO_OBJECT (psink, "prebuf: %d", actual->prebuf);
1037 GST_INFO_OBJECT (psink, "minreq: %d (wanted %d)", actual->minreq,
1040 spec->segsize = actual->minreq;
1041 spec->segtotal = actual->tlength / spec->segsize;
1043 pa_threaded_mainloop_unlock (mainloop);
1050 gst_pulsering_destroy_stream (pbuf);
1051 pa_threaded_mainloop_unlock (mainloop);
1057 GST_ELEMENT_ERROR (psink, RESOURCE, SETTINGS,
1058 ("Invalid sample specification."), (NULL));
1063 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1064 ("pa_context_subscribe() failed: %s",
1065 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1066 goto unlock_and_fail;
1070 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1071 ("Failed to create stream: %s",
1072 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1073 goto unlock_and_fail;
1077 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1078 ("Failed to connect stream: %s",
1079 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1080 goto unlock_and_fail;
1084 /* free the stream that we acquired before */
1086 gst_pulseringbuffer_release (GstAudioRingBuffer * buf)
1088 GstPulseRingBuffer *pbuf;
1090 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1092 pa_threaded_mainloop_lock (mainloop);
1093 gst_pulsering_destroy_stream (pbuf);
1094 pa_threaded_mainloop_unlock (mainloop);
1097 GstPulseSink *psink;
1099 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1100 g_atomic_int_set (&psink->format_lost, FALSE);
1101 psink->format_lost_time = GST_CLOCK_TIME_NONE;
1108 gst_pulsering_success_cb (pa_stream * s, int success, void *userdata)
1110 pa_threaded_mainloop_signal (mainloop, 0);
1113 /* update the corked state of a stream, must be called with the mainloop
1116 gst_pulsering_set_corked (GstPulseRingBuffer * pbuf, gboolean corked,
1119 pa_operation *o = NULL;
1120 GstPulseSink *psink;
1121 gboolean res = FALSE;
1123 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1125 if (g_atomic_int_get (&psink->format_lost)) {
1126 /* Sink format changed, stream's gone so fake being paused */
1130 GST_DEBUG_OBJECT (psink, "setting corked state to %d", corked);
1131 if (pbuf->corked != corked) {
1132 if (!(o = pa_stream_cork (pbuf->stream, corked,
1133 gst_pulsering_success_cb, pbuf)))
1136 while (wait && pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1137 pa_threaded_mainloop_wait (mainloop);
1138 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
1141 pbuf->corked = corked;
1143 GST_DEBUG_OBJECT (psink, "skipping, already in requested state");
1149 pa_operation_unref (o);
1156 GST_DEBUG_OBJECT (psink, "the server is dead");
1161 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1162 ("pa_stream_cork() failed: %s",
1163 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1169 gst_pulseringbuffer_clear (GstAudioRingBuffer * buf)
1171 GstPulseSink *psink;
1172 GstPulseRingBuffer *pbuf;
1173 pa_operation *o = NULL;
1175 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1176 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1178 pa_threaded_mainloop_lock (mainloop);
1179 GST_DEBUG_OBJECT (psink, "clearing");
1181 /* don't wait for the flush to complete */
1182 if ((o = pa_stream_flush (pbuf->stream, NULL, pbuf)))
1183 pa_operation_unref (o);
1185 pa_threaded_mainloop_unlock (mainloop);
1189 /* called from pulse thread with the mainloop lock */
1191 mainloop_enter_defer_cb (pa_mainloop_api * api, void *userdata)
1193 GstPulseSink *pulsesink = GST_PULSESINK (userdata);
1194 GstMessage *message;
1197 GST_DEBUG_OBJECT (pulsesink, "posting ENTER stream status");
1198 message = gst_message_new_stream_status (GST_OBJECT (pulsesink),
1199 GST_STREAM_STATUS_TYPE_ENTER, GST_ELEMENT (pulsesink));
1200 g_value_init (&val, GST_TYPE_G_THREAD);
1201 g_value_set_boxed (&val, g_thread_self ());
1202 gst_message_set_stream_status_object (message, &val);
1203 g_value_unset (&val);
1205 gst_element_post_message (GST_ELEMENT (pulsesink), message);
1207 g_return_if_fail (pulsesink->defer_pending);
1208 pulsesink->defer_pending--;
1209 pa_threaded_mainloop_signal (mainloop, 0);
1213 /* start/resume playback ASAP, we don't uncork here but in the commit method */
1215 gst_pulseringbuffer_start (GstAudioRingBuffer * buf)
1217 GstPulseSink *psink;
1218 GstPulseRingBuffer *pbuf;
1220 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1221 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1223 pa_threaded_mainloop_lock (mainloop);
1225 GST_DEBUG_OBJECT (psink, "starting");
1226 pbuf->paused = FALSE;
1228 /* EOS needs running clock */
1229 if (GST_BASE_SINK_CAST (psink)->eos ||
1230 g_atomic_int_get (&GST_AUDIO_BASE_SINK (psink)->eos_rendering))
1231 gst_pulsering_set_corked (pbuf, FALSE, FALSE);
1234 GST_DEBUG_OBJECT (psink, "scheduling stream status");
1235 psink->defer_pending++;
1236 pa_mainloop_api_once (pa_threaded_mainloop_get_api (mainloop),
1237 mainloop_enter_defer_cb, psink);
1239 /* Wait for the stream status message to be posted. This needs to be done
1240 * synchronously because the callback will take the mainloop lock
1241 * (implicitly) and then take the GST_OBJECT_LOCK. Everywhere else, we take
1242 * the locks in the reverse order, so not doing this synchronously could
1243 * cause a deadlock. */
1244 GST_DEBUG_OBJECT (psink, "waiting for stream status (ENTER) to be posted");
1245 pa_threaded_mainloop_wait (mainloop);
1248 pa_threaded_mainloop_unlock (mainloop);
1253 /* pause/stop playback ASAP */
1255 gst_pulseringbuffer_pause (GstAudioRingBuffer * buf)
1257 GstPulseSink *psink;
1258 GstPulseRingBuffer *pbuf;
1261 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1262 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1264 pa_threaded_mainloop_lock (mainloop);
1265 GST_DEBUG_OBJECT (psink, "pausing and corking");
1266 /* make sure the commit method stops writing */
1267 pbuf->paused = TRUE;
1268 res = gst_pulsering_set_corked (pbuf, TRUE, TRUE);
1269 if (pbuf->in_commit) {
1270 /* we are waiting in a commit, signal */
1271 GST_DEBUG_OBJECT (psink, "signal commit");
1272 pa_threaded_mainloop_signal (mainloop, 0);
1274 pa_threaded_mainloop_unlock (mainloop);
1280 /* called from pulse thread with the mainloop lock */
1282 mainloop_leave_defer_cb (pa_mainloop_api * api, void *userdata)
1284 GstPulseSink *pulsesink = GST_PULSESINK (userdata);
1285 GstMessage *message;
1288 GST_DEBUG_OBJECT (pulsesink, "posting LEAVE stream status");
1289 message = gst_message_new_stream_status (GST_OBJECT (pulsesink),
1290 GST_STREAM_STATUS_TYPE_LEAVE, GST_ELEMENT (pulsesink));
1291 g_value_init (&val, GST_TYPE_G_THREAD);
1292 g_value_set_boxed (&val, g_thread_self ());
1293 gst_message_set_stream_status_object (message, &val);
1294 g_value_unset (&val);
1296 gst_element_post_message (GST_ELEMENT (pulsesink), message);
1298 g_return_if_fail (pulsesink->defer_pending);
1299 pulsesink->defer_pending--;
1300 pa_threaded_mainloop_signal (mainloop, 0);
1304 /* stop playback, we flush everything. */
1306 gst_pulseringbuffer_stop (GstAudioRingBuffer * buf)
1308 GstPulseSink *psink;
1309 GstPulseRingBuffer *pbuf;
1310 gboolean res = FALSE;
1311 pa_operation *o = NULL;
1313 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1314 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1316 pa_threaded_mainloop_lock (mainloop);
1318 pbuf->paused = TRUE;
1319 res = gst_pulsering_set_corked (pbuf, TRUE, TRUE);
1321 /* Inform anyone waiting in _commit() call that it shall wakeup */
1322 if (pbuf->in_commit) {
1323 GST_DEBUG_OBJECT (psink, "signal commit thread");
1324 pa_threaded_mainloop_signal (mainloop, 0);
1326 if (g_atomic_int_get (&psink->format_lost)) {
1327 /* Don't try to flush, the stream's probably gone by now */
1332 /* then try to flush, it's not fatal when this fails */
1333 GST_DEBUG_OBJECT (psink, "flushing");
1334 if ((o = pa_stream_flush (pbuf->stream, gst_pulsering_success_cb, pbuf))) {
1335 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1336 GST_DEBUG_OBJECT (psink, "wait for completion");
1337 pa_threaded_mainloop_wait (mainloop);
1338 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
1341 GST_DEBUG_OBJECT (psink, "flush completed");
1347 pa_operation_cancel (o);
1348 pa_operation_unref (o);
1351 GST_DEBUG_OBJECT (psink, "scheduling stream status");
1352 psink->defer_pending++;
1353 pa_mainloop_api_once (pa_threaded_mainloop_get_api (mainloop),
1354 mainloop_leave_defer_cb, psink);
1356 /* Wait for the stream status message to be posted. This needs to be done
1357 * synchronously because the callback will take the mainloop lock
1358 * (implicitly) and then take the GST_OBJECT_LOCK. Everywhere else, we take
1359 * the locks in the reverse order, so not doing this synchronously could
1360 * cause a deadlock. */
1361 GST_DEBUG_OBJECT (psink, "waiting for stream status (LEAVE) to be posted");
1362 pa_threaded_mainloop_wait (mainloop);
1365 pa_threaded_mainloop_unlock (mainloop);
1372 GST_DEBUG_OBJECT (psink, "the server is dead");
1377 /* in_samples >= out_samples, rate > 1.0 */
1378 #define FWD_UP_SAMPLES(s,se,d,de) \
1380 guint8 *sb = s, *db = d; \
1381 while (s <= se && d < de) { \
1382 memcpy (d, s, bpf); \
1385 if ((*accum << 1) >= inr) { \
1390 in_samples -= (s - sb)/bpf; \
1391 out_samples -= (d - db)/bpf; \
1392 GST_DEBUG ("fwd_up end %d/%d",*accum,*toprocess); \
1395 /* out_samples > in_samples, for rates smaller than 1.0 */
1396 #define FWD_DOWN_SAMPLES(s,se,d,de) \
1398 guint8 *sb = s, *db = d; \
1399 while (s <= se && d < de) { \
1400 memcpy (d, s, bpf); \
1403 if ((*accum << 1) >= outr) { \
1408 in_samples -= (s - sb)/bpf; \
1409 out_samples -= (d - db)/bpf; \
1410 GST_DEBUG ("fwd_down end %d/%d",*accum,*toprocess); \
1413 #define REV_UP_SAMPLES(s,se,d,de) \
1415 guint8 *sb = se, *db = d; \
1416 while (s <= se && d < de) { \
1417 memcpy (d, se, bpf); \
1420 while (d < de && (*accum << 1) >= inr) { \
1425 in_samples -= (sb - se)/bpf; \
1426 out_samples -= (d - db)/bpf; \
1427 GST_DEBUG ("rev_up end %d/%d",*accum,*toprocess); \
1430 #define REV_DOWN_SAMPLES(s,se,d,de) \
1432 guint8 *sb = se, *db = d; \
1433 while (s <= se && d < de) { \
1434 memcpy (d, se, bpf); \
1437 while (s <= se && (*accum << 1) >= outr) { \
1442 in_samples -= (sb - se)/bpf; \
1443 out_samples -= (d - db)/bpf; \
1444 GST_DEBUG ("rev_down end %d/%d",*accum,*toprocess); \
1447 /* our custom commit function because we write into the buffer of pulseaudio
1448 * instead of keeping our own buffer */
1450 gst_pulseringbuffer_commit (GstAudioRingBuffer * buf, guint64 * sample,
1451 guchar * data, gint in_samples, gint out_samples, gint * accum)
1453 GstPulseSink *psink;
1454 GstPulseRingBuffer *pbuf;
1459 gint inr, outr, bpf;
1463 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1464 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1466 /* FIXME post message rather than using a signal (as mixer interface) */
1467 if (g_atomic_int_compare_and_exchange (&psink->notify, 1, 0)) {
1468 g_object_notify (G_OBJECT (psink), "volume");
1469 g_object_notify (G_OBJECT (psink), "mute");
1470 g_object_notify (G_OBJECT (psink), "current-device");
1473 /* make sure the ringbuffer is started */
1474 if (G_UNLIKELY (g_atomic_int_get (&buf->state) !=
1475 GST_AUDIO_RING_BUFFER_STATE_STARTED)) {
1476 /* see if we are allowed to start it */
1477 if (G_UNLIKELY (g_atomic_int_get (&buf->may_start) == FALSE))
1480 GST_DEBUG_OBJECT (buf, "start!");
1481 if (!gst_audio_ring_buffer_start (buf))
1485 pa_threaded_mainloop_lock (mainloop);
1487 GST_DEBUG_OBJECT (psink, "entering commit");
1488 pbuf->in_commit = TRUE;
1490 bpf = GST_AUDIO_INFO_BPF (&buf->spec.info);
1491 bufsize = buf->spec.segsize * buf->spec.segtotal;
1493 /* our toy resampler for trick modes */
1494 reverse = out_samples < 0;
1495 out_samples = ABS (out_samples);
1497 if (in_samples >= out_samples)
1498 toprocess = &in_samples;
1500 toprocess = &out_samples;
1502 inr = in_samples - 1;
1503 outr = out_samples - 1;
1505 GST_DEBUG_OBJECT (psink, "in %d, out %d", inr, outr);
1507 /* data_end points to the last sample we have to write, not past it. This is
1508 * needed to properly handle reverse playback: it points to the last sample. */
1509 data_end = data + (bpf * inr);
1511 if (g_atomic_int_get (&psink->format_lost)) {
1512 /* Sink format changed, drop the data and hope upstream renegotiates */
1519 /* offset is in bytes */
1520 offset = *sample * bpf;
1522 while (*toprocess > 0) {
1526 GST_LOG_OBJECT (psink,
1527 "need to write %d samples at offset %" G_GINT64_FORMAT, *toprocess,
1530 if (offset != pbuf->m_lastoffset)
1531 GST_LOG_OBJECT (psink, "discontinuity, offset is %" G_GINT64_FORMAT ", "
1532 "last offset was %" G_GINT64_FORMAT, offset, pbuf->m_lastoffset);
1534 towrite = out_samples * bpf;
1536 /* Wait for at least segsize bytes to become available */
1537 if (towrite > buf->spec.segsize)
1538 towrite = buf->spec.segsize;
1540 if ((pbuf->m_writable < towrite) || (offset != pbuf->m_lastoffset)) {
1541 /* if no room left or discontinuity in offset,
1542 we need to flush data and get a new buffer */
1544 /* flush the buffer if possible */
1545 if ((pbuf->m_data != NULL) && (pbuf->m_towrite > 0)) {
1547 GST_LOG_OBJECT (psink,
1548 "flushing %u samples at offset %" G_GINT64_FORMAT,
1549 (guint) pbuf->m_towrite / bpf, pbuf->m_offset);
1551 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1552 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1556 pbuf->m_towrite = 0;
1557 pbuf->m_offset = offset; /* keep track of current offset */
1559 /* get a buffer to write in for now on */
1561 pbuf->m_writable = pa_stream_writable_size (pbuf->stream);
1563 if (g_atomic_int_get (&psink->format_lost)) {
1564 /* Sink format changed, give up and hope upstream renegotiates */
1568 if (pbuf->m_writable == (size_t) - 1)
1569 goto writable_size_failed;
1571 pbuf->m_writable /= bpf;
1572 pbuf->m_writable *= bpf; /* handle only complete samples */
1574 if (pbuf->m_writable >= towrite)
1577 /* see if we need to uncork because we have no free space */
1579 if (!gst_pulsering_set_corked (pbuf, FALSE, FALSE))
1583 /* we can't write segsize bytes, wait a bit */
1584 GST_LOG_OBJECT (psink, "waiting for free space");
1585 pa_threaded_mainloop_wait (mainloop);
1591 /* Recalculate what we can write in the next chunk */
1592 towrite = out_samples * bpf;
1593 if (pbuf->m_writable > towrite)
1594 pbuf->m_writable = towrite;
1596 GST_LOG_OBJECT (psink, "requesting %" G_GSIZE_FORMAT " bytes of "
1597 "shared memory", pbuf->m_writable);
1599 if (pa_stream_begin_write (pbuf->stream, &pbuf->m_data,
1600 &pbuf->m_writable) < 0) {
1601 GST_LOG_OBJECT (psink, "pa_stream_begin_write() failed");
1602 goto writable_size_failed;
1605 GST_LOG_OBJECT (psink, "got %" G_GSIZE_FORMAT " bytes of shared memory",
1610 if (towrite > pbuf->m_writable)
1611 towrite = pbuf->m_writable;
1612 avail = towrite / bpf;
1614 GST_LOG_OBJECT (psink, "writing %u samples at offset %" G_GUINT64_FORMAT,
1615 (guint) avail, offset);
1617 /* No trick modes for passthrough streams */
1618 if (G_UNLIKELY (!pbuf->is_pcm && (inr != outr || reverse))) {
1619 GST_WARNING_OBJECT (psink, "Passthrough stream can't run in trick mode");
1620 goto unlock_and_fail;
1623 if (G_LIKELY (inr == outr && !reverse)) {
1624 /* no rate conversion, simply write out the samples */
1625 /* copy the data into internal buffer */
1627 memcpy ((guint8 *) pbuf->m_data + pbuf->m_towrite, data, towrite);
1628 pbuf->m_towrite += towrite;
1629 pbuf->m_writable -= towrite;
1632 in_samples -= avail;
1633 out_samples -= avail;
1635 guint8 *dest, *d, *d_end;
1637 /* write into the PulseAudio shm buffer */
1638 dest = d = (guint8 *) pbuf->m_data + pbuf->m_towrite;
1639 d_end = d + towrite;
1643 /* forward speed up */
1644 FWD_UP_SAMPLES (data, data_end, d, d_end);
1646 /* forward slow down */
1647 FWD_DOWN_SAMPLES (data, data_end, d, d_end);
1650 /* reverse speed up */
1651 REV_UP_SAMPLES (data, data_end, d, d_end);
1653 /* reverse slow down */
1654 REV_DOWN_SAMPLES (data, data_end, d, d_end);
1656 /* see what we have left to write */
1657 towrite = (d - dest);
1658 pbuf->m_towrite += towrite;
1659 pbuf->m_writable -= towrite;
1661 avail = towrite / bpf;
1664 /* flush the buffer if it's full */
1665 if ((pbuf->m_data != NULL) && (pbuf->m_towrite > 0)
1666 && (pbuf->m_writable == 0)) {
1667 GST_LOG_OBJECT (psink, "flushing %u samples at offset %" G_GINT64_FORMAT,
1668 (guint) pbuf->m_towrite / bpf, pbuf->m_offset);
1670 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1671 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1674 pbuf->m_towrite = 0;
1675 pbuf->m_offset = offset + towrite; /* keep track of current offset */
1679 offset += avail * bpf;
1680 pbuf->m_lastoffset = offset;
1682 /* check if we need to uncork after writing the samples */
1684 const pa_timing_info *info;
1686 if ((info = pa_stream_get_timing_info (pbuf->stream))) {
1687 GST_LOG_OBJECT (psink,
1688 "read_index at %" G_GUINT64_FORMAT ", offset %" G_GINT64_FORMAT,
1689 info->read_index, offset);
1691 /* we uncork when the read_index is too far behind the offset we need
1693 if (info->read_index + bufsize <= offset) {
1694 if (!gst_pulsering_set_corked (pbuf, FALSE, FALSE))
1698 GST_LOG_OBJECT (psink, "no timing info available yet");
1704 /* we consumed all samples here */
1705 data = data_end + bpf;
1707 pbuf->in_commit = FALSE;
1708 pa_threaded_mainloop_unlock (mainloop);
1711 result = inr - ((data_end - data) / bpf);
1712 GST_LOG_OBJECT (psink, "wrote %d samples", result);
1719 pbuf->in_commit = FALSE;
1720 GST_LOG_OBJECT (psink, "we are reset");
1721 pa_threaded_mainloop_unlock (mainloop);
1726 GST_LOG_OBJECT (psink, "we can not start");
1731 GST_LOG_OBJECT (psink, "failed to start the ringbuffer");
1736 pbuf->in_commit = FALSE;
1737 GST_ERROR_OBJECT (psink, "uncork failed");
1738 pa_threaded_mainloop_unlock (mainloop);
1743 pbuf->in_commit = FALSE;
1744 GST_LOG_OBJECT (psink, "we are paused");
1745 pa_threaded_mainloop_unlock (mainloop);
1748 writable_size_failed:
1750 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1751 ("pa_stream_writable_size() failed: %s",
1752 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1753 goto unlock_and_fail;
1757 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1758 ("pa_stream_write() failed: %s",
1759 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1760 goto unlock_and_fail;
1764 /* write pending local samples, must be called with the mainloop lock */
1766 gst_pulsering_flush (GstPulseRingBuffer * pbuf)
1768 GstPulseSink *psink;
1770 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1771 GST_DEBUG_OBJECT (psink, "entering flush");
1773 /* flush the buffer if possible */
1774 if (pbuf->stream && (pbuf->m_data != NULL) && (pbuf->m_towrite > 0)) {
1775 #ifndef GST_DISABLE_GST_DEBUG
1778 bpf = (GST_AUDIO_RING_BUFFER_CAST (pbuf))->spec.info.bpf;
1779 GST_LOG_OBJECT (psink,
1780 "flushing %u samples at offset %" G_GINT64_FORMAT,
1781 (guint) pbuf->m_towrite / bpf, pbuf->m_offset);
1784 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1785 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1789 pbuf->m_towrite = 0;
1790 pbuf->m_offset += pbuf->m_towrite; /* keep track of current offset */
1799 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1800 ("pa_stream_write() failed: %s",
1801 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1806 static void gst_pulsesink_set_property (GObject * object, guint prop_id,
1807 const GValue * value, GParamSpec * pspec);
1808 static void gst_pulsesink_get_property (GObject * object, guint prop_id,
1809 GValue * value, GParamSpec * pspec);
1810 static void gst_pulsesink_finalize (GObject * object);
1812 static gboolean gst_pulsesink_event (GstBaseSink * sink, GstEvent * event);
1813 static gboolean gst_pulsesink_query (GstBaseSink * sink, GstQuery * query);
1815 static GstStateChangeReturn gst_pulsesink_change_state (GstElement * element,
1816 GstStateChange transition);
1818 #define gst_pulsesink_parent_class parent_class
1819 G_DEFINE_TYPE_WITH_CODE (GstPulseSink, gst_pulsesink, GST_TYPE_AUDIO_BASE_SINK,
1820 gst_pulsesink_init_contexts ();
1821 G_IMPLEMENT_INTERFACE (GST_TYPE_STREAM_VOLUME, NULL)
1824 static GstAudioRingBuffer *
1825 gst_pulsesink_create_ringbuffer (GstAudioBaseSink * sink)
1827 GstAudioRingBuffer *buffer;
1829 GST_DEBUG_OBJECT (sink, "creating ringbuffer");
1830 buffer = g_object_new (GST_TYPE_PULSERING_BUFFER, NULL);
1831 GST_DEBUG_OBJECT (sink, "created ringbuffer @%p", buffer);
1837 gst_pulsesink_payload (GstAudioBaseSink * sink, GstBuffer * buf)
1839 switch (sink->ringbuffer->spec.type) {
1840 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3:
1841 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_EAC3:
1842 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS:
1843 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_MPEG:
1844 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_MPEG2_AAC:
1845 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_MPEG4_AAC:
1847 /* FIXME: alloc memory from PA if possible */
1848 gint framesize = gst_audio_iec61937_frame_size (&sink->ringbuffer->spec);
1850 GstMapInfo inmap, outmap;
1856 out = gst_buffer_new_and_alloc (framesize);
1858 gst_buffer_map (buf, &inmap, GST_MAP_READ);
1859 gst_buffer_map (out, &outmap, GST_MAP_WRITE);
1861 res = gst_audio_iec61937_payload (inmap.data, inmap.size,
1862 outmap.data, outmap.size, &sink->ringbuffer->spec, G_BIG_ENDIAN);
1864 gst_buffer_unmap (buf, &inmap);
1865 gst_buffer_unmap (out, &outmap);
1868 gst_buffer_unref (out);
1872 gst_buffer_copy_into (out, buf, GST_BUFFER_COPY_METADATA, 0, -1);
1877 return gst_buffer_ref (buf);
1882 gst_pulsesink_class_init (GstPulseSinkClass * klass)
1884 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1885 GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
1886 GstBaseSinkClass *bc;
1887 GstAudioBaseSinkClass *gstaudiosink_class = GST_AUDIO_BASE_SINK_CLASS (klass);
1888 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1892 gobject_class->finalize = gst_pulsesink_finalize;
1893 gobject_class->set_property = gst_pulsesink_set_property;
1894 gobject_class->get_property = gst_pulsesink_get_property;
1896 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_pulsesink_event);
1897 gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_pulsesink_query);
1899 /* restore the original basesink pull methods */
1900 bc = g_type_class_peek (GST_TYPE_BASE_SINK);
1901 gstbasesink_class->activate_pull = GST_DEBUG_FUNCPTR (bc->activate_pull);
1903 gstelement_class->change_state =
1904 GST_DEBUG_FUNCPTR (gst_pulsesink_change_state);
1906 gstaudiosink_class->create_ringbuffer =
1907 GST_DEBUG_FUNCPTR (gst_pulsesink_create_ringbuffer);
1908 gstaudiosink_class->payload = GST_DEBUG_FUNCPTR (gst_pulsesink_payload);
1910 /* Overwrite GObject fields */
1911 g_object_class_install_property (gobject_class,
1913 g_param_spec_string ("server", "Server",
1914 "The PulseAudio server to connect to", DEFAULT_SERVER,
1915 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1917 g_object_class_install_property (gobject_class, PROP_DEVICE,
1918 g_param_spec_string ("device", "Device",
1919 "The PulseAudio sink device to connect to", DEFAULT_DEVICE,
1920 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1922 g_object_class_install_property (gobject_class, PROP_CURRENT_DEVICE,
1923 g_param_spec_string ("current-device", "Current Device",
1924 "The current PulseAudio sink device", DEFAULT_CURRENT_DEVICE,
1925 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1927 g_object_class_install_property (gobject_class,
1929 g_param_spec_string ("device-name", "Device name",
1930 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
1931 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1933 g_object_class_install_property (gobject_class,
1935 g_param_spec_double ("volume", "Volume",
1936 "Linear volume of this stream, 1.0=100%", 0.0, MAX_VOLUME,
1937 DEFAULT_VOLUME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1938 g_object_class_install_property (gobject_class,
1940 g_param_spec_boolean ("mute", "Mute",
1941 "Mute state of this stream", DEFAULT_MUTE,
1942 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1945 * GstPulseSink:client-name:
1947 * The PulseAudio client name to use.
1949 clientname = gst_pulse_client_name ();
1950 g_object_class_install_property (gobject_class,
1952 g_param_spec_string ("client-name", "Client Name",
1953 "The PulseAudio client name to use", clientname,
1954 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1955 GST_PARAM_MUTABLE_READY));
1956 g_free (clientname);
1959 * GstPulseSink:stream-properties:
1961 * List of pulseaudio stream properties. A list of defined properties can be
1962 * found in the <ulink url="http://0pointer.de/lennart/projects/pulseaudio/doxygen/proplist_8h.html">pulseaudio api docs</ulink>.
1964 * Below is an example for registering as a music application to pulseaudio.
1966 * GstStructure *props;
1968 * props = gst_structure_from_string ("props,media.role=music", NULL);
1969 * g_object_set (pulse, "stream-properties", props, NULL);
1970 * gst_structure_free
1973 g_object_class_install_property (gobject_class,
1974 PROP_STREAM_PROPERTIES,
1975 g_param_spec_boxed ("stream-properties", "stream properties",
1976 "list of pulseaudio stream properties",
1977 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1979 gst_element_class_set_static_metadata (gstelement_class,
1980 "PulseAudio Audio Sink",
1981 "Sink/Audio", "Plays audio to a PulseAudio server", "Lennart Poettering");
1984 gst_pulse_fix_pcm_caps (gst_caps_from_string (PULSE_SINK_TEMPLATE_CAPS));
1985 gst_element_class_add_pad_template (gstelement_class,
1986 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, caps));
1987 gst_caps_unref (caps);
1991 free_device_info (GstPulseDeviceInfo * device_info)
1995 g_free (device_info->description);
1997 for (l = g_list_first (device_info->formats); l; l = g_list_next (l))
1998 pa_format_info_free ((pa_format_info *) l->data);
2000 g_list_free (device_info->formats);
2003 /* Returns the current time of the sink ringbuffer. The timing_info is updated
2004 * on every data write/flush and every 100ms (PA_STREAM_AUTO_TIMING_UPDATE).
2007 gst_pulsesink_get_time (GstClock * clock, GstAudioBaseSink * sink)
2009 GstPulseSink *psink;
2010 GstPulseRingBuffer *pbuf;
2013 if (!sink->ringbuffer || !sink->ringbuffer->acquired)
2014 return GST_CLOCK_TIME_NONE;
2016 pbuf = GST_PULSERING_BUFFER_CAST (sink->ringbuffer);
2017 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
2019 if (g_atomic_int_get (&psink->format_lost)) {
2020 /* Stream was lost in a format change, it'll get set up again once
2021 * upstream renegotiates */
2022 return psink->format_lost_time;
2025 pa_threaded_mainloop_lock (mainloop);
2026 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
2029 /* if we don't have enough data to get a timestamp, just return NONE, which
2030 * will return the last reported time */
2031 if (pa_stream_get_time (pbuf->stream, &time) < 0) {
2032 GST_DEBUG_OBJECT (psink, "could not get time");
2033 time = GST_CLOCK_TIME_NONE;
2036 pa_threaded_mainloop_unlock (mainloop);
2038 GST_LOG_OBJECT (psink, "current time is %" GST_TIME_FORMAT,
2039 GST_TIME_ARGS (time));
2046 GST_DEBUG_OBJECT (psink, "the server is dead");
2047 pa_threaded_mainloop_unlock (mainloop);
2049 return GST_CLOCK_TIME_NONE;
2054 gst_pulsesink_sink_info_cb (pa_context * c, const pa_sink_info * i, int eol,
2057 GstPulseDeviceInfo *device_info = (GstPulseDeviceInfo *) userdata;
2063 device_info->description = g_strdup (i->description);
2065 device_info->formats = NULL;
2066 for (j = 0; j < i->n_formats; j++)
2067 device_info->formats = g_list_prepend (device_info->formats,
2068 pa_format_info_copy (i->formats[j]));
2071 pa_threaded_mainloop_signal (mainloop, 0);
2074 /* Call with mainloop lock held */
2076 gst_pulsesink_create_probe_stream (GstPulseSink * psink,
2077 GstPulseRingBuffer * pbuf, pa_format_info * format)
2079 pa_format_info *formats[1] = { format };
2081 pa_stream_flags_t flags;
2083 GST_LOG_OBJECT (psink, "Creating probe stream");
2085 if (!(stream = pa_stream_new_extended (pbuf->context, "pulsesink probe",
2086 formats, 1, psink->proplist)))
2089 /* construct the flags */
2090 flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE |
2091 PA_STREAM_ADJUST_LATENCY | PA_STREAM_START_CORKED;
2093 pa_stream_set_state_callback (stream, gst_pulsering_stream_state_cb, pbuf);
2095 if (pa_stream_connect_playback (stream, psink->device, NULL, flags, NULL,
2099 if (!gst_pulsering_wait_for_stream_ready (psink, stream))
2106 pa_stream_unref (stream);
2111 gst_pulsesink_query_getcaps (GstPulseSink * psink, GstCaps * filter)
2113 GstPulseRingBuffer *pbuf = NULL;
2114 GstPulseDeviceInfo device_info = { NULL, NULL };
2115 GstCaps *ret = NULL;
2117 pa_operation *o = NULL;
2120 GST_OBJECT_LOCK (psink);
2121 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2123 gst_object_ref (pbuf);
2124 GST_OBJECT_UNLOCK (psink);
2127 ret = gst_pad_get_pad_template_caps (GST_AUDIO_BASE_SINK_PAD (psink));
2131 GST_OBJECT_LOCK (pbuf);
2132 pa_threaded_mainloop_lock (mainloop);
2134 if (!pbuf->context) {
2135 ret = gst_pad_get_pad_template_caps (GST_AUDIO_BASE_SINK_PAD (psink));
2139 ret = gst_caps_new_empty ();
2142 /* We're in PAUSED or higher */
2143 stream = pbuf->stream;
2145 } else if (pbuf->probe_stream) {
2146 /* We're not paused, but have a cached probe stream */
2147 stream = pbuf->probe_stream;
2150 /* We're not yet in PAUSED and still need to create a probe stream.
2152 * FIXME: PA doesn't accept "any" format. We fix something reasonable since
2153 * this is merely a probe. This should eventually be fixed in PA and
2154 * hard-coding the format should be dropped. */
2155 pa_format_info *format = pa_format_info_new ();
2156 format->encoding = PA_ENCODING_PCM;
2157 pa_format_info_set_sample_format (format, PA_SAMPLE_S16LE);
2158 pa_format_info_set_rate (format, GST_AUDIO_DEF_RATE);
2159 pa_format_info_set_channels (format, GST_AUDIO_DEF_CHANNELS);
2161 pbuf->probe_stream = gst_pulsesink_create_probe_stream (psink, pbuf,
2164 pa_format_info_free (format);
2166 if (!pbuf->probe_stream) {
2167 GST_WARNING_OBJECT (psink, "Could not create probe stream");
2171 stream = pbuf->probe_stream;
2174 if (!(o = pa_context_get_sink_info_by_name (pbuf->context,
2175 pa_stream_get_device_name (stream), gst_pulsesink_sink_info_cb,
2179 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2180 pa_threaded_mainloop_wait (mainloop);
2181 if (gst_pulsering_is_dead (psink, pbuf, FALSE))
2185 for (i = g_list_first (device_info.formats); i; i = g_list_next (i)) {
2186 GstCaps *caps = gst_pulse_format_info_to_caps ((pa_format_info *) i->data);
2188 gst_caps_append (ret, caps);
2192 pa_threaded_mainloop_unlock (mainloop);
2193 /* FIXME: this could be freed after device_name is got */
2194 GST_OBJECT_UNLOCK (pbuf);
2197 GstCaps *tmp = gst_caps_intersect_full (filter, ret,
2198 GST_CAPS_INTERSECT_FIRST);
2199 gst_caps_unref (ret);
2204 free_device_info (&device_info);
2207 pa_operation_unref (o);
2210 gst_object_unref (pbuf);
2212 GST_DEBUG_OBJECT (psink, "caps %" GST_PTR_FORMAT, ret);
2218 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2219 ("pa_context_get_sink_input_info() failed: %s",
2220 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2226 gst_pulsesink_query_acceptcaps (GstPulseSink * psink, GstCaps * caps)
2228 GstPulseRingBuffer *pbuf = NULL;
2229 GstPulseDeviceInfo device_info = { NULL, NULL };
2232 gboolean ret = FALSE;
2234 GstAudioRingBufferSpec spec = { 0 };
2235 pa_operation *o = NULL;
2236 pa_channel_map channel_map;
2237 pa_format_info *format = NULL;
2240 pad_caps = gst_pad_get_pad_template_caps (GST_BASE_SINK_PAD (psink));
2241 ret = gst_caps_is_subset (caps, pad_caps);
2242 gst_caps_unref (pad_caps);
2244 GST_DEBUG_OBJECT (psink, "caps %" GST_PTR_FORMAT, caps);
2246 /* Template caps didn't match */
2250 /* If we've not got fixed caps, creating a stream might fail, so let's just
2251 * return from here with default acceptcaps behaviour */
2252 if (!gst_caps_is_fixed (caps))
2255 GST_OBJECT_LOCK (psink);
2256 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2258 gst_object_ref (pbuf);
2259 GST_OBJECT_UNLOCK (psink);
2261 /* We're still in NULL state */
2265 GST_OBJECT_LOCK (pbuf);
2266 pa_threaded_mainloop_lock (mainloop);
2268 if (pbuf->context == NULL)
2273 spec.latency_time = GST_AUDIO_BASE_SINK (psink)->latency_time;
2274 if (!gst_audio_ring_buffer_parse_caps (&spec, caps))
2277 if (!gst_pulse_fill_format_info (&spec, &format, &channels))
2280 /* Make sure input is framed (one frame per buffer) and can be payloaded */
2281 if (!pa_format_info_is_pcm (format)) {
2282 gboolean framed = FALSE, parsed = FALSE;
2283 st = gst_caps_get_structure (caps, 0);
2285 gst_structure_get_boolean (st, "framed", &framed);
2286 gst_structure_get_boolean (st, "parsed", &parsed);
2287 if ((!framed && !parsed) || gst_audio_iec61937_frame_size (&spec) <= 0)
2291 /* initialize the channel map */
2292 if (pa_format_info_is_pcm (format) &&
2293 gst_pulse_gst_to_channel_map (&channel_map, &spec))
2294 pa_format_info_set_channel_map (format, &channel_map);
2296 if (pbuf->stream || pbuf->probe_stream) {
2297 /* We're already in PAUSED or above, so just reuse this stream to query
2298 * sink formats and use those. */
2300 const char *device_name = pa_stream_get_device_name (pbuf->stream ?
2301 pbuf->stream : pbuf->probe_stream);
2303 if (!(o = pa_context_get_sink_info_by_name (pbuf->context, device_name,
2304 gst_pulsesink_sink_info_cb, &device_info)))
2307 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2308 pa_threaded_mainloop_wait (mainloop);
2309 if (gst_pulsering_is_dead (psink, pbuf, FALSE))
2313 for (i = g_list_first (device_info.formats); i; i = g_list_next (i)) {
2314 if (pa_format_info_is_compatible ((pa_format_info *) i->data, format)) {
2320 /* We're in READY, let's connect a stream to see if the format is
2321 * accepted by whatever sink we're routed to */
2322 pbuf->probe_stream = gst_pulsesink_create_probe_stream (psink, pbuf,
2324 if (pbuf->probe_stream)
2330 pa_format_info_free (format);
2332 free_device_info (&device_info);
2335 pa_operation_unref (o);
2337 pa_threaded_mainloop_unlock (mainloop);
2338 GST_OBJECT_UNLOCK (pbuf);
2340 gst_caps_replace (&spec.caps, NULL);
2341 gst_object_unref (pbuf);
2349 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2350 ("pa_context_get_sink_input_info() failed: %s",
2351 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2357 gst_pulsesink_init (GstPulseSink * pulsesink)
2359 pulsesink->server = NULL;
2360 pulsesink->device = NULL;
2361 pulsesink->device_info.description = NULL;
2362 pulsesink->client_name = gst_pulse_client_name ();
2364 pulsesink->device_info.formats = NULL;
2366 pulsesink->volume = DEFAULT_VOLUME;
2367 pulsesink->volume_set = FALSE;
2369 pulsesink->mute = DEFAULT_MUTE;
2370 pulsesink->mute_set = FALSE;
2372 pulsesink->notify = 0;
2374 g_atomic_int_set (&pulsesink->format_lost, FALSE);
2375 pulsesink->format_lost_time = GST_CLOCK_TIME_NONE;
2377 pulsesink->properties = NULL;
2378 pulsesink->proplist = NULL;
2380 /* override with a custom clock */
2381 if (GST_AUDIO_BASE_SINK (pulsesink)->provided_clock)
2382 gst_object_unref (GST_AUDIO_BASE_SINK (pulsesink)->provided_clock);
2384 GST_AUDIO_BASE_SINK (pulsesink)->provided_clock =
2385 gst_audio_clock_new ("GstPulseSinkClock",
2386 (GstAudioClockGetTimeFunc) gst_pulsesink_get_time, pulsesink, NULL);
2390 gst_pulsesink_finalize (GObject * object)
2392 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2394 g_free (pulsesink->server);
2395 g_free (pulsesink->device);
2396 g_free (pulsesink->client_name);
2397 g_free (pulsesink->current_sink_name);
2399 free_device_info (&pulsesink->device_info);
2401 if (pulsesink->properties)
2402 gst_structure_free (pulsesink->properties);
2403 if (pulsesink->proplist)
2404 pa_proplist_free (pulsesink->proplist);
2406 G_OBJECT_CLASS (parent_class)->finalize (object);
2410 gst_pulsesink_set_volume (GstPulseSink * psink, gdouble volume)
2413 pa_operation *o = NULL;
2414 GstPulseRingBuffer *pbuf;
2420 pa_threaded_mainloop_lock (mainloop);
2422 GST_DEBUG_OBJECT (psink, "setting volume to %f", volume);
2424 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2425 if (pbuf == NULL || pbuf->stream == NULL)
2428 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2432 gst_pulse_cvolume_from_linear (&v, pbuf->channels, volume);
2434 /* FIXME: this will eventually be superceded by checks to see if the volume
2435 * is readable/writable */
2438 if (!(o = pa_context_set_sink_input_volume (pbuf->context, idx,
2442 /* We don't really care about the result of this call */
2446 pa_operation_unref (o);
2448 pa_threaded_mainloop_unlock (mainloop);
2455 psink->volume = volume;
2456 psink->volume_set = TRUE;
2458 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2463 psink->volume = volume;
2464 psink->volume_set = TRUE;
2466 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2471 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2476 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2477 ("pa_stream_set_sink_input_volume() failed: %s",
2478 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2484 gst_pulsesink_set_mute (GstPulseSink * psink, gboolean mute)
2486 pa_operation *o = NULL;
2487 GstPulseRingBuffer *pbuf;
2493 pa_threaded_mainloop_lock (mainloop);
2495 GST_DEBUG_OBJECT (psink, "setting mute state to %d", mute);
2497 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2498 if (pbuf == NULL || pbuf->stream == NULL)
2501 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2504 if (!(o = pa_context_set_sink_input_mute (pbuf->context, idx,
2508 /* We don't really care about the result of this call */
2512 pa_operation_unref (o);
2514 pa_threaded_mainloop_unlock (mainloop);
2522 psink->mute_set = TRUE;
2524 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2530 psink->mute_set = TRUE;
2532 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2537 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2542 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2543 ("pa_stream_set_sink_input_mute() failed: %s",
2544 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2550 gst_pulsesink_sink_input_info_cb (pa_context * c, const pa_sink_input_info * i,
2551 int eol, void *userdata)
2553 GstPulseRingBuffer *pbuf;
2554 GstPulseSink *psink;
2556 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
2557 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
2565 /* If the index doesn't match our current stream,
2566 * it implies we just recreated the stream (caps change)
2568 if (i->index == pa_stream_get_index (pbuf->stream)) {
2569 psink->volume = pa_sw_volume_to_linear (pa_cvolume_max (&i->volume));
2570 psink->mute = i->mute;
2571 psink->current_sink_idx = i->sink;
2573 if (psink->volume > MAX_VOLUME) {
2574 GST_WARNING_OBJECT (psink, "Clipped volume from %f to %f", psink->volume,
2576 psink->volume = MAX_VOLUME;
2581 pa_threaded_mainloop_signal (mainloop, 0);
2585 gst_pulsesink_get_sink_input_info (GstPulseSink * psink, gdouble * volume,
2588 GstPulseRingBuffer *pbuf;
2589 pa_operation *o = NULL;
2595 pa_threaded_mainloop_lock (mainloop);
2597 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2598 if (pbuf == NULL || pbuf->stream == NULL)
2601 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2604 if (!(o = pa_context_get_sink_input_info (pbuf->context, idx,
2605 gst_pulsesink_sink_input_info_cb, pbuf)))
2608 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2609 pa_threaded_mainloop_wait (mainloop);
2610 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
2616 *volume = psink->volume;
2618 *mute = psink->mute;
2621 pa_operation_unref (o);
2623 pa_threaded_mainloop_unlock (mainloop);
2631 *volume = psink->volume;
2633 *mute = psink->mute;
2635 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2640 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2645 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2650 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2651 ("pa_context_get_sink_input_info() failed: %s",
2652 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2658 gst_pulsesink_current_sink_info_cb (pa_context * c, const pa_sink_info * i,
2659 int eol, void *userdata)
2661 GstPulseSink *psink;
2663 psink = GST_PULSESINK_CAST (userdata);
2668 /* If the index doesn't match our current stream,
2669 * it implies we just recreated the stream (caps change)
2671 if (i->index == psink->current_sink_idx) {
2672 g_free (psink->current_sink_name);
2673 psink->current_sink_name = g_strdup (i->name);
2677 pa_threaded_mainloop_signal (mainloop, 0);
2681 gst_pulsesink_get_current_device (GstPulseSink * pulsesink)
2683 pa_operation *o = NULL;
2684 GstPulseRingBuffer *pbuf;
2685 gchar *current_sink;
2691 GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (pulsesink)->ringbuffer);
2692 if (pbuf == NULL || pbuf->stream == NULL)
2695 gst_pulsesink_get_sink_input_info (pulsesink, NULL, NULL);
2697 pa_threaded_mainloop_lock (mainloop);
2699 if (!(o = pa_context_get_sink_info_by_index (pbuf->context,
2700 pulsesink->current_sink_idx, gst_pulsesink_current_sink_info_cb,
2704 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2705 pa_threaded_mainloop_wait (mainloop);
2706 if (gst_pulsering_is_dead (pulsesink, pbuf, TRUE))
2712 current_sink = g_strdup (pulsesink->current_sink_name);
2715 pa_operation_unref (o);
2717 pa_threaded_mainloop_unlock (mainloop);
2719 return current_sink;
2724 GST_DEBUG_OBJECT (pulsesink, "we have no mainloop");
2729 GST_DEBUG_OBJECT (pulsesink, "we have no ringbuffer");
2734 GST_ELEMENT_ERROR (pulsesink, RESOURCE, FAILED,
2735 ("pa_context_get_sink_input_info() failed: %s",
2736 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2742 gst_pulsesink_device_description (GstPulseSink * psink)
2744 GstPulseRingBuffer *pbuf;
2745 pa_operation *o = NULL;
2751 pa_threaded_mainloop_lock (mainloop);
2752 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2756 free_device_info (&psink->device_info);
2757 if (!(o = pa_context_get_sink_info_by_name (pbuf->context,
2758 psink->device, gst_pulsesink_sink_info_cb, &psink->device_info)))
2761 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2762 pa_threaded_mainloop_wait (mainloop);
2763 if (gst_pulsering_is_dead (psink, pbuf, FALSE))
2769 pa_operation_unref (o);
2771 t = g_strdup (psink->device_info.description);
2772 pa_threaded_mainloop_unlock (mainloop);
2779 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2784 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2789 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2790 ("pa_context_get_sink_info_by_index() failed: %s",
2791 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2797 gst_pulsesink_set_stream_device (GstPulseSink * psink, const gchar * device)
2799 pa_operation *o = NULL;
2800 GstPulseRingBuffer *pbuf;
2806 pa_threaded_mainloop_lock (mainloop);
2808 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2809 if (pbuf == NULL || pbuf->stream == NULL)
2812 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2816 GST_DEBUG_OBJECT (psink, "setting stream device to %s", device);
2818 if (!(o = pa_context_move_sink_input_by_name (pbuf->context, idx, device,
2825 pa_operation_unref (o);
2827 pa_threaded_mainloop_unlock (mainloop);
2834 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2839 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2844 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2849 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2850 ("pa_context_move_sink_input_by_name(%s) failed: %s", device,
2851 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2858 gst_pulsesink_set_property (GObject * object,
2859 guint prop_id, const GValue * value, GParamSpec * pspec)
2861 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2865 g_free (pulsesink->server);
2866 pulsesink->server = g_value_dup_string (value);
2869 g_free (pulsesink->device);
2870 pulsesink->device = g_value_dup_string (value);
2871 gst_pulsesink_set_stream_device (pulsesink, pulsesink->device);
2874 gst_pulsesink_set_volume (pulsesink, g_value_get_double (value));
2877 gst_pulsesink_set_mute (pulsesink, g_value_get_boolean (value));
2879 case PROP_CLIENT_NAME:
2880 g_free (pulsesink->client_name);
2881 if (!g_value_get_string (value)) {
2882 GST_WARNING_OBJECT (pulsesink,
2883 "Empty PulseAudio client name not allowed. Resetting to default value");
2884 pulsesink->client_name = gst_pulse_client_name ();
2886 pulsesink->client_name = g_value_dup_string (value);
2888 case PROP_STREAM_PROPERTIES:
2889 if (pulsesink->properties)
2890 gst_structure_free (pulsesink->properties);
2891 pulsesink->properties =
2892 gst_structure_copy (gst_value_get_structure (value));
2893 if (pulsesink->proplist)
2894 pa_proplist_free (pulsesink->proplist);
2895 pulsesink->proplist = gst_pulse_make_proplist (pulsesink->properties);
2898 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2904 gst_pulsesink_get_property (GObject * object,
2905 guint prop_id, GValue * value, GParamSpec * pspec)
2908 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2912 g_value_set_string (value, pulsesink->server);
2915 g_value_set_string (value, pulsesink->device);
2917 case PROP_CURRENT_DEVICE:
2919 gchar *current_device = gst_pulsesink_get_current_device (pulsesink);
2921 g_value_take_string (value, current_device);
2923 g_value_set_string (value, "");
2926 case PROP_DEVICE_NAME:
2927 g_value_take_string (value, gst_pulsesink_device_description (pulsesink));
2933 gst_pulsesink_get_sink_input_info (pulsesink, &volume, NULL);
2934 g_value_set_double (value, volume);
2941 gst_pulsesink_get_sink_input_info (pulsesink, NULL, &mute);
2942 g_value_set_boolean (value, mute);
2945 case PROP_CLIENT_NAME:
2946 g_value_set_string (value, pulsesink->client_name);
2948 case PROP_STREAM_PROPERTIES:
2949 gst_value_set_structure (value, pulsesink->properties);
2952 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2958 gst_pulsesink_change_title (GstPulseSink * psink, const gchar * t)
2960 pa_operation *o = NULL;
2961 GstPulseRingBuffer *pbuf;
2963 pa_threaded_mainloop_lock (mainloop);
2965 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2967 if (pbuf == NULL || pbuf->stream == NULL)
2970 g_free (pbuf->stream_name);
2971 pbuf->stream_name = g_strdup (t);
2973 if (!(o = pa_stream_set_name (pbuf->stream, pbuf->stream_name, NULL, NULL)))
2976 /* We're not interested if this operation failed or not */
2980 pa_operation_unref (o);
2981 pa_threaded_mainloop_unlock (mainloop);
2988 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2993 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2994 ("pa_stream_set_name() failed: %s",
2995 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
3001 gst_pulsesink_change_props (GstPulseSink * psink, GstTagList * l)
3003 static const gchar *const map[] = {
3004 GST_TAG_TITLE, PA_PROP_MEDIA_TITLE,
3006 /* might get overriden in the next iteration by GST_TAG_ARTIST */
3007 GST_TAG_PERFORMER, PA_PROP_MEDIA_ARTIST,
3009 GST_TAG_ARTIST, PA_PROP_MEDIA_ARTIST,
3010 GST_TAG_LANGUAGE_CODE, PA_PROP_MEDIA_LANGUAGE,
3011 GST_TAG_LOCATION, PA_PROP_MEDIA_FILENAME,
3012 /* We might add more here later on ... */
3015 pa_proplist *pl = NULL;
3016 const gchar *const *t;
3017 gboolean empty = TRUE;
3018 pa_operation *o = NULL;
3019 GstPulseRingBuffer *pbuf;
3021 pl = pa_proplist_new ();
3023 for (t = map; *t; t += 2) {
3026 if (gst_tag_list_get_string (l, *t, &n)) {
3029 pa_proplist_sets (pl, *(t + 1), n);
3039 pa_threaded_mainloop_lock (mainloop);
3040 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
3041 if (pbuf == NULL || pbuf->stream == NULL)
3044 /* We're not interested if this operation failed or not */
3045 if (!(o = pa_stream_proplist_update (pbuf->stream, PA_UPDATE_REPLACE,
3047 GST_DEBUG_OBJECT (psink, "pa_stream_proplist_update() failed");
3053 pa_operation_unref (o);
3055 pa_threaded_mainloop_unlock (mainloop);
3060 pa_proplist_free (pl);
3067 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
3073 gst_pulsesink_flush_ringbuffer (GstPulseSink * psink)
3075 GstPulseRingBuffer *pbuf;
3077 pa_threaded_mainloop_lock (mainloop);
3079 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
3081 if (pbuf == NULL || pbuf->stream == NULL)
3084 gst_pulsering_flush (pbuf);
3086 /* Uncork if we haven't already (happens when waiting to get enough data
3087 * to send out the first time) */
3089 gst_pulsering_set_corked (pbuf, FALSE, FALSE);
3091 /* We're not interested if this operation failed or not */
3093 pa_threaded_mainloop_unlock (mainloop);
3100 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
3106 gst_pulsesink_event (GstBaseSink * sink, GstEvent * event)
3108 GstPulseSink *pulsesink = GST_PULSESINK_CAST (sink);
3110 switch (GST_EVENT_TYPE (event)) {
3111 case GST_EVENT_TAG:{
3112 gchar *title = NULL, *artist = NULL, *location = NULL, *description =
3113 NULL, *t = NULL, *buf = NULL;
3116 gst_event_parse_tag (event, &l);
3118 gst_tag_list_get_string (l, GST_TAG_TITLE, &title);
3119 gst_tag_list_get_string (l, GST_TAG_ARTIST, &artist);
3120 gst_tag_list_get_string (l, GST_TAG_LOCATION, &location);
3121 gst_tag_list_get_string (l, GST_TAG_DESCRIPTION, &description);
3124 gst_tag_list_get_string (l, GST_TAG_PERFORMER, &artist);
3126 if (title && artist)
3127 /* TRANSLATORS: 'song title' by 'artist name' */
3128 t = buf = g_strdup_printf (_("'%s' by '%s'"), g_strstrip (title),
3129 g_strstrip (artist));
3131 t = g_strstrip (title);
3132 else if (description)
3133 t = g_strstrip (description);
3135 t = g_strstrip (location);
3138 gst_pulsesink_change_title (pulsesink, t);
3143 g_free (description);
3146 gst_pulsesink_change_props (pulsesink, l);
3150 case GST_EVENT_GAP:{
3151 GstClockTime timestamp, duration;
3153 gst_event_parse_gap (event, ×tamp, &duration);
3154 if (duration == GST_CLOCK_TIME_NONE)
3155 gst_pulsesink_flush_ringbuffer (pulsesink);
3159 gst_pulsesink_flush_ringbuffer (pulsesink);
3165 return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
3169 gst_pulsesink_query (GstBaseSink * sink, GstQuery * query)
3171 GstPulseSink *pulsesink = GST_PULSESINK_CAST (sink);
3172 gboolean ret = FALSE;
3174 switch (GST_QUERY_TYPE (query)) {
3175 case GST_QUERY_CAPS:
3177 GstCaps *caps, *filter;
3179 gst_query_parse_caps (query, &filter);
3180 caps = gst_pulsesink_query_getcaps (pulsesink, filter);
3183 gst_query_set_caps_result (query, caps);
3184 gst_caps_unref (caps);
3189 case GST_QUERY_ACCEPT_CAPS:
3193 gst_query_parse_accept_caps (query, &caps);
3194 ret = gst_pulsesink_query_acceptcaps (pulsesink, caps);
3195 gst_query_set_accept_caps_result (query, ret);
3200 ret = GST_BASE_SINK_CLASS (parent_class)->query (sink, query);
3207 gst_pulsesink_release_mainloop (GstPulseSink * psink)
3212 pa_threaded_mainloop_lock (mainloop);
3213 while (psink->defer_pending) {
3214 GST_DEBUG_OBJECT (psink, "waiting for stream status message emission");
3215 pa_threaded_mainloop_wait (mainloop);
3217 pa_threaded_mainloop_unlock (mainloop);
3219 g_mutex_lock (&pa_shared_resource_mutex);
3221 if (!mainloop_ref_ct) {
3222 GST_INFO_OBJECT (psink, "terminating pa main loop thread");
3223 pa_threaded_mainloop_stop (mainloop);
3224 pa_threaded_mainloop_free (mainloop);
3227 g_mutex_unlock (&pa_shared_resource_mutex);
3230 static GstStateChangeReturn
3231 gst_pulsesink_change_state (GstElement * element, GstStateChange transition)
3233 GstPulseSink *pulsesink = GST_PULSESINK (element);
3234 GstStateChangeReturn ret;
3236 switch (transition) {
3237 case GST_STATE_CHANGE_NULL_TO_READY:
3238 g_mutex_lock (&pa_shared_resource_mutex);
3239 if (!mainloop_ref_ct) {
3240 GST_INFO_OBJECT (element, "new pa main loop thread");
3241 if (!(mainloop = pa_threaded_mainloop_new ()))
3242 goto mainloop_failed;
3243 if (pa_threaded_mainloop_start (mainloop) < 0) {
3244 pa_threaded_mainloop_free (mainloop);
3245 goto mainloop_start_failed;
3247 mainloop_ref_ct = 1;
3248 g_mutex_unlock (&pa_shared_resource_mutex);
3250 GST_INFO_OBJECT (element, "reusing pa main loop thread");
3252 g_mutex_unlock (&pa_shared_resource_mutex);
3255 case GST_STATE_CHANGE_READY_TO_PAUSED:
3256 gst_element_post_message (element,
3257 gst_message_new_clock_provide (GST_OBJECT_CAST (element),
3258 GST_AUDIO_BASE_SINK (pulsesink)->provided_clock, TRUE));
3265 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3266 if (ret == GST_STATE_CHANGE_FAILURE)
3269 switch (transition) {
3270 case GST_STATE_CHANGE_PAUSED_TO_READY:
3271 /* format_lost is reset in release() in audiobasesink */
3272 gst_element_post_message (element,
3273 gst_message_new_clock_lost (GST_OBJECT_CAST (element),
3274 GST_AUDIO_BASE_SINK (pulsesink)->provided_clock));
3276 case GST_STATE_CHANGE_READY_TO_NULL:
3277 gst_pulsesink_release_mainloop (pulsesink);
3288 g_mutex_unlock (&pa_shared_resource_mutex);
3289 GST_ELEMENT_ERROR (pulsesink, RESOURCE, FAILED,
3290 ("pa_threaded_mainloop_new() failed"), (NULL));
3291 return GST_STATE_CHANGE_FAILURE;
3293 mainloop_start_failed:
3295 g_mutex_unlock (&pa_shared_resource_mutex);
3296 GST_ELEMENT_ERROR (pulsesink, RESOURCE, FAILED,
3297 ("pa_threaded_mainloop_start() failed"), (NULL));
3298 return GST_STATE_CHANGE_FAILURE;
3302 if (transition == GST_STATE_CHANGE_NULL_TO_READY) {
3303 /* Clear the PA mainloop if audiobasesink failed to open the ring_buffer */
3304 g_assert (mainloop);
3305 gst_pulsesink_release_mainloop (pulsesink);