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
17 #include <gtest/gtest.h>
18
19 #define LOG_TAG "SysAudio_Test"
20 #include <log/log.h>
21 #include <media/PatchBuilder.h>
22 #include <system/audio.h>
23
24 using namespace android;
25
TEST(SystemAudioTest,PatchInvalid)26 TEST(SystemAudioTest, PatchInvalid) {
27 audio_patch patch{};
28 ASSERT_FALSE(audio_patch_is_valid(&patch));
29 patch.num_sources = AUDIO_PATCH_PORTS_MAX + 1;
30 patch.num_sinks = 1;
31 ASSERT_FALSE(audio_patch_is_valid(&patch));
32 patch.num_sources = 1;
33 patch.num_sinks = AUDIO_PATCH_PORTS_MAX + 1;
34 ASSERT_FALSE(audio_patch_is_valid(&patch));
35 patch.num_sources = 0;
36 patch.num_sinks = 1;
37 ASSERT_FALSE(audio_patch_is_valid(&patch));
38 }
39
TEST(SystemAudioTest,PatchValid)40 TEST(SystemAudioTest, PatchValid) {
41 const audio_port_config src = {
42 .id = 1, .role = AUDIO_PORT_ROLE_SOURCE, .type = AUDIO_PORT_TYPE_DEVICE };
43 // It's OK not to have sinks.
44 ASSERT_TRUE(audio_patch_is_valid((PatchBuilder{}).addSource(src).patch()));
45 const audio_port_config sink = {
46 .id = 2, .role = AUDIO_PORT_ROLE_SINK, .type = AUDIO_PORT_TYPE_DEVICE };
47 ASSERT_TRUE(audio_patch_is_valid((PatchBuilder{}).addSource(src).addSink(sink).patch()));
48 ASSERT_TRUE(audio_patch_is_valid(
49 (PatchBuilder{}).addSource(src).addSource(src).addSink(sink).patch()));
50 ASSERT_TRUE(audio_patch_is_valid(
51 (PatchBuilder{}).addSource(src).addSink(sink).addSink(sink).patch()));
52 ASSERT_TRUE(audio_patch_is_valid(
53 (PatchBuilder{}).addSource(src).addSource(src).
54 addSink(sink).addSink(sink).patch()));
55 }
56
TEST(SystemAudioTest,PatchHwAvSync)57 TEST(SystemAudioTest, PatchHwAvSync) {
58 audio_port_config device_src_cfg = {
59 .id = 1, .role = AUDIO_PORT_ROLE_SOURCE, .type = AUDIO_PORT_TYPE_DEVICE };
60 ASSERT_FALSE(audio_port_config_has_hw_av_sync(&device_src_cfg));
61 device_src_cfg.config_mask |= AUDIO_PORT_CONFIG_FLAGS;
62 ASSERT_FALSE(audio_port_config_has_hw_av_sync(&device_src_cfg));
63 device_src_cfg.flags.input = AUDIO_INPUT_FLAG_HW_AV_SYNC;
64 ASSERT_TRUE(audio_port_config_has_hw_av_sync(&device_src_cfg));
65
66 audio_port_config device_sink_cfg = {
67 .id = 1, .role = AUDIO_PORT_ROLE_SINK, .type = AUDIO_PORT_TYPE_DEVICE };
68 ASSERT_FALSE(audio_port_config_has_hw_av_sync(&device_sink_cfg));
69 device_sink_cfg.config_mask |= AUDIO_PORT_CONFIG_FLAGS;
70 ASSERT_FALSE(audio_port_config_has_hw_av_sync(&device_sink_cfg));
71 device_sink_cfg.flags.output = AUDIO_OUTPUT_FLAG_HW_AV_SYNC;
72 ASSERT_TRUE(audio_port_config_has_hw_av_sync(&device_sink_cfg));
73
74 audio_port_config mix_sink_cfg = {
75 .id = 1, .role = AUDIO_PORT_ROLE_SINK, .type = AUDIO_PORT_TYPE_MIX };
76 ASSERT_FALSE(audio_port_config_has_hw_av_sync(&mix_sink_cfg));
77 mix_sink_cfg.config_mask |= AUDIO_PORT_CONFIG_FLAGS;
78 ASSERT_FALSE(audio_port_config_has_hw_av_sync(&mix_sink_cfg));
79 mix_sink_cfg.flags.input = AUDIO_INPUT_FLAG_HW_AV_SYNC;
80 ASSERT_TRUE(audio_port_config_has_hw_av_sync(&mix_sink_cfg));
81
82 audio_port_config mix_src_cfg = {
83 .id = 1, .role = AUDIO_PORT_ROLE_SOURCE, .type = AUDIO_PORT_TYPE_MIX };
84 ASSERT_FALSE(audio_port_config_has_hw_av_sync(&mix_src_cfg));
85 mix_src_cfg.config_mask |= AUDIO_PORT_CONFIG_FLAGS;
86 ASSERT_FALSE(audio_port_config_has_hw_av_sync(&mix_src_cfg));
87 mix_src_cfg.flags.output = AUDIO_OUTPUT_FLAG_HW_AV_SYNC;
88 ASSERT_TRUE(audio_port_config_has_hw_av_sync(&mix_src_cfg));
89 }
90
TEST(SystemAudioTest,PatchEqual)91 TEST(SystemAudioTest, PatchEqual) {
92 const audio_patch patch1{}, patch2{};
93 // Invalid patches are not equal.
94 ASSERT_FALSE(audio_patches_are_equal(&patch1, &patch2));
95 const audio_port_config src = {
96 .id = 1, .role = AUDIO_PORT_ROLE_SOURCE, .type = AUDIO_PORT_TYPE_DEVICE };
97 const audio_port_config sink = {
98 .id = 2, .role = AUDIO_PORT_ROLE_SINK, .type = AUDIO_PORT_TYPE_DEVICE };
99 ASSERT_FALSE(audio_patches_are_equal(
100 (PatchBuilder{}).addSource(src).patch(),
101 (PatchBuilder{}).addSource(src).addSink(sink).patch()));
102 ASSERT_TRUE(audio_patches_are_equal(
103 (PatchBuilder{}).addSource(src).addSink(sink).patch(),
104 (PatchBuilder{}).addSource(src).addSink(sink).patch()));
105 ASSERT_FALSE(audio_patches_are_equal(
106 (PatchBuilder{}).addSource(src).addSink(sink).patch(),
107 (PatchBuilder{}).addSource(src).addSource(src).addSink(sink).patch()));
108 audio_port_config sink_hw_av_sync = sink;
109 sink_hw_av_sync.config_mask |= AUDIO_PORT_CONFIG_FLAGS;
110 sink_hw_av_sync.flags.output = AUDIO_OUTPUT_FLAG_HW_AV_SYNC;
111 ASSERT_FALSE(audio_patches_are_equal(
112 (PatchBuilder{}).addSource(src).addSink(sink).patch(),
113 (PatchBuilder{}).addSource(src).addSink(sink_hw_av_sync).patch()));
114 ASSERT_TRUE(audio_patches_are_equal(
115 (PatchBuilder{}).addSource(src).addSink(sink_hw_av_sync).patch(),
116 (PatchBuilder{}).addSource(src).addSink(sink_hw_av_sync).patch()));
117 }
118