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"
15 class AudioDeviceInfo
;
17 MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(cubeb_stream_prefs
)
21 class AudioThreadRegistry
;
23 namespace CubebUtils
{
25 typedef cubeb_devid AudioDeviceID
;
27 template <AudioSampleFormat N
>
28 struct ToCubebFormat
{
29 static const cubeb_sample_format value
= CUBEB_SAMPLE_FLOAT32NE
;
33 struct ToCubebFormat
<AUDIO_FORMAT_S16
> {
34 static const cubeb_sample_format value
= CUBEB_SAMPLE_S16NE
;
37 // Initialize Audio Library. Some Audio backends require initializing the
38 // library before using it.
41 // Shutdown Audio Library. Some Audio backends require shutting down the
42 // library after using it.
43 void ShutdownLibrary();
45 bool SandboxEnabled();
47 // Returns the global instance of AudioThreadRegistry. Initialized and
48 // destroying in Init/ShutdownLibrary(), and safe from all threads.
49 AudioThreadRegistry
* GetAudioThreadRegistry();
51 // Returns the maximum number of channels supported by the audio hardware.
52 uint32_t MaxNumberOfChannels();
54 // Get the sample rate the hardware/mixer runs at. Thread safe.
55 uint32_t PreferredSampleRate();
57 // Initialize a cubeb stream. A pass through wrapper for cubeb_stream_init,
58 // that can simulate streams that are very slow to start, by setting the pref
59 // media.cubeb.slow_stream_init_ms.
60 int CubebStreamInit(cubeb
* context
, cubeb_stream
** stream
,
61 char const* stream_name
, cubeb_devid input_device
,
62 cubeb_stream_params
* input_stream_params
,
63 cubeb_devid output_device
,
64 cubeb_stream_params
* output_stream_params
,
65 uint32_t latency_frames
, cubeb_data_callback data_callback
,
66 cubeb_state_callback state_callback
, void* user_ptr
);
68 enum Side
{ Input
, Output
};
70 double GetVolumeScale();
71 bool GetFirstStream();
72 cubeb
* GetCubebContext();
73 void ReportCubebStreamInitFailure(bool aIsFirstStream
);
74 void ReportCubebBackendUsed();
75 uint32_t GetCubebPlaybackLatencyInMilliseconds();
76 uint32_t GetCubebMTGLatencyInFrames(cubeb_stream_params
* params
);
77 bool CubebLatencyPrefSet();
78 void GetCurrentBackend(nsAString
& aBackend
);
79 cubeb_stream_prefs
GetDefaultStreamPrefs(cubeb_device_type aType
);
80 char* GetForcedOutputDevice();
81 // No-op on all platforms but Android, where it tells the device's AudioManager
82 // to switch to "communication mode", which might change audio routing,
83 // bluetooth communication type, etc.
84 void SetInCommunication(bool aInCommunication
);
85 // Returns true if the output streams should be routed like a stream containing
86 // voice data, and not generic audio. This can influence audio processing and
88 bool RouteOutputAsVoice();
89 // Returns, in seconds, the roundtrip latency Gecko thinks there is between the
90 // default input and output devices. This is for diagnosing purposes, the
91 // latency figures are best used directly from the cubeb streams themselves, as
92 // the devices being used matter. This is blocking.
93 bool EstimatedRoundTripLatencyDefaultDevices(double* aMean
, double* aStdDev
);
95 # ifdef MOZ_WIDGET_ANDROID
96 uint32_t AndroidGetAudioOutputSampleRate();
97 uint32_t AndroidGetAudioOutputFramesPerBuffer();
100 # ifdef ENABLE_SET_CUBEB_BACKEND
101 void ForceSetCubebContext(cubeb
* aCubebContext
);
103 } // namespace CubebUtils
104 } // namespace mozilla
106 #endif // CubebUtils_h_