Home | History | Annotate | Download | only in test
      1 /* Copyright (c) 2012-2014, The Linux Foundataion. All rights reserved.
      2  *
      3  * Redistribution and use in source and binary forms, with or without
      4  * modification, are permitted provided that the following conditions are
      5  * met:
      6  *     * Redistributions of source code must retain the above copyright
      7  *       notice, this list of conditions and the following disclaimer.
      8  *     * Redistributions in binary form must reproduce the above
      9  *       copyright notice, this list of conditions and the following
     10  *       disclaimer in the documentation and/or other materials provided
     11  *       with the distribution.
     12  *     * Neither the name of The Linux Foundation nor the names of its
     13  *       contributors may be used to endorse or promote products derived
     14  *       from this software without specific prior written permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
     17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
     23  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     25  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  *
     28  */
     29 
     30 #ifndef QCAMERA_TEST_H
     31 #define QCAMERA_TEST_H
     32 
     33 #include <SkData.h>
     34 #include <SkBitmap.h>
     35 #include <SkStream.h>
     36 
     37 namespace qcamera {
     38 
     39 using namespace android;
     40 
     41 class TestContext;
     42 
     43 class CameraContext : public CameraListener,
     44     public ICameraRecordingProxyListener{
     45 public:
     46     typedef enum {
     47         READ_METADATA = 1,
     48         READ_IMAGE = 2,
     49         READ_ALL = 3
     50     } ReadMode_t;
     51 
     52     // This structure is used to store jpeg file sections in memory.
     53     typedef struct {
     54         unsigned char *  Data;
     55         int      Type;
     56         unsigned Size;
     57     } Sections_t;
     58 
     59 public:
     60 
     61     CameraContext(int cameraIndex);
     62     virtual ~CameraContext();
     63 
     64 
     65 
     66     status_t openCamera();
     67     status_t closeCamera();
     68 
     69     status_t startPreview();
     70     status_t stopPreview();
     71     status_t resumePreview();
     72     status_t autoFocus();
     73     status_t enablePreviewCallbacks();
     74     status_t takePicture();
     75     status_t startRecording();
     76     status_t stopRecording();
     77     status_t startViVRecording();
     78     status_t stopViVRecording();
     79     status_t configureViVRecording();
     80 
     81     status_t nextPreviewSize();
     82     status_t setPreviewSize(const char *format);
     83     status_t getCurrentPreviewSize(Size &previewSize);
     84 
     85     status_t nextPictureSize();
     86     status_t getCurrentPictureSize(Size &pictureSize);
     87     status_t setPictureSize(const char *format);
     88 
     89     status_t nextVideoSize();
     90     status_t setVideoSize(const char *format);
     91     status_t getCurrentVideoSize(Size &videoSize);
     92     status_t configureRecorder();
     93     status_t unconfigureRecorder();
     94     Sections_t *FindSection(int SectionType);
     95     status_t ReadSectionsFromBuffer (unsigned char *buffer,
     96         unsigned int buffer_size, ReadMode_t ReadMode);
     97     virtual IBinder* onAsBinder();
     98     void setTestCtxInstance(TestContext *instance);
     99 
    100     void printMenu(sp<CameraContext> currentCamera);
    101     void printSupportedParams();
    102 
    103 
    104     int getCameraIndex() { return mCameraIndex; }
    105     int getNumberOfCameras();
    106     void enablePrintPreview();
    107     void disablePrintPreview();
    108     void enablePiPCapture();
    109     void disablePiPCapture();
    110     void CheckSectionsAllocated();
    111     void DiscardData();
    112     void DiscardSections();
    113     size_t calcBufferSize(int width, int height);
    114     size_t calcStride(int width);
    115     size_t calcYScanLines(int height);
    116     size_t calcUVScanLines(int height);
    117 
    118     virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2);
    119     virtual void postData(int32_t msgType,
    120             const sp<IMemory>& dataPtr,
    121             camera_frame_metadata_t *metadata);
    122 
    123     virtual void postDataTimestamp(nsecs_t timestamp,
    124             int32_t msgType,
    125             const sp<IMemory>& dataPtr);
    126     virtual void dataCallbackTimestamp(nsecs_t timestamp,
    127             int32_t msgType,
    128             const sp<IMemory>& dataPtr);
    129 
    130 private:
    131 
    132     status_t createPreviewSurface(unsigned int width,
    133             unsigned int height,
    134             int32_t pixFormat);
    135     status_t destroyPreviewSurface();
    136 
    137     status_t saveFile(const sp<IMemory>& mem, String8 path);
    138     SkBitmap * PiPCopyToOneFile(SkBitmap *bitmap0, SkBitmap *bitmap1);
    139     SkBitmap *decodeJPEG(const sp<IMemory>& mem);
    140     status_t encodeJPEG(SkWStream * stream, const SkBitmap *bitmap,
    141         String8 path);
    142     void previewCallback(const sp<IMemory>& mem);
    143 
    144     static int JpegIdx;
    145     int mCameraIndex;
    146     bool mResizePreview;
    147     bool mHardwareActive;
    148     bool mPreviewRunning;
    149     bool mRecordRunning;
    150     int  mVideoFd;
    151     int  mVideoIdx;
    152     bool mRecordingHint;
    153     bool mDoPrintMenu;
    154     bool mPiPCapture;
    155     static int mPiPIdx;
    156     int mfmtMultiplier;
    157     int mWidthTmp[2];
    158     int mHeightTmp[2];
    159     int mSectionsRead;
    160     int mSectionsAllocated;
    161     Sections_t * mSections;
    162     Sections_t * mJEXIFTmp;
    163     Sections_t mJEXIFSection;
    164     int mHaveAll;
    165     TestContext *mInterpr;
    166     Mutex mViVLock;
    167     Condition mViVCond;
    168     bool mViVinUse;
    169 
    170 
    171     sp<Camera> mCamera;
    172     sp<SurfaceComposerClient> mClient;
    173     sp<SurfaceControl> mSurfaceControl;
    174     sp<Surface> mPreviewSurface;
    175     sp<MediaRecorder> mRecorder;
    176     CameraParameters mParams;
    177     SkBitmap *skBMDec;
    178     SkImageEncoder* skJpegEnc;
    179     static SkBitmap *skBMtmp[2];
    180     static sp<IMemory> PiPPtrTmp[2];
    181 
    182     int mCurrentPreviewSizeIdx;
    183     Size getPreviewSizeFromVideoSizes(Size currentVideoSize);
    184     int mCurrentPictureSizeIdx;
    185     int mCurrentVideoSizeIdx;
    186     Vector<Size> mSupportedPreviewSizes;
    187     Vector<Size> mSupportedPictureSizes;
    188     Vector<Size> mSupportedVideoSizes;
    189 
    190     bool mInUse;
    191     Mutex mLock;
    192     Condition mCond;
    193 
    194     void useLock();
    195     void signalFinished();
    196     void mutexLock();
    197     void mutexUnlock();
    198 
    199     //------------------------------------------------------------------------
    200     // JPEG markers consist of one or more 0xFF bytes, followed by a marker
    201     // code byte (which is not an FF).  Here are the marker codes of interest
    202     // in this program.  (See jdmarker.c for a more complete list.)
    203     //------------------------------------------------------------------------
    204     #define M_SOF0  0xC0          // Start Of Frame N
    205     #define M_SOF1  0xC1          // N indicates which compression process
    206     #define M_SOF2  0xC2          // Only SOF0-SOF2 are now in common use
    207     #define M_SOF3  0xC3
    208     #define M_SOF5  0xC5          // NB: codes C4 and CC are NOT SOF markers
    209     #define M_SOF6  0xC6
    210     #define M_SOF7  0xC7
    211     #define M_SOF9  0xC9
    212     #define M_SOF10 0xCA
    213     #define M_SOF11 0xCB
    214     #define M_SOF13 0xCD
    215     #define M_SOF14 0xCE
    216     #define M_SOF15 0xCF
    217     #define M_SOI   0xD8          // Start Of Image (beginning of datastream)
    218     #define M_EOI   0xD9          // End Of Image (end of datastream)
    219     #define M_SOS   0xDA          // Start Of Scan (begins compressed data)
    220     #define M_JFIF  0xE0          // Jfif marker
    221     #define M_EXIF  0xE1          // Exif marker.  Also used for XMP data!
    222     #define M_XMP   0x10E1        // Not a real tag same value as Exif!
    223     #define M_COM   0xFE          // COMment
    224     #define M_DQT   0xDB
    225     #define M_DHT   0xC4
    226     #define M_DRI   0xDD
    227     #define M_IPTC  0xED          // IPTC marker
    228     #define PSEUDO_IMAGE_MARKER 0x123; // Extra value.
    229 };
    230 
    231 class Interpreter
    232 {
    233 public:
    234     enum Commands_e {
    235         SWITCH_CAMERA_CMD = 'A',
    236         RESUME_PREVIEW_CMD = '[',
    237         START_PREVIEW_CMD = '1',
    238         STOP_PREVIEW_CMD = '2',
    239         CHANGE_VIDEO_SIZE_CMD = '3',
    240         CHANGE_PREVIEW_SIZE_CMD = '4',
    241         CHANGE_PICTURE_SIZE_CMD = '5',
    242         START_RECORD_CMD = '6',
    243         STOP_RECORD_CMD = '7',
    244         START_VIV_RECORD_CMD = '8',
    245         STOP_VIV_RECORD_CMD = '9',
    246         DUMP_CAPS_CMD = 'E',
    247         AUTOFOCUS_CMD = 'f',
    248         TAKEPICTURE_CMD = 'p',
    249         TAKEPICTURE_IN_PICTURE_CMD = 'P',
    250         ENABLE_PRV_CALLBACKS_CMD = '&',
    251         EXIT_CMD = 'q',
    252         DELAY = 'd',
    253         INVALID_CMD = '0'
    254     };
    255 
    256     struct Command {
    257         Command( Commands_e cmd, char *arg = NULL)
    258         : cmd(cmd)
    259         , arg(arg) {}
    260         Command()
    261         : cmd(INVALID_CMD)
    262         , arg(NULL) {}
    263         Commands_e cmd;
    264         char *arg;
    265     };
    266 
    267     /* API */
    268     Interpreter()
    269     : mUseScript(false)
    270     , mScript(NULL) {}
    271 
    272     Interpreter(const char *file);
    273     ~Interpreter();
    274 
    275     Command getCommand(sp<CameraContext> currentCamera);
    276     void releasePiPBuff();
    277     status_t configureViVCodec();
    278     //status_t configureViVCodec();
    279     void setViVSize(Size VideoSize, int camIndex);
    280     void setTestCtxInst(TestContext *instance);
    281     status_t unconfigureViVCodec();
    282     status_t ViVEncoderThread();
    283     void ViVEncode();
    284     static void *ThreadWrapper(void *context);
    285 
    286 private:
    287     static const int numberOfCommands;
    288 
    289     bool mUseScript;
    290     int mCmdIndex;
    291     char *mScript;
    292     Vector<Command> mCommands;
    293     TestContext *mTestContext;
    294     pthread_t mViVEncThread;
    295 };
    296 
    297 class TestContext
    298 {
    299     friend class CameraContext;
    300     friend class Interpreter;
    301 public:
    302     TestContext();
    303     ~TestContext();
    304 
    305     int32_t GetCamerasNum();
    306     status_t FunctionalTest();
    307     status_t AddScriptFromFile(const char *scriptFile);
    308     void setViVSize(Size VideoSize, int camIndex);
    309 
    310 private:
    311     char GetNextCmd(sp<qcamera::CameraContext> currentCamera);
    312     int  mCurrentCameraIndex;
    313     int  mSaveCurrentCameraIndex;
    314     Vector< sp<qcamera::CameraContext> > mAvailableCameras;
    315     bool mTestRunning;
    316     Interpreter *mInterpreter;
    317 
    318     typedef struct ViVBuff_t{
    319         void *buff;
    320         size_t buffSize;
    321         size_t YStride;
    322         size_t UVStride;
    323         size_t YScanLines;
    324         size_t UVScanLines;
    325         size_t srcWidth;
    326         size_t srcHeight;
    327     } ViVBuff_t;
    328 
    329     typedef struct ViVVid_t{
    330         sp<IGraphicBufferProducer> bufferProducer;
    331         sp<Surface> surface;
    332         sp<MediaCodec> codec;
    333         sp<MediaMuxer> muxer;
    334         sp<ANativeWindow> ANW;
    335         Vector<sp<ABuffer> > buffers;
    336         Size VideoSizes[2];
    337         int ViVIdx;
    338         size_t buff_cnt;
    339         sp<GraphicBuffer> graphBuf;
    340         void * mappedBuff;
    341         bool isBuffValid;
    342         int sourceCameraID;
    343         int destinationCameraID;
    344     } vidPiP_t;
    345 
    346     ViVVid_t mViVVid;
    347     ViVBuff_t mViVBuff;
    348 };
    349 
    350 }; //namespace qcamera
    351 
    352 #endif
    353