blob: 964700b9fd8363d6d71ddc6d0bc5f147115dc299 [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,
86 CAMERA_CMD_SET_DISPLAY_ORIENTATION = 3,
87};
88
89// camera fatal errors
90enum {
91 CAMERA_ERROR_UKNOWN = 1,
92 CAMERA_ERROR_SERVER_DIED = 100
93};
94
Chih-Chung Changddbdb352010-06-10 13:32:16 +080095enum {
96 CAMERA_FACING_BACK = 0,
97 CAMERA_FACING_FRONT = 1 /* The camera faces to the user */
98};
99
100struct CameraInfo {
101
102 /**
103 * The direction that the camera faces to. It should be
104 * CAMERA_FACING_BACK or CAMERA_FACING_FRONT.
105 */
106 int facing;
107
108 /**
109 * The orientation of the camera image. The value is the angle that the
110 * camera image needs to be rotated clockwise so it shows correctly on
111 * the display in its natural orientation. It should be 0, 90, 180, or 270.
112 *
113 * For example, suppose a device has a naturally tall screen, but the camera
114 * sensor is mounted in landscape. If the top side of the camera sensor is
115 * aligned with the right edge of the display in natural orientation, the
116 * value should be 90.
117 */
118 int orientation;
119};
120
Mathias Agopian3cf61352010-02-09 17:46:37 -0800121class ICameraService;
122class ICamera;
123class Surface;
124class Mutex;
125class String8;
126
127// ref-counted object for callbacks
128class CameraListener: virtual public RefBase
129{
130public:
131 virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2) = 0;
132 virtual void postData(int32_t msgType, const sp<IMemory>& dataPtr) = 0;
133 virtual void postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) = 0;
134};
135
136class Camera : public BnCameraClient, public IBinder::DeathRecipient
137{
138public:
139 // construct a camera client from an existing remote
140 static sp<Camera> create(const sp<ICamera>& camera);
Chih-Chung Chang35a055b2010-05-06 16:36:58 +0800141 static int32_t getNumberOfCameras();
Chih-Chung Changddbdb352010-06-10 13:32:16 +0800142 static status_t getCameraInfo(int cameraId,
143 struct CameraInfo* cameraInfo);
Chih-Chung Chang35a055b2010-05-06 16:36:58 +0800144 static sp<Camera> connect(int cameraId);
Mathias Agopian3cf61352010-02-09 17:46:37 -0800145 ~Camera();
146 void init();
147
148 status_t reconnect();
149 void disconnect();
150 status_t lock();
151 status_t unlock();
152
153 status_t getStatus() { return mStatus; }
154
155 // pass the buffered ISurface to the camera service
156 status_t setPreviewDisplay(const sp<Surface>& surface);
157 status_t setPreviewDisplay(const sp<ISurface>& surface);
158
159 // start preview mode, must call setPreviewDisplay first
160 status_t startPreview();
161
162 // stop preview mode
163 void stopPreview();
164
165 // get preview state
166 bool previewEnabled();
167
168 // start recording mode, must call setPreviewDisplay first
169 status_t startRecording();
170
171 // stop recording mode
172 void stopRecording();
173
174 // get recording state
175 bool recordingEnabled();
176
177 // release a recording frame
178 void releaseRecordingFrame(const sp<IMemory>& mem);
179
180 // autoFocus - status returned from callback
181 status_t autoFocus();
182
183 // cancel auto focus
184 status_t cancelAutoFocus();
185
186 // take a picture - picture returned from callback
187 status_t takePicture();
188
189 // set preview/capture parameters - key/value pairs
190 status_t setParameters(const String8& params);
191
192 // get preview/capture parameters - key/value pairs
193 String8 getParameters() const;
194
195 // send command to camera driver
196 status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
197
198 void setListener(const sp<CameraListener>& listener);
199 void setPreviewCallbackFlags(int preview_callback_flag);
200
201 // ICameraClient interface
202 virtual void notifyCallback(int32_t msgType, int32_t ext, int32_t ext2);
203 virtual void dataCallback(int32_t msgType, const sp<IMemory>& dataPtr);
204 virtual void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
205
206 sp<ICamera> remote();
207
208private:
209 Camera();
210 Camera(const Camera&);
211 Camera& operator=(const Camera);
212 virtual void binderDied(const wp<IBinder>& who);
213
214 class DeathNotifier: public IBinder::DeathRecipient
215 {
216 public:
217 DeathNotifier() {
218 }
219
220 virtual void binderDied(const wp<IBinder>& who);
221 };
222
223 static sp<DeathNotifier> mDeathNotifier;
224
225 // helper function to obtain camera service handle
226 static const sp<ICameraService>& getCameraService();
227
228 sp<ICamera> mCamera;
229 status_t mStatus;
230
231 sp<CameraListener> mListener;
232
233 friend class DeathNotifier;
234
235 static Mutex mLock;
236 static sp<ICameraService> mCameraService;
237
238};
239
240}; // namespace android
241
242#endif