CameraSource.h revision ea7b485595f8cec6a66668b5c54c8f297d843f77
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/CameraParameters.h>
25#include <utils/List.h>
26#include <utils/RefBase.h>
27
28namespace android {
29
30class IMemory;
31class Camera;
32class Surface;
33
34class CameraSource : public MediaSource, public MediaBufferObserver {
35public:
36    /**
37     * Factory method to create a new CameraSource using the current
38     * settings (such as video size, frame rate, color format, etc)
39     * from the default camera.
40     *
41     * @return NULL on error.
42     */
43    static CameraSource *Create();
44
45    /**
46     * Factory method to create a new CameraSource.
47     *
48     * @param camera the video input frame data source. If it is NULL,
49     *          we will try to connect to the camera with the given
50     *          cameraId.
51     *
52     * @param cameraId the id of the camera that the source will connect
53     *          to if camera is NULL; otherwise ignored.
54     *
55     * @param videoSize the dimension (in pixels) of the video frame
56     * @param frameRate the target frames per second
57     * @param surface the preview surface for display where preview
58     *          frames are sent to
59     * @param storeMetaDataInVideoBuffers true to request the camera
60     *          source to store meta data in video buffers; false to
61     *          request the camera source to store real YUV frame data
62     *          in the video buffers. The camera source may not support
63     *          storing meta data in video buffers, if so, a request
64     *          to do that will NOT be honored. To find out whether
65     *          meta data is actually being stored in video buffers
66     *          during recording, call isMetaDataStoredInVideoBuffers().
67     *
68     * @return NULL on error.
69     */
70    static CameraSource *CreateFromCamera(const sp<ICamera> &camera,
71                                          int32_t cameraId,
72                                          Size videoSize,
73                                          int32_t frameRate,
74                                          const sp<Surface>& surface,
75                                          bool storeMetaDataInVideoBuffers = false);
76
77    virtual ~CameraSource();
78
79    virtual status_t start(MetaData *params = NULL);
80    virtual status_t stop();
81    virtual status_t read(
82            MediaBuffer **buffer, const ReadOptions *options = NULL);
83
84    /**
85     * Check whether a CameraSource object is properly initialized.
86     * Must call this method before stop().
87     * @return OK if initialization has successfully completed.
88     */
89    virtual status_t initCheck() const;
90
91    /**
92     * Returns the MetaData associated with the CameraSource,
93     * including:
94     * kKeyColorFormat: YUV color format of the video frames
95     * kKeyWidth, kKeyHeight: dimension (in pixels) of the video frames
96     * kKeySampleRate: frame rate in frames per second
97     * kKeyMIMEType: always fixed to be MEDIA_MIMETYPE_VIDEO_RAW
98     */
99    virtual sp<MetaData> getFormat();
100
101    /**
102     * Retrieve the total number of video buffers available from
103     * this source.
104     *
105     * This method is useful if these video buffers are used
106     * for passing video frame data to other media components,
107     * such as OMX video encoders, in order to eliminate the
108     * memcpy of the data.
109     *
110     * @return the total numbner of video buffers. Returns 0 to
111     *      indicate that this source does not make the video
112     *      buffer information availalble.
113     */
114    size_t getNumberOfVideoBuffers() const;
115
116    /**
117     * Retrieve the individual video buffer available from
118     * this source.
119     *
120     * @param index the index corresponding to the video buffer.
121     *      Valid range of the index is [0, n], where n =
122     *      getNumberOfVideoBuffers() - 1.
123     *
124     * @return the video buffer corresponding to the given index.
125     *      If index is out of range, 0 should be returned.
126     */
127    sp<IMemory> getVideoBuffer(size_t index) const;
128
129    /**
130     * Tell whether this camera source stores meta data or real YUV
131     * frame data in video buffers.
132     *
133     * @return true if meta data is stored in the video
134     *      buffers; false if real YUV data is stored in
135     *      the video buffers.
136     */
137    bool isMetaDataStoredInVideoBuffers() const;
138
139    virtual void signalBufferReturned(MediaBuffer* buffer);
140
141protected:
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  mVideoFrameRate;
150    int32_t  mColorFormat;
151    status_t mInitCheck;
152
153    sp<Camera>   mCamera;
154    sp<Surface>  mSurface;
155    sp<MetaData> mMeta;
156
157    int64_t mStartTimeUs;
158    int32_t mNumFramesReceived;
159    int64_t mLastFrameTimestampUs;
160    bool mStarted;
161
162    CameraSource(const sp<ICamera>& camera, int32_t cameraId,
163                 Size videoSize, int32_t frameRate,
164                 const sp<Surface>& surface,
165                 bool storeMetaDataInVideoBuffers);
166
167    virtual void startCameraRecording();
168    virtual void stopCameraRecording();
169    virtual void releaseRecordingFrame(const sp<IMemory>& frame);
170
171    // Returns true if need to skip the current frame.
172    // Called from dataCallbackTimestamp.
173    virtual bool skipCurrentFrame(int64_t timestampUs) {return false;}
174
175    // Callback called when still camera raw data is available.
176    virtual void dataCallback(int32_t msgType, const sp<IMemory> &data) {}
177
178    virtual void dataCallbackTimestamp(int64_t timestampUs, int32_t msgType,
179            const sp<IMemory> &data);
180
181private:
182    friend class CameraSourceListener;
183
184    Mutex mLock;
185    Condition mFrameAvailableCondition;
186    Condition mFrameCompleteCondition;
187    List<sp<IMemory> > mFramesReceived;
188    List<sp<IMemory> > mFramesBeingEncoded;
189    List<int64_t> mFrameTimes;
190
191    int64_t mFirstFrameTimeUs;
192    int32_t mNumFramesEncoded;
193    int32_t mNumFramesDropped;
194    int32_t mNumGlitches;
195    int64_t mGlitchDurationThresholdUs;
196    bool mCollectStats;
197    bool mIsMetaDataStoredInVideoBuffers;
198
199    void releaseQueuedFrames();
200    void releaseOneRecordingFrame(const sp<IMemory>& frame);
201
202
203    status_t init(const sp<ICamera>& camera, int32_t cameraId,
204                Size videoSize, int32_t frameRate,
205                bool storeMetaDataInVideoBuffers);
206    status_t isCameraAvailable(const sp<ICamera>& camera, int32_t cameraId);
207    status_t isCameraColorFormatSupported(const CameraParameters& params);
208    status_t configureCamera(CameraParameters* params,
209                    int32_t width, int32_t height,
210                    int32_t frameRate);
211
212    status_t checkVideoSize(const CameraParameters& params,
213                    int32_t width, int32_t height);
214
215    status_t checkFrameRate(const CameraParameters& params,
216                    int32_t frameRate);
217
218    void releaseCamera();
219
220    CameraSource(const CameraSource &);
221    CameraSource &operator=(const CameraSource &);
222};
223
224}  // namespace android
225
226#endif  // CAMERA_SOURCE_H_
227