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

Commit b7f8edcf authored by Robert Wu's avatar Robert Wu
Browse files

Use routed devices throughout the audio framework

After you open an audio stream, you can call getRoutedDevice() to
get which output device is actually used.

However, if you play an ALARM with a headset attached, audio comes
out of both the speaker and the headset. This is not properly reflected
with our current APIs.

Throughout the audio framework, we should use DeviceIdVector instead
of a single device when getRoutedDevice(s) are called.

This change touches many files because onAudioDeviceUpdate() has many
callers.

BYPASS_LARGE_CHANGE_WARNING

Bug: 367816690
Test: atest AAudioTests
Test: atest audiorecord_tests
Test: atest audiosystem_tests
Test: atest audiotrack_tests
Test: adb shell /data/fuzz/arm64/libaaudio_fuzzer/libaaudio_fuzzer
Test: adb shell /data/fuzz/arm64/mediaplayer_fuzzer/mediaplayer_fuzzer
Test: adb shell /data/fuzz/arm64/mediarecorder_fuzzer/mediarecorder_fuzzer
Test: OboeTester Test Output with Alarm and USB
Flag: EXEMPT refactor
Change-Id: I5e32ac5d1c24229e60112ef00d82af41d8eff6e7
parent 55a32238
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -60,6 +60,7 @@ cc_fuzz {
        "libaaudio",
        "libaaudio_internal",
        "libaudioclient",
        "libaudiofoundation",
        "libaudioutils",
        "libbase_ndk",
        "libcutils",
+23 −1
Original line number Diff line number Diff line
@@ -1909,10 +1909,32 @@ AAUDIO_API int32_t AAudioStream_getSamplesPerFrame(AAudioStream* _Nonnull stream
 * Available since API level 26.
 *
 * @param stream reference provided by AAudioStreamBuilder_openStream()
 * @return actual device ID
 * @return actual device id. If there are multiple device ids used, the first device picked by
 *         the audio policy engine will be returned.
 */
AAUDIO_API int32_t AAudioStream_getDeviceId(AAudioStream* _Nonnull stream) __INTRODUCED_IN(26);

/**
 * Available since API level 36.
 *
 * Call this function after AAudioStreamBuilder_openStream().
 * This function will crash if stream is null.
 * An array of size 16 should generally be large enough to fit all device identifiers.
 *
 * @param stream reference provided by AAudioStreamBuilder_openStream().
 * @param ids reference to an array of ids.
 * @params numIds size allocated to the array of ids.
 *         The input should be the size of the ids array.
 *         The output will be the actual number of device ids.
 * @return {@link #AAUDIO_OK} or an error code.
 *         If numIds is null, return {@link #AAUDIO_ERROR_ILLEGAL_ARGUMENT}.
 *         If numIds is smaller than the number of device ids, return
 *         {@link #AAUDIO_ERROR_OUT_OF_RANGE}. The value of numIds will still be updated.
 *         Otherwise, if ids is null, return {@link #AAUDIO_ERROR_ILLEGAL_ARGUMENT}.
 */
AAUDIO_API aaudio_result_t AAudioStream_getDeviceIds(AAudioStream* _Nonnull stream,
        int32_t* _Nonnull ids, int32_t* _Nonnull numIds) __INTRODUCED_IN(36);

/**
 * Available since API level 26.
 *
+2 −0
Original line number Diff line number Diff line
@@ -103,6 +103,7 @@ cc_library {
        "framework-permission-aidl-cpp",
        "libaaudio_internal",
        "libaudioclient",
        "libaudiofoundation",
        "libaudioutils",
        "libbinder",
        "libcutils",
@@ -166,6 +167,7 @@ cc_library {
        "framework-permission-aidl-cpp",
        "libaudioclient",
        "libaudioclient_aidl_conversion",
        "libaudiofoundation",
        "libaudioutils",
        "libbinder",
        "libcutils",
+19 −2
Original line number Diff line number Diff line
@@ -34,7 +34,16 @@ using android::media::audio::common::AudioFormatDescription;
AAudioStreamConfiguration::AAudioStreamConfiguration(const StreamParameters& parcelable) {
    setChannelMask(parcelable.channelMask);
    setSampleRate(parcelable.sampleRate);
    setDeviceId(parcelable.deviceId);
    auto deviceIds = android::convertContainer<android::DeviceIdVector>(
            parcelable.deviceIds, android::aidl2legacy_int32_t_audio_port_handle_t);
    if (deviceIds.ok()) {
        setDeviceIds(deviceIds.value());
    } else {
        ALOGE("deviceIds (%s) aidl2legacy conversion failed",
              android::toString(parcelable.deviceIds).c_str());
        android::DeviceIdVector emptyDeviceIds;
        setDeviceIds(emptyDeviceIds);
    }
    static_assert(sizeof(aaudio_sharing_mode_t) == sizeof(parcelable.sharingMode));
    setSharingMode(parcelable.sharingMode);
    auto convFormat = android::aidl2legacy_AudioFormatDescription_audio_format_t(
@@ -87,7 +96,15 @@ StreamParameters AAudioStreamConfiguration::parcelable() const {
    StreamParameters result;
    result.channelMask = getChannelMask();
    result.sampleRate = getSampleRate();
    result.deviceId = getDeviceId();
    auto deviceIds = android::convertContainer<std::vector<int32_t>>(
            getDeviceIds(), android::legacy2aidl_audio_port_handle_t_int32_t);
    if (deviceIds.ok()) {
        result.deviceIds = deviceIds.value();
    } else {
        ALOGE("deviceIds (%s) legacy2aidl conversion failed",
              android::toString(getDeviceIds()).c_str());
        result.deviceIds = {};
    }
    static_assert(sizeof(aaudio_sharing_mode_t) == sizeof(result.sharingMode));
    result.sharingMode = getSharingMode();
    auto convAudioFormat = android::legacy2aidl_audio_format_t_AudioFormatDescription(getFormat());
+1 −1
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ import android.media.audio.common.AudioFormatDescription;
parcelable StreamParameters {
    int                                       channelMask;  //          = AAUDIO_UNSPECIFIED;
    int                                       sampleRate;  //           = AAUDIO_UNSPECIFIED;
    int                                       deviceId;  //             = AAUDIO_UNSPECIFIED;
    int[]                                     deviceIds;  //            = null;
    int /* aaudio_sharing_mode_t */           sharingMode;  //          = AAUDIO_SHARING_MODE_SHARED;
    AudioFormatDescription                    audioFormat;  //          = AUDIO_FORMAT_DEFAULT;
    int /* aaudio_direction_t */              direction;  //            = AAUDIO_DIRECTION_OUTPUT;
Loading