Camera.h revision bfa33aae4f54c0020a0568b16a3acb7b30b6ca3d
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#include <gui/ISurfaceTexture.h>
23
24namespace android {
25
26/*
27 * A set of bit masks for specifying how the received preview frames are
28 * handled before the previewCallback() call.
29 *
30 * The least significant 3 bits of an "int" value are used for this purpose:
31 *
32 * ..... 0 0 0
33 *       ^ ^ ^
34 *       | | |---------> determine whether the callback is enabled or not
35 *       | |-----------> determine whether the callback is one-shot or not
36 *       |-------------> determine whether the frame is copied out or not
37 *
38 * WARNING:
39 * When a frame is sent directly without copying, it is the frame receiver's
40 * responsiblity to make sure that the frame data won't get corrupted by
41 * subsequent preview frames filled by the camera. This flag is recommended
42 * only when copying out data brings significant performance price and the
43 * handling/processing of the received frame data is always faster than
44 * the preview frame rate so that data corruption won't occur.
45 *
46 * For instance,
47 * 1. 0x00 disables the callback. In this case, copy out and one shot bits
48 *    are ignored.
49 * 2. 0x01 enables a callback without copying out the received frames. A
50 *    typical use case is the Camcorder application to avoid making costly
51 *    frame copies.
52 * 3. 0x05 is enabling a callback with frame copied out repeatedly. A typical
53 *    use case is the Camera application.
54 * 4. 0x07 is enabling a callback with frame copied out only once. A typical use
55 *    case is the Barcode scanner application.
56 */
57#define FRAME_CALLBACK_FLAG_ENABLE_MASK              0x01
58#define FRAME_CALLBACK_FLAG_ONE_SHOT_MASK            0x02
59#define FRAME_CALLBACK_FLAG_COPY_OUT_MASK            0x04
60
61// Typical use cases
62#define FRAME_CALLBACK_FLAG_NOOP                     0x00
63#define FRAME_CALLBACK_FLAG_CAMCORDER                0x01
64#define FRAME_CALLBACK_FLAG_CAMERA                   0x05
65#define FRAME_CALLBACK_FLAG_BARCODE_SCANNER          0x07
66
67// msgType in notifyCallback and dataCallback functions
68enum {
69    CAMERA_MSG_ERROR            = 0x001,
70    CAMERA_MSG_SHUTTER          = 0x002,
71    CAMERA_MSG_FOCUS            = 0x004,
72    CAMERA_MSG_ZOOM             = 0x008,
73    CAMERA_MSG_PREVIEW_FRAME    = 0x010,
74    CAMERA_MSG_VIDEO_FRAME      = 0x020,
75    CAMERA_MSG_POSTVIEW_FRAME   = 0x040,
76    CAMERA_MSG_RAW_IMAGE        = 0x080,
77    CAMERA_MSG_COMPRESSED_IMAGE = 0x100,
78    CAMERA_MSG_ALL_MSGS         = 0x1FF
79};
80
81// cmdType in sendCommand functions
82enum {
83    CAMERA_CMD_START_SMOOTH_ZOOM     = 1,
84    CAMERA_CMD_STOP_SMOOTH_ZOOM      = 2,
85    // Set the clockwise rotation of preview display (setPreviewDisplay) in
86    // degrees. This affects the preview frames and the picture displayed after
87    // snapshot. This method is useful for portrait mode applications. Note that
88    // preview display of front-facing cameras is flipped horizontally before
89    // the rotation, that is, the image is reflected along the central vertical
90    // axis of the camera sensor. So the users can see themselves as looking
91    // into a mirror.
92    //
93    // This does not affect the order of byte array of CAMERA_MSG_PREVIEW_FRAME,
94    // CAMERA_MSG_VIDEO_FRAME, CAMERA_MSG_POSTVIEW_FRAME, CAMERA_MSG_RAW_IMAGE,
95    // or CAMERA_MSG_COMPRESSED_IMAGE. This is not allowed to be set during
96    // preview.
97    CAMERA_CMD_SET_DISPLAY_ORIENTATION = 3,
98
99    // cmdType to disable/enable shutter sound.
100    // In sendCommand passing arg1 = 0 will disable,
101    // while passing arg1 = 1 will enable the shutter sound.
102    CAMERA_CMD_ENABLE_SHUTTER_SOUND = 4,
103
104    // cmdType to play recording sound.
105    CAMERA_CMD_PLAY_RECORDING_SOUND = 5,
106};
107
108// camera fatal errors
109enum {
110    CAMERA_ERROR_UNKNOWN  = 1,
111    CAMERA_ERROR_SERVER_DIED = 100
112};
113
114enum {
115    CAMERA_FACING_BACK = 0, /* The facing of the camera is opposite to that of the screen. */
116    CAMERA_FACING_FRONT = 1 /* The facing of the camera is the same as that of the screen. */
117};
118
119struct CameraInfo {
120
121    /**
122     * The direction that the camera faces to. It should be
123     * CAMERA_FACING_BACK or CAMERA_FACING_FRONT.
124     */
125    int facing;
126
127    /**
128     * The orientation of the camera image. The value is the angle that the
129     * camera image needs to be rotated clockwise so it shows correctly on
130     * the display in its natural orientation. It should be 0, 90, 180, or 270.
131     *
132     * For example, suppose a device has a naturally tall screen. The
133     * back-facing camera sensor is mounted in landscape. You are looking at
134     * the screen. If the top side of the camera sensor is aligned with the
135     * right edge of the screen in natural orientation, the value should be
136     * 90. If the top side of a front-facing camera sensor is aligned with
137     * the right of the screen, the value should be 270.
138     */
139    int orientation;
140};
141
142class ICameraService;
143class ICamera;
144class Surface;
145class Mutex;
146class String8;
147
148// ref-counted object for callbacks
149class CameraListener: virtual public RefBase
150{
151public:
152    virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2) = 0;
153    virtual void postData(int32_t msgType, const sp<IMemory>& dataPtr) = 0;
154    virtual void postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) = 0;
155};
156
157class Camera : public BnCameraClient, public IBinder::DeathRecipient
158{
159public:
160            // construct a camera client from an existing remote
161    static  sp<Camera>  create(const sp<ICamera>& camera);
162    static  int32_t     getNumberOfCameras();
163    static  status_t    getCameraInfo(int cameraId,
164                                      struct CameraInfo* cameraInfo);
165    static  sp<Camera>  connect(int cameraId);
166                        ~Camera();
167            void        init();
168
169            status_t    reconnect();
170            void        disconnect();
171            status_t    lock();
172            status_t    unlock();
173
174            status_t    getStatus() { return mStatus; }
175
176            // pass the buffered Surface to the camera service
177            status_t    setPreviewDisplay(const sp<Surface>& surface);
178
179            // pass the buffered ISurfaceTexture to the camera service
180            status_t    setPreviewTexture(const sp<ISurfaceTexture>& surfaceTexture);
181
182            // start preview mode, must call setPreviewDisplay first
183            status_t    startPreview();
184
185            // stop preview mode
186            void        stopPreview();
187
188            // get preview state
189            bool        previewEnabled();
190
191            // start recording mode, must call setPreviewDisplay first
192            status_t    startRecording();
193
194            // stop recording mode
195            void        stopRecording();
196
197            // get recording state
198            bool        recordingEnabled();
199
200            // release a recording frame
201            void        releaseRecordingFrame(const sp<IMemory>& mem);
202
203            // autoFocus - status returned from callback
204            status_t    autoFocus();
205
206            // cancel auto focus
207            status_t    cancelAutoFocus();
208
209            // take a picture - picture returned from callback
210            status_t    takePicture();
211
212            // set preview/capture parameters - key/value pairs
213            status_t    setParameters(const String8& params);
214
215            // get preview/capture parameters - key/value pairs
216            String8     getParameters() const;
217
218            // send command to camera driver
219            status_t    sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
220
221            // return the total number of available video buffers.
222            int32_t     getNumberOfVideoBuffers() const;
223
224            // return the individual video buffer corresponding to the given index.
225            sp<IMemory> getVideoBuffer(int32_t index) const;
226
227            // tell camera hal to store meta data or real YUV in video buffers.
228            status_t    storeMetaDataInBuffers(bool enabled);
229
230            void        setListener(const sp<CameraListener>& listener);
231            void        setPreviewCallbackFlags(int preview_callback_flag);
232
233    // ICameraClient interface
234    virtual void        notifyCallback(int32_t msgType, int32_t ext, int32_t ext2);
235    virtual void        dataCallback(int32_t msgType, const sp<IMemory>& dataPtr);
236    virtual void        dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
237
238    sp<ICamera>         remote();
239
240private:
241                        Camera();
242                        Camera(const Camera&);
243                        Camera& operator=(const Camera);
244                        virtual void binderDied(const wp<IBinder>& who);
245
246            class DeathNotifier: public IBinder::DeathRecipient
247            {
248            public:
249                DeathNotifier() {
250                }
251
252                virtual void binderDied(const wp<IBinder>& who);
253            };
254
255            static sp<DeathNotifier> mDeathNotifier;
256
257            // helper function to obtain camera service handle
258            static const sp<ICameraService>& getCameraService();
259
260            sp<ICamera>         mCamera;
261            status_t            mStatus;
262
263            sp<CameraListener>  mListener;
264
265            friend class DeathNotifier;
266
267            static  Mutex               mLock;
268            static  sp<ICameraService>  mCameraService;
269
270};
271
272}; // namespace android
273
274#endif
275