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

Side by Side Diff: content/renderer/media/render_audiosourceprovider.cc

Issue 9655018: Make AudioParameters a class instead of a struct (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix tests Created 8 years, 9 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/renderer/media/render_audiosourceprovider.h" 5 #include "content/renderer/media/render_audiosourceprovider.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebAudioSourceProvide rClient.h" 9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebAudioSourceProvide rClient.h"
10 10
11 using std::vector; 11 using std::vector;
12 using WebKit::WebVector; 12 using WebKit::WebVector;
13 13
14 RenderAudioSourceProvider::RenderAudioSourceProvider() 14 RenderAudioSourceProvider::RenderAudioSourceProvider()
15 : is_initialized_(false), 15 : is_initialized_(false),
16 channels_(0), 16 channels_(0),
17 sample_rate_(0.0), 17 samples_per_second_(0.0),
18 is_running_(false), 18 is_running_(false),
19 volume_(1.0), 19 volume_(1.0),
20 renderer_(NULL), 20 renderer_(NULL),
21 client_(NULL) { 21 client_(NULL) {
22 // We create the AudioDevice here because it must be created in the 22 // We create the AudioDevice here because it must be created in the
23 // main thread. But we don't yet know the audio format (sample-rate, etc.) 23 // main thread. But we don't yet know the audio format (sample-rate, etc.)
24 // at this point. Later, when Initialize() is called, we have 24 // at this point. Later, when Initialize() is called, we have
25 // the audio format information and call the AudioDevice::Initialize() 25 // the audio format information and call the AudioDevice::Initialize()
26 // method to fully initialize it. 26 // method to fully initialize it.
27 default_sink_ = new AudioDevice(); 27 default_sink_ = new AudioDevice();
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 } 66 }
67 67
68 void RenderAudioSourceProvider::GetVolume(double* volume) { 68 void RenderAudioSourceProvider::GetVolume(double* volume) {
69 if (!client_) 69 if (!client_)
70 default_sink_->GetVolume(volume); 70 default_sink_->GetVolume(volume);
71 else if (volume) 71 else if (volume)
72 *volume = volume_; 72 *volume = volume_;
73 } 73 }
74 74
75 void RenderAudioSourceProvider::Initialize( 75 void RenderAudioSourceProvider::Initialize(
76 size_t buffer_size, 76 const AudioParameters& params, RenderCallback* renderer) {
77 int channels,
78 double sample_rate,
79 AudioParameters::Format latency_format,
80 RenderCallback* renderer) {
81 base::AutoLock auto_lock(sink_lock_); 77 base::AutoLock auto_lock(sink_lock_);
82 CHECK(!is_initialized_); 78 CHECK(!is_initialized_);
83 renderer_ = renderer; 79 renderer_ = renderer;
84 80
85 default_sink_->Initialize(buffer_size, 81 default_sink_->Initialize(params, renderer);
86 channels, 82
87 sample_rate, 83 // Keep track of the format in case the client hasn't yet been set.
88 latency_format, 84 channels_ = params.channels();
89 renderer); 85 samples_per_second_ = params.samples_per_second();
90 86
91 if (client_) { 87 if (client_) {
92 // Inform WebKit about the audio stream format. 88 // Inform WebKit about the audio stream format.
93 client_->setFormat(channels, sample_rate); 89 client_->setFormat(channels_, samples_per_second_);
94 } 90 }
95 91
96 // Keep track of the format in case the client hasn't yet been set.
97 channels_ = channels;
98 sample_rate_ = sample_rate;
99 is_initialized_ = true; 92 is_initialized_ = true;
100 } 93 }
101 94
102 void RenderAudioSourceProvider::setClient( 95 void RenderAudioSourceProvider::setClient(
103 WebKit::WebAudioSourceProviderClient* client) { 96 WebKit::WebAudioSourceProviderClient* client) {
104 // Synchronize with other uses of client_ and default_sink_. 97 // Synchronize with other uses of client_ and default_sink_.
105 base::AutoLock auto_lock(sink_lock_); 98 base::AutoLock auto_lock(sink_lock_);
106 99
107 if (client && client != client_) { 100 if (client && client != client_) {
108 // Detach the audio renderer from normal playback. 101 // Detach the audio renderer from normal playback.
109 default_sink_->Pause(true); 102 default_sink_->Pause(true);
110 103
111 // The client will now take control by calling provideInput() periodically. 104 // The client will now take control by calling provideInput() periodically.
112 client_ = client; 105 client_ = client;
113 106
114 if (is_initialized_) { 107 if (is_initialized_) {
115 // The client needs to be notified of the audio format, if available. 108 // The client needs to be notified of the audio format, if available.
116 // If the format is not yet available, we'll be notified later 109 // If the format is not yet available, we'll be notified later
117 // when Initialize() is called. 110 // when Initialize() is called.
118 111
119 // Inform WebKit about the audio stream format. 112 // Inform WebKit about the audio stream format.
120 client->setFormat(channels_, sample_rate_); 113 client->setFormat(channels_, samples_per_second_);
121 } 114 }
122 } else if (!client && client_) { 115 } else if (!client && client_) {
123 // Restore normal playback. 116 // Restore normal playback.
124 client_ = NULL; 117 client_ = NULL;
125 // TODO(crogers): We should call default_sink_->Play() if we're 118 // TODO(crogers): We should call default_sink_->Play() if we're
126 // in the playing state. 119 // in the playing state.
127 } 120 }
128 } 121 }
129 122
130 void RenderAudioSourceProvider::provideInput( 123 void RenderAudioSourceProvider::provideInput(
131 const WebVector<float*>& audio_data, size_t number_of_frames) { 124 const WebVector<float*>& audio_data, size_t number_of_frames) {
132 DCHECK(client_); 125 DCHECK(client_);
133 126
134 if (renderer_ && is_initialized_ && is_running_) { 127 if (renderer_ && is_initialized_ && is_running_) {
135 // Wrap WebVector as std::vector. 128 // Wrap WebVector as std::vector.
136 vector<float*> v(audio_data.size()); 129 vector<float*> v(audio_data.size());
137 for (size_t i = 0; i < audio_data.size(); ++i) 130 for (size_t i = 0; i < audio_data.size(); ++i)
138 v[i] = audio_data[i]; 131 v[i] = audio_data[i];
139 132
140 // TODO(crogers): figure out if we should volume scale here or in common 133 // TODO(crogers): figure out if we should volume scale here or in common
141 // WebAudio code. In any case we need to take care of volume. 134 // WebAudio code. In any case we need to take care of volume.
142 renderer_->Render(v, number_of_frames, 0); 135 renderer_->Render(v, number_of_frames, 0);
143 } else { 136 } else {
144 // Provide silence if the source is not running. 137 // Provide silence if the source is not running.
145 for (size_t i = 0; i < audio_data.size(); ++i) 138 for (size_t i = 0; i < audio_data.size(); ++i)
146 memset(audio_data[i], 0, sizeof(float) * number_of_frames); 139 memset(audio_data[i], 0, sizeof(float) * number_of_frames);
147 } 140 }
148 } 141 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698