1 /* GStreamer |
|
2 * Copyright (C) 2005 Wim Taymans <wim@fluendo.com> |
|
3 * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net> |
|
4 * |
|
5 * gstalsasink.c: |
|
6 * |
|
7 * This library is free software; you can redistribute it and/or |
|
8 * modify it under the terms of the GNU Library General Public |
|
9 * License as published by the Free Software Foundation; either |
|
10 * version 2 of the License, or (at your option) any later version. |
|
11 * |
|
12 * This library is distributed in the hope that it will be useful, |
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
15 * Library General Public License for more details. |
|
16 * |
|
17 * You should have received a copy of the GNU Library General Public |
|
18 * License along with this library; if not, write to the |
|
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
|
20 * Boston, MA 02111-1307, USA. |
|
21 */ |
|
22 |
|
23 /** |
|
24 * SECTION:element-alsasink |
|
25 * @short_description: play audio to an ALSA device |
|
26 * @see_also: alsasrc, alsamixer |
|
27 * |
|
28 * <refsect2> |
|
29 * <para> |
|
30 * This element renders raw audio samples using the ALSA api. |
|
31 * </para> |
|
32 * <title>Example pipelines</title> |
|
33 * <para> |
|
34 * Play an Ogg/Vorbis file. |
|
35 * </para> |
|
36 * <programlisting> |
|
37 * gst-launch -v filesrc location=sine.ogg ! oggdemux ! vorbisdec ! audioconvert ! audioresample ! alsasink |
|
38 * </programlisting> |
|
39 * </refsect2> |
|
40 * |
|
41 * Last reviewed on 2006-03-01 (0.10.4) |
|
42 */ |
|
43 |
|
44 #ifdef HAVE_CONFIG_H |
|
45 #include "config.h" |
|
46 #endif |
|
47 #include <sys/ioctl.h> |
|
48 #include <fcntl.h> |
|
49 #include <errno.h> |
|
50 #include <unistd.h> |
|
51 #include <string.h> |
|
52 #include <getopt.h> |
|
53 #include <alsa/asoundlib.h> |
|
54 |
|
55 #include "gstalsa.h" |
|
56 #include "gstalsasink.h" |
|
57 #include "gstalsadeviceprobe.h" |
|
58 |
|
59 #include <gst/gst-i18n-plugin.h> |
|
60 |
|
61 /* elementfactory information */ |
|
62 static const GstElementDetails gst_alsasink_details = |
|
63 GST_ELEMENT_DETAILS ("Audio sink (ALSA)", |
|
64 "Sink/Audio", |
|
65 "Output to a sound card via ALSA", |
|
66 "Wim Taymans <wim@fluendo.com>"); |
|
67 |
|
68 #define DEFAULT_DEVICE "default" |
|
69 #define DEFAULT_DEVICE_NAME "" |
|
70 #define SPDIF_PERIOD_SIZE 1536 |
|
71 #define SPDIF_BUFFER_SIZE 15360 |
|
72 |
|
73 enum |
|
74 { |
|
75 PROP_0, |
|
76 PROP_DEVICE, |
|
77 PROP_DEVICE_NAME |
|
78 }; |
|
79 |
|
80 static void gst_alsasink_init_interfaces (GType type); |
|
81 |
|
82 GST_BOILERPLATE_FULL (GstAlsaSink, gst_alsasink, GstAudioSink, |
|
83 GST_TYPE_AUDIO_SINK, gst_alsasink_init_interfaces); |
|
84 |
|
85 static void gst_alsasink_finalise (GObject * object); |
|
86 static void gst_alsasink_set_property (GObject * object, |
|
87 guint prop_id, const GValue * value, GParamSpec * pspec); |
|
88 static void gst_alsasink_get_property (GObject * object, |
|
89 guint prop_id, GValue * value, GParamSpec * pspec); |
|
90 |
|
91 static GstCaps *gst_alsasink_getcaps (GstBaseSink * bsink); |
|
92 |
|
93 static gboolean gst_alsasink_open (GstAudioSink * asink); |
|
94 static gboolean gst_alsasink_prepare (GstAudioSink * asink, |
|
95 GstRingBufferSpec * spec); |
|
96 static gboolean gst_alsasink_unprepare (GstAudioSink * asink); |
|
97 static gboolean gst_alsasink_close (GstAudioSink * asink); |
|
98 static guint gst_alsasink_write (GstAudioSink * asink, gpointer data, |
|
99 guint length); |
|
100 static guint gst_alsasink_delay (GstAudioSink * asink); |
|
101 static void gst_alsasink_reset (GstAudioSink * asink); |
|
102 |
|
103 static gint output_ref; /* 0 */ |
|
104 static snd_output_t *output; /* NULL */ |
|
105 static GStaticMutex output_mutex = G_STATIC_MUTEX_INIT; |
|
106 |
|
107 |
|
108 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN) |
|
109 # define ALSA_SINK_FACTORY_ENDIANNESS "LITTLE_ENDIAN, BIG_ENDIAN" |
|
110 #else |
|
111 # define ALSA_SINK_FACTORY_ENDIANNESS "BIG_ENDIAN, LITTLE_ENDIAN" |
|
112 #endif |
|
113 |
|
114 static GstStaticPadTemplate alsasink_sink_factory = |
|
115 GST_STATIC_PAD_TEMPLATE ("sink", |
|
116 GST_PAD_SINK, |
|
117 GST_PAD_ALWAYS, |
|
118 GST_STATIC_CAPS ("audio/x-raw-int, " |
|
119 "endianness = (int) { " ALSA_SINK_FACTORY_ENDIANNESS " }, " |
|
120 "signed = (boolean) { TRUE, FALSE }, " |
|
121 "width = (int) 32, " |
|
122 "depth = (int) 32, " |
|
123 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]; " |
|
124 "audio/x-raw-int, " |
|
125 "endianness = (int) { " ALSA_SINK_FACTORY_ENDIANNESS " }, " |
|
126 "signed = (boolean) { TRUE, FALSE }, " |
|
127 "width = (int) 24, " |
|
128 "depth = (int) 24, " |
|
129 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]; " |
|
130 "audio/x-raw-int, " |
|
131 "endianness = (int) { " ALSA_SINK_FACTORY_ENDIANNESS " }, " |
|
132 "signed = (boolean) { TRUE, FALSE }, " |
|
133 "width = (int) 32, " |
|
134 "depth = (int) 24, " |
|
135 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]; " |
|
136 "audio/x-raw-int, " |
|
137 "endianness = (int) { " ALSA_SINK_FACTORY_ENDIANNESS " }, " |
|
138 "signed = (boolean) { TRUE, FALSE }, " |
|
139 "width = (int) 16, " |
|
140 "depth = (int) 16, " |
|
141 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]; " |
|
142 "audio/x-raw-int, " |
|
143 "signed = (boolean) { TRUE, FALSE }, " |
|
144 "width = (int) 8, " |
|
145 "depth = (int) 8, " |
|
146 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ];" |
|
147 "audio/x-iec958") |
|
148 ); |
|
149 |
|
150 static void |
|
151 gst_alsasink_finalise (GObject * object) |
|
152 { |
|
153 GstAlsaSink *sink = GST_ALSA_SINK (object); |
|
154 |
|
155 g_free (sink->device); |
|
156 g_mutex_free (sink->alsa_lock); |
|
157 |
|
158 g_static_mutex_lock (&output_mutex); |
|
159 --output_ref; |
|
160 if (output_ref == 0) { |
|
161 snd_output_close (output); |
|
162 output = NULL; |
|
163 } |
|
164 g_static_mutex_unlock (&output_mutex); |
|
165 |
|
166 G_OBJECT_CLASS (parent_class)->finalize (object); |
|
167 } |
|
168 |
|
169 static void |
|
170 gst_alsasink_init_interfaces (GType type) |
|
171 { |
|
172 gst_alsa_type_add_device_property_probe_interface (type); |
|
173 } |
|
174 |
|
175 static void |
|
176 gst_alsasink_base_init (gpointer g_class) |
|
177 { |
|
178 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class); |
|
179 |
|
180 gst_element_class_set_details (element_class, &gst_alsasink_details); |
|
181 |
|
182 gst_element_class_add_pad_template (element_class, |
|
183 gst_static_pad_template_get (&alsasink_sink_factory)); |
|
184 } |
|
185 static void |
|
186 gst_alsasink_class_init (GstAlsaSinkClass * klass) |
|
187 { |
|
188 GObjectClass *gobject_class; |
|
189 GstElementClass *gstelement_class; |
|
190 GstBaseSinkClass *gstbasesink_class; |
|
191 GstBaseAudioSinkClass *gstbaseaudiosink_class; |
|
192 GstAudioSinkClass *gstaudiosink_class; |
|
193 |
|
194 gobject_class = (GObjectClass *) klass; |
|
195 gstelement_class = (GstElementClass *) klass; |
|
196 gstbasesink_class = (GstBaseSinkClass *) klass; |
|
197 gstbaseaudiosink_class = (GstBaseAudioSinkClass *) klass; |
|
198 gstaudiosink_class = (GstAudioSinkClass *) klass; |
|
199 |
|
200 parent_class = g_type_class_peek_parent (klass); |
|
201 |
|
202 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_alsasink_finalise); |
|
203 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_alsasink_get_property); |
|
204 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_alsasink_set_property); |
|
205 |
|
206 gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_alsasink_getcaps); |
|
207 |
|
208 gstaudiosink_class->open = GST_DEBUG_FUNCPTR (gst_alsasink_open); |
|
209 gstaudiosink_class->prepare = GST_DEBUG_FUNCPTR (gst_alsasink_prepare); |
|
210 gstaudiosink_class->unprepare = GST_DEBUG_FUNCPTR (gst_alsasink_unprepare); |
|
211 gstaudiosink_class->close = GST_DEBUG_FUNCPTR (gst_alsasink_close); |
|
212 gstaudiosink_class->write = GST_DEBUG_FUNCPTR (gst_alsasink_write); |
|
213 gstaudiosink_class->delay = GST_DEBUG_FUNCPTR (gst_alsasink_delay); |
|
214 gstaudiosink_class->reset = GST_DEBUG_FUNCPTR (gst_alsasink_reset); |
|
215 |
|
216 g_object_class_install_property (gobject_class, PROP_DEVICE, |
|
217 g_param_spec_string ("device", "Device", |
|
218 "ALSA device, as defined in an asound configuration file", |
|
219 DEFAULT_DEVICE, G_PARAM_READWRITE)); |
|
220 |
|
221 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME, |
|
222 g_param_spec_string ("device-name", "Device name", |
|
223 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME, |
|
224 G_PARAM_READABLE)); |
|
225 } |
|
226 |
|
227 static void |
|
228 gst_alsasink_set_property (GObject * object, guint prop_id, |
|
229 const GValue * value, GParamSpec * pspec) |
|
230 { |
|
231 GstAlsaSink *sink; |
|
232 |
|
233 sink = GST_ALSA_SINK (object); |
|
234 |
|
235 switch (prop_id) { |
|
236 case PROP_DEVICE: |
|
237 g_free (sink->device); |
|
238 sink->device = g_value_dup_string (value); |
|
239 /* setting NULL restores the default device */ |
|
240 if (sink->device == NULL) { |
|
241 sink->device = g_strdup (DEFAULT_DEVICE); |
|
242 } |
|
243 break; |
|
244 default: |
|
245 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
|
246 break; |
|
247 } |
|
248 } |
|
249 |
|
250 static void |
|
251 gst_alsasink_get_property (GObject * object, guint prop_id, |
|
252 GValue * value, GParamSpec * pspec) |
|
253 { |
|
254 GstAlsaSink *sink; |
|
255 |
|
256 sink = GST_ALSA_SINK (object); |
|
257 |
|
258 switch (prop_id) { |
|
259 case PROP_DEVICE: |
|
260 g_value_set_string (value, sink->device); |
|
261 break; |
|
262 case PROP_DEVICE_NAME: |
|
263 g_value_take_string (value, |
|
264 gst_alsa_find_device_name (GST_OBJECT_CAST (sink), |
|
265 sink->device, sink->handle, SND_PCM_STREAM_PLAYBACK)); |
|
266 break; |
|
267 default: |
|
268 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
|
269 break; |
|
270 } |
|
271 } |
|
272 |
|
273 static void |
|
274 gst_alsasink_init (GstAlsaSink * alsasink, GstAlsaSinkClass * g_class) |
|
275 { |
|
276 GST_DEBUG_OBJECT (alsasink, "initializing alsasink"); |
|
277 |
|
278 alsasink->device = g_strdup (DEFAULT_DEVICE); |
|
279 alsasink->handle = NULL; |
|
280 alsasink->cached_caps = NULL; |
|
281 alsasink->alsa_lock = g_mutex_new (); |
|
282 |
|
283 g_static_mutex_lock (&output_mutex); |
|
284 if (output_ref == 0) { |
|
285 snd_output_stdio_attach (&output, stdout, 0); |
|
286 ++output_ref; |
|
287 } |
|
288 g_static_mutex_unlock (&output_mutex); |
|
289 } |
|
290 |
|
291 #define CHECK(call, error) \ |
|
292 G_STMT_START { \ |
|
293 if ((err = call) < 0) \ |
|
294 goto error; \ |
|
295 } G_STMT_END; |
|
296 |
|
297 static GstCaps * |
|
298 gst_alsasink_getcaps (GstBaseSink * bsink) |
|
299 { |
|
300 GstElementClass *element_class; |
|
301 GstPadTemplate *pad_template; |
|
302 GstAlsaSink *sink = GST_ALSA_SINK (bsink); |
|
303 GstCaps *caps; |
|
304 |
|
305 if (sink->handle == NULL) { |
|
306 GST_DEBUG_OBJECT (sink, "device not open, using template caps"); |
|
307 return NULL; /* base class will get template caps for us */ |
|
308 } |
|
309 |
|
310 if (sink->cached_caps) { |
|
311 GST_LOG_OBJECT (sink, "Returning cached caps"); |
|
312 return gst_caps_ref (sink->cached_caps); |
|
313 } |
|
314 |
|
315 element_class = GST_ELEMENT_GET_CLASS (sink); |
|
316 pad_template = gst_element_class_get_pad_template (element_class, "sink"); |
|
317 g_return_val_if_fail (pad_template != NULL, NULL); |
|
318 |
|
319 caps = gst_alsa_probe_supported_formats (GST_OBJECT (sink), sink->handle, |
|
320 gst_pad_template_get_caps (pad_template)); |
|
321 |
|
322 if (caps) { |
|
323 sink->cached_caps = gst_caps_ref (caps); |
|
324 } |
|
325 |
|
326 GST_INFO_OBJECT (sink, "returning caps %" GST_PTR_FORMAT, caps); |
|
327 |
|
328 return caps; |
|
329 } |
|
330 |
|
331 static int |
|
332 set_hwparams (GstAlsaSink * alsa) |
|
333 { |
|
334 guint rrate; |
|
335 gint err, dir; |
|
336 snd_pcm_hw_params_t *params; |
|
337 guint period_time, buffer_time; |
|
338 |
|
339 snd_pcm_hw_params_malloc (¶ms); |
|
340 |
|
341 GST_DEBUG_OBJECT (alsa, "Negotiating to %d channels @ %d Hz (format = %s) " |
|
342 "SPDIF (%d)", alsa->channels, alsa->rate, |
|
343 snd_pcm_format_name (alsa->format), alsa->iec958); |
|
344 |
|
345 /* start with requested values, if we cannot configure alsa for those values, |
|
346 * we set these values to -1, which will leave the default alsa values */ |
|
347 buffer_time = alsa->buffer_time; |
|
348 period_time = alsa->period_time; |
|
349 |
|
350 retry: |
|
351 /* choose all parameters */ |
|
352 CHECK (snd_pcm_hw_params_any (alsa->handle, params), no_config); |
|
353 /* set the interleaved read/write format */ |
|
354 CHECK (snd_pcm_hw_params_set_access (alsa->handle, params, alsa->access), |
|
355 wrong_access); |
|
356 /* set the sample format */ |
|
357 if (alsa->iec958) { |
|
358 /* Try to use big endian first else fallback to le and swap bytes */ |
|
359 if (snd_pcm_hw_params_set_format (alsa->handle, params, alsa->format) < 0) { |
|
360 alsa->format = SND_PCM_FORMAT_S16_LE; |
|
361 alsa->need_swap = TRUE; |
|
362 GST_DEBUG_OBJECT (alsa, "falling back to little endian with swapping"); |
|
363 } else { |
|
364 alsa->need_swap = FALSE; |
|
365 } |
|
366 } |
|
367 CHECK (snd_pcm_hw_params_set_format (alsa->handle, params, alsa->format), |
|
368 no_sample_format); |
|
369 /* set the count of channels */ |
|
370 CHECK (snd_pcm_hw_params_set_channels (alsa->handle, params, alsa->channels), |
|
371 no_channels); |
|
372 /* set the stream rate */ |
|
373 rrate = alsa->rate; |
|
374 CHECK (snd_pcm_hw_params_set_rate_near (alsa->handle, params, &rrate, NULL), |
|
375 no_rate); |
|
376 if (rrate != alsa->rate) |
|
377 goto rate_match; |
|
378 |
|
379 /* get and dump some limits */ |
|
380 { |
|
381 guint min, max; |
|
382 |
|
383 snd_pcm_hw_params_get_buffer_time_min (params, &min, &dir); |
|
384 snd_pcm_hw_params_get_buffer_time_max (params, &max, &dir); |
|
385 |
|
386 GST_DEBUG_OBJECT (alsa, "buffer time %u, min %u, max %u", |
|
387 alsa->buffer_time, min, max); |
|
388 |
|
389 snd_pcm_hw_params_get_period_time_min (params, &min, &dir); |
|
390 snd_pcm_hw_params_get_period_time_max (params, &max, &dir); |
|
391 |
|
392 GST_DEBUG_OBJECT (alsa, "period time %u, min %u, max %u", |
|
393 alsa->period_time, min, max); |
|
394 |
|
395 snd_pcm_hw_params_get_periods_min (params, &min, &dir); |
|
396 snd_pcm_hw_params_get_periods_max (params, &max, &dir); |
|
397 |
|
398 GST_DEBUG_OBJECT (alsa, "periods min %u, max %u", min, max); |
|
399 } |
|
400 |
|
401 /* now try to configure the buffer time and period time, if one |
|
402 * of those fail, we fall back to the defaults and emit a warning. */ |
|
403 if (buffer_time != -1 && !alsa->iec958) { |
|
404 /* set the buffer time */ |
|
405 if ((err = snd_pcm_hw_params_set_buffer_time_near (alsa->handle, params, |
|
406 &buffer_time, &dir)) < 0) { |
|
407 GST_ELEMENT_WARNING (alsa, RESOURCE, SETTINGS, (NULL), |
|
408 ("Unable to set buffer time %i for playback: %s", |
|
409 buffer_time, snd_strerror (err))); |
|
410 /* disable buffer_time the next round */ |
|
411 buffer_time = -1; |
|
412 goto retry; |
|
413 } |
|
414 GST_DEBUG_OBJECT (alsa, "buffer time %u", buffer_time); |
|
415 } |
|
416 if (period_time != -1 && !alsa->iec958) { |
|
417 /* set the period time */ |
|
418 if ((err = snd_pcm_hw_params_set_period_time_near (alsa->handle, params, |
|
419 &period_time, &dir)) < 0) { |
|
420 GST_ELEMENT_WARNING (alsa, RESOURCE, SETTINGS, (NULL), |
|
421 ("Unable to set period time %i for playback: %s", |
|
422 period_time, snd_strerror (err))); |
|
423 /* disable period_time the next round */ |
|
424 period_time = -1; |
|
425 goto retry; |
|
426 } |
|
427 GST_DEBUG_OBJECT (alsa, "period time %u", period_time); |
|
428 } |
|
429 |
|
430 /* Set buffer size and period size manually for SPDIF */ |
|
431 if (G_UNLIKELY (alsa->iec958)) { |
|
432 snd_pcm_uframes_t buffer_size = SPDIF_BUFFER_SIZE; |
|
433 snd_pcm_uframes_t period_size = SPDIF_PERIOD_SIZE; |
|
434 |
|
435 CHECK (snd_pcm_hw_params_set_buffer_size_near (alsa->handle, params, |
|
436 &buffer_size), buffer_size); |
|
437 CHECK (snd_pcm_hw_params_set_period_size_near (alsa->handle, params, |
|
438 &period_size, NULL), period_size); |
|
439 } |
|
440 |
|
441 /* write the parameters to device */ |
|
442 CHECK (snd_pcm_hw_params (alsa->handle, params), set_hw_params); |
|
443 |
|
444 /* now get the configured values */ |
|
445 CHECK (snd_pcm_hw_params_get_buffer_size (params, &alsa->buffer_size), |
|
446 buffer_size); |
|
447 CHECK (snd_pcm_hw_params_get_period_size (params, &alsa->period_size, &dir), |
|
448 period_size); |
|
449 |
|
450 GST_DEBUG_OBJECT (alsa, "buffer size %lu, period size %lu", alsa->buffer_size, |
|
451 alsa->period_size); |
|
452 |
|
453 snd_pcm_hw_params_free (params); |
|
454 return 0; |
|
455 |
|
456 /* ERRORS */ |
|
457 no_config: |
|
458 { |
|
459 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL), |
|
460 ("Broken configuration for playback: no configurations available: %s", |
|
461 snd_strerror (err))); |
|
462 snd_pcm_hw_params_free (params); |
|
463 return err; |
|
464 } |
|
465 wrong_access: |
|
466 { |
|
467 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL), |
|
468 ("Access type not available for playback: %s", snd_strerror (err))); |
|
469 snd_pcm_hw_params_free (params); |
|
470 return err; |
|
471 } |
|
472 no_sample_format: |
|
473 { |
|
474 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL), |
|
475 ("Sample format not available for playback: %s", snd_strerror (err))); |
|
476 snd_pcm_hw_params_free (params); |
|
477 return err; |
|
478 } |
|
479 no_channels: |
|
480 { |
|
481 gchar *msg = NULL; |
|
482 |
|
483 if ((alsa->channels) == 1) |
|
484 msg = g_strdup (_("Could not open device for playback in mono mode.")); |
|
485 if ((alsa->channels) == 2) |
|
486 msg = g_strdup (_("Could not open device for playback in stereo mode.")); |
|
487 if ((alsa->channels) > 2) |
|
488 msg = |
|
489 g_strdup_printf (_ |
|
490 ("Could not open device for playback in %d-channel mode."), |
|
491 alsa->channels); |
|
492 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (msg), (snd_strerror (err))); |
|
493 g_free (msg); |
|
494 snd_pcm_hw_params_free (params); |
|
495 return err; |
|
496 } |
|
497 no_rate: |
|
498 { |
|
499 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL), |
|
500 ("Rate %iHz not available for playback: %s", |
|
501 alsa->rate, snd_strerror (err))); |
|
502 return err; |
|
503 } |
|
504 rate_match: |
|
505 { |
|
506 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL), |
|
507 ("Rate doesn't match (requested %iHz, get %iHz)", alsa->rate, err)); |
|
508 snd_pcm_hw_params_free (params); |
|
509 return -EINVAL; |
|
510 } |
|
511 buffer_size: |
|
512 { |
|
513 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL), |
|
514 ("Unable to get buffer size for playback: %s", snd_strerror (err))); |
|
515 snd_pcm_hw_params_free (params); |
|
516 return err; |
|
517 } |
|
518 period_size: |
|
519 { |
|
520 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL), |
|
521 ("Unable to get period size for playback: %s", snd_strerror (err))); |
|
522 snd_pcm_hw_params_free (params); |
|
523 return err; |
|
524 } |
|
525 set_hw_params: |
|
526 { |
|
527 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL), |
|
528 ("Unable to set hw params for playback: %s", snd_strerror (err))); |
|
529 snd_pcm_hw_params_free (params); |
|
530 return err; |
|
531 } |
|
532 } |
|
533 |
|
534 static int |
|
535 set_swparams (GstAlsaSink * alsa) |
|
536 { |
|
537 int err; |
|
538 snd_pcm_sw_params_t *params; |
|
539 |
|
540 snd_pcm_sw_params_malloc (¶ms); |
|
541 |
|
542 /* get the current swparams */ |
|
543 CHECK (snd_pcm_sw_params_current (alsa->handle, params), no_config); |
|
544 /* start the transfer when the buffer is almost full: */ |
|
545 /* (buffer_size / avail_min) * avail_min */ |
|
546 CHECK (snd_pcm_sw_params_set_start_threshold (alsa->handle, params, |
|
547 (alsa->buffer_size / alsa->period_size) * alsa->period_size), |
|
548 start_threshold); |
|
549 |
|
550 /* allow the transfer when at least period_size samples can be processed */ |
|
551 CHECK (snd_pcm_sw_params_set_avail_min (alsa->handle, params, |
|
552 alsa->period_size), set_avail); |
|
553 |
|
554 #if GST_CHECK_ALSA_VERSION(1,0,16) |
|
555 /* snd_pcm_sw_params_set_xfer_align() is deprecated, alignment is always 1 */ |
|
556 #else |
|
557 /* align all transfers to 1 sample */ |
|
558 CHECK (snd_pcm_sw_params_set_xfer_align (alsa->handle, params, 1), set_align); |
|
559 #endif |
|
560 |
|
561 /* write the parameters to the playback device */ |
|
562 CHECK (snd_pcm_sw_params (alsa->handle, params), set_sw_params); |
|
563 |
|
564 snd_pcm_sw_params_free (params); |
|
565 return 0; |
|
566 |
|
567 /* ERRORS */ |
|
568 no_config: |
|
569 { |
|
570 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL), |
|
571 ("Unable to determine current swparams for playback: %s", |
|
572 snd_strerror (err))); |
|
573 snd_pcm_sw_params_free (params); |
|
574 return err; |
|
575 } |
|
576 start_threshold: |
|
577 { |
|
578 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL), |
|
579 ("Unable to set start threshold mode for playback: %s", |
|
580 snd_strerror (err))); |
|
581 snd_pcm_sw_params_free (params); |
|
582 return err; |
|
583 } |
|
584 set_avail: |
|
585 { |
|
586 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL), |
|
587 ("Unable to set avail min for playback: %s", snd_strerror (err))); |
|
588 snd_pcm_sw_params_free (params); |
|
589 return err; |
|
590 } |
|
591 #if !GST_CHECK_ALSA_VERSION(1,0,16) |
|
592 set_align: |
|
593 { |
|
594 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL), |
|
595 ("Unable to set transfer align for playback: %s", snd_strerror (err))); |
|
596 snd_pcm_sw_params_free (params); |
|
597 return err; |
|
598 } |
|
599 #endif |
|
600 set_sw_params: |
|
601 { |
|
602 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL), |
|
603 ("Unable to set sw params for playback: %s", snd_strerror (err))); |
|
604 snd_pcm_sw_params_free (params); |
|
605 return err; |
|
606 } |
|
607 } |
|
608 |
|
609 static gboolean |
|
610 alsasink_parse_spec (GstAlsaSink * alsa, GstRingBufferSpec * spec) |
|
611 { |
|
612 /* Initialize our boolean */ |
|
613 alsa->iec958 = FALSE; |
|
614 |
|
615 switch (spec->type) { |
|
616 case GST_BUFTYPE_LINEAR: |
|
617 GST_DEBUG_OBJECT (alsa, |
|
618 "Linear format : depth=%d, width=%d, sign=%d, bigend=%d", spec->depth, |
|
619 spec->width, spec->sign, spec->bigend); |
|
620 |
|
621 alsa->format = snd_pcm_build_linear_format (spec->depth, spec->width, |
|
622 spec->sign ? 0 : 1, spec->bigend ? 1 : 0); |
|
623 break; |
|
624 case GST_BUFTYPE_FLOAT: |
|
625 switch (spec->format) { |
|
626 case GST_FLOAT32_LE: |
|
627 alsa->format = SND_PCM_FORMAT_FLOAT_LE; |
|
628 break; |
|
629 case GST_FLOAT32_BE: |
|
630 alsa->format = SND_PCM_FORMAT_FLOAT_BE; |
|
631 break; |
|
632 case GST_FLOAT64_LE: |
|
633 alsa->format = SND_PCM_FORMAT_FLOAT64_LE; |
|
634 break; |
|
635 case GST_FLOAT64_BE: |
|
636 alsa->format = SND_PCM_FORMAT_FLOAT64_BE; |
|
637 break; |
|
638 default: |
|
639 goto error; |
|
640 } |
|
641 break; |
|
642 case GST_BUFTYPE_A_LAW: |
|
643 alsa->format = SND_PCM_FORMAT_A_LAW; |
|
644 break; |
|
645 case GST_BUFTYPE_MU_LAW: |
|
646 alsa->format = SND_PCM_FORMAT_MU_LAW; |
|
647 break; |
|
648 case GST_BUFTYPE_IEC958: |
|
649 alsa->format = SND_PCM_FORMAT_S16_BE; |
|
650 alsa->iec958 = TRUE; |
|
651 break; |
|
652 default: |
|
653 goto error; |
|
654 |
|
655 } |
|
656 alsa->rate = spec->rate; |
|
657 alsa->channels = spec->channels; |
|
658 alsa->buffer_time = spec->buffer_time; |
|
659 alsa->period_time = spec->latency_time; |
|
660 alsa->access = SND_PCM_ACCESS_RW_INTERLEAVED; |
|
661 |
|
662 return TRUE; |
|
663 |
|
664 /* ERRORS */ |
|
665 error: |
|
666 { |
|
667 return FALSE; |
|
668 } |
|
669 } |
|
670 |
|
671 static gboolean |
|
672 gst_alsasink_open (GstAudioSink * asink) |
|
673 { |
|
674 GstAlsaSink *alsa; |
|
675 gint err; |
|
676 |
|
677 alsa = GST_ALSA_SINK (asink); |
|
678 |
|
679 CHECK (snd_pcm_open (&alsa->handle, alsa->device, SND_PCM_STREAM_PLAYBACK, |
|
680 SND_PCM_NONBLOCK), open_error); |
|
681 GST_LOG_OBJECT (alsa, "Opened device %s", alsa->device); |
|
682 |
|
683 return TRUE; |
|
684 |
|
685 /* ERRORS */ |
|
686 open_error: |
|
687 { |
|
688 if (err == -EBUSY) { |
|
689 GST_ELEMENT_ERROR (alsa, RESOURCE, BUSY, |
|
690 (_("Could not open audio device for playback. " |
|
691 "Device is being used by another application.")), |
|
692 ("Device '%s' is busy", alsa->device)); |
|
693 } else { |
|
694 GST_ELEMENT_ERROR (alsa, RESOURCE, OPEN_WRITE, |
|
695 (_("Could not open audio device for playback.")), |
|
696 ("Playback open error on device '%s': %s", alsa->device, |
|
697 snd_strerror (err))); |
|
698 } |
|
699 return FALSE; |
|
700 } |
|
701 } |
|
702 |
|
703 static gboolean |
|
704 gst_alsasink_prepare (GstAudioSink * asink, GstRingBufferSpec * spec) |
|
705 { |
|
706 GstAlsaSink *alsa; |
|
707 gint err; |
|
708 |
|
709 alsa = GST_ALSA_SINK (asink); |
|
710 |
|
711 if (spec->format == GST_IEC958) { |
|
712 snd_pcm_close (alsa->handle); |
|
713 alsa->handle = gst_alsa_open_iec958_pcm (GST_OBJECT (alsa)); |
|
714 if (G_UNLIKELY (!alsa->handle)) { |
|
715 goto no_iec958; |
|
716 } |
|
717 } |
|
718 |
|
719 if (!alsasink_parse_spec (alsa, spec)) |
|
720 goto spec_parse; |
|
721 |
|
722 CHECK (snd_pcm_nonblock (alsa->handle, 0), non_block); |
|
723 |
|
724 CHECK (set_hwparams (alsa), hw_params_failed); |
|
725 CHECK (set_swparams (alsa), sw_params_failed); |
|
726 |
|
727 alsa->bytes_per_sample = spec->bytes_per_sample; |
|
728 spec->segsize = alsa->period_size * spec->bytes_per_sample; |
|
729 spec->segtotal = alsa->buffer_size / alsa->period_size; |
|
730 |
|
731 { |
|
732 snd_output_t *out_buf = NULL; |
|
733 char *msg = NULL; |
|
734 |
|
735 snd_output_buffer_open (&out_buf); |
|
736 snd_pcm_dump_hw_setup (alsa->handle, out_buf); |
|
737 snd_output_buffer_string (out_buf, &msg); |
|
738 GST_DEBUG_OBJECT (alsa, "Hardware setup: \n%s", msg); |
|
739 snd_output_close (out_buf); |
|
740 snd_output_buffer_open (&out_buf); |
|
741 snd_pcm_dump_sw_setup (alsa->handle, out_buf); |
|
742 snd_output_buffer_string (out_buf, &msg); |
|
743 GST_DEBUG_OBJECT (alsa, "Software setup: \n%s", msg); |
|
744 snd_output_close (out_buf); |
|
745 } |
|
746 |
|
747 return TRUE; |
|
748 |
|
749 /* ERRORS */ |
|
750 no_iec958: |
|
751 { |
|
752 GST_ELEMENT_ERROR (alsa, RESOURCE, OPEN_WRITE, (NULL), |
|
753 ("Could not open IEC958 (SPDIF) device for playback")); |
|
754 return FALSE; |
|
755 } |
|
756 spec_parse: |
|
757 { |
|
758 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL), |
|
759 ("Error parsing spec")); |
|
760 return FALSE; |
|
761 } |
|
762 non_block: |
|
763 { |
|
764 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL), |
|
765 ("Could not set device to blocking: %s", snd_strerror (err))); |
|
766 return FALSE; |
|
767 } |
|
768 hw_params_failed: |
|
769 { |
|
770 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL), |
|
771 ("Setting of hwparams failed: %s", snd_strerror (err))); |
|
772 return FALSE; |
|
773 } |
|
774 sw_params_failed: |
|
775 { |
|
776 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL), |
|
777 ("Setting of swparams failed: %s", snd_strerror (err))); |
|
778 return FALSE; |
|
779 } |
|
780 } |
|
781 |
|
782 static gboolean |
|
783 gst_alsasink_unprepare (GstAudioSink * asink) |
|
784 { |
|
785 GstAlsaSink *alsa; |
|
786 gint err; |
|
787 |
|
788 alsa = GST_ALSA_SINK (asink); |
|
789 |
|
790 CHECK (snd_pcm_drop (alsa->handle), drop); |
|
791 |
|
792 CHECK (snd_pcm_hw_free (alsa->handle), hw_free); |
|
793 |
|
794 CHECK (snd_pcm_nonblock (alsa->handle, 1), non_block); |
|
795 |
|
796 return TRUE; |
|
797 |
|
798 /* ERRORS */ |
|
799 drop: |
|
800 { |
|
801 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL), |
|
802 ("Could not drop samples: %s", snd_strerror (err))); |
|
803 return FALSE; |
|
804 } |
|
805 hw_free: |
|
806 { |
|
807 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL), |
|
808 ("Could not free hw params: %s", snd_strerror (err))); |
|
809 return FALSE; |
|
810 } |
|
811 non_block: |
|
812 { |
|
813 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL), |
|
814 ("Could not set device to nonblocking: %s", snd_strerror (err))); |
|
815 return FALSE; |
|
816 } |
|
817 } |
|
818 |
|
819 static gboolean |
|
820 gst_alsasink_close (GstAudioSink * asink) |
|
821 { |
|
822 GstAlsaSink *alsa = GST_ALSA_SINK (asink); |
|
823 gint err; |
|
824 |
|
825 if (alsa->handle) { |
|
826 CHECK (snd_pcm_close (alsa->handle), close_error); |
|
827 alsa->handle = NULL; |
|
828 } |
|
829 gst_caps_replace (&alsa->cached_caps, NULL); |
|
830 |
|
831 return TRUE; |
|
832 |
|
833 /* ERRORS */ |
|
834 close_error: |
|
835 { |
|
836 GST_ELEMENT_ERROR (alsa, RESOURCE, CLOSE, (NULL), |
|
837 ("Playback close error: %s", snd_strerror (err))); |
|
838 return FALSE; |
|
839 } |
|
840 } |
|
841 |
|
842 |
|
843 /* |
|
844 * Underrun and suspend recovery |
|
845 */ |
|
846 static gint |
|
847 xrun_recovery (GstAlsaSink * alsa, snd_pcm_t * handle, gint err) |
|
848 { |
|
849 GST_DEBUG_OBJECT (alsa, "xrun recovery %d", err); |
|
850 |
|
851 if (err == -EPIPE) { /* under-run */ |
|
852 err = snd_pcm_prepare (handle); |
|
853 if (err < 0) |
|
854 GST_WARNING_OBJECT (alsa, |
|
855 "Can't recovery from underrun, prepare failed: %s", |
|
856 snd_strerror (err)); |
|
857 return 0; |
|
858 } else if (err == -ESTRPIPE) { |
|
859 while ((err = snd_pcm_resume (handle)) == -EAGAIN) |
|
860 g_usleep (100); /* wait until the suspend flag is released */ |
|
861 |
|
862 if (err < 0) { |
|
863 err = snd_pcm_prepare (handle); |
|
864 if (err < 0) |
|
865 GST_WARNING_OBJECT (alsa, |
|
866 "Can't recovery from suspend, prepare failed: %s", |
|
867 snd_strerror (err)); |
|
868 } |
|
869 return 0; |
|
870 } |
|
871 return err; |
|
872 } |
|
873 |
|
874 static guint |
|
875 gst_alsasink_write (GstAudioSink * asink, gpointer data, guint length) |
|
876 { |
|
877 GstAlsaSink *alsa; |
|
878 gint err; |
|
879 gint cptr; |
|
880 gint16 *ptr = data; |
|
881 |
|
882 alsa = GST_ALSA_SINK (asink); |
|
883 |
|
884 if (alsa->iec958 && alsa->need_swap) { |
|
885 guint i; |
|
886 |
|
887 GST_DEBUG_OBJECT (asink, "swapping bytes"); |
|
888 for (i = 0; i < length / 2; i++) { |
|
889 ptr[i] = GUINT16_SWAP_LE_BE (ptr[i]); |
|
890 } |
|
891 } |
|
892 |
|
893 GST_LOG_OBJECT (asink, "received audio samples buffer of %u bytes", length); |
|
894 |
|
895 cptr = length / alsa->bytes_per_sample; |
|
896 |
|
897 GST_ALSA_SINK_LOCK (asink); |
|
898 while (cptr > 0) { |
|
899 err = snd_pcm_writei (alsa->handle, ptr, cptr); |
|
900 |
|
901 GST_DEBUG_OBJECT (asink, "written %d frames out of %d", err, cptr); |
|
902 if (err < 0) { |
|
903 GST_DEBUG_OBJECT (asink, "Write error: %s", snd_strerror (err)); |
|
904 if (err == -EAGAIN) { |
|
905 continue; |
|
906 } else if (xrun_recovery (alsa, alsa->handle, err) < 0) { |
|
907 goto write_error; |
|
908 } |
|
909 continue; |
|
910 } |
|
911 |
|
912 ptr += snd_pcm_frames_to_bytes (alsa->handle, err); |
|
913 cptr -= err; |
|
914 } |
|
915 GST_ALSA_SINK_UNLOCK (asink); |
|
916 |
|
917 return length - (cptr * alsa->bytes_per_sample); |
|
918 |
|
919 write_error: |
|
920 { |
|
921 GST_ALSA_SINK_UNLOCK (asink); |
|
922 return length; /* skip one period */ |
|
923 } |
|
924 } |
|
925 |
|
926 static guint |
|
927 gst_alsasink_delay (GstAudioSink * asink) |
|
928 { |
|
929 GstAlsaSink *alsa; |
|
930 snd_pcm_sframes_t delay; |
|
931 int res; |
|
932 |
|
933 alsa = GST_ALSA_SINK (asink); |
|
934 |
|
935 res = snd_pcm_delay (alsa->handle, &delay); |
|
936 if (G_UNLIKELY (res < 0)) { |
|
937 /* on errors, report 0 delay */ |
|
938 GST_DEBUG_OBJECT (alsa, "snd_pcm_delay returned %d", res); |
|
939 delay = 0; |
|
940 } |
|
941 if (G_UNLIKELY (delay < 0)) { |
|
942 /* make sure we never return a negative delay */ |
|
943 GST_WARNING_OBJECT (alsa, "snd_pcm_delay returned negative delay"); |
|
944 delay = 0; |
|
945 } |
|
946 |
|
947 return delay; |
|
948 } |
|
949 |
|
950 static void |
|
951 gst_alsasink_reset (GstAudioSink * asink) |
|
952 { |
|
953 GstAlsaSink *alsa; |
|
954 gint err; |
|
955 |
|
956 alsa = GST_ALSA_SINK (asink); |
|
957 |
|
958 GST_ALSA_SINK_LOCK (asink); |
|
959 GST_DEBUG_OBJECT (alsa, "drop"); |
|
960 CHECK (snd_pcm_drop (alsa->handle), drop_error); |
|
961 GST_DEBUG_OBJECT (alsa, "prepare"); |
|
962 CHECK (snd_pcm_prepare (alsa->handle), prepare_error); |
|
963 GST_DEBUG_OBJECT (alsa, "reset done"); |
|
964 GST_ALSA_SINK_UNLOCK (asink); |
|
965 |
|
966 return; |
|
967 |
|
968 /* ERRORS */ |
|
969 drop_error: |
|
970 { |
|
971 GST_ERROR_OBJECT (alsa, "alsa-reset: pcm drop error: %s", |
|
972 snd_strerror (err)); |
|
973 GST_ALSA_SINK_UNLOCK (asink); |
|
974 return; |
|
975 } |
|
976 prepare_error: |
|
977 { |
|
978 GST_ERROR_OBJECT (alsa, "alsa-reset: pcm prepare error: %s", |
|
979 snd_strerror (err)); |
|
980 GST_ALSA_SINK_UNLOCK (asink); |
|
981 return; |
|
982 } |
|
983 } |
|