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 |
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/browser/media/audio_state_provider.h" | |
18 #include "content/common/content_export.h" | 17 #include "content/common/content_export.h" |
19 #include "media/audio/audio_output_controller.h" | 18 #include "media/audio/audio_output_controller.h" |
20 | 19 |
21 namespace base { | 20 namespace base { |
22 class TickClock; | 21 class TickClock; |
23 } | 22 } |
24 | 23 |
25 namespace content { | 24 namespace content { |
26 | 25 |
| 26 class WebContents; |
| 27 |
27 // Repeatedly polls audio streams for their power levels, and "debounces" the | 28 // Repeatedly polls audio streams for their power levels, and "debounces" the |
28 // information into a simple, binary "was recently audible" result for the audio | 29 // 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 | 30 // 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 | 31 // 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 | 32 // 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 | 33 // 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. | 34 // update notifications only when needed, but may be queried at any time. |
34 // | 35 // |
35 class CONTENT_EXPORT AudioStreamMonitor : public AudioStateProvider { | 36 // Each WebContentsImpl owns an AudioStreamMonitor. |
| 37 class CONTENT_EXPORT AudioStreamMonitor { |
36 public: | 38 public: |
37 explicit AudioStreamMonitor(WebContents* contents); | 39 explicit AudioStreamMonitor(WebContents* contents); |
38 ~AudioStreamMonitor() override; | 40 ~AudioStreamMonitor(); |
39 | 41 |
40 // 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 |
41 // AudioOutputController can and will monitor output power levels. | 43 // AudioOutputController can and will monitor output power levels. |
42 bool IsAudioStateAvailable() const override; | 44 static bool monitoring_available() { |
43 | 45 return media::AudioOutputController::will_monitor_audio_levels(); |
44 // This provider is a monitor, the method returns |this|. | 46 } |
45 AudioStreamMonitor* audio_stream_monitor() override; | |
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 |
49 // other use cases as well (e.g., the OOM killer uses this to de-prioritize | 50 // other use cases as well (e.g., the OOM killer uses this to de-prioritize |
50 // the killing of tabs making sounds). | 51 // the killing of tabs making sounds). |
51 bool WasRecentlyAudible() const override; | 52 bool WasRecentlyAudible() const; |
52 | 53 |
53 // Starts or stops audio level monitoring respectively for the stream owned by | 54 // Starts or stops audio level monitoring respectively for the stream owned by |
54 // the specified renderer. Safe to call from any thread. | 55 // the specified renderer. Safe to call from any thread. |
55 // | 56 // |
56 // The callback returns the current power level (in dBFS units) and the clip | 57 // 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 // 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 // callback run). |stream_id| must be unique within a |render_process_id|. |
59 typedef base::Callback<std::pair<float, bool>()> ReadPowerAndClipCallback; | 60 typedef base::Callback<std::pair<float, bool>()> ReadPowerAndClipCallback; |
60 static void StartMonitoringStream( | 61 static void StartMonitoringStream( |
61 int render_process_id, | 62 int render_process_id, |
62 int render_frame_id, | 63 int render_frame_id, |
63 int stream_id, | 64 int stream_id, |
64 const ReadPowerAndClipCallback& read_power_callback); | 65 const ReadPowerAndClipCallback& read_power_callback); |
65 static void StopMonitoringStream(int render_process_id, | 66 static void StopMonitoringStream(int render_process_id, |
66 int render_frame_id, | 67 int render_frame_id, |
67 int stream_id); | 68 int stream_id); |
68 | 69 |
| 70 void set_was_recently_audible_for_testing(bool value) { |
| 71 was_recently_audible_ = value; |
| 72 } |
| 73 |
69 private: | 74 private: |
70 friend class AudioStreamMonitorTest; | 75 friend class AudioStreamMonitorTest; |
71 | 76 |
72 enum { | 77 enum { |
73 // Desired polling frequency. Note: If this is set too low, short-duration | 78 // Desired polling frequency. Note: If this is set too low, short-duration |
74 // "blip" sounds won't be detected. http://crbug.com/339133#c4 | 79 // "blip" sounds won't be detected. http://crbug.com/339133#c4 |
75 kPowerMeasurementsPerSecond = 15, | 80 kPowerMeasurementsPerSecond = 15, |
76 | 81 |
77 // Amount of time to hold a tab indicator on after its last blurt. | 82 // Amount of time to hold a tab indicator on after its last blurt. |
78 kHoldOnMilliseconds = 2000 | 83 kHoldOnMilliseconds = 2000 |
(...skipping 22 matching lines...) Expand all Loading... |
101 | 106 |
102 // 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 |
103 // playing in the tab. | 108 // playing in the tab. |
104 void Poll(); | 109 void Poll(); |
105 | 110 |
106 // 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 |
107 // updates through |web_contents_| if needed. When the indicator is turned | 112 // updates through |web_contents_| if needed. When the indicator is turned |
108 // 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. |
109 void MaybeToggle(); | 114 void MaybeToggle(); |
110 | 115 |
| 116 // The WebContents instance to receive indicator toggle notifications. This |
| 117 // pointer should be valid for the lifetime of AudioStreamMonitor. |
| 118 WebContents* const web_contents_; |
| 119 |
111 // Note: |clock_| is always |&default_tick_clock_|, except during unit | 120 // Note: |clock_| is always |&default_tick_clock_|, except during unit |
112 // testing. | 121 // testing. |
113 base::DefaultTickClock default_tick_clock_; | 122 base::DefaultTickClock default_tick_clock_; |
114 base::TickClock* const clock_; | 123 base::TickClock* const clock_; |
115 | 124 |
116 // Confirms single-threaded access in debug builds. | 125 // Confirms single-threaded access in debug builds. |
117 base::ThreadChecker thread_checker_; | 126 base::ThreadChecker thread_checker_; |
118 | 127 |
119 // The callbacks to read power levels for each stream. Only playing (i.e., | 128 // The callbacks to read power levels for each stream. Only playing (i.e., |
120 // not paused) streams will have an entry in this map. | 129 // not paused) streams will have an entry in this map. |
121 typedef std::pair<int, int> StreamID; | 130 typedef std::pair<int, int> StreamID; |
122 typedef std::map<StreamID, ReadPowerAndClipCallback> StreamPollCallbackMap; | 131 typedef std::map<StreamID, ReadPowerAndClipCallback> StreamPollCallbackMap; |
123 StreamPollCallbackMap poll_callbacks_; | 132 StreamPollCallbackMap poll_callbacks_; |
124 | 133 |
125 // Records the last time at which sound was audible from any stream. | 134 // Records the last time at which sound was audible from any stream. |
126 base::TimeTicks last_blurt_time_; | 135 base::TimeTicks last_blurt_time_; |
127 | 136 |
| 137 // Set to true if the last call to MaybeToggle() determined the indicator |
| 138 // should be turned on. |
| 139 bool was_recently_audible_; |
| 140 |
128 // Calls Poll() at regular intervals while |poll_callbacks_| is non-empty. | 141 // Calls Poll() at regular intervals while |poll_callbacks_| is non-empty. |
129 base::RepeatingTimer<AudioStreamMonitor> poll_timer_; | 142 base::RepeatingTimer<AudioStreamMonitor> poll_timer_; |
130 | 143 |
131 // Started only when an indicator is toggled on, to turn it off again in the | 144 // Started only when an indicator is toggled on, to turn it off again in the |
132 // future. | 145 // future. |
133 base::OneShotTimer<AudioStreamMonitor> off_timer_; | 146 base::OneShotTimer<AudioStreamMonitor> off_timer_; |
134 | 147 |
135 DISALLOW_COPY_AND_ASSIGN(AudioStreamMonitor); | 148 DISALLOW_COPY_AND_ASSIGN(AudioStreamMonitor); |
136 }; | 149 }; |
137 | 150 |
138 } // namespace content | 151 } // namespace content |
139 | 152 |
140 #endif // CONTENT_BROWSER_MEDIA_AUDIO_STREAM_MONITOR_H_ | 153 #endif // CONTENT_BROWSER_MEDIA_AUDIO_STREAM_MONITOR_H_ |
OLD | NEW |