DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (b6d82b1a6b02)

VCS Links

Line Code
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "CubebDeviceEnumerator.h"
#include "mozilla/StaticPtr.h"

namespace mozilla {

using namespace CubebUtils;

/* static */
StaticRefPtr<CubebDeviceEnumerator> CubebDeviceEnumerator::sInstance;

/* static */
already_AddRefed<CubebDeviceEnumerator> CubebDeviceEnumerator::GetInstance() {
  if (!sInstance) {
    sInstance = new CubebDeviceEnumerator();
  }
  RefPtr<CubebDeviceEnumerator> instance = sInstance.get();
  MOZ_ASSERT(instance);
  return instance.forget();
}

CubebDeviceEnumerator::CubebDeviceEnumerator()
    : mMutex("CubebDeviceListMutex"),
      mManualInputInvalidation(false),
      mManualOutputInvalidation(false) {
  int rv = cubeb_register_device_collection_changed(
      GetCubebContext(), CUBEB_DEVICE_TYPE_OUTPUT,
      &OutputAudioDeviceListChanged_s, this);
  if (rv != CUBEB_OK) {
    NS_WARNING(
        "Could not register the audio output"
        " device collection changed callback.");
    mManualOutputInvalidation = true;
  }
  rv = cubeb_register_device_collection_changed(
      GetCubebContext(), CUBEB_DEVICE_TYPE_INPUT,
      &InputAudioDeviceListChanged_s, this);
  if (rv != CUBEB_OK) {
    NS_WARNING(
        "Could not register the audio input"
        " device collection changed callback.");
    mManualInputInvalidation = true;
  }
}

/* static */
void CubebDeviceEnumerator::Shutdown() {
  if (sInstance) {
    sInstance = nullptr;
  }
}

CubebDeviceEnumerator::~CubebDeviceEnumerator() {
  int rv = cubeb_register_device_collection_changed(
      GetCubebContext(), CUBEB_DEVICE_TYPE_OUTPUT, nullptr, this);
  if (rv != CUBEB_OK) {
    NS_WARNING(
        "Could not unregister the audio output"
        " device collection changed callback.");
  }
  rv = cubeb_register_device_collection_changed(
      GetCubebContext(), CUBEB_DEVICE_TYPE_INPUT, nullptr, this);
  if (rv != CUBEB_OK) {
    NS_WARNING(
        "Could not unregister the audio input"
        " device collection changed callback.");
  }
}

void CubebDeviceEnumerator::EnumerateAudioInputDevices(
    nsTArray<RefPtr<AudioDeviceInfo>>& aOutDevices) {
  MutexAutoLock lock(mMutex);
  aOutDevices.Clear();
  EnumerateAudioDevices(Side::INPUT);
  aOutDevices.AppendElements(mInputDevices);
}

void CubebDeviceEnumerator::EnumerateAudioOutputDevices(
    nsTArray<RefPtr<AudioDeviceInfo>>& aOutDevices) {
  MutexAutoLock lock(mMutex);
  aOutDevices.Clear();
  EnumerateAudioDevices(Side::OUTPUT);
  aOutDevices.AppendElements(mOutputDevices);
}

#ifndef ANDROID
static uint16_t ConvertCubebType(cubeb_device_type aType) {
  uint16_t map[] = {
      nsIAudioDeviceInfo::TYPE_UNKNOWN,  // CUBEB_DEVICE_TYPE_UNKNOWN
      nsIAudioDeviceInfo::TYPE_INPUT,    // CUBEB_DEVICE_TYPE_INPUT,
      nsIAudioDeviceInfo::TYPE_OUTPUT    // CUBEB_DEVICE_TYPE_OUTPUT
  };
  return map[aType];
}

static uint16_t ConvertCubebState(cubeb_device_state aState) {
  uint16_t map[] = {
      nsIAudioDeviceInfo::STATE_DISABLED,   // CUBEB_DEVICE_STATE_DISABLED
      nsIAudioDeviceInfo::STATE_UNPLUGGED,  // CUBEB_DEVICE_STATE_UNPLUGGED
      nsIAudioDeviceInfo::STATE_ENABLED     // CUBEB_DEVICE_STATE_ENABLED
  };
  return map[aState];
}

static uint16_t ConvertCubebPreferred(cubeb_device_pref aPreferred) {
  if (aPreferred == CUBEB_DEVICE_PREF_NONE) {
    return nsIAudioDeviceInfo::PREF_NONE;
  }
  if (aPreferred == CUBEB_DEVICE_PREF_ALL) {
    return nsIAudioDeviceInfo::PREF_ALL;
  }

  uint16_t preferred = 0;
  if (aPreferred & CUBEB_DEVICE_PREF_MULTIMEDIA) {
    preferred |= nsIAudioDeviceInfo::PREF_MULTIMEDIA;
  }
  if (aPreferred & CUBEB_DEVICE_PREF_VOICE) {
    preferred |= nsIAudioDeviceInfo::PREF_VOICE;
  }
  if (aPreferred & CUBEB_DEVICE_PREF_NOTIFICATION) {
    preferred |= nsIAudioDeviceInfo::PREF_NOTIFICATION;
  }
  return preferred;
}

static uint16_t ConvertCubebFormat(cubeb_device_fmt aFormat) {
  uint16_t format = 0;
  if (aFormat & CUBEB_DEVICE_FMT_S16LE) {
    format |= nsIAudioDeviceInfo::FMT_S16LE;
  }
  if (aFormat & CUBEB_DEVICE_FMT_S16BE) {
    format |= nsIAudioDeviceInfo::FMT_S16BE;
  }
  if (aFormat & CUBEB_DEVICE_FMT_F32LE) {
    format |= nsIAudioDeviceInfo::FMT_F32LE;
  }
  if (aFormat & CUBEB_DEVICE_FMT_F32BE) {
    format |= nsIAudioDeviceInfo::FMT_F32BE;
  }
  return format;
}

static void GetDeviceCollection(nsTArray<RefPtr<AudioDeviceInfo>>& aDeviceInfos,
                                Side aSide) {
  cubeb* context = GetCubebContext();
  if (context) {
    cubeb_device_collection collection = {nullptr, 0};
    if (cubeb_enumerate_devices(
            context,
            aSide == Input ? CUBEB_DEVICE_TYPE_INPUT : CUBEB_DEVICE_TYPE_OUTPUT,
            &collection) == CUBEB_OK) {
      for (unsigned int i = 0; i < collection.count; ++i) {
        auto device = collection.device[i];
        RefPtr<AudioDeviceInfo> info = new AudioDeviceInfo(
            device.devid, NS_ConvertUTF8toUTF16(device.friendly_name),
            NS_ConvertUTF8toUTF16(device.group_id),
            NS_ConvertUTF8toUTF16(device.vendor_name),
            ConvertCubebType(device.type), ConvertCubebState(device.state),
            ConvertCubebPreferred(device.preferred),
            ConvertCubebFormat(device.format),
            ConvertCubebFormat(device.default_format), device.max_channels,
            device.default_rate, device.max_rate, device.min_rate,
            device.latency_hi, device.latency_lo);
        aDeviceInfos.AppendElement(info);
      }
    }
    cubeb_device_collection_destroy(context, &collection);
  }
}
#endif  // non ANDROID

void CubebDeviceEnumerator::EnumerateAudioDevices(
    CubebDeviceEnumerator::Side aSide) {
  mMutex.AssertCurrentThreadOwns();
  MOZ_ASSERT(aSide == Side::INPUT || aSide == Side::OUTPUT);

  nsTArray<RefPtr<AudioDeviceInfo>> devices;
  bool manualInvalidation = true;

  if (aSide == Side::INPUT) {
    devices.SwapElements(mInputDevices);
    manualInvalidation = mManualInputInvalidation;
  } else {
    MOZ_ASSERT(aSide == Side::OUTPUT);
    devices.SwapElements(mOutputDevices);
    manualInvalidation = mManualOutputInvalidation;
  }

  cubeb* context = GetCubebContext();
  if (!context) {
    return;
  }

#ifdef ANDROID
  cubeb_device_type type = CUBEB_DEVICE_TYPE_UNKNOWN;
  uint32_t channels = 0;
  if (aSide == Side::INPUT) {
    type = CUBEB_DEVICE_TYPE_INPUT;
    channels = 1;
  } else {
    MOZ_ASSERT(aSide == Side::OUTPUT);
    type = CUBEB_DEVICE_TYPE_OUTPUT;
    channels = 2;
  }

  if (devices.IsEmpty()) {
    // Bug 1473346: enumerating devices is not supported on Android in cubeb,
    // simply state that there is a single sink, that it is the default, and has
    // a single channel. All the other values are made up and are not to be
    // used.
    RefPtr<AudioDeviceInfo> info = new AudioDeviceInfo(
        nullptr, NS_ConvertUTF8toUTF16(""), NS_ConvertUTF8toUTF16(""),
        NS_ConvertUTF8toUTF16(""), type, CUBEB_DEVICE_STATE_ENABLED,
        CUBEB_DEVICE_PREF_ALL, CUBEB_DEVICE_FMT_ALL, CUBEB_DEVICE_FMT_S16NE,
        channels, 44100, 44100, 41000, 410, 128);
    devices.AppendElement(info);
  }
#else
  if (devices.IsEmpty() || manualInvalidation) {
    devices.Clear();
    GetDeviceCollection(devices, (aSide == Side::INPUT) ? CubebUtils::Input
                                                        : CubebUtils::Output);
  }
#endif

  if (aSide == Side::INPUT) {
    mInputDevices.AppendElements(devices);
  } else {
    mOutputDevices.AppendElements(devices);
  }
}

already_AddRefed<AudioDeviceInfo> CubebDeviceEnumerator::DeviceInfoFromID(
    CubebUtils::AudioDeviceID aID) {
  MutexAutoLock lock(mMutex);

  if (mInputDevices.IsEmpty() || mManualInputInvalidation) {
    EnumerateAudioDevices(Side::INPUT);
  }
  for (uint32_t i = 0; i < mInputDevices.Length(); i++) {
    if (mInputDevices[i]->DeviceID() == aID) {
      RefPtr<AudioDeviceInfo> other = mInputDevices[i];
      return other.forget();
    }
  }

  if (mOutputDevices.IsEmpty() || mManualOutputInvalidation) {
    EnumerateAudioDevices(Side::OUTPUT);
  }
  for (uint32_t i = 0; i < mOutputDevices.Length(); i++) {
    if (mOutputDevices[i]->DeviceID() == aID) {
      RefPtr<AudioDeviceInfo> other = mOutputDevices[i];
      return other.forget();
    }
  }
  return nullptr;
}

already_AddRefed<AudioDeviceInfo> CubebDeviceEnumerator::DeviceInfoFromName(
    const nsString& aName) {
  RefPtr<AudioDeviceInfo> other = DeviceInfoFromName(aName, Side::INPUT);
  if (other) {
    return other.forget();
  }
  return DeviceInfoFromName(aName, Side::OUTPUT);
}

already_AddRefed<AudioDeviceInfo> CubebDeviceEnumerator::DeviceInfoFromName(
    const nsString& aName, Side aSide) {
  MutexAutoLock lock(mMutex);

  nsTArray<RefPtr<AudioDeviceInfo>>& devices =
      (aSide == Side::INPUT) ? mInputDevices : mOutputDevices;
  bool manualInvalidation = (aSide == Side::INPUT) ? mManualInputInvalidation
                                                   : mManualOutputInvalidation;

  if (devices.IsEmpty() || manualInvalidation) {
    EnumerateAudioDevices(aSide);
  }
  for (uint32_t i = 0; i < devices.Length(); i++) {
    if (devices[i]->Name().Equals(aName)) {
      RefPtr<AudioDeviceInfo> other = devices[i];
      return other.forget();
    }
  }

  return nullptr;
}

void CubebDeviceEnumerator::InputAudioDeviceListChanged_s(cubeb* aContext,
                                                          void* aUser) {
  CubebDeviceEnumerator* self = reinterpret_cast<CubebDeviceEnumerator*>(aUser);
  self->AudioDeviceListChanged(CubebDeviceEnumerator::Side::INPUT);
}

void CubebDeviceEnumerator::OutputAudioDeviceListChanged_s(cubeb* aContext,
                                                           void* aUser) {
  CubebDeviceEnumerator* self = reinterpret_cast<CubebDeviceEnumerator*>(aUser);
  self->AudioDeviceListChanged(CubebDeviceEnumerator::Side::OUTPUT);
}

void CubebDeviceEnumerator::AudioDeviceListChanged(Side aSide) {
  MutexAutoLock lock(mMutex);

  if (aSide == Side::INPUT) {
    mInputDevices.Clear();
  } else {
    MOZ_ASSERT(aSide == Side::OUTPUT);
    mOutputDevices.Clear();
  }
}

}  // namespace mozilla