• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  GStreamer pulseaudio plugin
3  *
4  *  Copyright (c) 2004-2008 Lennart Poettering
5  *
6  *  gst-pulse is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU Lesser General Public License as
8  *  published by the Free Software Foundation; either version 2.1 of the
9  *  License, or (at your option) any later version.
10  *
11  *  gst-pulse is distributed in the hope that it will be useful, but
12  *  WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with gst-pulse; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
19  *  USA.
20  */
21 
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 
26 #include <gst/audio/audio.h>
27 
28 #include "pulseutil.h"
29 
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>            /* getpid on UNIX */
32 #endif
33 #ifdef HAVE_PROCESS_H
34 # include <process.h>           /* getpid on win32 */
35 #endif
36 
37 static const struct
38 {
39   GstAudioChannelPosition gst_pos;
40   pa_channel_position_t pa_pos;
41 } gst_pa_pos_table[] = {
42   {
43   GST_AUDIO_CHANNEL_POSITION_MONO, PA_CHANNEL_POSITION_MONO}, {
44   GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_LEFT}, {
45   GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT, PA_CHANNEL_POSITION_FRONT_RIGHT}, {
46   GST_AUDIO_CHANNEL_POSITION_REAR_CENTER, PA_CHANNEL_POSITION_REAR_CENTER}, {
47   GST_AUDIO_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_LEFT}, {
48   GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT, PA_CHANNEL_POSITION_REAR_RIGHT}, {
49   GST_AUDIO_CHANNEL_POSITION_LFE1, PA_CHANNEL_POSITION_LFE}, {
50   GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER, PA_CHANNEL_POSITION_FRONT_CENTER}, {
51   GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER,
52         PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER}, {
53   GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER,
54         PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER}, {
55   GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT, PA_CHANNEL_POSITION_SIDE_LEFT}, {
56   GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT, PA_CHANNEL_POSITION_SIDE_RIGHT}, {
57   GST_AUDIO_CHANNEL_POSITION_TOP_CENTER, PA_CHANNEL_POSITION_TOP_CENTER}, {
58   GST_AUDIO_CHANNEL_POSITION_TOP_FRONT_LEFT,
59         PA_CHANNEL_POSITION_TOP_FRONT_LEFT}, {
60   GST_AUDIO_CHANNEL_POSITION_TOP_FRONT_RIGHT,
61         PA_CHANNEL_POSITION_TOP_FRONT_RIGHT}, {
62   GST_AUDIO_CHANNEL_POSITION_TOP_FRONT_CENTER,
63         PA_CHANNEL_POSITION_TOP_FRONT_CENTER}, {
64   GST_AUDIO_CHANNEL_POSITION_TOP_REAR_LEFT, PA_CHANNEL_POSITION_TOP_REAR_LEFT}, {
65   GST_AUDIO_CHANNEL_POSITION_TOP_REAR_RIGHT,
66         PA_CHANNEL_POSITION_TOP_REAR_RIGHT}, {
67   GST_AUDIO_CHANNEL_POSITION_TOP_REAR_CENTER,
68         PA_CHANNEL_POSITION_TOP_REAR_CENTER}, {
69   GST_AUDIO_CHANNEL_POSITION_NONE, PA_CHANNEL_POSITION_INVALID}
70 };
71 
72 static gboolean
gstaudioformat_to_pasampleformat(GstAudioFormat format,pa_sample_format_t * sf)73 gstaudioformat_to_pasampleformat (GstAudioFormat format,
74     pa_sample_format_t * sf)
75 {
76   switch (format) {
77     case GST_AUDIO_FORMAT_U8:
78       *sf = PA_SAMPLE_U8;
79       break;
80     case GST_AUDIO_FORMAT_S16LE:
81       *sf = PA_SAMPLE_S16LE;
82       break;
83     case GST_AUDIO_FORMAT_S16BE:
84       *sf = PA_SAMPLE_S16BE;
85       break;
86     case GST_AUDIO_FORMAT_F32LE:
87       *sf = PA_SAMPLE_FLOAT32LE;
88       break;
89     case GST_AUDIO_FORMAT_F32BE:
90       *sf = PA_SAMPLE_FLOAT32BE;
91       break;
92     case GST_AUDIO_FORMAT_S32LE:
93       *sf = PA_SAMPLE_S32LE;
94       break;
95     case GST_AUDIO_FORMAT_S32BE:
96       *sf = PA_SAMPLE_S32BE;
97       break;
98     case GST_AUDIO_FORMAT_S24LE:
99       *sf = PA_SAMPLE_S24LE;
100       break;
101     case GST_AUDIO_FORMAT_S24BE:
102       *sf = PA_SAMPLE_S24BE;
103       break;
104     case GST_AUDIO_FORMAT_S24_32LE:
105       *sf = PA_SAMPLE_S24_32LE;
106       break;
107     case GST_AUDIO_FORMAT_S24_32BE:
108       *sf = PA_SAMPLE_S24_32BE;
109       break;
110     default:
111       return FALSE;
112   }
113   return TRUE;
114 }
115 
116 gboolean
gst_pulse_fill_sample_spec(GstAudioRingBufferSpec * spec,pa_sample_spec * ss)117 gst_pulse_fill_sample_spec (GstAudioRingBufferSpec * spec, pa_sample_spec * ss)
118 {
119   if (spec->type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_RAW) {
120     if (!gstaudioformat_to_pasampleformat (GST_AUDIO_INFO_FORMAT (&spec->info),
121             &ss->format))
122       return FALSE;
123   } else if (spec->type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_MU_LAW) {
124     ss->format = PA_SAMPLE_ULAW;
125   } else if (spec->type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_A_LAW) {
126     ss->format = PA_SAMPLE_ALAW;
127   } else
128     return FALSE;
129 
130   ss->channels = GST_AUDIO_INFO_CHANNELS (&spec->info);
131   ss->rate = GST_AUDIO_INFO_RATE (&spec->info);
132 
133   if (!pa_sample_spec_valid (ss))
134     return FALSE;
135 
136   return TRUE;
137 }
138 
139 gboolean
gst_pulse_fill_format_info(GstAudioRingBufferSpec * spec,pa_format_info ** f,guint * channels)140 gst_pulse_fill_format_info (GstAudioRingBufferSpec * spec, pa_format_info ** f,
141     guint * channels)
142 {
143   pa_format_info *format;
144   pa_sample_format_t sf = PA_SAMPLE_INVALID;
145   GstAudioInfo *ainfo = &spec->info;
146 
147   format = pa_format_info_new ();
148 
149   if (spec->type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_MU_LAW) {
150     format->encoding = PA_ENCODING_PCM;
151     sf = PA_SAMPLE_ULAW;
152   } else if (spec->type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_A_LAW) {
153     format->encoding = PA_ENCODING_PCM;
154     sf = PA_SAMPLE_ALAW;
155   } else if (spec->type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_RAW) {
156     format->encoding = PA_ENCODING_PCM;
157     if (!gstaudioformat_to_pasampleformat (GST_AUDIO_INFO_FORMAT (ainfo), &sf))
158       goto fail;
159   } else if (spec->type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3) {
160     format->encoding = PA_ENCODING_AC3_IEC61937;
161   } else if (spec->type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_EAC3) {
162     format->encoding = PA_ENCODING_EAC3_IEC61937;
163   } else if (spec->type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS) {
164     format->encoding = PA_ENCODING_DTS_IEC61937;
165   } else if (spec->type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_MPEG) {
166     format->encoding = PA_ENCODING_MPEG_IEC61937;
167 #if PA_CHECK_VERSION(3,99,0)
168   } else if (spec->type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_MPEG2_AAC) {
169     format->encoding = PA_ENCODING_MPEG2_AAC_IEC61937;
170   } else if (spec->type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_MPEG4_AAC) {
171     /* HACK. treat MPEG4 AAC as MPEG2 AAC for the moment */
172     format->encoding = PA_ENCODING_MPEG2_AAC_IEC61937;
173 #endif
174   } else {
175     goto fail;
176   }
177 
178   if (format->encoding == PA_ENCODING_PCM) {
179     pa_format_info_set_sample_format (format, sf);
180     pa_format_info_set_channels (format, GST_AUDIO_INFO_CHANNELS (ainfo));
181   }
182 
183   pa_format_info_set_rate (format, GST_AUDIO_INFO_RATE (ainfo));
184 
185   if (!pa_format_info_valid (format))
186     goto fail;
187 
188   *f = format;
189   *channels = GST_AUDIO_INFO_CHANNELS (ainfo);
190 
191   return TRUE;
192 
193 fail:
194   if (format)
195     pa_format_info_free (format);
196   return FALSE;
197 }
198 
199 const char *
gst_pulse_sample_format_to_caps_format(pa_sample_format_t sf)200 gst_pulse_sample_format_to_caps_format (pa_sample_format_t sf)
201 {
202   switch (sf) {
203     case PA_SAMPLE_U8:
204       return "U8";
205 
206     case PA_SAMPLE_S16LE:
207       return "S16LE";
208 
209     case PA_SAMPLE_S16BE:
210       return "S16BE";
211 
212     case PA_SAMPLE_FLOAT32LE:
213       return "F32LE";
214 
215     case PA_SAMPLE_FLOAT32BE:
216       return "F32BE";
217 
218     case PA_SAMPLE_S32LE:
219       return "S32LE";
220 
221     case PA_SAMPLE_S32BE:
222       return "S32BE";
223 
224     case PA_SAMPLE_S24LE:
225       return "S24LE";
226 
227     case PA_SAMPLE_S24BE:
228       return "S24BE";
229 
230     case PA_SAMPLE_S24_32LE:
231       return "S24_32LE";
232 
233     case PA_SAMPLE_S24_32BE:
234       return "S24_32BE";
235 
236     default:
237       return NULL;
238   }
239 }
240 
241 /* PATH_MAX is not defined everywhere, e.g. on GNU Hurd */
242 #ifndef PATH_MAX
243 #define PATH_MAX 4096
244 #endif
245 
246 gchar *
gst_pulse_client_name(void)247 gst_pulse_client_name (void)
248 {
249   gchar buf[PATH_MAX];
250 
251   const char *c;
252 
253   if ((c = g_get_application_name ()))
254     return g_strdup (c);
255   else if (pa_get_binary_name (buf, sizeof (buf)))
256     return g_strdup (buf);
257   else
258     return g_strdup_printf ("GStreamer-pid-%lu", (gulong) getpid ());
259 }
260 
261 pa_channel_map *
gst_pulse_gst_to_channel_map(pa_channel_map * map,const GstAudioRingBufferSpec * spec)262 gst_pulse_gst_to_channel_map (pa_channel_map * map,
263     const GstAudioRingBufferSpec * spec)
264 {
265   gint i, j;
266   gint channels;
267   const GstAudioChannelPosition *pos;
268 
269   pa_channel_map_init (map);
270 
271   channels = GST_AUDIO_INFO_CHANNELS (&spec->info);
272   pos = spec->info.position;
273 
274   for (j = 0; j < channels; j++) {
275     for (i = 0; i < G_N_ELEMENTS (gst_pa_pos_table); i++) {
276       if (pos[j] == gst_pa_pos_table[i].gst_pos) {
277         map->map[j] = gst_pa_pos_table[i].pa_pos;
278         break;
279       }
280     }
281     if (i == G_N_ELEMENTS (gst_pa_pos_table))
282       return NULL;
283   }
284 
285   if (j != spec->info.channels) {
286     return NULL;
287   }
288 
289   map->channels = spec->info.channels;
290 
291   if (!pa_channel_map_valid (map)) {
292     return NULL;
293   }
294 
295   return map;
296 }
297 
298 GstAudioRingBufferSpec *
gst_pulse_channel_map_to_gst(const pa_channel_map * map,GstAudioRingBufferSpec * spec)299 gst_pulse_channel_map_to_gst (const pa_channel_map * map,
300     GstAudioRingBufferSpec * spec)
301 {
302   gint i, j;
303   gboolean invalid = FALSE;
304   gint channels;
305   GstAudioChannelPosition *pos;
306 
307   channels = GST_AUDIO_INFO_CHANNELS (&spec->info);
308 
309   g_return_val_if_fail (map->channels == channels, NULL);
310 
311   pos = spec->info.position;
312 
313   for (j = 0; j < channels; j++) {
314     for (i = 0; j < channels && i < G_N_ELEMENTS (gst_pa_pos_table); i++) {
315       if (map->map[j] == gst_pa_pos_table[i].pa_pos) {
316         pos[j] = gst_pa_pos_table[i].gst_pos;
317         break;
318       }
319     }
320     if (i == G_N_ELEMENTS (gst_pa_pos_table))
321       return NULL;
322   }
323 
324   if (!invalid
325       && !gst_audio_check_valid_channel_positions (pos, channels, FALSE))
326     invalid = TRUE;
327 
328   if (invalid) {
329     for (i = 0; i < channels; i++)
330       pos[i] = GST_AUDIO_CHANNEL_POSITION_NONE;
331   } else {
332     if (pos[0] != GST_AUDIO_CHANNEL_POSITION_NONE)
333       spec->info.flags &= ~GST_AUDIO_FLAG_UNPOSITIONED;
334   }
335 
336   return spec;
337 }
338 
339 void
gst_pulse_cvolume_from_linear(pa_cvolume * v,unsigned channels,gdouble volume)340 gst_pulse_cvolume_from_linear (pa_cvolume * v, unsigned channels,
341     gdouble volume)
342 {
343   pa_cvolume_set (v, channels, pa_sw_volume_from_linear (volume));
344 }
345 
346 static gboolean
make_proplist_item(GQuark field_id,const GValue * value,gpointer user_data)347 make_proplist_item (GQuark field_id, const GValue * value, gpointer user_data)
348 {
349   pa_proplist *p = (pa_proplist *) user_data;
350   gchar *prop_id = (gchar *) g_quark_to_string (field_id);
351 
352   /* http://0pointer.de/lennart/projects/pulseaudio/doxygen/proplist_8h.html */
353 
354   /* match prop id */
355 
356   /* check type */
357   switch (G_VALUE_TYPE (value)) {
358     case G_TYPE_STRING:
359       pa_proplist_sets (p, prop_id, g_value_get_string (value));
360       break;
361     default:
362       GST_WARNING ("unmapped property type %s", G_VALUE_TYPE_NAME (value));
363       break;
364   }
365 
366   return TRUE;
367 }
368 
369 pa_proplist *
gst_pulse_make_proplist(const GstStructure * properties)370 gst_pulse_make_proplist (const GstStructure * properties)
371 {
372   pa_proplist *proplist = pa_proplist_new ();
373 
374   /* iterate the structure and fill the proplist */
375   gst_structure_foreach (properties, make_proplist_item, proplist);
376   return proplist;
377 }
378 
379 GstStructure *
gst_pulse_make_structure(pa_proplist * properties)380 gst_pulse_make_structure (pa_proplist * properties)
381 {
382   GstStructure *str;
383   void *state = NULL;
384 
385   str = gst_structure_new_empty ("pulse-proplist");
386 
387   while (TRUE) {
388     const char *key, *val;
389 
390     key = pa_proplist_iterate (properties, &state);
391     if (key == NULL)
392       break;
393 
394     val = pa_proplist_gets (properties, key);
395 
396     gst_structure_set (str, key, G_TYPE_STRING, val, NULL);
397   }
398   return str;
399 }
400 
401 static gboolean
gst_pulse_format_info_int_prop_to_value(pa_format_info * format,const char * key,GValue * value)402 gst_pulse_format_info_int_prop_to_value (pa_format_info * format,
403     const char *key, GValue * value)
404 {
405   GValue v = { 0, };
406   int i;
407   int *a, n;
408   int min, max;
409 
410   if (pa_format_info_get_prop_int (format, key, &i) == 0) {
411     g_value_init (value, G_TYPE_INT);
412     g_value_set_int (value, i);
413 
414   } else if (pa_format_info_get_prop_int_array (format, key, &a, &n) == 0) {
415     g_value_init (value, GST_TYPE_LIST);
416     g_value_init (&v, G_TYPE_INT);
417 
418     for (i = 0; i < n; i++) {
419       g_value_set_int (&v, a[i]);
420       gst_value_list_append_value (value, &v);
421     }
422 
423     pa_xfree (a);
424 
425   } else if (pa_format_info_get_prop_int_range (format, key, &min, &max) == 0) {
426     g_value_init (value, GST_TYPE_INT_RANGE);
427     gst_value_set_int_range (value, min, max);
428 
429   } else {
430     /* Property not available or is not an int type */
431     return FALSE;
432   }
433 
434   return TRUE;
435 }
436 
437 GstCaps *
gst_pulse_format_info_to_caps(pa_format_info * format)438 gst_pulse_format_info_to_caps (pa_format_info * format)
439 {
440   GstCaps *ret = NULL;
441   GValue v = { 0, };
442   pa_sample_spec ss;
443 
444   switch (format->encoding) {
445     case PA_ENCODING_PCM:{
446       char *tmp = NULL;
447 
448       pa_format_info_to_sample_spec (format, &ss, NULL);
449 
450       if (pa_format_info_get_prop_string (format,
451               PA_PROP_FORMAT_SAMPLE_FORMAT, &tmp)) {
452         /* No specific sample format means any sample format */
453         ret = gst_pulse_fix_pcm_caps (gst_caps_from_string (_PULSE_CAPS_PCM));
454         goto out;
455 
456       } else if (ss.format == PA_SAMPLE_ALAW) {
457         ret = gst_caps_from_string (_PULSE_CAPS_ALAW);
458 
459       } else if (ss.format == PA_SAMPLE_ULAW) {
460         ret = gst_caps_from_string (_PULSE_CAPS_MULAW);
461 
462       } else {
463         /* Linear PCM format */
464         const char *sformat =
465             gst_pulse_sample_format_to_caps_format (ss.format);
466 
467         ret = gst_caps_from_string (_PULSE_CAPS_LINEAR);
468 
469         if (sformat)
470           gst_caps_set_simple (ret, "format", G_TYPE_STRING, sformat, NULL);
471       }
472 
473       pa_xfree (tmp);
474       break;
475     }
476 
477     case PA_ENCODING_AC3_IEC61937:
478       ret = gst_caps_from_string (_PULSE_CAPS_AC3);
479       break;
480 
481     case PA_ENCODING_EAC3_IEC61937:
482       ret = gst_caps_from_string (_PULSE_CAPS_EAC3);
483       break;
484 
485     case PA_ENCODING_DTS_IEC61937:
486       ret = gst_caps_from_string (_PULSE_CAPS_DTS);
487       break;
488 
489     case PA_ENCODING_MPEG_IEC61937:
490       ret = gst_caps_from_string (_PULSE_CAPS_MP3);
491       break;
492 
493     default:
494       GST_WARNING ("Found a PA format that we don't support yet");
495       goto out;
496   }
497 
498   if (gst_pulse_format_info_int_prop_to_value (format, PA_PROP_FORMAT_RATE, &v))
499     gst_caps_set_value (ret, "rate", &v);
500 
501   g_value_unset (&v);
502 
503   if (gst_pulse_format_info_int_prop_to_value (format, PA_PROP_FORMAT_CHANNELS,
504           &v))
505     gst_caps_set_value (ret, "channels", &v);
506 
507 out:
508   return ret;
509 }
510 
511 GstCaps *
gst_pulse_fix_pcm_caps(GstCaps * incaps)512 gst_pulse_fix_pcm_caps (GstCaps * incaps)
513 {
514   GstCaps *outcaps;
515   int i;
516 
517   outcaps = gst_caps_make_writable (incaps);
518 
519   for (i = 0; i < gst_caps_get_size (outcaps); i++) {
520     GstStructure *st = gst_caps_get_structure (outcaps, i);
521     const gchar *format = gst_structure_get_name (st);
522     const GValue *value;
523     GValue new_value = G_VALUE_INIT;
524     gint min, max, step;
525 
526     if (!(g_str_equal (format, "audio/x-raw") ||
527             g_str_equal (format, "audio/x-alaw") ||
528             g_str_equal (format, "audio/x-mulaw")))
529       continue;
530 
531     value = gst_structure_get_value (st, "rate");
532 
533     if (!GST_VALUE_HOLDS_INT_RANGE (value))
534       continue;
535 
536     min = gst_value_get_int_range_min (value);
537     max = gst_value_get_int_range_max (value);
538     step = gst_value_get_int_range_step (value);
539 
540     if (min > PA_RATE_MAX)
541       min = PA_RATE_MAX;
542     if (max > PA_RATE_MAX)
543       max = PA_RATE_MAX;
544 
545     g_value_init (&new_value, GST_TYPE_INT_RANGE);
546     gst_value_set_int_range_step (&new_value, min, max, step);
547 
548     gst_structure_take_value (st, "rate", &new_value);
549   }
550 
551   return outcaps;
552 }
553