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
17package com.android.camera.one.v2.camera2proxy;
18
19import android.hardware.camera2.CameraAccessException;
20import android.hardware.camera2.CameraCaptureSession;
21import android.hardware.camera2.CaptureFailure;
22import android.hardware.camera2.CaptureRequest;
23import android.hardware.camera2.CaptureResult;
24import android.hardware.camera2.TotalCaptureResult;
25import android.os.Handler;
26
27import java.util.List;
28
29/**
30 * A CameraCaptureSessionProxy backed by an
31 * {@link android.hardware.camera2.CameraCaptureSession}.
32 */
33public class AndroidCameraCaptureSessionProxy implements CameraCaptureSessionProxy {
34    private class AndroidCaptureCallback extends CameraCaptureSession.CaptureCallback {
35        private final CaptureCallback mCallback;
36
37        private AndroidCaptureCallback(CaptureCallback callback) {
38            mCallback = callback;
39        }
40
41        @Override
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
49        public void onCaptureProgressed(CameraCaptureSession session, CaptureRequest request,
50                CaptureResult partialResult) {
51            mCallback.onCaptureProgressed(AndroidCameraCaptureSessionProxy.this, request,
52                    partialResult);
53        }
54
55        @Override
56        public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request,
57                TotalCaptureResult result) {
58            mCallback.onCaptureCompleted(AndroidCameraCaptureSessionProxy.this, request, result);
59        }
60
61        @Override
62        public void onCaptureFailed(CameraCaptureSession session, CaptureRequest request,
63                CaptureFailure failure) {
64            mCallback.onCaptureFailed(AndroidCameraCaptureSessionProxy.this, request, failure);
65        }
66
67        @Override
68        public void onCaptureSequenceCompleted(CameraCaptureSession session, int sequenceId,
69                long frameNumber) {
70            mCallback.onCaptureSequenceCompleted(AndroidCameraCaptureSessionProxy.this,
71                    sequenceId, frameNumber);
72        }
73
74        @Override
75        public void onCaptureSequenceAborted(CameraCaptureSession session, int sequenceId) {
76            mCallback.onCaptureSequenceAborted(AndroidCameraCaptureSessionProxy.this, sequenceId);
77        }
78    }
79
80    private final CameraCaptureSession mSession;
81
82    public AndroidCameraCaptureSessionProxy(CameraCaptureSession session) {
83        mSession = session;
84    }
85
86    @Override
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
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
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
116    public void close() {
117        mSession.close();
118    }
119
120    @Override
121    public CameraDeviceProxy getDevice() {
122        return new AndroidCameraDeviceProxy(mSession.getDevice());
123    }
124
125    @Override
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
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
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