blob: 4a39fbf26575cee2191d9606b76127d8fb35169d [file] [log] [blame]
Andreas Hubere46b7be2009-07-14 16:56:47 -07001/*
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>
James Dong0c128b62010-10-08 11:59:32 -070023#include <camera/ICamera.h>
24#include <camera/CameraParameters.h>
Andreas Hubere46b7be2009-07-14 16:56:47 -070025#include <utils/List.h>
26#include <utils/RefBase.h>
Andreas Hubere46b7be2009-07-14 16:56:47 -070027
28namespace android {
29
Andreas Hubere46b7be2009-07-14 16:56:47 -070030class IMemory;
Andreas Huber155e2ad2009-10-13 17:08:31 -070031class Camera;
James Dong0c128b62010-10-08 11:59:32 -070032class Surface;
Andreas Hubere46b7be2009-07-14 16:56:47 -070033
James Dongdfb1dd62010-05-27 16:05:58 -070034class CameraSource : public MediaSource, public MediaBufferObserver {
Andreas Hubere46b7be2009-07-14 16:56:47 -070035public:
James Dong0c128b62010-10-08 11:59:32 -070036 /**
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 */
Andreas Hubere46b7be2009-07-14 16:56:47 -070043 static CameraSource *Create();
James Dong0c128b62010-10-08 11:59:32 -070044
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
James Dongab79d1f2010-10-18 21:42:27 -070059 * @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().
James Dong0c128b62010-10-08 11:59:32 -070067 *
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,
James Dongab79d1f2010-10-18 21:42:27 -070074 const sp<Surface>& surface,
75 bool storeMetaDataInVideoBuffers = false);
Andreas Hubere46b7be2009-07-14 16:56:47 -070076
77 virtual ~CameraSource();
78
79 virtual status_t start(MetaData *params = NULL);
80 virtual status_t stop();
James Dongab79d1f2010-10-18 21:42:27 -070081 virtual status_t read(
82 MediaBuffer **buffer, const ReadOptions *options = NULL);
Andreas Hubere46b7be2009-07-14 16:56:47 -070083
James Dong0c128b62010-10-08 11:59:32 -070084 /**
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
James Dong997eaa22010-10-09 01:16:58 -070097 * kKeyMIMEType: always fixed to be MEDIA_MIMETYPE_VIDEO_RAW
James Dong0c128b62010-10-08 11:59:32 -070098 */
Andreas Hubere46b7be2009-07-14 16:56:47 -070099 virtual sp<MetaData> getFormat();
100
James Dongab79d1f2010-10-18 21:42:27 -0700101 /**
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;
Andreas Hubere46b7be2009-07-14 16:56:47 -0700138
James Dongdfb1dd62010-05-27 16:05:58 -0700139 virtual void signalBufferReturned(MediaBuffer* buffer);
140
Nipun Kwatraf9b80182010-07-12 09:17:14 -0700141protected:
James Dong0c128b62010-10-08 11:59:32 -0700142 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;
James Dongc2f328d2010-06-03 11:48:31 -0700155 sp<MetaData> mMeta;
Andreas Hubere46b7be2009-07-14 16:56:47 -0700156
Nipun Kwatraf9b80182010-07-12 09:17:14 -0700157 int64_t mStartTimeUs;
158 int32_t mNumFramesReceived;
159 int64_t mLastFrameTimestampUs;
160 bool mStarted;
James Dong5f3ab062011-01-25 16:31:28 -0800161 int32_t mNumFramesEncoded;
Nipun Kwatraf9b80182010-07-12 09:17:14 -0700162
James Dong0c128b62010-10-08 11:59:32 -0700163 CameraSource(const sp<ICamera>& camera, int32_t cameraId,
164 Size videoSize, int32_t frameRate,
James Dongab79d1f2010-10-18 21:42:27 -0700165 const sp<Surface>& surface,
166 bool storeMetaDataInVideoBuffers);
Nipun Kwatraf9b80182010-07-12 09:17:14 -0700167
168 virtual void startCameraRecording();
169 virtual void stopCameraRecording();
170 virtual void releaseRecordingFrame(const sp<IMemory>& frame);
171
172 // Returns true if need to skip the current frame.
173 // Called from dataCallbackTimestamp.
174 virtual bool skipCurrentFrame(int64_t timestampUs) {return false;}
175
176 // Callback called when still camera raw data is available.
177 virtual void dataCallback(int32_t msgType, const sp<IMemory> &data) {}
178
179 virtual void dataCallbackTimestamp(int64_t timestampUs, int32_t msgType,
180 const sp<IMemory> &data);
181
182private:
183 friend class CameraSourceListener;
184
Andreas Hubere46b7be2009-07-14 16:56:47 -0700185 Mutex mLock;
186 Condition mFrameAvailableCondition;
James Dongdfb1dd62010-05-27 16:05:58 -0700187 Condition mFrameCompleteCondition;
188 List<sp<IMemory> > mFramesReceived;
189 List<sp<IMemory> > mFramesBeingEncoded;
Andreas Huber155e2ad2009-10-13 17:08:31 -0700190 List<int64_t> mFrameTimes;
Andreas Hubere46b7be2009-07-14 16:56:47 -0700191
Andreas Huber155e2ad2009-10-13 17:08:31 -0700192 int64_t mFirstFrameTimeUs;
James Dong3300e962010-04-21 16:14:15 -0700193 int32_t mNumFramesDropped;
James Dong36e573b2010-06-19 09:04:18 -0700194 int32_t mNumGlitches;
195 int64_t mGlitchDurationThresholdUs;
James Dongdae9fd32010-06-04 13:59:27 -0700196 bool mCollectStats;
James Dongab79d1f2010-10-18 21:42:27 -0700197 bool mIsMetaDataStoredInVideoBuffers;
James Dongb00e2462010-04-26 17:48:26 -0700198
199 void releaseQueuedFrames();
James Dong36e573b2010-06-19 09:04:18 -0700200 void releaseOneRecordingFrame(const sp<IMemory>& frame);
Andreas Huber155e2ad2009-10-13 17:08:31 -0700201
James Dong0c128b62010-10-08 11:59:32 -0700202
203 status_t init(const sp<ICamera>& camera, int32_t cameraId,
James Dongab79d1f2010-10-18 21:42:27 -0700204 Size videoSize, int32_t frameRate,
205 bool storeMetaDataInVideoBuffers);
James Dong0c128b62010-10-08 11:59:32 -0700206 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
James Dong5df53fe2010-12-05 14:25:34 -0800218 void releaseCamera();
219
Andreas Hubere46b7be2009-07-14 16:56:47 -0700220 CameraSource(const CameraSource &);
221 CameraSource &operator=(const CameraSource &);
222};
223
224} // namespace android
225
226#endif // CAMERA_SOURCE_H_