summaryrefslogtreecommitdiff
path: root/sys/oss4/oss4-audio.c
blob: 44d2f6db9fef0aef86c344ac44f655d4591d97b9 (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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
/* GStreamer OSS4 audio plugin
 * Copyright (C) 2007-2008 Tim-Philipp Müller <tim centricular net>
 *
 * 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., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

/* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
 * with newer GLib versions (>= 2.31.0) */
#define GLIB_DISABLE_DEPRECATION_WARNINGS

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

#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

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

#include "oss4-audio.h"
#include "oss4-property-probe.h"
#include "oss4-sink.h"
#include "oss4-source.h"
#include "oss4-soundcard.h"

GST_DEBUG_CATEGORY (oss4mixer_debug);
GST_DEBUG_CATEGORY (oss4sink_debug);
GST_DEBUG_CATEGORY (oss4src_debug);
GST_DEBUG_CATEGORY (oss4_debug);

#define GST_CAT_DEFAULT oss4_debug

typedef struct
{
  const GstAudioRingBufferFormatType gst_rbfmt;
  const GstAudioFormat gst_rfmt;
  const gint oss_fmt;
  const gchar name[16];
} GstOss4AudioFormat;

/* *INDENT-OFF* */
static const GstOss4AudioFormat fmt_map[] = {
  /* note: keep sorted by preference, preferred formats first */
  {
  GST_AUDIO_RING_BUFFER_FORMAT_TYPE_MU_LAW, 0,
      AFMT_MU_LAW, "audio/x-mulaw"}, {
  GST_AUDIO_RING_BUFFER_FORMAT_TYPE_A_LAW, 0,
      AFMT_A_LAW, "audio/x-alaw"}, {
  GST_AUDIO_RING_BUFFER_FORMAT_TYPE_RAW, GST_AUDIO_FORMAT_S32LE,
      AFMT_S32_LE, "audio/x-raw"}, {
  GST_AUDIO_RING_BUFFER_FORMAT_TYPE_RAW, GST_AUDIO_FORMAT_S32BE,
      AFMT_S32_BE, "audio/x-raw"}, {
  GST_AUDIO_RING_BUFFER_FORMAT_TYPE_RAW, GST_AUDIO_FORMAT_S24_32LE,
      AFMT_S24_LE, "audio/x-raw"}, {
  GST_AUDIO_RING_BUFFER_FORMAT_TYPE_RAW, GST_AUDIO_FORMAT_S24_32BE,
      AFMT_S24_BE, "audio/x-raw"}, {
  GST_AUDIO_RING_BUFFER_FORMAT_TYPE_RAW, GST_AUDIO_FORMAT_S24LE,
      AFMT_S24_PACKED, "audio/x-raw"}, {
  GST_AUDIO_RING_BUFFER_FORMAT_TYPE_RAW, GST_AUDIO_FORMAT_S16LE,
      AFMT_S16_LE, "audio/x-raw"}, {
  GST_AUDIO_RING_BUFFER_FORMAT_TYPE_RAW, GST_AUDIO_FORMAT_S16BE,
      AFMT_S16_BE, "audio/x-raw"}, {
  GST_AUDIO_RING_BUFFER_FORMAT_TYPE_RAW, GST_AUDIO_FORMAT_U16LE,
      AFMT_U16_LE, "audio/x-raw"}, {
  GST_AUDIO_RING_BUFFER_FORMAT_TYPE_RAW, GST_AUDIO_FORMAT_U16BE,
      AFMT_U16_BE, "audio/x-raw"}, {
  GST_AUDIO_RING_BUFFER_FORMAT_TYPE_RAW, GST_AUDIO_FORMAT_S8,
      AFMT_S8, "audio/x-raw"}, {
  GST_AUDIO_RING_BUFFER_FORMAT_TYPE_RAW, GST_AUDIO_FORMAT_U8,
      AFMT_U8, "audio/x-raw"}
};
/* *INDENT-ON* */

/* formats we assume the OSS4 layer can always handle and convert internally */
#define CONVERTIBLE_FORMATS (   \
    AFMT_MU_LAW | AFMT_A_LAW |  \
    AFMT_S32_LE | AFMT_S32_BE | \
    AFMT_S24_LE | AFMT_S24_BE | \
    AFMT_S24_PACKED |           \
    AFMT_S16_LE | AFMT_S16_BE | \
    AFMT_U16_LE | AFMT_U16_BE | \
    AFMT_S8 | AFMT_U8 )

