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>
57 #include <gst/audio/gstaudioiec61937.h>
59 #include <gst/pbutils/pbutils.h> /* only used for GST_PLUGINS_BASE_VERSION_* */
61 #include "pulsesink.h"
62 #include "pulseutil.h"
64 GST_DEBUG_CATEGORY_EXTERN (pulse_debug);
65 #define GST_CAT_DEFAULT pulse_debug
67 #define DEFAULT_SERVER NULL
68 #define DEFAULT_DEVICE NULL
69 #define DEFAULT_DEVICE_NAME NULL
70 #define DEFAULT_VOLUME 1.0
71 #define DEFAULT_MUTE FALSE
72 #define MAX_VOLUME 10.0
83 PROP_STREAM_PROPERTIES,
87 #define GST_TYPE_PULSERING_BUFFER \
88 (gst_pulseringbuffer_get_type())
89 #define GST_PULSERING_BUFFER(obj) \
90 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_PULSERING_BUFFER,GstPulseRingBuffer))
91 #define GST_PULSERING_BUFFER_CLASS(klass) \
92 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_PULSERING_BUFFER,GstPulseRingBufferClass))
93 #define GST_PULSERING_BUFFER_GET_CLASS(obj) \
94 (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_PULSERING_BUFFER, GstPulseRingBufferClass))
95 #define GST_PULSERING_BUFFER_CAST(obj) \
96 ((GstPulseRingBuffer *)obj)
97 #define GST_IS_PULSERING_BUFFER(obj) \
98 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_PULSERING_BUFFER))
99 #define GST_IS_PULSERING_BUFFER_CLASS(klass)\
100 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_PULSERING_BUFFER))
102 typedef struct _GstPulseRingBuffer GstPulseRingBuffer;
103 typedef struct _GstPulseRingBufferClass GstPulseRingBufferClass;
105 typedef struct _GstPulseContext GstPulseContext;
107 /* Store the PA contexts in a hash table to allow easy sharing among
108 * multiple instances of the sink. Keys are $context_name@$server_name
109 * (strings) and values should be GstPulseContext pointers.
111 struct _GstPulseContext
114 GSList *ring_buffers;
117 static GHashTable *gst_pulse_shared_contexts = NULL;
119 /* use one static main-loop for all instances
120 * this is needed to make the context sharing work as the contexts are
121 * released when releasing their parent main-loop
123 static pa_threaded_mainloop *mainloop = NULL;
124 static guint mainloop_ref_ct = 0;
126 /* lock for access to shared resources */
127 static GMutex *pa_shared_resource_mutex = NULL;
129 /* We keep a custom ringbuffer that is backed up by data allocated by
130 * pulseaudio. We must also overide the commit function to write into
131 * pulseaudio memory instead. */
132 struct _GstPulseRingBuffer
134 GstRingBuffer object;
142 #ifdef HAVE_PULSE_1_0
143 pa_format_info *format;
146 pa_sample_spec sample_spec;
156 gboolean in_commit:1;
159 struct _GstPulseRingBufferClass
161 GstRingBufferClass parent_class;
164 static GType gst_pulseringbuffer_get_type (void);
165 static void gst_pulseringbuffer_finalize (GObject * object);
167 static GstRingBufferClass *ring_parent_class = NULL;
169 static gboolean gst_pulseringbuffer_open_device (GstRingBuffer * buf);
170 static gboolean gst_pulseringbuffer_close_device (GstRingBuffer * buf);
171 static gboolean gst_pulseringbuffer_acquire (GstRingBuffer * buf,
172 GstRingBufferSpec * spec);
173 static gboolean gst_pulseringbuffer_release (GstRingBuffer * buf);
174 static gboolean gst_pulseringbuffer_start (GstRingBuffer * buf);
175 static gboolean gst_pulseringbuffer_pause (GstRingBuffer * buf);
176 static gboolean gst_pulseringbuffer_stop (GstRingBuffer * buf);
177 static void gst_pulseringbuffer_clear (GstRingBuffer * buf);
178 static guint gst_pulseringbuffer_commit (GstRingBuffer * buf,
179 guint64 * sample, guchar * data, gint in_samples, gint out_samples,
182 G_DEFINE_TYPE (GstPulseRingBuffer, gst_pulseringbuffer, GST_TYPE_RING_BUFFER);
185 gst_pulsesink_init_contexts (void)
187 g_assert (pa_shared_resource_mutex == NULL);
188 pa_shared_resource_mutex = g_mutex_new ();
189 gst_pulse_shared_contexts = g_hash_table_new_full (g_str_hash, g_str_equal,
194 gst_pulseringbuffer_class_init (GstPulseRingBufferClass * klass)
196 GObjectClass *gobject_class;
197 GstRingBufferClass *gstringbuffer_class;
199 gobject_class = (GObjectClass *) klass;
200 gstringbuffer_class = (GstRingBufferClass *) klass;
202 ring_parent_class = g_type_class_peek_parent (klass);
204 gobject_class->finalize = gst_pulseringbuffer_finalize;
206 gstringbuffer_class->open_device =
207 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_open_device);
208 gstringbuffer_class->close_device =
209 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_close_device);
210 gstringbuffer_class->acquire =
211 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_acquire);
212 gstringbuffer_class->release =
213 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_release);
214 gstringbuffer_class->start = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_start);
215 gstringbuffer_class->pause = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_pause);
216 gstringbuffer_class->resume = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_start);
217 gstringbuffer_class->stop = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_stop);
218 gstringbuffer_class->clear_all =
219 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_clear);
221 gstringbuffer_class->commit = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_commit);
225 gst_pulseringbuffer_init (GstPulseRingBuffer * pbuf)
227 pbuf->stream_name = NULL;
228 pbuf->context = NULL;
231 #ifdef HAVE_PULSE_1_0
235 pa_sample_spec_init (&pbuf->sample_spec);
240 pbuf->m_writable = 0;
242 pbuf->m_lastoffset = 0;
245 pbuf->in_commit = FALSE;
246 pbuf->paused = FALSE;
250 gst_pulsering_destroy_stream (GstPulseRingBuffer * pbuf)
255 /* drop shm memory buffer */
256 pa_stream_cancel_write (pbuf->stream);
258 /* reset internal variables */
261 pbuf->m_writable = 0;
263 pbuf->m_lastoffset = 0;
265 #ifdef HAVE_PULSE_1_0
267 pa_format_info_free (pbuf->format);
273 pa_stream_disconnect (pbuf->stream);
275 /* Make sure we don't get any further callbacks */
276 pa_stream_set_state_callback (pbuf->stream, NULL, NULL);
277 pa_stream_set_write_callback (pbuf->stream, NULL, NULL);
278 pa_stream_set_underflow_callback (pbuf->stream, NULL, NULL);
279 pa_stream_set_overflow_callback (pbuf->stream, NULL, NULL);
281 pa_stream_unref (pbuf->stream);
285 g_free (pbuf->stream_name);
286 pbuf->stream_name = NULL;
290 gst_pulsering_destroy_context (GstPulseRingBuffer * pbuf)
292 g_mutex_lock (pa_shared_resource_mutex);
294 GST_DEBUG_OBJECT (pbuf, "destroying ringbuffer %p", pbuf);
296 gst_pulsering_destroy_stream (pbuf);
299 pa_context_unref (pbuf->context);
300 pbuf->context = NULL;
303 if (pbuf->context_name) {
304 GstPulseContext *pctx;
306 pctx = g_hash_table_lookup (gst_pulse_shared_contexts, pbuf->context_name);
308 GST_DEBUG_OBJECT (pbuf, "releasing context with name %s, pbuf=%p, pctx=%p",
309 pbuf->context_name, pbuf, pctx);
312 pctx->ring_buffers = g_slist_remove (pctx->ring_buffers, pbuf);
313 if (pctx->ring_buffers == NULL) {
314 GST_DEBUG_OBJECT (pbuf,
315 "destroying final context with name %s, pbuf=%p, pctx=%p",
316 pbuf->context_name, pbuf, pctx);
318 pa_context_disconnect (pctx->context);
320 /* Make sure we don't get any further callbacks */
321 pa_context_set_state_callback (pctx->context, NULL, NULL);
322 pa_context_set_subscribe_callback (pctx->context, NULL, NULL);
324 g_hash_table_remove (gst_pulse_shared_contexts, pbuf->context_name);
326 pa_context_unref (pctx->context);
327 g_slice_free (GstPulseContext, pctx);
330 g_free (pbuf->context_name);
331 pbuf->context_name = NULL;
333 g_mutex_unlock (pa_shared_resource_mutex);
337 gst_pulseringbuffer_finalize (GObject * object)
339 GstPulseRingBuffer *ringbuffer;
341 ringbuffer = GST_PULSERING_BUFFER_CAST (object);
343 gst_pulsering_destroy_context (ringbuffer);
344 G_OBJECT_CLASS (ring_parent_class)->finalize (object);
348 #define CONTEXT_OK(c) ((c) && PA_CONTEXT_IS_GOOD (pa_context_get_state ((c))))
349 #define STREAM_OK(s) ((s) && PA_STREAM_IS_GOOD (pa_stream_get_state ((s))))
352 gst_pulsering_is_dead (GstPulseSink * psink, GstPulseRingBuffer * pbuf,
353 gboolean check_stream)
355 if (!CONTEXT_OK (pbuf->context))
358 if (check_stream && !STREAM_OK (pbuf->stream))
365 const gchar *err_str =
366 pbuf->context ? pa_strerror (pa_context_errno (pbuf->context)) : NULL;
367 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED, ("Disconnected: %s",
374 gst_pulsering_context_state_cb (pa_context * c, void *userdata)
376 pa_context_state_t state;
377 pa_threaded_mainloop *mainloop = (pa_threaded_mainloop *) userdata;
379 state = pa_context_get_state (c);
381 GST_LOG ("got new context state %d", state);
384 case PA_CONTEXT_READY:
385 case PA_CONTEXT_TERMINATED:
386 case PA_CONTEXT_FAILED:
387 GST_LOG ("signaling");
388 pa_threaded_mainloop_signal (mainloop, 0);
391 case PA_CONTEXT_UNCONNECTED:
392 case PA_CONTEXT_CONNECTING:
393 case PA_CONTEXT_AUTHORIZING:
394 case PA_CONTEXT_SETTING_NAME:
400 gst_pulsering_context_subscribe_cb (pa_context * c,
401 pa_subscription_event_type_t t, uint32_t idx, void *userdata)
404 GstPulseContext *pctx = (GstPulseContext *) userdata;
407 if (t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT | PA_SUBSCRIPTION_EVENT_CHANGE) &&
408 t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT | PA_SUBSCRIPTION_EVENT_NEW))
411 for (walk = pctx->ring_buffers; walk; walk = g_slist_next (walk)) {
412 GstPulseRingBuffer *pbuf = (GstPulseRingBuffer *) walk->data;
413 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
415 GST_LOG_OBJECT (psink, "type %d, idx %u", t, idx);
420 if (idx != pa_stream_get_index (pbuf->stream))
423 /* Actually this event is also triggered when other properties of
424 * the stream change that are unrelated to the volume. However it is
425 * probably cheaper to signal the change here and check for the
426 * volume when the GObject property is read instead of querying it always. */
428 /* inform streaming thread to notify */
429 g_atomic_int_compare_and_exchange (&psink->notify, 0, 1);
433 /* will be called when the device should be opened. In this case we will connect
434 * to the server. We should not try to open any streams in this state. */
436 gst_pulseringbuffer_open_device (GstRingBuffer * buf)
439 GstPulseRingBuffer *pbuf;
440 GstPulseContext *pctx;
441 pa_mainloop_api *api;
442 gboolean need_unlock_shared;
444 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
445 pbuf = GST_PULSERING_BUFFER_CAST (buf);
447 g_assert (!pbuf->stream);
448 g_assert (psink->client_name);
451 pbuf->context_name = g_strdup_printf ("%s@%s", psink->client_name,
454 pbuf->context_name = g_strdup (psink->client_name);
456 pa_threaded_mainloop_lock (mainloop);
458 g_mutex_lock (pa_shared_resource_mutex);
459 need_unlock_shared = TRUE;
461 pctx = g_hash_table_lookup (gst_pulse_shared_contexts, pbuf->context_name);
463 pctx = g_slice_new0 (GstPulseContext);
465 /* get the mainloop api and create a context */
466 GST_INFO_OBJECT (psink, "new context with name %s, pbuf=%p, pctx=%p",
467 pbuf->context_name, pbuf, pctx);
468 api = pa_threaded_mainloop_get_api (mainloop);
469 if (!(pctx->context = pa_context_new (api, pbuf->context_name)))
472 pctx->ring_buffers = g_slist_prepend (pctx->ring_buffers, pbuf);
473 g_hash_table_insert (gst_pulse_shared_contexts,
474 g_strdup (pbuf->context_name), (gpointer) pctx);
475 /* register some essential callbacks */
476 pa_context_set_state_callback (pctx->context,
477 gst_pulsering_context_state_cb, mainloop);
478 pa_context_set_subscribe_callback (pctx->context,
479 gst_pulsering_context_subscribe_cb, pctx);
481 /* try to connect to the server and wait for completion, we don't want to
482 * autospawn a deamon */
483 GST_LOG_OBJECT (psink, "connect to server %s",
484 GST_STR_NULL (psink->server));
485 if (pa_context_connect (pctx->context, psink->server,
486 PA_CONTEXT_NOAUTOSPAWN, NULL) < 0)
489 GST_INFO_OBJECT (psink,
490 "reusing shared context with name %s, pbuf=%p, pctx=%p",
491 pbuf->context_name, pbuf, pctx);
492 pctx->ring_buffers = g_slist_prepend (pctx->ring_buffers, pbuf);
495 g_mutex_unlock (pa_shared_resource_mutex);
496 need_unlock_shared = FALSE;
498 /* context created or shared okay */
499 pbuf->context = pa_context_ref (pctx->context);
502 pa_context_state_t state;
504 state = pa_context_get_state (pbuf->context);
506 GST_LOG_OBJECT (psink, "context state is now %d", state);
508 if (!PA_CONTEXT_IS_GOOD (state))
511 if (state == PA_CONTEXT_READY)
514 /* Wait until the context is ready */
515 GST_LOG_OBJECT (psink, "waiting..");
516 pa_threaded_mainloop_wait (mainloop);
519 GST_LOG_OBJECT (psink, "opened the device");
521 pa_threaded_mainloop_unlock (mainloop);
528 if (need_unlock_shared)
529 g_mutex_unlock (pa_shared_resource_mutex);
530 gst_pulsering_destroy_context (pbuf);
531 pa_threaded_mainloop_unlock (mainloop);
536 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
537 ("Failed to create context"), (NULL));
538 g_slice_free (GstPulseContext, pctx);
539 goto unlock_and_fail;
543 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED, ("Failed to connect: %s",
544 pa_strerror (pa_context_errno (pctx->context))), (NULL));
545 goto unlock_and_fail;
549 /* close the device */
551 gst_pulseringbuffer_close_device (GstRingBuffer * buf)
554 GstPulseRingBuffer *pbuf;
556 pbuf = GST_PULSERING_BUFFER_CAST (buf);
557 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
559 GST_LOG_OBJECT (psink, "closing device");
561 pa_threaded_mainloop_lock (mainloop);
562 gst_pulsering_destroy_context (pbuf);
563 pa_threaded_mainloop_unlock (mainloop);
565 GST_LOG_OBJECT (psink, "closed device");
571 gst_pulsering_stream_state_cb (pa_stream * s, void *userdata)
574 GstPulseRingBuffer *pbuf;
575 pa_stream_state_t state;
577 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
578 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
580 state = pa_stream_get_state (s);
581 GST_LOG_OBJECT (psink, "got new stream state %d", state);
584 case PA_STREAM_READY:
585 case PA_STREAM_FAILED:
586 case PA_STREAM_TERMINATED:
587 GST_LOG_OBJECT (psink, "signaling");
588 pa_threaded_mainloop_signal (mainloop, 0);
590 case PA_STREAM_UNCONNECTED:
591 case PA_STREAM_CREATING:
597 gst_pulsering_stream_request_cb (pa_stream * s, size_t length, void *userdata)
601 GstPulseRingBuffer *pbuf;
603 rbuf = GST_RING_BUFFER_CAST (userdata);
604 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
605 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
607 GST_LOG_OBJECT (psink, "got request for length %" G_GSIZE_FORMAT, length);
609 if (pbuf->in_commit && (length >= rbuf->spec.segsize)) {
610 /* only signal when we are waiting in the commit thread
611 * and got request for atleast a segment */
612 pa_threaded_mainloop_signal (mainloop, 0);
617 gst_pulsering_stream_underflow_cb (pa_stream * s, void *userdata)
620 GstPulseRingBuffer *pbuf;
622 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
623 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
625 GST_WARNING_OBJECT (psink, "Got underflow");
629 gst_pulsering_stream_overflow_cb (pa_stream * s, void *userdata)
632 GstPulseRingBuffer *pbuf;
634 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
635 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
637 GST_WARNING_OBJECT (psink, "Got overflow");
641 gst_pulsering_stream_latency_cb (pa_stream * s, void *userdata)
644 GstPulseRingBuffer *pbuf;
645 const pa_timing_info *info;
648 info = pa_stream_get_timing_info (s);
650 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
651 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
654 GST_LOG_OBJECT (psink, "latency update (information unknown)");
657 sink_usec = info->configured_sink_usec;
659 GST_LOG_OBJECT (psink,
660 "latency_update, %" G_GUINT64_FORMAT ", %d:%" G_GINT64_FORMAT ", %d:%"
661 G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT,
662 GST_TIMEVAL_TO_TIME (info->timestamp), info->write_index_corrupt,
663 info->write_index, info->read_index_corrupt, info->read_index,
664 info->sink_usec, sink_usec);
668 gst_pulsering_stream_suspended_cb (pa_stream * p, void *userdata)
671 GstPulseRingBuffer *pbuf;
673 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
674 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
676 if (pa_stream_is_suspended (p))
677 GST_DEBUG_OBJECT (psink, "stream suspended");
679 GST_DEBUG_OBJECT (psink, "stream resumed");
683 gst_pulsering_stream_started_cb (pa_stream * p, void *userdata)
686 GstPulseRingBuffer *pbuf;
688 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
689 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
691 GST_DEBUG_OBJECT (psink, "stream started");
695 gst_pulsering_stream_event_cb (pa_stream * p, const char *name,
696 pa_proplist * pl, void *userdata)
699 GstPulseRingBuffer *pbuf;
701 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
702 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
704 if (!strcmp (name, PA_STREAM_EVENT_REQUEST_CORK)) {
705 /* the stream wants to PAUSE, post a message for the application. */
706 GST_DEBUG_OBJECT (psink, "got request for CORK");
707 gst_element_post_message (GST_ELEMENT_CAST (psink),
708 gst_message_new_request_state (GST_OBJECT_CAST (psink),
711 } else if (!strcmp (name, PA_STREAM_EVENT_REQUEST_UNCORK)) {
712 GST_DEBUG_OBJECT (psink, "got request for UNCORK");
713 gst_element_post_message (GST_ELEMENT_CAST (psink),
714 gst_message_new_request_state (GST_OBJECT_CAST (psink),
716 #ifdef HAVE_PULSE_1_0
717 } else if (!strcmp (name, PA_STREAM_EVENT_FORMAT_LOST)) {
720 if (g_atomic_int_get (&psink->format_lost)) {
721 /* Duplicate event before we're done reconfiguring, discard */
725 GST_DEBUG_OBJECT (psink, "got FORMAT LOST");
726 g_atomic_int_set (&psink->format_lost, 1);
727 psink->format_lost_time = g_ascii_strtoull (pa_proplist_gets (pl,
728 "stream-time"), NULL, 0) * 1000;
730 g_free (psink->device);
731 psink->device = g_strdup (pa_proplist_gets (pl, "device"));
733 renego = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
734 gst_structure_new ("pulse-format-lost", NULL));
736 if (!gst_pad_push_event (GST_BASE_SINK (psink)->sinkpad, renego)) {
737 /* Nobody handled the format change - emit an error */
738 GST_ELEMENT_ERROR (psink, STREAM, FORMAT, ("Sink format changed"),
739 ("Sink format changed"));
743 GST_DEBUG_OBJECT (psink, "got unknown event %s", name);
747 /* Called with the mainloop locked */
749 gst_pulsering_wait_for_stream_ready (GstPulseSink * psink, pa_stream * stream)
751 pa_stream_state_t state;
754 state = pa_stream_get_state (stream);
756 GST_LOG_OBJECT (psink, "stream state is now %d", state);
758 if (!PA_STREAM_IS_GOOD (state))
761 if (state == PA_STREAM_READY)
764 /* Wait until the stream is ready */
765 pa_threaded_mainloop_wait (mainloop);
770 /* This method should create a new stream of the given @spec. No playback should
771 * start yet so we start in the corked state. */
773 gst_pulseringbuffer_acquire (GstRingBuffer * buf, GstRingBufferSpec * spec)
776 GstPulseRingBuffer *pbuf;
777 pa_buffer_attr wanted;
778 const pa_buffer_attr *actual;
779 pa_channel_map channel_map;
780 pa_operation *o = NULL;
781 #ifdef HAVE_PULSE_0_9_20
784 pa_cvolume *pv = NULL;
785 pa_stream_flags_t flags;
787 GstAudioClock *clock;
788 #ifdef HAVE_PULSE_1_0
789 pa_format_info *formats[1];
790 #ifndef GST_DISABLE_GST_DEBUG
791 gchar print_buf[PA_FORMAT_INFO_SNPRINT_MAX];
795 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
796 pbuf = GST_PULSERING_BUFFER_CAST (buf);
798 GST_LOG_OBJECT (psink, "creating sample spec");
799 /* convert the gstreamer sample spec to the pulseaudio format */
800 #ifdef HAVE_PULSE_1_0
801 if (!gst_pulse_fill_format_info (spec, &pbuf->format, &pbuf->channels))
804 if (!gst_pulse_fill_sample_spec (spec, &pbuf->sample_spec))
808 pa_threaded_mainloop_lock (mainloop);
810 /* we need a context and a no stream */
811 g_assert (pbuf->context);
812 g_assert (!pbuf->stream);
814 /* enable event notifications */
815 GST_LOG_OBJECT (psink, "subscribing to context events");
816 if (!(o = pa_context_subscribe (pbuf->context,
817 PA_SUBSCRIPTION_MASK_SINK_INPUT, NULL, NULL)))
818 goto subscribe_failed;
820 pa_operation_unref (o);
822 /* initialize the channel map */
823 #ifdef HAVE_PULSE_1_0
824 if (pa_format_info_is_pcm (pbuf->format) &&
825 gst_pulse_gst_to_channel_map (&channel_map, spec))
826 pa_format_info_set_channel_map (pbuf->format, &channel_map);
828 gst_pulse_gst_to_channel_map (&channel_map, spec);
831 /* find a good name for the stream */
832 if (psink->stream_name)
833 name = psink->stream_name;
835 name = "Playback Stream";
837 /* create a stream */
838 #ifdef HAVE_PULSE_1_0
839 formats[0] = pbuf->format;
840 if (!(pbuf->stream = pa_stream_new_extended (pbuf->context, name, formats, 1,
844 GST_LOG_OBJECT (psink, "creating stream with name %s", name);
845 if (!(pbuf->stream = pa_stream_new_with_proplist (pbuf->context, name,
846 &pbuf->sample_spec, &channel_map, psink->proplist)))
850 /* install essential callbacks */
851 pa_stream_set_state_callback (pbuf->stream,
852 gst_pulsering_stream_state_cb, pbuf);
853 pa_stream_set_write_callback (pbuf->stream,
854 gst_pulsering_stream_request_cb, pbuf);
855 pa_stream_set_underflow_callback (pbuf->stream,
856 gst_pulsering_stream_underflow_cb, pbuf);
857 pa_stream_set_overflow_callback (pbuf->stream,
858 gst_pulsering_stream_overflow_cb, pbuf);
859 pa_stream_set_latency_update_callback (pbuf->stream,
860 gst_pulsering_stream_latency_cb, pbuf);
861 pa_stream_set_suspended_callback (pbuf->stream,
862 gst_pulsering_stream_suspended_cb, pbuf);
863 pa_stream_set_started_callback (pbuf->stream,
864 gst_pulsering_stream_started_cb, pbuf);
865 pa_stream_set_event_callback (pbuf->stream,
866 gst_pulsering_stream_event_cb, pbuf);
868 /* buffering requirements. When setting prebuf to 0, the stream will not pause
869 * when we cause an underrun, which causes time to continue. */
870 memset (&wanted, 0, sizeof (wanted));
871 wanted.tlength = spec->segtotal * spec->segsize;
872 wanted.maxlength = -1;
874 wanted.minreq = spec->segsize;
876 GST_INFO_OBJECT (psink, "tlength: %d", wanted.tlength);
877 GST_INFO_OBJECT (psink, "maxlength: %d", wanted.maxlength);
878 GST_INFO_OBJECT (psink, "prebuf: %d", wanted.prebuf);
879 GST_INFO_OBJECT (psink, "minreq: %d", wanted.minreq);
881 #ifdef HAVE_PULSE_0_9_20
882 /* configure volume when we changed it, else we leave the default */
883 if (psink->volume_set) {
884 GST_LOG_OBJECT (psink, "have volume of %f", psink->volume);
886 #ifdef HAVE_PULSE_1_0
887 if (pa_format_info_is_pcm (pbuf->format))
888 gst_pulse_cvolume_from_linear (pv, pbuf->channels, psink->volume);
890 GST_DEBUG_OBJECT (psink, "passthrough stream, not setting volume");
894 gst_pulse_cvolume_from_linear (pv, pbuf->sample_spec.channels,
902 /* construct the flags */
903 flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE |
904 PA_STREAM_ADJUST_LATENCY | PA_STREAM_START_CORKED;
906 if (psink->mute_set && psink->mute)
907 flags |= PA_STREAM_START_MUTED;
909 /* we always start corked (see flags above) */
912 /* try to connect now */
913 GST_LOG_OBJECT (psink, "connect for playback to device %s",
914 GST_STR_NULL (psink->device));
915 if (pa_stream_connect_playback (pbuf->stream, psink->device,
916 &wanted, flags, pv, NULL) < 0)
919 /* our clock will now start from 0 again */
920 clock = GST_AUDIO_CLOCK (GST_BASE_AUDIO_SINK (psink)->provided_clock);
921 gst_audio_clock_reset (clock, 0);
923 if (!gst_pulsering_wait_for_stream_ready (psink, pbuf->stream))
926 #ifdef HAVE_PULSE_1_0
927 g_free (psink->device);
928 psink->device = g_strdup (pa_stream_get_device_name (pbuf->stream));
930 #ifndef GST_DISABLE_GST_DEBUG
931 pa_format_info_snprint (print_buf, sizeof (print_buf),
932 pa_stream_get_format_info (pbuf->stream));
933 GST_INFO_OBJECT (psink, "negotiated to: %s", print_buf);
937 /* After we passed the volume off of to PA we never want to set it
938 again, since it is PA's job to save/restore volumes. */
939 psink->volume_set = psink->mute_set = FALSE;
941 GST_LOG_OBJECT (psink, "stream is acquired now");
943 /* get the actual buffering properties now */
944 actual = pa_stream_get_buffer_attr (pbuf->stream);
946 GST_INFO_OBJECT (psink, "tlength: %d (wanted: %d)", actual->tlength,
948 GST_INFO_OBJECT (psink, "maxlength: %d", actual->maxlength);
949 GST_INFO_OBJECT (psink, "prebuf: %d", actual->prebuf);
950 GST_INFO_OBJECT (psink, "minreq: %d (wanted %d)", actual->minreq,
953 spec->segsize = actual->minreq;
954 spec->segtotal = actual->tlength / spec->segsize;
956 pa_threaded_mainloop_unlock (mainloop);
963 gst_pulsering_destroy_stream (pbuf);
964 pa_threaded_mainloop_unlock (mainloop);
970 GST_ELEMENT_ERROR (psink, RESOURCE, SETTINGS,
971 ("Invalid sample specification."), (NULL));
976 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
977 ("pa_context_subscribe() failed: %s",
978 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
979 goto unlock_and_fail;
983 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
984 ("Failed to create stream: %s",
985 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
986 goto unlock_and_fail;
990 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
991 ("Failed to connect stream: %s",
992 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
993 goto unlock_and_fail;
997 /* free the stream that we acquired before */
999 gst_pulseringbuffer_release (GstRingBuffer * buf)
1001 GstPulseRingBuffer *pbuf;
1003 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1005 pa_threaded_mainloop_lock (mainloop);
1006 gst_pulsering_destroy_stream (pbuf);
1007 pa_threaded_mainloop_unlock (mainloop);
1009 #ifdef HAVE_PULSE_1_0
1011 GstPulseSink *psink;
1013 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1014 g_atomic_int_set (&psink->format_lost, FALSE);
1015 psink->format_lost_time = GST_CLOCK_TIME_NONE;
1023 gst_pulsering_success_cb (pa_stream * s, int success, void *userdata)
1025 pa_threaded_mainloop_signal (mainloop, 0);
1028 /* update the corked state of a stream, must be called with the mainloop
1031 gst_pulsering_set_corked (GstPulseRingBuffer * pbuf, gboolean corked,
1034 pa_operation *o = NULL;
1035 GstPulseSink *psink;
1036 gboolean res = FALSE;
1038 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1040 #ifdef HAVE_PULSE_1_0
1041 if (g_atomic_int_get (&psink->format_lost)) {
1042 /* Sink format changed, stream's gone so fake being paused */
1047 GST_DEBUG_OBJECT (psink, "setting corked state to %d", corked);
1048 if (pbuf->corked != corked) {
1049 if (!(o = pa_stream_cork (pbuf->stream, corked,
1050 gst_pulsering_success_cb, pbuf)))
1053 while (wait && pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1054 pa_threaded_mainloop_wait (mainloop);
1055 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
1058 pbuf->corked = corked;
1060 GST_DEBUG_OBJECT (psink, "skipping, already in requested state");
1066 pa_operation_unref (o);
1073 GST_DEBUG_OBJECT (psink, "the server is dead");
1078 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1079 ("pa_stream_cork() failed: %s",
1080 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1086 gst_pulseringbuffer_clear (GstRingBuffer * buf)
1088 GstPulseSink *psink;
1089 GstPulseRingBuffer *pbuf;
1090 pa_operation *o = NULL;
1092 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1093 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1095 pa_threaded_mainloop_lock (mainloop);
1096 GST_DEBUG_OBJECT (psink, "clearing");
1098 /* don't wait for the flush to complete */
1099 if ((o = pa_stream_flush (pbuf->stream, NULL, pbuf)))
1100 pa_operation_unref (o);
1102 pa_threaded_mainloop_unlock (mainloop);
1105 /* called from pulse with the mainloop lock */
1107 mainloop_enter_defer_cb (pa_mainloop_api * api, void *userdata)
1109 GstPulseSink *pulsesink = GST_PULSESINK (userdata);
1110 GstMessage *message;
1113 g_value_init (&val, G_TYPE_POINTER);
1114 g_value_set_pointer (&val, g_thread_self ());
1116 GST_DEBUG_OBJECT (pulsesink, "posting ENTER stream status");
1117 message = gst_message_new_stream_status (GST_OBJECT (pulsesink),
1118 GST_STREAM_STATUS_TYPE_ENTER, GST_ELEMENT (pulsesink));
1119 gst_message_set_stream_status_object (message, &val);
1121 gst_element_post_message (GST_ELEMENT (pulsesink), message);
1123 g_return_if_fail (pulsesink->defer_pending);
1124 pulsesink->defer_pending--;
1125 pa_threaded_mainloop_signal (mainloop, 0);
1128 /* start/resume playback ASAP, we don't uncork here but in the commit method */
1130 gst_pulseringbuffer_start (GstRingBuffer * buf)
1132 GstPulseSink *psink;
1133 GstPulseRingBuffer *pbuf;
1135 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1136 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1138 pa_threaded_mainloop_lock (mainloop);
1140 GST_DEBUG_OBJECT (psink, "scheduling stream status");
1141 psink->defer_pending++;
1142 pa_mainloop_api_once (pa_threaded_mainloop_get_api (mainloop),
1143 mainloop_enter_defer_cb, psink);
1145 GST_DEBUG_OBJECT (psink, "starting");
1146 pbuf->paused = FALSE;
1148 /* EOS needs running clock */
1149 if (GST_BASE_SINK_CAST (psink)->eos ||
1150 g_atomic_int_get (&GST_BASE_AUDIO_SINK (psink)->abidata.ABI.
1152 gst_pulsering_set_corked (pbuf, FALSE, FALSE);
1154 pa_threaded_mainloop_unlock (mainloop);
1159 /* pause/stop playback ASAP */
1161 gst_pulseringbuffer_pause (GstRingBuffer * buf)
1163 GstPulseSink *psink;
1164 GstPulseRingBuffer *pbuf;
1167 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1168 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1170 pa_threaded_mainloop_lock (mainloop);
1171 GST_DEBUG_OBJECT (psink, "pausing and corking");
1172 /* make sure the commit method stops writing */
1173 pbuf->paused = TRUE;
1174 res = gst_pulsering_set_corked (pbuf, TRUE, TRUE);
1175 if (pbuf->in_commit) {
1176 /* we are waiting in a commit, signal */
1177 GST_DEBUG_OBJECT (psink, "signal commit");
1178 pa_threaded_mainloop_signal (mainloop, 0);
1180 pa_threaded_mainloop_unlock (mainloop);
1185 /* called from pulse with the mainloop lock */
1187 mainloop_leave_defer_cb (pa_mainloop_api * api, void *userdata)
1189 GstPulseSink *pulsesink = GST_PULSESINK (userdata);
1190 GstMessage *message;
1193 g_value_init (&val, G_TYPE_POINTER);
1194 g_value_set_pointer (&val, g_thread_self ());
1196 GST_DEBUG_OBJECT (pulsesink, "posting LEAVE stream status");
1197 message = gst_message_new_stream_status (GST_OBJECT (pulsesink),
1198 GST_STREAM_STATUS_TYPE_LEAVE, GST_ELEMENT (pulsesink));
1199 gst_message_set_stream_status_object (message, &val);
1200 gst_element_post_message (GST_ELEMENT (pulsesink), message);
1202 g_return_if_fail (pulsesink->defer_pending);
1203 pulsesink->defer_pending--;
1204 pa_threaded_mainloop_signal (mainloop, 0);
1207 /* stop playback, we flush everything. */
1209 gst_pulseringbuffer_stop (GstRingBuffer * buf)
1211 GstPulseSink *psink;
1212 GstPulseRingBuffer *pbuf;
1213 gboolean res = FALSE;
1214 pa_operation *o = NULL;
1216 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1217 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1219 pa_threaded_mainloop_lock (mainloop);
1221 pbuf->paused = TRUE;
1222 res = gst_pulsering_set_corked (pbuf, TRUE, TRUE);
1224 /* Inform anyone waiting in _commit() call that it shall wakeup */
1225 if (pbuf->in_commit) {
1226 GST_DEBUG_OBJECT (psink, "signal commit thread");
1227 pa_threaded_mainloop_signal (mainloop, 0);
1229 #ifdef HAVE_PULSE_1_0
1230 if (g_atomic_int_get (&psink->format_lost)) {
1231 /* Don't try to flush, the stream's probably gone by now */
1237 /* then try to flush, it's not fatal when this fails */
1238 GST_DEBUG_OBJECT (psink, "flushing");
1239 if ((o = pa_stream_flush (pbuf->stream, gst_pulsering_success_cb, pbuf))) {
1240 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1241 GST_DEBUG_OBJECT (psink, "wait for completion");
1242 pa_threaded_mainloop_wait (mainloop);
1243 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
1246 GST_DEBUG_OBJECT (psink, "flush completed");
1252 pa_operation_cancel (o);
1253 pa_operation_unref (o);
1256 GST_DEBUG_OBJECT (psink, "scheduling stream status");
1257 psink->defer_pending++;
1258 pa_mainloop_api_once (pa_threaded_mainloop_get_api (mainloop),
1259 mainloop_leave_defer_cb, psink);
1261 pa_threaded_mainloop_unlock (mainloop);
1268 GST_DEBUG_OBJECT (psink, "the server is dead");
1273 /* in_samples >= out_samples, rate > 1.0 */
1274 #define FWD_UP_SAMPLES(s,se,d,de) \
1276 guint8 *sb = s, *db = d; \
1277 while (s <= se && d < de) { \
1278 memcpy (d, s, bps); \
1281 if ((*accum << 1) >= inr) { \
1286 in_samples -= (s - sb)/bps; \
1287 out_samples -= (d - db)/bps; \
1288 GST_DEBUG ("fwd_up end %d/%d",*accum,*toprocess); \
1291 /* out_samples > in_samples, for rates smaller than 1.0 */
1292 #define FWD_DOWN_SAMPLES(s,se,d,de) \
1294 guint8 *sb = s, *db = d; \
1295 while (s <= se && d < de) { \
1296 memcpy (d, s, bps); \
1299 if ((*accum << 1) >= outr) { \
1304 in_samples -= (s - sb)/bps; \
1305 out_samples -= (d - db)/bps; \
1306 GST_DEBUG ("fwd_down end %d/%d",*accum,*toprocess); \
1309 #define REV_UP_SAMPLES(s,se,d,de) \
1311 guint8 *sb = se, *db = d; \
1312 while (s <= se && d < de) { \
1313 memcpy (d, se, bps); \
1316 while (d < de && (*accum << 1) >= inr) { \
1321 in_samples -= (sb - se)/bps; \
1322 out_samples -= (d - db)/bps; \
1323 GST_DEBUG ("rev_up end %d/%d",*accum,*toprocess); \
1326 #define REV_DOWN_SAMPLES(s,se,d,de) \
1328 guint8 *sb = se, *db = d; \
1329 while (s <= se && d < de) { \
1330 memcpy (d, se, bps); \
1333 while (s <= se && (*accum << 1) >= outr) { \
1338 in_samples -= (sb - se)/bps; \
1339 out_samples -= (d - db)/bps; \
1340 GST_DEBUG ("rev_down end %d/%d",*accum,*toprocess); \
1343 /* our custom commit function because we write into the buffer of pulseaudio
1344 * instead of keeping our own buffer */
1346 gst_pulseringbuffer_commit (GstRingBuffer * buf, guint64 * sample,
1347 guchar * data, gint in_samples, gint out_samples, gint * accum)
1349 GstPulseSink *psink;
1350 GstPulseRingBuffer *pbuf;
1355 gint inr, outr, bps;
1359 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1360 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1362 /* FIXME post message rather than using a signal (as mixer interface) */
1363 if (g_atomic_int_compare_and_exchange (&psink->notify, 1, 0)) {
1364 g_object_notify (G_OBJECT (psink), "volume");
1365 g_object_notify (G_OBJECT (psink), "mute");
1368 /* make sure the ringbuffer is started */
1369 if (G_UNLIKELY (g_atomic_int_get (&buf->state) !=
1370 GST_RING_BUFFER_STATE_STARTED)) {
1371 /* see if we are allowed to start it */
1372 if (G_UNLIKELY (g_atomic_int_get (&buf->abidata.ABI.may_start) == FALSE))
1375 GST_DEBUG_OBJECT (buf, "start!");
1376 if (!gst_ring_buffer_start (buf))
1380 pa_threaded_mainloop_lock (mainloop);
1382 GST_DEBUG_OBJECT (psink, "entering commit");
1383 pbuf->in_commit = TRUE;
1385 bps = buf->spec.bytes_per_sample;
1386 bufsize = buf->spec.segsize * buf->spec.segtotal;
1388 /* our toy resampler for trick modes */
1389 reverse = out_samples < 0;
1390 out_samples = ABS (out_samples);
1392 if (in_samples >= out_samples)
1393 toprocess = &in_samples;
1395 toprocess = &out_samples;
1397 inr = in_samples - 1;
1398 outr = out_samples - 1;
1400 GST_DEBUG_OBJECT (psink, "in %d, out %d", inr, outr);
1402 /* data_end points to the last sample we have to write, not past it. This is
1403 * needed to properly handle reverse playback: it points to the last sample. */
1404 data_end = data + (bps * inr);
1406 #ifdef HAVE_PULSE_1_0
1407 if (g_atomic_int_get (&psink->format_lost)) {
1408 /* Sink format changed, drop the data and hope upstream renegotiates */
1416 /* offset is in bytes */
1417 offset = *sample * bps;
1419 while (*toprocess > 0) {
1423 GST_LOG_OBJECT (psink,
1424 "need to write %d samples at offset %" G_GINT64_FORMAT, *toprocess,
1427 if (offset != pbuf->m_lastoffset)
1428 GST_LOG_OBJECT (psink, "discontinuity, offset is %" G_GINT64_FORMAT ", "
1429 "last offset was %" G_GINT64_FORMAT, offset, pbuf->m_lastoffset);
1431 towrite = out_samples * bps;
1433 /* Only ever write segsize bytes at once. This will
1434 * also limit the PA shm buffer to segsize
1436 if (towrite > buf->spec.segsize)
1437 towrite = buf->spec.segsize;
1439 if ((pbuf->m_writable < towrite) || (offset != pbuf->m_lastoffset)) {
1440 /* if no room left or discontinuity in offset,
1441 we need to flush data and get a new buffer */
1443 /* flush the buffer if possible */
1444 if ((pbuf->m_data != NULL) && (pbuf->m_towrite > 0)) {
1446 GST_LOG_OBJECT (psink,
1447 "flushing %u samples at offset %" G_GINT64_FORMAT,
1448 (guint) pbuf->m_towrite / bps, pbuf->m_offset);
1450 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1451 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1455 pbuf->m_towrite = 0;
1456 pbuf->m_offset = offset; /* keep track of current offset */
1458 /* get a buffer to write in for now on */
1460 pbuf->m_writable = pa_stream_writable_size (pbuf->stream);
1462 #ifdef HAVE_PULSE_1_0
1463 if (g_atomic_int_get (&psink->format_lost)) {
1464 /* Sink format changed, give up and hope upstream renegotiates */
1469 if (pbuf->m_writable == (size_t) - 1)
1470 goto writable_size_failed;
1472 pbuf->m_writable /= bps;
1473 pbuf->m_writable *= bps; /* handle only complete samples */
1475 if (pbuf->m_writable >= towrite)
1478 /* see if we need to uncork because we have no free space */
1480 if (!gst_pulsering_set_corked (pbuf, FALSE, FALSE))
1484 /* we can't write a single byte, wait a bit */
1485 GST_LOG_OBJECT (psink, "waiting for free space");
1486 pa_threaded_mainloop_wait (mainloop);
1492 /* make sure we only buffer up latency-time samples */
1493 if (pbuf->m_writable > buf->spec.segsize) {
1494 /* limit buffering to latency-time value */
1495 pbuf->m_writable = buf->spec.segsize;
1497 GST_LOG_OBJECT (psink, "Limiting buffering to %" G_GSIZE_FORMAT,
1501 GST_LOG_OBJECT (psink, "requesting %" G_GSIZE_FORMAT " bytes of "
1502 "shared memory", pbuf->m_writable);
1504 if (pa_stream_begin_write (pbuf->stream, &pbuf->m_data,
1505 &pbuf->m_writable) < 0) {
1506 GST_LOG_OBJECT (psink, "pa_stream_begin_write() failed");
1507 goto writable_size_failed;
1510 GST_LOG_OBJECT (psink, "got %" G_GSIZE_FORMAT " bytes of shared memory",
1513 /* Just to make sure that we didn't get more than requested */
1514 if (pbuf->m_writable > buf->spec.segsize) {
1515 /* limit buffering to latency-time value */
1516 pbuf->m_writable = buf->spec.segsize;
1520 if (pbuf->m_writable < towrite)
1521 towrite = pbuf->m_writable;
1522 avail = towrite / bps;
1524 GST_LOG_OBJECT (psink, "writing %u samples at offset %" G_GUINT64_FORMAT,
1525 (guint) avail, offset);
1527 #ifdef HAVE_PULSE_1_0
1528 /* No trick modes for passthrough streams */
1529 if (G_UNLIKELY (inr != outr || reverse)) {
1530 GST_WARNING_OBJECT (psink, "Passthrough stream can't run in trick mode");
1531 goto unlock_and_fail;
1535 if (G_LIKELY (inr == outr && !reverse)) {
1536 /* no rate conversion, simply write out the samples */
1537 /* copy the data into internal buffer */
1539 memcpy ((guint8 *) pbuf->m_data + pbuf->m_towrite, data, towrite);
1540 pbuf->m_towrite += towrite;
1541 pbuf->m_writable -= towrite;
1544 in_samples -= avail;
1545 out_samples -= avail;
1547 guint8 *dest, *d, *d_end;
1549 /* write into the PulseAudio shm buffer */
1550 dest = d = (guint8 *) pbuf->m_data + pbuf->m_towrite;
1551 d_end = d + towrite;
1555 /* forward speed up */
1556 FWD_UP_SAMPLES (data, data_end, d, d_end);
1558 /* forward slow down */
1559 FWD_DOWN_SAMPLES (data, data_end, d, d_end);
1562 /* reverse speed up */
1563 REV_UP_SAMPLES (data, data_end, d, d_end);
1565 /* reverse slow down */
1566 REV_DOWN_SAMPLES (data, data_end, d, d_end);
1568 /* see what we have left to write */
1569 towrite = (d - dest);
1570 pbuf->m_towrite += towrite;
1571 pbuf->m_writable -= towrite;
1573 avail = towrite / bps;
1576 /* flush the buffer if it's full */
1577 if ((pbuf->m_data != NULL) && (pbuf->m_towrite > 0)
1578 && (pbuf->m_writable == 0)) {
1579 GST_LOG_OBJECT (psink, "flushing %u samples at offset %" G_GINT64_FORMAT,
1580 (guint) pbuf->m_towrite / bps, pbuf->m_offset);
1582 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1583 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1586 pbuf->m_towrite = 0;
1587 pbuf->m_offset = offset + towrite; /* keep track of current offset */
1591 offset += avail * bps;
1592 pbuf->m_lastoffset = offset;
1594 /* check if we need to uncork after writing the samples */
1596 const pa_timing_info *info;
1598 if ((info = pa_stream_get_timing_info (pbuf->stream))) {
1599 GST_LOG_OBJECT (psink,
1600 "read_index at %" G_GUINT64_FORMAT ", offset %" G_GINT64_FORMAT,
1601 info->read_index, offset);
1603 /* we uncork when the read_index is too far behind the offset we need
1605 if (info->read_index + bufsize <= offset) {
1606 if (!gst_pulsering_set_corked (pbuf, FALSE, FALSE))
1610 GST_LOG_OBJECT (psink, "no timing info available yet");
1615 #ifdef HAVE_PULSE_1_0
1618 /* we consumed all samples here */
1619 data = data_end + bps;
1621 pbuf->in_commit = FALSE;
1622 pa_threaded_mainloop_unlock (mainloop);
1625 result = inr - ((data_end - data) / bps);
1626 GST_LOG_OBJECT (psink, "wrote %d samples", result);
1633 pbuf->in_commit = FALSE;
1634 GST_LOG_OBJECT (psink, "we are reset");
1635 pa_threaded_mainloop_unlock (mainloop);
1640 GST_LOG_OBJECT (psink, "we can not start");
1645 GST_LOG_OBJECT (psink, "failed to start the ringbuffer");
1650 pbuf->in_commit = FALSE;
1651 GST_ERROR_OBJECT (psink, "uncork failed");
1652 pa_threaded_mainloop_unlock (mainloop);
1657 pbuf->in_commit = FALSE;
1658 GST_LOG_OBJECT (psink, "we are paused");
1659 pa_threaded_mainloop_unlock (mainloop);
1662 writable_size_failed:
1664 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1665 ("pa_stream_writable_size() failed: %s",
1666 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1667 goto unlock_and_fail;
1671 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1672 ("pa_stream_write() failed: %s",
1673 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1674 goto unlock_and_fail;
1678 /* write pending local samples, must be called with the mainloop lock */
1680 gst_pulsering_flush (GstPulseRingBuffer * pbuf)
1682 GstPulseSink *psink;
1684 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1685 GST_DEBUG_OBJECT (psink, "entering flush");
1687 /* flush the buffer if possible */
1688 if (pbuf->stream && (pbuf->m_data != NULL) && (pbuf->m_towrite > 0)) {
1689 #ifndef GST_DISABLE_GST_DEBUG
1692 bps = (GST_RING_BUFFER_CAST (pbuf))->spec.bytes_per_sample;
1693 GST_LOG_OBJECT (psink,
1694 "flushing %u samples at offset %" G_GINT64_FORMAT,
1695 (guint) pbuf->m_towrite / bps, pbuf->m_offset);
1698 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1699 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1703 pbuf->m_towrite = 0;
1704 pbuf->m_offset += pbuf->m_towrite; /* keep track of current offset */
1713 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1714 ("pa_stream_write() failed: %s",
1715 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1720 static void gst_pulsesink_set_property (GObject * object, guint prop_id,
1721 const GValue * value, GParamSpec * pspec);
1722 static void gst_pulsesink_get_property (GObject * object, guint prop_id,
1723 GValue * value, GParamSpec * pspec);
1724 static void gst_pulsesink_finalize (GObject * object);
1726 static gboolean gst_pulsesink_event (GstBaseSink * sink, GstEvent * event);
1728 static GstStateChangeReturn gst_pulsesink_change_state (GstElement * element,
1729 GstStateChange transition);
1731 static void gst_pulsesink_init_interfaces (GType type);
1733 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
1734 # define ENDIANNESS "LITTLE_ENDIAN, BIG_ENDIAN"
1736 # define ENDIANNESS "BIG_ENDIAN, LITTLE_ENDIAN"
1739 GST_IMPLEMENT_PULSEPROBE_METHODS (GstPulseSink, gst_pulsesink);
1741 #define _do_init(type) \
1742 gst_pulsesink_init_contexts (); \
1743 gst_pulsesink_init_interfaces (type);
1745 GST_BOILERPLATE_FULL (GstPulseSink, gst_pulsesink, GstBaseAudioSink,
1746 GST_TYPE_BASE_AUDIO_SINK, _do_init);
1749 gst_pulsesink_interface_supported (GstImplementsInterface *
1750 iface, GType interface_type)
1752 GstPulseSink *this = GST_PULSESINK_CAST (iface);
1754 if (interface_type == GST_TYPE_PROPERTY_PROBE && this->probe)
1756 if (interface_type == GST_TYPE_STREAM_VOLUME)
1763 gst_pulsesink_implements_interface_init (GstImplementsInterfaceClass * klass)
1765 klass->supported = gst_pulsesink_interface_supported;
1769 gst_pulsesink_init_interfaces (GType type)
1771 static const GInterfaceInfo implements_iface_info = {
1772 (GInterfaceInitFunc) gst_pulsesink_implements_interface_init,
1776 static const GInterfaceInfo probe_iface_info = {
1777 (GInterfaceInitFunc) gst_pulsesink_property_probe_interface_init,
1781 static const GInterfaceInfo svol_iface_info = {
1785 g_type_add_interface_static (type, GST_TYPE_STREAM_VOLUME, &svol_iface_info);
1786 g_type_add_interface_static (type, GST_TYPE_IMPLEMENTS_INTERFACE,
1787 &implements_iface_info);
1788 g_type_add_interface_static (type, GST_TYPE_PROPERTY_PROBE,
1793 gst_pulsesink_base_init (gpointer g_class)
1795 static GstStaticPadTemplate pad_template = GST_STATIC_PAD_TEMPLATE ("sink",
1798 GST_STATIC_CAPS ("audio/x-raw-int, "
1799 "endianness = (int) { " ENDIANNESS " }, "
1800 "signed = (boolean) TRUE, "
1801 "width = (int) 16, "
1802 "depth = (int) 16, "
1803 "rate = (int) [ 1, MAX ], "
1804 "channels = (int) [ 1, 32 ];"
1805 "audio/x-raw-float, "
1806 "endianness = (int) { " ENDIANNESS " }, "
1807 "width = (int) 32, "
1808 "rate = (int) [ 1, MAX ], "
1809 "channels = (int) [ 1, 32 ];"
1811 "endianness = (int) { " ENDIANNESS " }, "
1812 "signed = (boolean) TRUE, "
1813 "width = (int) 32, "
1814 "depth = (int) 32, "
1815 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 32 ];"
1817 "endianness = (int) { " ENDIANNESS " }, "
1818 "signed = (boolean) TRUE, "
1819 "width = (int) 24, "
1820 "depth = (int) 24, "
1821 "rate = (int) [ 1, MAX ], "
1822 "channels = (int) [ 1, 32 ];"
1824 "endianness = (int) { " ENDIANNESS " }, "
1825 "signed = (boolean) TRUE, "
1826 "width = (int) 32, "
1827 "depth = (int) 24, "
1828 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 32 ];"
1830 "signed = (boolean) FALSE, "
1833 "rate = (int) [ 1, MAX ], "
1834 "channels = (int) [ 1, 32 ];"
1836 "rate = (int) [ 1, MAX], "
1837 "channels = (int) [ 1, 32 ];"
1839 "rate = (int) [ 1, MAX], " "channels = (int) [ 1, 32 ];"
1840 #ifdef HAVE_PULSE_1_0
1841 "audio/x-ac3, framed = (boolean) true;"
1842 "audio/x-eac3, framed = (boolean) true; "
1843 "audio/x-dts, framed = (boolean) true, "
1844 " block_size = (int) { 512, 1024, 2048 }; "
1845 "audio/mpeg, mpegversion = (int)1, "
1846 " mpegaudioversion = (int) [ 1, 2 ], parsed = (boolean) true; "
1850 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
1852 gst_element_class_set_details_simple (element_class,
1853 "PulseAudio Audio Sink",
1854 "Sink/Audio", "Plays audio to a PulseAudio server", "Lennart Poettering");
1855 gst_element_class_add_pad_template (element_class,
1856 gst_static_pad_template_get (&pad_template));
1859 static GstRingBuffer *
1860 gst_pulsesink_create_ringbuffer (GstBaseAudioSink * sink)
1862 GstRingBuffer *buffer;
1864 GST_DEBUG_OBJECT (sink, "creating ringbuffer");
1865 buffer = g_object_new (GST_TYPE_PULSERING_BUFFER, NULL);
1866 GST_DEBUG_OBJECT (sink, "created ringbuffer @%p", buffer);
1872 gst_pulsesink_payload (GstBaseAudioSink * sink, GstBuffer * buf)
1874 switch (sink->ringbuffer->spec.type) {
1875 case GST_BUFTYPE_AC3:
1876 case GST_BUFTYPE_EAC3:
1877 case GST_BUFTYPE_DTS:
1878 case GST_BUFTYPE_MPEG:
1880 /* FIXME: alloc memory from PA if possible */
1881 gint framesize = gst_audio_iec61937_frame_size (&sink->ringbuffer->spec);
1887 out = gst_buffer_new_and_alloc (framesize);
1889 if (!gst_audio_iec61937_payload (GST_BUFFER_DATA (buf),
1890 GST_BUFFER_SIZE (buf), GST_BUFFER_DATA (out),
1891 GST_BUFFER_SIZE (out), &sink->ringbuffer->spec)) {
1892 gst_buffer_unref (out);
1896 gst_buffer_copy_metadata (out, buf, GST_BUFFER_COPY_ALL);
1901 return gst_buffer_ref (buf);
1906 gst_pulsesink_class_init (GstPulseSinkClass * klass)
1908 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1909 GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
1910 GstBaseSinkClass *bc;
1911 GstBaseAudioSinkClass *gstaudiosink_class = GST_BASE_AUDIO_SINK_CLASS (klass);
1912 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1914 gobject_class->finalize = gst_pulsesink_finalize;
1915 gobject_class->set_property = gst_pulsesink_set_property;
1916 gobject_class->get_property = gst_pulsesink_get_property;
1918 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_pulsesink_event);
1920 /* restore the original basesink pull methods */
1921 bc = g_type_class_peek (GST_TYPE_BASE_SINK);
1922 gstbasesink_class->activate_pull = GST_DEBUG_FUNCPTR (bc->activate_pull);
1924 gstelement_class->change_state =
1925 GST_DEBUG_FUNCPTR (gst_pulsesink_change_state);
1927 gstaudiosink_class->create_ringbuffer =
1928 GST_DEBUG_FUNCPTR (gst_pulsesink_create_ringbuffer);
1929 gstaudiosink_class->payload = GST_DEBUG_FUNCPTR (gst_pulsesink_payload);
1931 /* Overwrite GObject fields */
1932 g_object_class_install_property (gobject_class,
1934 g_param_spec_string ("server", "Server",
1935 "The PulseAudio server to connect to", DEFAULT_SERVER,
1936 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1938 g_object_class_install_property (gobject_class, PROP_DEVICE,
1939 g_param_spec_string ("device", "Device",
1940 "The PulseAudio sink device to connect to", DEFAULT_DEVICE,
1941 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1943 g_object_class_install_property (gobject_class,
1945 g_param_spec_string ("device-name", "Device name",
1946 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
1947 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1949 g_object_class_install_property (gobject_class,
1951 g_param_spec_double ("volume", "Volume",
1952 "Linear volume of this stream, 1.0=100%", 0.0, MAX_VOLUME,
1953 DEFAULT_VOLUME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1954 g_object_class_install_property (gobject_class,
1956 g_param_spec_boolean ("mute", "Mute",
1957 "Mute state of this stream", DEFAULT_MUTE,
1958 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1961 * GstPulseSink:client
1963 * The PulseAudio client name to use.
1967 g_object_class_install_property (gobject_class,
1969 g_param_spec_string ("client", "Client",
1970 "The PulseAudio client name to use", gst_pulse_client_name (),
1971 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1972 GST_PARAM_MUTABLE_READY));
1975 * GstPulseSink:stream-properties
1977 * List of pulseaudio stream properties. A list of defined properties can be
1978 * found in the <ulink url="http://0pointer.de/lennart/projects/pulseaudio/doxygen/proplist_8h.html">pulseaudio api docs</ulink>.
1980 * Below is an example for registering as a music application to pulseaudio.
1982 * GstStructure *props;
1984 * props = gst_structure_from_string ("props,media.role=music", NULL);
1985 * g_object_set (pulse, "stream-properties", props, NULL);
1986 * gst_structure_free
1991 g_object_class_install_property (gobject_class,
1992 PROP_STREAM_PROPERTIES,
1993 g_param_spec_boxed ("stream-properties", "stream properties",
1994 "list of pulseaudio stream properties",
1995 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1998 /* returns the current time of the sink ringbuffer */
2000 gst_pulsesink_get_time (GstClock * clock, GstBaseAudioSink * sink)
2002 GstPulseSink *psink;
2003 GstPulseRingBuffer *pbuf;
2006 if (!sink->ringbuffer || !sink->ringbuffer->acquired)
2007 return GST_CLOCK_TIME_NONE;
2009 pbuf = GST_PULSERING_BUFFER_CAST (sink->ringbuffer);
2010 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
2012 #ifdef HAVE_PULSE_1_0
2013 if (g_atomic_int_get (&psink->format_lost)) {
2014 /* Stream was lost in a format change, it'll get set up again once
2015 * upstream renegotiates */
2016 return psink->format_lost_time;
2020 pa_threaded_mainloop_lock (mainloop);
2021 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
2024 /* if we don't have enough data to get a timestamp, just return NONE, which
2025 * will return the last reported time */
2026 if (pa_stream_get_time (pbuf->stream, &time) < 0) {
2027 GST_DEBUG_OBJECT (psink, "could not get time");
2028 time = GST_CLOCK_TIME_NONE;
2031 pa_threaded_mainloop_unlock (mainloop);
2033 GST_LOG_OBJECT (psink, "current time is %" GST_TIME_FORMAT,
2034 GST_TIME_ARGS (time));
2041 GST_DEBUG_OBJECT (psink, "the server is dead");
2042 pa_threaded_mainloop_unlock (mainloop);
2044 return GST_CLOCK_TIME_NONE;
2049 gst_pulsesink_sink_info_cb (pa_context * c, const pa_sink_info * i, int eol,
2052 GstPulseRingBuffer *pbuf;
2053 GstPulseSink *psink;
2054 #ifdef HAVE_PULSE_1_0
2059 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
2060 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
2065 g_free (psink->device_description);
2066 psink->device_description = g_strdup (i->description);
2068 #ifdef HAVE_PULSE_1_0
2069 g_mutex_lock (psink->sink_formats_lock);
2071 for (l = g_list_first (psink->sink_formats); l; l = g_list_next (l))
2072 pa_format_info_free ((pa_format_info *) l->data);
2074 g_list_free (psink->sink_formats);
2075 psink->sink_formats = NULL;
2077 for (j = 0; j < i->n_formats; j++)
2078 psink->sink_formats = g_list_prepend (psink->sink_formats,
2079 pa_format_info_copy (i->formats[j]));
2081 g_mutex_unlock (psink->sink_formats_lock);
2085 pa_threaded_mainloop_signal (mainloop, 0);
2088 #ifdef HAVE_PULSE_1_0
2090 gst_pulsesink_pad_acceptcaps (GstPad * pad, GstCaps * caps)
2092 GstPulseSink *psink = GST_PULSESINK (gst_pad_get_parent_element (pad));
2093 GstPulseRingBuffer *pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK
2094 (psink)->ringbuffer);
2097 gboolean ret = FALSE;
2099 GstRingBufferSpec spec = { 0 };
2100 pa_stream *stream = NULL;
2101 pa_operation *o = NULL;
2102 pa_channel_map channel_map;
2103 pa_stream_flags_t flags;
2104 pa_format_info *format = NULL, *formats[1];
2107 pad_caps = gst_pad_get_caps_reffed (pad);
2109 ret = gst_caps_can_intersect (pad_caps, caps);
2110 gst_caps_unref (pad_caps);
2113 /* Either template caps didn't match, or we're still in NULL state */
2114 if (!ret || !pbuf->context)
2117 /* If we've not got fixed caps, creating a stream might fail, so let's just
2118 * return from here with default acceptcaps behaviour */
2119 if (!gst_caps_is_fixed (caps))
2124 pa_threaded_mainloop_lock (mainloop);
2126 spec.latency_time = GST_BASE_AUDIO_SINK (psink)->latency_time;
2127 if (!gst_ring_buffer_parse_caps (&spec, caps))
2130 if (!gst_pulse_fill_format_info (&spec, &format, &channels))
2133 /* Make sure input is framed (one frame per buffer) and can be payloaded */
2134 if (!pa_format_info_is_pcm (format)) {
2135 gboolean framed = FALSE, parsed = FALSE;
2136 st = gst_caps_get_structure (caps, 0);
2138 gst_structure_get_boolean (st, "framed", &framed);
2139 gst_structure_get_boolean (st, "parsed", &parsed);
2140 if ((!framed && !parsed) || gst_audio_iec61937_frame_size (&spec) <= 0)
2144 /* initialize the channel map */
2145 if (pa_format_info_is_pcm (format) &&
2146 gst_pulse_gst_to_channel_map (&channel_map, &spec))
2147 pa_format_info_set_channel_map (format, &channel_map);
2150 /* We're already in PAUSED or above, so just reuse this stream to query
2151 * sink formats and use those. */
2154 if (!(o = pa_context_get_sink_info_by_name (pbuf->context, psink->device,
2155 gst_pulsesink_sink_info_cb, pbuf)))
2158 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2159 pa_threaded_mainloop_wait (mainloop);
2160 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
2164 g_mutex_lock (psink->sink_formats_lock);
2165 for (i = g_list_first (psink->sink_formats); i; i = g_list_next (i)) {
2166 if (pa_format_info_is_compatible ((pa_format_info *) i->data, format)) {
2171 g_mutex_unlock (psink->sink_formats_lock);
2173 /* We're in READY, let's connect a stream to see if the format is
2174 * accpeted by whatever sink we're routed to */
2175 formats[0] = format;
2177 if (!(stream = pa_stream_new_extended (pbuf->context, "pulsesink probe",
2178 formats, 1, psink->proplist)))
2181 /* construct the flags */
2182 flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE |
2183 PA_STREAM_ADJUST_LATENCY | PA_STREAM_START_CORKED;
2185 pa_stream_set_state_callback (stream, gst_pulsering_stream_state_cb, pbuf);
2187 if (pa_stream_connect_playback (stream, psink->device, NULL, flags, NULL,
2191 ret = gst_pulsering_wait_for_stream_ready (psink, stream);
2196 pa_format_info_free (format);
2199 pa_operation_unref (o);
2202 pa_stream_set_state_callback (stream, NULL, NULL);
2203 pa_stream_disconnect (stream);
2204 pa_stream_unref (stream);
2207 pa_threaded_mainloop_unlock (mainloop);
2210 gst_object_unref (psink);
2215 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2216 ("pa_context_get_sink_input_info() failed: %s",
2217 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2224 gst_pulsesink_init (GstPulseSink * pulsesink, GstPulseSinkClass * klass)
2226 pulsesink->server = NULL;
2227 pulsesink->device = NULL;
2228 pulsesink->device_description = NULL;
2229 pulsesink->client_name = gst_pulse_client_name ();
2231 #ifdef HAVE_PULSE_1_0
2232 pulsesink->sink_formats_lock = g_mutex_new ();
2233 pulsesink->sink_formats = NULL;
2236 pulsesink->volume = DEFAULT_VOLUME;
2237 pulsesink->volume_set = FALSE;
2239 pulsesink->mute = DEFAULT_MUTE;
2240 pulsesink->mute_set = FALSE;
2242 pulsesink->notify = 0;
2244 #ifdef HAVE_PULSE_1_0
2245 g_atomic_int_set (&pulsesink->format_lost, FALSE);
2246 pulsesink->format_lost_time = GST_CLOCK_TIME_NONE;
2249 pulsesink->properties = NULL;
2250 pulsesink->proplist = NULL;
2252 /* override with a custom clock */
2253 if (GST_BASE_AUDIO_SINK (pulsesink)->provided_clock)
2254 gst_object_unref (GST_BASE_AUDIO_SINK (pulsesink)->provided_clock);
2256 GST_BASE_AUDIO_SINK (pulsesink)->provided_clock =
2257 gst_audio_clock_new ("GstPulseSinkClock",
2258 (GstAudioClockGetTimeFunc) gst_pulsesink_get_time, pulsesink);
2260 #ifdef HAVE_PULSE_1_0
2261 gst_pad_set_acceptcaps_function (GST_BASE_SINK (pulsesink)->sinkpad,
2262 GST_DEBUG_FUNCPTR (gst_pulsesink_pad_acceptcaps));
2265 /* TRUE for sinks, FALSE for sources */
2266 pulsesink->probe = gst_pulseprobe_new (G_OBJECT (pulsesink),
2267 G_OBJECT_GET_CLASS (pulsesink), PROP_DEVICE, pulsesink->device,
2272 gst_pulsesink_finalize (GObject * object)
2274 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2275 #ifdef HAVE_PULSE_1_0
2279 g_free (pulsesink->server);
2280 g_free (pulsesink->device);
2281 g_free (pulsesink->device_description);
2282 g_free (pulsesink->client_name);
2284 #ifdef HAVE_PULSE_1_0
2285 for (i = g_list_first (pulsesink->sink_formats); i; i = g_list_next (i))
2286 pa_format_info_free ((pa_format_info *) i->data);
2288 g_list_free (pulsesink->sink_formats);
2289 g_mutex_free (pulsesink->sink_formats_lock);
2292 if (pulsesink->properties)
2293 gst_structure_free (pulsesink->properties);
2294 if (pulsesink->proplist)
2295 pa_proplist_free (pulsesink->proplist);
2297 if (pulsesink->probe) {
2298 gst_pulseprobe_free (pulsesink->probe);
2299 pulsesink->probe = NULL;
2302 G_OBJECT_CLASS (parent_class)->finalize (object);
2306 gst_pulsesink_set_volume (GstPulseSink * psink, gdouble volume)
2309 pa_operation *o = NULL;
2310 GstPulseRingBuffer *pbuf;
2316 pa_threaded_mainloop_lock (mainloop);
2318 GST_DEBUG_OBJECT (psink, "setting volume to %f", volume);
2320 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2321 if (pbuf == NULL || pbuf->stream == NULL)
2324 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2327 #ifdef HAVE_PULSE_1_0
2328 if (pa_format_info_is_pcm (pbuf->format))
2329 gst_pulse_cvolume_from_linear (&v, pbuf->channels, volume);
2331 /* FIXME: this will eventually be superceded by checks to see if the volume
2332 * is readable/writable */
2335 gst_pulse_cvolume_from_linear (&v, pbuf->sample_spec.channels, volume);
2338 if (!(o = pa_context_set_sink_input_volume (pbuf->context, idx,
2342 /* We don't really care about the result of this call */
2346 pa_operation_unref (o);
2348 pa_threaded_mainloop_unlock (mainloop);
2355 psink->volume = volume;
2356 psink->volume_set = TRUE;
2358 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2363 psink->volume = volume;
2364 psink->volume_set = TRUE;
2366 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2371 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2376 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2377 ("pa_stream_set_sink_input_volume() failed: %s",
2378 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2384 gst_pulsesink_set_mute (GstPulseSink * psink, gboolean mute)
2386 pa_operation *o = NULL;
2387 GstPulseRingBuffer *pbuf;
2393 pa_threaded_mainloop_lock (mainloop);
2395 GST_DEBUG_OBJECT (psink, "setting mute state to %d", mute);
2397 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2398 if (pbuf == NULL || pbuf->stream == NULL)
2401 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2404 if (!(o = pa_context_set_sink_input_mute (pbuf->context, idx,
2408 /* We don't really care about the result of this call */
2412 pa_operation_unref (o);
2414 pa_threaded_mainloop_unlock (mainloop);
2422 psink->mute_set = TRUE;
2424 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2430 psink->mute_set = TRUE;
2432 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2437 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2442 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2443 ("pa_stream_set_sink_input_mute() failed: %s",
2444 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2450 gst_pulsesink_sink_input_info_cb (pa_context * c, const pa_sink_input_info * i,
2451 int eol, void *userdata)
2453 GstPulseRingBuffer *pbuf;
2454 GstPulseSink *psink;
2456 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
2457 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
2465 /* If the index doesn't match our current stream,
2466 * it implies we just recreated the stream (caps change)
2468 if (i->index == pa_stream_get_index (pbuf->stream)) {
2469 psink->volume = pa_sw_volume_to_linear (pa_cvolume_max (&i->volume));
2470 psink->mute = i->mute;
2474 pa_threaded_mainloop_signal (mainloop, 0);
2478 gst_pulsesink_get_volume (GstPulseSink * psink)
2480 GstPulseRingBuffer *pbuf;
2481 pa_operation *o = NULL;
2482 gdouble v = DEFAULT_VOLUME;
2488 pa_threaded_mainloop_lock (mainloop);
2490 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2491 if (pbuf == NULL || pbuf->stream == NULL)
2494 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2497 if (!(o = pa_context_get_sink_input_info (pbuf->context, idx,
2498 gst_pulsesink_sink_input_info_cb, pbuf)))
2501 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2502 pa_threaded_mainloop_wait (mainloop);
2503 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
2511 pa_operation_unref (o);
2513 pa_threaded_mainloop_unlock (mainloop);
2515 if (v > MAX_VOLUME) {
2516 GST_WARNING_OBJECT (psink, "Clipped volume from %f to %f", v, MAX_VOLUME);
2526 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2531 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2536 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2541 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2542 ("pa_context_get_sink_input_info() failed: %s",
2543 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2549 gst_pulsesink_get_mute (GstPulseSink * psink)
2551 GstPulseRingBuffer *pbuf;
2552 pa_operation *o = NULL;
2554 gboolean mute = FALSE;
2559 pa_threaded_mainloop_lock (mainloop);
2562 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2563 if (pbuf == NULL || pbuf->stream == NULL)
2566 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2569 if (!(o = pa_context_get_sink_input_info (pbuf->context, idx,
2570 gst_pulsesink_sink_input_info_cb, pbuf)))
2573 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2574 pa_threaded_mainloop_wait (mainloop);
2575 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
2581 pa_operation_unref (o);
2583 pa_threaded_mainloop_unlock (mainloop);
2591 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2596 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2601 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2606 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2607 ("pa_context_get_sink_input_info() failed: %s",
2608 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2614 gst_pulsesink_device_description (GstPulseSink * psink)
2616 GstPulseRingBuffer *pbuf;
2617 pa_operation *o = NULL;
2623 pa_threaded_mainloop_lock (mainloop);
2624 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2628 if (!(o = pa_context_get_sink_info_by_name (pbuf->context,
2629 psink->device, gst_pulsesink_sink_info_cb, pbuf)))
2632 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2633 pa_threaded_mainloop_wait (mainloop);
2634 if (gst_pulsering_is_dead (psink, pbuf, FALSE))
2640 pa_operation_unref (o);
2642 t = g_strdup (psink->device_description);
2643 pa_threaded_mainloop_unlock (mainloop);
2650 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2655 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2660 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2661 ("pa_context_get_sink_info_by_index() failed: %s",
2662 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2668 gst_pulsesink_set_property (GObject * object,
2669 guint prop_id, const GValue * value, GParamSpec * pspec)
2671 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2675 g_free (pulsesink->server);
2676 pulsesink->server = g_value_dup_string (value);
2677 if (pulsesink->probe)
2678 gst_pulseprobe_set_server (pulsesink->probe, pulsesink->server);
2681 g_free (pulsesink->device);
2682 pulsesink->device = g_value_dup_string (value);
2685 gst_pulsesink_set_volume (pulsesink, g_value_get_double (value));
2688 gst_pulsesink_set_mute (pulsesink, g_value_get_boolean (value));
2691 g_free (pulsesink->client_name);
2692 if (!g_value_get_string (value)) {
2693 GST_WARNING_OBJECT (pulsesink,
2694 "Empty PulseAudio client name not allowed. Resetting to default value");
2695 pulsesink->client_name = gst_pulse_client_name ();
2697 pulsesink->client_name = g_value_dup_string (value);
2699 case PROP_STREAM_PROPERTIES:
2700 if (pulsesink->properties)
2701 gst_structure_free (pulsesink->properties);
2702 pulsesink->properties =
2703 gst_structure_copy (gst_value_get_structure (value));
2704 if (pulsesink->proplist)
2705 pa_proplist_free (pulsesink->proplist);
2706 pulsesink->proplist = gst_pulse_make_proplist (pulsesink->properties);
2709 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2715 gst_pulsesink_get_property (GObject * object,
2716 guint prop_id, GValue * value, GParamSpec * pspec)
2719 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2723 g_value_set_string (value, pulsesink->server);
2726 g_value_set_string (value, pulsesink->device);
2728 case PROP_DEVICE_NAME:
2729 g_value_take_string (value, gst_pulsesink_device_description (pulsesink));
2732 g_value_set_double (value, gst_pulsesink_get_volume (pulsesink));
2735 g_value_set_boolean (value, gst_pulsesink_get_mute (pulsesink));
2738 g_value_set_string (value, pulsesink->client_name);
2740 case PROP_STREAM_PROPERTIES:
2741 gst_value_set_structure (value, pulsesink->properties);
2744 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2750 gst_pulsesink_change_title (GstPulseSink * psink, const gchar * t)
2752 pa_operation *o = NULL;
2753 GstPulseRingBuffer *pbuf;
2755 pa_threaded_mainloop_lock (mainloop);
2757 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2759 if (pbuf == NULL || pbuf->stream == NULL)
2762 g_free (pbuf->stream_name);
2763 pbuf->stream_name = g_strdup (t);
2765 if (!(o = pa_stream_set_name (pbuf->stream, pbuf->stream_name, NULL, NULL)))
2768 /* We're not interested if this operation failed or not */
2772 pa_operation_unref (o);
2773 pa_threaded_mainloop_unlock (mainloop);
2780 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2785 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2786 ("pa_stream_set_name() failed: %s",
2787 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2793 gst_pulsesink_change_props (GstPulseSink * psink, GstTagList * l)
2795 static const gchar *const map[] = {
2796 GST_TAG_TITLE, PA_PROP_MEDIA_TITLE,
2798 /* might get overriden in the next iteration by GST_TAG_ARTIST */
2799 GST_TAG_PERFORMER, PA_PROP_MEDIA_ARTIST,
2801 GST_TAG_ARTIST, PA_PROP_MEDIA_ARTIST,
2802 GST_TAG_LANGUAGE_CODE, PA_PROP_MEDIA_LANGUAGE,
2803 GST_TAG_LOCATION, PA_PROP_MEDIA_FILENAME,
2804 /* We might add more here later on ... */
2807 pa_proplist *pl = NULL;
2808 const gchar *const *t;
2809 gboolean empty = TRUE;
2810 pa_operation *o = NULL;
2811 GstPulseRingBuffer *pbuf;
2813 pl = pa_proplist_new ();
2815 for (t = map; *t; t += 2) {
2818 if (gst_tag_list_get_string (l, *t, &n)) {
2821 pa_proplist_sets (pl, *(t + 1), n);
2831 pa_threaded_mainloop_lock (mainloop);
2832 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2833 if (pbuf == NULL || pbuf->stream == NULL)
2836 if (!(o = pa_stream_proplist_update (pbuf->stream, PA_UPDATE_REPLACE,
2840 /* We're not interested if this operation failed or not */
2844 pa_operation_unref (o);
2846 pa_threaded_mainloop_unlock (mainloop);
2851 pa_proplist_free (pl);
2858 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2863 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2864 ("pa_stream_proplist_update() failed: %s",
2865 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2871 gst_pulsesink_flush_ringbuffer (GstPulseSink * psink)
2873 GstPulseRingBuffer *pbuf;
2875 pa_threaded_mainloop_lock (mainloop);
2877 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2879 if (pbuf == NULL || pbuf->stream == NULL)
2882 gst_pulsering_flush (pbuf);
2884 /* Uncork if we haven't already (happens when waiting to get enough data
2885 * to send out the first time) */
2887 gst_pulsering_set_corked (pbuf, FALSE, FALSE);
2889 /* We're not interested if this operation failed or not */
2891 pa_threaded_mainloop_unlock (mainloop);
2898 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2904 gst_pulsesink_event (GstBaseSink * sink, GstEvent * event)
2906 GstPulseSink *pulsesink = GST_PULSESINK_CAST (sink);
2908 switch (GST_EVENT_TYPE (event)) {
2909 case GST_EVENT_TAG:{
2910 gchar *title = NULL, *artist = NULL, *location = NULL, *description =
2911 NULL, *t = NULL, *buf = NULL;
2914 gst_event_parse_tag (event, &l);
2916 gst_tag_list_get_string (l, GST_TAG_TITLE, &title);
2917 gst_tag_list_get_string (l, GST_TAG_ARTIST, &artist);
2918 gst_tag_list_get_string (l, GST_TAG_LOCATION, &location);
2919 gst_tag_list_get_string (l, GST_TAG_DESCRIPTION, &description);
2922 gst_tag_list_get_string (l, GST_TAG_PERFORMER, &artist);
2924 if (title && artist)
2925 /* TRANSLATORS: 'song title' by 'artist name' */
2926 t = buf = g_strdup_printf (_("'%s' by '%s'"), g_strstrip (title),
2927 g_strstrip (artist));
2929 t = g_strstrip (title);
2930 else if (description)
2931 t = g_strstrip (description);
2933 t = g_strstrip (location);
2936 gst_pulsesink_change_title (pulsesink, t);
2941 g_free (description);
2944 gst_pulsesink_change_props (pulsesink, l);
2949 gst_pulsesink_flush_ringbuffer (pulsesink);
2955 return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
2959 gst_pulsesink_release_mainloop (GstPulseSink * psink)
2964 pa_threaded_mainloop_lock (mainloop);
2965 while (psink->defer_pending) {
2966 GST_DEBUG_OBJECT (psink, "waiting for stream status message emission");
2967 pa_threaded_mainloop_wait (mainloop);
2969 pa_threaded_mainloop_unlock (mainloop);
2971 g_mutex_lock (pa_shared_resource_mutex);
2973 if (!mainloop_ref_ct) {
2974 GST_INFO_OBJECT (psink, "terminating pa main loop thread");
2975 pa_threaded_mainloop_stop (mainloop);
2976 pa_threaded_mainloop_free (mainloop);
2979 g_mutex_unlock (pa_shared_resource_mutex);
2982 static GstStateChangeReturn
2983 gst_pulsesink_change_state (GstElement * element, GstStateChange transition)
2985 GstPulseSink *pulsesink = GST_PULSESINK (element);
2986 GstStateChangeReturn ret;
2988 switch (transition) {
2989 case GST_STATE_CHANGE_NULL_TO_READY:
2990 g_mutex_lock (pa_shared_resource_mutex);
2991 if (!mainloop_ref_ct) {
2992 GST_INFO_OBJECT (element, "new pa main loop thread");
2993 if (!(mainloop = pa_threaded_mainloop_new ()))
2994 goto mainloop_failed;
2995 mainloop_ref_ct = 1;
2996 pa_threaded_mainloop_start (mainloop);
2997 g_mutex_unlock (pa_shared_resource_mutex);
2999 GST_INFO_OBJECT (element, "reusing pa main loop thread");
3001 g_mutex_unlock (pa_shared_resource_mutex);
3004 case GST_STATE_CHANGE_READY_TO_PAUSED:
3005 gst_element_post_message (element,
3006 gst_message_new_clock_provide (GST_OBJECT_CAST (element),
3007 GST_BASE_AUDIO_SINK (pulsesink)->provided_clock, TRUE));
3014 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3015 if (ret == GST_STATE_CHANGE_FAILURE)
3018 switch (transition) {
3019 case GST_STATE_CHANGE_PAUSED_TO_READY:
3020 /* format_lost is reset in release() in baseaudiosink */
3021 gst_element_post_message (element,
3022 gst_message_new_clock_lost (GST_OBJECT_CAST (element),
3023 GST_BASE_AUDIO_SINK (pulsesink)->provided_clock));
3025 case GST_STATE_CHANGE_READY_TO_NULL:
3026 gst_pulsesink_release_mainloop (pulsesink);
3037 g_mutex_unlock (pa_shared_resource_mutex);
3038 GST_ELEMENT_ERROR (pulsesink, RESOURCE, FAILED,
3039 ("pa_threaded_mainloop_new() failed"), (NULL));
3040 return GST_STATE_CHANGE_FAILURE;
3044 if (transition == GST_STATE_CHANGE_NULL_TO_READY) {
3045 /* Clear the PA mainloop if baseaudiosink failed to open the ring_buffer */
3046 g_assert (mainloop);
3047 gst_pulsesink_release_mainloop (pulsesink);