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
)
22 class CallbackThreadRegistry
;
24 namespace CubebUtils
{
26 typedef cubeb_devid AudioDeviceID
;
28 template <AudioSampleFormat N
>
29 struct ToCubebFormat
{
30 static const cubeb_sample_format value
= CUBEB_SAMPLE_FLOAT32NE
;
34 struct ToCubebFormat
<AUDIO_FORMAT_S16
> {
35 static const cubeb_sample_format value
= CUBEB_SAMPLE_S16NE
;
40 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CubebHandle
)
41 explicit CubebHandle(cubeb
* aCubeb
) : mCubeb(aCubeb
) {
42 MOZ_RELEASE_ASSERT(mCubeb
);
44 CubebHandle(const CubebHandle
&) = delete;
45 cubeb
* Context() const { return mCubeb
.get(); }
48 struct CubebDeletePolicy
{
49 void operator()(cubeb
* aCubeb
) { cubeb_destroy(aCubeb
); }
51 const UniquePtr
<cubeb
, CubebDeletePolicy
> mCubeb
;
52 ~CubebHandle() = default;
55 // Initialize Audio Library. Some Audio backends require initializing the
56 // library before using it.
59 // Shutdown Audio Library. Some Audio backends require shutting down the
60 // library after using it.
61 void ShutdownLibrary();
63 bool SandboxEnabled();
65 // Returns the maximum number of channels supported by the audio hardware.
66 uint32_t MaxNumberOfChannels();
68 // Get the sample rate the hardware/mixer runs at. Thread safe.
69 uint32_t PreferredSampleRate(bool aShouldResistFingerprinting
);
71 // Initialize a cubeb stream. A pass through wrapper for cubeb_stream_init,
72 // that can simulate streams that are very slow to start, by setting the pref
73 // media.cubeb.slow_stream_init_ms.
74 int CubebStreamInit(cubeb
* context
, cubeb_stream
** stream
,
75 char const* stream_name
, cubeb_devid input_device
,
76 cubeb_stream_params
* input_stream_params
,
77 cubeb_devid output_device
,
78 cubeb_stream_params
* output_stream_params
,
79 uint32_t latency_frames
, cubeb_data_callback data_callback
,
80 cubeb_state_callback state_callback
, void* user_ptr
);
82 enum Side
{ Input
, Output
};
84 double GetVolumeScale();
85 bool GetFirstStream();
86 RefPtr
<CubebHandle
> GetCubeb();
87 void ReportCubebStreamInitFailure(bool aIsFirstStream
);
88 void ReportCubebBackendUsed();
89 uint32_t GetCubebPlaybackLatencyInMilliseconds();
90 uint32_t GetCubebMTGLatencyInFrames(cubeb_stream_params
* params
);
91 bool CubebLatencyPrefSet();
92 void GetCurrentBackend(nsAString
& aBackend
);
93 cubeb_stream_prefs
GetDefaultStreamPrefs(cubeb_device_type aType
);
94 char* GetForcedOutputDevice();
95 // No-op on all platforms but Android, where it tells the device's AudioManager
96 // to switch to "communication mode", which might change audio routing,
97 // bluetooth communication type, etc.
98 void SetInCommunication(bool aInCommunication
);
99 // Returns true if the output streams should be routed like a stream containing
100 // voice data, and not generic audio. This can influence audio processing and
102 bool RouteOutputAsVoice();
103 // Returns, in seconds, the roundtrip latency Gecko thinks there is between the
104 // default input and output devices. This is for diagnosing purposes, the
105 // latency figures are best used directly from the cubeb streams themselves, as
106 // the devices being used matter. This is blocking.
107 bool EstimatedRoundTripLatencyDefaultDevices(double* aMean
, double* aStdDev
);
109 # ifdef MOZ_WIDGET_ANDROID
110 int32_t AndroidGetAudioOutputSampleRate();
111 int32_t AndroidGetAudioOutputFramesPerBuffer();
114 # ifdef ENABLE_SET_CUBEB_BACKEND
115 void ForceSetCubebContext(cubeb
* aCubebContext
);
117 } // namespace CubebUtils
118 } // namespace mozilla
120 #endif // CubebUtils_h_