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