OLD | NEW |
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 |
(...skipping 16 matching lines...) Expand all Loading... |
27 class WebContents; | 27 class WebContents; |
28 | 28 |
29 // Repeatedly polls audio streams for their power levels, and "debounces" the | 29 // Repeatedly polls audio streams for their power levels, and "debounces" the |
30 // information into a simple, binary "was recently audible" result for the audio | 30 // information into a simple, binary "was recently audible" result for the audio |
31 // indicators in the tab UI. The debouncing logic is to: 1) Turn on immediately | 31 // indicators in the tab UI. The debouncing logic is to: 1) Turn on immediately |
32 // when sound is audible; and 2) Hold on for X amount of time after sound has | 32 // when sound is audible; and 2) Hold on for X amount of time after sound has |
33 // gone silent, then turn off. Said another way, we don't want tab indicators | 33 // gone silent, then turn off. Said another way, we don't want tab indicators |
34 // to turn on/off repeatedly and annoy the user. AudioStreamMonitor sends UI | 34 // to turn on/off repeatedly and annoy the user. AudioStreamMonitor sends UI |
35 // update notifications only when needed, but may be queried at any time. | 35 // update notifications only when needed, but may be queried at any time. |
36 // | 36 // |
| 37 // When power level monitoring is not available, audibility is approximated |
| 38 // with having active audio streams. |
| 39 // |
37 // Each WebContentsImpl owns an AudioStreamMonitor. | 40 // Each WebContentsImpl owns an AudioStreamMonitor. |
38 class CONTENT_EXPORT AudioStreamMonitor { | 41 class CONTENT_EXPORT AudioStreamMonitor { |
39 public: | 42 public: |
40 explicit AudioStreamMonitor(WebContents* contents); | 43 explicit AudioStreamMonitor(WebContents* contents); |
41 ~AudioStreamMonitor(); | 44 ~AudioStreamMonitor(); |
42 | 45 |
43 // Indicates if audio stream monitoring is available. It's only available if | |
44 // AudioOutputController can and will monitor output power levels. | |
45 static bool monitoring_available() { | |
46 return media::AudioOutputController::will_monitor_audio_levels(); | |
47 } | |
48 | |
49 // Returns true if audio has recently been audible from the tab. This is | 46 // Returns true if audio has recently been audible from the tab. This is |
50 // usually called whenever the tab data model is refreshed; but there are | 47 // usually called whenever the tab data model is refreshed; but there are |
51 // other use cases as well (e.g., the OOM killer uses this to de-prioritize | 48 // other use cases as well (e.g., the OOM killer uses this to de-prioritize |
52 // the killing of tabs making sounds). | 49 // the killing of tabs making sounds). |
53 bool WasRecentlyAudible() const; | 50 bool WasRecentlyAudible() const; |
54 | 51 |
55 // Returns true if the audio is currently audible from the given WebContents. | 52 // Returns true if the audio is currently audible from the given WebContents. |
56 // The difference from WasRecentlyAudible() is that this method will return | 53 // The difference from WasRecentlyAudible() is that this method will return |
57 // false as soon as the WebContents stop producing sound. | 54 // false as soon as the WebContents stop producing sound. |
58 bool IsCurrentlyAudible() const; | 55 bool IsCurrentlyAudible() const; |
(...skipping 23 matching lines...) Expand all Loading... |
82 | 79 |
83 enum { | 80 enum { |
84 // Desired polling frequency. Note: If this is set too low, short-duration | 81 // Desired polling frequency. Note: If this is set too low, short-duration |
85 // "blip" sounds won't be detected. http://crbug.com/339133#c4 | 82 // "blip" sounds won't be detected. http://crbug.com/339133#c4 |
86 kPowerMeasurementsPerSecond = 15, | 83 kPowerMeasurementsPerSecond = 15, |
87 | 84 |
88 // Amount of time to hold a tab indicator on after its last blurt. | 85 // Amount of time to hold a tab indicator on after its last blurt. |
89 kHoldOnMilliseconds = 2000 | 86 kHoldOnMilliseconds = 2000 |
90 }; | 87 }; |
91 | 88 |
| 89 // Indicates if monitoring of audio stream power level is available. |
| 90 // It's only available if AudioOutputController can and will monitor |
| 91 // output power levels. |
| 92 static bool power_level_monitoring_available() { |
| 93 return media::AudioOutputController::will_monitor_audio_levels(); |
| 94 } |
| 95 |
92 // Helper methods for starting and stopping monitoring which lookup the | 96 // Helper methods for starting and stopping monitoring which lookup the |
93 // identified renderer and forward calls to the correct AudioStreamMonitor. | 97 // identified renderer and forward calls to the correct AudioStreamMonitor. |
94 static void StartMonitoringHelper( | 98 static void StartMonitoringHelper( |
95 int render_process_id, | 99 int render_process_id, |
96 int render_frame_id, | 100 int render_frame_id, |
97 int stream_id, | 101 int stream_id, |
98 const ReadPowerAndClipCallback& read_power_callback); | 102 const ReadPowerAndClipCallback& read_power_callback); |
99 static void StopMonitoringHelper(int render_process_id, | 103 static void StopMonitoringHelper(int render_process_id, |
100 int render_frame_id, | 104 int render_frame_id, |
101 int stream_id); | 105 int stream_id); |
(...skipping 10 matching lines...) Expand all Loading... |
112 | 116 |
113 // Called by |poll_timer_| to sample the power levels from each of the streams | 117 // Called by |poll_timer_| to sample the power levels from each of the streams |
114 // playing in the tab. | 118 // playing in the tab. |
115 void Poll(); | 119 void Poll(); |
116 | 120 |
117 // Compares last known indicator state with what it should be, and triggers UI | 121 // Compares last known indicator state with what it should be, and triggers UI |
118 // updates through |web_contents_| if needed. When the indicator is turned | 122 // updates through |web_contents_| if needed. When the indicator is turned |
119 // on, |off_timer_| is started to re-invoke this method in the future. | 123 // on, |off_timer_| is started to re-invoke this method in the future. |
120 void MaybeToggle(); | 124 void MaybeToggle(); |
121 | 125 |
| 126 // Helper functions to track number of active streams when power level |
| 127 // monitoring is not available. |
| 128 void OnStreamAdded(); |
| 129 void OnStreamRemoved(); |
| 130 |
122 // The WebContents instance to receive indicator toggle notifications. This | 131 // The WebContents instance to receive indicator toggle notifications. This |
123 // pointer should be valid for the lifetime of AudioStreamMonitor. | 132 // pointer should be valid for the lifetime of AudioStreamMonitor. |
124 WebContents* const web_contents_; | 133 WebContents* const web_contents_; |
125 | 134 |
126 // Note: |clock_| is always |&default_tick_clock_|, except during unit | 135 // Note: |clock_| is always |&default_tick_clock_|, except during unit |
127 // testing. | 136 // testing. |
128 base::DefaultTickClock default_tick_clock_; | 137 base::DefaultTickClock default_tick_clock_; |
129 base::TickClock* const clock_; | 138 base::TickClock* const clock_; |
130 | 139 |
131 // Confirms single-threaded access in debug builds. | 140 // Confirms single-threaded access in debug builds. |
(...skipping 15 matching lines...) Expand all Loading... |
147 // Whether the WebContents is currently audible. | 156 // Whether the WebContents is currently audible. |
148 bool is_audible_; | 157 bool is_audible_; |
149 | 158 |
150 // Calls Poll() at regular intervals while |poll_callbacks_| is non-empty. | 159 // Calls Poll() at regular intervals while |poll_callbacks_| is non-empty. |
151 base::RepeatingTimer poll_timer_; | 160 base::RepeatingTimer poll_timer_; |
152 | 161 |
153 // Started only when an indicator is toggled on, to turn it off again in the | 162 // Started only when an indicator is toggled on, to turn it off again in the |
154 // future. | 163 // future. |
155 base::OneShotTimer off_timer_; | 164 base::OneShotTimer off_timer_; |
156 | 165 |
| 166 // Number of active streams to be used as a proxy for audibility when power |
| 167 // level monitoring is not available. |
| 168 size_t active_streams_; |
| 169 |
157 DISALLOW_COPY_AND_ASSIGN(AudioStreamMonitor); | 170 DISALLOW_COPY_AND_ASSIGN(AudioStreamMonitor); |
158 }; | 171 }; |
159 | 172 |
160 } // namespace content | 173 } // namespace content |
161 | 174 |
162 #endif // CONTENT_BROWSER_MEDIA_AUDIO_STREAM_MONITOR_H_ | 175 #endif // CONTENT_BROWSER_MEDIA_AUDIO_STREAM_MONITOR_H_ |
OLD | NEW |