blob: e6d84ba9d2a613aebe17df2106033cb809fae034 [file] [log] [blame]
Mathias Agopian3cf61352010-02-09 17:46:37 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
Mathias Agopian3cf61352010-02-09 17:46:37 -08003 *
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_HARDWARE_CAMERA_H
18#define ANDROID_HARDWARE_CAMERA_H
19
20#include <utils/Timers.h>
21#include <camera/ICameraClient.h>
22
23namespace android {
24
25class ISurface;
26
27/*
28 * A set of bit masks for specifying how the received preview frames are
29 * handled before the previewCallback() call.
30 *
31 * The least significant 3 bits of an "int" value are used for this purpose:
32 *
33 * ..... 0 0 0
34 * ^ ^ ^
35 * | | |---------> determine whether the callback is enabled or not
36 * | |-----------> determine whether the callback is one-shot or not
37 * |-------------> determine whether the frame is copied out or not
38 *
39 * WARNING:
40 * When a frame is sent directly without copying, it is the frame receiver's
41 * responsiblity to make sure that the frame data won't get corrupted by
42 * subsequent preview frames filled by the camera. This flag is recommended
43 * only when copying out data brings significant performance price and the
44 * handling/processing of the received frame data is always faster than
45 * the preview frame rate so that data corruption won't occur.
46 *
47 * For instance,
48 * 1. 0x00 disables the callback. In this case, copy out and one shot bits
49 * are ignored.
50 * 2. 0x01 enables a callback without copying out the received frames. A
51 * typical use case is the Camcorder application to avoid making costly
52 * frame copies.
53 * 3. 0x05 is enabling a callback with frame copied out repeatedly. A typical
54 * use case is the Camera application.
55 * 4. 0x07 is enabling a callback with frame copied out only once. A typical use
56 * case is the Barcode scanner application.
57 */
58#define FRAME_CALLBACK_FLAG_ENABLE_MASK 0x01
59#define FRAME_CALLBACK_FLAG_ONE_SHOT_MASK 0x02
60#define FRAME_CALLBACK_FLAG_COPY_OUT_MASK 0x04
61
62// Typical use cases
63#define FRAME_CALLBACK_FLAG_NOOP 0x00
64#define FRAME_CALLBACK_FLAG_CAMCORDER 0x01
65#define FRAME_CALLBACK_FLAG_CAMERA 0x05
66#define FRAME_CALLBACK_FLAG_BARCODE_SCANNER 0x07
67
68// msgType in notifyCallback and dataCallback functions
69enum {
70 CAMERA_MSG_ERROR = 0x001,
71 CAMERA_MSG_SHUTTER = 0x002,
72 CAMERA_MSG_FOCUS = 0x004,
73 CAMERA_MSG_ZOOM = 0x008,
74 CAMERA_MSG_PREVIEW_FRAME = 0x010,
75 CAMERA_MSG_VIDEO_FRAME = 0x020,
76 CAMERA_MSG_POSTVIEW_FRAME = 0x040,
77 CAMERA_MSG_RAW_IMAGE = 0x080,
78 CAMERA_MSG_COMPRESSED_IMAGE = 0x100,
79 CAMERA_MSG_ALL_MSGS = 0x1FF
80};
81
82// cmdType in sendCommand functions
83enum {
84 CAMERA_CMD_START_SMOOTH_ZOOM = 1,
85 CAMERA_CMD_STOP_SMOOTH_ZOOM = 2,
Wu-cheng Li95b18cb2010-10-19 17:19:09 +080086 // Set the clockwise rotation of preview display (setPreviewDisplay) in
87 // degrees. This affects the preview frames and the picture displayed after
88 // snapshot. This method is useful for portrait mode applications. Note that
89 // preview display of front-facing cameras is flipped horizontally before
90 // the rotation, that is, the image is reflected along the central vertical
91 // axis of the camera sensor. So the users can see themselves as looking
92 // into a mirror.
93 //
94 // This does not affect the order of byte array of CAMERA_MSG_PREVIEW_FRAME,
95 // CAMERA_MSG_VIDEO_FRAME, CAMERA_MSG_POSTVIEW_FRAME, CAMERA_MSG_RAW_IMAGE,
96 // or CAMERA_MSG_COMPRESSED_IMAGE. This is not allowed to be set during
97 // preview.
Mathias Agopian3cf61352010-02-09 17:46:37 -080098 CAMERA_CMD_SET_DISPLAY_ORIENTATION = 3,
99};
100
101// camera fatal errors
102enum {
103 CAMERA_ERROR_UKNOWN = 1,
104 CAMERA_ERROR_SERVER_DIED = 100
105};
106
Chih-Chung Changddbdb352010-06-10 13:32:16 +0800107enum {
108 CAMERA_FACING_BACK = 0,
109 CAMERA_FACING_FRONT = 1 /* The camera faces to the user */
110};
111
112struct CameraInfo {
113
114 /**
115 * The direction that the camera faces to. It should be
116 * CAMERA_FACING_BACK or CAMERA_FACING_FRONT.
117 */
118 int facing;
119
120 /**
121 * The orientation of the camera image. The value is the angle that the
122 * camera image needs to be rotated clockwise so it shows correctly on
123 * the display in its natural orientation. It should be 0, 90, 180, or 270.
124 *
125 * For example, suppose a device has a naturally tall screen, but the camera
126 * sensor is mounted in landscape. If the top side of the camera sensor is
127 * aligned with the right edge of the display in natural orientation, the
128 * value should be 90.
129 */
130 int orientation;
131};
132
Mathias Agopian3cf61352010-02-09 17:46:37 -0800133class ICameraService;
134class ICamera;
135class Surface;
136class Mutex;
137class String8;
138
139// ref-counted object for callbacks
140class CameraListener: virtual public RefBase
141{
142public:
143 virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2) = 0;
144 virtual void postData(int32_t msgType, const sp<IMemory>& dataPtr) = 0;
145 virtual void postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) = 0;
146};
147
148class Camera : public BnCameraClient, public IBinder::DeathRecipient
149{
150public:
151 // construct a camera client from an existing remote
152 static sp<Camera> create(const sp<ICamera>& camera);
Chih-Chung Chang35a055b2010-05-06 16:36:58 +0800153 static int32_t getNumberOfCameras();
Chih-Chung Changddbdb352010-06-10 13:32:16 +0800154 static status_t getCameraInfo(int cameraId,
155 struct CameraInfo* cameraInfo);
Chih-Chung Chang35a055b2010-05-06 16:36:58 +0800156 static sp<Camera> connect(int cameraId);
Mathias Agopian3cf61352010-02-09 17:46:37 -0800157 ~Camera();
158 void init();
159
160 status_t reconnect();
161 void disconnect();
162 status_t lock();
163 status_t unlock();
164
165 status_t getStatus() { return mStatus; }
166
167 // pass the buffered ISurface to the camera service
168 status_t setPreviewDisplay(const sp<Surface>& surface);
169 status_t setPreviewDisplay(const sp<ISurface>& surface);
170
171 // start preview mode, must call setPreviewDisplay first
172 status_t startPreview();
173
174 // stop preview mode
175 void stopPreview();
176
177 // get preview state
178 bool previewEnabled();
179
180 // start recording mode, must call setPreviewDisplay first
181 status_t startRecording();
182
183 // stop recording mode
184 void stopRecording();
185
186 // get recording state
187 bool recordingEnabled();
188
189 // release a recording frame
190 void releaseRecordingFrame(const sp<IMemory>& mem);
191
192 // autoFocus - status returned from callback
193 status_t autoFocus();
194
195 // cancel auto focus
196 status_t cancelAutoFocus();
197
198 // take a picture - picture returned from callback
199 status_t takePicture();
200
201 // set preview/capture parameters - key/value pairs
202 status_t setParameters(const String8& params);
203
204 // get preview/capture parameters - key/value pairs
205 String8 getParameters() const;
206
207 // send command to camera driver
208 status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
209
210 void setListener(const sp<CameraListener>& listener);
211 void setPreviewCallbackFlags(int preview_callback_flag);
212
213 // ICameraClient interface
214 virtual void notifyCallback(int32_t msgType, int32_t ext, int32_t ext2);
215 virtual void dataCallback(int32_t msgType, const sp<IMemory>& dataPtr);
216 virtual void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
217
218 sp<ICamera> remote();
219
220private:
221 Camera();
222 Camera(const Camera&);
223 Camera& operator=(const Camera);
224 virtual void binderDied(const wp<IBinder>& who);
225
226 class DeathNotifier: public IBinder::DeathRecipient
227 {
228 public:
229 DeathNotifier() {
230 }
231
232 virtual void binderDied(const wp<IBinder>& who);
233 };
234
235 static sp<DeathNotifier> mDeathNotifier;
236
237 // helper function to obtain camera service handle
238 static const sp<ICameraService>& getCameraService();
239
240 sp<ICamera> mCamera;
241 status_t mStatus;
242
243 sp<CameraListener> mListener;
244
245 friend class DeathNotifier;
246
247 static Mutex mLock;
248 static sp<ICameraService> mCameraService;
249
250};
251
252}; // namespace android
253
254#endif