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

Commit d04b0ec6 authored by jiabin's avatar jiabin
Browse files

AAudio: fix command thread busy loop after disconnection.

After a stream is disconnected, put the stream in standby mode to avoid
thread busy loop. Treat disconnect as an idle state.

Bug: 272749846
Test: atest AAudioTests
Test: OboeTester test disconnection
Change-Id: I0ab0e5e810020cc2337f80641136f844699a236f
parent d24e38bb
Loading
Loading
Loading
Loading
+13 −5
Original line number Diff line number Diff line
@@ -418,7 +418,16 @@ void AAudioServiceStreamBase::run() {
    while (mThreadEnabled.load()) {
        loopCount++;
        int64_t timeoutNanos = -1;
        if (isRunning() || (isIdle_l() && !isStandby_l())) {
        if (isDisconnected_l()) {
            if (!isStandby_l()) {
                // If the stream is disconnected but not in standby mode, wait until standby time.
                timeoutNanos = standbyTime - AudioClock::getNanoseconds();
                timeoutNanos = std::max<int64_t>(0, timeoutNanos);
            } // else {
                // If the stream is disconnected and in standby mode, keep `timeoutNanos` as
                // -1 to wait forever until next command as the stream can only be closed.
            // }
        } else if (isRunning() || (isIdle_l() && !isStandby_l())) {
            timeoutNanos = (isRunning() ? std::min(nextTimestampReportTime, nextDataReportTime)
                                        : standbyTime) - AudioClock::getNanoseconds();
            timeoutNanos = std::max<int64_t>(0, timeoutNanos);
@@ -430,7 +439,7 @@ void AAudioServiceStreamBase::run() {
            break;
        }

        if (isRunning()) {
        if (isRunning() && !isDisconnected_l()) {
            auto currentTimestamp = AudioClock::getNanoseconds();
            if (currentTimestamp >= nextDataReportTime) {
                reportData_l();
@@ -441,12 +450,11 @@ void AAudioServiceStreamBase::run() {
                if (sendCurrentTimestamp_l() != AAUDIO_OK) {
                    ALOGE("Failed to send current timestamp, stop updating timestamp");
                    disconnect_l();
                } else {
                    nextTimestampReportTime = timestampScheduler.nextAbsoluteTime();
                }
                nextTimestampReportTime = timestampScheduler.nextAbsoluteTime();
            }
        }
        if (isIdle_l() && AudioClock::getNanoseconds() >= standbyTime) {
        if ((isIdle_l() || isDisconnected_l()) && AudioClock::getNanoseconds() >= standbyTime) {
            aaudio_result_t result = standby_l();
            if (result != AAUDIO_OK) {
                // If standby failed because of the function is not implemented, there is no