Title: [273629] branches/safari-610.4.3.1-branch/Source/WebCore/Modules/webaudio

Diff

Modified: branches/safari-610.4.3.1-branch/Source/WebCore/Modules/webaudio/AudioBuffer.cpp (273628 => 273629)


--- branches/safari-610.4.3.1-branch/Source/WebCore/Modules/webaudio/AudioBuffer.cpp	2021-02-27 22:39:16 UTC (rev 273628)
+++ branches/safari-610.4.3.1-branch/Source/WebCore/Modules/webaudio/AudioBuffer.cpp	2021-02-27 22:54:40 UTC (rev 273629)
@@ -222,6 +222,32 @@
     return cost;
 }
 
+bool AudioBuffer::topologyMatches(const AudioBuffer& other) const
+{
+    return numberOfChannels() == other.numberOfChannels() && length() == other.length() && sampleRate() == other.sampleRate();
+}
+
+bool AudioBuffer::copyTo(AudioBuffer& other) const
+{
+    if (!topologyMatches(other))
+        return false;
+
+    for (unsigned channelIndex = 0; channelIndex < numberOfChannels(); ++channelIndex)
+        memcpy(other.channelData(channelIndex)->data(), m_channels[channelIndex]->data(), length() * sizeof(float));
+
+    return true;
+}
+
+Ref<AudioBuffer> AudioBuffer::clone(ShouldCopyChannelData shouldCopyChannelData) const
+{
+    auto clone = AudioBuffer::create(numberOfChannels(), length(), sampleRate());
+    ASSERT(clone);
+    if (shouldCopyChannelData == ShouldCopyChannelData::Yes)
+        copyTo(*clone);
+    return clone.releaseNonNull();
+}
+
+
 } // namespace WebCore
 
 #endif // ENABLE(WEB_AUDIO)

Modified: branches/safari-610.4.3.1-branch/Source/WebCore/Modules/webaudio/AudioBuffer.h (273628 => 273629)


--- branches/safari-610.4.3.1-branch/Source/WebCore/Modules/webaudio/AudioBuffer.h	2021-02-27 22:39:16 UTC (rev 273628)
+++ branches/safari-610.4.3.1-branch/Source/WebCore/Modules/webaudio/AudioBuffer.h	2021-02-27 22:54:40 UTC (rev 273629)
@@ -70,6 +70,13 @@
 
     size_t memoryCost() const;
     
+    bool copyTo(AudioBuffer&) const;
+
+    enum class ShouldCopyChannelData : bool { No, Yes };
+    Ref<AudioBuffer> clone(ShouldCopyChannelData = ShouldCopyChannelData::Yes) const;
+
+    bool topologyMatches(const AudioBuffer&) const;
+
 private:
     AudioBuffer(unsigned numberOfChannels, size_t length, float sampleRate);
     explicit AudioBuffer(AudioBus&);

Modified: branches/safari-610.4.3.1-branch/Source/WebCore/Modules/webaudio/ScriptProcessorNode.cpp (273628 => 273629)


--- branches/safari-610.4.3.1-branch/Source/WebCore/Modules/webaudio/ScriptProcessorNode.cpp	2021-02-27 22:39:16 UTC (rev 273628)
+++ branches/safari-610.4.3.1-branch/Source/WebCore/Modules/webaudio/ScriptProcessorNode.cpp	2021-02-27 22:54:40 UTC (rev 273629)
@@ -104,6 +104,31 @@
     AudioNode::initialize();
 }
 
