1/*
2 *  Copyright (c) 2015 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/base/checks.h"
12#include "webrtc/base/safe_conversions.h"
13#include "webrtc/base/scoped_ptr.h"
14#include "webrtc/modules/audio_coding/codecs/ilbc/audio_encoder_ilbc.h"
15#include "webrtc/modules/audio_coding/neteq/tools/neteq_quality_test.h"
16#include "webrtc/test/testsupport/fileutils.h"
17
18using google::RegisterFlagValidator;
19using google::ParseCommandLineFlags;
20using std::string;
21using testing::InitGoogleTest;
22
23namespace webrtc {
24namespace test {
25namespace {
26static const int kInputSampleRateKhz = 8;
27static const int kOutputSampleRateKhz = 8;
28
29// Define switch for frame size.
30static bool ValidateFrameSize(const char* flagname, int32_t value) {
31  if (value == 20 || value == 30 || value == 40 || value == 60)
32    return true;
33  printf("Invalid frame size, should be 20, 30, 40, or 60 ms.");
34  return false;
35}
36
37DEFINE_int32(frame_size_ms, 20, "Codec frame size (milliseconds).");
38
39static const bool frame_size_dummy =
40    RegisterFlagValidator(&FLAGS_frame_size_ms, &ValidateFrameSize);
41
42}  // namespace
43
44class NetEqIlbcQualityTest : public NetEqQualityTest {
45 protected:
46  NetEqIlbcQualityTest()
47      : NetEqQualityTest(FLAGS_frame_size_ms,
48                         kInputSampleRateKhz,
49                         kOutputSampleRateKhz,
50                         NetEqDecoder::kDecoderILBC) {}
51
52  void SetUp() override {
53    ASSERT_EQ(1u, channels_) << "iLBC supports only mono audio.";
54    AudioEncoderIlbc::Config config;
55    config.frame_size_ms = FLAGS_frame_size_ms;
56    encoder_.reset(new AudioEncoderIlbc(config));
57    NetEqQualityTest::SetUp();
58  }
59
60  int EncodeBlock(int16_t* in_data,
61                  size_t block_size_samples,
62                  uint8_t* payload,
63                  size_t max_bytes) override {
64    const size_t kFrameSizeSamples = 80;  // Samples per 10 ms.
65    size_t encoded_samples = 0;
66    uint32_t dummy_timestamp = 0;
67    AudioEncoder::EncodedInfo info;
68    do {
69      info = encoder_->Encode(dummy_timestamp,
70                              rtc::ArrayView<const int16_t>(
71                                  in_data + encoded_samples, kFrameSizeSamples),
72                              max_bytes, payload);
73      encoded_samples += kFrameSizeSamples;
74    } while (info.encoded_bytes == 0);
75    return rtc::checked_cast<int>(info.encoded_bytes);
76  }
77
78 private:
79  rtc::scoped_ptr<AudioEncoderIlbc> encoder_;
80};
81
82TEST_F(NetEqIlbcQualityTest, Test) {
83  Simulate();
84}
85
86}  // namespace test
87}  // namespace webrtc
88