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