OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "remoting/protocol/webrtc_audio_source_adapter.h" |
| 6 |
| 7 #include <numeric> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/run_loop.h" |
| 13 #include "remoting/proto/audio.pb.h" |
| 14 #include "remoting/protocol/fake_audio_source.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "third_party/webrtc/api/mediastreaminterface.h" |
| 18 #include "third_party/webrtc/base/refcount.h" |
| 19 |
| 20 namespace remoting { |
| 21 namespace protocol { |
| 22 |
| 23 namespace { |
| 24 |
| 25 const int kSampleRate = 48000; |
| 26 const int kBytesPerSample = 2; |
| 27 const int kChannels = 2; |
| 28 constexpr base::TimeDelta kFrameDuration = |
| 29 base::TimeDelta::FromMilliseconds(10); |
| 30 |
| 31 class FakeAudioSink : public webrtc::AudioTrackSinkInterface{ |
| 32 public: |
| 33 FakeAudioSink() {} |
| 34 ~FakeAudioSink() override {} |
| 35 |
| 36 void OnData(const void* audio_data, |
| 37 int bits_per_sample, |
| 38 int sample_rate, |
| 39 size_t number_of_channels, |
| 40 size_t number_of_samples) override { |
| 41 EXPECT_EQ(kSampleRate, sample_rate); |
| 42 EXPECT_EQ(kBytesPerSample * 8, bits_per_sample); |
| 43 EXPECT_EQ(kChannels, static_cast<int>(number_of_channels)); |
| 44 EXPECT_EQ(kSampleRate * kFrameDuration / base::TimeDelta::FromSeconds(1), |
| 45 static_cast<int>(number_of_samples)); |
| 46 const int16_t* samples = reinterpret_cast<const int16_t*>(audio_data); |
| 47 samples_.insert(samples_.end(), samples, |
| 48 samples + number_of_samples * kChannels); |
| 49 } |
| 50 |
| 51 const std::vector<int16_t>& samples() { return samples_; } |
| 52 |
| 53 private: |
| 54 std::vector<int16_t> samples_; |
| 55 }; |
| 56 |
| 57 } // namespace |
| 58 |
| 59 class WebrtcAudioSourceAdapterTest : public testing::Test { |
| 60 public: |
| 61 void SetUp() override { |
| 62 audio_source_adapter_ = new rtc::RefCountedObject<WebrtcAudioSourceAdapter>( |
| 63 message_loop_.task_runner()); |
| 64 audio_source_ = new FakeAudioSource(); |
| 65 audio_source_adapter_->Start(base::WrapUnique(audio_source_)); |
| 66 audio_source_adapter_->AddSink(&sink_); |
| 67 base::RunLoop().RunUntilIdle(); |
| 68 } |
| 69 |
| 70 void TearDown() override { |
| 71 audio_source_adapter_ = nullptr; |
| 72 base::RunLoop().RunUntilIdle(); |
| 73 } |
| 74 |
| 75 protected: |
| 76 base::MessageLoop message_loop_; |
| 77 FakeAudioSource* audio_source_; |
| 78 scoped_refptr<WebrtcAudioSourceAdapter> audio_source_adapter_; |
| 79 FakeAudioSink sink_; |
| 80 }; |
| 81 |
| 82 TEST_F(WebrtcAudioSourceAdapterTest, PartialFrames) { |
| 83 int16_t sample_value = 1; |
| 84 std::vector<int> frame_sizes_ms = {10, 12, 18, 2, 5, 7, 55, 13, 8}; |
| 85 for (int frame_size_ms : frame_sizes_ms) { |
| 86 int num_samples = frame_size_ms * kSampleRate / 1000; |
| 87 std::vector<int16_t> data(num_samples * kChannels); |
| 88 for (int i = 0; i < num_samples; ++i) { |
| 89 data[i * kChannels] = sample_value; |
| 90 data[i * kChannels + 1] = -sample_value; |
| 91 ++sample_value; |
| 92 } |
| 93 |
| 94 std::unique_ptr<AudioPacket> packet(new AudioPacket()); |
| 95 packet->add_data(reinterpret_cast<char*>(&(data[0])), |
| 96 num_samples * kChannels * sizeof(int16_t)); |
| 97 packet->set_encoding(AudioPacket::ENCODING_RAW); |
| 98 packet->set_sampling_rate(AudioPacket::SAMPLING_RATE_48000); |
| 99 packet->set_bytes_per_sample(AudioPacket::BYTES_PER_SAMPLE_2); |
| 100 packet->set_channels(AudioPacket::CHANNELS_STEREO); |
| 101 audio_source_->callback().Run(std::move(packet)); |
| 102 } |
| 103 |
| 104 int total_length_ms = |
| 105 std::accumulate(frame_sizes_ms.begin(), frame_sizes_ms.end(), 0, |
| 106 [](int sum, int x) { return sum + x; }); |
| 107 const std::vector<int16_t>& received = sink_.samples(); |
| 108 int total_samples = total_length_ms * kSampleRate / 1000; |
| 109 ASSERT_EQ(total_samples * kChannels, static_cast<int>(received.size())); |
| 110 sample_value = 1; |
| 111 for (int i = 0; i < total_samples; ++i) { |
| 112 ASSERT_EQ(sample_value, received[i * kChannels]) << i; |
| 113 ASSERT_EQ(-sample_value, received[i * kChannels + 1]); |
| 114 ++sample_value; |
| 115 } |
| 116 } |
| 117 |
| 118 |
| 119 } // namespace protocol |
| 120 } // namespace remoting |
OLD | NEW |