From 2836e514661df0ffd816cf957ecc35c968377fbe Mon Sep 17 00:00:00 2001 From: Moonchild Date: Tue, 26 Jan 2021 14:55:48 +0000 Subject: [webaudio] Fix up ReverbInputBuffer's writeIndex. --- dom/media/webaudio/blink/ReverbInputBuffer.cpp | 21 +++++++++++---------- dom/media/webaudio/blink/ReverbInputBuffer.h | 3 ++- 2 files changed, 13 insertions(+), 11 deletions(-) (limited to 'dom/media/webaudio') diff --git a/dom/media/webaudio/blink/ReverbInputBuffer.cpp b/dom/media/webaudio/blink/ReverbInputBuffer.cpp index 8221f8151..6b8e642d2 100644 --- a/dom/media/webaudio/blink/ReverbInputBuffer.cpp +++ b/dom/media/webaudio/blink/ReverbInputBuffer.cpp @@ -42,19 +42,20 @@ ReverbInputBuffer::ReverbInputBuffer(size_t length) void ReverbInputBuffer::write(const float* sourceP, size_t numberOfFrames) { - size_t bufferLength = m_buffer.Length(); - bool isCopySafe = m_writeIndex + numberOfFrames <= bufferLength; - MOZ_ASSERT(isCopySafe); - if (!isCopySafe) - return; + // m_writeIndex is atomic and checked by other threads, so only touch + // it at the start and end. + size_t index = m_writeIndex; + + size_t bufferLength = m_buffer.Length(); + size_t newIndex = index + numberOfFrames; - memcpy(m_buffer.Elements() + m_writeIndex, sourceP, sizeof(float) * numberOfFrames); + MOZ_RELEASE_ASSERT(newIndex <= bufferLength); - m_writeIndex += numberOfFrames; - MOZ_ASSERT(m_writeIndex <= bufferLength); + memcpy(m_buffer.Elements() + index, sourceP, sizeof(float) * numberOfFrames); - if (m_writeIndex >= bufferLength) - m_writeIndex = 0; + if (newIndex >= bufferLength) { + m_writeIndex = 0; + } } float* ReverbInputBuffer::directReadFrom(int* readIndex, size_t numberOfFrames) diff --git a/dom/media/webaudio/blink/ReverbInputBuffer.h b/dom/media/webaudio/blink/ReverbInputBuffer.h index 906021c0d..f51aa3091 100644 --- a/dom/media/webaudio/blink/ReverbInputBuffer.h +++ b/dom/media/webaudio/blink/ReverbInputBuffer.h @@ -30,6 +30,7 @@ #define ReverbInputBuffer_h #include "nsTArray.h" +#include "mozilla/Atomics.h" #include "mozilla/MemoryReporting.h" namespace WebCore { @@ -63,7 +64,7 @@ public: private: nsTArray m_buffer; - size_t m_writeIndex; + mozilla::Atomic m_writeIndex; }; } // namespace WebCore -- cgit v1.2.3