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

Commit 783c48b0 authored by jiabin's avatar jiabin
Browse files

AHAL: support volume control for USB audio HAL.

Use mixer control to support master mute, master volume and hardware
volume for USB audio HAL.

Bug: 266216550
Test: atest VtsHalAudioCoreTargetTest
Change-Id: Iad544ba517cbfc778ebdf96dd161944886383b73
parent 960c33c5
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -596,6 +596,7 @@ interface IModule {
     * @param mute Whether the output from the module is muted.
     * @throws EX_UNSUPPORTED_OPERATION If muting of combined output
     *                                  is not supported by the module.
     * @throws EX_ILLEGAL_STATE If any error happens while muting of combined output.
     */
    void setMasterMute(boolean mute);

@@ -627,6 +628,8 @@ interface IModule {
     *                             accepted range.
     * @throws EX_UNSUPPORTED_OPERATION If attenuation of combined output
     *                                  is not supported by the module.
     * @throws EX_ILLEGAL_STATE If any error happens while updating attenuation of
                                combined output.
     */
    void setMasterVolume(float volume);

+2 −1
Original line number Diff line number Diff line
@@ -85,7 +85,8 @@ interface IStreamOut {
     * @throws EX_ILLEGAL_ARGUMENT If the number of elements in the provided
     *                             array does not match the channel count, or
     *                             attenuation values are out of range.
     * @throws EX_ILLEGAL_STATE If the stream is closed.
     * @throws EX_ILLEGAL_STATE If the stream is closed or there is any error happens
                                when applying hardware volume.
     * @throws EX_UNSUPPORTED_OPERATION If hardware volume control is not supported.
     */
    void setHwVolume(in float[] channelVolumes);
+1 −0
Original line number Diff line number Diff line
@@ -75,6 +75,7 @@ cc_library_static {
        "Telephony.cpp",
        "usb/ModuleUsb.cpp",
        "usb/StreamUsb.cpp",
        "usb/UsbAlsaMixerControl.cpp",
        "usb/UsbAlsaUtils.cpp",
    ],
    generated_sources: [
+40 −4
Original line number Diff line number Diff line
@@ -457,6 +457,7 @@ ndk::ScopedAStatus Module::connectExternalDevice(const AudioPort& in_templateIdA
        connectedPort.profiles = connectedProfilesIt->second;
    }
    ports.push_back(connectedPort);
    onExternalDeviceConnectionChanged(connectedPort, true /*connected*/);
    *_aidl_return = std::move(connectedPort);

    std::vector<AudioRoute> newRoutes;
@@ -510,6 +511,7 @@ ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) {
                   << configIt->id;
        return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
    }
    onExternalDeviceConnectionChanged(*portIt, false /*connected*/);
    ports.erase(portIt);
    mConnectedDevicePorts.erase(in_portId);
    LOG(DEBUG) << __func__ << ": connected device port " << in_portId << " released";
@@ -980,8 +982,17 @@ ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) {

ndk::ScopedAStatus Module::setMasterMute(bool in_mute) {
    LOG(DEBUG) << __func__ << ": " << in_mute;
    auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
                                                   : onMasterMuteChanged(in_mute);
    if (result.isOk()) {
        mMasterMute = in_mute;
    return ndk::ScopedAStatus::ok();
    } else {
        LOG(ERROR) << __func__ << ": failed calling onMasterMuteChanged(" << in_mute
                   << "), error=" << result;
        // Reset master mute if it failed.
        onMasterMuteChanged(mMasterMute);
    }
    return std::move(result);
}

ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) {
@@ -993,8 +1004,17 @@ ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) {
ndk::ScopedAStatus Module::setMasterVolume(float in_volume) {
    LOG(DEBUG) << __func__ << ": " << in_volume;
    if (in_volume >= 0.0f && in_volume <= 1.0f) {
        auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
                                                       : onMasterVolumeChanged(in_volume);
        if (result.isOk()) {
            mMasterVolume = in_volume;
        return ndk::ScopedAStatus::ok();
        } else {
            // Reset master volume if it failed.
            LOG(ERROR) << __func__ << ": failed calling onMasterVolumeChanged(" << in_volume
                       << "), error=" << result;
            onMasterVolumeChanged(mMasterVolume);
        }
        return std::move(result);
    }
    LOG(ERROR) << __func__ << ": invalid master volume value: " << in_volume;
    return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
@@ -1262,4 +1282,20 @@ ndk::ScopedAStatus Module::checkAudioPatchEndpointsMatch(
    return ndk::ScopedAStatus::ok();
}

void Module::onExternalDeviceConnectionChanged(
        const ::aidl::android::media::audio::common::AudioPort& audioPort __unused,
        bool connected __unused) {
    LOG(DEBUG) << __func__ << ": do nothing and return";
}

ndk::ScopedAStatus Module::onMasterMuteChanged(bool mute __unused) {
    LOG(VERBOSE) << __func__ << ": do nothing and return ok";
    return ndk::ScopedAStatus::ok();
}

ndk::ScopedAStatus Module::onMasterVolumeChanged(float volume __unused) {
    LOG(VERBOSE) << __func__ << ": do nothing and return ok";
    return ndk::ScopedAStatus::ok();
}

}  // namespace aidl::android::hardware::audio::core
+7 −2
Original line number Diff line number Diff line
@@ -163,8 +163,6 @@ class Module : public BnModule {
    // Maps port ids and port config ids to patch ids.
    // Multimap because both ports and configs can be used by multiple patches.
    std::multimap<int32_t, int32_t> mPatches;
    bool mMasterMute = false;
    float mMasterVolume = 1.0f;
    bool mMicMute = false;
    std::shared_ptr<sounddose::ISoundDose> mSoundDose;
    ndk::SpAIBinder mSoundDoseBinder;
@@ -180,6 +178,13 @@ class Module : public BnModule {
    virtual ndk::ScopedAStatus checkAudioPatchEndpointsMatch(
            const std::vector<::aidl::android::media::audio::common::AudioPortConfig*>& sources,
            const std::vector<::aidl::android::media::audio::common::AudioPortConfig*>& sinks);
    virtual void onExternalDeviceConnectionChanged(
            const ::aidl::android::media::audio::common::AudioPort& audioPort, bool connected);
    virtual ndk::ScopedAStatus onMasterMuteChanged(bool mute);
    virtual ndk::ScopedAStatus onMasterVolumeChanged(float volume);

    bool mMasterMute = false;
    float mMasterVolume = 1.0f;
};

}  // namespace aidl::android::hardware::audio::core
Loading