AAudioServiceEndpointCapture.cpp revision 87c9f646a94259d7c321c3b3d5947fa1778f5ac2
1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "AAudioService"
18//#define LOG_NDEBUG 0
19#include <utils/Log.h>
20
21#include <assert.h>
22#include <map>
23#include <mutex>
24#include <utils/Singleton.h>
25
26#include "AAudioEndpointManager.h"
27#include "AAudioServiceEndpoint.h"
28
29#include "core/AudioStreamBuilder.h"
30#include "AAudioServiceEndpoint.h"
31#include "AAudioServiceStreamShared.h"
32#include "AAudioServiceEndpointCapture.h"
33
34using namespace android;  // TODO just import names needed
35using namespace aaudio;   // TODO just import names needed
36
37AAudioServiceEndpointCapture::AAudioServiceEndpointCapture(AAudioService &audioService)
38        : mStreamInternalCapture(audioService, true) {
39}
40
41AAudioServiceEndpointCapture::~AAudioServiceEndpointCapture() {
42    delete mDistributionBuffer;
43}
44
45aaudio_result_t AAudioServiceEndpointCapture::open(int32_t deviceId) {
46    aaudio_result_t result = AAudioServiceEndpoint::open(deviceId);
47    if (result == AAUDIO_OK) {
48        delete mDistributionBuffer;
49        int distributionBufferSizeBytes = getStreamInternal()->getFramesPerBurst()
50                                          * getStreamInternal()->getBytesPerFrame();
51        mDistributionBuffer = new uint8_t[distributionBufferSizeBytes];
52    }
53    return result;
54}
55
56// Read data from the shared MMAP stream and then distribute it to the client streams.
57void *AAudioServiceEndpointCapture::callbackLoop() {
58    ALOGD("AAudioServiceEndpointCapture(): callbackLoop() entering");
59    int32_t underflowCount = 0;
60
61    aaudio_result_t result = getStreamInternal()->requestStart();
62
63    int64_t timeoutNanos = getStreamInternal()->calculateReasonableTimeout();
64
65    // result might be a frame count
66    while (mCallbackEnabled.load() && getStreamInternal()->isActive() && (result >= 0)) {
67        // Read audio data from stream using a blocking read.
68        result = getStreamInternal()->read(mDistributionBuffer, getFramesPerBurst(), timeoutNanos);
69        if (result == AAUDIO_ERROR_DISCONNECTED) {
70            disconnectRegisteredStreams();
71            break;
72        } else if (result != getFramesPerBurst()) {
73            ALOGW("AAudioServiceEndpointCapture(): callbackLoop() read %d / %d",
74                  result, getFramesPerBurst());
75            break;
76        }
77
78        // Distribute data to each active stream.
79        { // use lock guard
80            //ALOGD("AAudioServiceEndpointCapture(): try to lock()");
81            std::lock_guard <std::mutex> lock(mLockStreams);
82            //ALOGD("AAudioServiceEndpointCapture(): got lock()");
83            for (AAudioServiceStreamShared *sharedStream : mRunningStreams) {
84                FifoBuffer *fifo = sharedStream->getDataFifoBuffer();
85                if (fifo->getFifoControllerBase()->getEmptyFramesAvailable() <
86                    getFramesPerBurst()) {
87                    underflowCount++;
88                } else {
89                    fifo->write(mDistributionBuffer, getFramesPerBurst());
90                }
91                sharedStream->markTransferTime(AudioClock::getNanoseconds());
92            }
93        }
94    }
95
96    result = getStreamInternal()->requestStop();
97
98    ALOGD("AAudioServiceEndpointCapture(): callbackLoop() exiting, %d underflows", underflowCount);
99    return NULL; // TODO review
100}
101