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

Side by Side Diff: webrtc/modules/audio_device/ios/audio_device_unittest_ios.cc

Issue 1206783002: Cleanup of iOS AudioDevice implementation (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: More cleanup Created 5 years, 5 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 /* 1 /*
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include <algorithm> 11 #include <algorithm>
12 #include <limits> 12 #include <limits>
13 #include <list> 13 #include <list>
14 #include <numeric> 14 #include <numeric>
15 #include <string> 15 #include <string>
16 #include <vector> 16 #include <vector>
17 17
18 #include "testing/gmock/include/gmock/gmock.h" 18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "webrtc/base/arraysize.h" 20 #include "webrtc/base/arraysize.h"
21 #include "webrtc/base/criticalsection.h" 21 #include "webrtc/base/criticalsection.h"
22 #include "webrtc/base/scoped_ptr.h" 22 #include "webrtc/base/scoped_ptr.h"
23 #include "webrtc/base/scoped_ref_ptr.h" 23 #include "webrtc/base/scoped_ref_ptr.h"
24 #include "webrtc/modules/audio_device/android/audio_common.h"
25 #include "webrtc/modules/audio_device/android/audio_manager.h"
26 #include "webrtc/modules/audio_device/android/build_info.h"
27 #include "webrtc/modules/audio_device/android/ensure_initialized.h"
28 #include "webrtc/modules/audio_device/audio_device_impl.h" 24 #include "webrtc/modules/audio_device/audio_device_impl.h"
29 #include "webrtc/modules/audio_device/include/audio_device.h" 25 #include "webrtc/modules/audio_device/include/audio_device.h"
26 #include "webrtc/modules/audio_device/ios/audio_device_ios.h"
30 #include "webrtc/system_wrappers/interface/clock.h" 27 #include "webrtc/system_wrappers/interface/clock.h"
31 #include "webrtc/system_wrappers/interface/event_wrapper.h" 28 #include "webrtc/system_wrappers/interface/event_wrapper.h"
32 #include "webrtc/system_wrappers/interface/sleep.h" 29 #include "webrtc/system_wrappers/interface/sleep.h"
33 #include "webrtc/test/testsupport/fileutils.h" 30 #include "webrtc/test/testsupport/fileutils.h"
34 31
35 using std::cout; 32 using std::cout;
36 using std::endl; 33 using std::endl;
37 using ::testing::_; 34 using ::testing::_;
38 using ::testing::AtLeast; 35 using ::testing::AtLeast;
39 using ::testing::Gt; 36 using ::testing::Gt;
40 using ::testing::Invoke; 37 using ::testing::Invoke;
41 using ::testing::NiceMock; 38 using ::testing::NiceMock;
42 using ::testing::NotNull; 39 using ::testing::NotNull;
43 using ::testing::Return; 40 using ::testing::Return;
44 using ::testing::TestWithParam;
45 41
46 // #define ENABLE_DEBUG_PRINTF 42 // #define ENABLE_DEBUG_PRINTF
47 #ifdef ENABLE_DEBUG_PRINTF 43 #ifdef ENABLE_DEBUG_PRINTF
48 #define PRINTD(...) fprintf(stderr, __VA_ARGS__); 44 #define PRINTD(...) fprintf(stderr, __VA_ARGS__);
49 #else 45 #else
50 #define PRINTD(...) ((void)0) 46 #define PRINTD(...) ((void)0)
51 #endif 47 #endif
52 #define PRINT(...) fprintf(stderr, __VA_ARGS__); 48 #define PRINT(...) fprintf(stderr, __VA_ARGS__);
53 49
54 namespace webrtc { 50 namespace webrtc {
55 51
56 // Number of callbacks (input or output) the tests waits for before we set 52 // Number of callbacks (input or output) the tests waits for before we set
57 // an event indicating that the test was OK. 53 // an event indicating that the test was OK.
58 static const int kNumCallbacks = 10; 54 static const int kNumCallbacks = 10;
59 // Max amount of time we wait for an event to be set while counting callbacks. 55 // Max amount of time we wait for an event to be set while counting callbacks.
60 static const int kTestTimeOutInMilliseconds = 10 * 1000; 56 static const int kTestTimeOutInMilliseconds = 10 * 1000;
57 // Number of bits per PCM audio sample.
58 static const int kBitsPerSample = 16;
59 // Number of bytes per PCM audio sample.
60 static const int kBytesPerSample = kBitsPerSample / 8;
61 // Average number of audio callbacks per second assuming 10ms packet size. 61 // Average number of audio callbacks per second assuming 10ms packet size.
62 static const int kNumCallbacksPerSecond = 100; 62 static const int kNumCallbacksPerSecond = 100;
63 // Play out a test file during this time (unit is in seconds). 63 // Play out a test file during this time (unit is in seconds).
64 static const int kFilePlayTimeInSec = 5; 64 static const int kFilePlayTimeInSec = 15;
65 static const int kBitsPerSample = 16;
66 static const int kBytesPerSample = kBitsPerSample / 8;
67 // Run the full-duplex test during this time (unit is in seconds). 65 // Run the full-duplex test during this time (unit is in seconds).
68 // Note that first |kNumIgnoreFirstCallbacks| are ignored. 66 // Note that first |kNumIgnoreFirstCallbacks| are ignored.
69 static const int kFullDuplexTimeInSec = 5; 67 static const int kFullDuplexTimeInSec = 10;
70 // Wait for the callback sequence to stabilize by ignoring this amount of the 68 // Wait for the callback sequence to stabilize by ignoring this amount of the
71 // initial callbacks (avoids initial FIFO access). 69 // initial callbacks (avoids initial FIFO access).
72 // Only used in the RunPlayoutAndRecordingInFullDuplex test. 70 // Only used in the RunPlayoutAndRecordingInFullDuplex test.
73 static const int kNumIgnoreFirstCallbacks = 50; 71 static const int kNumIgnoreFirstCallbacks = 50;
74 // Sets the number of impulses per second in the latency test. 72 // Sets the number of impulses per second in the latency test.
75 static const int kImpulseFrequencyInHz = 1; 73 // TODO(henrika): fine tune this setting for iOS.
74 static const int kImpulseFrequencyInHz = 2;
76 // Length of round-trip latency measurements. Number of transmitted impulses 75 // Length of round-trip latency measurements. Number of transmitted impulses
77 // is kImpulseFrequencyInHz * kMeasureLatencyTimeInSec - 1. 76 // is kImpulseFrequencyInHz * kMeasureLatencyTimeInSec - 1.
78 static const int kMeasureLatencyTimeInSec = 11; 77 // TODO(henrika): fine tune this setting for iOS.
78 static const int kMeasureLatencyTimeInSec = 3;
79 // Utilized in round-trip latency measurements to avoid capturing noise samples. 79 // Utilized in round-trip latency measurements to avoid capturing noise samples.
80 static const int kImpulseThreshold = 1000; 80 // TODO(henrika): fine tune this setting for iOS.
81 static const int kImpulseThreshold = 100;
81 static const char kTag[] = "[..........] "; 82 static const char kTag[] = "[..........] ";
82 83
83 enum TransportType { 84 enum TransportType {
84 kPlayout = 0x1, 85 kPlayout = 0x1,
85 kRecording = 0x2, 86 kRecording = 0x2,
86 }; 87 };
87 88
88 // Interface for processing the audio stream. Real implementations can e.g. 89 // Interface for processing the audio stream. Real implementations can e.g.
89 // run audio in loopback, read audio from a file or perform latency 90 // run audio in loopback, read audio from a file or perform latency
90 // measurements. 91 // measurements.
91 class AudioStreamInterface { 92 class AudioStreamInterface {
92 public: 93 public:
93 virtual void Write(const void* source, int num_frames) = 0; 94 virtual void Write(const void* source, int num_frames) = 0;
94 virtual void Read(void* destination, int num_frames) = 0; 95 virtual void Read(void* destination, int num_frames) = 0;
96
95 protected: 97 protected:
96 virtual ~AudioStreamInterface() {} 98 virtual ~AudioStreamInterface() {}
97 }; 99 };
98 100
99 // Reads audio samples from a PCM file where the file is stored in memory at 101 // Reads audio samples from a PCM file where the file is stored in memory at
100 // construction. 102 // construction.
101 class FileAudioStream : public AudioStreamInterface { 103 class FileAudioStream : public AudioStreamInterface {
102 public: 104 public:
103 FileAudioStream( 105 FileAudioStream(int num_callbacks,
104 int num_callbacks, const std::string& file_name, int sample_rate) 106 const std::string& file_name,
105 : file_size_in_bytes_(0), 107 int sample_rate)
106 sample_rate_(sample_rate), 108 : file_size_in_bytes_(0), sample_rate_(sample_rate), file_pos_(0) {
107 file_pos_(0) {
108 file_size_in_bytes_ = test::GetFileSize(file_name); 109 file_size_in_bytes_ = test::GetFileSize(file_name);
109 sample_rate_ = sample_rate; 110 sample_rate_ = sample_rate;
110 EXPECT_GE(file_size_in_callbacks(), num_callbacks) 111 EXPECT_GE(file_size_in_callbacks(), num_callbacks)
111 << "Size of test file is not large enough to last during the test."; 112 << "Size of test file is not large enough to last during the test.";
112 const int num_16bit_samples = 113 const int num_16bit_samples =
113 test::GetFileSize(file_name) / kBytesPerSample; 114 test::GetFileSize(file_name) / kBytesPerSample;
114 file_.reset(new int16_t[num_16bit_samples]); 115 file_.reset(new int16_t[num_16bit_samples]);
115 FILE* audio_file = fopen(file_name.c_str(), "rb"); 116 FILE* audio_file = fopen(file_name.c_str(), "rb");
116 EXPECT_NE(audio_file, nullptr); 117 EXPECT_NE(audio_file, nullptr);
117 int num_samples_read = fread( 118 int num_samples_read =
118 file_.get(), sizeof(int16_t), num_16bit_samples, audio_file); 119 fread(file_.get(), sizeof(int16_t), num_16bit_samples, audio_file);
119 EXPECT_EQ(num_samples_read, num_16bit_samples); 120 EXPECT_EQ(num_samples_read, num_16bit_samples);
120 fclose(audio_file); 121 fclose(audio_file);
121 } 122 }
122 123
123 // AudioStreamInterface::Write() is not implemented. 124 // AudioStreamInterface::Write() is not implemented.
124 void Write(const void* source, int num_frames) override {} 125 void Write(const void* source, int num_frames) override {}
125 126
126 // Read samples from file stored in memory (at construction) and copy 127 // Read samples from file stored in memory (at construction) and copy
127 // |num_frames| (<=> 10ms) to the |destination| byte buffer. 128 // |num_frames| (<=> 10ms) to the |destination| byte buffer.
128 void Read(void* destination, int num_frames) override { 129 void Read(void* destination, int num_frames) override {
129 memcpy(destination, 130 memcpy(destination, static_cast<int16_t*>(&file_[file_pos_]),
130 static_cast<int16_t*> (&file_[file_pos_]),
131 num_frames * sizeof(int16_t)); 131 num_frames * sizeof(int16_t));
132 file_pos_ += num_frames; 132 file_pos_ += num_frames;
133 } 133 }
134 134
135 int file_size_in_seconds() const { 135 int file_size_in_seconds() const {
136 return (file_size_in_bytes_ / (kBytesPerSample * sample_rate_)); 136 return (file_size_in_bytes_ / (kBytesPerSample * sample_rate_));
137 } 137 }
138 int file_size_in_callbacks() const { 138 int file_size_in_callbacks() const {
139 return file_size_in_seconds() * kNumCallbacksPerSecond; 139 return file_size_in_seconds() * kNumCallbacksPerSecond;
140 } 140 }
(...skipping 18 matching lines...) Expand all
159 explicit FifoAudioStream(int frames_per_buffer) 159 explicit FifoAudioStream(int frames_per_buffer)
160 : frames_per_buffer_(frames_per_buffer), 160 : frames_per_buffer_(frames_per_buffer),
161 bytes_per_buffer_(frames_per_buffer_ * sizeof(int16_t)), 161 bytes_per_buffer_(frames_per_buffer_ * sizeof(int16_t)),
162 fifo_(new AudioBufferList), 162 fifo_(new AudioBufferList),
163 largest_size_(0), 163 largest_size_(0),
164 total_written_elements_(0), 164 total_written_elements_(0),
165 write_count_(0) { 165 write_count_(0) {
166 EXPECT_NE(fifo_.get(), nullptr); 166 EXPECT_NE(fifo_.get(), nullptr);
167 } 167 }
168 168
169 ~FifoAudioStream() { 169 ~FifoAudioStream() { Flush(); }
170 Flush();
171 }
172 170
173 // Allocate new memory, copy |num_frames| samples from |source| into memory 171 // Allocate new memory, copy |num_frames| samples from |source| into memory
174 // and add pointer to the memory location to end of the list. 172 // and add pointer to the memory location to end of the list.
175 // Increases the size of the FIFO by one element. 173 // Increases the size of the FIFO by one element.
176 void Write(const void* source, int num_frames) override { 174 void Write(const void* source, int num_frames) override {
177 ASSERT_EQ(num_frames, frames_per_buffer_); 175 ASSERT_EQ(num_frames, frames_per_buffer_);
178 PRINTD("+"); 176 PRINTD("+");
179 if (write_count_++ < kNumIgnoreFirstCallbacks) { 177 if (write_count_++ < kNumIgnoreFirstCallbacks) {
180 return; 178 return;
181 } 179 }
182 int16_t* memory = new int16_t[frames_per_buffer_]; 180 int16_t* memory = new int16_t[frames_per_buffer_];
183 memcpy(static_cast<int16_t*> (&memory[0]), 181 memcpy(static_cast<int16_t*>(&memory[0]), source, bytes_per_buffer_);
184 source,
185 bytes_per_buffer_);
186 rtc::CritScope lock(&lock_); 182 rtc::CritScope lock(&lock_);
187 fifo_->push_back(memory); 183 fifo_->push_back(memory);
188 const int size = fifo_->size(); 184 const int size = fifo_->size();
189 if (size > largest_size_) { 185 if (size > largest_size_) {
190 largest_size_ = size; 186 largest_size_ = size;
191 PRINTD("(%d)", largest_size_); 187 PRINTD("(%d)", largest_size_);
192 } 188 }
193 total_written_elements_ += size; 189 total_written_elements_ += size;
194 } 190 }
195 191
196 // Read pointer to data buffer from front of list, copy |num_frames| of stored 192 // Read pointer to data buffer from front of list, copy |num_frames| of stored
197 // data into |destination| and delete the utilized memory allocation. 193 // data into |destination| and delete the utilized memory allocation.
198 // Decreases the size of the FIFO by one element. 194 // Decreases the size of the FIFO by one element.
199 void Read(void* destination, int num_frames) override { 195 void Read(void* destination, int num_frames) override {
200 ASSERT_EQ(num_frames, frames_per_buffer_); 196 ASSERT_EQ(num_frames, frames_per_buffer_);
201 PRINTD("-"); 197 PRINTD("-");
202 rtc::CritScope lock(&lock_); 198 rtc::CritScope lock(&lock_);
203 if (fifo_->empty()) { 199 if (fifo_->empty()) {
204 memset(destination, 0, bytes_per_buffer_); 200 memset(destination, 0, bytes_per_buffer_);
205 } else { 201 } else {
206 int16_t* memory = fifo_->front(); 202 int16_t* memory = fifo_->front();
207 fifo_->pop_front(); 203 fifo_->pop_front();
208 memcpy(destination, 204 memcpy(destination, static_cast<int16_t*>(&memory[0]), bytes_per_buffer_);
209 static_cast<int16_t*> (&memory[0]),
210 bytes_per_buffer_);
211 delete memory; 205 delete memory;
212 } 206 }
213 } 207 }
214 208
215 int size() const { 209 int size() const { return fifo_->size(); }
216 return fifo_->size();
217 }
218 210
219 int largest_size() const { 211 int largest_size() const { return largest_size_; }
220 return largest_size_;
221 }
222 212
223 int average_size() const { 213 int average_size() const {
224 return (total_written_elements_ == 0) ? 0.0 : 0.5 + static_cast<float> ( 214 return (total_written_elements_ == 0)
225 total_written_elements_) / (write_count_ - kNumIgnoreFirstCallbacks); 215 ? 0.0
216 : 0.5 +
217 static_cast<float>(total_written_elements_) /
218 (write_count_ - kNumIgnoreFirstCallbacks);
226 } 219 }
227 220
228 private: 221 private:
229 void Flush() { 222 void Flush() {
230 for (auto it = fifo_->begin(); it != fifo_->end(); ++it) { 223 for (auto it = fifo_->begin(); it != fifo_->end(); ++it) {
231 delete *it; 224 delete *it;
232 } 225 }
233 fifo_->clear(); 226 fifo_->clear();
234 } 227 }
235 228
(...skipping 12 matching lines...) Expand all
248 // Usage requires a special hardware called Audio Loopback Dongle. 241 // Usage requires a special hardware called Audio Loopback Dongle.
249 // See http://source.android.com/devices/audio/loopback.html for details. 242 // See http://source.android.com/devices/audio/loopback.html for details.
250 class LatencyMeasuringAudioStream : public AudioStreamInterface { 243 class LatencyMeasuringAudioStream : public AudioStreamInterface {
251 public: 244 public:
252 explicit LatencyMeasuringAudioStream(int frames_per_buffer) 245 explicit LatencyMeasuringAudioStream(int frames_per_buffer)
253 : clock_(Clock::GetRealTimeClock()), 246 : clock_(Clock::GetRealTimeClock()),
254 frames_per_buffer_(frames_per_buffer), 247 frames_per_buffer_(frames_per_buffer),
255 bytes_per_buffer_(frames_per_buffer_ * sizeof(int16_t)), 248 bytes_per_buffer_(frames_per_buffer_ * sizeof(int16_t)),
256 play_count_(0), 249 play_count_(0),
257 rec_count_(0), 250 rec_count_(0),
258 pulse_time_(0) { 251 pulse_time_(0) {}
259 }
260 252
261 // Insert periodic impulses in first two samples of |destination|. 253 // Insert periodic impulses in first two samples of |destination|.
262 void Read(void* destination, int num_frames) override { 254 void Read(void* destination, int num_frames) override {
263 ASSERT_EQ(num_frames, frames_per_buffer_); 255 ASSERT_EQ(num_frames, frames_per_buffer_);
264 if (play_count_ == 0) { 256 if (play_count_ == 0) {
265 PRINT("["); 257 PRINT("[");
266 } 258 }
267 play_count_++; 259 play_count_++;
268 memset(destination, 0, bytes_per_buffer_); 260 memset(destination, 0, bytes_per_buffer_);
269 if (play_count_ % (kNumCallbacksPerSecond / kImpulseFrequencyInHz) == 0) { 261 if (play_count_ % (kNumCallbacksPerSecond / kImpulseFrequencyInHz) == 0) {
270 if (pulse_time_ == 0) { 262 if (pulse_time_ == 0) {
271 pulse_time_ = clock_->TimeInMilliseconds(); 263 pulse_time_ = clock_->TimeInMilliseconds();
272 } 264 }
273 PRINT("."); 265 PRINT(".");
274 const int16_t impulse = std::numeric_limits<int16_t>::max(); 266 const int16_t impulse = std::numeric_limits<int16_t>::max();
275 int16_t* ptr16 = static_cast<int16_t*> (destination); 267 int16_t* ptr16 = static_cast<int16_t*>(destination);
276 for (int i = 0; i < 2; ++i) { 268 for (int i = 0; i < 2; ++i) {
277 *ptr16++ = impulse; 269 *ptr16++ = impulse;
278 } 270 }
279 } 271 }
280 } 272 }
281 273
282 // Detect received impulses in |source|, derive time between transmission and 274 // Detect received impulses in |source|, derive time between transmission and
283 // detection and add the calculated delay to list of latencies. 275 // detection and add the calculated delay to list of latencies.
284 void Write(const void* source, int num_frames) override { 276 void Write(const void* source, int num_frames) override {
285 ASSERT_EQ(num_frames, frames_per_buffer_); 277 ASSERT_EQ(num_frames, frames_per_buffer_);
286 rec_count_++; 278 rec_count_++;
287 if (pulse_time_ == 0) { 279 if (pulse_time_ == 0) {
288 // Avoid detection of new impulse response until a new impulse has 280 // Avoid detection of new impulse response until a new impulse has
289 // been transmitted (sets |pulse_time_| to value larger than zero). 281 // been transmitted (sets |pulse_time_| to value larger than zero).
290 return; 282 return;
291 } 283 }
292 const int16_t* ptr16 = static_cast<const int16_t*> (source); 284 const int16_t* ptr16 = static_cast<const int16_t*>(source);
293 std::vector<int16_t> vec(ptr16, ptr16 + num_frames); 285 std::vector<int16_t> vec(ptr16, ptr16 + num_frames);
294 // Find max value in the audio buffer. 286 // Find max value in the audio buffer.
295 int max = *std::max_element(vec.begin(), vec.end()); 287 int max = *std::max_element(vec.begin(), vec.end());
296 // Find index (element position in vector) of the max element. 288 // Find index (element position in vector) of the max element.
297 int index_of_max = std::distance(vec.begin(), 289 int index_of_max =
298 std::find(vec.begin(), vec.end(), 290 std::distance(vec.begin(), std::find(vec.begin(), vec.end(), max));
299 max));
300 if (max > kImpulseThreshold) { 291 if (max > kImpulseThreshold) {
301 PRINTD("(%d,%d)", max, index_of_max); 292 PRINTD("(%d,%d)", max, index_of_max);
302 int64_t now_time = clock_->TimeInMilliseconds(); 293 int64_t now_time = clock_->TimeInMilliseconds();
303 int extra_delay = IndexToMilliseconds(static_cast<double> (index_of_max)); 294 int extra_delay = IndexToMilliseconds(static_cast<double>(index_of_max));
304 PRINTD("[%d]", static_cast<int> (now_time - pulse_time_)); 295 PRINTD("[%d]", static_cast<int>(now_time - pulse_time_));
305 PRINTD("[%d]", extra_delay); 296 PRINTD("[%d]", extra_delay);
306 // Total latency is the difference between transmit time and detection 297 // Total latency is the difference between transmit time and detection
307 // tome plus the extra delay within the buffer in which we detected the 298 // tome plus the extra delay within the buffer in which we detected the
308 // received impulse. It is transmitted at sample 0 but can be received 299 // received impulse. It is transmitted at sample 0 but can be received
309 // at sample N where N > 0. The term |extra_delay| accounts for N and it 300 // at sample N where N > 0. The term |extra_delay| accounts for N and it
310 // is a value between 0 and 10ms. 301 // is a value between 0 and 10ms.
311 latencies_.push_back(now_time - pulse_time_ + extra_delay); 302 latencies_.push_back(now_time - pulse_time_ + extra_delay);
312 pulse_time_ = 0; 303 pulse_time_ = 0;
313 } else { 304 } else {
314 PRINTD("-"); 305 PRINTD("-");
315 } 306 }
316 } 307 }
317 308
318 int num_latency_values() const { 309 int num_latency_values() const { return latencies_.size(); }
319 return latencies_.size();
320 }
321 310
322 int min_latency() const { 311 int min_latency() const {
323 if (latencies_.empty()) 312 if (latencies_.empty())
324 return 0; 313 return 0;
325 return *std::min_element(latencies_.begin(), latencies_.end()); 314 return *std::min_element(latencies_.begin(), latencies_.end());
326 } 315 }
327 316
328 int max_latency() const { 317 int max_latency() const {
329 if (latencies_.empty()) 318 if (latencies_.empty())
330 return 0; 319 return 0;
331 return *std::max_element(latencies_.begin(), latencies_.end()); 320 return *std::max_element(latencies_.begin(), latencies_.end());
332 } 321 }
333 322
334 int average_latency() const { 323 int average_latency() const {
335 if (latencies_.empty()) 324 if (latencies_.empty())
336 return 0; 325 return 0;
337 return 0.5 + static_cast<double> ( 326 return 0.5 +
338 std::accumulate(latencies_.begin(), latencies_.end(), 0)) / 327 static_cast<double>(
339 latencies_.size(); 328 std::accumulate(latencies_.begin(), latencies_.end(), 0)) /
329 latencies_.size();
340 } 330 }
341 331
342 void PrintResults() const { 332 void PrintResults() const {
343 PRINT("] "); 333 PRINT("] ");
344 for (auto it = latencies_.begin(); it != latencies_.end(); ++it) { 334 for (auto it = latencies_.begin(); it != latencies_.end(); ++it) {
345 PRINT("%d ", *it); 335 PRINT("%d ", *it);
346 } 336 }
347 PRINT("\n"); 337 PRINT("\n");
348 PRINT("%s[min, max, avg]=[%d, %d, %d] ms\n", kTag, 338 PRINT("%s[min, max, avg]=[%d, %d, %d] ms\n", kTag, min_latency(),
349 min_latency(), max_latency(), average_latency()); 339 max_latency(), average_latency());
350 } 340 }
351 341
352 int IndexToMilliseconds(double index) const { 342 int IndexToMilliseconds(double index) const {
353 return 10.0 * (index / frames_per_buffer_) + 0.5; 343 return 10.0 * (index / frames_per_buffer_) + 0.5;
354 } 344 }
355 345
356 private: 346 private:
357 Clock* clock_; 347 Clock* clock_;
358 const int frames_per_buffer_; 348 const int frames_per_buffer_;
359 const int bytes_per_buffer_; 349 const int bytes_per_buffer_;
360 int play_count_; 350 int play_count_;
361 int rec_count_; 351 int rec_count_;
362 int64_t pulse_time_; 352 int64_t pulse_time_;
363 std::vector<int> latencies_; 353 std::vector<int> latencies_;
364 }; 354 };
365
366 // Mocks the AudioTransport object and proxies actions for the two callbacks 355 // Mocks the AudioTransport object and proxies actions for the two callbacks
367 // (RecordedDataIsAvailable and NeedMorePlayData) to different implementations 356 // (RecordedDataIsAvailable and NeedMorePlayData) to different implementations
368 // of AudioStreamInterface. 357 // of AudioStreamInterface.
369 class MockAudioTransport : public AudioTransport { 358 class MockAudioTransport : public AudioTransport {
370 public: 359 public:
371 explicit MockAudioTransport(int type) 360 explicit MockAudioTransport(int type)
372 : num_callbacks_(0), 361 : num_callbacks_(0),
373 type_(type), 362 type_(type),
374 play_count_(0), 363 play_count_(0),
375 rec_count_(0), 364 rec_count_(0),
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
482 bool play_mode() const { return type_ & kPlayout; } 471 bool play_mode() const { return type_ & kPlayout; }
483 bool rec_mode() const { return type_ & kRecording; } 472 bool rec_mode() const { return type_ & kRecording; }
484 473
485 private: 474 private:
486 EventWrapper* test_is_done_; 475 EventWrapper* test_is_done_;
487 int num_callbacks_; 476 int num_callbacks_;
488 int type_; 477 int type_;
489 int play_count_; 478 int play_count_;
490 int rec_count_; 479 int rec_count_;
491 AudioStreamInterface* audio_stream_; 480 AudioStreamInterface* audio_stream_;
492 rtc::scoped_ptr<LatencyMeasuringAudioStream> latency_audio_stream_;
493 }; 481 };
494 482
495 // AudioDeviceTest test fixture. 483 // AudioDeviceTest test fixture.
496 class AudioDeviceTest : public ::testing::Test { 484 class AudioDeviceTest : public ::testing::Test {
497 protected: 485 protected:
498 AudioDeviceTest() 486 AudioDeviceTest() : test_is_done_(EventWrapper::Create()) {
499 : test_is_done_(EventWrapper::Create()) {
500 // One-time initialization of JVM and application context. Ensures that we
501 // can do calls between C++ and Java. Initializes both Java and OpenSL ES
502 // implementations.
503 webrtc::audiodevicemodule::EnsureInitialized();
504 // Creates an audio device using a default audio layer. 487 // Creates an audio device using a default audio layer.
505 audio_device_ = CreateAudioDevice(AudioDeviceModule::kPlatformDefaultAudio); 488 audio_device_ = CreateAudioDevice(AudioDeviceModule::kPlatformDefaultAudio);
506 EXPECT_NE(audio_device_.get(), nullptr); 489 EXPECT_NE(audio_device_.get(), nullptr);
507 EXPECT_EQ(0, audio_device_->Init()); 490 EXPECT_EQ(0, audio_device_->Init());
508 playout_parameters_ = audio_manager()->GetPlayoutAudioParameters(); 491 EXPECT_EQ(0,
509 record_parameters_ = audio_manager()->GetRecordAudioParameters(); 492 audio_device()->GetPlayoutAudioParameters(&playout_parameters_));
510 build_info_.reset(new BuildInfo()); 493 EXPECT_EQ(0, audio_device()->GetRecordAudioParameters(&record_parameters_));
511 } 494 }
512 virtual ~AudioDeviceTest() { 495 virtual ~AudioDeviceTest() { EXPECT_EQ(0, audio_device_->Terminate()); }
513 EXPECT_EQ(0, audio_device_->Terminate());
514 }
515 496
516 int playout_sample_rate() const { 497 // TODO(henrika): don't use hardcoded values below.
517 return playout_parameters_.sample_rate(); 498 int playout_sample_rate() const { return playout_parameters_.sample_rate(); }
518 } 499 int record_sample_rate() const { return record_parameters_.sample_rate(); }
519 int record_sample_rate() const { 500 int playout_channels() const { return playout_parameters_.channels(); }
520 return record_parameters_.sample_rate(); 501 int record_channels() const { return record_parameters_.channels(); }
521 }
522 int playout_channels() const {
523 return playout_parameters_.channels();
524 }
525 int record_channels() const {
526 return record_parameters_.channels();
527 }
528 int playout_frames_per_10ms_buffer() const { 502 int playout_frames_per_10ms_buffer() const {
529 return playout_parameters_.frames_per_10ms_buffer(); 503 return playout_parameters_.frames_per_10ms_buffer();
530 } 504 }
531 int record_frames_per_10ms_buffer() const { 505 int record_frames_per_10ms_buffer() const {
532 return record_parameters_.frames_per_10ms_buffer(); 506 return record_parameters_.frames_per_10ms_buffer();
533 } 507 }
534 508
535 int total_delay_ms() const { 509 int total_delay_ms() const {
536 return audio_manager()->GetDelayEstimateInMilliseconds(); 510 // TODO(henrika): improve this part.
511 return 100;
537 } 512 }
538 513
539 rtc::scoped_refptr<AudioDeviceModule> audio_device() const { 514 rtc::scoped_refptr<AudioDeviceModule> audio_device() const {
540 return audio_device_; 515 return audio_device_;
541 } 516 }
542 517
543 AudioDeviceModuleImpl* audio_device_impl() const { 518 AudioDeviceModuleImpl* audio_device_impl() const {
544 return static_cast<AudioDeviceModuleImpl*>(audio_device_.get()); 519 return static_cast<AudioDeviceModuleImpl*>(audio_device_.get());
545 } 520 }
546 521
547 AudioManager* audio_manager() const {
548 return audio_device_impl()->GetAndroidAudioManagerForTest();
549 }
550
551 AudioManager* GetAudioManager(AudioDeviceModule* adm) const {
552 return static_cast<AudioDeviceModuleImpl*>(adm)->
553 GetAndroidAudioManagerForTest();
554 }
555
556 AudioDeviceBuffer* audio_device_buffer() const { 522 AudioDeviceBuffer* audio_device_buffer() const {
557 return audio_device_impl()->GetAudioDeviceBuffer(); 523 return audio_device_impl()->GetAudioDeviceBuffer();
558 } 524 }
559 525
560 rtc::scoped_refptr<AudioDeviceModule> CreateAudioDevice( 526 rtc::scoped_refptr<AudioDeviceModule> CreateAudioDevice(
561 AudioDeviceModule::AudioLayer audio_layer) { 527 AudioDeviceModule::AudioLayer audio_layer) {
562 rtc::scoped_refptr<AudioDeviceModule> module( 528 rtc::scoped_refptr<AudioDeviceModule> module(
563 AudioDeviceModuleImpl::Create(0, audio_layer)); 529 AudioDeviceModuleImpl::Create(0, audio_layer));
564 return module; 530 return module;
565 } 531 }
566 532
567 // Returns file name relative to the resource root given a sample rate. 533 // Returns file name relative to the resource root given a sample rate.
568 std::string GetFileName(int sample_rate) { 534 std::string GetFileName(int sample_rate) {
569 EXPECT_TRUE(sample_rate == 48000 || sample_rate == 44100); 535 EXPECT_TRUE(sample_rate == 48000 || sample_rate == 44100 ||
536 sample_rate == 16000);
570 char fname[64]; 537 char fname[64];
571 snprintf(fname, 538 snprintf(fname, sizeof(fname), "audio_device/audio_short%d",
572 sizeof(fname),
573 "audio_device/audio_short%d",
574 sample_rate / 1000); 539 sample_rate / 1000);
575 std::string file_name(webrtc::test::ResourcePath(fname, "pcm")); 540 std::string file_name(webrtc::test::ResourcePath(fname, "pcm"));
576 EXPECT_TRUE(test::FileExists(file_name)); 541 EXPECT_TRUE(test::FileExists(file_name));
577 #ifdef ENABLE_PRINTF 542 #ifdef ENABLE_DEBUG_PRINTF
578 PRINT("file name: %s\n", file_name.c_str()); 543 PRINTD("file name: %s\n", file_name.c_str());
579 const int bytes = test::GetFileSize(file_name); 544 const int bytes = test::GetFileSize(file_name);
580 PRINT("file size: %d [bytes]\n", bytes); 545 PRINTD("file size: %d [bytes]\n", bytes);
581 PRINT("file size: %d [samples]\n", bytes / kBytesPerSample); 546 PRINTD("file size: %d [samples]\n", bytes / kBytesPerSample);
582 const int seconds = bytes / (sample_rate * kBytesPerSample); 547 const int seconds = bytes / (sample_rate * kBytesPerSample);
583 PRINT("file size: %d [secs]\n", seconds); 548 PRINTD("file size: %d [secs]\n", seconds);
584 PRINT("file size: %d [callbacks]\n", seconds * kNumCallbacksPerSecond); 549 PRINTD("file size: %d [callbacks]\n", seconds * kNumCallbacksPerSecond);
585 #endif 550 #endif
586 return file_name; 551 return file_name;
587 } 552 }
588 553
589 AudioDeviceModule::AudioLayer GetActiveAudioLayer() const {
590 AudioDeviceModule::AudioLayer audio_layer;
591 EXPECT_EQ(0, audio_device()->ActiveAudioLayer(&audio_layer));
592 return audio_layer;
593 }
594
595 int TestDelayOnAudioLayer(
596 const AudioDeviceModule::AudioLayer& layer_to_test) {
597 rtc::scoped_refptr<AudioDeviceModule> audio_device;
598 audio_device = CreateAudioDevice(layer_to_test);
599 EXPECT_NE(audio_device.get(), nullptr);
600 AudioManager* audio_manager = GetAudioManager(audio_device.get());
601 EXPECT_NE(audio_manager, nullptr);
602 return audio_manager->GetDelayEstimateInMilliseconds();
603 }
604
605 AudioDeviceModule::AudioLayer TestActiveAudioLayer(
606 const AudioDeviceModule::AudioLayer& layer_to_test) {
607 rtc::scoped_refptr<AudioDeviceModule> audio_device;
608 audio_device = CreateAudioDevice(layer_to_test);
609 EXPECT_NE(audio_device.get(), nullptr);
610 AudioDeviceModule::AudioLayer active;
611 EXPECT_EQ(0, audio_device->ActiveAudioLayer(&active));
612 return active;
613 }
614
615 bool DisableTestForThisDevice(const std::string& model) {
616 return (build_info_->GetDeviceModel() == model);
617 }
618
619 // Volume control is currently only supported for the Java output audio layer.
620 // For OpenSL ES, the internal stream volume is always on max level and there
621 // is no need for this test to set it to max.
622 bool AudioLayerSupportsVolumeControl() const {
623 return GetActiveAudioLayer() == AudioDeviceModule::kAndroidJavaAudio;
624 }
625
626 void SetMaxPlayoutVolume() {
627 if (!AudioLayerSupportsVolumeControl())
628 return;
629 uint32_t max_volume;
630 EXPECT_EQ(0, audio_device()->MaxSpeakerVolume(&max_volume));
631 EXPECT_EQ(0, audio_device()->SetSpeakerVolume(max_volume));
632 }
633
634 void DisableBuiltInAECIfAvailable() {
635 if (audio_device()->BuiltInAECIsAvailable()) {
636 EXPECT_EQ(0, audio_device()->EnableBuiltInAEC(false));
637 }
638 }
639
640 void StartPlayout() { 554 void StartPlayout() {
641 EXPECT_FALSE(audio_device()->PlayoutIsInitialized()); 555 EXPECT_FALSE(audio_device()->PlayoutIsInitialized());
642 EXPECT_FALSE(audio_device()->Playing()); 556 EXPECT_FALSE(audio_device()->Playing());
643 EXPECT_EQ(0, audio_device()->InitPlayout()); 557 EXPECT_EQ(0, audio_device()->InitPlayout());
644 EXPECT_TRUE(audio_device()->PlayoutIsInitialized()); 558 EXPECT_TRUE(audio_device()->PlayoutIsInitialized());
645 EXPECT_EQ(0, audio_device()->StartPlayout()); 559 EXPECT_EQ(0, audio_device()->StartPlayout());
646 EXPECT_TRUE(audio_device()->Playing()); 560 EXPECT_TRUE(audio_device()->Playing());
647 } 561 }
648 562
649 void StopPlayout() { 563 void StopPlayout() {
650 EXPECT_EQ(0, audio_device()->StopPlayout()); 564 EXPECT_EQ(0, audio_device()->StopPlayout());
651 EXPECT_FALSE(audio_device()->Playing()); 565 EXPECT_FALSE(audio_device()->Playing());
652 EXPECT_FALSE(audio_device()->PlayoutIsInitialized()); 566 EXPECT_FALSE(audio_device()->PlayoutIsInitialized());
653 } 567 }
654 568
655 void StartRecording() { 569 void StartRecording() {
656 EXPECT_FALSE(audio_device()->RecordingIsInitialized()); 570 EXPECT_FALSE(audio_device()->RecordingIsInitialized());
657 EXPECT_FALSE(audio_device()->Recording()); 571 EXPECT_FALSE(audio_device()->Recording());
658 EXPECT_EQ(0, audio_device()->InitRecording()); 572 EXPECT_EQ(0, audio_device()->InitRecording());
659 EXPECT_TRUE(audio_device()->RecordingIsInitialized()); 573 EXPECT_TRUE(audio_device()->RecordingIsInitialized());
660 EXPECT_EQ(0, audio_device()->StartRecording()); 574 EXPECT_EQ(0, audio_device()->StartRecording());
661 EXPECT_TRUE(audio_device()->Recording()); 575 EXPECT_TRUE(audio_device()->Recording());
662 } 576 }
663 577
664 void StopRecording() { 578 void StopRecording() {
665 EXPECT_EQ(0, audio_device()->StopRecording()); 579 EXPECT_EQ(0, audio_device()->StopRecording());
666 EXPECT_FALSE(audio_device()->Recording()); 580 EXPECT_FALSE(audio_device()->Recording());
667 } 581 }
668 582
669 int GetMaxSpeakerVolume() const {
670 uint32_t max_volume(0);
671 EXPECT_EQ(0, audio_device()->MaxSpeakerVolume(&max_volume));
672 return max_volume;
673 }
674
675 int GetMinSpeakerVolume() const {
676 uint32_t min_volume(0);
677 EXPECT_EQ(0, audio_device()->MinSpeakerVolume(&min_volume));
678 return min_volume;
679 }
680
681 int GetSpeakerVolume() const {
682 uint32_t volume(0);
683 EXPECT_EQ(0, audio_device()->SpeakerVolume(&volume));
684 return volume;
685 }
686
687 rtc::scoped_ptr<EventWrapper> test_is_done_; 583 rtc::scoped_ptr<EventWrapper> test_is_done_;
688 rtc::scoped_refptr<AudioDeviceModule> audio_device_; 584 rtc::scoped_refptr<AudioDeviceModule> audio_device_;
689 AudioParameters playout_parameters_; 585 AudioParameters playout_parameters_;
690 AudioParameters record_parameters_; 586 AudioParameters record_parameters_;
691 rtc::scoped_ptr<BuildInfo> build_info_;
692 }; 587 };
693 588
694 TEST_F(AudioDeviceTest, ConstructDestruct) { 589 TEST_F(AudioDeviceTest, ConstructDestruct) {
695 // Using the test fixture to create and destruct the audio device module. 590 // Using the test fixture to create and destruct the audio device module.
696 } 591 }
697 592
698 // We always ask for a default audio layer when the ADM is constructed. But the
699 // ADM will then internally set the best suitable combination of audio layers,
700 // for input and output based on if low-latency output audio in combination
701 // with OpenSL ES is supported or not. This test ensures that the correct
702 // selection is done.
703 TEST_F(AudioDeviceTest, VerifyDefaultAudioLayer) {
704 const AudioDeviceModule::AudioLayer audio_layer = GetActiveAudioLayer();
705 bool low_latency_output = audio_manager()->IsLowLatencyPlayoutSupported();
706 AudioDeviceModule::AudioLayer expected_audio_layer = low_latency_output ?
707 AudioDeviceModule::kAndroidJavaInputAndOpenSLESOutputAudio :
708 AudioDeviceModule::kAndroidJavaAudio;
709 EXPECT_EQ(expected_audio_layer, audio_layer);
710 }
711
712 // Verify that it is possible to explicitly create the two types of supported
713 // ADMs. These two tests overrides the default selection of native audio layer
714 // by ignoring if the device supports low-latency output or not.
715 TEST_F(AudioDeviceTest, CorrectAudioLayerIsUsedForCombinedJavaOpenSLCombo) {
716 AudioDeviceModule::AudioLayer expected_layer =
717 AudioDeviceModule::kAndroidJavaInputAndOpenSLESOutputAudio;
718 AudioDeviceModule::AudioLayer active_layer = TestActiveAudioLayer(
719 expected_layer);
720 EXPECT_EQ(expected_layer, active_layer);
721 }
722
723 TEST_F(AudioDeviceTest, CorrectAudioLayerIsUsedForJavaInBothDirections) {
724 AudioDeviceModule::AudioLayer expected_layer =
725 AudioDeviceModule::kAndroidJavaAudio;
726 AudioDeviceModule::AudioLayer active_layer = TestActiveAudioLayer(
727 expected_layer);
728 EXPECT_EQ(expected_layer, active_layer);
729 }
730
731 // The Android ADM supports two different delay reporting modes. One for the
732 // low-latency output path (in combination with OpenSL ES), and one for the
733 // high-latency output path (Java backends in both directions). These two tests
734 // verifies that the audio manager reports correct delay estimate given the
735 // selected audio layer. Note that, this delay estimate will only be utilized
736 // if the HW AEC is disabled.
737 TEST_F(AudioDeviceTest, UsesCorrectDelayEstimateForHighLatencyOutputPath) {
738 EXPECT_EQ(kHighLatencyModeDelayEstimateInMilliseconds,
739 TestDelayOnAudioLayer(AudioDeviceModule::kAndroidJavaAudio));
740 }
741
742 TEST_F(AudioDeviceTest, UsesCorrectDelayEstimateForLowLatencyOutputPath) {
743 EXPECT_EQ(kLowLatencyModeDelayEstimateInMilliseconds,
744 TestDelayOnAudioLayer(
745 AudioDeviceModule::kAndroidJavaInputAndOpenSLESOutputAudio));
746 }
747
748 // Ensure that the ADM internal audio device buffer is configured to use the
749 // correct set of parameters.
750 TEST_F(AudioDeviceTest, VerifyAudioDeviceBufferParameters) {
751 EXPECT_EQ(playout_parameters_.sample_rate(),
752 audio_device_buffer()->PlayoutSampleRate());
753 EXPECT_EQ(record_parameters_.sample_rate(),
754 audio_device_buffer()->RecordingSampleRate());
755 EXPECT_EQ(playout_parameters_.channels(),
756 audio_device_buffer()->PlayoutChannels());
757 EXPECT_EQ(record_parameters_.channels(),
758 audio_device_buffer()->RecordingChannels());
759 }
760
761
762 TEST_F(AudioDeviceTest, InitTerminate) { 593 TEST_F(AudioDeviceTest, InitTerminate) {
763 // Initialization is part of the test fixture. 594 // Initialization is part of the test fixture.
764 EXPECT_TRUE(audio_device()->Initialized()); 595 EXPECT_TRUE(audio_device()->Initialized());
596 // webrtc::SleepMs(5 * 1000);
765 EXPECT_EQ(0, audio_device()->Terminate()); 597 EXPECT_EQ(0, audio_device()->Terminate());
766 EXPECT_FALSE(audio_device()->Initialized()); 598 EXPECT_FALSE(audio_device()->Initialized());
767 } 599 }
768 600
769 TEST_F(AudioDeviceTest, Devices) {
770 // Device enumeration is not supported. Verify fixed values only.
771 EXPECT_EQ(1, audio_device()->PlayoutDevices());
772 EXPECT_EQ(1, audio_device()->RecordingDevices());
773 }
774
775 TEST_F(AudioDeviceTest, SpeakerVolumeShouldBeAvailable) {
776 // The OpenSL ES output audio path does not support volume control.
777 if (!AudioLayerSupportsVolumeControl())
778 return;
779 bool available;
780 EXPECT_EQ(0, audio_device()->SpeakerVolumeIsAvailable(&available));
781 EXPECT_TRUE(available);
782 }
783
784 TEST_F(AudioDeviceTest, MaxSpeakerVolumeIsPositive) {
785 // The OpenSL ES output audio path does not support volume control.
786 if (!AudioLayerSupportsVolumeControl())
787 return;
788 StartPlayout();
789 EXPECT_GT(GetMaxSpeakerVolume(), 0);
790 StopPlayout();
791 }
792
793 TEST_F(AudioDeviceTest, MinSpeakerVolumeIsZero) {
794 // The OpenSL ES output audio path does not support volume control.
795 if (!AudioLayerSupportsVolumeControl())
796 return;
797 EXPECT_EQ(GetMinSpeakerVolume(), 0);
798 }
799
800 TEST_F(AudioDeviceTest, DefaultSpeakerVolumeIsWithinMinMax) {
801 // The OpenSL ES output audio path does not support volume control.
802 if (!AudioLayerSupportsVolumeControl())
803 return;
804 const int default_volume = GetSpeakerVolume();
805 EXPECT_GE(default_volume, GetMinSpeakerVolume());
806 EXPECT_LE(default_volume, GetMaxSpeakerVolume());
807 }
808
809 TEST_F(AudioDeviceTest, SetSpeakerVolumeActuallySetsVolume) {
810 // The OpenSL ES output audio path does not support volume control.
811 if (!AudioLayerSupportsVolumeControl())
812 return;
813 const int default_volume = GetSpeakerVolume();
814 const int max_volume = GetMaxSpeakerVolume();
815 EXPECT_EQ(0, audio_device()->SetSpeakerVolume(max_volume));
816 int new_volume = GetSpeakerVolume();
817 EXPECT_EQ(new_volume, max_volume);
818 EXPECT_EQ(0, audio_device()->SetSpeakerVolume(default_volume));
819 }
820
821 // Tests that playout can be initiated, started and stopped. No audio callback 601 // Tests that playout can be initiated, started and stopped. No audio callback
822 // is registered in this test. 602 // is registered in this test.
823 TEST_F(AudioDeviceTest, StartStopPlayout) { 603 TEST_F(AudioDeviceTest, StartStopPlayout) {
824 StartPlayout(); 604 StartPlayout();
825 StopPlayout(); 605 StopPlayout();
826 StartPlayout(); 606 StartPlayout();
827 StopPlayout(); 607 StopPlayout();
828 } 608 }
829 609
610 // Tests that recording can be initiated, started and stopped. No audio callback
611 // is registered in this test.
612 TEST_F(AudioDeviceTest, StartStopRecording) {
613 StartRecording();
614 StopRecording();
615 StartRecording();
616 StopRecording();
617 }
618
830 // Verify that calling StopPlayout() will leave us in an uninitialized state 619 // Verify that calling StopPlayout() will leave us in an uninitialized state
831 // which will require a new call to InitPlayout(). This test does not call 620 // which will require a new call to InitPlayout(). This test does not call
832 // StartPlayout() while being uninitialized since doing so will hit a DCHECK. 621 // StartPlayout() while being uninitialized since doing so will hit a DCHECK.
833 TEST_F(AudioDeviceTest, StopPlayoutRequiresInitToRestart) { 622 TEST_F(AudioDeviceTest, StopPlayoutRequiresInitToRestart) {
834 EXPECT_EQ(0, audio_device()->InitPlayout()); 623 EXPECT_EQ(0, audio_device()->InitPlayout());
835 EXPECT_EQ(0, audio_device()->StartPlayout()); 624 EXPECT_EQ(0, audio_device()->StartPlayout());
836 EXPECT_EQ(0, audio_device()->StopPlayout()); 625 EXPECT_EQ(0, audio_device()->StopPlayout());
837 EXPECT_FALSE(audio_device()->PlayoutIsInitialized()); 626 EXPECT_FALSE(audio_device()->PlayoutIsInitialized());
838 } 627 }
839 628
840 // Start playout and verify that the native audio layer starts asking for real 629 // Start playout and verify that the native audio layer starts asking for real
841 // audio samples to play out using the NeedMorePlayData callback. 630 // audio samples to play out using the NeedMorePlayData callback.
842 TEST_F(AudioDeviceTest, StartPlayoutVerifyCallbacks) { 631 TEST_F(AudioDeviceTest, StartPlayoutVerifyCallbacks) {
843 MockAudioTransport mock(kPlayout); 632 MockAudioTransport mock(kPlayout);
844 mock.HandleCallbacks(test_is_done_.get(), nullptr, kNumCallbacks); 633 mock.HandleCallbacks(test_is_done_.get(), nullptr, kNumCallbacks);
845 EXPECT_CALL(mock, NeedMorePlayData(playout_frames_per_10ms_buffer(), 634 EXPECT_CALL(mock, NeedMorePlayData(playout_frames_per_10ms_buffer(),
846 kBytesPerSample, 635 kBytesPerSample, playout_channels(),
847 playout_channels(), 636 playout_sample_rate(), NotNull(), _, _, _))
848 playout_sample_rate(),
849 NotNull(),
850 _, _, _))
851 .Times(AtLeast(kNumCallbacks)); 637 .Times(AtLeast(kNumCallbacks));
852 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); 638 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock));
853 StartPlayout(); 639 StartPlayout();
854 test_is_done_->Wait(kTestTimeOutInMilliseconds); 640 test_is_done_->Wait(kTestTimeOutInMilliseconds);
855 StopPlayout(); 641 StopPlayout();
856 } 642 }
857 643
858 // Start recording and verify that the native audio layer starts feeding real 644 // Start recording and verify that the native audio layer starts feeding real
859 // audio samples via the RecordedDataIsAvailable callback. 645 // audio samples via the RecordedDataIsAvailable callback.
860 TEST_F(AudioDeviceTest, StartRecordingVerifyCallbacks) { 646 TEST_F(AudioDeviceTest, StartRecordingVerifyCallbacks) {
861 MockAudioTransport mock(kRecording); 647 MockAudioTransport mock(kRecording);
862 mock.HandleCallbacks(test_is_done_.get(), nullptr, kNumCallbacks); 648 mock.HandleCallbacks(test_is_done_.get(), nullptr, kNumCallbacks);
863 EXPECT_CALL(mock, RecordedDataIsAvailable(NotNull(), 649 EXPECT_CALL(mock,
864 record_frames_per_10ms_buffer(), 650 RecordedDataIsAvailable(
865 kBytesPerSample, 651 NotNull(), record_frames_per_10ms_buffer(), kBytesPerSample,
866 record_channels(), 652 record_channels(), record_sample_rate(),
867 record_sample_rate(), 653 _, // TODO(henrika): fix delay
868 total_delay_ms(), 654 0, 0, false, _)).Times(AtLeast(kNumCallbacks));
869 0,
870 0,
871 false,
872 _))
873 .Times(AtLeast(kNumCallbacks));
874 655
875 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); 656 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock));
876 StartRecording(); 657 StartRecording();
877 test_is_done_->Wait(kTestTimeOutInMilliseconds); 658 test_is_done_->Wait(kTestTimeOutInMilliseconds);
878 StopRecording(); 659 StopRecording();
879 } 660 }
880 661
881
882 // Start playout and recording (full-duplex audio) and verify that audio is 662 // Start playout and recording (full-duplex audio) and verify that audio is
883 // active in both directions. 663 // active in both directions.
884 TEST_F(AudioDeviceTest, StartPlayoutAndRecordingVerifyCallbacks) { 664 TEST_F(AudioDeviceTest, StartPlayoutAndRecordingVerifyCallbacks) {
885 MockAudioTransport mock(kPlayout | kRecording); 665 MockAudioTransport mock(kPlayout | kRecording);
886 mock.HandleCallbacks(test_is_done_.get(), nullptr, kNumCallbacks); 666 mock.HandleCallbacks(test_is_done_.get(), nullptr, kNumCallbacks);
887 EXPECT_CALL(mock, NeedMorePlayData(playout_frames_per_10ms_buffer(), 667 EXPECT_CALL(mock, NeedMorePlayData(playout_frames_per_10ms_buffer(),
888 kBytesPerSample, 668 kBytesPerSample, playout_channels(),
889 playout_channels(), 669 playout_sample_rate(), NotNull(), _, _, _))
890 playout_sample_rate(),
891 NotNull(),
892 _, _, _))
893 .Times(AtLeast(kNumCallbacks)); 670 .Times(AtLeast(kNumCallbacks));
894 EXPECT_CALL(mock, RecordedDataIsAvailable(NotNull(), 671 EXPECT_CALL(mock,
895 record_frames_per_10ms_buffer(), 672 RecordedDataIsAvailable(
896 kBytesPerSample, 673 NotNull(), record_frames_per_10ms_buffer(), kBytesPerSample,
897 record_channels(), 674 record_channels(), record_sample_rate(),
898 record_sample_rate(), 675 _, // TODO(henrika): fix delay
899 total_delay_ms(), 676 0, 0, false, _)).Times(AtLeast(kNumCallbacks));
900 0,
901 0,
902 false,
903 _))
904 .Times(AtLeast(kNumCallbacks));
905 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); 677 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock));
906 StartPlayout(); 678 StartPlayout();
907 StartRecording(); 679 StartRecording();
908 test_is_done_->Wait(kTestTimeOutInMilliseconds); 680 test_is_done_->Wait(kTestTimeOutInMilliseconds);
909 StopRecording(); 681 StopRecording();
910 StopPlayout(); 682 StopPlayout();
911 } 683 }
912 684
913 // Start playout and read audio from an external PCM file when the audio layer 685 // Start playout and read audio from an external PCM file when the audio layer
914 // asks for data to play out. Real audio is played out in this test but it does 686 // asks for data to play out. Real audio is played out in this test but it does
915 // not contain any explicit verification that the audio quality is perfect. 687 // not contain any explicit verification that the audio quality is perfect.
916 TEST_F(AudioDeviceTest, RunPlayoutWithFileAsSource) { 688 TEST_F(AudioDeviceTest, RunPlayoutWithFileAsSource) {
917 // TODO(henrika): extend test when mono output is supported. 689 // TODO(henrika): extend test when mono output is supported.
918 EXPECT_EQ(1, playout_channels()); 690 EXPECT_EQ(1, playout_channels());
919 NiceMock<MockAudioTransport> mock(kPlayout); 691 NiceMock<MockAudioTransport> mock(kPlayout);
920 const int num_callbacks = kFilePlayTimeInSec * kNumCallbacksPerSecond; 692 const int num_callbacks = kFilePlayTimeInSec * kNumCallbacksPerSecond;
921 std::string file_name = GetFileName(playout_sample_rate()); 693 std::string file_name = GetFileName(playout_sample_rate());
922 rtc::scoped_ptr<FileAudioStream> file_audio_stream( 694 rtc::scoped_ptr<FileAudioStream> file_audio_stream(
923 new FileAudioStream(num_callbacks, file_name, playout_sample_rate())); 695 new FileAudioStream(num_callbacks, file_name, playout_sample_rate()));
924 mock.HandleCallbacks(test_is_done_.get(), 696 mock.HandleCallbacks(test_is_done_.get(), file_audio_stream.get(),
925 file_audio_stream.get(),
926 num_callbacks); 697 num_callbacks);
927 // SetMaxPlayoutVolume(); 698 // SetMaxPlayoutVolume();
928 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); 699 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock));
929 StartPlayout(); 700 StartPlayout();
930 test_is_done_->Wait(kTestTimeOutInMilliseconds); 701 test_is_done_->Wait(kTestTimeOutInMilliseconds);
931 StopPlayout(); 702 StopPlayout();
932 } 703 }
933 704
705 TEST_F(AudioDeviceTest, Devices) {
706 // Device enumeration is not supported. Verify fixed values only.
707 EXPECT_EQ(1, audio_device()->PlayoutDevices());
708 EXPECT_EQ(1, audio_device()->RecordingDevices());
709 }
710
934 // Start playout and recording and store recorded data in an intermediate FIFO 711 // Start playout and recording and store recorded data in an intermediate FIFO
935 // buffer from which the playout side then reads its samples in the same order 712 // buffer from which the playout side then reads its samples in the same order
936 // as they were stored. Under ideal circumstances, a callback sequence would 713 // as they were stored. Under ideal circumstances, a callback sequence would
937 // look like: ...+-+-+-+-+-+-+-..., where '+' means 'packet recorded' and '-' 714 // look like: ...+-+-+-+-+-+-+-..., where '+' means 'packet recorded' and '-'
938 // means 'packet played'. Under such conditions, the FIFO would only contain 715 // means 'packet played'. Under such conditions, the FIFO would only contain
939 // one packet on average. However, under more realistic conditions, the size 716 // one packet on average. However, under more realistic conditions, the size
940 // of the FIFO will vary more due to an unbalance between the two sides. 717 // of the FIFO will vary more due to an unbalance between the two sides.
941 // This test tries to verify that the device maintains a balanced callback- 718 // This test tries to verify that the device maintains a balanced callback-
942 // sequence by running in loopback for ten seconds while measuring the size 719 // sequence by running in loopback for ten seconds while measuring the size
943 // (max and average) of the FIFO. The size of the FIFO is increased by the 720 // (max and average) of the FIFO. The size of the FIFO is increased by the
944 // recording side and decreased by the playout side. 721 // recording side and decreased by the playout side.
945 // TODO(henrika): tune the final test parameters after running tests on several 722 // TODO(henrika): tune the final test parameters after running tests on several
946 // different devices. 723 // different devices.
947 TEST_F(AudioDeviceTest, RunPlayoutAndRecordingInFullDuplex) { 724 TEST_F(AudioDeviceTest, RunPlayoutAndRecordingInFullDuplex) {
948 EXPECT_EQ(record_channels(), playout_channels()); 725 EXPECT_EQ(record_channels(), playout_channels());
949 EXPECT_EQ(record_sample_rate(), playout_sample_rate()); 726 EXPECT_EQ(record_sample_rate(), playout_sample_rate());
950 NiceMock<MockAudioTransport> mock(kPlayout | kRecording); 727 NiceMock<MockAudioTransport> mock(kPlayout | kRecording);
951 rtc::scoped_ptr<FifoAudioStream> fifo_audio_stream( 728 rtc::scoped_ptr<FifoAudioStream> fifo_audio_stream(
952 new FifoAudioStream(playout_frames_per_10ms_buffer())); 729 new FifoAudioStream(playout_frames_per_10ms_buffer()));
953 mock.HandleCallbacks(test_is_done_.get(), 730 mock.HandleCallbacks(test_is_done_.get(), fifo_audio_stream.get(),
954 fifo_audio_stream.get(),
955 kFullDuplexTimeInSec * kNumCallbacksPerSecond); 731 kFullDuplexTimeInSec * kNumCallbacksPerSecond);
956 SetMaxPlayoutVolume(); 732 // SetMaxPlayoutVolume();
957 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); 733 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock));
958 StartRecording(); 734 StartRecording();
959 StartPlayout(); 735 StartPlayout();
960 test_is_done_->Wait(std::max(kTestTimeOutInMilliseconds, 736 test_is_done_->Wait(
961 1000 * kFullDuplexTimeInSec)); 737 std::max(kTestTimeOutInMilliseconds, 1000 * kFullDuplexTimeInSec));
962 StopPlayout(); 738 StopPlayout();
963 StopRecording(); 739 StopRecording();
964 EXPECT_LE(fifo_audio_stream->average_size(), 10); 740 EXPECT_LE(fifo_audio_stream->average_size(), 10);
965 EXPECT_LE(fifo_audio_stream->largest_size(), 20); 741 EXPECT_LE(fifo_audio_stream->largest_size(), 20);
966 } 742 }
967 743
968 // Measures loopback latency and reports the min, max and average values for 744 // Measures loopback latency and reports the min, max and average values for
969 // a full duplex audio session. 745 // a full duplex audio session.
970 // The latency is measured like so: 746 // The latency is measured like so:
971 // - Insert impulses periodically on the output side. 747 // - Insert impulses periodically on the output side.
972 // - Detect the impulses on the input side. 748 // - Detect the impulses on the input side.
973 // - Measure the time difference between the transmit time and receive time. 749 // - Measure the time difference between the transmit time and receive time.
974 // - Store time differences in a vector and calculate min, max and average. 750 // - Store time differences in a vector and calculate min, max and average.
975 // This test requires a special hardware called Audio Loopback Dongle. 751 // This test requires a special hardware called Audio Loopback Dongle.
976 // See http://source.android.com/devices/audio/loopback.html for details. 752 // See http://source.android.com/devices/audio/loopback.html for details.
977 TEST_F(AudioDeviceTest, DISABLED_MeasureLoopbackLatency) { 753 TEST_F(AudioDeviceTest, DISABLED_MeasureLoopbackLatency) {
978 EXPECT_EQ(record_channels(), playout_channels()); 754 EXPECT_EQ(record_channels(), playout_channels());
979 EXPECT_EQ(record_sample_rate(), playout_sample_rate()); 755 EXPECT_EQ(record_sample_rate(), playout_sample_rate());
980 NiceMock<MockAudioTransport> mock(kPlayout | kRecording); 756 NiceMock<MockAudioTransport> mock(kPlayout | kRecording);
981 rtc::scoped_ptr<LatencyMeasuringAudioStream> latency_audio_stream( 757 rtc::scoped_ptr<LatencyMeasuringAudioStream> latency_audio_stream(
982 new LatencyMeasuringAudioStream(playout_frames_per_10ms_buffer())); 758 new LatencyMeasuringAudioStream(playout_frames_per_10ms_buffer()));
983 mock.HandleCallbacks(test_is_done_.get(), 759 mock.HandleCallbacks(test_is_done_.get(), latency_audio_stream.get(),
984 latency_audio_stream.get(),
985 kMeasureLatencyTimeInSec * kNumCallbacksPerSecond); 760 kMeasureLatencyTimeInSec * kNumCallbacksPerSecond);
986 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); 761 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock));
987 SetMaxPlayoutVolume(); 762 // SetMaxPlayoutVolume();
988 DisableBuiltInAECIfAvailable(); 763 // DisableBuiltInAECIfAvailable();
989 StartRecording(); 764 StartRecording();
990 StartPlayout(); 765 StartPlayout();
991 test_is_done_->Wait(std::max(kTestTimeOutInMilliseconds, 766 test_is_done_->Wait(
992 1000 * kMeasureLatencyTimeInSec)); 767 std::max(kTestTimeOutInMilliseconds, 1000 * kMeasureLatencyTimeInSec));
993 StopPlayout(); 768 StopPlayout();
994 StopRecording(); 769 StopRecording();
995 // Verify that the correct number of transmitted impulses are detected. 770 // Verify that the correct number of transmitted impulses are detected.
996 EXPECT_EQ(latency_audio_stream->num_latency_values(), 771 EXPECT_EQ(latency_audio_stream->num_latency_values(),
997 kImpulseFrequencyInHz * kMeasureLatencyTimeInSec - 1); 772 kImpulseFrequencyInHz * kMeasureLatencyTimeInSec - 1);
998 latency_audio_stream->PrintResults(); 773 latency_audio_stream->PrintResults();
999 } 774 }
1000 775
1001 } // namespace webrtc 776 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698