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 | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 63 | return AAudioServiceStreamBase::close(); |
| 64 | } |
| 65 | |
| 66 | // Open stream on HAL and pass information about the shared memory buffer back to the client. |
| 67 | aaudio_result_t AAudioServiceStreamMMAP::open(const aaudio::AAudioStreamRequest &request, |
| 68 | aaudio::AAudioStreamConfiguration &configurationOutput) { |
| 69 | const audio_attributes_t attributes = { |
| 70 | .content_type = AUDIO_CONTENT_TYPE_MUSIC, |
| 71 | .usage = AUDIO_USAGE_MEDIA, |
| 72 | .source = AUDIO_SOURCE_DEFAULT, |
| 73 | .flags = AUDIO_FLAG_LOW_LATENCY, |
| 74 | .tags = "" |
| 75 | }; |
| 76 | audio_config_base_t config; |
| 77 | |
| 78 | aaudio_result_t result = AAudioServiceStreamBase::open(request, configurationOutput); |
| 79 | if (result != AAUDIO_OK) { |
| 80 | ALOGE("AAudioServiceStreamBase open returned %d", result); |
| 81 | return result; |
| 82 | } |
| 83 | |
| 84 | const AAudioStreamConfiguration &configurationInput = request.getConstantConfiguration(); |
| 85 | audio_port_handle_t deviceId = configurationInput.getDeviceId(); |
| 86 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 87 | // ALOGI("open request dump()"); |
| 88 | // request.dump(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 89 | |
| 90 | mMmapClient.clientUid = request.getUserId(); |
| 91 | mMmapClient.clientPid = request.getProcessId(); |
| 92 | aaudio_direction_t direction = request.getDirection(); |
| 93 | |
| 94 | // Fill in config |
| 95 | aaudio_audio_format_t aaudioFormat = configurationInput.getAudioFormat(); |
| 96 | if (aaudioFormat == AAUDIO_UNSPECIFIED || aaudioFormat == AAUDIO_FORMAT_PCM_FLOAT) { |
| 97 | ALOGI("open forcing use of AAUDIO_FORMAT_PCM_I16"); |
| 98 | aaudioFormat = AAUDIO_FORMAT_PCM_I16; |
| 99 | } |
| 100 | config.format = AAudioConvert_aaudioToAndroidDataFormat(aaudioFormat); |
| 101 | |
| 102 | int32_t aaudioSampleRate = configurationInput.getSampleRate(); |
| 103 | if (aaudioSampleRate == AAUDIO_UNSPECIFIED) { |
| 104 | aaudioSampleRate = AAUDIO_SAMPLE_RATE_DEFAULT; |
| 105 | } |
| 106 | config.sample_rate = aaudioSampleRate; |
| 107 | |
| 108 | int32_t aaudioSamplesPerFrame = configurationInput.getSamplesPerFrame(); |
| 109 | |
| 110 | if (direction == AAUDIO_DIRECTION_OUTPUT) { |
| 111 | config.channel_mask = (aaudioSamplesPerFrame == AAUDIO_UNSPECIFIED) |
| 112 | ? AUDIO_CHANNEL_OUT_STEREO |
| 113 | : audio_channel_out_mask_from_count(aaudioSamplesPerFrame); |
| 114 | } else if (direction == AAUDIO_DIRECTION_INPUT) { |
| 115 | config.channel_mask = (aaudioSamplesPerFrame == AAUDIO_UNSPECIFIED) |
| 116 | ? AUDIO_CHANNEL_IN_STEREO |
| 117 | : audio_channel_in_mask_from_count(aaudioSamplesPerFrame); |
| 118 | } else { |
| 119 | ALOGE("openMmapStream - invalid direction = %d", direction); |
| 120 | return AAUDIO_ERROR_ILLEGAL_ARGUMENT; |
| 121 | } |
| 122 | |
| 123 | mMmapClient.packageName.setTo(String16("aaudio_service")); // FIXME what should we do here? |
| 124 | |
| 125 | MmapStreamInterface::stream_direction_t streamDirection = (direction == AAUDIO_DIRECTION_OUTPUT) |
| 126 | ? MmapStreamInterface::DIRECTION_OUTPUT : MmapStreamInterface::DIRECTION_INPUT; |
| 127 | |
Phil Burk | 4c5129b | 2017-04-28 15:17:32 -0700 | [diff] [blame^] | 128 | ALOGD("AAudioServiceStreamMMAP::open() request devId = %d, sRate = %d", |
| 129 | deviceId, config.sample_rate); |
| 130 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 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 | |
| 144 | // Create MMAP/NOIRQ buffer. |
| 145 | int32_t minSizeFrames = configurationInput.getBufferCapacity(); |
| 146 | if (minSizeFrames == 0) { // zero will get rejected |
| 147 | minSizeFrames = AAUDIO_BUFFER_CAPACITY_MIN; |
| 148 | } |
| 149 | status = mMmapStream->createMmapBuffer(minSizeFrames, &mMmapBufferinfo); |
| 150 | if (status != OK) { |
| 151 | ALOGE("%s: createMmapBuffer() returned status %d, return AAUDIO_ERROR_UNAVAILABLE", |
| 152 | __FILE__, status); |
| 153 | return AAUDIO_ERROR_UNAVAILABLE; |
| 154 | } else { |
| 155 | ALOGD("createMmapBuffer status %d shared_address = %p buffer_size %d burst_size %d", |
| 156 | status, mMmapBufferinfo.shared_memory_address, |
| 157 | mMmapBufferinfo.buffer_size_frames, |
| 158 | mMmapBufferinfo.burst_size_frames); |
| 159 | } |
| 160 | |
| 161 | // Get information about the stream and pass it back to the caller. |
| 162 | mSamplesPerFrame = (direction == AAUDIO_DIRECTION_OUTPUT) |
| 163 | ? audio_channel_count_from_out_mask(config.channel_mask) |
| 164 | : audio_channel_count_from_in_mask(config.channel_mask); |
| 165 | |
| 166 | mAudioDataFileDescriptor = mMmapBufferinfo.shared_memory_fd; |
| 167 | mFramesPerBurst = mMmapBufferinfo.burst_size_frames; |
| 168 | mCapacityInFrames = mMmapBufferinfo.buffer_size_frames; |
| 169 | mAudioFormat = AAudioConvert_androidToAAudioDataFormat(config.format); |
| 170 | mSampleRate = config.sample_rate; |
| 171 | |
Phil Burk | 4c5129b | 2017-04-28 15:17:32 -0700 | [diff] [blame^] | 172 | ALOGD("AAudioServiceStreamMMAP::open() got devId = %d, sRate = %d", |
| 173 | deviceId, config.sample_rate); |
| 174 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 175 | // Fill in AAudioStreamConfiguration |
| 176 | configurationOutput.setSampleRate(mSampleRate); |
| 177 | configurationOutput.setSamplesPerFrame(mSamplesPerFrame); |
| 178 | configurationOutput.setAudioFormat(mAudioFormat); |
| 179 | configurationOutput.setDeviceId(deviceId); |
| 180 | |
| 181 | return AAUDIO_OK; |
| 182 | } |
| 183 | |
| 184 | |
| 185 | /** |
| 186 | * Start the flow of data. |
| 187 | */ |
| 188 | aaudio_result_t AAudioServiceStreamMMAP::start() { |
| 189 | if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL; |
| 190 | aaudio_result_t result = mMmapStream->start(mMmapClient, &mPortHandle); |
| 191 | if (result != AAUDIO_OK) { |
| 192 | ALOGE("AAudioServiceStreamMMAP::start() mMmapStream->start() returned %d", result); |
| 193 | processError(); |
| 194 | } else { |
| 195 | result = AAudioServiceStreamBase::start(); |
| 196 | } |
| 197 | return result; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Stop the flow of data such that start() can resume with loss of data. |
| 202 | */ |
| 203 | aaudio_result_t AAudioServiceStreamMMAP::pause() { |
| 204 | if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL; |
| 205 | |
| 206 | aaudio_result_t result1 = AAudioServiceStreamBase::pause(); |
| 207 | aaudio_result_t result2 = mMmapStream->stop(mPortHandle); |
| 208 | mFramesRead.reset32(); |
| 209 | return (result1 != AAUDIO_OK) ? result1 : result2; |
| 210 | } |
| 211 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 212 | aaudio_result_t AAudioServiceStreamMMAP::stop() { |
| 213 | if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL; |
| 214 | |
| 215 | aaudio_result_t result1 = AAudioServiceStreamBase::stop(); |
| 216 | aaudio_result_t result2 = mMmapStream->stop(mPortHandle); |
| 217 | mFramesRead.reset32(); |
| 218 | return (result1 != AAUDIO_OK) ? result1 : result2; |
| 219 | } |
| 220 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 221 | /** |
| 222 | * Discard any data held by the underlying HAL or Service. |
| 223 | */ |
| 224 | aaudio_result_t AAudioServiceStreamMMAP::flush() { |
| 225 | if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL; |
| 226 | // TODO how do we flush an MMAP/NOIRQ buffer? sync pointers? |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 227 | ALOGD("AAudioServiceStreamMMAP::flush() send AAUDIO_SERVICE_EVENT_FLUSHED"); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 228 | sendServiceEvent(AAUDIO_SERVICE_EVENT_FLUSHED); |
| 229 | mState = AAUDIO_STREAM_STATE_FLUSHED; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 230 | return AAudioServiceStreamBase::flush();; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | |
| 234 | aaudio_result_t AAudioServiceStreamMMAP::getFreeRunningPosition(int64_t *positionFrames, |
| 235 | int64_t *timeNanos) { |
| 236 | struct audio_mmap_position position; |
| 237 | if (mMmapStream == nullptr) { |
| 238 | processError(); |
| 239 | return AAUDIO_ERROR_NULL; |
| 240 | } |
| 241 | status_t status = mMmapStream->getMmapPosition(&position); |
| 242 | if (status != OK) { |
| 243 | ALOGE("sendCurrentTimestamp(): getMmapPosition() returned %d", status); |
| 244 | processError(); |
| 245 | return AAudioConvert_androidToAAudioResult(status); |
| 246 | } else { |
| 247 | mFramesRead.update32(position.position_frames); |
| 248 | *positionFrames = mFramesRead.get(); |
| 249 | *timeNanos = position.time_nanoseconds; |
| 250 | } |
| 251 | return AAUDIO_OK; |
| 252 | } |
| 253 | |
| 254 | void AAudioServiceStreamMMAP::onTearDown() { |
| 255 | ALOGD("AAudioServiceStreamMMAP::onTearDown() called - TODO"); |
| 256 | }; |
| 257 | |
| 258 | void AAudioServiceStreamMMAP::onVolumeChanged(audio_channel_mask_t channels, |
| 259 | android::Vector<float> values) { |
| 260 | // TODO do we really need a different volume for each channel? |
| 261 | float volume = values[0]; |
| 262 | ALOGD("AAudioServiceStreamMMAP::onVolumeChanged() volume[0] = %f", volume); |
| 263 | sendServiceEvent(AAUDIO_SERVICE_EVENT_VOLUME, volume); |
| 264 | }; |
| 265 | |
| 266 | void AAudioServiceStreamMMAP::onRoutingChanged(audio_port_handle_t deviceId) { |
| 267 | ALOGD("AAudioServiceStreamMMAP::onRoutingChanged() called with %d, old = %d", |
| 268 | deviceId, mPortHandle); |
| 269 | if (mPortHandle > 0 && mPortHandle != deviceId) { |
| 270 | sendServiceEvent(AAUDIO_SERVICE_EVENT_DISCONNECTED); |
| 271 | } |
| 272 | mPortHandle = deviceId; |
| 273 | }; |
| 274 | |
| 275 | /** |
| 276 | * Get an immutable description of the data queue from the HAL. |
| 277 | */ |
| 278 | aaudio_result_t AAudioServiceStreamMMAP::getDownDataDescription(AudioEndpointParcelable &parcelable) |
| 279 | { |
| 280 | // Gather information on the data queue based on HAL info. |
| 281 | int32_t bytesPerFrame = calculateBytesPerFrame(); |
| 282 | int32_t capacityInBytes = mCapacityInFrames * bytesPerFrame; |
| 283 | int fdIndex = parcelable.addFileDescriptor(mAudioDataFileDescriptor, capacityInBytes); |
| 284 | parcelable.mDownDataQueueParcelable.setupMemory(fdIndex, 0, capacityInBytes); |
| 285 | parcelable.mDownDataQueueParcelable.setBytesPerFrame(bytesPerFrame); |
| 286 | parcelable.mDownDataQueueParcelable.setFramesPerBurst(mFramesPerBurst); |
| 287 | parcelable.mDownDataQueueParcelable.setCapacityInFrames(mCapacityInFrames); |
| 288 | return AAUDIO_OK; |
| 289 | } |