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

Commit 2d0ddeff authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge changes from topic "fix-b-321233946-bt-glitches-2" into main

* changes:
  audio: Align HAL buffer size logic with the framework
  audio: Set correct priority for the SPATIALIZER stream thread
parents 2036d294 f2f9ae08
Loading
Loading
Loading
Loading
+24 −5
Original line number Original line Diff line number Diff line
@@ -21,6 +21,7 @@
#include <Utils.h>
#include <Utils.h>
#include <android-base/logging.h>
#include <android-base/logging.h>
#include <android/binder_ibinder_platform.h>
#include <android/binder_ibinder_platform.h>
#include <cutils/properties.h>
#include <utils/SystemClock.h>
#include <utils/SystemClock.h>
#include <utils/Trace.h>
#include <utils/Trace.h>


@@ -652,16 +653,34 @@ ndk::ScopedAStatus StreamCommonImpl::initInstance(
         isBitPositionFlagSet(flags.template get<AudioIoFlags::Tag::input>(),
         isBitPositionFlagSet(flags.template get<AudioIoFlags::Tag::input>(),
                              AudioInputFlags::FAST)) ||
                              AudioInputFlags::FAST)) ||
        (flags.getTag() == AudioIoFlags::Tag::output &&
        (flags.getTag() == AudioIoFlags::Tag::output &&
         (isBitPositionFlagSet(flags.template get<AudioIoFlags::Tag::output>(),
                               AudioOutputFlags::FAST) ||
          isBitPositionFlagSet(flags.template get<AudioIoFlags::Tag::output>(),
          isBitPositionFlagSet(flags.template get<AudioIoFlags::Tag::output>(),
                              AudioOutputFlags::FAST))) {
                               AudioOutputFlags::SPATIALIZER)))) {
        // FAST workers should be run with a SCHED_FIFO scheduler, however the host process
        // FAST workers should be run with a SCHED_FIFO scheduler, however the host process
        // might be lacking the capability to request it, thus a failure to set is not an error.
        // might be lacking the capability to request it, thus a failure to set is not an error.
        pid_t workerTid = mWorker->getTid();
        pid_t workerTid = mWorker->getTid();
        if (workerTid > 0) {
        if (workerTid > 0) {
            struct sched_param param;
            constexpr int32_t kRTPriorityMin = 1;  // SchedulingPolicyService.PRIORITY_MIN (Java).
            param.sched_priority = 3;  // Must match SchedulingPolicyService.PRIORITY_MAX (Java).
            constexpr int32_t kRTPriorityMax = 3;  // SchedulingPolicyService.PRIORITY_MAX (Java).
            int priorityBoost = kRTPriorityMax;
            if (flags.getTag() == AudioIoFlags::Tag::output &&
                isBitPositionFlagSet(flags.template get<AudioIoFlags::Tag::output>(),
                                     AudioOutputFlags::SPATIALIZER)) {
                const int32_t sptPrio =
                        property_get_int32("audio.spatializer.priority", kRTPriorityMin);
                if (sptPrio >= kRTPriorityMin && sptPrio <= kRTPriorityMax) {
                    priorityBoost = sptPrio;
                } else {
                    LOG(WARNING) << __func__ << ": invalid spatializer priority: " << sptPrio;
                    return ndk::ScopedAStatus::ok();
                }
            }
            struct sched_param param = {
                    .sched_priority = priorityBoost,
            };
            if (sched_setscheduler(workerTid, SCHED_FIFO | SCHED_RESET_ON_FORK, &param) != 0) {
            if (sched_setscheduler(workerTid, SCHED_FIFO | SCHED_RESET_ON_FORK, &param) != 0) {
                PLOG(WARNING) << __func__ << ": failed to set FIFO scheduler for a fast thread";
                PLOG(WARNING) << __func__ << ": failed to set FIFO scheduler and priority";
            }
            }
        } else {
        } else {
            LOG(WARNING) << __func__ << ": invalid worker tid: " << workerTid;
            LOG(WARNING) << __func__ << ": invalid worker tid: " << workerTid;
+2 −8
Original line number Original line Diff line number Diff line
@@ -16,16 +16,13 @@


#include <algorithm>
#include <algorithm>


#define ATRACE_TAG ATRACE_TAG_AUDIO
#define LOG_TAG "AHAL_StreamBluetooth"
#define LOG_TAG "AHAL_StreamBluetooth"
#include <Utils.h>
#include <Utils.h>
#include <android-base/logging.h>
#include <android-base/logging.h>
#include <audio_utils/clock.h>
#include <audio_utils/clock.h>
#include <utils/Trace.h>


#include "core-impl/StreamBluetooth.h"
#include "core-impl/StreamBluetooth.h"


using aidl::android::hardware::audio::common::frameCountFromDurationUs;
using aidl::android::hardware::audio::common::SinkMetadata;
using aidl::android::hardware::audio::common::SinkMetadata;
using aidl::android::hardware::audio::common::SourceMetadata;
using aidl::android::hardware::audio::common::SourceMetadata;
using aidl::android::hardware::audio::core::VendorParameter;
using aidl::android::hardware::audio::core::VendorParameter;
@@ -67,8 +64,6 @@ StreamBluetooth::StreamBluetooth(StreamContext* context, const Metadata& metadat
                                       : (mIsInput ? kBluetoothDefaultInputBufferMs
                                       : (mIsInput ? kBluetoothDefaultInputBufferMs
                                                   : kBluetoothDefaultOutputBufferMs) *
                                                   : kBluetoothDefaultOutputBufferMs) *
                                                 1000),
                                                 1000),
      mPreferredFrameCount(
              frameCountFromDurationUs(mPreferredDataIntervalUs, pcmConfig.sampleRateHz)),
      mBtDeviceProxy(btDeviceProxy) {}
      mBtDeviceProxy(btDeviceProxy) {}


::android::status_t StreamBluetooth::init() {
::android::status_t StreamBluetooth::init() {
@@ -77,6 +72,7 @@ StreamBluetooth::StreamBluetooth(StreamContext* context, const Metadata& metadat
        // This is a normal situation in VTS tests.
        // This is a normal situation in VTS tests.
        LOG(INFO) << __func__ << ": no BT HAL proxy, stream is non-functional";
        LOG(INFO) << __func__ << ": no BT HAL proxy, stream is non-functional";
    }
    }
    LOG(INFO) << __func__ << ": preferred data interval (us): " << mPreferredDataIntervalUs;
    return ::android::OK;
    return ::android::OK;
}
}


@@ -108,12 +104,10 @@ StreamBluetooth::StreamBluetooth(StreamContext* context, const Metadata& metadat
        LOG(ERROR) << __func__ << ": state= " << mBtDeviceProxy->getState() << " failed to start";
        LOG(ERROR) << __func__ << ": state= " << mBtDeviceProxy->getState() << " failed to start";
        return -EIO;
        return -EIO;
    }
    }
    const size_t fc = std::min(frameCount, mPreferredFrameCount);
    const size_t bytesToTransfer = frameCount * mFrameSizeBytes;
    const size_t bytesToTransfer = fc * mFrameSizeBytes;
    const size_t bytesTransferred = mIsInput ? mBtDeviceProxy->readData(buffer, bytesToTransfer)
    const size_t bytesTransferred = mIsInput ? mBtDeviceProxy->readData(buffer, bytesToTransfer)
                                             : mBtDeviceProxy->writeData(buffer, bytesToTransfer);
                                             : mBtDeviceProxy->writeData(buffer, bytesToTransfer);
    *actualFrameCount = bytesTransferred / mFrameSizeBytes;
    *actualFrameCount = bytesTransferred / mFrameSizeBytes;
    ATRACE_INT("BTdropped", bytesToTransfer - bytesTransferred);
    PresentationPosition presentation_position;
    PresentationPosition presentation_position;
    if (!mBtDeviceProxy->getPresentationPosition(presentation_position)) {
    if (!mBtDeviceProxy->getPresentationPosition(presentation_position)) {
        presentation_position.remoteDeviceAudioDelayNanos =
        presentation_position.remoteDeviceAudioDelayNanos =
+2 −4
Original line number Original line Diff line number Diff line
@@ -211,10 +211,8 @@ class Module : public BnModule {
        const int32_t rawSizeFrames =
        const int32_t rawSizeFrames =
                aidl::android::hardware::audio::common::frameCountFromDurationMs(latencyMs,
                aidl::android::hardware::audio::common::frameCountFromDurationMs(latencyMs,
                                                                                 sampleRateHz);
                                                                                 sampleRateHz);
        if (latencyMs >= 5) return rawSizeFrames;
        // Round up to nearest 16 frames since in the framework this is the size of a mixer burst.
        int32_t powerOf2 = 1;
        return (rawSizeFrames + 15) & ~15;
        while (powerOf2 < rawSizeFrames) powerOf2 <<= 1;
        return powerOf2;
    }
    }


    ndk::ScopedAStatus bluetoothParametersUpdated();
    ndk::ScopedAStatus bluetoothParametersUpdated();
+0 −1
Original line number Original line Diff line number Diff line
@@ -63,7 +63,6 @@ class StreamBluetooth : public StreamCommonImpl {
    const std::weak_ptr<IBluetoothA2dp> mBluetoothA2dp;
    const std::weak_ptr<IBluetoothA2dp> mBluetoothA2dp;
    const std::weak_ptr<IBluetoothLe> mBluetoothLe;
    const std::weak_ptr<IBluetoothLe> mBluetoothLe;
    const size_t mPreferredDataIntervalUs;
    const size_t mPreferredDataIntervalUs;
    const size_t mPreferredFrameCount;
    mutable std::mutex mLock;
    mutable std::mutex mLock;
    // The lock is also used to serialize calls to the proxy.
    // The lock is also used to serialize calls to the proxy.
    std::shared_ptr<::android::bluetooth::audio::aidl::BluetoothAudioPortAidl> mBtDeviceProxy
    std::shared_ptr<::android::bluetooth::audio::aidl::BluetoothAudioPortAidl> mBtDeviceProxy