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

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

Issue 17334005: Re-land r207105 (Mac audio capture threading fix) with unit test memory leak issue resolved. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Using ref-counting (Patch Set 2 from Issue 17122006). Created 7 years, 6 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 | Annotate | Revision Log
« no previous file with comments | « media/audio/fake_audio_consumer.h ('k') | media/audio/fake_audio_consumer_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_consumer.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/logging.h" 10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop.h" 12 #include "base/message_loop.h"
11 #include "base/message_loop/message_loop_proxy.h" 13 #include "base/message_loop/message_loop_proxy.h"
14 #include "base/synchronization/lock.h"
15 #include "base/threading/thread_checker.h"
16 #include "base/time.h"
17 #include "media/audio/audio_parameters.h"
12 #include "media/base/audio_bus.h" 18 #include "media/base/audio_bus.h"
13 19
14 namespace media { 20 namespace media {
15 21
22 class FakeAudioConsumer::Worker
23 : public base::RefCountedThreadSafe<FakeAudioConsumer::Worker> {
24 public:
25 Worker(const scoped_refptr<base::MessageLoopProxy>& worker_loop,
26 const AudioParameters& params);
27
28 bool IsStopped();
29 void Start(const ReadCB& read_cb);
30 void Stop();
31
32 private:
33 friend class base::RefCountedThreadSafe<Worker>;
34 ~Worker();
35
36 // Initialize and start regular calls to DoRead() on the worker thread.
37 void DoStart();
38
39 // Cancel any delayed callbacks to DoRead() in the worker loop's queue.
40 void DoCancel();
41
42 // Task that regularly calls |read_cb_| according to the playback rate as
43 // determined by the audio parameters given during construction. Runs on
44 // the worker loop.
45 void DoRead();
46
47 const scoped_refptr<base::MessageLoopProxy> worker_loop_;
48 const scoped_ptr<AudioBus> audio_bus_;
49 const base::TimeDelta buffer_duration_;
50
51 base::Lock read_cb_lock_; // Held while mutating or running |read_cb_|.
52 ReadCB read_cb_;
53 base::TimeTicks next_read_time_;
54
55 // Used to cancel any delayed tasks still inside the worker loop's queue.
56 base::CancelableClosure read_task_cb_;
57
58 base::ThreadChecker thread_checker_;
59
60 DISALLOW_COPY_AND_ASSIGN(Worker);
61 };
62
16 FakeAudioConsumer::FakeAudioConsumer( 63 FakeAudioConsumer::FakeAudioConsumer(
17 const scoped_refptr<base::MessageLoopProxy>& message_loop, 64 const scoped_refptr<base::MessageLoopProxy>& worker_loop,
18 const AudioParameters& params) 65 const AudioParameters& params)
19 : message_loop_(message_loop), 66 : worker_(new Worker(worker_loop, params)) {
67 }
68
69 FakeAudioConsumer::~FakeAudioConsumer() {
70 DCHECK(worker_->IsStopped());
71 }
72
73 void FakeAudioConsumer::Start(const ReadCB& read_cb) {
74 DCHECK(worker_->IsStopped());
75 worker_->Start(read_cb);
76 }
77
78 void FakeAudioConsumer::Stop() {
79 worker_->Stop();
80 }
81
82 FakeAudioConsumer::Worker::Worker(
83 const scoped_refptr<base::MessageLoopProxy>& worker_loop,
84 const AudioParameters& params)
85 : worker_loop_(worker_loop),
20 audio_bus_(AudioBus::Create(params)), 86 audio_bus_(AudioBus::Create(params)),
21 buffer_duration_(base::TimeDelta::FromMicroseconds( 87 buffer_duration_(base::TimeDelta::FromMicroseconds(
22 params.frames_per_buffer() * base::Time::kMicrosecondsPerSecond / 88 params.frames_per_buffer() * base::Time::kMicrosecondsPerSecond /
23 static_cast<float>(params.sample_rate()))) { 89 static_cast<float>(params.sample_rate()))) {
24 audio_bus_->Zero(); 90 audio_bus_->Zero();
91
92 // Worker can be constructed on any thread, but will DCHECK that its
93 // Start/Stop methods are called from the same thread.
94 thread_checker_.DetachFromThread();
25 } 95 }
26 96
27 FakeAudioConsumer::~FakeAudioConsumer() { 97 FakeAudioConsumer::Worker::~Worker() {
28 DCHECK(read_cb_.is_null()); 98 DCHECK(read_cb_.is_null());
29 } 99 }
30 100
31 void FakeAudioConsumer::Start(const ReadCB& read_cb) { 101 bool FakeAudioConsumer::Worker::IsStopped() {
32 DCHECK(message_loop_->BelongsToCurrentThread()); 102 base::AutoLock scoped_lock(read_cb_lock_);
33 DCHECK(read_cb_.is_null()); 103 return read_cb_.is_null();
34 DCHECK(!read_cb.is_null());
35 read_cb_ = read_cb;
36 next_read_time_ = base::TimeTicks::Now();
37 read_task_cb_.Reset(base::Bind(
38 &FakeAudioConsumer::DoRead, base::Unretained(this)));
39 message_loop_->PostTask(FROM_HERE, read_task_cb_.callback());
40 } 104 }
41 105
42 void FakeAudioConsumer::Stop() { 106 void FakeAudioConsumer::Worker::Start(const ReadCB& read_cb) {
43 DCHECK(message_loop_->BelongsToCurrentThread()); 107 DCHECK(thread_checker_.CalledOnValidThread());
44 read_cb_.Reset(); 108 DCHECK(!read_cb.is_null());
109 {
110 base::AutoLock scoped_lock(read_cb_lock_);
111 DCHECK(read_cb_.is_null());
112 read_cb_ = read_cb;
113 }
114 worker_loop_->PostTask(FROM_HERE, base::Bind(&Worker::DoStart, this));
115 }
116
117 void FakeAudioConsumer::Worker::DoStart() {
118 DCHECK(worker_loop_->BelongsToCurrentThread());
119 next_read_time_ = base::TimeTicks::Now();
120 read_task_cb_.Reset(base::Bind(&Worker::DoRead, this));
121 read_task_cb_.callback().Run();
122 }
123
124 void FakeAudioConsumer::Worker::Stop() {
125 DCHECK(thread_checker_.CalledOnValidThread());
126 {
127 base::AutoLock scoped_lock(read_cb_lock_);
128 if (read_cb_.is_null())
129 return;
130 read_cb_.Reset();
131 }
132 worker_loop_->PostTask(FROM_HERE, base::Bind(&Worker::DoCancel, this));
133 }
134
135 void FakeAudioConsumer::Worker::DoCancel() {
136 DCHECK(worker_loop_->BelongsToCurrentThread());
45 read_task_cb_.Cancel(); 137 read_task_cb_.Cancel();
46 } 138 }
47 139
48 void FakeAudioConsumer::DoRead() { 140 void FakeAudioConsumer::Worker::DoRead() {
49 DCHECK(message_loop_->BelongsToCurrentThread()); 141 DCHECK(worker_loop_->BelongsToCurrentThread());
50 DCHECK(!read_cb_.is_null());
51 142
52 read_cb_.Run(audio_bus_.get()); 143 {
144 base::AutoLock scoped_lock(read_cb_lock_);
145 if (!read_cb_.is_null())
146 read_cb_.Run(audio_bus_.get());
147 }
53 148
54 // Need to account for time spent here due to the cost of |read_cb_| as well 149 // Need to account for time spent here due to the cost of |read_cb_| as well
55 // as the imprecision of PostDelayedTask(). 150 // as the imprecision of PostDelayedTask().
56 const base::TimeTicks now = base::TimeTicks::Now(); 151 const base::TimeTicks now = base::TimeTicks::Now();
57 base::TimeDelta delay = next_read_time_ + buffer_duration_ - now; 152 base::TimeDelta delay = next_read_time_ + buffer_duration_ - now;
58 153
59 // If we're behind, find the next nearest ontime interval. 154 // If we're behind, find the next nearest ontime interval.
60 if (delay < base::TimeDelta()) 155 if (delay < base::TimeDelta())
61 delay += buffer_duration_ * (-delay / buffer_duration_ + 1); 156 delay += buffer_duration_ * (-delay / buffer_duration_ + 1);
62 next_read_time_ = now + delay; 157 next_read_time_ = now + delay;
63 158
64 message_loop_->PostDelayedTask(FROM_HERE, read_task_cb_.callback(), delay); 159 worker_loop_->PostDelayedTask(FROM_HERE, read_task_cb_.callback(), delay);
65 } 160 }
66 161
67 } // namespace media 162 } // namespace media
OLDNEW
« no previous file with comments | « media/audio/fake_audio_consumer.h ('k') | media/audio/fake_audio_consumer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698