tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

OSXRunLoopSingleton.cpp (1442B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #include "OSXRunLoopSingleton.h"
      7 
      8 #include <AudioUnit/AudioUnit.h>
      9 #include <CoreAudio/AudioHardware.h>
     10 #include <CoreAudio/HostTime.h>
     11 #include <CoreFoundation/CoreFoundation.h>
     12 #include <mozilla/StaticMutex.h>
     13 
     14 static bool gRunLoopSet = false;
     15 static mozilla::StaticMutex gMutex MOZ_UNANNOTATED;
     16 
     17 void mozilla_set_coreaudio_notification_runloop_if_needed() {
     18  mozilla::StaticMutexAutoLock lock(gMutex);
     19  if (gRunLoopSet) {
     20    return;
     21  }
     22 
     23  /* This is needed so that AudioUnit listeners get called on this thread, and
     24   * not the main thread. If we don't do that, they are not called, or a crash
     25   * occur, depending on the OSX version. */
     26  AudioObjectPropertyAddress runloop_address = {
     27      kAudioHardwarePropertyRunLoop, kAudioObjectPropertyScopeGlobal,
     28      kAudioObjectPropertyElementMaster};
     29 
     30  CFRunLoopRef run_loop = nullptr;
     31 
     32  OSStatus r;
     33  r = AudioObjectSetPropertyData(kAudioObjectSystemObject, &runloop_address, 0,
     34                                 NULL, sizeof(CFRunLoopRef), &run_loop);
     35  if (r != noErr) {
     36    NS_WARNING(
     37        "Could not make global CoreAudio notifications use their own thread.");
     38  }
     39 
     40  gRunLoopSet = true;
     41 }