blob: 6a373308a498f7db898c9a6ecbd1a5c9cf154239 [file] [log] [blame]
Phil Burk87c9f642017-05-17 07:22:39 -07001/*
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
Eric Laurenta17ae742017-06-29 15:43:55 -070045aaudio_result_t AAudioServiceEndpointCapture::open(const AAudioStreamConfiguration& configuration) {
46 aaudio_result_t result = AAudioServiceEndpoint::open(configuration);
Phil Burk87c9f642017-05-17 07:22:39 -070047 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;
Eric Laurenta54f1282017-07-01 19:39:32 -070060 aaudio_result_t result = AAUDIO_OK;
Phil Burk87c9f642017-05-17 07:22:39 -070061 int64_t timeoutNanos = getStreamInternal()->calculateReasonableTimeout();
62
63 // result might be a frame count
64 while (mCallbackEnabled.load() && getStreamInternal()->isActive() && (result >= 0)) {
65 // Read audio data from stream using a blocking read.
66 result = getStreamInternal()->read(mDistributionBuffer, getFramesPerBurst(), timeoutNanos);
67 if (result == AAUDIO_ERROR_DISCONNECTED) {
68 disconnectRegisteredStreams();
69 break;
70 } else if (result != getFramesPerBurst()) {
71 ALOGW("AAudioServiceEndpointCapture(): callbackLoop() read %d / %d",
72 result, getFramesPerBurst());
73 break;
74 }
75
76 // Distribute data to each active stream.
77 { // use lock guard
Phil Burk87c9f642017-05-17 07:22:39 -070078 std::lock_guard <std::mutex> lock(mLockStreams);
Eric Laurenta54f1282017-07-01 19:39:32 -070079 for (sp<AAudioServiceStreamShared> sharedStream : mRegisteredStreams) {
80 if (sharedStream->isRunning()) {
81 FifoBuffer *fifo = sharedStream->getDataFifoBuffer();
82 if (fifo->getFifoControllerBase()->getEmptyFramesAvailable() <
83 getFramesPerBurst()) {
84 underflowCount++;
85 } else {
86 fifo->write(mDistributionBuffer, getFramesPerBurst());
87 }
88 sharedStream->markTransferTime(AudioClock::getNanoseconds());
Phil Burk87c9f642017-05-17 07:22:39 -070089 }
Phil Burk87c9f642017-05-17 07:22:39 -070090 }
91 }
92 }
93
Phil Burk87c9f642017-05-17 07:22:39 -070094 ALOGD("AAudioServiceEndpointCapture(): callbackLoop() exiting, %d underflows", underflowCount);
95 return NULL; // TODO review
96}