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(' '); |
| 50 | result << "\n"; |
| 51 | result << " Running Stream Count: " << mRunningStreamCount << "\n"; |
| 52 | |
| 53 | result << AAudioServiceEndpoint::dump(); |
| 54 | return result.str(); |
| 55 | } |
| 56 | |
| 57 | // Share an AudioStreamInternal. |
| 58 | aaudio_result_t AAudioServiceEndpointShared::open(const aaudio::AAudioStreamRequest &request) { |
| 59 | aaudio_result_t result = AAUDIO_OK; |
| 60 | const AAudioStreamConfiguration &configuration = request.getConstantConfiguration(); |
| 61 | |
| 62 | mRequestedDeviceId = configuration.getDeviceId(); |
| 63 | setDirection(configuration.getDirection()); |
| 64 | |
| 65 | AudioStreamBuilder builder; |
| 66 | builder.setSharingMode(AAUDIO_SHARING_MODE_EXCLUSIVE); |
| 67 | // Don't fall back to SHARED because that would cause recursion. |
| 68 | builder.setSharingModeMatchRequired(true); |
| 69 | builder.setDeviceId(mRequestedDeviceId); |
| 70 | builder.setFormat(configuration.getFormat()); |
| 71 | builder.setSampleRate(configuration.getSampleRate()); |
| 72 | builder.setSamplesPerFrame(configuration.getSamplesPerFrame()); |
| 73 | builder.setDirection(configuration.getDirection()); |
| 74 | builder.setBufferCapacity(DEFAULT_BUFFER_CAPACITY); |
| 75 | |
| 76 | result = mStreamInternal->open(builder); |
| 77 | |
| 78 | setSampleRate(mStreamInternal->getSampleRate()); |
| 79 | setSamplesPerFrame(mStreamInternal->getSamplesPerFrame()); |
| 80 | setDeviceId(mStreamInternal->getDeviceId()); |
| 81 | mFramesPerBurst = mStreamInternal->getFramesPerBurst(); |
| 82 | |
| 83 | return result; |
| 84 | } |
| 85 | |
| 86 | aaudio_result_t AAudioServiceEndpointShared::close() { |
| 87 | return getStreamInternal()->close(); |
| 88 | } |
| 89 | |
| 90 | // Glue between C and C++ callbacks. |
| 91 | static void *aaudio_endpoint_thread_proc(void *context) { |
| 92 | AAudioServiceEndpointShared *endpoint = (AAudioServiceEndpointShared *) context; |
| 93 | if (endpoint != NULL) { |
| 94 | return endpoint->callbackLoop(); |
| 95 | } else { |
| 96 | return NULL; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | aaudio_result_t aaudio::AAudioServiceEndpointShared::startSharingThread_l() { |
| 101 | // Launch the callback loop thread. |
| 102 | int64_t periodNanos = getStreamInternal()->getFramesPerBurst() |
| 103 | * AAUDIO_NANOS_PER_SECOND |
| 104 | / getSampleRate(); |
| 105 | mCallbackEnabled.store(true); |
| 106 | return getStreamInternal()->createThread(periodNanos, aaudio_endpoint_thread_proc, this); |
| 107 | } |
| 108 | |
| 109 | aaudio_result_t aaudio::AAudioServiceEndpointShared::stopSharingThread() { |
| 110 | mCallbackEnabled.store(false); |
| 111 | aaudio_result_t result = getStreamInternal()->joinThread(NULL); |
| 112 | return result; |
| 113 | } |
| 114 | |
| 115 | aaudio_result_t AAudioServiceEndpointShared::startStream(sp<AAudioServiceStreamBase> sharedStream, |
| 116 | audio_port_handle_t *clientHandle) { |
| 117 | aaudio_result_t result = AAUDIO_OK; |
| 118 | if (++mRunningStreamCount == 1) { |
| 119 | // TODO use real-time technique to avoid mutex, eg. atomic command FIFO |
| 120 | std::lock_guard<std::mutex> lock(mLockStreams); |
| 121 | result = getStreamInternal()->requestStart(); |
| 122 | startSharingThread_l(); |
| 123 | } |
| 124 | if (result == AAUDIO_OK) { |
| 125 | ALOGD("AAudioServiceEndpointShared::startStream() use shared stream client."); |
| 126 | result = getStreamInternal()->startClient(sharedStream->getAudioClient(), clientHandle); |
| 127 | } |
| 128 | return result; |
| 129 | } |
| 130 | |
| 131 | aaudio_result_t AAudioServiceEndpointShared::stopStream(sp<AAudioServiceStreamBase> sharedStream, |
| 132 | audio_port_handle_t clientHandle) { |
| 133 | // Don't lock here because the disconnectRegisteredStreams also uses the lock. |
| 134 | |
| 135 | // Ignore result. |
| 136 | (void) getStreamInternal()->stopClient(clientHandle); |
| 137 | |
| 138 | if (--mRunningStreamCount == 0) { // atomic |
| 139 | stopSharingThread(); |
| 140 | getStreamInternal()->requestStop(); |
| 141 | } |
| 142 | return AAUDIO_OK; |
| 143 | } |
| 144 | |
| 145 | |
| 146 | // Get timestamp that was written by the real-time service thread, eg. mixer. |
| 147 | aaudio_result_t AAudioServiceEndpointShared::getFreeRunningPosition(int64_t *positionFrames, |
| 148 | int64_t *timeNanos) { |
| 149 | if (mAtomicTimestamp.isValid()) { |
| 150 | Timestamp timestamp = mAtomicTimestamp.read(); |
| 151 | *positionFrames = timestamp.getPosition(); |
| 152 | *timeNanos = timestamp.getNanoseconds(); |
| 153 | return AAUDIO_OK; |
| 154 | } else { |
| 155 | return AAUDIO_ERROR_UNAVAILABLE; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | aaudio_result_t AAudioServiceEndpointShared::getTimestamp(int64_t *positionFrames, |
| 160 | int64_t *timeNanos) { |
| 161 | return mStreamInternal->getTimestamp(CLOCK_MONOTONIC, positionFrames, timeNanos); |
| 162 | } |