OLD | NEW |
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/LocalDOMWindow.h" | 12 #include "core/frame/LocalDOMWindow.h" |
13 #include "core/frame/UseCounter.h" | 13 #include "core/frame/UseCounter.h" |
14 #include "core/timing/DOMWindowPerformance.h" | 14 #include "core/timing/DOMWindowPerformance.h" |
15 #include "core/timing/Performance.h" | 15 #include "core/timing/Performance.h" |
16 #include "modules/webaudio/AudioBufferCallback.h" | 16 #include "modules/webaudio/AudioBufferCallback.h" |
| 17 #include "modules/webaudio/AudioContextOptions.h" |
17 #include "modules/webaudio/AudioTimestamp.h" | 18 #include "modules/webaudio/AudioTimestamp.h" |
| 19 #include "modules/webaudio/DefaultAudioDestinationNode.h" |
18 #include "platform/Histogram.h" | 20 #include "platform/Histogram.h" |
19 #include "platform/audio/AudioUtilities.h" | 21 #include "platform/audio/AudioUtilities.h" |
| 22 #include "public/platform/WebAudioLatencyHint.h" |
20 | 23 |
21 #if DEBUG_AUDIONODE_REFERENCES | 24 #if DEBUG_AUDIONODE_REFERENCES |
22 #include <stdio.h> | 25 #include <stdio.h> |
23 #endif | 26 #endif |
24 | 27 |
25 namespace blink { | 28 namespace blink { |
26 | 29 |
27 // Don't allow more than this number of simultaneous AudioContexts | 30 // Don't allow more than this number of simultaneous AudioContexts |
28 // talking to hardware. | 31 // talking to hardware. |
29 const unsigned MaxHardwareContexts = 6; | 32 const unsigned MaxHardwareContexts = 6; |
30 static unsigned s_hardwareContextCount = 0; | 33 static unsigned s_hardwareContextCount = 0; |
31 static unsigned s_contextId = 0; | 34 static unsigned s_contextId = 0; |
32 | 35 |
33 AudioContext* AudioContext::create(Document& document, | 36 AudioContext* AudioContext::create(Document& document, |
| 37 const AudioContextOptions& contextOptions, |
34 ExceptionState& exceptionState) { | 38 ExceptionState& exceptionState) { |
35 DCHECK(isMainThread()); | 39 DCHECK(isMainThread()); |
36 | 40 |
37 UseCounter::countCrossOriginIframe(document, | 41 UseCounter::countCrossOriginIframe(document, |
38 UseCounter::AudioContextCrossOriginIframe); | 42 UseCounter::AudioContextCrossOriginIframe); |
39 | 43 |
40 if (s_hardwareContextCount >= MaxHardwareContexts) { | 44 if (s_hardwareContextCount >= MaxHardwareContexts) { |
41 exceptionState.throwDOMException( | 45 exceptionState.throwDOMException( |
42 NotSupportedError, ExceptionMessages::indexExceedsMaximumBound( | 46 NotSupportedError, ExceptionMessages::indexExceedsMaximumBound( |
43 "number of hardware contexts", | 47 "number of hardware contexts", |
44 s_hardwareContextCount, MaxHardwareContexts)); | 48 s_hardwareContextCount, MaxHardwareContexts)); |
45 return nullptr; | 49 return nullptr; |
46 } | 50 } |
47 | 51 |
48 AudioContext* audioContext = new AudioContext(document); | 52 WebAudioLatencyHint latencyHint(WebAudioLatencyHint::kCategoryInteractive); |
| 53 if (contextOptions.latencyHint().isAudioContextLatencyCategory()) { |
| 54 latencyHint = WebAudioLatencyHint( |
| 55 contextOptions.latencyHint().getAsAudioContextLatencyCategory()); |
| 56 } |
| 57 // TODO: add support for latencyHint().isDouble() |
| 58 |
| 59 AudioContext* audioContext = new AudioContext(document, latencyHint); |
49 audioContext->suspendIfNeeded(); | 60 audioContext->suspendIfNeeded(); |
50 | 61 |
51 if (!AudioUtilities::isValidAudioBufferSampleRate( | 62 if (!AudioUtilities::isValidAudioBufferSampleRate( |
52 audioContext->sampleRate())) { | 63 audioContext->sampleRate())) { |
53 exceptionState.throwDOMException( | 64 exceptionState.throwDOMException( |
54 NotSupportedError, | 65 NotSupportedError, |
55 ExceptionMessages::indexOutsideRange( | 66 ExceptionMessages::indexOutsideRange( |
56 "hardware sample rate", audioContext->sampleRate(), | 67 "hardware sample rate", audioContext->sampleRate(), |
57 AudioUtilities::minAudioBufferSampleRate(), | 68 AudioUtilities::minAudioBufferSampleRate(), |
58 ExceptionMessages::InclusiveBound, | 69 ExceptionMessages::InclusiveBound, |
(...skipping 21 matching lines...) Expand all Loading... |
80 ("WebAudio.AudioContext.MaxChannelsAvailable")); | 91 ("WebAudio.AudioContext.MaxChannelsAvailable")); |
81 DEFINE_STATIC_LOCAL(SparseHistogram, sampleRateHistogram, | 92 DEFINE_STATIC_LOCAL(SparseHistogram, sampleRateHistogram, |
82 ("WebAudio.AudioContext.HardwareSampleRate")); | 93 ("WebAudio.AudioContext.HardwareSampleRate")); |
83 maxChannelCountHistogram.sample( | 94 maxChannelCountHistogram.sample( |
84 audioContext->destination()->maxChannelCount()); | 95 audioContext->destination()->maxChannelCount()); |
85 sampleRateHistogram.sample(audioContext->sampleRate()); | 96 sampleRateHistogram.sample(audioContext->sampleRate()); |
86 | 97 |
87 return audioContext; | 98 return audioContext; |
88 } | 99 } |
89 | 100 |
90 AudioContext::AudioContext(Document& document) | 101 AudioContext::AudioContext(Document& document, |
91 : BaseAudioContext(&document), m_contextId(s_contextId++) {} | 102 const WebAudioLatencyHint& latencyHint) |
| 103 : BaseAudioContext(&document), m_contextId(s_contextId++) { |
| 104 m_destinationNode = DefaultAudioDestinationNode::create(this, latencyHint); |
| 105 initialize(); |
| 106 } |
92 | 107 |
93 AudioContext::~AudioContext() { | 108 AudioContext::~AudioContext() { |
94 #if DEBUG_AUDIONODE_REFERENCES | 109 #if DEBUG_AUDIONODE_REFERENCES |
95 fprintf(stderr, "[%16p]: AudioContext::~AudioContext(): %u\n", this, | 110 fprintf(stderr, "[%16p]: AudioContext::~AudioContext(): %u\n", this, |
96 m_contextId); | 111 m_contextId); |
97 #endif | 112 #endif |
98 } | 113 } |
99 | 114 |
100 DEFINE_TRACE(AudioContext) { | 115 DEFINE_TRACE(AudioContext) { |
101 visitor->trace(m_closeResolver); | 116 visitor->trace(m_closeResolver); |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 DCHECK(isMainThread()); | 244 DCHECK(isMainThread()); |
230 DCHECK(destination()); | 245 DCHECK(destination()); |
231 | 246 |
232 if (contextState() == Running) { | 247 if (contextState() == Running) { |
233 destination()->audioDestinationHandler().stopRendering(); | 248 destination()->audioDestinationHandler().stopRendering(); |
234 setContextState(Suspended); | 249 setContextState(Suspended); |
235 deferredTaskHandler().clearHandlersToBeDeleted(); | 250 deferredTaskHandler().clearHandlersToBeDeleted(); |
236 } | 251 } |
237 } | 252 } |
238 | 253 |
| 254 double AudioContext::baseLatency() const { |
| 255 return framesPerBuffer() * 2 / static_cast<double>(sampleRate()); |
| 256 } |
| 257 |
239 } // namespace blink | 258 } // namespace blink |
OLD | NEW |