blob: bc769f0058acee3e69dd8f944dd7aeeb24b36934 [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
Ytai Ben-Tsvi734e3502020-08-24 14:57:36 -070038AAudioServiceEndpointCapture::AAudioServiceEndpointCapture(AAudioService& audioService)
39 : AAudioServiceEndpointShared(
40 new AudioStreamInternalCapture(audioService.asAAudioServiceInterface(), true)) {
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) {
Phil Burke358ec62020-10-12 23:42:30 +000068 ALOGD("%s() read() returned AAUDIO_ERROR_DISCONNECTED", __func__);
69 // We do not need the returned vector.
70 (void) AAudioServiceEndpointShared::disconnectRegisteredStreams();
Phil Burk87c9f642017-05-17 07:22:39 -070071 break;
72 } else if (result != getFramesPerBurst()) {
Phil Burkfbf031e2017-10-12 15:58:31 -070073 ALOGW("callbackLoop() read %d / %d",
Phil Burk87c9f642017-05-17 07:22:39 -070074 result, getFramesPerBurst());
75 break;
76 }
77
78 // Distribute data to each active stream.
Phil Burk97350f92017-07-21 15:59:44 -070079 { // brackets are for lock_guard
Phil Burk87c9f642017-05-17 07:22:39 -070080 std::lock_guard <std::mutex> lock(mLockStreams);
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -080081 for (const auto& clientStream : mRegisteredStreams) {
Phil Burk762365c2018-12-10 16:02:16 -080082 if (clientStream->isRunning() && !clientStream->isSuspended()) {
Phil Burk523b3042017-09-13 13:03:08 -070083 sp<AAudioServiceStreamShared> streamShared =
Phil Burk39f02dd2017-08-04 09:13:31 -070084 static_cast<AAudioServiceStreamShared *>(clientStream.get());
Phil Burk8f4fe502020-07-15 23:54:50 +000085 streamShared->writeDataIfRoom(mmapFramesRead,
86 mDistributionBuffer.get(),
87 getFramesPerBurst());
Phil Burk87c9f642017-05-17 07:22:39 -070088 }
Phil Burk87c9f642017-05-17 07:22:39 -070089 }
90 }
91 }
92
Phil Burk23296382017-11-20 15:45:11 -080093 ALOGD("callbackLoop() exiting");
Phil Burk87c9f642017-05-17 07:22:39 -070094 return NULL; // TODO review
95}