Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 78acced8 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge changes I1d2263f0,Idcf09587,I8bdcd0da

* changes:
  Add parcelable test for classes in libaudiofoundation.
  Add equals functions for classes in libaudiofoundation.
  Make audio stuff parcelable.
parents 5b9f07a2 8de018ad
Loading
Loading
Loading
Loading
+32 −7
Original line number Diff line number Diff line
@@ -24,6 +24,8 @@
#define ALOGVV(a...) do { } while(0)
#endif

#include <algorithm>

#include <android-base/stringprintf.h>
#include <media/AudioGain.h>
#include <utils/Log.h>
@@ -111,6 +113,22 @@ void AudioGain::dump(std::string *dst, int spaces, int index) const
    dst->append(base::StringPrintf("%*s- max_ramp_ms: %d ms\n", spaces, "", mGain.max_ramp_ms));
}

bool AudioGain::equals(const sp<AudioGain>& other) const
{
    return other != nullptr &&
           mUseInChannelMask == other->mUseInChannelMask &&
           mUseForVolume == other->mUseForVolume &&
           // Compare audio gain
           mGain.mode == other->mGain.mode &&
           mGain.channel_mask == other->mGain.channel_mask &&
           mGain.min_value == other->mGain.min_value &&
           mGain.max_value == other->mGain.max_value &&
           mGain.default_value == other->mGain.default_value &&
           mGain.step_value == other->mGain.step_value &&
           mGain.min_ramp_ms == other->mGain.min_ramp_ms &&
           mGain.max_ramp_ms == other->mGain.max_ramp_ms;
}

status_t AudioGain::writeToParcel(android::Parcel *parcel) const
{
    status_t status = NO_ERROR;
@@ -145,9 +163,17 @@ status_t AudioGain::readFromParcel(const android::Parcel *parcel)
    return status;
}

bool AudioGains::equals(const AudioGains &other) const
{
    return std::equal(begin(), end(), other.begin(), other.end(),
                      [](const sp<AudioGain>& left, const sp<AudioGain>& right) {
                          return left->equals(right);
                      });
}

