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

Side by Side Diff: content/browser/renderer_host/media/audio_renderer_host.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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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_renderer_host.h" 5 #include "content/browser/renderer_host/media/audio_renderer_host.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
11 #include "base/lazy_instance.h" 11 #include "base/lazy_instance.h"
12 #include "base/metrics/histogram_macros.h" 12 #include "base/metrics/histogram_macros.h"
13 #include "content/browser/bad_message.h" 13 #include "content/browser/bad_message.h"
14 #include "content/browser/browser_main_loop.h" 14 #include "content/browser/browser_main_loop.h"
15 #include "content/browser/media/audio_stream_monitor.h" 15 #include "content/browser/media/audio_stream_monitor.h"
16 #include "content/browser/media/capture/audio_mirroring_manager.h" 16 #include "content/browser/media/capture/audio_mirroring_manager.h"
17 #include "content/browser/media/media_internals.h" 17 #include "content/browser/media/media_internals.h"
18 #include "content/browser/renderer_host/media/audio_input_device_manager.h" 18 #include "content/browser/renderer_host/media/audio_input_device_manager.h"
19 #include "content/browser/renderer_host/media/audio_stream_registry.h"
19 #include "content/browser/renderer_host/media/audio_sync_reader.h" 20 #include "content/browser/renderer_host/media/audio_sync_reader.h"
20 #include "content/browser/renderer_host/media/media_stream_manager.h" 21 #include "content/browser/renderer_host/media/media_stream_manager.h"
21 #include "content/common/media/audio_messages.h" 22 #include "content/common/media/audio_messages.h"
22 #include "content/public/browser/content_browser_client.h" 23 #include "content/public/browser/content_browser_client.h"
23 #include "content/public/browser/media_device_id.h" 24 #include "content/public/browser/media_device_id.h"
24 #include "content/public/browser/media_observer.h" 25 #include "content/public/browser/media_observer.h"
25 #include "content/public/browser/render_frame_host.h" 26 #include "content/public/browser/render_frame_host.h"
26 #include "media/audio/audio_device_description.h" 27 #include "media/audio/audio_device_description.h"
27 #include "media/audio/audio_streams_tracker.h"
28 #include "media/base/audio_bus.h" 28 #include "media/base/audio_bus.h"
29 #include "media/base/limits.h" 29 #include "media/base/limits.h"
30 30
31 using media::AudioBus; 31 using media::AudioBus;
32 using media::AudioManager; 32 using media::AudioManager;
33 33
34 namespace content { 34 namespace content {
35 35
36 namespace { 36 namespace {
37 37
38 // Tracks the maximum number of simultaneous output streams browser-wide.
39 // Accessed on IO thread.
40 base::LazyInstance<media::AudioStreamsTracker> g_audio_streams_tracker =
41 LAZY_INSTANCE_INITIALIZER;
42
43 void NotifyRenderProcessHostThatAudioStateChanged(int render_process_id) {
44 DCHECK_CURRENTLY_ON(BrowserThread::UI);
45
46 RenderProcessHost* render_process_host =
47 RenderProcessHost::FromID(render_process_id);
48
49 if (render_process_host)
50 render_process_host->AudioStateChanged();
51 }
52
53 void UMALogDeviceAuthorizationTime(base::TimeTicks auth_start_time) { 38 void UMALogDeviceAuthorizationTime(base::TimeTicks auth_start_time) {
54 UMA_HISTOGRAM_CUSTOM_TIMES("Media.Audio.OutputDeviceAuthorizationTime", 39 UMA_HISTOGRAM_CUSTOM_TIMES("Media.Audio.OutputDeviceAuthorizationTime",
55 base::TimeTicks::Now() - auth_start_time, 40 base::TimeTicks::Now() - auth_start_time,
56 base::TimeDelta::FromMilliseconds(1), 41 base::TimeDelta::FromMilliseconds(1),
57 base::TimeDelta::FromMilliseconds(5000), 50); 42 base::TimeDelta::FromMilliseconds(5000), 50);
58 } 43 }
59 44
60 // Check that the routing ID references a valid RenderFrameHost, and run 45 // Check that the routing ID references a valid RenderFrameHost, and run
61 // |callback| on the IO thread with true if the ID is valid. 46 // |callback| on the IO thread with true if the ID is valid.
62 void ValidateRenderFrameId(int render_process_id, 47 void ValidateRenderFrameId(int render_process_id,
63 int render_frame_id, 48 int render_frame_id,
64 const base::Callback<void(bool)>& callback) { 49 const base::Callback<void(bool)>& callback) {
65 DCHECK_CURRENTLY_ON(BrowserThread::UI); 50 DCHECK_CURRENTLY_ON(BrowserThread::UI);
66 const bool frame_exists = 51 const bool frame_exists =
67 !!RenderFrameHost::FromID(render_process_id, render_frame_id); 52 !!RenderFrameHost::FromID(render_process_id, render_frame_id);
68 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 53 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
69 base::Bind(callback, frame_exists)); 54 base::Bind(callback, frame_exists));
70 } 55 }
71 56
72 } // namespace 57 } // namespace
73 58
74 /////////////////////////////////////////////////////////////////////////////// 59 ///////////////////////////////////////////////////////////////////////////////
75 // AudioRendererHost implementations. 60 // AudioRendererHost implementations.
76 61
77 AudioRendererHost::AudioRendererHost(int render_process_id, 62 AudioRendererHost::AudioRendererHost(int render_process_id,
63 AudioStreamRegistry* stream_registry,
78 media::AudioManager* audio_manager, 64 media::AudioManager* audio_manager,
79 AudioMirroringManager* mirroring_manager, 65 AudioMirroringManager* mirroring_manager,
80 MediaStreamManager* media_stream_manager, 66 MediaStreamManager* media_stream_manager,
81 const std::string& salt) 67 const std::string& salt)
82 : BrowserMessageFilter(AudioMsgStart), 68 : BrowserMessageFilter(AudioMsgStart),
83 render_process_id_(render_process_id), 69 render_process_id_(render_process_id),
70 stream_registry_(stream_registry),
84 audio_manager_(audio_manager), 71 audio_manager_(audio_manager),
85 mirroring_manager_(mirroring_manager), 72 mirroring_manager_(mirroring_manager),
86 media_stream_manager_(media_stream_manager), 73 media_stream_manager_(media_stream_manager),
87 num_playing_streams_(0),
88 salt_(salt), 74 salt_(salt),
89 validate_render_frame_id_function_(&ValidateRenderFrameId), 75 validate_render_frame_id_function_(&ValidateRenderFrameId),
90 max_simultaneous_streams_(0),
91 authorization_handler_(audio_manager_, 76 authorization_handler_(audio_manager_,
92 media_stream_manager, 77 media_stream_manager,
93 render_process_id_, 78 render_process_id_,
94 salt) { 79 salt) {
95 DCHECK(audio_manager_); 80 DCHECK(audio_manager_);
96 } 81 }
97 82
98 AudioRendererHost::~AudioRendererHost() { 83 AudioRendererHost::~AudioRendererHost() {
99 DCHECK_CURRENTLY_ON(BrowserThread::IO); 84 DCHECK_CURRENTLY_ON(BrowserThread::IO);
100 DCHECK(delegates_.empty()); 85 DCHECK(delegates_.empty());
101
102 // If we had any streams, report UMA stats for the maximum number of
103 // simultaneous streams for this render process and for the whole browser
104 // process since last reported.
105 if (max_simultaneous_streams_ > 0) {
106 UMA_HISTOGRAM_CUSTOM_COUNTS("Media.AudioRendererIpcStreams",
107 max_simultaneous_streams_, 1, 50, 51);
108 UMA_HISTOGRAM_CUSTOM_COUNTS(
109 "Media.AudioRendererIpcStreamsTotal",
110 g_audio_streams_tracker.Get().max_stream_count(),
111 1, 100, 101);
112 g_audio_streams_tracker.Get().ResetMaxStreamCount();
113 }
114 } 86 }
115 87
116 void AudioRendererHost::GetOutputControllers( 88 void AudioRendererHost::GetOutputControllers(
117 const RenderProcessHost::GetAudioOutputControllersCallback& 89 const RenderProcessHost::GetAudioOutputControllersCallback&
118 callback) const { 90 callback) const {
119 BrowserThread::PostTaskAndReplyWithResult( 91 BrowserThread::PostTaskAndReplyWithResult(
120 BrowserThread::IO, FROM_HERE, 92 BrowserThread::IO, FROM_HERE,
121 base::Bind(&AudioRendererHost::DoGetOutputControllers, this), callback); 93 base::Bind(&AudioRendererHost::DoGetOutputControllers, this), callback);
122 } 94 }
123 95
124 void AudioRendererHost::OnChannelClosing() { 96 void AudioRendererHost::OnChannelClosing() {
125 DCHECK_CURRENTLY_ON(BrowserThread::IO); 97 DCHECK_CURRENTLY_ON(BrowserThread::IO);
126 // Since the IPC sender is gone, close all requested audio streams. 98 // Since the IPC sender is gone, close all requested audio streams.
127 // The audio streams tracker isn't automatically decremented since the
128 // removal isn't done through OnCloseStream.
129 g_audio_streams_tracker.Get().DecreaseStreamCount(delegates_.size());
130 delegates_.clear(); 99 delegates_.clear();
131 100
132 // Remove any authorizations for streams that were not yet created 101 // Remove any authorizations for streams that were not yet created
133 authorizations_.clear(); 102 authorizations_.clear();
134 } 103 }
135 104
136 void AudioRendererHost::OnDestruct() const { 105 void AudioRendererHost::OnDestruct() const {
137 BrowserThread::DeleteOnIOThread::Destruct(this); 106 BrowserThread::DeleteOnIOThread::Destruct(this);
138 } 107 }
139 108
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 306
338 MediaObserver* const media_observer = 307 MediaObserver* const media_observer =
339 GetContentClient()->browser()->GetMediaObserver(); 308 GetContentClient()->browser()->GetMediaObserver();
340 309
341 MediaInternals* const media_internals = MediaInternals::GetInstance(); 310 MediaInternals* const media_internals = MediaInternals::GetInstance();
342 std::unique_ptr<media::AudioLog> audio_log = media_internals->CreateAudioLog( 311 std::unique_ptr<media::AudioLog> audio_log = media_internals->CreateAudioLog(
343 media::AudioLogFactory::AUDIO_OUTPUT_CONTROLLER); 312 media::AudioLogFactory::AUDIO_OUTPUT_CONTROLLER);
344 media_internals->SetWebContentsTitleForAudioLogEntry( 313 media_internals->SetWebContentsTitleForAudioLogEntry(
345 stream_id, render_process_id_, render_frame_id, audio_log.get()); 314 stream_id, render_process_id_, render_frame_id, audio_log.get());
346 delegates_.push_back(AudioOutputDelegate::Create( 315 delegates_.push_back(AudioOutputDelegate::Create(
347 this, audio_manager_, std::move(audio_log), mirroring_manager_, 316 this, stream_registry_, audio_manager_, std::move(audio_log),
348 media_observer, stream_id, render_frame_id, render_process_id_, params, 317 mirroring_manager_, media_observer, stream_id, render_frame_id,
349 device_unique_id)); 318 render_process_id_, params, device_unique_id));
350
351 g_audio_streams_tracker.Get().IncreaseStreamCount();
352
353 if (delegates_.size() > max_simultaneous_streams_)
354 max_simultaneous_streams_ = delegates_.size();
355 } 319 }
356 320
357 void AudioRendererHost::OnPlayStream(int stream_id) { 321 void AudioRendererHost::OnPlayStream(int stream_id) {
358 DCHECK_CURRENTLY_ON(BrowserThread::IO); 322 DCHECK_CURRENTLY_ON(BrowserThread::IO);
359 323
360 AudioOutputDelegate* delegate = LookupById(stream_id); 324 AudioOutputDelegate* delegate = LookupById(stream_id);
361 if (!delegate) { 325 if (!delegate) {
362 SendErrorMessage(stream_id); 326 SendErrorMessage(stream_id);
363 return; 327 return;
364 } 328 }
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 367
404 auto i = LookupIteratorById(stream_id); 368 auto i = LookupIteratorById(stream_id);
405 369
406 // Prevent oustanding callbacks from attempting to close/delete the same 370 // Prevent oustanding callbacks from attempting to close/delete the same
407 // AudioOutputDelegate twice. 371 // AudioOutputDelegate twice.
408 if (i == delegates_.end()) 372 if (i == delegates_.end())
409 return; 373 return;
410 374
411 std::swap(*i, delegates_.back()); 375 std::swap(*i, delegates_.back());
412 delegates_.pop_back(); 376 delegates_.pop_back();
413
414 g_audio_streams_tracker.Get().DecreaseStreamCount();
415 } 377 }
416 378
417 AudioRendererHost::AudioOutputDelegateVector::iterator 379 AudioRendererHost::AudioOutputDelegateVector::iterator
418 AudioRendererHost::LookupIteratorById(int stream_id) { 380 AudioRendererHost::LookupIteratorById(int stream_id) {
419 DCHECK_CURRENTLY_ON(BrowserThread::IO); 381 DCHECK_CURRENTLY_ON(BrowserThread::IO);
420 382
421 return std::find_if(delegates_.begin(), delegates_.end(), 383 return std::find_if(delegates_.begin(), delegates_.end(),
422 [stream_id](const AudioOutputDelegate::UniquePtr& d) { 384 [stream_id](const AudioOutputDelegate::UniquePtr& d) {
423 return d->stream_id() == stream_id; 385 return d->stream_id() == stream_id;
424 }); 386 });
425 } 387 }
426 388
427 AudioOutputDelegate* AudioRendererHost::LookupById(int stream_id) { 389 AudioOutputDelegate* AudioRendererHost::LookupById(int stream_id) {
428 DCHECK_CURRENTLY_ON(BrowserThread::IO); 390 DCHECK_CURRENTLY_ON(BrowserThread::IO);
429 391
430 auto i = LookupIteratorById(stream_id); 392 auto i = LookupIteratorById(stream_id);
431 return i != delegates_.end() ? i->get() : nullptr; 393 return i != delegates_.end() ? i->get() : nullptr;
432 } 394 }
433 395
434 void AudioRendererHost::OnStreamStateChanged(bool is_playing) {
435 DCHECK_CURRENTLY_ON(BrowserThread::IO);
436 if (is_playing) {
437 base::AtomicRefCountInc(&num_playing_streams_);
438
439 // Inform the RenderProcessHost when audio starts playing for the first
440 // time. The nonatomic increment-and-read is ok since this is the only
441 // thread that |num_plaing_streams_| may be updated on.
442 if (base::AtomicRefCountIsOne(&num_playing_streams_)) {
443 BrowserThread::PostTask(
444 BrowserThread::UI, FROM_HERE,
445 base::Bind(&NotifyRenderProcessHostThatAudioStateChanged,
446 render_process_id_));
447 }
448 } else {
449 // Inform the RenderProcessHost when there is no more audio playing.
450 if (!base::AtomicRefCountDec(&num_playing_streams_)) {
451 BrowserThread::PostTask(
452 BrowserThread::UI, FROM_HERE,
453 base::Bind(&NotifyRenderProcessHostThatAudioStateChanged,
454 render_process_id_));
455 }
456 }
457 }
458
459 bool AudioRendererHost::IsAuthorizationStarted(int stream_id) { 396 bool AudioRendererHost::IsAuthorizationStarted(int stream_id) {
460 DCHECK_CURRENTLY_ON(BrowserThread::IO); 397 DCHECK_CURRENTLY_ON(BrowserThread::IO);
461 return authorizations_.find(stream_id) != authorizations_.end(); 398 return authorizations_.find(stream_id) != authorizations_.end();
462 } 399 }
463 400
464 bool AudioRendererHost::HasActiveAudio() {
465 return !base::AtomicRefCountIsZero(&num_playing_streams_);
466 }
467
468 void AudioRendererHost::OverrideDevicePermissionsForTesting(bool has_access) { 401 void AudioRendererHost::OverrideDevicePermissionsForTesting(bool has_access) {
469 authorization_handler_.OverridePermissionsForTesting(has_access); 402 authorization_handler_.OverridePermissionsForTesting(has_access);
470 } 403 }
471 } // namespace content 404 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698