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

Side by Side Diff: third_party/WebKit/Source/platform/audio/AudioDestination.cpp

Issue 2501863003: Support for AudioContextOptions latencyHint. (Closed)
Patch Set: Adding additional tests. Created 4 years 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
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010 Google Inc. All rights reserved. 2 * Copyright (C) 2010 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 * 7 *
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 16 matching lines...) Expand all
27 */ 27 */
28 28
29 #include "platform/audio/AudioDestination.h" 29 #include "platform/audio/AudioDestination.h"
30 30
31 #include "platform/Histogram.h" 31 #include "platform/Histogram.h"
32 #include "platform/audio/AudioFIFO.h" 32 #include "platform/audio/AudioFIFO.h"
33 #include "platform/audio/AudioPullFIFO.h" 33 #include "platform/audio/AudioPullFIFO.h"
34 #include "platform/audio/AudioUtilities.h" 34 #include "platform/audio/AudioUtilities.h"
35 #include "platform/weborigin/SecurityOrigin.h" 35 #include "platform/weborigin/SecurityOrigin.h"
36 #include "public/platform/Platform.h" 36 #include "public/platform/Platform.h"
37 #include "public/platform/WebAudioLatencyHint.h"
37 #include "public/platform/WebSecurityOrigin.h" 38 #include "public/platform/WebSecurityOrigin.h"
38 #include "wtf/PtrUtil.h" 39 #include "wtf/PtrUtil.h"
39 #include <memory> 40 #include <memory>
40 41
41 namespace blink { 42 namespace blink {
42 43
43 // Size of the FIFO 44 // Size of the FIFO
44 const size_t fifoSize = 8192; 45 const size_t fifoSize = 8192;
45 46
46 // Factory method: Chromium-implementation 47 // Factory method: Chromium-implementation
47 std::unique_ptr<AudioDestination> AudioDestination::create( 48 std::unique_ptr<AudioDestination> AudioDestination::create(
48 AudioIOCallback& callback, 49 AudioIOCallback& callback,
49 const String& inputDeviceId, 50 const String& inputDeviceId,
50 unsigned numberOfInputChannels, 51 unsigned numberOfInputChannels,
51 unsigned numberOfOutputChannels, 52 unsigned numberOfOutputChannels,
52 float sampleRate, 53 const WebAudioLatencyHint& latencyHint,
53 PassRefPtr<SecurityOrigin> securityOrigin) { 54 PassRefPtr<SecurityOrigin> securityOrigin) {
54 return wrapUnique(new AudioDestination( 55 return wrapUnique(new AudioDestination(
55 callback, inputDeviceId, numberOfInputChannels, numberOfOutputChannels, 56 callback, inputDeviceId, numberOfInputChannels, numberOfOutputChannels,
56 sampleRate, std::move(securityOrigin))); 57 latencyHint, std::move(securityOrigin)));
57 } 58 }
58 59
59 AudioDestination::AudioDestination(AudioIOCallback& callback, 60 AudioDestination::AudioDestination(AudioIOCallback& callback,
60 const String& inputDeviceId, 61 const String& inputDeviceId,
61 unsigned numberOfInputChannels, 62 unsigned numberOfInputChannels,
62 unsigned numberOfOutputChannels, 63 unsigned numberOfOutputChannels,
63 float sampleRate, 64 const WebAudioLatencyHint& latencyHint,
64 PassRefPtr<SecurityOrigin> securityOrigin) 65 PassRefPtr<SecurityOrigin> securityOrigin)
65 : m_callback(callback), 66 : m_callback(callback),
66 m_numberOfOutputChannels(numberOfOutputChannels), 67 m_numberOfOutputChannels(numberOfOutputChannels),
67 m_inputBus(AudioBus::create(numberOfInputChannels, 68 m_inputBus(AudioBus::create(numberOfInputChannels,
68 AudioUtilities::kRenderQuantumFrames)), 69 AudioUtilities::kRenderQuantumFrames)),
69 m_renderBus(AudioBus::create(numberOfOutputChannels, 70 m_renderBus(AudioBus::create(numberOfOutputChannels,
70 AudioUtilities::kRenderQuantumFrames, 71 AudioUtilities::kRenderQuantumFrames,
71 false)), 72 false)),
72 m_sampleRate(sampleRate),
73 m_isPlaying(false) { 73 m_isPlaying(false) {
74 // Histogram for audioHardwareBufferSize 74 // Histogram for audioHardwareBufferSize
75 DEFINE_STATIC_LOCAL(SparseHistogram, hardwareBufferSizeHistogram, 75 DEFINE_STATIC_LOCAL(SparseHistogram, hardwareBufferSizeHistogram,
76 ("WebAudio.AudioDestination.HardwareBufferSize")); 76 ("WebAudio.AudioDestination.HardwareBufferSize"));
77 // Histogram for the actual callback size used. Typically, this is the same 77 // Histogram for the actual callback size used. Typically, this is the same
78 // as audioHardwareBufferSize, but can be adjusted depending on some 78 // as audioHardwareBufferSize, but can be adjusted depending on some
79 // heuristics below. 79 // heuristics below.
80 DEFINE_STATIC_LOCAL(SparseHistogram, callbackBufferSizeHistogram, 80 DEFINE_STATIC_LOCAL(SparseHistogram, callbackBufferSizeHistogram,
81 ("WebAudio.AudioDestination.CallbackBufferSize")); 81 ("WebAudio.AudioDestination.CallbackBufferSize"));
82 82
83 // Use the optimal buffer size recommended by the audio backend. 83 m_audioDevice = wrapUnique(Platform::current()->createAudioDevice(
84 size_t recommendedHardwareBufferSize = 84 numberOfInputChannels, numberOfOutputChannels, latencyHint, this,
85 Platform::current()->audioHardwareBufferSize(); 85 inputDeviceId, std::move(securityOrigin)));
86 m_callbackBufferSize = recommendedHardwareBufferSize; 86 DCHECK(m_audioDevice);
87 87
88 #if OS(ANDROID) 88 m_callbackBufferSize = m_audioDevice->framesPerBuffer();
89 // The optimum low-latency hardware buffer size is usually too small on
90 // Android for WebAudio to render without glitching. So, if it is small, use
91 // a larger size. If it was already large, use the requested size.
92 //
93 // Since WebAudio renders in 128-frame blocks, the small buffer sizes (144
94 // for a Galaxy Nexus), cause significant processing jitter. Sometimes
95 // multiple blocks will processed, but other times will not be since the FIFO
96 // can satisfy the request. By using a larger callbackBufferSize, we smooth
97 // out the jitter.
98 const size_t kSmallBufferSize = 1024;
99 const size_t kDefaultCallbackBufferSize = 2048;
100
101 if (m_callbackBufferSize <= kSmallBufferSize)
102 m_callbackBufferSize = kDefaultCallbackBufferSize;
103 #endif
104
105 // Quick exit if the requested size is too large. 89 // Quick exit if the requested size is too large.
106 DCHECK_LE(m_callbackBufferSize + AudioUtilities::kRenderQuantumFrames, 90 DCHECK_LE(m_callbackBufferSize + AudioUtilities::kRenderQuantumFrames,
107 fifoSize); 91 fifoSize);
108 if (m_callbackBufferSize + AudioUtilities::kRenderQuantumFrames > fifoSize) 92 if (m_callbackBufferSize + AudioUtilities::kRenderQuantumFrames > fifoSize)
109 return; 93 return;
110 94
111 m_audioDevice = wrapUnique(Platform::current()->createAudioDevice(
112 m_callbackBufferSize, numberOfInputChannels, numberOfOutputChannels,
113 sampleRate, this, inputDeviceId, std::move(securityOrigin)));
114 ASSERT(m_audioDevice);
115
116 // Record the sizes if we successfully created an output device. 95 // Record the sizes if we successfully created an output device.
117 hardwareBufferSizeHistogram.sample(recommendedHardwareBufferSize); 96 hardwareBufferSizeHistogram.sample(
97 Platform::current()->audioHardwareBufferSize());
118 callbackBufferSizeHistogram.sample(m_callbackBufferSize); 98 callbackBufferSizeHistogram.sample(m_callbackBufferSize);
o1ka 2016/12/07 20:56:01 rtoy@ - callback buffer size will be different for
Raymond Toy 2016/12/08 16:24:50 Good point. It probably would be a good idea to ad
119 99
120 // Create a FIFO to handle the possibility of the callback size 100 // Create a FIFO to handle the possibility of the callback size
121 // not being a multiple of the render size. If the FIFO already 101 // not being a multiple of the render size. If the FIFO already
122 // contains enough data, the data will be provided directly. 102 // contains enough data, the data will be provided directly.
123 // Otherwise, the FIFO will call the provider enough times to 103 // Otherwise, the FIFO will call the provider enough times to
124 // satisfy the request for data. 104 // satisfy the request for data.
125 m_fifo = wrapUnique(new AudioPullFIFO(*this, numberOfOutputChannels, fifoSize, 105 m_fifo = wrapUnique(new AudioPullFIFO(*this, numberOfOutputChannels, fifoSize,
126 AudioUtilities::kRenderQuantumFrames)); 106 AudioUtilities::kRenderQuantumFrames));
127 107
128 // Input buffering. 108 // Input buffering.
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 AudioBus* sourceBus = nullptr; 179 AudioBus* sourceBus = nullptr;
200 if (m_inputFifo->framesInFifo() >= framesToProcess) { 180 if (m_inputFifo->framesInFifo() >= framesToProcess) {
201 m_inputFifo->consume(m_inputBus.get(), framesToProcess); 181 m_inputFifo->consume(m_inputBus.get(), framesToProcess);
202 sourceBus = m_inputBus.get(); 182 sourceBus = m_inputBus.get();
203 } 183 }
204 184
205 m_callback.render(sourceBus, bus, framesToProcess); 185 m_callback.render(sourceBus, bus, framesToProcess);
206 } 186 }
207 187
208 } // namespace blink 188 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698