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.
49 #include <gst/base/gstbasesink.h>
50 #include <gst/gsttaglist.h>
51 #include <gst/interfaces/streamvolume.h>
52 #include <gst/gst-i18n-plugin.h>
54 #include <gst/pbutils/pbutils.h> /* only used for GST_PLUGINS_BASE_VERSION_* */
56 #include "pulsesink.h"
57 #include "pulseutil.h"
59 GST_DEBUG_CATEGORY_EXTERN (pulse_debug);
60 #define GST_CAT_DEFAULT pulse_debug
63 * http://www.pulseaudio.org/ticket/314
64 * we need pulse-0.9.12 to use sink volume properties
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
86 #define GST_TYPE_PULSERING_BUFFER \
87 (gst_pulseringbuffer_get_type())
88 #define GST_PULSERING_BUFFER(obj) \
89 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_PULSERING_BUFFER,GstPulseRingBuffer))
90 #define GST_PULSERING_BUFFER_CLASS(klass) \
91 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_PULSERING_BUFFER,GstPulseRingBufferClass))
92 #define GST_PULSERING_BUFFER_GET_CLASS(obj) \
93 (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_PULSERING_BUFFER, GstPulseRingBufferClass))
94 #define GST_PULSERING_BUFFER_CAST(obj) \
95 ((GstPulseRingBuffer *)obj)
96 #define GST_IS_PULSERING_BUFFER(obj) \
97 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_PULSERING_BUFFER))
98 #define GST_IS_PULSERING_BUFFER_CLASS(klass)\
99 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_PULSERING_BUFFER))
101 typedef struct _GstPulseRingBuffer GstPulseRingBuffer;
102 typedef struct _GstPulseRingBufferClass GstPulseRingBufferClass;
104 typedef struct _GstPulseContext GstPulseContext;
106 struct _GstPulseContext
109 GSList *ring_buffers;
112 /* Store the PA contexts in a hash table to allow easy sharing among
113 * multiple instances of the sink. Keys are $context_name@$server_name
114 * (strings) and values should be GstPulseContext pointers. */
115 static GHashTable *gst_pulse_shared_contexts;
117 /* We keep a custom ringbuffer that is backed up by data allocated by
118 * pulseaudio. We must also overide the commit function to write into
119 * pulseaudio memory instead. */
120 struct _GstPulseRingBuffer
122 GstRingBuffer object;
129 pa_sample_spec sample_spec;
131 #ifdef HAVE_PULSE_0_9_16
140 gboolean in_commit:1;
143 struct _GstPulseRingBufferClass
145 GstRingBufferClass parent_class;
148 static GType gst_pulseringbuffer_get_type (void);
149 static void gst_pulseringbuffer_finalize (GObject * object);
151 static GstRingBufferClass *ring_parent_class = NULL;
153 static gboolean gst_pulseringbuffer_open_device (GstRingBuffer * buf);
154 static gboolean gst_pulseringbuffer_close_device (GstRingBuffer * buf);
155 static gboolean gst_pulseringbuffer_acquire (GstRingBuffer * buf,
156 GstRingBufferSpec * spec);
157 static gboolean gst_pulseringbuffer_release (GstRingBuffer * buf);
158 static gboolean gst_pulseringbuffer_start (GstRingBuffer * buf);
159 static gboolean gst_pulseringbuffer_pause (GstRingBuffer * buf);
160 static gboolean gst_pulseringbuffer_stop (GstRingBuffer * buf);
161 static void gst_pulseringbuffer_clear (GstRingBuffer * buf);
162 static guint gst_pulseringbuffer_commit (GstRingBuffer * buf,
163 guint64 * sample, guchar * data, gint in_samples, gint out_samples,
166 G_DEFINE_TYPE (GstPulseRingBuffer, gst_pulseringbuffer, GST_TYPE_RING_BUFFER);
168 static GMutex *pa_ring_buffer_mutex = NULL;
170 gst_pulseringbuffer_init_contexts (void)
172 g_assert (pa_ring_buffer_mutex == NULL);
173 pa_ring_buffer_mutex = g_mutex_new ();
174 gst_pulse_shared_contexts = g_hash_table_new (g_str_hash, g_str_equal);
178 gst_pulseringbuffer_class_init (GstPulseRingBufferClass * klass)
180 GObjectClass *gobject_class;
181 GstRingBufferClass *gstringbuffer_class;
183 gobject_class = (GObjectClass *) klass;
184 gstringbuffer_class = (GstRingBufferClass *) klass;
186 ring_parent_class = g_type_class_peek_parent (klass);
188 gobject_class->finalize = gst_pulseringbuffer_finalize;
190 gstringbuffer_class->open_device =
191 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_open_device);
192 gstringbuffer_class->close_device =
193 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_close_device);
194 gstringbuffer_class->acquire =
195 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_acquire);
196 gstringbuffer_class->release =
197 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_release);
198 gstringbuffer_class->start = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_start);
199 gstringbuffer_class->pause = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_pause);
200 gstringbuffer_class->resume = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_start);
201 gstringbuffer_class->stop = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_stop);
202 gstringbuffer_class->clear_all =
203 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_clear);
205 gstringbuffer_class->commit = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_commit);
209 gst_pulseringbuffer_init (GstPulseRingBuffer * pbuf)
211 pbuf->stream_name = NULL;
214 #ifdef HAVE_PULSE_0_9_13
215 pa_sample_spec_init (&pbuf->sample_spec);
217 pbuf->sample_spec.format = PA_SAMPLE_INVALID;
218 pbuf->sample_spec.rate = 0;
219 pbuf->sample_spec.channels = 0;
222 #ifdef HAVE_PULSE_0_9_16
225 pbuf->m_writable = 0;
227 pbuf->m_lastoffset = 0;
231 pbuf->in_commit = FALSE;
232 pbuf->paused = FALSE;
236 gst_pulsering_destroy_stream (GstPulseRingBuffer * pbuf)
240 #ifdef HAVE_PULSE_0_9_16
242 /* drop shm memory buffer */
243 pa_stream_cancel_write (pbuf->stream);
245 /* reset internal variables */
248 pbuf->m_writable = 0;
250 pbuf->m_lastoffset = 0;
254 pa_stream_disconnect (pbuf->stream);
256 /* Make sure we don't get any further callbacks */
257 pa_stream_set_state_callback (pbuf->stream, NULL, NULL);
258 pa_stream_set_write_callback (pbuf->stream, NULL, NULL);
259 pa_stream_set_underflow_callback (pbuf->stream, NULL, NULL);
260 pa_stream_set_overflow_callback (pbuf->stream, NULL, NULL);
262 pa_stream_unref (pbuf->stream);
266 g_free (pbuf->stream_name);
267 pbuf->stream_name = NULL;
270 static GstPulseContext *
271 gst_pulsering_get_context (GstPulseRingBuffer * pbuf)
273 GstPulseContext *pctx;
276 g_mutex_lock (pa_ring_buffer_mutex);
277 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
278 pctx = g_hash_table_lookup (gst_pulse_shared_contexts, pbuf->context_name);
279 g_mutex_unlock (pa_ring_buffer_mutex);
284 gst_pulsering_destroy_context (GstPulseRingBuffer * pbuf)
286 GstPulseContext *pctx;
289 g_mutex_lock (pa_ring_buffer_mutex);
290 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
292 pctx = g_hash_table_lookup (gst_pulse_shared_contexts, pbuf->context_name);
294 gst_pulsering_destroy_stream (pbuf);
297 pctx->ring_buffers = g_slist_remove (pctx->ring_buffers, pbuf);
298 if (!g_slist_length (pctx->ring_buffers)) {
299 pa_context_disconnect (pctx->context);
301 /* Make sure we don't get any further callbacks */
302 pa_context_set_state_callback (pctx->context, NULL, NULL);
303 #ifdef HAVE_PULSE_0_9_12
304 pa_context_set_subscribe_callback (pctx->context, NULL, NULL);
307 pa_context_unref (pctx->context);
308 g_hash_table_remove (gst_pulse_shared_contexts, pbuf->context_name);
309 g_free (pbuf->context_name);
310 g_slice_free (GstPulseContext, pctx);
313 g_mutex_unlock (pa_ring_buffer_mutex);
317 gst_pulseringbuffer_finalize (GObject * object)
319 GstPulseRingBuffer *ringbuffer;
321 ringbuffer = GST_PULSERING_BUFFER_CAST (object);
323 gst_pulsering_destroy_context (ringbuffer);
325 G_OBJECT_CLASS (ring_parent_class)->finalize (object);
329 gst_pulsering_is_dead (GstPulseSink * psink, GstPulseRingBuffer * pbuf)
331 GstPulseContext *pctx = gst_pulsering_get_context (pbuf);
334 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED, ("Disconnected"), (NULL));
339 || !PA_CONTEXT_IS_GOOD (pa_context_get_state (pctx->context))
341 || !PA_STREAM_IS_GOOD (pa_stream_get_state (pbuf->stream))) {
342 const gchar *err_str =
343 pctx->context ? pa_strerror (pa_context_errno (pctx->context)) : NULL;
344 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED, ("Disconnected: %s",
352 gst_pulsering_context_state_cb (pa_context * c, void *userdata)
355 pa_context_state_t state;
357 GstPulseContext *pctx = (GstPulseContext *) userdata;
360 state = pa_context_get_state (c);
362 for (walk = pctx->ring_buffers; walk; walk = g_slist_next (walk)) {
363 GstPulseRingBuffer *pbuf = (GstPulseRingBuffer *) walk->data;
364 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
365 GST_LOG_OBJECT (psink, "got new context state %d", state);
367 /* psink can be null when we are shutting down and the ringbuffer is already
373 case PA_CONTEXT_READY:
374 case PA_CONTEXT_TERMINATED:
375 case PA_CONTEXT_FAILED:
376 GST_LOG_OBJECT (psink, "signaling");
377 pa_threaded_mainloop_signal (psink->mainloop, 0);
380 case PA_CONTEXT_UNCONNECTED:
381 case PA_CONTEXT_CONNECTING:
382 case PA_CONTEXT_AUTHORIZING:
383 case PA_CONTEXT_SETTING_NAME:
389 #ifdef HAVE_PULSE_0_9_12
391 gst_pulsering_context_subscribe_cb (pa_context * c,
392 pa_subscription_event_type_t t, uint32_t idx, void *userdata)
395 GstPulseContext *pctx = (GstPulseContext *) userdata;
398 if (t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT | PA_SUBSCRIPTION_EVENT_CHANGE) &&
399 t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT | PA_SUBSCRIPTION_EVENT_NEW))
402 for (walk = pctx->ring_buffers; walk; walk = g_slist_next (walk)) {
403 GstPulseRingBuffer *pbuf = (GstPulseRingBuffer *) walk->data;
404 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
406 GST_LOG_OBJECT (psink, "type %d, idx %u", t, idx);
411 if (idx != pa_stream_get_index (pbuf->stream))
414 /* Actually this event is also triggered when other properties of
415 * the stream change that are unrelated to the volume. However it is
416 * probably cheaper to signal the change here and check for the
417 * volume when the GObject property is read instead of querying it always. */
419 /* inform streaming thread to notify */
420 g_atomic_int_compare_and_exchange (&psink->notify, 0, 1);
425 /* will be called when the device should be opened. In this case we will connect
426 * to the server. We should not try to open any streams in this state. */
428 gst_pulseringbuffer_open_device (GstRingBuffer * buf)
431 GstPulseRingBuffer *pbuf;
432 GstPulseContext *pctx;
433 pa_mainloop_api *api;
435 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
436 pbuf = GST_PULSERING_BUFFER_CAST (buf);
438 g_assert (!pbuf->stream);
440 g_assert (psink->client_name);
441 pbuf->context_name = g_strdup_printf ("%s@%s", psink->client_name,
442 GST_STR_NULL (psink->server));
444 pa_threaded_mainloop_lock (psink->mainloop);
445 g_mutex_lock (pa_ring_buffer_mutex);
447 pctx = g_hash_table_lookup (gst_pulse_shared_contexts, pbuf->context_name);
449 pctx = g_slice_new0 (GstPulseContext);
450 /* get the mainloop api and create a context */
451 GST_LOG_OBJECT (psink, "new context with name %s",
452 GST_STR_NULL (pbuf->context_name));
453 api = pa_threaded_mainloop_get_api (psink->mainloop);
454 if (!(pctx->context = pa_context_new (api, pbuf->context_name)))
457 pctx->ring_buffers = g_slist_append (pctx->ring_buffers, pbuf);
458 g_hash_table_insert (gst_pulse_shared_contexts, pbuf->context_name,
460 /* register some essential callbacks */
461 pa_context_set_state_callback (pctx->context,
462 gst_pulsering_context_state_cb, pctx);
463 #ifdef HAVE_PULSE_0_9_12
464 pa_context_set_subscribe_callback (pctx->context,
465 gst_pulsering_context_subscribe_cb, pctx);
468 /* try to connect to the server and wait for completioni, we don't want to
469 * autospawn a deamon */
470 GST_LOG_OBJECT (psink, "connect to server %s",
471 GST_STR_NULL (psink->server));
472 if (pa_context_connect (pctx->context, psink->server,
473 PA_CONTEXT_NOAUTOSPAWN, NULL) < 0)
476 GST_LOG_OBJECT (psink, "reusing shared pulseaudio context with name %s",
477 GST_STR_NULL (pbuf->context_name));
478 pctx->ring_buffers = g_slist_append (pctx->ring_buffers, pbuf);
482 pa_context_state_t state;
484 state = pa_context_get_state (pctx->context);
486 GST_LOG_OBJECT (psink, "context state is now %d", state);
488 if (!PA_CONTEXT_IS_GOOD (state))
491 if (state == PA_CONTEXT_READY)
494 /* Wait until the context is ready */
495 GST_LOG_OBJECT (psink, "waiting..");
496 pa_threaded_mainloop_wait (psink->mainloop);
499 GST_LOG_OBJECT (psink, "opened the device");
501 g_mutex_unlock (pa_ring_buffer_mutex);
502 pa_threaded_mainloop_unlock (psink->mainloop);
509 g_mutex_unlock (pa_ring_buffer_mutex);
510 gst_pulsering_destroy_context (pbuf);
512 pa_threaded_mainloop_unlock (psink->mainloop);
517 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
518 ("Failed to create context"), (NULL));
519 goto unlock_and_fail;
523 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED, ("Failed to connect: %s",
524 pa_strerror (pa_context_errno (pctx->context))), (NULL));
525 goto unlock_and_fail;
529 /* close the device */
531 gst_pulseringbuffer_close_device (GstRingBuffer * buf)
534 GstPulseRingBuffer *pbuf;
536 pbuf = GST_PULSERING_BUFFER_CAST (buf);
537 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
539 GST_LOG_OBJECT (psink, "closing device");
541 pa_threaded_mainloop_lock (psink->mainloop);
542 gst_pulsering_destroy_context (pbuf);
543 pa_threaded_mainloop_unlock (psink->mainloop);
545 GST_LOG_OBJECT (psink, "closed device");
551 gst_pulsering_stream_state_cb (pa_stream * s, void *userdata)
554 GstPulseRingBuffer *pbuf;
555 pa_stream_state_t state;
557 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
558 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
560 state = pa_stream_get_state (s);
561 GST_LOG_OBJECT (psink, "got new stream state %d", state);
564 case PA_STREAM_READY:
565 case PA_STREAM_FAILED:
566 case PA_STREAM_TERMINATED:
567 GST_LOG_OBJECT (psink, "signaling");
568 pa_threaded_mainloop_signal (psink->mainloop, 0);
570 case PA_STREAM_UNCONNECTED:
571 case PA_STREAM_CREATING:
577 gst_pulsering_stream_request_cb (pa_stream * s, size_t length, void *userdata)
581 GstPulseRingBuffer *pbuf;
583 rbuf = GST_RING_BUFFER_CAST (userdata);
584 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
585 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
587 GST_LOG_OBJECT (psink, "got request for length %" G_GSIZE_FORMAT, length);
589 if (pbuf->in_commit && (length >= rbuf->spec.segsize)) {
590 /* only signal when we are waiting in the commit thread
591 * and got request for atleast a segment */
592 pa_threaded_mainloop_signal (psink->mainloop, 0);
597 gst_pulsering_stream_underflow_cb (pa_stream * s, void *userdata)
600 GstPulseRingBuffer *pbuf;
602 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
603 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
605 GST_WARNING_OBJECT (psink, "Got underflow");
609 gst_pulsering_stream_overflow_cb (pa_stream * s, void *userdata)
612 GstPulseRingBuffer *pbuf;
614 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
615 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
617 GST_WARNING_OBJECT (psink, "Got overflow");
621 gst_pulsering_stream_latency_cb (pa_stream * s, void *userdata)
624 GstPulseRingBuffer *pbuf;
625 const pa_timing_info *info;
628 info = pa_stream_get_timing_info (s);
630 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
631 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
634 GST_LOG_OBJECT (psink, "latency update (information unknown)");
637 #ifdef HAVE_PULSE_0_9_11
638 sink_usec = info->configured_sink_usec;
643 GST_LOG_OBJECT (psink,
644 "latency_update, %" G_GUINT64_FORMAT ", %d:%" G_GINT64_FORMAT ", %d:%"
645 G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT,
646 GST_TIMEVAL_TO_TIME (info->timestamp), info->write_index_corrupt,
647 info->write_index, info->read_index_corrupt, info->read_index,
648 info->sink_usec, sink_usec);
652 gst_pulsering_stream_suspended_cb (pa_stream * p, void *userdata)
655 GstPulseRingBuffer *pbuf;
657 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
658 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
660 if (pa_stream_is_suspended (p))
661 GST_DEBUG_OBJECT (psink, "stream suspended");
663 GST_DEBUG_OBJECT (psink, "stream resumed");
666 #ifdef HAVE_PULSE_0_9_11
668 gst_pulsering_stream_started_cb (pa_stream * p, void *userdata)
671 GstPulseRingBuffer *pbuf;
673 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
674 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
676 GST_DEBUG_OBJECT (psink, "stream started");
680 #ifdef HAVE_PULSE_0_9_15
682 gst_pulsering_stream_event_cb (pa_stream * p, const char *name,
683 pa_proplist * pl, void *userdata)
686 GstPulseRingBuffer *pbuf;
688 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
689 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
691 if (!strcmp (name, PA_STREAM_EVENT_REQUEST_CORK)) {
692 /* the stream wants to PAUSE, post a message for the application. */
693 GST_DEBUG_OBJECT (psink, "got request for CORK");
694 gst_element_post_message (GST_ELEMENT_CAST (psink),
695 gst_message_new_request_state (GST_OBJECT_CAST (psink),
698 } else if (!strcmp (name, PA_STREAM_EVENT_REQUEST_UNCORK)) {
699 GST_DEBUG_OBJECT (psink, "got request for UNCORK");
700 gst_element_post_message (GST_ELEMENT_CAST (psink),
701 gst_message_new_request_state (GST_OBJECT_CAST (psink),
704 GST_DEBUG_OBJECT (psink, "got unknown event %s", name);
709 /* This method should create a new stream of the given @spec. No playback should
710 * start yet so we start in the corked state. */
712 gst_pulseringbuffer_acquire (GstRingBuffer * buf, GstRingBufferSpec * spec)
715 GstPulseRingBuffer *pbuf;
716 GstPulseContext *pctx;
717 pa_buffer_attr wanted;
718 const pa_buffer_attr *actual;
719 pa_channel_map channel_map;
720 pa_operation *o = NULL;
721 #ifdef HAVE_PULSE_0_9_20
724 pa_cvolume *pv = NULL;
725 pa_stream_flags_t flags;
727 GstAudioClock *clock;
729 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
730 pbuf = GST_PULSERING_BUFFER_CAST (buf);
732 GST_LOG_OBJECT (psink, "creating sample spec");
733 /* convert the gstreamer sample spec to the pulseaudio format */
734 if (!gst_pulse_fill_sample_spec (spec, &pbuf->sample_spec))
737 pa_threaded_mainloop_lock (psink->mainloop);
739 /* we need a context and a no stream */
740 pctx = gst_pulsering_get_context (pbuf);
741 g_assert (!pbuf->stream);
743 /* enable event notifications */
744 GST_LOG_OBJECT (psink, "subscribing to context events");
745 if (!(o = pa_context_subscribe (pctx->context,
746 PA_SUBSCRIPTION_MASK_SINK_INPUT, NULL, NULL)))
747 goto subscribe_failed;
749 pa_operation_unref (o);
751 /* initialize the channel map */
752 gst_pulse_gst_to_channel_map (&channel_map, spec);
754 /* find a good name for the stream */
755 if (psink->stream_name)
756 name = psink->stream_name;
758 name = "Playback Stream";
760 /* create a stream */
761 GST_LOG_OBJECT (psink, "creating stream with name %s", name);
762 if (!(pbuf->stream = pa_stream_new (pctx->context,
763 name, &pbuf->sample_spec, &channel_map)))
766 /* install essential callbacks */
767 pa_stream_set_state_callback (pbuf->stream,
768 gst_pulsering_stream_state_cb, pbuf);
769 pa_stream_set_write_callback (pbuf->stream,
770 gst_pulsering_stream_request_cb, pbuf);
771 pa_stream_set_underflow_callback (pbuf->stream,
772 gst_pulsering_stream_underflow_cb, pbuf);
773 pa_stream_set_overflow_callback (pbuf->stream,
774 gst_pulsering_stream_overflow_cb, pbuf);
775 pa_stream_set_latency_update_callback (pbuf->stream,
776 gst_pulsering_stream_latency_cb, pbuf);
777 pa_stream_set_suspended_callback (pbuf->stream,
778 gst_pulsering_stream_suspended_cb, pbuf);
779 #ifdef HAVE_PULSE_0_9_11
780 pa_stream_set_started_callback (pbuf->stream,
781 gst_pulsering_stream_started_cb, pbuf);
783 #ifdef HAVE_PULSE_0_9_15
784 pa_stream_set_event_callback (pbuf->stream,
785 gst_pulsering_stream_event_cb, pbuf);
788 /* buffering requirements. When setting prebuf to 0, the stream will not pause
789 * when we cause an underrun, which causes time to continue. */
790 memset (&wanted, 0, sizeof (wanted));
791 wanted.tlength = spec->segtotal * spec->segsize;
792 wanted.maxlength = -1;
794 wanted.minreq = spec->segsize;
796 GST_INFO_OBJECT (psink, "tlength: %d", wanted.tlength);
797 GST_INFO_OBJECT (psink, "maxlength: %d", wanted.maxlength);
798 GST_INFO_OBJECT (psink, "prebuf: %d", wanted.prebuf);
799 GST_INFO_OBJECT (psink, "minreq: %d", wanted.minreq);
801 #ifdef HAVE_PULSE_0_9_20
802 /* configure volume when we changed it, else we leave the default */
803 if (psink->volume_set) {
804 GST_LOG_OBJECT (psink, "have volume of %f", psink->volume);
806 gst_pulse_cvolume_from_linear (pv, pbuf->sample_spec.channels,
813 /* construct the flags */
814 flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE |
815 #ifdef HAVE_PULSE_0_9_11
816 PA_STREAM_ADJUST_LATENCY |
818 PA_STREAM_START_CORKED;
820 #ifdef HAVE_PULSE_0_9_12
821 if (psink->mute_set && psink->mute)
822 flags |= PA_STREAM_START_MUTED;
825 /* we always start corked (see flags above) */
828 /* try to connect now */
829 GST_LOG_OBJECT (psink, "connect for playback to device %s",
830 GST_STR_NULL (psink->device));
831 if (pa_stream_connect_playback (pbuf->stream, psink->device,
832 &wanted, flags, pv, NULL) < 0)
835 /* our clock will now start from 0 again */
836 clock = GST_AUDIO_CLOCK (GST_BASE_AUDIO_SINK (psink)->provided_clock);
837 gst_audio_clock_reset (clock, 0);
840 pa_stream_state_t state;
842 state = pa_stream_get_state (pbuf->stream);
844 GST_LOG_OBJECT (psink, "stream state is now %d", state);
846 if (!PA_STREAM_IS_GOOD (state))
849 if (state == PA_STREAM_READY)
852 /* Wait until the stream is ready */
853 pa_threaded_mainloop_wait (psink->mainloop);
856 /* After we passed the volume off of to PA we never want to set it
857 again, since it is PA's job to save/restore volumes. */
858 psink->volume_set = psink->mute_set = FALSE;
860 GST_LOG_OBJECT (psink, "stream is acquired now");
862 /* get the actual buffering properties now */
863 actual = pa_stream_get_buffer_attr (pbuf->stream);
865 GST_INFO_OBJECT (psink, "tlength: %d (wanted: %d)", actual->tlength,
867 GST_INFO_OBJECT (psink, "maxlength: %d", actual->maxlength);
868 GST_INFO_OBJECT (psink, "prebuf: %d", actual->prebuf);
869 GST_INFO_OBJECT (psink, "minreq: %d (wanted %d)", actual->minreq,
872 spec->segsize = actual->minreq;
873 spec->segtotal = actual->tlength / spec->segsize;
875 pa_threaded_mainloop_unlock (psink->mainloop);
882 gst_pulsering_destroy_stream (pbuf);
883 pa_threaded_mainloop_unlock (psink->mainloop);
889 GST_ELEMENT_ERROR (psink, RESOURCE, SETTINGS,
890 ("Invalid sample specification."), (NULL));
895 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
896 ("pa_context_subscribe() failed: %s",
897 pa_strerror (pa_context_errno (pctx->context))), (NULL));
898 goto unlock_and_fail;
902 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
903 ("Failed to create stream: %s",
904 pa_strerror (pa_context_errno (pctx->context))), (NULL));
905 goto unlock_and_fail;
909 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
910 ("Failed to connect stream: %s",
911 pa_strerror (pa_context_errno (pctx->context))), (NULL));
912 goto unlock_and_fail;
916 /* free the stream that we acquired before */
918 gst_pulseringbuffer_release (GstRingBuffer * buf)
921 GstPulseRingBuffer *pbuf;
923 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
924 pbuf = GST_PULSERING_BUFFER_CAST (buf);
926 pa_threaded_mainloop_lock (psink->mainloop);
927 gst_pulsering_destroy_stream (pbuf);
928 pa_threaded_mainloop_unlock (psink->mainloop);
934 gst_pulsering_success_cb (pa_stream * s, int success, void *userdata)
936 GstPulseRingBuffer *pbuf;
939 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
940 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
942 pa_threaded_mainloop_signal (psink->mainloop, 0);
945 /* update the corked state of a stream, must be called with the mainloop
948 gst_pulsering_set_corked (GstPulseRingBuffer * pbuf, gboolean corked,
951 pa_operation *o = NULL;
953 GstPulseContext *pctx = NULL;
954 gboolean res = FALSE;
956 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
958 GST_DEBUG_OBJECT (psink, "setting corked state to %d", corked);
959 if (pbuf->corked != corked) {
960 if (!(o = pa_stream_cork (pbuf->stream, corked,
961 gst_pulsering_success_cb, pbuf)))
964 while (wait && pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
965 pa_threaded_mainloop_wait (psink->mainloop);
966 if (gst_pulsering_is_dead (psink, pbuf))
969 pbuf->corked = corked;
971 GST_DEBUG_OBJECT (psink, "skipping, already in requested state");
977 pa_operation_unref (o);
984 GST_DEBUG_OBJECT (psink, "the server is dead");
989 pctx = gst_pulsering_get_context (pbuf);
990 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
991 ("pa_stream_cork() failed: %s",
992 pa_strerror (pa_context_errno (pctx->context))), (NULL));
998 gst_pulseringbuffer_clear (GstRingBuffer * buf)
1000 GstPulseSink *psink;
1001 GstPulseRingBuffer *pbuf;
1002 pa_operation *o = NULL;
1004 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1005 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1007 pa_threaded_mainloop_lock (psink->mainloop);
1008 GST_DEBUG_OBJECT (psink, "clearing");
1010 /* don't wait for the flush to complete */
1011 if ((o = pa_stream_flush (pbuf->stream, NULL, pbuf)))
1012 pa_operation_unref (o);
1014 pa_threaded_mainloop_unlock (psink->mainloop);
1018 mainloop_enter_defer_cb (pa_mainloop_api * api, void *userdata)
1020 GstPulseSink *pulsesink = GST_PULSESINK (userdata);
1021 GstMessage *message;
1024 g_value_init (&val, G_TYPE_POINTER);
1025 g_value_set_pointer (&val, g_thread_self ());
1027 GST_DEBUG_OBJECT (pulsesink, "posting ENTER stream status");
1028 message = gst_message_new_stream_status (GST_OBJECT (pulsesink),
1029 GST_STREAM_STATUS_TYPE_ENTER, GST_ELEMENT (pulsesink));
1030 gst_message_set_stream_status_object (message, &val);
1032 gst_element_post_message (GST_ELEMENT (pulsesink), message);
1034 /* signal the waiter */
1035 pulsesink->pa_defer_ran = TRUE;
1036 pa_threaded_mainloop_signal (pulsesink->mainloop, 0);
1039 /* start/resume playback ASAP, we don't uncork here but in the commit method */
1041 gst_pulseringbuffer_start (GstRingBuffer * buf)
1043 GstPulseSink *psink;
1044 GstPulseRingBuffer *pbuf;
1046 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1047 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1049 pa_threaded_mainloop_lock (psink->mainloop);
1051 GST_DEBUG_OBJECT (psink, "scheduling stream status");
1052 psink->pa_defer_ran = FALSE;
1053 pa_mainloop_api_once (pa_threaded_mainloop_get_api (psink->mainloop),
1054 mainloop_enter_defer_cb, psink);
1056 GST_DEBUG_OBJECT (psink, "starting");
1057 pbuf->paused = FALSE;
1058 gst_pulsering_set_corked (pbuf, FALSE, FALSE);
1059 pa_threaded_mainloop_unlock (psink->mainloop);
1064 /* pause/stop playback ASAP */
1066 gst_pulseringbuffer_pause (GstRingBuffer * buf)
1068 GstPulseSink *psink;
1069 GstPulseRingBuffer *pbuf;
1072 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1073 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1075 pa_threaded_mainloop_lock (psink->mainloop);
1076 GST_DEBUG_OBJECT (psink, "pausing and corking");
1077 /* make sure the commit method stops writing */
1078 pbuf->paused = TRUE;
1079 res = gst_pulsering_set_corked (pbuf, TRUE, FALSE);
1080 if (pbuf->in_commit) {
1081 /* we are waiting in a commit, signal */
1082 GST_DEBUG_OBJECT (psink, "signal commit");
1083 pa_threaded_mainloop_signal (psink->mainloop, 0);
1085 pa_threaded_mainloop_unlock (psink->mainloop);
1091 mainloop_leave_defer_cb (pa_mainloop_api * api, void *userdata)
1093 GstPulseSink *pulsesink = GST_PULSESINK (userdata);
1094 GstMessage *message;
1097 g_value_init (&val, G_TYPE_POINTER);
1098 g_value_set_pointer (&val, g_thread_self ());
1100 GST_DEBUG_OBJECT (pulsesink, "posting LEAVE stream status");
1101 message = gst_message_new_stream_status (GST_OBJECT (pulsesink),
1102 GST_STREAM_STATUS_TYPE_LEAVE, GST_ELEMENT (pulsesink));
1103 gst_message_set_stream_status_object (message, &val);
1104 gst_element_post_message (GST_ELEMENT (pulsesink), message);
1106 pulsesink->pa_defer_ran = TRUE;
1107 pa_threaded_mainloop_signal (pulsesink->mainloop, 0);
1108 gst_object_unref (pulsesink);
1111 /* stop playback, we flush everything. */
1113 gst_pulseringbuffer_stop (GstRingBuffer * buf)
1115 GstPulseSink *psink;
1116 GstPulseRingBuffer *pbuf;
1117 gboolean res = FALSE;
1118 pa_operation *o = NULL;
1120 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1121 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1123 pa_threaded_mainloop_lock (psink->mainloop);
1124 pbuf->paused = TRUE;
1125 res = gst_pulsering_set_corked (pbuf, TRUE, TRUE);
1126 /* Inform anyone waiting in _commit() call that it shall wakeup */
1127 if (pbuf->in_commit) {
1128 GST_DEBUG_OBJECT (psink, "signal commit thread");
1129 pa_threaded_mainloop_signal (psink->mainloop, 0);
1132 if (strcmp (psink->pa_version, "0.9.12")) {
1133 /* then try to flush, it's not fatal when this fails */
1134 GST_DEBUG_OBJECT (psink, "flushing");
1135 if ((o = pa_stream_flush (pbuf->stream, gst_pulsering_success_cb, pbuf))) {
1136 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1137 GST_DEBUG_OBJECT (psink, "wait for completion");
1138 pa_threaded_mainloop_wait (psink->mainloop);
1139 if (gst_pulsering_is_dead (psink, pbuf))
1142 GST_DEBUG_OBJECT (psink, "flush completed");
1149 pa_operation_cancel (o);
1150 pa_operation_unref (o);
1153 GST_DEBUG_OBJECT (psink, "scheduling stream status");
1154 psink->pa_defer_ran = FALSE;
1155 gst_object_ref (psink);
1156 pa_mainloop_api_once (pa_threaded_mainloop_get_api (psink->mainloop),
1157 mainloop_leave_defer_cb, psink);
1159 GST_DEBUG_OBJECT (psink, "waiting for stream status");
1160 pa_threaded_mainloop_unlock (psink->mainloop);
1167 GST_DEBUG_OBJECT (psink, "the server is dead");
1172 /* in_samples >= out_samples, rate > 1.0 */
1173 #define FWD_UP_SAMPLES(s,se,d,de) \
1175 guint8 *sb = s, *db = d; \
1176 while (s <= se && d < de) { \
1177 memcpy (d, s, bps); \
1180 if ((*accum << 1) >= inr) { \
1185 in_samples -= (s - sb)/bps; \
1186 out_samples -= (d - db)/bps; \
1187 GST_DEBUG ("fwd_up end %d/%d",*accum,*toprocess); \
1190 /* out_samples > in_samples, for rates smaller than 1.0 */
1191 #define FWD_DOWN_SAMPLES(s,se,d,de) \
1193 guint8 *sb = s, *db = d; \
1194 while (s <= se && d < de) { \
1195 memcpy (d, s, bps); \
1198 if ((*accum << 1) >= outr) { \
1203 in_samples -= (s - sb)/bps; \
1204 out_samples -= (d - db)/bps; \
1205 GST_DEBUG ("fwd_down end %d/%d",*accum,*toprocess); \
1208 #define REV_UP_SAMPLES(s,se,d,de) \
1210 guint8 *sb = se, *db = d; \
1211 while (s <= se && d < de) { \
1212 memcpy (d, se, bps); \
1215 while (d < de && (*accum << 1) >= inr) { \
1220 in_samples -= (sb - se)/bps; \
1221 out_samples -= (d - db)/bps; \
1222 GST_DEBUG ("rev_up end %d/%d",*accum,*toprocess); \
1225 #define REV_DOWN_SAMPLES(s,se,d,de) \
1227 guint8 *sb = se, *db = d; \
1228 while (s <= se && d < de) { \
1229 memcpy (d, se, bps); \
1232 while (s <= se && (*accum << 1) >= outr) { \
1237 in_samples -= (sb - se)/bps; \
1238 out_samples -= (d - db)/bps; \
1239 GST_DEBUG ("rev_down end %d/%d",*accum,*toprocess); \
1243 /* our custom commit function because we write into the buffer of pulseaudio
1244 * instead of keeping our own buffer */
1246 gst_pulseringbuffer_commit (GstRingBuffer * buf, guint64 * sample,
1247 guchar * data, gint in_samples, gint out_samples, gint * accum)
1249 GstPulseSink *psink;
1250 GstPulseRingBuffer *pbuf;
1251 GstPulseContext *pctx;
1256 gint inr, outr, bps;
1260 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1261 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1263 /* FIXME post message rather than using a signal (as mixer interface) */
1264 if (g_atomic_int_compare_and_exchange (&psink->notify, 1, 0)) {
1265 g_object_notify (G_OBJECT (psink), "volume");
1266 g_object_notify (G_OBJECT (psink), "mute");
1269 /* make sure the ringbuffer is started */
1270 if (G_UNLIKELY (g_atomic_int_get (&buf->state) !=
1271 GST_RING_BUFFER_STATE_STARTED)) {
1272 /* see if we are allowed to start it */
1273 if (G_UNLIKELY (g_atomic_int_get (&buf->abidata.ABI.may_start) == FALSE))
1276 GST_DEBUG_OBJECT (buf, "start!");
1277 if (!gst_ring_buffer_start (buf))
1281 pa_threaded_mainloop_lock (psink->mainloop);
1282 GST_DEBUG_OBJECT (psink, "entering commit");
1283 pbuf->in_commit = TRUE;
1285 bps = buf->spec.bytes_per_sample;
1286 bufsize = buf->spec.segsize * buf->spec.segtotal;
1288 /* our toy resampler for trick modes */
1289 reverse = out_samples < 0;
1290 out_samples = ABS (out_samples);
1292 if (in_samples >= out_samples)
1293 toprocess = &in_samples;
1295 toprocess = &out_samples;
1297 inr = in_samples - 1;
1298 outr = out_samples - 1;
1300 GST_DEBUG_OBJECT (psink, "in %d, out %d", inr, outr);
1302 /* data_end points to the last sample we have to write, not past it. This is
1303 * needed to properly handle reverse playback: it points to the last sample. */
1304 data_end = data + (bps * inr);
1309 /* offset is in bytes */
1310 offset = *sample * bps;
1312 while (*toprocess > 0) {
1316 GST_LOG_OBJECT (psink,
1317 "need to write %d samples at offset %" G_GINT64_FORMAT, *toprocess,
1320 #ifdef HAVE_PULSE_0_9_16
1321 if (offset != pbuf->m_lastoffset)
1322 GST_LOG_OBJECT (psink, "discontinuity, offset is %" G_GINT64_FORMAT ", "
1323 "last offset was %" G_GINT64_FORMAT, offset, pbuf->m_lastoffset);
1325 towrite = out_samples * bps;
1327 /* Only ever write segsize bytes at once. This will
1328 * also limit the PA shm buffer to segsize
1330 if (towrite > buf->spec.segsize)
1331 towrite = buf->spec.segsize;
1333 if ((pbuf->m_writable < towrite) || (offset != pbuf->m_lastoffset)) {
1334 /* if no room left or discontinuity in offset,
1335 we need to flush data and get a new buffer */
1337 /* flush the buffer if possible */
1338 if ((pbuf->m_data != NULL) && (pbuf->m_towrite > 0)) {
1340 GST_LOG_OBJECT (psink,
1341 "flushing %u samples at offset %" G_GINT64_FORMAT,
1342 (guint) pbuf->m_towrite / bps, pbuf->m_offset);
1344 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1345 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1349 pbuf->m_towrite = 0;
1350 pbuf->m_offset = offset; /* keep track of current offset */
1352 /* get a buffer to write in for now on */
1354 pbuf->m_writable = pa_stream_writable_size (pbuf->stream);
1356 if (pbuf->m_writable == (size_t) - 1)
1357 goto writable_size_failed;
1359 pbuf->m_writable /= bps;
1360 pbuf->m_writable *= bps; /* handle only complete samples */
1362 if (pbuf->m_writable >= towrite)
1365 /* see if we need to uncork because we have no free space */
1367 if (!gst_pulsering_set_corked (pbuf, FALSE, FALSE))
1371 /* we can't write a single byte, wait a bit */
1372 GST_LOG_OBJECT (psink, "waiting for free space");
1373 pa_threaded_mainloop_wait (psink->mainloop);
1379 /* make sure we only buffer up latency-time samples */
1380 if (pbuf->m_writable > buf->spec.segsize) {
1381 /* limit buffering to latency-time value */
1382 pbuf->m_writable = buf->spec.segsize;
1384 GST_LOG_OBJECT (psink, "Limiting buffering to %" G_GSIZE_FORMAT,
1388 GST_LOG_OBJECT (psink, "requesting %" G_GSIZE_FORMAT " bytes of "
1389 "shared memory", pbuf->m_writable);
1391 if (pa_stream_begin_write (pbuf->stream, &pbuf->m_data,
1392 &pbuf->m_writable) < 0) {
1393 GST_LOG_OBJECT (psink, "pa_stream_begin_write() failed");
1394 goto writable_size_failed;
1397 GST_LOG_OBJECT (psink, "got %" G_GSIZE_FORMAT " bytes of shared memory",
1400 /* Just to make sure that we didn't get more than requested */
1401 if (pbuf->m_writable > buf->spec.segsize) {
1402 /* limit buffering to latency-time value */
1403 pbuf->m_writable = buf->spec.segsize;
1407 if (pbuf->m_writable < towrite)
1408 towrite = pbuf->m_writable;
1409 avail = towrite / bps;
1411 GST_LOG_OBJECT (psink, "writing %u samples at offset %" G_GUINT64_FORMAT,
1412 (guint) avail, offset);
1414 if (G_LIKELY (inr == outr && !reverse)) {
1415 /* no rate conversion, simply write out the samples */
1416 /* copy the data into internal buffer */
1418 memcpy ((guint8 *) pbuf->m_data + pbuf->m_towrite, data, towrite);
1419 pbuf->m_towrite += towrite;
1420 pbuf->m_writable -= towrite;
1423 in_samples -= avail;
1424 out_samples -= avail;
1426 guint8 *dest, *d, *d_end;
1428 /* write into the PulseAudio shm buffer */
1429 dest = d = (guint8 *) pbuf->m_data + pbuf->m_towrite;
1430 d_end = d + towrite;
1434 /* forward speed up */
1435 FWD_UP_SAMPLES (data, data_end, d, d_end);
1437 /* forward slow down */
1438 FWD_DOWN_SAMPLES (data, data_end, d, d_end);
1441 /* reverse speed up */
1442 REV_UP_SAMPLES (data, data_end, d, d_end);
1444 /* reverse slow down */
1445 REV_DOWN_SAMPLES (data, data_end, d, d_end);
1447 /* see what we have left to write */
1448 towrite = (d - dest);
1449 pbuf->m_towrite += towrite;
1450 pbuf->m_writable -= towrite;
1452 avail = towrite / bps;
1455 /* flush the buffer if it's full */
1456 if ((pbuf->m_data != NULL) && (pbuf->m_towrite > 0)
1457 && (pbuf->m_writable == 0)) {
1458 GST_LOG_OBJECT (psink, "flushing %u samples at offset %" G_GINT64_FORMAT,
1459 (guint) pbuf->m_towrite / bps, pbuf->m_offset);
1461 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1462 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1465 pbuf->m_towrite = 0;
1466 pbuf->m_offset = offset + towrite; /* keep track of current offset */
1471 /* FIXME, this is not quite right */
1472 if ((avail = pa_stream_writable_size (pbuf->stream)) == (size_t) - 1)
1473 goto writable_size_failed;
1475 /* We always try to satisfy a request for data */
1476 GST_LOG_OBJECT (psink, "writable bytes %" G_GSIZE_FORMAT, avail);
1478 /* convert to samples, we can only deal with multiples of the
1485 /* see if we need to uncork because we have no free space */
1487 if (!gst_pulsering_set_corked (pbuf, FALSE, FALSE))
1491 /* we can't write a single byte, wait a bit */
1492 GST_LOG_OBJECT (psink, "waiting for free space");
1493 pa_threaded_mainloop_wait (psink->mainloop);
1499 if (avail > out_samples)
1500 avail = out_samples;
1502 towrite = avail * bps;
1504 GST_LOG_OBJECT (psink, "writing %u samples at offset %" G_GUINT64_FORMAT,
1505 (guint) avail, offset);
1507 if (G_LIKELY (inr == outr && !reverse)) {
1508 /* no rate conversion, simply write out the samples */
1509 if (pa_stream_write (pbuf->stream, data, towrite, NULL, offset,
1510 PA_SEEK_ABSOLUTE) < 0)
1514 in_samples -= avail;
1515 out_samples -= avail;
1517 guint8 *dest, *d, *d_end;
1519 /* we need to allocate a temporary buffer to resample the data into,
1520 * FIXME, we should have a pulseaudio API to allocate this buffer for us
1521 * from the shared memory. */
1522 dest = d = g_malloc (towrite);
1523 d_end = d + towrite;
1527 /* forward speed up */
1528 FWD_UP_SAMPLES (data, data_end, d, d_end);
1530 /* forward slow down */
1531 FWD_DOWN_SAMPLES (data, data_end, d, d_end);
1534 /* reverse speed up */
1535 REV_UP_SAMPLES (data, data_end, d, d_end);
1537 /* reverse slow down */
1538 REV_DOWN_SAMPLES (data, data_end, d, d_end);
1540 /* see what we have left to write */
1541 towrite = (d - dest);
1542 if (pa_stream_write (pbuf->stream, dest, towrite,
1543 g_free, offset, PA_SEEK_ABSOLUTE) < 0)
1546 avail = towrite / bps;
1548 #endif /* HAVE_PULSE_0_9_16 */
1551 offset += avail * bps;
1553 #ifdef HAVE_PULSE_0_9_16
1554 pbuf->m_lastoffset = offset;
1557 /* check if we need to uncork after writing the samples */
1559 const pa_timing_info *info;
1561 if ((info = pa_stream_get_timing_info (pbuf->stream))) {
1562 GST_LOG_OBJECT (psink,
1563 "read_index at %" G_GUINT64_FORMAT ", offset %" G_GINT64_FORMAT,
1564 info->read_index, offset);
1566 /* we uncork when the read_index is too far behind the offset we need
1568 if (info->read_index + bufsize <= offset) {
1569 if (!gst_pulsering_set_corked (pbuf, FALSE, FALSE))
1573 GST_LOG_OBJECT (psink, "no timing info available yet");
1577 /* we consumed all samples here */
1578 data = data_end + bps;
1580 pbuf->in_commit = FALSE;
1581 pa_threaded_mainloop_unlock (psink->mainloop);
1584 result = inr - ((data_end - data) / bps);
1585 GST_LOG_OBJECT (psink, "wrote %d samples", result);
1592 pbuf->in_commit = FALSE;
1593 GST_LOG_OBJECT (psink, "we are reset");
1594 pa_threaded_mainloop_unlock (psink->mainloop);
1599 GST_LOG_OBJECT (psink, "we can not start");
1604 GST_LOG_OBJECT (psink, "failed to start the ringbuffer");
1609 pbuf->in_commit = FALSE;
1610 GST_ERROR_OBJECT (psink, "uncork failed");
1611 pa_threaded_mainloop_unlock (psink->mainloop);
1616 pbuf->in_commit = FALSE;
1617 GST_LOG_OBJECT (psink, "we are paused");
1618 pa_threaded_mainloop_unlock (psink->mainloop);
1621 writable_size_failed:
1623 pctx = gst_pulsering_get_context (pbuf);
1625 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1626 ("pa_stream_writable_size() failed: %s",
1627 pa_strerror (pa_context_errno (pctx->context))), (NULL));
1628 goto unlock_and_fail;
1632 pctx = gst_pulsering_get_context (pbuf);
1634 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1635 ("pa_stream_write() failed: %s",
1636 pa_strerror (pa_context_errno (pctx->context))), (NULL));
1637 goto unlock_and_fail;
1641 static void gst_pulsesink_set_property (GObject * object, guint prop_id,
1642 const GValue * value, GParamSpec * pspec);
1643 static void gst_pulsesink_get_property (GObject * object, guint prop_id,
1644 GValue * value, GParamSpec * pspec);
1645 static void gst_pulsesink_finalize (GObject * object);
1647 static gboolean gst_pulsesink_event (GstBaseSink * sink, GstEvent * event);
1649 static GstStateChangeReturn gst_pulsesink_change_state (GstElement * element,
1650 GstStateChange transition);
1652 static void gst_pulsesink_init_interfaces (GType type);
1654 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
1655 # define ENDIANNESS "LITTLE_ENDIAN, BIG_ENDIAN"
1657 # define ENDIANNESS "BIG_ENDIAN, LITTLE_ENDIAN"
1660 GST_IMPLEMENT_PULSEPROBE_METHODS (GstPulseSink, gst_pulsesink);
1661 GST_BOILERPLATE_FULL (GstPulseSink, gst_pulsesink, GstBaseAudioSink,
1662 GST_TYPE_BASE_AUDIO_SINK, gst_pulsesink_init_interfaces);
1665 gst_pulsesink_interface_supported (GstImplementsInterface *
1666 iface, GType interface_type)
1668 GstPulseSink *this = GST_PULSESINK_CAST (iface);
1670 if (interface_type == GST_TYPE_PROPERTY_PROBE && this->probe)
1672 if (interface_type == GST_TYPE_STREAM_VOLUME)
1679 gst_pulsesink_implements_interface_init (GstImplementsInterfaceClass * klass)
1681 klass->supported = gst_pulsesink_interface_supported;
1685 gst_pulsesink_init_interfaces (GType type)
1687 static const GInterfaceInfo implements_iface_info = {
1688 (GInterfaceInitFunc) gst_pulsesink_implements_interface_init,
1692 static const GInterfaceInfo probe_iface_info = {
1693 (GInterfaceInitFunc) gst_pulsesink_property_probe_interface_init,
1697 #ifdef HAVE_PULSE_0_9_12
1698 static const GInterfaceInfo svol_iface_info = {
1702 g_type_add_interface_static (type, GST_TYPE_STREAM_VOLUME, &svol_iface_info);
1705 g_type_add_interface_static (type, GST_TYPE_IMPLEMENTS_INTERFACE,
1706 &implements_iface_info);
1707 g_type_add_interface_static (type, GST_TYPE_PROPERTY_PROBE,
1712 gst_pulsesink_base_init (gpointer g_class)
1714 static GstStaticPadTemplate pad_template = GST_STATIC_PAD_TEMPLATE ("sink",
1717 GST_STATIC_CAPS ("audio/x-raw-int, "
1718 "endianness = (int) { " ENDIANNESS " }, "
1719 "signed = (boolean) TRUE, "
1720 "width = (int) 16, "
1721 "depth = (int) 16, "
1722 "rate = (int) [ 1, MAX ], "
1723 "channels = (int) [ 1, 32 ];"
1724 "audio/x-raw-float, "
1725 "endianness = (int) { " ENDIANNESS " }, "
1726 "width = (int) 32, "
1727 "rate = (int) [ 1, MAX ], "
1728 "channels = (int) [ 1, 32 ];"
1730 "endianness = (int) { " ENDIANNESS " }, "
1731 "signed = (boolean) TRUE, "
1732 "width = (int) 32, "
1733 "depth = (int) 32, "
1734 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 32 ];"
1735 #ifdef HAVE_PULSE_0_9_15
1737 "endianness = (int) { " ENDIANNESS " }, "
1738 "signed = (boolean) TRUE, "
1739 "width = (int) 24, "
1740 "depth = (int) 24, "
1741 "rate = (int) [ 1, MAX ], "
1742 "channels = (int) [ 1, 32 ];"
1744 "endianness = (int) { " ENDIANNESS " }, "
1745 "signed = (boolean) TRUE, "
1746 "width = (int) 32, "
1747 "depth = (int) 24, "
1748 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 32 ];"
1751 "signed = (boolean) FALSE, "
1754 "rate = (int) [ 1, MAX ], "
1755 "channels = (int) [ 1, 32 ];"
1757 "rate = (int) [ 1, MAX], "
1758 "channels = (int) [ 1, 32 ];"
1760 "rate = (int) [ 1, MAX], " "channels = (int) [ 1, 32 ]")
1763 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
1765 gst_element_class_set_details_simple (element_class,
1766 "PulseAudio Audio Sink",
1767 "Sink/Audio", "Plays audio to a PulseAudio server", "Lennart Poettering");
1768 gst_element_class_add_pad_template (element_class,
1769 gst_static_pad_template_get (&pad_template));
1772 static GstRingBuffer *
1773 gst_pulsesink_create_ringbuffer (GstBaseAudioSink * sink)
1775 GstRingBuffer *buffer;
1777 GST_DEBUG_OBJECT (sink, "creating ringbuffer");
1778 buffer = g_object_new (GST_TYPE_PULSERING_BUFFER, NULL);
1779 GST_DEBUG_OBJECT (sink, "created ringbuffer @%p", buffer);
1785 gst_pulsesink_class_init (GstPulseSinkClass * klass)
1787 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1788 GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
1789 GstBaseSinkClass *bc;
1790 GstBaseAudioSinkClass *gstaudiosink_class = GST_BASE_AUDIO_SINK_CLASS (klass);
1791 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1793 gobject_class->finalize = gst_pulsesink_finalize;
1794 gobject_class->set_property = gst_pulsesink_set_property;
1795 gobject_class->get_property = gst_pulsesink_get_property;
1797 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_pulsesink_event);
1799 /* restore the original basesink pull methods */
1800 bc = g_type_class_peek (GST_TYPE_BASE_SINK);
1801 gstbasesink_class->activate_pull = GST_DEBUG_FUNCPTR (bc->activate_pull);
1803 gstelement_class->change_state =
1804 GST_DEBUG_FUNCPTR (gst_pulsesink_change_state);
1806 gst_pulseringbuffer_init_contexts ();
1808 gstaudiosink_class->create_ringbuffer =
1809 GST_DEBUG_FUNCPTR (gst_pulsesink_create_ringbuffer);
1811 /* Overwrite GObject fields */
1812 g_object_class_install_property (gobject_class,
1814 g_param_spec_string ("server", "Server",
1815 "The PulseAudio server to connect to", DEFAULT_SERVER,
1816 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1818 g_object_class_install_property (gobject_class, PROP_DEVICE,
1819 g_param_spec_string ("device", "Device",
1820 "The PulseAudio sink device to connect to", DEFAULT_DEVICE,
1821 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1823 g_object_class_install_property (gobject_class,
1825 g_param_spec_string ("device-name", "Device name",
1826 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
1827 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1829 #ifdef HAVE_PULSE_0_9_12
1830 g_object_class_install_property (gobject_class,
1832 g_param_spec_double ("volume", "Volume",
1833 "Linear volume of this stream, 1.0=100%", 0.0, MAX_VOLUME,
1834 DEFAULT_VOLUME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1835 g_object_class_install_property (gobject_class,
1837 g_param_spec_boolean ("mute", "Mute",
1838 "Mute state of this stream", DEFAULT_MUTE,
1839 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1842 g_object_class_install_property (gobject_class,
1844 g_param_spec_string ("client", "Client",
1845 "The PulseAudio client name to use", gst_pulse_client_name (),
1846 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1847 GST_PARAM_MUTABLE_READY));
1850 /* returns the current time of the sink ringbuffer */
1852 gst_pulsesink_get_time (GstClock * clock, GstBaseAudioSink * sink)
1854 GstPulseSink *psink;
1855 GstPulseRingBuffer *pbuf;
1858 if (!sink->ringbuffer || !sink->ringbuffer->acquired)
1859 return GST_CLOCK_TIME_NONE;
1861 pbuf = GST_PULSERING_BUFFER_CAST (sink->ringbuffer);
1862 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1864 pa_threaded_mainloop_lock (psink->mainloop);
1865 if (gst_pulsering_is_dead (psink, pbuf))
1868 /* if we don't have enough data to get a timestamp, just return NONE, which
1869 * will return the last reported time */
1870 if (pa_stream_get_time (pbuf->stream, &time) < 0) {
1871 GST_DEBUG_OBJECT (psink, "could not get time");
1872 time = GST_CLOCK_TIME_NONE;
1875 pa_threaded_mainloop_unlock (psink->mainloop);
1877 GST_LOG_OBJECT (psink, "current time is %" GST_TIME_FORMAT,
1878 GST_TIME_ARGS (time));
1885 GST_DEBUG_OBJECT (psink, "the server is dead");
1886 pa_threaded_mainloop_unlock (psink->mainloop);
1888 return GST_CLOCK_TIME_NONE;
1893 gst_pulsesink_init (GstPulseSink * pulsesink, GstPulseSinkClass * klass)
1895 pulsesink->server = NULL;
1896 pulsesink->device = NULL;
1897 pulsesink->device_description = NULL;
1898 pulsesink->client_name = gst_pulse_client_name ();
1900 pulsesink->volume = DEFAULT_VOLUME;
1901 pulsesink->volume_set = FALSE;
1903 pulsesink->mute = DEFAULT_MUTE;
1904 pulsesink->mute_set = FALSE;
1906 pulsesink->notify = 0;
1908 /* needed for conditional execution */
1909 pulsesink->pa_version = pa_get_library_version ();
1911 GST_DEBUG_OBJECT (pulsesink, "using pulseaudio version %s",
1912 pulsesink->pa_version);
1914 /* TRUE for sinks, FALSE for sources */
1915 pulsesink->probe = gst_pulseprobe_new (G_OBJECT (pulsesink),
1916 G_OBJECT_GET_CLASS (pulsesink), PROP_DEVICE, pulsesink->device,
1921 gst_pulsesink_finalize (GObject * object)
1923 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
1925 g_free (pulsesink->server);
1926 g_free (pulsesink->device);
1927 g_free (pulsesink->device_description);
1928 g_free (pulsesink->client_name);
1930 if (pulsesink->probe) {
1931 gst_pulseprobe_free (pulsesink->probe);
1932 pulsesink->probe = NULL;
1935 G_OBJECT_CLASS (parent_class)->finalize (object);
1938 #ifdef HAVE_PULSE_0_9_12
1940 gst_pulsesink_set_volume (GstPulseSink * psink, gdouble volume)
1943 pa_operation *o = NULL;
1944 GstPulseRingBuffer *pbuf;
1945 GstPulseContext *pctx;
1948 if (!psink->mainloop)
1951 pa_threaded_mainloop_lock (psink->mainloop);
1953 GST_DEBUG_OBJECT (psink, "setting volume to %f", volume);
1955 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
1956 if (pbuf == NULL || pbuf->stream == NULL)
1959 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
1962 gst_pulse_cvolume_from_linear (&v, pbuf->sample_spec.channels, volume);
1964 pctx = gst_pulsering_get_context (pbuf);
1966 if (!(o = pa_context_set_sink_input_volume (pctx->context, idx,
1970 /* We don't really care about the result of this call */
1974 pa_operation_unref (o);
1976 pa_threaded_mainloop_unlock (psink->mainloop);
1983 psink->volume = volume;
1984 psink->volume_set = TRUE;
1986 GST_DEBUG_OBJECT (psink, "we have no mainloop");
1991 psink->volume = volume;
1992 psink->volume_set = TRUE;
1994 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
1999 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2004 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2005 ("pa_stream_set_sink_input_volume() failed: %s",
2006 pa_strerror (pa_context_errno (pctx->context))), (NULL));
2012 gst_pulsesink_set_mute (GstPulseSink * psink, gboolean mute)
2014 pa_operation *o = NULL;
2015 GstPulseRingBuffer *pbuf;
2016 GstPulseContext *pctx;
2019 if (!psink->mainloop)
2022 pa_threaded_mainloop_lock (psink->mainloop);
2024 GST_DEBUG_OBJECT (psink, "setting mute state to %d", mute);
2026 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2027 if (pbuf == NULL || pbuf->stream == NULL)
2030 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2033 pctx = gst_pulsering_get_context (pbuf);
2035 if (!(o = pa_context_set_sink_input_mute (pctx->context, idx,
2039 /* We don't really care about the result of this call */
2043 pa_operation_unref (o);
2045 pa_threaded_mainloop_unlock (psink->mainloop);
2053 psink->mute_set = TRUE;
2055 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2061 psink->mute_set = TRUE;
2063 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2068 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2073 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2074 ("pa_stream_set_sink_input_mute() failed: %s",
2075 pa_strerror (pa_context_errno (pctx->context))), (NULL));
2081 gst_pulsesink_sink_input_info_cb (pa_context * c, const pa_sink_input_info * i,
2082 int eol, void *userdata)
2084 GstPulseRingBuffer *pbuf;
2085 GstPulseSink *psink;
2087 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
2088 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
2096 /* If the index doesn't match our current stream,
2097 * it implies we just recreated the stream (caps change)
2099 if (i->index == pa_stream_get_index (pbuf->stream)) {
2100 psink->volume = pa_sw_volume_to_linear (pa_cvolume_max (&i->volume));
2101 psink->mute = i->mute;
2105 pa_threaded_mainloop_signal (psink->mainloop, 0);
2109 gst_pulsesink_get_volume (GstPulseSink * psink)
2111 GstPulseRingBuffer *pbuf;
2112 GstPulseContext *pctx;
2113 pa_operation *o = NULL;
2114 gdouble v = DEFAULT_VOLUME;
2117 if (!psink->mainloop)
2120 pa_threaded_mainloop_lock (psink->mainloop);
2122 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2123 if (pbuf == NULL || pbuf->stream == NULL)
2126 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2129 pctx = gst_pulsering_get_context (pbuf);
2131 if (!(o = pa_context_get_sink_input_info (pctx->context, idx,
2132 gst_pulsesink_sink_input_info_cb, pbuf)))
2135 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2136 pa_threaded_mainloop_wait (psink->mainloop);
2137 if (gst_pulsering_is_dead (psink, pbuf))
2145 pa_operation_unref (o);
2147 pa_threaded_mainloop_unlock (psink->mainloop);
2149 if (v > MAX_VOLUME) {
2150 GST_WARNING_OBJECT (psink, "Clipped volume from %f to %f", v, MAX_VOLUME);
2160 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2165 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2170 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2175 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2176 ("pa_context_get_sink_input_info() failed: %s",
2177 pa_strerror (pa_context_errno (pctx->context))), (NULL));
2183 gst_pulsesink_get_mute (GstPulseSink * psink)
2185 GstPulseRingBuffer *pbuf;
2186 GstPulseContext *pctx;
2187 pa_operation *o = NULL;
2189 gboolean mute = FALSE;
2191 if (!psink->mainloop)
2194 pa_threaded_mainloop_lock (psink->mainloop);
2197 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2198 if (pbuf == NULL || pbuf->stream == NULL)
2201 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2204 pctx = gst_pulsering_get_context (pbuf);
2206 if (!(o = pa_context_get_sink_input_info (pctx->context, idx,
2207 gst_pulsesink_sink_input_info_cb, pbuf)))
2210 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2211 pa_threaded_mainloop_wait (psink->mainloop);
2212 if (gst_pulsering_is_dead (psink, pbuf))
2219 pa_operation_unref (o);
2221 pa_threaded_mainloop_unlock (psink->mainloop);
2229 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2234 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2239 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2244 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2245 ("pa_context_get_sink_input_info() failed: %s",
2246 pa_strerror (pa_context_errno (pctx->context))), (NULL));
2253 gst_pulsesink_sink_info_cb (pa_context * c, const pa_sink_info * i, int eol,
2256 GstPulseRingBuffer *pbuf;
2257 GstPulseSink *psink;
2259 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
2260 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
2268 g_assert (i->index == pa_stream_get_device_index (pbuf->stream));
2270 g_free (psink->device_description);
2271 psink->device_description = g_strdup (i->description);
2274 pa_threaded_mainloop_signal (psink->mainloop, 0);
2278 gst_pulsesink_device_description (GstPulseSink * psink)
2280 GstPulseRingBuffer *pbuf;
2281 GstPulseContext *pctx;
2282 pa_operation *o = NULL;
2285 if (!psink->mainloop)
2288 pa_threaded_mainloop_lock (psink->mainloop);
2289 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2290 if (pbuf == NULL || pbuf->stream == NULL)
2293 pctx = gst_pulsering_get_context (pbuf);
2295 if (!(o = pa_context_get_sink_info_by_index (pctx->context,
2296 pa_stream_get_device_index (pbuf->stream),
2297 gst_pulsesink_sink_info_cb, pbuf)))
2300 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2301 pa_threaded_mainloop_wait (psink->mainloop);
2302 if (gst_pulsering_is_dead (psink, pbuf))
2309 pa_operation_unref (o);
2311 t = g_strdup (psink->device_description);
2312 pa_threaded_mainloop_unlock (psink->mainloop);
2319 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2324 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2329 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2330 ("pa_context_get_sink_info_by_index() failed: %s",
2331 pa_strerror (pa_context_errno (pctx->context))), (NULL));
2337 gst_pulsesink_set_property (GObject * object,
2338 guint prop_id, const GValue * value, GParamSpec * pspec)
2340 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2344 g_free (pulsesink->server);
2345 pulsesink->server = g_value_dup_string (value);
2346 if (pulsesink->probe)
2347 gst_pulseprobe_set_server (pulsesink->probe, pulsesink->server);
2350 g_free (pulsesink->device);
2351 pulsesink->device = g_value_dup_string (value);
2353 #ifdef HAVE_PULSE_0_9_12
2355 gst_pulsesink_set_volume (pulsesink, g_value_get_double (value));
2358 gst_pulsesink_set_mute (pulsesink, g_value_get_boolean (value));
2362 g_free (pulsesink->client_name);
2363 if (!g_value_get_string (value)) {
2364 GST_WARNING_OBJECT (pulsesink,
2365 "Empty PulseAudio client name not allowed. Resetting to default value");
2366 pulsesink->client_name = gst_pulse_client_name ();
2368 pulsesink->client_name = g_value_dup_string (value);
2371 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2377 gst_pulsesink_get_property (GObject * object,
2378 guint prop_id, GValue * value, GParamSpec * pspec)
2381 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2385 g_value_set_string (value, pulsesink->server);
2388 g_value_set_string (value, pulsesink->device);
2390 case PROP_DEVICE_NAME:
2391 g_value_take_string (value, gst_pulsesink_device_description (pulsesink));
2393 #ifdef HAVE_PULSE_0_9_12
2395 g_value_set_double (value, gst_pulsesink_get_volume (pulsesink));
2398 g_value_set_boolean (value, gst_pulsesink_get_mute (pulsesink));
2402 g_value_set_string (value, pulsesink->client_name);
2405 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2411 gst_pulsesink_change_title (GstPulseSink * psink, const gchar * t)
2413 pa_operation *o = NULL;
2414 GstPulseRingBuffer *pbuf;
2415 GstPulseContext *pctx;
2417 pa_threaded_mainloop_lock (psink->mainloop);
2419 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2421 if (pbuf == NULL || pbuf->stream == NULL)
2424 g_free (pbuf->stream_name);
2425 pbuf->stream_name = g_strdup (t);
2427 pctx = gst_pulsering_get_context (pbuf);
2429 if (!(o = pa_stream_set_name (pbuf->stream, pbuf->stream_name, NULL, NULL)))
2432 /* We're not interested if this operation failed or not */
2436 pa_operation_unref (o);
2437 pa_threaded_mainloop_unlock (psink->mainloop);
2444 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2449 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2450 ("pa_stream_set_name() failed: %s",
2451 pa_strerror (pa_context_errno (pctx->context))), (NULL));
2456 #ifdef HAVE_PULSE_0_9_11
2458 gst_pulsesink_change_props (GstPulseSink * psink, GstTagList * l)
2460 static const gchar *const map[] = {
2461 GST_TAG_TITLE, PA_PROP_MEDIA_TITLE,
2463 /* might get overriden in the next iteration by GST_TAG_ARTIST */
2464 GST_TAG_PERFORMER, PA_PROP_MEDIA_ARTIST,
2466 GST_TAG_ARTIST, PA_PROP_MEDIA_ARTIST,
2467 GST_TAG_LANGUAGE_CODE, PA_PROP_MEDIA_LANGUAGE,
2468 GST_TAG_LOCATION, PA_PROP_MEDIA_FILENAME,
2469 /* We might add more here later on ... */
2472 pa_proplist *pl = NULL;
2473 const gchar *const *t;
2474 gboolean empty = TRUE;
2475 pa_operation *o = NULL;
2476 GstPulseRingBuffer *pbuf;
2477 GstPulseContext *pctx;
2479 pl = pa_proplist_new ();
2481 for (t = map; *t; t += 2) {
2484 if (gst_tag_list_get_string (l, *t, &n)) {
2487 pa_proplist_sets (pl, *(t + 1), n);
2497 pa_threaded_mainloop_lock (psink->mainloop);
2499 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2500 if (pbuf == NULL || pbuf->stream == NULL)
2503 pctx = gst_pulsering_get_context (pbuf);
2505 if (!(o = pa_stream_proplist_update (pbuf->stream, PA_UPDATE_REPLACE,
2509 /* We're not interested if this operation failed or not */
2513 pa_operation_unref (o);
2515 pa_threaded_mainloop_unlock (psink->mainloop);
2520 pa_proplist_free (pl);
2527 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2532 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2533 ("pa_stream_proplist_update() failed: %s",
2534 pa_strerror (pa_context_errno (pctx->context))), (NULL));
2541 gst_pulsesink_event (GstBaseSink * sink, GstEvent * event)
2543 GstPulseSink *pulsesink = GST_PULSESINK_CAST (sink);
2545 switch (GST_EVENT_TYPE (event)) {
2546 case GST_EVENT_TAG:{
2547 gchar *title = NULL, *artist = NULL, *location = NULL, *description =
2548 NULL, *t = NULL, *buf = NULL;
2551 gst_event_parse_tag (event, &l);
2553 gst_tag_list_get_string (l, GST_TAG_TITLE, &title);
2554 gst_tag_list_get_string (l, GST_TAG_ARTIST, &artist);
2555 gst_tag_list_get_string (l, GST_TAG_LOCATION, &location);
2556 gst_tag_list_get_string (l, GST_TAG_DESCRIPTION, &description);
2559 gst_tag_list_get_string (l, GST_TAG_PERFORMER, &artist);
2561 if (title && artist)
2562 /* TRANSLATORS: 'song title' by 'artist name' */
2563 t = buf = g_strdup_printf (_("'%s' by '%s'"), g_strstrip (title),
2564 g_strstrip (artist));
2566 t = g_strstrip (title);
2567 else if (description)
2568 t = g_strstrip (description);
2570 t = g_strstrip (location);
2573 gst_pulsesink_change_title (pulsesink, t);
2578 g_free (description);
2581 #ifdef HAVE_PULSE_0_9_11
2582 gst_pulsesink_change_props (pulsesink, l);
2591 return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
2594 static GstStateChangeReturn
2595 gst_pulsesink_change_state (GstElement * element, GstStateChange transition)
2597 GstPulseSink *pulsesink = GST_PULSESINK (element);
2598 GstStateChangeReturn ret;
2601 switch (transition) {
2602 case GST_STATE_CHANGE_NULL_TO_READY:
2603 g_assert (pulsesink->mainloop == NULL);
2604 pulsesink->mainloop = pa_threaded_mainloop_new ();
2605 g_assert (pulsesink->mainloop != NULL);
2606 res = pa_threaded_mainloop_start (pulsesink->mainloop);
2607 g_assert (res == 0);
2609 /* override with a custom clock */
2610 if (GST_BASE_AUDIO_SINK (pulsesink)->provided_clock)
2611 gst_object_unref (GST_BASE_AUDIO_SINK (pulsesink)->provided_clock);
2613 /* FIXME: get rid once we can depend on core/base git again (>= 0.10.30.1)
2614 * (and the pbutils include above as well) */
2615 #if defined(GST_PLUGINS_BASE_VERSION_MAJOR)
2616 GST_BASE_AUDIO_SINK (pulsesink)->provided_clock =
2617 gst_audio_clock_new_full ("GstPulseSinkClock",
2618 (GstAudioClockGetTimeFunc) gst_pulsesink_get_time,
2619 gst_object_ref (pulsesink), (GDestroyNotify) gst_object_unref);
2621 GST_BASE_AUDIO_SINK (pulsesink)->provided_clock =
2622 gst_audio_clock_new ("GstPulseSinkClock",
2623 (GstAudioClockGetTimeFunc) gst_pulsesink_get_time, pulsesink);
2626 case GST_STATE_CHANGE_READY_TO_PAUSED:
2627 gst_element_post_message (element,
2628 gst_message_new_clock_provide (GST_OBJECT_CAST (element),
2629 GST_BASE_AUDIO_SINK (pulsesink)->provided_clock, TRUE));
2635 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2637 /* Clear the PA mainloop if baseaudiosink failed to open the ring_buffer */
2638 if (ret == GST_STATE_CHANGE_FAILURE
2639 && transition == GST_STATE_CHANGE_NULL_TO_READY) {
2640 if (GST_BASE_AUDIO_SINK (pulsesink)->provided_clock)
2641 gst_object_unref (GST_BASE_AUDIO_SINK (pulsesink)->provided_clock);
2642 GST_BASE_AUDIO_SINK (pulsesink)->provided_clock = NULL;
2644 g_assert (pulsesink->mainloop);
2645 pa_threaded_mainloop_stop (pulsesink->mainloop);
2646 pa_threaded_mainloop_free (pulsesink->mainloop);
2647 pulsesink->mainloop = NULL;
2650 switch (transition) {
2651 case GST_STATE_CHANGE_PAUSED_TO_READY:
2652 gst_element_post_message (element,
2653 gst_message_new_clock_lost (GST_OBJECT_CAST (element),
2654 GST_BASE_AUDIO_SINK (pulsesink)->provided_clock));
2656 case GST_STATE_CHANGE_READY_TO_NULL:
2657 if (GST_BASE_AUDIO_SINK (pulsesink)->provided_clock)
2658 gst_object_unref (GST_BASE_AUDIO_SINK (pulsesink)->provided_clock);
2659 GST_BASE_AUDIO_SINK (pulsesink)->provided_clock = NULL;
2660 if (pulsesink->mainloop) {
2661 pa_threaded_mainloop_stop (pulsesink->mainloop);
2662 pa_threaded_mainloop_free (pulsesink->mainloop);
2663 pulsesink->mainloop = NULL;