From 36d22d82aa202bb199967e9512281e9a53db42c9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 21:33:14 +0200 Subject: Adding upstream version 115.7.0esr. Signed-off-by: Daniel Baumann --- dom/media/CubebInputStream.h | 81 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 dom/media/CubebInputStream.h (limited to 'dom/media/CubebInputStream.h') diff --git a/dom/media/CubebInputStream.h b/dom/media/CubebInputStream.h new file mode 100644 index 0000000000..a83d212896 --- /dev/null +++ b/dom/media/CubebInputStream.h @@ -0,0 +1,81 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +#ifndef DOM_MEDIA_CUBEBINPUTSTREAM_H_ +#define DOM_MEDIA_CUBEBINPUTSTREAM_H_ + +#include "CubebUtils.h" +#include "mozilla/Maybe.h" +#include "mozilla/RefPtr.h" +#include "mozilla/UniquePtr.h" +#include "nsISupportsImpl.h" + +namespace mozilla { + +// A light-weight wrapper to operate the C style Cubeb APIs for an input-only +// audio stream in a C++-friendly way. +// Limitation: Do not call these APIs in an audio callback thread. Otherwise we +// may get a deadlock. +class CubebInputStream final { + public: + ~CubebInputStream() = default; + + class Listener { + public: + NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING; + + // This will be fired on audio callback thread. + virtual long DataCallback(const void* aBuffer, long aFrames) = 0; + // This can be fired on any thread. + virtual void StateCallback(cubeb_state aState) = 0; + // This can be fired on any thread. + virtual void DeviceChangedCallback() = 0; + + protected: + Listener() = default; + virtual ~Listener() = default; + }; + + // Return a non-null pointer if the stream has been initialized + // successfully. Otherwise return a null pointer. + static UniquePtr Create(cubeb_devid aDeviceId, + uint32_t aChannels, uint32_t aRate, + bool aIsVoice, Listener* aListener); + + // Start producing audio data. + int Start(); + + // Stop producing audio data. + int Stop(); + + private: + struct CubebDestroyPolicy { + void operator()(cubeb_stream* aStream) const; + }; + CubebInputStream(already_AddRefed&& aListener, + UniquePtr&& aStream); + + void Init(); + + template + int InvokeCubeb(Function aFunction, Args&&... aArgs); + + // Static wrapper function cubeb callbacks. + static long DataCallback_s(cubeb_stream* aStream, void* aUser, + const void* aInputBuffer, void* aOutputBuffer, + long aFrames); + static void StateCallback_s(cubeb_stream* aStream, void* aUser, + cubeb_state aState); + static void DeviceChangedCallback_s(void* aUser); + + // mListener must outlive the life time of the mStream. + const RefPtr mListener; + const UniquePtr mStream; +}; + +} // namespace mozilla + +#endif // DOM_MEDIA_CUBEBINPUTSTREAM_H_ -- cgit v1.2.3