OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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 "base/bind.h" |
| 6 #include "base/message_loop/message_loop.h" |
| 7 #include "base/run_loop.h" |
| 8 #include "base/threading/thread.h" |
| 9 #include "remoting/base/auto_thread.h" |
| 10 #include "remoting/base/auto_thread_task_runner.h" |
| 11 #include "remoting/client/audio_decode_scheduler.h" |
| 12 #include "remoting/client/fake_audio_consumer.h" |
| 13 #include "remoting/protocol/session_config.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace { |
| 17 |
| 18 const int kAudioSampleBytes = 4; |
| 19 const uint8_t kDummyAudioData = 0x8B; |
| 20 |
| 21 } // namespace |
| 22 |
| 23 namespace remoting { |
| 24 |
| 25 class AudioDecodeSchedulerTest : public ::testing::Test { |
| 26 public: |
| 27 AudioDecodeSchedulerTest() {} |
| 28 |
| 29 void SetUp() override; |
| 30 void TearDown() override; |
| 31 |
| 32 protected: |
| 33 base::MessageLoop message_loop_; |
| 34 base::RunLoop run_loop_; |
| 35 scoped_refptr<AutoThreadTaskRunner> audio_decode_task_runner_; |
| 36 scoped_refptr<AutoThreadTaskRunner> main_task_runner_; |
| 37 std::unique_ptr<protocol::SessionConfig> session_config_; |
| 38 }; |
| 39 |
| 40 void AudioDecodeSchedulerTest::SetUp() { |
| 41 main_task_runner_ = new AutoThreadTaskRunner(message_loop_.task_runner(), |
| 42 run_loop_.QuitClosure()); |
| 43 audio_decode_task_runner_ = AutoThread::Create("decode", main_task_runner_); |
| 44 session_config_ = protocol::SessionConfig::ForTestWithAudio(); |
| 45 } |
| 46 |
| 47 void AudioDecodeSchedulerTest::TearDown() { |
| 48 // Release the task runners, so that the test can quit. |
| 49 audio_decode_task_runner_ = nullptr; |
| 50 main_task_runner_ = nullptr; |
| 51 |
| 52 // Run the MessageLoop until everything has torn down. |
| 53 run_loop_.Run(); |
| 54 } |
| 55 |
| 56 // TODO:: could share the following in a common class for use other places. |
| 57 std::unique_ptr<AudioPacket> CreatePacketWithSamplingRate_( |
| 58 AudioPacket::SamplingRate rate, |
| 59 int samples) { |
| 60 std::unique_ptr<AudioPacket> packet(new AudioPacket()); |
| 61 packet->set_encoding(AudioPacket::ENCODING_RAW); |
| 62 packet->set_sampling_rate(rate); |
| 63 packet->set_bytes_per_sample(AudioPacket::BYTES_PER_SAMPLE_2); |
| 64 packet->set_channels(AudioPacket::CHANNELS_STEREO); |
| 65 |
| 66 // The data must be a multiple of 4 bytes (channels x bytes_per_sample). |
| 67 std::string data; |
| 68 data.resize(samples * kAudioSampleBytes, kDummyAudioData); |
| 69 packet->add_data(data); |
| 70 |
| 71 return packet; |
| 72 } |
| 73 |
| 74 std::unique_ptr<AudioPacket> CreatePacket44100Hz_(int samples) { |
| 75 return CreatePacketWithSamplingRate_(AudioPacket::SAMPLING_RATE_44100, |
| 76 samples); |
| 77 } |
| 78 |
| 79 std::unique_ptr<AudioPacket> CreatePacket48000Hz_(int samples) { |
| 80 return CreatePacketWithSamplingRate_(AudioPacket::SAMPLING_RATE_48000, |
| 81 samples); |
| 82 } |
| 83 |
| 84 void Done() {} |
| 85 |
| 86 TEST_F(AudioDecodeSchedulerTest, Shutdown) { |
| 87 std::unique_ptr<FakeAudioConsumer> audio_consumer(new FakeAudioConsumer()); |
| 88 std::unique_ptr<AudioDecodeScheduler> audio_scheduler( |
| 89 new AudioDecodeScheduler(main_task_runner_, audio_decode_task_runner_, |
| 90 audio_consumer->GetWeakPtr())); |
| 91 |
| 92 audio_scheduler->Initialize(*session_config_); |
| 93 |
| 94 audio_scheduler->ProcessAudioPacket(CreatePacket44100Hz_(1000), |
| 95 base::Bind(&Done)); |
| 96 |
| 97 audio_scheduler.reset(); |
| 98 audio_consumer.reset(); |
| 99 } |
| 100 |
| 101 } // namespace remoting |
OLD | NEW |