OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/logging.h" | 5 #include "base/logging.h" |
6 #include "base/basictypes.h" | 6 #include "base/basictypes.h" |
7 #include "base/scoped_ptr.h" | 7 #include "base/scoped_ptr.h" |
8 #include "base/time.h" | 8 #include "base/time.h" |
| 9 #include "media/audio/fake_audio_output_stream.h" |
9 #include "media/audio/simple_sources.h" | 10 #include "media/audio/simple_sources.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
11 | 12 |
12 namespace { | 13 namespace { |
13 | 14 |
14 void GenerateRandomData(char* buffer, size_t len) { | 15 void GenerateRandomData(char* buffer, size_t len) { |
15 static bool called = false; | 16 static bool called = false; |
16 if (!called) { | 17 if (!called) { |
17 called = true; | 18 called = true; |
18 int seed = static_cast<int>(base::Time::Now().ToInternalValue()); | 19 int seed = static_cast<int>(base::Time::Now().ToInternalValue()); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 // Read everything from the push source. | 54 // Read everything from the push source. |
54 for (size_t i = 0; i < kDataSize; i += kReadSize) { | 55 for (size_t i = 0; i < kDataSize; i += kReadSize) { |
55 size_t size = std::min(kDataSize - i , kReadSize); | 56 size_t size = std::min(kDataSize - i , kReadSize); |
56 EXPECT_EQ(size, push_source.OnMoreData(NULL, read_data.get(), size)); | 57 EXPECT_EQ(size, push_source.OnMoreData(NULL, read_data.get(), size)); |
57 EXPECT_EQ(0, memcmp(data.get() + i, read_data.get(), size)); | 58 EXPECT_EQ(0, memcmp(data.get() + i, read_data.get(), size)); |
58 } | 59 } |
59 EXPECT_EQ(0u, push_source.UnProcessedBytes()); | 60 EXPECT_EQ(0u, push_source.UnProcessedBytes()); |
60 | 61 |
61 push_source.OnClose(NULL); | 62 push_source.OnClose(NULL); |
62 } | 63 } |
| 64 |
| 65 // Validate that the SineWaveAudioSource writes the expected values for |
| 66 // the FORMAT_16BIT_MONO. The values are carefully selected so rounding issues |
| 67 // do not affect the result. We also test that AudioManager::GetLastMockBuffer |
| 68 // works. |
| 69 TEST(SimpleSources, SineWaveAudio16MonoTest) { |
| 70 const size_t samples = 1024; |
| 71 const size_t bytes_per_sample = 2; |
| 72 const int freq = 200; |
| 73 |
| 74 SineWaveAudioSource source(SineWaveAudioSource::FORMAT_16BIT_LINEAR_PCM, 1, |
| 75 freq, AudioManager::kTelephoneSampleRate); |
| 76 |
| 77 AudioManager* audio_man = AudioManager::GetAudioManager(); |
| 78 ASSERT_TRUE(NULL != audio_man); |
| 79 AudioOutputStream* oas = |
| 80 audio_man->MakeAudioStream(AudioManager::AUDIO_MOCK, 1, |
| 81 AudioManager::kTelephoneSampleRate, |
| 82 bytes_per_sample * 2); |
| 83 ASSERT_TRUE(NULL != oas); |
| 84 EXPECT_TRUE(oas->Open(samples * bytes_per_sample)); |
| 85 |
| 86 oas->Start(&source); |
| 87 oas->Stop(); |
| 88 oas->Close(); |
| 89 |
| 90 ASSERT_TRUE(FakeAudioOutputStream::GetLastFakeStream()); |
| 91 const int16* last_buffer = |
| 92 reinterpret_cast<int16*>( |
| 93 FakeAudioOutputStream::GetLastFakeStream()->buffer()); |
| 94 ASSERT_TRUE(NULL != last_buffer); |
| 95 |
| 96 size_t half_period = AudioManager::kTelephoneSampleRate / (freq * 2); |
| 97 |
| 98 // Spot test positive incursion of sine wave. |
| 99 EXPECT_EQ(0, last_buffer[0]); |
| 100 EXPECT_EQ(5126, last_buffer[1]); |
| 101 EXPECT_TRUE(last_buffer[1] < last_buffer[2]); |
| 102 EXPECT_TRUE(last_buffer[2] < last_buffer[3]); |
| 103 // Spot test negative incursion of sine wave. |
| 104 EXPECT_EQ(0, last_buffer[half_period]); |
| 105 EXPECT_EQ(-5126, last_buffer[half_period + 1]); |
| 106 EXPECT_TRUE(last_buffer[half_period + 1] > last_buffer[half_period + 2]); |
| 107 EXPECT_TRUE(last_buffer[half_period + 2] > last_buffer[half_period + 3]); |
| 108 } |
OLD | NEW |