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

Commit aec565e3 authored by Mikhail Naganov's avatar Mikhail Naganov
Browse files

audioflinger: Move async callback setting to onFirstRef

Since initializing async callback involves passing
PlaybackThread's 'this' around, it's safer to do that
in 'onFirstRef' instead of the constructor. Since
'onFirstRef' runs before the thread loop is started,
shared data access model is not affected.

Bug: 206737711
Test: atest CtsMediaAudioTestCases:AudioTrackOffloadTest
Test: check offloaded playback on the device
Change-Id: I26f76b9ff843c871911fe3546027cb197f1f0272
parent f5839887
Loading
Loading
Loading
Loading
+13 −10
Original line number Diff line number Diff line
@@ -2140,9 +2140,19 @@ void AudioFlinger::PlaybackThread::onFirstRef()
    if (!isStreamInitialized()) {
        ALOGE("The stream is not open yet"); // This should not happen.
    } else {
        // setEventCallback will need a strong pointer as a parameter. Calling it
        // here instead of constructor of PlaybackThread so that the onFirstRef
        // callback would not be made on an incompletely constructed object.
        // Callbacks take strong or weak pointers as a parameter.
        // Since PlaybackThread passes itself as a callback handler, it can only
        // be done outside of the constructor. Creating weak and especially strong
        // pointers to a refcounted object in its own constructor is strongly
        // discouraged, see comments in system/core/libutils/include/utils/RefBase.h.
        // Even if a function takes a weak pointer, it is possible that it will
        // need to convert it to a strong pointer down the line.
        if (mOutput->flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING &&
                mOutput->stream->setCallback(this) == OK) {
            mUseAsyncWrite = true;
            mCallbackThread = new AudioFlinger::AsyncCallbackThread(this);
        }

        if (mOutput->stream->setEventCallback(this) != OK) {
            ALOGD("Failed to add event callback");
        }
@@ -3007,13 +3017,6 @@ void AudioFlinger::PlaybackThread::readOutputParameters_l()
                mFrameCount);
    }

    if (mOutput->flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) {
        if (mOutput->stream->setCallback(this) == OK) {
            mUseAsyncWrite = true;
            mCallbackThread = new AudioFlinger::AsyncCallbackThread(this);
        }
    }

    mHwSupportsPause = false;
    if (mOutput->flags & AUDIO_OUTPUT_FLAG_DIRECT) {
        bool supportsPause = false, supportsResume = false;