| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 "content/browser/renderer_host/media/audio_output_delegate.h" | 5 #include "content/browser/renderer_host/media/audio_output_delegate.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "content/browser/media/audio_stream_monitor.h" | 10 #include "content/browser/media/audio_stream_monitor.h" |
| 11 #include "content/browser/media/capture/audio_mirroring_manager.h" | 11 #include "content/browser/media/capture/audio_mirroring_manager.h" |
| 12 #include "content/browser/media/media_internals.h" | 12 #include "content/browser/media/media_internals.h" |
| 13 #include "content/browser/renderer_host/media/audio_sync_reader.h" | 13 #include "content/browser/renderer_host/media/audio_sync_reader.h" |
| 14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 15 #include "content/public/browser/media_observer.h" | 15 #include "content/public/browser/media_observer.h" |
| 16 | 16 |
| 17 namespace content { | 17 namespace content { |
| 18 | 18 |
| 19 AudioOutputDelegate::AudioOutputDelegate( | 19 AudioOutputDelegate::AudioOutputDelegate( |
| 20 EventHandler* handler, | 20 EventHandler* handler, |
| 21 AudioStreamRegistry* stream_registry, |
| 21 media::AudioManager* audio_manager, | 22 media::AudioManager* audio_manager, |
| 22 std::unique_ptr<media::AudioLog> audio_log, | 23 std::unique_ptr<media::AudioLog> audio_log, |
| 23 int stream_id, | 24 int stream_id, |
| 24 int render_frame_id, | 25 int render_frame_id, |
| 25 int render_process_id, | 26 int render_process_id, |
| 26 const media::AudioParameters& params, | 27 const media::AudioParameters& params, |
| 27 const std::string& output_device_id) | 28 const std::string& output_device_id) |
| 28 : handler_(handler), | 29 : handler_(handler), |
| 30 stream_registry_(stream_registry), |
| 29 audio_log_(std::move(audio_log)), | 31 audio_log_(std::move(audio_log)), |
| 30 reader_(AudioSyncReader::Create(params)), | 32 reader_(AudioSyncReader::Create(params)), |
| 31 stream_id_(stream_id), | 33 stream_id_(stream_id), |
| 32 render_frame_id_(render_frame_id), | 34 render_frame_id_(render_frame_id), |
| 33 render_process_id_(render_process_id), | 35 render_process_id_(render_process_id), |
| 34 weak_factory_(this) { | 36 weak_factory_(this) { |
| 35 DCHECK(handler_); | 37 DCHECK(handler_); |
| 36 DCHECK(audio_manager); | 38 DCHECK(audio_manager); |
| 37 DCHECK(audio_log_); | 39 DCHECK(audio_log_); |
| 38 weak_this_ = weak_factory_.GetWeakPtr(); | 40 weak_this_ = weak_factory_.GetWeakPtr(); |
| 39 audio_log_->OnCreated(stream_id, params, output_device_id); | 41 audio_log_->OnCreated(stream_id, params, output_device_id); |
| 42 stream_registry->RegisterOutputStream(this); |
| 40 controller_ = media::AudioOutputController::Create( | 43 controller_ = media::AudioOutputController::Create( |
| 41 audio_manager, this, params, output_device_id, reader_.get()); | 44 audio_manager, this, params, output_device_id, reader_.get()); |
| 42 DCHECK(controller_); | 45 DCHECK(controller_); |
| 43 } | 46 } |
| 44 | 47 |
| 45 AudioOutputDelegate::~AudioOutputDelegate() { | 48 AudioOutputDelegate::~AudioOutputDelegate() { |
| 46 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 49 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 47 DCHECK(!playing_); | 50 DCHECK(!playing_); |
| 48 DCHECK(!handler_); | 51 DCHECK(!handler_); |
| 49 } | 52 } |
| 50 | 53 |
| 51 void AudioOutputDelegate::Deleter::operator()(AudioOutputDelegate* delegate) { | 54 void AudioOutputDelegate::Deleter::operator()(AudioOutputDelegate* delegate) { |
| 52 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 55 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 53 delegate->UpdatePlayingState(false); | 56 delegate->UpdatePlayingState(false); |
| 57 delegate->stream_registry_->DeregisterOutputStream(delegate); |
| 54 delegate->handler_ = nullptr; | 58 delegate->handler_ = nullptr; |
| 55 delegate->audio_log_->OnClosed(delegate->stream_id_); | 59 delegate->audio_log_->OnClosed(delegate->stream_id_); |
| 56 | 60 |
| 57 // |controller| will call the closure (on IO thread) when it's done closing, | 61 // |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 | 62 // 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| | 63 // closure ownership of |delegate|, we keep delegate alive until |controller| |
| 60 // is closed. | 64 // is closed. |
| 61 // | 65 // |
| 62 // The mirroring manager is a leaky singleton, so Unretained is fine. | 66 // The mirroring manager is a leaky singleton, so Unretained is fine. |
| 63 delegate->controller_->Close(base::Bind( | 67 delegate->controller_->Close(base::Bind( |
| (...skipping 11 matching lines...) Expand all Loading... |
| 75 // removed. | 79 // removed. |
| 76 if (mirroring_manager) | 80 if (mirroring_manager) |
| 77 mirroring_manager->RemoveDiverter(delegate->controller_.get()); | 81 mirroring_manager->RemoveDiverter(delegate->controller_.get()); |
| 78 }, | 82 }, |
| 79 base::Owned(delegate), base::Unretained(mirroring_manager_))); | 83 base::Owned(delegate), base::Unretained(mirroring_manager_))); |
| 80 } | 84 } |
| 81 | 85 |
| 82 // static | 86 // static |
| 83 AudioOutputDelegate::UniquePtr AudioOutputDelegate::Create( | 87 AudioOutputDelegate::UniquePtr AudioOutputDelegate::Create( |
| 84 EventHandler* handler, | 88 EventHandler* handler, |
| 89 AudioStreamRegistry* stream_registry, |
| 85 media::AudioManager* audio_manager, | 90 media::AudioManager* audio_manager, |
| 86 std::unique_ptr<media::AudioLog> audio_log, | 91 std::unique_ptr<media::AudioLog> audio_log, |
| 87 AudioMirroringManager* mirroring_manager, | 92 AudioMirroringManager* mirroring_manager, |
| 88 MediaObserver* media_observer, | 93 MediaObserver* media_observer, |
| 89 int stream_id, | 94 int stream_id, |
| 90 int render_frame_id, | 95 int render_frame_id, |
| 91 int render_process_id, | 96 int render_process_id, |
| 92 const media::AudioParameters& params, | 97 const media::AudioParameters& params, |
| 93 const std::string& output_device_id) { | 98 const std::string& output_device_id) { |
| 94 if (media_observer) | 99 if (media_observer) |
| 95 media_observer->OnCreatingAudioStream(render_process_id, render_frame_id); | 100 media_observer->OnCreatingAudioStream(render_process_id, render_frame_id); |
| 96 UniquePtr delegate( | 101 UniquePtr delegate( |
| 97 new AudioOutputDelegate(handler, audio_manager, std::move(audio_log), | 102 new AudioOutputDelegate(handler, stream_registry, audio_manager, |
| 98 stream_id, render_frame_id, render_process_id, | 103 std::move(audio_log), stream_id, render_frame_id, |
| 99 params, output_device_id), | 104 render_process_id, params, output_device_id), |
| 100 Deleter(mirroring_manager)); | 105 Deleter(mirroring_manager)); |
| 101 if (mirroring_manager) | 106 if (mirroring_manager) |
| 102 mirroring_manager->AddDiverter(render_process_id, render_frame_id, | 107 mirroring_manager->AddDiverter(render_process_id, render_frame_id, |
| 103 delegate->controller_.get()); | 108 delegate->controller_.get()); |
| 104 return delegate; | 109 return delegate; |
| 105 } | 110 } |
| 106 | 111 |
| 107 void AudioOutputDelegate::OnPlayStream() { | 112 void AudioOutputDelegate::OnPlayStream() { |
| 108 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 113 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 109 controller_->Play(); | 114 controller_->Play(); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 reader_->foreign_socket()); | 160 reader_->foreign_socket()); |
| 156 } | 161 } |
| 157 } | 162 } |
| 158 | 163 |
| 159 void AudioOutputDelegate::UpdatePlayingState(bool playing) { | 164 void AudioOutputDelegate::UpdatePlayingState(bool playing) { |
| 160 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 165 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 161 if (!handler_ || playing == playing_) | 166 if (!handler_ || playing == playing_) |
| 162 return; | 167 return; |
| 163 | 168 |
| 164 playing_ = playing; | 169 playing_ = playing; |
| 165 handler_->OnStreamStateChanged(playing); | 170 stream_registry_->OutputStreamStateChanged(playing); |
| 166 if (playing) { | 171 if (playing) { |
| 167 // Note that this takes a reference to |controller_|, and | 172 // Note that this takes a reference to |controller_|, and |
| 168 // (Start|Stop)MonitoringStream calls are async, so we don't have a | 173 // (Start|Stop)MonitoringStream calls are async, so we don't have a |
| 169 // guarantee for when the controller is destroyed. | 174 // guarantee for when the controller is destroyed. |
| 170 AudioStreamMonitor::StartMonitoringStream( | 175 AudioStreamMonitor::StartMonitoringStream( |
| 171 render_process_id_, render_frame_id_, stream_id_, | 176 render_process_id_, render_frame_id_, stream_id_, |
| 172 base::Bind(&media::AudioOutputController::ReadCurrentPowerAndClip, | 177 base::Bind(&media::AudioOutputController::ReadCurrentPowerAndClip, |
| 173 controller_)); | 178 controller_)); |
| 174 } else { | 179 } else { |
| 175 AudioStreamMonitor::StopMonitoringStream(render_process_id_, | 180 AudioStreamMonitor::StopMonitoringStream(render_process_id_, |
| 176 render_frame_id_, stream_id_); | 181 render_frame_id_, stream_id_); |
| 177 } | 182 } |
| 178 } | 183 } |
| 179 | 184 |
| 180 void AudioOutputDelegate::OnError() { | 185 void AudioOutputDelegate::OnError() { |
| 181 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 186 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 182 | 187 |
| 183 if (!handler_) | 188 if (!handler_) |
| 184 return; | 189 return; |
| 185 | 190 |
| 186 audio_log_->OnError(stream_id_); | 191 audio_log_->OnError(stream_id_); |
| 187 handler_->OnStreamError(stream_id_); | 192 handler_->OnStreamError(stream_id_); |
| 188 } | 193 } |
| 189 | 194 |
| 190 } // namespace content | 195 } // namespace content |
| OLD | NEW |