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

Commit 029aa6de authored by Robert Wu's avatar Robert Wu
Browse files

AAudio: Scale capacity by device sample rate

We should scale capacity by the device sample rate instead of 48000.
Although 48000 works in most cases, some use cases don't use 48000 as
the default sample rate sample rate.

This involves adding this calculation in two separate places for the
exclusive and shared cases.

Bug: 382715299
Test: atest AAudioTests
Test: OboeTester Test Glitches
Flag: EXEMPT bugfix
Change-Id: Ie3b6bb69dd56abed029e39d377976baa7ddb49bc
parent 9b70ca24
Loading
Loading
Loading
Loading
+1 −6
Original line number Diff line number Diff line
@@ -134,12 +134,7 @@ aaudio_result_t AudioStreamInternal::open(const AudioStreamBuilder &builder) {
    request.getConfiguration().setInputPreset(getInputPreset());
    request.getConfiguration().setPrivacySensitive(isPrivacySensitive());

    // When sample rate conversion is needed, we use the device sample rate instead of the
    // requested sample rate to scale the capacity in configureDataInformation().
    // Thus, we should scale the capacity here to cancel out the (sampleRate / deviceSampleRate)
    // scaling there.
    request.getConfiguration().setBufferCapacity(builder.getBufferCapacity()
            * 48000 / getSampleRate());
    request.getConfiguration().setBufferCapacity(builder.getBufferCapacity());

    mServiceStreamHandleInfo = mServiceInterface.openStream(request, configurationOutput);
    if (getServiceHandle() < 0
+15 −0
Original line number Diff line number Diff line
@@ -243,6 +243,21 @@ aaudio_result_t AAudioServiceEndpointMMAP::openWithConfig(
    ALOGD("%s(format = 0x%X) deviceIds = %s, sessionId = %d",
          __func__, config->format, toString(getDeviceIds()).c_str(), getSessionId());

    ALOGD("%s bufferCapacity = %d, deviceSampleRate = %d, requestedSampleRate = %d",
          __func__, getBufferCapacity(), config->sample_rate, getSampleRate());

    const int32_t requestedSampleRate = getSampleRate();
    const int32_t deviceSampleRate = config->sample_rate;

    // When sample rate conversion is needed, we use the device sample rate and the
    // requested sample rate to scale the capacity in configureDataInformation().
    // Thus, we should scale the capacity here to cancel out the
    // (requestedSampleRate / deviceSampleRate) scaling there.
    if (requestedSampleRate != AAUDIO_UNSPECIFIED && requestedSampleRate != deviceSampleRate) {
        setBufferCapacity(static_cast<int64_t>(getBufferCapacity()) * deviceSampleRate /
                          requestedSampleRate);
    }

    // Create MMAP/NOIRQ buffer.
    result = createMmapBuffer_l();
    if (result != AAUDIO_OK) {
+17 −2
Original line number Diff line number Diff line
@@ -76,7 +76,21 @@ std::string AAudioServiceStreamShared::dump() const NO_THREAD_SAFETY_ANALYSIS {
}

int32_t AAudioServiceStreamShared::calculateBufferCapacity(int32_t requestedCapacityFrames,
                                                           int32_t framesPerBurst) {
                                                           int32_t framesPerBurst,
                                                           int32_t requestedSampleRate,
                                                           int32_t deviceSampleRate) {
    if (requestedSampleRate != AAUDIO_UNSPECIFIED && requestedSampleRate != deviceSampleRate) {
        // When sample rate conversion is needed, we use the device sample rate and the
        // requested sample rate to scale the capacity in configureDataInformation().
        // Thus, we should scale the capacity here to cancel out the
        // (requestedSampleRate / deviceSampleRate) scaling there.

        requestedCapacityFrames = static_cast<int64_t>(requestedCapacityFrames) * deviceSampleRate
                                  / requestedSampleRate;
        ALOGV("calculateBufferCapacity() scaled buffer capacity to %d frames, requested SR = %d"
              ", device SR = %d",
              requestedCapacityFrames, requestedSampleRate, deviceSampleRate);
    }

    if (requestedCapacityFrames > MAX_FRAMES_PER_BUFFER) {
        ALOGE("calculateBufferCapacity() requested capacity %d > max %d",
@@ -168,7 +182,8 @@ aaudio_result_t AAudioServiceStreamShared::open(const aaudio::AAudioStreamReques
    }

    setBufferCapacity(calculateBufferCapacity(configurationInput.getBufferCapacity(),
                                     mFramesPerBurst));
                                              mFramesPerBurst, configurationInput.getSampleRate(),
                                              getSampleRate()));
    if (getBufferCapacity() < 0) {
        result = getBufferCapacity(); // negative error code
        setBufferCapacity(0);
+3 −1
Original line number Diff line number Diff line
@@ -104,7 +104,9 @@ protected:
     * @return capacity or negative error
     */
    static int32_t calculateBufferCapacity(int32_t requestedCapacityFrames,
                                            int32_t framesPerBurst);
                                           int32_t framesPerBurst,
                                           int32_t requestedSampleRate,
                                           int32_t deviceSampleRate);

private: