Bug 1430807 - Cap the MediaStreamGraph sample-rate to 48kHz.
MozReview-Commit-ID: 9Tck0pJ2fmm
--- a/dom/media/CubebUtils.cpp
+++ b/dom/media/CubebUtils.cpp
@@ -317,16 +317,25 @@ uint32_t PreferredSampleRate()
{
if (!InitPreferredSampleRate()) {
return 44100;
}
MOZ_ASSERT(sPreferredSampleRate);
return sPreferredSampleRate;
}
+uint32_t PreferredSampleRateClamped()
+{
+ if (!InitPreferredSampleRate()) {
+ return 44100;
+ }
+ MOZ_ASSERT(sPreferredSampleRate);
+ return std::min<int>(sPreferredSampleRate, 48000);
+}
+
bool InitPreferredChannelLayout()
{
{
StaticMutexAutoLock lock(sMutex);
if (sPreferredChannelLayout != 0) {
return true;
}
}
--- a/dom/media/CubebUtils.h
+++ b/dom/media/CubebUtils.h
@@ -25,16 +25,22 @@ void InitLibrary();
void ShutdownLibrary();
// Returns the maximum number of channels supported by the audio hardware.
uint32_t MaxNumberOfChannels();
// Get the sample rate the hardware/mixer runs at. Thread safe.
uint32_t PreferredSampleRate();
+// Same as the function above, but clamps the result to 48kHz, so that
+// webrtc.org code does not error out when devices are set to higher
+// sample-rates.
+// This is to be used for the MediaStreamGraph, not HTMLMediaElement.
+uint32_t PreferredSampleRateClamped();
+
// Get the bit mask of the connected audio device's preferred layout.
uint32_t PreferredChannelMap(uint32_t aChannels);
enum Side {
Input,
Output
};
--- a/dom/media/GraphDriver.cpp
+++ b/dom/media/GraphDriver.cpp
@@ -600,17 +600,17 @@ AudioCallbackDriver::Init()
cubeb_stream_params output;
cubeb_stream_params input;
uint32_t latency_frames;
bool firstStream = CubebUtils::GetFirstStream();
MOZ_ASSERT(!NS_IsMainThread(),
"This is blocking and should never run on the main thread.");
- mSampleRate = output.rate = CubebUtils::PreferredSampleRate();
+ mSampleRate = output.rate = GraphImpl()->GraphRate();
if (AUDIO_OUTPUT_FORMAT == AUDIO_FORMAT_S16) {
output.format = CUBEB_SAMPLE_S16NE;
} else {
output.format = CUBEB_SAMPLE_FLOAT32NE;
}
// Query and set the number of channels this AudioCallbackDriver will use.
--- a/dom/media/MediaStreamGraph.cpp
+++ b/dom/media/MediaStreamGraph.cpp
@@ -3676,17 +3676,17 @@ MediaStreamGraph::GetInstance(MediaStrea
if (aWindow) {
nsCOMPtr<nsIGlobalObject> parentObject = do_QueryInterface(aWindow);
mainThread = parentObject->AbstractMainThreadFor(TaskCategory::Other);
} else {
// Uncommon case, only for some old configuration of webspeech.
mainThread = AbstractThread::MainThread();
}
graph = new MediaStreamGraphImpl(aGraphDriverRequested,
- CubebUtils::PreferredSampleRate(),
+ CubebUtils::PreferredSampleRateClamped(),
mainThread);
uint32_t hashkey = WindowToHash(aWindow);
gGraphs.Put(hashkey, graph);
LOG(LogLevel::Debug,
("Starting up MediaStreamGraph %p for window %p", graph, aWindow));
}
--- a/dom/media/webaudio/AudioContext.cpp
+++ b/dom/media/webaudio/AudioContext.cpp
@@ -118,17 +118,17 @@ NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(
NS_INTERFACE_MAP_ENTRY(nsIMemoryReporter)
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
static float GetSampleRateForAudioContext(bool aIsOffline, float aSampleRate)
{
if (aIsOffline) {
return aSampleRate;
} else {
- return static_cast<float>(CubebUtils::PreferredSampleRate());
+ return static_cast<float>(CubebUtils::PreferredSampleRateClamped());
}
}
AudioContext::AudioContext(nsPIDOMWindowInner* aWindow,
bool aIsOffline,
uint32_t aNumberOfChannels,
uint32_t aLength,
float aSampleRate)