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

Commit 26b28087 authored by Phil Burk's avatar Phil Burk Committed by Android (Google) Code Review
Browse files

Merge "aaudio: set performance mode of input stream based on actual result" into oc-dev

parents c2c3e7cb c42d5f9b
Loading
Loading
Loading
Loading
+14 −4
Original line number Diff line number Diff line
@@ -24,7 +24,7 @@
#include <aaudio/AAudio.h>

#define SAMPLE_RATE        48000
#define NUM_SECONDS           6
#define NUM_SECONDS        5
#define NANOS_PER_MICROSECOND ((int64_t)1000)
#define NANOS_PER_MILLISECOND (NANOS_PER_MICROSECOND * 1000)
#define NANOS_PER_SECOND      (NANOS_PER_MILLISECOND * 1000)
@@ -57,6 +57,11 @@ int main(int argc, char **argv)
    const aaudio_audio_format_t requestedDataFormat = AAUDIO_FORMAT_PCM_I16;
    aaudio_audio_format_t actualDataFormat;

    const int requestedInputChannelCount = 1; // Can affect whether we get a FAST path.

    //aaudio_performance_mode_t requestedPerformanceMode = AAUDIO_PERFORMANCE_MODE_NONE;
    const aaudio_performance_mode_t requestedPerformanceMode = AAUDIO_PERFORMANCE_MODE_LOW_LATENCY;
    //aaudio_performance_mode_t requestedPerformanceMode = AAUDIO_PERFORMANCE_MODE_POWER_SAVING;
    const aaudio_sharing_mode_t requestedSharingMode = AAUDIO_SHARING_MODE_SHARED;
    //const aaudio_sharing_mode_t requestedSharingMode = AAUDIO_SHARING_MODE_EXCLUSIVE;
    aaudio_sharing_mode_t actualSharingMode;
@@ -89,6 +94,8 @@ int main(int argc, char **argv)
    AAudioStreamBuilder_setDirection(aaudioBuilder, AAUDIO_DIRECTION_INPUT);
    AAudioStreamBuilder_setFormat(aaudioBuilder, requestedDataFormat);
    AAudioStreamBuilder_setSharingMode(aaudioBuilder, requestedSharingMode);
    AAudioStreamBuilder_setPerformanceMode(aaudioBuilder, requestedPerformanceMode);
    AAudioStreamBuilder_setChannelCount(aaudioBuilder, requestedInputChannelCount);

    // Create an AAudioStream using the Builder.
    result = AAudioStreamBuilder_openStream(aaudioBuilder, &aaudioStream);
@@ -124,6 +131,9 @@ int main(int argc, char **argv)
    // TODO handle other data formats
    assert(actualDataFormat == AAUDIO_FORMAT_PCM_I16);

    printf("PerformanceMode: requested = %d, actual = %d\n", requestedPerformanceMode,
           AAudioStream_getPerformanceMode(aaudioStream));

    // Allocate a buffer for the audio data.
    data = new(std::nothrow) int16_t[framesPerRead * actualSamplesPerFrame];
    if (data == nullptr) {
+20 −1
Original line number Diff line number Diff line
@@ -69,7 +69,8 @@ aaudio_result_t AudioStreamRecord::open(const AudioStreamBuilder& builder)
            : AAudioConvert_aaudioToAndroidDataFormat(getFormat());

    audio_input_flags_t flags = AUDIO_INPUT_FLAG_NONE;
    switch(getPerformanceMode()) {
    aaudio_performance_mode_t perfMode = getPerformanceMode();
    switch (perfMode) {
        case AAUDIO_PERFORMANCE_MODE_LOW_LATENCY:
            flags = (audio_input_flags_t) (AUDIO_INPUT_FLAG_FAST | AUDIO_INPUT_FLAG_RAW);
            break;
@@ -135,6 +136,24 @@ aaudio_result_t AudioStreamRecord::open(const AudioStreamBuilder& builder)
        mBlockAdapter = nullptr;
    }

    // Update performance mode based on the actual stream.
    // For example, if the sample rate does not match native then you won't get a FAST track.
    audio_input_flags_t actualFlags = mAudioRecord->getFlags();
    aaudio_performance_mode_t actualPerformanceMode = AAUDIO_PERFORMANCE_MODE_NONE;
    // FIXME Some platforms do not advertise RAW mode for low latency inputs.
    if ((actualFlags & (AUDIO_INPUT_FLAG_FAST))
        == (AUDIO_INPUT_FLAG_FAST)) {
        actualPerformanceMode = AAUDIO_PERFORMANCE_MODE_LOW_LATENCY;
    }
    setPerformanceMode(actualPerformanceMode);
    // Log warning if we did not get what we asked for.
    ALOGW_IF(actualFlags != flags,
             "AudioStreamRecord::open() flags changed from 0x%08X to 0x%08X",
             flags, actualFlags);
    ALOGW_IF(actualPerformanceMode != perfMode,
             "AudioStreamRecord::open() perfMode changed from %d to %d",
             perfMode, actualPerformanceMode);

    setState(AAUDIO_STREAM_STATE_OPEN);

    return AAUDIO_OK;