blob: 6504cc1838633f06f2c7a6df42efcc26439b8022 [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)) {
Phil Burk97350f92017-07-21 15:59:44 -070065
66 int64_t mmapFramesRead = getStreamInternal()->getFramesRead();
67
Phil Burk87c9f642017-05-17 07:22:39 -070068 // Read audio data from stream using a blocking read.
69 result = getStreamInternal()->read(mDistributionBuffer, getFramesPerBurst(), timeoutNanos);
70 if (result == AAUDIO_ERROR_DISCONNECTED) {
71 disconnectRegisteredStreams();
72 break;
73 } else if (result != getFramesPerBurst()) {
74 ALOGW("AAudioServiceEndpointCapture(): callbackLoop() read %d / %d",
75 result, getFramesPerBurst());
76 break;
77 }
78
79 // Distribute data to each active stream.
Phil Burk97350f92017-07-21 15:59:44 -070080 { // brackets are for lock_guard
81
Phil Burk87c9f642017-05-17 07:22:39 -070082 std::lock_guard <std::mutex> lock(mLockStreams);
Phil Burk97350f92017-07-21 15:59:44 -070083 for (sp<AAudioServiceStreamShared> clientStream : mRegisteredStreams) {
84 if (clientStream->isRunning()) {
85 FifoBuffer *fifo = clientStream->getDataFifoBuffer();
86
87 // Determine offset between framePosition in client's stream vs the underlying
88 // MMAP stream.
89 int64_t clientFramesWritten = fifo->getWriteCounter();
90 // There are two indices that refer to the same frame.
91 int64_t positionOffset = mmapFramesRead - clientFramesWritten;
92 clientStream->setTimestampPositionOffset(positionOffset);
93
Eric Laurenta54f1282017-07-01 19:39:32 -070094 if (fifo->getFifoControllerBase()->getEmptyFramesAvailable() <
95 getFramesPerBurst()) {
96 underflowCount++;
97 } else {
98 fifo->write(mDistributionBuffer, getFramesPerBurst());
99 }
Phil Burk97350f92017-07-21 15:59:44 -0700100
101 // This timestamp represents the completion of data being written into the
102 // client buffer. It is sent to the client and used in the timing model
103 // to decide when data will be available to read.
104 Timestamp timestamp(fifo->getWriteCounter(), AudioClock::getNanoseconds());
105 clientStream->markTransferTime(timestamp);
Phil Burk87c9f642017-05-17 07:22:39 -0700106 }
Phil Burk87c9f642017-05-17 07:22:39 -0700107 }
108 }
109 }
110
Phil Burk87c9f642017-05-17 07:22:39 -0700111 ALOGD("AAudioServiceEndpointCapture(): callbackLoop() exiting, %d underflows", underflowCount);
112 return NULL; // TODO review
113}