Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1800)

Unified Diff: Source/modules/webaudio/OfflineAudioContext.h

Issue 1140723003: Implement suspend() and resume() for OfflineAudioContext (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Adapting CL to AbstractAudioContext Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/modules/webaudio/OfflineAudioContext.h
diff --git a/Source/modules/webaudio/OfflineAudioContext.h b/Source/modules/webaudio/OfflineAudioContext.h
index 030586f4d409064f9104460129d823414c9ac71f..eb3b51f403bcdc49a3b661543ef89ea1f31e8062 100644
--- a/Source/modules/webaudio/OfflineAudioContext.h
+++ b/Source/modules/webaudio/OfflineAudioContext.h
@@ -28,9 +28,19 @@
#include "modules/ModulesExport.h"
#include "modules/webaudio/AbstractAudioContext.h"
+#include "wtf/HashMap.h"
+
namespace blink {
class ExceptionState;
+class ScheduledSuspendContainer;
+class OfflineAudioDestinationHandler;
+
+// The HashMap with 'zero' key is needed because |currentSampleFrame| can be
+// zero. Also ScheduledSuspendContainer is a raw pointer (rather than RefPtr)
+// because it must be guaranteed that the main thread keeps it alive while the
Raymond Toy 2015/07/15 20:59:14 What does "must be guaranteed that the main thread
hongchan 2015/07/15 23:24:22 Conceptually, I believe it is cleaner to separate
Raymond Toy 2015/07/16 16:52:04 Acknowledged.
+// render thread uses it.
+using SuspendContainerMap = HashMap<size_t, ScheduledSuspendContainer*, DefaultHash<size_t>::Hash, WTF::UnsignedWithZeroKeyHashTraits<size_t>>;
class MODULES_EXPORT OfflineAudioContext final : public AbstractAudioContext {
DEFINE_WRAPPERTYPEINFO();
@@ -39,16 +49,95 @@ public:
~OfflineAudioContext() override;
+ DECLARE_VIRTUAL_TRACE();
+
+ DEFINE_ATTRIBUTE_EVENT_LISTENER(complete);
+
+ // Check all the scheduled suspends if the context should suspend at
+ // currentTime(). Then post tasks to resolve promises on the main thread
+ // if necessary.
+ bool shouldSuspendNow();
+
+ // Clear suspensions marked as 'resolved' in the list.
+ void resolvePendingSuspendPromises();
+
+ // Fire completion event when the rendering is finished.
+ void fireCompletionEvent();
+
ScriptPromise startOfflineRendering(ScriptState*);
ScriptPromise closeContext(ScriptState*) final;
- ScriptPromise suspendContext(ScriptState*) final;
+ ScriptPromise suspendContext(ScriptState*, double) final;
ScriptPromise resumeContext(ScriptState*) final;
+ // This is to implement the pure virtual method from AbstractAudioContext.
+ // CANNOT be called on OfflineAudioContext.
+ ScriptPromise suspendContext(ScriptState*) final;
+
bool hasRealtimeConstraint() final { return false; }
private:
OfflineAudioContext(Document*, unsigned numberOfChannels, size_t numberOfFrames, float sampleRate);
+
+ // Fetch directly the destination handler.
+ OfflineAudioDestinationHandler& destinationHandler();
+
+ // Check a suspend container if it has a duplicate scheduled frame or
+ // is behind the current frame. If the validation fails, post a task to the
+ // main thread to reject the promise.
+ void validateSuspendContainerOnRenderThread(ScheduledSuspendContainer*);
+
+ // Reject a suspend container on the main thread when the validation fails.
+ void rejectSuspendContainerOnMainThread(ScheduledSuspendContainer*);
+
+ // Resolve a pending suspend container.
+ void resolveSuspendContainerOnMainThread(ScheduledSuspendContainer*);
+
+ SuspendContainerMap m_scheduledSuspends;
+ RefPtrWillBeMember<ScriptPromiseResolver> m_completeResolver;
+
+ // This flag is necessary to indicate the rendering has actually started.
+ // Note that initial state of context is 'Suspended', which is the same
+ // state when the context is suspended.
+ bool m_isRenderingStarted;
+
+ // Total render sample length.
+ size_t m_totalRenderFrames;
+};
+
+// A container class for a pair of time information and the suspend promise
+// resolver. This class does not need to be |ThreadSafeRefCounted| because it
+// needs to be created and destructed in the same thread.
Raymond Toy 2015/07/15 20:59:14 "same thread" as what? And why does it need to be
hongchan 2015/07/15 23:24:22 This object is created and destroyed in the main t
Raymond Toy 2015/07/16 16:24:23 Just change the comment from "same thread" to "mai
+class ScheduledSuspendContainer {
+public:
+ static ScheduledSuspendContainer* create(double suspendTime, size_t suspendFrame, PassRefPtrWillBeRawPtr<ScriptPromiseResolver>);
+ ~ScheduledSuspendContainer();
+
+ double suspendTime() const { return m_suspendTime; }
+ size_t suspendFrame() const { return m_suspendFrame; }
+
+ // Set the error message for the reason of rejection on the render thread
+ // before sending it to the main thread.
+ void setErrorMessageForRejection(ExceptionCode, const String&);
+
+ // {Resolve, Reject} the promise resolver on the main thread.
+ void resolvePromise();
+ void rejectPromise();
+
+private:
+ ScheduledSuspendContainer(double suspendTime, size_t suspendFrame, PassRefPtrWillBeRawPtr<ScriptPromiseResolver>);
+
+ // Actual suspend time before the quantization by render quantum frame.
+ double m_suspendTime;
+
+ // Suspend sample frame. This is quantized by the render quantum size.
+ size_t m_suspendFrame;
+
+ // Associated promise resolver.
+ RefPtrWillBePersistent<ScriptPromiseResolver> m_resolver;
+
+ ExceptionCode m_errorCode;
+ String m_errorMessage;
};
} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698