1 /* Copyright (c) 2012-2016, The Linux Foundation. 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 #define MAX_CAM_INSTANCES 3
42 
43 class TestContext;
44 
45 class CameraContext : public CameraListener,
46     public ICameraRecordingProxyListener{
47 public:
48     typedef enum {
49         READ_METADATA = 1,
50         READ_IMAGE = 2,
51         READ_ALL = 3
52     } ReadMode_t;
53 
54     // This structure is used to store jpeg file sections in memory.
55     typedef struct {
56         unsigned char *  Data;
57         int      Type;
58         size_t   Size;
59     } Sections_t;
60 
61 public:
62     static const char KEY_ZSL[];
63 
64     CameraContext(int cameraIndex);
65     virtual ~CameraContext();
66 
67 
68 
69     status_t openCamera();
70     status_t closeCamera();
71 
72     status_t startPreview();
73     status_t stopPreview();
74     status_t resumePreview();
75     status_t autoFocus();
76     status_t enablePreviewCallbacks();
77     status_t takePicture();
78     status_t startRecording();
79     status_t stopRecording();
80     status_t startViVRecording();
81     status_t stopViVRecording();
82     status_t configureViVRecording();
83 
84     status_t nextPreviewSize();
85     status_t setPreviewSize(const char *format);
86     status_t getCurrentPreviewSize(Size &previewSize);
87 
88     status_t nextPictureSize();
89     status_t getCurrentPictureSize(Size &pictureSize);
90     status_t setPictureSize(const char *format);
91 
92     status_t nextVideoSize();
93     status_t setVideoSize(const char *format);
94     status_t getCurrentVideoSize(Size &videoSize);
95     status_t configureRecorder();
96     status_t unconfigureRecorder();
97     Sections_t *FindSection(int SectionType);
98     status_t ReadSectionsFromBuffer (unsigned char *buffer,
99             size_t buffer_size, ReadMode_t ReadMode);
100     virtual IBinder* onAsBinder();
101     void setTestCtxInstance(TestContext *instance);
102 
103     void printMenu(sp<CameraContext> currentCamera);
104     void printSupportedParams();
105     const char *getZSL();
106     void setZSL(const char *value);
107 
108 
getCameraIndex()109     int getCameraIndex() { return mCameraIndex; }
110     int getNumberOfCameras();
111     void enablePrintPreview();
112     void disablePrintPreview();
113     void enablePiPCapture();
114     void disablePiPCapture();
115     void CheckSectionsAllocated();
116     void DiscardData();
117     void DiscardSections();
118     size_t calcBufferSize(int width, int height);
119     size_t calcStride(int width);
120     size_t calcYScanLines(int height);
121     size_t calcUVScanLines(int height);
122 
123     virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2);
124     virtual void postData(int32_t msgType,
125             const sp<IMemory>& dataPtr,
126             camera_frame_metadata_t *metadata);
127 
128     virtual void postDataTimestamp(nsecs_t timestamp,
129             int32_t msgType,
130             const sp<IMemory>& dataPtr);
131     virtual void dataCallbackTimestamp(nsecs_t timestamp,
132             int32_t msgType,
133             const sp<IMemory>& dataPtr);
134 
135 private:
136 
137     status_t createPreviewSurface(int width, int height, int32_t pixFormat);
138     status_t destroyPreviewSurface();
139 
140     status_t saveFile(const sp<IMemory>& mem, String8 path);
141     SkBitmap * PiPCopyToOneFile(SkBitmap *bitmap0, SkBitmap *bitmap1);
142     status_t decodeJPEG(const sp<IMemory>& mem, SkBitmap *skBM);
143     status_t encodeJPEG(SkWStream * stream, const SkBitmap *bitmap,
144         String8 path);
145     void previewCallback(const sp<IMemory>& mem);
146 
147     static int JpegIdx;
148     int mCameraIndex;
149     bool mResizePreview;
150     bool mHardwareActive;
151     bool mPreviewRunning;
152     bool mRecordRunning;
153     int  mVideoFd;
154     int  mVideoIdx;
155     bool mRecordingHint;
156     bool mDoPrintMenu;
157     bool mPiPCapture;
158     static int mPiPIdx;
159     unsigned int mfmtMultiplier;
160     int mWidthTmp;
161     int mHeightTmp;
162     size_t mSectionsRead;
163     size_t mSectionsAllocated;
164     Sections_t * mSections;
165     Sections_t * mJEXIFTmp;
166     Sections_t mJEXIFSection;
167     int mHaveAll;
168     TestContext *mInterpr;
169 
170     sp<Camera> mCamera;
171     sp<SurfaceComposerClient> mClient;
172     sp<SurfaceControl> mSurfaceControl;
173     sp<Surface> mPreviewSurface;
174     sp<MediaRecorder> mRecorder;
175     CameraParameters mParams;
176     SkBitmap *skBMDec;
177     SkBitmap skBMtmp;
178     sp<IMemory> PiPPtrTmp;
179 
180     size_t mCurrentPreviewSizeIdx;
181     Size getPreviewSizeFromVideoSizes(Size currentVideoSize);
182     size_t mCurrentPictureSizeIdx;
183     size_t mCurrentVideoSizeIdx;
184     Vector<Size> mSupportedPreviewSizes;
185     Vector<Size> mSupportedPictureSizes;
186     Vector<Size> mSupportedVideoSizes;
187 
188     bool mInUse;
189     Mutex mLock;
190     Condition mCond;
191 
192     void useLock();
193     void signalFinished();
194 
195     //------------------------------------------------------------------------
196     // JPEG markers consist of one or more 0xFF bytes, followed by a marker
197     // code byte (which is not an FF).  Here are the marker codes of interest
198     // in this program.  (See jdmarker.c for a more complete list.)
199     //------------------------------------------------------------------------
200     #define M_SOF0  0xC0          // Start Of Frame N
201     #define M_SOF1  0xC1          // N indicates which compression process
202     #define M_SOF2  0xC2          // Only SOF0-SOF2 are now in common use
203     #define M_SOF3  0xC3
204     #define M_SOF5  0xC5          // NB: codes C4 and CC are NOT SOF markers
205     #define M_SOF6  0xC6
206     #define M_SOF7  0xC7
207     #define M_SOF9  0xC9
208     #define M_SOF10 0xCA
209     #define M_SOF11 0xCB
210     #define M_SOF13 0xCD
211     #define M_SOF14 0xCE
212     #define M_SOF15 0xCF
213     #define M_SOI   0xD8          // Start Of Image (beginning of datastream)
214     #define M_EOI   0xD9          // End Of Image (end of datastream)
215     #define M_SOS   0xDA          // Start Of Scan (begins compressed data)
216     #define M_JFIF  0xE0          // Jfif marker
217     #define M_EXIF  0xE1          // Exif marker.  Also used for XMP data!
218     #define M_XMP   0x10E1        // Not a real tag same value as Exif!
219     #define M_COM   0xFE          // COMment
220     #define M_DQT   0xDB
221     #define M_DHT   0xC4
222     #define M_DRI   0xDD
223     #define M_IPTC  0xED          // IPTC marker
224     #define PSEUDO_IMAGE_MARKER 0x123; // Extra value.
225 };
226 
227 class Interpreter
228 {
229 public:
230     enum Commands_e {
231         SWITCH_CAMERA_CMD = 'A',
232         RESUME_PREVIEW_CMD = '[',
233         START_PREVIEW_CMD = '1',
234         STOP_PREVIEW_CMD = '2',
235         CHANGE_VIDEO_SIZE_CMD = '3',
236         CHANGE_PREVIEW_SIZE_CMD = '4',
237         CHANGE_PICTURE_SIZE_CMD = '5',
238         START_RECORD_CMD = '6',
239         STOP_RECORD_CMD = '7',
240         START_VIV_RECORD_CMD = '8',
241         STOP_VIV_RECORD_CMD = '9',
242         DUMP_CAPS_CMD = 'E',
243         AUTOFOCUS_CMD = 'f',
244         TAKEPICTURE_CMD = 'p',
245         TAKEPICTURE_IN_PICTURE_CMD = 'P',
246         ENABLE_PRV_CALLBACKS_CMD = '&',
247         EXIT_CMD = 'q',
248         DELAY = 'd',
249         ZSL_CMD = 'z',
250         INVALID_CMD = '0'
251     };
252 
253     struct Command {
254         Command( Commands_e cmd_, char *arg_ = NULL)
cmdCommand255         : cmd(cmd_)
256         , arg(arg_) {}
CommandCommand257         Command()
258         : cmd(INVALID_CMD)
259         , arg(NULL) {}
260         Commands_e cmd;
261         char *arg;
262     };
263 
264     /* API */
Interpreter()265     Interpreter()
266     : mUseScript(false)
267     , mScript(NULL) {}
268 
269     Interpreter(const char *file);
270     ~Interpreter();
271 
272     Command getCommand(sp<CameraContext> currentCamera);
273     void releasePiPBuff();
274     status_t configureViVCodec();
275     void setViVSize(Size VideoSize, int camIndex);
276     void setTestCtxInst(TestContext *instance);
277     status_t unconfigureViVCodec();
278     status_t ViVEncoderThread();
279     void ViVEncode();
280     static void *ThreadWrapper(void *context);
281 
282 private:
283     static const int numberOfCommands;
284 
285     bool mUseScript;
286     size_t mCmdIndex;
287     char *mScript;
288     Vector<Command> mCommands;
289     TestContext *mTestContext;
290     pthread_t mViVEncThread;
291 };
292 
293 class TestContext
294 {
295     friend class CameraContext;
296     friend class Interpreter;
297 public:
298     TestContext();
299     ~TestContext();
300 
301     size_t GetCamerasNum();
302     status_t FunctionalTest();
303     status_t AddScriptFromFile(const char *scriptFile);
304     void setViVSize(Size VideoSize, int camIndex);
305     void PiPLock();
306     void PiPUnlock();
307     void ViVLock();
308     void ViVUnlock();
309 
310 private:
311     sp<CameraContext> camera[MAX_CAM_INSTANCES];
312     char GetNextCmd(sp<qcamera::CameraContext> currentCamera);
313     size_t mCurrentCameraIndex;
314     size_t mSaveCurrentCameraIndex;
315     Vector< sp<qcamera::CameraContext> > mAvailableCameras;
316     bool mTestRunning;
317     Interpreter *mInterpreter;
318     Mutex mPiPLock;
319     Condition mPiPCond;
320     bool mPiPinUse;
321     Mutex mViVLock;
322     Condition mViVCond;
323     bool mViVinUse;
324     bool mIsZSLOn;
325 
326     typedef struct ViVBuff_t{
327         void *buff;
328         size_t buffSize;
329         size_t YStride;
330         size_t UVStride;
331         size_t YScanLines;
332         size_t UVScanLines;
333         size_t srcWidth;
334         size_t srcHeight;
335     } ViVBuff_t;
336 
337     typedef struct ViVVid_t{
338         sp<IGraphicBufferProducer> bufferProducer;
339         sp<Surface> surface;
340         sp<MediaCodec> codec;
341         sp<MediaMuxer> muxer;
342         sp<ANativeWindow> ANW;
343         Vector<sp<ABuffer> > buffers;
344         Size VideoSizes[2];
345         int ViVIdx;
346         size_t buff_cnt;
347         sp<GraphicBuffer> graphBuf;
348         void * mappedBuff;
349         bool isBuffValid;
350         int sourceCameraID;
351         int destinationCameraID;
352     } vidPiP_t;
353 
354     ViVVid_t mViVVid;
355     ViVBuff_t mViVBuff;
356 };
357 
358 }; //namespace qcamera
359 
360 #endif
361