static void
gst_oss4_append_format_to_caps (const GstOss4AudioFormat * fmt, GstCaps * caps)
{
  GstStructure *s;

  s = gst_structure_new_empty (fmt->name);
  if (fmt->gst_rbfmt == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_RAW) {
    gst_structure_set (s, "format", G_TYPE_STRING,
        gst_audio_format_to_string (fmt->gst_rfmt),
        "layout", G_TYPE_STRING, "interleaved", NULL);
  }
  gst_caps_append_structure (caps, s);
}

static gint
gst_oss4_audio_get_oss_format (GstAudioRingBufferFormatType fmt,
    GstAudioFormat rfmt)
{
  guint i;

  for (i = 0; i < G_N_ELEMENTS (fmt_map); ++i) {
    if (fmt_map[i].gst_rbfmt == fmt && fmt_map[i].gst_rfmt == rfmt)
      return fmt_map[i].oss_fmt;
  }
  return 0;
}

/* These are pretty random */
#define GST_OSS4_MIN_SAMPLE_RATE 1
#define GST_OSS4_MAX_SAMPLE_RATE 192000

static gboolean
gst_oss4_audio_detect_rates (GstObject * obj, oss_audioinfo * ai,
    GstCaps * caps)
{
  GValue val = { 0, };
  int minrate, maxrate, i;

  minrate = ai->min_rate;
  maxrate = ai->max_rate;

  /* sanity check */
  if (minrate > maxrate) {
    GST_WARNING_OBJECT (obj, "min_rate %d > max_rate %d (buggy driver?)",
        minrate, maxrate);
    maxrate = ai->min_rate;     /* swap */
    minrate = ai->max_rate;
  }

  /* limit to something sensible */
  if (minrate < GST_OSS4_MIN_SAMPLE_RATE)
    minrate = GST_OSS4_MIN_SAMPLE_RATE;
  if (maxrate > GST_OSS4_MAX_SAMPLE_RATE)
    maxrate = GST_OSS4_MAX_SAMPLE_RATE;

  if (maxrate < GST_OSS4_MIN_SAMPLE_RATE) {
    GST_WARNING_OBJECT (obj, "max_rate < %d, which makes no sense",
        GST_OSS4_MIN_SAMPLE_RATE);
    return FALSE;
  }

  GST_LOG_OBJECT (obj, "min_rate %d, max_rate %d (originally: %d, %d)",
      minrate, maxrate, ai->min_rate, ai->max_rate);

  if ((ai->caps & PCM_CAP_FREERATE)) {
    GST_LOG_OBJECT (obj, "device supports any sample rate between min and max");
    if (minrate == maxrate) {
      g_value_init (&val, G_TYPE_INT);
      g_value_set_int (&val, maxrate);
    } else {
      g_value_init (&val, GST_TYPE_INT_RANGE);
      gst_value_set_int_range (&val, minrate, maxrate);
    }
  } else {
    GST_LOG_OBJECT (obj, "%d sample rates:", ai->nrates);
    g_value_init (&val, GST_TYPE_LIST);
    for (i = 0; i < ai->nrates; ++i) {
      GST_LOG_OBJECT (obj, " rate: %d", ai->rates[i]);

      if (ai->rates[i] >= minrate && ai->rates[i] <= maxrate) {
        GValue rate_val = { 0, };

        g_value_init (&rate_val, G_TYPE_INT);
        g_value_set_int (&rate_val, ai->rates[i]);
        gst_value_list_append_value (&val, &rate_val);
        g_value_unset (&rate_val);
      }
    }

    if (gst_value_list_get_size (&val) == 0) {
      g_value_unset (&val);
      return FALSE;
    }
  }

  for (i = 0; i < gst_caps_get_size (caps); ++i) {
    GstStructure *s;

    s = gst_caps_get_structure (caps, i);
    gst_structure_set_value (s, "rate", &val);
  }

  g_value_unset (&val);

  return TRUE;
}

