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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 * SECTION:element-pulsesink
26 * @see_also: pulsesrc, pulsemixer
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 -v filesrc location=sine.ogg ! oggdemux ! vorbisdec ! audioconvert ! audioresample ! pulsesink
35 * ]| Play an Ogg/Vorbis file.
37 * gst-launch -v audiotestsrc ! audioconvert ! volume volume=0.4 ! pulsesink
38 * ]| Play a 440Hz sine wave.
40 * gst-launch -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/streamvolume.h>
56 #include <gst/gst-i18n-plugin.h>
57 #include <gst/audio/gstaudioiec61937.h>
59 #include <gst/pbutils/pbutils.h> /* only used for GST_PLUGINS_BASE_VERSION_* */
61 #include "pulsesink.h"
62 #include "pulseutil.h"
64 GST_DEBUG_CATEGORY_EXTERN (pulse_debug);
65 #define GST_CAT_DEFAULT pulse_debug
67 #define DEFAULT_SERVER NULL
68 #define DEFAULT_DEVICE NULL
69 #define DEFAULT_DEVICE_NAME NULL
70 #define DEFAULT_VOLUME 1.0
71 #define DEFAULT_MUTE FALSE
72 #define MAX_VOLUME 10.0
83 PROP_STREAM_PROPERTIES,
87 #define GST_TYPE_PULSERING_BUFFER \
88 (gst_pulseringbuffer_get_type())
89 #define GST_PULSERING_BUFFER(obj) \
90 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_PULSERING_BUFFER,GstPulseRingBuffer))
91 #define GST_PULSERING_BUFFER_CLASS(klass) \
92 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_PULSERING_BUFFER,GstPulseRingBufferClass))
93 #define GST_PULSERING_BUFFER_GET_CLASS(obj) \
94 (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_PULSERING_BUFFER, GstPulseRingBufferClass))
95 #define GST_PULSERING_BUFFER_CAST(obj) \
96 ((GstPulseRingBuffer *)obj)
97 #define GST_IS_PULSERING_BUFFER(obj) \
98 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_PULSERING_BUFFER))
99 #define GST_IS_PULSERING_BUFFER_CLASS(klass)\
100 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_PULSERING_BUFFER))
102 typedef struct _GstPulseRingBuffer GstPulseRingBuffer;
103 typedef struct _GstPulseRingBufferClass GstPulseRingBufferClass;
105 typedef struct _GstPulseContext GstPulseContext;
107 /* Store the PA contexts in a hash table to allow easy sharing among
108 * multiple instances of the sink. Keys are $context_name@$server_name
109 * (strings) and values should be GstPulseContext pointers.
111 struct _GstPulseContext
114 GSList *ring_buffers;
117 static GHashTable *gst_pulse_shared_contexts = NULL;
119 /* use one static main-loop for all instances
120 * this is needed to make the context sharing work as the contexts are
121 * released when releasing their parent main-loop
123 static pa_threaded_mainloop *mainloop = NULL;
124 static guint mainloop_ref_ct = 0;
126 /* lock for access to shared resources */
127 static GMutex *pa_shared_resource_mutex = NULL;
129 /* We keep a custom ringbuffer that is backed up by data allocated by
130 * pulseaudio. We must also overide the commit function to write into
131 * pulseaudio memory instead. */
132 struct _GstPulseRingBuffer
134 GstAudioRingBuffer object;
142 pa_format_info *format;
153 gboolean in_commit:1;
156 struct _GstPulseRingBufferClass
158 GstAudioRingBufferClass parent_class;
161 static GType gst_pulseringbuffer_get_type (void);
162 static void gst_pulseringbuffer_finalize (GObject * object);
164 static GstAudioRingBufferClass *ring_parent_class = NULL;
166 static gboolean gst_pulseringbuffer_open_device (GstAudioRingBuffer * buf);
167 static gboolean gst_pulseringbuffer_close_device (GstAudioRingBuffer * buf);
168 static gboolean gst_pulseringbuffer_acquire (GstAudioRingBuffer * buf,
169 GstAudioRingBufferSpec * spec);
170 static gboolean gst_pulseringbuffer_release (GstAudioRingBuffer * buf);
171 static gboolean gst_pulseringbuffer_start (GstAudioRingBuffer * buf);
172 static gboolean gst_pulseringbuffer_pause (GstAudioRingBuffer * buf);
173 static gboolean gst_pulseringbuffer_stop (GstAudioRingBuffer * buf);
174 static void gst_pulseringbuffer_clear (GstAudioRingBuffer * buf);
175 static guint gst_pulseringbuffer_commit (GstAudioRingBuffer * buf,
176 guint64 * sample, guchar * data, gint in_samples, gint out_samples,
179 G_DEFINE_TYPE (GstPulseRingBuffer, gst_pulseringbuffer,
180 GST_TYPE_AUDIO_RING_BUFFER);
183 gst_pulsesink_init_contexts (void)
185 g_assert (pa_shared_resource_mutex == NULL);
186 pa_shared_resource_mutex = g_mutex_new ();
187 gst_pulse_shared_contexts = g_hash_table_new_full (g_str_hash, g_str_equal,
192 gst_pulseringbuffer_class_init (GstPulseRingBufferClass * klass)
194 GObjectClass *gobject_class;
195 GstAudioRingBufferClass *gstringbuffer_class;
197 gobject_class = (GObjectClass *) klass;
198 gstringbuffer_class = (GstAudioRingBufferClass *) klass;
200 ring_parent_class = g_type_class_peek_parent (klass);
202 gobject_class->finalize = gst_pulseringbuffer_finalize;
204 gstringbuffer_class->open_device =
205 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_open_device);
206 gstringbuffer_class->close_device =
207 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_close_device);
208 gstringbuffer_class->acquire =
209 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_acquire);
210 gstringbuffer_class->release =
211 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_release);
212 gstringbuffer_class->start = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_start);
213 gstringbuffer_class->pause = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_pause);
214 gstringbuffer_class->resume = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_start);
215 gstringbuffer_class->stop = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_stop);
216 gstringbuffer_class->clear_all =
217 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_clear);
219 gstringbuffer_class->commit = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_commit);
223 gst_pulseringbuffer_init (GstPulseRingBuffer * pbuf)
225 pbuf->stream_name = NULL;
226 pbuf->context = NULL;
231 pbuf->is_pcm = FALSE;
235 pbuf->m_writable = 0;
237 pbuf->m_lastoffset = 0;
240 pbuf->in_commit = FALSE;
241 pbuf->paused = FALSE;
245 gst_pulsering_destroy_stream (GstPulseRingBuffer * pbuf)
250 /* drop shm memory buffer */
251 pa_stream_cancel_write (pbuf->stream);
253 /* reset internal variables */
256 pbuf->m_writable = 0;
258 pbuf->m_lastoffset = 0;
261 pa_format_info_free (pbuf->format);
264 pbuf->is_pcm = FALSE;
267 pa_stream_disconnect (pbuf->stream);
269 /* Make sure we don't get any further callbacks */
270 pa_stream_set_state_callback (pbuf->stream, NULL, NULL);
271 pa_stream_set_write_callback (pbuf->stream, NULL, NULL);
272 pa_stream_set_underflow_callback (pbuf->stream, NULL, NULL);
273 pa_stream_set_overflow_callback (pbuf->stream, NULL, NULL);
275 pa_stream_unref (pbuf->stream);
279 g_free (pbuf->stream_name);
280 pbuf->stream_name = NULL;
284 gst_pulsering_destroy_context (GstPulseRingBuffer * pbuf)
286 g_mutex_lock (pa_shared_resource_mutex);
288 GST_DEBUG_OBJECT (pbuf, "destroying ringbuffer %p", pbuf);
290 gst_pulsering_destroy_stream (pbuf);
293 pa_context_unref (pbuf->context);
294 pbuf->context = NULL;
297 if (pbuf->context_name) {
298 GstPulseContext *pctx;
300 pctx = g_hash_table_lookup (gst_pulse_shared_contexts, pbuf->context_name);
302 GST_DEBUG_OBJECT (pbuf, "releasing context with name %s, pbuf=%p, pctx=%p",
303 pbuf->context_name, pbuf, pctx);
306 pctx->ring_buffers = g_slist_remove (pctx->ring_buffers, pbuf);
307 if (pctx->ring_buffers == NULL) {
308 GST_DEBUG_OBJECT (pbuf,
309 "destroying final context with name %s, pbuf=%p, pctx=%p",
310 pbuf->context_name, pbuf, pctx);
312 pa_context_disconnect (pctx->context);
314 /* Make sure we don't get any further callbacks */
315 pa_context_set_state_callback (pctx->context, NULL, NULL);
316 pa_context_set_subscribe_callback (pctx->context, NULL, NULL);
318 g_hash_table_remove (gst_pulse_shared_contexts, pbuf->context_name);
320 pa_context_unref (pctx->context);
321 g_slice_free (GstPulseContext, pctx);
324 g_free (pbuf->context_name);
325 pbuf->context_name = NULL;
327 g_mutex_unlock (pa_shared_resource_mutex);
331 gst_pulseringbuffer_finalize (GObject * object)
333 GstPulseRingBuffer *ringbuffer;
335 ringbuffer = GST_PULSERING_BUFFER_CAST (object);
337 gst_pulsering_destroy_context (ringbuffer);
338 G_OBJECT_CLASS (ring_parent_class)->finalize (object);
342 #define CONTEXT_OK(c) ((c) && PA_CONTEXT_IS_GOOD (pa_context_get_state ((c))))
343 #define STREAM_OK(s) ((s) && PA_STREAM_IS_GOOD (pa_stream_get_state ((s))))
346 gst_pulsering_is_dead (GstPulseSink * psink, GstPulseRingBuffer * pbuf,
347 gboolean check_stream)
349 if (!CONTEXT_OK (pbuf->context))
352 if (check_stream && !STREAM_OK (pbuf->stream))
359 const gchar *err_str =
360 pbuf->context ? pa_strerror (pa_context_errno (pbuf->context)) : NULL;
361 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED, ("Disconnected: %s",
368 gst_pulsering_context_state_cb (pa_context * c, void *userdata)
370 pa_context_state_t state;
371 pa_threaded_mainloop *mainloop = (pa_threaded_mainloop *) userdata;
373 state = pa_context_get_state (c);
375 GST_LOG ("got new context state %d", state);
378 case PA_CONTEXT_READY:
379 case PA_CONTEXT_TERMINATED:
380 case PA_CONTEXT_FAILED:
381 GST_LOG ("signaling");
382 pa_threaded_mainloop_signal (mainloop, 0);
385 case PA_CONTEXT_UNCONNECTED:
386 case PA_CONTEXT_CONNECTING:
387 case PA_CONTEXT_AUTHORIZING:
388 case PA_CONTEXT_SETTING_NAME:
394 gst_pulsering_context_subscribe_cb (pa_context * c,
395 pa_subscription_event_type_t t, uint32_t idx, void *userdata)
398 GstPulseContext *pctx = (GstPulseContext *) userdata;
401 if (t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT | PA_SUBSCRIPTION_EVENT_CHANGE) &&
402 t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT | PA_SUBSCRIPTION_EVENT_NEW))
405 for (walk = pctx->ring_buffers; walk; walk = g_slist_next (walk)) {
406 GstPulseRingBuffer *pbuf = (GstPulseRingBuffer *) walk->data;
407 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
409 GST_LOG_OBJECT (psink, "type %d, idx %u", t, idx);
414 if (idx != pa_stream_get_index (pbuf->stream))
417 if (psink->device && pbuf->is_pcm &&
418 !g_str_equal (psink->device,
419 pa_stream_get_device_name (pbuf->stream))) {
420 /* Underlying sink changed. And this is not a passthrough stream. Let's
421 * see if someone upstream wants to try to renegotiate. */
424 g_free (psink->device);
425 psink->device = g_strdup (pa_stream_get_device_name (pbuf->stream));
427 GST_INFO_OBJECT (psink, "emitting sink-changed");
429 renego = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
430 gst_structure_new_empty ("pulse-sink-changed"));
432 if (!gst_pad_push_event (GST_BASE_SINK (psink)->sinkpad, renego))
433 GST_DEBUG_OBJECT (psink, "Emitted sink-changed - nobody was listening");
436 /* Actually this event is also triggered when other properties of
437 * the stream change that are unrelated to the volume. However it is
438 * probably cheaper to signal the change here and check for the
439 * volume when the GObject property is read instead of querying it always. */
441 /* inform streaming thread to notify */
442 g_atomic_int_compare_and_exchange (&psink->notify, 0, 1);
446 /* will be called when the device should be opened. In this case we will connect
447 * to the server. We should not try to open any streams in this state. */
449 gst_pulseringbuffer_open_device (GstAudioRingBuffer * buf)
452 GstPulseRingBuffer *pbuf;
453 GstPulseContext *pctx;
454 pa_mainloop_api *api;
455 gboolean need_unlock_shared;
457 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
458 pbuf = GST_PULSERING_BUFFER_CAST (buf);
460 g_assert (!pbuf->stream);
461 g_assert (psink->client_name);
464 pbuf->context_name = g_strdup_printf ("%s@%s", psink->client_name,
467 pbuf->context_name = g_strdup (psink->client_name);
469 pa_threaded_mainloop_lock (mainloop);
471 g_mutex_lock (pa_shared_resource_mutex);
472 need_unlock_shared = TRUE;
474 pctx = g_hash_table_lookup (gst_pulse_shared_contexts, pbuf->context_name);
476 pctx = g_slice_new0 (GstPulseContext);
478 /* get the mainloop api and create a context */
479 GST_INFO_OBJECT (psink, "new context with name %s, pbuf=%p, pctx=%p",
480 pbuf->context_name, pbuf, pctx);
481 api = pa_threaded_mainloop_get_api (mainloop);
482 if (!(pctx->context = pa_context_new (api, pbuf->context_name)))
485 pctx->ring_buffers = g_slist_prepend (pctx->ring_buffers, pbuf);
486 g_hash_table_insert (gst_pulse_shared_contexts,
487 g_strdup (pbuf->context_name), (gpointer) pctx);
488 /* register some essential callbacks */
489 pa_context_set_state_callback (pctx->context,
490 gst_pulsering_context_state_cb, mainloop);
491 pa_context_set_subscribe_callback (pctx->context,
492 gst_pulsering_context_subscribe_cb, pctx);
494 /* try to connect to the server and wait for completion, we don't want to
495 * autospawn a deamon */
496 GST_LOG_OBJECT (psink, "connect to server %s",
497 GST_STR_NULL (psink->server));
498 if (pa_context_connect (pctx->context, psink->server,
499 PA_CONTEXT_NOAUTOSPAWN, NULL) < 0)
502 GST_INFO_OBJECT (psink,
503 "reusing shared context with name %s, pbuf=%p, pctx=%p",
504 pbuf->context_name, pbuf, pctx);
505 pctx->ring_buffers = g_slist_prepend (pctx->ring_buffers, pbuf);
508 g_mutex_unlock (pa_shared_resource_mutex);
509 need_unlock_shared = FALSE;
511 /* context created or shared okay */
512 pbuf->context = pa_context_ref (pctx->context);
515 pa_context_state_t state;
517 state = pa_context_get_state (pbuf->context);
519 GST_LOG_OBJECT (psink, "context state is now %d", state);
521 if (!PA_CONTEXT_IS_GOOD (state))
524 if (state == PA_CONTEXT_READY)
527 /* Wait until the context is ready */
528 GST_LOG_OBJECT (psink, "waiting..");
529 pa_threaded_mainloop_wait (mainloop);
532 GST_LOG_OBJECT (psink, "opened the device");
534 pa_threaded_mainloop_unlock (mainloop);
541 if (need_unlock_shared)
542 g_mutex_unlock (pa_shared_resource_mutex);
543 gst_pulsering_destroy_context (pbuf);
544 pa_threaded_mainloop_unlock (mainloop);
549 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
550 ("Failed to create context"), (NULL));
551 g_slice_free (GstPulseContext, pctx);
552 goto unlock_and_fail;
556 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED, ("Failed to connect: %s",
557 pa_strerror (pa_context_errno (pctx->context))), (NULL));
558 goto unlock_and_fail;
562 /* close the device */
564 gst_pulseringbuffer_close_device (GstAudioRingBuffer * buf)
567 GstPulseRingBuffer *pbuf;
569 pbuf = GST_PULSERING_BUFFER_CAST (buf);
570 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
572 GST_LOG_OBJECT (psink, "closing device");
574 pa_threaded_mainloop_lock (mainloop);
575 gst_pulsering_destroy_context (pbuf);
576 pa_threaded_mainloop_unlock (mainloop);
578 GST_LOG_OBJECT (psink, "closed device");
584 gst_pulsering_stream_state_cb (pa_stream * s, void *userdata)
587 GstPulseRingBuffer *pbuf;
588 pa_stream_state_t state;
590 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
591 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
593 state = pa_stream_get_state (s);
594 GST_LOG_OBJECT (psink, "got new stream state %d", state);
597 case PA_STREAM_READY:
598 case PA_STREAM_FAILED:
599 case PA_STREAM_TERMINATED:
600 GST_LOG_OBJECT (psink, "signaling");
601 pa_threaded_mainloop_signal (mainloop, 0);
603 case PA_STREAM_UNCONNECTED:
604 case PA_STREAM_CREATING:
610 gst_pulsering_stream_request_cb (pa_stream * s, size_t length, void *userdata)
613 GstAudioRingBuffer *rbuf;
614 GstPulseRingBuffer *pbuf;
616 rbuf = GST_AUDIO_RING_BUFFER_CAST (userdata);
617 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
618 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
620 GST_LOG_OBJECT (psink, "got request for length %" G_GSIZE_FORMAT, length);
622 if (pbuf->in_commit && (length >= rbuf->spec.segsize)) {
623 /* only signal when we are waiting in the commit thread
624 * and got request for atleast a segment */
625 pa_threaded_mainloop_signal (mainloop, 0);
630 gst_pulsering_stream_underflow_cb (pa_stream * s, void *userdata)
633 GstPulseRingBuffer *pbuf;
635 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
636 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
638 GST_WARNING_OBJECT (psink, "Got underflow");
642 gst_pulsering_stream_overflow_cb (pa_stream * s, void *userdata)
645 GstPulseRingBuffer *pbuf;
647 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
648 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
650 GST_WARNING_OBJECT (psink, "Got overflow");
654 gst_pulsering_stream_latency_cb (pa_stream * s, void *userdata)
657 GstPulseRingBuffer *pbuf;
658 const pa_timing_info *info;
661 info = pa_stream_get_timing_info (s);
663 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
664 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
667 GST_LOG_OBJECT (psink, "latency update (information unknown)");
670 sink_usec = info->configured_sink_usec;
672 GST_LOG_OBJECT (psink,
673 "latency_update, %" G_GUINT64_FORMAT ", %d:%" G_GINT64_FORMAT ", %d:%"
674 G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT,
675 GST_TIMEVAL_TO_TIME (info->timestamp), info->write_index_corrupt,
676 info->write_index, info->read_index_corrupt, info->read_index,
677 info->sink_usec, sink_usec);
681 gst_pulsering_stream_suspended_cb (pa_stream * p, void *userdata)
684 GstPulseRingBuffer *pbuf;
686 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
687 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
689 if (pa_stream_is_suspended (p))
690 GST_DEBUG_OBJECT (psink, "stream suspended");
692 GST_DEBUG_OBJECT (psink, "stream resumed");
696 gst_pulsering_stream_started_cb (pa_stream * p, void *userdata)
699 GstPulseRingBuffer *pbuf;
701 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
702 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
704 GST_DEBUG_OBJECT (psink, "stream started");
708 gst_pulsering_stream_event_cb (pa_stream * p, const char *name,
709 pa_proplist * pl, void *userdata)
712 GstPulseRingBuffer *pbuf;
714 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
715 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
717 if (!strcmp (name, PA_STREAM_EVENT_REQUEST_CORK)) {
718 /* the stream wants to PAUSE, post a message for the application. */
719 GST_DEBUG_OBJECT (psink, "got request for CORK");
720 gst_element_post_message (GST_ELEMENT_CAST (psink),
721 gst_message_new_request_state (GST_OBJECT_CAST (psink),
724 } else if (!strcmp (name, PA_STREAM_EVENT_REQUEST_UNCORK)) {
725 GST_DEBUG_OBJECT (psink, "got request for UNCORK");
726 gst_element_post_message (GST_ELEMENT_CAST (psink),
727 gst_message_new_request_state (GST_OBJECT_CAST (psink),
729 } else if (!strcmp (name, PA_STREAM_EVENT_FORMAT_LOST)) {
732 if (g_atomic_int_get (&psink->format_lost)) {
733 /* Duplicate event before we're done reconfiguring, discard */
737 GST_DEBUG_OBJECT (psink, "got FORMAT LOST");
738 g_atomic_int_set (&psink->format_lost, 1);
739 psink->format_lost_time = g_ascii_strtoull (pa_proplist_gets (pl,
740 "stream-time"), NULL, 0) * 1000;
742 g_free (psink->device);
743 psink->device = g_strdup (pa_proplist_gets (pl, "device"));
745 renego = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
746 gst_structure_new_empty ("pulse-format-lost"));
748 if (!gst_pad_push_event (GST_BASE_SINK (psink)->sinkpad, renego)) {
749 /* Nobody handled the format change - emit an error */
750 GST_ELEMENT_ERROR (psink, STREAM, FORMAT, ("Sink format changed"),
751 ("Sink format changed"));
754 GST_DEBUG_OBJECT (psink, "got unknown event %s", name);
758 /* Called with the mainloop locked */
760 gst_pulsering_wait_for_stream_ready (GstPulseSink * psink, pa_stream * stream)
762 pa_stream_state_t state;
765 state = pa_stream_get_state (stream);
767 GST_LOG_OBJECT (psink, "stream state is now %d", state);
769 if (!PA_STREAM_IS_GOOD (state))
772 if (state == PA_STREAM_READY)
775 /* Wait until the stream is ready */
776 pa_threaded_mainloop_wait (mainloop);
781 /* This method should create a new stream of the given @spec. No playback should
782 * start yet so we start in the corked state. */
784 gst_pulseringbuffer_acquire (GstAudioRingBuffer * buf,
785 GstAudioRingBufferSpec * spec)
788 GstPulseRingBuffer *pbuf;
789 pa_buffer_attr wanted;
790 const pa_buffer_attr *actual;
791 pa_channel_map channel_map;
792 pa_operation *o = NULL;
794 pa_cvolume *pv = NULL;
795 pa_stream_flags_t flags;
797 GstAudioClock *clock;
798 pa_format_info *formats[1];
799 #ifndef GST_DISABLE_GST_DEBUG
800 gchar print_buf[PA_FORMAT_INFO_SNPRINT_MAX];
803 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
804 pbuf = GST_PULSERING_BUFFER_CAST (buf);
806 GST_LOG_OBJECT (psink, "creating sample spec");
807 /* convert the gstreamer sample spec to the pulseaudio format */
808 if (!gst_pulse_fill_format_info (spec, &pbuf->format, &pbuf->channels))
810 pbuf->is_pcm = pa_format_info_is_pcm (pbuf->format);
812 pa_threaded_mainloop_lock (mainloop);
814 /* we need a context and a no stream */
815 g_assert (pbuf->context);
816 g_assert (!pbuf->stream);
818 /* enable event notifications */
819 GST_LOG_OBJECT (psink, "subscribing to context events");
820 if (!(o = pa_context_subscribe (pbuf->context,
821 PA_SUBSCRIPTION_MASK_SINK_INPUT, NULL, NULL)))
822 goto subscribe_failed;
824 pa_operation_unref (o);
826 /* initialize the channel map */
827 if (pbuf->is_pcm && gst_pulse_gst_to_channel_map (&channel_map, spec))
828 pa_format_info_set_channel_map (pbuf->format, &channel_map);
830 /* find a good name for the stream */
831 if (psink->stream_name)
832 name = psink->stream_name;
834 name = "Playback Stream";
836 /* create a stream */
837 formats[0] = pbuf->format;
838 if (!(pbuf->stream = pa_stream_new_extended (pbuf->context, name, formats, 1,
842 /* install essential callbacks */
843 pa_stream_set_state_callback (pbuf->stream,
844 gst_pulsering_stream_state_cb, pbuf);
845 pa_stream_set_write_callback (pbuf->stream,
846 gst_pulsering_stream_request_cb, pbuf);
847 pa_stream_set_underflow_callback (pbuf->stream,
848 gst_pulsering_stream_underflow_cb, pbuf);
849 pa_stream_set_overflow_callback (pbuf->stream,
850 gst_pulsering_stream_overflow_cb, pbuf);
851 pa_stream_set_latency_update_callback (pbuf->stream,
852 gst_pulsering_stream_latency_cb, pbuf);
853 pa_stream_set_suspended_callback (pbuf->stream,
854 gst_pulsering_stream_suspended_cb, pbuf);
855 pa_stream_set_started_callback (pbuf->stream,
856 gst_pulsering_stream_started_cb, pbuf);
857 pa_stream_set_event_callback (pbuf->stream,
858 gst_pulsering_stream_event_cb, pbuf);
860 /* buffering requirements. When setting prebuf to 0, the stream will not pause
861 * when we cause an underrun, which causes time to continue. */
862 memset (&wanted, 0, sizeof (wanted));
863 wanted.tlength = spec->segtotal * spec->segsize;
864 wanted.maxlength = -1;
866 wanted.minreq = spec->segsize;
868 GST_INFO_OBJECT (psink, "tlength: %d", wanted.tlength);
869 GST_INFO_OBJECT (psink, "maxlength: %d", wanted.maxlength);
870 GST_INFO_OBJECT (psink, "prebuf: %d", wanted.prebuf);
871 GST_INFO_OBJECT (psink, "minreq: %d", wanted.minreq);
873 /* configure volume when we changed it, else we leave the default */
874 if (psink->volume_set) {
875 GST_LOG_OBJECT (psink, "have volume of %f", psink->volume);
878 gst_pulse_cvolume_from_linear (pv, pbuf->channels, psink->volume);
880 GST_DEBUG_OBJECT (psink, "passthrough stream, not setting volume");
887 /* construct the flags */
888 flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE |
889 PA_STREAM_ADJUST_LATENCY | PA_STREAM_START_CORKED;
891 if (psink->mute_set && psink->mute)
892 flags |= PA_STREAM_START_MUTED;
894 /* we always start corked (see flags above) */
897 /* try to connect now */
898 GST_LOG_OBJECT (psink, "connect for playback to device %s",
899 GST_STR_NULL (psink->device));
900 if (pa_stream_connect_playback (pbuf->stream, psink->device,
901 &wanted, flags, pv, NULL) < 0)
904 /* our clock will now start from 0 again */
905 clock = GST_AUDIO_CLOCK (GST_AUDIO_BASE_SINK (psink)->provided_clock);
906 gst_audio_clock_reset (clock, 0);
908 if (!gst_pulsering_wait_for_stream_ready (psink, pbuf->stream))
911 g_free (psink->device);
912 psink->device = g_strdup (pa_stream_get_device_name (pbuf->stream));
914 #ifndef GST_DISABLE_GST_DEBUG
915 pa_format_info_snprint (print_buf, sizeof (print_buf),
916 pa_stream_get_format_info (pbuf->stream));
917 GST_INFO_OBJECT (psink, "negotiated to: %s", print_buf);
920 /* After we passed the volume off of to PA we never want to set it
921 again, since it is PA's job to save/restore volumes. */
922 psink->volume_set = psink->mute_set = FALSE;
924 GST_LOG_OBJECT (psink, "stream is acquired now");
926 /* get the actual buffering properties now */
927 actual = pa_stream_get_buffer_attr (pbuf->stream);
929 GST_INFO_OBJECT (psink, "tlength: %d (wanted: %d)", actual->tlength,
931 GST_INFO_OBJECT (psink, "maxlength: %d", actual->maxlength);
932 GST_INFO_OBJECT (psink, "prebuf: %d", actual->prebuf);
933 GST_INFO_OBJECT (psink, "minreq: %d (wanted %d)", actual->minreq,
936 spec->segsize = actual->minreq;
937 spec->segtotal = actual->tlength / spec->segsize;
939 pa_threaded_mainloop_unlock (mainloop);
946 gst_pulsering_destroy_stream (pbuf);
947 pa_threaded_mainloop_unlock (mainloop);
953 GST_ELEMENT_ERROR (psink, RESOURCE, SETTINGS,
954 ("Invalid sample specification."), (NULL));
959 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
960 ("pa_context_subscribe() failed: %s",
961 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
962 goto unlock_and_fail;
966 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
967 ("Failed to create stream: %s",
968 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
969 goto unlock_and_fail;
973 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
974 ("Failed to connect stream: %s",
975 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
976 goto unlock_and_fail;
980 /* free the stream that we acquired before */
982 gst_pulseringbuffer_release (GstAudioRingBuffer * buf)
984 GstPulseRingBuffer *pbuf;
986 pbuf = GST_PULSERING_BUFFER_CAST (buf);
988 pa_threaded_mainloop_lock (mainloop);
989 gst_pulsering_destroy_stream (pbuf);
990 pa_threaded_mainloop_unlock (mainloop);
995 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
996 g_atomic_int_set (&psink->format_lost, FALSE);
997 psink->format_lost_time = GST_CLOCK_TIME_NONE;
1004 gst_pulsering_success_cb (pa_stream * s, int success, void *userdata)
1006 pa_threaded_mainloop_signal (mainloop, 0);
1009 /* update the corked state of a stream, must be called with the mainloop
1012 gst_pulsering_set_corked (GstPulseRingBuffer * pbuf, gboolean corked,
1015 pa_operation *o = NULL;
1016 GstPulseSink *psink;
1017 gboolean res = FALSE;
1019 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1021 if (g_atomic_int_get (&psink->format_lost)) {
1022 /* Sink format changed, stream's gone so fake being paused */
1026 GST_DEBUG_OBJECT (psink, "setting corked state to %d", corked);
1027 if (pbuf->corked != corked) {
1028 if (!(o = pa_stream_cork (pbuf->stream, corked,
1029 gst_pulsering_success_cb, pbuf)))
1032 while (wait && pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1033 pa_threaded_mainloop_wait (mainloop);
1034 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
1037 pbuf->corked = corked;
1039 GST_DEBUG_OBJECT (psink, "skipping, already in requested state");
1045 pa_operation_unref (o);
1052 GST_DEBUG_OBJECT (psink, "the server is dead");
1057 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1058 ("pa_stream_cork() failed: %s",
1059 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1065 gst_pulseringbuffer_clear (GstAudioRingBuffer * buf)
1067 GstPulseSink *psink;
1068 GstPulseRingBuffer *pbuf;
1069 pa_operation *o = NULL;
1071 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1072 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1074 pa_threaded_mainloop_lock (mainloop);
1075 GST_DEBUG_OBJECT (psink, "clearing");
1077 /* don't wait for the flush to complete */
1078 if ((o = pa_stream_flush (pbuf->stream, NULL, pbuf)))
1079 pa_operation_unref (o);
1081 pa_threaded_mainloop_unlock (mainloop);
1084 /* called from pulse with the mainloop lock */
1086 mainloop_enter_defer_cb (pa_mainloop_api * api, void *userdata)
1088 GstPulseSink *pulsesink = GST_PULSESINK (userdata);
1089 GstMessage *message;
1092 g_value_init (&val, G_TYPE_POINTER);
1093 g_value_set_pointer (&val, g_thread_self ());
1095 GST_DEBUG_OBJECT (pulsesink, "posting ENTER stream status");
1096 message = gst_message_new_stream_status (GST_OBJECT (pulsesink),
1097 GST_STREAM_STATUS_TYPE_ENTER, GST_ELEMENT (pulsesink));
1098 gst_message_set_stream_status_object (message, &val);
1100 gst_element_post_message (GST_ELEMENT (pulsesink), message);
1102 g_return_if_fail (pulsesink->defer_pending);
1103 pulsesink->defer_pending--;
1104 pa_threaded_mainloop_signal (mainloop, 0);
1107 /* start/resume playback ASAP, we don't uncork here but in the commit method */
1109 gst_pulseringbuffer_start (GstAudioRingBuffer * buf)
1111 GstPulseSink *psink;
1112 GstPulseRingBuffer *pbuf;
1114 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1115 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1117 pa_threaded_mainloop_lock (mainloop);
1119 GST_DEBUG_OBJECT (psink, "scheduling stream status");
1120 psink->defer_pending++;
1121 pa_mainloop_api_once (pa_threaded_mainloop_get_api (mainloop),
1122 mainloop_enter_defer_cb, psink);
1124 GST_DEBUG_OBJECT (psink, "starting");
1125 pbuf->paused = FALSE;
1127 /* EOS needs running clock */
1128 if (GST_BASE_SINK_CAST (psink)->eos ||
1129 g_atomic_int_get (&GST_AUDIO_BASE_SINK (psink)->eos_rendering))
1130 gst_pulsering_set_corked (pbuf, FALSE, FALSE);
1132 pa_threaded_mainloop_unlock (mainloop);
1137 /* pause/stop playback ASAP */
1139 gst_pulseringbuffer_pause (GstAudioRingBuffer * buf)
1141 GstPulseSink *psink;
1142 GstPulseRingBuffer *pbuf;
1145 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1146 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1148 pa_threaded_mainloop_lock (mainloop);
1149 GST_DEBUG_OBJECT (psink, "pausing and corking");
1150 /* make sure the commit method stops writing */
1151 pbuf->paused = TRUE;
1152 res = gst_pulsering_set_corked (pbuf, TRUE, TRUE);
1153 if (pbuf->in_commit) {
1154 /* we are waiting in a commit, signal */
1155 GST_DEBUG_OBJECT (psink, "signal commit");
1156 pa_threaded_mainloop_signal (mainloop, 0);
1158 pa_threaded_mainloop_unlock (mainloop);
1163 /* called from pulse with the mainloop lock */
1165 mainloop_leave_defer_cb (pa_mainloop_api * api, void *userdata)
1167 GstPulseSink *pulsesink = GST_PULSESINK (userdata);
1168 GstMessage *message;
1171 g_value_init (&val, G_TYPE_POINTER);
1172 g_value_set_pointer (&val, g_thread_self ());
1174 GST_DEBUG_OBJECT (pulsesink, "posting LEAVE stream status");
1175 message = gst_message_new_stream_status (GST_OBJECT (pulsesink),
1176 GST_STREAM_STATUS_TYPE_LEAVE, GST_ELEMENT (pulsesink));
1177 gst_message_set_stream_status_object (message, &val);
1178 gst_element_post_message (GST_ELEMENT (pulsesink), message);
1180 g_return_if_fail (pulsesink->defer_pending);
1181 pulsesink->defer_pending--;
1182 pa_threaded_mainloop_signal (mainloop, 0);
1185 /* stop playback, we flush everything. */
1187 gst_pulseringbuffer_stop (GstAudioRingBuffer * buf)
1189 GstPulseSink *psink;
1190 GstPulseRingBuffer *pbuf;
1191 gboolean res = FALSE;
1192 pa_operation *o = NULL;
1194 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1195 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1197 pa_threaded_mainloop_lock (mainloop);
1199 pbuf->paused = TRUE;
1200 res = gst_pulsering_set_corked (pbuf, TRUE, TRUE);
1202 /* Inform anyone waiting in _commit() call that it shall wakeup */
1203 if (pbuf->in_commit) {
1204 GST_DEBUG_OBJECT (psink, "signal commit thread");
1205 pa_threaded_mainloop_signal (mainloop, 0);
1207 if (g_atomic_int_get (&psink->format_lost)) {
1208 /* Don't try to flush, the stream's probably gone by now */
1213 /* then try to flush, it's not fatal when this fails */
1214 GST_DEBUG_OBJECT (psink, "flushing");
1215 if ((o = pa_stream_flush (pbuf->stream, gst_pulsering_success_cb, pbuf))) {
1216 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1217 GST_DEBUG_OBJECT (psink, "wait for completion");
1218 pa_threaded_mainloop_wait (mainloop);
1219 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
1222 GST_DEBUG_OBJECT (psink, "flush completed");
1228 pa_operation_cancel (o);
1229 pa_operation_unref (o);
1232 GST_DEBUG_OBJECT (psink, "scheduling stream status");
1233 psink->defer_pending++;
1234 pa_mainloop_api_once (pa_threaded_mainloop_get_api (mainloop),
1235 mainloop_leave_defer_cb, psink);
1237 pa_threaded_mainloop_unlock (mainloop);
1244 GST_DEBUG_OBJECT (psink, "the server is dead");
1249 /* in_samples >= out_samples, rate > 1.0 */
1250 #define FWD_UP_SAMPLES(s,se,d,de) \
1252 guint8 *sb = s, *db = d; \
1253 while (s <= se && d < de) { \
1254 memcpy (d, s, bpf); \
1257 if ((*accum << 1) >= inr) { \
1262 in_samples -= (s - sb)/bpf; \
1263 out_samples -= (d - db)/bpf; \
1264 GST_DEBUG ("fwd_up end %d/%d",*accum,*toprocess); \
1267 /* out_samples > in_samples, for rates smaller than 1.0 */
1268 #define FWD_DOWN_SAMPLES(s,se,d,de) \
1270 guint8 *sb = s, *db = d; \
1271 while (s <= se && d < de) { \
1272 memcpy (d, s, bpf); \
1275 if ((*accum << 1) >= outr) { \
1280 in_samples -= (s - sb)/bpf; \
1281 out_samples -= (d - db)/bpf; \
1282 GST_DEBUG ("fwd_down end %d/%d",*accum,*toprocess); \
1285 #define REV_UP_SAMPLES(s,se,d,de) \
1287 guint8 *sb = se, *db = d; \
1288 while (s <= se && d < de) { \
1289 memcpy (d, se, bpf); \
1292 while (d < de && (*accum << 1) >= inr) { \
1297 in_samples -= (sb - se)/bpf; \
1298 out_samples -= (d - db)/bpf; \
1299 GST_DEBUG ("rev_up end %d/%d",*accum,*toprocess); \
1302 #define REV_DOWN_SAMPLES(s,se,d,de) \
1304 guint8 *sb = se, *db = d; \
1305 while (s <= se && d < de) { \
1306 memcpy (d, se, bpf); \
1309 while (s <= se && (*accum << 1) >= outr) { \
1314 in_samples -= (sb - se)/bpf; \
1315 out_samples -= (d - db)/bpf; \
1316 GST_DEBUG ("rev_down end %d/%d",*accum,*toprocess); \
1319 /* our custom commit function because we write into the buffer of pulseaudio
1320 * instead of keeping our own buffer */
1322 gst_pulseringbuffer_commit (GstAudioRingBuffer * buf, guint64 * sample,
1323 guchar * data, gint in_samples, gint out_samples, gint * accum)
1325 GstPulseSink *psink;
1326 GstPulseRingBuffer *pbuf;
1331 gint inr, outr, bpf;
1335 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1336 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1338 /* FIXME post message rather than using a signal (as mixer interface) */
1339 if (g_atomic_int_compare_and_exchange (&psink->notify, 1, 0)) {
1340 g_object_notify (G_OBJECT (psink), "volume");
1341 g_object_notify (G_OBJECT (psink), "mute");
1344 /* make sure the ringbuffer is started */
1345 if (G_UNLIKELY (g_atomic_int_get (&buf->state) !=
1346 GST_AUDIO_RING_BUFFER_STATE_STARTED)) {
1347 /* see if we are allowed to start it */
1348 if (G_UNLIKELY (g_atomic_int_get (&buf->may_start) == FALSE))
1351 GST_DEBUG_OBJECT (buf, "start!");
1352 if (!gst_audio_ring_buffer_start (buf))
1356 pa_threaded_mainloop_lock (mainloop);
1358 GST_DEBUG_OBJECT (psink, "entering commit");
1359 pbuf->in_commit = TRUE;
1361 bpf = GST_AUDIO_INFO_BPF (&buf->spec.info);
1362 bufsize = buf->spec.segsize * buf->spec.segtotal;
1364 /* our toy resampler for trick modes */
1365 reverse = out_samples < 0;
1366 out_samples = ABS (out_samples);
1368 if (in_samples >= out_samples)
1369 toprocess = &in_samples;
1371 toprocess = &out_samples;
1373 inr = in_samples - 1;
1374 outr = out_samples - 1;
1376 GST_DEBUG_OBJECT (psink, "in %d, out %d", inr, outr);
1378 /* data_end points to the last sample we have to write, not past it. This is
1379 * needed to properly handle reverse playback: it points to the last sample. */
1380 data_end = data + (bpf * inr);
1382 if (g_atomic_int_get (&psink->format_lost)) {
1383 /* Sink format changed, drop the data and hope upstream renegotiates */
1390 /* offset is in bytes */
1391 offset = *sample * bpf;
1393 while (*toprocess > 0) {
1397 GST_LOG_OBJECT (psink,
1398 "need to write %d samples at offset %" G_GINT64_FORMAT, *toprocess,
1401 if (offset != pbuf->m_lastoffset)
1402 GST_LOG_OBJECT (psink, "discontinuity, offset is %" G_GINT64_FORMAT ", "
1403 "last offset was %" G_GINT64_FORMAT, offset, pbuf->m_lastoffset);
1405 towrite = out_samples * bpf;
1407 /* Wait for at least segsize bytes to become available */
1408 if (towrite > buf->spec.segsize)
1409 towrite = buf->spec.segsize;
1411 if ((pbuf->m_writable < towrite) || (offset != pbuf->m_lastoffset)) {
1412 /* if no room left or discontinuity in offset,
1413 we need to flush data and get a new buffer */
1415 /* flush the buffer if possible */
1416 if ((pbuf->m_data != NULL) && (pbuf->m_towrite > 0)) {
1418 GST_LOG_OBJECT (psink,
1419 "flushing %u samples at offset %" G_GINT64_FORMAT,
1420 (guint) pbuf->m_towrite / bpf, pbuf->m_offset);
1422 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1423 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1427 pbuf->m_towrite = 0;
1428 pbuf->m_offset = offset; /* keep track of current offset */
1430 /* get a buffer to write in for now on */
1432 pbuf->m_writable = pa_stream_writable_size (pbuf->stream);
1434 if (g_atomic_int_get (&psink->format_lost)) {
1435 /* Sink format changed, give up and hope upstream renegotiates */
1439 if (pbuf->m_writable == (size_t) - 1)
1440 goto writable_size_failed;
1442 pbuf->m_writable /= bpf;
1443 pbuf->m_writable *= bpf; /* handle only complete samples */
1445 if (pbuf->m_writable >= towrite)
1448 /* see if we need to uncork because we have no free space */
1450 if (!gst_pulsering_set_corked (pbuf, FALSE, FALSE))
1454 /* we can't write segsize bytes, wait a bit */
1455 GST_LOG_OBJECT (psink, "waiting for free space");
1456 pa_threaded_mainloop_wait (mainloop);
1462 /* Recalculate what we can write in the next chunk */
1463 towrite = out_samples * bpf;
1464 if (pbuf->m_writable > towrite)
1465 pbuf->m_writable = towrite;
1467 GST_LOG_OBJECT (psink, "requesting %" G_GSIZE_FORMAT " bytes of "
1468 "shared memory", pbuf->m_writable);
1470 if (pa_stream_begin_write (pbuf->stream, &pbuf->m_data,
1471 &pbuf->m_writable) < 0) {
1472 GST_LOG_OBJECT (psink, "pa_stream_begin_write() failed");
1473 goto writable_size_failed;
1476 GST_LOG_OBJECT (psink, "got %" G_GSIZE_FORMAT " bytes of shared memory",
1481 if (towrite > pbuf->m_writable)
1482 towrite = pbuf->m_writable;
1483 avail = towrite / bpf;
1485 GST_LOG_OBJECT (psink, "writing %u samples at offset %" G_GUINT64_FORMAT,
1486 (guint) avail, offset);
1488 /* No trick modes for passthrough streams */
1489 if (G_UNLIKELY (!pbuf->is_pcm && (inr != outr || reverse))) {
1490 GST_WARNING_OBJECT (psink, "Passthrough stream can't run in trick mode");
1491 goto unlock_and_fail;
1494 if (G_LIKELY (inr == outr && !reverse)) {
1495 /* no rate conversion, simply write out the samples */
1496 /* copy the data into internal buffer */
1498 memcpy ((guint8 *) pbuf->m_data + pbuf->m_towrite, data, towrite);
1499 pbuf->m_towrite += towrite;
1500 pbuf->m_writable -= towrite;
1503 in_samples -= avail;
1504 out_samples -= avail;
1506 guint8 *dest, *d, *d_end;
1508 /* write into the PulseAudio shm buffer */
1509 dest = d = (guint8 *) pbuf->m_data + pbuf->m_towrite;
1510 d_end = d + towrite;
1514 /* forward speed up */
1515 FWD_UP_SAMPLES (data, data_end, d, d_end);
1517 /* forward slow down */
1518 FWD_DOWN_SAMPLES (data, data_end, d, d_end);
1521 /* reverse speed up */
1522 REV_UP_SAMPLES (data, data_end, d, d_end);
1524 /* reverse slow down */
1525 REV_DOWN_SAMPLES (data, data_end, d, d_end);
1527 /* see what we have left to write */
1528 towrite = (d - dest);
1529 pbuf->m_towrite += towrite;
1530 pbuf->m_writable -= towrite;
1532 avail = towrite / bpf;
1535 /* flush the buffer if it's full */
1536 if ((pbuf->m_data != NULL) && (pbuf->m_towrite > 0)
1537 && (pbuf->m_writable == 0)) {
1538 GST_LOG_OBJECT (psink, "flushing %u samples at offset %" G_GINT64_FORMAT,
1539 (guint) pbuf->m_towrite / bpf, pbuf->m_offset);
1541 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1542 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1545 pbuf->m_towrite = 0;
1546 pbuf->m_offset = offset + towrite; /* keep track of current offset */
1550 offset += avail * bpf;
1551 pbuf->m_lastoffset = offset;
1553 /* check if we need to uncork after writing the samples */
1555 const pa_timing_info *info;
1557 if ((info = pa_stream_get_timing_info (pbuf->stream))) {
1558 GST_LOG_OBJECT (psink,
1559 "read_index at %" G_GUINT64_FORMAT ", offset %" G_GINT64_FORMAT,
1560 info->read_index, offset);
1562 /* we uncork when the read_index is too far behind the offset we need
1564 if (info->read_index + bufsize <= offset) {
1565 if (!gst_pulsering_set_corked (pbuf, FALSE, FALSE))
1569 GST_LOG_OBJECT (psink, "no timing info available yet");
1575 /* we consumed all samples here */
1576 data = data_end + bpf;
1578 pbuf->in_commit = FALSE;
1579 pa_threaded_mainloop_unlock (mainloop);
1582 result = inr - ((data_end - data) / bpf);
1583 GST_LOG_OBJECT (psink, "wrote %d samples", result);
1590 pbuf->in_commit = FALSE;
1591 GST_LOG_OBJECT (psink, "we are reset");
1592 pa_threaded_mainloop_unlock (mainloop);
1597 GST_LOG_OBJECT (psink, "we can not start");
1602 GST_LOG_OBJECT (psink, "failed to start the ringbuffer");
1607 pbuf->in_commit = FALSE;
1608 GST_ERROR_OBJECT (psink, "uncork failed");
1609 pa_threaded_mainloop_unlock (mainloop);
1614 pbuf->in_commit = FALSE;
1615 GST_LOG_OBJECT (psink, "we are paused");
1616 pa_threaded_mainloop_unlock (mainloop);
1619 writable_size_failed:
1621 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1622 ("pa_stream_writable_size() failed: %s",
1623 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1624 goto unlock_and_fail;
1628 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1629 ("pa_stream_write() failed: %s",
1630 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1631 goto unlock_and_fail;
1635 /* write pending local samples, must be called with the mainloop lock */
1637 gst_pulsering_flush (GstPulseRingBuffer * pbuf)
1639 GstPulseSink *psink;
1641 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1642 GST_DEBUG_OBJECT (psink, "entering flush");
1644 /* flush the buffer if possible */
1645 if (pbuf->stream && (pbuf->m_data != NULL) && (pbuf->m_towrite > 0)) {
1646 #ifndef GST_DISABLE_GST_DEBUG
1649 bpf = (GST_AUDIO_RING_BUFFER_CAST (pbuf))->spec.info.bpf;
1650 GST_LOG_OBJECT (psink,
1651 "flushing %u samples at offset %" G_GINT64_FORMAT,
1652 (guint) pbuf->m_towrite / bpf, pbuf->m_offset);
1655 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1656 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1660 pbuf->m_towrite = 0;
1661 pbuf->m_offset += pbuf->m_towrite; /* keep track of current offset */
1670 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1671 ("pa_stream_write() failed: %s",
1672 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1677 static void gst_pulsesink_set_property (GObject * object, guint prop_id,
1678 const GValue * value, GParamSpec * pspec);
1679 static void gst_pulsesink_get_property (GObject * object, guint prop_id,
1680 GValue * value, GParamSpec * pspec);
1681 static void gst_pulsesink_finalize (GObject * object);
1683 static gboolean gst_pulsesink_event (GstBaseSink * sink, GstEvent * event);
1684 static gboolean gst_pulsesink_query (GstBaseSink * sink, GstQuery * query);
1686 static GstStateChangeReturn gst_pulsesink_change_state (GstElement * element,
1687 GstStateChange transition);
1689 static GstStaticPadTemplate pad_template = GST_STATIC_PAD_TEMPLATE ("sink",
1692 GST_STATIC_CAPS (PULSE_SINK_TEMPLATE_CAPS));
1694 #define gst_pulsesink_parent_class parent_class
1695 G_DEFINE_TYPE_WITH_CODE (GstPulseSink, gst_pulsesink, GST_TYPE_AUDIO_BASE_SINK,
1696 gst_pulsesink_init_contexts ();
1697 G_IMPLEMENT_INTERFACE (GST_TYPE_STREAM_VOLUME, NULL)
1700 static GstAudioRingBuffer *
1701 gst_pulsesink_create_ringbuffer (GstAudioBaseSink * sink)
1703 GstAudioRingBuffer *buffer;
1705 GST_DEBUG_OBJECT (sink, "creating ringbuffer");
1706 buffer = g_object_new (GST_TYPE_PULSERING_BUFFER, NULL);
1707 GST_DEBUG_OBJECT (sink, "created ringbuffer @%p", buffer);
1713 gst_pulsesink_payload (GstAudioBaseSink * sink, GstBuffer * buf)
1715 switch (sink->ringbuffer->spec.type) {
1716 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3:
1717 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_EAC3:
1718 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS:
1719 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_MPEG:
1721 /* FIXME: alloc memory from PA if possible */
1722 gint framesize = gst_audio_iec61937_frame_size (&sink->ringbuffer->spec);
1724 guint8 *indata, *outdata;
1725 gsize insize, outsize;
1731 out = gst_buffer_new_and_alloc (framesize);
1733 indata = gst_buffer_map (buf, &insize, NULL, GST_MAP_READ);
1734 outdata = gst_buffer_map (out, &outsize, NULL, GST_MAP_WRITE);
1736 res = gst_audio_iec61937_payload (indata, insize,
1737 outdata, outsize, &sink->ringbuffer->spec);
1739 gst_buffer_unmap (buf, indata, insize);
1740 gst_buffer_unmap (out, outdata, outsize);
1743 gst_buffer_unref (out);
1747 gst_buffer_copy_into (out, buf, GST_BUFFER_COPY_METADATA, 0, -1);
1752 return gst_buffer_ref (buf);
1757 gst_pulsesink_class_init (GstPulseSinkClass * klass)
1759 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1760 GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
1761 GstBaseSinkClass *bc;
1762 GstAudioBaseSinkClass *gstaudiosink_class = GST_AUDIO_BASE_SINK_CLASS (klass);
1763 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1766 gobject_class->finalize = gst_pulsesink_finalize;
1767 gobject_class->set_property = gst_pulsesink_set_property;
1768 gobject_class->get_property = gst_pulsesink_get_property;
1770 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_pulsesink_event);
1771 gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_pulsesink_query);
1773 /* restore the original basesink pull methods */
1774 bc = g_type_class_peek (GST_TYPE_BASE_SINK);
1775 gstbasesink_class->activate_pull = GST_DEBUG_FUNCPTR (bc->activate_pull);
1777 gstelement_class->change_state =
1778 GST_DEBUG_FUNCPTR (gst_pulsesink_change_state);
1780 gstaudiosink_class->create_ringbuffer =
1781 GST_DEBUG_FUNCPTR (gst_pulsesink_create_ringbuffer);
1782 gstaudiosink_class->payload = GST_DEBUG_FUNCPTR (gst_pulsesink_payload);
1784 /* Overwrite GObject fields */
1785 g_object_class_install_property (gobject_class,
1787 g_param_spec_string ("server", "Server",
1788 "The PulseAudio server to connect to", DEFAULT_SERVER,
1789 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1791 g_object_class_install_property (gobject_class, PROP_DEVICE,
1792 g_param_spec_string ("device", "Device",
1793 "The PulseAudio sink device to connect to", DEFAULT_DEVICE,
1794 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1796 g_object_class_install_property (gobject_class,
1798 g_param_spec_string ("device-name", "Device name",
1799 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
1800 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1802 g_object_class_install_property (gobject_class,
1804 g_param_spec_double ("volume", "Volume",
1805 "Linear volume of this stream, 1.0=100%", 0.0, MAX_VOLUME,
1806 DEFAULT_VOLUME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1807 g_object_class_install_property (gobject_class,
1809 g_param_spec_boolean ("mute", "Mute",
1810 "Mute state of this stream", DEFAULT_MUTE,
1811 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1814 * GstPulseSink:client-name
1816 * The PulseAudio client name to use.
1818 clientname = gst_pulse_client_name ();
1819 g_object_class_install_property (gobject_class,
1821 g_param_spec_string ("client-name", "Client Name",
1822 "The PulseAudio client name to use", clientname,
1823 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1824 GST_PARAM_MUTABLE_READY));
1825 g_free (clientname);
1828 * GstPulseSink:stream-properties
1830 * List of pulseaudio stream properties. A list of defined properties can be
1831 * found in the <ulink url="http://0pointer.de/lennart/projects/pulseaudio/doxygen/proplist_8h.html">pulseaudio api docs</ulink>.
1833 * Below is an example for registering as a music application to pulseaudio.
1835 * GstStructure *props;
1837 * props = gst_structure_from_string ("props,media.role=music", NULL);
1838 * g_object_set (pulse, "stream-properties", props, NULL);
1839 * gst_structure_free
1844 g_object_class_install_property (gobject_class,
1845 PROP_STREAM_PROPERTIES,
1846 g_param_spec_boxed ("stream-properties", "stream properties",
1847 "list of pulseaudio stream properties",
1848 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1850 gst_element_class_set_details_simple (gstelement_class,
1851 "PulseAudio Audio Sink",
1852 "Sink/Audio", "Plays audio to a PulseAudio server", "Lennart Poettering");
1853 gst_element_class_add_pad_template (gstelement_class,
1854 gst_static_pad_template_get (&pad_template));
1857 /* returns the current time of the sink ringbuffer */
1859 gst_pulsesink_get_time (GstClock * clock, GstAudioBaseSink * sink)
1861 GstPulseSink *psink;
1862 GstPulseRingBuffer *pbuf;
1865 if (!sink->ringbuffer || !sink->ringbuffer->acquired)
1866 return GST_CLOCK_TIME_NONE;
1868 pbuf = GST_PULSERING_BUFFER_CAST (sink->ringbuffer);
1869 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1871 if (g_atomic_int_get (&psink->format_lost)) {
1872 /* Stream was lost in a format change, it'll get set up again once
1873 * upstream renegotiates */
1874 return psink->format_lost_time;
1877 pa_threaded_mainloop_lock (mainloop);
1878 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
1881 /* if we don't have enough data to get a timestamp, just return NONE, which
1882 * will return the last reported time */
1883 if (pa_stream_get_time (pbuf->stream, &time) < 0) {
1884 GST_DEBUG_OBJECT (psink, "could not get time");
1885 time = GST_CLOCK_TIME_NONE;
1888 pa_threaded_mainloop_unlock (mainloop);
1890 GST_LOG_OBJECT (psink, "current time is %" GST_TIME_FORMAT,
1891 GST_TIME_ARGS (time));
1898 GST_DEBUG_OBJECT (psink, "the server is dead");
1899 pa_threaded_mainloop_unlock (mainloop);
1901 return GST_CLOCK_TIME_NONE;
1906 gst_pulsesink_sink_info_cb (pa_context * c, const pa_sink_info * i, int eol,
1909 GstPulseRingBuffer *pbuf;
1910 GstPulseSink *psink;
1914 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
1915 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1920 g_free (psink->device_description);
1921 psink->device_description = g_strdup (i->description);
1923 g_mutex_lock (psink->sink_formats_lock);
1925 for (l = g_list_first (psink->sink_formats); l; l = g_list_next (l))
1926 pa_format_info_free ((pa_format_info *) l->data);
1928 g_list_free (psink->sink_formats);
1929 psink->sink_formats = NULL;
1931 for (j = 0; j < i->n_formats; j++)
1932 psink->sink_formats = g_list_prepend (psink->sink_formats,
1933 pa_format_info_copy (i->formats[j]));
1935 g_mutex_unlock (psink->sink_formats_lock);
1938 pa_threaded_mainloop_signal (mainloop, 0);
1941 /* NOTE: If you're making changes here, see if pulseaudiosink acceptcaps also
1942 * needs to be changed accordingly. */
1944 gst_pulsesink_query_acceptcaps (GstPulseSink * psink, GstCaps * caps)
1946 GstPulseRingBuffer *pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK
1947 (psink)->ringbuffer);
1948 GstPad *pad = GST_BASE_SINK_PAD (psink);
1951 gboolean ret = FALSE;
1953 GstAudioRingBufferSpec spec = { 0 };
1954 pa_stream *stream = NULL;
1955 pa_operation *o = NULL;
1956 pa_channel_map channel_map;
1957 pa_stream_flags_t flags;
1958 pa_format_info *format = NULL, *formats[1];
1961 pad_caps = gst_pad_query_caps (pad, caps);
1962 ret = pad_caps != NULL;
1963 gst_caps_unref (pad_caps);
1965 /* Either template caps didn't match, or we're still in NULL state */
1966 if (!ret || !pbuf->context)
1969 /* If we've not got fixed caps, creating a stream might fail, so let's just
1970 * return from here with default acceptcaps behaviour */
1971 if (!gst_caps_is_fixed (caps))
1976 pa_threaded_mainloop_lock (mainloop);
1978 spec.latency_time = GST_AUDIO_BASE_SINK (psink)->latency_time;
1979 if (!gst_audio_ring_buffer_parse_caps (&spec, caps))
1982 if (!gst_pulse_fill_format_info (&spec, &format, &channels))
1985 /* Make sure input is framed (one frame per buffer) and can be payloaded */
1986 if (!pa_format_info_is_pcm (format)) {
1987 gboolean framed = FALSE, parsed = FALSE;
1988 st = gst_caps_get_structure (caps, 0);
1990 gst_structure_get_boolean (st, "framed", &framed);
1991 gst_structure_get_boolean (st, "parsed", &parsed);
1992 if ((!framed && !parsed) || gst_audio_iec61937_frame_size (&spec) <= 0)
1996 /* initialize the channel map */
1997 if (pa_format_info_is_pcm (format) &&
1998 gst_pulse_gst_to_channel_map (&channel_map, &spec))
1999 pa_format_info_set_channel_map (format, &channel_map);
2002 /* We're already in PAUSED or above, so just reuse this stream to query
2003 * sink formats and use those. */
2006 if (!(o = pa_context_get_sink_info_by_name (pbuf->context, psink->device,
2007 gst_pulsesink_sink_info_cb, pbuf)))
2010 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2011 pa_threaded_mainloop_wait (mainloop);
2012 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
2016 g_mutex_lock (psink->sink_formats_lock);
2017 for (i = g_list_first (psink->sink_formats); i; i = g_list_next (i)) {
2018 if (pa_format_info_is_compatible ((pa_format_info *) i->data, format)) {
2023 g_mutex_unlock (psink->sink_formats_lock);
2025 /* We're in READY, let's connect a stream to see if the format is
2026 * accpeted by whatever sink we're routed to */
2027 formats[0] = format;
2029 if (!(stream = pa_stream_new_extended (pbuf->context, "pulsesink probe",
2030 formats, 1, psink->proplist)))
2033 /* construct the flags */
2034 flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE |
2035 PA_STREAM_ADJUST_LATENCY | PA_STREAM_START_CORKED;
2037 pa_stream_set_state_callback (stream, gst_pulsering_stream_state_cb, pbuf);
2039 if (pa_stream_connect_playback (stream, psink->device, NULL, flags, NULL,
2043 ret = gst_pulsering_wait_for_stream_ready (psink, stream);
2048 pa_format_info_free (format);
2051 pa_operation_unref (o);
2054 pa_stream_set_state_callback (stream, NULL, NULL);
2055 pa_stream_disconnect (stream);
2056 pa_stream_unref (stream);
2059 pa_threaded_mainloop_unlock (mainloop);
2066 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2067 ("pa_context_get_sink_input_info() failed: %s",
2068 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2074 gst_pulsesink_init (GstPulseSink * pulsesink)
2076 pulsesink->server = NULL;
2077 pulsesink->device = NULL;
2078 pulsesink->device_description = NULL;
2079 pulsesink->client_name = gst_pulse_client_name ();
2081 pulsesink->sink_formats_lock = g_mutex_new ();
2082 pulsesink->sink_formats = NULL;
2084 pulsesink->volume = DEFAULT_VOLUME;
2085 pulsesink->volume_set = FALSE;
2087 pulsesink->mute = DEFAULT_MUTE;
2088 pulsesink->mute_set = FALSE;
2090 pulsesink->notify = 0;
2092 g_atomic_int_set (&pulsesink->format_lost, FALSE);
2093 pulsesink->format_lost_time = GST_CLOCK_TIME_NONE;
2095 pulsesink->properties = NULL;
2096 pulsesink->proplist = NULL;
2098 /* override with a custom clock */
2099 if (GST_AUDIO_BASE_SINK (pulsesink)->provided_clock)
2100 gst_object_unref (GST_AUDIO_BASE_SINK (pulsesink)->provided_clock);
2102 GST_AUDIO_BASE_SINK (pulsesink)->provided_clock =
2103 gst_audio_clock_new ("GstPulseSinkClock",
2104 (GstAudioClockGetTimeFunc) gst_pulsesink_get_time, pulsesink, NULL);
2106 /* TRUE for sinks, FALSE for sources */
2107 pulsesink->probe = gst_pulseprobe_new (G_OBJECT (pulsesink),
2108 G_OBJECT_GET_CLASS (pulsesink), PROP_DEVICE, pulsesink->device,
2113 gst_pulsesink_finalize (GObject * object)
2115 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2118 g_free (pulsesink->server);
2119 g_free (pulsesink->device);
2120 g_free (pulsesink->device_description);
2121 g_free (pulsesink->client_name);
2123 for (i = g_list_first (pulsesink->sink_formats); i; i = g_list_next (i))
2124 pa_format_info_free ((pa_format_info *) i->data);
2126 g_list_free (pulsesink->sink_formats);
2127 g_mutex_free (pulsesink->sink_formats_lock);
2129 if (pulsesink->properties)
2130 gst_structure_free (pulsesink->properties);
2131 if (pulsesink->proplist)
2132 pa_proplist_free (pulsesink->proplist);
2134 if (pulsesink->probe) {
2135 gst_pulseprobe_free (pulsesink->probe);
2136 pulsesink->probe = NULL;
2139 G_OBJECT_CLASS (parent_class)->finalize (object);
2143 gst_pulsesink_set_volume (GstPulseSink * psink, gdouble volume)
2146 pa_operation *o = NULL;
2147 GstPulseRingBuffer *pbuf;
2153 pa_threaded_mainloop_lock (mainloop);
2155 GST_DEBUG_OBJECT (psink, "setting volume to %f", volume);
2157 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2158 if (pbuf == NULL || pbuf->stream == NULL)
2161 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2165 gst_pulse_cvolume_from_linear (&v, pbuf->channels, volume);
2167 /* FIXME: this will eventually be superceded by checks to see if the volume
2168 * is readable/writable */
2171 if (!(o = pa_context_set_sink_input_volume (pbuf->context, idx,
2175 /* We don't really care about the result of this call */
2179 pa_operation_unref (o);
2181 pa_threaded_mainloop_unlock (mainloop);
2188 psink->volume = volume;
2189 psink->volume_set = TRUE;
2191 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2196 psink->volume = volume;
2197 psink->volume_set = TRUE;
2199 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2204 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2209 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2210 ("pa_stream_set_sink_input_volume() failed: %s",
2211 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2217 gst_pulsesink_set_mute (GstPulseSink * psink, gboolean mute)
2219 pa_operation *o = NULL;
2220 GstPulseRingBuffer *pbuf;
2226 pa_threaded_mainloop_lock (mainloop);
2228 GST_DEBUG_OBJECT (psink, "setting mute state to %d", mute);
2230 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2231 if (pbuf == NULL || pbuf->stream == NULL)
2234 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2237 if (!(o = pa_context_set_sink_input_mute (pbuf->context, idx,
2241 /* We don't really care about the result of this call */
2245 pa_operation_unref (o);
2247 pa_threaded_mainloop_unlock (mainloop);
2255 psink->mute_set = TRUE;
2257 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2263 psink->mute_set = TRUE;
2265 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2270 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2275 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2276 ("pa_stream_set_sink_input_mute() failed: %s",
2277 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2283 gst_pulsesink_sink_input_info_cb (pa_context * c, const pa_sink_input_info * i,
2284 int eol, void *userdata)
2286 GstPulseRingBuffer *pbuf;
2287 GstPulseSink *psink;
2289 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
2290 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
2298 /* If the index doesn't match our current stream,
2299 * it implies we just recreated the stream (caps change)
2301 if (i->index == pa_stream_get_index (pbuf->stream)) {
2302 psink->volume = pa_sw_volume_to_linear (pa_cvolume_max (&i->volume));
2303 psink->mute = i->mute;
2307 pa_threaded_mainloop_signal (mainloop, 0);
2311 gst_pulsesink_get_volume (GstPulseSink * psink)
2313 GstPulseRingBuffer *pbuf;
2314 pa_operation *o = NULL;
2315 gdouble v = DEFAULT_VOLUME;
2321 pa_threaded_mainloop_lock (mainloop);
2323 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2324 if (pbuf == NULL || pbuf->stream == NULL)
2327 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2330 if (!(o = pa_context_get_sink_input_info (pbuf->context, idx,
2331 gst_pulsesink_sink_input_info_cb, pbuf)))
2334 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2335 pa_threaded_mainloop_wait (mainloop);
2336 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
2344 pa_operation_unref (o);
2346 pa_threaded_mainloop_unlock (mainloop);
2348 if (v > MAX_VOLUME) {
2349 GST_WARNING_OBJECT (psink, "Clipped volume from %f to %f", v, MAX_VOLUME);
2359 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2364 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2369 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2374 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2375 ("pa_context_get_sink_input_info() failed: %s",
2376 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2382 gst_pulsesink_get_mute (GstPulseSink * psink)
2384 GstPulseRingBuffer *pbuf;
2385 pa_operation *o = NULL;
2387 gboolean mute = FALSE;
2392 pa_threaded_mainloop_lock (mainloop);
2395 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2396 if (pbuf == NULL || pbuf->stream == NULL)
2399 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2402 if (!(o = pa_context_get_sink_input_info (pbuf->context, idx,
2403 gst_pulsesink_sink_input_info_cb, pbuf)))
2406 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2407 pa_threaded_mainloop_wait (mainloop);
2408 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
2414 pa_operation_unref (o);
2416 pa_threaded_mainloop_unlock (mainloop);
2424 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2429 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2434 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2439 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2440 ("pa_context_get_sink_input_info() failed: %s",
2441 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2447 gst_pulsesink_device_description (GstPulseSink * psink)
2449 GstPulseRingBuffer *pbuf;
2450 pa_operation *o = NULL;
2456 pa_threaded_mainloop_lock (mainloop);
2457 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2461 if (!(o = pa_context_get_sink_info_by_name (pbuf->context,
2462 psink->device, gst_pulsesink_sink_info_cb, pbuf)))
2465 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2466 pa_threaded_mainloop_wait (mainloop);
2467 if (gst_pulsering_is_dead (psink, pbuf, FALSE))
2473 pa_operation_unref (o);
2475 t = g_strdup (psink->device_description);
2476 pa_threaded_mainloop_unlock (mainloop);
2483 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2488 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2493 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2494 ("pa_context_get_sink_info_by_index() failed: %s",
2495 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2501 gst_pulsesink_set_property (GObject * object,
2502 guint prop_id, const GValue * value, GParamSpec * pspec)
2504 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2508 g_free (pulsesink->server);
2509 pulsesink->server = g_value_dup_string (value);
2510 if (pulsesink->probe)
2511 gst_pulseprobe_set_server (pulsesink->probe, pulsesink->server);
2514 g_free (pulsesink->device);
2515 pulsesink->device = g_value_dup_string (value);
2518 gst_pulsesink_set_volume (pulsesink, g_value_get_double (value));
2521 gst_pulsesink_set_mute (pulsesink, g_value_get_boolean (value));
2523 case PROP_CLIENT_NAME:
2524 g_free (pulsesink->client_name);
2525 if (!g_value_get_string (value)) {
2526 GST_WARNING_OBJECT (pulsesink,
2527 "Empty PulseAudio client name not allowed. Resetting to default value");
2528 pulsesink->client_name = gst_pulse_client_name ();
2530 pulsesink->client_name = g_value_dup_string (value);
2532 case PROP_STREAM_PROPERTIES:
2533 if (pulsesink->properties)
2534 gst_structure_free (pulsesink->properties);
2535 pulsesink->properties =
2536 gst_structure_copy (gst_value_get_structure (value));
2537 if (pulsesink->proplist)
2538 pa_proplist_free (pulsesink->proplist);
2539 pulsesink->proplist = gst_pulse_make_proplist (pulsesink->properties);
2542 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2548 gst_pulsesink_get_property (GObject * object,
2549 guint prop_id, GValue * value, GParamSpec * pspec)
2552 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2556 g_value_set_string (value, pulsesink->server);
2559 g_value_set_string (value, pulsesink->device);
2561 case PROP_DEVICE_NAME:
2562 g_value_take_string (value, gst_pulsesink_device_description (pulsesink));
2565 g_value_set_double (value, gst_pulsesink_get_volume (pulsesink));
2568 g_value_set_boolean (value, gst_pulsesink_get_mute (pulsesink));
2570 case PROP_CLIENT_NAME:
2571 g_value_set_string (value, pulsesink->client_name);
2573 case PROP_STREAM_PROPERTIES:
2574 gst_value_set_structure (value, pulsesink->properties);
2577 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2583 gst_pulsesink_change_title (GstPulseSink * psink, const gchar * t)
2585 pa_operation *o = NULL;
2586 GstPulseRingBuffer *pbuf;
2588 pa_threaded_mainloop_lock (mainloop);
2590 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2592 if (pbuf == NULL || pbuf->stream == NULL)
2595 g_free (pbuf->stream_name);
2596 pbuf->stream_name = g_strdup (t);
2598 if (!(o = pa_stream_set_name (pbuf->stream, pbuf->stream_name, NULL, NULL)))
2601 /* We're not interested if this operation failed or not */
2605 pa_operation_unref (o);
2606 pa_threaded_mainloop_unlock (mainloop);
2613 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2618 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2619 ("pa_stream_set_name() failed: %s",
2620 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2626 gst_pulsesink_change_props (GstPulseSink * psink, GstTagList * l)
2628 static const gchar *const map[] = {
2629 GST_TAG_TITLE, PA_PROP_MEDIA_TITLE,
2631 /* might get overriden in the next iteration by GST_TAG_ARTIST */
2632 GST_TAG_PERFORMER, PA_PROP_MEDIA_ARTIST,
2634 GST_TAG_ARTIST, PA_PROP_MEDIA_ARTIST,
2635 GST_TAG_LANGUAGE_CODE, PA_PROP_MEDIA_LANGUAGE,
2636 GST_TAG_LOCATION, PA_PROP_MEDIA_FILENAME,
2637 /* We might add more here later on ... */
2640 pa_proplist *pl = NULL;
2641 const gchar *const *t;
2642 gboolean empty = TRUE;
2643 pa_operation *o = NULL;
2644 GstPulseRingBuffer *pbuf;
2646 pl = pa_proplist_new ();
2648 for (t = map; *t; t += 2) {
2651 if (gst_tag_list_get_string (l, *t, &n)) {
2654 pa_proplist_sets (pl, *(t + 1), n);
2664 pa_threaded_mainloop_lock (mainloop);
2665 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2666 if (pbuf == NULL || pbuf->stream == NULL)
2669 if (!(o = pa_stream_proplist_update (pbuf->stream, PA_UPDATE_REPLACE,
2673 /* We're not interested if this operation failed or not */
2677 pa_operation_unref (o);
2679 pa_threaded_mainloop_unlock (mainloop);
2684 pa_proplist_free (pl);
2691 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2696 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2697 ("pa_stream_proplist_update() failed: %s",
2698 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2704 gst_pulsesink_flush_ringbuffer (GstPulseSink * psink)
2706 GstPulseRingBuffer *pbuf;
2708 pa_threaded_mainloop_lock (mainloop);
2710 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2712 if (pbuf == NULL || pbuf->stream == NULL)
2715 gst_pulsering_flush (pbuf);
2717 /* Uncork if we haven't already (happens when waiting to get enough data
2718 * to send out the first time) */
2720 gst_pulsering_set_corked (pbuf, FALSE, FALSE);
2722 /* We're not interested if this operation failed or not */
2724 pa_threaded_mainloop_unlock (mainloop);
2731 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2737 gst_pulsesink_event (GstBaseSink * sink, GstEvent * event)
2739 GstPulseSink *pulsesink = GST_PULSESINK_CAST (sink);
2741 switch (GST_EVENT_TYPE (event)) {
2742 case GST_EVENT_TAG:{
2743 gchar *title = NULL, *artist = NULL, *location = NULL, *description =
2744 NULL, *t = NULL, *buf = NULL;
2747 gst_event_parse_tag (event, &l);
2749 gst_tag_list_get_string (l, GST_TAG_TITLE, &title);
2750 gst_tag_list_get_string (l, GST_TAG_ARTIST, &artist);
2751 gst_tag_list_get_string (l, GST_TAG_LOCATION, &location);
2752 gst_tag_list_get_string (l, GST_TAG_DESCRIPTION, &description);
2755 gst_tag_list_get_string (l, GST_TAG_PERFORMER, &artist);
2757 if (title && artist)
2758 /* TRANSLATORS: 'song title' by 'artist name' */
2759 t = buf = g_strdup_printf (_("'%s' by '%s'"), g_strstrip (title),
2760 g_strstrip (artist));
2762 t = g_strstrip (title);
2763 else if (description)
2764 t = g_strstrip (description);
2766 t = g_strstrip (location);
2769 gst_pulsesink_change_title (pulsesink, t);
2774 g_free (description);
2777 gst_pulsesink_change_props (pulsesink, l);
2782 gst_pulsesink_flush_ringbuffer (pulsesink);
2788 return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
2792 gst_pulsesink_query (GstBaseSink * sink, GstQuery * query)
2794 GstPulseSink *pulsesink = GST_PULSESINK_CAST (sink);
2797 switch (GST_QUERY_TYPE (query)) {
2798 case GST_QUERY_ACCEPT_CAPS:
2802 gst_query_parse_accept_caps (query, &caps);
2803 ret = gst_pulsesink_query_acceptcaps (pulsesink, caps);
2804 gst_query_set_accept_caps_result (query, ret);
2809 ret = GST_BASE_SINK_CLASS (parent_class)->query (sink, query);
2816 gst_pulsesink_release_mainloop (GstPulseSink * psink)
2821 pa_threaded_mainloop_lock (mainloop);
2822 while (psink->defer_pending) {
2823 GST_DEBUG_OBJECT (psink, "waiting for stream status message emission");
2824 pa_threaded_mainloop_wait (mainloop);
2826 pa_threaded_mainloop_unlock (mainloop);
2828 g_mutex_lock (pa_shared_resource_mutex);
2830 if (!mainloop_ref_ct) {
2831 GST_INFO_OBJECT (psink, "terminating pa main loop thread");
2832 pa_threaded_mainloop_stop (mainloop);
2833 pa_threaded_mainloop_free (mainloop);
2836 g_mutex_unlock (pa_shared_resource_mutex);
2839 static GstStateChangeReturn
2840 gst_pulsesink_change_state (GstElement * element, GstStateChange transition)
2842 GstPulseSink *pulsesink = GST_PULSESINK (element);
2843 GstStateChangeReturn ret;
2845 switch (transition) {
2846 case GST_STATE_CHANGE_NULL_TO_READY:
2847 g_mutex_lock (pa_shared_resource_mutex);
2848 if (!mainloop_ref_ct) {
2849 GST_INFO_OBJECT (element, "new pa main loop thread");
2850 if (!(mainloop = pa_threaded_mainloop_new ()))
2851 goto mainloop_failed;
2852 mainloop_ref_ct = 1;
2853 pa_threaded_mainloop_start (mainloop);
2854 g_mutex_unlock (pa_shared_resource_mutex);
2856 GST_INFO_OBJECT (element, "reusing pa main loop thread");
2858 g_mutex_unlock (pa_shared_resource_mutex);
2861 case GST_STATE_CHANGE_READY_TO_PAUSED:
2862 gst_element_post_message (element,
2863 gst_message_new_clock_provide (GST_OBJECT_CAST (element),
2864 GST_AUDIO_BASE_SINK (pulsesink)->provided_clock, TRUE));
2871 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2872 if (ret == GST_STATE_CHANGE_FAILURE)
2875 switch (transition) {
2876 case GST_STATE_CHANGE_PAUSED_TO_READY:
2877 /* format_lost is reset in release() in audiobasesink */
2878 gst_element_post_message (element,
2879 gst_message_new_clock_lost (GST_OBJECT_CAST (element),
2880 GST_AUDIO_BASE_SINK (pulsesink)->provided_clock));
2882 case GST_STATE_CHANGE_READY_TO_NULL:
2883 gst_pulsesink_release_mainloop (pulsesink);
2894 g_mutex_unlock (pa_shared_resource_mutex);
2895 GST_ELEMENT_ERROR (pulsesink, RESOURCE, FAILED,
2896 ("pa_threaded_mainloop_new() failed"), (NULL));
2897 return GST_STATE_CHANGE_FAILURE;
2901 if (transition == GST_STATE_CHANGE_NULL_TO_READY) {
2902 /* Clear the PA mainloop if audiobasesink failed to open the ring_buffer */
2903 g_assert (mainloop);
2904 gst_pulsesink_release_mainloop (pulsesink);