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

Commit 310037a3 authored by Robert Wu's avatar Robert Wu
Browse files

AAudio: Add AAudioStream_getHardware* functions

Add new methods to AAudioStream that allow for queries on the hardware
aspects of streams.

These will allow developers understand the hardware limitations of their
Android devices after they create AAudio streams.

Bug: 245412674
Test: atest AAudioTests
Change-Id: I104a738df89ab22aba9e0658b43402ddf053b887
parent d01cbef7
Loading
Loading
Loading
Loading
+52 −3
Original line number Diff line number Diff line
@@ -1694,10 +1694,26 @@ AAUDIO_API int32_t AAudioStream_getXRunCount(AAudioStream* stream) __INTRODUCED_
 * Available since API level 26.
 *
 * @param stream reference provided by AAudioStreamBuilder_openStream()
 * @return actual sample rate
 * @return actual sample rate of the stream
 */
AAUDIO_API int32_t AAudioStream_getSampleRate(AAudioStream* stream) __INTRODUCED_IN(26);

/**
 * There may be sample rate conversions in the Audio framework.
 * The sample rate set in the stream builder may not be actual sample rate used in the hardware.
 *
 * This returns the sample rate used by the hardware.
 *
 * If AAudioStreamBuilder_openStream() returned AAUDIO_OK, the result should always be valid.
 *
 * Available since API level 34.
 *
 * @param stream reference provided by AAudioStreamBuilder_openStream()
 * @return actual sample rate of the underlying hardware
 */
AAUDIO_API int32_t AAudioStream_getHardwareSampleRate(AAudioStream* stream)
        __INTRODUCED_IN(__ANDROID_API_U__);

/**
 * A stream has one or more channels of data.
 * A frame will contain one sample for each channel.
@@ -1705,10 +1721,27 @@ AAUDIO_API int32_t AAudioStream_getSampleRate(AAudioStream* stream) __INTRODUCED
 * Available since API level 26.
 *
 * @param stream reference provided by AAudioStreamBuilder_openStream()
 * @return actual number of channels
 * @return actual number of channels of the stream
 */
AAUDIO_API int32_t AAudioStream_getChannelCount(AAudioStream* stream) __INTRODUCED_IN(26);

/**
 * There may be channel conversions in the Audio framework.
 * The channel count or channel mask set in the stream builder may not be actual number of
 * channels used in the hardware.
 *
 * This returns the channel count used by the hardware.
 *
 * If AAudioStreamBuilder_openStream() returned AAUDIO_OK, the result should always be valid.
 *
 * Available since API level 34.
 *
 * @param stream reference provided by AAudioStreamBuilder_openStream()
 * @return actual number of channels of the underlying hardware
 */
AAUDIO_API int32_t AAudioStream_getHardwareChannelCount(AAudioStream* stream)
        __INTRODUCED_IN(__ANDROID_API_U__);

/**
 * Identical to AAudioStream_getChannelCount().
 *
@@ -1731,10 +1764,26 @@ AAUDIO_API int32_t AAudioStream_getDeviceId(AAudioStream* stream) __INTRODUCED_I
 * Available since API level 26.
 *
 * @param stream reference provided by AAudioStreamBuilder_openStream()
 * @return actual data format
 * @return actual data format of the stream
 */
AAUDIO_API aaudio_format_t AAudioStream_getFormat(AAudioStream* stream) __INTRODUCED_IN(26);

/**
 * There may be data format conversions in the Audio framework.
 * The data format set in the stream builder may not be actual format used in the hardware.
 *
 * This returns the audio format used by the hardware.
 * AUDIO_FORMAT_PCM_8_24_BIT is currently not supported in AAudio, but the hardware may use it.
 * If AUDIO_FORMAT_PCM_8_24_BIT is used by the hardware, return AAUDIO_FORMAT_PCM_I24_PACKED.
 *
 * Available since API level 34.
 *
 * @param stream reference provided by AAudioStreamBuilder_openStream()
 * @return actual data format of the underlying hardware.
 */
