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

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

Merge "aaudio: fix race at start of stream"

parents 9eea5143 ec8ca529
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -353,6 +353,8 @@ aaudio_result_t AudioStreamInternal::requestStart()
    // Clear any stale timestamps from the previous run.
    drainTimestampsFromService();

    prepareBuffersForStart(); // tell subclasses to get ready

    aaudio_result_t result = mServiceInterface.startStream(mServiceStreamHandle);
    if (result == AAUDIO_ERROR_INVALID_HANDLE) {
        ALOGD("%s() INVALID_HANDLE, stream was probably stolen", __func__);
+3 −1
Original line number Diff line number Diff line
@@ -123,7 +123,9 @@ protected:

    aaudio_result_t stopCallback();

    virtual void advanceClientToMatchServerPosition() = 0;
    virtual void prepareBuffersForStart() {}

    virtual void advanceClientToMatchServerPosition(int32_t serverMargin = 0) = 0;

    virtual void onFlushFromServer() {}

+2 −2
Original line number Diff line number Diff line
@@ -41,9 +41,9 @@ AudioStreamInternalCapture::AudioStreamInternalCapture(AAudioServiceInterface &

AudioStreamInternalCapture::~AudioStreamInternalCapture() {}

void AudioStreamInternalCapture::advanceClientToMatchServerPosition() {
void AudioStreamInternalCapture::advanceClientToMatchServerPosition(int32_t serverMargin) {
    int64_t readCounter = mAudioEndpoint->getDataReadCounter();
    int64_t writeCounter = mAudioEndpoint->getDataWriteCounter();
    int64_t writeCounter = mAudioEndpoint->getDataWriteCounter() + serverMargin;

    // Bump offset so caller does not see the retrograde motion in getFramesRead().
    int64_t offset = readCounter - writeCounter;
+1 −1
Original line number Diff line number Diff line
@@ -46,7 +46,7 @@ public:
    }
protected:

    void advanceClientToMatchServerPosition() override;
    void advanceClientToMatchServerPosition(int32_t serverOffset = 0) override;

/**
 * Low level data processing that will not block. It will just read or write as much as it can.
+10 −3
Original line number Diff line number Diff line
@@ -86,8 +86,13 @@ aaudio_result_t AudioStreamInternalPlay::requestFlush() {
    return mServiceInterface.flushStream(mServiceStreamHandle);
}

void AudioStreamInternalPlay::advanceClientToMatchServerPosition() {
    int64_t readCounter = mAudioEndpoint->getDataReadCounter();
void AudioStreamInternalPlay::prepareBuffersForStart() {
    // Prevent stale data from being played.
    mAudioEndpoint->eraseDataMemory();
}

void AudioStreamInternalPlay::advanceClientToMatchServerPosition(int32_t serverMargin) {
    int64_t readCounter = mAudioEndpoint->getDataReadCounter() + serverMargin;
    int64_t writeCounter = mAudioEndpoint->getDataWriteCounter();

    // Bump offset so caller does not see the retrograde motion in getFramesRead().
@@ -145,7 +150,9 @@ aaudio_result_t AudioStreamInternalPlay::processDataNow(void *buffer, int32_t nu
    if (mNeedCatchUp.isRequested()) {
        // Catch an MMAP pointer that is already advancing.
        // This will avoid initial underruns caused by a slow cold start.
        advanceClientToMatchServerPosition();
        // We add a one burst margin in case the DSP advances before we can write the data.
        // This can help prevent the beginning of the stream from being skipped.
        advanceClientToMatchServerPosition(getFramesPerBurst());
        mNeedCatchUp.acknowledge();
    }

Loading