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
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_MEDIA_AUDIO_STREAM_MONITOR_H_
6 #define CONTENT_BROWSER_MEDIA_AUDIO_STREAM_MONITOR_H_
11 #include "base/callback_forward.h"
12 #include "base/threading/thread_checker.h"
13 #include "base/time/default_tick_clock.h"
14 #include "base/time/time.h"
15 #include "base/timer/timer.h"
16 #include "build/build_config.h"
17 #include "content/common/content_export.h"
18 #include "media/audio/audio_output_controller.h"
28 // Repeatedly polls audio streams for their power levels, and "debounces" the
29 // information into a simple, binary "was recently audible" result for the audio
30 // indicators in the tab UI. The debouncing logic is to: 1) Turn on immediately
31 // when sound is audible; and 2) Hold on for X amount of time after sound has
32 // gone silent, then turn off. Said another way, we don't want tab indicators
33 // to turn on/off repeatedly and annoy the user. AudioStreamMonitor sends UI
34 // update notifications only when needed, but may be queried at any time.
36 // Each WebContentsImpl owns an AudioStreamMonitor.
37 class CONTENT_EXPORT AudioStreamMonitor
{
39 explicit AudioStreamMonitor(WebContents
* contents
);
40 ~AudioStreamMonitor();
42 // Indicates if audio stream monitoring is available. It's only available if
43 // AudioOutputController can and will monitor output power levels.
44 static bool monitoring_available() {
45 return media::AudioOutputController::will_monitor_audio_levels();
48 // Returns true if audio has recently been audible from the tab. This is
49 // usually called whenever the tab data model is refreshed; but there are
50 // other use cases as well (e.g., the OOM killer uses this to de-prioritize
51 // the killing of tabs making sounds).
52 bool WasRecentlyAudible() const;
54 // Starts or stops audio level monitoring respectively for the stream owned by
55 // the specified renderer. Safe to call from any thread.
57 // The callback returns the current power level (in dBFS units) and the clip
58 // status (true if any part of the audio signal has clipped since the last
59 // callback run). |stream_id| must be unique within a |render_process_id|.
60 typedef base::Callback
<std::pair
<float, bool>()> ReadPowerAndClipCallback
;
61 static void StartMonitoringStream(
62 int render_process_id
,
65 const ReadPowerAndClipCallback
& read_power_callback
);
66 static void StopMonitoringStream(int render_process_id
,
70 void set_was_recently_audible_for_testing(bool value
) {
71 was_recently_audible_
= value
;
75 friend class AudioStreamMonitorTest
;
78 // Desired polling frequency. Note: If this is set too low, short-duration
79 // "blip" sounds won't be detected. http://crbug.com/339133#c4
80 kPowerMeasurementsPerSecond
= 15,
82 // Amount of time to hold a tab indicator on after its last blurt.
83 kHoldOnMilliseconds
= 2000
86 // Helper methods for starting and stopping monitoring which lookup the
87 // identified renderer and forward calls to the correct AudioStreamMonitor.
88 static void StartMonitoringHelper(
89 int render_process_id
,
92 const ReadPowerAndClipCallback
& read_power_callback
);
93 static void StopMonitoringHelper(int render_process_id
,
97 // Starts polling the stream for audio stream power levels using |callback|.
98 void StartMonitoringStreamOnUIThread(
99 int render_process_id
,
101 const ReadPowerAndClipCallback
& callback
);
103 // Stops polling the stream, discarding the internal copy of the |callback|
104 // provided in the call to StartMonitoringStream().
105 void StopMonitoringStreamOnUIThread(int render_process_id
, int stream_id
);
107 // Called by |poll_timer_| to sample the power levels from each of the streams
108 // playing in the tab.
111 // Compares last known indicator state with what it should be, and triggers UI
112 // updates through |web_contents_| if needed. When the indicator is turned
113 // on, |off_timer_| is started to re-invoke this method in the future.
116 // The WebContents instance to receive indicator toggle notifications. This
117 // pointer should be valid for the lifetime of AudioStreamMonitor.
118 WebContents
* const web_contents_
;
120 // Note: |clock_| is always |&default_tick_clock_|, except during unit
122 base::DefaultTickClock default_tick_clock_
;
123 base::TickClock
* const clock_
;
125 // Confirms single-threaded access in debug builds.
126 base::ThreadChecker thread_checker_
;
128 // The callbacks to read power levels for each stream. Only playing (i.e.,
129 // not paused) streams will have an entry in this map.
130 typedef std::pair
<int, int> StreamID
;
131 typedef std::map
<StreamID
, ReadPowerAndClipCallback
> StreamPollCallbackMap
;
132 StreamPollCallbackMap poll_callbacks_
;
134 // Records the last time at which sound was audible from any stream.
135 base::TimeTicks last_blurt_time_
;
137 // Set to true if the last call to MaybeToggle() determined the indicator
138 // should be turned on.
139 bool was_recently_audible_
;
141 // Calls Poll() at regular intervals while |poll_callbacks_| is non-empty.
142 base::RepeatingTimer
<AudioStreamMonitor
> poll_timer_
;
144 // Started only when an indicator is toggled on, to turn it off again in the
146 base::OneShotTimer
<AudioStreamMonitor
> off_timer_
;
148 DISALLOW_COPY_AND_ASSIGN(AudioStreamMonitor
);
151 } // namespace content
153 #endif // CONTENT_BROWSER_MEDIA_AUDIO_STREAM_MONITOR_H_