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

Commit e7733542 authored by Andy Hung's avatar Andy Hung
Browse files

AudioFlinger: Move vibration methods to afutils

Test: atest AudioRecordTest AudioTrackTest
Test: Camera YouTube
Bug: 182392769
Bug: 292018229
Merged-In: Ifeda24e9f887db48642ce4230c08a4acefa4330c
Change-Id: Ifeda24e9f887db48642ce4230c08a4acefa4330c
parent 9d0ea8ef
Loading
Loading
Loading
Loading
+0 −43
Original line number Original line Diff line number Diff line
@@ -148,21 +148,6 @@ static void sMediaLogInit()
    }
    }
}
}


// Keep a strong reference to external vibrator service
static sp<os::IExternalVibratorService> sExternalVibratorService;

static sp<os::IExternalVibratorService> getExternalVibratorService() {
    if (sExternalVibratorService == 0) {
        sp<IBinder> binder = defaultServiceManager()->getService(
            String16("external_vibrator_service"));
        if (binder != 0) {
            sExternalVibratorService =
                interface_cast<os::IExternalVibratorService>(binder);
        }
    }
    return sExternalVibratorService;
}

// Creates association between Binder code to name for IAudioFlinger.
// Creates association between Binder code to name for IAudioFlinger.
#define IAUDIOFLINGER_BINDER_METHOD_MACRO_LIST \
#define IAUDIOFLINGER_BINDER_METHOD_MACRO_LIST \
BINDER_METHOD_ENTRY(createTrack) \
BINDER_METHOD_ENTRY(createTrack) \
@@ -675,34 +660,6 @@ status_t AudioFlinger::openMmapStream(MmapStreamInterface::stream_direction_t di
    return ret;
    return ret;
}
}


/* static */
os::HapticScale AudioFlinger::onExternalVibrationStart(
        const sp<os::ExternalVibration>& externalVibration) {
    sp<os::IExternalVibratorService> evs = getExternalVibratorService();
    if (evs != nullptr) {
        int32_t ret;
        binder::Status status = evs->onExternalVibrationStart(*externalVibration, &ret);
        if (status.isOk()) {
            ALOGD("%s, start external vibration with intensity as %d", __func__, ret);
            return os::ExternalVibration::externalVibrationScaleToHapticScale(ret);
        }
    }
    ALOGD("%s, start external vibration with intensity as MUTE due to %s",
            __func__,
            evs == nullptr ? "external vibration service not found"
                           : "error when querying intensity");
    return os::HapticScale::MUTE;
}

/* static */
void AudioFlinger::onExternalVibrationStop(const sp<os::ExternalVibration>& externalVibration) {
    sp<os::IExternalVibratorService> evs = getExternalVibratorService();
    if (evs != 0) {
        ALOGD("%s, stopping external vibration", __func__);
        evs->onExternalVibrationStop(*externalVibration);
    }
}

status_t AudioFlinger::addEffectToHal(
status_t AudioFlinger::addEffectToHal(
        const struct audio_port_config *device, const sp<EffectHalInterface>& effect) {
        const struct audio_port_config *device, const sp<EffectHalInterface>& effect) {
    AutoMutex lock(mHardwareLock);
    AutoMutex lock(mHardwareLock);
+0 −5
Original line number Original line Diff line number Diff line
@@ -480,11 +480,6 @@ public:
                            const sp<MmapStreamCallback>& callback,
                            const sp<MmapStreamCallback>& callback,
                            sp<MmapStreamInterface>& interface,
                            sp<MmapStreamInterface>& interface,
                            audio_port_handle_t *handle);
                            audio_port_handle_t *handle);

    static os::HapticScale onExternalVibrationStart(
        const sp<os::ExternalVibration>& externalVibration);
    static void onExternalVibrationStop(const sp<os::ExternalVibration>& externalVibration);

private:
private:
    // FIXME The 400 is temporarily too high until a leak of writers in media.log is fixed.
    // FIXME The 400 is temporarily too high until a leak of writers in media.log is fixed.
    static const size_t kLogMemorySize = 400 * 1024;
    static const size_t kLogMemorySize = 400 * 1024;
+3 −2
Original line number Original line Diff line number Diff line
@@ -97,6 +97,7 @@
#include <afutils/DumpTryLock.h>
#include <afutils/DumpTryLock.h>
#include <afutils/Permission.h>
#include <afutils/Permission.h>
#include <afutils/TypedLogger.h>
#include <afutils/TypedLogger.h>
#include <afutils/Vibrator.h>


// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------


@@ -2838,7 +2839,7 @@ NO_THREAD_SAFETY_ANALYSIS // release and re-acquire mLock
            // Unlock due to VibratorService will lock for this call and will
            // Unlock due to VibratorService will lock for this call and will
            // call Tracks.mute/unmute which also require thread's lock.
            // call Tracks.mute/unmute which also require thread's lock.
            mLock.unlock();
            mLock.unlock();
            const os::HapticScale intensity = AudioFlinger::onExternalVibrationStart(
            const os::HapticScale intensity = afutils::onExternalVibrationStart(
                    track->getExternalVibration());
                    track->getExternalVibration());
            std::optional<media::AudioVibratorInfo> vibratorInfo;
            std::optional<media::AudioVibratorInfo> vibratorInfo;
            {
            {
@@ -4655,7 +4656,7 @@ NO_THREAD_SAFETY_ANALYSIS // release and re-acquire mLock
            mLock.unlock();
            mLock.unlock();
            // Unlock due to VibratorService will lock for this call and will
            // Unlock due to VibratorService will lock for this call and will
            // call Tracks.mute/unmute which also require thread's lock.
            // call Tracks.mute/unmute which also require thread's lock.
            AudioFlinger::onExternalVibrationStop(track->getExternalVibration());
            afutils::onExternalVibrationStop(track->getExternalVibration());
            mLock.lock();
            mLock.lock();


            // When the track is stop, set the haptic intensity as MUTE
            // When the track is stop, set the haptic intensity as MUTE
+2 −0
Original line number Original line Diff line number Diff line
@@ -42,6 +42,7 @@ cc_library {
        "Permission.cpp",
        "Permission.cpp",
        "PropertyUtils.cpp",
        "PropertyUtils.cpp",
        "TypedLogger.cpp",
        "TypedLogger.cpp",
        "Vibrator.cpp",
    ],
    ],


    shared_libs: [
    shared_libs: [
@@ -55,6 +56,7 @@ cc_library {
        "libnbaio",
        "libnbaio",
        "libnblog",
        "libnblog",
        "libutils",
        "libutils",
        "libvibrator",
    ],
    ],


    static_libs: [
    static_libs: [
+71 −0
Original line number Original line Diff line number Diff line
/*
 *
 * Copyright 2023, 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.
 */

#define LOG_TAG "AudioFlinger::Vibrator"
//#define LOG_NDEBUG 0

#include "Vibrator.h"

#include <android/os/IExternalVibratorService.h>
#include <binder/IServiceManager.h>
#include <utils/Log.h>

#include <mutex>

namespace android::afutils {

static sp<os::IExternalVibratorService> getExternalVibratorService() {
    static std::mutex m;
    static sp<os::IExternalVibratorService> sExternalVibratorService;

    std::lock_guard l(m);
    if (sExternalVibratorService == nullptr) {
        const sp<IBinder> binder = defaultServiceManager()->getService(
                String16("external_vibrator_service"));
        if (binder != nullptr) {
            sExternalVibratorService = interface_cast<os::IExternalVibratorService>(binder);
        }
    }
    return sExternalVibratorService;
}

os::HapticScale onExternalVibrationStart(const sp<os::ExternalVibration>& externalVibration) {
    const sp<os::IExternalVibratorService> evs = getExternalVibratorService();
    if (evs != nullptr) {
        int32_t ret;
        binder::Status status = evs->onExternalVibrationStart(*externalVibration, &ret);
        if (status.isOk()) {
            ALOGD("%s, start external vibration with intensity as %d", __func__, ret);
            return os::ExternalVibration::externalVibrationScaleToHapticScale(ret);
        }
    }
    ALOGD("%s, start external vibration with intensity as MUTE due to %s",
            __func__,
            evs == nullptr ? "external vibration service not found"
                           : "error when querying intensity");
    return os::HapticScale::MUTE;
}

void onExternalVibrationStop(const sp<os::ExternalVibration>& externalVibration) {
    const sp<os::IExternalVibratorService> evs = getExternalVibratorService();
    if (evs != nullptr) {
        ALOGD("%s, stop external vibration", __func__);
        evs->onExternalVibrationStop(*externalVibration);
    }
}

}  // namespace android::afutils
Loading