1 /*-*- Mode: C; c-basic-offset: 2 -*-*/
3 /* GStreamer pulseaudio plugin
5 * Copyright (c) 2004-2008 Lennart Poettering
8 * gst-pulse is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation; either version 2.1 of the
11 * License, or (at your option) any later version.
13 * gst-pulse is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with gst-pulse; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
25 * SECTION:element-pulsesink
28 * This element outputs audio to a
29 * <ulink href="http://www.pulseaudio.org">PulseAudio sound server</ulink>.
32 * <title>Example pipelines</title>
34 * gst-launch-1.0 -v filesrc location=sine.ogg ! oggdemux ! vorbisdec ! audioconvert ! audioresample ! pulsesink
35 * ]| Play an Ogg/Vorbis file.
37 * gst-launch-1.0 -v audiotestsrc ! audioconvert ! volume volume=0.4 ! pulsesink
38 * ]| Play a 440Hz sine wave.
40 * gst-launch-1.0 -v audiotestsrc ! pulsesink stream-properties="props,media.title=test"
41 * ]| Play a sine wave and set a stream property. The property can be checked
53 #include <gst/base/gstbasesink.h>
54 #include <gst/gsttaglist.h>
55 #include <gst/audio/audio.h>
56 #include <gst/gst-i18n-plugin.h>
58 #include <gst/pbutils/pbutils.h> /* only used for GST_PLUGINS_BASE_VERSION_* */
60 #include <gst/glib-compat-private.h>
61 #if defined(__TIZEN__) && defined(PCM_DUMP_ENABLE)
63 #endif /* __TIZEN__ && PCM_DUMP_ENABLE */
64 #include "pulsesink.h"
65 #include "pulseutil.h"
67 GST_DEBUG_CATEGORY_EXTERN (pulse_debug);
68 #define GST_CAT_DEFAULT pulse_debug
70 #define DEFAULT_SERVER NULL
71 #define DEFAULT_DEVICE NULL
72 #define DEFAULT_CURRENT_DEVICE NULL
73 #define DEFAULT_DEVICE_NAME NULL
74 #define DEFAULT_VOLUME 1.0
75 #define DEFAULT_MUTE FALSE
76 #define MAX_VOLUME 10.0
78 #define DEFAULT_AUDIO_LATENCY "mid"
79 #endif /* __TIZEN__ */
91 PROP_STREAM_PROPERTIES,
94 #endif /* __TIZEN__ */
98 #if defined(__TIZEN__) && defined(PCM_DUMP_ENABLE)
99 #define GST_PULSESINK_DUMP_VCONF_KEY "memory/private/sound/pcm_dump"
100 #define GST_PULSESINK_DUMP_INPUT_PATH_PREFIX "/tmp/dump_pulsesink_in_"
101 #define GST_PULSESINK_DUMP_OUTPUT_PATH_PREFIX "/tmp/dump_pulsesink_out_"
102 #define GST_PULSESINK_DUMP_INPUT_FLAG 0x00000400
103 #define GST_PULSESINK_DUMP_OUTPUT_FLAG 0x00000800
104 #endif /* __TIZEN__ && PCM_DUMP_ENABLE */
106 #define GST_TYPE_PULSERING_BUFFER \
107 (gst_pulseringbuffer_get_type())
108 #define GST_PULSERING_BUFFER(obj) \
109 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_PULSERING_BUFFER,GstPulseRingBuffer))
110 #define GST_PULSERING_BUFFER_CLASS(klass) \
111 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_PULSERING_BUFFER,GstPulseRingBufferClass))
112 #define GST_PULSERING_BUFFER_GET_CLASS(obj) \
113 (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_PULSERING_BUFFER, GstPulseRingBufferClass))
114 #define GST_PULSERING_BUFFER_CAST(obj) \
115 ((GstPulseRingBuffer *)obj)
116 #define GST_IS_PULSERING_BUFFER(obj) \
117 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_PULSERING_BUFFER))
118 #define GST_IS_PULSERING_BUFFER_CLASS(klass)\
119 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_PULSERING_BUFFER))
121 typedef struct _GstPulseRingBuffer GstPulseRingBuffer;
122 typedef struct _GstPulseRingBufferClass GstPulseRingBufferClass;
124 typedef struct _GstPulseContext GstPulseContext;
126 /* A note on threading.
128 * We use a pa_threaded_mainloop to interact with the PulseAudio server. This
129 * starts up a separate thread that runs a mainloop to carry back events,
130 * messages and timing updates from the PulseAudio server.
132 * In most cases, the PulseAudio API we use communicates with the server and
133 * processes replies asynchronously. Operations on PA objects that result in
134 * such communication are protected with a pa_threaded_mainloop_lock() and
135 * pa_threaded_mainloop_unlock(). These guarantee mutual exclusion with the
136 * mainloop thread -- when an iteration of the mainloop thread begins, it first
137 * tries to acquire this lock, and cannot do so if our code also holds that
140 * When we need to complete an operation synchronously, we use
141 * pa_threaded_mainloop_wait() and pa_threaded_mainloop_signal(). These work
142 * much as pthread conditionals do. pa_threaded_mainloop_wait() is called with
143 * the mainloop lock held. It releases the lock (thereby allowing the mainloop
144 * to execute), and waits till one of our callbacks to be executed by the
145 * mainloop thread calls pa_threaded_mainloop_signal(). At the end of the
146 * mainloop iteration, the pa_threaded_mainloop_wait() will reacquire the
147 * mainloop lock and return control to the caller.
150 /* Store the PA contexts in a hash table to allow easy sharing among
151 * multiple instances of the sink. Keys are $context_name@$server_name
152 * (strings) and values should be GstPulseContext pointers.
154 struct _GstPulseContext
157 GSList *ring_buffers;
160 static GHashTable *gst_pulse_shared_contexts = NULL;
162 /* use one static main-loop for all instances
163 * this is needed to make the context sharing work as the contexts are
164 * released when releasing their parent main-loop
166 static pa_threaded_mainloop *mainloop = NULL;
167 static guint mainloop_ref_ct = 0;
169 /* lock for access to shared resources */
170 static GMutex pa_shared_resource_mutex;
172 /* We keep a custom ringbuffer that is backed up by data allocated by
173 * pulseaudio. We must also overide the commit function to write into
174 * pulseaudio memory instead. */
175 struct _GstPulseRingBuffer
177 GstAudioRingBuffer object;
184 pa_stream *probe_stream;
186 pa_format_info *format;
197 gboolean in_commit:1;
200 struct _GstPulseRingBufferClass
202 GstAudioRingBufferClass parent_class;
205 static GType gst_pulseringbuffer_get_type (void);
206 static void gst_pulseringbuffer_finalize (GObject * object);
208 static GstAudioRingBufferClass *ring_parent_class = NULL;
210 static gboolean gst_pulseringbuffer_open_device (GstAudioRingBuffer * buf);
211 static gboolean gst_pulseringbuffer_close_device (GstAudioRingBuffer * buf);
212 static gboolean gst_pulseringbuffer_acquire (GstAudioRingBuffer * buf,
213 GstAudioRingBufferSpec * spec);
214 static gboolean gst_pulseringbuffer_release (GstAudioRingBuffer * buf);
215 static gboolean gst_pulseringbuffer_start (GstAudioRingBuffer * buf);
216 static gboolean gst_pulseringbuffer_pause (GstAudioRingBuffer * buf);
217 static gboolean gst_pulseringbuffer_stop (GstAudioRingBuffer * buf);
218 static void gst_pulseringbuffer_clear (GstAudioRingBuffer * buf);
219 static guint gst_pulseringbuffer_commit (GstAudioRingBuffer * buf,
220 guint64 * sample, guchar * data, gint in_samples, gint out_samples,
223 static gboolean gst_pulsering_set_corked (GstPulseRingBuffer * pbuf, gboolean corked,
227 G_DEFINE_TYPE (GstPulseRingBuffer, gst_pulseringbuffer,
228 GST_TYPE_AUDIO_RING_BUFFER);
231 gst_pulsesink_init_contexts (void)
233 g_mutex_init (&pa_shared_resource_mutex);
234 gst_pulse_shared_contexts = g_hash_table_new_full (g_str_hash, g_str_equal,
239 gst_pulseringbuffer_class_init (GstPulseRingBufferClass * klass)
241 GObjectClass *gobject_class;
242 GstAudioRingBufferClass *gstringbuffer_class;
244 gobject_class = (GObjectClass *) klass;
245 gstringbuffer_class = (GstAudioRingBufferClass *) klass;
247 ring_parent_class = g_type_class_peek_parent (klass);
249 gobject_class->finalize = gst_pulseringbuffer_finalize;
251 gstringbuffer_class->open_device =
252 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_open_device);
253 gstringbuffer_class->close_device =
254 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_close_device);
255 gstringbuffer_class->acquire =
256 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_acquire);
257 gstringbuffer_class->release =
258 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_release);
259 gstringbuffer_class->start = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_start);
260 gstringbuffer_class->pause = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_pause);
261 gstringbuffer_class->resume = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_start);
262 gstringbuffer_class->stop = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_stop);
263 gstringbuffer_class->clear_all =
264 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_clear);
266 gstringbuffer_class->commit = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_commit);
270 gst_pulseringbuffer_init (GstPulseRingBuffer * pbuf)
272 pbuf->stream_name = NULL;
273 pbuf->context = NULL;
275 pbuf->probe_stream = NULL;
279 pbuf->is_pcm = FALSE;
283 pbuf->m_writable = 0;
285 pbuf->m_lastoffset = 0;
288 pbuf->in_commit = FALSE;
289 pbuf->paused = FALSE;
292 /* Call with mainloop lock held if wait == TRUE) */
294 gst_pulse_destroy_stream (pa_stream * stream, gboolean wait)
296 /* Make sure we don't get any further callbacks */
297 pa_stream_set_write_callback (stream, NULL, NULL);
298 pa_stream_set_underflow_callback (stream, NULL, NULL);
299 pa_stream_set_overflow_callback (stream, NULL, NULL);
301 pa_stream_disconnect (stream);
304 pa_threaded_mainloop_wait (mainloop);
306 pa_stream_set_state_callback (stream, NULL, NULL);
307 pa_stream_unref (stream);
311 gst_pulsering_destroy_stream (GstPulseRingBuffer * pbuf)
313 if (pbuf->probe_stream) {
314 gst_pulse_destroy_stream (pbuf->probe_stream, FALSE);
315 pbuf->probe_stream = NULL;
321 /* drop shm memory buffer */
322 pa_stream_cancel_write (pbuf->stream);
324 /* reset internal variables */
327 pbuf->m_writable = 0;
329 pbuf->m_lastoffset = 0;
332 pa_format_info_free (pbuf->format);
335 pbuf->is_pcm = FALSE;
338 pa_stream_disconnect (pbuf->stream);
340 /* Make sure we don't get any further callbacks */
341 pa_stream_set_state_callback (pbuf->stream, NULL, NULL);
342 pa_stream_set_write_callback (pbuf->stream, NULL, NULL);
343 pa_stream_set_underflow_callback (pbuf->stream, NULL, NULL);
344 pa_stream_set_overflow_callback (pbuf->stream, NULL, NULL);
346 pa_stream_unref (pbuf->stream);
350 g_free (pbuf->stream_name);
351 pbuf->stream_name = NULL;
355 gst_pulsering_destroy_context (GstPulseRingBuffer * pbuf)
357 g_mutex_lock (&pa_shared_resource_mutex);
359 GST_DEBUG_OBJECT (pbuf, "destroying ringbuffer %p", pbuf);
361 gst_pulsering_destroy_stream (pbuf);
364 pa_context_unref (pbuf->context);
365 pbuf->context = NULL;
368 if (pbuf->context_name) {
369 GstPulseContext *pctx;
371 pctx = g_hash_table_lookup (gst_pulse_shared_contexts, pbuf->context_name);
373 GST_DEBUG_OBJECT (pbuf, "releasing context with name %s, pbuf=%p, pctx=%p",
374 pbuf->context_name, pbuf, pctx);
377 pctx->ring_buffers = g_slist_remove (pctx->ring_buffers, pbuf);
378 if (pctx->ring_buffers == NULL) {
379 GST_DEBUG_OBJECT (pbuf,
380 "destroying final context with name %s, pbuf=%p, pctx=%p",
381 pbuf->context_name, pbuf, pctx);
383 pa_context_disconnect (pctx->context);
385 /* Make sure we don't get any further callbacks */
386 pa_context_set_state_callback (pctx->context, NULL, NULL);
387 pa_context_set_subscribe_callback (pctx->context, NULL, NULL);
389 g_hash_table_remove (gst_pulse_shared_contexts, pbuf->context_name);
391 pa_context_unref (pctx->context);
392 g_slice_free (GstPulseContext, pctx);
395 g_free (pbuf->context_name);
396 pbuf->context_name = NULL;
398 g_mutex_unlock (&pa_shared_resource_mutex);
402 gst_pulseringbuffer_finalize (GObject * object)
404 GstPulseRingBuffer *ringbuffer;
406 ringbuffer = GST_PULSERING_BUFFER_CAST (object);
408 gst_pulsering_destroy_context (ringbuffer);
409 G_OBJECT_CLASS (ring_parent_class)->finalize (object);
413 #define CONTEXT_OK(c) ((c) && PA_CONTEXT_IS_GOOD (pa_context_get_state ((c))))
414 #define STREAM_OK(s) ((s) && PA_STREAM_IS_GOOD (pa_stream_get_state ((s))))
417 gst_pulsering_is_dead (GstPulseSink * psink, GstPulseRingBuffer * pbuf,
418 gboolean check_stream)
420 if (!CONTEXT_OK (pbuf->context))
423 if (check_stream && !STREAM_OK (pbuf->stream))
430 const gchar *err_str =
431 pbuf->context ? pa_strerror (pa_context_errno (pbuf->context)) : NULL;
432 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED, ("Disconnected: %s",
439 gst_pulsering_context_state_cb (pa_context * c, void *userdata)
441 pa_context_state_t state;
442 pa_threaded_mainloop *mainloop = (pa_threaded_mainloop *) userdata;
444 state = pa_context_get_state (c);
446 GST_LOG ("got new context state %d", state);
449 case PA_CONTEXT_READY:
450 case PA_CONTEXT_TERMINATED:
451 case PA_CONTEXT_FAILED:
452 GST_LOG ("signaling");
453 pa_threaded_mainloop_signal (mainloop, 0);
456 case PA_CONTEXT_UNCONNECTED:
457 case PA_CONTEXT_CONNECTING:
458 case PA_CONTEXT_AUTHORIZING:
459 case PA_CONTEXT_SETTING_NAME:
465 gst_pulsering_context_subscribe_cb (pa_context * c,
466 pa_subscription_event_type_t t, uint32_t idx, void *userdata)
469 GstPulseContext *pctx = (GstPulseContext *) userdata;
472 if (t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT | PA_SUBSCRIPTION_EVENT_CHANGE) &&
473 t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT | PA_SUBSCRIPTION_EVENT_NEW))
476 for (walk = pctx->ring_buffers; walk; walk = g_slist_next (walk)) {
477 GstPulseRingBuffer *pbuf = (GstPulseRingBuffer *) walk->data;
478 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
480 GST_LOG_OBJECT (psink, "type %04x, idx %u", t, idx);
485 if (idx != pa_stream_get_index (pbuf->stream))
488 if (psink->device && pbuf->is_pcm &&
489 !g_str_equal (psink->device,
490 pa_stream_get_device_name (pbuf->stream))) {
491 /* Underlying sink changed. And this is not a passthrough stream. Let's
492 * see if someone upstream wants to try to renegotiate. */
495 g_free (psink->device);
496 psink->device = g_strdup (pa_stream_get_device_name (pbuf->stream));
498 GST_INFO_OBJECT (psink, "emitting sink-changed");
500 /* FIXME: send reconfigure event instead and let decodebin/playbin
501 * handle that. Also take care of ac3 alignment. See "pulse-format-lost" */
502 renego = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
503 gst_structure_new_empty ("pulse-sink-changed"));
505 if (!gst_pad_push_event (GST_BASE_SINK (psink)->sinkpad, renego))
506 GST_DEBUG_OBJECT (psink, "Emitted sink-changed - nobody was listening");
509 /* Actually this event is also triggered when other properties of
510 * the stream change that are unrelated to the volume. However it is
511 * probably cheaper to signal the change here and check for the
512 * volume when the GObject property is read instead of querying it always. */
514 /* inform streaming thread to notify */
515 g_atomic_int_compare_and_exchange (&psink->notify, 0, 1);
519 /* will be called when the device should be opened. In this case we will connect
520 * to the server. We should not try to open any streams in this state. */
522 gst_pulseringbuffer_open_device (GstAudioRingBuffer * buf)
525 GstPulseRingBuffer *pbuf;
526 GstPulseContext *pctx;
527 pa_mainloop_api *api;
528 gboolean need_unlock_shared;
530 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
531 pbuf = GST_PULSERING_BUFFER_CAST (buf);
533 g_assert (!pbuf->stream);
534 g_assert (psink->client_name);
537 pbuf->context_name = g_strdup_printf ("%s@%s", psink->client_name,
540 pbuf->context_name = g_strdup (psink->client_name);
542 pa_threaded_mainloop_lock (mainloop);
544 g_mutex_lock (&pa_shared_resource_mutex);
545 need_unlock_shared = TRUE;
547 pctx = g_hash_table_lookup (gst_pulse_shared_contexts, pbuf->context_name);
549 pctx = g_slice_new0 (GstPulseContext);
551 /* get the mainloop api and create a context */
552 GST_INFO_OBJECT (psink, "new context with name %s, pbuf=%p, pctx=%p",
553 pbuf->context_name, pbuf, pctx);
554 api = pa_threaded_mainloop_get_api (mainloop);
555 if (!(pctx->context = pa_context_new (api, pbuf->context_name)))
558 pctx->ring_buffers = g_slist_prepend (pctx->ring_buffers, pbuf);
559 g_hash_table_insert (gst_pulse_shared_contexts,
560 g_strdup (pbuf->context_name), (gpointer) pctx);
561 /* register some essential callbacks */
562 pa_context_set_state_callback (pctx->context,
563 gst_pulsering_context_state_cb, mainloop);
564 pa_context_set_subscribe_callback (pctx->context,
565 gst_pulsering_context_subscribe_cb, pctx);
567 /* try to connect to the server and wait for completion, we don't want to
568 * autospawn a deamon */
569 GST_LOG_OBJECT (psink, "connect to server %s",
570 GST_STR_NULL (psink->server));
571 if (pa_context_connect (pctx->context, psink->server,
572 PA_CONTEXT_NOAUTOSPAWN, NULL) < 0)
575 GST_INFO_OBJECT (psink,
576 "reusing shared context with name %s, pbuf=%p, pctx=%p",
577 pbuf->context_name, pbuf, pctx);
578 pctx->ring_buffers = g_slist_prepend (pctx->ring_buffers, pbuf);
581 g_mutex_unlock (&pa_shared_resource_mutex);
582 need_unlock_shared = FALSE;
584 /* context created or shared okay */
585 pbuf->context = pa_context_ref (pctx->context);
588 pa_context_state_t state;
590 state = pa_context_get_state (pbuf->context);
592 GST_LOG_OBJECT (psink, "context state is now %d", state);
594 if (!PA_CONTEXT_IS_GOOD (state))
597 if (state == PA_CONTEXT_READY)
600 /* Wait until the context is ready */
601 GST_LOG_OBJECT (psink, "waiting..");
602 pa_threaded_mainloop_wait (mainloop);
605 if (pa_context_get_server_protocol_version (pbuf->context) < 22) {
606 /* We need PulseAudio >= 1.0 on the server side for the extended API */
607 goto bad_server_version;
610 GST_LOG_OBJECT (psink, "opened the device");
612 pa_threaded_mainloop_unlock (mainloop);
619 if (need_unlock_shared)
620 g_mutex_unlock (&pa_shared_resource_mutex);
621 gst_pulsering_destroy_context (pbuf);
622 pa_threaded_mainloop_unlock (mainloop);
627 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
628 ("Failed to create context"), (NULL));
629 g_slice_free (GstPulseContext, pctx);
630 goto unlock_and_fail;
634 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED, ("Failed to connect: %s",
635 pa_strerror (pa_context_errno (pctx->context))), (NULL));
636 goto unlock_and_fail;
640 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED, ("PulseAudio server version "
641 "is too old."), (NULL));
642 goto unlock_and_fail;
646 /* close the device */
648 gst_pulseringbuffer_close_device (GstAudioRingBuffer * buf)
651 GstPulseRingBuffer *pbuf;
653 pbuf = GST_PULSERING_BUFFER_CAST (buf);
654 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
656 GST_LOG_OBJECT (psink, "closing device");
658 pa_threaded_mainloop_lock (mainloop);
659 gst_pulsering_destroy_context (pbuf);
660 pa_threaded_mainloop_unlock (mainloop);
662 #if defined(__TIZEN__) && defined(PCM_DUMP_ENABLE)
663 if (psink->dump_fd_input) {
664 fclose(psink->dump_fd_input);
665 psink->dump_fd_input = NULL;
667 #endif /* __TIZEN__ && PCM_DUMP_ENABLE */
669 GST_LOG_OBJECT (psink, "closed device");
675 gst_pulsering_stream_state_cb (pa_stream * s, void *userdata)
678 GstPulseRingBuffer *pbuf;
679 pa_stream_state_t state;
681 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
682 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
684 state = pa_stream_get_state (s);
685 GST_LOG_OBJECT (psink, "got new stream state %d", state);
688 case PA_STREAM_READY:
689 case PA_STREAM_FAILED:
690 case PA_STREAM_TERMINATED:
691 GST_LOG_OBJECT (psink, "signaling");
692 pa_threaded_mainloop_signal (mainloop, 0);
694 case PA_STREAM_UNCONNECTED:
695 case PA_STREAM_CREATING:
701 gst_pulsering_stream_request_cb (pa_stream * s, size_t length, void *userdata)
704 GstAudioRingBuffer *rbuf;
705 GstPulseRingBuffer *pbuf;
707 rbuf = GST_AUDIO_RING_BUFFER_CAST (userdata);
708 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
709 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
711 GST_LOG_OBJECT (psink, "got request for length %" G_GSIZE_FORMAT, length);
713 if (pbuf->in_commit && (length >= rbuf->spec.segsize)) {
714 /* only signal when we are waiting in the commit thread
715 * and got request for atleast a segment */
716 pa_threaded_mainloop_signal (mainloop, 0);
721 gst_pulsering_stream_underflow_cb (pa_stream * s, void *userdata)
724 GstPulseRingBuffer *pbuf;
726 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
727 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
729 GST_WARNING_OBJECT (psink, "Got underflow");
733 gst_pulsering_stream_overflow_cb (pa_stream * s, void *userdata)
736 GstPulseRingBuffer *pbuf;
738 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
739 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
741 GST_WARNING_OBJECT (psink, "Got overflow");
745 gst_pulsering_stream_latency_cb (pa_stream * s, void *userdata)
748 GstPulseRingBuffer *pbuf;
749 GstAudioRingBuffer *ringbuf;
750 const pa_timing_info *info;
753 info = pa_stream_get_timing_info (s);
755 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
756 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
757 ringbuf = GST_AUDIO_RING_BUFFER (pbuf);
760 GST_LOG_OBJECT (psink, "latency update (information unknown)");
764 if (!info->read_index_corrupt) {
765 /* Update segdone based on the read index. segdone is of segment
766 * granularity, while the read index is at byte granularity. We take the
767 * ceiling while converting the latter to the former since it is more
768 * conservative to report that we've read more than we have than to report
769 * less. One concern here is that latency updates happen every 100ms, which
770 * means segdone is not updated very often, but increasing the update
771 * frequency would mean more communication overhead. */
772 g_atomic_int_set (&ringbuf->segdone,
773 (int) gst_util_uint64_scale_ceil (info->read_index, 1,
774 ringbuf->spec.segsize));
777 sink_usec = info->configured_sink_usec;
779 GST_LOG_OBJECT (psink,
780 "latency_update, %" G_GUINT64_FORMAT ", %d:%" G_GINT64_FORMAT ", %d:%"
781 G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT,
782 GST_TIMEVAL_TO_TIME (info->timestamp), info->write_index_corrupt,
783 info->write_index, info->read_index_corrupt, info->read_index,
784 info->sink_usec, sink_usec);
788 gst_pulsering_stream_suspended_cb (pa_stream * p, void *userdata)
791 GstPulseRingBuffer *pbuf;
793 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
794 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
796 if (pa_stream_is_suspended (p))
797 GST_DEBUG_OBJECT (psink, "stream suspended");
799 GST_DEBUG_OBJECT (psink, "stream resumed");
803 gst_pulsering_stream_started_cb (pa_stream * p, void *userdata)
806 GstPulseRingBuffer *pbuf;
808 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
809 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
811 GST_DEBUG_OBJECT (psink, "stream started");
815 gst_pulsering_stream_event_cb (pa_stream * p, const char *name,
816 pa_proplist * pl, void *userdata)
819 GstPulseRingBuffer *pbuf;
821 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
822 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
824 if (!strcmp (name, PA_STREAM_EVENT_REQUEST_CORK)) {
825 /* the stream wants to PAUSE, post a message for the application. */
826 GST_DEBUG_OBJECT (psink, "got request for CORK");
827 gst_element_post_message (GST_ELEMENT_CAST (psink),
828 gst_message_new_request_state (GST_OBJECT_CAST (psink),
831 } else if (!strcmp (name, PA_STREAM_EVENT_REQUEST_UNCORK)) {
832 GST_DEBUG_OBJECT (psink, "got request for UNCORK");
833 gst_element_post_message (GST_ELEMENT_CAST (psink),
834 gst_message_new_request_state (GST_OBJECT_CAST (psink),
836 } else if (!strcmp (name, PA_STREAM_EVENT_FORMAT_LOST)) {
839 if (g_atomic_int_get (&psink->format_lost)) {
840 /* Duplicate event before we're done reconfiguring, discard */
844 GST_DEBUG_OBJECT (psink, "got FORMAT LOST");
845 g_atomic_int_set (&psink->format_lost, 1);
846 psink->format_lost_time = g_ascii_strtoull (pa_proplist_gets (pl,
847 "stream-time"), NULL, 0) * 1000;
849 g_free (psink->device);
850 psink->device = g_strdup (pa_proplist_gets (pl, "device"));
852 /* FIXME: send reconfigure event instead and let decodebin/playbin
853 * handle that. Also take care of ac3 alignment */
854 renego = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
855 gst_structure_new_empty ("pulse-format-lost"));
858 if (g_str_equal (gst_structure_get_name (st), "audio/x-eac3")) {
859 GstStructure *event_st = gst_structure_new ("ac3parse-set-alignment",
860 "alignment", G_TYPE_STRING, pbin->dbin ? "frame" : "iec61937", NULL);
862 if (!gst_pad_push_event (pbin->sinkpad,
863 gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, event_st)))
864 GST_WARNING_OBJECT (pbin->sinkpad, "Could not update alignment");
868 if (!gst_pad_push_event (GST_BASE_SINK (psink)->sinkpad, renego)) {
869 /* Nobody handled the format change - emit an error */
870 GST_ELEMENT_ERROR (psink, STREAM, FORMAT, ("Sink format changed"),
871 ("Sink format changed"));
874 } else if (!strcmp (name, PA_STREAM_EVENT_POP_TIMEOUT)) {
875 GST_WARNING_OBJECT (psink, "got event [%s], cork stream now!!!!", name);
876 gst_pulsering_set_corked (pbuf, TRUE, FALSE);
879 GST_DEBUG_OBJECT (psink, "got unknown event %s", name);
883 /* Called with the mainloop locked */
885 gst_pulsering_wait_for_stream_ready (GstPulseSink * psink, pa_stream * stream)
887 pa_stream_state_t state;
890 state = pa_stream_get_state (stream);
892 GST_LOG_OBJECT (psink, "stream state is now %d", state);
894 if (!PA_STREAM_IS_GOOD (state))
897 if (state == PA_STREAM_READY)
900 /* Wait until the stream is ready */
901 pa_threaded_mainloop_wait (mainloop);
906 /* This method should create a new stream of the given @spec. No playback should
907 * start yet so we start in the corked state. */
909 gst_pulseringbuffer_acquire (GstAudioRingBuffer * buf,
910 GstAudioRingBufferSpec * spec)
913 GstPulseRingBuffer *pbuf;
914 pa_buffer_attr wanted;
915 const pa_buffer_attr *actual;
916 pa_channel_map channel_map;
917 pa_operation *o = NULL;
919 pa_cvolume *pv = NULL;
920 pa_stream_flags_t flags;
922 GstAudioClock *clock;
923 pa_format_info *formats[1];
924 #ifndef GST_DISABLE_GST_DEBUG
925 gchar print_buf[PA_FORMAT_INFO_SNPRINT_MAX];
928 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
929 pbuf = GST_PULSERING_BUFFER_CAST (buf);
931 GST_LOG_OBJECT (psink, "creating sample spec");
932 /* convert the gstreamer sample spec to the pulseaudio format */
933 if (!gst_pulse_fill_format_info (spec, &pbuf->format, &pbuf->channels))
935 pbuf->is_pcm = pa_format_info_is_pcm (pbuf->format);
937 pa_threaded_mainloop_lock (mainloop);
939 /* we need a context and a no stream */
940 g_assert (pbuf->context);
941 g_assert (!pbuf->stream);
943 /* if we have a probe, disconnect it first so that if we're creating a
944 * compressed stream, it doesn't get blocked by a PCM stream */
945 if (pbuf->probe_stream) {
946 gst_pulse_destroy_stream (pbuf->probe_stream, TRUE);
947 pbuf->probe_stream = NULL;
950 /* enable event notifications */
951 GST_LOG_OBJECT (psink, "subscribing to context events");
952 if (!(o = pa_context_subscribe (pbuf->context,
953 PA_SUBSCRIPTION_MASK_SINK_INPUT, NULL, NULL)))
954 goto subscribe_failed;
956 pa_operation_unref (o);
958 /* initialize the channel map */
959 if (pbuf->is_pcm && gst_pulse_gst_to_channel_map (&channel_map, spec))
960 pa_format_info_set_channel_map (pbuf->format, &channel_map);
962 /* find a good name for the stream */
963 if (psink->stream_name)
964 name = psink->stream_name;
966 name = "Playback Stream";
968 #if defined(__TIZEN__) && defined(PCM_DUMP_ENABLE)
969 if (psink->need_dump_input == TRUE && psink->dump_fd_input == NULL) {
970 char *suffix , *dump_path;
971 GDateTime *time = g_date_time_new_now_local();
973 suffix = g_date_time_format(time, "%m%d_%H%M%S");
974 dump_path = g_strdup_printf("%s_%dch_%dhz_%s.pcm", GST_PULSESINK_DUMP_INPUT_PATH_PREFIX, pbuf->channels, spec->rate, suffix);
976 psink->dump_fd_input = fopen(dump_path, "w+");
980 g_date_time_unref(time);
982 #endif /* __TIZEN__ && PCM_DUMP_ENABLE */
984 /* create a stream */
985 formats[0] = pbuf->format;
986 if (!(pbuf->stream = pa_stream_new_extended (pbuf->context, name, formats, 1,
990 /* install essential callbacks */
991 pa_stream_set_state_callback (pbuf->stream,
992 gst_pulsering_stream_state_cb, pbuf);
993 pa_stream_set_write_callback (pbuf->stream,
994 gst_pulsering_stream_request_cb, pbuf);
995 pa_stream_set_underflow_callback (pbuf->stream,
996 gst_pulsering_stream_underflow_cb, pbuf);
997 pa_stream_set_overflow_callback (pbuf->stream,
998 gst_pulsering_stream_overflow_cb, pbuf);
999 pa_stream_set_latency_update_callback (pbuf->stream,
1000 gst_pulsering_stream_latency_cb, pbuf);
1001 pa_stream_set_suspended_callback (pbuf->stream,
1002 gst_pulsering_stream_suspended_cb, pbuf);
1003 pa_stream_set_started_callback (pbuf->stream,
1004 gst_pulsering_stream_started_cb, pbuf);
1005 pa_stream_set_event_callback (pbuf->stream,
1006 gst_pulsering_stream_event_cb, pbuf);
1008 /* buffering requirements. When setting prebuf to 0, the stream will not pause
1009 * when we cause an underrun, which causes time to continue. */
1010 memset (&wanted, 0, sizeof (wanted));
1011 wanted.tlength = spec->segtotal * spec->segsize;
1012 wanted.maxlength = -1;
1014 wanted.minreq = spec->segsize;
1016 GST_INFO_OBJECT (psink, "tlength: %d", wanted.tlength);
1017 GST_INFO_OBJECT (psink, "maxlength: %d", wanted.maxlength);
1018 GST_INFO_OBJECT (psink, "prebuf: %d", wanted.prebuf);
1019 GST_INFO_OBJECT (psink, "minreq: %d", wanted.minreq);
1021 /* configure volume when we changed it, else we leave the default */
1022 if (psink->volume_set) {
1023 GST_LOG_OBJECT (psink, "have volume of %f", psink->volume);
1026 gst_pulse_cvolume_from_linear (pv, pbuf->channels, psink->volume);
1028 GST_DEBUG_OBJECT (psink, "passthrough stream, not setting volume");
1035 /* construct the flags */
1036 flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE |
1037 PA_STREAM_ADJUST_LATENCY | PA_STREAM_START_CORKED;
1039 if (psink->mute_set) {
1041 flags |= PA_STREAM_START_MUTED;
1043 flags |= PA_STREAM_START_UNMUTED;
1046 /* we always start corked (see flags above) */
1047 pbuf->corked = TRUE;
1049 /* try to connect now */
1050 GST_LOG_OBJECT (psink, "connect for playback to device %s",
1051 GST_STR_NULL (psink->device));
1052 if (pa_stream_connect_playback (pbuf->stream, psink->device,
1053 &wanted, flags, pv, NULL) < 0)
1054 goto connect_failed;
1056 /* our clock will now start from 0 again */
1057 clock = GST_AUDIO_CLOCK (GST_AUDIO_BASE_SINK (psink)->provided_clock);
1058 gst_audio_clock_reset (clock, 0);
1060 if (!gst_pulsering_wait_for_stream_ready (psink, pbuf->stream))
1061 goto connect_failed;
1063 g_free (psink->device);
1064 psink->device = g_strdup (pa_stream_get_device_name (pbuf->stream));
1066 #ifndef GST_DISABLE_GST_DEBUG
1067 pa_format_info_snprint (print_buf, sizeof (print_buf),
1068 pa_stream_get_format_info (pbuf->stream));
1069 GST_INFO_OBJECT (psink, "negotiated to: %s", print_buf);
1072 /* After we passed the volume off of to PA we never want to set it
1073 again, since it is PA's job to save/restore volumes. */
1074 psink->volume_set = psink->mute_set = FALSE;
1076 GST_LOG_OBJECT (psink, "stream is acquired now");
1078 /* get the actual buffering properties now */
1079 actual = pa_stream_get_buffer_attr (pbuf->stream);
1081 GST_INFO_OBJECT (psink, "tlength: %d (wanted: %d)", actual->tlength,
1083 GST_INFO_OBJECT (psink, "maxlength: %d", actual->maxlength);
1084 GST_INFO_OBJECT (psink, "prebuf: %d", actual->prebuf);
1085 GST_INFO_OBJECT (psink, "minreq: %d (wanted %d)", actual->minreq,
1088 spec->segsize = actual->minreq;
1089 spec->segtotal = actual->tlength / spec->segsize;
1091 pa_threaded_mainloop_unlock (mainloop);
1098 gst_pulsering_destroy_stream (pbuf);
1099 pa_threaded_mainloop_unlock (mainloop);
1105 GST_ELEMENT_ERROR (psink, RESOURCE, SETTINGS,
1106 ("Invalid sample specification."), (NULL));
1111 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1112 ("pa_context_subscribe() failed: %s",
1113 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1114 goto unlock_and_fail;
1118 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1119 ("Failed to create stream: %s",
1120 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1121 goto unlock_and_fail;
1125 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1126 ("Failed to connect stream: %s",
1127 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1128 goto unlock_and_fail;
1132 /* free the stream that we acquired before */
1134 gst_pulseringbuffer_release (GstAudioRingBuffer * buf)
1136 GstPulseRingBuffer *pbuf;
1138 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1140 pa_threaded_mainloop_lock (mainloop);
1141 gst_pulsering_destroy_stream (pbuf);
1142 pa_threaded_mainloop_unlock (mainloop);
1145 GstPulseSink *psink;
1147 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1148 g_atomic_int_set (&psink->format_lost, FALSE);
1149 psink->format_lost_time = GST_CLOCK_TIME_NONE;
1156 gst_pulsering_success_cb (pa_stream * s, int success, void *userdata)
1158 pa_threaded_mainloop_signal (mainloop, 0);
1161 /* update the corked state of a stream, must be called with the mainloop
1164 gst_pulsering_set_corked (GstPulseRingBuffer * pbuf, gboolean corked,
1167 pa_operation *o = NULL;
1168 GstPulseSink *psink;
1169 gboolean res = FALSE;
1171 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1173 if (g_atomic_int_get (&psink->format_lost)) {
1174 /* Sink format changed, stream's gone so fake being paused */
1178 GST_DEBUG_OBJECT (psink, "setting corked state to %d", corked);
1179 if (pbuf->corked != corked) {
1180 if (!(o = pa_stream_cork (pbuf->stream, corked,
1181 gst_pulsering_success_cb, pbuf)))
1184 while (wait && pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1185 pa_threaded_mainloop_wait (mainloop);
1186 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
1189 pbuf->corked = corked;
1191 GST_DEBUG_OBJECT (psink, "skipping, already in requested state");
1197 pa_operation_unref (o);
1204 GST_DEBUG_OBJECT (psink, "the server is dead");
1209 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1210 ("pa_stream_cork() failed: %s",
1211 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1217 gst_pulseringbuffer_clear (GstAudioRingBuffer * buf)
1219 GstPulseSink *psink;
1220 GstPulseRingBuffer *pbuf;
1221 pa_operation *o = NULL;
1223 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1224 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1226 pa_threaded_mainloop_lock (mainloop);
1227 GST_DEBUG_OBJECT (psink, "clearing");
1229 /* don't wait for the flush to complete */
1230 if ((o = pa_stream_flush (pbuf->stream, NULL, pbuf)))
1231 pa_operation_unref (o);
1233 pa_threaded_mainloop_unlock (mainloop);
1237 /* called from pulse thread with the mainloop lock */
1239 mainloop_enter_defer_cb (pa_mainloop_api * api, void *userdata)
1241 GstPulseSink *pulsesink = GST_PULSESINK (userdata);
1242 GstMessage *message;
1245 GST_DEBUG_OBJECT (pulsesink, "posting ENTER stream status");
1246 message = gst_message_new_stream_status (GST_OBJECT (pulsesink),
1247 GST_STREAM_STATUS_TYPE_ENTER, GST_ELEMENT (pulsesink));
1248 g_value_init (&val, GST_TYPE_G_THREAD);
1249 g_value_set_boxed (&val, g_thread_self ());
1250 gst_message_set_stream_status_object (message, &val);
1251 g_value_unset (&val);
1253 gst_element_post_message (GST_ELEMENT (pulsesink), message);
1255 g_return_if_fail (pulsesink->defer_pending);
1256 pulsesink->defer_pending--;
1257 pa_threaded_mainloop_signal (mainloop, 0);
1261 /* start/resume playback ASAP, we don't uncork here but in the commit method */
1263 gst_pulseringbuffer_start (GstAudioRingBuffer * buf)
1265 GstPulseSink *psink;
1266 GstPulseRingBuffer *pbuf;
1268 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1269 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1271 pa_threaded_mainloop_lock (mainloop);
1273 GST_DEBUG_OBJECT (psink, "starting");
1274 pbuf->paused = FALSE;
1276 /* EOS needs running clock */
1277 if (GST_BASE_SINK_CAST (psink)->eos ||
1278 g_atomic_int_get (&GST_AUDIO_BASE_SINK (psink)->eos_rendering))
1279 gst_pulsering_set_corked (pbuf, FALSE, FALSE);
1282 GST_DEBUG_OBJECT (psink, "scheduling stream status");
1283 psink->defer_pending++;
1284 pa_mainloop_api_once (pa_threaded_mainloop_get_api (mainloop),
1285 mainloop_enter_defer_cb, psink);
1287 /* Wait for the stream status message to be posted. This needs to be done
1288 * synchronously because the callback will take the mainloop lock
1289 * (implicitly) and then take the GST_OBJECT_LOCK. Everywhere else, we take
1290 * the locks in the reverse order, so not doing this synchronously could
1291 * cause a deadlock. */
1292 GST_DEBUG_OBJECT (psink, "waiting for stream status (ENTER) to be posted");
1293 pa_threaded_mainloop_wait (mainloop);
1296 pa_threaded_mainloop_unlock (mainloop);
1301 /* pause/stop playback ASAP */
1303 gst_pulseringbuffer_pause (GstAudioRingBuffer * buf)
1305 GstPulseSink *psink;
1306 GstPulseRingBuffer *pbuf;
1309 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1310 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1312 pa_threaded_mainloop_lock (mainloop);
1313 GST_DEBUG_OBJECT (psink, "pausing and corking");
1314 /* make sure the commit method stops writing */
1315 pbuf->paused = TRUE;
1316 res = gst_pulsering_set_corked (pbuf, TRUE, TRUE);
1317 if (pbuf->in_commit) {
1318 /* we are waiting in a commit, signal */
1319 GST_DEBUG_OBJECT (psink, "signal commit");
1320 pa_threaded_mainloop_signal (mainloop, 0);
1322 pa_threaded_mainloop_unlock (mainloop);
1328 /* called from pulse thread with the mainloop lock */
1330 mainloop_leave_defer_cb (pa_mainloop_api * api, void *userdata)
1332 GstPulseSink *pulsesink = GST_PULSESINK (userdata);
1333 GstMessage *message;
1336 GST_DEBUG_OBJECT (pulsesink, "posting LEAVE stream status");
1337 message = gst_message_new_stream_status (GST_OBJECT (pulsesink),
1338 GST_STREAM_STATUS_TYPE_LEAVE, GST_ELEMENT (pulsesink));
1339 g_value_init (&val, GST_TYPE_G_THREAD);
1340 g_value_set_boxed (&val, g_thread_self ());
1341 gst_message_set_stream_status_object (message, &val);
1342 g_value_unset (&val);
1344 gst_element_post_message (GST_ELEMENT (pulsesink), message);
1346 g_return_if_fail (pulsesink->defer_pending);
1347 pulsesink->defer_pending--;
1348 pa_threaded_mainloop_signal (mainloop, 0);
1352 /* stop playback, we flush everything. */
1354 gst_pulseringbuffer_stop (GstAudioRingBuffer * buf)
1356 GstPulseSink *psink;
1357 GstPulseRingBuffer *pbuf;
1358 gboolean res = FALSE;
1359 pa_operation *o = NULL;
1361 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1362 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1364 pa_threaded_mainloop_lock (mainloop);
1366 pbuf->paused = TRUE;
1367 res = gst_pulsering_set_corked (pbuf, TRUE, TRUE);
1369 /* Inform anyone waiting in _commit() call that it shall wakeup */
1370 if (pbuf->in_commit) {
1371 GST_DEBUG_OBJECT (psink, "signal commit thread");
1372 pa_threaded_mainloop_signal (mainloop, 0);
1374 if (g_atomic_int_get (&psink->format_lost)) {
1375 /* Don't try to flush, the stream's probably gone by now */
1380 /* then try to flush, it's not fatal when this fails */
1381 GST_DEBUG_OBJECT (psink, "flushing");
1382 if ((o = pa_stream_flush (pbuf->stream, gst_pulsering_success_cb, pbuf))) {
1383 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1384 GST_DEBUG_OBJECT (psink, "wait for completion");
1385 pa_threaded_mainloop_wait (mainloop);
1386 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
1389 GST_DEBUG_OBJECT (psink, "flush completed");
1395 pa_operation_cancel (o);
1396 pa_operation_unref (o);
1399 GST_DEBUG_OBJECT (psink, "scheduling stream status");
1400 psink->defer_pending++;
1401 pa_mainloop_api_once (pa_threaded_mainloop_get_api (mainloop),
1402 mainloop_leave_defer_cb, psink);
1404 /* Wait for the stream status message to be posted. This needs to be done
1405 * synchronously because the callback will take the mainloop lock
1406 * (implicitly) and then take the GST_OBJECT_LOCK. Everywhere else, we take
1407 * the locks in the reverse order, so not doing this synchronously could
1408 * cause a deadlock. */
1409 GST_DEBUG_OBJECT (psink, "waiting for stream status (LEAVE) to be posted");
1410 pa_threaded_mainloop_wait (mainloop);
1413 pa_threaded_mainloop_unlock (mainloop);
1420 GST_DEBUG_OBJECT (psink, "the server is dead");
1425 /* in_samples >= out_samples, rate > 1.0 */
1426 #define FWD_UP_SAMPLES(s,se,d,de) \
1428 guint8 *sb = s, *db = d; \
1429 while (s <= se && d < de) { \
1430 memcpy (d, s, bpf); \
1433 if ((*accum << 1) >= inr) { \
1438 in_samples -= (s - sb)/bpf; \
1439 out_samples -= (d - db)/bpf; \
1440 GST_DEBUG ("fwd_up end %d/%d",*accum,*toprocess); \
1443 /* out_samples > in_samples, for rates smaller than 1.0 */
1444 #define FWD_DOWN_SAMPLES(s,se,d,de) \
1446 guint8 *sb = s, *db = d; \
1447 while (s <= se && d < de) { \
1448 memcpy (d, s, bpf); \
1451 if ((*accum << 1) >= outr) { \
1456 in_samples -= (s - sb)/bpf; \
1457 out_samples -= (d - db)/bpf; \
1458 GST_DEBUG ("fwd_down end %d/%d",*accum,*toprocess); \
1461 #define REV_UP_SAMPLES(s,se,d,de) \
1463 guint8 *sb = se, *db = d; \
1464 while (s <= se && d < de) { \
1465 memcpy (d, se, bpf); \
1468 while (d < de && (*accum << 1) >= inr) { \
1473 in_samples -= (sb - se)/bpf; \
1474 out_samples -= (d - db)/bpf; \
1475 GST_DEBUG ("rev_up end %d/%d",*accum,*toprocess); \
1478 #define REV_DOWN_SAMPLES(s,se,d,de) \
1480 guint8 *sb = se, *db = d; \
1481 while (s <= se && d < de) { \
1482 memcpy (d, se, bpf); \
1485 while (s <= se && (*accum << 1) >= outr) { \
1490 in_samples -= (sb - se)/bpf; \
1491 out_samples -= (d - db)/bpf; \
1492 GST_DEBUG ("rev_down end %d/%d",*accum,*toprocess); \
1495 /* our custom commit function because we write into the buffer of pulseaudio
1496 * instead of keeping our own buffer */
1498 gst_pulseringbuffer_commit (GstAudioRingBuffer * buf, guint64 * sample,
1499 guchar * data, gint in_samples, gint out_samples, gint * accum)
1501 GstPulseSink *psink;
1502 GstPulseRingBuffer *pbuf;
1507 gint inr, outr, bpf;
1511 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1512 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1514 /* FIXME post message rather than using a signal (as mixer interface) */
1515 if (g_atomic_int_compare_and_exchange (&psink->notify, 1, 0)) {
1516 g_object_notify (G_OBJECT (psink), "volume");
1517 g_object_notify (G_OBJECT (psink), "mute");
1518 g_object_notify (G_OBJECT (psink), "current-device");
1521 /* make sure the ringbuffer is started */
1522 if (G_UNLIKELY (g_atomic_int_get (&buf->state) !=
1523 GST_AUDIO_RING_BUFFER_STATE_STARTED)) {
1524 /* see if we are allowed to start it */
1525 if (G_UNLIKELY (g_atomic_int_get (&buf->may_start) == FALSE))
1528 GST_DEBUG_OBJECT (buf, "start!");
1529 if (!gst_audio_ring_buffer_start (buf))
1533 pa_threaded_mainloop_lock (mainloop);
1535 GST_DEBUG_OBJECT (psink, "entering commit");
1536 pbuf->in_commit = TRUE;
1538 bpf = GST_AUDIO_INFO_BPF (&buf->spec.info);
1539 bufsize = buf->spec.segsize * buf->spec.segtotal;
1541 /* our toy resampler for trick modes */
1542 reverse = out_samples < 0;
1543 out_samples = ABS (out_samples);
1545 if (in_samples >= out_samples)
1546 toprocess = &in_samples;
1548 toprocess = &out_samples;
1550 inr = in_samples - 1;
1551 outr = out_samples - 1;
1553 GST_DEBUG_OBJECT (psink, "in %d, out %d", inr, outr);
1555 /* data_end points to the last sample we have to write, not past it. This is
1556 * needed to properly handle reverse playback: it points to the last sample. */
1557 data_end = data + (bpf * inr);
1559 if (g_atomic_int_get (&psink->format_lost)) {
1560 /* Sink format changed, drop the data and hope upstream renegotiates */
1567 /* ensure running clock for whatever out there */
1569 if (!gst_pulsering_set_corked (pbuf, FALSE, FALSE))
1573 /* offset is in bytes */
1574 offset = *sample * bpf;
1576 while (*toprocess > 0) {
1580 GST_LOG_OBJECT (psink,
1581 "need to write %d samples at offset %" G_GINT64_FORMAT, *toprocess,
1584 if (offset != pbuf->m_lastoffset)
1585 GST_LOG_OBJECT (psink, "discontinuity, offset is %" G_GINT64_FORMAT ", "
1586 "last offset was %" G_GINT64_FORMAT, offset, pbuf->m_lastoffset);
1588 towrite = out_samples * bpf;
1590 /* Wait for at least segsize bytes to become available */
1591 if (towrite > buf->spec.segsize)
1592 towrite = buf->spec.segsize;
1594 if ((pbuf->m_writable < towrite) || (offset != pbuf->m_lastoffset)) {
1595 /* if no room left or discontinuity in offset,
1596 we need to flush data and get a new buffer */
1598 /* flush the buffer if possible */
1599 if ((pbuf->m_data != NULL) && (pbuf->m_towrite > 0)) {
1601 GST_LOG_OBJECT (psink,
1602 "flushing %u samples at offset %" G_GINT64_FORMAT,
1603 (guint) pbuf->m_towrite / bpf, pbuf->m_offset);
1605 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1606 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1610 pbuf->m_towrite = 0;
1611 pbuf->m_offset = offset; /* keep track of current offset */
1613 /* get a buffer to write in for now on */
1615 pbuf->m_writable = pa_stream_writable_size (pbuf->stream);
1617 if (g_atomic_int_get (&psink->format_lost)) {
1618 /* Sink format changed, give up and hope upstream renegotiates */
1622 if (pbuf->m_writable == (size_t) - 1)
1623 goto writable_size_failed;
1625 pbuf->m_writable /= bpf;
1626 pbuf->m_writable *= bpf; /* handle only complete samples */
1628 if (pbuf->m_writable >= towrite)
1631 /* see if we need to uncork because we have no free space */
1633 if (!gst_pulsering_set_corked (pbuf, FALSE, FALSE))
1637 /* we can't write segsize bytes, wait a bit */
1638 GST_LOG_OBJECT (psink, "waiting for free space");
1639 pa_threaded_mainloop_wait (mainloop);
1645 /* Recalculate what we can write in the next chunk */
1646 towrite = out_samples * bpf;
1647 if (pbuf->m_writable > towrite)
1648 pbuf->m_writable = towrite;
1650 GST_LOG_OBJECT (psink, "requesting %" G_GSIZE_FORMAT " bytes of "
1651 "shared memory", pbuf->m_writable);
1653 if (pa_stream_begin_write (pbuf->stream, &pbuf->m_data,
1654 &pbuf->m_writable) < 0) {
1655 GST_LOG_OBJECT (psink, "pa_stream_begin_write() failed");
1656 goto writable_size_failed;
1659 GST_LOG_OBJECT (psink, "got %" G_GSIZE_FORMAT " bytes of shared memory",
1664 if (towrite > pbuf->m_writable)
1665 towrite = pbuf->m_writable;
1666 avail = towrite / bpf;
1668 GST_LOG_OBJECT (psink, "writing %u samples at offset %" G_GUINT64_FORMAT,
1669 (guint) avail, offset);
1671 /* No trick modes for passthrough streams */
1672 if (G_UNLIKELY (!pbuf->is_pcm && (inr != outr || reverse))) {
1673 GST_WARNING_OBJECT (psink, "Passthrough stream can't run in trick mode");
1674 goto unlock_and_fail;
1677 if (G_LIKELY (inr == outr && !reverse)) {
1678 /* no rate conversion, simply write out the samples */
1679 /* copy the data into internal buffer */
1681 memcpy ((guint8 *) pbuf->m_data + pbuf->m_towrite, data, towrite);
1682 pbuf->m_towrite += towrite;
1683 pbuf->m_writable -= towrite;
1686 in_samples -= avail;
1687 out_samples -= avail;
1689 guint8 *dest, *d, *d_end;
1691 /* write into the PulseAudio shm buffer */
1692 dest = d = (guint8 *) pbuf->m_data + pbuf->m_towrite;
1693 d_end = d + towrite;
1697 /* forward speed up */
1698 FWD_UP_SAMPLES (data, data_end, d, d_end);
1700 /* forward slow down */
1701 FWD_DOWN_SAMPLES (data, data_end, d, d_end);
1704 /* reverse speed up */
1705 REV_UP_SAMPLES (data, data_end, d, d_end);
1707 /* reverse slow down */
1708 REV_DOWN_SAMPLES (data, data_end, d, d_end);
1710 /* see what we have left to write */
1711 towrite = (d - dest);
1712 pbuf->m_towrite += towrite;
1713 pbuf->m_writable -= towrite;
1715 avail = towrite / bpf;
1718 /* flush the buffer if it's full */
1719 if ((pbuf->m_data != NULL) && (pbuf->m_towrite > 0)
1720 && (pbuf->m_writable == 0)) {
1721 GST_LOG_OBJECT (psink, "flushing %u samples at offset %" G_GINT64_FORMAT,
1722 (guint) pbuf->m_towrite / bpf, pbuf->m_offset);
1724 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1725 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1728 pbuf->m_towrite = 0;
1729 pbuf->m_offset = offset + towrite; /* keep track of current offset */
1733 offset += avail * bpf;
1734 pbuf->m_lastoffset = offset;
1736 /* check if we need to uncork after writing the samples */
1738 const pa_timing_info *info;
1740 if ((info = pa_stream_get_timing_info (pbuf->stream))) {
1741 GST_LOG_OBJECT (psink,
1742 "read_index at %" G_GUINT64_FORMAT ", offset %" G_GINT64_FORMAT,
1743 info->read_index, offset);
1745 /* we uncork when the read_index is too far behind the offset we need
1747 if (info->read_index + bufsize <= offset) {
1748 if (!gst_pulsering_set_corked (pbuf, FALSE, FALSE))
1752 GST_LOG_OBJECT (psink, "no timing info available yet");
1758 /* we consumed all samples here */
1759 data = data_end + bpf;
1761 pbuf->in_commit = FALSE;
1762 pa_threaded_mainloop_unlock (mainloop);
1765 result = inr - ((data_end - data) / bpf);
1766 GST_LOG_OBJECT (psink, "wrote %d samples", result);
1773 pbuf->in_commit = FALSE;
1774 GST_LOG_OBJECT (psink, "we are reset");
1775 pa_threaded_mainloop_unlock (mainloop);
1780 GST_LOG_OBJECT (psink, "we can not start");
1785 GST_LOG_OBJECT (psink, "failed to start the ringbuffer");
1790 pbuf->in_commit = FALSE;
1791 GST_ERROR_OBJECT (psink, "uncork failed");
1792 pa_threaded_mainloop_unlock (mainloop);
1797 pbuf->in_commit = FALSE;
1798 GST_LOG_OBJECT (psink, "we are paused");
1799 pa_threaded_mainloop_unlock (mainloop);
1802 writable_size_failed:
1804 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1805 ("pa_stream_writable_size() failed: %s",
1806 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1807 goto unlock_and_fail;
1811 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1812 ("pa_stream_write() failed: %s",
1813 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1814 goto unlock_and_fail;
1818 /* write pending local samples, must be called with the mainloop lock */
1820 gst_pulsering_flush (GstPulseRingBuffer * pbuf)
1822 GstPulseSink *psink;
1824 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1825 GST_DEBUG_OBJECT (psink, "entering flush");
1827 /* flush the buffer if possible */
1828 if (pbuf->stream && (pbuf->m_data != NULL) && (pbuf->m_towrite > 0)) {
1829 #ifndef GST_DISABLE_GST_DEBUG
1832 bpf = (GST_AUDIO_RING_BUFFER_CAST (pbuf))->spec.info.bpf;
1833 GST_LOG_OBJECT (psink,
1834 "flushing %u samples at offset %" G_GINT64_FORMAT,
1835 (guint) pbuf->m_towrite / bpf, pbuf->m_offset);
1838 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1839 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1843 pbuf->m_towrite = 0;
1844 pbuf->m_offset += pbuf->m_towrite; /* keep track of current offset */
1853 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1854 ("pa_stream_write() failed: %s",
1855 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1860 static void gst_pulsesink_set_property (GObject * object, guint prop_id,
1861 const GValue * value, GParamSpec * pspec);
1862 static void gst_pulsesink_get_property (GObject * object, guint prop_id,
1863 GValue * value, GParamSpec * pspec);
1864 static void gst_pulsesink_finalize (GObject * object);
1866 static gboolean gst_pulsesink_event (GstBaseSink * sink, GstEvent * event);
1867 static gboolean gst_pulsesink_query (GstBaseSink * sink, GstQuery * query);
1869 static GstStateChangeReturn gst_pulsesink_change_state (GstElement * element,
1870 GstStateChange transition);
1872 static GstStaticPadTemplate pad_template = GST_STATIC_PAD_TEMPLATE ("sink",
1875 GST_STATIC_CAPS (PULSE_SINK_TEMPLATE_CAPS));
1877 #define gst_pulsesink_parent_class parent_class
1878 G_DEFINE_TYPE_WITH_CODE (GstPulseSink, gst_pulsesink, GST_TYPE_AUDIO_BASE_SINK,
1879 gst_pulsesink_init_contexts ();
1880 G_IMPLEMENT_INTERFACE (GST_TYPE_STREAM_VOLUME, NULL)
1883 static GstAudioRingBuffer *
1884 gst_pulsesink_create_ringbuffer (GstAudioBaseSink * sink)
1886 GstAudioRingBuffer *buffer;
1888 GST_DEBUG_OBJECT (sink, "creating ringbuffer");
1889 buffer = g_object_new (GST_TYPE_PULSERING_BUFFER, NULL);
1890 GST_DEBUG_OBJECT (sink, "created ringbuffer @%p", buffer);
1896 gst_pulsesink_payload (GstAudioBaseSink * sink, GstBuffer * buf)
1898 switch (sink->ringbuffer->spec.type) {
1899 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3:
1900 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_EAC3:
1901 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS:
1902 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_MPEG:
1903 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_MPEG2_AAC:
1904 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_MPEG4_AAC:
1906 /* FIXME: alloc memory from PA if possible */
1907 gint framesize = gst_audio_iec61937_frame_size (&sink->ringbuffer->spec);
1909 GstMapInfo inmap, outmap;
1915 out = gst_buffer_new_and_alloc (framesize);
1917 gst_buffer_map (buf, &inmap, GST_MAP_READ);
1918 gst_buffer_map (out, &outmap, GST_MAP_WRITE);
1920 res = gst_audio_iec61937_payload (inmap.data, inmap.size,
1921 outmap.data, outmap.size, &sink->ringbuffer->spec, G_BIG_ENDIAN);
1923 gst_buffer_unmap (buf, &inmap);
1924 gst_buffer_unmap (out, &outmap);
1927 gst_buffer_unref (out);
1931 gst_buffer_copy_into (out, buf, GST_BUFFER_COPY_METADATA, 0, -1);
1936 return gst_buffer_ref (buf);
1940 #if defined(__TIZEN__) && defined(PCM_DUMP_ENABLE)
1942 gst_pulsesink_pad_dump_handler (GstPad *pad, GstBuffer *buffer, gpointer data)
1944 GstPulseSink *psink = GST_PULSESINK_CAST (data);
1947 if (psink->dump_fd_input)
1948 ret = fwrite(GST_BUFFER_DATA(buffer), 1, GST_BUFFER_SIZE(buffer), psink->dump_fd_input);
1952 #endif /* __TIZEN__ && PCM_DUMP_ENABLE */
1955 gst_pulsesink_class_init (GstPulseSinkClass * klass)
1957 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1958 GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
1959 GstBaseSinkClass *bc;
1960 GstAudioBaseSinkClass *gstaudiosink_class = GST_AUDIO_BASE_SINK_CLASS (klass);
1961 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1964 gobject_class->finalize = gst_pulsesink_finalize;
1965 gobject_class->set_property = gst_pulsesink_set_property;
1966 gobject_class->get_property = gst_pulsesink_get_property;
1968 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_pulsesink_event);
1969 gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_pulsesink_query);
1971 /* restore the original basesink pull methods */
1972 bc = g_type_class_peek (GST_TYPE_BASE_SINK);
1973 gstbasesink_class->activate_pull = GST_DEBUG_FUNCPTR (bc->activate_pull);
1975 gstelement_class->change_state =
1976 GST_DEBUG_FUNCPTR (gst_pulsesink_change_state);
1978 gstaudiosink_class->create_ringbuffer =
1979 GST_DEBUG_FUNCPTR (gst_pulsesink_create_ringbuffer);
1980 gstaudiosink_class->payload = GST_DEBUG_FUNCPTR (gst_pulsesink_payload);
1982 /* Overwrite GObject fields */
1983 g_object_class_install_property (gobject_class,
1985 g_param_spec_string ("server", "Server",
1986 "The PulseAudio server to connect to", DEFAULT_SERVER,
1987 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1989 g_object_class_install_property (gobject_class, PROP_DEVICE,
1990 g_param_spec_string ("device", "Device",
1991 "The PulseAudio sink device to connect to", DEFAULT_DEVICE,
1992 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1994 g_object_class_install_property (gobject_class, PROP_CURRENT_DEVICE,
1995 g_param_spec_string ("current-device", "Current Device",
1996 "The current PulseAudio sink device", DEFAULT_CURRENT_DEVICE,
1997 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1999 g_object_class_install_property (gobject_class,
2001 g_param_spec_string ("device-name", "Device name",
2002 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
2003 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
2005 g_object_class_install_property (gobject_class,
2007 g_param_spec_double ("volume", "Volume",
2008 "Linear volume of this stream, 1.0=100%", 0.0, MAX_VOLUME,
2009 DEFAULT_VOLUME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2010 g_object_class_install_property (gobject_class,
2012 g_param_spec_boolean ("mute", "Mute",
2013 "Mute state of this stream", DEFAULT_MUTE,
2014 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2017 * GstPulseSink:client-name:
2019 * The PulseAudio client name to use.
2021 clientname = gst_pulse_client_name ();
2022 g_object_class_install_property (gobject_class,
2024 g_param_spec_string ("client-name", "Client Name",
2025 "The PulseAudio client name to use", clientname,
2026 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
2027 GST_PARAM_MUTABLE_READY));
2028 g_free (clientname);
2031 * GstPulseSink:stream-properties:
2033 * List of pulseaudio stream properties. A list of defined properties can be
2034 * found in the <ulink url="http://0pointer.de/lennart/projects/pulseaudio/doxygen/proplist_8h.html">pulseaudio api docs</ulink>.
2036 * Below is an example for registering as a music application to pulseaudio.
2038 * GstStructure *props;
2040 * props = gst_structure_from_string ("props,media.role=music", NULL);
2041 * g_object_set (pulse, "stream-properties", props, NULL);
2042 * gst_structure_free
2045 g_object_class_install_property (gobject_class,
2046 PROP_STREAM_PROPERTIES,
2047 g_param_spec_boxed ("stream-properties", "stream properties",
2048 "list of pulseaudio stream properties",
2049 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2052 g_object_class_install_property (gobject_class,
2054 g_param_spec_string ("latency", "Audio Backend Latency",
2055 "Audio Backend Latency (\"low\": Low Latency, \"mid\": Mid Latency, \"high\": High Latency)",
2056 DEFAULT_AUDIO_LATENCY,
2057 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2058 #endif /* __TIZEN__ */
2060 gst_element_class_set_static_metadata (gstelement_class,
2061 "PulseAudio Audio Sink",
2062 "Sink/Audio", "Plays audio to a PulseAudio server", "Lennart Poettering");
2063 gst_element_class_add_pad_template (gstelement_class,
2064 gst_static_pad_template_get (&pad_template));
2068 free_device_info (GstPulseDeviceInfo * device_info)
2072 g_free (device_info->description);
2074 for (l = g_list_first (device_info->formats); l; l = g_list_next (l))
2075 pa_format_info_free ((pa_format_info *) l->data);
2077 g_list_free (device_info->formats);
2080 /* Returns the current time of the sink ringbuffer. The timing_info is updated
2081 * on every data write/flush and every 100ms (PA_STREAM_AUTO_TIMING_UPDATE).
2084 gst_pulsesink_get_time (GstClock * clock, GstAudioBaseSink * sink)
2086 GstPulseSink *psink;
2087 GstPulseRingBuffer *pbuf;
2090 if (!sink->ringbuffer || !sink->ringbuffer->acquired)
2091 return GST_CLOCK_TIME_NONE;
2093 pbuf = GST_PULSERING_BUFFER_CAST (sink->ringbuffer);
2094 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
2096 if (g_atomic_int_get (&psink->format_lost)) {
2097 /* Stream was lost in a format change, it'll get set up again once
2098 * upstream renegotiates */
2099 return psink->format_lost_time;
2102 pa_threaded_mainloop_lock (mainloop);
2103 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
2106 /* if we don't have enough data to get a timestamp, just return NONE, which
2107 * will return the last reported time */
2108 if (pa_stream_get_time (pbuf->stream, &time) < 0) {
2109 GST_DEBUG_OBJECT (psink, "could not get time");
2110 time = GST_CLOCK_TIME_NONE;
2113 pa_threaded_mainloop_unlock (mainloop);
2115 GST_LOG_OBJECT (psink, "current time is %" GST_TIME_FORMAT,
2116 GST_TIME_ARGS (time));
2123 GST_DEBUG_OBJECT (psink, "the server is dead");
2124 pa_threaded_mainloop_unlock (mainloop);
2126 return GST_CLOCK_TIME_NONE;
2131 gst_pulsesink_sink_info_cb (pa_context * c, const pa_sink_info * i, int eol,
2134 GstPulseDeviceInfo *device_info = (GstPulseDeviceInfo *) userdata;
2140 device_info->description = g_strdup (i->description);
2142 device_info->formats = NULL;
2143 for (j = 0; j < i->n_formats; j++)
2144 device_info->formats = g_list_prepend (device_info->formats,
2145 pa_format_info_copy (i->formats[j]));
2148 pa_threaded_mainloop_signal (mainloop, 0);
2151 /* Call with mainloop lock held */
2153 gst_pulsesink_create_probe_stream (GstPulseSink * psink,
2154 GstPulseRingBuffer * pbuf, pa_format_info * format)
2156 pa_format_info *formats[1] = { format };
2158 pa_stream_flags_t flags;
2160 GST_LOG_OBJECT (psink, "Creating probe stream");
2162 if (!(stream = pa_stream_new_extended (pbuf->context, "pulsesink probe",
2163 formats, 1, psink->proplist)))
2166 /* construct the flags */
2167 flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE |
2168 PA_STREAM_ADJUST_LATENCY | PA_STREAM_START_CORKED;
2170 pa_stream_set_state_callback (stream, gst_pulsering_stream_state_cb, pbuf);
2172 if (pa_stream_connect_playback (stream, psink->device, NULL, flags, NULL,
2176 if (!gst_pulsering_wait_for_stream_ready (psink, stream))
2183 pa_stream_unref (stream);
2188 gst_pulsesink_query_getcaps (GstPulseSink * psink, GstCaps * filter)
2190 GstPulseRingBuffer *pbuf = NULL;
2191 GstPulseDeviceInfo device_info = { NULL, NULL };
2192 GstCaps *ret = NULL;
2194 pa_operation *o = NULL;
2197 GST_OBJECT_LOCK (psink);
2198 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2200 gst_object_ref (pbuf);
2201 GST_OBJECT_UNLOCK (psink);
2204 ret = gst_pad_get_pad_template_caps (GST_AUDIO_BASE_SINK_PAD (psink));
2208 GST_OBJECT_LOCK (pbuf);
2209 pa_threaded_mainloop_lock (mainloop);
2211 if (!pbuf->context) {
2212 ret = gst_pad_get_pad_template_caps (GST_AUDIO_BASE_SINK_PAD (psink));
2216 ret = gst_caps_new_empty ();
2219 /* We're in PAUSED or higher */
2220 stream = pbuf->stream;
2222 } else if (pbuf->probe_stream) {
2223 /* We're not paused, but have a cached probe stream */
2224 stream = pbuf->probe_stream;
2227 /* We're not yet in PAUSED and still need to create a probe stream.
2229 * FIXME: PA doesn't accept "any" format. We fix something reasonable since
2230 * this is merely a probe. This should eventually be fixed in PA and
2231 * hard-coding the format should be dropped. */
2232 pa_format_info *format = pa_format_info_new ();
2233 format->encoding = PA_ENCODING_PCM;
2234 pa_format_info_set_sample_format (format, PA_SAMPLE_S16LE);
2235 pa_format_info_set_rate (format, GST_AUDIO_DEF_RATE);
2236 pa_format_info_set_channels (format, GST_AUDIO_DEF_CHANNELS);
2238 pbuf->probe_stream = gst_pulsesink_create_probe_stream (psink, pbuf,
2241 pa_format_info_free (format);
2243 if (!pbuf->probe_stream) {
2244 GST_WARNING_OBJECT (psink, "Could not create probe stream");
2248 stream = pbuf->probe_stream;
2251 if (!(o = pa_context_get_sink_info_by_name (pbuf->context,
2252 pa_stream_get_device_name (stream), gst_pulsesink_sink_info_cb,
2256 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2257 pa_threaded_mainloop_wait (mainloop);
2258 if (gst_pulsering_is_dead (psink, pbuf, FALSE))
2262 for (i = g_list_first (device_info.formats); i; i = g_list_next (i)) {
2263 gst_caps_append (ret,
2264 gst_pulse_format_info_to_caps ((pa_format_info *) i->data));
2268 pa_threaded_mainloop_unlock (mainloop);
2269 /* FIXME: this could be freed after device_name is got */
2270 GST_OBJECT_UNLOCK (pbuf);
2273 GstCaps *tmp = gst_caps_intersect_full (filter, ret,
2274 GST_CAPS_INTERSECT_FIRST);
2275 gst_caps_unref (ret);
2280 free_device_info (&device_info);
2283 pa_operation_unref (o);
2286 gst_object_unref (pbuf);
2288 GST_DEBUG_OBJECT (psink, "caps %" GST_PTR_FORMAT, ret);
2294 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2295 ("pa_context_get_sink_input_info() failed: %s",
2296 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2302 gst_pulsesink_query_acceptcaps (GstPulseSink * psink, GstCaps * caps)
2304 GstPulseRingBuffer *pbuf = NULL;
2305 GstPulseDeviceInfo device_info = { NULL, NULL };
2308 gboolean ret = FALSE;
2310 GstAudioRingBufferSpec spec = { 0 };
2311 pa_operation *o = NULL;
2312 pa_channel_map channel_map;
2313 pa_format_info *format = NULL;
2316 pad_caps = gst_pad_get_pad_template_caps (GST_BASE_SINK_PAD (psink));
2317 ret = gst_caps_is_subset (caps, pad_caps);
2318 gst_caps_unref (pad_caps);
2320 GST_DEBUG_OBJECT (psink, "caps %" GST_PTR_FORMAT, caps);
2322 /* Template caps didn't match */
2326 /* If we've not got fixed caps, creating a stream might fail, so let's just
2327 * return from here with default acceptcaps behaviour */
2328 if (!gst_caps_is_fixed (caps))
2331 GST_OBJECT_LOCK (psink);
2332 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2334 gst_object_ref (pbuf);
2335 GST_OBJECT_UNLOCK (psink);
2337 /* We're still in NULL state */
2341 GST_OBJECT_LOCK (pbuf);
2342 pa_threaded_mainloop_lock (mainloop);
2344 if (pbuf->context == NULL)
2349 spec.latency_time = GST_AUDIO_BASE_SINK (psink)->latency_time;
2350 if (!gst_audio_ring_buffer_parse_caps (&spec, caps))
2353 if (!gst_pulse_fill_format_info (&spec, &format, &channels))
2356 /* Make sure input is framed (one frame per buffer) and can be payloaded */
2357 if (!pa_format_info_is_pcm (format)) {
2358 gboolean framed = FALSE, parsed = FALSE;
2359 st = gst_caps_get_structure (caps, 0);
2361 gst_structure_get_boolean (st, "framed", &framed);
2362 gst_structure_get_boolean (st, "parsed", &parsed);
2363 if ((!framed && !parsed) || gst_audio_iec61937_frame_size (&spec) <= 0)
2367 /* initialize the channel map */
2368 if (pa_format_info_is_pcm (format) &&
2369 gst_pulse_gst_to_channel_map (&channel_map, &spec))
2370 pa_format_info_set_channel_map (format, &channel_map);
2372 if (pbuf->stream || pbuf->probe_stream) {
2373 /* We're already in PAUSED or above, so just reuse this stream to query
2374 * sink formats and use those. */
2376 const char *device_name = pa_stream_get_device_name (pbuf->stream ?
2377 pbuf->stream : pbuf->probe_stream);
2379 if (!(o = pa_context_get_sink_info_by_name (pbuf->context, device_name,
2380 gst_pulsesink_sink_info_cb, &device_info)))
2383 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2384 pa_threaded_mainloop_wait (mainloop);
2385 if (gst_pulsering_is_dead (psink, pbuf, FALSE))
2389 for (i = g_list_first (device_info.formats); i; i = g_list_next (i)) {
2390 if (pa_format_info_is_compatible ((pa_format_info *) i->data, format)) {
2396 /* We're in READY, let's connect a stream to see if the format is
2397 * accepted by whatever sink we're routed to */
2398 pbuf->probe_stream = gst_pulsesink_create_probe_stream (psink, pbuf,
2400 if (pbuf->probe_stream)
2406 pa_format_info_free (format);
2408 free_device_info (&device_info);
2411 pa_operation_unref (o);
2413 pa_threaded_mainloop_unlock (mainloop);
2414 GST_OBJECT_UNLOCK (pbuf);
2416 gst_caps_replace (&spec.caps, NULL);
2417 gst_object_unref (pbuf);
2425 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2426 ("pa_context_get_sink_input_info() failed: %s",
2427 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2433 gst_pulsesink_init (GstPulseSink * pulsesink)
2435 #if defined(__TIZEN__) && defined(PCM_DUMP_ENABLE)
2436 GstPad *sinkpad = NULL;
2438 #endif /* __TIZEN__ && PCM_DUMP_ENABLE */
2440 pulsesink->server = NULL;
2441 pulsesink->device = NULL;
2442 pulsesink->device_info.description = NULL;
2443 pulsesink->client_name = gst_pulse_client_name ();
2445 pulsesink->device_info.formats = NULL;
2447 pulsesink->volume = DEFAULT_VOLUME;
2448 pulsesink->volume_set = FALSE;
2450 pulsesink->mute = DEFAULT_MUTE;
2451 pulsesink->mute_set = FALSE;
2453 pulsesink->notify = 0;
2455 g_atomic_int_set (&pulsesink->format_lost, FALSE);
2456 pulsesink->format_lost_time = GST_CLOCK_TIME_NONE;
2458 pulsesink->properties = NULL;
2459 pulsesink->proplist = NULL;
2461 pulsesink->latency = g_strdup (DEFAULT_AUDIO_LATENCY);
2462 pulsesink->proplist = pa_proplist_new();
2463 pa_proplist_sets(pulsesink->proplist, PA_PROP_MEDIA_TIZEN_AUDIO_LATENCY, pulsesink->latency);
2464 #ifdef PCM_DUMP_ENABLE
2465 if (vconf_get_int(GST_PULSESINK_DUMP_VCONF_KEY, &vconf_dump)) {
2466 GST_WARNING("vconf_get_int %s failed", GST_PULSESINK_DUMP_VCONF_KEY);
2468 pulsesink->need_dump_input = vconf_dump & GST_PULSESINK_DUMP_INPUT_FLAG ? TRUE : FALSE;
2469 pulsesink->dump_fd_input = NULL;
2470 if (pulsesink->need_dump_input) {
2471 sinkpad = gst_element_get_static_pad((GstElement *)pulsesink, "sink");
2473 gst_pad_add_buffer_probe (sinkpad, G_CALLBACK (gst_pulsesink_pad_dump_handler), pulsesink);
2474 gst_object_unref (GST_OBJECT(sinkpad));
2478 #endif /* __TIZEN__ */
2480 /* override with a custom clock */
2481 if (GST_AUDIO_BASE_SINK (pulsesink)->provided_clock)
2482 gst_object_unref (GST_AUDIO_BASE_SINK (pulsesink)->provided_clock);
2484 GST_AUDIO_BASE_SINK (pulsesink)->provided_clock =
2485 gst_audio_clock_new ("GstPulseSinkClock",
2486 (GstAudioClockGetTimeFunc) gst_pulsesink_get_time, pulsesink, NULL);
2490 gst_pulsesink_finalize (GObject * object)
2492 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2494 g_free (pulsesink->server);
2495 g_free (pulsesink->device);
2496 g_free (pulsesink->client_name);
2497 g_free (pulsesink->current_sink_name);
2499 free_device_info (&pulsesink->device_info);
2501 if (pulsesink->properties)
2502 gst_structure_free (pulsesink->properties);
2503 if (pulsesink->proplist)
2504 pa_proplist_free (pulsesink->proplist);
2507 g_free (pulsesink->latency);
2508 #endif /* __TIZEN__ */
2510 G_OBJECT_CLASS (parent_class)->finalize (object);
2514 gst_pulsesink_set_volume (GstPulseSink * psink, gdouble volume)
2517 pa_operation *o = NULL;
2518 GstPulseRingBuffer *pbuf;
2524 pa_threaded_mainloop_lock (mainloop);
2526 GST_DEBUG_OBJECT (psink, "setting volume to %f", volume);
2528 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2529 if (pbuf == NULL || pbuf->stream == NULL)
2532 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2536 gst_pulse_cvolume_from_linear (&v, pbuf->channels, volume);
2538 /* FIXME: this will eventually be superceded by checks to see if the volume
2539 * is readable/writable */
2542 if (!(o = pa_context_set_sink_input_volume (pbuf->context, idx,
2546 /* We don't really care about the result of this call */
2550 pa_operation_unref (o);
2552 pa_threaded_mainloop_unlock (mainloop);
2559 psink->volume = volume;
2560 psink->volume_set = TRUE;
2562 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2567 psink->volume = volume;
2568 psink->volume_set = TRUE;
2570 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2575 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2580 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2581 ("pa_stream_set_sink_input_volume() failed: %s",
2582 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2588 gst_pulsesink_set_mute (GstPulseSink * psink, gboolean mute)
2590 pa_operation *o = NULL;
2591 GstPulseRingBuffer *pbuf;
2597 pa_threaded_mainloop_lock (mainloop);
2599 GST_DEBUG_OBJECT (psink, "setting mute state to %d", mute);
2601 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2602 if (pbuf == NULL || pbuf->stream == NULL)
2605 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2608 if (!(o = pa_context_set_sink_input_mute (pbuf->context, idx,
2612 /* We don't really care about the result of this call */
2616 pa_operation_unref (o);
2618 pa_threaded_mainloop_unlock (mainloop);
2626 psink->mute_set = TRUE;
2628 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2634 psink->mute_set = TRUE;
2636 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2641 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2646 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2647 ("pa_stream_set_sink_input_mute() failed: %s",
2648 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2654 gst_pulsesink_sink_input_info_cb (pa_context * c, const pa_sink_input_info * i,
2655 int eol, void *userdata)
2657 GstPulseRingBuffer *pbuf;
2658 GstPulseSink *psink;
2660 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
2661 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
2669 /* If the index doesn't match our current stream,
2670 * it implies we just recreated the stream (caps change)
2672 if (i->index == pa_stream_get_index (pbuf->stream)) {
2673 psink->volume = pa_sw_volume_to_linear (pa_cvolume_max (&i->volume));
2674 psink->mute = i->mute;
2675 psink->current_sink_idx = i->sink;
2677 if (psink->volume > MAX_VOLUME) {
2678 GST_WARNING_OBJECT (psink, "Clipped volume from %f to %f", psink->volume,
2680 psink->volume = MAX_VOLUME;
2685 pa_threaded_mainloop_signal (mainloop, 0);
2689 gst_pulsesink_get_sink_input_info (GstPulseSink * psink, gdouble * volume,
2692 GstPulseRingBuffer *pbuf;
2693 pa_operation *o = NULL;
2699 pa_threaded_mainloop_lock (mainloop);
2701 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2702 if (pbuf == NULL || pbuf->stream == NULL)
2705 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2708 if (!(o = pa_context_get_sink_input_info (pbuf->context, idx,
2709 gst_pulsesink_sink_input_info_cb, pbuf)))
2712 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2713 pa_threaded_mainloop_wait (mainloop);
2714 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
2720 *volume = psink->volume;
2722 *mute = psink->mute;
2725 pa_operation_unref (o);
2727 pa_threaded_mainloop_unlock (mainloop);
2735 *volume = psink->volume;
2737 *mute = psink->mute;
2739 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2744 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2749 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2754 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2755 ("pa_context_get_sink_input_info() failed: %s",
2756 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2762 gst_pulsesink_current_sink_info_cb (pa_context * c, const pa_sink_info * i,
2763 int eol, void *userdata)
2765 GstPulseSink *psink;
2767 psink = GST_PULSESINK_CAST (userdata);
2772 /* If the index doesn't match our current stream,
2773 * it implies we just recreated the stream (caps change)
2775 if (i->index == psink->current_sink_idx) {
2776 g_free (psink->current_sink_name);
2777 psink->current_sink_name = g_strdup (i->name);
2781 pa_threaded_mainloop_signal (mainloop, 0);
2785 gst_pulsesink_get_current_device (GstPulseSink * pulsesink)
2787 pa_operation *o = NULL;
2788 GstPulseRingBuffer *pbuf;
2789 gchar *current_sink;
2795 GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (pulsesink)->ringbuffer);
2796 if (pbuf == NULL || pbuf->stream == NULL)
2799 gst_pulsesink_get_sink_input_info (pulsesink, NULL, NULL);
2801 pa_threaded_mainloop_lock (mainloop);
2803 if (!(o = pa_context_get_sink_info_by_index (pbuf->context,
2804 pulsesink->current_sink_idx, gst_pulsesink_current_sink_info_cb,
2808 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2809 pa_threaded_mainloop_wait (mainloop);
2810 if (gst_pulsering_is_dead (pulsesink, pbuf, TRUE))
2816 current_sink = g_strdup (pulsesink->current_sink_name);
2819 pa_operation_unref (o);
2821 pa_threaded_mainloop_unlock (mainloop);
2823 return current_sink;
2828 GST_DEBUG_OBJECT (pulsesink, "we have no mainloop");
2833 GST_DEBUG_OBJECT (pulsesink, "we have no ringbuffer");
2838 GST_ELEMENT_ERROR (pulsesink, RESOURCE, FAILED,
2839 ("pa_context_get_sink_input_info() failed: %s",
2840 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2846 gst_pulsesink_device_description (GstPulseSink * psink)
2848 GstPulseRingBuffer *pbuf;
2849 pa_operation *o = NULL;
2855 pa_threaded_mainloop_lock (mainloop);
2856 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2860 free_device_info (&psink->device_info);
2861 if (!(o = pa_context_get_sink_info_by_name (pbuf->context,
2862 psink->device, gst_pulsesink_sink_info_cb, &psink->device_info)))
2865 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2866 pa_threaded_mainloop_wait (mainloop);
2867 if (gst_pulsering_is_dead (psink, pbuf, FALSE))
2873 pa_operation_unref (o);
2875 t = g_strdup (psink->device_info.description);
2876 pa_threaded_mainloop_unlock (mainloop);
2883 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2888 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2893 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2894 ("pa_context_get_sink_info_by_index() failed: %s",
2895 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2901 gst_pulsesink_set_stream_device (GstPulseSink * psink, const gchar * device)
2903 pa_operation *o = NULL;
2904 GstPulseRingBuffer *pbuf;
2910 pa_threaded_mainloop_lock (mainloop);
2912 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2913 if (pbuf == NULL || pbuf->stream == NULL)
2916 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2920 GST_DEBUG_OBJECT (psink, "setting stream device to %s", device);
2922 if (!(o = pa_context_move_sink_input_by_name (pbuf->context, idx, device,
2929 pa_operation_unref (o);
2931 pa_threaded_mainloop_unlock (mainloop);
2938 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2943 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2948 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2953 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2954 ("pa_context_move_sink_input_by_name(%s) failed: %s", device,
2955 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2962 gst_pulsesink_set_property (GObject * object,
2963 guint prop_id, const GValue * value, GParamSpec * pspec)
2965 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2969 g_free (pulsesink->server);
2970 pulsesink->server = g_value_dup_string (value);
2973 g_free (pulsesink->device);
2974 pulsesink->device = g_value_dup_string (value);
2975 gst_pulsesink_set_stream_device (pulsesink, pulsesink->device);
2978 gst_pulsesink_set_volume (pulsesink, g_value_get_double (value));
2981 gst_pulsesink_set_mute (pulsesink, g_value_get_boolean (value));
2983 case PROP_CLIENT_NAME:
2984 g_free (pulsesink->client_name);
2985 if (!g_value_get_string (value)) {
2986 GST_WARNING_OBJECT (pulsesink,
2987 "Empty PulseAudio client name not allowed. Resetting to default value");
2988 pulsesink->client_name = gst_pulse_client_name ();
2990 pulsesink->client_name = g_value_dup_string (value);
2992 case PROP_STREAM_PROPERTIES:
2993 if (pulsesink->properties)
2994 gst_structure_free (pulsesink->properties);
2995 pulsesink->properties =
2996 gst_structure_copy (gst_value_get_structure (value));
2997 if (pulsesink->proplist)
2998 pa_proplist_free (pulsesink->proplist);
2999 pulsesink->proplist = gst_pulse_make_proplist (pulsesink->properties);
3002 case PROP_AUDIO_LATENCY:
3003 g_free (pulsesink->latency);
3004 pulsesink->latency = g_value_dup_string (value);
3005 /* setting NULL restores the default latency */
3006 if (pulsesink->latency == NULL) {
3007 pulsesink->latency = g_strdup (DEFAULT_AUDIO_LATENCY);
3009 if (!pulsesink->proplist) {
3010 pulsesink->proplist = pa_proplist_new();
3012 pa_proplist_sets(pulsesink->proplist, PA_PROP_MEDIA_TIZEN_AUDIO_LATENCY, pulsesink->latency);
3013 GST_DEBUG_OBJECT(pulsesink, "latency(%s)", pulsesink->latency);
3015 #endif /* __TIZEN__ */
3017 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3023 gst_pulsesink_get_property (GObject * object,
3024 guint prop_id, GValue * value, GParamSpec * pspec)
3027 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
3031 g_value_set_string (value, pulsesink->server);
3034 g_value_set_string (value, pulsesink->device);
3036 case PROP_CURRENT_DEVICE:
3038 gchar *current_device = gst_pulsesink_get_current_device (pulsesink);
3040 g_value_take_string (value, current_device);
3042 g_value_set_string (value, "");
3045 case PROP_DEVICE_NAME:
3046 g_value_take_string (value, gst_pulsesink_device_description (pulsesink));
3052 gst_pulsesink_get_sink_input_info (pulsesink, &volume, NULL);
3053 g_value_set_double (value, volume);
3060 gst_pulsesink_get_sink_input_info (pulsesink, NULL, &mute);
3061 g_value_set_boolean (value, mute);
3064 case PROP_CLIENT_NAME:
3065 g_value_set_string (value, pulsesink->client_name);
3067 case PROP_STREAM_PROPERTIES:
3068 gst_value_set_structure (value, pulsesink->properties);
3071 case PROP_AUDIO_LATENCY:
3072 g_value_set_string (value, pulsesink->latency);
3074 #endif /* __TIZEN__ */
3076 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3082 gst_pulsesink_change_title (GstPulseSink * psink, const gchar * t)
3084 pa_operation *o = NULL;
3085 GstPulseRingBuffer *pbuf;
3087 pa_threaded_mainloop_lock (mainloop);
3089 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
3091 if (pbuf == NULL || pbuf->stream == NULL)
3094 g_free (pbuf->stream_name);
3095 pbuf->stream_name = g_strdup (t);
3097 if (!(o = pa_stream_set_name (pbuf->stream, pbuf->stream_name, NULL, NULL)))
3100 /* We're not interested if this operation failed or not */
3104 pa_operation_unref (o);
3105 pa_threaded_mainloop_unlock (mainloop);
3112 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
3117 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
3118 ("pa_stream_set_name() failed: %s",
3119 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
3125 gst_pulsesink_change_props (GstPulseSink * psink, GstTagList * l)
3127 static const gchar *const map[] = {
3128 GST_TAG_TITLE, PA_PROP_MEDIA_TITLE,
3130 /* might get overriden in the next iteration by GST_TAG_ARTIST */
3131 GST_TAG_PERFORMER, PA_PROP_MEDIA_ARTIST,
3133 GST_TAG_ARTIST, PA_PROP_MEDIA_ARTIST,
3134 GST_TAG_LANGUAGE_CODE, PA_PROP_MEDIA_LANGUAGE,
3135 GST_TAG_LOCATION, PA_PROP_MEDIA_FILENAME,
3136 /* We might add more here later on ... */
3139 pa_proplist *pl = NULL;
3140 const gchar *const *t;
3141 gboolean empty = TRUE;
3142 pa_operation *o = NULL;
3143 GstPulseRingBuffer *pbuf;
3145 pl = pa_proplist_new ();
3147 for (t = map; *t; t += 2) {
3150 if (gst_tag_list_get_string (l, *t, &n)) {
3153 pa_proplist_sets (pl, *(t + 1), n);
3163 pa_threaded_mainloop_lock (mainloop);
3164 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
3165 if (pbuf == NULL || pbuf->stream == NULL)
3168 /* We're not interested if this operation failed or not */
3169 if (!(o = pa_stream_proplist_update (pbuf->stream, PA_UPDATE_REPLACE,
3171 GST_DEBUG_OBJECT (psink, "pa_stream_proplist_update() failed");
3177 pa_operation_unref (o);
3179 pa_threaded_mainloop_unlock (mainloop);
3184 pa_proplist_free (pl);
3191 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
3197 gst_pulsesink_flush_ringbuffer (GstPulseSink * psink)
3199 GstPulseRingBuffer *pbuf;
3201 pa_threaded_mainloop_lock (mainloop);
3203 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
3205 if (pbuf == NULL || pbuf->stream == NULL)
3208 gst_pulsering_flush (pbuf);
3210 /* Uncork if we haven't already (happens when waiting to get enough data
3211 * to send out the first time) */
3213 gst_pulsering_set_corked (pbuf, FALSE, FALSE);
3215 /* We're not interested if this operation failed or not */
3217 pa_threaded_mainloop_unlock (mainloop);
3224 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
3230 gst_pulsesink_event (GstBaseSink * sink, GstEvent * event)
3232 GstPulseSink *pulsesink = GST_PULSESINK_CAST (sink);
3234 switch (GST_EVENT_TYPE (event)) {
3235 case GST_EVENT_TAG:{
3236 gchar *title = NULL, *artist = NULL, *location = NULL, *description =
3237 NULL, *t = NULL, *buf = NULL;
3240 gst_event_parse_tag (event, &l);
3242 gst_tag_list_get_string (l, GST_TAG_TITLE, &title);
3243 gst_tag_list_get_string (l, GST_TAG_ARTIST, &artist);
3244 gst_tag_list_get_string (l, GST_TAG_LOCATION, &location);
3245 gst_tag_list_get_string (l, GST_TAG_DESCRIPTION, &description);
3248 gst_tag_list_get_string (l, GST_TAG_PERFORMER, &artist);
3250 if (title && artist)
3251 /* TRANSLATORS: 'song title' by 'artist name' */
3252 t = buf = g_strdup_printf (_("'%s' by '%s'"), g_strstrip (title),
3253 g_strstrip (artist));
3255 t = g_strstrip (title);
3256 else if (description)
3257 t = g_strstrip (description);
3259 t = g_strstrip (location);
3262 gst_pulsesink_change_title (pulsesink, t);
3267 g_free (description);
3270 gst_pulsesink_change_props (pulsesink, l);
3274 case GST_EVENT_GAP:{
3275 GstClockTime timestamp, duration;
3277 gst_event_parse_gap (event, ×tamp, &duration);
3278 if (duration == GST_CLOCK_TIME_NONE)
3279 gst_pulsesink_flush_ringbuffer (pulsesink);
3283 gst_pulsesink_flush_ringbuffer (pulsesink);
3289 return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
3293 gst_pulsesink_query (GstBaseSink * sink, GstQuery * query)
3295 GstPulseSink *pulsesink = GST_PULSESINK_CAST (sink);
3296 gboolean ret = FALSE;
3298 switch (GST_QUERY_TYPE (query)) {
3299 case GST_QUERY_CAPS:
3301 GstCaps *caps, *filter;
3303 gst_query_parse_caps (query, &filter);
3304 caps = gst_pulsesink_query_getcaps (pulsesink, filter);
3307 gst_query_set_caps_result (query, caps);
3308 gst_caps_unref (caps);
3313 case GST_QUERY_ACCEPT_CAPS:
3317 gst_query_parse_accept_caps (query, &caps);
3318 ret = gst_pulsesink_query_acceptcaps (pulsesink, caps);
3319 gst_query_set_accept_caps_result (query, ret);
3324 ret = GST_BASE_SINK_CLASS (parent_class)->query (sink, query);
3331 gst_pulsesink_release_mainloop (GstPulseSink * psink)
3336 pa_threaded_mainloop_lock (mainloop);
3337 while (psink->defer_pending) {
3338 GST_DEBUG_OBJECT (psink, "waiting for stream status message emission");
3339 pa_threaded_mainloop_wait (mainloop);
3341 pa_threaded_mainloop_unlock (mainloop);
3343 g_mutex_lock (&pa_shared_resource_mutex);
3345 if (!mainloop_ref_ct) {
3346 GST_INFO_OBJECT (psink, "terminating pa main loop thread");
3347 pa_threaded_mainloop_stop (mainloop);
3348 pa_threaded_mainloop_free (mainloop);
3351 g_mutex_unlock (&pa_shared_resource_mutex);
3354 static GstStateChangeReturn
3355 gst_pulsesink_change_state (GstElement * element, GstStateChange transition)
3357 GstPulseSink *pulsesink = GST_PULSESINK (element);
3358 GstStateChangeReturn ret;
3360 switch (transition) {
3361 case GST_STATE_CHANGE_NULL_TO_READY:
3362 g_mutex_lock (&pa_shared_resource_mutex);
3363 if (!mainloop_ref_ct) {
3364 GST_INFO_OBJECT (element, "new pa main loop thread");
3365 if (!(mainloop = pa_threaded_mainloop_new ()))
3366 goto mainloop_failed;
3367 if (pa_threaded_mainloop_start (mainloop) < 0) {
3368 pa_threaded_mainloop_free (mainloop);
3369 goto mainloop_start_failed;
3371 mainloop_ref_ct = 1;
3372 g_mutex_unlock (&pa_shared_resource_mutex);
3374 GST_INFO_OBJECT (element, "reusing pa main loop thread");
3376 g_mutex_unlock (&pa_shared_resource_mutex);
3379 case GST_STATE_CHANGE_READY_TO_PAUSED:
3380 gst_element_post_message (element,
3381 gst_message_new_clock_provide (GST_OBJECT_CAST (element),
3382 GST_AUDIO_BASE_SINK (pulsesink)->provided_clock, TRUE));
3389 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3390 if (ret == GST_STATE_CHANGE_FAILURE)
3393 switch (transition) {
3394 case GST_STATE_CHANGE_PAUSED_TO_READY:
3395 /* format_lost is reset in release() in audiobasesink */
3396 gst_element_post_message (element,
3397 gst_message_new_clock_lost (GST_OBJECT_CAST (element),
3398 GST_AUDIO_BASE_SINK (pulsesink)->provided_clock));
3400 case GST_STATE_CHANGE_READY_TO_NULL:
3401 gst_pulsesink_release_mainloop (pulsesink);
3412 g_mutex_unlock (&pa_shared_resource_mutex);
3413 GST_ELEMENT_ERROR (pulsesink, RESOURCE, FAILED,
3414 ("pa_threaded_mainloop_new() failed"), (NULL));
3415 return GST_STATE_CHANGE_FAILURE;
3417 mainloop_start_failed:
3419 g_mutex_unlock (&pa_shared_resource_mutex);
3420 GST_ELEMENT_ERROR (pulsesink, RESOURCE, FAILED,
3421 ("pa_threaded_mainloop_start() failed"), (NULL));
3422 return GST_STATE_CHANGE_FAILURE;
3426 if (transition == GST_STATE_CHANGE_NULL_TO_READY) {
3427 /* Clear the PA mainloop if audiobasesink failed to open the ring_buffer */
3428 g_assert (mainloop);
3429 gst_pulsesink_release_mainloop (pulsesink);