audio: Use auto-generated MicrophoneInfoFw class

Replace use of manually written MicrophoneInfo class
with MicrophoneInfoFw which is generated automatically
from the AIDL definition.

Bug: 263964254
Test: m
Change-Id: I61e842ad0dbad970c1b6ddfe6c72fcda83b5843c
Merged-In: I61e842ad0dbad970c1b6ddfe6c72fcda83b5843c
This commit is contained in:
Mikhail Naganov 2023-02-13 11:43:12 -08:00
parent 3cac9278de
commit 3aa51da158
5 changed files with 51 additions and 70 deletions

View File

@ -18,29 +18,25 @@
#define LOG_TAG "AudioRecord-JNI"
#include <inttypes.h>
#include <jni.h>
#include <nativehelper/JNIHelp.h>
#include "core_jni_helpers.h"
#include <utils/Log.h>
#include <media/AudioRecord.h>
#include <media/MicrophoneInfo.h>
#include <vector>
#include <android/content/AttributionSourceState.h>
#include <android_os_Parcel.h>
#include <inttypes.h>
#include <jni.h>
#include <media/AudioRecord.h>
#include <nativehelper/JNIHelp.h>
#include <nativehelper/ScopedUtfChars.h>
#include <utils/Log.h>
#include <vector>
#include "android_media_AudioAttributes.h"
#include "android_media_AudioFormat.h"
#include "android_media_AudioErrors.h"
#include "android_media_AudioFormat.h"
#include "android_media_DeviceCallback.h"
#include "android_media_JNIUtils.h"
#include "android_media_MediaMetricsJNI.h"
#include "android_media_MicrophoneInfo.h"
#include "core_jni_helpers.h"
// ----------------------------------------------------------------------------
@ -721,7 +717,7 @@ static jint android_media_AudioRecord_get_active_microphones(JNIEnv *env,
}
jint jStatus = AUDIO_JAVA_SUCCESS;
std::vector<media::MicrophoneInfo> activeMicrophones;
std::vector<media::MicrophoneInfoFw> activeMicrophones;
status_t status = lpRecorder->getActiveMicrophones(&activeMicrophones);
if (status != NO_ERROR) {
ALOGE_IF(status != NO_ERROR, "AudioRecord::getActiveMicrophones error %d", status);

View File

@ -27,7 +27,6 @@
#include <media/AudioContainers.h>
#include <media/AudioPolicy.h>
#include <media/AudioSystem.h>
#include <media/MicrophoneInfo.h>
#include <nativehelper/JNIHelp.h>
#include <nativehelper/ScopedLocalRef.h>
#include <system/audio.h>
@ -2325,7 +2324,7 @@ android_media_AudioSystem_getMicrophones(JNIEnv *env, jobject thiz, jobject jMic
}
jint jStatus;
std::vector<media::MicrophoneInfo> microphones;
std::vector<media::MicrophoneInfoFw> microphones;
status_t status = AudioSystem::getMicrophones(&microphones);
if (status != NO_ERROR) {
ALOGE("AudioSystem::getMicrophones error %d", status);

View File

@ -15,6 +15,9 @@
*/
#include "android_media_MicrophoneInfo.h"
#include <media/AidlConversion.h>
#include "android_media_AudioErrors.h"
#include "core_jni_helpers.h"
@ -46,8 +49,17 @@ static jmethodID gPairCstor;
namespace android {
jint convertMicrophoneInfoFromNative(JNIEnv *env, jobject *jMicrophoneInfo,
const media::MicrophoneInfo *microphoneInfo)
{
const media::MicrophoneInfoFw *microphoneInfo) {
// The Java object uses the same enum values as the C enum values, which are
// generated from HIDL. Thus, we can use the legacy structure as the source for
// creating the Java object. Once we start removing legacy types, we can add
// direct converters between Java and AIDL, this will eliminate the need
// to have JNI code like this one.
auto conv = aidl2legacy_MicrophoneInfoFw_audio_microphone_characteristic_t(*microphoneInfo);
if (!conv.ok()) {
return nativeToJavaStatus(conv.error());
}
jint jStatus = (jint)AUDIO_JAVA_SUCCESS;
jstring jDeviceId = NULL;
jstring jAddress = NULL;
@ -56,36 +68,23 @@ jint convertMicrophoneInfoFromNative(JNIEnv *env, jobject *jMicrophoneInfo,
jobject jFrequencyResponses = NULL;
jobject jChannelMappings = NULL;
jDeviceId = env->NewStringUTF(microphoneInfo->getDeviceId().c_str());
jAddress = env->NewStringUTF(microphoneInfo->getAddress().c_str());
if (microphoneInfo->getGeometricLocation().size() != 3 ||
microphoneInfo->getOrientation().size() != 3) {
jStatus = nativeToJavaStatus(BAD_VALUE);
goto exit;
}
jGeometricLocation = env->NewObject(gMicrophoneInfoCoordinateClass,
gMicrophoneInfoCoordinateCstor,
microphoneInfo->getGeometricLocation()[0],
microphoneInfo->getGeometricLocation()[1],
microphoneInfo->getGeometricLocation()[2]);
jOrientation = env->NewObject(gMicrophoneInfoCoordinateClass,
gMicrophoneInfoCoordinateCstor,
microphoneInfo->getOrientation()[0],
microphoneInfo->getOrientation()[1],
microphoneInfo->getOrientation()[2]);
const auto &micInfo = conv.value();
jDeviceId = env->NewStringUTF(micInfo.device_id);
jAddress = env->NewStringUTF(micInfo.address);
jGeometricLocation =
env->NewObject(gMicrophoneInfoCoordinateClass, gMicrophoneInfoCoordinateCstor,
micInfo.geometric_location.x, micInfo.geometric_location.y,
micInfo.geometric_location.z);
jOrientation =
env->NewObject(gMicrophoneInfoCoordinateClass, gMicrophoneInfoCoordinateCstor,
micInfo.orientation.x, micInfo.orientation.y, micInfo.orientation.z);
// Create a list of Pair for frequency response.
if (microphoneInfo->getFrequencyResponses().size() != 2 ||
microphoneInfo->getFrequencyResponses()[0].size() !=
microphoneInfo->getFrequencyResponses()[1].size()) {
jStatus = nativeToJavaStatus(BAD_VALUE);
goto exit;
}
jFrequencyResponses = env->NewObject(gArrayListClass, gArrayListCstor);
for (size_t i = 0; i < microphoneInfo->getFrequencyResponses()[0].size(); i++) {
jobject jFrequency = env->NewObject(gFloatClass, gFloatCstor,
microphoneInfo->getFrequencyResponses()[0][i]);
jobject jResponse = env->NewObject(gFloatClass, gFloatCstor,
microphoneInfo->getFrequencyResponses()[1][i]);
for (size_t i = 0; i < micInfo.num_frequency_responses; i++) {
jobject jFrequency =
env->NewObject(gFloatClass, gFloatCstor, micInfo.frequency_responses[0][i]);
jobject jResponse =
env->NewObject(gFloatClass, gFloatCstor, micInfo.frequency_responses[1][i]);
jobject jFrequencyResponse = env->NewObject(gPairClass, gPairCstor, jFrequency, jResponse);
env->CallBooleanMethod(jFrequencyResponses, gArrayListMethods.add, jFrequencyResponse);
env->DeleteLocalRef(jFrequency);
@ -93,13 +92,9 @@ jint convertMicrophoneInfoFromNative(JNIEnv *env, jobject *jMicrophoneInfo,
env->DeleteLocalRef(jFrequencyResponse);
}
// Create a list of Pair for channel mapping.
if (microphoneInfo->getChannelMapping().size() != AUDIO_CHANNEL_COUNT_MAX) {
jStatus = nativeToJavaStatus(BAD_VALUE);
goto exit;
}
jChannelMappings = env->NewObject(gArrayListClass, gArrayListCstor);
for (size_t i = 0; i < microphoneInfo->getChannelMapping().size(); i++) {
int channelMappingType = microphoneInfo->getChannelMapping()[i];
const auto &channelMapping = micInfo.channel_mapping;
for (size_t i = 0; i < std::size(channelMapping); i++) {
int channelMappingType = channelMapping[i];
if (channelMappingType != AUDIO_MICROPHONE_CHANNEL_MAPPING_UNUSED) {
jobject jChannelIndex = env->NewObject(gIntegerClass, gIntegerCstor, i);
jobject jChannelMappingType = env->NewObject(gIntegerClass, gIntegerCstor,
@ -113,18 +108,11 @@ jint convertMicrophoneInfoFromNative(JNIEnv *env, jobject *jMicrophoneInfo,
}
}
*jMicrophoneInfo = env->NewObject(gMicrophoneInfoClass, gMicrophoneInfoCstor, jDeviceId,
microphoneInfo->getType(), jAddress,
microphoneInfo->getDeviceLocation(),
microphoneInfo->getDeviceGroup(),
microphoneInfo->getIndexInTheGroup(),
jGeometricLocation, jOrientation,
jFrequencyResponses, jChannelMappings,
microphoneInfo->getSensitivity(),
microphoneInfo->getMaxSpl(),
microphoneInfo->getMinSpl(),
microphoneInfo->getDirectionality());
micInfo.device, jAddress, micInfo.location, micInfo.group,
micInfo.index_in_the_group, jGeometricLocation, jOrientation,
jFrequencyResponses, jChannelMappings, micInfo.sensitivity,
micInfo.max_spl, micInfo.min_spl, micInfo.directionality);
exit:
if (jDeviceId != NULL) {
env->DeleteLocalRef(jDeviceId);
}
@ -145,7 +133,6 @@ exit:
}
return jStatus;
}
}
int register_android_media_MicrophoneInfo(JNIEnv *env)

View File

@ -17,8 +17,8 @@
#ifndef ANDROID_MEDIA_MICROPHONEINFO_H
#define ANDROID_MEDIA_MICROPHONEINFO_H
#include <android/media/MicrophoneInfoFw.h>
#include <system/audio.h>
#include <media/MicrophoneInfo.h>
#include "jni.h"
@ -27,7 +27,7 @@ namespace android {
// Conversion from C++ MicrophoneInfo object to Java MicrophoneInfo object
extern jint convertMicrophoneInfoFromNative(JNIEnv *env, jobject *jMicrophoneInfo,
const media::MicrophoneInfo *microphoneInfo);
const media::MicrophoneInfoFw *microphoneInfo);
} // namespace android
#endif
#endif

View File

@ -30,7 +30,6 @@
#include <camera/Camera.h>
#include <media/mediarecorder.h>
#include <media/MediaMetricsItem.h>
#include <media/MicrophoneInfo.h>
#include <media/stagefright/PersistentSurface.h>
#include <utils/threads.h>
@ -774,7 +773,7 @@ android_media_MediaRecord_getActiveMicrophones(JNIEnv *env,
}
jint jStatus = AUDIO_JAVA_SUCCESS;
std::vector<media::MicrophoneInfo> activeMicrophones;
std::vector<media::MicrophoneInfoFw> activeMicrophones;
status_t status = mr->getActiveMicrophones(&activeMicrophones);
if (status != NO_ERROR) {
ALOGE_IF(status != NO_ERROR, "MediaRecorder::getActiveMicrophones error %d", status);