1 /*
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 "AAudioServiceEndpointCapture"
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 #include "AAudioServiceEndpointShared.h"
34
35 using namespace android; // TODO just import names needed
36 using namespace aaudio; // TODO just import names needed
37
AAudioServiceEndpointCapture(AAudioService & audioService)38 AAudioServiceEndpointCapture::AAudioServiceEndpointCapture(AAudioService& audioService)
39 : AAudioServiceEndpointShared(
40 new AudioStreamInternalCapture(audioService.asAAudioServiceInterface(), true)) {
41 }
42
open(const aaudio::AAudioStreamRequest & request)43 aaudio_result_t AAudioServiceEndpointCapture::open(const aaudio::AAudioStreamRequest &request) {
44 aaudio_result_t result = AAudioServiceEndpointShared::open(request);
45 if (result == AAUDIO_OK) {
46 int distributionBufferSizeBytes = getStreamInternal()->getFramesPerBurst()
47 * getStreamInternal()->getBytesPerFrame();
48 mDistributionBuffer = std::make_unique<uint8_t[]>(distributionBufferSizeBytes);
49 }
50 return result;
51 }
52
53 // Read data from the shared MMAP stream and then distribute it to the client streams.
callbackLoop()54 void *AAudioServiceEndpointCapture::callbackLoop() {
55 ALOGD("callbackLoop() entering");
56 aaudio_result_t result = AAUDIO_OK;
57 int64_t timeoutNanos = getStreamInternal()->calculateReasonableTimeout();
58
59 // result might be a frame count
60 while (mCallbackEnabled.load() && getStreamInternal()->isActive() && (result >= 0)) {
61
62 int64_t mmapFramesRead = getStreamInternal()->getFramesRead();
63
64 // Read audio data from stream using a blocking read.
65 result = getStreamInternal()->read(mDistributionBuffer.get(),
66 getFramesPerBurst(), timeoutNanos);
67 if (result == AAUDIO_ERROR_DISCONNECTED) {
68 ALOGD("%s() read() returned AAUDIO_ERROR_DISCONNECTED", __func__);
69 AAudioServiceEndpointShared::handleDisconnectRegisteredStreamsAsync();
70 break;
71 } else if (result != getFramesPerBurst()) {
72 ALOGW("callbackLoop() read %d / %d",
73 result, getFramesPerBurst());
74 break;
75 }
76
77 // Distribute data to each active stream.
78 { // brackets are for lock_guard
79 std::lock_guard <std::mutex> lock(mLockStreams);
80 for (const auto& clientStream : mRegisteredStreams) {
81 if (clientStream->isRunning() && !clientStream->isSuspended()) {
82 sp<AAudioServiceStreamShared> streamShared =
83 static_cast<AAudioServiceStreamShared *>(clientStream.get());
84 streamShared->writeDataIfRoom(mmapFramesRead,
85 mDistributionBuffer.get(),
86 getFramesPerBurst());
87 }
88 }
89 }
90 }
91
92 ALOGD("callbackLoop() exiting");
93 return nullptr; // TODO review
94 }
95