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

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

Merge changes from topic "APC-app-policy"

* changes:
  Add application wide capture policy
  Add aaudio APC opt-out
parents a95ef984 b99cc75f
Loading
Loading
Loading
Loading
+58 −0
Original line number Diff line number Diff line
@@ -36,11 +36,14 @@ static void (*s_setContentType)(AAudioStreamBuilder* builder,
                                aaudio_content_type_t contentType) = nullptr;
static void (*s_setInputPreset)(AAudioStreamBuilder* builder,
                                aaudio_input_preset_t inputPreset) = nullptr;
static void (*s_setAllowedCapturePolicy)(AAudioStreamBuilder* builder,
                                          aaudio_allowed_capture_policy_t usage) = nullptr;

static bool s_loadAttempted = false;
static aaudio_usage_t (*s_getUsage)(AAudioStream *stream) = nullptr;
static aaudio_content_type_t (*s_getContentType)(AAudioStream *stream) = nullptr;
static aaudio_input_preset_t (*s_getInputPreset)(AAudioStream *stream) = nullptr;
static aaudio_allowed_capture_policy_t (*s_getAllowedCapturePolicy)(AAudioStream *stream) = nullptr;

// Link to test functions in shared library.
static void loadFutureFunctions() {
@@ -61,6 +64,10 @@ static void loadFutureFunctions() {
                dlsym(handle, "AAudioStreamBuilder_setInputPreset");
        if (s_setInputPreset == nullptr) goto error;

        s_setAllowedCapturePolicy = (void (*)(AAudioStreamBuilder *, aaudio_input_preset_t))
                dlsym(handle, "AAudioStreamBuilder_setAllowedCapturePolicy");
        if (s_setAllowedCapturePolicy == nullptr) goto error;

        s_getUsage = (aaudio_usage_t (*)(AAudioStream *))
                dlsym(handle, "AAudioStream_getUsage");
        if (s_getUsage == nullptr) goto error;
@@ -72,6 +79,10 @@ static void loadFutureFunctions() {
        s_getInputPreset = (aaudio_input_preset_t (*)(AAudioStream *))
                dlsym(handle, "AAudioStream_getInputPreset");
        if (s_getInputPreset == nullptr) goto error;

        s_getAllowedCapturePolicy = (aaudio_input_preset_t (*)(AAudioStream *))
                dlsym(handle, "AAudioStream_getAllowedCapturePolicy");
        if (s_getAllowedCapturePolicy == nullptr) goto error;
    }
    return;

@@ -169,6 +180,14 @@ public:
        mInputPreset = inputPreset;
    }

    aaudio_allowed_capture_policy_t getAllowedCapturePolicy() const {
        return mAllowedCapturePolicy;
    }

    void setAllowedCapturePolicy(aaudio_allowed_capture_policy_t policy) {
        mAllowedCapturePolicy = policy;
    }

    int32_t getDeviceId() const {
        return mDeviceId;
    }
@@ -223,6 +242,13 @@ public:
        } else if (mUsage != AAUDIO_UNSPECIFIED){
            printf("WARNING: setInputPreset not supported");
        }

        // Call Q functions if supported.
        if (s_setAllowedCapturePolicy != nullptr) {
            s_setAllowedCapturePolicy(builder, mAllowedCapturePolicy);
        } else if (mAllowedCapturePolicy != AAUDIO_UNSPECIFIED){
            printf("WARNING: setAllowedCapturePolicy not supported");
        }
    }

private:
@@ -238,6 +264,7 @@ private:
    aaudio_usage_t             mUsage           = AAUDIO_UNSPECIFIED;
    aaudio_content_type_t      mContentType     = AAUDIO_UNSPECIFIED;
    aaudio_input_preset_t      mInputPreset     = AAUDIO_UNSPECIFIED;
    aaudio_allowed_capture_policy_t mAllowedCapturePolicy     = AAUDIO_UNSPECIFIED;

    int32_t                    mNumberOfBursts  = AAUDIO_UNSPECIFIED;
    int32_t                    mFramesPerCallback = AAUDIO_UNSPECIFIED;
@@ -267,6 +294,9 @@ public:
                case 'c':
                    setChannelCount(atoi(&arg[2]));
                    break;
                case 'C':
                    setAllowedCapturePolicy(parseAllowedCapturePolicy(arg[2]));
                    break;
                case 'd':
                    setDeviceId(atoi(&arg[2]));
                    break;
@@ -341,6 +371,10 @@ public:
        printf("      Default values are UNSPECIFIED unless otherwise stated.\n");
        printf("      -b{bufferCapacity} frames\n");
        printf("      -c{channels} for example 2 for stereo\n");
        printf("      -C{a|s|n} set playback capture policy\n");
        printf("          a = _ALL (default)\n");
        printf("          s = _SYSTEM\n");
        printf("          n = _NONE\n");
        printf("      -d{deviceId} default is %d\n", AAUDIO_UNSPECIFIED);
        printf("      -f{0|1|2} set format\n");
        printf("          0 = UNSPECIFIED\n");
