summaryrefslogtreecommitdiffstats
path: root/media/audio/simple_sources_unittest.cc
blob: 74d19f23fae60000526245b0f8fb8433006fe504 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <algorithm>  // std::min

#include "base/logging.h"
#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "base/time.h"
#include "media/audio/audio_manager.h"
#include "media/audio/fake_audio_output_stream.h"
#include "media/audio/simple_sources.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace media {

static void GenerateRandomData(char* buffer, uint32 len) {
  static bool called = false;
  if (!called) {
    called = true;
    int seed = static_cast<int>(base::Time::Now().ToInternalValue());
    srand(seed);
    VLOG(1) << "Random seed: " << seed;
  }

  for (uint32 i = 0; i < len; i++)
    buffer[i] = static_cast<char>(rand());  // NOLINT
}

// To test write size smaller than read size.
TEST(SimpleSourcesTest, PushSourceSmallerWrite) {
  const uint32 kDataSize = 40960;
  scoped_array<char> data(new char[kDataSize]);
  GenerateRandomData(data.get(), kDataSize);

  // Choose two prime numbers for read and write sizes.
  const uint32 kWriteSize = 283;
  const uint32 kReadSize = 293;
  scoped_array<uint8> read_data(new uint8[kReadSize]);

  // Create a PushSource.
  PushSource push_source;
  EXPECT_EQ(0u, push_source.UnProcessedBytes());

  // Write everything into this push source.
  for (uint32 i = 0; i < kDataSize; i += kWriteSize) {
    uint32 size = std::min(kDataSize - i, kWriteSize);
    EXPECT_TRUE(push_source.Write(data.get() + i, size));
  }
  EXPECT_EQ(kDataSize, push_source.UnProcessedBytes());

  // Read everything from the push source.
  for (uint32 i = 0; i < kDataSize; i += kReadSize) {
    uint32 size = std::min(kDataSize - i , kReadSize);
    EXPECT_EQ(size, push_source.OnMoreData(read_data.get(), size,
                                           AudioBuffersState()));
    EXPECT_EQ(0, memcmp(data.get() + i, read_data.get(), size));
  }
  EXPECT_EQ(0u, push_source.UnProcessedBytes());
}

// Validate that the SineWaveAudioSource writes the expected values for
// the FORMAT_16BIT_MONO. The values are carefully selected so rounding issues
// do not affect the result. We also test that AudioManager::GetLastMockBuffer
// works.
TEST(SimpleSources, SineWaveAudio16MonoTest) {
  const uint32 samples = 1024;
  const uint32 bytes_per_sample = 2;
  const int freq = 200;

  SineWaveAudioSource source(SineWaveAudioSource::FORMAT_16BIT_LINEAR_PCM, 1,
                             freq, AudioParameters::kTelephoneSampleRate);

  scoped_ptr<AudioManager> audio_man(AudioManager::Create());
  AudioParameters params(
      AudioParameters::AUDIO_MOCK, CHANNEL_LAYOUT_MONO,
      AudioParameters::kTelephoneSampleRate, bytes_per_sample * 8, samples);
  AudioOutputStream* oas = audio_man->MakeAudioOutputStream(params);
  ASSERT_TRUE(NULL != oas);
  EXPECT_TRUE(oas->Open());

  oas->Start(&source);
  oas->Stop();

  ASSERT_TRUE(FakeAudioOutputStream::GetCurrentFakeStream());
  const int16* last_buffer =
      reinterpret_cast<int16*>(
          FakeAudioOutputStream::GetCurrentFakeStream()->buffer());
  ASSERT_TRUE(NULL != last_buffer);

  uint32 half_period = AudioParameters::kTelephoneSampleRate / (freq * 2);

  // Spot test positive incursion of sine wave.
  EXPECT_EQ(0, last_buffer[0]);
  EXPECT_EQ(5126, last_buffer[1]);
  EXPECT_TRUE(last_buffer[1] < last_buffer[2]);
  EXPECT_TRUE(last_buffer[2] < last_buffer[3]);
  // Spot test negative incursion of sine wave.
  EXPECT_EQ(0, last_buffer[half_period]);
  EXPECT_EQ(-5126, last_buffer[half_period + 1]);
  EXPECT_TRUE(last_buffer[half_period + 1] > last_buffer[half_period + 2]);
  EXPECT_TRUE(last_buffer[half_period + 2] > last_buffer[half_period + 3]);
  oas->Close();
}

}  // namespace media