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

Side by Side Diff: media/base/audio_renderer_mixer_input.cc

Issue 1748183006: Add lock to fix race in AudioRendererMixerInput. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 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
« no previous file with comments | « media/base/audio_renderer_mixer_input.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "media/base/audio_renderer_mixer_input.h" 5 #include "media/base/audio_renderer_mixer_input.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback_helpers.h" 10 #include "base/callback_helpers.h"
(...skipping 20 matching lines...) Expand all
31 error_cb_(base::Bind(&AudioRendererMixerInput::OnRenderError, 31 error_cb_(base::Bind(&AudioRendererMixerInput::OnRenderError,
32 base::Unretained(this))) {} 32 base::Unretained(this))) {}
33 33
34 AudioRendererMixerInput::~AudioRendererMixerInput() { 34 AudioRendererMixerInput::~AudioRendererMixerInput() {
35 DCHECK(!mixer_); 35 DCHECK(!mixer_);
36 } 36 }
37 37
38 void AudioRendererMixerInput::Initialize( 38 void AudioRendererMixerInput::Initialize(
39 const AudioParameters& params, 39 const AudioParameters& params,
40 AudioRendererSink::RenderCallback* callback) { 40 AudioRendererSink::RenderCallback* callback) {
41 base::AutoLock auto_lock(lock_);
42
41 DCHECK(!mixer_); 43 DCHECK(!mixer_);
42 DCHECK(callback); 44 DCHECK(callback);
43 45
44 params_ = params; 46 params_ = params;
45 callback_ = callback; 47 callback_ = callback;
46 } 48 }
47 49
48 void AudioRendererMixerInput::Start() { 50 void AudioRendererMixerInput::Start() {
51 base::AutoLock auto_lock(lock_);
52
49 DCHECK(!started_); 53 DCHECK(!started_);
50 DCHECK(!mixer_); 54 DCHECK(!mixer_);
51 DCHECK(callback_); // Initialized. 55 DCHECK(callback_); // Initialized.
52 56
53 started_ = true; 57 started_ = true;
54 mixer_ = get_mixer_cb_.Run(params_, device_id_, security_origin_, nullptr); 58 mixer_ = get_mixer_cb_.Run(params_, device_id_, security_origin_, nullptr);
55 if (!mixer_) { 59 if (!mixer_) {
56 callback_->OnRenderError(); 60 callback_->OnRenderError();
57 return; 61 return;
58 } 62 }
59 63
60 // Note: OnRenderError() may be called immediately after this call returns. 64 // Note: OnRenderError() may be called immediately after this call returns.
61 mixer_->AddErrorCallback(error_cb_); 65 mixer_->AddErrorCallback(error_cb_);
62 66
63 if (!pending_switch_callback_.is_null()) { 67 if (!pending_switch_callback_.is_null()) {
64 SwitchOutputDevice(pending_switch_device_id_, 68 SwitchOutputDevice_Locked(pending_switch_device_id_,
65 pending_switch_security_origin_, 69 pending_switch_security_origin_,
66 base::ResetAndReturn(&pending_switch_callback_)); 70 base::ResetAndReturn(&pending_switch_callback_));
67 } 71 }
68 } 72 }
69 73
70 void AudioRendererMixerInput::Stop() { 74 void AudioRendererMixerInput::Stop() {
75 base::AutoLock auto_lock(lock_);
76 Stop_Locked();
77 }
78
79 void AudioRendererMixerInput::Stop_Locked() {
80 lock_.AssertAcquired();
81
71 // Stop() may be called at any time, if Pause() hasn't been called we need to 82 // Stop() may be called at any time, if Pause() hasn't been called we need to
72 // remove our mixer input before shutdown. 83 // remove our mixer input before shutdown.
73 Pause(); 84 Pause_Locked();
74 85
75 if (mixer_) { 86 if (mixer_) {
76 // TODO(dalecurtis): This is required so that |callback_| isn't called after 87 // TODO(dalecurtis): This is required so that |callback_| isn't called after
77 // Stop() by an error event since it may outlive this ref-counted object. We 88 // Stop() by an error event since it may outlive this ref-counted object. We
78 // should instead have sane ownership semantics: http://crbug.com/151051 89 // should instead have sane ownership semantics: http://crbug.com/151051
79 mixer_->RemoveErrorCallback(error_cb_); 90 mixer_->RemoveErrorCallback(error_cb_);
80 remove_mixer_cb_.Run(params_, device_id_, security_origin_); 91 remove_mixer_cb_.Run(params_, device_id_, security_origin_);
81 mixer_ = nullptr; 92 mixer_ = nullptr;
82 } 93 }
83 94
84 started_ = false; 95 started_ = false;
85 96
86 if (!pending_switch_callback_.is_null()) { 97 if (!pending_switch_callback_.is_null()) {
87 base::ResetAndReturn(&pending_switch_callback_) 98 base::ResetAndReturn(&pending_switch_callback_)
88 .Run(OUTPUT_DEVICE_STATUS_ERROR_INTERNAL); 99 .Run(OUTPUT_DEVICE_STATUS_ERROR_INTERNAL);
89 } 100 }
90 } 101 }
91 102
92 void AudioRendererMixerInput::Play() { 103 void AudioRendererMixerInput::Play() {
104 base::AutoLock auto_lock(lock_);
105 Play_Locked();
106 }
107
108 void AudioRendererMixerInput::Play_Locked() {
109 lock_.AssertAcquired();
110
93 if (playing_ || !mixer_) 111 if (playing_ || !mixer_)
94 return; 112 return;
95 113
96 mixer_->AddMixerInput(params_, this); 114 mixer_->AddMixerInput(params_, this);
97 playing_ = true; 115 playing_ = true;
98 } 116 }
99 117
100 void AudioRendererMixerInput::Pause() { 118 void AudioRendererMixerInput::Pause() {
119 base::AutoLock auto_lock(lock_);
120 Pause_Locked();
121 }
122
123 void AudioRendererMixerInput::Pause_Locked() {
124 lock_.AssertAcquired();
125
101 if (!playing_ || !mixer_) 126 if (!playing_ || !mixer_)
102 return; 127 return;
103 128
104 mixer_->RemoveMixerInput(params_, this); 129 mixer_->RemoveMixerInput(params_, this);
105 playing_ = false; 130 playing_ = false;
106 } 131 }
107 132
108 bool AudioRendererMixerInput::SetVolume(double volume) { 133 bool AudioRendererMixerInput::SetVolume(double volume) {
134 base::AutoLock auto_lock(lock_);
135
109 volume_ = volume; 136 volume_ = volume;
110 return true; 137 return true;
111 } 138 }
112 139
113 OutputDevice* AudioRendererMixerInput::GetOutputDevice() { 140 OutputDevice* AudioRendererMixerInput::GetOutputDevice() {
114 return this; 141 return this;
115 } 142 }
116 143
117 void AudioRendererMixerInput::SwitchOutputDevice( 144 void AudioRendererMixerInput::SwitchOutputDevice(
118 const std::string& device_id, 145 const std::string& device_id,
119 const url::Origin& security_origin, 146 const url::Origin& security_origin,
120 const SwitchOutputDeviceCB& callback) { 147 const SwitchOutputDeviceCB& callback) {
148 base::AutoLock auto_lock(lock_);
149 SwitchOutputDevice_Locked(device_id, security_origin, callback);
150 }
151
152 void AudioRendererMixerInput::SwitchOutputDevice_Locked(
153 const std::string& device_id,
154 const url::Origin& security_origin,
155 const SwitchOutputDeviceCB& callback) {
156 lock_.AssertAcquired();
157
121 if (!mixer_) { 158 if (!mixer_) {
122 if (pending_switch_callback_.is_null()) { 159 if (pending_switch_callback_.is_null()) {
123 pending_switch_callback_ = callback; 160 pending_switch_callback_ = callback;
124 pending_switch_device_id_ = device_id; 161 pending_switch_device_id_ = device_id;
125 pending_switch_security_origin_ = security_origin; 162 pending_switch_security_origin_ = security_origin;
126 } else { 163 } else {
127 callback.Run(OUTPUT_DEVICE_STATUS_ERROR_INTERNAL); 164 callback.Run(OUTPUT_DEVICE_STATUS_ERROR_INTERNAL);
128 } 165 }
129 166
130 return; 167 return;
131 } 168 }
132 169
133 DCHECK(pending_switch_callback_.is_null()); 170 DCHECK(pending_switch_callback_.is_null());
134 if (device_id == device_id_) { 171 if (device_id == device_id_) {
135 callback.Run(OUTPUT_DEVICE_STATUS_OK); 172 callback.Run(OUTPUT_DEVICE_STATUS_OK);
136 return; 173 return;
137 } 174 }
138 175
139 OutputDeviceStatus new_mixer_status = OUTPUT_DEVICE_STATUS_ERROR_INTERNAL; 176 OutputDeviceStatus new_mixer_status = OUTPUT_DEVICE_STATUS_ERROR_INTERNAL;
140 AudioRendererMixer* new_mixer = 177 AudioRendererMixer* new_mixer =
141 get_mixer_cb_.Run(params_, device_id, security_origin, &new_mixer_status); 178 get_mixer_cb_.Run(params_, device_id, security_origin, &new_mixer_status);
142 if (new_mixer_status != OUTPUT_DEVICE_STATUS_OK) { 179 if (new_mixer_status != OUTPUT_DEVICE_STATUS_OK) {
143 callback.Run(new_mixer_status); 180 callback.Run(new_mixer_status);
144 return; 181 return;
145 } 182 }
146 183
147 bool was_playing = playing_; 184 bool was_playing = playing_;
148 Stop(); 185 Stop_Locked();
149 device_id_ = device_id; 186 device_id_ = device_id;
150 security_origin_ = security_origin; 187 security_origin_ = security_origin;
151 mixer_ = new_mixer; 188 mixer_ = new_mixer;
152 mixer_->AddErrorCallback(error_cb_); 189 mixer_->AddErrorCallback(error_cb_);
153 started_ = true; 190 started_ = true;
154 191
155 if (was_playing) 192 if (was_playing)
156 Play(); 193 Play_Locked();
157 194
158 callback.Run(OUTPUT_DEVICE_STATUS_OK); 195 callback.Run(OUTPUT_DEVICE_STATUS_OK);
159 } 196 }
160 197
161 AudioParameters AudioRendererMixerInput::GetOutputParameters() { 198 AudioParameters AudioRendererMixerInput::GetOutputParameters() {
199 base::AutoLock auto_lock(lock_);
200
162 if (mixer_) 201 if (mixer_)
163 return mixer_->GetOutputDevice()->GetOutputParameters(); 202 return mixer_->GetOutputDevice()->GetOutputParameters();
164 return get_hardware_params_cb_.Run(device_id_, security_origin_); 203 return get_hardware_params_cb_.Run(device_id_, security_origin_);
165 } 204 }
166 205
167 OutputDeviceStatus AudioRendererMixerInput::GetDeviceStatus() { 206 OutputDeviceStatus AudioRendererMixerInput::GetDeviceStatus() {
207 base::AutoLock auto_lock(lock_);
208
168 if (mixer_) 209 if (mixer_)
169 return mixer_->GetOutputDevice()->GetDeviceStatus(); 210 return mixer_->GetOutputDevice()->GetDeviceStatus();
170 211
171 if (started_) 212 if (started_)
172 return OUTPUT_DEVICE_STATUS_ERROR_INTERNAL; 213 return OUTPUT_DEVICE_STATUS_ERROR_INTERNAL;
173 214
174 return OUTPUT_DEVICE_STATUS_OK; 215 return OUTPUT_DEVICE_STATUS_OK;
175 } 216 }
176 217
177 double AudioRendererMixerInput::ProvideInput(AudioBus* audio_bus, 218 double AudioRendererMixerInput::ProvideInput(AudioBus* audio_bus,
178 base::TimeDelta buffer_delay) { 219 base::TimeDelta buffer_delay) {
220 base::AutoLock auto_lock(lock_);
o1ka 2016/03/03 11:04:02 ProvideInput runs on real-time "render" thread, so
chcunningham 2016/03/03 18:40:20 Ack. I'll remove locks from Start/Stop/Switch and
221
179 // TODO(chcunningham): Delete this conversion and change ProvideInput to more 222 // TODO(chcunningham): Delete this conversion and change ProvideInput to more
180 // precisely describe delay as a count of frames delayed instead of TimeDelta. 223 // precisely describe delay as a count of frames delayed instead of TimeDelta.
181 // See http://crbug.com/587522. 224 // See http://crbug.com/587522.
182 uint32_t frames_delayed = std::round(buffer_delay.InMicroseconds() / 225 uint32_t frames_delayed = std::round(buffer_delay.InMicroseconds() /
183 params_.GetMicrosecondsPerFrame()); 226 params_.GetMicrosecondsPerFrame());
184 227
185 int frames_filled = callback_->Render(audio_bus, frames_delayed, 0); 228 int frames_filled = callback_->Render(audio_bus, frames_delayed, 0);
186 229
187 // AudioConverter expects unfilled frames to be zeroed. 230 // AudioConverter expects unfilled frames to be zeroed.
188 if (frames_filled < audio_bus->frames()) { 231 if (frames_filled < audio_bus->frames()) {
189 audio_bus->ZeroFramesPartial( 232 audio_bus->ZeroFramesPartial(
190 frames_filled, audio_bus->frames() - frames_filled); 233 frames_filled, audio_bus->frames() - frames_filled);
191 } 234 }
192 235
193 return frames_filled > 0 ? volume_ : 0; 236 return frames_filled > 0 ? volume_ : 0;
194 } 237 }
195 238
196 void AudioRendererMixerInput::OnRenderError() { 239 void AudioRendererMixerInput::OnRenderError() {
240 base::AutoLock auto_lock(lock_);
o1ka 2016/03/03 11:04:02 OnRenderError is a callback which can potentially
chcunningham 2016/03/03 18:40:20 Ack, will remove.
241
197 callback_->OnRenderError(); 242 callback_->OnRenderError();
198 } 243 }
199 244
200 } // namespace media 245 } // namespace media
OLDNEW
« no previous file with comments | « media/base/audio_renderer_mixer_input.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698