CameraSource.h revision 9bb976e1c78048081cf9df4d8a1db67311413e5b
1/*
2 * Copyright (C) 2009 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 CAMERA_SOURCE_H_
18
19#define CAMERA_SOURCE_H_
20
21#include <media/stagefright/MediaBuffer.h>
22#include <media/stagefright/MediaSource.h>
23#include <camera/ICamera.h>
24#include <camera/ICameraRecordingProxyListener.h>
25#include <camera/CameraParameters.h>
26#include <utils/List.h>
27#include <utils/RefBase.h>
28#include <utils/String16.h>
29
30namespace android {
31
32class IMemory;
33class Camera;
34class Surface;
35
36class CameraSource : public MediaSource, public MediaBufferObserver {
37public:
38    /**
39     * Factory method to create a new CameraSource using the current
40     * settings (such as video size, frame rate, color format, etc)
41     * from the default camera.
42     *
43     * @param clientName The package/process name of the client application.
44     *    This is used for permissions checking.
45     * @return NULL on error.
46     */
47    static CameraSource *Create(const String16 &clientName);
48
49    /**
50     * Factory method to create a new CameraSource.
51     *
52     * @param camera the video input frame data source. If it is NULL,
53     *          we will try to connect to the camera with the given
54     *          cameraId.
55     *
56     * @param cameraId the id of the camera that the source will connect
57     *          to if camera is NULL; otherwise ignored.
58     * @param clientName the package/process name of the camera-using
59     *          application if camera is NULL; otherwise ignored. Used for
60     *          permissions checking.
61     * @param clientUid the UID of the camera-using application if camera is
62     *          NULL; otherwise ignored. Used for permissions checking.
63     * @param videoSize the dimension (in pixels) of the video frame
64     * @param frameRate the target frames per second
65     * @param surface the preview surface for display where preview
66     *          frames are sent to
67     * @param storeMetaDataInVideoBuffers true to request the camera
68     *          source to store meta data in video buffers; false to
69     *          request the camera source to store real YUV frame data
70     *          in the video buffers. The camera source may not support
71     *          storing meta data in video buffers, if so, a request
72     *          to do that will NOT be honored. To find out whether
73     *          meta data is actually being stored in video buffers
74     *          during recording, call isMetaDataStoredInVideoBuffers().
75     *
76     * @return NULL on error.
77     */
78    static CameraSource *CreateFromCamera(const sp<ICamera> &camera,
79                                          const sp<ICameraRecordingProxy> &proxy,
80                                          int32_t cameraId,
81                                          const String16& clientName,
82                                          uid_t clientUid,
83                                          Size videoSize,
84                                          int32_t frameRate,
85                                          const sp<IGraphicBufferProducer>& surface,
86                                          bool storeMetaDataInVideoBuffers = false);
87
88    virtual ~CameraSource();
89
90    virtual status_t start(MetaData *params = NULL);
91    virtual status_t stop() { return reset(); }
92    virtual status_t read(
93            MediaBuffer **buffer, const ReadOptions *options = NULL);
94
95    /**
96     * Check whether a CameraSource object is properly initialized.
97     * Must call this method before stop().
98     * @return OK if initialization has successfully completed.
99     */
100    virtual status_t initCheck() const;
101
102    /**
103     * Returns the MetaData associated with the CameraSource,
104     * including:
105     * kKeyColorFormat: YUV color format of the video frames
106     * kKeyWidth, kKeyHeight: dimension (in pixels) of the video frames
107     * kKeySampleRate: frame rate in frames per second
108     * kKeyMIMEType: always fixed to be MEDIA_MIMETYPE_VIDEO_RAW
109     */
110    virtual sp<MetaData> getFormat();
111
112    /**
113     * Tell whether this camera source stores meta data or real YUV
114     * frame data in video buffers.
115     *
116     * @return true if meta data is stored in the video
117     *      buffers; false if real YUV data is stored in
118     *      the video buffers.
119     */
120    bool isMetaDataStoredInVideoBuffers() const;
121
122    virtual void signalBufferReturned(MediaBuffer* buffer);
123
124protected:
125    class ProxyListener: public BnCameraRecordingProxyListener {
126    public:
127        ProxyListener(const sp<CameraSource>& source);
128        virtual void dataCallbackTimestamp(int64_t timestampUs, int32_t msgType,
129                const sp<IMemory> &data);
130
131    private:
132        sp<CameraSource> mSource;
133    };
134
135    // isBinderAlive needs linkToDeath to work.
136    class DeathNotifier: public IBinder::DeathRecipient {
137    public:
138        DeathNotifier() {}
139        virtual void binderDied(const wp<IBinder>& who);
140    };
141
142    enum CameraFlags {
143        FLAGS_SET_CAMERA = 1L << 0,
144        FLAGS_HOT_CAMERA = 1L << 1,
145    };
146
147    int32_t  mCameraFlags;
148    Size     mVideoSize;
149    int32_t  mNumInputBuffers;
150    int32_t  mVideoFrameRate;
151    int32_t  mColorFormat;
152    status_t mInitCheck;
153
154    sp<Camera>   mCamera;
155    sp<ICameraRecordingProxy>   mCameraRecordingProxy;
156    sp<DeathNotifier> mDeathNotifier;
157    sp<IGraphicBufferProducer>  mSurface;
158    sp<MetaData> mMeta;
159
160    int64_t mStartTimeUs;
161    int32_t mNumFramesReceived;
162    int64_t mLastFrameTimestampUs;
163    bool mStarted;
164    int32_t mNumFramesEncoded;
165
166    // Time between capture of two frames.
167    int64_t mTimeBetweenFrameCaptureUs;
168
169    CameraSource(const sp<ICamera>& camera, const sp<ICameraRecordingProxy>& proxy,
170                 int32_t cameraId, const String16& clientName, uid_t clientUid,
171                 Size videoSize, int32_t frameRate,
172                 const sp<IGraphicBufferProducer>& surface,
173                 bool storeMetaDataInVideoBuffers);
174
175    virtual void startCameraRecording();
176    virtual void releaseRecordingFrame(const sp<IMemory>& frame);
177
178    // Returns true if need to skip the current frame.
179    // Called from dataCallbackTimestamp.
180    virtual bool skipCurrentFrame(int64_t timestampUs) {return false;}
181
182    // Callback called when still camera raw data is available.
183    virtual void dataCallback(int32_t msgType, const sp<IMemory> &data) {}
184
185    virtual void dataCallbackTimestamp(int64_t timestampUs, int32_t msgType,
186            const sp<IMemory> &data);
187
188    void releaseCamera();
189
190private:
191    friend class CameraSourceListener;
192
193    Mutex mLock;
194    Condition mFrameAvailableCondition;
195    Condition mFrameCompleteCondition;
196    List<sp<IMemory> > mFramesReceived;
197    List<sp<IMemory> > mFramesBeingEncoded;
198    List<int64_t> mFrameTimes;
199
200    int64_t mFirstFrameTimeUs;
201    int32_t mNumFramesDropped;
202    int32_t mNumGlitches;
203    int64_t mGlitchDurationThresholdUs;
204    bool mCollectStats;
205    bool mIsMetaDataStoredInVideoBuffers;
206
207    void releaseQueuedFrames();
208    void releaseOneRecordingFrame(const sp<IMemory>& frame);
209
210
211    status_t init(const sp<ICamera>& camera, const sp<ICameraRecordingProxy>& proxy,
212                  int32_t cameraId, const String16& clientName, uid_t clientUid,
213                  Size videoSize, int32_t frameRate, bool storeMetaDataInVideoBuffers);
214
215    status_t initWithCameraAccess(
216                  const sp<ICamera>& camera, const sp<ICameraRecordingProxy>& proxy,
217                  int32_t cameraId, const String16& clientName, uid_t clientUid,
218                  Size videoSize, int32_t frameRate, bool storeMetaDataInVideoBuffers);
219
220    status_t isCameraAvailable(const sp<ICamera>& camera,
221                               const sp<ICameraRecordingProxy>& proxy,
222                               int32_t cameraId,
223                               const String16& clientName,
224                               uid_t clientUid);
225
226    status_t isCameraColorFormatSupported(const CameraParameters& params);
227    status_t configureCamera(CameraParameters* params,
228                    int32_t width, int32_t height,
229                    int32_t frameRate);
230
231    status_t checkVideoSize(const CameraParameters& params,
232                    int32_t width, int32_t height);
233
234    status_t checkFrameRate(const CameraParameters& params,
235                    int32_t frameRate);
236
237    void stopCameraRecording();
238    status_t reset();
239
240    CameraSource(const CameraSource &);
241    CameraSource &operator=(const CameraSource &);
242};
243
244}  // namespace android
245
246#endif  // CAMERA_SOURCE_H_
247