tor-browser

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

CubebInputStream.h (3089B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=2 et sw=2 tw=80: */
      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 https://mozilla.org/MPL/2.0/. */
      6 
      7 #ifndef DOM_MEDIA_CUBEBINPUTSTREAM_H_
      8 #define DOM_MEDIA_CUBEBINPUTSTREAM_H_
      9 
     10 #include "CubebUtils.h"
     11 #include "mozilla/RefPtr.h"
     12 #include "mozilla/UniquePtr.h"
     13 #include "nsISupportsImpl.h"
     14 
     15 namespace mozilla {
     16 
     17 // A light-weight wrapper to operate the C style Cubeb APIs for an input-only
     18 // audio stream in a C++-friendly way.
     19 // Limitation: Do not call these APIs in an audio callback thread. Otherwise we
     20 // may get a deadlock.
     21 class CubebInputStream final {
     22 public:
     23  ~CubebInputStream() = default;
     24 
     25  class Listener {
     26   public:
     27    NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING;
     28 
     29    // This will be fired on audio callback thread.
     30    virtual long DataCallback(const void* aBuffer, long aFrames) = 0;
     31    // This can be fired on any thread.
     32    virtual void StateCallback(cubeb_state aState) = 0;
     33    // This can be fired on any thread.
     34    virtual void DeviceChangedCallback() = 0;
     35 
     36   protected:
     37    Listener() = default;
     38    virtual ~Listener() = default;
     39  };
     40 
     41  // Return a non-null pointer if the stream has been initialized
     42  // successfully. Otherwise return a null pointer.
     43  static UniquePtr<CubebInputStream> Create(cubeb_devid aDeviceId,
     44                                            uint32_t aChannels, uint32_t aRate,
     45                                            bool aIsVoice, Listener* aListener);
     46 
     47  // Start producing audio data.
     48  int Start();
     49 
     50  // Stop producing audio data.
     51  int Stop();
     52 
     53  // Apply the given processing params.
     54  int SetProcessingParams(cubeb_input_processing_params aParams);
     55 
     56  // Gets the approximate stream latency in frames.
     57  int Latency(uint32_t* aLatencyFrames);
     58 
     59 private:
     60  struct CubebDestroyPolicy {
     61    void operator()(cubeb_stream* aStream) const;
     62  };
     63  CubebInputStream(already_AddRefed<Listener>&& aListener,
     64                   already_AddRefed<CubebUtils::CubebHandle>&& aCubeb,
     65                   UniquePtr<cubeb_stream, CubebDestroyPolicy>&& aStream);
     66 
     67  void Init();
     68 
     69  template <typename Function, typename... Args>
     70  int InvokeCubeb(Function aFunction, Args&&... aArgs);
     71 
     72  // Static wrapper function cubeb callbacks.
     73  static long DataCallback_s(cubeb_stream* aStream, void* aUser,
     74                             const void* aInputBuffer, void* aOutputBuffer,
     75                             long aFrames);
     76  static void StateCallback_s(cubeb_stream* aStream, void* aUser,
     77                              cubeb_state aState);
     78  static void DeviceChangedCallback_s(void* aUser);
     79 
     80  // mListener must outlive the life time of the mStream.
     81  const RefPtr<Listener> mListener;
     82  // So must mCubeb (mStream has a bare pointer to cubeb).
     83  const RefPtr<CubebUtils::CubebHandle> mCubeb;
     84  const UniquePtr<cubeb_stream, CubebDestroyPolicy> mStream;
     85 };
     86 
     87 }  // namespace mozilla
     88 
     89 #endif  // DOM_MEDIA_CUBEBINPUTSTREAM_H_