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

Side by Side Diff: content/browser/media/audio_stream_monitor.h

Issue 757033005: Make tab audible and muted states and cause available for an extension API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 #ifndef CONTENT_BROWSER_MEDIA_AUDIO_STREAM_MONITOR_H_ 5 #ifndef CONTENT_BROWSER_MEDIA_AUDIO_STREAM_MONITOR_H_
6 #define CONTENT_BROWSER_MEDIA_AUDIO_STREAM_MONITOR_H_ 6 #define CONTENT_BROWSER_MEDIA_AUDIO_STREAM_MONITOR_H_
7 7
8 #include <map> 8 #include <map>
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/callback_forward.h" 11 #include "base/callback_forward.h"
12 #include "base/threading/thread_checker.h" 12 #include "base/threading/thread_checker.h"
13 #include "base/time/default_tick_clock.h" 13 #include "base/time/default_tick_clock.h"
14 #include "base/time/time.h" 14 #include "base/time/time.h"
15 #include "base/timer/timer.h" 15 #include "base/timer/timer.h"
16 #include "build/build_config.h" 16 #include "build/build_config.h"
17 #include "content/common/content_export.h" 17 #include "content/common/content_export.h"
18 #include "media/audio/audio_output_controller.h" 18 #include "media/audio/audio_output_controller.h"
19 19
20 namespace base { 20 namespace base {
21 class TickClock; 21 class TickClock;
22 } 22 }
23 23
24 namespace content { 24 namespace content {
25 class WebContents;
26 25
27 // Repeatedly polls audio streams for their power levels, and "debounces" the 26 // Repeatedly polls audio streams for their power levels, and "debounces" the
28 // information into a simple, binary "was recently audible" result for the audio 27 // information into a simple, binary "was recently audible" result for the audio
29 // indicators in the tab UI. The debouncing logic is to: 1) Turn on immediately 28 // indicators in the tab UI. The debouncing logic is to: 1) Turn on immediately
30 // when sound is audible; and 2) Hold on for X amount of time after sound has 29 // when sound is audible; and 2) Hold on for X amount of time after sound has
31 // gone silent, then turn off. Said another way, we don't want tab indicators 30 // gone silent, then turn off. Said another way, we don't want tab indicators
32 // to turn on/off repeatedly and annoy the user. AudioStreamMonitor sends UI 31 // to turn on/off repeatedly and annoy the user. AudioStreamMonitor sends UI
33 // update notifications only when needed, but may be queried at any time. 32 // update notifications only when needed, but may be queried at any time.
34 // 33 //
35 // Each WebContentsImpl owns an AudioStreamMonitor. 34 // Each WebContentsImpl owns an AudioStreamMonitor.
36 class CONTENT_EXPORT AudioStreamMonitor { 35 class CONTENT_EXPORT AudioStreamMonitor {
37 public: 36 public:
38 explicit AudioStreamMonitor(WebContents* contents); 37 using AudibleChangeCallback = base::Callback<void(bool)>;
38
39 explicit AudioStreamMonitor(const AudibleChangeCallback& callback);
39 ~AudioStreamMonitor(); 40 ~AudioStreamMonitor();
40 41
41 // Indicates if audio stream monitoring is available. It's only available if 42 // Indicates if audio stream monitoring is available. It's only available if
42 // AudioOutputController can and will monitor output power levels. 43 // AudioOutputController can and will monitor output power levels.
43 static bool monitoring_available() { 44 static bool monitoring_available() {
44 return media::AudioOutputController::will_monitor_audio_levels(); 45 return media::AudioOutputController::will_monitor_audio_levels();
45 } 46 }
46 47
47 // Returns true if audio has recently been audible from the tab. This is 48 // Returns true if audio has recently been audible from the tab. This is
48 // usually called whenever the tab data model is refreshed; but there are 49 // usually called whenever the tab data model is refreshed; but there are
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 106
106 // Called by |poll_timer_| to sample the power levels from each of the streams 107 // Called by |poll_timer_| to sample the power levels from each of the streams
107 // playing in the tab. 108 // playing in the tab.
108 void Poll(); 109 void Poll();
109 110
110 // Compares last known indicator state with what it should be, and triggers UI 111 // Compares last known indicator state with what it should be, and triggers UI
111 // updates through |web_contents_| if needed. When the indicator is turned 112 // updates through |web_contents_| if needed. When the indicator is turned
112 // on, |off_timer_| is started to re-invoke this method in the future. 113 // on, |off_timer_| is started to re-invoke this method in the future.
113 void MaybeToggle(); 114 void MaybeToggle();
114 115
115 // The WebContents instance instance to receive indicator toggle 116 // The Callback to run to receive indicator toggle notifications.
116 // notifications. This pointer should be valid for the lifetime of 117 const AudibleChangeCallback audible_change_callback_;
117 // AudioStreamMonitor.
118 WebContents* const web_contents_;
119 118
120 // Note: |clock_| is always |&default_tick_clock_|, except during unit 119 // Note: |clock_| is always |&default_tick_clock_|, except during unit
121 // testing. 120 // testing.
122 base::DefaultTickClock default_tick_clock_; 121 base::DefaultTickClock default_tick_clock_;
123 base::TickClock* const clock_; 122 base::TickClock* const clock_;
124 123
125 // Confirms single-threaded access in debug builds. 124 // Confirms single-threaded access in debug builds.
126 base::ThreadChecker thread_checker_; 125 base::ThreadChecker thread_checker_;
127 126
128 // The callbacks to read power levels for each stream. Only playing (i.e., 127 // The callbacks to read power levels for each stream. Only playing (i.e.,
(...skipping 15 matching lines...) Expand all
144 // Started only when an indicator is toggled on, to turn it off again in the 143 // Started only when an indicator is toggled on, to turn it off again in the
145 // future. 144 // future.
146 base::OneShotTimer<AudioStreamMonitor> off_timer_; 145 base::OneShotTimer<AudioStreamMonitor> off_timer_;
147 146
148 DISALLOW_COPY_AND_ASSIGN(AudioStreamMonitor); 147 DISALLOW_COPY_AND_ASSIGN(AudioStreamMonitor);
149 }; 148 };
150 149
151 } // namespace content 150 } // namespace content
152 151
153 #endif // CONTENT_BROWSER_MEDIA_AUDIO_STREAM_MONITOR_H_ 152 #endif // CONTENT_BROWSER_MEDIA_AUDIO_STREAM_MONITOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698