VolumeShaper: Initial implementation

The VolumeShaper is used to apply a volume
envelope to an AudioTrack or a MediaPlayer.

Test: CTS
Bug: 30920125
Bug: 31015569
Change-Id: I42e2f13bd6879299dc780e60d143c2d465483a44
diff --git a/include/media/AudioTrack.h b/include/media/AudioTrack.h
index cd33a44..1996dbe 100644
--- a/include/media/AudioTrack.h
+++ b/include/media/AudioTrack.h
@@ -732,6 +732,14 @@
     /* Set parameters - only possible when using direct output */
             status_t    setParameters(const String8& keyValuePairs);
 
+    /* Sets the volume shaper object */
+            VolumeShaper::Status applyVolumeShaper(
+                    const sp<VolumeShaper::Configuration>& configuration,
+                    const sp<VolumeShaper::Operation>& operation);
+
+    /* Gets the volume shaper state */
+            sp<VolumeShaper::State> getVolumeShaperState(int id);
+
     /* Get parameters */
             String8     getParameters(const String8& keys);
 
@@ -1118,6 +1126,10 @@
     //  a value of AUDIO_PORT_HANDLE_NONE indicated default (AudioPolicyManager) routing.
     audio_port_handle_t     mSelectedDeviceId;
 
+    sp<VolumeHandler>       mVolumeHandler;
+
+    int32_t                 mVolumeShaperId;
+
 private:
     class DeathNotifier : public IBinder::DeathRecipient {
     public:
diff --git a/include/media/IAudioTrack.h b/include/media/IAudioTrack.h
index a31cec6..27a62d6 100644
--- a/include/media/IAudioTrack.h
+++ b/include/media/IAudioTrack.h
@@ -26,6 +26,7 @@
 #include <binder/IMemory.h>
 #include <utils/String8.h>
 #include <media/AudioTimestamp.h>
+#include <media/VolumeShaper.h>
 
 namespace android {
 
@@ -74,6 +75,14 @@
 
     /* Signal the playback thread for a change in control block */
     virtual void        signal() = 0;
+
+    /* Sets the volume shaper */
+    virtual VolumeShaper::Status applyVolumeShaper(
+            const sp<VolumeShaper::Configuration>& configuration,
+            const sp<VolumeShaper::Operation>& operation) = 0;
+
+    /* gets the volume shaper state */
+    virtual sp<VolumeShaper::State> getVolumeShaperState(int id) = 0;
 };
 
 // ----------------------------------------------------------------------------
diff --git a/include/media/IMediaPlayer.h b/include/media/IMediaPlayer.h
index ca865a8..a4cc152 100644
--- a/include/media/IMediaPlayer.h
+++ b/include/media/IMediaPlayer.h
@@ -25,6 +25,7 @@
 
 #include <media/IMediaSource.h>
 #include <media/drm/DrmAPI.h>   // for DrmPlugin::* enum
+#include <media/VolumeShaper.h>
 
 // Fwd decl to make sure everyone agrees that the scope of struct sockaddr_in is
 // global, and not in android::
@@ -90,6 +91,12 @@
     virtual status_t        setRetransmitEndpoint(const struct sockaddr_in* endpoint) = 0;
     virtual status_t        getRetransmitEndpoint(struct sockaddr_in* endpoint) = 0;
     virtual status_t        setNextPlayer(const sp<IMediaPlayer>& next) = 0;
+
+    virtual VolumeShaper::Status applyVolumeShaper(
+                                    const sp<VolumeShaper::Configuration>& configuration,
+                                    const sp<VolumeShaper::Operation>& operation) = 0;
+    virtual sp<VolumeShaper::State> getVolumeShaperState(int id) = 0;
+
     // ModDrm
     virtual status_t        prepareDrm(const uint8_t uuid[16], const int mode) = 0;
     virtual status_t        releaseDrm() = 0;
diff --git a/include/media/Interpolator.h b/include/media/Interpolator.h
new file mode 100644
index 0000000..1b26b87
--- /dev/null
+++ b/include/media/Interpolator.h
@@ -0,0 +1,331 @@
+/*
+ * Copyright 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_INTERPOLATOR_H
+#define ANDROID_INTERPOLATOR_H
+
+#include <map>
+#include <sstream>
+#include <unordered_map>
+
+#include <binder/Parcel.h>
+#include <utils/RefBase.h>
+
+#pragma push_macro("LOG_TAG")
+#undef LOG_TAG
+#define LOG_TAG "Interpolator"
+
+namespace android {
+
+/*
+ * A general purpose spline interpolator class which takes a set of points
+ * and performs interpolation.  This is used for the VolumeShaper class.
+ */
+
+template <typename S, typename T>
+class Interpolator : public std::map<S, T> {
+public:
+    // Polynomial spline interpolators
+    // Extend only at the end of enum, as this must match order in VolumeShapers.java.
+    enum InterpolatorType : int32_t {
+        INTERPOLATOR_TYPE_STEP,   // Not continuous
+        INTERPOLATOR_TYPE_LINEAR, // C0
+        INTERPOLATOR_TYPE_CUBIC,  // C1
+        INTERPOLATOR_TYPE_CUBIC_MONOTONIC, // C1 (to provide locally monotonic curves)
+        // INTERPOLATOR_TYPE_CUBIC_C2, // TODO - requires global computation / cache
+    };
+
+    explicit Interpolator(
+            InterpolatorType interpolatorType = INTERPOLATOR_TYPE_LINEAR,
+            bool cache = true)
+        : mCache(cache)
+        , mFirstSlope(0)
+        , mLastSlope(0) {
+        setInterpolatorType(interpolatorType);
+    }
+
+    std::pair<S, T> first() const {
+        return *this->begin();
+    }
+
+    std::pair<S, T> last() const {
+        return *this->rbegin();
+    }
+
+    // find the corresponding Y point from a X point.
+    T findY(S x) { // logically const, but modifies cache
+        auto high = this->lower_bound(x);
+        // greater than last point
+        if (high == this->end()) {
+            return this->rbegin()->second;
+        }
+        // at or before first point
+        if (high == this->begin()) {
+            return high->second;
+        }
+        // go lower.
+        auto low = high;
+        --low;
+
+        // now that we have two adjacent points:
+        switch (mInterpolatorType) {
+        case INTERPOLATOR_TYPE_STEP:
+            return high->first == x ? high->second : low->second;
+        case INTERPOLATOR_TYPE_LINEAR:
+            return ((high->first - x) * low->second + (x - low->first) * high->second)
+                    / (high->first - low->first);
+        case INTERPOLATOR_TYPE_CUBIC:
+        case INTERPOLATOR_TYPE_CUBIC_MONOTONIC:
+        default: {
+            // See https://en.wikipedia.org/wiki/Cubic_Hermite_spline
+
+            const S interval =  high->first - low->first;
+
+            // check to see if we've cached the polynomial coefficients
+            if (mMemo.count(low->first) != 0) {
+                const S t = (x - low->first) / interval;
+                const S t2 = t * t;
+                const auto &memo = mMemo[low->first];
+                return low->second + std::get<0>(memo) * t
+                        + (std::get<1>(memo) + std::get<2>(memo) * t) * t2;
+            }
+
+            // find the neighboring points (low2 < low < high < high2)
+            auto low2 = this->end();
+            if (low != this->begin()) {
+                low2 = low;
+                --low2; // decrementing this->begin() is undefined
+            }
+            auto high2 = high;
+            ++high2;
+
+            // you could have catmullRom with monotonic or
+            // non catmullRom (finite difference) with regular cubic;
+            // the choices here minimize computation.
+            bool monotonic, catmullRom;
+            if (mInterpolatorType == INTERPOLATOR_TYPE_CUBIC_MONOTONIC) {
+                monotonic = true;
+                catmullRom = false;
+            } else {
+                monotonic = false;
+                catmullRom = true;
+            }
+
+            // secants are only needed for finite difference splines or
+            // monotonic computation.
+            // we use lazy computation here - if we precompute in
+            // a single pass, duplicate secant computations may be avoided.
+            S sec, sec0, sec1;
+            if (!catmullRom || monotonic) {
+                sec = (high->second - low->second) / interval;
+                sec0 = low2 != this->end()
+                        ? (low->second - low2->second) / (low->first - low2->first)
+                        : mFirstSlope;
+                sec1 = high2 != this->end()
+                        ? (high2->second - high->second) / (high2->first - high->first)
+                        : mLastSlope;
+            }
+
+            // compute the tangent slopes at the control points
+            S m0, m1;
+            if (catmullRom) {
+                // Catmull-Rom spline
+                m0 = low2 != this->end()
+                        ? (high->second - low2->second) / (high->first - low2->first)
+                        : mFirstSlope;
+
+                m1 = high2 != this->end()
+                        ? (high2->second - low->second) / (high2->first - low->first)
+                        : mLastSlope;
+            } else {
+                // finite difference spline
+                m0 = (sec0 + sec) * 0.5;
+                m1 = (sec1 + sec) * 0.5;
+            }
+
+            if (monotonic) {
+                // https://en.wikipedia.org/wiki/Monotone_cubic_interpolation
+                // A sufficient condition for Fritsch–Carlson monotonicity is constraining
+                // (1) the normalized slopes to be within the circle of radius 3, or
+                // (2) the normalized slopes to be within the square of radius 3.
+                // Condition (2) is more generous and easier to compute.
+                const S maxSlope = 3 * sec;
+                m0 = constrainSlope(m0, maxSlope);
+                m1 = constrainSlope(m1, maxSlope);
+
+                m0 = constrainSlope(m0, 3 * sec0);
+                m1 = constrainSlope(m1, 3 * sec1);
+            }
+
+            const S t = (x - low->first) / interval;
+            const S t2 = t * t;
+            if (mCache) {
+                // convert to cubic polynomial coefficients and compute
+                m0 *= interval;
+                m1 *= interval;
+                const T dy = high->second - low->second;
+                const S c0 = low->second;
+                const S c1 = m0;
+                const S c2 = 3 * dy - 2 * m0 - m1;
+                const S c3 = m0 + m1 - 2 * dy;
+                mMemo[low->first] = std::make_tuple(c1, c2, c3);
+                return c0 + c1 * t + (c2 + c3 * t) * t2;
+            } else {
+                // classic Hermite interpolation
+                const S t3 = t2 * t;
+                const S h00 =  2 * t3 - 3 * t2     + 1;
+                const S h10 =      t3 - 2 * t2 + t    ;
+                const S h01 = -2 * t3 + 3 * t2        ;
+                const S h11 =      t3     - t2        ;
+                return h00 * low->second + (h10 * m0 + h11 * m1) * interval + h01 * high->second;
+            }
+        } // default
+        }
+    }
+
+    InterpolatorType getInterpolatorType() const {
+        return mInterpolatorType;
+    }
+
+    status_t setInterpolatorType(InterpolatorType interpolatorType) {
+        switch (interpolatorType) {
+        case INTERPOLATOR_TYPE_STEP:   // Not continuous
+        case INTERPOLATOR_TYPE_LINEAR: // C0
+        case INTERPOLATOR_TYPE_CUBIC:  // C1
+        case INTERPOLATOR_TYPE_CUBIC_MONOTONIC: // C1 + other constraints
+        // case INTERPOLATOR_TYPE_CUBIC_C2:
+            mInterpolatorType = interpolatorType;
+            return NO_ERROR;
+        default:
+            ALOGE("invalid interpolatorType: %d", interpolatorType);
+            return BAD_VALUE;
+        }
+    }
+
+    T getFirstSlope() const {
+        return mFirstSlope;
+    }
+
+    void setFirstSlope(T slope) {
+        mFirstSlope = slope;
+    }
+
+    T getLastSlope() const {
+        return mLastSlope;
+    }
+
+    void setLastSlope(T slope) {
+        mLastSlope = slope;
+    }
+
+    void clearCache() {
+        mMemo.clear();
+    }
+
+    status_t writeToParcel(Parcel *parcel) const {
+        if (parcel == nullptr) {
+            return BAD_VALUE;
+        }
+        status_t res = parcel->writeInt32(mInterpolatorType)
+                ?: parcel->writeFloat(mFirstSlope)
+                ?: parcel->writeFloat(mLastSlope)
+                ?: parcel->writeUint32((uint32_t)this->size()); // silent truncation
+        if (res != NO_ERROR) {
+            return res;
+        }
+        for (const auto &pt : *this) {
+            res = parcel->writeFloat(pt.first)
+                    ?: parcel->writeFloat(pt.second);
+            if (res != NO_ERROR) {
+                return res;
+            }
+        }
+        return NO_ERROR;
+    }
+
+    status_t readFromParcel(const Parcel &parcel) {
+        this->clear();
+        int32_t type;
+        uint32_t size;
+        status_t res = parcel.readInt32(&type)
+                        ?: parcel.readFloat(&mFirstSlope)
+                        ?: parcel.readFloat(&mLastSlope)
+                        ?: parcel.readUint32(&size)
+                        ?: setInterpolatorType((InterpolatorType)type);
+        if (res != NO_ERROR) {
+            return res;
+        }
+        // Note: We don't need to check size is within some bounds as
+        // the Parcel read will fail if size is incorrectly specified too large.
+        float lastx;
+        for (uint32_t i = 0; i < size; ++i) {
+            float x, y;
+            res = parcel.readFloat(&x)
+                    ?: parcel.readFloat(&y);
+            if (res != NO_ERROR) {
+                return res;
+            }
+            if (i > 0 && !(x > lastx) /* handle nan */
+                    || y != y /* handle nan */) {
+                // This is a std::map object which imposes sorted order
+                // automatically on emplace.
+                // Nevertheless for reading from a Parcel,
+                // we require that the points be specified monotonic in x.
+                return BAD_VALUE;
+            }
+            this->emplace(x, y);
+            lastx = x;
+        }
+        return NO_ERROR;
+    }
+
+    std::string toString() const {
+        std::stringstream ss;
+        ss << "mInterpolatorType: " << mInterpolatorType << std::endl;
+        for (const auto &pt : *this) {
+            ss << pt.first << " " << pt.second << std::endl;
+        }
+        return ss.str();
+    }
+
+private:
+    static S constrainSlope(S slope, S maxSlope) {
+        if (maxSlope > 0) {
+            slope = std::min(slope, maxSlope);
+            slope = std::max(slope, S(0)); // not globally monotonic
+        } else {
+            slope = std::max(slope, maxSlope);
+            slope = std::min(slope, S(0)); // not globally monotonic
+        }
+        return slope;
+    }
+
+    InterpolatorType mInterpolatorType;
+    bool mCache; // whether we cache spline coefficient computation
+
+    // for cubic interpolation, the boundary conditions in slope.
+    S mFirstSlope;
+    S mLastSlope;
+
+    // spline cubic polynomial coefficient cache
+    std::unordered_map<S, std::tuple<S /* c1 */, S /* c2 */, S /* c3 */>> mMemo;
+};
+
+} // namespace android
+
+#pragma pop_macro("LOG_TAG")
+
+#endif // ANDROID_INTERPOLATOR_H
diff --git a/include/media/MediaPlayerInterface.h b/include/media/MediaPlayerInterface.h
index 4e18f1f..b3e53fc 100644
--- a/include/media/MediaPlayerInterface.h
+++ b/include/media/MediaPlayerInterface.h
@@ -145,6 +145,11 @@
 
         virtual status_t    setParameters(const String8& /* keyValuePairs */) { return NO_ERROR; }
         virtual String8     getParameters(const String8& /* keys */) { return String8::empty(); }
