From 6a0d05c00a987be62f22af1919e580c1a7361d31 Mon Sep 17 00:00:00 2001 From: Dorin Drimus Date: Fri, 6 May 2022 15:39:27 +0200 Subject: [PATCH] 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 --- core/jni/android_media_AudioSystem.cpp | 39 ++++++++++++++++++++------ 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/core/jni/android_media_AudioSystem.cpp b/core/jni/android_media_AudioSystem.cpp index b6fbe206a262d..f24c66695052d 100644 --- a/core/jni/android_media_AudioSystem.cpp +++ b/core/jni/android_media_AudioSystem.cpp @@ -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; }