Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
jiabin | 9ccb28b | 2019-07-31 09:59:09 -0700 | [diff] [blame] | 17 | #define LOG_TAG "AudioGain" |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | |
| 20 | //#define VERY_VERBOSE_LOGGING |
| 21 | #ifdef VERY_VERBOSE_LOGGING |
| 22 | #define ALOGVV ALOGV |
| 23 | #else |
| 24 | #define ALOGVV(a...) do { } while(0) |
| 25 | #endif |
| 26 | |
jiabin | 49e69a1 | 2019-10-15 16:04:13 -0700 | [diff] [blame] | 27 | #include <algorithm> |
| 28 | |
jiabin | 9ccb28b | 2019-07-31 09:59:09 -0700 | [diff] [blame] | 29 | #include <android-base/stringprintf.h> |
| 30 | #include <media/AudioGain.h> |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 31 | #include <utils/Log.h> |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 32 | |
| 33 | #include <math.h> |
| 34 | |
| 35 | namespace android { |
| 36 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 37 | AudioGain::AudioGain(int index, bool useInChannelMask) |
| 38 | { |
| 39 | mIndex = index; |
| 40 | mUseInChannelMask = useInChannelMask; |
| 41 | memset(&mGain, 0, sizeof(struct audio_gain)); |
| 42 | } |
| 43 | |
| 44 | void AudioGain::getDefaultConfig(struct audio_gain_config *config) |
| 45 | { |
| 46 | config->index = mIndex; |
| 47 | config->mode = mGain.mode; |
| 48 | config->channel_mask = mGain.channel_mask; |
| 49 | if ((mGain.mode & AUDIO_GAIN_MODE_JOINT) == AUDIO_GAIN_MODE_JOINT) { |
| 50 | config->values[0] = mGain.default_value; |
| 51 | } else { |
| 52 | uint32_t numValues; |
| 53 | if (mUseInChannelMask) { |
| 54 | numValues = audio_channel_count_from_in_mask(mGain.channel_mask); |
| 55 | } else { |
| 56 | numValues = audio_channel_count_from_out_mask(mGain.channel_mask); |
| 57 | } |
| 58 | for (size_t i = 0; i < numValues; i++) { |
| 59 | config->values[i] = mGain.default_value; |
| 60 | } |
| 61 | } |
| 62 | if ((mGain.mode & AUDIO_GAIN_MODE_RAMP) == AUDIO_GAIN_MODE_RAMP) { |
| 63 | config->ramp_duration_ms = mGain.min_ramp_ms; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | status_t AudioGain::checkConfig(const struct audio_gain_config *config) |
| 68 | { |
| 69 | if ((config->mode & ~mGain.mode) != 0) { |
| 70 | return BAD_VALUE; |
| 71 | } |
| 72 | if ((config->mode & AUDIO_GAIN_MODE_JOINT) == AUDIO_GAIN_MODE_JOINT) { |
| 73 | if ((config->values[0] < mGain.min_value) || |
| 74 | (config->values[0] > mGain.max_value)) { |
| 75 | return BAD_VALUE; |
| 76 | } |
| 77 | } else { |
| 78 | if ((config->channel_mask & ~mGain.channel_mask) != 0) { |
| 79 | return BAD_VALUE; |
| 80 | } |
| 81 | uint32_t numValues; |
| 82 | if (mUseInChannelMask) { |
| 83 | numValues = audio_channel_count_from_in_mask(config->channel_mask); |
| 84 | } else { |
| 85 | numValues = audio_channel_count_from_out_mask(config->channel_mask); |
| 86 | } |
| 87 | for (size_t i = 0; i < numValues; i++) { |
| 88 | if ((config->values[i] < mGain.min_value) || |
| 89 | (config->values[i] > mGain.max_value)) { |
| 90 | return BAD_VALUE; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | if ((config->mode & AUDIO_GAIN_MODE_RAMP) == AUDIO_GAIN_MODE_RAMP) { |
| 95 | if ((config->ramp_duration_ms < mGain.min_ramp_ms) || |
| 96 | (config->ramp_duration_ms > mGain.max_ramp_ms)) { |
| 97 | return BAD_VALUE; |
| 98 | } |
| 99 | } |
| 100 | return NO_ERROR; |
| 101 | } |
| 102 | |
jiabin | 9ccb28b | 2019-07-31 09:59:09 -0700 | [diff] [blame] | 103 | void AudioGain::dump(std::string *dst, int spaces, int index) const |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 104 | { |
jiabin | 9ccb28b | 2019-07-31 09:59:09 -0700 | [diff] [blame] | 105 | dst->append(base::StringPrintf("%*sGain %d:\n", spaces, "", index+1)); |
| 106 | dst->append(base::StringPrintf("%*s- mode: %08x\n", spaces, "", mGain.mode)); |
| 107 | dst->append(base::StringPrintf("%*s- channel_mask: %08x\n", spaces, "", mGain.channel_mask)); |
| 108 | dst->append(base::StringPrintf("%*s- min_value: %d mB\n", spaces, "", mGain.min_value)); |
| 109 | dst->append(base::StringPrintf("%*s- max_value: %d mB\n", spaces, "", mGain.max_value)); |
| 110 | dst->append(base::StringPrintf("%*s- default_value: %d mB\n", spaces, "", mGain.default_value)); |
| 111 | dst->append(base::StringPrintf("%*s- step_value: %d mB\n", spaces, "", mGain.step_value)); |
| 112 | dst->append(base::StringPrintf("%*s- min_ramp_ms: %d ms\n", spaces, "", mGain.min_ramp_ms)); |
| 113 | dst->append(base::StringPrintf("%*s- max_ramp_ms: %d ms\n", spaces, "", mGain.max_ramp_ms)); |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 114 | } |
| 115 | |
jiabin | 49e69a1 | 2019-10-15 16:04:13 -0700 | [diff] [blame] | 116 | bool AudioGain::equals(const sp<AudioGain>& other) const |
| 117 | { |
| 118 | return other != nullptr && |
| 119 | mUseInChannelMask == other->mUseInChannelMask && |
| 120 | mUseForVolume == other->mUseForVolume && |
| 121 | // Compare audio gain |
| 122 | mGain.mode == other->mGain.mode && |
| 123 | mGain.channel_mask == other->mGain.channel_mask && |
| 124 | mGain.min_value == other->mGain.min_value && |
| 125 | mGain.max_value == other->mGain.max_value && |
| 126 | mGain.default_value == other->mGain.default_value && |
| 127 | mGain.step_value == other->mGain.step_value && |
| 128 | mGain.min_ramp_ms == other->mGain.min_ramp_ms && |
| 129 | mGain.max_ramp_ms == other->mGain.max_ramp_ms; |
| 130 | } |
| 131 | |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 132 | status_t AudioGain::writeToParcel(android::Parcel *parcel) const { |
| 133 | media::AudioGain parcelable; |
| 134 | return writeToParcelable(&parcelable) |
| 135 | ?: parcelable.writeToParcel(parcel); |
jiabin | 84e43c7 | 2019-07-31 17:23:17 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 138 | status_t AudioGain::writeToParcelable(media::AudioGain* parcelable) const { |
| 139 | parcelable->index = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(mIndex)); |
| 140 | parcelable->useInChannelMask = mUseInChannelMask; |
| 141 | parcelable->useForVolume = mUseForVolume; |
Ytai Ben-Tsvi | 0cf9265 | 2020-11-23 13:23:00 -0800 | [diff] [blame] | 142 | parcelable->mode = VALUE_OR_RETURN_STATUS( |
Andy Hung | 973638a | 2020-12-08 20:47:45 -0800 | [diff] [blame] | 143 | legacy2aidl_audio_gain_mode_t_int32_t_mask(mGain.mode)); |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 144 | parcelable->channelMask = VALUE_OR_RETURN_STATUS( |
| 145 | legacy2aidl_audio_channel_mask_t_int32_t(mGain.channel_mask)); |
| 146 | parcelable->minValue = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(mGain.min_value)); |
| 147 | parcelable->maxValue = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(mGain.max_value)); |
| 148 | parcelable->defaultValue = VALUE_OR_RETURN_STATUS( |
| 149 | convertIntegral<int32_t>(mGain.default_value)); |
| 150 | parcelable->stepValue = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(mGain.step_value)); |
| 151 | parcelable->minRampMs = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(mGain.min_ramp_ms)); |
| 152 | parcelable->maxRampMs = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(mGain.max_ramp_ms)); |
| 153 | return OK; |
| 154 | } |
| 155 | |
| 156 | status_t AudioGain::readFromParcel(const android::Parcel *parcel) { |
| 157 | media::AudioGain parcelable; |
| 158 | return parcelable.readFromParcel(parcel) |
| 159 | ?: readFromParcelable(parcelable); |
| 160 | } |
| 161 | |
| 162 | status_t AudioGain::readFromParcelable(const media::AudioGain& parcelable) { |
| 163 | mIndex = VALUE_OR_RETURN_STATUS(convertIntegral<int>(parcelable.index)); |
| 164 | mUseInChannelMask = parcelable.useInChannelMask; |
| 165 | mUseForVolume = parcelable.useForVolume; |
Ytai Ben-Tsvi | 0cf9265 | 2020-11-23 13:23:00 -0800 | [diff] [blame] | 166 | mGain.mode = VALUE_OR_RETURN_STATUS( |
| 167 | aidl2legacy_int32_t_audio_gain_mode_t_mask(parcelable.mode)); |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 168 | mGain.channel_mask = VALUE_OR_RETURN_STATUS( |
| 169 | aidl2legacy_int32_t_audio_channel_mask_t(parcelable.channelMask)); |
| 170 | mGain.min_value = VALUE_OR_RETURN_STATUS(convertIntegral<int>(parcelable.minValue)); |
| 171 | mGain.max_value = VALUE_OR_RETURN_STATUS(convertIntegral<int>(parcelable.maxValue)); |
| 172 | mGain.default_value = VALUE_OR_RETURN_STATUS(convertIntegral<int>(parcelable.defaultValue)); |
| 173 | mGain.step_value = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(parcelable.stepValue)); |
| 174 | mGain.min_ramp_ms = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(parcelable.minRampMs)); |
| 175 | mGain.max_ramp_ms = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(parcelable.maxRampMs)); |
| 176 | return OK; |
jiabin | 84e43c7 | 2019-07-31 17:23:17 -0700 | [diff] [blame] | 177 | } |
| 178 | |
jiabin | 49e69a1 | 2019-10-15 16:04:13 -0700 | [diff] [blame] | 179 | bool AudioGains::equals(const AudioGains &other) const |
| 180 | { |
| 181 | return std::equal(begin(), end(), other.begin(), other.end(), |
| 182 | [](const sp<AudioGain>& left, const sp<AudioGain>& right) { |
| 183 | return left->equals(right); |
| 184 | }); |
| 185 | } |
| 186 | |
jiabin | 84e43c7 | 2019-07-31 17:23:17 -0700 | [diff] [blame] | 187 | status_t AudioGains::writeToParcel(android::Parcel *parcel) const { |
| 188 | status_t status = NO_ERROR; |
jiabin | 17058fa | 2019-10-08 17:33:38 -0700 | [diff] [blame] | 189 | if ((status = parcel->writeVectorSize(*this)) != NO_ERROR) return status; |
jiabin | 84e43c7 | 2019-07-31 17:23:17 -0700 | [diff] [blame] | 190 | for (const auto &audioGain : *this) { |
| 191 | if ((status = parcel->writeParcelable(*audioGain)) != NO_ERROR) { |
| 192 | break; |
| 193 | } |
| 194 | } |
| 195 | return status; |
| 196 | } |
| 197 | |
| 198 | status_t AudioGains::readFromParcel(const android::Parcel *parcel) { |
| 199 | status_t status = NO_ERROR; |
jiabin | 17058fa | 2019-10-08 17:33:38 -0700 | [diff] [blame] | 200 | this->clear(); |
| 201 | if ((status = parcel->resizeOutVector(this)) != NO_ERROR) return status; |
| 202 | for (size_t i = 0; i < this->size(); i++) { |
| 203 | this->at(i) = new AudioGain(0, false); |
| 204 | if ((status = parcel->readParcelable(this->at(i).get())) != NO_ERROR) { |
jiabin | 84e43c7 | 2019-07-31 17:23:17 -0700 | [diff] [blame] | 205 | this->clear(); |
| 206 | break; |
| 207 | } |
jiabin | 84e43c7 | 2019-07-31 17:23:17 -0700 | [diff] [blame] | 208 | } |
| 209 | return status; |
| 210 | } |
| 211 | |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 212 | ConversionResult<sp<AudioGain>> |
| 213 | aidl2legacy_AudioGain(const media::AudioGain& aidl) { |
| 214 | sp<AudioGain> legacy = new AudioGain(0, false); |
| 215 | status_t status = legacy->readFromParcelable(aidl); |
| 216 | if (status != OK) { |
| 217 | return base::unexpected(status); |
| 218 | } |
| 219 | return legacy; |
| 220 | } |
| 221 | |
| 222 | ConversionResult<media::AudioGain> |
| 223 | legacy2aidl_AudioGain(const sp<AudioGain>& legacy) { |
| 224 | media::AudioGain aidl; |
| 225 | status_t status = legacy->writeToParcelable(&aidl); |
| 226 | if (status != OK) { |
| 227 | return base::unexpected(status); |
| 228 | } |
| 229 | return aidl; |
| 230 | } |
| 231 | |
| 232 | ConversionResult<AudioGains> |
| 233 | aidl2legacy_AudioGains(const std::vector<media::AudioGain>& aidl) { |
| 234 | return convertContainer<AudioGains>(aidl, aidl2legacy_AudioGain); |
| 235 | } |
| 236 | |
| 237 | ConversionResult<std::vector<media::AudioGain>> |
| 238 | legacy2aidl_AudioGains(const AudioGains& legacy) { |
| 239 | return convertContainer<std::vector<media::AudioGain>>(legacy, legacy2aidl_AudioGain); |
| 240 | } |
| 241 | |
Mikhail Naganov | 1b2a794 | 2017-12-08 10:18:09 -0800 | [diff] [blame] | 242 | } // namespace android |