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

Commit 28aeab19 authored by David Li's avatar David Li Committed by Shunkai Yao
Browse files

AIDL effect: check if common config is vaild

Also validate downmix effect output channel to be stereo

Bug: 315878718
Test: mm; manual test
Change-Id: Iad6e1500d3e2dbab19b751fadcb3a465fcc6bcb1
parent 6490d84c
Loading
Loading
Loading
Loading
+50 −16
Original line number Original line Diff line number Diff line
@@ -20,12 +20,60 @@


#include "DownmixContext.h"
#include "DownmixContext.h"


using aidl::android::hardware::audio::effect::IEffect;
using aidl::android::hardware::audio::common::getChannelCount;
using aidl::android::hardware::audio::common::getChannelCount;
using aidl::android::hardware::audio::effect::IEffect;
using aidl::android::media::audio::common::AudioChannelLayout;
using aidl::android::media::audio::common::AudioChannelLayout;
using aidl::android::media::audio::common::AudioConfig;


namespace aidl::android::hardware::audio::effect {
namespace aidl::android::hardware::audio::effect {


namespace {

inline bool isChannelMaskValid(const AudioChannelLayout& channelMask) {
    if (channelMask.getTag() != AudioChannelLayout::layoutMask) return false;
    int chMask = channelMask.get<AudioChannelLayout::layoutMask>();
    // check against unsupported channels (up to FCC_26)
    constexpr uint32_t MAXIMUM_CHANNEL_MASK = AudioChannelLayout::LAYOUT_22POINT2 |
                                              AudioChannelLayout::CHANNEL_FRONT_WIDE_LEFT |
                                              AudioChannelLayout::CHANNEL_FRONT_WIDE_RIGHT;
    if (chMask & ~MAXIMUM_CHANNEL_MASK) {
        LOG(ERROR) << "Unsupported channels in " << (chMask & ~MAXIMUM_CHANNEL_MASK);
        return false;
    }
    return true;
}

inline bool isStereoChannelMask(const AudioChannelLayout& channelMask) {
    if (channelMask.getTag() != AudioChannelLayout::layoutMask) return false;

    return channelMask.get<AudioChannelLayout::layoutMask>() == AudioChannelLayout::LAYOUT_STEREO;
}

}  // namespace

bool DownmixContext::validateCommonConfig(const Parameter::Common& common) {
    const AudioConfig& input = common.input;
    const AudioConfig& output = common.output;
    if (input.base.sampleRate != output.base.sampleRate) {
        LOG(ERROR) << __func__ << ": SRC not supported, input: " << input.toString()
                   << " output: " << output.toString();
        return false;
    }

    if (!isStereoChannelMask(output.base.channelMask)) {
        LOG(ERROR) << __func__ << ": output should be stereo, not "
                   << output.base.channelMask.toString();
        return false;
    }

    if (!isChannelMaskValid(input.base.channelMask)) {
        LOG(ERROR) << __func__ << ": invalid input channel, " << input.base.channelMask.toString();
        return false;
    }

    return true;
}

DownmixContext::DownmixContext(int statusDepth, const Parameter::Common& common)
DownmixContext::DownmixContext(int statusDepth, const Parameter::Common& common)
    : EffectContext(statusDepth, common) {
    : EffectContext(statusDepth, common) {
    LOG(DEBUG) << __func__;
    LOG(DEBUG) << __func__;
@@ -62,7 +110,7 @@ void DownmixContext::reset() {
    resetBuffer();
    resetBuffer();
}
}


IEffect::Status DownmixContext::lvmProcess(float* in, float* out, int samples) {
IEffect::Status DownmixContext::downmixProcess(float* in, float* out, int samples) {
    LOG(DEBUG) << __func__ << " in " << in << " out " << out << " sample " << samples;
    LOG(DEBUG) << __func__ << " in " << in << " out " << out << " sample " << samples;
    IEffect::Status status = {EX_ILLEGAL_ARGUMENT, 0, 0};
    IEffect::Status status = {EX_ILLEGAL_ARGUMENT, 0, 0};


@@ -122,18 +170,4 @@ void DownmixContext::init_params(const Parameter::Common& common) {
    }
    }
}
}


bool DownmixContext::isChannelMaskValid(AudioChannelLayout channelMask) {
    if (channelMask.getTag() != AudioChannelLayout::layoutMask) return false;
    int chMask = channelMask.get<AudioChannelLayout::layoutMask>();
    // check against unsupported channels (up to FCC_26)
    constexpr uint32_t MAXIMUM_CHANNEL_MASK = AudioChannelLayout::LAYOUT_22POINT2 |
                                              AudioChannelLayout::CHANNEL_FRONT_WIDE_LEFT |
                                              AudioChannelLayout::CHANNEL_FRONT_WIDE_RIGHT;
    if (chMask & ~MAXIMUM_CHANNEL_MASK) {
        LOG(ERROR) << "Unsupported channels in " << (chMask & ~MAXIMUM_CHANNEL_MASK);
        return false;
    }
    return true;
}

}  // namespace aidl::android::hardware::audio::effect
}  // namespace aidl::android::hardware::audio::effect
+3 −2
Original line number Original line Diff line number Diff line
@@ -50,7 +50,9 @@ class DownmixContext final : public EffectContext {
        return RetCode::SUCCESS;
        return RetCode::SUCCESS;
    }
    }


    IEffect::Status lvmProcess(float* in, float* out, int samples);
    IEffect::Status downmixProcess(float* in, float* out, int samples);

    static bool validateCommonConfig(const Parameter::Common& common);


  private:
  private:
    DownmixState mState;
    DownmixState mState;
@@ -60,7 +62,6 @@ class DownmixContext final : public EffectContext {


    // Common Params
    // Common Params
    void init_params(const Parameter::Common& common);
    void init_params(const Parameter::Common& common);
    bool isChannelMaskValid(::aidl::android::media::audio::common::AudioChannelLayout channelMask);
};
};


}  // namespace aidl::android::hardware::audio::effect
}  // namespace aidl::android::hardware::audio::effect
+3 −1
Original line number Original line Diff line number Diff line
@@ -193,6 +193,8 @@ std::shared_ptr<EffectContext> DownmixImpl::createContext(const Parameter::Commo
        return mContext;
        return mContext;
    }
    }


    if (!DownmixContext::validateCommonConfig(common)) return nullptr;

    mContext = std::make_shared<DownmixContext>(1 /* statusFmqDepth */, common);
    mContext = std::make_shared<DownmixContext>(1 /* statusFmqDepth */, common);
    return mContext;
    return mContext;
}
}
@@ -210,7 +212,7 @@ IEffect::Status DownmixImpl::effectProcessImpl(float* in, float* out, int sample
        LOG(ERROR) << __func__ << " nullContext";
        LOG(ERROR) << __func__ << " nullContext";
        return {EX_NULL_POINTER, 0, 0};
        return {EX_NULL_POINTER, 0, 0};
    }
    }
    return mContext->lvmProcess(in, out, sampleToProcess);
    return mContext->downmixProcess(in, out, sampleToProcess);
}
}


}  // namespace aidl::android::hardware::audio::effect
}  // namespace aidl::android::hardware::audio::effect