static void
gst_oss4_audio_get_channel_layout (GstObject * obj, guint64 layout,
    guint num_channels, GstAudioChannelPosition * ch_layout)
{
  const GstAudioChannelPosition pos_map[16] = {
    GST_AUDIO_CHANNEL_POSITION_NONE,    /* 0 = dunno          */
    GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,      /* 1 = left           */
    GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,     /* 2 = right          */
    GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,    /* 3 = center         */
    GST_AUDIO_CHANNEL_POSITION_LFE1,    /* 4 = lfe            */
    GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,       /* 5 = left surround  */
    GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT,      /* 6 = right surround */
    GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,       /* 7 = left rear      */
    GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,      /* 8 = right rear     */
    GST_AUDIO_CHANNEL_POSITION_NONE,
    GST_AUDIO_CHANNEL_POSITION_NONE,
    GST_AUDIO_CHANNEL_POSITION_NONE,
    GST_AUDIO_CHANNEL_POSITION_NONE,
    GST_AUDIO_CHANNEL_POSITION_NONE,
    GST_AUDIO_CHANNEL_POSITION_NONE,
    GST_AUDIO_CHANNEL_POSITION_NONE
  };
  guint speaker_pos;            /* speaker position as defined by OSS */
  guint i;

  for (i = 0; i < num_channels; ++i) {
    /* layout contains up to 16 speaker positions, with each taking up 4 bits */
    speaker_pos = (guint) ((layout >> (i * 4)) & 0x0f);

    /* if it's a channel position that's unknown to us, set all to NONE and
     * bail out */
    if (G_UNLIKELY (pos_map[speaker_pos] == GST_AUDIO_CHANNEL_POSITION_NONE))
      goto no_layout;

    ch_layout[i] = pos_map[speaker_pos];
  }

  return;

no_layout:
  {
    /* only warn if it's really unknown, position 0 is ok and represents NONE
     * (in which case we also just set all others to NONE ignoring the other
     * positions in the OSS-given layout, because that's what we currently
     * require in GStreamer) */
    if (speaker_pos != 0) {
      GST_WARNING_OBJECT (obj, "unknown OSS channel position %x", ch_layout[i]);
    }
    for (i = 0; i < num_channels; ++i) {
      ch_layout[i] = GST_AUDIO_CHANNEL_POSITION_NONE;
    }
    return;
  }
}

static void
gst_oss4_audio_set_ringbuffer_channel_layout (GstObject * obj, gint fd,
    GstAudioRingBufferSpec * spec)
{
  guint num_channels;
  guint64 layout = 0;
  GstAudioRingBuffer *rb;
  GstAudioChannelPosition ch_layout[8] = { 0, };

  num_channels = GST_AUDIO_INFO_CHANNELS (&spec->info);
  if (num_channels < 3 || num_channels > 8)
    return;

  if (spec->type != GST_AUDIO_RING_BUFFER_FORMAT_TYPE_RAW)
    return;

  if (GST_IS_OSS4_SINK (obj)) {
    rb = GST_AUDIO_BASE_SINK (obj)->ringbuffer;
  } else if (GST_IS_OSS4_SOURCE (obj)) {
    rb = GST_AUDIO_BASE_SRC (obj)->ringbuffer;
  } else
    g_return_if_reached ();

  /* -1 = get info for currently open device (fd). This will fail with
   * OSS build <= 1013 because of a bug in OSS */
  if (ioctl (fd, SNDCTL_DSP_GET_CHNORDER, &layout) == -1) {
    GST_WARNING_OBJECT (obj, "couldn't query channel layout, assuming default");
    layout = CHNORDER_NORMAL;
  }
  GST_DEBUG_OBJECT (obj, "channel layout: %08" G_GINT64_MODIFIER "x", layout);


  gst_oss4_audio_get_channel_layout (obj, layout, num_channels, ch_layout);
  gst_audio_ring_buffer_set_channel_positions (rb, ch_layout);

  return;
}

