1 /*-*- Mode: C; c-basic-offset: 2 -*-*/
2
3 /* GStreamer pulseaudio plugin
4 *
5 * Copyright (c) 2004-2008 Lennart Poettering
6 * (c) 2009 Wim Taymans
7 *
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.
12 *
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.
17 *
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
21 * USA.
22 */
23
24 /**
25 * SECTION:element-pulsesink
26 * @title: pulsesink
27 * @see_also: pulsesrc
28 *
29 * This element outputs audio to a
30 * [PulseAudio sound server](http://www.pulseaudio.org).
31 *
32 * ## Example pipelines
33 * |[
34 * gst-launch-1.0 -v filesrc location=sine.ogg ! oggdemux ! vorbisdec ! audioconvert ! audioresample ! pulsesink
35 * ]| Play an Ogg/Vorbis file.
36 * |[
37 * gst-launch-1.0 -v audiotestsrc ! audioconvert ! volume volume=0.4 ! pulsesink
38 * ]| Play a 440Hz sine wave.
39 * |[
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
42 * with "pactl list".
43 *
44 */
45
46 #ifdef HAVE_CONFIG_H
47 #include "config.h"
48 #endif
49
50 #include <string.h>
51 #include <stdio.h>
52
53 #include <gst/base/gstbasesink.h>
54 #include <gst/gsttaglist.h>
55 #include <gst/audio/audio.h>
56 #include <gst/gst-i18n-plugin.h>
57
58 #include <gst/pbutils/pbutils.h> /* only used for GST_PLUGINS_BASE_VERSION_* */
59
60 #include <gst/glib-compat-private.h>
61
62 #include "gstpulseelements.h"
63 #include "pulsesink.h"
64 #include "pulseutil.h"
65
66 GST_DEBUG_CATEGORY_EXTERN (pulse_debug);
67 #define GST_CAT_DEFAULT pulse_debug
68
69 #define DEFAULT_SERVER NULL
70 #define DEFAULT_DEVICE NULL
71 #define DEFAULT_CURRENT_DEVICE NULL
72 #define DEFAULT_DEVICE_NAME NULL
73 #define DEFAULT_VOLUME 1.0
74 #define DEFAULT_MUTE FALSE
75 #define MAX_VOLUME 10.0
76
77 enum
78 {
79 PROP_0,
80 PROP_SERVER,
81 PROP_DEVICE,
82 PROP_CURRENT_DEVICE,
83 PROP_DEVICE_NAME,
84 PROP_VOLUME,
85 PROP_MUTE,
86 PROP_CLIENT_NAME,
87 PROP_STREAM_PROPERTIES,
88 PROP_LAST
89 };
90
91 #define GST_TYPE_PULSERING_BUFFER \
92 (gst_pulseringbuffer_get_type())
93 #define GST_PULSERING_BUFFER(obj) \
94 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_PULSERING_BUFFER,GstPulseRingBuffer))
95 #define GST_PULSERING_BUFFER_CLASS(klass) \
96 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_PULSERING_BUFFER,GstPulseRingBufferClass))
97 #define GST_PULSERING_BUFFER_GET_CLASS(obj) \
98 (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_PULSERING_BUFFER, GstPulseRingBufferClass))
99 #define GST_PULSERING_BUFFER_CAST(obj) \
100 ((GstPulseRingBuffer *)obj)
101 #define GST_IS_PULSERING_BUFFER(obj) \
102 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_PULSERING_BUFFER))
103 #define GST_IS_PULSERING_BUFFER_CLASS(klass)\
104 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_PULSERING_BUFFER))
105
106 typedef struct _GstPulseRingBuffer GstPulseRingBuffer;
107 typedef struct _GstPulseRingBufferClass GstPulseRingBufferClass;
108
109 typedef struct _GstPulseContext GstPulseContext;
110
111 /* A note on threading.
112 *
113 * We use a pa_threaded_mainloop to interact with the PulseAudio server. This
114 * starts up a separate thread that runs a mainloop to carry back events,
115 * messages and timing updates from the PulseAudio server.
116 *
117 * In most cases, the PulseAudio API we use communicates with the server and
118 * processes replies asynchronously. Operations on PA objects that result in
119 * such communication are protected with a pa_threaded_mainloop_lock() and
120 * pa_threaded_mainloop_unlock(). These guarantee mutual exclusion with the
121 * mainloop thread -- when an iteration of the mainloop thread begins, it first
122 * tries to acquire this lock, and cannot do so if our code also holds that
123 * lock.
124 *
125 * When we need to complete an operation synchronously, we use
126 * pa_threaded_mainloop_wait() and pa_threaded_mainloop_signal(). These work
127 * much as pthread conditionals do. pa_threaded_mainloop_wait() is called with
128 * the mainloop lock held. It releases the lock (thereby allowing the mainloop
129 * to execute), and waits till one of our callbacks to be executed by the
130 * mainloop thread calls pa_threaded_mainloop_signal(). At the end of the
131 * mainloop iteration, the pa_threaded_mainloop_wait() will reacquire the
132 * mainloop lock and return control to the caller.
133 */
134
135 /* Store the PA contexts in a hash table to allow easy sharing among
136 * multiple instances of the sink. Keys are $context_name@$server_name
137 * (strings) and values should be GstPulseContext pointers.
138 */
139 struct _GstPulseContext
140 {
141 pa_context *context;
142 GSList *ring_buffers;
143 };
144
145 static GHashTable *gst_pulse_shared_contexts = NULL;
146
147 /* use one static main-loop for all instances
148 * this is needed to make the context sharing work as the contexts are
149 * released when releasing their parent main-loop
150 */
151 static pa_threaded_mainloop *mainloop = NULL;
152 static guint mainloop_ref_ct = 0;
153
154 /* lock for access to shared resources */
155 static GMutex pa_shared_resource_mutex;
156
157 /* We keep a custom ringbuffer that is backed up by data allocated by
158 * pulseaudio. We must also override the commit function to write into
159 * pulseaudio memory instead. */
160 struct _GstPulseRingBuffer
161 {
162 GstAudioRingBuffer object;
163
164 gchar *context_name;
165 gchar *stream_name;
166
167 pa_context *context;
168 pa_stream *stream;
169 pa_stream *probe_stream;
170
171 pa_format_info *format;
172 guint channels;
173 gboolean is_pcm;
174
175 void *m_data;
176 size_t m_towrite;
177 size_t m_writable;
178 gint64 m_offset;
179 gint64 m_lastoffset;
180
181 gboolean corked:1;
182 gboolean in_commit:1;
183 gboolean paused:1;
184 };
185 struct _GstPulseRingBufferClass
186 {
187 GstAudioRingBufferClass parent_class;
188 };
189
190 static GType gst_pulseringbuffer_get_type (void);
191 static void gst_pulseringbuffer_finalize (GObject * object);
192
193 static GstAudioRingBufferClass *ring_parent_class = NULL;
194
195 static gboolean gst_pulseringbuffer_open_device (GstAudioRingBuffer * buf);
196 static gboolean gst_pulseringbuffer_close_device (GstAudioRingBuffer * buf);
197 static gboolean gst_pulseringbuffer_acquire (GstAudioRingBuffer * buf,
198 GstAudioRingBufferSpec * spec);
199 static gboolean gst_pulseringbuffer_release (GstAudioRingBuffer * buf);
200 static gboolean gst_pulseringbuffer_start (GstAudioRingBuffer * buf);
201 static gboolean gst_pulseringbuffer_pause (GstAudioRingBuffer * buf);
202 static gboolean gst_pulseringbuffer_stop (GstAudioRingBuffer * buf);
203 static void gst_pulseringbuffer_clear (GstAudioRingBuffer * buf);
204 static guint gst_pulseringbuffer_commit (GstAudioRingBuffer * buf,
205 guint64 * sample, guchar * data, gint in_samples, gint out_samples,
206 gint * accum);
207
208 G_DEFINE_TYPE (GstPulseRingBuffer, gst_pulseringbuffer,
209 GST_TYPE_AUDIO_RING_BUFFER);
210
211 static void
gst_pulsesink_init_contexts(void)212 gst_pulsesink_init_contexts (void)
213 {
214 g_mutex_init (&pa_shared_resource_mutex);
215 gst_pulse_shared_contexts = g_hash_table_new_full (g_str_hash, g_str_equal,
216 g_free, NULL);
217 }
218
219 static void
gst_pulseringbuffer_class_init(GstPulseRingBufferClass * klass)220 gst_pulseringbuffer_class_init (GstPulseRingBufferClass * klass)
221 {
222 GObjectClass *gobject_class;
223 GstAudioRingBufferClass *gstringbuffer_class;
224
225 gobject_class = (GObjectClass *) klass;
226 gstringbuffer_class = (GstAudioRingBufferClass *) klass;
227
228 ring_parent_class = g_type_class_peek_parent (klass);
229
230 gobject_class->finalize = gst_pulseringbuffer_finalize;
231
232 gstringbuffer_class->open_device =
233 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_open_device);
234 gstringbuffer_class->close_device =
235 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_close_device);
236 gstringbuffer_class->acquire =
237 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_acquire);
238 gstringbuffer_class->release =
239 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_release);
240 gstringbuffer_class->start = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_start);
241 gstringbuffer_class->pause = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_pause);
242 gstringbuffer_class->resume = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_start);
243 gstringbuffer_class->stop = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_stop);
244 gstringbuffer_class->clear_all =
245 GST_DEBUG_FUNCPTR (gst_pulseringbuffer_clear);
246
247 gstringbuffer_class->commit = GST_DEBUG_FUNCPTR (gst_pulseringbuffer_commit);
248 }
249
250 static void
gst_pulseringbuffer_init(GstPulseRingBuffer * pbuf)251 gst_pulseringbuffer_init (GstPulseRingBuffer * pbuf)
252 {
253 pbuf->stream_name = NULL;
254 pbuf->context = NULL;
255 pbuf->stream = NULL;
256 pbuf->probe_stream = NULL;
257
258 pbuf->format = NULL;
259 pbuf->channels = 0;
260 pbuf->is_pcm = FALSE;
261
262 pbuf->m_data = NULL;
263 pbuf->m_towrite = 0;
264 pbuf->m_writable = 0;
265 pbuf->m_offset = 0;
266 pbuf->m_lastoffset = 0;
267
268 pbuf->corked = TRUE;
269 pbuf->in_commit = FALSE;
270 pbuf->paused = FALSE;
271 }
272
273 /* Call with mainloop lock held if wait == TRUE) */
274 static void
gst_pulse_destroy_stream(pa_stream * stream,gboolean wait)275 gst_pulse_destroy_stream (pa_stream * stream, gboolean wait)
276 {
277 /* Make sure we don't get any further callbacks */
278 pa_stream_set_write_callback (stream, NULL, NULL);
279 pa_stream_set_underflow_callback (stream, NULL, NULL);
280 pa_stream_set_overflow_callback (stream, NULL, NULL);
281
282 pa_stream_disconnect (stream);
283
284 if (wait)
285 pa_threaded_mainloop_wait (mainloop);
286
287 pa_stream_set_state_callback (stream, NULL, NULL);
288 pa_stream_unref (stream);
289 }
290
291 static void
gst_pulsering_destroy_stream(GstPulseRingBuffer * pbuf)292 gst_pulsering_destroy_stream (GstPulseRingBuffer * pbuf)
293 {
294 if (pbuf->probe_stream) {
295 gst_pulse_destroy_stream (pbuf->probe_stream, FALSE);
296 pbuf->probe_stream = NULL;
297 }
298
299 if (pbuf->stream) {
300
301 if (pbuf->m_data) {
302 /* drop shm memory buffer */
303 pa_stream_cancel_write (pbuf->stream);
304
305 /* reset internal variables */
306 pbuf->m_data = NULL;
307 pbuf->m_towrite = 0;
308 pbuf->m_writable = 0;
309 pbuf->m_offset = 0;
310 pbuf->m_lastoffset = 0;
311 }
312 if (pbuf->format) {
313 pa_format_info_free (pbuf->format);
314 pbuf->format = NULL;
315 pbuf->channels = 0;
316 pbuf->is_pcm = FALSE;
317 }
318
319 pa_stream_disconnect (pbuf->stream);
320
321 /* Make sure we don't get any further callbacks */
322 pa_stream_set_state_callback (pbuf->stream, NULL, NULL);
323 pa_stream_set_write_callback (pbuf->stream, NULL, NULL);
324 pa_stream_set_underflow_callback (pbuf->stream, NULL, NULL);
325 pa_stream_set_overflow_callback (pbuf->stream, NULL, NULL);
326
327 pa_stream_unref (pbuf->stream);
328 pbuf->stream = NULL;
329 }
330
331 g_free (pbuf->stream_name);
332 pbuf->stream_name = NULL;
333 }
334
335 static void
gst_pulsering_destroy_context(GstPulseRingBuffer * pbuf)336 gst_pulsering_destroy_context (GstPulseRingBuffer * pbuf)
337 {
338 g_mutex_lock (&pa_shared_resource_mutex);
339
340 GST_DEBUG_OBJECT (pbuf, "destroying ringbuffer %p", pbuf);
341
342 gst_pulsering_destroy_stream (pbuf);
343
344 if (pbuf->context) {
345 pa_context_unref (pbuf->context);
346 pbuf->context = NULL;
347 }
348
349 if (pbuf->context_name) {
350 GstPulseContext *pctx;
351
352 pctx = g_hash_table_lookup (gst_pulse_shared_contexts, pbuf->context_name);
353
354 GST_DEBUG_OBJECT (pbuf, "releasing context with name %s, pbuf=%p, pctx=%p",
355 pbuf->context_name, pbuf, pctx);
356
357 if (pctx) {
358 pctx->ring_buffers = g_slist_remove (pctx->ring_buffers, pbuf);
359 if (pctx->ring_buffers == NULL) {
360 GST_DEBUG_OBJECT (pbuf,
361 "destroying final context with name %s, pbuf=%p, pctx=%p",
362 pbuf->context_name, pbuf, pctx);
363
364 pa_context_disconnect (pctx->context);
365
366 /* Make sure we don't get any further callbacks */
367 pa_context_set_state_callback (pctx->context, NULL, NULL);
368 pa_context_set_subscribe_callback (pctx->context, NULL, NULL);
369
370 g_hash_table_remove (gst_pulse_shared_contexts, pbuf->context_name);
371
372 pa_context_unref (pctx->context);
373 g_slice_free (GstPulseContext, pctx);
374 }
375 }
376 g_free (pbuf->context_name);
377 pbuf->context_name = NULL;
378 }
379 g_mutex_unlock (&pa_shared_resource_mutex);
380 }
381
382 static void
gst_pulseringbuffer_finalize(GObject * object)383 gst_pulseringbuffer_finalize (GObject * object)
384 {
385 GstPulseRingBuffer *ringbuffer;
386
387 ringbuffer = GST_PULSERING_BUFFER_CAST (object);
388
389 gst_pulsering_destroy_context (ringbuffer);
390 G_OBJECT_CLASS (ring_parent_class)->finalize (object);
391 }
392
393
394 #define CONTEXT_OK(c) ((c) && PA_CONTEXT_IS_GOOD (pa_context_get_state ((c))))
395 #define STREAM_OK(s) ((s) && PA_STREAM_IS_GOOD (pa_stream_get_state ((s))))
396
397 static gboolean
gst_pulsering_is_dead(GstPulseSink * psink,GstPulseRingBuffer * pbuf,gboolean check_stream)398 gst_pulsering_is_dead (GstPulseSink * psink, GstPulseRingBuffer * pbuf,
399 gboolean check_stream)
400 {
401 if (!CONTEXT_OK (pbuf->context))
402 goto error;
403
404 if (check_stream && !STREAM_OK (pbuf->stream))
405 goto error;
406
407 return FALSE;
408
409 error:
410 {
411 const gchar *err_str =
412 pbuf->context ? pa_strerror (pa_context_errno (pbuf->context)) : NULL;
413 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED, ("Disconnected: %s",
414 err_str), (NULL));
415 return TRUE;
416 }
417 }
418
419 static void
gst_pulsering_context_state_cb(pa_context * c,void * userdata)420 gst_pulsering_context_state_cb (pa_context * c, void *userdata)
421 {
422 pa_context_state_t state;
423 pa_threaded_mainloop *mainloop = (pa_threaded_mainloop *) userdata;
424
425 state = pa_context_get_state (c);
426
427 GST_LOG ("got new context state %d", state);
428
429 switch (state) {
430 case PA_CONTEXT_READY:
431 case PA_CONTEXT_TERMINATED:
432 case PA_CONTEXT_FAILED:
433 GST_LOG ("signaling");
434 pa_threaded_mainloop_signal (mainloop, 0);
435 break;
436
437 case PA_CONTEXT_UNCONNECTED:
438 case PA_CONTEXT_CONNECTING:
439 case PA_CONTEXT_AUTHORIZING:
440 case PA_CONTEXT_SETTING_NAME:
441 break;
442 }
443 }
444
445 static void
gst_pulsering_context_subscribe_cb(pa_context * c,pa_subscription_event_type_t t,uint32_t idx,void * userdata)446 gst_pulsering_context_subscribe_cb (pa_context * c,
447 pa_subscription_event_type_t t, uint32_t idx, void *userdata)
448 {
449 GstPulseSink *psink;
450 GstPulseContext *pctx = (GstPulseContext *) userdata;
451 GSList *walk;
452
453 if (t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT | PA_SUBSCRIPTION_EVENT_CHANGE) &&
454 t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT | PA_SUBSCRIPTION_EVENT_NEW))
455 return;
456
457 for (walk = pctx->ring_buffers; walk; walk = g_slist_next (walk)) {
458 GstPulseRingBuffer *pbuf = (GstPulseRingBuffer *) walk->data;
459 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
460
461 GST_LOG_OBJECT (psink, "type %04x, idx %u", t, idx);
462
463 if (!pbuf->stream)
464 continue;
465
466 if (idx != pa_stream_get_index (pbuf->stream))
467 continue;
468
469 if (psink->device && pbuf->is_pcm &&
470 !g_str_equal (psink->device,
471 pa_stream_get_device_name (pbuf->stream))) {
472 /* Underlying sink changed. And this is not a passthrough stream. Let's
473 * see if someone upstream wants to try to renegotiate. */
474 GstEvent *renego;
475
476 g_free (psink->device);
477 psink->device = g_strdup (pa_stream_get_device_name (pbuf->stream));
478
479 GST_INFO_OBJECT (psink, "emitting sink-changed");
480
481 /* FIXME: send reconfigure event instead and let decodebin/playbin
482 * handle that. Also take care of ac3 alignment. See "pulse-format-lost" */
483 renego = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
484 gst_structure_new_empty ("pulse-sink-changed"));
485
486 if (!gst_pad_push_event (GST_BASE_SINK (psink)->sinkpad, renego))
487 GST_DEBUG_OBJECT (psink, "Emitted sink-changed - nobody was listening");
488 }
489
490 /* Actually this event is also triggered when other properties of
491 * the stream change that are unrelated to the volume. However it is
492 * probably cheaper to signal the change here and check for the
493 * volume when the GObject property is read instead of querying it always. */
494
495 /* inform streaming thread to notify */
496 g_atomic_int_compare_and_exchange (&psink->notify, 0, 1);
497 }
498 }
499
500 /* will be called when the device should be opened. In this case we will connect
501 * to the server. We should not try to open any streams in this state. */
502 static gboolean
gst_pulseringbuffer_open_device(GstAudioRingBuffer * buf)503 gst_pulseringbuffer_open_device (GstAudioRingBuffer * buf)
504 {
505 GstPulseSink *psink;
506 GstPulseRingBuffer *pbuf;
507 GstPulseContext *pctx;
508 pa_mainloop_api *api;
509 gboolean need_unlock_shared;
510
511 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
512 pbuf = GST_PULSERING_BUFFER_CAST (buf);
513
514 g_assert (!pbuf->stream);
515 g_assert (psink->client_name);
516
517 if (psink->server)
518 pbuf->context_name = g_strdup_printf ("%s@%s", psink->client_name,
519 psink->server);
520 else
521 pbuf->context_name = g_strdup (psink->client_name);
522
523 pa_threaded_mainloop_lock (mainloop);
524
525 g_mutex_lock (&pa_shared_resource_mutex);
526 need_unlock_shared = TRUE;
527
528 pctx = g_hash_table_lookup (gst_pulse_shared_contexts, pbuf->context_name);
529 if (pctx == NULL) {
530 pctx = g_slice_new0 (GstPulseContext);
531
532 /* get the mainloop api and create a context */
533 GST_INFO_OBJECT (psink, "new context with name %s, pbuf=%p, pctx=%p",
534 pbuf->context_name, pbuf, pctx);
535 api = pa_threaded_mainloop_get_api (mainloop);
536 if (!(pctx->context = pa_context_new (api, pbuf->context_name)))
537 goto create_failed;
538
539 pctx->ring_buffers = g_slist_prepend (pctx->ring_buffers, pbuf);
540 g_hash_table_insert (gst_pulse_shared_contexts,
541 g_strdup (pbuf->context_name), (gpointer) pctx);
542 /* register some essential callbacks */
543 pa_context_set_state_callback (pctx->context,
544 gst_pulsering_context_state_cb, mainloop);
545 pa_context_set_subscribe_callback (pctx->context,
546 gst_pulsering_context_subscribe_cb, pctx);
547
548 /* try to connect to the server and wait for completion, we don't want to
549 * autospawn a daemon */
550 GST_LOG_OBJECT (psink, "connect to server %s",
551 GST_STR_NULL (psink->server));
552 if (pa_context_connect (pctx->context, psink->server,
553 PA_CONTEXT_NOAUTOSPAWN, NULL) < 0)
554 goto connect_failed;
555 } else {
556 GST_INFO_OBJECT (psink,
557 "reusing shared context with name %s, pbuf=%p, pctx=%p",
558 pbuf->context_name, pbuf, pctx);
559 pctx->ring_buffers = g_slist_prepend (pctx->ring_buffers, pbuf);
560 }
561
562 g_mutex_unlock (&pa_shared_resource_mutex);
563 need_unlock_shared = FALSE;
564
565 /* context created or shared okay */
566 pbuf->context = pa_context_ref (pctx->context);
567
568 for (;;) {
569 pa_context_state_t state;
570
571 state = pa_context_get_state (pbuf->context);
572
573 GST_LOG_OBJECT (psink, "context state is now %d", state);
574
575 if (!PA_CONTEXT_IS_GOOD (state))
576 goto connect_failed;
577
578 if (state == PA_CONTEXT_READY)
579 break;
580
581 /* Wait until the context is ready */
582 GST_LOG_OBJECT (psink, "waiting..");
583 pa_threaded_mainloop_wait (mainloop);
584 }
585
586 if (pa_context_get_server_protocol_version (pbuf->context) < 22) {
587 /* We need PulseAudio >= 1.0 on the server side for the extended API */
588 goto bad_server_version;
589 }
590
591 GST_LOG_OBJECT (psink, "opened the device");
592
593 pa_threaded_mainloop_unlock (mainloop);
594
595 return TRUE;
596
597 /* ERRORS */
598 unlock_and_fail:
599 {
600 if (need_unlock_shared)
601 g_mutex_unlock (&pa_shared_resource_mutex);
602 gst_pulsering_destroy_context (pbuf);
603 pa_threaded_mainloop_unlock (mainloop);
604 return FALSE;
605 }
606 create_failed:
607 {
608 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
609 ("Failed to create context"), (NULL));
610 g_slice_free (GstPulseContext, pctx);
611 goto unlock_and_fail;
612 }
613 connect_failed:
614 {
615 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED, ("Failed to connect: %s",
616 pa_strerror (pa_context_errno (pctx->context))), (NULL));
617 goto unlock_and_fail;
618 }
619 bad_server_version:
620 {
621 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED, ("PulseAudio server version "
622 "is too old."), (NULL));
623 goto unlock_and_fail;
624 }
625 }
626
627 /* close the device */
628 static gboolean
gst_pulseringbuffer_close_device(GstAudioRingBuffer * buf)629 gst_pulseringbuffer_close_device (GstAudioRingBuffer * buf)
630 {
631 GstPulseSink *psink;
632 GstPulseRingBuffer *pbuf;
633
634 pbuf = GST_PULSERING_BUFFER_CAST (buf);
635 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
636
637 GST_LOG_OBJECT (psink, "closing device");
638
639 pa_threaded_mainloop_lock (mainloop);
640 gst_pulsering_destroy_context (pbuf);
641 pa_threaded_mainloop_unlock (mainloop);
642
643 GST_LOG_OBJECT (psink, "closed device");
644
645 return TRUE;
646 }
647
648 static void
gst_pulsering_stream_state_cb(pa_stream * s,void * userdata)649 gst_pulsering_stream_state_cb (pa_stream * s, void *userdata)
650 {
651 GstPulseSink *psink;
652 GstPulseRingBuffer *pbuf;
653 pa_stream_state_t state;
654
655 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
656 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
657
658 state = pa_stream_get_state (s);
659 GST_LOG_OBJECT (psink, "got new stream state %d", state);
660
661 switch (state) {
662 case PA_STREAM_READY:
663 case PA_STREAM_FAILED:
664 case PA_STREAM_TERMINATED:
665 GST_LOG_OBJECT (psink, "signaling");
666 pa_threaded_mainloop_signal (mainloop, 0);
667 break;
668 case PA_STREAM_UNCONNECTED:
669 case PA_STREAM_CREATING:
670 break;
671 }
672 }
673
674 static void
gst_pulsering_stream_request_cb(pa_stream * s,size_t length,void * userdata)675 gst_pulsering_stream_request_cb (pa_stream * s, size_t length, void *userdata)
676 {
677 GstPulseSink *psink;
678 GstAudioRingBuffer *rbuf;
679 GstPulseRingBuffer *pbuf;
680
681 rbuf = GST_AUDIO_RING_BUFFER_CAST (userdata);
682 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
683 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
684
685 GST_LOG_OBJECT (psink, "got request for length %" G_GSIZE_FORMAT, length);
686
687 if (pbuf->in_commit && (length >= rbuf->spec.segsize)) {
688 /* only signal when we are waiting in the commit thread
689 * and got request for at least a segment */
690 pa_threaded_mainloop_signal (mainloop, 0);
691 }
692 }
693
694 static void
gst_pulsering_stream_underflow_cb(pa_stream * s,void * userdata)695 gst_pulsering_stream_underflow_cb (pa_stream * s, void *userdata)
696 {
697 GstPulseSink *psink;
698 GstPulseRingBuffer *pbuf;
699
700 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
701 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
702
703 GST_WARNING_OBJECT (psink, "Got underflow");
704 }
705
706 static void
gst_pulsering_stream_overflow_cb(pa_stream * s,void * userdata)707 gst_pulsering_stream_overflow_cb (pa_stream * s, void *userdata)
708 {
709 GstPulseSink *psink;
710 GstPulseRingBuffer *pbuf;
711
712 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
713 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
714
715 GST_WARNING_OBJECT (psink, "Got overflow");
716 }
717
718 static void
gst_pulsering_stream_latency_cb(pa_stream * s,void * userdata)719 gst_pulsering_stream_latency_cb (pa_stream * s, void *userdata)
720 {
721 GstPulseSink *psink;
722 GstPulseRingBuffer *pbuf;
723 GstAudioRingBuffer *ringbuf;
724 const pa_timing_info *info;
725 pa_usec_t sink_usec;
726
727 info = pa_stream_get_timing_info (s);
728
729 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
730 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
731 ringbuf = GST_AUDIO_RING_BUFFER (pbuf);
732
733 if (!info) {
734 GST_LOG_OBJECT (psink, "latency update (information unknown)");
735 return;
736 }
737
738 if (!info->read_index_corrupt) {
739 /* Update segdone based on the read index. segdone is of segment
740 * granularity, while the read index is at byte granularity. We take the
741 * ceiling while converting the latter to the former since it is more
742 * conservative to report that we've read more than we have than to report
743 * less. One concern here is that latency updates happen every 100ms, which
744 * means segdone is not updated very often, but increasing the update
745 * frequency would mean more communication overhead. */
746 g_atomic_int_set (&ringbuf->segdone,
747 (int) gst_util_uint64_scale_ceil (info->read_index, 1,
748 ringbuf->spec.segsize));
749 }
750
751 sink_usec = info->configured_sink_usec;
752
753 GST_LOG_OBJECT (psink,
754 "latency_update, %" G_GUINT64_FORMAT ", %d:%" G_GINT64_FORMAT ", %d:%"
755 G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT,
756 GST_TIMEVAL_TO_TIME (info->timestamp), info->write_index_corrupt,
757 info->write_index, info->read_index_corrupt, info->read_index,
758 info->sink_usec, sink_usec);
759 }
760
761 static void
gst_pulsering_stream_suspended_cb(pa_stream * p,void * userdata)762 gst_pulsering_stream_suspended_cb (pa_stream * p, void *userdata)
763 {
764 GstPulseSink *psink;
765 GstPulseRingBuffer *pbuf;
766
767 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
768 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
769
770 if (pa_stream_is_suspended (p))
771 GST_DEBUG_OBJECT (psink, "stream suspended");
772 else
773 GST_DEBUG_OBJECT (psink, "stream resumed");
774 }
775
776 static void
gst_pulsering_stream_started_cb(pa_stream * p,void * userdata)777 gst_pulsering_stream_started_cb (pa_stream * p, void *userdata)
778 {
779 GstPulseSink *psink;
780 GstPulseRingBuffer *pbuf;
781
782 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
783 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
784
785 GST_DEBUG_OBJECT (psink, "stream started");
786 }
787
788 static void
gst_pulsering_stream_event_cb(pa_stream * p,const char * name,pa_proplist * pl,void * userdata)789 gst_pulsering_stream_event_cb (pa_stream * p, const char *name,
790 pa_proplist * pl, void *userdata)
791 {
792 GstPulseSink *psink;
793 GstPulseRingBuffer *pbuf;
794
795 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
796 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
797
798 if (!strcmp (name, PA_STREAM_EVENT_REQUEST_CORK)) {
799 /* the stream wants to PAUSE, post a message for the application. */
800 GST_DEBUG_OBJECT (psink, "got request for CORK");
801 gst_element_post_message (GST_ELEMENT_CAST (psink),
802 gst_message_new_request_state (GST_OBJECT_CAST (psink),
803 GST_STATE_PAUSED));
804
805 } else if (!strcmp (name, PA_STREAM_EVENT_REQUEST_UNCORK)) {
806 GST_DEBUG_OBJECT (psink, "got request for UNCORK");
807 gst_element_post_message (GST_ELEMENT_CAST (psink),
808 gst_message_new_request_state (GST_OBJECT_CAST (psink),
809 GST_STATE_PLAYING));
810 } else if (!strcmp (name, PA_STREAM_EVENT_FORMAT_LOST)) {
811 GstEvent *renego;
812
813 if (g_atomic_int_get (&psink->format_lost)) {
814 /* Duplicate event before we're done reconfiguring, discard */
815 return;
816 }
817
818 GST_DEBUG_OBJECT (psink, "got FORMAT LOST");
819 g_atomic_int_set (&psink->format_lost, 1);
820 psink->format_lost_time = g_ascii_strtoull (pa_proplist_gets (pl,
821 "stream-time"), NULL, 0) * 1000;
822
823 g_free (psink->device);
824 psink->device = g_strdup (pa_proplist_gets (pl, "device"));
825
826 /* FIXME: send reconfigure event instead and let decodebin/playbin
827 * handle that. Also take care of ac3 alignment */
828 renego = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
829 gst_structure_new_empty ("pulse-format-lost"));
830
831 #if 0
832 if (g_str_equal (gst_structure_get_name (st), "audio/x-eac3")) {
833 GstStructure *event_st = gst_structure_new ("ac3parse-set-alignment",
834 "alignment", G_TYPE_STRING, pbin->dbin ? "frame" : "iec61937", NULL);
835
836 if (!gst_pad_push_event (pbin->sinkpad,
837 gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, event_st)))
838 GST_WARNING_OBJECT (pbin->sinkpad, "Could not update alignment");
839 }
840 #endif
841
842 if (!gst_pad_push_event (GST_BASE_SINK (psink)->sinkpad, renego)) {
843 /* Nobody handled the format change - emit an error */
844 GST_ELEMENT_ERROR (psink, STREAM, FORMAT, ("Sink format changed"),
845 ("Sink format changed"));
846 }
847 } else {
848 GST_DEBUG_OBJECT (psink, "got unknown event %s", name);
849 }
850 }
851
852 /* Called with the mainloop locked */
853 static gboolean
gst_pulsering_wait_for_stream_ready(GstPulseSink * psink,pa_stream * stream)854 gst_pulsering_wait_for_stream_ready (GstPulseSink * psink, pa_stream * stream)
855 {
856 pa_stream_state_t state;
857
858 for (;;) {
859 state = pa_stream_get_state (stream);
860
861 GST_LOG_OBJECT (psink, "stream state is now %d", state);
862
863 if (!PA_STREAM_IS_GOOD (state))
864 return FALSE;
865
866 if (state == PA_STREAM_READY)
867 return TRUE;
868
869 /* Wait until the stream is ready */
870 pa_threaded_mainloop_wait (mainloop);
871 }
872 }
873
874
875 /* This method should create a new stream of the given @spec. No playback should
876 * start yet so we start in the corked state. */
877 static gboolean
gst_pulseringbuffer_acquire(GstAudioRingBuffer * buf,GstAudioRingBufferSpec * spec)878 gst_pulseringbuffer_acquire (GstAudioRingBuffer * buf,
879 GstAudioRingBufferSpec * spec)
880 {
881 GstPulseSink *psink;
882 GstPulseRingBuffer *pbuf;
883 pa_buffer_attr wanted;
884 const pa_buffer_attr *actual;
885 pa_channel_map channel_map;
886 pa_operation *o = NULL;
887 pa_cvolume v;
888 pa_cvolume *pv = NULL;
889 pa_stream_flags_t flags;
890 const gchar *name;
891 GstAudioClock *clock;
892 pa_format_info *formats[1];
893 #ifndef GST_DISABLE_GST_DEBUG
894 gchar print_buf[PA_FORMAT_INFO_SNPRINT_MAX];
895 #endif
896
897 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (buf));
898 pbuf = GST_PULSERING_BUFFER_CAST (buf);
899
900 GST_LOG_OBJECT (psink, "creating sample spec");
901 /* convert the gstreamer sample spec to the pulseaudio format */
902 if (!gst_pulse_fill_format_info (spec, &pbuf->format, &pbuf->channels))
903 goto invalid_spec;
904 pbuf->is_pcm = pa_format_info_is_pcm (pbuf->format);
905
906 pa_threaded_mainloop_lock (mainloop);
907
908 /* we need a context and a no stream */
909 g_assert (pbuf->context);
910 g_assert (!pbuf->stream);
911
912 /* if we have a probe, disconnect it first so that if we're creating a
913 * compressed stream, it doesn't get blocked by a PCM stream */
914 if (pbuf->probe_stream) {
915 gst_pulse_destroy_stream (pbuf->probe_stream, TRUE);
916 pbuf->probe_stream = NULL;
917 }
918
919 /* enable event notifications */
920 GST_LOG_OBJECT (psink, "subscribing to context events");
921 if (!(o = pa_context_subscribe (pbuf->context,
922 PA_SUBSCRIPTION_MASK_SINK_INPUT, NULL, NULL)))
923 goto subscribe_failed;
924
925 pa_operation_unref (o);
926
927 /* initialize the channel map */
928 if (pbuf->is_pcm && gst_pulse_gst_to_channel_map (&channel_map, spec))
929 pa_format_info_set_channel_map (pbuf->format, &channel_map);
930
931 /* find a good name for the stream */
932 if (psink->stream_name)
933 name = psink->stream_name;
934 else
935 name = "Playback Stream";
936
937 /* create a stream */
938 formats[0] = pbuf->format;
939 if (!(pbuf->stream = pa_stream_new_extended (pbuf->context, name, formats, 1,
940 psink->proplist)))
941 goto stream_failed;
942
943 /* install essential callbacks */
944 pa_stream_set_state_callback (pbuf->stream,
945 gst_pulsering_stream_state_cb, pbuf);
946 pa_stream_set_write_callback (pbuf->stream,
947 gst_pulsering_stream_request_cb, pbuf);
948 pa_stream_set_underflow_callback (pbuf->stream,
949 gst_pulsering_stream_underflow_cb, pbuf);
950 pa_stream_set_overflow_callback (pbuf->stream,
951 gst_pulsering_stream_overflow_cb, pbuf);
952 pa_stream_set_latency_update_callback (pbuf->stream,
953 gst_pulsering_stream_latency_cb, pbuf);
954 pa_stream_set_suspended_callback (pbuf->stream,
955 gst_pulsering_stream_suspended_cb, pbuf);
956 pa_stream_set_started_callback (pbuf->stream,
957 gst_pulsering_stream_started_cb, pbuf);
958 pa_stream_set_event_callback (pbuf->stream,
959 gst_pulsering_stream_event_cb, pbuf);
960
961 /* buffering requirements. When setting prebuf to 0, the stream will not pause
962 * when we cause an underrun, which causes time to continue. */
963 memset (&wanted, 0, sizeof (wanted));
964 wanted.tlength = spec->segtotal * spec->segsize;
965 wanted.maxlength = -1;
966 wanted.prebuf = 0;
967 wanted.minreq = spec->segsize;
968
969 GST_INFO_OBJECT (psink, "tlength: %d", wanted.tlength);
970 GST_INFO_OBJECT (psink, "maxlength: %d", wanted.maxlength);
971 GST_INFO_OBJECT (psink, "prebuf: %d", wanted.prebuf);
972 GST_INFO_OBJECT (psink, "minreq: %d", wanted.minreq);
973
974 /* configure volume when we changed it, else we leave the default */
975 if (psink->volume_set) {
976 GST_LOG_OBJECT (psink, "have volume of %f", psink->volume);
977 pv = &v;
978 if (pbuf->is_pcm)
979 gst_pulse_cvolume_from_linear (pv, pbuf->channels, psink->volume);
980 else {
981 GST_DEBUG_OBJECT (psink, "passthrough stream, not setting volume");
982 pv = NULL;
983 }
984 } else {
985 pv = NULL;
986 }
987
988 /* construct the flags */
989 flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE |
990 PA_STREAM_ADJUST_LATENCY | PA_STREAM_START_CORKED;
991
992 if (psink->mute_set) {
993 if (psink->mute)
994 flags |= PA_STREAM_START_MUTED;
995 else
996 flags |= PA_STREAM_START_UNMUTED;
997 }
998
999 /* we always start corked (see flags above) */
1000 pbuf->corked = TRUE;
1001
1002 /* try to connect now */
1003 GST_LOG_OBJECT (psink, "connect for playback to device %s",
1004 GST_STR_NULL (psink->device));
1005 if (pa_stream_connect_playback (pbuf->stream, psink->device,
1006 &wanted, flags, pv, NULL) < 0)
1007 goto connect_failed;
1008
1009 /* our clock will now start from 0 again */
1010 clock = GST_AUDIO_CLOCK (GST_AUDIO_BASE_SINK (psink)->provided_clock);
1011 gst_audio_clock_reset (clock, 0);
1012
1013 if (!gst_pulsering_wait_for_stream_ready (psink, pbuf->stream))
1014 goto connect_failed;
1015
1016 g_free (psink->device);
1017 psink->device = g_strdup (pa_stream_get_device_name (pbuf->stream));
1018
1019 #ifndef GST_DISABLE_GST_DEBUG
1020 pa_format_info_snprint (print_buf, sizeof (print_buf),
1021 pa_stream_get_format_info (pbuf->stream));
1022 GST_INFO_OBJECT (psink, "negotiated to: %s", print_buf);
1023 #endif
1024
1025 /* After we passed the volume off of to PA we never want to set it
1026 again, since it is PA's job to save/restore volumes. */
1027 psink->volume_set = psink->mute_set = FALSE;
1028
1029 GST_LOG_OBJECT (psink, "stream is acquired now");
1030
1031 /* get the actual buffering properties now */
1032 actual = pa_stream_get_buffer_attr (pbuf->stream);
1033
1034 GST_INFO_OBJECT (psink, "tlength: %d (wanted: %d)", actual->tlength,
1035 wanted.tlength);
1036 GST_INFO_OBJECT (psink, "maxlength: %d", actual->maxlength);
1037 GST_INFO_OBJECT (psink, "prebuf: %d", actual->prebuf);
1038 GST_INFO_OBJECT (psink, "minreq: %d (wanted %d)", actual->minreq,
1039 wanted.minreq);
1040
1041 spec->segsize = actual->minreq;
1042 spec->segtotal = actual->tlength / spec->segsize;
1043
1044 pa_threaded_mainloop_unlock (mainloop);
1045
1046 return TRUE;
1047
1048 /* ERRORS */
1049 unlock_and_fail:
1050 {
1051 gst_pulsering_destroy_stream (pbuf);
1052 pa_threaded_mainloop_unlock (mainloop);
1053
1054 return FALSE;
1055 }
1056 invalid_spec:
1057 {
1058 GST_ELEMENT_ERROR (psink, RESOURCE, SETTINGS,
1059 ("Invalid sample specification."), (NULL));
1060 return FALSE;
1061 }
1062 subscribe_failed:
1063 {
1064 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1065 ("pa_context_subscribe() failed: %s",
1066 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1067 goto unlock_and_fail;
1068 }
1069 stream_failed:
1070 {
1071 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1072 ("Failed to create stream: %s",
1073 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1074 goto unlock_and_fail;
1075 }
1076 connect_failed:
1077 {
1078 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1079 ("Failed to connect stream: %s",
1080 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1081 goto unlock_and_fail;
1082 }
1083 }
1084
1085 /* free the stream that we acquired before */
1086 static gboolean
gst_pulseringbuffer_release(GstAudioRingBuffer * buf)1087 gst_pulseringbuffer_release (GstAudioRingBuffer * buf)
1088 {
1089 GstPulseRingBuffer *pbuf;
1090
1091 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1092
1093 pa_threaded_mainloop_lock (mainloop);
1094 gst_pulsering_destroy_stream (pbuf);
1095 pa_threaded_mainloop_unlock (mainloop);
1096
1097 {
1098 GstPulseSink *psink;
1099
1100 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1101 g_atomic_int_set (&psink->format_lost, FALSE);
1102 psink->format_lost_time = GST_CLOCK_TIME_NONE;
1103 }
1104
1105 return TRUE;
1106 }
1107
1108 static void
gst_pulsering_success_cb(pa_stream * s,int success,void * userdata)1109 gst_pulsering_success_cb (pa_stream * s, int success, void *userdata)
1110 {
1111 pa_threaded_mainloop_signal (mainloop, 0);
1112 }
1113
1114 /* update the corked state of a stream, must be called with the mainloop
1115 * lock */
1116 static gboolean
gst_pulsering_set_corked(GstPulseRingBuffer * pbuf,gboolean corked,gboolean wait)1117 gst_pulsering_set_corked (GstPulseRingBuffer * pbuf, gboolean corked,
1118 gboolean wait)
1119 {
1120 pa_operation *o = NULL;
1121 GstPulseSink *psink;
1122 gboolean res = FALSE;
1123
1124 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1125
1126 if (g_atomic_int_get (&psink->format_lost)) {
1127 /* Sink format changed, stream's gone so fake being paused */
1128 return TRUE;
1129 }
1130
1131 GST_DEBUG_OBJECT (psink, "setting corked state to %d", corked);
1132 if (pbuf->corked != corked) {
1133 if (!(o = pa_stream_cork (pbuf->stream, corked,
1134 gst_pulsering_success_cb, pbuf)))
1135 goto cork_failed;
1136
1137 while (wait && pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1138 pa_threaded_mainloop_wait (mainloop);
1139 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
1140 goto server_dead;
1141 }
1142 pbuf->corked = corked;
1143 } else {
1144 GST_DEBUG_OBJECT (psink, "skipping, already in requested state");
1145 }
1146 res = TRUE;
1147
1148 cleanup:
1149 if (o)
1150 pa_operation_unref (o);
1151
1152 return res;
1153
1154 /* ERRORS */
1155 server_dead:
1156 {
1157 GST_DEBUG_OBJECT (psink, "the server is dead");
1158 goto cleanup;
1159 }
1160 cork_failed:
1161 {
1162 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1163 ("pa_stream_cork() failed: %s",
1164 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1165 goto cleanup;
1166 }
1167 }
1168
1169 static void
gst_pulseringbuffer_clear(GstAudioRingBuffer * buf)1170 gst_pulseringbuffer_clear (GstAudioRingBuffer * buf)
1171 {
1172 GstPulseSink *psink;
1173 GstPulseRingBuffer *pbuf;
1174 pa_operation *o = NULL;
1175
1176 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1177 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1178
1179 pa_threaded_mainloop_lock (mainloop);
1180 GST_DEBUG_OBJECT (psink, "clearing");
1181 if (pbuf->stream) {
1182 /* don't wait for the flush to complete */
1183 if ((o = pa_stream_flush (pbuf->stream, NULL, pbuf)))
1184 pa_operation_unref (o);
1185 }
1186 pa_threaded_mainloop_unlock (mainloop);
1187 }
1188
1189 #if 0
1190 /* called from pulse thread with the mainloop lock */
1191 static void
1192 mainloop_enter_defer_cb (pa_mainloop_api * api, void *userdata)
1193 {
1194 GstPulseSink *pulsesink = GST_PULSESINK (userdata);
1195 GstMessage *message;
1196 GValue val = { 0 };
1197
1198 GST_DEBUG_OBJECT (pulsesink, "posting ENTER stream status");
1199 message = gst_message_new_stream_status (GST_OBJECT (pulsesink),
1200 GST_STREAM_STATUS_TYPE_ENTER, GST_ELEMENT (pulsesink));
1201 g_value_init (&val, GST_TYPE_G_THREAD);
1202 g_value_set_boxed (&val, g_thread_self ());
1203 gst_message_set_stream_status_object (message, &val);
1204 g_value_unset (&val);
1205
1206 gst_element_post_message (GST_ELEMENT (pulsesink), message);
1207
1208 g_return_if_fail (pulsesink->defer_pending);
1209 pulsesink->defer_pending--;
1210 pa_threaded_mainloop_signal (mainloop, 0);
1211 }
1212 #endif
1213
1214 /* start/resume playback ASAP, we don't uncork here but in the commit method */
1215 static gboolean
gst_pulseringbuffer_start(GstAudioRingBuffer * buf)1216 gst_pulseringbuffer_start (GstAudioRingBuffer * buf)
1217 {
1218 GstPulseSink *psink;
1219 GstPulseRingBuffer *pbuf;
1220
1221 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1222 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1223
1224 pa_threaded_mainloop_lock (mainloop);
1225
1226 GST_DEBUG_OBJECT (psink, "starting");
1227 pbuf->paused = FALSE;
1228
1229 /* EOS needs running clock */
1230 if (GST_BASE_SINK_CAST (psink)->eos ||
1231 g_atomic_int_get (&GST_AUDIO_BASE_SINK (psink)->eos_rendering))
1232 gst_pulsering_set_corked (pbuf, FALSE, FALSE);
1233
1234 #if 0
1235 GST_DEBUG_OBJECT (psink, "scheduling stream status");
1236 psink->defer_pending++;
1237 pa_mainloop_api_once (pa_threaded_mainloop_get_api (mainloop),
1238 mainloop_enter_defer_cb, psink);
1239
1240 /* Wait for the stream status message to be posted. This needs to be done
1241 * synchronously because the callback will take the mainloop lock
1242 * (implicitly) and then take the GST_OBJECT_LOCK. Everywhere else, we take
1243 * the locks in the reverse order, so not doing this synchronously could
1244 * cause a deadlock. */
1245 GST_DEBUG_OBJECT (psink, "waiting for stream status (ENTER) to be posted");
1246 pa_threaded_mainloop_wait (mainloop);
1247 #endif
1248
1249 pa_threaded_mainloop_unlock (mainloop);
1250
1251 return TRUE;
1252 }
1253
1254 /* pause/stop playback ASAP */
1255 static gboolean
gst_pulseringbuffer_pause(GstAudioRingBuffer * buf)1256 gst_pulseringbuffer_pause (GstAudioRingBuffer * buf)
1257 {
1258 GstPulseSink *psink;
1259 GstPulseRingBuffer *pbuf;
1260 gboolean res;
1261
1262 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1263 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1264
1265 pa_threaded_mainloop_lock (mainloop);
1266 GST_DEBUG_OBJECT (psink, "pausing and corking");
1267 /* make sure the commit method stops writing */
1268 pbuf->paused = TRUE;
1269 res = gst_pulsering_set_corked (pbuf, TRUE, TRUE);
1270 if (pbuf->in_commit) {
1271 /* we are waiting in a commit, signal */
1272 GST_DEBUG_OBJECT (psink, "signal commit");
1273 pa_threaded_mainloop_signal (mainloop, 0);
1274 }
1275 pa_threaded_mainloop_unlock (mainloop);
1276
1277 return res;
1278 }
1279
1280 #if 0
1281 /* called from pulse thread with the mainloop lock */
1282 static void
1283 mainloop_leave_defer_cb (pa_mainloop_api * api, void *userdata)
1284 {
1285 GstPulseSink *pulsesink = GST_PULSESINK (userdata);
1286 GstMessage *message;
1287 GValue val = { 0 };
1288
1289 GST_DEBUG_OBJECT (pulsesink, "posting LEAVE stream status");
1290 message = gst_message_new_stream_status (GST_OBJECT (pulsesink),
1291 GST_STREAM_STATUS_TYPE_LEAVE, GST_ELEMENT (pulsesink));
1292 g_value_init (&val, GST_TYPE_G_THREAD);
1293 g_value_set_boxed (&val, g_thread_self ());
1294 gst_message_set_stream_status_object (message, &val);
1295 g_value_unset (&val);
1296
1297 gst_element_post_message (GST_ELEMENT (pulsesink), message);
1298
1299 g_return_if_fail (pulsesink->defer_pending);
1300 pulsesink->defer_pending--;
1301 pa_threaded_mainloop_signal (mainloop, 0);
1302 }
1303 #endif
1304
1305 /* stop playback, we flush everything. */
1306 static gboolean
gst_pulseringbuffer_stop(GstAudioRingBuffer * buf)1307 gst_pulseringbuffer_stop (GstAudioRingBuffer * buf)
1308 {
1309 GstPulseSink *psink;
1310 GstPulseRingBuffer *pbuf;
1311 gboolean res = FALSE;
1312 pa_operation *o = NULL;
1313
1314 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1315 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1316
1317 pa_threaded_mainloop_lock (mainloop);
1318
1319 pbuf->paused = TRUE;
1320 res = gst_pulsering_set_corked (pbuf, TRUE, TRUE);
1321
1322 /* Inform anyone waiting in _commit() call that it shall wakeup */
1323 if (pbuf->in_commit) {
1324 GST_DEBUG_OBJECT (psink, "signal commit thread");
1325 pa_threaded_mainloop_signal (mainloop, 0);
1326 }
1327 if (g_atomic_int_get (&psink->format_lost)) {
1328 /* Don't try to flush, the stream's probably gone by now */
1329 res = TRUE;
1330 goto cleanup;
1331 }
1332
1333 /* then try to flush, it's not fatal when this fails */
1334 GST_DEBUG_OBJECT (psink, "flushing");
1335 if ((o = pa_stream_flush (pbuf->stream, gst_pulsering_success_cb, pbuf))) {
1336 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1337 GST_DEBUG_OBJECT (psink, "wait for completion");
1338 pa_threaded_mainloop_wait (mainloop);
1339 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
1340 goto server_dead;
1341 }
1342 GST_DEBUG_OBJECT (psink, "flush completed");
1343 }
1344 res = TRUE;
1345
1346 cleanup:
1347 if (o) {
1348 pa_operation_cancel (o);
1349 pa_operation_unref (o);
1350 }
1351 #if 0
1352 GST_DEBUG_OBJECT (psink, "scheduling stream status");
1353 psink->defer_pending++;
1354 pa_mainloop_api_once (pa_threaded_mainloop_get_api (mainloop),
1355 mainloop_leave_defer_cb, psink);
1356
1357 /* Wait for the stream status message to be posted. This needs to be done
1358 * synchronously because the callback will take the mainloop lock
1359 * (implicitly) and then take the GST_OBJECT_LOCK. Everywhere else, we take
1360 * the locks in the reverse order, so not doing this synchronously could
1361 * cause a deadlock. */
1362 GST_DEBUG_OBJECT (psink, "waiting for stream status (LEAVE) to be posted");
1363 pa_threaded_mainloop_wait (mainloop);
1364 #endif
1365
1366 pa_threaded_mainloop_unlock (mainloop);
1367
1368 return res;
1369
1370 /* ERRORS */
1371 server_dead:
1372 {
1373 GST_DEBUG_OBJECT (psink, "the server is dead");
1374 goto cleanup;
1375 }
1376 }
1377
1378 /* in_samples >= out_samples, rate > 1.0 */
1379 #define FWD_UP_SAMPLES(s,se,d,de) \
1380 G_STMT_START { \
1381 guint8 *sb = s, *db = d; \
1382 while (s <= se && d < de) { \
1383 memcpy (d, s, bpf); \
1384 s += bpf; \
1385 *accum += outr; \
1386 if ((*accum << 1) >= inr) { \
1387 *accum -= inr; \
1388 d += bpf; \
1389 } \
1390 } \
1391 in_samples -= (s - sb)/bpf; \
1392 out_samples -= (d - db)/bpf; \
1393 GST_DEBUG ("fwd_up end %d/%d",*accum,*toprocess); \
1394 } G_STMT_END
1395
1396 /* out_samples > in_samples, for rates smaller than 1.0 */
1397 #define FWD_DOWN_SAMPLES(s,se,d,de) \
1398 G_STMT_START { \
1399 guint8 *sb = s, *db = d; \
1400 while (s <= se && d < de) { \
1401 memcpy (d, s, bpf); \
1402 d += bpf; \
1403 *accum += inr; \
1404 if ((*accum << 1) >= outr) { \
1405 *accum -= outr; \
1406 s += bpf; \
1407 } \
1408 } \
1409 in_samples -= (s - sb)/bpf; \
1410 out_samples -= (d - db)/bpf; \
1411 GST_DEBUG ("fwd_down end %d/%d",*accum,*toprocess); \
1412 } G_STMT_END
1413
1414 #define REV_UP_SAMPLES(s,se,d,de) \
1415 G_STMT_START { \
1416 guint8 *sb = se, *db = d; \
1417 while (s <= se && d < de) { \
1418 memcpy (d, se, bpf); \
1419 se -= bpf; \
1420 *accum += outr; \
1421 while (d < de && (*accum << 1) >= inr) { \
1422 *accum -= inr; \
1423 d += bpf; \
1424 } \
1425 } \
1426 in_samples -= (sb - se)/bpf; \
1427 out_samples -= (d - db)/bpf; \
1428 GST_DEBUG ("rev_up end %d/%d",*accum,*toprocess); \
1429 } G_STMT_END
1430
1431 #define REV_DOWN_SAMPLES(s,se,d,de) \
1432 G_STMT_START { \
1433 guint8 *sb = se, *db = d; \
1434 while (s <= se && d < de) { \
1435 memcpy (d, se, bpf); \
1436 d += bpf; \
1437 *accum += inr; \
1438 while (s <= se && (*accum << 1) >= outr) { \
1439 *accum -= outr; \
1440 se -= bpf; \
1441 } \
1442 } \
1443 in_samples -= (sb - se)/bpf; \
1444 out_samples -= (d - db)/bpf; \
1445 GST_DEBUG ("rev_down end %d/%d",*accum,*toprocess); \
1446 } G_STMT_END
1447
1448 /* our custom commit function because we write into the buffer of pulseaudio
1449 * instead of keeping our own buffer */
1450 static guint
gst_pulseringbuffer_commit(GstAudioRingBuffer * buf,guint64 * sample,guchar * data,gint in_samples,gint out_samples,gint * accum)1451 gst_pulseringbuffer_commit (GstAudioRingBuffer * buf, guint64 * sample,
1452 guchar * data, gint in_samples, gint out_samples, gint * accum)
1453 {
1454 GstPulseSink *psink;
1455 GstPulseRingBuffer *pbuf;
1456 guint result;
1457 guint8 *data_end;
1458 gboolean reverse;
1459 gint *toprocess;
1460 gint inr, outr, bpf;
1461 gint64 offset;
1462 guint bufsize;
1463
1464 pbuf = GST_PULSERING_BUFFER_CAST (buf);
1465 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1466
1467 /* FIXME post message rather than using a signal (as mixer interface) */
1468 if (g_atomic_int_compare_and_exchange (&psink->notify, 1, 0)) {
1469 g_object_notify (G_OBJECT (psink), "volume");
1470 g_object_notify (G_OBJECT (psink), "mute");
1471 g_object_notify (G_OBJECT (psink), "current-device");
1472 }
1473
1474 /* make sure the ringbuffer is started */
1475 if (G_UNLIKELY (g_atomic_int_get (&buf->state) !=
1476 GST_AUDIO_RING_BUFFER_STATE_STARTED)) {
1477 /* see if we are allowed to start it */
1478 if (G_UNLIKELY (g_atomic_int_get (&buf->may_start) == FALSE))
1479 goto no_start;
1480
1481 GST_DEBUG_OBJECT (buf, "start!");
1482 if (!gst_audio_ring_buffer_start (buf))
1483 goto start_failed;
1484 }
1485
1486 pa_threaded_mainloop_lock (mainloop);
1487
1488 GST_DEBUG_OBJECT (psink, "entering commit");
1489 pbuf->in_commit = TRUE;
1490
1491 bpf = GST_AUDIO_INFO_BPF (&buf->spec.info);
1492 bufsize = buf->spec.segsize * buf->spec.segtotal;
1493
1494 /* our toy resampler for trick modes */
1495 reverse = out_samples < 0;
1496 out_samples = ABS (out_samples);
1497
1498 if (in_samples >= out_samples)
1499 toprocess = &in_samples;
1500 else
1501 toprocess = &out_samples;
1502
1503 inr = in_samples - 1;
1504 outr = out_samples - 1;
1505
1506 GST_DEBUG_OBJECT (psink, "in %d, out %d", inr, outr);
1507
1508 /* data_end points to the last sample we have to write, not past it. This is
1509 * needed to properly handle reverse playback: it points to the last sample. */
1510 data_end = data + (bpf * inr);
1511
1512 if (g_atomic_int_get (&psink->format_lost)) {
1513 /* Sink format changed, drop the data and hope upstream renegotiates */
1514 goto fake_done;
1515 }
1516
1517 if (pbuf->paused)
1518 goto was_paused;
1519
1520 /* offset is in bytes */
1521 offset = *sample * bpf;
1522
1523 while (*toprocess > 0) {
1524 size_t avail;
1525 guint towrite;
1526
1527 GST_LOG_OBJECT (psink,
1528 "need to write %d samples at offset %" G_GINT64_FORMAT, *toprocess,
1529 offset);
1530
1531 if (offset != pbuf->m_lastoffset)
1532 GST_LOG_OBJECT (psink, "discontinuity, offset is %" G_GINT64_FORMAT ", "
1533 "last offset was %" G_GINT64_FORMAT, offset, pbuf->m_lastoffset);
1534
1535 towrite = out_samples * bpf;
1536
1537 /* Wait for at least segsize bytes to become available */
1538 if (towrite > buf->spec.segsize)
1539 towrite = buf->spec.segsize;
1540
1541 if ((pbuf->m_writable < towrite) || (offset != pbuf->m_lastoffset)) {
1542 /* if no room left or discontinuity in offset,
1543 we need to flush data and get a new buffer */
1544
1545 /* flush the buffer if possible */
1546 if ((pbuf->m_data != NULL) && (pbuf->m_towrite > 0)) {
1547
1548 GST_LOG_OBJECT (psink,
1549 "flushing %u samples at offset %" G_GINT64_FORMAT,
1550 (guint) pbuf->m_towrite / bpf, pbuf->m_offset);
1551
1552 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1553 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1554 goto write_failed;
1555 }
1556 }
1557 pbuf->m_towrite = 0;
1558 pbuf->m_offset = offset; /* keep track of current offset */
1559
1560 /* get a buffer to write in for now on */
1561 for (;;) {
1562 pbuf->m_writable = pa_stream_writable_size (pbuf->stream);
1563
1564 if (g_atomic_int_get (&psink->format_lost)) {
1565 /* Sink format changed, give up and hope upstream renegotiates */
1566 goto fake_done;
1567 }
1568
1569 if (pbuf->m_writable == (size_t) - 1)
1570 goto writable_size_failed;
1571
1572 pbuf->m_writable /= bpf;
1573 pbuf->m_writable *= bpf; /* handle only complete samples */
1574
1575 if (pbuf->m_writable >= towrite)
1576 break;
1577
1578 /* see if we need to uncork because we have no free space */
1579 if (pbuf->corked) {
1580 if (!gst_pulsering_set_corked (pbuf, FALSE, FALSE))
1581 goto uncork_failed;
1582 }
1583
1584 /* we can't write segsize bytes, wait a bit */
1585 GST_LOG_OBJECT (psink, "waiting for free space");
1586 pa_threaded_mainloop_wait (mainloop);
1587
1588 if (pbuf->paused)
1589 goto was_paused;
1590 }
1591
1592 /* Recalculate what we can write in the next chunk */
1593 towrite = out_samples * bpf;
1594 if (pbuf->m_writable > towrite)
1595 pbuf->m_writable = towrite;
1596
1597 GST_LOG_OBJECT (psink, "requesting %" G_GSIZE_FORMAT " bytes of "
1598 "shared memory", pbuf->m_writable);
1599
1600 if (pa_stream_begin_write (pbuf->stream, &pbuf->m_data,
1601 &pbuf->m_writable) < 0) {
1602 GST_LOG_OBJECT (psink, "pa_stream_begin_write() failed");
1603 goto writable_size_failed;
1604 }
1605
1606 GST_LOG_OBJECT (psink, "got %" G_GSIZE_FORMAT " bytes of shared memory",
1607 pbuf->m_writable);
1608
1609 }
1610
1611 if (towrite > pbuf->m_writable)
1612 towrite = pbuf->m_writable;
1613 avail = towrite / bpf;
1614
1615 GST_LOG_OBJECT (psink, "writing %u samples at offset %" G_GUINT64_FORMAT,
1616 (guint) avail, offset);
1617
1618 /* No trick modes for passthrough streams */
1619 if (G_UNLIKELY (!pbuf->is_pcm && (inr != outr || reverse))) {
1620 GST_WARNING_OBJECT (psink, "Passthrough stream can't run in trick mode");
1621 goto unlock_and_fail;
1622 }
1623
1624 if (G_LIKELY (inr == outr && !reverse)) {
1625 /* no rate conversion, simply write out the samples */
1626 /* copy the data into internal buffer */
1627
1628 memcpy ((guint8 *) pbuf->m_data + pbuf->m_towrite, data, towrite);
1629 pbuf->m_towrite += towrite;
1630 pbuf->m_writable -= towrite;
1631
1632 data += towrite;
1633 in_samples -= avail;
1634 out_samples -= avail;
1635 } else {
1636 guint8 *dest, *d, *d_end;
1637
1638 /* write into the PulseAudio shm buffer */
1639 dest = d = (guint8 *) pbuf->m_data + pbuf->m_towrite;
1640 d_end = d + towrite;
1641
1642 if (!reverse) {
1643 if (inr >= outr)
1644 /* forward speed up */
1645 FWD_UP_SAMPLES (data, data_end, d, d_end);
1646 else
1647 /* forward slow down */
1648 FWD_DOWN_SAMPLES (data, data_end, d, d_end);
1649 } else {
1650 if (inr >= outr)
1651 /* reverse speed up */
1652 REV_UP_SAMPLES (data, data_end, d, d_end);
1653 else
1654 /* reverse slow down */
1655 REV_DOWN_SAMPLES (data, data_end, d, d_end);
1656 }
1657 /* see what we have left to write */
1658 towrite = (d - dest);
1659 pbuf->m_towrite += towrite;
1660 pbuf->m_writable -= towrite;
1661
1662 avail = towrite / bpf;
1663 }
1664
1665 /* flush the buffer if it's full */
1666 if ((pbuf->m_data != NULL) && (pbuf->m_towrite > 0)
1667 && (pbuf->m_writable == 0)) {
1668 GST_LOG_OBJECT (psink, "flushing %u samples at offset %" G_GINT64_FORMAT,
1669 (guint) pbuf->m_towrite / bpf, pbuf->m_offset);
1670
1671 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1672 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1673 goto write_failed;
1674 }
1675 pbuf->m_towrite = 0;
1676 pbuf->m_offset = offset + towrite; /* keep track of current offset */
1677 }
1678
1679 *sample += avail;
1680 offset += avail * bpf;
1681 pbuf->m_lastoffset = offset;
1682
1683 /* check if we need to uncork after writing the samples */
1684 if (pbuf->corked) {
1685 const pa_timing_info *info;
1686
1687 if ((info = pa_stream_get_timing_info (pbuf->stream))) {
1688 GST_LOG_OBJECT (psink,
1689 "read_index at %" G_GUINT64_FORMAT ", offset %" G_GINT64_FORMAT,
1690 info->read_index, offset);
1691
1692 /* we uncork when the read_index is too far behind the offset we need
1693 * to write to. */
1694 if (info->read_index + bufsize <= offset) {
1695 if (!gst_pulsering_set_corked (pbuf, FALSE, FALSE))
1696 goto uncork_failed;
1697 }
1698 } else {
1699 GST_LOG_OBJECT (psink, "no timing info available yet");
1700 }
1701 }
1702 }
1703
1704 fake_done:
1705 /* we consumed all samples here */
1706 data = data_end + bpf;
1707
1708 pbuf->in_commit = FALSE;
1709 pa_threaded_mainloop_unlock (mainloop);
1710
1711 done:
1712 result = inr - ((data_end - data) / bpf);
1713 GST_LOG_OBJECT (psink, "wrote %d samples", result);
1714
1715 return result;
1716
1717 /* ERRORS */
1718 unlock_and_fail:
1719 {
1720 pbuf->in_commit = FALSE;
1721 GST_LOG_OBJECT (psink, "we are reset");
1722 pa_threaded_mainloop_unlock (mainloop);
1723 goto done;
1724 }
1725 no_start:
1726 {
1727 GST_LOG_OBJECT (psink, "we can not start");
1728 return 0;
1729 }
1730 start_failed:
1731 {
1732 GST_LOG_OBJECT (psink, "failed to start the ringbuffer");
1733 return 0;
1734 }
1735 uncork_failed:
1736 {
1737 pbuf->in_commit = FALSE;
1738 GST_ERROR_OBJECT (psink, "uncork failed");
1739 pa_threaded_mainloop_unlock (mainloop);
1740 goto done;
1741 }
1742 was_paused:
1743 {
1744 pbuf->in_commit = FALSE;
1745 GST_LOG_OBJECT (psink, "we are paused");
1746 pa_threaded_mainloop_unlock (mainloop);
1747 goto done;
1748 }
1749 writable_size_failed:
1750 {
1751 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1752 ("pa_stream_writable_size() failed: %s",
1753 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1754 goto unlock_and_fail;
1755 }
1756 write_failed:
1757 {
1758 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1759 ("pa_stream_write() failed: %s",
1760 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1761 goto unlock_and_fail;
1762 }
1763 }
1764
1765 /* write pending local samples, must be called with the mainloop lock */
1766 static void
gst_pulsering_flush(GstPulseRingBuffer * pbuf)1767 gst_pulsering_flush (GstPulseRingBuffer * pbuf)
1768 {
1769 GstPulseSink *psink;
1770
1771 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
1772 GST_DEBUG_OBJECT (psink, "entering flush");
1773
1774 /* flush the buffer if possible */
1775 if (pbuf->stream && (pbuf->m_data != NULL) && (pbuf->m_towrite > 0)) {
1776 #ifndef GST_DISABLE_GST_DEBUG
1777 gint bpf;
1778
1779 bpf = (GST_AUDIO_RING_BUFFER_CAST (pbuf))->spec.info.bpf;
1780 GST_LOG_OBJECT (psink,
1781 "flushing %u samples at offset %" G_GINT64_FORMAT,
1782 (guint) pbuf->m_towrite / bpf, pbuf->m_offset);
1783 #endif
1784
1785 if (pa_stream_write (pbuf->stream, (uint8_t *) pbuf->m_data,
1786 pbuf->m_towrite, NULL, pbuf->m_offset, PA_SEEK_ABSOLUTE) < 0) {
1787 goto write_failed;
1788 }
1789
1790 pbuf->m_towrite = 0;
1791 pbuf->m_offset += pbuf->m_towrite; /* keep track of current offset */
1792 }
1793
1794 done:
1795 return;
1796
1797 /* ERRORS */
1798 write_failed:
1799 {
1800 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
1801 ("pa_stream_write() failed: %s",
1802 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
1803 goto done;
1804 }
1805 }
1806
1807 static void gst_pulsesink_set_property (GObject * object, guint prop_id,
1808 const GValue * value, GParamSpec * pspec);
1809 static void gst_pulsesink_get_property (GObject * object, guint prop_id,
1810 GValue * value, GParamSpec * pspec);
1811 static void gst_pulsesink_finalize (GObject * object);
1812
1813 static gboolean gst_pulsesink_event (GstBaseSink * sink, GstEvent * event);
1814 static gboolean gst_pulsesink_query (GstBaseSink * sink, GstQuery * query);
1815
1816 static GstStateChangeReturn gst_pulsesink_change_state (GstElement * element,
1817 GstStateChange transition);
1818
1819 #define gst_pulsesink_parent_class parent_class
1820 G_DEFINE_TYPE_WITH_CODE (GstPulseSink, gst_pulsesink, GST_TYPE_AUDIO_BASE_SINK,
1821 gst_pulsesink_init_contexts ();
1822 G_IMPLEMENT_INTERFACE (GST_TYPE_STREAM_VOLUME, NULL)
1823 );
1824 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (pulsesink, "pulsesink",
1825 GST_RANK_PRIMARY + 10, GST_TYPE_PULSESINK, pulse_element_init (plugin));
1826
1827 static GstAudioRingBuffer *
gst_pulsesink_create_ringbuffer(GstAudioBaseSink * sink)1828 gst_pulsesink_create_ringbuffer (GstAudioBaseSink * sink)
1829 {
1830 GstAudioRingBuffer *buffer;
1831
1832 GST_DEBUG_OBJECT (sink, "creating ringbuffer");
1833 buffer = g_object_new (GST_TYPE_PULSERING_BUFFER, NULL);
1834 GST_DEBUG_OBJECT (sink, "created ringbuffer @%p", buffer);
1835
1836 return buffer;
1837 }
1838
1839 static GstBuffer *
gst_pulsesink_payload(GstAudioBaseSink * sink,GstBuffer * buf)1840 gst_pulsesink_payload (GstAudioBaseSink * sink, GstBuffer * buf)
1841 {
1842 switch (sink->ringbuffer->spec.type) {
1843 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3:
1844 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_EAC3:
1845 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS:
1846 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_MPEG:
1847 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_MPEG2_AAC:
1848 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_MPEG4_AAC:
1849 {
1850 /* FIXME: alloc memory from PA if possible */
1851 gint framesize = gst_audio_iec61937_frame_size (&sink->ringbuffer->spec);
1852 GstBuffer *out;
1853 GstMapInfo inmap, outmap;
1854 gboolean res;
1855
1856 if (framesize <= 0)
1857 return NULL;
1858
1859 out = gst_buffer_new_and_alloc (framesize);
1860
1861 gst_buffer_map (buf, &inmap, GST_MAP_READ);
1862 gst_buffer_map (out, &outmap, GST_MAP_WRITE);
1863
1864 res = gst_audio_iec61937_payload (inmap.data, inmap.size,
1865 outmap.data, outmap.size, &sink->ringbuffer->spec, G_BIG_ENDIAN);
1866
1867 gst_buffer_unmap (buf, &inmap);
1868 gst_buffer_unmap (out, &outmap);
1869
1870 if (!res) {
1871 gst_buffer_unref (out);
1872 return NULL;
1873 }
1874
1875 gst_buffer_copy_into (out, buf, GST_BUFFER_COPY_METADATA, 0, -1);
1876 return out;
1877 }
1878
1879 default:
1880 return gst_buffer_ref (buf);
1881 }
1882 }
1883
1884 static void
gst_pulsesink_class_init(GstPulseSinkClass * klass)1885 gst_pulsesink_class_init (GstPulseSinkClass * klass)
1886 {
1887 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1888 GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
1889 GstBaseSinkClass *bc;
1890 GstAudioBaseSinkClass *gstaudiosink_class = GST_AUDIO_BASE_SINK_CLASS (klass);
1891 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1892 GstCaps *caps;
1893 gchar *clientname;
1894
1895 gobject_class->finalize = gst_pulsesink_finalize;
1896 gobject_class->set_property = gst_pulsesink_set_property;
1897 gobject_class->get_property = gst_pulsesink_get_property;
1898
1899 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_pulsesink_event);
1900 gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_pulsesink_query);
1901
1902 /* restore the original basesink pull methods */
1903 bc = g_type_class_peek (GST_TYPE_BASE_SINK);
1904 gstbasesink_class->activate_pull = GST_DEBUG_FUNCPTR (bc->activate_pull);
1905
1906 gstelement_class->change_state =
1907 GST_DEBUG_FUNCPTR (gst_pulsesink_change_state);
1908
1909 gstaudiosink_class->create_ringbuffer =
1910 GST_DEBUG_FUNCPTR (gst_pulsesink_create_ringbuffer);
1911 gstaudiosink_class->payload = GST_DEBUG_FUNCPTR (gst_pulsesink_payload);
1912
1913 /* Overwrite GObject fields */
1914 g_object_class_install_property (gobject_class,
1915 PROP_SERVER,
1916 g_param_spec_string ("server", "Server",
1917 "The PulseAudio server to connect to", DEFAULT_SERVER,
1918 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1919
1920 g_object_class_install_property (gobject_class, PROP_DEVICE,
1921 g_param_spec_string ("device", "Device",
1922 "The PulseAudio sink device to connect to", DEFAULT_DEVICE,
1923 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1924
1925 g_object_class_install_property (gobject_class, PROP_CURRENT_DEVICE,
1926 g_param_spec_string ("current-device", "Current Device",
1927 "The current PulseAudio sink device", DEFAULT_CURRENT_DEVICE,
1928 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1929
1930 g_object_class_install_property (gobject_class,
1931 PROP_DEVICE_NAME,
1932 g_param_spec_string ("device-name", "Device name",
1933 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
1934 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1935
1936 g_object_class_install_property (gobject_class,
1937 PROP_VOLUME,
1938 g_param_spec_double ("volume", "Volume",
1939 "Linear volume of this stream, 1.0=100%", 0.0, MAX_VOLUME,
1940 DEFAULT_VOLUME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1941 g_object_class_install_property (gobject_class,
1942 PROP_MUTE,
1943 g_param_spec_boolean ("mute", "Mute",
1944 "Mute state of this stream", DEFAULT_MUTE,
1945 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1946
1947 /**
1948 * GstPulseSink:client-name:
1949 *
1950 * The PulseAudio client name to use.
1951 */
1952 clientname = gst_pulse_client_name ();
1953 g_object_class_install_property (gobject_class,
1954 PROP_CLIENT_NAME,
1955 g_param_spec_string ("client-name", "Client Name",
1956 "The PulseAudio client name to use", clientname,
1957 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1958 GST_PARAM_MUTABLE_READY));
1959 g_free (clientname);
1960
1961 /**
1962 * GstPulseSink:stream-properties:
1963 *
1964 * List of pulseaudio stream properties. A list of defined properties can be
1965 * found in the [pulseaudio api docs](http://0pointer.de/lennart/projects/pulseaudio/doxygen/proplist_8h.html).
1966 *
1967 * Below is an example for registering as a music application to pulseaudio.
1968 * |[
1969 * GstStructure *props;
1970 *
1971 * props = gst_structure_from_string ("props,media.role=music", NULL);
1972 * g_object_set (pulse, "stream-properties", props, NULL);
1973 * gst_structure_free
1974 * ]|
1975 */
1976 g_object_class_install_property (gobject_class,
1977 PROP_STREAM_PROPERTIES,
1978 g_param_spec_boxed ("stream-properties", "stream properties",
1979 "list of pulseaudio stream properties",
1980 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1981
1982 gst_element_class_set_static_metadata (gstelement_class,
1983 "PulseAudio Audio Sink",
1984 "Sink/Audio", "Plays audio to a PulseAudio server", "Lennart Poettering");
1985
1986 caps =
1987 gst_pulse_fix_pcm_caps (gst_caps_from_string (PULSE_SINK_TEMPLATE_CAPS));
1988 gst_element_class_add_pad_template (gstelement_class,
1989 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, caps));
1990 gst_caps_unref (caps);
1991 }
1992
1993 static void
free_device_info(GstPulseDeviceInfo * device_info)1994 free_device_info (GstPulseDeviceInfo * device_info)
1995 {
1996 GList *l;
1997
1998 g_free (device_info->description);
1999
2000 for (l = g_list_first (device_info->formats); l; l = g_list_next (l))
2001 pa_format_info_free ((pa_format_info *) l->data);
2002
2003 g_list_free (device_info->formats);
2004 }
2005
2006 /* Returns the current time of the sink ringbuffer. The timing_info is updated
2007 * on every data write/flush and every 100ms (PA_STREAM_AUTO_TIMING_UPDATE).
2008 */
2009 static GstClockTime
gst_pulsesink_get_time(GstClock * clock,GstAudioBaseSink * sink)2010 gst_pulsesink_get_time (GstClock * clock, GstAudioBaseSink * sink)
2011 {
2012 GstPulseSink *psink;
2013 GstPulseRingBuffer *pbuf;
2014 pa_usec_t time;
2015
2016 if (!sink->ringbuffer || !sink->ringbuffer->acquired)
2017 return GST_CLOCK_TIME_NONE;
2018
2019 pbuf = GST_PULSERING_BUFFER_CAST (sink->ringbuffer);
2020 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
2021
2022 if (g_atomic_int_get (&psink->format_lost)) {
2023 /* Stream was lost in a format change, it'll get set up again once
2024 * upstream renegotiates */
2025 return psink->format_lost_time;
2026 }
2027
2028 pa_threaded_mainloop_lock (mainloop);
2029 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
2030 goto server_dead;
2031
2032 /* if we don't have enough data to get a timestamp, just return NONE, which
2033 * will return the last reported time */
2034 if (pa_stream_get_time (pbuf->stream, &time) < 0) {
2035 GST_DEBUG_OBJECT (psink, "could not get time");
2036 time = GST_CLOCK_TIME_NONE;
2037 } else
2038 time *= 1000;
2039 pa_threaded_mainloop_unlock (mainloop);
2040
2041 GST_LOG_OBJECT (psink, "current time is %" GST_TIME_FORMAT,
2042 GST_TIME_ARGS (time));
2043
2044 return time;
2045
2046 /* ERRORS */
2047 server_dead:
2048 {
2049 GST_DEBUG_OBJECT (psink, "the server is dead");
2050 pa_threaded_mainloop_unlock (mainloop);
2051
2052 return GST_CLOCK_TIME_NONE;
2053 }
2054 }
2055
2056 static void
gst_pulsesink_sink_info_cb(pa_context * c,const pa_sink_info * i,int eol,void * userdata)2057 gst_pulsesink_sink_info_cb (pa_context * c, const pa_sink_info * i, int eol,
2058 void *userdata)
2059 {
2060 GstPulseDeviceInfo *device_info = (GstPulseDeviceInfo *) userdata;
2061 guint8 j;
2062
2063 if (!i)
2064 goto done;
2065
2066 device_info->description = g_strdup (i->description);
2067
2068 device_info->formats = NULL;
2069 for (j = 0; j < i->n_formats; j++)
2070 device_info->formats = g_list_prepend (device_info->formats,
2071 pa_format_info_copy (i->formats[j]));
2072
2073 done:
2074 pa_threaded_mainloop_signal (mainloop, 0);
2075 }
2076
2077 /* Call with mainloop lock held */
2078 static pa_stream *
gst_pulsesink_create_probe_stream(GstPulseSink * psink,GstPulseRingBuffer * pbuf,pa_format_info * format)2079 gst_pulsesink_create_probe_stream (GstPulseSink * psink,
2080 GstPulseRingBuffer * pbuf, pa_format_info * format)
2081 {
2082 pa_format_info *formats[1] = { format };
2083 pa_stream *stream;
2084 pa_stream_flags_t flags;
2085
2086 GST_LOG_OBJECT (psink, "Creating probe stream");
2087
2088 if (!(stream = pa_stream_new_extended (pbuf->context, "pulsesink probe",
2089 formats, 1, psink->proplist)))
2090 goto error;
2091
2092 /* construct the flags */
2093 flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE |
2094 PA_STREAM_ADJUST_LATENCY | PA_STREAM_START_CORKED;
2095
2096 pa_stream_set_state_callback (stream, gst_pulsering_stream_state_cb, pbuf);
2097
2098 if (pa_stream_connect_playback (stream, psink->device, NULL, flags, NULL,
2099 NULL) < 0)
2100 goto error;
2101
2102 if (!gst_pulsering_wait_for_stream_ready (psink, stream))
2103 goto error;
2104
2105 return stream;
2106
2107 error:
2108 if (stream)
2109 pa_stream_unref (stream);
2110 return NULL;
2111 }
2112
2113 static GstCaps *
gst_pulsesink_query_getcaps(GstPulseSink * psink,GstCaps * filter)2114 gst_pulsesink_query_getcaps (GstPulseSink * psink, GstCaps * filter)
2115 {
2116 GstPulseRingBuffer *pbuf = NULL;
2117 GstPulseDeviceInfo device_info = { NULL, NULL };
2118 GstCaps *ret = NULL;
2119 GList *i;
2120 pa_operation *o = NULL;
2121 pa_stream *stream;
2122
2123 GST_OBJECT_LOCK (psink);
2124 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2125 if (pbuf != NULL)
2126 gst_object_ref (pbuf);
2127 GST_OBJECT_UNLOCK (psink);
2128
2129 if (!pbuf) {
2130 ret = gst_pad_get_pad_template_caps (GST_AUDIO_BASE_SINK_PAD (psink));
2131 goto out;
2132 }
2133
2134 GST_OBJECT_LOCK (pbuf);
2135 pa_threaded_mainloop_lock (mainloop);
2136
2137 if (!pbuf->context) {
2138 ret = gst_pad_get_pad_template_caps (GST_AUDIO_BASE_SINK_PAD (psink));
2139 goto unlock;
2140 }
2141
2142 ret = gst_caps_new_empty ();
2143
2144 if (pbuf->stream) {
2145 /* We're in PAUSED or higher */
2146 stream = pbuf->stream;
2147
2148 } else if (pbuf->probe_stream) {
2149 /* We're not paused, but have a cached probe stream */
2150 stream = pbuf->probe_stream;
2151
2152 } else {
2153 /* We're not yet in PAUSED and still need to create a probe stream.
2154 *
2155 * FIXME: PA doesn't accept "any" format. We fix something reasonable since
2156 * this is merely a probe. This should eventually be fixed in PA and
2157 * hard-coding the format should be dropped. */
2158 pa_format_info *format = pa_format_info_new ();
2159 format->encoding = PA_ENCODING_PCM;
2160 pa_format_info_set_sample_format (format, PA_SAMPLE_S16LE);
2161 pa_format_info_set_rate (format, GST_AUDIO_DEF_RATE);
2162 pa_format_info_set_channels (format, GST_AUDIO_DEF_CHANNELS);
2163
2164 pbuf->probe_stream = gst_pulsesink_create_probe_stream (psink, pbuf,
2165 format);
2166
2167 pa_format_info_free (format);
2168
2169 if (!pbuf->probe_stream) {
2170 GST_WARNING_OBJECT (psink, "Could not create probe stream");
2171 goto unlock;
2172 }
2173
2174 stream = pbuf->probe_stream;
2175 }
2176
2177 if (!(o = pa_context_get_sink_info_by_name (pbuf->context,
2178 pa_stream_get_device_name (stream), gst_pulsesink_sink_info_cb,
2179 &device_info)))
2180 goto info_failed;
2181
2182 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2183 pa_threaded_mainloop_wait (mainloop);
2184 if (gst_pulsering_is_dead (psink, pbuf, FALSE))
2185 goto unlock;
2186 }
2187
2188 for (i = g_list_first (device_info.formats); i; i = g_list_next (i)) {
2189 GstCaps *caps = gst_pulse_format_info_to_caps ((pa_format_info *) i->data);
2190 if (caps)
2191 gst_caps_append (ret, caps);
2192 }
2193
2194 unlock:
2195 pa_threaded_mainloop_unlock (mainloop);
2196 /* FIXME: this could be freed after device_name is got */
2197 GST_OBJECT_UNLOCK (pbuf);
2198
2199 if (filter) {
2200 GstCaps *tmp = gst_caps_intersect_full (filter, ret,
2201 GST_CAPS_INTERSECT_FIRST);
2202 gst_caps_unref (ret);
2203 ret = tmp;
2204 }
2205
2206 out:
2207 free_device_info (&device_info);
2208
2209 if (o)
2210 pa_operation_unref (o);
2211
2212 if (pbuf)
2213 gst_object_unref (pbuf);
2214
2215 GST_DEBUG_OBJECT (psink, "caps %" GST_PTR_FORMAT, ret);
2216
2217 return ret;
2218
2219 info_failed:
2220 {
2221 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2222 ("pa_context_get_sink_input_info() failed: %s",
2223 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2224 goto unlock;
2225 }
2226 }
2227
2228 static gboolean
gst_pulsesink_query_acceptcaps(GstPulseSink * psink,GstCaps * caps)2229 gst_pulsesink_query_acceptcaps (GstPulseSink * psink, GstCaps * caps)
2230 {
2231 GstPulseRingBuffer *pbuf = NULL;
2232 GstPulseDeviceInfo device_info = { NULL, NULL };
2233 GstCaps *pad_caps;
2234 GstStructure *st;
2235 gboolean ret = FALSE;
2236
2237 GstAudioRingBufferSpec spec = { 0 };
2238 pa_operation *o = NULL;
2239 pa_channel_map channel_map;
2240 pa_format_info *format = NULL;
2241 guint channels;
2242
2243 pad_caps = gst_pad_get_pad_template_caps (GST_BASE_SINK_PAD (psink));
2244 ret = gst_caps_is_subset (caps, pad_caps);
2245 gst_caps_unref (pad_caps);
2246
2247 GST_DEBUG_OBJECT (psink, "caps %" GST_PTR_FORMAT, caps);
2248
2249 /* Template caps didn't match */
2250 if (!ret)
2251 goto done;
2252
2253 /* If we've not got fixed caps, creating a stream might fail, so let's just
2254 * return from here with default acceptcaps behaviour */
2255 if (!gst_caps_is_fixed (caps))
2256 goto done;
2257
2258 GST_OBJECT_LOCK (psink);
2259 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2260 if (pbuf != NULL)
2261 gst_object_ref (pbuf);
2262 GST_OBJECT_UNLOCK (psink);
2263
2264 /* We're still in NULL state */
2265 if (pbuf == NULL)
2266 goto done;
2267
2268 GST_OBJECT_LOCK (pbuf);
2269 pa_threaded_mainloop_lock (mainloop);
2270
2271 if (pbuf->context == NULL)
2272 goto out;
2273
2274 ret = FALSE;
2275
2276 spec.latency_time = GST_AUDIO_BASE_SINK (psink)->latency_time;
2277 if (!gst_audio_ring_buffer_parse_caps (&spec, caps))
2278 goto out;
2279
2280 if (!gst_pulse_fill_format_info (&spec, &format, &channels))
2281 goto out;
2282
2283 /* Make sure input is framed (one frame per buffer) and can be payloaded */
2284 if (!pa_format_info_is_pcm (format)) {
2285 gboolean framed = FALSE, parsed = FALSE;
2286 st = gst_caps_get_structure (caps, 0);
2287
2288 gst_structure_get_boolean (st, "framed", &framed);
2289 gst_structure_get_boolean (st, "parsed", &parsed);
2290 if ((!framed && !parsed) || gst_audio_iec61937_frame_size (&spec) <= 0)
2291 goto out;
2292 }
2293
2294 /* initialize the channel map */
2295 if (pa_format_info_is_pcm (format) &&
2296 gst_pulse_gst_to_channel_map (&channel_map, &spec))
2297 pa_format_info_set_channel_map (format, &channel_map);
2298
2299 if (pbuf->stream || pbuf->probe_stream) {
2300 /* We're already in PAUSED or above, so just reuse this stream to query
2301 * sink formats and use those. */
2302 GList *i;
2303 const char *device_name = pa_stream_get_device_name (pbuf->stream ?
2304 pbuf->stream : pbuf->probe_stream);
2305
2306 if (!(o = pa_context_get_sink_info_by_name (pbuf->context, device_name,
2307 gst_pulsesink_sink_info_cb, &device_info)))
2308 goto info_failed;
2309
2310 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2311 pa_threaded_mainloop_wait (mainloop);
2312 if (gst_pulsering_is_dead (psink, pbuf, FALSE))
2313 goto out;
2314 }
2315
2316 for (i = g_list_first (device_info.formats); i; i = g_list_next (i)) {
2317 if (pa_format_info_is_compatible ((pa_format_info *) i->data, format)) {
2318 ret = TRUE;
2319 break;
2320 }
2321 }
2322 } else {
2323 /* We're in READY, let's connect a stream to see if the format is
2324 * accepted by whatever sink we're routed to */
2325 pbuf->probe_stream = gst_pulsesink_create_probe_stream (psink, pbuf,
2326 format);
2327 if (pbuf->probe_stream)
2328 ret = TRUE;
2329 }
2330
2331 out:
2332 if (format)
2333 pa_format_info_free (format);
2334
2335 free_device_info (&device_info);
2336
2337 if (o)
2338 pa_operation_unref (o);
2339
2340 pa_threaded_mainloop_unlock (mainloop);
2341 GST_OBJECT_UNLOCK (pbuf);
2342
2343 gst_caps_replace (&spec.caps, NULL);
2344 gst_object_unref (pbuf);
2345
2346 done:
2347
2348 return ret;
2349
2350 info_failed:
2351 {
2352 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2353 ("pa_context_get_sink_input_info() failed: %s",
2354 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2355 goto out;
2356 }
2357 }
2358
2359 static void
gst_pulsesink_init(GstPulseSink * pulsesink)2360 gst_pulsesink_init (GstPulseSink * pulsesink)
2361 {
2362 pulsesink->server = NULL;
2363 pulsesink->device = NULL;
2364 pulsesink->device_info.description = NULL;
2365 pulsesink->client_name = gst_pulse_client_name ();
2366
2367 pulsesink->device_info.formats = NULL;
2368
2369 pulsesink->volume = DEFAULT_VOLUME;
2370 pulsesink->volume_set = FALSE;
2371
2372 pulsesink->mute = DEFAULT_MUTE;
2373 pulsesink->mute_set = FALSE;
2374
2375 pulsesink->notify = 0;
2376
2377 g_atomic_int_set (&pulsesink->format_lost, FALSE);
2378 pulsesink->format_lost_time = GST_CLOCK_TIME_NONE;
2379
2380 pulsesink->properties = NULL;
2381 pulsesink->proplist = NULL;
2382
2383 /* override with a custom clock */
2384 if (GST_AUDIO_BASE_SINK (pulsesink)->provided_clock)
2385 gst_object_unref (GST_AUDIO_BASE_SINK (pulsesink)->provided_clock);
2386
2387 GST_AUDIO_BASE_SINK (pulsesink)->provided_clock =
2388 gst_audio_clock_new ("GstPulseSinkClock",
2389 (GstAudioClockGetTimeFunc) gst_pulsesink_get_time, pulsesink, NULL);
2390 }
2391
2392 static void
gst_pulsesink_finalize(GObject * object)2393 gst_pulsesink_finalize (GObject * object)
2394 {
2395 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2396
2397 g_free (pulsesink->server);
2398 g_free (pulsesink->device);
2399 g_free (pulsesink->client_name);
2400 g_free (pulsesink->current_sink_name);
2401
2402 free_device_info (&pulsesink->device_info);
2403
2404 if (pulsesink->properties)
2405 gst_structure_free (pulsesink->properties);
2406 if (pulsesink->proplist)
2407 pa_proplist_free (pulsesink->proplist);
2408
2409 G_OBJECT_CLASS (parent_class)->finalize (object);
2410 }
2411
2412 static void
gst_pulsesink_set_volume(GstPulseSink * psink,gdouble volume)2413 gst_pulsesink_set_volume (GstPulseSink * psink, gdouble volume)
2414 {
2415 pa_cvolume v;
2416 pa_operation *o = NULL;
2417 GstPulseRingBuffer *pbuf;
2418 uint32_t idx;
2419
2420 if (!mainloop)
2421 goto no_mainloop;
2422
2423 pa_threaded_mainloop_lock (mainloop);
2424
2425 GST_DEBUG_OBJECT (psink, "setting volume to %f", volume);
2426
2427 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2428 if (pbuf == NULL || pbuf->stream == NULL)
2429 goto no_buffer;
2430
2431 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2432 goto no_index;
2433
2434 if (pbuf->is_pcm)
2435 gst_pulse_cvolume_from_linear (&v, pbuf->channels, volume);
2436 else
2437 /* FIXME: this will eventually be superseded by checks to see if the volume
2438 * is readable/writable */
2439 goto unlock;
2440
2441 if (!(o = pa_context_set_sink_input_volume (pbuf->context, idx,
2442 &v, NULL, NULL)))
2443 goto volume_failed;
2444
2445 /* We don't really care about the result of this call */
2446 unlock:
2447
2448 if (o)
2449 pa_operation_unref (o);
2450
2451 pa_threaded_mainloop_unlock (mainloop);
2452
2453 return;
2454
2455 /* ERRORS */
2456 no_mainloop:
2457 {
2458 psink->volume = volume;
2459 psink->volume_set = TRUE;
2460
2461 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2462 return;
2463 }
2464 no_buffer:
2465 {
2466 psink->volume = volume;
2467 psink->volume_set = TRUE;
2468
2469 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2470 goto unlock;
2471 }
2472 no_index:
2473 {
2474 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2475 goto unlock;
2476 }
2477 volume_failed:
2478 {
2479 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2480 ("pa_stream_set_sink_input_volume() failed: %s",
2481 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2482 goto unlock;
2483 }
2484 }
2485
2486 static void
gst_pulsesink_set_mute(GstPulseSink * psink,gboolean mute)2487 gst_pulsesink_set_mute (GstPulseSink * psink, gboolean mute)
2488 {
2489 pa_operation *o = NULL;
2490 GstPulseRingBuffer *pbuf;
2491 uint32_t idx;
2492
2493 if (!mainloop)
2494 goto no_mainloop;
2495
2496 pa_threaded_mainloop_lock (mainloop);
2497
2498 GST_DEBUG_OBJECT (psink, "setting mute state to %d", mute);
2499
2500 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2501 if (pbuf == NULL || pbuf->stream == NULL)
2502 goto no_buffer;
2503
2504 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2505 goto no_index;
2506
2507 if (!(o = pa_context_set_sink_input_mute (pbuf->context, idx,
2508 mute, NULL, NULL)))
2509 goto mute_failed;
2510
2511 /* We don't really care about the result of this call */
2512 unlock:
2513
2514 if (o)
2515 pa_operation_unref (o);
2516
2517 pa_threaded_mainloop_unlock (mainloop);
2518
2519 return;
2520
2521 /* ERRORS */
2522 no_mainloop:
2523 {
2524 psink->mute = mute;
2525 psink->mute_set = TRUE;
2526
2527 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2528 return;
2529 }
2530 no_buffer:
2531 {
2532 psink->mute = mute;
2533 psink->mute_set = TRUE;
2534
2535 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2536 goto unlock;
2537 }
2538 no_index:
2539 {
2540 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2541 goto unlock;
2542 }
2543 mute_failed:
2544 {
2545 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2546 ("pa_stream_set_sink_input_mute() failed: %s",
2547 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2548 goto unlock;
2549 }
2550 }
2551
2552 static void
gst_pulsesink_sink_input_info_cb(pa_context * c,const pa_sink_input_info * i,int eol,void * userdata)2553 gst_pulsesink_sink_input_info_cb (pa_context * c, const pa_sink_input_info * i,
2554 int eol, void *userdata)
2555 {
2556 GstPulseRingBuffer *pbuf;
2557 GstPulseSink *psink;
2558
2559 pbuf = GST_PULSERING_BUFFER_CAST (userdata);
2560 psink = GST_PULSESINK_CAST (GST_OBJECT_PARENT (pbuf));
2561
2562 if (!i)
2563 goto done;
2564
2565 if (!pbuf->stream)
2566 goto done;
2567
2568 /* If the index doesn't match our current stream,
2569 * it implies we just recreated the stream (caps change)
2570 */
2571 if (i->index == pa_stream_get_index (pbuf->stream)) {
2572 psink->volume = pa_sw_volume_to_linear (pa_cvolume_max (&i->volume));
2573 psink->mute = i->mute;
2574 psink->current_sink_idx = i->sink;
2575
2576 if (psink->volume > MAX_VOLUME) {
2577 GST_WARNING_OBJECT (psink, "Clipped volume from %f to %f", psink->volume,
2578 MAX_VOLUME);
2579 psink->volume = MAX_VOLUME;
2580 }
2581 }
2582
2583 done:
2584 pa_threaded_mainloop_signal (mainloop, 0);
2585 }
2586
2587 static void
gst_pulsesink_get_sink_input_info(GstPulseSink * psink,gdouble * volume,gboolean * mute)2588 gst_pulsesink_get_sink_input_info (GstPulseSink * psink, gdouble * volume,
2589 gboolean * mute)
2590 {
2591 GstPulseRingBuffer *pbuf;
2592 pa_operation *o = NULL;
2593 uint32_t idx;
2594
2595 if (!mainloop)
2596 goto no_mainloop;
2597
2598 pa_threaded_mainloop_lock (mainloop);
2599
2600 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2601 if (pbuf == NULL || pbuf->stream == NULL)
2602 goto no_buffer;
2603
2604 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2605 goto no_index;
2606
2607 if (!(o = pa_context_get_sink_input_info (pbuf->context, idx,
2608 gst_pulsesink_sink_input_info_cb, pbuf)))
2609 goto info_failed;
2610
2611 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2612 pa_threaded_mainloop_wait (mainloop);
2613 if (gst_pulsering_is_dead (psink, pbuf, TRUE))
2614 goto unlock;
2615 }
2616
2617 unlock:
2618 if (volume)
2619 *volume = psink->volume;
2620 if (mute)
2621 *mute = psink->mute;
2622
2623 if (o)
2624 pa_operation_unref (o);
2625
2626 pa_threaded_mainloop_unlock (mainloop);
2627
2628 return;
2629
2630 /* ERRORS */
2631 no_mainloop:
2632 {
2633 if (volume)
2634 *volume = psink->volume;
2635 if (mute)
2636 *mute = psink->mute;
2637
2638 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2639 return;
2640 }
2641 no_buffer:
2642 {
2643 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2644 goto unlock;
2645 }
2646 no_index:
2647 {
2648 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2649 goto unlock;
2650 }
2651 info_failed:
2652 {
2653 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2654 ("pa_context_get_sink_input_info() failed: %s",
2655 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2656 goto unlock;
2657 }
2658 }
2659
2660 static void
gst_pulsesink_current_sink_info_cb(pa_context * c,const pa_sink_info * i,int eol,void * userdata)2661 gst_pulsesink_current_sink_info_cb (pa_context * c, const pa_sink_info * i,
2662 int eol, void *userdata)
2663 {
2664 GstPulseSink *psink;
2665
2666 psink = GST_PULSESINK_CAST (userdata);
2667
2668 if (!i)
2669 goto done;
2670
2671 /* If the index doesn't match our current stream,
2672 * it implies we just recreated the stream (caps change)
2673 */
2674 if (i->index == psink->current_sink_idx) {
2675 g_free (psink->current_sink_name);
2676 psink->current_sink_name = g_strdup (i->name);
2677 }
2678
2679 done:
2680 pa_threaded_mainloop_signal (mainloop, 0);
2681 }
2682
2683 static gchar *
gst_pulsesink_get_current_device(GstPulseSink * pulsesink)2684 gst_pulsesink_get_current_device (GstPulseSink * pulsesink)
2685 {
2686 pa_operation *o = NULL;
2687 GstPulseRingBuffer *pbuf;
2688 gchar *current_sink;
2689
2690 if (!mainloop)
2691 goto no_mainloop;
2692
2693 pbuf =
2694 GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (pulsesink)->ringbuffer);
2695 if (pbuf == NULL || pbuf->stream == NULL)
2696 goto no_buffer;
2697
2698 gst_pulsesink_get_sink_input_info (pulsesink, NULL, NULL);
2699
2700 pa_threaded_mainloop_lock (mainloop);
2701
2702 if (!(o = pa_context_get_sink_info_by_index (pbuf->context,
2703 pulsesink->current_sink_idx, gst_pulsesink_current_sink_info_cb,
2704 pulsesink)))
2705 goto info_failed;
2706
2707 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2708 pa_threaded_mainloop_wait (mainloop);
2709 if (gst_pulsering_is_dead (pulsesink, pbuf, TRUE))
2710 goto unlock;
2711 }
2712
2713 unlock:
2714
2715 current_sink = g_strdup (pulsesink->current_sink_name);
2716
2717 if (o)
2718 pa_operation_unref (o);
2719
2720 pa_threaded_mainloop_unlock (mainloop);
2721
2722 return current_sink;
2723
2724 /* ERRORS */
2725 no_mainloop:
2726 {
2727 GST_DEBUG_OBJECT (pulsesink, "we have no mainloop");
2728 return NULL;
2729 }
2730 no_buffer:
2731 {
2732 GST_DEBUG_OBJECT (pulsesink, "we have no ringbuffer");
2733 return NULL;
2734 }
2735 info_failed:
2736 {
2737 GST_ELEMENT_ERROR (pulsesink, RESOURCE, FAILED,
2738 ("pa_context_get_sink_input_info() failed: %s",
2739 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2740 goto unlock;
2741 }
2742 }
2743
2744 static gchar *
gst_pulsesink_device_description(GstPulseSink * psink)2745 gst_pulsesink_device_description (GstPulseSink * psink)
2746 {
2747 GstPulseRingBuffer *pbuf;
2748 pa_operation *o = NULL;
2749 gchar *t;
2750
2751 if (!mainloop)
2752 goto no_mainloop;
2753
2754 pa_threaded_mainloop_lock (mainloop);
2755 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2756 if (pbuf == NULL)
2757 goto no_buffer;
2758
2759 free_device_info (&psink->device_info);
2760 if (!(o = pa_context_get_sink_info_by_name (pbuf->context,
2761 psink->device, gst_pulsesink_sink_info_cb, &psink->device_info)))
2762 goto info_failed;
2763
2764 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
2765 pa_threaded_mainloop_wait (mainloop);
2766 if (gst_pulsering_is_dead (psink, pbuf, FALSE))
2767 goto unlock;
2768 }
2769
2770 unlock:
2771 if (o)
2772 pa_operation_unref (o);
2773
2774 t = g_strdup (psink->device_info.description);
2775 pa_threaded_mainloop_unlock (mainloop);
2776
2777 return t;
2778
2779 /* ERRORS */
2780 no_mainloop:
2781 {
2782 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2783 return NULL;
2784 }
2785 no_buffer:
2786 {
2787 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2788 goto unlock;
2789 }
2790 info_failed:
2791 {
2792 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2793 ("pa_context_get_sink_info_by_index() failed: %s",
2794 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2795 goto unlock;
2796 }
2797 }
2798
2799 static void
gst_pulsesink_set_stream_device(GstPulseSink * psink,const gchar * device)2800 gst_pulsesink_set_stream_device (GstPulseSink * psink, const gchar * device)
2801 {
2802 pa_operation *o = NULL;
2803 GstPulseRingBuffer *pbuf;
2804 uint32_t idx;
2805
2806 if (!mainloop)
2807 goto no_mainloop;
2808
2809 pa_threaded_mainloop_lock (mainloop);
2810
2811 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2812 if (pbuf == NULL || pbuf->stream == NULL)
2813 goto no_buffer;
2814
2815 if ((idx = pa_stream_get_index (pbuf->stream)) == PA_INVALID_INDEX)
2816 goto no_index;
2817
2818
2819 GST_DEBUG_OBJECT (psink, "setting stream device to %s", device);
2820
2821 if (!(o = pa_context_move_sink_input_by_name (pbuf->context, idx, device,
2822 NULL, NULL)))
2823 goto move_failed;
2824
2825 unlock:
2826
2827 if (o)
2828 pa_operation_unref (o);
2829
2830 pa_threaded_mainloop_unlock (mainloop);
2831
2832 return;
2833
2834 /* ERRORS */
2835 no_mainloop:
2836 {
2837 GST_DEBUG_OBJECT (psink, "we have no mainloop");
2838 return;
2839 }
2840 no_buffer:
2841 {
2842 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2843 goto unlock;
2844 }
2845 no_index:
2846 {
2847 GST_DEBUG_OBJECT (psink, "we don't have a stream index");
2848 return;
2849 }
2850 move_failed:
2851 {
2852 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2853 ("pa_context_move_sink_input_by_name(%s) failed: %s", device,
2854 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2855 goto unlock;
2856 }
2857 }
2858
2859
2860 static void
gst_pulsesink_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)2861 gst_pulsesink_set_property (GObject * object,
2862 guint prop_id, const GValue * value, GParamSpec * pspec)
2863 {
2864 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2865
2866 switch (prop_id) {
2867 case PROP_SERVER:
2868 g_free (pulsesink->server);
2869 pulsesink->server = g_value_dup_string (value);
2870 break;
2871 case PROP_DEVICE:
2872 g_free (pulsesink->device);
2873 pulsesink->device = g_value_dup_string (value);
2874 gst_pulsesink_set_stream_device (pulsesink, pulsesink->device);
2875 break;
2876 case PROP_VOLUME:
2877 gst_pulsesink_set_volume (pulsesink, g_value_get_double (value));
2878 break;
2879 case PROP_MUTE:
2880 gst_pulsesink_set_mute (pulsesink, g_value_get_boolean (value));
2881 break;
2882 case PROP_CLIENT_NAME:
2883 g_free (pulsesink->client_name);
2884 if (!g_value_get_string (value)) {
2885 GST_WARNING_OBJECT (pulsesink,
2886 "Empty PulseAudio client name not allowed. Resetting to default value");
2887 pulsesink->client_name = gst_pulse_client_name ();
2888 } else
2889 pulsesink->client_name = g_value_dup_string (value);
2890 break;
2891 case PROP_STREAM_PROPERTIES:
2892 if (pulsesink->properties)
2893 gst_structure_free (pulsesink->properties);
2894 pulsesink->properties =
2895 gst_structure_copy (gst_value_get_structure (value));
2896 if (pulsesink->proplist)
2897 pa_proplist_free (pulsesink->proplist);
2898 pulsesink->proplist = gst_pulse_make_proplist (pulsesink->properties);
2899 break;
2900 default:
2901 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2902 break;
2903 }
2904 }
2905
2906 static void
gst_pulsesink_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)2907 gst_pulsesink_get_property (GObject * object,
2908 guint prop_id, GValue * value, GParamSpec * pspec)
2909 {
2910
2911 GstPulseSink *pulsesink = GST_PULSESINK_CAST (object);
2912
2913 switch (prop_id) {
2914 case PROP_SERVER:
2915 g_value_set_string (value, pulsesink->server);
2916 break;
2917 case PROP_DEVICE:
2918 g_value_set_string (value, pulsesink->device);
2919 break;
2920 case PROP_CURRENT_DEVICE:
2921 {
2922 gchar *current_device = gst_pulsesink_get_current_device (pulsesink);
2923 if (current_device)
2924 g_value_take_string (value, current_device);
2925 else
2926 g_value_set_string (value, "");
2927 break;
2928 }
2929 case PROP_DEVICE_NAME:
2930 g_value_take_string (value, gst_pulsesink_device_description (pulsesink));
2931 break;
2932 case PROP_VOLUME:
2933 {
2934 gdouble volume;
2935
2936 gst_pulsesink_get_sink_input_info (pulsesink, &volume, NULL);
2937 g_value_set_double (value, volume);
2938 break;
2939 }
2940 case PROP_MUTE:
2941 {
2942 gboolean mute;
2943
2944 gst_pulsesink_get_sink_input_info (pulsesink, NULL, &mute);
2945 g_value_set_boolean (value, mute);
2946 break;
2947 }
2948 case PROP_CLIENT_NAME:
2949 g_value_set_string (value, pulsesink->client_name);
2950 break;
2951 case PROP_STREAM_PROPERTIES:
2952 gst_value_set_structure (value, pulsesink->properties);
2953 break;
2954 default:
2955 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2956 break;
2957 }
2958 }
2959
2960 static void
gst_pulsesink_change_title(GstPulseSink * psink,const gchar * t)2961 gst_pulsesink_change_title (GstPulseSink * psink, const gchar * t)
2962 {
2963 pa_operation *o = NULL;
2964 GstPulseRingBuffer *pbuf;
2965
2966 pa_threaded_mainloop_lock (mainloop);
2967
2968 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
2969
2970 if (pbuf == NULL || pbuf->stream == NULL)
2971 goto no_buffer;
2972
2973 g_free (pbuf->stream_name);
2974 pbuf->stream_name = g_strdup (t);
2975
2976 if (!(o = pa_stream_set_name (pbuf->stream, pbuf->stream_name, NULL, NULL)))
2977 goto name_failed;
2978
2979 /* We're not interested if this operation failed or not */
2980 unlock:
2981
2982 if (o)
2983 pa_operation_unref (o);
2984 pa_threaded_mainloop_unlock (mainloop);
2985
2986 return;
2987
2988 /* ERRORS */
2989 no_buffer:
2990 {
2991 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
2992 goto unlock;
2993 }
2994 name_failed:
2995 {
2996 GST_ELEMENT_ERROR (psink, RESOURCE, FAILED,
2997 ("pa_stream_set_name() failed: %s",
2998 pa_strerror (pa_context_errno (pbuf->context))), (NULL));
2999 goto unlock;
3000 }
3001 }
3002
3003 static void
gst_pulsesink_change_props(GstPulseSink * psink,GstTagList * l)3004 gst_pulsesink_change_props (GstPulseSink * psink, GstTagList * l)
3005 {
3006 static const gchar *const map[] = {
3007 GST_TAG_TITLE, PA_PROP_MEDIA_TITLE,
3008
3009 /* might get overridden in the next iteration by GST_TAG_ARTIST */
3010 GST_TAG_PERFORMER, PA_PROP_MEDIA_ARTIST,
3011
3012 GST_TAG_ARTIST, PA_PROP_MEDIA_ARTIST,
3013 GST_TAG_LANGUAGE_CODE, PA_PROP_MEDIA_LANGUAGE,
3014 GST_TAG_LOCATION, PA_PROP_MEDIA_FILENAME,
3015 /* We might add more here later on ... */
3016 NULL
3017 };
3018 pa_proplist *pl = NULL;
3019 const gchar *const *t;
3020 gboolean empty = TRUE;
3021 pa_operation *o = NULL;
3022 GstPulseRingBuffer *pbuf;
3023
3024 pl = pa_proplist_new ();
3025
3026 for (t = map; *t; t += 2) {
3027 gchar *n = NULL;
3028
3029 if (gst_tag_list_get_string (l, *t, &n)) {
3030
3031 if (n && *n) {
3032 pa_proplist_sets (pl, *(t + 1), n);
3033 empty = FALSE;
3034 }
3035
3036 g_free (n);
3037 }
3038 }
3039 if (empty)
3040 goto finish;
3041
3042 pa_threaded_mainloop_lock (mainloop);
3043 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
3044 if (pbuf == NULL || pbuf->stream == NULL)
3045 goto no_buffer;
3046
3047 /* We're not interested if this operation failed or not */
3048 if (!(o = pa_stream_proplist_update (pbuf->stream, PA_UPDATE_REPLACE,
3049 pl, NULL, NULL))) {
3050 GST_DEBUG_OBJECT (psink, "pa_stream_proplist_update() failed");
3051 }
3052
3053 unlock:
3054
3055 if (o)
3056 pa_operation_unref (o);
3057
3058 pa_threaded_mainloop_unlock (mainloop);
3059
3060 finish:
3061
3062 if (pl)
3063 pa_proplist_free (pl);
3064
3065 return;
3066
3067 /* ERRORS */
3068 no_buffer:
3069 {
3070 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
3071 goto unlock;
3072 }
3073 }
3074
3075 static void
gst_pulsesink_flush_ringbuffer(GstPulseSink * psink)3076 gst_pulsesink_flush_ringbuffer (GstPulseSink * psink)
3077 {
3078 GstPulseRingBuffer *pbuf;
3079
3080 pa_threaded_mainloop_lock (mainloop);
3081
3082 pbuf = GST_PULSERING_BUFFER_CAST (GST_AUDIO_BASE_SINK (psink)->ringbuffer);
3083
3084 if (pbuf == NULL || pbuf->stream == NULL)
3085 goto no_buffer;
3086
3087 gst_pulsering_flush (pbuf);
3088
3089 /* Uncork if we haven't already (happens when waiting to get enough data
3090 * to send out the first time) */
3091 if (pbuf->corked)
3092 gst_pulsering_set_corked (pbuf, FALSE, FALSE);
3093
3094 /* We're not interested if this operation failed or not */
3095 unlock:
3096 pa_threaded_mainloop_unlock (mainloop);
3097
3098 return;
3099
3100 /* ERRORS */
3101 no_buffer:
3102 {
3103 GST_DEBUG_OBJECT (psink, "we have no ringbuffer");
3104 goto unlock;
3105 }
3106 }
3107
3108 static gboolean
gst_pulsesink_event(GstBaseSink * sink,GstEvent * event)3109 gst_pulsesink_event (GstBaseSink * sink, GstEvent * event)
3110 {
3111 GstPulseSink *pulsesink = GST_PULSESINK_CAST (sink);
3112
3113 switch (GST_EVENT_TYPE (event)) {
3114 case GST_EVENT_TAG:{
3115 gchar *title = NULL, *artist = NULL, *location = NULL, *description =
3116 NULL, *t = NULL, *buf = NULL;
3117 GstTagList *l;
3118
3119 gst_event_parse_tag (event, &l);
3120
3121 gst_tag_list_get_string (l, GST_TAG_TITLE, &title);
3122 gst_tag_list_get_string (l, GST_TAG_ARTIST, &artist);
3123 gst_tag_list_get_string (l, GST_TAG_LOCATION, &location);
3124 gst_tag_list_get_string (l, GST_TAG_DESCRIPTION, &description);
3125
3126 if (!artist)
3127 gst_tag_list_get_string (l, GST_TAG_PERFORMER, &artist);
3128
3129 if (title && artist)
3130 /* TRANSLATORS: 'song title' by 'artist name' */
3131 t = buf = g_strdup_printf (_("'%s' by '%s'"), g_strstrip (title),
3132 g_strstrip (artist));
3133 else if (title)
3134 t = g_strstrip (title);
3135 else if (description)
3136 t = g_strstrip (description);
3137 else if (location)
3138 t = g_strstrip (location);
3139
3140 if (t)
3141 gst_pulsesink_change_title (pulsesink, t);
3142
3143 g_free (title);
3144 g_free (artist);
3145 g_free (location);
3146 g_free (description);
3147 g_free (buf);
3148
3149 gst_pulsesink_change_props (pulsesink, l);
3150
3151 break;
3152 }
3153 case GST_EVENT_GAP:{
3154 GstClockTime timestamp, duration;
3155
3156 gst_event_parse_gap (event, ×tamp, &duration);
3157 if (duration == GST_CLOCK_TIME_NONE)
3158 gst_pulsesink_flush_ringbuffer (pulsesink);
3159 break;
3160 }
3161 case GST_EVENT_EOS:
3162 gst_pulsesink_flush_ringbuffer (pulsesink);
3163 break;
3164 default:
3165 ;
3166 }
3167
3168 return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
3169 }
3170
3171 static gboolean
gst_pulsesink_query(GstBaseSink * sink,GstQuery * query)3172 gst_pulsesink_query (GstBaseSink * sink, GstQuery * query)
3173 {
3174 GstPulseSink *pulsesink = GST_PULSESINK_CAST (sink);
3175 gboolean ret = FALSE;
3176
3177 switch (GST_QUERY_TYPE (query)) {
3178 case GST_QUERY_CAPS:
3179 {
3180 GstCaps *caps, *filter;
3181
3182 gst_query_parse_caps (query, &filter);
3183 caps = gst_pulsesink_query_getcaps (pulsesink, filter);
3184
3185 if (caps) {
3186 gst_query_set_caps_result (query, caps);
3187 gst_caps_unref (caps);
3188 ret = TRUE;
3189 }
3190 break;
3191 }
3192 case GST_QUERY_ACCEPT_CAPS:
3193 {
3194 GstCaps *caps;
3195
3196 gst_query_parse_accept_caps (query, &caps);
3197 ret = gst_pulsesink_query_acceptcaps (pulsesink, caps);
3198 gst_query_set_accept_caps_result (query, ret);
3199 ret = TRUE;
3200 break;
3201 }
3202 default:
3203 ret = GST_BASE_SINK_CLASS (parent_class)->query (sink, query);
3204 break;
3205 }
3206 return ret;
3207 }
3208
3209 static void
gst_pulsesink_release_mainloop(GstPulseSink * psink)3210 gst_pulsesink_release_mainloop (GstPulseSink * psink)
3211 {
3212 if (!mainloop)
3213 return;
3214
3215 pa_threaded_mainloop_lock (mainloop);
3216 while (psink->defer_pending) {
3217 GST_DEBUG_OBJECT (psink, "waiting for stream status message emission");
3218 pa_threaded_mainloop_wait (mainloop);
3219 }
3220 pa_threaded_mainloop_unlock (mainloop);
3221
3222 g_mutex_lock (&pa_shared_resource_mutex);
3223 mainloop_ref_ct--;
3224 if (!mainloop_ref_ct) {
3225 GST_INFO_OBJECT (psink, "terminating pa main loop thread");
3226 pa_threaded_mainloop_stop (mainloop);
3227 pa_threaded_mainloop_free (mainloop);
3228 mainloop = NULL;
3229 }
3230 g_mutex_unlock (&pa_shared_resource_mutex);
3231 }
3232
3233 static GstStateChangeReturn
gst_pulsesink_change_state(GstElement * element,GstStateChange transition)3234 gst_pulsesink_change_state (GstElement * element, GstStateChange transition)
3235 {
3236 GstPulseSink *pulsesink = GST_PULSESINK (element);
3237 GstStateChangeReturn ret;
3238
3239 switch (transition) {
3240 case GST_STATE_CHANGE_NULL_TO_READY:
3241 g_mutex_lock (&pa_shared_resource_mutex);
3242 if (!mainloop_ref_ct) {
3243 GST_INFO_OBJECT (element, "new pa main loop thread");
3244 if (!(mainloop = pa_threaded_mainloop_new ()))
3245 goto mainloop_failed;
3246 if (pa_threaded_mainloop_start (mainloop) < 0) {
3247 pa_threaded_mainloop_free (mainloop);
3248 goto mainloop_start_failed;
3249 }
3250 mainloop_ref_ct = 1;
3251 g_mutex_unlock (&pa_shared_resource_mutex);
3252 } else {
3253 GST_INFO_OBJECT (element, "reusing pa main loop thread");
3254 mainloop_ref_ct++;
3255 g_mutex_unlock (&pa_shared_resource_mutex);
3256 }
3257 break;
3258 case GST_STATE_CHANGE_READY_TO_PAUSED:
3259 gst_element_post_message (element,
3260 gst_message_new_clock_provide (GST_OBJECT_CAST (element),
3261 GST_AUDIO_BASE_SINK (pulsesink)->provided_clock, TRUE));
3262 break;
3263
3264 default:
3265 break;
3266 }
3267
3268 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3269 if (ret == GST_STATE_CHANGE_FAILURE)
3270 goto state_failure;
3271
3272 switch (transition) {
3273 case GST_STATE_CHANGE_PAUSED_TO_READY:
3274 /* format_lost is reset in release() in audiobasesink */
3275 gst_element_post_message (element,
3276 gst_message_new_clock_lost (GST_OBJECT_CAST (element),
3277 GST_AUDIO_BASE_SINK (pulsesink)->provided_clock));
3278 break;
3279 case GST_STATE_CHANGE_READY_TO_NULL:
3280 gst_pulsesink_release_mainloop (pulsesink);
3281 break;
3282 default:
3283 break;
3284 }
3285
3286 return ret;
3287
3288 /* ERRORS */
3289 mainloop_failed:
3290 {
3291 g_mutex_unlock (&pa_shared_resource_mutex);
3292 GST_ELEMENT_ERROR (pulsesink, RESOURCE, FAILED,
3293 ("pa_threaded_mainloop_new() failed"), (NULL));
3294 return GST_STATE_CHANGE_FAILURE;
3295 }
3296 mainloop_start_failed:
3297 {
3298 g_mutex_unlock (&pa_shared_resource_mutex);
3299 GST_ELEMENT_ERROR (pulsesink, RESOURCE, FAILED,
3300 ("pa_threaded_mainloop_start() failed"), (NULL));
3301 return GST_STATE_CHANGE_FAILURE;
3302 }
3303 state_failure:
3304 {
3305 if (transition == GST_STATE_CHANGE_NULL_TO_READY) {
3306 /* Clear the PA mainloop if audiobasesink failed to open the ring_buffer */
3307 g_assert (mainloop);
3308 gst_pulsesink_release_mainloop (pulsesink);
3309 }
3310 return ret;
3311 }
3312 }
3313