Home | History | Annotate | Download | only in stagefright
      1 /*
      2  * Copyright (C) 2010 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_TIME_LAPSE_H_
     18 
     19 #define CAMERA_SOURCE_TIME_LAPSE_H_
     20 
     21 #include <pthread.h>
     22 
     23 #include <utils/RefBase.h>
     24 #include <utils/threads.h>
     25 #include <utils/String16.h>
     26 
     27 namespace android {
     28 
     29 namespace hardware {
     30 class ICamera;
     31 }
     32 
     33 class IMemory;
     34 class Camera;
     35 
     36 class CameraSourceTimeLapse : public CameraSource {
     37 public:
     38     static CameraSourceTimeLapse *CreateFromCamera(
     39         const sp<hardware::ICamera> &camera,
     40         const sp<ICameraRecordingProxy> &proxy,
     41         int32_t cameraId,
     42         const String16& clientName,
     43         uid_t clientUid,
     44         pid_t clientPid,
     45         Size videoSize,
     46         int32_t videoFrameRate,
     47         const sp<IGraphicBufferProducer>& surface,
     48         int64_t timeBetweenTimeLapseFrameCaptureUs,
     49         bool storeMetaDataInVideoBuffers = true);
     50 
     51     virtual ~CameraSourceTimeLapse();
     52 
     53     // If the frame capture interval is large, read will block for a long time.
     54     // Due to the way the mediaRecorder framework works, a stop() call from
     55     // mediaRecorder waits until the read returns, causing a long wait for
     56     // stop() to return. To avoid this, we can make read() return a copy of the
     57     // last read frame with the same time stamp frequently. This keeps the
     58     // read() call from blocking too long. Calling this function quickly
     59     // captures another frame, keeps its copy, and enables this mode of read()
     60     // returning quickly.
     61     void startQuickReadReturns();
     62 
     63 private:
     64     // size of the encoded video.
     65     int32_t mVideoWidth;
     66     int32_t mVideoHeight;
     67 
     68     // Time between two frames in final video (1/frameRate)
     69     int64_t mTimeBetweenTimeLapseVideoFramesUs;
     70 
     71     // Real timestamp of the last encoded time lapse frame
     72     int64_t mLastTimeLapseFrameRealTimestampUs;
     73 
     74     // Variable set in dataCallbackTimestamp() to help skipCurrentFrame()
     75     // to know if current frame needs to be skipped.
     76     bool mSkipCurrentFrame;
     77 
     78     // Lock for accessing mCameraIdle
     79     Mutex mCameraIdleLock;
     80 
     81     // Condition variable to wait on if camera is is not yet idle. Once the
     82     // camera gets idle, this variable will be signalled.
     83     Condition mCameraIdleCondition;
     84 
     85     // True if camera is in preview mode and ready for takePicture().
     86     // False after a call to takePicture() but before the final compressed
     87     // data callback has been called and preview has been restarted.
     88     volatile bool mCameraIdle;
     89 
     90     // True if stop() is waiting for camera to get idle, i.e. for the last
     91     // takePicture() to complete. This is needed so that dataCallbackTimestamp()
     92     // can return immediately.
     93     volatile bool mStopWaitingForIdleCamera;
     94 
     95     // Lock for accessing quick stop variables.
     96     Mutex mQuickStopLock;
     97 
     98     // mQuickStop is set to true if we use quick read() returns, otherwise it is set
     99     // to false. Once in this mode read() return a copy of the last read frame
    100     // with the same time stamp. See startQuickReadReturns().
    101     volatile bool mQuickStop;
    102 
    103     // Forces the next frame passed to dataCallbackTimestamp() to be read
    104     // as a time lapse frame. Used by startQuickReadReturns() so that the next
    105     // frame wakes up any blocking read.
    106     volatile bool mForceRead;
    107 
    108     // Stores a copy of the MediaBuffer read in the last read() call after
    109     // mQuickStop was true.
    110     MediaBufferBase* mLastReadBufferCopy;
    111 
    112     // Status code for last read.
    113     status_t mLastReadStatus;
    114 
    115     CameraSourceTimeLapse(
    116         const sp<hardware::ICamera> &camera,
    117         const sp<ICameraRecordingProxy> &proxy,
    118         int32_t cameraId,
    119         const String16& clientName,
    120         uid_t clientUid,
    121         pid_t clientPid,
    122         Size videoSize,
    123         int32_t videoFrameRate,
    124         const sp<IGraphicBufferProducer>& surface,
    125         int64_t timeBetweenTimeLapseFrameCaptureUs,
    126         bool storeMetaDataInVideoBuffers = true);
    127 
    128     // Wrapper over CameraSource::signalBufferReturned() to implement quick stop.
    129     // It only handles the case when mLastReadBufferCopy is signalled. Otherwise
    130     // it calls the base class' function.
    131     virtual void signalBufferReturned(MediaBufferBase* buffer);
    132 
    133     // Wrapper over CameraSource::read() to implement quick stop.
    134     virtual status_t read(MediaBufferBase **buffer, const ReadOptions *options = NULL);
    135 
    136     // mSkipCurrentFrame is set to true in dataCallbackTimestamp() if the current
    137     // frame needs to be skipped and this function just returns the value of mSkipCurrentFrame.
    138     virtual bool skipCurrentFrame(int64_t timestampUs);
    139 
    140     // In the video camera case calls skipFrameAndModifyTimeStamp() to modify
    141     // timestamp and set mSkipCurrentFrame.
    142     // Then it calls the base CameraSource::dataCallbackTimestamp()
    143     // This will be called in VIDEO_BUFFER_MODE_DATA_CALLBACK_YUV and
    144     // VIDEO_BUFFER_MODE_DATA_CALLBACK_METADATA mode.
    145     virtual void dataCallbackTimestamp(int64_t timestampUs, int32_t msgType,
    146             const sp<IMemory> &data);
    147 
    148     // In the video camera case calls skipFrameAndModifyTimeStamp() to modify
    149     // timestamp and set mSkipCurrentFrame.
    150     // Then it calls the base CameraSource::recordingFrameHandleCallbackTimestamp() or
    151     // CameraSource::recordingFrameHandleCallbackTimestampBatch()
    152     // This will be called in VIDEO_BUFFER_MODE_DATA_CALLBACK_METADATA mode when
    153     // the metadata is VideoNativeHandleMetadata.
    154     virtual void recordingFrameHandleCallbackTimestamp(int64_t timestampUs,
    155             native_handle_t* handle);
    156 
    157     // In the video camera case calls skipFrameAndModifyTimeStamp() to modify
    158     // timestamp and set mSkipCurrentFrame.
    159     // Then it calls the base CameraSource::recordingFrameHandleCallbackTimestamp() or
    160     // CameraSource::recordingFrameHandleCallbackTimestampBatch()
    161     // This will be called in VIDEO_BUFFER_MODE_DATA_CALLBACK_METADATA mode when
    162     // the metadata is VideoNativeHandleMetadata.
    163     virtual void recordingFrameHandleCallbackTimestampBatch(
    164             const std::vector<int64_t>& timestampsUs,
    165             const std::vector<native_handle_t*>& handles);
    166 
    167     // Process a buffer item received in CameraSource::BufferQueueListener.
    168     // This will be called in VIDEO_BUFFER_MODE_BUFFER_QUEUE mode.
    169     virtual void processBufferQueueFrame(BufferItem& buffer);
    170 
    171     // Convenience function to fill mLastReadBufferCopy from the just read
    172     // buffer.
    173     void fillLastReadBufferCopy(MediaBufferBase& sourceBuffer);
    174 
    175     // If the passed in size (width x height) is a supported video/preview size,
    176     // the function sets the camera's video/preview size to it and returns true.
    177     // Otherwise returns false.
    178     bool trySettingVideoSize(int32_t width, int32_t height);
    179 
    180     // When video camera is used for time lapse capture, returns true
    181     // until enough time has passed for the next time lapse frame. When
    182     // the frame needs to be encoded, it returns false and also modifies
    183     // the time stamp to be one frame time ahead of the last encoded
    184     // frame's time stamp.
    185     bool skipFrameAndModifyTimeStamp(int64_t *timestampUs);
    186 
    187     // Wrapper to enter threadTimeLapseEntry()
    188     static void *ThreadTimeLapseWrapper(void *me);
    189 
    190     // Creates a copy of source_data into a new memory of final type MemoryBase.
    191     sp<IMemory> createIMemoryCopy(const sp<IMemory> &source_data);
    192 
    193     CameraSourceTimeLapse(const CameraSourceTimeLapse &);
    194     CameraSourceTimeLapse &operator=(const CameraSourceTimeLapse &);
    195 };
    196 
    197 }  // namespace android
    198 
    199 #endif  // CAMERA_SOURCE_TIME_LAPSE_H_
    200