+
+        virtual VolumeShaper::Status applyVolumeShaper(
+                                    const sp<VolumeShaper::Configuration>& configuration,
+                                    const sp<VolumeShaper::Operation>& operation);
+        virtual sp<VolumeShaper::State> getVolumeShaperState(int id);
     };
 
                         MediaPlayerBase() : mCookie(0), mNotify(0) {}
diff --git a/include/media/VolumeShaper.h b/include/media/VolumeShaper.h
new file mode 100644
index 0000000..acb22ab
--- /dev/null
+++ b/include/media/VolumeShaper.h
@@ -0,0 +1,736 @@
+/*
+ * Copyright 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_VOLUME_SHAPER_H
+#define ANDROID_VOLUME_SHAPER_H
+
+#include <list>
+#include <math.h>
+#include <sstream>
+
+#include <binder/Parcel.h>
+#include <media/Interpolator.h>
+#include <utils/Mutex.h>
+#include <utils/RefBase.h>
+
+#pragma push_macro("LOG_TAG")
+#undef LOG_TAG
+#define LOG_TAG "VolumeShaper"
+
+// turn on VolumeShaper logging
+#if 0
+#define VS_LOG ALOGD
+#else
+#define VS_LOG(...)
+#endif
+
+namespace android {
+
+// The native VolumeShaper class mirrors the java VolumeShaper class;
+// in addition, the native class contains implementation for actual operation.
+//
+// VolumeShaper methods are not safe for multiple thread access.
+// Use VolumeHandler for thread-safe encapsulation of multiple VolumeShapers.
+//
+// Classes below written are to avoid naked pointers so there are no
+// explicit destructors required.
+
+class VolumeShaper {
+public:
+    using S = float;
+    using T = float;
+
+    static const int kSystemIdMax = 16;
+
+    // VolumeShaper::Status is equivalent to status_t if negative
+    // but if non-negative represents the id operated on.
+    // It must be expressible as an int32_t for binder purposes.
+    using Status = status_t;
+
+    class Configuration : public Interpolator<S, T>, public RefBase {
+    public:
+        /* VolumeShaper.Configuration derives from the Interpolator class and adds
+         * parameters relating to the volume shape.
+         */
+
+        // TODO document as per VolumeShaper.java flags.
+
+        // must match with VolumeShaper.java in frameworks/base
+        enum Type : int32_t {
+            TYPE_ID,
+            TYPE_SCALE,
+        };
+
+        // must match with VolumeShaper.java in frameworks/base
+        enum OptionFlag : int32_t {
+            OPTION_FLAG_NONE           = 0,
+            OPTION_FLAG_VOLUME_IN_DBFS = (1 << 0),
+            OPTION_FLAG_CLOCK_TIME     = (1 << 1),
+
+            OPTION_FLAG_ALL            = (OPTION_FLAG_VOLUME_IN_DBFS | OPTION_FLAG_CLOCK_TIME),
+        };
+
+        // bring to derived class; must match with VolumeShaper.java in frameworks/base
+        using InterpolatorType = Interpolator<S, T>::InterpolatorType;
+
+        Configuration()
+            : Interpolator<S, T>()
+            , mType(TYPE_SCALE)
+            , mOptionFlags(OPTION_FLAG_NONE)
+            , mDurationMs(1000.)
+            , mId(-1) {
+        }
+
+        Type getType() const {
+            return mType;
+        }
+
+        status_t setType(Type type) {
+            switch (type) {
+            case TYPE_ID:
+            case TYPE_SCALE:
+                mType = type;
+                return NO_ERROR;
+            default:
+                ALOGE("invalid Type: %d", type);
+                return BAD_VALUE;
+            }
+        }
+
+        OptionFlag getOptionFlags() const {
+            return mOptionFlags;
+        }
+
+        status_t setOptionFlags(OptionFlag optionFlags) {
+            if ((optionFlags & ~OPTION_FLAG_ALL) != 0) {
+                ALOGE("optionFlags has invalid bits: %#x", optionFlags);
+                return BAD_VALUE;
+            }
+            mOptionFlags = optionFlags;
+            return NO_ERROR;
+        }
+
+        double getDurationMs() const {
+            return mDurationMs;
+        }
+
+        void setDurationMs(double durationMs) {
+            mDurationMs = durationMs;
+        }
+
+        int32_t getId() const {
+            return mId;
+        }
+
+        void setId(int32_t id) {
+            mId = id;
+        }
+
+        T adjustVolume(T volume) const {
+            if ((getOptionFlags() & OPTION_FLAG_VOLUME_IN_DBFS) != 0) {
+                const T out = powf(10.f, volume / 10.);
+                VS_LOG("in: %f  out: %f", volume, out);
+                volume = out;
+            }
+            // clamp
+            if (volume < 0.f) {
+                volume = 0.f;
+            } else if (volume > 1.f) {
+                volume = 1.f;
+            }
+            return volume;
+        }
+
+        status_t checkCurve() {
+            if (mType == TYPE_ID) return NO_ERROR;
+            if (this->size() < 2) {
+                ALOGE("curve must have at least 2 points");
+                return BAD_VALUE;
+            }
+            if (first().first != 0.f || last().first != 1.f) {
+                ALOGE("curve must start at 0.f and end at 1.f");
+                return BAD_VALUE;
+            }
+            if ((getOptionFlags() & OPTION_FLAG_VOLUME_IN_DBFS) != 0) {
+                for (const auto &pt : *this) {
+                    if (!(pt.second <= 0.f) /* handle nan */) {
+                        ALOGE("positive volume dbFS");
+                        return BAD_VALUE;
+                    }
+                }
+            } else {
+                for (const auto &pt : *this) {
+                    if (!(pt.second >= 0.f) || !(pt.second <= 1.f) /* handle nan */) {
+                        ALOGE("volume < 0.f or > 1.f");
+                        return BAD_VALUE;
+                    }
+                }
+            }
+            return NO_ERROR;
+        }
+
+        void clampVolume() {
+            if ((mOptionFlags & OPTION_FLAG_VOLUME_IN_DBFS) != 0) {
+                for (auto it = this->begin(); it != this->end(); ++it) {
+                    if (!(it->second <= 0.f) /* handle nan */) {
+                        it->second = 0.f;
+                    }
+                }
+            } else {
+                for (auto it = this->begin(); it != this->end(); ++it) {
+                    if (!(it->second >= 0.f) /* handle nan */) {
+                        it->second = 0.f;
+                    } else if (!(it->second <= 1.f)) {
+                        it->second = 1.f;
+                    }
+                }
+            }
+        }
+
+        /* scaleToStartVolume() is used to set the start volume of a
+         * new VolumeShaper curve, when replacing one VolumeShaper
+         * with another using the "join" (volume match) option.
+         *
+         * It works best for monotonic volume ramps or ducks.
+         */
+        void scaleToStartVolume(T volume) {
+            if (this->size() < 2) {
+                return;
+            }
+            const T startVolume = first().second;
+            const T endVolume = last().second;
+            if (endVolume == startVolume) {
+                // match with linear ramp
+                const T offset = volume - startVolume;
+                for (auto it = this->begin(); it != this->end(); ++it) {
+                    it->second = it->second + offset * (1.f - it->first);
+                }
+            } else {
+                const T  scale = (volume - endVolume) / (startVolume - endVolume);
+                for (auto it = this->begin(); it != this->end(); ++it) {
+                    it->second = scale * (it->second - endVolume) + endVolume;
+                }
+            }
+            clampVolume();
+        }
+
+        status_t writeToParcel(Parcel *parcel) const {
+            if (parcel == nullptr) return BAD_VALUE;
+            return parcel->writeInt32((int32_t)mType)
+                    ?: parcel->writeInt32(mId)
+                    ?: mType == TYPE_ID
+                        ? NO_ERROR
+                        : parcel->writeInt32((int32_t)mOptionFlags)
+                            ?: parcel->writeDouble(mDurationMs)
+                            ?: Interpolator<S, T>::writeToParcel(parcel);
+        }
+
+        status_t readFromParcel(const Parcel &parcel) {
+            int32_t type, optionFlags;
+            return parcel.readInt32(&type)
+                    ?: setType((Type)type)
+                    ?: parcel.readInt32(&mId)
+                    ?: mType == TYPE_ID
+                        ? NO_ERROR
+                        : parcel.readInt32(&optionFlags)
+                            ?: setOptionFlags((OptionFlag)optionFlags)
+                            ?: parcel.readDouble(&mDurationMs)
+                            ?: Interpolator<S, T>::readFromParcel(parcel)
+                            ?: checkCurve();
+        }
+
+        std::string toString() const {
+            std::stringstream ss;
+            ss << "mType: " << mType << std::endl;
+            ss << "mId: " << mId << std::endl;
+            if (mType != TYPE_ID) {
+                ss << "mOptionFlags: " << mOptionFlags << std::endl;
+                ss << "mDurationMs: " << mDurationMs << std::endl;
+                ss << Interpolator<S, T>::toString().c_str();
+            }
+            return ss.str();
+        }
+
+    private:
+        Type mType;
+        int32_t mId;
+        OptionFlag mOptionFlags;
+        double mDurationMs;
+    }; // Configuration
+
+    // must match with VolumeShaper.java in frameworks/base
+    // TODO document per VolumeShaper.java flags.
+    class Operation : public RefBase {
+    public:
+        enum Flag : int32_t {
+            FLAG_NONE      = 0,
+            FLAG_REVERSE   = (1 << 0),
+            FLAG_TERMINATE = (1 << 1),
+            FLAG_JOIN      = (1 << 2),
+            FLAG_DELAY     = (1 << 3),
+
+            FLAG_ALL       = (FLAG_REVERSE | FLAG_TERMINATE | FLAG_JOIN | FLAG_DELAY),
+        };
+
+        Operation()
+            : mFlags(FLAG_NONE)
+            , mReplaceId(-1) {
+        }
+
+        explicit Operation(Flag flags, int replaceId)
+            : mFlags(flags)
+            , mReplaceId(replaceId) {
+        }
+
+        int32_t getReplaceId() const {
+            return mReplaceId;
+        }
+
+        void setReplaceId(int32_t replaceId) {
+            mReplaceId = replaceId;
+        }
+
+        Flag getFlags() const {
+            return mFlags;
+        }
+
+        status_t setFlags(Flag flags) {
+            if ((flags & ~FLAG_ALL) != 0) {
+                ALOGE("flags has invalid bits: %#x", flags);
+                return BAD_VALUE;
+            }
+            mFlags = flags;
+            return NO_ERROR;
+        }
+
+        status_t writeToParcel(Parcel *parcel) const {
+            if (parcel == nullptr) return BAD_VALUE;
+            return parcel->writeInt32((int32_t)mFlags)
+                    ?: parcel->writeInt32(mReplaceId);
+        }
+
+        status_t readFromParcel(const Parcel &parcel) {
+            int32_t flags;
+            return parcel.readInt32(&flags)
+                    ?: parcel.readInt32(&mReplaceId)
+                    ?: setFlags((Flag)flags);
+        }
+
+        std::string toString() const {
+            std::stringstream ss;
+            ss << "mFlags: " << mFlags << std::endl;
+            ss << "mReplaceId: " << mReplaceId << std::endl;
+            return ss.str();
+        }
+
+    private:
+        Flag mFlags;
+        int32_t mReplaceId;
+    }; // Operation
+
+    // must match with VolumeShaper.java in frameworks/base
+    class State : public RefBase {
+    public:
+        explicit State(T volume, S xOffset)
+            : mVolume(volume)
+            , mXOffset(xOffset) {
+        }
+
+        State()
+            : State(-1.f, -1.f) { }
+
+        T getVolume() const {
+            return mVolume;
+        }
+
+        void setVolume(T volume) {
+            mVolume = volume;
+        }
+
+        S getXOffset() const {
+            return mXOffset;
+        }
+
+        void setXOffset(S xOffset) {
+            mXOffset = xOffset;
+        }
+
+        status_t writeToParcel(Parcel *parcel) const {
+            if (parcel == nullptr) return BAD_VALUE;
+            return parcel->writeFloat(mVolume)
+                    ?: parcel->writeFloat(mXOffset);
+        }
+
+        status_t readFromParcel(const Parcel &parcel) {
+            return parcel.readFloat(&mVolume)
+                     ?: parcel.readFloat(&mXOffset);
+        }
+
+        std::string toString() const {
+            std::stringstream ss;
+            ss << "mVolume: " << mVolume << std::endl;
+            ss << "mXOffset: " << mXOffset << std::endl;
+            return ss.str();
+        }
+
+    private:
+        T mVolume;
+        S mXOffset;
+    }; // State
+
+    template <typename R>
+    class Translate {
+    public:
+        Translate()
+            : mOffset(0)
+            , mScale(1) {
+        }
+
+        R getOffset() const {
+            return mOffset;
+        }
+
+        void setOffset(R offset) {
+            mOffset = offset;
+        }
+
+        R getScale() const {
+            return mScale;
+        }
+
+        void setScale(R scale) {
+            mScale = scale;
+        }
+
+        R operator()(R in) const {
+            return mScale * (in - mOffset);
+        }
+
+        std::string toString() const {
+            std::stringstream ss;
+            ss << "mOffset: " << mOffset << std::endl;
+            ss << "mScale: " << mScale << std::endl;
+            return ss.str();
+        }
+
+    private:
+        R mOffset;
+        R mScale;
+    }; // Translate
+
+    static int64_t convertTimespecToUs(const struct timespec &tv)
+    {
+        return tv.tv_sec * 1000000ll + tv.tv_nsec / 1000;
+    }
+
+    // current monotonic time in microseconds.
+    static int64_t getNowUs()
+    {
+        struct timespec tv;
+        if (clock_gettime(CLOCK_MONOTONIC, &tv) != 0) {
+            return 0; // system is really sick, just return 0 for consistency.
+        }
+        return convertTimespecToUs(tv);
+    }
+
+    Translate<S> mXTranslate;
+    Translate<T> mYTranslate;
+    sp<VolumeShaper::Configuration> mConfiguration;
+    sp<VolumeShaper::Operation> mOperation;
+    int64_t mStartFrame;
+    T mLastVolume;
+    S mXOffset;
+
+    // TODO: Since we pass configuration and operation as shared pointers
+    // there is a potential risk that the caller may modify these after
+    // delivery.  Currently, we don't require copies made here.
+    explicit VolumeShaper(
+            const sp<VolumeShaper::Configuration> &configuration,
+            const sp<VolumeShaper::Operation> &operation)
+        : mConfiguration(configuration) // we do not make a copy
+        , mOperation(operation)         // ditto
+        , mStartFrame(-1)
+        , mLastVolume(T(1))
+        , mXOffset(0.f) {
+        if (configuration.get() != nullptr
+                && (getFlags() & VolumeShaper::Operation::FLAG_DELAY) == 0) {
+            mLastVolume = configuration->first().second;
+        }
+    }
+
+    void updatePosition(int64_t startFrame, double sampleRate) {
+        double scale = (mConfiguration->last().first - mConfiguration->first().first)
+                        / (mConfiguration->getDurationMs() * 0.001 * sampleRate);
+        const double minScale = 1. / INT64_MAX;
+        scale = std::max(scale, minScale);
+        VS_LOG("update position: scale %lf  frameCount:%lld, sampleRate:%lf",
+                scale, (long long) startFrame, sampleRate);
+        mXTranslate.setOffset(startFrame - mConfiguration->first().first / scale);
+        mXTranslate.setScale(scale);
+        VS_LOG("translate: %s", mXTranslate.toString().c_str());
+    }
+
+    // We allow a null operation here, though VolumeHandler always provides one.
+    VolumeShaper::Operation::Flag getFlags() const {
+        return mOperation == nullptr
+                ? VolumeShaper::Operation::FLAG_NONE :mOperation->getFlags();
+    }
+
+    sp<VolumeShaper::State> getState() const {
+        return new VolumeShaper::State(mLastVolume, mXOffset);
+    }
+
+    std::pair<T, bool> getVolume(int64_t trackFrameCount, double trackSampleRate) {
+        if (mConfiguration.get() == nullptr || mConfiguration->empty()) {
+            ALOGE("nonexistent VolumeShaper, removing");
+            mLastVolume = T(1);
+            mXOffset = 0.f;
+            return std::make_pair(T(1), true);
+        }
+        if ((getFlags() & VolumeShaper::Operation::FLAG_DELAY) != 0) {
+            VS_LOG("delayed VolumeShaper, ignoring");
+            mLastVolume = T(1);
+            mXOffset = 0.;
+            return std::make_pair(T(1), false);
+        }
+        const bool clockTime = (mConfiguration->getOptionFlags()
+                & VolumeShaper::Configuration::OPTION_FLAG_CLOCK_TIME) != 0;
+        const int64_t frameCount = clockTime ? getNowUs() : trackFrameCount;
+        const double sampleRate = clockTime ? 1000000 : trackSampleRate;
+
+        if (mStartFrame < 0) {
+            updatePosition(frameCount, sampleRate);
+            mStartFrame = frameCount;
+        }
+        VS_LOG("frameCount: %lld", (long long)frameCount);
+        S x = mXTranslate((T)frameCount);
+        VS_LOG("translation: %f", x);
+
+        // handle reversal of position
+        if (getFlags() & VolumeShaper::Operation::FLAG_REVERSE) {
+            x = 1.f - x;
+            VS_LOG("reversing to %f", x);
+            if (x < mConfiguration->first().first) {
+                mXOffset = 1.f;
+                const T volume = mConfiguration->adjustVolume(
+                        mConfiguration->first().second);  // persist last value
+                VS_LOG("persisting volume %f", volume);
+                mLastVolume = volume;
+                return std::make_pair(volume, false);
+            }
+            if (x > mConfiguration->last().first) {
+                mXOffset = 0.f;
+                mLastVolume = 1.f;
+                return std::make_pair(T(1), false); // too early
+            }
+        } else {
+            if (x < mConfiguration->first().first) {
+                mXOffset = 0.f;
+                mLastVolume = 1.f;
+                return std::make_pair(T(1), false); // too early
+            }
+            if (x > mConfiguration->last().first) {
+                mXOffset = 1.f;
+                const T volume = mConfiguration->adjustVolume(
+                        mConfiguration->last().second);  // persist last value
+                VS_LOG("persisting volume %f", volume);
+                mLastVolume = volume;
+                return std::make_pair(volume, false);
+            }
+        }
+        mXOffset = x;
+        // x contains the location on the volume curve to use.
+        const T unscaledVolume = mConfiguration->findY(x);
+        const T volumeChange = mYTranslate(unscaledVolume);
+        const T volume = mConfiguration->adjustVolume(volumeChange);
+        VS_LOG("volume: %f  unscaled: %f", volume, unscaledVolume);
+        mLastVolume = volume;
+        return std::make_pair(volume, false);
+    }
+
+    std::string toString() const {
+        std::stringstream ss;
+        ss << "StartFrame: " << mStartFrame << std::endl;
+        ss << mXTranslate.toString().c_str();
+        ss << mYTranslate.toString().c_str();
+        if (mConfiguration.get() == nullptr) {
+            ss << "VolumeShaper::Configuration: nullptr" << std::endl;
+        } else {
+            ss << "VolumeShaper::Configuration:" << std::endl;
+            ss << mConfiguration->toString().c_str();
+        }
+        if (mOperation.get() == nullptr) {
+            ss << "VolumeShaper::Operation: nullptr" << std::endl;
+        } else {
+            ss << "VolumeShaper::Operation:" << std::endl;
+            ss << mOperation->toString().c_str();
+        }
+        return ss.str();
+    }
+}; // VolumeShaper
+
+// VolumeHandler combines the volume factors of multiple VolumeShapers and handles
+// multiple thread access by synchronizing all public methods.
+class VolumeHandler : public RefBase {
+public:
+    using S = float;
+    using T = float;
+
+    explicit VolumeHandler(uint32_t sampleRate)
+        : mSampleRate((double)sampleRate)
+        , mLastFrame(0) {
+    }
+
+    VolumeShaper::Status applyVolumeShaper(
+            const sp<VolumeShaper::Configuration> &configuration,
+            const sp<VolumeShaper::Operation> &operation) {
+        AutoMutex _l(mLock);
+        if (configuration == nullptr) {
+            ALOGE("null configuration");
+            return VolumeShaper::Status(BAD_VALUE);
+        }
+        if (operation == nullptr) {
+            ALOGE("null operation");
+            return VolumeShaper::Status(BAD_VALUE);
+        }
+        const int32_t id = configuration->getId();
+        if (id < 0) {
+            ALOGE("negative id: %d", id);
+            return VolumeShaper::Status(BAD_VALUE);
+        }
+        VS_LOG("applyVolumeShaper id: %d", id);
+
+        switch (configuration->getType()) {
+        case VolumeShaper::Configuration::TYPE_ID: {
+            VS_LOG("trying to find id: %d", id);
+            auto it = findId_l(id);
+            if (it == mVolumeShapers.end()) {
+                VS_LOG("couldn't find id: %d\n%s", id, this->toString().c_str());
+                return VolumeShaper::Status(INVALID_OPERATION);
+            }
+            if ((it->getFlags() & VolumeShaper::Operation::FLAG_TERMINATE) != 0) {
+                VS_LOG("terminate id: %d", id);
+                mVolumeShapers.erase(it);
+                break;
+            }
+            if ((it->getFlags() & VolumeShaper::Operation::FLAG_REVERSE) !=
+                    (operation->getFlags() & VolumeShaper::Operation::FLAG_REVERSE)) {
+                const S x = it->mXTranslate((T)mLastFrame);
+                VS_LOG("translation: %f", x);
+                // reflect position
+                S target = 1.f - x;
+                if (target < it->mConfiguration->first().first) {
+                    VS_LOG("clamp to start - begin immediately");
+                    target = 0.;
+                }
+                VS_LOG("target: %f", target);
+                it->mXTranslate.setOffset(it->mXTranslate.getOffset()
+                        + (x - target) / it->mXTranslate.getScale());
+            }
+            it->mOperation = operation; // replace the operation
+        } break;
+        case VolumeShaper::Configuration::TYPE_SCALE: {
+            const int replaceId = operation->getReplaceId();
+            if (replaceId >= 0) {
+                auto replaceIt = findId_l(replaceId);
+                if (replaceIt == mVolumeShapers.end()) {
+                    ALOGW("cannot find replace id: %d", replaceId);
+                } else {
+                    if ((replaceIt->getFlags() & VolumeShaper::Operation::FLAG_JOIN) != 0) {
+                        // For join, we scale the start volume of the current configuration
+                        // to match the last-used volume of the replacing VolumeShaper.
+                        auto state = replaceIt->getState();
+                        if (state->getXOffset() >= 0) { // valid
+                            const T volume = state->getVolume();
+                            ALOGD("join: scaling start volume to %f", volume);
+                            configuration->scaleToStartVolume(volume);
+                        }
+                    }
+                    (void)mVolumeShapers.erase(replaceIt);
+                }
+            }
+            // check if we have another of the same id.
+            auto oldIt = findId_l(id);
+            if (oldIt != mVolumeShapers.end()) {
+                ALOGW("duplicate id, removing old %d", id);
+                (void)mVolumeShapers.erase(oldIt);
+            }
+            // create new VolumeShaper
+            mVolumeShapers.emplace_back(configuration, operation);
+        } break;
+        }
+        return VolumeShaper::Status(id);
+    }
+
+    sp<VolumeShaper::State> getVolumeShaperState(int id) {
+        AutoMutex _l(mLock);
+        auto it = findId_l(id);
+        if (it == mVolumeShapers.end()) {
+            return nullptr;
+        }
+        return it->getState();
+    }
+
+    T getVolume(int64_t trackFrameCount) {
+        AutoMutex _l(mLock);
+        mLastFrame = trackFrameCount;
+        T volume(1);
+        for (auto it = mVolumeShapers.begin(); it != mVolumeShapers.end();) {
+            std::pair<T, bool> shaperVolume =
+                    it->getVolume(trackFrameCount, mSampleRate);
+            volume *= shaperVolume.first;
+            if (shaperVolume.second) {
+                it = mVolumeShapers.erase(it);
+                continue;
+            }
+            ++it;
+        }
+        return volume;
+    }
+
+    std::string toString() const {
+        AutoMutex _l(mLock);
+        std::stringstream ss;
+        ss << "mSampleRate: " << mSampleRate << std::endl;
+        ss << "mLastFrame: " << mLastFrame << std::endl;
+        for (const auto &shaper : mVolumeShapers) {
+            ss << shaper.toString().c_str();
+        }
+        return ss.str();
+    }
+
+private:
+    std::list<VolumeShaper>::iterator findId_l(int32_t id) {
+        std::list<VolumeShaper>::iterator it = mVolumeShapers.begin();
+        for (; it != mVolumeShapers.end(); ++it) {
+            if (it->mConfiguration->getId() == id) {
+                break;
+            }
+        }
+        return it;
+    }
+
+    mutable Mutex mLock;
+    double mSampleRate; // in samples (frames) per second
+    int64_t mLastFrame; // logging purpose only
+    std::list<VolumeShaper> mVolumeShapers; // list provides stable iterators on erase
+}; // VolumeHandler
+
+} // namespace android
+
+#pragma pop_macro("LOG_TAG")
+
+#endif // ANDROID_VOLUME_SHAPER_H
diff --git a/include/media/mediaplayer.h b/include/media/mediaplayer.h
index 2c5ff1f..fbe3926 100644
--- a/include/media/mediaplayer.h
+++ b/include/media/mediaplayer.h
@@ -261,6 +261,11 @@
             status_t        getParameter(int key, Parcel* reply);
             status_t        setRetransmitEndpoint(const char* addrString, uint16_t port);
             status_t        setNextMediaPlayer(const sp<MediaPlayer>& player);
+
+            VolumeShaper::Status applyVolumeShaper(
+                                    const sp<VolumeShaper::Configuration>& configuration,
+                                    const sp<VolumeShaper::Operation>& operation);
+            sp<VolumeShaper::State> getVolumeShaperState(int id);
             // ModDrm
             status_t        prepareDrm(const uint8_t uuid[16], const int mode);
             status_t        releaseDrm();
diff --git a/media/libaudioclient/AudioTrack.cpp b/media/libaudioclient/AudioTrack.cpp
index 0de3559..d35cfe3 100644
--- a/media/libaudioclient/AudioTrack.cpp
+++ b/media/libaudioclient/AudioTrack.cpp
@@ -184,6 +184,7 @@
       mPreviousSchedulingGroup(SP_DEFAULT),
       mPausedPosition(0),
       mSelectedDeviceId(AUDIO_PORT_HANDLE_NONE),
+      mVolumeShaperId(VolumeShaper::kSystemIdMax),
       mPortId(AUDIO_PORT_HANDLE_NONE)
 {
     mAttributes.content_type = AUDIO_CONTENT_TYPE_UNKNOWN;
@@ -556,7 +557,7 @@
     mFramesWritten = 0;
     mFramesWrittenServerOffset = 0;
     mFramesWrittenAtRestore = -1; // -1 is a unique initializer.
-
+    mVolumeHandler = new VolumeHandler(mSampleRate);
     return NO_ERROR;
 }
 
