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
66 #define DEFAULT_SERVER NULL
67 #define DEFAULT_DEVICE NULL
68 #define DEFAULT_DEVICE_NAME NULL
69 #define DEFAULT_VOLUME 1.0
70 #define DEFAULT_MUTE FALSE
71 #define MAX_VOLUME 10.0
82 PROP_STREAM_PROPERTIES,
86 #define GST_TYPE_PULSERING_BUFFER \
87 (gst_pulseringbuffer_get_type())
88 #define GST_PULSERING_BUFFER(obj) \
89 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_PULSERING_BUFFER,GstPulseRingBuffer))
90 #define GST_PULSERING_BUFFER_CLASS(klass) \
91 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_PULSERING_BUFFER,GstPulseRingBufferClass))
92 #define GST_PULSERING_BUFFER_GET_CLASS(obj) \
93 (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_PULSERING_BUFFER, GstPulseRingBufferClass))
94 #define GST_PULSERING_BUFFER_CAST(obj) \
95 ((GstPulseRingBuffer *)obj)
96 #define GST_IS_PULSERING_BUFFER(obj) \
97 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_PULSERING_BUFFER))
98 #define GST_IS_PULSERING_BUFFER_CLASS(klass)\
99 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_PULSERING_BUFFER))
101 typedef struct _GstPulseRingBuffer GstPulseRingBuffer;
102 typedef struct _GstPulseRingBufferClass GstPulseRingBufferClass;
104 typedef struct _GstPulseContext GstPulseContext;
106 /* Store the PA contexts in a hash table to allow easy sharing among
107 * multiple instances of the sink. Keys are $context_name@$server_name
108 * (strings) and values should be GstPulseContext pointers.
110 struct _GstPulseContext
113 GSList *ring_buffers;
116 static GHashTable *gst_pulse_shared_contexts = NULL;
118 /* use one static main-loop for all instances
119 * this is needed to make the context sharing work as the contexts are
120 * released when releasing their parent main-loop
122 static pa_threaded_mainloop *mainloop = NULL;
123 static guint mainloop_ref_ct = 0;
125 /* lock for access to shared resources */
126 static GMutex *pa_shared_resource_mutex = NULL;
128 /* We keep a custom ringbuffer that is backed up by data allocated by
129 * pulseaudio. We must also overide the commit function to write into
130 * pulseaudio memory instead. */
131 struct _GstPulseRingBuffer
133 GstRingBuffer object;
141 pa_sample_spec sample_spec;
150 gboolean in_commit:1;
153 struct _GstPulseRingBufferClass
155 GstRingBufferClass parent_class;
158 static GType gst_pulseringbuffer_get_type (void);
159 static void gst_pulseringbuffer_finalize (GObject * object);
161 static GstRingBufferClass *ring_parent_class = NULL;
163 static gboolean gst_pulseringbuffer_open_device (GstRingBuffer * buf);
164 static gboolean gst_pulseringbuffer_close_device (GstRingBuffer * buf);
165 static gboolean gst_pulseringbuffer_acquire (GstRingBuffer * buf,
166 GstRingBufferSpec * spec);
167 static gboolean gst_pulseringbuffer_release (GstRingBuffer * buf);
168 static gboolean gst_pulseringbuffer_start (GstRingBuffer * buf);
169 static gboolean gst_pulseringbuffer_pause (GstRingBuffer * buf);
170 static gboolean gst_pulseringbuffer_stop (GstRingBuffer * buf);
171 static void gst_pulseringbuffer_clear (GstRingBuffer * buf);
172 static guint gst_pulseringbuffer_commit (GstRingBuffer * buf,
173 guint64 * sample, guchar * data, gint in_samples, gint out_samples,
176 G_DEFINE_TYPE (GstPulseRingBuffer, gst_pulseringbuffer, GST_TYPE_RING_BUFFER);
179 gst_pulsesink_init_contexts (void)
181 g_assert (pa_shared_resource_mutex == NULL);
182 pa_shared_resource_mutex = g_mutex_new ();
183 gst_pulse_shared_contexts = g_hash_table_new_full (g_str_hash, g_str_equal,
188 gst_pulseringbuffer_class_init (GstPulseRingBufferClass * klass)
190 GObjectClass *gobject_class;
191 GstRingBufferClass *gstringbuffer_class;
193 gobject_class = (GObjectClass *) klass;
194 gstringbuffer_class = (GstRingBufferClass *) klass;
196 ring_parent_class = g_type_class_peek_parent (klass);
198 gobject_class->finalize = gst_pulseringbuffer_finalize;
200 gstringbuffer_class->open_device =
201 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_open_device);
202 gstringbuffer_class->close_device =
203 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_close_device);
204 gstringbuffer_class->acquire =
205 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_acquire);
206 gstringbuffer_class->release =
207 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_release);
208 gstringbuffer_class->start = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_start);
209 gstringbuffer_class->pause = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_pause);
210 gstringbuffer_class->resume = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_start);
211 gstringbuffer_class->stop = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_stop);
212 gstringbuffer_class->clear_all =
213 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_clear);
215 gstringbuffer_class->commit = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_commit);
219 gst_pulseringbuffer_init (GstPulseRingBuffer * pbuf)
221 pbuf->stream_name = NULL;
222 pbuf->context = NULL;
225 pa_sample_spec_init (&pbuf->sample_spec);
229 pbuf->m_writable = 0;
231 pbuf->m_lastoffset = 0;
234 pbuf->in_commit = FALSE;
235 pbuf->paused = FALSE;
239 gst_pulsering_destroy_stream (GstPulseRingBuffer * pbuf)
244 /* drop shm memory buffer */
245 pa_stream_cancel_write (pbuf->stream);
247 /* reset internal variables */
250 pbuf->m_writable = 0;
252 pbuf->m_lastoffset = 0;
255 pa_stream_disconnect (pbuf->stream);
257 /* Make sure we don't get any further callbacks */
258 pa_stream_set_state_callback (pbuf->stream, NULL, NULL);
259 pa_stream_set_write_callback (pbuf->stream, NULL, NULL);
260 pa_stream_set_underflow_callback (pbuf->stream, NULL, NULL);
261 pa_stream_set_overflow_callback (pbuf->stream, NULL, NULL);
263 pa_stream_unref (pbuf->stream);
267 g_free (pbuf->stream_name);
268 pbuf->stream_name = NULL;
272 gst_pulsering_destroy_context (GstPulseRingBuffer * pbuf)
274 g_mutex_lock (pa_shared_resource_mutex);
276 GST_DEBUG_OBJECT (pbuf, "destroying ringbuffer %p", pbuf);
278 gst_pulsering_destroy_stream (pbuf);
281 pa_context_unref (pbuf->context);
282 pbuf->context = NULL;
285 if (pbuf->context_name) {
286 GstPulseContext *pctx;
288 pctx = g_hash_table_lookup (gst_pulse_shared_contexts, pbuf->context_name);
290 GST_DEBUG_OBJECT (pbuf, "releasing context with name %s, pbuf=%p, pctx=%p",
291 pbuf->context_name, pbuf, pctx);
294 pctx->ring_buffers = g_slist_remove (pctx->ring_buffers, pbuf);
295 if (pctx->ring_buffers == NULL) {
296 GST_DEBUG_OBJECT (pbuf,
297 "destroying final context with name %s, pbuf=%p, pctx=%p",
298 pbuf->context_name, pbuf, pctx);
300 pa_context_disconnect (pctx->context);
302 /* Make sure we don't get any further callbacks */
303 pa_context_set_state_callback (pctx->context, NULL, NULL);
304 pa_context_set_subscribe_callback (pctx->context, NULL, NULL);
306 g_hash_table_remove (gst_pulse_shared_contexts, pbuf->context_name);
308 pa_context_unref (pctx->context);
309 g_slice_free (GstPulseContext, pctx);
312 g_free (pbuf->context_name);
313 pbuf->context_name = NULL;
315 g_mutex_unlock (pa_shared_resource_mutex);
319 gst_pulseringbuffer_finalize (GObject * object)
321 GstPulseRingBuffer *ringbuffer;
323 ringbuffer = GST_PULSERING_BUFFER_CAST (object);
325 gst_pulsering_destroy_context (ringbuffer);
326 G_OBJECT_CLASS (ring_parent_class)->finalize (object);
330 #define CONTEXT_OK(c) ((c) && PA_CONTEXT_IS_GOOD (pa_context_get_state ((c))))
331 #define STREAM_OK(s) ((s) && PA_STREAM_IS_GOOD (pa_stream_get_state ((s))))
334 gst_pulsering_is_dead (GstPulseSink * psink, GstPulseRingBuffer * pbuf,
335 gboolean check_stream)
337 if (!CONTEXT_OK (pbuf->context))
340 if (check_stream && !STREAM_OK (pbuf->stream))
347 const gchar *err_str =
348 pbuf->context ? pa_strerror (pa_context_errno (pbuf->context)) : NULL;
349 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED, ("Disconnected: %s",
356 gst_pulsering_context_state_cb (pa_context * c, void *userdata)
358 pa_context_state_t state;
359 pa_threaded_mainloop *mainloop = (pa_threaded_mainloop *) userdata;
361 state = pa_context_get_state (c);
363 GST_LOG ("got new context state %d", state);
366 case PA_CONTEXT_READY:
367 case PA_CONTEXT_TERMINATED:
368 case PA_CONTEXT_FAILED:
369 GST_LOG ("signaling");
370 pa_threaded_mainloop_signal (mainloop, 0);
373 case PA_CONTEXT_UNCONNECTED:
374 case PA_CONTEXT_CONNECTING:
375 case PA_CONTEXT_AUTHORIZING:
376 case PA_CONTEXT_SETTING_NAME:
382 gst_pulsering_context_subscribe_cb (pa_context * c,
383 pa_subscription_event_type_t t, uint32_t idx, void *userdata)
386 GstPulseContext *pctx = (GstPulseContext *) userdata;
389 if (t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT | PA_SUBSCRIPTION_EVENT_CHANGE) &&
390 t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT | PA_SUBSCRIPTION_EVENT_NEW))
393 for (walk = pctx->ring_buffers; walk; walk = g_slist_next (walk)) {
394 GstPulseRingBuffer *pbuf = (GstPulseRingBuffer *) walk->data;
395 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
397 GST_LOG_OBJECT (psink, "type %d, idx %u", t, idx);
402 if (idx != pa_stream_get_index (pbuf->stream))
405 /* Actually this event is also triggered when other properties of
406 * the stream change that are unrelated to the volume. However it is
407 * probably cheaper to signal the change here and check for the
408 * volume when the GObject property is read instead of querying it always. */
410 /* inform streaming thread to notify */
411 g_atomic_int_compare_and_exchange (&psink->notify, 0, 1);
415 /* will be called when the device should be opened. In this case we will connect
416 * to the server. We should not try to open any streams in this state. */
418 gst_pulseringbuffer_open_device (GstRingBuffer * buf)
421 GstPulseRingBuffer *pbuf;
422 GstPulseContext *pctx;
423 pa_mainloop_api *api;
424 gboolean need_unlock_shared;
426 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
427 pbuf = GST_PULSERING_BUFFER_CAST (buf);
429 g_assert (!pbuf->stream);
430 g_assert (psink->client_name);
433 pbuf->context_name = g_strdup_printf ("%s@%s", psink->client_name,
436 pbuf->context_name = g_strdup (psink->client_name);
438 pa_threaded_mainloop_lock (mainloop);
440 g_mutex_lock (pa_shared_resource_mutex);
441 need_unlock_shared = TRUE;
443 pctx = g_hash_table_lookup (gst_pulse_shared_contexts, pbuf->context_name);
445 pctx = g_slice_new0 (GstPulseContext);
447 /* get the mainloop api and create a context */
448 GST_INFO_OBJECT (psink, "new context with name %s, pbuf=%p, pctx=%p",
449 pbuf->context_name, pbuf, pctx);
450 api = pa_threaded_mainloop_get_api (mainloop);
451 if (!(pctx->context = pa_context_new (api, pbuf->context_name)))
454 pctx->ring_buffers = g_slist_prepend (pctx->ring_buffers, pbuf);
455 g_hash_table_insert (gst_pulse_shared_contexts,
456 g_strdup (pbuf->context_name), (gpointer) pctx);
457 /* register some essential callbacks */
458 pa_context_set_state_callback (pctx->context,
459 gst_pulsering_context_state_cb, mainloop);
460 pa_context_set_subscribe_callback (pctx->context,
461 gst_pulsering_context_subscribe_cb, pctx);
463 /* try to connect to the server and wait for completion, we don't want to
464 * autospawn a deamon */
465 GST_LOG_OBJECT (psink, "connect to server %s",
466 GST_STR_NULL (psink->server));
467 if (pa_context_connect (pctx->context, psink->server,
468 PA_CONTEXT_NOAUTOSPAWN, NULL) < 0)
471 GST_INFO_OBJECT (psink,
472 "reusing shared context with name %s, pbuf=%p, pctx=%p",
473 pbuf->context_name, pbuf, pctx);
474 pctx->ring_buffers = g_slist_prepend (pctx->ring_buffers, pbuf);
477 g_mutex_unlock (pa_shared_resource_mutex);
478 need_unlock_shared = FALSE;
480 /* context created or shared okay */
481 pbuf->context = pa_context_ref (pctx->context);
484 pa_context_state_t state;
486 state = pa_context_get_state (pbuf->context);
488 GST_LOG_OBJECT (psink, "context state is now %d", state);
490 if (!PA_CONTEXT_IS_GOOD (state))
493 if (state == PA_CONTEXT_READY)
496 /* Wait until the context is ready */
497 GST_LOG_OBJECT (psink, "waiting..");
498 pa_threaded_mainloop_wait (mainloop);
501 GST_LOG_OBJECT (psink, "opened the device");
503 pa_threaded_mainloop_unlock (mainloop);
510 if (need_unlock_shared)
511 g_mutex_unlock (pa_shared_resource_mutex);
512 gst_pulsering_destroy_context (pbuf);
513 pa_threaded_mainloop_unlock (mainloop);
518 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
519 ("Failed to create context"), (NULL));
520 g_slice_free (GstPulseContext, pctx);
521 goto unlock_and_fail;
525 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED, ("Failed to connect: %s",
526 pa_strerror (pa_context_errno (pctx->context))), (NULL));
527 goto unlock_and_fail;
531 /* close the device */
533 gst_pulseringbuffer_close_device (GstRingBuffer * buf)
536 GstPulseRingBuffer *pbuf;
538 pbuf = GST_PULSERING_BUFFER_CAST (buf);
539 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
541 GST_LOG_OBJECT (psink, "closing device");
543 pa_threaded_mainloop_lock (mainloop);
544 gst_pulsering_destroy_context (pbuf);
545 pa_threaded_mainloop_unlock (mainloop);
547 GST_LOG_OBJECT (psink, "closed device");
553 gst_pulsering_stream_state_cb (pa_stream * s, void *userdata)
556 GstPulseRingBuffer *pbuf;
557 pa_stream_state_t state;
559 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
560 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
562 state = pa_stream_get_state (s);
563 GST_LOG_OBJECT (psink, "got new stream state %d", state);
566 case PA_STREAM_READY:
567 case PA_STREAM_FAILED:
568 case PA_STREAM_TERMINATED:
569 GST_LOG_OBJECT (psink, "signaling");
570 pa_threaded_mainloop_signal (mainloop, 0);
572 case PA_STREAM_UNCONNECTED:
573 case PA_STREAM_CREATING:
579 gst_pulsering_stream_request_cb (pa_stream * s, size_t length, void *userdata)
583 GstPulseRingBuffer *pbuf;
585 rbuf = GST_RING_BUFFER_CAST (userdata);
586 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
587 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
589 GST_LOG_OBJECT (psink, "got request for length %" G_GSIZE_FORMAT, length);
591 if (pbuf->in_commit && (length >= rbuf->spec.segsize)) {
592 /* only signal when we are waiting in the commit thread
593 * and got request for atleast a segment */
594 pa_threaded_mainloop_signal (mainloop, 0);
599 gst_pulsering_stream_underflow_cb (pa_stream * s, void *userdata)
602 GstPulseRingBuffer *pbuf;
604 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
605 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
607 GST_WARNING_OBJECT (psink, "Got underflow");
611 gst_pulsering_stream_overflow_cb (pa_stream * s, void *userdata)
614 GstPulseRingBuffer *pbuf;
616 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
617 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
619 GST_WARNING_OBJECT (psink, "Got overflow");
623 gst_pulsering_stream_latency_cb (pa_stream * s, void *userdata)
626 GstPulseRingBuffer *pbuf;
627 const pa_timing_info *info;
630 info = pa_stream_get_timing_info (s);
632 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
633 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
636 GST_LOG_OBJECT (psink, "latency update (information unknown)");
639 sink_usec = info->configured_sink_usec;
641 GST_LOG_OBJECT (psink,
642 "latency_update, %" G_GUINT64_FORMAT ", %d:%" G_GINT64_FORMAT ", %d:%"
643 G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT,
644 GST_TIMEVAL_TO_TIME (info->timestamp), info->write_index_corrupt,
645 info->write_index, info->read_index_corrupt, info->read_index,
646 info->sink_usec, sink_usec);
650 gst_pulsering_stream_suspended_cb (pa_stream * p, void *userdata)
653 GstPulseRingBuffer *pbuf;
655 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
656 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
658 if (pa_stream_is_suspended (p))
659 GST_DEBUG_OBJECT (psink, "stream suspended");
661 GST_DEBUG_OBJECT (psink, "stream resumed");
665 gst_pulsering_stream_started_cb (pa_stream * p, void *userdata)
668 GstPulseRingBuffer *pbuf;
670 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
671 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
673 GST_DEBUG_OBJECT (psink, "stream started");
677 gst_pulsering_stream_event_cb (pa_stream * p, const char *name,
678 pa_proplist * pl, void *userdata)
681 GstPulseRingBuffer *pbuf;
683 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
684 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
686 if (!strcmp (name, PA_STREAM_EVENT_REQUEST_CORK)) {
687 /* the stream wants to PAUSE, post a message for the application. */
688 GST_DEBUG_OBJECT (psink, "got request for CORK");
689 gst_element_post_message (GST_ELEMENT_CAST (psink),
690 gst_message_new_request_state (GST_OBJECT_CAST (psink),
693 } else if (!strcmp (name, PA_STREAM_EVENT_REQUEST_UNCORK)) {
694 GST_DEBUG_OBJECT (psink, "got request for UNCORK");
695 gst_element_post_message (GST_ELEMENT_CAST (psink),
696 gst_message_new_request_state (GST_OBJECT_CAST (psink),
699 GST_DEBUG_OBJECT (psink, "got unknown event %s", name);
703 /* This method should create a new stream of the given @spec. No playback should
704 * start yet so we start in the corked state. */
706 gst_pulseringbuffer_acquire (GstRingBuffer * buf, GstRingBufferSpec * spec)
709 GstPulseRingBuffer *pbuf;
710 pa_buffer_attr wanted;
711 const pa_buffer_attr *actual;
712 pa_channel_map channel_map;
713 pa_operation *o = NULL;
714 #ifdef HAVE_PULSE_0_9_20
717 pa_cvolume *pv = NULL;
718 pa_stream_flags_t flags;
720 GstAudioClock *clock;
722 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
723 pbuf = GST_PULSERING_BUFFER_CAST (buf);
725 GST_LOG_OBJECT (psink, "creating sample spec");
726 /* convert the gstreamer sample spec to the pulseaudio format */
727 if (!gst_pulse_fill_sample_spec (spec, &pbuf->sample_spec))
730 pa_threaded_mainloop_lock (mainloop);
732 /* we need a context and a no stream */
733 g_assert (pbuf->context);
734 g_assert (!pbuf->stream);
736 /* enable event notifications */
737 GST_LOG_OBJECT (psink, "subscribing to context events");
738 if (!(o = pa_context_subscribe (pbuf->context,
739 PA_SUBSCRIPTION_MASK_SINK_INPUT, NULL, NULL)))
740 goto subscribe_failed;
742 pa_operation_unref (o);
744 /* initialize the channel map */
745 gst_pulse_gst_to_channel_map (&channel_map, spec);
747 /* find a good name for the stream */
748 if (psink->stream_name)
749 name = psink->stream_name;
751 name = "Playback Stream";
753 /* create a stream */
754 GST_LOG_OBJECT (psink, "creating stream with name %s", name);
755 if (!(pbuf->stream = pa_stream_new_with_proplist (pbuf->context, name,
756 &pbuf->sample_spec, &channel_map, psink->proplist)))
759 /* install essential callbacks */
760 pa_stream_set_state_callback (pbuf->stream,
761 gst_pulsering_stream_state_cb, pbuf);
762 pa_stream_set_write_callback (pbuf->stream,
763 gst_pulsering_stream_request_cb, pbuf);
764 pa_stream_set_underflow_callback (pbuf->stream,
765 gst_pulsering_stream_underflow_cb, pbuf);
766 pa_stream_set_overflow_callback (pbuf->stream,
767 gst_pulsering_stream_overflow_cb, pbuf);
768 pa_stream_set_latency_update_callback (pbuf->stream,
769 gst_pulsering_stream_latency_cb, pbuf);
770 pa_stream_set_suspended_callback (pbuf->stream,
771 gst_pulsering_stream_suspended_cb, pbuf);
772 pa_stream_set_started_callback (pbuf->stream,
773 gst_pulsering_stream_started_cb, pbuf);
774 pa_stream_set_event_callback (pbuf->stream,
775 gst_pulsering_stream_event_cb, pbuf);
777 /* buffering requirements. When setting prebuf to 0, the stream will not pause
778 * when we cause an underrun, which causes time to continue. */
779 memset (&wanted, 0, sizeof (wanted));
780 wanted.tlength = spec->segtotal * spec->segsize;
781 wanted.maxlength = -1;
783 wanted.minreq = spec->segsize;
785 GST_INFO_OBJECT (psink, "tlength: %d", wanted.tlength);
786 GST_INFO_OBJECT (psink, "maxlength: %d", wanted.maxlength);
787 GST_INFO_OBJECT (psink, "prebuf: %d", wanted.prebuf);
788 GST_INFO_OBJECT (psink, "minreq: %d", wanted.minreq);
790 #ifdef HAVE_PULSE_0_9_20
791 /* configure volume when we changed it, else we leave the default */
792 if (psink->volume_set) {
793 GST_LOG_OBJECT (psink, "have volume of %f", psink->volume);
795 gst_pulse_cvolume_from_linear (pv, pbuf->sample_spec.channels,
802 /* construct the flags */
803 flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE |
804 PA_STREAM_ADJUST_LATENCY | PA_STREAM_START_CORKED;
806 if (psink->mute_set && psink->mute)
807 flags |= PA_STREAM_START_MUTED;
809 /* we always start corked (see flags above) */
812 /* try to connect now */
813 GST_LOG_OBJECT (psink, "connect for playback to device %s",
814 GST_STR_NULL (psink->device));
815 if (pa_stream_connect_playback (pbuf->stream, psink->device,
816 &wanted, flags, pv, NULL) < 0)
819 /* our clock will now start from 0 again */
820 clock = GST_AUDIO_CLOCK (GST_BASE_AUDIO_SINK (psink)->provided_clock);
821 gst_audio_clock_reset (clock, 0);
824 pa_stream_state_t state;
826 state = pa_stream_get_state (pbuf->stream);
828 GST_LOG_OBJECT (psink, "stream state is now %d", state);
830 if (!PA_STREAM_IS_GOOD (state))
833 if (state == PA_STREAM_READY)
836 /* Wait until the stream is ready */
837 pa_threaded_mainloop_wait (mainloop);
840 /* After we passed the volume off of to PA we never want to set it
841 again, since it is PA's job to save/restore volumes. */
842 psink->volume_set = psink->mute_set = FALSE;
844 GST_LOG_OBJECT (psink, "stream is acquired now");
846 /* get the actual buffering properties now */
847 actual = pa_stream_get_buffer_attr (pbuf->stream);
849 GST_INFO_OBJECT (psink, "tlength: %d (wanted: %d)", actual->tlength,
851 GST_INFO_OBJECT (psink, "maxlength: %d", actual->maxlength);
852 GST_INFO_OBJECT (psink, "prebuf: %d", actual->prebuf);
853 GST_INFO_OBJECT (psink, "minreq: %d (wanted %d)", actual->minreq,
856 spec->segsize = actual->minreq;
857 spec->segtotal = actual->tlength / spec->segsize;
859 pa_threaded_mainloop_unlock (mainloop);
866 gst_pulsering_destroy_stream (pbuf);
867 pa_threaded_mainloop_unlock (mainloop);
873 GST_ELEMENT_ERROR (psink, RESOURCE, SETTINGS,
874 ("Invalid sample specification."), (NULL));
879 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
880 ("pa_context_subscribe() failed: %s",
881 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
882 goto unlock_and_fail;
886 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
887 ("Failed to create stream: %s",
888 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
889 goto unlock_and_fail;
893 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
894 ("Failed to connect stream: %s",
895 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
896 goto unlock_and_fail;
900 /* free the stream that we acquired before */
902 gst_pulseringbuffer_release (GstRingBuffer * buf)
904 GstPulseRingBuffer *pbuf;
906 pbuf = GST_PULSERING_BUFFER_CAST (buf);
908 pa_threaded_mainloop_lock (mainloop);
909 gst_pulsering_destroy_stream (pbuf);
910 pa_threaded_mainloop_unlock (mainloop);
916 gst_pulsering_success_cb (pa_stream * s, int success, void *userdata)
918 pa_threaded_mainloop_signal (mainloop, 0);
921 /* update the corked state of a stream, must be called with the mainloop
924 gst_pulsering_set_corked (GstPulseRingBuffer * pbuf, gboolean corked,
927 pa_operation *o = NULL;
929 gboolean res = FALSE;
931 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
933 GST_DEBUG_OBJECT (psink, "setting corked state to %d", corked);
934 if (pbuf->corked != corked) {
935 if (!(o = pa_stream_cork (pbuf->stream, corked,
936 gst_pulsering_success_cb, pbuf)))
939 while (wait && pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
940 pa_threaded_mainloop_wait (mainloop);
941 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
944 pbuf->corked = corked;
946 GST_DEBUG_OBJECT (psink, "skipping, already in requested state");
952 pa_operation_unref (o);
959 GST_DEBUG_OBJECT (psink, "the server is dead");
964 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
965 ("pa_stream_cork() failed: %s",
966 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
972 gst_pulseringbuffer_clear (GstRingBuffer * buf)
975 GstPulseRingBuffer *pbuf;
976 pa_operation *o = NULL;
978 pbuf = GST_PULSERING_BUFFER_CAST (buf);
979 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
981 pa_threaded_mainloop_lock (mainloop);
982 GST_DEBUG_OBJECT (psink, "clearing");
984 /* don't wait for the flush to complete */
985 if ((o = pa_stream_flush (pbuf->stream, NULL, pbuf)))
986 pa_operation_unref (o);
988 pa_threaded_mainloop_unlock (mainloop);
991 /* called from pulse with the mainloop lock */
993 mainloop_enter_defer_cb (pa_mainloop_api * api, void *userdata)
995 GstPulseSink *pulsesink = GST_PULSESINK (userdata);
999 g_value_init (&val, G_TYPE_POINTER);
1000 g_value_set_pointer (&val, g_thread_self ());
1002 GST_DEBUG_OBJECT (pulsesink, "posting ENTER stream status");
1003 message = gst_message_new_stream_status (GST_OBJECT (pulsesink),
1004 GST_STREAM_STATUS_TYPE_ENTER, GST_ELEMENT (pulsesink));
1005 gst_message_set_stream_status_object (message, &val);
1007 gst_element_post_message (GST_ELEMENT (pulsesink), message);
1009 g_return_if_fail (pulsesink->defer_pending);
1010 pulsesink->defer_pending--;
1011 pa_threaded_mainloop_signal (mainloop, 0);
1014 /* start/resume playback ASAP, we don't uncork here but in the commit method */
1016 gst_pulseringbuffer_start (GstRingBuffer * buf)
1018 GstPulseSink *psink;
1019 GstPulseRingBuffer *pbuf;
1021 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1022 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1024 pa_threaded_mainloop_lock (mainloop);
1026 GST_DEBUG_OBJECT (psink, "scheduling stream status");
1027 psink->defer_pending++;
1028 pa_mainloop_api_once (pa_threaded_mainloop_get_api (mainloop),
1029 mainloop_enter_defer_cb, psink);
1031 GST_DEBUG_OBJECT (psink, "starting");
1032 pbuf->paused = FALSE;
1034 /* EOS needs running clock */
1035 if (GST_BASE_SINK_CAST (psink)->eos ||
1036 g_atomic_int_get (&GST_BASE_AUDIO_SINK (psink)->eos_rendering))
1037 gst_pulsering_set_corked (pbuf, FALSE, FALSE);
1039 pa_threaded_mainloop_unlock (mainloop);
1044 /* pause/stop playback ASAP */
1046 gst_pulseringbuffer_pause (GstRingBuffer * buf)
1048 GstPulseSink *psink;
1049 GstPulseRingBuffer *pbuf;
1052 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1053 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1055 pa_threaded_mainloop_lock (mainloop);
1056 GST_DEBUG_OBJECT (psink, "pausing and corking");
1057 /* make sure the commit method stops writing */
1058 pbuf->paused = TRUE;
1059 res = gst_pulsering_set_corked (pbuf, TRUE, TRUE);
1060 if (pbuf->in_commit) {
1061 /* we are waiting in a commit, signal */
1062 GST_DEBUG_OBJECT (psink, "signal commit");
1063 pa_threaded_mainloop_signal (mainloop, 0);
1065 pa_threaded_mainloop_unlock (mainloop);
1070 /* called from pulse with the mainloop lock */
1072 mainloop_leave_defer_cb (pa_mainloop_api * api, void *userdata)
1074 GstPulseSink *pulsesink = GST_PULSESINK (userdata);
1075 GstMessage *message;
1078 g_value_init (&val, G_TYPE_POINTER);
1079 g_value_set_pointer (&val, g_thread_self ());
1081 GST_DEBUG_OBJECT (pulsesink, "posting LEAVE stream status");
1082 message = gst_message_new_stream_status (GST_OBJECT (pulsesink),
1083 GST_STREAM_STATUS_TYPE_LEAVE, GST_ELEMENT (pulsesink));
1084 gst_message_set_stream_status_object (message, &val);
1085 gst_element_post_message (GST_ELEMENT (pulsesink), message);
1087 g_return_if_fail (pulsesink->defer_pending);
1088 pulsesink->defer_pending--;
1089 pa_threaded_mainloop_signal (mainloop, 0);
1092 /* stop playback, we flush everything. */
1094 gst_pulseringbuffer_stop (GstRingBuffer * buf)
1096 GstPulseSink *psink;
1097 GstPulseRingBuffer *pbuf;
1098 gboolean res = FALSE;
1099 pa_operation *o = NULL;
1101 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1102 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1104 pa_threaded_mainloop_lock (mainloop);
1105 pbuf->paused = TRUE;
1106 res = gst_pulsering_set_corked (pbuf, TRUE, TRUE);
1107 /* Inform anyone waiting in _commit() call that it shall wakeup */
1108 if (pbuf->in_commit) {
1109 GST_DEBUG_OBJECT (psink, "signal commit thread");
1110 pa_threaded_mainloop_signal (mainloop, 0);
1113 /* then try to flush, it's not fatal when this fails */
1114 GST_DEBUG_OBJECT (psink, "flushing");
1115 if ((o = pa_stream_flush (pbuf->stream, gst_pulsering_success_cb, pbuf))) {
1116 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1117 GST_DEBUG_OBJECT (psink, "wait for completion");
1118 pa_threaded_mainloop_wait (mainloop);
1119 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
1122 GST_DEBUG_OBJECT (psink, "flush completed");
1128 pa_operation_cancel (o);
1129 pa_operation_unref (o);
1132 GST_DEBUG_OBJECT (psink, "scheduling stream status");
1133 psink->defer_pending++;
1134 pa_mainloop_api_once (pa_threaded_mainloop_get_api (mainloop),
1135 mainloop_leave_defer_cb, psink);
1137 pa_threaded_mainloop_unlock (mainloop);
1144 GST_DEBUG_OBJECT (psink, "the server is dead");
1149 /* in_samples >= out_samples, rate > 1.0 */
1150 #define FWD_UP_SAMPLES(s,se,d,de) \
1152 guint8 *sb = s, *db = d; \
1153 while (s <= se && d < de) { \
1154 memcpy (d, s, bps); \
1157 if ((*accum << 1) >= inr) { \
1162 in_samples -= (s - sb)/bps; \
1163 out_samples -= (d - db)/bps; \
1164 GST_DEBUG ("fwd_up end %d/%d",*accum,*toprocess); \
1167 /* out_samples > in_samples, for rates smaller than 1.0 */
1168 #define FWD_DOWN_SAMPLES(s,se,d,de) \
1170 guint8 *sb = s, *db = d; \
1171 while (s <= se && d < de) { \
1172 memcpy (d, s, bps); \
1175 if ((*accum << 1) >= outr) { \
1180 in_samples -= (s - sb)/bps; \
1181 out_samples -= (d - db)/bps; \
1182 GST_DEBUG ("fwd_down end %d/%d",*accum,*toprocess); \
1185 #define REV_UP_SAMPLES(s,se,d,de) \
1187 guint8 *sb = se, *db = d; \
1188 while (s <= se && d < de) { \
1189 memcpy (d, se, bps); \
1192 while (d < de && (*accum << 1) >= inr) { \
1197 in_samples -= (sb - se)/bps; \
1198 out_samples -= (d - db)/bps; \
1199 GST_DEBUG ("rev_up end %d/%d",*accum,*toprocess); \
1202 #define REV_DOWN_SAMPLES(s,se,d,de) \
1204 guint8 *sb = se, *db = d; \
1205 while (s <= se && d < de) { \
1206 memcpy (d, se, bps); \
1209 while (s <= se && (*accum << 1) >= outr) { \
1214 in_samples -= (sb - se)/bps; \
1215 out_samples -= (d - db)/bps; \
1216 GST_DEBUG ("rev_down end %d/%d",*accum,*toprocess); \
1220 /* our custom commit function because we write into the buffer of pulseaudio
1221 * instead of keeping our own buffer */
1223 gst_pulseringbuffer_commit (GstRingBuffer * buf, guint64 * sample,
1224 guchar * data, gint in_samples, gint out_samples, gint * accum)
1226 GstPulseSink *psink;
1227 GstPulseRingBuffer *pbuf;
1232 gint inr, outr, bps;
1236 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1237 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1239 /* FIXME post message rather than using a signal (as mixer interface) */
1240 if (g_atomic_int_compare_and_exchange (&psink->notify, 1, 0)) {
1241 g_object_notify (G_OBJECT (psink), "volume");
1242 g_object_notify (G_OBJECT (psink), "mute");
1245 /* make sure the ringbuffer is started */
1246 if (G_UNLIKELY (g_atomic_int_get (&buf->state) !=
1247 GST_RING_BUFFER_STATE_STARTED)) {
1248 /* see if we are allowed to start it */
1249 if (G_UNLIKELY (g_atomic_int_get (&buf->may_start) == FALSE))
1252 GST_DEBUG_OBJECT (buf, "start!");
1253 if (!gst_ring_buffer_start (buf))
1257 pa_threaded_mainloop_lock (mainloop);
1258 GST_DEBUG_OBJECT (psink, "entering commit");
1259 pbuf->in_commit = TRUE;
1261 bps = buf->spec.bytes_per_sample;
1262 bufsize = buf->spec.segsize * buf->spec.segtotal;
1264 /* our toy resampler for trick modes */
1265 reverse = out_samples < 0;
1266 out_samples = ABS (out_samples);
1268 if (in_samples >= out_samples)
1269 toprocess = &in_samples;
1271 toprocess = &out_samples;
1273 inr = in_samples - 1;
1274 outr = out_samples - 1;
1276 GST_DEBUG_OBJECT (psink, "in %d, out %d", inr, outr);
1278 /* data_end points to the last sample we have to write, not past it. This is
1279 * needed to properly handle reverse playback: it points to the last sample. */
1280 data_end = data + (bps * inr);
1285 /* offset is in bytes */
1286 offset = *sample * bps;
1288 while (*toprocess > 0) {
1292 GST_LOG_OBJECT (psink,
1293 "need to write %d samples at offset %" G_GINT64_FORMAT, *toprocess,
1296 if (offset != pbuf->m_lastoffset)
1297 GST_LOG_OBJECT (psink, "discontinuity, offset is %" G_GINT64_FORMAT ", "
1298 "last offset was %" G_GINT64_FORMAT, offset, pbuf->m_lastoffset);
1300 towrite = out_samples * bps;
1302 /* Only ever write segsize bytes at once. This will
1303 * also limit the PA shm buffer to segsize
1305 if (towrite > buf->spec.segsize)
1306 towrite = buf->spec.segsize;
1308 if ((pbuf->m_writable < towrite) || (offset != pbuf->m_lastoffset)) {
1309 /* if no room left or discontinuity in offset,
1310 we need to flush data and get a new buffer */
1312 /* flush the buffer if possible */
1313 if ((pbuf->m_data != NULL) && (pbuf->m_towrite > 0)) {
1315 GST_LOG_OBJECT (psink,
1316 "flushing %u samples at offset %" G_GINT64_FORMAT,
1317 (guint) pbuf->m_towrite / bps, pbuf->m_offset);
1319 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1320 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1324 pbuf->m_towrite = 0;
1325 pbuf->m_offset = offset; /* keep track of current offset */
1327 /* get a buffer to write in for now on */
1329 pbuf->m_writable = pa_stream_writable_size (pbuf->stream);
1331 if (pbuf->m_writable == (size_t) - 1)
1332 goto writable_size_failed;
1334 pbuf->m_writable /= bps;
1335 pbuf->m_writable *= bps; /* handle only complete samples */
1337 if (pbuf->m_writable >= towrite)
1340 /* see if we need to uncork because we have no free space */
1342 if (!gst_pulsering_set_corked (pbuf, FALSE, FALSE))
1346 /* we can't write a single byte, wait a bit */
1347 GST_LOG_OBJECT (psink, "waiting for free space");
1348 pa_threaded_mainloop_wait (mainloop);
1354 /* make sure we only buffer up latency-time samples */
1355 if (pbuf->m_writable > buf->spec.segsize) {
1356 /* limit buffering to latency-time value */
1357 pbuf->m_writable = buf->spec.segsize;
1359 GST_LOG_OBJECT (psink, "Limiting buffering to %" G_GSIZE_FORMAT,
1363 GST_LOG_OBJECT (psink, "requesting %" G_GSIZE_FORMAT " bytes of "
1364 "shared memory", pbuf->m_writable);
1366 if (pa_stream_begin_write (pbuf->stream, &pbuf->m_data,
1367 &pbuf->m_writable) < 0) {
1368 GST_LOG_OBJECT (psink, "pa_stream_begin_write() failed");
1369 goto writable_size_failed;
1372 GST_LOG_OBJECT (psink, "got %" G_GSIZE_FORMAT " bytes of shared memory",
1375 /* Just to make sure that we didn't get more than requested */
1376 if (pbuf->m_writable > buf->spec.segsize) {
1377 /* limit buffering to latency-time value */
1378 pbuf->m_writable = buf->spec.segsize;
1382 if (pbuf->m_writable < towrite)
1383 towrite = pbuf->m_writable;
1384 avail = towrite / bps;
1386 GST_LOG_OBJECT (psink, "writing %u samples at offset %" G_GUINT64_FORMAT,
1387 (guint) avail, offset);
1389 if (G_LIKELY (inr == outr && !reverse)) {
1390 /* no rate conversion, simply write out the samples */
1391 /* copy the data into internal buffer */
1393 memcpy ((guint8 *) pbuf->m_data + pbuf->m_towrite, data, towrite);
1394 pbuf->m_towrite += towrite;
1395 pbuf->m_writable -= towrite;
1398 in_samples -= avail;
1399 out_samples -= avail;
1401 guint8 *dest, *d, *d_end;
1403 /* write into the PulseAudio shm buffer */
1404 dest = d = (guint8 *) pbuf->m_data + pbuf->m_towrite;
1405 d_end = d + towrite;
1409 /* forward speed up */
1410 FWD_UP_SAMPLES (data, data_end, d, d_end);
1412 /* forward slow down */
1413 FWD_DOWN_SAMPLES (data, data_end, d, d_end);
1416 /* reverse speed up */
1417 REV_UP_SAMPLES (data, data_end, d, d_end);
1419 /* reverse slow down */
1420 REV_DOWN_SAMPLES (data, data_end, d, d_end);
1422 /* see what we have left to write */
1423 towrite = (d - dest);
1424 pbuf->m_towrite += towrite;
1425 pbuf->m_writable -= towrite;
1427 avail = towrite / bps;
1430 /* flush the buffer if it's full */
1431 if ((pbuf->m_data != NULL) && (pbuf->m_towrite > 0)
1432 && (pbuf->m_writable == 0)) {
1433 GST_LOG_OBJECT (psink, "flushing %u samples at offset %" G_GINT64_FORMAT,
1434 (guint) pbuf->m_towrite / bps, pbuf->m_offset);
1436 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1437 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1440 pbuf->m_towrite = 0;
1441 pbuf->m_offset = offset + towrite; /* keep track of current offset */
1445 offset += avail * bps;
1446 pbuf->m_lastoffset = offset;
1448 /* check if we need to uncork after writing the samples */
1450 const pa_timing_info *info;
1452 if ((info = pa_stream_get_timing_info (pbuf->stream))) {
1453 GST_LOG_OBJECT (psink,
1454 "read_index at %" G_GUINT64_FORMAT ", offset %" G_GINT64_FORMAT,
1455 info->read_index, offset);
1457 /* we uncork when the read_index is too far behind the offset we need
1459 if (info->read_index + bufsize <= offset) {
1460 if (!gst_pulsering_set_corked (pbuf, FALSE, FALSE))
1464 GST_LOG_OBJECT (psink, "no timing info available yet");
1468 /* we consumed all samples here */
1469 data = data_end + bps;
1471 pbuf->in_commit = FALSE;
1472 pa_threaded_mainloop_unlock (mainloop);
1475 result = inr - ((data_end - data) / bps);
1476 GST_LOG_OBJECT (psink, "wrote %d samples", result);
1483 pbuf->in_commit = FALSE;
1484 GST_LOG_OBJECT (psink, "we are reset");
1485 pa_threaded_mainloop_unlock (mainloop);
1490 GST_LOG_OBJECT (psink, "we can not start");
1495 GST_LOG_OBJECT (psink, "failed to start the ringbuffer");
1500 pbuf->in_commit = FALSE;
1501 GST_ERROR_OBJECT (psink, "uncork failed");
1502 pa_threaded_mainloop_unlock (mainloop);
1507 pbuf->in_commit = FALSE;
1508 GST_LOG_OBJECT (psink, "we are paused");
1509 pa_threaded_mainloop_unlock (mainloop);
1512 writable_size_failed:
1514 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1515 ("pa_stream_writable_size() failed: %s",
1516 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1517 goto unlock_and_fail;
1521 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1522 ("pa_stream_write() failed: %s",
1523 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1524 goto unlock_and_fail;
1528 /* write pending local samples, must be called with the mainloop lock */
1530 gst_pulsering_flush (GstPulseRingBuffer * pbuf)
1532 GstPulseSink *psink;
1534 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1535 GST_DEBUG_OBJECT (psink, "entering flush");
1537 /* flush the buffer if possible */
1538 if (pbuf->stream && (pbuf->m_data != NULL) && (pbuf->m_towrite > 0)) {
1539 #ifndef GST_DISABLE_GST_DEBUG
1542 bps = (GST_RING_BUFFER_CAST (pbuf))->spec.bytes_per_sample;
1543 GST_LOG_OBJECT (psink,
1544 "flushing %u samples at offset %" G_GINT64_FORMAT,
1545 (guint) pbuf->m_towrite / bps, pbuf->m_offset);
1548 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1549 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1553 pbuf->m_towrite = 0;
1554 pbuf->m_offset += pbuf->m_towrite; /* keep track of current offset */
1563 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1564 ("pa_stream_write() failed: %s",
1565 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1570 static void gst_pulsesink_set_property (GObject * object, guint prop_id,
1571 const GValue * value, GParamSpec * pspec);
1572 static void gst_pulsesink_get_property (GObject * object, guint prop_id,
1573 GValue * value, GParamSpec * pspec);
1574 static void gst_pulsesink_finalize (GObject * object);
1576 static gboolean gst_pulsesink_event (GstBaseSink * sink, GstEvent * event);
1578 static GstStateChangeReturn gst_pulsesink_change_state (GstElement * element,
1579 GstStateChange transition);
1581 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
1582 # define ENDIANNESS "LITTLE_ENDIAN, BIG_ENDIAN"
1584 # define ENDIANNESS "BIG_ENDIAN, LITTLE_ENDIAN"
1587 static GstStaticPadTemplate pad_template = GST_STATIC_PAD_TEMPLATE ("sink",
1590 GST_STATIC_CAPS ("audio/x-raw-int, "
1591 "endianness = (int) { " ENDIANNESS " }, "
1592 "signed = (boolean) TRUE, "
1593 "width = (int) 16, "
1594 "depth = (int) 16, "
1595 "rate = (int) [ 1, MAX ], "
1596 "channels = (int) [ 1, 32 ];"
1597 "audio/x-raw-float, "
1598 "endianness = (int) { " ENDIANNESS " }, "
1599 "width = (int) 32, "
1600 "rate = (int) [ 1, MAX ], "
1601 "channels = (int) [ 1, 32 ];"
1603 "endianness = (int) { " ENDIANNESS " }, "
1604 "signed = (boolean) TRUE, "
1605 "width = (int) 32, "
1606 "depth = (int) 32, "
1607 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 32 ];"
1608 #ifdef HAVE_PULSE_0_9_15
1610 "endianness = (int) { " ENDIANNESS " }, "
1611 "signed = (boolean) TRUE, "
1612 "width = (int) 24, "
1613 "depth = (int) 24, "
1614 "rate = (int) [ 1, MAX ], "
1615 "channels = (int) [ 1, 32 ];"
1617 "endianness = (int) { " ENDIANNESS " }, "
1618 "signed = (boolean) TRUE, "
1619 "width = (int) 32, "
1620 "depth = (int) 24, "
1621 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 32 ];"
1624 "signed = (boolean) FALSE, "
1627 "rate = (int) [ 1, MAX ], "
1628 "channels = (int) [ 1, 32 ];"
1630 "rate = (int) [ 1, MAX], "
1631 "channels = (int) [ 1, 32 ];"
1633 "rate = (int) [ 1, MAX], " "channels = (int) [ 1, 32 ]")
1636 GST_IMPLEMENT_PULSEPROBE_METHODS (GstPulseSink, gst_pulsesink);
1638 #define _do_init(type) \
1639 gst_pulsesink_init_contexts (); \
1640 gst_pulsesink_init_interfaces (type);
1642 #define gst_pulsesink_parent_class parent_class
1643 G_DEFINE_TYPE_WITH_CODE (GstPulseSink, gst_pulsesink, GST_TYPE_BASE_AUDIO_SINK,
1644 gst_pulsesink_init_contexts ();
1645 G_IMPLEMENT_INTERFACE (GST_TYPE_PROPERTY_PROBE,
1646 gst_pulsesink_property_probe_interface_init);
1647 G_IMPLEMENT_INTERFACE (GST_TYPE_STREAM_VOLUME, NULL)
1650 static GstRingBuffer *
1651 gst_pulsesink_create_ringbuffer (GstBaseAudioSink * sink)
1653 GstRingBuffer *buffer;
1655 GST_DEBUG_OBJECT (sink, "creating ringbuffer");
1656 buffer = g_object_new (GST_TYPE_PULSERING_BUFFER, NULL);
1657 GST_DEBUG_OBJECT (sink, "created ringbuffer @%p", buffer);
1663 gst_pulsesink_class_init (GstPulseSinkClass * klass)
1665 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1666 GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
1667 GstBaseSinkClass *bc;
1668 GstBaseAudioSinkClass *gstaudiosink_class = GST_BASE_AUDIO_SINK_CLASS (klass);
1669 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1671 gobject_class->finalize = gst_pulsesink_finalize;
1672 gobject_class->set_property = gst_pulsesink_set_property;
1673 gobject_class->get_property = gst_pulsesink_get_property;
1675 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_pulsesink_event);
1677 /* restore the original basesink pull methods */
1678 bc = g_type_class_peek (GST_TYPE_BASE_SINK);
1679 gstbasesink_class->activate_pull = GST_DEBUG_FUNCPTR (bc->activate_pull);
1681 gstelement_class->change_state =
1682 GST_DEBUG_FUNCPTR (gst_pulsesink_change_state);
1684 gstaudiosink_class->create_ringbuffer =
1685 GST_DEBUG_FUNCPTR (gst_pulsesink_create_ringbuffer);
1687 /* Overwrite GObject fields */
1688 g_object_class_install_property (gobject_class,
1690 g_param_spec_string ("server", "Server",
1691 "The PulseAudio server to connect to", DEFAULT_SERVER,
1692 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1694 g_object_class_install_property (gobject_class, PROP_DEVICE,
1695 g_param_spec_string ("device", "Device",
1696 "The PulseAudio sink device to connect to", DEFAULT_DEVICE,
1697 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1699 g_object_class_install_property (gobject_class,
1701 g_param_spec_string ("device-name", "Device name",
1702 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
1703 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1705 g_object_class_install_property (gobject_class,
1707 g_param_spec_double ("volume", "Volume",
1708 "Linear volume of this stream, 1.0=100%", 0.0, MAX_VOLUME,
1709 DEFAULT_VOLUME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1710 g_object_class_install_property (gobject_class,
1712 g_param_spec_boolean ("mute", "Mute",
1713 "Mute state of this stream", DEFAULT_MUTE,
1714 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1717 * GstPulseSink:client
1719 * The PulseAudio client name to use.
1723 g_object_class_install_property (gobject_class,
1725 g_param_spec_string ("client", "Client",
1726 "The PulseAudio client name to use", gst_pulse_client_name (),
1727 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1728 GST_PARAM_MUTABLE_READY));
1731 * GstPulseSink:stream-properties
1733 * List of pulseaudio stream properties. A list of defined properties can be
1734 * found in the <ulink url="http://0pointer.de/lennart/projects/pulseaudio/doxygen/proplist_8h.html">pulseaudio api docs</ulink>.
1736 * Below is an example for registering as a music application to pulseaudio.
1738 * GstStructure *props;
1740 * props = gst_structure_from_string ("props,media.role=music", NULL);
1741 * g_object_set (pulse, "stream-properties", props, NULL);
1742 * gst_structure_free
1747 g_object_class_install_property (gobject_class,
1748 PROP_STREAM_PROPERTIES,
1749 g_param_spec_boxed ("stream-properties", "stream properties",
1750 "list of pulseaudio stream properties",
1751 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1753 gst_element_class_set_details_simple (gstelement_class,
1754 "PulseAudio Audio Sink",
1755 "Sink/Audio", "Plays audio to a PulseAudio server", "Lennart Poettering");
1756 gst_element_class_add_pad_template (gstelement_class,
1757 gst_static_pad_template_get (&pad_template));
1760 /* returns the current time of the sink ringbuffer */
1762 gst_pulsesink_get_time (GstClock * clock, GstBaseAudioSink * sink)
1764 GstPulseSink *psink;
1765 GstPulseRingBuffer *pbuf;
1768 if (!sink->ringbuffer || !sink->ringbuffer->acquired)
1769 return GST_CLOCK_TIME_NONE;
1771 pbuf = GST_PULSERING_BUFFER_CAST (sink->ringbuffer);
1772 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1774 pa_threaded_mainloop_lock (mainloop);
1775 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
1778 /* if we don't have enough data to get a timestamp, just return NONE, which
1779 * will return the last reported time */
1780 if (pa_stream_get_time (pbuf->stream, &time) < 0) {
1781 GST_DEBUG_OBJECT (psink, "could not get time");
1782 time = GST_CLOCK_TIME_NONE;
1785 pa_threaded_mainloop_unlock (mainloop);
1787 GST_LOG_OBJECT (psink, "current time is %" GST_TIME_FORMAT,
1788 GST_TIME_ARGS (time));
1795 GST_DEBUG_OBJECT (psink, "the server is dead");
1796 pa_threaded_mainloop_unlock (mainloop);
1798 return GST_CLOCK_TIME_NONE;
1803 gst_pulsesink_init (GstPulseSink * pulsesink)
1805 pulsesink->server = NULL;
1806 pulsesink->device = NULL;
1807 pulsesink->device_description = NULL;
1808 pulsesink->client_name = gst_pulse_client_name ();
1810 pulsesink->volume = DEFAULT_VOLUME;
1811 pulsesink->volume_set = FALSE;
1813 pulsesink->mute = DEFAULT_MUTE;
1814 pulsesink->mute_set = FALSE;
1816 pulsesink->notify = 0;
1818 pulsesink->properties = NULL;
1819 pulsesink->proplist = NULL;
1821 /* override with a custom clock */
1822 if (GST_BASE_AUDIO_SINK (pulsesink)->provided_clock)
1823 gst_object_unref (GST_BASE_AUDIO_SINK (pulsesink)->provided_clock);
1825 GST_BASE_AUDIO_SINK (pulsesink)->provided_clock =
1826 gst_audio_clock_new ("GstPulseSinkClock",
1827 (GstAudioClockGetTimeFunc) gst_pulsesink_get_time, pulsesink);
1829 /* TRUE for sinks, FALSE for sources */
1830 pulsesink->probe = gst_pulseprobe_new (G_OBJECT (pulsesink),
1831 G_OBJECT_GET_CLASS (pulsesink), PROP_DEVICE, pulsesink->device,
1836 gst_pulsesink_finalize (GObject * object)
1838 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
1840 g_free (pulsesink->server);
1841 g_free (pulsesink->device);
1842 g_free (pulsesink->device_description);
1843 g_free (pulsesink->client_name);
1845 if (pulsesink->properties)
1846 gst_structure_free (pulsesink->properties);
1847 if (pulsesink->proplist)
1848 pa_proplist_free (pulsesink->proplist);
1850 if (pulsesink->probe) {
1851 gst_pulseprobe_free (pulsesink->probe);
1852 pulsesink->probe = NULL;
1855 G_OBJECT_CLASS (parent_class)->finalize (object);
1859 gst_pulsesink_set_volume (GstPulseSink * psink, gdouble volume)
1862 pa_operation *o = NULL;
1863 GstPulseRingBuffer *pbuf;
1869 pa_threaded_mainloop_lock (mainloop);
1871 GST_DEBUG_OBJECT (psink, "setting volume to %f", volume);
1873 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
1874 if (pbuf == NULL || pbuf->stream == NULL)
1877 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
1880 gst_pulse_cvolume_from_linear (&v, pbuf->sample_spec.channels, volume);
1882 if (!(o = pa_context_set_sink_input_volume (pbuf->context, idx,
1886 /* We don't really care about the result of this call */
1890 pa_operation_unref (o);
1892 pa_threaded_mainloop_unlock (mainloop);
1899 psink->volume = volume;
1900 psink->volume_set = TRUE;
1902 GST_DEBUG_OBJECT (psink, "we have no mainloop");
1907 psink->volume = volume;
1908 psink->volume_set = TRUE;
1910 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
1915 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
1920 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1921 ("pa_stream_set_sink_input_volume() failed: %s",
1922 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1928 gst_pulsesink_set_mute (GstPulseSink * psink, gboolean mute)
1930 pa_operation *o = NULL;
1931 GstPulseRingBuffer *pbuf;
1937 pa_threaded_mainloop_lock (mainloop);
1939 GST_DEBUG_OBJECT (psink, "setting mute state to %d", mute);
1941 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
1942 if (pbuf == NULL || pbuf->stream == NULL)
1945 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
1948 if (!(o = pa_context_set_sink_input_mute (pbuf->context, idx,
1952 /* We don't really care about the result of this call */
1956 pa_operation_unref (o);
1958 pa_threaded_mainloop_unlock (mainloop);
1966 psink->mute_set = TRUE;
1968 GST_DEBUG_OBJECT (psink, "we have no mainloop");
1974 psink->mute_set = TRUE;
1976 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
1981 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
1986 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1987 ("pa_stream_set_sink_input_mute() failed: %s",
1988 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1994 gst_pulsesink_sink_input_info_cb (pa_context * c, const pa_sink_input_info * i,
1995 int eol, void *userdata)
1997 GstPulseRingBuffer *pbuf;
1998 GstPulseSink *psink;
2000 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
2001 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
2009 /* If the index doesn't match our current stream,
2010 * it implies we just recreated the stream (caps change)
2012 if (i->index == pa_stream_get_index (pbuf->stream)) {
2013 psink->volume = pa_sw_volume_to_linear (pa_cvolume_max (&i->volume));
2014 psink->mute = i->mute;
2018 pa_threaded_mainloop_signal (mainloop, 0);
2022 gst_pulsesink_get_volume (GstPulseSink * psink)
2024 GstPulseRingBuffer *pbuf;
2025 pa_operation *o = NULL;
2026 gdouble v = DEFAULT_VOLUME;
2032 pa_threaded_mainloop_lock (mainloop);
2034 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2035 if (pbuf == NULL || pbuf->stream == NULL)
2038 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2041 if (!(o = pa_context_get_sink_input_info (pbuf->context, idx,
2042 gst_pulsesink_sink_input_info_cb, pbuf)))
2045 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2046 pa_threaded_mainloop_wait (mainloop);
2047 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
2055 pa_operation_unref (o);
2057 pa_threaded_mainloop_unlock (mainloop);
2059 if (v > MAX_VOLUME) {
2060 GST_WARNING_OBJECT (psink, "Clipped volume from %f to %f", v, MAX_VOLUME);
2070 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2075 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2080 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2085 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2086 ("pa_context_get_sink_input_info() failed: %s",
2087 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2093 gst_pulsesink_get_mute (GstPulseSink * psink)
2095 GstPulseRingBuffer *pbuf;
2096 pa_operation *o = NULL;
2098 gboolean mute = FALSE;
2103 pa_threaded_mainloop_lock (mainloop);
2106 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2107 if (pbuf == NULL || pbuf->stream == NULL)
2110 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2113 if (!(o = pa_context_get_sink_input_info (pbuf->context, idx,
2114 gst_pulsesink_sink_input_info_cb, pbuf)))
2117 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2118 pa_threaded_mainloop_wait (mainloop);
2119 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
2125 pa_operation_unref (o);
2127 pa_threaded_mainloop_unlock (mainloop);
2135 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2140 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2145 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2150 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2151 ("pa_context_get_sink_input_info() failed: %s",
2152 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2158 gst_pulsesink_sink_info_cb (pa_context * c, const pa_sink_info * i, int eol,
2161 GstPulseRingBuffer *pbuf;
2162 GstPulseSink *psink;
2164 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
2165 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
2170 g_free (psink->device_description);
2171 psink->device_description = g_strdup (i->description);
2174 pa_threaded_mainloop_signal (mainloop, 0);
2178 gst_pulsesink_device_description (GstPulseSink * psink)
2180 GstPulseRingBuffer *pbuf;
2181 pa_operation *o = NULL;
2187 pa_threaded_mainloop_lock (mainloop);
2188 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2192 if (!(o = pa_context_get_sink_info_by_name (pbuf->context,
2193 psink->device, gst_pulsesink_sink_info_cb, pbuf)))
2196 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2197 pa_threaded_mainloop_wait (mainloop);
2198 if (gst_pulsering_is_dead (psink, pbuf, FALSE))
2204 pa_operation_unref (o);
2206 t = g_strdup (psink->device_description);
2207 pa_threaded_mainloop_unlock (mainloop);
2214 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2219 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2224 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2225 ("pa_context_get_sink_info_by_index() failed: %s",
2226 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2232 gst_pulsesink_set_property (GObject * object,
2233 guint prop_id, const GValue * value, GParamSpec * pspec)
2235 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2239 g_free (pulsesink->server);
2240 pulsesink->server = g_value_dup_string (value);
2241 if (pulsesink->probe)
2242 gst_pulseprobe_set_server (pulsesink->probe, pulsesink->server);
2245 g_free (pulsesink->device);
2246 pulsesink->device = g_value_dup_string (value);
2249 gst_pulsesink_set_volume (pulsesink, g_value_get_double (value));
2252 gst_pulsesink_set_mute (pulsesink, g_value_get_boolean (value));
2255 g_free (pulsesink->client_name);
2256 if (!g_value_get_string (value)) {
2257 GST_WARNING_OBJECT (pulsesink,
2258 "Empty PulseAudio client name not allowed. Resetting to default value");
2259 pulsesink->client_name = gst_pulse_client_name ();
2261 pulsesink->client_name = g_value_dup_string (value);
2263 case PROP_STREAM_PROPERTIES:
2264 if (pulsesink->properties)
2265 gst_structure_free (pulsesink->properties);
2266 pulsesink->properties =
2267 gst_structure_copy (gst_value_get_structure (value));
2268 if (pulsesink->proplist)
2269 pa_proplist_free (pulsesink->proplist);
2270 pulsesink->proplist = gst_pulse_make_proplist (pulsesink->properties);
2273 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2279 gst_pulsesink_get_property (GObject * object,
2280 guint prop_id, GValue * value, GParamSpec * pspec)
2283 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2287 g_value_set_string (value, pulsesink->server);
2290 g_value_set_string (value, pulsesink->device);
2292 case PROP_DEVICE_NAME:
2293 g_value_take_string (value, gst_pulsesink_device_description (pulsesink));
2296 g_value_set_double (value, gst_pulsesink_get_volume (pulsesink));
2299 g_value_set_boolean (value, gst_pulsesink_get_mute (pulsesink));
2302 g_value_set_string (value, pulsesink->client_name);
2304 case PROP_STREAM_PROPERTIES:
2305 gst_value_set_structure (value, pulsesink->properties);
2308 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2314 gst_pulsesink_change_title (GstPulseSink * psink, const gchar * t)
2316 pa_operation *o = NULL;
2317 GstPulseRingBuffer *pbuf;
2319 pa_threaded_mainloop_lock (mainloop);
2321 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2323 if (pbuf == NULL || pbuf->stream == NULL)
2326 g_free (pbuf->stream_name);
2327 pbuf->stream_name = g_strdup (t);
2329 if (!(o = pa_stream_set_name (pbuf->stream, pbuf->stream_name, NULL, NULL)))
2332 /* We're not interested if this operation failed or not */
2336 pa_operation_unref (o);
2337 pa_threaded_mainloop_unlock (mainloop);
2344 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2349 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2350 ("pa_stream_set_name() failed: %s",
2351 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2357 gst_pulsesink_change_props (GstPulseSink * psink, GstTagList * l)
2359 static const gchar *const map[] = {
2360 GST_TAG_TITLE, PA_PROP_MEDIA_TITLE,
2362 /* might get overriden in the next iteration by GST_TAG_ARTIST */
2363 GST_TAG_PERFORMER, PA_PROP_MEDIA_ARTIST,
2365 GST_TAG_ARTIST, PA_PROP_MEDIA_ARTIST,
2366 GST_TAG_LANGUAGE_CODE, PA_PROP_MEDIA_LANGUAGE,
2367 GST_TAG_LOCATION, PA_PROP_MEDIA_FILENAME,
2368 /* We might add more here later on ... */
2371 pa_proplist *pl = NULL;
2372 const gchar *const *t;
2373 gboolean empty = TRUE;
2374 pa_operation *o = NULL;
2375 GstPulseRingBuffer *pbuf;
2377 pl = pa_proplist_new ();
2379 for (t = map; *t; t += 2) {
2382 if (gst_tag_list_get_string (l, *t, &n)) {
2385 pa_proplist_sets (pl, *(t + 1), n);
2395 pa_threaded_mainloop_lock (mainloop);
2396 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2397 if (pbuf == NULL || pbuf->stream == NULL)
2400 if (!(o = pa_stream_proplist_update (pbuf->stream, PA_UPDATE_REPLACE,
2404 /* We're not interested if this operation failed or not */
2408 pa_operation_unref (o);
2410 pa_threaded_mainloop_unlock (mainloop);
2415 pa_proplist_free (pl);
2422 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2427 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2428 ("pa_stream_proplist_update() failed: %s",
2429 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2435 gst_pulsesink_flush_ringbuffer (GstPulseSink * psink)
2437 GstPulseRingBuffer *pbuf;
2439 pa_threaded_mainloop_lock (mainloop);
2441 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2443 if (pbuf == NULL || pbuf->stream == NULL)
2446 gst_pulsering_flush (pbuf);
2448 /* Uncork if we haven't already (happens when waiting to get enough data
2449 * to send out the first time) */
2451 gst_pulsering_set_corked (pbuf, FALSE, FALSE);
2453 /* We're not interested if this operation failed or not */
2455 pa_threaded_mainloop_unlock (mainloop);
2462 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2468 gst_pulsesink_event (GstBaseSink * sink, GstEvent * event)
2470 GstPulseSink *pulsesink = GST_PULSESINK_CAST (sink);
2472 switch (GST_EVENT_TYPE (event)) {
2473 case GST_EVENT_TAG:{
2474 gchar *title = NULL, *artist = NULL, *location = NULL, *description =
2475 NULL, *t = NULL, *buf = NULL;
2478 gst_event_parse_tag (event, &l);
2480 gst_tag_list_get_string (l, GST_TAG_TITLE, &title);
2481 gst_tag_list_get_string (l, GST_TAG_ARTIST, &artist);
2482 gst_tag_list_get_string (l, GST_TAG_LOCATION, &location);
2483 gst_tag_list_get_string (l, GST_TAG_DESCRIPTION, &description);
2486 gst_tag_list_get_string (l, GST_TAG_PERFORMER, &artist);
2488 if (title && artist)
2489 /* TRANSLATORS: 'song title' by 'artist name' */
2490 t = buf = g_strdup_printf (_("'%s' by '%s'"), g_strstrip (title),
2491 g_strstrip (artist));
2493 t = g_strstrip (title);
2494 else if (description)
2495 t = g_strstrip (description);
2497 t = g_strstrip (location);
2500 gst_pulsesink_change_title (pulsesink, t);
2505 g_free (description);
2508 gst_pulsesink_change_props (pulsesink, l);
2513 gst_pulsesink_flush_ringbuffer (pulsesink);
2519 return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
2523 gst_pulsesink_release_mainloop (GstPulseSink * psink)
2528 pa_threaded_mainloop_lock (mainloop);
2529 while (psink->defer_pending) {
2530 GST_DEBUG_OBJECT (psink, "waiting for stream status message emission");
2531 pa_threaded_mainloop_wait (mainloop);
2533 pa_threaded_mainloop_unlock (mainloop);
2535 g_mutex_lock (pa_shared_resource_mutex);
2537 if (!mainloop_ref_ct) {
2538 GST_INFO_OBJECT (psink, "terminating pa main loop thread");
2539 pa_threaded_mainloop_stop (mainloop);
2540 pa_threaded_mainloop_free (mainloop);
2543 g_mutex_unlock (pa_shared_resource_mutex);
2546 static GstStateChangeReturn
2547 gst_pulsesink_change_state (GstElement * element, GstStateChange transition)
2549 GstPulseSink *pulsesink = GST_PULSESINK (element);
2550 GstStateChangeReturn ret;
2552 switch (transition) {
2553 case GST_STATE_CHANGE_NULL_TO_READY:
2554 g_mutex_lock (pa_shared_resource_mutex);
2555 if (!mainloop_ref_ct) {
2556 GST_INFO_OBJECT (element, "new pa main loop thread");
2557 if (!(mainloop = pa_threaded_mainloop_new ()))
2558 goto mainloop_failed;
2559 mainloop_ref_ct = 1;
2560 pa_threaded_mainloop_start (mainloop);
2561 g_mutex_unlock (pa_shared_resource_mutex);
2563 GST_INFO_OBJECT (element, "reusing pa main loop thread");
2565 g_mutex_unlock (pa_shared_resource_mutex);
2568 case GST_STATE_CHANGE_READY_TO_PAUSED:
2569 gst_element_post_message (element,
2570 gst_message_new_clock_provide (GST_OBJECT_CAST (element),
2571 GST_BASE_AUDIO_SINK (pulsesink)->provided_clock, TRUE));
2577 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2578 if (ret == GST_STATE_CHANGE_FAILURE)
2581 switch (transition) {
2582 case GST_STATE_CHANGE_PAUSED_TO_READY:
2583 gst_element_post_message (element,
2584 gst_message_new_clock_lost (GST_OBJECT_CAST (element),
2585 GST_BASE_AUDIO_SINK (pulsesink)->provided_clock));
2587 case GST_STATE_CHANGE_READY_TO_NULL:
2588 gst_pulsesink_release_mainloop (pulsesink);
2599 g_mutex_unlock (pa_shared_resource_mutex);
2600 GST_ELEMENT_ERROR (pulsesink, RESOURCE, FAILED,
2601 ("pa_threaded_mainloop_new() failed"), (NULL));
2602 return GST_STATE_CHANGE_FAILURE;
2606 if (transition == GST_STATE_CHANGE_NULL_TO_READY) {
2607 /* Clear the PA mainloop if baseaudiosink failed to open the ring_buffer */
2608 g_assert (mainloop);
2609 gst_pulsesink_release_mainloop (pulsesink);