commit fc84989374baf6eac06941c9c953e456ffec0ddc
parent dc3ebfbdcba61bf0e2e5eb14b592f6893f308906
Author: Karl Tomlinson <karlt+@karlt.net>
Date: Wed, 8 Oct 2025 05:54:47 +0000
Bug 1992922 Clear scratch buffer on switch to fallback driver r=pehrsons
so that it is not output at an inappropriate later time.
Differential Revision: https://phabricator.services.mozilla.com/D267738
Diffstat:
2 files changed, 10 insertions(+), 0 deletions(-)
diff --git a/dom/media/AudioBufferUtils.h b/dom/media/AudioBufferUtils.h
@@ -167,6 +167,8 @@ class SpillBuffer {
return this->operator=(aOther);
}
+ bool IsEmpty() const { return mPosition == 0; }
+ void Empty() { mPosition = 0; }
/* Empty the spill buffer into the buffer of the audio callback. This returns
* the number of frames written. */
uint32_t Empty(AudioCallbackBufferWrapper<T>& aBuffer) {
diff --git a/dom/media/GraphDriver.cpp b/dom/media/GraphDriver.cpp
@@ -866,6 +866,7 @@ long AudioCallbackDriver::DataCallback(const AudioDataValue* aInputBuffer,
if (mAudioStreamState.compareExchange(AudioStreamState::Starting,
AudioStreamState::Running)) {
+ MOZ_ASSERT(mScratchBuffer.IsEmpty());
LOG(LogLevel::Verbose, ("%p: AudioCallbackDriver %p First audio callback "
"close the Fallback driver",
Graph(), this));
@@ -1296,6 +1297,13 @@ void AudioCallbackDriver::FallbackToSystemClockDriver() {
LOG(LogLevel::Debug,
("%p: AudioCallbackDriver %p Falling back to SystemClockDriver.", Graph(),
this));
+ // On DeviceChangedCallback() or StateChangeCallback(), mScratchBuffer might
+ // not be empty, but switching to a fallback driver is giving up on
+ // outputting mScratchBuffer contiguously.
+ // Clear the buffer so that it is not output later when an audio callback
+ // arrives for a new discontiguous output stream.
+ mScratchBuffer.Empty();
+
mNextReInitBackoffStep =
TimeDuration::FromMilliseconds(AUDIO_INITIAL_FALLBACK_BACKOFF_STEP_MS);
mNextReInitAttempt = TimeStamp::Now() + mNextReInitBackoffStep;