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 <stddef.h>
6 
7 #include "base/stl_util.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "media/base/audio_parameters.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 
12 namespace media {
13 
TEST(AudioParameters,Constructor_Default)14 TEST(AudioParameters, Constructor_Default) {
15   AudioParameters::Format expected_format = AudioParameters::AUDIO_PCM_LINEAR;
16   int expected_channels = 0;
17   ChannelLayout expected_channel_layout = CHANNEL_LAYOUT_NONE;
18   int expected_rate = 0;
19   int expected_samples = 0;
20   AudioParameters::PlatformEffectsMask expected_effects =
21       AudioParameters::NO_EFFECTS;
22   std::vector<Point> expected_mic_positions;
23 
24   AudioParameters params;
25 
26   EXPECT_EQ(expected_format, params.format());
27   EXPECT_EQ(expected_channels, params.channels());
28   EXPECT_EQ(expected_channel_layout, params.channel_layout());
29   EXPECT_EQ(expected_rate, params.sample_rate());
30   EXPECT_EQ(expected_samples, params.frames_per_buffer());
31   EXPECT_EQ(expected_effects, params.effects());
32   EXPECT_EQ(expected_mic_positions, params.mic_positions());
33 }
34 
TEST(AudioParameters,Constructor_ParameterValues)35 TEST(AudioParameters, Constructor_ParameterValues) {
36   AudioParameters::Format expected_format =
37       AudioParameters::AUDIO_PCM_LOW_LATENCY;
38   int expected_channels = 6;
39   ChannelLayout expected_channel_layout = CHANNEL_LAYOUT_5_1;
40   int expected_rate = 44100;
41   int expected_samples = 880;
42 
43   AudioParameters params(expected_format, expected_channel_layout,
44                          expected_rate, expected_samples);
45 
46   EXPECT_EQ(expected_format, params.format());
47   EXPECT_EQ(expected_channels, params.channels());
48   EXPECT_EQ(expected_channel_layout, params.channel_layout());
49   EXPECT_EQ(expected_rate, params.sample_rate());
50   EXPECT_EQ(expected_samples, params.frames_per_buffer());
51 }
52 
TEST(AudioParameters,GetBytesPerBuffer)53 TEST(AudioParameters, GetBytesPerBuffer) {
54   EXPECT_EQ(100, AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
55                                  CHANNEL_LAYOUT_MONO, 1000, 100)
56                      .GetBytesPerBuffer(kSampleFormatU8));
57   EXPECT_EQ(200, AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
58                                  CHANNEL_LAYOUT_MONO, 1000, 100)
59                      .GetBytesPerBuffer(kSampleFormatS16));
60   EXPECT_EQ(200, AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
61                                  CHANNEL_LAYOUT_STEREO, 1000, 100)
62                      .GetBytesPerBuffer(kSampleFormatU8));
63   EXPECT_EQ(200, AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
64                                  CHANNEL_LAYOUT_MONO, 1000, 200)
65                      .GetBytesPerBuffer(kSampleFormatU8));
66   EXPECT_EQ(800, AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
67                                  CHANNEL_LAYOUT_STEREO, 1000, 200)
68                      .GetBytesPerBuffer(kSampleFormatS16));
69   EXPECT_EQ(300,
70             AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
71                             CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC, 1000, 100)
72                 .GetBytesPerBuffer(kSampleFormatU8));
73 }
74 
TEST(AudioParameters,Compare)75 TEST(AudioParameters, Compare) {
76   AudioParameters values[] = {
77       AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO,
78                       1000, 100),
79       AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO,
80                       1000, 200),
81       AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO,
82                       2000, 100),
83       AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO,
84                       2000, 200),
85 
86       AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO,
87                       1000, 100),
88       AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO,
89                       1000, 200),
90       AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO,
91                       2000, 100),
92       AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO,
93                       2000, 200),
94 
95       AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
96                       CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC, 1000, 100),
97       AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
98                       CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC, 1000, 200),
99       AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
100                       CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC, 2000, 100),
101       AudioParameters(AudioParameters::AUDIO_PCM_LINEAR,
102                       CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC, 2000, 200),
103 
104       AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
105                       CHANNEL_LAYOUT_MONO, 1000, 100),
106       AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
107                       CHANNEL_LAYOUT_MONO, 1000, 200),
108       AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
109                       CHANNEL_LAYOUT_MONO, 2000, 100),
110       AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
111                       CHANNEL_LAYOUT_MONO, 2000, 200),
112 
113       AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
114                       CHANNEL_LAYOUT_STEREO, 1000, 100),
115       AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
116                       CHANNEL_LAYOUT_STEREO, 1000, 200),
117       AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
118                       CHANNEL_LAYOUT_STEREO, 2000, 100),
119       AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
120                       CHANNEL_LAYOUT_STEREO, 2000, 200),
121 
122       AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
123                       CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC, 1000, 100),
124       AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
125                       CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC, 1000, 200),
126       AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
127                       CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC, 2000, 100),
128       AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
129                       CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC, 2000, 200),
130   };
131 
132   for (size_t i = 0; i < base::size(values); ++i) {
133     for (size_t j = 0; j < base::size(values); ++j) {
134       SCOPED_TRACE("i=" + base::NumberToString(i) +
135                    " j=" + base::NumberToString(j));
136       EXPECT_EQ(i < j, values[i] < values[j]);
137     }
138 
139     // Verify that a value is never less than itself.
140     EXPECT_FALSE(values[i] < values[i]);
141   }
142 }
143 
TEST(AudioParameters,Constructor_ValidChannelCounts)144 TEST(AudioParameters, Constructor_ValidChannelCounts) {
145   int expected_channels = 8;
146   ChannelLayout expected_layout = CHANNEL_LAYOUT_DISCRETE;
147 
148   AudioParameters params(AudioParameters::AUDIO_PCM_LOW_LATENCY,
149                          expected_layout, 44100, 880);
150   params.set_channels_for_discrete(expected_channels);
151   EXPECT_EQ(expected_channels, params.channels());
152   EXPECT_EQ(expected_layout, params.channel_layout());
153   EXPECT_TRUE(params.IsValid());
154 }
155 
156 }  // namespace media
157