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 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 17 | #define LOG_TAG "AAudioServiceStreamMMAP" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 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 | |
Phil Burk | e72481c | 2017-08-08 13:20:45 -0700 | [diff] [blame] | 34 | using android::base::unique_fd; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 35 | using namespace android; |
| 36 | using namespace aaudio; |
| 37 | |
| 38 | #define AAUDIO_BUFFER_CAPACITY_MIN 4 * 512 |
| 39 | #define AAUDIO_SAMPLE_RATE_DEFAULT 48000 |
| 40 | |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 41 | // This is an estimate of the time difference between the HW and the MMAP time. |
| 42 | // TODO Get presentation timestamps from the HAL instead of using these estimates. |
| 43 | #define OUTPUT_ESTIMATED_HARDWARE_OFFSET_NANOS (3 * AAUDIO_NANOS_PER_MILLISECOND) |
| 44 | #define INPUT_ESTIMATED_HARDWARE_OFFSET_NANOS (-1 * AAUDIO_NANOS_PER_MILLISECOND) |
| 45 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 46 | /** |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 47 | * Service Stream that uses an MMAP buffer. |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 48 | */ |
| 49 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 50 | AAudioServiceStreamMMAP::AAudioServiceStreamMMAP(const android::AudioClient& serviceClient, |
| 51 | bool inService) |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 52 | : AAudioServiceStreamBase() |
| 53 | , mMmapStreamCallback(new MyMmapStreamCallback(*this)) |
| 54 | , mPreviousFrameCounter(0) |
Eric Laurent | d51329e | 2017-06-30 16:06:16 -0700 | [diff] [blame] | 55 | , mMmapStream(nullptr) |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 56 | , mServiceClient(serviceClient) |
| 57 | , mInService(inService) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 60 | aaudio_result_t AAudioServiceStreamMMAP::close() { |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 61 | if (mState == AAUDIO_STREAM_STATE_CLOSED) { |
| 62 | return AAUDIO_OK; |
| 63 | } |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 64 | stop(); |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 65 | if (mMmapStream != 0) { |
| 66 | mMmapStream.clear(); // TODO review. Is that all we have to do? |
| 67 | // Apparently the above close is asynchronous. An attempt to open a new device |
| 68 | // right after a close can fail. Also some callbacks may still be in flight! |
| 69 | // FIXME Make closing synchronous. |
| 70 | AudioClock::sleepForNanos(100 * AAUDIO_NANOS_PER_MILLISECOND); |
| 71 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 72 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 73 | return AAudioServiceStreamBase::close(); |
| 74 | } |
| 75 | |
| 76 | // Open stream on HAL and pass information about the shared memory buffer back to the client. |
| 77 | aaudio_result_t AAudioServiceStreamMMAP::open(const aaudio::AAudioStreamRequest &request, |
| 78 | aaudio::AAudioStreamConfiguration &configurationOutput) { |
| 79 | const audio_attributes_t attributes = { |
| 80 | .content_type = AUDIO_CONTENT_TYPE_MUSIC, |
| 81 | .usage = AUDIO_USAGE_MEDIA, |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 82 | .source = AUDIO_SOURCE_VOICE_RECOGNITION, |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 83 | .flags = AUDIO_FLAG_LOW_LATENCY, |
| 84 | .tags = "" |
| 85 | }; |
| 86 | audio_config_base_t config; |
| 87 | |
| 88 | aaudio_result_t result = AAudioServiceStreamBase::open(request, configurationOutput); |
| 89 | if (result != AAUDIO_OK) { |
| 90 | ALOGE("AAudioServiceStreamBase open returned %d", result); |
| 91 | return result; |
| 92 | } |
| 93 | |
| 94 | const AAudioStreamConfiguration &configurationInput = request.getConstantConfiguration(); |
| 95 | audio_port_handle_t deviceId = configurationInput.getDeviceId(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 96 | aaudio_direction_t direction = request.getDirection(); |
| 97 | |
| 98 | // Fill in config |
jiabin | 901f65d | 2017-07-12 17:56:35 -0700 | [diff] [blame] | 99 | aaudio_format_t aaudioFormat = configurationInput.getFormat(); |
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); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 117 | mHardwareTimeOffsetNanos = OUTPUT_ESTIMATED_HARDWARE_OFFSET_NANOS; // frames at DAC later |
| 118 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 119 | } else if (direction == AAUDIO_DIRECTION_INPUT) { |
| 120 | config.channel_mask = (aaudioSamplesPerFrame == AAUDIO_UNSPECIFIED) |
| 121 | ? AUDIO_CHANNEL_IN_STEREO |
| 122 | : audio_channel_in_mask_from_count(aaudioSamplesPerFrame); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 123 | mHardwareTimeOffsetNanos = INPUT_ESTIMATED_HARDWARE_OFFSET_NANOS; // frames at ADC earlier |
| 124 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 125 | } else { |
| 126 | ALOGE("openMmapStream - invalid direction = %d", direction); |
| 127 | return AAUDIO_ERROR_ILLEGAL_ARGUMENT; |
| 128 | } |
| 129 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 130 | MmapStreamInterface::stream_direction_t streamDirection = (direction == AAUDIO_DIRECTION_OUTPUT) |
| 131 | ? MmapStreamInterface::DIRECTION_OUTPUT : MmapStreamInterface::DIRECTION_INPUT; |
| 132 | |
| 133 | // Open HAL stream. |
| 134 | status_t status = MmapStreamInterface::openMmapStream(streamDirection, |
| 135 | &attributes, |
| 136 | &config, |
| 137 | mMmapClient, |
| 138 | &deviceId, |
| 139 | mMmapStreamCallback, |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 140 | mMmapStream, |
| 141 | &mPortHandle); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 142 | if (status != OK) { |
| 143 | ALOGE("openMmapStream returned status %d", status); |
| 144 | return AAUDIO_ERROR_UNAVAILABLE; |
| 145 | } |
| 146 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 147 | if (deviceId == AAUDIO_UNSPECIFIED) { |
| 148 | ALOGW("AAudioServiceStreamMMAP::open() - openMmapStream() failed to set deviceId"); |
| 149 | } |
| 150 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 151 | // Create MMAP/NOIRQ buffer. |
| 152 | int32_t minSizeFrames = configurationInput.getBufferCapacity(); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 153 | if (minSizeFrames <= 0) { // zero will get rejected |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 154 | minSizeFrames = AAUDIO_BUFFER_CAPACITY_MIN; |
| 155 | } |
| 156 | status = mMmapStream->createMmapBuffer(minSizeFrames, &mMmapBufferinfo); |
| 157 | if (status != OK) { |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 158 | ALOGE("AAudioServiceStreamMMAP::open() - createMmapBuffer() returned status %d", |
| 159 | status); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 160 | return AAUDIO_ERROR_UNAVAILABLE; |
| 161 | } else { |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 162 | ALOGD("createMmapBuffer status = %d, buffer_size = %d, burst_size %d" |
Phil Burk | c7abac4 | 2017-07-17 11:13:37 -0700 | [diff] [blame] | 163 | ", Sharable FD: %s", |
| 164 | status, |
Eric Laurent | d51329e | 2017-06-30 16:06:16 -0700 | [diff] [blame] | 165 | abs(mMmapBufferinfo.buffer_size_frames), |
| 166 | mMmapBufferinfo.burst_size_frames, |
| 167 | mMmapBufferinfo.buffer_size_frames < 0 ? "Yes" : "No"); |
| 168 | } |
| 169 | |
| 170 | mCapacityInFrames = mMmapBufferinfo.buffer_size_frames; |
| 171 | // FIXME: the audio HAL indicates if the shared memory fd can be shared outside of audioserver |
| 172 | // by returning a negative buffer size |
| 173 | if (mCapacityInFrames < 0) { |
| 174 | // Exclusive mode is possible from any client |
| 175 | mCapacityInFrames = -mCapacityInFrames; |
| 176 | } else { |
| 177 | // exclusive mode is only possible if the final fd destination is inside audioserver |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 178 | if ((mMmapClient.clientUid != mServiceClient.clientUid) && |
Eric Laurent | d51329e | 2017-06-30 16:06:16 -0700 | [diff] [blame] | 179 | configurationInput.getSharingMode() == AAUDIO_SHARING_MODE_EXCLUSIVE) { |
| 180 | // Fallback is handled by caller but indicate what is possible in case |
| 181 | // this is used in the future |
| 182 | configurationOutput.setSharingMode(AAUDIO_SHARING_MODE_SHARED); |
| 183 | return AAUDIO_ERROR_UNAVAILABLE; |
| 184 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | // Get information about the stream and pass it back to the caller. |
| 188 | mSamplesPerFrame = (direction == AAUDIO_DIRECTION_OUTPUT) |
| 189 | ? audio_channel_count_from_out_mask(config.channel_mask) |
| 190 | : audio_channel_count_from_in_mask(config.channel_mask); |
| 191 | |
Phil Burk | e72481c | 2017-08-08 13:20:45 -0700 | [diff] [blame] | 192 | // AAudio creates a copy of this FD and retains ownership of the copy. |
| 193 | // Assume that AudioFlinger will close the original shared_memory_fd. |
| 194 | mAudioDataFileDescriptor.reset(dup(mMmapBufferinfo.shared_memory_fd)); |
| 195 | if (mAudioDataFileDescriptor.get() == -1) { |
| 196 | ALOGE("AAudioServiceStreamMMAP::open() - could not dup shared_memory_fd"); |
| 197 | return AAUDIO_ERROR_INTERNAL; // TODO review |
| 198 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 199 | mFramesPerBurst = mMmapBufferinfo.burst_size_frames; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 200 | mAudioFormat = AAudioConvert_androidToAAudioDataFormat(config.format); |
| 201 | mSampleRate = config.sample_rate; |
| 202 | |
Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 203 | // Scale up the burst size to meet the minimum equivalent in microseconds. |
| 204 | // This is to avoid waking the CPU too often when the HW burst is very small |
| 205 | // or at high sample rates. |
| 206 | int32_t burstMinMicros = AAudioProperty_getHardwareBurstMinMicros(); |
| 207 | int32_t burstMicros = 0; |
| 208 | do { |
| 209 | if (burstMicros > 0) { // skip first loop |
| 210 | mFramesPerBurst *= 2; |
| 211 | } |
| 212 | burstMicros = mFramesPerBurst * static_cast<int64_t>(1000000) / mSampleRate; |
| 213 | } while (burstMicros < burstMinMicros); |
| 214 | |
| 215 | ALOGD("AAudioServiceStreamMMAP::open() original burst = %d, minMicros = %d, final burst = %d\n", |
| 216 | mMmapBufferinfo.burst_size_frames, burstMinMicros, mFramesPerBurst); |
| 217 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 218 | ALOGD("AAudioServiceStreamMMAP::open() actual rate = %d, channels = %d, deviceId = %d\n", |
| 219 | mSampleRate, mSamplesPerFrame, deviceId); |
| 220 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 221 | // Fill in AAudioStreamConfiguration |
| 222 | configurationOutput.setSampleRate(mSampleRate); |
| 223 | configurationOutput.setSamplesPerFrame(mSamplesPerFrame); |
jiabin | 901f65d | 2017-07-12 17:56:35 -0700 | [diff] [blame] | 224 | configurationOutput.setFormat(mAudioFormat); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 225 | configurationOutput.setDeviceId(deviceId); |
| 226 | |
Phil Burk | 5a26e66 | 2017-07-07 12:44:48 -0700 | [diff] [blame] | 227 | setState(AAUDIO_STREAM_STATE_OPEN); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 228 | return AAUDIO_OK; |
| 229 | } |
| 230 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 231 | /** |
| 232 | * Start the flow of data. |
| 233 | */ |
| 234 | aaudio_result_t AAudioServiceStreamMMAP::start() { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 235 | if (isRunning()) { |
| 236 | return AAUDIO_OK; |
| 237 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 238 | if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 239 | aaudio_result_t result; |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 240 | status_t status = mMmapStream->start(mServiceClient, &mPortHandle); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 241 | if (status != OK) { |
| 242 | ALOGE("AAudioServiceStreamMMAP::start() mMmapStream->start() returned %d", status); |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 243 | disconnect(); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 244 | result = AAudioConvert_androidToAAudioResult(status); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 245 | } else { |
| 246 | result = AAudioServiceStreamBase::start(); |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 247 | if (!mInService && result == AAUDIO_OK) { |
| 248 | startClient(mMmapClient, &mClientHandle); |
| 249 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 250 | } |
| 251 | return result; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Stop the flow of data such that start() can resume with loss of data. |
| 256 | */ |
| 257 | aaudio_result_t AAudioServiceStreamMMAP::pause() { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 258 | if (!isRunning()) { |
| 259 | return AAUDIO_OK; |
| 260 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 261 | if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 262 | aaudio_result_t result1 = AAudioServiceStreamBase::pause(); |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 263 | if (!mInService) { |
| 264 | stopClient(mClientHandle); |
| 265 | } |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 266 | status_t status = mMmapStream->stop(mPortHandle); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 267 | mFramesRead.reset32(); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 268 | return (result1 != AAUDIO_OK) ? result1 : AAudioConvert_androidToAAudioResult(status); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 269 | } |
| 270 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 271 | aaudio_result_t AAudioServiceStreamMMAP::stop() { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 272 | if (!isRunning()) { |
| 273 | return AAUDIO_OK; |
| 274 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 275 | if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 276 | aaudio_result_t result1 = AAudioServiceStreamBase::stop(); |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 277 | if (!mInService) { |
| 278 | stopClient(mClientHandle); |
| 279 | } |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 280 | aaudio_result_t status = mMmapStream->stop(mPortHandle); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 281 | mFramesRead.reset32(); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 282 | return (result1 != AAUDIO_OK) ? result1 : AAudioConvert_androidToAAudioResult(status); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 285 | /** |
| 286 | * Discard any data held by the underlying HAL or Service. |
| 287 | */ |
| 288 | aaudio_result_t AAudioServiceStreamMMAP::flush() { |
| 289 | if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL; |
| 290 | // TODO how do we flush an MMAP/NOIRQ buffer? sync pointers? |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 291 | return AAudioServiceStreamBase::flush();; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 292 | } |
| 293 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 294 | aaudio_result_t AAudioServiceStreamMMAP::startClient(const android::AudioClient& client, |
| 295 | audio_port_handle_t *clientHandle) { |
| 296 | return AAudioConvert_androidToAAudioResult(mMmapStream->start(client, clientHandle)); |
| 297 | } |
| 298 | |
| 299 | aaudio_result_t AAudioServiceStreamMMAP::stopClient(audio_port_handle_t clientHandle) { |
| 300 | return AAudioConvert_androidToAAudioResult(mMmapStream->stop(clientHandle)); |
| 301 | } |
| 302 | |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 303 | // Get free-running DSP or DMA hardware position from the HAL. |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 304 | aaudio_result_t AAudioServiceStreamMMAP::getFreeRunningPosition(int64_t *positionFrames, |
| 305 | int64_t *timeNanos) { |
| 306 | struct audio_mmap_position position; |
| 307 | if (mMmapStream == nullptr) { |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 308 | disconnect(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 309 | return AAUDIO_ERROR_NULL; |
| 310 | } |
| 311 | status_t status = mMmapStream->getMmapPosition(&position); |
Phil Burk | 940083c | 2017-07-17 17:00:02 -0700 | [diff] [blame] | 312 | aaudio_result_t result = AAudioConvert_androidToAAudioResult(status); |
| 313 | if (result == AAUDIO_ERROR_UNAVAILABLE) { |
| 314 | ALOGW("sendCurrentTimestamp(): getMmapPosition() has no position data yet"); |
| 315 | } else if (result != AAUDIO_OK) { |
| 316 | ALOGE("sendCurrentTimestamp(): getMmapPosition() returned status %d", status); |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 317 | disconnect(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 318 | } else { |
| 319 | mFramesRead.update32(position.position_frames); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 320 | |
| 321 | Timestamp timestamp(mFramesRead.get(), position.time_nanoseconds); |
| 322 | mAtomicTimestamp.write(timestamp); |
| 323 | *positionFrames = timestamp.getPosition(); |
| 324 | *timeNanos = timestamp.getNanoseconds(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 325 | } |
Phil Burk | 940083c | 2017-07-17 17:00:02 -0700 | [diff] [blame] | 326 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 327 | } |
| 328 | |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 329 | // Get timestamp that was written by getFreeRunningPosition() |
| 330 | aaudio_result_t AAudioServiceStreamMMAP::getHardwareTimestamp(int64_t *positionFrames, |
| 331 | int64_t *timeNanos) { |
| 332 | // TODO Get presentation timestamp from the HAL |
| 333 | if (mAtomicTimestamp.isValid()) { |
| 334 | Timestamp timestamp = mAtomicTimestamp.read(); |
| 335 | *positionFrames = timestamp.getPosition(); |
| 336 | *timeNanos = timestamp.getNanoseconds() + mHardwareTimeOffsetNanos; |
| 337 | return AAUDIO_OK; |
| 338 | } else { |
| 339 | return AAUDIO_ERROR_UNAVAILABLE; |
| 340 | } |
| 341 | } |
| 342 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 343 | void AAudioServiceStreamMMAP::onTearDown() { |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 344 | ALOGD("AAudioServiceStreamMMAP::onTearDown() called"); |
| 345 | disconnect(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 346 | }; |
| 347 | |
| 348 | void AAudioServiceStreamMMAP::onVolumeChanged(audio_channel_mask_t channels, |
| 349 | android::Vector<float> values) { |
| 350 | // TODO do we really need a different volume for each channel? |
| 351 | float volume = values[0]; |
| 352 | ALOGD("AAudioServiceStreamMMAP::onVolumeChanged() volume[0] = %f", volume); |
| 353 | sendServiceEvent(AAUDIO_SERVICE_EVENT_VOLUME, volume); |
| 354 | }; |
| 355 | |
| 356 | void AAudioServiceStreamMMAP::onRoutingChanged(audio_port_handle_t deviceId) { |
| 357 | ALOGD("AAudioServiceStreamMMAP::onRoutingChanged() called with %d, old = %d", |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 358 | deviceId, mDeviceId); |
| 359 | if (mDeviceId != AUDIO_PORT_HANDLE_NONE && mDeviceId != deviceId) { |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 360 | disconnect(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 361 | } |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 362 | mDeviceId = deviceId; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 363 | }; |
| 364 | |
| 365 | /** |
| 366 | * Get an immutable description of the data queue from the HAL. |
| 367 | */ |
| 368 | aaudio_result_t AAudioServiceStreamMMAP::getDownDataDescription(AudioEndpointParcelable &parcelable) |
| 369 | { |
| 370 | // Gather information on the data queue based on HAL info. |
| 371 | int32_t bytesPerFrame = calculateBytesPerFrame(); |
| 372 | int32_t capacityInBytes = mCapacityInFrames * bytesPerFrame; |
| 373 | int fdIndex = parcelable.addFileDescriptor(mAudioDataFileDescriptor, capacityInBytes); |
| 374 | parcelable.mDownDataQueueParcelable.setupMemory(fdIndex, 0, capacityInBytes); |
| 375 | parcelable.mDownDataQueueParcelable.setBytesPerFrame(bytesPerFrame); |
| 376 | parcelable.mDownDataQueueParcelable.setFramesPerBurst(mFramesPerBurst); |
| 377 | parcelable.mDownDataQueueParcelable.setCapacityInFrames(mCapacityInFrames); |
| 378 | return AAUDIO_OK; |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 379 | } |