blob: 1e4d07fc6de063a5ad4373fc37f57071e95932ff [file] [log] [blame]
Mikhail Naganovac9858b2018-06-15 13:12:37 -07001/*
2 * Copyright (C) 2018 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
jiabind7631f02019-09-16 17:08:21 -070017#include <unordered_set>
18
Mikhail Naganovac9858b2018-06-15 13:12:37 -070019#include <gtest/gtest.h>
20
21#define LOG_TAG "SysAudio_Test"
22#include <log/log.h>
23#include <media/PatchBuilder.h>
24#include <system/audio.h>
25
26using namespace android;
27
28TEST(SystemAudioTest, PatchInvalid) {
29 audio_patch patch{};
30 ASSERT_FALSE(audio_patch_is_valid(&patch));
31 patch.num_sources = AUDIO_PATCH_PORTS_MAX + 1;
32 patch.num_sinks = 1;
33 ASSERT_FALSE(audio_patch_is_valid(&patch));
34 patch.num_sources = 1;
35 patch.num_sinks = AUDIO_PATCH_PORTS_MAX + 1;
36 ASSERT_FALSE(audio_patch_is_valid(&patch));
37 patch.num_sources = 0;
38 patch.num_sinks = 1;
39 ASSERT_FALSE(audio_patch_is_valid(&patch));
40}
41
42TEST(SystemAudioTest, PatchValid) {
43 const audio_port_config src = {
44 .id = 1, .role = AUDIO_PORT_ROLE_SOURCE, .type = AUDIO_PORT_TYPE_DEVICE };
45 // It's OK not to have sinks.
46 ASSERT_TRUE(audio_patch_is_valid((PatchBuilder{}).addSource(src).patch()));
47 const audio_port_config sink = {
48 .id = 2, .role = AUDIO_PORT_ROLE_SINK, .type = AUDIO_PORT_TYPE_DEVICE };
49 ASSERT_TRUE(audio_patch_is_valid((PatchBuilder{}).addSource(src).addSink(sink).patch()));
50 ASSERT_TRUE(audio_patch_is_valid(
51 (PatchBuilder{}).addSource(src).addSource(src).addSink(sink).patch()));
52 ASSERT_TRUE(audio_patch_is_valid(
53 (PatchBuilder{}).addSource(src).addSink(sink).addSink(sink).patch()));
54 ASSERT_TRUE(audio_patch_is_valid(
55 (PatchBuilder{}).addSource(src).addSource(src).
56 addSink(sink).addSink(sink).patch()));
57}
58
59TEST(SystemAudioTest, PatchHwAvSync) {
60 audio_port_config device_src_cfg = {
61 .id = 1, .role = AUDIO_PORT_ROLE_SOURCE, .type = AUDIO_PORT_TYPE_DEVICE };
62 ASSERT_FALSE(audio_port_config_has_hw_av_sync(&device_src_cfg));
63 device_src_cfg.config_mask |= AUDIO_PORT_CONFIG_FLAGS;
64 ASSERT_FALSE(audio_port_config_has_hw_av_sync(&device_src_cfg));
65 device_src_cfg.flags.input = AUDIO_INPUT_FLAG_HW_AV_SYNC;
66 ASSERT_TRUE(audio_port_config_has_hw_av_sync(&device_src_cfg));
67
68 audio_port_config device_sink_cfg = {
69 .id = 1, .role = AUDIO_PORT_ROLE_SINK, .type = AUDIO_PORT_TYPE_DEVICE };
70 ASSERT_FALSE(audio_port_config_has_hw_av_sync(&device_sink_cfg));
71 device_sink_cfg.config_mask |= AUDIO_PORT_CONFIG_FLAGS;
72 ASSERT_FALSE(audio_port_config_has_hw_av_sync(&device_sink_cfg));
73 device_sink_cfg.flags.output = AUDIO_OUTPUT_FLAG_HW_AV_SYNC;
74 ASSERT_TRUE(audio_port_config_has_hw_av_sync(&device_sink_cfg));
75
76 audio_port_config mix_sink_cfg = {
77 .id = 1, .role = AUDIO_PORT_ROLE_SINK, .type = AUDIO_PORT_TYPE_MIX };
78 ASSERT_FALSE(audio_port_config_has_hw_av_sync(&mix_sink_cfg));
79 mix_sink_cfg.config_mask |= AUDIO_PORT_CONFIG_FLAGS;
80 ASSERT_FALSE(audio_port_config_has_hw_av_sync(&mix_sink_cfg));
81 mix_sink_cfg.flags.input = AUDIO_INPUT_FLAG_HW_AV_SYNC;
82 ASSERT_TRUE(audio_port_config_has_hw_av_sync(&mix_sink_cfg));
83
84 audio_port_config mix_src_cfg = {
85 .id = 1, .role = AUDIO_PORT_ROLE_SOURCE, .type = AUDIO_PORT_TYPE_MIX };
86 ASSERT_FALSE(audio_port_config_has_hw_av_sync(&mix_src_cfg));
87 mix_src_cfg.config_mask |= AUDIO_PORT_CONFIG_FLAGS;
88 ASSERT_FALSE(audio_port_config_has_hw_av_sync(&mix_src_cfg));
89 mix_src_cfg.flags.output = AUDIO_OUTPUT_FLAG_HW_AV_SYNC;
90 ASSERT_TRUE(audio_port_config_has_hw_av_sync(&mix_src_cfg));
91}
92
93TEST(SystemAudioTest, PatchEqual) {
94 const audio_patch patch1{}, patch2{};
95 // Invalid patches are not equal.
96 ASSERT_FALSE(audio_patches_are_equal(&patch1, &patch2));
97 const audio_port_config src = {
98 .id = 1, .role = AUDIO_PORT_ROLE_SOURCE, .type = AUDIO_PORT_TYPE_DEVICE };
99 const audio_port_config sink = {
100 .id = 2, .role = AUDIO_PORT_ROLE_SINK, .type = AUDIO_PORT_TYPE_DEVICE };
101 ASSERT_FALSE(audio_patches_are_equal(
102 (PatchBuilder{}).addSource(src).patch(),
103 (PatchBuilder{}).addSource(src).addSink(sink).patch()));
104 ASSERT_TRUE(audio_patches_are_equal(
105 (PatchBuilder{}).addSource(src).addSink(sink).patch(),
106 (PatchBuilder{}).addSource(src).addSink(sink).patch()));
107 ASSERT_FALSE(audio_patches_are_equal(
108 (PatchBuilder{}).addSource(src).addSink(sink).patch(),
109 (PatchBuilder{}).addSource(src).addSource(src).addSink(sink).patch()));
110 audio_port_config sink_hw_av_sync = sink;
111 sink_hw_av_sync.config_mask |= AUDIO_PORT_CONFIG_FLAGS;
112 sink_hw_av_sync.flags.output = AUDIO_OUTPUT_FLAG_HW_AV_SYNC;
113 ASSERT_FALSE(audio_patches_are_equal(
114 (PatchBuilder{}).addSource(src).addSink(sink).patch(),
115 (PatchBuilder{}).addSource(src).addSink(sink_hw_av_sync).patch()));
116 ASSERT_TRUE(audio_patches_are_equal(
117 (PatchBuilder{}).addSource(src).addSink(sink_hw_av_sync).patch(),
118 (PatchBuilder{}).addSource(src).addSink(sink_hw_av_sync).patch()));
119}
jiabind7631f02019-09-16 17:08:21 -0700120
121void runAudioDeviceTypeHelperFunction(const std::unordered_set<audio_devices_t>& allDevices,
122 const audio_devices_t targetDevices[],
123 unsigned int targetDeviceCount,
124 const std::string& deviceTag,
125 bool (*device_type_helper_function)(audio_devices_t))
126{
127 std::unordered_set<audio_devices_t> devices(targetDevices, targetDevices + targetDeviceCount);
128 for (auto device : allDevices) {
129 if (devices.find(device) == devices.end()) {
130 ASSERT_FALSE(device_type_helper_function(device))
131 << std::hex << device << " should not be " << deviceTag << " device";
132 } else {
133 ASSERT_TRUE(device_type_helper_function(device))
134 << std::hex << device << " should be " << deviceTag << " device";
135 }
136 }
137}
138
139TEST(SystemAudioTest, AudioDeviceTypeHelperFunction) {
140 std::unordered_set<audio_devices_t> allDeviceTypes;
141 allDeviceTypes.insert(std::begin(AUDIO_DEVICE_OUT_ALL_ARRAY),
142 std::end(AUDIO_DEVICE_OUT_ALL_ARRAY));
143 allDeviceTypes.insert(std::begin(AUDIO_DEVICE_IN_ALL_ARRAY),
144 std::end(AUDIO_DEVICE_IN_ALL_ARRAY));
145
146 runAudioDeviceTypeHelperFunction(allDeviceTypes, AUDIO_DEVICE_OUT_ALL_ARRAY,
jiabinf3d474d2019-09-19 17:55:16 -0700147 std::size(AUDIO_DEVICE_OUT_ALL_ARRAY), "output", audio_is_output_device);
jiabind7631f02019-09-16 17:08:21 -0700148 runAudioDeviceTypeHelperFunction(allDeviceTypes, AUDIO_DEVICE_IN_ALL_ARRAY,
jiabinf3d474d2019-09-19 17:55:16 -0700149 std::size(AUDIO_DEVICE_IN_ALL_ARRAY), "input", audio_is_input_device);
jiabind7631f02019-09-16 17:08:21 -0700150 runAudioDeviceTypeHelperFunction(allDeviceTypes, AUDIO_DEVICE_OUT_ALL_A2DP_ARRAY,
jiabinf3d474d2019-09-19 17:55:16 -0700151 std::size(AUDIO_DEVICE_OUT_ALL_A2DP_ARRAY), "a2dp out", audio_is_a2dp_out_device);
jiabind7631f02019-09-16 17:08:21 -0700152 const audio_devices_t bluetoothInA2dpDevices[] = { AUDIO_DEVICE_IN_BLUETOOTH_A2DP };
153 runAudioDeviceTypeHelperFunction(allDeviceTypes, bluetoothInA2dpDevices,
jiabinf3d474d2019-09-19 17:55:16 -0700154 std::size(bluetoothInA2dpDevices), "a2dp in", audio_is_a2dp_in_device);
jiabind7631f02019-09-16 17:08:21 -0700155 runAudioDeviceTypeHelperFunction(allDeviceTypes, AUDIO_DEVICE_OUT_ALL_SCO_ARRAY,
jiabinf3d474d2019-09-19 17:55:16 -0700156 std::size(AUDIO_DEVICE_OUT_ALL_SCO_ARRAY), "bluetooth out sco",
157 audio_is_bluetooth_out_sco_device);
jiabind7631f02019-09-16 17:08:21 -0700158 runAudioDeviceTypeHelperFunction(allDeviceTypes, AUDIO_DEVICE_IN_ALL_SCO_ARRAY,
jiabinf3d474d2019-09-19 17:55:16 -0700159 std::size(AUDIO_DEVICE_IN_ALL_SCO_ARRAY), "bluetooth in sco",
160 audio_is_bluetooth_in_sco_device);
jiabind7631f02019-09-16 17:08:21 -0700161 const unsigned int scoDeviceCount = AUDIO_DEVICE_OUT_SCO_CNT + AUDIO_DEVICE_IN_SCO_CNT;
162 audio_devices_t scoDevices[scoDeviceCount];
163 std::copy(std::begin(AUDIO_DEVICE_OUT_ALL_SCO_ARRAY), std::end(AUDIO_DEVICE_OUT_ALL_SCO_ARRAY),
164 std::begin(scoDevices));
165 std::copy(std::begin(AUDIO_DEVICE_IN_ALL_SCO_ARRAY), std::end(AUDIO_DEVICE_IN_ALL_SCO_ARRAY),
166 std::begin(scoDevices) + AUDIO_DEVICE_OUT_SCO_CNT);
167 runAudioDeviceTypeHelperFunction(allDeviceTypes, scoDevices,
jiabinf3d474d2019-09-19 17:55:16 -0700168 std::size(scoDevices), "bluetooth sco", audio_is_bluetooth_sco_device);
jiabind7631f02019-09-16 17:08:21 -0700169 const audio_devices_t hearingAidOutDevices[] = { AUDIO_DEVICE_OUT_HEARING_AID };
170 runAudioDeviceTypeHelperFunction(allDeviceTypes, hearingAidOutDevices,
jiabinf3d474d2019-09-19 17:55:16 -0700171 std::size(hearingAidOutDevices), "hearing aid out", audio_is_hearing_aid_out_device);
jiabind7631f02019-09-16 17:08:21 -0700172 runAudioDeviceTypeHelperFunction(allDeviceTypes, AUDIO_DEVICE_OUT_ALL_USB_ARRAY,
jiabinf3d474d2019-09-19 17:55:16 -0700173 std::size(AUDIO_DEVICE_OUT_ALL_USB_ARRAY), "usb out", audio_is_usb_out_device);
jiabind7631f02019-09-16 17:08:21 -0700174 runAudioDeviceTypeHelperFunction(allDeviceTypes, AUDIO_DEVICE_IN_ALL_USB_ARRAY,
jiabinf3d474d2019-09-19 17:55:16 -0700175 std::size(AUDIO_DEVICE_IN_ALL_USB_ARRAY), "usb in", audio_is_usb_in_device);
176 const audio_devices_t remoteSubmixDevices[] = {
177 AUDIO_DEVICE_IN_REMOTE_SUBMIX, AUDIO_DEVICE_OUT_REMOTE_SUBMIX };
178 runAudioDeviceTypeHelperFunction(allDeviceTypes, remoteSubmixDevices,
179 std::size(remoteSubmixDevices), "remote submix", audio_is_remote_submix_device);
180 runAudioDeviceTypeHelperFunction(allDeviceTypes, AUDIO_DEVICE_OUT_ALL_DIGITAL_ARRAY,
181 std::size(AUDIO_DEVICE_OUT_ALL_DIGITAL_ARRAY), "digital out",
182 audio_is_digital_out_device);
183 runAudioDeviceTypeHelperFunction(allDeviceTypes, AUDIO_DEVICE_IN_ALL_DIGITAL_ARRAY,
184 std::size(AUDIO_DEVICE_IN_ALL_DIGITAL_ARRAY), "digital in",
185 audio_is_digital_in_device);
186 const unsigned int digitalDeviceCount
187 = AUDIO_DEVICE_OUT_DIGITAL_CNT + AUDIO_DEVICE_IN_DIGITAL_CNT;
188 audio_devices_t digitalDevices[digitalDeviceCount];
189 std::copy(std::begin(AUDIO_DEVICE_OUT_ALL_DIGITAL_ARRAY),
190 std::end(AUDIO_DEVICE_OUT_ALL_DIGITAL_ARRAY),
191 std::begin(digitalDevices));
192 std::copy(std::begin(AUDIO_DEVICE_IN_ALL_DIGITAL_ARRAY),
193 std::end(AUDIO_DEVICE_IN_ALL_DIGITAL_ARRAY),
194 std::begin(digitalDevices) + AUDIO_DEVICE_OUT_DIGITAL_CNT);
195 runAudioDeviceTypeHelperFunction(allDeviceTypes, digitalDevices,
196 std::size(digitalDevices), "digital", audio_device_is_digital);
jiabind7631f02019-09-16 17:08:21 -0700197}