@@ -365,6 +399,25 @@ public:
        printf("      -z{callbackSize} or block size, in frames, default = 0\n");
    }

    static aaudio_performance_mode_t parseAllowedCapturePolicy(char c) {
        aaudio_allowed_capture_policy_t policy = AAUDIO_ALLOW_CAPTURE_BY_ALL;
        switch (c) {
            case 'a':
                policy = AAUDIO_ALLOW_CAPTURE_BY_ALL;
                break;
            case 's':
                policy = AAUDIO_ALLOW_CAPTURE_BY_SYSTEM;
                break;
            case 'n':
                policy = AAUDIO_ALLOW_CAPTURE_BY_NONE;
                break;
            default:
                printf("ERROR: invalid playback capture policy %c\n", c);
                break;
        }
        return policy;
    }

    static aaudio_performance_mode_t parsePerformanceMode(char c) {
        aaudio_performance_mode_t mode = AAUDIO_PERFORMANCE_MODE_NONE;
        switch (c) {
@@ -449,6 +502,11 @@ public:
        printf("  Is MMAP used? %s\n", AAudioStream_isMMapUsed(stream)
               ? "yes" : "no");

        if (s_getAllowedCapturePolicy != nullptr) {
            printf("  ContentType:  requested = %d, actual = %d\n",
                   getAllowedCapturePolicy(), s_getAllowedCapturePolicy(stream));
        }

    }

    int32_t getDurationSeconds() const {
+74 −2
Original line number Diff line number Diff line
@@ -232,7 +232,8 @@ typedef int32_t aaudio_performance_mode_t;
 * This information is used by certain platforms or routing policies
 * to make more refined volume or routing decisions.
 *
 * Note that these match the equivalent values in AudioAttributes in the Android Java API.
 * Note that these match the equivalent values in {@link android.media.AudioAttributes}
 * in the Android Java API.
 *
 * Added in API level 28.
 */
@@ -308,7 +309,8 @@ typedef int32_t aaudio_usage_t;
 * an audio book application) this information might be used by the audio framework to
 * enforce audio focus.
 *
 * Note that these match the equivalent values in AudioAttributes in the Android Java API.
 * Note that these match the equivalent values in {@link android.media.AudioAttributes}
 * in the Android Java API.
 *
 * Added in API level 28.
 */
@@ -383,6 +385,48 @@ enum {
};
typedef int32_t aaudio_input_preset_t;

/**
 * Specifying if audio may or may not be captured by other apps or the system.
 *
 * Note that these match the equivalent values in {@link android.media.AudioAttributes}
 * in the Android Java API.
 *
 * Added in API level 29.
 */
enum {
    /**
     * Indicates that the audio may be captured by any app.
     *
     * For privacy, the following usages can not be recorded: AAUDIO_VOICE_COMMUNICATION*,
     * AAUDIO_USAGE_NOTIFICATION*, AAUDIO_USAGE_ASSISTANCE* and AAUDIO_USAGE_ASSISTANT.
     *
     * On {@link Android.os.Build.VERSION_CODES.Q}, this means only {@link AAUDIO_USAGE_MEDIA}
     * and {@link AAUDIO_USAGE_GAME} may be captured.
     *
     * See {@link android.media.AudioAttributes.ALLOW_CAPTURE_BY_ALL}.
     */
    AAUDIO_ALLOW_CAPTURE_BY_ALL = 1,
    /**
     * Indicates that the audio may only be captured by system apps.
     *
     * System apps can capture for many purposes like accessibility, user guidance...
     * but have strong restriction. See
     * {@link android.media.AudioAttributes.ALLOW_CAPTURE_BY_SYSTEM} for what the system apps
     * can do with the capture audio.
     */
    AAUDIO_ALLOW_CAPTURE_BY_SYSTEM = 2,
    /**
     * Indicates that the audio may not be recorded by any app, even if it is a system app.
     *
     * It is encouraged to use {@link ALLOW_CAPTURE_BY_SYSTEM} instead of this value as system apps
     * provide significant and useful features for the user (eg. accessibility).
     * See {@link android.media.AudioAttributes.ALLOW_CAPTURE_BY_NONE}.
     */
    AAUDIO_ALLOW_CAPTURE_BY_NONE = 3,
};

typedef int32_t aaudio_allowed_capture_policy_t;

/**
 * These may be used with AAudioStreamBuilder_setSessionId().
 *
@@ -643,6 +687,22 @@ AAUDIO_API void AAudioStreamBuilder_setContentType(AAudioStreamBuilder* builder,
AAUDIO_API void AAudioStreamBuilder_setInputPreset(AAudioStreamBuilder* builder,
        aaudio_input_preset_t inputPreset) __INTRODUCED_IN(28);

/**
 * Specify whether this stream audio may or may not be captured by other apps or the system.
 *
 * The default is AAUDIO_ALLOW_CAPTURE_BY_ALL.
 *
 * Note that an application can also set its global policy, in which case the most restrictive
 * policy is always applied. See {@link android.media.AudioAttributes.setAllowedCapturePolicy}
 *
 * Added in API level 29.
 *
 * @param builder reference provided by AAudio_createStreamBuilder()
 * @param inputPreset the desired level of opt-out from being captured.
 */
AAUDIO_API void AAudioStreamBuilder_setAllowedCapturePolicy(AAudioStreamBuilder* builder,
        aaudio_allowed_capture_policy_t capturePolicy) __INTRODUCED_IN(29);

/** Set the requested session ID.
 *
 * The session ID can be used to associate a stream with effects processors.
@@ -1277,6 +1337,18 @@ AAUDIO_API aaudio_content_type_t AAudioStream_getContentType(AAudioStream* strea
AAUDIO_API aaudio_input_preset_t AAudioStream_getInputPreset(AAudioStream* stream)
        __INTRODUCED_IN(28);

/**
 * Return the policy that determines whether the audio may or may not be captured
 * by other apps or the system.
 *
 * Added in API level 29.
 *
 * @param stream reference provided by AAudioStreamBuilder_openStream()
 * @return the allowed capture policy, for example AAUDIO_ALLOW_CAPTURE_BY_ALL
 */
AAUDIO_API aaudio_allowed_capture_policy_t AAudioStream_getAllowedCapturePolicy(
        AAudioStream* stream) __INTRODUCED_IN(29);

#ifdef __cplusplus
}
#endif
+5 −0
Original line number Diff line number Diff line
@@ -62,6 +62,8 @@ status_t AAudioStreamConfiguration::writeToParcel(Parcel* parcel) const {
    if (status != NO_ERROR) goto error;
    status = parcel->writeInt32((int32_t) getInputPreset());
    if (status != NO_ERROR) goto error;
    status = parcel->writeInt32((int32_t) getAllowedCapturePolicy());
    if (status != NO_ERROR) goto error;
    status = parcel->writeInt32(getSessionId());
    if (status != NO_ERROR) goto error;
    return NO_ERROR;
@@ -105,6 +107,9 @@ status_t AAudioStreamConfiguration::readFromParcel(const Parcel* parcel) {
    setInputPreset((aaudio_input_preset_t) value);
    status = parcel->readInt32(&value);
    if (status != NO_ERROR) goto error;
    setAllowedCapturePolicy((aaudio_allowed_capture_policy_t) value);
    status = parcel->readInt32(&value);
    if (status != NO_ERROR) goto error;
    setSessionId(value);

    return NO_ERROR;
+13 −0
Original line number Diff line number Diff line
@@ -204,6 +204,12 @@ AAUDIO_API void AAudioStreamBuilder_setBufferCapacityInFrames(AAudioStreamBuilde
    streamBuilder->setBufferCapacity(frames);
}

AAUDIO_API void AAudioStreamBuilder_setAllowedCapturePolicy(
        AAudioStreamBuilder* builder, aaudio_allowed_capture_policy_t policy) {
    AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
    streamBuilder->setAllowedCapturePolicy(policy);
}

AAUDIO_API void AAudioStreamBuilder_setSessionId(AAudioStreamBuilder* builder,
                                                 aaudio_session_id_t sessionId)
{
@@ -494,6 +500,13 @@ AAUDIO_API aaudio_input_preset_t AAudioStream_getInputPreset(AAudioStream* strea
    return audioStream->getInputPreset();
}

AAUDIO_API aaudio_allowed_capture_policy_t AAudioStream_getAllowedCapturePolicy(
        AAudioStream* stream)
{
    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
    return audioStream->getAllowedCapturePolicy();
}

AAUDIO_API int32_t AAudioStream_getSessionId(AAudioStream* stream)
{
    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
+36 −22
Original line number Diff line number Diff line
@@ -46,6 +46,7 @@ void AAudioStreamParameters::copyFrom(const AAudioStreamParameters &other) {
    mUsage                = other.mUsage;
    mContentType          = other.mContentType;
    mInputPreset          = other.mInputPreset;
    mAllowedCapturePolicy = other.mAllowedCapturePolicy;
}

static aaudio_result_t isFormatValid(audio_format_t format) {
@@ -166,6 +167,18 @@ aaudio_result_t AAudioStreamParameters::validate() const {
            // break;
    }

    switch (mAllowedCapturePolicy) {
        case AAUDIO_UNSPECIFIED:
        case AAUDIO_ALLOW_CAPTURE_BY_ALL:
        case AAUDIO_ALLOW_CAPTURE_BY_SYSTEM:
        case AAUDIO_ALLOW_CAPTURE_BY_NONE:
            break; // valid
        default:
            ALOGE("allowed capture policy not valid = %d", mAllowedCapturePolicy);
            return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
            // break;
    }

    return AAUDIO_OK;
}

@@ -181,4 +194,5 @@ void AAudioStreamParameters::dump() const {
    ALOGD("mUsage                = %6d", mUsage);
    ALOGD("mContentType          = %6d", mContentType);
    ALOGD("mInputPreset          = %6d", mInputPreset);
    ALOGD("mAllowedCapturePolicy = %6d", mAllowedCapturePolicy);
}
Loading