CameraSource.h revision 8fc3c670fff26cb21d1e16dbfcbc4410d7758574
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> 244ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Li#include <camera/ICameraRecordingProxyListener.h> 2554ff19ac69ace7c05ea90d225e26dab3b133f487James Dong#include <camera/CameraParameters.h> 268cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen#include <gui/BufferItemConsumer.h> 2720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#include <utils/List.h> 2820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#include <utils/RefBase.h> 29ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala#include <utils/String16.h> 3020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 3120111aa043c5f404472bc63b90bc5aad906b1101Andreas Hubernamespace android { 3220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 3320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberclass IMemory; 34be5c74f5da6a93b0d23f96e11848acfcc3b4d1d9Andreas Huberclass Camera; 3554ff19ac69ace7c05ea90d225e26dab3b133f487James Dongclass Surface; 3620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 377278cf32f1aa6a322f6dff1f8b7dacf7b6dddba6James Dongclass CameraSource : public MediaSource, public MediaBufferObserver { 3820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberpublic: 3954ff19ac69ace7c05ea90d225e26dab3b133f487James Dong /** 4054ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * Factory method to create a new CameraSource using the current 4154ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * settings (such as video size, frame rate, color format, etc) 4254ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * from the default camera. 4354ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * 44ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala * @param clientName The package/process name of the client application. 45ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala * This is used for permissions checking. 4654ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * @return NULL on error. 4754ff19ac69ace7c05ea90d225e26dab3b133f487James Dong */ 48ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala static CameraSource *Create(const String16 &clientName); 4954ff19ac69ace7c05ea90d225e26dab3b133f487James Dong 5054ff19ac69ace7c05ea90d225e26dab3b133f487James Dong /** 5154ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * Factory method to create a new CameraSource. 5254ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * 5354ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * @param camera the video input frame data source. If it is NULL, 5454ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * we will try to connect to the camera with the given 5554ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * cameraId. 5654ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * 5754ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * @param cameraId the id of the camera that the source will connect 5854ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * to if camera is NULL; otherwise ignored. 59ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala * @param clientName the package/process name of the camera-using 60ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala * application if camera is NULL; otherwise ignored. Used for 61ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala * permissions checking. 62ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala * @param clientUid the UID of the camera-using application if camera is 63ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala * NULL; otherwise ignored. Used for permissions checking. 6498a668f6ea51e4d894d2ebb61a0e18287fb14008Chien-Yu Chen * @param clientPid the PID of the camera-using application if camera is 6598a668f6ea51e4d894d2ebb61a0e18287fb14008Chien-Yu Chen * NULL; otherwise ignored. Used for permissions checking. 6654ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * @param videoSize the dimension (in pixels) of the video frame 6754ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * @param frameRate the target frames per second 6854ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * @param surface the preview surface for display where preview 6954ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * frames are sent to 705c9523154d106b555db6c41f85ab205a4f189b02James Dong * @param storeMetaDataInVideoBuffers true to request the camera 715c9523154d106b555db6c41f85ab205a4f189b02James Dong * source to store meta data in video buffers; false to 725c9523154d106b555db6c41f85ab205a4f189b02James Dong * request the camera source to store real YUV frame data 735c9523154d106b555db6c41f85ab205a4f189b02James Dong * in the video buffers. The camera source may not support 745c9523154d106b555db6c41f85ab205a4f189b02James Dong * storing meta data in video buffers, if so, a request 755c9523154d106b555db6c41f85ab205a4f189b02James Dong * to do that will NOT be honored. To find out whether 765c9523154d106b555db6c41f85ab205a4f189b02James Dong * meta data is actually being stored in video buffers 775c9523154d106b555db6c41f85ab205a4f189b02James Dong * during recording, call isMetaDataStoredInVideoBuffers(). 7854ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * 7954ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * @return NULL on error. 8054ff19ac69ace7c05ea90d225e26dab3b133f487James Dong */ 8154ff19ac69ace7c05ea90d225e26dab3b133f487James Dong static CameraSource *CreateFromCamera(const sp<ICamera> &camera, 824ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Li const sp<ICameraRecordingProxy> &proxy, 8354ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t cameraId, 84ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala const String16& clientName, 85ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala uid_t clientUid, 8698a668f6ea51e4d894d2ebb61a0e18287fb14008Chien-Yu Chen pid_t clientPid, 8754ff19ac69ace7c05ea90d225e26dab3b133f487James Dong Size videoSize, 8854ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t frameRate, 8999617adda9bc46c43f511f0940bc735c73de61deMathias Agopian const sp<IGraphicBufferProducer>& surface, 90d46a6b9fd8b2a4f9098757384711e2cd03a91651Eino-Ville Talvala bool storeMetaDataInVideoBuffers = true); 9120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 9220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber virtual ~CameraSource(); 9320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 9420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber virtual status_t start(MetaData *params = NULL); 95b44c9d2bdc0d5b9cb03254022a58e017b516e9e6James Dong virtual status_t stop() { return reset(); } 965c9523154d106b555db6c41f85ab205a4f189b02James Dong virtual status_t read( 975c9523154d106b555db6c41f85ab205a4f189b02James Dong MediaBuffer **buffer, const ReadOptions *options = NULL); 9820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 9954ff19ac69ace7c05ea90d225e26dab3b133f487James Dong /** 10054ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * Check whether a CameraSource object is properly initialized. 10154ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * Must call this method before stop(). 10254ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * @return OK if initialization has successfully completed. 10354ff19ac69ace7c05ea90d225e26dab3b133f487James Dong */ 10454ff19ac69ace7c05ea90d225e26dab3b133f487James Dong virtual status_t initCheck() const; 10554ff19ac69ace7c05ea90d225e26dab3b133f487James Dong 10654ff19ac69ace7c05ea90d225e26dab3b133f487James Dong /** 10754ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * Returns the MetaData associated with the CameraSource, 10854ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * including: 10954ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * kKeyColorFormat: YUV color format of the video frames 11054ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * kKeyWidth, kKeyHeight: dimension (in pixels) of the video frames 11154ff19ac69ace7c05ea90d225e26dab3b133f487James Dong * kKeySampleRate: frame rate in frames per second 1122b37ced30f89437c804c3945b901019b86d210aeJames Dong * kKeyMIMEType: always fixed to be MEDIA_MIMETYPE_VIDEO_RAW 11354ff19ac69ace7c05ea90d225e26dab3b133f487James Dong */ 11420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber virtual sp<MetaData> getFormat(); 11520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 1165c9523154d106b555db6c41f85ab205a4f189b02James Dong /** 1175c9523154d106b555db6c41f85ab205a4f189b02James Dong * Tell whether this camera source stores meta data or real YUV 1185c9523154d106b555db6c41f85ab205a4f189b02James Dong * frame data in video buffers. 1195c9523154d106b555db6c41f85ab205a4f189b02James Dong * 1205c9523154d106b555db6c41f85ab205a4f189b02James Dong * @return true if meta data is stored in the video 1215c9523154d106b555db6c41f85ab205a4f189b02James Dong * buffers; false if real YUV data is stored in 1225c9523154d106b555db6c41f85ab205a4f189b02James Dong * the video buffers. 1235c9523154d106b555db6c41f85ab205a4f189b02James Dong */ 1245c9523154d106b555db6c41f85ab205a4f189b02James Dong bool isMetaDataStoredInVideoBuffers() const; 12520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 1267278cf32f1aa6a322f6dff1f8b7dacf7b6dddba6James Dong virtual void signalBufferReturned(MediaBuffer* buffer); 1277278cf32f1aa6a322f6dff1f8b7dacf7b6dddba6James Dong 12865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatraprotected: 1298cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen 1308cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen /** 1318cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen * The class for listening to BnCameraRecordingProxyListener. This is used to receive video 1328cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen * buffers in VIDEO_BUFFER_MODE_DATA_CALLBACK_YUV and VIDEO_BUFFER_MODE_DATA_CALLBACK_METADATA 1338cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen * mode. When a frame is available, CameraSource::dataCallbackTimestamp() will be called. 1348cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen */ 1354ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Li class ProxyListener: public BnCameraRecordingProxyListener { 1364ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Li public: 1374ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Li ProxyListener(const sp<CameraSource>& source); 1384ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Li virtual void dataCallbackTimestamp(int64_t timestampUs, int32_t msgType, 1394ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Li const sp<IMemory> &data); 1404ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Li 1414ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Li private: 1424ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Li sp<CameraSource> mSource; 1434ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Li }; 1444ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Li 1458cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen /** 1468cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen * The class for listening to BufferQueue's onFrameAvailable. This is used to receive video 1478cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen * buffers in VIDEO_BUFFER_MODE_BUFFER_QUEUE mode. When a frame is available, 1488cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen * CameraSource::processBufferQueueFrame() will be called. 1498cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen */ 1508cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen class BufferQueueListener : public Thread, public BufferItemConsumer::FrameAvailableListener { 1518cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen public: 1528cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen BufferQueueListener(const sp<BufferItemConsumer> &consumer, 1538cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen const sp<CameraSource> &cameraSource); 1548cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen virtual void onFrameAvailable(const BufferItem& item); 1558cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen virtual bool threadLoop(); 1568cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen private: 1578cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen static const nsecs_t kFrameAvailableTimeout = 50000000; // 50ms 1588cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen 1598cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen sp<BufferItemConsumer> mConsumer; 1608cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen sp<CameraSource> mCameraSource; 1618cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen 1628cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen Mutex mLock; 1638cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen Condition mFrameAvailableSignal; 1648cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen bool mFrameAvailable; 1658cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen }; 1668cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen 1674ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Li // isBinderAlive needs linkToDeath to work. 1684ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Li class DeathNotifier: public IBinder::DeathRecipient { 1694ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Li public: 1704ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Li DeathNotifier() {} 1714ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Li virtual void binderDied(const wp<IBinder>& who); 1724ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Li }; 1734ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Li 17454ff19ac69ace7c05ea90d225e26dab3b133f487James Dong enum CameraFlags { 17554ff19ac69ace7c05ea90d225e26dab3b133f487James Dong FLAGS_SET_CAMERA = 1L << 0, 17654ff19ac69ace7c05ea90d225e26dab3b133f487James Dong FLAGS_HOT_CAMERA = 1L << 1, 17754ff19ac69ace7c05ea90d225e26dab3b133f487James Dong }; 17854ff19ac69ace7c05ea90d225e26dab3b133f487James Dong 17954ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t mCameraFlags; 18054ff19ac69ace7c05ea90d225e26dab3b133f487James Dong Size mVideoSize; 181983cf231ab2d176a14595cdae46ff1b0c239af47James Dong int32_t mNumInputBuffers; 18254ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t mVideoFrameRate; 18354ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t mColorFormat; 184d46a6b9fd8b2a4f9098757384711e2cd03a91651Eino-Ville Talvala int32_t mEncoderFormat; 185d46a6b9fd8b2a4f9098757384711e2cd03a91651Eino-Ville Talvala int32_t mEncoderDataSpace; 18654ff19ac69ace7c05ea90d225e26dab3b133f487James Dong status_t mInitCheck; 18754ff19ac69ace7c05ea90d225e26dab3b133f487James Dong 18854ff19ac69ace7c05ea90d225e26dab3b133f487James Dong sp<Camera> mCamera; 1894ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Li sp<ICameraRecordingProxy> mCameraRecordingProxy; 1904ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Li sp<DeathNotifier> mDeathNotifier; 19199617adda9bc46c43f511f0940bc735c73de61deMathias Agopian sp<IGraphicBufferProducer> mSurface; 192653252be963c07c99109d20f942d1f30c52a9360James Dong sp<MetaData> mMeta; 19320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 19465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra int64_t mStartTimeUs; 19565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra int32_t mNumFramesReceived; 19665e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra int64_t mLastFrameTimestampUs; 19765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra bool mStarted; 1987757f5010a771fb8824b6fdf9788f588a1577e3fJames Dong int32_t mNumFramesEncoded; 19965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 200e8e5f86e9e310b065596c8cbbca1543eb833dee1James Dong // Time between capture of two frames. 201e8e5f86e9e310b065596c8cbbca1543eb833dee1James Dong int64_t mTimeBetweenFrameCaptureUs; 202e8e5f86e9e310b065596c8cbbca1543eb833dee1James Dong 2034ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Li CameraSource(const sp<ICamera>& camera, const sp<ICameraRecordingProxy>& proxy, 20498a668f6ea51e4d894d2ebb61a0e18287fb14008Chien-Yu Chen int32_t cameraId, const String16& clientName, uid_t clientUid, pid_t clientPid, 20554ff19ac69ace7c05ea90d225e26dab3b133f487James Dong Size videoSize, int32_t frameRate, 20699617adda9bc46c43f511f0940bc735c73de61deMathias Agopian const sp<IGraphicBufferProducer>& surface, 2075c9523154d106b555db6c41f85ab205a4f189b02James Dong bool storeMetaDataInVideoBuffers); 20865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 20926cee964ab4e0a2bd6ae9ad199ba78ea9634421cRuben Brunk virtual status_t startCameraRecording(); 21065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra virtual void releaseRecordingFrame(const sp<IMemory>& frame); 21165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 21265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // Returns true if need to skip the current frame. 21365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // Called from dataCallbackTimestamp. 21465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra virtual bool skipCurrentFrame(int64_t timestampUs) {return false;} 21565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 21665e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // Callback called when still camera raw data is available. 21765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra virtual void dataCallback(int32_t msgType, const sp<IMemory> &data) {} 21865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 21965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra virtual void dataCallbackTimestamp(int64_t timestampUs, int32_t msgType, 22065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra const sp<IMemory> &data); 22165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 2228fc3c670fff26cb21d1e16dbfcbc4410d7758574Chien-Yu Chen // Process a buffer item received in BufferQueueListener. 2238fc3c670fff26cb21d1e16dbfcbc4410d7758574Chien-Yu Chen virtual void processBufferQueueFrame(BufferItem& buffer); 2248fc3c670fff26cb21d1e16dbfcbc4410d7758574Chien-Yu Chen 2259bb976e1c78048081cf9df4d8a1db67311413e5bRobert Shih void releaseCamera(); 2269bb976e1c78048081cf9df4d8a1db67311413e5bRobert Shih 22765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatraprivate: 228ee4e1b1a63758941460ae79a064249d3a5189443Lajos Molnar friend struct CameraSourceListener; 22965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 23020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber Mutex mLock; 23120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber Condition mFrameAvailableCondition; 2327278cf32f1aa6a322f6dff1f8b7dacf7b6dddba6James Dong Condition mFrameCompleteCondition; 2337278cf32f1aa6a322f6dff1f8b7dacf7b6dddba6James Dong List<sp<IMemory> > mFramesReceived; 2347278cf32f1aa6a322f6dff1f8b7dacf7b6dddba6James Dong List<sp<IMemory> > mFramesBeingEncoded; 235be5c74f5da6a93b0d23f96e11848acfcc3b4d1d9Andreas Huber List<int64_t> mFrameTimes; 23620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 237be5c74f5da6a93b0d23f96e11848acfcc3b4d1d9Andreas Huber int64_t mFirstFrameTimeUs; 23813aec890216948b0c364f8f92792129d0335f506James Dong int32_t mNumFramesDropped; 239f60cafe0e6aad8f9ce54660fa88b651ae4e749e6James Dong int32_t mNumGlitches; 240f60cafe0e6aad8f9ce54660fa88b651ae4e749e6James Dong int64_t mGlitchDurationThresholdUs; 241365a963142093a1cd8efdcea76b5f65096a5b115James Dong bool mCollectStats; 2428cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen 2438cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen // The mode video buffers are received from camera. One of VIDEO_BUFFER_MODE_*. 2448cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen int32_t mVideoBufferMode; 2458cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen 2468cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen /** 2478cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen * The following variables are used in VIDEO_BUFFER_MODE_BUFFER_QUEUE mode. 2488cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen */ 2498cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen static const size_t kConsumerBufferCount = 8; 2500419ba7b1c1bd4f19b2fab06234eacb4547a7c04Chien-Yu Chen static const nsecs_t kMemoryBaseAvailableTimeoutNs = 200000000; // 200ms 2518cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen // Consumer and producer of the buffer queue between this class and camera. 2528cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen sp<BufferItemConsumer> mVideoBufferConsumer; 2538cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen sp<IGraphicBufferProducer> mVideoBufferProducer; 2548cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen // Memory used to send the buffers to encoder, where sp<IMemory> stores VideoNativeMetadata. 2558cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen sp<IMemoryHeap> mMemoryHeapBase; 2568cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen List<sp<IMemory>> mMemoryBases; 2570419ba7b1c1bd4f19b2fab06234eacb4547a7c04Chien-Yu Chen // The condition that will be signaled when there is an entry available in mMemoryBases. 2580419ba7b1c1bd4f19b2fab06234eacb4547a7c04Chien-Yu Chen Condition mMemoryBaseAvailableCond; 2598cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen // A mapping from ANativeWindowBuffer sent to encoder to BufferItem received from camera. 2608cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen // This is protected by mLock. 2618cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen KeyedVector<ANativeWindowBuffer*, BufferItem> mReceivedBufferItemMap; 2628cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen sp<BufferQueueListener> mBufferQueueListener; 263c32cd79d9ad4aba7d959b5b3be7361b4715e6f18James Dong 264c32cd79d9ad4aba7d959b5b3be7361b4715e6f18James Dong void releaseQueuedFrames(); 265f60cafe0e6aad8f9ce54660fa88b651ae4e749e6James Dong void releaseOneRecordingFrame(const sp<IMemory>& frame); 26654ff19ac69ace7c05ea90d225e26dab3b133f487James Dong 2674ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Li status_t init(const sp<ICamera>& camera, const sp<ICameraRecordingProxy>& proxy, 26898a668f6ea51e4d894d2ebb61a0e18287fb14008Chien-Yu Chen int32_t cameraId, const String16& clientName, uid_t clientUid, pid_t clientPid, 269ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala Size videoSize, int32_t frameRate, bool storeMetaDataInVideoBuffers); 270ae4c1ac6401185539c03ce0819e174fd1b04b136James Dong 271ae4c1ac6401185539c03ce0819e174fd1b04b136James Dong status_t initWithCameraAccess( 272ae4c1ac6401185539c03ce0819e174fd1b04b136James Dong const sp<ICamera>& camera, const sp<ICameraRecordingProxy>& proxy, 27398a668f6ea51e4d894d2ebb61a0e18287fb14008Chien-Yu Chen int32_t cameraId, const String16& clientName, uid_t clientUid, pid_t clientPid, 274ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala Size videoSize, int32_t frameRate, bool storeMetaDataInVideoBuffers); 275ae4c1ac6401185539c03ce0819e174fd1b04b136James Dong 2768cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen // Initialize the buffer queue used in VIDEO_BUFFER_MODE_BUFFER_QUEUE mode. 2778cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen status_t initBufferQueue(uint32_t width, uint32_t height, uint32_t format, 2788cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen android_dataspace dataSpace, uint32_t bufferCount); 2798cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen 2804ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Li status_t isCameraAvailable(const sp<ICamera>& camera, 2814ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Li const sp<ICameraRecordingProxy>& proxy, 282ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala int32_t cameraId, 283ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala const String16& clientName, 28498a668f6ea51e4d894d2ebb61a0e18287fb14008Chien-Yu Chen uid_t clientUid, 28598a668f6ea51e4d894d2ebb61a0e18287fb14008Chien-Yu Chen pid_t clientPid); 286ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala 28754ff19ac69ace7c05ea90d225e26dab3b133f487James Dong status_t isCameraColorFormatSupported(const CameraParameters& params); 28854ff19ac69ace7c05ea90d225e26dab3b133f487James Dong status_t configureCamera(CameraParameters* params, 28954ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t width, int32_t height, 29054ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t frameRate); 29154ff19ac69ace7c05ea90d225e26dab3b133f487James Dong 29254ff19ac69ace7c05ea90d225e26dab3b133f487James Dong status_t checkVideoSize(const CameraParameters& params, 29354ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t width, int32_t height); 29454ff19ac69ace7c05ea90d225e26dab3b133f487James Dong 29554ff19ac69ace7c05ea90d225e26dab3b133f487James Dong status_t checkFrameRate(const CameraParameters& params, 29654ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t frameRate); 29754ff19ac69ace7c05ea90d225e26dab3b133f487James Dong 2988cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen // Check if this frame should be skipped based on the frame's timestamp in microsecond. 2998cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen // mLock must be locked before calling this function. 3008cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen bool shouldSkipFrameLocked(int64_t timestampUs); 3018cca0750a84c2d97224c0cfef7cf255308ee80b3Chien-Yu Chen 302b44c9d2bdc0d5b9cb03254022a58e017b516e9e6James Dong void stopCameraRecording(); 303b44c9d2bdc0d5b9cb03254022a58e017b516e9e6James Dong status_t reset(); 304ea7b485595f8cec6a66668b5c54c8f297d843f77James Dong 30520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber CameraSource(const CameraSource &); 30620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber CameraSource &operator=(const CameraSource &); 30720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}; 30820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 30920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber} // namespace android 31020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 31120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#endif // CAMERA_SOURCE_H_ 312