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 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/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/threading/thread_checker.h" | 13 #include "base/threading/thread_checker.h" |
| 14 #include "base/time/default_tick_clock.h" | 14 #include "base/time/default_tick_clock.h" |
| 15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 16 #include "base/timer/timer.h" | 16 #include "base/timer/timer.h" |
| 17 #include "base/tuple.h" | |
| 17 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 18 #include "content/common/content_export.h" | 19 #include "content/common/content_export.h" |
| 19 #include "media/audio/audio_output_controller.h" | 20 #include "media/audio/audio_output_controller.h" |
| 20 | 21 |
| 21 namespace base { | 22 namespace base { |
| 22 class TickClock; | 23 class TickClock; |
| 23 } | 24 } |
| 24 | 25 |
| 25 namespace content { | 26 namespace content { |
| 26 | 27 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 104 int render_frame_id, | 105 int render_frame_id, |
| 105 int stream_id, | 106 int stream_id, |
| 106 const ReadPowerAndClipCallback& read_power_callback); | 107 const ReadPowerAndClipCallback& read_power_callback); |
| 107 static void StopMonitoringHelper(int render_process_id, | 108 static void StopMonitoringHelper(int render_process_id, |
| 108 int render_frame_id, | 109 int render_frame_id, |
| 109 int stream_id); | 110 int stream_id); |
| 110 | 111 |
| 111 // Starts polling the stream for audio stream power levels using |callback|. | 112 // Starts polling the stream for audio stream power levels using |callback|. |
| 112 void StartMonitoringStreamOnUIThread( | 113 void StartMonitoringStreamOnUIThread( |
| 113 int render_process_id, | 114 int render_process_id, |
| 115 int render_frame_id, | |
| 114 int stream_id, | 116 int stream_id, |
| 115 const ReadPowerAndClipCallback& callback); | 117 const ReadPowerAndClipCallback& callback); |
| 116 | 118 |
| 117 // Stops polling the stream, discarding the internal copy of the |callback| | 119 // Stops polling the stream, discarding the internal copy of the |callback| |
| 118 // provided in the call to StartMonitoringStream(). | 120 // provided in the call to StartMonitoringStream(). |
| 119 void StopMonitoringStreamOnUIThread(int render_process_id, int stream_id); | 121 void StopMonitoringStreamOnUIThread(int render_process_id, |
| 122 int render_frame_id, | |
| 123 int stream_id); | |
| 120 | 124 |
| 121 // Called by |poll_timer_| to sample the power levels from each of the streams | 125 // Called by |poll_timer_| to sample the power levels from each of the streams |
| 122 // playing in the tab. | 126 // playing in the tab. |
| 123 void Poll(); | 127 void Poll(); |
| 124 | 128 |
| 125 // Compares last known indicator state with what it should be, and triggers UI | 129 // Compares last known indicator state with what it should be, and triggers UI |
| 126 // updates through |web_contents_| if needed. When the indicator is turned | 130 // updates through |web_contents_| if needed. When the indicator is turned |
| 127 // on, |off_timer_| is started to re-invoke this method in the future. | 131 // on, |off_timer_| is started to re-invoke this method in the future. |
| 128 void MaybeToggle(); | 132 void MaybeToggle(); |
| 129 | 133 |
| 130 // Helper functions to track number of active streams when power level | 134 // Helper functions to track number of active streams when power level |
| 131 // monitoring is not available. | 135 // monitoring is not available. |
| 132 void OnStreamAdded(); | 136 void OnStreamAdded(); |
| 133 void OnStreamRemoved(); | 137 void OnStreamRemoved(); |
| 134 | 138 |
| 135 // The WebContents instance to receive indicator toggle notifications. This | 139 // The WebContents instance to receive indicator toggle notifications. This |
| 136 // pointer should be valid for the lifetime of AudioStreamMonitor. | 140 // pointer should be valid for the lifetime of AudioStreamMonitor. |
| 137 WebContents* const web_contents_; | 141 WebContents* const web_contents_; |
| 138 | 142 |
| 139 // Note: |clock_| is always |&default_tick_clock_|, except during unit | 143 // Note: |clock_| is always |&default_tick_clock_|, except during unit |
| 140 // testing. | 144 // testing. |
| 141 base::DefaultTickClock default_tick_clock_; | 145 base::DefaultTickClock default_tick_clock_; |
| 142 base::TickClock* const clock_; | 146 base::TickClock* const clock_; |
| 143 | 147 |
| 144 // Confirms single-threaded access in debug builds. | 148 // Confirms single-threaded access in debug builds. |
| 145 base::ThreadChecker thread_checker_; | 149 base::ThreadChecker thread_checker_; |
| 146 | 150 |
| 147 // The callbacks to read power levels for each stream. Only playing (i.e., | 151 // The callbacks to read power levels for each stream. Only playing (i.e., |
| 148 // not paused) streams will have an entry in this map. | 152 // not paused) streams will have an entry in this map. |
| 149 using StreamID = std::pair<int, int>; | 153 using StreamID = std::tuple<int, int, int>; |
|
chrisha
2017/06/20 14:30:35
IMO it would be useful to document what each entry
lpy
2017/06/20 22:28:53
Done.
| |
| 150 using StreamPollCallbackMap = std::map<StreamID, ReadPowerAndClipCallback>; | 154 using StreamPollCallbackMap = std::map<StreamID, ReadPowerAndClipCallback>; |
| 151 StreamPollCallbackMap poll_callbacks_; | 155 StreamPollCallbackMap poll_callbacks_; |
| 152 | 156 |
| 153 // Records the last time at which sound was audible from any stream. | 157 // Records the last time at which sound was audible from any stream. |
| 154 base::TimeTicks last_blurt_time_; | 158 base::TimeTicks last_blurt_time_; |
| 155 | 159 |
| 156 // Set to true if the last call to MaybeToggle() determined the indicator | 160 // Set to true if the last call to MaybeToggle() determined the indicator |
| 157 // should be turned on. | 161 // should be turned on. |
| 158 bool was_recently_audible_; | 162 bool was_recently_audible_; |
| 159 | 163 |
| 160 // Whether the WebContents is currently audible. | 164 // Whether the WebContents is currently audible. |
| 161 bool is_audible_; | 165 bool is_audible_; |
| 162 | 166 |
| 163 // Calls Poll() at regular intervals while |poll_callbacks_| is non-empty. | 167 // Calls Poll() at regular intervals while |poll_callbacks_| is non-empty. |
| 164 base::RepeatingTimer poll_timer_; | 168 base::RepeatingTimer poll_timer_; |
| 165 | 169 |
| 166 // Started only when an indicator is toggled on, to turn it off again in the | 170 // Started only when an indicator is toggled on, to turn it off again in the |
| 167 // future. | 171 // future. |
| 168 base::OneShotTimer off_timer_; | 172 base::OneShotTimer off_timer_; |
| 169 | 173 |
| 170 DISALLOW_COPY_AND_ASSIGN(AudioStreamMonitor); | 174 DISALLOW_COPY_AND_ASSIGN(AudioStreamMonitor); |
| 171 }; | 175 }; |
| 172 | 176 |
| 173 } // namespace content | 177 } // namespace content |
| 174 | 178 |
| 175 #endif // CONTENT_BROWSER_MEDIA_AUDIO_STREAM_MONITOR_H_ | 179 #endif // CONTENT_BROWSER_MEDIA_AUDIO_STREAM_MONITOR_H_ |
| OLD | NEW |