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

Side by Side Diff: media/audio/fake_audio_worker_unittest.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.cc ('k') | media/audio/null_audio_sink.h » ('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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "base/bind.h" 5 #include "base/bind.h"
6 #include "base/message_loop/message_loop.h" 6 #include "base/message_loop/message_loop.h"
7 #include "base/time/time.h" 7 #include "base/time/time.h"
8 #include "media/audio/audio_parameters.h" 8 #include "media/audio/audio_parameters.h"
9 #include "media/audio/fake_audio_consumer.h" 9 #include "media/audio/fake_audio_worker.h"
10 #include "media/audio/simple_sources.h" 10 #include "media/audio/simple_sources.h"
11 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
12 12
13 namespace media { 13 namespace media {
14 14
15 static const int kTestCallbacks = 5; 15 static const int kTestCallbacks = 5;
16 16
17 class FakeAudioConsumerTest : public testing::Test { 17 class FakeAudioWorkerTest : public testing::Test {
18 public: 18 public:
19 FakeAudioConsumerTest() 19 FakeAudioWorkerTest()
20 : params_( 20 : params_(
21 AudioParameters::AUDIO_FAKE, CHANNEL_LAYOUT_STEREO, 44100, 8, 128), 21 AudioParameters::AUDIO_FAKE, CHANNEL_LAYOUT_STEREO, 44100, 8, 128),
22 fake_consumer_(message_loop_.message_loop_proxy(), params_), 22 fake_worker_(message_loop_.message_loop_proxy(), params_),
23 source_(params_.channels(), 200.0, params_.sample_rate()) { 23 seen_callbacks_(0) {
24 time_between_callbacks_ = base::TimeDelta::FromMicroseconds( 24 time_between_callbacks_ = base::TimeDelta::FromMicroseconds(
25 params_.frames_per_buffer() * base::Time::kMicrosecondsPerSecond / 25 params_.frames_per_buffer() * base::Time::kMicrosecondsPerSecond /
26 static_cast<float>(params_.sample_rate())); 26 static_cast<float>(params_.sample_rate()));
27 } 27 }
28 28
29 ~FakeAudioConsumerTest() override {} 29 ~FakeAudioWorkerTest() override {}
30 30
31 void ConsumeData(AudioBus* audio_bus) { 31 void CalledByFakeWorker() {
32 source_.OnMoreData(audio_bus, 0); 32 seen_callbacks_++;
33 } 33 }
34 34
35 void RunOnAudioThread() { 35 void RunOnAudioThread() {
36 ASSERT_TRUE(message_loop_.message_loop_proxy()->BelongsToCurrentThread()); 36 ASSERT_TRUE(message_loop_.message_loop_proxy()->BelongsToCurrentThread());
37 fake_consumer_.Start(base::Bind( 37 fake_worker_.Start(base::Bind(
38 &FakeAudioConsumerTest::ConsumeData, base::Unretained(this))); 38 &FakeAudioWorkerTest::CalledByFakeWorker, base::Unretained(this)));
39 } 39 }
40 40
41 void RunOnceOnAudioThread() { 41 void RunOnceOnAudioThread() {
42 ASSERT_TRUE(message_loop_.message_loop_proxy()->BelongsToCurrentThread()); 42 ASSERT_TRUE(message_loop_.message_loop_proxy()->BelongsToCurrentThread());
43 RunOnAudioThread(); 43 RunOnAudioThread();
44 // Start() should immediately post a task to run the source callback, so we 44 // Start() should immediately post a task to run the callback, so we
45 // should end up with only a single callback being run. 45 // should end up with only a single callback being run.
46 message_loop_.PostTask(FROM_HERE, base::Bind( 46 message_loop_.PostTask(FROM_HERE, base::Bind(
47 &FakeAudioConsumerTest::EndTest, base::Unretained(this), 1)); 47 &FakeAudioWorkerTest::EndTest, base::Unretained(this), 1));
48 } 48 }
49 49
50 void StopStartOnAudioThread() { 50 void StopStartOnAudioThread() {
51 ASSERT_TRUE(message_loop_.message_loop_proxy()->BelongsToCurrentThread()); 51 ASSERT_TRUE(message_loop_.message_loop_proxy()->BelongsToCurrentThread());
52 fake_consumer_.Stop(); 52 fake_worker_.Stop();
53 RunOnAudioThread(); 53 RunOnAudioThread();
54 } 54 }
55 55
56 void TimeCallbacksOnAudioThread(int callbacks) { 56 void TimeCallbacksOnAudioThread(int callbacks) {
57 ASSERT_TRUE(message_loop_.message_loop_proxy()->BelongsToCurrentThread()); 57 ASSERT_TRUE(message_loop_.message_loop_proxy()->BelongsToCurrentThread());
58 58
59 if (source_.callbacks() == 0) { 59 if (seen_callbacks_ == 0) {
60 RunOnAudioThread(); 60 RunOnAudioThread();
61 start_time_ = base::TimeTicks::Now(); 61 start_time_ = base::TimeTicks::Now();
62 } 62 }
63 63
64 // Keep going until we've seen the requested number of callbacks. 64 // Keep going until we've seen the requested number of callbacks.
65 if (source_.callbacks() < callbacks) { 65 if (seen_callbacks_ < callbacks) {
66 message_loop_.PostDelayedTask(FROM_HERE, base::Bind( 66 message_loop_.PostDelayedTask(FROM_HERE, base::Bind(
67 &FakeAudioConsumerTest::TimeCallbacksOnAudioThread, 67 &FakeAudioWorkerTest::TimeCallbacksOnAudioThread,
68 base::Unretained(this), callbacks), time_between_callbacks_ / 2); 68 base::Unretained(this), callbacks), time_between_callbacks_ / 2);
69 } else { 69 } else {
70 end_time_ = base::TimeTicks::Now(); 70 end_time_ = base::TimeTicks::Now();
71 EndTest(callbacks); 71 EndTest(callbacks);
72 } 72 }
73 } 73 }
74 74
75 void EndTest(int callbacks) { 75 void EndTest(int callbacks) {
76 ASSERT_TRUE(message_loop_.message_loop_proxy()->BelongsToCurrentThread()); 76 ASSERT_TRUE(message_loop_.message_loop_proxy()->BelongsToCurrentThread());
77 fake_consumer_.Stop(); 77 fake_worker_.Stop();
78 EXPECT_LE(callbacks, source_.callbacks()); 78 EXPECT_LE(callbacks, seen_callbacks_);
79 message_loop_.PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); 79 message_loop_.PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
80 } 80 }
81 81
82 protected: 82 protected:
83 base::MessageLoop message_loop_; 83 base::MessageLoop message_loop_;
84 AudioParameters params_; 84 AudioParameters params_;
85 FakeAudioConsumer fake_consumer_; 85 FakeAudioWorker fake_worker_;
86 SineWaveAudioSource source_;
87 base::TimeTicks start_time_; 86 base::TimeTicks start_time_;
88 base::TimeTicks end_time_; 87 base::TimeTicks end_time_;
89 base::TimeDelta time_between_callbacks_; 88 base::TimeDelta time_between_callbacks_;
89 int seen_callbacks_;
90 90
91 private: 91 private:
92 DISALLOW_COPY_AND_ASSIGN(FakeAudioConsumerTest); 92 DISALLOW_COPY_AND_ASSIGN(FakeAudioWorkerTest);
93 }; 93 };
94 94
95 // Ensure the fake audio stream runs on the audio thread and handles fires 95 // Ensure the worker runs on the audio thread and fires callbacks.
96 // callbacks to the AudioSourceCallback. 96 TEST_F(FakeAudioWorkerTest, FakeBasicCallback) {
97 TEST_F(FakeAudioConsumerTest, FakeStreamBasicCallback) {
98 message_loop_.PostTask(FROM_HERE, base::Bind( 97 message_loop_.PostTask(FROM_HERE, base::Bind(
99 &FakeAudioConsumerTest::RunOnceOnAudioThread, 98 &FakeAudioWorkerTest::RunOnceOnAudioThread,
100 base::Unretained(this))); 99 base::Unretained(this)));
101 message_loop_.Run(); 100 message_loop_.Run();
102 } 101 }
103 102
104 // Ensure the time between callbacks is sane. 103 // Ensure the time between callbacks is sane.
105 TEST_F(FakeAudioConsumerTest, TimeBetweenCallbacks) { 104 TEST_F(FakeAudioWorkerTest, TimeBetweenCallbacks) {
106 message_loop_.PostTask(FROM_HERE, base::Bind( 105 message_loop_.PostTask(FROM_HERE, base::Bind(
107 &FakeAudioConsumerTest::TimeCallbacksOnAudioThread, 106 &FakeAudioWorkerTest::TimeCallbacksOnAudioThread,
108 base::Unretained(this), kTestCallbacks)); 107 base::Unretained(this), kTestCallbacks));
109 message_loop_.Run(); 108 message_loop_.Run();
110 109
111 // There are only (kTestCallbacks - 1) intervals between kTestCallbacks. 110 // There are only (kTestCallbacks - 1) intervals between kTestCallbacks.
112 base::TimeDelta actual_time_between_callbacks = 111 base::TimeDelta actual_time_between_callbacks =
113 (end_time_ - start_time_) / (source_.callbacks() - 1); 112 (end_time_ - start_time_) / (seen_callbacks_ - 1);
114 113
115 // Ensure callback time is no faster than the expected time between callbacks. 114 // Ensure callback time is no faster than the expected time between callbacks.
116 EXPECT_TRUE(actual_time_between_callbacks >= time_between_callbacks_); 115 EXPECT_GE(actual_time_between_callbacks, time_between_callbacks_);
117 116
118 // Softly check if the callback time is no slower than twice the expected time 117 // Softly check if the callback time is no slower than twice the expected time
119 // between callbacks. Since this test runs on the bots we can't be too strict 118 // between callbacks. Since this test runs on the bots we can't be too strict
120 // with the bounds. 119 // with the bounds.
121 if (actual_time_between_callbacks > 2 * time_between_callbacks_) 120 if (actual_time_between_callbacks > 2 * time_between_callbacks_)
122 LOG(ERROR) << "Time between fake audio callbacks is too large!"; 121 LOG(ERROR) << "Time between fake audio callbacks is too large!";
123 } 122 }
124 123
125 // Ensure Start()/Stop() on the stream doesn't generate too many callbacks. See 124 // Ensure Start()/Stop() on the worker doesn't generate too many callbacks. See
126 // http://crbug.com/159049 125 // http://crbug.com/159049.
127 TEST_F(FakeAudioConsumerTest, StartStopClearsCallbacks) { 126 TEST_F(FakeAudioWorkerTest, StartStopClearsCallbacks) {
128 message_loop_.PostTask(FROM_HERE, base::Bind( 127 message_loop_.PostTask(FROM_HERE, base::Bind(
129 &FakeAudioConsumerTest::TimeCallbacksOnAudioThread, 128 &FakeAudioWorkerTest::TimeCallbacksOnAudioThread,
130 base::Unretained(this), kTestCallbacks)); 129 base::Unretained(this), kTestCallbacks));
131 130
132 // Issue a Stop() / Start() in between expected callbacks to maximize the 131 // Issue a Stop() / Start() in between expected callbacks to maximize the
133 // chance of catching the FakeAudioOutputStream doing the wrong thing. 132 // chance of catching the worker doing the wrong thing.
134 message_loop_.PostDelayedTask(FROM_HERE, base::Bind( 133 message_loop_.PostDelayedTask(FROM_HERE, base::Bind(
135 &FakeAudioConsumerTest::StopStartOnAudioThread, 134 &FakeAudioWorkerTest::StopStartOnAudioThread,
136 base::Unretained(this)), time_between_callbacks_ / 2); 135 base::Unretained(this)), time_between_callbacks_ / 2);
137 136
138 // EndTest() will ensure the proper number of callbacks have occurred. 137 // EndTest() will ensure the proper number of callbacks have occurred.
139 message_loop_.Run(); 138 message_loop_.Run();
140 } 139 }
141 140
142 } // namespace media 141 } // namespace media
OLDNEW
« no previous file with comments | « media/audio/fake_audio_worker.cc ('k') | media/audio/null_audio_sink.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698