| 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/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 "webrtc/base/array_view.h" |
| 16 #include "webrtc/modules/audio_device/mock_audio_device_buffer.h" | 17 #include "webrtc/modules/audio_device/mock_audio_device_buffer.h" |
| 17 #include "webrtc/test/gmock.h" | 18 #include "webrtc/test/gmock.h" |
| 18 #include "webrtc/test/gtest.h" | 19 #include "webrtc/test/gtest.h" |
| 19 | 20 |
| 20 using ::testing::_; | 21 using ::testing::_; |
| 21 using ::testing::AtLeast; | 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 |
| 28 const int kSampleRate = 44100; |
| 29 const int kSamplesPer10Ms = kSampleRate * 10 / 1000; |
| 30 |
| 27 // The fake audio data is 0,1,..SCHAR_MAX-1,0,1,... This is to make it easy | 31 // 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. | 32 // 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 | 33 // 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 | 34 // buffer 2 would contain 3,4,5. Note that SCHAR_MAX is 127 so wrap-around |
| 31 // will happen. | 35 // will happen. |
| 32 // |buffer| is the audio buffer to verify. | 36 // |buffer| is the audio buffer to verify. |
| 33 bool VerifyBuffer(const int8_t* buffer, int buffer_number, int size) { | 37 bool VerifyBuffer(const int8_t* buffer, int buffer_number, int size) { |
| 34 int start_value = (buffer_number * size) % SCHAR_MAX; | 38 int start_value = (buffer_number * size) % SCHAR_MAX; |
| 35 for (int i = 0; i < size; ++i) { | 39 for (int i = 0; i < size; ++i) { |
| 36 if (buffer[i] != (i + start_value) % SCHAR_MAX) { | 40 if (buffer[i] != (i + start_value) % SCHAR_MAX) { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 ACTION_P2(VerifyInputBuffer, iteration, samples_per_10_ms) { | 77 ACTION_P2(VerifyInputBuffer, iteration, samples_per_10_ms) { |
| 74 const int8_t* buffer = static_cast<const int8_t*>(arg0); | 78 const int8_t* buffer = static_cast<const int8_t*>(arg0); |
| 75 int bytes_per_10_ms = samples_per_10_ms * static_cast<int>(sizeof(int16_t)); | 79 int bytes_per_10_ms = samples_per_10_ms * static_cast<int>(sizeof(int16_t)); |
| 76 int start_value = (iteration * bytes_per_10_ms) % SCHAR_MAX; | 80 int start_value = (iteration * bytes_per_10_ms) % SCHAR_MAX; |
| 77 for (int i = 0; i < bytes_per_10_ms; ++i) { | 81 for (int i = 0; i < bytes_per_10_ms; ++i) { |
| 78 EXPECT_EQ(buffer[i], (i + start_value) % SCHAR_MAX); | 82 EXPECT_EQ(buffer[i], (i + start_value) % SCHAR_MAX); |
| 79 } | 83 } |
| 80 return 0; | 84 return 0; |
| 81 } | 85 } |
| 82 | 86 |
| 83 void RunFineBufferTest(int sample_rate, int frame_size_in_samples) { | 87 void RunFineBufferTest(int frame_size_in_samples) { |
| 84 const int kSamplesPer10Ms = sample_rate * 10 / 1000; | |
| 85 const int kFrameSizeBytes = | 88 const int kFrameSizeBytes = |
| 86 frame_size_in_samples * static_cast<int>(sizeof(int16_t)); | 89 frame_size_in_samples * static_cast<int>(sizeof(int16_t)); |
| 87 const int kNumberOfFrames = 5; | 90 const int kNumberOfFrames = 5; |
| 88 // Ceiling of integer division: 1 + ((x - 1) / y) | 91 // Ceiling of integer division: 1 + ((x - 1) / y) |
| 89 const int kNumberOfUpdateBufferCalls = | 92 const int kNumberOfUpdateBufferCalls = |
| 90 1 + ((kNumberOfFrames * frame_size_in_samples - 1) / kSamplesPer10Ms); | 93 1 + ((kNumberOfFrames * frame_size_in_samples - 1) / kSamplesPer10Ms); |
| 91 | 94 |
| 92 MockAudioDeviceBuffer audio_device_buffer; | 95 MockAudioDeviceBuffer audio_device_buffer; |
| 93 EXPECT_CALL(audio_device_buffer, RequestPlayoutData(_)) | 96 EXPECT_CALL(audio_device_buffer, RequestPlayoutData(_)) |
| 94 .WillRepeatedly(Return(kSamplesPer10Ms)); | 97 .WillRepeatedly(Return(kSamplesPer10Ms)); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 107 .WillOnce(VerifyInputBuffer(j, kSamplesPer10Ms)) | 110 .WillOnce(VerifyInputBuffer(j, kSamplesPer10Ms)) |
| 108 .RetiresOnSaturation(); | 111 .RetiresOnSaturation(); |
| 109 } | 112 } |
| 110 } | 113 } |
| 111 EXPECT_CALL(audio_device_buffer, SetVQEData(_, _, _)) | 114 EXPECT_CALL(audio_device_buffer, SetVQEData(_, _, _)) |
| 112 .Times(kNumberOfUpdateBufferCalls - 1); | 115 .Times(kNumberOfUpdateBufferCalls - 1); |
| 113 EXPECT_CALL(audio_device_buffer, DeliverRecordedData()) | 116 EXPECT_CALL(audio_device_buffer, DeliverRecordedData()) |
| 114 .Times(kNumberOfUpdateBufferCalls - 1) | 117 .Times(kNumberOfUpdateBufferCalls - 1) |
| 115 .WillRepeatedly(Return(kSamplesPer10Ms)); | 118 .WillRepeatedly(Return(kSamplesPer10Ms)); |
| 116 | 119 |
| 117 FineAudioBuffer fine_buffer(&audio_device_buffer, kFrameSizeBytes, | 120 FineAudioBuffer fine_buffer(&audio_device_buffer, kSampleRate, |
| 118 sample_rate); | 121 kFrameSizeBytes); |
| 122 std::unique_ptr<int8_t[]> out_buffer(new int8_t[kFrameSizeBytes]); |
| 123 std::unique_ptr<int8_t[]> in_buffer(new int8_t[kFrameSizeBytes]); |
| 119 | 124 |
| 120 std::unique_ptr<int8_t[]> out_buffer; | |
| 121 out_buffer.reset(new int8_t[kFrameSizeBytes]); | |
| 122 std::unique_ptr<int8_t[]> in_buffer; | |
| 123 in_buffer.reset(new int8_t[kFrameSizeBytes]); | |
| 124 for (int i = 0; i < kNumberOfFrames; ++i) { | 125 for (int i = 0; i < kNumberOfFrames; ++i) { |
| 125 fine_buffer.GetPlayoutData(out_buffer.get()); | 126 fine_buffer.GetPlayoutData( |
| 127 rtc::ArrayView<int8_t>(out_buffer.get(), kFrameSizeBytes)); |
| 126 EXPECT_TRUE(VerifyBuffer(out_buffer.get(), i, kFrameSizeBytes)); | 128 EXPECT_TRUE(VerifyBuffer(out_buffer.get(), i, kFrameSizeBytes)); |
| 127 UpdateInputBuffer(in_buffer.get(), i, kFrameSizeBytes); | 129 UpdateInputBuffer(in_buffer.get(), i, kFrameSizeBytes); |
| 128 fine_buffer.DeliverRecordedData(in_buffer.get(), kFrameSizeBytes, 0, 0); | 130 fine_buffer.DeliverRecordedData( |
| 131 rtc::ArrayView<const int8_t>(in_buffer.get(), kFrameSizeBytes), 0, 0); |
| 129 } | 132 } |
| 130 } | 133 } |
| 131 | 134 |
| 132 TEST(FineBufferTest, BufferLessThan10ms) { | 135 TEST(FineBufferTest, BufferLessThan10ms) { |
| 133 const int kSampleRate = 44100; | |
| 134 const int kSamplesPer10Ms = kSampleRate * 10 / 1000; | |
| 135 const int kFrameSizeSamples = kSamplesPer10Ms - 50; | 136 const int kFrameSizeSamples = kSamplesPer10Ms - 50; |
| 136 RunFineBufferTest(kSampleRate, kFrameSizeSamples); | 137 RunFineBufferTest(kFrameSizeSamples); |
| 137 } | 138 } |
| 138 | 139 |
| 139 TEST(FineBufferTest, GreaterThan10ms) { | 140 TEST(FineBufferTest, GreaterThan10ms) { |
| 140 const int kSampleRate = 44100; | |
| 141 const int kSamplesPer10Ms = kSampleRate * 10 / 1000; | |
| 142 const int kFrameSizeSamples = kSamplesPer10Ms + 50; | 141 const int kFrameSizeSamples = kSamplesPer10Ms + 50; |
| 143 RunFineBufferTest(kSampleRate, kFrameSizeSamples); | 142 RunFineBufferTest(kFrameSizeSamples); |
| 144 } | 143 } |
| 145 | 144 |
| 146 } // namespace webrtc | 145 } // namespace webrtc |
| OLD | NEW |