1 /******************************************************************************
2  *
3  *  Copyright (C) 2017 The Android Open Source Project
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #include <gtest/gtest.h>
20 
21 #include "audio_a2dp_hw/include/audio_a2dp_hw.h"
22 
23 namespace {
codec_sample_rate2value(btav_a2dp_codec_sample_rate_t codec_sample_rate)24 static uint32_t codec_sample_rate2value(
25     btav_a2dp_codec_sample_rate_t codec_sample_rate) {
26   switch (codec_sample_rate) {
27     case BTAV_A2DP_CODEC_SAMPLE_RATE_44100:
28       return 44100;
29     case BTAV_A2DP_CODEC_SAMPLE_RATE_48000:
30       return 48000;
31     case BTAV_A2DP_CODEC_SAMPLE_RATE_88200:
32       return 88200;
33     case BTAV_A2DP_CODEC_SAMPLE_RATE_96000:
34       return 96000;
35     case BTAV_A2DP_CODEC_SAMPLE_RATE_176400:
36       return 176400;
37     case BTAV_A2DP_CODEC_SAMPLE_RATE_192000:
38       return 192000;
39     case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE:
40       break;
41   }
42   return 0;
43 }
44 
codec_bits_per_sample2value(btav_a2dp_codec_bits_per_sample_t codec_bits_per_sample)45 static uint32_t codec_bits_per_sample2value(
46     btav_a2dp_codec_bits_per_sample_t codec_bits_per_sample) {
47   switch (codec_bits_per_sample) {
48     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
49       return 16;
50     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
51       return 24;
52     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
53       return 32;
54     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
55       break;
56   }
57   return 0;
58 }
59 
codec_channel_mode2value(btav_a2dp_codec_channel_mode_t codec_channel_mode)60 static uint32_t codec_channel_mode2value(
61     btav_a2dp_codec_channel_mode_t codec_channel_mode) {
62   switch (codec_channel_mode) {
63     case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO:
64       return 1;
65     case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO:
66       return 2;
67     case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE:
68       break;
69   }
70   return 0;
71 }
72 
73 }  // namespace
74 
75 class AudioA2dpHwTest : public ::testing::Test {
76  protected:
AudioA2dpHwTest()77   AudioA2dpHwTest() {}
78 
79  private:
80 };
81 
TEST_F(AudioA2dpHwTest,test_compute_buffer_size)82 TEST_F(AudioA2dpHwTest, test_compute_buffer_size) {
83   const btav_a2dp_codec_sample_rate_t codec_sample_rate_array[] = {
84       BTAV_A2DP_CODEC_SAMPLE_RATE_NONE,  BTAV_A2DP_CODEC_SAMPLE_RATE_44100,
85       BTAV_A2DP_CODEC_SAMPLE_RATE_48000, BTAV_A2DP_CODEC_SAMPLE_RATE_88200,
86       BTAV_A2DP_CODEC_SAMPLE_RATE_96000, BTAV_A2DP_CODEC_SAMPLE_RATE_176400,
87       BTAV_A2DP_CODEC_SAMPLE_RATE_192000};
88 
89   const btav_a2dp_codec_bits_per_sample_t codec_bits_per_sample_array[] = {
90       BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE, BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16,
91       BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24, BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32};
92 
93   const btav_a2dp_codec_channel_mode_t codec_channel_mode_array[] = {
94       BTAV_A2DP_CODEC_CHANNEL_MODE_NONE, BTAV_A2DP_CODEC_CHANNEL_MODE_MONO,
95       BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO};
96 
97   for (const auto codec_sample_rate : codec_sample_rate_array) {
98     for (const auto codec_bits_per_sample : codec_bits_per_sample_array) {
99       for (const auto codec_channel_mode : codec_channel_mode_array) {
100         size_t buffer_size = audio_a2dp_hw_stream_compute_buffer_size(
101             codec_sample_rate, codec_bits_per_sample, codec_channel_mode);
102 
103         // Check for invalid input
104         if ((codec_sample_rate == BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) ||
105             (codec_bits_per_sample == BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE) ||
106             (codec_channel_mode == BTAV_A2DP_CODEC_CHANNEL_MODE_NONE)) {
107           EXPECT_EQ(buffer_size,
108                     static_cast<size_t>(AUDIO_STREAM_OUTPUT_BUFFER_SZ));
109           continue;
110         }
111 
112         uint32_t sample_rate = codec_sample_rate2value(codec_sample_rate);
113         EXPECT_TRUE(sample_rate != 0);
114 
115         uint32_t bits_per_sample =
116             codec_bits_per_sample2value(codec_bits_per_sample);
117         EXPECT_TRUE(bits_per_sample != 0);
118 
119         uint32_t number_of_channels =
120             codec_channel_mode2value(codec_channel_mode);
121         EXPECT_TRUE(number_of_channels != 0);
122 
123         const uint64_t time_period_ms = 20;  // TODO: Must be a parameter
124         size_t expected_buffer_size =
125             (time_period_ms * AUDIO_STREAM_OUTPUT_BUFFER_PERIODS * sample_rate *
126              number_of_channels * (bits_per_sample / 8)) /
127             1000;
128 
129         // Compute the divisor and adjust the buffer size
130         const size_t divisor = (AUDIO_STREAM_OUTPUT_BUFFER_PERIODS * 16 *
131                                 number_of_channels * bits_per_sample) /
132                                8;
133         const size_t remainder = expected_buffer_size % divisor;
134         if (remainder != 0) {
135           expected_buffer_size += divisor - remainder;
136         }
137 
138         EXPECT_EQ(buffer_size, expected_buffer_size);
139       }
140     }
141   }
142 }
143