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