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

Commit e42ff24d authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Revert^2 "AAudio: add support of Audio Attributes tags"" into main

parents fbbb0702 51a8a777
Loading
Loading
Loading
Loading
+80 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * This is the system APIs for AAudio.
 */
#ifndef SYSTEM_AAUDIO_H
#define SYSTEM_AAUDIO_H

#include <aaudio/AAudio.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
 * The tags string attributes allows OEMs to extend the
 * <a href="/reference/android/media/AudioAttributes">AudioAttributes</a>.
 *
 * Note that the maximum length includes all tags combined with delimiters and null terminator.
 *
 * Note that it matches the equivalent value in
 * <a href="/reference/android/system/media/audio">AUDIO_ATTRIBUTES_TAGS_MAX_SIZE</a>
 * in the Android native API.
 */
#define AAUDIO_ATTRIBUTES_TAGS_MAX_SIZE 256

/**
 * Set one or more vendor extension tags that the output stream will carry.
 *
 * The tags can be used by the audio policy engine for routing purpose.
 * Routing is based on audio attributes, translated into legacy stream type.
 * The stream types cannot be extended, so the product strategies have been introduced to allow
 * vendor extension of routing capabilities.
 * This could, for example, affect how volume and routing is handled for the stream.
 *
 * The tags can also be used by a System App to pass vendor specific information through the
 * framework to the HAL. That info could affect routing, ducking or other audio behavior in the HAL.
 *
 * By default, audio attributes tags are empty if this method is not called.
 *
 * @param builder reference provided by AAudio_createStreamBuilder()
 * @param tags the desired tags to add, which must be UTF-8 format and null-terminated. The size
 *             of the tags must be at most {@link #AAUDIO_ATTRIBUTES_TAGS_MAX_SIZE}. Multiple tags
 *             must be separated by semicolons.
 * @return {@link #AAUDIO_OK} on success or {@link #AAUDIO_ERROR_ILLEGAL_ARGUMENT} if the given
 *         tags is null or its length is greater than {@link #AAUDIO_ATTRIBUTES_TAGS_MAX_SIZE}.
 */
aaudio_result_t AAudioStreamBuilder_setTags(AAudioStreamBuilder* _Nonnull builder,
                                            const char* _Nonnull tags);

/**
 * Read the audio attributes' tags for the stream into a buffer.
 * The caller is responsible for allocating and freeing the returned data.
 *
 * @param stream reference provided by AAudioStreamBuilder_openStream()
 * @param tags pointer to write the value to in UTF-8 that containing OEM extension tags. It must
 *             be sized with {@link #AAUDIO_ATTRIBUTES_TAGS_MAX_SIZE}.
 * @return {@link #AAUDIO_OK} or {@link #AAUDIO_ERROR_ILLEGAL_ARGUMENT} if the given tags is null.
 */
aaudio_result_t AAudioStream_getTags(AAudioStream* _Nonnull stream, char* _Nonnull tags);

#ifdef __cplusplus
}
#endif

