Index: content/browser/renderer_host/media/audio_output_delegate.cc |
diff --git a/content/browser/renderer_host/media/audio_output_delegate.cc b/content/browser/renderer_host/media/audio_output_delegate.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..da498cb90bdf2d0e5113dad7555d21c59463bb71 |
--- /dev/null |
+++ b/content/browser/renderer_host/media/audio_output_delegate.cc |
@@ -0,0 +1,175 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/browser/renderer_host/media/audio_output_delegate.h" |
+ |
+#include <utility> |
+ |
+#include "base/bind.h" |
+#include "content/browser/media/audio_stream_monitor.h" |
+#include "content/browser/media/media_internals.h" |
+#include "content/public/browser/browser_thread.h" |
+ |
+namespace content { |
+ |
+AudioOutputDelegate::AudioOutputDelegate( |
+ EventHandler* handler, |
+ std::unique_ptr<media::AudioLog> audio_log, |
+ AudioMirroringManager* mirroring_manager, |
+ MediaObserver* media_observer, |
+ int stream_id, |
+ int render_frame_id, |
+ int render_process_id, |
+ media::AudioParameters params, |
+ const std::string& output_device_id) |
+ : handler_(handler), |
+ audio_log_(std::move(audio_log)), |
+ mirroring_manager_(mirroring_manager), |
+ reader_(AudioSyncReader::Create(params)), |
+ controller_( |
+ media::AudioOutputController::Create(media::AudioManager::Get(), |
o1ka
2016/10/27 09:52:38
We need to figure out and state here why it's guar
Max Morin
2016/10/27 15:04:38
I'll just pass the AudioManager* in to the constru
|
+ this, |
+ params, |
+ output_device_id, |
+ reader_.get())), |
+ stream_id_(stream_id), |
+ render_frame_id_(render_frame_id), |
+ render_process_id_(render_process_id), |
+ weak_factory_(this) { |
+ DCHECK(controller_); |
+ if (media_observer) |
+ media_observer->OnCreatingAudioStream(render_process_id_, render_frame_id_); |
o1ka
2016/10/27 09:52:38
We can do it in Create() now.
Max Morin
2016/10/28 14:55:47
Why is that preferable?
|
+ if (mirroring_manager_) |
+ mirroring_manager_->AddDiverter(render_process_id_, render_frame_id_, |
+ controller_.get()); |
+ audio_log_->OnCreated(stream_id, params, output_device_id); |
+ MediaInternals::GetInstance()->SetWebContentsTitleForAudioLogEntry( |
o1ka
2016/10/27 12:59:52
Would be nice to avoid dependency on MediaInternal
Max Morin
2016/10/28 14:55:47
There is probably a reason OnCreated is called bef
|
+ stream_id_, render_process_id_, render_frame_id_, audio_log_.get()); |
+} |
+ |
+AudioOutputDelegate::~AudioOutputDelegate() { |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+ AudioStreamMonitor::StopMonitoringStream(render_process_id_, render_frame_id_, |
+ stream_id_); |
+ |
+ mirroring_manager_->RemoveDiverter(controller_.get()); |
+ DCHECK(!playing_); |
o1ka
2016/10/27 09:52:38
nit: move DCHECK to the top? (It's a prerequisite)
Max Morin
2016/10/27 15:04:38
Done.
|
+} |
+ |
+void AudioOutputDelegate::Deleter::operator()(AudioOutputDelegate* delegate) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+ delegate->UpdatePlayingStateForHandler(false); |
o1ka
2016/10/28 09:13:22
It will call handler_->OnStreamStateChange, and th
Max Morin
2016/10/28 14:55:47
I see the point that the handler could otherwise b
o1ka
2016/11/03 13:39:11
Have you done that?
|
+ delegate->handler_ = nullptr; |
+ delegate->audio_log_->OnClosed(delegate->stream_id_); |
+ // Keep delegate alive until controller is closed: |
o1ka
2016/10/28 09:13:22
Could you extend the comment to explain that contr
Max Morin
2016/10/28 14:55:47
Done.
|
+ delegate->controller_->Close( |
+ base::Bind([](AudioOutputDelegate*) {}, base::Owned(delegate))); |
o1ka
2016/10/27 09:52:38
Looks like there is no guarantee that the destruct
o1ka
2016/10/27 12:59:52
Ok, looks like it's fine, we just need to add a co
Max Morin
2016/10/27 15:04:38
Done.
|
+} |
+// static |
+AudioOutputDelegate::UniquePtr AudioOutputDelegate::Create( |
+ EventHandler* handler, |
+ std::unique_ptr<media::AudioLog> audio_log, |
+ AudioMirroringManager* mirroring_manager, |
+ MediaObserver* media_observer, |
+ int stream_id, |
+ int render_frame_id, |
+ int render_process_id, |
+ media::AudioParameters params, |
+ const std::string& output_device_id) { |
+ return UniquePtr(new AudioOutputDelegate( |
+ handler, std::move(audio_log), mirroring_manager, media_observer, |
+ stream_id, render_frame_id, render_process_id, params, output_device_id)); |
+} |
+ |
+void AudioOutputDelegate::OnPlayStream() { |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+ controller_->Play(); |
+ audio_log_->OnStarted(stream_id_); |
+} |
+ |
+void AudioOutputDelegate::OnPauseStream() { |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+ controller_->Pause(); |
+ audio_log_->OnStopped(stream_id_); |
+} |
+ |
+void AudioOutputDelegate::OnSetVolume(double volume) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+ controller_->SetVolume(volume); |
+ audio_log_->OnSetVolume(stream_id_, volume); |
+} |
+ |
+void AudioOutputDelegate::OnControllerCreated() { |
+ if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
+ BrowserThread::PostTask( |
+ BrowserThread::IO, FROM_HERE, |
+ base::Bind(&AudioOutputDelegate::OnControllerCreated, |
+ weak_factory_.GetWeakPtr())); |
+ return; |
+ } |
+ if (!handler_) |
+ return; |
+ |
+ handler_->OnStreamCreated(stream_id_, reader_->shared_memory(), |
+ reader_->foreign_socket()); |
+} |
+ |
+void AudioOutputDelegate::OnControllerPlaying() { |
+ if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
+ BrowserThread::PostTask( |
+ BrowserThread::IO, FROM_HERE, |
+ base::Bind(&AudioOutputDelegate::OnControllerPlaying, |
+ weak_factory_.GetWeakPtr())); |
+ return; |
+ } |
+ if (!handler_) |
+ return; |
+ |
+ UpdatePlayingStateForHandler(true); |
+ |
+ AudioStreamMonitor::StartMonitoringStream( |
+ render_process_id_, render_frame_id_, stream_id_, |
+ base::Bind(&media::AudioOutputController::ReadCurrentPowerAndClip, |
+ controller_)); |
+} |
+ |
+void AudioOutputDelegate::OnControllerPaused() { |
+ if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
+ BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
+ base::Bind(&AudioOutputDelegate::OnControllerPaused, |
+ weak_factory_.GetWeakPtr())); |
+ return; |
+ } |
+ if (!handler_) |
+ return; |
+ |
+ UpdatePlayingStateForHandler(false); |
+ |
+ AudioStreamMonitor::StopMonitoringStream(render_process_id_, render_frame_id_, |
o1ka
2016/10/27 09:52:38
We may never stop monitoring if |handler_| was res
Max Morin
2016/10/27 15:04:38
We stop monitoring in the destructor. The idea wit
|
+ stream_id_); |
+} |
+ |
+void AudioOutputDelegate::OnControllerError() { |
+ if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
+ BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
+ base::Bind(&AudioOutputDelegate::OnControllerError, |
+ weak_factory_.GetWeakPtr())); |
+ return; |
+ } |
+ if (!handler_) |
+ return; |
+ |
+ audio_log_->OnError(stream_id_); |
o1ka
2016/10/27 09:52:38
Do we probably want to log an error even if |handl
Max Morin
2016/10/27 15:04:38
Ok.
|
+ handler_->OnStreamError(stream_id_); |
+} |
+ |
+void AudioOutputDelegate::UpdatePlayingStateForHandler(bool playing) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+ if (playing != playing_) { |
+ playing_ = playing; |
o1ka
2016/10/27 09:52:38
Note that we want to update |playing_| even if |ha
Max Morin
2016/10/27 15:04:38
If we reset the handler while |playing_| is true,
o1ka
2016/10/28 09:13:22
Ok, sounds good. Than let's (1) DCHECK |handler_|
Max Morin
2016/10/28 14:55:47
Done.
|
+ handler_->OnStreamStateChanged(playing_); |
+ } |
+} |
+ |
+} // namespace content |