| 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 owner_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 owner_id_(owner_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(owner_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(owner_id_, params_, device_id_, security_origin_); |
| 81 mixer_ = nullptr; | 85 mixer_ = nullptr; |
| 82 } | 86 } |
| 83 | 87 |
| 84 started_ = false; | 88 started_ = false; |
| 85 | 89 |
| 86 if (!pending_switch_callback_.is_null()) { | 90 if (!pending_switch_callback_.is_null()) { |
| 87 base::ResetAndReturn(&pending_switch_callback_) | 91 base::ResetAndReturn(&pending_switch_callback_) |
| 88 .Run(OUTPUT_DEVICE_STATUS_ERROR_INTERNAL); | 92 .Run(OUTPUT_DEVICE_STATUS_ERROR_INTERNAL); |
| 89 } | 93 } |
| 90 } | 94 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 105 playing_ = false; | 109 playing_ = false; |
| 106 } | 110 } |
| 107 | 111 |
| 108 bool AudioRendererMixerInput::SetVolume(double volume) { | 112 bool AudioRendererMixerInput::SetVolume(double volume) { |
| 109 base::AutoLock auto_lock(volume_lock_); | 113 base::AutoLock auto_lock(volume_lock_); |
| 110 volume_ = volume; | 114 volume_ = volume; |
| 111 return true; | 115 return true; |
| 112 } | 116 } |
| 113 | 117 |
| 114 OutputDeviceInfo AudioRendererMixerInput::GetOutputDeviceInfo() { | 118 OutputDeviceInfo AudioRendererMixerInput::GetOutputDeviceInfo() { |
| 115 return mixer_ ? mixer_->GetOutputDeviceInfo() : OutputDeviceInfo(); | 119 return mixer_ |
| 120 ? mixer_->GetOutputDeviceInfo() |
| 121 : mixer_pool_->GetOutputDeviceInfo(owner_id_, 0 /* session_id */, |
| 122 device_id_, security_origin_); |
| 116 } | 123 } |
| 117 | 124 |
| 118 void AudioRendererMixerInput::SwitchOutputDevice( | 125 void AudioRendererMixerInput::SwitchOutputDevice( |
| 119 const std::string& device_id, | 126 const std::string& device_id, |
| 120 const url::Origin& security_origin, | 127 const url::Origin& security_origin, |
| 121 const OutputDeviceStatusCB& callback) { | 128 const OutputDeviceStatusCB& callback) { |
| 122 if (!mixer_) { | 129 if (!mixer_) { |
| 123 if (pending_switch_callback_.is_null()) { | 130 if (pending_switch_callback_.is_null()) { |
| 124 pending_switch_callback_ = callback; | 131 pending_switch_callback_ = callback; |
| 125 pending_switch_device_id_ = device_id; | 132 pending_switch_device_id_ = device_id; |
| 126 pending_switch_security_origin_ = security_origin; | 133 pending_switch_security_origin_ = security_origin; |
| 127 } else { | 134 } else { |
| 128 callback.Run(OUTPUT_DEVICE_STATUS_ERROR_INTERNAL); | 135 callback.Run(OUTPUT_DEVICE_STATUS_ERROR_INTERNAL); |
| 129 } | 136 } |
| 130 | 137 |
| 131 return; | 138 return; |
| 132 } | 139 } |
| 133 | 140 |
| 134 DCHECK(pending_switch_callback_.is_null()); | 141 DCHECK(pending_switch_callback_.is_null()); |
| 135 if (device_id == device_id_) { | 142 if (device_id == device_id_) { |
| 136 callback.Run(OUTPUT_DEVICE_STATUS_OK); | 143 callback.Run(OUTPUT_DEVICE_STATUS_OK); |
| 137 return; | 144 return; |
| 138 } | 145 } |
| 139 | 146 |
| 140 OutputDeviceStatus new_mixer_status = OUTPUT_DEVICE_STATUS_ERROR_INTERNAL; | 147 OutputDeviceStatus new_mixer_status = OUTPUT_DEVICE_STATUS_ERROR_INTERNAL; |
| 141 AudioRendererMixer* new_mixer = | 148 AudioRendererMixer* new_mixer = mixer_pool_->GetMixer( |
| 142 get_mixer_cb_.Run(params_, device_id, security_origin, &new_mixer_status); | 149 owner_id_, params_, device_id, security_origin, &new_mixer_status); |
| 143 if (new_mixer_status != OUTPUT_DEVICE_STATUS_OK) { | 150 if (new_mixer_status != OUTPUT_DEVICE_STATUS_OK) { |
| 144 callback.Run(new_mixer_status); | 151 callback.Run(new_mixer_status); |
| 145 return; | 152 return; |
| 146 } | 153 } |
| 147 | 154 |
| 148 bool was_playing = playing_; | 155 bool was_playing = playing_; |
| 149 Stop(); | 156 Stop(); |
| 150 device_id_ = device_id; | 157 device_id_ = device_id; |
| 151 security_origin_ = security_origin; | 158 security_origin_ = security_origin; |
| 152 mixer_ = new_mixer; | 159 mixer_ = new_mixer; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 182 base::AutoLock auto_lock(volume_lock_); | 189 base::AutoLock auto_lock(volume_lock_); |
| 183 return frames_filled > 0 ? volume_ : 0; | 190 return frames_filled > 0 ? volume_ : 0; |
| 184 } | 191 } |
| 185 } | 192 } |
| 186 | 193 |
| 187 void AudioRendererMixerInput::OnRenderError() { | 194 void AudioRendererMixerInput::OnRenderError() { |
| 188 callback_->OnRenderError(); | 195 callback_->OnRenderError(); |
| 189 } | 196 } |
| 190 | 197 |
| 191 } // namespace media | 198 } // namespace media |
| OLD | NEW |