Backed out changeset 06f41c22f3a6 (bug 1888460) for causing linux xpcshell failures...
[gecko.git] / dom / media / CubebInputStream.h
bloba35924a976c337bc8aa5638c8bc4335fb92f02f3
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"
16 namespace mozilla {
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 {
23 public:
24 ~CubebInputStream() = default;
26 class Listener {
27 public:
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;
37 protected:
38 Listener() = default;
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.
49 int Start();
51 // Stop producing audio data.
52 int Stop();
54 // Gets the approximate stream latency in frames.
55 int Latency(uint32_t* aLatencyFrames);
57 private:
58 struct CubebDestroyPolicy {
59 void operator()(cubeb_stream* aStream) const;
61 CubebInputStream(already_AddRefed<Listener>&& aListener,
62 UniquePtr<cubeb_stream, CubebDestroyPolicy>&& aStream);
64 void Init();
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,
72 long aFrames);
73 static void StateCallback_s(cubeb_stream* aStream, void* aUser,
74 cubeb_state aState);
75 static void DeviceChangedCallback_s(void* aUser);
77 // mListener must outlive the life time of the mStream.
78 const RefPtr<Listener> mListener;
79 // So must mCubeb (mStream has a bare pointer to cubeb).
80 const RefPtr<CubebUtils::CubebHandle> mCubeb;
81 const UniquePtr<cubeb_stream, CubebDestroyPolicy> mStream;
84 } // namespace mozilla
86 #endif // DOM_MEDIA_CUBEBINPUTSTREAM_H_