Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 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 | |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 17 | #define LOG_TAG "AAudioServiceEndpointCapture" |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 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" |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 33 | #include "AAudioServiceEndpointShared.h" |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 34 | |
| 35 | using namespace android; // TODO just import names needed |
| 36 | using namespace aaudio; // TODO just import names needed |
| 37 | |
| 38 | AAudioServiceEndpointCapture::AAudioServiceEndpointCapture(AAudioService &audioService) |
Phil Burk | 79224ca | 2020-08-12 14:29:10 +0000 | [diff] [blame] | 39 | : AAudioServiceEndpointShared( |
| 40 | (AudioStreamInternal *)(new AudioStreamInternalCapture(audioService, true))) { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | AAudioServiceEndpointCapture::~AAudioServiceEndpointCapture() { |
Phil Burk | 79224ca | 2020-08-12 14:29:10 +0000 | [diff] [blame] | 44 | delete[] mDistributionBuffer; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 47 | aaudio_result_t AAudioServiceEndpointCapture::open(const aaudio::AAudioStreamRequest &request) { |
| 48 | aaudio_result_t result = AAudioServiceEndpointShared::open(request); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 49 | if (result == AAUDIO_OK) { |
Phil Burk | 79224ca | 2020-08-12 14:29:10 +0000 | [diff] [blame] | 50 | delete[] mDistributionBuffer; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 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. |
| 59 | void *AAudioServiceEndpointCapture::callbackLoop() { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 60 | ALOGD("callbackLoop() entering"); |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 61 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 62 | int64_t timeoutNanos = getStreamInternal()->calculateReasonableTimeout(); |
| 63 | |
| 64 | // result might be a frame count |
| 65 | while (mCallbackEnabled.load() && getStreamInternal()->isActive() && (result >= 0)) { |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 66 | |
| 67 | int64_t mmapFramesRead = getStreamInternal()->getFramesRead(); |
| 68 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 69 | // Read audio data from stream using a blocking read. |
| 70 | result = getStreamInternal()->read(mDistributionBuffer, getFramesPerBurst(), timeoutNanos); |
| 71 | if (result == AAUDIO_ERROR_DISCONNECTED) { |
Phil Burk | f2d8470 | 2020-10-12 23:42:30 +0000 | [diff] [blame] | 72 | ALOGD("%s() read() returned AAUDIO_ERROR_DISCONNECTED", __func__); |
| 73 | // We do not need the returned vector. |
| 74 | (void) AAudioServiceEndpointShared::disconnectRegisteredStreams(); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 75 | break; |
| 76 | } else if (result != getFramesPerBurst()) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 77 | ALOGW("callbackLoop() read %d / %d", |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 78 | result, getFramesPerBurst()); |
| 79 | break; |
| 80 | } |
| 81 | |
| 82 | // Distribute data to each active stream. |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 83 | { // brackets are for lock_guard |
| 84 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 85 | std::lock_guard <std::mutex> lock(mLockStreams); |
Chih-Hung Hsieh | 3ef324d | 2018-12-11 11:48:12 -0800 | [diff] [blame] | 86 | for (const auto& clientStream : mRegisteredStreams) { |
Phil Burk | 762365c | 2018-12-10 16:02:16 -0800 | [diff] [blame] | 87 | if (clientStream->isRunning() && !clientStream->isSuspended()) { |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 88 | int64_t clientFramesWritten = 0; |
Phil Burk | 762365c | 2018-12-10 16:02:16 -0800 | [diff] [blame] | 89 | |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 90 | sp<AAudioServiceStreamShared> streamShared = |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 91 | static_cast<AAudioServiceStreamShared *>(clientStream.get()); |
| 92 | |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 93 | { |
| 94 | // Lock the AudioFifo to protect against close. |
| 95 | std::lock_guard <std::mutex> lock(streamShared->getAudioDataQueueLock()); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 96 | |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 97 | FifoBuffer *fifo = streamShared->getAudioDataFifoBuffer_l(); |
| 98 | if (fifo != nullptr) { |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 99 | |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 100 | // Determine offset between framePosition in client's stream |
| 101 | // vs the underlying MMAP stream. |
| 102 | clientFramesWritten = fifo->getWriteCounter(); |
| 103 | // There are two indices that refer to the same frame. |
| 104 | int64_t positionOffset = mmapFramesRead - clientFramesWritten; |
| 105 | streamShared->setTimestampPositionOffset(positionOffset); |
| 106 | |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 107 | // Is the buffer too full to write a burst? |
Phil Burk | 882c520 | 2018-04-23 10:32:45 -0700 | [diff] [blame] | 108 | if (fifo->getEmptyFramesAvailable() < |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 109 | getFramesPerBurst()) { |
| 110 | streamShared->incrementXRunCount(); |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 111 | } else { |
| 112 | fifo->write(mDistributionBuffer, getFramesPerBurst()); |
| 113 | } |
| 114 | clientFramesWritten = fifo->getWriteCounter(); |
| 115 | } |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 116 | } |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 117 | |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 118 | if (clientFramesWritten > 0) { |
| 119 | // This timestamp represents the completion of data being written into the |
| 120 | // client buffer. It is sent to the client and used in the timing model |
| 121 | // to decide when data will be available to read. |
| 122 | Timestamp timestamp(clientFramesWritten, AudioClock::getNanoseconds()); |
| 123 | streamShared->markTransferTime(timestamp); |
| 124 | } |
| 125 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 126 | } |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 131 | ALOGD("callbackLoop() exiting"); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 132 | return NULL; // TODO review |
| 133 | } |