Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2ee3ac8c authored by William Escande's avatar William Escande
Browse files

Add regression test for aptx

Test: atest --host libaptx_enc_tests
Test: atest --host libaptxhd_enc_tests
Bug: 226572369
Change-Id: Ib717faa54ca87ecec46c33a3333f52f87d9f40c3
parent f74a2fe8
Loading
Loading
Loading
Loading

TEST_MAPPING

100755 → 100644
+6 −0
Original line number Diff line number Diff line
@@ -62,6 +62,12 @@
    {
      "name" : "net_test_btif_hf_client_service"
    },
    {
      "name" : "libaptx_enc_tests"
    },
    {
      "name" : "libaptxhd_enc_tests"
    },
    {
      "name" : "net_test_stack_btm"
    }
+1 −1
Original line number Diff line number Diff line
@@ -51,7 +51,7 @@ APTXHDBTENCEXPORT const char* aptxhdbtenc_version(void);
APTXHDBTENCEXPORT int aptxhdbtenc_init(void* _state, short endian);

/* StereoEncode will take 8 audio samples (24-bit per sample)
 * and generate one 24-bit codeword with autosync inserted.
 * and generate two 24-bit codeword with autosync inserted.
 * The bitstream is compatible with be BC05 implementation. */
APTXHDBTENCEXPORT int aptxhdbtenc_encodestereo(void* _state, void* _pcmL,
                                               void* _pcmR, void* _buffer);
+35 −0
Original line number Diff line number Diff line
cc_test {
    name: "libaptx_enc_tests",
    defaults: [
        "mts_defaults",
    ],
    test_suites: ["device-tests"],
    host_supported: true,
    test_options: {
        unit_test: true,
    },
    srcs: [ "src/aptx.cc" ],
    whole_static_libs: [ "libaptx_enc" ],
    sanitize: {
        address: true,
        cfi: true,
    },
}

cc_test {
    name: "libaptxhd_enc_tests",
    defaults: [
        "mts_defaults",
    ],
    test_suites: ["device-tests"],
    host_supported: true,
    test_options: {
        unit_test: true,
    },
    srcs: [ "src/aptxhd.cc" ],
    whole_static_libs: [ "libaptxhd_enc" ],
    sanitize: {
        address: true,
        cfi: true,
    },
}
+79 −0
Original line number Diff line number Diff line
/*
 * Copyright 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <fcntl.h>
#include <gtest/gtest.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <fstream>
#include <iostream>

#include "aptXbtenc.h"

#define BYTES_PER_CODEWORD 16

class LibAptxEncTest : public ::testing::Test {
 private:
  void* aptxbtenc = nullptr;

 protected:
  void SetUp() override {
    aptxbtenc = malloc(SizeofAptxbtenc());
    ASSERT_NE(aptxbtenc, nullptr);
    ASSERT_EQ(aptxbtenc_init(aptxbtenc, 0), 0);
  }

  void TearDown() override { free(aptxbtenc); }

  void codeword_cmp(const uint16_t pcm[8], const uint32_t codeword) {
    uint32_t pcmL[4];
    uint32_t pcmR[4];
    for (size_t i = 0; i < 4; i++) {
      pcmL[i] = pcm[0];
      pcmR[i] = pcm[1];
      pcm += 2;
    }
    uint32_t encoded_sample;
    aptxbtenc_encodestereo(aptxbtenc, &pcmL, &pcmR, &encoded_sample);
    ASSERT_EQ(encoded_sample, codeword);
  }
};

TEST_F(LibAptxEncTest, encoder_size) { ASSERT_EQ(SizeofAptxbtenc(), 5008); }

TEST_F(LibAptxEncTest, encode_fake_data) {
  const char input[] =
      "012345678901234567890123456789012345678901234567890123456789012345678901"
      "23456789";
  const uint32_t aptx_codeword[] = {1270827967, 134154239, 670640127,
                                    1280265295, 2485752873};

  ASSERT_EQ((sizeof(input) - 1) % BYTES_PER_CODEWORD, 0);
  ASSERT_EQ((sizeof(input) - 1) / BYTES_PER_CODEWORD,
            sizeof(aptx_codeword) / sizeof(uint32_t));

  size_t idx = 0;

  uint16_t pcm[8];

  while (idx * BYTES_PER_CODEWORD < sizeof(input) - 1) {
    memcpy(pcm, input + idx * BYTES_PER_CODEWORD, BYTES_PER_CODEWORD);
    codeword_cmp(pcm, aptx_codeword[idx]);
    ++idx;
  }
}
+82 −0
Original line number Diff line number Diff line
/*
 * Copyright 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <fcntl.h>
#include <gtest/gtest.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <fstream>
#include <iostream>

#include "aptXHDbtenc.h"

#define BYTES_PER_CODEWORD 24

class LibAptxHdEncTest : public ::testing::Test {
 private:
 protected:
  void* aptxhdbtenc = nullptr;
  void SetUp() override {
    aptxhdbtenc = malloc(SizeofAptxhdbtenc());
    ASSERT_NE(aptxhdbtenc, nullptr);
    ASSERT_EQ(aptxhdbtenc_init(aptxhdbtenc, 0), 0);
  }

  void TearDown() override { free(aptxhdbtenc); }

  void codeword_cmp(const uint8_t p[BYTES_PER_CODEWORD],
                    const uint32_t codeword[2]) {
    uint32_t pcmL[4];
    uint32_t pcmR[4];
    for (size_t i = 0; i < 4; i++) {
      pcmL[i] = ((p[0] << 0) | (p[1] << 8) | (((int8_t)p[2]) << 16));
      p += 3;
      pcmR[i] = ((p[0] << 0) | (p[1] << 8) | (((int8_t)p[2]) << 16));
      p += 3;
    }
    uint32_t encoded_sample[2];
    aptxhdbtenc_encodestereo(aptxhdbtenc, &pcmL, &pcmR, (void*)encoded_sample);

    ASSERT_EQ(encoded_sample[0], codeword[0]);
    ASSERT_EQ(encoded_sample[1], codeword[1]);
  }
};

TEST_F(LibAptxHdEncTest, encoder_size) { ASSERT_EQ(SizeofAptxhdbtenc(), 5256); }

TEST_F(LibAptxHdEncTest, encode_fake_data) {
  const char input[] =
      "012345678901234567890123456789012345678901234567890123456789012345678901"
      "234567890123456789012345678901234567890123456789";
  const uint32_t aptxhd_codeword[] = {7585535, 7585535, 32767,   32767,
                                      557055,  557027,  7586105, 7586109,
                                      9748656, 10764446};

  ASSERT_EQ((sizeof(input) - 1) % BYTES_PER_CODEWORD, 0);
  ASSERT_EQ((sizeof(input) - 1) / BYTES_PER_CODEWORD,
            sizeof(aptxhd_codeword) / sizeof(uint32_t) / 2);

  size_t idx = 0;

  uint8_t pcm[BYTES_PER_CODEWORD];
  while (idx * BYTES_PER_CODEWORD < sizeof(input) - 1) {
    memcpy(pcm, input + idx * BYTES_PER_CODEWORD, BYTES_PER_CODEWORD);
    codeword_cmp(pcm, aptxhd_codeword + idx * 2);
    ++idx;
  }
}
Loading