@@ -2310,6 +2311,40 @@
     return mAudioTrack->setParameters(keyValuePairs);
 }
 
+VolumeShaper::Status AudioTrack::applyVolumeShaper(
+        const sp<VolumeShaper::Configuration>& configuration,
+        const sp<VolumeShaper::Operation>& operation)
+{
+    AutoMutex lock(mLock);
+    if (configuration->getType() == VolumeShaper::Configuration::TYPE_SCALE) {
+        const int id = configuration->getId();
+        LOG_ALWAYS_FATAL_IF(id >= VolumeShaper::kSystemIdMax || id < -1,
+                "id must be -1 or a system id (less than kSystemIdMax)");
+        if (id == -1) {
+            // if not a system id, reassign to a unique id
+            configuration->setId(mVolumeShaperId);
+            ALOGD("setting id to %d", mVolumeShaperId);
+            // increment and avoid signed overflow.
+            if (mVolumeShaperId == INT32_MAX) {
+                mVolumeShaperId = VolumeShaper::kSystemIdMax;
+            } else {
+                ++mVolumeShaperId;
+            }
+        }
+    }
+    VolumeShaper::Status status = mAudioTrack->applyVolumeShaper(configuration, operation);
+    // TODO: For restoration purposes, record successful creation and termination.
+    return status;
+}
+
+sp<VolumeShaper::State> AudioTrack::getVolumeShaperState(int id)
+{
+    // TODO: To properly restore the AudioTrack
+    // we will need to save the last state in AudioTrackShared.
+    AutoMutex lock(mLock);
+    return mAudioTrack->getVolumeShaperState(id);
+}
+
 status_t AudioTrack::getTimestamp(ExtendedTimestamp *timestamp)
 {
     if (timestamp == nullptr) {
diff --git a/media/libaudioclient/IAudioTrack.cpp b/media/libaudioclient/IAudioTrack.cpp
index 89e0fcc..79e864d 100644
--- a/media/libaudioclient/IAudioTrack.cpp
+++ b/media/libaudioclient/IAudioTrack.cpp
@@ -39,6 +39,8 @@
     SET_PARAMETERS,
     GET_TIMESTAMP,
     SIGNAL,
+    APPLY_VOLUME_SHAPER,
+    GET_VOLUME_SHAPER_STATE,
 };
 
 class BpAudioTrack : public BpInterface<IAudioTrack>
@@ -143,6 +145,52 @@
         data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor());
         remote()->transact(SIGNAL, data, &reply);
     }
