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 58% |
rename from chrome/browser/media/audio_stream_monitor.h |
rename to content/browser/media/audio_stream_monitor.h |
index 3282dbd8ba77edde54c6c35afb17bd4f49e5068e..d1a32d5f857b05925f884ef64d834e673c8c5f29 100644 |
--- a/chrome/browser/media/audio_stream_monitor.h |
+++ b/content/browser/media/audio_stream_monitor.h |
@@ -2,22 +2,28 @@ |
// 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 "content/public/browser/web_contents_user_data.h" |
+#include "build/build_config.h" |
+#include "content/common/content_export.h" |
+#include "media/audio/audio_output_controller.h" |
namespace base { |
class TickClock; |
} |
+namespace content { |
+class WebContents; |
+ |
// 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 +32,45 @@ 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> { |
+// Each WebContentsImpl owns an AudioStreamMonitor. |
+class CONTENT_EXPORT AudioStreamMonitor { |
public: |
+ explicit AudioStreamMonitor(WebContents* contents); |
+ ~AudioStreamMonitor(); |
+ |
+ // Indicates if audio stream monitoring is available. It's only available if |
+ // AudioOutputController can and will monitor output power levels. |
+ static bool monitoring_available() { |
+ 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 AudioStreamMonitorTest; |
enum { |
@@ -62,9 +82,26 @@ class AudioStreamMonitor |
kHoldOnMilliseconds = 2000 |
}; |
- // Invoked by content::WebContentsUserData only. |
- explicit AudioStreamMonitor(content::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. |
@@ -78,7 +115,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 +127,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; |
+ typedef std::map<StreamID, ReadPowerAndClipCallback> StreamPollCallbackMap; |
StreamPollCallbackMap poll_callbacks_; |
// Records the last time at which sound was audible from any stream. |
@@ -110,4 +148,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_ |