• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 <gtest/gtest.h>
18 
19 #include <media/AudioCommonTypes.h>
20 #include <media/AidlConversion.h>
21 
22 using namespace android;
23 using namespace android::aidl_utils;
24 
25 using media::audio::common::AudioChannelLayout;
26 using media::audio::common::AudioDeviceDescription;
27 using media::audio::common::AudioDeviceType;
28 using media::audio::common::AudioFormatDescription;
29 using media::audio::common::AudioFormatType;
30 using media::audio::common::PcmType;
31 
32 namespace {
33 
hash(const T & t)34 template<typename T> size_t hash(const T& t) {
35     return std::hash<T>{}(t);
36 }
37 
make_ACL_None()38 AudioChannelLayout make_ACL_None() {
39     return AudioChannelLayout{};
40 }
41 
make_ACL_Invalid()42 AudioChannelLayout make_ACL_Invalid() {
43     return AudioChannelLayout::make<AudioChannelLayout::Tag::invalid>(0);
44 }
45 
make_ACL_Stereo()46 AudioChannelLayout make_ACL_Stereo() {
47     return AudioChannelLayout::make<AudioChannelLayout::Tag::layoutMask>(
48             AudioChannelLayout::LAYOUT_STEREO);
49 }
50 
make_ACL_LayoutArbitrary()51 AudioChannelLayout make_ACL_LayoutArbitrary() {
52     return AudioChannelLayout::make<AudioChannelLayout::Tag::layoutMask>(
53             // Use channels that exist both for input and output,
54             // but doesn't form a known layout mask.
55             AudioChannelLayout::CHANNEL_FRONT_LEFT |
56             AudioChannelLayout::CHANNEL_FRONT_RIGHT |
57             AudioChannelLayout::CHANNEL_TOP_SIDE_LEFT |
58             AudioChannelLayout::CHANNEL_TOP_SIDE_RIGHT);
59 }
60 
make_ACL_ChannelIndex2()61 AudioChannelLayout make_ACL_ChannelIndex2() {
62     return AudioChannelLayout::make<AudioChannelLayout::Tag::indexMask>(
63             AudioChannelLayout::INDEX_MASK_2);
64 }
65 
make_ACL_ChannelIndexArbitrary()66 AudioChannelLayout make_ACL_ChannelIndexArbitrary() {
67     // Use channels 1 and 3.
68     return AudioChannelLayout::make<AudioChannelLayout::Tag::indexMask>(5);
69 }
70 
make_ACL_VoiceCall()71 AudioChannelLayout make_ACL_VoiceCall() {
72     return AudioChannelLayout::make<AudioChannelLayout::Tag::voiceMask>(
73             AudioChannelLayout::VOICE_CALL_MONO);
74 }
75 
make_AudioDeviceDescription(AudioDeviceType type,const std::string & connection="")76 AudioDeviceDescription make_AudioDeviceDescription(AudioDeviceType type,
77         const std::string& connection = "") {
78     AudioDeviceDescription result;
79     result.type = type;
80     result.connection = connection;
81     return result;
82 }
83 
make_ADD_None()84 AudioDeviceDescription make_ADD_None() {
85     return AudioDeviceDescription{};
86 }
87 
make_ADD_DefaultIn()88 AudioDeviceDescription make_ADD_DefaultIn() {
89     return make_AudioDeviceDescription(AudioDeviceType::IN_DEFAULT);
90 }
91 
make_ADD_DefaultOut()92 AudioDeviceDescription make_ADD_DefaultOut() {
93     return make_AudioDeviceDescription(AudioDeviceType::OUT_DEFAULT);
94 }
95 
make_ADD_WiredHeadset()96 AudioDeviceDescription make_ADD_WiredHeadset() {
97     return make_AudioDeviceDescription(AudioDeviceType::OUT_HEADSET,
98             AudioDeviceDescription::CONNECTION_ANALOG());
99 }
100 
make_ADD_BtScoHeadset()101 AudioDeviceDescription make_ADD_BtScoHeadset() {
102     return make_AudioDeviceDescription(AudioDeviceType::OUT_HEADSET,
103             AudioDeviceDescription::CONNECTION_BT_SCO());
104 }
105 
make_AudioFormatDescription(AudioFormatType type)106 AudioFormatDescription make_AudioFormatDescription(AudioFormatType type) {
107     AudioFormatDescription result;
108     result.type = type;
109     return result;
110 }
111 
make_AudioFormatDescription(PcmType pcm)112 AudioFormatDescription make_AudioFormatDescription(PcmType pcm) {
113     auto result = make_AudioFormatDescription(AudioFormatType::PCM);
114     result.pcm = pcm;
115     return result;
116 }
117 
make_AudioFormatDescription(const std::string & encoding)118 AudioFormatDescription make_AudioFormatDescription(const std::string& encoding) {
119     AudioFormatDescription result;
120     result.encoding = encoding;
121     return result;
122 }
123 
make_AudioFormatDescription(PcmType transport,const std::string & encoding)124 AudioFormatDescription make_AudioFormatDescription(PcmType transport,
125         const std::string& encoding) {
126     auto result = make_AudioFormatDescription(encoding);
127     result.pcm = transport;
128     return result;
129 }
130 
make_AFD_Default()131 AudioFormatDescription make_AFD_Default() {
132     return AudioFormatDescription{};
133 }
134 
make_AFD_Invalid()135 AudioFormatDescription make_AFD_Invalid() {
136     return make_AudioFormatDescription(AudioFormatType::SYS_RESERVED_INVALID);
137 }
138 
make_AFD_Pcm16Bit()139 AudioFormatDescription make_AFD_Pcm16Bit() {
140     return make_AudioFormatDescription(PcmType::INT_16_BIT);
141 }
142 
make_AFD_Bitstream()143 AudioFormatDescription make_AFD_Bitstream() {
144     return make_AudioFormatDescription("example");
145 }
146 
make_AFD_Encap()147 AudioFormatDescription make_AFD_Encap() {
148     return make_AudioFormatDescription(PcmType::INT_16_BIT, "example.encap");
149 }
150 
make_AFD_Encap_with_Enc()151 AudioFormatDescription make_AFD_Encap_with_Enc() {
152     auto afd = make_AFD_Encap();
153     afd.encoding += "+example";
154     return afd;
155 }
156 
157 }  // namespace
158 
159 // Verify that two independently constructed ADDs/AFDs have the same hash.
160 // This ensures that regardless of whether the ADD/AFD instance originates
161 // from, it can be correctly compared to other ADD/AFD instance. Thus,
162 // for example, a 16-bit integer format description provided by HAL
163 // is identical to the same format description constructed by the framework.
164 class HashIdentityTest : public ::testing::Test {
165   public:
verifyHashIdentity(const std::vector<std::function<T ()>> & valueGens)166     template<typename T> void verifyHashIdentity(const std::vector<std::function<T()>>& valueGens) {
167         for (size_t i = 0; i < valueGens.size(); ++i) {
168             for (size_t j = 0; j < valueGens.size(); ++j) {
169                 if (i == j) {
170                     EXPECT_EQ(hash(valueGens[i]()), hash(valueGens[i]())) << i;
171                 } else {
172                     EXPECT_NE(hash(valueGens[i]()), hash(valueGens[j]())) << i << ", " << j;
173                 }
174             }
175         }
176     }
177 };
178 
TEST_F(HashIdentityTest,AudioChannelLayoutHashIdentity)179 TEST_F(HashIdentityTest, AudioChannelLayoutHashIdentity) {
180     verifyHashIdentity<AudioChannelLayout>({
181             make_ACL_None, make_ACL_Invalid, make_ACL_Stereo,
182             make_ACL_LayoutArbitrary, make_ACL_ChannelIndex2,
183             make_ACL_ChannelIndexArbitrary, make_ACL_VoiceCall});
184 }
185 
TEST_F(HashIdentityTest,AudioDeviceDescriptionHashIdentity)186 TEST_F(HashIdentityTest, AudioDeviceDescriptionHashIdentity) {
187     verifyHashIdentity<AudioDeviceDescription>({
188             make_ADD_None, make_ADD_DefaultIn, make_ADD_DefaultOut, make_ADD_WiredHeadset,
189             make_ADD_BtScoHeadset});
190 }
191 
TEST_F(HashIdentityTest,AudioFormatDescriptionHashIdentity)192 TEST_F(HashIdentityTest, AudioFormatDescriptionHashIdentity) {
193     verifyHashIdentity<AudioFormatDescription>({
194             make_AFD_Default, make_AFD_Invalid, make_AFD_Pcm16Bit, make_AFD_Bitstream,
195             make_AFD_Encap, make_AFD_Encap_with_Enc});
196 }
197 
198 using ChannelLayoutParam = std::tuple<AudioChannelLayout, bool /*isInput*/>;
199 class AudioChannelLayoutRoundTripTest :
200         public testing::TestWithParam<ChannelLayoutParam> {};
TEST_P(AudioChannelLayoutRoundTripTest,Aidl2Legacy2Aidl)201 TEST_P(AudioChannelLayoutRoundTripTest, Aidl2Legacy2Aidl) {
202     const auto initial = std::get<0>(GetParam());
203     const bool isInput = std::get<1>(GetParam());
204     auto conv = aidl2legacy_AudioChannelLayout_audio_channel_mask_t(initial, isInput);
205     ASSERT_TRUE(conv.ok());
206     auto convBack = legacy2aidl_audio_channel_mask_t_AudioChannelLayout(conv.value(), isInput);
207     ASSERT_TRUE(convBack.ok());
208     EXPECT_EQ(initial, convBack.value());
209 }
210 INSTANTIATE_TEST_SUITE_P(AudioChannelLayoutRoundTrip,
211         AudioChannelLayoutRoundTripTest,
212         testing::Combine(
213                 testing::Values(AudioChannelLayout{}, make_ACL_Invalid(), make_ACL_Stereo(),
214                         make_ACL_LayoutArbitrary(), make_ACL_ChannelIndex2(),
215                         make_ACL_ChannelIndexArbitrary()),
216                 testing::Values(false, true)));
217 INSTANTIATE_TEST_SUITE_P(AudioChannelVoiceRoundTrip,
218         AudioChannelLayoutRoundTripTest,
219         // In legacy constants the voice call is only defined for input.
220         testing::Combine(testing::Values(make_ACL_VoiceCall()), testing::Values(true)));
221 
222 using ChannelLayoutEdgeCaseParam = std::tuple<int /*legacy*/, bool /*isInput*/, bool /*isValid*/>;
223 class AudioChannelLayoutEdgeCaseTest :
224         public testing::TestWithParam<ChannelLayoutEdgeCaseParam> {};
TEST_P(AudioChannelLayoutEdgeCaseTest,Legacy2Aidl)225 TEST_P(AudioChannelLayoutEdgeCaseTest, Legacy2Aidl) {
226     const audio_channel_mask_t legacy = static_cast<audio_channel_mask_t>(std::get<0>(GetParam()));
227     const bool isInput = std::get<1>(GetParam());
228     const bool isValid = std::get<2>(GetParam());
229     auto conv = legacy2aidl_audio_channel_mask_t_AudioChannelLayout(legacy, isInput);
230     EXPECT_EQ(isValid, conv.ok());
231 }
232 INSTANTIATE_TEST_SUITE_P(AudioChannelLayoutEdgeCase,
233         AudioChannelLayoutEdgeCaseTest,
234         testing::Values(
235                 // Valid legacy input masks.
236                 std::make_tuple(AUDIO_CHANNEL_IN_VOICE_UPLINK_MONO, true, true),
237                 std::make_tuple(AUDIO_CHANNEL_IN_VOICE_DNLINK_MONO, true, true),
238                 std::make_tuple(AUDIO_CHANNEL_IN_VOICE_CALL_MONO, true, true),
239                 // Valid legacy output masks.
240                 std::make_tuple(
241                         // This has the same numerical representation as Mask 'A' below
242                         AUDIO_CHANNEL_OUT_FRONT_CENTER | AUDIO_CHANNEL_OUT_LOW_FREQUENCY |
243                         AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT, false, true),
244                 std::make_tuple(
245                         // This has the same numerical representation as Mask 'B' below
246                         AUDIO_CHANNEL_OUT_FRONT_CENTER | AUDIO_CHANNEL_OUT_LOW_FREQUENCY |
247                         AUDIO_CHANNEL_OUT_TOP_BACK_LEFT, false, true),
248                 // Invalid legacy input masks.
249                 std::make_tuple(AUDIO_CHANNEL_IN_6, true, false),
250                 std::make_tuple(
251                         AUDIO_CHANNEL_IN_6 | AUDIO_CHANNEL_IN_FRONT_PROCESSED, true, false),
252                 std::make_tuple(
253                         AUDIO_CHANNEL_IN_PRESSURE | AUDIO_CHANNEL_IN_X_AXIS |
254                         AUDIO_CHANNEL_IN_Y_AXIS | AUDIO_CHANNEL_IN_Z_AXIS, true, false),
255                 std::make_tuple(  // Mask 'A'
256                         AUDIO_CHANNEL_IN_STEREO | AUDIO_CHANNEL_IN_VOICE_UPLINK, true, false),
257                 std::make_tuple(  // Mask 'B'
258                         AUDIO_CHANNEL_IN_STEREO | AUDIO_CHANNEL_IN_VOICE_DNLINK, true, false)));
259 
260 class AudioDeviceDescriptionRoundTripTest :
261         public testing::TestWithParam<AudioDeviceDescription> {};
TEST_P(AudioDeviceDescriptionRoundTripTest,Aidl2Legacy2Aidl)262 TEST_P(AudioDeviceDescriptionRoundTripTest, Aidl2Legacy2Aidl) {
263     const auto initial = GetParam();
264     auto conv = aidl2legacy_AudioDeviceDescription_audio_devices_t(initial);
265     ASSERT_TRUE(conv.ok());
266     auto convBack = legacy2aidl_audio_devices_t_AudioDeviceDescription(conv.value());
267     ASSERT_TRUE(convBack.ok());
268     EXPECT_EQ(initial, convBack.value());
269 }
270 INSTANTIATE_TEST_SUITE_P(AudioDeviceDescriptionRoundTrip,
271         AudioDeviceDescriptionRoundTripTest,
272         testing::Values(AudioDeviceDescription{}, make_ADD_DefaultIn(),
273                 make_ADD_DefaultOut(), make_ADD_WiredHeadset(), make_ADD_BtScoHeadset()));
274 
275 class AudioFormatDescriptionRoundTripTest :
276         public testing::TestWithParam<AudioFormatDescription> {};
TEST_P(AudioFormatDescriptionRoundTripTest,Aidl2Legacy2Aidl)277 TEST_P(AudioFormatDescriptionRoundTripTest, Aidl2Legacy2Aidl) {
278     const auto initial = GetParam();
279     auto conv = aidl2legacy_AudioFormatDescription_audio_format_t(initial);
280     ASSERT_TRUE(conv.ok());
281     auto convBack = legacy2aidl_audio_format_t_AudioFormatDescription(conv.value());
282     ASSERT_TRUE(convBack.ok());
283     EXPECT_EQ(initial, convBack.value());
284 }
285 INSTANTIATE_TEST_SUITE_P(AudioFormatDescriptionRoundTrip,
286         AudioFormatDescriptionRoundTripTest,
287         testing::Values(make_AFD_Invalid(), AudioFormatDescription{}, make_AFD_Pcm16Bit()));
288