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 <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" |
11 #include "media/base/audio_renderer_mixer.h" | 11 #include "media/base/audio_renderer_mixer.h" |
12 #include "media/base/audio_renderer_mixer_pool.h" | 12 #include "media/base/audio_renderer_mixer_pool.h" |
13 | 13 |
14 namespace media { | 14 namespace media { |
15 | 15 |
16 AudioRendererMixerInput::AudioRendererMixerInput( | 16 AudioRendererMixerInput::AudioRendererMixerInput( |
17 AudioRendererMixerPool* mixer_pool, | 17 AudioRendererMixerPool* mixer_pool, |
18 int owner_id, | 18 int owner_id, |
19 const std::string& device_id, | 19 const std::string& device_id, |
20 const url::Origin& security_origin) | 20 const url::Origin& security_origin, |
| 21 AudioLatency::LatencyType latency) |
21 : mixer_pool_(mixer_pool), | 22 : mixer_pool_(mixer_pool), |
22 started_(false), | 23 started_(false), |
23 playing_(false), | 24 playing_(false), |
24 volume_(1.0f), | 25 volume_(1.0f), |
25 owner_id_(owner_id), | 26 owner_id_(owner_id), |
26 device_id_(device_id), | 27 device_id_(device_id), |
27 security_origin_(security_origin), | 28 security_origin_(security_origin), |
| 29 latency_(latency), |
28 mixer_(nullptr), | 30 mixer_(nullptr), |
29 callback_(nullptr), | 31 callback_(nullptr), |
30 error_cb_(base::Bind(&AudioRendererMixerInput::OnRenderError, | 32 error_cb_(base::Bind(&AudioRendererMixerInput::OnRenderError, |
31 base::Unretained(this))) { | 33 base::Unretained(this))) { |
32 DCHECK(mixer_pool_); | 34 DCHECK(mixer_pool_); |
33 } | 35 } |
34 | 36 |
35 AudioRendererMixerInput::~AudioRendererMixerInput() { | 37 AudioRendererMixerInput::~AudioRendererMixerInput() { |
36 DCHECK(!started_); | 38 DCHECK(!started_); |
37 DCHECK(!mixer_); | 39 DCHECK(!mixer_); |
38 } | 40 } |
39 | 41 |
40 void AudioRendererMixerInput::Initialize( | 42 void AudioRendererMixerInput::Initialize( |
41 const AudioParameters& params, | 43 const AudioParameters& params, |
42 AudioRendererSink::RenderCallback* callback) { | 44 AudioRendererSink::RenderCallback* callback) { |
43 DCHECK(!started_); | 45 DCHECK(!started_); |
44 DCHECK(!mixer_); | 46 DCHECK(!mixer_); |
45 DCHECK(callback); | 47 DCHECK(callback); |
46 | 48 |
47 params_ = params; | 49 params_ = params; |
48 callback_ = callback; | 50 callback_ = callback; |
49 } | 51 } |
50 | 52 |
51 void AudioRendererMixerInput::Start() { | 53 void AudioRendererMixerInput::Start() { |
52 DCHECK(!started_); | 54 DCHECK(!started_); |
53 DCHECK(!mixer_); | 55 DCHECK(!mixer_); |
54 DCHECK(callback_); // Initialized. | 56 DCHECK(callback_); // Initialized. |
55 | 57 |
56 started_ = true; | 58 started_ = true; |
57 mixer_ = mixer_pool_->GetMixer(owner_id_, params_, device_id_, | 59 mixer_ = mixer_pool_->GetMixer(owner_id_, params_, latency_, device_id_, |
58 security_origin_, nullptr); | 60 security_origin_, nullptr); |
59 if (!mixer_) { | 61 if (!mixer_) { |
60 callback_->OnRenderError(); | 62 callback_->OnRenderError(); |
61 return; | 63 return; |
62 } | 64 } |
63 | 65 |
64 // Note: OnRenderError() may be called immediately after this call returns. | 66 // Note: OnRenderError() may be called immediately after this call returns. |
65 mixer_->AddErrorCallback(error_cb_); | 67 mixer_->AddErrorCallback(error_cb_); |
66 | 68 |
67 if (!pending_switch_callback_.is_null()) { | 69 if (!pending_switch_callback_.is_null()) { |
68 SwitchOutputDevice(pending_switch_device_id_, | 70 SwitchOutputDevice(pending_switch_device_id_, |
69 pending_switch_security_origin_, | 71 pending_switch_security_origin_, |
70 base::ResetAndReturn(&pending_switch_callback_)); | 72 base::ResetAndReturn(&pending_switch_callback_)); |
71 } | 73 } |
72 } | 74 } |
73 | 75 |
74 void AudioRendererMixerInput::Stop() { | 76 void AudioRendererMixerInput::Stop() { |
75 // Stop() may be called at any time, if Pause() hasn't been called we need to | 77 // Stop() may be called at any time, if Pause() hasn't been called we need to |
76 // remove our mixer input before shutdown. | 78 // remove our mixer input before shutdown. |
77 Pause(); | 79 Pause(); |
78 | 80 |
79 if (mixer_) { | 81 if (mixer_) { |
80 // TODO(dalecurtis): This is required so that |callback_| isn't called after | 82 // TODO(dalecurtis): This is required so that |callback_| isn't called after |
81 // Stop() by an error event since it may outlive this ref-counted object. We | 83 // Stop() by an error event since it may outlive this ref-counted object. We |
82 // should instead have sane ownership semantics: http://crbug.com/151051 | 84 // should instead have sane ownership semantics: http://crbug.com/151051 |
83 mixer_->RemoveErrorCallback(error_cb_); | 85 mixer_->RemoveErrorCallback(error_cb_); |
84 mixer_pool_->ReturnMixer(owner_id_, params_, device_id_, security_origin_); | 86 mixer_pool_->ReturnMixer(mixer_); |
85 mixer_ = nullptr; | 87 mixer_ = nullptr; |
86 } | 88 } |
87 | 89 |
88 started_ = false; | 90 started_ = false; |
89 | 91 |
90 if (!pending_switch_callback_.is_null()) { | 92 if (!pending_switch_callback_.is_null()) { |
91 base::ResetAndReturn(&pending_switch_callback_) | 93 base::ResetAndReturn(&pending_switch_callback_) |
92 .Run(OUTPUT_DEVICE_STATUS_ERROR_INTERNAL); | 94 .Run(OUTPUT_DEVICE_STATUS_ERROR_INTERNAL); |
93 } | 95 } |
94 } | 96 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 return; | 145 return; |
144 } | 146 } |
145 | 147 |
146 DCHECK(pending_switch_callback_.is_null()); | 148 DCHECK(pending_switch_callback_.is_null()); |
147 if (device_id == device_id_) { | 149 if (device_id == device_id_) { |
148 callback.Run(OUTPUT_DEVICE_STATUS_OK); | 150 callback.Run(OUTPUT_DEVICE_STATUS_OK); |
149 return; | 151 return; |
150 } | 152 } |
151 | 153 |
152 OutputDeviceStatus new_mixer_status = OUTPUT_DEVICE_STATUS_ERROR_INTERNAL; | 154 OutputDeviceStatus new_mixer_status = OUTPUT_DEVICE_STATUS_ERROR_INTERNAL; |
153 AudioRendererMixer* new_mixer = mixer_pool_->GetMixer( | 155 AudioRendererMixer* new_mixer = |
154 owner_id_, params_, device_id, security_origin, &new_mixer_status); | 156 mixer_pool_->GetMixer(owner_id_, params_, latency_, device_id, |
| 157 security_origin, &new_mixer_status); |
155 if (new_mixer_status != OUTPUT_DEVICE_STATUS_OK) { | 158 if (new_mixer_status != OUTPUT_DEVICE_STATUS_OK) { |
156 callback.Run(new_mixer_status); | 159 callback.Run(new_mixer_status); |
157 return; | 160 return; |
158 } | 161 } |
159 | 162 |
160 bool was_playing = playing_; | 163 bool was_playing = playing_; |
161 Stop(); | 164 Stop(); |
162 device_id_ = device_id; | 165 device_id_ = device_id; |
163 security_origin_ = security_origin; | 166 security_origin_ = security_origin; |
164 mixer_ = new_mixer; | 167 mixer_ = new_mixer; |
(...skipping 23 matching lines...) Expand all Loading... |
188 base::AutoLock auto_lock(volume_lock_); | 191 base::AutoLock auto_lock(volume_lock_); |
189 return frames_filled > 0 ? volume_ : 0; | 192 return frames_filled > 0 ? volume_ : 0; |
190 } | 193 } |
191 } | 194 } |
192 | 195 |
193 void AudioRendererMixerInput::OnRenderError() { | 196 void AudioRendererMixerInput::OnRenderError() { |
194 callback_->OnRenderError(); | 197 callback_->OnRenderError(); |
195 } | 198 } |
196 | 199 |
197 } // namespace media | 200 } // namespace media |
OLD | NEW |