Move more logic from AudioDestinationNode to its subclasses
[WebKit-https.git] / Source / WebCore / Modules / webaudio / OfflineAudioDestinationNode.cpp
index 5ef86afcd81de4fa8185b43200f11440f0ccd34f..aefe2a8436d365e186813267e38ac86174bbfef5 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2011, Google Inc. All rights reserved.
+ * Copyright (C) 2020-2021, Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -28,6 +29,7 @@
 
 #include "OfflineAudioDestinationNode.h"
 
+#include "AudioBuffer.h"
 #include "AudioBus.h"
 #include "AudioContext.h"
 #include "AudioUtilities.h"
@@ -49,9 +51,9 @@ OfflineAudioDestinationNode::OfflineAudioDestinationNode(OfflineAudioContext& co
     : AudioDestinationNode(context, sampleRate)
     , m_numberOfChannels(numberOfChannels)
     , m_renderTarget(WTFMove(renderTarget))
+    , m_renderBus(AudioBus::create(numberOfChannels, AudioUtilities::renderQuantumSize))
     , m_framesToProcess(m_renderTarget ? m_renderTarget->length() : 0)
 {
-    m_renderBus = AudioBus::create(numberOfChannels, AudioUtilities::renderQuantumSize);
     initializeDefaultNodeOptions(numberOfChannels, ChannelCountMode::Explicit, ChannelInterpretation::Speakers);
 }
 
@@ -70,11 +72,6 @@ const OfflineAudioContext& OfflineAudioDestinationNode::context() const
     return downcast<OfflineAudioContext>(AudioDestinationNode::context());
 }
 
-unsigned OfflineAudioDestinationNode::maxChannelCount() const
-{
-    return m_numberOfChannels;
-}
-
 void OfflineAudioDestinationNode::initialize()
 {
     if (isInitialized())
@@ -121,18 +118,18 @@ void OfflineAudioDestinationNode::startRendering(CompletionHandler<void(Optional
     auto protectedThis = makeRef(*this);
 
     auto offThreadRendering = [this, protectedThis = WTFMove(protectedThis)]() mutable {
-        auto result = offlineRender();
-        callOnMainThread([this, result, currentSampleFrame = m_currentSampleFrame.load(), protectedThis = WTFMove(protectedThis)]() mutable {
+        auto result = renderOnAudioThread();
+        callOnMainThread([this, result, currentSampleFrame = this->currentSampleFrame(), protectedThis = WTFMove(protectedThis)]() mutable {
             context().postTask([this, protectedThis = WTFMove(protectedThis), result, currentSampleFrame]() mutable {
                 m_startedRendering = false;
                 switch (result) {
-                case OfflineRenderResult::Failure:
+                case RenderResult::Failure:
                     context().finishedRendering(false);
                     break;
-                case OfflineRenderResult::Complete:
+                case RenderResult::Complete:
                     context().finishedRendering(true);
                     break;
-                case OfflineRenderResult::Suspended:
+                case RenderResult::Suspended:
                     context().didSuspendRendering(currentSampleFrame);
                     break;
                 }
@@ -152,25 +149,25 @@ void OfflineAudioDestinationNode::startRendering(CompletionHandler<void(Optional
     completionHandler(WTF::nullopt);
 }
 
-auto OfflineAudioDestinationNode::offlineRender() -> OfflineRenderResult
+auto OfflineAudioDestinationNode::renderOnAudioThread() -> RenderResult
 {
     ASSERT(!isMainThread());
     ASSERT(m_renderBus.get());
 
     if (!m_renderBus.get())
-        return OfflineRenderResult::Failure;
+        return RenderResult::Failure;
 
     RELEASE_ASSERT(context().isInitialized());
 
     bool channelsMatch = m_renderBus->numberOfChannels() == m_renderTarget->numberOfChannels();
     ASSERT(channelsMatch);
     if (!channelsMatch)
-        return OfflineRenderResult::Failure;
+        return RenderResult::Failure;
 
     bool isRenderBusAllocated = m_renderBus->length() >= AudioUtilities::renderQuantumSize;
     ASSERT(isRenderBusAllocated);
     if (!isRenderBusAllocated)
-        return OfflineRenderResult::Failure;
+        return RenderResult::Failure;
 
     // Break up the render target into smaller "render quantize" sized pieces.
     // Render until we're finished.
@@ -178,10 +175,10 @@ auto OfflineAudioDestinationNode::offlineRender() -> OfflineRenderResult
 
     while (m_framesToProcess > 0) {
         if (context().shouldSuspend())
-            return OfflineRenderResult::Suspended;
+            return RenderResult::Suspended;
 
         // Render one render quantum.
-        render(0, m_renderBus.get(), AudioUtilities::renderQuantumSize, { });
+        renderQuantum(m_renderBus.get(), AudioUtilities::renderQuantumSize, { });
         
         size_t framesAvailableToCopy = std::min(m_framesToProcess, AudioUtilities::renderQuantumSize);
         
@@ -195,7 +192,7 @@ auto OfflineAudioDestinationNode::offlineRender() -> OfflineRenderResult
         m_framesToProcess -= framesAvailableToCopy;
     }
 
-    return OfflineRenderResult::Complete;
+    return RenderResult::Complete;
 }
 
 } // namespace WebCore