Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(924)

Side by Side Diff: media/audio/simple_sources_unittest.cc

Issue 10832285: Switch OnMoreData() to use AudioBus. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase. Comments. Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « media/audio/simple_sources.cc ('k') | media/audio/win/audio_low_latency_output_win.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 <algorithm> // std::min 5 #include <algorithm> // std::min
6 #include <limits>
6 7
7 #include "base/logging.h" 8 #include "base/logging.h"
8 #include "base/basictypes.h" 9 #include "base/basictypes.h"
9 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
10 #include "base/time.h" 11 #include "base/time.h"
11 #include "media/audio/audio_manager.h" 12 #include "media/audio/audio_manager.h"
12 #include "media/audio/fake_audio_output_stream.h" 13 #include "media/audio/fake_audio_output_stream.h"
13 #include "media/audio/simple_sources.h" 14 #include "media/audio/simple_sources.h"
15 #include "media/base/audio_bus.h"
14 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
15 17
16 namespace media { 18 namespace media {
17 19
18 static void GenerateRandomData(char* buffer, uint32 len) { 20 // Validate that the SineWaveAudioSource writes the expected values.
19 static bool called = false; 21 TEST(SimpleSources, SineWaveAudioSource) {
20 if (!called) {
21 called = true;
22 int seed = static_cast<int>(base::Time::Now().ToInternalValue());
23 srand(seed);
24 VLOG(1) << "Random seed: " << seed;
25 }
26
27 for (uint32 i = 0; i < len; i++)
28 buffer[i] = static_cast<char>(rand()); // NOLINT
29 }
30
31 // To test write size smaller than read size.
32 TEST(SimpleSourcesTest, PushSourceSmallerWrite) {
33 const uint32 kDataSize = 40960;
34 scoped_array<char> data(new char[kDataSize]);
35 GenerateRandomData(data.get(), kDataSize);
36
37 // Choose two prime numbers for read and write sizes.
38 const uint32 kWriteSize = 283;
39 const uint32 kReadSize = 293;
40 scoped_array<uint8> read_data(new uint8[kReadSize]);
41
42 // Create a PushSource.
43 PushSource push_source;
44 EXPECT_EQ(0u, push_source.UnProcessedBytes());
45
46 // Write everything into this push source.
47 for (uint32 i = 0; i < kDataSize; i += kWriteSize) {
48 uint32 size = std::min(kDataSize - i, kWriteSize);
49 EXPECT_TRUE(push_source.Write(data.get() + i, size));
50 }
51 EXPECT_EQ(kDataSize, push_source.UnProcessedBytes());
52
53 // Read everything from the push source.
54 for (uint32 i = 0; i < kDataSize; i += kReadSize) {
55 uint32 size = std::min(kDataSize - i , kReadSize);
56 EXPECT_EQ(size, push_source.OnMoreData(read_data.get(), size,
57 AudioBuffersState()));
58 EXPECT_EQ(0, memcmp(data.get() + i, read_data.get(), size));
59 }
60 EXPECT_EQ(0u, push_source.UnProcessedBytes());
61 }
62
63 // Validate that the SineWaveAudioSource writes the expected values for
64 // the FORMAT_16BIT_MONO. The values are carefully selected so rounding issues
65 // do not affect the result. We also test that AudioManager::GetLastMockBuffer
66 // works.
67 TEST(SimpleSources, SineWaveAudio16MonoTest) {
68 const uint32 samples = 1024; 22 const uint32 samples = 1024;
69 const uint32 bytes_per_sample = 2; 23 const uint32 bytes_per_sample = 2;
70 const int freq = 200; 24 const int freq = 200;
71 25
72 SineWaveAudioSource source(SineWaveAudioSource::FORMAT_16BIT_LINEAR_PCM, 1, 26 SineWaveAudioSource source(1, freq, AudioParameters::kTelephoneSampleRate);
73 freq, AudioParameters::kTelephoneSampleRate);
74 27
75 scoped_ptr<AudioManager> audio_man(AudioManager::Create()); 28 scoped_ptr<AudioManager> audio_man(AudioManager::Create());
76 AudioParameters params( 29 AudioParameters params(
77 AudioParameters::AUDIO_MOCK, CHANNEL_LAYOUT_MONO, 30 AudioParameters::AUDIO_MOCK, CHANNEL_LAYOUT_MONO,
78 AudioParameters::kTelephoneSampleRate, bytes_per_sample * 8, samples); 31 AudioParameters::kTelephoneSampleRate, bytes_per_sample * 8, samples);
79 AudioOutputStream* oas = audio_man->MakeAudioOutputStream(params); 32 AudioOutputStream* oas = audio_man->MakeAudioOutputStream(params);
80 ASSERT_TRUE(NULL != oas); 33 ASSERT_TRUE(NULL != oas);
81 EXPECT_TRUE(oas->Open()); 34 EXPECT_TRUE(oas->Open());
82 35
83 oas->Start(&source); 36 oas->Start(&source);
84 oas->Stop(); 37 oas->Stop();
85 38
86 ASSERT_TRUE(FakeAudioOutputStream::GetCurrentFakeStream()); 39 ASSERT_TRUE(FakeAudioOutputStream::GetCurrentFakeStream());
87 const int16* last_buffer = 40 const AudioBus* last_audio_bus =
88 reinterpret_cast<int16*>( 41 FakeAudioOutputStream::GetCurrentFakeStream()->audio_bus();
89 FakeAudioOutputStream::GetCurrentFakeStream()->buffer()); 42 ASSERT_TRUE(NULL != last_audio_bus);
90 ASSERT_TRUE(NULL != last_buffer);
91 43
92 uint32 half_period = AudioParameters::kTelephoneSampleRate / (freq * 2); 44 uint32 half_period = AudioParameters::kTelephoneSampleRate / (freq * 2);
93 45
94 // Spot test positive incursion of sine wave. 46 // Spot test positive incursion of sine wave.
95 EXPECT_EQ(0, last_buffer[0]); 47 EXPECT_NEAR(0, last_audio_bus->channel(0)[0],
96 EXPECT_EQ(5126, last_buffer[1]); 48 std::numeric_limits<float>::epsilon());
97 EXPECT_TRUE(last_buffer[1] < last_buffer[2]); 49 EXPECT_FLOAT_EQ(0.15643446f, last_audio_bus->channel(0)[1]);
98 EXPECT_TRUE(last_buffer[2] < last_buffer[3]); 50 EXPECT_LT(last_audio_bus->channel(0)[1], last_audio_bus->channel(0)[2]);
51 EXPECT_LT(last_audio_bus->channel(0)[2], last_audio_bus->channel(0)[3]);
99 // Spot test negative incursion of sine wave. 52 // Spot test negative incursion of sine wave.
100 EXPECT_EQ(0, last_buffer[half_period]); 53 EXPECT_NEAR(0, last_audio_bus->channel(0)[half_period],
101 EXPECT_EQ(-5126, last_buffer[half_period + 1]); 54 std::numeric_limits<float>::epsilon());
102 EXPECT_TRUE(last_buffer[half_period + 1] > last_buffer[half_period + 2]); 55 EXPECT_FLOAT_EQ(-0.15643446f, last_audio_bus->channel(0)[half_period + 1]);
103 EXPECT_TRUE(last_buffer[half_period + 2] > last_buffer[half_period + 3]); 56 EXPECT_GT(last_audio_bus->channel(0)[half_period + 1],
57 last_audio_bus->channel(0)[half_period + 2]);
58 EXPECT_GT(last_audio_bus->channel(0)[half_period + 2],
59 last_audio_bus->channel(0)[half_period + 3]);
60
104 oas->Close(); 61 oas->Close();
105 } 62 }
106 63
64 TEST(SimpleSources, SineWaveAudioCapped) {
65 SineWaveAudioSource source(1, 200, AudioParameters::kTelephoneSampleRate);
66
67 static const int kSampleCap = 100;
68 source.CapSamples(kSampleCap);
69
70 scoped_ptr<AudioBus> audio_bus = AudioBus::Create(1, 2 * kSampleCap);
71 EXPECT_EQ(source.OnMoreData(
72 audio_bus.get(), AudioBuffersState()), kSampleCap);
73 EXPECT_EQ(source.OnMoreData(audio_bus.get(), AudioBuffersState()), 0);
74 source.Reset();
75 EXPECT_EQ(source.OnMoreData(
76 audio_bus.get(), AudioBuffersState()), kSampleCap);
77 }
78
107 } // namespace media 79 } // namespace media
OLDNEW
« no previous file with comments | « media/audio/simple_sources.cc ('k') | media/audio/win/audio_low_latency_output_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698