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/interfaces/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 GstRingBuffer object;
142 #ifdef HAVE_PULSE_1_0
143 pa_format_info *format;
146 pa_sample_spec sample_spec;
156 gboolean in_commit:1;
159 struct _GstPulseRingBufferClass
161 GstRingBufferClass parent_class;
164 static GType gst_pulseringbuffer_get_type (void);
165 static void gst_pulseringbuffer_finalize (GObject * object);
167 static GstRingBufferClass *ring_parent_class = NULL;
169 static gboolean gst_pulseringbuffer_open_device (GstRingBuffer * buf);
170 static gboolean gst_pulseringbuffer_close_device (GstRingBuffer * buf);
171 static gboolean gst_pulseringbuffer_acquire (GstRingBuffer * buf,
172 GstRingBufferSpec * spec);
173 static gboolean gst_pulseringbuffer_release (GstRingBuffer * buf);
174 static gboolean gst_pulseringbuffer_start (GstRingBuffer * buf);
175 static gboolean gst_pulseringbuffer_pause (GstRingBuffer * buf);
176 static gboolean gst_pulseringbuffer_stop (GstRingBuffer * buf);
177 static void gst_pulseringbuffer_clear (GstRingBuffer * buf);
178 static guint gst_pulseringbuffer_commit (GstRingBuffer * buf,
179 guint64 * sample, guchar * data, gint in_samples, gint out_samples,
182 G_DEFINE_TYPE (GstPulseRingBuffer, gst_pulseringbuffer, GST_TYPE_RING_BUFFER);
185 gst_pulsesink_init_contexts (void)
187 g_assert (pa_shared_resource_mutex == NULL);
188 pa_shared_resource_mutex = g_mutex_new ();
189 gst_pulse_shared_contexts = g_hash_table_new_full (g_str_hash, g_str_equal,
194 gst_pulseringbuffer_class_init (GstPulseRingBufferClass * klass)
196 GObjectClass *gobject_class;
197 GstRingBufferClass *gstringbuffer_class;
199 gobject_class = (GObjectClass *) klass;
200 gstringbuffer_class = (GstRingBufferClass *) klass;
202 ring_parent_class = g_type_class_peek_parent (klass);
204 gobject_class->finalize = gst_pulseringbuffer_finalize;
206 gstringbuffer_class->open_device =
207 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_open_device);
208 gstringbuffer_class->close_device =
209 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_close_device);
210 gstringbuffer_class->acquire =
211 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_acquire);
212 gstringbuffer_class->release =
213 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_release);
214 gstringbuffer_class->start = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_start);
215 gstringbuffer_class->pause = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_pause);
216 gstringbuffer_class->resume = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_start);
217 gstringbuffer_class->stop = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_stop);
218 gstringbuffer_class->clear_all =
219 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_clear);
221 gstringbuffer_class->commit = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_commit);
225 gst_pulseringbuffer_init (GstPulseRingBuffer * pbuf)
227 pbuf->stream_name = NULL;
228 pbuf->context = NULL;
231 #ifdef HAVE_PULSE_1_0
235 pa_sample_spec_init (&pbuf->sample_spec);
240 pbuf->m_writable = 0;
242 pbuf->m_lastoffset = 0;
245 pbuf->in_commit = FALSE;
246 pbuf->paused = FALSE;
250 gst_pulsering_destroy_stream (GstPulseRingBuffer * pbuf)
255 /* drop shm memory buffer */
256 pa_stream_cancel_write (pbuf->stream);
258 /* reset internal variables */
261 pbuf->m_writable = 0;
263 pbuf->m_lastoffset = 0;
265 #ifdef HAVE_PULSE_1_0
267 pa_format_info_free (pbuf->format);
273 pa_stream_disconnect (pbuf->stream);
275 /* Make sure we don't get any further callbacks */
276 pa_stream_set_state_callback (pbuf->stream, NULL, NULL);
277 pa_stream_set_write_callback (pbuf->stream, NULL, NULL);
278 pa_stream_set_underflow_callback (pbuf->stream, NULL, NULL);
279 pa_stream_set_overflow_callback (pbuf->stream, NULL, NULL);
281 pa_stream_unref (pbuf->stream);
285 g_free (pbuf->stream_name);
286 pbuf->stream_name = NULL;
290 gst_pulsering_destroy_context (GstPulseRingBuffer * pbuf)
292 g_mutex_lock (pa_shared_resource_mutex);
294 GST_DEBUG_OBJECT (pbuf, "destroying ringbuffer %p", pbuf);
296 gst_pulsering_destroy_stream (pbuf);
299 pa_context_unref (pbuf->context);
300 pbuf->context = NULL;
303 if (pbuf->context_name) {
304 GstPulseContext *pctx;
306 pctx = g_hash_table_lookup (gst_pulse_shared_contexts, pbuf->context_name);
308 GST_DEBUG_OBJECT (pbuf, "releasing context with name %s, pbuf=%p, pctx=%p",
309 pbuf->context_name, pbuf, pctx);
312 pctx->ring_buffers = g_slist_remove (pctx->ring_buffers, pbuf);
313 if (pctx->ring_buffers == NULL) {
314 GST_DEBUG_OBJECT (pbuf,
315 "destroying final context with name %s, pbuf=%p, pctx=%p",
316 pbuf->context_name, pbuf, pctx);
318 pa_context_disconnect (pctx->context);
320 /* Make sure we don't get any further callbacks */
321 pa_context_set_state_callback (pctx->context, NULL, NULL);
322 pa_context_set_subscribe_callback (pctx->context, NULL, NULL);
324 g_hash_table_remove (gst_pulse_shared_contexts, pbuf->context_name);
326 pa_context_unref (pctx->context);
327 g_slice_free (GstPulseContext, pctx);
330 g_free (pbuf->context_name);
331 pbuf->context_name = NULL;
333 g_mutex_unlock (pa_shared_resource_mutex);
337 gst_pulseringbuffer_finalize (GObject * object)
339 GstPulseRingBuffer *ringbuffer;
341 ringbuffer = GST_PULSERING_BUFFER_CAST (object);
343 gst_pulsering_destroy_context (ringbuffer);
344 G_OBJECT_CLASS (ring_parent_class)->finalize (object);
348 #define CONTEXT_OK(c) ((c) && PA_CONTEXT_IS_GOOD (pa_context_get_state ((c))))
349 #define STREAM_OK(s) ((s) && PA_STREAM_IS_GOOD (pa_stream_get_state ((s))))
352 gst_pulsering_is_dead (GstPulseSink * psink, GstPulseRingBuffer * pbuf,
353 gboolean check_stream)
355 if (!CONTEXT_OK (pbuf->context))
358 if (check_stream && !STREAM_OK (pbuf->stream))
365 const gchar *err_str =
366 pbuf->context ? pa_strerror (pa_context_errno (pbuf->context)) : NULL;
367 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED, ("Disconnected: %s",
374 gst_pulsering_context_state_cb (pa_context * c, void *userdata)
376 pa_context_state_t state;
377 pa_threaded_mainloop *mainloop = (pa_threaded_mainloop *) userdata;
379 state = pa_context_get_state (c);
381 GST_LOG ("got new context state %d", state);
384 case PA_CONTEXT_READY:
385 case PA_CONTEXT_TERMINATED:
386 case PA_CONTEXT_FAILED:
387 GST_LOG ("signaling");
388 pa_threaded_mainloop_signal (mainloop, 0);
391 case PA_CONTEXT_UNCONNECTED:
392 case PA_CONTEXT_CONNECTING:
393 case PA_CONTEXT_AUTHORIZING:
394 case PA_CONTEXT_SETTING_NAME:
400 gst_pulsering_context_subscribe_cb (pa_context * c,
401 pa_subscription_event_type_t t, uint32_t idx, void *userdata)
404 GstPulseContext *pctx = (GstPulseContext *) userdata;
407 if (t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT | PA_SUBSCRIPTION_EVENT_CHANGE) &&
408 t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT | PA_SUBSCRIPTION_EVENT_NEW))
411 for (walk = pctx->ring_buffers; walk; walk = g_slist_next (walk)) {
412 GstPulseRingBuffer *pbuf = (GstPulseRingBuffer *) walk->data;
413 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
415 GST_LOG_OBJECT (psink, "type %d, idx %u", t, idx);
420 if (idx != pa_stream_get_index (pbuf->stream))
423 #ifdef HAVE_PULSE_1_0
424 if (psink->device && pa_format_info_is_pcm (pbuf->format) &&
425 !g_str_equal (psink->device,
426 pa_stream_get_device_name (pbuf->stream))) {
427 /* Underlying sink changed. And this is not a passthrough stream. Let's
428 * see if someone upstream wants to try to renegotiate. */
431 g_free (psink->device);
432 psink->device = g_strdup (pa_stream_get_device_name (pbuf->stream));
434 GST_INFO_OBJECT (psink, "emitting sink-changed");
436 renego = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
437 gst_structure_new ("pulse-sink-changed", NULL));
439 if (!gst_pad_push_event (GST_BASE_SINK (psink)->sinkpad, renego))
440 GST_DEBUG_OBJECT (psink, "Emitted sink-changed - nobody was listening");
444 /* Actually this event is also triggered when other properties of
445 * the stream change that are unrelated to the volume. However it is
446 * probably cheaper to signal the change here and check for the
447 * volume when the GObject property is read instead of querying it always. */
449 /* inform streaming thread to notify */
450 g_atomic_int_compare_and_exchange (&psink->notify, 0, 1);
454 /* will be called when the device should be opened. In this case we will connect
455 * to the server. We should not try to open any streams in this state. */
457 gst_pulseringbuffer_open_device (GstRingBuffer * buf)
460 GstPulseRingBuffer *pbuf;
461 GstPulseContext *pctx;
462 pa_mainloop_api *api;
463 gboolean need_unlock_shared;
465 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
466 pbuf = GST_PULSERING_BUFFER_CAST (buf);
468 g_assert (!pbuf->stream);
469 g_assert (psink->client_name);
472 pbuf->context_name = g_strdup_printf ("%s@%s", psink->client_name,
475 pbuf->context_name = g_strdup (psink->client_name);
477 pa_threaded_mainloop_lock (mainloop);
479 g_mutex_lock (pa_shared_resource_mutex);
480 need_unlock_shared = TRUE;
482 pctx = g_hash_table_lookup (gst_pulse_shared_contexts, pbuf->context_name);
484 pctx = g_slice_new0 (GstPulseContext);
486 /* get the mainloop api and create a context */
487 GST_INFO_OBJECT (psink, "new context with name %s, pbuf=%p, pctx=%p",
488 pbuf->context_name, pbuf, pctx);
489 api = pa_threaded_mainloop_get_api (mainloop);
490 if (!(pctx->context = pa_context_new (api, pbuf->context_name)))
493 pctx->ring_buffers = g_slist_prepend (pctx->ring_buffers, pbuf);
494 g_hash_table_insert (gst_pulse_shared_contexts,
495 g_strdup (pbuf->context_name), (gpointer) pctx);
496 /* register some essential callbacks */
497 pa_context_set_state_callback (pctx->context,
498 gst_pulsering_context_state_cb, mainloop);
499 pa_context_set_subscribe_callback (pctx->context,
500 gst_pulsering_context_subscribe_cb, pctx);
502 /* try to connect to the server and wait for completion, we don't want to
503 * autospawn a deamon */
504 GST_LOG_OBJECT (psink, "connect to server %s",
505 GST_STR_NULL (psink->server));
506 if (pa_context_connect (pctx->context, psink->server,
507 PA_CONTEXT_NOAUTOSPAWN, NULL) < 0)
510 GST_INFO_OBJECT (psink,
511 "reusing shared context with name %s, pbuf=%p, pctx=%p",
512 pbuf->context_name, pbuf, pctx);
513 pctx->ring_buffers = g_slist_prepend (pctx->ring_buffers, pbuf);
516 g_mutex_unlock (pa_shared_resource_mutex);
517 need_unlock_shared = FALSE;
519 /* context created or shared okay */
520 pbuf->context = pa_context_ref (pctx->context);
523 pa_context_state_t state;
525 state = pa_context_get_state (pbuf->context);
527 GST_LOG_OBJECT (psink, "context state is now %d", state);
529 if (!PA_CONTEXT_IS_GOOD (state))
532 if (state == PA_CONTEXT_READY)
535 /* Wait until the context is ready */
536 GST_LOG_OBJECT (psink, "waiting..");
537 pa_threaded_mainloop_wait (mainloop);
540 GST_LOG_OBJECT (psink, "opened the device");
542 pa_threaded_mainloop_unlock (mainloop);
549 if (need_unlock_shared)
550 g_mutex_unlock (pa_shared_resource_mutex);
551 gst_pulsering_destroy_context (pbuf);
552 pa_threaded_mainloop_unlock (mainloop);
557 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
558 ("Failed to create context"), (NULL));
559 g_slice_free (GstPulseContext, pctx);
560 goto unlock_and_fail;
564 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED, ("Failed to connect: %s",
565 pa_strerror (pa_context_errno (pctx->context))), (NULL));
566 goto unlock_and_fail;
570 /* close the device */
572 gst_pulseringbuffer_close_device (GstRingBuffer * buf)
575 GstPulseRingBuffer *pbuf;
577 pbuf = GST_PULSERING_BUFFER_CAST (buf);
578 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
580 GST_LOG_OBJECT (psink, "closing device");
582 pa_threaded_mainloop_lock (mainloop);
583 gst_pulsering_destroy_context (pbuf);
584 pa_threaded_mainloop_unlock (mainloop);
586 GST_LOG_OBJECT (psink, "closed device");
592 gst_pulsering_stream_state_cb (pa_stream * s, void *userdata)
595 GstPulseRingBuffer *pbuf;
596 pa_stream_state_t state;
598 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
599 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
601 state = pa_stream_get_state (s);
602 GST_LOG_OBJECT (psink, "got new stream state %d", state);
605 case PA_STREAM_READY:
606 case PA_STREAM_FAILED:
607 case PA_STREAM_TERMINATED:
608 GST_LOG_OBJECT (psink, "signaling");
609 pa_threaded_mainloop_signal (mainloop, 0);
611 case PA_STREAM_UNCONNECTED:
612 case PA_STREAM_CREATING:
618 gst_pulsering_stream_request_cb (pa_stream * s, size_t length, void *userdata)
622 GstPulseRingBuffer *pbuf;
624 rbuf = GST_RING_BUFFER_CAST (userdata);
625 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
626 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
628 GST_LOG_OBJECT (psink, "got request for length %" G_GSIZE_FORMAT, length);
630 if (pbuf->in_commit && (length >= rbuf->spec.segsize)) {
631 /* only signal when we are waiting in the commit thread
632 * and got request for atleast a segment */
633 pa_threaded_mainloop_signal (mainloop, 0);
638 gst_pulsering_stream_underflow_cb (pa_stream * s, void *userdata)
641 GstPulseRingBuffer *pbuf;
643 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
644 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
646 GST_WARNING_OBJECT (psink, "Got underflow");
650 gst_pulsering_stream_overflow_cb (pa_stream * s, void *userdata)
653 GstPulseRingBuffer *pbuf;
655 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
656 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
658 GST_WARNING_OBJECT (psink, "Got overflow");
662 gst_pulsering_stream_latency_cb (pa_stream * s, void *userdata)
665 GstPulseRingBuffer *pbuf;
666 const pa_timing_info *info;
669 info = pa_stream_get_timing_info (s);
671 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
672 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
675 GST_LOG_OBJECT (psink, "latency update (information unknown)");
678 sink_usec = info->configured_sink_usec;
680 GST_LOG_OBJECT (psink,
681 "latency_update, %" G_GUINT64_FORMAT ", %d:%" G_GINT64_FORMAT ", %d:%"
682 G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT,
683 GST_TIMEVAL_TO_TIME (info->timestamp), info->write_index_corrupt,
684 info->write_index, info->read_index_corrupt, info->read_index,
685 info->sink_usec, sink_usec);
689 gst_pulsering_stream_suspended_cb (pa_stream * p, void *userdata)
692 GstPulseRingBuffer *pbuf;
694 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
695 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
697 if (pa_stream_is_suspended (p))
698 GST_DEBUG_OBJECT (psink, "stream suspended");
700 GST_DEBUG_OBJECT (psink, "stream resumed");
704 gst_pulsering_stream_started_cb (pa_stream * p, void *userdata)
707 GstPulseRingBuffer *pbuf;
709 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
710 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
712 GST_DEBUG_OBJECT (psink, "stream started");
716 gst_pulsering_stream_event_cb (pa_stream * p, const char *name,
717 pa_proplist * pl, void *userdata)
720 GstPulseRingBuffer *pbuf;
722 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
723 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
725 if (!strcmp (name, PA_STREAM_EVENT_REQUEST_CORK)) {
726 /* the stream wants to PAUSE, post a message for the application. */
727 GST_DEBUG_OBJECT (psink, "got request for CORK");
728 gst_element_post_message (GST_ELEMENT_CAST (psink),
729 gst_message_new_request_state (GST_OBJECT_CAST (psink),
732 } else if (!strcmp (name, PA_STREAM_EVENT_REQUEST_UNCORK)) {
733 GST_DEBUG_OBJECT (psink, "got request for UNCORK");
734 gst_element_post_message (GST_ELEMENT_CAST (psink),
735 gst_message_new_request_state (GST_OBJECT_CAST (psink),
737 #ifdef HAVE_PULSE_1_0
738 } else if (!strcmp (name, PA_STREAM_EVENT_FORMAT_LOST)) {
741 if (g_atomic_int_get (&psink->format_lost)) {
742 /* Duplicate event before we're done reconfiguring, discard */
746 GST_DEBUG_OBJECT (psink, "got FORMAT LOST");
747 g_atomic_int_set (&psink->format_lost, 1);
748 psink->format_lost_time = g_ascii_strtoull (pa_proplist_gets (pl,
749 "stream-time"), NULL, 0) * 1000;
751 g_free (psink->device);
752 psink->device = g_strdup (pa_proplist_gets (pl, "device"));
754 renego = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
755 gst_structure_new ("pulse-format-lost", NULL));
757 if (!gst_pad_push_event (GST_BASE_SINK (psink)->sinkpad, renego)) {
758 /* Nobody handled the format change - emit an error */
759 GST_ELEMENT_ERROR (psink, STREAM, FORMAT, ("Sink format changed"),
760 ("Sink format changed"));
764 GST_DEBUG_OBJECT (psink, "got unknown event %s", name);
768 /* Called with the mainloop locked */
770 gst_pulsering_wait_for_stream_ready (GstPulseSink * psink, pa_stream * stream)
772 pa_stream_state_t state;
775 state = pa_stream_get_state (stream);
777 GST_LOG_OBJECT (psink, "stream state is now %d", state);
779 if (!PA_STREAM_IS_GOOD (state))
782 if (state == PA_STREAM_READY)
785 /* Wait until the stream is ready */
786 pa_threaded_mainloop_wait (mainloop);
791 /* This method should create a new stream of the given @spec. No playback should
792 * start yet so we start in the corked state. */
794 gst_pulseringbuffer_acquire (GstRingBuffer * buf, GstRingBufferSpec * spec)
797 GstPulseRingBuffer *pbuf;
798 pa_buffer_attr wanted;
799 const pa_buffer_attr *actual;
800 pa_channel_map channel_map;
801 pa_operation *o = NULL;
802 #ifdef HAVE_PULSE_0_9_20
805 pa_cvolume *pv = NULL;
806 pa_stream_flags_t flags;
808 GstAudioClock *clock;
809 #ifdef HAVE_PULSE_1_0
810 pa_format_info *formats[1];
811 #ifndef GST_DISABLE_GST_DEBUG
812 gchar print_buf[PA_FORMAT_INFO_SNPRINT_MAX];
816 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
817 pbuf = GST_PULSERING_BUFFER_CAST (buf);
819 GST_LOG_OBJECT (psink, "creating sample spec");
820 /* convert the gstreamer sample spec to the pulseaudio format */
821 #ifdef HAVE_PULSE_1_0
822 if (!gst_pulse_fill_format_info (spec, &pbuf->format, &pbuf->channels))
825 if (!gst_pulse_fill_sample_spec (spec, &pbuf->sample_spec))
829 pa_threaded_mainloop_lock (mainloop);
831 /* we need a context and a no stream */
832 g_assert (pbuf->context);
833 g_assert (!pbuf->stream);
835 /* enable event notifications */
836 GST_LOG_OBJECT (psink, "subscribing to context events");
837 if (!(o = pa_context_subscribe (pbuf->context,
838 PA_SUBSCRIPTION_MASK_SINK_INPUT, NULL, NULL)))
839 goto subscribe_failed;
841 pa_operation_unref (o);
843 /* initialize the channel map */
844 #ifdef HAVE_PULSE_1_0
845 if (pa_format_info_is_pcm (pbuf->format) &&
846 gst_pulse_gst_to_channel_map (&channel_map, spec))
847 pa_format_info_set_channel_map (pbuf->format, &channel_map);
849 gst_pulse_gst_to_channel_map (&channel_map, spec);
852 /* find a good name for the stream */
853 if (psink->stream_name)
854 name = psink->stream_name;
856 name = "Playback Stream";
858 /* create a stream */
859 #ifdef HAVE_PULSE_1_0
860 formats[0] = pbuf->format;
861 if (!(pbuf->stream = pa_stream_new_extended (pbuf->context, name, formats, 1,
865 GST_LOG_OBJECT (psink, "creating stream with name %s", name);
866 if (!(pbuf->stream = pa_stream_new_with_proplist (pbuf->context, name,
867 &pbuf->sample_spec, &channel_map, psink->proplist)))
871 /* install essential callbacks */
872 pa_stream_set_state_callback (pbuf->stream,
873 gst_pulsering_stream_state_cb, pbuf);
874 pa_stream_set_write_callback (pbuf->stream,
875 gst_pulsering_stream_request_cb, pbuf);
876 pa_stream_set_underflow_callback (pbuf->stream,
877 gst_pulsering_stream_underflow_cb, pbuf);
878 pa_stream_set_overflow_callback (pbuf->stream,
879 gst_pulsering_stream_overflow_cb, pbuf);
880 pa_stream_set_latency_update_callback (pbuf->stream,
881 gst_pulsering_stream_latency_cb, pbuf);
882 pa_stream_set_suspended_callback (pbuf->stream,
883 gst_pulsering_stream_suspended_cb, pbuf);
884 pa_stream_set_started_callback (pbuf->stream,
885 gst_pulsering_stream_started_cb, pbuf);
886 pa_stream_set_event_callback (pbuf->stream,
887 gst_pulsering_stream_event_cb, pbuf);
889 /* buffering requirements. When setting prebuf to 0, the stream will not pause
890 * when we cause an underrun, which causes time to continue. */
891 memset (&wanted, 0, sizeof (wanted));
892 wanted.tlength = spec->segtotal * spec->segsize;
893 wanted.maxlength = -1;
895 wanted.minreq = spec->segsize;
897 GST_INFO_OBJECT (psink, "tlength: %d", wanted.tlength);
898 GST_INFO_OBJECT (psink, "maxlength: %d", wanted.maxlength);
899 GST_INFO_OBJECT (psink, "prebuf: %d", wanted.prebuf);
900 GST_INFO_OBJECT (psink, "minreq: %d", wanted.minreq);
902 #ifdef HAVE_PULSE_0_9_20
903 /* configure volume when we changed it, else we leave the default */
904 if (psink->volume_set) {
905 GST_LOG_OBJECT (psink, "have volume of %f", psink->volume);
907 #ifdef HAVE_PULSE_1_0
908 if (pa_format_info_is_pcm (pbuf->format))
909 gst_pulse_cvolume_from_linear (pv, pbuf->channels, psink->volume);
911 GST_DEBUG_OBJECT (psink, "passthrough stream, not setting volume");
915 gst_pulse_cvolume_from_linear (pv, pbuf->sample_spec.channels,
923 /* construct the flags */
924 flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE |
925 PA_STREAM_ADJUST_LATENCY | PA_STREAM_START_CORKED;
927 if (psink->mute_set && psink->mute)
928 flags |= PA_STREAM_START_MUTED;
930 /* we always start corked (see flags above) */
933 /* try to connect now */
934 GST_LOG_OBJECT (psink, "connect for playback to device %s",
935 GST_STR_NULL (psink->device));
936 if (pa_stream_connect_playback (pbuf->stream, psink->device,
937 &wanted, flags, pv, NULL) < 0)
940 /* our clock will now start from 0 again */
941 clock = GST_AUDIO_CLOCK (GST_BASE_AUDIO_SINK (psink)->provided_clock);
942 gst_audio_clock_reset (clock, 0);
944 if (!gst_pulsering_wait_for_stream_ready (psink, pbuf->stream))
947 #ifdef HAVE_PULSE_1_0
948 g_free (psink->device);
949 psink->device = g_strdup (pa_stream_get_device_name (pbuf->stream));
951 #ifndef GST_DISABLE_GST_DEBUG
952 pa_format_info_snprint (print_buf, sizeof (print_buf),
953 pa_stream_get_format_info (pbuf->stream));
954 GST_INFO_OBJECT (psink, "negotiated to: %s", print_buf);
958 /* After we passed the volume off of to PA we never want to set it
959 again, since it is PA's job to save/restore volumes. */
960 psink->volume_set = psink->mute_set = FALSE;
962 GST_LOG_OBJECT (psink, "stream is acquired now");
964 /* get the actual buffering properties now */
965 actual = pa_stream_get_buffer_attr (pbuf->stream);
967 GST_INFO_OBJECT (psink, "tlength: %d (wanted: %d)", actual->tlength,
969 GST_INFO_OBJECT (psink, "maxlength: %d", actual->maxlength);
970 GST_INFO_OBJECT (psink, "prebuf: %d", actual->prebuf);
971 GST_INFO_OBJECT (psink, "minreq: %d (wanted %d)", actual->minreq,
974 spec->segsize = actual->minreq;
975 spec->segtotal = actual->tlength / spec->segsize;
977 pa_threaded_mainloop_unlock (mainloop);
984 gst_pulsering_destroy_stream (pbuf);
985 pa_threaded_mainloop_unlock (mainloop);
991 GST_ELEMENT_ERROR (psink, RESOURCE, SETTINGS,
992 ("Invalid sample specification."), (NULL));
997 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
998 ("pa_context_subscribe() failed: %s",
999 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1000 goto unlock_and_fail;
1004 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1005 ("Failed to create stream: %s",
1006 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1007 goto unlock_and_fail;
1011 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1012 ("Failed to connect stream: %s",
1013 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1014 goto unlock_and_fail;
1018 /* free the stream that we acquired before */
1020 gst_pulseringbuffer_release (GstRingBuffer * buf)
1022 GstPulseRingBuffer *pbuf;
1024 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1026 pa_threaded_mainloop_lock (mainloop);
1027 gst_pulsering_destroy_stream (pbuf);
1028 pa_threaded_mainloop_unlock (mainloop);
1030 #ifdef HAVE_PULSE_1_0
1032 GstPulseSink *psink;
1034 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1035 g_atomic_int_set (&psink->format_lost, FALSE);
1036 psink->format_lost_time = GST_CLOCK_TIME_NONE;
1044 gst_pulsering_success_cb (pa_stream * s, int success, void *userdata)
1046 pa_threaded_mainloop_signal (mainloop, 0);
1049 /* update the corked state of a stream, must be called with the mainloop
1052 gst_pulsering_set_corked (GstPulseRingBuffer * pbuf, gboolean corked,
1055 pa_operation *o = NULL;
1056 GstPulseSink *psink;
1057 gboolean res = FALSE;
1059 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1061 #ifdef HAVE_PULSE_1_0
1062 if (g_atomic_int_get (&psink->format_lost)) {
1063 /* Sink format changed, stream's gone so fake being paused */
1068 GST_DEBUG_OBJECT (psink, "setting corked state to %d", corked);
1069 if (pbuf->corked != corked) {
1070 if (!(o = pa_stream_cork (pbuf->stream, corked,
1071 gst_pulsering_success_cb, pbuf)))
1074 while (wait && pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1075 pa_threaded_mainloop_wait (mainloop);
1076 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
1079 pbuf->corked = corked;
1081 GST_DEBUG_OBJECT (psink, "skipping, already in requested state");
1087 pa_operation_unref (o);
1094 GST_DEBUG_OBJECT (psink, "the server is dead");
1099 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1100 ("pa_stream_cork() failed: %s",
1101 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1107 gst_pulseringbuffer_clear (GstRingBuffer * buf)
1109 GstPulseSink *psink;
1110 GstPulseRingBuffer *pbuf;
1111 pa_operation *o = NULL;
1113 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1114 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1116 pa_threaded_mainloop_lock (mainloop);
1117 GST_DEBUG_OBJECT (psink, "clearing");
1119 /* don't wait for the flush to complete */
1120 if ((o = pa_stream_flush (pbuf->stream, NULL, pbuf)))
1121 pa_operation_unref (o);
1123 pa_threaded_mainloop_unlock (mainloop);
1126 /* called from pulse with the mainloop lock */
1128 mainloop_enter_defer_cb (pa_mainloop_api * api, void *userdata)
1130 GstPulseSink *pulsesink = GST_PULSESINK (userdata);
1131 GstMessage *message;
1134 g_value_init (&val, G_TYPE_POINTER);
1135 g_value_set_pointer (&val, g_thread_self ());
1137 GST_DEBUG_OBJECT (pulsesink, "posting ENTER stream status");
1138 message = gst_message_new_stream_status (GST_OBJECT (pulsesink),
1139 GST_STREAM_STATUS_TYPE_ENTER, GST_ELEMENT (pulsesink));
1140 gst_message_set_stream_status_object (message, &val);
1142 gst_element_post_message (GST_ELEMENT (pulsesink), message);
1144 g_return_if_fail (pulsesink->defer_pending);
1145 pulsesink->defer_pending--;
1146 pa_threaded_mainloop_signal (mainloop, 0);
1149 /* start/resume playback ASAP, we don't uncork here but in the commit method */
1151 gst_pulseringbuffer_start (GstRingBuffer * buf)
1153 GstPulseSink *psink;
1154 GstPulseRingBuffer *pbuf;
1156 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1157 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1159 pa_threaded_mainloop_lock (mainloop);
1161 GST_DEBUG_OBJECT (psink, "scheduling stream status");
1162 psink->defer_pending++;
1163 pa_mainloop_api_once (pa_threaded_mainloop_get_api (mainloop),
1164 mainloop_enter_defer_cb, psink);
1166 GST_DEBUG_OBJECT (psink, "starting");
1167 pbuf->paused = FALSE;
1169 /* EOS needs running clock */
1170 if (GST_BASE_SINK_CAST (psink)->eos ||
1171 g_atomic_int_get (&GST_BASE_AUDIO_SINK (psink)->eos_rendering))
1172 gst_pulsering_set_corked (pbuf, FALSE, FALSE);
1174 pa_threaded_mainloop_unlock (mainloop);
1179 /* pause/stop playback ASAP */
1181 gst_pulseringbuffer_pause (GstRingBuffer * buf)
1183 GstPulseSink *psink;
1184 GstPulseRingBuffer *pbuf;
1187 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1188 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1190 pa_threaded_mainloop_lock (mainloop);
1191 GST_DEBUG_OBJECT (psink, "pausing and corking");
1192 /* make sure the commit method stops writing */
1193 pbuf->paused = TRUE;
1194 res = gst_pulsering_set_corked (pbuf, TRUE, TRUE);
1195 if (pbuf->in_commit) {
1196 /* we are waiting in a commit, signal */
1197 GST_DEBUG_OBJECT (psink, "signal commit");
1198 pa_threaded_mainloop_signal (mainloop, 0);
1200 pa_threaded_mainloop_unlock (mainloop);
1205 /* called from pulse with the mainloop lock */
1207 mainloop_leave_defer_cb (pa_mainloop_api * api, void *userdata)
1209 GstPulseSink *pulsesink = GST_PULSESINK (userdata);
1210 GstMessage *message;
1213 g_value_init (&val, G_TYPE_POINTER);
1214 g_value_set_pointer (&val, g_thread_self ());
1216 GST_DEBUG_OBJECT (pulsesink, "posting LEAVE stream status");
1217 message = gst_message_new_stream_status (GST_OBJECT (pulsesink),
1218 GST_STREAM_STATUS_TYPE_LEAVE, GST_ELEMENT (pulsesink));
1219 gst_message_set_stream_status_object (message, &val);
1220 gst_element_post_message (GST_ELEMENT (pulsesink), message);
1222 g_return_if_fail (pulsesink->defer_pending);
1223 pulsesink->defer_pending--;
1224 pa_threaded_mainloop_signal (mainloop, 0);
1227 /* stop playback, we flush everything. */
1229 gst_pulseringbuffer_stop (GstRingBuffer * buf)
1231 GstPulseSink *psink;
1232 GstPulseRingBuffer *pbuf;
1233 gboolean res = FALSE;
1234 pa_operation *o = NULL;
1236 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1237 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1239 pa_threaded_mainloop_lock (mainloop);
1241 pbuf->paused = TRUE;
1242 res = gst_pulsering_set_corked (pbuf, TRUE, TRUE);
1244 /* Inform anyone waiting in _commit() call that it shall wakeup */
1245 if (pbuf->in_commit) {
1246 GST_DEBUG_OBJECT (psink, "signal commit thread");
1247 pa_threaded_mainloop_signal (mainloop, 0);
1249 #ifdef HAVE_PULSE_1_0
1250 if (g_atomic_int_get (&psink->format_lost)) {
1251 /* Don't try to flush, the stream's probably gone by now */
1257 /* then try to flush, it's not fatal when this fails */
1258 GST_DEBUG_OBJECT (psink, "flushing");
1259 if ((o = pa_stream_flush (pbuf->stream, gst_pulsering_success_cb, pbuf))) {
1260 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1261 GST_DEBUG_OBJECT (psink, "wait for completion");
1262 pa_threaded_mainloop_wait (mainloop);
1263 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
1266 GST_DEBUG_OBJECT (psink, "flush completed");
1272 pa_operation_cancel (o);
1273 pa_operation_unref (o);
1276 GST_DEBUG_OBJECT (psink, "scheduling stream status");
1277 psink->defer_pending++;
1278 pa_mainloop_api_once (pa_threaded_mainloop_get_api (mainloop),
1279 mainloop_leave_defer_cb, psink);
1281 pa_threaded_mainloop_unlock (mainloop);
1288 GST_DEBUG_OBJECT (psink, "the server is dead");
1293 /* in_samples >= out_samples, rate > 1.0 */
1294 #define FWD_UP_SAMPLES(s,se,d,de) \
1296 guint8 *sb = s, *db = d; \
1297 while (s <= se && d < de) { \
1298 memcpy (d, s, bpf); \
1301 if ((*accum << 1) >= inr) { \
1306 in_samples -= (s - sb)/bpf; \
1307 out_samples -= (d - db)/bpf; \
1308 GST_DEBUG ("fwd_up end %d/%d",*accum,*toprocess); \
1311 /* out_samples > in_samples, for rates smaller than 1.0 */
1312 #define FWD_DOWN_SAMPLES(s,se,d,de) \
1314 guint8 *sb = s, *db = d; \
1315 while (s <= se && d < de) { \
1316 memcpy (d, s, bpf); \
1319 if ((*accum << 1) >= outr) { \
1324 in_samples -= (s - sb)/bpf; \
1325 out_samples -= (d - db)/bpf; \
1326 GST_DEBUG ("fwd_down end %d/%d",*accum,*toprocess); \
1329 #define REV_UP_SAMPLES(s,se,d,de) \
1331 guint8 *sb = se, *db = d; \
1332 while (s <= se && d < de) { \
1333 memcpy (d, se, bpf); \
1336 while (d < de && (*accum << 1) >= inr) { \
1341 in_samples -= (sb - se)/bpf; \
1342 out_samples -= (d - db)/bpf; \
1343 GST_DEBUG ("rev_up end %d/%d",*accum,*toprocess); \
1346 #define REV_DOWN_SAMPLES(s,se,d,de) \
1348 guint8 *sb = se, *db = d; \
1349 while (s <= se && d < de) { \
1350 memcpy (d, se, bpf); \
1353 while (s <= se && (*accum << 1) >= outr) { \
1358 in_samples -= (sb - se)/bpf; \
1359 out_samples -= (d - db)/bpf; \
1360 GST_DEBUG ("rev_down end %d/%d",*accum,*toprocess); \
1363 /* our custom commit function because we write into the buffer of pulseaudio
1364 * instead of keeping our own buffer */
1366 gst_pulseringbuffer_commit (GstRingBuffer * buf, guint64 * sample,
1367 guchar * data, gint in_samples, gint out_samples, gint * accum)
1369 GstPulseSink *psink;
1370 GstPulseRingBuffer *pbuf;
1375 gint inr, outr, bpf;
1379 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1380 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1382 /* FIXME post message rather than using a signal (as mixer interface) */
1383 if (g_atomic_int_compare_and_exchange (&psink->notify, 1, 0)) {
1384 g_object_notify (G_OBJECT (psink), "volume");
1385 g_object_notify (G_OBJECT (psink), "mute");
1388 /* make sure the ringbuffer is started */
1389 if (G_UNLIKELY (g_atomic_int_get (&buf->state) !=
1390 GST_RING_BUFFER_STATE_STARTED)) {
1391 /* see if we are allowed to start it */
1392 if (G_UNLIKELY (g_atomic_int_get (&buf->may_start) == FALSE))
1395 GST_DEBUG_OBJECT (buf, "start!");
1396 if (!gst_ring_buffer_start (buf))
1400 pa_threaded_mainloop_lock (mainloop);
1402 GST_DEBUG_OBJECT (psink, "entering commit");
1403 pbuf->in_commit = TRUE;
1405 bpf = GST_AUDIO_INFO_BPF (&buf->spec.info);
1406 bufsize = buf->spec.segsize * buf->spec.segtotal;
1408 /* our toy resampler for trick modes */
1409 reverse = out_samples < 0;
1410 out_samples = ABS (out_samples);
1412 if (in_samples >= out_samples)
1413 toprocess = &in_samples;
1415 toprocess = &out_samples;
1417 inr = in_samples - 1;
1418 outr = out_samples - 1;
1420 GST_DEBUG_OBJECT (psink, "in %d, out %d", inr, outr);
1422 /* data_end points to the last sample we have to write, not past it. This is
1423 * needed to properly handle reverse playback: it points to the last sample. */
1424 data_end = data + (bpf * inr);
1426 #ifdef HAVE_PULSE_1_0
1427 if (g_atomic_int_get (&psink->format_lost)) {
1428 /* Sink format changed, drop the data and hope upstream renegotiates */
1436 /* offset is in bytes */
1437 offset = *sample * bpf;
1439 while (*toprocess > 0) {
1443 GST_LOG_OBJECT (psink,
1444 "need to write %d samples at offset %" G_GINT64_FORMAT, *toprocess,
1447 if (offset != pbuf->m_lastoffset)
1448 GST_LOG_OBJECT (psink, "discontinuity, offset is %" G_GINT64_FORMAT ", "
1449 "last offset was %" G_GINT64_FORMAT, offset, pbuf->m_lastoffset);
1451 towrite = out_samples * bpf;
1453 /* Wait for at least segsize bytes to become available */
1454 if (towrite > buf->spec.segsize)
1455 towrite = buf->spec.segsize;
1457 if ((pbuf->m_writable < towrite) || (offset != pbuf->m_lastoffset)) {
1458 /* if no room left or discontinuity in offset,
1459 we need to flush data and get a new buffer */
1461 /* flush the buffer if possible */
1462 if ((pbuf->m_data != NULL) && (pbuf->m_towrite > 0)) {
1464 GST_LOG_OBJECT (psink,
1465 "flushing %u samples at offset %" G_GINT64_FORMAT,
1466 (guint) pbuf->m_towrite / bpf, pbuf->m_offset);
1468 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1469 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1473 pbuf->m_towrite = 0;
1474 pbuf->m_offset = offset; /* keep track of current offset */
1476 /* get a buffer to write in for now on */
1478 pbuf->m_writable = pa_stream_writable_size (pbuf->stream);
1480 #ifdef HAVE_PULSE_1_0
1481 if (g_atomic_int_get (&psink->format_lost)) {
1482 /* Sink format changed, give up and hope upstream renegotiates */
1487 if (pbuf->m_writable == (size_t) - 1)
1488 goto writable_size_failed;
1490 pbuf->m_writable /= bpf;
1491 pbuf->m_writable *= bpf; /* handle only complete samples */
1493 if (pbuf->m_writable >= towrite)
1496 /* see if we need to uncork because we have no free space */
1498 if (!gst_pulsering_set_corked (pbuf, FALSE, FALSE))
1502 /* we can't write segsize bytes, wait a bit */
1503 GST_LOG_OBJECT (psink, "waiting for free space");
1504 pa_threaded_mainloop_wait (mainloop);
1510 /* Recalculate what we can write in the next chunk */
1511 towrite = out_samples * bpf;
1512 if (pbuf->m_writable > towrite)
1513 pbuf->m_writable = towrite;
1515 GST_LOG_OBJECT (psink, "requesting %" G_GSIZE_FORMAT " bytes of "
1516 "shared memory", pbuf->m_writable);
1518 if (pa_stream_begin_write (pbuf->stream, &pbuf->m_data,
1519 &pbuf->m_writable) < 0) {
1520 GST_LOG_OBJECT (psink, "pa_stream_begin_write() failed");
1521 goto writable_size_failed;
1524 GST_LOG_OBJECT (psink, "got %" G_GSIZE_FORMAT " bytes of shared memory",
1529 if (towrite > pbuf->m_writable)
1530 towrite = pbuf->m_writable;
1531 avail = towrite / bpf;
1533 GST_LOG_OBJECT (psink, "writing %u samples at offset %" G_GUINT64_FORMAT,
1534 (guint) avail, offset);
1536 #ifdef HAVE_PULSE_1_0
1537 /* No trick modes for passthrough streams */
1538 if (G_UNLIKELY (inr != outr || reverse)) {
1539 GST_WARNING_OBJECT (psink, "Passthrough stream can't run in trick mode");
1540 goto unlock_and_fail;
1544 if (G_LIKELY (inr == outr && !reverse)) {
1545 /* no rate conversion, simply write out the samples */
1546 /* copy the data into internal buffer */
1548 memcpy ((guint8 *) pbuf->m_data + pbuf->m_towrite, data, towrite);
1549 pbuf->m_towrite += towrite;
1550 pbuf->m_writable -= towrite;
1553 in_samples -= avail;
1554 out_samples -= avail;
1556 guint8 *dest, *d, *d_end;
1558 /* write into the PulseAudio shm buffer */
1559 dest = d = (guint8 *) pbuf->m_data + pbuf->m_towrite;
1560 d_end = d + towrite;
1564 /* forward speed up */
1565 FWD_UP_SAMPLES (data, data_end, d, d_end);
1567 /* forward slow down */
1568 FWD_DOWN_SAMPLES (data, data_end, d, d_end);
1571 /* reverse speed up */
1572 REV_UP_SAMPLES (data, data_end, d, d_end);
1574 /* reverse slow down */
1575 REV_DOWN_SAMPLES (data, data_end, d, d_end);
1577 /* see what we have left to write */
1578 towrite = (d - dest);
1579 pbuf->m_towrite += towrite;
1580 pbuf->m_writable -= towrite;
1582 avail = towrite / bpf;
1585 /* flush the buffer if it's full */
1586 if ((pbuf->m_data != NULL) && (pbuf->m_towrite > 0)
1587 && (pbuf->m_writable == 0)) {
1588 GST_LOG_OBJECT (psink, "flushing %u samples at offset %" G_GINT64_FORMAT,
1589 (guint) pbuf->m_towrite / bpf, pbuf->m_offset);
1591 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1592 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1595 pbuf->m_towrite = 0;
1596 pbuf->m_offset = offset + towrite; /* keep track of current offset */
1600 offset += avail * bpf;
1601 pbuf->m_lastoffset = offset;
1603 /* check if we need to uncork after writing the samples */
1605 const pa_timing_info *info;
1607 if ((info = pa_stream_get_timing_info (pbuf->stream))) {
1608 GST_LOG_OBJECT (psink,
1609 "read_index at %" G_GUINT64_FORMAT ", offset %" G_GINT64_FORMAT,
1610 info->read_index, offset);
1612 /* we uncork when the read_index is too far behind the offset we need
1614 if (info->read_index + bufsize <= offset) {
1615 if (!gst_pulsering_set_corked (pbuf, FALSE, FALSE))
1619 GST_LOG_OBJECT (psink, "no timing info available yet");
1624 #ifdef HAVE_PULSE_1_0
1627 /* we consumed all samples here */
1628 data = data_end + bpf;
1630 pbuf->in_commit = FALSE;
1631 pa_threaded_mainloop_unlock (mainloop);
1634 result = inr - ((data_end - data) / bpf);
1635 GST_LOG_OBJECT (psink, "wrote %d samples", result);
1642 pbuf->in_commit = FALSE;
1643 GST_LOG_OBJECT (psink, "we are reset");
1644 pa_threaded_mainloop_unlock (mainloop);
1649 GST_LOG_OBJECT (psink, "we can not start");
1654 GST_LOG_OBJECT (psink, "failed to start the ringbuffer");
1659 pbuf->in_commit = FALSE;
1660 GST_ERROR_OBJECT (psink, "uncork failed");
1661 pa_threaded_mainloop_unlock (mainloop);
1666 pbuf->in_commit = FALSE;
1667 GST_LOG_OBJECT (psink, "we are paused");
1668 pa_threaded_mainloop_unlock (mainloop);
1671 writable_size_failed:
1673 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1674 ("pa_stream_writable_size() failed: %s",
1675 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1676 goto unlock_and_fail;
1680 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1681 ("pa_stream_write() failed: %s",
1682 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1683 goto unlock_and_fail;
1687 /* write pending local samples, must be called with the mainloop lock */
1689 gst_pulsering_flush (GstPulseRingBuffer * pbuf)
1691 GstPulseSink *psink;
1693 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1694 GST_DEBUG_OBJECT (psink, "entering flush");
1696 /* flush the buffer if possible */
1697 if (pbuf->stream && (pbuf->m_data != NULL) && (pbuf->m_towrite > 0)) {
1698 #ifndef GST_DISABLE_GST_DEBUG
1701 bpf = (GST_RING_BUFFER_CAST (pbuf))->spec.info.bpf;
1702 GST_LOG_OBJECT (psink,
1703 "flushing %u samples at offset %" G_GINT64_FORMAT,
1704 (guint) pbuf->m_towrite / bpf, pbuf->m_offset);
1707 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1708 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1712 pbuf->m_towrite = 0;
1713 pbuf->m_offset += pbuf->m_towrite; /* keep track of current offset */
1722 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1723 ("pa_stream_write() failed: %s",
1724 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1729 static void gst_pulsesink_set_property (GObject * object, guint prop_id,
1730 const GValue * value, GParamSpec * pspec);
1731 static void gst_pulsesink_get_property (GObject * object, guint prop_id,
1732 GValue * value, GParamSpec * pspec);
1733 static void gst_pulsesink_finalize (GObject * object);
1735 static gboolean gst_pulsesink_event (GstBaseSink * sink, GstEvent * event);
1737 static GstStateChangeReturn gst_pulsesink_change_state (GstElement * element,
1738 GstStateChange transition);
1740 static GstStaticPadTemplate pad_template = GST_STATIC_PAD_TEMPLATE ("sink",
1743 GST_STATIC_CAPS (PULSE_SINK_TEMPLATE_CAPS));
1745 GST_IMPLEMENT_PULSEPROBE_METHODS (GstPulseSink, gst_pulsesink);
1747 #define gst_pulsesink_parent_class parent_class
1748 G_DEFINE_TYPE_WITH_CODE (GstPulseSink, gst_pulsesink, GST_TYPE_BASE_AUDIO_SINK,
1749 gst_pulsesink_init_contexts ();
1750 G_IMPLEMENT_INTERFACE (GST_TYPE_PROPERTY_PROBE,
1751 gst_pulsesink_property_probe_interface_init);
1752 G_IMPLEMENT_INTERFACE (GST_TYPE_STREAM_VOLUME, NULL)
1755 static GstRingBuffer *
1756 gst_pulsesink_create_ringbuffer (GstBaseAudioSink * sink)
1758 GstRingBuffer *buffer;
1760 GST_DEBUG_OBJECT (sink, "creating ringbuffer");
1761 buffer = g_object_new (GST_TYPE_PULSERING_BUFFER, NULL);
1762 GST_DEBUG_OBJECT (sink, "created ringbuffer @%p", buffer);
1768 gst_pulsesink_payload (GstBaseAudioSink * sink, GstBuffer * buf)
1770 switch (sink->ringbuffer->spec.type) {
1771 case GST_BUFTYPE_AC3:
1772 case GST_BUFTYPE_EAC3:
1773 case GST_BUFTYPE_DTS:
1774 case GST_BUFTYPE_MPEG:
1776 /* FIXME: alloc memory from PA if possible */
1777 gint framesize = gst_audio_iec61937_frame_size (&sink->ringbuffer->spec);
1779 guint8 *indata, *outdata;
1780 gsize insize, outsize;
1786 out = gst_buffer_new_and_alloc (framesize);
1788 indata = gst_buffer_map (buf, &insize, NULL, GST_MAP_READ);
1789 outdata = gst_buffer_map (out, &outsize, NULL, GST_MAP_WRITE);
1791 res = gst_audio_iec61937_payload (indata, insize,
1792 outdata, outsize, &sink->ringbuffer->spec);
1794 gst_buffer_unmap (buf, indata, insize);
1795 gst_buffer_unmap (out, outdata, outsize);
1798 gst_buffer_unref (out);
1802 gst_buffer_copy_into (out, buf, GST_BUFFER_COPY_METADATA, 0, -1);
1807 return gst_buffer_ref (buf);
1812 gst_pulsesink_class_init (GstPulseSinkClass * klass)
1814 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1815 GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
1816 GstBaseSinkClass *bc;
1817 GstBaseAudioSinkClass *gstaudiosink_class = GST_BASE_AUDIO_SINK_CLASS (klass);
1818 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1820 gobject_class->finalize = gst_pulsesink_finalize;
1821 gobject_class->set_property = gst_pulsesink_set_property;
1822 gobject_class->get_property = gst_pulsesink_get_property;
1824 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_pulsesink_event);
1826 /* restore the original basesink pull methods */
1827 bc = g_type_class_peek (GST_TYPE_BASE_SINK);
1828 gstbasesink_class->activate_pull = GST_DEBUG_FUNCPTR (bc->activate_pull);
1830 gstelement_class->change_state =
1831 GST_DEBUG_FUNCPTR (gst_pulsesink_change_state);
1833 gstaudiosink_class->create_ringbuffer =
1834 GST_DEBUG_FUNCPTR (gst_pulsesink_create_ringbuffer);
1835 gstaudiosink_class->payload = GST_DEBUG_FUNCPTR (gst_pulsesink_payload);
1837 /* Overwrite GObject fields */
1838 g_object_class_install_property (gobject_class,
1840 g_param_spec_string ("server", "Server",
1841 "The PulseAudio server to connect to", DEFAULT_SERVER,
1842 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1844 g_object_class_install_property (gobject_class, PROP_DEVICE,
1845 g_param_spec_string ("device", "Device",
1846 "The PulseAudio sink device to connect to", DEFAULT_DEVICE,
1847 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1849 g_object_class_install_property (gobject_class,
1851 g_param_spec_string ("device-name", "Device name",
1852 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
1853 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1855 g_object_class_install_property (gobject_class,
1857 g_param_spec_double ("volume", "Volume",
1858 "Linear volume of this stream, 1.0=100%", 0.0, MAX_VOLUME,
1859 DEFAULT_VOLUME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1860 g_object_class_install_property (gobject_class,
1862 g_param_spec_boolean ("mute", "Mute",
1863 "Mute state of this stream", DEFAULT_MUTE,
1864 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1867 * GstPulseSink:client
1869 * The PulseAudio client name to use.
1873 g_object_class_install_property (gobject_class,
1875 g_param_spec_string ("client", "Client",
1876 "The PulseAudio client name to use", gst_pulse_client_name (),
1877 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1878 GST_PARAM_MUTABLE_READY));
1881 * GstPulseSink:stream-properties
1883 * List of pulseaudio stream properties. A list of defined properties can be
1884 * found in the <ulink url="http://0pointer.de/lennart/projects/pulseaudio/doxygen/proplist_8h.html">pulseaudio api docs</ulink>.
1886 * Below is an example for registering as a music application to pulseaudio.
1888 * GstStructure *props;
1890 * props = gst_structure_from_string ("props,media.role=music", NULL);
1891 * g_object_set (pulse, "stream-properties", props, NULL);
1892 * gst_structure_free
1897 g_object_class_install_property (gobject_class,
1898 PROP_STREAM_PROPERTIES,
1899 g_param_spec_boxed ("stream-properties", "stream properties",
1900 "list of pulseaudio stream properties",
1901 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1903 gst_element_class_set_details_simple (gstelement_class,
1904 "PulseAudio Audio Sink",
1905 "Sink/Audio", "Plays audio to a PulseAudio server", "Lennart Poettering");
1906 gst_element_class_add_pad_template (gstelement_class,
1907 gst_static_pad_template_get (&pad_template));
1910 /* returns the current time of the sink ringbuffer */
1912 gst_pulsesink_get_time (GstClock * clock, GstBaseAudioSink * sink)
1914 GstPulseSink *psink;
1915 GstPulseRingBuffer *pbuf;
1918 if (!sink->ringbuffer || !sink->ringbuffer->acquired)
1919 return GST_CLOCK_TIME_NONE;
1921 pbuf = GST_PULSERING_BUFFER_CAST (sink->ringbuffer);
1922 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1924 #ifdef HAVE_PULSE_1_0
1925 if (g_atomic_int_get (&psink->format_lost)) {
1926 /* Stream was lost in a format change, it'll get set up again once
1927 * upstream renegotiates */
1928 return psink->format_lost_time;
1932 pa_threaded_mainloop_lock (mainloop);
1933 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
1936 /* if we don't have enough data to get a timestamp, just return NONE, which
1937 * will return the last reported time */
1938 if (pa_stream_get_time (pbuf->stream, &time) < 0) {
1939 GST_DEBUG_OBJECT (psink, "could not get time");
1940 time = GST_CLOCK_TIME_NONE;
1943 pa_threaded_mainloop_unlock (mainloop);
1945 GST_LOG_OBJECT (psink, "current time is %" GST_TIME_FORMAT,
1946 GST_TIME_ARGS (time));
1953 GST_DEBUG_OBJECT (psink, "the server is dead");
1954 pa_threaded_mainloop_unlock (mainloop);
1956 return GST_CLOCK_TIME_NONE;
1961 gst_pulsesink_sink_info_cb (pa_context * c, const pa_sink_info * i, int eol,
1964 GstPulseRingBuffer *pbuf;
1965 GstPulseSink *psink;
1966 #ifdef HAVE_PULSE_1_0
1971 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
1972 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1977 g_free (psink->device_description);
1978 psink->device_description = g_strdup (i->description);
1980 #ifdef HAVE_PULSE_1_0
1981 g_mutex_lock (psink->sink_formats_lock);
1983 for (l = g_list_first (psink->sink_formats); l; l = g_list_next (l))
1984 pa_format_info_free ((pa_format_info *) l->data);
1986 g_list_free (psink->sink_formats);
1987 psink->sink_formats = NULL;
1989 for (j = 0; j < i->n_formats; j++)
1990 psink->sink_formats = g_list_prepend (psink->sink_formats,
1991 pa_format_info_copy (i->formats[j]));
1993 g_mutex_unlock (psink->sink_formats_lock);
1997 pa_threaded_mainloop_signal (mainloop, 0);
2000 #ifdef HAVE_PULSE_1_0
2001 /* NOTE: If you're making changes here, see if pulseaudiosink acceptcaps also
2002 * needs to be changed accordingly. */
2004 gst_pulsesink_pad_acceptcaps (GstPad * pad, GstCaps * caps)
2006 GstPulseSink *psink = GST_PULSESINK (gst_pad_get_parent_element (pad));
2007 GstPulseRingBuffer *pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK
2008 (psink)->ringbuffer);
2011 gboolean ret = FALSE;
2013 GstRingBufferSpec spec = { 0 };
2014 pa_stream *stream = NULL;
2015 pa_operation *o = NULL;
2016 pa_channel_map channel_map;
2017 pa_stream_flags_t flags;
2018 pa_format_info *format = NULL, *formats[1];
2021 pad_caps = gst_pad_get_caps_reffed (pad);
2023 ret = gst_caps_can_intersect (pad_caps, caps);
2024 gst_caps_unref (pad_caps);
2027 /* Either template caps didn't match, or we're still in NULL state */
2028 if (!ret || !pbuf->context)
2031 /* If we've not got fixed caps, creating a stream might fail, so let's just
2032 * return from here with default acceptcaps behaviour */
2033 if (!gst_caps_is_fixed (caps))
2038 pa_threaded_mainloop_lock (mainloop);
2040 spec.latency_time = GST_BASE_AUDIO_SINK (psink)->latency_time;
2041 if (!gst_ring_buffer_parse_caps (&spec, caps))
2044 if (!gst_pulse_fill_format_info (&spec, &format, &channels))
2047 /* Make sure input is framed (one frame per buffer) and can be payloaded */
2048 if (!pa_format_info_is_pcm (format)) {
2049 gboolean framed = FALSE, parsed = FALSE;
2050 st = gst_caps_get_structure (caps, 0);
2052 gst_structure_get_boolean (st, "framed", &framed);
2053 gst_structure_get_boolean (st, "parsed", &parsed);
2054 if ((!framed && !parsed) || gst_audio_iec61937_frame_size (&spec) <= 0)
2058 /* initialize the channel map */
2059 if (pa_format_info_is_pcm (format) &&
2060 gst_pulse_gst_to_channel_map (&channel_map, &spec))
2061 pa_format_info_set_channel_map (format, &channel_map);
2064 /* We're already in PAUSED or above, so just reuse this stream to query
2065 * sink formats and use those. */
2068 if (!(o = pa_context_get_sink_info_by_name (pbuf->context, psink->device,
2069 gst_pulsesink_sink_info_cb, pbuf)))
2072 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2073 pa_threaded_mainloop_wait (mainloop);
2074 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
2078 g_mutex_lock (psink->sink_formats_lock);
2079 for (i = g_list_first (psink->sink_formats); i; i = g_list_next (i)) {
2080 if (pa_format_info_is_compatible ((pa_format_info *) i->data, format)) {
2085 g_mutex_unlock (psink->sink_formats_lock);
2087 /* We're in READY, let's connect a stream to see if the format is
2088 * accpeted by whatever sink we're routed to */
2089 formats[0] = format;
2091 if (!(stream = pa_stream_new_extended (pbuf->context, "pulsesink probe",
2092 formats, 1, psink->proplist)))
2095 /* construct the flags */
2096 flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE |
2097 PA_STREAM_ADJUST_LATENCY | PA_STREAM_START_CORKED;
2099 pa_stream_set_state_callback (stream, gst_pulsering_stream_state_cb, pbuf);
2101 if (pa_stream_connect_playback (stream, psink->device, NULL, flags, NULL,
2105 ret = gst_pulsering_wait_for_stream_ready (psink, stream);
2110 pa_format_info_free (format);
2113 pa_operation_unref (o);
2116 pa_stream_set_state_callback (stream, NULL, NULL);
2117 pa_stream_disconnect (stream);
2118 pa_stream_unref (stream);
2121 pa_threaded_mainloop_unlock (mainloop);
2124 gst_object_unref (psink);
2129 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2130 ("pa_context_get_sink_input_info() failed: %s",
2131 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2138 gst_pulsesink_init (GstPulseSink * pulsesink)
2140 pulsesink->server = NULL;
2141 pulsesink->device = NULL;
2142 pulsesink->device_description = NULL;
2143 pulsesink->client_name = gst_pulse_client_name ();
2145 #ifdef HAVE_PULSE_1_0
2146 pulsesink->sink_formats_lock = g_mutex_new ();
2147 pulsesink->sink_formats = NULL;
2150 pulsesink->volume = DEFAULT_VOLUME;
2151 pulsesink->volume_set = FALSE;
2153 pulsesink->mute = DEFAULT_MUTE;
2154 pulsesink->mute_set = FALSE;
2156 pulsesink->notify = 0;
2158 #ifdef HAVE_PULSE_1_0
2159 g_atomic_int_set (&pulsesink->format_lost, FALSE);
2160 pulsesink->format_lost_time = GST_CLOCK_TIME_NONE;
2163 pulsesink->properties = NULL;
2164 pulsesink->proplist = NULL;
2166 /* override with a custom clock */
2167 if (GST_BASE_AUDIO_SINK (pulsesink)->provided_clock)
2168 gst_object_unref (GST_BASE_AUDIO_SINK (pulsesink)->provided_clock);
2170 GST_BASE_AUDIO_SINK (pulsesink)->provided_clock =
2171 gst_audio_clock_new ("GstPulseSinkClock",
2172 (GstAudioClockGetTimeFunc) gst_pulsesink_get_time, pulsesink);
2174 #ifdef HAVE_PULSE_1_0
2175 gst_pad_set_acceptcaps_function (GST_BASE_SINK (pulsesink)->sinkpad,
2176 GST_DEBUG_FUNCPTR (gst_pulsesink_pad_acceptcaps));
2179 /* TRUE for sinks, FALSE for sources */
2180 pulsesink->probe = gst_pulseprobe_new (G_OBJECT (pulsesink),
2181 G_OBJECT_GET_CLASS (pulsesink), PROP_DEVICE, pulsesink->device,
2186 gst_pulsesink_finalize (GObject * object)
2188 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2189 #ifdef HAVE_PULSE_1_0
2193 g_free (pulsesink->server);
2194 g_free (pulsesink->device);
2195 g_free (pulsesink->device_description);
2196 g_free (pulsesink->client_name);
2198 #ifdef HAVE_PULSE_1_0
2199 for (i = g_list_first (pulsesink->sink_formats); i; i = g_list_next (i))
2200 pa_format_info_free ((pa_format_info *) i->data);
2202 g_list_free (pulsesink->sink_formats);
2203 g_mutex_free (pulsesink->sink_formats_lock);
2206 if (pulsesink->properties)
2207 gst_structure_free (pulsesink->properties);
2208 if (pulsesink->proplist)
2209 pa_proplist_free (pulsesink->proplist);
2211 if (pulsesink->probe) {
2212 gst_pulseprobe_free (pulsesink->probe);
2213 pulsesink->probe = NULL;
2216 G_OBJECT_CLASS (parent_class)->finalize (object);
2220 gst_pulsesink_set_volume (GstPulseSink * psink, gdouble volume)
2223 pa_operation *o = NULL;
2224 GstPulseRingBuffer *pbuf;
2230 pa_threaded_mainloop_lock (mainloop);
2232 GST_DEBUG_OBJECT (psink, "setting volume to %f", volume);
2234 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2235 if (pbuf == NULL || pbuf->stream == NULL)
2238 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2241 #ifdef HAVE_PULSE_1_0
2242 if (pa_format_info_is_pcm (pbuf->format))
2243 gst_pulse_cvolume_from_linear (&v, pbuf->channels, volume);
2245 /* FIXME: this will eventually be superceded by checks to see if the volume
2246 * is readable/writable */
2249 gst_pulse_cvolume_from_linear (&v, pbuf->sample_spec.channels, volume);
2252 if (!(o = pa_context_set_sink_input_volume (pbuf->context, idx,
2256 /* We don't really care about the result of this call */
2260 pa_operation_unref (o);
2262 pa_threaded_mainloop_unlock (mainloop);
2269 psink->volume = volume;
2270 psink->volume_set = TRUE;
2272 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2277 psink->volume = volume;
2278 psink->volume_set = TRUE;
2280 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2285 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2290 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2291 ("pa_stream_set_sink_input_volume() failed: %s",
2292 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2298 gst_pulsesink_set_mute (GstPulseSink * psink, gboolean mute)
2300 pa_operation *o = NULL;
2301 GstPulseRingBuffer *pbuf;
2307 pa_threaded_mainloop_lock (mainloop);
2309 GST_DEBUG_OBJECT (psink, "setting mute state to %d", mute);
2311 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2312 if (pbuf == NULL || pbuf->stream == NULL)
2315 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2318 if (!(o = pa_context_set_sink_input_mute (pbuf->context, idx,
2322 /* We don't really care about the result of this call */
2326 pa_operation_unref (o);
2328 pa_threaded_mainloop_unlock (mainloop);
2336 psink->mute_set = TRUE;
2338 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2344 psink->mute_set = TRUE;
2346 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2351 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2356 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2357 ("pa_stream_set_sink_input_mute() failed: %s",
2358 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2364 gst_pulsesink_sink_input_info_cb (pa_context * c, const pa_sink_input_info * i,
2365 int eol, void *userdata)
2367 GstPulseRingBuffer *pbuf;
2368 GstPulseSink *psink;
2370 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
2371 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
2379 /* If the index doesn't match our current stream,
2380 * it implies we just recreated the stream (caps change)
2382 if (i->index == pa_stream_get_index (pbuf->stream)) {
2383 psink->volume = pa_sw_volume_to_linear (pa_cvolume_max (&i->volume));
2384 psink->mute = i->mute;
2388 pa_threaded_mainloop_signal (mainloop, 0);
2392 gst_pulsesink_get_volume (GstPulseSink * psink)
2394 GstPulseRingBuffer *pbuf;
2395 pa_operation *o = NULL;
2396 gdouble v = DEFAULT_VOLUME;
2402 pa_threaded_mainloop_lock (mainloop);
2404 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2405 if (pbuf == NULL || pbuf->stream == NULL)
2408 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2411 if (!(o = pa_context_get_sink_input_info (pbuf->context, idx,
2412 gst_pulsesink_sink_input_info_cb, pbuf)))
2415 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2416 pa_threaded_mainloop_wait (mainloop);
2417 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
2425 pa_operation_unref (o);
2427 pa_threaded_mainloop_unlock (mainloop);
2429 if (v > MAX_VOLUME) {
2430 GST_WARNING_OBJECT (psink, "Clipped volume from %f to %f", v, MAX_VOLUME);
2440 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2445 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2450 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2455 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2456 ("pa_context_get_sink_input_info() failed: %s",
2457 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2463 gst_pulsesink_get_mute (GstPulseSink * psink)
2465 GstPulseRingBuffer *pbuf;
2466 pa_operation *o = NULL;
2468 gboolean mute = FALSE;
2473 pa_threaded_mainloop_lock (mainloop);
2476 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2477 if (pbuf == NULL || pbuf->stream == NULL)
2480 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2483 if (!(o = pa_context_get_sink_input_info (pbuf->context, idx,
2484 gst_pulsesink_sink_input_info_cb, pbuf)))
2487 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2488 pa_threaded_mainloop_wait (mainloop);
2489 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
2495 pa_operation_unref (o);
2497 pa_threaded_mainloop_unlock (mainloop);
2505 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2510 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2515 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2520 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2521 ("pa_context_get_sink_input_info() failed: %s",
2522 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2528 gst_pulsesink_device_description (GstPulseSink * psink)
2530 GstPulseRingBuffer *pbuf;
2531 pa_operation *o = NULL;
2537 pa_threaded_mainloop_lock (mainloop);
2538 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2542 if (!(o = pa_context_get_sink_info_by_name (pbuf->context,
2543 psink->device, gst_pulsesink_sink_info_cb, pbuf)))
2546 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2547 pa_threaded_mainloop_wait (mainloop);
2548 if (gst_pulsering_is_dead (psink, pbuf, FALSE))
2554 pa_operation_unref (o);
2556 t = g_strdup (psink->device_description);
2557 pa_threaded_mainloop_unlock (mainloop);
2564 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2569 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2574 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2575 ("pa_context_get_sink_info_by_index() failed: %s",
2576 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2582 gst_pulsesink_set_property (GObject * object,
2583 guint prop_id, const GValue * value, GParamSpec * pspec)
2585 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2589 g_free (pulsesink->server);
2590 pulsesink->server = g_value_dup_string (value);
2591 if (pulsesink->probe)
2592 gst_pulseprobe_set_server (pulsesink->probe, pulsesink->server);
2595 g_free (pulsesink->device);
2596 pulsesink->device = g_value_dup_string (value);
2599 gst_pulsesink_set_volume (pulsesink, g_value_get_double (value));
2602 gst_pulsesink_set_mute (pulsesink, g_value_get_boolean (value));
2605 g_free (pulsesink->client_name);
2606 if (!g_value_get_string (value)) {
2607 GST_WARNING_OBJECT (pulsesink,
2608 "Empty PulseAudio client name not allowed. Resetting to default value");
2609 pulsesink->client_name = gst_pulse_client_name ();
2611 pulsesink->client_name = g_value_dup_string (value);
2613 case PROP_STREAM_PROPERTIES:
2614 if (pulsesink->properties)
2615 gst_structure_free (pulsesink->properties);
2616 pulsesink->properties =
2617 gst_structure_copy (gst_value_get_structure (value));
2618 if (pulsesink->proplist)
2619 pa_proplist_free (pulsesink->proplist);
2620 pulsesink->proplist = gst_pulse_make_proplist (pulsesink->properties);
2623 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2629 gst_pulsesink_get_property (GObject * object,
2630 guint prop_id, GValue * value, GParamSpec * pspec)
2633 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2637 g_value_set_string (value, pulsesink->server);
2640 g_value_set_string (value, pulsesink->device);
2642 case PROP_DEVICE_NAME:
2643 g_value_take_string (value, gst_pulsesink_device_description (pulsesink));
2646 g_value_set_double (value, gst_pulsesink_get_volume (pulsesink));
2649 g_value_set_boolean (value, gst_pulsesink_get_mute (pulsesink));
2652 g_value_set_string (value, pulsesink->client_name);
2654 case PROP_STREAM_PROPERTIES:
2655 gst_value_set_structure (value, pulsesink->properties);
2658 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2664 gst_pulsesink_change_title (GstPulseSink * psink, const gchar * t)
2666 pa_operation *o = NULL;
2667 GstPulseRingBuffer *pbuf;
2669 pa_threaded_mainloop_lock (mainloop);
2671 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2673 if (pbuf == NULL || pbuf->stream == NULL)
2676 g_free (pbuf->stream_name);
2677 pbuf->stream_name = g_strdup (t);
2679 if (!(o = pa_stream_set_name (pbuf->stream, pbuf->stream_name, NULL, NULL)))
2682 /* We're not interested if this operation failed or not */
2686 pa_operation_unref (o);
2687 pa_threaded_mainloop_unlock (mainloop);
2694 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2699 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2700 ("pa_stream_set_name() failed: %s",
2701 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2707 gst_pulsesink_change_props (GstPulseSink * psink, GstTagList * l)
2709 static const gchar *const map[] = {
2710 GST_TAG_TITLE, PA_PROP_MEDIA_TITLE,
2712 /* might get overriden in the next iteration by GST_TAG_ARTIST */
2713 GST_TAG_PERFORMER, PA_PROP_MEDIA_ARTIST,
2715 GST_TAG_ARTIST, PA_PROP_MEDIA_ARTIST,
2716 GST_TAG_LANGUAGE_CODE, PA_PROP_MEDIA_LANGUAGE,
2717 GST_TAG_LOCATION, PA_PROP_MEDIA_FILENAME,
2718 /* We might add more here later on ... */
2721 pa_proplist *pl = NULL;
2722 const gchar *const *t;
2723 gboolean empty = TRUE;
2724 pa_operation *o = NULL;
2725 GstPulseRingBuffer *pbuf;
2727 pl = pa_proplist_new ();
2729 for (t = map; *t; t += 2) {
2732 if (gst_tag_list_get_string (l, *t, &n)) {
2735 pa_proplist_sets (pl, *(t + 1), n);
2745 pa_threaded_mainloop_lock (mainloop);
2746 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2747 if (pbuf == NULL || pbuf->stream == NULL)
2750 if (!(o = pa_stream_proplist_update (pbuf->stream, PA_UPDATE_REPLACE,
2754 /* We're not interested if this operation failed or not */
2758 pa_operation_unref (o);
2760 pa_threaded_mainloop_unlock (mainloop);
2765 pa_proplist_free (pl);
2772 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2777 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2778 ("pa_stream_proplist_update() failed: %s",
2779 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2785 gst_pulsesink_flush_ringbuffer (GstPulseSink * psink)
2787 GstPulseRingBuffer *pbuf;
2789 pa_threaded_mainloop_lock (mainloop);
2791 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2793 if (pbuf == NULL || pbuf->stream == NULL)
2796 gst_pulsering_flush (pbuf);
2798 /* Uncork if we haven't already (happens when waiting to get enough data
2799 * to send out the first time) */
2801 gst_pulsering_set_corked (pbuf, FALSE, FALSE);
2803 /* We're not interested if this operation failed or not */
2805 pa_threaded_mainloop_unlock (mainloop);
2812 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2818 gst_pulsesink_event (GstBaseSink * sink, GstEvent * event)
2820 GstPulseSink *pulsesink = GST_PULSESINK_CAST (sink);
2822 switch (GST_EVENT_TYPE (event)) {
2823 case GST_EVENT_TAG:{
2824 gchar *title = NULL, *artist = NULL, *location = NULL, *description =
2825 NULL, *t = NULL, *buf = NULL;
2828 gst_event_parse_tag (event, &l);
2830 gst_tag_list_get_string (l, GST_TAG_TITLE, &title);
2831 gst_tag_list_get_string (l, GST_TAG_ARTIST, &artist);
2832 gst_tag_list_get_string (l, GST_TAG_LOCATION, &location);
2833 gst_tag_list_get_string (l, GST_TAG_DESCRIPTION, &description);
2836 gst_tag_list_get_string (l, GST_TAG_PERFORMER, &artist);
2838 if (title && artist)
2839 /* TRANSLATORS: 'song title' by 'artist name' */
2840 t = buf = g_strdup_printf (_("'%s' by '%s'"), g_strstrip (title),
2841 g_strstrip (artist));
2843 t = g_strstrip (title);
2844 else if (description)
2845 t = g_strstrip (description);
2847 t = g_strstrip (location);
2850 gst_pulsesink_change_title (pulsesink, t);
2855 g_free (description);
2858 gst_pulsesink_change_props (pulsesink, l);
2863 gst_pulsesink_flush_ringbuffer (pulsesink);
2869 return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
2873 gst_pulsesink_release_mainloop (GstPulseSink * psink)
2878 pa_threaded_mainloop_lock (mainloop);
2879 while (psink->defer_pending) {
2880 GST_DEBUG_OBJECT (psink, "waiting for stream status message emission");
2881 pa_threaded_mainloop_wait (mainloop);
2883 pa_threaded_mainloop_unlock (mainloop);
2885 g_mutex_lock (pa_shared_resource_mutex);
2887 if (!mainloop_ref_ct) {
2888 GST_INFO_OBJECT (psink, "terminating pa main loop thread");
2889 pa_threaded_mainloop_stop (mainloop);
2890 pa_threaded_mainloop_free (mainloop);
2893 g_mutex_unlock (pa_shared_resource_mutex);
2896 static GstStateChangeReturn
2897 gst_pulsesink_change_state (GstElement * element, GstStateChange transition)
2899 GstPulseSink *pulsesink = GST_PULSESINK (element);
2900 GstStateChangeReturn ret;
2902 switch (transition) {
2903 case GST_STATE_CHANGE_NULL_TO_READY:
2904 g_mutex_lock (pa_shared_resource_mutex);
2905 if (!mainloop_ref_ct) {
2906 GST_INFO_OBJECT (element, "new pa main loop thread");
2907 if (!(mainloop = pa_threaded_mainloop_new ()))
2908 goto mainloop_failed;
2909 mainloop_ref_ct = 1;
2910 pa_threaded_mainloop_start (mainloop);
2911 g_mutex_unlock (pa_shared_resource_mutex);
2913 GST_INFO_OBJECT (element, "reusing pa main loop thread");
2915 g_mutex_unlock (pa_shared_resource_mutex);
2918 case GST_STATE_CHANGE_READY_TO_PAUSED:
2919 gst_element_post_message (element,
2920 gst_message_new_clock_provide (GST_OBJECT_CAST (element),
2921 GST_BASE_AUDIO_SINK (pulsesink)->provided_clock, TRUE));
2928 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2929 if (ret == GST_STATE_CHANGE_FAILURE)
2932 switch (transition) {
2933 case GST_STATE_CHANGE_PAUSED_TO_READY:
2934 /* format_lost is reset in release() in baseaudiosink */
2935 gst_element_post_message (element,
2936 gst_message_new_clock_lost (GST_OBJECT_CAST (element),
2937 GST_BASE_AUDIO_SINK (pulsesink)->provided_clock));
2939 case GST_STATE_CHANGE_READY_TO_NULL:
2940 gst_pulsesink_release_mainloop (pulsesink);
2951 g_mutex_unlock (pa_shared_resource_mutex);
2952 GST_ELEMENT_ERROR (pulsesink, RESOURCE, FAILED,
2953 ("pa_threaded_mainloop_new() failed"), (NULL));
2954 return GST_STATE_CHANGE_FAILURE;
2958 if (transition == GST_STATE_CHANGE_NULL_TO_READY) {
2959 /* Clear the PA mainloop if baseaudiosink failed to open the ring_buffer */
2960 g_assert (mainloop);
2961 gst_pulsesink_release_mainloop (pulsesink);