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 com.android.camera.one.v2.camera2proxy;
18 
19 import android.hardware.camera2.CameraAccessException;
20 import android.hardware.camera2.CameraCaptureSession;
21 import android.hardware.camera2.CaptureFailure;
22 import android.hardware.camera2.CaptureRequest;
23 import android.hardware.camera2.CaptureResult;
24 import android.hardware.camera2.TotalCaptureResult;
25 import android.os.Handler;
26 
27 import java.util.List;
28 
29 /**
30  * A CameraCaptureSessionProxy backed by an
31  * {@link android.hardware.camera2.CameraCaptureSession}.
32  */
33 public class AndroidCameraCaptureSessionProxy implements CameraCaptureSessionProxy {
34     private class AndroidCaptureCallback extends CameraCaptureSession.CaptureCallback {
35         private final CaptureCallback mCallback;
36 
AndroidCaptureCallback(CaptureCallback callback)37         private AndroidCaptureCallback(CaptureCallback callback) {
38             mCallback = callback;
39         }
40 
41         @Override
onCaptureStarted(CameraCaptureSession session, CaptureRequest request, long timestamp, long frameNumber)42         public void onCaptureStarted(CameraCaptureSession session, CaptureRequest request,
43                 long timestamp, long frameNumber) {
44             mCallback.onCaptureStarted(AndroidCameraCaptureSessionProxy.this, request, timestamp,
45                     frameNumber);
46         }
47 
48         @Override
onCaptureProgressed(CameraCaptureSession session, CaptureRequest request, CaptureResult partialResult)49         public void onCaptureProgressed(CameraCaptureSession session, CaptureRequest request,
50                 CaptureResult partialResult) {
51             mCallback.onCaptureProgressed(AndroidCameraCaptureSessionProxy.this, request,
52                     partialResult);
53         }
54 
55         @Override
onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result)56         public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request,
57                 TotalCaptureResult result) {
58             mCallback.onCaptureCompleted(AndroidCameraCaptureSessionProxy.this, request, result);
59         }
60 
61         @Override
onCaptureFailed(CameraCaptureSession session, CaptureRequest request, CaptureFailure failure)62         public void onCaptureFailed(CameraCaptureSession session, CaptureRequest request,
63                 CaptureFailure failure) {
64             mCallback.onCaptureFailed(AndroidCameraCaptureSessionProxy.this, request, failure);
65         }
66 
67         @Override
onCaptureSequenceCompleted(CameraCaptureSession session, int sequenceId, long frameNumber)68         public void onCaptureSequenceCompleted(CameraCaptureSession session, int sequenceId,
69                 long frameNumber) {
70             mCallback.onCaptureSequenceCompleted(AndroidCameraCaptureSessionProxy.this,
71                     sequenceId, frameNumber);
72         }
73 
74         @Override
onCaptureSequenceAborted(CameraCaptureSession session, int sequenceId)75         public void onCaptureSequenceAborted(CameraCaptureSession session, int sequenceId) {
76             mCallback.onCaptureSequenceAborted(AndroidCameraCaptureSessionProxy.this, sequenceId);
77         }
78     }
79 
80     private final CameraCaptureSession mSession;
81 
AndroidCameraCaptureSessionProxy(CameraCaptureSession session)82     public AndroidCameraCaptureSessionProxy(CameraCaptureSession session) {
83         mSession = session;
84     }
85 
86     @Override
abortCaptures()87     public void abortCaptures() throws CameraAccessException, CameraCaptureSessionClosedException {
88         try {
89             mSession.abortCaptures();
90         } catch (IllegalStateException e) {
91             throw new CameraCaptureSessionClosedException(e);
92         }
93     }
94 
95     @Override
capture(CaptureRequest request, CaptureCallback listener, Handler handler)96     public int capture(CaptureRequest request, CaptureCallback listener, Handler handler)
97             throws CameraAccessException, CameraCaptureSessionClosedException {
98         try {
99             return mSession.capture(request, new AndroidCaptureCallback(listener), handler);
100         } catch (IllegalStateException e) {
101             throw new CameraCaptureSessionClosedException(e);
102         }
103     }
104 
105     @Override
captureBurst(List<CaptureRequest> requests, CaptureCallback listener, Handler handler)106     public int captureBurst(List<CaptureRequest> requests, CaptureCallback listener, Handler handler)
107             throws CameraAccessException, CameraCaptureSessionClosedException {
108         try {
109             return mSession.captureBurst(requests, new AndroidCaptureCallback(listener), handler);
110         } catch (IllegalStateException e) {
111             throw new CameraCaptureSessionClosedException(e);
112         }
113     }
114 
115     @Override
close()116     public void close() {
117         mSession.close();
118     }
119 
120     @Override
getDevice()121     public CameraDeviceProxy getDevice() {
122         return new AndroidCameraDeviceProxy(mSession.getDevice());
123     }
124 
125     @Override
setRepeatingBurst(List<CaptureRequest> requests, CaptureCallback listener, Handler handler)126     public int setRepeatingBurst(List<CaptureRequest> requests, CaptureCallback listener,
127             Handler handler) throws CameraAccessException, CameraCaptureSessionClosedException {
128         try {
129             return mSession.setRepeatingBurst(requests, new AndroidCaptureCallback(listener), handler);
130         } catch (IllegalStateException e) {
131             throw new CameraCaptureSessionClosedException(e);
132         }
133     }
134 
135     @Override
setRepeatingRequest(CaptureRequest request, CaptureCallback listener, Handler handler)136     public int setRepeatingRequest(CaptureRequest request, CaptureCallback listener, Handler handler)
137             throws CameraAccessException, CameraCaptureSessionClosedException {
138         try {
139             return mSession.setRepeatingRequest(request, new AndroidCaptureCallback(listener),
140                     handler);
141         } catch (IllegalStateException e) {
142             throw new CameraCaptureSessionClosedException(e);
143         }
144     }
145 
146     @Override
stopRepeating()147     public void stopRepeating() throws CameraAccessException, CameraCaptureSessionClosedException {
148         try {
149             mSession.stopRepeating();
150         } catch (IllegalStateException e) {
151             throw new CameraCaptureSessionClosedException(e);
152         }
153     }
154 }
155