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>
58 #include <gst/pbutils/pbutils.h> /* only used for GST_PLUGINS_BASE_VERSION_* */
60 #include "pulsesink.h"
61 #include "pulseutil.h"
63 GST_DEBUG_CATEGORY_EXTERN (pulse_debug);
64 #define GST_CAT_DEFAULT pulse_debug
67 * http://www.pulseaudio.org/ticket/314
68 * we need pulse-0.9.12 to use sink volume properties
71 #define DEFAULT_SERVER NULL
72 #define DEFAULT_DEVICE NULL
73 #define DEFAULT_DEVICE_NAME NULL
74 #define DEFAULT_VOLUME 1.0
75 #define DEFAULT_MUTE FALSE
76 #define MAX_VOLUME 10.0
87 PROP_STREAM_PROPERTIES,
91 #define GST_TYPE_PULSERING_BUFFER \
92 (gst_pulseringbuffer_get_type())
93 #define GST_PULSERING_BUFFER(obj) \
94 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_PULSERING_BUFFER,GstPulseRingBuffer))
95 #define GST_PULSERING_BUFFER_CLASS(klass) \
96 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_PULSERING_BUFFER,GstPulseRingBufferClass))
97 #define GST_PULSERING_BUFFER_GET_CLASS(obj) \
98 (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_PULSERING_BUFFER, GstPulseRingBufferClass))
99 #define GST_PULSERING_BUFFER_CAST(obj) \
100 ((GstPulseRingBuffer *)obj)
101 #define GST_IS_PULSERING_BUFFER(obj) \
102 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_PULSERING_BUFFER))
103 #define GST_IS_PULSERING_BUFFER_CLASS(klass)\
104 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_PULSERING_BUFFER))
106 typedef struct _GstPulseRingBuffer GstPulseRingBuffer;
107 typedef struct _GstPulseRingBufferClass GstPulseRingBufferClass;
109 typedef struct _GstPulseContext GstPulseContext;
111 /* Store the PA contexts in a hash table to allow easy sharing among
112 * multiple instances of the sink. Keys are $context_name@$server_name
113 * (strings) and values should be GstPulseContext pointers.
115 struct _GstPulseContext
118 GSList *ring_buffers;
121 static GHashTable *gst_pulse_shared_contexts = NULL;
123 /* use one static main-loop for all instances
124 * this is needed to make the context sharing work as the contexts are
125 * released when releasing their parent main-loop
127 static pa_threaded_mainloop *mainloop = NULL;
128 static guint mainloop_ref_ct = 0;
130 /* lock for access to shared resources */
131 static GMutex *pa_shared_resource_mutex = NULL;
133 /* We keep a custom ringbuffer that is backed up by data allocated by
134 * pulseaudio. We must also overide the commit function to write into
135 * pulseaudio memory instead. */
136 struct _GstPulseRingBuffer
138 GstRingBuffer object;
146 pa_sample_spec sample_spec;
148 #ifdef HAVE_PULSE_0_9_16
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_0_9_13
233 pa_sample_spec_init (&pbuf->sample_spec);
235 pbuf->sample_spec.format = PA_SAMPLE_INVALID;
236 pbuf->sample_spec.rate = 0;
237 pbuf->sample_spec.channels = 0;
240 #ifdef HAVE_PULSE_0_9_16
243 pbuf->m_writable = 0;
245 pbuf->m_lastoffset = 0;
249 pbuf->in_commit = FALSE;
250 pbuf->paused = FALSE;
254 gst_pulsering_destroy_stream (GstPulseRingBuffer * pbuf)
258 #ifdef HAVE_PULSE_0_9_16
260 /* drop shm memory buffer */
261 pa_stream_cancel_write (pbuf->stream);
263 /* reset internal variables */
266 pbuf->m_writable = 0;
268 pbuf->m_lastoffset = 0;
272 pa_stream_disconnect (pbuf->stream);
274 /* Make sure we don't get any further callbacks */
275 pa_stream_set_state_callback (pbuf->stream, NULL, NULL);
276 pa_stream_set_write_callback (pbuf->stream, NULL, NULL);
277 pa_stream_set_underflow_callback (pbuf->stream, NULL, NULL);
278 pa_stream_set_overflow_callback (pbuf->stream, NULL, NULL);
280 pa_stream_unref (pbuf->stream);
284 g_free (pbuf->stream_name);
285 pbuf->stream_name = NULL;
289 gst_pulsering_destroy_context (GstPulseRingBuffer * pbuf)
291 g_mutex_lock (pa_shared_resource_mutex);
293 GST_DEBUG_OBJECT (pbuf, "destroying ringbuffer %p", pbuf);
295 gst_pulsering_destroy_stream (pbuf);
298 pa_context_unref (pbuf->context);
299 pbuf->context = NULL;
302 if (pbuf->context_name) {
303 GstPulseContext *pctx;
305 pctx = g_hash_table_lookup (gst_pulse_shared_contexts, pbuf->context_name);
307 GST_DEBUG_OBJECT (pbuf, "releasing context with name %s, pbuf=%p, pctx=%p",
308 pbuf->context_name, pbuf, pctx);
311 pctx->ring_buffers = g_slist_remove (pctx->ring_buffers, pbuf);
312 if (pctx->ring_buffers == NULL) {
313 GST_DEBUG_OBJECT (pbuf,
314 "destroying final context with name %s, pbuf=%p, pctx=%p",
315 pbuf->context_name, pbuf, pctx);
317 pa_context_disconnect (pctx->context);
319 /* Make sure we don't get any further callbacks */
320 pa_context_set_state_callback (pctx->context, NULL, NULL);
321 #ifdef HAVE_PULSE_0_9_12
322 pa_context_set_subscribe_callback (pctx->context, NULL, NULL);
325 g_hash_table_remove (gst_pulse_shared_contexts, pbuf->context_name);
327 pa_context_unref (pctx->context);
328 g_slice_free (GstPulseContext, pctx);
331 g_free (pbuf->context_name);
332 pbuf->context_name = NULL;
334 g_mutex_unlock (pa_shared_resource_mutex);
338 gst_pulseringbuffer_finalize (GObject * object)
340 GstPulseRingBuffer *ringbuffer;
342 ringbuffer = GST_PULSERING_BUFFER_CAST (object);
344 gst_pulsering_destroy_context (ringbuffer);
345 G_OBJECT_CLASS (ring_parent_class)->finalize (object);
349 #define CONTEXT_OK(c) ((c) && PA_CONTEXT_IS_GOOD (pa_context_get_state ((c))))
350 #define STREAM_OK(s) ((s) && PA_STREAM_IS_GOOD (pa_stream_get_state ((s))))
353 gst_pulsering_is_dead (GstPulseSink * psink, GstPulseRingBuffer * pbuf,
354 gboolean check_stream)
356 if (!CONTEXT_OK (pbuf->context))
359 if (check_stream && !STREAM_OK (pbuf->stream))
366 const gchar *err_str =
367 pbuf->context ? pa_strerror (pa_context_errno (pbuf->context)) : NULL;
368 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED, ("Disconnected: %s",
375 gst_pulsering_context_state_cb (pa_context * c, void *userdata)
377 pa_context_state_t state;
378 pa_threaded_mainloop *mainloop = (pa_threaded_mainloop *) userdata;
380 state = pa_context_get_state (c);
382 GST_LOG ("got new context state %d", state);
385 case PA_CONTEXT_READY:
386 case PA_CONTEXT_TERMINATED:
387 case PA_CONTEXT_FAILED:
388 GST_LOG ("signaling");
389 pa_threaded_mainloop_signal (mainloop, 0);
392 case PA_CONTEXT_UNCONNECTED:
393 case PA_CONTEXT_CONNECTING:
394 case PA_CONTEXT_AUTHORIZING:
395 case PA_CONTEXT_SETTING_NAME:
400 #ifdef HAVE_PULSE_0_9_12
402 gst_pulsering_context_subscribe_cb (pa_context * c,
403 pa_subscription_event_type_t t, uint32_t idx, void *userdata)
406 GstPulseContext *pctx = (GstPulseContext *) userdata;
409 if (t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT | PA_SUBSCRIPTION_EVENT_CHANGE) &&
410 t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT | PA_SUBSCRIPTION_EVENT_NEW))
413 for (walk = pctx->ring_buffers; walk; walk = g_slist_next (walk)) {
414 GstPulseRingBuffer *pbuf = (GstPulseRingBuffer *) walk->data;
415 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
417 GST_LOG_OBJECT (psink, "type %d, idx %u", t, idx);
422 if (idx != pa_stream_get_index (pbuf->stream))
425 /* Actually this event is also triggered when other properties of
426 * the stream change that are unrelated to the volume. However it is
427 * probably cheaper to signal the change here and check for the
428 * volume when the GObject property is read instead of querying it always. */
430 /* inform streaming thread to notify */
431 g_atomic_int_compare_and_exchange (&psink->notify, 0, 1);
436 /* will be called when the device should be opened. In this case we will connect
437 * to the server. We should not try to open any streams in this state. */
439 gst_pulseringbuffer_open_device (GstRingBuffer * buf)
442 GstPulseRingBuffer *pbuf;
443 GstPulseContext *pctx;
444 pa_mainloop_api *api;
445 gboolean need_unlock_shared;
447 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
448 pbuf = GST_PULSERING_BUFFER_CAST (buf);
450 g_assert (!pbuf->stream);
451 g_assert (psink->client_name);
454 pbuf->context_name = g_strdup_printf ("%s@%s", psink->client_name,
457 pbuf->context_name = g_strdup (psink->client_name);
459 pa_threaded_mainloop_lock (mainloop);
461 g_mutex_lock (pa_shared_resource_mutex);
462 need_unlock_shared = TRUE;
464 pctx = g_hash_table_lookup (gst_pulse_shared_contexts, pbuf->context_name);
466 pctx = g_slice_new0 (GstPulseContext);
468 /* get the mainloop api and create a context */
469 GST_INFO_OBJECT (psink, "new context with name %s, pbuf=%p, pctx=%p",
470 pbuf->context_name, pbuf, pctx);
471 api = pa_threaded_mainloop_get_api (mainloop);
472 if (!(pctx->context = pa_context_new (api, pbuf->context_name)))
475 pctx->ring_buffers = g_slist_prepend (pctx->ring_buffers, pbuf);
476 g_hash_table_insert (gst_pulse_shared_contexts,
477 g_strdup (pbuf->context_name), (gpointer) pctx);
478 /* register some essential callbacks */
479 pa_context_set_state_callback (pctx->context,
480 gst_pulsering_context_state_cb, mainloop);
481 #ifdef HAVE_PULSE_0_9_12
482 pa_context_set_subscribe_callback (pctx->context,
483 gst_pulsering_context_subscribe_cb, pctx);
486 /* try to connect to the server and wait for completion, we don't want to
487 * autospawn a deamon */
488 GST_LOG_OBJECT (psink, "connect to server %s",
489 GST_STR_NULL (psink->server));
490 if (pa_context_connect (pctx->context, psink->server,
491 PA_CONTEXT_NOAUTOSPAWN, NULL) < 0)
494 GST_INFO_OBJECT (psink,
495 "reusing shared context with name %s, pbuf=%p, pctx=%p",
496 pbuf->context_name, pbuf, pctx);
497 pctx->ring_buffers = g_slist_prepend (pctx->ring_buffers, pbuf);
500 g_mutex_unlock (pa_shared_resource_mutex);
501 need_unlock_shared = FALSE;
503 /* context created or shared okay */
504 pbuf->context = pa_context_ref (pctx->context);
507 pa_context_state_t state;
509 state = pa_context_get_state (pbuf->context);
511 GST_LOG_OBJECT (psink, "context state is now %d", state);
513 if (!PA_CONTEXT_IS_GOOD (state))
516 if (state == PA_CONTEXT_READY)
519 /* Wait until the context is ready */
520 GST_LOG_OBJECT (psink, "waiting..");
521 pa_threaded_mainloop_wait (mainloop);
524 GST_LOG_OBJECT (psink, "opened the device");
526 pa_threaded_mainloop_unlock (mainloop);
533 if (need_unlock_shared)
534 g_mutex_unlock (pa_shared_resource_mutex);
535 gst_pulsering_destroy_context (pbuf);
536 pa_threaded_mainloop_unlock (mainloop);
541 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
542 ("Failed to create context"), (NULL));
543 g_slice_free (GstPulseContext, pctx);
544 goto unlock_and_fail;
548 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED, ("Failed to connect: %s",
549 pa_strerror (pa_context_errno (pctx->context))), (NULL));
550 goto unlock_and_fail;
554 /* close the device */
556 gst_pulseringbuffer_close_device (GstRingBuffer * buf)
559 GstPulseRingBuffer *pbuf;
561 pbuf = GST_PULSERING_BUFFER_CAST (buf);
562 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
564 GST_LOG_OBJECT (psink, "closing device");
566 pa_threaded_mainloop_lock (mainloop);
567 gst_pulsering_destroy_context (pbuf);
568 pa_threaded_mainloop_unlock (mainloop);
570 GST_LOG_OBJECT (psink, "closed device");
576 gst_pulsering_stream_state_cb (pa_stream * s, void *userdata)
579 GstPulseRingBuffer *pbuf;
580 pa_stream_state_t state;
582 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
583 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
585 state = pa_stream_get_state (s);
586 GST_LOG_OBJECT (psink, "got new stream state %d", state);
589 case PA_STREAM_READY:
590 case PA_STREAM_FAILED:
591 case PA_STREAM_TERMINATED:
592 GST_LOG_OBJECT (psink, "signaling");
593 pa_threaded_mainloop_signal (mainloop, 0);
595 case PA_STREAM_UNCONNECTED:
596 case PA_STREAM_CREATING:
602 gst_pulsering_stream_request_cb (pa_stream * s, size_t length, void *userdata)
606 GstPulseRingBuffer *pbuf;
608 rbuf = GST_RING_BUFFER_CAST (userdata);
609 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
610 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
612 GST_LOG_OBJECT (psink, "got request for length %" G_GSIZE_FORMAT, length);
614 if (pbuf->in_commit && (length >= rbuf->spec.segsize)) {
615 /* only signal when we are waiting in the commit thread
616 * and got request for atleast a segment */
617 pa_threaded_mainloop_signal (mainloop, 0);
622 gst_pulsering_stream_underflow_cb (pa_stream * s, void *userdata)
625 GstPulseRingBuffer *pbuf;
627 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
628 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
630 GST_WARNING_OBJECT (psink, "Got underflow");
634 gst_pulsering_stream_overflow_cb (pa_stream * s, void *userdata)
637 GstPulseRingBuffer *pbuf;
639 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
640 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
642 GST_WARNING_OBJECT (psink, "Got overflow");
646 gst_pulsering_stream_latency_cb (pa_stream * s, void *userdata)
649 GstPulseRingBuffer *pbuf;
650 const pa_timing_info *info;
653 info = pa_stream_get_timing_info (s);
655 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
656 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
659 GST_LOG_OBJECT (psink, "latency update (information unknown)");
662 #ifdef HAVE_PULSE_0_9_11
663 sink_usec = info->configured_sink_usec;
668 GST_LOG_OBJECT (psink,
669 "latency_update, %" G_GUINT64_FORMAT ", %d:%" G_GINT64_FORMAT ", %d:%"
670 G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT,
671 GST_TIMEVAL_TO_TIME (info->timestamp), info->write_index_corrupt,
672 info->write_index, info->read_index_corrupt, info->read_index,
673 info->sink_usec, sink_usec);
677 gst_pulsering_stream_suspended_cb (pa_stream * p, void *userdata)
680 GstPulseRingBuffer *pbuf;
682 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
683 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
685 if (pa_stream_is_suspended (p))
686 GST_DEBUG_OBJECT (psink, "stream suspended");
688 GST_DEBUG_OBJECT (psink, "stream resumed");
691 #ifdef HAVE_PULSE_0_9_11
693 gst_pulsering_stream_started_cb (pa_stream * p, void *userdata)
696 GstPulseRingBuffer *pbuf;
698 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
699 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
701 GST_DEBUG_OBJECT (psink, "stream started");
705 #ifdef HAVE_PULSE_0_9_15
707 gst_pulsering_stream_event_cb (pa_stream * p, const char *name,
708 pa_proplist * pl, void *userdata)
711 GstPulseRingBuffer *pbuf;
713 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
714 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
716 if (!strcmp (name, PA_STREAM_EVENT_REQUEST_CORK)) {
717 /* the stream wants to PAUSE, post a message for the application. */
718 GST_DEBUG_OBJECT (psink, "got request for CORK");
719 gst_element_post_message (GST_ELEMENT_CAST (psink),
720 gst_message_new_request_state (GST_OBJECT_CAST (psink),
723 } else if (!strcmp (name, PA_STREAM_EVENT_REQUEST_UNCORK)) {
724 GST_DEBUG_OBJECT (psink, "got request for UNCORK");
725 gst_element_post_message (GST_ELEMENT_CAST (psink),
726 gst_message_new_request_state (GST_OBJECT_CAST (psink),
729 GST_DEBUG_OBJECT (psink, "got unknown event %s", name);
734 /* This method should create a new stream of the given @spec. No playback should
735 * start yet so we start in the corked state. */
737 gst_pulseringbuffer_acquire (GstRingBuffer * buf, GstRingBufferSpec * spec)
740 GstPulseRingBuffer *pbuf;
741 pa_buffer_attr wanted;
742 const pa_buffer_attr *actual;
743 pa_channel_map channel_map;
744 pa_operation *o = NULL;
745 #ifdef HAVE_PULSE_0_9_20
748 pa_cvolume *pv = NULL;
749 pa_stream_flags_t flags;
751 GstAudioClock *clock;
753 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
754 pbuf = GST_PULSERING_BUFFER_CAST (buf);
756 GST_LOG_OBJECT (psink, "creating sample spec");
757 /* convert the gstreamer sample spec to the pulseaudio format */
758 if (!gst_pulse_fill_sample_spec (spec, &pbuf->sample_spec))
761 pa_threaded_mainloop_lock (mainloop);
763 /* we need a context and a no stream */
764 g_assert (pbuf->context);
765 g_assert (!pbuf->stream);
767 /* enable event notifications */
768 GST_LOG_OBJECT (psink, "subscribing to context events");
769 if (!(o = pa_context_subscribe (pbuf->context,
770 PA_SUBSCRIPTION_MASK_SINK_INPUT, NULL, NULL)))
771 goto subscribe_failed;
773 pa_operation_unref (o);
775 /* initialize the channel map */
776 gst_pulse_gst_to_channel_map (&channel_map, spec);
778 /* find a good name for the stream */
779 if (psink->stream_name)
780 name = psink->stream_name;
782 name = "Playback Stream";
784 /* create a stream */
785 GST_LOG_OBJECT (psink, "creating stream with name %s", name);
786 if (!(pbuf->stream = pa_stream_new_with_proplist (pbuf->context, name,
787 &pbuf->sample_spec, &channel_map, psink->proplist)))
790 /* install essential callbacks */
791 pa_stream_set_state_callback (pbuf->stream,
792 gst_pulsering_stream_state_cb, pbuf);
793 pa_stream_set_write_callback (pbuf->stream,
794 gst_pulsering_stream_request_cb, pbuf);
795 pa_stream_set_underflow_callback (pbuf->stream,
796 gst_pulsering_stream_underflow_cb, pbuf);
797 pa_stream_set_overflow_callback (pbuf->stream,
798 gst_pulsering_stream_overflow_cb, pbuf);
799 pa_stream_set_latency_update_callback (pbuf->stream,
800 gst_pulsering_stream_latency_cb, pbuf);
801 pa_stream_set_suspended_callback (pbuf->stream,
802 gst_pulsering_stream_suspended_cb, pbuf);
803 #ifdef HAVE_PULSE_0_9_11
804 pa_stream_set_started_callback (pbuf->stream,
805 gst_pulsering_stream_started_cb, pbuf);
807 #ifdef HAVE_PULSE_0_9_15
808 pa_stream_set_event_callback (pbuf->stream,
809 gst_pulsering_stream_event_cb, pbuf);
812 /* buffering requirements. When setting prebuf to 0, the stream will not pause
813 * when we cause an underrun, which causes time to continue. */
814 memset (&wanted, 0, sizeof (wanted));
815 wanted.tlength = spec->segtotal * spec->segsize;
816 wanted.maxlength = -1;
818 wanted.minreq = spec->segsize;
820 GST_INFO_OBJECT (psink, "tlength: %d", wanted.tlength);
821 GST_INFO_OBJECT (psink, "maxlength: %d", wanted.maxlength);
822 GST_INFO_OBJECT (psink, "prebuf: %d", wanted.prebuf);
823 GST_INFO_OBJECT (psink, "minreq: %d", wanted.minreq);
825 #ifdef HAVE_PULSE_0_9_20
826 /* configure volume when we changed it, else we leave the default */
827 if (psink->volume_set) {
828 GST_LOG_OBJECT (psink, "have volume of %f", psink->volume);
830 gst_pulse_cvolume_from_linear (pv, pbuf->sample_spec.channels,
837 /* construct the flags */
838 flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE |
839 #ifdef HAVE_PULSE_0_9_11
840 PA_STREAM_ADJUST_LATENCY |
842 PA_STREAM_START_CORKED;
844 #ifdef HAVE_PULSE_0_9_12
845 if (psink->mute_set && psink->mute)
846 flags |= PA_STREAM_START_MUTED;
849 /* we always start corked (see flags above) */
852 /* try to connect now */
853 GST_LOG_OBJECT (psink, "connect for playback to device %s",
854 GST_STR_NULL (psink->device));
855 if (pa_stream_connect_playback (pbuf->stream, psink->device,
856 &wanted, flags, pv, NULL) < 0)
859 /* our clock will now start from 0 again */
860 clock = GST_AUDIO_CLOCK (GST_BASE_AUDIO_SINK (psink)->provided_clock);
861 gst_audio_clock_reset (clock, 0);
864 pa_stream_state_t state;
866 state = pa_stream_get_state (pbuf->stream);
868 GST_LOG_OBJECT (psink, "stream state is now %d", state);
870 if (!PA_STREAM_IS_GOOD (state))
873 if (state == PA_STREAM_READY)
876 /* Wait until the stream is ready */
877 pa_threaded_mainloop_wait (mainloop);
880 /* After we passed the volume off of to PA we never want to set it
881 again, since it is PA's job to save/restore volumes. */
882 psink->volume_set = psink->mute_set = FALSE;
884 GST_LOG_OBJECT (psink, "stream is acquired now");
886 /* get the actual buffering properties now */
887 actual = pa_stream_get_buffer_attr (pbuf->stream);
889 GST_INFO_OBJECT (psink, "tlength: %d (wanted: %d)", actual->tlength,
891 GST_INFO_OBJECT (psink, "maxlength: %d", actual->maxlength);
892 GST_INFO_OBJECT (psink, "prebuf: %d", actual->prebuf);
893 GST_INFO_OBJECT (psink, "minreq: %d (wanted %d)", actual->minreq,
896 spec->segsize = actual->minreq;
897 spec->segtotal = actual->tlength / spec->segsize;
899 pa_threaded_mainloop_unlock (mainloop);
906 gst_pulsering_destroy_stream (pbuf);
907 pa_threaded_mainloop_unlock (mainloop);
913 GST_ELEMENT_ERROR (psink, RESOURCE, SETTINGS,
914 ("Invalid sample specification."), (NULL));
919 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
920 ("pa_context_subscribe() failed: %s",
921 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
922 goto unlock_and_fail;
926 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
927 ("Failed to create stream: %s",
928 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
929 goto unlock_and_fail;
933 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
934 ("Failed to connect stream: %s",
935 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
936 goto unlock_and_fail;
940 /* free the stream that we acquired before */
942 gst_pulseringbuffer_release (GstRingBuffer * buf)
944 GstPulseRingBuffer *pbuf;
946 pbuf = GST_PULSERING_BUFFER_CAST (buf);
948 pa_threaded_mainloop_lock (mainloop);
949 gst_pulsering_destroy_stream (pbuf);
950 pa_threaded_mainloop_unlock (mainloop);
956 gst_pulsering_success_cb (pa_stream * s, int success, void *userdata)
958 pa_threaded_mainloop_signal (mainloop, 0);
961 /* update the corked state of a stream, must be called with the mainloop
964 gst_pulsering_set_corked (GstPulseRingBuffer * pbuf, gboolean corked,
967 pa_operation *o = NULL;
969 gboolean res = FALSE;
971 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
973 GST_DEBUG_OBJECT (psink, "setting corked state to %d", corked);
974 if (pbuf->corked != corked) {
975 if (!(o = pa_stream_cork (pbuf->stream, corked,
976 gst_pulsering_success_cb, pbuf)))
979 while (wait && pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
980 pa_threaded_mainloop_wait (mainloop);
981 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
984 pbuf->corked = corked;
986 GST_DEBUG_OBJECT (psink, "skipping, already in requested state");
992 pa_operation_unref (o);
999 GST_DEBUG_OBJECT (psink, "the server is dead");
1004 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1005 ("pa_stream_cork() failed: %s",
1006 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1012 gst_pulseringbuffer_clear (GstRingBuffer * buf)
1014 GstPulseSink *psink;
1015 GstPulseRingBuffer *pbuf;
1016 pa_operation *o = NULL;
1018 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1019 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1021 pa_threaded_mainloop_lock (mainloop);
1022 GST_DEBUG_OBJECT (psink, "clearing");
1024 /* don't wait for the flush to complete */
1025 if ((o = pa_stream_flush (pbuf->stream, NULL, pbuf)))
1026 pa_operation_unref (o);
1028 pa_threaded_mainloop_unlock (mainloop);
1032 mainloop_enter_defer_cb (pa_mainloop_api * api, void *userdata)
1034 GstPulseSink *pulsesink = GST_PULSESINK (userdata);
1035 GstMessage *message;
1038 g_value_init (&val, G_TYPE_POINTER);
1039 g_value_set_pointer (&val, g_thread_self ());
1041 GST_DEBUG_OBJECT (pulsesink, "posting ENTER stream status");
1042 message = gst_message_new_stream_status (GST_OBJECT (pulsesink),
1043 GST_STREAM_STATUS_TYPE_ENTER, GST_ELEMENT (pulsesink));
1044 gst_message_set_stream_status_object (message, &val);
1046 gst_element_post_message (GST_ELEMENT (pulsesink), message);
1048 /* signal the waiter */
1049 pulsesink->pa_defer_ran = TRUE;
1050 pa_threaded_mainloop_signal (mainloop, 0);
1053 /* start/resume playback ASAP, we don't uncork here but in the commit method */
1055 gst_pulseringbuffer_start (GstRingBuffer * buf)
1057 GstPulseSink *psink;
1058 GstPulseRingBuffer *pbuf;
1060 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1061 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1063 pa_threaded_mainloop_lock (mainloop);
1065 GST_DEBUG_OBJECT (psink, "scheduling stream status");
1066 psink->pa_defer_ran = FALSE;
1067 pa_mainloop_api_once (pa_threaded_mainloop_get_api (mainloop),
1068 mainloop_enter_defer_cb, psink);
1070 GST_DEBUG_OBJECT (psink, "starting");
1071 pbuf->paused = FALSE;
1073 /* EOS needs running clock */
1074 if (GST_BASE_SINK_CAST (psink)->eos ||
1075 g_atomic_int_get (&GST_BASE_AUDIO_SINK (psink)->abidata.ABI.
1077 gst_pulsering_set_corked (pbuf, FALSE, FALSE);
1079 pa_threaded_mainloop_unlock (mainloop);
1084 /* pause/stop playback ASAP */
1086 gst_pulseringbuffer_pause (GstRingBuffer * buf)
1088 GstPulseSink *psink;
1089 GstPulseRingBuffer *pbuf;
1092 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1093 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1095 pa_threaded_mainloop_lock (mainloop);
1096 GST_DEBUG_OBJECT (psink, "pausing and corking");
1097 /* make sure the commit method stops writing */
1098 pbuf->paused = TRUE;
1099 res = gst_pulsering_set_corked (pbuf, TRUE, TRUE);
1100 if (pbuf->in_commit) {
1101 /* we are waiting in a commit, signal */
1102 GST_DEBUG_OBJECT (psink, "signal commit");
1103 pa_threaded_mainloop_signal (mainloop, 0);
1105 pa_threaded_mainloop_unlock (mainloop);
1111 mainloop_leave_defer_cb (pa_mainloop_api * api, void *userdata)
1113 GstPulseSink *pulsesink = GST_PULSESINK (userdata);
1114 GstMessage *message;
1117 g_value_init (&val, G_TYPE_POINTER);
1118 g_value_set_pointer (&val, g_thread_self ());
1120 GST_DEBUG_OBJECT (pulsesink, "posting LEAVE stream status");
1121 message = gst_message_new_stream_status (GST_OBJECT (pulsesink),
1122 GST_STREAM_STATUS_TYPE_LEAVE, GST_ELEMENT (pulsesink));
1123 gst_message_set_stream_status_object (message, &val);
1124 gst_element_post_message (GST_ELEMENT (pulsesink), message);
1126 pulsesink->pa_defer_ran = TRUE;
1127 pa_threaded_mainloop_signal (mainloop, 0);
1128 gst_object_unref (pulsesink);
1131 /* stop playback, we flush everything. */
1133 gst_pulseringbuffer_stop (GstRingBuffer * buf)
1135 GstPulseSink *psink;
1136 GstPulseRingBuffer *pbuf;
1137 gboolean res = FALSE;
1138 pa_operation *o = NULL;
1140 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1141 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1143 pa_threaded_mainloop_lock (mainloop);
1144 pbuf->paused = TRUE;
1145 res = gst_pulsering_set_corked (pbuf, TRUE, TRUE);
1146 /* Inform anyone waiting in _commit() call that it shall wakeup */
1147 if (pbuf->in_commit) {
1148 GST_DEBUG_OBJECT (psink, "signal commit thread");
1149 pa_threaded_mainloop_signal (mainloop, 0);
1152 if (strcmp (psink->pa_version, "0.9.12")) {
1153 /* then try to flush, it's not fatal when this fails */
1154 GST_DEBUG_OBJECT (psink, "flushing");
1155 if ((o = pa_stream_flush (pbuf->stream, gst_pulsering_success_cb, pbuf))) {
1156 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1157 GST_DEBUG_OBJECT (psink, "wait for completion");
1158 pa_threaded_mainloop_wait (mainloop);
1159 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
1162 GST_DEBUG_OBJECT (psink, "flush completed");
1169 pa_operation_cancel (o);
1170 pa_operation_unref (o);
1173 GST_DEBUG_OBJECT (psink, "scheduling stream status");
1174 psink->pa_defer_ran = FALSE;
1175 gst_object_ref (psink);
1176 pa_mainloop_api_once (pa_threaded_mainloop_get_api (mainloop),
1177 mainloop_leave_defer_cb, psink);
1179 GST_DEBUG_OBJECT (psink, "waiting for stream status");
1180 pa_threaded_mainloop_unlock (mainloop);
1187 GST_DEBUG_OBJECT (psink, "the server is dead");
1192 /* in_samples >= out_samples, rate > 1.0 */
1193 #define FWD_UP_SAMPLES(s,se,d,de) \
1195 guint8 *sb = s, *db = d; \
1196 while (s <= se && d < de) { \
1197 memcpy (d, s, bps); \
1200 if ((*accum << 1) >= inr) { \
1205 in_samples -= (s - sb)/bps; \
1206 out_samples -= (d - db)/bps; \
1207 GST_DEBUG ("fwd_up end %d/%d",*accum,*toprocess); \
1210 /* out_samples > in_samples, for rates smaller than 1.0 */
1211 #define FWD_DOWN_SAMPLES(s,se,d,de) \
1213 guint8 *sb = s, *db = d; \
1214 while (s <= se && d < de) { \
1215 memcpy (d, s, bps); \
1218 if ((*accum << 1) >= outr) { \
1223 in_samples -= (s - sb)/bps; \
1224 out_samples -= (d - db)/bps; \
1225 GST_DEBUG ("fwd_down end %d/%d",*accum,*toprocess); \
1228 #define REV_UP_SAMPLES(s,se,d,de) \
1230 guint8 *sb = se, *db = d; \
1231 while (s <= se && d < de) { \
1232 memcpy (d, se, bps); \
1235 while (d < de && (*accum << 1) >= inr) { \
1240 in_samples -= (sb - se)/bps; \
1241 out_samples -= (d - db)/bps; \
1242 GST_DEBUG ("rev_up end %d/%d",*accum,*toprocess); \
1245 #define REV_DOWN_SAMPLES(s,se,d,de) \
1247 guint8 *sb = se, *db = d; \
1248 while (s <= se && d < de) { \
1249 memcpy (d, se, bps); \
1252 while (s <= se && (*accum << 1) >= outr) { \
1257 in_samples -= (sb - se)/bps; \
1258 out_samples -= (d - db)/bps; \
1259 GST_DEBUG ("rev_down end %d/%d",*accum,*toprocess); \
1263 /* our custom commit function because we write into the buffer of pulseaudio
1264 * instead of keeping our own buffer */
1266 gst_pulseringbuffer_commit (GstRingBuffer * buf, guint64 * sample,
1267 guchar * data, gint in_samples, gint out_samples, gint * accum)
1269 GstPulseSink *psink;
1270 GstPulseRingBuffer *pbuf;
1275 gint inr, outr, bps;
1279 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1280 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1282 /* FIXME post message rather than using a signal (as mixer interface) */
1283 if (g_atomic_int_compare_and_exchange (&psink->notify, 1, 0)) {
1284 g_object_notify (G_OBJECT (psink), "volume");
1285 g_object_notify (G_OBJECT (psink), "mute");
1288 /* make sure the ringbuffer is started */
1289 if (G_UNLIKELY (g_atomic_int_get (&buf->state) !=
1290 GST_RING_BUFFER_STATE_STARTED)) {
1291 /* see if we are allowed to start it */
1292 if (G_UNLIKELY (g_atomic_int_get (&buf->abidata.ABI.may_start) == FALSE))
1295 GST_DEBUG_OBJECT (buf, "start!");
1296 if (!gst_ring_buffer_start (buf))
1300 pa_threaded_mainloop_lock (mainloop);
1301 GST_DEBUG_OBJECT (psink, "entering commit");
1302 pbuf->in_commit = TRUE;
1304 bps = buf->spec.bytes_per_sample;
1305 bufsize = buf->spec.segsize * buf->spec.segtotal;
1307 /* our toy resampler for trick modes */
1308 reverse = out_samples < 0;
1309 out_samples = ABS (out_samples);
1311 if (in_samples >= out_samples)
1312 toprocess = &in_samples;
1314 toprocess = &out_samples;
1316 inr = in_samples - 1;
1317 outr = out_samples - 1;
1319 GST_DEBUG_OBJECT (psink, "in %d, out %d", inr, outr);
1321 /* data_end points to the last sample we have to write, not past it. This is
1322 * needed to properly handle reverse playback: it points to the last sample. */
1323 data_end = data + (bps * inr);
1328 /* offset is in bytes */
1329 offset = *sample * bps;
1331 while (*toprocess > 0) {
1335 GST_LOG_OBJECT (psink,
1336 "need to write %d samples at offset %" G_GINT64_FORMAT, *toprocess,
1339 #ifdef HAVE_PULSE_0_9_16
1340 if (offset != pbuf->m_lastoffset)
1341 GST_LOG_OBJECT (psink, "discontinuity, offset is %" G_GINT64_FORMAT ", "
1342 "last offset was %" G_GINT64_FORMAT, offset, pbuf->m_lastoffset);
1344 towrite = out_samples * bps;
1346 /* Only ever write segsize bytes at once. This will
1347 * also limit the PA shm buffer to segsize
1349 if (towrite > buf->spec.segsize)
1350 towrite = buf->spec.segsize;
1352 if ((pbuf->m_writable < towrite) || (offset != pbuf->m_lastoffset)) {
1353 /* if no room left or discontinuity in offset,
1354 we need to flush data and get a new buffer */
1356 /* flush the buffer if possible */
1357 if ((pbuf->m_data != NULL) && (pbuf->m_towrite > 0)) {
1359 GST_LOG_OBJECT (psink,
1360 "flushing %u samples at offset %" G_GINT64_FORMAT,
1361 (guint) pbuf->m_towrite / bps, pbuf->m_offset);
1363 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1364 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1368 pbuf->m_towrite = 0;
1369 pbuf->m_offset = offset; /* keep track of current offset */
1371 /* get a buffer to write in for now on */
1373 pbuf->m_writable = pa_stream_writable_size (pbuf->stream);
1375 if (pbuf->m_writable == (size_t) - 1)
1376 goto writable_size_failed;
1378 pbuf->m_writable /= bps;
1379 pbuf->m_writable *= bps; /* handle only complete samples */
1381 if (pbuf->m_writable >= towrite)
1384 /* see if we need to uncork because we have no free space */
1386 if (!gst_pulsering_set_corked (pbuf, FALSE, FALSE))
1390 /* we can't write a single byte, wait a bit */
1391 GST_LOG_OBJECT (psink, "waiting for free space");
1392 pa_threaded_mainloop_wait (mainloop);
1398 /* make sure we only buffer up latency-time samples */
1399 if (pbuf->m_writable > buf->spec.segsize) {
1400 /* limit buffering to latency-time value */
1401 pbuf->m_writable = buf->spec.segsize;
1403 GST_LOG_OBJECT (psink, "Limiting buffering to %" G_GSIZE_FORMAT,
1407 GST_LOG_OBJECT (psink, "requesting %" G_GSIZE_FORMAT " bytes of "
1408 "shared memory", pbuf->m_writable);
1410 if (pa_stream_begin_write (pbuf->stream, &pbuf->m_data,
1411 &pbuf->m_writable) < 0) {
1412 GST_LOG_OBJECT (psink, "pa_stream_begin_write() failed");
1413 goto writable_size_failed;
1416 GST_LOG_OBJECT (psink, "got %" G_GSIZE_FORMAT " bytes of shared memory",
1419 /* Just to make sure that we didn't get more than requested */
1420 if (pbuf->m_writable > buf->spec.segsize) {
1421 /* limit buffering to latency-time value */
1422 pbuf->m_writable = buf->spec.segsize;
1426 if (pbuf->m_writable < towrite)
1427 towrite = pbuf->m_writable;
1428 avail = towrite / bps;
1430 GST_LOG_OBJECT (psink, "writing %u samples at offset %" G_GUINT64_FORMAT,
1431 (guint) avail, offset);
1433 if (G_LIKELY (inr == outr && !reverse)) {
1434 /* no rate conversion, simply write out the samples */
1435 /* copy the data into internal buffer */
1437 memcpy ((guint8 *) pbuf->m_data + pbuf->m_towrite, data, towrite);
1438 pbuf->m_towrite += towrite;
1439 pbuf->m_writable -= towrite;
1442 in_samples -= avail;
1443 out_samples -= avail;
1445 guint8 *dest, *d, *d_end;
1447 /* write into the PulseAudio shm buffer */
1448 dest = d = (guint8 *) pbuf->m_data + pbuf->m_towrite;
1449 d_end = d + towrite;
1453 /* forward speed up */
1454 FWD_UP_SAMPLES (data, data_end, d, d_end);
1456 /* forward slow down */
1457 FWD_DOWN_SAMPLES (data, data_end, d, d_end);
1460 /* reverse speed up */
1461 REV_UP_SAMPLES (data, data_end, d, d_end);
1463 /* reverse slow down */
1464 REV_DOWN_SAMPLES (data, data_end, d, d_end);
1466 /* see what we have left to write */
1467 towrite = (d - dest);
1468 pbuf->m_towrite += towrite;
1469 pbuf->m_writable -= towrite;
1471 avail = towrite / bps;
1474 /* flush the buffer if it's full */
1475 if ((pbuf->m_data != NULL) && (pbuf->m_towrite > 0)
1476 && (pbuf->m_writable == 0)) {
1477 GST_LOG_OBJECT (psink, "flushing %u samples at offset %" G_GINT64_FORMAT,
1478 (guint) pbuf->m_towrite / bps, pbuf->m_offset);
1480 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1481 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1484 pbuf->m_towrite = 0;
1485 pbuf->m_offset = offset + towrite; /* keep track of current offset */
1490 /* FIXME, this is not quite right */
1491 if ((avail = pa_stream_writable_size (pbuf->stream)) == (size_t) - 1)
1492 goto writable_size_failed;
1494 /* We always try to satisfy a request for data */
1495 GST_LOG_OBJECT (psink, "writable bytes %" G_GSIZE_FORMAT, avail);
1497 /* convert to samples, we can only deal with multiples of the
1504 /* see if we need to uncork because we have no free space */
1506 if (!gst_pulsering_set_corked (pbuf, FALSE, FALSE))
1510 /* we can't write a single byte, wait a bit */
1511 GST_LOG_OBJECT (psink, "waiting for free space");
1512 pa_threaded_mainloop_wait (mainloop);
1518 if (avail > out_samples)
1519 avail = out_samples;
1521 towrite = avail * bps;
1523 GST_LOG_OBJECT (psink, "writing %u samples at offset %" G_GUINT64_FORMAT,
1524 (guint) avail, offset);
1526 if (G_LIKELY (inr == outr && !reverse)) {
1527 /* no rate conversion, simply write out the samples */
1528 if (pa_stream_write (pbuf->stream, data, towrite, NULL, offset,
1529 PA_SEEK_ABSOLUTE) < 0)
1533 in_samples -= avail;
1534 out_samples -= avail;
1536 guint8 *dest, *d, *d_end;
1538 /* we need to allocate a temporary buffer to resample the data into,
1539 * FIXME, we should have a pulseaudio API to allocate this buffer for us
1540 * from the shared memory. */
1541 dest = d = g_malloc (towrite);
1542 d_end = d + towrite;
1546 /* forward speed up */
1547 FWD_UP_SAMPLES (data, data_end, d, d_end);
1549 /* forward slow down */
1550 FWD_DOWN_SAMPLES (data, data_end, d, d_end);
1553 /* reverse speed up */
1554 REV_UP_SAMPLES (data, data_end, d, d_end);
1556 /* reverse slow down */
1557 REV_DOWN_SAMPLES (data, data_end, d, d_end);
1559 /* see what we have left to write */
1560 towrite = (d - dest);
1561 if (pa_stream_write (pbuf->stream, dest, towrite,
1562 g_free, offset, PA_SEEK_ABSOLUTE) < 0)
1565 avail = towrite / bps;
1567 #endif /* HAVE_PULSE_0_9_16 */
1570 offset += avail * bps;
1572 #ifdef HAVE_PULSE_0_9_16
1573 pbuf->m_lastoffset = offset;
1576 /* check if we need to uncork after writing the samples */
1578 const pa_timing_info *info;
1580 if ((info = pa_stream_get_timing_info (pbuf->stream))) {
1581 GST_LOG_OBJECT (psink,
1582 "read_index at %" G_GUINT64_FORMAT ", offset %" G_GINT64_FORMAT,
1583 info->read_index, offset);
1585 /* we uncork when the read_index is too far behind the offset we need
1587 if (info->read_index + bufsize <= offset) {
1588 if (!gst_pulsering_set_corked (pbuf, FALSE, FALSE))
1592 GST_LOG_OBJECT (psink, "no timing info available yet");
1596 /* we consumed all samples here */
1597 data = data_end + bps;
1599 pbuf->in_commit = FALSE;
1600 pa_threaded_mainloop_unlock (mainloop);
1603 result = inr - ((data_end - data) / bps);
1604 GST_LOG_OBJECT (psink, "wrote %d samples", result);
1611 pbuf->in_commit = FALSE;
1612 GST_LOG_OBJECT (psink, "we are reset");
1613 pa_threaded_mainloop_unlock (mainloop);
1618 GST_LOG_OBJECT (psink, "we can not start");
1623 GST_LOG_OBJECT (psink, "failed to start the ringbuffer");
1628 pbuf->in_commit = FALSE;
1629 GST_ERROR_OBJECT (psink, "uncork failed");
1630 pa_threaded_mainloop_unlock (mainloop);
1635 pbuf->in_commit = FALSE;
1636 GST_LOG_OBJECT (psink, "we are paused");
1637 pa_threaded_mainloop_unlock (mainloop);
1640 writable_size_failed:
1642 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1643 ("pa_stream_writable_size() failed: %s",
1644 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1645 goto unlock_and_fail;
1649 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1650 ("pa_stream_write() failed: %s",
1651 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1652 goto unlock_and_fail;
1656 /* write pending local samples, must be called with the mainloop lock */
1658 gst_pulsering_flush (GstPulseRingBuffer * pbuf)
1660 #ifdef HAVE_PULSE_0_9_16
1661 GstPulseSink *psink;
1663 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1664 GST_DEBUG_OBJECT (psink, "entering flush");
1666 /* flush the buffer if possible */
1667 if (pbuf->stream && (pbuf->m_data != NULL) && (pbuf->m_towrite > 0)) {
1668 #ifndef GST_DISABLE_GST_DEBUG
1671 bps = (GST_RING_BUFFER_CAST (pbuf))->spec.bytes_per_sample;
1672 GST_LOG_OBJECT (psink,
1673 "flushing %u samples at offset %" G_GINT64_FORMAT,
1674 (guint) pbuf->m_towrite / bps, pbuf->m_offset);
1677 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1678 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1682 pbuf->m_towrite = 0;
1683 pbuf->m_offset += pbuf->m_towrite; /* keep track of current offset */
1692 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1693 ("pa_stream_write() failed: %s",
1694 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1700 static void gst_pulsesink_set_property (GObject * object, guint prop_id,
1701 const GValue * value, GParamSpec * pspec);
1702 static void gst_pulsesink_get_property (GObject * object, guint prop_id,
1703 GValue * value, GParamSpec * pspec);
1704 static void gst_pulsesink_finalize (GObject * object);
1706 static gboolean gst_pulsesink_event (GstBaseSink * sink, GstEvent * event);
1708 static GstStateChangeReturn gst_pulsesink_change_state (GstElement * element,
1709 GstStateChange transition);
1711 static void gst_pulsesink_init_interfaces (GType type);
1713 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
1714 # define ENDIANNESS "LITTLE_ENDIAN, BIG_ENDIAN"
1716 # define ENDIANNESS "BIG_ENDIAN, LITTLE_ENDIAN"
1719 GST_IMPLEMENT_PULSEPROBE_METHODS (GstPulseSink, gst_pulsesink);
1721 #define _do_init(type) \
1722 gst_pulsesink_init_contexts (); \
1723 gst_pulsesink_init_interfaces (type);
1725 GST_BOILERPLATE_FULL (GstPulseSink, gst_pulsesink, GstBaseAudioSink,
1726 GST_TYPE_BASE_AUDIO_SINK, _do_init);
1729 gst_pulsesink_interface_supported (GstImplementsInterface *
1730 iface, GType interface_type)
1732 GstPulseSink *this = GST_PULSESINK_CAST (iface);
1734 if (interface_type == GST_TYPE_PROPERTY_PROBE && this->probe)
1736 if (interface_type == GST_TYPE_STREAM_VOLUME)
1743 gst_pulsesink_implements_interface_init (GstImplementsInterfaceClass * klass)
1745 klass->supported = gst_pulsesink_interface_supported;
1749 gst_pulsesink_init_interfaces (GType type)
1751 static const GInterfaceInfo implements_iface_info = {
1752 (GInterfaceInitFunc) gst_pulsesink_implements_interface_init,
1756 static const GInterfaceInfo probe_iface_info = {
1757 (GInterfaceInitFunc) gst_pulsesink_property_probe_interface_init,
1761 #ifdef HAVE_PULSE_0_9_12
1762 static const GInterfaceInfo svol_iface_info = {
1766 g_type_add_interface_static (type, GST_TYPE_STREAM_VOLUME, &svol_iface_info);
1769 g_type_add_interface_static (type, GST_TYPE_IMPLEMENTS_INTERFACE,
1770 &implements_iface_info);
1771 g_type_add_interface_static (type, GST_TYPE_PROPERTY_PROBE,
1776 gst_pulsesink_base_init (gpointer g_class)
1778 static GstStaticPadTemplate pad_template = GST_STATIC_PAD_TEMPLATE ("sink",
1781 GST_STATIC_CAPS ("audio/x-raw-int, "
1782 "endianness = (int) { " ENDIANNESS " }, "
1783 "signed = (boolean) TRUE, "
1784 "width = (int) 16, "
1785 "depth = (int) 16, "
1786 "rate = (int) [ 1, MAX ], "
1787 "channels = (int) [ 1, 32 ];"
1788 "audio/x-raw-float, "
1789 "endianness = (int) { " ENDIANNESS " }, "
1790 "width = (int) 32, "
1791 "rate = (int) [ 1, MAX ], "
1792 "channels = (int) [ 1, 32 ];"
1794 "endianness = (int) { " ENDIANNESS " }, "
1795 "signed = (boolean) TRUE, "
1796 "width = (int) 32, "
1797 "depth = (int) 32, "
1798 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 32 ];"
1799 #ifdef HAVE_PULSE_0_9_15
1801 "endianness = (int) { " ENDIANNESS " }, "
1802 "signed = (boolean) TRUE, "
1803 "width = (int) 24, "
1804 "depth = (int) 24, "
1805 "rate = (int) [ 1, MAX ], "
1806 "channels = (int) [ 1, 32 ];"
1808 "endianness = (int) { " ENDIANNESS " }, "
1809 "signed = (boolean) TRUE, "
1810 "width = (int) 32, "
1811 "depth = (int) 24, "
1812 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 32 ];"
1815 "signed = (boolean) FALSE, "
1818 "rate = (int) [ 1, MAX ], "
1819 "channels = (int) [ 1, 32 ];"
1821 "rate = (int) [ 1, MAX], "
1822 "channels = (int) [ 1, 32 ];"
1824 "rate = (int) [ 1, MAX], " "channels = (int) [ 1, 32 ]")
1827 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
1829 gst_element_class_set_details_simple (element_class,
1830 "PulseAudio Audio Sink",
1831 "Sink/Audio", "Plays audio to a PulseAudio server", "Lennart Poettering");
1832 gst_element_class_add_pad_template (element_class,
1833 gst_static_pad_template_get (&pad_template));
1836 static GstRingBuffer *
1837 gst_pulsesink_create_ringbuffer (GstBaseAudioSink * sink)
1839 GstRingBuffer *buffer;
1841 GST_DEBUG_OBJECT (sink, "creating ringbuffer");
1842 buffer = g_object_new (GST_TYPE_PULSERING_BUFFER, NULL);
1843 GST_DEBUG_OBJECT (sink, "created ringbuffer @%p", buffer);
1849 gst_pulsesink_class_init (GstPulseSinkClass * klass)
1851 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1852 GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
1853 GstBaseSinkClass *bc;
1854 GstBaseAudioSinkClass *gstaudiosink_class = GST_BASE_AUDIO_SINK_CLASS (klass);
1855 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1857 gobject_class->finalize = gst_pulsesink_finalize;
1858 gobject_class->set_property = gst_pulsesink_set_property;
1859 gobject_class->get_property = gst_pulsesink_get_property;
1861 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_pulsesink_event);
1863 /* restore the original basesink pull methods */
1864 bc = g_type_class_peek (GST_TYPE_BASE_SINK);
1865 gstbasesink_class->activate_pull = GST_DEBUG_FUNCPTR (bc->activate_pull);
1867 gstelement_class->change_state =
1868 GST_DEBUG_FUNCPTR (gst_pulsesink_change_state);
1870 gstaudiosink_class->create_ringbuffer =
1871 GST_DEBUG_FUNCPTR (gst_pulsesink_create_ringbuffer);
1873 /* Overwrite GObject fields */
1874 g_object_class_install_property (gobject_class,
1876 g_param_spec_string ("server", "Server",
1877 "The PulseAudio server to connect to", DEFAULT_SERVER,
1878 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1880 g_object_class_install_property (gobject_class, PROP_DEVICE,
1881 g_param_spec_string ("device", "Device",
1882 "The PulseAudio sink device to connect to", DEFAULT_DEVICE,
1883 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1885 g_object_class_install_property (gobject_class,
1887 g_param_spec_string ("device-name", "Device name",
1888 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
1889 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1891 #ifdef HAVE_PULSE_0_9_12
1892 g_object_class_install_property (gobject_class,
1894 g_param_spec_double ("volume", "Volume",
1895 "Linear volume of this stream, 1.0=100%", 0.0, MAX_VOLUME,
1896 DEFAULT_VOLUME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1897 g_object_class_install_property (gobject_class,
1899 g_param_spec_boolean ("mute", "Mute",
1900 "Mute state of this stream", DEFAULT_MUTE,
1901 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1905 * GstPulseSink:client
1907 * The PulseAudio client name to use.
1911 g_object_class_install_property (gobject_class,
1913 g_param_spec_string ("client", "Client",
1914 "The PulseAudio client name to use", gst_pulse_client_name (),
1915 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1916 GST_PARAM_MUTABLE_READY));
1919 * GstPulseSink:stream-properties
1921 * List of pulseaudio stream properties. A list of defined properties can be
1922 * found in the <ulink url="http://0pointer.de/lennart/projects/pulseaudio/doxygen/proplist_8h.html">pulseaudio api docs</ulink>.
1924 * Below is an example for registering as a music application to pulseaudio.
1926 * GstStructure *props;
1928 * props = gst_structure_from_string ("props,media.role=music", NULL);
1929 * g_object_set (pulse, "stream-properties", props, NULL);
1930 * gst_structure_free
1935 g_object_class_install_property (gobject_class,
1936 PROP_STREAM_PROPERTIES,
1937 g_param_spec_boxed ("stream-properties", "stream properties",
1938 "list of pulseaudio stream properties",
1939 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1942 /* returns the current time of the sink ringbuffer */
1944 gst_pulsesink_get_time (GstClock * clock, GstBaseAudioSink * sink)
1946 GstPulseSink *psink;
1947 GstPulseRingBuffer *pbuf;
1950 if (!sink->ringbuffer || !sink->ringbuffer->acquired)
1951 return GST_CLOCK_TIME_NONE;
1953 pbuf = GST_PULSERING_BUFFER_CAST (sink->ringbuffer);
1954 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1956 pa_threaded_mainloop_lock (mainloop);
1957 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
1960 /* if we don't have enough data to get a timestamp, just return NONE, which
1961 * will return the last reported time */
1962 if (pa_stream_get_time (pbuf->stream, &time) < 0) {
1963 GST_DEBUG_OBJECT (psink, "could not get time");
1964 time = GST_CLOCK_TIME_NONE;
1967 pa_threaded_mainloop_unlock (mainloop);
1969 GST_LOG_OBJECT (psink, "current time is %" GST_TIME_FORMAT,
1970 GST_TIME_ARGS (time));
1977 GST_DEBUG_OBJECT (psink, "the server is dead");
1978 pa_threaded_mainloop_unlock (mainloop);
1980 return GST_CLOCK_TIME_NONE;
1985 gst_pulsesink_init (GstPulseSink * pulsesink, GstPulseSinkClass * klass)
1987 pulsesink->server = NULL;
1988 pulsesink->device = NULL;
1989 pulsesink->device_description = NULL;
1990 pulsesink->client_name = gst_pulse_client_name ();
1992 pulsesink->volume = DEFAULT_VOLUME;
1993 pulsesink->volume_set = FALSE;
1995 pulsesink->mute = DEFAULT_MUTE;
1996 pulsesink->mute_set = FALSE;
1998 pulsesink->notify = 0;
2000 /* needed for conditional execution */
2001 pulsesink->pa_version = pa_get_library_version ();
2003 pulsesink->properties = NULL;
2004 pulsesink->proplist = NULL;
2006 GST_DEBUG_OBJECT (pulsesink, "using pulseaudio version %s",
2007 pulsesink->pa_version);
2009 /* override with a custom clock */
2010 if (GST_BASE_AUDIO_SINK (pulsesink)->provided_clock)
2011 gst_object_unref (GST_BASE_AUDIO_SINK (pulsesink)->provided_clock);
2013 GST_BASE_AUDIO_SINK (pulsesink)->provided_clock =
2014 gst_audio_clock_new ("GstPulseSinkClock",
2015 (GstAudioClockGetTimeFunc) gst_pulsesink_get_time, pulsesink);
2017 /* TRUE for sinks, FALSE for sources */
2018 pulsesink->probe = gst_pulseprobe_new (G_OBJECT (pulsesink),
2019 G_OBJECT_GET_CLASS (pulsesink), PROP_DEVICE, pulsesink->device,
2024 gst_pulsesink_finalize (GObject * object)
2026 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2028 g_free (pulsesink->server);
2029 g_free (pulsesink->device);
2030 g_free (pulsesink->device_description);
2031 g_free (pulsesink->client_name);
2033 if (pulsesink->properties)
2034 gst_structure_free (pulsesink->properties);
2035 if (pulsesink->proplist)
2036 pa_proplist_free (pulsesink->proplist);
2038 if (pulsesink->probe) {
2039 gst_pulseprobe_free (pulsesink->probe);
2040 pulsesink->probe = NULL;
2043 G_OBJECT_CLASS (parent_class)->finalize (object);
2046 #ifdef HAVE_PULSE_0_9_12
2048 gst_pulsesink_set_volume (GstPulseSink * psink, gdouble volume)
2051 pa_operation *o = NULL;
2052 GstPulseRingBuffer *pbuf;
2058 pa_threaded_mainloop_lock (mainloop);
2060 GST_DEBUG_OBJECT (psink, "setting volume to %f", volume);
2062 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2063 if (pbuf == NULL || pbuf->stream == NULL)
2066 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2069 gst_pulse_cvolume_from_linear (&v, pbuf->sample_spec.channels, volume);
2071 if (!(o = pa_context_set_sink_input_volume (pbuf->context, idx,
2075 /* We don't really care about the result of this call */
2079 pa_operation_unref (o);
2081 pa_threaded_mainloop_unlock (mainloop);
2088 psink->volume = volume;
2089 psink->volume_set = TRUE;
2091 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2096 psink->volume = volume;
2097 psink->volume_set = TRUE;
2099 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2104 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2109 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2110 ("pa_stream_set_sink_input_volume() failed: %s",
2111 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2117 gst_pulsesink_set_mute (GstPulseSink * psink, gboolean mute)
2119 pa_operation *o = NULL;
2120 GstPulseRingBuffer *pbuf;
2126 pa_threaded_mainloop_lock (mainloop);
2128 GST_DEBUG_OBJECT (psink, "setting mute state to %d", mute);
2130 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2131 if (pbuf == NULL || pbuf->stream == NULL)
2134 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2137 if (!(o = pa_context_set_sink_input_mute (pbuf->context, idx,
2141 /* We don't really care about the result of this call */
2145 pa_operation_unref (o);
2147 pa_threaded_mainloop_unlock (mainloop);
2155 psink->mute_set = TRUE;
2157 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2163 psink->mute_set = TRUE;
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_stream_set_sink_input_mute() failed: %s",
2177 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2183 gst_pulsesink_sink_input_info_cb (pa_context * c, const pa_sink_input_info * i,
2184 int eol, void *userdata)
2186 GstPulseRingBuffer *pbuf;
2187 GstPulseSink *psink;
2189 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
2190 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
2198 /* If the index doesn't match our current stream,
2199 * it implies we just recreated the stream (caps change)
2201 if (i->index == pa_stream_get_index (pbuf->stream)) {
2202 psink->volume = pa_sw_volume_to_linear (pa_cvolume_max (&i->volume));
2203 psink->mute = i->mute;
2207 pa_threaded_mainloop_signal (mainloop, 0);
2211 gst_pulsesink_get_volume (GstPulseSink * psink)
2213 GstPulseRingBuffer *pbuf;
2214 pa_operation *o = NULL;
2215 gdouble v = DEFAULT_VOLUME;
2221 pa_threaded_mainloop_lock (mainloop);
2223 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2224 if (pbuf == NULL || pbuf->stream == NULL)
2227 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2230 if (!(o = pa_context_get_sink_input_info (pbuf->context, idx,
2231 gst_pulsesink_sink_input_info_cb, pbuf)))
2234 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2235 pa_threaded_mainloop_wait (mainloop);
2236 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
2244 pa_operation_unref (o);
2246 pa_threaded_mainloop_unlock (mainloop);
2248 if (v > MAX_VOLUME) {
2249 GST_WARNING_OBJECT (psink, "Clipped volume from %f to %f", v, MAX_VOLUME);
2259 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2264 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2269 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2274 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2275 ("pa_context_get_sink_input_info() failed: %s",
2276 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2282 gst_pulsesink_get_mute (GstPulseSink * psink)
2284 GstPulseRingBuffer *pbuf;
2285 pa_operation *o = NULL;
2287 gboolean mute = FALSE;
2292 pa_threaded_mainloop_lock (mainloop);
2295 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2296 if (pbuf == NULL || pbuf->stream == NULL)
2299 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2302 if (!(o = pa_context_get_sink_input_info (pbuf->context, idx,
2303 gst_pulsesink_sink_input_info_cb, pbuf)))
2306 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2307 pa_threaded_mainloop_wait (mainloop);
2308 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
2314 pa_operation_unref (o);
2316 pa_threaded_mainloop_unlock (mainloop);
2324 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2329 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2334 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2339 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2340 ("pa_context_get_sink_input_info() failed: %s",
2341 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2348 gst_pulsesink_sink_info_cb (pa_context * c, const pa_sink_info * i, int eol,
2351 GstPulseRingBuffer *pbuf;
2352 GstPulseSink *psink;
2354 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
2355 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
2360 g_free (psink->device_description);
2361 psink->device_description = g_strdup (i->description);
2364 pa_threaded_mainloop_signal (mainloop, 0);
2368 gst_pulsesink_device_description (GstPulseSink * psink)
2370 GstPulseRingBuffer *pbuf;
2371 pa_operation *o = NULL;
2377 pa_threaded_mainloop_lock (mainloop);
2378 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2382 if (!(o = pa_context_get_sink_info_by_name (pbuf->context,
2383 psink->device, gst_pulsesink_sink_info_cb, pbuf)))
2386 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2387 pa_threaded_mainloop_wait (mainloop);
2388 if (gst_pulsering_is_dead (psink, pbuf, FALSE))
2394 pa_operation_unref (o);
2396 t = g_strdup (psink->device_description);
2397 pa_threaded_mainloop_unlock (mainloop);
2404 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2409 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2414 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2415 ("pa_context_get_sink_info_by_index() failed: %s",
2416 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2422 gst_pulsesink_set_property (GObject * object,
2423 guint prop_id, const GValue * value, GParamSpec * pspec)
2425 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2429 g_free (pulsesink->server);
2430 pulsesink->server = g_value_dup_string (value);
2431 if (pulsesink->probe)
2432 gst_pulseprobe_set_server (pulsesink->probe, pulsesink->server);
2435 g_free (pulsesink->device);
2436 pulsesink->device = g_value_dup_string (value);
2438 #ifdef HAVE_PULSE_0_9_12
2440 gst_pulsesink_set_volume (pulsesink, g_value_get_double (value));
2443 gst_pulsesink_set_mute (pulsesink, g_value_get_boolean (value));
2447 g_free (pulsesink->client_name);
2448 if (!g_value_get_string (value)) {
2449 GST_WARNING_OBJECT (pulsesink,
2450 "Empty PulseAudio client name not allowed. Resetting to default value");
2451 pulsesink->client_name = gst_pulse_client_name ();
2453 pulsesink->client_name = g_value_dup_string (value);
2455 case PROP_STREAM_PROPERTIES:
2456 if (pulsesink->properties)
2457 gst_structure_free (pulsesink->properties);
2458 pulsesink->properties =
2459 gst_structure_copy (gst_value_get_structure (value));
2460 if (pulsesink->proplist)
2461 pa_proplist_free (pulsesink->proplist);
2462 pulsesink->proplist = gst_pulse_make_proplist (pulsesink->properties);
2465 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2471 gst_pulsesink_get_property (GObject * object,
2472 guint prop_id, GValue * value, GParamSpec * pspec)
2475 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2479 g_value_set_string (value, pulsesink->server);
2482 g_value_set_string (value, pulsesink->device);
2484 case PROP_DEVICE_NAME:
2485 g_value_take_string (value, gst_pulsesink_device_description (pulsesink));
2487 #ifdef HAVE_PULSE_0_9_12
2489 g_value_set_double (value, gst_pulsesink_get_volume (pulsesink));
2492 g_value_set_boolean (value, gst_pulsesink_get_mute (pulsesink));
2496 g_value_set_string (value, pulsesink->client_name);
2498 case PROP_STREAM_PROPERTIES:
2499 gst_value_set_structure (value, pulsesink->properties);
2502 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2508 gst_pulsesink_change_title (GstPulseSink * psink, const gchar * t)
2510 pa_operation *o = NULL;
2511 GstPulseRingBuffer *pbuf;
2513 pa_threaded_mainloop_lock (mainloop);
2515 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2517 if (pbuf == NULL || pbuf->stream == NULL)
2520 g_free (pbuf->stream_name);
2521 pbuf->stream_name = g_strdup (t);
2523 if (!(o = pa_stream_set_name (pbuf->stream, pbuf->stream_name, NULL, NULL)))
2526 /* We're not interested if this operation failed or not */
2530 pa_operation_unref (o);
2531 pa_threaded_mainloop_unlock (mainloop);
2538 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2543 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2544 ("pa_stream_set_name() failed: %s",
2545 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2550 #ifdef HAVE_PULSE_0_9_11
2552 gst_pulsesink_change_props (GstPulseSink * psink, GstTagList * l)
2554 static const gchar *const map[] = {
2555 GST_TAG_TITLE, PA_PROP_MEDIA_TITLE,
2557 /* might get overriden in the next iteration by GST_TAG_ARTIST */
2558 GST_TAG_PERFORMER, PA_PROP_MEDIA_ARTIST,
2560 GST_TAG_ARTIST, PA_PROP_MEDIA_ARTIST,
2561 GST_TAG_LANGUAGE_CODE, PA_PROP_MEDIA_LANGUAGE,
2562 GST_TAG_LOCATION, PA_PROP_MEDIA_FILENAME,
2563 /* We might add more here later on ... */
2566 pa_proplist *pl = NULL;
2567 const gchar *const *t;
2568 gboolean empty = TRUE;
2569 pa_operation *o = NULL;
2570 GstPulseRingBuffer *pbuf;
2572 pl = pa_proplist_new ();
2574 for (t = map; *t; t += 2) {
2577 if (gst_tag_list_get_string (l, *t, &n)) {
2580 pa_proplist_sets (pl, *(t + 1), n);
2590 pa_threaded_mainloop_lock (mainloop);
2591 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2592 if (pbuf == NULL || pbuf->stream == NULL)
2595 if (!(o = pa_stream_proplist_update (pbuf->stream, PA_UPDATE_REPLACE,
2599 /* We're not interested if this operation failed or not */
2603 pa_operation_unref (o);
2605 pa_threaded_mainloop_unlock (mainloop);
2610 pa_proplist_free (pl);
2617 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2622 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2623 ("pa_stream_proplist_update() failed: %s",
2624 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2631 gst_pulsesink_flush_ringbuffer (GstPulseSink * psink)
2633 GstPulseRingBuffer *pbuf;
2635 pa_threaded_mainloop_lock (mainloop);
2637 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2639 if (pbuf == NULL || pbuf->stream == NULL)
2642 gst_pulsering_flush (pbuf);
2644 /* Uncork if we haven't already (happens when waiting to get enough data
2645 * to send out the first time) */
2647 gst_pulsering_set_corked (pbuf, FALSE, FALSE);
2649 /* We're not interested if this operation failed or not */
2651 pa_threaded_mainloop_unlock (mainloop);
2658 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2664 gst_pulsesink_event (GstBaseSink * sink, GstEvent * event)
2666 GstPulseSink *pulsesink = GST_PULSESINK_CAST (sink);
2668 switch (GST_EVENT_TYPE (event)) {
2669 case GST_EVENT_TAG:{
2670 gchar *title = NULL, *artist = NULL, *location = NULL, *description =
2671 NULL, *t = NULL, *buf = NULL;
2674 gst_event_parse_tag (event, &l);
2676 gst_tag_list_get_string (l, GST_TAG_TITLE, &title);
2677 gst_tag_list_get_string (l, GST_TAG_ARTIST, &artist);
2678 gst_tag_list_get_string (l, GST_TAG_LOCATION, &location);
2679 gst_tag_list_get_string (l, GST_TAG_DESCRIPTION, &description);
2682 gst_tag_list_get_string (l, GST_TAG_PERFORMER, &artist);
2684 if (title && artist)
2685 /* TRANSLATORS: 'song title' by 'artist name' */
2686 t = buf = g_strdup_printf (_("'%s' by '%s'"), g_strstrip (title),
2687 g_strstrip (artist));
2689 t = g_strstrip (title);
2690 else if (description)
2691 t = g_strstrip (description);
2693 t = g_strstrip (location);
2696 gst_pulsesink_change_title (pulsesink, t);
2701 g_free (description);
2704 #ifdef HAVE_PULSE_0_9_11
2705 gst_pulsesink_change_props (pulsesink, l);
2711 gst_pulsesink_flush_ringbuffer (pulsesink);
2717 return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
2720 static GstStateChangeReturn
2721 gst_pulsesink_change_state (GstElement * element, GstStateChange transition)
2723 GstPulseSink *pulsesink = GST_PULSESINK (element);
2724 GstStateChangeReturn ret;
2726 switch (transition) {
2727 case GST_STATE_CHANGE_NULL_TO_READY:
2728 g_mutex_lock (pa_shared_resource_mutex);
2729 if (!mainloop_ref_ct) {
2730 GST_INFO_OBJECT (element, "new pa main loop thread");
2731 if (!(mainloop = pa_threaded_mainloop_new ()))
2732 goto mainloop_failed;
2733 mainloop_ref_ct = 1;
2734 pa_threaded_mainloop_start (mainloop);
2735 g_mutex_unlock (pa_shared_resource_mutex);
2737 GST_INFO_OBJECT (element, "reusing pa main loop thread");
2739 g_mutex_unlock (pa_shared_resource_mutex);
2742 case GST_STATE_CHANGE_READY_TO_PAUSED:
2743 gst_element_post_message (element,
2744 gst_message_new_clock_provide (GST_OBJECT_CAST (element),
2745 GST_BASE_AUDIO_SINK (pulsesink)->provided_clock, TRUE));
2751 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2752 if (ret == GST_STATE_CHANGE_FAILURE)
2755 switch (transition) {
2756 case GST_STATE_CHANGE_PAUSED_TO_READY:
2757 gst_element_post_message (element,
2758 gst_message_new_clock_lost (GST_OBJECT_CAST (element),
2759 GST_BASE_AUDIO_SINK (pulsesink)->provided_clock));
2761 case GST_STATE_CHANGE_READY_TO_NULL:
2763 g_mutex_lock (pa_shared_resource_mutex);
2765 if (!mainloop_ref_ct) {
2766 GST_INFO_OBJECT (element, "terminating pa main loop thread");
2767 pa_threaded_mainloop_stop (mainloop);
2768 pa_threaded_mainloop_free (mainloop);
2771 g_mutex_unlock (pa_shared_resource_mutex);
2783 g_mutex_unlock (pa_shared_resource_mutex);
2784 GST_ELEMENT_ERROR (pulsesink, RESOURCE, FAILED,
2785 ("pa_threaded_mainloop_new() failed"), (NULL));
2786 return GST_STATE_CHANGE_FAILURE;
2790 if (transition == GST_STATE_CHANGE_NULL_TO_READY) {
2791 /* Clear the PA mainloop if baseaudiosink failed to open the ring_buffer */
2792 g_assert (mainloop);
2793 g_mutex_lock (pa_shared_resource_mutex);
2795 if (!mainloop_ref_ct) {
2796 GST_INFO_OBJECT (element, "terminating pa main loop thread");
2797 pa_threaded_mainloop_stop (mainloop);
2798 pa_threaded_mainloop_free (mainloop);
2801 g_mutex_unlock (pa_shared_resource_mutex);