static void
gst_oss4_audio_add_channel_layout (GstObject * obj, guint64 layout,
    guint num_channels, GstStructure * s)
{
  GstAudioChannelPosition ch_layout[8] = { 0, };
  guint64 mask;

  g_return_if_fail (num_channels <= G_N_ELEMENTS (ch_layout));

  gst_oss4_audio_get_channel_layout (obj, layout, num_channels, ch_layout);
  if (gst_audio_channel_positions_to_mask (ch_layout, num_channels, FALSE,
          &mask))
    gst_structure_set (s, "channel-mask", GST_TYPE_BITMASK, mask, NULL);

  return;
}

/* arbitrary max. limit */
#define GST_OSS4_MIN_CHANNELS 1
#define GST_OSS4_MAX_CHANNELS 4096

/* takes ownership of the input caps */
static GstCaps *
gst_oss4_audio_detect_channels (GstObject * obj, int fd, oss_audioinfo * ai,
    GstCaps * in_caps)
{
  const gchar *forced_layout;
  GstStructure *s = NULL;
  guint64 layout = 0;
  GstCaps *chan_caps = NULL;
  GstCaps *out_caps = NULL;
  int minchans, maxchans;
  int c, i, j;

  /* GST_OSS4_CHANNEL_LAYOUT environment variable: may be used to force a
   * particular channel layout (if it contains an odd number of channel
   * positions it will also make us advertise a channel layout for that
   * channel count, even if we'd usually skip it; this is especially useful
   * for folks with 2.1 speakers, I guess) */
  forced_layout = g_getenv ("GST_OSS4_CHANNEL_LAYOUT");

  minchans = ai->min_channels;
  maxchans = ai->max_channels;

  /* sanity check */
  if (minchans > maxchans) {
    GST_WARNING_OBJECT (obj, "min_chans %d > max_chans %d (buggy driver?)",
        minchans, maxchans);
    maxchans = ai->min_channels;        /* swap */
    minchans = ai->max_channels;
  }

  /* limit to something sensible */
  if (minchans < GST_OSS4_MIN_CHANNELS)
    minchans = GST_OSS4_MIN_CHANNELS;
  if (maxchans > GST_OSS4_MAX_CHANNELS)
    maxchans = GST_OSS4_MAX_CHANNELS;

  if (maxchans < GST_OSS4_MIN_CHANNELS) {
    GST_WARNING_OBJECT (obj, "max_chans < %d, which makes no sense",
        GST_OSS4_MIN_CHANNELS);
    gst_caps_unref (in_caps);
    return NULL;
  }

  GST_LOG_OBJECT (obj, "min_channels %d, max_channels %d (originally: %d, %d)",
      minchans, maxchans, ai->min_channels, ai->max_channels);

  chan_caps = gst_caps_new_empty ();

  /* first do the simple cases: mono + stereo (channel layout implied) */
  if (minchans == 1 && maxchans == 1)
    s = gst_structure_new ("x", "channels", G_TYPE_INT, 1, NULL);
  else if (minchans == 2 && maxchans >= 2)
    s = gst_structure_new ("x", "channels", G_TYPE_INT, 2, NULL);
  else if (minchans == 1 && maxchans >= 2)
    s = gst_structure_new ("x", "channels", GST_TYPE_INT_RANGE, 1, 2, NULL);
  gst_caps_append_structure (chan_caps, s);
  s = NULL;

  /* TODO: we assume all drivers use a left/right layout for stereo here */
  if (maxchans <= 2)
    goto done;

  if (ioctl (fd, SNDCTL_DSP_GET_CHNORDER, &layout) == -1) {
    GST_WARNING_OBJECT (obj, "couldn't query channel layout, assuming default");
    layout = CHNORDER_NORMAL;
  }
  GST_DEBUG_OBJECT (obj, "channel layout: %08" G_GINT64_MODIFIER "x", layout);

  /* e.g. forced 2.1 layout would be GST_OSS4_CHANNEL_LAYOUT=421 */
  if (forced_layout != NULL && *forced_layout != '\0') {
    guint layout_len;

    layout_len = strlen (forced_layout);
    if (layout_len >= minchans && layout_len <= maxchans) {
      layout = g_ascii_strtoull (forced_layout, NULL, 16);
      maxchans = layout_len;
      GST_DEBUG_OBJECT (obj, "forced channel layout: %08" G_GINT64_MODIFIER "x"
          " ('%s'), maxchans now %d", layout, forced_layout, maxchans);
    } else {
      GST_WARNING_OBJECT (obj, "ignoring forced channel layout: layout has %d "
          "channel positions but maxchans is %d", layout_len, maxchans);
    }
  }

  /* need to advertise channel layouts for anything >2 and <=8 channels */
  for (c = MAX (3, minchans); c <= MIN (maxchans, 8); c++) {
    /* "The min_channels and max_channels fields define the limits for the
     * number of channels. However some devices don't support all channels
     * within this range. It's possible that the odd values (3, 5, 7, 9, etc).
     * are not supported. There is currently no way to check for this other
     * than checking if SNDCTL_DSP_CHANNELS accepts the requested value.
     * Another approach is trying to avoid using odd number of channels."
     *
     * So, we don't know for sure if these odd values are supported:
     */
    if ((c == 3 || c == 5 || c == 7) && (c != maxchans)) {
      GST_LOG_OBJECT (obj, "not adding layout with %d channels", c);
      continue;
    }

    s = gst_structure_new ("x", "channels", G_TYPE_INT, c, NULL);
    gst_oss4_audio_add_channel_layout (obj, layout, c, s);
    GST_LOG_OBJECT (obj, "c=%u, appending struct %" GST_PTR_FORMAT, c, s);
    gst_caps_append_structure (chan_caps, s);
    s = NULL;
  }

  if (maxchans <= 8)
    goto done;

  /* for everything >8 channels, CHANNEL_POSITION_NONE is implied. */
  if (minchans == maxchans || maxchans == 9) {
    s = gst_structure_new ("x", "channels", G_TYPE_INT, maxchans, NULL);
  } else {
    s = gst_structure_new ("x", "channels", GST_TYPE_INT_RANGE,
        MAX (9, minchans), maxchans, NULL);
  }
  gst_caps_append_structure (chan_caps, s);
  s = NULL;

done:

  GST_LOG_OBJECT (obj, "channel structures: %" GST_PTR_FORMAT, chan_caps);

  out_caps = gst_caps_new_empty ();

  /* combine each structure in the input caps with each channel caps struct */
  for (i = 0; i < gst_caps_get_size (in_caps); ++i) {
    const GstStructure *in_s;

    in_s = gst_caps_get_structure (in_caps, i);

    for (j = 0; j < gst_caps_get_size (chan_caps); ++j) {
      const GstStructure *chan_s;
      const GValue *val;

      s = gst_structure_copy (in_s);
      chan_s = gst_caps_get_structure (chan_caps, j);
      if ((val = gst_structure_get_value (chan_s, "channels")))
        gst_structure_set_value (s, "channels", val);
      if ((val = gst_structure_get_value (chan_s, "channel-mask")))
        gst_structure_set_value (s, "channel-mask", val);

      gst_caps_append_structure (out_caps, s);
      s = NULL;
    }
  }

  gst_caps_unref (in_caps);
  gst_caps_unref (chan_caps);
  return out_caps;
}

