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

Commit 18c8476f authored by Phil Burk's avatar Phil Burk
Browse files

aaudio: fix framesWritten for Legacy INPUT

Was set to 2X framesRead on stop.
It turns out that AudioRecord::stop() does NOT reset getPosition()
to zero. But AudioRecord::start() does.
So the logic handling the reset had to move to the start code.

Bug: 121196899
Test: test_return_stop -i -n
Change-Id: I93635f9ba501b71b99b1df34c7920f7bfea5ca88
parent bd63c6ff
Loading
Loading
Loading
Loading
+10 −13
Original line number Diff line number Diff line
@@ -326,16 +326,13 @@ aaudio_result_t AudioStreamRecord::requestStart()
    if (mAudioRecord.get() == nullptr) {
        return AAUDIO_ERROR_INVALID_STATE;
    }
    // Get current position so we can detect when the track is recording.
    status_t err = mAudioRecord->getPosition(&mPositionWhenStarting);
    if (err != OK) {
        return AAudioConvert_androidToAAudioResult(err);
    }

    // Enable callback before starting AudioTrack to avoid shutting
    // Enable callback before starting AudioRecord to avoid shutting
    // down because of a race condition.
    mCallbackEnabled.store(true);
    err = mAudioRecord->start();
    mFramesWritten.reset32(); // service writes frames
    mTimestampPosition.reset32();
    status_t err = mAudioRecord->start(); // resets position to zero
    if (err != OK) {
        return AAudioConvert_androidToAAudioResult(err);
    } else {
@@ -349,12 +346,10 @@ aaudio_result_t AudioStreamRecord::requestStop() {
        return AAUDIO_ERROR_INVALID_STATE;
    }
    setState(AAUDIO_STREAM_STATE_STOPPING);
    incrementFramesWritten(getFramesRead() - getFramesWritten()); // TODO review
    mTimestampPosition.set(getFramesRead());
    mFramesWritten.catchUpTo(getFramesRead());
    mTimestampPosition.catchUpTo(getFramesRead());
    mAudioRecord->stop();
    mCallbackEnabled.store(false);
    mFramesWritten.reset32(); // service writes frames, service position reset on flush
    mTimestampPosition.reset32();
    // Pass false to prevent errorCallback from being called after disconnect
    // when app has already requested a stop().
    return checkForDisconnectRequest(false);
@@ -368,10 +363,12 @@ aaudio_result_t AudioStreamRecord::updateStateMachine()
    switch (getState()) {
    // TODO add better state visibility to AudioRecord
    case AAUDIO_STREAM_STATE_STARTING:
        // When starting, the position will begin at zero and then go positive.
        // The position can wrap but by that time the state will not be STARTING.
        err = mAudioRecord->getPosition(&position);
        if (err != OK) {
            result = AAudioConvert_androidToAAudioResult(err);
        } else if (position != mPositionWhenStarting) {
        } else if (position > 0) {
            setState(AAUDIO_STREAM_STATE_STARTED);
        }
        break;
@@ -504,12 +501,12 @@ int64_t AudioStreamRecord::getFramesWritten() {
    switch (getState()) {
        case AAUDIO_STREAM_STATE_STARTING:
        case AAUDIO_STREAM_STATE_STARTED:
        case AAUDIO_STREAM_STATE_STOPPING:
            result = mAudioRecord->getPosition(&position);
            if (result == OK) {
                mFramesWritten.update32(position);
            }
            break;
        case AAUDIO_STREAM_STATE_STOPPING:
        default:
            break;
    }
+2 −2
Original line number Diff line number Diff line
@@ -323,8 +323,8 @@ aaudio_result_t AudioStreamTrack::requestStop() {
    }

    setState(AAUDIO_STREAM_STATE_STOPPING);
    incrementFramesRead(getFramesWritten() - getFramesRead()); // TODO review
    mTimestampPosition.set(getFramesWritten());
    mFramesRead.catchUpTo(getFramesWritten());
    mTimestampPosition.catchUpTo(getFramesWritten());
    mFramesRead.reset32(); // service reads frames, service position reset on stop
    mTimestampPosition.reset32();
    mAudioTrack->stop();
+5 −3
Original line number Diff line number Diff line
@@ -41,11 +41,13 @@ public:
    }

    /**
     * set the current value of the counter
     * advance the current value to match the counter
     */
    void set(int64_t counter) {
    void catchUpTo(int64_t counter) {
        if ((counter - mCounter64) > 0) {
            mCounter64 = counter;
        }
    }

    /**
     * Advance the counter if delta is positive.