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