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

Unified Diff: content/browser/media/audio_stream_monitor.cc

Issue 478543003: Use AudioStreamMonitor to control power save blocking. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Move it! Created 6 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: content/browser/media/audio_stream_monitor.cc
diff --git a/content/browser/media/audio_stream_monitor.cc b/content/browser/media/audio_stream_monitor.cc
new file mode 100644
index 0000000000000000000000000000000000000000..7b13bc4adceb2b9462a9700841c51e193e9bbf7a
--- /dev/null
+++ b/content/browser/media/audio_stream_monitor.cc
@@ -0,0 +1,180 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
DaleCurtis 2014/08/29 23:56:47 Sadly codereview isn't detecting the diff here, bu
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/browser/media/audio_stream_monitor.h"
+
+#include "base/bind.h"
+#include "base/bind_helpers.h"
+#include "content/public/browser/browser_thread.h"
+#include "content/public/browser/invalidate_type.h"
+#include "content/public/browser/power_save_blocker.h"
+#include "content/public/browser/render_frame_host.h"
+#include "content/public/browser/web_contents.h"
+
+namespace content {
+
+namespace {
+
+AudioStreamMonitor* AudioStreamMonitorFromRenderFrame(int render_process_id,
+ int render_frame_id) {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ content::WebContents* const web_contents =
+ content::WebContents::FromRenderFrameHost(
+ content::RenderFrameHost::FromID(render_process_id, render_frame_id));
+ if (!web_contents)
+ return NULL;
+
+ // Note: Calling CreateForWebContents() multiple times is valid (see usage
+ // info for content::WebContentsUserData).
+ AudioStreamMonitor::CreateForWebContents(web_contents);
+ return AudioStreamMonitor::FromWebContents(web_contents);
+}
+
+} // namespace
+
+// 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
+// kLocatorKey is created as a local symbol without the CONTENT_EXPORT
+// addition... without it, nm shows:
+//
+// $ nm content_unittests.audio_stream_monitor_unittest.o | grep kLocatorKey
+// U _ZN7content19...AudioStreamMonitorEE11kLocatorKeyE
+// $ nm libcontent.so | grep AudioStreamMonitor | grep kLocatorkey
+// 000000000139d8f0 b _ZN7content19...AudioStreamMonitorEE11kLocatorKeyE
+//
+template <>
+CONTENT_EXPORT int
+ content::WebContentsUserData<AudioStreamMonitor>::kLocatorKey = 0;
+
+AudioStreamMonitor::AudioStreamMonitor(content::WebContents* contents)
+ : web_contents_(contents),
+ clock_(&default_tick_clock_),
+ was_recently_audible_(false) {
+ DCHECK(web_contents_);
+}
+
+AudioStreamMonitor::~AudioStreamMonitor() {}
+
+bool AudioStreamMonitor::WasRecentlyAudible() const {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ return was_recently_audible_;
+}
+
+/* static */
+void AudioStreamMonitor::StartMonitoringStream(
+ int render_process_id,
+ int render_frame_id,
+ int stream_id,
+ const ReadPowerAndClipCallback& read_power_callback) {
+ BrowserThread::PostTask(BrowserThread::UI,
+ FROM_HERE,
+ base::Bind(&StartMonitoringHelper,
+ render_process_id,
+ render_frame_id,
+ stream_id,
+ read_power_callback));
+}
+
+/* static */
+void AudioStreamMonitor::StopMonitoringStream(int render_process_id,
+ int render_frame_id,
+ int stream_id) {
+ BrowserThread::PostTask(BrowserThread::UI,
+ FROM_HERE,
+ base::Bind(&StopMonitoringHelper,
+ render_process_id,
+ render_frame_id,
+ stream_id));
+}
+
+/* static */
+void AudioStreamMonitor::StartMonitoringHelper(
+ int render_process_id,
+ int render_frame_id,
+ int stream_id,
+ const ReadPowerAndClipCallback& read_power_callback) {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ AudioStreamMonitor* const monitor =
+ AudioStreamMonitorFromRenderFrame(render_process_id, render_frame_id);
+ if (monitor)
+ monitor->StartMonitoringStreamOnUIThread(stream_id, read_power_callback);
+}
+
+/* static */
+void AudioStreamMonitor::StopMonitoringHelper(int render_process_id,
+ int render_frame_id,
+ int stream_id) {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ AudioStreamMonitor* const monitor =
+ AudioStreamMonitorFromRenderFrame(render_process_id, render_frame_id);
+ if (monitor)
+ monitor->StopMonitoringStreamOnUIThread(stream_id);
+}
+
+void AudioStreamMonitor::StartMonitoringStreamOnUIThread(
+ int stream_id,
+ const ReadPowerAndClipCallback& read_power_callback) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ DCHECK(!read_power_callback.is_null());
+ poll_callbacks_[stream_id] = read_power_callback;
+ if (!poll_timer_.IsRunning()) {
+ poll_timer_.Start(
+ FROM_HERE,
+ base::TimeDelta::FromSeconds(1) / kPowerMeasurementsPerSecond,
+ base::Bind(&AudioStreamMonitor::Poll, base::Unretained(this)));
+ }
+}
+
+void AudioStreamMonitor::StopMonitoringStreamOnUIThread(int stream_id) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ poll_callbacks_.erase(stream_id);
+ if (poll_callbacks_.empty())
+ poll_timer_.Stop();
+}
+
+void AudioStreamMonitor::Poll() {
+ for (StreamPollCallbackMap::const_iterator it = poll_callbacks_.begin();
+ it != poll_callbacks_.end();
+ ++it) {
+ // TODO(miu): A new UI for delivering specific power level and clipping
+ // information is still in the works. For now, we throw away all
+ // information except for "is it audible?"
+ const float power_dbfs = it->second.Run().first;
+ const float kSilenceThresholdDBFS = -72.24719896f;
+ if (power_dbfs >= kSilenceThresholdDBFS) {
+ last_blurt_time_ = clock_->NowTicks();
+ MaybeToggle();
+ break; // No need to poll remaining streams.
+ }
+ }
+}
+
+void AudioStreamMonitor::MaybeToggle() {
+ const bool indicator_was_on = was_recently_audible_;
+ const base::TimeTicks off_time =
+ last_blurt_time_ + base::TimeDelta::FromMilliseconds(kHoldOnMilliseconds);
+ const base::TimeTicks now = clock_->NowTicks();
+ const bool should_indicator_be_on = now < off_time;
+
+ if (should_indicator_be_on != indicator_was_on) {
+ was_recently_audible_ = should_indicator_be_on;
+ web_contents_->NotifyNavigationStateChanged(content::INVALIDATE_TYPE_TAB);
+ }
+
+ if (!should_indicator_be_on) {
+ off_timer_.Stop();
+ blocker_.reset();
+ } else if (!off_timer_.IsRunning()) {
+ if (!blocker_) {
+ blocker_ = content::PowerSaveBlocker::Create(
+ content::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension,
+ "Playing Audio");
+ }
+ off_timer_.Start(
+ FROM_HERE,
+ off_time - now,
+ base::Bind(&AudioStreamMonitor::MaybeToggle, base::Unretained(this)));
+ }
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698