summaryrefslogtreecommitdiffstats
path: root/sys/oss/gstossmixer.c
blob: b54268fbe067a675304bb81332c6f7d5ebbe6ea1 (plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/* GStreamer OSS Mixer implementation
 * Copyright (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
 *
 * gstossmixer.c: mixer interface implementation for OSS
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; 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 <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>

#ifdef HAVE_OSS_INCLUDE_IN_SYS
#include <sys/soundcard.h>
#else

#ifdef HAVE_OSS_INCLUDE_IN_ROOT
#include <soundcard.h>
#else

#include <machine/soundcard.h>

#endif /* HAVE_OSS_INCLUDE_IN_ROOT */

#endif /* HAVE_OSS_INCLUDE_IN_SYS */

#include <gst/gst-i18n-plugin.h>

#include "gstossmixer.h"

#define MASK_BIT_IS_SET(mask, bit) \
  (mask & (1 << bit))

static void gst_ossmixer_track_class_init (GstOssMixerTrackClass * klass);
static void gst_ossmixer_track_init (GstOssMixerTrack * track);

static gboolean gst_ossmixer_supported (GstImplementsInterface * iface,
    GType iface_type);
static const GList *gst_ossmixer_list_tracks (GstMixer * ossmixer);

static void gst_ossmixer_set_volume (GstMixer * ossmixer,
    GstMixerTrack * track, gint * volumes);
static void gst_ossmixer_get_volume (GstMixer * ossmixer,
    GstMixerTrack * track, gint * volumes);

static void gst_ossmixer_set_record (GstMixer * ossmixer,
    GstMixerTrack * track, gboolean record);
static void gst_ossmixer_set_mute (GstMixer * ossmixer,
    GstMixerTrack * track, gboolean mute);

static const gchar **labels = NULL;
static GstMixerTrackClass *parent_class = NULL;

/* three functions: firstly, OSS has the nasty habit of inserting
 * spaces in the labels, we want to get rid of them. Secondly,
 * i18n is impossible with OSS' way of providing us with mixer
 * labels, so we make a 'given' list of i18n'ed labels. Thirdly, I
 * personally don't like the "1337" names that OSS gives to their
 * labels ("Vol", "Mic", "Rec"), I'd rather see full names. */

static void
fill_labels (void)
{
  gint i, pos;
  gchar *origs[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_LABELS;
  struct
  {
    gchar *given, *wanted;
  }
  cases[] =
  {
    /* Note: this list is simply ripped from soundcard.h. For
     * some people, some values might be missing (3D surround,
     * etc.) - feel free to add them. That's the reason why
     * I'm doing this in such a horribly complicated way. */
    {
    "Vol  ", _("Volume")}
    , {
    "Bass ", _("Bass")}
    , {
    "Trebl", _("Treble")}
    , {
    "Synth", _("Synth")}
    , {
    "Pcm  ", _("PCM")}
    , {
    "Spkr ", _("Speaker")}
    , {
    "Line ", _("Line-in")}
    , {
    "Mic  ", _("Microphone")}
    , {
    "CD   ", _("CD")}
    , {
    "Mix  ", _("Mixer")}
    , {
    "Pcm2 ", _("PCM-2")}
    , {
    "Rec  ", _("Record")}
    , {
    "IGain", _("In-gain")}
    , {
    "OGain", _("Out-gain")}
    , {
    "Line1", _("Line-1")}
    , {
    "Line2", _("Line-2")}
    , {
    "Line3", _("Line-3")}
    , {
    "Digital1", _("Digital-1")}
    , {
    "Digital2", _("Digital-2")}
    , {
    "Digital3", _("Digital-3")}
    , {
    "PhoneIn", _("Phone-in")}
    , {
    "PhoneOut", _("Phone-out")}
    , {
    "Video", _("Video")}
    , {
    "Radio", _("Radio")}
    , {
    "Monitor", _("Monitor")}
    , {
    NULL, NULL}
  };

  labels = g_malloc (sizeof (gchar *) * SOUND_MIXER_NRDEVICES);

  for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
    for (pos = 0; cases[pos].given != NULL; pos++) {
      if (!strcmp (cases[pos].given, origs[i])) {
        labels[i] = g_strdup (cases[pos].wanted);
        break;
      }
    }
    if (cases[pos].given == NULL)
      labels[i] = g_strdup (origs[i]);
  }
}

GType
gst_ossmixer_track_get_type (void)
{
  static GType gst_ossmixer_track_type = 0;

  if (!gst_ossmixer_track_type) {
    static const GTypeInfo ossmixer_track_info = {
      sizeof (GstOssMixerTrackClass),
      NULL,
      NULL,
      (GClassInitFunc) gst_ossmixer_track_class_init,
      NULL,
      NULL,
      sizeof (GstOssMixerTrack),
      0,
      (GInstanceInitFunc) gst_ossmixer_track_init,
      NULL
    };

    gst_ossmixer_track_type =
        g_type_register_static (GST_TYPE_MIXER_TRACK,
        "GstOssMixerTrack", &ossmixer_track_info, 0);
  }

  return gst_ossmixer_track_type;
}

static void
gst_ossmixer_track_class_init (GstOssMixerTrackClass * klass)
{
  parent_class = g_type_class_ref (GST_TYPE_MIXER_TRACK);
}

static void
gst_ossmixer_track_init (GstOssMixerTrack * track)
{
  track->lvol = track->rvol = 0;
  track->track_num = 0;
}

GstMixerTrack *
gst_ossmixer_track_new (GstOssElement * oss,
    gint track_num, gint max_chans, gint flags)
{
  GstOssMixerTrack *osstrack;
  GstMixerTrack *track;
  gint volume;

  if (!labels)
    fill_labels ();

  osstrack = g_object_new (GST_TYPE_OSSMIXER_TRACK, NULL);
  track = GST_MIXER_TRACK (osstrack);
  track->label = g_strdup (labels[track_num]);
  track->num_channels = max_chans;
  track->flags = flags;
  track->min_volume = 0;
  track->max_volume = 100;
  osstrack->track_num = track_num;

  /* volume */
  if (ioctl (oss->mixer_fd, MIXER_READ (osstrack->track_num), &volume) < 0) {
    g_warning ("Error getting device (%d) volume: %s",
        osstrack->track_num, strerror (errno));
    volume = 0;
  }
  osstrack->lvol = (volume & 0xff);
  if (track->num_channels == 2) {
    osstrack->rvol = ((volume >> 8) & 0xff);
  }

  return track;
}

void
gst_oss_interface_init (GstImplementsInterfaceClass * klass)
{
  /* default virtual functions */
  klass->supported = gst_ossmixer_supported;
}

void
gst_ossmixer_interface_init (GstMixerClass * klass)
{
  GST_MIXER_TYPE (klass) = GST_MIXER_HARDWARE;

  /* default virtual functions */
  klass->list_tracks = gst_ossmixer_list_tracks;
  klass->set_volume = gst_ossmixer_set_volume;
  klass->get_volume = gst_ossmixer_get_volume;
  klass->set_mute = gst_ossmixer_set_mute;
  klass->set_record = gst_ossmixer_set_record;
}

static gboolean
gst_ossmixer_supported (GstImplementsInterface * iface, GType iface_type)
{
  g_assert (iface_type == GST_TYPE_MIXER);

  return (GST_OSSELEMENT (iface)->mixer_fd != -1);
}

/* unused with G_DISABLE_* */
static G_GNUC_UNUSED gboolean
gst_ossmixer_contains_track (GstOssElement * oss, GstOssMixerTrack * osstrack)
{
  const GList *item;

  for (item = oss->tracklist; item != NULL; item = item->next)
    if (item->data == osstrack)
      return TRUE;

  return FALSE;
}

static const GList *
gst_ossmixer_list_tracks (GstMixer * mixer)
{
  return (const GList *) GST_OSSELEMENT (mixer)->tracklist;
}

static void
gst_ossmixer_get_volume (GstMixer * mixer,
    GstMixerTrack * track, gint * volumes)
{
  gint volume;
  GstOssElement *oss = GST_OSSELEMENT (mixer);
  GstOssMixerTrack *osstrack = GST_OSSMIXER_TRACK (track);

  /* assert that we're opened and that we're using a known item */
  g_return_if_fail (oss->mixer_fd != -1);
  g_return_if_fail (gst_ossmixer_contains_track (oss, osstrack));

  if (track->flags & GST_MIXER_TRACK_MUTE) {
    volumes[0] = osstrack->lvol;
    if (track->num_channels == 2) {
      volumes[1] = osstrack->rvol;
    }
  } else {
    /* get */
    if (ioctl (oss->mixer_fd, MIXER_READ (osstrack->track_num), &volume) < 0) {
      g_warning ("Error getting recording device (%d) volume: %s",
          osstrack->track_num, strerror (errno));
      volume = 0;
    }

    osstrack->lvol = volumes[0] = (volume & 0xff);
    if (track->num_channels == 2) {
      osstrack->rvol = volumes[1] = ((volume >> 8) & 0xff);
    }
  }
}

static void
gst_ossmixer_set_volume (GstMixer * mixer,
    GstMixerTrack * track, gint * volumes)
{
  gint volume;
  GstOssElement *oss = GST_OSSELEMENT (mixer);
  GstOssMixerTrack *osstrack = GST_OSSMIXER_TRACK (track);

  /* assert that we're opened and that we're using a known item */
  g_return_if_fail (oss->mixer_fd != -1);
  g_return_if_fail (gst_ossmixer_contains_track (oss, osstrack));

  /* prepare the value for ioctl() */
  if (!(track->flags & GST_MIXER_TRACK_MUTE)) {
    volume = (volumes[0] & 0xff);
    if (track->num_channels == 2) {
      volume |= ((volumes[1] & 0xff) << 8);
    }

    /* set */
    if (ioctl (oss->mixer_fd, MIXER_WRITE (osstrack->track_num), &volume) < 0) {
      g_warning ("Error setting recording device (%d) volume (0x%x): %s",
          osstrack->track_num, volume, strerror (errno));
      return;
    }
  }

  osstrack->lvol = volumes[0];
  if (track->num_channels == 2) {
    osstrack->rvol = volumes[1];
  }
}

static void
gst_ossmixer_set_mute (GstMixer * mixer, GstMixerTrack * track, gboolean mute)
{
  int volume;
  GstOssElement *oss = GST_OSSELEMENT (mixer);
  GstOssMixerTrack *osstrack = GST_OSSMIXER_TRACK (track);

  /* assert that we're opened and that we're using a known item */
  g_return_if_fail (oss->mixer_fd != -1);
  g_return_if_fail (gst_ossmixer_contains_track (oss, osstrack));

  if (mute) {
    volume = 0;
  } else {
    volume = (osstrack->lvol & 0xff);
    if (MASK_BIT_IS_SET (oss->stereomask, osstrack->track_num)) {
      volume |= ((osstrack->rvol & 0xff) << 8);
    }
  }

  if (ioctl (oss->mixer_fd, MIXER_WRITE (osstrack->track_num), &volume) < 0) {
    g_warning ("Error setting mixer recording device volume (0x%x): %s",
        volume, strerror (errno));
    return;
  }

  if (mute) {
    track->flags |= GST_MIXER_TRACK_MUTE;
  } else {
    track->flags &= ~GST_MIXER_TRACK_MUTE;
  }
}

static void
gst_ossmixer_set_record (GstMixer * mixer,
    GstMixerTrack * track, gboolean record)
{
  GstOssElement *oss = GST_OSSELEMENT (mixer);
  GstOssMixerTrack *osstrack = GST_OSSMIXER_TRACK (track);

  /* assert that we're opened and that we're using a known item */
  g_return_if_fail (oss->mixer_fd != -1);
  g_return_if_fail (gst_ossmixer_contains_track (oss, osstrack));

  /* if there's nothing to do... */
  if ((record && GST_MIXER_TRACK_HAS_FLAG (track, GST_MIXER_TRACK_RECORD)) ||
      (!record && !GST_MIXER_TRACK_HAS_FLAG (track, GST_MIXER_TRACK_RECORD)))
    return;

  /* if we're exclusive, then we need to unset the current one(s) */
  if (oss->mixcaps & SOUND_CAP_EXCL_INPUT) {
    GList *track;

    for (track = oss->tracklist; track != NULL; track = track->next) {
      GstMixerTrack *turn = (GstMixerTrack *) track->data;

      turn->flags &= ~GST_MIXER_TRACK_RECORD;
    }
    oss->recdevs = 0;
  }

  /* set new record bit, if needed */
  if (record) {
    oss->recdevs |= (1 << osstrack->track_num);
  } else {
    oss->recdevs &= ~(1 << osstrack->track_num);
  }

  /* set it to the device */
  if (ioctl (oss->mixer_fd, SOUND_MIXER_WRITE_RECSRC, &oss->recdevs) < 0) {
    g_warning ("Error setting mixer recording devices (0x%x): %s",
        oss->recdevs, strerror (errno));
    return;
  }

  if (record) {
    track->flags |= GST_MIXER_TRACK_RECORD;
  } else {
    track->flags &= ~GST_MIXER_TRACK_RECORD;
  }
}

void
gst_ossmixer_build_list (GstOssElement * oss)
{
  gint i, devmask, master = -1;
  const GList *pads = gst_element_get_pad_list (GST_ELEMENT (oss));
  GstPadDirection dir = GST_PAD_UNKNOWN;

#ifdef SOUND_MIXER_INFO
  struct mixer_info minfo;
#endif

  g_return_if_fail (oss->mixer_fd == -1);

  oss->mixer_fd = open (oss->mixer_dev, O_RDWR);
  if (oss->mixer_fd == -1) {
    /* this is valid. OSS devices don't need to expose a mixer */
    GST_DEBUG ("Failed to open mixer device %s, mixing disabled: %s",
        oss->mixer_dev, strerror (errno));
    return;
  }

  /* get direction */
  if (pads && g_list_length ((GList *) pads) == 1)
    dir = GST_PAD_DIRECTION (GST_PAD (pads->data));

  /* get masks */
  if (ioctl (oss->mixer_fd, SOUND_MIXER_READ_RECMASK, &oss->recmask) < 0 ||
      ioctl (oss->mixer_fd, SOUND_MIXER_READ_RECSRC, &oss->recdevs) < 0 ||
      ioctl (oss->mixer_fd, SOUND_MIXER_READ_STEREODEVS, &oss->stereomask) < 0
      || ioctl (oss->mixer_fd, SOUND_MIXER_READ_DEVMASK, &devmask) < 0
      || ioctl (oss->mixer_fd, SOUND_MIXER_READ_CAPS, &oss->mixcaps) < 0) {
    GST_DEBUG ("Failed to get device masks - disabling mixer");
    close (oss->mixer_fd);
    oss->mixer_fd = -1;
    return;
  }

  /* get name */
#ifdef SOUND_MIXER_INFO
  if (ioctl (oss->mixer_fd, SOUND_MIXER_INFO, &minfo) == 0) {
    oss->device_name = g_strdup (minfo.name);
  }
#else
  oss->device_name = g_strdup ("Unknown");
#endif

  /* find master volume */
  if (devmask & SOUND_MASK_VOLUME)
    master = SOUND_MIXER_VOLUME;
  else if (devmask & SOUND_MASK_PCM)
    master = SOUND_MIXER_PCM;
  else if (devmask & SOUND_MASK_SPEAKER)
    master = SOUND_MIXER_SPEAKER;       /* doubtful... */
  /* else: no master, so we won't set any */

  /* build track list */
  for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
    if (devmask & (1 << i)) {
      GstMixerTrack *track;
      gboolean input = FALSE, stereo = FALSE, record = FALSE;

      /* track exists, make up capabilities */
      if (MASK_BIT_IS_SET (oss->stereomask, i))
        stereo = TRUE;
      if (MASK_BIT_IS_SET (oss->recmask, i))
        input = TRUE;
      if (MASK_BIT_IS_SET (oss->recdevs, i))
        record = TRUE;

      /* do we want this in our list? */
      if ((dir == GST_PAD_SRC && input == FALSE) ||
          (dir == GST_PAD_SINK && i != SOUND_MIXER_PCM))
        continue;

      /* add track to list */
      track = gst_ossmixer_track_new (oss, i, stereo ? 2 : 1,
          (record ? GST_MIXER_TRACK_RECORD : 0) |
          (input ? GST_MIXER_TRACK_INPUT :
              GST_MIXER_TRACK_OUTPUT) |
          ((master != i) ? 0 : GST_MIXER_TRACK_MASTER));
      oss->tracklist = g_list_append (oss->tracklist, track);
    }
  }
}

void
gst_ossmixer_free_list (GstOssElement * oss)
{
  if (oss->mixer_fd == -1)
    return;

  g_list_foreach (oss->tracklist, (GFunc) g_object_unref, NULL);
  g_list_free (oss->tracklist);
  oss->tracklist = NULL;

  if (oss->device_name) {
    g_free (oss->device_name);
    oss->device_name = NULL;
  }

  close (oss->mixer_fd);
  oss->mixer_fd = -1;
}