CameraSource.h revision 54ff19ac69ace7c05ea90d225e26dab3b133f487
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/CameraParameters.h> 25#include <utils/List.h> 26#include <utils/RefBase.h> 27 28namespace android { 29 30class IMemory; 31class Camera; 32class Surface; 33 34class CameraSource : public MediaSource, public MediaBufferObserver { 35public: 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 */ 43 static CameraSource *Create(); 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 59 * 60 * @return NULL on error. 61 */ 62 static CameraSource *CreateFromCamera(const sp<ICamera> &camera, 63 int32_t cameraId, 64 Size videoSize, 65 int32_t frameRate, 66 const sp<Surface>& surface); 67 68 virtual ~CameraSource(); 69 70 virtual status_t start(MetaData *params = NULL); 71 virtual status_t stop(); 72 73 /** 74 * Check whether a CameraSource object is properly initialized. 75 * Must call this method before stop(). 76 * @return OK if initialization has successfully completed. 77 */ 78 virtual status_t initCheck() const; 79 80 /** 81 * Returns the MetaData associated with the CameraSource, 82 * including: 83 * kKeyColorFormat: YUV color format of the video frames 84 * kKeyWidth, kKeyHeight: dimension (in pixels) of the video frames 85 * kKeySampleRate: frame rate in frames per second 86 * kKeyMimeType: always fixed 87 */ 88 virtual sp<MetaData> getFormat(); 89 90 virtual status_t read( 91 MediaBuffer **buffer, const ReadOptions *options = NULL); 92 93 virtual void signalBufferReturned(MediaBuffer* buffer); 94 95protected: 96 enum CameraFlags { 97 FLAGS_SET_CAMERA = 1L << 0, 98 FLAGS_HOT_CAMERA = 1L << 1, 99 }; 100 101 int32_t mCameraFlags; 102 Size mVideoSize; 103 int32_t mVideoFrameRate; 104 int32_t mColorFormat; 105 status_t mInitCheck; 106 107 sp<Camera> mCamera; 108 sp<Surface> mSurface; 109 sp<MetaData> mMeta; 110 111 int64_t mStartTimeUs; 112 int32_t mNumFramesReceived; 113 int64_t mLastFrameTimestampUs; 114 bool mStarted; 115 116 CameraSource(const sp<ICamera>& camera, int32_t cameraId, 117 Size videoSize, int32_t frameRate, 118 const sp<Surface>& surface); 119 120 virtual void startCameraRecording(); 121 virtual void stopCameraRecording(); 122 virtual void releaseRecordingFrame(const sp<IMemory>& frame); 123 124 // Returns true if need to skip the current frame. 125 // Called from dataCallbackTimestamp. 126 virtual bool skipCurrentFrame(int64_t timestampUs) {return false;} 127 128 // Callback called when still camera raw data is available. 129 virtual void dataCallback(int32_t msgType, const sp<IMemory> &data) {} 130 131 virtual void dataCallbackTimestamp(int64_t timestampUs, int32_t msgType, 132 const sp<IMemory> &data); 133 134private: 135 friend class CameraSourceListener; 136 137 Mutex mLock; 138 Condition mFrameAvailableCondition; 139 Condition mFrameCompleteCondition; 140 List<sp<IMemory> > mFramesReceived; 141 List<sp<IMemory> > mFramesBeingEncoded; 142 List<int64_t> mFrameTimes; 143 144 int64_t mFirstFrameTimeUs; 145 int32_t mNumFramesEncoded; 146 int32_t mNumFramesDropped; 147 int32_t mNumGlitches; 148 int64_t mGlitchDurationThresholdUs; 149 bool mCollectStats; 150 151 void releaseQueuedFrames(); 152 void releaseOneRecordingFrame(const sp<IMemory>& frame); 153 154 155 status_t init(const sp<ICamera>& camera, int32_t cameraId, 156 Size videoSize, int32_t frameRate); 157 status_t isCameraAvailable(const sp<ICamera>& camera, int32_t cameraId); 158 status_t isCameraColorFormatSupported(const CameraParameters& params); 159 status_t configureCamera(CameraParameters* params, 160 int32_t width, int32_t height, 161 int32_t frameRate); 162 163 status_t checkVideoSize(const CameraParameters& params, 164 int32_t width, int32_t height); 165 166 status_t checkFrameRate(const CameraParameters& params, 167 int32_t frameRate); 168 169 CameraSource(const CameraSource &); 170 CameraSource &operator=(const CameraSource &); 171}; 172 173} // namespace android 174 175#endif // CAMERA_SOURCE_H_ 176