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
3// found in the LICENSE file.
4
5#include "base/basictypes.h"
6#include "base/strings/string_number_conversions.h"
7#include "media/audio/audio_parameters.h"
8#include "testing/gtest/include/gtest/gtest.h"
9
10namespace media {
11
12TEST(AudioParameters, Constructor_Default) {
13  AudioParameters::Format expected_format = AudioParameters::AUDIO_PCM_LINEAR;
14  int expected_bits = 0;
15  int expected_channels = 0;
16  ChannelLayout expected_channel_layout = CHANNEL_LAYOUT_NONE;
17  int expected_rate = 0;
18  int expected_samples = 0;
19
20  AudioParameters params;
21
22  EXPECT_EQ(expected_format, params.format());
23  EXPECT_EQ(expected_bits, params.bits_per_sample());
24  EXPECT_EQ(expected_channels, params.channels());
25  EXPECT_EQ(expected_channel_layout, params.channel_layout());
26  EXPECT_EQ(expected_rate, params.sample_rate());
27  EXPECT_EQ(expected_samples, params.frames_per_buffer());
28}
29
30TEST(AudioParameters, Constructor_ParameterValues) {
31  AudioParameters::Format expected_format =
32      AudioParameters::AUDIO_PCM_LOW_LATENCY;
33  int expected_bits = 16;
34  int expected_channels = 6;
35  ChannelLayout expected_channel_layout = CHANNEL_LAYOUT_5_1;
36  int expected_rate = 44100;
37  int expected_samples = 880;
38
39  AudioParameters params(expected_format, expected_channel_layout,
40                         expected_rate, expected_bits, expected_samples);
41
42  EXPECT_EQ(expected_format, params.format());
43  EXPECT_EQ(expected_bits, params.bits_per_sample());
44  EXPECT_EQ(expected_channels, params.channels());
45  EXPECT_EQ(expected_channel_layout, params.channel_layout());
46  EXPECT_EQ(expected_rate, params.sample_rate());
47  EXPECT_EQ(expected_samples, params.frames_per_buffer());
48}
49
50TEST(AudioParameters, GetBytesPerBuffer) {
51  EXPECT_EQ(100, AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
52                                 CHANNEL_LAYOUT_MONO, 1000,  8, 100)
53                                 .GetBytesPerBuffer());
54  EXPECT_EQ(200, AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
55                                 CHANNEL_LAYOUT_MONO, 1000,  16, 100)
56                                 .GetBytesPerBuffer());
57  EXPECT_EQ(200, AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
58                                 CHANNEL_LAYOUT_STEREO, 1000,  8, 100)
59                                 .GetBytesPerBuffer());
60  EXPECT_EQ(200, AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
61                                 CHANNEL_LAYOUT_MONO, 1000,  8, 200)
62                                 .GetBytesPerBuffer());
63  EXPECT_EQ(800, AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
64                                 CHANNEL_LAYOUT_STEREO, 1000,  16, 200)
65                                 .GetBytesPerBuffer());
66  EXPECT_EQ(300, AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
67                                 CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC,
68                                 1000, 8, 100)
69                                 .GetBytesPerBuffer());
70}
71
72TEST(AudioParameters, GetBytesPerSecond) {
73  EXPECT_EQ(0, AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
74                               CHANNEL_LAYOUT_NONE, 0, 0, 0)
75                               .GetBytesPerSecond());
76  EXPECT_EQ(0, AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
77                               CHANNEL_LAYOUT_STEREO, 0, 0, 0)
78                               .GetBytesPerSecond());
79  EXPECT_EQ(0, AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
80                               CHANNEL_LAYOUT_NONE, 100, 0, 0)
81                               .GetBytesPerSecond());
82  EXPECT_EQ(0, AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
83                               CHANNEL_LAYOUT_NONE, 0, 8, 0)
84                               .GetBytesPerSecond());
85  EXPECT_EQ(200, AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
86                                 CHANNEL_LAYOUT_STEREO, 100, 8, 0)
87                                 .GetBytesPerSecond());
88}
89
90TEST(AudioParameters, Compare) {
91  AudioParameters values[] = {
92    AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO,
93                    1000,  8, 100),
94    AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO,
95                    1000,  8, 200),
96    AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO,
97                    1000, 16, 100),
98    AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO,
99                    1000, 16, 200),
100    AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO,
101                    2000,  8, 100),
102    AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO,
103                    2000,  8, 200),
104    AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO,
105                    2000, 16, 100),
106    AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO,
107                    2000, 16, 200),
108
109    AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO,
110                    1000,  8, 100),
111    AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO,
112                    1000,  8, 200),
113    AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO,
114                    1000, 16, 100),
115    AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO,
116                    1000, 16, 200),
117    AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO,
118                    2000,  8, 100),
119    AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO,
120                    2000,  8, 200),
121    AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO,
122                    2000, 16, 100),
123    AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO,
124                    2000, 16, 200),
125
126    AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
127                    CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC, 1000,  8, 100),
128    AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
129                    CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC, 1000,  8, 200),
130    AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
131                    CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC, 1000, 16, 100),
132    AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
133                    CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC, 1000, 16, 200),
134    AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
135                    CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC, 2000,  8, 100),
136    AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
137                    CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC, 2000,  8, 200),
138    AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
139                    CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC, 2000, 16, 100),
140    AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
141                    CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC, 2000, 16, 200),
142
143    AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_MONO,
144                    1000,  8, 100),
145    AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_MONO,
146                    1000,  8, 200),
147    AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_MONO,
148                    1000, 16, 100),
149    AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_MONO,
150                    1000, 16, 200),
151    AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_MONO,
152                    2000,  8, 100),
153    AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_MONO,
154                    2000,  8, 200),
155    AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_MONO,
156                    2000, 16, 100),
157    AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_MONO,
158                    2000, 16, 200),
159
160    AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
161                    CHANNEL_LAYOUT_STEREO, 1000,  8, 100),
162    AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
163                    CHANNEL_LAYOUT_STEREO, 1000,  8, 200),
164    AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
165                    CHANNEL_LAYOUT_STEREO, 1000, 16, 100),
166    AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
167                    CHANNEL_LAYOUT_STEREO, 1000, 16, 200),
168    AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
169                    CHANNEL_LAYOUT_STEREO, 2000,  8, 100),
170    AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
171                    CHANNEL_LAYOUT_STEREO, 2000,  8, 200),
172    AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
173                    CHANNEL_LAYOUT_STEREO, 2000, 16, 100),
174    AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
175                    CHANNEL_LAYOUT_STEREO, 2000, 16, 200),
176
177    AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
178                    CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC, 1000,  8, 100),
179    AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
180                    CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC, 1000,  8, 200),
181    AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
182                    CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC, 1000, 16, 100),
183    AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
184                    CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC, 1000, 16, 200),
185    AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
186                    CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC, 2000,  8, 100),
187    AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
188                    CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC, 2000,  8, 200),
189    AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
190                    CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC, 2000, 16, 100),
191    AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
192                    CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC, 2000, 16, 200),
193  };
194
195  for (size_t i = 0; i < arraysize(values); ++i) {
196    for (size_t j = 0; j < arraysize(values); ++j) {
197      SCOPED_TRACE("i=" + base::IntToString(i) + " j=" + base::IntToString(j));
198      EXPECT_EQ(i < j, values[i] < values[j]);
199    }
200
201    // Verify that a value is never less than itself.
202    EXPECT_FALSE(values[i] < values[i]);
203  }
204}
205
206}  // namespace media
207