GstCaps *
gst_oss4_audio_probe_caps (GstObject * obj, int fd)
{
  oss_audioinfo ai = { 0, };
  gboolean output;
  GstCaps *caps;
  int nonnative_formats = 0;
  int formats, i;

  output = GST_IS_OSS4_SINK (obj);

  /* -1 = get info for currently open device (fd). This will fail with
   * OSS build <= 1013 because of a bug in OSS */
  ai.dev = -1;
  if (ioctl (fd, SNDCTL_ENGINEINFO, &ai) == -1)
    goto engineinfo_failed;

  formats = (output) ? ai.oformats : ai.iformats;

  GST_LOG_OBJECT (obj, "%s formats : 0x%08x", (output) ? "out" : "in", formats);

  caps = gst_caps_new_empty ();

  /* first list all the formats natively supported */
  for (i = 0; i < G_N_ELEMENTS (fmt_map); ++i) {
    if ((formats & fmt_map[i].oss_fmt)) {
      gst_oss4_append_format_to_caps (&fmt_map[i], caps);
    } else if ((fmt_map[i].oss_fmt & CONVERTIBLE_FORMATS)) {
      nonnative_formats |= fmt_map[i].oss_fmt;
    }
  }

  GST_LOG_OBJECT (obj, "adding non-native %s formats : 0x%08x",
      (output) ? "out" : "in", nonnative_formats);

  /* now append non-native formats for which conversion would be needed */
  for (i = 0; i < G_N_ELEMENTS (fmt_map); ++i) {
    if ((nonnative_formats & fmt_map[i].oss_fmt)) {
      gst_oss4_append_format_to_caps (&fmt_map[i], caps);
    }
  }

  caps = gst_caps_simplify (caps);
  GST_LOG_OBJECT (obj, "formats: %" GST_PTR_FORMAT, caps);

  if (!gst_oss4_audio_detect_rates (obj, &ai, caps))
    goto detect_rates_failed;

  caps = gst_oss4_audio_detect_channels (obj, fd, &ai, caps);
  if (caps == NULL)
    goto detect_channels_failed;

  GST_LOG_OBJECT (obj, "probed caps: %" GST_PTR_FORMAT, caps);

  return caps;

/* ERRORS */
engineinfo_failed:
  {
    GST_WARNING ("ENGINEINFO supported formats probe failed: %s",
        g_strerror (errno));
    return NULL;
  }
detect_rates_failed:
  {
    GST_WARNING_OBJECT (obj, "failed to detect supported sample rates");
    gst_caps_unref (caps);
    return NULL;
  }
detect_channels_failed:
  {
    GST_WARNING_OBJECT (obj, "failed to detect supported channels");
    gst_caps_unref (caps);
    return NULL;
  }
}