AAUDIO_API aaudio_format_t AAudioStream_getHardwareFormat(AAudioStream* stream)
        __INTRODUCED_IN(__ANDROID_API_U__);

/**
 * Provide actual sharing mode.
 *
+28 −1
Original line number Diff line number Diff line
@@ -40,6 +40,10 @@ AAudioStreamConfiguration::AAudioStreamConfiguration(const StreamParameters& par
    auto convFormat = android::aidl2legacy_AudioFormatDescription_audio_format_t(
            parcelable.audioFormat);
    setFormat(convFormat.ok() ? convFormat.value() : AUDIO_FORMAT_INVALID);
    if (!convFormat.ok()) {
        ALOGE("audioFormat (%s) aidl2legacy conversion failed",
              parcelable.hardwareAudioFormat.toString().c_str());
    }
    static_assert(sizeof(aaudio_direction_t) == sizeof(parcelable.direction));
    setDirection(parcelable.direction);
    static_assert(sizeof(audio_usage_t) == sizeof(parcelable.usage));
@@ -52,7 +56,6 @@ AAudioStreamConfiguration::AAudioStreamConfiguration(const StreamParameters& par
    setSpatializationBehavior(parcelable.spatializationBehavior);
    setIsContentSpatialized(parcelable.isContentSpatialized);


    static_assert(sizeof(aaudio_input_preset_t) == sizeof(parcelable.inputPreset));
    setInputPreset(parcelable.inputPreset);
    setBufferCapacity(parcelable.bufferCapacity);
@@ -62,6 +65,15 @@ AAudioStreamConfiguration::AAudioStreamConfiguration(const StreamParameters& par
    static_assert(sizeof(aaudio_session_id_t) == sizeof(parcelable.sessionId));
    setSessionId(parcelable.sessionId);
    setPrivacySensitive(parcelable.isPrivacySensitive);
    setHardwareSamplesPerFrame(parcelable.hardwareSamplesPerFrame);
    setHardwareSampleRate(parcelable.hardwareSampleRate);
    auto convHardwareFormat = android::aidl2legacy_AudioFormatDescription_audio_format_t(
            parcelable.hardwareAudioFormat);
    setHardwareFormat(convHardwareFormat.ok() ? convHardwareFormat.value() : AUDIO_FORMAT_INVALID);
    if (!convHardwareFormat.ok()) {
        ALOGE("hardwareAudioFormat (%s) aidl2legacy conversion failed",
              parcelable.hardwareAudioFormat.toString().c_str());
    }
}

AAudioStreamConfiguration&
@@ -82,6 +94,8 @@ StreamParameters AAudioStreamConfiguration::parcelable() const {
    if (convAudioFormat.ok()) {
        result.audioFormat = convAudioFormat.value();
    } else {
        ALOGE("audioFormat (%s) legacy2aidl conversion failed",
              audio_format_to_string(getFormat()));
        result.audioFormat = AudioFormatDescription{};
        result.audioFormat.type =
                android::media::audio::common::AudioFormatType::SYS_RESERVED_INVALID;
@@ -100,5 +114,18 @@ StreamParameters AAudioStreamConfiguration::parcelable() const {
    static_assert(sizeof(aaudio_session_id_t) == sizeof(result.sessionId));
    result.sessionId = getSessionId();
    result.isPrivacySensitive = isPrivacySensitive();
    result.hardwareSamplesPerFrame = getHardwareSamplesPerFrame();
    result.hardwareSampleRate = getHardwareSampleRate();
    auto convHardwareAudioFormat = android::legacy2aidl_audio_format_t_AudioFormatDescription(
            getHardwareFormat());
    if (convHardwareAudioFormat.ok()) {
        result.hardwareAudioFormat = convHardwareAudioFormat.value();
    } else {
        ALOGE("hardwareAudioFormat (%s) legacy2aidl conversion failed",
              audio_format_to_string(getHardwareFormat()));
        result.hardwareAudioFormat = AudioFormatDescription{};
        result.hardwareAudioFormat.type =
                android::media::audio::common::AudioFormatType::SYS_RESERVED_INVALID;
    }
    return result;
}
+3 −0
Original line number Diff line number Diff line
@@ -34,4 +34,7 @@ parcelable StreamParameters {
    int /* aaudio_allowed_capture_policy_t */ allowedCapturePolicy;  // = AAUDIO_UNSPECIFIED;
    int /* aaudio_session_id_t */             sessionId;  //            = AAUDIO_SESSION_ID_NONE;
    boolean                                   isPrivacySensitive;  //   = false;
    int                                       hardwareSamplesPerFrame;//= AAUDIO_UNSPECIFIED;
    int                                       hardwareSampleRate;  //   = AAUDIO_UNSPECIFIED;
    AudioFormatDescription                    hardwareAudioFormat;  //  = AUDIO_FORMAT_DEFAULT;
}
+8 −0
Original line number Diff line number Diff line
@@ -131,6 +131,10 @@ aaudio_result_t AudioStreamInternal::open(const AudioStreamBuilder &builder) {

    request.getConfiguration().setBufferCapacity(builder.getBufferCapacity());

    request.getConfiguration().setHardwareSamplesPerFrame(builder.getHardwareSamplesPerFrame());
    request.getConfiguration().setHardwareSampleRate(builder.getHardwareSampleRate());
    request.getConfiguration().setHardwareFormat(builder.getHardwareFormat());

    mDeviceChannelCount = getSamplesPerFrame(); // Assume it will be the same. Update if not.

    mServiceStreamHandle = mServiceInterface.openStream(request, configurationOutput);
@@ -192,6 +196,10 @@ aaudio_result_t AudioStreamInternal::open(const AudioStreamBuilder &builder) {
    // Save device format so we can do format conversion and volume scaling together.
    setDeviceFormat(configurationOutput.getFormat());

    setHardwareSamplesPerFrame(configurationOutput.getHardwareSamplesPerFrame());
    setHardwareSampleRate(configurationOutput.getHardwareSampleRate());
    setHardwareFormat(configurationOutput.getHardwareFormat());

    result = mServiceInterface.getStreamDescription(mServiceStreamHandle, mEndPointParcelable);
    if (result != AAUDIO_OK) {
        goto error;
+20 −0
Original line number Diff line number Diff line
@@ -418,12 +418,24 @@ AAUDIO_API int32_t AAudioStream_getSampleRate(AAudioStream* stream)
    return audioStream->getSampleRate();
}

AAUDIO_API int32_t AAudioStream_getHardwareSampleRate(AAudioStream* stream)
{
    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
    return audioStream->getHardwareSampleRate();
}

AAUDIO_API int32_t AAudioStream_getChannelCount(AAudioStream* stream)
{
    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
    return audioStream->getSamplesPerFrame();
}

AAUDIO_API int32_t AAudioStream_getHardwareChannelCount(AAudioStream* stream)
{
    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
    return audioStream->getHardwareSamplesPerFrame();
}

AAUDIO_API int32_t AAudioStream_getSamplesPerFrame(AAudioStream* stream)
{
    return AAudioStream_getChannelCount(stream);
@@ -443,6 +455,14 @@ AAUDIO_API aaudio_format_t AAudioStream_getFormat(AAudioStream* stream)
    return AAudioConvert_androidToAAudioDataFormat(internalFormat);
}

AAUDIO_API aaudio_format_t AAudioStream_getHardwareFormat(AAudioStream* stream)
{
    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
    // Use audio_format_t internally.
    audio_format_t internalFormat = audioStream->getHardwareFormat();
    return AAudioConvert_androidToNearestAAudioDataFormat(internalFormat);
}

AAUDIO_API aaudio_result_t AAudioStream_setBufferSizeInFrames(AAudioStream* stream,
                                                int32_t requestedFrames)
{
Loading