Chromium Code Reviews| Index: content/browser/media/audio_stream_monitor.h |
| diff --git a/chrome/browser/media/audio_stream_monitor.h b/content/browser/media/audio_stream_monitor.h |
| similarity index 54% |
| rename from chrome/browser/media/audio_stream_monitor.h |
| rename to content/browser/media/audio_stream_monitor.h |
| index 3282dbd8ba77edde54c6c35afb17bd4f49e5068e..97311ca5bbbbcd2863e779c1e9b4d4761822d74b 100644 |
| --- a/chrome/browser/media/audio_stream_monitor.h |
| +++ b/content/browser/media/audio_stream_monitor.h |
| @@ -2,22 +2,27 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -#ifndef CHROME_BROWSER_MEDIA_AUDIO_STREAM_MONITOR_H_ |
| -#define CHROME_BROWSER_MEDIA_AUDIO_STREAM_MONITOR_H_ |
| +#ifndef CONTENT_BROWSER_MEDIA_AUDIO_STREAM_MONITOR_H_ |
| +#define CONTENT_BROWSER_MEDIA_AUDIO_STREAM_MONITOR_H_ |
| #include <map> |
| +#include <utility> |
| #include "base/callback_forward.h" |
| #include "base/threading/thread_checker.h" |
| #include "base/time/default_tick_clock.h" |
| #include "base/time/time.h" |
| #include "base/timer/timer.h" |
| +#include "build/build_config.h" |
| #include "content/public/browser/web_contents_user_data.h" |
| +#include "media/audio/audio_output_controller.h" |
| namespace base { |
| class TickClock; |
| } |
| +namespace content { |
| + |
| // Repeatedly polls audio streams for their power levels, and "debounces" the |
| // information into a simple, binary "was recently audible" result for the audio |
| // indicators in the tab UI. The debouncing logic is to: 1) Turn on immediately |
| @@ -26,31 +31,50 @@ class TickClock; |
| // to turn on/off repeatedly and annoy the user. AudioStreamMonitor sends UI |
| // update notifications only when needed, but may be queried at any time. |
| // |
| -// There are zero or one instances of AudioStreamMonitor per |
| -// content::WebContents instance (referred to as "the tab" in comments below). |
| -// AudioStreamMonitor is created on-demand, and automatically destroyed when its |
| -// associated WebContents is destroyed. See content::WebContentsUserData for |
| -// usage. |
| -class AudioStreamMonitor |
| - : public content::WebContentsUserData<AudioStreamMonitor> { |
| +// There are zero or one instances of AudioStreamMonitor per WebContents |
| +// instance (referred to as "the tab" in comments below). AudioStreamMonitor is |
| +// created on-demand, and automatically destroyed when its associated |
| +// WebContents is destroyed. See WebContentsUserData for usage. |
| +// |
| +// 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.
|
| +// suspension when audible audio is being produced. |
| +class CONTENT_EXPORT AudioStreamMonitor |
| + : public WebContentsUserData<AudioStreamMonitor> { |
| public: |
| + // Indicates if the tab audio indicator is available. It's only available if |
| + // the AudioOutputController can and will be monitoring output power levels. |
| + 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.
|
| + return media::AudioOutputController::will_monitor_audio_levels(); |
| + } |
| + |
| // Returns true if audio has recently been audible from the tab. This is |
| // usually called whenever the tab data model is refreshed; but there are |
| // other use cases as well (e.g., the OOM killer uses this to de-prioritize |
| // the killing of tabs making sounds). |
| bool WasRecentlyAudible() const; |
| - // Starts polling the stream for audio stream power levels using |callback|. |
| + // Starts or stops audio level monitoring respectively for the stream owned by |
| + // the specified renderer. Safe to call from any thread. |
| + // |
| + // The callback returns the current power level (in dBFS units) and the clip |
| + // status (true if any part of the audio signal has clipped since the last |
| + // callback run). |stream_id| must be unique within a |render_process_id|. |
| typedef base::Callback<std::pair<float, bool>()> ReadPowerAndClipCallback; |
| - void StartMonitoringStream(int stream_id, |
| - const ReadPowerAndClipCallback& callback); |
| - |
| - // Stops polling the stream, discarding the internal copy of the |callback| |
| - // provided in the call to StartMonitoringStream(). |
| - void StopMonitoringStream(int stream_id); |
| + static void StartMonitoringStream( |
| + int render_process_id, |
| + int render_frame_id, |
| + int stream_id, |
| + const ReadPowerAndClipCallback& read_power_callback); |
| + static void StopMonitoringStream(int render_process_id, |
| + int render_frame_id, |
| + int stream_id); |
| + |
| + void set_was_recently_audible_for_testing(bool value) { |
| + was_recently_audible_ = value; |
| + } |
| private: |
| - friend class content::WebContentsUserData<AudioStreamMonitor>; |
| + friend class WebContentsUserData<AudioStreamMonitor>; |
| friend class AudioStreamMonitorTest; |
| enum { |
| @@ -62,10 +86,31 @@ class AudioStreamMonitor |
| kHoldOnMilliseconds = 2000 |
| }; |
| - // Invoked by content::WebContentsUserData only. |
| - explicit AudioStreamMonitor(content::WebContents* contents); |
| + // Invoked by WebContentsUserData only. |
| + explicit AudioStreamMonitor(WebContents* contents); |
| virtual ~AudioStreamMonitor(); |
| + // Helper methods for starting and stopping monitoring which lookup the |
| + // identified renderer and forward calls to the correct AudioStreamMonitor. |
| + static void StartMonitoringHelper( |
| + int render_process_id, |
| + int render_frame_id, |
| + int stream_id, |
| + const ReadPowerAndClipCallback& read_power_callback); |
| + static void StopMonitoringHelper(int render_process_id, |
| + int render_frame_id, |
| + int stream_id); |
| + |
| + // Starts polling the stream for audio stream power levels using |callback|. |
| + void StartMonitoringStreamOnUIThread( |
| + int render_process_id, |
| + int stream_id, |
| + const ReadPowerAndClipCallback& callback); |
| + |
| + // Stops polling the stream, discarding the internal copy of the |callback| |
| + // provided in the call to StartMonitoringStream(). |
| + void StopMonitoringStreamOnUIThread(int render_process_id, int stream_id); |
| + |
| // Called by |poll_timer_| to sample the power levels from each of the streams |
| // playing in the tab. |
| void Poll(); |
| @@ -78,7 +123,7 @@ class AudioStreamMonitor |
| // The WebContents instance instance to receive indicator toggle |
| // notifications. This pointer should be valid for the lifetime of |
| // AudioStreamMonitor. |
| - content::WebContents* const web_contents_; |
| + WebContents* const web_contents_; |
| // Note: |clock_| is always |&default_tick_clock_|, except during unit |
| // testing. |
| @@ -90,7 +135,8 @@ class AudioStreamMonitor |
| // The callbacks to read power levels for each stream. Only playing (i.e., |
| // not paused) streams will have an entry in this map. |
| - typedef std::map<int, ReadPowerAndClipCallback> StreamPollCallbackMap; |
| + typedef std::pair<int, int> StreamID; |
|
miu
2014/09/05 18:20:25
Thanks for fixing this! :)
|
| + typedef std::map<StreamID, ReadPowerAndClipCallback> StreamPollCallbackMap; |
| StreamPollCallbackMap poll_callbacks_; |
| // Records the last time at which sound was audible from any stream. |
| @@ -110,4 +156,6 @@ class AudioStreamMonitor |
| DISALLOW_COPY_AND_ASSIGN(AudioStreamMonitor); |
| }; |
| -#endif // CHROME_BROWSER_MEDIA_AUDIO_STREAM_MONITOR_H_ |
| +} // namespace content |
| + |
| +#endif // CONTENT_BROWSER_MEDIA_AUDIO_STREAM_MONITOR_H_ |