Skip to content
Snippets Groups Projects
Commit b535902d authored by Sandeep Samdaria's avatar Sandeep Samdaria
Browse files

Unit test for btif avrcp audio track

New unit-tests are added to validate gain is set appropriately for the
audio track buffer. It also validates the transcoding of the audio
buffer does use the appropriate gain value.

Bug: 278602076
Tag: #stability
Test: atest net_test_btif_avrcp_audio_track --iterations 20
Change-Id: I9656d77d554ebef4b189599ca9ad5e8ee9746f53
parent 9ae6b903
No related branches found
No related tags found
No related merge requests found
...@@ -45,6 +45,9 @@ ...@@ -45,6 +45,9 @@
{ {
"name": "net_test_btif_profile_queue" "name": "net_test_btif_profile_queue"
}, },
{
"name": "net_test_btif_avrcp_audio_track"
},
{ {
"name": "net_test_device" "name": "net_test_device"
}, },
...@@ -255,6 +258,9 @@ ...@@ -255,6 +258,9 @@
{ {
"name": "net_test_btif_profile_queue" "name": "net_test_btif_profile_queue"
}, },
{
"name": "net_test_btif_avrcp_audio_track"
},
{ {
"name": "net_test_device" "name": "net_test_device"
}, },
......
...@@ -335,6 +335,38 @@ cc_test { ...@@ -335,6 +335,38 @@ cc_test {
cflags: ["-DBUILDCFG"], cflags: ["-DBUILDCFG"],
} }
// btif avrcp audio track unit tests
cc_test {
name: "net_test_btif_avrcp_audio_track",
defaults: [
"bluetooth_gtest_x86_asan_workaround",
"fluoride_defaults",
"mts_defaults",
],
test_suites: ["device-tests"],
include_dirs: btifCommonIncludes + [
"frameworks/av/media/libaaudio/include",
],
srcs: [
":TestCommonMockFunctions",
":TestMockFrameworks",
"src/btif_avrcp_audio_track.cc",
"test/btif_avrcp_audio_track_test.cc",
],
header_libs: ["libbluetooth_headers"],
generated_headers: [
"BluetoothGeneratedDumpsysDataSchema_h",
"BluetoothGeneratedPackets_h",
],
static_libs: [
"libbluetooth-types",
"libchrome",
"libflatbuffers-cpp",
"libosi",
],
cflags: ["-DBUILDCFG"],
}
// btif rc unit tests for target // btif rc unit tests for target
cc_test { cc_test {
name: "net_test_btif_rc", name: "net_test_btif_rc",
......
#include "btif/include/btif_avrcp_audio_track.h"
#include <aaudio/AAudio.h>
#include <aaudio/AAudioTesting.h>
#include <gtest/gtest.h>
#include <memory>
// Define the incomplete audio stream struct type.
struct AAudioStreamStruct {
// The ID of the stream.
int32_t streamId;
};
// Expected audio track.
typedef struct {
AAudioStream* stream;
int bitsPerSample;
int channelCount;
float* buffer;
size_t bufferLength;
float gain;
} BtifAvrcpAudioTrack;
class BtifAvrcpAudioTrackTest : public ::testing::Test {};
TEST_F(BtifAvrcpAudioTrackTest, setAudioTrackGain_maxGainSet) {
void* track_handle = BtifAvrcpAudioTrackCreate(10, 16, 3);
BtifAvrcpSetAudioTrackGain(track_handle, 1.0f);
BtifAvrcpAudioTrack* trackHolder =
static_cast<BtifAvrcpAudioTrack*>(track_handle);
EXPECT_EQ(trackHolder->gain, 1.0f);
BtifAvrcpAudioTrackDelete(track_handle);
}
TEST_F(BtifAvrcpAudioTrackTest, setAudioTrackGain_minimumGainSet) {
void* track_handle = BtifAvrcpAudioTrackCreate(10, 16, 3);
BtifAvrcpSetAudioTrackGain(track_handle, 0.0f);
BtifAvrcpAudioTrack* trackHolder =
static_cast<BtifAvrcpAudioTrack*>(track_handle);
EXPECT_EQ(trackHolder->gain, 0.0f);
BtifAvrcpAudioTrackDelete(track_handle);
}
TEST_F(BtifAvrcpAudioTrackTest,
setAudioTrackGain_maxGainOutOfBounds_setsCappedGain) {
void* track_handle = BtifAvrcpAudioTrackCreate(10, 16, 3);
BtifAvrcpAudioTrack* trackHolder =
static_cast<BtifAvrcpAudioTrack*>(track_handle);
BtifAvrcpSetAudioTrackGain(track_handle, 2.0f);
EXPECT_EQ(trackHolder->gain, 1.0f);
BtifAvrcpAudioTrackDelete(track_handle);
}
TEST_F(BtifAvrcpAudioTrackTest,
setAudioTrackGain_minGainOutOfBounds_setsCappedGain) {
void* track_handle = BtifAvrcpAudioTrackCreate(10, 16, 3);
BtifAvrcpAudioTrack* trackHolder =
static_cast<BtifAvrcpAudioTrack*>(track_handle);
BtifAvrcpSetAudioTrackGain(track_handle, -2.0f);
EXPECT_EQ(trackHolder->gain, 0.0f);
BtifAvrcpAudioTrackDelete(track_handle);
}
TEST_F(BtifAvrcpAudioTrackTest,
setMaxAudioTrackGain_minGain_bufferStreamDucked) {
constexpr float scaleQ15ToFloat = 1.0f / 32768.0f;
constexpr size_t bufferLength = 100;
constexpr int bitsPerSample = 16;
constexpr size_t sampleSize = bitsPerSample / 8;
constexpr auto gainValue = 0.5f;
void* track_handle = BtifAvrcpAudioTrackCreate(10, bitsPerSample, 3);
BtifAvrcpAudioTrack* trackHolder =
static_cast<BtifAvrcpAudioTrack*>(track_handle);
std::unique_ptr<AAudioStream> stream(new AAudioStream);
// Set the values to track holder as mock audio lib APIs are a no-op.
trackHolder->stream = stream.get();
trackHolder->bufferLength = bufferLength;
trackHolder->buffer = new float[trackHolder->bufferLength]();
BtifAvrcpSetAudioTrackGain(trackHolder, gainValue);
// Create a fake buffer.
uint8_t data[bufferLength];
for (size_t index = 0; index < bufferLength; ++index) {
data[index] = index;
}
BtifAvrcpAudioTrackWriteData(trackHolder, data, bufferLength - 1);
const int16_t* dataInt = (int16_t*)data;
for (size_t index = 0; index < bufferLength / sampleSize; ++index) {
const float expected = dataInt[index] * scaleQ15ToFloat * gainValue;
EXPECT_NEAR(expected, trackHolder->buffer[index], 0.01f);
}
BtifAvrcpAudioTrackDelete(trackHolder);
}
...@@ -13,6 +13,7 @@ known_tests=( ...@@ -13,6 +13,7 @@ known_tests=(
net_test_bta_security net_test_bta_security
net_test_btif net_test_btif
net_test_btif_profile_queue net_test_btif_profile_queue
net_test_btif_avrcp_audio_track
net_test_btif_config_cache net_test_btif_config_cache
net_test_device net_test_device
net_test_device_iot_config net_test_device_iot_config
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment