OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 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 "webrtc/modules/audio_device/android/fine_audio_buffer.h" | 11 #include "webrtc/modules/audio_device/fine_audio_buffer.h" |
12 | 12 |
13 #include <limits.h> | 13 #include <limits.h> |
14 #include <memory> | 14 #include <memory> |
15 | 15 |
16 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 #include "webrtc/base/scoped_ptr.h" | 18 #include "webrtc/base/scoped_ptr.h" |
19 #include "webrtc/modules/audio_device/mock_audio_device_buffer.h" | 19 #include "webrtc/modules/audio_device/mock_audio_device_buffer.h" |
20 | 20 |
21 using ::testing::_; | 21 using ::testing::_; |
| 22 using ::testing::AtLeast; |
22 using ::testing::InSequence; | 23 using ::testing::InSequence; |
23 using ::testing::Return; | 24 using ::testing::Return; |
24 | 25 |
25 namespace webrtc { | 26 namespace webrtc { |
26 | 27 |
27 // The fake audio data is 0,1,..SCHAR_MAX-1,0,1,... This is to make it easy | 28 // The fake audio data is 0,1,..SCHAR_MAX-1,0,1,... This is to make it easy |
28 // to detect errors. This function verifies that the buffers contain such data. | 29 // to detect errors. This function verifies that the buffers contain such data. |
29 // E.g. if there are two buffers of size 3, buffer 1 would contain 0,1,2 and | 30 // E.g. if there are two buffers of size 3, buffer 1 would contain 0,1,2 and |
30 // buffer 2 would contain 3,4,5. Note that SCHAR_MAX is 127 so wrap-around | 31 // buffer 2 would contain 3,4,5. Note that SCHAR_MAX is 127 so wrap-around |
31 // will happen. | 32 // will happen. |
32 // |buffer| is the audio buffer to verify. | 33 // |buffer| is the audio buffer to verify. |
33 bool VerifyBuffer(const int8_t* buffer, int buffer_number, int size) { | 34 bool VerifyBuffer(const int8_t* buffer, int buffer_number, int size) { |
34 int start_value = (buffer_number * size) % SCHAR_MAX; | 35 int start_value = (buffer_number * size) % SCHAR_MAX; |
35 for (int i = 0; i < size; ++i) { | 36 for (int i = 0; i < size; ++i) { |
36 if (buffer[i] != (i + start_value) % SCHAR_MAX) { | 37 if (buffer[i] != (i + start_value) % SCHAR_MAX) { |
37 return false; | 38 return false; |
38 } | 39 } |
39 } | 40 } |
40 return true; | 41 return true; |
41 } | 42 } |
42 | 43 |
43 // This function replaces GetPlayoutData when it's called (which is done | 44 // This function replaces the real AudioDeviceBuffer::GetPlayoutData when it's |
44 // implicitly when calling GetBufferData). It writes the sequence | 45 // called (which is done implicitly when calling GetBufferData). It writes the |
45 // 0,1,..SCHAR_MAX-1,0,1,... to the buffer. Note that this is likely a buffer of | 46 // sequence 0,1,..SCHAR_MAX-1,0,1,... to the buffer. Note that this is likely a |
46 // different size than the one VerifyBuffer verifies. | 47 // buffer of different size than the one VerifyBuffer verifies. |
47 // |iteration| is the number of calls made to UpdateBuffer prior to this call. | 48 // |iteration| is the number of calls made to UpdateBuffer prior to this call. |
48 // |samples_per_10_ms| is the number of samples that should be written to the | 49 // |samples_per_10_ms| is the number of samples that should be written to the |
49 // buffer (|arg0|). | 50 // buffer (|arg0|). |
50 ACTION_P2(UpdateBuffer, iteration, samples_per_10_ms) { | 51 ACTION_P2(UpdateBuffer, iteration, samples_per_10_ms) { |
51 int8_t* buffer = static_cast<int8_t*>(arg0); | 52 int8_t* buffer = static_cast<int8_t*>(arg0); |
52 int bytes_per_10_ms = samples_per_10_ms * static_cast<int>(sizeof(int16_t)); | 53 int bytes_per_10_ms = samples_per_10_ms * static_cast<int>(sizeof(int16_t)); |
53 int start_value = (iteration * bytes_per_10_ms) % SCHAR_MAX; | 54 int start_value = (iteration * bytes_per_10_ms) % SCHAR_MAX; |
54 for (int i = 0; i < bytes_per_10_ms; ++i) { | 55 for (int i = 0; i < bytes_per_10_ms; ++i) { |
55 buffer[i] = (i + start_value) % SCHAR_MAX; | 56 buffer[i] = (i + start_value) % SCHAR_MAX; |
56 } | 57 } |
57 return samples_per_10_ms; | 58 return samples_per_10_ms; |
58 } | 59 } |
59 | 60 |
| 61 // Writes a periodic ramp pattern to the supplied |buffer|. See UpdateBuffer() |
| 62 // for details. |
| 63 void UpdateInputBuffer(int8_t* buffer, int iteration, int size) { |
| 64 int start_value = (iteration * size) % SCHAR_MAX; |
| 65 for (int i = 0; i < size; ++i) { |
| 66 buffer[i] = (i + start_value) % SCHAR_MAX; |
| 67 } |
| 68 } |
| 69 |
| 70 // Action macro which verifies that the recorded 10ms chunk of audio data |
| 71 // (in |arg0|) contains the correct reference values even if they have been |
| 72 // supplied using a buffer size that is smaller or larger than 10ms. |
| 73 // See VerifyBuffer() for details. |
| 74 ACTION_P2(VerifyInputBuffer, iteration, samples_per_10_ms) { |
| 75 const int8_t* buffer = static_cast<const int8_t*>(arg0); |
| 76 int bytes_per_10_ms = samples_per_10_ms * static_cast<int>(sizeof(int16_t)); |
| 77 int start_value = (iteration * bytes_per_10_ms) % SCHAR_MAX; |
| 78 for (int i = 0; i < bytes_per_10_ms; ++i) { |
| 79 EXPECT_EQ(buffer[i], (i + start_value) % SCHAR_MAX); |
| 80 } |
| 81 return 0; |
| 82 } |
| 83 |
60 void RunFineBufferTest(int sample_rate, int frame_size_in_samples) { | 84 void RunFineBufferTest(int sample_rate, int frame_size_in_samples) { |
61 const int kSamplesPer10Ms = sample_rate * 10 / 1000; | 85 const int kSamplesPer10Ms = sample_rate * 10 / 1000; |
62 const int kFrameSizeBytes = frame_size_in_samples * | 86 const int kFrameSizeBytes = |
63 static_cast<int>(sizeof(int16_t)); | 87 frame_size_in_samples * static_cast<int>(sizeof(int16_t)); |
64 const int kNumberOfFrames = 5; | 88 const int kNumberOfFrames = 5; |
65 // Ceiling of integer division: 1 + ((x - 1) / y) | 89 // Ceiling of integer division: 1 + ((x - 1) / y) |
66 const int kNumberOfUpdateBufferCalls = | 90 const int kNumberOfUpdateBufferCalls = |
67 1 + ((kNumberOfFrames * frame_size_in_samples - 1) / kSamplesPer10Ms); | 91 1 + ((kNumberOfFrames * frame_size_in_samples - 1) / kSamplesPer10Ms); |
68 | 92 |
69 MockAudioDeviceBuffer audio_device_buffer; | 93 MockAudioDeviceBuffer audio_device_buffer; |
70 EXPECT_CALL(audio_device_buffer, RequestPlayoutData(_)) | 94 EXPECT_CALL(audio_device_buffer, RequestPlayoutData(_)) |
71 .WillRepeatedly(Return(kSamplesPer10Ms)); | 95 .WillRepeatedly(Return(kSamplesPer10Ms)); |
72 { | 96 { |
73 InSequence s; | 97 InSequence s; |
74 for (int i = 0; i < kNumberOfUpdateBufferCalls; ++i) { | 98 for (int i = 0; i < kNumberOfUpdateBufferCalls; ++i) { |
75 EXPECT_CALL(audio_device_buffer, GetPlayoutData(_)) | 99 EXPECT_CALL(audio_device_buffer, GetPlayoutData(_)) |
76 .WillOnce(UpdateBuffer(i, kSamplesPer10Ms)) | 100 .WillOnce(UpdateBuffer(i, kSamplesPer10Ms)) |
77 .RetiresOnSaturation(); | 101 .RetiresOnSaturation(); |
78 } | 102 } |
79 } | 103 } |
| 104 { |
| 105 InSequence s; |
| 106 for (int j = 0; j < kNumberOfUpdateBufferCalls - 1; ++j) { |
| 107 EXPECT_CALL(audio_device_buffer, SetRecordedBuffer(_, kSamplesPer10Ms)) |
| 108 .WillOnce(VerifyInputBuffer(j, kSamplesPer10Ms)) |
| 109 .RetiresOnSaturation(); |
| 110 } |
| 111 } |
| 112 EXPECT_CALL(audio_device_buffer, SetVQEData(_, _, _)) |
| 113 .Times(kNumberOfUpdateBufferCalls - 1); |
| 114 EXPECT_CALL(audio_device_buffer, DeliverRecordedData()) |
| 115 .Times(kNumberOfUpdateBufferCalls - 1) |
| 116 .WillRepeatedly(Return(kSamplesPer10Ms)); |
| 117 |
80 FineAudioBuffer fine_buffer(&audio_device_buffer, kFrameSizeBytes, | 118 FineAudioBuffer fine_buffer(&audio_device_buffer, kFrameSizeBytes, |
81 sample_rate); | 119 sample_rate); |
82 | 120 |
83 rtc::scoped_ptr<int8_t[]> out_buffer; | 121 rtc::scoped_ptr<int8_t[]> out_buffer; |
84 out_buffer.reset( | 122 out_buffer.reset(new int8_t[fine_buffer.RequiredPlayoutBufferSizeBytes()]); |
85 new int8_t[fine_buffer.RequiredBufferSizeBytes()]); | 123 rtc::scoped_ptr<int8_t[]> in_buffer; |
| 124 in_buffer.reset(new int8_t[kFrameSizeBytes]); |
86 for (int i = 0; i < kNumberOfFrames; ++i) { | 125 for (int i = 0; i < kNumberOfFrames; ++i) { |
87 fine_buffer.GetBufferData(out_buffer.get()); | 126 fine_buffer.GetPlayoutData(out_buffer.get()); |
88 EXPECT_TRUE(VerifyBuffer(out_buffer.get(), i, kFrameSizeBytes)); | 127 EXPECT_TRUE(VerifyBuffer(out_buffer.get(), i, kFrameSizeBytes)); |
| 128 UpdateInputBuffer(in_buffer.get(), i, kFrameSizeBytes); |
| 129 fine_buffer.DeliverRecordedData(in_buffer.get(), kFrameSizeBytes, 0, 0); |
89 } | 130 } |
90 } | 131 } |
91 | 132 |
92 TEST(FineBufferTest, BufferLessThan10ms) { | 133 TEST(FineBufferTest, BufferLessThan10ms) { |
93 const int kSampleRate = 44100; | 134 const int kSampleRate = 44100; |
94 const int kSamplesPer10Ms = kSampleRate * 10 / 1000; | 135 const int kSamplesPer10Ms = kSampleRate * 10 / 1000; |
95 const int kFrameSizeSamples = kSamplesPer10Ms - 50; | 136 const int kFrameSizeSamples = kSamplesPer10Ms - 50; |
96 RunFineBufferTest(kSampleRate, kFrameSizeSamples); | 137 RunFineBufferTest(kSampleRate, kFrameSizeSamples); |
97 } | 138 } |
98 | 139 |
99 TEST(FineBufferTest, GreaterThan10ms) { | 140 TEST(FineBufferTest, GreaterThan10ms) { |
100 const int kSampleRate = 44100; | 141 const int kSampleRate = 44100; |
101 const int kSamplesPer10Ms = kSampleRate * 10 / 1000; | 142 const int kSamplesPer10Ms = kSampleRate * 10 / 1000; |
102 const int kFrameSizeSamples = kSamplesPer10Ms + 50; | 143 const int kFrameSizeSamples = kSamplesPer10Ms + 50; |
103 RunFineBufferTest(kSampleRate, kFrameSizeSamples); | 144 RunFineBufferTest(kSampleRate, kFrameSizeSamples); |
104 } | 145 } |
105 | 146 |
106 } // namespace webrtc | 147 } // namespace webrtc |
OLD | NEW |