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 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 { | 47 { |
48 } | 48 } |
49 | 49 |
50 void AsyncAudioDecoder::decodeAsync(ArrayBuffer* audioData, float sampleRate, Pa
ssRefPtr<AudioBufferCallback> successCallback, PassRefPtr<AudioBufferCallback> e
rrorCallback) | 50 void AsyncAudioDecoder::decodeAsync(ArrayBuffer* audioData, float sampleRate, Pa
ssRefPtr<AudioBufferCallback> successCallback, PassRefPtr<AudioBufferCallback> e
rrorCallback) |
51 { | 51 { |
52 ASSERT(isMainThread()); | 52 ASSERT(isMainThread()); |
53 ASSERT(audioData); | 53 ASSERT(audioData); |
54 if (!audioData) | 54 if (!audioData) |
55 return; | 55 return; |
56 | 56 |
| 57 // Add a ref to keep audioData alive until completion of decoding. |
| 58 RefPtr<ArrayBuffer> audioDataRef(audioData); |
| 59 |
57 // The leak references to successCallback and errorCallback are picked up on
notifyComplete. | 60 // The leak references to successCallback and errorCallback are picked up on
notifyComplete. |
58 m_thread->postTask(new Task(WTF::bind(&AsyncAudioDecoder::decode, audioData,
sampleRate, successCallback.leakRef(), errorCallback.leakRef()))); | 61 m_thread->postTask(new Task(WTF::bind(&AsyncAudioDecoder::decode, audioDataR
ef.release().leakRef(), sampleRate, successCallback.leakRef(), errorCallback.lea
kRef()))); |
59 } | 62 } |
60 | 63 |
61 void AsyncAudioDecoder::decode(ArrayBuffer* audioData, float sampleRate, AudioBu
fferCallback* successCallback, AudioBufferCallback* errorCallback) | 64 void AsyncAudioDecoder::decode(ArrayBuffer* audioData, float sampleRate, AudioBu
fferCallback* successCallback, AudioBufferCallback* errorCallback) |
62 { | 65 { |
63 // Do the actual decoding and invoke the callback. | 66 // Do the actual decoding and invoke the callback. |
64 RefPtr<AudioBuffer> audioBuffer = AudioBuffer::createFromAudioFileData(audio
Data->data(), audioData->byteLength(), false, sampleRate); | 67 RefPtr<AudioBuffer> audioBuffer = AudioBuffer::createFromAudioFileData(audio
Data->data(), audioData->byteLength(), false, sampleRate); |
65 | 68 |
66 // 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. |
67 // The leaked reference to audioBuffer is picked up in notifyComplete. | 70 // The leaked reference to audioBuffer is picked up in notifyComplete. |
68 callOnMainThread(WTF::bind(&AsyncAudioDecoder::notifyComplete, audioData, su
ccessCallback, errorCallback, audioBuffer.release().leakRef())); | 71 callOnMainThread(WTF::bind(&AsyncAudioDecoder::notifyComplete, audioData, su
ccessCallback, errorCallback, audioBuffer.release().leakRef())); |
69 } | 72 } |
70 | 73 |
71 void AsyncAudioDecoder::notifyComplete(ArrayBuffer* audioData, AudioBufferCallba
ck* successCallback, AudioBufferCallback* errorCallback, AudioBuffer* audioBuffe
r) | 74 void AsyncAudioDecoder::notifyComplete(ArrayBuffer* audioData, AudioBufferCallba
ck* successCallback, AudioBufferCallback* errorCallback, AudioBuffer* audioBuffe
r) |
72 { | 75 { |
73 // Adopt references, so everything gets correctly dereffed. | 76 // Adopt references, so everything gets correctly dereffed. |
74 RefPtr<ArrayBuffer> audioDataRef = adoptRef(audioData); | 77 RefPtr<ArrayBuffer> audioDataRef = adoptRef(audioData); |
75 RefPtr<AudioBufferCallback> successCallbackRef = adoptRef(successCallback); | 78 RefPtr<AudioBufferCallback> successCallbackRef = adoptRef(successCallback); |
76 RefPtr<AudioBufferCallback> errorCallbackRef = adoptRef(errorCallback); | 79 RefPtr<AudioBufferCallback> errorCallbackRef = adoptRef(errorCallback); |
77 RefPtr<AudioBuffer> audioBufferRef = adoptRef(audioBuffer); | 80 RefPtr<AudioBuffer> audioBufferRef = adoptRef(audioBuffer); |
78 | 81 |
79 if (audioBuffer && successCallback) | 82 if (audioBuffer && successCallback) |
80 successCallback->handleEvent(audioBuffer); | 83 successCallback->handleEvent(audioBuffer); |
81 else if (errorCallback) | 84 else if (errorCallback) |
82 errorCallback->handleEvent(audioBuffer); | 85 errorCallback->handleEvent(audioBuffer); |
83 } | 86 } |
84 | 87 |
85 } // namespace WebCore | 88 } // namespace WebCore |
86 | 89 |
87 #endif // ENABLE(WEB_AUDIO) | 90 #endif // ENABLE(WEB_AUDIO) |
OLD | NEW |