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

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

Issue 2869733005: Convert some audio code to OnceCallback. (Closed)
Patch Set: Rebase, comments on unretained. Created 3 years, 7 months 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/renderer_host/media/audio_output_delegate_impl.h" 5 #include "content/browser/renderer_host/media/audio_output_delegate_impl.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
(...skipping 26 matching lines...) Expand all
37 base::WeakPtr<AudioOutputDelegateImpl> delegate_; 37 base::WeakPtr<AudioOutputDelegateImpl> delegate_;
38 }; 38 };
39 39
40 AudioOutputDelegateImpl::ControllerEventHandler::ControllerEventHandler( 40 AudioOutputDelegateImpl::ControllerEventHandler::ControllerEventHandler(
41 base::WeakPtr<AudioOutputDelegateImpl> delegate) 41 base::WeakPtr<AudioOutputDelegateImpl> delegate)
42 : delegate_(std::move(delegate)) {} 42 : delegate_(std::move(delegate)) {}
43 43
44 void AudioOutputDelegateImpl::ControllerEventHandler::OnControllerCreated() { 44 void AudioOutputDelegateImpl::ControllerEventHandler::OnControllerCreated() {
45 BrowserThread::PostTask( 45 BrowserThread::PostTask(
46 BrowserThread::IO, FROM_HERE, 46 BrowserThread::IO, FROM_HERE,
47 base::Bind(&AudioOutputDelegateImpl::SendCreatedNotification, delegate_)); 47 base::BindOnce(&AudioOutputDelegateImpl::SendCreatedNotification,
48 delegate_));
48 } 49 }
49 50
50 void AudioOutputDelegateImpl::ControllerEventHandler::OnControllerPlaying() { 51 void AudioOutputDelegateImpl::ControllerEventHandler::OnControllerPlaying() {
51 BrowserThread::PostTask( 52 BrowserThread::PostTask(
52 BrowserThread::IO, FROM_HERE, 53 BrowserThread::IO, FROM_HERE,
53 base::Bind(&AudioOutputDelegateImpl::UpdatePlayingState, delegate_, 54 base::BindOnce(&AudioOutputDelegateImpl::UpdatePlayingState, delegate_,
54 true)); 55 true));
55 } 56 }
56 57
57 void AudioOutputDelegateImpl::ControllerEventHandler::OnControllerPaused() { 58 void AudioOutputDelegateImpl::ControllerEventHandler::OnControllerPaused() {
58 BrowserThread::PostTask( 59 BrowserThread::PostTask(
59 BrowserThread::IO, FROM_HERE, 60 BrowserThread::IO, FROM_HERE,
60 base::Bind(&AudioOutputDelegateImpl::UpdatePlayingState, delegate_, 61 base::BindOnce(&AudioOutputDelegateImpl::UpdatePlayingState, delegate_,
61 false)); 62 false));
62 } 63 }
63 64
64 void AudioOutputDelegateImpl::ControllerEventHandler::OnControllerError() { 65 void AudioOutputDelegateImpl::ControllerEventHandler::OnControllerError() {
65 BrowserThread::PostTask( 66 BrowserThread::PostTask(
66 BrowserThread::IO, FROM_HERE, 67 BrowserThread::IO, FROM_HERE,
67 base::Bind(&AudioOutputDelegateImpl::OnError, delegate_)); 68 base::BindOnce(&AudioOutputDelegateImpl::OnError, delegate_));
68 } 69 }
69 70
70 AudioOutputDelegateImpl::AudioOutputDelegateImpl( 71 AudioOutputDelegateImpl::AudioOutputDelegateImpl(
71 EventHandler* handler, 72 EventHandler* handler,
72 media::AudioManager* audio_manager, 73 media::AudioManager* audio_manager,
73 std::unique_ptr<media::AudioLog> audio_log, 74 std::unique_ptr<media::AudioLog> audio_log,
74 AudioMirroringManager* mirroring_manager, 75 AudioMirroringManager* mirroring_manager,
75 MediaObserver* media_observer, 76 MediaObserver* media_observer,
76 int stream_id, 77 int stream_id,
77 int render_frame_id, 78 int render_frame_id,
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 DCHECK_CURRENTLY_ON(BrowserThread::IO); 112 DCHECK_CURRENTLY_ON(BrowserThread::IO);
112 UpdatePlayingState(false); 113 UpdatePlayingState(false);
113 audio_log_->OnClosed(stream_id_); 114 audio_log_->OnClosed(stream_id_);
114 115
115 // Since the ownership of |controller_| is shared, we instead use its Close 116 // Since the ownership of |controller_| is shared, we instead use its Close
116 // method to stop callbacks from it. |controller_| will call the closure (on 117 // method to stop callbacks from it. |controller_| will call the closure (on
117 // the IO thread) when it's done closing, and it is only after that call that 118 // the IO thread) when it's done closing, and it is only after that call that
118 // we can delete |controller_event_handler_| and |reader_|. By giving the 119 // we can delete |controller_event_handler_| and |reader_|. By giving the
119 // closure ownership of these, we keep them alive until |controller_| is 120 // closure ownership of these, we keep them alive until |controller_| is
120 // closed. |mirroring_manager_| is a lazy instance, so passing it is safe. 121 // closed. |mirroring_manager_| is a lazy instance, so passing it is safe.
121 controller_->Close(base::Bind( 122 controller_->Close(base::BindOnce(
122 [](AudioMirroringManager* mirroring_manager, 123 [](AudioMirroringManager* mirroring_manager,
123 std::unique_ptr<ControllerEventHandler> event_handler, 124 std::unique_ptr<ControllerEventHandler> event_handler,
124 std::unique_ptr<AudioSyncReader> reader, 125 std::unique_ptr<AudioSyncReader> reader,
125 scoped_refptr<media::AudioOutputController> controller) { 126 scoped_refptr<media::AudioOutputController> controller) {
126 // De-register the controller from the AudioMirroringManager now that 127 // De-register the controller from the AudioMirroringManager now that
127 // the controller has closed the AudioOutputStream and shut itself down. 128 // the controller has closed the AudioOutputStream and shut itself down.
128 // This ensures that calling RemoveDiverter() here won't trigger the 129 // This ensures that calling RemoveDiverter() here won't trigger the
129 // controller to re-start the default AudioOutputStream and cause a 130 // controller to re-start the default AudioOutputStream and cause a
130 // brief audio blip to come out the user's speakers. 131 // brief audio blip to come out the user's speakers.
131 // http://crbug.com/474432 132 // http://crbug.com/474432
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 if (playing == playing_) 175 if (playing == playing_)
175 return; 176 return;
176 177
177 playing_ = playing; 178 playing_ = playing;
178 if (playing) { 179 if (playing) {
179 // Note that this takes a reference to |controller_|, and 180 // Note that this takes a reference to |controller_|, and
180 // (Start|Stop)MonitoringStream calls are async, so we don't have a 181 // (Start|Stop)MonitoringStream calls are async, so we don't have a
181 // guarantee for when the controller is destroyed. 182 // guarantee for when the controller is destroyed.
182 AudioStreamMonitor::StartMonitoringStream( 183 AudioStreamMonitor::StartMonitoringStream(
183 render_process_id_, render_frame_id_, stream_id_, 184 render_process_id_, render_frame_id_, stream_id_,
184 base::Bind(&media::AudioOutputController::ReadCurrentPowerAndClip, 185 base::BindRepeating(
185 controller_)); 186 &media::AudioOutputController::ReadCurrentPowerAndClip,
187 controller_));
186 } else { 188 } else {
187 AudioStreamMonitor::StopMonitoringStream(render_process_id_, 189 AudioStreamMonitor::StopMonitoringStream(render_process_id_,
188 render_frame_id_, stream_id_); 190 render_frame_id_, stream_id_);
189 } 191 }
190 } 192 }
191 193
192 void AudioOutputDelegateImpl::OnError() { 194 void AudioOutputDelegateImpl::OnError() {
193 DCHECK_CURRENTLY_ON(BrowserThread::IO); 195 DCHECK_CURRENTLY_ON(BrowserThread::IO);
194 196
195 audio_log_->OnError(stream_id_); 197 audio_log_->OnError(stream_id_);
196 subscriber_->OnStreamError(stream_id_); 198 subscriber_->OnStreamError(stream_id_);
197 } 199 }
198 200
199 media::AudioOutputController* AudioOutputDelegateImpl::GetControllerForTesting() 201 media::AudioOutputController* AudioOutputDelegateImpl::GetControllerForTesting()
200 const { 202 const {
201 return controller_.get(); 203 return controller_.get();
202 } 204 }
203 205
204 } // namespace content 206 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698