1 /*
2 * Copyright 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "btif/include/btif_avrcp_audio_track.h"
18
19 #ifndef __INTRODUCED_IN
20 #define __INTRODUCED_IN(x)
21 #endif
22
23 #include <aaudio/AAudio.h>
24 #include <aaudio/AAudioTesting.h>
25 #include <gtest/gtest.h>
26
27 #include <memory>
28
29 // Define the incomplete audio stream struct type.
30 struct AAudioStreamStruct {
31 // The ID of the stream.
32 int32_t streamId;
33 };
34
35 // Expected audio track.
36 typedef struct {
37 AAudioStream* stream;
38 int bitsPerSample;
39 int channelCount;
40 float* buffer;
41 size_t bufferLength;
42 float gain;
43 } BtifAvrcpAudioTrack;
44
45 class BtifAvrcpAudioTrackTest : public ::testing::Test {};
46
TEST_F(BtifAvrcpAudioTrackTest,setAudioTrackGain_maxGainSet)47 TEST_F(BtifAvrcpAudioTrackTest, setAudioTrackGain_maxGainSet) {
48 void* track_handle = BtifAvrcpAudioTrackCreate(10, 16, 3);
49 BtifAvrcpSetAudioTrackGain(track_handle, 1.0f);
50 BtifAvrcpAudioTrack* trackHolder = static_cast<BtifAvrcpAudioTrack*>(track_handle);
51 EXPECT_EQ(trackHolder->gain, 1.0f);
52 BtifAvrcpAudioTrackDelete(track_handle);
53 }
54
TEST_F(BtifAvrcpAudioTrackTest,setAudioTrackGain_minimumGainSet)55 TEST_F(BtifAvrcpAudioTrackTest, setAudioTrackGain_minimumGainSet) {
56 void* track_handle = BtifAvrcpAudioTrackCreate(10, 16, 3);
57 BtifAvrcpSetAudioTrackGain(track_handle, 0.0f);
58 BtifAvrcpAudioTrack* trackHolder = static_cast<BtifAvrcpAudioTrack*>(track_handle);
59 EXPECT_EQ(trackHolder->gain, 0.0f);
60 BtifAvrcpAudioTrackDelete(track_handle);
61 }
62
TEST_F(BtifAvrcpAudioTrackTest,setAudioTrackGain_maxGainOutOfBounds_setsCappedGain)63 TEST_F(BtifAvrcpAudioTrackTest, setAudioTrackGain_maxGainOutOfBounds_setsCappedGain) {
64 void* track_handle = BtifAvrcpAudioTrackCreate(10, 16, 3);
65 BtifAvrcpAudioTrack* trackHolder = static_cast<BtifAvrcpAudioTrack*>(track_handle);
66 BtifAvrcpSetAudioTrackGain(track_handle, 2.0f);
67 EXPECT_EQ(trackHolder->gain, 1.0f);
68 BtifAvrcpAudioTrackDelete(track_handle);
69 }
70
TEST_F(BtifAvrcpAudioTrackTest,setAudioTrackGain_minGainOutOfBounds_setsCappedGain)71 TEST_F(BtifAvrcpAudioTrackTest, setAudioTrackGain_minGainOutOfBounds_setsCappedGain) {
72 void* track_handle = BtifAvrcpAudioTrackCreate(10, 16, 3);
73 BtifAvrcpAudioTrack* trackHolder = static_cast<BtifAvrcpAudioTrack*>(track_handle);
74 BtifAvrcpSetAudioTrackGain(track_handle, -2.0f);
75 EXPECT_EQ(trackHolder->gain, 0.0f);
76 BtifAvrcpAudioTrackDelete(track_handle);
77 }
78
TEST_F(BtifAvrcpAudioTrackTest,setMaxAudioTrackGain_minGain_bufferStreamDucked)79 TEST_F(BtifAvrcpAudioTrackTest, setMaxAudioTrackGain_minGain_bufferStreamDucked) {
80 constexpr float scaleQ15ToFloat = 1.0f / 32768.0f;
81 constexpr size_t bufferLength = 100;
82 constexpr int bitsPerSample = 16;
83 constexpr size_t sampleSize = bitsPerSample / 8;
84 constexpr auto gainValue = 0.5f;
85 void* track_handle = BtifAvrcpAudioTrackCreate(10, bitsPerSample, 3);
86 BtifAvrcpAudioTrack* trackHolder = static_cast<BtifAvrcpAudioTrack*>(track_handle);
87 std::unique_ptr<AAudioStream> stream(new AAudioStream);
88 // Set the values to track holder as mock audio lib APIs are a no-op.
89 trackHolder->stream = stream.get();
90 trackHolder->bufferLength = bufferLength;
91 trackHolder->buffer = new float[trackHolder->bufferLength]();
92
93 BtifAvrcpSetAudioTrackGain(trackHolder, gainValue);
94 // Create a fake buffer.
95 uint8_t data[bufferLength];
96 for (size_t index = 0; index < bufferLength; ++index) {
97 data[index] = index;
98 }
99 BtifAvrcpAudioTrackWriteData(trackHolder, data, bufferLength);
100 const int16_t* dataInt = (int16_t*)data;
101 for (size_t index = 0; index < bufferLength / sampleSize; ++index) {
102 const float expected = dataInt[index] * scaleQ15ToFloat * gainValue;
103 EXPECT_NEAR(expected, trackHolder->buffer[index], 0.01f);
104 }
105 BtifAvrcpAudioTrackDelete(trackHolder);
106 }
107