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 static GstStaticPadTemplate pad_template = GST_STATIC_PAD_TEMPLATE ("sink",
1821 GST_STATIC_CAPS (PULSE_SINK_TEMPLATE_CAPS));
1823 #define gst_pulsesink_parent_class parent_class
1824 G_DEFINE_TYPE_WITH_CODE (GstPulseSink, gst_pulsesink, GST_TYPE_AUDIO_BASE_SINK,
1825 gst_pulsesink_init_contexts ();
1826 G_IMPLEMENT_INTERFACE (GST_TYPE_STREAM_VOLUME, NULL)
1829 static GstAudioRingBuffer *
1830 gst_pulsesink_create_ringbuffer (GstAudioBaseSink * sink)
1832 GstAudioRingBuffer *buffer;
1834 GST_DEBUG_OBJECT (sink, "creating ringbuffer");
1835 buffer = g_object_new (GST_TYPE_PULSERING_BUFFER, NULL);
1836 GST_DEBUG_OBJECT (sink, "created ringbuffer @%p", buffer);
1842 gst_pulsesink_payload (GstAudioBaseSink * sink, GstBuffer * buf)
1844 switch (sink->ringbuffer->spec.type) {
1845 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3:
1846 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_EAC3:
1847 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS:
1848 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_MPEG:
1849 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_MPEG2_AAC:
1850 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_MPEG4_AAC:
1852 /* FIXME: alloc memory from PA if possible */
1853 gint framesize = gst_audio_iec61937_frame_size (&sink->ringbuffer->spec);
1855 GstMapInfo inmap, outmap;
1861 out = gst_buffer_new_and_alloc (framesize);
1863 gst_buffer_map (buf, &inmap, GST_MAP_READ);
1864 gst_buffer_map (out, &outmap, GST_MAP_WRITE);
1866 res = gst_audio_iec61937_payload (inmap.data, inmap.size,
1867 outmap.data, outmap.size, &sink->ringbuffer->spec, G_BIG_ENDIAN);
1869 gst_buffer_unmap (buf, &inmap);
1870 gst_buffer_unmap (out, &outmap);
1873 gst_buffer_unref (out);
1877 gst_buffer_copy_into (out, buf, GST_BUFFER_COPY_METADATA, 0, -1);
1882 return gst_buffer_ref (buf);
1887 gst_pulsesink_class_init (GstPulseSinkClass * klass)
1889 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1890 GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
1891 GstBaseSinkClass *bc;
1892 GstAudioBaseSinkClass *gstaudiosink_class = GST_AUDIO_BASE_SINK_CLASS (klass);
1893 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1896 gobject_class->finalize = gst_pulsesink_finalize;
1897 gobject_class->set_property = gst_pulsesink_set_property;
1898 gobject_class->get_property = gst_pulsesink_get_property;
1900 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_pulsesink_event);
1901 gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_pulsesink_query);
1903 /* restore the original basesink pull methods */
1904 bc = g_type_class_peek (GST_TYPE_BASE_SINK);
1905 gstbasesink_class->activate_pull = GST_DEBUG_FUNCPTR (bc->activate_pull);
1907 gstelement_class->change_state =
1908 GST_DEBUG_FUNCPTR (gst_pulsesink_change_state);
1910 gstaudiosink_class->create_ringbuffer =
1911 GST_DEBUG_FUNCPTR (gst_pulsesink_create_ringbuffer);
1912 gstaudiosink_class->payload = GST_DEBUG_FUNCPTR (gst_pulsesink_payload);
1914 /* Overwrite GObject fields */
1915 g_object_class_install_property (gobject_class,
1917 g_param_spec_string ("server", "Server",
1918 "The PulseAudio server to connect to", DEFAULT_SERVER,
1919 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1921 g_object_class_install_property (gobject_class, PROP_DEVICE,
1922 g_param_spec_string ("device", "Device",
1923 "The PulseAudio sink device to connect to", DEFAULT_DEVICE,
1924 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1926 g_object_class_install_property (gobject_class, PROP_CURRENT_DEVICE,
1927 g_param_spec_string ("current-device", "Current Device",
1928 "The current PulseAudio sink device", DEFAULT_CURRENT_DEVICE,
1929 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1931 g_object_class_install_property (gobject_class,
1933 g_param_spec_string ("device-name", "Device name",
1934 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
1935 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1937 g_object_class_install_property (gobject_class,
1939 g_param_spec_double ("volume", "Volume",
1940 "Linear volume of this stream, 1.0=100%", 0.0, MAX_VOLUME,
1941 DEFAULT_VOLUME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1942 g_object_class_install_property (gobject_class,
1944 g_param_spec_boolean ("mute", "Mute",
1945 "Mute state of this stream", DEFAULT_MUTE,
1946 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1949 * GstPulseSink:client-name:
1951 * The PulseAudio client name to use.
1953 clientname = gst_pulse_client_name ();
1954 g_object_class_install_property (gobject_class,
1956 g_param_spec_string ("client-name", "Client Name",
1957 "The PulseAudio client name to use", clientname,
1958 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1959 GST_PARAM_MUTABLE_READY));
1960 g_free (clientname);
1963 * GstPulseSink:stream-properties:
1965 * List of pulseaudio stream properties. A list of defined properties can be
1966 * found in the <ulink url="http://0pointer.de/lennart/projects/pulseaudio/doxygen/proplist_8h.html">pulseaudio api docs</ulink>.
1968 * Below is an example for registering as a music application to pulseaudio.
1970 * GstStructure *props;
1972 * props = gst_structure_from_string ("props,media.role=music", NULL);
1973 * g_object_set (pulse, "stream-properties", props, NULL);
1974 * gst_structure_free
1977 g_object_class_install_property (gobject_class,
1978 PROP_STREAM_PROPERTIES,
1979 g_param_spec_boxed ("stream-properties", "stream properties",
1980 "list of pulseaudio stream properties",
1981 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1983 gst_element_class_set_static_metadata (gstelement_class,
1984 "PulseAudio Audio Sink",
1985 "Sink/Audio", "Plays audio to a PulseAudio server", "Lennart Poettering");
1986 gst_element_class_add_static_pad_template (gstelement_class, &pad_template);
1990 free_device_info (GstPulseDeviceInfo * device_info)
1994 g_free (device_info->description);
1996 for (l = g_list_first (device_info->formats); l; l = g_list_next (l))
1997 pa_format_info_free ((pa_format_info *) l->data);
1999 g_list_free (device_info->formats);
2002 /* Returns the current time of the sink ringbuffer. The timing_info is updated
2003 * on every data write/flush and every 100ms (PA_STREAM_AUTO_TIMING_UPDATE).
2006 gst_pulsesink_get_time (GstClock * clock, GstAudioBaseSink * sink)
2008 GstPulseSink *psink;
2009 GstPulseRingBuffer *pbuf;
2012 if (!sink->ringbuffer || !sink->ringbuffer->acquired)
2013 return GST_CLOCK_TIME_NONE;
2015 pbuf = GST_PULSERING_BUFFER_CAST (sink->ringbuffer);
2016 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
2018 if (g_atomic_int_get (&psink->format_lost)) {
2019 /* Stream was lost in a format change, it'll get set up again once
2020 * upstream renegotiates */
2021 return psink->format_lost_time;
2024 pa_threaded_mainloop_lock (mainloop);
2025 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
2028 /* if we don't have enough data to get a timestamp, just return NONE, which
2029 * will return the last reported time */
2030 if (pa_stream_get_time (pbuf->stream, &time) < 0) {
2031 GST_DEBUG_OBJECT (psink, "could not get time");
2032 time = GST_CLOCK_TIME_NONE;
2035 pa_threaded_mainloop_unlock (mainloop);
2037 GST_LOG_OBJECT (psink, "current time is %" GST_TIME_FORMAT,
2038 GST_TIME_ARGS (time));
2045 GST_DEBUG_OBJECT (psink, "the server is dead");
2046 pa_threaded_mainloop_unlock (mainloop);
2048 return GST_CLOCK_TIME_NONE;
2053 gst_pulsesink_sink_info_cb (pa_context * c, const pa_sink_info * i, int eol,
2056 GstPulseDeviceInfo *device_info = (GstPulseDeviceInfo *) userdata;
2062 device_info->description = g_strdup (i->description);
2064 device_info->formats = NULL;
2065 for (j = 0; j < i->n_formats; j++)
2066 device_info->formats = g_list_prepend (device_info->formats,
2067 pa_format_info_copy (i->formats[j]));
2070 pa_threaded_mainloop_signal (mainloop, 0);
2073 /* Call with mainloop lock held */
2075 gst_pulsesink_create_probe_stream (GstPulseSink * psink,
2076 GstPulseRingBuffer * pbuf, pa_format_info * format)
2078 pa_format_info *formats[1] = { format };
2080 pa_stream_flags_t flags;
2082 GST_LOG_OBJECT (psink, "Creating probe stream");
2084 if (!(stream = pa_stream_new_extended (pbuf->context, "pulsesink probe",
2085 formats, 1, psink->proplist)))
2088 /* construct the flags */
2089 flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE |
2090 PA_STREAM_ADJUST_LATENCY | PA_STREAM_START_CORKED;
2092 pa_stream_set_state_callback (stream, gst_pulsering_stream_state_cb, pbuf);
2094 if (pa_stream_connect_playback (stream, psink->device, NULL, flags, NULL,
2098 if (!gst_pulsering_wait_for_stream_ready (psink, stream))
2105 pa_stream_unref (stream);
2110 gst_pulsesink_query_getcaps (GstPulseSink * psink, GstCaps * filter)
2112 GstPulseRingBuffer *pbuf = NULL;
2113 GstPulseDeviceInfo device_info = { NULL, NULL };
2114 GstCaps *ret = NULL;
2116 pa_operation *o = NULL;
2119 GST_OBJECT_LOCK (psink);
2120 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2122 gst_object_ref (pbuf);
2123 GST_OBJECT_UNLOCK (psink);
2126 ret = gst_pad_get_pad_template_caps (GST_AUDIO_BASE_SINK_PAD (psink));
2130 GST_OBJECT_LOCK (pbuf);
2131 pa_threaded_mainloop_lock (mainloop);
2133 if (!pbuf->context) {
2134 ret = gst_pad_get_pad_template_caps (GST_AUDIO_BASE_SINK_PAD (psink));
2138 ret = gst_caps_new_empty ();
2141 /* We're in PAUSED or higher */
2142 stream = pbuf->stream;
2144 } else if (pbuf->probe_stream) {
2145 /* We're not paused, but have a cached probe stream */
2146 stream = pbuf->probe_stream;
2149 /* We're not yet in PAUSED and still need to create a probe stream.
2151 * FIXME: PA doesn't accept "any" format. We fix something reasonable since
2152 * this is merely a probe. This should eventually be fixed in PA and
2153 * hard-coding the format should be dropped. */
2154 pa_format_info *format = pa_format_info_new ();
2155 format->encoding = PA_ENCODING_PCM;
2156 pa_format_info_set_sample_format (format, PA_SAMPLE_S16LE);
2157 pa_format_info_set_rate (format, GST_AUDIO_DEF_RATE);
2158 pa_format_info_set_channels (format, GST_AUDIO_DEF_CHANNELS);
2160 pbuf->probe_stream = gst_pulsesink_create_probe_stream (psink, pbuf,
2163 pa_format_info_free (format);
2165 if (!pbuf->probe_stream) {
2166 GST_WARNING_OBJECT (psink, "Could not create probe stream");
2170 stream = pbuf->probe_stream;
2173 if (!(o = pa_context_get_sink_info_by_name (pbuf->context,
2174 pa_stream_get_device_name (stream), gst_pulsesink_sink_info_cb,
2178 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2179 pa_threaded_mainloop_wait (mainloop);
2180 if (gst_pulsering_is_dead (psink, pbuf, FALSE))
2184 for (i = g_list_first (device_info.formats); i; i = g_list_next (i)) {
2185 gst_caps_append (ret,
2186 gst_pulse_format_info_to_caps ((pa_format_info *) i->data));
2190 pa_threaded_mainloop_unlock (mainloop);
2191 /* FIXME: this could be freed after device_name is got */
2192 GST_OBJECT_UNLOCK (pbuf);
2195 GstCaps *tmp = gst_caps_intersect_full (filter, ret,
2196 GST_CAPS_INTERSECT_FIRST);
2197 gst_caps_unref (ret);
2202 free_device_info (&device_info);
2205 pa_operation_unref (o);
2208 gst_object_unref (pbuf);
2210 GST_DEBUG_OBJECT (psink, "caps %" GST_PTR_FORMAT, ret);
2216 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2217 ("pa_context_get_sink_input_info() failed: %s",
2218 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2224 gst_pulsesink_query_acceptcaps (GstPulseSink * psink, GstCaps * caps)
2226 GstPulseRingBuffer *pbuf = NULL;
2227 GstPulseDeviceInfo device_info = { NULL, NULL };
2230 gboolean ret = FALSE;
2232 GstAudioRingBufferSpec spec = { 0 };
2233 pa_operation *o = NULL;
2234 pa_channel_map channel_map;
2235 pa_format_info *format = NULL;
2238 pad_caps = gst_pad_get_pad_template_caps (GST_BASE_SINK_PAD (psink));
2239 ret = gst_caps_is_subset (caps, pad_caps);
2240 gst_caps_unref (pad_caps);
2242 GST_DEBUG_OBJECT (psink, "caps %" GST_PTR_FORMAT, caps);
2244 /* Template caps didn't match */
2248 /* If we've not got fixed caps, creating a stream might fail, so let's just
2249 * return from here with default acceptcaps behaviour */
2250 if (!gst_caps_is_fixed (caps))
2253 GST_OBJECT_LOCK (psink);
2254 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2256 gst_object_ref (pbuf);
2257 GST_OBJECT_UNLOCK (psink);
2259 /* We're still in NULL state */
2263 GST_OBJECT_LOCK (pbuf);
2264 pa_threaded_mainloop_lock (mainloop);
2266 if (pbuf->context == NULL)
2271 spec.latency_time = GST_AUDIO_BASE_SINK (psink)->latency_time;
2272 if (!gst_audio_ring_buffer_parse_caps (&spec, caps))
2275 if (!gst_pulse_fill_format_info (&spec, &format, &channels))
2278 /* Make sure input is framed (one frame per buffer) and can be payloaded */
2279 if (!pa_format_info_is_pcm (format)) {
2280 gboolean framed = FALSE, parsed = FALSE;
2281 st = gst_caps_get_structure (caps, 0);
2283 gst_structure_get_boolean (st, "framed", &framed);
2284 gst_structure_get_boolean (st, "parsed", &parsed);
2285 if ((!framed && !parsed) || gst_audio_iec61937_frame_size (&spec) <= 0)
2289 /* initialize the channel map */
2290 if (pa_format_info_is_pcm (format) &&
2291 gst_pulse_gst_to_channel_map (&channel_map, &spec))
2292 pa_format_info_set_channel_map (format, &channel_map);
2294 if (pbuf->stream || pbuf->probe_stream) {
2295 /* We're already in PAUSED or above, so just reuse this stream to query
2296 * sink formats and use those. */
2298 const char *device_name = pa_stream_get_device_name (pbuf->stream ?
2299 pbuf->stream : pbuf->probe_stream);
2301 if (!(o = pa_context_get_sink_info_by_name (pbuf->context, device_name,
2302 gst_pulsesink_sink_info_cb, &device_info)))
2305 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2306 pa_threaded_mainloop_wait (mainloop);
2307 if (gst_pulsering_is_dead (psink, pbuf, FALSE))
2311 for (i = g_list_first (device_info.formats); i; i = g_list_next (i)) {
2312 if (pa_format_info_is_compatible ((pa_format_info *) i->data, format)) {
2318 /* We're in READY, let's connect a stream to see if the format is
2319 * accepted by whatever sink we're routed to */
2320 pbuf->probe_stream = gst_pulsesink_create_probe_stream (psink, pbuf,
2322 if (pbuf->probe_stream)
2328 pa_format_info_free (format);
2330 free_device_info (&device_info);
2333 pa_operation_unref (o);
2335 pa_threaded_mainloop_unlock (mainloop);
2336 GST_OBJECT_UNLOCK (pbuf);
2338 gst_caps_replace (&spec.caps, NULL);
2339 gst_object_unref (pbuf);
2347 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2348 ("pa_context_get_sink_input_info() failed: %s",
2349 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2355 gst_pulsesink_init (GstPulseSink * pulsesink)
2357 pulsesink->server = NULL;
2358 pulsesink->device = NULL;
2359 pulsesink->device_info.description = NULL;
2360 pulsesink->client_name = gst_pulse_client_name ();
2362 pulsesink->device_info.formats = NULL;
2364 pulsesink->volume = DEFAULT_VOLUME;
2365 pulsesink->volume_set = FALSE;
2367 pulsesink->mute = DEFAULT_MUTE;
2368 pulsesink->mute_set = FALSE;
2370 pulsesink->notify = 0;
2372 g_atomic_int_set (&pulsesink->format_lost, FALSE);
2373 pulsesink->format_lost_time = GST_CLOCK_TIME_NONE;
2375 pulsesink->properties = NULL;
2376 pulsesink->proplist = NULL;
2378 /* override with a custom clock */
2379 if (GST_AUDIO_BASE_SINK (pulsesink)->provided_clock)
2380 gst_object_unref (GST_AUDIO_BASE_SINK (pulsesink)->provided_clock);
2382 GST_AUDIO_BASE_SINK (pulsesink)->provided_clock =
2383 gst_audio_clock_new ("GstPulseSinkClock",
2384 (GstAudioClockGetTimeFunc) gst_pulsesink_get_time, pulsesink, NULL);
2388 gst_pulsesink_finalize (GObject * object)
2390 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2392 g_free (pulsesink->server);
2393 g_free (pulsesink->device);
2394 g_free (pulsesink->client_name);
2395 g_free (pulsesink->current_sink_name);
2397 free_device_info (&pulsesink->device_info);
2399 if (pulsesink->properties)
2400 gst_structure_free (pulsesink->properties);
2401 if (pulsesink->proplist)
2402 pa_proplist_free (pulsesink->proplist);
2404 G_OBJECT_CLASS (parent_class)->finalize (object);
2408 gst_pulsesink_set_volume (GstPulseSink * psink, gdouble volume)
2411 pa_operation *o = NULL;
2412 GstPulseRingBuffer *pbuf;
2418 pa_threaded_mainloop_lock (mainloop);
2420 GST_DEBUG_OBJECT (psink, "setting volume to %f", volume);
2422 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2423 if (pbuf == NULL || pbuf->stream == NULL)
2426 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2430 gst_pulse_cvolume_from_linear (&v, pbuf->channels, volume);
2432 /* FIXME: this will eventually be superceded by checks to see if the volume
2433 * is readable/writable */
2436 if (!(o = pa_context_set_sink_input_volume (pbuf->context, idx,
2440 /* We don't really care about the result of this call */
2444 pa_operation_unref (o);
2446 pa_threaded_mainloop_unlock (mainloop);
2453 psink->volume = volume;
2454 psink->volume_set = TRUE;
2456 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2461 psink->volume = volume;
2462 psink->volume_set = TRUE;
2464 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2469 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2474 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2475 ("pa_stream_set_sink_input_volume() failed: %s",
2476 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2482 gst_pulsesink_set_mute (GstPulseSink * psink, gboolean mute)
2484 pa_operation *o = NULL;
2485 GstPulseRingBuffer *pbuf;
2491 pa_threaded_mainloop_lock (mainloop);
2493 GST_DEBUG_OBJECT (psink, "setting mute state to %d", mute);
2495 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2496 if (pbuf == NULL || pbuf->stream == NULL)
2499 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2502 if (!(o = pa_context_set_sink_input_mute (pbuf->context, idx,
2506 /* We don't really care about the result of this call */
2510 pa_operation_unref (o);
2512 pa_threaded_mainloop_unlock (mainloop);
2520 psink->mute_set = TRUE;
2522 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2528 psink->mute_set = TRUE;
2530 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2535 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2540 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2541 ("pa_stream_set_sink_input_mute() failed: %s",
2542 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2548 gst_pulsesink_sink_input_info_cb (pa_context * c, const pa_sink_input_info * i,
2549 int eol, void *userdata)
2551 GstPulseRingBuffer *pbuf;
2552 GstPulseSink *psink;
2554 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
2555 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
2563 /* If the index doesn't match our current stream,
2564 * it implies we just recreated the stream (caps change)
2566 if (i->index == pa_stream_get_index (pbuf->stream)) {
2567 psink->volume = pa_sw_volume_to_linear (pa_cvolume_max (&i->volume));
2568 psink->mute = i->mute;
2569 psink->current_sink_idx = i->sink;
2571 if (psink->volume > MAX_VOLUME) {
2572 GST_WARNING_OBJECT (psink, "Clipped volume from %f to %f", psink->volume,
2574 psink->volume = MAX_VOLUME;
2579 pa_threaded_mainloop_signal (mainloop, 0);
2583 gst_pulsesink_get_sink_input_info (GstPulseSink * psink, gdouble * volume,
2586 GstPulseRingBuffer *pbuf;
2587 pa_operation *o = NULL;
2593 pa_threaded_mainloop_lock (mainloop);
2595 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2596 if (pbuf == NULL || pbuf->stream == NULL)
2599 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2602 if (!(o = pa_context_get_sink_input_info (pbuf->context, idx,
2603 gst_pulsesink_sink_input_info_cb, pbuf)))
2606 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2607 pa_threaded_mainloop_wait (mainloop);
2608 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
2614 *volume = psink->volume;
2616 *mute = psink->mute;
2619 pa_operation_unref (o);
2621 pa_threaded_mainloop_unlock (mainloop);
2629 *volume = psink->volume;
2631 *mute = psink->mute;
2633 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2638 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2643 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2648 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2649 ("pa_context_get_sink_input_info() failed: %s",
2650 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2656 gst_pulsesink_current_sink_info_cb (pa_context * c, const pa_sink_info * i,
2657 int eol, void *userdata)
2659 GstPulseSink *psink;
2661 psink = GST_PULSESINK_CAST (userdata);
2666 /* If the index doesn't match our current stream,
2667 * it implies we just recreated the stream (caps change)
2669 if (i->index == psink->current_sink_idx) {
2670 g_free (psink->current_sink_name);
2671 psink->current_sink_name = g_strdup (i->name);
2675 pa_threaded_mainloop_signal (mainloop, 0);
2679 gst_pulsesink_get_current_device (GstPulseSink * pulsesink)
2681 pa_operation *o = NULL;
2682 GstPulseRingBuffer *pbuf;
2683 gchar *current_sink;
2689 GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (pulsesink)->ringbuffer);
2690 if (pbuf == NULL || pbuf->stream == NULL)
2693 gst_pulsesink_get_sink_input_info (pulsesink, NULL, NULL);
2695 pa_threaded_mainloop_lock (mainloop);
2697 if (!(o = pa_context_get_sink_info_by_index (pbuf->context,
2698 pulsesink->current_sink_idx, gst_pulsesink_current_sink_info_cb,
2702 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2703 pa_threaded_mainloop_wait (mainloop);
2704 if (gst_pulsering_is_dead (pulsesink, pbuf, TRUE))
2710 current_sink = g_strdup (pulsesink->current_sink_name);
2713 pa_operation_unref (o);
2715 pa_threaded_mainloop_unlock (mainloop);
2717 return current_sink;
2722 GST_DEBUG_OBJECT (pulsesink, "we have no mainloop");
2727 GST_DEBUG_OBJECT (pulsesink, "we have no ringbuffer");
2732 GST_ELEMENT_ERROR (pulsesink, RESOURCE, FAILED,
2733 ("pa_context_get_sink_input_info() failed: %s",
2734 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2740 gst_pulsesink_device_description (GstPulseSink * psink)
2742 GstPulseRingBuffer *pbuf;
2743 pa_operation *o = NULL;
2749 pa_threaded_mainloop_lock (mainloop);
2750 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2754 free_device_info (&psink->device_info);
2755 if (!(o = pa_context_get_sink_info_by_name (pbuf->context,
2756 psink->device, gst_pulsesink_sink_info_cb, &psink->device_info)))
2759 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2760 pa_threaded_mainloop_wait (mainloop);
2761 if (gst_pulsering_is_dead (psink, pbuf, FALSE))
2767 pa_operation_unref (o);
2769 t = g_strdup (psink->device_info.description);
2770 pa_threaded_mainloop_unlock (mainloop);
2777 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2782 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2787 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2788 ("pa_context_get_sink_info_by_index() failed: %s",
2789 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2795 gst_pulsesink_set_stream_device (GstPulseSink * psink, const gchar * device)
2797 pa_operation *o = NULL;
2798 GstPulseRingBuffer *pbuf;
2804 pa_threaded_mainloop_lock (mainloop);
2806 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2807 if (pbuf == NULL || pbuf->stream == NULL)
2810 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2814 GST_DEBUG_OBJECT (psink, "setting stream device to %s", device);
2816 if (!(o = pa_context_move_sink_input_by_name (pbuf->context, idx, device,
2823 pa_operation_unref (o);
2825 pa_threaded_mainloop_unlock (mainloop);
2832 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2837 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2842 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2847 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2848 ("pa_context_move_sink_input_by_name(%s) failed: %s", device,
2849 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2856 gst_pulsesink_set_property (GObject * object,
2857 guint prop_id, const GValue * value, GParamSpec * pspec)
2859 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2863 g_free (pulsesink->server);
2864 pulsesink->server = g_value_dup_string (value);
2867 g_free (pulsesink->device);
2868 pulsesink->device = g_value_dup_string (value);
2869 gst_pulsesink_set_stream_device (pulsesink, pulsesink->device);
2872 gst_pulsesink_set_volume (pulsesink, g_value_get_double (value));
2875 gst_pulsesink_set_mute (pulsesink, g_value_get_boolean (value));
2877 case PROP_CLIENT_NAME:
2878 g_free (pulsesink->client_name);
2879 if (!g_value_get_string (value)) {
2880 GST_WARNING_OBJECT (pulsesink,
2881 "Empty PulseAudio client name not allowed. Resetting to default value");
2882 pulsesink->client_name = gst_pulse_client_name ();
2884 pulsesink->client_name = g_value_dup_string (value);
2886 case PROP_STREAM_PROPERTIES:
2887 if (pulsesink->properties)
2888 gst_structure_free (pulsesink->properties);
2889 pulsesink->properties =
2890 gst_structure_copy (gst_value_get_structure (value));
2891 if (pulsesink->proplist)
2892 pa_proplist_free (pulsesink->proplist);
2893 pulsesink->proplist = gst_pulse_make_proplist (pulsesink->properties);
2896 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2902 gst_pulsesink_get_property (GObject * object,
2903 guint prop_id, GValue * value, GParamSpec * pspec)
2906 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2910 g_value_set_string (value, pulsesink->server);
2913 g_value_set_string (value, pulsesink->device);
2915 case PROP_CURRENT_DEVICE:
2917 gchar *current_device = gst_pulsesink_get_current_device (pulsesink);
2919 g_value_take_string (value, current_device);
2921 g_value_set_string (value, "");
2924 case PROP_DEVICE_NAME:
2925 g_value_take_string (value, gst_pulsesink_device_description (pulsesink));
2931 gst_pulsesink_get_sink_input_info (pulsesink, &volume, NULL);
2932 g_value_set_double (value, volume);
2939 gst_pulsesink_get_sink_input_info (pulsesink, NULL, &mute);
2940 g_value_set_boolean (value, mute);
2943 case PROP_CLIENT_NAME:
2944 g_value_set_string (value, pulsesink->client_name);
2946 case PROP_STREAM_PROPERTIES:
2947 gst_value_set_structure (value, pulsesink->properties);
2950 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2956 gst_pulsesink_change_title (GstPulseSink * psink, const gchar * t)
2958 pa_operation *o = NULL;
2959 GstPulseRingBuffer *pbuf;
2961 pa_threaded_mainloop_lock (mainloop);
2963 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2965 if (pbuf == NULL || pbuf->stream == NULL)
2968 g_free (pbuf->stream_name);
2969 pbuf->stream_name = g_strdup (t);
2971 if (!(o = pa_stream_set_name (pbuf->stream, pbuf->stream_name, NULL, NULL)))
2974 /* We're not interested if this operation failed or not */
2978 pa_operation_unref (o);
2979 pa_threaded_mainloop_unlock (mainloop);
2986 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2991 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2992 ("pa_stream_set_name() failed: %s",
2993 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2999 gst_pulsesink_change_props (GstPulseSink * psink, GstTagList * l)
3001 static const gchar *const map[] = {
3002 GST_TAG_TITLE, PA_PROP_MEDIA_TITLE,
3004 /* might get overriden in the next iteration by GST_TAG_ARTIST */
3005 GST_TAG_PERFORMER, PA_PROP_MEDIA_ARTIST,
3007 GST_TAG_ARTIST, PA_PROP_MEDIA_ARTIST,
3008 GST_TAG_LANGUAGE_CODE, PA_PROP_MEDIA_LANGUAGE,
3009 GST_TAG_LOCATION, PA_PROP_MEDIA_FILENAME,
3010 /* We might add more here later on ... */
3013 pa_proplist *pl = NULL;
3014 const gchar *const *t;
3015 gboolean empty = TRUE;
3016 pa_operation *o = NULL;
3017 GstPulseRingBuffer *pbuf;
3019 pl = pa_proplist_new ();
3021 for (t = map; *t; t += 2) {
3024 if (gst_tag_list_get_string (l, *t, &n)) {
3027 pa_proplist_sets (pl, *(t + 1), n);
3037 pa_threaded_mainloop_lock (mainloop);
3038 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
3039 if (pbuf == NULL || pbuf->stream == NULL)
3042 /* We're not interested if this operation failed or not */
3043 if (!(o = pa_stream_proplist_update (pbuf->stream, PA_UPDATE_REPLACE,
3045 GST_DEBUG_OBJECT (psink, "pa_stream_proplist_update() failed");
3051 pa_operation_unref (o);
3053 pa_threaded_mainloop_unlock (mainloop);
3058 pa_proplist_free (pl);
3065 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
3071 gst_pulsesink_flush_ringbuffer (GstPulseSink * psink)
3073 GstPulseRingBuffer *pbuf;
3075 pa_threaded_mainloop_lock (mainloop);
3077 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
3079 if (pbuf == NULL || pbuf->stream == NULL)
3082 gst_pulsering_flush (pbuf);
3084 /* Uncork if we haven't already (happens when waiting to get enough data
3085 * to send out the first time) */
3087 gst_pulsering_set_corked (pbuf, FALSE, FALSE);
3089 /* We're not interested if this operation failed or not */
3091 pa_threaded_mainloop_unlock (mainloop);
3098 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
3104 gst_pulsesink_event (GstBaseSink * sink, GstEvent * event)
3106 GstPulseSink *pulsesink = GST_PULSESINK_CAST (sink);
3108 switch (GST_EVENT_TYPE (event)) {
3109 case GST_EVENT_TAG:{
3110 gchar *title = NULL, *artist = NULL, *location = NULL, *description =
3111 NULL, *t = NULL, *buf = NULL;
3114 gst_event_parse_tag (event, &l);
3116 gst_tag_list_get_string (l, GST_TAG_TITLE, &title);
3117 gst_tag_list_get_string (l, GST_TAG_ARTIST, &artist);
3118 gst_tag_list_get_string (l, GST_TAG_LOCATION, &location);
3119 gst_tag_list_get_string (l, GST_TAG_DESCRIPTION, &description);
3122 gst_tag_list_get_string (l, GST_TAG_PERFORMER, &artist);
3124 if (title && artist)
3125 /* TRANSLATORS: 'song title' by 'artist name' */
3126 t = buf = g_strdup_printf (_("'%s' by '%s'"), g_strstrip (title),
3127 g_strstrip (artist));
3129 t = g_strstrip (title);
3130 else if (description)
3131 t = g_strstrip (description);
3133 t = g_strstrip (location);
3136 gst_pulsesink_change_title (pulsesink, t);
3141 g_free (description);
3144 gst_pulsesink_change_props (pulsesink, l);
3148 case GST_EVENT_GAP:{
3149 GstClockTime timestamp, duration;
3151 gst_event_parse_gap (event, ×tamp, &duration);
3152 if (duration == GST_CLOCK_TIME_NONE)
3153 gst_pulsesink_flush_ringbuffer (pulsesink);
3157 gst_pulsesink_flush_ringbuffer (pulsesink);
3163 return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
3167 gst_pulsesink_query (GstBaseSink * sink, GstQuery * query)
3169 GstPulseSink *pulsesink = GST_PULSESINK_CAST (sink);
3170 gboolean ret = FALSE;
3172 switch (GST_QUERY_TYPE (query)) {
3173 case GST_QUERY_CAPS:
3175 GstCaps *caps, *filter;
3177 gst_query_parse_caps (query, &filter);
3178 caps = gst_pulsesink_query_getcaps (pulsesink, filter);
3181 gst_query_set_caps_result (query, caps);
3182 gst_caps_unref (caps);
3187 case GST_QUERY_ACCEPT_CAPS:
3191 gst_query_parse_accept_caps (query, &caps);
3192 ret = gst_pulsesink_query_acceptcaps (pulsesink, caps);
3193 gst_query_set_accept_caps_result (query, ret);
3198 ret = GST_BASE_SINK_CLASS (parent_class)->query (sink, query);
3205 gst_pulsesink_release_mainloop (GstPulseSink * psink)
3210 pa_threaded_mainloop_lock (mainloop);
3211 while (psink->defer_pending) {
3212 GST_DEBUG_OBJECT (psink, "waiting for stream status message emission");
3213 pa_threaded_mainloop_wait (mainloop);
3215 pa_threaded_mainloop_unlock (mainloop);
3217 g_mutex_lock (&pa_shared_resource_mutex);
3219 if (!mainloop_ref_ct) {
3220 GST_INFO_OBJECT (psink, "terminating pa main loop thread");
3221 pa_threaded_mainloop_stop (mainloop);
3222 pa_threaded_mainloop_free (mainloop);
3225 g_mutex_unlock (&pa_shared_resource_mutex);
3228 static GstStateChangeReturn
3229 gst_pulsesink_change_state (GstElement * element, GstStateChange transition)
3231 GstPulseSink *pulsesink = GST_PULSESINK (element);
3232 GstStateChangeReturn ret;
3234 switch (transition) {
3235 case GST_STATE_CHANGE_NULL_TO_READY:
3236 g_mutex_lock (&pa_shared_resource_mutex);
3237 if (!mainloop_ref_ct) {
3238 GST_INFO_OBJECT (element, "new pa main loop thread");
3239 if (!(mainloop = pa_threaded_mainloop_new ()))
3240 goto mainloop_failed;
3241 if (pa_threaded_mainloop_start (mainloop) < 0) {
3242 pa_threaded_mainloop_free (mainloop);
3243 goto mainloop_start_failed;
3245 mainloop_ref_ct = 1;
3246 g_mutex_unlock (&pa_shared_resource_mutex);
3248 GST_INFO_OBJECT (element, "reusing pa main loop thread");
3250 g_mutex_unlock (&pa_shared_resource_mutex);
3253 case GST_STATE_CHANGE_READY_TO_PAUSED:
3254 gst_element_post_message (element,
3255 gst_message_new_clock_provide (GST_OBJECT_CAST (element),
3256 GST_AUDIO_BASE_SINK (pulsesink)->provided_clock, TRUE));
3263 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3264 if (ret == GST_STATE_CHANGE_FAILURE)
3267 switch (transition) {
3268 case GST_STATE_CHANGE_PAUSED_TO_READY:
3269 /* format_lost is reset in release() in audiobasesink */
3270 gst_element_post_message (element,
3271 gst_message_new_clock_lost (GST_OBJECT_CAST (element),
3272 GST_AUDIO_BASE_SINK (pulsesink)->provided_clock));
3274 case GST_STATE_CHANGE_READY_TO_NULL:
3275 gst_pulsesink_release_mainloop (pulsesink);
3286 g_mutex_unlock (&pa_shared_resource_mutex);
3287 GST_ELEMENT_ERROR (pulsesink, RESOURCE, FAILED,
3288 ("pa_threaded_mainloop_new() failed"), (NULL));
3289 return GST_STATE_CHANGE_FAILURE;
3291 mainloop_start_failed:
3293 g_mutex_unlock (&pa_shared_resource_mutex);
3294 GST_ELEMENT_ERROR (pulsesink, RESOURCE, FAILED,
3295 ("pa_threaded_mainloop_start() failed"), (NULL));
3296 return GST_STATE_CHANGE_FAILURE;
3300 if (transition == GST_STATE_CHANGE_NULL_TO_READY) {
3301 /* Clear the PA mainloop if audiobasesink failed to open the ring_buffer */
3302 g_assert (mainloop);
3303 gst_pulsesink_release_mainloop (pulsesink);