CameraSource.h revision 54ff19ac69ace7c05ea90d225e26dab3b133f487
120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber/* 220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * Copyright (C) 2009 The Android Open Source Project 320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * 420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * Licensed under the Apache License, Version 2.0 (the "License"); 520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * you may not use this file except in compliance with the License. 620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * You may obtain a copy of the License at 720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * 820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * http://www.apache.org/licenses/LICENSE-2.0 920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * 1020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * Unless required by applicable law or agreed to in writing, software 1120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * distributed under the License is distributed on an "AS IS" BASIS, 1220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * See the License for the specific language governing permissions and 1420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * limitations under the License. 1520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber */ 1620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 1720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#ifndef CAMERA_SOURCE_H_ 1820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 1920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#define CAMERA_SOURCE_H_ 2020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 2120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#include <media/stagefright/MediaBuffer.h> 2220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#include <media/stagefright/MediaSource.h> 2354ff19ac69ace7c05ea90d225e26dab3b133f487James Dong#include <camera/ICamera.h> 2454ff19ac69ace7c05ea90d225e26dab3b133f487James Dong#include <camera/CameraParameters.h> 2520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#include <utils/List.h> 2620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#include <utils/RefBase.h> 2720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 2820111aa043c5f404472bc63b90bc5aad906b1101Andreas Hubernamespace android { 2920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 3020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberclass IMemory; 31be5c74f5da6a93b0d23f96e11848acfcc3b4d1d9Andreas Huberclass Camera; 3254ff19ac69ace7c05ea90d225e26dab3b133f487James Dongclass Surface; 3320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 347278cf32f1aa6a322f6dff1f8b7dacf7b6dddba6James Dongclass CameraSource : public MediaSource, public MediaBufferObserver { 3520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberpublic: 3654ff19ac69ace7c05ea90d225e26dab3b133f487James Dong /** 3754ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * Factory method to create a new CameraSource using the current 3854ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * settings (such as video size, frame rate, color format, etc) 3954ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * from the default camera. 4054ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * 4154ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * @return NULL on error. 4254ff19ac69ace7c05ea90d225e26dab3b133f487James Dong */ 4320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber static CameraSource *Create(); 4454ff19ac69ace7c05ea90d225e26dab3b133f487James Dong 4554ff19ac69ace7c05ea90d225e26dab3b133f487James Dong /** 4654ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * Factory method to create a new CameraSource. 4754ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * 4854ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * @param camera the video input frame data source. If it is NULL, 4954ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * we will try to connect to the camera with the given 5054ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * cameraId. 5154ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * 5254ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * @param cameraId the id of the camera that the source will connect 5354ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * to if camera is NULL; otherwise ignored. 5454ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * 5554ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * @param videoSize the dimension (in pixels) of the video frame 5654ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * @param frameRate the target frames per second 5754ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * @param surface the preview surface for display where preview 5854ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * frames are sent to 5954ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * 6054ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * @return NULL on error. 6154ff19ac69ace7c05ea90d225e26dab3b133f487James Dong */ 6254ff19ac69ace7c05ea90d225e26dab3b133f487James Dong static CameraSource *CreateFromCamera(const sp<ICamera> &camera, 6354ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t cameraId, 6454ff19ac69ace7c05ea90d225e26dab3b133f487James Dong Size videoSize, 6554ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t frameRate, 6654ff19ac69ace7c05ea90d225e26dab3b133f487James Dong const sp<Surface>& surface); 6720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 6820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber virtual ~CameraSource(); 6920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 7020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber virtual status_t start(MetaData *params = NULL); 7120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber virtual status_t stop(); 7220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 7354ff19ac69ace7c05ea90d225e26dab3b133f487James Dong /** 7454ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * Check whether a CameraSource object is properly initialized. 7554ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * Must call this method before stop(). 7654ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * @return OK if initialization has successfully completed. 7754ff19ac69ace7c05ea90d225e26dab3b133f487James Dong */ 7854ff19ac69ace7c05ea90d225e26dab3b133f487James Dong virtual status_t initCheck() const; 7954ff19ac69ace7c05ea90d225e26dab3b133f487James Dong 8054ff19ac69ace7c05ea90d225e26dab3b133f487James Dong /** 8154ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * Returns the MetaData associated with the CameraSource, 8254ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * including: 8354ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * kKeyColorFormat: YUV color format of the video frames 8454ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * kKeyWidth, kKeyHeight: dimension (in pixels) of the video frames 8554ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * kKeySampleRate: frame rate in frames per second 8654ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * kKeyMimeType: always fixed 8754ff19ac69ace7c05ea90d225e26dab3b133f487James Dong */ 8820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber virtual sp<MetaData> getFormat(); 8920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 9020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber virtual status_t read( 9120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber MediaBuffer **buffer, const ReadOptions *options = NULL); 9220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 937278cf32f1aa6a322f6dff1f8b7dacf7b6dddba6James Dong virtual void signalBufferReturned(MediaBuffer* buffer); 947278cf32f1aa6a322f6dff1f8b7dacf7b6dddba6James Dong 9565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatraprotected: 9654ff19ac69ace7c05ea90d225e26dab3b133f487James Dong enum CameraFlags { 9754ff19ac69ace7c05ea90d225e26dab3b133f487James Dong FLAGS_SET_CAMERA = 1L << 0, 9854ff19ac69ace7c05ea90d225e26dab3b133f487James Dong FLAGS_HOT_CAMERA = 1L << 1, 9954ff19ac69ace7c05ea90d225e26dab3b133f487James Dong }; 10054ff19ac69ace7c05ea90d225e26dab3b133f487James Dong 10154ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t mCameraFlags; 10254ff19ac69ace7c05ea90d225e26dab3b133f487James Dong Size mVideoSize; 10354ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t mVideoFrameRate; 10454ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t mColorFormat; 10554ff19ac69ace7c05ea90d225e26dab3b133f487James Dong status_t mInitCheck; 10654ff19ac69ace7c05ea90d225e26dab3b133f487James Dong 10754ff19ac69ace7c05ea90d225e26dab3b133f487James Dong sp<Camera> mCamera; 10854ff19ac69ace7c05ea90d225e26dab3b133f487James Dong sp<Surface> mSurface; 109653252be963c07c99109d20f942d1f30c52a9360James Dong sp<MetaData> mMeta; 11020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 11165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra int64_t mStartTimeUs; 11265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra int32_t mNumFramesReceived; 11365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra int64_t mLastFrameTimestampUs; 11465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra bool mStarted; 11565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 11654ff19ac69ace7c05ea90d225e26dab3b133f487James Dong CameraSource(const sp<ICamera>& camera, int32_t cameraId, 11754ff19ac69ace7c05ea90d225e26dab3b133f487James Dong Size videoSize, int32_t frameRate, 11854ff19ac69ace7c05ea90d225e26dab3b133f487James Dong const sp<Surface>& surface); 11965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 12065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra virtual void startCameraRecording(); 12165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra virtual void stopCameraRecording(); 12265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra virtual void releaseRecordingFrame(const sp<IMemory>& frame); 12365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 12465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // Returns true if need to skip the current frame. 12565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // Called from dataCallbackTimestamp. 12665e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra virtual bool skipCurrentFrame(int64_t timestampUs) {return false;} 12765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 12865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // Callback called when still camera raw data is available. 12965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra virtual void dataCallback(int32_t msgType, const sp<IMemory> &data) {} 13065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 13165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra virtual void dataCallbackTimestamp(int64_t timestampUs, int32_t msgType, 13265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra const sp<IMemory> &data); 13365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 13465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatraprivate: 13565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra friend class CameraSourceListener; 13665e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 13720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber Mutex mLock; 13820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber Condition mFrameAvailableCondition; 1397278cf32f1aa6a322f6dff1f8b7dacf7b6dddba6James Dong Condition mFrameCompleteCondition; 1407278cf32f1aa6a322f6dff1f8b7dacf7b6dddba6James Dong List<sp<IMemory> > mFramesReceived; 1417278cf32f1aa6a322f6dff1f8b7dacf7b6dddba6James Dong List<sp<IMemory> > mFramesBeingEncoded; 142be5c74f5da6a93b0d23f96e11848acfcc3b4d1d9Andreas Huber List<int64_t> mFrameTimes; 14320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 144be5c74f5da6a93b0d23f96e11848acfcc3b4d1d9Andreas Huber int64_t mFirstFrameTimeUs; 14513aec890216948b0c364f8f92792129d0335f506James Dong int32_t mNumFramesEncoded; 14613aec890216948b0c364f8f92792129d0335f506James Dong int32_t mNumFramesDropped; 147f60cafe0e6aad8f9ce54660fa88b651ae4e749e6James Dong int32_t mNumGlitches; 148f60cafe0e6aad8f9ce54660fa88b651ae4e749e6James Dong int64_t mGlitchDurationThresholdUs; 149365a963142093a1cd8efdcea76b5f65096a5b115James Dong bool mCollectStats; 150c32cd79d9ad4aba7d959b5b3be7361b4715e6f18James Dong 151c32cd79d9ad4aba7d959b5b3be7361b4715e6f18James Dong void releaseQueuedFrames(); 152f60cafe0e6aad8f9ce54660fa88b651ae4e749e6James Dong void releaseOneRecordingFrame(const sp<IMemory>& frame); 153be5c74f5da6a93b0d23f96e11848acfcc3b4d1d9Andreas Huber 15454ff19ac69ace7c05ea90d225e26dab3b133f487James Dong 15554ff19ac69ace7c05ea90d225e26dab3b133f487James Dong status_t init(const sp<ICamera>& camera, int32_t cameraId, 15654ff19ac69ace7c05ea90d225e26dab3b133f487James Dong Size videoSize, int32_t frameRate); 15754ff19ac69ace7c05ea90d225e26dab3b133f487James Dong status_t isCameraAvailable(const sp<ICamera>& camera, int32_t cameraId); 15854ff19ac69ace7c05ea90d225e26dab3b133f487James Dong status_t isCameraColorFormatSupported(const CameraParameters& params); 15954ff19ac69ace7c05ea90d225e26dab3b133f487James Dong status_t configureCamera(CameraParameters* params, 16054ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t width, int32_t height, 16154ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t frameRate); 16254ff19ac69ace7c05ea90d225e26dab3b133f487James Dong 16354ff19ac69ace7c05ea90d225e26dab3b133f487James Dong status_t checkVideoSize(const CameraParameters& params, 16454ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t width, int32_t height); 16554ff19ac69ace7c05ea90d225e26dab3b133f487James Dong 16654ff19ac69ace7c05ea90d225e26dab3b133f487James Dong status_t checkFrameRate(const CameraParameters& params, 16754ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t frameRate); 16854ff19ac69ace7c05ea90d225e26dab3b133f487James Dong 16920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber CameraSource(const CameraSource &); 17020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber CameraSource &operator=(const CameraSource &); 17120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}; 17220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 17320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber} // namespace android 17420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 17520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#endif // CAMERA_SOURCE_H_ 176