Camera.h revision c2c8868e59bde7f48998e1d89a0d77217bc64767
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
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    // 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.
98    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
107enum {
108    CAMERA_FACING_BACK = 0, /* The facing of the camera is opposite to that of the screen. */
109    CAMERA_FACING_FRONT = 1 /* The facing of the camera is the same as that of the screen. */
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. The
126     * back-facing camera sensor is mounted in landscape. You are looking at
127     * the screen. If the top side of the camera sensor is aligned with the
128     * right edge of the screen in natural orientation, the value should be
129     * 90. If the top side of a front-facing camera sensor is aligned with
130     * the right of the screen, the value should be 270.
131     */
132    int orientation;
133};
134
135class ICameraService;
136class ICamera;
137class Surface;
138class Mutex;
139class String8;
140
141// ref-counted object for callbacks
142class CameraListener: virtual public RefBase
143{
144public:
145    virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2) = 0;
146    virtual void postData(int32_t msgType, const sp<IMemory>& dataPtr) = 0;
147    virtual void postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) = 0;
148};
149
150class Camera : public BnCameraClient, public IBinder::DeathRecipient
151{
152public:
153            // construct a camera client from an existing remote
154    static  sp<Camera>  create(const sp<ICamera>& camera);
155    static  int32_t     getNumberOfCameras();
156    static  status_t    getCameraInfo(int cameraId,
157                                      struct CameraInfo* cameraInfo);
158    static  sp<Camera>  connect(int cameraId);
159                        ~Camera();
160            void        init();
161
162            status_t    reconnect();
163            void        disconnect();
164            status_t    lock();
165            status_t    unlock();
166
167            status_t    getStatus() { return mStatus; }
168
169            // pass the buffered ISurface to the camera service
170            status_t    setPreviewDisplay(const sp<Surface>& surface);
171            status_t    setPreviewDisplay(const sp<ISurface>& surface);
172
173            // start preview mode, must call setPreviewDisplay first
174            status_t    startPreview();
175
176            // stop preview mode
177            void        stopPreview();
178
179            // get preview state
180            bool        previewEnabled();
181
182            // start recording mode, must call setPreviewDisplay first
183            status_t    startRecording();
184
185            // stop recording mode
186            void        stopRecording();
187
188            // get recording state
189            bool        recordingEnabled();
190
191            // release a recording frame
192            void        releaseRecordingFrame(const sp<IMemory>& mem);
193
194            // autoFocus - status returned from callback
195            status_t    autoFocus();
196
197            // cancel auto focus
198            status_t    cancelAutoFocus();
199
200            // take a picture - picture returned from callback
201            status_t    takePicture();
202
203            // set preview/capture parameters - key/value pairs
204            status_t    setParameters(const String8& params);
205
206            // get preview/capture parameters - key/value pairs
207            String8     getParameters() const;
208
209            // send command to camera driver
210            status_t    sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
211
212            void        setListener(const sp<CameraListener>& listener);
213            void        setPreviewCallbackFlags(int preview_callback_flag);
214
215    // ICameraClient interface
216    virtual void        notifyCallback(int32_t msgType, int32_t ext, int32_t ext2);
217    virtual void        dataCallback(int32_t msgType, const sp<IMemory>& dataPtr);
218    virtual void        dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
219
220    sp<ICamera>         remote();
221
222private:
223                        Camera();
224                        Camera(const Camera&);
225                        Camera& operator=(const Camera);
226                        virtual void binderDied(const wp<IBinder>& who);
227
228            class DeathNotifier: public IBinder::DeathRecipient
229            {
230            public:
231                DeathNotifier() {
232                }
233
234                virtual void binderDied(const wp<IBinder>& who);
235            };
236
237            static sp<DeathNotifier> mDeathNotifier;
238
239            // helper function to obtain camera service handle
240            static const sp<ICameraService>& getCameraService();
241
242            sp<ICamera>         mCamera;
243            status_t            mStatus;
244
245            sp<CameraListener>  mListener;
246
247            friend class DeathNotifier;
248
249            static  Mutex               mLock;
250            static  sp<ICameraService>  mCameraService;
251
252};
253
254}; // namespace android
255
256#endif
257