| 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, | 
 | 79 |         .source = AUDIO_SOURCE_DEFAULT, | 
 | 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 | 71f35bb | 2017-04-13 16:05:07 -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 | 942bdc0 | 2017-05-03 11:36:50 -0700 | [diff] [blame^] | 174 |     ALOGV("AAudioServiceStreamMMAP::open LEAK? mAudioDataFileDescriptor = %d\n", | 
 | 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 | 4c5129b | 2017-04-28 15:17:32 -0700 | [diff] [blame] | 181 |     ALOGD("AAudioServiceStreamMMAP::open() got devId = %d, sRate = %d", | 
 | 182 |           deviceId, config.sample_rate); | 
 | 183 |  | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 184 |     // Fill in AAudioStreamConfiguration | 
 | 185 |     configurationOutput.setSampleRate(mSampleRate); | 
 | 186 |     configurationOutput.setSamplesPerFrame(mSamplesPerFrame); | 
 | 187 |     configurationOutput.setAudioFormat(mAudioFormat); | 
 | 188 |     configurationOutput.setDeviceId(deviceId); | 
 | 189 |  | 
 | 190 |     return AAUDIO_OK; | 
 | 191 | } | 
 | 192 |  | 
 | 193 |  | 
 | 194 | /** | 
 | 195 |  * Start the flow of data. | 
 | 196 |  */ | 
 | 197 | aaudio_result_t AAudioServiceStreamMMAP::start() { | 
 | 198 |     if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL; | 
 | 199 |     aaudio_result_t result = mMmapStream->start(mMmapClient, &mPortHandle); | 
 | 200 |     if (result != AAUDIO_OK) { | 
 | 201 |         ALOGE("AAudioServiceStreamMMAP::start() mMmapStream->start() returned %d", result); | 
 | 202 |         processError(); | 
 | 203 |     } else { | 
 | 204 |         result = AAudioServiceStreamBase::start(); | 
 | 205 |     } | 
 | 206 |     return result; | 
 | 207 | } | 
 | 208 |  | 
 | 209 | /** | 
 | 210 |  * Stop the flow of data such that start() can resume with loss of data. | 
 | 211 |  */ | 
 | 212 | aaudio_result_t AAudioServiceStreamMMAP::pause() { | 
 | 213 |     if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL; | 
 | 214 |  | 
 | 215 |     aaudio_result_t result1 = AAudioServiceStreamBase::pause(); | 
 | 216 |     aaudio_result_t result2 = mMmapStream->stop(mPortHandle); | 
 | 217 |     mFramesRead.reset32(); | 
 | 218 |     return (result1 != AAUDIO_OK) ? result1 : result2; | 
 | 219 | } | 
 | 220 |  | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 221 | aaudio_result_t AAudioServiceStreamMMAP::stop() { | 
 | 222 |     if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL; | 
 | 223 |  | 
 | 224 |     aaudio_result_t result1 = AAudioServiceStreamBase::stop(); | 
 | 225 |     aaudio_result_t result2 = mMmapStream->stop(mPortHandle); | 
 | 226 |     mFramesRead.reset32(); | 
 | 227 |     return (result1 != AAUDIO_OK) ? result1 : result2; | 
 | 228 | } | 
 | 229 |  | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 230 | /** | 
 | 231 |  *  Discard any data held by the underlying HAL or Service. | 
 | 232 |  */ | 
 | 233 | aaudio_result_t AAudioServiceStreamMMAP::flush() { | 
 | 234 |     if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL; | 
 | 235 |     // TODO how do we flush an MMAP/NOIRQ buffer? sync pointers? | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 236 |     ALOGD("AAudioServiceStreamMMAP::flush() send AAUDIO_SERVICE_EVENT_FLUSHED"); | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 237 |     sendServiceEvent(AAUDIO_SERVICE_EVENT_FLUSHED); | 
 | 238 |     mState = AAUDIO_STREAM_STATE_FLUSHED; | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 239 |     return AAudioServiceStreamBase::flush();; | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 240 | } | 
 | 241 |  | 
 | 242 |  | 
 | 243 | aaudio_result_t AAudioServiceStreamMMAP::getFreeRunningPosition(int64_t *positionFrames, | 
 | 244 |                                                                 int64_t *timeNanos) { | 
 | 245 |     struct audio_mmap_position position; | 
 | 246 |     if (mMmapStream == nullptr) { | 
 | 247 |         processError(); | 
 | 248 |         return AAUDIO_ERROR_NULL; | 
 | 249 |     } | 
 | 250 |     status_t status = mMmapStream->getMmapPosition(&position); | 
 | 251 |     if (status != OK) { | 
 | 252 |         ALOGE("sendCurrentTimestamp(): getMmapPosition() returned %d", status); | 
 | 253 |         processError(); | 
 | 254 |         return AAudioConvert_androidToAAudioResult(status); | 
 | 255 |     } else { | 
 | 256 |         mFramesRead.update32(position.position_frames); | 
 | 257 |         *positionFrames = mFramesRead.get(); | 
 | 258 |         *timeNanos = position.time_nanoseconds; | 
 | 259 |     } | 
 | 260 |     return AAUDIO_OK; | 
 | 261 | } | 
 | 262 |  | 
 | 263 | void AAudioServiceStreamMMAP::onTearDown() { | 
 | 264 |     ALOGD("AAudioServiceStreamMMAP::onTearDown() called - TODO"); | 
 | 265 | }; | 
 | 266 |  | 
 | 267 | void AAudioServiceStreamMMAP::onVolumeChanged(audio_channel_mask_t channels, | 
 | 268 |                      android::Vector<float> values) { | 
 | 269 |     // TODO do we really need a different volume for each channel? | 
 | 270 |     float volume = values[0]; | 
 | 271 |     ALOGD("AAudioServiceStreamMMAP::onVolumeChanged() volume[0] = %f", volume); | 
 | 272 |     sendServiceEvent(AAUDIO_SERVICE_EVENT_VOLUME, volume); | 
 | 273 | }; | 
 | 274 |  | 
 | 275 | void AAudioServiceStreamMMAP::onRoutingChanged(audio_port_handle_t deviceId) { | 
 | 276 |     ALOGD("AAudioServiceStreamMMAP::onRoutingChanged() called with %d, old = %d", | 
 | 277 |           deviceId, mPortHandle); | 
 | 278 |     if (mPortHandle > 0 && mPortHandle != deviceId) { | 
 | 279 |         sendServiceEvent(AAUDIO_SERVICE_EVENT_DISCONNECTED); | 
 | 280 |     } | 
 | 281 |     mPortHandle = deviceId; | 
 | 282 | }; | 
 | 283 |  | 
 | 284 | /** | 
 | 285 |  * Get an immutable description of the data queue from the HAL. | 
 | 286 |  */ | 
 | 287 | aaudio_result_t AAudioServiceStreamMMAP::getDownDataDescription(AudioEndpointParcelable &parcelable) | 
 | 288 | { | 
 | 289 |     // Gather information on the data queue based on HAL info. | 
 | 290 |     int32_t bytesPerFrame = calculateBytesPerFrame(); | 
 | 291 |     int32_t capacityInBytes = mCapacityInFrames * bytesPerFrame; | 
 | 292 |     int fdIndex = parcelable.addFileDescriptor(mAudioDataFileDescriptor, capacityInBytes); | 
 | 293 |     parcelable.mDownDataQueueParcelable.setupMemory(fdIndex, 0, capacityInBytes); | 
 | 294 |     parcelable.mDownDataQueueParcelable.setBytesPerFrame(bytesPerFrame); | 
 | 295 |     parcelable.mDownDataQueueParcelable.setFramesPerBurst(mFramesPerBurst); | 
 | 296 |     parcelable.mDownDataQueueParcelable.setCapacityInFrames(mCapacityInFrames); | 
 | 297 |     return AAUDIO_OK; | 
 | 298 | } |