oss4-mixer.c 54 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
/* GStreamer OSS4 mixer implementation
 * 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 mixer library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/**
 * SECTION:element-oss4mixer
 *
 * This element lets you adjust sound input and output levels with the
 * Open Sound System (OSS) version 4. It supports the GstMixer interface, which
 * can be used to obtain a list of available mixer tracks. Set the mixer
 * element to READY state before using the GstMixer interface on it.
Stefan Kost's avatar
Stefan Kost committed
27 28
 * 
 * <refsect2>
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
 * <title>Example pipelines</title>
 * <para>
 * oss4mixer can&apos;t be used in a sensible way in gst-launch.
 * </para>
 * </refsect2>
 *
 * Since: 0.10.7
 */

/* Note: ioctl calls on the same open mixer device are serialised via
 * the object lock to make sure we don't do concurrent ioctls from two
 * different threads (e.g. app thread and mixer watch thread), since that
 * will probably confuse OSS. */

#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>

#include <gst/interfaces/mixer.h>
#include <gst/gst-i18n-plugin.h>
57
#include "gst/glib-compat-private.h"
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

#include <glib/gprintf.h>

#define NO_LEGACY_MIXER

#include "oss4-audio.h"
#include "oss4-mixer.h"
#include "oss4-mixer-enum.h"
#include "oss4-mixer-slider.h"
#include "oss4-mixer-switch.h"
#include "oss4-property-probe.h"
#include "oss4-soundcard.h"

#define GST_OSS4_MIXER_WATCH_INTERVAL   500     /* in millisecs, ie. 0.5s */

GST_DEBUG_CATEGORY_EXTERN (oss4mixer_debug);
#define GST_CAT_DEFAULT oss4mixer_debug

#define DEFAULT_DEVICE       NULL
#define DEFAULT_DEVICE_NAME  NULL

enum
{
  PROP_0,
  PROP_DEVICE,
  PROP_DEVICE_NAME
};

static void gst_oss4_mixer_init_interfaces (GType type);

GST_BOILERPLATE_FULL (GstOss4Mixer, gst_oss4_mixer, GstElement,
    GST_TYPE_ELEMENT, gst_oss4_mixer_init_interfaces);

static GstStateChangeReturn gst_oss4_mixer_change_state (GstElement *
    element, GstStateChange transition);

static void gst_oss4_mixer_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);
static void gst_oss4_mixer_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec);
static void gst_oss4_mixer_finalize (GObject * object);

static gboolean gst_oss4_mixer_open (GstOss4Mixer * mixer,
    gboolean silent_errors);
static void gst_oss4_mixer_close (GstOss4Mixer * mixer);

static gboolean gst_oss4_mixer_enum_control_update_enum_list (GstOss4Mixer * m,
    GstOss4MixerControl * mc);

107
#ifndef GST_DISABLE_GST_DEBUG
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
static const gchar *mixer_ext_type_get_name (gint type);
static const gchar *mixer_ext_flags_get_string (gint flags);
#endif

static void
gst_oss4_mixer_base_init (gpointer klass)
{
  gst_element_class_set_details_simple (GST_ELEMENT_CLASS (klass),
      "OSS v4 Audio Mixer", "Generic/Audio",
      "Control sound input and output levels with OSS4",
      "Tim-Philipp Müller <tim centricular net>");
}

