Align the order of buffers to the listed cameras

There is no guarantee that the order of the EVS incoming buffers follows
the order of listed cameras in the config. So I added a logic to check
the deviceId of the buffers, and re-order them based on the camera ids.

Also added locks to protect the doneWithFrame_1_1 method calls.

Bug:160805910
Bug: 162599435
Test: Build and run on local machine.

Change-Id: I150b1fa5f38c83d6a5a4385d03181878ba3182b5
Merged-In: I150b1fa5f38c83d6a5a4385d03181878ba3182b5
(cherry picked from commit cf3ea74f7a528e16966330a4199c6e4cd78c7189)
diff --git a/surround_view/service-impl/SurroundView2dSession.cpp b/surround_view/service-impl/SurroundView2dSession.cpp
index 26c42b9..823907a 100644
--- a/surround_view/service-impl/SurroundView2dSession.cpp
+++ b/surround_view/service-impl/SurroundView2dSession.cpp
@@ -102,18 +102,40 @@
     }
 
     if (buffers.size() != kNumFrames) {
+        scoped_lock<mutex> lock(mSession->mAccessLock);
         LOG(ERROR) << "The number of incoming frames is " << buffers.size()
                    << ", which is different from the number " << kNumFrames
                    << ", specified in config file";
+        mSession->mProcessingEvsFrames = false;
+        mCamera->doneWithFrame_1_1(buffers);
         return {};
     }
 
     {
         scoped_lock<mutex> lock(mSession->mAccessLock);
+        vector<int> indices;
+        for (const auto& id
+                : mSession->mIOModuleConfig->cameraConfig.evsCameraIds) {
+            for (int i = 0; i < kNumFrames; i++) {
+                if (buffers[i].deviceId == id) {
+                    indices.emplace_back(i);
+                    break;
+                }
+            }
+        }
+
+        if (indices.size() != kNumFrames) {
+            LOG(ERROR) << "The frames are not from the cameras we expected!";
+            mSession->mProcessingEvsFrames = false;
+            mCamera->doneWithFrame_1_1(buffers);
+            return {};
+        }
+
         for (int i = 0; i < kNumFrames; i++) {
-            LOG(DEBUG) << "Copying buffer No." << i
-                       << " to Surround View Service";
-            mSession->copyFromBufferToPointers(buffers[i],
+            LOG(DEBUG) << "Copying buffer from camera ["
+                       << buffers[indices[i]].deviceId
+                       << "] to Surround View Service";
+            mSession->copyFromBufferToPointers(buffers[indices[i]],
                                                mSession->mInputPointers[i]);
         }
     }
diff --git a/surround_view/service-impl/SurroundView3dSession.cpp b/surround_view/service-impl/SurroundView3dSession.cpp
index 6ee4a32..f2d7c2e 100644
--- a/surround_view/service-impl/SurroundView3dSession.cpp
+++ b/surround_view/service-impl/SurroundView3dSession.cpp
@@ -108,18 +108,47 @@
     }
 
     if (buffers.size() != kNumFrames) {
+        scoped_lock<mutex> lock(mSession->mAccessLock);
         LOG(ERROR) << "The number of incoming frames is " << buffers.size()
                    << ", which is different from the number " << kNumFrames
                    << ", specified in config file";
+        mSession->mProcessingEvsFrames = false;
+        mCamera->doneWithFrame_1_1(buffers);
         return {};
     }
 
     {
         scoped_lock<mutex> lock(mSession->mAccessLock);
+
+        // The incoming frames may not follow the same order as listed cameras.
+        // We should re-order them following the camera ids listed in camera
+        // config.
+        vector<int> indices;
+        for (const auto& id
+                : mSession->mIOModuleConfig->cameraConfig.evsCameraIds) {
+            for (int i = 0; i < kNumFrames; i++) {
+                if (buffers[i].deviceId == id) {
+                    indices.emplace_back(i);
+                    break;
+                }
+            }
+        }
+
+        // If the size of indices is smaller than the kNumFrames, it means that
+        // there is frame(s) that comes from different camera(s) than we
+        // expected.
+        if (indices.size() != kNumFrames) {
+            LOG(ERROR) << "The frames are not from the cameras we expected!";
+            mSession->mProcessingEvsFrames = false;
+            mCamera->doneWithFrame_1_1(buffers);
+            return {};
+        }
+
         for (int i = 0; i < kNumFrames; i++) {
-            LOG(DEBUG) << "Copying buffer No." << i
-                       << " to Surround View Service";
-            mSession->copyFromBufferToPointers(buffers[i],
+            LOG(DEBUG) << "Copying buffer from camera ["
+                       << buffers[indices[i]].deviceId
+                       << "] to Surround View Service";
+            mSession->copyFromBufferToPointers(buffers[indices[i]],
                                                mSession->mInputPointers[i]);
         }
     }