GstCaps *
gst_oss4_audio_get_template_caps (void)
{
  GstCaps *caps;
  gint i;

  caps = gst_caps_new_empty ();

  for (i = 0; i < G_N_ELEMENTS (fmt_map); ++i) {
    gst_oss4_append_format_to_caps (&fmt_map[i], caps);
  }

  caps = gst_caps_simplify (caps);

  for (i = 0; i < gst_caps_get_size (caps); ++i) {
    GstStructure *s;

    s = gst_caps_get_structure (caps, i);
    gst_structure_set (s, "rate", GST_TYPE_INT_RANGE, GST_OSS4_MIN_SAMPLE_RATE,
        GST_OSS4_MAX_SAMPLE_RATE, "channels", GST_TYPE_INT_RANGE,
        GST_OSS4_MIN_CHANNELS, GST_OSS4_MAX_CHANNELS, NULL);
  }

  return caps;
}

/* called by gst_oss4_sink_prepare() and gst_oss4_source_prepare() */
gboolean
gst_oss4_audio_set_format (GstObject * obj, int fd,
    GstAudioRingBufferSpec * spec)
{
  struct audio_buf_info info = { 0, };
  int ofmt, fmt, chans, rate, width;

  fmt = gst_oss4_audio_get_oss_format (spec->type,
      GST_AUDIO_INFO_FORMAT (&spec->info));

  if (fmt == 0)
    goto wrong_format;

  ofmt = fmt;
  chans = GST_AUDIO_INFO_CHANNELS (&spec->info);
  rate = GST_AUDIO_INFO_RATE (&spec->info);
  width = GST_AUDIO_INFO_WIDTH (&spec->info);

  if (spec->type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_RAW &&
      width != 32 && width != 24 && width != 16 && width != 8) {
    goto dodgy_width;
  }

  /* format */
  GST_LOG_OBJECT (obj, "setting format: %d", fmt);
  if (ioctl (fd, SNDCTL_DSP_SETFMT, &fmt) == -1)
    goto set_format_failed;

  /* channels */
  GST_LOG_OBJECT (obj, "setting channels: %d", chans);
  if (ioctl (fd, SNDCTL_DSP_CHANNELS, &chans) == -1)
    goto set_channels_failed;

  /* rate */
  GST_LOG_OBJECT (obj, "setting rate: %d", rate);
  if (ioctl (fd, SNDCTL_DSP_SPEED, &rate) == -1)
    goto set_rate_failed;

  GST_DEBUG_OBJECT (obj, "effective format   : %d", fmt);
  GST_DEBUG_OBJECT (obj, "effective channels : %d", chans);
  GST_DEBUG_OBJECT (obj, "effective rate     : %d", rate);

  /* make sure format, channels, and rate are the ones we requested */
  if (fmt != ofmt || chans != GST_AUDIO_INFO_CHANNELS (&spec->info) ||
      rate != GST_AUDIO_INFO_RATE (&spec->info)) {
    /* This shouldn't happen, but hey */
    goto format_not_what_was_requested;
  }

  if (GST_IS_OSS4_SOURCE (obj)) {
    if (ioctl (fd, SNDCTL_DSP_GETISPACE, &info) == -1)
      goto get_ispace_failed;
  } else {
    if (ioctl (fd, SNDCTL_DSP_GETOSPACE, &info) == -1)
      goto get_ospace_failed;
  }

  spec->segsize = info.fragsize;

  /* we add some extra fragments -- this helps us account for delays due to
   * conversion buffer, streams queueing, etc.  It is important that these
   * be taken into account because otherwise the delay counter can wind up
   * being too large, and the buffer will wrap.  */
  spec->segtotal = info.fragstotal + 4;

  GST_DEBUG_OBJECT (obj, "got segsize: %d, segtotal: %d, value: %08x",
      spec->segsize, spec->segtotal, info.fragsize);

  gst_oss4_audio_set_ringbuffer_channel_layout (obj, fd, spec);

  return TRUE;

/* ERRORS */
wrong_format:
  {
    GST_ELEMENT_ERROR (obj, RESOURCE, SETTINGS, (NULL),
        ("Unable to get format (%d, %d)", spec->type,
            GST_AUDIO_INFO_FORMAT (&spec->info)));
    return FALSE;
  }
dodgy_width:
  {
    GST_ELEMENT_ERROR (obj, RESOURCE, SETTINGS, (NULL),
        ("unexpected width %d", width));
    return FALSE;
  }
set_format_failed:
  {
    GST_ELEMENT_ERROR (obj, RESOURCE, SETTINGS, (NULL),
        ("DSP_SETFMT(%d) failed: %s", fmt, g_strerror (errno)));
    return FALSE;
  }
set_channels_failed:
  {
    GST_ELEMENT_ERROR (obj, RESOURCE, SETTINGS, (NULL),
        ("DSP_CHANNELS(%d) failed: %s", chans, g_strerror (errno)));
    return FALSE;
  }
set_rate_failed:
  {
    GST_ELEMENT_ERROR (obj, RESOURCE, SETTINGS, (NULL),
        ("DSP_SPEED(%d) failed: %s", rate, g_strerror (errno)));
    return FALSE;
  }
get_ospace_failed:
  {
    GST_ELEMENT_ERROR (obj, RESOURCE, SETTINGS, (NULL),
        ("DSP_GETOSPACE failed: %s", g_strerror (errno)));
    return FALSE;
  }
get_ispace_failed:
  {
    GST_ELEMENT_ERROR (obj, RESOURCE, SETTINGS, (NULL),
        ("DSP_GETISPACE failed: %s", g_strerror (errno)));
    return FALSE;
  }
format_not_what_was_requested:
  {
    GST_ELEMENT_ERROR (obj, RESOURCE, SETTINGS, (NULL),
        ("Format actually configured wasn't the one we requested. This is "
            "probably either a bug in the driver or in the format probing code."));
    return FALSE;
  }
}

