Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 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> |
James Dong | 0c128b6 | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 23 | #include <camera/ICamera.h> |
| 24 | #include <camera/CameraParameters.h> |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 25 | #include <utils/List.h> |
| 26 | #include <utils/RefBase.h> |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 30 | class IMemory; |
Andreas Huber | 155e2ad | 2009-10-13 17:08:31 -0700 | [diff] [blame] | 31 | class Camera; |
James Dong | 0c128b6 | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 32 | class Surface; |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 33 | |
James Dong | dfb1dd6 | 2010-05-27 16:05:58 -0700 | [diff] [blame] | 34 | class CameraSource : public MediaSource, public MediaBufferObserver { |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 35 | public: |
James Dong | 0c128b6 | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 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 | */ |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 43 | static CameraSource *Create(); |
James Dong | 0c128b6 | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 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 |
James Dong | ab79d1f | 2010-10-18 21:42:27 -0700 | [diff] [blame] | 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(). |
James Dong | 0c128b6 | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 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, |
James Dong | ab79d1f | 2010-10-18 21:42:27 -0700 | [diff] [blame] | 74 | const sp<Surface>& surface, |
| 75 | bool storeMetaDataInVideoBuffers = false); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 76 | |
| 77 | virtual ~CameraSource(); |
| 78 | |
| 79 | virtual status_t start(MetaData *params = NULL); |
| 80 | virtual status_t stop(); |
James Dong | ab79d1f | 2010-10-18 21:42:27 -0700 | [diff] [blame] | 81 | virtual status_t read( |
| 82 | MediaBuffer **buffer, const ReadOptions *options = NULL); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 83 | |
James Dong | 0c128b6 | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 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 |
James Dong | 997eaa2 | 2010-10-09 01:16:58 -0700 | [diff] [blame] | 97 | * kKeyMIMEType: always fixed to be MEDIA_MIMETYPE_VIDEO_RAW |
James Dong | 0c128b6 | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 98 | */ |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 99 | virtual sp<MetaData> getFormat(); |
| 100 | |
James Dong | ab79d1f | 2010-10-18 21:42:27 -0700 | [diff] [blame] | 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; |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 138 | |
James Dong | dfb1dd6 | 2010-05-27 16:05:58 -0700 | [diff] [blame] | 139 | virtual void signalBufferReturned(MediaBuffer* buffer); |
| 140 | |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 141 | protected: |
James Dong | 0c128b6 | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 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; |
James Dong | c2f328d | 2010-06-03 11:48:31 -0700 | [diff] [blame] | 155 | sp<MetaData> mMeta; |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 156 | |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 157 | int64_t mStartTimeUs; |
| 158 | int32_t mNumFramesReceived; |
| 159 | int64_t mLastFrameTimestampUs; |
| 160 | bool mStarted; |
James Dong | 5f3ab06 | 2011-01-25 16:31:28 -0800 | [diff] [blame^] | 161 | int32_t mNumFramesEncoded; |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 162 | |
James Dong | 0c128b6 | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 163 | CameraSource(const sp<ICamera>& camera, int32_t cameraId, |
| 164 | Size videoSize, int32_t frameRate, |
James Dong | ab79d1f | 2010-10-18 21:42:27 -0700 | [diff] [blame] | 165 | const sp<Surface>& surface, |
| 166 | bool storeMetaDataInVideoBuffers); |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 167 | |
| 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 | |
| 182 | private: |
| 183 | friend class CameraSourceListener; |
| 184 | |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 185 | Mutex mLock; |
| 186 | Condition mFrameAvailableCondition; |
James Dong | dfb1dd6 | 2010-05-27 16:05:58 -0700 | [diff] [blame] | 187 | Condition mFrameCompleteCondition; |
| 188 | List<sp<IMemory> > mFramesReceived; |
| 189 | List<sp<IMemory> > mFramesBeingEncoded; |
Andreas Huber | 155e2ad | 2009-10-13 17:08:31 -0700 | [diff] [blame] | 190 | List<int64_t> mFrameTimes; |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 191 | |
Andreas Huber | 155e2ad | 2009-10-13 17:08:31 -0700 | [diff] [blame] | 192 | int64_t mFirstFrameTimeUs; |
James Dong | 3300e96 | 2010-04-21 16:14:15 -0700 | [diff] [blame] | 193 | int32_t mNumFramesDropped; |
James Dong | 36e573b | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 194 | int32_t mNumGlitches; |
| 195 | int64_t mGlitchDurationThresholdUs; |
James Dong | dae9fd3 | 2010-06-04 13:59:27 -0700 | [diff] [blame] | 196 | bool mCollectStats; |
James Dong | ab79d1f | 2010-10-18 21:42:27 -0700 | [diff] [blame] | 197 | bool mIsMetaDataStoredInVideoBuffers; |
James Dong | b00e246 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 198 | |
| 199 | void releaseQueuedFrames(); |
James Dong | 36e573b | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 200 | void releaseOneRecordingFrame(const sp<IMemory>& frame); |
Andreas Huber | 155e2ad | 2009-10-13 17:08:31 -0700 | [diff] [blame] | 201 | |
James Dong | 0c128b6 | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 202 | |
| 203 | status_t init(const sp<ICamera>& camera, int32_t cameraId, |
James Dong | ab79d1f | 2010-10-18 21:42:27 -0700 | [diff] [blame] | 204 | Size videoSize, int32_t frameRate, |
| 205 | bool storeMetaDataInVideoBuffers); |
James Dong | 0c128b6 | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 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 | |
James Dong | 5df53fe | 2010-12-05 14:25:34 -0800 | [diff] [blame] | 218 | void releaseCamera(); |
| 219 | |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 220 | CameraSource(const CameraSource &); |
| 221 | CameraSource &operator=(const CameraSource &); |
| 222 | }; |
| 223 | |
| 224 | } // namespace android |
| 225 | |
| 226 | #endif // CAMERA_SOURCE_H_ |