heif: reserve item ids at track start

When multiple images are encoded, samples from different
image tracks could arrive almost simultaneously. If the writer
thread is busy writing when bufferChunk is called, multiple
tracks could get blocked on the mLock, and when the writer
enters wait, a random track will be awakened to queue the sample,
not following the original order when they got blocked.

This will result in the images not being sorted to the original
specified order. The file is still well-formed and the primary
index will point to the correct image content, but the re-ordering
could be undesirable (and unexpected by the client).

Fixing this by reserving the item ids for image tracks upfront.
Actual samples could be written at random order, but item ids
will be preserved.

bug: 147111445
test: android.media.cts.HeifWriterTest#testInputBuffer_NoGrid*
modified to run 100x

Change-Id: I62ceca5ab2a04f2ebbf5825b667b8f34ae798041
2 files changed