int
gst_oss4_audio_get_version (GstObject * obj, int fd)
{
  gint ver = 0;

  /* we use the old ioctl here on purpose instead of SNDCTL_SYSINFO */
  if (ioctl (fd, OSS_GETVERSION, &ver) < 0) {
    GST_LOG_OBJECT (obj, "OSS_GETVERSION failed: %s", g_strerror (errno));
    return -1;
  }
  GST_LOG_OBJECT (obj, "OSS version: 0x%08x", ver);
  return ver;
}

gboolean
gst_oss4_audio_check_version (GstObject * obj, int fd)
{
  return (gst_oss4_audio_get_version (obj, fd) >= GST_MIN_OSS4_VERSION);
}

gchar *
gst_oss4_audio_find_device (GstObject * oss)
{
  GValueArray *arr;
  gchar *ret = NULL;

  arr = gst_oss4_property_probe_get_values (GST_OBJECT (oss), "device");

  if (arr != NULL) {
    if (arr->n_values > 0) {
      const GValue *val;

      val = g_value_array_get_nth (arr, 0);
      ret = g_value_dup_string (val);
    }
    g_value_array_free (arr);
  }

  GST_LOG_OBJECT (oss, "first device found: %s", GST_STR_NULL (ret));

  return ret;
}

static gboolean
plugin_init (GstPlugin * plugin)
{
  gint rank;

  GST_DEBUG_CATEGORY_INIT (oss4sink_debug, "oss4sink", 0, "OSS4 audio sink");
  GST_DEBUG_CATEGORY_INIT (oss4src_debug, "oss4src", 0, "OSS4 audio src");
  GST_DEBUG_CATEGORY_INIT (oss4mixer_debug, "oss4mixer", 0, "OSS4 mixer");
  GST_DEBUG_CATEGORY_INIT (oss4_debug, "oss4", 0, "OSS4 plugin");

#ifdef ENABLE_NLS
  GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
      LOCALEDIR);
  bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
#endif

  /* we want a higher rank than the legacy OSS elements have now */
  rank = GST_RANK_SECONDARY + 1;

  if (!gst_element_register (plugin, "oss4sink", rank, GST_TYPE_OSS4_SINK) ||
      !gst_element_register (plugin, "oss4src", rank, GST_TYPE_OSS4_SOURCE)) {
    return FALSE;
  }

  return TRUE;
}

GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
    GST_VERSION_MINOR,
    oss4,
    "Open Sound System (OSS) version 4 support for GStreamer",
    plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)