Chromium Code Reviews| Index: content/renderer/media/webaudio_suspender.h |
| diff --git a/content/renderer/media/webaudio_suspender.h b/content/renderer/media/webaudio_suspender.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f6d6b2e53dbb0b0cfb1d58f348e886e1283bfe0a |
| --- /dev/null |
| +++ b/content/renderer/media/webaudio_suspender.h |
| @@ -0,0 +1,104 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
|
ncarter (slow)
2016/09/23 16:55:11
2016 yo
DaleCurtis
2016/09/23 20:24:19
Done.
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CONTENT_RENDERER_MEDIA_RENDERER_WEBAUDIO_SUSPENDER_H_ |
| +#define CONTENT_RENDERER_MEDIA_RENDERER_WEBAUDIO_SUSPENDER_H_ |
| + |
| +#include <stdint.h> |
| + |
| +#include <deque> |
| + |
| +#include "base/cancelable_callback.h" |
| +#include "base/macros.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/time/time.h" |
| +#include "content/common/content_export.h" |
| +#include "media/audio/fake_audio_worker.h" |
| +#include "media/base/audio_parameters.h" |
| +#include "media/base/audio_renderer_sink.h" |
| + |
| +namespace base { |
| +class SingleThreadTaskRunner; |
| +} |
| + |
| +namespace content { |
| + |
| +// Helper class for suspending AudioRenderSink instances when WebAudio is idle. |
| +// For our purposes idle means silence has been output for some time. When this |
| +// is detected, the provided |sink_| is paused and a fake sink is injected to |
| +// black hole the silent audio data and avoid physical hardware usage. |
| +class CONTENT_EXPORT WebAudioSuspender |
|
ncarter (slow)
2016/09/23 16:55:11
If this is WebAudioSuspender, the filename ought t
DaleCurtis
2016/09/23 20:24:19
True, but I named it this way since all the other
|
| + : public media::AudioRendererSink::RenderCallback { |
| + public: |
| + // |callback| is the true producer of audio data, |params| are the parameters |
| + // used to initialize |sink|, |sink| is the sink to monitor for idle, and |
| + // |worker| is the task runner to run the fake Render() callbacks on. |
| + WebAudioSuspender(media::AudioRendererSink::RenderCallback* callback, |
| + const media::AudioParameters& params, |
| + const scoped_refptr<media::AudioRendererSink>& sink, |
| + const scoped_refptr<base::SingleThreadTaskRunner>& worker); |
| + ~WebAudioSuspender() override; |
| + |
| + // AudioRendererSink::RenderCallback implementation. |
| + int Render(media::AudioBus* dest, |
| + uint32_t frames_delayed, |
| + uint32_t frames_skipped) override; |
| + void OnRenderError() override; |
| + |
| + bool is_using_fake_sink_for_testing() const { return is_using_fake_sink_; } |
| + void set_silent_timeout_for_testing(base::TimeDelta silent_timeout) { |
|
o1ka
2016/09/23 10:34:35
(side note: I'm not a fan of those "set for testin
DaleCurtis
2016/09/23 20:24:19
In this case I agree with you, removed the setter
|
| + silent_timeout_ = silent_timeout; |
| + } |
| + |
| + private: |
| + // If |use_fake_sink| is true, pauses |sink_| and plays |fake_sink_|; if |
| + // false, pauses |fake_sink_| and plays |sink_|. |
| + void TransitionSinks(bool use_fake_sink); |
| + |
| + // Actual RenderCallback providing audio data to the output device. |
| + media::AudioRendererSink::RenderCallback* const callback_; |
| + |
| + // Parameters used to construct |sink_|. |
| + const media::AudioParameters params_; |
| + |
| + // Sink monitored for silent output. |
| + scoped_refptr<media::AudioRendererSink> sink_; |
| + |
| + // Task runner this class is constructed on. Used to run TransitionSinks(). |
| + scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| + |
| + // Time when the silence starts. |
| + base::TimeTicks first_silence_time_; |
| + |
| + // Amount of time that can elapse before repacing |sink_| with |fake_sink_|. |
| + base::TimeDelta silent_timeout_ = base::TimeDelta::FromSeconds(30); |
| + |
| + // A fake audio sink object that consumes data when long period of silence |
| + // audio is detected. This object lives on |task_runner_| and will run |
| + // callbacks on RenderThreadImpl::GetMediaThreadTaskRunner(). |
| + media::FakeAudioWorker fake_sink_; |
| + |
| + // Whether audio output is directed to |fake_sink_|. Must only be used by |
| + // Render() or when both |sink_| and |fake_sink_| are paused. |
| + bool is_using_fake_sink_ = false; |
| + |
| + // Whether we're in the middle of a transition to or from |fake_sink_|. |
| + bool is_transition_pending_ = false; |
| + |
| + // Buffers accumulated during the sink transition from |fake_sink_| to |
| + // |sink_|. Must only be used by Render() or when both |sink_| and |
| + // |fake_sink_| are paused. |
| + std::deque<std::unique_ptr<media::AudioBus>> buffers_after_silence_; |
| + |
| + // A cancelable task that is posted to switch to or from the |fake_sink_| |
| + // after a period of silence or first non-silent audio respective. We do this |
| + // on Android to save battery consumption. |
| + base::CancelableCallback<void(bool)> sink_transition_callback_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(WebAudioSuspender); |
| +}; |
| + |
| +} // namespace content |
| + |
| +#endif // CONTENT_RENDERER_MEDIA_RENDERER_WEBAUDIO_SUSPENDER_H_ |