androidopensl

Cannot play pcm > 60kb with the AndroidSimpleBufferQueue


I've initialized an AndroidSimpleBufferQueue such as values:

SLDataLocator_AndroidSimpleBufferQueue bufferLocator = {
  SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2};
SLDataFormat_PCM pcmFormat = {
  SL_DATAFORMAT_PCM,           1,
  SL_SAMPLINGRATE_44_1,        SL_PCMSAMPLEFORMAT_FIXED_16,
  SL_PCMSAMPLEFORMAT_FIXED_16, SL_SPEAKER_FRONT_CENTER,
  SL_BYTEORDER_LITTLEENDIAN};

SLDataSource            audioSource      = {&bufferLocator, &pcmFormat};
SLDataLocator_OutputMix locatorOutputMix = {SL_DATALOCATOR_OUTPUTMIX,
                                            outputMixObj.getObject()};
SLDataSink audioSink = {&locatorOutputMix, nullptr};
[...]

Then when I need to play a sound I enqueue such as:

bufferQueue->Enqueue(bufferQueue, (uint8_t*)buffer.data(),
          buffer.size())

It works well for buffer smaller than 60k however when the buffer is bigger no sound are played or sometime just an artifact.


Solution

  • From the Specifications:

    The buffers that are queued are used in place and are not required to be copied by the device, although this may be implementation-dependent. The application developer should be aware that modifying the content of a buffer after it has been queued is undefined and can cause audio corruption

    So the memory must remains valid until the sound fully played.

    Read the documentation and be aware of unintended struct copy :)