Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(154)

Side by Side Diff: content/renderer/media/webaudio_suspender.h

Issue 2365723003: Break out WebAudio suspension code into new class. Add tests. (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // 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.
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 CONTENT_RENDERER_MEDIA_RENDERER_WEBAUDIO_SUSPENDER_H_
6 #define CONTENT_RENDERER_MEDIA_RENDERER_WEBAUDIO_SUSPENDER_H_
7
8 #include <stdint.h>
9
10 #include <deque>
11
12 #include "base/cancelable_callback.h"
13 #include "base/macros.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/time/time.h"
16 #include "content/common/content_export.h"
17 #include "media/audio/fake_audio_worker.h"
18 #include "media/base/audio_parameters.h"
19 #include "media/base/audio_renderer_sink.h"
20
21 namespace base {
22 class SingleThreadTaskRunner;
23 }
24
25 namespace content {
26
27 // Helper class for suspending AudioRenderSink instances when WebAudio is idle.
28 // For our purposes idle means silence has been output for some time. When this
29 // is detected, the provided |sink_| is paused and a fake sink is injected to
30 // black hole the silent audio data and avoid physical hardware usage.
31 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
32 : public media::AudioRendererSink::RenderCallback {
33 public:
34 // |callback| is the true producer of audio data, |params| are the parameters
35 // used to initialize |sink|, |sink| is the sink to monitor for idle, and
36 // |worker| is the task runner to run the fake Render() callbacks on.
37 WebAudioSuspender(media::AudioRendererSink::RenderCallback* callback,
38 const media::AudioParameters& params,
39 const scoped_refptr<media::AudioRendererSink>& sink,
40 const scoped_refptr<base::SingleThreadTaskRunner>& worker);
41 ~WebAudioSuspender() override;
42
43 // AudioRendererSink::RenderCallback implementation.
44 int Render(media::AudioBus* dest,
45 uint32_t frames_delayed,
46 uint32_t frames_skipped) override;
47 void OnRenderError() override;
48
49 bool is_using_fake_sink_for_testing() const { return is_using_fake_sink_; }
50 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
51 silent_timeout_ = silent_timeout;
52 }
53
54 private:
55 // If |use_fake_sink| is true, pauses |sink_| and plays |fake_sink_|; if
56 // false, pauses |fake_sink_| and plays |sink_|.
57 void TransitionSinks(bool use_fake_sink);
58
59 // Actual RenderCallback providing audio data to the output device.
60 media::AudioRendererSink::RenderCallback* const callback_;
61
62 // Parameters used to construct |sink_|.
63 const media::AudioParameters params_;
64
65 // Sink monitored for silent output.
66 scoped_refptr<media::AudioRendererSink> sink_;
67
68 // Task runner this class is constructed on. Used to run TransitionSinks().
69 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
70
71 // Time when the silence starts.
72 base::TimeTicks first_silence_time_;
73
74 // Amount of time that can elapse before repacing |sink_| with |fake_sink_|.
75 base::TimeDelta silent_timeout_ = base::TimeDelta::FromSeconds(30);
76
77 // A fake audio sink object that consumes data when long period of silence
78 // audio is detected. This object lives on |task_runner_| and will run
79 // callbacks on RenderThreadImpl::GetMediaThreadTaskRunner().
80 media::FakeAudioWorker fake_sink_;
81
82 // Whether audio output is directed to |fake_sink_|. Must only be used by
83 // Render() or when both |sink_| and |fake_sink_| are paused.
84 bool is_using_fake_sink_ = false;
85
86 // Whether we're in the middle of a transition to or from |fake_sink_|.
87 bool is_transition_pending_ = false;
88
89 // Buffers accumulated during the sink transition from |fake_sink_| to
90 // |sink_|. Must only be used by Render() or when both |sink_| and
91 // |fake_sink_| are paused.
92 std::deque<std::unique_ptr<media::AudioBus>> buffers_after_silence_;
93
94 // A cancelable task that is posted to switch to or from the |fake_sink_|
95 // after a period of silence or first non-silent audio respective. We do this
96 // on Android to save battery consumption.
97 base::CancelableCallback<void(bool)> sink_transition_callback_;
98
99 DISALLOW_COPY_AND_ASSIGN(WebAudioSuspender);
100 };
101
102 } // namespace content
103
104 #endif // CONTENT_RENDERER_MEDIA_RENDERER_WEBAUDIO_SUSPENDER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698