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 (d1c688190228)

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 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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* 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 "CubebUtils.h"

#include "MediaInfo.h"
#include "mozilla/AbstractThread.h"
#include "mozilla/dom/ContentChild.h"
#include "mozilla/ipc/FileDescriptor.h"
#include "mozilla/Logging.h"
#include "mozilla/Preferences.h"
#include "mozilla/Services.h"
#include "mozilla/Sprintf.h"
#include "mozilla/StaticMutex.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/Telemetry.h"
#include "nsAutoRef.h"
#include "nsDebug.h"
#include "nsIStringBundle.h"
#include "nsString.h"
#include "nsThreadUtils.h"
#include "prdtoa.h"
#include <algorithm>
#include <stdint.h>
#ifdef MOZ_WIDGET_ANDROID
#include "GeneratedJNIWrappers.h"
#endif

#define PREF_VOLUME_SCALE "media.volume_scale"
#define PREF_CUBEB_BACKEND "media.cubeb.backend"
#define PREF_CUBEB_LATENCY_PLAYBACK "media.cubeb_latency_playback_ms"
#define PREF_CUBEB_LATENCY_MSG "media.cubeb_latency_msg_frames"
// Allows to get something non-default for the preferred sample-rate, to allow
// troubleshooting in the field and testing.
#define PREF_CUBEB_FORCE_SAMPLE_RATE "media.cubeb.force_sample_rate"
#define PREF_CUBEB_LOGGING_LEVEL "media.cubeb.logging_level"
#define PREF_CUBEB_SANDBOX "media.cubeb.sandbox"

#define MASK_MONO (1 << AudioConfig::CHANNEL_MONO)
#define MASK_MONO_LFE (MASK_MONO | (1 << AudioConfig::CHANNEL_LFE))
#define MASK_STEREO \
  ((1 << AudioConfig::CHANNEL_LEFT) | (1 << AudioConfig::CHANNEL_RIGHT))
#define MASK_STEREO_LFE (MASK_STEREO | (1 << AudioConfig::CHANNEL_LFE))
#define MASK_3F (MASK_STEREO | (1 << AudioConfig::CHANNEL_CENTER))
#define MASK_3F_LFE (MASK_3F | (1 << AudioConfig::CHANNEL_LFE))
#define MASK_2F1 (MASK_STEREO | (1 << AudioConfig::CHANNEL_RCENTER))
#define MASK_2F1_LFE (MASK_2F1 | (1 << AudioConfig::CHANNEL_LFE))
#define MASK_3F1 (MASK_3F | (1 < AudioConfig::CHANNEL_RCENTER))
#define MASK_3F1_LFE (MASK_3F1 | (1 << AudioConfig::CHANNEL_LFE))
#define MASK_2F2                                  \
  (MASK_STEREO | (1 << AudioConfig::CHANNEL_LS) | \
   (1 << AudioConfig::CHANNEL_RS))
#define MASK_2F2_LFE (MASK_2F2 | (1 << AudioConfig::CHANNEL_LFE))
#define MASK_3F2 \
  (MASK_3F | (1 << AudioConfig::CHANNEL_LS) | (1 << AudioConfig::CHANNEL_RS))
#define MASK_3F2_LFE (MASK_3F2 | (1 << AudioConfig::CHANNEL_LFE))
#define MASK_3F3R_LFE (MASK_3F2_LFE | (1 << AudioConfig::CHANNEL_RCENTER))
#define MASK_3F4_LFE                                \
  (MASK_3F2_LFE | (1 << AudioConfig::CHANNEL_RLS) | \
   (1 << AudioConfig::CHANNEL_RRS))

#if (defined(XP_LINUX) && !defined(MOZ_WIDGET_ANDROID)) || defined(XP_MACOSX)
#define MOZ_CUBEB_REMOTING
#endif

