blob: 37d105bf5e4ec7ebb156f7afd0eaa08799266f1a [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
43AAudioServiceEndpointCapture::~AAudioServiceEndpointCapture() {
44 delete mDistributionBuffer;
45}
46
Phil Burk39f02dd2017-08-04 09:13:31 -070047aaudio_result_t AAudioServiceEndpointCapture::open(const aaudio::AAudioStreamRequest &request) {
48 aaudio_result_t result = AAudioServiceEndpointShared::open(request);
Phil Burk87c9f642017-05-17 07:22:39 -070049 if (result == AAUDIO_OK) {
50 delete mDistributionBuffer;
51 int distributionBufferSizeBytes = getStreamInternal()->getFramesPerBurst()
52 * getStreamInternal()->getBytesPerFrame();
53 mDistributionBuffer = new uint8_t[distributionBufferSizeBytes];
54 }
55 return result;
56}
57
58// Read data from the shared MMAP stream and then distribute it to the client streams.
59void *AAudioServiceEndpointCapture::callbackLoop() {
Phil Burkfbf031e2017-10-12 15:58:31 -070060 ALOGD("callbackLoop() entering");
Eric Laurenta54f1282017-07-01 19:39:32 -070061 aaudio_result_t result = AAUDIO_OK;
Phil Burk87c9f642017-05-17 07:22:39 -070062 int64_t timeoutNanos = getStreamInternal()->calculateReasonableTimeout();
63
64 // result might be a frame count
65 while (mCallbackEnabled.load() && getStreamInternal()->isActive() && (result >= 0)) {
Phil Burk97350f92017-07-21 15:59:44 -070066
67 int64_t mmapFramesRead = getStreamInternal()->getFramesRead();
68
Phil Burk87c9f642017-05-17 07:22:39 -070069 // Read audio data from stream using a blocking read.
70 result = getStreamInternal()->read(mDistributionBuffer, getFramesPerBurst(), timeoutNanos);
71 if (result == AAUDIO_ERROR_DISCONNECTED) {
72 disconnectRegisteredStreams();
73 break;
74 } else if (result != getFramesPerBurst()) {
Phil Burkfbf031e2017-10-12 15:58:31 -070075 ALOGW("callbackLoop() read %d / %d",
Phil Burk87c9f642017-05-17 07:22:39 -070076 result, getFramesPerBurst());
77 break;
78 }
79
80 // Distribute data to each active stream.
Phil Burk97350f92017-07-21 15:59:44 -070081 { // brackets are for lock_guard
82
Phil Burk87c9f642017-05-17 07:22:39 -070083 std::lock_guard <std::mutex> lock(mLockStreams);
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -080084 for (const auto& clientStream : mRegisteredStreams) {
Phil Burk762365c2018-12-10 16:02:16 -080085 if (clientStream->isRunning() && !clientStream->isSuspended()) {
Phil Burk523b3042017-09-13 13:03:08 -070086 int64_t clientFramesWritten = 0;
Phil Burk762365c2018-12-10 16:02:16 -080087
Phil Burk523b3042017-09-13 13:03:08 -070088 sp<AAudioServiceStreamShared> streamShared =
Phil Burk39f02dd2017-08-04 09:13:31 -070089 static_cast<AAudioServiceStreamShared *>(clientStream.get());
90
Phil Burk523b3042017-09-13 13:03:08 -070091 {
92 // Lock the AudioFifo to protect against close.
93 std::lock_guard <std::mutex> lock(streamShared->getAudioDataQueueLock());
Phil Burk97350f92017-07-21 15:59:44 -070094
Phil Burk523b3042017-09-13 13:03:08 -070095 FifoBuffer *fifo = streamShared->getAudioDataFifoBuffer_l();
96 if (fifo != nullptr) {
Phil Burk97350f92017-07-21 15:59:44 -070097
Phil Burk523b3042017-09-13 13:03:08 -070098 // Determine offset between framePosition in client's stream
99 // vs the underlying MMAP stream.
100 clientFramesWritten = fifo->getWriteCounter();
101 // There are two indices that refer to the same frame.
102 int64_t positionOffset = mmapFramesRead - clientFramesWritten;
103 streamShared->setTimestampPositionOffset(positionOffset);
104
Phil Burk23296382017-11-20 15:45:11 -0800105 // Is the buffer too full to write a burst?
Phil Burk882c5202018-04-23 10:32:45 -0700106 if (fifo->getEmptyFramesAvailable() <
Phil Burk23296382017-11-20 15:45:11 -0800107 getFramesPerBurst()) {
108 streamShared->incrementXRunCount();
Phil Burk523b3042017-09-13 13:03:08 -0700109 } else {
110 fifo->write(mDistributionBuffer, getFramesPerBurst());
111 }
112 clientFramesWritten = fifo->getWriteCounter();
113 }
Eric Laurenta54f1282017-07-01 19:39:32 -0700114 }
Phil Burk97350f92017-07-21 15:59:44 -0700115
Phil Burk523b3042017-09-13 13:03:08 -0700116 if (clientFramesWritten > 0) {
117 // This timestamp represents the completion of data being written into the
118 // client buffer. It is sent to the client and used in the timing model
119 // to decide when data will be available to read.
120 Timestamp timestamp(clientFramesWritten, AudioClock::getNanoseconds());
121 streamShared->markTransferTime(timestamp);
122 }
123
Phil Burk87c9f642017-05-17 07:22:39 -0700124 }
Phil Burk87c9f642017-05-17 07:22:39 -0700125 }
126 }
127 }
128
Phil Burk23296382017-11-20 15:45:11 -0800129 ALOGD("callbackLoop() exiting");
Phil Burk87c9f642017-05-17 07:22:39 -0700130 return NULL; // TODO review
131}