/* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef CAMERA_SOURCE_H_ #define CAMERA_SOURCE_H_ #include #include #include #include #include #include namespace android { class IMemory; class Camera; class Surface; class CameraSource : public MediaSource, public MediaBufferObserver { public: /** * Factory method to create a new CameraSource using the current * settings (such as video size, frame rate, color format, etc) * from the default camera. * * @return NULL on error. */ static CameraSource *Create(); /** * Factory method to create a new CameraSource. * * @param camera the video input frame data source. If it is NULL, * we will try to connect to the camera with the given * cameraId. * * @param cameraId the id of the camera that the source will connect * to if camera is NULL; otherwise ignored. * * @param videoSize the dimension (in pixels) of the video frame * @param frameRate the target frames per second * @param surface the preview surface for display where preview * frames are sent to * * @return NULL on error. */ static CameraSource *CreateFromCamera(const sp &camera, int32_t cameraId, Size videoSize, int32_t frameRate, const sp& surface); virtual ~CameraSource(); virtual status_t start(MetaData *params = NULL); virtual status_t stop(); /** * Check whether a CameraSource object is properly initialized. * Must call this method before stop(). * @return OK if initialization has successfully completed. */ virtual status_t initCheck() const; /** * Returns the MetaData associated with the CameraSource, * including: * kKeyColorFormat: YUV color format of the video frames * kKeyWidth, kKeyHeight: dimension (in pixels) of the video frames * kKeySampleRate: frame rate in frames per second * kKeyMIMEType: always fixed to be MEDIA_MIMETYPE_VIDEO_RAW */ virtual sp getFormat(); virtual status_t read( MediaBuffer **buffer, const ReadOptions *options = NULL); virtual void signalBufferReturned(MediaBuffer* buffer); protected: enum CameraFlags { FLAGS_SET_CAMERA = 1L << 0, FLAGS_HOT_CAMERA = 1L << 1, }; int32_t mCameraFlags; Size mVideoSize; int32_t mVideoFrameRate; int32_t mColorFormat; status_t mInitCheck; sp mCamera; sp mSurface; sp mMeta; int64_t mStartTimeUs; int32_t mNumFramesReceived; int64_t mLastFrameTimestampUs; bool mStarted; CameraSource(const sp& camera, int32_t cameraId, Size videoSize, int32_t frameRate, const sp& surface); virtual void startCameraRecording(); virtual void stopCameraRecording(); virtual void releaseRecordingFrame(const sp& frame); // Returns true if need to skip the current frame. // Called from dataCallbackTimestamp. virtual bool skipCurrentFrame(int64_t timestampUs) {return false;} // Callback called when still camera raw data is available. virtual void dataCallback(int32_t msgType, const sp &data) {} virtual void dataCallbackTimestamp(int64_t timestampUs, int32_t msgType, const sp &data); private: friend class CameraSourceListener; Mutex mLock; Condition mFrameAvailableCondition; Condition mFrameCompleteCondition; List > mFramesReceived; List > mFramesBeingEncoded; List mFrameTimes; int64_t mFirstFrameTimeUs; int32_t mNumFramesEncoded; int32_t mNumFramesDropped; int32_t mNumGlitches; int64_t mGlitchDurationThresholdUs; bool mCollectStats; void releaseQueuedFrames(); void releaseOneRecordingFrame(const sp& frame); status_t init(const sp& camera, int32_t cameraId, Size videoSize, int32_t frameRate); status_t isCameraAvailable(const sp& camera, int32_t cameraId); status_t isCameraColorFormatSupported(const CameraParameters& params); status_t configureCamera(CameraParameters* params, int32_t width, int32_t height, int32_t frameRate); status_t checkVideoSize(const CameraParameters& params, int32_t width, int32_t height); status_t checkFrameRate(const CameraParameters& params, int32_t frameRate); CameraSource(const CameraSource &); CameraSource &operator=(const CameraSource &); }; } // namespace android #endif // CAMERA_SOURCE_H_