blob: c603e4e1db7896807307fff249e3081407a2c0a3 [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
Phil Burkfbf031e2017-10-12 15:58:31 -070017#define LOG_TAG "AAudioServiceEndpointCapture"
Phil Burk87c9f642017-05-17 07:22:39 -070018//#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"
Phil Burk39f02dd2017-08-04 09:13:31 -070033#include "AAudioServiceEndpointShared.h"
Phil Burk87c9f642017-05-17 07:22:39 -070034
35using namespace android; // TODO just import names needed
36using namespace aaudio; // TODO just import names needed
37
38AAudioServiceEndpointCapture::AAudioServiceEndpointCapture(AAudioService &audioService)
39 : mStreamInternalCapture(audioService, true) {
Phil Burk39f02dd2017-08-04 09:13:31 -070040 mStreamInternal = &mStreamInternalCapture;
Phil Burk87c9f642017-05-17 07:22:39 -070041}
42
Phil Burk39f02dd2017-08-04 09:13:31 -070043aaudio_result_t AAudioServiceEndpointCapture::open(const aaudio::AAudioStreamRequest &request) {
44 aaudio_result_t result = AAudioServiceEndpointShared::open(request);
Phil Burk87c9f642017-05-17 07:22:39 -070045 if (result == AAUDIO_OK) {
Phil Burk87c9f642017-05-17 07:22:39 -070046 int distributionBufferSizeBytes = getStreamInternal()->getFramesPerBurst()
47 * getStreamInternal()->getBytesPerFrame();
Phil Burk3244f662020-06-25 23:28:35 +000048 mDistributionBuffer = std::make_unique<uint8_t[]>(distributionBufferSizeBytes);
Phil Burk87c9f642017-05-17 07:22:39 -070049 }
50 return result;
51}
52
53// Read data from the shared MMAP stream and then distribute it to the client streams.
54void *AAudioServiceEndpointCapture::callbackLoop() {
Phil Burkfbf031e2017-10-12 15:58:31 -070055 ALOGD("callbackLoop() entering");
Eric Laurenta54f1282017-07-01 19:39:32 -070056 aaudio_result_t result = AAUDIO_OK;
Phil Burk87c9f642017-05-17 07:22:39 -070057 int64_t timeoutNanos = getStreamInternal()->calculateReasonableTimeout();
58
59 // result might be a frame count
60 while (mCallbackEnabled.load() && getStreamInternal()->isActive() && (result >= 0)) {
Phil Burk97350f92017-07-21 15:59:44 -070061
62 int64_t mmapFramesRead = getStreamInternal()->getFramesRead();
63
Phil Burk87c9f642017-05-17 07:22:39 -070064 // Read audio data from stream using a blocking read.
Phil Burk3244f662020-06-25 23:28:35 +000065 result = getStreamInternal()->read(mDistributionBuffer.get(),
66 getFramesPerBurst(), timeoutNanos);
Phil Burk87c9f642017-05-17 07:22:39 -070067 if (result == AAUDIO_ERROR_DISCONNECTED) {
68 disconnectRegisteredStreams();
69 break;
70 } else if (result != getFramesPerBurst()) {
Phil Burkfbf031e2017-10-12 15:58:31 -070071 ALOGW("callbackLoop() read %d / %d",
Phil Burk87c9f642017-05-17 07:22:39 -070072 result, getFramesPerBurst());
73 break;
74 }
75
76 // Distribute data to each active stream.
Phil Burk97350f92017-07-21 15:59:44 -070077 { // brackets are for lock_guard
78
Phil Burk87c9f642017-05-17 07:22:39 -070079 std::lock_guard <std::mutex> lock(mLockStreams);
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -080080 for (const auto& clientStream : mRegisteredStreams) {
Phil Burk762365c2018-12-10 16:02:16 -080081 if (clientStream->isRunning() && !clientStream->isSuspended()) {
Phil Burk523b3042017-09-13 13:03:08 -070082 int64_t clientFramesWritten = 0;
Phil Burk762365c2018-12-10 16:02:16 -080083
Phil Burk523b3042017-09-13 13:03:08 -070084 sp<AAudioServiceStreamShared> streamShared =
Phil Burk39f02dd2017-08-04 09:13:31 -070085 static_cast<AAudioServiceStreamShared *>(clientStream.get());
86
Phil Burk523b3042017-09-13 13:03:08 -070087 {
88 // Lock the AudioFifo to protect against close.
89 std::lock_guard <std::mutex> lock(streamShared->getAudioDataQueueLock());
Phil Burk97350f92017-07-21 15:59:44 -070090
Phil Burk523b3042017-09-13 13:03:08 -070091 FifoBuffer *fifo = streamShared->getAudioDataFifoBuffer_l();
92 if (fifo != nullptr) {
Phil Burk97350f92017-07-21 15:59:44 -070093
Phil Burk523b3042017-09-13 13:03:08 -070094 // Determine offset between framePosition in client's stream
95 // vs the underlying MMAP stream.
96 clientFramesWritten = fifo->getWriteCounter();
97 // There are two indices that refer to the same frame.
98 int64_t positionOffset = mmapFramesRead - clientFramesWritten;
99 streamShared->setTimestampPositionOffset(positionOffset);
100
Phil Burk23296382017-11-20 15:45:11 -0800101 // Is the buffer too full to write a burst?
Phil Burk882c5202018-04-23 10:32:45 -0700102 if (fifo->getEmptyFramesAvailable() <
Phil Burk23296382017-11-20 15:45:11 -0800103 getFramesPerBurst()) {
104 streamShared->incrementXRunCount();
Phil Burk523b3042017-09-13 13:03:08 -0700105 } else {
Phil Burk3244f662020-06-25 23:28:35 +0000106 fifo->write(mDistributionBuffer.get(), getFramesPerBurst());
Phil Burk523b3042017-09-13 13:03:08 -0700107 }
108 clientFramesWritten = fifo->getWriteCounter();
109 }
Eric Laurenta54f1282017-07-01 19:39:32 -0700110 }
Phil Burk97350f92017-07-21 15:59:44 -0700111
Phil Burk523b3042017-09-13 13:03:08 -0700112 if (clientFramesWritten > 0) {
113 // This timestamp represents the completion of data being written into the
114 // client buffer. It is sent to the client and used in the timing model
115 // to decide when data will be available to read.
116 Timestamp timestamp(clientFramesWritten, AudioClock::getNanoseconds());
117 streamShared->markTransferTime(timestamp);
118 }
119
Phil Burk87c9f642017-05-17 07:22:39 -0700120 }
Phil Burk87c9f642017-05-17 07:22:39 -0700121 }
122 }
123 }
124
Phil Burk23296382017-11-20 15:45:11 -0800125 ALOGD("callbackLoop() exiting");
Phil Burk87c9f642017-05-17 07:22:39 -0700126 return NULL; // TODO review
127}