Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [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 "AAudioService" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <atomic> |
| 22 | #include <stdint.h> |
| 23 | |
| 24 | #include <utils/String16.h> |
| 25 | #include <media/nbaio/AudioStreamOutSink.h> |
| 26 | #include <media/MmapStreamInterface.h> |
| 27 | |
| 28 | #include "AAudioServiceStreamBase.h" |
| 29 | #include "AAudioServiceStreamMMAP.h" |
| 30 | #include "binding/AudioEndpointParcelable.h" |
| 31 | #include "SharedMemoryProxy.h" |
| 32 | #include "utility/AAudioUtilities.h" |
| 33 | |
| 34 | using namespace android; |
| 35 | using namespace aaudio; |
| 36 | |
| 37 | #define AAUDIO_BUFFER_CAPACITY_MIN 4 * 512 |
| 38 | #define AAUDIO_SAMPLE_RATE_DEFAULT 48000 |
| 39 | |
| 40 | /** |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 41 | * Service Stream that uses an MMAP buffer. |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 42 | */ |
| 43 | |
Eric Laurent | d51329e | 2017-06-30 16:06:16 -0700 | [diff] [blame] | 44 | AAudioServiceStreamMMAP::AAudioServiceStreamMMAP(uid_t serviceUid) |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 45 | : AAudioServiceStreamBase() |
| 46 | , mMmapStreamCallback(new MyMmapStreamCallback(*this)) |
| 47 | , mPreviousFrameCounter(0) |
Eric Laurent | d51329e | 2017-06-30 16:06:16 -0700 | [diff] [blame] | 48 | , mMmapStream(nullptr) |
| 49 | , mCachedUserId(serviceUid) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 52 | aaudio_result_t AAudioServiceStreamMMAP::close() { |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame^] | 53 | if (mState == AAUDIO_STREAM_STATE_CLOSED) { |
| 54 | return AAUDIO_OK; |
| 55 | } |
| 56 | |
| 57 | if (mMmapStream != 0) { |
| 58 | mMmapStream.clear(); // TODO review. Is that all we have to do? |
| 59 | // Apparently the above close is asynchronous. An attempt to open a new device |
| 60 | // right after a close can fail. Also some callbacks may still be in flight! |
| 61 | // FIXME Make closing synchronous. |
| 62 | AudioClock::sleepForNanos(100 * AAUDIO_NANOS_PER_MILLISECOND); |
| 63 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 64 | |
Phil Burk | 942bdc0 | 2017-05-03 11:36:50 -0700 | [diff] [blame] | 65 | if (mAudioDataFileDescriptor != -1) { |
Phil Burk | 942bdc0 | 2017-05-03 11:36:50 -0700 | [diff] [blame] | 66 | ::close(mAudioDataFileDescriptor); |
| 67 | mAudioDataFileDescriptor = -1; |
| 68 | } |
| 69 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 70 | return AAudioServiceStreamBase::close(); |
| 71 | } |
| 72 | |
| 73 | // Open stream on HAL and pass information about the shared memory buffer back to the client. |
| 74 | aaudio_result_t AAudioServiceStreamMMAP::open(const aaudio::AAudioStreamRequest &request, |
| 75 | aaudio::AAudioStreamConfiguration &configurationOutput) { |
| 76 | const audio_attributes_t attributes = { |
| 77 | .content_type = AUDIO_CONTENT_TYPE_MUSIC, |
| 78 | .usage = AUDIO_USAGE_MEDIA, |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 79 | .source = AUDIO_SOURCE_VOICE_RECOGNITION, |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 80 | .flags = AUDIO_FLAG_LOW_LATENCY, |
| 81 | .tags = "" |
| 82 | }; |
| 83 | audio_config_base_t config; |
| 84 | |
| 85 | aaudio_result_t result = AAudioServiceStreamBase::open(request, configurationOutput); |
| 86 | if (result != AAUDIO_OK) { |
| 87 | ALOGE("AAudioServiceStreamBase open returned %d", result); |
| 88 | return result; |
| 89 | } |
| 90 | |
| 91 | const AAudioStreamConfiguration &configurationInput = request.getConstantConfiguration(); |
| 92 | audio_port_handle_t deviceId = configurationInput.getDeviceId(); |
| 93 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 94 | mMmapClient.clientUid = request.getUserId(); |
| 95 | mMmapClient.clientPid = request.getProcessId(); |
| 96 | aaudio_direction_t direction = request.getDirection(); |
| 97 | |
| 98 | // Fill in config |
Phil Burk | 9dca982 | 2017-05-26 14:27:43 -0700 | [diff] [blame] | 99 | aaudio_format_t aaudioFormat = configurationInput.getAudioFormat(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 100 | if (aaudioFormat == AAUDIO_UNSPECIFIED || aaudioFormat == AAUDIO_FORMAT_PCM_FLOAT) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 101 | aaudioFormat = AAUDIO_FORMAT_PCM_I16; |
| 102 | } |
| 103 | config.format = AAudioConvert_aaudioToAndroidDataFormat(aaudioFormat); |
| 104 | |
| 105 | int32_t aaudioSampleRate = configurationInput.getSampleRate(); |
| 106 | if (aaudioSampleRate == AAUDIO_UNSPECIFIED) { |
| 107 | aaudioSampleRate = AAUDIO_SAMPLE_RATE_DEFAULT; |
| 108 | } |
| 109 | config.sample_rate = aaudioSampleRate; |
| 110 | |
| 111 | int32_t aaudioSamplesPerFrame = configurationInput.getSamplesPerFrame(); |
| 112 | |
| 113 | if (direction == AAUDIO_DIRECTION_OUTPUT) { |
| 114 | config.channel_mask = (aaudioSamplesPerFrame == AAUDIO_UNSPECIFIED) |
| 115 | ? AUDIO_CHANNEL_OUT_STEREO |
| 116 | : audio_channel_out_mask_from_count(aaudioSamplesPerFrame); |
| 117 | } else if (direction == AAUDIO_DIRECTION_INPUT) { |
| 118 | config.channel_mask = (aaudioSamplesPerFrame == AAUDIO_UNSPECIFIED) |
| 119 | ? AUDIO_CHANNEL_IN_STEREO |
| 120 | : audio_channel_in_mask_from_count(aaudioSamplesPerFrame); |
| 121 | } else { |
| 122 | ALOGE("openMmapStream - invalid direction = %d", direction); |
| 123 | return AAUDIO_ERROR_ILLEGAL_ARGUMENT; |
| 124 | } |
| 125 | |
| 126 | mMmapClient.packageName.setTo(String16("aaudio_service")); // FIXME what should we do here? |
| 127 | |
| 128 | MmapStreamInterface::stream_direction_t streamDirection = (direction == AAUDIO_DIRECTION_OUTPUT) |
| 129 | ? MmapStreamInterface::DIRECTION_OUTPUT : MmapStreamInterface::DIRECTION_INPUT; |
| 130 | |
| 131 | // Open HAL stream. |
| 132 | status_t status = MmapStreamInterface::openMmapStream(streamDirection, |
| 133 | &attributes, |
| 134 | &config, |
| 135 | mMmapClient, |
| 136 | &deviceId, |
| 137 | mMmapStreamCallback, |
| 138 | mMmapStream); |
| 139 | if (status != OK) { |
| 140 | ALOGE("openMmapStream returned status %d", status); |
| 141 | return AAUDIO_ERROR_UNAVAILABLE; |
| 142 | } |
| 143 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 144 | if (deviceId == AAUDIO_UNSPECIFIED) { |
| 145 | ALOGW("AAudioServiceStreamMMAP::open() - openMmapStream() failed to set deviceId"); |
| 146 | } |
| 147 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 148 | // Create MMAP/NOIRQ buffer. |
| 149 | int32_t minSizeFrames = configurationInput.getBufferCapacity(); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 150 | if (minSizeFrames <= 0) { // zero will get rejected |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 151 | minSizeFrames = AAUDIO_BUFFER_CAPACITY_MIN; |
| 152 | } |
| 153 | status = mMmapStream->createMmapBuffer(minSizeFrames, &mMmapBufferinfo); |
| 154 | if (status != OK) { |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 155 | ALOGE("AAudioServiceStreamMMAP::open() - createMmapBuffer() returned status %d", |
| 156 | status); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 157 | return AAUDIO_ERROR_UNAVAILABLE; |
| 158 | } else { |
Eric Laurent | d51329e | 2017-06-30 16:06:16 -0700 | [diff] [blame] | 159 | ALOGD("createMmapBuffer status %d shared_address = %p buffer_size %d burst_size %d" |
| 160 | "Sharable FD: %s", |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 161 | status, mMmapBufferinfo.shared_memory_address, |
Eric Laurent | d51329e | 2017-06-30 16:06:16 -0700 | [diff] [blame] | 162 | abs(mMmapBufferinfo.buffer_size_frames), |
| 163 | mMmapBufferinfo.burst_size_frames, |
| 164 | mMmapBufferinfo.buffer_size_frames < 0 ? "Yes" : "No"); |
| 165 | } |
| 166 | |
| 167 | mCapacityInFrames = mMmapBufferinfo.buffer_size_frames; |
| 168 | // FIXME: the audio HAL indicates if the shared memory fd can be shared outside of audioserver |
| 169 | // by returning a negative buffer size |
| 170 | if (mCapacityInFrames < 0) { |
| 171 | // Exclusive mode is possible from any client |
| 172 | mCapacityInFrames = -mCapacityInFrames; |
| 173 | } else { |
| 174 | // exclusive mode is only possible if the final fd destination is inside audioserver |
| 175 | if ((mMmapClient.clientUid != mCachedUserId) && |
| 176 | configurationInput.getSharingMode() == AAUDIO_SHARING_MODE_EXCLUSIVE) { |
| 177 | // Fallback is handled by caller but indicate what is possible in case |
| 178 | // this is used in the future |
| 179 | configurationOutput.setSharingMode(AAUDIO_SHARING_MODE_SHARED); |
| 180 | return AAUDIO_ERROR_UNAVAILABLE; |
| 181 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | // Get information about the stream and pass it back to the caller. |
| 185 | mSamplesPerFrame = (direction == AAUDIO_DIRECTION_OUTPUT) |
| 186 | ? audio_channel_count_from_out_mask(config.channel_mask) |
| 187 | : audio_channel_count_from_in_mask(config.channel_mask); |
| 188 | |
| 189 | mAudioDataFileDescriptor = mMmapBufferinfo.shared_memory_fd; |
| 190 | mFramesPerBurst = mMmapBufferinfo.burst_size_frames; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 191 | mAudioFormat = AAudioConvert_androidToAAudioDataFormat(config.format); |
| 192 | mSampleRate = config.sample_rate; |
| 193 | |
Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 194 | // Scale up the burst size to meet the minimum equivalent in microseconds. |
| 195 | // This is to avoid waking the CPU too often when the HW burst is very small |
| 196 | // or at high sample rates. |
| 197 | int32_t burstMinMicros = AAudioProperty_getHardwareBurstMinMicros(); |
| 198 | int32_t burstMicros = 0; |
| 199 | do { |
| 200 | if (burstMicros > 0) { // skip first loop |
| 201 | mFramesPerBurst *= 2; |
| 202 | } |
| 203 | burstMicros = mFramesPerBurst * static_cast<int64_t>(1000000) / mSampleRate; |
| 204 | } while (burstMicros < burstMinMicros); |
| 205 | |
| 206 | ALOGD("AAudioServiceStreamMMAP::open() original burst = %d, minMicros = %d, final burst = %d\n", |
| 207 | mMmapBufferinfo.burst_size_frames, burstMinMicros, mFramesPerBurst); |
| 208 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 209 | ALOGD("AAudioServiceStreamMMAP::open() actual rate = %d, channels = %d, deviceId = %d\n", |
| 210 | mSampleRate, mSamplesPerFrame, deviceId); |
| 211 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 212 | // Fill in AAudioStreamConfiguration |
| 213 | configurationOutput.setSampleRate(mSampleRate); |
| 214 | configurationOutput.setSamplesPerFrame(mSamplesPerFrame); |
| 215 | configurationOutput.setAudioFormat(mAudioFormat); |
| 216 | configurationOutput.setDeviceId(deviceId); |
| 217 | |
| 218 | return AAUDIO_OK; |
| 219 | } |
| 220 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 221 | /** |
| 222 | * Start the flow of data. |
| 223 | */ |
| 224 | aaudio_result_t AAudioServiceStreamMMAP::start() { |
| 225 | if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 226 | aaudio_result_t result; |
| 227 | status_t status = mMmapStream->start(mMmapClient, &mPortHandle); |
| 228 | if (status != OK) { |
| 229 | ALOGE("AAudioServiceStreamMMAP::start() mMmapStream->start() returned %d", status); |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 230 | disconnect(); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 231 | result = AAudioConvert_androidToAAudioResult(status); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 232 | } else { |
| 233 | result = AAudioServiceStreamBase::start(); |
| 234 | } |
| 235 | return result; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Stop the flow of data such that start() can resume with loss of data. |
| 240 | */ |
| 241 | aaudio_result_t AAudioServiceStreamMMAP::pause() { |
| 242 | if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL; |
| 243 | |
| 244 | aaudio_result_t result1 = AAudioServiceStreamBase::pause(); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 245 | status_t status = mMmapStream->stop(mPortHandle); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 246 | mFramesRead.reset32(); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 247 | return (result1 != AAUDIO_OK) ? result1 : AAudioConvert_androidToAAudioResult(status); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 248 | } |
| 249 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 250 | aaudio_result_t AAudioServiceStreamMMAP::stop() { |
| 251 | if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL; |
| 252 | |
| 253 | aaudio_result_t result1 = AAudioServiceStreamBase::stop(); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 254 | aaudio_result_t status = mMmapStream->stop(mPortHandle); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 255 | mFramesRead.reset32(); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 256 | return (result1 != AAUDIO_OK) ? result1 : AAudioConvert_androidToAAudioResult(status); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 259 | /** |
| 260 | * Discard any data held by the underlying HAL or Service. |
| 261 | */ |
| 262 | aaudio_result_t AAudioServiceStreamMMAP::flush() { |
| 263 | if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL; |
| 264 | // TODO how do we flush an MMAP/NOIRQ buffer? sync pointers? |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 265 | return AAudioServiceStreamBase::flush();; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 268 | aaudio_result_t AAudioServiceStreamMMAP::getFreeRunningPosition(int64_t *positionFrames, |
| 269 | int64_t *timeNanos) { |
| 270 | struct audio_mmap_position position; |
| 271 | if (mMmapStream == nullptr) { |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 272 | disconnect(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 273 | return AAUDIO_ERROR_NULL; |
| 274 | } |
| 275 | status_t status = mMmapStream->getMmapPosition(&position); |
| 276 | if (status != OK) { |
| 277 | ALOGE("sendCurrentTimestamp(): getMmapPosition() returned %d", status); |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 278 | disconnect(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 279 | return AAudioConvert_androidToAAudioResult(status); |
| 280 | } else { |
| 281 | mFramesRead.update32(position.position_frames); |
| 282 | *positionFrames = mFramesRead.get(); |
| 283 | *timeNanos = position.time_nanoseconds; |
| 284 | } |
| 285 | return AAUDIO_OK; |
| 286 | } |
| 287 | |
| 288 | void AAudioServiceStreamMMAP::onTearDown() { |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 289 | ALOGD("AAudioServiceStreamMMAP::onTearDown() called"); |
| 290 | disconnect(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 291 | }; |
| 292 | |
| 293 | void AAudioServiceStreamMMAP::onVolumeChanged(audio_channel_mask_t channels, |
| 294 | android::Vector<float> values) { |
| 295 | // TODO do we really need a different volume for each channel? |
| 296 | float volume = values[0]; |
| 297 | ALOGD("AAudioServiceStreamMMAP::onVolumeChanged() volume[0] = %f", volume); |
| 298 | sendServiceEvent(AAUDIO_SERVICE_EVENT_VOLUME, volume); |
| 299 | }; |
| 300 | |
| 301 | void AAudioServiceStreamMMAP::onRoutingChanged(audio_port_handle_t deviceId) { |
| 302 | ALOGD("AAudioServiceStreamMMAP::onRoutingChanged() called with %d, old = %d", |
| 303 | deviceId, mPortHandle); |
| 304 | if (mPortHandle > 0 && mPortHandle != deviceId) { |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 305 | disconnect(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 306 | } |
| 307 | mPortHandle = deviceId; |
| 308 | }; |
| 309 | |
| 310 | /** |
| 311 | * Get an immutable description of the data queue from the HAL. |
| 312 | */ |
| 313 | aaudio_result_t AAudioServiceStreamMMAP::getDownDataDescription(AudioEndpointParcelable &parcelable) |
| 314 | { |
| 315 | // Gather information on the data queue based on HAL info. |
| 316 | int32_t bytesPerFrame = calculateBytesPerFrame(); |
| 317 | int32_t capacityInBytes = mCapacityInFrames * bytesPerFrame; |
| 318 | int fdIndex = parcelable.addFileDescriptor(mAudioDataFileDescriptor, capacityInBytes); |
| 319 | parcelable.mDownDataQueueParcelable.setupMemory(fdIndex, 0, capacityInBytes); |
| 320 | parcelable.mDownDataQueueParcelable.setBytesPerFrame(bytesPerFrame); |
| 321 | parcelable.mDownDataQueueParcelable.setFramesPerBurst(mFramesPerBurst); |
| 322 | parcelable.mDownDataQueueParcelable.setCapacityInFrames(mCapacityInFrames); |
| 323 | return AAUDIO_OK; |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 324 | } |