+
+    virtual VolumeShaper::Status applyVolumeShaper(
+            const sp<VolumeShaper::Configuration>& configuration,
+            const sp<VolumeShaper::Operation>& operation) {
+        Parcel data, reply;
+        data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor());
+
+        status_t status = configuration.get() == nullptr
+                ? data.writeInt32(0)
+                :  data.writeInt32(1)
+                    ?: configuration->writeToParcel(&data);
+        if (status != NO_ERROR) {
+            return VolumeShaper::Status(status);
+        }
+
+        status = operation.get() == nullptr
+                ? status = data.writeInt32(0)
+                : data.writeInt32(1)
+                    ?: operation->writeToParcel(&data);
+        if (status != NO_ERROR) {
+            return VolumeShaper::Status(status);
+        }
+
+        int32_t remoteVolumeShaperStatus;
+        status = remote()->transact(APPLY_VOLUME_SHAPER, data, &reply)
+                 ?: reply.readInt32(&remoteVolumeShaperStatus);
+
+        return VolumeShaper::Status(status ?: remoteVolumeShaperStatus);
+    }
+
+    virtual sp<VolumeShaper::State> getVolumeShaperState(int id) {
+        Parcel data, reply;
+        data.writeInterfaceToken(IAudioTrack::getInterfaceDescriptor());
+
+        data.writeInt32(id);
+        status_t status = remote()->transact(GET_VOLUME_SHAPER_STATE, data, &reply);
+        if (status != NO_ERROR) {
+            return nullptr;
+        }
+        sp<VolumeShaper::State> state = new VolumeShaper::State;
+        status = state->readFromParcel(reply);
+        if (status != NO_ERROR) {
+            return nullptr;
+        }
+        return state;
+    }
 };
 
 IMPLEMENT_META_INTERFACE(AudioTrack, "android.media.IAudioTrack");
