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 : mStreamInternalCapture(audioService, true) {
40 mStreamInternal = &mStreamInternalCapture;
41 }
42
~AAudioServiceEndpointCapture()43 AAudioServiceEndpointCapture::~AAudioServiceEndpointCapture() {
44 delete mDistributionBuffer;
45 }
46
open(const aaudio::AAudioStreamRequest & request)47 aaudio_result_t AAudioServiceEndpointCapture::open(const aaudio::AAudioStreamRequest &request) {
48 aaudio_result_t result = AAudioServiceEndpointShared::open(request);
49 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.
callbackLoop()59 void *AAudioServiceEndpointCapture::callbackLoop() {
60 ALOGD("callbackLoop() entering");
61 aaudio_result_t result = AAUDIO_OK;
62 int64_t timeoutNanos = getStreamInternal()->calculateReasonableTimeout();
63
64 // result might be a frame count
65 while (mCallbackEnabled.load() && getStreamInternal()->isActive() && (result >= 0)) {
66
67 int64_t mmapFramesRead = getStreamInternal()->getFramesRead();
68
69 // 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()) {
75 ALOGW("callbackLoop() read %d / %d",
76 result, getFramesPerBurst());
77 break;
78 }
79
80 // Distribute data to each active stream.
81 { // brackets are for lock_guard
82
83 std::lock_guard <std::mutex> lock(mLockStreams);
84 for (const auto clientStream : mRegisteredStreams) {
85 if (clientStream->isRunning()) {
86 int64_t clientFramesWritten = 0;
87 sp<AAudioServiceStreamShared> streamShared =
88 static_cast<AAudioServiceStreamShared *>(clientStream.get());
89
90 {
91 // Lock the AudioFifo to protect against close.
92 std::lock_guard <std::mutex> lock(streamShared->getAudioDataQueueLock());
93
94 FifoBuffer *fifo = streamShared->getAudioDataFifoBuffer_l();
95 if (fifo != nullptr) {
96
97 // Determine offset between framePosition in client's stream
98 // vs the underlying MMAP stream.
99 clientFramesWritten = fifo->getWriteCounter();
100 // There are two indices that refer to the same frame.
101 int64_t positionOffset = mmapFramesRead - clientFramesWritten;
102 streamShared->setTimestampPositionOffset(positionOffset);
103
104 // Is the buffer too full to write a burst?
105 if (fifo->getFifoControllerBase()->getEmptyFramesAvailable() <
106 getFramesPerBurst()) {
107 streamShared->incrementXRunCount();
108 } else {
109 fifo->write(mDistributionBuffer, getFramesPerBurst());
110 }
111 clientFramesWritten = fifo->getWriteCounter();
112 }
113 }
114
115 if (clientFramesWritten > 0) {
116 // This timestamp represents the completion of data being written into the
117 // client buffer. It is sent to the client and used in the timing model
118 // to decide when data will be available to read.
119 Timestamp timestamp(clientFramesWritten, AudioClock::getNanoseconds());
120 streamShared->markTransferTime(timestamp);
121 }
122
123 }
124 }
125 }
126 }
127
128 ALOGD("callbackLoop() exiting");
129 return NULL; // TODO review
130 }
131