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