static void
gst_oss4_mixer_class_init (GstOss4MixerClass * klass)
{
  GstElementClass *element_class;
  GObjectClass *gobject_class;

  element_class = (GstElementClass *) klass;
  gobject_class = (GObjectClass *) klass;

  gobject_class->finalize = gst_oss4_mixer_finalize;
  gobject_class->set_property = gst_oss4_mixer_set_property;
  gobject_class->get_property = gst_oss4_mixer_get_property;

  /**
   * GstOss4Mixer:device
   *
   * OSS4 mixer device (e.g. /dev/oss/hdaudio0/mix0 or /dev/mixerN)
   *
   **/
  g_object_class_install_property (gobject_class, PROP_DEVICE,
      g_param_spec_string ("device", "Device",
          "OSS mixer device (e.g. /dev/oss/hdaudio0/mix0 or /dev/mixerN) "
          "(NULL = use first mixer device found)", DEFAULT_DEVICE,
144
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
145 146 147 148 149 150 151 152 153 154 155

  /**
   * GstOss4Mixer:device-name
   *
   * Human-readable name of the sound device. May be NULL if the device is
   * not open (ie. when the mixer is in NULL state)
   *
   **/
  g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
      g_param_spec_string ("device-name", "Device name",
          "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
156
          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
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

  element_class->change_state = GST_DEBUG_FUNCPTR (gst_oss4_mixer_change_state);
}

static void
gst_oss4_mixer_finalize (GObject * obj)
{
  GstOss4Mixer *mixer = GST_OSS4_MIXER (obj);

  g_free (mixer->device);

  G_OBJECT_CLASS (parent_class)->finalize (obj);
}

static void
gst_oss4_mixer_reset (GstOss4Mixer * mixer)
{
  mixer->fd = -1;
  mixer->need_update = TRUE;
  memset (&mixer->last_mixext, 0, sizeof (oss_mixext));
}

static void
gst_oss4_mixer_init (GstOss4Mixer * mixer, GstOss4MixerClass * g_class)
{
  mixer->device = g_strdup (DEFAULT_DEVICE);
  mixer->device_name = NULL;

  gst_oss4_mixer_reset (mixer);
}

static void
gst_oss4_mixer_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstOss4Mixer *mixer = GST_OSS4_MIXER (object);

  switch (prop_id) {
    case PROP_DEVICE:
      GST_OBJECT_LOCK (mixer);
      if (!GST_OSS4_MIXER_IS_OPEN (mixer)) {
        g_free (mixer->device);
        mixer->device = g_value_dup_string (value);
        /* unset any cached device-name */
        g_free (mixer->device_name);
        mixer->device_name = NULL;
      } else {
        g_warning ("%s: can't change \"device\" property while mixer is open",
            GST_OBJECT_NAME (mixer));
      }
      GST_OBJECT_UNLOCK (mixer);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_oss4_mixer_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
  GstOss4Mixer *mixer = GST_OSS4_MIXER (object);

  switch (prop_id) {
    case PROP_DEVICE:
      GST_OBJECT_LOCK (mixer);
      g_value_set_string (value, mixer->device);
      GST_OBJECT_UNLOCK (mixer);
      break;
    case PROP_DEVICE_NAME:
      GST_OBJECT_LOCK (mixer);
      /* If device is set, try to retrieve the name even if we're not open */
      if (mixer->fd == -1 && mixer->device != NULL) {
        if (gst_oss4_mixer_open (mixer, TRUE)) {
          g_value_set_string (value, mixer->device_name);
          gst_oss4_mixer_close (mixer);
        } else {
          g_value_set_string (value, mixer->device_name);
        }
      } else {
        g_value_set_string (value, mixer->device_name);
      }
      GST_OBJECT_UNLOCK (mixer);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static gboolean
gst_oss4_mixer_open (GstOss4Mixer * mixer, gboolean silent_errors)
{
  struct oss_mixerinfo mi = { 0, };
  gchar *device;

  g_return_val_if_fail (!GST_OSS4_MIXER_IS_OPEN (mixer), FALSE);

  if (mixer->device)
    device = g_strdup (mixer->device);
  else
    device = gst_oss4_audio_find_device (GST_OBJECT_CAST (mixer));

  /* desperate times, desperate measures */
  if (device == NULL)
    device = g_strdup ("/dev/mixer");

  GST_INFO_OBJECT (mixer, "Trying to open OSS4 mixer device '%s'", device);

  mixer->fd = open (device, O_RDWR, 0);
  if (mixer->fd < 0)
    goto open_failed;

  /* Make sure it's OSS4. If it's old OSS, let the old ossmixer handle it */
  if (!gst_oss4_audio_check_version (GST_OBJECT (mixer), mixer->fd))
    goto legacy_oss;

  GST_INFO_OBJECT (mixer, "Opened mixer device '%s', which is mixer %d",
      device, mi.dev);

  /* Get device name for currently open fd */
  mi.dev = -1;
  if (ioctl (mixer->fd, SNDCTL_MIXERINFO, &mi) == 0) {
    mixer->modify_counter = mi.modify_counter;
    if (mi.name[0] != '\0') {
      mixer->device_name = g_strdup (mi.name);
    }
  } else {
    mixer->modify_counter = 0;
  }

  if (mixer->device_name == NULL) {
    mixer->device_name = g_strdup ("Unknown");
  }
  GST_INFO_OBJECT (mixer, "device name = '%s'", mixer->device_name);

  mixer->open_device = device;

  return TRUE;

  /* ERRORS */
open_failed:
  {
    if (!silent_errors) {
      GST_ELEMENT_ERROR (mixer, RESOURCE, OPEN_READ_WRITE,
          (_("Could not open audio device for mixer control handling.")),
          GST_ERROR_SYSTEM);
305 306 307
    } else {
      GST_DEBUG_OBJECT (mixer, "open failed: %s (ignoring errors)",
          g_strerror (errno));
308 309 310 311 312 313 314 315 316
    }
    g_free (device);
    return FALSE;
  }
legacy_oss:
  {
    gst_oss4_mixer_close (mixer);
    if (!silent_errors) {
      GST_ELEMENT_ERROR (mixer, RESOURCE, OPEN_READ_WRITE,
317
          (_("Could not open audio device for mixer control handling. "
318 319
                  "This version of the Open Sound System is not supported by this "
                  "element.")), ("Try the 'ossmixer' element instead"));
320 321
    } else {
      GST_DEBUG_OBJECT (mixer, "open failed: legacy oss (ignoring errors)");
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
    }
    g_free (device);
    return FALSE;
  }
}

static void
gst_oss4_mixer_control_free (GstOss4MixerControl * mc)
{
  g_list_free (mc->children);
  g_list_free (mc->mute_group);
  g_free (mc->enum_vals);
  memset (mc, 0, sizeof (GstOss4MixerControl));
  g_free (mc);
}

static void
gst_oss4_mixer_free_tracks (GstOss4Mixer * mixer)
{
  g_list_foreach (mixer->tracks, (GFunc) g_object_unref, NULL);
  g_list_free (mixer->tracks);
  mixer->tracks = NULL;

  g_list_foreach (mixer->controls, (GFunc) gst_oss4_mixer_control_free, NULL);
  g_list_free (mixer->controls);
  mixer->controls = NULL;
}

static void
gst_oss4_mixer_close (GstOss4Mixer * mixer)
{
  g_free (mixer->device_name);
  mixer->device_name = NULL;

  g_free (mixer->open_device);
  mixer->open_device = NULL;

  gst_oss4_mixer_free_tracks (mixer);

  if (mixer->fd != -1) {
    close (mixer->fd);
    mixer->fd = -1;
  }

  gst_oss4_mixer_reset (mixer);
}

static void
gst_oss4_mixer_watch_process_changes (GstOss4Mixer * mixer)
{
  GList *c, *t, *tracks = NULL;

  GST_INFO_OBJECT (mixer, "mixer interface or control changed");

  /* this is all with the mixer object lock held */

  /* we go through the list backwards so we can bail out faster when the entire
   * interface needs to be rebuilt */
  for (c = g_list_last (mixer->controls); c != NULL; c = c->prev) {
    GstOss4MixerControl *mc = c->data;
    oss_mixer_value ossval = { 0, };

    mc->changed = FALSE;
    mc->list_changed = FALSE;

    /* not interested in controls we don't expose in the mixer interface */
    if (!mc->used)
      continue;

    /* don't try to read a value from controls that don't have one */
    if (mc->mixext.type == MIXT_DEVROOT || mc->mixext.type == MIXT_GROUP)
      continue;

    /* is this an enum control whose list may change? */
    if (mc->mixext.type == MIXT_ENUM && mc->enum_version != 0) {
      if (gst_oss4_mixer_enum_control_update_enum_list (mixer, mc))
        mc->list_changed = TRUE;
    }

    ossval.dev = mc->mixext.dev;
    ossval.ctrl = mc->mixext.ctrl;
    ossval.timestamp = mc->mixext.timestamp;

    if (ioctl (mixer->fd, SNDCTL_MIX_READ, &ossval) == -1) {
      if (errno == EIDRM || errno == EFAULT) {
        GST_DEBUG ("%s has disappeared", mc->mixext.extname);
        goto mixer_changed;
      }
      GST_WARNING_OBJECT (mixer, "MIX_READ failed: %s", g_strerror (errno));
      /* just ignore, move on to next one */
      continue;
    }

    if (ossval.value == mc->last_val) { /* no change */
      /* GST_LOG_OBJECT (mixer, "%s hasn't changed", mc->mixext.extname); */
      continue;
    }

    mc->last_val = ossval.value;
    GST_LOG_OBJECT (mixer, "%s changed value to %u 0x%08x",
        mc->mixext.extname, ossval.value, ossval.value);
    mc->changed = TRUE;
  }

  /* copy list and take track refs, so we can safely drop the object lock,
   * which we need to do to be able to post messages on the bus */
  tracks = g_list_copy (mixer->tracks);
  g_list_foreach (tracks, (GFunc) g_object_ref, NULL);

  GST_OBJECT_UNLOCK (mixer);

  /* since we don't know (or want to know exactly) which controls belong to
   * which track, we just go through the tracks one-by-one now and make them
   * check themselves if any of their controls have changed and which messages
   * to post on the bus as a result */
  for (t = tracks; t != NULL; t = t->next) {
    GstMixerTrack *track = t->data;

    if (GST_IS_OSS4_MIXER_SLIDER (track)) {
      gst_oss4_mixer_slider_process_change_unlocked (track);
    } else if (GST_IS_OSS4_MIXER_SWITCH (track)) {
      gst_oss4_mixer_switch_process_change_unlocked (track);
    } else if (GST_IS_OSS4_MIXER_ENUM (track)) {
      gst_oss4_mixer_enum_process_change_unlocked (track);
    }

    g_object_unref (track);
  }
  g_list_free (tracks);

  GST_OBJECT_LOCK (mixer);
  return;

mixer_changed:
  {
    GST_OBJECT_UNLOCK (mixer);
    gst_mixer_mixer_changed (GST_MIXER (mixer));
    GST_OBJECT_LOCK (mixer);
    return;
  }
}

/* This thread watches the mixer for changes in a somewhat inefficient way
 * (running an ioctl every half second or so). This is still better and
 * cheaper than apps polling all tracks for changes a few times a second
 * though. Needs more thought. There's probably (hopefully) a way to get
 * notifications via the fd directly somehow. */
static gpointer
gst_oss4_mixer_watch_thread (gpointer thread_data)
{
  GstOss4Mixer *mixer = GST_OSS4_MIXER_CAST (thread_data);

  GST_DEBUG_OBJECT (mixer, "watch thread running");

  GST_OBJECT_LOCK (mixer);
  while (!mixer->watch_shutdown) {
    oss_mixerinfo mi = { 0, };
    GTimeVal tv;

    mi.dev = -1;
    if (ioctl (mixer->fd, SNDCTL_MIXERINFO, &mi) == 0) {
      if (mixer->modify_counter != mi.modify_counter) {
        /* GST_LOG ("processing changes"); */
        gst_oss4_mixer_watch_process_changes (mixer);
        mixer->modify_counter = mi.modify_counter;
      } else {
        /* GST_LOG ("no changes"); */
      }
    } else {
      GST_WARNING_OBJECT (mixer, "MIXERINFO failed: %s", g_strerror (errno));
    }

    /* we could move the _get_current_time out of the loop and just do the
     * add in ever iteration, which would be less exact, but who cares */
    g_get_current_time (&tv);
    g_time_val_add (&tv, GST_OSS4_MIXER_WATCH_INTERVAL * 1000);
498 499
    (void) g_cond_timed_wait (mixer->watch_cond, GST_OBJECT_GET_LOCK (mixer),
        &tv);
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
  }
  GST_OBJECT_UNLOCK (mixer);

  GST_DEBUG_OBJECT (mixer, "watch thread done");
  gst_object_unref (mixer);
  return NULL;
}

/* call with object lock held */
static void
gst_oss4_mixer_wake_up_watch_task (GstOss4Mixer * mixer)
{
  GST_LOG_OBJECT (mixer, "signalling watch thread to wake up");
  g_cond_signal (mixer->watch_cond);
}

static void
gst_oss4_mixer_stop_watch_task (GstOss4Mixer * mixer)
{
  if (mixer->watch_thread) {
    GST_OBJECT_LOCK (mixer);
    mixer->watch_shutdown = TRUE;
    GST_LOG_OBJECT (mixer, "signalling watch thread to stop");
    g_cond_signal (mixer->watch_cond);
    GST_OBJECT_UNLOCK (mixer);
    GST_LOG_OBJECT (mixer, "waiting for watch thread to join");
    g_thread_join (mixer->watch_thread);
    GST_DEBUG_OBJECT (mixer, "watch thread stopped");
    mixer->watch_thread = NULL;
  }

  if (mixer->watch_cond) {
    g_cond_free (mixer->watch_cond);
    mixer->watch_cond = NULL;
  }
}

static void
gst_oss4_mixer_start_watch_task (GstOss4Mixer * mixer)
{
  GError *err = NULL;

  mixer->watch_cond = g_cond_new ();
  mixer->watch_shutdown = FALSE;

  mixer->watch_thread = g_thread_create (gst_oss4_mixer_watch_thread,
      gst_object_ref (mixer), TRUE, &err);

  if (mixer->watch_thread == NULL) {
    GST_ERROR_OBJECT (mixer, "Could not create watch thread: %s", err->message);
    g_cond_free (mixer->watch_cond);
    mixer->watch_cond = NULL;
    g_error_free (err);
  }
}

static GstStateChangeReturn
gst_oss4_mixer_change_state (GstElement * element, GstStateChange transition)
{
  GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
  GstOss4Mixer *mixer = GST_OSS4_MIXER (element);

  switch (transition) {
    case GST_STATE_CHANGE_NULL_TO_READY:
      if (!gst_oss4_mixer_open (mixer, FALSE))
        return GST_STATE_CHANGE_FAILURE;
      gst_oss4_mixer_start_watch_task (mixer);
      break;
    default:
      break;
  }

  ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
  if (ret == GST_STATE_CHANGE_FAILURE)
    return ret;

  switch (transition) {
    case GST_STATE_CHANGE_READY_TO_NULL:
      gst_oss4_mixer_stop_watch_task (mixer);
      gst_oss4_mixer_close (mixer);
      break;
    default:
      break;
  }

  return ret;
}

/* === GstMixer interface === */

static inline gboolean
gst_oss4_mixer_contains_track (GstMixer * mixer, GstMixerTrack * track)
{
  return (g_list_find (GST_OSS4_MIXER (mixer)->tracks, track) != NULL);
}

static inline gboolean
gst_oss4_mixer_contains_options (GstMixer * mixer, GstMixerOptions * options)
{
  return (g_list_find (GST_OSS4_MIXER (mixer)->tracks, options) != NULL);
}

static void
gst_oss4_mixer_post_mixer_changed_msg (GstOss4Mixer * mixer)
{
  /* only post mixer-changed message once */
  if (!mixer->need_update) {
    gst_mixer_mixer_changed (GST_MIXER (mixer));
    mixer->need_update = TRUE;
  }
}

/* call with mixer object lock held to serialise ioctl */
gboolean
gst_oss4_mixer_get_control_val (GstOss4Mixer * mixer, GstOss4MixerControl * mc,
    int *val)
{
  oss_mixer_value ossval = { 0, };

  if (GST_OBJECT_TRYLOCK (mixer)) {
    GST_ERROR ("must be called with mixer lock held");
    GST_OBJECT_UNLOCK (mixer);
  }

  ossval.dev = mc->mixext.dev;
  ossval.ctrl = mc->mixext.ctrl;
  ossval.timestamp = mc->mixext.timestamp;

  if (ioctl (mixer->fd, SNDCTL_MIX_READ, &ossval) == -1) {
    if (errno == EIDRM) {
      GST_DEBUG_OBJECT (mixer, "MIX_READ failed: mixer interface has changed");
      gst_oss4_mixer_post_mixer_changed_msg (mixer);
    } else {
      GST_WARNING_OBJECT (mixer, "MIX_READ failed: %s", g_strerror (errno));
    }
    *val = 0;
    mc->last_val = 0;
    return FALSE;
  }

  *val = ossval.value;
  mc->last_val = ossval.value;
  GST_LOG_OBJECT (mixer, "got value 0x%08x from %s)", *val, mc->mixext.extname);
  return TRUE;
}

/* call with mixer object lock held to serialise ioctl */
gboolean
gst_oss4_mixer_set_control_val (GstOss4Mixer * mixer, GstOss4MixerControl * mc,
    int val)
{
  oss_mixer_value ossval = { 0, };

  ossval.dev = mc->mixext.dev;
  ossval.ctrl = mc->mixext.ctrl;
  ossval.timestamp = mc->mixext.timestamp;
  ossval.value = val;

  if (GST_OBJECT_TRYLOCK (mixer)) {
    GST_ERROR ("must be called with mixer lock held");
    GST_OBJECT_UNLOCK (mixer);
  }

  if (ioctl (mixer->fd, SNDCTL_MIX_WRITE, &ossval) == -1) {
    if (errno == EIDRM) {
      GST_LOG_OBJECT (mixer, "MIX_WRITE failed: mixer interface has changed");
      gst_oss4_mixer_post_mixer_changed_msg (mixer);
    } else {
      GST_WARNING_OBJECT (mixer, "MIX_WRITE failed: %s", g_strerror (errno));
    }
    return FALSE;
  }

  mc->last_val = val;
  GST_LOG_OBJECT (mixer, "set value 0x%08x on %s", val, mc->mixext.extname);
  return TRUE;
}

678
#if 0
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
static gchar *
gst_oss4_mixer_control_get_pretty_name (GstOss4MixerControl * mc)
{
  gchar *name;

  const gchar *name, *u;

  /* "The id field is the original name given by the driver when it called
   * mixer_ext_create_control. This name can be used by fully featured GUI
   * mixers. However this name should be downshifted and cut before the last
   * underscore ("_") to get the proper name. For example mixer control name
   * created as "MYDRV_MAINVOL" will become just "mainvol" after this
   * transformation." */
  name = mc->mixext.extname;
  u = MAX (strrchr (name, '_'), strrchr (name, '.'));
  if (u != NULL)
    name = u + 1;

  /* maybe capitalize the first letter? */
  return g_ascii_strdown (name, -1);
  /* the .id thing doesn't really seem to work right, ie. for some sliders
   * it's just '-' so you have to use the name of the parent control etc.
   * let's not use it for now, much too painful. */
  if (g_str_has_prefix (mc->mixext.extname, "misc."))
    name = g_strdup (mc->mixext.extname + 5);
  else
    name = g_strdup (mc->mixext.extname);
  /* chop off trailing digit (only one for now) */
  if (strlen (name) > 0 && g_ascii_isdigit (name[strlen (name) - 1]))
    name[strlen (name) - 1] = '\0';
  g_strdelimit (name, ".", ' ');
  return name;
}
712
#endif
713

714 715 716
/* these translations are a bit ad-hoc and horribly incomplete; it is not
 * really going to work this way with all the different chipsets and drivers.
 * We also use these for translating option values. */
717 718 719 720
static struct
{
  const gchar oss_name[32];
  const gchar *label;
721
} labels[] = {
722
  {
723 724
  "volume", N_("Volume")}, {
  "master", N_("Master")}, {
725 726
  "front", N_("Front")}, {
  "rear", N_("Rear")}, {
727 728 729 730
  "headphones", N_("Headphones")}, {
  "center", N_("Center")}, {
  "lfe", N_("LFE")}, {
  "surround", N_("Surround")}, {
731
  "side", N_("Side")}, {
732 733 734 735 736 737 738 739 740 741
  "speaker", N_("Built-in Speaker")}, {
  "aux1-out", N_("AUX 1 Out")}, {
  "aux2-out", N_("AUX 2 Out")}, {
  "aux-out", N_("AUX Out")}, {
  "bass", N_("Bass")}, {
  "treble", N_("Treble")}, {
  "3d-depth", N_("3D Depth")}, {
  "3d-center", N_("3D Center")}, {
  "3d-enhance", N_("3D Enhance")}, {
  "phone", N_("Telephone")}, {
742
  "mic", N_("Microphone")}, {
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
  "line-out", N_("Line Out")}, {
  "line-in", N_("Line In")}, {
  "linein", N_("Line In")}, {
  "cd", N_("Internal CD")}, {
  "video", N_("Video In")}, {
  "aux1-in", N_("AUX 1 In")}, {
  "aux2-in", N_("AUX 2 In")}, {
  "aux-in", N_("AUX In")}, {
  "pcm", N_("PCM")}, {
  "record-gain", N_("Record Gain")}, {
  "igain", N_("Record Gain")}, {
  "ogain", N_("Output Gain")}, {
  "micboost", N_("Microphone Boost")}, {
  "loopback", N_("Loopback")}, {
  "diag", N_("Diagnostic")}, {
  "loudness", N_("Bass Boost")}, {
  "outputs", N_("Playback Ports")}, {
760
  "input", N_("Input")}, {
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
  "inputs", N_("Record Source")}, {
  "record-source", N_("Record Source")}, {
  "monitor-source", N_("Monitor Source")}, {
  "beep", N_("Keyboard Beep")}, {
  "monitor-gain", N_("Monitor")}, {
  "stereo-simulate", N_("Simulate Stereo")}, {
  "stereo", N_("Stereo")}, {
  "multich", N_("Surround Sound")}, {
  "mic-gain", N_("Microphone Gain")}, {
  "speaker-source", N_("Speaker Source")}, {
  "mic-source", N_("Microphone Source")}, {
  "jack", N_("Jack")}, {
  "center/lfe", N_("Center / LFE")}, {
  "stereo-mix", N_("Stereo Mix")}, {
  "mono-mix", N_("Mono Mix")}, {
  "input-mix", N_("Input Mix")}, {
  "spdif-in", N_("SPDIF In")}, {
  "spdif-out", N_("SPDIF Out")}, {
  "mic1", N_("Microphone 1")}, {
  "mic2", N_("Microphone 2")}, {
  "digital-out", N_("Digital Out")}, {
  "digital-in", N_("Digital In")}, {
  "hdmi", N_("HDMI")}, {
  "modem", N_("Modem")}, {
  "handset", N_("Handset")}, {
  "other", N_("Other")}, {
  "stereo", N_("Stereo")}, {
  "none", N_("None")}, {
  "on", N_("On")}, {
  "off", N_("Off")}, {
  "mute", N_("Mute")}, {
  "fast", N_("Fast")}, {
793
    /* TRANSLATORS: "Very Low" is a quality setting here */
794
  "very-low", N_("Very Low")}, {
795
    /* TRANSLATORS: "Low" is a quality setting here */
796
  "low", N_("Low")}, {
797
    /* TRANSLATORS: "Medium" is a quality setting here */
798
  "medium", N_("Medium")}, {
799
    /* TRANSLATORS: "High" is a quality setting here */
800
  "high", N_("High")}, {
801
    /* TRANSLATORS: "Very High" is a quality setting here */
802 803
  "very-high", N_("Very High")}, {
  "high+", N_("Very High")}, {
804
    /* TRANSLATORS: "Production" is a quality setting here */
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
  "production", N_("Production")}, {
  "fp-mic", N_("Front Panel Microphone")}, {
  "fp-linein", N_("Front Panel Line In")}, {
  "fp-headphones", N_("Front Panel Headphones")}, {
  "fp-lineout", N_("Front Panel Line Out")}, {
  "green", N_("Green Connector")}, {
  "pink", N_("Pink Connector")}, {
  "blue", N_("Blue Connector")}, {
  "white", N_("White Connector")}, {
  "black", N_("Black Connector")}, {
  "gray", N_("Gray Connector")}, {
  "orange", N_("Orange Connector")}, {
  "red", N_("Red Connector")}, {
  "yellow", N_("Yellow Connector")}, {
  "fp-green", N_("Green Front Panel Connector")}, {
  "fp-pink", N_("Pink Front Panel Connector")}, {
  "fp-blue", N_("Blue Front Panel Connector")}, {
  "fp-white", N_("White Front Panel Connector")}, {
  "fp-black", N_("Black Front Panel Connector")}, {
  "fp-gray", N_("Gray Front Panel Connector")}, {
  "fp-orange", N_("Orange Front Panel Connector")}, {
  "fp-red", N_("Red Front Panel Connector")}, {
  "fp-yellow", N_("Yellow Front Panel Connector")}, {
  "spread", N_("Spread Output")}, {
  "downmix", N_("Downmix")},
      /* FIXME translate Audigy NX USB labels) */
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
/*
  { "rec.src", N_("Record Source") },
  { "output.mute", N_("Mute output") }
 headph (Controller group)
   headph.src (Enumeration control)
   headph.mute (On/Off switch)
 digital2 (Controller group)
   digital2.src (Enumeration control)
   digital2.mute (On/Off switch)
 digital (Controller group)
   digital.mute1 (On/Off switch)
   digital.vol (Controller group)
     digital.vol.front (Stereo slider (0-255))
     digital.vol.surr (Stereo slider (0-255))
     digital.vol.c/l (Stereo slider (0-255))
     digital.vol.center (Stereo slider (0-255))
   digital.mute2 (On/Off switch)
   digital.vol (Stereo slider (0-255))
 line (Controller group)
   line.mute (On/Off switch)
   line.vol (Stereo slider (0-255))
 play-altset (Enumeration control)
 rec-altset (Enumeration control)
*/
};

/* Decent i18n is pretty much impossible with OSS's way of providing us with
 * mixer labels (and the fact that they are pretty much random), but that
 * doesn't mean we shouldn't at least try. */
860
static gchar *
861 862
gst_oss4_mixer_control_get_translated_name (GstOss4MixerControl * mc)
{
863 864 865
  gchar name[128] = { 0, };
  gchar vmix_str[32] = { '\0', };
  gchar *ptr;
866
  int dummy, i;
867
  int num = -1;
868
  gboolean function_suffix = FALSE;
869 870 871 872

  /* main virtual mixer controls (we hide the stream volumes) */
  if (sscanf (mc->mixext.extname, "vmix%d-%32c", &dummy, vmix_str) == 2) {
    if (strcmp (vmix_str, "src") == 0)
873
      return g_strdup (_("Virtual Mixer Input"));
874
    else if (strcmp (vmix_str, "vol") == 0)
875
      return g_strdup (_("Virtual Mixer Output"));
876
    else if (strcmp (vmix_str, "channels") == 0)
877
      return g_strdup (_("Virtual Mixer Channels"));
878 879
  }

880
  g_strlcpy (name, mc->mixext.extname, sizeof (name));
881

882 883 884 885 886
  /* we deal with either "connector." or "jack." */
  if ((g_str_has_prefix (name, "connector.")) ||
      (g_str_has_prefix (name, "jack."))) {
    ptr = strchr (mc->mixext.extname, '.');
    ptr++;
887
    g_strlcpy (name, ptr, sizeof (name));
888
  }
889

890 891
  /* special handling for jack retasking suffixes */
  if (g_str_has_suffix (name, ".function") || g_str_has_suffix (name, ".mode")) {
892
    function_suffix = TRUE;
893 894 895
    ptr = strrchr (name, '.');
    *ptr = 0;
  }
896

897 898 899 900
  /* parse off trailing numbers */
  i = strlen (name);
  while ((i > 0) && (g_ascii_isdigit (name[i - 1]))) {
    i--;
901
  }
902 903 904 905 906 907 908 909 910 911 912 913
  /* the check catches the case where the control name is just a number */
  if ((i > 0) && (name[i] != '\0')) {
    num = atoi (name + i);
    name[i] = '\0';
  }

  /* look for a match, progressively skipping '.' delimited prefixes as we go */
  ptr = name;
  do {
    if (*ptr == '.')
      ptr++;
    for (i = 0; i < G_N_ELEMENTS (labels); ++i) {
914
      if (g_ascii_strcasecmp (ptr, labels[i].oss_name) == 0) {
915 916
        g_strlcpy (name, _(labels[i].label), sizeof (name));
        goto append_suffixes;
917 918 919
      }
    }
  } while ((ptr = strchr (ptr, '.')) != NULL);
920

921
  /* failing that, just replace periods with spaces */
922
  g_strdelimit (name, ".", ' ');
923 924 925 926

append_suffixes:
  if (num > -1) {
    if (function_suffix) {
927
      /* TRANSLATORS: name + number of a volume mixer control */
928 929
      return g_strdup_printf (_("%s %d Function"), name, num);
    } else {
930
      return g_strdup_printf ("%s %d", name, num);
931 932 933
    }
  } else {
    if (function_suffix) {
934
      /* TRANSLATORS: name of a volume mixer control */
935 936 937 938 939
      return g_strdup_printf (_("%s Function"), name);
    } else {
      return g_strdup (name);
    }
  }
940 941
}

942 943
static const gchar *
gst_oss4_mixer_control_get_translated_option (const gchar * name)
944 945 946
{
  int i;
  for (i = 0; i < G_N_ELEMENTS (labels); ++i) {
947
    if (g_ascii_strcasecmp (name, labels[i].oss_name) == 0) {
948
      return _(labels[i].label);
949 950 951
    }
  }
  return (name);
952 953
}

954
#ifndef GST_DISABLE_GST_DEBUG
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
static const gchar *
mixer_ext_type_get_name (gint type)
{
  switch (type) {
    case MIXT_DEVROOT:
      return "Device root entry";
    case MIXT_GROUP:
      return "Controller group";
    case MIXT_ONOFF:
      return "On/Off switch";
    case MIXT_ENUM:
      return "Enumeration control";
    case MIXT_MONOSLIDER:
      return "Mono slider (0-255)";
    case MIXT_STEREOSLIDER:
      return "Stereo slider (0-255)";
    case MIXT_MESSAGE:
      return "Textual message"; /* whatever this is */
    case MIXT_MONOVU:
      return "Mono VU meter value";
    case MIXT_STEREOVU:
      return "Stereo VU meter value";
    case MIXT_MONOPEAK:
      return "Mono VU meter peak value";
    case MIXT_STEREOPEAK:
      return "Stereo VU meter peak value";
    case MIXT_RADIOGROUP:
      return "Radio button group";
    case MIXT_MARKER:          /* Separator between normal and extension entries */
      return "Separator";
    case MIXT_VALUE:
      return "Decimal value entry";
    case MIXT_HEXVALUE:
      return "Hex value entry";
    case MIXT_SLIDER:
      return "Mono slider (31-bit value range)";
    case MIXT_3D:
      return "3D";              /* what's this? */
    case MIXT_MONOSLIDER16:
      return "Mono slider (0-32767)";
    case MIXT_STEREOSLIDER16:
      return "Stereo slider (0-32767)";
    case MIXT_MUTE:
      return "Mute switch";
    default:
      break;
  }
  return "unknown";
}
1004
#endif /* GST_DISABLE_GST_DEBUG */
1005

1006
#ifndef GST_DISABLE_GST_DEBUG
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
static const gchar *
mixer_ext_flags_get_string (gint flags)
{
  struct
  {
    gint flag;
    gchar nick[16];
  } all_flags[] = {
    /* first the important ones */
    {
    MIXF_MAINVOL, "MAINVOL"}, {
    MIXF_PCMVOL, "PCMVOL"}, {
    MIXF_RECVOL, "RECVOL"}, {
    MIXF_MONVOL, "MONVOL"}, {
    MIXF_DESCR, "DESCR"},
        /* now the rest in the right order */
    {
    MIXF_READABLE, "READABLE"}, {
    MIXF_WRITEABLE, "WRITABLE"}, {
    MIXF_POLL, "POLL"}, {
    MIXF_HZ, "HZ"}, {
    MIXF_STRING, "STRING"}, {
    MIXF_DYNAMIC, "DYNAMIC"}, {
    MIXF_OKFAIL, "OKFAIL"}, {
    MIXF_FLAT, "FLAT"}, {
    MIXF_LEGACY, "LEGACY"}, {
    MIXF_CENTIBEL, "CENTIBEL"}, {
    MIXF_DECIBEL, "DECIBEL"}, {
    MIXF_WIDE, "WIDE"}
  };
  GString *s;
  GQuark q;
  gint i;

  if (flags == 0)
    return "None";

  s = g_string_new ("");
  for (i = 0; i < G_N_ELEMENTS (all_flags); ++i) {
    if ((flags & all_flags[i].flag)) {
      if (s->len > 0)
        g_string_append (s, " | ");
      g_string_append (s, all_flags[i].nick);
      flags &= ~all_flags[i].flag;      /* unset */
    }
  }

  /* unknown flags? */
  if (flags != 0) {
    if (s->len > 0)
      g_string_append (s, " | ");
    g_string_append (s, "???");
  }

  /* we'll just put it into the global quark table (cheeky, eh?) */
  q = g_quark_from_string (s->str);
  g_string_free (s, TRUE);

  return g_quark_to_string (q);
}
1067
#endif /* GST_DISABLE_GST_DEBUG */
1068

1069
#ifndef GST_DISABLE_GST_DEBUG
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
static void
gst_oss4_mixer_control_dump_tree (GstOss4MixerControl * mc, gint depth)
{
  GList *c;
  gchar spaces[64];
  gint i;

  depth = MIN (sizeof (spaces) - 1, depth);
  for (i = 0; i < depth; ++i)
    spaces[i] = ' ';
  spaces[i] = '\0';

  GST_LOG ("%s%s (%s)", spaces, mc->mixext.extname,
      mixer_ext_type_get_name (mc->mixext.type));
  for (c = mc->children; c != NULL; c = c->next) {
    GstOss4MixerControl *child_mc = (GstOss4MixerControl *) c->data;

    gst_oss4_mixer_control_dump_tree (child_mc, depth + 2);
  }
}
1090
#endif /* GST_DISABLE_GST_DEBUG */
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167

static GList *
gst_oss4_mixer_get_controls (GstOss4Mixer * mixer)
{
  GstOss4MixerControl *root_mc = NULL;
  oss_mixerinfo mi = { 0, };
  GList *controls = NULL;
  GList *l;
  guint i;

  /* Get info for currently open fd */
  mi.dev = -1;
  if (ioctl (mixer->fd, SNDCTL_MIXERINFO, &mi) == -1)
    goto no_mixerinfo;

  if (mi.nrext <= 0) {
    GST_DEBUG ("mixer %s has no controls", mi.id);
    return NULL;
  }

  GST_INFO ("Reading controls for mixer %s", mi.id);

  for (i = 0; i < mi.nrext; ++i) {
    GstOss4MixerControl *mc;
    oss_mixext mix_ext = { 0, };

    mix_ext.dev = mi.dev;
    mix_ext.ctrl = i;

    if (ioctl (mixer->fd, SNDCTL_MIX_EXTINFO, &mix_ext) == -1) {
      GST_DEBUG ("SNDCTL_MIX_EXTINFO failed on mixer %s, control %d: %s",
          mi.id, i, g_strerror (errno));
      continue;
    }

    /* if this is the last one, save it for is-interface-up-to-date checking */
    if (i == mi.nrext)
      mixer->last_mixext = mix_ext;

    mc = g_new0 (GstOss4MixerControl, 1);
    mc->mixext = mix_ext;

    /* both control_no and desc fields are pretty useless, ie. not always set,
     * if ever, so not listed here */
    GST_INFO ("Control %d", mix_ext.ctrl);
    GST_INFO ("  name   : %s", mix_ext.extname);
    GST_INFO ("  type   : %s (%d)", mixer_ext_type_get_name (mix_ext.type),
        mix_ext.type);
    GST_INFO ("  flags  : %s (0x%04x)",
        mixer_ext_flags_get_string (mix_ext.flags), mix_ext.flags);
    GST_INFO ("  parent : %d", mix_ext.parent);

    if (!MIXEXT_IS_ROOT (mix_ext)) {
      /* find parent (we assume it comes in the list before the child) */
      for (l = controls; l != NULL; l = l->next) {
        GstOss4MixerControl *parent_mc = (GstOss4MixerControl *) l->data;

        if (parent_mc->mixext.ctrl == mix_ext.parent) {
          mc->parent = parent_mc;
          parent_mc->children = g_list_append (parent_mc->children, mc);
          break;
        }
      }
      if (mc->parent == NULL) {
        GST_ERROR_OBJECT (mixer, "couldn't find parent for control %d", i);
        g_free (mc);
        continue;
      }
    } else if (root_mc == NULL) {
      root_mc = mc;
    } else {
      GST_WARNING_OBJECT (mixer, "two root controls?!");
    }

    controls = g_list_prepend (controls, mc);
  }

1168
#ifndef GST_DISABLE_GST_DEBUG
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
  gst_oss4_mixer_control_dump_tree (root_mc, 0);
#endif

  return g_list_reverse (controls);

/* ERRORS */
no_mixerinfo:
  {
    GST_WARNING ("SNDCTL_MIXERINFO failed on mixer device %s: %s", mi.id,
        g_strerror (errno));
    return NULL;
  }
}

static void
gst_oss4_mixer_controls_guess_master (GstOss4Mixer * mixer,
    const GList * controls)
{
  GstOss4MixerControl *master_mc = NULL;
  const GList *l;

  for (l = controls; l != NULL; l = l->next) {
    GstOss4MixerControl *mc = (GstOss4MixerControl *) l->data;

    /* do we need to check if it's a slider type here? */
1194
    if ((mc->mixext.flags & MIXF_PCMVOL)) {
1195
      GST_INFO_OBJECT (mixer, "First PCM control: %s", mc->mixext.extname);
1196 1197
      master_mc = mc;
      break;
1198 1199
    }

1200 1201 1202 1203 1204
    if (((mc->mixext.flags & MIXF_MAINVOL)) && master_mc == NULL) {
      GST_INFO_OBJECT (mixer, "First main volume control: %s",
          mc->mixext.extname);
      master_mc = mc;
    }
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
  }

  if (master_mc != NULL)
    master_mc->is_master = TRUE;
}

/* type: -1 for all types, otherwise just return siblings with requested type */
static GList *
gst_oss4_mixer_control_get_siblings (GstOss4MixerControl * mc, gint type)
{
  GList *s, *siblings = NULL;

  if (mc->parent == NULL)
    return NULL;

  for (s = mc->parent->children; s != NULL; s = s->next) {
    GstOss4MixerControl *sibling = (GstOss4MixerControl *) s->data;

    if (mc != sibling && (type < 0 || sibling->mixext.type == type))
      siblings = g_list_append (siblings, sibling);
  }

  return siblings;
}

static void
gst_oss4_mixer_controls_find_sliders (GstOss4Mixer * mixer,
    const GList * controls)
{
  const GList *l;

  for (l = controls; l != NULL; l = l->next) {
    GstOss4MixerControl *mc = (GstOss4MixerControl *) l->data;
    GList *s, *siblings;

    if (!MIXEXT_IS_SLIDER (mc->mixext) || mc->parent == NULL || mc->used)
      continue;

    mc->is_slider = TRUE;
    mc->used = TRUE;

    siblings = gst_oss4_mixer_control_get_siblings (mc, -1);

    /* Note: the names can be misleading and may not reflect the actual
     * hierarchy of the controls, e.g. it's possible that a slider is called
     * connector.green and the mute control then connector.green.mute, but
     * both controls are in fact siblings and both children of the group
     * 'green' instead of mute being a child of connector.green as the naming
     * would seem to suggest */
    GST_LOG ("Slider: %s, parent=%s, %d siblings", mc->mixext.extname,
        mc->parent->mixext.extname, g_list_length (siblings));

    for (s = siblings; s != NULL; s = s->next) {
      GstOss4MixerControl *sibling = (GstOss4MixerControl *) s->data;

      GST_LOG ("    %s (%s)", sibling->mixext.extname,
          mixer_ext_type_get_name (sibling->mixext.type));

      if (sibling->mixext.type == MIXT_MUTE ||
          g_str_has_suffix (sibling->mixext.extname, ".mute")) {
        /* simple case: slider with single mute sibling. We assume the .mute
         * suffix in the name won't change - can't really do much else anyway */
        if (sibling->mixext.type == MIXT_ONOFF ||
            sibling->mixext.type == MIXT_MUTE) {
          GST_LOG ("    mute control for %s is %s", mc->mixext.extname,
              sibling->mixext.extname);
          mc->mute = sibling;
          sibling->used = TRUE;
        }
        /* a group of .mute controls. We assume they are all switches here */
        if (sibling->mixext.type == MIXT_GROUP) {
          GList *m;

          for (m = sibling->children; m != NULL; m = m->next) {
            GstOss4MixerControl *grouped_sibling = m->data;

            if (grouped_sibling->mixext.type == MIXT_ONOFF ||
                grouped_sibling->mixext.type == MIXT_MUTE) {
              GST_LOG ("    %s is grouped mute control for %s",
                  grouped_sibling->mixext.extname, mc->mixext.extname);
              mc->mute_group = g_list_append (mc->mute_group, grouped_sibling);
            }
          }

          GST_LOG ("    %s has a group of %d mute controls",
              mc->mixext.extname, g_list_length (mc->mute_group));

          /* we don't mark the individual mute controls as used, only the
           * group control, as we still want individual switches for the
           * individual controls */
          sibling->used = TRUE;
        }
      }
    }
    g_list_free (siblings);
  }
}

/* should be called with the mixer object lock held because of the ioctl;
 * returns TRUE if the list was read the first time or modified */
static gboolean
gst_oss4_mixer_enum_control_update_enum_list (GstOss4Mixer * mixer,
    GstOss4MixerControl * mc)
{
  oss_mixer_enuminfo ei = { 0, };
  guint num_existing = 0;
  int i;

  /* count and existing values */
  while (mc->enum_vals != NULL && mc->enum_vals[num_existing] != 0)
    ++num_existing;

  ei.dev = mc->mixext.dev;
  ei.ctrl = mc->mixext.ctrl;

  /* if we have create a generic list with numeric IDs already and the
   * number of values hasn't changed, then there's not much to do here */
  if (mc->no_list && mc->enum_vals != NULL &&
      num_existing == mc->mixext.maxvalue) {
    return FALSE;
  }

  /* if we have a list and it doesn't change, there's nothing to do either */
  if (mc->enum_vals != NULL && mc->enum_version == 0)
    return FALSE;

  if (ioctl (mixer->fd, SNDCTL_MIX_ENUMINFO, &ei) == -1) {
    g_free (mc->enum_vals);
    mc->enum_vals = g_new0 (GQuark, mc->mixext.maxvalue + 1);

    GST_DEBUG ("no enum info available, creating numeric values from 0-%d",
        mc->mixext.maxvalue - 1);

    /* "It is possible that some enum controls don't have any name list
     * available. In this case the application should automatically generate
     * list of numbers (0 to N-1)" */
    for (i = 0; i < mc->mixext.maxvalue; ++i) {
      gchar num_str[8];

      g_snprintf (num_str, sizeof (num_str), "%d", i);
      mc->enum_vals[i] = g_quark_from_string (num_str);
    }
    mc->enum_version = 0;       /* the only way to change is via maxvalue */
  } else {
    /* old list same as current list? */
    if (mc->enum_vals != NULL && mc->enum_version == ei.version)
      return FALSE;

    /* no list yet, or the list has changed */
    GST_LOG ("%s", (mc->enum_vals) ? "enum list has changed" : "reading list");
    if (ei.nvalues != mc->mixext.maxvalue) {
      GST_WARNING_OBJECT (mixer, "Enum: %s, nvalues %d != maxvalue %d",
          mc->mixext.extname, ei.nvalues, mc->mixext.maxvalue);
      mc->mixext.maxvalue = MIN (ei.nvalues, mc->mixext.maxvalue);
    }

    mc->mixext.maxvalue = MIN (mc->mixext.maxvalue, OSS_ENUM_MAXVALUE);

    g_free (mc->enum_vals);
    mc->enum_vals = g_new0 (GQuark, mc->mixext.maxvalue + 1);
    for (i = 0; i < mc->mixext.maxvalue; ++i) {
      GST_LOG ("  %s", ei.strings + ei.strindex[i]);
1367 1368 1369
      mc->enum_vals[i] =
          g_quark_from_string (gst_oss4_mixer_control_get_translated_option
          (ei.strings + ei.strindex[i]));
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
    }
  }

  return TRUE;
}

static void
gst_oss4_mixer_controls_find_enums (GstOss4Mixer * mixer,
    const GList * controls)
{
  const GList *l;

  for (l = controls; l != NULL; l = l->next) {
    GstOss4MixerControl *mc = (GstOss4MixerControl *) l->data;

    if (mc->mixext.type != MIXT_ENUM || mc->used)
      continue;

    mc->is_enum = TRUE;
    mc->used = TRUE;

    /* Note: enums are special: for most controls, the maxvalue is inclusive,
     * but for enum controls it's actually exclusive (boggle), so that
     * mixext.maxvalue = num_values */

    GST_LOG ("Enum: %s, parent=%s, num_enums=%d", mc->mixext.extname,
        mc->parent->mixext.extname, mc->mixext.maxvalue);

    gst_oss4_mixer_enum_control_update_enum_list (mixer, mc);
  }
}

static void
gst_oss4_mixer_controls_find_switches (GstOss4Mixer * mixer,
    const GList * controls)
{
  const GList *l;

  for (l = controls; l != NULL; l = l->next) {
    GstOss4MixerControl *mc = (GstOss4MixerControl *) l->data;

    if (mc->used)
      continue;

    if (mc->mixext.type != MIXT_ONOFF && mc->mixext.type != MIXT_MUTE)
      continue;

    mc->is_switch = TRUE;
    mc->used = TRUE;

    GST_LOG ("Switch: %s, parent=%s", mc->mixext.extname,
        mc->parent->mixext.extname);
  }
}

static void
gst_oss4_mixer_controls_find_virtual (GstOss4Mixer * mixer,
    const GList * controls)
{
  const GList *l;

  for (l = controls; l != NULL; l = l->next) {
    GstOss4MixerControl *mc = (GstOss4MixerControl *) l->data;

    /* or sscanf (mc->mixext.extname, "vmix%d-out.", &n) == 1 ? */
    /* for now we just flag all virtual controls with managed labels, those
     * are really more appropriate for a pavucontrol-type control thing than
     * the (more hardware-oriented) mixer interface */
    if (mc->mixext.id[0] == '@') {
      mc->is_virtual = TRUE;
      GST_LOG ("%s is virtual control with managed label", mc->mixext.extname);
    }
  }
}

static void
gst_oss4_mixer_controls_dump_unused (GstOss4Mixer * mixer,
    const GList * controls)
{
  const GList *l;

  for (l = controls; l != NULL; l = l->next) {
    GstOss4MixerControl *mc = (GstOss4MixerControl *) l->data;

    if (mc->used)
      continue;

    switch (mc->mixext.type) {
      case MIXT_DEVROOT:
      case MIXT_GROUP:
      case MIXT_MESSAGE:
      case MIXT_MONOVU:
      case MIXT_STEREOVU:
      case MIXT_MONOPEAK:
      case MIXT_STEREOPEAK:
      case MIXT_MARKER:
        continue;               /* not interested in these types of controls */
      case MIXT_MONODB:
      case MIXT_STEREODB:
        GST_DEBUG ("obsolete control type %d", mc->mixext.type);
        continue;
      case MIXT_MONOSLIDER:
      case MIXT_STEREOSLIDER:
      case MIXT_SLIDER:
      case MIXT_MONOSLIDER16:
      case MIXT_STEREOSLIDER16:
        /* this shouldn't happen */
        GST_ERROR ("unused slider control?!");
        continue;
      case MIXT_VALUE:
      case MIXT_HEXVALUE:
        /* value entry, not sure what to do with that, skip for now */
        continue;
      case MIXT_ONOFF:
      case MIXT_MUTE:
      case MIXT_ENUM:
      case MIXT_3D:
      case MIXT_RADIOGROUP:
        GST_DEBUG ("FIXME: handle %s %s",
            mixer_ext_type_get_name (mc->mixext.type), mc->mixext.extname);
        break;
      default:
        GST_WARNING ("unknown control type %d", mc->mixext.type);
        continue;
    }
  }
}

static GList *
gst_oss4_mixer_create_tracks (GstOss4Mixer * mixer, const GList * controls)
{
  const GList *c;
  GList *tracks = NULL;

  for (c = controls; c != NULL; c = c->next) {
    GstOss4MixerControl *mc = (GstOss4MixerControl *) c->data;
    GstMixerTrack *track = NULL;

    if (mc->is_virtual)
      continue;

    if (mc->is_slider) {
      track = gst_oss4_mixer_slider_new (mixer, mc);
    } else if (mc->is_enum) {
      track = gst_oss4_mixer_enum_new (mixer, mc);
    } else if (mc->is_switch) {
      track = gst_oss4_mixer_switch_new (mixer, mc);
    }

    if (track == NULL)
      continue;

1522
    track->label = gst_oss4_mixer_control_get_translated_name (mc);
1523 1524 1525
    track->flags = 0;

    GST_LOG ("translated label: %s [%s] = %s", track->label, mc->mixext.id,
1526
        track->label);
1527 1528 1529 1530 1531

    /* This whole 'a track is either INPUT or OUTPUT' model is just flawed,
     * esp. if a slider's role can be changed on the fly, like when you change
     * function of a connector. What should we do in that case? Change the flag
     * and make the app rebuild the interface? Ignore it? */
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
    if (mc->mixext.flags & (MIXF_MAINVOL | MIXF_PCMVOL)) {
      track->flags = GST_MIXER_TRACK_OUTPUT | GST_MIXER_TRACK_WHITELIST;

    } else if (mc->mixext.flags & MIXF_RECVOL) {
      /* record gain whitelisted by default */
      track->flags = GST_MIXER_TRACK_INPUT | GST_MIXER_TRACK_NO_RECORD |
          GST_MIXER_TRACK_WHITELIST;

    } else if (mc->mixext.flags & MIXF_MONVOL) {
      /* monitor sources not whitelisted by default */
      track->flags = GST_MIXER_TRACK_INPUT | GST_MIXER_TRACK_NO_RECORD;
1543 1544
    }

1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564
    /*
     * The kernel may give us better clues about the scope of a control.
     * If so, try to honor it.
     */
    switch (mc->mixext.desc & MIXEXT_SCOPE_MASK) {
      case MIXEXT_SCOPE_INPUT:
      case MIXEXT_SCOPE_RECSWITCH:
        track->flags |= GST_MIXER_TRACK_INPUT | GST_MIXER_TRACK_NO_RECORD |
            GST_MIXER_TRACK_WHITELIST;
        break;
      case MIXEXT_SCOPE_MONITOR:
        /* don't whitelist monitor tracks by default */
        track->flags |= GST_MIXER_TRACK_INPUT | GST_MIXER_TRACK_NO_RECORD;
        break;
      case MIXEXT_SCOPE_OUTPUT:
        track->flags = GST_MIXER_TRACK_OUTPUT | GST_MIXER_TRACK_WHITELIST;
        break;
    }

    if (mc->is_master) {
1565
      track->flags |= GST_MIXER_TRACK_OUTPUT;
1566
    }
1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678

    if (mc->is_master)
      track->flags |= GST_MIXER_TRACK_MASTER;

    tracks = g_list_append (tracks, track);
  }

  return tracks;
}

static void
gst_oss4_mixer_update_tracks (GstOss4Mixer * mixer)
{
  GList *controls, *tracks;

  /* read and process controls */
  controls = gst_oss4_mixer_get_controls (mixer);

  gst_oss4_mixer_controls_guess_master (mixer, controls);

  gst_oss4_mixer_controls_find_sliders (mixer, controls);

  gst_oss4_mixer_controls_find_enums (mixer, controls);

  gst_oss4_mixer_controls_find_switches (mixer, controls);

  gst_oss4_mixer_controls_find_virtual (mixer, controls);

  gst_oss4_mixer_controls_dump_unused (mixer, controls);

  tracks = gst_oss4_mixer_create_tracks (mixer, controls);

  /* free old tracks and controls */
  gst_oss4_mixer_free_tracks (mixer);

  /* replace old with new */
  mixer->tracks = tracks;
  mixer->controls = controls;
}

static const GList *
gst_oss4_mixer_list_tracks (GstMixer * mixer_iface)
{
  GstOss4Mixer *mixer = GST_OSS4_MIXER (mixer_iface);

  g_return_val_if_fail (mixer != NULL, NULL);
  g_return_val_if_fail (GST_OSS4_MIXER_IS_OPEN (mixer), NULL);

  GST_OBJECT_LOCK (mixer);

  /* Do a read on the last control to check if the interface has changed */
  if (!mixer->need_update && mixer->last_mixext.ctrl > 0) {
    GstOss4MixerControl mc = { {0,}
    ,
    };
    int val;

    mc.mixext = mixer->last_mixext;
    gst_oss4_mixer_get_control_val (mixer, &mc, &val);
  }

  if (mixer->need_update || mixer->tracks == NULL) {
    gst_oss4_mixer_update_tracks (mixer);
    mixer->need_update = FALSE;
  }

  GST_OBJECT_UNLOCK (mixer);

  return (const GList *) mixer->tracks;
}

static void
gst_oss4_mixer_set_volume (GstMixer * mixer, GstMixerTrack * track,
    gint * volumes)
{
  GstOss4Mixer *oss;

  g_return_if_fail (mixer != NULL);
  g_return_if_fail (GST_IS_OSS4_MIXER (mixer));
  g_return_if_fail (GST_OSS4_MIXER_IS_OPEN (mixer));
  g_return_if_fail (gst_oss4_mixer_contains_track (mixer, track));
  g_return_if_fail (volumes != NULL);

  oss = GST_OSS4_MIXER (mixer);

  GST_OBJECT_LOCK (oss);

  if (GST_IS_OSS4_MIXER_SLIDER (track)) {
    gst_oss4_mixer_slider_set_volume (GST_OSS4_MIXER_SLIDER (track), volumes);
  }

  GST_OBJECT_UNLOCK (oss);
}

static void
gst_oss4_mixer_get_volume (GstMixer * mixer, GstMixerTrack * track,
    gint * volumes)
{
  GstOss4Mixer *oss;

  g_return_if_fail (mixer != NULL);
  g_return_if_fail (GST_IS_OSS4_MIXER (mixer));
  g_return_if_fail (GST_OSS4_MIXER_IS_OPEN (mixer));
  g_return_if_fail (gst_oss4_mixer_contains_track (mixer, track));
  g_return_if_fail (volumes != NULL);

  oss = GST_OSS4_MIXER (mixer);

  GST_OBJECT_LOCK (oss);

  memset (volumes, 0, track->num_channels * sizeof (gint));

1679 1680 1681 1682
  if (GST_IS_OSS4_MIXER_SWITCH (track)) {
    gboolean enabled = FALSE;
    gst_oss4_mixer_switch_get (GST_OSS4_MIXER_SWITCH (track), &enabled);
  }
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734
  if (GST_IS_OSS4_MIXER_SLIDER (track)) {
    gst_oss4_mixer_slider_get_volume (GST_OSS4_MIXER_SLIDER (track), volumes);
  }

  GST_OBJECT_UNLOCK (oss);
}

static void
gst_oss4_mixer_set_record (GstMixer * mixer, GstMixerTrack * track,
    gboolean record)
{
  GstOss4Mixer *oss;

  g_return_if_fail (mixer != NULL);
  g_return_if_fail (GST_IS_OSS4_MIXER (mixer));
  g_return_if_fail (GST_OSS4_MIXER_IS_OPEN (mixer));
  g_return_if_fail (gst_oss4_mixer_contains_track (mixer, track));

  oss = GST_OSS4_MIXER (mixer);

  GST_OBJECT_LOCK (oss);

  if (GST_IS_OSS4_MIXER_SLIDER (track)) {
    gst_oss4_mixer_slider_set_record (GST_OSS4_MIXER_SLIDER (track), record);
  } else if (GST_IS_OSS4_MIXER_SWITCH (track)) {
    if ((track->flags & GST_MIXER_TRACK_INPUT)) {
      gst_oss4_mixer_switch_set (GST_OSS4_MIXER_SWITCH (track), record);
    } else {
      GST_WARNING_OBJECT (track, "set_record called on non-INPUT track");
    }
  }

  GST_OBJECT_UNLOCK (oss);
}

static void
gst_oss4_mixer_set_mute (GstMixer * mixer, GstMixerTrack * track, gboolean mute)
{
  GstOss4Mixer *oss;

  g_return_if_fail (mixer != NULL);
  g_return_if_fail (GST_IS_OSS4_MIXER (mixer));
  g_return_if_fail (GST_OSS4_MIXER_IS_OPEN (mixer));
  g_return_if_fail (gst_oss4_mixer_contains_track (mixer, track));

  oss = GST_OSS4_MIXER (mixer);

  GST_OBJECT_LOCK (oss);

  if (GST_IS_OSS4_MIXER_SLIDER (track)) {
    gst_oss4_mixer_slider_set_mute (GST_OSS4_MIXER_SLIDER (track), mute);
  } else if (GST_IS_OSS4_MIXER_SWITCH (track)) {
1735
    gst_oss4_mixer_switch_set (GST_OSS4_MIXER_SWITCH (track), mute);
1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798
  }

  GST_OBJECT_UNLOCK (oss);
}

static void
gst_oss4_mixer_set_option (GstMixer * mixer, GstMixerOptions * options,
    gchar * value)
{
  GstOss4Mixer *oss;

  g_return_if_fail (mixer != NULL);
  g_return_if_fail (value != NULL);
  g_return_if_fail (GST_IS_OSS4_MIXER (mixer));
  g_return_if_fail (GST_OSS4_MIXER_IS_OPEN (mixer));
  g_return_if_fail (GST_IS_OSS4_MIXER_ENUM (options));
  g_return_if_fail (gst_oss4_mixer_contains_options (mixer, options));

  oss = GST_OSS4_MIXER (mixer);

  GST_OBJECT_LOCK (oss);

  if (!gst_oss4_mixer_enum_set_option (GST_OSS4_MIXER_ENUM (options), value)) {
    /* not much we can do here but wake up the watch thread early, so it
     * can do its thing and post messages if anything has changed */
    gst_oss4_mixer_wake_up_watch_task (oss);
  }

  GST_OBJECT_UNLOCK (oss);
}

static const gchar *
gst_oss4_mixer_get_option (GstMixer * mixer, GstMixerOptions * options)
{
  GstOss4Mixer *oss;
  const gchar *current_val;

  g_return_val_if_fail (mixer != NULL, NULL);
  g_return_val_if_fail (GST_IS_OSS4_MIXER (mixer), NULL);
  g_return_val_if_fail (GST_OSS4_MIXER_IS_OPEN (mixer), NULL);
  g_return_val_if_fail (GST_IS_OSS4_MIXER_ENUM (options), NULL);
  g_return_val_if_fail (gst_oss4_mixer_contains_options (mixer, options), NULL);

  oss = GST_OSS4_MIXER (mixer);

  GST_OBJECT_LOCK (oss);

  current_val = gst_oss4_mixer_enum_get_option (GST_OSS4_MIXER_ENUM (options));

  if (current_val == NULL) {
    /* not much we can do here but wake up the watch thread early, so it
     * can do its thing and post messages if anything has changed */
    gst_oss4_mixer_wake_up_watch_task (oss);
  }

  GST_OBJECT_UNLOCK (oss);

  return current_val;
}

static GstMixerFlags
gst_oss4_mixer_get_mixer_flags (GstMixer * mixer)
{
1799 1800
  return GST_MIXER_FLAG_AUTO_NOTIFICATIONS | GST_MIXER_FLAG_HAS_WHITELIST |
      GST_MIXER_FLAG_GROUPING;
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857
}

static void
gst_oss4_mixer_interface_init (GstMixerClass * klass)
{
  GST_MIXER_TYPE (klass) = GST_MIXER_HARDWARE;

  klass->list_tracks = gst_oss4_mixer_list_tracks;
  klass->set_volume = gst_oss4_mixer_set_volume;
  klass->get_volume = gst_oss4_mixer_get_volume;
  klass->set_mute = gst_oss4_mixer_set_mute;
  klass->set_record = gst_oss4_mixer_set_record;
  klass->set_option = gst_oss4_mixer_set_option;
  klass->get_option = gst_oss4_mixer_get_option;
  klass->get_mixer_flags = gst_oss4_mixer_get_mixer_flags;
}

/* Implement the horror that is GstImplementsInterface */

static gboolean
gst_oss4_mixer_supported (GstImplementsInterface * iface, GType iface_type)
{
  GstOss4Mixer *mixer;

  g_return_val_if_fail (iface_type == GST_TYPE_MIXER, FALSE);

  mixer = GST_OSS4_MIXER (iface);

  return GST_OSS4_MIXER_IS_OPEN (mixer);
}

static void
gst_oss4_mixer_implements_interface_init (GstImplementsInterfaceClass * klass)
{
  klass->supported = gst_oss4_mixer_supported;
}

static void
gst_oss4_mixer_init_interfaces (GType type)
{
  static const GInterfaceInfo implements_iface_info = {
    (GInterfaceInitFunc) gst_oss4_mixer_implements_interface_init,
    NULL,
    NULL,
  };
  static const GInterfaceInfo mixer_iface_info = {
    (GInterfaceInitFunc) gst_oss4_mixer_interface_init,
    NULL,
    NULL,
  };

  g_type_add_interface_static (type, GST_TYPE_IMPLEMENTS_INTERFACE,
      &implements_iface_info);
  g_type_add_interface_static (type, GST_TYPE_MIXER, &mixer_iface_info);

  gst_oss4_add_property_probe_interface (type);
}