Mikhail Naganov | ac9858b | 2018-06-15 13:12:37 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
jiabin | d7631f0 | 2019-09-16 17:08:21 -0700 | [diff] [blame^] | 17 | #include <unordered_set> |
| 18 | |
Mikhail Naganov | ac9858b | 2018-06-15 13:12:37 -0700 | [diff] [blame] | 19 | #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 | |
| 26 | using namespace android; |
| 27 | |
| 28 | TEST(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 | |
| 42 | TEST(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 | |
| 59 | TEST(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 | |
| 93 | TEST(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 | } |
jiabin | d7631f0 | 2019-09-16 17:08:21 -0700 | [diff] [blame^] | 120 | |
| 121 | void 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 | |
| 139 | TEST(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, |
| 147 | AUDIO_DEVICE_OUT_CNT, "output", audio_is_output_device); |
| 148 | runAudioDeviceTypeHelperFunction(allDeviceTypes, AUDIO_DEVICE_IN_ALL_ARRAY, |
| 149 | AUDIO_DEVICE_IN_CNT, "input", audio_is_input_device); |
| 150 | runAudioDeviceTypeHelperFunction(allDeviceTypes, AUDIO_DEVICE_OUT_ALL_A2DP_ARRAY, |
| 151 | AUDIO_DEVICE_OUT_A2DP_CNT, "a2dp out", audio_is_a2dp_out_device); |
| 152 | const audio_devices_t bluetoothInA2dpDevices[] = { AUDIO_DEVICE_IN_BLUETOOTH_A2DP }; |
| 153 | runAudioDeviceTypeHelperFunction(allDeviceTypes, bluetoothInA2dpDevices, |
| 154 | 1 /*targetDeviceCount*/, "a2dp in", audio_is_a2dp_in_device); |
| 155 | runAudioDeviceTypeHelperFunction(allDeviceTypes, AUDIO_DEVICE_OUT_ALL_SCO_ARRAY, |
| 156 | AUDIO_DEVICE_OUT_SCO_CNT, "bluetooth out sco", audio_is_bluetooth_out_sco_device); |
| 157 | runAudioDeviceTypeHelperFunction(allDeviceTypes, AUDIO_DEVICE_IN_ALL_SCO_ARRAY, |
| 158 | AUDIO_DEVICE_IN_SCO_CNT, "bluetooth in sco", audio_is_bluetooth_in_sco_device); |
| 159 | const unsigned int scoDeviceCount = AUDIO_DEVICE_OUT_SCO_CNT + AUDIO_DEVICE_IN_SCO_CNT; |
| 160 | audio_devices_t scoDevices[scoDeviceCount]; |
| 161 | std::copy(std::begin(AUDIO_DEVICE_OUT_ALL_SCO_ARRAY), std::end(AUDIO_DEVICE_OUT_ALL_SCO_ARRAY), |
| 162 | std::begin(scoDevices)); |
| 163 | std::copy(std::begin(AUDIO_DEVICE_IN_ALL_SCO_ARRAY), std::end(AUDIO_DEVICE_IN_ALL_SCO_ARRAY), |
| 164 | std::begin(scoDevices) + AUDIO_DEVICE_OUT_SCO_CNT); |
| 165 | runAudioDeviceTypeHelperFunction(allDeviceTypes, scoDevices, |
| 166 | scoDeviceCount, "bluetooth sco", audio_is_bluetooth_sco_device); |
| 167 | const audio_devices_t hearingAidOutDevices[] = { AUDIO_DEVICE_OUT_HEARING_AID }; |
| 168 | runAudioDeviceTypeHelperFunction(allDeviceTypes, hearingAidOutDevices, |
| 169 | 1 /*targetDeviceCount*/, "hearing aid out", audio_is_hearing_aid_out_device); |
| 170 | runAudioDeviceTypeHelperFunction(allDeviceTypes, AUDIO_DEVICE_OUT_ALL_USB_ARRAY, |
| 171 | AUDIO_DEVICE_OUT_USB_CNT, "usb out", audio_is_usb_out_device); |
| 172 | runAudioDeviceTypeHelperFunction(allDeviceTypes, AUDIO_DEVICE_IN_ALL_USB_ARRAY, |
| 173 | AUDIO_DEVICE_IN_USB_CNT, "usb in", audio_is_usb_in_device); |
| 174 | } |