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