summaryrefslogtreecommitdiffstats
path: root/camera2/public/src/com/android/ex/camera2/blocking/BlockingCaptureListener.java
blob: eae85d133565891abccafb6f4d18a10fbb604352 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * Copyright 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.ex.camera2.blocking;

import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CaptureFailure;
import android.hardware.camera2.CaptureRequest;
import android.hardware.camera2.CaptureResult;
import android.hardware.camera2.TotalCaptureResult;
import android.util.Log;

import com.android.ex.camera2.utils.StateChangeListener;
import com.android.ex.camera2.utils.StateWaiter;

/**
 * A camera capture listener that implements blocking operations on state changes for a
 * particular capture request.
 *
 * <p>Provides a waiter that can be used to block until the next unobserved state of the
 * requested type arrives.</p>
 *
 * <p>Pass-through all StateListener changes to the proxy.</p>
 *
 * @see #getStateWaiter
 */
public class BlockingCaptureListener extends CameraCaptureSession.CaptureListener {

    /**
     * {@link #onCaptureStarted} has been called.
     */
    public static final int CAPTURE_STARTED = 0;

    /**
     * {@link #onCaptureProgressed} has been
     * called.
     */
    public static final int CAPTURE_PROGRESSED = 1;

    /**
     * {@link #onCaptureCompleted} has
     * been called.
     */
    public static final int CAPTURE_COMPLETED = 2;

    /**
     * {@link #onCaptureFailed} has been
     * called.
     */
    public static final int CAPTURE_FAILED = 3;

    /**
     * {@link #onCaptureSequenceCompleted} has been called.
     */
    public static final int CAPTURE_SEQUENCE_COMPLETED = 4;

    /**
     * {@link #onCaptureSequenceAborted} has been called.
     */
    public static final int CAPTURE_SEQUENCE_ABORTED = 5;

    private static final String[] sStateNames = {
            "CAPTURE_STARTED",
            "CAPTURE_PROGRESSED",
            "CAPTURE_COMPLETED",
            "CAPTURE_FAILED",
            "CAPTURE_SEQUENCE_COMPLETED",
            "CAPTURE_SEQUENCE_ABORTED"
    };

    private static final String TAG = "BlockingCaptureListener";
    private static final boolean VERBOSE = Log.isLoggable(TAG, Log.VERBOSE);

    private final CameraCaptureSession.CaptureListener mProxy;

    private final StateWaiter mStateWaiter = new StateWaiter(sStateNames);
    private final StateChangeListener mStateChangeListener = mStateWaiter.getListener();

    /**
     * Create a blocking capture listener without forwarding the capture listener invocations
     * to another capture listener.
     */
    public BlockingCaptureListener() {
        mProxy = null;
    }

    /**
     * Create a blocking capture listener; forward original listener invocations
     * into {@code listener}.
     *
     * @param listener a non-{@code null} listener to forward invocations into
     *
     * @throws NullPointerException if {@code listener} was {@code null}
     */
    public BlockingCaptureListener(CameraCaptureSession.CaptureListener listener) {
        if (listener == null) {
            throw new NullPointerException("listener must not be null");
        }
        mProxy = listener;
    }

    /**
     * Acquire the state waiter; can be used to block until a set of state transitions have
     * been reached.
     *
     * <p>Only one thread should wait at a time.</p>
     */
    public StateWaiter getStateWaiter() {
        return mStateWaiter;
    }

    @Override
    public void onCaptureStarted(CameraCaptureSession session, CaptureRequest request,
                                 long timestamp) {
        if (mProxy != null) mProxy.onCaptureStarted(session, request, timestamp);
        mStateChangeListener.onStateChanged(CAPTURE_STARTED);
    }

    @Override
    public void onCaptureProgressed(CameraCaptureSession session, CaptureRequest request,
                                    CaptureResult partialResult) {
        if (mProxy != null) mProxy.onCaptureProgressed(session, request, partialResult);
        mStateChangeListener.onStateChanged(CAPTURE_PROGRESSED);
    }

    @Override
    public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request,
                                   TotalCaptureResult result) {
        if (mProxy != null) mProxy.onCaptureCompleted(session, request, result);
        mStateChangeListener.onStateChanged(CAPTURE_COMPLETED);
    }

    @Override
    public void onCaptureFailed(CameraCaptureSession session, CaptureRequest request,
                                CaptureFailure failure) {
        if (mProxy != null) mProxy.onCaptureFailed(session, request, failure);
        mStateChangeListener.onStateChanged(CAPTURE_FAILED);
    }

    @Override
    public void onCaptureSequenceCompleted(CameraCaptureSession session, int sequenceId,
                                           long frameNumber) {
        if (mProxy != null) mProxy.onCaptureSequenceCompleted(session, sequenceId, frameNumber);
        mStateChangeListener.onStateChanged(CAPTURE_SEQUENCE_COMPLETED);
    }

    @Override
    public void onCaptureSequenceAborted(CameraCaptureSession session, int sequenceId) {
        if (mProxy != null) mProxy.onCaptureSequenceAborted(session, sequenceId);
        mStateChangeListener.onStateChanged(CAPTURE_SEQUENCE_ABORTED);
    }
}