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 | /** |
James Dong | ab79d1f | 2010-10-18 21:42:27 -0700 | [diff] [blame] | 102 | * 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 Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 110 | |
James Dong | dfb1dd6 | 2010-05-27 16:05:58 -0700 | [diff] [blame] | 111 | virtual void signalBufferReturned(MediaBuffer* buffer); |
| 112 | |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 113 | protected: |
James Dong | 0c128b6 | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 114 | 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 Dong | c2f328d | 2010-06-03 11:48:31 -0700 | [diff] [blame] | 127 | sp<MetaData> mMeta; |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 128 | |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 129 | int64_t mStartTimeUs; |
| 130 | int32_t mNumFramesReceived; |
| 131 | int64_t mLastFrameTimestampUs; |
| 132 | bool mStarted; |
James Dong | 5f3ab06 | 2011-01-25 16:31:28 -0800 | [diff] [blame] | 133 | int32_t mNumFramesEncoded; |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 134 | |
James Dong | 0c128b6 | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 135 | CameraSource(const sp<ICamera>& camera, int32_t cameraId, |
| 136 | Size videoSize, int32_t frameRate, |
James Dong | ab79d1f | 2010-10-18 21:42:27 -0700 | [diff] [blame] | 137 | const sp<Surface>& surface, |
| 138 | bool storeMetaDataInVideoBuffers); |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 139 | |
| 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 | |
| 154 | private: |
| 155 | friend class CameraSourceListener; |
| 156 | |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 157 | Mutex mLock; |
| 158 | Condition mFrameAvailableCondition; |
James Dong | dfb1dd6 | 2010-05-27 16:05:58 -0700 | [diff] [blame] | 159 | Condition mFrameCompleteCondition; |
| 160 | List<sp<IMemory> > mFramesReceived; |
| 161 | List<sp<IMemory> > mFramesBeingEncoded; |
Andreas Huber | 155e2ad | 2009-10-13 17:08:31 -0700 | [diff] [blame] | 162 | List<int64_t> mFrameTimes; |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 163 | |
Andreas Huber | 155e2ad | 2009-10-13 17:08:31 -0700 | [diff] [blame] | 164 | int64_t mFirstFrameTimeUs; |
James Dong | 3300e96 | 2010-04-21 16:14:15 -0700 | [diff] [blame] | 165 | int32_t mNumFramesDropped; |
James Dong | 36e573b | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 166 | int32_t mNumGlitches; |
| 167 | int64_t mGlitchDurationThresholdUs; |
James Dong | dae9fd3 | 2010-06-04 13:59:27 -0700 | [diff] [blame] | 168 | bool mCollectStats; |
James Dong | ab79d1f | 2010-10-18 21:42:27 -0700 | [diff] [blame] | 169 | bool mIsMetaDataStoredInVideoBuffers; |
James Dong | b00e246 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 170 | |
| 171 | void releaseQueuedFrames(); |
James Dong | 36e573b | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 172 | void releaseOneRecordingFrame(const sp<IMemory>& frame); |
Andreas Huber | 155e2ad | 2009-10-13 17:08:31 -0700 | [diff] [blame] | 173 | |
James Dong | 0c128b6 | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 174 | |
| 175 | status_t init(const sp<ICamera>& camera, int32_t cameraId, |
James Dong | ab79d1f | 2010-10-18 21:42:27 -0700 | [diff] [blame] | 176 | Size videoSize, int32_t frameRate, |
| 177 | bool storeMetaDataInVideoBuffers); |
James Dong | 0c128b6 | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 178 | 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 Dong | 5df53fe | 2010-12-05 14:25:34 -0800 | [diff] [blame] | 190 | void releaseCamera(); |
| 191 | |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 192 | CameraSource(const CameraSource &); |
| 193 | CameraSource &operator=(const CameraSource &); |
| 194 | }; |
| 195 | |
| 196 | } // namespace android |
| 197 | |
| 198 | #endif // CAMERA_SOURCE_H_ |