Bug 816143 - Teach AudioStream::EnsureTimeStretcherInitialized() about infallible malloc. r=kinetik
authorPaul Adenot <paul@paul.cx>
Thu, 29 Nov 2012 15:40:57 +0100
changeset 123594 ccabd35aa7d43c61bb87b570587e4ff8d317daef
parent 123593 5d2fb47b62788c819bcecd9221f3b16c8c1e4a1f
child 123595 cbeb16996226570632aa7f8a470a6f228e258e5b
push id2151
push userlsblakk@mozilla.com
push dateTue, 19 Feb 2013 18:06:57 +0000
treeherdermozilla-beta@4952e88741ec [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskinetik
bugs816143
milestone20.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 816143 - Teach AudioStream::EnsureTimeStretcherInitialized() about infallible malloc. r=kinetik
content/media/AudioStream.cpp
content/media/AudioStream.h
--- a/content/media/AudioStream.cpp
+++ b/content/media/AudioStream.cpp
@@ -214,62 +214,58 @@ void AudioStream::ShutdownLibrary()
   }
 #endif
 }
 
 AudioStream::~AudioStream()
 {
 }
 
-bool AudioStream::EnsureTimeStretcherInitialized()
+void AudioStream::EnsureTimeStretcherInitialized()
 {
-  if (mTimeStretcher)
-    return true;
-  mTimeStretcher = new soundtouch::SoundTouch();
   if (!mTimeStretcher) {
-    return false;
+    mTimeStretcher = new soundtouch::SoundTouch();
+    mTimeStretcher->setSampleRate(mInRate);
+    mTimeStretcher->setChannels(mChannels);
+    mTimeStretcher->setPitch(1.0);
   }
-  mTimeStretcher->setSampleRate(mInRate);
-  mTimeStretcher->setChannels(mChannels);
-  mTimeStretcher->setPitch(1.0);
-  return true;
 }
 
 nsresult AudioStream::SetPlaybackRate(double aPlaybackRate)
 {
   NS_ASSERTION(aPlaybackRate > 0.0,
                "Can't handle negative or null playbackrate in the AudioStream.");
   // Avoid instantiating the resampler if we are not changing the playback rate.
   if (aPlaybackRate == mAudioClock.GetPlaybackRate()) {
     return NS_OK;
   }
   mAudioClock.SetPlaybackRate(aPlaybackRate);
   mOutRate = mInRate / aPlaybackRate;
-  if (!EnsureTimeStretcherInitialized()) {
-    return NS_ERROR_FAILURE;
-  }
+
+  EnsureTimeStretcherInitialized();
+
   if (mAudioClock.GetPreservesPitch()) {
     mTimeStretcher->setTempo(aPlaybackRate);
     mTimeStretcher->setRate(1.0f);
   } else {
     mTimeStretcher->setTempo(1.0f);
     mTimeStretcher->setRate(aPlaybackRate);
   }
   return NS_OK;
 }
 
 nsresult AudioStream::SetPreservesPitch(bool aPreservesPitch)
 {
   // Avoid instantiating the timestretcher instance if not needed.
   if (aPreservesPitch == mAudioClock.GetPreservesPitch()) {
     return NS_OK;
   }
-  if (!EnsureTimeStretcherInitialized()) {
-    return NS_ERROR_FAILURE;
-  }
+
+  EnsureTimeStretcherInitialized();
+
   if (aPreservesPitch == true) {
     mTimeStretcher->setTempo(mAudioClock.GetPlaybackRate());
     mTimeStretcher->setRate(1.0f);
   } else {
     mTimeStretcher->setTempo(1.0f);
     mTimeStretcher->setRate(mAudioClock.GetPlaybackRate());
   }
 
@@ -364,19 +360,17 @@ nsresult NativeAudioStream::Write(const 
 
   if (mInError)
     return NS_ERROR_FAILURE;
 
   uint32_t samples = aFrames * mChannels;
   int32_t written = -1;
 
   if (mInRate != mOutRate) {
-    if (!EnsureTimeStretcherInitialized()) {
-      return NS_ERROR_FAILURE;
-    }
+    EnsureTimeStretcherInitialized();
     mTimeStretcher->putSamples(aBuf, aFrames);
     uint32_t numFrames = mTimeStretcher->numSamples();
     uint32_t arraySize = numFrames * mChannels * sizeof(AudioDataValue);
     nsAutoArrayPtr<AudioDataValue> data(new AudioDataValue[arraySize]);
     uint32_t framesAvailable = mTimeStretcher->receiveSamples(data, numFrames);
     NS_ASSERTION(mTimeStretcher->numSamples() == 0,
                  "We did not get all the data from the SoundTouch pipeline.");
     // It is possible to have nothing to write: the data are in the processing
@@ -960,19 +954,19 @@ BufferedAudioStream::GetUnprocessed(void
   memcpy(wpos, input[1], input_size[1]);
   return BytesToFrames(available) + flushedFrames;
 }
 
 long
 BufferedAudioStream::GetTimeStretched(void* aBuffer, long aFrames)
 {
   long processedFrames = 0;
-  if (!EnsureTimeStretcherInitialized()) {
-    return -1;
-  }
+
+  EnsureTimeStretcherInitialized();
+
   uint8_t* wpos = reinterpret_cast<uint8_t*>(aBuffer);
   double playbackRate = static_cast<double>(mInRate) / mOutRate;
   uint32_t toPopBytes = FramesToBytes(ceil(aFrames / playbackRate));
   uint32_t available = 0;
   bool lowOnBufferedData = false;
   do {
     // Check if we already have enough data in the time stretcher pipeline.
     if (mTimeStretcher->numSamples() <= static_cast<uint32_t>(aFrames)) {
--- a/content/media/AudioStream.h
+++ b/content/media/AudioStream.h
@@ -154,19 +154,18 @@ public:
 
   // Returns the minimum number of audio frames which must be written before
   // you can be sure that something will be played.
   virtual int32_t GetMinWriteSize() = 0;
 
   int GetRate() { return mOutRate; }
   int GetChannels() { return mChannels; }
 
-  // This should be called before attempting to use the time stretcher. It
-  // return false in case of error.
-  bool EnsureTimeStretcherInitialized();
+  // This should be called before attempting to use the time stretcher.
+  void EnsureTimeStretcherInitialized();
   // Set playback rate as a multiple of the intrinsic playback rate. This is to
   // be called only with aPlaybackRate > 0.0.
   virtual nsresult SetPlaybackRate(double aPlaybackRate);
   // Switch between resampling (if false) and time stretching (if true, default).
   virtual nsresult SetPreservesPitch(bool aPreservesPitch);
 
 protected:
   // Input rate in Hz (characteristic of the media being played)