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;
157 gboolean in_commit:1;
160 struct _GstPulseRingBufferClass
162 GstRingBufferClass parent_class;
165 static GType gst_pulseringbuffer_get_type (void);
166 static void gst_pulseringbuffer_finalize (GObject * object);
168 static GstRingBufferClass *ring_parent_class = NULL;
170 static gboolean gst_pulseringbuffer_open_device (GstRingBuffer * buf);
171 static gboolean gst_pulseringbuffer_close_device (GstRingBuffer * buf);
172 static gboolean gst_pulseringbuffer_acquire (GstRingBuffer * buf,
173 GstRingBufferSpec * spec);
174 static gboolean gst_pulseringbuffer_release (GstRingBuffer * buf);
175 static gboolean gst_pulseringbuffer_start (GstRingBuffer * buf);
176 static gboolean gst_pulseringbuffer_pause (GstRingBuffer * buf);
177 static gboolean gst_pulseringbuffer_stop (GstRingBuffer * buf);
178 static void gst_pulseringbuffer_clear (GstRingBuffer * buf);
179 static guint gst_pulseringbuffer_commit (GstRingBuffer * buf,
180 guint64 * sample, guchar * data, gint in_samples, gint out_samples,
183 G_DEFINE_TYPE (GstPulseRingBuffer, gst_pulseringbuffer, GST_TYPE_RING_BUFFER);
186 gst_pulsesink_init_contexts (void)
188 g_assert (pa_shared_resource_mutex == NULL);
189 pa_shared_resource_mutex = g_mutex_new ();
190 gst_pulse_shared_contexts = g_hash_table_new_full (g_str_hash, g_str_equal,
195 gst_pulseringbuffer_class_init (GstPulseRingBufferClass * klass)
197 GObjectClass *gobject_class;
198 GstRingBufferClass *gstringbuffer_class;
200 gobject_class = (GObjectClass *) klass;
201 gstringbuffer_class = (GstRingBufferClass *) klass;
203 ring_parent_class = g_type_class_peek_parent (klass);
205 gobject_class->finalize = gst_pulseringbuffer_finalize;
207 gstringbuffer_class->open_device =
208 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_open_device);
209 gstringbuffer_class->close_device =
210 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_close_device);
211 gstringbuffer_class->acquire =
212 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_acquire);
213 gstringbuffer_class->release =
214 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_release);
215 gstringbuffer_class->start = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_start);
216 gstringbuffer_class->pause = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_pause);
217 gstringbuffer_class->resume = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_start);
218 gstringbuffer_class->stop = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_stop);
219 gstringbuffer_class->clear_all =
220 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_clear);
222 gstringbuffer_class->commit = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_commit);
226 gst_pulseringbuffer_init (GstPulseRingBuffer * pbuf)
228 pbuf->stream_name = NULL;
229 pbuf->context = NULL;
232 #ifdef HAVE_PULSE_1_0
236 pa_sample_spec_init (&pbuf->sample_spec);
238 pbuf->is_pcm = FALSE;
242 pbuf->m_writable = 0;
244 pbuf->m_lastoffset = 0;
247 pbuf->in_commit = FALSE;
248 pbuf->paused = FALSE;
252 gst_pulsering_destroy_stream (GstPulseRingBuffer * pbuf)
257 /* drop shm memory buffer */
258 pa_stream_cancel_write (pbuf->stream);
260 /* reset internal variables */
263 pbuf->m_writable = 0;
265 pbuf->m_lastoffset = 0;
267 #ifdef HAVE_PULSE_1_0
269 pa_format_info_free (pbuf->format);
272 pbuf->is_pcm = FALSE;
276 pa_stream_disconnect (pbuf->stream);
278 /* Make sure we don't get any further callbacks */
279 pa_stream_set_state_callback (pbuf->stream, NULL, NULL);
280 pa_stream_set_write_callback (pbuf->stream, NULL, NULL);
281 pa_stream_set_underflow_callback (pbuf->stream, NULL, NULL);
282 pa_stream_set_overflow_callback (pbuf->stream, NULL, NULL);
284 pa_stream_unref (pbuf->stream);
288 g_free (pbuf->stream_name);
289 pbuf->stream_name = NULL;
293 gst_pulsering_destroy_context (GstPulseRingBuffer * pbuf)
295 g_mutex_lock (pa_shared_resource_mutex);
297 GST_DEBUG_OBJECT (pbuf, "destroying ringbuffer %p", pbuf);
299 gst_pulsering_destroy_stream (pbuf);
302 pa_context_unref (pbuf->context);
303 pbuf->context = NULL;
306 if (pbuf->context_name) {
307 GstPulseContext *pctx;
309 pctx = g_hash_table_lookup (gst_pulse_shared_contexts, pbuf->context_name);
311 GST_DEBUG_OBJECT (pbuf, "releasing context with name %s, pbuf=%p, pctx=%p",
312 pbuf->context_name, pbuf, pctx);
315 pctx->ring_buffers = g_slist_remove (pctx->ring_buffers, pbuf);
316 if (pctx->ring_buffers == NULL) {
317 GST_DEBUG_OBJECT (pbuf,
318 "destroying final context with name %s, pbuf=%p, pctx=%p",
319 pbuf->context_name, pbuf, pctx);
321 pa_context_disconnect (pctx->context);
323 /* Make sure we don't get any further callbacks */
324 pa_context_set_state_callback (pctx->context, NULL, NULL);
325 pa_context_set_subscribe_callback (pctx->context, NULL, NULL);
327 g_hash_table_remove (gst_pulse_shared_contexts, pbuf->context_name);
329 pa_context_unref (pctx->context);
330 g_slice_free (GstPulseContext, pctx);
333 g_free (pbuf->context_name);
334 pbuf->context_name = NULL;
336 g_mutex_unlock (pa_shared_resource_mutex);
340 gst_pulseringbuffer_finalize (GObject * object)
342 GstPulseRingBuffer *ringbuffer;
344 ringbuffer = GST_PULSERING_BUFFER_CAST (object);
346 gst_pulsering_destroy_context (ringbuffer);
347 G_OBJECT_CLASS (ring_parent_class)->finalize (object);
351 #define CONTEXT_OK(c) ((c) && PA_CONTEXT_IS_GOOD (pa_context_get_state ((c))))
352 #define STREAM_OK(s) ((s) && PA_STREAM_IS_GOOD (pa_stream_get_state ((s))))
355 gst_pulsering_is_dead (GstPulseSink * psink, GstPulseRingBuffer * pbuf,
356 gboolean check_stream)
358 if (!CONTEXT_OK (pbuf->context))
361 if (check_stream && !STREAM_OK (pbuf->stream))
368 const gchar *err_str =
369 pbuf->context ? pa_strerror (pa_context_errno (pbuf->context)) : NULL;
370 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED, ("Disconnected: %s",
377 gst_pulsering_context_state_cb (pa_context * c, void *userdata)
379 pa_context_state_t state;
380 pa_threaded_mainloop *mainloop = (pa_threaded_mainloop *) userdata;
382 state = pa_context_get_state (c);
384 GST_LOG ("got new context state %d", state);
387 case PA_CONTEXT_READY:
388 case PA_CONTEXT_TERMINATED:
389 case PA_CONTEXT_FAILED:
390 GST_LOG ("signaling");
391 pa_threaded_mainloop_signal (mainloop, 0);
394 case PA_CONTEXT_UNCONNECTED:
395 case PA_CONTEXT_CONNECTING:
396 case PA_CONTEXT_AUTHORIZING:
397 case PA_CONTEXT_SETTING_NAME:
403 gst_pulsering_context_subscribe_cb (pa_context * c,
404 pa_subscription_event_type_t t, uint32_t idx, void *userdata)
407 GstPulseContext *pctx = (GstPulseContext *) userdata;
410 if (t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT | PA_SUBSCRIPTION_EVENT_CHANGE) &&
411 t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT | PA_SUBSCRIPTION_EVENT_NEW))
414 for (walk = pctx->ring_buffers; walk; walk = g_slist_next (walk)) {
415 GstPulseRingBuffer *pbuf = (GstPulseRingBuffer *) walk->data;
416 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
418 GST_LOG_OBJECT (psink, "type %d, idx %u", t, idx);
423 if (idx != pa_stream_get_index (pbuf->stream))
426 #ifdef HAVE_PULSE_1_0
427 if (psink->device && pbuf->is_pcm &&
428 !g_str_equal (psink->device,
429 pa_stream_get_device_name (pbuf->stream))) {
430 /* Underlying sink changed. And this is not a passthrough stream. Let's
431 * see if someone upstream wants to try to renegotiate. */
434 g_free (psink->device);
435 psink->device = g_strdup (pa_stream_get_device_name (pbuf->stream));
437 GST_INFO_OBJECT (psink, "emitting sink-changed");
439 renego = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
440 gst_structure_new ("pulse-sink-changed", NULL));
442 if (!gst_pad_push_event (GST_BASE_SINK (psink)->sinkpad, renego))
443 GST_DEBUG_OBJECT (psink, "Emitted sink-changed - nobody was listening");
447 /* Actually this event is also triggered when other properties of
448 * the stream change that are unrelated to the volume. However it is
449 * probably cheaper to signal the change here and check for the
450 * volume when the GObject property is read instead of querying it always. */
452 /* inform streaming thread to notify */
453 g_atomic_int_compare_and_exchange (&psink->notify, 0, 1);
457 /* will be called when the device should be opened. In this case we will connect
458 * to the server. We should not try to open any streams in this state. */
460 gst_pulseringbuffer_open_device (GstRingBuffer * buf)
463 GstPulseRingBuffer *pbuf;
464 GstPulseContext *pctx;
465 pa_mainloop_api *api;
466 gboolean need_unlock_shared;
468 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
469 pbuf = GST_PULSERING_BUFFER_CAST (buf);
471 g_assert (!pbuf->stream);
472 g_assert (psink->client_name);
475 pbuf->context_name = g_strdup_printf ("%s@%s", psink->client_name,
478 pbuf->context_name = g_strdup (psink->client_name);
480 pa_threaded_mainloop_lock (mainloop);
482 g_mutex_lock (pa_shared_resource_mutex);
483 need_unlock_shared = TRUE;
485 pctx = g_hash_table_lookup (gst_pulse_shared_contexts, pbuf->context_name);
487 pctx = g_slice_new0 (GstPulseContext);
489 /* get the mainloop api and create a context */
490 GST_INFO_OBJECT (psink, "new context with name %s, pbuf=%p, pctx=%p",
491 pbuf->context_name, pbuf, pctx);
492 api = pa_threaded_mainloop_get_api (mainloop);
493 if (!(pctx->context = pa_context_new (api, pbuf->context_name)))
496 pctx->ring_buffers = g_slist_prepend (pctx->ring_buffers, pbuf);
497 g_hash_table_insert (gst_pulse_shared_contexts,
498 g_strdup (pbuf->context_name), (gpointer) pctx);
499 /* register some essential callbacks */
500 pa_context_set_state_callback (pctx->context,
501 gst_pulsering_context_state_cb, mainloop);
502 pa_context_set_subscribe_callback (pctx->context,
503 gst_pulsering_context_subscribe_cb, pctx);
505 /* try to connect to the server and wait for completion, we don't want to
506 * autospawn a deamon */
507 GST_LOG_OBJECT (psink, "connect to server %s",
508 GST_STR_NULL (psink->server));
509 if (pa_context_connect (pctx->context, psink->server,
510 PA_CONTEXT_NOAUTOSPAWN, NULL) < 0)
513 GST_INFO_OBJECT (psink,
514 "reusing shared context with name %s, pbuf=%p, pctx=%p",
515 pbuf->context_name, pbuf, pctx);
516 pctx->ring_buffers = g_slist_prepend (pctx->ring_buffers, pbuf);
519 g_mutex_unlock (pa_shared_resource_mutex);
520 need_unlock_shared = FALSE;
522 /* context created or shared okay */
523 pbuf->context = pa_context_ref (pctx->context);
526 pa_context_state_t state;
528 state = pa_context_get_state (pbuf->context);
530 GST_LOG_OBJECT (psink, "context state is now %d", state);
532 if (!PA_CONTEXT_IS_GOOD (state))
535 if (state == PA_CONTEXT_READY)
538 /* Wait until the context is ready */
539 GST_LOG_OBJECT (psink, "waiting..");
540 pa_threaded_mainloop_wait (mainloop);
543 GST_LOG_OBJECT (psink, "opened the device");
545 pa_threaded_mainloop_unlock (mainloop);
552 if (need_unlock_shared)
553 g_mutex_unlock (pa_shared_resource_mutex);
554 gst_pulsering_destroy_context (pbuf);
555 pa_threaded_mainloop_unlock (mainloop);
560 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
561 ("Failed to create context"), (NULL));
562 g_slice_free (GstPulseContext, pctx);
563 goto unlock_and_fail;
567 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED, ("Failed to connect: %s",
568 pa_strerror (pa_context_errno (pctx->context))), (NULL));
569 goto unlock_and_fail;
573 /* close the device */
575 gst_pulseringbuffer_close_device (GstRingBuffer * buf)
578 GstPulseRingBuffer *pbuf;
580 pbuf = GST_PULSERING_BUFFER_CAST (buf);
581 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
583 GST_LOG_OBJECT (psink, "closing device");
585 pa_threaded_mainloop_lock (mainloop);
586 gst_pulsering_destroy_context (pbuf);
587 pa_threaded_mainloop_unlock (mainloop);
589 GST_LOG_OBJECT (psink, "closed device");
595 gst_pulsering_stream_state_cb (pa_stream * s, void *userdata)
598 GstPulseRingBuffer *pbuf;
599 pa_stream_state_t state;
601 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
602 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
604 state = pa_stream_get_state (s);
605 GST_LOG_OBJECT (psink, "got new stream state %d", state);
608 case PA_STREAM_READY:
609 case PA_STREAM_FAILED:
610 case PA_STREAM_TERMINATED:
611 GST_LOG_OBJECT (psink, "signaling");
612 pa_threaded_mainloop_signal (mainloop, 0);
614 case PA_STREAM_UNCONNECTED:
615 case PA_STREAM_CREATING:
621 gst_pulsering_stream_request_cb (pa_stream * s, size_t length, void *userdata)
625 GstPulseRingBuffer *pbuf;
627 rbuf = GST_RING_BUFFER_CAST (userdata);
628 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
629 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
631 GST_LOG_OBJECT (psink, "got request for length %" G_GSIZE_FORMAT, length);
633 if (pbuf->in_commit && (length >= rbuf->spec.segsize)) {
634 /* only signal when we are waiting in the commit thread
635 * and got request for atleast a segment */
636 pa_threaded_mainloop_signal (mainloop, 0);
641 gst_pulsering_stream_underflow_cb (pa_stream * s, void *userdata)
644 GstPulseRingBuffer *pbuf;
646 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
647 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
649 GST_WARNING_OBJECT (psink, "Got underflow");
653 gst_pulsering_stream_overflow_cb (pa_stream * s, void *userdata)
656 GstPulseRingBuffer *pbuf;
658 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
659 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
661 GST_WARNING_OBJECT (psink, "Got overflow");
665 gst_pulsering_stream_latency_cb (pa_stream * s, void *userdata)
668 GstPulseRingBuffer *pbuf;
669 const pa_timing_info *info;
672 info = pa_stream_get_timing_info (s);
674 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
675 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
678 GST_LOG_OBJECT (psink, "latency update (information unknown)");
681 sink_usec = info->configured_sink_usec;
683 GST_LOG_OBJECT (psink,
684 "latency_update, %" G_GUINT64_FORMAT ", %d:%" G_GINT64_FORMAT ", %d:%"
685 G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT,
686 GST_TIMEVAL_TO_TIME (info->timestamp), info->write_index_corrupt,
687 info->write_index, info->read_index_corrupt, info->read_index,
688 info->sink_usec, sink_usec);
692 gst_pulsering_stream_suspended_cb (pa_stream * p, void *userdata)
695 GstPulseRingBuffer *pbuf;
697 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
698 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
700 if (pa_stream_is_suspended (p))
701 GST_DEBUG_OBJECT (psink, "stream suspended");
703 GST_DEBUG_OBJECT (psink, "stream resumed");
707 gst_pulsering_stream_started_cb (pa_stream * p, void *userdata)
710 GstPulseRingBuffer *pbuf;
712 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
713 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
715 GST_DEBUG_OBJECT (psink, "stream started");
719 gst_pulsering_stream_event_cb (pa_stream * p, const char *name,
720 pa_proplist * pl, void *userdata)
723 GstPulseRingBuffer *pbuf;
725 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
726 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
728 if (!strcmp (name, PA_STREAM_EVENT_REQUEST_CORK)) {
729 /* the stream wants to PAUSE, post a message for the application. */
730 GST_DEBUG_OBJECT (psink, "got request for CORK");
731 gst_element_post_message (GST_ELEMENT_CAST (psink),
732 gst_message_new_request_state (GST_OBJECT_CAST (psink),
735 } else if (!strcmp (name, PA_STREAM_EVENT_REQUEST_UNCORK)) {
736 GST_DEBUG_OBJECT (psink, "got request for UNCORK");
737 gst_element_post_message (GST_ELEMENT_CAST (psink),
738 gst_message_new_request_state (GST_OBJECT_CAST (psink),
740 #ifdef HAVE_PULSE_1_0
741 } else if (!strcmp (name, PA_STREAM_EVENT_FORMAT_LOST)) {
744 if (g_atomic_int_get (&psink->format_lost)) {
745 /* Duplicate event before we're done reconfiguring, discard */
749 GST_DEBUG_OBJECT (psink, "got FORMAT LOST");
750 g_atomic_int_set (&psink->format_lost, 1);
751 psink->format_lost_time = g_ascii_strtoull (pa_proplist_gets (pl,
752 "stream-time"), NULL, 0) * 1000;
754 g_free (psink->device);
755 psink->device = g_strdup (pa_proplist_gets (pl, "device"));
757 renego = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
758 gst_structure_new ("pulse-format-lost", NULL));
760 if (!gst_pad_push_event (GST_BASE_SINK (psink)->sinkpad, renego)) {
761 /* Nobody handled the format change - emit an error */
762 GST_ELEMENT_ERROR (psink, STREAM, FORMAT, ("Sink format changed"),
763 ("Sink format changed"));
767 GST_DEBUG_OBJECT (psink, "got unknown event %s", name);
771 /* Called with the mainloop locked */
773 gst_pulsering_wait_for_stream_ready (GstPulseSink * psink, pa_stream * stream)
775 pa_stream_state_t state;
778 state = pa_stream_get_state (stream);
780 GST_LOG_OBJECT (psink, "stream state is now %d", state);
782 if (!PA_STREAM_IS_GOOD (state))
785 if (state == PA_STREAM_READY)
788 /* Wait until the stream is ready */
789 pa_threaded_mainloop_wait (mainloop);
794 /* This method should create a new stream of the given @spec. No playback should
795 * start yet so we start in the corked state. */
797 gst_pulseringbuffer_acquire (GstRingBuffer * buf, GstRingBufferSpec * spec)
800 GstPulseRingBuffer *pbuf;
801 pa_buffer_attr wanted;
802 const pa_buffer_attr *actual;
803 pa_channel_map channel_map;
804 pa_operation *o = NULL;
805 #ifdef HAVE_PULSE_0_9_20
808 pa_cvolume *pv = NULL;
809 pa_stream_flags_t flags;
811 GstAudioClock *clock;
812 #ifdef HAVE_PULSE_1_0
813 pa_format_info *formats[1];
814 #ifndef GST_DISABLE_GST_DEBUG
815 gchar print_buf[PA_FORMAT_INFO_SNPRINT_MAX];
819 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
820 pbuf = GST_PULSERING_BUFFER_CAST (buf);
822 GST_LOG_OBJECT (psink, "creating sample spec");
823 /* convert the gstreamer sample spec to the pulseaudio format */
824 #ifdef HAVE_PULSE_1_0
825 if (!gst_pulse_fill_format_info (spec, &pbuf->format, &pbuf->channels))
827 pbuf->is_pcm = pa_format_info_is_pcm (pbuf->format);
829 if (!gst_pulse_fill_sample_spec (spec, &pbuf->sample_spec))
834 pa_threaded_mainloop_lock (mainloop);
836 /* we need a context and a no stream */
837 g_assert (pbuf->context);
838 g_assert (!pbuf->stream);
840 /* enable event notifications */
841 GST_LOG_OBJECT (psink, "subscribing to context events");
842 if (!(o = pa_context_subscribe (pbuf->context,
843 PA_SUBSCRIPTION_MASK_SINK_INPUT, NULL, NULL)))
844 goto subscribe_failed;
846 pa_operation_unref (o);
848 /* initialize the channel map */
849 #ifdef HAVE_PULSE_1_0
850 if (pbuf->is_pcm && gst_pulse_gst_to_channel_map (&channel_map, spec))
851 pa_format_info_set_channel_map (pbuf->format, &channel_map);
853 gst_pulse_gst_to_channel_map (&channel_map, spec);
856 /* find a good name for the stream */
857 if (psink->stream_name)
858 name = psink->stream_name;
860 name = "Playback Stream";
862 /* create a stream */
863 #ifdef HAVE_PULSE_1_0
864 formats[0] = pbuf->format;
865 if (!(pbuf->stream = pa_stream_new_extended (pbuf->context, name, formats, 1,
869 GST_LOG_OBJECT (psink, "creating stream with name %s", name);
870 if (!(pbuf->stream = pa_stream_new_with_proplist (pbuf->context, name,
871 &pbuf->sample_spec, &channel_map, psink->proplist)))
875 /* install essential callbacks */
876 pa_stream_set_state_callback (pbuf->stream,
877 gst_pulsering_stream_state_cb, pbuf);
878 pa_stream_set_write_callback (pbuf->stream,
879 gst_pulsering_stream_request_cb, pbuf);
880 pa_stream_set_underflow_callback (pbuf->stream,
881 gst_pulsering_stream_underflow_cb, pbuf);
882 pa_stream_set_overflow_callback (pbuf->stream,
883 gst_pulsering_stream_overflow_cb, pbuf);
884 pa_stream_set_latency_update_callback (pbuf->stream,
885 gst_pulsering_stream_latency_cb, pbuf);
886 pa_stream_set_suspended_callback (pbuf->stream,
887 gst_pulsering_stream_suspended_cb, pbuf);
888 pa_stream_set_started_callback (pbuf->stream,
889 gst_pulsering_stream_started_cb, pbuf);
890 pa_stream_set_event_callback (pbuf->stream,
891 gst_pulsering_stream_event_cb, pbuf);
893 /* buffering requirements. When setting prebuf to 0, the stream will not pause
894 * when we cause an underrun, which causes time to continue. */
895 memset (&wanted, 0, sizeof (wanted));
896 wanted.tlength = spec->segtotal * spec->segsize;
897 wanted.maxlength = -1;
899 wanted.minreq = spec->segsize;
901 GST_INFO_OBJECT (psink, "tlength: %d", wanted.tlength);
902 GST_INFO_OBJECT (psink, "maxlength: %d", wanted.maxlength);
903 GST_INFO_OBJECT (psink, "prebuf: %d", wanted.prebuf);
904 GST_INFO_OBJECT (psink, "minreq: %d", wanted.minreq);
906 #ifdef HAVE_PULSE_0_9_20
907 /* configure volume when we changed it, else we leave the default */
908 if (psink->volume_set) {
909 GST_LOG_OBJECT (psink, "have volume of %f", psink->volume);
911 #ifdef HAVE_PULSE_1_0
913 gst_pulse_cvolume_from_linear (pv, pbuf->channels, psink->volume);
915 GST_DEBUG_OBJECT (psink, "passthrough stream, not setting volume");
919 gst_pulse_cvolume_from_linear (pv, pbuf->sample_spec.channels,
927 /* construct the flags */
928 flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE |
929 PA_STREAM_ADJUST_LATENCY | PA_STREAM_START_CORKED;
931 if (psink->mute_set && psink->mute)
932 flags |= PA_STREAM_START_MUTED;
934 /* we always start corked (see flags above) */
937 /* try to connect now */
938 GST_LOG_OBJECT (psink, "connect for playback to device %s",
939 GST_STR_NULL (psink->device));
940 if (pa_stream_connect_playback (pbuf->stream, psink->device,
941 &wanted, flags, pv, NULL) < 0)
944 /* our clock will now start from 0 again */
945 clock = GST_AUDIO_CLOCK (GST_BASE_AUDIO_SINK (psink)->provided_clock);
946 gst_audio_clock_reset (clock, 0);
948 if (!gst_pulsering_wait_for_stream_ready (psink, pbuf->stream))
951 #ifdef HAVE_PULSE_1_0
952 g_free (psink->device);
953 psink->device = g_strdup (pa_stream_get_device_name (pbuf->stream));
955 #ifndef GST_DISABLE_GST_DEBUG
956 pa_format_info_snprint (print_buf, sizeof (print_buf),
957 pa_stream_get_format_info (pbuf->stream));
958 GST_INFO_OBJECT (psink, "negotiated to: %s", print_buf);
962 /* After we passed the volume off of to PA we never want to set it
963 again, since it is PA's job to save/restore volumes. */
964 psink->volume_set = psink->mute_set = FALSE;
966 GST_LOG_OBJECT (psink, "stream is acquired now");
968 /* get the actual buffering properties now */
969 actual = pa_stream_get_buffer_attr (pbuf->stream);
971 GST_INFO_OBJECT (psink, "tlength: %d (wanted: %d)", actual->tlength,
973 GST_INFO_OBJECT (psink, "maxlength: %d", actual->maxlength);
974 GST_INFO_OBJECT (psink, "prebuf: %d", actual->prebuf);
975 GST_INFO_OBJECT (psink, "minreq: %d (wanted %d)", actual->minreq,
978 spec->segsize = actual->minreq;
979 spec->segtotal = actual->tlength / spec->segsize;
981 pa_threaded_mainloop_unlock (mainloop);
988 gst_pulsering_destroy_stream (pbuf);
989 pa_threaded_mainloop_unlock (mainloop);
995 GST_ELEMENT_ERROR (psink, RESOURCE, SETTINGS,
996 ("Invalid sample specification."), (NULL));
1001 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1002 ("pa_context_subscribe() failed: %s",
1003 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1004 goto unlock_and_fail;
1008 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1009 ("Failed to create stream: %s",
1010 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1011 goto unlock_and_fail;
1015 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1016 ("Failed to connect stream: %s",
1017 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1018 goto unlock_and_fail;
1022 /* free the stream that we acquired before */
1024 gst_pulseringbuffer_release (GstRingBuffer * buf)
1026 GstPulseRingBuffer *pbuf;
1028 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1030 pa_threaded_mainloop_lock (mainloop);
1031 gst_pulsering_destroy_stream (pbuf);
1032 pa_threaded_mainloop_unlock (mainloop);
1034 #ifdef HAVE_PULSE_1_0
1036 GstPulseSink *psink;
1038 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1039 g_atomic_int_set (&psink->format_lost, FALSE);
1040 psink->format_lost_time = GST_CLOCK_TIME_NONE;
1048 gst_pulsering_success_cb (pa_stream * s, int success, void *userdata)
1050 pa_threaded_mainloop_signal (mainloop, 0);
1053 /* update the corked state of a stream, must be called with the mainloop
1056 gst_pulsering_set_corked (GstPulseRingBuffer * pbuf, gboolean corked,
1059 pa_operation *o = NULL;
1060 GstPulseSink *psink;
1061 gboolean res = FALSE;
1063 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1065 #ifdef HAVE_PULSE_1_0
1066 if (g_atomic_int_get (&psink->format_lost)) {
1067 /* Sink format changed, stream's gone so fake being paused */
1072 GST_DEBUG_OBJECT (psink, "setting corked state to %d", corked);
1073 if (pbuf->corked != corked) {
1074 if (!(o = pa_stream_cork (pbuf->stream, corked,
1075 gst_pulsering_success_cb, pbuf)))
1078 while (wait && pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1079 pa_threaded_mainloop_wait (mainloop);
1080 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
1083 pbuf->corked = corked;
1085 GST_DEBUG_OBJECT (psink, "skipping, already in requested state");
1091 pa_operation_unref (o);
1098 GST_DEBUG_OBJECT (psink, "the server is dead");
1103 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1104 ("pa_stream_cork() failed: %s",
1105 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1111 gst_pulseringbuffer_clear (GstRingBuffer * buf)
1113 GstPulseSink *psink;
1114 GstPulseRingBuffer *pbuf;
1115 pa_operation *o = NULL;
1117 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1118 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1120 pa_threaded_mainloop_lock (mainloop);
1121 GST_DEBUG_OBJECT (psink, "clearing");
1123 /* don't wait for the flush to complete */
1124 if ((o = pa_stream_flush (pbuf->stream, NULL, pbuf)))
1125 pa_operation_unref (o);
1127 pa_threaded_mainloop_unlock (mainloop);
1130 /* called from pulse with the mainloop lock */
1132 mainloop_enter_defer_cb (pa_mainloop_api * api, void *userdata)
1134 GstPulseSink *pulsesink = GST_PULSESINK (userdata);
1135 GstMessage *message;
1138 g_value_init (&val, G_TYPE_POINTER);
1139 g_value_set_pointer (&val, g_thread_self ());
1141 GST_DEBUG_OBJECT (pulsesink, "posting ENTER stream status");
1142 message = gst_message_new_stream_status (GST_OBJECT (pulsesink),
1143 GST_STREAM_STATUS_TYPE_ENTER, GST_ELEMENT (pulsesink));
1144 gst_message_set_stream_status_object (message, &val);
1146 gst_element_post_message (GST_ELEMENT (pulsesink), message);
1148 g_return_if_fail (pulsesink->defer_pending);
1149 pulsesink->defer_pending--;
1150 pa_threaded_mainloop_signal (mainloop, 0);
1153 /* start/resume playback ASAP, we don't uncork here but in the commit method */
1155 gst_pulseringbuffer_start (GstRingBuffer * buf)
1157 GstPulseSink *psink;
1158 GstPulseRingBuffer *pbuf;
1160 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1161 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1163 pa_threaded_mainloop_lock (mainloop);
1165 GST_DEBUG_OBJECT (psink, "scheduling stream status");
1166 psink->defer_pending++;
1167 pa_mainloop_api_once (pa_threaded_mainloop_get_api (mainloop),
1168 mainloop_enter_defer_cb, psink);
1170 GST_DEBUG_OBJECT (psink, "starting");
1171 pbuf->paused = FALSE;
1173 /* EOS needs running clock */
1174 if (GST_BASE_SINK_CAST (psink)->eos ||
1175 g_atomic_int_get (&GST_BASE_AUDIO_SINK (psink)->abidata.
1177 gst_pulsering_set_corked (pbuf, FALSE, FALSE);
1179 pa_threaded_mainloop_unlock (mainloop);
1184 /* pause/stop playback ASAP */
1186 gst_pulseringbuffer_pause (GstRingBuffer * buf)
1188 GstPulseSink *psink;
1189 GstPulseRingBuffer *pbuf;
1192 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1193 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1195 pa_threaded_mainloop_lock (mainloop);
1196 GST_DEBUG_OBJECT (psink, "pausing and corking");
1197 /* make sure the commit method stops writing */
1198 pbuf->paused = TRUE;
1199 res = gst_pulsering_set_corked (pbuf, TRUE, TRUE);
1200 if (pbuf->in_commit) {
1201 /* we are waiting in a commit, signal */
1202 GST_DEBUG_OBJECT (psink, "signal commit");
1203 pa_threaded_mainloop_signal (mainloop, 0);
1205 pa_threaded_mainloop_unlock (mainloop);
1210 /* called from pulse with the mainloop lock */
1212 mainloop_leave_defer_cb (pa_mainloop_api * api, void *userdata)
1214 GstPulseSink *pulsesink = GST_PULSESINK (userdata);
1215 GstMessage *message;
1218 g_value_init (&val, G_TYPE_POINTER);
1219 g_value_set_pointer (&val, g_thread_self ());
1221 GST_DEBUG_OBJECT (pulsesink, "posting LEAVE stream status");
1222 message = gst_message_new_stream_status (GST_OBJECT (pulsesink),
1223 GST_STREAM_STATUS_TYPE_LEAVE, GST_ELEMENT (pulsesink));
1224 gst_message_set_stream_status_object (message, &val);
1225 gst_element_post_message (GST_ELEMENT (pulsesink), message);
1227 g_return_if_fail (pulsesink->defer_pending);
1228 pulsesink->defer_pending--;
1229 pa_threaded_mainloop_signal (mainloop, 0);
1232 /* stop playback, we flush everything. */
1234 gst_pulseringbuffer_stop (GstRingBuffer * buf)
1236 GstPulseSink *psink;
1237 GstPulseRingBuffer *pbuf;
1238 gboolean res = FALSE;
1239 pa_operation *o = NULL;
1241 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1242 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1244 pa_threaded_mainloop_lock (mainloop);
1246 pbuf->paused = TRUE;
1247 res = gst_pulsering_set_corked (pbuf, TRUE, TRUE);
1249 /* Inform anyone waiting in _commit() call that it shall wakeup */
1250 if (pbuf->in_commit) {
1251 GST_DEBUG_OBJECT (psink, "signal commit thread");
1252 pa_threaded_mainloop_signal (mainloop, 0);
1254 #ifdef HAVE_PULSE_1_0
1255 if (g_atomic_int_get (&psink->format_lost)) {
1256 /* Don't try to flush, the stream's probably gone by now */
1262 /* then try to flush, it's not fatal when this fails */
1263 GST_DEBUG_OBJECT (psink, "flushing");
1264 if ((o = pa_stream_flush (pbuf->stream, gst_pulsering_success_cb, pbuf))) {
1265 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1266 GST_DEBUG_OBJECT (psink, "wait for completion");
1267 pa_threaded_mainloop_wait (mainloop);
1268 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
1271 GST_DEBUG_OBJECT (psink, "flush completed");
1277 pa_operation_cancel (o);
1278 pa_operation_unref (o);
1281 GST_DEBUG_OBJECT (psink, "scheduling stream status");
1282 psink->defer_pending++;
1283 pa_mainloop_api_once (pa_threaded_mainloop_get_api (mainloop),
1284 mainloop_leave_defer_cb, psink);
1286 pa_threaded_mainloop_unlock (mainloop);
1293 GST_DEBUG_OBJECT (psink, "the server is dead");
1298 /* in_samples >= out_samples, rate > 1.0 */
1299 #define FWD_UP_SAMPLES(s,se,d,de) \
1301 guint8 *sb = s, *db = d; \
1302 while (s <= se && d < de) { \
1303 memcpy (d, s, bps); \
1306 if ((*accum << 1) >= inr) { \
1311 in_samples -= (s - sb)/bps; \
1312 out_samples -= (d - db)/bps; \
1313 GST_DEBUG ("fwd_up end %d/%d",*accum,*toprocess); \
1316 /* out_samples > in_samples, for rates smaller than 1.0 */
1317 #define FWD_DOWN_SAMPLES(s,se,d,de) \
1319 guint8 *sb = s, *db = d; \
1320 while (s <= se && d < de) { \
1321 memcpy (d, s, bps); \
1324 if ((*accum << 1) >= outr) { \
1329 in_samples -= (s - sb)/bps; \
1330 out_samples -= (d - db)/bps; \
1331 GST_DEBUG ("fwd_down end %d/%d",*accum,*toprocess); \
1334 #define REV_UP_SAMPLES(s,se,d,de) \
1336 guint8 *sb = se, *db = d; \
1337 while (s <= se && d < de) { \
1338 memcpy (d, se, bps); \
1341 while (d < de && (*accum << 1) >= inr) { \
1346 in_samples -= (sb - se)/bps; \
1347 out_samples -= (d - db)/bps; \
1348 GST_DEBUG ("rev_up end %d/%d",*accum,*toprocess); \
1351 #define REV_DOWN_SAMPLES(s,se,d,de) \
1353 guint8 *sb = se, *db = d; \
1354 while (s <= se && d < de) { \
1355 memcpy (d, se, bps); \
1358 while (s <= se && (*accum << 1) >= outr) { \
1363 in_samples -= (sb - se)/bps; \
1364 out_samples -= (d - db)/bps; \
1365 GST_DEBUG ("rev_down end %d/%d",*accum,*toprocess); \
1368 /* our custom commit function because we write into the buffer of pulseaudio
1369 * instead of keeping our own buffer */
1371 gst_pulseringbuffer_commit (GstRingBuffer * buf, guint64 * sample,
1372 guchar * data, gint in_samples, gint out_samples, gint * accum)
1374 GstPulseSink *psink;
1375 GstPulseRingBuffer *pbuf;
1380 gint inr, outr, bps;
1384 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1385 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1387 /* FIXME post message rather than using a signal (as mixer interface) */
1388 if (g_atomic_int_compare_and_exchange (&psink->notify, 1, 0)) {
1389 g_object_notify (G_OBJECT (psink), "volume");
1390 g_object_notify (G_OBJECT (psink), "mute");
1393 /* make sure the ringbuffer is started */
1394 if (G_UNLIKELY (g_atomic_int_get (&buf->state) !=
1395 GST_RING_BUFFER_STATE_STARTED)) {
1396 /* see if we are allowed to start it */
1397 if (G_UNLIKELY (g_atomic_int_get (&buf->abidata.ABI.may_start) == FALSE))
1400 GST_DEBUG_OBJECT (buf, "start!");
1401 if (!gst_ring_buffer_start (buf))
1405 pa_threaded_mainloop_lock (mainloop);
1407 GST_DEBUG_OBJECT (psink, "entering commit");
1408 pbuf->in_commit = TRUE;
1410 bps = buf->spec.bytes_per_sample;
1411 bufsize = buf->spec.segsize * buf->spec.segtotal;
1413 /* our toy resampler for trick modes */
1414 reverse = out_samples < 0;
1415 out_samples = ABS (out_samples);
1417 if (in_samples >= out_samples)
1418 toprocess = &in_samples;
1420 toprocess = &out_samples;
1422 inr = in_samples - 1;
1423 outr = out_samples - 1;
1425 GST_DEBUG_OBJECT (psink, "in %d, out %d", inr, outr);
1427 /* data_end points to the last sample we have to write, not past it. This is
1428 * needed to properly handle reverse playback: it points to the last sample. */
1429 data_end = data + (bps * inr);
1431 #ifdef HAVE_PULSE_1_0
1432 if (g_atomic_int_get (&psink->format_lost)) {
1433 /* Sink format changed, drop the data and hope upstream renegotiates */
1441 /* offset is in bytes */
1442 offset = *sample * bps;
1444 while (*toprocess > 0) {
1448 GST_LOG_OBJECT (psink,
1449 "need to write %d samples at offset %" G_GINT64_FORMAT, *toprocess,
1452 if (offset != pbuf->m_lastoffset)
1453 GST_LOG_OBJECT (psink, "discontinuity, offset is %" G_GINT64_FORMAT ", "
1454 "last offset was %" G_GINT64_FORMAT, offset, pbuf->m_lastoffset);
1456 towrite = out_samples * bps;
1458 /* Wait for at least segsize bytes to become available */
1459 if (towrite > buf->spec.segsize)
1460 towrite = buf->spec.segsize;
1462 if ((pbuf->m_writable < towrite) || (offset != pbuf->m_lastoffset)) {
1463 /* if no room left or discontinuity in offset,
1464 we need to flush data and get a new buffer */
1466 /* flush the buffer if possible */
1467 if ((pbuf->m_data != NULL) && (pbuf->m_towrite > 0)) {
1469 GST_LOG_OBJECT (psink,
1470 "flushing %u samples at offset %" G_GINT64_FORMAT,
1471 (guint) pbuf->m_towrite / bps, pbuf->m_offset);
1473 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1474 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1478 pbuf->m_towrite = 0;
1479 pbuf->m_offset = offset; /* keep track of current offset */
1481 /* get a buffer to write in for now on */
1483 pbuf->m_writable = pa_stream_writable_size (pbuf->stream);
1485 #ifdef HAVE_PULSE_1_0
1486 if (g_atomic_int_get (&psink->format_lost)) {
1487 /* Sink format changed, give up and hope upstream renegotiates */
1492 if (pbuf->m_writable == (size_t) - 1)
1493 goto writable_size_failed;
1495 pbuf->m_writable /= bps;
1496 pbuf->m_writable *= bps; /* handle only complete samples */
1498 if (pbuf->m_writable >= towrite)
1501 /* see if we need to uncork because we have no free space */
1503 if (!gst_pulsering_set_corked (pbuf, FALSE, FALSE))
1507 /* we can't write segsize bytes, wait a bit */
1508 GST_LOG_OBJECT (psink, "waiting for free space");
1509 pa_threaded_mainloop_wait (mainloop);
1515 /* Recalculate what we can write in the next chunk */
1516 towrite = out_samples * bps;
1517 if (pbuf->m_writable > towrite)
1518 pbuf->m_writable = towrite;
1520 GST_LOG_OBJECT (psink, "requesting %" G_GSIZE_FORMAT " bytes of "
1521 "shared memory", pbuf->m_writable);
1523 if (pa_stream_begin_write (pbuf->stream, &pbuf->m_data,
1524 &pbuf->m_writable) < 0) {
1525 GST_LOG_OBJECT (psink, "pa_stream_begin_write() failed");
1526 goto writable_size_failed;
1529 GST_LOG_OBJECT (psink, "got %" G_GSIZE_FORMAT " bytes of shared memory",
1534 if (towrite > pbuf->m_writable)
1535 towrite = pbuf->m_writable;
1536 avail = towrite / bps;
1538 GST_LOG_OBJECT (psink, "writing %u samples at offset %" G_GUINT64_FORMAT,
1539 (guint) avail, offset);
1541 #ifdef HAVE_PULSE_1_0
1542 /* No trick modes for passthrough streams */
1543 if (G_UNLIKELY (!pbuf->is_pcm && (inr != outr || reverse))) {
1544 GST_WARNING_OBJECT (psink, "Passthrough stream can't run in trick mode");
1545 goto unlock_and_fail;
1549 if (G_LIKELY (inr == outr && !reverse)) {
1550 /* no rate conversion, simply write out the samples */
1551 /* copy the data into internal buffer */
1553 memcpy ((guint8 *) pbuf->m_data + pbuf->m_towrite, data, towrite);
1554 pbuf->m_towrite += towrite;
1555 pbuf->m_writable -= towrite;
1558 in_samples -= avail;
1559 out_samples -= avail;
1561 guint8 *dest, *d, *d_end;
1563 /* write into the PulseAudio shm buffer */
1564 dest = d = (guint8 *) pbuf->m_data + pbuf->m_towrite;
1565 d_end = d + towrite;
1569 /* forward speed up */
1570 FWD_UP_SAMPLES (data, data_end, d, d_end);
1572 /* forward slow down */
1573 FWD_DOWN_SAMPLES (data, data_end, d, d_end);
1576 /* reverse speed up */
1577 REV_UP_SAMPLES (data, data_end, d, d_end);
1579 /* reverse slow down */
1580 REV_DOWN_SAMPLES (data, data_end, d, d_end);
1582 /* see what we have left to write */
1583 towrite = (d - dest);
1584 pbuf->m_towrite += towrite;
1585 pbuf->m_writable -= towrite;
1587 avail = towrite / bps;
1590 /* flush the buffer if it's full */
1591 if ((pbuf->m_data != NULL) && (pbuf->m_towrite > 0)
1592 && (pbuf->m_writable == 0)) {
1593 GST_LOG_OBJECT (psink, "flushing %u samples at offset %" G_GINT64_FORMAT,
1594 (guint) pbuf->m_towrite / bps, pbuf->m_offset);
1596 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1597 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1600 pbuf->m_towrite = 0;
1601 pbuf->m_offset = offset + towrite; /* keep track of current offset */
1605 offset += avail * bps;
1606 pbuf->m_lastoffset = offset;
1608 /* check if we need to uncork after writing the samples */
1610 const pa_timing_info *info;
1612 if ((info = pa_stream_get_timing_info (pbuf->stream))) {
1613 GST_LOG_OBJECT (psink,
1614 "read_index at %" G_GUINT64_FORMAT ", offset %" G_GINT64_FORMAT,
1615 info->read_index, offset);
1617 /* we uncork when the read_index is too far behind the offset we need
1619 if (info->read_index + bufsize <= offset) {
1620 if (!gst_pulsering_set_corked (pbuf, FALSE, FALSE))
1624 GST_LOG_OBJECT (psink, "no timing info available yet");
1629 #ifdef HAVE_PULSE_1_0
1632 /* we consumed all samples here */
1633 data = data_end + bps;
1635 pbuf->in_commit = FALSE;
1636 pa_threaded_mainloop_unlock (mainloop);
1639 result = inr - ((data_end - data) / bps);
1640 GST_LOG_OBJECT (psink, "wrote %d samples", result);
1647 pbuf->in_commit = FALSE;
1648 GST_LOG_OBJECT (psink, "we are reset");
1649 pa_threaded_mainloop_unlock (mainloop);
1654 GST_LOG_OBJECT (psink, "we can not start");
1659 GST_LOG_OBJECT (psink, "failed to start the ringbuffer");
1664 pbuf->in_commit = FALSE;
1665 GST_ERROR_OBJECT (psink, "uncork failed");
1666 pa_threaded_mainloop_unlock (mainloop);
1671 pbuf->in_commit = FALSE;
1672 GST_LOG_OBJECT (psink, "we are paused");
1673 pa_threaded_mainloop_unlock (mainloop);
1676 writable_size_failed:
1678 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1679 ("pa_stream_writable_size() failed: %s",
1680 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1681 goto unlock_and_fail;
1685 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1686 ("pa_stream_write() failed: %s",
1687 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1688 goto unlock_and_fail;
1692 /* write pending local samples, must be called with the mainloop lock */
1694 gst_pulsering_flush (GstPulseRingBuffer * pbuf)
1696 GstPulseSink *psink;
1698 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1699 GST_DEBUG_OBJECT (psink, "entering flush");
1701 /* flush the buffer if possible */
1702 if (pbuf->stream && (pbuf->m_data != NULL) && (pbuf->m_towrite > 0)) {
1703 #ifndef GST_DISABLE_GST_DEBUG
1706 bps = (GST_RING_BUFFER_CAST (pbuf))->spec.bytes_per_sample;
1707 GST_LOG_OBJECT (psink,
1708 "flushing %u samples at offset %" G_GINT64_FORMAT,
1709 (guint) pbuf->m_towrite / bps, pbuf->m_offset);
1712 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1713 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1717 pbuf->m_towrite = 0;
1718 pbuf->m_offset += pbuf->m_towrite; /* keep track of current offset */
1727 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1728 ("pa_stream_write() failed: %s",
1729 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1734 static void gst_pulsesink_set_property (GObject * object, guint prop_id,
1735 const GValue * value, GParamSpec * pspec);
1736 static void gst_pulsesink_get_property (GObject * object, guint prop_id,
1737 GValue * value, GParamSpec * pspec);
1738 static void gst_pulsesink_finalize (GObject * object);
1740 static gboolean gst_pulsesink_event (GstBaseSink * sink, GstEvent * event);
1742 static GstStateChangeReturn gst_pulsesink_change_state (GstElement * element,
1743 GstStateChange transition);
1745 static void gst_pulsesink_init_interfaces (GType type);
1747 GST_IMPLEMENT_PULSEPROBE_METHODS (GstPulseSink, gst_pulsesink);
1749 #define _do_init(type) \
1750 gst_pulsesink_init_contexts (); \
1751 gst_pulsesink_init_interfaces (type);
1753 GST_BOILERPLATE_FULL (GstPulseSink, gst_pulsesink, GstBaseAudioSink,
1754 GST_TYPE_BASE_AUDIO_SINK, _do_init);
1757 gst_pulsesink_interface_supported (GstImplementsInterface *
1758 iface, GType interface_type)
1760 GstPulseSink *this = GST_PULSESINK_CAST (iface);
1762 if (interface_type == GST_TYPE_PROPERTY_PROBE && this->probe)
1764 if (interface_type == GST_TYPE_STREAM_VOLUME)
1771 gst_pulsesink_implements_interface_init (GstImplementsInterfaceClass * klass)
1773 klass->supported = gst_pulsesink_interface_supported;
1777 gst_pulsesink_init_interfaces (GType type)
1779 static const GInterfaceInfo implements_iface_info = {
1780 (GInterfaceInitFunc) gst_pulsesink_implements_interface_init,
1784 static const GInterfaceInfo probe_iface_info = {
1785 (GInterfaceInitFunc) gst_pulsesink_property_probe_interface_init,
1789 static const GInterfaceInfo svol_iface_info = {
1793 g_type_add_interface_static (type, GST_TYPE_STREAM_VOLUME, &svol_iface_info);
1794 g_type_add_interface_static (type, GST_TYPE_IMPLEMENTS_INTERFACE,
1795 &implements_iface_info);
1796 g_type_add_interface_static (type, GST_TYPE_PROPERTY_PROBE,
1801 gst_pulsesink_base_init (gpointer g_class)
1803 static GstStaticPadTemplate pad_template = GST_STATIC_PAD_TEMPLATE ("sink",
1806 GST_STATIC_CAPS (PULSE_SINK_TEMPLATE_CAPS));
1808 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
1810 gst_element_class_set_details_simple (element_class,
1811 "PulseAudio Audio Sink",
1812 "Sink/Audio", "Plays audio to a PulseAudio server", "Lennart Poettering");
1813 gst_element_class_add_pad_template (element_class,
1814 gst_static_pad_template_get (&pad_template));
1817 static GstRingBuffer *
1818 gst_pulsesink_create_ringbuffer (GstBaseAudioSink * sink)
1820 GstRingBuffer *buffer;
1822 GST_DEBUG_OBJECT (sink, "creating ringbuffer");
1823 buffer = g_object_new (GST_TYPE_PULSERING_BUFFER, NULL);
1824 GST_DEBUG_OBJECT (sink, "created ringbuffer @%p", buffer);
1830 gst_pulsesink_payload (GstBaseAudioSink * sink, GstBuffer * buf)
1832 switch (sink->ringbuffer->spec.type) {
1833 case GST_BUFTYPE_AC3:
1834 case GST_BUFTYPE_EAC3:
1835 case GST_BUFTYPE_DTS:
1836 case GST_BUFTYPE_MPEG:
1838 /* FIXME: alloc memory from PA if possible */
1839 gint framesize = gst_audio_iec61937_frame_size (&sink->ringbuffer->spec);
1845 out = gst_buffer_new_and_alloc (framesize);
1847 if (!gst_audio_iec61937_payload (GST_BUFFER_DATA (buf),
1848 GST_BUFFER_SIZE (buf), GST_BUFFER_DATA (out),
1849 GST_BUFFER_SIZE (out), &sink->ringbuffer->spec)) {
1850 gst_buffer_unref (out);
1854 gst_buffer_copy_metadata (out, buf, GST_BUFFER_COPY_ALL);
1859 return gst_buffer_ref (buf);
1864 gst_pulsesink_class_init (GstPulseSinkClass * klass)
1866 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1867 GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
1868 GstBaseSinkClass *bc;
1869 GstBaseAudioSinkClass *gstaudiosink_class = GST_BASE_AUDIO_SINK_CLASS (klass);
1870 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1872 gobject_class->finalize = gst_pulsesink_finalize;
1873 gobject_class->set_property = gst_pulsesink_set_property;
1874 gobject_class->get_property = gst_pulsesink_get_property;
1876 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_pulsesink_event);
1878 /* restore the original basesink pull methods */
1879 bc = g_type_class_peek (GST_TYPE_BASE_SINK);
1880 gstbasesink_class->activate_pull = GST_DEBUG_FUNCPTR (bc->activate_pull);
1882 gstelement_class->change_state =
1883 GST_DEBUG_FUNCPTR (gst_pulsesink_change_state);
1885 gstaudiosink_class->create_ringbuffer =
1886 GST_DEBUG_FUNCPTR (gst_pulsesink_create_ringbuffer);
1887 gstaudiosink_class->payload = GST_DEBUG_FUNCPTR (gst_pulsesink_payload);
1889 /* Overwrite GObject fields */
1890 g_object_class_install_property (gobject_class,
1892 g_param_spec_string ("server", "Server",
1893 "The PulseAudio server to connect to", DEFAULT_SERVER,
1894 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1896 g_object_class_install_property (gobject_class, PROP_DEVICE,
1897 g_param_spec_string ("device", "Device",
1898 "The PulseAudio sink device to connect to", DEFAULT_DEVICE,
1899 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1901 g_object_class_install_property (gobject_class,
1903 g_param_spec_string ("device-name", "Device name",
1904 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
1905 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1907 g_object_class_install_property (gobject_class,
1909 g_param_spec_double ("volume", "Volume",
1910 "Linear volume of this stream, 1.0=100%", 0.0, MAX_VOLUME,
1911 DEFAULT_VOLUME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1912 g_object_class_install_property (gobject_class,
1914 g_param_spec_boolean ("mute", "Mute",
1915 "Mute state of this stream", DEFAULT_MUTE,
1916 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1919 * GstPulseSink:client
1921 * The PulseAudio client name to use.
1925 g_object_class_install_property (gobject_class,
1927 g_param_spec_string ("client", "Client",
1928 "The PulseAudio client name to use", gst_pulse_client_name (),
1929 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1930 GST_PARAM_MUTABLE_READY));
1933 * GstPulseSink:stream-properties
1935 * List of pulseaudio stream properties. A list of defined properties can be
1936 * found in the <ulink url="http://0pointer.de/lennart/projects/pulseaudio/doxygen/proplist_8h.html">pulseaudio api docs</ulink>.
1938 * Below is an example for registering as a music application to pulseaudio.
1940 * GstStructure *props;
1942 * props = gst_structure_from_string ("props,media.role=music", NULL);
1943 * g_object_set (pulse, "stream-properties", props, NULL);
1944 * gst_structure_free
1949 g_object_class_install_property (gobject_class,
1950 PROP_STREAM_PROPERTIES,
1951 g_param_spec_boxed ("stream-properties", "stream properties",
1952 "list of pulseaudio stream properties",
1953 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1956 /* returns the current time of the sink ringbuffer */
1958 gst_pulsesink_get_time (GstClock * clock, GstBaseAudioSink * sink)
1960 GstPulseSink *psink;
1961 GstPulseRingBuffer *pbuf;
1964 if (!sink->ringbuffer || !sink->ringbuffer->acquired)
1965 return GST_CLOCK_TIME_NONE;
1967 pbuf = GST_PULSERING_BUFFER_CAST (sink->ringbuffer);
1968 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1970 #ifdef HAVE_PULSE_1_0
1971 if (g_atomic_int_get (&psink->format_lost)) {
1972 /* Stream was lost in a format change, it'll get set up again once
1973 * upstream renegotiates */
1974 return psink->format_lost_time;
1978 pa_threaded_mainloop_lock (mainloop);
1979 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
1982 /* if we don't have enough data to get a timestamp, just return NONE, which
1983 * will return the last reported time */
1984 if (pa_stream_get_time (pbuf->stream, &time) < 0) {
1985 GST_DEBUG_OBJECT (psink, "could not get time");
1986 time = GST_CLOCK_TIME_NONE;
1989 pa_threaded_mainloop_unlock (mainloop);
1991 GST_LOG_OBJECT (psink, "current time is %" GST_TIME_FORMAT,
1992 GST_TIME_ARGS (time));
1999 GST_DEBUG_OBJECT (psink, "the server is dead");
2000 pa_threaded_mainloop_unlock (mainloop);
2002 return GST_CLOCK_TIME_NONE;
2007 gst_pulsesink_sink_info_cb (pa_context * c, const pa_sink_info * i, int eol,
2010 GstPulseRingBuffer *pbuf;
2011 GstPulseSink *psink;
2012 #ifdef HAVE_PULSE_1_0
2017 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
2018 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
2023 g_free (psink->device_description);
2024 psink->device_description = g_strdup (i->description);
2026 #ifdef HAVE_PULSE_1_0
2027 g_mutex_lock (psink->sink_formats_lock);
2029 for (l = g_list_first (psink->sink_formats); l; l = g_list_next (l))
2030 pa_format_info_free ((pa_format_info *) l->data);
2032 g_list_free (psink->sink_formats);
2033 psink->sink_formats = NULL;
2035 for (j = 0; j < i->n_formats; j++)
2036 psink->sink_formats = g_list_prepend (psink->sink_formats,
2037 pa_format_info_copy (i->formats[j]));
2039 g_mutex_unlock (psink->sink_formats_lock);
2043 pa_threaded_mainloop_signal (mainloop, 0);
2046 #ifdef HAVE_PULSE_1_0
2047 /* NOTE: If you're making changes here, see if pulseaudiosink acceptcaps also
2048 * needs to be changed accordingly. */
2050 gst_pulsesink_pad_acceptcaps (GstPad * pad, GstCaps * caps)
2052 GstPulseSink *psink = GST_PULSESINK (gst_pad_get_parent_element (pad));
2053 GstPulseRingBuffer *pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK
2054 (psink)->ringbuffer);
2057 gboolean ret = FALSE;
2059 GstRingBufferSpec spec = { 0 };
2060 pa_stream *stream = NULL;
2061 pa_operation *o = NULL;
2062 pa_channel_map channel_map;
2063 pa_stream_flags_t flags;
2064 pa_format_info *format = NULL, *formats[1];
2067 pad_caps = gst_pad_get_caps_reffed (pad);
2069 ret = gst_caps_can_intersect (pad_caps, caps);
2070 gst_caps_unref (pad_caps);
2073 /* Either template caps didn't match, or we're still in NULL state */
2074 if (!ret || !pbuf->context)
2077 /* If we've not got fixed caps, creating a stream might fail, so let's just
2078 * return from here with default acceptcaps behaviour */
2079 if (!gst_caps_is_fixed (caps))
2084 pa_threaded_mainloop_lock (mainloop);
2086 spec.latency_time = GST_BASE_AUDIO_SINK (psink)->latency_time;
2087 if (!gst_ring_buffer_parse_caps (&spec, caps))
2090 if (!gst_pulse_fill_format_info (&spec, &format, &channels))
2093 /* Make sure input is framed (one frame per buffer) and can be payloaded */
2094 if (!pa_format_info_is_pcm (format)) {
2095 gboolean framed = FALSE, parsed = FALSE;
2096 st = gst_caps_get_structure (caps, 0);
2098 gst_structure_get_boolean (st, "framed", &framed);
2099 gst_structure_get_boolean (st, "parsed", &parsed);
2100 if ((!framed && !parsed) || gst_audio_iec61937_frame_size (&spec) <= 0)
2104 /* initialize the channel map */
2105 if (pa_format_info_is_pcm (format) &&
2106 gst_pulse_gst_to_channel_map (&channel_map, &spec))
2107 pa_format_info_set_channel_map (format, &channel_map);
2110 /* We're already in PAUSED or above, so just reuse this stream to query
2111 * sink formats and use those. */
2114 if (!(o = pa_context_get_sink_info_by_name (pbuf->context, psink->device,
2115 gst_pulsesink_sink_info_cb, pbuf)))
2118 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2119 pa_threaded_mainloop_wait (mainloop);
2120 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
2124 g_mutex_lock (psink->sink_formats_lock);
2125 for (i = g_list_first (psink->sink_formats); i; i = g_list_next (i)) {
2126 if (pa_format_info_is_compatible ((pa_format_info *) i->data, format)) {
2131 g_mutex_unlock (psink->sink_formats_lock);
2133 /* We're in READY, let's connect a stream to see if the format is
2134 * accpeted by whatever sink we're routed to */
2135 formats[0] = format;
2137 if (!(stream = pa_stream_new_extended (pbuf->context, "pulsesink probe",
2138 formats, 1, psink->proplist)))
2141 /* construct the flags */
2142 flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE |
2143 PA_STREAM_ADJUST_LATENCY | PA_STREAM_START_CORKED;
2145 pa_stream_set_state_callback (stream, gst_pulsering_stream_state_cb, pbuf);
2147 if (pa_stream_connect_playback (stream, psink->device, NULL, flags, NULL,
2151 ret = gst_pulsering_wait_for_stream_ready (psink, stream);
2156 pa_format_info_free (format);
2159 pa_operation_unref (o);
2162 pa_stream_set_state_callback (stream, NULL, NULL);
2163 pa_stream_disconnect (stream);
2164 pa_stream_unref (stream);
2167 pa_threaded_mainloop_unlock (mainloop);
2170 gst_object_unref (psink);
2175 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2176 ("pa_context_get_sink_input_info() failed: %s",
2177 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2184 gst_pulsesink_init (GstPulseSink * pulsesink, GstPulseSinkClass * klass)
2186 pulsesink->server = NULL;
2187 pulsesink->device = NULL;
2188 pulsesink->device_description = NULL;
2189 pulsesink->client_name = gst_pulse_client_name ();
2191 #ifdef HAVE_PULSE_1_0
2192 pulsesink->sink_formats_lock = g_mutex_new ();
2193 pulsesink->sink_formats = NULL;
2196 pulsesink->volume = DEFAULT_VOLUME;
2197 pulsesink->volume_set = FALSE;
2199 pulsesink->mute = DEFAULT_MUTE;
2200 pulsesink->mute_set = FALSE;
2202 pulsesink->notify = 0;
2204 #ifdef HAVE_PULSE_1_0
2205 g_atomic_int_set (&pulsesink->format_lost, FALSE);
2206 pulsesink->format_lost_time = GST_CLOCK_TIME_NONE;
2209 pulsesink->properties = NULL;
2210 pulsesink->proplist = NULL;
2212 /* override with a custom clock */
2213 if (GST_BASE_AUDIO_SINK (pulsesink)->provided_clock)
2214 gst_object_unref (GST_BASE_AUDIO_SINK (pulsesink)->provided_clock);
2216 GST_BASE_AUDIO_SINK (pulsesink)->provided_clock =
2217 gst_audio_clock_new ("GstPulseSinkClock",
2218 (GstAudioClockGetTimeFunc) gst_pulsesink_get_time, pulsesink);
2220 #ifdef HAVE_PULSE_1_0
2221 gst_pad_set_acceptcaps_function (GST_BASE_SINK (pulsesink)->sinkpad,
2222 GST_DEBUG_FUNCPTR (gst_pulsesink_pad_acceptcaps));
2225 /* TRUE for sinks, FALSE for sources */
2226 pulsesink->probe = gst_pulseprobe_new (G_OBJECT (pulsesink),
2227 G_OBJECT_GET_CLASS (pulsesink), PROP_DEVICE, pulsesink->device,
2232 gst_pulsesink_finalize (GObject * object)
2234 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2235 #ifdef HAVE_PULSE_1_0
2239 g_free (pulsesink->server);
2240 g_free (pulsesink->device);
2241 g_free (pulsesink->device_description);
2242 g_free (pulsesink->client_name);
2244 #ifdef HAVE_PULSE_1_0
2245 for (i = g_list_first (pulsesink->sink_formats); i; i = g_list_next (i))
2246 pa_format_info_free ((pa_format_info *) i->data);
2248 g_list_free (pulsesink->sink_formats);
2249 g_mutex_free (pulsesink->sink_formats_lock);
2252 if (pulsesink->properties)
2253 gst_structure_free (pulsesink->properties);
2254 if (pulsesink->proplist)
2255 pa_proplist_free (pulsesink->proplist);
2257 if (pulsesink->probe) {
2258 gst_pulseprobe_free (pulsesink->probe);
2259 pulsesink->probe = NULL;
2262 G_OBJECT_CLASS (parent_class)->finalize (object);
2266 gst_pulsesink_set_volume (GstPulseSink * psink, gdouble volume)
2269 pa_operation *o = NULL;
2270 GstPulseRingBuffer *pbuf;
2276 pa_threaded_mainloop_lock (mainloop);
2278 GST_DEBUG_OBJECT (psink, "setting volume to %f", volume);
2280 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2281 if (pbuf == NULL || pbuf->stream == NULL)
2284 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2287 #ifdef HAVE_PULSE_1_0
2289 gst_pulse_cvolume_from_linear (&v, pbuf->channels, volume);
2291 /* FIXME: this will eventually be superceded by checks to see if the volume
2292 * is readable/writable */
2295 gst_pulse_cvolume_from_linear (&v, pbuf->sample_spec.channels, volume);
2298 if (!(o = pa_context_set_sink_input_volume (pbuf->context, idx,
2302 /* We don't really care about the result of this call */
2306 pa_operation_unref (o);
2308 pa_threaded_mainloop_unlock (mainloop);
2315 psink->volume = volume;
2316 psink->volume_set = TRUE;
2318 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2323 psink->volume = volume;
2324 psink->volume_set = TRUE;
2326 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2331 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2336 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2337 ("pa_stream_set_sink_input_volume() failed: %s",
2338 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2344 gst_pulsesink_set_mute (GstPulseSink * psink, gboolean mute)
2346 pa_operation *o = NULL;
2347 GstPulseRingBuffer *pbuf;
2353 pa_threaded_mainloop_lock (mainloop);
2355 GST_DEBUG_OBJECT (psink, "setting mute state to %d", mute);
2357 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2358 if (pbuf == NULL || pbuf->stream == NULL)
2361 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2364 if (!(o = pa_context_set_sink_input_mute (pbuf->context, idx,
2368 /* We don't really care about the result of this call */
2372 pa_operation_unref (o);
2374 pa_threaded_mainloop_unlock (mainloop);
2382 psink->mute_set = TRUE;
2384 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2390 psink->mute_set = TRUE;
2392 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2397 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2402 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2403 ("pa_stream_set_sink_input_mute() failed: %s",
2404 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2410 gst_pulsesink_sink_input_info_cb (pa_context * c, const pa_sink_input_info * i,
2411 int eol, void *userdata)
2413 GstPulseRingBuffer *pbuf;
2414 GstPulseSink *psink;
2416 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
2417 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
2425 /* If the index doesn't match our current stream,
2426 * it implies we just recreated the stream (caps change)
2428 if (i->index == pa_stream_get_index (pbuf->stream)) {
2429 psink->volume = pa_sw_volume_to_linear (pa_cvolume_max (&i->volume));
2430 psink->mute = i->mute;
2434 pa_threaded_mainloop_signal (mainloop, 0);
2438 gst_pulsesink_get_volume (GstPulseSink * psink)
2440 GstPulseRingBuffer *pbuf;
2441 pa_operation *o = NULL;
2442 gdouble v = DEFAULT_VOLUME;
2448 pa_threaded_mainloop_lock (mainloop);
2450 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2451 if (pbuf == NULL || pbuf->stream == NULL)
2454 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2457 if (!(o = pa_context_get_sink_input_info (pbuf->context, idx,
2458 gst_pulsesink_sink_input_info_cb, pbuf)))
2461 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2462 pa_threaded_mainloop_wait (mainloop);
2463 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
2471 pa_operation_unref (o);
2473 pa_threaded_mainloop_unlock (mainloop);
2475 if (v > MAX_VOLUME) {
2476 GST_WARNING_OBJECT (psink, "Clipped volume from %f to %f", v, MAX_VOLUME);
2486 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2491 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2496 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2501 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2502 ("pa_context_get_sink_input_info() failed: %s",
2503 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2509 gst_pulsesink_get_mute (GstPulseSink * psink)
2511 GstPulseRingBuffer *pbuf;
2512 pa_operation *o = NULL;
2514 gboolean mute = FALSE;
2519 pa_threaded_mainloop_lock (mainloop);
2522 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2523 if (pbuf == NULL || pbuf->stream == NULL)
2526 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2529 if (!(o = pa_context_get_sink_input_info (pbuf->context, idx,
2530 gst_pulsesink_sink_input_info_cb, pbuf)))
2533 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2534 pa_threaded_mainloop_wait (mainloop);
2535 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
2541 pa_operation_unref (o);
2543 pa_threaded_mainloop_unlock (mainloop);
2551 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2556 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2561 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2566 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2567 ("pa_context_get_sink_input_info() failed: %s",
2568 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2574 gst_pulsesink_device_description (GstPulseSink * psink)
2576 GstPulseRingBuffer *pbuf;
2577 pa_operation *o = NULL;
2583 pa_threaded_mainloop_lock (mainloop);
2584 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2588 if (!(o = pa_context_get_sink_info_by_name (pbuf->context,
2589 psink->device, gst_pulsesink_sink_info_cb, pbuf)))
2592 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2593 pa_threaded_mainloop_wait (mainloop);
2594 if (gst_pulsering_is_dead (psink, pbuf, FALSE))
2600 pa_operation_unref (o);
2602 t = g_strdup (psink->device_description);
2603 pa_threaded_mainloop_unlock (mainloop);
2610 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2615 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2620 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2621 ("pa_context_get_sink_info_by_index() failed: %s",
2622 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2628 gst_pulsesink_set_property (GObject * object,
2629 guint prop_id, const GValue * value, GParamSpec * pspec)
2631 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2635 g_free (pulsesink->server);
2636 pulsesink->server = g_value_dup_string (value);
2637 if (pulsesink->probe)
2638 gst_pulseprobe_set_server (pulsesink->probe, pulsesink->server);
2641 g_free (pulsesink->device);
2642 pulsesink->device = g_value_dup_string (value);
2645 gst_pulsesink_set_volume (pulsesink, g_value_get_double (value));
2648 gst_pulsesink_set_mute (pulsesink, g_value_get_boolean (value));
2651 g_free (pulsesink->client_name);
2652 if (!g_value_get_string (value)) {
2653 GST_WARNING_OBJECT (pulsesink,
2654 "Empty PulseAudio client name not allowed. Resetting to default value");
2655 pulsesink->client_name = gst_pulse_client_name ();
2657 pulsesink->client_name = g_value_dup_string (value);
2659 case PROP_STREAM_PROPERTIES:
2660 if (pulsesink->properties)
2661 gst_structure_free (pulsesink->properties);
2662 pulsesink->properties =
2663 gst_structure_copy (gst_value_get_structure (value));
2664 if (pulsesink->proplist)
2665 pa_proplist_free (pulsesink->proplist);
2666 pulsesink->proplist = gst_pulse_make_proplist (pulsesink->properties);
2669 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2675 gst_pulsesink_get_property (GObject * object,
2676 guint prop_id, GValue * value, GParamSpec * pspec)
2679 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2683 g_value_set_string (value, pulsesink->server);
2686 g_value_set_string (value, pulsesink->device);
2688 case PROP_DEVICE_NAME:
2689 g_value_take_string (value, gst_pulsesink_device_description (pulsesink));
2692 g_value_set_double (value, gst_pulsesink_get_volume (pulsesink));
2695 g_value_set_boolean (value, gst_pulsesink_get_mute (pulsesink));
2698 g_value_set_string (value, pulsesink->client_name);
2700 case PROP_STREAM_PROPERTIES:
2701 gst_value_set_structure (value, pulsesink->properties);
2704 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2710 gst_pulsesink_change_title (GstPulseSink * psink, const gchar * t)
2712 pa_operation *o = NULL;
2713 GstPulseRingBuffer *pbuf;
2715 pa_threaded_mainloop_lock (mainloop);
2717 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2719 if (pbuf == NULL || pbuf->stream == NULL)
2722 g_free (pbuf->stream_name);
2723 pbuf->stream_name = g_strdup (t);
2725 if (!(o = pa_stream_set_name (pbuf->stream, pbuf->stream_name, NULL, NULL)))
2728 /* We're not interested if this operation failed or not */
2732 pa_operation_unref (o);
2733 pa_threaded_mainloop_unlock (mainloop);
2740 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2745 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2746 ("pa_stream_set_name() failed: %s",
2747 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2753 gst_pulsesink_change_props (GstPulseSink * psink, GstTagList * l)
2755 static const gchar *const map[] = {
2756 GST_TAG_TITLE, PA_PROP_MEDIA_TITLE,
2758 /* might get overriden in the next iteration by GST_TAG_ARTIST */
2759 GST_TAG_PERFORMER, PA_PROP_MEDIA_ARTIST,
2761 GST_TAG_ARTIST, PA_PROP_MEDIA_ARTIST,
2762 GST_TAG_LANGUAGE_CODE, PA_PROP_MEDIA_LANGUAGE,
2763 GST_TAG_LOCATION, PA_PROP_MEDIA_FILENAME,
2764 /* We might add more here later on ... */
2767 pa_proplist *pl = NULL;
2768 const gchar *const *t;
2769 gboolean empty = TRUE;
2770 pa_operation *o = NULL;
2771 GstPulseRingBuffer *pbuf;
2773 pl = pa_proplist_new ();
2775 for (t = map; *t; t += 2) {
2778 if (gst_tag_list_get_string (l, *t, &n)) {
2781 pa_proplist_sets (pl, *(t + 1), n);
2791 pa_threaded_mainloop_lock (mainloop);
2792 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2793 if (pbuf == NULL || pbuf->stream == NULL)
2796 if (!(o = pa_stream_proplist_update (pbuf->stream, PA_UPDATE_REPLACE,
2800 /* We're not interested if this operation failed or not */
2804 pa_operation_unref (o);
2806 pa_threaded_mainloop_unlock (mainloop);
2811 pa_proplist_free (pl);
2818 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2823 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2824 ("pa_stream_proplist_update() failed: %s",
2825 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2831 gst_pulsesink_flush_ringbuffer (GstPulseSink * psink)
2833 GstPulseRingBuffer *pbuf;
2835 pa_threaded_mainloop_lock (mainloop);
2837 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2839 if (pbuf == NULL || pbuf->stream == NULL)
2842 gst_pulsering_flush (pbuf);
2844 /* Uncork if we haven't already (happens when waiting to get enough data
2845 * to send out the first time) */
2847 gst_pulsering_set_corked (pbuf, FALSE, FALSE);
2849 /* We're not interested if this operation failed or not */
2851 pa_threaded_mainloop_unlock (mainloop);
2858 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2864 gst_pulsesink_event (GstBaseSink * sink, GstEvent * event)
2866 GstPulseSink *pulsesink = GST_PULSESINK_CAST (sink);
2868 switch (GST_EVENT_TYPE (event)) {
2869 case GST_EVENT_TAG:{
2870 gchar *title = NULL, *artist = NULL, *location = NULL, *description =
2871 NULL, *t = NULL, *buf = NULL;
2874 gst_event_parse_tag (event, &l);
2876 gst_tag_list_get_string (l, GST_TAG_TITLE, &title);
2877 gst_tag_list_get_string (l, GST_TAG_ARTIST, &artist);
2878 gst_tag_list_get_string (l, GST_TAG_LOCATION, &location);
2879 gst_tag_list_get_string (l, GST_TAG_DESCRIPTION, &description);
2882 gst_tag_list_get_string (l, GST_TAG_PERFORMER, &artist);
2884 if (title && artist)
2885 /* TRANSLATORS: 'song title' by 'artist name' */
2886 t = buf = g_strdup_printf (_("'%s' by '%s'"), g_strstrip (title),
2887 g_strstrip (artist));
2889 t = g_strstrip (title);
2890 else if (description)
2891 t = g_strstrip (description);
2893 t = g_strstrip (location);
2896 gst_pulsesink_change_title (pulsesink, t);
2901 g_free (description);
2904 gst_pulsesink_change_props (pulsesink, l);
2909 gst_pulsesink_flush_ringbuffer (pulsesink);
2915 return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
2919 gst_pulsesink_release_mainloop (GstPulseSink * psink)
2924 pa_threaded_mainloop_lock (mainloop);
2925 while (psink->defer_pending) {
2926 GST_DEBUG_OBJECT (psink, "waiting for stream status message emission");
2927 pa_threaded_mainloop_wait (mainloop);
2929 pa_threaded_mainloop_unlock (mainloop);
2931 g_mutex_lock (pa_shared_resource_mutex);
2933 if (!mainloop_ref_ct) {
2934 GST_INFO_OBJECT (psink, "terminating pa main loop thread");
2935 pa_threaded_mainloop_stop (mainloop);
2936 pa_threaded_mainloop_free (mainloop);
2939 g_mutex_unlock (pa_shared_resource_mutex);
2942 static GstStateChangeReturn
2943 gst_pulsesink_change_state (GstElement * element, GstStateChange transition)
2945 GstPulseSink *pulsesink = GST_PULSESINK (element);
2946 GstStateChangeReturn ret;
2948 switch (transition) {
2949 case GST_STATE_CHANGE_NULL_TO_READY:
2950 g_mutex_lock (pa_shared_resource_mutex);
2951 if (!mainloop_ref_ct) {
2952 GST_INFO_OBJECT (element, "new pa main loop thread");
2953 if (!(mainloop = pa_threaded_mainloop_new ()))
2954 goto mainloop_failed;
2955 mainloop_ref_ct = 1;
2956 pa_threaded_mainloop_start (mainloop);
2957 g_mutex_unlock (pa_shared_resource_mutex);
2959 GST_INFO_OBJECT (element, "reusing pa main loop thread");
2961 g_mutex_unlock (pa_shared_resource_mutex);
2964 case GST_STATE_CHANGE_READY_TO_PAUSED:
2965 gst_element_post_message (element,
2966 gst_message_new_clock_provide (GST_OBJECT_CAST (element),
2967 GST_BASE_AUDIO_SINK (pulsesink)->provided_clock, TRUE));
2974 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2975 if (ret == GST_STATE_CHANGE_FAILURE)
2978 switch (transition) {
2979 case GST_STATE_CHANGE_PAUSED_TO_READY:
2980 /* format_lost is reset in release() in baseaudiosink */
2981 gst_element_post_message (element,
2982 gst_message_new_clock_lost (GST_OBJECT_CAST (element),
2983 GST_BASE_AUDIO_SINK (pulsesink)->provided_clock));
2985 case GST_STATE_CHANGE_READY_TO_NULL:
2986 gst_pulsesink_release_mainloop (pulsesink);
2997 g_mutex_unlock (pa_shared_resource_mutex);
2998 GST_ELEMENT_ERROR (pulsesink, RESOURCE, FAILED,
2999 ("pa_threaded_mainloop_new() failed"), (NULL));
3000 return GST_STATE_CHANGE_FAILURE;
3004 if (transition == GST_STATE_CHANGE_NULL_TO_READY) {
3005 /* Clear the PA mainloop if baseaudiosink failed to open the ring_buffer */
3006 g_assert (mainloop);
3007 gst_pulsesink_release_mainloop (pulsesink);