| 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/html/HTMLMediaElement.h" |
| 13 #include "modules/mediastream/MediaStream.h" |
| 12 #include "modules/webaudio/AudioBufferCallback.h" | 14 #include "modules/webaudio/AudioBufferCallback.h" |
| 15 #include "modules/webaudio/MediaElementAudioSourceNode.h" |
| 16 #include "modules/webaudio/MediaStreamAudioDestinationNode.h" |
| 17 #include "modules/webaudio/MediaStreamAudioSourceNode.h" |
| 13 #include "platform/audio/AudioUtilities.h" | 18 #include "platform/audio/AudioUtilities.h" |
| 14 | 19 |
| 15 #if DEBUG_AUDIONODE_REFERENCES | 20 #if DEBUG_AUDIONODE_REFERENCES |
| 16 #include <stdio.h> | 21 #include <stdio.h> |
| 17 #endif | 22 #endif |
| 18 | 23 |
| 19 namespace blink { | 24 namespace blink { |
| 20 | 25 |
| 21 // Don't allow more than this number of simultaneous AudioContexts | 26 // Don't allow more than this number of simultaneous AudioContexts |
| 22 // talking to hardware. | 27 // talking to hardware. |
| 23 const unsigned MaxHardwareContexts = 6; | 28 const unsigned MaxHardwareContexts = 6; |
| 24 static unsigned s_hardwareContextCount = 0; | 29 static unsigned s_hardwareContextCount = 0; |
| 25 static unsigned s_contextId = 0; | 30 static unsigned s_contextId = 0; |
| 26 | 31 |
| 27 AbstractAudioContext* AudioContext::create(Document& document, ExceptionState& e
xceptionState) | 32 AudioContext* AudioContext::create(Document& document, ExceptionState& exception
State) |
| 28 { | 33 { |
| 29 ASSERT(isMainThread()); | 34 ASSERT(isMainThread()); |
| 30 if (s_hardwareContextCount >= MaxHardwareContexts) { | 35 if (s_hardwareContextCount >= MaxHardwareContexts) { |
| 31 exceptionState.throwDOMException( | 36 exceptionState.throwDOMException( |
| 32 NotSupportedError, | 37 NotSupportedError, |
| 33 ExceptionMessages::indexExceedsMaximumBound( | 38 ExceptionMessages::indexExceedsMaximumBound( |
| 34 "number of hardware contexts", | 39 "number of hardware contexts", |
| 35 s_hardwareContextCount, | 40 s_hardwareContextCount, |
| 36 MaxHardwareContexts)); | 41 MaxHardwareContexts)); |
| 37 return nullptr; | 42 return nullptr; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 63 ++s_hardwareContextCount; | 68 ++s_hardwareContextCount; |
| 64 #if DEBUG_AUDIONODE_REFERENCES | 69 #if DEBUG_AUDIONODE_REFERENCES |
| 65 fprintf(stderr, "%p: AudioContext::AudioContext(): %u #%u\n", | 70 fprintf(stderr, "%p: AudioContext::AudioContext(): %u #%u\n", |
| 66 audioContext, audioContext->m_contextId, s_hardwareContextCount); | 71 audioContext, audioContext->m_contextId, s_hardwareContextCount); |
| 67 #endif | 72 #endif |
| 68 | 73 |
| 69 return audioContext; | 74 return audioContext; |
| 70 } | 75 } |
| 71 | 76 |
| 72 AudioContext::AudioContext(Document& document) | 77 AudioContext::AudioContext(Document& document) |
| 73 : AbstractAudioContext(&document) | 78 : BaseAudioContext(&document) |
| 74 , m_contextId(s_contextId++) | 79 , m_contextId(s_contextId++) |
| 75 { | 80 { |
| 76 } | 81 } |
| 77 | 82 |
| 78 AudioContext::~AudioContext() | 83 AudioContext::~AudioContext() |
| 79 { | 84 { |
| 80 #if DEBUG_AUDIONODE_REFERENCES | 85 #if DEBUG_AUDIONODE_REFERENCES |
| 81 fprintf(stderr, "%p: AudioContext::~AudioContext(): %u\n", this, m_contextId
); | 86 fprintf(stderr, "%p: AudioContext::~AudioContext(): %u\n", this, m_contextId
); |
| 82 #endif | 87 #endif |
| 83 } | 88 } |
| 84 | 89 |
| 85 DEFINE_TRACE(AudioContext) | 90 DEFINE_TRACE(AudioContext) |
| 86 { | 91 { |
| 87 visitor->trace(m_closeResolver); | 92 visitor->trace(m_closeResolver); |
| 88 AbstractAudioContext::trace(visitor); | 93 BaseAudioContext::trace(visitor); |
| 94 } |
| 95 |
| 96 MediaElementAudioSourceNode* AudioContext::createMediaElementSource(HTMLMediaEle
ment* mediaElement, ExceptionState& exceptionState) |
| 97 { |
| 98 ASSERT(isMainThread()); |
| 99 |
| 100 if (isContextClosed()) { |
| 101 throwExceptionForClosedState(exceptionState); |
| 102 return nullptr; |
| 103 } |
| 104 |
| 105 // First check if this media element already has a source node. |
| 106 if (mediaElement->audioSourceNode()) { |
| 107 exceptionState.throwDOMException( |
| 108 InvalidStateError, |
| 109 "HTMLMediaElement already connected previously to a different MediaE
lementSourceNode."); |
| 110 return nullptr; |
| 111 } |
| 112 |
| 113 MediaElementAudioSourceNode* node = MediaElementAudioSourceNode::create(*thi
s, *mediaElement); |
| 114 |
| 115 mediaElement->setAudioSourceNode(node); |
| 116 |
| 117 notifySourceNodeStartedProcessing(node); // context keeps reference until no
de is disconnected |
| 118 return node; |
| 119 } |
| 120 |
| 121 MediaStreamAudioSourceNode* AudioContext::createMediaStreamSource(MediaStream* m
ediaStream, ExceptionState& exceptionState) |
| 122 { |
| 123 ASSERT(isMainThread()); |
| 124 |
| 125 if (isContextClosed()) { |
| 126 throwExceptionForClosedState(exceptionState); |
| 127 return nullptr; |
| 128 } |
| 129 |
| 130 MediaStreamTrackVector audioTracks = mediaStream->getAudioTracks(); |
| 131 if (audioTracks.isEmpty()) { |
| 132 exceptionState.throwDOMException( |
| 133 InvalidStateError, |
| 134 "MediaStream has no audio track"); |
| 135 return nullptr; |
| 136 } |
| 137 |
| 138 // Use the first audio track in the media stream. |
| 139 MediaStreamTrack* audioTrack = audioTracks[0]; |
| 140 OwnPtr<AudioSourceProvider> provider = audioTrack->createWebAudioSource(); |
| 141 MediaStreamAudioSourceNode* node = MediaStreamAudioSourceNode::create(*this,
*mediaStream, audioTrack, provider.release()); |
| 142 |
| 143 // FIXME: Only stereo streams are supported right now. We should be able to
accept multi-channel streams. |
| 144 node->setFormat(2, sampleRate()); |
| 145 |
| 146 notifySourceNodeStartedProcessing(node); // context keeps reference until no
de is disconnected |
| 147 return node; |
| 148 } |
| 149 |
| 150 MediaStreamAudioDestinationNode* AudioContext::createMediaStreamDestination(Exce
ptionState& exceptionState) |
| 151 { |
| 152 if (isContextClosed()) { |
| 153 throwExceptionForClosedState(exceptionState); |
| 154 return nullptr; |
| 155 } |
| 156 |
| 157 // Set number of output channels to stereo by default. |
| 158 return MediaStreamAudioDestinationNode::create(*this, 2); |
| 89 } | 159 } |
| 90 | 160 |
| 91 ScriptPromise AudioContext::suspendContext(ScriptState* scriptState) | 161 ScriptPromise AudioContext::suspendContext(ScriptState* scriptState) |
| 92 { | 162 { |
| 93 ASSERT(isMainThread()); | 163 ASSERT(isMainThread()); |
| 94 AutoLocker locker(this); | 164 AutoLocker locker(this); |
| 95 | 165 |
| 96 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 166 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 97 ScriptPromise promise = resolver->promise(); | 167 ScriptPromise promise = resolver->promise(); |
| 98 | 168 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 | 245 |
| 176 ASSERT(s_hardwareContextCount); | 246 ASSERT(s_hardwareContextCount); |
| 177 --s_hardwareContextCount; | 247 --s_hardwareContextCount; |
| 178 | 248 |
| 179 if (m_closeResolver) | 249 if (m_closeResolver) |
| 180 m_closeResolver->resolve(); | 250 m_closeResolver->resolve(); |
| 181 } | 251 } |
| 182 | 252 |
| 183 bool AudioContext::isContextClosed() const | 253 bool AudioContext::isContextClosed() const |
| 184 { | 254 { |
| 185 return m_closeResolver || AbstractAudioContext::isContextClosed(); | 255 return m_closeResolver || BaseAudioContext::isContextClosed(); |
| 186 } | 256 } |
| 187 | 257 |
| 188 void AudioContext::stopRendering() | 258 void AudioContext::stopRendering() |
| 189 { | 259 { |
| 190 ASSERT(isMainThread()); | 260 ASSERT(isMainThread()); |
| 191 ASSERT(destination()); | 261 ASSERT(destination()); |
| 192 | 262 |
| 193 if (contextState() == Running) { | 263 if (contextState() == Running) { |
| 194 destination()->audioDestinationHandler().stopRendering(); | 264 destination()->audioDestinationHandler().stopRendering(); |
| 195 setContextState(Suspended); | 265 setContextState(Suspended); |
| 196 deferredTaskHandler().clearHandlersToBeDeleted(); | 266 deferredTaskHandler().clearHandlersToBeDeleted(); |
| 197 } | 267 } |
| 198 } | 268 } |
| 199 | 269 |
| 200 } // namespace blink | 270 } // namespace blink |
| 201 | 271 |
| OLD | NEW |