| 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 |  | 
| Phil Burk | 58f5ce1 | 2020-08-12 14:29:10 +0000 | [diff] [blame] | 43 | AAudioServiceEndpointShared::AAudioServiceEndpointShared(AudioStreamInternal *streamInternal) | 
|  | 44 | : mStreamInternal(streamInternal) {} | 
|  | 45 |  | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 46 | std::string AAudioServiceEndpointShared::dump() const { | 
|  | 47 | std::stringstream result; | 
|  | 48 |  | 
|  | 49 | result << "  SHARED: sharing exclusive stream with handle = 0x" | 
|  | 50 | << std::setfill('0') << std::setw(8) | 
|  | 51 | << std::hex << mStreamInternal->getServiceHandle() | 
|  | 52 | << std::dec << std::setfill(' '); | 
| Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 53 | result << ", XRuns = " << mStreamInternal->getXRunCount(); | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 54 | result << "\n"; | 
|  | 55 | result << "    Running Stream Count: " << mRunningStreamCount << "\n"; | 
|  | 56 |  | 
|  | 57 | result << AAudioServiceEndpoint::dump(); | 
|  | 58 | return result.str(); | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | // Share an AudioStreamInternal. | 
|  | 62 | aaudio_result_t AAudioServiceEndpointShared::open(const aaudio::AAudioStreamRequest &request) { | 
|  | 63 | aaudio_result_t result = AAUDIO_OK; | 
|  | 64 | const AAudioStreamConfiguration &configuration = request.getConstantConfiguration(); | 
|  | 65 |  | 
| Phil Burk | a62fb95 | 2018-01-16 12:44:06 -0800 | [diff] [blame] | 66 | copyFrom(configuration); | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 67 | mRequestedDeviceId = configuration.getDeviceId(); | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 68 |  | 
|  | 69 | AudioStreamBuilder builder; | 
| Phil Burk | a62fb95 | 2018-01-16 12:44:06 -0800 | [diff] [blame] | 70 | builder.copyFrom(configuration); | 
|  | 71 |  | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 72 | builder.setSharingMode(AAUDIO_SHARING_MODE_EXCLUSIVE); | 
|  | 73 | // Don't fall back to SHARED because that would cause recursion. | 
|  | 74 | builder.setSharingModeMatchRequired(true); | 
| Phil Burk | a62fb95 | 2018-01-16 12:44:06 -0800 | [diff] [blame] | 75 |  | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 76 | builder.setBufferCapacity(DEFAULT_BUFFER_CAPACITY); | 
|  | 77 |  | 
|  | 78 | result = mStreamInternal->open(builder); | 
|  | 79 |  | 
|  | 80 | setSampleRate(mStreamInternal->getSampleRate()); | 
|  | 81 | setSamplesPerFrame(mStreamInternal->getSamplesPerFrame()); | 
|  | 82 | setDeviceId(mStreamInternal->getDeviceId()); | 
| Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 83 | setSessionId(mStreamInternal->getSessionId()); | 
| Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 84 | setFormat(AUDIO_FORMAT_PCM_FLOAT); // force for mixer | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 85 | mFramesPerBurst = mStreamInternal->getFramesPerBurst(); | 
|  | 86 |  | 
|  | 87 | return result; | 
|  | 88 | } | 
|  | 89 |  | 
| Phil Burk | 320910f | 2020-08-12 14:29:10 +0000 | [diff] [blame] | 90 | void AAudioServiceEndpointShared::close() { | 
| Phil Burk | dd58292 | 2020-10-15 20:29:51 +0000 | [diff] [blame^] | 91 | stopSharingThread(); | 
|  | 92 | getStreamInternal()->safeReleaseClose(); | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 93 | } | 
|  | 94 |  | 
|  | 95 | // Glue between C and C++ callbacks. | 
| Phil Burk | be64550 | 2018-03-22 13:48:18 -0700 | [diff] [blame] | 96 | static void *aaudio_endpoint_thread_proc(void *arg) { | 
|  | 97 | assert(arg != nullptr); | 
| Phil Burk | dd58292 | 2020-10-15 20:29:51 +0000 | [diff] [blame^] | 98 | ALOGD("%s() called", __func__); | 
| Phil Burk | be64550 | 2018-03-22 13:48:18 -0700 | [diff] [blame] | 99 |  | 
| Phil Burk | dd58292 | 2020-10-15 20:29:51 +0000 | [diff] [blame^] | 100 | // Prevent the stream from being deleted while being used. | 
|  | 101 | // This is just for extra safety. It is probably not needed because | 
|  | 102 | // this callback should be joined before the stream is closed. | 
|  | 103 | AAudioServiceEndpointShared *endpointPtr = | 
|  | 104 | static_cast<AAudioServiceEndpointShared *>(arg); | 
|  | 105 | android::sp<AAudioServiceEndpointShared> endpoint(endpointPtr); | 
|  | 106 | // Balance the incStrong() in startSharingThread_l(). | 
|  | 107 | endpoint->decStrong(nullptr); | 
|  | 108 |  | 
| Phil Burk | be64550 | 2018-03-22 13:48:18 -0700 | [diff] [blame] | 109 | void *result = endpoint->callbackLoop(); | 
|  | 110 | // Close now so that the HW resource is freed and we can open a new device. | 
|  | 111 | if (!endpoint->isConnected()) { | 
| Phil Burk | dd58292 | 2020-10-15 20:29:51 +0000 | [diff] [blame^] | 112 | ALOGD("%s() call safeReleaseCloseFromCallback()", __func__); | 
|  | 113 | // Release and close under a lock with no check for callback collisions. | 
|  | 114 | endpoint->getStreamInternal()->safeReleaseCloseFromCallback(); | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 115 | } | 
| Phil Burk | be64550 | 2018-03-22 13:48:18 -0700 | [diff] [blame] | 116 |  | 
|  | 117 | return result; | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 118 | } | 
|  | 119 |  | 
|  | 120 | aaudio_result_t aaudio::AAudioServiceEndpointShared::startSharingThread_l() { | 
|  | 121 | // Launch the callback loop thread. | 
|  | 122 | int64_t periodNanos = getStreamInternal()->getFramesPerBurst() | 
|  | 123 | * AAUDIO_NANOS_PER_SECOND | 
|  | 124 | / getSampleRate(); | 
|  | 125 | mCallbackEnabled.store(true); | 
| Phil Burk | dd58292 | 2020-10-15 20:29:51 +0000 | [diff] [blame^] | 126 | // Prevent this object from getting deleted before the thread has a chance to create | 
|  | 127 | // its strong pointer. Assume the thread will call decStrong(). | 
|  | 128 | this->incStrong(nullptr); | 
|  | 129 | aaudio_result_t result = getStreamInternal()->createThread_l(periodNanos, | 
|  | 130 | aaudio_endpoint_thread_proc, | 
|  | 131 | this); | 
| Phil Burk | be64550 | 2018-03-22 13:48:18 -0700 | [diff] [blame] | 132 | if (result != AAUDIO_OK) { | 
| Phil Burk | dd58292 | 2020-10-15 20:29:51 +0000 | [diff] [blame^] | 133 | this->decStrong(nullptr); // Because the thread won't do it. | 
| Phil Burk | be64550 | 2018-03-22 13:48:18 -0700 | [diff] [blame] | 134 | } | 
|  | 135 | return result; | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 136 | } | 
|  | 137 |  | 
|  | 138 | aaudio_result_t aaudio::AAudioServiceEndpointShared::stopSharingThread() { | 
|  | 139 | mCallbackEnabled.store(false); | 
|  | 140 | aaudio_result_t result = getStreamInternal()->joinThread(NULL); | 
|  | 141 | return result; | 
|  | 142 | } | 
|  | 143 |  | 
|  | 144 | aaudio_result_t AAudioServiceEndpointShared::startStream(sp<AAudioServiceStreamBase> sharedStream, | 
|  | 145 | audio_port_handle_t *clientHandle) { | 
|  | 146 | aaudio_result_t result = AAUDIO_OK; | 
| Phil Burk | b92c988 | 2017-09-15 11:39:15 -0700 | [diff] [blame] | 147 |  | 
|  | 148 | { | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 149 | std::lock_guard<std::mutex> lock(mLockStreams); | 
| Phil Burk | b92c988 | 2017-09-15 11:39:15 -0700 | [diff] [blame] | 150 | if (++mRunningStreamCount == 1) { // atomic | 
| Phil Burk | dd58292 | 2020-10-15 20:29:51 +0000 | [diff] [blame^] | 151 | result = getStreamInternal()->requestStart_l(); | 
| Phil Burk | b92c988 | 2017-09-15 11:39:15 -0700 | [diff] [blame] | 152 | if (result != AAUDIO_OK) { | 
|  | 153 | --mRunningStreamCount; | 
|  | 154 | } else { | 
|  | 155 | result = startSharingThread_l(); | 
|  | 156 | if (result != AAUDIO_OK) { | 
| Phil Burk | dd58292 | 2020-10-15 20:29:51 +0000 | [diff] [blame^] | 157 | getStreamInternal()->requestStop_l(); | 
| Phil Burk | b92c988 | 2017-09-15 11:39:15 -0700 | [diff] [blame] | 158 | --mRunningStreamCount; | 
|  | 159 | } | 
|  | 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 | if (result == AAUDIO_OK) { | 
| jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 165 | const audio_attributes_t attr = getAudioAttributesFrom(sharedStream.get()); | 
|  | 166 | result = getStreamInternal()->startClient( | 
|  | 167 | sharedStream->getAudioClient(), &attr, clientHandle); | 
| Phil Burk | b92c988 | 2017-09-15 11:39:15 -0700 | [diff] [blame] | 168 | if (result != AAUDIO_OK) { | 
|  | 169 | if (--mRunningStreamCount == 0) { // atomic | 
|  | 170 | stopSharingThread(); | 
| Phil Burk | dd58292 | 2020-10-15 20:29:51 +0000 | [diff] [blame^] | 171 | getStreamInternal()->requestStop_l(); | 
| Phil Burk | b92c988 | 2017-09-15 11:39:15 -0700 | [diff] [blame] | 172 | } | 
|  | 173 | } | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 174 | } | 
| Phil Burk | b92c988 | 2017-09-15 11:39:15 -0700 | [diff] [blame] | 175 |  | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 176 | return result; | 
|  | 177 | } | 
|  | 178 |  | 
|  | 179 | aaudio_result_t AAudioServiceEndpointShared::stopStream(sp<AAudioServiceStreamBase> sharedStream, | 
|  | 180 | audio_port_handle_t clientHandle) { | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 181 | // Ignore result. | 
|  | 182 | (void) getStreamInternal()->stopClient(clientHandle); | 
|  | 183 |  | 
|  | 184 | if (--mRunningStreamCount == 0) { // atomic | 
| Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 185 | stopSharingThread(); // the sharing thread locks mLockStreams | 
| Phil Burk | dd58292 | 2020-10-15 20:29:51 +0000 | [diff] [blame^] | 186 | getStreamInternal()->requestStop_l(); | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 187 | } | 
|  | 188 | return AAUDIO_OK; | 
|  | 189 | } | 
|  | 190 |  | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 191 | // Get timestamp that was written by the real-time service thread, eg. mixer. | 
|  | 192 | aaudio_result_t AAudioServiceEndpointShared::getFreeRunningPosition(int64_t *positionFrames, | 
|  | 193 | int64_t *timeNanos) { | 
| Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 194 | if (mAtomicEndpointTimestamp.isValid()) { | 
|  | 195 | Timestamp timestamp = mAtomicEndpointTimestamp.read(); | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 196 | *positionFrames = timestamp.getPosition(); | 
|  | 197 | *timeNanos = timestamp.getNanoseconds(); | 
|  | 198 | return AAUDIO_OK; | 
|  | 199 | } else { | 
|  | 200 | return AAUDIO_ERROR_UNAVAILABLE; | 
|  | 201 | } | 
|  | 202 | } | 
|  | 203 |  | 
|  | 204 | aaudio_result_t AAudioServiceEndpointShared::getTimestamp(int64_t *positionFrames, | 
|  | 205 | int64_t *timeNanos) { | 
| Phil Burk | 56d34f2 | 2018-10-11 13:30:56 -0700 | [diff] [blame] | 206 | aaudio_result_t result = mStreamInternal->getTimestamp(CLOCK_MONOTONIC, positionFrames, timeNanos); | 
|  | 207 | if (result == AAUDIO_ERROR_INVALID_STATE) { | 
|  | 208 | // getTimestamp() can return AAUDIO_ERROR_INVALID_STATE if the stream has | 
|  | 209 | // not completely started. This can cause a race condition that kills the | 
|  | 210 | // timestamp service thread.  So we reduce the error to a less serious one | 
|  | 211 | // that allows the timestamp thread to continue. | 
|  | 212 | result = AAUDIO_ERROR_UNAVAILABLE; | 
|  | 213 | } | 
|  | 214 | return result; | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 215 | } |