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

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

Issue 8980008: Integrate HTMLMediaElement with Web Audio API's MediaElementAudioSourceNode (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 8 years, 11 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
(Empty)
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/renderer/render_audiosourceprovider.h"
6
7 #include "base/basictypes.h"
8 #include "base/logging.h"
9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebAudioSourceProvide rClient.h"
10
11 using std::vector;
12 using WebKit::WebVector;
13
14 RenderAudioSourceProvider::RenderAudioSourceProvider()
15 : is_initialized_(false),
16 channels_(0),
17 sample_rate_(0.0),
18 is_running_(false),
19 volume_(1.0),
20 renderer_(NULL),
21 client_(NULL) {
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.)
24 // at this point. Later, when Initialize() is called, we have
25 // the audio format information and call the AudioDevice::Initialize()
26 // method to fully initialize it.
27 default_sink_ = new AudioDevice();
28 }
29
30 RenderAudioSourceProvider::~RenderAudioSourceProvider() {}
31
32 void RenderAudioSourceProvider::Start() {
33 base::AutoLock auto_lock(sink_lock_);
34 if (!client_)
35 default_sink_->Start();
36 is_running_ = true;
37 }
38
39 void RenderAudioSourceProvider::Stop() {
40 base::AutoLock auto_lock(sink_lock_);
41 if (!client_)
42 default_sink_->Stop();
43 is_running_ = false;
44 }
45
46 void RenderAudioSourceProvider::Play() {
47 base::AutoLock auto_lock(sink_lock_);
48 if (!client_)
49 default_sink_->Play();
50 is_running_ = true;
51 }
52
53 void RenderAudioSourceProvider::Pause(bool flush) {
54 base::AutoLock auto_lock(sink_lock_);
55 if (!client_)
56 default_sink_->Pause(flush);
57 is_running_ = false;
58 }
59
60 bool RenderAudioSourceProvider::SetVolume(double volume) {
61 base::AutoLock auto_lock(sink_lock_);
62 if (!client_)
63 default_sink_->SetVolume(volume);
64 volume_ = volume;
65 return true;
66 }
67
68 void RenderAudioSourceProvider::GetVolume(double* volume) {
69 if (!client_)
70 default_sink_->GetVolume(volume);
71 else if (volume)
72 *volume = volume_;
73 }
74
75 void RenderAudioSourceProvider::Initialize(
76 size_t buffer_size,
77 int channels,
78 double sample_rate,
79 AudioParameters::Format latency_format,
80 RenderCallback* renderer) {
81 base::AutoLock auto_lock(sink_lock_);
82 CHECK(!is_initialized_);
83 renderer_ = renderer;
84
85 default_sink_->Initialize(buffer_size,
86 channels,
87 sample_rate,
88 latency_format,
89 renderer);
90
91 if (client_) {
92 // Inform WebKit about the audio stream format.
93 client_->setFormat(channels, sample_rate);
94 }
95
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;
100 }
101
102 void RenderAudioSourceProvider::setClient(
103 WebKit::WebAudioSourceProviderClient* client) {
104 // Synchronize with other uses of client_ and default_sink_.
105 base::AutoLock auto_lock(sink_lock_);
106
107 if (client && client != client_) {
108 // Detach the audio renderer from normal playback.
109 default_sink_->Pause(true);
110
111 // The client will now take control by calling provideInput() periodically.
112 client_ = client;
113
114 if (is_initialized_) {
115 // 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
117 // when Initialize() is called.
118
119 // Inform WebKit about the audio stream format.
120 client->setFormat(channels_, sample_rate_);
121 }
122 } else if (!client && client_) {
123 // Restore normal playback.
124 client_ = NULL;
125 // TODO(crogers): We should call default_sink_->Play() if we're
126 // in the playing state.
127 }
128 }
129
130 void RenderAudioSourceProvider::provideInput(
131 const WebVector<float*>& audio_data, size_t number_of_frames) {
132 DCHECK(client_);
133
134 if (renderer_ && is_initialized_ && is_running_) {
135 // Wrap WebVector as std::vector.
136 vector<float*> v(audio_data.size());
137 for (size_t i = 0; i < audio_data.size(); ++i)
138 v[i] = audio_data[i];
139
140 // 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.
142 renderer_->Render(v, number_of_frames, 0);
143 } else {
144 // Provide silence if the source is not running.
145 for (size_t i = 0; i < audio_data.size(); ++i)
146 memset(audio_data[i], 0, sizeof(float) * number_of_frames);
147 }
148 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698