Home | History | Annotate | Download | only in test
      1 /******************************************************************************
      2  *
      3  *  Copyright 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 {
     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_16000:
     40       return 16000;
     41     case BTAV_A2DP_CODEC_SAMPLE_RATE_24000:
     42       return 24000;
     43     case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE:
     44       break;
     45   }
     46   return 0;
     47 }
     48 
     49 static uint32_t codec_bits_per_sample2value(
     50     btav_a2dp_codec_bits_per_sample_t codec_bits_per_sample) {
     51   switch (codec_bits_per_sample) {
     52     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
     53       return 16;
     54     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
     55       return 24;
     56     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
     57       return 32;
     58     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
     59       break;
     60   }
     61   return 0;
     62 }
     63 
     64 static uint32_t codec_channel_mode2value(
     65     btav_a2dp_codec_channel_mode_t codec_channel_mode) {
     66   switch (codec_channel_mode) {
     67     case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO:
     68       return 1;
     69     case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO:
     70       return 2;
     71     case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE:
     72       break;
     73   }
     74   return 0;
     75 }
     76 
     77 }  // namespace
     78 
     79 class AudioA2dpHwTest : public ::testing::Test {
     80  protected:
     81   AudioA2dpHwTest() {}
     82 
     83  private:
     84 };
     85 
     86 TEST_F(AudioA2dpHwTest, test_compute_buffer_size) {
     87   const btav_a2dp_codec_sample_rate_t codec_sample_rate_array[] = {
     88       BTAV_A2DP_CODEC_SAMPLE_RATE_NONE,  BTAV_A2DP_CODEC_SAMPLE_RATE_44100,
     89       BTAV_A2DP_CODEC_SAMPLE_RATE_48000, BTAV_A2DP_CODEC_SAMPLE_RATE_88200,
     90       BTAV_A2DP_CODEC_SAMPLE_RATE_96000, BTAV_A2DP_CODEC_SAMPLE_RATE_176400,
     91       BTAV_A2DP_CODEC_SAMPLE_RATE_192000};
     92 
     93   const btav_a2dp_codec_bits_per_sample_t codec_bits_per_sample_array[] = {
     94       BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE, BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16,
     95       BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24, BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32};
     96 
     97   const btav_a2dp_codec_channel_mode_t codec_channel_mode_array[] = {
     98       BTAV_A2DP_CODEC_CHANNEL_MODE_NONE, BTAV_A2DP_CODEC_CHANNEL_MODE_MONO,
     99       BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO};
    100 
    101   for (const auto codec_sample_rate : codec_sample_rate_array) {
    102     for (const auto codec_bits_per_sample : codec_bits_per_sample_array) {
    103       for (const auto codec_channel_mode : codec_channel_mode_array) {
    104         size_t buffer_size = audio_a2dp_hw_stream_compute_buffer_size(
    105             codec_sample_rate, codec_bits_per_sample, codec_channel_mode);
    106 
    107         // Check for invalid input
    108         if ((codec_sample_rate == BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) ||
    109             (codec_bits_per_sample == BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE) ||
    110             (codec_channel_mode == BTAV_A2DP_CODEC_CHANNEL_MODE_NONE)) {
    111           EXPECT_EQ(buffer_size,
    112                     static_cast<size_t>(AUDIO_STREAM_OUTPUT_BUFFER_SZ));
    113           continue;
    114         }
    115 
    116         uint32_t sample_rate = codec_sample_rate2value(codec_sample_rate);
    117         EXPECT_TRUE(sample_rate != 0);
    118 
    119         uint32_t bits_per_sample =
    120             codec_bits_per_sample2value(codec_bits_per_sample);
    121         EXPECT_TRUE(bits_per_sample != 0);
    122 
    123         uint32_t number_of_channels =
    124             codec_channel_mode2value(codec_channel_mode);
    125         EXPECT_TRUE(number_of_channels != 0);
    126 
    127         const uint64_t time_period_ms = 20;  // TODO: Must be a parameter
    128         size_t expected_buffer_size =
    129             (time_period_ms * AUDIO_STREAM_OUTPUT_BUFFER_PERIODS * sample_rate *
    130              number_of_channels * (bits_per_sample / 8)) /
    131             1000;
    132 
    133         // Compute the divisor and adjust the buffer size
    134         const size_t divisor = (AUDIO_STREAM_OUTPUT_BUFFER_PERIODS * 16 *
    135                                 number_of_channels * bits_per_sample) /
    136                                8;
    137         const size_t remainder = expected_buffer_size % divisor;
    138         if (remainder != 0) {
    139           expected_buffer_size += divisor - remainder;
    140         }
    141 
    142         EXPECT_EQ(buffer_size, expected_buffer_size);
    143       }
    144     }
    145   }
    146 }
    147