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 CHROME_BROWSER_MEDIA_AUDIO_STREAM_MONITOR_H_ |
6 #define CONTENT_BROWSER_MEDIA_AUDIO_STREAM_MONITOR_H_ | 6 #define CHROME_BROWSER_MEDIA_AUDIO_STREAM_MONITOR_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <utility> | |
10 | 9 |
11 #include "base/callback_forward.h" | 10 #include "base/callback_forward.h" |
12 #include "base/threading/thread_checker.h" | 11 #include "base/threading/thread_checker.h" |
13 #include "base/time/default_tick_clock.h" | 12 #include "base/time/default_tick_clock.h" |
14 #include "base/time/time.h" | 13 #include "base/time/time.h" |
15 #include "base/timer/timer.h" | 14 #include "base/timer/timer.h" |
16 #include "build/build_config.h" | 15 #include "content/public/browser/web_contents_user_data.h" |
17 #include "content/common/content_export.h" | |
18 #include "media/audio/audio_output_controller.h" | |
19 | 16 |
20 namespace base { | 17 namespace base { |
21 class TickClock; | 18 class TickClock; |
22 } | 19 } |
23 | 20 |
24 namespace content { | |
25 class WebContents; | |
26 | |
27 // Repeatedly polls audio streams for their power levels, and "debounces" the | 21 // Repeatedly polls audio streams for their power levels, and "debounces" the |
28 // information into a simple, binary "was recently audible" result for the audio | 22 // 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 | 23 // 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 | 24 // 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 | 25 // 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 | 26 // 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. | 27 // update notifications only when needed, but may be queried at any time. |
34 // | 28 // |
35 // Each WebContentsImpl owns an AudioStreamMonitor. | 29 // There are zero or one instances of AudioStreamMonitor per |
36 class CONTENT_EXPORT AudioStreamMonitor { | 30 // content::WebContents instance (referred to as "the tab" in comments below). |
| 31 // AudioStreamMonitor is created on-demand, and automatically destroyed when its |
| 32 // associated WebContents is destroyed. See content::WebContentsUserData for |
| 33 // usage. |
| 34 class AudioStreamMonitor |
| 35 : public content::WebContentsUserData<AudioStreamMonitor> { |
37 public: | 36 public: |
38 explicit AudioStreamMonitor(WebContents* contents); | |
39 ~AudioStreamMonitor(); | |
40 | |
41 // Indicates if audio stream monitoring is available. It's only available if | |
42 // AudioOutputController can and will monitor output power levels. | |
43 static bool monitoring_available() { | |
44 return media::AudioOutputController::will_monitor_audio_levels(); | |
45 } | |
46 | |
47 // Returns true if audio has recently been audible from the tab. This is | 37 // 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 | 38 // usually called whenever the tab data model is refreshed; but there are |
49 // other use cases as well (e.g., the OOM killer uses this to de-prioritize | 39 // other use cases as well (e.g., the OOM killer uses this to de-prioritize |
50 // the killing of tabs making sounds). | 40 // the killing of tabs making sounds). |
51 bool WasRecentlyAudible() const; | 41 bool WasRecentlyAudible() const; |
52 | 42 |
53 // Starts or stops audio level monitoring respectively for the stream owned by | 43 // Starts polling the stream for audio stream power levels using |callback|. |
54 // the specified renderer. Safe to call from any thread. | |
55 // | |
56 // The callback returns the current power level (in dBFS units) and the clip | |
57 // status (true if any part of the audio signal has clipped since the last | |
58 // callback run). |stream_id| must be unique within a |render_process_id|. | |
59 typedef base::Callback<std::pair<float, bool>()> ReadPowerAndClipCallback; | 44 typedef base::Callback<std::pair<float, bool>()> ReadPowerAndClipCallback; |
60 static void StartMonitoringStream( | 45 void StartMonitoringStream(int stream_id, |
61 int render_process_id, | 46 const ReadPowerAndClipCallback& callback); |
62 int render_frame_id, | |
63 int stream_id, | |
64 const ReadPowerAndClipCallback& read_power_callback); | |
65 static void StopMonitoringStream(int render_process_id, | |
66 int render_frame_id, | |
67 int stream_id); | |
68 | 47 |
69 void set_was_recently_audible_for_testing(bool value) { | 48 // Stops polling the stream, discarding the internal copy of the |callback| |
70 was_recently_audible_ = value; | 49 // provided in the call to StartMonitoringStream(). |
71 } | 50 void StopMonitoringStream(int stream_id); |
72 | 51 |
73 private: | 52 private: |
| 53 friend class content::WebContentsUserData<AudioStreamMonitor>; |
74 friend class AudioStreamMonitorTest; | 54 friend class AudioStreamMonitorTest; |
75 | 55 |
76 enum { | 56 enum { |
77 // Desired polling frequency. Note: If this is set too low, short-duration | 57 // Desired polling frequency. Note: If this is set too low, short-duration |
78 // "blip" sounds won't be detected. http://crbug.com/339133#c4 | 58 // "blip" sounds won't be detected. http://crbug.com/339133#c4 |
79 kPowerMeasurementsPerSecond = 15, | 59 kPowerMeasurementsPerSecond = 15, |
80 | 60 |
81 // Amount of time to hold a tab indicator on after its last blurt. | 61 // Amount of time to hold a tab indicator on after its last blurt. |
82 kHoldOnMilliseconds = 2000 | 62 kHoldOnMilliseconds = 2000 |
83 }; | 63 }; |
84 | 64 |
85 // Helper methods for starting and stopping monitoring which lookup the | 65 // Invoked by content::WebContentsUserData only. |
86 // identified renderer and forward calls to the correct AudioStreamMonitor. | 66 explicit AudioStreamMonitor(content::WebContents* contents); |
87 static void StartMonitoringHelper( | 67 virtual ~AudioStreamMonitor(); |
88 int render_process_id, | |
89 int render_frame_id, | |
90 int stream_id, | |
91 const ReadPowerAndClipCallback& read_power_callback); | |
92 static void StopMonitoringHelper(int render_process_id, | |
93 int render_frame_id, | |
94 int stream_id); | |
95 | |
96 // Starts polling the stream for audio stream power levels using |callback|. | |
97 void StartMonitoringStreamOnUIThread( | |
98 int render_process_id, | |
99 int stream_id, | |
100 const ReadPowerAndClipCallback& callback); | |
101 | |
102 // Stops polling the stream, discarding the internal copy of the |callback| | |
103 // provided in the call to StartMonitoringStream(). | |
104 void StopMonitoringStreamOnUIThread(int render_process_id, int stream_id); | |
105 | 68 |
106 // Called by |poll_timer_| to sample the power levels from each of the streams | 69 // Called by |poll_timer_| to sample the power levels from each of the streams |
107 // playing in the tab. | 70 // playing in the tab. |
108 void Poll(); | 71 void Poll(); |
109 | 72 |
110 // Compares last known indicator state with what it should be, and triggers UI | 73 // 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 | 74 // 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. | 75 // on, |off_timer_| is started to re-invoke this method in the future. |
113 void MaybeToggle(); | 76 void MaybeToggle(); |
114 | 77 |
115 // The WebContents instance instance to receive indicator toggle | 78 // The WebContents instance instance to receive indicator toggle |
116 // notifications. This pointer should be valid for the lifetime of | 79 // notifications. This pointer should be valid for the lifetime of |
117 // AudioStreamMonitor. | 80 // AudioStreamMonitor. |
118 WebContents* const web_contents_; | 81 content::WebContents* const web_contents_; |
119 | 82 |
120 // Note: |clock_| is always |&default_tick_clock_|, except during unit | 83 // Note: |clock_| is always |&default_tick_clock_|, except during unit |
121 // testing. | 84 // testing. |
122 base::DefaultTickClock default_tick_clock_; | 85 base::DefaultTickClock default_tick_clock_; |
123 base::TickClock* const clock_; | 86 base::TickClock* const clock_; |
124 | 87 |
125 // Confirms single-threaded access in debug builds. | 88 // Confirms single-threaded access in debug builds. |
126 base::ThreadChecker thread_checker_; | 89 base::ThreadChecker thread_checker_; |
127 | 90 |
128 // The callbacks to read power levels for each stream. Only playing (i.e., | 91 // The callbacks to read power levels for each stream. Only playing (i.e., |
129 // not paused) streams will have an entry in this map. | 92 // not paused) streams will have an entry in this map. |
130 typedef std::pair<int, int> StreamID; | 93 typedef std::map<int, ReadPowerAndClipCallback> StreamPollCallbackMap; |
131 typedef std::map<StreamID, ReadPowerAndClipCallback> StreamPollCallbackMap; | |
132 StreamPollCallbackMap poll_callbacks_; | 94 StreamPollCallbackMap poll_callbacks_; |
133 | 95 |
134 // Records the last time at which sound was audible from any stream. | 96 // Records the last time at which sound was audible from any stream. |
135 base::TimeTicks last_blurt_time_; | 97 base::TimeTicks last_blurt_time_; |
136 | 98 |
137 // Set to true if the last call to MaybeToggle() determined the indicator | 99 // Set to true if the last call to MaybeToggle() determined the indicator |
138 // should be turned on. | 100 // should be turned on. |
139 bool was_recently_audible_; | 101 bool was_recently_audible_; |
140 | 102 |
141 // Calls Poll() at regular intervals while |poll_callbacks_| is non-empty. | 103 // Calls Poll() at regular intervals while |poll_callbacks_| is non-empty. |
142 base::RepeatingTimer<AudioStreamMonitor> poll_timer_; | 104 base::RepeatingTimer<AudioStreamMonitor> poll_timer_; |
143 | 105 |
144 // Started only when an indicator is toggled on, to turn it off again in the | 106 // Started only when an indicator is toggled on, to turn it off again in the |
145 // future. | 107 // future. |
146 base::OneShotTimer<AudioStreamMonitor> off_timer_; | 108 base::OneShotTimer<AudioStreamMonitor> off_timer_; |
147 | 109 |
148 DISALLOW_COPY_AND_ASSIGN(AudioStreamMonitor); | 110 DISALLOW_COPY_AND_ASSIGN(AudioStreamMonitor); |
149 }; | 111 }; |
150 | 112 |
151 } // namespace content | 113 #endif // CHROME_BROWSER_MEDIA_AUDIO_STREAM_MONITOR_H_ |
152 | |
153 #endif // CONTENT_BROWSER_MEDIA_AUDIO_STREAM_MONITOR_H_ | |
OLD | NEW |