CameraDeviceBase.h revision 28c9b6f298134624cb52b1af4ed8716dddb983d3
1/*
2 * Copyright (C) 2013 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#ifndef ANDROID_SERVERS_CAMERA_CAMERADEVICEBASE_H
18#define ANDROID_SERVERS_CAMERA_CAMERADEVICEBASE_H
19
20#include <utils/RefBase.h>
21#include <utils/String8.h>
22#include <utils/String16.h>
23#include <utils/Vector.h>
24#include <utils/Timers.h>
25#include <utils/List.h>
26
27#include <camera/camera2/ICameraDeviceCallbacks.h>
28#include "hardware/camera2.h"
29#include "hardware/camera3.h"
30#include "camera/CameraMetadata.h"
31#include "camera/CaptureResult.h"
32
33namespace android {
34
35/**
36 * Base interface for version >= 2 camera device classes, which interface to
37 * camera HAL device versions >= 2.
38 */
39class CameraDeviceBase : public virtual RefBase {
40  public:
41    virtual ~CameraDeviceBase();
42
43    /**
44     * The device's camera ID
45     */
46    virtual int      getId() const = 0;
47
48    virtual status_t initialize(camera_module_t *module) = 0;
49    virtual status_t disconnect() = 0;
50
51    virtual status_t dump(int fd, const Vector<String16> &args) = 0;
52
53    /**
54     * The device's static characteristics metadata buffer
55     */
56    virtual const CameraMetadata& info() const = 0;
57
58    /**
59     * Submit request for capture. The CameraDevice takes ownership of the
60     * passed-in buffer.
61     * Output lastFrameNumber is the expected frame number of this request.
62     */
63    virtual status_t capture(CameraMetadata &request, int64_t *lastFrameNumber = NULL) = 0;
64
65    /**
66     * Submit a list of requests.
67     * Output lastFrameNumber is the expected last frame number of the list of requests.
68     */
69    virtual status_t captureList(const List<const CameraMetadata> &requests,
70                                 int64_t *lastFrameNumber = NULL) = 0;
71
72    /**
73     * Submit request for streaming. The CameraDevice makes a copy of the
74     * passed-in buffer and the caller retains ownership.
75     * Output lastFrameNumber is the last frame number of the previous streaming request.
76     */
77    virtual status_t setStreamingRequest(const CameraMetadata &request,
78                                         int64_t *lastFrameNumber = NULL) = 0;
79
80    /**
81     * Submit a list of requests for streaming.
82     * Output lastFrameNumber is the last frame number of the previous streaming request.
83     */
84    virtual status_t setStreamingRequestList(const List<const CameraMetadata> &requests,
85                                             int64_t *lastFrameNumber = NULL) = 0;
86
87    /**
88     * Clear the streaming request slot.
89     * Output lastFrameNumber is the last frame number of the previous streaming request.
90     */
91    virtual status_t clearStreamingRequest(int64_t *lastFrameNumber = NULL) = 0;
92
93    /**
94     * Wait until a request with the given ID has been dequeued by the
95     * HAL. Returns TIMED_OUT if the timeout duration is reached. Returns
96     * immediately if the latest request received by the HAL has this id.
97     */
98    virtual status_t waitUntilRequestReceived(int32_t requestId,
99            nsecs_t timeout) = 0;
100
101    /**
102     * Create an output stream of the requested size and format.
103     *
104     * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device selects
105     * an appropriate format; it can be queried with getStreamInfo.
106     *
107     * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must be
108     * equal to the size in bytes of the buffers to allocate for the stream. For
109     * other formats, the size parameter is ignored.
110     */
111    virtual status_t createStream(sp<ANativeWindow> consumer,
112            uint32_t width, uint32_t height, int format, int *id) = 0;
113
114    /**
115     * Create an input reprocess stream that uses buffers from an existing
116     * output stream.
117     */
118    virtual status_t createReprocessStreamFromStream(int outputId, int *id) = 0;
119
120    /**
121     * Get information about a given stream.
122     */
123    virtual status_t getStreamInfo(int id,
124            uint32_t *width, uint32_t *height, uint32_t *format) = 0;
125
126    /**
127     * Set stream gralloc buffer transform
128     */
129    virtual status_t setStreamTransform(int id, int transform) = 0;
130
131    /**
132     * Delete stream. Must not be called if there are requests in flight which
133     * reference that stream.
134     */
135    virtual status_t deleteStream(int id) = 0;
136
137    /**
138     * Delete reprocess stream. Must not be called if there are requests in
139     * flight which reference that stream.
140     */
141    virtual status_t deleteReprocessStream(int id) = 0;
142
143    /**
144     * Create a metadata buffer with fields that the HAL device believes are
145     * best for the given use case
146     */
147    virtual status_t createDefaultRequest(int templateId,
148            CameraMetadata *request) = 0;
149
150    /**
151     * Wait until all requests have been processed. Returns INVALID_OPERATION if
152     * the streaming slot is not empty, or TIMED_OUT if the requests haven't
153     * finished processing in 10 seconds.
154     */
155    virtual status_t waitUntilDrained() = 0;
156
157    /**
158     * Get Jpeg buffer size for a given jpeg resolution.
159     * Negative values are error codes.
160     */
161    virtual ssize_t getJpegBufferSize(uint32_t width, uint32_t height) const = 0;
162
163    /**
164     * Abstract class for HAL notification listeners
165     */
166    class NotificationListener {
167      public:
168        // The set of notifications is a merge of the notifications required for
169        // API1 and API2.
170
171        // Required for API 1 and 2
172        virtual void notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode,
173                                 const CaptureResultExtras &resultExtras) = 0;
174
175        // Required only for API2
176        virtual void notifyIdle() = 0;
177        virtual void notifyShutter(const CaptureResultExtras &resultExtras,
178                nsecs_t timestamp) = 0;
179
180        // Required only for API1
181        virtual void notifyAutoFocus(uint8_t newState, int triggerId) = 0;
182        virtual void notifyAutoExposure(uint8_t newState, int triggerId) = 0;
183        virtual void notifyAutoWhitebalance(uint8_t newState,
184                int triggerId) = 0;
185      protected:
186        virtual ~NotificationListener();
187    };
188
189    /**
190     * Connect HAL notifications to a listener. Overwrites previous
191     * listener. Set to NULL to stop receiving notifications.
192     */
193    virtual status_t setNotifyCallback(NotificationListener *listener) = 0;
194
195    /**
196     * Whether the device supports calling notifyAutofocus, notifyAutoExposure,
197     * and notifyAutoWhitebalance; if this returns false, the client must
198     * synthesize these notifications from received frame metadata.
199     */
200    virtual bool     willNotify3A() = 0;
201
202    /**
203     * Wait for a new frame to be produced, with timeout in nanoseconds.
204     * Returns TIMED_OUT when no frame produced within the specified duration
205     * May be called concurrently to most methods, except for getNextFrame
206     */
207    virtual status_t waitForNextFrame(nsecs_t timeout) = 0;
208
209    /**
210     * Get next capture result frame from the result queue. Returns NOT_ENOUGH_DATA
211     * if the queue is empty; caller takes ownership of the metadata buffer inside
212     * the capture result object's metadata field.
213     * May be called concurrently to most methods, except for waitForNextFrame.
214     */
215    virtual status_t getNextResult(CaptureResult *frame) = 0;
216
217    /**
218     * Trigger auto-focus. The latest ID used in a trigger autofocus or cancel
219     * autofocus call will be returned by the HAL in all subsequent AF
220     * notifications.
221     */
222    virtual status_t triggerAutofocus(uint32_t id) = 0;
223
224    /**
225     * Cancel auto-focus. The latest ID used in a trigger autofocus/cancel
226     * autofocus call will be returned by the HAL in all subsequent AF
227     * notifications.
228     */
229    virtual status_t triggerCancelAutofocus(uint32_t id) = 0;
230
231    /**
232     * Trigger pre-capture metering. The latest ID used in a trigger pre-capture
233     * call will be returned by the HAL in all subsequent AE and AWB
234     * notifications.
235     */
236    virtual status_t triggerPrecaptureMetering(uint32_t id) = 0;
237
238    /**
239     * Abstract interface for clients that want to listen to reprocess buffer
240     * release events
241     */
242    struct BufferReleasedListener : public virtual RefBase {
243        virtual void onBufferReleased(buffer_handle_t *handle) = 0;
244    };
245
246    /**
247     * Push a buffer to be reprocessed into a reprocessing stream, and
248     * provide a listener to call once the buffer is returned by the HAL
249     */
250    virtual status_t pushReprocessBuffer(int reprocessStreamId,
251            buffer_handle_t *buffer, wp<BufferReleasedListener> listener) = 0;
252
253    /**
254     * Flush all pending and in-flight requests. Blocks until flush is
255     * complete.
256     * Output lastFrameNumber is the last frame number of the previous streaming request.
257     */
258    virtual status_t flush(int64_t *lastFrameNumber = NULL) = 0;
259
260    /**
261     * Get the HAL device version.
262     */
263    virtual uint32_t getDeviceVersion() = 0;
264};
265
266}; // namespace android
267
268#endif
269