henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_coding/neteq/sync_buffer.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 12 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame^] | 13 | #include "rtc_base/numerics/safe_conversions.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "test/gtest.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 15 | |
| 16 | namespace webrtc { |
| 17 | |
| 18 | TEST(SyncBuffer, CreateAndDestroy) { |
| 19 | // Create a SyncBuffer with two channels and 10 samples each. |
| 20 | static const size_t kLen = 10; |
| 21 | static const size_t kChannels = 2; |
| 22 | SyncBuffer sync_buffer(kChannels, kLen); |
| 23 | EXPECT_EQ(kChannels, sync_buffer.Channels()); |
| 24 | EXPECT_EQ(kLen, sync_buffer.Size()); |
| 25 | // When the buffer is empty, the next index to play out is at the end. |
| 26 | EXPECT_EQ(kLen, sync_buffer.next_index()); |
| 27 | // Verify that all elements are zero. |
| 28 | for (size_t channel = 0; channel < kChannels; ++channel) { |
| 29 | for (size_t i = 0; i < kLen; ++i) { |
| 30 | EXPECT_EQ(0, sync_buffer[channel][i]); |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | TEST(SyncBuffer, SetNextIndex) { |
| 36 | // Create a SyncBuffer with two channels and 100 samples each. |
| 37 | static const size_t kLen = 100; |
| 38 | static const size_t kChannels = 2; |
| 39 | SyncBuffer sync_buffer(kChannels, kLen); |
| 40 | sync_buffer.set_next_index(0); |
| 41 | EXPECT_EQ(0u, sync_buffer.next_index()); |
| 42 | sync_buffer.set_next_index(kLen / 2); |
| 43 | EXPECT_EQ(kLen / 2, sync_buffer.next_index()); |
| 44 | sync_buffer.set_next_index(kLen); |
| 45 | EXPECT_EQ(kLen, sync_buffer.next_index()); |
| 46 | // Try to set larger than the buffer size; should cap at buffer size. |
| 47 | sync_buffer.set_next_index(kLen + 1); |
| 48 | EXPECT_EQ(kLen, sync_buffer.next_index()); |
| 49 | } |
| 50 | |
| 51 | TEST(SyncBuffer, PushBackAndFlush) { |
| 52 | // Create a SyncBuffer with two channels and 100 samples each. |
| 53 | static const size_t kLen = 100; |
| 54 | static const size_t kChannels = 2; |
| 55 | SyncBuffer sync_buffer(kChannels, kLen); |
| 56 | static const size_t kNewLen = 10; |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 +0000 | [diff] [blame] | 57 | AudioMultiVector new_data(kChannels, kNewLen); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 58 | // Populate |new_data|. |
| 59 | for (size_t channel = 0; channel < kChannels; ++channel) { |
| 60 | for (size_t i = 0; i < kNewLen; ++i) { |
Mirko Bonadei | 737e073 | 2017-10-19 09:00:17 +0200 | [diff] [blame] | 61 | new_data[channel][i] = rtc::checked_cast<int16_t>(i); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | // Push back |new_data| into |sync_buffer|. This operation should pop out |
| 65 | // data from the front of |sync_buffer|, so that the size of the buffer |
| 66 | // remains the same. The |next_index_| should also move with the same length. |
| 67 | sync_buffer.PushBack(new_data); |
| 68 | ASSERT_EQ(kLen, sync_buffer.Size()); |
| 69 | // Verify that |next_index_| moved accordingly. |
| 70 | EXPECT_EQ(kLen - kNewLen, sync_buffer.next_index()); |
| 71 | // Verify the new contents. |
| 72 | for (size_t channel = 0; channel < kChannels; ++channel) { |
| 73 | for (size_t i = 0; i < kNewLen; ++i) { |
| 74 | EXPECT_EQ(new_data[channel][i], |
| 75 | sync_buffer[channel][sync_buffer.next_index() + i]); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // Now flush the buffer, and verify that it is all zeros, and that next_index |
| 80 | // points to the end. |
| 81 | sync_buffer.Flush(); |
| 82 | ASSERT_EQ(kLen, sync_buffer.Size()); |
| 83 | EXPECT_EQ(kLen, sync_buffer.next_index()); |
| 84 | for (size_t channel = 0; channel < kChannels; ++channel) { |
| 85 | for (size_t i = 0; i < kLen; ++i) { |
| 86 | EXPECT_EQ(0, sync_buffer[channel][i]); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | TEST(SyncBuffer, PushFrontZeros) { |
| 92 | // Create a SyncBuffer with two channels and 100 samples each. |
| 93 | static const size_t kLen = 100; |
| 94 | static const size_t kChannels = 2; |
| 95 | SyncBuffer sync_buffer(kChannels, kLen); |
| 96 | static const size_t kNewLen = 10; |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 +0000 | [diff] [blame] | 97 | AudioMultiVector new_data(kChannels, kNewLen); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 98 | // Populate |new_data|. |
| 99 | for (size_t channel = 0; channel < kChannels; ++channel) { |
| 100 | for (size_t i = 0; i < kNewLen; ++i) { |
Mirko Bonadei | 737e073 | 2017-10-19 09:00:17 +0200 | [diff] [blame] | 101 | new_data[channel][i] = rtc::checked_cast<int16_t>(1000 + i); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | sync_buffer.PushBack(new_data); |
| 105 | EXPECT_EQ(kLen, sync_buffer.Size()); |
| 106 | |
| 107 | // Push |kNewLen| - 1 zeros into each channel in the front of the SyncBuffer. |
| 108 | sync_buffer.PushFrontZeros(kNewLen - 1); |
| 109 | EXPECT_EQ(kLen, sync_buffer.Size()); // Size should remain the same. |
| 110 | // Verify that |next_index_| moved accordingly. Should be at the end - 1. |
| 111 | EXPECT_EQ(kLen - 1, sync_buffer.next_index()); |
| 112 | // Verify the zeros. |
| 113 | for (size_t channel = 0; channel < kChannels; ++channel) { |
| 114 | for (size_t i = 0; i < kNewLen - 1; ++i) { |
| 115 | EXPECT_EQ(0, sync_buffer[channel][i]); |
| 116 | } |
| 117 | } |
| 118 | // Verify that the correct data is at the end of the SyncBuffer. |
| 119 | for (size_t channel = 0; channel < kChannels; ++channel) { |
| 120 | EXPECT_EQ(1000, sync_buffer[channel][sync_buffer.next_index()]); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | TEST(SyncBuffer, GetNextAudioInterleaved) { |
| 125 | // Create a SyncBuffer with two channels and 100 samples each. |
| 126 | static const size_t kLen = 100; |
| 127 | static const size_t kChannels = 2; |
| 128 | SyncBuffer sync_buffer(kChannels, kLen); |
| 129 | static const size_t kNewLen = 10; |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 +0000 | [diff] [blame] | 130 | AudioMultiVector new_data(kChannels, kNewLen); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 131 | // Populate |new_data|. |
| 132 | for (size_t channel = 0; channel < kChannels; ++channel) { |
| 133 | for (size_t i = 0; i < kNewLen; ++i) { |
Mirko Bonadei | 737e073 | 2017-10-19 09:00:17 +0200 | [diff] [blame] | 134 | new_data[channel][i] = rtc::checked_cast<int16_t>(i); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | // Push back |new_data| into |sync_buffer|. This operation should pop out |
| 138 | // data from the front of |sync_buffer|, so that the size of the buffer |
| 139 | // remains the same. The |next_index_| should also move with the same length. |
| 140 | sync_buffer.PushBack(new_data); |
| 141 | |
| 142 | // Read to interleaved output. Read in two batches, where each read operation |
| 143 | // should automatically update the |net_index_| in the SyncBuffer. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 144 | // Note that |samples_read| is the number of samples read from each channel. |
| 145 | // That is, the number of samples written to |output| is |
| 146 | // |samples_read| * |kChannels|. |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 147 | AudioFrame output1; |
| 148 | sync_buffer.GetNextAudioInterleaved(kNewLen / 2, &output1); |
| 149 | EXPECT_EQ(kChannels, output1.num_channels_); |
| 150 | EXPECT_EQ(kNewLen / 2, output1.samples_per_channel_); |
| 151 | |
| 152 | AudioFrame output2; |
| 153 | sync_buffer.GetNextAudioInterleaved(kNewLen / 2, &output2); |
| 154 | EXPECT_EQ(kChannels, output2.num_channels_); |
| 155 | EXPECT_EQ(kNewLen / 2, output2.samples_per_channel_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 156 | |
| 157 | // Verify the data. |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 158 | const int16_t* output_ptr = output1.data(); |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 159 | for (size_t i = 0; i < kNewLen / 2; ++i) { |
| 160 | for (size_t channel = 0; channel < kChannels; ++channel) { |
| 161 | EXPECT_EQ(new_data[channel][i], *output_ptr); |
| 162 | ++output_ptr; |
| 163 | } |
| 164 | } |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 165 | output_ptr = output2.data(); |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 166 | for (size_t i = kNewLen / 2; i < kNewLen; ++i) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 167 | for (size_t channel = 0; channel < kChannels; ++channel) { |
| 168 | EXPECT_EQ(new_data[channel][i], *output_ptr); |
| 169 | ++output_ptr; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | } // namespace webrtc |