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