1 /*
2  * Copyright (C) 2020 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 
21 import java.util.concurrent.Executor;
22 
23 /**
24  * A camera capture session that enables access to device-specific camera extensions, which
25  * often use multi-frame bursts and sophisticated post-process algorithms for image capture.
26  *
27  * <p>The capture session will be returned after a successful call to
28  * {@link CameraDevice#createExtensionSession} as part of the argument
29  * in the registered state callback {@link StateCallback#onConfigured}
30  * method. </p>
31  *
32  * <p>Note that CameraExtensionSession is currently limited to a maximum of two output
33  * surfaces for continuous repeating and multi-frame processing respectively. Some
34  * features such as capture settings will not be supported as the device-specific
35  * Extension is allowed to override all capture parameters.</p>
36  *
37  * <p>Information about support for specific device-specific extensions can be queried
38  * from {@link CameraExtensionCharacteristics}. </p>
39  */
40 public abstract class CameraExtensionSession implements AutoCloseable {
41      /** @hide */
CameraExtensionSession()42     public CameraExtensionSession () {}
43 
44     /**
45      * A callback object for tracking the progress of a
46      * {@link CaptureRequest} submitted to the camera device.
47      *
48      * <p>This callback is invoked when a request triggers a capture to start,
49      * and when the device-specific Extension post processing begins. In case of an
50      * error capturing an image, the error method is triggered instead of
51      * the completion method.</p>
52      *
53      * @see #capture
54      * @see #setRepeatingRequest
55      */
56     public static abstract class ExtensionCaptureCallback {
57 
58         /**
59          * This method is called when the camera device has started
60          * capturing the initial input image of the device-specific extension
61          * post-process request.
62          *
63          * <p>This callback is invoked right as the capture of a frame begins,
64          * so it is the most appropriate time for playing a shutter sound,
65          * or triggering UI indicators of capture.</p>
66          *
67          * <p>The request that is being used for this capture is provided,
68          * along with the actual timestamp for the start of exposure.</p>
69          *
70          * <p>The default implementation of this method does nothing.</p>
71          *
72          * @param session   the session received during
73          *                  {@link StateCallback#onConfigured(CameraExtensionSession)}
74          * @param request   the request for the capture that just begun
75          * @param timestamp the timestamp at start of capture for repeating
76          *                  request or the timestamp at start of capture of the
77          *                  first frame in a multi-frame capture.
78          */
onCaptureStarted(@onNull CameraExtensionSession session, @NonNull CaptureRequest request, long timestamp)79         public void onCaptureStarted(@NonNull CameraExtensionSession session,
80                 @NonNull CaptureRequest request, long timestamp) {
81             // default empty implementation
82         }
83 
84         /**
85          * This method is called when an image (or images in case of multi-frame
86          * capture) is captured and device-specific extension
87          * processing is triggered.
88          *
89          * <p>Each request will generate at most {@code 1}
90          * {@link #onCaptureProcessStarted}.</p>
91          *
92          * <p>The default implementation of this method does nothing.</p>
93          *
94          * @param session the session received during
95          *                {@link StateCallback#onConfigured(CameraExtensionSession)}
96          * @param request The request that was given to the CameraExtensionSession
97          *
98          * @see #capture
99          * @see #setRepeatingRequest
100          */
onCaptureProcessStarted(@onNull CameraExtensionSession session, @NonNull CaptureRequest request)101         public void onCaptureProcessStarted(@NonNull CameraExtensionSession session,
102                 @NonNull CaptureRequest request) {
103             // default empty implementation
104         }
105 
106         /**
107          * This method is called instead of
108          * {@link #onCaptureProcessStarted} when the camera device failed
109          * to produce the required input for the device-specific extension. The
110          * cause could be a failed camera capture request, a failed
111          * capture result or dropped camera frame.
112          *
113          * <p>Other requests are unaffected, and some or all image buffers
114          * from the capture may have been pushed to their respective output
115          * streams.</p>
116          *
117          * <p>The default implementation of this method does nothing.</p>
118          *
119          * @param session the session received during
120          *                {@link StateCallback#onConfigured(CameraExtensionSession)}
121          * @param request The request that was given to the CameraDevice
122          *
123          * @see #capture
124          * @see #setRepeatingRequest
125          */
onCaptureFailed(@onNull CameraExtensionSession session, @NonNull CaptureRequest request)126         public void onCaptureFailed(@NonNull CameraExtensionSession session,
127                 @NonNull CaptureRequest request) {
128             // default empty implementation
129         }
130 
131         /**
132          * This method is called independently of the others in
133          * ExtensionCaptureCallback, when a capture sequence finishes.
134          *
135          * <p>In total, there will be at least one
136          * {@link #onCaptureProcessStarted}/{@link #onCaptureFailed}
137          * invocation before this callback is triggered. If the capture
138          * sequence is aborted before any requests have begun processing,
139          * {@link #onCaptureSequenceAborted} is invoked instead.</p>
140          *
141          * <p>The default implementation does nothing.</p>
142          *
143          * @param session    the session received during
144          *                   {@link StateCallback#onConfigured(CameraExtensionSession)}
145          * @param sequenceId A sequence ID returned by the {@link #capture}
146          *                   family of functions.
147          * @see #onCaptureSequenceAborted
148          */
onCaptureSequenceCompleted(@onNull CameraExtensionSession session, int sequenceId)149         public void onCaptureSequenceCompleted(@NonNull CameraExtensionSession session,
150                 int sequenceId) {
151             // default empty implementation
152         }
153 
154         /**
155          * This method is called when a capture sequence aborts.
156          *
157          * <p>Due to the asynchronous nature of the camera device, not all
158          * submitted captures are immediately processed. It is possible to
159          * clear out the pending requests by a variety of operations such
160          * as {@link CameraExtensionSession#stopRepeating}. When such an event
161          * happens, {@link #onCaptureProcessStarted} will not be called.</p>
162          *
163          * <p>The default implementation does nothing.</p>
164          *
165          * @param session    the session received during
166          *                   {@link StateCallback#onConfigured(CameraExtensionSession)}
167          * @param sequenceId A sequence ID returned by the {@link #capture}
168          *                   family of functions.
169          * @see #onCaptureProcessStarted
170          */
onCaptureSequenceAborted(@onNull CameraExtensionSession session, int sequenceId)171         public void onCaptureSequenceAborted(@NonNull CameraExtensionSession session,
172                 int sequenceId) {
173             // default empty implementation
174         }
175     }
176 
177     /**
178      * A callback object for receiving updates about the state of a camera extension session.
179      *
180      */
181     public static abstract class StateCallback {
182 
183         /**
184          * This method is called when the camera device has finished configuring itself, and the
185          * session can start processing capture requests.
186          *
187          * <p>If the camera device configuration fails, then {@link #onConfigureFailed} will
188          * be invoked instead of this callback.</p>
189          *
190          * @param session A valid extension session
191          */
onConfigured(@onNull CameraExtensionSession session)192         public abstract void onConfigured(@NonNull CameraExtensionSession session);
193 
194         /**
195          * This method is called if the session cannot be configured as requested.
196          *
197          * <p>This can happen if the set of requested outputs contains unsupported sizes,
198          * too many outputs are requested at once or when trying to initialize multiple
199          * concurrent extension sessions from two (or more) separate camera devices
200          * or the camera device encounters an unrecoverable error during configuration.</p>
201          *
202          * <p>The session is considered to be closed, and all methods called on it after this
203          * callback is invoked will throw an IllegalStateException.</p>
204          *
205          * @param session the session instance that failed to configure
206          */
onConfigureFailed(@onNull CameraExtensionSession session)207         public abstract void onConfigureFailed(@NonNull CameraExtensionSession session);
208 
209         /**
210          * This method is called when the session is closed.
211          *
212          * <p>A session is closed when a new session is created by the parent camera device,
213          * or when the parent camera device is closed (either by the user closing the device,
214          * or due to a camera device disconnection or fatal error).</p>
215          *
216          * <p>Once a session is closed, all methods on it will throw an IllegalStateException, and
217          * any repeating requests are stopped (as if {@link #stopRepeating()} was called).
218          * However, any in-progress capture requests submitted to the session will be completed
219          * as normal.</p>
220          *
221          * @param session the session received during
222          *                {@link StateCallback#onConfigured(CameraExtensionSession)}
223          */
onClosed(@onNull CameraExtensionSession session)224         public void onClosed(@NonNull CameraExtensionSession session) {
225             // default empty implementation
226         }
227     }
228 
229     /**
230      * Get the camera device that this session is created for.
231      */
232     @NonNull
getDevice()233     public android.hardware.camera2.CameraDevice getDevice() {
234         throw new UnsupportedOperationException("Subclasses must override this method");
235     }
236 
237     /**
238      * Submit a request for device-specific processing using input
239      * from the camera device, to produce a single high-quality output result.
240      *
241      * <p>Note that single capture requests currently do not support
242      * client parameters except for {@link CaptureRequest#JPEG_ORIENTATION orientation} and
243      * {@link CaptureRequest#JPEG_QUALITY quality} in case of ImageFormat.JPEG output target.
244      * The rest of the settings included in the request will be entirely overridden by
245      * the device-specific extension. </p>
246      *
247      * <p>The {@link CaptureRequest.Builder#addTarget} supports only one
248      * ImageFormat.YUV_420_888 or ImageFormat.JPEG target surface. {@link CaptureRequest}
249      * arguments that include further targets will cause
250      * IllegalArgumentException to be thrown. </p>
251      *
252      * <p>Each request will produce one new frame for one target Surface, set
253      * with the CaptureRequest builder's
254      * {@link CaptureRequest.Builder#addTarget} method.</p>
255      *
256      * <p>Multiple requests can be in progress at once. Requests are
257      * processed in first-in, first-out order.</p>
258      *
259      * <p>Requests submitted through this method have higher priority than
260      * those submitted through {@link #setRepeatingRequest}, and will be
261      * processed as soon as the current repeat processing completes.</p>
262      *
263      * @param request the settings for this capture
264      * @param executor the executor which will be used for invoking the
265      *                 listener.
266      * @param listener The callback object to notify once this request has
267      *                 been processed.
268      * @return int A unique capture sequence ID used by
269      * {@link ExtensionCaptureCallback#onCaptureSequenceCompleted}.
270      * @throws CameraAccessException    if the camera device is no longer
271      *                                  connected or has encountered a fatal error
272      * @throws IllegalStateException    if this session is no longer active,
273      *                                  either because the session was explicitly closed, a new
274      *                                  session has been created or the camera device has been
275      *                                  closed.
276      * @throws IllegalArgumentException if the request targets no Surfaces
277      *                                  or Surfaces that are not configured as outputs for this
278      *                                  session; or the request targets a set of Surfaces that
279      *                                  cannot be submitted simultaneously.
280      */
capture(@onNull CaptureRequest request, @NonNull Executor executor, @NonNull ExtensionCaptureCallback listener)281     public int capture(@NonNull CaptureRequest request,
282             @NonNull Executor executor,
283             @NonNull ExtensionCaptureCallback listener) throws CameraAccessException {
284         throw new UnsupportedOperationException("Subclasses must override this method");
285     }
286 
287     /**
288      * Request endlessly repeating device-specific extension processing of
289      * camera images.
290      *
291      * <p>With this method, the camera device will continually capture images
292      * and process them using the device-specific extension at the maximum
293      * rate possible.</p>
294      *
295      * <p>Note that repeating capture requests currently do not support
296      * client parameters. Settings included in the request will
297      * be completely overridden by the device-specific extension.</p>
298      *
299      * <p>The {@link CaptureRequest.Builder#addTarget} supports only one
300      * target surface. {@link CaptureRequest} arguments that include further
301      * targets will cause IllegalArgumentException to be thrown.</p>
302      *
303      * <p>Repeating requests are a simple way for an application to maintain a
304      * preview or other continuous stream of frames.</p>
305      *
306      * <p>Repeat requests have lower priority than those submitted
307      * through {@link #capture}, so if  {@link #capture} is called when a
308      * repeating request is active, the capture request will be processed
309      * before any further repeating requests are processed.</p>
310      *
311      * <p>To stop the repeating capture, call {@link #stopRepeating}.</p>
312      *
313      * <p>Calling this method will replace any earlier repeating request.</p>
314      *
315      * @param request the request to repeat indefinitely
316      * @param executor the executor which will be used for invoking the
317      *                 listener.
318      * @param listener The callback object to notify every time the
319      *                 request finishes processing.
320      * @return int A unique capture sequence ID used by
321      * {@link ExtensionCaptureCallback#onCaptureSequenceCompleted}.
322      * @throws CameraAccessException    if the camera device is no longer
323      *                                  connected or has encountered a fatal error
324      * @throws IllegalStateException    if this session is no longer active,
325      *                                  either because the session was explicitly closed, a new
326      *                                  session has been created or the camera device has been
327      *                                  closed.
328      * @throws IllegalArgumentException If the request references no
329      *                                  Surfaces or references Surfaces that are not currently
330      *                                  configured as outputs.
331      * @see #capture
332      */
setRepeatingRequest(@onNull CaptureRequest request, @NonNull Executor executor, @NonNull ExtensionCaptureCallback listener)333     public int setRepeatingRequest(@NonNull CaptureRequest request,
334             @NonNull Executor executor,
335             @NonNull ExtensionCaptureCallback listener) throws CameraAccessException {
336         throw new UnsupportedOperationException("Subclasses must override this method");
337     }
338 
339     /**
340      * Cancel any ongoing repeating capture set by
341      * {@link #setRepeatingRequest setRepeatingRequest}. Has no effect on
342      * requests submitted through {@link #capture capture}.
343      *
344      * <p>Any currently in-flight captures will still complete.</p>
345      *
346      * @throws CameraAccessException if the camera device is no longer
347      *                               connected or has  encountered a fatal error
348      * @throws IllegalStateException if this session is no longer active,
349      *                               either because the session was explicitly closed, a new
350      *                               session has been created or the camera device has been closed.
351      * @see #setRepeatingRequest
352      */
stopRepeating()353     public void stopRepeating() throws CameraAccessException {
354         throw new UnsupportedOperationException("Subclasses must override this method");
355     }
356 
357     /**
358      * Close this capture session asynchronously.
359      *
360      * <p>Closing a session frees up the target output Surfaces of the session
361      * for reuse with either a new session, or to other APIs that can draw
362      * to Surfaces.</p>
363      *
364      * <p>Note that creating a new capture session with
365      * {@link android.hardware.camera2.CameraDevice#createCaptureSession} or
366      * {@link android.hardware.camera2.CameraDevice#createExtensionSession}
367      * will close any existing capture session automatically, and call the
368      * older session listener's {@link StateCallback#onClosed} callback.
369      * Using
370      * {@link android.hardware.camera2.CameraDevice#createCaptureSession} or
371      * {@link android.hardware.camera2.CameraDevice#createExtensionSession}
372      * directly without closing is the recommended approach for quickly
373      * switching to a new session, since unchanged target outputs can be
374      * reused more efficiently.</p>
375      *
376      * <p>Once a session is closed, all methods on it will throw an
377      * IllegalStateException, and any repeating requests are
378      * stopped (as if {@link #stopRepeating()} was called).</p>
379      *
380      * <p>Closing a session is idempotent; closing more than once has no
381      * effect.</p>
382      */
close()383     public void close() throws CameraAccessException {
384         throw new UnsupportedOperationException("Subclasses must override this method");
385     }
386 }
387