187c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk/*
287c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk * Copyright (C) 2017 The Android Open Source Project
387c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk *
487c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk * Licensed under the Apache License, Version 2.0 (the "License");
587c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk * you may not use this file except in compliance with the License.
687c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk * You may obtain a copy of the License at
787c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk *
887c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk *      http://www.apache.org/licenses/LICENSE-2.0
987c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk *
1087c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk * Unless required by applicable law or agreed to in writing, software
1187c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk * distributed under the License is distributed on an "AS IS" BASIS,
1287c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1387c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk * See the License for the specific language governing permissions and
1487c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk * limitations under the License.
1587c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk */
1687c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk
1787c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk#define LOG_TAG "AAudioService"
1887c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk//#define LOG_NDEBUG 0
1987c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk#include <utils/Log.h>
2087c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk
2187c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk#include <assert.h>
2287c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk#include <map>
2387c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk#include <mutex>
2487c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk#include <utils/Singleton.h>
2587c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk
2687c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk#include "AAudioEndpointManager.h"
2787c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk#include "AAudioServiceEndpoint.h"
2887c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk#include <algorithm>
2987c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk#include <mutex>
3087c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk#include <vector>
3187c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk
3287c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk#include "core/AudioStreamBuilder.h"
3387c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk#include "AAudioServiceEndpoint.h"
3487c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk#include "AAudioServiceStreamShared.h"
3587c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk#include "AAudioServiceEndpointPlay.h"
3687c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk
3787c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burkusing namespace android;  // TODO just import names needed
3887c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burkusing namespace aaudio;   // TODO just import names needed
3987c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk
4087c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk#define BURSTS_PER_BUFFER_DEFAULT   2
4187c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk
4287c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil BurkAAudioServiceEndpointPlay::AAudioServiceEndpointPlay(AAudioService &audioService)
4387c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk        : mStreamInternalPlay(audioService, true) {
4487c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk}
4587c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk
4687c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil BurkAAudioServiceEndpointPlay::~AAudioServiceEndpointPlay() {
4787c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk}
4887c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk
4987c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burkaaudio_result_t AAudioServiceEndpointPlay::open(int32_t deviceId) {
5087c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    aaudio_result_t result = AAudioServiceEndpoint::open(deviceId);
5187c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    if (result == AAUDIO_OK) {
5287c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk        mMixer.allocate(getStreamInternal()->getSamplesPerFrame(),
5387c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk                        getStreamInternal()->getFramesPerBurst());
5487c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk
5587c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk        int32_t burstsPerBuffer = AAudioProperty_getMixerBursts();
5687c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk        if (burstsPerBuffer == 0) {
5787c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk            mLatencyTuningEnabled = true;
5887c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk            burstsPerBuffer = BURSTS_PER_BUFFER_DEFAULT;
5987c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk        }
6087c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk        ALOGD("AAudioServiceEndpoint(): burstsPerBuffer = %d", burstsPerBuffer);
6187c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk        int32_t desiredBufferSize = burstsPerBuffer * getStreamInternal()->getFramesPerBurst();
6287c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk        getStreamInternal()->setBufferSize(desiredBufferSize);
6387c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    }
6487c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    return result;
6587c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk}
6687c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk
6787c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk// Mix data from each application stream and write result to the shared MMAP stream.
6887c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burkvoid *AAudioServiceEndpointPlay::callbackLoop() {
6987c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    ALOGD("AAudioServiceEndpointPlay(): callbackLoop() entering");
7087c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    int32_t underflowCount = 0;
7187c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk
7287c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    aaudio_result_t result = getStreamInternal()->requestStart();
7387c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk
7487c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    int64_t timeoutNanos = getStreamInternal()->calculateReasonableTimeout();
7587c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk
7687c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    // result might be a frame count
7787c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    while (mCallbackEnabled.load() && getStreamInternal()->isActive() && (result >= 0)) {
7887c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk        // Mix data from each active stream.
7987c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk        mMixer.clear();
8087c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk        { // use lock guard
8187c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk            std::lock_guard <std::mutex> lock(mLockStreams);
8287c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk            for (AAudioServiceStreamShared *sharedStream : mRunningStreams) {
8387c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk                FifoBuffer *fifo = sharedStream->getDataFifoBuffer();
8487c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk                float volume = 0.5; // TODO get from system
8587c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk                bool underflowed = mMixer.mix(fifo, volume);
8687c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk                underflowCount += underflowed ? 1 : 0;
8787c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk                // TODO log underflows in each stream
8887c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk                sharedStream->markTransferTime(AudioClock::getNanoseconds());
8987c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk            }
9087c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk        }
9187c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk
9287c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk        // Write mixer output to stream using a blocking write.
9387c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk        result = getStreamInternal()->write(mMixer.getOutputBuffer(),
9487c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk                                            getFramesPerBurst(), timeoutNanos);
9587c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk        if (result == AAUDIO_ERROR_DISCONNECTED) {
9687c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk            disconnectRegisteredStreams();
9787c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk            break;
9887c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk        } else if (result != getFramesPerBurst()) {
9987c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk            ALOGW("AAudioServiceEndpoint(): callbackLoop() wrote %d / %d",
10087c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk                  result, getFramesPerBurst());
10187c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk            break;
10287c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk        }
10387c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    }
10487c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk
10587c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    result = getStreamInternal()->requestStop();
10687c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk
10787c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    ALOGD("AAudioServiceEndpointPlay(): callbackLoop() exiting, %d underflows", underflowCount);
10887c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    return NULL; // TODO review
10987c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk}
110