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 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 54 | std::string AAudioServiceEndpointMMAP::dump() const { |
| 55 | std::stringstream result; |
| 56 | |
| 57 | result << " MMAP: framesTransferred = " << mFramesTransferred.get(); |
| 58 | result << ", HW nanos = " << mHardwareTimeOffsetNanos; |
| 59 | result << ", port handle = " << mPortHandle; |
| 60 | result << ", audio data FD = " << mAudioDataFileDescriptor; |
| 61 | result << "\n"; |
| 62 | |
| 63 | result << " HW Offset Micros: " << |
| 64 | (getHardwareTimeOffsetNanos() |
| 65 | / AAUDIO_NANOS_PER_MICROSECOND) << "\n"; |
| 66 | |
| 67 | result << AAudioServiceEndpoint::dump(); |
| 68 | return result.str(); |
| 69 | } |
| 70 | |
| 71 | aaudio_result_t AAudioServiceEndpointMMAP::open(const aaudio::AAudioStreamRequest &request) { |
| 72 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 73 | copyFrom(request.getConstantConfiguration()); |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 74 | mMmapClient.attributionSource = request.getAttributionSource(); |
| 75 | // TODO b/182392769: use attribution source util |
| 76 | mMmapClient.attributionSource.uid = VALUE_OR_FATAL( |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 77 | legacy2aidl_uid_t_int32_t(IPCThreadState::self()->getCallingUid())); |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 78 | mMmapClient.attributionSource.pid = VALUE_OR_FATAL( |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 79 | legacy2aidl_pid_t_int32_t(IPCThreadState::self()->getCallingPid())); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 80 | |
Phil Burk | 04e805b | 2018-03-27 09:13:53 -0700 | [diff] [blame] | 81 | audio_format_t audioFormat = getFormat(); |
| 82 | |
millerliang | a75a83f | 2021-04-30 17:43:00 +0800 | [diff] [blame] | 83 | // FLOAT is not directly supported by the HAL so ask for a 32-bit. |
| 84 | if (audioFormat == AUDIO_FORMAT_PCM_FLOAT) { |
Phil Burk | 04e805b | 2018-03-27 09:13:53 -0700 | [diff] [blame] | 85 | // TODO remove these logs when finished debugging. |
millerliang | a75a83f | 2021-04-30 17:43:00 +0800 | [diff] [blame] | 86 | ALOGD("%s() change format from %d to 32_BIT", __func__, audioFormat); |
| 87 | audioFormat = AUDIO_FORMAT_PCM_32_BIT; |
Phil Burk | 04e805b | 2018-03-27 09:13:53 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | result = openWithFormat(audioFormat); |
| 91 | if (result == AAUDIO_OK) return result; |
| 92 | |
millerliang | a75a83f | 2021-04-30 17:43:00 +0800 | [diff] [blame] | 93 | if (result == AAUDIO_ERROR_UNAVAILABLE && audioFormat == AUDIO_FORMAT_PCM_32_BIT) { |
| 94 | ALOGD("%s() 32_BIT failed, perhaps due to format. Try again with 24_BIT_PACKED", __func__); |
| 95 | audioFormat = AUDIO_FORMAT_PCM_24_BIT_PACKED; |
| 96 | result = openWithFormat(audioFormat); |
| 97 | } |
| 98 | if (result == AAUDIO_OK) return result; |
| 99 | |
Phil Burk | 04e805b | 2018-03-27 09:13:53 -0700 | [diff] [blame] | 100 | // TODO The HAL and AudioFlinger should be recommending a format if the open fails. |
| 101 | // But that recommendation is not propagating back from the HAL. |
| 102 | // So for now just try something very likely to work. |
| 103 | if (result == AAUDIO_ERROR_UNAVAILABLE && audioFormat == AUDIO_FORMAT_PCM_24_BIT_PACKED) { |
| 104 | ALOGD("%s() 24_BIT failed, perhaps due to format. Try again with 16_BIT", __func__); |
| 105 | audioFormat = AUDIO_FORMAT_PCM_16_BIT; |
| 106 | result = openWithFormat(audioFormat); |
| 107 | } |
| 108 | return result; |
| 109 | } |
| 110 | |
| 111 | aaudio_result_t AAudioServiceEndpointMMAP::openWithFormat(audio_format_t audioFormat) { |
| 112 | aaudio_result_t result = AAUDIO_OK; |
| 113 | audio_config_base_t config; |
| 114 | audio_port_handle_t deviceId; |
| 115 | |
| 116 | const audio_attributes_t attributes = getAudioAttributesFrom(this); |
| 117 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 118 | mRequestedDeviceId = deviceId = getDeviceId(); |
| 119 | |
| 120 | // Fill in config |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 121 | config.format = audioFormat; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 122 | |
| 123 | int32_t aaudioSampleRate = getSampleRate(); |
| 124 | if (aaudioSampleRate == AAUDIO_UNSPECIFIED) { |
| 125 | aaudioSampleRate = AAUDIO_SAMPLE_RATE_DEFAULT; |
| 126 | } |
| 127 | config.sample_rate = aaudioSampleRate; |
| 128 | |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 129 | const aaudio_direction_t direction = getDirection(); |
| 130 | |
jiabin | a909409 | 2021-06-28 20:36:45 +0000 | [diff] [blame] | 131 | config.channel_mask = AAudio_getChannelMaskForOpen( |
| 132 | getChannelMask(), getSamplesPerFrame(), direction == AAUDIO_DIRECTION_INPUT); |
| 133 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 134 | if (direction == AAUDIO_DIRECTION_OUTPUT) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 135 | mHardwareTimeOffsetNanos = OUTPUT_ESTIMATED_HARDWARE_OFFSET_NANOS; // frames at DAC later |
| 136 | |
| 137 | } else if (direction == AAUDIO_DIRECTION_INPUT) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 138 | mHardwareTimeOffsetNanos = INPUT_ESTIMATED_HARDWARE_OFFSET_NANOS; // frames at ADC earlier |
| 139 | |
| 140 | } else { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 141 | ALOGE("%s() invalid direction = %d", __func__, direction); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 142 | return AAUDIO_ERROR_ILLEGAL_ARGUMENT; |
| 143 | } |
| 144 | |
| 145 | MmapStreamInterface::stream_direction_t streamDirection = |
| 146 | (direction == AAUDIO_DIRECTION_OUTPUT) |
| 147 | ? MmapStreamInterface::DIRECTION_OUTPUT |
| 148 | : MmapStreamInterface::DIRECTION_INPUT; |
| 149 | |
Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 150 | aaudio_session_id_t requestedSessionId = getSessionId(); |
| 151 | audio_session_t sessionId = AAudioConvert_aaudioToAndroidSessionId(requestedSessionId); |
| 152 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 153 | // Open HAL stream. Set mMmapStream |
| 154 | status_t status = MmapStreamInterface::openMmapStream(streamDirection, |
| 155 | &attributes, |
| 156 | &config, |
| 157 | mMmapClient, |
| 158 | &deviceId, |
Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 159 | &sessionId, |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 160 | this, // callback |
| 161 | mMmapStream, |
| 162 | &mPortHandle); |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 163 | ALOGD("%s() mMapClient.attributionSource = %s => portHandle = %d\n", |
| 164 | __func__, mMmapClient.attributionSource.toString().c_str(), mPortHandle); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 165 | if (status != OK) { |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 166 | // This can happen if the resource is busy or the config does |
| 167 | // not match the hardware. |
| 168 | ALOGD("%s() - openMmapStream() returned status %d", __func__, status); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 169 | return AAUDIO_ERROR_UNAVAILABLE; |
| 170 | } |
| 171 | |
| 172 | if (deviceId == AAUDIO_UNSPECIFIED) { |
Phil Burk | a3901e9 | 2018-10-08 13:54:38 -0700 | [diff] [blame] | 173 | ALOGW("%s() - openMmapStream() failed to set deviceId", __func__); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 174 | } |
| 175 | setDeviceId(deviceId); |
| 176 | |
Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 177 | if (sessionId == AUDIO_SESSION_ALLOCATE) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 178 | ALOGW("%s() - openMmapStream() failed to set sessionId", __func__); |
Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | aaudio_session_id_t actualSessionId = |
| 182 | (requestedSessionId == AAUDIO_SESSION_ID_NONE) |
| 183 | ? AAUDIO_SESSION_ID_NONE |
| 184 | : (aaudio_session_id_t) sessionId; |
| 185 | setSessionId(actualSessionId); |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 186 | ALOGD("%s() deviceId = %d, sessionId = %d", __func__, getDeviceId(), getSessionId()); |
Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 187 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 188 | // Create MMAP/NOIRQ buffer. |
| 189 | int32_t minSizeFrames = getBufferCapacity(); |
| 190 | if (minSizeFrames <= 0) { // zero will get rejected |
| 191 | minSizeFrames = AAUDIO_BUFFER_CAPACITY_MIN; |
| 192 | } |
| 193 | status = mMmapStream->createMmapBuffer(minSizeFrames, &mMmapBufferinfo); |
Kevin Rocard | 734334f | 2018-07-12 19:37:41 -0700 | [diff] [blame] | 194 | bool isBufferShareable = mMmapBufferinfo.flags & AUDIO_MMAP_APPLICATION_SHAREABLE; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 195 | if (status != OK) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 196 | ALOGE("%s() - createMmapBuffer() failed with status %d %s", |
| 197 | __func__, status, strerror(-status)); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 198 | result = AAUDIO_ERROR_UNAVAILABLE; |
| 199 | goto error; |
| 200 | } else { |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 201 | ALOGD("%s() createMmapBuffer() buffer_size = %d fr, burst_size %d fr" |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 202 | ", Sharable FD: %s", |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 203 | __func__, |
Kevin Rocard | 734334f | 2018-07-12 19:37:41 -0700 | [diff] [blame] | 204 | mMmapBufferinfo.buffer_size_frames, |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 205 | mMmapBufferinfo.burst_size_frames, |
Kevin Rocard | 734334f | 2018-07-12 19:37:41 -0700 | [diff] [blame] | 206 | isBufferShareable ? "Yes" : "No"); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | setBufferCapacity(mMmapBufferinfo.buffer_size_frames); |
Kevin Rocard | 734334f | 2018-07-12 19:37:41 -0700 | [diff] [blame] | 210 | if (!isBufferShareable) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 211 | // Exclusive mode can only be used by the service because the FD cannot be shared. |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 212 | int32_t audioServiceUid = |
| 213 | VALUE_OR_FATAL(legacy2aidl_uid_t_int32_t(getuid())); |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 214 | if ((mMmapClient.attributionSource.uid != audioServiceUid) && |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 215 | getSharingMode() == AAUDIO_SHARING_MODE_EXCLUSIVE) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 216 | ALOGW("%s() - exclusive FD cannot be used by client", __func__); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 217 | result = AAUDIO_ERROR_UNAVAILABLE; |
| 218 | goto error; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // Get information about the stream and pass it back to the caller. |
jiabin | a909409 | 2021-06-28 20:36:45 +0000 | [diff] [blame] | 223 | setChannelMask(AAudioConvert_androidToAAudioChannelMask( |
| 224 | config.channel_mask, getDirection() == AAUDIO_DIRECTION_INPUT, |
| 225 | AAudio_isChannelIndexMask(config.channel_mask))); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 226 | |
| 227 | // AAudio creates a copy of this FD and retains ownership of the copy. |
| 228 | // Assume that AudioFlinger will close the original shared_memory_fd. |
| 229 | mAudioDataFileDescriptor.reset(dup(mMmapBufferinfo.shared_memory_fd)); |
| 230 | if (mAudioDataFileDescriptor.get() == -1) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 231 | ALOGE("%s() - could not dup shared_memory_fd", __func__); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 232 | result = AAUDIO_ERROR_INTERNAL; |
| 233 | goto error; |
| 234 | } |
Jasmine Cha | c47a784 | 2021-05-27 11:56:37 +0800 | [diff] [blame] | 235 | // Call to HAL to make sure the transport FD was able to be closed by binder. |
| 236 | // This is a tricky workaround for a problem in Binder. |
| 237 | // TODO:[b/192048842] When that problem is fixed we may be able to remove or change this code. |
| 238 | struct audio_mmap_position position; |
| 239 | mMmapStream->getMmapPosition(&position); |
| 240 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 241 | mFramesPerBurst = mMmapBufferinfo.burst_size_frames; |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 242 | setFormat(config.format); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 243 | setSampleRate(config.sample_rate); |
| 244 | |
jiabin | a909409 | 2021-06-28 20:36:45 +0000 | [diff] [blame] | 245 | ALOGD("%s() actual rate = %d, channels = %d channelMask = %#x, deviceId = %d, capacity = %d\n", |
| 246 | __func__, getSampleRate(), getSamplesPerFrame(), getChannelMask(), |
| 247 | deviceId, getBufferCapacity()); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 248 | |
Phil Burk | 3c4e6b5 | 2019-01-22 15:53:36 -0800 | [diff] [blame] | 249 | ALOGD("%s() format = 0x%08x, frame size = %d, burst size = %d", |
| 250 | __func__, getFormat(), calculateBytesPerFrame(), mFramesPerBurst); |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 251 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 252 | return result; |
| 253 | |
| 254 | error: |
| 255 | close(); |
| 256 | return result; |
| 257 | } |
| 258 | |
Phil Burk | 320910f | 2020-08-12 14:29:10 +0000 | [diff] [blame] | 259 | void AAudioServiceEndpointMMAP::close() { |
Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 260 | if (mMmapStream != nullptr) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 261 | // Needs to be explicitly cleared or CTS will fail but it is not clear why. |
| 262 | mMmapStream.clear(); |
| 263 | // Apparently the above close is asynchronous. An attempt to open a new device |
| 264 | // right after a close can fail. Also some callbacks may still be in flight! |
| 265 | // FIXME Make closing synchronous. |
| 266 | AudioClock::sleepForNanos(100 * AAUDIO_NANOS_PER_MILLISECOND); |
| 267 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | aaudio_result_t AAudioServiceEndpointMMAP::startStream(sp<AAudioServiceStreamBase> stream, |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 271 | audio_port_handle_t *clientHandle __unused) { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 272 | // Start the client on behalf of the AAudio service. |
| 273 | // Use the port handle that was provided by openMmapStream(). |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 274 | audio_port_handle_t tempHandle = mPortHandle; |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 275 | audio_attributes_t attr = {}; |
| 276 | if (stream != nullptr) { |
| 277 | attr = getAudioAttributesFrom(stream.get()); |
| 278 | } |
| 279 | aaudio_result_t result = startClient( |
| 280 | mMmapClient, stream == nullptr ? nullptr : &attr, &tempHandle); |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 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); |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 285 | ALOGV("%s() mPortHandle = %d", __func__, mPortHandle); |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 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(). |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 300 | ALOGV("%s() mPortHandle = %d", __func__, 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, |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 305 | const audio_attributes_t *attr, |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 306 | audio_port_handle_t *clientHandle) { |
| 307 | if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL; |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 308 | status_t status = mMmapStream->start(client, attr, clientHandle); |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 309 | return AAudioConvert_androidToAAudioResult(status); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | aaudio_result_t AAudioServiceEndpointMMAP::stopClient(audio_port_handle_t clientHandle) { |
| 313 | if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL; |
| 314 | aaudio_result_t result = AAudioConvert_androidToAAudioResult(mMmapStream->stop(clientHandle)); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 315 | return result; |
| 316 | } |
| 317 | |
| 318 | // Get free-running DSP or DMA hardware position from the HAL. |
| 319 | aaudio_result_t AAudioServiceEndpointMMAP::getFreeRunningPosition(int64_t *positionFrames, |
| 320 | int64_t *timeNanos) { |
| 321 | struct audio_mmap_position position; |
| 322 | if (mMmapStream == nullptr) { |
| 323 | return AAUDIO_ERROR_NULL; |
| 324 | } |
| 325 | status_t status = mMmapStream->getMmapPosition(&position); |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 326 | ALOGV("%s() status= %d, pos = %d, nanos = %lld\n", |
| 327 | __func__, status, position.position_frames, (long long) position.time_nanoseconds); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 328 | aaudio_result_t result = AAudioConvert_androidToAAudioResult(status); |
| 329 | if (result == AAUDIO_ERROR_UNAVAILABLE) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 330 | ALOGW("%s(): getMmapPosition() has no position data available", __func__); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 331 | } else if (result != AAUDIO_OK) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 332 | ALOGE("%s(): getMmapPosition() returned status %d", __func__, status); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 333 | } else { |
| 334 | // Convert 32-bit position to 64-bit position. |
| 335 | mFramesTransferred.update32(position.position_frames); |
| 336 | *positionFrames = mFramesTransferred.get(); |
| 337 | *timeNanos = position.time_nanoseconds; |
| 338 | } |
| 339 | return result; |
| 340 | } |
| 341 | |
| 342 | aaudio_result_t AAudioServiceEndpointMMAP::getTimestamp(int64_t *positionFrames, |
| 343 | int64_t *timeNanos) { |
| 344 | return 0; // TODO |
| 345 | } |
| 346 | |
Phil Burk | a77869d | 2020-05-07 10:39:47 -0700 | [diff] [blame] | 347 | // This is called by onTearDown() in a separate thread to avoid deadlocks. |
| 348 | void AAudioServiceEndpointMMAP::handleTearDownAsync(audio_port_handle_t portHandle) { |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 349 | // Are we tearing down the EXCLUSIVE MMAP stream? |
| 350 | if (isStreamRegistered(portHandle)) { |
| 351 | ALOGD("%s(%d) tearing down this entire MMAP endpoint", __func__, portHandle); |
| 352 | disconnectRegisteredStreams(); |
| 353 | } else { |
| 354 | // Must be a SHARED stream? |
| 355 | ALOGD("%s(%d) disconnect a specific stream", __func__, portHandle); |
| 356 | aaudio_result_t result = mAAudioService.disconnectStreamByPortHandle(portHandle); |
| 357 | ALOGD("%s(%d) disconnectStreamByPortHandle returned %d", __func__, portHandle, result); |
| 358 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 359 | }; |
| 360 | |
Phil Burk | a77869d | 2020-05-07 10:39:47 -0700 | [diff] [blame] | 361 | // This is called by AudioFlinger when it wants to destroy a stream. |
| 362 | void AAudioServiceEndpointMMAP::onTearDown(audio_port_handle_t portHandle) { |
| 363 | ALOGD("%s(portHandle = %d) called", __func__, portHandle); |
Phil Burk | 3d20194 | 2021-04-08 23:27:04 +0000 | [diff] [blame] | 364 | android::sp<AAudioServiceEndpointMMAP> holdEndpoint(this); |
| 365 | std::thread asyncTask([holdEndpoint, portHandle]() { |
| 366 | holdEndpoint->handleTearDownAsync(portHandle); |
| 367 | }); |
Phil Burk | a77869d | 2020-05-07 10:39:47 -0700 | [diff] [blame] | 368 | asyncTask.detach(); |
| 369 | } |
| 370 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 371 | void AAudioServiceEndpointMMAP::onVolumeChanged(audio_channel_mask_t channels, |
| 372 | android::Vector<float> values) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 373 | // TODO Do we really need a different volume for each channel? |
| 374 | // We get called with an array filled with a single value! |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 375 | float volume = values[0]; |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 376 | ALOGD("%s() volume[0] = %f", __func__, volume); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 377 | std::lock_guard<std::mutex> lock(mLockStreams); |
Chih-Hung Hsieh | 3ef324d | 2018-12-11 11:48:12 -0800 | [diff] [blame] | 378 | for(const auto& stream : mRegisteredStreams) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 379 | stream->onVolumeChanged(volume); |
| 380 | } |
| 381 | }; |
| 382 | |
Phil Burk | a77869d | 2020-05-07 10:39:47 -0700 | [diff] [blame] | 383 | void AAudioServiceEndpointMMAP::onRoutingChanged(audio_port_handle_t portHandle) { |
| 384 | const int32_t deviceId = static_cast<int32_t>(portHandle); |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 385 | ALOGD("%s() called with dev %d, old = %d", __func__, deviceId, getDeviceId()); |
Phil Burk | a77869d | 2020-05-07 10:39:47 -0700 | [diff] [blame] | 386 | if (getDeviceId() != deviceId) { |
| 387 | if (getDeviceId() != AUDIO_PORT_HANDLE_NONE) { |
Phil Burk | 3d20194 | 2021-04-08 23:27:04 +0000 | [diff] [blame] | 388 | android::sp<AAudioServiceEndpointMMAP> holdEndpoint(this); |
| 389 | std::thread asyncTask([holdEndpoint, deviceId]() { |
| 390 | ALOGD("onRoutingChanged() asyncTask launched"); |
| 391 | holdEndpoint->disconnectRegisteredStreams(); |
| 392 | holdEndpoint->setDeviceId(deviceId); |
Phil Burk | a77869d | 2020-05-07 10:39:47 -0700 | [diff] [blame] | 393 | }); |
| 394 | asyncTask.detach(); |
| 395 | } else { |
| 396 | setDeviceId(deviceId); |
| 397 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 398 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 399 | }; |
| 400 | |
| 401 | /** |
| 402 | * Get an immutable description of the data queue from the HAL. |
| 403 | */ |
| 404 | aaudio_result_t AAudioServiceEndpointMMAP::getDownDataDescription(AudioEndpointParcelable &parcelable) |
| 405 | { |
| 406 | // Gather information on the data queue based on HAL info. |
| 407 | int32_t bytesPerFrame = calculateBytesPerFrame(); |
| 408 | int32_t capacityInBytes = getBufferCapacity() * bytesPerFrame; |
| 409 | int fdIndex = parcelable.addFileDescriptor(mAudioDataFileDescriptor, capacityInBytes); |
| 410 | parcelable.mDownDataQueueParcelable.setupMemory(fdIndex, 0, capacityInBytes); |
| 411 | parcelable.mDownDataQueueParcelable.setBytesPerFrame(bytesPerFrame); |
| 412 | parcelable.mDownDataQueueParcelable.setFramesPerBurst(mFramesPerBurst); |
| 413 | parcelable.mDownDataQueueParcelable.setCapacityInFrames(getBufferCapacity()); |
| 414 | return AAUDIO_OK; |
| 415 | } |
jiabin | b7d8c5a | 2020-08-26 17:24:52 -0700 | [diff] [blame] | 416 | |
| 417 | aaudio_result_t AAudioServiceEndpointMMAP::getExternalPosition(uint64_t *positionFrames, |
| 418 | int64_t *timeNanos) |
| 419 | { |
| 420 | if (!mExternalPositionSupported) { |
| 421 | return AAUDIO_ERROR_INVALID_STATE; |
| 422 | } |
| 423 | status_t status = mMmapStream->getExternalPosition(positionFrames, timeNanos); |
| 424 | if (status == INVALID_OPERATION) { |
| 425 | // getExternalPosition is not supported. Set mExternalPositionSupported as false |
| 426 | // so that the call will not go to the HAL next time. |
| 427 | mExternalPositionSupported = false; |
| 428 | } |
| 429 | return AAudioConvert_androidToAAudioResult(status); |
| 430 | } |