blob: bb25bae3007e9d8702a95d1f51285148a3f01a0c [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 /**
James Dongab79d1f2010-10-18 21:42:27 -0700102 * Tell whether this camera source stores meta data or real YUV
103 * frame data in video buffers.
104 *
105 * @return true if meta data is stored in the video
106 * buffers; false if real YUV data is stored in
107 * the video buffers.
108 */
109 bool isMetaDataStoredInVideoBuffers() const;
Andreas Hubere46b7be2009-07-14 16:56:47 -0700110
James Dongdfb1dd62010-05-27 16:05:58 -0700111 virtual void signalBufferReturned(MediaBuffer* buffer);
112
Nipun Kwatraf9b80182010-07-12 09:17:14 -0700113protected:
James Dong0c128b62010-10-08 11:59:32 -0700114 enum CameraFlags {
115 FLAGS_SET_CAMERA = 1L << 0,
116 FLAGS_HOT_CAMERA = 1L << 1,
117 };
118
119 int32_t mCameraFlags;
120 Size mVideoSize;
121 int32_t mVideoFrameRate;
122 int32_t mColorFormat;
123 status_t mInitCheck;
124
125 sp<Camera> mCamera;
126 sp<Surface> mSurface;
James Dongc2f328d2010-06-03 11:48:31 -0700127 sp<MetaData> mMeta;
Andreas Hubere46b7be2009-07-14 16:56:47 -0700128
Nipun Kwatraf9b80182010-07-12 09:17:14 -0700129 int64_t mStartTimeUs;
130 int32_t mNumFramesReceived;
131 int64_t mLastFrameTimestampUs;
132 bool mStarted;
James Dong5f3ab062011-01-25 16:31:28 -0800133 int32_t mNumFramesEncoded;
Nipun Kwatraf9b80182010-07-12 09:17:14 -0700134
James Dong0c128b62010-10-08 11:59:32 -0700135 CameraSource(const sp<ICamera>& camera, int32_t cameraId,
136 Size videoSize, int32_t frameRate,
James Dongab79d1f2010-10-18 21:42:27 -0700137 const sp<Surface>& surface,
138 bool storeMetaDataInVideoBuffers);
Nipun Kwatraf9b80182010-07-12 09:17:14 -0700139
140 virtual void startCameraRecording();
141 virtual void stopCameraRecording();
142 virtual void releaseRecordingFrame(const sp<IMemory>& frame);
143
144 // Returns true if need to skip the current frame.
145 // Called from dataCallbackTimestamp.
146 virtual bool skipCurrentFrame(int64_t timestampUs) {return false;}
147
148 // Callback called when still camera raw data is available.
149 virtual void dataCallback(int32_t msgType, const sp<IMemory> &data) {}
150
151 virtual void dataCallbackTimestamp(int64_t timestampUs, int32_t msgType,
152 const sp<IMemory> &data);
153
154private:
155 friend class CameraSourceListener;
156
Andreas Hubere46b7be2009-07-14 16:56:47 -0700157 Mutex mLock;
158 Condition mFrameAvailableCondition;
James Dongdfb1dd62010-05-27 16:05:58 -0700159 Condition mFrameCompleteCondition;
160 List<sp<IMemory> > mFramesReceived;
161 List<sp<IMemory> > mFramesBeingEncoded;
Andreas Huber155e2ad2009-10-13 17:08:31 -0700162 List<int64_t> mFrameTimes;
Andreas Hubere46b7be2009-07-14 16:56:47 -0700163
Andreas Huber155e2ad2009-10-13 17:08:31 -0700164 int64_t mFirstFrameTimeUs;
James Dong3300e962010-04-21 16:14:15 -0700165 int32_t mNumFramesDropped;
James Dong36e573b2010-06-19 09:04:18 -0700166 int32_t mNumGlitches;
167 int64_t mGlitchDurationThresholdUs;
James Dongdae9fd32010-06-04 13:59:27 -0700168 bool mCollectStats;
James Dongab79d1f2010-10-18 21:42:27 -0700169 bool mIsMetaDataStoredInVideoBuffers;
James Dongb00e2462010-04-26 17:48:26 -0700170
171 void releaseQueuedFrames();
James Dong36e573b2010-06-19 09:04:18 -0700172 void releaseOneRecordingFrame(const sp<IMemory>& frame);
Andreas Huber155e2ad2009-10-13 17:08:31 -0700173
James Dong0c128b62010-10-08 11:59:32 -0700174
175 status_t init(const sp<ICamera>& camera, int32_t cameraId,
James Dongab79d1f2010-10-18 21:42:27 -0700176 Size videoSize, int32_t frameRate,
177 bool storeMetaDataInVideoBuffers);
James Dong0c128b62010-10-08 11:59:32 -0700178 status_t isCameraAvailable(const sp<ICamera>& camera, int32_t cameraId);
179 status_t isCameraColorFormatSupported(const CameraParameters& params);
180 status_t configureCamera(CameraParameters* params,
181 int32_t width, int32_t height,
182 int32_t frameRate);
183
184 status_t checkVideoSize(const CameraParameters& params,
185 int32_t width, int32_t height);
186
187 status_t checkFrameRate(const CameraParameters& params,
188 int32_t frameRate);
189
James Dong5df53fe2010-12-05 14:25:34 -0800190 void releaseCamera();
191
Andreas Hubere46b7be2009-07-14 16:56:47 -0700192 CameraSource(const CameraSource &);
193 CameraSource &operator=(const CameraSource &);
194};
195
196} // namespace android
197
198#endif // CAMERA_SOURCE_H_