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