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 | 13 |
13 namespace media { | 14 namespace media { |
14 | 15 |
15 AudioRendererMixerInput::AudioRendererMixerInput( | 16 AudioRendererMixerInput::AudioRendererMixerInput( |
16 const GetMixerCB& get_mixer_cb, | 17 AudioRendererMixerPool* mixer_pool, |
17 const RemoveMixerCB& remove_mixer_cb, | 18 int source_render_frame_id, |
18 const std::string& device_id, | 19 const std::string& device_id, |
19 const url::Origin& security_origin) | 20 const url::Origin& security_origin) |
20 : started_(false), | 21 : mixer_pool_(mixer_pool), |
| 22 started_(false), |
21 playing_(false), | 23 playing_(false), |
22 volume_(1.0f), | 24 volume_(1.0f), |
23 get_mixer_cb_(get_mixer_cb), | 25 source_render_frame_id_(source_render_frame_id), |
24 remove_mixer_cb_(remove_mixer_cb), | |
25 device_id_(device_id), | 26 device_id_(device_id), |
26 security_origin_(security_origin), | 27 security_origin_(security_origin), |
27 mixer_(nullptr), | 28 mixer_(nullptr), |
28 callback_(nullptr), | 29 callback_(nullptr), |
29 error_cb_(base::Bind(&AudioRendererMixerInput::OnRenderError, | 30 error_cb_(base::Bind(&AudioRendererMixerInput::OnRenderError, |
30 base::Unretained(this))) {} | 31 base::Unretained(this))) { |
| 32 DCHECK(mixer_pool_); |
| 33 } |
31 | 34 |
32 AudioRendererMixerInput::~AudioRendererMixerInput() { | 35 AudioRendererMixerInput::~AudioRendererMixerInput() { |
33 DCHECK(!started_); | 36 DCHECK(!started_); |
34 DCHECK(!mixer_); | 37 DCHECK(!mixer_); |
35 } | 38 } |
36 | 39 |
37 void AudioRendererMixerInput::Initialize( | 40 void AudioRendererMixerInput::Initialize( |
38 const AudioParameters& params, | 41 const AudioParameters& params, |
39 AudioRendererSink::RenderCallback* callback) { | 42 AudioRendererSink::RenderCallback* callback) { |
40 DCHECK(!started_); | 43 DCHECK(!started_); |
41 DCHECK(!mixer_); | 44 DCHECK(!mixer_); |
42 DCHECK(callback); | 45 DCHECK(callback); |
43 | 46 |
44 params_ = params; | 47 params_ = params; |
45 callback_ = callback; | 48 callback_ = callback; |
46 } | 49 } |
47 | 50 |
48 void AudioRendererMixerInput::Start() { | 51 void AudioRendererMixerInput::Start() { |
49 DCHECK(!started_); | 52 DCHECK(!started_); |
50 DCHECK(!mixer_); | 53 DCHECK(!mixer_); |
51 DCHECK(callback_); // Initialized. | 54 DCHECK(callback_); // Initialized. |
52 | 55 |
53 started_ = true; | 56 started_ = true; |
54 mixer_ = get_mixer_cb_.Run(params_, device_id_, security_origin_, nullptr); | 57 mixer_ = mixer_pool_->GetMixer(source_render_frame_id_, params_, device_id_, |
| 58 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(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() { |
71 // Stop() may be called at any time, if Pause() hasn't been called we need to | 75 // Stop() may be called at any time, if Pause() hasn't been called we need to |
72 // remove our mixer input before shutdown. | 76 // remove our mixer input before shutdown. |
73 Pause(); | 77 Pause(); |
74 | 78 |
75 if (mixer_) { | 79 if (mixer_) { |
76 // TODO(dalecurtis): This is required so that |callback_| isn't called after | 80 // 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 | 81 // 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 | 82 // should instead have sane ownership semantics: http://crbug.com/151051 |
79 mixer_->RemoveErrorCallback(error_cb_); | 83 mixer_->RemoveErrorCallback(error_cb_); |
80 remove_mixer_cb_.Run(params_, device_id_, security_origin_); | 84 mixer_pool_->ReturnMixer(source_render_frame_id_, params_, device_id_, |
| 85 security_origin_); |
81 mixer_ = nullptr; | 86 mixer_ = nullptr; |
82 } | 87 } |
83 | 88 |
84 started_ = false; | 89 started_ = false; |
85 | 90 |
86 if (!pending_switch_callback_.is_null()) { | 91 if (!pending_switch_callback_.is_null()) { |
87 base::ResetAndReturn(&pending_switch_callback_) | 92 base::ResetAndReturn(&pending_switch_callback_) |
88 .Run(OUTPUT_DEVICE_STATUS_ERROR_INTERNAL); | 93 .Run(OUTPUT_DEVICE_STATUS_ERROR_INTERNAL); |
89 } | 94 } |
90 } | 95 } |
(...skipping 14 matching lines...) Expand all Loading... |
105 playing_ = false; | 110 playing_ = false; |
106 } | 111 } |
107 | 112 |
108 bool AudioRendererMixerInput::SetVolume(double volume) { | 113 bool AudioRendererMixerInput::SetVolume(double volume) { |
109 base::AutoLock auto_lock(volume_lock_); | 114 base::AutoLock auto_lock(volume_lock_); |
110 volume_ = volume; | 115 volume_ = volume; |
111 return true; | 116 return true; |
112 } | 117 } |
113 | 118 |
114 OutputDeviceInfo AudioRendererMixerInput::GetOutputDeviceInfo() { | 119 OutputDeviceInfo AudioRendererMixerInput::GetOutputDeviceInfo() { |
115 return mixer_ ? mixer_->GetOutputDeviceInfo() : OutputDeviceInfo(); | 120 return mixer_ ? mixer_->GetOutputDeviceInfo() |
| 121 : mixer_pool_->GetOutputDeviceInfo( |
| 122 source_render_frame_id_, 0 /* session_id */, device_id_, |
| 123 security_origin_); |
116 } | 124 } |
117 | 125 |
118 void AudioRendererMixerInput::SwitchOutputDevice( | 126 void AudioRendererMixerInput::SwitchOutputDevice( |
119 const std::string& device_id, | 127 const std::string& device_id, |
120 const url::Origin& security_origin, | 128 const url::Origin& security_origin, |
121 const OutputDeviceStatusCB& callback) { | 129 const OutputDeviceStatusCB& callback) { |
122 if (!mixer_) { | 130 if (!mixer_) { |
123 if (pending_switch_callback_.is_null()) { | 131 if (pending_switch_callback_.is_null()) { |
124 pending_switch_callback_ = callback; | 132 pending_switch_callback_ = callback; |
125 pending_switch_device_id_ = device_id; | 133 pending_switch_device_id_ = device_id; |
126 pending_switch_security_origin_ = security_origin; | 134 pending_switch_security_origin_ = security_origin; |
127 } else { | 135 } else { |
128 callback.Run(OUTPUT_DEVICE_STATUS_ERROR_INTERNAL); | 136 callback.Run(OUTPUT_DEVICE_STATUS_ERROR_INTERNAL); |
129 } | 137 } |
130 | 138 |
131 return; | 139 return; |
132 } | 140 } |
133 | 141 |
134 DCHECK(pending_switch_callback_.is_null()); | 142 DCHECK(pending_switch_callback_.is_null()); |
135 if (device_id == device_id_) { | 143 if (device_id == device_id_) { |
136 callback.Run(OUTPUT_DEVICE_STATUS_OK); | 144 callback.Run(OUTPUT_DEVICE_STATUS_OK); |
137 return; | 145 return; |
138 } | 146 } |
139 | 147 |
140 OutputDeviceStatus new_mixer_status = OUTPUT_DEVICE_STATUS_ERROR_INTERNAL; | 148 OutputDeviceStatus new_mixer_status = OUTPUT_DEVICE_STATUS_ERROR_INTERNAL; |
141 AudioRendererMixer* new_mixer = | 149 AudioRendererMixer* new_mixer = |
142 get_mixer_cb_.Run(params_, device_id, security_origin, &new_mixer_status); | 150 mixer_pool_->GetMixer(source_render_frame_id_, params_, device_id, |
| 151 security_origin, &new_mixer_status); |
143 if (new_mixer_status != OUTPUT_DEVICE_STATUS_OK) { | 152 if (new_mixer_status != OUTPUT_DEVICE_STATUS_OK) { |
144 callback.Run(new_mixer_status); | 153 callback.Run(new_mixer_status); |
145 return; | 154 return; |
146 } | 155 } |
147 | 156 |
148 bool was_playing = playing_; | 157 bool was_playing = playing_; |
149 Stop(); | 158 Stop(); |
150 device_id_ = device_id; | 159 device_id_ = device_id; |
151 security_origin_ = security_origin; | 160 security_origin_ = security_origin; |
152 mixer_ = new_mixer; | 161 mixer_ = new_mixer; |
(...skipping 29 matching lines...) Expand all Loading... |
182 base::AutoLock auto_lock(volume_lock_); | 191 base::AutoLock auto_lock(volume_lock_); |
183 return frames_filled > 0 ? volume_ : 0; | 192 return frames_filled > 0 ? volume_ : 0; |
184 } | 193 } |
185 } | 194 } |
186 | 195 |
187 void AudioRendererMixerInput::OnRenderError() { | 196 void AudioRendererMixerInput::OnRenderError() { |
188 callback_->OnRenderError(); | 197 callback_->OnRenderError(); |
189 } | 198 } |
190 | 199 |
191 } // namespace media | 200 } // namespace media |
OLD | NEW |