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 
getCameraIndex()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     virtual void postRecordingFrameHandleTimestamp(nsecs_t timestamp,
130             native_handle_t* handle);
131     virtual void recordingFrameHandleCallbackTimestamp(nsecs_t timestamp,
132             native_handle_t* handle);
133 
134 private:
135 
136     status_t createPreviewSurface(unsigned int width,
137             unsigned int height,
138             int32_t pixFormat);
139     status_t destroyPreviewSurface();
140 
141     status_t saveFile(const sp<IMemory>& mem, String8 path);
142     SkBitmap * PiPCopyToOneFile(SkBitmap *bitmap0, SkBitmap *bitmap1);
143     SkBitmap *decodeJPEG(const sp<IMemory>& mem);
144     status_t encodeJPEG(SkWStream * stream, const SkBitmap *bitmap,
145         String8 path);
146     void previewCallback(const sp<IMemory>& mem);
147 
148     static int JpegIdx;
149     int mCameraIndex;
150     bool mResizePreview;
151     bool mHardwareActive;
152     bool mPreviewRunning;
153     bool mRecordRunning;
154     int  mVideoFd;
155     int  mVideoIdx;
156     bool mRecordingHint;
157     bool mDoPrintMenu;
158     bool mPiPCapture;
159     static int mPiPIdx;
160     int mfmtMultiplier;
161     int mWidthTmp[2];
162     int mHeightTmp[2];
163     int mSectionsRead;
164     int mSectionsAllocated;
165     Sections_t * mSections;
166     Sections_t * mJEXIFTmp;
167     Sections_t mJEXIFSection;
168     int mHaveAll;
169     TestContext *mInterpr;
170     Mutex mViVLock;
171     Condition mViVCond;
172     bool mViVinUse;
173 
174 
175     sp<Camera> mCamera;
176     sp<SurfaceComposerClient> mClient;
177     sp<SurfaceControl> mSurfaceControl;
178     sp<Surface> mPreviewSurface;
179     sp<MediaRecorder> mRecorder;
180     CameraParameters mParams;
181     SkBitmap *skBMDec;
182     SkImageEncoder* skJpegEnc;
183     static SkBitmap *skBMtmp[2];
184     static sp<IMemory> PiPPtrTmp[2];
185 
186     int mCurrentPreviewSizeIdx;
187     Size getPreviewSizeFromVideoSizes(Size currentVideoSize);
188     int mCurrentPictureSizeIdx;
189     int mCurrentVideoSizeIdx;
190     Vector<Size> mSupportedPreviewSizes;
191     Vector<Size> mSupportedPictureSizes;
192     Vector<Size> mSupportedVideoSizes;
193 
194     bool mInUse;
195     Mutex mLock;
196     Condition mCond;
197 
198     void useLock();
199     void signalFinished();
200     void mutexLock();
201     void mutexUnlock();
202 
203     //------------------------------------------------------------------------
204     // JPEG markers consist of one or more 0xFF bytes, followed by a marker
205     // code byte (which is not an FF).  Here are the marker codes of interest
206     // in this program.  (See jdmarker.c for a more complete list.)
207     //------------------------------------------------------------------------
208     #define M_SOF0  0xC0          // Start Of Frame N
209     #define M_SOF1  0xC1          // N indicates which compression process
210     #define M_SOF2  0xC2          // Only SOF0-SOF2 are now in common use
211     #define M_SOF3  0xC3
212     #define M_SOF5  0xC5          // NB: codes C4 and CC are NOT SOF markers
213     #define M_SOF6  0xC6
214     #define M_SOF7  0xC7
215     #define M_SOF9  0xC9
216     #define M_SOF10 0xCA
217     #define M_SOF11 0xCB
218     #define M_SOF13 0xCD
219     #define M_SOF14 0xCE
220     #define M_SOF15 0xCF
221     #define M_SOI   0xD8          // Start Of Image (beginning of datastream)
222     #define M_EOI   0xD9          // End Of Image (end of datastream)
223     #define M_SOS   0xDA          // Start Of Scan (begins compressed data)
224     #define M_JFIF  0xE0          // Jfif marker
225     #define M_EXIF  0xE1          // Exif marker.  Also used for XMP data!
226     #define M_XMP   0x10E1        // Not a real tag same value as Exif!
227     #define M_COM   0xFE          // COMment
228     #define M_DQT   0xDB
229     #define M_DHT   0xC4
230     #define M_DRI   0xDD
231     #define M_IPTC  0xED          // IPTC marker
232     #define PSEUDO_IMAGE_MARKER 0x123; // Extra value.
233 };
234 
235 class Interpreter
236 {
237 public:
238     enum Commands_e {
239         SWITCH_CAMERA_CMD = 'A',
240         RESUME_PREVIEW_CMD = '[',
241         START_PREVIEW_CMD = '1',
242         STOP_PREVIEW_CMD = '2',
243         CHANGE_VIDEO_SIZE_CMD = '3',
244         CHANGE_PREVIEW_SIZE_CMD = '4',
245         CHANGE_PICTURE_SIZE_CMD = '5',
246         START_RECORD_CMD = '6',
247         STOP_RECORD_CMD = '7',
248         START_VIV_RECORD_CMD = '8',
249         STOP_VIV_RECORD_CMD = '9',
250         DUMP_CAPS_CMD = 'E',
251         AUTOFOCUS_CMD = 'f',
252         TAKEPICTURE_CMD = 'p',
253         TAKEPICTURE_IN_PICTURE_CMD = 'P',
254         ENABLE_PRV_CALLBACKS_CMD = '&',
255         EXIT_CMD = 'q',
256         DELAY = 'd',
257         INVALID_CMD = '0'
258     };
259 
260     struct Command {
261         Command( Commands_e cmd, char *arg = NULL)
cmdCommand262         : cmd(cmd)
263         , arg(arg) {}
CommandCommand264         Command()
265         : cmd(INVALID_CMD)
266         , arg(NULL) {}
267         Commands_e cmd;
268         char *arg;
269     };
270 
271     /* API */
Interpreter()272     Interpreter()
273     : mUseScript(false)
274     , mScript(NULL) {}
275 
276     Interpreter(const char *file);
277     ~Interpreter();
278 
279     Command getCommand(sp<CameraContext> currentCamera);
280     void releasePiPBuff();
281     status_t configureViVCodec();
282     //status_t configureViVCodec();
283     void setViVSize(Size VideoSize, int camIndex);
284     void setTestCtxInst(TestContext *instance);
285     status_t unconfigureViVCodec();
286     status_t ViVEncoderThread();
287     void ViVEncode();
288     static void *ThreadWrapper(void *context);
289 
290 private:
291     static const int numberOfCommands;
292 
293     bool mUseScript;
294     int mCmdIndex;
295     char *mScript;
296     Vector<Command> mCommands;
297     TestContext *mTestContext;
298     pthread_t mViVEncThread;
299 };
300 
301 class TestContext
302 {
303     friend class CameraContext;
304     friend class Interpreter;
305 public:
306     TestContext();
307     ~TestContext();
308 
309     int32_t GetCamerasNum();
310     status_t FunctionalTest();
311     status_t AddScriptFromFile(const char *scriptFile);
312     void setViVSize(Size VideoSize, int camIndex);
313 
314 private:
315     char GetNextCmd(sp<qcamera::CameraContext> currentCamera);
316     int  mCurrentCameraIndex;
317     int  mSaveCurrentCameraIndex;
318     Vector< sp<qcamera::CameraContext> > mAvailableCameras;
319     bool mTestRunning;
320     Interpreter *mInterpreter;
321 
322     typedef struct ViVBuff_t{
323         void *buff;
324         size_t buffSize;
325         size_t YStride;
326         size_t UVStride;
327         size_t YScanLines;
328         size_t UVScanLines;
329         size_t srcWidth;
330         size_t srcHeight;
331     } ViVBuff_t;
332 
333     typedef struct ViVVid_t{
334         sp<IGraphicBufferProducer> bufferProducer;
335         sp<Surface> surface;
336         sp<MediaCodec> codec;
337         sp<MediaMuxer> muxer;
338         sp<ANativeWindow> ANW;
339         Vector<sp<ABuffer> > buffers;
340         Size VideoSizes[2];
341         int ViVIdx;
342         size_t buff_cnt;
343         sp<GraphicBuffer> graphBuf;
344         void * mappedBuff;
345         bool isBuffValid;
346         int sourceCameraID;
347         int destinationCameraID;
348     } vidPiP_t;
349 
350     ViVVid_t mViVVid;
351     ViVBuff_t mViVBuff;
352 };
353 
354 }; //namespace qcamera
355 
356 #endif
357