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

Commit 6a0d05c0 authored by Dorin Drimus's avatar Dorin Drimus
Browse files

Filter unsuported native audio formats in getDirectProfilesForAttributes

The audio HAL and native layer knows of more audio formats than the Java
layer for now. In getDirectProfilesForAttributes filter the invalid
encodings.

Bug: 231539208
Test: atest audiopolicy_tests
Test: atest DirectAudioProfilesForAttributesTest
Change-Id: I9f38c54d0466767c2690196183b3ea3c135618f5
parent db2310a2
Loading
Loading
Loading
Loading
+31 −8
Original line number Diff line number Diff line
@@ -1264,6 +1264,12 @@ static jint convertAudioProfileFromNative(JNIEnv *env, jobject *jAudioProfile,
    size_t numPositionMasks = 0;
    size_t numIndexMasks = 0;

    int audioFormat = audioFormatFromNative(nAudioProfile->format);
    if (audioFormat == ENCODING_INVALID) {
        ALOGW("Unknown native audio format for JAVA API: %u", nAudioProfile->format);
        return AUDIO_JAVA_BAD_VALUE;
    }

    // count up how many masks are positional and indexed
    for (size_t index = 0; index < nAudioProfile->num_channel_masks; index++) {
        const audio_channel_mask_t mask = nAudioProfile->channel_masks[index];
@@ -1306,10 +1312,9 @@ static jint convertAudioProfileFromNative(JNIEnv *env, jobject *jAudioProfile,
        ALOGW("Unknown encapsulation type for JAVA API: %u", nAudioProfile->encapsulation_type);
    }

    *jAudioProfile =
            env->NewObject(gAudioProfileClass, gAudioProfileCstor,
                           audioFormatFromNative(nAudioProfile->format), jSamplingRates.get(),
                           jChannelMasks.get(), jChannelIndexMasks.get(), encapsulationType);
    *jAudioProfile = env->NewObject(gAudioProfileClass, gAudioProfileCstor, audioFormat,
                                    jSamplingRates.get(), jChannelMasks.get(),
                                    jChannelIndexMasks.get(), encapsulationType);

    if (*jAudioProfile == nullptr) {
        return AUDIO_JAVA_ERROR;
@@ -1368,6 +1373,10 @@ static jint convertAudioPortFromNative(JNIEnv *env, jobject *jAudioPort,
        jobject jAudioProfile = nullptr;
        jStatus = convertAudioProfileFromNative(env, &jAudioProfile, &nAudioPort->audio_profiles[i],
                                                useInMask);
        if (jStatus == AUDIO_JAVA_BAD_VALUE) {
            // skipping Java layer unsupported audio formats
            continue;
        }
        if (jStatus != NO_ERROR) {
            jStatus = (jint)AUDIO_JAVA_ERROR;
            goto exit;
@@ -2406,8 +2415,13 @@ static jint android_media_AudioSystem_getSurroundFormats(JNIEnv *env, jobject th
        goto exit;
    }
    for (size_t i = 0; i < numSurroundFormats; i++) {
        jobject surroundFormat = env->NewObject(gIntegerClass, gIntegerCstor,
                                                audioFormatFromNative(surroundFormats[i]));
        int audioFormat = audioFormatFromNative(surroundFormats[i]);
        if (audioFormat == ENCODING_INVALID) {
            // skipping Java layer unsupported audio formats
            ALOGW("Unknown surround native audio format for JAVA API: %u", surroundFormats[i]);
            continue;
        }
        jobject surroundFormat = env->NewObject(gIntegerClass, gIntegerCstor, audioFormat);
        jobject enabled = env->NewObject(gBooleanClass, gBooleanCstor, surroundFormatsEnabled[i]);
        env->CallObjectMethod(jSurroundFormats, gMapPut, surroundFormat, enabled);
        env->DeleteLocalRef(surroundFormat);
@@ -2453,8 +2467,13 @@ static jint android_media_AudioSystem_getReportedSurroundFormats(JNIEnv *env, jo
        goto exit;
    }
    for (size_t i = 0; i < numSurroundFormats; i++) {
        jobject surroundFormat = env->NewObject(gIntegerClass, gIntegerCstor,
                                                audioFormatFromNative(surroundFormats[i]));
        int audioFormat = audioFormatFromNative(surroundFormats[i]);
        if (audioFormat == ENCODING_INVALID) {
            // skipping Java layer unsupported audio formats
            ALOGW("Unknown surround native audio format for JAVA API: %u", surroundFormats[i]);
            continue;
        }
        jobject surroundFormat = env->NewObject(gIntegerClass, gIntegerCstor, audioFormat);
        env->CallObjectMethod(jSurroundFormats, gArrayListMethods.add, surroundFormat);
        env->DeleteLocalRef(surroundFormat);
    }
@@ -2919,6 +2938,10 @@ static jint android_media_AudioSystem_getDirectProfilesForAttributes(JNIEnv *env
    for (const auto &audioProfile : audioProfiles) {
        jobject jAudioProfile;
        jStatus = convertAudioProfileFromNative(env, &jAudioProfile, &audioProfile, false);
        if (jStatus == AUDIO_JAVA_BAD_VALUE) {
            // skipping Java layer unsupported audio formats
            continue;
        }
        if (jStatus != AUDIO_JAVA_SUCCESS) {
            return jStatus;
        }