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/android/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/modules/audio_device/mock_audio_device_buffer.h"
19#include "webrtc/system_wrappers/interface/scoped_ptr.h"
20
21using ::testing::_;
22using ::testing::InSequence;
23using ::testing::Return;
24
25namespace webrtc {
26
27// 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// 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// will happen.
32// |buffer| is the audio buffer to verify.
33bool VerifyBuffer(const int8_t* buffer, int buffer_number, int size) {
34  int start_value = (buffer_number * size) % SCHAR_MAX;
35  for (int i = 0; i < size; ++i) {
36    if (buffer[i] != (i + start_value) % SCHAR_MAX) {
37      return false;
38    }
39  }
40  return true;
41}
42
43// This function replaces GetPlayoutData when it's called (which is done
44// implicitly when calling GetBufferData). It writes the sequence
45// 0,1,..SCHAR_MAX-1,0,1,... to the buffer. Note that this is likely a buffer of
46// different size than the one VerifyBuffer verifies.
47// |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// buffer (|arg0|).
50ACTION_P2(UpdateBuffer, iteration, samples_per_10_ms) {
51  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 start_value = (iteration * bytes_per_10_ms) % SCHAR_MAX;
54  for (int i = 0; i < bytes_per_10_ms; ++i) {
55    buffer[i] = (i + start_value) % SCHAR_MAX;
56  }
57  return samples_per_10_ms;
58}
59
60void RunFineBufferTest(int sample_rate, int frame_size_in_samples) {
61  const int kSamplesPer10Ms = sample_rate * 10 / 1000;
62  const int kFrameSizeBytes = frame_size_in_samples *
63      static_cast<int>(sizeof(int16_t));
64  const int kNumberOfFrames = 5;
65  // Ceiling of integer division: 1 + ((x - 1) / y)
66  const int kNumberOfUpdateBufferCalls =
67      1 + ((kNumberOfFrames * frame_size_in_samples - 1) / kSamplesPer10Ms);
68
69  MockAudioDeviceBuffer audio_device_buffer;
70  EXPECT_CALL(audio_device_buffer, RequestPlayoutData(_))
71      .WillRepeatedly(Return(kSamplesPer10Ms));
72  {
73    InSequence s;
74    for (int i = 0; i < kNumberOfUpdateBufferCalls; ++i) {
75      EXPECT_CALL(audio_device_buffer, GetPlayoutData(_))
76          .WillOnce(UpdateBuffer(i, kSamplesPer10Ms))
77          .RetiresOnSaturation();
78    }
79  }
80  FineAudioBuffer fine_buffer(&audio_device_buffer, kFrameSizeBytes,
81                              sample_rate);
82
83  scoped_ptr<int8_t[]> out_buffer;
84  out_buffer.reset(
85      new int8_t[fine_buffer.RequiredBufferSizeBytes()]);
86  for (int i = 0; i < kNumberOfFrames; ++i) {
87    fine_buffer.GetBufferData(out_buffer.get());
88    EXPECT_TRUE(VerifyBuffer(out_buffer.get(), i, kFrameSizeBytes));
89  }
90}
91
92TEST(FineBufferTest, BufferLessThan10ms) {
93  const int kSampleRate = 44100;
94  const int kSamplesPer10Ms = kSampleRate * 10 / 1000;
95  const int kFrameSizeSamples = kSamplesPer10Ms - 50;
96  RunFineBufferTest(kSampleRate, kFrameSizeSamples);
97}
98
99TEST(FineBufferTest, GreaterThan10ms) {
100  const int kSampleRate = 44100;
101  const int kSamplesPer10Ms = kSampleRate * 10 / 1000;
102  const int kFrameSizeSamples = kSamplesPer10Ms + 50;
103  RunFineBufferTest(kSampleRate, kFrameSizeSamples);
104}
105
106}  // namespace webrtc
107