| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2012, Google Inc. All rights reserved. | 2 * Copyright (C) 2012, Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | 21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 */ | 23 */ |
| 24 | 24 |
| 25 #ifndef OfflineAudioContext_h | 25 #ifndef OfflineAudioContext_h |
| 26 #define OfflineAudioContext_h | 26 #define OfflineAudioContext_h |
| 27 | 27 |
| 28 #include "modules/ModulesExport.h" | 28 #include "modules/ModulesExport.h" |
| 29 #include "modules/webaudio/AbstractAudioContext.h" | 29 #include "modules/webaudio/AbstractAudioContext.h" |
| 30 | 30 |
| 31 #include "wtf/HashMap.h" | |
| 32 | |
| 33 namespace blink { | 31 namespace blink { |
| 34 | 32 |
| 35 class ExceptionState; | 33 class ExceptionState; |
| 36 class ScheduledSuspendContainer; | |
| 37 class OfflineAudioDestinationHandler; | |
| 38 | |
| 39 // The HashMap with 'zero' key is needed because |currentSampleFrame| can be | |
| 40 // zero. Also ScheduledSuspendContainer is a raw pointer (rather than RefPtr) | |
| 41 // because it must be guaranteed that the main thread keeps it alive while the | |
| 42 // render thread uses it. | |
| 43 using SuspendContainerMap = HashMap<size_t, ScheduledSuspendContainer*, DefaultH
ash<size_t>::Hash, WTF::UnsignedWithZeroKeyHashTraits<size_t>>; | |
| 44 | 34 |
| 45 class MODULES_EXPORT OfflineAudioContext final : public AbstractAudioContext { | 35 class MODULES_EXPORT OfflineAudioContext final : public AbstractAudioContext { |
| 46 DEFINE_WRAPPERTYPEINFO(); | 36 DEFINE_WRAPPERTYPEINFO(); |
| 47 public: | 37 public: |
| 48 static OfflineAudioContext* create(ExecutionContext*, unsigned numberOfChann
els, size_t numberOfFrames, float sampleRate, ExceptionState&); | 38 static OfflineAudioContext* create(ExecutionContext*, unsigned numberOfChann
els, size_t numberOfFrames, float sampleRate, ExceptionState&); |
| 49 | 39 |
| 50 ~OfflineAudioContext() override; | 40 ~OfflineAudioContext() override; |
| 51 | 41 |
| 52 DECLARE_VIRTUAL_TRACE(); | |
| 53 | |
| 54 DEFINE_ATTRIBUTE_EVENT_LISTENER(complete); | |
| 55 | |
| 56 // Check all the scheduled suspends if the context should suspend at | |
| 57 // currentTime(). Then post tasks to resolve promises on the main thread | |
| 58 // if necessary. | |
| 59 bool shouldSuspendNow(); | |
| 60 | |
| 61 // Clear suspensions marked as 'resolved' in the list. | |
| 62 void resolvePendingSuspendPromises(); | |
| 63 | |
| 64 // Fire completion event when the rendering is finished. | |
| 65 void fireCompletionEvent(); | |
| 66 | |
| 67 ScriptPromise startOfflineRendering(ScriptState*); | 42 ScriptPromise startOfflineRendering(ScriptState*); |
| 68 | 43 |
| 69 ScriptPromise closeContext(ScriptState*) final; | 44 ScriptPromise closeContext(ScriptState*) final; |
| 70 ScriptPromise suspendContext(ScriptState*, double) final; | 45 ScriptPromise suspendContext(ScriptState*) final; |
| 71 ScriptPromise resumeContext(ScriptState*) final; | 46 ScriptPromise resumeContext(ScriptState*) final; |
| 72 | 47 |
| 73 // This is to implement the pure virtual method from AbstractAudioContext. | |
| 74 // CANNOT be called on OfflineAudioContext. | |
| 75 ScriptPromise suspendContext(ScriptState*) final; | |
| 76 | |
| 77 bool hasRealtimeConstraint() final { return false; } | 48 bool hasRealtimeConstraint() final { return false; } |
| 78 | 49 |
| 79 private: | 50 private: |
| 80 OfflineAudioContext(Document*, unsigned numberOfChannels, size_t numberOfFra
mes, float sampleRate); | 51 OfflineAudioContext(Document*, unsigned numberOfChannels, size_t numberOfFra
mes, float sampleRate); |
| 81 | |
| 82 // Fetch directly the destination handler. | |
| 83 OfflineAudioDestinationHandler& destinationHandler(); | |
| 84 | |
| 85 // Check a suspend container if it has a duplicate scheduled frame or | |
| 86 // is behind the current frame. If the validation fails, post a task to the | |
| 87 // main thread to reject the promise. | |
| 88 void validateSuspendContainerOnRenderThread(ScheduledSuspendContainer*); | |
| 89 | |
| 90 // Reject a suspend container on the main thread when the validation fails. | |
| 91 void rejectSuspendContainerOnMainThread(ScheduledSuspendContainer*); | |
| 92 | |
| 93 // Resolve a pending suspend container. | |
| 94 void resolveSuspendContainerOnMainThread(ScheduledSuspendContainer*); | |
| 95 | |
| 96 SuspendContainerMap m_scheduledSuspends; | |
| 97 RefPtrWillBeMember<ScriptPromiseResolver> m_completeResolver; | |
| 98 | |
| 99 // This flag is necessary to indicate the rendering has actually started. | |
| 100 // Note that initial state of context is 'Suspended', which is the same | |
| 101 // state when the context is suspended. | |
| 102 bool m_isRenderingStarted; | |
| 103 | |
| 104 // Total render sample length. | |
| 105 size_t m_totalRenderFrames; | |
| 106 }; | |
| 107 | |
| 108 // A container class for a pair of time information and the suspend promise | |
| 109 // resolver. This class does not need to be |ThreadSafeRefCounted| because it | |
| 110 // needs to be created and destructed in the main thread. | |
| 111 class ScheduledSuspendContainer { | |
| 112 public: | |
| 113 static ScheduledSuspendContainer* create(double suspendTime, size_t suspendF
rame, PassRefPtrWillBeRawPtr<ScriptPromiseResolver>); | |
| 114 ~ScheduledSuspendContainer(); | |
| 115 | |
| 116 double suspendTime() const { return m_suspendTime; } | |
| 117 size_t suspendFrame() const { return m_suspendFrame; } | |
| 118 | |
| 119 // Set the error message for the reason of rejection on the render thread | |
| 120 // before sending it to the main thread. | |
| 121 void setErrorMessageForRejection(ExceptionCode, const String&); | |
| 122 | |
| 123 // {Resolve, Reject} the promise resolver on the main thread. | |
| 124 void resolvePromise(); | |
| 125 void rejectPromise(); | |
| 126 | |
| 127 private: | |
| 128 ScheduledSuspendContainer(double suspendTime, size_t suspendFrame, PassRefPt
rWillBeRawPtr<ScriptPromiseResolver>); | |
| 129 | |
| 130 // Actual suspend time before the quantization by render quantum frame. | |
| 131 double m_suspendTime; | |
| 132 | |
| 133 // Suspend sample frame. This is quantized by the render quantum size. | |
| 134 size_t m_suspendFrame; | |
| 135 | |
| 136 // Associated promise resolver. | |
| 137 RefPtrWillBePersistent<ScriptPromiseResolver> m_resolver; | |
| 138 | |
| 139 ExceptionCode m_errorCode; | |
| 140 String m_errorMessage; | |
| 141 }; | 52 }; |
| 142 | 53 |
| 143 } // namespace blink | 54 } // namespace blink |
| 144 | 55 |
| 145 #endif // OfflineAudioContext_h | 56 #endif // OfflineAudioContext_h |
| OLD | NEW |