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

Commit d04aeea0 authored by Phil Burk's avatar Phil Burk Committed by Eric Laurent
Browse files

aaudio: AAudio_setMMapPolicy()



Controls whether MMAP data path is used NEVER, AUTO or ALWAYS
For testing only. To improve code coverage in CTS.

Bug: 38268547
Test: write_sine.cpp
Change-Id: Iad97876fb6f1d0c653262161b7b5519d83d16ac5
Signed-off-by: default avatarPhil Burk <philburk@google.com>
parent 629afae6
Loading
Loading
Loading
Loading
+18 −7
Original line number Diff line number Diff line
@@ -20,13 +20,20 @@
#include <stdlib.h>
#include <math.h>
#include <aaudio/AAudio.h>
#include <aaudio/AAudioTesting.h>
#include "AAudioExampleUtils.h"
#include "AAudioSimplePlayer.h"

#define SAMPLE_RATE           48000
#define NUM_SECONDS           20

#define MMAP_POLICY              AAUDIO_UNSPECIFIED
//#define MMAP_POLICY              AAUDIO_POLICY_NEVER
//#define MMAP_POLICY              AAUDIO_POLICY_AUTO
//#define MMAP_POLICY              AAUDIO_POLICY_ALWAYS

#define REQUESTED_FORMAT         AAUDIO_FORMAT_PCM_I16

#define REQUESTED_SHARING_MODE   AAUDIO_SHARING_MODE_SHARED
//#define REQUESTED_SHARING_MODE   AAUDIO_SHARING_MODE_EXCLUSIVE

