Chromium Code Reviews| 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/media_internals.h" | |
| 12 #include "content/public/browser/browser_thread.h" | |
| 13 | |
| 14 namespace content { | |
| 15 | |
| 16 AudioOutputDelegate::AudioOutputDelegate( | |
| 17 EventHandler* handler, | |
| 18 std::unique_ptr<media::AudioLog> audio_log, | |
| 19 AudioMirroringManager* mirroring_manager, | |
| 20 MediaObserver* media_observer, | |
| 21 int stream_id, | |
| 22 int render_frame_id, | |
| 23 int render_process_id, | |
| 24 media::AudioParameters params, | |
| 25 const std::string& output_device_id) | |
| 26 : handler_(handler), | |
| 27 audio_log_(std::move(audio_log)), | |
| 28 mirroring_manager_(mirroring_manager), | |
| 29 reader_(AudioSyncReader::Create(params)), | |
| 30 controller_( | |
| 31 media::AudioOutputController::Create(media::AudioManager::Get(), | |
| 32 this, | |
| 33 params, | |
| 34 output_device_id, | |
| 35 reader_.get())), | |
| 36 stream_id_(stream_id), | |
| 37 render_frame_id_(render_frame_id), | |
| 38 render_process_id_(render_process_id), | |
| 39 weak_factory_(this) { | |
| 40 DCHECK(controller_); | |
| 41 if (media_observer) | |
| 42 media_observer->OnCreatingAudioStream(render_process_id_, render_frame_id_); | |
| 43 if (mirroring_manager_) | |
| 44 mirroring_manager_->AddDiverter(render_process_id_, render_frame_id_, | |
| 45 controller_.get()); | |
| 46 audio_log_->OnCreated(stream_id, params, output_device_id); | |
| 47 MediaInternals::GetInstance()->SetWebContentsTitleForAudioLogEntry( | |
| 48 stream_id_, render_process_id_, render_frame_id_, audio_log_.get()); | |
| 49 } | |
| 50 | |
| 51 AudioOutputDelegate::~AudioOutputDelegate() {} | |
| 52 | |
| 53 void AudioOutputDelegate::OnPlayStream() { | |
| 54 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 55 controller_->Play(); | |
| 56 audio_log_->OnStarted(stream_id_); | |
| 57 } | |
| 58 | |
| 59 void AudioOutputDelegate::OnPauseStream() { | |
| 60 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 61 controller_->Pause(); | |
| 62 audio_log_->OnStopped(stream_id_); | |
| 63 } | |
| 64 | |
| 65 void AudioOutputDelegate::OnSetVolume(double volume) { | |
| 66 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 67 controller_->SetVolume(volume); | |
| 68 audio_log_->OnSetVolume(stream_id_, volume); | |
| 69 } | |
| 70 | |
| 71 void AudioOutputDelegate::OnCloseStream( | |
| 72 std::unique_ptr<AudioOutputDelegate> owned_this) { | |
| 73 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 74 weak_factory_.InvalidateWeakPtrs(); | |
|
o1ka
2016/10/25 15:59:41
Why do you want to do it at this very moment?
Max Morin
2016/10/26 09:20:18
I'll just add a flag instead.
| |
| 75 if (playing_) { | |
| 76 handler_->OnStreamStateChanged(false); | |
| 77 playing_ = false; | |
| 78 } | |
| 79 audio_log_->OnClosed(stream_id_); | |
| 80 controller_->Close(base::Bind(&AudioOutputDelegate::FinishClosing, | |
| 81 base::Owned(owned_this.release()))); | |
| 82 } | |
| 83 | |
| 84 void AudioOutputDelegate::FinishClosing() { | |
| 85 AudioStreamMonitor::StopMonitoringStream(render_process_id_, render_frame_id_, | |
| 86 stream_id_); | |
| 87 | |
| 88 mirroring_manager_->RemoveDiverter(controller_.get()); | |
| 89 } | |
| 90 | |
| 91 void AudioOutputDelegate::OnControllerCreated() { | |
| 92 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | |
| 93 BrowserThread::PostTask( | |
| 94 BrowserThread::IO, FROM_HERE, | |
| 95 base::Bind(&AudioOutputDelegate::OnControllerCreated, | |
| 96 weak_factory_.GetWeakPtr())); | |
| 97 return; | |
| 98 } | |
| 99 | |
| 100 handler_->OnStreamCreated(stream_id_, reader_->shared_memory(), | |
| 101 reader_->foreign_socket()); | |
| 102 } | |
| 103 | |
| 104 void AudioOutputDelegate::OnControllerPlaying() { | |
| 105 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | |
| 106 BrowserThread::PostTask( | |
| 107 BrowserThread::IO, FROM_HERE, | |
| 108 base::Bind(&AudioOutputDelegate::OnControllerPlaying, | |
| 109 weak_factory_.GetWeakPtr())); | |
| 110 return; | |
| 111 } | |
| 112 | |
| 113 if (!playing_) { | |
| 114 handler_->OnStreamStateChanged(true); | |
| 115 playing_ = true; | |
| 116 } | |
| 117 AudioStreamMonitor::StartMonitoringStream( | |
| 118 render_process_id_, render_frame_id_, stream_id_, | |
| 119 base::Bind(&media::AudioOutputController::ReadCurrentPowerAndClip, | |
| 120 controller_)); | |
| 121 } | |
| 122 | |
| 123 void AudioOutputDelegate::OnControllerPaused() { | |
| 124 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | |
| 125 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
| 126 base::Bind(&AudioOutputDelegate::OnControllerPaused, | |
| 127 weak_factory_.GetWeakPtr())); | |
| 128 return; | |
| 129 } | |
| 130 | |
| 131 if (playing_) { | |
| 132 handler_->OnStreamStateChanged(false); | |
| 133 playing_ = false; | |
| 134 } | |
| 135 AudioStreamMonitor::StopMonitoringStream(render_process_id_, render_frame_id_, | |
| 136 stream_id_); | |
| 137 } | |
| 138 | |
| 139 void AudioOutputDelegate::OnControllerError() { | |
| 140 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | |
| 141 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
| 142 base::Bind(&AudioOutputDelegate::OnControllerError, | |
| 143 weak_factory_.GetWeakPtr())); | |
| 144 return; | |
| 145 } | |
| 146 | |
| 147 audio_log_->OnError(stream_id_); | |
| 148 handler_->OnStreamError(stream_id_); | |
| 149 } | |
| 150 | |
| 151 } // namespace content | |
| OLD | NEW |