blob: 1421ae7018c25cc9930305a5497dddf1a6e05e8f [file] [log] [blame]
Andy Hung9fc8b5c2017-01-24 13:36:48 -08001/*
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
40namespace 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
51class VolumeShaper {
52public:
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
Andy Hung10cbff12017-02-21 17:30:14 -080097 Configuration(const Configuration &configuration)
98 : Interpolator<S, T>(*static_cast<const Interpolator<S, T> *>(&configuration))
99 , mType(configuration.mType)
100 , mOptionFlags(configuration.mOptionFlags)
101 , mDurationMs(configuration.mDurationMs)
102 , mId(configuration.mId) {
103 }
104
Andy Hung9fc8b5c2017-01-24 13:36:48 -0800105 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) {
152 const T out = powf(10.f, volume / 10.);
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
238 status_t writeToParcel(Parcel *parcel) const {
239 if (parcel == nullptr) return BAD_VALUE;
240 return parcel->writeInt32((int32_t)mType)
241 ?: parcel->writeInt32(mId)
242 ?: mType == TYPE_ID
243 ? NO_ERROR
244 : parcel->writeInt32((int32_t)mOptionFlags)
245 ?: parcel->writeDouble(mDurationMs)
246 ?: Interpolator<S, T>::writeToParcel(parcel);
247 }
248
249 status_t readFromParcel(const Parcel &parcel) {
250 int32_t type, optionFlags;
251 return parcel.readInt32(&type)
252 ?: setType((Type)type)
253 ?: parcel.readInt32(&mId)
254 ?: mType == TYPE_ID
255 ? NO_ERROR
256 : parcel.readInt32(&optionFlags)
257 ?: setOptionFlags((OptionFlag)optionFlags)
258 ?: parcel.readDouble(&mDurationMs)
259 ?: Interpolator<S, T>::readFromParcel(parcel)
260 ?: checkCurve();
261 }
262
263 std::string toString() const {
264 std::stringstream ss;
265 ss << "mType: " << mType << std::endl;
266 ss << "mId: " << mId << std::endl;
267 if (mType != TYPE_ID) {
268 ss << "mOptionFlags: " << mOptionFlags << std::endl;
269 ss << "mDurationMs: " << mDurationMs << std::endl;
270 ss << Interpolator<S, T>::toString().c_str();
271 }
272 return ss.str();
273 }
274
275 private:
276 Type mType;
277 int32_t mId;
278 OptionFlag mOptionFlags;
279 double mDurationMs;
280 }; // Configuration
281
282 // must match with VolumeShaper.java in frameworks/base
283 // TODO document per VolumeShaper.java flags.
284 class Operation : public RefBase {
285 public:
286 enum Flag : int32_t {
287 FLAG_NONE = 0,
288 FLAG_REVERSE = (1 << 0),
289 FLAG_TERMINATE = (1 << 1),
290 FLAG_JOIN = (1 << 2),
291 FLAG_DELAY = (1 << 3),
292
293 FLAG_ALL = (FLAG_REVERSE | FLAG_TERMINATE | FLAG_JOIN | FLAG_DELAY),
294 };
295
296 Operation()
297 : mFlags(FLAG_NONE)
298 , mReplaceId(-1) {
299 }
300
301 explicit Operation(Flag flags, int replaceId)
302 : mFlags(flags)
303 , mReplaceId(replaceId) {
304 }
305
306 int32_t getReplaceId() const {
307 return mReplaceId;
308 }
309
310 void setReplaceId(int32_t replaceId) {
311 mReplaceId = replaceId;
312 }
313
314 Flag getFlags() const {
315 return mFlags;
316 }
317
318 status_t setFlags(Flag flags) {
319 if ((flags & ~FLAG_ALL) != 0) {
320 ALOGE("flags has invalid bits: %#x", flags);
321 return BAD_VALUE;
322 }
323 mFlags = flags;
324 return NO_ERROR;
325 }
326
327 status_t writeToParcel(Parcel *parcel) const {
328 if (parcel == nullptr) return BAD_VALUE;
329 return parcel->writeInt32((int32_t)mFlags)
330 ?: parcel->writeInt32(mReplaceId);
331 }
332
333 status_t readFromParcel(const Parcel &parcel) {
334 int32_t flags;
335 return parcel.readInt32(&flags)
336 ?: parcel.readInt32(&mReplaceId)
337 ?: setFlags((Flag)flags);
338 }
339
340 std::string toString() const {
341 std::stringstream ss;
342 ss << "mFlags: " << mFlags << std::endl;
343 ss << "mReplaceId: " << mReplaceId << std::endl;
344 return ss.str();
345 }
346
347 private:
348 Flag mFlags;
349 int32_t mReplaceId;
350 }; // Operation
351
352 // must match with VolumeShaper.java in frameworks/base
353 class State : public RefBase {
354 public:
355 explicit State(T volume, S xOffset)
356 : mVolume(volume)
357 , mXOffset(xOffset) {
358 }
359
360 State()
361 : State(-1.f, -1.f) { }
362
363 T getVolume() const {
364 return mVolume;
365 }
366
367 void setVolume(T volume) {
368 mVolume = volume;
369 }
370
371 S getXOffset() const {
372 return mXOffset;
373 }
374
375 void setXOffset(S xOffset) {
376 mXOffset = xOffset;
377 }
378
379 status_t writeToParcel(Parcel *parcel) const {
380 if (parcel == nullptr) return BAD_VALUE;
381 return parcel->writeFloat(mVolume)
382 ?: parcel->writeFloat(mXOffset);
383 }
384
385 status_t readFromParcel(const Parcel &parcel) {
386 return parcel.readFloat(&mVolume)
387 ?: parcel.readFloat(&mXOffset);
388 }
389
390 std::string toString() const {
391 std::stringstream ss;
392 ss << "mVolume: " << mVolume << std::endl;
393 ss << "mXOffset: " << mXOffset << std::endl;
394 return ss.str();
395 }
396
397 private:
398 T mVolume;
399 S mXOffset;
400 }; // State
401
402 template <typename R>
403 class Translate {
404 public:
405 Translate()
406 : mOffset(0)
407 , mScale(1) {
408 }
409
410 R getOffset() const {
411 return mOffset;
412 }
413
414 void setOffset(R offset) {
415 mOffset = offset;
416 }
417
418 R getScale() const {
419 return mScale;
420 }
421
422 void setScale(R scale) {
423 mScale = scale;
424 }
425
426 R operator()(R in) const {
427 return mScale * (in - mOffset);
428 }
429
430 std::string toString() const {
431 std::stringstream ss;
432 ss << "mOffset: " << mOffset << std::endl;
433 ss << "mScale: " << mScale << std::endl;
434 return ss.str();
435 }
436
437 private:
438 R mOffset;
439 R mScale;
440 }; // Translate
441
442 static int64_t convertTimespecToUs(const struct timespec &tv)
443 {
444 return tv.tv_sec * 1000000ll + tv.tv_nsec / 1000;
445 }
446
447 // current monotonic time in microseconds.
448 static int64_t getNowUs()
449 {
450 struct timespec tv;
451 if (clock_gettime(CLOCK_MONOTONIC, &tv) != 0) {
452 return 0; // system is really sick, just return 0 for consistency.
453 }
454 return convertTimespecToUs(tv);
455 }
456
457 Translate<S> mXTranslate;
458 Translate<T> mYTranslate;
459 sp<VolumeShaper::Configuration> mConfiguration;
460 sp<VolumeShaper::Operation> mOperation;
461 int64_t mStartFrame;
462 T mLastVolume;
463 S mXOffset;
464
465 // TODO: Since we pass configuration and operation as shared pointers
466 // there is a potential risk that the caller may modify these after
467 // delivery. Currently, we don't require copies made here.
468 explicit VolumeShaper(
469 const sp<VolumeShaper::Configuration> &configuration,
470 const sp<VolumeShaper::Operation> &operation)
471 : mConfiguration(configuration) // we do not make a copy
472 , mOperation(operation) // ditto
473 , mStartFrame(-1)
474 , mLastVolume(T(1))
475 , mXOffset(0.f) {
476 if (configuration.get() != nullptr
477 && (getFlags() & VolumeShaper::Operation::FLAG_DELAY) == 0) {
478 mLastVolume = configuration->first().second;
479 }
480 }
481
482 void updatePosition(int64_t startFrame, double sampleRate) {
483 double scale = (mConfiguration->last().first - mConfiguration->first().first)
484 / (mConfiguration->getDurationMs() * 0.001 * sampleRate);
485 const double minScale = 1. / INT64_MAX;
486 scale = std::max(scale, minScale);
487 VS_LOG("update position: scale %lf frameCount:%lld, sampleRate:%lf",
488 scale, (long long) startFrame, sampleRate);
489 mXTranslate.setOffset(startFrame - mConfiguration->first().first / scale);
490 mXTranslate.setScale(scale);
491 VS_LOG("translate: %s", mXTranslate.toString().c_str());
492 }
493
494 // We allow a null operation here, though VolumeHandler always provides one.
495 VolumeShaper::Operation::Flag getFlags() const {
496 return mOperation == nullptr
497 ? VolumeShaper::Operation::FLAG_NONE :mOperation->getFlags();
498 }
499
500 sp<VolumeShaper::State> getState() const {
501 return new VolumeShaper::State(mLastVolume, mXOffset);
502 }
503
Andy Hung10cbff12017-02-21 17:30:14 -0800504 std::pair<T /* volume */, bool /* active */> getVolume(
505 int64_t trackFrameCount, double trackSampleRate) {
Andy Hung9fc8b5c2017-01-24 13:36:48 -0800506 if ((getFlags() & VolumeShaper::Operation::FLAG_DELAY) != 0) {
507 VS_LOG("delayed VolumeShaper, ignoring");
508 mLastVolume = T(1);
509 mXOffset = 0.;
510 return std::make_pair(T(1), false);
511 }
512 const bool clockTime = (mConfiguration->getOptionFlags()
513 & VolumeShaper::Configuration::OPTION_FLAG_CLOCK_TIME) != 0;
514 const int64_t frameCount = clockTime ? getNowUs() : trackFrameCount;
515 const double sampleRate = clockTime ? 1000000 : trackSampleRate;
516
517 if (mStartFrame < 0) {
518 updatePosition(frameCount, sampleRate);
519 mStartFrame = frameCount;
520 }
521 VS_LOG("frameCount: %lld", (long long)frameCount);
522 S x = mXTranslate((T)frameCount);
523 VS_LOG("translation: %f", x);
524
525 // handle reversal of position
526 if (getFlags() & VolumeShaper::Operation::FLAG_REVERSE) {
527 x = 1.f - x;
528 VS_LOG("reversing to %f", x);
529 if (x < mConfiguration->first().first) {
530 mXOffset = 1.f;
531 const T volume = mConfiguration->adjustVolume(
532 mConfiguration->first().second); // persist last value
533 VS_LOG("persisting volume %f", volume);
534 mLastVolume = volume;
535 return std::make_pair(volume, false);
536 }
537 if (x > mConfiguration->last().first) {
538 mXOffset = 0.f;
539 mLastVolume = 1.f;
Andy Hung10cbff12017-02-21 17:30:14 -0800540 return std::make_pair(T(1), true); // too early
Andy Hung9fc8b5c2017-01-24 13:36:48 -0800541 }
542 } else {
543 if (x < mConfiguration->first().first) {
544 mXOffset = 0.f;
545 mLastVolume = 1.f;
Andy Hung10cbff12017-02-21 17:30:14 -0800546 return std::make_pair(T(1), true); // too early
Andy Hung9fc8b5c2017-01-24 13:36:48 -0800547 }
548 if (x > mConfiguration->last().first) {
549 mXOffset = 1.f;
550 const T volume = mConfiguration->adjustVolume(
551 mConfiguration->last().second); // persist last value
552 VS_LOG("persisting volume %f", volume);
553 mLastVolume = volume;
554 return std::make_pair(volume, false);
555 }
556 }
557 mXOffset = x;
558 // x contains the location on the volume curve to use.
559 const T unscaledVolume = mConfiguration->findY(x);
560 const T volumeChange = mYTranslate(unscaledVolume);
561 const T volume = mConfiguration->adjustVolume(volumeChange);
562 VS_LOG("volume: %f unscaled: %f", volume, unscaledVolume);
563 mLastVolume = volume;
Andy Hung10cbff12017-02-21 17:30:14 -0800564 return std::make_pair(volume, true);
Andy Hung9fc8b5c2017-01-24 13:36:48 -0800565 }
566
567 std::string toString() const {
568 std::stringstream ss;
569 ss << "StartFrame: " << mStartFrame << std::endl;
570 ss << mXTranslate.toString().c_str();
571 ss << mYTranslate.toString().c_str();
572 if (mConfiguration.get() == nullptr) {
573 ss << "VolumeShaper::Configuration: nullptr" << std::endl;
574 } else {
575 ss << "VolumeShaper::Configuration:" << std::endl;
576 ss << mConfiguration->toString().c_str();
577 }
578 if (mOperation.get() == nullptr) {
579 ss << "VolumeShaper::Operation: nullptr" << std::endl;
580 } else {
581 ss << "VolumeShaper::Operation:" << std::endl;
582 ss << mOperation->toString().c_str();
583 }
584 return ss.str();
585 }
586}; // VolumeShaper
587
588// VolumeHandler combines the volume factors of multiple VolumeShapers and handles
589// multiple thread access by synchronizing all public methods.
590class VolumeHandler : public RefBase {
591public:
592 using S = float;
593 using T = float;
594
595 explicit VolumeHandler(uint32_t sampleRate)
596 : mSampleRate((double)sampleRate)
597 , mLastFrame(0) {
598 }
599
600 VolumeShaper::Status applyVolumeShaper(
601 const sp<VolumeShaper::Configuration> &configuration,
602 const sp<VolumeShaper::Operation> &operation) {
Andy Hung10cbff12017-02-21 17:30:14 -0800603 VS_LOG("applyVolumeShaper:configuration: %s", configuration->toString().c_str());
604 VS_LOG("applyVolumeShaper:operation: %s", operation->toString().c_str());
Andy Hung9fc8b5c2017-01-24 13:36:48 -0800605 AutoMutex _l(mLock);
606 if (configuration == nullptr) {
607 ALOGE("null configuration");
608 return VolumeShaper::Status(BAD_VALUE);
609 }
610 if (operation == nullptr) {
611 ALOGE("null operation");
612 return VolumeShaper::Status(BAD_VALUE);
613 }
614 const int32_t id = configuration->getId();
615 if (id < 0) {
616 ALOGE("negative id: %d", id);
617 return VolumeShaper::Status(BAD_VALUE);
618 }
619 VS_LOG("applyVolumeShaper id: %d", id);
620
621 switch (configuration->getType()) {
Andy Hung9fc8b5c2017-01-24 13:36:48 -0800622 case VolumeShaper::Configuration::TYPE_SCALE: {
623 const int replaceId = operation->getReplaceId();
624 if (replaceId >= 0) {
625 auto replaceIt = findId_l(replaceId);
626 if (replaceIt == mVolumeShapers.end()) {
627 ALOGW("cannot find replace id: %d", replaceId);
628 } else {
629 if ((replaceIt->getFlags() & VolumeShaper::Operation::FLAG_JOIN) != 0) {
630 // For join, we scale the start volume of the current configuration
631 // to match the last-used volume of the replacing VolumeShaper.
632 auto state = replaceIt->getState();
633 if (state->getXOffset() >= 0) { // valid
634 const T volume = state->getVolume();
635 ALOGD("join: scaling start volume to %f", volume);
636 configuration->scaleToStartVolume(volume);
637 }
638 }
639 (void)mVolumeShapers.erase(replaceIt);
640 }
641 }
642 // check if we have another of the same id.
643 auto oldIt = findId_l(id);
644 if (oldIt != mVolumeShapers.end()) {
645 ALOGW("duplicate id, removing old %d", id);
646 (void)mVolumeShapers.erase(oldIt);
647 }
648 // create new VolumeShaper
649 mVolumeShapers.emplace_back(configuration, operation);
Andy Hung10cbff12017-02-21 17:30:14 -0800650 }
651 // fall through to handle the operation
652 case VolumeShaper::Configuration::TYPE_ID: {
653 VS_LOG("trying to find id: %d", id);
654 auto it = findId_l(id);
655 if (it == mVolumeShapers.end()) {
656 VS_LOG("couldn't find id: %d", id);
657 return VolumeShaper::Status(INVALID_OPERATION);
658 }
659 if ((it->getFlags() & VolumeShaper::Operation::FLAG_TERMINATE) != 0) {
660 VS_LOG("terminate id: %d", id);
661 mVolumeShapers.erase(it);
662 break;
663 }
664 const bool clockTime = (it->mConfiguration->getOptionFlags()
665 & VolumeShaper::Configuration::OPTION_FLAG_CLOCK_TIME) != 0;
666 if ((it->getFlags() & VolumeShaper::Operation::FLAG_REVERSE) !=
667 (operation->getFlags() & VolumeShaper::Operation::FLAG_REVERSE)) {
668 const int64_t frameCount = clockTime ? VolumeShaper::getNowUs() : mLastFrame;
669 const S x = it->mXTranslate((T)frameCount);
670 VS_LOG("reverse translation: %f", x);
671 // reflect position
672 S target = 1.f - x;
673 if (target < it->mConfiguration->first().first) {
674 VS_LOG("clamp to start - begin immediately");
675 target = 0.;
676 }
677 VS_LOG("target reverse: %f", target);
678 it->mXTranslate.setOffset(it->mXTranslate.getOffset()
679 + (x - target) / it->mXTranslate.getScale());
680 }
681 it->mOperation = operation; // replace the operation
Andy Hung9fc8b5c2017-01-24 13:36:48 -0800682 } break;
683 }
684 return VolumeShaper::Status(id);
685 }
686
687 sp<VolumeShaper::State> getVolumeShaperState(int id) {
688 AutoMutex _l(mLock);
689 auto it = findId_l(id);
690 if (it == mVolumeShapers.end()) {
691 return nullptr;
692 }
693 return it->getState();
694 }
695
Andy Hung10cbff12017-02-21 17:30:14 -0800696 std::pair<T /* volume */, bool /* active */> getVolume(int64_t trackFrameCount) {
Andy Hung9fc8b5c2017-01-24 13:36:48 -0800697 AutoMutex _l(mLock);
698 mLastFrame = trackFrameCount;
699 T volume(1);
Andy Hung10cbff12017-02-21 17:30:14 -0800700 size_t activeCount = 0;
Andy Hung9fc8b5c2017-01-24 13:36:48 -0800701 for (auto it = mVolumeShapers.begin(); it != mVolumeShapers.end();) {
702 std::pair<T, bool> shaperVolume =
703 it->getVolume(trackFrameCount, mSampleRate);
704 volume *= shaperVolume.first;
Andy Hung10cbff12017-02-21 17:30:14 -0800705 activeCount += shaperVolume.second;
Andy Hung9fc8b5c2017-01-24 13:36:48 -0800706 ++it;
707 }
Andy Hung10cbff12017-02-21 17:30:14 -0800708 return std::make_pair(volume, activeCount != 0);
Andy Hung9fc8b5c2017-01-24 13:36:48 -0800709 }
710
711 std::string toString() const {
712 AutoMutex _l(mLock);
713 std::stringstream ss;
714 ss << "mSampleRate: " << mSampleRate << std::endl;
715 ss << "mLastFrame: " << mLastFrame << std::endl;
716 for (const auto &shaper : mVolumeShapers) {
717 ss << shaper.toString().c_str();
718 }
719 return ss.str();
720 }
721
722private:
723 std::list<VolumeShaper>::iterator findId_l(int32_t id) {
724 std::list<VolumeShaper>::iterator it = mVolumeShapers.begin();
725 for (; it != mVolumeShapers.end(); ++it) {
726 if (it->mConfiguration->getId() == id) {
727 break;
728 }
729 }
730 return it;
731 }
732
733 mutable Mutex mLock;
734 double mSampleRate; // in samples (frames) per second
735 int64_t mLastFrame; // logging purpose only
736 std::list<VolumeShaper> mVolumeShapers; // list provides stable iterators on erase
737}; // VolumeHandler
738
739} // namespace android
740
741#pragma pop_macro("LOG_TAG")
742
743#endif // ANDROID_VOLUME_SHAPER_H