OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/renderer_host/media/audio_output_delegate.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "content/browser/media/audio_stream_monitor.h" |
| 11 #include "content/browser/media/capture/audio_mirroring_manager.h" |
| 12 #include "content/browser/media/media_internals.h" |
| 13 #include "content/browser/renderer_host/media/audio_sync_reader.h" |
| 14 #include "content/public/browser/browser_thread.h" |
| 15 #include "content/public/browser/media_observer.h" |
| 16 |
| 17 namespace content { |
| 18 |
| 19 AudioOutputDelegate::AudioOutputDelegate( |
| 20 EventHandler* handler, |
| 21 media::AudioManager* audio_manager, |
| 22 std::unique_ptr<media::AudioLog> audio_log, |
| 23 int stream_id, |
| 24 int render_frame_id, |
| 25 int render_process_id, |
| 26 const media::AudioParameters& params, |
| 27 const std::string& output_device_id) |
| 28 : handler_(handler), |
| 29 audio_log_(std::move(audio_log)), |
| 30 reader_(AudioSyncReader::Create(params)), |
| 31 stream_id_(stream_id), |
| 32 render_frame_id_(render_frame_id), |
| 33 render_process_id_(render_process_id), |
| 34 weak_factory_(this) { |
| 35 DCHECK(handler_); |
| 36 DCHECK(audio_manager); |
| 37 DCHECK(audio_log_); |
| 38 weak_this_ = weak_factory_.GetWeakPtr(); |
| 39 audio_log_->OnCreated(stream_id, params, output_device_id); |
| 40 controller_ = media::AudioOutputController::Create( |
| 41 audio_manager, this, params, output_device_id, reader_.get()); |
| 42 DCHECK(controller_); |
| 43 } |
| 44 |
| 45 AudioOutputDelegate::~AudioOutputDelegate() { |
| 46 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 47 DCHECK(!playing_); |
| 48 DCHECK(!handler_); |
| 49 } |
| 50 |
| 51 void AudioOutputDelegate::Deleter::operator()(AudioOutputDelegate* delegate) { |
| 52 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 53 delegate->UpdatePlayingState(false); |
| 54 delegate->handler_ = nullptr; |
| 55 delegate->audio_log_->OnClosed(delegate->stream_id_); |
| 56 |
| 57 // |controller| will call the closure (on IO thread) when it's done closing, |
| 58 // and it is only after that call that we can delete |delegate|. By giving the |
| 59 // closure ownership of |delegate|, we keep delegate alive until |controller| |
| 60 // is closed. |
| 61 // |
| 62 // The mirroring manager is a leaky singleton, so Unretained is fine. |
| 63 delegate->controller_->Close(base::Bind( |
| 64 [](AudioOutputDelegate* delegate, |
| 65 AudioMirroringManager* mirroring_manager) { |
| 66 // De-register the controller from the AudioMirroringManager now that |
| 67 // the controller has closed the AudioOutputStream and shut itself down. |
| 68 // This ensures that calling RemoveDiverter() here won't trigger the |
| 69 // controller to re-start the default AudioOutputStream and cause a |
| 70 // brief audio blip to come out the user's speakers. |
| 71 // http://crbug.com/474432 |
| 72 // |
| 73 // It's fine if the task is canceled during shutdown, since the |
| 74 // mirroring manager doesn't require that all diverters are |
| 75 // removed. |
| 76 if (mirroring_manager) |
| 77 mirroring_manager->RemoveDiverter(delegate->controller_.get()); |
| 78 }, |
| 79 base::Owned(delegate), base::Unretained(mirroring_manager_))); |
| 80 } |
| 81 |
| 82 // static |
| 83 AudioOutputDelegate::UniquePtr AudioOutputDelegate::Create( |
| 84 EventHandler* handler, |
| 85 media::AudioManager* audio_manager, |
| 86 std::unique_ptr<media::AudioLog> audio_log, |
| 87 AudioMirroringManager* mirroring_manager, |
| 88 MediaObserver* media_observer, |
| 89 int stream_id, |
| 90 int render_frame_id, |
| 91 int render_process_id, |
| 92 const media::AudioParameters& params, |
| 93 const std::string& output_device_id) { |
| 94 if (media_observer) |
| 95 media_observer->OnCreatingAudioStream(render_process_id, render_frame_id); |
| 96 UniquePtr delegate( |
| 97 new AudioOutputDelegate(handler, audio_manager, std::move(audio_log), |
| 98 stream_id, render_frame_id, render_process_id, |
| 99 params, output_device_id), |
| 100 Deleter(mirroring_manager)); |
| 101 if (mirroring_manager) |
| 102 mirroring_manager->AddDiverter(render_process_id, render_frame_id, |
| 103 delegate->controller_.get()); |
| 104 return delegate; |
| 105 } |
| 106 |
| 107 void AudioOutputDelegate::OnPlayStream() { |
| 108 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 109 controller_->Play(); |
| 110 audio_log_->OnStarted(stream_id_); |
| 111 } |
| 112 |
| 113 void AudioOutputDelegate::OnPauseStream() { |
| 114 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 115 controller_->Pause(); |
| 116 audio_log_->OnStopped(stream_id_); |
| 117 } |
| 118 |
| 119 void AudioOutputDelegate::OnSetVolume(double volume) { |
| 120 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 121 DCHECK_GE(volume, 0); |
| 122 DCHECK_LE(volume, 1); |
| 123 controller_->SetVolume(volume); |
| 124 audio_log_->OnSetVolume(stream_id_, volume); |
| 125 } |
| 126 |
| 127 void AudioOutputDelegate::OnControllerCreated() { |
| 128 BrowserThread::PostTask( |
| 129 BrowserThread::IO, FROM_HERE, |
| 130 base::Bind(&AudioOutputDelegate::SendCreatedNotification, weak_this_)); |
| 131 } |
| 132 |
| 133 void AudioOutputDelegate::OnControllerPlaying() { |
| 134 BrowserThread::PostTask( |
| 135 BrowserThread::IO, FROM_HERE, |
| 136 base::Bind(&AudioOutputDelegate::UpdatePlayingState, weak_this_, true)); |
| 137 } |
| 138 |
| 139 void AudioOutputDelegate::OnControllerPaused() { |
| 140 BrowserThread::PostTask( |
| 141 BrowserThread::IO, FROM_HERE, |
| 142 base::Bind(&AudioOutputDelegate::UpdatePlayingState, weak_this_, false)); |
| 143 } |
| 144 |
| 145 void AudioOutputDelegate::OnControllerError() { |
| 146 BrowserThread::PostTask( |
| 147 BrowserThread::IO, FROM_HERE, |
| 148 base::Bind(&AudioOutputDelegate::OnError, weak_this_)); |
| 149 } |
| 150 |
| 151 void AudioOutputDelegate::SendCreatedNotification() { |
| 152 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 153 if (handler_) { |
| 154 handler_->OnStreamCreated(stream_id_, reader_->shared_memory(), |
| 155 reader_->foreign_socket()); |
| 156 } |
| 157 } |
| 158 |
| 159 void AudioOutputDelegate::UpdatePlayingState(bool playing) { |
| 160 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 161 if (!handler_ || playing == playing_) |
| 162 return; |
| 163 |
| 164 playing_ = playing; |
| 165 handler_->OnStreamStateChanged(playing); |
| 166 if (playing) { |
| 167 // Note that this takes a reference to |controller_|, and |
| 168 // (Start|Stop)MonitoringStream calls are async, so we don't have a |
| 169 // guarantee for when the controller is destroyed. |
| 170 AudioStreamMonitor::StartMonitoringStream( |
| 171 render_process_id_, render_frame_id_, stream_id_, |
| 172 base::Bind(&media::AudioOutputController::ReadCurrentPowerAndClip, |
| 173 controller_)); |
| 174 } else { |
| 175 AudioStreamMonitor::StopMonitoringStream(render_process_id_, |
| 176 render_frame_id_, stream_id_); |
| 177 } |
| 178 } |
| 179 |
| 180 void AudioOutputDelegate::OnError() { |
| 181 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 182 |
| 183 if (!handler_) |
| 184 return; |
| 185 |
| 186 audio_log_->OnError(stream_id_); |
| 187 handler_->OnStreamError(stream_id_); |
| 188 } |
| 189 |
| 190 } // namespace content |
OLD | NEW |