blob: c95f31edfcbb8efa13a398f8b0d5897d6a1c09d3 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
The Android Open Source Project9066cfe2009-03-03 19:31:44 -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
Dave Sparks59c1a932009-07-08 15:56:53 -070020#include <utils/Timers.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080021#include <camera/ICameraClient.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022
23namespace android {
24
25/*
26 * A set of bit masks for specifying how the received preview frames are
27 * handled before the previewCallback() call.
28 *
29 * The least significant 3 bits of an "int" value are used for this purpose:
30 *
31 * ..... 0 0 0
32 * ^ ^ ^
33 * | | |---------> determine whether the callback is enabled or not
34 * | |-----------> determine whether the callback is one-shot or not
35 * |-------------> determine whether the frame is copied out or not
36 *
37 * WARNING:
38 * When a frame is sent directly without copying, it is the frame receiver's
39 * responsiblity to make sure that the frame data won't get corrupted by
40 * subsequent preview frames filled by the camera. This flag is recommended
41 * only when copying out data brings significant performance price and the
42 * handling/processing of the received frame data is always faster than
43 * the preview frame rate so that data corruption won't occur.
44 *
45 * For instance,
46 * 1. 0x00 disables the callback. In this case, copy out and one shot bits
47 * are ignored.
48 * 2. 0x01 enables a callback without copying out the received frames. A
49 * typical use case is the Camcorder application to avoid making costly
50 * frame copies.
51 * 3. 0x05 is enabling a callback with frame copied out repeatedly. A typical
52 * use case is the Camera application.
53 * 4. 0x07 is enabling a callback with frame copied out only once. A typical use
54 * case is the Barcode scanner application.
55 */
56#define FRAME_CALLBACK_FLAG_ENABLE_MASK 0x01
57#define FRAME_CALLBACK_FLAG_ONE_SHOT_MASK 0x02
58#define FRAME_CALLBACK_FLAG_COPY_OUT_MASK 0x04
59
60// Typical use cases
61#define FRAME_CALLBACK_FLAG_NOOP 0x00
62#define FRAME_CALLBACK_FLAG_CAMCORDER 0x01
63#define FRAME_CALLBACK_FLAG_CAMERA 0x05
64#define FRAME_CALLBACK_FLAG_BARCODE_SCANNER 0x07
65
Dave Sparksc62f9bd2009-06-26 13:33:32 -070066// msgType in notifyCallback and dataCallback functions
Dave Sparksd6289b12009-05-07 19:27:32 -070067enum {
Benny Wongda83f462009-08-12 12:01:27 -050068 CAMERA_MSG_ERROR = 0x001,
69 CAMERA_MSG_SHUTTER = 0x002,
70 CAMERA_MSG_FOCUS = 0x004,
71 CAMERA_MSG_ZOOM = 0x008,
72 CAMERA_MSG_PREVIEW_FRAME = 0x010,
73 CAMERA_MSG_VIDEO_FRAME = 0x020,
74 CAMERA_MSG_POSTVIEW_FRAME = 0x040,
75 CAMERA_MSG_RAW_IMAGE = 0x080,
76 CAMERA_MSG_COMPRESSED_IMAGE = 0x100,
77 CAMERA_MSG_ALL_MSGS = 0x1FF
Dave Sparksd6289b12009-05-07 19:27:32 -070078};
79
Wu-cheng Li36f68b82009-09-28 16:14:58 -070080// cmdType in sendCommand functions
81enum {
82 CAMERA_CMD_START_SMOOTH_ZOOM = 1,
83 CAMERA_CMD_STOP_SMOOTH_ZOOM = 2,
Wu-cheng Lid55f7e52010-10-14 20:17:44 +080084 // Set the clockwise rotation of preview display (setPreviewDisplay) in
85 // degrees. This affects the preview frames and the picture displayed after
86 // snapshot. This method is useful for portrait mode applications. Note that
87 // preview display of front-facing cameras is flipped horizontally before
88 // the rotation, that is, the image is reflected along the central vertical
89 // axis of the camera sensor. So the users can see themselves as looking
90 // into a mirror.
91 //
92 // This does not affect the order of byte array of CAMERA_MSG_PREVIEW_FRAME,
93 // CAMERA_MSG_VIDEO_FRAME, CAMERA_MSG_POSTVIEW_FRAME, CAMERA_MSG_RAW_IMAGE,
94 // or CAMERA_MSG_COMPRESSED_IMAGE. This is not allowed to be set during
95 // preview.
Chih-Chung Changd1d77062010-01-22 17:49:48 -080096 CAMERA_CMD_SET_DISPLAY_ORIENTATION = 3,
Nipun Kwatra088146a2010-09-11 19:31:10 -070097
98 // cmdType to disable/enable shutter sound.
99 // In sendCommand passing arg1 = 0 will disable,
100 // while passing arg1 = 1 will enable the shutter sound.
101 CAMERA_CMD_ENABLE_SHUTTER_SOUND = 4,
Nipun Kwatra4e94c302010-09-14 16:49:08 -0700102
103 // cmdType to play recording sound.
104 CAMERA_CMD_PLAY_RECORDING_SOUND = 5,
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700105};
106
James Donga1b653d2009-07-02 10:04:20 -0700107// camera fatal errors
108enum {
James Dong853f6fd2011-01-04 18:32:16 -0800109 CAMERA_ERROR_UNKNOWN = 1,
James Donga1b653d2009-07-02 10:04:20 -0700110 CAMERA_ERROR_SERVER_DIED = 100
111};
112
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800113enum {
Wu-cheng Li99a3f3e2010-11-19 15:56:16 +0800114 CAMERA_FACING_BACK = 0, /* The facing of the camera is opposite to that of the screen. */
115 CAMERA_FACING_FRONT = 1 /* The facing of the camera is the same as that of the screen. */
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800116};
117
118struct CameraInfo {
119
120 /**
121 * The direction that the camera faces to. It should be
122 * CAMERA_FACING_BACK or CAMERA_FACING_FRONT.
123 */
124 int facing;
125
126 /**
127 * The orientation of the camera image. The value is the angle that the
128 * camera image needs to be rotated clockwise so it shows correctly on
129 * the display in its natural orientation. It should be 0, 90, 180, or 270.
130 *
Wu-cheng Li99a3f3e2010-11-19 15:56:16 +0800131 * For example, suppose a device has a naturally tall screen. The
132 * back-facing camera sensor is mounted in landscape. You are looking at
133 * the screen. If the top side of the camera sensor is aligned with the
134 * right edge of the screen in natural orientation, the value should be
135 * 90. If the top side of a front-facing camera sensor is aligned with
136 * the right of the screen, the value should be 270.
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800137 */
138 int orientation;
139};
140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141class ICameraService;
142class ICamera;
143class Surface;
144class Mutex;
145class String8;
146
Dave Sparks5e271152009-06-23 17:30:11 -0700147// ref-counted object for callbacks
148class CameraListener: virtual public RefBase
149{
150public:
151 virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2) = 0;
152 virtual void postData(int32_t msgType, const sp<IMemory>& dataPtr) = 0;
Dave Sparks59c1a932009-07-08 15:56:53 -0700153 virtual void postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) = 0;
Dave Sparks5e271152009-06-23 17:30:11 -0700154};
155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156class Camera : public BnCameraClient, public IBinder::DeathRecipient
157{
158public:
159 // construct a camera client from an existing remote
James Dong2adc2db2009-04-23 14:07:23 -0700160 static sp<Camera> create(const sp<ICamera>& camera);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800161 static int32_t getNumberOfCameras();
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800162 static status_t getCameraInfo(int cameraId,
163 struct CameraInfo* cameraInfo);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800164 static sp<Camera> connect(int cameraId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 ~Camera();
166 void init();
167
168 status_t reconnect();
169 void disconnect();
170 status_t lock();
171 status_t unlock();
172
173 status_t getStatus() { return mStatus; }
174
Jamie Gennis85cfdd02010-08-10 16:37:53 -0700175 // pass the buffered Surface to the camera service
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 status_t setPreviewDisplay(const sp<Surface>& surface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177
178 // start preview mode, must call setPreviewDisplay first
179 status_t startPreview();
180
181 // stop preview mode
182 void stopPreview();
183
184 // get preview state
185 bool previewEnabled();
186
187 // start recording mode, must call setPreviewDisplay first
188 status_t startRecording();
189
190 // stop recording mode
191 void stopRecording();
192
193 // get recording state
194 bool recordingEnabled();
195
196 // release a recording frame
197 void releaseRecordingFrame(const sp<IMemory>& mem);
198
199 // autoFocus - status returned from callback
200 status_t autoFocus();
201
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800202 // cancel auto focus
203 status_t cancelAutoFocus();
204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 // take a picture - picture returned from callback
206 status_t takePicture();
207
208 // set preview/capture parameters - key/value pairs
209 status_t setParameters(const String8& params);
210
211 // get preview/capture parameters - key/value pairs
212 String8 getParameters() const;
213
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700214 // send command to camera driver
215 status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
216
James Dong38311852010-10-18 20:42:51 -0700217 // return the total number of available video buffers.
218 int32_t getNumberOfVideoBuffers() const;
219
220 // return the individual video buffer corresponding to the given index.
221 sp<IMemory> getVideoBuffer(int32_t index) const;
222
223 // tell camera hal to store meta data or real YUV in video buffers.
224 status_t storeMetaDataInBuffers(bool enabled);
225
Dave Sparks5e271152009-06-23 17:30:11 -0700226 void setListener(const sp<CameraListener>& listener);
227 void setPreviewCallbackFlags(int preview_callback_flag);
228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 // ICameraClient interface
Dave Sparks2a04aef2009-05-07 12:25:25 -0700230 virtual void notifyCallback(int32_t msgType, int32_t ext, int32_t ext2);
Dave Sparksd6289b12009-05-07 19:27:32 -0700231 virtual void dataCallback(int32_t msgType, const sp<IMemory>& dataPtr);
Dave Sparks59c1a932009-07-08 15:56:53 -0700232 virtual void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233
234 sp<ICamera> remote();
235
236private:
237 Camera();
Dave Sparks6f0602e2009-06-24 10:42:53 -0700238 Camera(const Camera&);
239 Camera& operator=(const Camera);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 virtual void binderDied(const wp<IBinder>& who);
241
242 class DeathNotifier: public IBinder::DeathRecipient
243 {
244 public:
245 DeathNotifier() {
246 }
247
248 virtual void binderDied(const wp<IBinder>& who);
249 };
250
251 static sp<DeathNotifier> mDeathNotifier;
252
253 // helper function to obtain camera service handle
254 static const sp<ICameraService>& getCameraService();
255
256 sp<ICamera> mCamera;
257 status_t mStatus;
258
Dave Sparks5e271152009-06-23 17:30:11 -0700259 sp<CameraListener> mListener;
260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 friend class DeathNotifier;
262
263 static Mutex mLock;
264 static sp<ICameraService> mCameraService;
265
266};
267
268}; // namespace android
269
270#endif