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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
25 * SECTION:element-pulsesink
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-1.0 -v filesrc location=sine.ogg ! oggdemux ! vorbisdec ! audioconvert ! audioresample ! pulsesink
35 * ]| Play an Ogg/Vorbis file.
37 * gst-launch-1.0 -v audiotestsrc ! audioconvert ! volume volume=0.4 ! pulsesink
38 * ]| Play a 440Hz sine wave.
40 * gst-launch-1.0 -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/audio/audio.h>
56 #include <gst/gst-i18n-plugin.h>
58 #include <gst/pbutils/pbutils.h> /* only used for GST_PLUGINS_BASE_VERSION_* */
60 #include <gst/glib-compat-private.h>
62 #include "pulsesink.h"
63 #include "pulseutil.h"
65 GST_DEBUG_CATEGORY_EXTERN (pulse_debug);
66 #define GST_CAT_DEFAULT pulse_debug
68 #define DEFAULT_SERVER NULL
69 #define DEFAULT_DEVICE NULL
70 #define DEFAULT_DEVICE_NAME NULL
71 #define DEFAULT_VOLUME 1.0
72 #define DEFAULT_MUTE FALSE
73 #define MAX_VOLUME 10.0
84 PROP_STREAM_PROPERTIES,
88 #define GST_TYPE_PULSERING_BUFFER \
89 (gst_pulseringbuffer_get_type())
90 #define GST_PULSERING_BUFFER(obj) \
91 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_PULSERING_BUFFER,GstPulseRingBuffer))
92 #define GST_PULSERING_BUFFER_CLASS(klass) \
93 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_PULSERING_BUFFER,GstPulseRingBufferClass))
94 #define GST_PULSERING_BUFFER_GET_CLASS(obj) \
95 (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_PULSERING_BUFFER, GstPulseRingBufferClass))
96 #define GST_PULSERING_BUFFER_CAST(obj) \
97 ((GstPulseRingBuffer *)obj)
98 #define GST_IS_PULSERING_BUFFER(obj) \
99 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_PULSERING_BUFFER))
100 #define GST_IS_PULSERING_BUFFER_CLASS(klass)\
101 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_PULSERING_BUFFER))
103 typedef struct _GstPulseRingBuffer GstPulseRingBuffer;
104 typedef struct _GstPulseRingBufferClass GstPulseRingBufferClass;
106 typedef struct _GstPulseContext GstPulseContext;
108 /* Store the PA contexts in a hash table to allow easy sharing among
109 * multiple instances of the sink. Keys are $context_name@$server_name
110 * (strings) and values should be GstPulseContext pointers.
112 struct _GstPulseContext
115 GSList *ring_buffers;
118 static GHashTable *gst_pulse_shared_contexts = NULL;
120 /* use one static main-loop for all instances
121 * this is needed to make the context sharing work as the contexts are
122 * released when releasing their parent main-loop
124 static pa_threaded_mainloop *mainloop = NULL;
125 static guint mainloop_ref_ct = 0;
127 /* lock for access to shared resources */
128 static GMutex pa_shared_resource_mutex;
130 /* We keep a custom ringbuffer that is backed up by data allocated by
131 * pulseaudio. We must also overide the commit function to write into
132 * pulseaudio memory instead. */
133 struct _GstPulseRingBuffer
135 GstAudioRingBuffer object;
143 pa_format_info *format;
154 gboolean in_commit:1;
157 struct _GstPulseRingBufferClass
159 GstAudioRingBufferClass parent_class;
162 static GType gst_pulseringbuffer_get_type (void);
163 static void gst_pulseringbuffer_finalize (GObject * object);
165 static GstAudioRingBufferClass *ring_parent_class = NULL;
167 static gboolean gst_pulseringbuffer_open_device (GstAudioRingBuffer * buf);
168 static gboolean gst_pulseringbuffer_close_device (GstAudioRingBuffer * buf);
169 static gboolean gst_pulseringbuffer_acquire (GstAudioRingBuffer * buf,
170 GstAudioRingBufferSpec * spec);
171 static gboolean gst_pulseringbuffer_release (GstAudioRingBuffer * buf);
172 static gboolean gst_pulseringbuffer_start (GstAudioRingBuffer * buf);
173 static gboolean gst_pulseringbuffer_pause (GstAudioRingBuffer * buf);
174 static gboolean gst_pulseringbuffer_stop (GstAudioRingBuffer * buf);
175 static void gst_pulseringbuffer_clear (GstAudioRingBuffer * buf);
176 static guint gst_pulseringbuffer_commit (GstAudioRingBuffer * buf,
177 guint64 * sample, guchar * data, gint in_samples, gint out_samples,
180 G_DEFINE_TYPE (GstPulseRingBuffer, gst_pulseringbuffer,
181 GST_TYPE_AUDIO_RING_BUFFER);
184 gst_pulsesink_init_contexts (void)
186 g_mutex_init (&pa_shared_resource_mutex);
187 gst_pulse_shared_contexts = g_hash_table_new_full (g_str_hash, g_str_equal,
192 gst_pulseringbuffer_class_init (GstPulseRingBufferClass * klass)
194 GObjectClass *gobject_class;
195 GstAudioRingBufferClass *gstringbuffer_class;
197 gobject_class = (GObjectClass *) klass;
198 gstringbuffer_class = (GstAudioRingBufferClass *) klass;
200 ring_parent_class = g_type_class_peek_parent (klass);
202 gobject_class->finalize = gst_pulseringbuffer_finalize;
204 gstringbuffer_class->open_device =
205 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_open_device);
206 gstringbuffer_class->close_device =
207 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_close_device);
208 gstringbuffer_class->acquire =
209 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_acquire);
210 gstringbuffer_class->release =
211 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_release);
212 gstringbuffer_class->start = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_start);
213 gstringbuffer_class->pause = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_pause);
214 gstringbuffer_class->resume = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_start);
215 gstringbuffer_class->stop = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_stop);
216 gstringbuffer_class->clear_all =
217 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_clear);
219 gstringbuffer_class->commit = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_commit);
223 gst_pulseringbuffer_init (GstPulseRingBuffer * pbuf)
225 pbuf->stream_name = NULL;
226 pbuf->context = NULL;
231 pbuf->is_pcm = FALSE;
235 pbuf->m_writable = 0;
237 pbuf->m_lastoffset = 0;
240 pbuf->in_commit = FALSE;
241 pbuf->paused = FALSE;
245 gst_pulsering_destroy_stream (GstPulseRingBuffer * pbuf)
250 /* drop shm memory buffer */
251 pa_stream_cancel_write (pbuf->stream);
253 /* reset internal variables */
256 pbuf->m_writable = 0;
258 pbuf->m_lastoffset = 0;
261 pa_format_info_free (pbuf->format);
264 pbuf->is_pcm = FALSE;
267 pa_stream_disconnect (pbuf->stream);
269 /* Make sure we don't get any further callbacks */
270 pa_stream_set_state_callback (pbuf->stream, NULL, NULL);
271 pa_stream_set_write_callback (pbuf->stream, NULL, NULL);
272 pa_stream_set_underflow_callback (pbuf->stream, NULL, NULL);
273 pa_stream_set_overflow_callback (pbuf->stream, NULL, NULL);
275 pa_stream_unref (pbuf->stream);
279 g_free (pbuf->stream_name);
280 pbuf->stream_name = NULL;
284 gst_pulsering_destroy_context (GstPulseRingBuffer * pbuf)
286 g_mutex_lock (&pa_shared_resource_mutex);
288 GST_DEBUG_OBJECT (pbuf, "destroying ringbuffer %p", pbuf);
290 gst_pulsering_destroy_stream (pbuf);
293 pa_context_unref (pbuf->context);
294 pbuf->context = NULL;
297 if (pbuf->context_name) {
298 GstPulseContext *pctx;
300 pctx = g_hash_table_lookup (gst_pulse_shared_contexts, pbuf->context_name);
302 GST_DEBUG_OBJECT (pbuf, "releasing context with name %s, pbuf=%p, pctx=%p",
303 pbuf->context_name, pbuf, pctx);
306 pctx->ring_buffers = g_slist_remove (pctx->ring_buffers, pbuf);
307 if (pctx->ring_buffers == NULL) {
308 GST_DEBUG_OBJECT (pbuf,
309 "destroying final context with name %s, pbuf=%p, pctx=%p",
310 pbuf->context_name, pbuf, pctx);
312 pa_context_disconnect (pctx->context);
314 /* Make sure we don't get any further callbacks */
315 pa_context_set_state_callback (pctx->context, NULL, NULL);
316 pa_context_set_subscribe_callback (pctx->context, NULL, NULL);
318 g_hash_table_remove (gst_pulse_shared_contexts, pbuf->context_name);
320 pa_context_unref (pctx->context);
321 g_slice_free (GstPulseContext, pctx);
324 g_free (pbuf->context_name);
325 pbuf->context_name = NULL;
327 g_mutex_unlock (&pa_shared_resource_mutex);
331 gst_pulseringbuffer_finalize (GObject * object)
333 GstPulseRingBuffer *ringbuffer;
335 ringbuffer = GST_PULSERING_BUFFER_CAST (object);
337 gst_pulsering_destroy_context (ringbuffer);
338 G_OBJECT_CLASS (ring_parent_class)->finalize (object);
342 #define CONTEXT_OK(c) ((c) && PA_CONTEXT_IS_GOOD (pa_context_get_state ((c))))
343 #define STREAM_OK(s) ((s) && PA_STREAM_IS_GOOD (pa_stream_get_state ((s))))
346 gst_pulsering_is_dead (GstPulseSink * psink, GstPulseRingBuffer * pbuf,
347 gboolean check_stream)
349 if (!CONTEXT_OK (pbuf->context))
352 if (check_stream && !STREAM_OK (pbuf->stream))
359 const gchar *err_str =
360 pbuf->context ? pa_strerror (pa_context_errno (pbuf->context)) : NULL;
361 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED, ("Disconnected: %s",
368 gst_pulsering_context_state_cb (pa_context * c, void *userdata)
370 pa_context_state_t state;
371 pa_threaded_mainloop *mainloop = (pa_threaded_mainloop *) userdata;
373 state = pa_context_get_state (c);
375 GST_LOG ("got new context state %d", state);
378 case PA_CONTEXT_READY:
379 case PA_CONTEXT_TERMINATED:
380 case PA_CONTEXT_FAILED:
381 GST_LOG ("signaling");
382 pa_threaded_mainloop_signal (mainloop, 0);
385 case PA_CONTEXT_UNCONNECTED:
386 case PA_CONTEXT_CONNECTING:
387 case PA_CONTEXT_AUTHORIZING:
388 case PA_CONTEXT_SETTING_NAME:
394 gst_pulsering_context_subscribe_cb (pa_context * c,
395 pa_subscription_event_type_t t, uint32_t idx, void *userdata)
398 GstPulseContext *pctx = (GstPulseContext *) userdata;
401 if (t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT | PA_SUBSCRIPTION_EVENT_CHANGE) &&
402 t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT | PA_SUBSCRIPTION_EVENT_NEW))
405 for (walk = pctx->ring_buffers; walk; walk = g_slist_next (walk)) {
406 GstPulseRingBuffer *pbuf = (GstPulseRingBuffer *) walk->data;
407 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
409 GST_LOG_OBJECT (psink, "type %04x, idx %u", t, idx);
414 if (idx != pa_stream_get_index (pbuf->stream))
417 if (psink->device && pbuf->is_pcm &&
418 !g_str_equal (psink->device,
419 pa_stream_get_device_name (pbuf->stream))) {
420 /* Underlying sink changed. And this is not a passthrough stream. Let's
421 * see if someone upstream wants to try to renegotiate. */
424 g_free (psink->device);
425 psink->device = g_strdup (pa_stream_get_device_name (pbuf->stream));
427 GST_INFO_OBJECT (psink, "emitting sink-changed");
429 /* FIXME: send reconfigure event instead and let decodebin/playbin
430 * handle that. Also take care of ac3 alignment. See "pulse-format-lost" */
431 renego = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
432 gst_structure_new_empty ("pulse-sink-changed"));
434 if (!gst_pad_push_event (GST_BASE_SINK (psink)->sinkpad, renego))
435 GST_DEBUG_OBJECT (psink, "Emitted sink-changed - nobody was listening");
438 /* Actually this event is also triggered when other properties of
439 * the stream change that are unrelated to the volume. However it is
440 * probably cheaper to signal the change here and check for the
441 * volume when the GObject property is read instead of querying it always. */
443 /* inform streaming thread to notify */
444 g_atomic_int_compare_and_exchange (&psink->notify, 0, 1);
448 /* will be called when the device should be opened. In this case we will connect
449 * to the server. We should not try to open any streams in this state. */
451 gst_pulseringbuffer_open_device (GstAudioRingBuffer * buf)
454 GstPulseRingBuffer *pbuf;
455 GstPulseContext *pctx;
456 pa_mainloop_api *api;
457 gboolean need_unlock_shared;
459 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
460 pbuf = GST_PULSERING_BUFFER_CAST (buf);
462 g_assert (!pbuf->stream);
463 g_assert (psink->client_name);
466 pbuf->context_name = g_strdup_printf ("%s@%s", psink->client_name,
469 pbuf->context_name = g_strdup (psink->client_name);
471 pa_threaded_mainloop_lock (mainloop);
473 g_mutex_lock (&pa_shared_resource_mutex);
474 need_unlock_shared = TRUE;
476 pctx = g_hash_table_lookup (gst_pulse_shared_contexts, pbuf->context_name);
478 pctx = g_slice_new0 (GstPulseContext);
480 /* get the mainloop api and create a context */
481 GST_INFO_OBJECT (psink, "new context with name %s, pbuf=%p, pctx=%p",
482 pbuf->context_name, pbuf, pctx);
483 api = pa_threaded_mainloop_get_api (mainloop);
484 if (!(pctx->context = pa_context_new (api, pbuf->context_name)))
487 pctx->ring_buffers = g_slist_prepend (pctx->ring_buffers, pbuf);
488 g_hash_table_insert (gst_pulse_shared_contexts,
489 g_strdup (pbuf->context_name), (gpointer) pctx);
490 /* register some essential callbacks */
491 pa_context_set_state_callback (pctx->context,
492 gst_pulsering_context_state_cb, mainloop);
493 pa_context_set_subscribe_callback (pctx->context,
494 gst_pulsering_context_subscribe_cb, pctx);
496 /* try to connect to the server and wait for completion, we don't want to
497 * autospawn a deamon */
498 GST_LOG_OBJECT (psink, "connect to server %s",
499 GST_STR_NULL (psink->server));
500 if (pa_context_connect (pctx->context, psink->server,
501 PA_CONTEXT_NOAUTOSPAWN, NULL) < 0)
504 GST_INFO_OBJECT (psink,
505 "reusing shared context with name %s, pbuf=%p, pctx=%p",
506 pbuf->context_name, pbuf, pctx);
507 pctx->ring_buffers = g_slist_prepend (pctx->ring_buffers, pbuf);
510 g_mutex_unlock (&pa_shared_resource_mutex);
511 need_unlock_shared = FALSE;
513 /* context created or shared okay */
514 pbuf->context = pa_context_ref (pctx->context);
517 pa_context_state_t state;
519 state = pa_context_get_state (pbuf->context);
521 GST_LOG_OBJECT (psink, "context state is now %d", state);
523 if (!PA_CONTEXT_IS_GOOD (state))
526 if (state == PA_CONTEXT_READY)
529 /* Wait until the context is ready */
530 GST_LOG_OBJECT (psink, "waiting..");
531 pa_threaded_mainloop_wait (mainloop);
534 GST_LOG_OBJECT (psink, "opened the device");
536 pa_threaded_mainloop_unlock (mainloop);
543 if (need_unlock_shared)
544 g_mutex_unlock (&pa_shared_resource_mutex);
545 gst_pulsering_destroy_context (pbuf);
546 pa_threaded_mainloop_unlock (mainloop);
551 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
552 ("Failed to create context"), (NULL));
553 g_slice_free (GstPulseContext, pctx);
554 goto unlock_and_fail;
558 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED, ("Failed to connect: %s",
559 pa_strerror (pa_context_errno (pctx->context))), (NULL));
560 goto unlock_and_fail;
564 /* close the device */
566 gst_pulseringbuffer_close_device (GstAudioRingBuffer * buf)
569 GstPulseRingBuffer *pbuf;
571 pbuf = GST_PULSERING_BUFFER_CAST (buf);
572 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
574 GST_LOG_OBJECT (psink, "closing device");
576 pa_threaded_mainloop_lock (mainloop);
577 gst_pulsering_destroy_context (pbuf);
578 pa_threaded_mainloop_unlock (mainloop);
580 GST_LOG_OBJECT (psink, "closed device");
586 gst_pulsering_stream_state_cb (pa_stream * s, void *userdata)
589 GstPulseRingBuffer *pbuf;
590 pa_stream_state_t state;
592 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
593 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
595 state = pa_stream_get_state (s);
596 GST_LOG_OBJECT (psink, "got new stream state %d", state);
599 case PA_STREAM_READY:
600 case PA_STREAM_FAILED:
601 case PA_STREAM_TERMINATED:
602 GST_LOG_OBJECT (psink, "signaling");
603 pa_threaded_mainloop_signal (mainloop, 0);
605 case PA_STREAM_UNCONNECTED:
606 case PA_STREAM_CREATING:
612 gst_pulsering_stream_request_cb (pa_stream * s, size_t length, void *userdata)
615 GstAudioRingBuffer *rbuf;
616 GstPulseRingBuffer *pbuf;
618 rbuf = GST_AUDIO_RING_BUFFER_CAST (userdata);
619 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
620 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
622 GST_LOG_OBJECT (psink, "got request for length %" G_GSIZE_FORMAT, length);
624 if (pbuf->in_commit && (length >= rbuf->spec.segsize)) {
625 /* only signal when we are waiting in the commit thread
626 * and got request for atleast a segment */
627 pa_threaded_mainloop_signal (mainloop, 0);
632 gst_pulsering_stream_underflow_cb (pa_stream * s, void *userdata)
635 GstPulseRingBuffer *pbuf;
637 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
638 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
640 GST_WARNING_OBJECT (psink, "Got underflow");
644 gst_pulsering_stream_overflow_cb (pa_stream * s, void *userdata)
647 GstPulseRingBuffer *pbuf;
649 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
650 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
652 GST_WARNING_OBJECT (psink, "Got overflow");
656 gst_pulsering_stream_latency_cb (pa_stream * s, void *userdata)
659 GstPulseRingBuffer *pbuf;
660 GstAudioRingBuffer *ringbuf;
661 const pa_timing_info *info;
664 info = pa_stream_get_timing_info (s);
666 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
667 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
668 ringbuf = GST_AUDIO_RING_BUFFER (pbuf);
671 GST_LOG_OBJECT (psink, "latency update (information unknown)");
675 if (!info->read_index_corrupt) {
676 /* Update segdone based on the read index. segdone is of segment
677 * granularity, while the read index is at byte granularity. We take the
678 * ceiling while converting the latter to the former since it is more
679 * conservative to report that we've read more than we have than to report
680 * less. One concern here is that latency updates happen every 100ms, which
681 * means segdone is not updated very often, but increasing the update
682 * frequency would mean more communication overhead. */
683 g_atomic_int_set (&ringbuf->segdone,
684 (int) gst_util_uint64_scale_ceil (info->read_index, 1,
685 ringbuf->spec.segsize));
688 sink_usec = info->configured_sink_usec;
690 GST_LOG_OBJECT (psink,
691 "latency_update, %" G_GUINT64_FORMAT ", %d:%" G_GINT64_FORMAT ", %d:%"
692 G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT,
693 GST_TIMEVAL_TO_TIME (info->timestamp), info->write_index_corrupt,
694 info->write_index, info->read_index_corrupt, info->read_index,
695 info->sink_usec, sink_usec);
699 gst_pulsering_stream_suspended_cb (pa_stream * p, void *userdata)
702 GstPulseRingBuffer *pbuf;
704 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
705 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
707 if (pa_stream_is_suspended (p))
708 GST_DEBUG_OBJECT (psink, "stream suspended");
710 GST_DEBUG_OBJECT (psink, "stream resumed");
714 gst_pulsering_stream_started_cb (pa_stream * p, void *userdata)
717 GstPulseRingBuffer *pbuf;
719 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
720 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
722 GST_DEBUG_OBJECT (psink, "stream started");
726 gst_pulsering_stream_event_cb (pa_stream * p, const char *name,
727 pa_proplist * pl, void *userdata)
730 GstPulseRingBuffer *pbuf;
732 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
733 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
735 if (!strcmp (name, PA_STREAM_EVENT_REQUEST_CORK)) {
736 /* the stream wants to PAUSE, post a message for the application. */
737 GST_DEBUG_OBJECT (psink, "got request for CORK");
738 gst_element_post_message (GST_ELEMENT_CAST (psink),
739 gst_message_new_request_state (GST_OBJECT_CAST (psink),
742 } else if (!strcmp (name, PA_STREAM_EVENT_REQUEST_UNCORK)) {
743 GST_DEBUG_OBJECT (psink, "got request for UNCORK");
744 gst_element_post_message (GST_ELEMENT_CAST (psink),
745 gst_message_new_request_state (GST_OBJECT_CAST (psink),
747 } else if (!strcmp (name, PA_STREAM_EVENT_FORMAT_LOST)) {
750 if (g_atomic_int_get (&psink->format_lost)) {
751 /* Duplicate event before we're done reconfiguring, discard */
755 GST_DEBUG_OBJECT (psink, "got FORMAT LOST");
756 g_atomic_int_set (&psink->format_lost, 1);
757 psink->format_lost_time = g_ascii_strtoull (pa_proplist_gets (pl,
758 "stream-time"), NULL, 0) * 1000;
760 g_free (psink->device);
761 psink->device = g_strdup (pa_proplist_gets (pl, "device"));
763 /* FIXME: send reconfigure event instead and let decodebin/playbin
764 * handle that. Also take care of ac3 alignment */
765 renego = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
766 gst_structure_new_empty ("pulse-format-lost"));
769 if (g_str_equal (gst_structure_get_name (st), "audio/x-eac3")) {
770 GstStructure *event_st = gst_structure_new ("ac3parse-set-alignment",
771 "alignment", G_TYPE_STRING, pbin->dbin ? "frame" : "iec61937", NULL);
773 if (!gst_pad_push_event (pbin->sinkpad,
774 gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, event_st)))
775 GST_WARNING_OBJECT (pbin->sinkpad, "Could not update alignment");
779 if (!gst_pad_push_event (GST_BASE_SINK (psink)->sinkpad, renego)) {
780 /* Nobody handled the format change - emit an error */
781 GST_ELEMENT_ERROR (psink, STREAM, FORMAT, ("Sink format changed"),
782 ("Sink format changed"));
785 GST_DEBUG_OBJECT (psink, "got unknown event %s", name);
789 /* Called with the mainloop locked */
791 gst_pulsering_wait_for_stream_ready (GstPulseSink * psink, pa_stream * stream)
793 pa_stream_state_t state;
796 state = pa_stream_get_state (stream);
798 GST_LOG_OBJECT (psink, "stream state is now %d", state);
800 if (!PA_STREAM_IS_GOOD (state))
803 if (state == PA_STREAM_READY)
806 /* Wait until the stream is ready */
807 pa_threaded_mainloop_wait (mainloop);
812 /* This method should create a new stream of the given @spec. No playback should
813 * start yet so we start in the corked state. */
815 gst_pulseringbuffer_acquire (GstAudioRingBuffer * buf,
816 GstAudioRingBufferSpec * spec)
819 GstPulseRingBuffer *pbuf;
820 pa_buffer_attr wanted;
821 const pa_buffer_attr *actual;
822 pa_channel_map channel_map;
823 pa_operation *o = NULL;
825 pa_cvolume *pv = NULL;
826 pa_stream_flags_t flags;
828 GstAudioClock *clock;
829 pa_format_info *formats[1];
830 #ifndef GST_DISABLE_GST_DEBUG
831 gchar print_buf[PA_FORMAT_INFO_SNPRINT_MAX];
834 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
835 pbuf = GST_PULSERING_BUFFER_CAST (buf);
837 GST_LOG_OBJECT (psink, "creating sample spec");
838 /* convert the gstreamer sample spec to the pulseaudio format */
839 if (!gst_pulse_fill_format_info (spec, &pbuf->format, &pbuf->channels))
841 pbuf->is_pcm = pa_format_info_is_pcm (pbuf->format);
843 pa_threaded_mainloop_lock (mainloop);
845 /* we need a context and a no stream */
846 g_assert (pbuf->context);
847 g_assert (!pbuf->stream);
849 /* enable event notifications */
850 GST_LOG_OBJECT (psink, "subscribing to context events");
851 if (!(o = pa_context_subscribe (pbuf->context,
852 PA_SUBSCRIPTION_MASK_SINK_INPUT, NULL, NULL)))
853 goto subscribe_failed;
855 pa_operation_unref (o);
857 /* initialize the channel map */
858 if (pbuf->is_pcm && gst_pulse_gst_to_channel_map (&channel_map, spec))
859 pa_format_info_set_channel_map (pbuf->format, &channel_map);
861 /* find a good name for the stream */
862 if (psink->stream_name)
863 name = psink->stream_name;
865 name = "Playback Stream";
867 /* create a stream */
868 formats[0] = pbuf->format;
869 if (!(pbuf->stream = pa_stream_new_extended (pbuf->context, name, formats, 1,
873 /* install essential callbacks */
874 pa_stream_set_state_callback (pbuf->stream,
875 gst_pulsering_stream_state_cb, pbuf);
876 pa_stream_set_write_callback (pbuf->stream,
877 gst_pulsering_stream_request_cb, pbuf);
878 pa_stream_set_underflow_callback (pbuf->stream,
879 gst_pulsering_stream_underflow_cb, pbuf);
880 pa_stream_set_overflow_callback (pbuf->stream,
881 gst_pulsering_stream_overflow_cb, pbuf);
882 pa_stream_set_latency_update_callback (pbuf->stream,
883 gst_pulsering_stream_latency_cb, pbuf);
884 pa_stream_set_suspended_callback (pbuf->stream,
885 gst_pulsering_stream_suspended_cb, pbuf);
886 pa_stream_set_started_callback (pbuf->stream,
887 gst_pulsering_stream_started_cb, pbuf);
888 pa_stream_set_event_callback (pbuf->stream,
889 gst_pulsering_stream_event_cb, pbuf);
891 /* buffering requirements. When setting prebuf to 0, the stream will not pause
892 * when we cause an underrun, which causes time to continue. */
893 memset (&wanted, 0, sizeof (wanted));
894 wanted.tlength = spec->segtotal * spec->segsize;
895 wanted.maxlength = -1;
897 wanted.minreq = spec->segsize;
899 GST_INFO_OBJECT (psink, "tlength: %d", wanted.tlength);
900 GST_INFO_OBJECT (psink, "maxlength: %d", wanted.maxlength);
901 GST_INFO_OBJECT (psink, "prebuf: %d", wanted.prebuf);
902 GST_INFO_OBJECT (psink, "minreq: %d", wanted.minreq);
904 /* configure volume when we changed it, else we leave the default */
905 if (psink->volume_set) {
906 GST_LOG_OBJECT (psink, "have volume of %f", psink->volume);
909 gst_pulse_cvolume_from_linear (pv, pbuf->channels, psink->volume);
911 GST_DEBUG_OBJECT (psink, "passthrough stream, not setting volume");
918 /* construct the flags */
919 flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE |
920 PA_STREAM_ADJUST_LATENCY | PA_STREAM_START_CORKED;
922 if (psink->mute_set) {
924 flags |= PA_STREAM_START_MUTED;
926 flags |= PA_STREAM_START_UNMUTED;
929 /* we always start corked (see flags above) */
932 /* try to connect now */
933 GST_LOG_OBJECT (psink, "connect for playback to device %s",
934 GST_STR_NULL (psink->device));
935 if (pa_stream_connect_playback (pbuf->stream, psink->device,
936 &wanted, flags, pv, NULL) < 0)
939 /* our clock will now start from 0 again */
940 clock = GST_AUDIO_CLOCK (GST_AUDIO_BASE_SINK (psink)->provided_clock);
941 gst_audio_clock_reset (clock, 0);
943 if (!gst_pulsering_wait_for_stream_ready (psink, pbuf->stream))
946 g_free (psink->device);
947 psink->device = g_strdup (pa_stream_get_device_name (pbuf->stream));
949 #ifndef GST_DISABLE_GST_DEBUG
950 pa_format_info_snprint (print_buf, sizeof (print_buf),
951 pa_stream_get_format_info (pbuf->stream));
952 GST_INFO_OBJECT (psink, "negotiated to: %s", print_buf);
955 /* After we passed the volume off of to PA we never want to set it
956 again, since it is PA's job to save/restore volumes. */
957 psink->volume_set = psink->mute_set = FALSE;
959 GST_LOG_OBJECT (psink, "stream is acquired now");
961 /* get the actual buffering properties now */
962 actual = pa_stream_get_buffer_attr (pbuf->stream);
964 GST_INFO_OBJECT (psink, "tlength: %d (wanted: %d)", actual->tlength,
966 GST_INFO_OBJECT (psink, "maxlength: %d", actual->maxlength);
967 GST_INFO_OBJECT (psink, "prebuf: %d", actual->prebuf);
968 GST_INFO_OBJECT (psink, "minreq: %d (wanted %d)", actual->minreq,
971 spec->segsize = actual->minreq;
972 spec->segtotal = actual->tlength / spec->segsize;
974 pa_threaded_mainloop_unlock (mainloop);
981 gst_pulsering_destroy_stream (pbuf);
982 pa_threaded_mainloop_unlock (mainloop);
988 GST_ELEMENT_ERROR (psink, RESOURCE, SETTINGS,
989 ("Invalid sample specification."), (NULL));
994 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
995 ("pa_context_subscribe() failed: %s",
996 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
997 goto unlock_and_fail;
1001 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1002 ("Failed to create stream: %s",
1003 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1004 goto unlock_and_fail;
1008 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1009 ("Failed to connect stream: %s",
1010 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1011 goto unlock_and_fail;
1015 /* free the stream that we acquired before */
1017 gst_pulseringbuffer_release (GstAudioRingBuffer * buf)
1019 GstPulseRingBuffer *pbuf;
1021 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1023 pa_threaded_mainloop_lock (mainloop);
1024 gst_pulsering_destroy_stream (pbuf);
1025 pa_threaded_mainloop_unlock (mainloop);
1028 GstPulseSink *psink;
1030 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1031 g_atomic_int_set (&psink->format_lost, FALSE);
1032 psink->format_lost_time = GST_CLOCK_TIME_NONE;
1039 gst_pulsering_success_cb (pa_stream * s, int success, void *userdata)
1041 pa_threaded_mainloop_signal (mainloop, 0);
1044 /* update the corked state of a stream, must be called with the mainloop
1047 gst_pulsering_set_corked (GstPulseRingBuffer * pbuf, gboolean corked,
1050 pa_operation *o = NULL;
1051 GstPulseSink *psink;
1052 gboolean res = FALSE;
1054 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1056 if (g_atomic_int_get (&psink->format_lost)) {
1057 /* Sink format changed, stream's gone so fake being paused */
1061 GST_DEBUG_OBJECT (psink, "setting corked state to %d", corked);
1062 if (pbuf->corked != corked) {
1063 if (!(o = pa_stream_cork (pbuf->stream, corked,
1064 gst_pulsering_success_cb, pbuf)))
1067 while (wait && pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1068 pa_threaded_mainloop_wait (mainloop);
1069 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
1072 pbuf->corked = corked;
1074 GST_DEBUG_OBJECT (psink, "skipping, already in requested state");
1080 pa_operation_unref (o);
1087 GST_DEBUG_OBJECT (psink, "the server is dead");
1092 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1093 ("pa_stream_cork() failed: %s",
1094 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1100 gst_pulseringbuffer_clear (GstAudioRingBuffer * buf)
1102 GstPulseSink *psink;
1103 GstPulseRingBuffer *pbuf;
1104 pa_operation *o = NULL;
1106 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1107 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1109 pa_threaded_mainloop_lock (mainloop);
1110 GST_DEBUG_OBJECT (psink, "clearing");
1112 /* don't wait for the flush to complete */
1113 if ((o = pa_stream_flush (pbuf->stream, NULL, pbuf)))
1114 pa_operation_unref (o);
1116 pa_threaded_mainloop_unlock (mainloop);
1119 /* called from pulse with the mainloop lock */
1121 mainloop_enter_defer_cb (pa_mainloop_api * api, void *userdata)
1123 GstPulseSink *pulsesink = GST_PULSESINK (userdata);
1124 GstMessage *message;
1127 GST_DEBUG_OBJECT (pulsesink, "posting ENTER stream status");
1128 message = gst_message_new_stream_status (GST_OBJECT (pulsesink),
1129 GST_STREAM_STATUS_TYPE_ENTER, GST_ELEMENT (pulsesink));
1130 g_value_init (&val, GST_TYPE_G_THREAD);
1131 g_value_set_boxed (&val, g_thread_self ());
1132 gst_message_set_stream_status_object (message, &val);
1133 g_value_unset (&val);
1135 gst_element_post_message (GST_ELEMENT (pulsesink), message);
1137 g_return_if_fail (pulsesink->defer_pending);
1138 pulsesink->defer_pending--;
1139 pa_threaded_mainloop_signal (mainloop, 0);
1142 /* start/resume playback ASAP, we don't uncork here but in the commit method */
1144 gst_pulseringbuffer_start (GstAudioRingBuffer * buf)
1146 GstPulseSink *psink;
1147 GstPulseRingBuffer *pbuf;
1149 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1150 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1152 pa_threaded_mainloop_lock (mainloop);
1154 GST_DEBUG_OBJECT (psink, "scheduling stream status");
1155 psink->defer_pending++;
1156 pa_mainloop_api_once (pa_threaded_mainloop_get_api (mainloop),
1157 mainloop_enter_defer_cb, psink);
1159 GST_DEBUG_OBJECT (psink, "starting");
1160 pbuf->paused = FALSE;
1162 /* EOS needs running clock */
1163 if (GST_BASE_SINK_CAST (psink)->eos ||
1164 g_atomic_int_get (&GST_AUDIO_BASE_SINK (psink)->eos_rendering))
1165 gst_pulsering_set_corked (pbuf, FALSE, FALSE);
1167 pa_threaded_mainloop_unlock (mainloop);
1172 /* pause/stop playback ASAP */
1174 gst_pulseringbuffer_pause (GstAudioRingBuffer * buf)
1176 GstPulseSink *psink;
1177 GstPulseRingBuffer *pbuf;
1180 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1181 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1183 pa_threaded_mainloop_lock (mainloop);
1184 GST_DEBUG_OBJECT (psink, "pausing and corking");
1185 /* make sure the commit method stops writing */
1186 pbuf->paused = TRUE;
1187 res = gst_pulsering_set_corked (pbuf, TRUE, TRUE);
1188 if (pbuf->in_commit) {
1189 /* we are waiting in a commit, signal */
1190 GST_DEBUG_OBJECT (psink, "signal commit");
1191 pa_threaded_mainloop_signal (mainloop, 0);
1193 pa_threaded_mainloop_unlock (mainloop);
1198 /* called from pulse with the mainloop lock */
1200 mainloop_leave_defer_cb (pa_mainloop_api * api, void *userdata)
1202 GstPulseSink *pulsesink = GST_PULSESINK (userdata);
1203 GstMessage *message;
1206 GST_DEBUG_OBJECT (pulsesink, "posting LEAVE stream status");
1207 message = gst_message_new_stream_status (GST_OBJECT (pulsesink),
1208 GST_STREAM_STATUS_TYPE_LEAVE, GST_ELEMENT (pulsesink));
1209 g_value_init (&val, GST_TYPE_G_THREAD);
1210 g_value_set_boxed (&val, g_thread_self ());
1211 gst_message_set_stream_status_object (message, &val);
1212 g_value_unset (&val);
1214 gst_element_post_message (GST_ELEMENT (pulsesink), message);
1216 g_return_if_fail (pulsesink->defer_pending);
1217 pulsesink->defer_pending--;
1218 pa_threaded_mainloop_signal (mainloop, 0);
1221 /* stop playback, we flush everything. */
1223 gst_pulseringbuffer_stop (GstAudioRingBuffer * buf)
1225 GstPulseSink *psink;
1226 GstPulseRingBuffer *pbuf;
1227 gboolean res = FALSE;
1228 pa_operation *o = NULL;
1230 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1231 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1233 pa_threaded_mainloop_lock (mainloop);
1235 pbuf->paused = TRUE;
1236 res = gst_pulsering_set_corked (pbuf, TRUE, TRUE);
1238 /* Inform anyone waiting in _commit() call that it shall wakeup */
1239 if (pbuf->in_commit) {
1240 GST_DEBUG_OBJECT (psink, "signal commit thread");
1241 pa_threaded_mainloop_signal (mainloop, 0);
1243 if (g_atomic_int_get (&psink->format_lost)) {
1244 /* Don't try to flush, the stream's probably gone by now */
1249 /* then try to flush, it's not fatal when this fails */
1250 GST_DEBUG_OBJECT (psink, "flushing");
1251 if ((o = pa_stream_flush (pbuf->stream, gst_pulsering_success_cb, pbuf))) {
1252 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1253 GST_DEBUG_OBJECT (psink, "wait for completion");
1254 pa_threaded_mainloop_wait (mainloop);
1255 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
1258 GST_DEBUG_OBJECT (psink, "flush completed");
1264 pa_operation_cancel (o);
1265 pa_operation_unref (o);
1268 GST_DEBUG_OBJECT (psink, "scheduling stream status");
1269 psink->defer_pending++;
1270 pa_mainloop_api_once (pa_threaded_mainloop_get_api (mainloop),
1271 mainloop_leave_defer_cb, psink);
1273 pa_threaded_mainloop_unlock (mainloop);
1280 GST_DEBUG_OBJECT (psink, "the server is dead");
1285 /* in_samples >= out_samples, rate > 1.0 */
1286 #define FWD_UP_SAMPLES(s,se,d,de) \
1288 guint8 *sb = s, *db = d; \
1289 while (s <= se && d < de) { \
1290 memcpy (d, s, bpf); \
1293 if ((*accum << 1) >= inr) { \
1298 in_samples -= (s - sb)/bpf; \
1299 out_samples -= (d - db)/bpf; \
1300 GST_DEBUG ("fwd_up end %d/%d",*accum,*toprocess); \
1303 /* out_samples > in_samples, for rates smaller than 1.0 */
1304 #define FWD_DOWN_SAMPLES(s,se,d,de) \
1306 guint8 *sb = s, *db = d; \
1307 while (s <= se && d < de) { \
1308 memcpy (d, s, bpf); \
1311 if ((*accum << 1) >= outr) { \
1316 in_samples -= (s - sb)/bpf; \
1317 out_samples -= (d - db)/bpf; \
1318 GST_DEBUG ("fwd_down end %d/%d",*accum,*toprocess); \
1321 #define REV_UP_SAMPLES(s,se,d,de) \
1323 guint8 *sb = se, *db = d; \
1324 while (s <= se && d < de) { \
1325 memcpy (d, se, bpf); \
1328 while (d < de && (*accum << 1) >= inr) { \
1333 in_samples -= (sb - se)/bpf; \
1334 out_samples -= (d - db)/bpf; \
1335 GST_DEBUG ("rev_up end %d/%d",*accum,*toprocess); \
1338 #define REV_DOWN_SAMPLES(s,se,d,de) \
1340 guint8 *sb = se, *db = d; \
1341 while (s <= se && d < de) { \
1342 memcpy (d, se, bpf); \
1345 while (s <= se && (*accum << 1) >= outr) { \
1350 in_samples -= (sb - se)/bpf; \
1351 out_samples -= (d - db)/bpf; \
1352 GST_DEBUG ("rev_down end %d/%d",*accum,*toprocess); \
1355 /* our custom commit function because we write into the buffer of pulseaudio
1356 * instead of keeping our own buffer */
1358 gst_pulseringbuffer_commit (GstAudioRingBuffer * buf, guint64 * sample,
1359 guchar * data, gint in_samples, gint out_samples, gint * accum)
1361 GstPulseSink *psink;
1362 GstPulseRingBuffer *pbuf;
1367 gint inr, outr, bpf;
1371 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1372 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1374 /* FIXME post message rather than using a signal (as mixer interface) */
1375 if (g_atomic_int_compare_and_exchange (&psink->notify, 1, 0)) {
1376 g_object_notify (G_OBJECT (psink), "volume");
1377 g_object_notify (G_OBJECT (psink), "mute");
1380 /* make sure the ringbuffer is started */
1381 if (G_UNLIKELY (g_atomic_int_get (&buf->state) !=
1382 GST_AUDIO_RING_BUFFER_STATE_STARTED)) {
1383 /* see if we are allowed to start it */
1384 if (G_UNLIKELY (g_atomic_int_get (&buf->may_start) == FALSE))
1387 GST_DEBUG_OBJECT (buf, "start!");
1388 if (!gst_audio_ring_buffer_start (buf))
1392 pa_threaded_mainloop_lock (mainloop);
1394 GST_DEBUG_OBJECT (psink, "entering commit");
1395 pbuf->in_commit = TRUE;
1397 bpf = GST_AUDIO_INFO_BPF (&buf->spec.info);
1398 bufsize = buf->spec.segsize * buf->spec.segtotal;
1400 /* our toy resampler for trick modes */
1401 reverse = out_samples < 0;
1402 out_samples = ABS (out_samples);
1404 if (in_samples >= out_samples)
1405 toprocess = &in_samples;
1407 toprocess = &out_samples;
1409 inr = in_samples - 1;
1410 outr = out_samples - 1;
1412 GST_DEBUG_OBJECT (psink, "in %d, out %d", inr, outr);
1414 /* data_end points to the last sample we have to write, not past it. This is
1415 * needed to properly handle reverse playback: it points to the last sample. */
1416 data_end = data + (bpf * inr);
1418 if (g_atomic_int_get (&psink->format_lost)) {
1419 /* Sink format changed, drop the data and hope upstream renegotiates */
1426 /* offset is in bytes */
1427 offset = *sample * bpf;
1429 while (*toprocess > 0) {
1433 GST_LOG_OBJECT (psink,
1434 "need to write %d samples at offset %" G_GINT64_FORMAT, *toprocess,
1437 if (offset != pbuf->m_lastoffset)
1438 GST_LOG_OBJECT (psink, "discontinuity, offset is %" G_GINT64_FORMAT ", "
1439 "last offset was %" G_GINT64_FORMAT, offset, pbuf->m_lastoffset);
1441 towrite = out_samples * bpf;
1443 /* Wait for at least segsize bytes to become available */
1444 if (towrite > buf->spec.segsize)
1445 towrite = buf->spec.segsize;
1447 if ((pbuf->m_writable < towrite) || (offset != pbuf->m_lastoffset)) {
1448 /* if no room left or discontinuity in offset,
1449 we need to flush data and get a new buffer */
1451 /* flush the buffer if possible */
1452 if ((pbuf->m_data != NULL) && (pbuf->m_towrite > 0)) {
1454 GST_LOG_OBJECT (psink,
1455 "flushing %u samples at offset %" G_GINT64_FORMAT,
1456 (guint) pbuf->m_towrite / bpf, pbuf->m_offset);
1458 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1459 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1463 pbuf->m_towrite = 0;
1464 pbuf->m_offset = offset; /* keep track of current offset */
1466 /* get a buffer to write in for now on */
1468 pbuf->m_writable = pa_stream_writable_size (pbuf->stream);
1470 if (g_atomic_int_get (&psink->format_lost)) {
1471 /* Sink format changed, give up and hope upstream renegotiates */
1475 if (pbuf->m_writable == (size_t) - 1)
1476 goto writable_size_failed;
1478 pbuf->m_writable /= bpf;
1479 pbuf->m_writable *= bpf; /* handle only complete samples */
1481 if (pbuf->m_writable >= towrite)
1484 /* see if we need to uncork because we have no free space */
1486 if (!gst_pulsering_set_corked (pbuf, FALSE, FALSE))
1490 /* we can't write segsize bytes, wait a bit */
1491 GST_LOG_OBJECT (psink, "waiting for free space");
1492 pa_threaded_mainloop_wait (mainloop);
1498 /* Recalculate what we can write in the next chunk */
1499 towrite = out_samples * bpf;
1500 if (pbuf->m_writable > towrite)
1501 pbuf->m_writable = towrite;
1503 GST_LOG_OBJECT (psink, "requesting %" G_GSIZE_FORMAT " bytes of "
1504 "shared memory", pbuf->m_writable);
1506 if (pa_stream_begin_write (pbuf->stream, &pbuf->m_data,
1507 &pbuf->m_writable) < 0) {
1508 GST_LOG_OBJECT (psink, "pa_stream_begin_write() failed");
1509 goto writable_size_failed;
1512 GST_LOG_OBJECT (psink, "got %" G_GSIZE_FORMAT " bytes of shared memory",
1517 if (towrite > pbuf->m_writable)
1518 towrite = pbuf->m_writable;
1519 avail = towrite / bpf;
1521 GST_LOG_OBJECT (psink, "writing %u samples at offset %" G_GUINT64_FORMAT,
1522 (guint) avail, offset);
1524 /* No trick modes for passthrough streams */
1525 if (G_UNLIKELY (!pbuf->is_pcm && (inr != outr || reverse))) {
1526 GST_WARNING_OBJECT (psink, "Passthrough stream can't run in trick mode");
1527 goto unlock_and_fail;
1530 if (G_LIKELY (inr == outr && !reverse)) {
1531 /* no rate conversion, simply write out the samples */
1532 /* copy the data into internal buffer */
1534 memcpy ((guint8 *) pbuf->m_data + pbuf->m_towrite, data, towrite);
1535 pbuf->m_towrite += towrite;
1536 pbuf->m_writable -= towrite;
1539 in_samples -= avail;
1540 out_samples -= avail;
1542 guint8 *dest, *d, *d_end;
1544 /* write into the PulseAudio shm buffer */
1545 dest = d = (guint8 *) pbuf->m_data + pbuf->m_towrite;
1546 d_end = d + towrite;
1550 /* forward speed up */
1551 FWD_UP_SAMPLES (data, data_end, d, d_end);
1553 /* forward slow down */
1554 FWD_DOWN_SAMPLES (data, data_end, d, d_end);
1557 /* reverse speed up */
1558 REV_UP_SAMPLES (data, data_end, d, d_end);
1560 /* reverse slow down */
1561 REV_DOWN_SAMPLES (data, data_end, d, d_end);
1563 /* see what we have left to write */
1564 towrite = (d - dest);
1565 pbuf->m_towrite += towrite;
1566 pbuf->m_writable -= towrite;
1568 avail = towrite / bpf;
1571 /* flush the buffer if it's full */
1572 if ((pbuf->m_data != NULL) && (pbuf->m_towrite > 0)
1573 && (pbuf->m_writable == 0)) {
1574 GST_LOG_OBJECT (psink, "flushing %u samples at offset %" G_GINT64_FORMAT,
1575 (guint) pbuf->m_towrite / bpf, pbuf->m_offset);
1577 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1578 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1581 pbuf->m_towrite = 0;
1582 pbuf->m_offset = offset + towrite; /* keep track of current offset */
1586 offset += avail * bpf;
1587 pbuf->m_lastoffset = offset;
1589 /* check if we need to uncork after writing the samples */
1591 const pa_timing_info *info;
1593 if ((info = pa_stream_get_timing_info (pbuf->stream))) {
1594 GST_LOG_OBJECT (psink,
1595 "read_index at %" G_GUINT64_FORMAT ", offset %" G_GINT64_FORMAT,
1596 info->read_index, offset);
1598 /* we uncork when the read_index is too far behind the offset we need
1600 if (info->read_index + bufsize <= offset) {
1601 if (!gst_pulsering_set_corked (pbuf, FALSE, FALSE))
1605 GST_LOG_OBJECT (psink, "no timing info available yet");
1611 /* we consumed all samples here */
1612 data = data_end + bpf;
1614 pbuf->in_commit = FALSE;
1615 pa_threaded_mainloop_unlock (mainloop);
1618 result = inr - ((data_end - data) / bpf);
1619 GST_LOG_OBJECT (psink, "wrote %d samples", result);
1626 pbuf->in_commit = FALSE;
1627 GST_LOG_OBJECT (psink, "we are reset");
1628 pa_threaded_mainloop_unlock (mainloop);
1633 GST_LOG_OBJECT (psink, "we can not start");
1638 GST_LOG_OBJECT (psink, "failed to start the ringbuffer");
1643 pbuf->in_commit = FALSE;
1644 GST_ERROR_OBJECT (psink, "uncork failed");
1645 pa_threaded_mainloop_unlock (mainloop);
1650 pbuf->in_commit = FALSE;
1651 GST_LOG_OBJECT (psink, "we are paused");
1652 pa_threaded_mainloop_unlock (mainloop);
1655 writable_size_failed:
1657 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1658 ("pa_stream_writable_size() failed: %s",
1659 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1660 goto unlock_and_fail;
1664 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1665 ("pa_stream_write() failed: %s",
1666 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1667 goto unlock_and_fail;
1671 /* write pending local samples, must be called with the mainloop lock */
1673 gst_pulsering_flush (GstPulseRingBuffer * pbuf)
1675 GstPulseSink *psink;
1677 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1678 GST_DEBUG_OBJECT (psink, "entering flush");
1680 /* flush the buffer if possible */
1681 if (pbuf->stream && (pbuf->m_data != NULL) && (pbuf->m_towrite > 0)) {
1682 #ifndef GST_DISABLE_GST_DEBUG
1685 bpf = (GST_AUDIO_RING_BUFFER_CAST (pbuf))->spec.info.bpf;
1686 GST_LOG_OBJECT (psink,
1687 "flushing %u samples at offset %" G_GINT64_FORMAT,
1688 (guint) pbuf->m_towrite / bpf, pbuf->m_offset);
1691 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1692 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1696 pbuf->m_towrite = 0;
1697 pbuf->m_offset += pbuf->m_towrite; /* keep track of current offset */
1706 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1707 ("pa_stream_write() failed: %s",
1708 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1713 static void gst_pulsesink_set_property (GObject * object, guint prop_id,
1714 const GValue * value, GParamSpec * pspec);
1715 static void gst_pulsesink_get_property (GObject * object, guint prop_id,
1716 GValue * value, GParamSpec * pspec);
1717 static void gst_pulsesink_finalize (GObject * object);
1719 static gboolean gst_pulsesink_event (GstBaseSink * sink, GstEvent * event);
1720 static gboolean gst_pulsesink_query (GstBaseSink * sink, GstQuery * query);
1722 static GstStateChangeReturn gst_pulsesink_change_state (GstElement * element,
1723 GstStateChange transition);
1725 static GstStaticPadTemplate pad_template = GST_STATIC_PAD_TEMPLATE ("sink",
1728 GST_STATIC_CAPS (PULSE_SINK_TEMPLATE_CAPS));
1730 #define gst_pulsesink_parent_class parent_class
1731 G_DEFINE_TYPE_WITH_CODE (GstPulseSink, gst_pulsesink, GST_TYPE_AUDIO_BASE_SINK,
1732 gst_pulsesink_init_contexts ();
1733 G_IMPLEMENT_INTERFACE (GST_TYPE_STREAM_VOLUME, NULL)
1736 static GstAudioRingBuffer *
1737 gst_pulsesink_create_ringbuffer (GstAudioBaseSink * sink)
1739 GstAudioRingBuffer *buffer;
1741 GST_DEBUG_OBJECT (sink, "creating ringbuffer");
1742 buffer = g_object_new (GST_TYPE_PULSERING_BUFFER, NULL);
1743 GST_DEBUG_OBJECT (sink, "created ringbuffer @%p", buffer);
1749 gst_pulsesink_payload (GstAudioBaseSink * sink, GstBuffer * buf)
1751 switch (sink->ringbuffer->spec.type) {
1752 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3:
1753 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_EAC3:
1754 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS:
1755 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_MPEG:
1757 /* FIXME: alloc memory from PA if possible */
1758 gint framesize = gst_audio_iec61937_frame_size (&sink->ringbuffer->spec);
1760 GstMapInfo inmap, outmap;
1766 out = gst_buffer_new_and_alloc (framesize);
1768 gst_buffer_map (buf, &inmap, GST_MAP_READ);
1769 gst_buffer_map (out, &outmap, GST_MAP_WRITE);
1771 res = gst_audio_iec61937_payload (inmap.data, inmap.size,
1772 outmap.data, outmap.size, &sink->ringbuffer->spec, G_BIG_ENDIAN);
1774 gst_buffer_unmap (buf, &inmap);
1775 gst_buffer_unmap (out, &outmap);
1778 gst_buffer_unref (out);
1782 gst_buffer_copy_into (out, buf, GST_BUFFER_COPY_METADATA, 0, -1);
1787 return gst_buffer_ref (buf);
1792 gst_pulsesink_class_init (GstPulseSinkClass * klass)
1794 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1795 GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
1796 GstBaseSinkClass *bc;
1797 GstAudioBaseSinkClass *gstaudiosink_class = GST_AUDIO_BASE_SINK_CLASS (klass);
1798 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1801 gobject_class->finalize = gst_pulsesink_finalize;
1802 gobject_class->set_property = gst_pulsesink_set_property;
1803 gobject_class->get_property = gst_pulsesink_get_property;
1805 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_pulsesink_event);
1806 gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_pulsesink_query);
1808 /* restore the original basesink pull methods */
1809 bc = g_type_class_peek (GST_TYPE_BASE_SINK);
1810 gstbasesink_class->activate_pull = GST_DEBUG_FUNCPTR (bc->activate_pull);
1812 gstelement_class->change_state =
1813 GST_DEBUG_FUNCPTR (gst_pulsesink_change_state);
1815 gstaudiosink_class->create_ringbuffer =
1816 GST_DEBUG_FUNCPTR (gst_pulsesink_create_ringbuffer);
1817 gstaudiosink_class->payload = GST_DEBUG_FUNCPTR (gst_pulsesink_payload);
1819 /* Overwrite GObject fields */
1820 g_object_class_install_property (gobject_class,
1822 g_param_spec_string ("server", "Server",
1823 "The PulseAudio server to connect to", DEFAULT_SERVER,
1824 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1826 g_object_class_install_property (gobject_class, PROP_DEVICE,
1827 g_param_spec_string ("device", "Device",
1828 "The PulseAudio sink device to connect to", DEFAULT_DEVICE,
1829 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1831 g_object_class_install_property (gobject_class,
1833 g_param_spec_string ("device-name", "Device name",
1834 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
1835 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1837 g_object_class_install_property (gobject_class,
1839 g_param_spec_double ("volume", "Volume",
1840 "Linear volume of this stream, 1.0=100%", 0.0, MAX_VOLUME,
1841 DEFAULT_VOLUME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1842 g_object_class_install_property (gobject_class,
1844 g_param_spec_boolean ("mute", "Mute",
1845 "Mute state of this stream", DEFAULT_MUTE,
1846 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1849 * GstPulseSink:client-name
1851 * The PulseAudio client name to use.
1853 clientname = gst_pulse_client_name ();
1854 g_object_class_install_property (gobject_class,
1856 g_param_spec_string ("client-name", "Client Name",
1857 "The PulseAudio client name to use", clientname,
1858 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1859 GST_PARAM_MUTABLE_READY));
1860 g_free (clientname);
1863 * GstPulseSink:stream-properties
1865 * List of pulseaudio stream properties. A list of defined properties can be
1866 * found in the <ulink url="http://0pointer.de/lennart/projects/pulseaudio/doxygen/proplist_8h.html">pulseaudio api docs</ulink>.
1868 * Below is an example for registering as a music application to pulseaudio.
1870 * GstStructure *props;
1872 * props = gst_structure_from_string ("props,media.role=music", NULL);
1873 * g_object_set (pulse, "stream-properties", props, NULL);
1874 * gst_structure_free
1879 g_object_class_install_property (gobject_class,
1880 PROP_STREAM_PROPERTIES,
1881 g_param_spec_boxed ("stream-properties", "stream properties",
1882 "list of pulseaudio stream properties",
1883 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1885 gst_element_class_set_static_metadata (gstelement_class,
1886 "PulseAudio Audio Sink",
1887 "Sink/Audio", "Plays audio to a PulseAudio server", "Lennart Poettering");
1888 gst_element_class_add_pad_template (gstelement_class,
1889 gst_static_pad_template_get (&pad_template));
1892 /* Returns the current time of the sink ringbuffer. The timing_info is updated
1893 * on every data write/flush and every 100ms (PA_STREAM_AUTO_TIMING_UPDATE).
1896 gst_pulsesink_get_time (GstClock * clock, GstAudioBaseSink * sink)
1898 GstPulseSink *psink;
1899 GstPulseRingBuffer *pbuf;
1902 if (!sink->ringbuffer || !sink->ringbuffer->acquired)
1903 return GST_CLOCK_TIME_NONE;
1905 pbuf = GST_PULSERING_BUFFER_CAST (sink->ringbuffer);
1906 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1908 if (g_atomic_int_get (&psink->format_lost)) {
1909 /* Stream was lost in a format change, it'll get set up again once
1910 * upstream renegotiates */
1911 return psink->format_lost_time;
1914 pa_threaded_mainloop_lock (mainloop);
1915 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
1918 /* if we don't have enough data to get a timestamp, just return NONE, which
1919 * will return the last reported time */
1920 if (pa_stream_get_time (pbuf->stream, &time) < 0) {
1921 GST_DEBUG_OBJECT (psink, "could not get time");
1922 time = GST_CLOCK_TIME_NONE;
1925 pa_threaded_mainloop_unlock (mainloop);
1927 GST_LOG_OBJECT (psink, "current time is %" GST_TIME_FORMAT,
1928 GST_TIME_ARGS (time));
1935 GST_DEBUG_OBJECT (psink, "the server is dead");
1936 pa_threaded_mainloop_unlock (mainloop);
1938 return GST_CLOCK_TIME_NONE;
1943 gst_pulsesink_sink_info_cb (pa_context * c, const pa_sink_info * i, int eol,
1946 GstPulseRingBuffer *pbuf;
1947 GstPulseSink *psink;
1951 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
1952 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1957 g_free (psink->device_description);
1958 psink->device_description = g_strdup (i->description);
1960 g_mutex_lock (&psink->sink_formats_lock);
1962 for (l = g_list_first (psink->sink_formats); l; l = g_list_next (l))
1963 pa_format_info_free ((pa_format_info *) l->data);
1965 g_list_free (psink->sink_formats);
1966 psink->sink_formats = NULL;
1968 for (j = 0; j < i->n_formats; j++)
1969 psink->sink_formats = g_list_prepend (psink->sink_formats,
1970 pa_format_info_copy (i->formats[j]));
1972 g_mutex_unlock (&psink->sink_formats_lock);
1975 pa_threaded_mainloop_signal (mainloop, 0);
1979 gst_pulsesink_query_acceptcaps (GstPulseSink * psink, GstCaps * caps)
1981 GstPulseRingBuffer *pbuf = NULL;
1984 gboolean ret = FALSE;
1986 GstAudioRingBufferSpec spec = { 0 };
1987 pa_stream *stream = NULL;
1988 pa_operation *o = NULL;
1989 pa_channel_map channel_map;
1990 pa_stream_flags_t flags;
1991 pa_format_info *format = NULL, *formats[1];
1994 pad_caps = gst_pad_query_caps (GST_BASE_SINK_PAD (psink), caps);
1995 ret = pad_caps != NULL;
1996 gst_caps_unref (pad_caps);
1998 GST_DEBUG_OBJECT (psink, "caps %" GST_PTR_FORMAT, caps);
2000 /* Template caps didn't match */
2004 /* If we've not got fixed caps, creating a stream might fail, so let's just
2005 * return from here with default acceptcaps behaviour */
2006 if (!gst_caps_is_fixed (caps))
2009 GST_OBJECT_LOCK (psink);
2010 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2012 gst_object_ref (pbuf);
2013 GST_OBJECT_UNLOCK (psink);
2015 /* We're still in NULL state */
2019 pa_threaded_mainloop_lock (mainloop);
2021 if (pbuf->context == NULL)
2026 spec.latency_time = GST_AUDIO_BASE_SINK (psink)->latency_time;
2027 if (!gst_audio_ring_buffer_parse_caps (&spec, caps))
2030 if (!gst_pulse_fill_format_info (&spec, &format, &channels))
2033 /* Make sure input is framed (one frame per buffer) and can be payloaded */
2034 if (!pa_format_info_is_pcm (format)) {
2035 gboolean framed = FALSE, parsed = FALSE;
2036 st = gst_caps_get_structure (caps, 0);
2038 gst_structure_get_boolean (st, "framed", &framed);
2039 gst_structure_get_boolean (st, "parsed", &parsed);
2040 if ((!framed && !parsed) || gst_audio_iec61937_frame_size (&spec) <= 0)
2044 /* initialize the channel map */
2045 if (pa_format_info_is_pcm (format) &&
2046 gst_pulse_gst_to_channel_map (&channel_map, &spec))
2047 pa_format_info_set_channel_map (format, &channel_map);
2050 /* We're already in PAUSED or above, so just reuse this stream to query
2051 * sink formats and use those. */
2054 if (!(o = pa_context_get_sink_info_by_name (pbuf->context, psink->device,
2055 gst_pulsesink_sink_info_cb, pbuf)))
2058 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2059 pa_threaded_mainloop_wait (mainloop);
2060 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
2064 g_mutex_lock (&psink->sink_formats_lock);
2065 for (i = g_list_first (psink->sink_formats); i; i = g_list_next (i)) {
2066 if (pa_format_info_is_compatible ((pa_format_info *) i->data, format)) {
2071 g_mutex_unlock (&psink->sink_formats_lock);
2073 /* We're in READY, let's connect a stream to see if the format is
2074 * accpeted by whatever sink we're routed to */
2075 formats[0] = format;
2077 if (!(stream = pa_stream_new_extended (pbuf->context, "pulsesink probe",
2078 formats, 1, psink->proplist)))
2081 /* construct the flags */
2082 flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE |
2083 PA_STREAM_ADJUST_LATENCY | PA_STREAM_START_CORKED;
2085 pa_stream_set_state_callback (stream, gst_pulsering_stream_state_cb, pbuf);
2087 if (pa_stream_connect_playback (stream, psink->device, NULL, flags, NULL,
2091 ret = gst_pulsering_wait_for_stream_ready (psink, stream);
2096 pa_format_info_free (format);
2099 pa_operation_unref (o);
2102 pa_stream_set_state_callback (stream, NULL, NULL);
2103 pa_stream_disconnect (stream);
2104 pa_stream_unref (stream);
2107 pa_threaded_mainloop_unlock (mainloop);
2109 gst_caps_replace (&spec.caps, NULL);
2110 gst_object_unref (pbuf);
2118 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2119 ("pa_context_get_sink_input_info() failed: %s",
2120 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2126 gst_pulsesink_init (GstPulseSink * pulsesink)
2128 pulsesink->server = NULL;
2129 pulsesink->device = NULL;
2130 pulsesink->device_description = NULL;
2131 pulsesink->client_name = gst_pulse_client_name ();
2133 g_mutex_init (&pulsesink->sink_formats_lock);
2134 pulsesink->sink_formats = NULL;
2136 pulsesink->volume = DEFAULT_VOLUME;
2137 pulsesink->volume_set = FALSE;
2139 pulsesink->mute = DEFAULT_MUTE;
2140 pulsesink->mute_set = FALSE;
2142 pulsesink->notify = 0;
2144 g_atomic_int_set (&pulsesink->format_lost, FALSE);
2145 pulsesink->format_lost_time = GST_CLOCK_TIME_NONE;
2147 pulsesink->properties = NULL;
2148 pulsesink->proplist = NULL;
2150 /* override with a custom clock */
2151 if (GST_AUDIO_BASE_SINK (pulsesink)->provided_clock)
2152 gst_object_unref (GST_AUDIO_BASE_SINK (pulsesink)->provided_clock);
2154 GST_AUDIO_BASE_SINK (pulsesink)->provided_clock =
2155 gst_audio_clock_new ("GstPulseSinkClock",
2156 (GstAudioClockGetTimeFunc) gst_pulsesink_get_time, pulsesink, NULL);
2158 /* TRUE for sinks, FALSE for sources */
2159 pulsesink->probe = gst_pulseprobe_new (G_OBJECT (pulsesink),
2160 G_OBJECT_GET_CLASS (pulsesink), PROP_DEVICE, pulsesink->device,
2165 gst_pulsesink_finalize (GObject * object)
2167 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2170 g_free (pulsesink->server);
2171 g_free (pulsesink->device);
2172 g_free (pulsesink->device_description);
2173 g_free (pulsesink->client_name);
2175 for (i = g_list_first (pulsesink->sink_formats); i; i = g_list_next (i))
2176 pa_format_info_free ((pa_format_info *) i->data);
2178 g_list_free (pulsesink->sink_formats);
2179 g_mutex_clear (&pulsesink->sink_formats_lock);
2181 if (pulsesink->properties)
2182 gst_structure_free (pulsesink->properties);
2183 if (pulsesink->proplist)
2184 pa_proplist_free (pulsesink->proplist);
2186 if (pulsesink->probe) {
2187 gst_pulseprobe_free (pulsesink->probe);
2188 pulsesink->probe = NULL;
2191 G_OBJECT_CLASS (parent_class)->finalize (object);
2195 gst_pulsesink_set_volume (GstPulseSink * psink, gdouble volume)
2198 pa_operation *o = NULL;
2199 GstPulseRingBuffer *pbuf;
2205 pa_threaded_mainloop_lock (mainloop);
2207 GST_DEBUG_OBJECT (psink, "setting volume to %f", volume);
2209 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2210 if (pbuf == NULL || pbuf->stream == NULL)
2213 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2217 gst_pulse_cvolume_from_linear (&v, pbuf->channels, volume);
2219 /* FIXME: this will eventually be superceded by checks to see if the volume
2220 * is readable/writable */
2223 if (!(o = pa_context_set_sink_input_volume (pbuf->context, idx,
2227 /* We don't really care about the result of this call */
2231 pa_operation_unref (o);
2233 pa_threaded_mainloop_unlock (mainloop);
2240 psink->volume = volume;
2241 psink->volume_set = TRUE;
2243 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2248 psink->volume = volume;
2249 psink->volume_set = TRUE;
2251 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2256 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2261 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2262 ("pa_stream_set_sink_input_volume() failed: %s",
2263 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2269 gst_pulsesink_set_mute (GstPulseSink * psink, gboolean mute)
2271 pa_operation *o = NULL;
2272 GstPulseRingBuffer *pbuf;
2278 pa_threaded_mainloop_lock (mainloop);
2280 GST_DEBUG_OBJECT (psink, "setting mute state to %d", mute);
2282 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2283 if (pbuf == NULL || pbuf->stream == NULL)
2286 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2289 if (!(o = pa_context_set_sink_input_mute (pbuf->context, idx,
2293 /* We don't really care about the result of this call */
2297 pa_operation_unref (o);
2299 pa_threaded_mainloop_unlock (mainloop);
2307 psink->mute_set = TRUE;
2309 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2315 psink->mute_set = TRUE;
2317 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2322 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2327 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2328 ("pa_stream_set_sink_input_mute() failed: %s",
2329 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2335 gst_pulsesink_sink_input_info_cb (pa_context * c, const pa_sink_input_info * i,
2336 int eol, void *userdata)
2338 GstPulseRingBuffer *pbuf;
2339 GstPulseSink *psink;
2341 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
2342 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
2350 /* If the index doesn't match our current stream,
2351 * it implies we just recreated the stream (caps change)
2353 if (i->index == pa_stream_get_index (pbuf->stream)) {
2354 psink->volume = pa_sw_volume_to_linear (pa_cvolume_max (&i->volume));
2355 psink->mute = i->mute;
2359 pa_threaded_mainloop_signal (mainloop, 0);
2363 gst_pulsesink_get_volume (GstPulseSink * psink)
2365 GstPulseRingBuffer *pbuf;
2366 pa_operation *o = NULL;
2367 gdouble v = DEFAULT_VOLUME;
2373 pa_threaded_mainloop_lock (mainloop);
2375 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2376 if (pbuf == NULL || pbuf->stream == NULL)
2379 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2382 if (!(o = pa_context_get_sink_input_info (pbuf->context, idx,
2383 gst_pulsesink_sink_input_info_cb, pbuf)))
2386 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2387 pa_threaded_mainloop_wait (mainloop);
2388 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
2396 pa_operation_unref (o);
2398 pa_threaded_mainloop_unlock (mainloop);
2400 if (v > MAX_VOLUME) {
2401 GST_WARNING_OBJECT (psink, "Clipped volume from %f to %f", v, MAX_VOLUME);
2411 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2416 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2421 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2426 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2427 ("pa_context_get_sink_input_info() failed: %s",
2428 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2434 gst_pulsesink_get_mute (GstPulseSink * psink)
2436 GstPulseRingBuffer *pbuf;
2437 pa_operation *o = NULL;
2439 gboolean mute = FALSE;
2444 pa_threaded_mainloop_lock (mainloop);
2447 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2448 if (pbuf == NULL || pbuf->stream == NULL)
2451 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2454 if (!(o = pa_context_get_sink_input_info (pbuf->context, idx,
2455 gst_pulsesink_sink_input_info_cb, pbuf)))
2458 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2459 pa_threaded_mainloop_wait (mainloop);
2460 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
2466 pa_operation_unref (o);
2468 pa_threaded_mainloop_unlock (mainloop);
2476 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2481 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2486 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2491 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2492 ("pa_context_get_sink_input_info() failed: %s",
2493 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2499 gst_pulsesink_device_description (GstPulseSink * psink)
2501 GstPulseRingBuffer *pbuf;
2502 pa_operation *o = NULL;
2508 pa_threaded_mainloop_lock (mainloop);
2509 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2513 if (!(o = pa_context_get_sink_info_by_name (pbuf->context,
2514 psink->device, gst_pulsesink_sink_info_cb, pbuf)))
2517 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2518 pa_threaded_mainloop_wait (mainloop);
2519 if (gst_pulsering_is_dead (psink, pbuf, FALSE))
2525 pa_operation_unref (o);
2527 t = g_strdup (psink->device_description);
2528 pa_threaded_mainloop_unlock (mainloop);
2535 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2540 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2545 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2546 ("pa_context_get_sink_info_by_index() failed: %s",
2547 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2553 gst_pulsesink_set_property (GObject * object,
2554 guint prop_id, const GValue * value, GParamSpec * pspec)
2556 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2560 g_free (pulsesink->server);
2561 pulsesink->server = g_value_dup_string (value);
2562 if (pulsesink->probe)
2563 gst_pulseprobe_set_server (pulsesink->probe, pulsesink->server);
2566 g_free (pulsesink->device);
2567 pulsesink->device = g_value_dup_string (value);
2570 gst_pulsesink_set_volume (pulsesink, g_value_get_double (value));
2573 gst_pulsesink_set_mute (pulsesink, g_value_get_boolean (value));
2575 case PROP_CLIENT_NAME:
2576 g_free (pulsesink->client_name);
2577 if (!g_value_get_string (value)) {
2578 GST_WARNING_OBJECT (pulsesink,
2579 "Empty PulseAudio client name not allowed. Resetting to default value");
2580 pulsesink->client_name = gst_pulse_client_name ();
2582 pulsesink->client_name = g_value_dup_string (value);
2584 case PROP_STREAM_PROPERTIES:
2585 if (pulsesink->properties)
2586 gst_structure_free (pulsesink->properties);
2587 pulsesink->properties =
2588 gst_structure_copy (gst_value_get_structure (value));
2589 if (pulsesink->proplist)
2590 pa_proplist_free (pulsesink->proplist);
2591 pulsesink->proplist = gst_pulse_make_proplist (pulsesink->properties);
2594 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2600 gst_pulsesink_get_property (GObject * object,
2601 guint prop_id, GValue * value, GParamSpec * pspec)
2604 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2608 g_value_set_string (value, pulsesink->server);
2611 g_value_set_string (value, pulsesink->device);
2613 case PROP_DEVICE_NAME:
2614 g_value_take_string (value, gst_pulsesink_device_description (pulsesink));
2617 g_value_set_double (value, gst_pulsesink_get_volume (pulsesink));
2620 g_value_set_boolean (value, gst_pulsesink_get_mute (pulsesink));
2622 case PROP_CLIENT_NAME:
2623 g_value_set_string (value, pulsesink->client_name);
2625 case PROP_STREAM_PROPERTIES:
2626 gst_value_set_structure (value, pulsesink->properties);
2629 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2635 gst_pulsesink_change_title (GstPulseSink * psink, const gchar * t)
2637 pa_operation *o = NULL;
2638 GstPulseRingBuffer *pbuf;
2640 pa_threaded_mainloop_lock (mainloop);
2642 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2644 if (pbuf == NULL || pbuf->stream == NULL)
2647 g_free (pbuf->stream_name);
2648 pbuf->stream_name = g_strdup (t);
2650 if (!(o = pa_stream_set_name (pbuf->stream, pbuf->stream_name, NULL, NULL)))
2653 /* We're not interested if this operation failed or not */
2657 pa_operation_unref (o);
2658 pa_threaded_mainloop_unlock (mainloop);
2665 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2670 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2671 ("pa_stream_set_name() failed: %s",
2672 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2678 gst_pulsesink_change_props (GstPulseSink * psink, GstTagList * l)
2680 static const gchar *const map[] = {
2681 GST_TAG_TITLE, PA_PROP_MEDIA_TITLE,
2683 /* might get overriden in the next iteration by GST_TAG_ARTIST */
2684 GST_TAG_PERFORMER, PA_PROP_MEDIA_ARTIST,
2686 GST_TAG_ARTIST, PA_PROP_MEDIA_ARTIST,
2687 GST_TAG_LANGUAGE_CODE, PA_PROP_MEDIA_LANGUAGE,
2688 GST_TAG_LOCATION, PA_PROP_MEDIA_FILENAME,
2689 /* We might add more here later on ... */
2692 pa_proplist *pl = NULL;
2693 const gchar *const *t;
2694 gboolean empty = TRUE;
2695 pa_operation *o = NULL;
2696 GstPulseRingBuffer *pbuf;
2698 pl = pa_proplist_new ();
2700 for (t = map; *t; t += 2) {
2703 if (gst_tag_list_get_string (l, *t, &n)) {
2706 pa_proplist_sets (pl, *(t + 1), n);
2716 pa_threaded_mainloop_lock (mainloop);
2717 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2718 if (pbuf == NULL || pbuf->stream == NULL)
2721 /* We're not interested if this operation failed or not */
2722 if (!(o = pa_stream_proplist_update (pbuf->stream, PA_UPDATE_REPLACE,
2724 GST_DEBUG_OBJECT (psink, "pa_stream_proplist_update() failed");
2730 pa_operation_unref (o);
2732 pa_threaded_mainloop_unlock (mainloop);
2737 pa_proplist_free (pl);
2744 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2750 gst_pulsesink_flush_ringbuffer (GstPulseSink * psink)
2752 GstPulseRingBuffer *pbuf;
2754 pa_threaded_mainloop_lock (mainloop);
2756 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2758 if (pbuf == NULL || pbuf->stream == NULL)
2761 gst_pulsering_flush (pbuf);
2763 /* Uncork if we haven't already (happens when waiting to get enough data
2764 * to send out the first time) */
2766 gst_pulsering_set_corked (pbuf, FALSE, FALSE);
2768 /* We're not interested if this operation failed or not */
2770 pa_threaded_mainloop_unlock (mainloop);
2777 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2783 gst_pulsesink_event (GstBaseSink * sink, GstEvent * event)
2785 GstPulseSink *pulsesink = GST_PULSESINK_CAST (sink);
2787 switch (GST_EVENT_TYPE (event)) {
2788 case GST_EVENT_TAG:{
2789 gchar *title = NULL, *artist = NULL, *location = NULL, *description =
2790 NULL, *t = NULL, *buf = NULL;
2793 gst_event_parse_tag (event, &l);
2795 gst_tag_list_get_string (l, GST_TAG_TITLE, &title);
2796 gst_tag_list_get_string (l, GST_TAG_ARTIST, &artist);
2797 gst_tag_list_get_string (l, GST_TAG_LOCATION, &location);
2798 gst_tag_list_get_string (l, GST_TAG_DESCRIPTION, &description);
2801 gst_tag_list_get_string (l, GST_TAG_PERFORMER, &artist);
2803 if (title && artist)
2804 /* TRANSLATORS: 'song title' by 'artist name' */
2805 t = buf = g_strdup_printf (_("'%s' by '%s'"), g_strstrip (title),
2806 g_strstrip (artist));
2808 t = g_strstrip (title);
2809 else if (description)
2810 t = g_strstrip (description);
2812 t = g_strstrip (location);
2815 gst_pulsesink_change_title (pulsesink, t);
2820 g_free (description);
2823 gst_pulsesink_change_props (pulsesink, l);
2827 case GST_EVENT_GAP:{
2828 GstClockTime timestamp, duration;
2830 gst_event_parse_gap (event, ×tamp, &duration);
2831 if (duration == GST_CLOCK_TIME_NONE)
2832 gst_pulsesink_flush_ringbuffer (pulsesink);
2836 gst_pulsesink_flush_ringbuffer (pulsesink);
2842 return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
2846 gst_pulsesink_query (GstBaseSink * sink, GstQuery * query)
2848 GstPulseSink *pulsesink = GST_PULSESINK_CAST (sink);
2851 switch (GST_QUERY_TYPE (query)) {
2852 case GST_QUERY_ACCEPT_CAPS:
2856 gst_query_parse_accept_caps (query, &caps);
2857 ret = gst_pulsesink_query_acceptcaps (pulsesink, caps);
2858 gst_query_set_accept_caps_result (query, ret);
2863 ret = GST_BASE_SINK_CLASS (parent_class)->query (sink, query);
2870 gst_pulsesink_release_mainloop (GstPulseSink * psink)
2875 pa_threaded_mainloop_lock (mainloop);
2876 while (psink->defer_pending) {
2877 GST_DEBUG_OBJECT (psink, "waiting for stream status message emission");
2878 pa_threaded_mainloop_wait (mainloop);
2880 pa_threaded_mainloop_unlock (mainloop);
2882 g_mutex_lock (&pa_shared_resource_mutex);
2884 if (!mainloop_ref_ct) {
2885 GST_INFO_OBJECT (psink, "terminating pa main loop thread");
2886 pa_threaded_mainloop_stop (mainloop);
2887 pa_threaded_mainloop_free (mainloop);
2890 g_mutex_unlock (&pa_shared_resource_mutex);
2893 static GstStateChangeReturn
2894 gst_pulsesink_change_state (GstElement * element, GstStateChange transition)
2896 GstPulseSink *pulsesink = GST_PULSESINK (element);
2897 GstStateChangeReturn ret;
2899 switch (transition) {
2900 case GST_STATE_CHANGE_NULL_TO_READY:
2901 g_mutex_lock (&pa_shared_resource_mutex);
2902 if (!mainloop_ref_ct) {
2903 GST_INFO_OBJECT (element, "new pa main loop thread");
2904 if (!(mainloop = pa_threaded_mainloop_new ()))
2905 goto mainloop_failed;
2906 if (pa_threaded_mainloop_start (mainloop) < 0) {
2907 pa_threaded_mainloop_free (mainloop);
2908 goto mainloop_start_failed;
2910 mainloop_ref_ct = 1;
2911 g_mutex_unlock (&pa_shared_resource_mutex);
2913 GST_INFO_OBJECT (element, "reusing pa main loop thread");
2915 g_mutex_unlock (&pa_shared_resource_mutex);
2918 case GST_STATE_CHANGE_READY_TO_PAUSED:
2919 gst_element_post_message (element,
2920 gst_message_new_clock_provide (GST_OBJECT_CAST (element),
2921 GST_AUDIO_BASE_SINK (pulsesink)->provided_clock, TRUE));
2928 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2929 if (ret == GST_STATE_CHANGE_FAILURE)
2932 switch (transition) {
2933 case GST_STATE_CHANGE_PAUSED_TO_READY:
2934 /* format_lost is reset in release() in audiobasesink */
2935 gst_element_post_message (element,
2936 gst_message_new_clock_lost (GST_OBJECT_CAST (element),
2937 GST_AUDIO_BASE_SINK (pulsesink)->provided_clock));
2939 case GST_STATE_CHANGE_READY_TO_NULL:
2940 gst_pulsesink_release_mainloop (pulsesink);
2951 g_mutex_unlock (&pa_shared_resource_mutex);
2952 GST_ELEMENT_ERROR (pulsesink, RESOURCE, FAILED,
2953 ("pa_threaded_mainloop_new() failed"), (NULL));
2954 return GST_STATE_CHANGE_FAILURE;
2956 mainloop_start_failed:
2958 g_mutex_unlock (&pa_shared_resource_mutex);
2959 GST_ELEMENT_ERROR (pulsesink, RESOURCE, FAILED,
2960 ("pa_threaded_mainloop_start() failed"), (NULL));
2961 return GST_STATE_CHANGE_FAILURE;
2965 if (transition == GST_STATE_CHANGE_NULL_TO_READY) {
2966 /* Clear the PA mainloop if audiobasesink failed to open the ring_buffer */
2967 g_assert (mainloop);
2968 gst_pulsesink_release_mainloop (pulsesink);