tor-browser

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

CubebUtils.h (4793B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #if !defined(CubebUtils_h_)
      8 #  define CubebUtils_h_
      9 
     10 #  include "AudioSampleFormat.h"
     11 #  include "cubeb/cubeb.h"
     12 #  include "nsISupportsImpl.h"
     13 #  include "nsString.h"
     14 
     15 #  ifdef ENABLE_SET_CUBEB_BACKEND
     16 #    include "MockCubeb.h"
     17 #  endif
     18 
     19 class AudioDeviceInfo;
     20 
     21 MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(cubeb_stream_prefs)
     22 MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(cubeb_input_processing_params)
     23 
     24 namespace mozilla {
     25 
     26 class CallbackThreadRegistry;
     27 class SharedThreadPool;
     28 
     29 namespace CubebUtils {
     30 
     31 typedef cubeb_devid AudioDeviceID;
     32 
     33 template <AudioSampleFormat N>
     34 struct ToCubebFormat {
     35  static const cubeb_sample_format value = CUBEB_SAMPLE_FLOAT32NE;
     36 };
     37 
     38 template <>
     39 struct ToCubebFormat<AUDIO_FORMAT_S16> {
     40  static const cubeb_sample_format value = CUBEB_SAMPLE_S16NE;
     41 };
     42 
     43 nsCString ProcessingParamsToString(cubeb_input_processing_params aParams);
     44 
     45 class CubebHandle {
     46 public:
     47  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CubebHandle)
     48  explicit CubebHandle(cubeb* aCubeb) : mCubeb(aCubeb) {
     49    MOZ_RELEASE_ASSERT(mCubeb);
     50  };
     51  CubebHandle(const CubebHandle&) = delete;
     52  cubeb* Context() const { return mCubeb.get(); }
     53 
     54 private:
     55  struct CubebDeletePolicy {
     56    void operator()(cubeb* aCubeb) { cubeb_destroy(aCubeb); }
     57  };
     58  const UniquePtr<cubeb, CubebDeletePolicy> mCubeb;
     59  ~CubebHandle() = default;
     60 };
     61 
     62 // Initialize Audio Library. Some Audio backends require initializing the
     63 // library before using it.
     64 void InitLibrary();
     65 
     66 // Shutdown Audio Library. Some Audio backends require shutting down the
     67 // library after using it.
     68 void ShutdownLibrary();
     69 
     70 bool SandboxEnabled();
     71 
     72 // A thread pool containing only one thread to execute the cubeb operations. We
     73 // should always use this thread to init, destroy, start, or stop cubeb streams,
     74 // to avoid data racing or deadlock issues across platforms.
     75 already_AddRefed<SharedThreadPool> GetCubebOperationThread();
     76 
     77 // Returns the maximum number of channels supported by the audio hardware.
     78 uint32_t MaxNumberOfChannels();
     79 
     80 // Get the sample rate the hardware/mixer runs at. Thread safe.
     81 uint32_t PreferredSampleRate(bool aShouldResistFingerprinting);
     82 
     83 // Initialize a cubeb stream. A pass through wrapper for cubeb_stream_init,
     84 // that can simulate streams that are very slow to start, by setting the pref
     85 // media.cubeb.slow_stream_init_ms.
     86 int CubebStreamInit(cubeb* context, cubeb_stream** stream,
     87                    char const* stream_name, cubeb_devid input_device,
     88                    cubeb_stream_params* input_stream_params,
     89                    cubeb_devid output_device,
     90                    cubeb_stream_params* output_stream_params,
     91                    uint32_t latency_frames, cubeb_data_callback data_callback,
     92                    cubeb_state_callback state_callback, void* user_ptr);
     93 
     94 enum Side { Input = 1 << 0, Output = 1 << 1 };
     95 
     96 double GetVolumeScale();
     97 bool GetFirstStream();
     98 RefPtr<CubebHandle> GetCubeb();
     99 void ReportCubebStreamInitFailure(bool aIsFirstStream);
    100 void ReportCubebBackendUsed();
    101 uint32_t GetCubebPlaybackLatencyInMilliseconds();
    102 uint32_t GetCubebMTGLatencyInFrames(cubeb_stream_params* params);
    103 bool CubebLatencyPrefSet();
    104 void GetCurrentBackend(nsAString& aBackend);
    105 cubeb_stream_prefs GetDefaultStreamPrefs(cubeb_device_type aType);
    106 char* GetForcedOutputDevice();
    107 // No-op on all platforms but Android, where it tells the device's AudioManager
    108 // to switch to "communication mode", which might change audio routing,
    109 // bluetooth communication type, etc.
    110 void SetInCommunication(bool aInCommunication);
    111 // Returns true if the output streams should be routed like a stream containing
    112 // voice data, and not generic audio. This can influence audio processing and
    113 // device selection.
    114 bool RouteOutputAsVoice();
    115 // Returns, in seconds, the roundtrip latency Gecko thinks there is between the
    116 // default input and output devices. This is for diagnosing purposes, the
    117 // latency figures are best used directly from the cubeb streams themselves, as
    118 // the devices being used matter. This is blocking.
    119 bool EstimatedLatencyDefaultDevices(
    120    double* aMean, double* aStdDev,
    121    Side aSide = static_cast<Side>(Side::Input | Side::Output));
    122 
    123 #  ifdef MOZ_WIDGET_ANDROID
    124 int32_t AndroidGetAudioOutputSampleRate();
    125 int32_t AndroidGetAudioOutputFramesPerBuffer();
    126 #  endif
    127 
    128 #  if defined(ENABLE_TESTS) || defined(FUZZING)
    129 void ForceSetCubebContext(cubeb* aCubebContext);
    130 void ForceUnsetCubebContext();
    131 #  endif
    132 }  // namespace CubebUtils
    133 }  // namespace mozilla
    134 
    135 #endif  // CubebUtils_h_