OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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/at_exit.h" |
| 6 #include "base/basictypes.h" |
| 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" |
| 9 #include "base/location.h" |
| 10 #include "base/single_thread_task_runner.h" |
| 11 #include "base/synchronization/waitable_event.h" |
| 12 #include "base/thread_task_runner_handle.h" |
| 13 #include "base/threading/thread.h" |
| 14 #include "chromecast/media/audio/cast_audio_manager_factory.h" |
| 15 #include "chromecast/public/cast_media_shlib.h" |
| 16 #include "media/audio/audio_io.h" |
| 17 #include "media/audio/audio_manager_base.h" |
| 18 #include "media/audio/audio_manager_factory.h" |
| 19 #include "media/audio/simple_sources.h" |
| 20 #include "media/base/bind_to_current_loop.h" |
| 21 #include "media/base/data_buffer.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" |
| 23 |
| 24 namespace chromecast { |
| 25 namespace media { |
| 26 |
| 27 namespace { |
| 28 base::WaitableEvent g_event(false, false); |
| 29 |
| 30 class SineWaveRenderer { |
| 31 public: |
| 32 SineWaveRenderer(int sampling_frequency, int sine_frequency) |
| 33 : sampling_frequency_(sampling_frequency) { |
| 34 sine_wave_source_.reset(new ::media::SineWaveAudioSource( |
| 35 1, sine_frequency, sampling_frequency_)); |
| 36 } |
| 37 |
| 38 void Play(const base::TimeDelta& sine_duration, |
| 39 const base::Closure& playback_done_cb) { |
| 40 playback_done_cb_ = playback_done_cb; |
| 41 ::media::AudioParameters params( |
| 42 ::media::AudioParameters::AUDIO_PCM_LOW_LATENCY, |
| 43 ::media::CHANNEL_LAYOUT_STEREO, |
| 44 sampling_frequency_, |
| 45 16, |
| 46 1024); |
| 47 |
| 48 audio_output_stream_ = |
| 49 ::media::AudioManager::Get()->MakeAudioOutputStream(params, ""); |
| 50 DCHECK(audio_output_stream_); |
| 51 audio_output_stream_->Open(); |
| 52 |
| 53 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 54 FROM_HERE, |
| 55 base::Bind(&SineWaveRenderer::OnPlaybackDone, base::Unretained(this)), |
| 56 sine_duration); |
| 57 audio_output_stream_->SetVolume(0.01); |
| 58 audio_output_stream_->Start(sine_wave_source_.get()); |
| 59 } |
| 60 |
| 61 protected: |
| 62 void OnPlaybackDone() { |
| 63 audio_output_stream_->Close(); |
| 64 audio_output_stream_ = NULL; |
| 65 playback_done_cb_.Run(); |
| 66 } |
| 67 |
| 68 private: |
| 69 int sampling_frequency_; |
| 70 scoped_ptr< ::media::SineWaveAudioSource> sine_wave_source_; |
| 71 ::media::AudioOutputStream* audio_output_stream_; |
| 72 base::Closure playback_done_cb_; |
| 73 |
| 74 DISALLOW_COPY_AND_ASSIGN(SineWaveRenderer); |
| 75 }; |
| 76 |
| 77 } // namespace |
| 78 |
| 79 class AudioOutputStreamTest : public testing::Test { |
| 80 public: |
| 81 void OnPlaybackDone(); |
| 82 |
| 83 protected: |
| 84 AudioOutputStreamTest(); |
| 85 ~AudioOutputStreamTest() override; |
| 86 |
| 87 // testing::Test implementation: |
| 88 void SetUp() override; |
| 89 void TearDown() override; |
| 90 |
| 91 int sine_count_; |
| 92 |
| 93 private: |
| 94 scoped_ptr< ::media::AudioManager> audio_manager_; |
| 95 |
| 96 DISALLOW_COPY_AND_ASSIGN(AudioOutputStreamTest); |
| 97 }; |
| 98 |
| 99 AudioOutputStreamTest::AudioOutputStreamTest() : sine_count_(0) { |
| 100 } |
| 101 |
| 102 AudioOutputStreamTest::~AudioOutputStreamTest() { |
| 103 } |
| 104 |
| 105 void AudioOutputStreamTest::SetUp() { |
| 106 ASSERT_TRUE(base::CommandLine::InitializedForCurrentProcess()); |
| 107 |
| 108 // Initialize the presentation engine. |
| 109 chromecast::media::CastMediaShlib::Initialize( |
| 110 base::CommandLine::ForCurrentProcess()->argv()); |
| 111 |
| 112 ::media::AudioManager::SetFactory(new CastAudioManagerFactory()); |
| 113 audio_manager_.reset(::media::AudioManager::CreateForTesting()); |
| 114 } |
| 115 |
| 116 void AudioOutputStreamTest::TearDown() { |
| 117 ::media::AudioManager::ResetFactoryForTesting(); |
| 118 |
| 119 // These must be called to reset underlying static instances. |
| 120 chromecast::media::CastMediaShlib::Finalize(); |
| 121 } |
| 122 |
| 123 void AudioOutputStreamTest::OnPlaybackDone() { |
| 124 sine_count_--; |
| 125 if (sine_count_ == 0) { |
| 126 g_event.Signal(); |
| 127 } |
| 128 } |
| 129 |
| 130 TEST_F(AudioOutputStreamTest, PlaybackSineWaves) { |
| 131 const base::TimeDelta duration(base::TimeDelta::FromSeconds(10)); |
| 132 const int sampling_frequency = 32000; |
| 133 |
| 134 scoped_ptr<base::Thread> audio_thread(new base::Thread("AudioThread")); |
| 135 audio_thread->Start(); |
| 136 |
| 137 base::Closure on_playback_done = base::Bind( |
| 138 &AudioOutputStreamTest::OnPlaybackDone, base::Unretained(this)); |
| 139 |
| 140 ++sine_count_; |
| 141 SineWaveRenderer renderer(sampling_frequency, 440); |
| 142 audio_thread->task_runner()->PostTask(FROM_HERE, |
| 143 base::Bind(&SineWaveRenderer::Play, |
| 144 base::Unretained(&renderer), |
| 145 duration, |
| 146 on_playback_done)); |
| 147 |
| 148 ++sine_count_; |
| 149 SineWaveRenderer renderer2(sampling_frequency, 330); |
| 150 audio_thread->task_runner()->PostTask(FROM_HERE, |
| 151 base::Bind(&SineWaveRenderer::Play, |
| 152 base::Unretained(&renderer2), |
| 153 duration, |
| 154 on_playback_done)); |
| 155 |
| 156 ++sine_count_; |
| 157 SineWaveRenderer renderer3(sampling_frequency, 220); |
| 158 audio_thread->task_runner()->PostTask(FROM_HERE, |
| 159 base::Bind(&SineWaveRenderer::Play, |
| 160 base::Unretained(&renderer3), |
| 161 duration, |
| 162 on_playback_done)); |
| 163 |
| 164 g_event.Wait(); |
| 165 audio_thread.reset(); |
| 166 } |
| 167 |
| 168 } // namespace media |
| 169 } // namespace chromecast |
OLD | NEW |