Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
|
DaleCurtis
2014/08/29 23:56:47
Sadly codereview isn't detecting the diff here, bu
| |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/media/audio_stream_monitor.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "content/public/browser/browser_thread.h" | |
| 10 #include "content/public/browser/invalidate_type.h" | |
| 11 #include "content/public/browser/power_save_blocker.h" | |
| 12 #include "content/public/browser/render_frame_host.h" | |
| 13 #include "content/public/browser/web_contents.h" | |
| 14 | |
| 15 namespace content { | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 AudioStreamMonitor* AudioStreamMonitorFromRenderFrame(int render_process_id, | |
| 20 int render_frame_id) { | |
| 21 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 22 content::WebContents* const web_contents = | |
| 23 content::WebContents::FromRenderFrameHost( | |
| 24 content::RenderFrameHost::FromID(render_process_id, render_frame_id)); | |
| 25 if (!web_contents) | |
| 26 return NULL; | |
| 27 | |
| 28 // Note: Calling CreateForWebContents() multiple times is valid (see usage | |
| 29 // info for content::WebContentsUserData). | |
| 30 AudioStreamMonitor::CreateForWebContents(web_contents); | |
| 31 return AudioStreamMonitor::FromWebContents(web_contents); | |
| 32 } | |
| 33 | |
| 34 } // namespace | |
| 35 | |
| 36 // TODO(dalecurtis): ???? DEFINE_WEB_CONTENTS_USER_DATA_KEY won't link, | |
|
DaleCurtis
2014/08/29 23:56:47
Not sure what to do about this. Sadly there are n
| |
| 37 // kLocatorKey is created as a local symbol without the CONTENT_EXPORT | |
| 38 // addition... without it, nm shows: | |
| 39 // | |
| 40 // $ nm content_unittests.audio_stream_monitor_unittest.o | grep kLocatorKey | |
| 41 // U _ZN7content19...AudioStreamMonitorEE11kLocatorKeyE | |
| 42 // $ nm libcontent.so | grep AudioStreamMonitor | grep kLocatorkey | |
| 43 // 000000000139d8f0 b _ZN7content19...AudioStreamMonitorEE11kLocatorKeyE | |
| 44 // | |
| 45 template <> | |
| 46 CONTENT_EXPORT int | |
| 47 content::WebContentsUserData<AudioStreamMonitor>::kLocatorKey = 0; | |
| 48 | |
| 49 AudioStreamMonitor::AudioStreamMonitor(content::WebContents* contents) | |
| 50 : web_contents_(contents), | |
| 51 clock_(&default_tick_clock_), | |
| 52 was_recently_audible_(false) { | |
| 53 DCHECK(web_contents_); | |
| 54 } | |
| 55 | |
| 56 AudioStreamMonitor::~AudioStreamMonitor() {} | |
| 57 | |
| 58 bool AudioStreamMonitor::WasRecentlyAudible() const { | |
| 59 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 60 return was_recently_audible_; | |
| 61 } | |
| 62 | |
| 63 /* static */ | |
| 64 void AudioStreamMonitor::StartMonitoringStream( | |
| 65 int render_process_id, | |
| 66 int render_frame_id, | |
| 67 int stream_id, | |
| 68 const ReadPowerAndClipCallback& read_power_callback) { | |
| 69 BrowserThread::PostTask(BrowserThread::UI, | |
| 70 FROM_HERE, | |
| 71 base::Bind(&StartMonitoringHelper, | |
| 72 render_process_id, | |
| 73 render_frame_id, | |
| 74 stream_id, | |
| 75 read_power_callback)); | |
| 76 } | |
| 77 | |
| 78 /* static */ | |
| 79 void AudioStreamMonitor::StopMonitoringStream(int render_process_id, | |
| 80 int render_frame_id, | |
| 81 int stream_id) { | |
| 82 BrowserThread::PostTask(BrowserThread::UI, | |
| 83 FROM_HERE, | |
| 84 base::Bind(&StopMonitoringHelper, | |
| 85 render_process_id, | |
| 86 render_frame_id, | |
| 87 stream_id)); | |
| 88 } | |
| 89 | |
| 90 /* static */ | |
| 91 void AudioStreamMonitor::StartMonitoringHelper( | |
| 92 int render_process_id, | |
| 93 int render_frame_id, | |
| 94 int stream_id, | |
| 95 const ReadPowerAndClipCallback& read_power_callback) { | |
| 96 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 97 AudioStreamMonitor* const monitor = | |
| 98 AudioStreamMonitorFromRenderFrame(render_process_id, render_frame_id); | |
| 99 if (monitor) | |
| 100 monitor->StartMonitoringStreamOnUIThread(stream_id, read_power_callback); | |
| 101 } | |
| 102 | |
| 103 /* static */ | |
| 104 void AudioStreamMonitor::StopMonitoringHelper(int render_process_id, | |
| 105 int render_frame_id, | |
| 106 int stream_id) { | |
| 107 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 108 AudioStreamMonitor* const monitor = | |
| 109 AudioStreamMonitorFromRenderFrame(render_process_id, render_frame_id); | |
| 110 if (monitor) | |
| 111 monitor->StopMonitoringStreamOnUIThread(stream_id); | |
| 112 } | |
| 113 | |
| 114 void AudioStreamMonitor::StartMonitoringStreamOnUIThread( | |
| 115 int stream_id, | |
| 116 const ReadPowerAndClipCallback& read_power_callback) { | |
| 117 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 118 DCHECK(!read_power_callback.is_null()); | |
| 119 poll_callbacks_[stream_id] = read_power_callback; | |
| 120 if (!poll_timer_.IsRunning()) { | |
| 121 poll_timer_.Start( | |
| 122 FROM_HERE, | |
| 123 base::TimeDelta::FromSeconds(1) / kPowerMeasurementsPerSecond, | |
| 124 base::Bind(&AudioStreamMonitor::Poll, base::Unretained(this))); | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 void AudioStreamMonitor::StopMonitoringStreamOnUIThread(int stream_id) { | |
| 129 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 130 poll_callbacks_.erase(stream_id); | |
| 131 if (poll_callbacks_.empty()) | |
| 132 poll_timer_.Stop(); | |
| 133 } | |
| 134 | |
| 135 void AudioStreamMonitor::Poll() { | |
| 136 for (StreamPollCallbackMap::const_iterator it = poll_callbacks_.begin(); | |
| 137 it != poll_callbacks_.end(); | |
| 138 ++it) { | |
| 139 // TODO(miu): A new UI for delivering specific power level and clipping | |
| 140 // information is still in the works. For now, we throw away all | |
| 141 // information except for "is it audible?" | |
| 142 const float power_dbfs = it->second.Run().first; | |
| 143 const float kSilenceThresholdDBFS = -72.24719896f; | |
| 144 if (power_dbfs >= kSilenceThresholdDBFS) { | |
| 145 last_blurt_time_ = clock_->NowTicks(); | |
| 146 MaybeToggle(); | |
| 147 break; // No need to poll remaining streams. | |
| 148 } | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 void AudioStreamMonitor::MaybeToggle() { | |
| 153 const bool indicator_was_on = was_recently_audible_; | |
| 154 const base::TimeTicks off_time = | |
| 155 last_blurt_time_ + base::TimeDelta::FromMilliseconds(kHoldOnMilliseconds); | |
| 156 const base::TimeTicks now = clock_->NowTicks(); | |
| 157 const bool should_indicator_be_on = now < off_time; | |
| 158 | |
| 159 if (should_indicator_be_on != indicator_was_on) { | |
| 160 was_recently_audible_ = should_indicator_be_on; | |
| 161 web_contents_->NotifyNavigationStateChanged(content::INVALIDATE_TYPE_TAB); | |
| 162 } | |
| 163 | |
| 164 if (!should_indicator_be_on) { | |
| 165 off_timer_.Stop(); | |
| 166 blocker_.reset(); | |
| 167 } else if (!off_timer_.IsRunning()) { | |
| 168 if (!blocker_) { | |
| 169 blocker_ = content::PowerSaveBlocker::Create( | |
| 170 content::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, | |
| 171 "Playing Audio"); | |
| 172 } | |
| 173 off_timer_.Start( | |
| 174 FROM_HERE, | |
| 175 off_time - now, | |
| 176 base::Bind(&AudioStreamMonitor::MaybeToggle, base::Unretained(this))); | |
| 177 } | |
| 178 } | |
| 179 | |
| 180 } // namespace content | |
| OLD | NEW |