1 /*
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>
19 #include <android/media/audio/common/AudioMMapPolicy.h>
20 #include <cutils/properties.h>
21
22 #include "PropertyUtils.h"
23
24 namespace android {
25
26 using media::audio::common::AudioMMapPolicy;
27 using media::audio::common::AudioMMapPolicyType;
28 using media::audio::common::AudioMMapPolicyInfo;
29
getMmapPolicyProperty(AudioMMapPolicyType policyType)30 std::string getMmapPolicyProperty(AudioMMapPolicyType policyType) {
31 switch (policyType) {
32 case AudioMMapPolicyType::DEFAULT:
33 return "aaudio.mmap_policy";
34 case AudioMMapPolicyType::EXCLUSIVE:
35 return "aaudio.mmap_exclusive_policy";
36 default:
37 return "";
38 }
39 }
40
getDefaultPolicyFromType(AudioMMapPolicyType policyType)41 int getDefaultPolicyFromType(AudioMMapPolicyType policyType) {
42 switch (policyType) {
43 case AudioMMapPolicyType::EXCLUSIVE:
44 return AAUDIO_UNSPECIFIED;
45 case AudioMMapPolicyType::DEFAULT:
46 default:
47 return AAUDIO_POLICY_NEVER;
48 }
49 }
50
legacy2aidl_aaudio_policy_t_AudioMMapPolicy(aaudio_policy_t legacy)51 AudioMMapPolicy legacy2aidl_aaudio_policy_t_AudioMMapPolicy(aaudio_policy_t legacy) {
52 switch (legacy) {
53 case AAUDIO_POLICY_NEVER:
54 return AudioMMapPolicy::NEVER;
55 case AAUDIO_POLICY_AUTO:
56 return AudioMMapPolicy::AUTO;
57 case AAUDIO_POLICY_ALWAYS:
58 return AudioMMapPolicy::ALWAYS;
59 case AAUDIO_UNSPECIFIED:
60 return AudioMMapPolicy::UNSPECIFIED;
61 default:
62 ALOGE("%s unknown aaudio policy: %d", __func__, legacy);
63 return AudioMMapPolicy::UNSPECIFIED;
64 }
65 }
66
getMmapPolicyInfosFromSystemProperty(AudioMMapPolicyType policyType,std::vector<AudioMMapPolicyInfo> * policyInfos)67 status_t getMmapPolicyInfosFromSystemProperty(
68 AudioMMapPolicyType policyType, std::vector<AudioMMapPolicyInfo> *policyInfos) {
69 AudioMMapPolicyInfo policyInfo;
70 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
getAAudioMixerBurstCountFromSystemProperty()80 int32_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
getAAudioHardwareBurstMinUsecFromSystemProperty()92 int32_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
104 } // namespace android
105