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

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: Bring to ToT 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
11 * documentation and/or other materials provided with the distribution. 11 * documentation and/or other materials provided with the distribution.
12 * 12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND AN Y 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND AN Y
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR AN Y 16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR AN Y
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND O N 19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND O N
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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/AudioContext.h" 29 #include "modules/webaudio/AudioContext.h"
30 #include "wtf/HashMap.h"
30 31
31 namespace blink { 32 namespace blink {
32 33
33 class ExceptionState; 34 class ExceptionState;
35 class ScheduledSuspendContainer;
36
37 // The HashMap with 'zero' key is needed because |currentSampleFrame| can be zer o.
38 using SuspendContainerMap = HashMap<size_t, RefPtr<ScheduledSuspendContainer>, D efaultHash<size_t>::Hash, WTF::UnsignedWithZeroKeyHashTraits<size_t>>;
haraken 2015/07/08 04:05:41 Actually this ScheduledSuspendContainer doesn't ne
hongchan 2015/07/08 17:47:32 Done. However, I am having a cross-thread issue in
34 39
35 class MODULES_EXPORT OfflineAudioContext final : public AudioContext { 40 class MODULES_EXPORT OfflineAudioContext final : public AudioContext {
36 DEFINE_WRAPPERTYPEINFO(); 41 DEFINE_WRAPPERTYPEINFO();
37 public: 42 public:
38 static OfflineAudioContext* create(ExecutionContext*, unsigned numberOfChann els, size_t numberOfFrames, float sampleRate, ExceptionState&); 43 static OfflineAudioContext* create(ExecutionContext*, unsigned numberOfChann els, size_t numberOfFrames, float sampleRate, ExceptionState&);
39 44
40 virtual ~OfflineAudioContext(); 45 virtual ~OfflineAudioContext();
41 46
47 DECLARE_VIRTUAL_TRACE();
48
49 // Fire completion event when the rendering is finished.
50 void fireCompletionEvent() override;
51
52 // Check all the scheduled suspends if the context should suspend at
53 // currentTime(). Then post tasks to resolve promises on the main thread
54 // if necessary.
55 bool shouldSuspendNow() override;
56
57 // Clear suspensions marked as 'resolved' in the list.
58 void resolvePendingSuspendPromises() override;
59
60 DEFINE_ATTRIBUTE_EVENT_LISTENER(complete);
61
42 ScriptPromise startOfflineRendering(ScriptState*); 62 ScriptPromise startOfflineRendering(ScriptState*);
63 ScriptPromise suspendOfflineRendering(ScriptState*, double);
64 ScriptPromise resumeOfflineRendering(ScriptState*);
65
43 private: 66 private:
44 OfflineAudioContext(Document*, unsigned numberOfChannels, size_t numberOfFra mes, float sampleRate); 67 OfflineAudioContext(Document*, unsigned numberOfChannels, size_t numberOfFra mes, float sampleRate);
68
69 // Check a suspend container if it has a duplicate scheduled frame or
70 // is behind the current frame. If the validation fails, post a task to the
71 // main thread to reject the promise.
72 void validateSuspendContainerOnRenderThread(PassRefPtr<ScheduledSuspendConta iner>);
73
74 // Reject a suspend container on the main thread when the validation fails.
75 void rejectSuspendContainerOnMainThread(PassRefPtr<ScheduledSuspendContainer >);
76
77 // Resolve a pending suspend container and removes it from the map.
78 void resolveSuspendContainerOnMainThread(PassRefPtr<ScheduledSuspendContaine r>);
79
80 SuspendContainerMap m_scheduledSuspends;
81 RefPtrWillBeMember<ScriptPromiseResolver> m_completeResolver;
82
83 // This flag is necessary to indicate the rendering has actually started.
84 // Note that initial state of context is 'Suspended', which is the same
85 // state when the context is suspended.
86 bool m_isRenderingStarted;
haraken 2015/07/08 04:05:41 I'm not sure but would it be better to introduce a
hongchan 2015/07/08 17:47:32 rtoy@ and I thought about having a 'created' state
87
88 // Total render sample length.
89 size_t m_totalRenderFrames;
90 };
91
92 // A container class for a pair of time information and the suspend promise
93 // resolver.
94 //
95 // TODO(hongchan): This class is |ThreadSafeRefCounted| because it needs to be
96 // accessed by the offline render thread which is not oilpan-enabled. This is a
97 // short-term solution until the offline render thread gets oilpan coverage.
98 class ScheduledSuspendContainer : public ThreadSafeRefCounted<ScheduledSuspendCo ntainer> {
haraken 2015/07/08 04:05:42 As commented above, the render thread can keep Sch
hongchan 2015/07/08 17:47:32 Unfortunately, |resolvePendingSuspendPromises()| c
99 public:
100 static PassRefPtr<ScheduledSuspendContainer> create(double suspendTime, size _t suspendFrame, PassRefPtrWillBeRawPtr<ScriptPromiseResolver>);
101 ~ScheduledSuspendContainer();
102
103 double suspendTime() const { return m_suspendTime; }
104 size_t suspendFrame() const { return m_suspendFrame; }
105
106 // Query if the rendering should be suspended at |whenFrame|.
107 bool shouldSuspendAtFrame(size_t whenFrame) const;
108
109 // Set the error message for the reason of rejection on the render thread
110 // before sending it to the main thread.
111 void setErrorMessageForRejection(ExceptionCode, const String&);
112
113 // {Resolve, Reject} the promise resolver on the main thread.
114 void resolvePromise();
115 void rejectPromise();
116
117 private:
118 ScheduledSuspendContainer(double suspendTime, size_t suspendFrame, PassRefPt rWillBeRawPtr<ScriptPromiseResolver>);
119
120 // Actual suspend time before the quantization by render quantum frame.
121 double m_suspendTime;
122
123 // Suspend sample frame. This is quantized by the render quantum size.
124 size_t m_suspendFrame;
125
126 // Associated promise resolver.
127 RefPtrWillBePersistent<ScriptPromiseResolver> m_resolver;
128
129 ExceptionCode m_errorCode;
130 String m_errorMessage;
45 }; 131 };
46 132
47 } // namespace blink 133 } // namespace blink
48 134
49 #endif // OfflineAudioContext_h 135 #endif // OfflineAudioContext_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698