1 /*
2  * Copyright (C) 2017 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 
18 package android.hardware.camera2.params;
19 
20 import android.annotation.CallbackExecutor;
21 import android.annotation.NonNull;
22 import android.annotation.Nullable;
23 import android.annotation.IntDef;
24 import android.hardware.camera2.CameraCaptureSession;
25 import android.hardware.camera2.CameraCharacteristics;
26 import android.hardware.camera2.CameraDevice;
27 import android.hardware.camera2.CaptureRequest;
28 import android.hardware.camera2.params.InputConfiguration;
29 import android.hardware.camera2.params.OutputConfiguration;
30 
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.ArrayList;
34 import java.util.concurrent.Executor;
35 import java.lang.annotation.Retention;
36 import java.lang.annotation.RetentionPolicy;
37 
38 import static com.android.internal.util.Preconditions.*;
39 
40 /**
41  * A helper class that aggregates all supported arguments for capture session initialization.
42  */
43 public final class SessionConfiguration {
44     /**
45      * A regular session type containing instances of {@link OutputConfiguration} running
46      * at regular non high speed FPS ranges and optionally {@link InputConfiguration} for
47      * reprocessable sessions.
48      *
49      * @see CameraDevice#createCaptureSession
50      * @see CameraDevice#createReprocessableCaptureSession
51      */
52     public static final int SESSION_REGULAR = CameraDevice.SESSION_OPERATION_MODE_NORMAL;
53 
54     /**
55      * A high speed session type that can only contain instances of {@link OutputConfiguration}.
56      * The outputs can run using high speed FPS ranges. Calls to {@link #setInputConfiguration}
57      * are not supported.
58      *
59      * @see CameraDevice#createConstrainedHighSpeedCaptureSession
60      */
61     public static final int SESSION_HIGH_SPEED =
62         CameraDevice.SESSION_OPERATION_MODE_CONSTRAINED_HIGH_SPEED;
63 
64     /**
65      * First vendor-specific session mode
66      * @hide
67      */
68     public static final int SESSION_VENDOR_START =
69         CameraDevice.SESSION_OPERATION_MODE_VENDOR_START;
70 
71      /** @hide */
72     @Retention(RetentionPolicy.SOURCE)
73     @IntDef(prefix = {"SESSION_"}, value =
74             {SESSION_REGULAR,
75              SESSION_HIGH_SPEED })
76     public @interface SessionMode {};
77 
78     // Camera capture session related parameters.
79     private List<OutputConfiguration> mOutputConfigurations;
80     private CameraCaptureSession.StateCallback mStateCallback;
81     private int mSessionType;
82     private Executor mExecutor = null;
83     private InputConfiguration mInputConfig = null;
84     private CaptureRequest mSessionParameters = null;
85 
86     /**
87      * Create a new {@link SessionConfiguration}.
88      *
89      * @param sessionType The session type.
90      * @param outputs A list of output configurations for the capture session.
91      * @param executor The executor which should be used to invoke the callback. In general it is
92      *                 recommended that camera operations are not done on the main (UI) thread.
93      * @param cb A state callback interface implementation.
94      *
95      * @see #SESSION_REGULAR
96      * @see #SESSION_HIGH_SPEED
97      * @see CameraDevice#createCaptureSession(List, CameraCaptureSession.StateCallback, Handler)
98      * @see CameraDevice#createCaptureSessionByOutputConfigurations
99      * @see CameraDevice#createReprocessableCaptureSession
100      * @see CameraDevice#createConstrainedHighSpeedCaptureSession
101      */
SessionConfiguration(@essionMode int sessionType, @NonNull List<OutputConfiguration> outputs, @NonNull @CallbackExecutor Executor executor, @NonNull CameraCaptureSession.StateCallback cb)102     public SessionConfiguration(@SessionMode int sessionType,
103             @NonNull List<OutputConfiguration> outputs,
104             @NonNull @CallbackExecutor Executor executor,
105             @NonNull CameraCaptureSession.StateCallback cb) {
106         mSessionType = sessionType;
107         mOutputConfigurations = Collections.unmodifiableList(new ArrayList<>(outputs));
108         mStateCallback = cb;
109         mExecutor = executor;
110     }
111 
112     /**
113      * Retrieve the type of the capture session.
114      *
115      * @return The capture session type.
116      */
getSessionType()117     public @SessionMode int getSessionType() {
118         return mSessionType;
119     }
120 
121     /**
122      * Retrieve the {@link OutputConfiguration} list for the capture session.
123      *
124      * @return A list of output configurations for the capture session.
125      */
getOutputConfigurations()126     public List<OutputConfiguration> getOutputConfigurations() {
127         return mOutputConfigurations;
128     }
129 
130     /**
131      * Retrieve the {@link CameraCaptureSession.StateCallback} for the capture session.
132      *
133      * @return A state callback interface implementation.
134      */
getStateCallback()135     public CameraCaptureSession.StateCallback getStateCallback() {
136         return mStateCallback;
137     }
138 
139     /**
140      * Retrieve the {@link java.util.concurrent.Executor} for the capture session.
141      *
142      * @return The Executor on which the callback will be invoked.
143      */
getExecutor()144     public Executor getExecutor() {
145         return mExecutor;
146     }
147 
148     /**
149      * Sets the {@link InputConfiguration} for a reprocessable session. Input configuration are not
150      * supported for {@link #SESSION_HIGH_SPEED}.
151      *
152      * @param input Input configuration.
153      * @throws UnsupportedOperationException In case it is called for {@link #SESSION_HIGH_SPEED}
154      *                                       type session configuration.
155      */
setInputConfiguration(@onNull InputConfiguration input)156     public void setInputConfiguration(@NonNull InputConfiguration input) {
157         if (mSessionType != SESSION_HIGH_SPEED) {
158             mInputConfig = input;
159         } else {
160             throw new UnsupportedOperationException("Method not supported for high speed session" +
161                     " types");
162         }
163     }
164 
165     /**
166      * Retrieve the {@link InputConfiguration}.
167      *
168      * @return The capture session input configuration.
169      */
getInputConfiguration()170     public InputConfiguration getInputConfiguration() {
171         return mInputConfig;
172     }
173 
174     /**
175      * Sets the session wide camera parameters (see {@link CaptureRequest}). This argument can
176      * be set for every supported session type and will be passed to the camera device as part
177      * of the capture session initialization. Session parameters are a subset of the available
178      * capture request parameters (see {@link CameraCharacteristics#getAvailableSessionKeys})
179      * and their application can introduce internal camera delays. To improve camera performance
180      * it is suggested to change them sparingly within the lifetime of the capture session and
181      * to pass their initial values as part of this method.
182      *
183      * @param params A capture request that includes the initial values for any available
184      *               session wide capture keys. Tags (see {@link CaptureRequest.Builder#setTag}) and
185      *               output targets (see {@link CaptureRequest.Builder#addTarget}) are ignored if
186      *               set. Parameter values not part of
187      *               {@link CameraCharacteristics#getAvailableSessionKeys} will also be ignored. It
188      *               is recommended to build the session parameters using the same template type as
189      *               the initial capture request, so that the session and initial request parameters
190      *               match as much as possible.
191      */
setSessionParameters(CaptureRequest params)192     public void setSessionParameters(CaptureRequest params) {
193         mSessionParameters = params;
194     }
195 
196     /**
197      * Retrieve the session wide camera parameters (see {@link CaptureRequest}).
198      *
199      * @return A capture request that includes the initial values for any available
200      *         session wide capture keys.
201      */
getSessionParameters()202     public CaptureRequest getSessionParameters() {
203         return mSessionParameters;
204     }
205 }
206