| 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 |  | 
| Ytai Ben-Tsvi | 734e350 | 2020-08-24 14:57:36 -0700 | [diff] [blame] | 38 | AAudioServiceEndpointCapture::AAudioServiceEndpointCapture(AAudioService& audioService) | 
|  | 39 | : AAudioServiceEndpointShared( | 
|  | 40 | new AudioStreamInternalCapture(audioService.asAAudioServiceInterface(), true)) { | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 41 | } | 
|  | 42 |  | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 43 | aaudio_result_t AAudioServiceEndpointCapture::open(const aaudio::AAudioStreamRequest &request) { | 
|  | 44 | aaudio_result_t result = AAudioServiceEndpointShared::open(request); | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 45 | if (result == AAUDIO_OK) { | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 46 | int distributionBufferSizeBytes = getStreamInternal()->getFramesPerBurst() | 
|  | 47 | * getStreamInternal()->getBytesPerFrame(); | 
| Phil Burk | 3244f66 | 2020-06-25 23:28:35 +0000 | [diff] [blame] | 48 | mDistributionBuffer = std::make_unique<uint8_t[]>(distributionBufferSizeBytes); | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 49 | } | 
|  | 50 | return result; | 
|  | 51 | } | 
|  | 52 |  | 
|  | 53 | // Read data from the shared MMAP stream and then distribute it to the client streams. | 
|  | 54 | void *AAudioServiceEndpointCapture::callbackLoop() { | 
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 55 | ALOGD("callbackLoop() entering"); | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 56 | aaudio_result_t result = AAUDIO_OK; | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 57 | int64_t timeoutNanos = getStreamInternal()->calculateReasonableTimeout(); | 
|  | 58 |  | 
|  | 59 | // result might be a frame count | 
|  | 60 | while (mCallbackEnabled.load() && getStreamInternal()->isActive() && (result >= 0)) { | 
| Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 61 |  | 
|  | 62 | int64_t mmapFramesRead = getStreamInternal()->getFramesRead(); | 
|  | 63 |  | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 64 | // Read audio data from stream using a blocking read. | 
| Phil Burk | 3244f66 | 2020-06-25 23:28:35 +0000 | [diff] [blame] | 65 | result = getStreamInternal()->read(mDistributionBuffer.get(), | 
|  | 66 | getFramesPerBurst(), timeoutNanos); | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 67 | if (result == AAUDIO_ERROR_DISCONNECTED) { | 
| Phil Burk | e358ec6 | 2020-10-12 23:42:30 +0000 | [diff] [blame] | 68 | ALOGD("%s() read() returned AAUDIO_ERROR_DISCONNECTED", __func__); | 
|  | 69 | // We do not need the returned vector. | 
|  | 70 | (void) AAudioServiceEndpointShared::disconnectRegisteredStreams(); | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 71 | break; | 
|  | 72 | } else if (result != getFramesPerBurst()) { | 
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 73 | ALOGW("callbackLoop() read %d / %d", | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 74 | result, getFramesPerBurst()); | 
|  | 75 | break; | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | // Distribute data to each active stream. | 
| Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 79 | { // brackets are for lock_guard | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 80 | std::lock_guard <std::mutex> lock(mLockStreams); | 
| Chih-Hung Hsieh | 3ef324d | 2018-12-11 11:48:12 -0800 | [diff] [blame] | 81 | for (const auto& clientStream : mRegisteredStreams) { | 
| Phil Burk | 762365c | 2018-12-10 16:02:16 -0800 | [diff] [blame] | 82 | if (clientStream->isRunning() && !clientStream->isSuspended()) { | 
| Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 83 | sp<AAudioServiceStreamShared> streamShared = | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 84 | static_cast<AAudioServiceStreamShared *>(clientStream.get()); | 
| Phil Burk | 8f4fe50 | 2020-07-15 23:54:50 +0000 | [diff] [blame] | 85 | streamShared->writeDataIfRoom(mmapFramesRead, | 
|  | 86 | mDistributionBuffer.get(), | 
|  | 87 | getFramesPerBurst()); | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 88 | } | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 89 | } | 
|  | 90 | } | 
|  | 91 | } | 
|  | 92 |  | 
| Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 93 | ALOGD("callbackLoop() exiting"); | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 94 | return NULL; // TODO review | 
|  | 95 | } |