status_t AudioGains::writeToParcel(android::Parcel *parcel) const {
    status_t status = NO_ERROR;
    if ((status = parcel->writeUint64(this->size())) != NO_ERROR) return status;
    if ((status = parcel->writeVectorSize(*this)) != NO_ERROR) return status;
    for (const auto &audioGain : *this) {
        if ((status = parcel->writeParcelable(*audioGain)) != NO_ERROR) {
            break;
@@ -158,15 +184,14 @@ status_t AudioGains::writeToParcel(android::Parcel *parcel) const {

status_t AudioGains::readFromParcel(const android::Parcel *parcel) {
    status_t status = NO_ERROR;
    uint64_t count;
    if ((status = parcel->readUint64(&count)) != NO_ERROR) return status;
    for (uint64_t i = 0; i < count; i++) {
        sp<AudioGain> audioGain = new AudioGain(0, false);
        if ((status = parcel->readParcelable(audioGain.get())) != NO_ERROR) {
    this->clear();
    if ((status = parcel->resizeOutVector(this)) != NO_ERROR) return status;
    for (size_t i = 0; i < this->size(); i++) {
        this->at(i) = new AudioGain(0, false);
        if ((status = parcel->readParcelable(this->at(i).get())) != NO_ERROR) {
            this->clear();
            break;
        }
        this->push_back(audioGain);
    }
    return status;
}
+96 −0
Original line number Diff line number Diff line
@@ -105,6 +105,46 @@ void AudioPort::log(const char* indent) const
    ALOGI("%s Port[nm:%s, type:%d, role:%d]", indent, mName.c_str(), mType, mRole);
}

bool AudioPort::equals(const sp<AudioPort> &other) const
{
    return other != nullptr &&
           mGains.equals(other->getGains()) &&
           mName.compare(other->getName()) == 0 &&
           mType == other->getType() &&
           mRole == other->getRole() &&
           mProfiles.equals(other->getAudioProfiles());
}

status_t AudioPort::writeToParcel(Parcel *parcel) const
{
    status_t status = NO_ERROR;
    if ((status = parcel->writeUtf8AsUtf16(mName)) != NO_ERROR) return status;
    if ((status = parcel->writeUint32(mType)) != NO_ERROR) return status;
    if ((status = parcel->writeUint32(mRole)) != NO_ERROR) return status;
    if ((status = parcel->writeParcelable(mProfiles)) != NO_ERROR) return status;
    if ((status = parcel->writeParcelable(mGains)) != NO_ERROR) return status;
    return status;
}

status_t AudioPort::readFromParcel(const Parcel *parcel)
{
    status_t status = NO_ERROR;
    if ((status = parcel->readUtf8FromUtf16(&mName)) != NO_ERROR) return status;
    static_assert(sizeof(mType) == sizeof(uint32_t));
    if ((status = parcel->readUint32(reinterpret_cast<uint32_t*>(&mType))) != NO_ERROR) {
        return status;
    }
    static_assert(sizeof(mRole) == sizeof(uint32_t));
    if ((status = parcel->readUint32(reinterpret_cast<uint32_t*>(&mRole))) != NO_ERROR) {
        return status;
    }
    mProfiles.clear();
    if ((status = parcel->readParcelable(&mProfiles)) != NO_ERROR) return status;
    mGains.clear();
    if ((status = parcel->readParcelable(&mGains)) != NO_ERROR) return status;
    return status;
}

// --- AudioPortConfig class implementation

status_t AudioPortConfig::applyAudioPortConfig(
@@ -188,4 +228,60 @@ bool AudioPortConfig::hasGainController(bool canUseForVolume) const
                           : audioport->getGains().size() > 0;
}

bool AudioPortConfig::equals(const sp<AudioPortConfig> &other) const
{
    return other != nullptr &&
           mSamplingRate == other->getSamplingRate() &&
           mFormat == other->getFormat() &&
           mChannelMask == other->getChannelMask() &&
           // Compare audio gain config
           mGain.index == other->mGain.index &&
           mGain.mode == other->mGain.mode &&
           mGain.channel_mask == other->mGain.channel_mask &&
           std::equal(std::begin(mGain.values), std::end(mGain.values),
                      std::begin(other->mGain.values)) &&
           mGain.ramp_duration_ms == other->mGain.ramp_duration_ms;
}

status_t AudioPortConfig::writeToParcel(Parcel *parcel) const
{
    status_t status = NO_ERROR;
    if ((status = parcel->writeUint32(mSamplingRate)) != NO_ERROR) return status;
    if ((status = parcel->writeUint32(mFormat)) != NO_ERROR) return status;
    if ((status = parcel->writeUint32(mChannelMask)) != NO_ERROR) return status;
    if ((status = parcel->writeInt32(mId)) != NO_ERROR) return status;
    // Write mGain to parcel.
    if ((status = parcel->writeInt32(mGain.index)) != NO_ERROR) return status;
    if ((status = parcel->writeUint32(mGain.mode)) != NO_ERROR) return status;
    if ((status = parcel->writeUint32(mGain.channel_mask)) != NO_ERROR) return status;
    if ((status = parcel->writeUint32(mGain.ramp_duration_ms)) != NO_ERROR) return status;
    std::vector<int> values(std::begin(mGain.values), std::end(mGain.values));
    if ((status = parcel->writeInt32Vector(values)) != NO_ERROR) return status;
    return status;
}

status_t AudioPortConfig::readFromParcel(const Parcel *parcel)
{
    status_t status = NO_ERROR;
    if ((status = parcel->readUint32(&mSamplingRate)) != NO_ERROR) return status;
    static_assert(sizeof(mFormat) == sizeof(uint32_t));
    if ((status = parcel->readUint32(reinterpret_cast<uint32_t*>(&mFormat))) != NO_ERROR) {
        return status;
    }
    if ((status = parcel->readUint32(&mChannelMask)) != NO_ERROR) return status;
    if ((status = parcel->readInt32(&mId)) != NO_ERROR) return status;
    // Read mGain from parcel.
    if ((status = parcel->readInt32(&mGain.index)) != NO_ERROR) return status;
    if ((status = parcel->readUint32(&mGain.mode)) != NO_ERROR) return status;
    if ((status = parcel->readUint32(&mGain.channel_mask)) != NO_ERROR) return status;
    if ((status = parcel->readUint32(&mGain.ramp_duration_ms)) != NO_ERROR) return status;
    std::vector<int> values;
    if ((status = parcel->readInt32Vector(&values)) != NO_ERROR) return status;
    if (values.size() != std::size(mGain.values)) {
        return BAD_VALUE;
    }
    std::copy(values.begin(), values.end(), mGain.values);
    return status;
}

} // namespace android
+85 −0
Original line number Diff line number Diff line
@@ -118,6 +118,56 @@ void AudioProfile::dump(std::string *dst, int spaces) const
    }
}

bool AudioProfile::equals(const sp<AudioProfile>& other) const
{
    return other != nullptr &&
           mName.compare(other->mName) == 0 &&
           mFormat == other->getFormat() &&
           mChannelMasks == other->getChannels() &&
           mSamplingRates == other->getSampleRates() &&
           mIsDynamicFormat == other->isDynamicFormat() &&
           mIsDynamicChannels == other->isDynamicChannels() &&
           mIsDynamicRate == other->isDynamicRate();
}

status_t AudioProfile::writeToParcel(Parcel *parcel) const
{
    status_t status = NO_ERROR;
    if ((status = parcel->writeUtf8AsUtf16(mName)) != NO_ERROR) return status;
    if ((status = parcel->writeUint32(mFormat)) != NO_ERROR) return status;
    std::vector<int> values(mChannelMasks.begin(), mChannelMasks.end());
    if ((status = parcel->writeInt32Vector(values)) != NO_ERROR) return status;
    values.clear();
    values.assign(mSamplingRates.begin(), mSamplingRates.end());
    if ((status = parcel->writeInt32Vector(values)) != NO_ERROR) return status;
    if ((status = parcel->writeBool(mIsDynamicFormat)) != NO_ERROR) return status;
    if ((status = parcel->writeBool(mIsDynamicChannels)) != NO_ERROR) return status;
    if ((status = parcel->writeBool(mIsDynamicRate)) != NO_ERROR) return status;
    return status;
}

status_t AudioProfile::readFromParcel(const Parcel *parcel)
{
    status_t status = NO_ERROR;
    if ((status = parcel->readUtf8FromUtf16(&mName)) != NO_ERROR) return status;
    static_assert(sizeof(mFormat) == sizeof(uint32_t));
    if ((status = parcel->readUint32(reinterpret_cast<uint32_t*>(&mFormat))) != NO_ERROR) {
        return status;
    }
    std::vector<int> values;
    if ((status = parcel->readInt32Vector(&values)) != NO_ERROR) return status;
    mChannelMasks.clear();
    mChannelMasks.insert(values.begin(), values.end());
    values.clear();
    if ((status = parcel->readInt32Vector(&values)) != NO_ERROR) return status;
    mSamplingRates.clear();
    mSamplingRates.insert(values.begin(), values.end());
    if ((status = parcel->readBool(&mIsDynamicFormat)) != NO_ERROR) return status;
    if ((status = parcel->readBool(&mIsDynamicChannels)) != NO_ERROR) return status;
    if ((status = parcel->readBool(&mIsDynamicRate)) != NO_ERROR) return status;
    return status;
}

ssize_t AudioProfileVector::add(const sp<AudioProfile> &profile)
{
    ssize_t index = size();
@@ -219,4 +269,39 @@ void AudioProfileVector::dump(std::string *dst, int spaces) const
    }
}

status_t AudioProfileVector::writeToParcel(Parcel *parcel) const
{
    status_t status = NO_ERROR;
    if ((status = parcel->writeVectorSize(*this)) != NO_ERROR) return status;
    for (const auto &audioProfile : *this) {
        if ((status = parcel->writeParcelable(*audioProfile)) != NO_ERROR) {
            break;
        }
    }
    return status;
}

status_t AudioProfileVector::readFromParcel(const Parcel *parcel)
{
    status_t status = NO_ERROR;
    this->clear();
    if ((status = parcel->resizeOutVector(this)) != NO_ERROR) return status;
    for (size_t i = 0; i < this->size(); ++i) {
        this->at(i) = new AudioProfile(AUDIO_FORMAT_DEFAULT, AUDIO_CHANNEL_NONE, 0 /*sampleRate*/);
        if ((status = parcel->readParcelable(this->at(i).get())) != NO_ERROR) {
            this->clear();
            break;
        }
    }
    return status;
}

bool AudioProfileVector::equals(const AudioProfileVector& other) const
{
    return std::equal(begin(), end(), other.begin(), other.end(),
                      [](const sp<AudioProfile>& left, const sp<AudioProfile>& right) {
                          return left->equals(right);
                      });
}

} // namespace android
+29 −0
Original line number Diff line number Diff line
@@ -110,4 +110,33 @@ void DeviceDescriptorBase::log() const
    AudioPort::log("  ");
}

