1 /*
2 * Copyright (C) 2022 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 #define LOG_TAG "test_mmap_path"
18
19 #include <vector>
20
21 #include <aaudio/AAudio.h>
22 #include <aaudio/AAudioTesting.h>
23 #include <android/log.h>
24 #include <android/media/audio/common/AudioMMapPolicyInfo.h>
25 #include <android/media/audio/common/AudioMMapPolicyType.h>
26 #include <media/AudioSystem.h>
27
28 #include <gtest/gtest.h>
29
30 #include "utility/AAudioUtilities.h"
31
32 using android::media::audio::common::AudioMMapPolicyInfo;
33 using android::media::audio::common::AudioMMapPolicyType;
34
35 /**
36 * Open a stream via AAudio API and set the performance mode as LOW_LATENCY. When MMAP is supported,
37 * the stream is supposed to be on MMAP path instead of legacy path. This is guaranteed on pixel
38 * devices, but may not be guaranteed on other vendor devices.
39 * @param direction the direction for the stream
40 */
openStreamAndVerify(aaudio_direction_t direction)41 static void openStreamAndVerify(aaudio_direction_t direction) {
42 std::vector<AudioMMapPolicyInfo> policyInfos;
43 ASSERT_EQ(android::NO_ERROR, android::AudioSystem::getMmapPolicyInfo(
44 AudioMMapPolicyType::DEFAULT, &policyInfos));
45 if (AAudio_getAAudioPolicy(policyInfos) == AAUDIO_POLICY_NEVER) {
46 // Query the system MMAP policy, if it is NEVER, it indicates there is no MMAP support.
47 // In that case, there is no need to run the test. The reason of adding the query is to
48 // avoid someone accidentally run the test on device that doesn't support MMAP,
49 // such as cuttlefish.
50 ALOGD("Skip test as mmap is not supported");
51 return;
52 }
53
54 AAudioStreamBuilder *aaudioBuilder = nullptr;
55 AAudioStream *aaudioStream = nullptr;
56
57 ASSERT_EQ(AAUDIO_OK, AAudio_createStreamBuilder(&aaudioBuilder));
58
59 AAudioStreamBuilder_setDirection(aaudioBuilder, direction);
60 AAudioStreamBuilder_setPerformanceMode(aaudioBuilder, AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
61
62 EXPECT_EQ(AAUDIO_OK, AAudioStreamBuilder_openStream(aaudioBuilder, &aaudioStream));
63 EXPECT_EQ(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY, AAudioStream_getPerformanceMode(aaudioStream));
64 EXPECT_TRUE(AAudioStream_isMMapUsed(aaudioStream));
65
66 AAudioStream_close(aaudioStream);
67 AAudioStreamBuilder_delete(aaudioBuilder);
68 }
69
TEST(test_mmap_path,input)70 TEST(test_mmap_path, input) {
71 openStreamAndVerify(AAUDIO_DIRECTION_INPUT);
72 }
73
TEST(test_mmap_path,output)74 TEST(test_mmap_path, output) {
75 openStreamAndVerify(AAUDIO_DIRECTION_OUTPUT);
76 }
77