Camera.h revision e2b43843fd12783188edd2c54188ea8d26864788
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
22#include <android/hardware/ICameraService.h>
23
24#include <gui/IGraphicBufferProducer.h>
25#include <system/camera.h>
26#include <camera/ICameraRecordingProxy.h>
27#include <camera/ICameraRecordingProxyListener.h>
28#include <camera/android/hardware/ICamera.h>
29#include <camera/android/hardware/ICameraClient.h>
30#include <camera/CameraBase.h>
31
32namespace android {
33
34class Surface;
35class String8;
36class String16;
37
38// ref-counted object for callbacks
39class CameraListener: virtual public RefBase
40{
41public:
42    virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2) = 0;
43    virtual void postData(int32_t msgType, const sp<IMemory>& dataPtr,
44                          camera_frame_metadata_t *metadata) = 0;
45    virtual void postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) = 0;
46    virtual void postRecordingFrameHandleTimestamp(nsecs_t timestamp, native_handle_t* handle) = 0;
47};
48
49class Camera;
50
51template <>
52struct CameraTraits<Camera>
53{
54    typedef CameraListener                     TCamListener;
55    typedef ::android::hardware::ICamera       TCamUser;
56    typedef ::android::hardware::ICameraClient TCamCallbacks;
57    typedef ::android::binder::Status(::android::hardware::ICameraService::*TCamConnectService)
58        (const sp<::android::hardware::ICameraClient>&,
59        int, const String16&, int, int,
60        /*out*/
61        sp<::android::hardware::ICamera>*);
62    static TCamConnectService     fnConnectService;
63};
64
65
66class Camera :
67    public CameraBase<Camera>,
68    public ::android::hardware::BnCameraClient
69{
70public:
71    enum {
72        USE_CALLING_UID = ::android::hardware::ICameraService::USE_CALLING_UID
73    };
74    enum {
75        USE_CALLING_PID = ::android::hardware::ICameraService::USE_CALLING_PID
76    };
77
78            // construct a camera client from an existing remote
79    static  sp<Camera>  create(const sp<::android::hardware::ICamera>& camera);
80    static  sp<Camera>  connect(int cameraId,
81                                const String16& clientPackageName,
82                                int clientUid, int clientPid);
83
84    static  status_t  connectLegacy(int cameraId, int halVersion,
85                                     const String16& clientPackageName,
86                                     int clientUid, sp<Camera>& camera);
87
88            virtual     ~Camera();
89
90            status_t    reconnect();
91            status_t    lock();
92            status_t    unlock();
93
94            // pass the buffered IGraphicBufferProducer to the camera service
95            status_t    setPreviewTarget(const sp<IGraphicBufferProducer>& bufferProducer);
96
97            // start preview mode, must call setPreviewTarget first
98            status_t    startPreview();
99
100            // stop preview mode
101            void        stopPreview();
102
103            // get preview state
104            bool        previewEnabled();
105
106            // start recording mode, must call setPreviewTarget first
107            status_t    startRecording();
108
109            // stop recording mode
110            void        stopRecording();
111
112            // get recording state
113            bool        recordingEnabled();
114
115            // release a recording frame
116            void        releaseRecordingFrame(const sp<IMemory>& mem);
117
118            // release a recording frame handle
119            void        releaseRecordingFrameHandle(native_handle_t *handle);
120
121            // autoFocus - status returned from callback
122            status_t    autoFocus();
123
124            // cancel auto focus
125            status_t    cancelAutoFocus();
126
127            // take a picture - picture returned from callback
128            status_t    takePicture(int msgType);
129
130            // set preview/capture parameters - key/value pairs
131            status_t    setParameters(const String8& params);
132
133            // get preview/capture parameters - key/value pairs
134            String8     getParameters() const;
135
136            // send command to camera driver
137            status_t    sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
138
139            // Tell camera how to pass video buffers. videoBufferMode is one of VIDEO_BUFFER_MODE_*.
140            // Returns OK if the specified video buffer mode is supported. If videoBufferMode is
141            // VIDEO_BUFFER_MODE_BUFFER_QUEUE, setVideoTarget() must be called before starting
142            // video recording.
143            status_t    setVideoBufferMode(int32_t videoBufferMode);
144
145            // Set the video buffer producer for camera to use in VIDEO_BUFFER_MODE_BUFFER_QUEUE
146            // mode.
147            status_t    setVideoTarget(const sp<IGraphicBufferProducer>& bufferProducer);
148
149            void        setListener(const sp<CameraListener>& listener);
150            void        setRecordingProxyListener(const sp<ICameraRecordingProxyListener>& listener);
151
152            // Configure preview callbacks to app. Only one of the older
153            // callbacks or the callback surface can be active at the same time;
154            // enabling one will disable the other if active. Flags can be
155            // disabled by calling it with CAMERA_FRAME_CALLBACK_FLAG_NOOP, and
156            // Target by calling it with a NULL interface.
157            void        setPreviewCallbackFlags(int preview_callback_flag);
158            status_t    setPreviewCallbackTarget(
159                    const sp<IGraphicBufferProducer>& callbackProducer);
160
161            sp<ICameraRecordingProxy> getRecordingProxy();
162
163    // ICameraClient interface
164    virtual void        notifyCallback(int32_t msgType, int32_t ext, int32_t ext2);
165    virtual void        dataCallback(int32_t msgType, const sp<IMemory>& dataPtr,
166                                     camera_frame_metadata_t *metadata);
167    virtual void        dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
168    virtual void        recordingFrameHandleCallbackTimestamp(nsecs_t timestamp, native_handle_t* handle);
169
170    class RecordingProxy : public BnCameraRecordingProxy
171    {
172    public:
173        explicit RecordingProxy(const sp<Camera>& camera);
174
175        // ICameraRecordingProxy interface
176        virtual status_t startRecording(const sp<ICameraRecordingProxyListener>& listener);
177        virtual void stopRecording();
178        virtual void releaseRecordingFrame(const sp<IMemory>& mem);
179        virtual void releaseRecordingFrameHandle(native_handle_t* handle);
180
181    private:
182        sp<Camera>         mCamera;
183    };
184
185protected:
186    explicit            Camera(int cameraId);
187                        Camera(const Camera&);
188                        Camera& operator=(const Camera);
189
190    sp<ICameraRecordingProxyListener>  mRecordingProxyListener;
191
192    friend class        CameraBase;
193};
194
195}; // namespace android
196
197#endif
198