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

Commit 8d97b8e3 authored by Phil Burk's avatar Phil Burk
Browse files

aaudio: cache framesPerBurst and capacity

These do not change after opening the stream so we can
simply return a stored value and avoid querying the device.

Test: atest AAudioTests
Change-Id: I7a66c12bd695fd732194ff0a14ac9c8d9cacf923
parent 301e21d4
Loading
Loading
Loading
Loading
+8 −12
Original line number Diff line number Diff line
@@ -210,10 +210,10 @@ aaudio_result_t AudioStreamInternal::open(const AudioStreamBuilder &builder) {
        result = AAUDIO_ERROR_OUT_OF_RANGE;
        goto error;
    }
    mFramesPerBurst = framesPerBurst; // only save good value
    setFramesPerBurst(framesPerBurst); // only save good value

    mBufferCapacityInFrames = mEndpointDescriptor.dataQueueDescriptor.capacityInFrames;
    if (mBufferCapacityInFrames < mFramesPerBurst
    if (mBufferCapacityInFrames < getFramesPerBurst()
            || mBufferCapacityInFrames > MAX_BUFFER_CAPACITY_IN_FRAMES) {
        ALOGE("%s - bufferCapacity out of range = %d", __func__, mBufferCapacityInFrames);
        result = AAUDIO_ERROR_OUT_OF_RANGE;
@@ -238,7 +238,7 @@ aaudio_result_t AudioStreamInternal::open(const AudioStreamBuilder &builder) {

        }
        if (mCallbackFrames == AAUDIO_UNSPECIFIED) {
            mCallbackFrames = mFramesPerBurst;
            mCallbackFrames = getFramesPerBurst();
        }

        const int32_t callbackBufferSize = mCallbackFrames * getBytesPerFrame();
@@ -756,9 +756,9 @@ void AudioStreamInternal::processTimestamp(uint64_t position, int64_t time) {

aaudio_result_t AudioStreamInternal::setBufferSize(int32_t requestedFrames) {
    int32_t adjustedFrames = requestedFrames;
    const int32_t maximumSize = getBufferCapacity() - mFramesPerBurst;
    const int32_t maximumSize = getBufferCapacity() - getFramesPerBurst();
    // Minimum size should be a multiple number of bursts.
    const int32_t minimumSize = 1 * mFramesPerBurst;
    const int32_t minimumSize = 1 * getFramesPerBurst();

    // Clip to minimum size so that rounding up will work better.
    adjustedFrames = std::max(minimumSize, adjustedFrames);
@@ -768,9 +768,9 @@ aaudio_result_t AudioStreamInternal::setBufferSize(int32_t requestedFrames) {
        adjustedFrames = maximumSize;
    } else {
        // Round to the next highest burst size.
        int32_t numBursts = (adjustedFrames + mFramesPerBurst - 1) / mFramesPerBurst;
        adjustedFrames = numBursts * mFramesPerBurst;
        // Clip just in case maximumSize is not a multiple of mFramesPerBurst.
        int32_t numBursts = (adjustedFrames + getFramesPerBurst() - 1) / getFramesPerBurst();
        adjustedFrames = numBursts * getFramesPerBurst();
        // Clip just in case maximumSize is not a multiple of getFramesPerBurst().
        adjustedFrames = std::min(maximumSize, adjustedFrames);
    }

@@ -805,10 +805,6 @@ int32_t AudioStreamInternal::getBufferCapacity() const {
    return mBufferCapacityInFrames;
}

int32_t AudioStreamInternal::getFramesPerBurst() const {
    return mFramesPerBurst;
}

// This must be called under mStreamLock.
aaudio_result_t AudioStreamInternal::joinThread(void** returnArg) {
    return AudioStream::joinThread(returnArg, calculateReasonableTimeout(getFramesPerBurst()));
+0 −3
Original line number Diff line number Diff line
@@ -64,8 +64,6 @@ public:

    int32_t getBufferCapacity() const override;

    int32_t getFramesPerBurst() const override;

    int32_t getXRunCount() const override {
        return mXRunCount;
    }
@@ -159,7 +157,6 @@ protected:

    aaudio_handle_t          mServiceStreamHandle; // opaque handle returned from service

    int32_t                  mFramesPerBurst = MIN_FRAMES_PER_BURST; // frames per HAL transfer
    int32_t                  mXRunCount = 0;      // how many underrun events?

    // Offset from underlying frame position.
+1 −1
Original line number Diff line number Diff line
@@ -149,7 +149,7 @@ aaudio_result_t AudioStreamInternalCapture::processDataNow(void *buffer, int32_t
                // Calculate frame position based off of the readCounter because
                // the writeCounter might have just advanced in the background,
                // causing us to sleep until a later burst.
                int64_t nextPosition = mAudioEndpoint->getDataReadCounter() + mFramesPerBurst;
                int64_t nextPosition = mAudioEndpoint->getDataReadCounter() + getFramesPerBurst();
                wakeTime = mClockModel.convertPositionToLatestTime(nextPosition);
            }
                break;
+19 −14
Original line number Diff line number Diff line
@@ -202,11 +202,11 @@ public:
    }

    virtual int32_t getBufferCapacity() const {
        return AAUDIO_ERROR_UNIMPLEMENTED;
        return mBufferCapacity;
    }

    virtual int32_t getFramesPerBurst() const {
        return AAUDIO_ERROR_UNIMPLEMENTED;
        return mFramesPerBurst;
    }

    virtual int32_t getXRunCount() const {
@@ -498,30 +498,32 @@ protected:
        mSampleRate = sampleRate;
    }

    /**
     * This should not be called after the open() call.
     */
    // This should not be called after the open() call.
    void setSamplesPerFrame(int32_t samplesPerFrame) {
        mSamplesPerFrame = samplesPerFrame;
    }

    /**
     * This should not be called after the open() call.
     */
    // This should not be called after the open() call.
    void setFramesPerBurst(int32_t framesPerBurst) {
        mFramesPerBurst = framesPerBurst;
    }

    // This should not be called after the open() call.
    void setBufferCapacity(int32_t bufferCapacity) {
        mBufferCapacity = bufferCapacity;
    }

    // This should not be called after the open() call.
    void setSharingMode(aaudio_sharing_mode_t sharingMode) {
        mSharingMode = sharingMode;
    }

    /**
     * This should not be called after the open() call.
     */
    // This should not be called after the open() call.
    void setFormat(audio_format_t format) {
        mFormat = format;
    }

    /**
     * This should not be called after the open() call.
     */
    // This should not be called after the open() call.
    void setDeviceFormat(audio_format_t format) {
        mDeviceFormat = format;
    }
@@ -536,6 +538,7 @@ protected:
        mDeviceId = deviceId;
    }

    // This should not be called after the open() call.
    void setSessionId(int32_t sessionId) {
        mSessionId = sessionId;
    }
@@ -623,6 +626,8 @@ private:
    audio_format_t              mFormat = AUDIO_FORMAT_DEFAULT;
    aaudio_stream_state_t       mState = AAUDIO_STREAM_STATE_UNINITIALIZED;
    aaudio_performance_mode_t   mPerformanceMode = AAUDIO_PERFORMANCE_MODE_NONE;
    int32_t                     mFramesPerBurst = 0;
    int32_t                     mBufferCapacity = 0;

    aaudio_usage_t              mUsage           = AAUDIO_UNSPECIFIED;
    aaudio_content_type_t       mContentType     = AAUDIO_UNSPECIFIED;
+12 −0
Original line number Diff line number Diff line
@@ -112,6 +112,18 @@ protected:
        return mFramesRead.increment(frames);
    }

    /**
     * Get the framesPerBurst from the underlying API.
     * @return framesPerBurst
     */
    virtual int32_t getFramesPerBurstFromDevice() const = 0;

    /**
     * Get the bufferCapacity from the underlying API.
     * @return bufferCapacity in frames
     */
    virtual int32_t getBufferCapacityFromDevice() const = 0;

    // This is used for exact matching by MediaMetrics. So do not change it.
    // MediaMetricsConstants.h: AMEDIAMETRICS_PROP_CALLERNAME_VALUE_AAUDIO
    static constexpr char     kCallerName[] = "aaudio";
Loading