pulseutil.c 7.95 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 *  GStreamer pulseaudio plugin
 *
 *  Copyright (c) 2004-2008 Lennart Poettering
 *
 *  gst-pulse is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as
 *  published by the Free Software Foundation; either version 2.1 of the
 *  License, or (at your option) any later version.
 *
 *  gst-pulse is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with gst-pulse; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 *  USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "pulseutil.h"
#include <gst/audio/multichannel.h>

29 30 31 32 33 34 35
#ifdef HAVE_UNISTD_H
# include <unistd.h>            /* getpid on UNIX */
#endif
#ifdef HAVE_PROCESS_H
# include <process.h>           /* getpid on win32 */
#endif

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
static const pa_channel_position_t gst_pos_to_pa[GST_AUDIO_CHANNEL_POSITION_NUM]
    = {
  [GST_AUDIO_CHANNEL_POSITION_FRONT_MONO] = PA_CHANNEL_POSITION_MONO,
  [GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT] = PA_CHANNEL_POSITION_FRONT_LEFT,
  [GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT] = PA_CHANNEL_POSITION_FRONT_RIGHT,
  [GST_AUDIO_CHANNEL_POSITION_REAR_CENTER] = PA_CHANNEL_POSITION_REAR_CENTER,
  [GST_AUDIO_CHANNEL_POSITION_REAR_LEFT] = PA_CHANNEL_POSITION_REAR_LEFT,
  [GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT] = PA_CHANNEL_POSITION_REAR_RIGHT,
  [GST_AUDIO_CHANNEL_POSITION_LFE] = PA_CHANNEL_POSITION_LFE,
  [GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER] = PA_CHANNEL_POSITION_FRONT_CENTER,
  [GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER] =
      PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER,
  [GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER] =
      PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER,
  [GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT] = PA_CHANNEL_POSITION_SIDE_LEFT,
  [GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT] = PA_CHANNEL_POSITION_SIDE_RIGHT,
  [GST_AUDIO_CHANNEL_POSITION_NONE] = PA_CHANNEL_POSITION_INVALID
};

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
/* All index are increased by one because PA_CHANNEL_POSITION_INVALID == -1 */
static const GstAudioChannelPosition
    pa_to_gst_pos[GST_AUDIO_CHANNEL_POSITION_NUM]
    = {
  [PA_CHANNEL_POSITION_MONO + 1] = GST_AUDIO_CHANNEL_POSITION_FRONT_MONO,
  [PA_CHANNEL_POSITION_FRONT_LEFT + 1] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
  [PA_CHANNEL_POSITION_FRONT_RIGHT + 1] =
      GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
  [PA_CHANNEL_POSITION_REAR_CENTER + 1] =
      GST_AUDIO_CHANNEL_POSITION_REAR_CENTER,
  [PA_CHANNEL_POSITION_REAR_LEFT + 1] = GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
  [PA_CHANNEL_POSITION_REAR_RIGHT + 1] = GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
  [PA_CHANNEL_POSITION_LFE + 1] = GST_AUDIO_CHANNEL_POSITION_LFE,
  [PA_CHANNEL_POSITION_FRONT_CENTER + 1] =
      GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
  [PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER + 1] =
      GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER,
  [PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER + 1] =
      GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER,
  [PA_CHANNEL_POSITION_SIDE_LEFT + 1] = GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
  [PA_CHANNEL_POSITION_SIDE_RIGHT + 1] = GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT,
  [PA_CHANNEL_POSITION_INVALID + 1] = GST_AUDIO_CHANNEL_POSITION_NONE,
};

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
gboolean
gst_pulse_fill_sample_spec (GstRingBufferSpec * spec, pa_sample_spec * ss)
{

  if (spec->format == GST_MU_LAW && spec->width == 8)
    ss->format = PA_SAMPLE_ULAW;
  else if (spec->format == GST_A_LAW && spec->width == 8)
    ss->format = PA_SAMPLE_ALAW;
  else if (spec->format == GST_U8 && spec->width == 8)
    ss->format = PA_SAMPLE_U8;
  else if (spec->format == GST_S16_LE && spec->width == 16)
    ss->format = PA_SAMPLE_S16LE;
  else if (spec->format == GST_S16_BE && spec->width == 16)
    ss->format = PA_SAMPLE_S16BE;
  else if (spec->format == GST_FLOAT32_LE && spec->width == 32)
    ss->format = PA_SAMPLE_FLOAT32LE;
  else if (spec->format == GST_FLOAT32_BE && spec->width == 32)
    ss->format = PA_SAMPLE_FLOAT32BE;
  else if (spec->format == GST_S32_LE && spec->width == 32)
    ss->format = PA_SAMPLE_S32LE;
  else if (spec->format == GST_S32_BE && spec->width == 32)
    ss->format = PA_SAMPLE_S32BE;
101
#ifdef HAVE_PULSE_0_9_15
102 103 104 105 106 107 108 109 110
  else if (spec->format == GST_S24_3LE && spec->width == 24)
    ss->format = PA_SAMPLE_S24LE;
  else if (spec->format == GST_S24_3BE && spec->width == 24)
    ss->format = PA_SAMPLE_S24BE;
  else if (spec->format == GST_S24_LE && spec->width == 32)
    ss->format = PA_SAMPLE_S24_32LE;
  else if (spec->format == GST_S24_BE && spec->width == 32)
    ss->format = PA_SAMPLE_S24_32BE;
#endif
111 112 113 114 115 116 117 118 119 120 121 122
  else
    return FALSE;

  ss->channels = spec->channels;
  ss->rate = spec->rate;

  if (!pa_sample_spec_valid (ss))
    return FALSE;

  return TRUE;
}

