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/. */
7 #ifndef DOM_MEDIA_CUBEBINPUTSTREAM_H_
8 #define DOM_MEDIA_CUBEBINPUTSTREAM_H_
10 #include "CubebUtils.h"
11 #include "mozilla/Maybe.h"
12 #include "mozilla/RefPtr.h"
13 #include "mozilla/UniquePtr.h"
14 #include "nsISupportsImpl.h"
18 // A light-weight wrapper to operate the C style Cubeb APIs for an input-only
19 // audio stream in a C++-friendly way.
20 // Limitation: Do not call these APIs in an audio callback thread. Otherwise we
21 // may get a deadlock.
22 class CubebInputStream final
{
24 ~CubebInputStream() = default;
28 NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
;
30 // This will be fired on audio callback thread.
31 virtual long DataCallback(const void* aBuffer
, long aFrames
) = 0;
32 // This can be fired on any thread.
33 virtual void StateCallback(cubeb_state aState
) = 0;
34 // This can be fired on any thread.
35 virtual void DeviceChangedCallback() = 0;
39 virtual ~Listener() = default;
42 // Return a non-null pointer if the stream has been initialized
43 // successfully. Otherwise return a null pointer.
44 static UniquePtr
<CubebInputStream
> Create(cubeb_devid aDeviceId
,
45 uint32_t aChannels
, uint32_t aRate
,
46 bool aIsVoice
, Listener
* aListener
);
48 // Start producing audio data.
51 // Stop producing audio data.
54 // Gets the approximate stream latency in frames.
55 int Latency(uint32_t* aLatencyFrames
);
58 struct CubebDestroyPolicy
{
59 void operator()(cubeb_stream
* aStream
) const;
61 CubebInputStream(already_AddRefed
<Listener
>&& aListener
,
62 UniquePtr
<cubeb_stream
, CubebDestroyPolicy
>&& aStream
);
66 template <typename Function
, typename
... Args
>
67 int InvokeCubeb(Function aFunction
, Args
&&... aArgs
);
69 // Static wrapper function cubeb callbacks.
70 static long DataCallback_s(cubeb_stream
* aStream
, void* aUser
,
71 const void* aInputBuffer
, void* aOutputBuffer
,
73 static void StateCallback_s(cubeb_stream
* aStream
, void* aUser
,
75 static void DeviceChangedCallback_s(void* aUser
);
77 // mListener must outlive the life time of the mStream.
78 const RefPtr
<Listener
> mListener
;
79 const UniquePtr
<cubeb_stream
, CubebDestroyPolicy
> mStream
;
82 } // namespace mozilla
84 #endif // DOM_MEDIA_CUBEBINPUTSTREAM_H_