Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(403)

Side by Side Diff: content/browser/renderer_host/media/audio_output_delegate.cc

Issue 2443573003: Factor out AudioOutputDelegate from AudioRendererHost. (Closed)
Patch Set: Remove [&] from lambda. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 media::AudioManager* audio_manager,
19 std::unique_ptr<media::AudioLog> audio_log,
20 AudioMirroringManager* mirroring_manager,
21 MediaObserver* media_observer,
22 int stream_id,
23 int render_frame_id,
24 int render_process_id,
25 media::AudioParameters params,
26 const std::string& output_device_id)
27 : handler_(handler),
o1ka 2016/11/03 13:39:12 Either we need to DCHECK(handler_) or we need to h
Max Morin 2016/11/18 16:03:42 Done.
28 audio_log_(std::move(audio_log)),
29 mirroring_manager_(mirroring_manager),
30 reader_(AudioSyncReader::Create(params)),
31 controller_(media::AudioOutputController::Create(audio_manager,
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(audio_log_);
41 DCHECK(controller_);
42 if (media_observer)
43 media_observer->OnCreatingAudioStream(render_process_id_, render_frame_id_);
44 if (mirroring_manager_)
45 mirroring_manager_->AddDiverter(render_process_id_, render_frame_id_,
46 controller_.get());
47
48 audio_log_->OnCreated(stream_id, params, output_device_id);
49 }
50
51 AudioOutputDelegate::~AudioOutputDelegate() {
52 DCHECK_CURRENTLY_ON(BrowserThread::IO);
53 DCHECK(!playing_);
o1ka 2016/11/03 13:39:12 DCHECK(!handler_) probably?
Max Morin 2016/11/18 16:03:42 Done.
54 AudioStreamMonitor::StopMonitoringStream(render_process_id_, render_frame_id_,
o1ka 2016/11/03 13:39:12 I suspect we do not monitor at this point? Is it f
Max Morin 2016/11/18 16:03:42 Yes, it's fine to start monitoring while already m
55 stream_id_);
56
57 mirroring_manager_->RemoveDiverter(controller_.get());
o1ka 2016/11/03 13:39:12 Please restore the comment on why diverter is remo
Max Morin 2016/11/18 16:03:42 Done.
58 }
59
60 void AudioOutputDelegate::Deleter::operator()(AudioOutputDelegate* delegate) {
61 DCHECK_CURRENTLY_ON(BrowserThread::IO);
62 delegate->UpdatePlayingStateForHandler(false);
63 delegate->handler_ = nullptr;
64 delegate->audio_log_->OnClosed(delegate->stream_id_);
65 // |controller_| will call the closure (on IO thread) when it's done closing,
66 // and it is only after that call that we can delete |delegate|. By giving the
67 // closure ownership of |delegate|, we keep delegate alive until |controller_|
68 // is closed.
69 delegate->controller_->Close(
70 base::Bind([](AudioOutputDelegate*) {}, base::Owned(delegate)));
71 }
72 // static
o1ka 2016/11/03 13:39:12 nit: add a line before
Max Morin 2016/11/18 16:03:42 Done.
73 AudioOutputDelegate::UniquePtr AudioOutputDelegate::Create(
74 EventHandler* handler,
75 media::AudioManager* audio_manager,
76 std::unique_ptr<media::AudioLog> audio_log,
77 AudioMirroringManager* mirroring_manager,
78 MediaObserver* media_observer,
79 int stream_id,
80 int render_frame_id,
81 int render_process_id,
82 media::AudioParameters params,
83 const std::string& output_device_id) {
84 return UniquePtr(new AudioOutputDelegate(
85 handler, audio_manager, std::move(audio_log), mirroring_manager,
86 media_observer, stream_id, render_frame_id, render_process_id, params,
87 output_device_id));
88 }
89
90 void AudioOutputDelegate::OnPlayStream() {
91 DCHECK_CURRENTLY_ON(BrowserThread::IO);
92 controller_->Play();
93 audio_log_->OnStarted(stream_id_);
94 }
95
96 void AudioOutputDelegate::OnPauseStream() {
97 DCHECK_CURRENTLY_ON(BrowserThread::IO);
98 controller_->Pause();
99 audio_log_->OnStopped(stream_id_);
100 }
101
102 void AudioOutputDelegate::OnSetVolume(double volume) {
103 DCHECK_CURRENTLY_ON(BrowserThread::IO);
104 controller_->SetVolume(volume);
105 audio_log_->OnSetVolume(stream_id_, volume);
106 }
107
108 void AudioOutputDelegate::OnControllerCreated() {
109 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
110 BrowserThread::PostTask(
111 BrowserThread::IO, FROM_HERE,
112 base::Bind(&AudioOutputDelegate::OnControllerCreated,
113 weak_factory_.GetWeakPtr()));
114 return;
115 }
116 if (!handler_)
117 return;
118
119 handler_->OnStreamCreated(stream_id_, reader_->shared_memory(),
120 reader_->foreign_socket());
121 }
122
123 void AudioOutputDelegate::OnControllerPlaying() {
124 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
125 BrowserThread::PostTask(
126 BrowserThread::IO, FROM_HERE,
127 base::Bind(&AudioOutputDelegate::OnControllerPlaying,
128 weak_factory_.GetWeakPtr()));
129 return;
130 }
131 if (!handler_)
132 return;
133
134 UpdatePlayingStateForHandler(true);
135
136 AudioStreamMonitor::StartMonitoringStream(
o1ka 2016/11/03 13:39:12 We want to do it only if we switched from pause to
Max Morin 2016/11/18 16:03:42 Done.
137 render_process_id_, render_frame_id_, stream_id_,
138 base::Bind(&media::AudioOutputController::ReadCurrentPowerAndClip,
139 controller_));
o1ka 2016/11/03 13:39:12 I would add an explicit comment that AudioStreamMo
Max Morin 2016/11/18 16:03:42 Good point. We don't have to worry too much since
140 }
141
142 void AudioOutputDelegate::OnControllerPaused() {
143 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
144 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
145 base::Bind(&AudioOutputDelegate::OnControllerPaused,
146 weak_factory_.GetWeakPtr()));
147 return;
148 }
149 if (!handler_)
150 return;
151
152 UpdatePlayingStateForHandler(false);
153
154 AudioStreamMonitor::StopMonitoringStream(render_process_id_, render_frame_id_,
o1ka 2016/11/03 13:39:12 same here: if we do not monitor it, do we want to
Max Morin 2016/11/18 16:03:42 Done.
155 stream_id_);
156 }
157
158 void AudioOutputDelegate::OnControllerError() {
159 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
160 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
161 base::Bind(&AudioOutputDelegate::OnControllerError,
162 weak_factory_.GetWeakPtr()));
163 return;
164 }
165 audio_log_->OnError(stream_id_);
166
167 if (!handler_)
168 return;
169
170 handler_->OnStreamError(stream_id_);
171 }
172
173 void AudioOutputDelegate::UpdatePlayingStateForHandler(bool playing) {
174 DCHECK_CURRENTLY_ON(BrowserThread::IO);
175 DCHECK(handler_);
176 if (playing != playing_) {
177 playing_ = playing;
178 handler_->OnStreamStateChanged(playing_);
179 }
180 }
181
182 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698