CCodecBufferChannel: Add the allocated block to ref-tracker in EncryptedLinearInputBuffers

When a EncryptedLinearBlockBuffer is allocated, update the
weak-reference to that block in the corresponding slot so
it does not get overwritten by subsequent allocations.

Fixes corruption issue with secure video playback (esp. for apps
that do not call MediaCodec.getInputBuffer() for every input available;
but rely on {index, ByteBuffer} association from an initial call to
MediaCodec.getInputBuffers(..))

Bug: 117861662
Test: play video on Netflix player, Youtube, Local playback

Change-Id: I9200438a552a20cfabe0ee5f39e6d56729205ae9
(cherry picked from commit 075f99539e1e7782369cf812e15417b325fc43e5)
diff --git a/media/sfplugin/CCodecBufferChannel.cpp b/media/sfplugin/CCodecBufferChannel.cpp
index ea8f015..69e59e1 100644
--- a/media/sfplugin/CCodecBufferChannel.cpp
+++ b/media/sfplugin/CCodecBufferChannel.cpp
@@ -789,7 +789,7 @@
         return std::move(array);
     }
 
-    virtual sp<Codec2Buffer> alloc(size_t size) const {
+    virtual sp<Codec2Buffer> alloc(size_t size) {
         C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
         std::shared_ptr<C2LinearBlock> block;
 
@@ -837,11 +837,12 @@
     ~EncryptedLinearInputBuffers() override {
     }
 
-    sp<Codec2Buffer> alloc(size_t size) const override {
+    sp<Codec2Buffer> alloc(size_t size) override {
         sp<IMemory> memory;
-        for (const Entry &entry : mMemoryVector) {
-            if (entry.block.expired()) {
-                memory = entry.memory;
+        size_t slot = 0;
+        for (; slot < mMemoryVector.size(); ++slot) {
+            if (mMemoryVector[slot].block.expired()) {
+                memory = mMemoryVector[slot].memory;
                 break;
             }
         }
@@ -851,10 +852,11 @@
 
         std::shared_ptr<C2LinearBlock> block;
         c2_status_t err = mPool->fetchLinearBlock(size, mUsage, &block);
-        if (err != C2_OK) {
+        if (err != C2_OK || block == nullptr) {
             return nullptr;
         }
 
+        mMemoryVector[slot].block = block;
         return new EncryptedLinearBlockBuffer(mFormat, block, memory, mHeapSeqNum);
     }