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

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: Removing ThreadSafeRefCounted from ScheduledSuspendContainer 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
38 // zero. Also ScheduledSuspendContainer is a raw pointer (rather than RefPtr)
39 // because it must be guaranteed that the main thread keeps it alive while the
40 // render thread uses it.
41 using SuspendContainerMap = HashMap<size_t, ScheduledSuspendContainer*, DefaultH ash<size_t>::Hash, WTF::UnsignedWithZeroKeyHashTraits<size_t>>;
34 42
35 class MODULES_EXPORT OfflineAudioContext final : public AudioContext { 43 class MODULES_EXPORT OfflineAudioContext final : public AudioContext {
36 DEFINE_WRAPPERTYPEINFO(); 44 DEFINE_WRAPPERTYPEINFO();
37 public: 45 public:
38 static OfflineAudioContext* create(ExecutionContext*, unsigned numberOfChann els, size_t numberOfFrames, float sampleRate, ExceptionState&); 46 static OfflineAudioContext* create(ExecutionContext*, unsigned numberOfChann els, size_t numberOfFrames, float sampleRate, ExceptionState&);
39 47
40 virtual ~OfflineAudioContext(); 48 virtual ~OfflineAudioContext();
41 49
50 DECLARE_VIRTUAL_TRACE();
51
52 // Fire completion event when the rendering is finished.
53 void fireCompletionEvent() override;
54
55 // Check all the scheduled suspends if the context should suspend at
56 // currentTime(). Then post tasks to resolve promises on the main thread
57 // if necessary.
58 bool shouldSuspendNow() override;
59
60 // Clear suspensions marked as 'resolved' in the list.
61 void resolvePendingSuspendPromises() override;
62
63 DEFINE_ATTRIBUTE_EVENT_LISTENER(complete);
64
42 ScriptPromise startOfflineRendering(ScriptState*); 65 ScriptPromise startOfflineRendering(ScriptState*);
66 ScriptPromise suspendOfflineRendering(ScriptState*, double);
67 ScriptPromise resumeOfflineRendering(ScriptState*);
68
43 private: 69 private:
44 OfflineAudioContext(Document*, unsigned numberOfChannels, size_t numberOfFra mes, float sampleRate); 70 OfflineAudioContext(Document*, unsigned numberOfChannels, size_t numberOfFra mes, float sampleRate);
71
72 // Check a suspend container if it has a duplicate scheduled frame or
73 // is behind the current frame. If the validation fails, post a task to the
74 // main thread to reject the promise.
75 void validateSuspendContainerOnRenderThread(ScheduledSuspendContainer*);
76
77 // Reject a suspend container on the main thread when the validation fails.
78 void rejectSuspendContainerOnMainThread(ScheduledSuspendContainer*);
79
80 // Resolve a pending suspend container.
81 void resolveSuspendContainerOnMainThread(ScheduledSuspendContainer*);
82
83 SuspendContainerMap m_scheduledSuspends;
84 RefPtrWillBeMember<ScriptPromiseResolver> m_completeResolver;
85
86 // This flag is necessary to indicate the rendering has actually started.
87 // Note that initial state of context is 'Suspended', which is the same
88 // state when the context is suspended.
89 bool m_isRenderingStarted;
90
91 // Total render sample length.
92 size_t m_totalRenderFrames;
93 };
94
95 // A container class for a pair of time information and the suspend promise
96 // resolver. This class does not need to be |ThreadSafeRefCounted| because it
97 // needs to be created and destructed in the same thread.
98 class ScheduledSuspendContainer {
99 public:
100 static ScheduledSuspendContainer* create(double suspendTime, size_t suspendF rame, PassRefPtrWillBeRawPtr<ScriptPromiseResolver>);
101 ~ScheduledSuspendContainer();
102
103 double suspendTime() const { return m_suspendTime; }
104 size_t suspendFrame() const { return m_suspendFrame; }
105
106 // Set the error message for the reason of rejection on the render thread
107 // before sending it to the main thread.
108 void setErrorMessageForRejection(ExceptionCode, const String&);
109
110 // {Resolve, Reject} the promise resolver on the main thread.
111 void resolvePromise();
112 void rejectPromise();
113
114 private:
115 ScheduledSuspendContainer(double suspendTime, size_t suspendFrame, PassRefPt rWillBeRawPtr<ScriptPromiseResolver>);
116
117 // Actual suspend time before the quantization by render quantum frame.
118 double m_suspendTime;
119
120 // Suspend sample frame. This is quantized by the render quantum size.
121 size_t m_suspendFrame;
122
123 // Associated promise resolver.
124 RefPtrWillBePersistent<ScriptPromiseResolver> m_resolver;
125
126 ExceptionCode m_errorCode;
127 String m_errorMessage;
45 }; 128 };
46 129
47 } // namespace blink 130 } // namespace blink
48 131
49 #endif // OfflineAudioContext_h 132 #endif // OfflineAudioContext_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698