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 "media/base/audio_renderer_mixer_input.h" | 5 #include "media/base/audio_renderer_mixer_input.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback_helpers.h" | 8 #include "base/callback_helpers.h" |
9 #include "media/base/audio_renderer_mixer.h" | 9 #include "media/base/audio_renderer_mixer.h" |
10 | 10 |
11 namespace media { | 11 namespace media { |
12 | 12 |
13 AudioRendererMixerInput::AudioRendererMixerInput( | 13 AudioRendererMixerInput::AudioRendererMixerInput( |
14 const GetMixerCB& get_mixer_cb, | 14 const GetMixerCB& get_mixer_cb, |
15 const RemoveMixerCB& remove_mixer_cb, | 15 const RemoveMixerCB& remove_mixer_cb, |
| 16 const GetHardwareParamsCB& get_hardware_params_cb, |
16 const std::string& device_id, | 17 const std::string& device_id, |
17 const url::Origin& security_origin) | 18 const url::Origin& security_origin) |
18 : initialized_(false), | 19 : started_(false), |
19 playing_(false), | 20 playing_(false), |
20 volume_(1.0f), | 21 volume_(1.0f), |
21 get_mixer_cb_(get_mixer_cb), | 22 get_mixer_cb_(get_mixer_cb), |
22 remove_mixer_cb_(remove_mixer_cb), | 23 remove_mixer_cb_(remove_mixer_cb), |
| 24 get_hardware_params_cb_(get_hardware_params_cb), |
23 device_id_(device_id), | 25 device_id_(device_id), |
24 security_origin_(security_origin), | 26 security_origin_(security_origin), |
25 mixer_(NULL), | 27 mixer_(nullptr), |
26 callback_(NULL), | 28 callback_(nullptr), |
27 error_cb_(base::Bind(&AudioRendererMixerInput::OnRenderError, | 29 error_cb_(base::Bind(&AudioRendererMixerInput::OnRenderError, |
28 base::Unretained(this))) {} | 30 base::Unretained(this))) {} |
29 | 31 |
30 AudioRendererMixerInput::~AudioRendererMixerInput() { | 32 AudioRendererMixerInput::~AudioRendererMixerInput() { |
31 DCHECK(!mixer_); | 33 DCHECK(!mixer_); |
32 } | 34 } |
33 | 35 |
34 void AudioRendererMixerInput::Initialize( | 36 void AudioRendererMixerInput::Initialize( |
35 const AudioParameters& params, | 37 const AudioParameters& params, |
36 AudioRendererSink::RenderCallback* callback) { | 38 AudioRendererSink::RenderCallback* callback) { |
37 DCHECK(!mixer_); | 39 DCHECK(!mixer_); |
38 DCHECK(callback); | 40 DCHECK(callback); |
39 | 41 |
40 params_ = params; | 42 params_ = params; |
41 callback_ = callback; | 43 callback_ = callback; |
42 initialized_ = true; | |
43 } | 44 } |
44 | 45 |
45 void AudioRendererMixerInput::Start() { | 46 void AudioRendererMixerInput::Start() { |
46 DCHECK(initialized_); | 47 DCHECK(!started_); |
47 DCHECK(!mixer_); | 48 DCHECK(!mixer_); |
| 49 DCHECK(callback_); // Initialized. |
| 50 |
| 51 started_ = true; |
48 mixer_ = get_mixer_cb_.Run(params_, device_id_, security_origin_, nullptr); | 52 mixer_ = get_mixer_cb_.Run(params_, device_id_, security_origin_, nullptr); |
49 if (!mixer_) { | 53 if (!mixer_) { |
50 callback_->OnRenderError(); | 54 callback_->OnRenderError(); |
51 return; | 55 return; |
52 } | 56 } |
53 | 57 |
54 // Note: OnRenderError() may be called immediately after this call returns. | 58 // Note: OnRenderError() may be called immediately after this call returns. |
55 mixer_->AddErrorCallback(error_cb_); | 59 mixer_->AddErrorCallback(error_cb_); |
56 | 60 |
57 if (!pending_switch_callback_.is_null()) { | 61 if (!pending_switch_callback_.is_null()) { |
58 SwitchOutputDevice(pending_switch_device_id_, | 62 SwitchOutputDevice(pending_switch_device_id_, |
59 pending_switch_security_origin_, | 63 pending_switch_security_origin_, |
60 base::ResetAndReturn(&pending_switch_callback_)); | 64 base::ResetAndReturn(&pending_switch_callback_)); |
61 } | 65 } |
62 } | 66 } |
63 | 67 |
64 void AudioRendererMixerInput::Stop() { | 68 void AudioRendererMixerInput::Stop() { |
65 // Stop() may be called at any time, if Pause() hasn't been called we need to | 69 // Stop() may be called at any time, if Pause() hasn't been called we need to |
66 // remove our mixer input before shutdown. | 70 // remove our mixer input before shutdown. |
67 if (playing_) { | 71 Pause(); |
68 mixer_->RemoveMixerInput(params_, this); | |
69 playing_ = false; | |
70 } | |
71 | 72 |
72 if (mixer_) { | 73 if (mixer_) { |
73 // TODO(dalecurtis): This is required so that |callback_| isn't called after | 74 // TODO(dalecurtis): This is required so that |callback_| isn't called after |
74 // Stop() by an error event since it may outlive this ref-counted object. We | 75 // Stop() by an error event since it may outlive this ref-counted object. We |
75 // should instead have sane ownership semantics: http://crbug.com/151051 | 76 // should instead have sane ownership semantics: http://crbug.com/151051 |
76 mixer_->RemoveErrorCallback(error_cb_); | 77 mixer_->RemoveErrorCallback(error_cb_); |
77 remove_mixer_cb_.Run(params_, device_id_, security_origin_); | 78 remove_mixer_cb_.Run(params_, device_id_, security_origin_); |
78 mixer_ = NULL; | 79 mixer_ = nullptr; |
79 } | 80 } |
80 | 81 |
| 82 started_ = false; |
| 83 |
81 if (!pending_switch_callback_.is_null()) { | 84 if (!pending_switch_callback_.is_null()) { |
82 base::ResetAndReturn(&pending_switch_callback_) | 85 base::ResetAndReturn(&pending_switch_callback_) |
83 .Run(OUTPUT_DEVICE_STATUS_ERROR_INTERNAL); | 86 .Run(OUTPUT_DEVICE_STATUS_ERROR_INTERNAL); |
84 } | 87 } |
85 } | 88 } |
86 | 89 |
87 void AudioRendererMixerInput::Play() { | 90 void AudioRendererMixerInput::Play() { |
88 if (playing_ || !mixer_) | 91 if (playing_ || !mixer_) |
89 return; | 92 return; |
90 | 93 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 callback.Run(new_mixer_status); | 141 callback.Run(new_mixer_status); |
139 return; | 142 return; |
140 } | 143 } |
141 | 144 |
142 bool was_playing = playing_; | 145 bool was_playing = playing_; |
143 Stop(); | 146 Stop(); |
144 device_id_ = device_id; | 147 device_id_ = device_id; |
145 security_origin_ = security_origin; | 148 security_origin_ = security_origin; |
146 mixer_ = new_mixer; | 149 mixer_ = new_mixer; |
147 mixer_->AddErrorCallback(error_cb_); | 150 mixer_->AddErrorCallback(error_cb_); |
| 151 started_ = true; |
148 | 152 |
149 if (was_playing) | 153 if (was_playing) |
150 Play(); | 154 Play(); |
151 | 155 |
152 callback.Run(OUTPUT_DEVICE_STATUS_OK); | 156 callback.Run(OUTPUT_DEVICE_STATUS_OK); |
153 } | 157 } |
154 | 158 |
155 AudioParameters AudioRendererMixerInput::GetOutputParameters() { | 159 AudioParameters AudioRendererMixerInput::GetOutputParameters() { |
156 return mixer_->GetOutputDevice()->GetOutputParameters(); | 160 if (mixer_) |
| 161 return mixer_->GetOutputDevice()->GetOutputParameters(); |
| 162 return get_hardware_params_cb_.Run(device_id_, security_origin_); |
157 } | 163 } |
158 | 164 |
159 OutputDeviceStatus AudioRendererMixerInput::GetDeviceStatus() { | 165 OutputDeviceStatus AudioRendererMixerInput::GetDeviceStatus() { |
160 if (!mixer_) | 166 if (mixer_) |
| 167 return mixer_->GetOutputDevice()->GetDeviceStatus(); |
| 168 |
| 169 if (started_) |
161 return OUTPUT_DEVICE_STATUS_ERROR_INTERNAL; | 170 return OUTPUT_DEVICE_STATUS_ERROR_INTERNAL; |
162 | 171 |
163 return mixer_->GetOutputDevice()->GetDeviceStatus(); | 172 return OUTPUT_DEVICE_STATUS_OK; |
164 } | 173 } |
165 | 174 |
166 double AudioRendererMixerInput::ProvideInput(AudioBus* audio_bus, | 175 double AudioRendererMixerInput::ProvideInput(AudioBus* audio_bus, |
167 base::TimeDelta buffer_delay) { | 176 base::TimeDelta buffer_delay) { |
168 int frames_filled = callback_->Render( | 177 int frames_filled = callback_->Render( |
169 audio_bus, static_cast<int>(buffer_delay.InMillisecondsF() + 0.5), 0); | 178 audio_bus, static_cast<int>(buffer_delay.InMillisecondsF() + 0.5), 0); |
170 | 179 |
171 // AudioConverter expects unfilled frames to be zeroed. | 180 // AudioConverter expects unfilled frames to be zeroed. |
172 if (frames_filled < audio_bus->frames()) { | 181 if (frames_filled < audio_bus->frames()) { |
173 audio_bus->ZeroFramesPartial( | 182 audio_bus->ZeroFramesPartial( |
174 frames_filled, audio_bus->frames() - frames_filled); | 183 frames_filled, audio_bus->frames() - frames_filled); |
175 } | 184 } |
176 | 185 |
177 return frames_filled > 0 ? volume_ : 0; | 186 return frames_filled > 0 ? volume_ : 0; |
178 } | 187 } |
179 | 188 |
180 void AudioRendererMixerInput::OnRenderError() { | 189 void AudioRendererMixerInput::OnRenderError() { |
181 callback_->OnRenderError(); | 190 callback_->OnRenderError(); |
182 } | 191 } |
183 | 192 |
184 } // namespace media | 193 } // namespace media |
OLD | NEW |