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