File: webrtc_audio_source_adapter_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (123 lines) | stat: -rw-r--r-- 4,328 bytes parent folder | download | duplicates (5)
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
121
122
123
// Copyright 2016 The Chromium Authors
// 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 <algorithm>
#include <iterator>
#include <numeric>
#include <vector>

#include "base/containers/span.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/test/task_environment.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/media_stream_interface.h"
#include "third_party/webrtc/rtc_base/ref_count.h"
#include "third_party/webrtc/rtc_base/ref_counted_object.h"

namespace remoting::protocol {

namespace {

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

class FakeAudioSink : public webrtc::AudioTrackSinkInterface {
 public:
  FakeAudioSink() = default;
  ~FakeAudioSink() override = default;

  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).InSeconds(),
              static_cast<int>(number_of_samples));
    const int16_t* samples = static_cast<const int16_t*>(audio_data);
    size_t sample_count = number_of_samples * kChannels;
    samples_.reserve(samples_.size() + sample_count);
    std::copy_n(samples, sample_count, std::back_inserter(samples_));
  }

  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 webrtc::RefCountedObject<WebrtcAudioSourceAdapter>(
            task_environment_.GetMainThreadTaskRunner());
    audio_source_ = new FakeAudioSource();
    audio_source_adapter_->Start(base::WrapUnique(audio_source_.get()));
    audio_source_adapter_->AddSink(&sink_);
    base::RunLoop().RunUntilIdle();
  }

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

 protected:
  base::test::SingleThreadTaskEnvironment task_environment_;
  raw_ptr<FakeAudioSource, AcrossTasksDanglingUntriaged> 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());
    auto bytes = base::as_bytes(base::span(data));
    packet->add_data(reinterpret_cast<const char*>(bytes.data()), bytes.size());
    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 remoting::protocol