CameraSource.h revision b44c9d2bdc0d5b9cb03254022a58e017b516e9e6
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 29namespace android { 30 31class IMemory; 32class Camera; 33class Surface; 34 35class CameraSource : public MediaSource, public MediaBufferObserver { 36public: 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() { return reset(); } 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 115protected: 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 releaseRecordingFrame(const sp<IMemory>& frame); 167 168 // Returns true if need to skip the current frame. 169 // Called from dataCallbackTimestamp. 170 virtual bool skipCurrentFrame(int64_t timestampUs) {return false;} 171 172 // Callback called when still camera raw data is available. 173 virtual void dataCallback(int32_t msgType, const sp<IMemory> &data) {} 174 175 virtual void dataCallbackTimestamp(int64_t timestampUs, int32_t msgType, 176 const sp<IMemory> &data); 177 178private: 179 friend class CameraSourceListener; 180 181 Mutex mLock; 182 Condition mFrameAvailableCondition; 183 Condition mFrameCompleteCondition; 184 List<sp<IMemory> > mFramesReceived; 185 List<sp<IMemory> > mFramesBeingEncoded; 186 List<int64_t> mFrameTimes; 187 188 int64_t mFirstFrameTimeUs; 189 int32_t mNumFramesDropped; 190 int32_t mNumGlitches; 191 int64_t mGlitchDurationThresholdUs; 192 bool mCollectStats; 193 bool mIsMetaDataStoredInVideoBuffers; 194 195 void releaseQueuedFrames(); 196 void releaseOneRecordingFrame(const sp<IMemory>& frame); 197 198 199 status_t init(const sp<ICamera>& camera, const sp<ICameraRecordingProxy>& proxy, 200 int32_t cameraId, Size videoSize, int32_t frameRate, 201 bool storeMetaDataInVideoBuffers); 202 203 status_t initWithCameraAccess( 204 const sp<ICamera>& camera, const sp<ICameraRecordingProxy>& proxy, 205 int32_t cameraId, Size videoSize, int32_t frameRate, 206 bool storeMetaDataInVideoBuffers); 207 208 status_t isCameraAvailable(const sp<ICamera>& camera, 209 const sp<ICameraRecordingProxy>& proxy, 210 int32_t cameraId); 211 status_t isCameraColorFormatSupported(const CameraParameters& params); 212 status_t configureCamera(CameraParameters* params, 213 int32_t width, int32_t height, 214 int32_t frameRate); 215 216 status_t checkVideoSize(const CameraParameters& params, 217 int32_t width, int32_t height); 218 219 status_t checkFrameRate(const CameraParameters& params, 220 int32_t frameRate); 221 222 void stopCameraRecording(); 223 void releaseCamera(); 224 status_t reset(); 225 226 CameraSource(const CameraSource &); 227 CameraSource &operator=(const CameraSource &); 228}; 229 230} // namespace android 231 232#endif // CAMERA_SOURCE_H_ 233