+RefPtr<AudioBuffer> ScriptProcessorNode::createInputBufferForJS(AudioBuffer* inputBuffer) const
+{
+    if (!inputBuffer)
+        return nullptr;
+
+    // As an optimization, we reuse the same buffer as last time when possible.
+    if (!m_cachedInputBufferForJS || !m_cachedInputBufferForJS->topologyMatches(*inputBuffer))
+        m_cachedInputBufferForJS = inputBuffer->clone();
+    else
+        inputBuffer->copyTo(*m_cachedInputBufferForJS);
+
+    return m_cachedInputBufferForJS;
+}
+
+RefPtr<AudioBuffer> ScriptProcessorNode::createOutputBufferForJS(AudioBuffer& outputBuffer) const
+{
+    // As an optimization, we reuse the same buffer as last time when possible.
+    if (!m_cachedOutputBufferForJS || !m_cachedOutputBufferForJS->topologyMatches(outputBuffer))
+        m_cachedOutputBufferForJS = outputBuffer.clone(AudioBuffer::ShouldCopyChannelData::No);
+    else
+        m_cachedOutputBufferForJS->zero();
+
+    return m_cachedOutputBufferForJS;
+}
+
 void ScriptProcessorNode::uninitialize()
 {
     if (!isInitialized())
@@ -126,14 +151,23 @@
 {
     // Discussion about inputs and outputs:
     // As in other AudioNodes, ScriptProcessorNode uses an AudioBus for its input and output (see inputBus and outputBus below).
-    // Additionally, there is a double-buffering for input and output which is exposed directly to _javascript_ (see inputBuffer and outputBuffer below).
+    // Additionally, there is a double-buffering for input and output (see inputBuffer and outputBuffer below).
     // This node is the producer for inputBuffer and the consumer for outputBuffer.
-    // The _javascript_ code is the consumer of inputBuffer and the producer for outputBuffer.
+    // The _javascript_ code is the consumer of inputBuffer and the producer for outputBuffer. The _javascript_ gets its own copy
+    // of the buffers for safety reasons.
 
     // Get input and output busses.
     AudioBus* inputBus = this->input(0)->bus();
     AudioBus* outputBus = this->output(0)->bus();
 
+    auto locker = tryHoldLock(m_processLock);
+    if (!locker) {
+        // We're late in handling the previous request. The main thread must be
+        // very busy. The best we can do is clear out the buffer ourself here.
+        outputBus->zero();
+        return;
+    }
+
     // Get input and output buffers. We double-buffer both the input and output sides.
     unsigned doubleBufferIndex = this->doubleBufferIndex();
     bool isDoubleBufferIndexGood = doubleBufferIndex < 2 && doubleBufferIndex < m_inputBuffers.size() && doubleBufferIndex < m_outputBuffers.size();
@@ -211,6 +245,7 @@
                 deref();
             } else {
                 callOnMainThread([this] {
+                    auto locker = holdLock(m_processLock);
                     fireProcessEvent();
                     // De-reference to match the ref() call in process().
                     deref();
@@ -238,17 +273,24 @@
         return;
 
     // Avoid firing the event if the document has already gone away.
-    if (!context().isStopped()) {
-        // Let the audio thread know we've gotten to the point where it's OK for it to make another request.
-        m_isRequestOutstanding = false;
+    if (context().isStopped())
+        return;
 
-        // Calculate playbackTime with the buffersize which needs to be processed each time when onaudioprocess is called.
-        // The outputBuffer being passed to JS will be played after exhausting previous outputBuffer by double-buffering.
-        double playbackTime = (context().currentSampleFrame() + m_bufferSize) / static_cast<double>(context().sampleRate());
+    // Let the audio thread know we've gotten to the point where it's OK for it to make another request.
+    m_isRequestOutstanding = false;
 
-        // Call the _javascript_ event handler which will do the audio processing.
-        dispatchEvent(AudioProcessingEvent::create(inputBuffer, outputBuffer, playbackTime));
-    }
+    // Calculate playbackTime with the buffersize which needs to be processed each time when onaudioprocess is called.
+    // The outputBuffer being passed to JS will be played after exhausting previous outputBuffer by double-buffering.
+    double playbackTime = (context().currentSampleFrame() + m_bufferSize) / static_cast<double>(context().sampleRate());
+
+    auto inputBufferForJS = createInputBufferForJS(inputBuffer);
+    auto outputBufferForJS = createOutputBufferForJS(*outputBuffer);
+
+    // Call the _javascript_ event handler which will do the audio processing.
+    dispatchEvent(AudioProcessingEvent::create(inputBufferForJS.get(), outputBufferForJS.get(), playbackTime));
+
+    if (!outputBufferForJS->copyTo(*outputBuffer))
+        outputBuffer->zero();
 }
 
 void ScriptProcessorNode::reset()

Modified: branches/safari-610.4.3.1-branch/Source/WebCore/Modules/webaudio/ScriptProcessorNode.h (273628 => 273629)


--- branches/safari-610.4.3.1-branch/Source/WebCore/Modules/webaudio/ScriptProcessorNode.h	2021-02-27 22:39:16 UTC (rev 273628)
+++ branches/safari-610.4.3.1-branch/Source/WebCore/Modules/webaudio/ScriptProcessorNode.h	2021-02-27 22:54:40 UTC (rev 273629)
@@ -75,6 +75,9 @@
 
     void fireProcessEvent();
 
+    RefPtr<AudioBuffer> createInputBufferForJS(AudioBuffer*) const;
+    RefPtr<AudioBuffer> createOutputBufferForJS(AudioBuffer&) const;
+
     // Double buffering
     unsigned doubleBufferIndex() const { return m_doubleBufferIndex; }
     void swapBuffers() { m_doubleBufferIndex = 1 - m_doubleBufferIndex; }
@@ -82,6 +85,8 @@
     unsigned m_doubleBufferIndexForEvent;
     Vector<RefPtr<AudioBuffer>> m_inputBuffers;
     Vector<RefPtr<AudioBuffer>> m_outputBuffers;
+    mutable RefPtr<AudioBuffer> m_cachedInputBufferForJS;
+    mutable RefPtr<AudioBuffer> m_cachedOutputBufferForJS;
 
     size_t m_bufferSize;
     unsigned m_bufferReadWriteIndex;
@@ -92,6 +97,7 @@
 
     RefPtr<AudioBus> m_internalInputBus;
     RefPtr<PendingActivity<ScriptProcessorNode>> m_pendingActivity;
+    Lock m_processLock;
 };
 
 } // namespace WebCore
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to