Fix a race condition in CCodecBufferChannel::requestInitialInputBuffers

requestInitialInputBuffers() send buffers to the client, so it should be
guarded behind QueueGuard. The lack of the guard led to input buffers
being reported while the codec is not running, thus making the MediaCodec
internal state inconsistent.

Change-Id: Ibf6ca49a656c675314faa691c6bd3ff2911a2fbb
Test: atest MctsMediaV2TestCases
Bug: 343502509
Flag: android.media.codec.codec_buffer_state_cleanup
diff --git a/media/codec2/sfplugin/CCodecBufferChannel.cpp b/media/codec2/sfplugin/CCodecBufferChannel.cpp
index 3ef2f84..acf1c0b 100644
--- a/media/codec2/sfplugin/CCodecBufferChannel.cpp
+++ b/media/codec2/sfplugin/CCodecBufferChannel.cpp
@@ -2052,6 +2052,14 @@
 
 status_t CCodecBufferChannel::requestInitialInputBuffers(
         std::map<size_t, sp<MediaCodecBuffer>> &&clientInputBuffers) {
+    std::optional<QueueGuard> guard;
+    if (android::media::codec::provider_->codec_buffer_state_cleanup()) {
+        guard.emplace(mSync);
+        if (!guard->isRunning()) {
+            ALOGD("[%s] skip requestInitialInputBuffers when not running", mName);
+            return OK;
+        }
+    }
     C2StreamBufferTypeSetting::output oStreamFormat(0u);
     C2PrependHeaderModeSetting prepend(PREPEND_HEADER_TO_NONE);
     c2_status_t err = mComponent->query({ &oStreamFormat, &prepend }, {}, C2_DONT_BLOCK, nullptr);