File: webrtc_audio_source_adapter_unittest.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (120 lines) | stat: -rw-r--r-- 4,060 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/protocol/webrtc_audio_source_adapter.h"

#include <numeric>
#include <vector>

#include "base/memory/ptr_util.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "remoting/proto/audio.pb.h"
#include "remoting/protocol/fake_audio_source.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/webrtc/api/mediastreaminterface.h"
#include "third_party/webrtc/base/refcount.h"

namespace remoting {
namespace protocol {

namespace {

const int kSampleRate = 48000;
const int kBytesPerSample = 2;
const int kChannels = 2;
constexpr base::TimeDelta kFrameDuration =
    base::TimeDelta::FromMilliseconds(10);

class FakeAudioSink : public webrtc::AudioTrackSinkInterface{
 public:
  FakeAudioSink() {}
  ~FakeAudioSink() override {}

  void OnData(const void* audio_data,
              int bits_per_sample,
              int sample_rate,
              size_t number_of_channels,
              size_t number_of_samples) override {
    EXPECT_EQ(kSampleRate, sample_rate);
    EXPECT_EQ(kBytesPerSample * 8, bits_per_sample);
    EXPECT_EQ(kChannels, static_cast<int>(number_of_channels));
    EXPECT_EQ(kSampleRate * kFrameDuration / base::TimeDelta::FromSeconds(1),
              static_cast<int>(number_of_samples));
    const int16_t* samples = reinterpret_cast<const int16_t*>(audio_data);
    samples_.insert(samples_.end(), samples,
                    samples + number_of_samples * kChannels);
  }

  const std::vector<int16_t>& samples() { return samples_; }

 private:
  std::vector<int16_t> samples_;
};

}  // namespace

class WebrtcAudioSourceAdapterTest : public testing::Test {
 public:
  void SetUp() override {
    audio_source_adapter_ = new rtc::RefCountedObject<WebrtcAudioSourceAdapter>(
        message_loop_.task_runner());
    audio_source_ = new FakeAudioSource();
    audio_source_adapter_->Start(base::WrapUnique(audio_source_));
    audio_source_adapter_->AddSink(&sink_);
    base::RunLoop().RunUntilIdle();
  }

  void TearDown() override {
    audio_source_adapter_ = nullptr;
    base::RunLoop().RunUntilIdle();
  }

 protected:
  base::MessageLoop message_loop_;
  FakeAudioSource* audio_source_;
  scoped_refptr<WebrtcAudioSourceAdapter> audio_source_adapter_;
  FakeAudioSink sink_;
};

TEST_F(WebrtcAudioSourceAdapterTest, PartialFrames) {
  int16_t sample_value = 1;
  std::vector<int> frame_sizes_ms = {10, 12, 18, 2, 5, 7, 55, 13, 8};
  for (int frame_size_ms : frame_sizes_ms) {
    int num_samples = frame_size_ms * kSampleRate / 1000;
    std::vector<int16_t> data(num_samples * kChannels);
    for (int i = 0; i < num_samples; ++i) {
      data[i * kChannels] = sample_value;
      data[i * kChannels + 1] = -sample_value;
      ++sample_value;
    }

    std::unique_ptr<AudioPacket> packet(new AudioPacket());
    packet->add_data(reinterpret_cast<char*>(&(data[0])),
                     num_samples * kChannels * sizeof(int16_t));
    packet->set_encoding(AudioPacket::ENCODING_RAW);
    packet->set_sampling_rate(AudioPacket::SAMPLING_RATE_48000);
    packet->set_bytes_per_sample(AudioPacket::BYTES_PER_SAMPLE_2);
    packet->set_channels(AudioPacket::CHANNELS_STEREO);
    audio_source_->callback().Run(std::move(packet));
  }

  int total_length_ms =
      std::accumulate(frame_sizes_ms.begin(), frame_sizes_ms.end(), 0,
                      [](int sum, int x) { return sum + x; });
  const std::vector<int16_t>& received = sink_.samples();
  int total_samples = total_length_ms * kSampleRate / 1000;
  ASSERT_EQ(total_samples * kChannels, static_cast<int>(received.size()));
  sample_value = 1;
  for (int i = 0; i < total_samples; ++i) {
    ASSERT_EQ(sample_value, received[i * kChannels]) << i;
    ASSERT_EQ(-sample_value, received[i * kChannels + 1]);
    ++sample_value;
  }
}


}  // namespace protocol
}  // namespace remoting