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

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

Issue 2578983003: Add AudioStreamRegistry. Move stream counting logic (Closed)
Patch Set: Add missing EXPECT_TRUE. Created 4 years 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.h" 5 #include "content/browser/renderer_host/media/audio_output_delegate.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "content/browser/media/audio_stream_monitor.h" 10 #include "content/browser/media/audio_stream_monitor.h"
11 #include "content/browser/media/capture/audio_mirroring_manager.h" 11 #include "content/browser/media/capture/audio_mirroring_manager.h"
12 #include "content/browser/media/media_internals.h" 12 #include "content/browser/media/media_internals.h"
13 #include "content/browser/renderer_host/media/audio_sync_reader.h" 13 #include "content/browser/renderer_host/media/audio_sync_reader.h"
14 #include "content/public/browser/browser_thread.h" 14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/media_observer.h" 15 #include "content/public/browser/media_observer.h"
16 16
17 namespace content { 17 namespace content {
18 18
19 AudioOutputDelegate::AudioOutputDelegate( 19 AudioOutputDelegate::AudioOutputDelegate(
20 EventHandler* handler, 20 EventHandler* handler,
21 AudioStreamRegistry* stream_registry,
21 media::AudioManager* audio_manager, 22 media::AudioManager* audio_manager,
22 std::unique_ptr<media::AudioLog> audio_log, 23 std::unique_ptr<media::AudioLog> audio_log,
23 int stream_id, 24 int stream_id,
24 int render_frame_id, 25 int render_frame_id,
25 int render_process_id, 26 int render_process_id,
26 const media::AudioParameters& params, 27 const media::AudioParameters& params,
27 const std::string& output_device_id) 28 const std::string& output_device_id)
28 : handler_(handler), 29 : handler_(handler),
30 stream_registry_(stream_registry),
29 audio_log_(std::move(audio_log)), 31 audio_log_(std::move(audio_log)),
30 reader_(AudioSyncReader::Create(params)), 32 reader_(AudioSyncReader::Create(params)),
31 stream_id_(stream_id), 33 stream_id_(stream_id),
32 render_frame_id_(render_frame_id), 34 render_frame_id_(render_frame_id),
33 render_process_id_(render_process_id), 35 render_process_id_(render_process_id),
34 weak_factory_(this) { 36 weak_factory_(this) {
35 DCHECK(handler_); 37 DCHECK(handler_);
36 DCHECK(audio_manager); 38 DCHECK(audio_manager);
37 DCHECK(audio_log_); 39 DCHECK(audio_log_);
38 weak_this_ = weak_factory_.GetWeakPtr(); 40 weak_this_ = weak_factory_.GetWeakPtr();
39 audio_log_->OnCreated(stream_id, params, output_device_id); 41 audio_log_->OnCreated(stream_id, params, output_device_id);
42 stream_registry_->RegisterOutputStream(this);
40 controller_ = media::AudioOutputController::Create( 43 controller_ = media::AudioOutputController::Create(
41 audio_manager, this, params, output_device_id, reader_.get()); 44 audio_manager, this, params, output_device_id, reader_.get());
42 DCHECK(controller_); 45 DCHECK(controller_);
43 } 46 }
44 47
45 AudioOutputDelegate::~AudioOutputDelegate() { 48 AudioOutputDelegate::~AudioOutputDelegate() {
46 DCHECK_CURRENTLY_ON(BrowserThread::IO); 49 DCHECK_CURRENTLY_ON(BrowserThread::IO);
47 DCHECK(!playing_); 50 DCHECK(!playing_);
48 DCHECK(!handler_); 51 DCHECK(!handler_);
49 } 52 }
50 53
51 void AudioOutputDelegate::Deleter::operator()(AudioOutputDelegate* delegate) { 54 void AudioOutputDelegate::Deleter::operator()(AudioOutputDelegate* delegate) {
52 DCHECK_CURRENTLY_ON(BrowserThread::IO); 55 DCHECK_CURRENTLY_ON(BrowserThread::IO);
53 delegate->UpdatePlayingState(false); 56 delegate->UpdatePlayingState(false);
57 delegate->stream_registry_->DeregisterOutputStream(delegate);
54 delegate->handler_ = nullptr; 58 delegate->handler_ = nullptr;
55 delegate->audio_log_->OnClosed(delegate->stream_id_); 59 delegate->audio_log_->OnClosed(delegate->stream_id_);
56 60
57 // |controller| will call the closure (on IO thread) when it's done closing, 61 // |controller| will call the closure (on IO thread) when it's done closing,
58 // and it is only after that call that we can delete |delegate|. By giving the 62 // and it is only after that call that we can delete |delegate|. By giving the
59 // closure ownership of |delegate|, we keep delegate alive until |controller| 63 // closure ownership of |delegate|, we keep delegate alive until |controller|
60 // is closed. 64 // is closed.
61 // 65 //
62 // The mirroring manager is a leaky singleton, so Unretained is fine. 66 // The mirroring manager is a leaky singleton, so Unretained is fine.
63 delegate->controller_->Close(base::Bind( 67 delegate->controller_->Close(base::Bind(
(...skipping 11 matching lines...) Expand all
75 // removed. 79 // removed.
76 if (mirroring_manager) 80 if (mirroring_manager)
77 mirroring_manager->RemoveDiverter(delegate->controller_.get()); 81 mirroring_manager->RemoveDiverter(delegate->controller_.get());
78 }, 82 },
79 base::Owned(delegate), base::Unretained(mirroring_manager_))); 83 base::Owned(delegate), base::Unretained(mirroring_manager_)));
80 } 84 }
81 85
82 // static 86 // static
83 AudioOutputDelegate::UniquePtr AudioOutputDelegate::Create( 87 AudioOutputDelegate::UniquePtr AudioOutputDelegate::Create(
84 EventHandler* handler, 88 EventHandler* handler,
89 AudioStreamRegistry* stream_registry,
85 media::AudioManager* audio_manager, 90 media::AudioManager* audio_manager,
86 std::unique_ptr<media::AudioLog> audio_log, 91 std::unique_ptr<media::AudioLog> audio_log,
87 AudioMirroringManager* mirroring_manager, 92 AudioMirroringManager* mirroring_manager,
88 MediaObserver* media_observer, 93 MediaObserver* media_observer,
89 int stream_id, 94 int stream_id,
90 int render_frame_id, 95 int render_frame_id,
91 int render_process_id, 96 int render_process_id,
92 const media::AudioParameters& params, 97 const media::AudioParameters& params,
93 const std::string& output_device_id) { 98 const std::string& output_device_id) {
94 if (media_observer) 99 if (media_observer)
95 media_observer->OnCreatingAudioStream(render_process_id, render_frame_id); 100 media_observer->OnCreatingAudioStream(render_process_id, render_frame_id);
96 UniquePtr delegate( 101 UniquePtr delegate(
97 new AudioOutputDelegate(handler, audio_manager, std::move(audio_log), 102 new AudioOutputDelegate(handler, stream_registry, audio_manager,
98 stream_id, render_frame_id, render_process_id, 103 std::move(audio_log), stream_id, render_frame_id,
99 params, output_device_id), 104 render_process_id, params, output_device_id),
100 Deleter(mirroring_manager)); 105 Deleter(mirroring_manager));
101 if (mirroring_manager) 106 if (mirroring_manager)
102 mirroring_manager->AddDiverter(render_process_id, render_frame_id, 107 mirroring_manager->AddDiverter(render_process_id, render_frame_id,
103 delegate->controller_.get()); 108 delegate->controller_.get());
104 return delegate; 109 return delegate;
105 } 110 }
106 111
112 #if BUILDFLAG(ENABLE_WEBRTC)
113 void AudioOutputDelegate::EnableDebugRecording(
114 const base::FilePath& base_file_name) {}
115
116 void AudioOutputDelegate::DisableDebugRecording() {}
117 #endif
118
107 void AudioOutputDelegate::OnPlayStream() { 119 void AudioOutputDelegate::OnPlayStream() {
108 DCHECK_CURRENTLY_ON(BrowserThread::IO); 120 DCHECK_CURRENTLY_ON(BrowserThread::IO);
109 controller_->Play(); 121 controller_->Play();
110 audio_log_->OnStarted(stream_id_); 122 audio_log_->OnStarted(stream_id_);
111 } 123 }
112 124
113 void AudioOutputDelegate::OnPauseStream() { 125 void AudioOutputDelegate::OnPauseStream() {
114 DCHECK_CURRENTLY_ON(BrowserThread::IO); 126 DCHECK_CURRENTLY_ON(BrowserThread::IO);
115 controller_->Pause(); 127 controller_->Pause();
116 audio_log_->OnStopped(stream_id_); 128 audio_log_->OnStopped(stream_id_);
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 reader_->foreign_socket()); 167 reader_->foreign_socket());
156 } 168 }
157 } 169 }
158 170
159 void AudioOutputDelegate::UpdatePlayingState(bool playing) { 171 void AudioOutputDelegate::UpdatePlayingState(bool playing) {
160 DCHECK_CURRENTLY_ON(BrowserThread::IO); 172 DCHECK_CURRENTLY_ON(BrowserThread::IO);
161 if (!handler_ || playing == playing_) 173 if (!handler_ || playing == playing_)
162 return; 174 return;
163 175
164 playing_ = playing; 176 playing_ = playing;
165 handler_->OnStreamStateChanged(playing); 177 stream_registry_->OutputStreamStateChanged(this, playing);
166 if (playing) { 178 if (playing) {
167 // Note that this takes a reference to |controller_|, and 179 // Note that this takes a reference to |controller_|, and
168 // (Start|Stop)MonitoringStream calls are async, so we don't have a 180 // (Start|Stop)MonitoringStream calls are async, so we don't have a
169 // guarantee for when the controller is destroyed. 181 // guarantee for when the controller is destroyed.
170 AudioStreamMonitor::StartMonitoringStream( 182 AudioStreamMonitor::StartMonitoringStream(
171 render_process_id_, render_frame_id_, stream_id_, 183 render_process_id_, render_frame_id_, stream_id_,
172 base::Bind(&media::AudioOutputController::ReadCurrentPowerAndClip, 184 base::Bind(&media::AudioOutputController::ReadCurrentPowerAndClip,
173 controller_)); 185 controller_));
174 } else { 186 } else {
175 AudioStreamMonitor::StopMonitoringStream(render_process_id_, 187 AudioStreamMonitor::StopMonitoringStream(render_process_id_,
176 render_frame_id_, stream_id_); 188 render_frame_id_, stream_id_);
177 } 189 }
178 } 190 }
179 191
180 void AudioOutputDelegate::OnError() { 192 void AudioOutputDelegate::OnError() {
181 DCHECK_CURRENTLY_ON(BrowserThread::IO); 193 DCHECK_CURRENTLY_ON(BrowserThread::IO);
182 194
183 if (!handler_) 195 if (!handler_)
184 return; 196 return;
185 197
186 audio_log_->OnError(stream_id_); 198 audio_log_->OnError(stream_id_);
187 handler_->OnStreamError(stream_id_); 199 handler_->OnStreamError(stream_id_);
188 } 200 }
189 201
190 } // namespace content 202 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698