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

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

Issue 2655413004: Strip out stream counting from AudioRendererHost. (Closed)
Patch Set: Created 3 years, 10 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 (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_sync_reader.h" 19 #include "content/browser/renderer_host/media/audio_sync_reader.h"
20 #include "content/browser/renderer_host/media/media_stream_manager.h" 20 #include "content/browser/renderer_host/media/media_stream_manager.h"
21 #include "content/common/media/audio_messages.h" 21 #include "content/common/media/audio_messages.h"
22 #include "content/public/browser/content_browser_client.h" 22 #include "content/public/browser/content_browser_client.h"
23 #include "content/public/browser/media_device_id.h" 23 #include "content/public/browser/media_device_id.h"
24 #include "content/public/browser/media_observer.h" 24 #include "content/public/browser/media_observer.h"
25 #include "content/public/browser/render_frame_host.h" 25 #include "content/public/browser/render_frame_host.h"
26 #include "media/audio/audio_device_description.h" 26 #include "media/audio/audio_device_description.h"
27 #include "media/audio/audio_streams_tracker.h"
28 #include "media/base/audio_bus.h" 27 #include "media/base/audio_bus.h"
29 #include "media/base/limits.h" 28 #include "media/base/limits.h"
30 29
31 using media::AudioBus; 30 using media::AudioBus;
32 using media::AudioManager; 31 using media::AudioManager;
33 32
34 namespace content { 33 namespace content {
35 34
36 namespace { 35 namespace {
37 36
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) { 37 void UMALogDeviceAuthorizationTime(base::TimeTicks auth_start_time) {
54 UMA_HISTOGRAM_CUSTOM_TIMES("Media.Audio.OutputDeviceAuthorizationTime", 38 UMA_HISTOGRAM_CUSTOM_TIMES("Media.Audio.OutputDeviceAuthorizationTime",
55 base::TimeTicks::Now() - auth_start_time, 39 base::TimeTicks::Now() - auth_start_time,
56 base::TimeDelta::FromMilliseconds(1), 40 base::TimeDelta::FromMilliseconds(1),
57 base::TimeDelta::FromMilliseconds(5000), 50); 41 base::TimeDelta::FromMilliseconds(5000), 50);
58 } 42 }
59 43
60 // Check that the routing ID references a valid RenderFrameHost, and run 44 // Check that the routing ID references a valid RenderFrameHost, and run
61 // |callback| on the IO thread with true if the ID is valid. 45 // |callback| on the IO thread with true if the ID is valid.
62 void ValidateRenderFrameId(int render_process_id, 46 void ValidateRenderFrameId(int render_process_id,
(...skipping 14 matching lines...) Expand all
77 AudioRendererHost::AudioRendererHost(int render_process_id, 61 AudioRendererHost::AudioRendererHost(int render_process_id,
78 media::AudioManager* audio_manager, 62 media::AudioManager* audio_manager,
79 AudioMirroringManager* mirroring_manager, 63 AudioMirroringManager* mirroring_manager,
80 MediaStreamManager* media_stream_manager, 64 MediaStreamManager* media_stream_manager,
81 const std::string& salt) 65 const std::string& salt)
82 : BrowserMessageFilter(AudioMsgStart), 66 : BrowserMessageFilter(AudioMsgStart),
83 render_process_id_(render_process_id), 67 render_process_id_(render_process_id),
84 audio_manager_(audio_manager), 68 audio_manager_(audio_manager),
85 mirroring_manager_(mirroring_manager), 69 mirroring_manager_(mirroring_manager),
86 media_stream_manager_(media_stream_manager), 70 media_stream_manager_(media_stream_manager),
87 num_playing_streams_(0),
88 salt_(salt), 71 salt_(salt),
89 validate_render_frame_id_function_(&ValidateRenderFrameId), 72 validate_render_frame_id_function_(&ValidateRenderFrameId),
90 max_simultaneous_streams_(0),
91 authorization_handler_(audio_manager_, 73 authorization_handler_(audio_manager_,
92 media_stream_manager, 74 media_stream_manager,
93 render_process_id_, 75 render_process_id_,
94 salt) { 76 salt) {
95 DCHECK(audio_manager_); 77 DCHECK(audio_manager_);
96 } 78 }
97 79
98 AudioRendererHost::~AudioRendererHost() { 80 AudioRendererHost::~AudioRendererHost() {
99 DCHECK_CURRENTLY_ON(BrowserThread::IO); 81 DCHECK_CURRENTLY_ON(BrowserThread::IO);
100 DCHECK(delegates_.empty()); 82 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 } 83 }
115 84
116 void AudioRendererHost::GetOutputControllers( 85 void AudioRendererHost::GetOutputControllers(
117 const RenderProcessHost::GetAudioOutputControllersCallback& 86 const RenderProcessHost::GetAudioOutputControllersCallback&
118 callback) const { 87 callback) const {
119 BrowserThread::PostTaskAndReplyWithResult( 88 BrowserThread::PostTaskAndReplyWithResult(
120 BrowserThread::IO, FROM_HERE, 89 BrowserThread::IO, FROM_HERE,
121 base::Bind(&AudioRendererHost::DoGetOutputControllers, this), callback); 90 base::Bind(&AudioRendererHost::DoGetOutputControllers, this), callback);
122 } 91 }
123 92
124 void AudioRendererHost::OnChannelClosing() { 93 void AudioRendererHost::OnChannelClosing() {
125 DCHECK_CURRENTLY_ON(BrowserThread::IO); 94 DCHECK_CURRENTLY_ON(BrowserThread::IO);
126 // 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(); 95 delegates_.clear();
131 96
132 // Remove any authorizations for streams that were not yet created 97 // Remove any authorizations for streams that were not yet created
133 authorizations_.clear(); 98 authorizations_.clear();
134 } 99 }
135 100
136 void AudioRendererHost::OnDestruct() const { 101 void AudioRendererHost::OnDestruct() const {
137 BrowserThread::DeleteOnIOThread::Destruct(this); 102 BrowserThread::DeleteOnIOThread::Destruct(this);
138 } 103 }
139 104
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 305
341 MediaInternals* const media_internals = MediaInternals::GetInstance(); 306 MediaInternals* const media_internals = MediaInternals::GetInstance();
342 std::unique_ptr<media::AudioLog> audio_log = media_internals->CreateAudioLog( 307 std::unique_ptr<media::AudioLog> audio_log = media_internals->CreateAudioLog(
343 media::AudioLogFactory::AUDIO_OUTPUT_CONTROLLER); 308 media::AudioLogFactory::AUDIO_OUTPUT_CONTROLLER);
344 media_internals->SetWebContentsTitleForAudioLogEntry( 309 media_internals->SetWebContentsTitleForAudioLogEntry(
345 stream_id, render_process_id_, render_frame_id, audio_log.get()); 310 stream_id, render_process_id_, render_frame_id, audio_log.get());
346 delegates_.push_back(AudioOutputDelegate::Create( 311 delegates_.push_back(AudioOutputDelegate::Create(
347 this, audio_manager_, std::move(audio_log), mirroring_manager_, 312 this, audio_manager_, std::move(audio_log), mirroring_manager_,
348 media_observer, stream_id, render_frame_id, render_process_id_, params, 313 media_observer, stream_id, render_frame_id, render_process_id_, params,
349 device_unique_id)); 314 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 } 315 }
356 316
357 void AudioRendererHost::OnPlayStream(int stream_id) { 317 void AudioRendererHost::OnPlayStream(int stream_id) {
358 DCHECK_CURRENTLY_ON(BrowserThread::IO); 318 DCHECK_CURRENTLY_ON(BrowserThread::IO);
359 319
360 AudioOutputDelegate* delegate = LookupById(stream_id); 320 AudioOutputDelegate* delegate = LookupById(stream_id);
361 if (!delegate) { 321 if (!delegate) {
362 SendErrorMessage(stream_id); 322 SendErrorMessage(stream_id);
363 return; 323 return;
364 } 324 }
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 363
404 auto i = LookupIteratorById(stream_id); 364 auto i = LookupIteratorById(stream_id);
405 365
406 // Prevent oustanding callbacks from attempting to close/delete the same 366 // Prevent oustanding callbacks from attempting to close/delete the same
407 // AudioOutputDelegate twice. 367 // AudioOutputDelegate twice.
408 if (i == delegates_.end()) 368 if (i == delegates_.end())
409 return; 369 return;
410 370
411 std::swap(*i, delegates_.back()); 371 std::swap(*i, delegates_.back());
412 delegates_.pop_back(); 372 delegates_.pop_back();
413
414 g_audio_streams_tracker.Get().DecreaseStreamCount();
415 } 373 }
416 374
417 AudioRendererHost::AudioOutputDelegateVector::iterator 375 AudioRendererHost::AudioOutputDelegateVector::iterator
418 AudioRendererHost::LookupIteratorById(int stream_id) { 376 AudioRendererHost::LookupIteratorById(int stream_id) {
419 DCHECK_CURRENTLY_ON(BrowserThread::IO); 377 DCHECK_CURRENTLY_ON(BrowserThread::IO);
420 378
421 return std::find_if(delegates_.begin(), delegates_.end(), 379 return std::find_if(delegates_.begin(), delegates_.end(),
422 [stream_id](const AudioOutputDelegate::UniquePtr& d) { 380 [stream_id](const AudioOutputDelegate::UniquePtr& d) {
423 return d->stream_id() == stream_id; 381 return d->stream_id() == stream_id;
424 }); 382 });
425 } 383 }
426 384
427 AudioOutputDelegate* AudioRendererHost::LookupById(int stream_id) { 385 AudioOutputDelegate* AudioRendererHost::LookupById(int stream_id) {
428 DCHECK_CURRENTLY_ON(BrowserThread::IO); 386 DCHECK_CURRENTLY_ON(BrowserThread::IO);
429 387
430 auto i = LookupIteratorById(stream_id); 388 auto i = LookupIteratorById(stream_id);
431 return i != delegates_.end() ? i->get() : nullptr; 389 return i != delegates_.end() ? i->get() : nullptr;
432 } 390 }
433 391
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) { 392 bool AudioRendererHost::IsAuthorizationStarted(int stream_id) {
460 DCHECK_CURRENTLY_ON(BrowserThread::IO); 393 DCHECK_CURRENTLY_ON(BrowserThread::IO);
461 return authorizations_.find(stream_id) != authorizations_.end(); 394 return authorizations_.find(stream_id) != authorizations_.end();
462 } 395 }
463 396
464 bool AudioRendererHost::HasActiveAudio() {
465 return !base::AtomicRefCountIsZero(&num_playing_streams_);
466 }
467
468 void AudioRendererHost::OverrideDevicePermissionsForTesting(bool has_access) { 397 void AudioRendererHost::OverrideDevicePermissionsForTesting(bool has_access) {
469 authorization_handler_.OverridePermissionsForTesting(has_access); 398 authorization_handler_.OverridePermissionsForTesting(has_access);
470 } 399 }
471 } // namespace content 400 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698