| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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. |
| 4 |
| 5 #ifndef MEDIA_BASE_SILENT_SINK_SUSPENDER_H_ |
| 6 #define MEDIA_BASE_SILENT_SINK_SUSPENDER_H_ |
| 7 |
| 8 #include <stdint.h> |
| 9 |
| 10 #include <deque> |
| 11 |
| 12 #include "base/cancelable_callback.h" |
| 13 #include "base/compiler_specific.h" |
| 14 #include "base/macros.h" |
| 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/synchronization/lock.h" |
| 17 #include "base/time/time.h" |
| 18 #include "media/audio/fake_audio_worker.h" |
| 19 #include "media/base/audio_parameters.h" |
| 20 #include "media/base/audio_renderer_sink.h" |
| 21 #include "media/base/media_export.h" |
| 22 |
| 23 namespace base { |
| 24 class SingleThreadTaskRunner; |
| 25 } |
| 26 |
| 27 namespace media { |
| 28 |
| 29 // Helper class for suspending AudioRenderSink instances after silence has been |
| 30 // detected for some time. When this is detected, the provided |sink_| is paused |
| 31 // and a fake sink is injected to black hole the silent audio data and avoid |
| 32 // physical hardware usage. |
| 33 class MEDIA_EXPORT SilentSinkSuspender |
| 34 : NON_EXPORTED_BASE(public AudioRendererSink::RenderCallback) { |
| 35 public: |
| 36 // |callback| is the true producer of audio data, |params| are the parameters |
| 37 // used to initialize |sink|, |sink| is the sink to monitor for idle, and |
| 38 // |worker| is the task runner to run the fake Render() callbacks on. The |
| 39 // amount of silence to allow before suspension is |silence_timeout|. |
| 40 SilentSinkSuspender( |
| 41 AudioRendererSink::RenderCallback* callback, |
| 42 base::TimeDelta silence_timeout, |
| 43 const AudioParameters& params, |
| 44 const scoped_refptr<AudioRendererSink>& sink, |
| 45 const scoped_refptr<base::SingleThreadTaskRunner>& worker); |
| 46 ~SilentSinkSuspender() override; |
| 47 |
| 48 // AudioRendererSink::RenderCallback implementation. |
| 49 int Render(AudioBus* dest, |
| 50 uint32_t frames_delayed, |
| 51 uint32_t frames_skipped) override; |
| 52 void OnRenderError() override; |
| 53 |
| 54 bool is_using_fake_sink_for_testing() const { return is_using_fake_sink_; } |
| 55 |
| 56 private: |
| 57 // If |use_fake_sink| is true, pauses |sink_| and plays |fake_sink_|; if |
| 58 // false, pauses |fake_sink_| and plays |sink_|. |
| 59 void TransitionSinks(bool use_fake_sink); |
| 60 |
| 61 // Actual RenderCallback providing audio data to the output device. |
| 62 AudioRendererSink::RenderCallback* const callback_; |
| 63 |
| 64 // Parameters used to construct |sink_|. |
| 65 const AudioParameters params_; |
| 66 |
| 67 // Sink monitored for silent output. |
| 68 scoped_refptr<AudioRendererSink> sink_; |
| 69 |
| 70 // Task runner this class is constructed on. Used to run TransitionSinks(). |
| 71 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 72 |
| 73 // Time when the silence starts. |
| 74 base::TimeTicks first_silence_time_; |
| 75 |
| 76 // Amount of time that can elapse before replacing |sink_| with |fake_sink_|. |
| 77 const base::TimeDelta silence_timeout_; |
| 78 |
| 79 // A fake audio sink object that consumes data when long period of silence |
| 80 // audio is detected. This object lives on |task_runner_| and will run |
| 81 // callbacks on RenderThreadImpl::GetMediaThreadTaskRunner(). |
| 82 FakeAudioWorker fake_sink_; |
| 83 |
| 84 // AudioRendererSink::Pause() is not synchronous, so we need a lock to ensure |
| 85 // we don't have concurrent access to Render(). |
| 86 base::Lock transition_lock_; |
| 87 |
| 88 // Whether audio output is directed to |fake_sink_|. Must only be used when |
| 89 // |transition_lock_| is held or both sinks are stopped. |
| 90 bool is_using_fake_sink_ = false; |
| 91 |
| 92 // Whether we're in the middle of a transition to or from |fake_sink_|. Must |
| 93 // only be used when |transition_lock_| is held or both sinks are stopped. |
| 94 bool is_transition_pending_ = false; |
| 95 |
| 96 // Buffers accumulated during the transition from |fake_sink_| to |sink_|. |
| 97 std::deque<std::unique_ptr<AudioBus>> buffers_after_silence_; |
| 98 |
| 99 // A cancelable task that is posted to switch to or from the |fake_sink_| |
| 100 // after a period of silence or first non-silent audio respective. We do this |
| 101 // on Android to save battery consumption. |
| 102 base::CancelableCallback<void(bool)> sink_transition_callback_; |
| 103 |
| 104 DISALLOW_COPY_AND_ASSIGN(SilentSinkSuspender); |
| 105 }; |
| 106 |
| 107 } // namespace content |
| 108 |
| 109 #endif // MEDIA_BASE_SILENT_SINK_SUSPENDER_H_ |
| OLD | NEW |