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 | #define LOG_TAG "AAudioServiceEndpointMMAP" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <algorithm> |
| 22 | #include <assert.h> |
| 23 | #include <map> |
| 24 | #include <mutex> |
| 25 | #include <sstream> |
Phil Burk | a77869d | 2020-05-07 10:39:47 -0700 | [diff] [blame] | 26 | #include <thread> |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 27 | #include <utils/Singleton.h> |
| 28 | #include <vector> |
| 29 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 30 | #include "AAudioEndpointManager.h" |
| 31 | #include "AAudioServiceEndpoint.h" |
| 32 | |
| 33 | #include "core/AudioStreamBuilder.h" |
| 34 | #include "AAudioServiceEndpoint.h" |
| 35 | #include "AAudioServiceStreamShared.h" |
| 36 | #include "AAudioServiceEndpointPlay.h" |
| 37 | #include "AAudioServiceEndpointMMAP.h" |
| 38 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 39 | #define AAUDIO_BUFFER_CAPACITY_MIN 4 * 512 |
| 40 | #define AAUDIO_SAMPLE_RATE_DEFAULT 48000 |
| 41 | |
| 42 | // This is an estimate of the time difference between the HW and the MMAP time. |
| 43 | // TODO Get presentation timestamps from the HAL instead of using these estimates. |
| 44 | #define OUTPUT_ESTIMATED_HARDWARE_OFFSET_NANOS (3 * AAUDIO_NANOS_PER_MILLISECOND) |
| 45 | #define INPUT_ESTIMATED_HARDWARE_OFFSET_NANOS (-1 * AAUDIO_NANOS_PER_MILLISECOND) |
| 46 | |
| 47 | using namespace android; // TODO just import names needed |
| 48 | using namespace aaudio; // TODO just import names needed |
| 49 | |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 50 | AAudioServiceEndpointMMAP::AAudioServiceEndpointMMAP(AAudioService &audioService) |
| 51 | : mMmapStream(nullptr) |
| 52 | , mAAudioService(audioService) {} |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 53 | |
| 54 | AAudioServiceEndpointMMAP::~AAudioServiceEndpointMMAP() {} |
| 55 | |
| 56 | std::string AAudioServiceEndpointMMAP::dump() const { |
| 57 | std::stringstream result; |
| 58 | |
| 59 | result << " MMAP: framesTransferred = " << mFramesTransferred.get(); |
| 60 | result << ", HW nanos = " << mHardwareTimeOffsetNanos; |
| 61 | result << ", port handle = " << mPortHandle; |
| 62 | result << ", audio data FD = " << mAudioDataFileDescriptor; |
| 63 | result << "\n"; |
| 64 | |
| 65 | result << " HW Offset Micros: " << |
| 66 | (getHardwareTimeOffsetNanos() |
| 67 | / AAUDIO_NANOS_PER_MICROSECOND) << "\n"; |
| 68 | |
| 69 | result << AAudioServiceEndpoint::dump(); |
| 70 | return result.str(); |
| 71 | } |
| 72 | |
| 73 | aaudio_result_t AAudioServiceEndpointMMAP::open(const aaudio::AAudioStreamRequest &request) { |
| 74 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 75 | audio_config_base_t config; |
| 76 | audio_port_handle_t deviceId; |
| 77 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 78 | copyFrom(request.getConstantConfiguration()); |
| 79 | |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 80 | const audio_attributes_t attributes = getAudioAttributesFrom(this); |
Phil Burk | a62fb95 | 2018-01-16 12:44:06 -0800 | [diff] [blame] | 81 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 82 | mMmapClient.clientUid = request.getUserId(); |
| 83 | mMmapClient.clientPid = request.getProcessId(); |
| 84 | mMmapClient.packageName.setTo(String16("")); |
| 85 | |
| 86 | mRequestedDeviceId = deviceId = getDeviceId(); |
| 87 | |
| 88 | // Fill in config |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 89 | audio_format_t audioFormat = getFormat(); |
| 90 | if (audioFormat == AUDIO_FORMAT_DEFAULT || audioFormat == AUDIO_FORMAT_PCM_FLOAT) { |
| 91 | audioFormat = AUDIO_FORMAT_PCM_16_BIT; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 92 | } |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 93 | config.format = audioFormat; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 94 | |
| 95 | int32_t aaudioSampleRate = getSampleRate(); |
| 96 | if (aaudioSampleRate == AAUDIO_UNSPECIFIED) { |
| 97 | aaudioSampleRate = AAUDIO_SAMPLE_RATE_DEFAULT; |
| 98 | } |
| 99 | config.sample_rate = aaudioSampleRate; |
| 100 | |
| 101 | int32_t aaudioSamplesPerFrame = getSamplesPerFrame(); |
| 102 | |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 103 | const aaudio_direction_t direction = getDirection(); |
| 104 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 105 | if (direction == AAUDIO_DIRECTION_OUTPUT) { |
| 106 | config.channel_mask = (aaudioSamplesPerFrame == AAUDIO_UNSPECIFIED) |
| 107 | ? AUDIO_CHANNEL_OUT_STEREO |
| 108 | : audio_channel_out_mask_from_count(aaudioSamplesPerFrame); |
| 109 | mHardwareTimeOffsetNanos = OUTPUT_ESTIMATED_HARDWARE_OFFSET_NANOS; // frames at DAC later |
| 110 | |
| 111 | } else if (direction == AAUDIO_DIRECTION_INPUT) { |
| 112 | config.channel_mask = (aaudioSamplesPerFrame == AAUDIO_UNSPECIFIED) |
| 113 | ? AUDIO_CHANNEL_IN_STEREO |
| 114 | : audio_channel_in_mask_from_count(aaudioSamplesPerFrame); |
| 115 | mHardwareTimeOffsetNanos = INPUT_ESTIMATED_HARDWARE_OFFSET_NANOS; // frames at ADC earlier |
| 116 | |
| 117 | } else { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 118 | ALOGE("%s() invalid direction = %d", __func__, direction); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 119 | return AAUDIO_ERROR_ILLEGAL_ARGUMENT; |
| 120 | } |
| 121 | |
| 122 | MmapStreamInterface::stream_direction_t streamDirection = |
| 123 | (direction == AAUDIO_DIRECTION_OUTPUT) |
| 124 | ? MmapStreamInterface::DIRECTION_OUTPUT |
| 125 | : MmapStreamInterface::DIRECTION_INPUT; |
| 126 | |
Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 127 | aaudio_session_id_t requestedSessionId = getSessionId(); |
| 128 | audio_session_t sessionId = AAudioConvert_aaudioToAndroidSessionId(requestedSessionId); |
| 129 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 130 | // Open HAL stream. Set mMmapStream |
| 131 | status_t status = MmapStreamInterface::openMmapStream(streamDirection, |
| 132 | &attributes, |
| 133 | &config, |
| 134 | mMmapClient, |
| 135 | &deviceId, |
Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 136 | &sessionId, |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 137 | this, // callback |
| 138 | mMmapStream, |
| 139 | &mPortHandle); |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 140 | ALOGD("%s() mMapClient.uid = %d, pid = %d => portHandle = %d\n", |
| 141 | __func__, mMmapClient.clientUid, mMmapClient.clientPid, mPortHandle); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 142 | if (status != OK) { |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 143 | // This can happen if the resource is busy or the config does |
| 144 | // not match the hardware. |
| 145 | ALOGD("%s() - openMmapStream() returned status %d", __func__, status); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 146 | return AAUDIO_ERROR_UNAVAILABLE; |
| 147 | } |
| 148 | |
| 149 | if (deviceId == AAUDIO_UNSPECIFIED) { |
Phil Burk | a3901e9 | 2018-10-08 13:54:38 -0700 | [diff] [blame] | 150 | ALOGW("%s() - openMmapStream() failed to set deviceId", __func__); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 151 | } |
| 152 | setDeviceId(deviceId); |
| 153 | |
Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 154 | if (sessionId == AUDIO_SESSION_ALLOCATE) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 155 | ALOGW("%s() - openMmapStream() failed to set sessionId", __func__); |
Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | aaudio_session_id_t actualSessionId = |
| 159 | (requestedSessionId == AAUDIO_SESSION_ID_NONE) |
| 160 | ? AAUDIO_SESSION_ID_NONE |
| 161 | : (aaudio_session_id_t) sessionId; |
| 162 | setSessionId(actualSessionId); |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 163 | ALOGD("%s() deviceId = %d, sessionId = %d", __func__, getDeviceId(), getSessionId()); |
Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 164 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 165 | // Create MMAP/NOIRQ buffer. |
| 166 | int32_t minSizeFrames = getBufferCapacity(); |
| 167 | if (minSizeFrames <= 0) { // zero will get rejected |
| 168 | minSizeFrames = AAUDIO_BUFFER_CAPACITY_MIN; |
| 169 | } |
| 170 | status = mMmapStream->createMmapBuffer(minSizeFrames, &mMmapBufferinfo); |
Kevin Rocard | 734334f | 2018-07-12 19:37:41 -0700 | [diff] [blame] | 171 | bool isBufferShareable = mMmapBufferinfo.flags & AUDIO_MMAP_APPLICATION_SHAREABLE; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 172 | if (status != OK) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 173 | ALOGE("%s() - createMmapBuffer() failed with status %d %s", |
| 174 | __func__, status, strerror(-status)); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 175 | result = AAUDIO_ERROR_UNAVAILABLE; |
| 176 | goto error; |
| 177 | } else { |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 178 | ALOGD("%s() createMmapBuffer() buffer_size = %d fr, burst_size %d fr" |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 179 | ", Sharable FD: %s", |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 180 | __func__, |
Kevin Rocard | 734334f | 2018-07-12 19:37:41 -0700 | [diff] [blame] | 181 | mMmapBufferinfo.buffer_size_frames, |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 182 | mMmapBufferinfo.burst_size_frames, |
Kevin Rocard | 734334f | 2018-07-12 19:37:41 -0700 | [diff] [blame] | 183 | isBufferShareable ? "Yes" : "No"); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | setBufferCapacity(mMmapBufferinfo.buffer_size_frames); |
Kevin Rocard | 734334f | 2018-07-12 19:37:41 -0700 | [diff] [blame] | 187 | if (!isBufferShareable) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 188 | // Exclusive mode can only be used by the service because the FD cannot be shared. |
| 189 | uid_t audioServiceUid = getuid(); |
| 190 | if ((mMmapClient.clientUid != audioServiceUid) && |
| 191 | getSharingMode() == AAUDIO_SHARING_MODE_EXCLUSIVE) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 192 | ALOGW("%s() - exclusive FD cannot be used by client", __func__); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 193 | result = AAUDIO_ERROR_UNAVAILABLE; |
| 194 | goto error; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // Get information about the stream and pass it back to the caller. |
| 199 | setSamplesPerFrame((direction == AAUDIO_DIRECTION_OUTPUT) |
| 200 | ? audio_channel_count_from_out_mask(config.channel_mask) |
| 201 | : audio_channel_count_from_in_mask(config.channel_mask)); |
| 202 | |
| 203 | // AAudio creates a copy of this FD and retains ownership of the copy. |
| 204 | // Assume that AudioFlinger will close the original shared_memory_fd. |
| 205 | mAudioDataFileDescriptor.reset(dup(mMmapBufferinfo.shared_memory_fd)); |
| 206 | if (mAudioDataFileDescriptor.get() == -1) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 207 | ALOGE("%s() - could not dup shared_memory_fd", __func__); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 208 | result = AAUDIO_ERROR_INTERNAL; |
| 209 | goto error; |
| 210 | } |
| 211 | mFramesPerBurst = mMmapBufferinfo.burst_size_frames; |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 212 | setFormat(config.format); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 213 | setSampleRate(config.sample_rate); |
| 214 | |
Phil Burk | 3c4e6b5 | 2019-01-22 15:53:36 -0800 | [diff] [blame] | 215 | ALOGD("%s() actual rate = %d, channels = %d" |
| 216 | ", deviceId = %d, capacity = %d\n", |
| 217 | __func__, getSampleRate(), getSamplesPerFrame(), deviceId, getBufferCapacity()); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 218 | |
Phil Burk | 3c4e6b5 | 2019-01-22 15:53:36 -0800 | [diff] [blame] | 219 | ALOGD("%s() format = 0x%08x, frame size = %d, burst size = %d", |
| 220 | __func__, getFormat(), calculateBytesPerFrame(), mFramesPerBurst); |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 221 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 222 | return result; |
| 223 | |
| 224 | error: |
| 225 | close(); |
| 226 | return result; |
| 227 | } |
| 228 | |
Phil Burk | 320910f | 2020-08-12 14:29:10 +0000 | [diff] [blame] | 229 | void AAudioServiceEndpointMMAP::close() { |
Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 230 | if (mMmapStream != nullptr) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 231 | // Needs to be explicitly cleared or CTS will fail but it is not clear why. |
| 232 | mMmapStream.clear(); |
| 233 | // Apparently the above close is asynchronous. An attempt to open a new device |
| 234 | // right after a close can fail. Also some callbacks may still be in flight! |
| 235 | // FIXME Make closing synchronous. |
| 236 | AudioClock::sleepForNanos(100 * AAUDIO_NANOS_PER_MILLISECOND); |
| 237 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | aaudio_result_t AAudioServiceEndpointMMAP::startStream(sp<AAudioServiceStreamBase> stream, |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 241 | audio_port_handle_t *clientHandle __unused) { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 242 | // Start the client on behalf of the AAudio service. |
| 243 | // Use the port handle that was provided by openMmapStream(). |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 244 | audio_port_handle_t tempHandle = mPortHandle; |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 245 | audio_attributes_t attr = {}; |
| 246 | if (stream != nullptr) { |
| 247 | attr = getAudioAttributesFrom(stream.get()); |
| 248 | } |
| 249 | aaudio_result_t result = startClient( |
| 250 | mMmapClient, stream == nullptr ? nullptr : &attr, &tempHandle); |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 251 | // When AudioFlinger is passed a valid port handle then it should not change it. |
| 252 | LOG_ALWAYS_FATAL_IF(tempHandle != mPortHandle, |
| 253 | "%s() port handle not expected to change from %d to %d", |
| 254 | __func__, mPortHandle, tempHandle); |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 255 | ALOGV("%s() mPortHandle = %d", __func__, mPortHandle); |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 256 | return result; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | aaudio_result_t AAudioServiceEndpointMMAP::stopStream(sp<AAudioServiceStreamBase> stream, |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 260 | audio_port_handle_t clientHandle __unused) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 261 | mFramesTransferred.reset32(); |
Phil Burk | 73af62a | 2017-10-26 12:11:47 -0700 | [diff] [blame] | 262 | |
| 263 | // Round 64-bit counter up to a multiple of the buffer capacity. |
| 264 | // This is required because the 64-bit counter is used as an index |
| 265 | // into a circular buffer and the actual HW position is reset to zero |
| 266 | // when the stream is stopped. |
| 267 | mFramesTransferred.roundUp64(getBufferCapacity()); |
| 268 | |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 269 | // Use the port handle that was provided by openMmapStream(). |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 270 | ALOGV("%s() mPortHandle = %d", __func__, mPortHandle); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 271 | return stopClient(mPortHandle); |
| 272 | } |
| 273 | |
| 274 | aaudio_result_t AAudioServiceEndpointMMAP::startClient(const android::AudioClient& client, |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 275 | const audio_attributes_t *attr, |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 276 | audio_port_handle_t *clientHandle) { |
| 277 | if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL; |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 278 | status_t status = mMmapStream->start(client, attr, clientHandle); |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 279 | return AAudioConvert_androidToAAudioResult(status); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | aaudio_result_t AAudioServiceEndpointMMAP::stopClient(audio_port_handle_t clientHandle) { |
| 283 | if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL; |
| 284 | aaudio_result_t result = AAudioConvert_androidToAAudioResult(mMmapStream->stop(clientHandle)); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 285 | return result; |
| 286 | } |
| 287 | |
| 288 | // Get free-running DSP or DMA hardware position from the HAL. |
| 289 | aaudio_result_t AAudioServiceEndpointMMAP::getFreeRunningPosition(int64_t *positionFrames, |
| 290 | int64_t *timeNanos) { |
| 291 | struct audio_mmap_position position; |
| 292 | if (mMmapStream == nullptr) { |
| 293 | return AAUDIO_ERROR_NULL; |
| 294 | } |
| 295 | status_t status = mMmapStream->getMmapPosition(&position); |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 296 | ALOGV("%s() status= %d, pos = %d, nanos = %lld\n", |
| 297 | __func__, status, position.position_frames, (long long) position.time_nanoseconds); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 298 | aaudio_result_t result = AAudioConvert_androidToAAudioResult(status); |
| 299 | if (result == AAUDIO_ERROR_UNAVAILABLE) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 300 | ALOGW("%s(): getMmapPosition() has no position data available", __func__); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 301 | } else if (result != AAUDIO_OK) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 302 | ALOGE("%s(): getMmapPosition() returned status %d", __func__, status); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 303 | } else { |
| 304 | // Convert 32-bit position to 64-bit position. |
| 305 | mFramesTransferred.update32(position.position_frames); |
| 306 | *positionFrames = mFramesTransferred.get(); |
| 307 | *timeNanos = position.time_nanoseconds; |
| 308 | } |
| 309 | return result; |
| 310 | } |
| 311 | |
| 312 | aaudio_result_t AAudioServiceEndpointMMAP::getTimestamp(int64_t *positionFrames, |
| 313 | int64_t *timeNanos) { |
| 314 | return 0; // TODO |
| 315 | } |
| 316 | |
Phil Burk | a77869d | 2020-05-07 10:39:47 -0700 | [diff] [blame] | 317 | // This is called by onTearDown() in a separate thread to avoid deadlocks. |
| 318 | void AAudioServiceEndpointMMAP::handleTearDownAsync(audio_port_handle_t portHandle) { |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 319 | // Are we tearing down the EXCLUSIVE MMAP stream? |
| 320 | if (isStreamRegistered(portHandle)) { |
| 321 | ALOGD("%s(%d) tearing down this entire MMAP endpoint", __func__, portHandle); |
| 322 | disconnectRegisteredStreams(); |
| 323 | } else { |
| 324 | // Must be a SHARED stream? |
| 325 | ALOGD("%s(%d) disconnect a specific stream", __func__, portHandle); |
| 326 | aaudio_result_t result = mAAudioService.disconnectStreamByPortHandle(portHandle); |
| 327 | ALOGD("%s(%d) disconnectStreamByPortHandle returned %d", __func__, portHandle, result); |
| 328 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 329 | }; |
| 330 | |
Phil Burk | a77869d | 2020-05-07 10:39:47 -0700 | [diff] [blame] | 331 | // This is called by AudioFlinger when it wants to destroy a stream. |
| 332 | void AAudioServiceEndpointMMAP::onTearDown(audio_port_handle_t portHandle) { |
| 333 | ALOGD("%s(portHandle = %d) called", __func__, portHandle); |
| 334 | std::thread asyncTask(&AAudioServiceEndpointMMAP::handleTearDownAsync, this, portHandle); |
| 335 | asyncTask.detach(); |
| 336 | } |
| 337 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 338 | void AAudioServiceEndpointMMAP::onVolumeChanged(audio_channel_mask_t channels, |
| 339 | android::Vector<float> values) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 340 | // TODO Do we really need a different volume for each channel? |
| 341 | // We get called with an array filled with a single value! |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 342 | float volume = values[0]; |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 343 | ALOGD("%s() volume[0] = %f", __func__, volume); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 344 | std::lock_guard<std::mutex> lock(mLockStreams); |
Chih-Hung Hsieh | 3ef324d | 2018-12-11 11:48:12 -0800 | [diff] [blame] | 345 | for(const auto& stream : mRegisteredStreams) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 346 | stream->onVolumeChanged(volume); |
| 347 | } |
| 348 | }; |
| 349 | |
Phil Burk | a77869d | 2020-05-07 10:39:47 -0700 | [diff] [blame] | 350 | void AAudioServiceEndpointMMAP::onRoutingChanged(audio_port_handle_t portHandle) { |
| 351 | const int32_t deviceId = static_cast<int32_t>(portHandle); |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 352 | ALOGD("%s() called with dev %d, old = %d", __func__, deviceId, getDeviceId()); |
Phil Burk | a77869d | 2020-05-07 10:39:47 -0700 | [diff] [blame] | 353 | if (getDeviceId() != deviceId) { |
| 354 | if (getDeviceId() != AUDIO_PORT_HANDLE_NONE) { |
| 355 | std::thread asyncTask([this, deviceId]() { |
| 356 | disconnectRegisteredStreams(); |
| 357 | setDeviceId(deviceId); |
| 358 | }); |
| 359 | asyncTask.detach(); |
| 360 | } else { |
| 361 | setDeviceId(deviceId); |
| 362 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 363 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 364 | }; |
| 365 | |
| 366 | /** |
| 367 | * Get an immutable description of the data queue from the HAL. |
| 368 | */ |
| 369 | aaudio_result_t AAudioServiceEndpointMMAP::getDownDataDescription(AudioEndpointParcelable &parcelable) |
| 370 | { |
| 371 | // Gather information on the data queue based on HAL info. |
| 372 | int32_t bytesPerFrame = calculateBytesPerFrame(); |
| 373 | int32_t capacityInBytes = getBufferCapacity() * bytesPerFrame; |
| 374 | int fdIndex = parcelable.addFileDescriptor(mAudioDataFileDescriptor, capacityInBytes); |
| 375 | parcelable.mDownDataQueueParcelable.setupMemory(fdIndex, 0, capacityInBytes); |
| 376 | parcelable.mDownDataQueueParcelable.setBytesPerFrame(bytesPerFrame); |
| 377 | parcelable.mDownDataQueueParcelable.setFramesPerBurst(mFramesPerBurst); |
| 378 | parcelable.mDownDataQueueParcelable.setCapacityInFrames(getBufferCapacity()); |
| 379 | return AAUDIO_OK; |
| 380 | } |
jiabin | b7d8c5a | 2020-08-26 17:24:52 -0700 | [diff] [blame] | 381 | |
| 382 | aaudio_result_t AAudioServiceEndpointMMAP::getExternalPosition(uint64_t *positionFrames, |
| 383 | int64_t *timeNanos) |
| 384 | { |
| 385 | if (!mExternalPositionSupported) { |
| 386 | return AAUDIO_ERROR_INVALID_STATE; |
| 387 | } |
| 388 | status_t status = mMmapStream->getExternalPosition(positionFrames, timeNanos); |
| 389 | if (status == INVALID_OPERATION) { |
| 390 | // getExternalPosition is not supported. Set mExternalPositionSupported as false |
| 391 | // so that the call will not go to the HAL next time. |
| 392 | mExternalPositionSupported = false; |
| 393 | } |
| 394 | return AAudioConvert_androidToAAudioResult(status); |
| 395 | } |