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)->eos_rendering))
1151 gst_pulsering_set_corked (pbuf, FALSE, FALSE);
1153 pa_threaded_mainloop_unlock (mainloop);
1158 /* pause/stop playback ASAP */
1160 gst_pulseringbuffer_pause (GstRingBuffer * buf)
1162 GstPulseSink *psink;
1163 GstPulseRingBuffer *pbuf;
1166 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1167 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1169 pa_threaded_mainloop_lock (mainloop);
1170 GST_DEBUG_OBJECT (psink, "pausing and corking");
1171 /* make sure the commit method stops writing */
1172 pbuf->paused = TRUE;
1173 res = gst_pulsering_set_corked (pbuf, TRUE, TRUE);
1174 if (pbuf->in_commit) {
1175 /* we are waiting in a commit, signal */
1176 GST_DEBUG_OBJECT (psink, "signal commit");
1177 pa_threaded_mainloop_signal (mainloop, 0);
1179 pa_threaded_mainloop_unlock (mainloop);
1184 /* called from pulse with the mainloop lock */
1186 mainloop_leave_defer_cb (pa_mainloop_api * api, void *userdata)
1188 GstPulseSink *pulsesink = GST_PULSESINK (userdata);
1189 GstMessage *message;
1192 g_value_init (&val, G_TYPE_POINTER);
1193 g_value_set_pointer (&val, g_thread_self ());
1195 GST_DEBUG_OBJECT (pulsesink, "posting LEAVE stream status");
1196 message = gst_message_new_stream_status (GST_OBJECT (pulsesink),
1197 GST_STREAM_STATUS_TYPE_LEAVE, GST_ELEMENT (pulsesink));
1198 gst_message_set_stream_status_object (message, &val);
1199 gst_element_post_message (GST_ELEMENT (pulsesink), message);
1201 g_return_if_fail (pulsesink->defer_pending);
1202 pulsesink->defer_pending--;
1203 pa_threaded_mainloop_signal (mainloop, 0);
1206 /* stop playback, we flush everything. */
1208 gst_pulseringbuffer_stop (GstRingBuffer * buf)
1210 GstPulseSink *psink;
1211 GstPulseRingBuffer *pbuf;
1212 gboolean res = FALSE;
1213 pa_operation *o = NULL;
1215 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1216 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1218 pa_threaded_mainloop_lock (mainloop);
1220 pbuf->paused = TRUE;
1221 res = gst_pulsering_set_corked (pbuf, TRUE, TRUE);
1223 /* Inform anyone waiting in _commit() call that it shall wakeup */
1224 if (pbuf->in_commit) {
1225 GST_DEBUG_OBJECT (psink, "signal commit thread");
1226 pa_threaded_mainloop_signal (mainloop, 0);
1228 #ifdef HAVE_PULSE_1_0
1229 if (g_atomic_int_get (&psink->format_lost)) {
1230 /* Don't try to flush, the stream's probably gone by now */
1236 /* then try to flush, it's not fatal when this fails */
1237 GST_DEBUG_OBJECT (psink, "flushing");
1238 if ((o = pa_stream_flush (pbuf->stream, gst_pulsering_success_cb, pbuf))) {
1239 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1240 GST_DEBUG_OBJECT (psink, "wait for completion");
1241 pa_threaded_mainloop_wait (mainloop);
1242 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
1245 GST_DEBUG_OBJECT (psink, "flush completed");
1251 pa_operation_cancel (o);
1252 pa_operation_unref (o);
1255 GST_DEBUG_OBJECT (psink, "scheduling stream status");
1256 psink->defer_pending++;
1257 pa_mainloop_api_once (pa_threaded_mainloop_get_api (mainloop),
1258 mainloop_leave_defer_cb, psink);
1260 pa_threaded_mainloop_unlock (mainloop);
1267 GST_DEBUG_OBJECT (psink, "the server is dead");
1272 /* in_samples >= out_samples, rate > 1.0 */
1273 #define FWD_UP_SAMPLES(s,se,d,de) \
1275 guint8 *sb = s, *db = d; \
1276 while (s <= se && d < de) { \
1277 memcpy (d, s, bpf); \
1280 if ((*accum << 1) >= inr) { \
1285 in_samples -= (s - sb)/bpf; \
1286 out_samples -= (d - db)/bpf; \
1287 GST_DEBUG ("fwd_up end %d/%d",*accum,*toprocess); \
1290 /* out_samples > in_samples, for rates smaller than 1.0 */
1291 #define FWD_DOWN_SAMPLES(s,se,d,de) \
1293 guint8 *sb = s, *db = d; \
1294 while (s <= se && d < de) { \
1295 memcpy (d, s, bpf); \
1298 if ((*accum << 1) >= outr) { \
1303 in_samples -= (s - sb)/bpf; \
1304 out_samples -= (d - db)/bpf; \
1305 GST_DEBUG ("fwd_down end %d/%d",*accum,*toprocess); \
1308 #define REV_UP_SAMPLES(s,se,d,de) \
1310 guint8 *sb = se, *db = d; \
1311 while (s <= se && d < de) { \
1312 memcpy (d, se, bpf); \
1315 while (d < de && (*accum << 1) >= inr) { \
1320 in_samples -= (sb - se)/bpf; \
1321 out_samples -= (d - db)/bpf; \
1322 GST_DEBUG ("rev_up end %d/%d",*accum,*toprocess); \
1325 #define REV_DOWN_SAMPLES(s,se,d,de) \
1327 guint8 *sb = se, *db = d; \
1328 while (s <= se && d < de) { \
1329 memcpy (d, se, bpf); \
1332 while (s <= se && (*accum << 1) >= outr) { \
1337 in_samples -= (sb - se)/bpf; \
1338 out_samples -= (d - db)/bpf; \
1339 GST_DEBUG ("rev_down end %d/%d",*accum,*toprocess); \
1342 /* our custom commit function because we write into the buffer of pulseaudio
1343 * instead of keeping our own buffer */
1345 gst_pulseringbuffer_commit (GstRingBuffer * buf, guint64 * sample,
1346 guchar * data, gint in_samples, gint out_samples, gint * accum)
1348 GstPulseSink *psink;
1349 GstPulseRingBuffer *pbuf;
1354 gint inr, outr, bpf;
1358 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1359 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1361 /* FIXME post message rather than using a signal (as mixer interface) */
1362 if (g_atomic_int_compare_and_exchange (&psink->notify, 1, 0)) {
1363 g_object_notify (G_OBJECT (psink), "volume");
1364 g_object_notify (G_OBJECT (psink), "mute");
1367 /* make sure the ringbuffer is started */
1368 if (G_UNLIKELY (g_atomic_int_get (&buf->state) !=
1369 GST_RING_BUFFER_STATE_STARTED)) {
1370 /* see if we are allowed to start it */
1371 if (G_UNLIKELY (g_atomic_int_get (&buf->may_start) == FALSE))
1374 GST_DEBUG_OBJECT (buf, "start!");
1375 if (!gst_ring_buffer_start (buf))
1379 pa_threaded_mainloop_lock (mainloop);
1381 GST_DEBUG_OBJECT (psink, "entering commit");
1382 pbuf->in_commit = TRUE;
1384 bpf = GST_AUDIO_INFO_BPF (&buf->spec.info);
1385 bufsize = buf->spec.segsize * buf->spec.segtotal;
1387 /* our toy resampler for trick modes */
1388 reverse = out_samples < 0;
1389 out_samples = ABS (out_samples);
1391 if (in_samples >= out_samples)
1392 toprocess = &in_samples;
1394 toprocess = &out_samples;
1396 inr = in_samples - 1;
1397 outr = out_samples - 1;
1399 GST_DEBUG_OBJECT (psink, "in %d, out %d", inr, outr);
1401 /* data_end points to the last sample we have to write, not past it. This is
1402 * needed to properly handle reverse playback: it points to the last sample. */
1403 data_end = data + (bpf * inr);
1405 #ifdef HAVE_PULSE_1_0
1406 if (g_atomic_int_get (&psink->format_lost)) {
1407 /* Sink format changed, drop the data and hope upstream renegotiates */
1415 /* offset is in bytes */
1416 offset = *sample * bpf;
1418 while (*toprocess > 0) {
1422 GST_LOG_OBJECT (psink,
1423 "need to write %d samples at offset %" G_GINT64_FORMAT, *toprocess,
1426 if (offset != pbuf->m_lastoffset)
1427 GST_LOG_OBJECT (psink, "discontinuity, offset is %" G_GINT64_FORMAT ", "
1428 "last offset was %" G_GINT64_FORMAT, offset, pbuf->m_lastoffset);
1430 towrite = out_samples * bpf;
1432 /* Wait for at least segsize bytes to become available */
1433 if (towrite > buf->spec.segsize)
1434 towrite = buf->spec.segsize;
1436 if ((pbuf->m_writable < towrite) || (offset != pbuf->m_lastoffset)) {
1437 /* if no room left or discontinuity in offset,
1438 we need to flush data and get a new buffer */
1440 /* flush the buffer if possible */
1441 if ((pbuf->m_data != NULL) && (pbuf->m_towrite > 0)) {
1443 GST_LOG_OBJECT (psink,
1444 "flushing %u samples at offset %" G_GINT64_FORMAT,
1445 (guint) pbuf->m_towrite / bpf, pbuf->m_offset);
1447 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1448 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1452 pbuf->m_towrite = 0;
1453 pbuf->m_offset = offset; /* keep track of current offset */
1455 /* get a buffer to write in for now on */
1457 pbuf->m_writable = pa_stream_writable_size (pbuf->stream);
1459 #ifdef HAVE_PULSE_1_0
1460 if (g_atomic_int_get (&psink->format_lost)) {
1461 /* Sink format changed, give up and hope upstream renegotiates */
1466 if (pbuf->m_writable == (size_t) - 1)
1467 goto writable_size_failed;
1469 pbuf->m_writable /= bpf;
1470 pbuf->m_writable *= bpf; /* handle only complete samples */
1472 if (pbuf->m_writable >= towrite)
1475 /* see if we need to uncork because we have no free space */
1477 if (!gst_pulsering_set_corked (pbuf, FALSE, FALSE))
1481 /* we can't write segsize bytes, wait a bit */
1482 GST_LOG_OBJECT (psink, "waiting for free space");
1483 pa_threaded_mainloop_wait (mainloop);
1489 /* Recalculate what we can write in the next chunk */
1490 towrite = out_samples * bpf;
1491 if (pbuf->m_writable > towrite)
1492 pbuf->m_writable = towrite;
1494 GST_LOG_OBJECT (psink, "requesting %" G_GSIZE_FORMAT " bytes of "
1495 "shared memory", pbuf->m_writable);
1497 if (pa_stream_begin_write (pbuf->stream, &pbuf->m_data,
1498 &pbuf->m_writable) < 0) {
1499 GST_LOG_OBJECT (psink, "pa_stream_begin_write() failed");
1500 goto writable_size_failed;
1503 GST_LOG_OBJECT (psink, "got %" G_GSIZE_FORMAT " bytes of shared memory",
1508 if (towrite > pbuf->m_writable)
1509 towrite = pbuf->m_writable;
1510 avail = towrite / bpf;
1512 GST_LOG_OBJECT (psink, "writing %u samples at offset %" G_GUINT64_FORMAT,
1513 (guint) avail, offset);
1515 #ifdef HAVE_PULSE_1_0
1516 /* No trick modes for passthrough streams */
1517 if (G_UNLIKELY (inr != outr || reverse)) {
1518 GST_WARNING_OBJECT (psink, "Passthrough stream can't run in trick mode");
1519 goto unlock_and_fail;
1523 if (G_LIKELY (inr == outr && !reverse)) {
1524 /* no rate conversion, simply write out the samples */
1525 /* copy the data into internal buffer */
1527 memcpy ((guint8 *) pbuf->m_data + pbuf->m_towrite, data, towrite);
1528 pbuf->m_towrite += towrite;
1529 pbuf->m_writable -= towrite;
1532 in_samples -= avail;
1533 out_samples -= avail;
1535 guint8 *dest, *d, *d_end;
1537 /* write into the PulseAudio shm buffer */
1538 dest = d = (guint8 *) pbuf->m_data + pbuf->m_towrite;
1539 d_end = d + towrite;
1543 /* forward speed up */
1544 FWD_UP_SAMPLES (data, data_end, d, d_end);
1546 /* forward slow down */
1547 FWD_DOWN_SAMPLES (data, data_end, d, d_end);
1550 /* reverse speed up */
1551 REV_UP_SAMPLES (data, data_end, d, d_end);
1553 /* reverse slow down */
1554 REV_DOWN_SAMPLES (data, data_end, d, d_end);
1556 /* see what we have left to write */
1557 towrite = (d - dest);
1558 pbuf->m_towrite += towrite;
1559 pbuf->m_writable -= towrite;
1561 avail = towrite / bpf;
1564 /* flush the buffer if it's full */
1565 if ((pbuf->m_data != NULL) && (pbuf->m_towrite > 0)
1566 && (pbuf->m_writable == 0)) {
1567 GST_LOG_OBJECT (psink, "flushing %u samples at offset %" G_GINT64_FORMAT,
1568 (guint) pbuf->m_towrite / bpf, pbuf->m_offset);
1570 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1571 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1574 pbuf->m_towrite = 0;
1575 pbuf->m_offset = offset + towrite; /* keep track of current offset */
1579 offset += avail * bpf;
1580 pbuf->m_lastoffset = offset;
1582 /* check if we need to uncork after writing the samples */
1584 const pa_timing_info *info;
1586 if ((info = pa_stream_get_timing_info (pbuf->stream))) {
1587 GST_LOG_OBJECT (psink,
1588 "read_index at %" G_GUINT64_FORMAT ", offset %" G_GINT64_FORMAT,
1589 info->read_index, offset);
1591 /* we uncork when the read_index is too far behind the offset we need
1593 if (info->read_index + bufsize <= offset) {
1594 if (!gst_pulsering_set_corked (pbuf, FALSE, FALSE))
1598 GST_LOG_OBJECT (psink, "no timing info available yet");
1603 #ifdef HAVE_PULSE_1_0
1606 /* we consumed all samples here */
1607 data = data_end + bpf;
1609 pbuf->in_commit = FALSE;
1610 pa_threaded_mainloop_unlock (mainloop);
1613 result = inr - ((data_end - data) / bpf);
1614 GST_LOG_OBJECT (psink, "wrote %d samples", result);
1621 pbuf->in_commit = FALSE;
1622 GST_LOG_OBJECT (psink, "we are reset");
1623 pa_threaded_mainloop_unlock (mainloop);
1628 GST_LOG_OBJECT (psink, "we can not start");
1633 GST_LOG_OBJECT (psink, "failed to start the ringbuffer");
1638 pbuf->in_commit = FALSE;
1639 GST_ERROR_OBJECT (psink, "uncork failed");
1640 pa_threaded_mainloop_unlock (mainloop);
1645 pbuf->in_commit = FALSE;
1646 GST_LOG_OBJECT (psink, "we are paused");
1647 pa_threaded_mainloop_unlock (mainloop);
1650 writable_size_failed:
1652 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1653 ("pa_stream_writable_size() failed: %s",
1654 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1655 goto unlock_and_fail;
1659 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1660 ("pa_stream_write() failed: %s",
1661 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1662 goto unlock_and_fail;
1666 /* write pending local samples, must be called with the mainloop lock */
1668 gst_pulsering_flush (GstPulseRingBuffer * pbuf)
1670 GstPulseSink *psink;
1672 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1673 GST_DEBUG_OBJECT (psink, "entering flush");
1675 /* flush the buffer if possible */
1676 if (pbuf->stream && (pbuf->m_data != NULL) && (pbuf->m_towrite > 0)) {
1677 #ifndef GST_DISABLE_GST_DEBUG
1680 bpf = (GST_RING_BUFFER_CAST (pbuf))->spec.info.bpf;
1681 GST_LOG_OBJECT (psink,
1682 "flushing %u samples at offset %" G_GINT64_FORMAT,
1683 (guint) pbuf->m_towrite / bpf, pbuf->m_offset);
1686 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1687 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1691 pbuf->m_towrite = 0;
1692 pbuf->m_offset += pbuf->m_towrite; /* keep track of current offset */
1701 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1702 ("pa_stream_write() failed: %s",
1703 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1708 static void gst_pulsesink_set_property (GObject * object, guint prop_id,
1709 const GValue * value, GParamSpec * pspec);
1710 static void gst_pulsesink_get_property (GObject * object, guint prop_id,
1711 GValue * value, GParamSpec * pspec);
1712 static void gst_pulsesink_finalize (GObject * object);
1714 static gboolean gst_pulsesink_event (GstBaseSink * sink, GstEvent * event);
1716 static GstStateChangeReturn gst_pulsesink_change_state (GstElement * element,
1717 GstStateChange transition);
1719 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
1720 # define FORMATS "{ S16LE, S16BE, F32LE, F32BE, S32LE, S32BE, " \
1721 "S24LE, S24BE, S24_32LE, S24_32BE, S8 }"
1723 # define FORMATS "{ S16BE, S16LE, F32BE, F32LE, S32BE, S32LE, " \
1724 "S24BE, S24LE, S24_32BE, S24_32LE, S8 }"
1727 static GstStaticPadTemplate pad_template = GST_STATIC_PAD_TEMPLATE ("sink",
1730 GST_STATIC_CAPS ("audio/x-raw, "
1731 "format = (string) " FORMATS ", "
1732 "rate = (int) [ 1, MAX ], "
1733 "channels = (int) [ 1, 32 ];"
1735 "rate = (int) [ 1, MAX], "
1736 "channels = (int) [ 1, 32 ];"
1738 "rate = (int) [ 1, MAX], " "channels = (int) [ 1, 32 ];"
1739 #ifdef HAVE_PULSE_1_0
1740 "audio/x-ac3, framed = (boolean) true;"
1741 "audio/x-eac3, framed = (boolean) true; "
1742 "audio/x-dts, framed = (boolean) true, "
1743 " block_size = (int) { 512, 1024, 2048 }; "
1744 "audio/mpeg, mpegversion = (int)1, "
1745 " mpegaudioversion = (int) [ 1, 2 ], parsed = (boolean) true; "
1749 GST_IMPLEMENT_PULSEPROBE_METHODS (GstPulseSink, gst_pulsesink);
1751 #define gst_pulsesink_parent_class parent_class
1752 G_DEFINE_TYPE_WITH_CODE (GstPulseSink, gst_pulsesink, GST_TYPE_BASE_AUDIO_SINK,
1753 gst_pulsesink_init_contexts ();
1754 G_IMPLEMENT_INTERFACE (GST_TYPE_PROPERTY_PROBE,
1755 gst_pulsesink_property_probe_interface_init);
1756 G_IMPLEMENT_INTERFACE (GST_TYPE_STREAM_VOLUME, NULL)
1759 static GstRingBuffer *
1760 gst_pulsesink_create_ringbuffer (GstBaseAudioSink * sink)
1762 GstRingBuffer *buffer;
1764 GST_DEBUG_OBJECT (sink, "creating ringbuffer");
1765 buffer = g_object_new (GST_TYPE_PULSERING_BUFFER, NULL);
1766 GST_DEBUG_OBJECT (sink, "created ringbuffer @%p", buffer);
1772 gst_pulsesink_payload (GstBaseAudioSink * sink, GstBuffer * buf)
1774 switch (sink->ringbuffer->spec.type) {
1775 case GST_BUFTYPE_AC3:
1776 case GST_BUFTYPE_EAC3:
1777 case GST_BUFTYPE_DTS:
1778 case GST_BUFTYPE_MPEG:
1780 /* FIXME: alloc memory from PA if possible */
1781 gint framesize = gst_audio_iec61937_frame_size (&sink->ringbuffer->spec);
1783 guint8 *indata, *outdata;
1784 gsize insize, outsize;
1790 out = gst_buffer_new_and_alloc (framesize);
1792 indata = gst_buffer_map (buf, &insize, NULL, GST_MAP_READ);
1793 outdata = gst_buffer_map (out, &outsize, NULL, GST_MAP_WRITE);
1795 res = gst_audio_iec61937_payload (indata, insize,
1796 outdata, outsize, &sink->ringbuffer->spec);
1798 gst_buffer_unmap (buf, indata, insize);
1799 gst_buffer_unmap (out, outdata, outsize);
1802 gst_buffer_unref (out);
1806 gst_buffer_copy_into (out, buf, GST_BUFFER_COPY_METADATA, 0, -1);
1811 return gst_buffer_ref (buf);
1816 gst_pulsesink_class_init (GstPulseSinkClass * klass)
1818 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1819 GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
1820 GstBaseSinkClass *bc;
1821 GstBaseAudioSinkClass *gstaudiosink_class = GST_BASE_AUDIO_SINK_CLASS (klass);
1822 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1824 gobject_class->finalize = gst_pulsesink_finalize;
1825 gobject_class->set_property = gst_pulsesink_set_property;
1826 gobject_class->get_property = gst_pulsesink_get_property;
1828 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_pulsesink_event);
1830 /* restore the original basesink pull methods */
1831 bc = g_type_class_peek (GST_TYPE_BASE_SINK);
1832 gstbasesink_class->activate_pull = GST_DEBUG_FUNCPTR (bc->activate_pull);
1834 gstelement_class->change_state =
1835 GST_DEBUG_FUNCPTR (gst_pulsesink_change_state);
1837 gstaudiosink_class->create_ringbuffer =
1838 GST_DEBUG_FUNCPTR (gst_pulsesink_create_ringbuffer);
1839 gstaudiosink_class->payload = GST_DEBUG_FUNCPTR (gst_pulsesink_payload);
1841 /* Overwrite GObject fields */
1842 g_object_class_install_property (gobject_class,
1844 g_param_spec_string ("server", "Server",
1845 "The PulseAudio server to connect to", DEFAULT_SERVER,
1846 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1848 g_object_class_install_property (gobject_class, PROP_DEVICE,
1849 g_param_spec_string ("device", "Device",
1850 "The PulseAudio sink device to connect to", DEFAULT_DEVICE,
1851 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1853 g_object_class_install_property (gobject_class,
1855 g_param_spec_string ("device-name", "Device name",
1856 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
1857 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1859 g_object_class_install_property (gobject_class,
1861 g_param_spec_double ("volume", "Volume",
1862 "Linear volume of this stream, 1.0=100%", 0.0, MAX_VOLUME,
1863 DEFAULT_VOLUME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1864 g_object_class_install_property (gobject_class,
1866 g_param_spec_boolean ("mute", "Mute",
1867 "Mute state of this stream", DEFAULT_MUTE,
1868 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1871 * GstPulseSink:client
1873 * The PulseAudio client name to use.
1877 g_object_class_install_property (gobject_class,
1879 g_param_spec_string ("client", "Client",
1880 "The PulseAudio client name to use", gst_pulse_client_name (),
1881 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1882 GST_PARAM_MUTABLE_READY));
1885 * GstPulseSink:stream-properties
1887 * List of pulseaudio stream properties. A list of defined properties can be
1888 * found in the <ulink url="http://0pointer.de/lennart/projects/pulseaudio/doxygen/proplist_8h.html">pulseaudio api docs</ulink>.
1890 * Below is an example for registering as a music application to pulseaudio.
1892 * GstStructure *props;
1894 * props = gst_structure_from_string ("props,media.role=music", NULL);
1895 * g_object_set (pulse, "stream-properties", props, NULL);
1896 * gst_structure_free
1901 g_object_class_install_property (gobject_class,
1902 PROP_STREAM_PROPERTIES,
1903 g_param_spec_boxed ("stream-properties", "stream properties",
1904 "list of pulseaudio stream properties",
1905 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1907 gst_element_class_set_details_simple (gstelement_class,
1908 "PulseAudio Audio Sink",
1909 "Sink/Audio", "Plays audio to a PulseAudio server", "Lennart Poettering");
1910 gst_element_class_add_pad_template (gstelement_class,
1911 gst_static_pad_template_get (&pad_template));
1914 /* returns the current time of the sink ringbuffer */
1916 gst_pulsesink_get_time (GstClock * clock, GstBaseAudioSink * sink)
1918 GstPulseSink *psink;
1919 GstPulseRingBuffer *pbuf;
1922 if (!sink->ringbuffer || !sink->ringbuffer->acquired)
1923 return GST_CLOCK_TIME_NONE;
1925 pbuf = GST_PULSERING_BUFFER_CAST (sink->ringbuffer);
1926 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1928 #ifdef HAVE_PULSE_1_0
1929 if (g_atomic_int_get (&psink->format_lost)) {
1930 /* Stream was lost in a format change, it'll get set up again once
1931 * upstream renegotiates */
1932 return psink->format_lost_time;
1936 pa_threaded_mainloop_lock (mainloop);
1937 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
1940 /* if we don't have enough data to get a timestamp, just return NONE, which
1941 * will return the last reported time */
1942 if (pa_stream_get_time (pbuf->stream, &time) < 0) {
1943 GST_DEBUG_OBJECT (psink, "could not get time");
1944 time = GST_CLOCK_TIME_NONE;
1947 pa_threaded_mainloop_unlock (mainloop);
1949 GST_LOG_OBJECT (psink, "current time is %" GST_TIME_FORMAT,
1950 GST_TIME_ARGS (time));
1957 GST_DEBUG_OBJECT (psink, "the server is dead");
1958 pa_threaded_mainloop_unlock (mainloop);
1960 return GST_CLOCK_TIME_NONE;
1965 gst_pulsesink_sink_info_cb (pa_context * c, const pa_sink_info * i, int eol,
1968 GstPulseRingBuffer *pbuf;
1969 GstPulseSink *psink;
1970 #ifdef HAVE_PULSE_1_0
1975 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
1976 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1981 g_free (psink->device_description);
1982 psink->device_description = g_strdup (i->description);
1984 #ifdef HAVE_PULSE_1_0
1985 g_mutex_lock (psink->sink_formats_lock);
1987 for (l = g_list_first (psink->sink_formats); l; l = g_list_next (l))
1988 pa_format_info_free ((pa_format_info *) l->data);
1990 g_list_free (psink->sink_formats);
1991 psink->sink_formats = NULL;
1993 for (j = 0; j < i->n_formats; j++)
1994 psink->sink_formats = g_list_prepend (psink->sink_formats,
1995 pa_format_info_copy (i->formats[j]));
1997 g_mutex_unlock (psink->sink_formats_lock);
2001 pa_threaded_mainloop_signal (mainloop, 0);
2004 #ifdef HAVE_PULSE_1_0
2006 gst_pulsesink_pad_acceptcaps (GstPad * pad, GstCaps * caps)
2008 GstPulseSink *psink = GST_PULSESINK (gst_pad_get_parent_element (pad));
2009 GstPulseRingBuffer *pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK
2010 (psink)->ringbuffer);
2013 gboolean ret = FALSE;
2015 GstRingBufferSpec spec = { 0 };
2016 pa_stream *stream = NULL;
2017 pa_operation *o = NULL;
2018 pa_channel_map channel_map;
2019 pa_stream_flags_t flags;
2020 pa_format_info *format = NULL, *formats[1];
2023 pad_caps = gst_pad_get_caps_reffed (pad);
2025 ret = gst_caps_can_intersect (pad_caps, caps);
2026 gst_caps_unref (pad_caps);
2029 /* Either template caps didn't match, or we're still in NULL state */
2030 if (!ret || !pbuf->context)
2033 /* If we've not got fixed caps, creating a stream might fail, so let's just
2034 * return from here with default acceptcaps behaviour */
2035 if (!gst_caps_is_fixed (caps))
2040 pa_threaded_mainloop_lock (mainloop);
2042 spec.latency_time = GST_BASE_AUDIO_SINK (psink)->latency_time;
2043 if (!gst_ring_buffer_parse_caps (&spec, caps))
2046 if (!gst_pulse_fill_format_info (&spec, &format, &channels))
2049 /* Make sure input is framed (one frame per buffer) and can be payloaded */
2050 if (!pa_format_info_is_pcm (format)) {
2051 gboolean framed = FALSE, parsed = FALSE;
2052 st = gst_caps_get_structure (caps, 0);
2054 gst_structure_get_boolean (st, "framed", &framed);
2055 gst_structure_get_boolean (st, "parsed", &parsed);
2056 if ((!framed && !parsed) || gst_audio_iec61937_frame_size (&spec) <= 0)
2060 /* initialize the channel map */
2061 if (pa_format_info_is_pcm (format) &&
2062 gst_pulse_gst_to_channel_map (&channel_map, &spec))
2063 pa_format_info_set_channel_map (format, &channel_map);
2066 /* We're already in PAUSED or above, so just reuse this stream to query
2067 * sink formats and use those. */
2070 if (!(o = pa_context_get_sink_info_by_name (pbuf->context, psink->device,
2071 gst_pulsesink_sink_info_cb, pbuf)))
2074 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2075 pa_threaded_mainloop_wait (mainloop);
2076 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
2080 g_mutex_lock (psink->sink_formats_lock);
2081 for (i = g_list_first (psink->sink_formats); i; i = g_list_next (i)) {
2082 if (pa_format_info_is_compatible ((pa_format_info *) i->data, format)) {
2087 g_mutex_unlock (psink->sink_formats_lock);
2089 /* We're in READY, let's connect a stream to see if the format is
2090 * accpeted by whatever sink we're routed to */
2091 formats[0] = format;
2093 if (!(stream = pa_stream_new_extended (pbuf->context, "pulsesink probe",
2094 formats, 1, psink->proplist)))
2097 /* construct the flags */
2098 flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE |
2099 PA_STREAM_ADJUST_LATENCY | PA_STREAM_START_CORKED;
2101 pa_stream_set_state_callback (stream, gst_pulsering_stream_state_cb, pbuf);
2103 if (pa_stream_connect_playback (stream, psink->device, NULL, flags, NULL,
2107 ret = gst_pulsering_wait_for_stream_ready (psink, stream);
2112 pa_format_info_free (format);
2115 pa_operation_unref (o);
2118 pa_stream_set_state_callback (stream, NULL, NULL);
2119 pa_stream_disconnect (stream);
2120 pa_stream_unref (stream);
2123 pa_threaded_mainloop_unlock (mainloop);
2126 gst_object_unref (psink);
2131 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2132 ("pa_context_get_sink_input_info() failed: %s",
2133 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2140 gst_pulsesink_init (GstPulseSink * pulsesink)
2142 pulsesink->server = NULL;
2143 pulsesink->device = NULL;
2144 pulsesink->device_description = NULL;
2145 pulsesink->client_name = gst_pulse_client_name ();
2147 #ifdef HAVE_PULSE_1_0
2148 pulsesink->sink_formats_lock = g_mutex_new ();
2149 pulsesink->sink_formats = NULL;
2152 pulsesink->volume = DEFAULT_VOLUME;
2153 pulsesink->volume_set = FALSE;
2155 pulsesink->mute = DEFAULT_MUTE;
2156 pulsesink->mute_set = FALSE;
2158 pulsesink->notify = 0;
2160 #ifdef HAVE_PULSE_1_0
2161 g_atomic_int_set (&pulsesink->format_lost, FALSE);
2162 pulsesink->format_lost_time = GST_CLOCK_TIME_NONE;
2165 pulsesink->properties = NULL;
2166 pulsesink->proplist = NULL;
2168 /* override with a custom clock */
2169 if (GST_BASE_AUDIO_SINK (pulsesink)->provided_clock)
2170 gst_object_unref (GST_BASE_AUDIO_SINK (pulsesink)->provided_clock);
2172 GST_BASE_AUDIO_SINK (pulsesink)->provided_clock =
2173 gst_audio_clock_new ("GstPulseSinkClock",
2174 (GstAudioClockGetTimeFunc) gst_pulsesink_get_time, pulsesink);
2176 #ifdef HAVE_PULSE_1_0
2177 gst_pad_set_acceptcaps_function (GST_BASE_SINK (pulsesink)->sinkpad,
2178 GST_DEBUG_FUNCPTR (gst_pulsesink_pad_acceptcaps));
2181 /* TRUE for sinks, FALSE for sources */
2182 pulsesink->probe = gst_pulseprobe_new (G_OBJECT (pulsesink),
2183 G_OBJECT_GET_CLASS (pulsesink), PROP_DEVICE, pulsesink->device,
2188 gst_pulsesink_finalize (GObject * object)
2190 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2191 #ifdef HAVE_PULSE_1_0
2195 g_free (pulsesink->server);
2196 g_free (pulsesink->device);
2197 g_free (pulsesink->device_description);
2198 g_free (pulsesink->client_name);
2200 #ifdef HAVE_PULSE_1_0
2201 for (i = g_list_first (pulsesink->sink_formats); i; i = g_list_next (i))
2202 pa_format_info_free ((pa_format_info *) i->data);
2204 g_list_free (pulsesink->sink_formats);
2205 g_mutex_free (pulsesink->sink_formats_lock);
2208 if (pulsesink->properties)
2209 gst_structure_free (pulsesink->properties);
2210 if (pulsesink->proplist)
2211 pa_proplist_free (pulsesink->proplist);
2213 if (pulsesink->probe) {
2214 gst_pulseprobe_free (pulsesink->probe);
2215 pulsesink->probe = NULL;
2218 G_OBJECT_CLASS (parent_class)->finalize (object);
2222 gst_pulsesink_set_volume (GstPulseSink * psink, gdouble volume)
2225 pa_operation *o = NULL;
2226 GstPulseRingBuffer *pbuf;
2232 pa_threaded_mainloop_lock (mainloop);
2234 GST_DEBUG_OBJECT (psink, "setting volume to %f", volume);
2236 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2237 if (pbuf == NULL || pbuf->stream == NULL)
2240 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2243 #ifdef HAVE_PULSE_1_0
2244 if (pa_format_info_is_pcm (pbuf->format))
2245 gst_pulse_cvolume_from_linear (&v, pbuf->channels, volume);
2247 /* FIXME: this will eventually be superceded by checks to see if the volume
2248 * is readable/writable */
2251 gst_pulse_cvolume_from_linear (&v, pbuf->sample_spec.channels, volume);
2254 if (!(o = pa_context_set_sink_input_volume (pbuf->context, idx,
2258 /* We don't really care about the result of this call */
2262 pa_operation_unref (o);
2264 pa_threaded_mainloop_unlock (mainloop);
2271 psink->volume = volume;
2272 psink->volume_set = TRUE;
2274 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2279 psink->volume = volume;
2280 psink->volume_set = TRUE;
2282 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2287 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2292 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2293 ("pa_stream_set_sink_input_volume() failed: %s",
2294 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2300 gst_pulsesink_set_mute (GstPulseSink * psink, gboolean mute)
2302 pa_operation *o = NULL;
2303 GstPulseRingBuffer *pbuf;
2309 pa_threaded_mainloop_lock (mainloop);
2311 GST_DEBUG_OBJECT (psink, "setting mute state to %d", mute);
2313 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2314 if (pbuf == NULL || pbuf->stream == NULL)
2317 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2320 if (!(o = pa_context_set_sink_input_mute (pbuf->context, idx,
2324 /* We don't really care about the result of this call */
2328 pa_operation_unref (o);
2330 pa_threaded_mainloop_unlock (mainloop);
2338 psink->mute_set = TRUE;
2340 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2346 psink->mute_set = TRUE;
2348 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2353 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2358 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2359 ("pa_stream_set_sink_input_mute() failed: %s",
2360 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2366 gst_pulsesink_sink_input_info_cb (pa_context * c, const pa_sink_input_info * i,
2367 int eol, void *userdata)
2369 GstPulseRingBuffer *pbuf;
2370 GstPulseSink *psink;
2372 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
2373 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
2381 /* If the index doesn't match our current stream,
2382 * it implies we just recreated the stream (caps change)
2384 if (i->index == pa_stream_get_index (pbuf->stream)) {
2385 psink->volume = pa_sw_volume_to_linear (pa_cvolume_max (&i->volume));
2386 psink->mute = i->mute;
2390 pa_threaded_mainloop_signal (mainloop, 0);
2394 gst_pulsesink_get_volume (GstPulseSink * psink)
2396 GstPulseRingBuffer *pbuf;
2397 pa_operation *o = NULL;
2398 gdouble v = DEFAULT_VOLUME;
2404 pa_threaded_mainloop_lock (mainloop);
2406 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2407 if (pbuf == NULL || pbuf->stream == NULL)
2410 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2413 if (!(o = pa_context_get_sink_input_info (pbuf->context, idx,
2414 gst_pulsesink_sink_input_info_cb, pbuf)))
2417 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2418 pa_threaded_mainloop_wait (mainloop);
2419 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
2427 pa_operation_unref (o);
2429 pa_threaded_mainloop_unlock (mainloop);
2431 if (v > MAX_VOLUME) {
2432 GST_WARNING_OBJECT (psink, "Clipped volume from %f to %f", v, MAX_VOLUME);
2442 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2447 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2452 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2457 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2458 ("pa_context_get_sink_input_info() failed: %s",
2459 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2465 gst_pulsesink_get_mute (GstPulseSink * psink)
2467 GstPulseRingBuffer *pbuf;
2468 pa_operation *o = NULL;
2470 gboolean mute = FALSE;
2475 pa_threaded_mainloop_lock (mainloop);
2478 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2479 if (pbuf == NULL || pbuf->stream == NULL)
2482 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2485 if (!(o = pa_context_get_sink_input_info (pbuf->context, idx,
2486 gst_pulsesink_sink_input_info_cb, pbuf)))
2489 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2490 pa_threaded_mainloop_wait (mainloop);
2491 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
2497 pa_operation_unref (o);
2499 pa_threaded_mainloop_unlock (mainloop);
2507 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2512 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2517 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2522 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2523 ("pa_context_get_sink_input_info() failed: %s",
2524 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2530 gst_pulsesink_device_description (GstPulseSink * psink)
2532 GstPulseRingBuffer *pbuf;
2533 pa_operation *o = NULL;
2539 pa_threaded_mainloop_lock (mainloop);
2540 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2544 if (!(o = pa_context_get_sink_info_by_name (pbuf->context,
2545 psink->device, gst_pulsesink_sink_info_cb, pbuf)))
2548 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2549 pa_threaded_mainloop_wait (mainloop);
2550 if (gst_pulsering_is_dead (psink, pbuf, FALSE))
2556 pa_operation_unref (o);
2558 t = g_strdup (psink->device_description);
2559 pa_threaded_mainloop_unlock (mainloop);
2566 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2571 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2576 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2577 ("pa_context_get_sink_info_by_index() failed: %s",
2578 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2584 gst_pulsesink_set_property (GObject * object,
2585 guint prop_id, const GValue * value, GParamSpec * pspec)
2587 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2591 g_free (pulsesink->server);
2592 pulsesink->server = g_value_dup_string (value);
2593 if (pulsesink->probe)
2594 gst_pulseprobe_set_server (pulsesink->probe, pulsesink->server);
2597 g_free (pulsesink->device);
2598 pulsesink->device = g_value_dup_string (value);
2601 gst_pulsesink_set_volume (pulsesink, g_value_get_double (value));
2604 gst_pulsesink_set_mute (pulsesink, g_value_get_boolean (value));
2607 g_free (pulsesink->client_name);
2608 if (!g_value_get_string (value)) {
2609 GST_WARNING_OBJECT (pulsesink,
2610 "Empty PulseAudio client name not allowed. Resetting to default value");
2611 pulsesink->client_name = gst_pulse_client_name ();
2613 pulsesink->client_name = g_value_dup_string (value);
2615 case PROP_STREAM_PROPERTIES:
2616 if (pulsesink->properties)
2617 gst_structure_free (pulsesink->properties);
2618 pulsesink->properties =
2619 gst_structure_copy (gst_value_get_structure (value));
2620 if (pulsesink->proplist)
2621 pa_proplist_free (pulsesink->proplist);
2622 pulsesink->proplist = gst_pulse_make_proplist (pulsesink->properties);
2625 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2631 gst_pulsesink_get_property (GObject * object,
2632 guint prop_id, GValue * value, GParamSpec * pspec)
2635 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2639 g_value_set_string (value, pulsesink->server);
2642 g_value_set_string (value, pulsesink->device);
2644 case PROP_DEVICE_NAME:
2645 g_value_take_string (value, gst_pulsesink_device_description (pulsesink));
2648 g_value_set_double (value, gst_pulsesink_get_volume (pulsesink));
2651 g_value_set_boolean (value, gst_pulsesink_get_mute (pulsesink));
2654 g_value_set_string (value, pulsesink->client_name);
2656 case PROP_STREAM_PROPERTIES:
2657 gst_value_set_structure (value, pulsesink->properties);
2660 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2666 gst_pulsesink_change_title (GstPulseSink * psink, const gchar * t)
2668 pa_operation *o = NULL;
2669 GstPulseRingBuffer *pbuf;
2671 pa_threaded_mainloop_lock (mainloop);
2673 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2675 if (pbuf == NULL || pbuf->stream == NULL)
2678 g_free (pbuf->stream_name);
2679 pbuf->stream_name = g_strdup (t);
2681 if (!(o = pa_stream_set_name (pbuf->stream, pbuf->stream_name, NULL, NULL)))
2684 /* We're not interested if this operation failed or not */
2688 pa_operation_unref (o);
2689 pa_threaded_mainloop_unlock (mainloop);
2696 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2701 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2702 ("pa_stream_set_name() failed: %s",
2703 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2709 gst_pulsesink_change_props (GstPulseSink * psink, GstTagList * l)
2711 static const gchar *const map[] = {
2712 GST_TAG_TITLE, PA_PROP_MEDIA_TITLE,
2714 /* might get overriden in the next iteration by GST_TAG_ARTIST */
2715 GST_TAG_PERFORMER, PA_PROP_MEDIA_ARTIST,
2717 GST_TAG_ARTIST, PA_PROP_MEDIA_ARTIST,
2718 GST_TAG_LANGUAGE_CODE, PA_PROP_MEDIA_LANGUAGE,
2719 GST_TAG_LOCATION, PA_PROP_MEDIA_FILENAME,
2720 /* We might add more here later on ... */
2723 pa_proplist *pl = NULL;
2724 const gchar *const *t;
2725 gboolean empty = TRUE;
2726 pa_operation *o = NULL;
2727 GstPulseRingBuffer *pbuf;
2729 pl = pa_proplist_new ();
2731 for (t = map; *t; t += 2) {
2734 if (gst_tag_list_get_string (l, *t, &n)) {
2737 pa_proplist_sets (pl, *(t + 1), n);
2747 pa_threaded_mainloop_lock (mainloop);
2748 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2749 if (pbuf == NULL || pbuf->stream == NULL)
2752 if (!(o = pa_stream_proplist_update (pbuf->stream, PA_UPDATE_REPLACE,
2756 /* We're not interested if this operation failed or not */
2760 pa_operation_unref (o);
2762 pa_threaded_mainloop_unlock (mainloop);
2767 pa_proplist_free (pl);
2774 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2779 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2780 ("pa_stream_proplist_update() failed: %s",
2781 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2787 gst_pulsesink_flush_ringbuffer (GstPulseSink * psink)
2789 GstPulseRingBuffer *pbuf;
2791 pa_threaded_mainloop_lock (mainloop);
2793 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2795 if (pbuf == NULL || pbuf->stream == NULL)
2798 gst_pulsering_flush (pbuf);
2800 /* Uncork if we haven't already (happens when waiting to get enough data
2801 * to send out the first time) */
2803 gst_pulsering_set_corked (pbuf, FALSE, FALSE);
2805 /* We're not interested if this operation failed or not */
2807 pa_threaded_mainloop_unlock (mainloop);
2814 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2820 gst_pulsesink_event (GstBaseSink * sink, GstEvent * event)
2822 GstPulseSink *pulsesink = GST_PULSESINK_CAST (sink);
2824 switch (GST_EVENT_TYPE (event)) {
2825 case GST_EVENT_TAG:{
2826 gchar *title = NULL, *artist = NULL, *location = NULL, *description =
2827 NULL, *t = NULL, *buf = NULL;
2830 gst_event_parse_tag (event, &l);
2832 gst_tag_list_get_string (l, GST_TAG_TITLE, &title);
2833 gst_tag_list_get_string (l, GST_TAG_ARTIST, &artist);
2834 gst_tag_list_get_string (l, GST_TAG_LOCATION, &location);
2835 gst_tag_list_get_string (l, GST_TAG_DESCRIPTION, &description);
2838 gst_tag_list_get_string (l, GST_TAG_PERFORMER, &artist);
2840 if (title && artist)
2841 /* TRANSLATORS: 'song title' by 'artist name' */
2842 t = buf = g_strdup_printf (_("'%s' by '%s'"), g_strstrip (title),
2843 g_strstrip (artist));
2845 t = g_strstrip (title);
2846 else if (description)
2847 t = g_strstrip (description);
2849 t = g_strstrip (location);
2852 gst_pulsesink_change_title (pulsesink, t);
2857 g_free (description);
2860 gst_pulsesink_change_props (pulsesink, l);
2865 gst_pulsesink_flush_ringbuffer (pulsesink);
2871 return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
2875 gst_pulsesink_release_mainloop (GstPulseSink * psink)
2880 pa_threaded_mainloop_lock (mainloop);
2881 while (psink->defer_pending) {
2882 GST_DEBUG_OBJECT (psink, "waiting for stream status message emission");
2883 pa_threaded_mainloop_wait (mainloop);
2885 pa_threaded_mainloop_unlock (mainloop);
2887 g_mutex_lock (pa_shared_resource_mutex);
2889 if (!mainloop_ref_ct) {
2890 GST_INFO_OBJECT (psink, "terminating pa main loop thread");
2891 pa_threaded_mainloop_stop (mainloop);
2892 pa_threaded_mainloop_free (mainloop);
2895 g_mutex_unlock (pa_shared_resource_mutex);
2898 static GstStateChangeReturn
2899 gst_pulsesink_change_state (GstElement * element, GstStateChange transition)
2901 GstPulseSink *pulsesink = GST_PULSESINK (element);
2902 GstStateChangeReturn ret;
2904 switch (transition) {
2905 case GST_STATE_CHANGE_NULL_TO_READY:
2906 g_mutex_lock (pa_shared_resource_mutex);
2907 if (!mainloop_ref_ct) {
2908 GST_INFO_OBJECT (element, "new pa main loop thread");
2909 if (!(mainloop = pa_threaded_mainloop_new ()))
2910 goto mainloop_failed;
2911 mainloop_ref_ct = 1;
2912 pa_threaded_mainloop_start (mainloop);
2913 g_mutex_unlock (pa_shared_resource_mutex);
2915 GST_INFO_OBJECT (element, "reusing pa main loop thread");
2917 g_mutex_unlock (pa_shared_resource_mutex);
2920 case GST_STATE_CHANGE_READY_TO_PAUSED:
2921 gst_element_post_message (element,
2922 gst_message_new_clock_provide (GST_OBJECT_CAST (element),
2923 GST_BASE_AUDIO_SINK (pulsesink)->provided_clock, TRUE));
2930 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2931 if (ret == GST_STATE_CHANGE_FAILURE)
2934 switch (transition) {
2935 case GST_STATE_CHANGE_PAUSED_TO_READY:
2936 /* format_lost is reset in release() in baseaudiosink */
2937 gst_element_post_message (element,
2938 gst_message_new_clock_lost (GST_OBJECT_CAST (element),
2939 GST_BASE_AUDIO_SINK (pulsesink)->provided_clock));
2941 case GST_STATE_CHANGE_READY_TO_NULL:
2942 gst_pulsesink_release_mainloop (pulsesink);
2953 g_mutex_unlock (pa_shared_resource_mutex);
2954 GST_ELEMENT_ERROR (pulsesink, RESOURCE, FAILED,
2955 ("pa_threaded_mainloop_new() failed"), (NULL));
2956 return GST_STATE_CHANGE_FAILURE;
2960 if (transition == GST_STATE_CHANGE_NULL_TO_READY) {
2961 /* Clear the PA mainloop if baseaudiosink failed to open the ring_buffer */
2962 g_assert (mainloop);
2963 gst_pulsesink_release_mainloop (pulsesink);