Home | History | Annotate | Download | only in camera2
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.hardware.camera2;
     18 
     19 import android.annotation.NonNull;
     20 import android.annotation.Nullable;
     21 import android.os.Handler;
     22 import android.view.Surface;
     23 
     24 import java.util.List;
     25 
     26 
     27 /**
     28  * A configured capture session for a {@link CameraDevice}, used for capturing images from the
     29  * camera or reprocessing images captured from the camera in the same session previously.
     30  *
     31  * <p>A CameraCaptureSession is created by providing a set of target output surfaces to
     32  * {@link CameraDevice#createCaptureSession createCaptureSession}, or by providing an
     33  * {@link android.hardware.camera2.params.InputConfiguration} and a set of target output surfaces to
     34  * {@link CameraDevice#createReprocessableCaptureSession createReprocessableCaptureSession} for a
     35  * reprocessable capture session. Once created, the session is active until a new session is
     36  * created by the camera device, or the camera device is closed.</p>
     37  *
     38  * <p>All capture sessions can be used for capturing images from the camera but only reprocessable
     39  * capture sessions can reprocess images captured from the camera in the same session previously.
     40  * </p>
     41  *
     42  * <p>Creating a session is an expensive operation and can take several hundred milliseconds, since
     43  * it requires configuring the camera device's internal pipelines and allocating memory buffers for
     44  * sending images to the desired targets. Therefore the setup is done asynchronously, and
     45  * {@link CameraDevice#createCaptureSession createCaptureSession} and
     46  * {@link CameraDevice#createReprocessableCaptureSession createReprocessableCaptureSession} will
     47  * send the ready-to-use CameraCaptureSession to the provided listener's
     48  * {@link CameraCaptureSession.StateCallback#onConfigured onConfigured} callback. If configuration
     49  * cannot be completed, then the
     50  * {@link CameraCaptureSession.StateCallback#onConfigureFailed onConfigureFailed} is called, and the
     51  * session will not become active.</p>
     52  *<!--
     53  * <p>Any capture requests (repeating or non-repeating) submitted before the session is ready will
     54  * be queued up and will begin capture once the session becomes ready. In case the session cannot be
     55  * configured and {@link StateCallback#onConfigureFailed onConfigureFailed} is called, all queued
     56  * capture requests are discarded.</p>
     57  *-->
     58  * <p>If a new session is created by the camera device, then the previous session is closed, and its
     59  * associated {@link StateCallback#onClosed onClosed} callback will be invoked.  All
     60  * of the session methods will throw an IllegalStateException if called once the session is
     61  * closed.</p>
     62  *
     63  * <p>A closed session clears any repeating requests (as if {@link #stopRepeating} had been called),
     64  * but will still complete all of its in-progress capture requests as normal, before a newly
     65  * created session takes over and reconfigures the camera device.</p>
     66  */
     67 public abstract class CameraCaptureSession implements AutoCloseable {
     68 
     69     /**
     70      * Used to identify invalid session ID.
     71      * @hide
     72      */
     73     public static final int SESSION_ID_NONE = -1;
     74 
     75     /**
     76      * Get the camera device that this session is created for.
     77      */
     78     @NonNull
     79     public abstract CameraDevice getDevice();
     80 
     81     /**
     82      * <p>Pre-allocate all buffers for an output Surface.</p>
     83      *
     84      * <p>Normally, the image buffers for a given output Surface are allocated on-demand,
     85      * to minimize startup latency and memory overhead.</p>
     86      *
     87      * <p>However, in some cases, it may be desirable for the buffers to be allocated before
     88      * any requests targeting the Surface are actually submitted to the device. Large buffers
     89      * may take some time to allocate, which can result in delays in submitting requests until
     90      * sufficient buffers are allocated to reach steady-state behavior. Such delays can cause
     91      * bursts to take longer than desired, or cause skips or stutters in preview output.</p>
     92      *
     93      * <p>The prepare() method can be used to perform this preallocation. It may only be called for
     94      * a given output Surface before that Surface is used as a target for a request. The number of
     95      * buffers allocated is the sum of the count needed by the consumer providing the output
     96      * Surface, and the maximum number needed by the camera device to fill its pipeline. Since this
     97      * may be a larger number than what is actually required for steady-state operation, using
     98      * prepare may result in higher memory consumption than the normal on-demand behavior results
     99      * in. Prepare() will also delay the time to first output to a given Surface, in exchange for
    100      * smoother frame rate once the allocation is complete.</p>
    101      *
    102      * <p>For example, an application that creates an
    103      * {@link android.media.ImageReader#newInstance ImageReader} with a maxImages argument of 10,
    104      * but only uses 3 simultaneous Images at once would normally only cause those 3 images to be
    105      * allocated (plus what is needed by the camera device for smooth operation).  But using
    106      * prepare() on the ImageReader Surface will result in all 10 Images being allocated. So
    107      * applications using this method should take care to request only the number of buffers
    108      * actually necessary for their application.</p>
    109      *
    110      * <p>If the same output Surface is used in consecutive sessions (without closing the first
    111      * session explicitly), then its already-allocated buffers are carried over, and if it was
    112      * used as a target of a capture request in the first session, prepare cannot be called on it
    113      * in the second session.</p>
    114      *
    115      * <p>Once allocation is complete, {@link StateCallback#onSurfacePrepared} will be invoked with
    116      * the Surface provided to this method. Between the prepare call and the onSurfacePrepared call,
    117      * the Surface provided to prepare must not be used as a target of a CaptureRequest submitted
    118      * to this session.</p>
    119      *
    120      * <p>{@link android.hardware.camera2.CameraCharacteristics#INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY LEGACY}
    121      * devices cannot pre-allocate output buffers; for those devices,
    122      * {@link StateCallback#onSurfacePrepared} will be immediately called, and no preallocation is
    123      * done.</p>
    124      *
    125      * @param surface the output Surface for which buffers should be pre-allocated. Must be one of
    126      * the output Surfaces used to create this session.
    127      *
    128      * @throws CameraAccessException if the camera device is no longer connected or has
    129      *                               encountered a fatal error
    130      * @throws IllegalStateException if this session is no longer active, either because the session
    131      *                               was explicitly closed, a new session has been created
    132      *                               or the camera device has been closed.
    133      * @throws IllegalArgumentException if the Surface is invalid, not part of this Session, or has
    134      *                                  already been used as a target of a CaptureRequest in this
    135      *                                  session or immediately prior sessions.
    136      *
    137      * @see StateCallback#onSurfacePrepared
    138      */
    139     public abstract void prepare(@NonNull Surface surface) throws CameraAccessException;
    140 
    141 
    142     /**
    143      * <p>Free all buffers allocated for an output Surface.</p>
    144      *
    145      * <p>Normally, once allocated, the image buffers for a given output Surface remain allocated
    146      * for the lifetime of the capture session, to minimize latency of captures and to reduce
    147      * memory allocation overhead.</p>
    148      *
    149      * <p>However, in some cases, it may be desirable for allocated buffers to be freed to reduce
    150      * the application's memory consumption, if the particular output Surface will not be used by
    151      * the application for some time.</p>
    152      *
    153      * <p>The tearDown() method can be used to perform this operation. After the call finishes, all
    154      * unfilled image buffers will have been freed. Any future use of the target Surface may require
    155      * allocation of additional buffers, as if the session had just been created.  Buffers being
    156      * held by the application (either explicitly as Image objects from ImageReader, or implicitly
    157      * as the current texture in a SurfaceTexture or the current contents of a RS Allocation, will
    158      * remain valid and allocated even when tearDown is invoked.</p>
    159      *
    160      * <p>A Surface that has had tearDown() called on it is eligible to have prepare() invoked on it
    161      * again even if it was used as a request target before the tearDown() call, as long as it
    162      * doesn't get used as a target of a request between the tearDown() and prepare() calls.</p>
    163      *
    164      * @param surface the output Surface for which buffers should be freed. Must be one of the
    165      * the output Surfaces used to create this session.
    166      *
    167      * @throws CameraAccessException if the camera device is no longer connected or has
    168      *                               encountered a fatal error.
    169      * @throws IllegalStateException if this session is no longer active, either because the session
    170      *                               was explicitly closed, a new session has been created
    171      *                               or the camera device has been closed.
    172      * @throws IllegalArgumentException if the Surface is invalid, not part of this Session, or has
    173      *                                  already been used as a target of a CaptureRequest in this
    174      *                                  session or immediately prior sessions.
    175      *
    176      * @hide
    177      */
    178     public abstract void tearDown(@NonNull Surface surface) throws CameraAccessException;
    179 
    180     /**
    181      * <p>Submit a request for an image to be captured by the camera device.</p>
    182      *
    183      * <p>The request defines all the parameters for capturing the single image,
    184      * including sensor, lens, flash, and post-processing settings.</p>
    185      *
    186      * <p>Each request will produce one {@link CaptureResult} and produce new frames for one or more
    187      * target Surfaces, set with the CaptureRequest builder's
    188      * {@link CaptureRequest.Builder#addTarget} method. The target surfaces (set with
    189      * {@link CaptureRequest.Builder#addTarget}) must be a subset of the surfaces provided when this
    190      * capture session was created.</p>
    191      *
    192      * <p>Multiple regular and reprocess requests can be in progress at once. If there are only
    193      * regular requests or reprocess requests in progress, they are processed in first-in,
    194      * first-out order. If there are both regular and reprocess requests in progress, regular
    195      * requests are processed in first-in, first-out order and reprocess requests are processed in
    196      * first-in, first-out order, respectively. However, the processing order of a regular request
    197      * and a reprocess request in progress is not specified. In other words, a regular request
    198      * will always be processed before regular requets that are submitted later. A reprocess request
    199      * will always be processed before reprocess requests that are submitted later. However, a
    200      * regular request may not be processed before reprocess requests that are submitted later.<p>
    201      *
    202      * <p>Requests submitted through this method have higher priority than
    203      * those submitted through {@link #setRepeatingRequest} or
    204      * {@link #setRepeatingBurst}, and will be processed as soon as the current
    205      * repeat/repeatBurst processing completes.</p>
    206      *
    207      * <p>All capture sessions can be used for capturing images from the camera but only capture
    208      * sessions created by
    209      * {@link CameraDevice#createReprocessableCaptureSession createReprocessableCaptureSession}
    210      * can submit reprocess capture requests. Submitting a reprocess request to a regular capture
    211      * session will result in an {@link IllegalArgumentException}.</p>
    212      *
    213      * @param request the settings for this capture
    214      * @param listener The callback object to notify once this request has been
    215      * processed. If null, no metadata will be produced for this capture,
    216      * although image data will still be produced.
    217      * @param handler the handler on which the listener should be invoked, or
    218      * {@code null} to use the current thread's {@link android.os.Looper
    219      * looper}.
    220      *
    221      * @return int A unique capture sequence ID used by
    222      *             {@link CaptureCallback#onCaptureSequenceCompleted}.
    223      *
    224      * @throws CameraAccessException if the camera device is no longer connected or has
    225      *                               encountered a fatal error
    226      * @throws IllegalStateException if this session is no longer active, either because the session
    227      *                               was explicitly closed, a new session has been created
    228      *                               or the camera device has been closed.
    229      * @throws IllegalArgumentException if the request targets no Surfaces or Surfaces that are not
    230      *                                  configured as outputs for this session; or the request
    231      *                                  targets a set of Surfaces that cannot be submitted
    232      *                                  simultaneously in a reprocessable capture session; or a
    233      *                                  reprocess capture request is submitted in a
    234      *                                  non-reprocessable capture session; or the reprocess capture
    235      *                                  request was created with a {@link TotalCaptureResult} from
    236      *                                  a different session; or the capture targets a Surface in
    237      *                                  the middle of being {@link #prepare prepared}; or the
    238      *                                  handler is null, the listener is not null, and the calling
    239      *                                  thread has no looper.
    240      *
    241      * @see #captureBurst
    242      * @see #setRepeatingRequest
    243      * @see #setRepeatingBurst
    244      * @see #abortCaptures
    245      * @see CameraDevice#createReprocessableCaptureSession
    246      */
    247     public abstract int capture(@NonNull CaptureRequest request,
    248             @Nullable CaptureCallback listener, @Nullable Handler handler)
    249             throws CameraAccessException;
    250 
    251     /**
    252      * Submit a list of requests to be captured in sequence as a burst. The
    253      * burst will be captured in the minimum amount of time possible, and will
    254      * not be interleaved with requests submitted by other capture or repeat
    255      * calls.
    256      *
    257      * <p>Regular and reprocess requests can be mixed together in a single burst. Regular requests
    258      * will be captured in order and reprocess requests will be processed in order, respectively.
    259      * However, the processing order between a regular request and a reprocess request is not
    260      * specified. Each capture produces one {@link CaptureResult} and image buffers for one or more
    261      * target {@link android.view.Surface surfaces}. The target surfaces (set with
    262      * {@link CaptureRequest.Builder#addTarget}) must be a subset of the surfaces provided when
    263      * this capture session was created.</p>
    264      *
    265      * <p>The main difference between this method and simply calling
    266      * {@link #capture} repeatedly is that this method guarantees that no
    267      * other requests will be interspersed with the burst.</p>
    268      *
    269      * <p>All capture sessions can be used for capturing images from the camera but only capture
    270      * sessions created by
    271      * {@link CameraDevice#createReprocessableCaptureSession createReprocessableCaptureSession}
    272      * can submit reprocess capture requests. Submitting a reprocess request to a regular
    273      * capture session will result in an {@link IllegalArgumentException}.</p>
    274      *
    275      * @param requests the list of settings for this burst capture
    276      * @param listener The callback object to notify each time one of the
    277      * requests in the burst has been processed. If null, no metadata will be
    278      * produced for any requests in this burst, although image data will still
    279      * be produced.
    280      * @param handler the handler on which the listener should be invoked, or
    281      * {@code null} to use the current thread's {@link android.os.Looper
    282      * looper}.
    283      *
    284      * @return int A unique capture sequence ID used by
    285      *             {@link CaptureCallback#onCaptureSequenceCompleted}.
    286      *
    287      * @throws CameraAccessException if the camera device is no longer connected or has
    288      *                               encountered a fatal error
    289      * @throws IllegalStateException if this session is no longer active, either because the session
    290      *                               was explicitly closed, a new session has been created
    291      *                               or the camera device has been closed.
    292      * @throws IllegalArgumentException If the requests target no Surfaces, or the requests target
    293      *                                  Surfaces not currently configured as outputs; or one of the
    294      *                                  requests targets a set of Surfaces that cannot be submitted
    295      *                                  simultaneously in a reprocessable capture session; or a
    296      *                                  reprocess capture request is submitted in a
    297      *                                  non-reprocessable capture session; or one of the reprocess
    298      *                                  capture requests was created with a
    299      *                                  {@link TotalCaptureResult} from a different session; or one
    300      *                                  of the captures targets a Surface in the middle of being
    301      *                                  {@link #prepare prepared}; or if the handler is null, the
    302      *                                  listener is not null, and the calling thread has no looper.
    303      *
    304      * @see #capture
    305      * @see #setRepeatingRequest
    306      * @see #setRepeatingBurst
    307      * @see #abortCaptures
    308      */
    309     public abstract int captureBurst(@NonNull List<CaptureRequest> requests,
    310             @Nullable CaptureCallback listener, @Nullable Handler handler)
    311             throws CameraAccessException;
    312 
    313     /**
    314      * Request endlessly repeating capture of images by this capture session.
    315      *
    316      * <p>With this method, the camera device will continually capture images
    317      * using the settings in the provided {@link CaptureRequest}, at the maximum
    318      * rate possible.</p>
    319      *
    320      * <p>Repeating requests are a simple way for an application to maintain a
    321      * preview or other continuous stream of frames, without having to
    322      * continually submit identical requests through {@link #capture}.</p>
    323      *
    324      * <p>Repeat requests have lower priority than those submitted
    325      * through {@link #capture} or {@link #captureBurst}, so if
    326      * {@link #capture} is called when a repeating request is active, the
    327      * capture request will be processed before any further repeating
    328      * requests are processed.<p>
    329      *
    330      * <p>To stop the repeating capture, call {@link #stopRepeating}. Calling
    331      * {@link #abortCaptures} will also clear the request.</p>
    332      *
    333      * <p>Calling this method will replace any earlier repeating request or
    334      * burst set up by this method or {@link #setRepeatingBurst}, although any
    335      * in-progress burst will be completed before the new repeat request will be
    336      * used.</p>
    337      *
    338      * <p>This method does not support reprocess capture requests because each reprocess
    339      * {@link CaptureRequest} must be created from the {@link TotalCaptureResult} that matches
    340      * the input image to be reprocessed. This is either the {@link TotalCaptureResult} of capture
    341      * that is sent for reprocessing, or one of the {@link TotalCaptureResult TotalCaptureResults}
    342      * of a set of captures, when data from the whole set is combined by the application into a
    343      * single reprocess input image. The request must be capturing images from the camera. If a
    344      * reprocess capture request is submitted, this method will throw IllegalArgumentException.</p>
    345      *
    346      * @param request the request to repeat indefinitely
    347      * @param listener The callback object to notify every time the
    348      * request finishes processing. If null, no metadata will be
    349      * produced for this stream of requests, although image data will
    350      * still be produced.
    351      * @param handler the handler on which the listener should be invoked, or
    352      * {@code null} to use the current thread's {@link android.os.Looper
    353      * looper}.
    354      *
    355      * @return int A unique capture sequence ID used by
    356      *             {@link CaptureCallback#onCaptureSequenceCompleted}.
    357      *
    358      * @throws CameraAccessException if the camera device is no longer connected or has
    359      *                               encountered a fatal error
    360      * @throws IllegalStateException if this session is no longer active, either because the session
    361      *                               was explicitly closed, a new session has been created
    362      *                               or the camera device has been closed.
    363      * @throws IllegalArgumentException If the request references no Surfaces or references Surfaces
    364      *                                  that are not currently configured as outputs; or the request
    365      *                                  is a reprocess capture request; or the capture targets a
    366      *                                  Surface in the middle of being {@link #prepare prepared}; or
    367      *                                  the handler is null, the listener is not null, and the
    368      *                                  calling thread has no looper; or no requests were passed in.
    369      *
    370      * @see #capture
    371      * @see #captureBurst
    372      * @see #setRepeatingBurst
    373      * @see #stopRepeating
    374      * @see #abortCaptures
    375      */
    376     public abstract int setRepeatingRequest(@NonNull CaptureRequest request,
    377             @Nullable CaptureCallback listener, @Nullable Handler handler)
    378             throws CameraAccessException;
    379 
    380     /**
    381      * <p>Request endlessly repeating capture of a sequence of images by this
    382      * capture session.</p>
    383      *
    384      * <p>With this method, the camera device will continually capture images,
    385      * cycling through the settings in the provided list of
    386      * {@link CaptureRequest CaptureRequests}, at the maximum rate possible.</p>
    387      *
    388      * <p>If a request is submitted through {@link #capture} or
    389      * {@link #captureBurst}, the current repetition of the request list will be
    390      * completed before the higher-priority request is handled. This guarantees
    391      * that the application always receives a complete repeat burst captured in
    392      * minimal time, instead of bursts interleaved with higher-priority
    393      * captures, or incomplete captures.</p>
    394      *
    395      * <p>Repeating burst requests are a simple way for an application to
    396      * maintain a preview or other continuous stream of frames where each
    397      * request is different in a predicatable way, without having to continually
    398      * submit requests through {@link #captureBurst}.</p>
    399      *
    400      * <p>To stop the repeating capture, call {@link #stopRepeating}. Any
    401      * ongoing burst will still be completed, however. Calling
    402      * {@link #abortCaptures} will also clear the request.</p>
    403      *
    404      * <p>Calling this method will replace a previously-set repeating request or
    405      * burst set up by this method or {@link #setRepeatingRequest}, although any
    406      * in-progress burst will be completed before the new repeat burst will be
    407      * used.</p>
    408      *
    409      * <p>This method does not support reprocess capture requests because each reprocess
    410      * {@link CaptureRequest} must be created from the {@link TotalCaptureResult} that matches
    411      * the input image to be reprocessed. This is either the {@link TotalCaptureResult} of capture
    412      * that is sent for reprocessing, or one of the {@link TotalCaptureResult TotalCaptureResults}
    413      * of a set of captures, when data from the whole set is combined by the application into a
    414      * single reprocess input image. The request must be capturing images from the camera. If a
    415      * reprocess capture request is submitted, this method will throw IllegalArgumentException.</p>
    416      *
    417      * @param requests the list of requests to cycle through indefinitely
    418      * @param listener The callback object to notify each time one of the
    419      * requests in the repeating bursts has finished processing. If null, no
    420      * metadata will be produced for this stream of requests, although image
    421      * data will still be produced.
    422      * @param handler the handler on which the listener should be invoked, or
    423      * {@code null} to use the current thread's {@link android.os.Looper
    424      * looper}.
    425      *
    426      * @return int A unique capture sequence ID used by
    427      *             {@link CaptureCallback#onCaptureSequenceCompleted}.
    428      *
    429      * @throws CameraAccessException if the camera device is no longer connected or has
    430      *                               encountered a fatal error
    431      * @throws IllegalStateException if this session is no longer active, either because the session
    432      *                               was explicitly closed, a new session has been created
    433      *                               or the camera device has been closed.
    434      * @throws IllegalArgumentException If the requests reference no Surfaces or reference Surfaces
    435      *                                  not currently configured as outputs; or one of the requests
    436      *                                  is a reprocess capture request; or one of the captures
    437      *                                  targets a Surface in the middle of being
    438      *                                  {@link #prepare prepared}; or the handler is null, the
    439      *                                  listener is not null, and the calling thread has no looper;
    440      *                                  or no requests were passed in.
    441      *
    442      * @see #capture
    443      * @see #captureBurst
    444      * @see #setRepeatingRequest
    445      * @see #stopRepeating
    446      * @see #abortCaptures
    447      */
    448     public abstract int setRepeatingBurst(@NonNull List<CaptureRequest> requests,
    449             @Nullable CaptureCallback listener, @Nullable Handler handler)
    450             throws CameraAccessException;
    451 
    452     /**
    453      * <p>Cancel any ongoing repeating capture set by either
    454      * {@link #setRepeatingRequest setRepeatingRequest} or
    455      * {@link #setRepeatingBurst}. Has no effect on requests submitted through
    456      * {@link #capture capture} or {@link #captureBurst captureBurst}.</p>
    457      *
    458      * <p>Any currently in-flight captures will still complete, as will any burst that is
    459      * mid-capture. To ensure that the device has finished processing all of its capture requests
    460      * and is in ready state, wait for the {@link StateCallback#onReady} callback after
    461      * calling this method.</p>
    462      *
    463      * @throws CameraAccessException if the camera device is no longer connected or has
    464      *                               encountered a fatal error
    465      * @throws IllegalStateException if this session is no longer active, either because the session
    466      *                               was explicitly closed, a new session has been created
    467      *                               or the camera device has been closed.
    468      *
    469      * @see #setRepeatingRequest
    470      * @see #setRepeatingBurst
    471      * @see StateCallback#onIdle
    472      */
    473     public abstract void stopRepeating() throws CameraAccessException;
    474 
    475     /**
    476      * Discard all captures currently pending and in-progress as fast as possible.
    477      *
    478      * <p>The camera device will discard all of its current work as fast as possible. Some in-flight
    479      * captures may complete successfully and call {@link CaptureCallback#onCaptureCompleted}, while
    480      * others will trigger their {@link CaptureCallback#onCaptureFailed} callbacks. If a repeating
    481      * request or a repeating burst is set, it will be cleared.</p>
    482      *
    483      * <p>This method is the fastest way to switch the camera device to a new session with
    484      * {@link CameraDevice#createCaptureSession} or
    485      * {@link CameraDevice#createReprocessableCaptureSession}, at the cost of discarding in-progress
    486      * work. It must be called before the new session is created. Once all pending requests are
    487      * either completed or thrown away, the {@link StateCallback#onReady} callback will be called,
    488      * if the session has not been closed. Otherwise, the {@link StateCallback#onClosed}
    489      * callback will be fired when a new session is created by the camera device.</p>
    490      *
    491      * <p>Cancelling will introduce at least a brief pause in the stream of data from the camera
    492      * device, since once the camera device is emptied, the first new request has to make it through
    493      * the entire camera pipeline before new output buffers are produced.</p>
    494      *
    495      * <p>This means that using {@code abortCaptures()} to simply remove pending requests is not
    496      * recommended; it's best used for quickly switching output configurations, or for cancelling
    497      * long in-progress requests (such as a multi-second capture).</p>
    498      *
    499      * @throws CameraAccessException if the camera device is no longer connected or has
    500      *                               encountered a fatal error
    501      * @throws IllegalStateException if this session is no longer active, either because the session
    502      *                               was explicitly closed, a new session has been created
    503      *                               or the camera device has been closed.
    504      *
    505      * @see #setRepeatingRequest
    506      * @see #setRepeatingBurst
    507      * @see CameraDevice#createCaptureSession
    508      * @see CameraDevice#createReprocessableCaptureSession
    509      */
    510     public abstract void abortCaptures() throws CameraAccessException;
    511 
    512     /**
    513      * Return if the application can submit reprocess capture requests with this camera capture
    514      * session.
    515      *
    516      * @return {@code true} if the application can submit reprocess capture requests with this
    517      *         camera capture session. {@code false} otherwise.
    518      *
    519      * @see CameraDevice#createReprocessableCaptureSession
    520      */
    521     public abstract boolean isReprocessable();
    522 
    523     /**
    524      * Get the input Surface associated with a reprocessable capture session.
    525      *
    526      * <p>Each reprocessable capture session has an input {@link Surface} where the reprocess
    527      * capture requests get the input images from, rather than the camera device. The application
    528      * can create a {@link android.media.ImageWriter ImageWriter} with this input {@link Surface}
    529      * and use it to provide input images for reprocess capture requests. When the reprocessable
    530      * capture session is closed, the input {@link Surface} is abandoned and becomes invalid.</p>
    531      *
    532      * @return The {@link Surface} where reprocessing capture requests get the input images from. If
    533      *         this is not a reprocess capture session, {@code null} will be returned.
    534      *
    535      * @see CameraDevice#createReprocessableCaptureSession
    536      * @see android.media.ImageWriter
    537      * @see android.media.ImageReader
    538      */
    539     @Nullable
    540     public abstract Surface getInputSurface();
    541 
    542     /**
    543      * Close this capture session asynchronously.
    544      *
    545      * <p>Closing a session frees up the target output Surfaces of the session for reuse with either
    546      * a new session, or to other APIs that can draw to Surfaces.</p>
    547      *
    548      * <p>Note that creating a new capture session with {@link CameraDevice#createCaptureSession}
    549      * will close any existing capture session automatically, and call the older session listener's
    550      * {@link StateCallback#onClosed} callback. Using {@link CameraDevice#createCaptureSession}
    551      * directly without closing is the recommended approach for quickly switching to a new session,
    552      * since unchanged target outputs can be reused more efficiently.</p>
    553      *
    554      * <p>Once a session is closed, all methods on it will throw an IllegalStateException, and any
    555      * repeating requests or bursts are stopped (as if {@link #stopRepeating()} was called).
    556      * However, any in-progress capture requests submitted to the session will be completed as
    557      * normal; once all captures have completed and the session has been torn down,
    558      * {@link StateCallback#onClosed} will be called.</p>
    559      *
    560      * <p>Closing a session is idempotent; closing more than once has no effect.</p>
    561      */
    562     @Override
    563     public abstract void close();
    564 
    565     /**
    566      * A callback object for receiving updates about the state of a camera capture session.
    567      *
    568      */
    569     public static abstract class StateCallback {
    570 
    571         /**
    572          * This method is called when the camera device has finished configuring itself, and the
    573          * session can start processing capture requests.
    574          *
    575          * <p>If there are capture requests already queued with the session, they will start
    576          * processing once this callback is invoked, and the session will call {@link #onActive}
    577          * right after this callback is invoked.</p>
    578          *
    579          * <p>If no capture requests have been submitted, then the session will invoke
    580          * {@link #onReady} right after this callback.</p>
    581          *
    582          * <p>If the camera device configuration fails, then {@link #onConfigureFailed} will
    583          * be invoked instead of this callback.</p>
    584          *
    585          * @param session the session returned by {@link CameraDevice#createCaptureSession}
    586          */
    587         public abstract void onConfigured(@NonNull CameraCaptureSession session);
    588 
    589         /**
    590          * This method is called if the session cannot be configured as requested.
    591          *
    592          * <p>This can happen if the set of requested outputs contains unsupported sizes,
    593          * or too many outputs are requested at once.</p>
    594          *
    595          * <p>The session is considered to be closed, and all methods called on it after this
    596          * callback is invoked will throw an IllegalStateException. Any capture requests submitted
    597          * to the session prior to this callback will be discarded and will not produce any
    598          * callbacks on their listeners.</p>
    599          *
    600          * @param session the session returned by {@link CameraDevice#createCaptureSession}
    601          */
    602         public abstract void onConfigureFailed(@NonNull CameraCaptureSession session);
    603 
    604         /**
    605          * This method is called every time the session has no more capture requests to process.
    606          *
    607          * <p>During the creation of a new session, this callback is invoked right after
    608          * {@link #onConfigured} if no capture requests were submitted to the session prior to it
    609          * completing configuration.</p>
    610          *
    611          * <p>Otherwise, this callback will be invoked any time the session finishes processing
    612          * all of its active capture requests, and no repeating request or burst is set up.</p>
    613          *
    614          * @param session the session returned by {@link CameraDevice#createCaptureSession}
    615          *
    616          */
    617         public void onReady(@NonNull CameraCaptureSession session) {
    618             // default empty implementation
    619         }
    620 
    621         /**
    622          * This method is called when the session starts actively processing capture requests.
    623          *
    624          * <p>If capture requests are submitted prior to {@link #onConfigured} being called,
    625          * then the session will start processing those requests immediately after the callback,
    626          * and this method will be immediately called after {@link #onConfigured}.
    627          *
    628          * <p>If the session runs out of capture requests to process and calls {@link #onReady},
    629          * then this callback will be invoked again once new requests are submitted for capture.</p>
    630          *
    631          * @param session the session returned by {@link CameraDevice#createCaptureSession}
    632          */
    633         public void onActive(@NonNull CameraCaptureSession session) {
    634             // default empty implementation
    635         }
    636 
    637         /**
    638          * This method is called when the session is closed.
    639          *
    640          * <p>A session is closed when a new session is created by the parent camera device,
    641          * or when the parent camera device is closed (either by the user closing the device,
    642          * or due to a camera device disconnection or fatal error).</p>
    643          *
    644          * <p>Once a session is closed, all methods on it will throw an IllegalStateException, and
    645          * any repeating requests or bursts are stopped (as if {@link #stopRepeating()} was called).
    646          * However, any in-progress capture requests submitted to the session will be completed
    647          * as normal.</p>
    648          *
    649          * @param session the session returned by {@link CameraDevice#createCaptureSession}
    650          */
    651         public void onClosed(@NonNull CameraCaptureSession session) {
    652             // default empty implementation
    653         }
    654 
    655         /**
    656          * This method is called when the buffer pre-allocation for an output Surface is complete.
    657          *
    658          * <p>Buffer pre-allocation for an output Surface is started by the {@link #prepare} call.
    659          * While allocation is underway, the Surface must not be used as a capture target.
    660          * Once this callback fires, the output Surface provided can again be used as a target for
    661          * a capture request.</p>
    662          *
    663          * <p>In case of a error during pre-allocation (such as running out of suitable memory),
    664          * this callback is still invoked after the error is encountered, though some buffers may
    665          * not have been successfully pre-allocated.</p>
    666          *
    667          * @param session the session returned by {@link CameraDevice#createCaptureSession}
    668          * @param surface the Surface that was used with the {@link #prepare} call.
    669          */
    670         public void onSurfacePrepared(@NonNull CameraCaptureSession session,
    671                 @NonNull Surface surface) {
    672             // default empty implementation
    673         }
    674     }
    675 
    676     /**
    677      * Temporary for migrating to Callback naming
    678      * @hide
    679      */
    680     public static abstract class StateListener extends StateCallback {
    681     }
    682 
    683     /**
    684      * <p>A callback object for tracking the progress of a {@link CaptureRequest} submitted to the
    685      * camera device.</p>
    686      *
    687      * <p>This callback is invoked when a request triggers a capture to start,
    688      * and when the capture is complete. In case on an error capturing an image,
    689      * the error method is triggered instead of the completion method.</p>
    690      *
    691      * @see #capture
    692      * @see #captureBurst
    693      * @see #setRepeatingRequest
    694      * @see #setRepeatingBurst
    695      */
    696     public static abstract class CaptureCallback {
    697 
    698         /**
    699          * This constant is used to indicate that no images were captured for
    700          * the request.
    701          *
    702          * @hide
    703          */
    704         public static final int NO_FRAMES_CAPTURED = -1;
    705 
    706         /**
    707          * This method is called when the camera device has started capturing
    708          * the output image for the request, at the beginning of image exposure, or
    709          * when the camera device has started processing an input image for a reprocess
    710          * request.
    711          *
    712          * <p>For a regular capture request, this callback is invoked right as
    713          * the capture of a frame begins, so it is the most appropriate time
    714          * for playing a shutter sound, or triggering UI indicators of capture.</p>
    715          *
    716          * <p>The request that is being used for this capture is provided, along
    717          * with the actual timestamp for the start of exposure. For a reprocess
    718          * request, this timestamp will be the input image's start of exposure
    719          * which matches {@link CaptureResult#SENSOR_TIMESTAMP the result timestamp field}
    720          * of the {@link TotalCaptureResult} that was used to
    721          * {@link CameraDevice#createReprocessCaptureRequest create the reprocess request}.
    722          * This timestamp matches the timestamps that will be
    723          * included in {@link CaptureResult#SENSOR_TIMESTAMP the result timestamp field},
    724          * and in the buffers sent to each output Surface. These buffer
    725          * timestamps are accessible through, for example,
    726          * {@link android.media.Image#getTimestamp() Image.getTimestamp()} or
    727          * {@link android.graphics.SurfaceTexture#getTimestamp()}.
    728          * The frame number included is equal to the frame number that will be included in
    729          * {@link CaptureResult#getFrameNumber}.</p>
    730          *
    731          * <p>For the simplest way to play a shutter sound camera shutter or a
    732          * video recording start/stop sound, see the
    733          * {@link android.media.MediaActionSound} class.</p>
    734          *
    735          * <p>The default implementation of this method does nothing.</p>
    736          *
    737          * @param session the session returned by {@link CameraDevice#createCaptureSession}
    738          * @param request the request for the capture that just begun
    739          * @param timestamp the timestamp at start of capture for a regular request, or
    740          *                  the timestamp at the input image's start of capture for a
    741          *                  reprocess request, in nanoseconds.
    742          * @param frameNumber the frame number for this capture
    743          *
    744          * @see android.media.MediaActionSound
    745          */
    746         public void onCaptureStarted(@NonNull CameraCaptureSession session,
    747                 @NonNull CaptureRequest request, long timestamp, long frameNumber) {
    748             // Temporary trampoline for API change transition
    749             onCaptureStarted(session, request, timestamp);
    750         }
    751 
    752         /**
    753          * Temporary for API change transition
    754          * @hide
    755          */
    756         public void onCaptureStarted(CameraCaptureSession session,
    757                 CaptureRequest request, long timestamp) {
    758             // default empty implementation
    759         }
    760 
    761         /**
    762          * This method is called when some results from an image capture are
    763          * available.
    764          *
    765          * <p>The result provided here will contain some subset of the fields of
    766          * a full result. Multiple onCapturePartial calls may happen per
    767          * capture; a given result field will only be present in one partial
    768          * capture at most. The final onCaptureCompleted call will always
    769          * contain all the fields, whether onCapturePartial was called or
    770          * not.</p>
    771          *
    772          * <p>The default implementation of this method does nothing.</p>
    773          *
    774          * @param session the session returned by {@link CameraDevice#createCaptureSession}
    775          * @param request The request that was given to the CameraDevice
    776          * @param result The partial output metadata from the capture, which
    777          * includes a subset of the CaptureResult fields.
    778          *
    779          * @see #capture
    780          * @see #captureBurst
    781          * @see #setRepeatingRequest
    782          * @see #setRepeatingBurst
    783          *
    784          * @hide
    785          */
    786         public void onCapturePartial(CameraCaptureSession session,
    787                 CaptureRequest request, CaptureResult result) {
    788             // default empty implementation
    789         }
    790 
    791         /**
    792          * This method is called when an image capture makes partial forward progress; some
    793          * (but not all) results from an image capture are available.
    794          *
    795          * <p>The result provided here will contain some subset of the fields of
    796          * a full result. Multiple {@link #onCaptureProgressed} calls may happen per
    797          * capture; a given result field will only be present in one partial
    798          * capture at most. The final {@link #onCaptureCompleted} call will always
    799          * contain all the fields (in particular, the union of all the fields of all
    800          * the partial results composing the total result).</p>
    801          *
    802          * <p>For each request, some result data might be available earlier than others. The typical
    803          * delay between each partial result (per request) is a single frame interval.
    804          * For performance-oriented use-cases, applications should query the metadata they need
    805          * to make forward progress from the partial results and avoid waiting for the completed
    806          * result.</p>
    807          *
    808          * <p>Each request will generate at least {@code 1} partial results, and at most
    809          * {@link CameraCharacteristics#REQUEST_PARTIAL_RESULT_COUNT} partial results.</p>
    810          *
    811          * <p>Depending on the request settings, the number of partial results per request
    812          * will vary, although typically the partial count could be the same as long as the
    813          * camera device subsystems enabled stay the same.</p>
    814          *
    815          * <p>The default implementation of this method does nothing.</p>
    816          *
    817          * @param session the session returned by {@link CameraDevice#createCaptureSession}
    818          * @param request The request that was given to the CameraDevice
    819          * @param partialResult The partial output metadata from the capture, which
    820          * includes a subset of the {@link TotalCaptureResult} fields.
    821          *
    822          * @see #capture
    823          * @see #captureBurst
    824          * @see #setRepeatingRequest
    825          * @see #setRepeatingBurst
    826          */
    827         public void onCaptureProgressed(@NonNull CameraCaptureSession session,
    828                 @NonNull CaptureRequest request, @NonNull CaptureResult partialResult) {
    829             // default empty implementation
    830         }
    831 
    832         /**
    833          * This method is called when an image capture has fully completed and all the
    834          * result metadata is available.
    835          *
    836          * <p>This callback will always fire after the last {@link #onCaptureProgressed};
    837          * in other words, no more partial results will be delivered once the completed result
    838          * is available.</p>
    839          *
    840          * <p>For performance-intensive use-cases where latency is a factor, consider
    841          * using {@link #onCaptureProgressed} instead.</p>
    842          *
    843          * <p>The default implementation of this method does nothing.</p>
    844          *
    845          * @param session the session returned by {@link CameraDevice#createCaptureSession}
    846          * @param request The request that was given to the CameraDevice
    847          * @param result The total output metadata from the capture, including the
    848          * final capture parameters and the state of the camera system during
    849          * capture.
    850          *
    851          * @see #capture
    852          * @see #captureBurst
    853          * @see #setRepeatingRequest
    854          * @see #setRepeatingBurst
    855          */
    856         public void onCaptureCompleted(@NonNull CameraCaptureSession session,
    857                 @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
    858             // default empty implementation
    859         }
    860 
    861         /**
    862          * This method is called instead of {@link #onCaptureCompleted} when the
    863          * camera device failed to produce a {@link CaptureResult} for the
    864          * request.
    865          *
    866          * <p>Other requests are unaffected, and some or all image buffers from
    867          * the capture may have been pushed to their respective output
    868          * streams.</p>
    869          *
    870          * <p>The default implementation of this method does nothing.</p>
    871          *
    872          * @param session
    873          *            The session returned by {@link CameraDevice#createCaptureSession}
    874          * @param request
    875          *            The request that was given to the CameraDevice
    876          * @param failure
    877          *            The output failure from the capture, including the failure reason
    878          *            and the frame number.
    879          *
    880          * @see #capture
    881          * @see #captureBurst
    882          * @see #setRepeatingRequest
    883          * @see #setRepeatingBurst
    884          */
    885         public void onCaptureFailed(@NonNull CameraCaptureSession session,
    886                 @NonNull CaptureRequest request, @NonNull CaptureFailure failure) {
    887             // default empty implementation
    888         }
    889 
    890         /**
    891          * This method is called independently of the others in CaptureCallback,
    892          * when a capture sequence finishes and all {@link CaptureResult}
    893          * or {@link CaptureFailure} for it have been returned via this listener.
    894          *
    895          * <p>In total, there will be at least one result/failure returned by this listener
    896          * before this callback is invoked. If the capture sequence is aborted before any
    897          * requests have been processed, {@link #onCaptureSequenceAborted} is invoked instead.</p>
    898          *
    899          * <p>The default implementation does nothing.</p>
    900          *
    901          * @param session
    902          *            The session returned by {@link CameraDevice#createCaptureSession}
    903          * @param sequenceId
    904          *            A sequence ID returned by the {@link #capture} family of functions.
    905          * @param frameNumber
    906          *            The last frame number (returned by {@link CaptureResult#getFrameNumber}
    907          *            or {@link CaptureFailure#getFrameNumber}) in the capture sequence.
    908          *
    909          * @see CaptureResult#getFrameNumber()
    910          * @see CaptureFailure#getFrameNumber()
    911          * @see CaptureResult#getSequenceId()
    912          * @see CaptureFailure#getSequenceId()
    913          * @see #onCaptureSequenceAborted
    914          */
    915         public void onCaptureSequenceCompleted(@NonNull CameraCaptureSession session,
    916                 int sequenceId, long frameNumber) {
    917             // default empty implementation
    918         }
    919 
    920         /**
    921          * This method is called independently of the others in CaptureCallback,
    922          * when a capture sequence aborts before any {@link CaptureResult}
    923          * or {@link CaptureFailure} for it have been returned via this listener.
    924          *
    925          * <p>Due to the asynchronous nature of the camera device, not all submitted captures
    926          * are immediately processed. It is possible to clear out the pending requests
    927          * by a variety of operations such as {@link CameraCaptureSession#stopRepeating} or
    928          * {@link CameraCaptureSession#abortCaptures}. When such an event happens,
    929          * {@link #onCaptureSequenceCompleted} will not be called.</p>
    930          *
    931          * <p>The default implementation does nothing.</p>
    932          *
    933          * @param session
    934          *            The session returned by {@link CameraDevice#createCaptureSession}
    935          * @param sequenceId
    936          *            A sequence ID returned by the {@link #capture} family of functions.
    937          *
    938          * @see CaptureResult#getFrameNumber()
    939          * @see CaptureFailure#getFrameNumber()
    940          * @see CaptureResult#getSequenceId()
    941          * @see CaptureFailure#getSequenceId()
    942          * @see #onCaptureSequenceCompleted
    943          */
    944         public void onCaptureSequenceAborted(@NonNull CameraCaptureSession session,
    945                 int sequenceId) {
    946             // default empty implementation
    947         }
    948     }
    949 
    950     /**
    951      * Temporary for migrating to Callback naming
    952      * @hide
    953      */
    954     public static abstract class CaptureListener extends CaptureCallback {
    955     }
    956 
    957 }
    958