Bug 1262746: P2. Use AudioConvert in AudioStream to downmix samples. r?jwwang
MozReview-Commit-ID: 4jgKR8xw1dc
--- a/dom/media/AudioStream.cpp
+++ b/dom/media/AudioStream.cpp
@@ -13,16 +13,17 @@
#include "mozilla/Monitor.h"
#include "mozilla/Mutex.h"
#include "mozilla/Snprintf.h"
#include <algorithm>
#include "mozilla/Telemetry.h"
#include "CubebUtils.h"
#include "nsPrintfCString.h"
#include "gfxPrefs.h"
+#include "AudioConverter.h"
namespace mozilla {
#undef LOG
#undef LOGW
LazyLogModule gAudioStreamLog("AudioStream");
// For simple logs
@@ -347,16 +348,19 @@ AudioStream::Init(uint32_t aNumChannels,
if (params.stream_type == CUBEB_STREAM_TYPE_MAX) {
return NS_ERROR_INVALID_ARG;
}
#endif
params.format = ToCubebFormat<AUDIO_OUTPUT_FORMAT>::value;
mAudioClock.Init();
+ AudioConfig inConfig(mChannels, mInRate);
+ AudioConfig outConfig(mOutChannels, mOutRate);
+ mAudioConverter = MakeUnique<AudioConverter>(inConfig, outConfig);
return OpenCubeb(params);
}
// This code used to live inside AudioStream::Init(), but on Mac (others?)
// it has been known to take 300-800 (or even 8500) ms to execute(!)
nsresult
AudioStream::OpenCubeb(cubeb_stream_params &aParams)
{
@@ -552,20 +556,20 @@ AudioStream::Downmix(Chunk* aChunk)
LOGW("mismatched sample %u, mInRate=%u", aChunk->Rate(), mInRate);
return false;
}
if (aChunk->Channels() > 8) {
return false;
}
- if (aChunk->Channels() > 2 && aChunk->Channels() <= 8) {
- DownmixAudioToStereo(aChunk->GetWritable(),
- aChunk->Channels(),
- aChunk->Frames());
+ if (aChunk->Channels() > 2) {
+ MOZ_ASSERT(mAudioConverter);
+ mAudioConverter->Process(aChunk->GetWritable(),
+ aChunk->Channels() * aChunk->Frames());
}
if (aChunk->Channels() >= 2 && mIsMonoAudioEnabled) {
DownmixStereoToMono(aChunk->GetWritable(), aChunk->Frames());
}
return true;
}
--- a/dom/media/AudioStream.h
+++ b/dom/media/AudioStream.h
@@ -24,16 +24,18 @@ struct CubebDestroyPolicy
{
void operator()(cubeb_stream* aStream) const {
cubeb_stream_destroy(aStream);
}
};
class AudioStream;
class FrameHistory;
+class AudioConfig;
+class AudioConverter;
class AudioClock
{
public:
explicit AudioClock(AudioStream* aStream);
// Initialize the clock with the current AudioStream. Need to be called
// before querying the clock. Called on the audio thread.
void Init();
@@ -362,17 +364,19 @@ private:
STOPPED, // Stopped by a call to Pause().
DRAINED, // StateCallback has indicated that the drain is complete.
ERRORED, // Stream disabled due to an internal error.
SHUTDOWN // Shutdown has been called
};
StreamState mState;
bool mIsFirst;
- // Get this value from the preferece, if true, we would downmix the stereo.
+ // Get this value from the preference, if true, we would downmix the stereo.
bool mIsMonoAudioEnabled;
DataSource& mDataSource;
+
+ UniquePtr<AudioConverter> mAudioConverter;
};
} // namespace mozilla
#endif