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 #include "wtf/HashSet.h" |
| 33 |
31 namespace blink { | 34 namespace blink { |
32 | 35 |
33 class ExceptionState; | 36 class ExceptionState; |
| 37 class ScheduledSuspendContainer; |
| 38 class OfflineAudioDestinationHandler; |
| 39 |
| 40 // The HashMap with 'zero' key is needed because |currentSampleFrame| can be |
| 41 // zero. Also ScheduledSuspendContainer is a raw pointer (rather than RefPtr) |
| 42 // because it must be guaranteed that the main thread keeps it alive while the |
| 43 // render thread uses it. |
| 44 using SuspendContainerMap = HashMap<size_t, ScheduledSuspendContainer*, DefaultH
ash<size_t>::Hash, WTF::UnsignedWithZeroKeyHashTraits<size_t>>; |
34 | 45 |
35 class MODULES_EXPORT OfflineAudioContext final : public AbstractAudioContext { | 46 class MODULES_EXPORT OfflineAudioContext final : public AbstractAudioContext { |
36 DEFINE_WRAPPERTYPEINFO(); | 47 DEFINE_WRAPPERTYPEINFO(); |
37 public: | 48 public: |
38 static OfflineAudioContext* create(ExecutionContext*, unsigned numberOfChann
els, size_t numberOfFrames, float sampleRate, ExceptionState&); | 49 static OfflineAudioContext* create(ExecutionContext*, unsigned numberOfChann
els, size_t numberOfFrames, float sampleRate, ExceptionState&); |
39 | 50 |
40 ~OfflineAudioContext() override; | 51 ~OfflineAudioContext() override; |
41 | 52 |
| 53 DECLARE_VIRTUAL_TRACE(); |
| 54 |
| 55 DEFINE_ATTRIBUTE_EVENT_LISTENER(complete); |
| 56 |
| 57 // Check all the scheduled suspends if the context should suspend at |
| 58 // currentTime(). Then post tasks to resolve promises on the main thread |
| 59 // if necessary. |
| 60 bool shouldSuspendNow(); |
| 61 |
| 62 // Clear suspensions marked as 'resolved' in the list. |
| 63 void resolvePendingSuspendPromises(); |
| 64 |
| 65 // Fire completion event when the rendering is finished. |
| 66 void fireCompletionEvent(); |
| 67 |
42 ScriptPromise startOfflineRendering(ScriptState*); | 68 ScriptPromise startOfflineRendering(ScriptState*); |
43 | 69 |
44 ScriptPromise closeContext(ScriptState*) final; | 70 ScriptPromise closeContext(ScriptState*) final; |
| 71 ScriptPromise suspendContext(ScriptState*, double) final; |
| 72 ScriptPromise resumeContext(ScriptState*) final; |
| 73 |
| 74 // This is to implement the pure virtual method from AbstractAudioContext. |
| 75 // CANNOT be called on OfflineAudioContext. |
45 ScriptPromise suspendContext(ScriptState*) final; | 76 ScriptPromise suspendContext(ScriptState*) final; |
46 ScriptPromise resumeContext(ScriptState*) final; | |
47 | 77 |
48 bool hasRealtimeConstraint() final { return false; } | 78 bool hasRealtimeConstraint() final { return false; } |
49 | 79 |
50 private: | 80 private: |
51 OfflineAudioContext(Document*, unsigned numberOfChannels, size_t numberOfFra
mes, float sampleRate); | 81 OfflineAudioContext(Document*, unsigned numberOfChannels, size_t numberOfFra
mes, float sampleRate); |
| 82 |
| 83 // Fetch directly the destination handler. |
| 84 OfflineAudioDestinationHandler& destinationHandler(); |
| 85 |
| 86 // Check a suspend container if it has a duplicate scheduled frame or |
| 87 // is behind the current frame. If the validation fails, post a task to the |
| 88 // main thread to reject the promise. |
| 89 void validateSuspendContainerOnRenderThread(ScheduledSuspendContainer*, doub
le, size_t); |
| 90 |
| 91 // Reject a suspend container on the main thread when the validation fails. |
| 92 void rejectSuspendContainerOnMainThread(ScheduledSuspendContainer*, const St
ring&); |
| 93 |
| 94 // Resolve a pending suspend container. |
| 95 void resolveSuspendContainerOnMainThread(ScheduledSuspendContainer*); |
| 96 |
| 97 // This set is for maintaining the ownership of SchduledSuspendContainer |
| 98 // raw pointers. It is accessed only on the main thread and its elements |
| 99 // (ScheduledSuspendContainer*) are constructed/destructed on the main |
| 100 // thread. |
| 101 HashSet<ScheduledSuspendContainer*> m_suspendContainers; |
| 102 |
| 103 // This map is to check the timing of scheduled suspends. This is accessed |
| 104 // only by the offline render thread and its value |
| 105 // (ScheduledSuspendContainer*) must not be dereferenced in the render |
| 106 // thread. |
| 107 SuspendContainerMap m_scheduledSuspendContainers; |
| 108 |
| 109 RefPtrWillBeMember<ScriptPromiseResolver> m_completeResolver; |
| 110 |
| 111 // This flag is necessary to indicate the rendering has actually started. |
| 112 // Note that initial state of context is 'Suspended', which is the same |
| 113 // state when the context is suspended. |
| 114 bool m_isRenderingStarted; |
| 115 |
| 116 // Total render sample length. |
| 117 size_t m_totalRenderFrames; |
| 118 }; |
| 119 |
| 120 // A container class for a pair of time information and the suspend promise |
| 121 // resolver. This class does not need to be |ThreadSafeRefCounted| because it |
| 122 // needs to be created and destructed in the main thread. |
| 123 class ScheduledSuspendContainer { |
| 124 public: |
| 125 static ScheduledSuspendContainer* create(PassRefPtrWillBeRawPtr<ScriptPromis
eResolver>); |
| 126 ~ScheduledSuspendContainer(); |
| 127 |
| 128 // {Resolve, Reject} the promise resolver on the main thread. |
| 129 void resolvePromise(); |
| 130 void rejectPromise(ExceptionCode, const String&); |
| 131 |
| 132 private: |
| 133 ScheduledSuspendContainer(PassRefPtrWillBeRawPtr<ScriptPromiseResolver>); |
| 134 |
| 135 // Associated promise resolver. |
| 136 RefPtrWillBePersistent<ScriptPromiseResolver> m_resolver; |
52 }; | 137 }; |
53 | 138 |
54 } // namespace blink | 139 } // namespace blink |
55 | 140 |
56 #endif // OfflineAudioContext_h | 141 #endif // OfflineAudioContext_h |
OLD | NEW |