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

Side by Side 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 unified diff | Download patch
OLDNEW
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
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
31 namespace blink { 33 namespace blink {
32 34
33 class ExceptionState; 35 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
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.
42 // render thread uses it.
43 using SuspendContainerMap = HashMap<size_t, ScheduledSuspendContainer*, DefaultH ash<size_t>::Hash, WTF::UnsignedWithZeroKeyHashTraits<size_t>>;
34 44
35 class MODULES_EXPORT OfflineAudioContext final : public AbstractAudioContext { 45 class MODULES_EXPORT OfflineAudioContext final : public AbstractAudioContext {
36 DEFINE_WRAPPERTYPEINFO(); 46 DEFINE_WRAPPERTYPEINFO();
37 public: 47 public:
38 static OfflineAudioContext* create(ExecutionContext*, unsigned numberOfChann els, size_t numberOfFrames, float sampleRate, ExceptionState&); 48 static OfflineAudioContext* create(ExecutionContext*, unsigned numberOfChann els, size_t numberOfFrames, float sampleRate, ExceptionState&);
39 49
40 ~OfflineAudioContext() override; 50 ~OfflineAudioContext() override;
41 51
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
42 ScriptPromise startOfflineRendering(ScriptState*); 67 ScriptPromise startOfflineRendering(ScriptState*);
43 68
44 ScriptPromise closeContext(ScriptState*) final; 69 ScriptPromise closeContext(ScriptState*) final;
70 ScriptPromise suspendContext(ScriptState*, double) final;
71 ScriptPromise resumeContext(ScriptState*) final;
72
73 // This is to implement the pure virtual method from AbstractAudioContext.
74 // CANNOT be called on OfflineAudioContext.
45 ScriptPromise suspendContext(ScriptState*) final; 75 ScriptPromise suspendContext(ScriptState*) final;
46 ScriptPromise resumeContext(ScriptState*) final;
47 76
48 bool hasRealtimeConstraint() final { return false; } 77 bool hasRealtimeConstraint() final { return false; }
49 78
50 private: 79 private:
51 OfflineAudioContext(Document*, unsigned numberOfChannels, size_t numberOfFra mes, float sampleRate); 80 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 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
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;
52 }; 141 };
53 142
54 } // namespace blink 143 } // namespace blink
55 144
56 #endif // OfflineAudioContext_h 145 #endif // OfflineAudioContext_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698