Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(187)

Side by Side Diff: third_party/WebKit/Source/modules/webaudio/AsyncAudioDecoder.cpp

Issue 2268403006: Use background thread for AsyncAudioDecoder (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sanity check for context Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/modules/webaudio/AsyncAudioDecoder.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 12 matching lines...) Expand all
23 */ 23 */
24 24
25 #include "core/dom/DOMArrayBuffer.h" 25 #include "core/dom/DOMArrayBuffer.h"
26 #include "modules/webaudio/AsyncAudioDecoder.h" 26 #include "modules/webaudio/AsyncAudioDecoder.h"
27 #include "modules/webaudio/AudioBuffer.h" 27 #include "modules/webaudio/AudioBuffer.h"
28 #include "modules/webaudio/AudioBufferCallback.h" 28 #include "modules/webaudio/AudioBufferCallback.h"
29 #include "modules/webaudio/BaseAudioContext.h" 29 #include "modules/webaudio/BaseAudioContext.h"
30 #include "platform/CrossThreadFunctional.h" 30 #include "platform/CrossThreadFunctional.h"
31 #include "platform/audio/AudioBus.h" 31 #include "platform/audio/AudioBus.h"
32 #include "platform/audio/AudioFileReader.h" 32 #include "platform/audio/AudioFileReader.h"
33 #include "platform/threading/BackgroundTaskRunner.h"
33 #include "public/platform/Platform.h" 34 #include "public/platform/Platform.h"
34 #include "public/platform/WebTraceLocation.h" 35 #include "public/platform/WebTraceLocation.h"
35 #include "wtf/PtrUtil.h" 36 #include "wtf/PtrUtil.h"
36 37
37 namespace blink { 38 namespace blink {
38 39
39 AsyncAudioDecoder::AsyncAudioDecoder() 40 // This threshold is determined by the assumption that the target audio
40 : m_thread(wrapUnique(Platform::current()->createThread("Audio Decoder"))) 41 // file is compressed in AAC or MP3. The decoding time varies upon different
41 { 42 // audio file encoding types, but it is fair to assume the usage of compressed
42 } 43 // file.
43 44 unsigned TaskSizeThresholdInByte = 512000;
esprehn 2016/08/30 21:40:16 static and prefix with "k"
Raymond Toy 2016/09/01 16:51:10 And const.
44 AsyncAudioDecoder::~AsyncAudioDecoder()
45 {
46 }
47 45
48 void AsyncAudioDecoder::decodeAsync(DOMArrayBuffer* audioData, float sampleRate, AudioBufferCallback* successCallback, AudioBufferCallback* errorCallback, Scrip tPromiseResolver* resolver, BaseAudioContext* context) 46 void AsyncAudioDecoder::decodeAsync(DOMArrayBuffer* audioData, float sampleRate, AudioBufferCallback* successCallback, AudioBufferCallback* errorCallback, Scrip tPromiseResolver* resolver, BaseAudioContext* context)
49 { 47 {
50 DCHECK(isMainThread()); 48 DCHECK(isMainThread());
51 DCHECK(audioData); 49 DCHECK(audioData);
52 if (!audioData) 50 if (!audioData)
53 return; 51 return;
54 52
55 m_thread->getWebTaskRunner()->postTask(BLINK_FROM_HERE, crossThreadBind(&Asy ncAudioDecoder::decode, wrapCrossThreadPersistent(audioData), sampleRate, wrapCr ossThreadPersistent(successCallback), wrapCrossThreadPersistent(errorCallback), wrapCrossThreadPersistent(resolver), wrapCrossThreadPersistent(context))); 53 BackgroundTaskRunner::TaskSize taskSize = audioData->byteLength() < TaskSize ThresholdInByte
54 ? BackgroundTaskRunner::TaskSizeShortRunningTask
55 : BackgroundTaskRunner::TaskSizeLongRunningTask;
56 BackgroundTaskRunner::postOnBackgroundThread(BLINK_FROM_HERE, crossThreadBin d(&AsyncAudioDecoder::decodeOnBackgroundThread, wrapCrossThreadPersistent(audioD ata), sampleRate, wrapCrossThreadPersistent(successCallback), wrapCrossThreadPer sistent(errorCallback), wrapCrossThreadPersistent(resolver), wrapCrossThreadPers istent(context)), taskSize);
56 } 57 }
57 58
58 void AsyncAudioDecoder::decode(DOMArrayBuffer* audioData, float sampleRate, Audi oBufferCallback* successCallback, AudioBufferCallback* errorCallback, ScriptProm iseResolver* resolver, BaseAudioContext* context) 59 void AsyncAudioDecoder::decodeOnBackgroundThread(DOMArrayBuffer* audioData, floa t sampleRate, AudioBufferCallback* successCallback, AudioBufferCallback* errorCa llback, ScriptPromiseResolver* resolver, BaseAudioContext* context)
59 { 60 {
61 DCHECK(!isMainThread());
60 RefPtr<AudioBus> bus = createBusFromInMemoryAudioFile(audioData->data(), aud ioData->byteLength(), false, sampleRate); 62 RefPtr<AudioBus> bus = createBusFromInMemoryAudioFile(audioData->data(), aud ioData->byteLength(), false, sampleRate);
61 63
62 // Decoding is finished, but we need to do the callbacks on the main thread. 64 // Decoding is finished, but we need to do the callbacks on the main thread.
63 // A reference to |*bus| is retained by WTF::Function and will be removed af ter notifyComplete() is done. 65 // A reference to |*bus| is retained by WTF::Function and will be removed
64 Platform::current()->mainThread()->getWebTaskRunner()->postTask(BLINK_FROM_H ERE, crossThreadBind(&AsyncAudioDecoder::notifyComplete, wrapCrossThreadPersiste nt(audioData), wrapCrossThreadPersistent(successCallback), wrapCrossThreadPersis tent(errorCallback), bus.release(), wrapCrossThreadPersistent(resolver), wrapCro ssThreadPersistent(context))); 66 // after notifyComplete() is done.
67 //
68 // We also want to avoid notifying the main thread if AudioContext does not
69 // exist any more.
70 if (context) {
71 Platform::current()->mainThread()->getWebTaskRunner()->postTask(BLINK_FR OM_HERE, crossThreadBind(&AsyncAudioDecoder::notifyComplete, wrapCrossThreadPers istent(audioData), wrapCrossThreadPersistent(successCallback), wrapCrossThreadPe rsistent(errorCallback), bus.release(), wrapCrossThreadPersistent(resolver), wra pCrossThreadPersistent(context)));
72 }
65 } 73 }
66 74
67 void AsyncAudioDecoder::notifyComplete(DOMArrayBuffer*, AudioBufferCallback* suc cessCallback, AudioBufferCallback* errorCallback, AudioBus* audioBus, ScriptProm iseResolver* resolver, BaseAudioContext* context) 75 void AsyncAudioDecoder::notifyComplete(DOMArrayBuffer*, AudioBufferCallback* suc cessCallback, AudioBufferCallback* errorCallback, AudioBus* audioBus, ScriptProm iseResolver* resolver, BaseAudioContext* context)
68 { 76 {
69 DCHECK(isMainThread()); 77 DCHECK(isMainThread());
70 78
71 AudioBuffer* audioBuffer = AudioBuffer::createFromAudioBus(audioBus); 79 AudioBuffer* audioBuffer = AudioBuffer::createFromAudioBus(audioBus);
72 80
73 // Let the context finish the notification. 81 // If the context is available, let the context finish the notification.
74 context->handleDecodeAudioData(audioBuffer, resolver, successCallback, error Callback); 82 if (context) {
83 context->handleDecodeAudioData(audioBuffer, resolver, successCallback, e rrorCallback);
84 }
75 } 85 }
76 86
77 } // namespace blink 87 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/modules/webaudio/AsyncAudioDecoder.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698