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