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 | /** |
| 41 | * Stream that uses an MMAP buffer. |
| 42 | */ |
| 43 | |
| 44 | AAudioServiceStreamMMAP::AAudioServiceStreamMMAP() |
| 45 | : AAudioServiceStreamBase() |
| 46 | , mMmapStreamCallback(new MyMmapStreamCallback(*this)) |
| 47 | , mPreviousFrameCounter(0) |
| 48 | , mMmapStream(nullptr) { |
| 49 | } |
| 50 | |
| 51 | AAudioServiceStreamMMAP::~AAudioServiceStreamMMAP() { |
| 52 | close(); |
| 53 | } |
| 54 | |
| 55 | aaudio_result_t AAudioServiceStreamMMAP::close() { |
| 56 | ALOGD("AAudioServiceStreamMMAP::close() called, %p", mMmapStream.get()); |
| 57 | mMmapStream.clear(); // TODO review. Is that all we have to do? |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 58 | // Apparently the above close is asynchronous. An attempt to open a new device |
| 59 | // right after a close can fail. Also some callbacks may still be in flight! |
| 60 | // FIXME Make closing synchronous. |
| 61 | AudioClock::sleepForNanos(100 * AAUDIO_NANOS_PER_MILLISECOND); |
| 62 | |
Phil Burk | 942bdc0 | 2017-05-03 11:36:50 -0700 | [diff] [blame] | 63 | if (mAudioDataFileDescriptor != -1) { |
| 64 | ALOGV("AAudioServiceStreamMMAP: LEAK? close(mAudioDataFileDescriptor = %d)\n", |
| 65 | mAudioDataFileDescriptor); |
| 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 | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame^] | 94 | ALOGI("open request dump()"); |
| 95 | request.dump(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 96 | |
| 97 | mMmapClient.clientUid = request.getUserId(); |
| 98 | mMmapClient.clientPid = request.getProcessId(); |
| 99 | aaudio_direction_t direction = request.getDirection(); |
| 100 | |
| 101 | // Fill in config |
| 102 | aaudio_audio_format_t aaudioFormat = configurationInput.getAudioFormat(); |
| 103 | if (aaudioFormat == AAUDIO_UNSPECIFIED || aaudioFormat == AAUDIO_FORMAT_PCM_FLOAT) { |
| 104 | ALOGI("open forcing use of AAUDIO_FORMAT_PCM_I16"); |
| 105 | aaudioFormat = AAUDIO_FORMAT_PCM_I16; |
| 106 | } |
| 107 | config.format = AAudioConvert_aaudioToAndroidDataFormat(aaudioFormat); |
| 108 | |
| 109 | int32_t aaudioSampleRate = configurationInput.getSampleRate(); |
| 110 | if (aaudioSampleRate == AAUDIO_UNSPECIFIED) { |
| 111 | aaudioSampleRate = AAUDIO_SAMPLE_RATE_DEFAULT; |
| 112 | } |
| 113 | config.sample_rate = aaudioSampleRate; |
| 114 | |
| 115 | int32_t aaudioSamplesPerFrame = configurationInput.getSamplesPerFrame(); |
| 116 | |
| 117 | if (direction == AAUDIO_DIRECTION_OUTPUT) { |
| 118 | config.channel_mask = (aaudioSamplesPerFrame == AAUDIO_UNSPECIFIED) |
| 119 | ? AUDIO_CHANNEL_OUT_STEREO |
| 120 | : audio_channel_out_mask_from_count(aaudioSamplesPerFrame); |
| 121 | } else if (direction == AAUDIO_DIRECTION_INPUT) { |
| 122 | config.channel_mask = (aaudioSamplesPerFrame == AAUDIO_UNSPECIFIED) |
| 123 | ? AUDIO_CHANNEL_IN_STEREO |
| 124 | : audio_channel_in_mask_from_count(aaudioSamplesPerFrame); |
| 125 | } else { |
| 126 | ALOGE("openMmapStream - invalid direction = %d", direction); |
| 127 | return AAUDIO_ERROR_ILLEGAL_ARGUMENT; |
| 128 | } |
| 129 | |
| 130 | mMmapClient.packageName.setTo(String16("aaudio_service")); // FIXME what should we do here? |
| 131 | |
| 132 | MmapStreamInterface::stream_direction_t streamDirection = (direction == AAUDIO_DIRECTION_OUTPUT) |
| 133 | ? MmapStreamInterface::DIRECTION_OUTPUT : MmapStreamInterface::DIRECTION_INPUT; |
| 134 | |
Phil Burk | 4c5129b | 2017-04-28 15:17:32 -0700 | [diff] [blame] | 135 | ALOGD("AAudioServiceStreamMMAP::open() request devId = %d, sRate = %d", |
| 136 | deviceId, config.sample_rate); |
| 137 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 138 | // Open HAL stream. |
| 139 | status_t status = MmapStreamInterface::openMmapStream(streamDirection, |
| 140 | &attributes, |
| 141 | &config, |
| 142 | mMmapClient, |
| 143 | &deviceId, |
| 144 | mMmapStreamCallback, |
| 145 | mMmapStream); |
| 146 | if (status != OK) { |
| 147 | ALOGE("openMmapStream returned status %d", status); |
| 148 | return AAUDIO_ERROR_UNAVAILABLE; |
| 149 | } |
| 150 | |
| 151 | // Create MMAP/NOIRQ buffer. |
| 152 | int32_t minSizeFrames = configurationInput.getBufferCapacity(); |
| 153 | if (minSizeFrames == 0) { // zero will get rejected |
| 154 | minSizeFrames = AAUDIO_BUFFER_CAPACITY_MIN; |
| 155 | } |
| 156 | status = mMmapStream->createMmapBuffer(minSizeFrames, &mMmapBufferinfo); |
| 157 | if (status != OK) { |
| 158 | ALOGE("%s: createMmapBuffer() returned status %d, return AAUDIO_ERROR_UNAVAILABLE", |
| 159 | __FILE__, status); |
| 160 | return AAUDIO_ERROR_UNAVAILABLE; |
| 161 | } else { |
| 162 | ALOGD("createMmapBuffer status %d shared_address = %p buffer_size %d burst_size %d", |
| 163 | status, mMmapBufferinfo.shared_memory_address, |
| 164 | mMmapBufferinfo.buffer_size_frames, |
| 165 | mMmapBufferinfo.burst_size_frames); |
| 166 | } |
| 167 | |
| 168 | // Get information about the stream and pass it back to the caller. |
| 169 | mSamplesPerFrame = (direction == AAUDIO_DIRECTION_OUTPUT) |
| 170 | ? audio_channel_count_from_out_mask(config.channel_mask) |
| 171 | : audio_channel_count_from_in_mask(config.channel_mask); |
| 172 | |
| 173 | mAudioDataFileDescriptor = mMmapBufferinfo.shared_memory_fd; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame^] | 174 | ALOGD("AAudioServiceStreamMMAP::open LEAK? mAudioDataFileDescriptor = %d\n", |
Phil Burk | 942bdc0 | 2017-05-03 11:36:50 -0700 | [diff] [blame] | 175 | mAudioDataFileDescriptor); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 176 | mFramesPerBurst = mMmapBufferinfo.burst_size_frames; |
| 177 | mCapacityInFrames = mMmapBufferinfo.buffer_size_frames; |
| 178 | mAudioFormat = AAudioConvert_androidToAAudioDataFormat(config.format); |
| 179 | mSampleRate = config.sample_rate; |
| 180 | |
Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 181 | // Scale up the burst size to meet the minimum equivalent in microseconds. |
| 182 | // This is to avoid waking the CPU too often when the HW burst is very small |
| 183 | // or at high sample rates. |
| 184 | int32_t burstMinMicros = AAudioProperty_getHardwareBurstMinMicros(); |
| 185 | int32_t burstMicros = 0; |
| 186 | do { |
| 187 | if (burstMicros > 0) { // skip first loop |
| 188 | mFramesPerBurst *= 2; |
| 189 | } |
| 190 | burstMicros = mFramesPerBurst * static_cast<int64_t>(1000000) / mSampleRate; |
| 191 | } while (burstMicros < burstMinMicros); |
| 192 | |
| 193 | ALOGD("AAudioServiceStreamMMAP::open() original burst = %d, minMicros = %d, final burst = %d\n", |
| 194 | mMmapBufferinfo.burst_size_frames, burstMinMicros, mFramesPerBurst); |
| 195 | |
Phil Burk | 4c5129b | 2017-04-28 15:17:32 -0700 | [diff] [blame] | 196 | ALOGD("AAudioServiceStreamMMAP::open() got devId = %d, sRate = %d", |
| 197 | deviceId, config.sample_rate); |
| 198 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 199 | // Fill in AAudioStreamConfiguration |
| 200 | configurationOutput.setSampleRate(mSampleRate); |
| 201 | configurationOutput.setSamplesPerFrame(mSamplesPerFrame); |
| 202 | configurationOutput.setAudioFormat(mAudioFormat); |
| 203 | configurationOutput.setDeviceId(deviceId); |
| 204 | |
| 205 | return AAUDIO_OK; |
| 206 | } |
| 207 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 208 | /** |
| 209 | * Start the flow of data. |
| 210 | */ |
| 211 | aaudio_result_t AAudioServiceStreamMMAP::start() { |
| 212 | if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame^] | 213 | aaudio_result_t result; |
| 214 | status_t status = mMmapStream->start(mMmapClient, &mPortHandle); |
| 215 | if (status != OK) { |
| 216 | ALOGE("AAudioServiceStreamMMAP::start() mMmapStream->start() returned %d", status); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 217 | processError(); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame^] | 218 | result = AAudioConvert_androidToAAudioResult(status); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 219 | } else { |
| 220 | result = AAudioServiceStreamBase::start(); |
| 221 | } |
| 222 | return result; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Stop the flow of data such that start() can resume with loss of data. |
| 227 | */ |
| 228 | aaudio_result_t AAudioServiceStreamMMAP::pause() { |
| 229 | if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL; |
| 230 | |
| 231 | aaudio_result_t result1 = AAudioServiceStreamBase::pause(); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame^] | 232 | status_t status = mMmapStream->stop(mPortHandle); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 233 | mFramesRead.reset32(); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame^] | 234 | return (result1 != AAUDIO_OK) ? result1 : AAudioConvert_androidToAAudioResult(status); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 235 | } |
| 236 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 237 | aaudio_result_t AAudioServiceStreamMMAP::stop() { |
| 238 | if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL; |
| 239 | |
| 240 | aaudio_result_t result1 = AAudioServiceStreamBase::stop(); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame^] | 241 | aaudio_result_t status = mMmapStream->stop(mPortHandle); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 242 | mFramesRead.reset32(); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame^] | 243 | return (result1 != AAUDIO_OK) ? result1 : AAudioConvert_androidToAAudioResult(status); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 246 | /** |
| 247 | * Discard any data held by the underlying HAL or Service. |
| 248 | */ |
| 249 | aaudio_result_t AAudioServiceStreamMMAP::flush() { |
| 250 | if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL; |
| 251 | // TODO how do we flush an MMAP/NOIRQ buffer? sync pointers? |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 252 | ALOGD("AAudioServiceStreamMMAP::flush() send AAUDIO_SERVICE_EVENT_FLUSHED"); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 253 | sendServiceEvent(AAUDIO_SERVICE_EVENT_FLUSHED); |
| 254 | mState = AAUDIO_STREAM_STATE_FLUSHED; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 255 | return AAudioServiceStreamBase::flush();; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | |
| 259 | aaudio_result_t AAudioServiceStreamMMAP::getFreeRunningPosition(int64_t *positionFrames, |
| 260 | int64_t *timeNanos) { |
| 261 | struct audio_mmap_position position; |
| 262 | if (mMmapStream == nullptr) { |
| 263 | processError(); |
| 264 | return AAUDIO_ERROR_NULL; |
| 265 | } |
| 266 | status_t status = mMmapStream->getMmapPosition(&position); |
| 267 | if (status != OK) { |
| 268 | ALOGE("sendCurrentTimestamp(): getMmapPosition() returned %d", status); |
| 269 | processError(); |
| 270 | return AAudioConvert_androidToAAudioResult(status); |
| 271 | } else { |
| 272 | mFramesRead.update32(position.position_frames); |
| 273 | *positionFrames = mFramesRead.get(); |
| 274 | *timeNanos = position.time_nanoseconds; |
| 275 | } |
| 276 | return AAUDIO_OK; |
| 277 | } |
| 278 | |
| 279 | void AAudioServiceStreamMMAP::onTearDown() { |
| 280 | ALOGD("AAudioServiceStreamMMAP::onTearDown() called - TODO"); |
| 281 | }; |
| 282 | |
| 283 | void AAudioServiceStreamMMAP::onVolumeChanged(audio_channel_mask_t channels, |
| 284 | android::Vector<float> values) { |
| 285 | // TODO do we really need a different volume for each channel? |
| 286 | float volume = values[0]; |
| 287 | ALOGD("AAudioServiceStreamMMAP::onVolumeChanged() volume[0] = %f", volume); |
| 288 | sendServiceEvent(AAUDIO_SERVICE_EVENT_VOLUME, volume); |
| 289 | }; |
| 290 | |
| 291 | void AAudioServiceStreamMMAP::onRoutingChanged(audio_port_handle_t deviceId) { |
| 292 | ALOGD("AAudioServiceStreamMMAP::onRoutingChanged() called with %d, old = %d", |
| 293 | deviceId, mPortHandle); |
| 294 | if (mPortHandle > 0 && mPortHandle != deviceId) { |
| 295 | sendServiceEvent(AAUDIO_SERVICE_EVENT_DISCONNECTED); |
| 296 | } |
| 297 | mPortHandle = deviceId; |
| 298 | }; |
| 299 | |
| 300 | /** |
| 301 | * Get an immutable description of the data queue from the HAL. |
| 302 | */ |
| 303 | aaudio_result_t AAudioServiceStreamMMAP::getDownDataDescription(AudioEndpointParcelable &parcelable) |
| 304 | { |
| 305 | // Gather information on the data queue based on HAL info. |
| 306 | int32_t bytesPerFrame = calculateBytesPerFrame(); |
| 307 | int32_t capacityInBytes = mCapacityInFrames * bytesPerFrame; |
| 308 | int fdIndex = parcelable.addFileDescriptor(mAudioDataFileDescriptor, capacityInBytes); |
| 309 | parcelable.mDownDataQueueParcelable.setupMemory(fdIndex, 0, capacityInBytes); |
| 310 | parcelable.mDownDataQueueParcelable.setBytesPerFrame(bytesPerFrame); |
| 311 | parcelable.mDownDataQueueParcelable.setFramesPerBurst(mFramesPerBurst); |
| 312 | parcelable.mDownDataQueueParcelable.setCapacityInFrames(mCapacityInFrames); |
| 313 | return AAUDIO_OK; |
| 314 | } |