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     SkImageEncoder* skJpegEnc;
178     SkBitmap skBMtmp;
179     sp<IMemory> PiPPtrTmp;
180 
181     size_t mCurrentPreviewSizeIdx;
182     Size getPreviewSizeFromVideoSizes(Size currentVideoSize);
183     size_t mCurrentPictureSizeIdx;
184     size_t mCurrentVideoSizeIdx;
185     Vector<Size> mSupportedPreviewSizes;
186     Vector<Size> mSupportedPictureSizes;
187     Vector<Size> mSupportedVideoSizes;
188 
189     bool mInUse;
190     Mutex mLock;
191     Condition mCond;
192 
193     void useLock();
194     void signalFinished();
195 
196     //------------------------------------------------------------------------
197     // JPEG markers consist of one or more 0xFF bytes, followed by a marker
198     // code byte (which is not an FF).  Here are the marker codes of interest
199     // in this program.  (See jdmarker.c for a more complete list.)
200     //------------------------------------------------------------------------
201     #define M_SOF0  0xC0          // Start Of Frame N
202     #define M_SOF1  0xC1          // N indicates which compression process
203     #define M_SOF2  0xC2          // Only SOF0-SOF2 are now in common use
204     #define M_SOF3  0xC3
205     #define M_SOF5  0xC5          // NB: codes C4 and CC are NOT SOF markers
206     #define M_SOF6  0xC6
207     #define M_SOF7  0xC7
208     #define M_SOF9  0xC9
209     #define M_SOF10 0xCA
210     #define M_SOF11 0xCB
211     #define M_SOF13 0xCD
212     #define M_SOF14 0xCE
213     #define M_SOF15 0xCF
214     #define M_SOI   0xD8          // Start Of Image (beginning of datastream)
215     #define M_EOI   0xD9          // End Of Image (end of datastream)
216     #define M_SOS   0xDA          // Start Of Scan (begins compressed data)
217     #define M_JFIF  0xE0          // Jfif marker
218     #define M_EXIF  0xE1          // Exif marker.  Also used for XMP data!
219     #define M_XMP   0x10E1        // Not a real tag same value as Exif!
220     #define M_COM   0xFE          // COMment
221     #define M_DQT   0xDB
222     #define M_DHT   0xC4
223     #define M_DRI   0xDD
224     #define M_IPTC  0xED          // IPTC marker
225     #define PSEUDO_IMAGE_MARKER 0x123; // Extra value.
226 };
227 
228 class Interpreter
229 {
230 public:
231     enum Commands_e {
232         SWITCH_CAMERA_CMD = 'A',
233         RESUME_PREVIEW_CMD = '[',
234         START_PREVIEW_CMD = '1',
235         STOP_PREVIEW_CMD = '2',
236         CHANGE_VIDEO_SIZE_CMD = '3',
237         CHANGE_PREVIEW_SIZE_CMD = '4',
238         CHANGE_PICTURE_SIZE_CMD = '5',
239         START_RECORD_CMD = '6',
240         STOP_RECORD_CMD = '7',
241         START_VIV_RECORD_CMD = '8',
242         STOP_VIV_RECORD_CMD = '9',
243         DUMP_CAPS_CMD = 'E',
244         AUTOFOCUS_CMD = 'f',
245         TAKEPICTURE_CMD = 'p',
246         TAKEPICTURE_IN_PICTURE_CMD = 'P',
247         ENABLE_PRV_CALLBACKS_CMD = '&',
248         EXIT_CMD = 'q',
249         DELAY = 'd',
250         ZSL_CMD = 'z',
251         INVALID_CMD = '0'
252     };
253 
254     struct Command {
255         Command( Commands_e cmd_, char *arg_ = NULL)
cmdCommand256         : cmd(cmd_)
257         , arg(arg_) {}
CommandCommand258         Command()
259         : cmd(INVALID_CMD)
260         , arg(NULL) {}
261         Commands_e cmd;
262         char *arg;
263     };
264 
265     /* API */
Interpreter()266     Interpreter()
267     : mUseScript(false)
268     , mScript(NULL) {}
269 
270     Interpreter(const char *file);
271     ~Interpreter();
272 
273     Command getCommand(sp<CameraContext> currentCamera);
274     void releasePiPBuff();
275     status_t configureViVCodec();
276     void setViVSize(Size VideoSize, int camIndex);
277     void setTestCtxInst(TestContext *instance);
278     status_t unconfigureViVCodec();
279     status_t ViVEncoderThread();
280     void ViVEncode();
281     static void *ThreadWrapper(void *context);
282 
283 private:
284     static const int numberOfCommands;
285 
286     bool mUseScript;
287     size_t mCmdIndex;
288     char *mScript;
289     Vector<Command> mCommands;
290     TestContext *mTestContext;
291     pthread_t mViVEncThread;
292 };
293 
294 class TestContext
295 {
296     friend class CameraContext;
297     friend class Interpreter;
298 public:
299     TestContext();
300     ~TestContext();
301 
302     size_t GetCamerasNum();
303     status_t FunctionalTest();
304     status_t AddScriptFromFile(const char *scriptFile);
305     void setViVSize(Size VideoSize, int camIndex);
306     void PiPLock();
307     void PiPUnlock();
308     void ViVLock();
309     void ViVUnlock();
310 
311 private:
312     sp<CameraContext> camera[MAX_CAM_INSTANCES];
313     char GetNextCmd(sp<qcamera::CameraContext> currentCamera);
314     size_t mCurrentCameraIndex;
315     size_t mSaveCurrentCameraIndex;
316     Vector< sp<qcamera::CameraContext> > mAvailableCameras;
317     bool mTestRunning;
318     Interpreter *mInterpreter;
319     Mutex mPiPLock;
320     Condition mPiPCond;
321     bool mPiPinUse;
322     Mutex mViVLock;
323     Condition mViVCond;
324     bool mViVinUse;
325     bool mIsZSLOn;
326 
327     typedef struct ViVBuff_t{
328         void *buff;
329         size_t buffSize;
330         size_t YStride;
331         size_t UVStride;
332         size_t YScanLines;
333         size_t UVScanLines;
334         size_t srcWidth;
335         size_t srcHeight;
336     } ViVBuff_t;
337 
338     typedef struct ViVVid_t{
339         sp<IGraphicBufferProducer> bufferProducer;
340         sp<Surface> surface;
341         sp<MediaCodec> codec;
342         sp<MediaMuxer> muxer;
343         sp<ANativeWindow> ANW;
344         Vector<sp<ABuffer> > buffers;
345         Size VideoSizes[2];
346         int ViVIdx;
347         size_t buff_cnt;
348         sp<GraphicBuffer> graphBuf;
349         void * mappedBuff;
350         bool isBuffValid;
351         int sourceCameraID;
352         int destinationCameraID;
353     } vidPiP_t;
354 
355     ViVVid_t mViVVid;
356     ViVBuff_t mViVBuff;
357 };
358 
359 }; //namespace qcamera
360 
361 #endif
362