title: min undequeued buffer count exceeded camera2
body: <p>I have an Android application that takes a photo without a preview surface. I have implemented this with the new camera2 for Lollipop - api 21</p>
  
  <p>The code is like this:</p>
  
  <pre><code>                final CaptureRequest.Builder jpegRequest = camera.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
                  // Render to our image reader:
                  jpegRequest.addTarget(imageReaderSurface);
                  // Configure auto-focus (AF) and auto-exposure (AE) modes:
                  jpegRequest.set(CaptureRequest.CONTROL_AF_MODE, CameraMetadata.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
                  jpegRequest.set(CaptureRequest.CONTROL_AE_MODE, autoExposureModeFinal);
                  jpegRequest.set(CaptureRequest.JPEG_ORIENTATION, correctOrientation(context, characteristics));
  
                  if (previews == 0) {
                      saveImage(camera, session, jpegRequest.build());
                  } else {
                      // Take previews to dummySurface to allow camera to warm up before saving jpeg:
                      final CaptureRequest.Builder captureBuilder = camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
                      // Render to our dummy preview surface:
                      captureBuilder.addTarget(outputSurfaces.get(1));
                      // Configure auto-focus (AF) and auto-exposure (AE) modes:
                      captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, CameraMetadata.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
                      captureBuilder.set(CaptureRequest.CONTROL_AE_MODE, autoExposureModeFinal);
  
                      session.setRepeatingRequest(captureBuilder.build(), new CameraCaptureSession.CaptureCallback() {
                          int mNumCaptures;
  
                          @Override
                          public void onCaptureCompleted(CameraCaptureSession completedSession, CaptureRequest request, TotalCaptureResult result) {
                              //Log.d("mNumCaptures", String.valueOf(mNumCaptures));
                              if (++mNumCaptures == previews) {
                                  try {
                                      completedSession.stopRepeating();
                                      saveImage(camera, session, jpegRequest.build());
                                      //session.abortCaptures();
                                      closeCamera(camera, looper);
                                  } catch (CameraAccessException e) {
                                      error("Error saving image", e);
                                      closeCamera(camera, looper);
                                  }
                              }
                          }
                      }, null);
  
                  }
  </code></pre>
  
  <p>I have tested this on a nexus 5x with Android 6 and it works. previews is set to 20 - you need to capture more images in order for the camera to adapt to the light.</p>
  
  <p>I have tested this code with Galaxy s5 - Android 5 and it does not work.
  I get this error: min undequeued buffer count exceeded</p>
  
  <pre><code>03-16 15:01:09.569: W/ArrayUtils(30646): Ignoring invalid value emboss
  03-16 15:01:09.569: W/ArrayUtils(30646): Ignoring invalid value sketch
  03-16 15:01:09.569: W/ArrayUtils(30646): Ignoring invalid value neon
  03-16 15:01:09.599: I/CameraManager(30646): Using legacy camera HAL.
  03-16 15:01:10.059: W/ArrayUtils(30646): Ignoring invalid value emboss
  03-16 15:01:10.059: W/ArrayUtils(30646): Ignoring invalid value sketch
  03-16 15:01:10.059: W/ArrayUtils(30646): Ignoring invalid value neon
  03-16 15:01:10.089: W/ArrayUtils(30646): Ignoring invalid value emboss
  03-16 15:01:10.089: W/ArrayUtils(30646): Ignoring invalid value sketch
  03-16 15:01:10.089: W/ArrayUtils(30646): Ignoring invalid value neon
  03-16 15:01:10.109: I/CameraDeviceState(30646): Legacy camera service transitioning to state CONFIGURING
  03-16 15:01:10.109: I/RequestThread-0(30646): Configure outputs: 2 surfaces configured.
  03-16 15:01:10.109: D/Camera(30646): app passed NULL surface
  03-16 15:01:10.119: I/RequestThread-0(30646): configureOutputs - set take picture size to 800x480
  03-16 15:01:10.209: I/CameraDeviceState(30646): Legacy camera service transitioning to state IDLE
  03-16 15:01:10.219: D/info(30646): Not using a front facing camera.
  03-16 15:01:10.229: D/info(30646): Sensor orientation: 90 degrees
  03-16 15:01:10.229: D/info(30646): Device orientation: 0 degrees
  03-16 15:01:10.229: D/info(30646): Returning JPEG orientation of 90 degrees
  03-16 15:01:10.229: I/RequestQueue(30646): Repeating capture request set.
  03-16 15:01:10.239: W/LegacyRequestMapper(30646): convertRequestMetadata - control.awbRegions setting is not supported, ignoring value
  03-16 15:01:10.239: W/LegacyRequestMapper(30646): Only received metering rectangles with weight 0.
  03-16 15:01:10.239: W/LegacyRequestMapper(30646): Only received metering rectangles with weight 0.
  03-16 15:01:11.019: I/CameraDeviceState(30646): Legacy camera service transitioning to state CAPTURING
  03-16 15:01:11.209: E/BufferQueueProducer(30646): [unnamed-30646-2] dequeueBuffer: min undequeued buffer count (2) exceeded (dequeued=5 undequeued=1)
  03-16 15:01:14.039: I/RequestQueue(30646): Repeating capture request cancelled.
  03-16 15:01:14.439: E/BufferQueueProducer(30646): [unnamed-30646-2] cancelBuffer: BufferQueue has been abandoned
  03-16 15:01:14.439: E/BufferQueueProducer(30646): [unnamed-30646-2] cancelBuffer: BufferQueue has been abandoned
  03-16 15:01:14.439: E/BufferQueueProducer(30646): [unnamed-30646-2] cancelBuffer: BufferQueue has been abandoned
  03-16 15:01:14.439: E/BufferQueueProducer(30646): [unnamed-30646-2] cancelBuffer: BufferQueue has been abandoned
  </code></pre>
  
  <p>I get the same error even if I set preview to 0.</p>
  
  <p>I have found some topics about the error "min undequeued buffer count exceeded" but all of them are related to the deprecated camera class.
  Do yo know if I could do something to fix this on Android 5? </p>
  
  <p>Thanks</p>