Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [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 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 17 | #define LOG_TAG "AAudioServiceEndpoint" |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | #include <utils/Log.h> |
| 20 | |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 21 | #include <algorithm> |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 22 | #include <assert.h> |
| 23 | #include <map> |
| 24 | #include <mutex> |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 25 | #include <sstream> |
| 26 | #include <vector> |
| 27 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 28 | #include <utils/Singleton.h> |
| 29 | |
| 30 | #include "AAudioEndpointManager.h" |
| 31 | #include "AAudioServiceEndpoint.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 32 | |
| 33 | #include "core/AudioStreamBuilder.h" |
| 34 | #include "AAudioServiceEndpoint.h" |
| 35 | #include "AAudioServiceStreamShared.h" |
| 36 | |
| 37 | using namespace android; // TODO just import names needed |
| 38 | using namespace aaudio; // TODO just import names needed |
| 39 | |
| 40 | #define MIN_TIMEOUT_NANOS (1000 * AAUDIO_NANOS_PER_MILLISECOND) |
| 41 | |
| 42 | // Wait at least this many times longer than the operation should take. |
| 43 | #define MIN_TIMEOUT_OPERATIONS 4 |
| 44 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 45 | // This is the maximum size in frames. The effective size can be tuned smaller at runtime. |
| 46 | #define DEFAULT_BUFFER_CAPACITY (48 * 8) |
| 47 | |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 48 | std::string AAudioServiceEndpoint::dump() const { |
| 49 | std::stringstream result; |
| 50 | |
| 51 | const bool isLocked = AAudio_tryUntilTrue( |
| 52 | [this]()->bool { return mLockStreams.try_lock(); } /* f */, |
| 53 | 50 /* times */, |
| 54 | 20 /* sleepMs */); |
| 55 | if (!isLocked) { |
| 56 | result << "EndpointManager may be deadlocked\n"; |
| 57 | } |
| 58 | |
| 59 | AudioStreamInternal *stream = mStreamInternal; |
| 60 | if (stream == nullptr) { |
| 61 | result << "null stream!" << "\n"; |
| 62 | } else { |
| 63 | result << "mmap stream: rate = " << stream->getSampleRate() << "\n"; |
| 64 | } |
| 65 | |
| 66 | result << " Registered Streams:" << "\n"; |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 67 | result << AAudioServiceStreamShared::dumpHeader() << "\n"; |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 68 | for (sp<AAudioServiceStreamShared> sharedStream : mRegisteredStreams) { |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 69 | result << sharedStream->dump() << "\n"; |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | if (isLocked) { |
| 73 | mLockStreams.unlock(); |
| 74 | } |
| 75 | return result.str(); |
| 76 | } |
| 77 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 78 | // Set up an EXCLUSIVE MMAP stream that will be shared. |
Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 79 | aaudio_result_t AAudioServiceEndpoint::open(const AAudioStreamConfiguration& configuration) { |
| 80 | mRequestedDeviceId = configuration.getDeviceId(); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 81 | mStreamInternal = getStreamInternal(); |
| 82 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 83 | AudioStreamBuilder builder; |
| 84 | builder.setSharingMode(AAUDIO_SHARING_MODE_EXCLUSIVE); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 85 | // Don't fall back to SHARED because that would cause recursion. |
| 86 | builder.setSharingModeMatchRequired(true); |
Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 87 | builder.setDeviceId(mRequestedDeviceId); |
jiabin | 901f65d | 2017-07-12 17:56:35 -0700 | [diff] [blame] | 88 | builder.setFormat(configuration.getFormat()); |
Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 89 | builder.setSampleRate(configuration.getSampleRate()); |
| 90 | builder.setSamplesPerFrame(configuration.getSamplesPerFrame()); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 91 | builder.setDirection(getDirection()); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 92 | builder.setBufferCapacity(DEFAULT_BUFFER_CAPACITY); |
| 93 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 94 | return getStreamInternal()->open(builder); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | aaudio_result_t AAudioServiceEndpoint::close() { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 98 | return getStreamInternal()->close(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // TODO, maybe use an interface to reduce exposure |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 102 | aaudio_result_t AAudioServiceEndpoint::registerStream(sp<AAudioServiceStreamShared>sharedStream) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 103 | std::lock_guard<std::mutex> lock(mLockStreams); |
| 104 | mRegisteredStreams.push_back(sharedStream); |
| 105 | return AAUDIO_OK; |
| 106 | } |
| 107 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 108 | aaudio_result_t AAudioServiceEndpoint::unregisterStream(sp<AAudioServiceStreamShared>sharedStream) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 109 | std::lock_guard<std::mutex> lock(mLockStreams); |
| 110 | mRegisteredStreams.erase(std::remove(mRegisteredStreams.begin(), mRegisteredStreams.end(), sharedStream), |
| 111 | mRegisteredStreams.end()); |
| 112 | return AAUDIO_OK; |
| 113 | } |
| 114 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 115 | aaudio_result_t AAudioServiceEndpoint::startStream(sp<AAudioServiceStreamShared> sharedStream) { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 116 | aaudio_result_t result = AAUDIO_OK; |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 117 | if (++mRunningStreams == 1) { |
Phil Burk | 940083c | 2017-07-17 17:00:02 -0700 | [diff] [blame] | 118 | // TODO use real-time technique to avoid mutex, eg. atomic command FIFO |
| 119 | std::lock_guard<std::mutex> lock(mLockStreams); |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 120 | result = getStreamInternal()->requestStart(); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 121 | startSharingThread_l(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 122 | } |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 123 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 126 | aaudio_result_t AAudioServiceEndpoint::stopStream(sp<AAudioServiceStreamShared> sharedStream) { |
Phil Burk | 940083c | 2017-07-17 17:00:02 -0700 | [diff] [blame] | 127 | // Don't lock here because the disconnectRegisteredStreams also uses the lock. |
| 128 | if (--mRunningStreams == 0) { // atomic |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 129 | stopSharingThread(); |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 130 | getStreamInternal()->requestStop(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 131 | } |
| 132 | return AAUDIO_OK; |
| 133 | } |
| 134 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 135 | static void *aaudio_endpoint_thread_proc(void *context) { |
| 136 | AAudioServiceEndpoint *endpoint = (AAudioServiceEndpoint *) context; |
| 137 | if (endpoint != NULL) { |
| 138 | return endpoint->callbackLoop(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 139 | } else { |
| 140 | return NULL; |
| 141 | } |
| 142 | } |
| 143 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 144 | aaudio_result_t AAudioServiceEndpoint::startSharingThread_l() { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 145 | // Launch the callback loop thread. |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 146 | int64_t periodNanos = getStreamInternal()->getFramesPerBurst() |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 147 | * AAUDIO_NANOS_PER_SECOND |
| 148 | / getSampleRate(); |
| 149 | mCallbackEnabled.store(true); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 150 | return getStreamInternal()->createThread(periodNanos, aaudio_endpoint_thread_proc, this); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 153 | aaudio_result_t AAudioServiceEndpoint::stopSharingThread() { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 154 | mCallbackEnabled.store(false); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 155 | aaudio_result_t result = getStreamInternal()->joinThread(NULL); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 156 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | void AAudioServiceEndpoint::disconnectRegisteredStreams() { |
| 160 | std::lock_guard<std::mutex> lock(mLockStreams); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 161 | for(auto sharedStream : mRegisteredStreams) { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 162 | sharedStream->stop(); |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 163 | sharedStream->disconnect(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 164 | } |
| 165 | mRegisteredStreams.clear(); |
| 166 | } |
Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 167 | |
| 168 | bool AAudioServiceEndpoint::matches(const AAudioStreamConfiguration& configuration) { |
| 169 | if (configuration.getDeviceId() != AAUDIO_UNSPECIFIED && |
| 170 | configuration.getDeviceId() != mStreamInternal->getDeviceId()) { |
| 171 | return false; |
| 172 | } |
| 173 | if (configuration.getSampleRate() != AAUDIO_UNSPECIFIED && |
| 174 | configuration.getSampleRate() != mStreamInternal->getSampleRate()) { |
| 175 | return false; |
| 176 | } |
| 177 | if (configuration.getSamplesPerFrame() != AAUDIO_UNSPECIFIED && |
| 178 | configuration.getSamplesPerFrame() != mStreamInternal->getSamplesPerFrame()) { |
| 179 | return false; |
| 180 | } |
Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 181 | return true; |
| 182 | } |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 183 | |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 184 | |
| 185 | aaudio_result_t AAudioServiceEndpoint::getTimestamp(int64_t *positionFrames, int64_t *timeNanos) { |
| 186 | return mStreamInternal->getTimestamp(CLOCK_MONOTONIC, positionFrames, timeNanos); |
| 187 | } |