Chromium Code Reviews| 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..ddfa37532f6b29d679bf749c2fcbab051ef1275b |
| --- /dev/null |
| +++ b/content/browser/renderer_host/media/audio_output_delegate.cc |
| @@ -0,0 +1,155 @@ |
| +// 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 "base/bind.h" |
| +#include "content/browser/media/audio_stream_monitor.h" |
| +#include "content/browser/media/media_internals.h" |
| +#include "content/browser/renderer_host/media/audio_sync_reader.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +namespace { |
| +void RemoveDiverter(content::AudioMirroringManager* mirroring_manager, |
| + scoped_refptr<media::AudioOutputController> controller) { |
| + // De-register the controller from the AudioMirroringManager now that the |
| + // controller has closed the AudioOutputStream and shut itself down. This |
| + // ensures that calling RemoveDiverter() here won't trigger the controller to |
| + // re-start the default AudioOutputStream and cause a brief audio blip to come |
| + // out the user's speakers. http://crbug.com/474432 |
| + mirroring_manager->RemoveDiverter(controller.get()); |
| +} |
| +} // namespace |
| + |
| +namespace content { |
| + |
| +AudioOutputDelegate::AudioOutputDelegate( |
| + EventHandler* handler, |
| + 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_(audio_log), |
|
o1ka
2016/10/25 12:32:34
It's probably fine to have an individual audio_log
Max Morin
2016/10/26 09:20:17
Done.
|
| + mirroring_manager_(mirroring_manager), |
| + controller_(media::AudioOutputController::Create( |
| + media::AudioManager::Get(), |
| + this, |
| + params, |
| + output_device_id, |
| + AudioSyncReader::Create(params))), |
| + 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_); |
| + 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( |
| + stream_id_, render_process_id_, render_frame_id_, audio_log_); |
| +} |
| + |
| +AudioOutputDelegate::~AudioOutputDelegate() {} |
|
o1ka
2016/10/25 12:32:34
AudioOutputController is refcounted, so it may now
Max Morin
2016/10/26 09:20:17
Done.
|
| + |
| +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::OnCloseStream() { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + if (playing_) { |
| + handler_->OnStreamStateChanged(false); |
| + playing_ = false; |
| + } |
| + AudioStreamMonitor::StopMonitoringStream(render_process_id_, render_frame_id_, |
| + stream_id_); |
| + if (mirroring_manager_) |
| + controller_->Close(base::Bind( |
| + &RemoveDiverter, base::Unretained(mirroring_manager_), controller_)); |
|
o1ka
2016/10/25 12:32:34
Unretained is dangerous: no obvious guarantee that
Max Morin
2016/10/26 09:20:17
Done.
|
| + else |
| + controller_->Close(base::Bind([]() {})); |
|
o1ka
2016/10/25 12:32:34
base::Bind(&DoNothing) is a common way
https://cs.
Max Morin
2016/10/26 09:20:17
Acknowledged.
|
| + audio_log_->OnClosed(stream_id_); |
| +} |
| + |
| +void AudioOutputDelegate::OnControllerCreated() { |
| + if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, FROM_HERE, |
| + base::Bind(&AudioOutputDelegate::OnControllerCreated, |
| + weak_factory_.GetWeakPtr())); |
| + return; |
| + } |
| + AudioSyncReader* reader = |
| + static_cast<AudioSyncReader*>(controller_->reader()); |
| + 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 (!playing_) { |
| + handler_->OnStreamStateChanged(true); |
| + playing_ = 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 (playing_) { |
| + handler_->OnStreamStateChanged(false); |
| + playing_ = false; |
| + } |
| + AudioStreamMonitor::StopMonitoringStream(render_process_id_, render_frame_id_, |
| + stream_id_); |
| +} |
| + |
| +void AudioOutputDelegate::OnControllerError() { |
| + if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| + BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| + base::Bind(&AudioOutputDelegate::OnControllerError, |
| + weak_factory_.GetWeakPtr())); |
| + return; |
| + } |
| + audio_log_->OnError(stream_id_); |
| + handler_->OnStreamError(stream_id_); |
| +} |
| + |
| +} // namespace content |