Home | History | Annotate | Download | only in stagefright
      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 <media/stagefright/MediaBuffer.h>
     22 #include <media/stagefright/MediaSource.h>
     23 #include <camera/ICamera.h>
     24 #include <camera/ICameraRecordingProxyListener.h>
     25 #include <camera/CameraParameters.h>
     26 #include <utils/List.h>
     27 #include <utils/RefBase.h>
     28 #include <utils/String16.h>
     29 
     30 namespace android {
     31 
     32 class IMemory;
     33 class Camera;
     34 class Surface;
     35 
     36 class CameraSource : public MediaSource, public MediaBufferObserver {
     37 public:
     38     /**
     39      * Factory method to create a new CameraSource using the current
     40      * settings (such as video size, frame rate, color format, etc)
     41      * from the default camera.
     42      *
     43      * @param clientName The package/process name of the client application.
     44      *    This is used for permissions checking.
     45      * @return NULL on error.
     46      */
     47     static CameraSource *Create(const String16 &clientName);
     48 
     49     /**
     50      * Factory method to create a new CameraSource.
     51      *
     52      * @param camera the video input frame data source. If it is NULL,
     53      *          we will try to connect to the camera with the given
     54      *          cameraId.
     55      *
     56      * @param cameraId the id of the camera that the source will connect
     57      *          to if camera is NULL; otherwise ignored.
     58      * @param clientName the package/process name of the camera-using
     59      *          application if camera is NULL; otherwise ignored. Used for
     60      *          permissions checking.
     61      * @param clientUid the UID of the camera-using application if camera is
     62      *          NULL; otherwise ignored. Used for permissions checking.
     63      * @param videoSize the dimension (in pixels) of the video frame
     64      * @param frameRate the target frames per second
     65      * @param surface the preview surface for display where preview
     66      *          frames are sent to
     67      * @param storeMetaDataInVideoBuffers true to request the camera
     68      *          source to store meta data in video buffers; false to
     69      *          request the camera source to store real YUV frame data
     70      *          in the video buffers. The camera source may not support
     71      *          storing meta data in video buffers, if so, a request
     72      *          to do that will NOT be honored. To find out whether
     73      *          meta data is actually being stored in video buffers
     74      *          during recording, call isMetaDataStoredInVideoBuffers().
     75      *
     76      * @return NULL on error.
     77      */
     78     static CameraSource *CreateFromCamera(const sp<ICamera> &camera,
     79                                           const sp<ICameraRecordingProxy> &proxy,
     80                                           int32_t cameraId,
     81                                           const String16& clientName,
     82                                           uid_t clientUid,
     83                                           Size videoSize,
     84                                           int32_t frameRate,
     85                                           const sp<IGraphicBufferProducer>& surface,
     86                                           bool storeMetaDataInVideoBuffers = false);
     87 
     88     virtual ~CameraSource();
     89 
     90     virtual status_t start(MetaData *params = NULL);
     91     virtual status_t stop() { return reset(); }
     92     virtual status_t read(
     93             MediaBuffer **buffer, const ReadOptions *options = NULL);
     94 
     95     /**
     96      * Check whether a CameraSource object is properly initialized.
     97      * Must call this method before stop().
     98      * @return OK if initialization has successfully completed.
     99      */
    100     virtual status_t initCheck() const;
    101 
    102     /**
    103      * Returns the MetaData associated with the CameraSource,
    104      * including:
    105      * kKeyColorFormat: YUV color format of the video frames
    106      * kKeyWidth, kKeyHeight: dimension (in pixels) of the video frames
    107      * kKeySampleRate: frame rate in frames per second
    108      * kKeyMIMEType: always fixed to be MEDIA_MIMETYPE_VIDEO_RAW
    109      */
    110     virtual sp<MetaData> getFormat();
    111 
    112     /**
    113      * Tell whether this camera source stores meta data or real YUV
    114      * frame data in video buffers.
    115      *
    116      * @return true if meta data is stored in the video
    117      *      buffers; false if real YUV data is stored in
    118      *      the video buffers.
    119      */
    120     bool isMetaDataStoredInVideoBuffers() const;
    121 
    122     virtual void signalBufferReturned(MediaBuffer* buffer);
    123 
    124 protected:
    125     class ProxyListener: public BnCameraRecordingProxyListener {
    126     public:
    127         ProxyListener(const sp<CameraSource>& source);
    128         virtual void dataCallbackTimestamp(int64_t timestampUs, int32_t msgType,
    129                 const sp<IMemory> &data);
    130 
    131     private:
    132         sp<CameraSource> mSource;
    133     };
    134 
    135     // isBinderAlive needs linkToDeath to work.
    136     class DeathNotifier: public IBinder::DeathRecipient {
    137     public:
    138         DeathNotifier() {}
    139         virtual void binderDied(const wp<IBinder>& who);
    140     };
    141 
    142     enum CameraFlags {
    143         FLAGS_SET_CAMERA = 1L << 0,
    144         FLAGS_HOT_CAMERA = 1L << 1,
    145     };
    146 
    147     int32_t  mCameraFlags;
    148     Size     mVideoSize;
    149     int32_t  mNumInputBuffers;
    150     int32_t  mVideoFrameRate;
    151     int32_t  mColorFormat;
    152     status_t mInitCheck;
    153 
    154     sp<Camera>   mCamera;
    155     sp<ICameraRecordingProxy>   mCameraRecordingProxy;
    156     sp<DeathNotifier> mDeathNotifier;
    157     sp<IGraphicBufferProducer>  mSurface;
    158     sp<MetaData> mMeta;
    159 
    160     int64_t mStartTimeUs;
    161     int32_t mNumFramesReceived;
    162     int64_t mLastFrameTimestampUs;
    163     bool mStarted;
    164     int32_t mNumFramesEncoded;
    165 
    166     // Time between capture of two frames.
    167     int64_t mTimeBetweenFrameCaptureUs;
    168 
    169     CameraSource(const sp<ICamera>& camera, const sp<ICameraRecordingProxy>& proxy,
    170                  int32_t cameraId, const String16& clientName, uid_t clientUid,
    171                  Size videoSize, int32_t frameRate,
    172                  const sp<IGraphicBufferProducer>& surface,
    173                  bool storeMetaDataInVideoBuffers);
    174 
    175     virtual void startCameraRecording();
    176     virtual void releaseRecordingFrame(const sp<IMemory>& frame);
    177 
    178     // Returns true if need to skip the current frame.
    179     // Called from dataCallbackTimestamp.
    180     virtual bool skipCurrentFrame(int64_t timestampUs) {return false;}
    181 
    182     // Callback called when still camera raw data is available.
    183     virtual void dataCallback(int32_t msgType, const sp<IMemory> &data) {}
    184 
    185     virtual void dataCallbackTimestamp(int64_t timestampUs, int32_t msgType,
    186             const sp<IMemory> &data);
    187 
    188 private:
    189     friend class CameraSourceListener;
    190 
    191     Mutex mLock;
    192     Condition mFrameAvailableCondition;
    193     Condition mFrameCompleteCondition;
    194     List<sp<IMemory> > mFramesReceived;
    195     List<sp<IMemory> > mFramesBeingEncoded;
    196     List<int64_t> mFrameTimes;
    197 
    198     int64_t mFirstFrameTimeUs;
    199     int32_t mNumFramesDropped;
    200     int32_t mNumGlitches;
    201     int64_t mGlitchDurationThresholdUs;
    202     bool mCollectStats;
    203     bool mIsMetaDataStoredInVideoBuffers;
    204 
    205     void releaseQueuedFrames();
    206     void releaseOneRecordingFrame(const sp<IMemory>& frame);
    207 
    208 
    209     status_t init(const sp<ICamera>& camera, const sp<ICameraRecordingProxy>& proxy,
    210                   int32_t cameraId, const String16& clientName, uid_t clientUid,
    211                   Size videoSize, int32_t frameRate, bool storeMetaDataInVideoBuffers);
    212 
    213     status_t initWithCameraAccess(
    214                   const sp<ICamera>& camera, const sp<ICameraRecordingProxy>& proxy,
    215                   int32_t cameraId, const String16& clientName, uid_t clientUid,
    216                   Size videoSize, int32_t frameRate, bool storeMetaDataInVideoBuffers);
    217 
    218     status_t isCameraAvailable(const sp<ICamera>& camera,
    219                                const sp<ICameraRecordingProxy>& proxy,
    220                                int32_t cameraId,
    221                                const String16& clientName,
    222                                uid_t clientUid);
    223 
    224     status_t isCameraColorFormatSupported(const CameraParameters& params);
    225     status_t configureCamera(CameraParameters* params,
    226                     int32_t width, int32_t height,
    227                     int32_t frameRate);
    228 
    229     status_t checkVideoSize(const CameraParameters& params,
    230                     int32_t width, int32_t height);
    231 
    232     status_t checkFrameRate(const CameraParameters& params,
    233                     int32_t frameRate);
    234 
    235     void stopCameraRecording();
    236     void releaseCamera();
    237     status_t reset();
    238 
    239     CameraSource(const CameraSource &);
    240     CameraSource &operator=(const CameraSource &);
    241 };
    242 
    243 }  // namespace android
    244 
    245 #endif  // CAMERA_SOURCE_H_
    246