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