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

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: Moving lock down in ProvideInput 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 11 matching lines...) Expand all
22 playing_(false), 22 playing_(false),
23 volume_(1.0f), 23 volume_(1.0f),
24 get_mixer_cb_(get_mixer_cb), 24 get_mixer_cb_(get_mixer_cb),
25 remove_mixer_cb_(remove_mixer_cb), 25 remove_mixer_cb_(remove_mixer_cb),
26 get_hardware_params_cb_(get_hardware_params_cb), 26 get_hardware_params_cb_(get_hardware_params_cb),
27 device_id_(device_id), 27 device_id_(device_id),
28 security_origin_(security_origin), 28 security_origin_(security_origin),
29 mixer_(nullptr), 29 mixer_(nullptr),
30 callback_(nullptr), 30 callback_(nullptr),
31 error_cb_(base::Bind(&AudioRendererMixerInput::OnRenderError, 31 error_cb_(base::Bind(&AudioRendererMixerInput::OnRenderError,
32 base::Unretained(this))) {} 32 base::Unretained(this))) {
33 // Can be constructed on any thread, but sink operations should all occur
34 // on same thread.
35 thread_checker_.DetachFromThread();
36 }
33 37
34 AudioRendererMixerInput::~AudioRendererMixerInput() { 38 AudioRendererMixerInput::~AudioRendererMixerInput() {
39 DCHECK(!started_);
35 DCHECK(!mixer_); 40 DCHECK(!mixer_);
36 } 41 }
37 42
38 void AudioRendererMixerInput::Initialize( 43 void AudioRendererMixerInput::Initialize(
39 const AudioParameters& params, 44 const AudioParameters& params,
40 AudioRendererSink::RenderCallback* callback) { 45 AudioRendererSink::RenderCallback* callback) {
46 DCHECK(thread_checker_.CalledOnValidThread());
47 DCHECK(!started_);
41 DCHECK(!mixer_); 48 DCHECK(!mixer_);
42 DCHECK(callback); 49 DCHECK(callback);
43 50
44 params_ = params; 51 params_ = params;
45 callback_ = callback; 52 callback_ = callback;
46 } 53 }
47 54
48 void AudioRendererMixerInput::Start() { 55 void AudioRendererMixerInput::Start() {
56 DCHECK(thread_checker_.CalledOnValidThread());
49 DCHECK(!started_); 57 DCHECK(!started_);
50 DCHECK(!mixer_); 58 DCHECK(!mixer_);
51 DCHECK(callback_); // Initialized. 59 DCHECK(callback_); // Initialized.
52 60
53 started_ = true; 61 started_ = true;
54 mixer_ = get_mixer_cb_.Run(params_, device_id_, security_origin_, nullptr); 62 mixer_ = get_mixer_cb_.Run(params_, device_id_, security_origin_, nullptr);
55 if (!mixer_) { 63 if (!mixer_) {
56 callback_->OnRenderError(); 64 callback_->OnRenderError();
57 return; 65 return;
58 } 66 }
59 67
60 // Note: OnRenderError() may be called immediately after this call returns. 68 // Note: OnRenderError() may be called immediately after this call returns.
61 mixer_->AddErrorCallback(error_cb_); 69 mixer_->AddErrorCallback(error_cb_);
62 70
63 if (!pending_switch_callback_.is_null()) { 71 if (!pending_switch_callback_.is_null()) {
64 SwitchOutputDevice(pending_switch_device_id_, 72 SwitchOutputDevice(pending_switch_device_id_,
65 pending_switch_security_origin_, 73 pending_switch_security_origin_,
66 base::ResetAndReturn(&pending_switch_callback_)); 74 base::ResetAndReturn(&pending_switch_callback_));
67 } 75 }
68 } 76 }
69 77
70 void AudioRendererMixerInput::Stop() { 78 void AudioRendererMixerInput::Stop() {
79 DCHECK(thread_checker_.CalledOnValidThread());
80
71 // Stop() may be called at any time, if Pause() hasn't been called we need to 81 // Stop() may be called at any time, if Pause() hasn't been called we need to
72 // remove our mixer input before shutdown. 82 // remove our mixer input before shutdown.
73 Pause(); 83 Pause();
74 84
75 if (mixer_) { 85 if (mixer_) {
76 // TODO(dalecurtis): This is required so that |callback_| isn't called after 86 // 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 87 // 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 88 // should instead have sane ownership semantics: http://crbug.com/151051
79 mixer_->RemoveErrorCallback(error_cb_); 89 mixer_->RemoveErrorCallback(error_cb_);
80 remove_mixer_cb_.Run(params_, device_id_, security_origin_); 90 remove_mixer_cb_.Run(params_, device_id_, security_origin_);
81 mixer_ = nullptr; 91 mixer_ = nullptr;
82 } 92 }
83 93
84 started_ = false; 94 started_ = false;
85 95
86 if (!pending_switch_callback_.is_null()) { 96 if (!pending_switch_callback_.is_null()) {
87 base::ResetAndReturn(&pending_switch_callback_) 97 base::ResetAndReturn(&pending_switch_callback_)
88 .Run(OUTPUT_DEVICE_STATUS_ERROR_INTERNAL); 98 .Run(OUTPUT_DEVICE_STATUS_ERROR_INTERNAL);
89 } 99 }
90 } 100 }
91 101
92 void AudioRendererMixerInput::Play() { 102 void AudioRendererMixerInput::Play() {
103 DCHECK(thread_checker_.CalledOnValidThread());
104
93 if (playing_ || !mixer_) 105 if (playing_ || !mixer_)
94 return; 106 return;
95 107
96 mixer_->AddMixerInput(params_, this); 108 mixer_->AddMixerInput(params_, this);
97 playing_ = true; 109 playing_ = true;
98 } 110 }
99 111
100 void AudioRendererMixerInput::Pause() { 112 void AudioRendererMixerInput::Pause() {
113 DCHECK(thread_checker_.CalledOnValidThread());
114
101 if (!playing_ || !mixer_) 115 if (!playing_ || !mixer_)
102 return; 116 return;
103 117
104 mixer_->RemoveMixerInput(params_, this); 118 mixer_->RemoveMixerInput(params_, this);
105 playing_ = false; 119 playing_ = false;
106 } 120 }
107 121
108 bool AudioRendererMixerInput::SetVolume(double volume) { 122 bool AudioRendererMixerInput::SetVolume(double volume) {
123 DCHECK(thread_checker_.CalledOnValidThread());
124 base::AutoLock auto_lock(volume_lock_);
109 volume_ = volume; 125 volume_ = volume;
110 return true; 126 return true;
111 } 127 }
112 128
113 OutputDevice* AudioRendererMixerInput::GetOutputDevice() { 129 OutputDevice* AudioRendererMixerInput::GetOutputDevice() {
114 return this; 130 return this;
115 } 131 }
116 132
117 void AudioRendererMixerInput::SwitchOutputDevice( 133 void AudioRendererMixerInput::SwitchOutputDevice(
118 const std::string& device_id, 134 const std::string& device_id,
119 const url::Origin& security_origin, 135 const url::Origin& security_origin,
120 const SwitchOutputDeviceCB& callback) { 136 const SwitchOutputDeviceCB& callback) {
137 DCHECK(thread_checker_.CalledOnValidThread());
138
121 if (!mixer_) { 139 if (!mixer_) {
122 if (pending_switch_callback_.is_null()) { 140 if (pending_switch_callback_.is_null()) {
123 pending_switch_callback_ = callback; 141 pending_switch_callback_ = callback;
124 pending_switch_device_id_ = device_id; 142 pending_switch_device_id_ = device_id;
125 pending_switch_security_origin_ = security_origin; 143 pending_switch_security_origin_ = security_origin;
126 } else { 144 } else {
127 callback.Run(OUTPUT_DEVICE_STATUS_ERROR_INTERNAL); 145 callback.Run(OUTPUT_DEVICE_STATUS_ERROR_INTERNAL);
128 } 146 }
129 147
130 return; 148 return;
(...skipping 21 matching lines...) Expand all
152 mixer_->AddErrorCallback(error_cb_); 170 mixer_->AddErrorCallback(error_cb_);
153 started_ = true; 171 started_ = true;
154 172
155 if (was_playing) 173 if (was_playing)
156 Play(); 174 Play();
157 175
158 callback.Run(OUTPUT_DEVICE_STATUS_OK); 176 callback.Run(OUTPUT_DEVICE_STATUS_OK);
159 } 177 }
160 178
161 AudioParameters AudioRendererMixerInput::GetOutputParameters() { 179 AudioParameters AudioRendererMixerInput::GetOutputParameters() {
180 DCHECK(thread_checker_.CalledOnValidThread());
181
162 if (mixer_) 182 if (mixer_)
163 return mixer_->GetOutputDevice()->GetOutputParameters(); 183 return mixer_->GetOutputDevice()->GetOutputParameters();
164 return get_hardware_params_cb_.Run(device_id_, security_origin_); 184 return get_hardware_params_cb_.Run(device_id_, security_origin_);
165 } 185 }
166 186
167 OutputDeviceStatus AudioRendererMixerInput::GetDeviceStatus() { 187 OutputDeviceStatus AudioRendererMixerInput::GetDeviceStatus() {
188 DCHECK(thread_checker_.CalledOnValidThread());
189
168 if (mixer_) 190 if (mixer_)
169 return mixer_->GetOutputDevice()->GetDeviceStatus(); 191 return mixer_->GetOutputDevice()->GetDeviceStatus();
170 192
171 if (started_) 193 if (started_)
172 return OUTPUT_DEVICE_STATUS_ERROR_INTERNAL; 194 return OUTPUT_DEVICE_STATUS_ERROR_INTERNAL;
173 195
174 return OUTPUT_DEVICE_STATUS_OK; 196 return OUTPUT_DEVICE_STATUS_OK;
175 } 197 }
176 198
177 double AudioRendererMixerInput::ProvideInput(AudioBus* audio_bus, 199 double AudioRendererMixerInput::ProvideInput(AudioBus* audio_bus,
178 base::TimeDelta buffer_delay) { 200 base::TimeDelta buffer_delay) {
201 // No thread checker here. This method is called on a different thread as part
202 // of audio rendering. AudioRendererMixer has locks that protect us from
203 // things like attempting to ProvideInput while simultaneously removing
204 // ourselves from mixer inputs (see Pause()).
205
179 // TODO(chcunningham): Delete this conversion and change ProvideInput to more 206 // TODO(chcunningham): Delete this conversion and change ProvideInput to more
180 // precisely describe delay as a count of frames delayed instead of TimeDelta. 207 // precisely describe delay as a count of frames delayed instead of TimeDelta.
181 // See http://crbug.com/587522. 208 // See http://crbug.com/587522.
182 uint32_t frames_delayed = std::round(buffer_delay.InMicroseconds() / 209 uint32_t frames_delayed = std::round(buffer_delay.InMicroseconds() /
183 params_.GetMicrosecondsPerFrame()); 210 params_.GetMicrosecondsPerFrame());
184 211
185 int frames_filled = callback_->Render(audio_bus, frames_delayed, 0); 212 int frames_filled = callback_->Render(audio_bus, frames_delayed, 0);
186 213
187 // AudioConverter expects unfilled frames to be zeroed. 214 // AudioConverter expects unfilled frames to be zeroed.
188 if (frames_filled < audio_bus->frames()) { 215 if (frames_filled < audio_bus->frames()) {
189 audio_bus->ZeroFramesPartial( 216 audio_bus->ZeroFramesPartial(
190 frames_filled, audio_bus->frames() - frames_filled); 217 frames_filled, audio_bus->frames() - frames_filled);
191 } 218 }
192 219
193 return frames_filled > 0 ? volume_ : 0; 220 // Synchronize access to |volume_| with SetVolume().
221 {
DaleCurtis 2016/03/03 23:08:01 {} are unnecessary since you're right above a retu
chcunningham1 2016/03/03 23:18:53 thought maybe you'd ask for it as a best practice.
DaleCurtis 2016/03/03 23:26:41 I don't feel strongly. We typically only use {} ar
222 base::AutoLock auto_lock(volume_lock_);
223 return frames_filled > 0 ? volume_ : 0;
224 }
194 } 225 }
195 226
196 void AudioRendererMixerInput::OnRenderError() { 227 void AudioRendererMixerInput::OnRenderError() {
197 callback_->OnRenderError(); 228 callback_->OnRenderError();
198 } 229 }
199 230
200 } // namespace media 231 } // 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