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

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

Issue 2501863003: Support for AudioContextOptions latencyHint. (Closed)
Patch Set: Created 4 years, 1 month 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),
Raymond Toy 2016/11/15 18:16:17 Where is this initialized now? It's really import
Andrew MacPherson 2016/11/16 10:58:30 Based on an earlier suggestion from o1ka@ I change
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);
hongchan 2016/11/15 23:46:01 So I guess now we have to query the sample rate vi
Andrew MacPherson 2016/11/16 10:58:29 Yes, this was the intention but I will let o1ka@ c
87 87
88 #if OS(ANDROID) 88 m_callbackBufferSize = m_audioDevice->framesPerBuffer();
hongchan 2016/11/15 23:46:01 o1ka@ If we do this, is this going to be an IPC c
o1ka 2016/11/17 17:37:25 No extra IPC, the value is cached in audio device.
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(m_callbackBufferSize);
o1ka 2016/11/15 22:44:28 I'm not sure if these two histograms have the righ
hongchan 2016/11/15 23:46:01 I think o1ka is right. This seems wrong and we mig
Raymond Toy 2016/11/15 23:50:38 Yeah, but I think we really want this somewhere. I
Andrew MacPherson 2016/11/16 10:58:30 Let me know if you'd like me to move this elsewher
118 callbackBufferSizeHistogram.sample(m_callbackBufferSize); 97 callbackBufferSizeHistogram.sample(m_callbackBufferSize);
119 98
120 // Create a FIFO to handle the possibility of the callback size 99 // Create a FIFO to handle the possibility of the callback size
121 // not being a multiple of the render size. If the FIFO already 100 // not being a multiple of the render size. If the FIFO already
122 // contains enough data, the data will be provided directly. 101 // contains enough data, the data will be provided directly.
123 // Otherwise, the FIFO will call the provider enough times to 102 // Otherwise, the FIFO will call the provider enough times to
124 // satisfy the request for data. 103 // satisfy the request for data.
125 m_fifo = wrapUnique(new AudioPullFIFO(*this, numberOfOutputChannels, fifoSize, 104 m_fifo = wrapUnique(new AudioPullFIFO(*this, numberOfOutputChannels, fifoSize,
126 AudioUtilities::kRenderQuantumFrames)); 105 AudioUtilities::kRenderQuantumFrames));
127 106
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 AudioBus* sourceBus = nullptr; 178 AudioBus* sourceBus = nullptr;
200 if (m_inputFifo->framesInFifo() >= framesToProcess) { 179 if (m_inputFifo->framesInFifo() >= framesToProcess) {
201 m_inputFifo->consume(m_inputBus.get(), framesToProcess); 180 m_inputFifo->consume(m_inputBus.get(), framesToProcess);
202 sourceBus = m_inputBus.get(); 181 sourceBus = m_inputBus.get();
203 } 182 }
204 183
205 m_callback.render(sourceBus, bus, framesToProcess); 184 m_callback.render(sourceBus, bus, framesToProcess);
206 } 185 }
207 186
208 } // namespace blink 187 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698