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

Commit dd2ce459 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "aaudio: check for callback thread outside the lock" into sc-dev

parents e1e347b2 5ff3b953
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -268,7 +268,7 @@ void *AudioStreamInternalCapture::callbackLoop() {

        if (callbackResult == AAUDIO_CALLBACK_RESULT_STOP) {
            ALOGD("%s(): callback returned AAUDIO_CALLBACK_RESULT_STOP", __func__);
            result = systemStopFromCallback();
            result = systemStopInternal();
            break;
        }
    }
+1 −1
Original line number Diff line number Diff line
@@ -301,7 +301,7 @@ void *AudioStreamInternalPlay::callbackLoop() {
            }
        } else if (callbackResult == AAUDIO_CALLBACK_RESULT_STOP) {
            ALOGD("%s(): callback returned AAUDIO_CALLBACK_RESULT_STOP", __func__);
            result = systemStopFromCallback();
            result = systemStopInternal();
            break;
        }
    }
+11 −19
Original line number Diff line number Diff line
@@ -143,13 +143,13 @@ void AudioStream::logReleaseBufferState() {
}

aaudio_result_t AudioStream::systemStart() {
    std::lock_guard<std::mutex> lock(mStreamLock);

    if (collidesWithCallback()) {
        ALOGE("%s cannot be called from a callback!", __func__);
        return AAUDIO_ERROR_INVALID_STATE;
    }

    std::lock_guard<std::mutex> lock(mStreamLock);

    switch (getState()) {
        // Is this a good time to start?
        case AAUDIO_STREAM_STATE_OPEN:
@@ -187,7 +187,6 @@ aaudio_result_t AudioStream::systemStart() {
}

aaudio_result_t AudioStream::systemPause() {
    std::lock_guard<std::mutex> lock(mStreamLock);

    if (!isPauseSupported()) {
        return AAUDIO_ERROR_UNIMPLEMENTED;
@@ -198,6 +197,7 @@ aaudio_result_t AudioStream::systemPause() {
        return AAUDIO_ERROR_INVALID_STATE;
    }

    std::lock_guard<std::mutex> lock(mStreamLock);
    switch (getState()) {
        // Proceed with pausing.
        case AAUDIO_STREAM_STATE_STARTING:
@@ -242,12 +242,12 @@ aaudio_result_t AudioStream::safeFlush() {
        return AAUDIO_ERROR_UNIMPLEMENTED;
    }

    std::lock_guard<std::mutex> lock(mStreamLock);
    if (collidesWithCallback()) {
        ALOGE("stream cannot be flushed from a callback!");
        return AAUDIO_ERROR_INVALID_STATE;
    }

    std::lock_guard<std::mutex> lock(mStreamLock);
    aaudio_result_t result = AAudio_isFlushAllowed(getState());
    if (result != AAUDIO_OK) {
        return result;
@@ -256,7 +256,7 @@ aaudio_result_t AudioStream::safeFlush() {
    return requestFlush_l();
}

aaudio_result_t AudioStream::systemStopFromCallback() {
aaudio_result_t AudioStream::systemStopInternal() {
    std::lock_guard<std::mutex> lock(mStreamLock);
    aaudio_result_t result = safeStop_l();
    if (result == AAUDIO_OK) {
@@ -267,17 +267,12 @@ aaudio_result_t AudioStream::systemStopFromCallback() {
}

aaudio_result_t AudioStream::systemStopFromApp() {
    std::lock_guard<std::mutex> lock(mStreamLock);
    // This check can and should be done outside the lock.
    if (collidesWithCallback()) {
        ALOGE("stream cannot be stopped by calling from a callback!");
        return AAUDIO_ERROR_INVALID_STATE;
    }
    aaudio_result_t result = safeStop_l();
    if (result == AAUDIO_OK) {
        // We only call this for logging in "dumpsys audio". So ignore return code.
        (void) mPlayerBase->stopWithStatus();
    }
    return result;
    return systemStopInternal();
}

aaudio_result_t AudioStream::safeStop_l() {
@@ -316,12 +311,12 @@ aaudio_result_t AudioStream::safeStop_l() {
}

aaudio_result_t AudioStream::safeRelease() {
    // This may get temporarily unlocked in the MMAP release() when joining callback threads.
    std::lock_guard<std::mutex> lock(mStreamLock);
    if (collidesWithCallback()) {
        ALOGE("%s cannot be called from a callback!", __func__);
        return AAUDIO_ERROR_INVALID_STATE;
    }
    // This may get temporarily unlocked in the MMAP release() when joining callback threads.
    std::lock_guard<std::mutex> lock(mStreamLock);
    if (getState() == AAUDIO_STREAM_STATE_CLOSING) { // already released?
        return AAUDIO_OK;
    }
@@ -329,17 +324,14 @@ aaudio_result_t AudioStream::safeRelease() {
}

aaudio_result_t AudioStream::safeReleaseClose() {
    // This get temporarily unlocked in the MMAP release() when joining callback threads.
    std::lock_guard<std::mutex> lock(mStreamLock);
    if (collidesWithCallback()) {
        ALOGE("%s cannot be called from a callback!", __func__);
        return AAUDIO_ERROR_INVALID_STATE;
    }
    releaseCloseFinal_l();
    return AAUDIO_OK;
    return safeReleaseCloseInternal();
}

aaudio_result_t AudioStream::safeReleaseCloseFromCallback() {
aaudio_result_t AudioStream::safeReleaseCloseInternal() {
    // This get temporarily unlocked in the MMAP release() when joining callback threads.
    std::lock_guard<std::mutex> lock(mStreamLock);
    releaseCloseFinal_l();
+2 −2
Original line number Diff line number Diff line
@@ -408,7 +408,7 @@ public:
    /**
     * This is called internally when an app callback returns AAUDIO_CALLBACK_RESULT_STOP.
     */
    aaudio_result_t systemStopFromCallback();
    aaudio_result_t systemStopInternal();

    /**
     * Safely RELEASE a stream after taking mStreamLock and checking
@@ -424,7 +424,7 @@ public:
     */
    aaudio_result_t safeReleaseClose();

    aaudio_result_t safeReleaseCloseFromCallback();
    aaudio_result_t safeReleaseCloseInternal();

protected:

+1 −1
Original line number Diff line number Diff line
@@ -124,7 +124,7 @@ void AudioStreamLegacy::processCallbackCommon(aaudio_callback_operation_t opcode
                              __func__, callbackResult);
                    }
                    audioBuffer->size = 0;
                    systemStopFromCallback();
                    systemStopInternal();
                    // Disable the callback just in case the system keeps trying to call us.
                    mCallbackEnabled.store(false);
                }
Loading