#endif //SYSTEM_AAUDIO_H
+3 −1
Original line number Diff line number Diff line
@@ -50,7 +50,7 @@ AAudioStreamConfiguration::AAudioStreamConfiguration(const StreamParameters& par
    setUsage(parcelable.usage);
    static_assert(sizeof(aaudio_content_type_t) == sizeof(parcelable.contentType));
    setContentType(parcelable.contentType);

    setTags(parcelable.tags);
    static_assert(sizeof(aaudio_spatialization_behavior_t) ==
            sizeof(parcelable.spatializationBehavior));
    setSpatializationBehavior(parcelable.spatializationBehavior);
@@ -106,6 +106,8 @@ StreamParameters AAudioStreamConfiguration::parcelable() const {
    result.usage = getUsage();
    static_assert(sizeof(aaudio_content_type_t) == sizeof(result.contentType));
    result.contentType = getContentType();
    std::optional<std::string> tags = getTags();
    result.tags = tags.has_value() ? tags.value() : "";
    static_assert(
            sizeof(aaudio_spatialization_behavior_t) == sizeof(result.spatializationBehavior));
    result.spatializationBehavior = getSpatializationBehavior();
+1 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ parcelable StreamParameters {
    int /* aaudio_direction_t */              direction;  //            = AAUDIO_DIRECTION_OUTPUT;
    int /* aaudio_usage_t */                  usage;  //                = AAUDIO_UNSPECIFIED;
    int /* aaudio_content_type_t */           contentType;  //          = AAUDIO_UNSPECIFIED;
    @utf8InCpp String                         tags;                     /* UTF8 */
    int /* aaudio_spatialization_behavior_t */spatializationBehavior; //= AAUDIO_UNSPECIFIED;
    boolean                                   isContentSpatialized;  // = false;
    int /* aaudio_input_preset_t */           inputPreset;  //          = AAUDIO_UNSPECIFIED;
+2 −0
Original line number Diff line number Diff line
@@ -129,6 +129,7 @@ aaudio_result_t AudioStreamInternal::open(const AudioStreamBuilder &builder) {

    request.getConfiguration().setUsage(getUsage());
    request.getConfiguration().setContentType(getContentType());
    request.getConfiguration().setTags(getTags());
    request.getConfiguration().setSpatializationBehavior(getSpatializationBehavior());
    request.getConfiguration().setIsContentSpatialized(isContentSpatialized());
    request.getConfiguration().setInputPreset(getInputPreset());
@@ -185,6 +186,7 @@ aaudio_result_t AudioStreamInternal::open(const AudioStreamBuilder &builder) {

    setUsage(configurationOutput.getUsage());
    setContentType(configurationOutput.getContentType());
    setTags(configurationOutput.getTags());
    setSpatializationBehavior(configurationOutput.getSpatializationBehavior());
    setIsContentSpatialized(configurationOutput.isContentSpatialized());
    setInputPreset(configurationOutput.getInputPreset());
+28 −0
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@

#include <aaudio/AAudio.h>
#include <aaudio/AAudioTesting.h>
#include <system/aaudio/AAudio.h>
#include "AudioClock.h"
#include "AudioGlobal.h"
#include "AudioStreamBuilder.h"
@@ -177,6 +178,17 @@ AAUDIO_API void AAudioStreamBuilder_setContentType(AAudioStreamBuilder* builder,
    streamBuilder->setContentType(contentType);
}

AAUDIO_API aaudio_result_t AAudioStreamBuilder_setTags(AAudioStreamBuilder* builder,
                                                       const char* tags) {
    if (tags == nullptr || strlen(tags) >= AAUDIO_ATTRIBUTES_TAGS_MAX_SIZE) {
        return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
    }
    AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
    std::optional<std::string> optionalTags = std::string(tags);
    streamBuilder->setTags(optionalTags);
    return AAUDIO_OK;
}

AAUDIO_API void AAudioStreamBuilder_setSpatializationBehavior(AAudioStreamBuilder* builder,
        aaudio_spatialization_behavior_t spatializationBehavior) {
    AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
@@ -546,6 +558,22 @@ AAUDIO_API aaudio_content_type_t AAudioStream_getContentType(AAudioStream* strea
    return audioStream->getContentType();
}

AAUDIO_API aaudio_result_t AAudioStream_getTags(AAudioStream* stream, char* tags)
{
    if (tags == nullptr) {
        return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
    }
    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
    std::optional<std::string> optTags = audioStream->getTags();
    if (optTags.has_value() && !optTags->empty()) {
        strncpy(tags, optTags.value().c_str(), AAUDIO_ATTRIBUTES_TAGS_MAX_SIZE);
        tags[AAUDIO_ATTRIBUTES_TAGS_MAX_SIZE-1] = '\0';
    } else {
        tags[0] = '\0';
    }
    return AAUDIO_OK;
}

AAUDIO_API aaudio_spatialization_behavior_t AAudioStream_getSpatializationBehavior(
        AAudioStream* stream)
{
Loading