blob: 65e2533bc8fb1456de2bbdc2171c44974b188b2e [file] [log] [blame]
Jiabin Huangebe64102021-09-07 20:01:07 +00001/*
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 <aaudio/AAudio.h>
18#include <aaudio/AAudioTesting.h>
jiabine99d0882021-09-17 05:21:25 +000019#include <android/media/audio/common/AudioMMapPolicy.h>
Jiabin Huangebe64102021-09-07 20:01:07 +000020#include <cutils/properties.h>
21
22#include "PropertyUtils.h"
23
24namespace android {
25
jiabine99d0882021-09-17 05:21:25 +000026using media::audio::common::AudioMMapPolicy;
27using media::audio::common::AudioMMapPolicyType;
28using media::audio::common::AudioMMapPolicyInfo;
29
30std::string getMmapPolicyProperty(AudioMMapPolicyType policyType) {
Jiabin Huangebe64102021-09-07 20:01:07 +000031 switch (policyType) {
jiabine99d0882021-09-17 05:21:25 +000032 case AudioMMapPolicyType::DEFAULT:
Jiabin Huangebe64102021-09-07 20:01:07 +000033 return "aaudio.mmap_policy";
jiabine99d0882021-09-17 05:21:25 +000034 case AudioMMapPolicyType::EXCLUSIVE:
Jiabin Huangebe64102021-09-07 20:01:07 +000035 return "aaudio.mmap_exclusive_policy";
36 default:
37 return "";
38 }
39}
40
jiabine99d0882021-09-17 05:21:25 +000041int getDefaultPolicyFromType(AudioMMapPolicyType policyType) {
Jiabin Huangebe64102021-09-07 20:01:07 +000042 switch (policyType) {
jiabine99d0882021-09-17 05:21:25 +000043 case AudioMMapPolicyType::EXCLUSIVE:
Jiabin Huangebe64102021-09-07 20:01:07 +000044 return AAUDIO_UNSPECIFIED;
jiabine99d0882021-09-17 05:21:25 +000045 case AudioMMapPolicyType::DEFAULT:
Jiabin Huangebe64102021-09-07 20:01:07 +000046 default:
47 return AAUDIO_POLICY_NEVER;
48 }
49}
50
jiabine99d0882021-09-17 05:21:25 +000051AudioMMapPolicy legacy2aidl_aaudio_policy_t_AudioMMapPolicy(aaudio_policy_t legacy) {
Jiabin Huangebe64102021-09-07 20:01:07 +000052 switch (legacy) {
53 case AAUDIO_POLICY_NEVER:
jiabine99d0882021-09-17 05:21:25 +000054 return AudioMMapPolicy::NEVER;
Jiabin Huangebe64102021-09-07 20:01:07 +000055 case AAUDIO_POLICY_AUTO:
jiabine99d0882021-09-17 05:21:25 +000056 return AudioMMapPolicy::AUTO;
Jiabin Huangebe64102021-09-07 20:01:07 +000057 case AAUDIO_POLICY_ALWAYS:
jiabine99d0882021-09-17 05:21:25 +000058 return AudioMMapPolicy::ALWAYS;
Jiabin Huangebe64102021-09-07 20:01:07 +000059 case AAUDIO_UNSPECIFIED:
jiabine99d0882021-09-17 05:21:25 +000060 return AudioMMapPolicy::UNSPECIFIED;
Jiabin Huangebe64102021-09-07 20:01:07 +000061 default:
62 ALOGE("%s unknown aaudio policy: %d", __func__, legacy);
jiabine99d0882021-09-17 05:21:25 +000063 return AudioMMapPolicy::UNSPECIFIED;
Jiabin Huangebe64102021-09-07 20:01:07 +000064 }
65}
66
67status_t getMmapPolicyInfosFromSystemProperty(
jiabine99d0882021-09-17 05:21:25 +000068 AudioMMapPolicyType policyType, std::vector<AudioMMapPolicyInfo> *policyInfos) {
69 AudioMMapPolicyInfo policyInfo;
Jiabin Huangebe64102021-09-07 20:01:07 +000070 const std::string propertyStr = getMmapPolicyProperty(policyType);
71 if (propertyStr.empty()) {
72 return BAD_VALUE;
73 }
74 policyInfo.mmapPolicy = legacy2aidl_aaudio_policy_t_AudioMMapPolicy(
75 property_get_int32(propertyStr.c_str(), getDefaultPolicyFromType(policyType)));
76 policyInfos->push_back(policyInfo);
77 return NO_ERROR;
78}
79
jiabine504e7b2021-09-18 00:27:08 +000080int32_t getAAudioMixerBurstCountFromSystemProperty() {
81 static const int32_t sDefaultBursts = 2; // arbitrary, use 2 for double buffered
82 static const int32_t sMaxBursts = 1024; // arbitrary
83 static const char* sPropMixerBursts = "aaudio.mixer_bursts";
84 int32_t prop = property_get_int32(sPropMixerBursts, sDefaultBursts);
85 if (prop <= 0 || prop > sMaxBursts) {
86 ALOGE("%s: invalid value %d, use default %d", __func__, prop, sDefaultBursts);
87 prop = sDefaultBursts;
88 }
89 return prop;
90}
91
92int32_t getAAudioHardwareBurstMinUsecFromSystemProperty() {
93 static const int32_t sDefaultMicros = 1000; // arbitrary
94 static const int32_t sMaxMicros = 1000 * 1000; // arbitrary
95 static const char* sPropHwBurstMinUsec = "aaudio.hw_burst_min_usec";
96 int32_t prop = property_get_int32(sPropHwBurstMinUsec, sDefaultMicros);
97 if (prop <= 0 || prop > sMaxMicros) {
98 ALOGE("%s invalid value %d, use default %d", __func__, prop, sDefaultMicros);
99 prop = sDefaultMicros;
100 }
101 return prop;
102}
103
Jiabin Huangebe64102021-09-07 20:01:07 +0000104} // namespace android