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

Commit 7b2ebfe7 authored by Eric Laurent's avatar Eric Laurent Committed by android-build-merger
Browse files

Merge "aaudio: modify endpoint sharing logic" into oc-dr1-dev am: 358ad425

am: cbaf5e98

Change-Id: I7334c473d2618e96a8441c6f5a59354c11c4d615
parents e049eb2d cbaf5e98
Loading
Loading
Loading
Loading
+28 −13
Original line number Original line Diff line number Diff line
@@ -51,13 +51,13 @@ std::string AAudioEndpointManager::dump() const {
    size_t inputs = mInputs.size();
    size_t inputs = mInputs.size();
    result << "Inputs: " << inputs << "\n";
    result << "Inputs: " << inputs << "\n";
    for (const auto &input : mInputs) {
    for (const auto &input : mInputs) {
        result << "  Input(" << input.first << ", " << input.second << ")\n";
        result << "  Input(" << input << ")\n";
    }
    }


    size_t outputs = mOutputs.size();
    size_t outputs = mOutputs.size();
    result << "Outputs: " << outputs << "\n";
    result << "Outputs: " << outputs << "\n";
    for (const auto &output : mOutputs) {
    for (const auto &output : mOutputs) {
        result << "  Output(" << output.first << ", " << output.second << ")\n";
        result << "  Output(" << output << ")\n";
    }
    }


    if (isLocked) {
    if (isLocked) {
@@ -66,27 +66,40 @@ std::string AAudioEndpointManager::dump() const {
    return result.str();
    return result.str();
}
}


AAudioServiceEndpoint *AAudioEndpointManager::openEndpoint(AAudioService &audioService, int32_t deviceId,
AAudioServiceEndpoint *AAudioEndpointManager::openEndpoint(AAudioService &audioService,
                                                           aaudio_direction_t direction) {
        const AAudioStreamConfiguration& configuration, aaudio_direction_t direction) {
    AAudioServiceEndpoint *endpoint = nullptr;
    AAudioServiceEndpoint *endpoint = nullptr;
    AAudioServiceEndpointCapture *capture = nullptr;
    AAudioServiceEndpointCapture *capture = nullptr;
    AAudioServiceEndpointPlay *player = nullptr;
    AAudioServiceEndpointPlay *player = nullptr;
    std::lock_guard<std::mutex> lock(mLock);
    std::lock_guard<std::mutex> lock(mLock);


    // Try to find an existing endpoint.
    // Try to find an existing endpoint.



    switch (direction) {
    switch (direction) {
        case AAUDIO_DIRECTION_INPUT:
        case AAUDIO_DIRECTION_INPUT:
            endpoint = mInputs[deviceId];
            for (AAudioServiceEndpoint *ep : mInputs) {
                if (ep->matches(configuration)) {
                    endpoint = ep;
                    break;
                }
            }
            break;
            break;
        case AAUDIO_DIRECTION_OUTPUT:
        case AAUDIO_DIRECTION_OUTPUT:
            endpoint = mOutputs[deviceId];
            for (AAudioServiceEndpoint *ep : mOutputs) {
                if (ep->matches(configuration)) {
                    endpoint = ep;
                    break;
                }
            }
            break;
            break;
        default:
        default:
            assert(false); // There are only two possible directions.
            assert(false); // There are only two possible directions.
            break;
            break;
    }
    }
    ALOGD("AAudioEndpointManager::openEndpoint(), found %p for device = %d, dir = %d",
    ALOGD("AAudioEndpointManager::openEndpoint(), found %p for device = %d, dir = %d",
          endpoint, deviceId, (int)direction);
          endpoint, configuration.getDeviceId(), (int)direction);


    // If we can't find an existing one then open a new one.
    // If we can't find an existing one then open a new one.
    if (endpoint == nullptr) {
    if (endpoint == nullptr) {
@@ -105,7 +118,7 @@ AAudioServiceEndpoint *AAudioEndpointManager::openEndpoint(AAudioService &audioS
    }
    }


    if (endpoint != nullptr) {
    if (endpoint != nullptr) {
        aaudio_result_t result = endpoint->open(deviceId);
        aaudio_result_t result = endpoint->open(configuration);
        if (result != AAUDIO_OK) {
        if (result != AAUDIO_OK) {
            ALOGE("AAudioEndpointManager::findEndpoint(), open failed");
            ALOGE("AAudioEndpointManager::findEndpoint(), open failed");
            delete endpoint;
            delete endpoint;
@@ -113,17 +126,17 @@ AAudioServiceEndpoint *AAudioEndpointManager::openEndpoint(AAudioService &audioS
        } else {
        } else {
            switch(direction) {
            switch(direction) {
                case AAUDIO_DIRECTION_INPUT:
                case AAUDIO_DIRECTION_INPUT:
                    mInputs[deviceId] = capture;
                    mInputs.push_back(capture);
                    break;
                    break;
                case AAUDIO_DIRECTION_OUTPUT:
                case AAUDIO_DIRECTION_OUTPUT:
                    mOutputs[deviceId] = player;
                    mOutputs.push_back(player);
                    break;
                    break;
                default:
                default:
                    break;
                    break;
            }
            }
        }
        }
        ALOGD("AAudioEndpointManager::openEndpoint(), created %p for device = %d, dir = %d",
        ALOGD("AAudioEndpointManager::openEndpoint(), created %p for device = %d, dir = %d",
              endpoint, deviceId, (int)direction);
              endpoint, configuration.getDeviceId(), (int)direction);
    }
    }


    if (endpoint != nullptr) {
    if (endpoint != nullptr) {
@@ -156,10 +169,12 @@ void AAudioEndpointManager::closeEndpoint(AAudioServiceEndpoint *serviceEndpoint


        switch (direction) {
        switch (direction) {
            case AAUDIO_DIRECTION_INPUT:
            case AAUDIO_DIRECTION_INPUT:
                mInputs.erase(deviceId);
                mInputs.erase(
                  std::remove(mInputs.begin(), mInputs.end(), serviceEndpoint), mInputs.end());
                break;
                break;
            case AAUDIO_DIRECTION_OUTPUT:
            case AAUDIO_DIRECTION_OUTPUT:
                mOutputs.erase(deviceId);
                mOutputs.erase(
                  std::remove(mOutputs.begin(), mOutputs.end(), serviceEndpoint), mOutputs.end());
                break;
                break;
            default:
            default:
                break;
                break;
+3 −3
Original line number Original line Diff line number Diff line
@@ -54,7 +54,7 @@ public:
     * @return endpoint or nullptr
     * @return endpoint or nullptr
     */
     */
    AAudioServiceEndpoint *openEndpoint(android::AAudioService &audioService,
    AAudioServiceEndpoint *openEndpoint(android::AAudioService &audioService,
                                        int32_t deviceId,
                                        const AAudioStreamConfiguration& configuration,
                                        aaudio_direction_t direction);
                                        aaudio_direction_t direction);


    void closeEndpoint(AAudioServiceEndpoint *serviceEndpoint);
    void closeEndpoint(AAudioServiceEndpoint *serviceEndpoint);
@@ -63,8 +63,8 @@ private:


    mutable std::mutex mLock;
    mutable std::mutex mLock;


    std::map<int32_t, AAudioServiceEndpointCapture *> mInputs;
    std::vector<AAudioServiceEndpointCapture *> mInputs;
    std::map<int32_t, AAudioServiceEndpointPlay *> mOutputs;
    std::vector<AAudioServiceEndpointPlay *> mOutputs;


};
};


+23 −3
Original line number Original line Diff line number Diff line
@@ -45,15 +45,18 @@ using namespace aaudio; // TODO just import names needed
#define DEFAULT_BUFFER_CAPACITY   (48 * 8)
#define DEFAULT_BUFFER_CAPACITY   (48 * 8)


// Set up an EXCLUSIVE MMAP stream that will be shared.
// Set up an EXCLUSIVE MMAP stream that will be shared.
aaudio_result_t AAudioServiceEndpoint::open(int32_t deviceId) {
aaudio_result_t AAudioServiceEndpoint::open(const AAudioStreamConfiguration& configuration) {
    mRequestedDeviceId = deviceId;
    mRequestedDeviceId = configuration.getDeviceId();
    mStreamInternal = getStreamInternal();
    mStreamInternal = getStreamInternal();


    AudioStreamBuilder builder;
    AudioStreamBuilder builder;
    builder.setSharingMode(AAUDIO_SHARING_MODE_EXCLUSIVE);
    builder.setSharingMode(AAUDIO_SHARING_MODE_EXCLUSIVE);
    // Don't fall back to SHARED because that would cause recursion.
    // Don't fall back to SHARED because that would cause recursion.
    builder.setSharingModeMatchRequired(true);
    builder.setSharingModeMatchRequired(true);
    builder.setDeviceId(deviceId);
    builder.setDeviceId(mRequestedDeviceId);
    builder.setFormat(configuration.getAudioFormat());
    builder.setSampleRate(configuration.getSampleRate());
    builder.setSamplesPerFrame(configuration.getSamplesPerFrame());
    builder.setDirection(getDirection());
    builder.setDirection(getDirection());
    builder.setBufferCapacity(DEFAULT_BUFFER_CAPACITY);
    builder.setBufferCapacity(DEFAULT_BUFFER_CAPACITY);


@@ -139,3 +142,20 @@ void AAudioServiceEndpoint::disconnectRegisteredStreams() {
    }
    }
    mRegisteredStreams.clear();
    mRegisteredStreams.clear();
}
}

bool AAudioServiceEndpoint::matches(const AAudioStreamConfiguration& configuration) {
    if (configuration.getDeviceId() != AAUDIO_UNSPECIFIED &&
            configuration.getDeviceId() != mStreamInternal->getDeviceId()) {
        return false;
    }
    if (configuration.getSampleRate() != AAUDIO_UNSPECIFIED &&
            configuration.getSampleRate() != mStreamInternal->getSampleRate()) {
        return false;
    }
    if (configuration.getSamplesPerFrame() != AAUDIO_UNSPECIFIED &&
            configuration.getSamplesPerFrame() != mStreamInternal->getSamplesPerFrame()) {
        return false;
    }

    return true;
}
+3 −1
Original line number Original line Diff line number Diff line
@@ -36,7 +36,7 @@ class AAudioServiceEndpoint {
public:
public:
    virtual ~AAudioServiceEndpoint() = default;
    virtual ~AAudioServiceEndpoint() = default;


    virtual aaudio_result_t open(int32_t deviceId);
    virtual aaudio_result_t open(const AAudioStreamConfiguration& configuration);


    int32_t getSampleRate() const { return mStreamInternal->getSampleRate(); }
    int32_t getSampleRate() const { return mStreamInternal->getSampleRate(); }
    int32_t getSamplesPerFrame() const { return mStreamInternal->getSamplesPerFrame();  }
    int32_t getSamplesPerFrame() const { return mStreamInternal->getSamplesPerFrame();  }
@@ -67,6 +67,8 @@ public:
        mReferenceCount = count;
        mReferenceCount = count;
    }
    }


    bool matches(const AAudioStreamConfiguration& configuration);

    virtual AudioStreamInternal *getStreamInternal() = 0;
    virtual AudioStreamInternal *getStreamInternal() = 0;


    std::atomic<bool>        mCallbackEnabled;
    std::atomic<bool>        mCallbackEnabled;
+2 −2
Original line number Original line Diff line number Diff line
@@ -42,8 +42,8 @@ AAudioServiceEndpointCapture::~AAudioServiceEndpointCapture() {
    delete mDistributionBuffer;
    delete mDistributionBuffer;
}
}


aaudio_result_t AAudioServiceEndpointCapture::open(int32_t deviceId) {
aaudio_result_t AAudioServiceEndpointCapture::open(const AAudioStreamConfiguration& configuration) {
    aaudio_result_t result = AAudioServiceEndpoint::open(deviceId);
    aaudio_result_t result = AAudioServiceEndpoint::open(configuration);
    if (result == AAUDIO_OK) {
    if (result == AAUDIO_OK) {
        delete mDistributionBuffer;
        delete mDistributionBuffer;
        int distributionBufferSizeBytes = getStreamInternal()->getFramesPerBurst()
        int distributionBufferSizeBytes = getStreamInternal()->getFramesPerBurst()
Loading