@@ -206,6 +254,40 @@
             signal();
             return NO_ERROR;
         } break;
+        case APPLY_VOLUME_SHAPER: {
+            CHECK_INTERFACE(IAudioTrack, data, reply);
+            sp<VolumeShaper::Configuration> configuration;
+            sp<VolumeShaper::Operation> operation;
+
+            int32_t present;
+            status_t status = data.readInt32(&present);
+            if (status == NO_ERROR && present != 0) {
+                configuration = new VolumeShaper::Configuration();
+                status = configuration->readFromParcel(data);
+            }
+            status = status ?: data.readInt32(&present);
+            if (status == NO_ERROR && present != 0) {
+                operation = new VolumeShaper::Operation();
+                status = operation->readFromParcel(data);
+            }
+            if (status == NO_ERROR) {
+                status = (status_t)applyVolumeShaper(configuration, operation);
+            }
+            reply->writeInt32(status);
+            return NO_ERROR;
+        } break;
+        case GET_VOLUME_SHAPER_STATE: {
+            CHECK_INTERFACE(IAudioTrack, data, reply);
+            int id;
+            status_t status = data.readInt32(&id);
+            if (status == NO_ERROR) {
+                sp<VolumeShaper::State> state = getVolumeShaperState(id);
+                if (state.get() != nullptr) {
+                     status = state->writeToParcel(reply);
+                }
+            }
+            return NO_ERROR;
+        } break;
         default:
             return BBinder::onTransact(code, data, reply, flags);
     }
diff --git a/media/libmedia/IMediaPlayer.cpp b/media/libmedia/IMediaPlayer.cpp
index 966267a..5222a42 100644
--- a/media/libmedia/IMediaPlayer.cpp
+++ b/media/libmedia/IMediaPlayer.cpp
@@ -70,6 +70,8 @@
     SET_RETRANSMIT_ENDPOINT,
     GET_RETRANSMIT_ENDPOINT,
     SET_NEXT_PLAYER,
+    APPLY_VOLUME_SHAPER,
+    GET_VOLUME_SHAPER_STATE,
     // ModDrm
     PREPARE_DRM,
     RELEASE_DRM,
