Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | #ifndef ANDROID_VOLUME_SHAPER_H |
| 18 | #define ANDROID_VOLUME_SHAPER_H |
| 19 | |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 20 | #include <cmath> |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 21 | #include <list> |
| 22 | #include <math.h> |
| 23 | #include <sstream> |
| 24 | |
| 25 | #include <binder/Parcel.h> |
| 26 | #include <media/Interpolator.h> |
| 27 | #include <utils/Mutex.h> |
| 28 | #include <utils/RefBase.h> |
| 29 | |
| 30 | #pragma push_macro("LOG_TAG") |
| 31 | #undef LOG_TAG |
| 32 | #define LOG_TAG "VolumeShaper" |
| 33 | |
| 34 | // turn on VolumeShaper logging |
Colin Cross | 4e39999 | 2017-04-27 16:15:51 -0700 | [diff] [blame] | 35 | #define VS_LOGGING 0 |
| 36 | #define VS_LOG(...) ALOGD_IF(VS_LOGGING, __VA_ARGS__) |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 37 | |
| 38 | namespace android { |
| 39 | |
| 40 | // The native VolumeShaper class mirrors the java VolumeShaper class; |
| 41 | // in addition, the native class contains implementation for actual operation. |
| 42 | // |
| 43 | // VolumeShaper methods are not safe for multiple thread access. |
| 44 | // Use VolumeHandler for thread-safe encapsulation of multiple VolumeShapers. |
| 45 | // |
| 46 | // Classes below written are to avoid naked pointers so there are no |
| 47 | // explicit destructors required. |
| 48 | |
| 49 | class VolumeShaper { |
| 50 | public: |
| 51 | using S = float; |
| 52 | using T = float; |
| 53 | |
| 54 | static const int kSystemIdMax = 16; |
| 55 | |
| 56 | // VolumeShaper::Status is equivalent to status_t if negative |
| 57 | // but if non-negative represents the id operated on. |
| 58 | // It must be expressible as an int32_t for binder purposes. |
| 59 | using Status = status_t; |
| 60 | |
| 61 | class Configuration : public Interpolator<S, T>, public RefBase { |
| 62 | public: |
| 63 | /* VolumeShaper.Configuration derives from the Interpolator class and adds |
| 64 | * parameters relating to the volume shape. |
| 65 | */ |
| 66 | |
| 67 | // TODO document as per VolumeShaper.java flags. |
| 68 | |
| 69 | // must match with VolumeShaper.java in frameworks/base |
| 70 | enum Type : int32_t { |
| 71 | TYPE_ID, |
| 72 | TYPE_SCALE, |
| 73 | }; |
| 74 | |
| 75 | // must match with VolumeShaper.java in frameworks/base |
| 76 | enum OptionFlag : int32_t { |
| 77 | OPTION_FLAG_NONE = 0, |
| 78 | OPTION_FLAG_VOLUME_IN_DBFS = (1 << 0), |
| 79 | OPTION_FLAG_CLOCK_TIME = (1 << 1), |
| 80 | |
| 81 | OPTION_FLAG_ALL = (OPTION_FLAG_VOLUME_IN_DBFS | OPTION_FLAG_CLOCK_TIME), |
| 82 | }; |
| 83 | |
| 84 | // bring to derived class; must match with VolumeShaper.java in frameworks/base |
| 85 | using InterpolatorType = Interpolator<S, T>::InterpolatorType; |
| 86 | |
| 87 | Configuration() |
| 88 | : Interpolator<S, T>() |
Colin Cross | 11280a1 | 2017-05-02 10:32:56 -0700 | [diff] [blame^] | 89 | , RefBase() |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 90 | , mType(TYPE_SCALE) |
Colin Cross | 4e39999 | 2017-04-27 16:15:51 -0700 | [diff] [blame] | 91 | , mId(-1) |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 92 | , mOptionFlags(OPTION_FLAG_NONE) |
Colin Cross | 4e39999 | 2017-04-27 16:15:51 -0700 | [diff] [blame] | 93 | , mDurationMs(1000.) { |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Andy Hung | 7d712bb | 2017-04-20 14:23:41 -0700 | [diff] [blame] | 96 | explicit Configuration(const Configuration &configuration) |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 97 | : Interpolator<S, T>(*static_cast<const Interpolator<S, T> *>(&configuration)) |
Colin Cross | 11280a1 | 2017-05-02 10:32:56 -0700 | [diff] [blame^] | 98 | , RefBase() |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 99 | , mType(configuration.mType) |
Colin Cross | 4e39999 | 2017-04-27 16:15:51 -0700 | [diff] [blame] | 100 | , mId(configuration.mId) |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 101 | , mOptionFlags(configuration.mOptionFlags) |
Colin Cross | 4e39999 | 2017-04-27 16:15:51 -0700 | [diff] [blame] | 102 | , mDurationMs(configuration.mDurationMs) { |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 105 | Type getType() const { |
| 106 | return mType; |
| 107 | } |
| 108 | |
| 109 | status_t setType(Type type) { |
| 110 | switch (type) { |
| 111 | case TYPE_ID: |
| 112 | case TYPE_SCALE: |
| 113 | mType = type; |
| 114 | return NO_ERROR; |
| 115 | default: |
| 116 | ALOGE("invalid Type: %d", type); |
| 117 | return BAD_VALUE; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | OptionFlag getOptionFlags() const { |
| 122 | return mOptionFlags; |
| 123 | } |
| 124 | |
| 125 | status_t setOptionFlags(OptionFlag optionFlags) { |
| 126 | if ((optionFlags & ~OPTION_FLAG_ALL) != 0) { |
| 127 | ALOGE("optionFlags has invalid bits: %#x", optionFlags); |
| 128 | return BAD_VALUE; |
| 129 | } |
| 130 | mOptionFlags = optionFlags; |
| 131 | return NO_ERROR; |
| 132 | } |
| 133 | |
| 134 | double getDurationMs() const { |
| 135 | return mDurationMs; |
| 136 | } |
| 137 | |
| 138 | void setDurationMs(double durationMs) { |
| 139 | mDurationMs = durationMs; |
| 140 | } |
| 141 | |
| 142 | int32_t getId() const { |
| 143 | return mId; |
| 144 | } |
| 145 | |
| 146 | void setId(int32_t id) { |
| 147 | mId = id; |
| 148 | } |
| 149 | |
| 150 | T adjustVolume(T volume) const { |
| 151 | if ((getOptionFlags() & OPTION_FLAG_VOLUME_IN_DBFS) != 0) { |
Colin Cross | 11280a1 | 2017-05-02 10:32:56 -0700 | [diff] [blame^] | 152 | const T out = powf(10.f, volume / 10.0f); |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 153 | VS_LOG("in: %f out: %f", volume, out); |
| 154 | volume = out; |
| 155 | } |
| 156 | // clamp |
| 157 | if (volume < 0.f) { |
| 158 | volume = 0.f; |
| 159 | } else if (volume > 1.f) { |
| 160 | volume = 1.f; |
| 161 | } |
| 162 | return volume; |
| 163 | } |
| 164 | |
| 165 | status_t checkCurve() { |
| 166 | if (mType == TYPE_ID) return NO_ERROR; |
| 167 | if (this->size() < 2) { |
| 168 | ALOGE("curve must have at least 2 points"); |
| 169 | return BAD_VALUE; |
| 170 | } |
| 171 | if (first().first != 0.f || last().first != 1.f) { |
| 172 | ALOGE("curve must start at 0.f and end at 1.f"); |
| 173 | return BAD_VALUE; |
| 174 | } |
| 175 | if ((getOptionFlags() & OPTION_FLAG_VOLUME_IN_DBFS) != 0) { |
| 176 | for (const auto &pt : *this) { |
| 177 | if (!(pt.second <= 0.f) /* handle nan */) { |
| 178 | ALOGE("positive volume dbFS"); |
| 179 | return BAD_VALUE; |
| 180 | } |
| 181 | } |
| 182 | } else { |
| 183 | for (const auto &pt : *this) { |
| 184 | if (!(pt.second >= 0.f) || !(pt.second <= 1.f) /* handle nan */) { |
| 185 | ALOGE("volume < 0.f or > 1.f"); |
| 186 | return BAD_VALUE; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | return NO_ERROR; |
| 191 | } |
| 192 | |
| 193 | void clampVolume() { |
| 194 | if ((mOptionFlags & OPTION_FLAG_VOLUME_IN_DBFS) != 0) { |
| 195 | for (auto it = this->begin(); it != this->end(); ++it) { |
| 196 | if (!(it->second <= 0.f) /* handle nan */) { |
| 197 | it->second = 0.f; |
| 198 | } |
| 199 | } |
| 200 | } else { |
| 201 | for (auto it = this->begin(); it != this->end(); ++it) { |
| 202 | if (!(it->second >= 0.f) /* handle nan */) { |
| 203 | it->second = 0.f; |
| 204 | } else if (!(it->second <= 1.f)) { |
| 205 | it->second = 1.f; |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /* scaleToStartVolume() is used to set the start volume of a |
| 212 | * new VolumeShaper curve, when replacing one VolumeShaper |
| 213 | * with another using the "join" (volume match) option. |
| 214 | * |
| 215 | * It works best for monotonic volume ramps or ducks. |
| 216 | */ |
| 217 | void scaleToStartVolume(T volume) { |
| 218 | if (this->size() < 2) { |
| 219 | return; |
| 220 | } |
| 221 | const T startVolume = first().second; |
| 222 | const T endVolume = last().second; |
| 223 | if (endVolume == startVolume) { |
| 224 | // match with linear ramp |
| 225 | const T offset = volume - startVolume; |
| 226 | for (auto it = this->begin(); it != this->end(); ++it) { |
| 227 | it->second = it->second + offset * (1.f - it->first); |
| 228 | } |
| 229 | } else { |
| 230 | const T scale = (volume - endVolume) / (startVolume - endVolume); |
| 231 | for (auto it = this->begin(); it != this->end(); ++it) { |
| 232 | it->second = scale * (it->second - endVolume) + endVolume; |
| 233 | } |
| 234 | } |
| 235 | clampVolume(); |
| 236 | } |
| 237 | |
Andy Hung | 7d712bb | 2017-04-20 14:23:41 -0700 | [diff] [blame] | 238 | // The parcel layout must match VolumeShaper.java |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 239 | status_t writeToParcel(Parcel *parcel) const { |
| 240 | if (parcel == nullptr) return BAD_VALUE; |
| 241 | return parcel->writeInt32((int32_t)mType) |
| 242 | ?: parcel->writeInt32(mId) |
| 243 | ?: mType == TYPE_ID |
| 244 | ? NO_ERROR |
| 245 | : parcel->writeInt32((int32_t)mOptionFlags) |
| 246 | ?: parcel->writeDouble(mDurationMs) |
| 247 | ?: Interpolator<S, T>::writeToParcel(parcel); |
| 248 | } |
| 249 | |
| 250 | status_t readFromParcel(const Parcel &parcel) { |
| 251 | int32_t type, optionFlags; |
| 252 | return parcel.readInt32(&type) |
| 253 | ?: setType((Type)type) |
| 254 | ?: parcel.readInt32(&mId) |
| 255 | ?: mType == TYPE_ID |
| 256 | ? NO_ERROR |
| 257 | : parcel.readInt32(&optionFlags) |
| 258 | ?: setOptionFlags((OptionFlag)optionFlags) |
| 259 | ?: parcel.readDouble(&mDurationMs) |
| 260 | ?: Interpolator<S, T>::readFromParcel(parcel) |
| 261 | ?: checkCurve(); |
| 262 | } |
| 263 | |
| 264 | std::string toString() const { |
| 265 | std::stringstream ss; |
Colin Cross | 11280a1 | 2017-05-02 10:32:56 -0700 | [diff] [blame^] | 266 | ss << "mType: " << static_cast<int32_t>(mType) << std::endl; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 267 | ss << "mId: " << mId << std::endl; |
| 268 | if (mType != TYPE_ID) { |
Colin Cross | 11280a1 | 2017-05-02 10:32:56 -0700 | [diff] [blame^] | 269 | ss << "mOptionFlags: " << static_cast<int32_t>(mOptionFlags) << std::endl; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 270 | ss << "mDurationMs: " << mDurationMs << std::endl; |
| 271 | ss << Interpolator<S, T>::toString().c_str(); |
| 272 | } |
| 273 | return ss.str(); |
| 274 | } |
| 275 | |
| 276 | private: |
| 277 | Type mType; |
| 278 | int32_t mId; |
| 279 | OptionFlag mOptionFlags; |
| 280 | double mDurationMs; |
| 281 | }; // Configuration |
| 282 | |
| 283 | // must match with VolumeShaper.java in frameworks/base |
| 284 | // TODO document per VolumeShaper.java flags. |
| 285 | class Operation : public RefBase { |
| 286 | public: |
| 287 | enum Flag : int32_t { |
| 288 | FLAG_NONE = 0, |
| 289 | FLAG_REVERSE = (1 << 0), |
| 290 | FLAG_TERMINATE = (1 << 1), |
| 291 | FLAG_JOIN = (1 << 2), |
| 292 | FLAG_DELAY = (1 << 3), |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 293 | FLAG_CREATE_IF_NECESSARY = (1 << 4), |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 294 | |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 295 | FLAG_ALL = (FLAG_REVERSE | FLAG_TERMINATE | FLAG_JOIN | FLAG_DELAY |
| 296 | | FLAG_CREATE_IF_NECESSARY), |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 297 | }; |
| 298 | |
| 299 | Operation() |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 300 | : Operation(FLAG_NONE, -1 /* replaceId */) { |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 301 | } |
| 302 | |
Andy Hung | 7d712bb | 2017-04-20 14:23:41 -0700 | [diff] [blame] | 303 | Operation(Flag flags, int replaceId) |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 304 | : Operation(flags, replaceId, std::numeric_limits<S>::quiet_NaN() /* xOffset */) { |
| 305 | } |
| 306 | |
Andy Hung | 7d712bb | 2017-04-20 14:23:41 -0700 | [diff] [blame] | 307 | explicit Operation(const Operation &operation) |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 308 | : Operation(operation.mFlags, operation.mReplaceId, operation.mXOffset) { |
| 309 | } |
| 310 | |
Andy Hung | 7d712bb | 2017-04-20 14:23:41 -0700 | [diff] [blame] | 311 | explicit Operation(const sp<Operation> &operation) |
| 312 | : Operation(*operation.get()) { |
| 313 | } |
| 314 | |
| 315 | Operation(Flag flags, int replaceId, S xOffset) |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 316 | : mFlags(flags) |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 317 | , mReplaceId(replaceId) |
| 318 | , mXOffset(xOffset) { |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | int32_t getReplaceId() const { |
| 322 | return mReplaceId; |
| 323 | } |
| 324 | |
| 325 | void setReplaceId(int32_t replaceId) { |
| 326 | mReplaceId = replaceId; |
| 327 | } |
| 328 | |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 329 | S getXOffset() const { |
| 330 | return mXOffset; |
| 331 | } |
| 332 | |
| 333 | void setXOffset(S xOffset) { |
| 334 | mXOffset = xOffset; |
| 335 | } |
| 336 | |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 337 | Flag getFlags() const { |
| 338 | return mFlags; |
| 339 | } |
| 340 | |
| 341 | status_t setFlags(Flag flags) { |
| 342 | if ((flags & ~FLAG_ALL) != 0) { |
| 343 | ALOGE("flags has invalid bits: %#x", flags); |
| 344 | return BAD_VALUE; |
| 345 | } |
| 346 | mFlags = flags; |
| 347 | return NO_ERROR; |
| 348 | } |
| 349 | |
| 350 | status_t writeToParcel(Parcel *parcel) const { |
| 351 | if (parcel == nullptr) return BAD_VALUE; |
| 352 | return parcel->writeInt32((int32_t)mFlags) |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 353 | ?: parcel->writeInt32(mReplaceId) |
| 354 | ?: parcel->writeFloat(mXOffset); |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | status_t readFromParcel(const Parcel &parcel) { |
| 358 | int32_t flags; |
| 359 | return parcel.readInt32(&flags) |
| 360 | ?: parcel.readInt32(&mReplaceId) |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 361 | ?: parcel.readFloat(&mXOffset) |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 362 | ?: setFlags((Flag)flags); |
| 363 | } |
| 364 | |
| 365 | std::string toString() const { |
| 366 | std::stringstream ss; |
Colin Cross | 11280a1 | 2017-05-02 10:32:56 -0700 | [diff] [blame^] | 367 | ss << "mFlags: " << static_cast<int32_t>(mFlags) << std::endl; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 368 | ss << "mReplaceId: " << mReplaceId << std::endl; |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 369 | ss << "mXOffset: " << mXOffset << std::endl; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 370 | return ss.str(); |
| 371 | } |
| 372 | |
| 373 | private: |
| 374 | Flag mFlags; |
| 375 | int32_t mReplaceId; |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 376 | S mXOffset; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 377 | }; // Operation |
| 378 | |
| 379 | // must match with VolumeShaper.java in frameworks/base |
| 380 | class State : public RefBase { |
| 381 | public: |
Andy Hung | 7d712bb | 2017-04-20 14:23:41 -0700 | [diff] [blame] | 382 | State(T volume, S xOffset) |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 383 | : mVolume(volume) |
| 384 | , mXOffset(xOffset) { |
| 385 | } |
| 386 | |
| 387 | State() |
| 388 | : State(-1.f, -1.f) { } |
| 389 | |
| 390 | T getVolume() const { |
| 391 | return mVolume; |
| 392 | } |
| 393 | |
| 394 | void setVolume(T volume) { |
| 395 | mVolume = volume; |
| 396 | } |
| 397 | |
| 398 | S getXOffset() const { |
| 399 | return mXOffset; |
| 400 | } |
| 401 | |
| 402 | void setXOffset(S xOffset) { |
| 403 | mXOffset = xOffset; |
| 404 | } |
| 405 | |
| 406 | status_t writeToParcel(Parcel *parcel) const { |
| 407 | if (parcel == nullptr) return BAD_VALUE; |
| 408 | return parcel->writeFloat(mVolume) |
| 409 | ?: parcel->writeFloat(mXOffset); |
| 410 | } |
| 411 | |
| 412 | status_t readFromParcel(const Parcel &parcel) { |
| 413 | return parcel.readFloat(&mVolume) |
| 414 | ?: parcel.readFloat(&mXOffset); |
| 415 | } |
| 416 | |
| 417 | std::string toString() const { |
| 418 | std::stringstream ss; |
| 419 | ss << "mVolume: " << mVolume << std::endl; |
| 420 | ss << "mXOffset: " << mXOffset << std::endl; |
| 421 | return ss.str(); |
| 422 | } |
| 423 | |
| 424 | private: |
| 425 | T mVolume; |
| 426 | S mXOffset; |
| 427 | }; // State |
| 428 | |
| 429 | template <typename R> |
| 430 | class Translate { |
| 431 | public: |
| 432 | Translate() |
| 433 | : mOffset(0) |
| 434 | , mScale(1) { |
| 435 | } |
| 436 | |
| 437 | R getOffset() const { |
| 438 | return mOffset; |
| 439 | } |
| 440 | |
| 441 | void setOffset(R offset) { |
| 442 | mOffset = offset; |
| 443 | } |
| 444 | |
| 445 | R getScale() const { |
| 446 | return mScale; |
| 447 | } |
| 448 | |
| 449 | void setScale(R scale) { |
| 450 | mScale = scale; |
| 451 | } |
| 452 | |
| 453 | R operator()(R in) const { |
| 454 | return mScale * (in - mOffset); |
| 455 | } |
| 456 | |
| 457 | std::string toString() const { |
| 458 | std::stringstream ss; |
| 459 | ss << "mOffset: " << mOffset << std::endl; |
| 460 | ss << "mScale: " << mScale << std::endl; |
| 461 | return ss.str(); |
| 462 | } |
| 463 | |
| 464 | private: |
| 465 | R mOffset; |
| 466 | R mScale; |
| 467 | }; // Translate |
| 468 | |
| 469 | static int64_t convertTimespecToUs(const struct timespec &tv) |
| 470 | { |
| 471 | return tv.tv_sec * 1000000ll + tv.tv_nsec / 1000; |
| 472 | } |
| 473 | |
| 474 | // current monotonic time in microseconds. |
| 475 | static int64_t getNowUs() |
| 476 | { |
| 477 | struct timespec tv; |
| 478 | if (clock_gettime(CLOCK_MONOTONIC, &tv) != 0) { |
| 479 | return 0; // system is really sick, just return 0 for consistency. |
| 480 | } |
| 481 | return convertTimespecToUs(tv); |
| 482 | } |
| 483 | |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 484 | // TODO: Since we pass configuration and operation as shared pointers |
| 485 | // there is a potential risk that the caller may modify these after |
| 486 | // delivery. Currently, we don't require copies made here. |
Andy Hung | 7d712bb | 2017-04-20 14:23:41 -0700 | [diff] [blame] | 487 | VolumeShaper( |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 488 | const sp<VolumeShaper::Configuration> &configuration, |
| 489 | const sp<VolumeShaper::Operation> &operation) |
| 490 | : mConfiguration(configuration) // we do not make a copy |
| 491 | , mOperation(operation) // ditto |
| 492 | , mStartFrame(-1) |
| 493 | , mLastVolume(T(1)) |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 494 | , mLastXOffset(0.f) |
| 495 | , mDelayXOffset(std::numeric_limits<S>::quiet_NaN()) { |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 496 | if (configuration.get() != nullptr |
| 497 | && (getFlags() & VolumeShaper::Operation::FLAG_DELAY) == 0) { |
| 498 | mLastVolume = configuration->first().second; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | void updatePosition(int64_t startFrame, double sampleRate) { |
| 503 | double scale = (mConfiguration->last().first - mConfiguration->first().first) |
| 504 | / (mConfiguration->getDurationMs() * 0.001 * sampleRate); |
Colin Cross | 11280a1 | 2017-05-02 10:32:56 -0700 | [diff] [blame^] | 505 | const double minScale = 1. / static_cast<double>(INT64_MAX); |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 506 | scale = std::max(scale, minScale); |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 507 | const S xOffset = std::isnan(mDelayXOffset) ? mConfiguration->first().first : mDelayXOffset; |
| 508 | VS_LOG("update position: scale %lf frameCount:%lld, sampleRate:%lf, xOffset:%f", |
| 509 | scale, (long long) startFrame, sampleRate, xOffset); |
| 510 | |
Colin Cross | 11280a1 | 2017-05-02 10:32:56 -0700 | [diff] [blame^] | 511 | mXTranslate.setOffset(static_cast<float>(static_cast<double>(startFrame) |
| 512 | - static_cast<double>(xOffset) / scale)); |
| 513 | mXTranslate.setScale(static_cast<float>(scale)); |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 514 | VS_LOG("translate: %s", mXTranslate.toString().c_str()); |
| 515 | } |
| 516 | |
| 517 | // We allow a null operation here, though VolumeHandler always provides one. |
| 518 | VolumeShaper::Operation::Flag getFlags() const { |
| 519 | return mOperation == nullptr |
| 520 | ? VolumeShaper::Operation::FLAG_NONE :mOperation->getFlags(); |
| 521 | } |
| 522 | |
| 523 | sp<VolumeShaper::State> getState() const { |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 524 | return new VolumeShaper::State(mLastVolume, mLastXOffset); |
| 525 | } |
| 526 | |
| 527 | void setDelayXOffset(S xOffset) { |
| 528 | mDelayXOffset = xOffset; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 529 | } |
| 530 | |
Andy Hung | 39399b6 | 2017-04-21 15:07:45 -0700 | [diff] [blame] | 531 | bool isStarted() const { |
| 532 | return mStartFrame >= 0; |
| 533 | } |
| 534 | |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 535 | std::pair<T /* volume */, bool /* active */> getVolume( |
| 536 | int64_t trackFrameCount, double trackSampleRate) { |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 537 | if ((getFlags() & VolumeShaper::Operation::FLAG_DELAY) != 0) { |
| 538 | VS_LOG("delayed VolumeShaper, ignoring"); |
| 539 | mLastVolume = T(1); |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 540 | mLastXOffset = 0.; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 541 | return std::make_pair(T(1), false); |
| 542 | } |
| 543 | const bool clockTime = (mConfiguration->getOptionFlags() |
| 544 | & VolumeShaper::Configuration::OPTION_FLAG_CLOCK_TIME) != 0; |
| 545 | const int64_t frameCount = clockTime ? getNowUs() : trackFrameCount; |
| 546 | const double sampleRate = clockTime ? 1000000 : trackSampleRate; |
| 547 | |
| 548 | if (mStartFrame < 0) { |
| 549 | updatePosition(frameCount, sampleRate); |
| 550 | mStartFrame = frameCount; |
| 551 | } |
| 552 | VS_LOG("frameCount: %lld", (long long)frameCount); |
| 553 | S x = mXTranslate((T)frameCount); |
| 554 | VS_LOG("translation: %f", x); |
| 555 | |
| 556 | // handle reversal of position |
| 557 | if (getFlags() & VolumeShaper::Operation::FLAG_REVERSE) { |
| 558 | x = 1.f - x; |
| 559 | VS_LOG("reversing to %f", x); |
| 560 | if (x < mConfiguration->first().first) { |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 561 | mLastXOffset = 1.f; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 562 | const T volume = mConfiguration->adjustVolume( |
| 563 | mConfiguration->first().second); // persist last value |
| 564 | VS_LOG("persisting volume %f", volume); |
| 565 | mLastVolume = volume; |
| 566 | return std::make_pair(volume, false); |
| 567 | } |
| 568 | if (x > mConfiguration->last().first) { |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 569 | mLastXOffset = 0.f; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 570 | mLastVolume = 1.f; |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 571 | return std::make_pair(T(1), true); // too early |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 572 | } |
| 573 | } else { |
| 574 | if (x < mConfiguration->first().first) { |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 575 | mLastXOffset = 0.f; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 576 | mLastVolume = 1.f; |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 577 | return std::make_pair(T(1), true); // too early |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 578 | } |
| 579 | if (x > mConfiguration->last().first) { |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 580 | mLastXOffset = 1.f; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 581 | const T volume = mConfiguration->adjustVolume( |
| 582 | mConfiguration->last().second); // persist last value |
| 583 | VS_LOG("persisting volume %f", volume); |
| 584 | mLastVolume = volume; |
| 585 | return std::make_pair(volume, false); |
| 586 | } |
| 587 | } |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 588 | mLastXOffset = x; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 589 | // x contains the location on the volume curve to use. |
| 590 | const T unscaledVolume = mConfiguration->findY(x); |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 591 | const T volume = mConfiguration->adjustVolume(unscaledVolume); // handle log scale |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 592 | VS_LOG("volume: %f unscaled: %f", volume, unscaledVolume); |
| 593 | mLastVolume = volume; |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 594 | return std::make_pair(volume, true); |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | std::string toString() const { |
| 598 | std::stringstream ss; |
| 599 | ss << "StartFrame: " << mStartFrame << std::endl; |
| 600 | ss << mXTranslate.toString().c_str(); |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 601 | if (mConfiguration.get() == nullptr) { |
| 602 | ss << "VolumeShaper::Configuration: nullptr" << std::endl; |
| 603 | } else { |
| 604 | ss << "VolumeShaper::Configuration:" << std::endl; |
| 605 | ss << mConfiguration->toString().c_str(); |
| 606 | } |
| 607 | if (mOperation.get() == nullptr) { |
| 608 | ss << "VolumeShaper::Operation: nullptr" << std::endl; |
| 609 | } else { |
| 610 | ss << "VolumeShaper::Operation:" << std::endl; |
| 611 | ss << mOperation->toString().c_str(); |
| 612 | } |
| 613 | return ss.str(); |
| 614 | } |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 615 | |
| 616 | Translate<S> mXTranslate; // x axis translation from frames (in usec for clock time) |
| 617 | sp<VolumeShaper::Configuration> mConfiguration; |
| 618 | sp<VolumeShaper::Operation> mOperation; |
| 619 | int64_t mStartFrame; // starting frame, non-negative when started (in usec for clock time) |
| 620 | T mLastVolume; // last computed interpolated volume (y-axis) |
| 621 | S mLastXOffset; // last computed interpolated xOffset/time (x-axis) |
| 622 | S mDelayXOffset; // delay xOffset on first volumeshaper start. |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 623 | }; // VolumeShaper |
| 624 | |
| 625 | // VolumeHandler combines the volume factors of multiple VolumeShapers and handles |
| 626 | // multiple thread access by synchronizing all public methods. |
| 627 | class VolumeHandler : public RefBase { |
| 628 | public: |
| 629 | using S = float; |
| 630 | using T = float; |
| 631 | |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 632 | // A volume handler which just keeps track of active VolumeShapers does not need sampleRate. |
| 633 | VolumeHandler() |
| 634 | : VolumeHandler(0 /* sampleRate */) { |
| 635 | } |
| 636 | |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 637 | explicit VolumeHandler(uint32_t sampleRate) |
| 638 | : mSampleRate((double)sampleRate) |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 639 | , mLastFrame(0) |
Andy Hung | da540db | 2017-04-20 14:06:17 -0700 | [diff] [blame] | 640 | , mVolumeShaperIdCounter(VolumeShaper::kSystemIdMax) |
| 641 | , mLastVolume(1.f, false) { |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | VolumeShaper::Status applyVolumeShaper( |
| 645 | const sp<VolumeShaper::Configuration> &configuration, |
| 646 | const sp<VolumeShaper::Operation> &operation) { |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 647 | VS_LOG("applyVolumeShaper:configuration: %s", configuration->toString().c_str()); |
| 648 | VS_LOG("applyVolumeShaper:operation: %s", operation->toString().c_str()); |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 649 | AutoMutex _l(mLock); |
| 650 | if (configuration == nullptr) { |
| 651 | ALOGE("null configuration"); |
| 652 | return VolumeShaper::Status(BAD_VALUE); |
| 653 | } |
| 654 | if (operation == nullptr) { |
| 655 | ALOGE("null operation"); |
| 656 | return VolumeShaper::Status(BAD_VALUE); |
| 657 | } |
| 658 | const int32_t id = configuration->getId(); |
| 659 | if (id < 0) { |
| 660 | ALOGE("negative id: %d", id); |
| 661 | return VolumeShaper::Status(BAD_VALUE); |
| 662 | } |
| 663 | VS_LOG("applyVolumeShaper id: %d", id); |
| 664 | |
| 665 | switch (configuration->getType()) { |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 666 | case VolumeShaper::Configuration::TYPE_SCALE: { |
| 667 | const int replaceId = operation->getReplaceId(); |
| 668 | if (replaceId >= 0) { |
| 669 | auto replaceIt = findId_l(replaceId); |
| 670 | if (replaceIt == mVolumeShapers.end()) { |
| 671 | ALOGW("cannot find replace id: %d", replaceId); |
| 672 | } else { |
| 673 | if ((replaceIt->getFlags() & VolumeShaper::Operation::FLAG_JOIN) != 0) { |
| 674 | // For join, we scale the start volume of the current configuration |
| 675 | // to match the last-used volume of the replacing VolumeShaper. |
| 676 | auto state = replaceIt->getState(); |
| 677 | if (state->getXOffset() >= 0) { // valid |
| 678 | const T volume = state->getVolume(); |
| 679 | ALOGD("join: scaling start volume to %f", volume); |
| 680 | configuration->scaleToStartVolume(volume); |
| 681 | } |
| 682 | } |
| 683 | (void)mVolumeShapers.erase(replaceIt); |
| 684 | } |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 685 | operation->setReplaceId(-1); |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 686 | } |
| 687 | // check if we have another of the same id. |
| 688 | auto oldIt = findId_l(id); |
| 689 | if (oldIt != mVolumeShapers.end()) { |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 690 | if ((operation->getFlags() |
| 691 | & VolumeShaper::Operation::FLAG_CREATE_IF_NECESSARY) != 0) { |
| 692 | // TODO: move the case to a separate function. |
| 693 | goto HANDLE_TYPE_ID; // no need to create, take over existing id. |
| 694 | } |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 695 | ALOGW("duplicate id, removing old %d", id); |
| 696 | (void)mVolumeShapers.erase(oldIt); |
| 697 | } |
| 698 | // create new VolumeShaper |
| 699 | mVolumeShapers.emplace_back(configuration, operation); |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 700 | } |
| 701 | // fall through to handle the operation |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 702 | HANDLE_TYPE_ID: |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 703 | case VolumeShaper::Configuration::TYPE_ID: { |
| 704 | VS_LOG("trying to find id: %d", id); |
| 705 | auto it = findId_l(id); |
| 706 | if (it == mVolumeShapers.end()) { |
| 707 | VS_LOG("couldn't find id: %d", id); |
| 708 | return VolumeShaper::Status(INVALID_OPERATION); |
| 709 | } |
| 710 | if ((it->getFlags() & VolumeShaper::Operation::FLAG_TERMINATE) != 0) { |
| 711 | VS_LOG("terminate id: %d", id); |
| 712 | mVolumeShapers.erase(it); |
| 713 | break; |
| 714 | } |
| 715 | const bool clockTime = (it->mConfiguration->getOptionFlags() |
| 716 | & VolumeShaper::Configuration::OPTION_FLAG_CLOCK_TIME) != 0; |
| 717 | if ((it->getFlags() & VolumeShaper::Operation::FLAG_REVERSE) != |
| 718 | (operation->getFlags() & VolumeShaper::Operation::FLAG_REVERSE)) { |
| 719 | const int64_t frameCount = clockTime ? VolumeShaper::getNowUs() : mLastFrame; |
| 720 | const S x = it->mXTranslate((T)frameCount); |
| 721 | VS_LOG("reverse translation: %f", x); |
| 722 | // reflect position |
| 723 | S target = 1.f - x; |
| 724 | if (target < it->mConfiguration->first().first) { |
| 725 | VS_LOG("clamp to start - begin immediately"); |
| 726 | target = 0.; |
| 727 | } |
| 728 | VS_LOG("target reverse: %f", target); |
| 729 | it->mXTranslate.setOffset(it->mXTranslate.getOffset() |
| 730 | + (x - target) / it->mXTranslate.getScale()); |
| 731 | } |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 732 | const S xOffset = operation->getXOffset(); |
| 733 | if (!std::isnan(xOffset)) { |
| 734 | const int64_t frameCount = clockTime ? VolumeShaper::getNowUs() : mLastFrame; |
| 735 | const S x = it->mXTranslate((T)frameCount); |
| 736 | VS_LOG("xOffset translation: %f", x); |
| 737 | const S target = xOffset; // offset |
| 738 | VS_LOG("xOffset target x offset: %f", target); |
| 739 | it->mXTranslate.setOffset(it->mXTranslate.getOffset() |
| 740 | + (x - target) / it->mXTranslate.getScale()); |
| 741 | it->setDelayXOffset(xOffset); |
| 742 | } |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 743 | it->mOperation = operation; // replace the operation |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 744 | } break; |
| 745 | } |
| 746 | return VolumeShaper::Status(id); |
| 747 | } |
| 748 | |
| 749 | sp<VolumeShaper::State> getVolumeShaperState(int id) { |
| 750 | AutoMutex _l(mLock); |
| 751 | auto it = findId_l(id); |
| 752 | if (it == mVolumeShapers.end()) { |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 753 | VS_LOG("cannot find state for id: %d", id); |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 754 | return nullptr; |
| 755 | } |
| 756 | return it->getState(); |
| 757 | } |
| 758 | |
Andy Hung | 39399b6 | 2017-04-21 15:07:45 -0700 | [diff] [blame] | 759 | // getVolume() is not const, as it updates internal state. |
| 760 | // Once called, any VolumeShapers not already started begin running. |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 761 | std::pair<T /* volume */, bool /* active */> getVolume(int64_t trackFrameCount) { |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 762 | AutoMutex _l(mLock); |
| 763 | mLastFrame = trackFrameCount; |
| 764 | T volume(1); |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 765 | size_t activeCount = 0; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 766 | for (auto it = mVolumeShapers.begin(); it != mVolumeShapers.end();) { |
| 767 | std::pair<T, bool> shaperVolume = |
| 768 | it->getVolume(trackFrameCount, mSampleRate); |
| 769 | volume *= shaperVolume.first; |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 770 | activeCount += shaperVolume.second; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 771 | ++it; |
| 772 | } |
Andy Hung | da540db | 2017-04-20 14:06:17 -0700 | [diff] [blame] | 773 | mLastVolume = std::make_pair(volume, activeCount != 0); |
| 774 | return mLastVolume; |
| 775 | } |
| 776 | |
Andy Hung | 39399b6 | 2017-04-21 15:07:45 -0700 | [diff] [blame] | 777 | // Used by a client side VolumeHandler to ensure all the VolumeShapers |
| 778 | // indicate that they have been started. Upon a change in audioserver |
| 779 | // output sink, this information is used for restoration of the server side |
| 780 | // VolumeHandler. |
| 781 | void setStarted() { |
| 782 | (void)getVolume(mLastFrame); // getVolume() will start the individual VolumeShapers. |
| 783 | } |
| 784 | |
Andy Hung | da540db | 2017-04-20 14:06:17 -0700 | [diff] [blame] | 785 | std::pair<T /* volume */, bool /* active */> getLastVolume() const { |
| 786 | AutoMutex _l(mLock); |
| 787 | return mLastVolume; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | std::string toString() const { |
| 791 | AutoMutex _l(mLock); |
| 792 | std::stringstream ss; |
| 793 | ss << "mSampleRate: " << mSampleRate << std::endl; |
| 794 | ss << "mLastFrame: " << mLastFrame << std::endl; |
| 795 | for (const auto &shaper : mVolumeShapers) { |
| 796 | ss << shaper.toString().c_str(); |
| 797 | } |
| 798 | return ss.str(); |
| 799 | } |
| 800 | |
Andy Hung | 39399b6 | 2017-04-21 15:07:45 -0700 | [diff] [blame] | 801 | void forall(const std::function<VolumeShaper::Status (const VolumeShaper &)> &lambda) { |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 802 | AutoMutex _l(mLock); |
Andy Hung | da540db | 2017-04-20 14:06:17 -0700 | [diff] [blame] | 803 | VS_LOG("forall: mVolumeShapers.size() %zu", mVolumeShapers.size()); |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 804 | for (const auto &shaper : mVolumeShapers) { |
Andy Hung | 39399b6 | 2017-04-21 15:07:45 -0700 | [diff] [blame] | 805 | VolumeShaper::Status status = lambda(shaper); |
| 806 | VS_LOG("forall applying lambda on shaper (%p): %d", &shaper, (int)status); |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 807 | } |
| 808 | } |
| 809 | |
| 810 | void reset() { |
| 811 | AutoMutex _l(mLock); |
| 812 | mVolumeShapers.clear(); |
Andy Hung | 7d712bb | 2017-04-20 14:23:41 -0700 | [diff] [blame] | 813 | mLastFrame = 0; |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 814 | // keep mVolumeShaperIdCounter as is. |
| 815 | } |
| 816 | |
| 817 | // Sets the configuration id if necessary - This is based on the counter |
| 818 | // internal to the VolumeHandler. |
| 819 | void setIdIfNecessary(const sp<VolumeShaper::Configuration> &configuration) { |
| 820 | if (configuration->getType() == VolumeShaper::Configuration::TYPE_SCALE) { |
| 821 | const int id = configuration->getId(); |
| 822 | if (id == -1) { |
| 823 | // Reassign to a unique id, skipping system ids. |
| 824 | AutoMutex _l(mLock); |
| 825 | while (true) { |
| 826 | if (mVolumeShaperIdCounter == INT32_MAX) { |
| 827 | mVolumeShaperIdCounter = VolumeShaper::kSystemIdMax; |
| 828 | } else { |
| 829 | ++mVolumeShaperIdCounter; |
| 830 | } |
| 831 | if (findId_l(mVolumeShaperIdCounter) != mVolumeShapers.end()) { |
| 832 | continue; // collision with an existing id. |
| 833 | } |
| 834 | configuration->setId(mVolumeShaperIdCounter); |
| 835 | ALOGD("setting id to %d", mVolumeShaperIdCounter); |
| 836 | break; |
| 837 | } |
| 838 | } |
| 839 | } |
| 840 | } |
| 841 | |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 842 | private: |
| 843 | std::list<VolumeShaper>::iterator findId_l(int32_t id) { |
| 844 | std::list<VolumeShaper>::iterator it = mVolumeShapers.begin(); |
| 845 | for (; it != mVolumeShapers.end(); ++it) { |
| 846 | if (it->mConfiguration->getId() == id) { |
| 847 | break; |
| 848 | } |
| 849 | } |
| 850 | return it; |
| 851 | } |
| 852 | |
| 853 | mutable Mutex mLock; |
| 854 | double mSampleRate; // in samples (frames) per second |
Andy Hung | 7d712bb | 2017-04-20 14:23:41 -0700 | [diff] [blame] | 855 | int64_t mLastFrame; // logging purpose only, 0 on start |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 856 | int32_t mVolumeShaperIdCounter; // a counter to return a unique volume shaper id. |
Andy Hung | da540db | 2017-04-20 14:06:17 -0700 | [diff] [blame] | 857 | std::pair<T /* volume */, bool /* active */> mLastVolume; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 858 | std::list<VolumeShaper> mVolumeShapers; // list provides stable iterators on erase |
| 859 | }; // VolumeHandler |
| 860 | |
| 861 | } // namespace android |
| 862 | |
| 863 | #pragma pop_macro("LOG_TAG") |
| 864 | |
| 865 | #endif // ANDROID_VOLUME_SHAPER_H |