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

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

Powered by Google App Engine
This is Rietveld 408576698