Bug 1752345 - Reset SPSC queue thread ids when the underlying audio callback thread changes. r=alwu

Differential Revision: https://phabricator.services.mozilla.com/D137668
This commit is contained in:
Paul Adenot committed 2022-02-03 15:18:14 +00:00
1 parent fac779ec47
commit 1454826c66
4 files changed
+35 -16

No files matched your search

+16 -10
View File
@@ -504,7 +504,8 @@ void AudioStream::GetUnprocessed(AudioBufferWriter& aWriter) {
}
while (aWriter.Available() > 0) {
uint32_t count = mDataSource.PopFrames(aWriter.Ptr(), aWriter.Available());
uint32_t count = mDataSource.PopFrames(aWriter.Ptr(), aWriter.Available(),
mAudioThreadChanged);
if (count == 0) {
break;
}
@@ -534,7 +535,8 @@ void AudioStream::GetTimeStretched(AudioBufferWriter& aWriter) {
}
buf.SetLength(size.value());
// ensure no variable channel count or something like that
uint32_t count = mDataSource.PopFrames(buf.Elements(), toPopFrames);
uint32_t count =
mDataSource.PopFrames(buf.Elements(), toPopFrames, mAudioThreadChanged);
if (count == 0) {
break;
}
@@ -553,8 +555,10 @@ bool AudioStream::CheckThreadIdChanged() {
ProfilerThreadId id = profiler_current_thread_id();
if (id != mAudioThreadId) {
mAudioThreadId = id;
mAudioThreadChanged = true;
return true;
}
mAudioThreadChanged = false;
return false;
}
@@ -619,7 +623,7 @@ long AudioStream::DataCallback(void* aBuffer, long aFrames) {
MonitorAutoLock mon(mMonitor);
#endif
mAudioClock.UpdateFrameHistory(aFrames - writer.Available(),
writer.Available());
writer.Available(), mAudioThreadChanged);
if (writer.Available() > 0) {
TRACE_COMMENT("AudioStream::DataCallback", "Underrun: %d frames missing",
writer.Available());
@@ -636,7 +640,7 @@ long AudioStream::DataCallback(void* aBuffer, long aFrames) {
#ifndef XP_MACOSX
MonitorAutoLock mon(mMonitor);
#endif
mAudioClock.UpdateFrameHistory(aFrames - writer.Available(), 0);
mAudioClock.UpdateFrameHistory(aFrames - writer.Available(), 0, mAudioThreadChanged);
}
mDumpFile.Write(static_cast<const AudioDataValue*>(aBuffer),
@@ -673,22 +677,24 @@ AudioClock::AudioClock(uint32_t aInRate)
mPreservesPitch(true),
mFrameHistory(new FrameHistory()) {}
void AudioClock::UpdateFrameHistory(uint32_t aServiced, uint32_t aUnderrun) {
void AudioClock::UpdateFrameHistory(uint32_t aServiced, uint32_t aUnderrun, bool aAudioThreadChanged) {
#ifdef XP_MACOSX
if (aAudioThreadChanged) {
mCallbackInfoQueue.ResetThreadIds();
}
// Flush the local items, if any, and then attempt to enqueue the current
// item. This is only a fallback mechanism, under non-critical load this is
// just going to enqueue an item in the queue.
while (!mAudioThreadCallbackInfo.IsEmpty()) {
CallbackInfo& info = mAudioThreadCallbackInfo[0];
int enqueued = mCallbackInfoQueue.Enqueue(info);
// Still full, keep it audio-thread side for now.
if (enqueued != 1) {
// If still full, keep it audio-thread side for now.
if (mCallbackInfoQueue.Enqueue(info) != 1) {
break;
}
mAudioThreadCallbackInfo.RemoveElementAt(0);
}
CallbackInfo info(aServiced, aUnderrun, mOutRate);
int enqueued = mCallbackInfoQueue.Enqueue(info);
if (enqueued != 1) {
if (mCallbackInfoQueue.Enqueue(info) != 1) {
NS_WARNING(
"mCallbackInfoQueue full, storing the values in the audio thread.");
mAudioThreadCallbackInfo.AppendElement(info);
+5 -2
View File
@@ -57,7 +57,7 @@ class AudioClock {
// Update the number of samples that has been written in the audio backend.
// Called on the audio thread only.
void UpdateFrameHistory(uint32_t aServiced, uint32_t aUnderrun);
void UpdateFrameHistory(uint32_t aServiced, uint32_t aUnderrun, bool aAudioThreadChanged);
/**
* @param aFrames The playback position in frames of the audio engine.
@@ -219,7 +219,8 @@ class AudioStream final {
public:
// Attempt to acquire aFrames frames of audio, and returns the number of
// frames successfuly acquired.
virtual uint32_t PopFrames(AudioDataValue* aAudio, uint32_t aFrames) = 0;
virtual uint32_t PopFrames(AudioDataValue* aAudio, uint32_t aFrames,
bool aAudioThreadChanged) = 0;
// Return true if no more data will be added to the source.
virtual bool Ended() const = 0;
@@ -364,6 +365,8 @@ class AudioStream final {
// Both written on the MDSM thread, read on the audio thread.
std::atomic<float> mPlaybackRate;
std::atomic<bool> mPreservesPitch;
// Audio thread only
bool mAudioThreadChanged = false;
};
} // namespace mozilla
+12 -3
View File
@@ -57,8 +57,9 @@ AudioSink::AudioSink(AbstractThread* aThread,
mAudioQueue(aAudioQueue) {
// Twice the limit that trigger a refill.
float capacitySeconds = LOW_AUDIO_USECS / 1000. / 1000. * 2;
mProcessedSPSCQueue = MakeUnique<SPSCQueue<AudioDataValue>>(
static_cast<uint32_t>(capacitySeconds * static_cast<float>(mOutputChannels * mOutputRate)));
mProcessedSPSCQueue =
MakeUnique<SPSCQueue<AudioDataValue>>(static_cast<uint32_t>(
capacitySeconds * static_cast<float>(mOutputChannels * mOutputRate)));
SINK_LOG("Ringbuffer has space for %u elements (%lf seconds)",
mProcessedSPSCQueue->Capacity(), capacitySeconds);
}
@@ -206,7 +207,15 @@ TimeUnit AudioSink::GetEndTime() const {
return std::min(mLastEndTime, played);
}
uint32_t AudioSink::PopFrames(AudioDataValue* aBuffer, uint32_t aFrames) {
uint32_t AudioSink::PopFrames(AudioDataValue* aBuffer, uint32_t aFrames,
bool aAudioThreadChanged) {
// This is safe, because we have the guarantee, by the OS, that audio
// callbacks are never called concurrently. Audio thread changes can only
// happen when not using cubeb remoting, and often when changing audio device
// at the system level.
if (aAudioThreadChanged) {
mProcessedSPSCQueue->ResetThreadIds();
}
const int samplesToPop = static_cast<int>(aFrames * mOutputChannels);
const int samplesRead = mProcessedSPSCQueue->Dequeue(aBuffer, samplesToPop);
MOZ_ASSERT(samplesRead % mOutputChannels == 0);
+2 -1
View File
@@ -80,7 +80,8 @@ class AudioSink : private AudioStream::DataSource {
// Interface of AudioStream::DataSource.
// Called on the callback thread of cubeb. Returns the number of frames that
// were available.
uint32_t PopFrames(AudioDataValue* aBuffer, uint32_t aFrames) override;
uint32_t PopFrames(AudioDataValue* aBuffer, uint32_t aFrames,
bool aAudioThreadChanged) override;
bool Ended() const override;
void CheckIsAudible(const Span<AudioDataValue>& aInterleaved,