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

Side by Side Diff: media/audio/fake_audio_worker.cc

Issue 922663002: Moved the fake input stream's processing onto the audio worker thread. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 10 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
« no previous file with comments | « media/audio/fake_audio_worker.h ('k') | media/audio/fake_audio_worker_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "media/audio/fake_audio_consumer.h" 5 #include "media/audio/fake_audio_worker.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/cancelable_callback.h" 9 #include "base/cancelable_callback.h"
10 #include "base/location.h" 10 #include "base/location.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/single_thread_task_runner.h" 13 #include "base/single_thread_task_runner.h"
14 #include "base/synchronization/lock.h" 14 #include "base/synchronization/lock.h"
15 #include "base/threading/thread_checker.h" 15 #include "base/threading/thread_checker.h"
16 #include "base/time/time.h" 16 #include "base/time/time.h"
17 #include "media/audio/audio_parameters.h" 17 #include "media/audio/audio_parameters.h"
18 #include "media/base/audio_bus.h"
19 18
20 namespace media { 19 namespace media {
21 20
22 class FakeAudioConsumer::Worker 21 class FakeAudioWorker::Worker
23 : public base::RefCountedThreadSafe<FakeAudioConsumer::Worker> { 22 : public base::RefCountedThreadSafe<FakeAudioWorker::Worker> {
24 public: 23 public:
25 Worker(const scoped_refptr<base::SingleThreadTaskRunner>& worker_task_runner, 24 Worker(const scoped_refptr<base::SingleThreadTaskRunner>& worker_task_runner,
26 const AudioParameters& params); 25 const AudioParameters& params);
27 26
28 bool IsStopped(); 27 bool IsStopped();
29 void Start(const ReadCB& read_cb); 28 void Start(const base::Closure& worker_cb);
30 void Stop(); 29 void Stop();
31 30
32 private: 31 private:
33 friend class base::RefCountedThreadSafe<Worker>; 32 friend class base::RefCountedThreadSafe<Worker>;
34 ~Worker(); 33 ~Worker();
35 34
36 // Initialize and start regular calls to DoRead() on the worker thread. 35 // Initialize and start regular calls to DoRead() on the worker thread.
37 void DoStart(); 36 void DoStart();
38 37
39 // Cancel any delayed callbacks to DoRead() in the worker loop's queue. 38 // Cancel any delayed callbacks to DoRead() in the worker loop's queue.
40 void DoCancel(); 39 void DoCancel();
41 40
42 // Task that regularly calls |read_cb_| according to the playback rate as 41 // Task that regularly calls |worker_cb_| according to the playback rate as
43 // determined by the audio parameters given during construction. Runs on 42 // determined by the audio parameters given during construction. Runs on
44 // the worker loop. 43 // the worker loop.
45 void DoRead(); 44 void DoRead();
46 45
47 const scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner_; 46 const scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner_;
48 const scoped_ptr<AudioBus> audio_bus_;
49 const base::TimeDelta buffer_duration_; 47 const base::TimeDelta buffer_duration_;
50 48
51 base::Lock read_cb_lock_; // Held while mutating or running |read_cb_|. 49 base::Lock worker_cb_lock_; // Held while mutating or running |worker_cb_|.
52 ReadCB read_cb_; 50 base::Closure worker_cb_;
53 base::TimeTicks next_read_time_; 51 base::TimeTicks next_read_time_;
54 52
55 // Used to cancel any delayed tasks still inside the worker loop's queue. 53 // Used to cancel any delayed tasks still inside the worker loop's queue.
56 base::CancelableClosure read_task_cb_; 54 base::CancelableClosure worker_task_cb_;
57 55
58 base::ThreadChecker thread_checker_; 56 base::ThreadChecker thread_checker_;
59 57
60 DISALLOW_COPY_AND_ASSIGN(Worker); 58 DISALLOW_COPY_AND_ASSIGN(Worker);
61 }; 59 };
62 60
63 FakeAudioConsumer::FakeAudioConsumer( 61 FakeAudioWorker::FakeAudioWorker(
64 const scoped_refptr<base::SingleThreadTaskRunner>& worker_task_runner, 62 const scoped_refptr<base::SingleThreadTaskRunner>& worker_task_runner,
65 const AudioParameters& params) 63 const AudioParameters& params)
66 : worker_(new Worker(worker_task_runner, params)) { 64 : worker_(new Worker(worker_task_runner, params)) {
67 } 65 }
68 66
69 FakeAudioConsumer::~FakeAudioConsumer() { 67 FakeAudioWorker::~FakeAudioWorker() {
70 DCHECK(worker_->IsStopped()); 68 DCHECK(worker_->IsStopped());
71 } 69 }
72 70
73 void FakeAudioConsumer::Start(const ReadCB& read_cb) { 71 void FakeAudioWorker::Start(const base::Closure& worker_cb) {
74 DCHECK(worker_->IsStopped()); 72 DCHECK(worker_->IsStopped());
75 worker_->Start(read_cb); 73 worker_->Start(worker_cb);
76 } 74 }
77 75
78 void FakeAudioConsumer::Stop() { 76 void FakeAudioWorker::Stop() {
79 worker_->Stop(); 77 worker_->Stop();
80 } 78 }
81 79
82 FakeAudioConsumer::Worker::Worker( 80 FakeAudioWorker::Worker::Worker(
83 const scoped_refptr<base::SingleThreadTaskRunner>& worker_task_runner, 81 const scoped_refptr<base::SingleThreadTaskRunner>& worker_task_runner,
84 const AudioParameters& params) 82 const AudioParameters& params)
85 : worker_task_runner_(worker_task_runner), 83 : worker_task_runner_(worker_task_runner),
86 audio_bus_(AudioBus::Create(params)),
87 buffer_duration_(base::TimeDelta::FromMicroseconds( 84 buffer_duration_(base::TimeDelta::FromMicroseconds(
88 params.frames_per_buffer() * base::Time::kMicrosecondsPerSecond / 85 params.frames_per_buffer() * base::Time::kMicrosecondsPerSecond /
89 static_cast<float>(params.sample_rate()))) { 86 static_cast<float>(params.sample_rate()))) {
90 audio_bus_->Zero();
91
92 // Worker can be constructed on any thread, but will DCHECK that its 87 // Worker can be constructed on any thread, but will DCHECK that its
93 // Start/Stop methods are called from the same thread. 88 // Start/Stop methods are called from the same thread.
94 thread_checker_.DetachFromThread(); 89 thread_checker_.DetachFromThread();
95 } 90 }
96 91
97 FakeAudioConsumer::Worker::~Worker() { 92 FakeAudioWorker::Worker::~Worker() {
98 DCHECK(read_cb_.is_null()); 93 DCHECK(worker_cb_.is_null());
99 } 94 }
100 95
101 bool FakeAudioConsumer::Worker::IsStopped() { 96 bool FakeAudioWorker::Worker::IsStopped() {
102 base::AutoLock scoped_lock(read_cb_lock_); 97 base::AutoLock scoped_lock(worker_cb_lock_);
103 return read_cb_.is_null(); 98 return worker_cb_.is_null();
104 } 99 }
105 100
106 void FakeAudioConsumer::Worker::Start(const ReadCB& read_cb) { 101 void FakeAudioWorker::Worker::Start(const base::Closure& worker_cb) {
107 DCHECK(thread_checker_.CalledOnValidThread()); 102 DCHECK(thread_checker_.CalledOnValidThread());
108 DCHECK(!read_cb.is_null()); 103 DCHECK(!worker_cb.is_null());
109 { 104 {
110 base::AutoLock scoped_lock(read_cb_lock_); 105 base::AutoLock scoped_lock(worker_cb_lock_);
111 DCHECK(read_cb_.is_null()); 106 DCHECK(worker_cb_.is_null());
112 read_cb_ = read_cb; 107 worker_cb_ = worker_cb;
113 } 108 }
114 worker_task_runner_->PostTask(FROM_HERE, base::Bind(&Worker::DoStart, this)); 109 worker_task_runner_->PostTask(FROM_HERE, base::Bind(&Worker::DoStart, this));
115 } 110 }
116 111
117 void FakeAudioConsumer::Worker::DoStart() { 112 void FakeAudioWorker::Worker::DoStart() {
118 DCHECK(worker_task_runner_->BelongsToCurrentThread()); 113 DCHECK(worker_task_runner_->BelongsToCurrentThread());
119 next_read_time_ = base::TimeTicks::Now(); 114 next_read_time_ = base::TimeTicks::Now();
120 read_task_cb_.Reset(base::Bind(&Worker::DoRead, this)); 115 worker_task_cb_.Reset(base::Bind(&Worker::DoRead, this));
121 read_task_cb_.callback().Run(); 116 worker_task_cb_.callback().Run();
122 } 117 }
123 118
124 void FakeAudioConsumer::Worker::Stop() { 119 void FakeAudioWorker::Worker::Stop() {
125 DCHECK(thread_checker_.CalledOnValidThread()); 120 DCHECK(thread_checker_.CalledOnValidThread());
126 { 121 {
127 base::AutoLock scoped_lock(read_cb_lock_); 122 base::AutoLock scoped_lock(worker_cb_lock_);
128 if (read_cb_.is_null()) 123 if (worker_cb_.is_null())
129 return; 124 return;
130 read_cb_.Reset(); 125 worker_cb_.Reset();
131 } 126 }
132 worker_task_runner_->PostTask(FROM_HERE, base::Bind(&Worker::DoCancel, this)); 127 worker_task_runner_->PostTask(FROM_HERE, base::Bind(&Worker::DoCancel, this));
133 } 128 }
134 129
135 void FakeAudioConsumer::Worker::DoCancel() { 130 void FakeAudioWorker::Worker::DoCancel() {
136 DCHECK(worker_task_runner_->BelongsToCurrentThread()); 131 DCHECK(worker_task_runner_->BelongsToCurrentThread());
137 read_task_cb_.Cancel(); 132 worker_task_cb_.Cancel();
138 } 133 }
139 134
140 void FakeAudioConsumer::Worker::DoRead() { 135 void FakeAudioWorker::Worker::DoRead() {
141 DCHECK(worker_task_runner_->BelongsToCurrentThread()); 136 DCHECK(worker_task_runner_->BelongsToCurrentThread());
142 137
143 { 138 {
144 base::AutoLock scoped_lock(read_cb_lock_); 139 base::AutoLock scoped_lock(worker_cb_lock_);
145 if (!read_cb_.is_null()) 140 if (!worker_cb_.is_null())
146 read_cb_.Run(audio_bus_.get()); 141 worker_cb_.Run();
147 } 142 }
148 143
149 // Need to account for time spent here due to the cost of |read_cb_| as well 144 // Need to account for time spent here due to the cost of |worker_cb| as well
150 // as the imprecision of PostDelayedTask(). 145 // as the imprecision of PostDelayedTask().
151 const base::TimeTicks now = base::TimeTicks::Now(); 146 const base::TimeTicks now = base::TimeTicks::Now();
152 base::TimeDelta delay = next_read_time_ + buffer_duration_ - now; 147 base::TimeDelta delay = next_read_time_ + buffer_duration_ - now;
153 148
154 // If we're behind, find the next nearest ontime interval. 149 // If we're behind, find the next nearest ontime interval.
155 if (delay < base::TimeDelta()) 150 if (delay < base::TimeDelta())
156 delay += buffer_duration_ * (-delay / buffer_duration_ + 1); 151 delay += buffer_duration_ * (-delay / buffer_duration_ + 1);
157 next_read_time_ = now + delay; 152 next_read_time_ = now + delay;
158 153
159 worker_task_runner_->PostDelayedTask( 154 worker_task_runner_->PostDelayedTask(
160 FROM_HERE, read_task_cb_.callback(), delay); 155 FROM_HERE, worker_task_cb_.callback(), delay);
161 } 156 }
162 157
163 } // namespace media 158 } // namespace media
OLDNEW
« no previous file with comments | « media/audio/fake_audio_worker.h ('k') | media/audio/fake_audio_worker_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698