ProCamera: add waitForFrameBuffer/waitForFrameResult blocking calls

Change-Id: I851d41aeecaa15245d5b9d622132e8706d6e292c
diff --git a/include/camera/ProCamera.h b/include/camera/ProCamera.h
index 11904f9..f813c1c 100644
--- a/include/camera/ProCamera.h
+++ b/include/camera/ProCamera.h
@@ -24,8 +24,12 @@
 #include <camera/IProCameraCallbacks.h>
 #include <camera/IProCameraUser.h>
 #include <camera/Camera.h>
+#include <camera/CameraMetadata.h>
 #include <gui/CpuConsumer.h>
 
+#include <utils/Condition.h>
+#include <utils/Mutex.h>
+
 struct camera_metadata;
 
 namespace android {
@@ -62,6 +66,20 @@
       *    free_camera_metadata.
       */
     virtual void onResultReceived(int32_t frameId, camera_metadata* result) = 0;
+
+
+    // A new frame buffer has been received for this stream.
+    // -- This callback only fires for createStreamCpu streams
+    // -- Use buf.timestamp to correlate with metadata's android.sensor.timestamp
+    // -- The buffer should be accessed with CpuConsumer::lockNextBuffer
+    //      and CpuConsumer::unlockBuffer
+    virtual void onFrameAvailable(int streamId,
+                                  const sp<CpuConsumer>& cpuConsumer) {
+    }
+
+    virtual bool useOnFrameAvailable() {
+        return false;
+    }
 };
 
 class ProCamera : public BnProCameraCallbacks, public IBinder::DeathRecipient
@@ -161,6 +179,7 @@
     status_t createStreamCpu(int width, int height, int format,
                           int heapCount,
                           /*out*/
+                          sp<CpuConsumer>* cpuConsumer,
                           int* streamId);
 
     // Create a request object from a template.
@@ -174,6 +193,24 @@
     // Get static camera metadata
     camera_metadata* getCameraInfo(int cameraId);
 
+    // Blocks until a frame is available (CPU streams only)
+    // - Obtain the frame data by calling CpuConsumer::lockNextBuffer
+    // - Release the frame data after use with CpuConsumer::unlockBuffer
+    // Error codes:
+    // -ETIMEDOUT if it took too long to get a frame
+    status_t waitForFrameBuffer(int streamId);
+
+    // Blocks until a metadata result is available
+    // - Obtain the metadata by calling consumeFrameMetadata()
+    // Error codes:
+    // -ETIMEDOUT if it took too long to get a frame
+    status_t waitForFrameMetadata();
+
+    // Get the latest metadata. This is destructive.
+    // - Calling this repeatedly will produce empty metadata objects.
+    // - Use waitForFrameMetadata to sync until new data is available.
+    CameraMetadata consumeFrameMetadata();
+
     sp<IProCameraUser>         remote();
 
 protected:
@@ -249,6 +286,7 @@
         StreamInfo(int streamId) {
             this->streamID = streamId;
             cpuStream = false;
+            frameReady = false;
         }
 
         StreamInfo() {
@@ -261,10 +299,15 @@
         sp<CpuConsumer> cpuConsumer;
         sp<ProFrameListener> frameAvailableListener;
         sp<Surface> stc;
+        bool frameReady;
     };
 
+    Condition mWaitCondition;
+    Mutex     mWaitMutex;
+    static const nsecs_t mWaitTimeout = 1000000000; // 1sec
     KeyedVector<int, StreamInfo> mStreams;
-
+    bool mMetadataReady;
+    CameraMetadata mLatestMetadata;
 
     void onFrameAvailable(int streamId);