Camera2: Clean up corner case error handling

- If a session is closed, and a new session is created immediately
afterwards, but then fails to be configured, the first session sees an
onUnconfigured call which it wasn't expecting, and throws an
exception on an internal thread, leading to app death.
Add a guard against this case.

- If the lower levels skip a frame (illegal per design), be slightly more
robust to that by accepting any successful result as the latest completed
frame, instead of just incrementing the completed frame count. This will
lead to missing results, but should allow shutdown, etc, to complete
cleanly.

- Convert TIMED_OUT error codes to CAMERA_ERROR CameraAccessExceptions.
This is a common error code returned by waitUntilIdle.

Also, improve debug logging to log a session ID with verbose logging,
and add a few verbose logs.

Bug: 16899526
Change-Id: I7a31f0a12effc2611e1f9c2408224ee82c37c912
diff --git a/core/java/android/hardware/camera2/impl/CameraDeviceImpl.java b/core/java/android/hardware/camera2/impl/CameraDeviceImpl.java
index f011d60..f917fdc 100644
--- a/core/java/android/hardware/camera2/impl/CameraDeviceImpl.java
+++ b/core/java/android/hardware/camera2/impl/CameraDeviceImpl.java
@@ -99,6 +99,7 @@
     private final FrameNumberTracker mFrameNumberTracker = new FrameNumberTracker();
 
     private CameraCaptureSessionImpl mCurrentSession;
+    private int mNextSessionId = 0;
 
     // Runnables for all state transitions, except error, which needs the
     // error code argument
@@ -445,7 +446,8 @@
 
             // Fire onConfigured if configureOutputs succeeded, fire onConfigureFailed otherwise.
             CameraCaptureSessionImpl newSession =
-                    new CameraCaptureSessionImpl(outputs, callback, handler, this, mDeviceHandler,
+                    new CameraCaptureSessionImpl(mNextSessionId++,
+                            outputs, callback, handler, this, mDeviceHandler,
                             configureSuccess);
 
             // TODO: wait until current session closes, then create the new session
@@ -1003,8 +1005,10 @@
                     Log.e(TAG, String.format(
                             "result frame number %d comes out of order, should be %d + 1",
                             frameNumber, mCompletedFrameNumber));
+                    // Continue on to set the completed frame number to this frame anyway,
+                    // to be robust to lower-level errors and allow for clean shutdowns.
                 }
-                mCompletedFrameNumber++;
+                mCompletedFrameNumber = frameNumber;
             }
             update();
         }