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/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" |
14 #include "platform/Histogram.h" | 15 #include "platform/Histogram.h" |
15 #include "platform/audio/AudioUtilities.h" | 16 #include "platform/audio/AudioUtilities.h" |
| 17 #include "public/platform/WebAudioLatencyHint.h" |
16 | 18 |
17 #if DEBUG_AUDIONODE_REFERENCES | 19 #if DEBUG_AUDIONODE_REFERENCES |
18 #include <stdio.h> | 20 #include <stdio.h> |
19 #endif | 21 #endif |
20 | 22 |
21 namespace blink { | 23 namespace blink { |
22 | 24 |
| 25 namespace { |
| 26 |
| 27 WebAudioLatencyHint::Category LatencyHintCategoryFromString(String category) { |
| 28 WebAudioLatencyHint::Category latencyCategory; |
| 29 if (category == "interactive") { |
| 30 latencyCategory = WebAudioLatencyHint::CategoryInteractive; |
| 31 } else if (category == "balanced") { |
| 32 latencyCategory = WebAudioLatencyHint::CategoryBalanced; |
| 33 } else if (category == "playback") { |
| 34 latencyCategory = WebAudioLatencyHint::CategoryPlayback; |
| 35 } else { |
| 36 NOTREACHED(); |
| 37 latencyCategory = WebAudioLatencyHint::CategoryInteractive; |
| 38 } |
| 39 return latencyCategory; |
| 40 } |
| 41 |
| 42 } // namespace |
| 43 |
23 // Don't allow more than this number of simultaneous AudioContexts | 44 // Don't allow more than this number of simultaneous AudioContexts |
24 // talking to hardware. | 45 // talking to hardware. |
25 const unsigned MaxHardwareContexts = 6; | 46 const unsigned MaxHardwareContexts = 6; |
26 static unsigned s_hardwareContextCount = 0; | 47 static unsigned s_hardwareContextCount = 0; |
27 static unsigned s_contextId = 0; | 48 static unsigned s_contextId = 0; |
28 | 49 |
29 AudioContext* AudioContext::create(Document& document, | 50 AudioContext* AudioContext::create(Document& document, |
| 51 const AudioContextOptions& contextOptions, |
| 52 ExceptionState& exceptionState) { |
| 53 WebAudioLatencyHint::Category latencyCategory = |
| 54 WebAudioLatencyHint::CategoryInteractive; |
| 55 if (contextOptions.latencyHint().isAudioContextLatencyCategory()) { |
| 56 latencyCategory = LatencyHintCategoryFromString( |
| 57 contextOptions.latencyHint().getAsAudioContextLatencyCategory()); |
| 58 } |
| 59 // TODO: add support for latencyHint().isDouble() |
| 60 |
| 61 return AudioContext::create(document, latencyCategory, exceptionState); |
| 62 } |
| 63 |
| 64 AudioContext* AudioContext::create(Document& document, |
| 65 const WebAudioLatencyHint& latencyHint, |
30 ExceptionState& exceptionState) { | 66 ExceptionState& exceptionState) { |
31 DCHECK(isMainThread()); | 67 DCHECK(isMainThread()); |
32 | 68 |
33 UseCounter::countCrossOriginIframe(document, | 69 UseCounter::countCrossOriginIframe(document, |
34 UseCounter::AudioContextCrossOriginIframe); | 70 UseCounter::AudioContextCrossOriginIframe); |
35 | 71 |
36 if (s_hardwareContextCount >= MaxHardwareContexts) { | 72 if (s_hardwareContextCount >= MaxHardwareContexts) { |
37 exceptionState.throwDOMException( | 73 exceptionState.throwDOMException( |
38 NotSupportedError, ExceptionMessages::indexExceedsMaximumBound( | 74 NotSupportedError, ExceptionMessages::indexExceedsMaximumBound( |
39 "number of hardware contexts", | 75 "number of hardware contexts", |
40 s_hardwareContextCount, MaxHardwareContexts)); | 76 s_hardwareContextCount, MaxHardwareContexts)); |
41 return nullptr; | 77 return nullptr; |
42 } | 78 } |
43 | 79 |
44 AudioContext* audioContext = new AudioContext(document); | 80 AudioContext* audioContext = new AudioContext(document, latencyHint); |
45 audioContext->suspendIfNeeded(); | 81 audioContext->suspendIfNeeded(); |
46 | 82 |
47 if (!AudioUtilities::isValidAudioBufferSampleRate( | 83 if (!AudioUtilities::isValidAudioBufferSampleRate( |
48 audioContext->sampleRate())) { | 84 audioContext->sampleRate())) { |
49 exceptionState.throwDOMException( | 85 exceptionState.throwDOMException( |
50 NotSupportedError, | 86 NotSupportedError, |
51 ExceptionMessages::indexOutsideRange( | 87 ExceptionMessages::indexOutsideRange( |
52 "hardware sample rate", audioContext->sampleRate(), | 88 "hardware sample rate", audioContext->sampleRate(), |
53 AudioUtilities::minAudioBufferSampleRate(), | 89 AudioUtilities::minAudioBufferSampleRate(), |
54 ExceptionMessages::InclusiveBound, | 90 ExceptionMessages::InclusiveBound, |
(...skipping 21 matching lines...) Expand all Loading... |
76 ("WebAudio.AudioContext.MaxChannelsAvailable")); | 112 ("WebAudio.AudioContext.MaxChannelsAvailable")); |
77 DEFINE_STATIC_LOCAL(SparseHistogram, sampleRateHistogram, | 113 DEFINE_STATIC_LOCAL(SparseHistogram, sampleRateHistogram, |
78 ("WebAudio.AudioContext.HardwareSampleRate")); | 114 ("WebAudio.AudioContext.HardwareSampleRate")); |
79 maxChannelCountHistogram.sample( | 115 maxChannelCountHistogram.sample( |
80 audioContext->destination()->maxChannelCount()); | 116 audioContext->destination()->maxChannelCount()); |
81 sampleRateHistogram.sample(audioContext->sampleRate()); | 117 sampleRateHistogram.sample(audioContext->sampleRate()); |
82 | 118 |
83 return audioContext; | 119 return audioContext; |
84 } | 120 } |
85 | 121 |
86 AudioContext::AudioContext(Document& document) | 122 AudioContext::AudioContext(Document& document, |
87 : BaseAudioContext(&document), m_contextId(s_contextId++) {} | 123 const WebAudioLatencyHint& latencyHint) |
| 124 : BaseAudioContext(&document, latencyHint), m_contextId(s_contextId++) {} |
88 | 125 |
89 AudioContext::~AudioContext() { | 126 AudioContext::~AudioContext() { |
90 #if DEBUG_AUDIONODE_REFERENCES | 127 #if DEBUG_AUDIONODE_REFERENCES |
91 fprintf(stderr, "[%16p]: AudioContext::~AudioContext(): %u\n", this, | 128 fprintf(stderr, "[%16p]: AudioContext::~AudioContext(): %u\n", this, |
92 m_contextId); | 129 m_contextId); |
93 #endif | 130 #endif |
94 } | 131 } |
95 | 132 |
96 DEFINE_TRACE(AudioContext) { | 133 DEFINE_TRACE(AudioContext) { |
97 visitor->trace(m_closeResolver); | 134 visitor->trace(m_closeResolver); |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 DCHECK(isMainThread()); | 235 DCHECK(isMainThread()); |
199 DCHECK(destination()); | 236 DCHECK(destination()); |
200 | 237 |
201 if (contextState() == Running) { | 238 if (contextState() == Running) { |
202 destination()->audioDestinationHandler().stopRendering(); | 239 destination()->audioDestinationHandler().stopRendering(); |
203 setContextState(Suspended); | 240 setContextState(Suspended); |
204 deferredTaskHandler().clearHandlersToBeDeleted(); | 241 deferredTaskHandler().clearHandlersToBeDeleted(); |
205 } | 242 } |
206 } | 243 } |
207 | 244 |
| 245 double AudioContext::baseLatency() const { |
| 246 return framesPerBuffer() * 2 / static_cast<double>(sampleRate()); |
| 247 } |
| 248 |
208 } // namespace blink | 249 } // namespace blink |
OLD | NEW |