@@ -64,6 +71,9 @@ int main(int argc, char **argv)

    printf("%s - Play a sine wave using AAudio\n", argv[0]);

    AAudio_setMMapPolicy(MMAP_POLICY);
    printf("requested MMapPolicy = %d\n", AAudio_getMMapPolicy());

    player.setSharingMode(REQUESTED_SHARING_MODE);

    result = player.open(requestedChannelCount, requestedSampleRate, requestedDataFormat,
@@ -112,12 +122,13 @@ int main(int argc, char **argv)
    printf("Buffer: framesPerBurst = %d\n",framesPerBurst);
    printf("Buffer: framesPerWrite = %d\n",framesPerWrite);

    printf("PerformanceMode        = %d\n", AAudioStream_getPerformanceMode(aaudioStream));
    printf("is MMAP used?          = %s\n", AAudioStream_isMMapUsed(aaudioStream) ? "yes" : "no");

    actualDataFormat = AAudioStream_getFormat(aaudioStream);
    printf("DataFormat: requested  = %d, actual = %d\n", REQUESTED_FORMAT, actualDataFormat);
    // TODO handle other data formats

    printf("PerformanceMode: %d\n", AAudioStream_getPerformanceMode(aaudioStream));

    // Allocate a buffer for the audio data.
    if (actualDataFormat == AAUDIO_FORMAT_PCM_FLOAT) {
        floatData = new float[framesPerWrite * actualChannelCount];
+94 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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 test support for AAudio.
 */
#ifndef AAUDIO_AAUDIO_TESTING_H
#define AAUDIO_AAUDIO_TESTING_H

#include <aaudio/AAudio.h>

#ifdef __cplusplus
extern "C" {
#endif

/************************************************************************************
 * The definitions below are only for testing. Do not use them in an application.
 * They may change or be removed at any time.
 ************************************************************************************/

enum {
    /**
     * Related feature is disabled and never used.
     */
    AAUDIO_POLICY_NEVER = 1,

    /**
     * If related feature works then use it. Otherwise fall back to something else.
     */
     AAUDIO_POLICY_AUTO,

    /**
     * Related feature must be used. If not available then fail.
     */
    AAUDIO_POLICY_ALWAYS
};
typedef int32_t aaudio_policy_t;

/**
 * Control whether AAudioStreamBuilder_openStream() will use the new MMAP data path
 * or the older "Legacy" data path.
 *
 * This will only affect the current process.
 *
 * If unspecified then the policy will be based on system properties or configuration.
 *
 * @note This is only for testing. Do not use this in an application.
 * It may change or be removed at any time.
 *
 * @param policy AAUDIO_UNSPECIFIED, AAUDIO_POLICY_NEVER, AAUDIO_POLICY_AUTO, or AAUDIO_POLICY_ALWAYS
 * @return AAUDIO_OK or a negative error
 */
AAUDIO_API aaudio_result_t AAudio_setMMapPolicy(aaudio_policy_t policy);

/**
 * Get the current MMAP policy set by AAudio_setMMapPolicy().
 *
 * @note This is only for testing. Do not use this in an application.
 * It may change or be removed at any time.
 *
 * @return current policy
 */
AAUDIO_API aaudio_policy_t AAudio_getMMapPolicy();

/**
 * Return true if the stream uses the MMAP data path versus the legacy path.
 *
 * @note This is only for testing. Do not use this in an application.
 * It may change or be removed at any time.
 *
 * @return true if the stream uses ther MMAP data path
 */
AAUDIO_API bool AAudioStream_isMMapUsed(AAudioStream* stream);

#ifdef __cplusplus
}
#endif

#endif //AAUDIO_AAUDIO_TESTING_H

/** @} */
+3 −0
Original line number Diff line number Diff line
@@ -3,6 +3,8 @@ LIBAAUDIO {
    AAudio_convertResultToText;
    AAudio_convertStreamStateToText;
    AAudio_createStreamBuilder;
    AAudio_getMMapPolicy;
    AAudio_setMMapPolicy;
    AAudioStreamBuilder_setPerformanceMode;
    AAudioStreamBuilder_setDeviceId;
    AAudioStreamBuilder_setDataCallback;
@@ -43,6 +45,7 @@ LIBAAUDIO {
    AAudioStream_getFramesWritten;
    AAudioStream_getFramesRead;
    AAudioStream_getTimestamp;
    AAudioStream_isMMapUsed;
  local:
    *;
};
+34 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@
#include <pthread.h>

#include <aaudio/AAudio.h>
#include <aaudio/AAudioTesting.h>

#include "AudioStreamBuilder.h"
#include "AudioStream.h"
@@ -94,6 +95,13 @@ AAUDIO_API const char * AAudio_convertStreamStateToText(aaudio_stream_state_t st

#undef AAUDIO_CASE_ENUM


/******************************************
 * Static globals.
 */
static aaudio_policy_t s_MMapPolicy = AAUDIO_UNSPECIFIED;


static AudioStream *convertAAudioStreamToAudioStream(AAudioStream* stream)
{
    return (AudioStream*) stream;
@@ -454,3 +462,29 @@ AAUDIO_API aaudio_result_t AAudioStream_getTimestamp(AAudioStream* stream,

    return audioStream->getTimestamp(clockid, framePosition, timeNanoseconds);
}

AAUDIO_API aaudio_policy_t AAudio_getMMapPolicy() {
    return s_MMapPolicy;
}

AAUDIO_API aaudio_result_t AAudio_setMMapPolicy(aaudio_policy_t policy) {
    aaudio_result_t result = AAUDIO_OK;
    switch(policy) {
        case AAUDIO_UNSPECIFIED:
        case AAUDIO_POLICY_NEVER:
        case AAUDIO_POLICY_AUTO:
        case AAUDIO_POLICY_ALWAYS:
            s_MMapPolicy = policy;
            break;
        default:
            result = AAUDIO_ERROR_ILLEGAL_ARGUMENT;
            break;
    }
    return result;
}

AAUDIO_API bool AAudioStream_isMMapUsed(AAudioStream* stream)
{
    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
    return audioStream->isMMap();
}
+27 −7
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@
#include <stdint.h>

#include <aaudio/AAudio.h>
#include <aaudio/AAudioTesting.h>

#include "binding/AAudioBinderClient.h"
#include "client/AudioStreamInternalCapture.h"
@@ -33,6 +34,9 @@

using namespace aaudio;

#define AAUDIO_MMAP_POLICY_DEFAULT             AAUDIO_POLICY_NEVER
#define AAUDIO_MMAP_EXCLUSIVE_POLICY_DEFAULT   AAUDIO_POLICY_NEVER

/*
 * AudioStreamBuilder
 */
@@ -76,25 +80,41 @@ static aaudio_result_t builder_createStream(aaudio_direction_t direction,
    return result;
}

// Try to open using MMAP path if that is enabled.
// Fall back to Legacy path is MMAP not available.
// Try to open using MMAP path if that is allowed.
// Fall back to Legacy path if MMAP not available.
// Exact behavior is controlled by MMapPolicy.
aaudio_result_t AudioStreamBuilder::build(AudioStream** streamPtr) {
    AudioStream *audioStream = nullptr;
    *streamPtr = nullptr;

    int32_t mmapEnabled = AAudioProperty_getMMapEnabled();
    int32_t mmapExclusiveEnabled = AAudioProperty_getMMapExclusiveEnabled();
    // The API setting is the highest priority.
    aaudio_policy_t mmapPolicy = AAudio_getMMapPolicy();
    // If not specified then get from a system property.
    if (mmapPolicy == AAUDIO_UNSPECIFIED) {
        mmapPolicy = AAudioProperty_getMMapPolicy();
    }
    // If still not specified then use the default.
    if (mmapPolicy == AAUDIO_UNSPECIFIED) {
        mmapPolicy = AAUDIO_MMAP_POLICY_DEFAULT;
    }

    int32_t mapExclusivePolicy = AAudioProperty_getMMapExclusivePolicy();
    if (mapExclusivePolicy == AAUDIO_UNSPECIFIED) {
        mapExclusivePolicy = AAUDIO_MMAP_EXCLUSIVE_POLICY_DEFAULT;
    }
    ALOGD("AudioStreamBuilder(): mmapPolicy = %d, mapExclusivePolicy = %d",
          mmapPolicy, mapExclusivePolicy);

    aaudio_sharing_mode_t sharingMode = getSharingMode();
    if ((sharingMode == AAUDIO_SHARING_MODE_EXCLUSIVE)
        && (mmapExclusiveEnabled == AAUDIO_USE_NEVER)) {
        && (mapExclusivePolicy == AAUDIO_POLICY_NEVER)) {
        ALOGW("AudioStreamBuilder(): EXCLUSIVE sharing mode not supported. Use SHARED.");
        sharingMode = AAUDIO_SHARING_MODE_SHARED;
        setSharingMode(sharingMode);
    }

    bool allowMMap = mmapEnabled != AAUDIO_USE_NEVER;
    bool allowLegacy = mmapEnabled != AAUDIO_USE_ALWAYS;
    bool allowMMap = mmapPolicy != AAUDIO_POLICY_NEVER;
    bool allowLegacy = mmapPolicy != AAUDIO_POLICY_ALWAYS;

    aaudio_result_t result = builder_createStream(getDirection(), sharingMode,
                                                  allowMMap, &audioStream);
Loading