crazy linker: convert relocation unpacking to Android style.
[chromium-blink-merge.git] / media / audio / fake_audio_worker.cc
blob44177d385eca7d0304fb001c8b3631dfd2f3c334
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
3 // found in the LICENSE file.
5 #include "media/audio/fake_audio_worker.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/cancelable_callback.h"
10 #include "base/location.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/synchronization/lock.h"
15 #include "base/threading/thread_checker.h"
16 #include "base/time/time.h"
17 #include "media/audio/audio_parameters.h"
19 namespace media {
21 class FakeAudioWorker::Worker
22 : public base::RefCountedThreadSafe<FakeAudioWorker::Worker> {
23 public:
24 Worker(const scoped_refptr<base::SingleThreadTaskRunner>& worker_task_runner,
25 const AudioParameters& params);
27 bool IsStopped();
28 void Start(const base::Closure& worker_cb);
29 void Stop();
31 private:
32 friend class base::RefCountedThreadSafe<Worker>;
33 ~Worker();
35 // Initialize and start regular calls to DoRead() on the worker thread.
36 void DoStart();
38 // Cancel any delayed callbacks to DoRead() in the worker loop's queue.
39 void DoCancel();
41 // Task that regularly calls |worker_cb_| according to the playback rate as
42 // determined by the audio parameters given during construction. Runs on
43 // the worker loop.
44 void DoRead();
46 const scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner_;
47 const base::TimeDelta buffer_duration_;
49 base::Lock worker_cb_lock_; // Held while mutating or running |worker_cb_|.
50 base::Closure worker_cb_;
51 base::TimeTicks next_read_time_;
53 // Used to cancel any delayed tasks still inside the worker loop's queue.
54 base::CancelableClosure worker_task_cb_;
56 base::ThreadChecker thread_checker_;
58 DISALLOW_COPY_AND_ASSIGN(Worker);
61 FakeAudioWorker::FakeAudioWorker(
62 const scoped_refptr<base::SingleThreadTaskRunner>& worker_task_runner,
63 const AudioParameters& params)
64 : worker_(new Worker(worker_task_runner, params)) {
67 FakeAudioWorker::~FakeAudioWorker() {
68 DCHECK(worker_->IsStopped());
71 void FakeAudioWorker::Start(const base::Closure& worker_cb) {
72 DCHECK(worker_->IsStopped());
73 worker_->Start(worker_cb);
76 void FakeAudioWorker::Stop() {
77 worker_->Stop();
80 FakeAudioWorker::Worker::Worker(
81 const scoped_refptr<base::SingleThreadTaskRunner>& worker_task_runner,
82 const AudioParameters& params)
83 : worker_task_runner_(worker_task_runner),
84 buffer_duration_(base::TimeDelta::FromMicroseconds(
85 params.frames_per_buffer() * base::Time::kMicrosecondsPerSecond /
86 static_cast<float>(params.sample_rate()))) {
87 // Worker can be constructed on any thread, but will DCHECK that its
88 // Start/Stop methods are called from the same thread.
89 thread_checker_.DetachFromThread();
92 FakeAudioWorker::Worker::~Worker() {
93 DCHECK(worker_cb_.is_null());
96 bool FakeAudioWorker::Worker::IsStopped() {
97 base::AutoLock scoped_lock(worker_cb_lock_);
98 return worker_cb_.is_null();
101 void FakeAudioWorker::Worker::Start(const base::Closure& worker_cb) {
102 DCHECK(thread_checker_.CalledOnValidThread());
103 DCHECK(!worker_cb.is_null());
105 base::AutoLock scoped_lock(worker_cb_lock_);
106 DCHECK(worker_cb_.is_null());
107 worker_cb_ = worker_cb;
109 worker_task_runner_->PostTask(FROM_HERE, base::Bind(&Worker::DoStart, this));
112 void FakeAudioWorker::Worker::DoStart() {
113 DCHECK(worker_task_runner_->BelongsToCurrentThread());
114 next_read_time_ = base::TimeTicks::Now();
115 worker_task_cb_.Reset(base::Bind(&Worker::DoRead, this));
116 worker_task_cb_.callback().Run();
119 void FakeAudioWorker::Worker::Stop() {
120 DCHECK(thread_checker_.CalledOnValidThread());
122 base::AutoLock scoped_lock(worker_cb_lock_);
123 if (worker_cb_.is_null())
124 return;
125 worker_cb_.Reset();
127 worker_task_runner_->PostTask(FROM_HERE, base::Bind(&Worker::DoCancel, this));
130 void FakeAudioWorker::Worker::DoCancel() {
131 DCHECK(worker_task_runner_->BelongsToCurrentThread());
132 worker_task_cb_.Cancel();
135 void FakeAudioWorker::Worker::DoRead() {
136 DCHECK(worker_task_runner_->BelongsToCurrentThread());
139 base::AutoLock scoped_lock(worker_cb_lock_);
140 if (!worker_cb_.is_null())
141 worker_cb_.Run();
144 // Need to account for time spent here due to the cost of |worker_cb| as well
145 // as the imprecision of PostDelayedTask().
146 const base::TimeTicks now = base::TimeTicks::Now();
147 base::TimeDelta delay = next_read_time_ + buffer_duration_ - now;
149 // If we're behind, find the next nearest ontime interval.
150 if (delay < base::TimeDelta())
151 delay += buffer_duration_ * (-delay / buffer_duration_ + 1);
152 next_read_time_ = now + delay;
154 worker_task_runner_->PostDelayedTask(
155 FROM_HERE, worker_task_cb_.callback(), delay);
158 } // namespace media