CameraSource.h revision 5c9523154d106b555db6c41f85ab205a4f189b02
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 595c9523154d106b555db6c41f85ab205a4f189b02James Dong * @param storeMetaDataInVideoBuffers true to request the camera 605c9523154d106b555db6c41f85ab205a4f189b02James Dong * source to store meta data in video buffers; false to 615c9523154d106b555db6c41f85ab205a4f189b02James Dong * request the camera source to store real YUV frame data 625c9523154d106b555db6c41f85ab205a4f189b02James Dong * in the video buffers. The camera source may not support 635c9523154d106b555db6c41f85ab205a4f189b02James Dong * storing meta data in video buffers, if so, a request 645c9523154d106b555db6c41f85ab205a4f189b02James Dong * to do that will NOT be honored. To find out whether 655c9523154d106b555db6c41f85ab205a4f189b02James Dong * meta data is actually being stored in video buffers 665c9523154d106b555db6c41f85ab205a4f189b02James Dong * during recording, call isMetaDataStoredInVideoBuffers(). 6754ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * 6854ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * @return NULL on error. 6954ff19ac69ace7c05ea90d225e26dab3b133f487James Dong */ 7054ff19ac69ace7c05ea90d225e26dab3b133f487James Dong static CameraSource *CreateFromCamera(const sp<ICamera> &camera, 7154ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t cameraId, 7254ff19ac69ace7c05ea90d225e26dab3b133f487James Dong Size videoSize, 7354ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t frameRate, 745c9523154d106b555db6c41f85ab205a4f189b02James Dong const sp<Surface>& surface, 755c9523154d106b555db6c41f85ab205a4f189b02James Dong bool storeMetaDataInVideoBuffers = false); 7620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 7720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber virtual ~CameraSource(); 7820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 7920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber virtual status_t start(MetaData *params = NULL); 8020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber virtual status_t stop(); 815c9523154d106b555db6c41f85ab205a4f189b02James Dong virtual status_t read( 825c9523154d106b555db6c41f85ab205a4f189b02James Dong MediaBuffer **buffer, const ReadOptions *options = NULL); 8320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 8454ff19ac69ace7c05ea90d225e26dab3b133f487James Dong /** 8554ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * Check whether a CameraSource object is properly initialized. 8654ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * Must call this method before stop(). 8754ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * @return OK if initialization has successfully completed. 8854ff19ac69ace7c05ea90d225e26dab3b133f487James Dong */ 8954ff19ac69ace7c05ea90d225e26dab3b133f487James Dong virtual status_t initCheck() const; 9054ff19ac69ace7c05ea90d225e26dab3b133f487James Dong 9154ff19ac69ace7c05ea90d225e26dab3b133f487James Dong /** 9254ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * Returns the MetaData associated with the CameraSource, 9354ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * including: 9454ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * kKeyColorFormat: YUV color format of the video frames 9554ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * kKeyWidth, kKeyHeight: dimension (in pixels) of the video frames 9654ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * kKeySampleRate: frame rate in frames per second 972b37ced30f89437c804c3945b901019b86d210aeJames Dong * kKeyMIMEType: always fixed to be MEDIA_MIMETYPE_VIDEO_RAW 9854ff19ac69ace7c05ea90d225e26dab3b133f487James Dong */ 9920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber virtual sp<MetaData> getFormat(); 10020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 1015c9523154d106b555db6c41f85ab205a4f189b02James Dong /** 1025c9523154d106b555db6c41f85ab205a4f189b02James Dong * Retrieve the total number of video buffers available from 1035c9523154d106b555db6c41f85ab205a4f189b02James Dong * this source. 1045c9523154d106b555db6c41f85ab205a4f189b02James Dong * 1055c9523154d106b555db6c41f85ab205a4f189b02James Dong * This method is useful if these video buffers are used 1065c9523154d106b555db6c41f85ab205a4f189b02James Dong * for passing video frame data to other media components, 1075c9523154d106b555db6c41f85ab205a4f189b02James Dong * such as OMX video encoders, in order to eliminate the 1085c9523154d106b555db6c41f85ab205a4f189b02James Dong * memcpy of the data. 1095c9523154d106b555db6c41f85ab205a4f189b02James Dong * 1105c9523154d106b555db6c41f85ab205a4f189b02James Dong * @return the total numbner of video buffers. Returns 0 to 1115c9523154d106b555db6c41f85ab205a4f189b02James Dong * indicate that this source does not make the video 1125c9523154d106b555db6c41f85ab205a4f189b02James Dong * buffer information availalble. 1135c9523154d106b555db6c41f85ab205a4f189b02James Dong */ 1145c9523154d106b555db6c41f85ab205a4f189b02James Dong size_t getNumberOfVideoBuffers() const; 1155c9523154d106b555db6c41f85ab205a4f189b02James Dong 1165c9523154d106b555db6c41f85ab205a4f189b02James Dong /** 1175c9523154d106b555db6c41f85ab205a4f189b02James Dong * Retrieve the individual video buffer available from 1185c9523154d106b555db6c41f85ab205a4f189b02James Dong * this source. 1195c9523154d106b555db6c41f85ab205a4f189b02James Dong * 1205c9523154d106b555db6c41f85ab205a4f189b02James Dong * @param index the index corresponding to the video buffer. 1215c9523154d106b555db6c41f85ab205a4f189b02James Dong * Valid range of the index is [0, n], where n = 1225c9523154d106b555db6c41f85ab205a4f189b02James Dong * getNumberOfVideoBuffers() - 1. 1235c9523154d106b555db6c41f85ab205a4f189b02James Dong * 1245c9523154d106b555db6c41f85ab205a4f189b02James Dong * @return the video buffer corresponding to the given index. 1255c9523154d106b555db6c41f85ab205a4f189b02James Dong * If index is out of range, 0 should be returned. 1265c9523154d106b555db6c41f85ab205a4f189b02James Dong */ 1275c9523154d106b555db6c41f85ab205a4f189b02James Dong sp<IMemory> getVideoBuffer(size_t index) const; 1285c9523154d106b555db6c41f85ab205a4f189b02James Dong 1295c9523154d106b555db6c41f85ab205a4f189b02James Dong /** 1305c9523154d106b555db6c41f85ab205a4f189b02James Dong * Tell whether this camera source stores meta data or real YUV 1315c9523154d106b555db6c41f85ab205a4f189b02James Dong * frame data in video buffers. 1325c9523154d106b555db6c41f85ab205a4f189b02James Dong * 1335c9523154d106b555db6c41f85ab205a4f189b02James Dong * @return true if meta data is stored in the video 1345c9523154d106b555db6c41f85ab205a4f189b02James Dong * buffers; false if real YUV data is stored in 1355c9523154d106b555db6c41f85ab205a4f189b02James Dong * the video buffers. 1365c9523154d106b555db6c41f85ab205a4f189b02James Dong */ 1375c9523154d106b555db6c41f85ab205a4f189b02James Dong bool isMetaDataStoredInVideoBuffers() const; 13820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 1397278cf32f1aa6a322f6dff1f8b7dacf7b6dddba6James Dong virtual void signalBufferReturned(MediaBuffer* buffer); 1407278cf32f1aa6a322f6dff1f8b7dacf7b6dddba6James Dong 14165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatraprotected: 14254ff19ac69ace7c05ea90d225e26dab3b133f487James Dong enum CameraFlags { 14354ff19ac69ace7c05ea90d225e26dab3b133f487James Dong FLAGS_SET_CAMERA = 1L << 0, 14454ff19ac69ace7c05ea90d225e26dab3b133f487James Dong FLAGS_HOT_CAMERA = 1L << 1, 14554ff19ac69ace7c05ea90d225e26dab3b133f487James Dong }; 14654ff19ac69ace7c05ea90d225e26dab3b133f487James Dong 14754ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t mCameraFlags; 14854ff19ac69ace7c05ea90d225e26dab3b133f487James Dong Size mVideoSize; 14954ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t mVideoFrameRate; 15054ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t mColorFormat; 15154ff19ac69ace7c05ea90d225e26dab3b133f487James Dong status_t mInitCheck; 15254ff19ac69ace7c05ea90d225e26dab3b133f487James Dong 15354ff19ac69ace7c05ea90d225e26dab3b133f487James Dong sp<Camera> mCamera; 15454ff19ac69ace7c05ea90d225e26dab3b133f487James Dong sp<Surface> mSurface; 155653252be963c07c99109d20f942d1f30c52a9360James Dong sp<MetaData> mMeta; 15620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 15765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra int64_t mStartTimeUs; 15865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra int32_t mNumFramesReceived; 15965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra int64_t mLastFrameTimestampUs; 16065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra bool mStarted; 16165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 16254ff19ac69ace7c05ea90d225e26dab3b133f487James Dong CameraSource(const sp<ICamera>& camera, int32_t cameraId, 16354ff19ac69ace7c05ea90d225e26dab3b133f487James Dong Size videoSize, int32_t frameRate, 1645c9523154d106b555db6c41f85ab205a4f189b02James Dong const sp<Surface>& surface, 1655c9523154d106b555db6c41f85ab205a4f189b02James Dong bool storeMetaDataInVideoBuffers); 16665e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 16765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra virtual void startCameraRecording(); 16865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra virtual void stopCameraRecording(); 16965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra virtual void releaseRecordingFrame(const sp<IMemory>& frame); 17065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 17165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // Returns true if need to skip the current frame. 17265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // Called from dataCallbackTimestamp. 17365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra virtual bool skipCurrentFrame(int64_t timestampUs) {return false;} 17465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 17565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // Callback called when still camera raw data is available. 17665e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra virtual void dataCallback(int32_t msgType, const sp<IMemory> &data) {} 17765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 17865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra virtual void dataCallbackTimestamp(int64_t timestampUs, int32_t msgType, 17965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra const sp<IMemory> &data); 18065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 18165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatraprivate: 18265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra friend class CameraSourceListener; 18365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 18420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber Mutex mLock; 18520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber Condition mFrameAvailableCondition; 1867278cf32f1aa6a322f6dff1f8b7dacf7b6dddba6James Dong Condition mFrameCompleteCondition; 1877278cf32f1aa6a322f6dff1f8b7dacf7b6dddba6James Dong List<sp<IMemory> > mFramesReceived; 1887278cf32f1aa6a322f6dff1f8b7dacf7b6dddba6James Dong List<sp<IMemory> > mFramesBeingEncoded; 189be5c74f5da6a93b0d23f96e11848acfcc3b4d1d9Andreas Huber List<int64_t> mFrameTimes; 19020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 191be5c74f5da6a93b0d23f96e11848acfcc3b4d1d9Andreas Huber int64_t mFirstFrameTimeUs; 19213aec890216948b0c364f8f92792129d0335f506James Dong int32_t mNumFramesEncoded; 19313aec890216948b0c364f8f92792129d0335f506James Dong int32_t mNumFramesDropped; 194f60cafe0e6aad8f9ce54660fa88b651ae4e749e6James Dong int32_t mNumGlitches; 195f60cafe0e6aad8f9ce54660fa88b651ae4e749e6James Dong int64_t mGlitchDurationThresholdUs; 196365a963142093a1cd8efdcea76b5f65096a5b115James Dong bool mCollectStats; 1975c9523154d106b555db6c41f85ab205a4f189b02James Dong bool mIsMetaDataStoredInVideoBuffers; 198c32cd79d9ad4aba7d959b5b3be7361b4715e6f18James Dong 199c32cd79d9ad4aba7d959b5b3be7361b4715e6f18James Dong void releaseQueuedFrames(); 200f60cafe0e6aad8f9ce54660fa88b651ae4e749e6James Dong void releaseOneRecordingFrame(const sp<IMemory>& frame); 201be5c74f5da6a93b0d23f96e11848acfcc3b4d1d9Andreas Huber 20254ff19ac69ace7c05ea90d225e26dab3b133f487James Dong 20354ff19ac69ace7c05ea90d225e26dab3b133f487James Dong status_t init(const sp<ICamera>& camera, int32_t cameraId, 2045c9523154d106b555db6c41f85ab205a4f189b02James Dong Size videoSize, int32_t frameRate, 2055c9523154d106b555db6c41f85ab205a4f189b02James Dong bool storeMetaDataInVideoBuffers); 20654ff19ac69ace7c05ea90d225e26dab3b133f487James Dong status_t isCameraAvailable(const sp<ICamera>& camera, int32_t cameraId); 20754ff19ac69ace7c05ea90d225e26dab3b133f487James Dong status_t isCameraColorFormatSupported(const CameraParameters& params); 20854ff19ac69ace7c05ea90d225e26dab3b133f487James Dong status_t configureCamera(CameraParameters* params, 20954ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t width, int32_t height, 21054ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t frameRate); 21154ff19ac69ace7c05ea90d225e26dab3b133f487James Dong 21254ff19ac69ace7c05ea90d225e26dab3b133f487James Dong status_t checkVideoSize(const CameraParameters& params, 21354ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t width, int32_t height); 21454ff19ac69ace7c05ea90d225e26dab3b133f487James Dong 21554ff19ac69ace7c05ea90d225e26dab3b133f487James Dong status_t checkFrameRate(const CameraParameters& params, 21654ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t frameRate); 21754ff19ac69ace7c05ea90d225e26dab3b133f487James Dong 21820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber CameraSource(const CameraSource &); 21920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber CameraSource &operator=(const CameraSource &); 22020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}; 22120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 22220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber} // namespace android 22320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 22420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#endif // CAMERA_SOURCE_H_ 225