Chromium Code Reviews| Index: Source/modules/webaudio/OfflineAudioContext.h |
| diff --git a/Source/modules/webaudio/OfflineAudioContext.h b/Source/modules/webaudio/OfflineAudioContext.h |
| index 030586f4d409064f9104460129d823414c9ac71f..12ff7cbfc2a283d4dd257c91b2bd6ab615be06f1 100644 |
| --- a/Source/modules/webaudio/OfflineAudioContext.h |
| +++ b/Source/modules/webaudio/OfflineAudioContext.h |
| @@ -28,9 +28,20 @@ |
| #include "modules/ModulesExport.h" |
| #include "modules/webaudio/AbstractAudioContext.h" |
| +#include "wtf/HashMap.h" |
| +#include "wtf/HashSet.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 |
| +// 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 +50,85 @@ 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*, double, size_t); |
| + |
| + // Reject a suspend container on the main thread when the validation fails. |
| + void rejectSuspendContainerOnMainThread(ScheduledSuspendContainer*, const String&); |
| + |
| + // Resolve a pending suspend container. |
| + void resolveSuspendContainerOnMainThread(ScheduledSuspendContainer*); |
| + |
| + // This set is for maintaining the ownership of SchduledSuspendContainer |
| + // raw pointers. |
|
hiroshige
2015/07/22 17:32:12
Please add a comment that this field is accessed o
hongchan
2015/07/22 20:36:35
Done.
|
| + HashSet<ScheduledSuspendContainer*> m_suspendContainers; |
|
hiroshige
2015/07/22 17:32:12
nit optional: the names of |m_suspendedContainers|
hongchan
2015/07/22 20:36:35
Acknowledged.
|
| + |
| + // This map is to check the timing of scheduled suspends. |
|
hiroshige
2015/07/22 17:32:12
Please add a comment that this field is accessed o
hongchan
2015/07/22 20:36:35
Done.
|
| + 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 main thread. |
| +class ScheduledSuspendContainer { |
| +public: |
| + static ScheduledSuspendContainer* create(PassRefPtrWillBeRawPtr<ScriptPromiseResolver>); |
| + ~ScheduledSuspendContainer(); |
| + |
| + // {Resolve, Reject} the promise resolver on the main thread. |
| + void resolvePromise(); |
| + void rejectPromise(ExceptionCode, const String&); |
| + |
| +private: |
| + ScheduledSuspendContainer(PassRefPtrWillBeRawPtr<ScriptPromiseResolver>); |
| + |
| + // Associated promise resolver. |
| + RefPtrWillBePersistent<ScriptPromiseResolver> m_resolver; |
| }; |
| } // namespace blink |