extern "C" {
// These functions are provided by audioipc-server crate
extern void* audioipc_server_start();
extern mozilla::ipc::FileDescriptor::PlatformHandleType
audioipc_server_new_client(void*);
extern void audioipc_server_stop(void*);
// These functions are provided by audioipc-client crate
extern int audioipc_client_init(cubeb**, const char*, int);
}

namespace mozilla {

namespace {

#ifdef MOZ_CUBEB_REMOTING
////////////////////////////////////////////////////////////////////////////////
// Cubeb Sound Server Thread
void* sServerHandle = nullptr;

// Initialized during early startup, protected by sMutex.
StaticAutoPtr<ipc::FileDescriptor> sIPCConnection;

static bool StartSoundServer() {
  sServerHandle = audioipc_server_start();
  return sServerHandle != nullptr;
}

static void ShutdownSoundServer() {
  if (!sServerHandle) return;

  audioipc_server_stop(sServerHandle);
  sServerHandle = nullptr;
}
#endif  // MOZ_CUBEB_REMOTING

////////////////////////////////////////////////////////////////////////////////

LazyLogModule gCubebLog("cubeb");

void CubebLogCallback(const char* aFmt, ...) {
  char buffer[256];

  va_list arglist;
  va_start(arglist, aFmt);
  VsprintfLiteral(buffer, aFmt, arglist);
  MOZ_LOG(gCubebLog, LogLevel::Error, ("%s", buffer));
  va_end(arglist);
}

// This mutex protects the variables below.
StaticMutex sMutex;
enum class CubebState {
  Uninitialized = 0,
  Initialized,
  Shutdown
} sCubebState = CubebState::Uninitialized;
cubeb* sCubebContext;
double sVolumeScale = 1.0;
uint32_t sCubebPlaybackLatencyInMilliseconds = 100;
uint32_t sCubebMSGLatencyInFrames = 512;
// If sCubebForcedSampleRate is zero, PreferredSampleRate will return the
// preferred sample-rate for the audio backend in use. Otherwise, it will be
// used as the preferred sample-rate.
uint32_t sCubebForcedSampleRate = 0;
bool sCubebPlaybackLatencyPrefSet = false;
bool sCubebMSGLatencyPrefSet = false;
bool sAudioStreamInitEverSucceeded = false;
#ifdef MOZ_CUBEB_REMOTING
bool sCubebSandbox;
#endif
StaticAutoPtr<char> sBrandName;
StaticAutoPtr<char> sCubebBackendName;

const char kBrandBundleURL[] = "chrome://branding/locale/brand.properties";

const char* AUDIOSTREAM_BACKEND_ID_STR[] = {
    "jack",  "pulse",       "alsa",  "audiounit", "audioqueue", "wasapi",
    "winmm", "directsound", "sndio", "opensl",    "audiotrack", "kai"};
/* Index for failures to create an audio stream the first time. */
const int CUBEB_BACKEND_INIT_FAILURE_FIRST =
    ArrayLength(AUDIOSTREAM_BACKEND_ID_STR);
/* Index for failures to create an audio stream after the first time */
const int CUBEB_BACKEND_INIT_FAILURE_OTHER =
    CUBEB_BACKEND_INIT_FAILURE_FIRST + 1;
/* Index for an unknown backend. */
const int CUBEB_BACKEND_UNKNOWN = CUBEB_BACKEND_INIT_FAILURE_FIRST + 2;

typedef struct {
  const char* name;
  const unsigned int channels;
  const uint32_t mask;
} layoutInfo;

const layoutInfo kLayoutInfos[CUBEB_LAYOUT_MAX] = {
    {"undefined", 0, 0},                    // CUBEB_LAYOUT_UNDEFINED
    {"dual mono", 2, MASK_STEREO},          // CUBEB_LAYOUT_DUAL_MONO
    {"dual mono lfe", 3, MASK_STEREO_LFE},  // CUBEB_LAYOUT_DUAL_MONO_LFE
    {"mono", 1, MASK_MONO},                 // CUBEB_LAYOUT_MONO
    {"mono lfe", 2, MASK_MONO_LFE},         // CUBEB_LAYOUT_MONO_LFE
    {"stereo", 2, MASK_STEREO},             // CUBEB_LAYOUT_STEREO
    {"stereo lfe", 3, MASK_STEREO_LFE},     // CUBEB_LAYOUT_STEREO_LFE
    {"3f", 3, MASK_3F},                     // CUBEB_LAYOUT_3F
    {"3f lfe", 4, MASK_3F_LFE},             // CUBEB_LAYOUT_3F_LFE
    {"2f1", 3, MASK_2F1},                   // CUBEB_LAYOUT_2F1
    {"2f1 lfe", 4, MASK_2F1_LFE},           // CUBEB_LAYOUT_2F1_LFE
    {"3f1", 4, MASK_3F1},                   // CUBEB_LAYOUT_3F1
    {"3f1 lfe", 5, MASK_3F1_LFE},           // CUBEB_LAYOUT_3F1_LFE
    {"2f2", 4, MASK_2F2_LFE},               // CUBEB_LAYOUT_2F2
    {"2f2 lfe", 5, MASK_2F2_LFE},           // CUBEB_LAYOUT_2F2_LFE
    {"3f2", 5, MASK_3F2},                   // CUBEB_LAYOUT_3F2
    {"3f2 lfe", 6, MASK_3F2_LFE},           // CUBEB_LAYOUT_3F2_LFE
    {"3f3r lfe", 7, MASK_3F3R_LFE},         // CUBEB_LAYOUT_3F3R_LFE
    {"3f4 lfe", 8, MASK_3F4_LFE}            // CUBEB_LAYOUT_3F4_LFE
};

// Prefered samplerate, in Hz (characteristic of the hardware, mixer, platform,
// and API used).
//
// sMutex protects *initialization* of this, which must be performed from each
// thread before fetching, after which it is safe to fetch without holding the
// mutex because it is only written once per process execution (by the first
// initialization to complete).  Since the init must have been called on a
// given thread before fetching the value, it's guaranteed (via the mutex) that
// sufficient memory barriers have occurred to ensure the correct value is
// visible on the querying thread/CPU.
uint32_t sPreferredSampleRate;

// We only support SMPTE layout in cubeb for now. If the value is
// CUBEB_LAYOUT_UNDEFINED, then it implies that the preferred layout is
// non-SMPTE format.
cubeb_channel_layout sPreferredChannelLayout;

}  // namespace

static const uint32_t CUBEB_NORMAL_LATENCY_MS = 100;
// Consevative default that can work on all platforms.
static const uint32_t CUBEB_NORMAL_LATENCY_FRAMES = 1024;

namespace CubebUtils {
cubeb* GetCubebContextUnlocked();

void PrefChanged(const char* aPref, void* aClosure) {
  if (strcmp(aPref, PREF_VOLUME_SCALE) == 0) {
    nsAutoCString value;
    Preferences::GetCString(aPref, value);
    StaticMutexAutoLock lock(sMutex);
    if (value.IsEmpty()) {
      sVolumeScale = 1.0;
    } else {
      sVolumeScale = std::max<double>(0, PR_strtod(value.get(), nullptr));
    }
  } else if (strcmp(aPref, PREF_CUBEB_LATENCY_PLAYBACK) == 0) {
    // Arbitrary default stream latency of 100ms.  The higher this
    // value, the longer stream volume changes will take to become
    // audible.
    sCubebPlaybackLatencyPrefSet = Preferences::HasUserValue(aPref);
    uint32_t value = Preferences::GetUint(aPref, CUBEB_NORMAL_LATENCY_MS);
    StaticMutexAutoLock lock(sMutex);
    sCubebPlaybackLatencyInMilliseconds =
        std::min<uint32_t>(std::max<uint32_t>(value, 1), 1000);
  } else if (strcmp(aPref, PREF_CUBEB_LATENCY_MSG) == 0) {
    sCubebMSGLatencyPrefSet = Preferences::HasUserValue(aPref);
    uint32_t value = Preferences::GetUint(aPref, CUBEB_NORMAL_LATENCY_FRAMES);
    StaticMutexAutoLock lock(sMutex);
    // 128 is the block size for the Web Audio API, which limits how low the
    // latency can be here.
    // We don't want to limit the upper limit too much, so that people can
    // experiment.
    sCubebMSGLatencyInFrames =
        std::min<uint32_t>(std::max<uint32_t>(value, 128), 1e6);
  } else if (strcmp(aPref, PREF_CUBEB_FORCE_SAMPLE_RATE) == 0) {
    StaticMutexAutoLock lock(sMutex);
    sCubebForcedSampleRate = Preferences::GetUint(aPref);
  } else if (strcmp(aPref, PREF_CUBEB_LOGGING_LEVEL) == 0) {
    nsAutoCString value;
    Preferences::GetCString(aPref, value);
    LogModule* cubebLog = LogModule::Get("cubeb");
    if (value.EqualsLiteral("verbose")) {
      cubeb_set_log_callback(CUBEB_LOG_VERBOSE, CubebLogCallback);
      cubebLog->SetLevel(LogLevel::Verbose);
    } else if (value.EqualsLiteral("normal")) {
      cubeb_set_log_callback(CUBEB_LOG_NORMAL, CubebLogCallback);
      cubebLog->SetLevel(LogLevel::Error);
    } else if (value.IsEmpty()) {
      cubeb_set_log_callback(CUBEB_LOG_DISABLED, nullptr);
      cubebLog->SetLevel(LogLevel::Disabled);
    }
  } else if (strcmp(aPref, PREF_CUBEB_BACKEND) == 0) {
    nsAutoCString value;
    Preferences::GetCString(aPref, value);
    if (value.IsEmpty()) {
      sCubebBackendName = nullptr;
    } else {
      sCubebBackendName = new char[value.Length() + 1];
      PodCopy(sCubebBackendName.get(), value.get(), value.Length());
      sCubebBackendName[value.Length()] = 0;
    }
  }
#ifdef MOZ_CUBEB_REMOTING
  else if (strcmp(aPref, PREF_CUBEB_SANDBOX) == 0) {
    StaticMutexAutoLock lock(sMutex);
    sCubebSandbox = Preferences::GetBool(aPref);
    MOZ_LOG(gCubebLog, LogLevel::Verbose,
            ("%s: %s", PREF_CUBEB_SANDBOX, sCubebSandbox ? "true" : "false"));

    if (sCubebSandbox && !sServerHandle && XRE_IsParentProcess()) {
      MOZ_LOG(gCubebLog, LogLevel::Debug, ("Starting cubeb server..."));
      StartSoundServer();
    }
  }
#endif
}

bool GetFirstStream() {
  static bool sFirstStream = true;

  StaticMutexAutoLock lock(sMutex);
  bool result = sFirstStream;
  sFirstStream = false;
  return result;
}

double GetVolumeScale() {
  StaticMutexAutoLock lock(sMutex);
  return sVolumeScale;
}

cubeb* GetCubebContext() {
  StaticMutexAutoLock lock(sMutex);
  return GetCubebContextUnlocked();
}

bool InitPreferredSampleRate() {
  StaticMutexAutoLock lock(sMutex);
  if (sPreferredSampleRate != 0) {
    return true;
  }
  cubeb* context = GetCubebContextUnlocked();
  if (!context) {
    return false;
  }
#ifdef MOZ_WIDGET_ANDROID
  sPreferredSampleRate = AndroidGetAudioOutputSampleRate();
#else
  if (cubeb_get_preferred_sample_rate(context, &sPreferredSampleRate) !=
      CUBEB_OK) {
    return false;
  }
#endif
  MOZ_ASSERT(sPreferredSampleRate);
  return true;
}

uint32_t PreferredSampleRate() {
  if (sCubebForcedSampleRate) {
    return sCubebForcedSampleRate;
  }
  if (!InitPreferredSampleRate()) {
    return 44100;
  }
  MOZ_ASSERT(sPreferredSampleRate);
  return sPreferredSampleRate;
}

bool InitPreferredChannelLayout() {
  {
    StaticMutexAutoLock lock(sMutex);
    if (sPreferredChannelLayout != 0) {
      return true;
    }
  }

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

  // Favor calling cubeb api with the mutex unlock, potential deadlock.
  cubeb_channel_layout layout;
  if (cubeb_get_preferred_channel_layout(context, &layout) != CUBEB_OK) {
    return false;
  }

  StaticMutexAutoLock lock(sMutex);
  sPreferredChannelLayout = layout;
  return true;
}

uint32_t PreferredChannelMap(uint32_t aChannels) {
  // Use SMPTE default channel map if we can't get preferred layout
  // or the channel counts of preferred layout is different from input's one
  if (!InitPreferredChannelLayout() ||
      kLayoutInfos[sPreferredChannelLayout].channels != aChannels) {
    AudioConfig::ChannelLayout smpteLayout(aChannels);
    return smpteLayout.Map();
  }

  return kLayoutInfos[sPreferredChannelLayout].mask;
}

cubeb_channel_layout GetPreferredChannelLayoutOrSMPTE(cubeb* context,
                                                      uint32_t aChannels) {
  cubeb_channel_layout layout = CUBEB_LAYOUT_UNDEFINED;
  if (cubeb_get_preferred_channel_layout(context, &layout) != CUBEB_OK) {
    return layout;  // undefined
  }

  if (kLayoutInfos[layout].channels != aChannels) {
    AudioConfig::ChannelLayout smpteLayout(aChannels);
    return ConvertChannelMapToCubebLayout(smpteLayout.Map());
  }

  return layout;
}

void InitBrandName() {
  if (sBrandName) {
    return;
  }
  nsAutoString brandName;
  nsCOMPtr<nsIStringBundleService> stringBundleService =
      mozilla::services::GetStringBundleService();
  if (stringBundleService) {
    nsCOMPtr<nsIStringBundle> brandBundle;
    nsresult rv = stringBundleService->CreateBundle(
        kBrandBundleURL, getter_AddRefs(brandBundle));
    if (NS_SUCCEEDED(rv)) {
      rv = brandBundle->GetStringFromName("brandShortName", brandName);
      NS_WARNING_ASSERTION(
          NS_SUCCEEDED(rv),
          "Could not get the program name for a cubeb stream.");
    }
  }
  NS_LossyConvertUTF16toASCII ascii(brandName);
  sBrandName = new char[ascii.Length() + 1];
  PodCopy(sBrandName.get(), ascii.get(), ascii.Length());
  sBrandName[ascii.Length()] = 0;
}

#ifdef MOZ_CUBEB_REMOTING
void InitAudioIPCConnection() {
  MOZ_ASSERT(NS_IsMainThread());
  auto contentChild = dom::ContentChild::GetSingleton();
  auto promise = contentChild->SendCreateAudioIPCConnection();
  promise->Then(
      AbstractThread::MainThread(), __func__,
      [](ipc::FileDescriptor aFD) {
        StaticMutexAutoLock lock(sMutex);
        MOZ_ASSERT(!sIPCConnection);
        sIPCConnection = new ipc::FileDescriptor(aFD);
      },
      [](mozilla::ipc::ResponseRejectReason aReason) {
        MOZ_LOG(gCubebLog, LogLevel::Error,
                ("SendCreateAudioIPCConnection failed: %d", int(aReason)));
      });
}
#endif

ipc::FileDescriptor CreateAudioIPCConnection() {
#ifdef MOZ_CUBEB_REMOTING
  MOZ_ASSERT(sServerHandle);
  int rawFD = audioipc_server_new_client(sServerHandle);
  ipc::FileDescriptor fd(rawFD);
  close(rawFD);
  return fd;
#else
  return ipc::FileDescriptor();
#endif
}

cubeb* GetCubebContextUnlocked() {
  sMutex.AssertCurrentThreadOwns();
  if (sCubebState != CubebState::Uninitialized) {
    // If we have already passed the initialization point (below), just return
    // the current context, which may be null (e.g., after error or shutdown.)
    return sCubebContext;
  }

  if (!sBrandName && NS_IsMainThread()) {
    InitBrandName();
  } else {
    NS_WARNING_ASSERTION(
        sBrandName,
        "Did not initialize sbrandName, and not on the main thread?");
  }

#ifdef MOZ_CUBEB_REMOTING
  if (sCubebSandbox) {
    if (XRE_IsParentProcess()) {
      // TODO: Don't use audio IPC when within the same process.
      MOZ_ASSERT(!sIPCConnection);
      sIPCConnection = new ipc::FileDescriptor(CreateAudioIPCConnection());
    } else {
      MOZ_DIAGNOSTIC_ASSERT(sIPCConnection);
    }
  }

  MOZ_LOG(gCubebLog, LogLevel::Info,
          ("%s: %s", PREF_CUBEB_SANDBOX, sCubebSandbox ? "true" : "false"));

  int rv = sCubebSandbox ? audioipc_client_init(
                               &sCubebContext, sBrandName,
                               sIPCConnection->ClonePlatformHandle().release())
                         : cubeb_init(&sCubebContext, sBrandName,
                                      sCubebBackendName.get());
  sIPCConnection = nullptr;
#else   // !MOZ_CUBEB_REMOTING
  int rv = cubeb_init(&sCubebContext, sBrandName, sCubebBackendName.get());
#endif  // MOZ_CUBEB_REMOTING
  NS_WARNING_ASSERTION(rv == CUBEB_OK, "Could not get a cubeb context.");
  sCubebState =
      (rv == CUBEB_OK) ? CubebState::Initialized : CubebState::Uninitialized;

  return sCubebContext;
}

void ReportCubebBackendUsed() {
  StaticMutexAutoLock lock(sMutex);

  sAudioStreamInitEverSucceeded = true;

  bool foundBackend = false;
  for (uint32_t i = 0; i < ArrayLength(AUDIOSTREAM_BACKEND_ID_STR); i++) {
    if (!strcmp(cubeb_get_backend_id(sCubebContext),
                AUDIOSTREAM_BACKEND_ID_STR[i])) {
      Telemetry::Accumulate(Telemetry::AUDIOSTREAM_BACKEND_USED, i);
      foundBackend = true;
    }
  }
  if (!foundBackend) {
    Telemetry::Accumulate(Telemetry::AUDIOSTREAM_BACKEND_USED,
                          CUBEB_BACKEND_UNKNOWN);
  }
}

void ReportCubebStreamInitFailure(bool aIsFirst) {
  StaticMutexAutoLock lock(sMutex);
  if (!aIsFirst && !sAudioStreamInitEverSucceeded) {
    // This machine has no audio hardware, or it's in really bad shape, don't
    // send this info, since we want CUBEB_BACKEND_INIT_FAILURE_OTHER to detect
    // failures to open multiple streams in a process over time.
    return;
  }
  Telemetry::Accumulate(Telemetry::AUDIOSTREAM_BACKEND_USED,
                        aIsFirst ? CUBEB_BACKEND_INIT_FAILURE_FIRST
                                 : CUBEB_BACKEND_INIT_FAILURE_OTHER);
}

uint32_t GetCubebPlaybackLatencyInMilliseconds() {
  StaticMutexAutoLock lock(sMutex);
  return sCubebPlaybackLatencyInMilliseconds;
}

bool CubebPlaybackLatencyPrefSet() {
  StaticMutexAutoLock lock(sMutex);
  return sCubebPlaybackLatencyPrefSet;
}

bool CubebMSGLatencyPrefSet() {
  StaticMutexAutoLock lock(sMutex);
  return sCubebMSGLatencyPrefSet;
}

uint32_t GetCubebMSGLatencyInFrames(cubeb_stream_params* params) {
  StaticMutexAutoLock lock(sMutex);
  if (sCubebMSGLatencyPrefSet) {
    MOZ_ASSERT(sCubebMSGLatencyInFrames > 0);
    return sCubebMSGLatencyInFrames;
  }

#ifdef MOZ_WIDGET_ANDROID
  return AndroidGetAudioOutputFramesPerBuffer();
#else
  cubeb* context = GetCubebContextUnlocked();
  if (!context) {
    return sCubebMSGLatencyInFrames;  // default 512
  }
  uint32_t latency_frames = 0;
  if (cubeb_get_min_latency(context, params, &latency_frames) != CUBEB_OK) {
    NS_WARNING("Could not get minimal latency from cubeb.");
    return sCubebMSGLatencyInFrames;  // default 512
  }
  return latency_frames;
#endif
}

void InitLibrary() {
  Preferences::RegisterCallbackAndCall(PrefChanged, PREF_VOLUME_SCALE);
  Preferences::RegisterCallbackAndCall(PrefChanged,
                                       PREF_CUBEB_LATENCY_PLAYBACK);
  Preferences::RegisterCallbackAndCall(PrefChanged, PREF_CUBEB_LATENCY_MSG);
  Preferences::RegisterCallback(PrefChanged, PREF_CUBEB_FORCE_SAMPLE_RATE);
  Preferences::RegisterCallbackAndCall(PrefChanged, PREF_CUBEB_BACKEND);
  Preferences::RegisterCallbackAndCall(PrefChanged, PREF_CUBEB_SANDBOX);
  if (MOZ_LOG_TEST(gCubebLog, LogLevel::Verbose)) {
    cubeb_set_log_callback(CUBEB_LOG_VERBOSE, CubebLogCallback);
  } else if (MOZ_LOG_TEST(gCubebLog, LogLevel::Error)) {
    cubeb_set_log_callback(CUBEB_LOG_NORMAL, CubebLogCallback);
  }
  // We don't want to call the callback on startup, because the pref is the
  // empty string by default ("", which means "logging disabled"). Because the
  // logging can be enabled via environment variables (MOZ_LOG="module:5"),
  // calling this callback on init would immediately re-disable the logging.
  Preferences::RegisterCallback(PrefChanged, PREF_CUBEB_LOGGING_LEVEL);
#ifndef MOZ_WIDGET_ANDROID
  AbstractThread::MainThread()->Dispatch(
      NS_NewRunnableFunction("CubebUtils::InitLibrary", &InitBrandName));
#endif
#ifdef MOZ_CUBEB_REMOTING
  if (sCubebSandbox && XRE_IsContentProcess()) {
    InitAudioIPCConnection();
  }
#endif
}

void ShutdownLibrary() {
  Preferences::UnregisterCallback(PrefChanged, PREF_VOLUME_SCALE);
  Preferences::UnregisterCallback(PrefChanged, PREF_CUBEB_SANDBOX);
  Preferences::UnregisterCallback(PrefChanged, PREF_CUBEB_BACKEND);
  Preferences::UnregisterCallback(PrefChanged, PREF_CUBEB_LATENCY_PLAYBACK);
  Preferences::UnregisterCallback(PrefChanged, PREF_CUBEB_FORCE_SAMPLE_RATE);
  Preferences::UnregisterCallback(PrefChanged, PREF_CUBEB_LATENCY_MSG);
  Preferences::UnregisterCallback(PrefChanged, PREF_CUBEB_LOGGING_LEVEL);

  StaticMutexAutoLock lock(sMutex);
  if (sCubebContext) {
    cubeb_destroy(sCubebContext);
    sCubebContext = nullptr;
  }
  sBrandName = nullptr;
  sCubebBackendName = nullptr;
  // This will ensure we don't try to re-create a context.
  sCubebState = CubebState::Shutdown;

#ifdef MOZ_CUBEB_REMOTING
  sIPCConnection = nullptr;
  ShutdownSoundServer();
#endif
}

uint32_t MaxNumberOfChannels() {
  cubeb* cubebContext = GetCubebContext();
  uint32_t maxNumberOfChannels;
  if (cubebContext && cubeb_get_max_channel_count(
                          cubebContext, &maxNumberOfChannels) == CUBEB_OK) {
    return maxNumberOfChannels;
  }

  return 0;
}

cubeb_channel_layout ConvertChannelMapToCubebLayout(uint32_t aChannelMap) {
  switch (aChannelMap) {
    case MASK_MONO:
      return CUBEB_LAYOUT_MONO;
    case MASK_MONO_LFE:
      return CUBEB_LAYOUT_MONO_LFE;
    case MASK_STEREO:
      return CUBEB_LAYOUT_STEREO;
    case MASK_STEREO_LFE:
      return CUBEB_LAYOUT_STEREO_LFE;
    case MASK_3F:
      return CUBEB_LAYOUT_3F;
    case MASK_3F_LFE:
      return CUBEB_LAYOUT_3F_LFE;
    case MASK_2F1:
      return CUBEB_LAYOUT_2F1;
    case MASK_2F1_LFE:
      return CUBEB_LAYOUT_2F1_LFE;
    case MASK_3F1:
      return CUBEB_LAYOUT_3F1;
    case MASK_3F1_LFE:
      return CUBEB_LAYOUT_3F1_LFE;
    case MASK_2F2:
      return CUBEB_LAYOUT_2F2;
    case MASK_2F2_LFE:
      return CUBEB_LAYOUT_2F2_LFE;
    case MASK_3F2:
      return CUBEB_LAYOUT_3F2;
    case MASK_3F2_LFE:
      return CUBEB_LAYOUT_3F2_LFE;
    case MASK_3F3R_LFE:
      return CUBEB_LAYOUT_3F3R_LFE;
    case MASK_3F4_LFE:
      return CUBEB_LAYOUT_3F4_LFE;
    default:
      NS_ERROR("The channel map is unsupported");
      return CUBEB_LAYOUT_UNDEFINED;
  }
}

void GetCurrentBackend(nsAString& aBackend) {
  cubeb* cubebContext = GetCubebContext();
  if (cubebContext) {
    const char* backend = cubeb_get_backend_id(cubebContext);
    if (backend) {
      aBackend.AssignASCII(backend);
      return;
    }
  }
  aBackend.AssignLiteral("unknown");
}

void GetPreferredChannelLayout(nsAString& aLayout) {
  const char* layout = InitPreferredChannelLayout()
                           ? kLayoutInfos[sPreferredChannelLayout].name
                           : "unknown";
  aLayout.AssignASCII(layout);
}

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];
}

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];
}

uint16_t ConvertCubebPreferred(cubeb_device_pref aPreferred) {
  if (aPreferred == CUBEB_DEVICE_PREF_NONE) {
    return nsIAudioDeviceInfo::PREF_NONE;
  } else 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;
}

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;
}

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(
            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);
  }
}

#ifdef MOZ_WIDGET_ANDROID
uint32_t AndroidGetAudioOutputSampleRate() {
  int32_t sample_rate = java::GeckoAppShell::GetAudioOutputSampleRate();
  MOZ_ASSERT(sample_rate > 0);
  return sample_rate;
}
uint32_t AndroidGetAudioOutputFramesPerBuffer() {
  int32_t frames = java::GeckoAppShell::GetAudioOutputFramesPerBuffer();
  MOZ_ASSERT(frames > 0);
  return frames;
}
#endif

}  // namespace CubebUtils
}  // namespace mozilla