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. |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 48 | aaudio_result_t AAudioServiceEndpoint::open(int32_t deviceId) { |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 49 | mRequestedDeviceId = deviceId; |
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); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 56 | builder.setDeviceId(deviceId); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 57 | builder.setDirection(getDirection()); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 58 | builder.setBufferCapacity(DEFAULT_BUFFER_CAPACITY); |
| 59 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 60 | return getStreamInternal()->open(builder); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | aaudio_result_t AAudioServiceEndpoint::close() { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 64 | return getStreamInternal()->close(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | // TODO, maybe use an interface to reduce exposure |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame^] | 68 | aaudio_result_t AAudioServiceEndpoint::registerStream(sp<AAudioServiceStreamShared>sharedStream) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 69 | std::lock_guard<std::mutex> lock(mLockStreams); |
| 70 | mRegisteredStreams.push_back(sharedStream); |
| 71 | return AAUDIO_OK; |
| 72 | } |
| 73 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame^] | 74 | aaudio_result_t AAudioServiceEndpoint::unregisterStream(sp<AAudioServiceStreamShared>sharedStream) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 75 | std::lock_guard<std::mutex> lock(mLockStreams); |
| 76 | mRegisteredStreams.erase(std::remove(mRegisteredStreams.begin(), mRegisteredStreams.end(), sharedStream), |
| 77 | mRegisteredStreams.end()); |
| 78 | return AAUDIO_OK; |
| 79 | } |
| 80 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame^] | 81 | aaudio_result_t AAudioServiceEndpoint::startStream(sp<AAudioServiceStreamShared> sharedStream) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 82 | // TODO use real-time technique to avoid mutex, eg. atomic command FIFO |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 83 | std::lock_guard<std::mutex> lock(mLockStreams); |
| 84 | mRunningStreams.push_back(sharedStream); |
| 85 | if (mRunningStreams.size() == 1) { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 86 | startSharingThread_l(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 87 | } |
| 88 | return AAUDIO_OK; |
| 89 | } |
| 90 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame^] | 91 | aaudio_result_t AAudioServiceEndpoint::stopStream(sp<AAudioServiceStreamShared> sharedStream) { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 92 | int numRunningStreams = 0; |
| 93 | { |
| 94 | std::lock_guard<std::mutex> lock(mLockStreams); |
| 95 | mRunningStreams.erase( |
| 96 | std::remove(mRunningStreams.begin(), mRunningStreams.end(), sharedStream), |
| 97 | mRunningStreams.end()); |
| 98 | numRunningStreams = mRunningStreams.size(); |
| 99 | } |
| 100 | if (numRunningStreams == 0) { |
| 101 | // Don't call this under a lock because the callbackLoop also uses the lock. |
| 102 | stopSharingThread(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 103 | } |
| 104 | return AAUDIO_OK; |
| 105 | } |
| 106 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 107 | static void *aaudio_endpoint_thread_proc(void *context) { |
| 108 | AAudioServiceEndpoint *endpoint = (AAudioServiceEndpoint *) context; |
| 109 | if (endpoint != NULL) { |
| 110 | return endpoint->callbackLoop(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 111 | } else { |
| 112 | return NULL; |
| 113 | } |
| 114 | } |
| 115 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 116 | aaudio_result_t AAudioServiceEndpoint::startSharingThread_l() { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 117 | // Launch the callback loop thread. |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 118 | int64_t periodNanos = getStreamInternal()->getFramesPerBurst() |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 119 | * AAUDIO_NANOS_PER_SECOND |
| 120 | / getSampleRate(); |
| 121 | mCallbackEnabled.store(true); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 122 | return getStreamInternal()->createThread(periodNanos, aaudio_endpoint_thread_proc, this); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 125 | aaudio_result_t AAudioServiceEndpoint::stopSharingThread() { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 126 | mCallbackEnabled.store(false); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 127 | aaudio_result_t result = getStreamInternal()->joinThread(NULL); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 128 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void AAudioServiceEndpoint::disconnectRegisteredStreams() { |
| 132 | std::lock_guard<std::mutex> lock(mLockStreams); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame^] | 133 | for(auto baseStream : mRunningStreams) { |
| 134 | baseStream->onStop(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 135 | } |
| 136 | mRunningStreams.clear(); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame^] | 137 | for(auto sharedStream : mRegisteredStreams) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 138 | sharedStream->onDisconnect(); |
| 139 | } |
| 140 | mRegisteredStreams.clear(); |
| 141 | } |