1/* 2 * Copyright (c) 2013 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 11#include "webrtc/modules/audio_device/fine_audio_buffer.h" 12 13#include <limits.h> 14#include <memory> 15 16#include "testing/gmock/include/gmock/gmock.h" 17#include "testing/gtest/include/gtest/gtest.h" 18#include "webrtc/base/scoped_ptr.h" 19#include "webrtc/modules/audio_device/mock_audio_device_buffer.h" 20 21using ::testing::_; 22using ::testing::AtLeast; 23using ::testing::InSequence; 24using ::testing::Return; 25 26namespace webrtc { 27 28// The fake audio data is 0,1,..SCHAR_MAX-1,0,1,... This is to make it easy 29// to detect errors. This function verifies that the buffers contain such data. 30// E.g. if there are two buffers of size 3, buffer 1 would contain 0,1,2 and 31// buffer 2 would contain 3,4,5. Note that SCHAR_MAX is 127 so wrap-around 32// will happen. 33// |buffer| is the audio buffer to verify. 34bool VerifyBuffer(const int8_t* buffer, int buffer_number, int size) { 35 int start_value = (buffer_number * size) % SCHAR_MAX; 36 for (int i = 0; i < size; ++i) { 37 if (buffer[i] != (i + start_value) % SCHAR_MAX) { 38 return false; 39 } 40 } 41 return true; 42} 43 44// This function replaces the real AudioDeviceBuffer::GetPlayoutData when it's 45// called (which is done implicitly when calling GetBufferData). It writes the 46// sequence 0,1,..SCHAR_MAX-1,0,1,... to the buffer. Note that this is likely a 47// buffer of different size than the one VerifyBuffer verifies. 48// |iteration| is the number of calls made to UpdateBuffer prior to this call. 49// |samples_per_10_ms| is the number of samples that should be written to the 50// buffer (|arg0|). 51ACTION_P2(UpdateBuffer, iteration, samples_per_10_ms) { 52 int8_t* buffer = static_cast<int8_t*>(arg0); 53 int bytes_per_10_ms = samples_per_10_ms * static_cast<int>(sizeof(int16_t)); 54 int start_value = (iteration * bytes_per_10_ms) % SCHAR_MAX; 55 for (int i = 0; i < bytes_per_10_ms; ++i) { 56 buffer[i] = (i + start_value) % SCHAR_MAX; 57 } 58 return samples_per_10_ms; 59} 60 61// Writes a periodic ramp pattern to the supplied |buffer|. See UpdateBuffer() 62// for details. 63void 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. 74ACTION_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 84void RunFineBufferTest(int sample_rate, int frame_size_in_samples) { 85 const int kSamplesPer10Ms = sample_rate * 10 / 1000; 86 const int kFrameSizeBytes = 87 frame_size_in_samples * static_cast<int>(sizeof(int16_t)); 88 const int kNumberOfFrames = 5; 89 // Ceiling of integer division: 1 + ((x - 1) / y) 90 const int kNumberOfUpdateBufferCalls = 91 1 + ((kNumberOfFrames * frame_size_in_samples - 1) / kSamplesPer10Ms); 92 93 MockAudioDeviceBuffer audio_device_buffer; 94 EXPECT_CALL(audio_device_buffer, RequestPlayoutData(_)) 95 .WillRepeatedly(Return(kSamplesPer10Ms)); 96 { 97 InSequence s; 98 for (int i = 0; i < kNumberOfUpdateBufferCalls; ++i) { 99 EXPECT_CALL(audio_device_buffer, GetPlayoutData(_)) 100 .WillOnce(UpdateBuffer(i, kSamplesPer10Ms)) 101 .RetiresOnSaturation(); 102 } 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 118 FineAudioBuffer fine_buffer(&audio_device_buffer, kFrameSizeBytes, 119 sample_rate); 120 121 rtc::scoped_ptr<int8_t[]> out_buffer; 122 out_buffer.reset(new int8_t[fine_buffer.RequiredPlayoutBufferSizeBytes()]); 123 rtc::scoped_ptr<int8_t[]> in_buffer; 124 in_buffer.reset(new int8_t[kFrameSizeBytes]); 125 for (int i = 0; i < kNumberOfFrames; ++i) { 126 fine_buffer.GetPlayoutData(out_buffer.get()); 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); 130 } 131} 132 133TEST(FineBufferTest, BufferLessThan10ms) { 134 const int kSampleRate = 44100; 135 const int kSamplesPer10Ms = kSampleRate * 10 / 1000; 136 const int kFrameSizeSamples = kSamplesPer10Ms - 50; 137 RunFineBufferTest(kSampleRate, kFrameSizeSamples); 138} 139 140TEST(FineBufferTest, GreaterThan10ms) { 141 const int kSampleRate = 44100; 142 const int kSamplesPer10Ms = kSampleRate * 10 / 1000; 143 const int kFrameSizeSamples = kSamplesPer10Ms + 50; 144 RunFineBufferTest(kSampleRate, kFrameSizeSamples); 145} 146 147} // namespace webrtc 148