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 "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
34 using namespace android; // TODO just import names needed
35 using namespace aaudio; // TODO just import names needed
36
AAudioServiceEndpointCapture(AAudioService & audioService)37 AAudioServiceEndpointCapture::AAudioServiceEndpointCapture(AAudioService &audioService)
38 : mStreamInternalCapture(audioService, true) {
39 }
40
~AAudioServiceEndpointCapture()41 AAudioServiceEndpointCapture::~AAudioServiceEndpointCapture() {
42 delete mDistributionBuffer;
43 }
44
open(int32_t deviceId)45 aaudio_result_t AAudioServiceEndpointCapture::open(int32_t deviceId) {
46 aaudio_result_t result = AAudioServiceEndpoint::open(deviceId);
47 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.
callbackLoop()57 void *AAudioServiceEndpointCapture::callbackLoop() {
58 ALOGD("AAudioServiceEndpointCapture(): callbackLoop() entering");
59 int32_t underflowCount = 0;
60
61 aaudio_result_t result = getStreamInternal()->requestStart();
62
63 int64_t timeoutNanos = getStreamInternal()->calculateReasonableTimeout();
64
65 // result might be a frame count
66 while (mCallbackEnabled.load() && getStreamInternal()->isActive() && (result >= 0)) {
67 // Read audio data from stream using a blocking read.
68 result = getStreamInternal()->read(mDistributionBuffer, getFramesPerBurst(), timeoutNanos);
69 if (result == AAUDIO_ERROR_DISCONNECTED) {
70 disconnectRegisteredStreams();
71 break;
72 } else if (result != getFramesPerBurst()) {
73 ALOGW("AAudioServiceEndpointCapture(): callbackLoop() read %d / %d",
74 result, getFramesPerBurst());
75 break;
76 }
77
78 // Distribute data to each active stream.
79 { // use lock guard
80 std::lock_guard <std::mutex> lock(mLockStreams);
81 for (AAudioServiceStreamShared *sharedStream : mRunningStreams) {
82 FifoBuffer *fifo = sharedStream->getDataFifoBuffer();
83 if (fifo->getFifoControllerBase()->getEmptyFramesAvailable() <
84 getFramesPerBurst()) {
85 underflowCount++;
86 } else {
87 fifo->write(mDistributionBuffer, getFramesPerBurst());
88 }
89 sharedStream->markTransferTime(AudioClock::getNanoseconds());
90 }
91 }
92 }
93
94 result = getStreamInternal()->requestStop();
95
96 ALOGD("AAudioServiceEndpointCapture(): callbackLoop() exiting, %d underflows", underflowCount);
97 return NULL; // TODO review
98 }
99