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
85 #define GST_TYPE_PULSERING_BUFFER \
86 (gst_pulseringbuffer_get_type())
87 #define GST_PULSERING_BUFFER(obj) \
88 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_PULSERING_BUFFER,GstPulseRingBuffer))
89 #define GST_PULSERING_BUFFER_CLASS(klass) \
90 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_PULSERING_BUFFER,GstPulseRingBufferClass))
91 #define GST_PULSERING_BUFFER_GET_CLASS(obj) \
92 (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_PULSERING_BUFFER, GstPulseRingBufferClass))
93 #define GST_PULSERING_BUFFER_CAST(obj) \
94 ((GstPulseRingBuffer *)obj)
95 #define GST_IS_PULSERING_BUFFER(obj) \
96 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_PULSERING_BUFFER))
97 #define GST_IS_PULSERING_BUFFER_CLASS(klass)\
98 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_PULSERING_BUFFER))
100 typedef struct _GstPulseRingBuffer GstPulseRingBuffer;
101 typedef struct _GstPulseRingBufferClass GstPulseRingBufferClass;
103 typedef struct _GstPulseContext GstPulseContext;
105 struct _GstPulseContext
108 GSList *ring_buffers;
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. */
114 static GHashTable *gst_pulse_shared_contexts;
116 /* We keep a custom ringbuffer that is backed up by data allocated by
117 * pulseaudio. We must also overide the commit function to write into
118 * pulseaudio memory instead. */
119 struct _GstPulseRingBuffer
121 GstRingBuffer object;
128 pa_sample_spec sample_spec;
130 #ifdef HAVE_PULSE_0_9_16
139 gboolean in_commit:1;
142 struct _GstPulseRingBufferClass
144 GstRingBufferClass parent_class;
147 static GType gst_pulseringbuffer_get_type (void);
148 static void gst_pulseringbuffer_finalize (GObject * object);
150 static GstRingBufferClass *ring_parent_class = NULL;
152 static gboolean gst_pulseringbuffer_open_device (GstRingBuffer * buf);
153 static gboolean gst_pulseringbuffer_close_device (GstRingBuffer * buf);
154 static gboolean gst_pulseringbuffer_acquire (GstRingBuffer * buf,
155 GstRingBufferSpec * spec);
156 static gboolean gst_pulseringbuffer_release (GstRingBuffer * buf);
157 static gboolean gst_pulseringbuffer_start (GstRingBuffer * buf);
158 static gboolean gst_pulseringbuffer_pause (GstRingBuffer * buf);
159 static gboolean gst_pulseringbuffer_stop (GstRingBuffer * buf);
160 static void gst_pulseringbuffer_clear (GstRingBuffer * buf);
161 static guint gst_pulseringbuffer_commit (GstRingBuffer * buf,
162 guint64 * sample, guchar * data, gint in_samples, gint out_samples,
165 G_DEFINE_TYPE (GstPulseRingBuffer, gst_pulseringbuffer, GST_TYPE_RING_BUFFER);
167 static GMutex *pa_ring_buffer_mutex = NULL;
169 gst_pulseringbuffer_init_contexts (void)
171 g_assert (pa_ring_buffer_mutex == NULL);
172 pa_ring_buffer_mutex = g_mutex_new ();
173 gst_pulse_shared_contexts = g_hash_table_new (g_str_hash, g_str_equal);
177 gst_pulseringbuffer_class_init (GstPulseRingBufferClass * klass)
179 GObjectClass *gobject_class;
180 GstRingBufferClass *gstringbuffer_class;
182 gobject_class = (GObjectClass *) klass;
183 gstringbuffer_class = (GstRingBufferClass *) klass;
185 ring_parent_class = g_type_class_peek_parent (klass);
187 gobject_class->finalize = gst_pulseringbuffer_finalize;
189 gstringbuffer_class->open_device =
190 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_open_device);
191 gstringbuffer_class->close_device =
192 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_close_device);
193 gstringbuffer_class->acquire =
194 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_acquire);
195 gstringbuffer_class->release =
196 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_release);
197 gstringbuffer_class->start = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_start);
198 gstringbuffer_class->pause = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_pause);
199 gstringbuffer_class->resume = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_start);
200 gstringbuffer_class->stop = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_stop);
201 gstringbuffer_class->clear_all =
202 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_clear);
204 gstringbuffer_class->commit = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_commit);
208 gst_pulseringbuffer_init (GstPulseRingBuffer * pbuf)
210 pbuf->stream_name = NULL;
213 #ifdef HAVE_PULSE_0_9_13
214 pa_sample_spec_init (&pbuf->sample_spec);
216 pbuf->sample_spec.format = PA_SAMPLE_INVALID;
217 pbuf->sample_spec.rate = 0;
218 pbuf->sample_spec.channels = 0;
221 #ifdef HAVE_PULSE_0_9_16
224 pbuf->m_writable = 0;
226 pbuf->m_lastoffset = 0;
230 pbuf->in_commit = FALSE;
231 pbuf->paused = FALSE;
235 gst_pulsering_destroy_stream (GstPulseRingBuffer * pbuf)
239 #ifdef HAVE_PULSE_0_9_16
241 /* drop shm memory buffer */
242 pa_stream_cancel_write (pbuf->stream);
244 /* reset internal variables */
247 pbuf->m_writable = 0;
249 pbuf->m_lastoffset = 0;
253 pa_stream_disconnect (pbuf->stream);
255 /* Make sure we don't get any further callbacks */
256 pa_stream_set_state_callback (pbuf->stream, NULL, NULL);
257 pa_stream_set_write_callback (pbuf->stream, NULL, NULL);
258 pa_stream_set_underflow_callback (pbuf->stream, NULL, NULL);
259 pa_stream_set_overflow_callback (pbuf->stream, NULL, NULL);
261 pa_stream_unref (pbuf->stream);
265 g_free (pbuf->stream_name);
266 pbuf->stream_name = NULL;
269 static GstPulseContext *
270 gst_pulsering_get_context (GstPulseRingBuffer * pbuf)
272 GstPulseContext *pctx;
275 g_mutex_lock (pa_ring_buffer_mutex);
276 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
277 pctx = g_hash_table_lookup (gst_pulse_shared_contexts, pbuf->context_name);
278 g_mutex_unlock (pa_ring_buffer_mutex);
283 gst_pulsering_destroy_context (GstPulseRingBuffer * pbuf)
285 GstPulseContext *pctx;
288 g_mutex_lock (pa_ring_buffer_mutex);
289 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
291 pctx = g_hash_table_lookup (gst_pulse_shared_contexts, pbuf->context_name);
293 gst_pulsering_destroy_stream (pbuf);
296 pctx->ring_buffers = g_slist_remove (pctx->ring_buffers, pbuf);
297 if (!g_slist_length (pctx->ring_buffers)) {
298 pa_context_disconnect (pctx->context);
300 /* Make sure we don't get any further callbacks */
301 pa_context_set_state_callback (pctx->context, NULL, NULL);
302 #ifdef HAVE_PULSE_0_9_12
303 pa_context_set_subscribe_callback (pctx->context, NULL, NULL);
306 pa_context_unref (pctx->context);
307 g_hash_table_remove (gst_pulse_shared_contexts, pbuf->context_name);
308 g_free (pbuf->context_name);
309 g_slice_free (GstPulseContext, pctx);
312 g_mutex_unlock (pa_ring_buffer_mutex);
316 gst_pulseringbuffer_finalize (GObject * object)
318 GstPulseRingBuffer *ringbuffer;
320 ringbuffer = GST_PULSERING_BUFFER_CAST (object);
322 gst_pulsering_destroy_context (ringbuffer);
324 G_OBJECT_CLASS (ring_parent_class)->finalize (object);
328 gst_pulsering_is_dead (GstPulseSink * psink, GstPulseRingBuffer * pbuf)
330 GstPulseContext *pctx = gst_pulsering_get_context (pbuf);
333 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED, ("Disconnected"), (NULL));
338 || !PA_CONTEXT_IS_GOOD (pa_context_get_state (pctx->context))
340 || !PA_STREAM_IS_GOOD (pa_stream_get_state (pbuf->stream))) {
341 const gchar *err_str =
342 pctx->context ? pa_strerror (pa_context_errno (pctx->context)) : NULL;
343 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED, ("Disconnected: %s",
351 gst_pulsering_context_state_cb (pa_context * c, void *userdata)
354 pa_context_state_t state;
356 GstPulseContext *pctx = (GstPulseContext *) userdata;
359 state = pa_context_get_state (c);
361 for (walk = pctx->ring_buffers; walk; walk = g_slist_next (walk)) {
362 GstPulseRingBuffer *pbuf = (GstPulseRingBuffer *) walk->data;
363 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
364 GST_LOG_OBJECT (psink, "got new context state %d", state);
366 /* psink can be null when we are shutting down and the ringbuffer is already
372 case PA_CONTEXT_READY:
373 case PA_CONTEXT_TERMINATED:
374 case PA_CONTEXT_FAILED:
375 GST_LOG_OBJECT (psink, "signaling");
376 pa_threaded_mainloop_signal (psink->mainloop, 0);
379 case PA_CONTEXT_UNCONNECTED:
380 case PA_CONTEXT_CONNECTING:
381 case PA_CONTEXT_AUTHORIZING:
382 case PA_CONTEXT_SETTING_NAME:
388 #ifdef HAVE_PULSE_0_9_12
390 gst_pulsering_context_subscribe_cb (pa_context * c,
391 pa_subscription_event_type_t t, uint32_t idx, void *userdata)
394 GstPulseContext *pctx = (GstPulseContext *) userdata;
397 if (t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT | PA_SUBSCRIPTION_EVENT_CHANGE) &&
398 t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT | PA_SUBSCRIPTION_EVENT_NEW))
401 for (walk = pctx->ring_buffers; walk; walk = g_slist_next (walk)) {
402 GstPulseRingBuffer *pbuf = (GstPulseRingBuffer *) walk->data;
403 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
405 GST_LOG_OBJECT (psink, "type %d, idx %u", t, idx);
410 if (idx != pa_stream_get_index (pbuf->stream))
413 /* Actually this event is also triggered when other properties of
414 * the stream change that are unrelated to the volume. However it is
415 * probably cheaper to signal the change here and check for the
416 * volume when the GObject property is read instead of querying it always. */
418 /* inform streaming thread to notify */
419 g_atomic_int_compare_and_exchange (&psink->notify, 0, 1);
424 /* will be called when the device should be opened. In this case we will connect
425 * to the server. We should not try to open any streams in this state. */
427 gst_pulseringbuffer_open_device (GstRingBuffer * buf)
430 GstPulseRingBuffer *pbuf;
431 GstPulseContext *pctx;
432 pa_mainloop_api *api;
434 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
435 pbuf = GST_PULSERING_BUFFER_CAST (buf);
437 g_assert (!pbuf->stream);
439 pbuf->context_name = g_strdup_printf ("%s@%s", gst_pulse_client_name (),
440 GST_STR_NULL (psink->server));
442 pa_threaded_mainloop_lock (psink->mainloop);
443 g_mutex_lock (pa_ring_buffer_mutex);
445 pctx = g_hash_table_lookup (gst_pulse_shared_contexts, pbuf->context_name);
447 pctx = g_slice_new0 (GstPulseContext);
448 /* get the mainloop api and create a context */
449 GST_LOG_OBJECT (psink, "new context with name %s",
450 GST_STR_NULL (pbuf->context_name));
451 api = pa_threaded_mainloop_get_api (psink->mainloop);
452 if (!(pctx->context = pa_context_new (api, pbuf->context_name)))
455 pctx->ring_buffers = g_slist_append (pctx->ring_buffers, pbuf);
456 g_hash_table_insert (gst_pulse_shared_contexts, pbuf->context_name,
458 /* register some essential callbacks */
459 pa_context_set_state_callback (pctx->context,
460 gst_pulsering_context_state_cb, pctx);
461 #ifdef HAVE_PULSE_0_9_12
462 pa_context_set_subscribe_callback (pctx->context,
463 gst_pulsering_context_subscribe_cb, pctx);
466 /* try to connect to the server and wait for completioni, we don't want to
467 * autospawn a deamon */
468 GST_LOG_OBJECT (psink, "connect to server %s",
469 GST_STR_NULL (psink->server));
470 if (pa_context_connect (pctx->context, psink->server,
471 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));
1843 /* returns the current time of the sink ringbuffer */
1845 gst_pulsesink_get_time (GstClock * clock, GstBaseAudioSink * sink)
1847 GstPulseSink *psink;
1848 GstPulseRingBuffer *pbuf;
1851 if (!sink->ringbuffer || !sink->ringbuffer->acquired)
1852 return GST_CLOCK_TIME_NONE;
1854 pbuf = GST_PULSERING_BUFFER_CAST (sink->ringbuffer);
1855 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1857 pa_threaded_mainloop_lock (psink->mainloop);
1858 if (gst_pulsering_is_dead (psink, pbuf))
1861 /* if we don't have enough data to get a timestamp, just return NONE, which
1862 * will return the last reported time */
1863 if (pa_stream_get_time (pbuf->stream, &time) < 0) {
1864 GST_DEBUG_OBJECT (psink, "could not get time");
1865 time = GST_CLOCK_TIME_NONE;
1868 pa_threaded_mainloop_unlock (psink->mainloop);
1870 GST_LOG_OBJECT (psink, "current time is %" GST_TIME_FORMAT,
1871 GST_TIME_ARGS (time));
1878 GST_DEBUG_OBJECT (psink, "the server is dead");
1879 pa_threaded_mainloop_unlock (psink->mainloop);
1881 return GST_CLOCK_TIME_NONE;
1886 gst_pulsesink_init (GstPulseSink * pulsesink, GstPulseSinkClass * klass)
1888 pulsesink->server = NULL;
1889 pulsesink->device = NULL;
1890 pulsesink->device_description = NULL;
1892 pulsesink->volume = DEFAULT_VOLUME;
1893 pulsesink->volume_set = FALSE;
1895 pulsesink->mute = DEFAULT_MUTE;
1896 pulsesink->mute_set = FALSE;
1898 pulsesink->notify = 0;
1900 /* needed for conditional execution */
1901 pulsesink->pa_version = pa_get_library_version ();
1903 GST_DEBUG_OBJECT (pulsesink, "using pulseaudio version %s",
1904 pulsesink->pa_version);
1906 /* TRUE for sinks, FALSE for sources */
1907 pulsesink->probe = gst_pulseprobe_new (G_OBJECT (pulsesink),
1908 G_OBJECT_GET_CLASS (pulsesink), PROP_DEVICE, pulsesink->device,
1913 gst_pulsesink_finalize (GObject * object)
1915 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
1917 g_free (pulsesink->server);
1918 g_free (pulsesink->device);
1919 g_free (pulsesink->device_description);
1921 if (pulsesink->probe) {
1922 gst_pulseprobe_free (pulsesink->probe);
1923 pulsesink->probe = NULL;
1926 G_OBJECT_CLASS (parent_class)->finalize (object);
1929 #ifdef HAVE_PULSE_0_9_12
1931 gst_pulsesink_set_volume (GstPulseSink * psink, gdouble volume)
1934 pa_operation *o = NULL;
1935 GstPulseRingBuffer *pbuf;
1936 GstPulseContext *pctx;
1939 if (!psink->mainloop)
1942 pa_threaded_mainloop_lock (psink->mainloop);
1944 GST_DEBUG_OBJECT (psink, "setting volume to %f", volume);
1946 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
1947 if (pbuf == NULL || pbuf->stream == NULL)
1950 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
1953 gst_pulse_cvolume_from_linear (&v, pbuf->sample_spec.channels, volume);
1955 pctx = gst_pulsering_get_context (pbuf);
1957 if (!(o = pa_context_set_sink_input_volume (pctx->context, idx,
1961 /* We don't really care about the result of this call */
1965 pa_operation_unref (o);
1967 pa_threaded_mainloop_unlock (psink->mainloop);
1974 psink->volume = volume;
1975 psink->volume_set = TRUE;
1977 GST_DEBUG_OBJECT (psink, "we have no mainloop");
1982 psink->volume = volume;
1983 psink->volume_set = TRUE;
1985 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
1990 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
1995 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1996 ("pa_stream_set_sink_input_volume() failed: %s",
1997 pa_strerror (pa_context_errno (pctx->context))), (NULL));
2003 gst_pulsesink_set_mute (GstPulseSink * psink, gboolean mute)
2005 pa_operation *o = NULL;
2006 GstPulseRingBuffer *pbuf;
2007 GstPulseContext *pctx;
2010 if (!psink->mainloop)
2013 pa_threaded_mainloop_lock (psink->mainloop);
2015 GST_DEBUG_OBJECT (psink, "setting mute state to %d", mute);
2017 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2018 if (pbuf == NULL || pbuf->stream == NULL)
2021 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2024 pctx = gst_pulsering_get_context (pbuf);
2026 if (!(o = pa_context_set_sink_input_mute (pctx->context, idx,
2030 /* We don't really care about the result of this call */
2034 pa_operation_unref (o);
2036 pa_threaded_mainloop_unlock (psink->mainloop);
2044 psink->mute_set = TRUE;
2046 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2052 psink->mute_set = TRUE;
2054 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2059 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2064 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2065 ("pa_stream_set_sink_input_mute() failed: %s",
2066 pa_strerror (pa_context_errno (pctx->context))), (NULL));
2072 gst_pulsesink_sink_input_info_cb (pa_context * c, const pa_sink_input_info * i,
2073 int eol, void *userdata)
2075 GstPulseRingBuffer *pbuf;
2076 GstPulseSink *psink;
2078 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
2079 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
2087 /* If the index doesn't match our current stream,
2088 * it implies we just recreated the stream (caps change)
2090 if (i->index == pa_stream_get_index (pbuf->stream)) {
2091 psink->volume = pa_sw_volume_to_linear (pa_cvolume_max (&i->volume));
2092 psink->mute = i->mute;
2096 pa_threaded_mainloop_signal (psink->mainloop, 0);
2100 gst_pulsesink_get_volume (GstPulseSink * psink)
2102 GstPulseRingBuffer *pbuf;
2103 GstPulseContext *pctx;
2104 pa_operation *o = NULL;
2105 gdouble v = DEFAULT_VOLUME;
2108 if (!psink->mainloop)
2111 pa_threaded_mainloop_lock (psink->mainloop);
2113 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2114 if (pbuf == NULL || pbuf->stream == NULL)
2117 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2120 pctx = gst_pulsering_get_context (pbuf);
2122 if (!(o = pa_context_get_sink_input_info (pctx->context, idx,
2123 gst_pulsesink_sink_input_info_cb, pbuf)))
2126 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2127 pa_threaded_mainloop_wait (psink->mainloop);
2128 if (gst_pulsering_is_dead (psink, pbuf))
2136 pa_operation_unref (o);
2138 pa_threaded_mainloop_unlock (psink->mainloop);
2140 if (v > MAX_VOLUME) {
2141 GST_WARNING_OBJECT (psink, "Clipped volume from %f to %f", v, MAX_VOLUME);
2151 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2156 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2161 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2166 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2167 ("pa_context_get_sink_input_info() failed: %s",
2168 pa_strerror (pa_context_errno (pctx->context))), (NULL));
2174 gst_pulsesink_get_mute (GstPulseSink * psink)
2176 GstPulseRingBuffer *pbuf;
2177 GstPulseContext *pctx;
2178 pa_operation *o = NULL;
2180 gboolean mute = FALSE;
2182 if (!psink->mainloop)
2185 pa_threaded_mainloop_lock (psink->mainloop);
2188 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2189 if (pbuf == NULL || pbuf->stream == NULL)
2192 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2195 pctx = gst_pulsering_get_context (pbuf);
2197 if (!(o = pa_context_get_sink_input_info (pctx->context, idx,
2198 gst_pulsesink_sink_input_info_cb, pbuf)))
2201 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2202 pa_threaded_mainloop_wait (psink->mainloop);
2203 if (gst_pulsering_is_dead (psink, pbuf))
2210 pa_operation_unref (o);
2212 pa_threaded_mainloop_unlock (psink->mainloop);
2220 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2225 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2230 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2235 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2236 ("pa_context_get_sink_input_info() failed: %s",
2237 pa_strerror (pa_context_errno (pctx->context))), (NULL));
2244 gst_pulsesink_sink_info_cb (pa_context * c, const pa_sink_info * i, int eol,
2247 GstPulseRingBuffer *pbuf;
2248 GstPulseSink *psink;
2250 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
2251 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
2259 g_assert (i->index == pa_stream_get_device_index (pbuf->stream));
2261 g_free (psink->device_description);
2262 psink->device_description = g_strdup (i->description);
2265 pa_threaded_mainloop_signal (psink->mainloop, 0);
2269 gst_pulsesink_device_description (GstPulseSink * psink)
2271 GstPulseRingBuffer *pbuf;
2272 GstPulseContext *pctx;
2273 pa_operation *o = NULL;
2276 if (!psink->mainloop)
2279 pa_threaded_mainloop_lock (psink->mainloop);
2280 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2281 if (pbuf == NULL || pbuf->stream == NULL)
2284 pctx = gst_pulsering_get_context (pbuf);
2286 if (!(o = pa_context_get_sink_info_by_index (pctx->context,
2287 pa_stream_get_device_index (pbuf->stream),
2288 gst_pulsesink_sink_info_cb, pbuf)))
2291 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2292 pa_threaded_mainloop_wait (psink->mainloop);
2293 if (gst_pulsering_is_dead (psink, pbuf))
2300 pa_operation_unref (o);
2302 t = g_strdup (psink->device_description);
2303 pa_threaded_mainloop_unlock (psink->mainloop);
2310 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2315 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2320 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2321 ("pa_context_get_sink_info_by_index() failed: %s",
2322 pa_strerror (pa_context_errno (pctx->context))), (NULL));
2328 gst_pulsesink_set_property (GObject * object,
2329 guint prop_id, const GValue * value, GParamSpec * pspec)
2331 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2335 g_free (pulsesink->server);
2336 pulsesink->server = g_value_dup_string (value);
2337 if (pulsesink->probe)
2338 gst_pulseprobe_set_server (pulsesink->probe, pulsesink->server);
2341 g_free (pulsesink->device);
2342 pulsesink->device = g_value_dup_string (value);
2344 #ifdef HAVE_PULSE_0_9_12
2346 gst_pulsesink_set_volume (pulsesink, g_value_get_double (value));
2349 gst_pulsesink_set_mute (pulsesink, g_value_get_boolean (value));
2353 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2359 gst_pulsesink_get_property (GObject * object,
2360 guint prop_id, GValue * value, GParamSpec * pspec)
2363 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2367 g_value_set_string (value, pulsesink->server);
2370 g_value_set_string (value, pulsesink->device);
2372 case PROP_DEVICE_NAME:
2373 g_value_take_string (value, gst_pulsesink_device_description (pulsesink));
2375 #ifdef HAVE_PULSE_0_9_12
2377 g_value_set_double (value, gst_pulsesink_get_volume (pulsesink));
2380 g_value_set_boolean (value, gst_pulsesink_get_mute (pulsesink));
2384 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2390 gst_pulsesink_change_title (GstPulseSink * psink, const gchar * t)
2392 pa_operation *o = NULL;
2393 GstPulseRingBuffer *pbuf;
2394 GstPulseContext *pctx;
2396 pa_threaded_mainloop_lock (psink->mainloop);
2398 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2400 if (pbuf == NULL || pbuf->stream == NULL)
2403 g_free (pbuf->stream_name);
2404 pbuf->stream_name = g_strdup (t);
2406 pctx = gst_pulsering_get_context (pbuf);
2408 if (!(o = pa_stream_set_name (pbuf->stream, pbuf->stream_name, NULL, NULL)))
2411 /* We're not interested if this operation failed or not */
2415 pa_operation_unref (o);
2416 pa_threaded_mainloop_unlock (psink->mainloop);
2423 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2428 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2429 ("pa_stream_set_name() failed: %s",
2430 pa_strerror (pa_context_errno (pctx->context))), (NULL));
2435 #ifdef HAVE_PULSE_0_9_11
2437 gst_pulsesink_change_props (GstPulseSink * psink, GstTagList * l)
2439 static const gchar *const map[] = {
2440 GST_TAG_TITLE, PA_PROP_MEDIA_TITLE,
2442 /* might get overriden in the next iteration by GST_TAG_ARTIST */
2443 GST_TAG_PERFORMER, PA_PROP_MEDIA_ARTIST,
2445 GST_TAG_ARTIST, PA_PROP_MEDIA_ARTIST,
2446 GST_TAG_LANGUAGE_CODE, PA_PROP_MEDIA_LANGUAGE,
2447 GST_TAG_LOCATION, PA_PROP_MEDIA_FILENAME,
2448 /* We might add more here later on ... */
2451 pa_proplist *pl = NULL;
2452 const gchar *const *t;
2453 gboolean empty = TRUE;
2454 pa_operation *o = NULL;
2455 GstPulseRingBuffer *pbuf;
2456 GstPulseContext *pctx;
2458 pl = pa_proplist_new ();
2460 for (t = map; *t; t += 2) {
2463 if (gst_tag_list_get_string (l, *t, &n)) {
2466 pa_proplist_sets (pl, *(t + 1), n);
2476 pa_threaded_mainloop_lock (psink->mainloop);
2478 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2479 if (pbuf == NULL || pbuf->stream == NULL)
2482 pctx = gst_pulsering_get_context (pbuf);
2484 if (!(o = pa_stream_proplist_update (pbuf->stream, PA_UPDATE_REPLACE,
2488 /* We're not interested if this operation failed or not */
2492 pa_operation_unref (o);
2494 pa_threaded_mainloop_unlock (psink->mainloop);
2499 pa_proplist_free (pl);
2506 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2511 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2512 ("pa_stream_proplist_update() failed: %s",
2513 pa_strerror (pa_context_errno (pctx->context))), (NULL));
2520 gst_pulsesink_event (GstBaseSink * sink, GstEvent * event)
2522 GstPulseSink *pulsesink = GST_PULSESINK_CAST (sink);
2524 switch (GST_EVENT_TYPE (event)) {
2525 case GST_EVENT_TAG:{
2526 gchar *title = NULL, *artist = NULL, *location = NULL, *description =
2527 NULL, *t = NULL, *buf = NULL;
2530 gst_event_parse_tag (event, &l);
2532 gst_tag_list_get_string (l, GST_TAG_TITLE, &title);
2533 gst_tag_list_get_string (l, GST_TAG_ARTIST, &artist);
2534 gst_tag_list_get_string (l, GST_TAG_LOCATION, &location);
2535 gst_tag_list_get_string (l, GST_TAG_DESCRIPTION, &description);
2538 gst_tag_list_get_string (l, GST_TAG_PERFORMER, &artist);
2540 if (title && artist)
2541 /* TRANSLATORS: 'song title' by 'artist name' */
2542 t = buf = g_strdup_printf (_("'%s' by '%s'"), g_strstrip (title),
2543 g_strstrip (artist));
2545 t = g_strstrip (title);
2546 else if (description)
2547 t = g_strstrip (description);
2549 t = g_strstrip (location);
2552 gst_pulsesink_change_title (pulsesink, t);
2557 g_free (description);
2560 #ifdef HAVE_PULSE_0_9_11
2561 gst_pulsesink_change_props (pulsesink, l);
2570 return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
2573 static GstStateChangeReturn
2574 gst_pulsesink_change_state (GstElement * element, GstStateChange transition)
2576 GstPulseSink *pulsesink = GST_PULSESINK (element);
2577 GstStateChangeReturn ret;
2580 switch (transition) {
2581 case GST_STATE_CHANGE_NULL_TO_READY:
2582 g_assert (pulsesink->mainloop == NULL);
2583 pulsesink->mainloop = pa_threaded_mainloop_new ();
2584 g_assert (pulsesink->mainloop != NULL);
2585 res = pa_threaded_mainloop_start (pulsesink->mainloop);
2586 g_assert (res == 0);
2588 /* override with a custom clock */
2589 if (GST_BASE_AUDIO_SINK (pulsesink)->provided_clock)
2590 gst_object_unref (GST_BASE_AUDIO_SINK (pulsesink)->provided_clock);
2592 /* FIXME: get rid once we can depend on core/base git again (>= 0.10.30.1)
2593 * (and the pbutils include above as well) */
2594 #if defined(GST_PLUGINS_BASE_VERSION_MAJOR)
2595 GST_BASE_AUDIO_SINK (pulsesink)->provided_clock =
2596 gst_audio_clock_new_full ("GstPulseSinkClock",
2597 (GstAudioClockGetTimeFunc) gst_pulsesink_get_time,
2598 gst_object_ref (pulsesink), (GDestroyNotify) gst_object_unref);
2600 GST_BASE_AUDIO_SINK (pulsesink)->provided_clock =
2601 gst_audio_clock_new ("GstPulseSinkClock",
2602 (GstAudioClockGetTimeFunc) gst_pulsesink_get_time, pulsesink);
2605 case GST_STATE_CHANGE_READY_TO_PAUSED:
2606 gst_element_post_message (element,
2607 gst_message_new_clock_provide (GST_OBJECT_CAST (element),
2608 GST_BASE_AUDIO_SINK (pulsesink)->provided_clock, TRUE));
2614 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2616 /* Clear the PA mainloop if baseaudiosink failed to open the ring_buffer */
2617 if (ret == GST_STATE_CHANGE_FAILURE
2618 && transition == GST_STATE_CHANGE_NULL_TO_READY) {
2619 if (GST_BASE_AUDIO_SINK (pulsesink)->provided_clock)
2620 gst_object_unref (GST_BASE_AUDIO_SINK (pulsesink)->provided_clock);
2621 GST_BASE_AUDIO_SINK (pulsesink)->provided_clock = NULL;
2623 g_assert (pulsesink->mainloop);
2624 pa_threaded_mainloop_stop (pulsesink->mainloop);
2625 pa_threaded_mainloop_free (pulsesink->mainloop);
2626 pulsesink->mainloop = NULL;
2629 switch (transition) {
2630 case GST_STATE_CHANGE_PAUSED_TO_READY:
2631 gst_element_post_message (element,
2632 gst_message_new_clock_lost (GST_OBJECT_CAST (element),
2633 GST_BASE_AUDIO_SINK (pulsesink)->provided_clock));
2635 case GST_STATE_CHANGE_READY_TO_NULL:
2636 if (GST_BASE_AUDIO_SINK (pulsesink)->provided_clock)
2637 gst_object_unref (GST_BASE_AUDIO_SINK (pulsesink)->provided_clock);
2638 GST_BASE_AUDIO_SINK (pulsesink)->provided_clock = NULL;
2639 if (pulsesink->mainloop) {
2640 pa_threaded_mainloop_stop (pulsesink->mainloop);
2641 pa_threaded_mainloop_free (pulsesink->mainloop);
2642 pulsesink->mainloop = NULL;