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 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 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" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 28 | #include <algorithm> |
| 29 | #include <mutex> |
| 30 | #include <vector> |
| 31 | |
| 32 | #include "core/AudioStreamBuilder.h" |
| 33 | #include "AAudioServiceEndpoint.h" |
| 34 | #include "AAudioServiceStreamShared.h" |
| 35 | |
| 36 | using namespace android; // TODO just import names needed |
| 37 | using namespace aaudio; // TODO just import names needed |
| 38 | |
| 39 | #define MIN_TIMEOUT_NANOS (1000 * AAUDIO_NANOS_PER_MILLISECOND) |
| 40 | |
| 41 | // Wait at least this many times longer than the operation should take. |
| 42 | #define MIN_TIMEOUT_OPERATIONS 4 |
| 43 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 44 | // This is the maximum size in frames. The effective size can be tuned smaller at runtime. |
| 45 | #define DEFAULT_BUFFER_CAPACITY (48 * 8) |
| 46 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 47 | // Set up an EXCLUSIVE MMAP stream that will be shared. |
Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 48 | aaudio_result_t AAudioServiceEndpoint::open(const AAudioStreamConfiguration& configuration) { |
| 49 | mRequestedDeviceId = configuration.getDeviceId(); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 50 | mStreamInternal = getStreamInternal(); |
| 51 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 52 | AudioStreamBuilder builder; |
| 53 | builder.setSharingMode(AAUDIO_SHARING_MODE_EXCLUSIVE); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 54 | // Don't fall back to SHARED because that would cause recursion. |
| 55 | builder.setSharingModeMatchRequired(true); |
Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 56 | builder.setDeviceId(mRequestedDeviceId); |
| 57 | builder.setFormat(configuration.getAudioFormat()); |
| 58 | builder.setSampleRate(configuration.getSampleRate()); |
| 59 | builder.setSamplesPerFrame(configuration.getSamplesPerFrame()); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 60 | builder.setDirection(getDirection()); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 61 | builder.setBufferCapacity(DEFAULT_BUFFER_CAPACITY); |
| 62 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 63 | return getStreamInternal()->open(builder); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | aaudio_result_t AAudioServiceEndpoint::close() { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 67 | return getStreamInternal()->close(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | // TODO, maybe use an interface to reduce exposure |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 71 | aaudio_result_t AAudioServiceEndpoint::registerStream(sp<AAudioServiceStreamShared>sharedStream) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 72 | std::lock_guard<std::mutex> lock(mLockStreams); |
| 73 | mRegisteredStreams.push_back(sharedStream); |
| 74 | return AAUDIO_OK; |
| 75 | } |
| 76 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 77 | aaudio_result_t AAudioServiceEndpoint::unregisterStream(sp<AAudioServiceStreamShared>sharedStream) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 78 | std::lock_guard<std::mutex> lock(mLockStreams); |
| 79 | mRegisteredStreams.erase(std::remove(mRegisteredStreams.begin(), mRegisteredStreams.end(), sharedStream), |
| 80 | mRegisteredStreams.end()); |
| 81 | return AAUDIO_OK; |
| 82 | } |
| 83 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 84 | aaudio_result_t AAudioServiceEndpoint::startStream(sp<AAudioServiceStreamShared> sharedStream) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 85 | // TODO use real-time technique to avoid mutex, eg. atomic command FIFO |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 86 | std::lock_guard<std::mutex> lock(mLockStreams); |
| 87 | mRunningStreams.push_back(sharedStream); |
| 88 | if (mRunningStreams.size() == 1) { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 89 | startSharingThread_l(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 90 | } |
| 91 | return AAUDIO_OK; |
| 92 | } |
| 93 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 94 | aaudio_result_t AAudioServiceEndpoint::stopStream(sp<AAudioServiceStreamShared> sharedStream) { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 95 | int numRunningStreams = 0; |
| 96 | { |
| 97 | std::lock_guard<std::mutex> lock(mLockStreams); |
| 98 | mRunningStreams.erase( |
| 99 | std::remove(mRunningStreams.begin(), mRunningStreams.end(), sharedStream), |
| 100 | mRunningStreams.end()); |
| 101 | numRunningStreams = mRunningStreams.size(); |
| 102 | } |
| 103 | if (numRunningStreams == 0) { |
| 104 | // Don't call this under a lock because the callbackLoop also uses the lock. |
| 105 | stopSharingThread(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 106 | } |
| 107 | return AAUDIO_OK; |
| 108 | } |
| 109 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 110 | static void *aaudio_endpoint_thread_proc(void *context) { |
| 111 | AAudioServiceEndpoint *endpoint = (AAudioServiceEndpoint *) context; |
| 112 | if (endpoint != NULL) { |
| 113 | return endpoint->callbackLoop(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 114 | } else { |
| 115 | return NULL; |
| 116 | } |
| 117 | } |
| 118 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 119 | aaudio_result_t AAudioServiceEndpoint::startSharingThread_l() { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 120 | // Launch the callback loop thread. |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 121 | int64_t periodNanos = getStreamInternal()->getFramesPerBurst() |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 122 | * AAUDIO_NANOS_PER_SECOND |
| 123 | / getSampleRate(); |
| 124 | mCallbackEnabled.store(true); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 125 | return getStreamInternal()->createThread(periodNanos, aaudio_endpoint_thread_proc, this); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 128 | aaudio_result_t AAudioServiceEndpoint::stopSharingThread() { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 129 | mCallbackEnabled.store(false); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 130 | aaudio_result_t result = getStreamInternal()->joinThread(NULL); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 131 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void AAudioServiceEndpoint::disconnectRegisteredStreams() { |
| 135 | std::lock_guard<std::mutex> lock(mLockStreams); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 136 | for(auto baseStream : mRunningStreams) { |
| 137 | baseStream->onStop(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 138 | } |
| 139 | mRunningStreams.clear(); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 140 | for(auto sharedStream : mRegisteredStreams) { |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame^] | 141 | sharedStream->disconnect(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 142 | } |
| 143 | mRegisteredStreams.clear(); |
| 144 | } |
Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 145 | |
| 146 | bool AAudioServiceEndpoint::matches(const AAudioStreamConfiguration& configuration) { |
| 147 | if (configuration.getDeviceId() != AAUDIO_UNSPECIFIED && |
| 148 | configuration.getDeviceId() != mStreamInternal->getDeviceId()) { |
| 149 | return false; |
| 150 | } |
| 151 | if (configuration.getSampleRate() != AAUDIO_UNSPECIFIED && |
| 152 | configuration.getSampleRate() != mStreamInternal->getSampleRate()) { |
| 153 | return false; |
| 154 | } |
| 155 | if (configuration.getSamplesPerFrame() != AAUDIO_UNSPECIFIED && |
| 156 | configuration.getSamplesPerFrame() != mStreamInternal->getSamplesPerFrame()) { |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | return true; |
| 161 | } |