| Andy Hung | 857d5a2 | 2015-03-26 18:46:00 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2015 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 "BufferProvider" | 
|  | 18 | //#define LOG_NDEBUG 0 | 
|  | 19 |  | 
|  | 20 | #include <audio_effects/effect_downmix.h> | 
|  | 21 | #include <audio_utils/primitives.h> | 
|  | 22 | #include <audio_utils/format.h> | 
| Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 23 | #include <media/AudioResamplerPublic.h> | 
| Andy Hung | 857d5a2 | 2015-03-26 18:46:00 -0700 | [diff] [blame] | 24 | #include <media/EffectsFactoryApi.h> | 
| Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 25 |  | 
| Andy Hung | 857d5a2 | 2015-03-26 18:46:00 -0700 | [diff] [blame] | 26 | #include <utils/Log.h> | 
|  | 27 |  | 
|  | 28 | #include "Configuration.h" | 
|  | 29 | #include "BufferProviders.h" | 
|  | 30 |  | 
|  | 31 | #ifndef ARRAY_SIZE | 
|  | 32 | #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0])) | 
|  | 33 | #endif | 
|  | 34 |  | 
|  | 35 | namespace android { | 
|  | 36 |  | 
|  | 37 | // ---------------------------------------------------------------------------- | 
|  | 38 |  | 
|  | 39 | template <typename T> | 
|  | 40 | static inline T min(const T& a, const T& b) | 
|  | 41 | { | 
|  | 42 | return a < b ? a : b; | 
|  | 43 | } | 
|  | 44 |  | 
|  | 45 | CopyBufferProvider::CopyBufferProvider(size_t inputFrameSize, | 
|  | 46 | size_t outputFrameSize, size_t bufferFrameCount) : | 
|  | 47 | mInputFrameSize(inputFrameSize), | 
|  | 48 | mOutputFrameSize(outputFrameSize), | 
|  | 49 | mLocalBufferFrameCount(bufferFrameCount), | 
|  | 50 | mLocalBufferData(NULL), | 
|  | 51 | mConsumed(0) | 
|  | 52 | { | 
|  | 53 | ALOGV("CopyBufferProvider(%p)(%zu, %zu, %zu)", this, | 
|  | 54 | inputFrameSize, outputFrameSize, bufferFrameCount); | 
|  | 55 | LOG_ALWAYS_FATAL_IF(inputFrameSize < outputFrameSize && bufferFrameCount == 0, | 
|  | 56 | "Requires local buffer if inputFrameSize(%zu) < outputFrameSize(%zu)", | 
|  | 57 | inputFrameSize, outputFrameSize); | 
|  | 58 | if (mLocalBufferFrameCount) { | 
|  | 59 | (void)posix_memalign(&mLocalBufferData, 32, mLocalBufferFrameCount * mOutputFrameSize); | 
|  | 60 | } | 
|  | 61 | mBuffer.frameCount = 0; | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | CopyBufferProvider::~CopyBufferProvider() | 
|  | 65 | { | 
|  | 66 | ALOGV("~CopyBufferProvider(%p)", this); | 
|  | 67 | if (mBuffer.frameCount != 0) { | 
|  | 68 | mTrackBufferProvider->releaseBuffer(&mBuffer); | 
|  | 69 | } | 
|  | 70 | free(mLocalBufferData); | 
|  | 71 | } | 
|  | 72 |  | 
|  | 73 | status_t CopyBufferProvider::getNextBuffer(AudioBufferProvider::Buffer *pBuffer, | 
|  | 74 | int64_t pts) | 
|  | 75 | { | 
|  | 76 | //ALOGV("CopyBufferProvider(%p)::getNextBuffer(%p (%zu), %lld)", | 
|  | 77 | //        this, pBuffer, pBuffer->frameCount, pts); | 
|  | 78 | if (mLocalBufferFrameCount == 0) { | 
|  | 79 | status_t res = mTrackBufferProvider->getNextBuffer(pBuffer, pts); | 
|  | 80 | if (res == OK) { | 
|  | 81 | copyFrames(pBuffer->raw, pBuffer->raw, pBuffer->frameCount); | 
|  | 82 | } | 
|  | 83 | return res; | 
|  | 84 | } | 
|  | 85 | if (mBuffer.frameCount == 0) { | 
|  | 86 | mBuffer.frameCount = pBuffer->frameCount; | 
|  | 87 | status_t res = mTrackBufferProvider->getNextBuffer(&mBuffer, pts); | 
|  | 88 | // At one time an upstream buffer provider had | 
|  | 89 | // res == OK and mBuffer.frameCount == 0, doesn't seem to happen now 7/18/2014. | 
|  | 90 | // | 
|  | 91 | // By API spec, if res != OK, then mBuffer.frameCount == 0. | 
|  | 92 | // but there may be improper implementations. | 
|  | 93 | ALOG_ASSERT(res == OK || mBuffer.frameCount == 0); | 
|  | 94 | if (res != OK || mBuffer.frameCount == 0) { // not needed by API spec, but to be safe. | 
|  | 95 | pBuffer->raw = NULL; | 
|  | 96 | pBuffer->frameCount = 0; | 
|  | 97 | return res; | 
|  | 98 | } | 
|  | 99 | mConsumed = 0; | 
|  | 100 | } | 
|  | 101 | ALOG_ASSERT(mConsumed < mBuffer.frameCount); | 
|  | 102 | size_t count = min(mLocalBufferFrameCount, mBuffer.frameCount - mConsumed); | 
|  | 103 | count = min(count, pBuffer->frameCount); | 
|  | 104 | pBuffer->raw = mLocalBufferData; | 
|  | 105 | pBuffer->frameCount = count; | 
|  | 106 | copyFrames(pBuffer->raw, (uint8_t*)mBuffer.raw + mConsumed * mInputFrameSize, | 
|  | 107 | pBuffer->frameCount); | 
|  | 108 | return OK; | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | void CopyBufferProvider::releaseBuffer(AudioBufferProvider::Buffer *pBuffer) | 
|  | 112 | { | 
|  | 113 | //ALOGV("CopyBufferProvider(%p)::releaseBuffer(%p(%zu))", | 
|  | 114 | //        this, pBuffer, pBuffer->frameCount); | 
|  | 115 | if (mLocalBufferFrameCount == 0) { | 
|  | 116 | mTrackBufferProvider->releaseBuffer(pBuffer); | 
|  | 117 | return; | 
|  | 118 | } | 
|  | 119 | // LOG_ALWAYS_FATAL_IF(pBuffer->frameCount == 0, "Invalid framecount"); | 
|  | 120 | mConsumed += pBuffer->frameCount; // TODO: update for efficiency to reuse existing content | 
|  | 121 | if (mConsumed != 0 && mConsumed >= mBuffer.frameCount) { | 
|  | 122 | mTrackBufferProvider->releaseBuffer(&mBuffer); | 
|  | 123 | ALOG_ASSERT(mBuffer.frameCount == 0); | 
|  | 124 | } | 
|  | 125 | pBuffer->raw = NULL; | 
|  | 126 | pBuffer->frameCount = 0; | 
|  | 127 | } | 
|  | 128 |  | 
|  | 129 | void CopyBufferProvider::reset() | 
|  | 130 | { | 
|  | 131 | if (mBuffer.frameCount != 0) { | 
|  | 132 | mTrackBufferProvider->releaseBuffer(&mBuffer); | 
|  | 133 | } | 
|  | 134 | mConsumed = 0; | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | DownmixerBufferProvider::DownmixerBufferProvider( | 
|  | 138 | audio_channel_mask_t inputChannelMask, | 
|  | 139 | audio_channel_mask_t outputChannelMask, audio_format_t format, | 
|  | 140 | uint32_t sampleRate, int32_t sessionId, size_t bufferFrameCount) : | 
|  | 141 | CopyBufferProvider( | 
|  | 142 | audio_bytes_per_sample(format) * audio_channel_count_from_out_mask(inputChannelMask), | 
|  | 143 | audio_bytes_per_sample(format) * audio_channel_count_from_out_mask(outputChannelMask), | 
|  | 144 | bufferFrameCount)  // set bufferFrameCount to 0 to do in-place | 
|  | 145 | { | 
|  | 146 | ALOGV("DownmixerBufferProvider(%p)(%#x, %#x, %#x %u %d)", | 
|  | 147 | this, inputChannelMask, outputChannelMask, format, | 
|  | 148 | sampleRate, sessionId); | 
|  | 149 | if (!sIsMultichannelCapable | 
|  | 150 | || EffectCreate(&sDwnmFxDesc.uuid, | 
|  | 151 | sessionId, | 
|  | 152 | SESSION_ID_INVALID_AND_IGNORED, | 
|  | 153 | &mDownmixHandle) != 0) { | 
|  | 154 | ALOGE("DownmixerBufferProvider() error creating downmixer effect"); | 
|  | 155 | mDownmixHandle = NULL; | 
|  | 156 | return; | 
|  | 157 | } | 
|  | 158 | // channel input configuration will be overridden per-track | 
|  | 159 | mDownmixConfig.inputCfg.channels = inputChannelMask;   // FIXME: Should be bits | 
|  | 160 | mDownmixConfig.outputCfg.channels = outputChannelMask; // FIXME: should be bits | 
|  | 161 | mDownmixConfig.inputCfg.format = format; | 
|  | 162 | mDownmixConfig.outputCfg.format = format; | 
|  | 163 | mDownmixConfig.inputCfg.samplingRate = sampleRate; | 
|  | 164 | mDownmixConfig.outputCfg.samplingRate = sampleRate; | 
|  | 165 | mDownmixConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ; | 
|  | 166 | mDownmixConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE; | 
|  | 167 | // input and output buffer provider, and frame count will not be used as the downmix effect | 
|  | 168 | // process() function is called directly (see DownmixerBufferProvider::getNextBuffer()) | 
|  | 169 | mDownmixConfig.inputCfg.mask = EFFECT_CONFIG_SMP_RATE | EFFECT_CONFIG_CHANNELS | | 
|  | 170 | EFFECT_CONFIG_FORMAT | EFFECT_CONFIG_ACC_MODE; | 
|  | 171 | mDownmixConfig.outputCfg.mask = mDownmixConfig.inputCfg.mask; | 
|  | 172 |  | 
|  | 173 | int cmdStatus; | 
|  | 174 | uint32_t replySize = sizeof(int); | 
|  | 175 |  | 
|  | 176 | // Configure downmixer | 
|  | 177 | status_t status = (*mDownmixHandle)->command(mDownmixHandle, | 
|  | 178 | EFFECT_CMD_SET_CONFIG /*cmdCode*/, sizeof(effect_config_t) /*cmdSize*/, | 
|  | 179 | &mDownmixConfig /*pCmdData*/, | 
|  | 180 | &replySize, &cmdStatus /*pReplyData*/); | 
|  | 181 | if (status != 0 || cmdStatus != 0) { | 
|  | 182 | ALOGE("DownmixerBufferProvider() error %d cmdStatus %d while configuring downmixer", | 
|  | 183 | status, cmdStatus); | 
|  | 184 | EffectRelease(mDownmixHandle); | 
|  | 185 | mDownmixHandle = NULL; | 
|  | 186 | return; | 
|  | 187 | } | 
|  | 188 |  | 
|  | 189 | // Enable downmixer | 
|  | 190 | replySize = sizeof(int); | 
|  | 191 | status = (*mDownmixHandle)->command(mDownmixHandle, | 
|  | 192 | EFFECT_CMD_ENABLE /*cmdCode*/, 0 /*cmdSize*/, NULL /*pCmdData*/, | 
|  | 193 | &replySize, &cmdStatus /*pReplyData*/); | 
|  | 194 | if (status != 0 || cmdStatus != 0) { | 
|  | 195 | ALOGE("DownmixerBufferProvider() error %d cmdStatus %d while enabling downmixer", | 
|  | 196 | status, cmdStatus); | 
|  | 197 | EffectRelease(mDownmixHandle); | 
|  | 198 | mDownmixHandle = NULL; | 
|  | 199 | return; | 
|  | 200 | } | 
|  | 201 |  | 
|  | 202 | // Set downmix type | 
|  | 203 | // parameter size rounded for padding on 32bit boundary | 
|  | 204 | const int psizePadded = ((sizeof(downmix_params_t) - 1)/sizeof(int) + 1) * sizeof(int); | 
|  | 205 | const int downmixParamSize = | 
|  | 206 | sizeof(effect_param_t) + psizePadded + sizeof(downmix_type_t); | 
|  | 207 | effect_param_t * const param = (effect_param_t *) malloc(downmixParamSize); | 
|  | 208 | param->psize = sizeof(downmix_params_t); | 
|  | 209 | const downmix_params_t downmixParam = DOWNMIX_PARAM_TYPE; | 
|  | 210 | memcpy(param->data, &downmixParam, param->psize); | 
|  | 211 | const downmix_type_t downmixType = DOWNMIX_TYPE_FOLD; | 
|  | 212 | param->vsize = sizeof(downmix_type_t); | 
|  | 213 | memcpy(param->data + psizePadded, &downmixType, param->vsize); | 
|  | 214 | replySize = sizeof(int); | 
|  | 215 | status = (*mDownmixHandle)->command(mDownmixHandle, | 
|  | 216 | EFFECT_CMD_SET_PARAM /* cmdCode */, downmixParamSize /* cmdSize */, | 
|  | 217 | param /*pCmdData*/, &replySize, &cmdStatus /*pReplyData*/); | 
|  | 218 | free(param); | 
|  | 219 | if (status != 0 || cmdStatus != 0) { | 
|  | 220 | ALOGE("DownmixerBufferProvider() error %d cmdStatus %d while setting downmix type", | 
|  | 221 | status, cmdStatus); | 
|  | 222 | EffectRelease(mDownmixHandle); | 
|  | 223 | mDownmixHandle = NULL; | 
|  | 224 | return; | 
|  | 225 | } | 
|  | 226 | ALOGV("DownmixerBufferProvider() downmix type set to %d", (int) downmixType); | 
|  | 227 | } | 
|  | 228 |  | 
|  | 229 | DownmixerBufferProvider::~DownmixerBufferProvider() | 
|  | 230 | { | 
|  | 231 | ALOGV("~DownmixerBufferProvider (%p)", this); | 
|  | 232 | EffectRelease(mDownmixHandle); | 
|  | 233 | mDownmixHandle = NULL; | 
|  | 234 | } | 
|  | 235 |  | 
|  | 236 | void DownmixerBufferProvider::copyFrames(void *dst, const void *src, size_t frames) | 
|  | 237 | { | 
|  | 238 | mDownmixConfig.inputCfg.buffer.frameCount = frames; | 
|  | 239 | mDownmixConfig.inputCfg.buffer.raw = const_cast<void *>(src); | 
|  | 240 | mDownmixConfig.outputCfg.buffer.frameCount = frames; | 
|  | 241 | mDownmixConfig.outputCfg.buffer.raw = dst; | 
|  | 242 | // may be in-place if src == dst. | 
|  | 243 | status_t res = (*mDownmixHandle)->process(mDownmixHandle, | 
|  | 244 | &mDownmixConfig.inputCfg.buffer, &mDownmixConfig.outputCfg.buffer); | 
|  | 245 | ALOGE_IF(res != OK, "DownmixBufferProvider error %d", res); | 
|  | 246 | } | 
|  | 247 |  | 
|  | 248 | /* call once in a pthread_once handler. */ | 
|  | 249 | /*static*/ status_t DownmixerBufferProvider::init() | 
|  | 250 | { | 
|  | 251 | // find multichannel downmix effect if we have to play multichannel content | 
|  | 252 | uint32_t numEffects = 0; | 
|  | 253 | int ret = EffectQueryNumberEffects(&numEffects); | 
|  | 254 | if (ret != 0) { | 
|  | 255 | ALOGE("AudioMixer() error %d querying number of effects", ret); | 
|  | 256 | return NO_INIT; | 
|  | 257 | } | 
|  | 258 | ALOGV("EffectQueryNumberEffects() numEffects=%d", numEffects); | 
|  | 259 |  | 
|  | 260 | for (uint32_t i = 0 ; i < numEffects ; i++) { | 
|  | 261 | if (EffectQueryEffect(i, &sDwnmFxDesc) == 0) { | 
|  | 262 | ALOGV("effect %d is called %s", i, sDwnmFxDesc.name); | 
|  | 263 | if (memcmp(&sDwnmFxDesc.type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0) { | 
|  | 264 | ALOGI("found effect \"%s\" from %s", | 
|  | 265 | sDwnmFxDesc.name, sDwnmFxDesc.implementor); | 
|  | 266 | sIsMultichannelCapable = true; | 
|  | 267 | break; | 
|  | 268 | } | 
|  | 269 | } | 
|  | 270 | } | 
|  | 271 | ALOGW_IF(!sIsMultichannelCapable, "unable to find downmix effect"); | 
|  | 272 | return NO_INIT; | 
|  | 273 | } | 
|  | 274 |  | 
|  | 275 | /*static*/ bool DownmixerBufferProvider::sIsMultichannelCapable = false; | 
|  | 276 | /*static*/ effect_descriptor_t DownmixerBufferProvider::sDwnmFxDesc; | 
|  | 277 |  | 
|  | 278 | RemixBufferProvider::RemixBufferProvider(audio_channel_mask_t inputChannelMask, | 
|  | 279 | audio_channel_mask_t outputChannelMask, audio_format_t format, | 
|  | 280 | size_t bufferFrameCount) : | 
|  | 281 | CopyBufferProvider( | 
|  | 282 | audio_bytes_per_sample(format) | 
|  | 283 | * audio_channel_count_from_out_mask(inputChannelMask), | 
|  | 284 | audio_bytes_per_sample(format) | 
|  | 285 | * audio_channel_count_from_out_mask(outputChannelMask), | 
|  | 286 | bufferFrameCount), | 
|  | 287 | mFormat(format), | 
|  | 288 | mSampleSize(audio_bytes_per_sample(format)), | 
|  | 289 | mInputChannels(audio_channel_count_from_out_mask(inputChannelMask)), | 
|  | 290 | mOutputChannels(audio_channel_count_from_out_mask(outputChannelMask)) | 
|  | 291 | { | 
|  | 292 | ALOGV("RemixBufferProvider(%p)(%#x, %#x, %#x) %zu %zu", | 
|  | 293 | this, format, inputChannelMask, outputChannelMask, | 
|  | 294 | mInputChannels, mOutputChannels); | 
| Andy Hung | 18aa270 | 2015-05-05 23:48:38 -0700 | [diff] [blame^] | 295 | (void) memcpy_by_index_array_initialization_from_channel_mask( | 
|  | 296 | mIdxAry, ARRAY_SIZE(mIdxAry), outputChannelMask, inputChannelMask); | 
| Andy Hung | 857d5a2 | 2015-03-26 18:46:00 -0700 | [diff] [blame] | 297 | } | 
|  | 298 |  | 
|  | 299 | void RemixBufferProvider::copyFrames(void *dst, const void *src, size_t frames) | 
|  | 300 | { | 
|  | 301 | memcpy_by_index_array(dst, mOutputChannels, | 
|  | 302 | src, mInputChannels, mIdxAry, mSampleSize, frames); | 
|  | 303 | } | 
|  | 304 |  | 
|  | 305 | ReformatBufferProvider::ReformatBufferProvider(int32_t channelCount, | 
|  | 306 | audio_format_t inputFormat, audio_format_t outputFormat, | 
|  | 307 | size_t bufferFrameCount) : | 
|  | 308 | CopyBufferProvider( | 
|  | 309 | channelCount * audio_bytes_per_sample(inputFormat), | 
|  | 310 | channelCount * audio_bytes_per_sample(outputFormat), | 
|  | 311 | bufferFrameCount), | 
|  | 312 | mChannelCount(channelCount), | 
|  | 313 | mInputFormat(inputFormat), | 
|  | 314 | mOutputFormat(outputFormat) | 
|  | 315 | { | 
|  | 316 | ALOGV("ReformatBufferProvider(%p)(%u, %#x, %#x)", | 
|  | 317 | this, channelCount, inputFormat, outputFormat); | 
|  | 318 | } | 
|  | 319 |  | 
|  | 320 | void ReformatBufferProvider::copyFrames(void *dst, const void *src, size_t frames) | 
|  | 321 | { | 
|  | 322 | memcpy_by_audio_format(dst, mOutputFormat, src, mInputFormat, frames * mChannelCount); | 
|  | 323 | } | 
|  | 324 |  | 
| Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 325 | TimestretchBufferProvider::TimestretchBufferProvider(int32_t channelCount, | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 326 | audio_format_t format, uint32_t sampleRate, const AudioPlaybackRate &playbackRate) : | 
| Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 327 | mChannelCount(channelCount), | 
|  | 328 | mFormat(format), | 
|  | 329 | mSampleRate(sampleRate), | 
|  | 330 | mFrameSize(channelCount * audio_bytes_per_sample(format)), | 
| Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 331 | mLocalBufferFrameCount(0), | 
|  | 332 | mLocalBufferData(NULL), | 
| Ricardo Garcia | f097cae | 2015-04-13 12:17:21 -0700 | [diff] [blame] | 333 | mRemaining(0), | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 334 | mSonicStream(sonicCreateStream(sampleRate, mChannelCount)), | 
|  | 335 | mFallbackFailErrorShown(false) | 
| Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 336 | { | 
| Ricardo Garcia | f097cae | 2015-04-13 12:17:21 -0700 | [diff] [blame] | 337 | LOG_ALWAYS_FATAL_IF(mSonicStream == NULL, | 
|  | 338 | "TimestretchBufferProvider can't allocate Sonic stream"); | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 339 |  | 
|  | 340 | setPlaybackRate(playbackRate); | 
|  | 341 | ALOGV("TimestretchBufferProvider(%p)(%u, %#x, %u %f %f %d %d)", | 
|  | 342 | this, channelCount, format, sampleRate, playbackRate.mSpeed, | 
|  | 343 | playbackRate.mPitch, playbackRate.mStretchMode, playbackRate.mFallbackMode); | 
|  | 344 | mBuffer.frameCount = 0; | 
| Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 345 | } | 
|  | 346 |  | 
|  | 347 | TimestretchBufferProvider::~TimestretchBufferProvider() | 
|  | 348 | { | 
|  | 349 | ALOGV("~TimestretchBufferProvider(%p)", this); | 
| Ricardo Garcia | f097cae | 2015-04-13 12:17:21 -0700 | [diff] [blame] | 350 | sonicDestroyStream(mSonicStream); | 
| Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 351 | if (mBuffer.frameCount != 0) { | 
|  | 352 | mTrackBufferProvider->releaseBuffer(&mBuffer); | 
|  | 353 | } | 
|  | 354 | free(mLocalBufferData); | 
|  | 355 | } | 
|  | 356 |  | 
|  | 357 | status_t TimestretchBufferProvider::getNextBuffer( | 
|  | 358 | AudioBufferProvider::Buffer *pBuffer, int64_t pts) | 
|  | 359 | { | 
|  | 360 | ALOGV("TimestretchBufferProvider(%p)::getNextBuffer(%p (%zu), %lld)", | 
|  | 361 | this, pBuffer, pBuffer->frameCount, pts); | 
|  | 362 |  | 
|  | 363 | // BYPASS | 
|  | 364 | //return mTrackBufferProvider->getNextBuffer(pBuffer, pts); | 
|  | 365 |  | 
|  | 366 | // check if previously processed data is sufficient. | 
|  | 367 | if (pBuffer->frameCount <= mRemaining) { | 
|  | 368 | ALOGV("previous sufficient"); | 
|  | 369 | pBuffer->raw = mLocalBufferData; | 
|  | 370 | return OK; | 
|  | 371 | } | 
|  | 372 |  | 
|  | 373 | // do we need to resize our buffer? | 
|  | 374 | if (pBuffer->frameCount > mLocalBufferFrameCount) { | 
|  | 375 | void *newmem; | 
|  | 376 | if (posix_memalign(&newmem, 32, pBuffer->frameCount * mFrameSize) == OK) { | 
|  | 377 | if (mRemaining != 0) { | 
|  | 378 | memcpy(newmem, mLocalBufferData, mRemaining * mFrameSize); | 
|  | 379 | } | 
|  | 380 | free(mLocalBufferData); | 
|  | 381 | mLocalBufferData = newmem; | 
|  | 382 | mLocalBufferFrameCount = pBuffer->frameCount; | 
|  | 383 | } | 
|  | 384 | } | 
|  | 385 |  | 
|  | 386 | // need to fetch more data | 
|  | 387 | const size_t outputDesired = pBuffer->frameCount - mRemaining; | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 388 | mBuffer.frameCount = mPlaybackRate.mSpeed == AUDIO_TIMESTRETCH_SPEED_NORMAL | 
|  | 389 | ? outputDesired : outputDesired * mPlaybackRate.mSpeed + 1; | 
| Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 390 |  | 
|  | 391 | status_t res = mTrackBufferProvider->getNextBuffer(&mBuffer, pts); | 
|  | 392 |  | 
|  | 393 | ALOG_ASSERT(res == OK || mBuffer.frameCount == 0); | 
|  | 394 | if (res != OK || mBuffer.frameCount == 0) { // not needed by API spec, but to be safe. | 
|  | 395 | ALOGD("buffer error"); | 
|  | 396 | if (mRemaining == 0) { | 
|  | 397 | pBuffer->raw = NULL; | 
|  | 398 | pBuffer->frameCount = 0; | 
|  | 399 | return res; | 
|  | 400 | } else { // return partial count | 
|  | 401 | pBuffer->raw = mLocalBufferData; | 
|  | 402 | pBuffer->frameCount = mRemaining; | 
|  | 403 | return OK; | 
|  | 404 | } | 
|  | 405 | } | 
|  | 406 |  | 
|  | 407 | // time-stretch the data | 
|  | 408 | size_t dstAvailable = min(mLocalBufferFrameCount - mRemaining, outputDesired); | 
|  | 409 | size_t srcAvailable = mBuffer.frameCount; | 
|  | 410 | processFrames((uint8_t*)mLocalBufferData + mRemaining * mFrameSize, &dstAvailable, | 
|  | 411 | mBuffer.raw, &srcAvailable); | 
|  | 412 |  | 
|  | 413 | // release all data consumed | 
|  | 414 | mBuffer.frameCount = srcAvailable; | 
|  | 415 | mTrackBufferProvider->releaseBuffer(&mBuffer); | 
|  | 416 |  | 
|  | 417 | // update buffer vars with the actual data processed and return with buffer | 
|  | 418 | mRemaining += dstAvailable; | 
|  | 419 |  | 
|  | 420 | pBuffer->raw = mLocalBufferData; | 
|  | 421 | pBuffer->frameCount = mRemaining; | 
|  | 422 |  | 
|  | 423 | return OK; | 
|  | 424 | } | 
|  | 425 |  | 
|  | 426 | void TimestretchBufferProvider::releaseBuffer(AudioBufferProvider::Buffer *pBuffer) | 
|  | 427 | { | 
|  | 428 | ALOGV("TimestretchBufferProvider(%p)::releaseBuffer(%p (%zu))", | 
|  | 429 | this, pBuffer, pBuffer->frameCount); | 
|  | 430 |  | 
|  | 431 | // BYPASS | 
|  | 432 | //return mTrackBufferProvider->releaseBuffer(pBuffer); | 
|  | 433 |  | 
|  | 434 | // LOG_ALWAYS_FATAL_IF(pBuffer->frameCount == 0, "Invalid framecount"); | 
|  | 435 | if (pBuffer->frameCount < mRemaining) { | 
|  | 436 | memcpy(mLocalBufferData, | 
|  | 437 | (uint8_t*)mLocalBufferData + pBuffer->frameCount * mFrameSize, | 
|  | 438 | (mRemaining - pBuffer->frameCount) * mFrameSize); | 
|  | 439 | mRemaining -= pBuffer->frameCount; | 
|  | 440 | } else if (pBuffer->frameCount == mRemaining) { | 
|  | 441 | mRemaining = 0; | 
|  | 442 | } else { | 
|  | 443 | LOG_ALWAYS_FATAL("Releasing more frames(%zu) than available(%zu)", | 
|  | 444 | pBuffer->frameCount, mRemaining); | 
|  | 445 | } | 
|  | 446 |  | 
|  | 447 | pBuffer->raw = NULL; | 
|  | 448 | pBuffer->frameCount = 0; | 
|  | 449 | } | 
|  | 450 |  | 
|  | 451 | void TimestretchBufferProvider::reset() | 
|  | 452 | { | 
|  | 453 | mRemaining = 0; | 
|  | 454 | } | 
|  | 455 |  | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 456 | status_t TimestretchBufferProvider::setPlaybackRate(const AudioPlaybackRate &playbackRate) | 
| Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 457 | { | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 458 | mPlaybackRate = playbackRate; | 
|  | 459 | mFallbackFailErrorShown = false; | 
|  | 460 | sonicSetSpeed(mSonicStream, mPlaybackRate.mSpeed); | 
| Ricardo Garcia | f097cae | 2015-04-13 12:17:21 -0700 | [diff] [blame] | 461 | //TODO: pitch is ignored for now | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 462 | //TODO: optimize: if parameters are the same, don't do any extra computation. | 
| Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 463 | return OK; | 
|  | 464 | } | 
|  | 465 |  | 
|  | 466 | void TimestretchBufferProvider::processFrames(void *dstBuffer, size_t *dstFrames, | 
|  | 467 | const void *srcBuffer, size_t *srcFrames) | 
|  | 468 | { | 
|  | 469 | ALOGV("processFrames(%zu %zu)  remaining(%zu)", *dstFrames, *srcFrames, mRemaining); | 
|  | 470 | // Note dstFrames is the required number of frames. | 
|  | 471 |  | 
|  | 472 | // Ensure consumption from src is as expected. | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 473 | //TODO: add logic to track "very accurate" consumption related to speed, original sampling | 
|  | 474 | //rate, actual frames processed. | 
|  | 475 | const size_t targetSrc = *dstFrames * mPlaybackRate.mSpeed; | 
| Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 476 | if (*srcFrames < targetSrc) { // limit dst frames to that possible | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 477 | *dstFrames = *srcFrames / mPlaybackRate.mSpeed; | 
| Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 478 | } else if (*srcFrames > targetSrc + 1) { | 
|  | 479 | *srcFrames = targetSrc + 1; | 
|  | 480 | } | 
|  | 481 |  | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 482 | if (mPlaybackRate.mSpeed< TIMESTRETCH_SONIC_SPEED_MIN  || | 
|  | 483 | mPlaybackRate.mSpeed >  TIMESTRETCH_SONIC_SPEED_MAX ) { | 
|  | 484 | //fallback mode | 
|  | 485 | if (*dstFrames > 0) { | 
|  | 486 | switch(mPlaybackRate.mFallbackMode) { | 
|  | 487 | case AUDIO_TIMESTRETCH_FALLBACK_CUT_REPEAT: | 
|  | 488 | if (*dstFrames <= *srcFrames) { | 
|  | 489 | size_t copySize = mFrameSize * *dstFrames; | 
|  | 490 | memcpy(dstBuffer, srcBuffer, copySize); | 
|  | 491 | } else { | 
|  | 492 | // cyclically repeat the source. | 
|  | 493 | for (size_t count = 0; count < *dstFrames; count += *srcFrames) { | 
|  | 494 | size_t remaining = min(*srcFrames, *dstFrames - count); | 
|  | 495 | memcpy((uint8_t*)dstBuffer + mFrameSize * count, | 
|  | 496 | srcBuffer, mFrameSize * remaining); | 
|  | 497 | } | 
|  | 498 | } | 
|  | 499 | break; | 
|  | 500 | case AUDIO_TIMESTRETCH_FALLBACK_DEFAULT: | 
|  | 501 | case AUDIO_TIMESTRETCH_FALLBACK_MUTE: | 
|  | 502 | memset(dstBuffer,0, mFrameSize * *dstFrames); | 
|  | 503 | break; | 
|  | 504 | case AUDIO_TIMESTRETCH_FALLBACK_FAIL: | 
|  | 505 | default: | 
|  | 506 | if(!mFallbackFailErrorShown) { | 
|  | 507 | ALOGE("invalid parameters in TimestretchBufferProvider fallbackMode:%d", | 
|  | 508 | mPlaybackRate.mFallbackMode); | 
|  | 509 | mFallbackFailErrorShown = true; | 
|  | 510 | } | 
|  | 511 | break; | 
|  | 512 | } | 
| Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 513 | } | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 514 | } else { | 
|  | 515 | switch (mFormat) { | 
|  | 516 | case AUDIO_FORMAT_PCM_FLOAT: | 
|  | 517 | if (sonicWriteFloatToStream(mSonicStream, (float*)srcBuffer, *srcFrames) != 1) { | 
|  | 518 | ALOGE("sonicWriteFloatToStream cannot realloc"); | 
|  | 519 | *srcFrames = 0; // cannot consume all of srcBuffer | 
|  | 520 | } | 
|  | 521 | *dstFrames = sonicReadFloatFromStream(mSonicStream, (float*)dstBuffer, *dstFrames); | 
|  | 522 | break; | 
|  | 523 | case AUDIO_FORMAT_PCM_16_BIT: | 
|  | 524 | if (sonicWriteShortToStream(mSonicStream, (short*)srcBuffer, *srcFrames) != 1) { | 
|  | 525 | ALOGE("sonicWriteShortToStream cannot realloc"); | 
|  | 526 | *srcFrames = 0; // cannot consume all of srcBuffer | 
|  | 527 | } | 
|  | 528 | *dstFrames = sonicReadShortFromStream(mSonicStream, (short*)dstBuffer, *dstFrames); | 
|  | 529 | break; | 
|  | 530 | default: | 
|  | 531 | // could also be caught on construction | 
|  | 532 | LOG_ALWAYS_FATAL("invalid format %#x for TimestretchBufferProvider", mFormat); | 
| Ricardo Garcia | f097cae | 2015-04-13 12:17:21 -0700 | [diff] [blame] | 533 | } | 
| Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 534 | } | 
|  | 535 | } | 
| Andy Hung | 857d5a2 | 2015-03-26 18:46:00 -0700 | [diff] [blame] | 536 | // ---------------------------------------------------------------------------- | 
|  | 537 | } // namespace android |