Phil Burk | 39f02dd | 2017-08-04 09:13:31 -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 | |
| 17 | |
| 18 | #define LOG_TAG "AAudioServiceEndpointShared" |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | #include <utils/Log.h> |
| 21 | |
| 22 | #include <iomanip> |
| 23 | #include <iostream> |
| 24 | #include <sstream> |
| 25 | |
| 26 | #include "binding/AAudioServiceMessage.h" |
| 27 | #include "client/AudioStreamInternal.h" |
| 28 | #include "client/AudioStreamInternalPlay.h" |
| 29 | #include "core/AudioStreamBuilder.h" |
| 30 | |
| 31 | #include "AAudioServiceEndpointShared.h" |
| 32 | #include "AAudioServiceStreamShared.h" |
| 33 | #include "AAudioServiceStreamMMAP.h" |
| 34 | #include "AAudioMixer.h" |
| 35 | #include "AAudioService.h" |
| 36 | |
| 37 | using namespace android; |
| 38 | using namespace aaudio; |
| 39 | |
| 40 | // This is the maximum size in frames. The effective size can be tuned smaller at runtime. |
| 41 | #define DEFAULT_BUFFER_CAPACITY (48 * 8) |
| 42 | |
| 43 | std::string AAudioServiceEndpointShared::dump() const { |
| 44 | std::stringstream result; |
| 45 | |
| 46 | result << " SHARED: sharing exclusive stream with handle = 0x" |
| 47 | << std::setfill('0') << std::setw(8) |
| 48 | << std::hex << mStreamInternal->getServiceHandle() |
| 49 | << std::dec << std::setfill(' '); |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 50 | result << ", XRuns = " << mStreamInternal->getXRunCount(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 51 | result << "\n"; |
| 52 | result << " Running Stream Count: " << mRunningStreamCount << "\n"; |
| 53 | |
| 54 | result << AAudioServiceEndpoint::dump(); |
| 55 | return result.str(); |
| 56 | } |
| 57 | |
| 58 | // Share an AudioStreamInternal. |
| 59 | aaudio_result_t AAudioServiceEndpointShared::open(const aaudio::AAudioStreamRequest &request) { |
| 60 | aaudio_result_t result = AAUDIO_OK; |
| 61 | const AAudioStreamConfiguration &configuration = request.getConstantConfiguration(); |
| 62 | |
Phil Burk | a62fb95 | 2018-01-16 12:44:06 -0800 | [diff] [blame] | 63 | copyFrom(configuration); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 64 | mRequestedDeviceId = configuration.getDeviceId(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 65 | |
| 66 | AudioStreamBuilder builder; |
Phil Burk | a62fb95 | 2018-01-16 12:44:06 -0800 | [diff] [blame] | 67 | builder.copyFrom(configuration); |
| 68 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 69 | builder.setSharingMode(AAUDIO_SHARING_MODE_EXCLUSIVE); |
| 70 | // Don't fall back to SHARED because that would cause recursion. |
| 71 | builder.setSharingModeMatchRequired(true); |
Phil Burk | a62fb95 | 2018-01-16 12:44:06 -0800 | [diff] [blame] | 72 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 73 | builder.setBufferCapacity(DEFAULT_BUFFER_CAPACITY); |
| 74 | |
| 75 | result = mStreamInternal->open(builder); |
| 76 | |
| 77 | setSampleRate(mStreamInternal->getSampleRate()); |
| 78 | setSamplesPerFrame(mStreamInternal->getSamplesPerFrame()); |
| 79 | setDeviceId(mStreamInternal->getDeviceId()); |
Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 80 | setSessionId(mStreamInternal->getSessionId()); |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 81 | setFormat(AUDIO_FORMAT_PCM_FLOAT); // force for mixer |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 82 | mFramesPerBurst = mStreamInternal->getFramesPerBurst(); |
| 83 | |
| 84 | return result; |
| 85 | } |
| 86 | |
| 87 | aaudio_result_t AAudioServiceEndpointShared::close() { |
| 88 | return getStreamInternal()->close(); |
| 89 | } |
| 90 | |
| 91 | // Glue between C and C++ callbacks. |
Phil Burk | be64550 | 2018-03-22 13:48:18 -0700 | [diff] [blame] | 92 | static void *aaudio_endpoint_thread_proc(void *arg) { |
| 93 | assert(arg != nullptr); |
| 94 | |
| 95 | // The caller passed in a smart pointer to prevent the endpoint from getting deleted |
| 96 | // while the thread was launching. |
| 97 | sp<AAudioServiceEndpointShared> *endpointForThread = |
| 98 | static_cast<sp<AAudioServiceEndpointShared> *>(arg); |
| 99 | sp<AAudioServiceEndpointShared> endpoint = *endpointForThread; |
| 100 | delete endpointForThread; // Just use scoped smart pointer. Don't need this anymore. |
| 101 | void *result = endpoint->callbackLoop(); |
| 102 | // Close now so that the HW resource is freed and we can open a new device. |
| 103 | if (!endpoint->isConnected()) { |
| 104 | endpoint->close(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 105 | } |
Phil Burk | be64550 | 2018-03-22 13:48:18 -0700 | [diff] [blame] | 106 | |
| 107 | return result; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | aaudio_result_t aaudio::AAudioServiceEndpointShared::startSharingThread_l() { |
| 111 | // Launch the callback loop thread. |
| 112 | int64_t periodNanos = getStreamInternal()->getFramesPerBurst() |
| 113 | * AAUDIO_NANOS_PER_SECOND |
| 114 | / getSampleRate(); |
| 115 | mCallbackEnabled.store(true); |
Phil Burk | be64550 | 2018-03-22 13:48:18 -0700 | [diff] [blame] | 116 | // Pass a smart pointer so the thread can hold a reference. |
| 117 | sp<AAudioServiceEndpointShared> *endpointForThread = new sp<AAudioServiceEndpointShared>(this); |
| 118 | aaudio_result_t result = getStreamInternal()->createThread(periodNanos, |
| 119 | aaudio_endpoint_thread_proc, |
| 120 | endpointForThread); |
| 121 | if (result != AAUDIO_OK) { |
| 122 | // The thread can't delete it so we have to do it here. |
| 123 | delete endpointForThread; |
| 124 | } |
| 125 | return result; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | aaudio_result_t aaudio::AAudioServiceEndpointShared::stopSharingThread() { |
| 129 | mCallbackEnabled.store(false); |
| 130 | aaudio_result_t result = getStreamInternal()->joinThread(NULL); |
| 131 | return result; |
| 132 | } |
| 133 | |
| 134 | aaudio_result_t AAudioServiceEndpointShared::startStream(sp<AAudioServiceStreamBase> sharedStream, |
| 135 | audio_port_handle_t *clientHandle) { |
| 136 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | b92c988 | 2017-09-15 11:39:15 -0700 | [diff] [blame] | 137 | |
| 138 | { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 139 | std::lock_guard<std::mutex> lock(mLockStreams); |
Phil Burk | b92c988 | 2017-09-15 11:39:15 -0700 | [diff] [blame] | 140 | if (++mRunningStreamCount == 1) { // atomic |
| 141 | result = getStreamInternal()->requestStart(); |
| 142 | if (result != AAUDIO_OK) { |
| 143 | --mRunningStreamCount; |
| 144 | } else { |
| 145 | result = startSharingThread_l(); |
| 146 | if (result != AAUDIO_OK) { |
| 147 | getStreamInternal()->requestStop(); |
| 148 | --mRunningStreamCount; |
| 149 | } |
| 150 | } |
| 151 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 152 | } |
Phil Burk | b92c988 | 2017-09-15 11:39:15 -0700 | [diff] [blame] | 153 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 154 | if (result == AAUDIO_OK) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 155 | result = getStreamInternal()->startClient(sharedStream->getAudioClient(), clientHandle); |
Phil Burk | b92c988 | 2017-09-15 11:39:15 -0700 | [diff] [blame] | 156 | if (result != AAUDIO_OK) { |
| 157 | if (--mRunningStreamCount == 0) { // atomic |
| 158 | stopSharingThread(); |
| 159 | getStreamInternal()->requestStop(); |
| 160 | } |
| 161 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 162 | } |
Phil Burk | b92c988 | 2017-09-15 11:39:15 -0700 | [diff] [blame] | 163 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 164 | return result; |
| 165 | } |
| 166 | |
| 167 | aaudio_result_t AAudioServiceEndpointShared::stopStream(sp<AAudioServiceStreamBase> sharedStream, |
| 168 | audio_port_handle_t clientHandle) { |
| 169 | // Don't lock here because the disconnectRegisteredStreams also uses the lock. |
| 170 | |
| 171 | // Ignore result. |
| 172 | (void) getStreamInternal()->stopClient(clientHandle); |
| 173 | |
| 174 | if (--mRunningStreamCount == 0) { // atomic |
| 175 | stopSharingThread(); |
| 176 | getStreamInternal()->requestStop(); |
| 177 | } |
| 178 | return AAUDIO_OK; |
| 179 | } |
| 180 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 181 | // Get timestamp that was written by the real-time service thread, eg. mixer. |
| 182 | aaudio_result_t AAudioServiceEndpointShared::getFreeRunningPosition(int64_t *positionFrames, |
| 183 | int64_t *timeNanos) { |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 184 | if (mAtomicEndpointTimestamp.isValid()) { |
| 185 | Timestamp timestamp = mAtomicEndpointTimestamp.read(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 186 | *positionFrames = timestamp.getPosition(); |
| 187 | *timeNanos = timestamp.getNanoseconds(); |
| 188 | return AAUDIO_OK; |
| 189 | } else { |
| 190 | return AAUDIO_ERROR_UNAVAILABLE; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | aaudio_result_t AAudioServiceEndpointShared::getTimestamp(int64_t *positionFrames, |
| 195 | int64_t *timeNanos) { |
Phil Burk | 56d34f2 | 2018-10-11 13:30:56 -0700 | [diff] [blame] | 196 | aaudio_result_t result = mStreamInternal->getTimestamp(CLOCK_MONOTONIC, positionFrames, timeNanos); |
| 197 | if (result == AAUDIO_ERROR_INVALID_STATE) { |
| 198 | // getTimestamp() can return AAUDIO_ERROR_INVALID_STATE if the stream has |
| 199 | // not completely started. This can cause a race condition that kills the |
| 200 | // timestamp service thread. So we reduce the error to a less serious one |
| 201 | // that allows the timestamp thread to continue. |
| 202 | result = AAUDIO_ERROR_UNAVAILABLE; |
| 203 | } |
| 204 | return result; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 205 | } |