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

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

Issue 2443573003: Factor out AudioOutputDelegate from AudioRendererHost. (Closed)
Patch Set: Add AudioOutputDelegate::Deleter. 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 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(),
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
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_);
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?
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(
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
48 stream_id_, render_process_id_, render_frame_id_, audio_log_.get());
49 }
50
51 AudioOutputDelegate::~AudioOutputDelegate() {
52 DCHECK_CURRENTLY_ON(BrowserThread::IO);
53 AudioStreamMonitor::StopMonitoringStream(render_process_id_, render_frame_id_,
54 stream_id_);
55
56 mirroring_manager_->RemoveDiverter(controller_.get());
57 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.
58 }
59
60 void AudioOutputDelegate::Deleter::operator()(AudioOutputDelegate* delegate) {
61 DCHECK_CURRENTLY_ON(BrowserThread::IO);
62 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?
63 delegate->handler_ = nullptr;
64 delegate->audio_log_->OnClosed(delegate->stream_id_);
65 // 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.
66 delegate->controller_->Close(
67 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.
68 }
69 // static
70 AudioOutputDelegate::UniquePtr AudioOutputDelegate::Create(
71 EventHandler* handler,
72 std::unique_ptr<media::AudioLog> audio_log,
73 AudioMirroringManager* mirroring_manager,
74 MediaObserver* media_observer,
75 int stream_id,
76 int render_frame_id,
77 int render_process_id,
78 media::AudioParameters params,
79 const std::string& output_device_id) {
80 return UniquePtr(new AudioOutputDelegate(
81 handler, std::move(audio_log), mirroring_manager, media_observer,
82 stream_id, render_frame_id, render_process_id, params, output_device_id));
83 }
84
85 void AudioOutputDelegate::OnPlayStream() {
86 DCHECK_CURRENTLY_ON(BrowserThread::IO);
87 controller_->Play();
88 audio_log_->OnStarted(stream_id_);
89 }
90
91 void AudioOutputDelegate::OnPauseStream() {
92 DCHECK_CURRENTLY_ON(BrowserThread::IO);
93 controller_->Pause();
94 audio_log_->OnStopped(stream_id_);
95 }
96
97 void AudioOutputDelegate::OnSetVolume(double volume) {
98 DCHECK_CURRENTLY_ON(BrowserThread::IO);
99 controller_->SetVolume(volume);
100 audio_log_->OnSetVolume(stream_id_, volume);
101 }
102
103 void AudioOutputDelegate::OnControllerCreated() {
104 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
105 BrowserThread::PostTask(
106 BrowserThread::IO, FROM_HERE,
107 base::Bind(&AudioOutputDelegate::OnControllerCreated,
108 weak_factory_.GetWeakPtr()));
109 return;
110 }
111 if (!handler_)
112 return;
113
114 handler_->OnStreamCreated(stream_id_, reader_->shared_memory(),
115 reader_->foreign_socket());
116 }
117
118 void AudioOutputDelegate::OnControllerPlaying() {
119 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
120 BrowserThread::PostTask(
121 BrowserThread::IO, FROM_HERE,
122 base::Bind(&AudioOutputDelegate::OnControllerPlaying,
123 weak_factory_.GetWeakPtr()));
124 return;
125 }
126 if (!handler_)
127 return;
128
129 UpdatePlayingStateForHandler(true);
130
131 AudioStreamMonitor::StartMonitoringStream(
132 render_process_id_, render_frame_id_, stream_id_,
133 base::Bind(&media::AudioOutputController::ReadCurrentPowerAndClip,
134 controller_));
135 }
136
137 void AudioOutputDelegate::OnControllerPaused() {
138 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
139 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
140 base::Bind(&AudioOutputDelegate::OnControllerPaused,
141 weak_factory_.GetWeakPtr()));
142 return;
143 }
144 if (!handler_)
145 return;
146
147 UpdatePlayingStateForHandler(false);
148
149 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
150 stream_id_);
151 }
152
153 void AudioOutputDelegate::OnControllerError() {
154 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
155 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
156 base::Bind(&AudioOutputDelegate::OnControllerError,
157 weak_factory_.GetWeakPtr()));
158 return;
159 }
160 if (!handler_)
161 return;
162
163 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.
164 handler_->OnStreamError(stream_id_);
165 }
166
167 void AudioOutputDelegate::UpdatePlayingStateForHandler(bool playing) {
168 DCHECK_CURRENTLY_ON(BrowserThread::IO);
169 if (playing != playing_) {
170 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.
171 handler_->OnStreamStateChanged(playing_);
172 }
173 }
174
175 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698