tizen 2.0 init
[framework/multimedia/gst-plugins-good0.10.git] / ext / pulse / pulseutil.c
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  *  USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include "pulseutil.h"
27 #include <gst/audio/multichannel.h>
28
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>            /* getpid on UNIX */
31 #endif
32 #ifdef HAVE_PROCESS_H
33 # include <process.h>           /* getpid on win32 */
34 #endif
35
36 static const pa_channel_position_t gst_pos_to_pa[GST_AUDIO_CHANNEL_POSITION_NUM]
37     = {
38   [GST_AUDIO_CHANNEL_POSITION_FRONT_MONO] = PA_CHANNEL_POSITION_MONO,
39   [GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT] = PA_CHANNEL_POSITION_FRONT_LEFT,
40   [GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT] = PA_CHANNEL_POSITION_FRONT_RIGHT,
41   [GST_AUDIO_CHANNEL_POSITION_REAR_CENTER] = PA_CHANNEL_POSITION_REAR_CENTER,
42   [GST_AUDIO_CHANNEL_POSITION_REAR_LEFT] = PA_CHANNEL_POSITION_REAR_LEFT,
43   [GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT] = PA_CHANNEL_POSITION_REAR_RIGHT,
44   [GST_AUDIO_CHANNEL_POSITION_LFE] = PA_CHANNEL_POSITION_LFE,
45   [GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER] = PA_CHANNEL_POSITION_FRONT_CENTER,
46   [GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER] =
47       PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER,
48   [GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER] =
49       PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER,
50   [GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT] = PA_CHANNEL_POSITION_SIDE_LEFT,
51   [GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT] = PA_CHANNEL_POSITION_SIDE_RIGHT,
52   [GST_AUDIO_CHANNEL_POSITION_NONE] = PA_CHANNEL_POSITION_INVALID
53 };
54
55 /* All index are increased by one because PA_CHANNEL_POSITION_INVALID == -1 */
56 static const GstAudioChannelPosition
57     pa_to_gst_pos[GST_AUDIO_CHANNEL_POSITION_NUM]
58     = {
59   [PA_CHANNEL_POSITION_MONO + 1] = GST_AUDIO_CHANNEL_POSITION_FRONT_MONO,
60   [PA_CHANNEL_POSITION_FRONT_LEFT + 1] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
61   [PA_CHANNEL_POSITION_FRONT_RIGHT + 1] =
62       GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
63   [PA_CHANNEL_POSITION_REAR_CENTER + 1] =
64       GST_AUDIO_CHANNEL_POSITION_REAR_CENTER,
65   [PA_CHANNEL_POSITION_REAR_LEFT + 1] = GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
66   [PA_CHANNEL_POSITION_REAR_RIGHT + 1] = GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
67   [PA_CHANNEL_POSITION_LFE + 1] = GST_AUDIO_CHANNEL_POSITION_LFE,
68   [PA_CHANNEL_POSITION_FRONT_CENTER + 1] =
69       GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
70   [PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER + 1] =
71       GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER,
72   [PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER + 1] =
73       GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER,
74   [PA_CHANNEL_POSITION_SIDE_LEFT + 1] = GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
75   [PA_CHANNEL_POSITION_SIDE_RIGHT + 1] = GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT,
76   [PA_CHANNEL_POSITION_INVALID + 1] = GST_AUDIO_CHANNEL_POSITION_NONE,
77 };
78
79 gboolean
80 gst_pulse_fill_sample_spec (GstRingBufferSpec * spec, pa_sample_spec * ss)
81 {
82
83   if (spec->format == GST_MU_LAW && spec->width == 8)
84     ss->format = PA_SAMPLE_ULAW;
85   else if (spec->format == GST_A_LAW && spec->width == 8)
86     ss->format = PA_SAMPLE_ALAW;
87   else if (spec->format == GST_U8 && spec->width == 8)
88     ss->format = PA_SAMPLE_U8;
89   else if (spec->format == GST_S16_LE && spec->width == 16)
90     ss->format = PA_SAMPLE_S16LE;
91   else if (spec->format == GST_S16_BE && spec->width == 16)
92     ss->format = PA_SAMPLE_S16BE;
93   else if (spec->format == GST_FLOAT32_LE && spec->width == 32)
94     ss->format = PA_SAMPLE_FLOAT32LE;
95   else if (spec->format == GST_FLOAT32_BE && spec->width == 32)
96     ss->format = PA_SAMPLE_FLOAT32BE;
97   else if (spec->format == GST_S32_LE && spec->width == 32)
98     ss->format = PA_SAMPLE_S32LE;
99   else if (spec->format == GST_S32_BE && spec->width == 32)
100     ss->format = PA_SAMPLE_S32BE;
101   else if (spec->format == GST_S24_3LE && spec->width == 24)
102     ss->format = PA_SAMPLE_S24LE;
103   else if (spec->format == GST_S24_3BE && spec->width == 24)
104     ss->format = PA_SAMPLE_S24BE;
105   else if (spec->format == GST_S24_LE && spec->width == 32)
106     ss->format = PA_SAMPLE_S24_32LE;
107   else if (spec->format == GST_S24_BE && spec->width == 32)
108     ss->format = PA_SAMPLE_S24_32BE;
109   else
110     return FALSE;
111
112   ss->channels = spec->channels;
113   ss->rate = spec->rate;
114
115   if (!pa_sample_spec_valid (ss))
116     return FALSE;
117
118   return TRUE;
119 }
120
121 #ifdef HAVE_PULSE_1_0
122 gboolean
123 gst_pulse_fill_format_info (GstRingBufferSpec * spec, pa_format_info ** f,
124     guint * channels)
125 {
126   pa_format_info *format;
127   pa_sample_format_t sf = PA_SAMPLE_INVALID;
128
129   format = pa_format_info_new ();
130
131   if (spec->format == GST_MU_LAW && spec->width == 8) {
132     format->encoding = PA_ENCODING_PCM;
133     sf = PA_SAMPLE_ULAW;
134   } else if (spec->format == GST_A_LAW && spec->width == 8) {
135     format->encoding = PA_ENCODING_PCM;
136     sf = PA_SAMPLE_ALAW;
137   } else if (spec->format == GST_U8 && spec->width == 8) {
138     format->encoding = PA_ENCODING_PCM;
139     sf = PA_SAMPLE_U8;
140   } else if (spec->format == GST_S16_LE && spec->width == 16) {
141     format->encoding = PA_ENCODING_PCM;
142     sf = PA_SAMPLE_S16LE;
143   } else if (spec->format == GST_S16_BE && spec->width == 16) {
144     format->encoding = PA_ENCODING_PCM;
145     sf = PA_SAMPLE_S16BE;
146   } else if (spec->format == GST_FLOAT32_LE && spec->width == 32) {
147     format->encoding = PA_ENCODING_PCM;
148     sf = PA_SAMPLE_FLOAT32LE;
149   } else if (spec->format == GST_FLOAT32_BE && spec->width == 32) {
150     format->encoding = PA_ENCODING_PCM;
151     sf = PA_SAMPLE_FLOAT32BE;
152   } else if (spec->format == GST_S32_LE && spec->width == 32) {
153     format->encoding = PA_ENCODING_PCM;
154     sf = PA_SAMPLE_S32LE;
155   } else if (spec->format == GST_S32_BE && spec->width == 32) {
156     format->encoding = PA_ENCODING_PCM;
157     sf = PA_SAMPLE_S32BE;
158   } else if (spec->format == GST_S24_3LE && spec->width == 24) {
159     format->encoding = PA_ENCODING_PCM;
160     sf = PA_SAMPLE_S24LE;
161   } else if (spec->format == GST_S24_3BE && spec->width == 24) {
162     format->encoding = PA_ENCODING_PCM;
163     sf = PA_SAMPLE_S24BE;
164   } else if (spec->format == GST_S24_LE && spec->width == 32) {
165     format->encoding = PA_ENCODING_PCM;
166     sf = PA_SAMPLE_S24_32LE;
167   } else if (spec->format == GST_S24_BE && spec->width == 32) {
168     format->encoding = PA_ENCODING_PCM;
169     sf = PA_SAMPLE_S24_32BE;
170   } else if (spec->format == GST_AC3) {
171     format->encoding = PA_ENCODING_AC3_IEC61937;
172   } else if (spec->format == GST_EAC3) {
173     format->encoding = PA_ENCODING_EAC3_IEC61937;
174   } else if (spec->format == GST_DTS) {
175     format->encoding = PA_ENCODING_DTS_IEC61937;
176   } else if (spec->format == GST_MPEG) {
177     format->encoding = PA_ENCODING_MPEG_IEC61937;
178   } else {
179     goto fail;
180   }
181
182   if (format->encoding == PA_ENCODING_PCM) {
183     pa_format_info_set_sample_format (format, sf);
184     pa_format_info_set_channels (format, spec->channels);
185   }
186
187   pa_format_info_set_rate (format, spec->rate);
188
189   if (!pa_format_info_valid (format))
190     goto fail;
191
192   *f = format;
193   *channels = spec->channels;
194
195   return TRUE;
196
197 fail:
198   if (format)
199     pa_format_info_free (format);
200   return FALSE;
201 }
202 #endif
203
204 /* PATH_MAX is not defined everywhere, e.g. on GNU Hurd */
205 #ifndef PATH_MAX
206 #define PATH_MAX 4096
207 #endif
208
209 gchar *
210 gst_pulse_client_name (void)
211 {
212   gchar buf[PATH_MAX];
213
214   const char *c;
215
216   if ((c = g_get_application_name ()))
217     return g_strdup (c);
218   else if (pa_get_binary_name (buf, sizeof (buf)))
219     return g_strdup (buf);
220   else
221     return g_strdup_printf ("GStreamer-pid-%lu", (gulong) getpid ());
222 }
223
224 pa_channel_map *
225 gst_pulse_gst_to_channel_map (pa_channel_map * map,
226     const GstRingBufferSpec * spec)
227 {
228   int i;
229   GstAudioChannelPosition *pos;
230
231   pa_channel_map_init (map);
232
233   if (!(pos =
234           gst_audio_get_channel_positions (gst_caps_get_structure (spec->caps,
235                   0)))) {
236     return NULL;
237   }
238
239   for (i = 0; i < spec->channels; i++) {
240     if (pos[i] == GST_AUDIO_CHANNEL_POSITION_NONE) {
241       /* no valid mappings for these channels */
242       g_free (pos);
243       return NULL;
244     } else if (pos[i] < GST_AUDIO_CHANNEL_POSITION_NUM)
245       map->map[i] = gst_pos_to_pa[pos[i]];
246     else
247       map->map[i] = PA_CHANNEL_POSITION_INVALID;
248   }
249
250   g_free (pos);
251   map->channels = spec->channels;
252
253   if (!pa_channel_map_valid (map)) {
254     return NULL;
255   }
256
257   return map;
258 }
259
260 GstRingBufferSpec *
261 gst_pulse_channel_map_to_gst (const pa_channel_map * map,
262     GstRingBufferSpec * spec)
263 {
264   int i;
265   GstAudioChannelPosition *pos;
266   gboolean invalid = FALSE;
267
268   g_return_val_if_fail (map->channels == spec->channels, NULL);
269
270   pos = g_new0 (GstAudioChannelPosition, spec->channels + 1);
271
272   for (i = 0; i < spec->channels; i++) {
273     if (map->map[i] == PA_CHANNEL_POSITION_INVALID) {
274       invalid = TRUE;
275       break;
276     } else if ((int) map->map[i] < (int) GST_AUDIO_CHANNEL_POSITION_NUM) {
277       pos[i] = pa_to_gst_pos[map->map[i] + 1];
278     } else {
279       invalid = TRUE;
280       break;
281     }
282   }
283
284   if (!invalid && !gst_audio_check_channel_positions (pos, spec->channels))
285     invalid = TRUE;
286
287   if (invalid) {
288     for (i = 0; i < spec->channels; i++)
289       pos[i] = GST_AUDIO_CHANNEL_POSITION_NONE;
290   }
291
292   gst_audio_set_channel_positions (gst_caps_get_structure (spec->caps, 0), pos);
293
294   g_free (pos);
295
296   return spec;
297 }
298
299 void
300 gst_pulse_cvolume_from_linear (pa_cvolume * v, unsigned channels,
301     gdouble volume)
302 {
303   pa_cvolume_set (v, channels, pa_sw_volume_from_linear (volume));
304 }
305
306 static gboolean
307 make_proplist_item (GQuark field_id, const GValue * value, gpointer user_data)
308 {
309   pa_proplist *p = (pa_proplist *) user_data;
310   gchar *prop_id = (gchar *) g_quark_to_string (field_id);
311
312   /* http://0pointer.de/lennart/projects/pulseaudio/doxygen/proplist_8h.html */
313
314   /* match prop id */
315
316   /* check type */
317   switch (G_VALUE_TYPE (value)) {
318     case G_TYPE_STRING:
319       pa_proplist_sets (p, prop_id, g_value_get_string (value));
320       break;
321     default:
322       GST_WARNING ("unmapped property type %s", G_VALUE_TYPE_NAME (value));
323       break;
324   }
325
326   return TRUE;
327 }
328
329 pa_proplist *
330 gst_pulse_make_proplist (const GstStructure * properties)
331 {
332   pa_proplist *proplist = pa_proplist_new ();
333
334   /* iterate the structure and fill the proplist */
335   gst_structure_foreach (properties, make_proplist_item, proplist);
336   return proplist;
337 }