123 124 125 126 127
/* PATH_MAX is not defined everywhere, e.g. on GNU Hurd */
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif

128 129 130 131 132 133 134 135
gchar *
gst_pulse_client_name (void)
{
  gchar buf[PATH_MAX];

  const char *c;

  if ((c = g_get_application_name ()))
136
    return g_strdup (c);
137
  else if (pa_get_binary_name (buf, sizeof (buf)))
138
    return g_strdup (buf);
139
  else
140
    return g_strdup_printf ("GStreamer-pid-%lu", (gulong) getpid ());
141 142 143
}

pa_channel_map *
144 145
gst_pulse_gst_to_channel_map (pa_channel_map * map,
    const GstRingBufferSpec * spec)
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
{
  int i;
  GstAudioChannelPosition *pos;

  pa_channel_map_init (map);

  if (!(pos =
          gst_audio_get_channel_positions (gst_caps_get_structure (spec->caps,
                  0)))) {
    return NULL;
  }

  for (i = 0; i < spec->channels; i++) {
    if (pos[i] == GST_AUDIO_CHANNEL_POSITION_NONE) {
      /* no valid mappings for these channels */
      g_free (pos);
      return NULL;
    } else if (pos[i] < GST_AUDIO_CHANNEL_POSITION_NUM)
      map->map[i] = gst_pos_to_pa[pos[i]];
    else
      map->map[i] = PA_CHANNEL_POSITION_INVALID;
  }

  g_free (pos);
  map->channels = spec->channels;

  if (!pa_channel_map_valid (map)) {
    return NULL;
  }

  return map;
}
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

GstRingBufferSpec *
gst_pulse_channel_map_to_gst (const pa_channel_map * map,
    GstRingBufferSpec * spec)
{
  int i;
  GstAudioChannelPosition *pos;
  gboolean invalid = FALSE;

  g_return_val_if_fail (map->channels == spec->channels, NULL);

  pos = g_new0 (GstAudioChannelPosition, spec->channels + 1);

  for (i = 0; i < spec->channels; i++) {
    if (map->map[i] == PA_CHANNEL_POSITION_INVALID) {
      invalid = TRUE;
      break;
195
    } else if ((int) map->map[i] < (int) GST_AUDIO_CHANNEL_POSITION_NUM) {
196 197 198 199 200 201 202
      pos[i] = pa_to_gst_pos[map->map[i] + 1];
    } else {
      invalid = TRUE;
      break;
    }
  }

203 204 205
  if (!invalid && !gst_audio_check_channel_positions (pos, spec->channels))
    invalid = TRUE;

206 207 208 209 210 211 212 213 214 215 216
  if (invalid) {
    for (i = 0; i < spec->channels; i++)
      pos[i] = GST_AUDIO_CHANNEL_POSITION_NONE;
  }

  gst_audio_set_channel_positions (gst_caps_get_structure (spec->caps, 0), pos);

  g_free (pos);

  return spec;
}
217 218 219 220 221 222 223

void
gst_pulse_cvolume_from_linear (pa_cvolume * v, unsigned channels,
    gdouble volume)
{
  pa_cvolume_set (v, channels, pa_sw_volume_from_linear (volume));
}
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

static gboolean
make_proplist_item (GQuark field_id, const GValue * value, gpointer user_data)
{
  pa_proplist *p = (pa_proplist *) user_data;
  gchar *prop_id = (gchar *) g_quark_to_string (field_id);

  /* http://0pointer.de/lennart/projects/pulseaudio/doxygen/proplist_8h.html */

  /* match prop id */

  /* check type */
  switch (G_VALUE_TYPE (value)) {
    case G_TYPE_STRING:
      pa_proplist_sets (p, prop_id, g_value_get_string (value));
      break;
    default:
      GST_WARNING ("unmapped property type %s", G_VALUE_TYPE_NAME (value));
      break;
  }

  return TRUE;
}

pa_proplist *
gst_pulse_make_proplist (const GstStructure * properties)
{
  pa_proplist *proplist = pa_proplist_new ();

  /* iterate the structure and fill the proplist */
  gst_structure_foreach (properties, make_proplist_item, proplist);
  return proplist;
}