0

I:

  1. Create android::MediaBufferGroup;
  2. Fill it up with multiple buf_group.add_buffer(new android::MediaBuffer(bufsize)); on initialisation;
  3. Do buf_group->acquire_buffer(&buffer) when I need a buffer to send somewhere;
  4. Use buffer->data() to get actual memory location to store the data at, use set_range and set up metadata, then feed the buffer into other component;
  5. That other component releases the buffer, retuning them back to the MediaBufferGroup.

It works, but not reliably. Sometimes acquired buffer's data() returns NULL, sometimes the program crashes on release()...

How to use MediaBufferGroup properly? Should I use some synchronization?

Vi.
  • 37,014
  • 18
  • 93
  • 148

2 Answers2

0

Almost all your steps are correct. The one point which is not clear is in step 4. Typically, MediaBuffer is pulled by a consumer from a producer through a read call. So, I presume in your setup,

  1. All steps mentioned above are performed by the producer

  2. Consumer invokes mSource->read(&newBuffer); where newBuffer is defined as MediaBuffer *newBuffer;

  3. At producer's end, MediaBuffer *mBuffer;. The read call would be processed and output shall be populated as *out = mBuffer;.

  4. For safety, please initialize mBuffer to NULL after this step.

  5. After consuming the buffer, the consumer shall release the buffer newBuffer->release;

  6. Again, for safety, please initialize newBuffer to NULL after this step.

With these changes, I presume your code should work fine based on your description.

Ganesh
  • 5,880
  • 2
  • 36
  • 54
  • Should buffer be `acquire_buffer`d in the same thread as `mSource->read` happens or it can be prepared in advance? I.e. can I acquire new buffers from one thread while other thread `release`ing old buffers? – Vi. Feb 03 '14 at 11:38
  • @Vi.. As long as the references are the same, you can `acquire` and `release` the buffers across 2 threads. I presume you would store the correct reference after you have `acquire`d the same, which is then `release`d from a different thread. It may also help to use a mutex to avoid accidental simultaneous accesses. – Ganesh Feb 03 '14 at 14:05
  • After I moved `acquire buffer` into the `read` itself (using my own queue instead of MediaBufferGroup), it started working more or less without failures. – Vi. Feb 03 '14 at 16:42
  • @Vi..Is it possible to share your code or atleast the relevant parts?? – Ganesh Feb 03 '14 at 17:12
0

MediaBuffer is a basic container in stagefright framework.

About the usage of MediaBuffer/MediaBufferGroup/MediaSource, There are some simple examples code under the ASOP frameworks/av/cmds/stagefright.

Pay attention to the implementation of class SineSource and its usage.

waveacme
  • 311
  • 3
  • 4