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 | |
| 63 | mRequestedDeviceId = configuration.getDeviceId(); |
| 64 | setDirection(configuration.getDirection()); |
| 65 | |
| 66 | AudioStreamBuilder builder; |
| 67 | builder.setSharingMode(AAUDIO_SHARING_MODE_EXCLUSIVE); |
| 68 | // Don't fall back to SHARED because that would cause recursion. |
| 69 | builder.setSharingModeMatchRequired(true); |
| 70 | builder.setDeviceId(mRequestedDeviceId); |
| 71 | builder.setFormat(configuration.getFormat()); |
| 72 | builder.setSampleRate(configuration.getSampleRate()); |
| 73 | builder.setSamplesPerFrame(configuration.getSamplesPerFrame()); |
| 74 | builder.setDirection(configuration.getDirection()); |
| 75 | builder.setBufferCapacity(DEFAULT_BUFFER_CAPACITY); |
| 76 | |
| 77 | result = mStreamInternal->open(builder); |
| 78 | |
| 79 | setSampleRate(mStreamInternal->getSampleRate()); |
| 80 | setSamplesPerFrame(mStreamInternal->getSamplesPerFrame()); |
| 81 | setDeviceId(mStreamInternal->getDeviceId()); |
| 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. |
| 92 | static void *aaudio_endpoint_thread_proc(void *context) { |
| 93 | AAudioServiceEndpointShared *endpoint = (AAudioServiceEndpointShared *) context; |
| 94 | if (endpoint != NULL) { |
Phil Burk | be0a5b6 | 2017-10-12 15:56:00 -0700 | [diff] [blame] | 95 | void *result = endpoint->callbackLoop(); |
| 96 | // Close now so that the HW resource is freed and we can open a new device. |
| 97 | if (!endpoint->isConnected()) { |
| 98 | endpoint->close(); |
| 99 | } |
| 100 | return result; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 101 | } else { |
| 102 | return NULL; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | aaudio_result_t aaudio::AAudioServiceEndpointShared::startSharingThread_l() { |
| 107 | // Launch the callback loop thread. |
| 108 | int64_t periodNanos = getStreamInternal()->getFramesPerBurst() |
| 109 | * AAUDIO_NANOS_PER_SECOND |
| 110 | / getSampleRate(); |
| 111 | mCallbackEnabled.store(true); |
| 112 | return getStreamInternal()->createThread(periodNanos, aaudio_endpoint_thread_proc, this); |
| 113 | } |
| 114 | |
| 115 | aaudio_result_t aaudio::AAudioServiceEndpointShared::stopSharingThread() { |
| 116 | mCallbackEnabled.store(false); |
| 117 | aaudio_result_t result = getStreamInternal()->joinThread(NULL); |
| 118 | return result; |
| 119 | } |
| 120 | |
| 121 | aaudio_result_t AAudioServiceEndpointShared::startStream(sp<AAudioServiceStreamBase> sharedStream, |
| 122 | audio_port_handle_t *clientHandle) { |
| 123 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | b92c988 | 2017-09-15 11:39:15 -0700 | [diff] [blame] | 124 | |
| 125 | { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 126 | std::lock_guard<std::mutex> lock(mLockStreams); |
Phil Burk | b92c988 | 2017-09-15 11:39:15 -0700 | [diff] [blame] | 127 | if (++mRunningStreamCount == 1) { // atomic |
| 128 | result = getStreamInternal()->requestStart(); |
| 129 | if (result != AAUDIO_OK) { |
| 130 | --mRunningStreamCount; |
| 131 | } else { |
| 132 | result = startSharingThread_l(); |
| 133 | if (result != AAUDIO_OK) { |
| 134 | getStreamInternal()->requestStop(); |
| 135 | --mRunningStreamCount; |
| 136 | } |
| 137 | } |
| 138 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 139 | } |
Phil Burk | b92c988 | 2017-09-15 11:39:15 -0700 | [diff] [blame] | 140 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 141 | if (result == AAUDIO_OK) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 142 | result = getStreamInternal()->startClient(sharedStream->getAudioClient(), clientHandle); |
Phil Burk | b92c988 | 2017-09-15 11:39:15 -0700 | [diff] [blame] | 143 | if (result != AAUDIO_OK) { |
| 144 | if (--mRunningStreamCount == 0) { // atomic |
| 145 | stopSharingThread(); |
| 146 | getStreamInternal()->requestStop(); |
| 147 | } |
| 148 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 149 | } |
Phil Burk | b92c988 | 2017-09-15 11:39:15 -0700 | [diff] [blame] | 150 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 151 | return result; |
| 152 | } |
| 153 | |
| 154 | aaudio_result_t AAudioServiceEndpointShared::stopStream(sp<AAudioServiceStreamBase> sharedStream, |
| 155 | audio_port_handle_t clientHandle) { |
| 156 | // Don't lock here because the disconnectRegisteredStreams also uses the lock. |
| 157 | |
| 158 | // Ignore result. |
| 159 | (void) getStreamInternal()->stopClient(clientHandle); |
| 160 | |
| 161 | if (--mRunningStreamCount == 0) { // atomic |
| 162 | stopSharingThread(); |
| 163 | getStreamInternal()->requestStop(); |
| 164 | } |
| 165 | return AAUDIO_OK; |
| 166 | } |
| 167 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 168 | // Get timestamp that was written by the real-time service thread, eg. mixer. |
| 169 | aaudio_result_t AAudioServiceEndpointShared::getFreeRunningPosition(int64_t *positionFrames, |
| 170 | int64_t *timeNanos) { |
| 171 | if (mAtomicTimestamp.isValid()) { |
| 172 | Timestamp timestamp = mAtomicTimestamp.read(); |
| 173 | *positionFrames = timestamp.getPosition(); |
| 174 | *timeNanos = timestamp.getNanoseconds(); |
| 175 | return AAUDIO_OK; |
| 176 | } else { |
| 177 | return AAUDIO_ERROR_UNAVAILABLE; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | aaudio_result_t AAudioServiceEndpointShared::getTimestamp(int64_t *positionFrames, |
| 182 | int64_t *timeNanos) { |
| 183 | return mStreamInternal->getTimestamp(CLOCK_MONOTONIC, positionFrames, timeNanos); |
| 184 | } |