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

Side by Side Diff: third_party/WebKit/Source/modules/webaudio/AudioContext.cpp

Issue 2501863003: Support for AudioContextOptions latencyHint. (Closed)
Patch Set: Check all LatencyHints WebAudioDeviceImpl test. Created 4 years 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 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "modules/webaudio/AudioContext.h" 5 #include "modules/webaudio/AudioContext.h"
6 6
7 #include "bindings/core/v8/ExceptionMessages.h" 7 #include "bindings/core/v8/ExceptionMessages.h"
8 #include "bindings/core/v8/ExceptionState.h" 8 #include "bindings/core/v8/ExceptionState.h"
9 #include "bindings/core/v8/ScriptPromiseResolver.h" 9 #include "bindings/core/v8/ScriptPromiseResolver.h"
10 #include "core/dom/DOMException.h" 10 #include "core/dom/DOMException.h"
11 #include "core/dom/ExceptionCode.h" 11 #include "core/dom/ExceptionCode.h"
12 #include "core/frame/UseCounter.h" 12 #include "core/frame/UseCounter.h"
13 #include "modules/webaudio/AudioBufferCallback.h" 13 #include "modules/webaudio/AudioBufferCallback.h"
14 #include "modules/webaudio/AudioContextOptions.h"
15 #include "modules/webaudio/DefaultAudioDestinationNode.h"
14 #include "platform/Histogram.h" 16 #include "platform/Histogram.h"
15 #include "platform/audio/AudioUtilities.h" 17 #include "platform/audio/AudioUtilities.h"
18 #include "public/platform/WebAudioLatencyHint.h"
16 19
17 #if DEBUG_AUDIONODE_REFERENCES 20 #if DEBUG_AUDIONODE_REFERENCES
18 #include <stdio.h> 21 #include <stdio.h>
19 #endif 22 #endif
20 23
21 namespace blink { 24 namespace blink {
22 25
23 // Don't allow more than this number of simultaneous AudioContexts 26 // Don't allow more than this number of simultaneous AudioContexts
24 // talking to hardware. 27 // talking to hardware.
25 const unsigned MaxHardwareContexts = 6; 28 const unsigned MaxHardwareContexts = 6;
26 static unsigned s_hardwareContextCount = 0; 29 static unsigned s_hardwareContextCount = 0;
27 static unsigned s_contextId = 0; 30 static unsigned s_contextId = 0;
28 31
29 AudioContext* AudioContext::create(Document& document, 32 AudioContext* AudioContext::create(Document& document,
33 const AudioContextOptions& contextOptions,
30 ExceptionState& exceptionState) { 34 ExceptionState& exceptionState) {
31 DCHECK(isMainThread()); 35 DCHECK(isMainThread());
32 36
33 UseCounter::countCrossOriginIframe(document, 37 UseCounter::countCrossOriginIframe(document,
34 UseCounter::AudioContextCrossOriginIframe); 38 UseCounter::AudioContextCrossOriginIframe);
35 39
36 if (s_hardwareContextCount >= MaxHardwareContexts) { 40 if (s_hardwareContextCount >= MaxHardwareContexts) {
37 exceptionState.throwDOMException( 41 exceptionState.throwDOMException(
38 NotSupportedError, ExceptionMessages::indexExceedsMaximumBound( 42 NotSupportedError, ExceptionMessages::indexExceedsMaximumBound(
39 "number of hardware contexts", 43 "number of hardware contexts",
40 s_hardwareContextCount, MaxHardwareContexts)); 44 s_hardwareContextCount, MaxHardwareContexts));
41 return nullptr; 45 return nullptr;
42 } 46 }
43 47
44 AudioContext* audioContext = new AudioContext(document); 48 WebAudioLatencyHint latencyHint(WebAudioLatencyHint::CategoryInteractive);
49 if (contextOptions.latencyHint().isAudioContextLatencyCategory()) {
50 latencyHint = WebAudioLatencyHint(
51 contextOptions.latencyHint().getAsAudioContextLatencyCategory());
52 }
53 // TODO: add support for latencyHint().isDouble()
54
55 AudioContext* audioContext = new AudioContext(document, latencyHint);
45 audioContext->suspendIfNeeded(); 56 audioContext->suspendIfNeeded();
46 57
47 if (!AudioUtilities::isValidAudioBufferSampleRate( 58 if (!AudioUtilities::isValidAudioBufferSampleRate(
48 audioContext->sampleRate())) { 59 audioContext->sampleRate())) {
49 exceptionState.throwDOMException( 60 exceptionState.throwDOMException(
50 NotSupportedError, 61 NotSupportedError,
51 ExceptionMessages::indexOutsideRange( 62 ExceptionMessages::indexOutsideRange(
52 "hardware sample rate", audioContext->sampleRate(), 63 "hardware sample rate", audioContext->sampleRate(),
53 AudioUtilities::minAudioBufferSampleRate(), 64 AudioUtilities::minAudioBufferSampleRate(),
54 ExceptionMessages::InclusiveBound, 65 ExceptionMessages::InclusiveBound,
(...skipping 21 matching lines...) Expand all
76 ("WebAudio.AudioContext.MaxChannelsAvailable")); 87 ("WebAudio.AudioContext.MaxChannelsAvailable"));
77 DEFINE_STATIC_LOCAL(SparseHistogram, sampleRateHistogram, 88 DEFINE_STATIC_LOCAL(SparseHistogram, sampleRateHistogram,
78 ("WebAudio.AudioContext.HardwareSampleRate")); 89 ("WebAudio.AudioContext.HardwareSampleRate"));
79 maxChannelCountHistogram.sample( 90 maxChannelCountHistogram.sample(
80 audioContext->destination()->maxChannelCount()); 91 audioContext->destination()->maxChannelCount());
81 sampleRateHistogram.sample(audioContext->sampleRate()); 92 sampleRateHistogram.sample(audioContext->sampleRate());
82 93
83 return audioContext; 94 return audioContext;
84 } 95 }
85 96
86 AudioContext::AudioContext(Document& document) 97 AudioContext::AudioContext(Document& document,
87 : BaseAudioContext(&document), m_contextId(s_contextId++) {} 98 const WebAudioLatencyHint& latencyHint)
99 : BaseAudioContext(&document), m_contextId(s_contextId++) {
100 m_destinationNode = DefaultAudioDestinationNode::create(this, latencyHint);
101 initialize();
102 }
88 103
89 AudioContext::~AudioContext() { 104 AudioContext::~AudioContext() {
90 #if DEBUG_AUDIONODE_REFERENCES 105 #if DEBUG_AUDIONODE_REFERENCES
91 fprintf(stderr, "[%16p]: AudioContext::~AudioContext(): %u\n", this, 106 fprintf(stderr, "[%16p]: AudioContext::~AudioContext(): %u\n", this,
92 m_contextId); 107 m_contextId);
93 #endif 108 #endif
94 } 109 }
95 110
96 DEFINE_TRACE(AudioContext) { 111 DEFINE_TRACE(AudioContext) {
97 visitor->trace(m_closeResolver); 112 visitor->trace(m_closeResolver);
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 DCHECK(isMainThread()); 213 DCHECK(isMainThread());
199 DCHECK(destination()); 214 DCHECK(destination());
200 215
201 if (contextState() == Running) { 216 if (contextState() == Running) {
202 destination()->audioDestinationHandler().stopRendering(); 217 destination()->audioDestinationHandler().stopRendering();
203 setContextState(Suspended); 218 setContextState(Suspended);
204 deferredTaskHandler().clearHandlersToBeDeleted(); 219 deferredTaskHandler().clearHandlersToBeDeleted();
205 } 220 }
206 } 221 }
207 222
223 double AudioContext::baseLatency() const {
224 return framesPerBuffer() * 2 / static_cast<double>(sampleRate());
225 }
226
208 } // namespace blink 227 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698