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 <algorithm>
21 
22 #include <audio_utils/primitives.h>
23 #include <audio_utils/format.h>
24 #include <audio_utils/channels.h>
25 #include <sonic.h>
26 #include <media/audiohal/EffectBufferHalInterface.h>
27 #include <media/audiohal/EffectHalInterface.h>
28 #include <media/audiohal/EffectsFactoryHalInterface.h>
29 #include <media/AudioResamplerPublic.h>
30 #include <media/BufferProviders.h>
31 #include <system/audio_effects/effect_downmix.h>
32 #include <utils/Log.h>
33 
34 #ifndef ARRAY_SIZE
35 #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
36 #endif
37 
38 namespace android {
39 
40 // ----------------------------------------------------------------------------
CopyBufferProvider(size_t inputFrameSize,size_t outputFrameSize,size_t bufferFrameCount)41 CopyBufferProvider::CopyBufferProvider(size_t inputFrameSize,
42         size_t outputFrameSize, size_t bufferFrameCount) :
43         mInputFrameSize(inputFrameSize),
44         mOutputFrameSize(outputFrameSize),
45         mLocalBufferFrameCount(bufferFrameCount),
46         mLocalBufferData(NULL),
47         mConsumed(0)
48 {
49     ALOGV("CopyBufferProvider(%p)(%zu, %zu, %zu)", this,
50             inputFrameSize, outputFrameSize, bufferFrameCount);
51     LOG_ALWAYS_FATAL_IF(inputFrameSize < outputFrameSize && bufferFrameCount == 0,
52             "Requires local buffer if inputFrameSize(%zu) < outputFrameSize(%zu)",
53             inputFrameSize, outputFrameSize);
54     if (mLocalBufferFrameCount) {
55         (void)posix_memalign(&mLocalBufferData, 32, mLocalBufferFrameCount * mOutputFrameSize);
56     }
57     mBuffer.frameCount = 0;
58 }
59 
~CopyBufferProvider()60 CopyBufferProvider::~CopyBufferProvider()
61 {
62     ALOGV("%s(%p) %zu %p %p",
63            __func__, this, mBuffer.frameCount, mTrackBufferProvider, mLocalBufferData);
64     if (mBuffer.frameCount != 0) {
65         mTrackBufferProvider->releaseBuffer(&mBuffer);
66     }
67     free(mLocalBufferData);
68 }
69 
getNextBuffer(AudioBufferProvider::Buffer * pBuffer)70 status_t CopyBufferProvider::getNextBuffer(AudioBufferProvider::Buffer *pBuffer)
71 {
72     //ALOGV("CopyBufferProvider(%p)::getNextBuffer(%p (%zu))",
73     //        this, pBuffer, pBuffer->frameCount);
74     if (mLocalBufferFrameCount == 0) {
75         status_t res = mTrackBufferProvider->getNextBuffer(pBuffer);
76         if (res == OK) {
77             copyFrames(pBuffer->raw, pBuffer->raw, pBuffer->frameCount);
78         }
79         return res;
80     }
81     if (mBuffer.frameCount == 0) {
82         mBuffer.frameCount = pBuffer->frameCount;
83         status_t res = mTrackBufferProvider->getNextBuffer(&mBuffer);
84         // At one time an upstream buffer provider had
85         // res == OK and mBuffer.frameCount == 0, doesn't seem to happen now 7/18/2014.
86         //
87         // By API spec, if res != OK, then mBuffer.frameCount == 0.
88         // but there may be improper implementations.
89         ALOG_ASSERT(res == OK || mBuffer.frameCount == 0);
90         if (res != OK || mBuffer.frameCount == 0) { // not needed by API spec, but to be safe.
91             pBuffer->raw = NULL;
92             pBuffer->frameCount = 0;
93             return res;
94         }
95         mConsumed = 0;
96     }
97     ALOG_ASSERT(mConsumed < mBuffer.frameCount);
98     size_t count = std::min(mLocalBufferFrameCount, mBuffer.frameCount - mConsumed);
99     count = std::min(count, pBuffer->frameCount);
100     pBuffer->raw = mLocalBufferData;
101     pBuffer->frameCount = count;
102     copyFrames(pBuffer->raw, (uint8_t*)mBuffer.raw + mConsumed * mInputFrameSize,
103             pBuffer->frameCount);
104     return OK;
105 }
106 
releaseBuffer(AudioBufferProvider::Buffer * pBuffer)107 void CopyBufferProvider::releaseBuffer(AudioBufferProvider::Buffer *pBuffer)
108 {
109     //ALOGV("CopyBufferProvider(%p)::releaseBuffer(%p(%zu))",
110     //        this, pBuffer, pBuffer->frameCount);
111     if (mLocalBufferFrameCount == 0) {
112         mTrackBufferProvider->releaseBuffer(pBuffer);
113         return;
114     }
115     // LOG_ALWAYS_FATAL_IF(pBuffer->frameCount == 0, "Invalid framecount");
116     mConsumed += pBuffer->frameCount; // TODO: update for efficiency to reuse existing content
117     if (mConsumed != 0 && mConsumed >= mBuffer.frameCount) {
118         mTrackBufferProvider->releaseBuffer(&mBuffer);
119         ALOG_ASSERT(mBuffer.frameCount == 0);
120     }
121     pBuffer->raw = NULL;
122     pBuffer->frameCount = 0;
123 }
124 
reset()125 void CopyBufferProvider::reset()
126 {
127     if (mBuffer.frameCount != 0) {
128         mTrackBufferProvider->releaseBuffer(&mBuffer);
129     }
130     mConsumed = 0;
131 }
132 
setBufferProvider(AudioBufferProvider * p)133 void CopyBufferProvider::setBufferProvider(AudioBufferProvider *p) {
134     ALOGV("%s(%p): mTrackBufferProvider:%p  mBuffer.frameCount:%zu",
135             __func__, p, mTrackBufferProvider, mBuffer.frameCount);
136     if (mTrackBufferProvider == p) {
137         return;
138     }
139     mBuffer.frameCount = 0;
140     PassthruBufferProvider::setBufferProvider(p);
141 }
142 
DownmixerBufferProvider(audio_channel_mask_t inputChannelMask,audio_channel_mask_t outputChannelMask,audio_format_t format,uint32_t sampleRate,int32_t sessionId,size_t bufferFrameCount)143 DownmixerBufferProvider::DownmixerBufferProvider(
144         audio_channel_mask_t inputChannelMask,
145         audio_channel_mask_t outputChannelMask, audio_format_t format,
146         uint32_t sampleRate, int32_t sessionId, size_t bufferFrameCount) :
147         CopyBufferProvider(
148             audio_bytes_per_sample(format) * audio_channel_count_from_out_mask(inputChannelMask),
149             audio_bytes_per_sample(format) * audio_channel_count_from_out_mask(outputChannelMask),
150             bufferFrameCount)  // set bufferFrameCount to 0 to do in-place
151 {
152     ALOGV("DownmixerBufferProvider(%p)(%#x, %#x, %#x %u %d %d)",
153             this, inputChannelMask, outputChannelMask, format,
154             sampleRate, sessionId, (int)bufferFrameCount);
155     if (!sIsMultichannelCapable) {
156         ALOGE("DownmixerBufferProvider() error: not multichannel capable");
157         return;
158     }
159     mEffectsFactory = EffectsFactoryHalInterface::create();
160     if (mEffectsFactory == 0) {
161         ALOGE("DownmixerBufferProvider() error: could not obtain the effects factory");
162         return;
163     }
164     if (mEffectsFactory->createEffect(&sDwnmFxDesc.uuid,
165                                       sessionId,
166                                       SESSION_ID_INVALID_AND_IGNORED,
167                                       AUDIO_PORT_HANDLE_NONE,
168                                       &mDownmixInterface) != 0) {
169          ALOGE("DownmixerBufferProvider() error creating downmixer effect");
170          mDownmixInterface.clear();
171          mEffectsFactory.clear();
172          return;
173      }
174      // channel input configuration will be overridden per-track
175      mDownmixConfig.inputCfg.channels = inputChannelMask;   // FIXME: Should be bits
176      mDownmixConfig.outputCfg.channels = outputChannelMask; // FIXME: should be bits
177      mDownmixConfig.inputCfg.format = format;
178      mDownmixConfig.outputCfg.format = format;
179      mDownmixConfig.inputCfg.samplingRate = sampleRate;
180      mDownmixConfig.outputCfg.samplingRate = sampleRate;
181      mDownmixConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
182      mDownmixConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
183      // input and output buffer provider, and frame count will not be used as the downmix effect
184      // process() function is called directly (see DownmixerBufferProvider::getNextBuffer())
185      mDownmixConfig.inputCfg.mask = EFFECT_CONFIG_SMP_RATE | EFFECT_CONFIG_CHANNELS |
186              EFFECT_CONFIG_FORMAT | EFFECT_CONFIG_ACC_MODE;
187      mDownmixConfig.outputCfg.mask = mDownmixConfig.inputCfg.mask;
188 
189      mInFrameSize =
190              audio_bytes_per_sample(format) * audio_channel_count_from_out_mask(inputChannelMask);
191      mOutFrameSize =
192              audio_bytes_per_sample(format) * audio_channel_count_from_out_mask(outputChannelMask);
193      status_t status;
194      status = mEffectsFactory->mirrorBuffer(
195              nullptr, mInFrameSize * bufferFrameCount, &mInBuffer);
196      if (status != 0) {
197          ALOGE("DownmixerBufferProvider() error %d while creating input buffer", status);
198          mDownmixInterface.clear();
199          mEffectsFactory.clear();
200          return;
201      }
202      status = mEffectsFactory->mirrorBuffer(
203              nullptr, mOutFrameSize * bufferFrameCount, &mOutBuffer);
204      if (status != 0) {
205          ALOGE("DownmixerBufferProvider() error %d while creating output buffer", status);
206          mInBuffer.clear();
207          mDownmixInterface.clear();
208          mEffectsFactory.clear();
209          return;
210      }
211      mDownmixInterface->setInBuffer(mInBuffer);
212      mDownmixInterface->setOutBuffer(mOutBuffer);
213 
214      int cmdStatus;
215      uint32_t replySize = sizeof(int);
216 
217      // Configure downmixer
218      status = mDownmixInterface->command(
219              EFFECT_CMD_SET_CONFIG /*cmdCode*/, sizeof(effect_config_t) /*cmdSize*/,
220              &mDownmixConfig /*pCmdData*/,
221              &replySize, &cmdStatus /*pReplyData*/);
222      if (status != 0 || cmdStatus != 0) {
223          ALOGE("DownmixerBufferProvider() error %d cmdStatus %d while configuring downmixer",
224                  status, cmdStatus);
225          mOutBuffer.clear();
226          mInBuffer.clear();
227          mDownmixInterface.clear();
228          mEffectsFactory.clear();
229          return;
230      }
231 
232      // Enable downmixer
233      replySize = sizeof(int);
234      status = mDownmixInterface->command(
235              EFFECT_CMD_ENABLE /*cmdCode*/, 0 /*cmdSize*/, NULL /*pCmdData*/,
236              &replySize, &cmdStatus /*pReplyData*/);
237      if (status != 0 || cmdStatus != 0) {
238          ALOGE("DownmixerBufferProvider() error %d cmdStatus %d while enabling downmixer",
239                  status, cmdStatus);
240          mOutBuffer.clear();
241          mInBuffer.clear();
242          mDownmixInterface.clear();
243          mEffectsFactory.clear();
244          return;
245      }
246 
247      // Set downmix type
248      // parameter size rounded for padding on 32bit boundary
249      const int psizePadded = ((sizeof(downmix_params_t) - 1)/sizeof(int) + 1) * sizeof(int);
250      const int downmixParamSize =
251              sizeof(effect_param_t) + psizePadded + sizeof(downmix_type_t);
252      effect_param_t * const param = (effect_param_t *) malloc(downmixParamSize);
253      param->psize = sizeof(downmix_params_t);
254      const downmix_params_t downmixParam = DOWNMIX_PARAM_TYPE;
255      memcpy(param->data, &downmixParam, param->psize);
256      const downmix_type_t downmixType = DOWNMIX_TYPE_FOLD;
257      param->vsize = sizeof(downmix_type_t);
258      memcpy(param->data + psizePadded, &downmixType, param->vsize);
259      replySize = sizeof(int);
260      status = mDownmixInterface->command(
261              EFFECT_CMD_SET_PARAM /* cmdCode */, downmixParamSize /* cmdSize */,
262              param /*pCmdData*/, &replySize, &cmdStatus /*pReplyData*/);
263      free(param);
264      if (status != 0 || cmdStatus != 0) {
265          ALOGE("DownmixerBufferProvider() error %d cmdStatus %d while setting downmix type",
266                  status, cmdStatus);
267          mOutBuffer.clear();
268          mInBuffer.clear();
269          mDownmixInterface.clear();
270          mEffectsFactory.clear();
271          return;
272      }
273      ALOGV("DownmixerBufferProvider() downmix type set to %d", (int) downmixType);
274 }
275 
~DownmixerBufferProvider()276 DownmixerBufferProvider::~DownmixerBufferProvider()
277 {
278     ALOGV("~DownmixerBufferProvider (%p)", this);
279     if (mDownmixInterface != 0) {
280         mDownmixInterface->close();
281     }
282 }
283 
copyFrames(void * dst,const void * src,size_t frames)284 void DownmixerBufferProvider::copyFrames(void *dst, const void *src, size_t frames)
285 {
286     mInBuffer->setExternalData(const_cast<void*>(src));
287     mInBuffer->setFrameCount(frames);
288     mInBuffer->update(mInFrameSize * frames);
289     mOutBuffer->setFrameCount(frames);
290     mOutBuffer->setExternalData(dst);
291     if (dst != src) {
292         // Downmix may be accumulating, need to populate the output buffer
293         // with the dst data.
294         mOutBuffer->update(mOutFrameSize * frames);
295     }
296     // may be in-place if src == dst.
297     status_t res = mDownmixInterface->process();
298     if (res == OK) {
299         mOutBuffer->commit(mOutFrameSize * frames);
300     } else {
301         ALOGE("DownmixBufferProvider error %d", res);
302     }
303 }
304 
305 /* call once in a pthread_once handler. */
init()306 /*static*/ status_t DownmixerBufferProvider::init()
307 {
308     // find multichannel downmix effect if we have to play multichannel content
309     sp<EffectsFactoryHalInterface> effectsFactory = EffectsFactoryHalInterface::create();
310     if (effectsFactory == 0) {
311         ALOGE("AudioMixer() error: could not obtain the effects factory");
312         return NO_INIT;
313     }
314     uint32_t numEffects = 0;
315     int ret = effectsFactory->queryNumberEffects(&numEffects);
316     if (ret != 0) {
317         ALOGE("AudioMixer() error %d querying number of effects", ret);
318         return NO_INIT;
319     }
320     ALOGV("EffectQueryNumberEffects() numEffects=%d", numEffects);
321 
322     for (uint32_t i = 0 ; i < numEffects ; i++) {
323         if (effectsFactory->getDescriptor(i, &sDwnmFxDesc) == 0) {
324             ALOGV("effect %d is called %s", i, sDwnmFxDesc.name);
325             if (memcmp(&sDwnmFxDesc.type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0) {
326                 ALOGI("found effect \"%s\" from %s",
327                         sDwnmFxDesc.name, sDwnmFxDesc.implementor);
328                 sIsMultichannelCapable = true;
329                 break;
330             }
331         }
332     }
333     ALOGW_IF(!sIsMultichannelCapable, "unable to find downmix effect");
334     return NO_INIT;
335 }
336 
337 /*static*/ bool DownmixerBufferProvider::sIsMultichannelCapable = false;
338 /*static*/ effect_descriptor_t DownmixerBufferProvider::sDwnmFxDesc;
339 
RemixBufferProvider(audio_channel_mask_t inputChannelMask,audio_channel_mask_t outputChannelMask,audio_format_t format,size_t bufferFrameCount)340 RemixBufferProvider::RemixBufferProvider(audio_channel_mask_t inputChannelMask,
341         audio_channel_mask_t outputChannelMask, audio_format_t format,
342         size_t bufferFrameCount) :
343         CopyBufferProvider(
344                 audio_bytes_per_sample(format)
345                     * audio_channel_count_from_out_mask(inputChannelMask),
346                 audio_bytes_per_sample(format)
347                     * audio_channel_count_from_out_mask(outputChannelMask),
348                 bufferFrameCount),
349         mFormat(format),
350         mSampleSize(audio_bytes_per_sample(format)),
351         mInputChannels(audio_channel_count_from_out_mask(inputChannelMask)),
352         mOutputChannels(audio_channel_count_from_out_mask(outputChannelMask))
353 {
354     ALOGV("RemixBufferProvider(%p)(%#x, %#x, %#x) %zu %zu",
355             this, format, inputChannelMask, outputChannelMask,
356             mInputChannels, mOutputChannels);
357     (void) memcpy_by_index_array_initialization_from_channel_mask(
358             mIdxAry, ARRAY_SIZE(mIdxAry), outputChannelMask, inputChannelMask);
359 }
360 
copyFrames(void * dst,const void * src,size_t frames)361 void RemixBufferProvider::copyFrames(void *dst, const void *src, size_t frames)
362 {
363     memcpy_by_index_array(dst, mOutputChannels,
364             src, mInputChannels, mIdxAry, mSampleSize, frames);
365 }
366 
ReformatBufferProvider(int32_t channelCount,audio_format_t inputFormat,audio_format_t outputFormat,size_t bufferFrameCount)367 ReformatBufferProvider::ReformatBufferProvider(int32_t channelCount,
368         audio_format_t inputFormat, audio_format_t outputFormat,
369         size_t bufferFrameCount) :
370         CopyBufferProvider(
371                 channelCount * audio_bytes_per_sample(inputFormat),
372                 channelCount * audio_bytes_per_sample(outputFormat),
373                 bufferFrameCount),
374         mChannelCount(channelCount),
375         mInputFormat(inputFormat),
376         mOutputFormat(outputFormat)
377 {
378     ALOGV("ReformatBufferProvider(%p)(%u, %#x, %#x)",
379             this, channelCount, inputFormat, outputFormat);
380 }
381 
copyFrames(void * dst,const void * src,size_t frames)382 void ReformatBufferProvider::copyFrames(void *dst, const void *src, size_t frames)
383 {
384     memcpy_by_audio_format(dst, mOutputFormat, src, mInputFormat, frames * mChannelCount);
385 }
386 
ClampFloatBufferProvider(int32_t channelCount,size_t bufferFrameCount)387 ClampFloatBufferProvider::ClampFloatBufferProvider(int32_t channelCount, size_t bufferFrameCount) :
388         CopyBufferProvider(
389                 channelCount * audio_bytes_per_sample(AUDIO_FORMAT_PCM_FLOAT),
390                 channelCount * audio_bytes_per_sample(AUDIO_FORMAT_PCM_FLOAT),
391                 bufferFrameCount),
392         mChannelCount(channelCount)
393 {
394     ALOGV("ClampFloatBufferProvider(%p)(%u)", this, channelCount);
395 }
396 
copyFrames(void * dst,const void * src,size_t frames)397 void ClampFloatBufferProvider::copyFrames(void *dst, const void *src, size_t frames)
398 {
399     memcpy_to_float_from_float_with_clamping((float*)dst, (const float*)src,
400                                              frames * mChannelCount,
401                                              FLOAT_NOMINAL_RANGE_HEADROOM);
402 }
403 
TimestretchBufferProvider(int32_t channelCount,audio_format_t format,uint32_t sampleRate,const AudioPlaybackRate & playbackRate)404 TimestretchBufferProvider::TimestretchBufferProvider(int32_t channelCount,
405         audio_format_t format, uint32_t sampleRate, const AudioPlaybackRate &playbackRate) :
406         mChannelCount(channelCount),
407         mFormat(format),
408         mSampleRate(sampleRate),
409         mFrameSize(channelCount * audio_bytes_per_sample(format)),
410         mLocalBufferFrameCount(0),
411         mLocalBufferData(NULL),
412         mRemaining(0),
413         mSonicStream(sonicCreateStream(sampleRate, mChannelCount)),
414         mFallbackFailErrorShown(false),
415         mAudioPlaybackRateValid(false)
416 {
417     LOG_ALWAYS_FATAL_IF(mSonicStream == NULL,
418             "TimestretchBufferProvider can't allocate Sonic stream");
419 
420     setPlaybackRate(playbackRate);
421     ALOGV("TimestretchBufferProvider(%p)(%u, %#x, %u %f %f %d %d)",
422             this, channelCount, format, sampleRate, playbackRate.mSpeed,
423             playbackRate.mPitch, playbackRate.mStretchMode, playbackRate.mFallbackMode);
424     mBuffer.frameCount = 0;
425 }
426 
~TimestretchBufferProvider()427 TimestretchBufferProvider::~TimestretchBufferProvider()
428 {
429     ALOGV("~TimestretchBufferProvider(%p)", this);
430     sonicDestroyStream(mSonicStream);
431     if (mBuffer.frameCount != 0) {
432         mTrackBufferProvider->releaseBuffer(&mBuffer);
433     }
434     free(mLocalBufferData);
435 }
436 
getNextBuffer(AudioBufferProvider::Buffer * pBuffer)437 status_t TimestretchBufferProvider::getNextBuffer(
438         AudioBufferProvider::Buffer *pBuffer)
439 {
440     ALOGV("TimestretchBufferProvider(%p)::getNextBuffer(%p (%zu))",
441             this, pBuffer, pBuffer->frameCount);
442 
443     // BYPASS
444     //return mTrackBufferProvider->getNextBuffer(pBuffer);
445 
446     // check if previously processed data is sufficient.
447     if (pBuffer->frameCount <= mRemaining) {
448         ALOGV("previous sufficient");
449         pBuffer->raw = mLocalBufferData;
450         return OK;
451     }
452 
453     // do we need to resize our buffer?
454     if (pBuffer->frameCount > mLocalBufferFrameCount) {
455         void *newmem;
456         if (posix_memalign(&newmem, 32, pBuffer->frameCount * mFrameSize) == OK) {
457             if (mRemaining != 0) {
458                 memcpy(newmem, mLocalBufferData, mRemaining * mFrameSize);
459             }
460             free(mLocalBufferData);
461             mLocalBufferData = newmem;
462             mLocalBufferFrameCount = pBuffer->frameCount;
463         }
464     }
465 
466     // need to fetch more data
467     const size_t outputDesired = pBuffer->frameCount - mRemaining;
468     size_t dstAvailable;
469     do {
470         mBuffer.frameCount = mPlaybackRate.mSpeed == AUDIO_TIMESTRETCH_SPEED_NORMAL
471                 ? outputDesired : outputDesired * mPlaybackRate.mSpeed + 1;
472 
473         status_t res = mTrackBufferProvider->getNextBuffer(&mBuffer);
474 
475         ALOG_ASSERT(res == OK || mBuffer.frameCount == 0);
476         if (res != OK || mBuffer.frameCount == 0) { // not needed by API spec, but to be safe.
477             ALOGV("upstream provider cannot provide data");
478             if (mRemaining == 0) {
479                 pBuffer->raw = NULL;
480                 pBuffer->frameCount = 0;
481                 return res;
482             } else { // return partial count
483                 pBuffer->raw = mLocalBufferData;
484                 pBuffer->frameCount = mRemaining;
485                 return OK;
486             }
487         }
488 
489         // time-stretch the data
490         dstAvailable = std::min(mLocalBufferFrameCount - mRemaining, outputDesired);
491         size_t srcAvailable = mBuffer.frameCount;
492         processFrames((uint8_t*)mLocalBufferData + mRemaining * mFrameSize, &dstAvailable,
493                 mBuffer.raw, &srcAvailable);
494 
495         // release all data consumed
496         mBuffer.frameCount = srcAvailable;
497         mTrackBufferProvider->releaseBuffer(&mBuffer);
498     } while (dstAvailable == 0); // try until we get output data or upstream provider fails.
499 
500     // update buffer vars with the actual data processed and return with buffer
501     mRemaining += dstAvailable;
502 
503     pBuffer->raw = mLocalBufferData;
504     pBuffer->frameCount = mRemaining;
505 
506     return OK;
507 }
508 
releaseBuffer(AudioBufferProvider::Buffer * pBuffer)509 void TimestretchBufferProvider::releaseBuffer(AudioBufferProvider::Buffer *pBuffer)
510 {
511     ALOGV("TimestretchBufferProvider(%p)::releaseBuffer(%p (%zu))",
512        this, pBuffer, pBuffer->frameCount);
513 
514     // BYPASS
515     //return mTrackBufferProvider->releaseBuffer(pBuffer);
516 
517     // LOG_ALWAYS_FATAL_IF(pBuffer->frameCount == 0, "Invalid framecount");
518     if (pBuffer->frameCount < mRemaining) {
519         memcpy(mLocalBufferData,
520                 (uint8_t*)mLocalBufferData + pBuffer->frameCount * mFrameSize,
521                 (mRemaining - pBuffer->frameCount) * mFrameSize);
522         mRemaining -= pBuffer->frameCount;
523     } else if (pBuffer->frameCount == mRemaining) {
524         mRemaining = 0;
525     } else {
526         LOG_ALWAYS_FATAL("Releasing more frames(%zu) than available(%zu)",
527                 pBuffer->frameCount, mRemaining);
528     }
529 
530     pBuffer->raw = NULL;
531     pBuffer->frameCount = 0;
532 }
533 
reset()534 void TimestretchBufferProvider::reset()
535 {
536     mRemaining = 0;
537 }
538 
setBufferProvider(AudioBufferProvider * p)539 void TimestretchBufferProvider::setBufferProvider(AudioBufferProvider *p) {
540     ALOGV("%s(%p): mTrackBufferProvider:%p  mBuffer.frameCount:%zu",
541             __func__, p, mTrackBufferProvider, mBuffer.frameCount);
542     if (mTrackBufferProvider == p) {
543         return;
544     }
545     mBuffer.frameCount = 0;
546     PassthruBufferProvider::setBufferProvider(p);
547 }
548 
setPlaybackRate(const AudioPlaybackRate & playbackRate)549 status_t TimestretchBufferProvider::setPlaybackRate(const AudioPlaybackRate &playbackRate)
550 {
551     mPlaybackRate = playbackRate;
552     mFallbackFailErrorShown = false;
553     sonicSetSpeed(mSonicStream, mPlaybackRate.mSpeed);
554     //TODO: pitch is ignored for now
555     //TODO: optimize: if parameters are the same, don't do any extra computation.
556 
557     mAudioPlaybackRateValid = isAudioPlaybackRateValid(mPlaybackRate);
558     return OK;
559 }
560 
processFrames(void * dstBuffer,size_t * dstFrames,const void * srcBuffer,size_t * srcFrames)561 void TimestretchBufferProvider::processFrames(void *dstBuffer, size_t *dstFrames,
562         const void *srcBuffer, size_t *srcFrames)
563 {
564     ALOGV("processFrames(%zu %zu)  remaining(%zu)", *dstFrames, *srcFrames, mRemaining);
565     // Note dstFrames is the required number of frames.
566 
567     if (!mAudioPlaybackRateValid) {
568         //fallback mode
569         // Ensure consumption from src is as expected.
570         // TODO: add logic to track "very accurate" consumption related to speed, original sampling
571         // rate, actual frames processed.
572 
573         const size_t targetSrc = *dstFrames * mPlaybackRate.mSpeed;
574         if (*srcFrames < targetSrc) { // limit dst frames to that possible
575             *dstFrames = *srcFrames / mPlaybackRate.mSpeed;
576         } else if (*srcFrames > targetSrc + 1) {
577             *srcFrames = targetSrc + 1;
578         }
579         if (*dstFrames > 0) {
580             switch(mPlaybackRate.mFallbackMode) {
581             case AUDIO_TIMESTRETCH_FALLBACK_CUT_REPEAT:
582                 if (*dstFrames <= *srcFrames) {
583                       size_t copySize = mFrameSize * *dstFrames;
584                       memcpy(dstBuffer, srcBuffer, copySize);
585                   } else {
586                       // cyclically repeat the source.
587                       for (size_t count = 0; count < *dstFrames; count += *srcFrames) {
588                           size_t remaining = std::min(*srcFrames, *dstFrames - count);
589                           memcpy((uint8_t*)dstBuffer + mFrameSize * count,
590                                   srcBuffer, mFrameSize * remaining);
591                       }
592                   }
593                 break;
594             case AUDIO_TIMESTRETCH_FALLBACK_DEFAULT:
595             case AUDIO_TIMESTRETCH_FALLBACK_MUTE:
596                 memset(dstBuffer,0, mFrameSize * *dstFrames);
597                 break;
598             case AUDIO_TIMESTRETCH_FALLBACK_FAIL:
599             default:
600                 if(!mFallbackFailErrorShown) {
601                     ALOGE("invalid parameters in TimestretchBufferProvider fallbackMode:%d",
602                             mPlaybackRate.mFallbackMode);
603                     mFallbackFailErrorShown = true;
604                 }
605                 break;
606             }
607         }
608     } else {
609         switch (mFormat) {
610         case AUDIO_FORMAT_PCM_FLOAT:
611             if (sonicWriteFloatToStream(mSonicStream, (float*)srcBuffer, *srcFrames) != 1) {
612                 ALOGE("sonicWriteFloatToStream cannot realloc");
613                 *srcFrames = 0; // cannot consume all of srcBuffer
614             }
615             *dstFrames = sonicReadFloatFromStream(mSonicStream, (float*)dstBuffer, *dstFrames);
616             break;
617         case AUDIO_FORMAT_PCM_16_BIT:
618             if (sonicWriteShortToStream(mSonicStream, (short*)srcBuffer, *srcFrames) != 1) {
619                 ALOGE("sonicWriteShortToStream cannot realloc");
620                 *srcFrames = 0; // cannot consume all of srcBuffer
621             }
622             *dstFrames = sonicReadShortFromStream(mSonicStream, (short*)dstBuffer, *dstFrames);
623             break;
624         default:
625             // could also be caught on construction
626             LOG_ALWAYS_FATAL("invalid format %#x for TimestretchBufferProvider", mFormat);
627         }
628     }
629 }
630 
AdjustChannelsBufferProvider(audio_format_t format,size_t inChannelCount,size_t outChannelCount,size_t frameCount,audio_format_t contractedFormat,void * contractedBuffer)631 AdjustChannelsBufferProvider::AdjustChannelsBufferProvider(
632         audio_format_t format, size_t inChannelCount, size_t outChannelCount,
633         size_t frameCount, audio_format_t contractedFormat, void* contractedBuffer) :
634         CopyBufferProvider(
635                 audio_bytes_per_frame(inChannelCount, format),
636                 audio_bytes_per_frame(std::max(inChannelCount, outChannelCount), format),
637                 frameCount),
638         mFormat(format),
639         mInChannelCount(inChannelCount),
640         mOutChannelCount(outChannelCount),
641         mSampleSizeInBytes(audio_bytes_per_sample(format)),
642         mFrameCount(frameCount),
643         mContractedChannelCount(inChannelCount - outChannelCount),
644         mContractedFormat(contractedFormat),
645         mContractedBuffer(contractedBuffer),
646         mContractedWrittenFrames(0)
647 {
648     ALOGV("AdjustChannelsBufferProvider(%p)(%#x, %zu, %zu, %zu, %#x, %p)", this, format,
649             inChannelCount, outChannelCount, frameCount, contractedFormat, contractedBuffer);
650     if (mContractedFormat != AUDIO_FORMAT_INVALID && mInChannelCount > mOutChannelCount) {
651         mContractedFrameSize = audio_bytes_per_frame(mContractedChannelCount, mContractedFormat);
652     }
653 }
654 
getNextBuffer(AudioBufferProvider::Buffer * pBuffer)655 status_t AdjustChannelsBufferProvider::getNextBuffer(AudioBufferProvider::Buffer* pBuffer)
656 {
657     if (mContractedBuffer != nullptr) {
658         // Restrict frame count only when it is needed to save contracted frames.
659         const size_t outFramesLeft = mFrameCount - mContractedWrittenFrames;
660         if (outFramesLeft < pBuffer->frameCount) {
661             // Restrict the frame count so that we don't write over the size of the output buffer.
662             pBuffer->frameCount = outFramesLeft;
663         }
664     }
665     return CopyBufferProvider::getNextBuffer(pBuffer);
666 }
667 
copyFrames(void * dst,const void * src,size_t frames)668 void AdjustChannelsBufferProvider::copyFrames(void *dst, const void *src, size_t frames)
669 {
670     if (mInChannelCount > mOutChannelCount) {
671         // For case multi to mono, adjust_channels has special logic that will mix first two input
672         // channels into a single output channel. In that case, use adjust_channels_non_destructive
673         // to keep only one channel data even when contracting to mono.
674         adjust_channels_non_destructive(src, mInChannelCount, dst, mOutChannelCount,
675                 mSampleSizeInBytes, frames * mInChannelCount * mSampleSizeInBytes);
676         if (mContractedFormat != AUDIO_FORMAT_INVALID
677             && mContractedBuffer != nullptr) {
678             const size_t contractedIdx = frames * mOutChannelCount * mSampleSizeInBytes;
679             memcpy_by_audio_format(
680                     (uint8_t*) mContractedBuffer + mContractedWrittenFrames * mContractedFrameSize,
681                     mContractedFormat, (uint8_t*) dst + contractedIdx, mFormat,
682                     mContractedChannelCount * frames);
683             mContractedWrittenFrames += frames;
684         }
685     } else {
686         // Prefer expanding data from the end of each audio frame.
687         adjust_channels(src, mInChannelCount, dst, mOutChannelCount,
688                 mSampleSizeInBytes, frames * mInChannelCount * mSampleSizeInBytes);
689     }
690 }
691 
reset()692 void AdjustChannelsBufferProvider::reset()
693 {
694     mContractedWrittenFrames = 0;
695     CopyBufferProvider::reset();
696 }
697 // ----------------------------------------------------------------------------
698 } // namespace android
699