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

Commit 30b86ba0 authored by Phil Burk's avatar Phil Burk Committed by Automerger Merge Worker
Browse files

Merge "aaudio: allow stream to restart after RESULT_STOP" into sc-dev am: fc1e97b9

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/av/+/15319294

Change-Id: I1127454d9a43f86b673605c19fcdb553a06a2df4
parents 899472be fc1e97b9
Loading
Loading
Loading
Loading
+6 −6
Original line number Diff line number Diff line
@@ -452,8 +452,8 @@ aaudio_result_t AudioStream::createThread_l(int64_t periodNanoseconds,
                                            void* threadArg)
{
    if (mHasThread) {
        ALOGE("%s() - mHasThread already true", __func__);
        return AAUDIO_ERROR_INVALID_STATE;
        ALOGD("%s() - previous thread was not joined, join now to be safe", __func__);
        joinThread_l(nullptr);
    }
    if (threadProc == nullptr) {
        return AAUDIO_ERROR_NULL;
@@ -462,6 +462,7 @@ aaudio_result_t AudioStream::createThread_l(int64_t periodNanoseconds,
    mThreadProc = threadProc;
    mThreadArg = threadArg;
    setPeriodNanoseconds(periodNanoseconds);
    mHasThread = true;
    // Prevent this object from getting deleted before the thread has a chance to create
    // its strong pointer. Assume the thread will call decStrong().
    this->incStrong(nullptr);
@@ -470,6 +471,7 @@ aaudio_result_t AudioStream::createThread_l(int64_t periodNanoseconds,
        android::status_t status = -errno;
        ALOGE("%s() - pthread_create() failed, %d", __func__, status);
        this->decStrong(nullptr); // Because the thread won't do it.
        mHasThread = false;
        return AAudioConvert_androidToAAudioResult(status);
    } else {
        // TODO Use AAudioThread or maybe AndroidThread
@@ -484,7 +486,6 @@ aaudio_result_t AudioStream::createThread_l(int64_t periodNanoseconds,
        err = pthread_setname_np(mThread, name);
        ALOGW_IF((err != 0), "Could not set name of AAudio thread. err = %d", err);

        mHasThread = true;
        return AAUDIO_OK;
    }
}
@@ -498,7 +499,7 @@ aaudio_result_t AudioStream::joinThread(void** returnArg) {
// This must be called under mStreamLock.
aaudio_result_t AudioStream::joinThread_l(void** returnArg) {
    if (!mHasThread) {
        ALOGD("joinThread() - but has no thread");
        ALOGD("joinThread() - but has no thread or already join()ed");
        return AAUDIO_ERROR_INVALID_STATE;
    }
    aaudio_result_t result = AAUDIO_OK;
@@ -515,8 +516,7 @@ aaudio_result_t AudioStream::joinThread_l(void** returnArg) {
            result = AAudioConvert_androidToAAudioResult(-err);
        } else {
            ALOGD("%s() pthread_join succeeded", __func__);
            // This must be set false so that the callback thread can be created
            // when the stream is restarted.
            // Prevent joining a second time, which has undefined behavior.
            mHasThread = false;
        }
    } else {
+13 −3
Original line number Diff line number Diff line
@@ -157,9 +157,13 @@ public:

    virtual aaudio_result_t setBufferSize(int32_t requestedFrames) = 0;

    virtual aaudio_result_t createThread_l(int64_t periodNanoseconds,
    aaudio_result_t createThread(int64_t periodNanoseconds,
                                 aaudio_audio_thread_proc_t threadProc,
                                           void *threadArg);
                                 void *threadArg)
                                 EXCLUDES(mStreamLock) {
        std::lock_guard<std::mutex> lock(mStreamLock);
        return createThread_l(periodNanoseconds, threadProc, threadArg);
    }

    aaudio_result_t joinThread(void **returnArg);

@@ -535,6 +539,11 @@ protected:
        mSessionId = sessionId;
    }

    aaudio_result_t createThread_l(int64_t periodNanoseconds,
                                           aaudio_audio_thread_proc_t threadProc,
                                           void *threadArg)
                                           REQUIRES(mStreamLock);

    aaudio_result_t joinThread_l(void **returnArg) REQUIRES(mStreamLock);

    std::atomic<bool>    mCallbackEnabled{false};
@@ -658,6 +667,7 @@ private:
    std::atomic<pid_t>          mErrorCallbackThread{CALLBACK_THREAD_NONE};

    // background thread ----------------------------------
    // Use mHasThread to prevent joining twice, which has undefined behavior.
    bool                        mHasThread GUARDED_BY(mStreamLock) = false;
    pthread_t                   mThread  GUARDED_BY(mStreamLock) = {};

+3 −3
Original line number Diff line number Diff line
@@ -126,7 +126,7 @@ aaudio_result_t aaudio::AAudioServiceEndpointShared::startSharingThread_l() {
    // Prevent this object from getting deleted before the thread has a chance to create
    // its strong pointer. Assume the thread will call decStrong().
    this->incStrong(nullptr);
    aaudio_result_t result = getStreamInternal()->createThread_l(periodNanos,
    aaudio_result_t result = getStreamInternal()->createThread(periodNanos,
                                                               aaudio_endpoint_thread_proc,
                                                               this);
    if (result != AAUDIO_OK) {