1 /*-*- Mode: C; c-basic-offset: 2 -*-*/
3 /* GStreamer pulseaudio plugin
5 * Copyright (c) 2004-2008 Lennart Poettering
8 * gst-pulse is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation; either version 2.1 of the
11 * License, or (at your option) any later version.
13 * gst-pulse is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with gst-pulse; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 * SECTION:element-pulsesink
26 * @see_also: pulsesrc, pulsemixer
28 * This element outputs audio to a
29 * <ulink href="http://www.pulseaudio.org">PulseAudio sound server</ulink>.
32 * <title>Example pipelines</title>
34 * gst-launch -v filesrc location=sine.ogg ! oggdemux ! vorbisdec ! audioconvert ! audioresample ! pulsesink
35 * ]| Play an Ogg/Vorbis file.
37 * gst-launch -v audiotestsrc ! audioconvert ! volume volume=0.4 ! pulsesink
38 * ]| Play a 440Hz sine wave.
49 #include <gst/base/gstbasesink.h>
50 #include <gst/gsttaglist.h>
51 #include <gst/interfaces/streamvolume.h>
52 #include <gst/gst-i18n-plugin.h>
54 #include "pulsesink.h"
55 #include "pulseutil.h"
57 GST_DEBUG_CATEGORY_EXTERN (pulse_debug);
58 #define GST_CAT_DEFAULT pulse_debug
61 * http://www.pulseaudio.org/ticket/314
62 * we need pulse-0.9.12 to use sink volume properties
65 #define DEFAULT_SERVER NULL
66 #define DEFAULT_DEVICE NULL
67 #define DEFAULT_DEVICE_NAME NULL
68 #define DEFAULT_VOLUME 1.0
69 #define DEFAULT_MUTE FALSE
70 #define MAX_VOLUME 10.0
83 #define GST_TYPE_PULSERING_BUFFER \
84 (gst_pulseringbuffer_get_type())
85 #define GST_PULSERING_BUFFER(obj) \
86 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_PULSERING_BUFFER,GstPulseRingBuffer))
87 #define GST_PULSERING_BUFFER_CLASS(klass) \
88 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_PULSERING_BUFFER,GstPulseRingBufferClass))
89 #define GST_PULSERING_BUFFER_GET_CLASS(obj) \
90 (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_PULSERING_BUFFER, GstPulseRingBufferClass))
91 #define GST_PULSERING_BUFFER_CAST(obj) \
92 ((GstPulseRingBuffer *)obj)
93 #define GST_IS_PULSERING_BUFFER(obj) \
94 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_PULSERING_BUFFER))
95 #define GST_IS_PULSERING_BUFFER_CLASS(klass)\
96 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_PULSERING_BUFFER))
98 typedef struct _GstPulseRingBuffer GstPulseRingBuffer;
99 typedef struct _GstPulseRingBufferClass GstPulseRingBufferClass;
101 /* We keep a custom ringbuffer that is backed up by data allocated by
102 * pulseaudio. We must also overide the commit function to write into
103 * pulseaudio memory instead. */
104 struct _GstPulseRingBuffer
106 GstRingBuffer object;
113 pa_sample_spec sample_spec;
116 gboolean in_commit:1;
120 struct _GstPulseRingBufferClass
122 GstRingBufferClass parent_class;
125 static void gst_pulseringbuffer_class_init (GstPulseRingBufferClass * klass);
126 static void gst_pulseringbuffer_init (GstPulseRingBuffer * ringbuffer,
127 GstPulseRingBufferClass * klass);
128 static void gst_pulseringbuffer_finalize (GObject * object);
130 static GstRingBufferClass *ring_parent_class = NULL;
132 static gboolean gst_pulseringbuffer_open_device (GstRingBuffer * buf);
133 static gboolean gst_pulseringbuffer_close_device (GstRingBuffer * buf);
134 static gboolean gst_pulseringbuffer_acquire (GstRingBuffer * buf,
135 GstRingBufferSpec * spec);
136 static gboolean gst_pulseringbuffer_release (GstRingBuffer * buf);
137 static gboolean gst_pulseringbuffer_start (GstRingBuffer * buf);
138 static gboolean gst_pulseringbuffer_pause (GstRingBuffer * buf);
139 static gboolean gst_pulseringbuffer_stop (GstRingBuffer * buf);
140 static void gst_pulseringbuffer_clear (GstRingBuffer * buf);
141 static guint gst_pulseringbuffer_commit (GstRingBuffer * buf,
142 guint64 * sample, guchar * data, gint in_samples, gint out_samples,
145 /* ringbuffer abstract base class */
147 gst_pulseringbuffer_get_type (void)
149 static GType ringbuffer_type = 0;
151 if (!ringbuffer_type) {
152 static const GTypeInfo ringbuffer_info = {
153 sizeof (GstPulseRingBufferClass),
156 (GClassInitFunc) gst_pulseringbuffer_class_init,
159 sizeof (GstPulseRingBuffer),
161 (GInstanceInitFunc) gst_pulseringbuffer_init,
166 g_type_register_static (GST_TYPE_RING_BUFFER, "GstPulseSinkRingBuffer",
167 &ringbuffer_info, 0);
169 return ringbuffer_type;
173 gst_pulseringbuffer_class_init (GstPulseRingBufferClass * klass)
175 GObjectClass *gobject_class;
176 GstRingBufferClass *gstringbuffer_class;
178 gobject_class = (GObjectClass *) klass;
179 gstringbuffer_class = (GstRingBufferClass *) klass;
181 ring_parent_class = g_type_class_peek_parent (klass);
183 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_finalize);
185 gstringbuffer_class->open_device =
186 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_open_device);
187 gstringbuffer_class->close_device =
188 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_close_device);
189 gstringbuffer_class->acquire =
190 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_acquire);
191 gstringbuffer_class->release =
192 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_release);
193 gstringbuffer_class->start = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_start);
194 gstringbuffer_class->pause = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_pause);
195 gstringbuffer_class->resume = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_start);
196 gstringbuffer_class->stop = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_stop);
197 gstringbuffer_class->clear_all =
198 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_clear);
200 gstringbuffer_class->commit = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_commit);
202 /* ref class from a thread-safe context to work around missing bit of
203 * thread-safety in GObject */
204 g_type_class_ref (GST_TYPE_PULSERING_BUFFER);
208 gst_pulseringbuffer_init (GstPulseRingBuffer * pbuf,
209 GstPulseRingBufferClass * g_class)
211 pbuf->stream_name = NULL;
212 pbuf->context = NULL;
215 #if HAVE_PULSE_0_9_13
216 pa_sample_spec_init (&pbuf->sample_spec);
218 pbuf->sample_spec.format = PA_SAMPLE_INVALID;
219 pbuf->sample_spec.rate = 0;
220 pbuf->sample_spec.channels = 0;
224 pbuf->in_commit = FALSE;
225 pbuf->paused = FALSE;
229 gst_pulsering_destroy_stream (GstPulseRingBuffer * pbuf)
232 pa_stream_disconnect (pbuf->stream);
234 /* Make sure we don't get any further callbacks */
235 pa_stream_set_state_callback (pbuf->stream, NULL, NULL);
236 pa_stream_set_write_callback (pbuf->stream, NULL, NULL);
237 pa_stream_set_underflow_callback (pbuf->stream, NULL, NULL);
238 pa_stream_set_overflow_callback (pbuf->stream, NULL, NULL);
240 pa_stream_unref (pbuf->stream);
244 g_free (pbuf->stream_name);
245 pbuf->stream_name = NULL;
249 gst_pulsering_destroy_context (GstPulseRingBuffer * pbuf)
251 gst_pulsering_destroy_stream (pbuf);
254 pa_context_disconnect (pbuf->context);
256 /* Make sure we don't get any further callbacks */
257 pa_context_set_state_callback (pbuf->context, NULL, NULL);
258 #if HAVE_PULSE_0_9_12
259 pa_context_set_subscribe_callback (pbuf->context, NULL, NULL);
262 pa_context_unref (pbuf->context);
263 pbuf->context = NULL;
268 gst_pulseringbuffer_finalize (GObject * object)
270 GstPulseRingBuffer *ringbuffer;
272 ringbuffer = GST_PULSERING_BUFFER_CAST (object);
274 gst_pulsering_destroy_context (ringbuffer);
276 G_OBJECT_CLASS (ring_parent_class)->finalize (object);
280 gst_pulsering_is_dead (GstPulseSink * psink, GstPulseRingBuffer * pbuf)
283 || !PA_CONTEXT_IS_GOOD (pa_context_get_state (pbuf->context))
285 || !PA_STREAM_IS_GOOD (pa_stream_get_state (pbuf->stream))) {
286 const gchar *err_str = pbuf->context ?
287 pa_strerror (pa_context_errno (pbuf->context)) : NULL;
289 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED, ("Disconnected: %s",
297 gst_pulsering_context_state_cb (pa_context * c, void *userdata)
300 GstPulseRingBuffer *pbuf;
301 pa_context_state_t state;
303 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
304 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
306 state = pa_context_get_state (c);
307 GST_LOG_OBJECT (psink, "got new context state %d", state);
309 /* psink can be null when we are shutting down and the ringbuffer is already
315 case PA_CONTEXT_READY:
316 case PA_CONTEXT_TERMINATED:
317 case PA_CONTEXT_FAILED:
318 GST_LOG_OBJECT (psink, "signaling");
319 pa_threaded_mainloop_signal (psink->mainloop, 0);
322 case PA_CONTEXT_UNCONNECTED:
323 case PA_CONTEXT_CONNECTING:
324 case PA_CONTEXT_AUTHORIZING:
325 case PA_CONTEXT_SETTING_NAME:
330 #if HAVE_PULSE_0_9_12
332 gst_pulsering_context_subscribe_cb (pa_context * c,
333 pa_subscription_event_type_t t, uint32_t idx, void *userdata)
336 GstPulseRingBuffer *pbuf;
338 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
339 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
341 GST_LOG_OBJECT (psink, "type %d, idx %u", t, idx);
343 if (t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT | PA_SUBSCRIPTION_EVENT_CHANGE) &&
344 t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT | PA_SUBSCRIPTION_EVENT_NEW))
350 if (idx != pa_stream_get_index (pbuf->stream))
353 /* Actually this event is also triggered when other properties of
354 * the stream change that are unrelated to the volume. However it is
355 * probably cheaper to signal the change here and check for the
356 * volume when the GObject property is read instead of querying it always. */
358 /* inform streaming thread to notify */
359 g_atomic_int_compare_and_exchange (&psink->notify, 0, 1);
363 /* will be called when the device should be opened. In this case we will connect
364 * to the server. We should not try to open any streams in this state. */
366 gst_pulseringbuffer_open_device (GstRingBuffer * buf)
369 GstPulseRingBuffer *pbuf;
371 pa_mainloop_api *api;
373 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
374 pbuf = GST_PULSERING_BUFFER_CAST (buf);
376 g_assert (!pbuf->context);
377 g_assert (!pbuf->stream);
379 name = gst_pulse_client_name ();
381 pa_threaded_mainloop_lock (psink->mainloop);
383 /* get the mainloop api and create a context */
384 GST_LOG_OBJECT (psink, "new context with name %s", GST_STR_NULL (name));
385 api = pa_threaded_mainloop_get_api (psink->mainloop);
386 if (!(pbuf->context = pa_context_new (api, name)))
389 /* register some essential callbacks */
390 pa_context_set_state_callback (pbuf->context,
391 gst_pulsering_context_state_cb, pbuf);
392 #if HAVE_PULSE_0_9_12
393 pa_context_set_subscribe_callback (pbuf->context,
394 gst_pulsering_context_subscribe_cb, pbuf);
397 /* try to connect to the server and wait for completioni, we don't want to
398 * autospawn a deamon */
399 GST_LOG_OBJECT (psink, "connect to server %s", GST_STR_NULL (psink->server));
400 if (pa_context_connect (pbuf->context, psink->server, PA_CONTEXT_NOAUTOSPAWN,
405 pa_context_state_t state;
407 state = pa_context_get_state (pbuf->context);
409 GST_LOG_OBJECT (psink, "context state is now %d", state);
411 if (!PA_CONTEXT_IS_GOOD (state))
414 if (state == PA_CONTEXT_READY)
417 /* Wait until the context is ready */
418 GST_LOG_OBJECT (psink, "waiting..");
419 pa_threaded_mainloop_wait (psink->mainloop);
422 GST_LOG_OBJECT (psink, "opened the device");
424 pa_threaded_mainloop_unlock (psink->mainloop);
432 gst_pulsering_destroy_context (pbuf);
434 pa_threaded_mainloop_unlock (psink->mainloop);
440 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
441 ("Failed to create context"), (NULL));
442 goto unlock_and_fail;
446 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED, ("Failed to connect: %s",
447 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
448 goto unlock_and_fail;
452 /* close the device */
454 gst_pulseringbuffer_close_device (GstRingBuffer * buf)
457 GstPulseRingBuffer *pbuf;
459 pbuf = GST_PULSERING_BUFFER_CAST (buf);
460 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
462 GST_LOG_OBJECT (psink, "closing device");
464 pa_threaded_mainloop_lock (psink->mainloop);
465 gst_pulsering_destroy_context (pbuf);
466 pa_threaded_mainloop_unlock (psink->mainloop);
468 GST_LOG_OBJECT (psink, "closed device");
474 gst_pulsering_stream_state_cb (pa_stream * s, void *userdata)
477 GstPulseRingBuffer *pbuf;
478 pa_stream_state_t state;
480 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
481 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
483 state = pa_stream_get_state (s);
484 GST_LOG_OBJECT (psink, "got new stream state %d", state);
487 case PA_STREAM_READY:
488 case PA_STREAM_FAILED:
489 case PA_STREAM_TERMINATED:
490 GST_LOG_OBJECT (psink, "signaling");
491 pa_threaded_mainloop_signal (psink->mainloop, 0);
493 case PA_STREAM_UNCONNECTED:
494 case PA_STREAM_CREATING:
500 gst_pulsering_stream_request_cb (pa_stream * s, size_t length, void *userdata)
504 GstPulseRingBuffer *pbuf;
506 rbuf = GST_RING_BUFFER_CAST (userdata);
507 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
508 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
510 GST_LOG_OBJECT (psink, "got request for length %" G_GSIZE_FORMAT, length);
512 if (pbuf->in_commit && (length >= rbuf->spec.segsize)) {
513 /* only signal when we are waiting in the commit thread
514 * and got request for atleast a segment */
515 pa_threaded_mainloop_signal (psink->mainloop, 0);
520 gst_pulsering_stream_underflow_cb (pa_stream * s, void *userdata)
523 GstPulseRingBuffer *pbuf;
525 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
526 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
528 GST_WARNING_OBJECT (psink, "Got underflow");
532 gst_pulsering_stream_overflow_cb (pa_stream * s, void *userdata)
535 GstPulseRingBuffer *pbuf;
537 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
538 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
540 GST_WARNING_OBJECT (psink, "Got overflow");
544 gst_pulsering_stream_latency_cb (pa_stream * s, void *userdata)
547 GstPulseRingBuffer *pbuf;
548 const pa_timing_info *info;
551 info = pa_stream_get_timing_info (s);
553 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
554 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
557 GST_LOG_OBJECT (psink, "latency update (information unknown)");
560 #if HAVE_PULSE_0_9_11
561 sink_usec = info->configured_sink_usec;
566 GST_LOG_OBJECT (psink,
567 "latency_update, %" G_GUINT64_FORMAT ", %d:%" G_GINT64_FORMAT ", %d:%"
568 G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT,
569 GST_TIMEVAL_TO_TIME (info->timestamp), info->write_index_corrupt,
570 info->write_index, info->read_index_corrupt, info->read_index,
571 info->sink_usec, sink_usec);
575 gst_pulsering_stream_suspended_cb (pa_stream * p, void *userdata)
578 GstPulseRingBuffer *pbuf;
580 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
581 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
583 if (pa_stream_is_suspended (p))
584 GST_DEBUG_OBJECT (psink, "stream suspended");
586 GST_DEBUG_OBJECT (psink, "stream resumed");
589 #if HAVE_PULSE_0_9_11
591 gst_pulsering_stream_started_cb (pa_stream * p, void *userdata)
594 GstPulseRingBuffer *pbuf;
596 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
597 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
599 GST_DEBUG_OBJECT (psink, "stream started");
603 #if HAVE_PULSE_0_9_15
605 gst_pulsering_stream_event_cb (pa_stream * p, const char *name,
606 pa_proplist * pl, void *userdata)
609 GstPulseRingBuffer *pbuf;
611 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
612 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
614 if (!strcmp (name, PA_STREAM_EVENT_REQUEST_CORK)) {
615 /* the stream wants to PAUSE, post a message for the application. */
616 GST_DEBUG_OBJECT (psink, "got request for CORK");
617 gst_element_post_message (GST_ELEMENT_CAST (psink),
618 gst_message_new_request_state (GST_OBJECT_CAST (psink),
621 } else if (!strcmp (name, PA_STREAM_EVENT_REQUEST_UNCORK)) {
622 GST_DEBUG_OBJECT (psink, "got request for UNCORK");
623 gst_element_post_message (GST_ELEMENT_CAST (psink),
624 gst_message_new_request_state (GST_OBJECT_CAST (psink),
627 GST_DEBUG_OBJECT (psink, "got unknown event %s", name);
632 /* This method should create a new stream of the given @spec. No playback should
633 * start yet so we start in the corked state. */
635 gst_pulseringbuffer_acquire (GstRingBuffer * buf, GstRingBufferSpec * spec)
638 GstPulseRingBuffer *pbuf;
639 pa_buffer_attr wanted;
640 const pa_buffer_attr *actual;
641 pa_channel_map channel_map;
642 pa_operation *o = NULL;
643 #if HAVE_PULSE_0_9_20
646 pa_cvolume *pv = NULL;
647 pa_stream_flags_t flags;
649 GstAudioClock *clock;
651 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
652 pbuf = GST_PULSERING_BUFFER_CAST (buf);
654 GST_LOG_OBJECT (psink, "creating sample spec");
655 /* convert the gstreamer sample spec to the pulseaudio format */
656 if (!gst_pulse_fill_sample_spec (spec, &pbuf->sample_spec))
659 pa_threaded_mainloop_lock (psink->mainloop);
661 /* we need a context and a no stream */
662 g_assert (pbuf->context);
663 g_assert (!pbuf->stream);
665 /* enable event notifications */
666 GST_LOG_OBJECT (psink, "subscribing to context events");
667 if (!(o = pa_context_subscribe (pbuf->context,
668 PA_SUBSCRIPTION_MASK_SINK_INPUT, NULL, NULL)))
669 goto subscribe_failed;
671 pa_operation_unref (o);
673 /* initialize the channel map */
674 gst_pulse_gst_to_channel_map (&channel_map, spec);
676 /* find a good name for the stream */
677 if (psink->stream_name)
678 name = psink->stream_name;
680 name = "Playback Stream";
682 /* create a stream */
683 GST_LOG_OBJECT (psink, "creating stream with name %s", name);
684 if (!(pbuf->stream = pa_stream_new (pbuf->context,
685 name, &pbuf->sample_spec, &channel_map)))
688 /* install essential callbacks */
689 pa_stream_set_state_callback (pbuf->stream,
690 gst_pulsering_stream_state_cb, pbuf);
691 pa_stream_set_write_callback (pbuf->stream,
692 gst_pulsering_stream_request_cb, pbuf);
693 pa_stream_set_underflow_callback (pbuf->stream,
694 gst_pulsering_stream_underflow_cb, pbuf);
695 pa_stream_set_overflow_callback (pbuf->stream,
696 gst_pulsering_stream_overflow_cb, pbuf);
697 pa_stream_set_latency_update_callback (pbuf->stream,
698 gst_pulsering_stream_latency_cb, pbuf);
699 pa_stream_set_suspended_callback (pbuf->stream,
700 gst_pulsering_stream_suspended_cb, pbuf);
701 #if HAVE_PULSE_0_9_11
702 pa_stream_set_started_callback (pbuf->stream,
703 gst_pulsering_stream_started_cb, pbuf);
705 #if HAVE_PULSE_0_9_15
706 pa_stream_set_event_callback (pbuf->stream,
707 gst_pulsering_stream_event_cb, pbuf);
710 /* buffering requirements. When setting prebuf to 0, the stream will not pause
711 * when we cause an underrun, which causes time to continue. */
712 memset (&wanted, 0, sizeof (wanted));
713 wanted.tlength = spec->segtotal * spec->segsize;
714 wanted.maxlength = -1;
716 wanted.minreq = spec->segsize;
718 GST_INFO_OBJECT (psink, "tlength: %d", wanted.tlength);
719 GST_INFO_OBJECT (psink, "maxlength: %d", wanted.maxlength);
720 GST_INFO_OBJECT (psink, "prebuf: %d", wanted.prebuf);
721 GST_INFO_OBJECT (psink, "minreq: %d", wanted.minreq);
723 #if HAVE_PULSE_0_9_20
724 /* configure volume when we changed it, else we leave the default */
725 if (psink->volume_set) {
726 GST_LOG_OBJECT (psink, "have volume of %f", psink->volume);
728 gst_pulse_cvolume_from_linear (pv, pbuf->sample_spec.channels,
735 /* construct the flags */
736 flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE |
737 #if HAVE_PULSE_0_9_11
738 PA_STREAM_ADJUST_LATENCY |
740 PA_STREAM_START_CORKED;
742 #if HAVE_PULSE_0_9_12
743 if (psink->mute_set && psink->mute)
744 flags |= PA_STREAM_START_MUTED;
747 /* we always start corked (see flags above) */
750 /* try to connect now */
751 GST_LOG_OBJECT (psink, "connect for playback to device %s",
752 GST_STR_NULL (psink->device));
753 if (pa_stream_connect_playback (pbuf->stream, psink->device,
754 &wanted, flags, pv, NULL) < 0)
757 /* our clock will now start from 0 again */
758 clock = GST_AUDIO_CLOCK (GST_BASE_AUDIO_SINK (psink)->provided_clock);
759 gst_audio_clock_reset (clock, 0);
762 pa_stream_state_t state;
764 state = pa_stream_get_state (pbuf->stream);
766 GST_LOG_OBJECT (psink, "stream state is now %d", state);
768 if (!PA_STREAM_IS_GOOD (state))
771 if (state == PA_STREAM_READY)
774 /* Wait until the stream is ready */
775 pa_threaded_mainloop_wait (psink->mainloop);
778 /* After we passed the volume off of to PA we never want to set it
779 again, since it is PA's job to save/restore volumes. */
780 psink->volume_set = psink->mute_set = FALSE;
782 GST_LOG_OBJECT (psink, "stream is acquired now");
784 /* get the actual buffering properties now */
785 actual = pa_stream_get_buffer_attr (pbuf->stream);
787 GST_INFO_OBJECT (psink, "tlength: %d (wanted: %d)", actual->tlength,
789 GST_INFO_OBJECT (psink, "maxlength: %d", actual->maxlength);
790 GST_INFO_OBJECT (psink, "prebuf: %d", actual->prebuf);
791 GST_INFO_OBJECT (psink, "minreq: %d (wanted %d)", actual->minreq,
794 spec->segsize = actual->minreq;
795 spec->segtotal = actual->tlength / spec->segsize;
797 pa_threaded_mainloop_unlock (psink->mainloop);
804 gst_pulsering_destroy_stream (pbuf);
805 pa_threaded_mainloop_unlock (psink->mainloop);
811 GST_ELEMENT_ERROR (psink, RESOURCE, SETTINGS,
812 ("Invalid sample specification."), (NULL));
817 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
818 ("pa_context_subscribe() failed: %s",
819 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
820 goto unlock_and_fail;
824 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
825 ("Failed to create stream: %s",
826 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
827 goto unlock_and_fail;
831 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
832 ("Failed to connect stream: %s",
833 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
834 goto unlock_and_fail;
838 /* free the stream that we acquired before */
840 gst_pulseringbuffer_release (GstRingBuffer * buf)
843 GstPulseRingBuffer *pbuf;
845 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
846 pbuf = GST_PULSERING_BUFFER_CAST (buf);
848 pa_threaded_mainloop_lock (psink->mainloop);
849 gst_pulsering_destroy_stream (pbuf);
850 pa_threaded_mainloop_unlock (psink->mainloop);
856 gst_pulsering_success_cb (pa_stream * s, int success, void *userdata)
858 GstPulseRingBuffer *pbuf;
861 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
862 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
864 pa_threaded_mainloop_signal (psink->mainloop, 0);
867 /* update the corked state of a stream, must be called with the mainloop
870 gst_pulsering_set_corked (GstPulseRingBuffer * pbuf, gboolean corked,
873 pa_operation *o = NULL;
875 gboolean res = FALSE;
877 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
879 GST_DEBUG_OBJECT (psink, "setting corked state to %d", corked);
880 if (pbuf->corked != corked) {
881 if (!(o = pa_stream_cork (pbuf->stream, corked,
882 gst_pulsering_success_cb, pbuf)))
885 while (wait && pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
886 pa_threaded_mainloop_wait (psink->mainloop);
887 if (gst_pulsering_is_dead (psink, pbuf))
890 pbuf->corked = corked;
892 GST_DEBUG_OBJECT (psink, "skipping, already in requested state");
898 pa_operation_unref (o);
905 GST_DEBUG_OBJECT (psink, "the server is dead");
910 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
911 ("pa_stream_cork() failed: %s",
912 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
918 gst_pulseringbuffer_clear (GstRingBuffer * buf)
921 GstPulseRingBuffer *pbuf;
922 pa_operation *o = NULL;
924 pbuf = GST_PULSERING_BUFFER_CAST (buf);
925 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
927 pa_threaded_mainloop_lock (psink->mainloop);
928 GST_DEBUG_OBJECT (psink, "clearing");
930 /* don't wait for the flush to complete */
931 if ((o = pa_stream_flush (pbuf->stream, NULL, pbuf)))
932 pa_operation_unref (o);
934 pa_threaded_mainloop_unlock (psink->mainloop);
938 mainloop_enter_defer_cb (pa_mainloop_api * api, void *userdata)
940 GstPulseSink *pulsesink = GST_PULSESINK (userdata);
944 g_value_init (&val, G_TYPE_POINTER);
945 g_value_set_pointer (&val, g_thread_self ());
947 GST_DEBUG_OBJECT (pulsesink, "posting ENTER stream status");
948 message = gst_message_new_stream_status (GST_OBJECT (pulsesink),
949 GST_STREAM_STATUS_TYPE_ENTER, GST_ELEMENT (pulsesink));
950 gst_message_set_stream_status_object (message, &val);
952 gst_element_post_message (GST_ELEMENT (pulsesink), message);
954 /* signal the waiter */
955 pulsesink->pa_defer_ran = TRUE;
956 pa_threaded_mainloop_signal (pulsesink->mainloop, 0);
959 /* start/resume playback ASAP, we don't uncork here but in the commit method */
961 gst_pulseringbuffer_start (GstRingBuffer * buf)
964 GstPulseRingBuffer *pbuf;
966 pbuf = GST_PULSERING_BUFFER_CAST (buf);
967 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
969 pa_threaded_mainloop_lock (psink->mainloop);
971 GST_DEBUG_OBJECT (psink, "scheduling stream status");
972 psink->pa_defer_ran = FALSE;
973 pa_mainloop_api_once (pa_threaded_mainloop_get_api (psink->mainloop),
974 mainloop_enter_defer_cb, psink);
976 GST_DEBUG_OBJECT (psink, "starting");
977 pbuf->paused = FALSE;
978 gst_pulsering_set_corked (pbuf, FALSE, FALSE);
979 pa_threaded_mainloop_unlock (psink->mainloop);
984 /* pause/stop playback ASAP */
986 gst_pulseringbuffer_pause (GstRingBuffer * buf)
989 GstPulseRingBuffer *pbuf;
992 pbuf = GST_PULSERING_BUFFER_CAST (buf);
993 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
995 pa_threaded_mainloop_lock (psink->mainloop);
996 GST_DEBUG_OBJECT (psink, "pausing and corking");
997 /* make sure the commit method stops writing */
999 res = gst_pulsering_set_corked (pbuf, TRUE, FALSE);
1000 if (pbuf->in_commit) {
1001 /* we are waiting in a commit, signal */
1002 GST_DEBUG_OBJECT (psink, "signal commit");
1003 pa_threaded_mainloop_signal (psink->mainloop, 0);
1005 pa_threaded_mainloop_unlock (psink->mainloop);
1011 mainloop_leave_defer_cb (pa_mainloop_api * api, void *userdata)
1013 GstPulseSink *pulsesink = GST_PULSESINK (userdata);
1014 GstMessage *message;
1017 g_value_init (&val, G_TYPE_POINTER);
1018 g_value_set_pointer (&val, g_thread_self ());
1020 GST_DEBUG_OBJECT (pulsesink, "posting LEAVE stream status");
1021 message = gst_message_new_stream_status (GST_OBJECT (pulsesink),
1022 GST_STREAM_STATUS_TYPE_LEAVE, GST_ELEMENT (pulsesink));
1023 gst_message_set_stream_status_object (message, &val);
1024 gst_element_post_message (GST_ELEMENT (pulsesink), message);
1026 pulsesink->pa_defer_ran = TRUE;
1027 pa_threaded_mainloop_signal (pulsesink->mainloop, 0);
1030 /* stop playback, we flush everything. */
1032 gst_pulseringbuffer_stop (GstRingBuffer * buf)
1034 GstPulseSink *psink;
1035 GstPulseRingBuffer *pbuf;
1036 gboolean res = FALSE;
1037 pa_operation *o = NULL;
1039 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1040 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1042 pa_threaded_mainloop_lock (psink->mainloop);
1043 pbuf->paused = TRUE;
1044 res = gst_pulsering_set_corked (pbuf, TRUE, TRUE);
1045 /* Inform anyone waiting in _commit() call that it shall wakeup */
1046 if (pbuf->in_commit) {
1047 GST_DEBUG_OBJECT (psink, "signal commit thread");
1048 pa_threaded_mainloop_signal (psink->mainloop, 0);
1051 if (strcmp (psink->pa_version, "0.9.12")) {
1052 /* then try to flush, it's not fatal when this fails */
1053 GST_DEBUG_OBJECT (psink, "flushing");
1054 if ((o = pa_stream_flush (pbuf->stream, gst_pulsering_success_cb, pbuf))) {
1055 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1056 GST_DEBUG_OBJECT (psink, "wait for completion");
1057 pa_threaded_mainloop_wait (psink->mainloop);
1058 if (gst_pulsering_is_dead (psink, pbuf))
1061 GST_DEBUG_OBJECT (psink, "flush completed");
1068 pa_operation_cancel (o);
1069 pa_operation_unref (o);
1072 GST_DEBUG_OBJECT (psink, "scheduling stream status");
1073 psink->pa_defer_ran = FALSE;
1074 pa_mainloop_api_once (pa_threaded_mainloop_get_api (psink->mainloop),
1075 mainloop_leave_defer_cb, psink);
1077 GST_DEBUG_OBJECT (psink, "waiting for stream status");
1078 pa_threaded_mainloop_unlock (psink->mainloop);
1085 GST_DEBUG_OBJECT (psink, "the server is dead");
1090 /* in_samples >= out_samples, rate > 1.0 */
1091 #define FWD_UP_SAMPLES(s,se,d,de) \
1093 guint8 *sb = s, *db = d; \
1094 while (s <= se && d < de) { \
1095 memcpy (d, s, bps); \
1098 if ((*accum << 1) >= inr) { \
1103 in_samples -= (s - sb)/bps; \
1104 out_samples -= (d - db)/bps; \
1105 GST_DEBUG ("fwd_up end %d/%d",*accum,*toprocess); \
1108 /* out_samples > in_samples, for rates smaller than 1.0 */
1109 #define FWD_DOWN_SAMPLES(s,se,d,de) \
1111 guint8 *sb = s, *db = d; \
1112 while (s <= se && d < de) { \
1113 memcpy (d, s, bps); \
1116 if ((*accum << 1) >= outr) { \
1121 in_samples -= (s - sb)/bps; \
1122 out_samples -= (d - db)/bps; \
1123 GST_DEBUG ("fwd_down end %d/%d",*accum,*toprocess); \
1126 #define REV_UP_SAMPLES(s,se,d,de) \
1128 guint8 *sb = se, *db = d; \
1129 while (s <= se && d < de) { \
1130 memcpy (d, se, bps); \
1133 while (d < de && (*accum << 1) >= inr) { \
1138 in_samples -= (sb - se)/bps; \
1139 out_samples -= (d - db)/bps; \
1140 GST_DEBUG ("rev_up end %d/%d",*accum,*toprocess); \
1143 #define REV_DOWN_SAMPLES(s,se,d,de) \
1145 guint8 *sb = se, *db = d; \
1146 while (s <= se && d < de) { \
1147 memcpy (d, se, bps); \
1150 while (s <= se && (*accum << 1) >= outr) { \
1155 in_samples -= (sb - se)/bps; \
1156 out_samples -= (d - db)/bps; \
1157 GST_DEBUG ("rev_down end %d/%d",*accum,*toprocess); \
1161 /* our custom commit function because we write into the buffer of pulseaudio
1162 * instead of keeping our own buffer */
1164 gst_pulseringbuffer_commit (GstRingBuffer * buf, guint64 * sample,
1165 guchar * data, gint in_samples, gint out_samples, gint * accum)
1167 GstPulseSink *psink;
1168 GstPulseRingBuffer *pbuf;
1173 gint inr, outr, bps;
1177 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1178 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1180 /* FIXME post message rather than using a signal (as mixer interface) */
1181 if (g_atomic_int_compare_and_exchange (&psink->notify, 1, 0)) {
1182 g_object_notify (G_OBJECT (psink), "volume");
1183 g_object_notify (G_OBJECT (psink), "mute");
1186 /* make sure the ringbuffer is started */
1187 if (G_UNLIKELY (g_atomic_int_get (&buf->state) !=
1188 GST_RING_BUFFER_STATE_STARTED)) {
1189 /* see if we are allowed to start it */
1190 if (G_UNLIKELY (g_atomic_int_get (&buf->abidata.ABI.may_start) == FALSE))
1193 GST_DEBUG_OBJECT (buf, "start!");
1194 if (!gst_ring_buffer_start (buf))
1198 pa_threaded_mainloop_lock (psink->mainloop);
1199 GST_DEBUG_OBJECT (psink, "entering commit");
1200 pbuf->in_commit = TRUE;
1202 bps = buf->spec.bytes_per_sample;
1203 bufsize = buf->spec.segsize * buf->spec.segtotal;
1205 /* our toy resampler for trick modes */
1206 reverse = out_samples < 0;
1207 out_samples = ABS (out_samples);
1209 if (in_samples >= out_samples)
1210 toprocess = &in_samples;
1212 toprocess = &out_samples;
1214 inr = in_samples - 1;
1215 outr = out_samples - 1;
1217 GST_DEBUG_OBJECT (psink, "in %d, out %d", inr, outr);
1219 /* data_end points to the last sample we have to write, not past it. This is
1220 * needed to properly handle reverse playback: it points to the last sample. */
1221 data_end = data + (bps * inr);
1226 /* offset is in bytes */
1227 offset = *sample * bps;
1229 while (*toprocess > 0) {
1233 GST_LOG_OBJECT (psink,
1234 "need to write %d samples at offset %" G_GINT64_FORMAT, *toprocess,
1238 /* FIXME, this is not quite right */
1239 if ((avail = pa_stream_writable_size (pbuf->stream)) == (size_t) - 1)
1240 goto writable_size_failed;
1242 /* We always try to satisfy a request for data */
1243 GST_LOG_OBJECT (psink, "writable bytes %" G_GSIZE_FORMAT, avail);
1245 /* convert to samples, we can only deal with multiples of the
1252 /* see if we need to uncork because we have no free space */
1254 if (!gst_pulsering_set_corked (pbuf, FALSE, FALSE))
1258 /* we can't write a single byte, wait a bit */
1259 GST_LOG_OBJECT (psink, "waiting for free space");
1260 pa_threaded_mainloop_wait (psink->mainloop);
1266 if (avail > out_samples)
1267 avail = out_samples;
1269 towrite = avail * bps;
1271 GST_LOG_OBJECT (psink, "writing %u samples at offset %" G_GUINT64_FORMAT,
1272 (guint) avail, offset);
1274 if (G_LIKELY (inr == outr && !reverse)) {
1275 /* no rate conversion, simply write out the samples */
1276 if (pa_stream_write (pbuf->stream, data, towrite, NULL, offset,
1277 PA_SEEK_ABSOLUTE) < 0)
1281 in_samples -= avail;
1282 out_samples -= avail;
1284 guint8 *dest, *d, *d_end;
1286 /* we need to allocate a temporary buffer to resample the data into,
1287 * FIXME, we should have a pulseaudio API to allocate this buffer for us
1288 * from the shared memory. */
1289 dest = d = g_malloc (towrite);
1290 d_end = d + towrite;
1294 /* forward speed up */
1295 FWD_UP_SAMPLES (data, data_end, d, d_end);
1297 /* forward slow down */
1298 FWD_DOWN_SAMPLES (data, data_end, d, d_end);
1301 /* reverse speed up */
1302 REV_UP_SAMPLES (data, data_end, d, d_end);
1304 /* reverse slow down */
1305 REV_DOWN_SAMPLES (data, data_end, d, d_end);
1307 /* see what we have left to write */
1308 towrite = (d - dest);
1309 if (pa_stream_write (pbuf->stream, dest, towrite,
1310 g_free, offset, PA_SEEK_ABSOLUTE) < 0)
1313 avail = towrite / bps;
1316 offset += avail * bps;
1318 /* check if we need to uncork after writing the samples */
1320 const pa_timing_info *info;
1322 if ((info = pa_stream_get_timing_info (pbuf->stream))) {
1323 GST_LOG_OBJECT (psink,
1324 "read_index at %" G_GUINT64_FORMAT ", offset %" G_GINT64_FORMAT,
1325 info->read_index, offset);
1327 /* we uncork when the read_index is too far behind the offset we need
1329 if (info->read_index + bufsize <= offset) {
1330 if (!gst_pulsering_set_corked (pbuf, FALSE, FALSE))
1334 GST_LOG_OBJECT (psink, "no timing info available yet");
1338 /* we consumed all samples here */
1339 data = data_end + bps;
1341 pbuf->in_commit = FALSE;
1342 pa_threaded_mainloop_unlock (psink->mainloop);
1345 result = inr - ((data_end - data) / bps);
1346 GST_LOG_OBJECT (psink, "wrote %d samples", result);
1353 pbuf->in_commit = FALSE;
1354 GST_LOG_OBJECT (psink, "we are reset");
1355 pa_threaded_mainloop_unlock (psink->mainloop);
1360 GST_LOG_OBJECT (psink, "we can not start");
1365 GST_LOG_OBJECT (psink, "failed to start the ringbuffer");
1370 pbuf->in_commit = FALSE;
1371 GST_ERROR_OBJECT (psink, "uncork failed");
1372 pa_threaded_mainloop_unlock (psink->mainloop);
1377 pbuf->in_commit = FALSE;
1378 GST_LOG_OBJECT (psink, "we are paused");
1379 pa_threaded_mainloop_unlock (psink->mainloop);
1382 writable_size_failed:
1384 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1385 ("pa_stream_writable_size() failed: %s",
1386 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1387 goto unlock_and_fail;
1391 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1392 ("pa_stream_write() failed: %s",
1393 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1394 goto unlock_and_fail;
1398 static void gst_pulsesink_set_property (GObject * object, guint prop_id,
1399 const GValue * value, GParamSpec * pspec);
1400 static void gst_pulsesink_get_property (GObject * object, guint prop_id,
1401 GValue * value, GParamSpec * pspec);
1402 static void gst_pulsesink_finalize (GObject * object);
1404 static gboolean gst_pulsesink_event (GstBaseSink * sink, GstEvent * event);
1406 static void gst_pulsesink_init_interfaces (GType type);
1408 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
1409 # define ENDIANNESS "LITTLE_ENDIAN, BIG_ENDIAN"
1411 # define ENDIANNESS "BIG_ENDIAN, LITTLE_ENDIAN"
1414 GST_IMPLEMENT_PULSEPROBE_METHODS (GstPulseSink, gst_pulsesink);
1415 GST_BOILERPLATE_FULL (GstPulseSink, gst_pulsesink, GstBaseAudioSink,
1416 GST_TYPE_BASE_AUDIO_SINK, gst_pulsesink_init_interfaces);
1419 gst_pulsesink_interface_supported (GstImplementsInterface *
1420 iface, GType interface_type)
1422 GstPulseSink *this = GST_PULSESINK_CAST (iface);
1424 if (interface_type == GST_TYPE_PROPERTY_PROBE && this->probe)
1426 if (interface_type == GST_TYPE_STREAM_VOLUME)
1433 gst_pulsesink_implements_interface_init (GstImplementsInterfaceClass * klass)
1435 klass->supported = gst_pulsesink_interface_supported;
1439 gst_pulsesink_init_interfaces (GType type)
1441 static const GInterfaceInfo implements_iface_info = {
1442 (GInterfaceInitFunc) gst_pulsesink_implements_interface_init,
1446 static const GInterfaceInfo probe_iface_info = {
1447 (GInterfaceInitFunc) gst_pulsesink_property_probe_interface_init,
1451 #if HAVE_PULSE_0_9_12
1452 static const GInterfaceInfo svol_iface_info = {
1456 g_type_add_interface_static (type, GST_TYPE_STREAM_VOLUME, &svol_iface_info);
1459 g_type_add_interface_static (type, GST_TYPE_IMPLEMENTS_INTERFACE,
1460 &implements_iface_info);
1461 g_type_add_interface_static (type, GST_TYPE_PROPERTY_PROBE,
1466 gst_pulsesink_base_init (gpointer g_class)
1468 static GstStaticPadTemplate pad_template = GST_STATIC_PAD_TEMPLATE ("sink",
1471 GST_STATIC_CAPS ("audio/x-raw-int, "
1472 "endianness = (int) { " ENDIANNESS " }, "
1473 "signed = (boolean) TRUE, "
1474 "width = (int) 16, "
1475 "depth = (int) 16, "
1476 "rate = (int) [ 1, MAX ], "
1477 "channels = (int) [ 1, 32 ];"
1478 "audio/x-raw-float, "
1479 "endianness = (int) { " ENDIANNESS " }, "
1480 "width = (int) 32, "
1481 "rate = (int) [ 1, MAX ], "
1482 "channels = (int) [ 1, 32 ];"
1484 "endianness = (int) { " ENDIANNESS " }, "
1485 "signed = (boolean) TRUE, "
1486 "width = (int) 32, "
1487 "depth = (int) 32, "
1488 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 32 ];"
1489 #if HAVE_PULSE_0_9_15
1491 "endianness = (int) { " ENDIANNESS " }, "
1492 "signed = (boolean) TRUE, "
1493 "width = (int) 24, "
1494 "depth = (int) 24, "
1495 "rate = (int) [ 1, MAX ], "
1496 "channels = (int) [ 1, 32 ];"
1498 "endianness = (int) { " ENDIANNESS " }, "
1499 "signed = (boolean) TRUE, "
1500 "width = (int) 32, "
1501 "depth = (int) 24, "
1502 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 32 ];"
1505 "signed = (boolean) FALSE, "
1508 "rate = (int) [ 1, MAX ], "
1509 "channels = (int) [ 1, 32 ];"
1511 "rate = (int) [ 1, MAX], "
1512 "channels = (int) [ 1, 32 ];"
1514 "rate = (int) [ 1, MAX], " "channels = (int) [ 1, 32 ]")
1517 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
1519 gst_element_class_set_details_simple (element_class,
1520 "PulseAudio Audio Sink",
1521 "Sink/Audio", "Plays audio to a PulseAudio server", "Lennart Poettering");
1522 gst_element_class_add_pad_template (element_class,
1523 gst_static_pad_template_get (&pad_template));
1526 static GstRingBuffer *
1527 gst_pulsesink_create_ringbuffer (GstBaseAudioSink * sink)
1529 GstRingBuffer *buffer;
1531 GST_DEBUG_OBJECT (sink, "creating ringbuffer");
1532 buffer = g_object_new (GST_TYPE_PULSERING_BUFFER, NULL);
1533 GST_DEBUG_OBJECT (sink, "created ringbuffer @%p", buffer);
1539 gst_pulsesink_class_init (GstPulseSinkClass * klass)
1541 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1542 GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
1543 GstBaseSinkClass *bc;
1544 GstBaseAudioSinkClass *gstaudiosink_class = GST_BASE_AUDIO_SINK_CLASS (klass);
1546 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_pulsesink_finalize);
1547 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_pulsesink_set_property);
1548 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_pulsesink_get_property);
1550 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_pulsesink_event);
1552 /* restore the original basesink pull methods */
1553 bc = g_type_class_peek (GST_TYPE_BASE_SINK);
1554 gstbasesink_class->activate_pull = GST_DEBUG_FUNCPTR (bc->activate_pull);
1556 gstaudiosink_class->create_ringbuffer =
1557 GST_DEBUG_FUNCPTR (gst_pulsesink_create_ringbuffer);
1559 /* Overwrite GObject fields */
1560 g_object_class_install_property (gobject_class,
1562 g_param_spec_string ("server", "Server",
1563 "The PulseAudio server to connect to", DEFAULT_SERVER,
1564 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1566 g_object_class_install_property (gobject_class, PROP_DEVICE,
1567 g_param_spec_string ("device", "Device",
1568 "The PulseAudio sink device to connect to", DEFAULT_DEVICE,
1569 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1571 g_object_class_install_property (gobject_class,
1573 g_param_spec_string ("device-name", "Device name",
1574 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
1575 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1577 #if HAVE_PULSE_0_9_12
1578 g_object_class_install_property (gobject_class,
1580 g_param_spec_double ("volume", "Volume",
1581 "Linear volume of this stream, 1.0=100%", 0.0, MAX_VOLUME,
1582 DEFAULT_VOLUME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1583 g_object_class_install_property (gobject_class,
1585 g_param_spec_boolean ("mute", "Mute",
1586 "Mute state of this stream", DEFAULT_MUTE,
1587 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1591 /* returns the current time of the sink ringbuffer */
1593 gst_pulsesink_get_time (GstClock * clock, GstBaseAudioSink * sink)
1595 GstPulseSink *psink;
1596 GstPulseRingBuffer *pbuf;
1599 if (!sink->ringbuffer || !sink->ringbuffer->acquired)
1600 return GST_CLOCK_TIME_NONE;
1602 pbuf = GST_PULSERING_BUFFER_CAST (sink->ringbuffer);
1603 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1605 pa_threaded_mainloop_lock (psink->mainloop);
1606 if (gst_pulsering_is_dead (psink, pbuf))
1609 /* if we don't have enough data to get a timestamp, just return NONE, which
1610 * will return the last reported time */
1611 if (pa_stream_get_time (pbuf->stream, &time) < 0) {
1612 GST_DEBUG_OBJECT (psink, "could not get time");
1613 time = GST_CLOCK_TIME_NONE;
1616 pa_threaded_mainloop_unlock (psink->mainloop);
1618 GST_LOG_OBJECT (psink, "current time is %" GST_TIME_FORMAT,
1619 GST_TIME_ARGS (time));
1626 GST_DEBUG_OBJECT (psink, "the server is dead");
1627 pa_threaded_mainloop_unlock (psink->mainloop);
1629 return GST_CLOCK_TIME_NONE;
1634 gst_pulsesink_init (GstPulseSink * pulsesink, GstPulseSinkClass * klass)
1638 pulsesink->server = NULL;
1639 pulsesink->device = NULL;
1640 pulsesink->device_description = NULL;
1642 pulsesink->volume = DEFAULT_VOLUME;
1643 pulsesink->volume_set = FALSE;
1645 pulsesink->mute = DEFAULT_MUTE;
1646 pulsesink->mute_set = FALSE;
1648 pulsesink->notify = 0;
1650 /* needed for conditional execution */
1651 pulsesink->pa_version = pa_get_library_version ();
1653 GST_DEBUG_OBJECT (pulsesink, "using pulseaudio version %s",
1654 pulsesink->pa_version);
1656 pulsesink->mainloop = pa_threaded_mainloop_new ();
1657 g_assert (pulsesink->mainloop != NULL);
1658 res = pa_threaded_mainloop_start (pulsesink->mainloop);
1659 g_assert (res == 0);
1661 /* TRUE for sinks, FALSE for sources */
1662 pulsesink->probe = gst_pulseprobe_new (G_OBJECT (pulsesink),
1663 G_OBJECT_GET_CLASS (pulsesink), PROP_DEVICE, pulsesink->device,
1666 /* override with a custom clock */
1667 if (GST_BASE_AUDIO_SINK (pulsesink)->provided_clock)
1668 gst_object_unref (GST_BASE_AUDIO_SINK (pulsesink)->provided_clock);
1669 GST_BASE_AUDIO_SINK (pulsesink)->provided_clock =
1670 gst_audio_clock_new ("GstPulseSinkClock",
1671 (GstAudioClockGetTimeFunc) gst_pulsesink_get_time, pulsesink);
1675 gst_pulsesink_finalize (GObject * object)
1677 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
1679 pa_threaded_mainloop_stop (pulsesink->mainloop);
1681 g_free (pulsesink->server);
1682 g_free (pulsesink->device);
1683 g_free (pulsesink->device_description);
1685 pa_threaded_mainloop_free (pulsesink->mainloop);
1687 if (pulsesink->probe) {
1688 gst_pulseprobe_free (pulsesink->probe);
1689 pulsesink->probe = NULL;
1692 G_OBJECT_CLASS (parent_class)->finalize (object);
1695 #if HAVE_PULSE_0_9_12
1697 gst_pulsesink_set_volume (GstPulseSink * psink, gdouble volume)
1700 pa_operation *o = NULL;
1701 GstPulseRingBuffer *pbuf;
1704 pa_threaded_mainloop_lock (psink->mainloop);
1706 GST_DEBUG_OBJECT (psink, "setting volume to %f", volume);
1708 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
1709 if (pbuf == NULL || pbuf->stream == NULL)
1712 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
1715 gst_pulse_cvolume_from_linear (&v, pbuf->sample_spec.channels, volume);
1717 if (!(o = pa_context_set_sink_input_volume (pbuf->context, idx,
1721 /* We don't really care about the result of this call */
1725 pa_operation_unref (o);
1727 pa_threaded_mainloop_unlock (psink->mainloop);
1734 psink->volume = volume;
1735 psink->volume_set = TRUE;
1737 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
1742 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
1747 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1748 ("pa_stream_set_sink_input_volume() failed: %s",
1749 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1755 gst_pulsesink_set_mute (GstPulseSink * psink, gboolean mute)
1757 pa_operation *o = NULL;
1758 GstPulseRingBuffer *pbuf;
1761 pa_threaded_mainloop_lock (psink->mainloop);
1763 GST_DEBUG_OBJECT (psink, "setting mute state to %d", mute);
1765 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
1766 if (pbuf == NULL || pbuf->stream == NULL)
1769 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
1772 if (!(o = pa_context_set_sink_input_mute (pbuf->context, idx,
1776 /* We don't really care about the result of this call */
1780 pa_operation_unref (o);
1782 pa_threaded_mainloop_unlock (psink->mainloop);
1790 psink->mute_set = TRUE;
1792 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
1797 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
1802 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1803 ("pa_stream_set_sink_input_mute() failed: %s",
1804 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1810 gst_pulsesink_sink_input_info_cb (pa_context * c, const pa_sink_input_info * i,
1811 int eol, void *userdata)
1813 GstPulseRingBuffer *pbuf;
1814 GstPulseSink *psink;
1816 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
1817 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1825 /* If the index doesn't match our current stream,
1826 * it implies we just recreated the stream (caps change)
1828 if (i->index == pa_stream_get_index (pbuf->stream)) {
1829 psink->volume = pa_sw_volume_to_linear (pa_cvolume_max (&i->volume));
1830 psink->mute = i->mute;
1834 pa_threaded_mainloop_signal (psink->mainloop, 0);
1838 gst_pulsesink_get_volume (GstPulseSink * psink)
1840 GstPulseRingBuffer *pbuf;
1841 pa_operation *o = NULL;
1842 gdouble v = DEFAULT_VOLUME;
1845 pa_threaded_mainloop_lock (psink->mainloop);
1847 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
1848 if (pbuf == NULL || pbuf->stream == NULL)
1851 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
1854 if (!(o = pa_context_get_sink_input_info (pbuf->context, idx,
1855 gst_pulsesink_sink_input_info_cb, pbuf)))
1858 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1859 pa_threaded_mainloop_wait (psink->mainloop);
1860 if (gst_pulsering_is_dead (psink, pbuf))
1868 pa_operation_unref (o);
1870 pa_threaded_mainloop_unlock (psink->mainloop);
1872 if (v > MAX_VOLUME) {
1873 GST_WARNING_OBJECT (psink, "Clipped volume from %f to %f", v, MAX_VOLUME);
1882 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
1887 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
1892 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1893 ("pa_context_get_sink_input_info() failed: %s",
1894 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1900 gst_pulsesink_get_mute (GstPulseSink * psink)
1902 GstPulseRingBuffer *pbuf;
1903 pa_operation *o = NULL;
1905 gboolean mute = FALSE;
1907 pa_threaded_mainloop_lock (psink->mainloop);
1909 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
1910 if (pbuf == NULL || pbuf->stream == NULL)
1913 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
1916 if (!(o = pa_context_get_sink_input_info (pbuf->context, idx,
1917 gst_pulsesink_sink_input_info_cb, pbuf)))
1920 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1921 pa_threaded_mainloop_wait (psink->mainloop);
1922 if (gst_pulsering_is_dead (psink, pbuf))
1930 pa_operation_unref (o);
1932 pa_threaded_mainloop_unlock (psink->mainloop);
1939 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
1944 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
1949 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1950 ("pa_context_get_sink_input_info() failed: %s",
1951 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1958 gst_pulsesink_sink_info_cb (pa_context * c, const pa_sink_info * i, int eol,
1961 GstPulseRingBuffer *pbuf;
1962 GstPulseSink *psink;
1964 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
1965 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1973 g_assert (i->index == pa_stream_get_device_index (pbuf->stream));
1975 g_free (psink->device_description);
1976 psink->device_description = g_strdup (i->description);
1979 pa_threaded_mainloop_signal (psink->mainloop, 0);
1983 gst_pulsesink_device_description (GstPulseSink * psink)
1985 GstPulseRingBuffer *pbuf;
1986 pa_operation *o = NULL;
1989 pa_threaded_mainloop_lock (psink->mainloop);
1990 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
1991 if (pbuf == NULL || pbuf->stream == NULL)
1994 if (!(o = pa_context_get_sink_info_by_index (pbuf->context,
1995 pa_stream_get_device_index (pbuf->stream),
1996 gst_pulsesink_sink_info_cb, pbuf)))
1999 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2000 pa_threaded_mainloop_wait (psink->mainloop);
2001 if (gst_pulsering_is_dead (psink, pbuf))
2007 pa_operation_unref (o);
2009 t = g_strdup (psink->device_description);
2010 pa_threaded_mainloop_unlock (psink->mainloop);
2017 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2022 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2023 ("pa_context_get_sink_info_by_index() failed: %s",
2024 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2030 gst_pulsesink_set_property (GObject * object,
2031 guint prop_id, const GValue * value, GParamSpec * pspec)
2033 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2037 g_free (pulsesink->server);
2038 pulsesink->server = g_value_dup_string (value);
2039 if (pulsesink->probe)
2040 gst_pulseprobe_set_server (pulsesink->probe, pulsesink->server);
2043 g_free (pulsesink->device);
2044 pulsesink->device = g_value_dup_string (value);
2046 #if HAVE_PULSE_0_9_12
2048 gst_pulsesink_set_volume (pulsesink, g_value_get_double (value));
2051 gst_pulsesink_set_mute (pulsesink, g_value_get_boolean (value));
2055 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2061 gst_pulsesink_get_property (GObject * object,
2062 guint prop_id, GValue * value, GParamSpec * pspec)
2065 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2069 g_value_set_string (value, pulsesink->server);
2072 g_value_set_string (value, pulsesink->device);
2074 case PROP_DEVICE_NAME:
2075 g_value_take_string (value, gst_pulsesink_device_description (pulsesink));
2077 #if HAVE_PULSE_0_9_12
2079 g_value_set_double (value, gst_pulsesink_get_volume (pulsesink));
2082 g_value_set_boolean (value, gst_pulsesink_get_mute (pulsesink));
2086 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2092 gst_pulsesink_change_title (GstPulseSink * psink, const gchar * t)
2094 pa_operation *o = NULL;
2095 GstPulseRingBuffer *pbuf;
2097 pa_threaded_mainloop_lock (psink->mainloop);
2099 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2101 if (pbuf == NULL || pbuf->stream == NULL)
2104 g_free (pbuf->stream_name);
2105 pbuf->stream_name = g_strdup (t);
2107 if (!(o = pa_stream_set_name (pbuf->stream, pbuf->stream_name, NULL, NULL)))
2110 /* We're not interested if this operation failed or not */
2113 pa_operation_unref (o);
2114 pa_threaded_mainloop_unlock (psink->mainloop);
2121 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2126 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2127 ("pa_stream_set_name() failed: %s",
2128 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2133 #if HAVE_PULSE_0_9_11
2135 gst_pulsesink_change_props (GstPulseSink * psink, GstTagList * l)
2137 static const gchar *const map[] = {
2138 GST_TAG_TITLE, PA_PROP_MEDIA_TITLE,
2140 /* might get overriden in the next iteration by GST_TAG_ARTIST */
2141 GST_TAG_PERFORMER, PA_PROP_MEDIA_ARTIST,
2143 GST_TAG_ARTIST, PA_PROP_MEDIA_ARTIST,
2144 GST_TAG_LANGUAGE_CODE, PA_PROP_MEDIA_LANGUAGE,
2145 GST_TAG_LOCATION, PA_PROP_MEDIA_FILENAME,
2146 /* We might add more here later on ... */
2149 pa_proplist *pl = NULL;
2150 const gchar *const *t;
2151 gboolean empty = TRUE;
2152 pa_operation *o = NULL;
2153 GstPulseRingBuffer *pbuf;
2155 pl = pa_proplist_new ();
2157 for (t = map; *t; t += 2) {
2160 if (gst_tag_list_get_string (l, *t, &n)) {
2163 pa_proplist_sets (pl, *(t + 1), n);
2173 pa_threaded_mainloop_lock (psink->mainloop);
2174 pbuf = GST_PULSERING_BUFFER_CAST (GST_BASE_AUDIO_SINK (psink)->ringbuffer);
2175 if (pbuf == NULL || pbuf->stream == NULL)
2178 if (!(o = pa_stream_proplist_update (pbuf->stream, PA_UPDATE_REPLACE,
2182 /* We're not interested if this operation failed or not */
2186 pa_operation_unref (o);
2188 pa_threaded_mainloop_unlock (psink->mainloop);
2193 pa_proplist_free (pl);
2200 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2205 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2206 ("pa_stream_proplist_update() failed: %s",
2207 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2214 gst_pulsesink_event (GstBaseSink * sink, GstEvent * event)
2216 GstPulseSink *pulsesink = GST_PULSESINK_CAST (sink);
2218 switch (GST_EVENT_TYPE (event)) {
2219 case GST_EVENT_TAG:{
2220 gchar *title = NULL, *artist = NULL, *location = NULL, *description =
2221 NULL, *t = NULL, *buf = NULL;
2224 gst_event_parse_tag (event, &l);
2226 gst_tag_list_get_string (l, GST_TAG_TITLE, &title);
2227 gst_tag_list_get_string (l, GST_TAG_ARTIST, &artist);
2228 gst_tag_list_get_string (l, GST_TAG_LOCATION, &location);
2229 gst_tag_list_get_string (l, GST_TAG_DESCRIPTION, &description);
2232 gst_tag_list_get_string (l, GST_TAG_PERFORMER, &artist);
2234 if (title && artist)
2235 /* TRANSLATORS: 'song title' by 'artist name' */
2236 t = buf = g_strdup_printf (_("'%s' by '%s'"), g_strstrip (title),
2237 g_strstrip (artist));
2239 t = g_strstrip (title);
2240 else if (description)
2241 t = g_strstrip (description);
2243 t = g_strstrip (location);
2246 gst_pulsesink_change_title (pulsesink, t);
2251 g_free (description);
2254 #if HAVE_PULSE_0_9_11
2255 gst_pulsesink_change_props (pulsesink, l);
2264 return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);