@@ -468,6 +470,57 @@
         return err;
     }
 
+    virtual VolumeShaper::Status applyVolumeShaper(
+            const sp<VolumeShaper::Configuration>& configuration,
+            const sp<VolumeShaper::Operation>& operation) {
+        Parcel data, reply;
+        data.writeInterfaceToken(IMediaPlayer::getInterfaceDescriptor());
+
+        status_t tmp;
+        status_t status = configuration.get() == nullptr
+                ? data.writeInt32(0)
+                : (tmp = data.writeInt32(1)) != NO_ERROR
+                    ? tmp : configuration->writeToParcel(&data);
+        if (status != NO_ERROR) {
+            return VolumeShaper::Status(status);
+        }
+
+        status = operation.get() == nullptr
+                ? status = data.writeInt32(0)
+                : (tmp = data.writeInt32(1)) != NO_ERROR
+                    ? tmp : operation->writeToParcel(&data);
+        if (status != NO_ERROR) {
+            return VolumeShaper::Status(status);
+        }
+
+        int32_t remoteVolumeShaperStatus;
+        status = remote()->transact(APPLY_VOLUME_SHAPER, data, &reply);
+        if (status == NO_ERROR) {
+            status = reply.readInt32(&remoteVolumeShaperStatus);
+        }
+        if (status != NO_ERROR) {
+            return VolumeShaper::Status(status);
+        }
+        return VolumeShaper::Status(remoteVolumeShaperStatus);
+    }
+
+    virtual sp<VolumeShaper::State> getVolumeShaperState(int id) {
+        Parcel data, reply;
+        data.writeInterfaceToken(IMediaPlayer::getInterfaceDescriptor());
+
+        data.writeInt32(id);
+        status_t status = remote()->transact(GET_VOLUME_SHAPER_STATE, data, &reply);
+        if (status != NO_ERROR) {
+            return nullptr;
+        }
+        sp<VolumeShaper::State> state = new VolumeShaper::State();
+        status = state->readFromParcel(reply);
+        if (status != NO_ERROR) {
+            return nullptr;
+        }
+        return state;
+    }
+
     // ModDrm
     status_t prepareDrm(const uint8_t uuid[16], const int mode)
     {
@@ -893,6 +946,43 @@
             return NO_ERROR;
         } break;
 
+        case APPLY_VOLUME_SHAPER: {
+            CHECK_INTERFACE(IMediaPlayer, data, reply);
+            sp<VolumeShaper::Configuration> configuration;
+            sp<VolumeShaper::Operation> operation;
+
+            int32_t present;
+            status_t status = data.readInt32(&present);
+            if (status == NO_ERROR && present != 0) {
+                configuration = new VolumeShaper::Configuration();
+                status = configuration->readFromParcel(data);
+            }
+            if (status == NO_ERROR) {
+                status = data.readInt32(&present);
+            }
+            if (status == NO_ERROR && present != 0) {
+                operation = new VolumeShaper::Operation();
+                status = operation->readFromParcel(data);
+            }
+            if (status == NO_ERROR) {
+                status = (status_t)applyVolumeShaper(configuration, operation);
+            }
+            reply->writeInt32(status);
+            return NO_ERROR;
+        } break;
+        case GET_VOLUME_SHAPER_STATE: {
+            CHECK_INTERFACE(IMediaPlayer, data, reply);
+            int id;
+            status_t status = data.readInt32(&id);
+            if (status == NO_ERROR) {
+                sp<VolumeShaper::State> state = getVolumeShaperState(id);
+                if (state.get() != nullptr) {
+                     status = state->writeToParcel(reply);
+                }
+            }
+            return NO_ERROR;
+        } break;
+
         // ModDrm
         case PREPARE_DRM: {
             CHECK_INTERFACE(IMediaPlayer, data, reply);
diff --git a/media/libmedia/mediaplayer.cpp b/media/libmedia/mediaplayer.cpp
index 2feb035..dfc2e1b 100644
--- a/media/libmedia/mediaplayer.cpp
+++ b/media/libmedia/mediaplayer.cpp
@@ -991,6 +991,27 @@
     return mPlayer->setNextPlayer(next == NULL ? NULL : next->mPlayer);
 }
 
+VolumeShaper::Status MediaPlayer::applyVolumeShaper(
+        const sp<VolumeShaper::Configuration>& configuration,
+        const sp<VolumeShaper::Operation>& operation)
+{
+    Mutex::Autolock _l(mLock);
+    if (mPlayer == nullptr) {
+        return VolumeShaper::Status(NO_INIT);
+    }
+    VolumeShaper::Status status = mPlayer->applyVolumeShaper(configuration, operation);
+    return status;
+}
+
+sp<VolumeShaper::State> MediaPlayer::getVolumeShaperState(int id)
+{
+    Mutex::Autolock _l(mLock);
+    if (mPlayer == nullptr) {
+        return nullptr;
+    }
+    return mPlayer->getVolumeShaperState(id);
+}
+
 // ModDrm
 status_t MediaPlayer::prepareDrm(const uint8_t uuid[16], const int mode)
 {
diff --git a/media/libmediaplayerservice/MediaPlayerService.cpp b/media/libmediaplayerservice/MediaPlayerService.cpp
index 2d4c475..cdae456 100644
--- a/media/libmediaplayerservice/MediaPlayerService.cpp
+++ b/media/libmediaplayerservice/MediaPlayerService.cpp
@@ -1147,6 +1147,42 @@
     return OK;
 }
 
+VolumeShaper::Status MediaPlayerService::Client::applyVolumeShaper(
+        const sp<VolumeShaper::Configuration>& configuration,
+        const sp<VolumeShaper::Operation>& operation) {
+    // for hardware output, call player instead
+    ALOGV("Client::applyVolumeShaper(%p)", this);
+    sp<MediaPlayerBase> p = getPlayer();
+    {
+        Mutex::Autolock l(mLock);
+        if (p != 0 && p->hardwareOutput()) {
+            // TODO: investigate internal implementation
+            return VolumeShaper::Status(INVALID_OPERATION);
+        }
+        if (mAudioOutput.get() != nullptr) {
+            return mAudioOutput->applyVolumeShaper(configuration, operation);
+        }
+    }
+    return VolumeShaper::Status(INVALID_OPERATION);
+}
+
+sp<VolumeShaper::State> MediaPlayerService::Client::getVolumeShaperState(int id) {
+    // for hardware output, call player instead
+    ALOGV("Client::getVolumeShaperState(%p)", this);
+    sp<MediaPlayerBase> p = getPlayer();
+    {
+        Mutex::Autolock l(mLock);
+        if (p != 0 && p->hardwareOutput()) {
+            // TODO: investigate internal implementation.
+            return nullptr;
+        }
+        if (mAudioOutput.get() != nullptr) {
+            return mAudioOutput->getVolumeShaperState(id);
+        }
+    }
+    return nullptr;
+}
+
 status_t MediaPlayerService::Client::seekTo(int msec, MediaPlayerSeekMode mode)
 {
     ALOGV("[%d] seekTo(%d, %d)", mConnId, msec, mode);
@@ -2146,6 +2182,27 @@
     return NO_ERROR;
 }
 
