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

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

Merge changes I93635f9b,I805905ff

* changes:
  aaudio: fix framesWritten for Legacy INPUT
  aaudio test_return_stop prints frames in buffer
parents ae340bde 18c8476f
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.
+37 −16
Original line number Diff line number Diff line
@@ -140,7 +140,7 @@ static void s_myErrorCallbackProc(
    printf("%s() - error = %d\n", __func__, error);
}

void usage() {
static void s_usage() {
    printf("test_return_stop [-i] [-x] [-n] [-c]\n");
    printf("     -i direction INPUT, otherwise OUTPUT\n");
    printf("     -x sharing mode EXCLUSIVE, otherwise SHARED\n");
@@ -148,6 +148,28 @@ void usage() {
    printf("     -c always return CONTINUE from callback, not STOP\n");
}

/**
 * @return 0 is OK, -1 for error
 */
static int s_checkEnginePositions(AudioEngine *engine) {
    const int64_t framesRead = AAudioStream_getFramesRead(engine->stream);
    const int64_t framesWritten = AAudioStream_getFramesWritten(engine->stream);
    const int32_t delta = (int32_t)(framesWritten - framesRead);
    printf("playing framesRead = %7d, framesWritten = %7d"
           ", delta = %4d, framesCalled = %6d, callbackCount = %4d\n",
           (int32_t) framesRead,
           (int32_t) framesWritten,
           delta,
           engine->framesCalled.load(),
           engine->callbackCount.load()
    );
    if (delta > AAudioStream_getBufferCapacityInFrames(engine->stream)) {
        printf("ERROR - delta > capacity\n");
        return -1;
    }
    return 0;
}

int main(int argc, char **argv) {
    (void) argc;
    (void) argv;
@@ -188,12 +210,12 @@ int main(int argc, char **argv) {
                    sharingMode = AAUDIO_SHARING_MODE_EXCLUSIVE;
                    break;
                default:
                    usage();
                    s_usage();
                    exit(EXIT_FAILURE);
                    break;
            }
        } else {
            usage();
            s_usage();
            exit(EXIT_FAILURE);
            break;
        }
@@ -201,12 +223,20 @@ int main(int argc, char **argv) {

    result = s_OpenAudioStream(&engine, direction, sharingMode, perfMode);
    if (result != AAUDIO_OK) {
        printf("s_OpenAudioStream returned %s",
        printf("s_OpenAudioStream returned %s\n",
               AAudio_convertResultToText(result));
        errorCount++;
    }

    int32_t framesPerBurst = AAudioStream_getFramesPerBurst(engine.stream);
    // Use double buffered stream.
    const int32_t bufferSize = AAudioStream_setBufferSizeInFrames(engine.stream, 2 * framesPerBurst);
    if (bufferSize < 0) {
        printf("AAudioStream_setBufferSizeInFrames returned %s\n",
               AAudio_convertResultToText(bufferSize));
        errorCount++;
    }

    // Check to see what kind of stream we actually got.
    int32_t deviceId = AAudioStream_getDeviceId(engine.stream);
    aaudio_performance_mode_t actualPerfMode = AAudioStream_getPerformanceMode(engine.stream);
@@ -235,14 +265,7 @@ int main(int argc, char **argv) {
        if (result == AAUDIO_OK) {
            const int watchLoops = LOOP_DURATION_MSEC / SLEEP_DURATION_MSEC;
            for (int i = watchLoops; i > 0; i--) {
                printf("playing silence #%02d, framesRead = %7d, framesWritten = %7d,"
                       " framesCalled = %6d, callbackCount = %4d\n",
                       i,
                       (int32_t) AAudioStream_getFramesRead(engine.stream),
                       (int32_t) AAudioStream_getFramesWritten(engine.stream),
                       engine.framesCalled.load(),
                       engine.callbackCount.load()
                );
                errorCount += s_checkEnginePositions(&engine) ? 1 : 0;
                usleep(SLEEP_DURATION_MSEC * 1000);
            }
        }
@@ -268,9 +291,7 @@ int main(int argc, char **argv) {
            errorCount++;
        }
        usleep(SLEEP_DURATION_MSEC * 1000);
        printf("getFramesRead() = %d, getFramesWritten() = %d\n",
               (int32_t) AAudioStream_getFramesRead(engine.stream),
               (int32_t) AAudioStream_getFramesWritten(engine.stream));
        errorCount += s_checkEnginePositions(&engine) ? 1 : 0;
    }

    s_CloseAudioStream(&engine);