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