Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 audio_device_ = new AudioDevice(); | |
| 28 } | |
| 29 | |
| 30 RenderAudioSourceProvider::~RenderAudioSourceProvider() {} | |
| 31 | |
| 32 void RenderAudioSourceProvider::SetRenderer( | |
| 33 media::AudioRendererSink::RenderCallback* renderer) { | |
| 34 DCHECK(renderer); | |
| 35 renderer_ = renderer; | |
|
acolwell GONE FROM CHROMIUM
2011/12/21 22:53:56
Why is this method needed? Can you use what is pas
Chris Rogers
2011/12/22 00:54:33
Done.
| |
| 36 } | |
| 37 | |
| 38 void RenderAudioSourceProvider::Start() { | |
| 39 base::AutoLock auto_lock(sink_lock_); | |
| 40 if (!client_) | |
| 41 audio_device_->Start(); | |
| 42 is_running_ = true; | |
| 43 } | |
| 44 | |
| 45 void RenderAudioSourceProvider::Stop() { | |
| 46 base::AutoLock auto_lock(sink_lock_); | |
| 47 if (!client_) | |
| 48 audio_device_->Stop(); | |
| 49 is_running_ = false; | |
| 50 } | |
| 51 | |
| 52 void RenderAudioSourceProvider::Play() { | |
| 53 base::AutoLock auto_lock(sink_lock_); | |
| 54 if (!client_) | |
| 55 audio_device_->Play(); | |
| 56 is_running_ = true; | |
| 57 } | |
| 58 | |
| 59 void RenderAudioSourceProvider::Pause(bool flush) { | |
| 60 base::AutoLock auto_lock(sink_lock_); | |
| 61 if (!client_) | |
| 62 audio_device_->Pause(flush); | |
| 63 is_running_ = false; | |
| 64 } | |
| 65 | |
| 66 bool RenderAudioSourceProvider::SetVolume(double volume) { | |
| 67 base::AutoLock auto_lock(sink_lock_); | |
| 68 if (!client_) | |
| 69 audio_device_->SetVolume(volume); | |
| 70 volume_ = volume; | |
| 71 return true; | |
| 72 } | |
| 73 | |
| 74 void RenderAudioSourceProvider::GetVolume(double* volume) { | |
| 75 if (!client_) | |
| 76 audio_device_->GetVolume(volume); | |
| 77 else if (volume) | |
| 78 *volume = volume_; | |
| 79 } | |
| 80 | |
| 81 void RenderAudioSourceProvider::Initialize( | |
| 82 size_t buffer_size, | |
| 83 int channels, | |
| 84 double sample_rate, | |
| 85 AudioParameters::Format latency_format, | |
| 86 RenderCallback* callback) { | |
| 87 base::AutoLock auto_lock(sink_lock_); | |
| 88 | |
| 89 if (!is_initialized_) { | |
|
acolwell GONE FROM CHROMIUM
2011/12/21 22:53:56
nit: reverse condition & return early so you don't
Chris Rogers
2011/12/22 00:54:33
Done.
| |
| 90 audio_device_->Initialize(buffer_size, | |
| 91 channels, | |
| 92 sample_rate, | |
| 93 latency_format, | |
| 94 callback); | |
| 95 | |
| 96 if (client_) { | |
| 97 // Inform WebKit about the audio stream format. | |
| 98 client_->setFormat(channels, sample_rate); | |
| 99 } | |
| 100 | |
| 101 // Keep track of the format in case the client hasn't yet been set. | |
| 102 channels_ = channels; | |
| 103 sample_rate_ = sample_rate; | |
| 104 is_initialized_ = true; | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 void RenderAudioSourceProvider::setClient( | |
| 109 WebKit::WebAudioSourceProviderClient* client) { | |
| 110 // Synchronize with other uses of client_ and audio_device_. | |
| 111 base::AutoLock auto_lock(sink_lock_); | |
| 112 | |
| 113 if (client && client != client_) { | |
| 114 // Detach the audio renderer from normal playback. | |
| 115 audio_device_->Pause(true); | |
| 116 | |
| 117 // The client will now take control by calling provideInput() periodically. | |
| 118 client_ = client; | |
| 119 | |
| 120 if (is_initialized_) { | |
| 121 // The client needs to be notified of the audio format, if available. | |
| 122 // If the format is not yet available, we'll be notified later | |
| 123 // when Initialize() is called. | |
| 124 | |
| 125 // Inform WebKit about the audio stream format. | |
| 126 client->setFormat(channels_, sample_rate_); | |
| 127 } | |
| 128 } else if (!client && client_) { | |
| 129 // Restore normal playback. | |
| 130 client_ = NULL; | |
| 131 // TODO(crogers): We should call audio_device_->Play() if we're | |
| 132 // in the playing state. | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 void RenderAudioSourceProvider::provideInput( | |
| 137 const WebVector<float*>& audio_data, size_t number_of_frames) { | |
| 138 DCHECK(renderer_); | |
| 139 DCHECK(client_); | |
| 140 | |
| 141 if (renderer_ && is_running_) { | |
| 142 // Wrap WebVector as std::vector. | |
| 143 vector<float*> v(audio_data.size()); | |
| 144 for (size_t i = 0; i < audio_data.size(); ++i) | |
| 145 v[i] = audio_data[i]; | |
| 146 | |
| 147 // TODO(crogers): figure out if we should volume scale here or in common | |
| 148 // WebAudio code. In any case we need to take care of volume. | |
| 149 renderer_->Render(v, number_of_frames, 0); | |
| 150 } else { | |
| 151 // Provide silence if the source is not running. | |
| 152 for (size_t i = 0; i < audio_data.size(); ++i) | |
| 153 memset(audio_data[i], 0, sizeof(float) * number_of_frames); | |
| 154 } | |
| 155 } | |
| OLD | NEW |