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