OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2011, Google Inc. All rights reserved. | 2 * Copyright (C) 2011, 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 #include "config.h" | 25 #include "config.h" |
26 #if ENABLE(WEB_AUDIO) | 26 #if ENABLE(WEB_AUDIO) |
27 #include "modules/webaudio/AsyncAudioDecoder.h" | 27 #include "modules/webaudio/AsyncAudioDecoder.h" |
28 | 28 |
| 29 #include "bindings/core/v8/ScriptPromiseResolver.h" |
29 #include "core/dom/DOMArrayBuffer.h" | 30 #include "core/dom/DOMArrayBuffer.h" |
| 31 #include "core/dom/DOMException.h" |
| 32 #include "core/dom/ExceptionCode.h" |
30 #include "modules/webaudio/AudioBuffer.h" | 33 #include "modules/webaudio/AudioBuffer.h" |
31 #include "modules/webaudio/AudioBufferCallback.h" | 34 #include "modules/webaudio/AudioBufferCallback.h" |
| 35 #include "modules/webaudio/AudioContext.h" |
32 #include "platform/Task.h" | 36 #include "platform/Task.h" |
33 #include "platform/audio/AudioBus.h" | 37 #include "platform/audio/AudioBus.h" |
34 #include "platform/audio/AudioFileReader.h" | 38 #include "platform/audio/AudioFileReader.h" |
35 #include "public/platform/Platform.h" | 39 #include "public/platform/Platform.h" |
36 #include "public/platform/WebTraceLocation.h" | 40 #include "public/platform/WebTraceLocation.h" |
37 #include "wtf/MainThread.h" | 41 #include "wtf/MainThread.h" |
38 #include "wtf/PassOwnPtr.h" | 42 #include "wtf/PassOwnPtr.h" |
39 | 43 |
40 namespace blink { | 44 namespace blink { |
41 | 45 |
42 AsyncAudioDecoder::AsyncAudioDecoder() | 46 AsyncAudioDecoder::AsyncAudioDecoder() |
43 : m_thread(adoptPtr(Platform::current()->createThread("Audio Decoder"))) | 47 : m_thread(adoptPtr(Platform::current()->createThread("Audio Decoder"))) |
44 { | 48 { |
45 } | 49 } |
46 | 50 |
47 AsyncAudioDecoder::~AsyncAudioDecoder() | 51 AsyncAudioDecoder::~AsyncAudioDecoder() |
48 { | 52 { |
49 } | 53 } |
50 | 54 |
51 void AsyncAudioDecoder::decodeAsync(DOMArrayBuffer* audioData, float sampleRate,
AudioBufferCallback* successCallback, AudioBufferCallback* errorCallback) | 55 void AsyncAudioDecoder::decodeAsync(DOMArrayBuffer* audioData, float sampleRate,
AudioBufferCallback* successCallback, AudioBufferCallback* errorCallback, Scrip
tPromiseResolver* resolver, AudioContext* context) |
52 { | 56 { |
53 ASSERT(isMainThread()); | 57 ASSERT(isMainThread()); |
54 ASSERT(audioData); | 58 ASSERT(audioData); |
55 if (!audioData) | |
56 return; | |
57 | 59 |
58 // Add a ref to keep audioData alive until completion of decoding. | 60 // Add a ref to keep audioData alive until completion of decoding. |
59 RefPtr<DOMArrayBuffer> audioDataRef(audioData); | 61 RefPtr<DOMArrayBuffer> audioDataRef(audioData); |
60 | 62 |
61 // The leak references to successCallback and errorCallback are picked up on
notifyComplete. | 63 // The leak references to successCallback and errorCallback are picked up on
notifyComplete. |
62 m_thread->postTask(FROM_HERE, new Task(bind(&AsyncAudioDecoder::decode, audi
oDataRef.release().leakRef(), sampleRate, successCallback, errorCallback))); | 64 m_thread->postTask(FROM_HERE, new Task(bind(&AsyncAudioDecoder::decode, audi
oDataRef.release().leakRef(), sampleRate, successCallback, errorCallback, resolv
er, context))); |
63 } | 65 } |
64 | 66 |
65 void AsyncAudioDecoder::decode(DOMArrayBuffer* audioData, float sampleRate, Audi
oBufferCallback* successCallback, AudioBufferCallback* errorCallback) | 67 void AsyncAudioDecoder::decode(DOMArrayBuffer* audioData, float sampleRate, Audi
oBufferCallback* successCallback, AudioBufferCallback* errorCallback, ScriptProm
iseResolver* resolver, AudioContext* context) |
66 { | 68 { |
67 RefPtr<AudioBus> bus = createBusFromInMemoryAudioFile(audioData->data(), aud
ioData->byteLength(), false, sampleRate); | 69 RefPtr<AudioBus> bus = createBusFromInMemoryAudioFile(audioData->data(), aud
ioData->byteLength(), false, sampleRate); |
68 | 70 |
69 // Decoding is finished, but we need to do the callbacks on the main thread. | 71 // Decoding is finished, but we need to do the callbacks on the main thread. |
70 // The leaked reference to audioBuffer is picked up in notifyComplete. | 72 // The leaked reference to audioBuffer is picked up in notifyComplete. |
71 Platform::current()->mainThread()->postTask(FROM_HERE, bind(&AsyncAudioDecod
er::notifyComplete, audioData, successCallback, errorCallback, bus.release().lea
kRef())); | 73 Platform::current()->mainThread()->postTask(FROM_HERE, bind(&AsyncAudioDecod
er::notifyComplete, audioData, successCallback, errorCallback, bus.release().lea
kRef(), resolver, context)); |
72 } | 74 } |
73 | 75 |
74 void AsyncAudioDecoder::notifyComplete(DOMArrayBuffer* audioData, AudioBufferCal
lback* successCallback, AudioBufferCallback* errorCallback, AudioBus* audioBus) | 76 void AsyncAudioDecoder::notifyComplete(DOMArrayBuffer* audioData, AudioBufferCal
lback* successCallback, AudioBufferCallback* errorCallback, AudioBus* audioBus,
ScriptPromiseResolver* resolver, AudioContext* context) |
75 { | 77 { |
| 78 ASSERT(isMainThread()); |
| 79 |
76 // Adopt references, so everything gets correctly dereffed. | 80 // Adopt references, so everything gets correctly dereffed. |
77 RefPtr<DOMArrayBuffer> audioDataRef = adoptRef(audioData); | 81 RefPtr<DOMArrayBuffer> audioDataRef = adoptRef(audioData); |
78 RefPtr<AudioBus> audioBusRef = adoptRef(audioBus); | 82 RefPtr<AudioBus> audioBusRef = adoptRef(audioBus); |
79 | 83 |
80 AudioBuffer* audioBuffer = AudioBuffer::createFromAudioBus(audioBus); | 84 AudioBuffer* audioBuffer = AudioBuffer::createFromAudioBus(audioBus); |
81 if (audioBuffer && successCallback) | 85 if (audioBuffer) { |
82 successCallback->handleEvent(audioBuffer); | 86 resolver->resolve(audioBuffer); |
83 else if (errorCallback) | 87 if (successCallback) |
84 errorCallback->handleEvent(audioBuffer); | 88 successCallback->handleEvent(audioBuffer); |
| 89 } else { |
| 90 RefPtrWillBeRawPtr<DOMException> error = DOMException::create( |
| 91 EncodingError, |
| 92 "Unable to decode audio data"); |
| 93 resolver->reject(error); |
| 94 if (errorCallback) |
| 95 errorCallback->handleEvent(error.get()); |
| 96 } |
| 97 // Tell context to remove this resolver since it's been fulfilled. |
| 98 context->removeAudioDecoderResolver(resolver); |
85 } | 99 } |
86 | 100 |
87 } // namespace blink | 101 } // namespace blink |
88 | 102 |
89 #endif // ENABLE(WEB_AUDIO) | 103 #endif // ENABLE(WEB_AUDIO) |
OLD | NEW |