1/*
2** Copyright 2008, Google Inc.
3** Copyright (c) 2009-2012, Code Aurora Forum. All rights reserved.
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9**     http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#ifndef ANDROID_HARDWARE_QCAMERA_STREAM_H
19#define ANDROID_HARDWARE_QCAMERA_STREAM_H
20
21
22#include <utils/threads.h>
23
24#include <binder/MemoryBase.h>
25#include <binder/MemoryHeapBase.h>
26#include <utils/threads.h>
27
28#include "QCameraHWI.h"
29#include "QCameraHWI_Mem.h"
30
31extern "C" {
32
33#include <camera_defs_i.h>
34#include <mm_camera_interface2.h>
35
36#define DEFAULT_STREAM_WIDTH 320
37#define DEFAULT_STREAM_HEIGHT 240
38#define DEFAULT_LIVESHOT_WIDTH 2592
39#define DEFAULT_LIVESHOT_HEIGHT 1944
40
41#define MM_CAMERA_CH_PREVIEW_MASK    (0x01 << MM_CAMERA_CH_PREVIEW)
42#define MM_CAMERA_CH_VIDEO_MASK      (0x01 << MM_CAMERA_CH_VIDEO)
43#define MM_CAMERA_CH_SNAPSHOT_MASK   (0x01 << MM_CAMERA_CH_SNAPSHOT)
44
45} /* extern C*/
46
47
48namespace android {
49
50class QCameraHardwareInterface;
51
52class StreamQueue {
53private:
54    Mutex mQueueLock;
55    Condition mQueueWait;
56    bool mInitialized;
57
58    //Vector<struct msm_frame *> mContainer;
59    Vector<void *> mContainer;
60public:
61    StreamQueue();
62    virtual ~StreamQueue();
63    bool enqueue(void *element);
64    void flush();
65    void* dequeue();
66    void init();
67    void deinit();
68    bool isInitialized();
69bool isEmpty();
70};
71
72
73class QCameraStream { //: public virtual RefBase{
74
75public:
76    bool mInit;
77    bool mActive;
78
79    virtual status_t    init();
80    virtual status_t    start();
81    virtual void        stop();
82    virtual void        release();
83
84    status_t setFormat(uint8_t ch_type_mask);
85    status_t setMode(int enable);
86
87    virtual void        setHALCameraControl(QCameraHardwareInterface* ctrl);
88
89    //static status_t     openChannel(mm_camera_t *, mm_camera_channel_type_t ch_type);
90    virtual status_t    initChannel(int cameraId, uint32_t ch_type_mask);
91    virtual status_t    deinitChannel(int cameraId, mm_camera_channel_type_t ch_type);
92    virtual void releaseRecordingFrame(const void *opaque)
93    {
94      ;
95    }
96#if 0 // mzhu
97    virtual status_t getBufferInfo(sp<IMemory>& Frame, size_t *alignedSize)
98    {
99      return NO_ERROR;
100    }
101#endif // mzhu
102    virtual void prepareHardware()
103    {
104      ;
105    }
106    virtual sp<IMemoryHeap> getHeap() const{return NULL;}
107    virtual status_t    initDisplayBuffers(){return NO_ERROR;}
108    virtual status_t initPreviewOnlyBuffers(){return NO_ERROR;}
109    virtual sp<IMemoryHeap> getRawHeap() const {return NULL;}
110    virtual void *getLastQueuedFrame(void){return NULL;}
111    virtual status_t takePictureZSL(void){return NO_ERROR;}
112    virtual status_t takeLiveSnapshot(){return NO_ERROR;}
113    virtual status_t takePictureLiveshot(mm_camera_ch_data_buf_t* recvd_frame,
114                                 cam_ctrl_dimension_t *dim,
115                                 int frame_len){return NO_ERROR;}
116	virtual void setModeLiveSnapshot(bool){;}
117    virtual status_t initSnapshotBuffers(cam_ctrl_dimension_t *dim,
118                                 int num_of_buf){return NO_ERROR;}
119
120    virtual void setFullSizeLiveshot(bool){};
121    /* Set the ANativeWindow */
122    virtual int setPreviewWindow(preview_stream_ops_t* window) {return NO_ERROR;}
123    virtual void notifyROIEvent(fd_roi_t roi) {;}
124    virtual void notifyWDenoiseEvent(cam_ctrl_status_t status, void * cookie) {;}
125    virtual void resetSnapshotCounters(void ){};
126
127    QCameraStream();
128    QCameraStream(int, camera_mode_t);
129    virtual             ~QCameraStream();
130    QCameraHardwareInterface*  mHalCamCtrl;
131    mm_camera_ch_crop_t mCrop;
132
133    int mCameraId;
134    camera_mode_t myMode;
135
136    mutable Mutex mStopCallbackLock;
137private:
138   StreamQueue mBusyQueue;
139   StreamQueue mFreeQueue;
140public:
141     friend void liveshot_callback(mm_camera_ch_data_buf_t *frame,void *user_data);
142};
143
144/*
145*   Record Class
146*/
147class QCameraStream_record : public QCameraStream {
148public:
149  status_t    init();
150  status_t    start() ;
151  void        stop()  ;
152  void        release() ;
153
154  static QCameraStream*  createInstance(int cameraId, camera_mode_t);
155  static void            deleteInstance(QCameraStream *p);
156
157  QCameraStream_record() {};
158  virtual             ~QCameraStream_record();
159
160  status_t processRecordFrame(void *data);
161  status_t initEncodeBuffers();
162  status_t getBufferInfo(sp<IMemory>& Frame, size_t *alignedSize);
163  //sp<IMemoryHeap> getHeap() const;
164
165  void releaseRecordingFrame(const void *opaque);
166  void debugShowVideoFPS() const;
167
168  status_t takeLiveSnapshot();
169private:
170  QCameraStream_record(int, camera_mode_t);
171  void releaseEncodeBuffer();
172
173  cam_ctrl_dimension_t             dim;
174  bool mDebugFps;
175
176  mm_camera_reg_buf_t              mRecordBuf;
177  //int                              record_frame_len;
178  //static const int                 maxFrameCnt = 16;
179  //camera_memory_t                 *mCameraMemoryPtr[maxFrameCnt];
180  //int                              mNumRecordFrames;
181  //sp<PmemPool>                     mRecordHeap[maxFrameCnt];
182  struct msm_frame                *recordframes;
183  //uint32_t                         record_offset[VIDEO_BUFFER_COUNT];
184  mm_camera_ch_data_buf_t          mRecordedFrames[MM_CAMERA_MAX_NUM_FRAMES];
185  //Mutex                            mRecordFreeQueueLock;
186  //Vector<mm_camera_ch_data_buf_t>  mRecordFreeQueue;
187
188  int mJpegMaxSize;
189  QCameraStream *mStreamSnap;
190
191};
192
193class QCameraStream_preview : public QCameraStream {
194public:
195    status_t    init();
196    status_t    start() ;
197    void        stop()  ;
198    void        release() ;
199
200    static QCameraStream*  createInstance(int, camera_mode_t);
201    static void            deleteInstance(QCameraStream *p);
202
203    QCameraStream_preview() {};
204    virtual             ~QCameraStream_preview();
205    void *getLastQueuedFrame(void);
206    /*init preview buffers with display case*/
207    status_t initDisplayBuffers();
208    /*init preview buffers without display case*/
209    status_t initPreviewOnlyBuffers();
210
211    status_t processPreviewFrame(mm_camera_ch_data_buf_t *frame);
212
213    /*init preview buffers with display case*/
214    status_t processPreviewFrameWithDisplay(mm_camera_ch_data_buf_t *frame);
215    /*init preview buffers without display case*/
216    status_t processPreviewFrameWithOutDisplay(mm_camera_ch_data_buf_t *frame);
217
218    int setPreviewWindow(preview_stream_ops_t* window);
219    void notifyROIEvent(fd_roi_t roi);
220    friend class QCameraHardwareInterface;
221
222private:
223    QCameraStream_preview(int cameraId, camera_mode_t);
224    /*allocate and free buffers with display case*/
225    status_t                 getBufferFromSurface();
226    status_t                 putBufferToSurface();
227
228    /*allocate and free buffers without display case*/
229    status_t                 getBufferNoDisplay();
230    status_t                 freeBufferNoDisplay();
231
232    void                     dumpFrameToFile(struct msm_frame* newFrame);
233    bool                     mFirstFrameRcvd;
234
235    int8_t                   my_id;
236    mm_camera_op_mode_type_t op_mode;
237    cam_ctrl_dimension_t     dim;
238    struct msm_frame        *mLastQueuedFrame;
239    mm_camera_reg_buf_t      mDisplayBuf;
240    mm_cameara_stream_buf_t  mDisplayStreamBuf;
241    Mutex                   mDisplayLock;
242    preview_stream_ops_t   *mPreviewWindow;
243    static const int        kPreviewBufferCount = PREVIEW_BUFFER_COUNT;
244    mm_camera_ch_data_buf_t mNotifyBuffer[16];
245    int8_t                  mNumFDRcvd;
246    int                     mVFEOutputs;
247    int                     mHFRFrameCnt;
248    int                     mHFRFrameSkip;
249};
250
251/* Snapshot Class - handle data flow*/
252class QCameraStream_Snapshot : public QCameraStream {
253public:
254    status_t    init();
255    status_t    start();
256    void        stop();
257    void        release();
258    void        prepareHardware();
259    static QCameraStream* createInstance(int cameraId, camera_mode_t);
260    static void deleteInstance(QCameraStream *p);
261
262    status_t takePictureZSL(void);
263    status_t takePictureLiveshot(mm_camera_ch_data_buf_t* recvd_frame,
264                                 cam_ctrl_dimension_t *dim,
265                                 int frame_len);
266    status_t receiveRawPicture(mm_camera_ch_data_buf_t* recvd_frame);
267    void receiveCompleteJpegPicture(jpeg_event_t event);
268    void receiveJpegFragment(uint8_t *ptr, uint32_t size);
269    void deInitBuffer(void);
270    sp<IMemoryHeap> getRawHeap() const;
271    int getSnapshotState();
272    /*Temp: to be removed once event handling is enabled in mm-camera*/
273    void runSnapshotThread(void *data);
274    bool isZSLMode();
275    void setFullSizeLiveshot(bool);
276    void notifyWDenoiseEvent(cam_ctrl_status_t status, void * cookie);
277    friend void liveshot_callback(mm_camera_ch_data_buf_t *frame,void *user_data);
278    void resetSnapshotCounters(void );
279
280private:
281    QCameraStream_Snapshot(int, camera_mode_t);
282    virtual ~QCameraStream_Snapshot();
283
284    /* snapshot related private members */
285    status_t initJPEGSnapshot(int num_of_snapshots);
286    status_t initRawSnapshot(int num_of_snapshots);
287    status_t initZSLSnapshot(void);
288    status_t initFullLiveshot(void);
289	status_t cancelPicture();
290    void notifyShutter(common_crop_t *crop,
291                       bool play_shutter_sound);
292    status_t initSnapshotBuffers(cam_ctrl_dimension_t *dim,
293                                 int num_of_buf);
294    status_t initRawSnapshotBuffers(cam_ctrl_dimension_t *dim,
295                                    int num_of_buf);
296    status_t deinitRawSnapshotBuffers(void);
297    status_t deinitSnapshotBuffers(void);
298    status_t initRawSnapshotChannel(cam_ctrl_dimension_t* dim,
299                                    int num_snapshots);
300    status_t initSnapshotFormat(cam_ctrl_dimension_t *dim);
301    status_t takePictureRaw(void);
302    status_t takePictureJPEG(void);
303    status_t startStreamZSL(void);
304    void deinitSnapshotChannel(mm_camera_channel_type_t);
305    status_t configSnapshotDimension(cam_ctrl_dimension_t* dim);
306    status_t encodeData(mm_camera_ch_data_buf_t* recvd_frame,
307                        common_crop_t *crop_info,
308                        int frame_len,
309                        bool enqueued);
310    status_t encodeDisplayAndSave(mm_camera_ch_data_buf_t* recvd_frame,
311                                  bool enqueued);
312    status_t setZSLChannelAttribute(void);
313    void handleError();
314    void setSnapshotState(int state);
315    void setModeLiveSnapshot(bool);
316    bool isLiveSnapshot(void);
317    void stopPolling(void);
318    bool isFullSizeLiveshot(void);
319    status_t doWaveletDenoise(mm_camera_ch_data_buf_t* frame);
320    status_t sendWDenoiseStartMsg(mm_camera_ch_data_buf_t * frame);
321    void lauchNextWDenoiseFromQueue();
322
323    /* Member variables */
324
325    int mSnapshotFormat;
326    int mPictureWidth;
327    int mPictureHeight;
328    cam_format_t mPictureFormat;
329    int mPostviewWidth;
330    int mPostviewHeight;
331    int mThumbnailWidth;
332    int mThumbnailHeight;
333    cam_format_t mThumbnailFormat;
334	int mJpegOffset;
335    int mSnapshotState;
336    int mNumOfSnapshot;
337	int mNumOfRecievedJPEG;
338    bool mModeLiveSnapshot;
339    bool mBurstModeFlag;
340	int mActualPictureWidth;
341    int mActualPictureHeight;
342    bool mJpegDownscaling;
343    sp<AshmemPool> mJpegHeap;
344    /*TBD:Bikas: This is defined in HWI too.*/
345#ifdef USE_ION
346    sp<IonPool>  mDisplayHeap;
347    sp<IonPool>  mPostviewHeap;
348#else
349    sp<PmemPool>  mDisplayHeap;
350    sp<PmemPool>  mPostviewHeap;
351#endif
352    mm_camera_ch_data_buf_t *mCurrentFrameEncoded;
353    mm_cameara_stream_buf_t mSnapshotStreamBuf;
354    mm_cameara_stream_buf_t mPostviewStreamBuf;
355    StreamQueue             mSnapshotQueue;
356    static const int        mMaxSnapshotBufferCount = 16;
357    int                     mSnapshotBufferNum;
358    int                     mMainfd[mMaxSnapshotBufferCount];
359    int                     mThumbfd[mMaxSnapshotBufferCount];
360    int                     mMainSize;
361    int                     mThumbSize;
362	camera_memory_t        *mCameraMemoryPtrMain[mMaxSnapshotBufferCount];
363	camera_memory_t        *mCameraMemoryPtrThumb[mMaxSnapshotBufferCount];
364    int                     mJpegSessionId;
365	int                     dump_fd;
366    bool mFullLiveshot;
367    StreamQueue             mWDNQueue; // queue to hold frames while one frame is sent out for WDN
368    bool                    mIsDoingWDN; // flag to indicate if WDN is going on (one frame is sent out for WDN)
369	bool                    mDropThumbnail;
370	int                     mJpegQuality;
371}; // QCameraStream_Snapshot
372
373
374}; // namespace android
375
376#endif
377