tor-browser

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

CubebDeviceEnumerator.h (3388B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 #ifndef CUBEBDEVICEENUMERATOR_H_
      6 #define CUBEBDEVICEENUMERATOR_H_
      7 
      8 #include "AudioDeviceInfo.h"
      9 #include "MediaEventSource.h"
     10 #include "cubeb/cubeb.h"
     11 #include "mozilla/Mutex.h"
     12 #include "nsTArray.h"
     13 
     14 namespace mozilla {
     15 
     16 namespace media {
     17 template <typename T>
     18 class Refcountable;
     19 }
     20 
     21 // This class implements a cache for accessing the audio device list.
     22 // It can be accessed on any thread.
     23 class CubebDeviceEnumerator final {
     24 public:
     25  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CubebDeviceEnumerator)
     26 
     27  static CubebDeviceEnumerator* GetInstance();
     28  static void Shutdown();
     29  using AudioDeviceSet = media::Refcountable<nsTArray<RefPtr<AudioDeviceInfo>>>;
     30  // This method returns a list of all the input audio devices
     31  // (sources) available on this machine.
     32  // This method is safe to call from all threads.
     33  RefPtr<const AudioDeviceSet> EnumerateAudioInputDevices();
     34  // Similar for the audio audio devices (sinks). Also thread safe.
     35  RefPtr<const AudioDeviceSet> EnumerateAudioOutputDevices();
     36  // From a device name, return the info for this device, if it's a valid name,
     37  // or nullptr otherwise.
     38  // This method is safe to call from any thread.
     39  enum class Side {
     40    INPUT,
     41    OUTPUT,
     42  };
     43  already_AddRefed<AudioDeviceInfo> DeviceInfoFromName(const nsString& aName,
     44                                                       Side aSide);
     45  // Event source to listen for changes to the audio input device list on.
     46  MediaEventSource<void>& OnAudioInputDeviceListChange() {
     47    return mOnInputDeviceListChange;
     48  }
     49 
     50  // Event source to listen for changes to the audio output device list on.
     51  MediaEventSource<void>& OnAudioOutputDeviceListChange() {
     52    return mOnOutputDeviceListChange;
     53  }
     54 
     55  // Return the default device for a particular side.
     56  RefPtr<AudioDeviceInfo> DefaultDevice(Side aSide);
     57 
     58 private:
     59  CubebDeviceEnumerator();
     60  ~CubebDeviceEnumerator();
     61  // Static functions called by cubeb when the audio device list changes
     62  // (i.e. when a new device is made available, or non-available). This
     63  // simply calls `AudioDeviceListChanged` below.
     64  static void InputAudioDeviceListChanged_s(cubeb* aContext, void* aUser);
     65  static void OutputAudioDeviceListChanged_s(cubeb* aContext, void* aUser);
     66  // Invalidates the cached audio input device list, can be called on any
     67  // thread.
     68  void AudioDeviceListChanged(Side aSide);
     69  RefPtr<const AudioDeviceSet> EnumerateAudioDevices(Side aSide);
     70  // Synchronize access to mInputDevices and mOutputDevices;
     71  Mutex mMutex MOZ_UNANNOTATED;
     72  RefPtr<const AudioDeviceSet> mInputDevices;
     73  RefPtr<const AudioDeviceSet> mOutputDevices;
     74  // If mManual*Invalidation is true, then it is necessary to query the device
     75  // list each time instead of relying on automatic invalidation of the cache by
     76  // cubeb itself. Set in the constructor and then can be access on any thread.
     77  bool mManualInputInvalidation;
     78  bool mManualOutputInvalidation;
     79  MediaEventProducer<void> mOnInputDeviceListChange;
     80  MediaEventProducer<void> mOnOutputDeviceListChange;
     81 };
     82 
     83 typedef CubebDeviceEnumerator Enumerator;
     84 typedef CubebDeviceEnumerator::Side EnumeratorSide;
     85 }  // namespace mozilla
     86 
     87 #endif  // CUBEBDEVICEENUMERATOR_H_