2 * GStreamer pulseaudio plugin
4 * Copyright (c) 2004-2008 Lennart Poettering
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.
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.
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
26 #include "pulseutil.h"
27 #include <gst/audio/multichannel.h>
30 # include <unistd.h> /* getpid on UNIX */
33 # include <process.h> /* getpid on win32 */
36 static const pa_channel_position_t gst_pos_to_pa[GST_AUDIO_CHANNEL_POSITION_NUM]
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
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]
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,
80 gst_pulse_fill_sample_spec (GstRingBufferSpec * spec, pa_sample_spec * ss)
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;
112 ss->channels = spec->channels;
113 ss->rate = spec->rate;
115 if (!pa_sample_spec_valid (ss))
121 #ifdef HAVE_PULSE_1_0
123 gst_pulse_fill_format_info (GstRingBufferSpec * spec, pa_format_info ** f,
126 pa_format_info *format;
127 pa_sample_format_t sf = PA_SAMPLE_INVALID;
129 format = pa_format_info_new ();
131 if (spec->format == GST_MU_LAW && spec->width == 8) {
132 format->encoding = PA_ENCODING_PCM;
134 } else if (spec->format == GST_A_LAW && spec->width == 8) {
135 format->encoding = PA_ENCODING_PCM;
137 } else if (spec->format == GST_U8 && spec->width == 8) {
138 format->encoding = PA_ENCODING_PCM;
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;
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);
187 pa_format_info_set_rate (format, spec->rate);
189 if (!pa_format_info_valid (format))
193 *channels = spec->channels;
199 pa_format_info_free (format);
204 /* PATH_MAX is not defined everywhere, e.g. on GNU Hurd */
206 #define PATH_MAX 4096
210 gst_pulse_client_name (void)
216 if ((c = g_get_application_name ()))
218 else if (pa_get_binary_name (buf, sizeof (buf)))
219 return g_strdup (buf);
221 return g_strdup_printf ("GStreamer-pid-%lu", (gulong) getpid ());
225 gst_pulse_gst_to_channel_map (pa_channel_map * map,
226 const GstRingBufferSpec * spec)
229 GstAudioChannelPosition *pos;
231 pa_channel_map_init (map);
234 gst_audio_get_channel_positions (gst_caps_get_structure (spec->caps,
239 for (i = 0; i < spec->channels; i++) {
240 if (pos[i] == GST_AUDIO_CHANNEL_POSITION_NONE) {
241 /* no valid mappings for these channels */
244 } else if (pos[i] < GST_AUDIO_CHANNEL_POSITION_NUM)
245 map->map[i] = gst_pos_to_pa[pos[i]];
247 map->map[i] = PA_CHANNEL_POSITION_INVALID;
251 map->channels = spec->channels;
253 if (!pa_channel_map_valid (map)) {
261 gst_pulse_channel_map_to_gst (const pa_channel_map * map,
262 GstRingBufferSpec * spec)
265 GstAudioChannelPosition *pos;
266 gboolean invalid = FALSE;
268 g_return_val_if_fail (map->channels == spec->channels, NULL);
270 pos = g_new0 (GstAudioChannelPosition, spec->channels + 1);
272 for (i = 0; i < spec->channels; i++) {
273 if (map->map[i] == PA_CHANNEL_POSITION_INVALID) {
276 } else if ((int) map->map[i] < (int) GST_AUDIO_CHANNEL_POSITION_NUM) {
277 pos[i] = pa_to_gst_pos[map->map[i] + 1];
284 if (!invalid && !gst_audio_check_channel_positions (pos, spec->channels))
288 for (i = 0; i < spec->channels; i++)
289 pos[i] = GST_AUDIO_CHANNEL_POSITION_NONE;
292 gst_audio_set_channel_positions (gst_caps_get_structure (spec->caps, 0), pos);
300 gst_pulse_cvolume_from_linear (pa_cvolume * v, unsigned channels,
303 pa_cvolume_set (v, channels, pa_sw_volume_from_linear (volume));
307 make_proplist_item (GQuark field_id, const GValue * value, gpointer user_data)
309 pa_proplist *p = (pa_proplist *) user_data;
310 gchar *prop_id = (gchar *) g_quark_to_string (field_id);
312 /* http://0pointer.de/lennart/projects/pulseaudio/doxygen/proplist_8h.html */
317 switch (G_VALUE_TYPE (value)) {
319 pa_proplist_sets (p, prop_id, g_value_get_string (value));
322 GST_WARNING ("unmapped property type %s", G_VALUE_TYPE_NAME (value));
330 gst_pulse_make_proplist (const GstStructure * properties)
332 pa_proplist *proplist = pa_proplist_new ();
334 /* iterate the structure and fill the proplist */
335 gst_structure_foreach (properties, make_proplist_item, proplist);