bool DeviceDescriptorBase::equals(const sp<DeviceDescriptorBase> &other) const
{
    return other != nullptr &&
           static_cast<const AudioPort*>(this)->equals(other) &&
           static_cast<const AudioPortConfig*>(this)->equals(other) &&
           mAddress.compare(other->address()) == 0 &&
           mDeviceType == other->type();
}

status_t DeviceDescriptorBase::writeToParcel(Parcel *parcel) const
{
    status_t status = NO_ERROR;
    if ((status = AudioPort::writeToParcel(parcel)) != NO_ERROR) return status;
    if ((status = AudioPortConfig::writeToParcel(parcel)) != NO_ERROR) return status;
    if ((status = parcel->writeUtf8AsUtf16(mAddress)) != NO_ERROR) return status;
    if ((status = parcel->writeUint32(mDeviceType)) != NO_ERROR) return status;
    return status;
}

status_t DeviceDescriptorBase::readFromParcel(const Parcel *parcel)
{
    status_t status = NO_ERROR;
    if ((status = AudioPort::readFromParcel(parcel)) != NO_ERROR) return status;
    if ((status = AudioPortConfig::readFromParcel(parcel)) != NO_ERROR) return status;
    if ((status = parcel->readUtf8FromUtf16(&mAddress)) != NO_ERROR) return status;
    if ((status = parcel->readUint32(&mDeviceType)) != NO_ERROR) return status;
    return status;
}

} // namespace android
+7 −0
Original line number Diff line number Diff line
{
  "presubmit": [
    {
       "name": "audiofoundation_parcelable_test"
    }
  ]
}
Loading