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 <deque> 22 #include <media/MediaSource.h> 23 #include <media/stagefright/MediaBuffer.h> 24 #include <camera/android/hardware/ICamera.h> 25 #include <camera/ICameraRecordingProxy.h> 26 #include <camera/ICameraRecordingProxyListener.h> 27 #include <camera/CameraParameters.h> 28 #include <gui/BufferItemConsumer.h> 29 #include <utils/List.h> 30 #include <utils/RefBase.h> 31 #include <utils/String16.h> 32 #include <media/hardware/MetadataBufferType.h> 33 34 namespace android { 35 36 class IMemory; 37 class Camera; 38 class Surface; 39 40 class CameraSource : public MediaSource, public MediaBufferObserver { 41 public: 42 /** 43 * Factory method to create a new CameraSource using the current 44 * settings (such as video size, frame rate, color format, etc) 45 * from the default camera. 46 * 47 * @param clientName The package/process name of the client application. 48 * This is used for permissions checking. 49 * @return NULL on error. 50 */ 51 static CameraSource *Create(const String16 &clientName); 52 53 /** 54 * Factory method to create a new CameraSource. 55 * 56 * @param camera the video input frame data source. If it is NULL, 57 * we will try to connect to the camera with the given 58 * cameraId. 59 * 60 * @param cameraId the id of the camera that the source will connect 61 * to if camera is NULL; otherwise ignored. 62 * @param clientName the package/process name of the camera-using 63 * application if camera is NULL; otherwise ignored. Used for 64 * permissions checking. 65 * @param clientUid the UID of the camera-using application if camera is 66 * NULL; otherwise ignored. Used for permissions checking. 67 * @param clientPid the PID of the camera-using application if camera is 68 * NULL; otherwise ignored. Used for permissions checking. 69 * @param videoSize the dimension (in pixels) of the video frame 70 * @param frameRate the target frames per second 71 * @param surface the preview surface for display where preview 72 * frames are sent to 73 * @param storeMetaDataInVideoBuffers true to request the camera 74 * source to store meta data in video buffers; false to 75 * request the camera source to store real YUV frame data 76 * in the video buffers. The camera source may not support 77 * storing meta data in video buffers, if so, a request 78 * to do that will NOT be honored. To find out whether 79 * meta data is actually being stored in video buffers 80 * during recording, call isMetaDataStoredInVideoBuffers(). 81 * 82 * @return NULL on error. 83 */ 84 static CameraSource *CreateFromCamera(const sp<hardware::ICamera> &camera, 85 const sp<ICameraRecordingProxy> &proxy, 86 int32_t cameraId, 87 const String16& clientName, 88 uid_t clientUid, 89 pid_t clientPid, 90 Size videoSize, 91 int32_t frameRate, 92 const sp<IGraphicBufferProducer>& surface, 93 bool storeMetaDataInVideoBuffers = true); 94 95 virtual ~CameraSource(); 96 97 virtual status_t start(MetaData *params = NULL); stop()98 virtual status_t stop() { return reset(); } 99 virtual status_t read( 100 MediaBufferBase **buffer, const ReadOptions *options = NULL); 101 virtual status_t setStopTimeUs(int64_t stopTimeUs); 102 103 /** 104 * Check whether a CameraSource object is properly initialized. 105 * Must call this method before stop(). 106 * @return OK if initialization has successfully completed. 107 */ 108 virtual status_t initCheck() const; 109 110 /** 111 * Returns the MetaData associated with the CameraSource, 112 * including: 113 * kKeyColorFormat: YUV color format of the video frames 114 * kKeyWidth, kKeyHeight: dimension (in pixels) of the video frames 115 * kKeySampleRate: frame rate in frames per second 116 * kKeyMIMEType: always fixed to be MEDIA_MIMETYPE_VIDEO_RAW 117 */ 118 virtual sp<MetaData> getFormat(); 119 120 /** 121 * Tell whether this camera source stores meta data or real YUV 122 * frame data in video buffers. 123 * 124 * @return a valid type if meta data is stored in the video 125 * buffers; kMetadataBufferTypeInvalid if real YUV data is stored in 126 * the video buffers. 127 */ 128 MetadataBufferType metaDataStoredInVideoBuffers() const; 129 130 virtual void signalBufferReturned(MediaBufferBase* buffer); 131 132 protected: 133 134 /** 135 * The class for listening to BnCameraRecordingProxyListener. This is used to receive video 136 * buffers in VIDEO_BUFFER_MODE_DATA_CALLBACK_YUV and VIDEO_BUFFER_MODE_DATA_CALLBACK_METADATA 137 * mode. When a frame is available, CameraSource::dataCallbackTimestamp() will be called. 138 */ 139 class ProxyListener: public BnCameraRecordingProxyListener { 140 public: 141 ProxyListener(const sp<CameraSource>& source); 142 virtual void dataCallbackTimestamp(int64_t timestampUs, int32_t msgType, 143 const sp<IMemory> &data); 144 virtual void recordingFrameHandleCallbackTimestamp(int64_t timestampUs, 145 native_handle_t* handle); 146 virtual void recordingFrameHandleCallbackTimestampBatch( 147 const std::vector<int64_t>& timestampsUs, 148 const std::vector<native_handle_t*>& handles); 149 150 private: 151 sp<CameraSource> mSource; 152 }; 153 154 /** 155 * The class for listening to BufferQueue's onFrameAvailable. This is used to receive video 156 * buffers in VIDEO_BUFFER_MODE_BUFFER_QUEUE mode. When a frame is available, 157 * CameraSource::processBufferQueueFrame() will be called. 158 */ 159 class BufferQueueListener : public Thread, public BufferItemConsumer::FrameAvailableListener { 160 public: 161 BufferQueueListener(const sp<BufferItemConsumer> &consumer, 162 const sp<CameraSource> &cameraSource); 163 virtual void onFrameAvailable(const BufferItem& item); 164 virtual bool threadLoop(); 165 private: 166 static const nsecs_t kFrameAvailableTimeout = 50000000; // 50ms 167 168 sp<BufferItemConsumer> mConsumer; 169 sp<CameraSource> mCameraSource; 170 171 Mutex mLock; 172 Condition mFrameAvailableSignal; 173 bool mFrameAvailable; 174 }; 175 176 // isBinderAlive needs linkToDeath to work. 177 class DeathNotifier: public IBinder::DeathRecipient { 178 public: DeathNotifier()179 DeathNotifier() {} 180 virtual void binderDied(const wp<IBinder>& who); 181 }; 182 183 enum CameraFlags { 184 FLAGS_SET_CAMERA = 1L << 0, 185 FLAGS_HOT_CAMERA = 1L << 1, 186 }; 187 188 int32_t mCameraFlags; 189 Size mVideoSize; 190 int32_t mNumInputBuffers; 191 int32_t mVideoFrameRate; 192 int32_t mColorFormat; 193 int32_t mEncoderFormat; 194 int32_t mEncoderDataSpace; 195 status_t mInitCheck; 196 197 sp<Camera> mCamera; 198 sp<ICameraRecordingProxy> mCameraRecordingProxy; 199 sp<DeathNotifier> mDeathNotifier; 200 sp<IGraphicBufferProducer> mSurface; 201 sp<MetaData> mMeta; 202 203 int64_t mStartTimeUs; 204 int32_t mNumFramesReceived; 205 int64_t mLastFrameTimestampUs; 206 bool mStarted; 207 bool mEos; 208 int32_t mNumFramesEncoded; 209 210 // Time between capture of two frames. 211 int64_t mTimeBetweenFrameCaptureUs; 212 213 CameraSource(const sp<hardware::ICamera>& camera, const sp<ICameraRecordingProxy>& proxy, 214 int32_t cameraId, const String16& clientName, uid_t clientUid, pid_t clientPid, 215 Size videoSize, int32_t frameRate, 216 const sp<IGraphicBufferProducer>& surface, 217 bool storeMetaDataInVideoBuffers); 218 219 virtual status_t startCameraRecording(); 220 virtual void releaseRecordingFrame(const sp<IMemory>& frame); 221 virtual void releaseRecordingFrameHandle(native_handle_t* handle); 222 // stagefright recorder not using this for now 223 virtual void releaseRecordingFrameHandleBatch(const std::vector<native_handle_t*>& handles); 224 225 // Returns true if need to skip the current frame. 226 // Called from dataCallbackTimestamp. skipCurrentFrame(int64_t)227 virtual bool skipCurrentFrame(int64_t /*timestampUs*/) {return false;} 228 229 // Callback called when still camera raw data is available. dataCallback(int32_t,const sp<IMemory> &)230 virtual void dataCallback(int32_t /*msgType*/, const sp<IMemory>& /*data*/) {} 231 232 virtual void dataCallbackTimestamp(int64_t timestampUs, int32_t msgType, 233 const sp<IMemory> &data); 234 235 virtual void recordingFrameHandleCallbackTimestamp(int64_t timestampUs, 236 native_handle_t* handle); 237 238 virtual void recordingFrameHandleCallbackTimestampBatch( 239 const std::vector<int64_t>& timestampsUs, 240 const std::vector<native_handle_t*>& handles); 241 242 // Process a buffer item received in BufferQueueListener. 243 virtual void processBufferQueueFrame(BufferItem& buffer); 244 245 void releaseCamera(); 246 247 private: 248 friend struct CameraSourceListener; 249 250 Mutex mLock; 251 Condition mFrameAvailableCondition; 252 Condition mFrameCompleteCondition; 253 List<sp<IMemory> > mFramesReceived; 254 List<sp<IMemory> > mFramesBeingEncoded; 255 List<int64_t> mFrameTimes; 256 257 int64_t mFirstFrameTimeUs; 258 int64_t mStopSystemTimeUs; 259 int32_t mNumFramesDropped; 260 int32_t mNumGlitches; 261 int64_t mGlitchDurationThresholdUs; 262 bool mCollectStats; 263 264 // The mode video buffers are received from camera. One of VIDEO_BUFFER_MODE_*. 265 int32_t mVideoBufferMode; 266 267 static const uint32_t kDefaultVideoBufferCount = 32; 268 269 /** 270 * The following variables are used in VIDEO_BUFFER_MODE_BUFFER_QUEUE mode. 271 */ 272 static const size_t kConsumerBufferCount = 8; 273 static const nsecs_t kMemoryBaseAvailableTimeoutNs = 200000000; // 200ms 274 // Consumer and producer of the buffer queue between this class and camera. 275 sp<BufferItemConsumer> mVideoBufferConsumer; 276 sp<IGraphicBufferProducer> mVideoBufferProducer; 277 // Memory used to send the buffers to encoder, where sp<IMemory> stores VideoNativeMetadata. 278 sp<IMemoryHeap> mMemoryHeapBase; 279 List<sp<IMemory>> mMemoryBases; 280 // The condition that will be signaled when there is an entry available in mMemoryBases. 281 Condition mMemoryBaseAvailableCond; 282 // A mapping from ANativeWindowBuffer sent to encoder to BufferItem received from camera. 283 // This is protected by mLock. 284 KeyedVector<ANativeWindowBuffer*, BufferItem> mReceivedBufferItemMap; 285 sp<BufferQueueListener> mBufferQueueListener; 286 287 Mutex mBatchLock; // protecting access to mInflightXXXXX members below 288 // Start of members protected by mBatchLock 289 std::deque<uint32_t> mInflightBatchSizes; 290 std::vector<native_handle_t*> mInflightReturnedHandles; 291 std::vector<const sp<IMemory>> mInflightReturnedMemorys; 292 // End of members protected by mBatchLock 293 294 void releaseQueuedFrames(); 295 void releaseOneRecordingFrame(const sp<IMemory>& frame); 296 void createVideoBufferMemoryHeap(size_t size, uint32_t bufferCount); 297 298 status_t init(const sp<hardware::ICamera>& camera, const sp<ICameraRecordingProxy>& proxy, 299 int32_t cameraId, const String16& clientName, uid_t clientUid, pid_t clientPid, 300 Size videoSize, int32_t frameRate, bool storeMetaDataInVideoBuffers); 301 302 status_t initWithCameraAccess( 303 const sp<hardware::ICamera>& camera, const sp<ICameraRecordingProxy>& proxy, 304 int32_t cameraId, const String16& clientName, uid_t clientUid, pid_t clientPid, 305 Size videoSize, int32_t frameRate, bool storeMetaDataInVideoBuffers); 306 307 // Initialize the buffer queue used in VIDEO_BUFFER_MODE_BUFFER_QUEUE mode. 308 status_t initBufferQueue(uint32_t width, uint32_t height, uint32_t format, 309 android_dataspace dataSpace, uint32_t bufferCount); 310 311 status_t isCameraAvailable(const sp<hardware::ICamera>& camera, 312 const sp<ICameraRecordingProxy>& proxy, 313 int32_t cameraId, 314 const String16& clientName, 315 uid_t clientUid, 316 pid_t clientPid); 317 318 status_t isCameraColorFormatSupported(const CameraParameters& params); 319 status_t configureCamera(CameraParameters* params, 320 int32_t width, int32_t height, 321 int32_t frameRate); 322 323 status_t checkVideoSize(const CameraParameters& params, 324 int32_t width, int32_t height); 325 326 status_t checkFrameRate(const CameraParameters& params, 327 int32_t frameRate); 328 329 // Check if this frame should be skipped based on the frame's timestamp in microsecond. 330 // mLock must be locked before calling this function. 331 bool shouldSkipFrameLocked(int64_t timestampUs); 332 333 void stopCameraRecording(); 334 status_t reset(); 335 336 CameraSource(const CameraSource &); 337 CameraSource &operator=(const CameraSource &); 338 }; 339 340 } // namespace android 341 342 #endif // CAMERA_SOURCE_H_ 343