+VolumeShaper::Status MediaPlayerService::AudioOutput::applyVolumeShaper(
+                const sp<VolumeShaper::Configuration>& configuration,
+                const sp<VolumeShaper::Operation>& operation)
+{
+    Mutex::Autolock lock(mLock);
+    ALOGV("AudioOutput::applyVolumeShaper");
+    if (mTrack != 0) {
+        return mTrack->applyVolumeShaper(configuration, operation);
+    }
+    return VolumeShaper::Status(INVALID_OPERATION);
+}
+
+sp<VolumeShaper::State> MediaPlayerService::AudioOutput::getVolumeShaperState(int id)
+{
+    Mutex::Autolock lock(mLock);
+    if (mTrack != 0) {
+        return mTrack->getVolumeShaperState(id);
+    }
+    return nullptr;
+}
+
 // static
 void MediaPlayerService::AudioOutput::CallbackWrapper(
         int event, void *cookie, void *info) {
diff --git a/media/libmediaplayerservice/MediaPlayerService.h b/media/libmediaplayerservice/MediaPlayerService.h
index 819973e..cbaf21c 100644
--- a/media/libmediaplayerservice/MediaPlayerService.h
+++ b/media/libmediaplayerservice/MediaPlayerService.h
@@ -129,6 +129,11 @@
         virtual status_t        setParameters(const String8& keyValuePairs);
         virtual String8         getParameters(const String8& keys);
 
+        virtual VolumeShaper::Status applyVolumeShaper(
+                                        const sp<VolumeShaper::Configuration>& configuration,
+                                        const sp<VolumeShaper::Operation>& operation) override;
+        virtual sp<VolumeShaper::State> getVolumeShaperState(int id) override;
+
     private:
         static void             setMinBufferCount();
         static void             CallbackWrapper(
@@ -323,6 +328,11 @@
         virtual status_t        getRetransmitEndpoint(struct sockaddr_in* endpoint);
         virtual status_t        setNextPlayer(const sp<IMediaPlayer>& player);
 
+        virtual VolumeShaper::Status applyVolumeShaper(
+                                        const sp<VolumeShaper::Configuration>& configuration,
+                                        const sp<VolumeShaper::Operation>& operation) override;
+        virtual sp<VolumeShaper::State> getVolumeShaperState(int id) override;
+
         sp<MediaPlayerBase>     createPlayer(player_type playerType);
 
         virtual status_t        setDataSource(
diff --git a/services/audioflinger/AudioFlinger.h b/services/audioflinger/AudioFlinger.h
index e97d1ed..fa4f68a 100644
--- a/services/audioflinger/AudioFlinger.h
+++ b/services/audioflinger/AudioFlinger.h
@@ -54,6 +54,7 @@
 #include <media/AudioMixer.h>
 #include <media/ExtendedAudioBufferProvider.h>
 #include <media/LinearMap.h>
+#include <media/VolumeShaper.h>
 
 #include "FastCapture.h"
 #include "FastMixer.h"
@@ -506,6 +507,10 @@
         virtual void        pause();
         virtual status_t    attachAuxEffect(int effectId);
         virtual status_t    setParameters(const String8& keyValuePairs);
+        virtual VolumeShaper::Status applyVolumeShaper(
+                const sp<VolumeShaper::Configuration>& configuration,
+                const sp<VolumeShaper::Operation>& operation) override;
+        virtual sp<VolumeShaper::State> getVolumeShaperState(int id) override;
         virtual status_t    getTimestamp(AudioTimestamp& timestamp);
         virtual void        signal(); // signal playback thread for a change in control block
 
diff --git a/services/audioflinger/PlaybackTracks.h b/services/audioflinger/PlaybackTracks.h
index 27e4627..02cac38 100644
--- a/services/audioflinger/PlaybackTracks.h
+++ b/services/audioflinger/PlaybackTracks.h
@@ -76,6 +76,13 @@
 
     virtual bool        isFastTrack() const { return (mFlags & AUDIO_OUTPUT_FLAG_FAST) != 0; }
 
+// implement volume handling.
+   VolumeShaper::Status applyVolumeShaper(
+                                const sp<VolumeShaper::Configuration>& configuration,
+                                const sp<VolumeShaper::Operation>& operation);
+sp<VolumeShaper::State> getVolumeShaperState(int id);
+    sp<VolumeHandler>   getVolumeHandler() { return mVolumeHandler; }
+
 protected:
     // for numerous
     friend class PlaybackThread;
@@ -153,6 +160,8 @@
 
     ExtendedTimestamp  mSinkTimestamp;
 
+    sp<VolumeHandler>  mVolumeHandler; // handles multiple VolumeShaper configs and operations
+
 private:
     // The following fields are only for fast tracks, and should be in a subclass
     int                 mFastIndex; // index within FastMixerState::mFastTracks[];
diff --git a/services/audioflinger/Threads.cpp b/services/audioflinger/Threads.cpp
index a1d81f9..3ba7881 100644
--- a/services/audioflinger/Threads.cpp
+++ b/services/audioflinger/Threads.cpp
@@ -4114,7 +4114,11 @@
                 }
                 // cache the combined master volume and stream type volume for fast mixer; this
                 // lacks any synchronization or barrier so VolumeProvider may read a stale value
-                track->mCachedVolume = masterVolume * mStreamTypes[track->streamType()].volume;
+                const float vh = track->getVolumeHandler()->getVolume(
+                        track->mAudioTrackServerProxy->framesReleased());
+                track->mCachedVolume = masterVolume
+                        * mStreamTypes[track->streamType()].volume
+                        * vh;
                 ++fastTracks;
             } else {
                 // was it previously active?
@@ -4256,9 +4260,11 @@
                     ALOGV("Track right volume out of range: %.3g", vrf);
                     vrf = GAIN_FLOAT_UNITY;
                 }
-                // now apply the master volume and stream type volume
-                vlf *= v;
-                vrf *= v;
+                const float vh = track->getVolumeHandler()->getVolume(
+                        track->mAudioTrackServerProxy->framesReleased());
+                // now apply the master volume and stream type volume and shaper volume
+                vlf *= v * vh;
+                vrf *= v * vh;
                 // assuming master volume and stream type volume each go up to 1.0,
                 // then derive vl and vr as U8.24 versions for the effect chain
                 const float scaleto8_24 = MAX_GAIN_INT * MAX_GAIN_INT;
@@ -4743,6 +4749,15 @@
         float typeVolume = mStreamTypes[track->streamType()].volume;
         float v = mMasterVolume * typeVolume;
         sp<AudioTrackServerProxy> proxy = track->mAudioTrackServerProxy;
+
+        if (audio_is_linear_pcm(mFormat) && !usesHwAvSync()) {
+            const float vh = track->getVolumeHandler()->getVolume(
+                    track->mAudioTrackServerProxy->framesReleased());
+            v *= vh;
+        } else {
+            // TODO: implement volume scaling in HW
+        }
+
         gain_minifloat_packed_t vlr = proxy->getVolumeLR();
         left = float_from_gain(gain_minifloat_unpack_left(vlr));
         if (left > GAIN_FLOAT_UNITY) {
diff --git a/services/audioflinger/Tracks.cpp b/services/audioflinger/Tracks.cpp
index f2dd884..b772b5e 100644
--- a/services/audioflinger/Tracks.cpp
+++ b/services/audioflinger/Tracks.cpp
@@ -312,6 +312,16 @@
     return mTrack->setParameters(keyValuePairs);
 }
 
+VolumeShaper::Status AudioFlinger::TrackHandle::applyVolumeShaper(
+        const sp<VolumeShaper::Configuration>& configuration,
+        const sp<VolumeShaper::Operation>& operation) {
+    return mTrack->applyVolumeShaper(configuration, operation);
+}
+
+sp<VolumeShaper::State> AudioFlinger::TrackHandle::getVolumeShaperState(int id) {
+    return mTrack->getVolumeShaperState(id);
+}
+
 status_t AudioFlinger::TrackHandle::getTimestamp(AudioTimestamp& timestamp)
 {
     return mTrack->getTimestamp(timestamp);
@@ -362,6 +372,7 @@
     mAuxEffectId(0), mHasVolumeController(false),
     mPresentationCompleteFrames(0),
     mFrameMap(16 /* sink-frame-to-track-frame map memory */),
+    mVolumeHandler(new VolumeHandler(sampleRate)),
     // mSinkTimestamp
     mFastIndex(-1),
     mCachedVolume(1.0),
@@ -874,6 +885,24 @@
     }
 }
 
+VolumeShaper::Status AudioFlinger::PlaybackThread::Track::applyVolumeShaper(
+        const sp<VolumeShaper::Configuration>& configuration,
+        const sp<VolumeShaper::Operation>& operation)
+{
+    // Note: We don't check if Thread exists.
+
+    // mVolumeHandler is thread-safe.
+    return mVolumeHandler->applyVolumeShaper(configuration, operation);
+}
+
+sp<VolumeShaper::State> AudioFlinger::PlaybackThread::Track::getVolumeShaperState(int id)
+{
+    // Note: We don't check if Thread exists.
+
+    // mVolumeHandler is thread safe.
+    return mVolumeHandler->getVolumeShaperState(id);
+}
+
 status_t AudioFlinger::PlaybackThread::Track::getTimestamp(AudioTimestamp& timestamp)
 {
     if (!isOffloaded() && !isDirect()) {