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/. */
7 #if !defined(CubebUtils_h_)
10 # include "cubeb/cubeb.h"
12 # include "AudioSampleFormat.h"
13 # include "nsString.h"
14 # include "nsISupportsImpl.h"
16 class AudioDeviceInfo
;
18 MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(cubeb_stream_prefs
)
19 MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(cubeb_input_processing_params
)
23 class CallbackThreadRegistry
;
24 class SharedThreadPool
;
26 namespace CubebUtils
{
28 typedef cubeb_devid AudioDeviceID
;
30 template <AudioSampleFormat N
>
31 struct ToCubebFormat
{
32 static const cubeb_sample_format value
= CUBEB_SAMPLE_FLOAT32NE
;
36 struct ToCubebFormat
<AUDIO_FORMAT_S16
> {
37 static const cubeb_sample_format value
= CUBEB_SAMPLE_S16NE
;
40 nsCString
ProcessingParamsToString(cubeb_input_processing_params aParams
);
44 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CubebHandle
)
45 explicit CubebHandle(cubeb
* aCubeb
) : mCubeb(aCubeb
) {
46 MOZ_RELEASE_ASSERT(mCubeb
);
48 CubebHandle(const CubebHandle
&) = delete;
49 cubeb
* Context() const { return mCubeb
.get(); }
52 struct CubebDeletePolicy
{
53 void operator()(cubeb
* aCubeb
) { cubeb_destroy(aCubeb
); }
55 const UniquePtr
<cubeb
, CubebDeletePolicy
> mCubeb
;
56 ~CubebHandle() = default;
59 // Initialize Audio Library. Some Audio backends require initializing the
60 // library before using it.
63 // Shutdown Audio Library. Some Audio backends require shutting down the
64 // library after using it.
65 void ShutdownLibrary();
67 bool SandboxEnabled();
69 // A thread pool containing only one thread to execute the cubeb operations. We
70 // should always use this thread to init, destroy, start, or stop cubeb streams,
71 // to avoid data racing or deadlock issues across platforms.
72 already_AddRefed
<SharedThreadPool
> GetCubebOperationThread();
74 // Returns the maximum number of channels supported by the audio hardware.
75 uint32_t MaxNumberOfChannels();
77 // Get the sample rate the hardware/mixer runs at. Thread safe.
78 uint32_t PreferredSampleRate(bool aShouldResistFingerprinting
);
80 // Initialize a cubeb stream. A pass through wrapper for cubeb_stream_init,
81 // that can simulate streams that are very slow to start, by setting the pref
82 // media.cubeb.slow_stream_init_ms.
83 int CubebStreamInit(cubeb
* context
, cubeb_stream
** stream
,
84 char const* stream_name
, cubeb_devid input_device
,
85 cubeb_stream_params
* input_stream_params
,
86 cubeb_devid output_device
,
87 cubeb_stream_params
* output_stream_params
,
88 uint32_t latency_frames
, cubeb_data_callback data_callback
,
89 cubeb_state_callback state_callback
, void* user_ptr
);
91 enum Side
{ Input
= 1 << 0, Output
= 1 << 1 };
93 double GetVolumeScale();
94 bool GetFirstStream();
95 RefPtr
<CubebHandle
> GetCubeb();
96 void ReportCubebStreamInitFailure(bool aIsFirstStream
);
97 void ReportCubebBackendUsed();
98 uint32_t GetCubebPlaybackLatencyInMilliseconds();
99 uint32_t GetCubebMTGLatencyInFrames(cubeb_stream_params
* params
);
100 bool CubebLatencyPrefSet();
101 void GetCurrentBackend(nsAString
& aBackend
);
102 cubeb_stream_prefs
GetDefaultStreamPrefs(cubeb_device_type aType
);
103 char* GetForcedOutputDevice();
104 // No-op on all platforms but Android, where it tells the device's AudioManager
105 // to switch to "communication mode", which might change audio routing,
106 // bluetooth communication type, etc.
107 void SetInCommunication(bool aInCommunication
);
108 // Returns true if the output streams should be routed like a stream containing
109 // voice data, and not generic audio. This can influence audio processing and
111 bool RouteOutputAsVoice();
112 // Returns, in seconds, the roundtrip latency Gecko thinks there is between the
113 // default input and output devices. This is for diagnosing purposes, the
114 // latency figures are best used directly from the cubeb streams themselves, as
115 // the devices being used matter. This is blocking.
116 bool EstimatedLatencyDefaultDevices(
117 double* aMean
, double* aStdDev
,
118 Side aSide
= static_cast<Side
>(Side::Input
| Side::Output
));
120 # ifdef MOZ_WIDGET_ANDROID
121 int32_t AndroidGetAudioOutputSampleRate();
122 int32_t AndroidGetAudioOutputFramesPerBuffer();
125 # ifdef ENABLE_SET_CUBEB_BACKEND
126 void ForceSetCubebContext(cubeb
* aCubebContext
);
128 } // namespace CubebUtils
129 } // namespace mozilla
131 #endif // CubebUtils_h_