| 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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 // 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 |
| 48 // 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 |
| 49 // the killing of tabs making sounds). | 49 // the killing of tabs making sounds). |
| 50 bool WasRecentlyAudible() const; | 50 bool WasRecentlyAudible() const; |
| 51 | 51 |
| 52 // 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. |
| 53 // The difference from WasRecentlyAudible() is that this method will return | 53 // The difference from WasRecentlyAudible() is that this method will return |
| 54 // false as soon as the WebContents stop producing sound. | 54 // false as soon as the WebContents stop producing sound. |
| 55 bool IsCurrentlyAudible() const; | 55 bool IsCurrentlyAudible() const; |
| 56 | 56 |
| 57 // Called by the WebContentsImpl if |render_process_id| dies; used to clear |
| 58 // any outstanding poll callbacks. |
| 59 void RenderProcessGone(int render_process_id); |
| 60 |
| 57 // Starts or stops audio level monitoring respectively for the stream owned by | 61 // Starts or stops audio level monitoring respectively for the stream owned by |
| 58 // the specified renderer. Safe to call from any thread. | 62 // the specified renderer. Safe to call from any thread. |
| 59 // | 63 // |
| 60 // The callback returns the current power level (in dBFS units) and the clip | 64 // The callback returns the current power level (in dBFS units) and the clip |
| 61 // status (true if any part of the audio signal has clipped since the last | 65 // status (true if any part of the audio signal has clipped since the last |
| 62 // callback run). |stream_id| must be unique within a |render_process_id|. | 66 // callback run). |stream_id| must be unique within a |render_process_id|. |
| 63 typedef base::Callback<std::pair<float, bool>()> ReadPowerAndClipCallback; | 67 typedef base::Callback<std::pair<float, bool>()> ReadPowerAndClipCallback; |
| 64 static void StartMonitoringStream( | 68 static void StartMonitoringStream( |
| 65 int render_process_id, | 69 int render_process_id, |
| 66 int render_frame_id, | 70 int render_frame_id, |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 // Note: |clock_| is always |&default_tick_clock_|, except during unit | 139 // Note: |clock_| is always |&default_tick_clock_|, except during unit |
| 136 // testing. | 140 // testing. |
| 137 base::DefaultTickClock default_tick_clock_; | 141 base::DefaultTickClock default_tick_clock_; |
| 138 base::TickClock* const clock_; | 142 base::TickClock* const clock_; |
| 139 | 143 |
| 140 // Confirms single-threaded access in debug builds. | 144 // Confirms single-threaded access in debug builds. |
| 141 base::ThreadChecker thread_checker_; | 145 base::ThreadChecker thread_checker_; |
| 142 | 146 |
| 143 // The callbacks to read power levels for each stream. Only playing (i.e., | 147 // The callbacks to read power levels for each stream. Only playing (i.e., |
| 144 // not paused) streams will have an entry in this map. | 148 // not paused) streams will have an entry in this map. |
| 145 typedef std::pair<int, int> StreamID; | 149 using StreamID = std::pair<int, int>; |
| 146 typedef std::map<StreamID, ReadPowerAndClipCallback> StreamPollCallbackMap; | 150 using StreamPollCallbackMap = std::map<StreamID, ReadPowerAndClipCallback>; |
| 147 StreamPollCallbackMap poll_callbacks_; | 151 StreamPollCallbackMap poll_callbacks_; |
| 148 | 152 |
| 149 // Records the last time at which sound was audible from any stream. | 153 // Records the last time at which sound was audible from any stream. |
| 150 base::TimeTicks last_blurt_time_; | 154 base::TimeTicks last_blurt_time_; |
| 151 | 155 |
| 152 // Set to true if the last call to MaybeToggle() determined the indicator | 156 // Set to true if the last call to MaybeToggle() determined the indicator |
| 153 // should be turned on. | 157 // should be turned on. |
| 154 bool was_recently_audible_; | 158 bool was_recently_audible_; |
| 155 | 159 |
| 156 // Whether the WebContents is currently audible. | 160 // Whether the WebContents is currently audible. |
| 157 bool is_audible_; | 161 bool is_audible_; |
| 158 | 162 |
| 159 // Calls Poll() at regular intervals while |poll_callbacks_| is non-empty. | 163 // Calls Poll() at regular intervals while |poll_callbacks_| is non-empty. |
| 160 base::RepeatingTimer poll_timer_; | 164 base::RepeatingTimer poll_timer_; |
| 161 | 165 |
| 162 // Started only when an indicator is toggled on, to turn it off again in the | 166 // Started only when an indicator is toggled on, to turn it off again in the |
| 163 // future. | 167 // future. |
| 164 base::OneShotTimer off_timer_; | 168 base::OneShotTimer off_timer_; |
| 165 | 169 |
| 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 | |
| 170 DISALLOW_COPY_AND_ASSIGN(AudioStreamMonitor); | 170 DISALLOW_COPY_AND_ASSIGN(AudioStreamMonitor); |
| 171 }; | 171 }; |
| 172 | 172 |
| 173 } // namespace content | 173 } // namespace content |
| 174 | 174 |
| 175 #endif // CONTENT_BROWSER_MEDIA_AUDIO_STREAM_MONITOR_H_ | 175 #endif // CONTENT_BROWSER_MEDIA_AUDIO_STREAM_MONITOR_H_ |
| OLD | NEW |