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