1 /*
2 Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
3 
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are
6 met:
7     * Redistributions of source code must retain the above copyright
8       notice, this list of conditions and the following disclaimer.
9     * Redistributions in binary form must reproduce the above
10       copyright notice, this list of conditions and the following
11       disclaimer in the documentation and/or other materials provided
12       with the distribution.
13     * Neither the name of The Linux Foundation nor the names of its
14       contributors may be used to endorse or promote products derived
15       from this software without specific prior written permission.
16 
17 THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 
30 #include <pthread.h>
31 #include <errno.h>
32 #include <sys/ioctl.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include <poll.h>
37 #include "mm_camera_dbg.h"
38 #include "mm_qcamera_app.h"
39 
40 #define BUFF_SIZE_128 128
41 
42 #ifdef TEST_ABORT_JPEG_ENCODE
43 static uint8_t aborted_flag = 1;
44 #endif
45 
46 //static mm_camera_ch_data_buf_t *mCurrentFrameEncoded;
47 static int JpegOffset = 0;
48 static int raw_snapshot_cnt = 0;
49 static int snapshot_cnt = 0;
50 static pthread_mutex_t g_s_mutex;
51 static int g_status = 0;
52 static pthread_cond_t g_s_cond_v;
53 
54 extern void dumpFrameToFile(mm_camera_buf_def_t* newFrame, int w, int h, char* name, int main_422,char *ext);
55 extern int mm_app_open_zsl(int cam_id);
56 extern int mm_app_open_camera(int cam_id);
57 extern int mm_app_start_preview(int cam_id);
58 extern int mm_stream_alloc_bufs(mm_camera_app_obj_t *pme,
59                                 mm_camear_app_buf_t* app_bufs,
60                                 mm_camera_frame_len_offset *frame_offset_info,
61                                 uint8_t num_bufs);
62 extern int mm_stream_release_bufs(mm_camera_app_obj_t *pme,
63                                   mm_camear_app_buf_t* app_bufs);
64 extern int mm_stream_invalid_cache(mm_camera_app_obj_t *pme,
65                                    mm_camera_buf_def_t *frame);
66 
mm_app_snapshot_done()67 static void mm_app_snapshot_done()
68 {
69     pthread_mutex_lock(&g_s_mutex);
70     g_status = TRUE;
71     pthread_cond_signal(&g_s_cond_v);
72     pthread_mutex_unlock(&g_s_mutex);
73 }
74 
mm_app_dump_snapshot_frame(struct msm_frame * frame,uint32_t len,int is_main,int is_raw)75 static int mm_app_dump_snapshot_frame(struct msm_frame *frame,
76                                       uint32_t len, int is_main, int is_raw)
77 {
78     char bufp[BUFF_SIZE_128];
79     int file_fdp;
80     int rc = 0;
81 
82     if (is_raw) {
83         snprintf(bufp, BUFF_SIZE_128, "/data/main_raw_%d.yuv", raw_snapshot_cnt);
84     } else {
85         if (is_main) {
86             snprintf(bufp, BUFF_SIZE_128, "/data/main_%d.yuv", snapshot_cnt);
87         } else {
88             snprintf(bufp, BUFF_SIZE_128, "/data/thumb_%d.yuv", snapshot_cnt);
89         }
90     }
91 
92     file_fdp = open(bufp, O_RDWR | O_CREAT, 0777);
93 
94     if (file_fdp < 0) {
95         CDBG("cannot open file %s\n", bufp);
96         rc = -1;
97         goto end;
98     }
99     CDBG("%s:dump snapshot frame to '%s'\n", __func__, bufp);
100     write(file_fdp,
101           (const void *)frame->buffer, len);
102     close(file_fdp);
103     end:
104     return rc;
105 }
106 
107 
mm_app_dump_jpeg_frame(const void * data,uint32_t size,char * name,char * ext,int index)108 static void mm_app_dump_jpeg_frame(const void * data, uint32_t size, char* name, char* ext, int index)
109 {
110     char buf[32];
111     int file_fd;
112     if ( data != NULL) {
113         char * str;
114         snprintf(buf, sizeof(buf), "/data/%s_%d.%s", name, index, ext);
115         CDBG("%s: %s size =%d, jobId=%d", __func__, buf, size, index);
116         file_fd = open(buf, O_RDWR | O_CREAT, 0777);
117         write(file_fd, data, size);
118         close(file_fd);
119     }
120 }
121 
mm_app_set_thumbnail_fmt(int cam_id,mm_camera_image_fmt_t * fmt)122 static int mm_app_set_thumbnail_fmt(int cam_id, mm_camera_image_fmt_t *fmt)
123 {
124     int rc = MM_CAMERA_OK;
125     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
126 
127     fmt->meta_header = MM_CAMEAR_META_DATA_TYPE_DEF;
128     fmt->fmt = pme->dim.thumb_format;
129     fmt->width = pme->dim.thumbnail_width;
130     fmt->height = pme->dim.thumbnail_height;
131     if (cam_id == 0) {
132         /* back camera, rotate 90 */
133         fmt->rotation = 90;
134     }
135     CDBG("Thumbnail Dimension = %dX%d",fmt->width,fmt->height);
136     return rc;
137 }
138 
mm_app_set_snapshot_fmt(int cam_id,mm_camera_image_fmt_t * fmt)139 int mm_app_set_snapshot_fmt(int cam_id, mm_camera_image_fmt_t *fmt)
140 {
141 
142     int rc = MM_CAMERA_OK;
143     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
144     fmt->meta_header = MM_CAMEAR_META_DATA_TYPE_DEF;
145     fmt->fmt = pme->dim.main_img_format;
146     fmt->width = pme->dim.picture_width;
147     fmt->height = pme->dim.picture_height;
148     if (cam_id == 0) {
149         /* back camera, rotate 90 */
150         fmt->rotation = 90;
151     }
152     CDBG("Snapshot Dimension = %dX%d",fmt->width,fmt->height);
153     return rc;
154 }
155 
mm_app_set_live_snapshot_fmt(int cam_id,mm_camera_image_fmt_t * fmt)156 int mm_app_set_live_snapshot_fmt(int cam_id, mm_camera_image_fmt_t *fmt)
157 {
158     int rc = MM_CAMERA_OK;
159     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
160     fmt->meta_header = MM_CAMEAR_META_DATA_TYPE_DEF;
161     fmt->fmt = pme->dim.enc_format;
162     fmt->width = pme->dim.video_width;
163     fmt->height = pme->dim.video_height;
164     CDBG("Livesnapshot Dimension = %dX%d",fmt->width,fmt->height);
165     return rc;
166 }
167 
mm_app_set_raw_snapshot_fmt(int cam_id,mm_camera_image_fmt_t * fmt)168 int mm_app_set_raw_snapshot_fmt(int cam_id,mm_camera_image_fmt_t *fmt)
169 {
170     int rc = MM_CAMERA_OK;
171     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
172 
173     fmt->meta_header = MM_CAMEAR_META_DATA_TYPE_DEF;
174     fmt->fmt = pme->dim.raw_img_format;
175     fmt->width = pme->dim.raw_picture_width;
176     fmt->height = pme->dim.raw_picture_height;
177     if (cam_id == 0) {
178         /* back camera, rotate 90 */
179         fmt->rotation = 90;
180     }
181     CDBG("%s: Raw Snapshot Dimension = %d X %d",__func__,pme->dim.raw_picture_width,pme->dim.raw_picture_height);
182     return rc;
183 }
184 
185 /* This callback is received once the complete JPEG encoding is done */
jpeg_encode_cb(jpeg_job_status_t status,uint8_t thumbnailDroppedFlag,uint32_t client_hdl,uint32_t jobId,uint8_t * out_data,uint32_t data_size,void * userData)186 static void jpeg_encode_cb(jpeg_job_status_t status,
187                            uint8_t thumbnailDroppedFlag,
188                            uint32_t client_hdl,
189                            uint32_t jobId,
190                            uint8_t* out_data,
191                            uint32_t data_size,
192                            void *userData)
193 {
194     int rc;
195     int i = 0;
196     mm_camera_buf_def_t *main_frame = NULL;
197     mm_camera_buf_def_t *thumb_frame = NULL;
198     mm_camera_app_obj_t *pme = NULL;
199     CDBG("%s: BEGIN\n", __func__);
200 
201     pme = (mm_camera_app_obj_t *)userData;
202     if (jobId != pme->current_job_id || !pme->current_job_frames) {
203         CDBG_ERROR("%s: NULL current job frames or not matching job ID (%d, %d)",
204                    __func__, jobId, pme->current_job_id);
205         return;
206     }
207 
208     /* dump jpeg img */
209     CDBG_ERROR("%s: job %d, status=%d, thumbnail_dropped=%d",
210                __func__, jobId, status, thumbnailDroppedFlag);
211     if (status == JPEG_JOB_STATUS_DONE) {
212         mm_app_dump_jpeg_frame(out_data, data_size, "jpeg_dump", "jpg", pme->my_id);
213     }
214 
215     /* buf done current encoding frames */
216     pme->current_job_id = 0;
217     for (i=0; i<pme->current_job_frames->num_bufs; i++) {
218         if (MM_CAMERA_OK != pme->cam->ops->qbuf(pme->current_job_frames->camera_handle,
219                                                 pme->current_job_frames->ch_id,
220                                                 pme->current_job_frames->bufs[i])) {
221             CDBG_ERROR("%s: Failed in Qbuf\n", __func__);
222         }
223     }
224     free(pme->current_job_frames);
225     pme->current_job_frames = NULL;
226 
227     /* signal snapshot is done */
228     mm_app_snapshot_done();
229 }
230 
encodeData(mm_camera_app_obj_t * pme,mm_camera_super_buf_t * recvd_frame)231 static int encodeData(mm_camera_app_obj_t *pme,
232                       mm_camera_super_buf_t* recvd_frame)
233 {
234     int rc = -1;
235     int i,index = -1;
236     mm_jpeg_job job;
237     mm_camera_buf_def_t *main_frame = NULL;
238     mm_camera_buf_def_t *thumb_frame = NULL;
239     src_image_buffer_info* main_buf_info = NULL;
240     src_image_buffer_info* thumb_buf_info = NULL;
241     mm_camera_frame_len_offset main_offset;
242     mm_camera_frame_len_offset thumb_offset;
243 
244     /* dump raw img for debug purpose */
245     CDBG("%s : total streams = %d",__func__,recvd_frame->num_bufs);
246     for (i=0; i<recvd_frame->num_bufs; i++) {
247         if (pme->stream[MM_CAMERA_SNAPSHOT_MAIN].id == recvd_frame->bufs[i]->stream_id) {
248             main_frame = recvd_frame->bufs[i];
249             CDBG("mainframe frame_idx = %d fd = %d frame length = %d",main_frame->frame_idx,main_frame->fd,main_frame->frame_len);
250             dumpFrameToFile(main_frame,pme->dim.picture_width,pme->dim.picture_height,"main", 1,"yuv");
251         } else if (pme->stream[MM_CAMERA_SNAPSHOT_THUMBNAIL].id == recvd_frame->bufs[i]->stream_id){
252             thumb_frame = recvd_frame->bufs[1];
253             CDBG("thumnail frame_idx = %d fd = %d frame length = %d",thumb_frame->frame_idx,thumb_frame->fd,thumb_frame->frame_len);
254             dumpFrameToFile(thumb_frame,pme->dim.thumbnail_width,pme->dim.thumbnail_height,"thumb", 1,"yuv");
255         }
256     }
257 
258     if (main_frame == NULL) {
259         CDBG_ERROR("%s: Main frame is NULL", __func__);
260         return rc;
261     }
262 
263     /* remember current frames being encoded */
264     pme->current_job_frames =
265         (mm_camera_super_buf_t *)malloc(sizeof(mm_camera_super_buf_t));
266     if (!pme->current_job_frames) {
267         CDBG_ERROR("%s: No memory for current_job_frames", __func__);
268         return rc;
269     }
270     memcpy(pme->current_job_frames, recvd_frame, sizeof(mm_camera_super_buf_t));
271 
272     memset(&job, 0, sizeof(job));
273     job.job_type = JPEG_JOB_TYPE_ENCODE;
274     job.encode_job.jpeg_cb = jpeg_encode_cb;
275     job.encode_job.userdata = (void*)pme;
276     job.encode_job.encode_parm.exif_data = NULL;
277     job.encode_job.encode_parm.exif_numEntries = 0;
278     job.encode_job.encode_parm.rotation = 0;
279     job.encode_job.encode_parm.buf_info.src_imgs.src_img_num = recvd_frame->num_bufs;
280     job.encode_job.encode_parm.rotation = 0;
281     if (pme->my_id == 0) {
282         /* back camera, rotate 90 */
283         job.encode_job.encode_parm.rotation = 90;
284     }
285 
286     /* fill in main src img encode param */
287     main_buf_info = &job.encode_job.encode_parm.buf_info.src_imgs.src_img[JPEG_SRC_IMAGE_TYPE_MAIN];
288     main_buf_info->type = JPEG_SRC_IMAGE_TYPE_MAIN;
289     main_buf_info->color_format = MM_JPEG_COLOR_FORMAT_YCRCBLP_H2V2; //TODO
290     main_buf_info->quality = 85;
291     main_buf_info->src_dim.width = pme->dim.picture_width;
292     main_buf_info->src_dim.height = pme->dim.picture_height;
293     main_buf_info->out_dim.width = pme->dim.picture_width;
294     main_buf_info->out_dim.height = pme->dim.picture_height;
295     main_buf_info->crop.width = pme->dim.picture_width;
296     main_buf_info->crop.height = pme->dim.picture_height;
297     main_buf_info->crop.offset_x = 0;
298     main_buf_info->crop.offset_y = 0;
299     main_buf_info->img_fmt = JPEG_SRC_IMAGE_FMT_YUV;
300     main_buf_info->num_bufs = 1;
301     main_buf_info->src_image[0].fd = main_frame->fd;
302     main_buf_info->src_image[0].buf_vaddr = main_frame->buffer;
303     pme->cam->ops->get_stream_parm(pme->cam->camera_handle,
304                                    pme->ch_id,
305                                    pme->stream[MM_CAMERA_SNAPSHOT_MAIN].id,
306                                    MM_CAMERA_STREAM_OFFSET,
307                                    &main_buf_info->src_image[0].offset);
308     CDBG("%s: main offset: num_planes=%d, frame length=%d, y_offset=%d, cbcr_offset=%d",
309          __func__, main_buf_info->src_image[0].offset.num_planes,
310          main_buf_info->src_image[0].offset.frame_len,
311          main_buf_info->src_image[0].offset.mp[0].offset,
312          main_buf_info->src_image[0].offset.mp[1].offset);
313 
314     mm_stream_clear_invalid_cache(pme,main_frame);
315 
316     if (thumb_frame) {
317         /* fill in thumbnail src img encode param */
318         thumb_buf_info = &job.encode_job.encode_parm.buf_info.src_imgs.src_img[JPEG_SRC_IMAGE_TYPE_THUMB];
319         thumb_buf_info->type = JPEG_SRC_IMAGE_TYPE_THUMB;
320         thumb_buf_info->color_format = MM_JPEG_COLOR_FORMAT_YCRCBLP_H2V2; //TODO
321         thumb_buf_info->quality = 85;
322         thumb_buf_info->src_dim.width = pme->dim.thumbnail_width;
323         thumb_buf_info->src_dim.height = pme->dim.thumbnail_height;
324         thumb_buf_info->out_dim.width = pme->dim.thumbnail_width;
325         thumb_buf_info->out_dim.height = pme->dim.thumbnail_height;
326         thumb_buf_info->crop.width = pme->dim.thumbnail_width;
327         thumb_buf_info->crop.height = pme->dim.thumbnail_height;
328         thumb_buf_info->crop.offset_x = 0;
329         thumb_buf_info->crop.offset_y = 0;
330         thumb_buf_info->img_fmt = JPEG_SRC_IMAGE_FMT_YUV;
331         thumb_buf_info->num_bufs = 1;
332         thumb_buf_info->src_image[0].fd = thumb_frame->fd;
333         thumb_buf_info->src_image[0].buf_vaddr = thumb_frame->buffer;
334         pme->cam->ops->get_stream_parm(pme->cam->camera_handle,
335                                        pme->ch_id,
336                                        pme->stream[MM_CAMERA_SNAPSHOT_THUMBNAIL].id,
337                                        MM_CAMERA_STREAM_OFFSET,
338                                        &thumb_buf_info->src_image[0].offset);
339     }
340 
341     /* fill in sink img param */
342     job.encode_job.encode_parm.buf_info.sink_img.buf_len = pme->jpeg_buf.bufs[0].frame_len;
343     job.encode_job.encode_parm.buf_info.sink_img.buf_vaddr = pme->jpeg_buf.bufs[0].buffer;
344     job.encode_job.encode_parm.buf_info.sink_img.fd = pme->jpeg_buf.bufs[0].fd;
345 
346     mm_stream_clear_invalid_cache(pme,thumb_frame);
347 
348     rc = pme->jpeg_ops.start_job(pme->jpeg_hdl, &job, &pme->current_job_id);
349     if ( 0 != rc ) {
350         free(pme->current_job_frames);
351         pme->current_job_frames = NULL;
352     }
353 
354     return rc;
355 }
356 
snapshot_yuv_cb(mm_camera_super_buf_t * bufs,void * user_data)357 static void snapshot_yuv_cb(mm_camera_super_buf_t *bufs,
358                             void *user_data)
359 {
360 
361     int rc;
362     int i = 0;
363     mm_camera_buf_def_t *main_frame = NULL;
364     mm_camera_buf_def_t *thumb_frame = NULL;
365     mm_camera_app_obj_t *pme = NULL;
366     char* cmd = "This is a private cmd from test app";
367 
368     CDBG("%s: BEGIN\n", __func__);
369 
370     pme = (mm_camera_app_obj_t *)user_data;
371 
372     CDBG("%s: send private ioctl here", __func__);
373     pme->cam->ops->send_command(bufs->camera_handle,
374                                 MM_CAMERA_CMD_TYPE_PRIVATE,
375                                 0,
376                                 strlen(cmd) + 1,
377                                 (void *)cmd);
378 
379     /* start jpeg encoding job */
380     rc = encodeData(pme, bufs);
381 
382     /* buf done rcvd frames in error case */
383     if ( 0 != rc ) {
384         for (i=0; i<bufs->num_bufs; i++) {
385             if (MM_CAMERA_OK != pme->cam->ops->qbuf(bufs->camera_handle,
386                                                     bufs->ch_id,
387                                                     bufs->bufs[i])) {
388                 CDBG_ERROR("%s: Failed in Qbuf\n", __func__);
389             }
390             mm_stream_invalid_cache(pme,bufs->bufs[i]);
391         }
392     }
393 #ifdef TEST_ABORT_JPEG_ENCODE
394     else {
395         if (aborted_flag) {
396             aborted_flag = 0;
397             /* abort the job */
398             CDBG("%s: abort jpeg encode job  here", __func__);
399             rc = pme->jpeg_ops.abort_job(pme->jpeg_hdl, pme->current_job_id);
400             if (NULL != pme->current_job_frames) {
401                 free(pme->current_job_frames);
402                 pme->current_job_frames = NULL;
403             }
404             CDBG("%s: abort jpeg encode job returns %d", __func__, rc);
405             for (i=0; i<bufs->num_bufs; i++) {
406                 if (MM_CAMERA_OK != pme->cam->ops->qbuf(bufs->camera_handle,
407                                                         bufs->ch_id,
408                                                         bufs->bufs[i])) {
409                     CDBG_ERROR("%s: Failed in Qbuf\n", __func__);
410                 }
411                 mm_stream_invalid_cache(pme,bufs->bufs[i]);
412             }
413 
414             /* signal snapshot is done */
415             mm_app_snapshot_done();
416         }
417     }
418 #endif
419 
420     CDBG("%s: END\n", __func__);
421 }
422 
snapshot_raw_cb(mm_camera_super_buf_t * recvd_frame,void * user_data)423 static void snapshot_raw_cb(mm_camera_super_buf_t *recvd_frame,
424                             void *user_data)
425 {
426     int rc;
427     int i = 0;
428     mm_camera_buf_def_t *main_frame = NULL;
429     mm_camera_app_obj_t *pme = NULL;
430 
431     CDBG("%s: BEGIN\n", __func__);
432 
433     pme = (mm_camera_app_obj_t *)user_data;
434 
435      if (pme->stream[MM_CAMERA_SNAPSHOT_MAIN].id == recvd_frame->bufs[i]->stream_id) {
436          main_frame = recvd_frame->bufs[i];
437          CDBG("mainframe frame_idx = %d fd = %d frame length = %d",main_frame->frame_idx,main_frame->fd,main_frame->frame_len);
438          dumpFrameToFile(main_frame,pme->dim.raw_picture_width,pme->dim.raw_picture_height,"raw_main", 1,"raw");
439      }
440      if (MM_CAMERA_OK != pme->cam->ops->qbuf(pme->cam->camera_handle,pme->ch_id,
441                                                 main_frame)) {
442         CDBG_ERROR("%s: Failed in Qbuf\n", __func__);
443      }
444 
445      mm_app_snapshot_done();
446 }
447 
encodeDisplayAndSave(mm_camera_super_buf_t * recvd_frame,int enqueued,mm_camera_app_obj_t * pme)448 static int encodeDisplayAndSave(mm_camera_super_buf_t* recvd_frame,
449                                 int enqueued, mm_camera_app_obj_t *pme)
450 {
451     int ret = -1;
452 #if 0
453 
454 
455     CDBG("%s: Send frame for encoding", __func__);
456     ret = encodeData(recvd_frame, pme->snapshot_buf.frame_len,
457                      enqueued, pme);
458     if (!ret) {
459         CDBG_ERROR("%s: Failure configuring JPEG encoder", __func__);
460     }
461 
462     LOGD("%s: X", __func__);
463 #endif
464     return ret;
465 }
466 
mm_app_add_snapshot_stream(int cam_id)467 int mm_app_add_snapshot_stream(int cam_id)
468 {
469     int rc = MM_CAMERA_OK;
470     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
471 
472     pme->stream[MM_CAMERA_SNAPSHOT_MAIN].id = pme->cam->ops->add_stream(pme->cam->camera_handle,pme->ch_id,
473                                                                         NULL,pme,
474                                                                         MM_CAMERA_SNAPSHOT_MAIN, 0);
475 
476     CDBG("Add Snapshot main is successfull stream ID = %d",pme->stream[MM_CAMERA_SNAPSHOT_MAIN].id);
477     if (!pme->stream[MM_CAMERA_SNAPSHOT_MAIN].id) {
478         CDBG_ERROR("%s:snapshot main streaming err=%d\n", __func__, rc);
479         rc = -1;
480         goto end;
481     }
482 
483     pme->stream[MM_CAMERA_SNAPSHOT_THUMBNAIL].id = pme->cam->ops->add_stream(pme->cam->camera_handle,pme->ch_id,
484                                                                              NULL,pme,
485                                                                              MM_CAMERA_SNAPSHOT_THUMBNAIL, 0);
486     if (!pme->stream[MM_CAMERA_SNAPSHOT_THUMBNAIL].id) {
487         CDBG_ERROR("%s:snapshot thumbnail streaming err=%d\n", __func__, rc);
488         rc = -1;
489         goto end;
490     }
491     end:
492     CDBG("%s: END, rc=%d\n", __func__, rc);
493     return rc;
494 }
495 
mm_app_set_snapshot_mode(int cam_id,int op_mode)496 void mm_app_set_snapshot_mode(int cam_id,int op_mode)
497 {
498     denoise_param_t wnr;
499     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
500     pme->cam->ops->set_parm(pme->cam->camera_handle,MM_CAMERA_PARM_OP_MODE, &op_mode);
501 
502     /* set wnr enabled */
503     memset(&wnr, 0, sizeof(denoise_param_t));
504     wnr.denoise_enable = 0;
505     wnr.process_plates = 0;
506  //   pme->cam->ops->set_parm(pme->cam->camera_handle, MM_CAMERA_PARM_WAVELET_DENOISE, &wnr);
507 }
508 
mm_app_config_raw_format(int cam_id)509 int mm_app_config_raw_format(int cam_id)
510 {
511     int rc = MM_CAMERA_OK;
512     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
513     mm_camera_image_fmt_t *m_fmt = NULL;
514     mm_camera_image_fmt_t *t_fmt = NULL;
515 
516     mm_app_set_raw_snapshot_fmt(cam_id,&pme->stream[MM_CAMERA_SNAPSHOT_MAIN].str_config.fmt);
517 
518     pme->stream[MM_CAMERA_SNAPSHOT_MAIN].str_config.need_stream_on = 1;
519     pme->stream[MM_CAMERA_SNAPSHOT_MAIN].str_config.num_of_bufs = 1;
520 
521     if (MM_CAMERA_OK != (rc = pme->cam->ops->config_stream(pme->cam->camera_handle,pme->ch_id,pme->stream[MM_CAMERA_SNAPSHOT_MAIN].id,
522                                                            &pme->stream[MM_CAMERA_SNAPSHOT_MAIN].str_config))) {
523         CDBG_ERROR("%s:preview streaming err=%d\n", __func__, rc);
524         goto end;
525     }
526 
527 end:
528     CDBG("%s: END, rc=%d\n", __func__, rc);
529     return rc;
530 
531 }
532 
mm_app_config_snapshot_format(int cam_id)533 int mm_app_config_snapshot_format(int cam_id)
534 {
535     int rc = MM_CAMERA_OK;
536     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
537 
538     mm_app_set_snapshot_fmt(cam_id,&pme->stream[MM_CAMERA_SNAPSHOT_MAIN].str_config.fmt);
539 
540     mm_app_set_thumbnail_fmt(cam_id,&pme->stream[MM_CAMERA_SNAPSHOT_THUMBNAIL].str_config.fmt);
541 
542     pme->stream[MM_CAMERA_SNAPSHOT_MAIN].str_config.need_stream_on = 1;
543     pme->stream[MM_CAMERA_SNAPSHOT_MAIN].str_config.num_of_bufs = 1;
544 
545     if (MM_CAMERA_OK != (rc = pme->cam->ops->config_stream(pme->cam->camera_handle,pme->ch_id,pme->stream[MM_CAMERA_SNAPSHOT_MAIN].id,
546                                                            &pme->stream[MM_CAMERA_SNAPSHOT_MAIN].str_config))) {
547         CDBG_ERROR("%s:preview streaming err=%d\n", __func__, rc);
548         goto end;
549     }
550 
551     pme->stream[MM_CAMERA_SNAPSHOT_THUMBNAIL].str_config.need_stream_on = 1;
552     pme->stream[MM_CAMERA_SNAPSHOT_THUMBNAIL].str_config.num_of_bufs = 1;
553 
554     if (MM_CAMERA_OK != (rc = pme->cam->ops->config_stream(pme->cam->camera_handle,pme->ch_id,pme->stream[MM_CAMERA_SNAPSHOT_THUMBNAIL].id,
555                                                            &pme->stream[MM_CAMERA_SNAPSHOT_THUMBNAIL].str_config))) {
556         CDBG_ERROR("%s:preview streaming err=%d\n", __func__, rc);
557         goto end;
558     }
559     end:
560     CDBG("%s: END, rc=%d\n", __func__, rc);
561     return rc;
562 
563 }
564 
mm_app_streamon_snapshot(int cam_id)565 int mm_app_streamon_snapshot(int cam_id)
566 {
567     int rc = MM_CAMERA_OK;
568     uint32_t stream[2];
569     mm_camera_bundle_attr_t attr;
570 
571     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
572 
573     stream[0] = pme->stream[MM_CAMERA_SNAPSHOT_MAIN].id;
574     stream[1] = pme->stream[MM_CAMERA_SNAPSHOT_THUMBNAIL].id;
575 
576     attr.notify_mode = MM_CAMERA_SUPER_BUF_NOTIFY_BURST;
577     attr.burst_num = 1;
578     attr.look_back = 2;
579     attr.post_frame_skip = 0;
580     attr.water_mark = 2;
581 
582     if (MM_CAMERA_OK != (rc = pme->cam->ops->init_stream_bundle(
583                                                                pme->cam->camera_handle,pme->ch_id,snapshot_yuv_cb,pme,&attr,2,stream))) {
584         CDBG_ERROR("%s:init_stream_bundle err=%d\n", __func__, rc);
585         goto end;
586     }
587 
588     if (MM_CAMERA_OK != (rc = pme->cam->ops->start_streams(pme->cam->camera_handle,pme->ch_id,
589                                                            2, stream))) {
590         CDBG_ERROR("%s:start_streams err=%d\n", __func__, rc);
591         goto end;
592     }
593 
594     if (MM_CAMERA_OK != (rc =pme->cam->ops->request_super_buf(pme->cam->camera_handle,pme->ch_id, attr.burst_num))) {
595         CDBG_ERROR("%s:request_super_buf err=%d\n", __func__, rc);
596         goto end;
597     }
598     pme->cam_state = CAMERA_STATE_SNAPSHOT;
599     end:
600     CDBG("%s: END, rc=%d\n", __func__, rc);
601     return rc;
602 }
603 
mm_app_streamon_raw(int cam_id)604 int mm_app_streamon_raw(int cam_id)
605 {
606     int rc = MM_CAMERA_OK;
607     uint32_t stream[2];
608     mm_camera_bundle_attr_t attr;
609 
610     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
611 
612     stream[0] = pme->stream[MM_CAMERA_SNAPSHOT_MAIN].id;
613 
614     attr.notify_mode = MM_CAMERA_SUPER_BUF_NOTIFY_BURST;
615     attr.burst_num = 1;
616     attr.look_back = 2;
617     attr.post_frame_skip = 0;
618     attr.water_mark = 2;
619 
620     if (MM_CAMERA_OK != (rc = pme->cam->ops->init_stream_bundle(
621                                                                pme->cam->camera_handle,pme->ch_id,snapshot_raw_cb,pme,&attr,1,stream))) {
622         CDBG_ERROR("%s:init_stream_bundle err=%d\n", __func__, rc);
623         goto end;
624     }
625 
626     if (MM_CAMERA_OK != (rc = pme->cam->ops->start_streams(pme->cam->camera_handle,pme->ch_id,
627                                                            1, stream))) {
628         CDBG_ERROR("%s:start_streams err=%d\n", __func__, rc);
629         goto end;
630     }
631 
632     if (MM_CAMERA_OK != (rc =pme->cam->ops->request_super_buf(pme->cam->camera_handle,pme->ch_id, attr.burst_num))) {
633         CDBG_ERROR("%s:request_super_buf err=%d\n", __func__, rc);
634         goto end;
635     }
636     pme->cam_state = CAMERA_STATE_SNAPSHOT;
637 end:
638     CDBG("%s: END, rc=%d\n", __func__, rc);
639     return rc;
640 }
641 
mm_app_streamoff_snapshot(int cam_id)642 int mm_app_streamoff_snapshot(int cam_id)
643 {
644     int rc = MM_CAMERA_OK;
645     uint32_t stream[2];
646 
647     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
648 
649     stream[0] = pme->stream[MM_CAMERA_SNAPSHOT_MAIN].id;
650     stream[1] = pme->stream[MM_CAMERA_SNAPSHOT_THUMBNAIL].id;
651 
652     if (MM_CAMERA_OK != (rc = pme->cam->ops->stop_streams(pme->cam->camera_handle,pme->ch_id,2,stream))) {
653         CDBG_ERROR("%s : Snapshot Stream off Error",__func__);
654         goto end;
655     }
656     CDBG("Stop snapshot main successfull");
657 
658     if (MM_CAMERA_OK != (rc = pme->cam->ops->destroy_stream_bundle(pme->cam->camera_handle,pme->ch_id))) {
659         CDBG_ERROR("%s : Snapshot destroy_stream_bundle Error",__func__);
660         goto end;
661     }
662 
663     if (MM_CAMERA_OK != (rc = pme->cam->ops->del_stream(pme->cam->camera_handle,pme->ch_id,pme->stream[MM_CAMERA_SNAPSHOT_MAIN].id))) {
664         CDBG_ERROR("%s : Snapshot main image del_stream Error",__func__);
665         goto end;
666     }
667     CDBG("del_stream successfull");
668 
669     if (MM_CAMERA_OK != (rc = pme->cam->ops->del_stream(pme->cam->camera_handle,pme->ch_id,pme->stream[MM_CAMERA_SNAPSHOT_THUMBNAIL].id))) {
670         CDBG_ERROR("%s : Snapshot thumnail image del_stream Error",__func__);
671         goto end;
672     }
673     CDBG("del_stream successfull");
674 
675     end:
676     return rc;
677 }
678 
mm_app_start_snapshot(int cam_id)679 int mm_app_start_snapshot(int cam_id)
680 {
681     int rc = MM_CAMERA_OK;
682     int stream[2];
683     int op_mode = 0;
684     uint8_t initial_reg_flag;
685     mm_camera_frame_len_offset frame_offset_info;
686 
687     mm_camera_bundle_attr_t attr;
688 
689 
690     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
691 
692     if (MM_CAMERA_OK != mm_app_stop_preview(cam_id)) {
693         CDBG_ERROR("%s: Stop preview Failed cam_id=%d\n",__func__,cam_id);
694         return -1;
695     }
696     op_mode = MM_CAMERA_OP_MODE_CAPTURE;
697     mm_app_set_snapshot_mode(cam_id,op_mode);
698     usleep(20*1000);
699 //    pme->cam->ops->prepare_snapshot(pme->cam->camera_handle,pme->ch_id,0);
700 
701     if (MM_CAMERA_OK != (rc = mm_app_add_snapshot_stream(cam_id))) {
702         CDBG_ERROR("%s : Add Snapshot stream err",__func__);
703         return rc;
704     }
705 
706     if (MM_CAMERA_OK != (rc = mm_app_config_snapshot_format(cam_id))) {
707         CDBG_ERROR("%s : Config Snapshot stream err",__func__);
708         return rc;
709     }
710 
711     if (MM_CAMERA_OK != (rc = mm_app_streamon_snapshot(cam_id))) {
712         CDBG_ERROR("%s : Stream on Snapshot stream err",__func__);
713         return rc;
714     }
715 
716     /* init jpeg buf */
717     pme->cam->ops->get_stream_parm(pme->cam->camera_handle,
718                                    pme->ch_id,
719                                    pme->stream[MM_CAMERA_SNAPSHOT_MAIN].id,
720                                    MM_CAMERA_STREAM_OFFSET,
721                                    &frame_offset_info);
722     CDBG_ERROR("%s : alloc jpeg buf (len=%d)",__func__, frame_offset_info.frame_len);
723     rc = mm_stream_alloc_bufs(pme,
724                               &pme->jpeg_buf,
725                               &frame_offset_info,
726                               1);
727     if (0 != rc) {
728         CDBG_ERROR("%s : mm_stream_alloc_bufs err",__func__);
729         return rc;
730     }
731 
732     CDBG("%s: END, rc=%d\n", __func__, rc);
733     return rc;
734 }
735 
mm_app_start_raw(int cam_id)736 int mm_app_start_raw(int cam_id)
737 {
738     int rc = MM_CAMERA_OK;
739     int op_mode = 0;
740 
741     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
742 
743     if (MM_CAMERA_OK != mm_app_stop_preview(cam_id)) {
744         CDBG_ERROR("%s: Stop preview Failed cam_id=%d\n",__func__,cam_id);
745         return -1;
746     }
747     op_mode = MM_CAMERA_OP_MODE_RAW;
748     pme->cam->ops->set_parm(pme->cam->camera_handle,MM_CAMERA_PARM_OP_MODE, &op_mode);
749     usleep(20*1000);
750 
751     pme->stream[MM_CAMERA_SNAPSHOT_MAIN].id = pme->cam->ops->add_stream(pme->cam->camera_handle,pme->ch_id,
752                                                                         NULL,pme,
753                                                                         MM_CAMERA_SNAPSHOT_MAIN, 0);
754 
755     CDBG("Add RAW main is successfull stream ID = %d",pme->stream[MM_CAMERA_SNAPSHOT_MAIN].id);
756     if (!pme->stream[MM_CAMERA_SNAPSHOT_MAIN].id) {
757         CDBG_ERROR("%s:Raw main streaming err=%d\n", __func__, rc);
758         rc = -1;
759         return rc;
760     }
761 
762     if (MM_CAMERA_OK != (rc = mm_app_config_raw_format(cam_id))) {
763         CDBG_ERROR("%s : Config Raw Snapshot stream err",__func__);
764         return rc;
765     }
766 
767     if (MM_CAMERA_OK != (rc = mm_app_streamon_raw(cam_id))) {
768         CDBG_ERROR("%s : Stream on Raw Snapshot stream err",__func__);
769         return rc;
770     }
771 
772     CDBG("%s: END, rc=%d\n", __func__, rc);
773     return rc;
774 }
775 
mm_app_stop_snapshot(int cam_id)776 int mm_app_stop_snapshot(int cam_id)
777 {
778     int rc = MM_CAMERA_OK;
779     int i;
780 
781     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
782 
783     if (pme->current_job_id) {
784         /* have current jpeg encoding running, abort the job */
785         if (pme->jpeg_ops.abort_job) {
786             pme->jpeg_ops.abort_job(pme->jpeg_hdl, pme->current_job_id);
787             pme->current_job_id = 0;
788         }
789 
790         if (pme->current_job_frames) {
791             for (i=0; i<pme->current_job_frames->num_bufs; i++) {
792                 if (MM_CAMERA_OK != pme->cam->ops->qbuf(pme->current_job_frames->camera_handle,
793                                                         pme->current_job_frames->ch_id,
794                                                         pme->current_job_frames->bufs[i])) {
795                     CDBG_ERROR("%s: Failed in Qbuf\n", __func__);
796                 }
797                 mm_stream_invalid_cache(pme,pme->current_job_frames->bufs[i]);
798             }
799             free(pme->current_job_frames);
800             pme->current_job_frames = NULL;
801         }
802     }
803 
804     if (MM_CAMERA_OK != (rc = mm_app_streamoff_snapshot(cam_id))) {
805         CDBG_ERROR("%s : Stream off Snapshot stream err",__func__);
806     }
807     pme->cam_state = CAMERA_STATE_OPEN;
808 
809     /* deinit jpeg buf */
810     rc = mm_stream_release_bufs(pme,
811                                 &pme->jpeg_buf);
812 end:
813     CDBG("%s: END, rc=%d\n", __func__, rc);
814 
815     return rc;
816 }
817 
mm_app_stop_raw(int cam_id)818 int mm_app_stop_raw(int cam_id)
819 {
820     int rc = MM_CAMERA_OK;
821     int i;
822     uint32_t stream[1];
823 
824     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
825 
826     stream[0] = pme->stream[MM_CAMERA_SNAPSHOT_MAIN].id;
827 
828     if (MM_CAMERA_OK != (rc = pme->cam->ops->stop_streams(pme->cam->camera_handle,pme->ch_id,1,stream))) {
829         CDBG_ERROR("%s : Snapshot Stream off Error",__func__);
830         goto end;
831     }
832     CDBG("Stop snapshot main successfull");
833 
834     if (MM_CAMERA_OK != (rc = pme->cam->ops->destroy_stream_bundle(pme->cam->camera_handle,pme->ch_id))) {
835         CDBG_ERROR("%s : Snapshot destroy_stream_bundle Error",__func__);
836         goto end;
837     }
838 
839     if (MM_CAMERA_OK != (rc = pme->cam->ops->del_stream(pme->cam->camera_handle,pme->ch_id,pme->stream[MM_CAMERA_SNAPSHOT_MAIN].id))) {
840         CDBG_ERROR("%s : Snapshot main image del_stream Error",__func__);
841         goto end;
842     }
843     CDBG("del_stream successfull");
844 
845     pme->cam_state = CAMERA_STATE_OPEN;
846 end:
847     return rc;
848 }
849 
mm_app_snapshot_wait(int cam_id)850 static void mm_app_snapshot_wait(int cam_id)
851 {
852     pthread_mutex_lock(&g_s_mutex);
853     if (FALSE == g_status) {
854         pthread_cond_wait(&g_s_cond_v, &g_s_mutex);
855         g_status = FALSE;
856     }
857     pthread_mutex_unlock(&g_s_mutex);
858 }
859 
mm_app_live_notify_cb(mm_camera_super_buf_t * bufs,void * user_data)860 static void mm_app_live_notify_cb(mm_camera_super_buf_t *bufs,
861                                   void *user_data)
862 {
863 
864     int rc;
865     int i = 0;
866     mm_camera_buf_def_t *main_frame = NULL;
867     mm_camera_app_obj_t *pme = NULL;
868     CDBG("%s: BEGIN\n", __func__);
869 
870     pme = (mm_camera_app_obj_t *)user_data;
871 
872     CDBG("%s : total streams = %d",__func__,bufs->num_bufs);
873     main_frame = bufs->bufs[0];
874     //thumb_frame = bufs->bufs[1];
875 
876     CDBG("mainframe frame_idx = %d fd = %d frame length = %d",main_frame->frame_idx,main_frame->fd,main_frame->frame_len);
877     //CDBG("thumnail frame_idx = %d fd = %d frame length = %d",thumb_frame->frame_idx,thumb_frame->fd,thumb_frame->frame_len);
878 
879     //dumpFrameToFile(main_frame->frame,pme->dim.picture_width,pme->dim.picture_height,"main", 1);
880 
881     dumpFrameToFile(main_frame,pme->dim.picture_width,pme->dim.picture_height,"liveshot_main", 1,"yuv");
882 
883     if (MM_CAMERA_OK != pme->cam->ops->qbuf(pme->cam->camera_handle,pme->ch_id,main_frame)) {
884         CDBG_ERROR("%s: Failed in thumbnail Qbuf\n", __func__);
885     }
886     mm_stream_invalid_cache(pme,main_frame);
887     /*if(MM_CAMERA_OK != pme->cam->ops->qbuf(pme->cam->camera_handle,pme->ch_id,thumb_frame))
888     {
889             CDBG_ERROR("%s: Failed in thumbnail Qbuf\n", __func__);
890     }*/
891 
892     mm_app_snapshot_done();
893     CDBG("%s: END\n", __func__);
894 
895 
896 }
897 
mm_app_prepare_live_snapshot(int cam_id)898 int mm_app_prepare_live_snapshot(int cam_id)
899 {
900     int rc = 0;
901     uint32_t stream[1];
902     mm_camera_bundle_attr_t attr;
903     int value = 0;
904 
905     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
906     CDBG("%s:BEGIN, cam_id=%d\n",__func__,cam_id);
907 
908     stream[0] = pme->stream[MM_CAMERA_SNAPSHOT_MAIN].id;
909     //stream[1] = pme->stream[MM_CAMERA_PREVIEW].id;  //Need to clarify
910 
911     attr.notify_mode = MM_CAMERA_SUPER_BUF_NOTIFY_BURST;
912     attr.burst_num = 1;
913     attr.look_back = 2;
914     attr.post_frame_skip = 0;
915     attr.water_mark = 2;
916 
917 
918     if (MM_CAMERA_OK != (rc = pme->cam->ops->set_parm(
919                                                      pme->cam->camera_handle,MM_CAMERA_PARM_FULL_LIVESHOT, (void *)&value))) {
920         CDBG_ERROR("%s: set dimension err=%d\n", __func__, rc);
921     }
922 
923     if (MM_CAMERA_OK != (rc = pme->cam->ops->init_stream_bundle(
924                                                                pme->cam->camera_handle,pme->ch_id,mm_app_live_notify_cb,pme,&attr,1,stream))) {
925         CDBG_ERROR("%s:init_stream_bundle err=%d\n", __func__, rc);
926         rc = -1;
927         goto end;
928     }
929 
930     if (MM_CAMERA_OK != (rc = pme->cam->ops->start_streams(pme->cam->camera_handle,pme->ch_id,
931                                                            1, stream))) {
932         CDBG_ERROR("%s:start_streams err=%d\n", __func__, rc);
933         rc = -1;
934         goto end;
935     }
936 
937 
938     end:
939     CDBG("%s:END, cam_id=%d\n",__func__,cam_id);
940     return rc;
941 }
942 
mm_app_unprepare_live_snapshot(int cam_id)943 int mm_app_unprepare_live_snapshot(int cam_id)
944 {
945     int rc = 0;
946     uint32_t stream[2];
947 
948     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
949     CDBG("%s:BEGIN, cam_id=%d\n",__func__,cam_id);
950 
951     stream[0] = pme->stream[MM_CAMERA_SNAPSHOT_MAIN].id;
952     if (MM_CAMERA_OK != (rc = pme->cam->ops->stop_streams(pme->cam->camera_handle,pme->ch_id,1,stream))) {
953         CDBG_ERROR("%s : Snapshot Stream off Error",__func__);
954         return -1;
955     }
956 
957     if (MM_CAMERA_OK != (rc = pme->cam->ops->destroy_stream_bundle(pme->cam->camera_handle,pme->ch_id))) {
958         CDBG_ERROR("%s : Snapshot destroy_stream_bundle Error",__func__);
959         return -1;
960     }
961     CDBG("%s:END, cam_id=%d\n",__func__,cam_id);
962     return rc;
963 }
964 
mm_app_take_live_snapshot(int cam_id)965 int mm_app_take_live_snapshot(int cam_id)
966 {
967     int rc = 0;
968     int stream[3];
969     mm_camera_bundle_attr_t attr;
970 
971     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
972 
973     CDBG("%s:BEGIN, cam_id=%d\n",__func__,cam_id);
974 
975     if (pme->cam_mode == RECORDER_MODE &&
976         pme->cam_state == CAMERA_STATE_RECORD) {
977         //Code to get live shot
978         if (mm_app_prepare_live_snapshot(cam_id) != MM_CAMERA_OK) {
979             CDBG_ERROR("%s: Failed prepare liveshot",__func__);
980             return -1;
981         }
982         if (MM_CAMERA_OK != (rc =pme->cam->ops->request_super_buf(pme->cam->camera_handle,pme->ch_id, 1))) {
983             CDBG_ERROR("%s:request_super_buf err=%d\n", __func__, rc);
984             return -1;
985         }
986         CDBG("%s:waiting images\n",__func__);
987         mm_app_snapshot_wait(cam_id);
988 
989         if (MM_CAMERA_OK !=mm_app_unprepare_live_snapshot(cam_id)) {
990             CDBG_ERROR("%s: Snapshot Stop error",__func__);
991         }
992 
993     } else {
994         CDBG_ERROR("%s: Should not come here for liveshot",__func__);
995     }
996     return rc;
997 }
998 
mm_app_take_picture_raw(int cam_id)999 int mm_app_take_picture_raw(int cam_id)
1000 {
1001 
1002     int rc;
1003     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
1004 
1005     CDBG("%s:BEGIN, cam_id=%d\n",__func__,cam_id);
1006 
1007     if (MM_CAMERA_OK != mm_app_start_raw(cam_id)) {
1008         CDBG_ERROR("%s: cam_id=%d\n",__func__,cam_id);
1009         rc = -1;
1010         goto end;
1011     }
1012 
1013     CDBG("%s:waiting images\n",__func__);
1014     mm_app_snapshot_wait(cam_id);
1015 
1016     if (MM_CAMERA_OK !=mm_app_stop_raw(cam_id)) {
1017         CDBG_ERROR("%s: Snapshot Stop error",__func__);
1018     }
1019 
1020 preview:
1021     if (MM_CAMERA_OK != (rc = mm_app_start_preview(cam_id))) {
1022         CDBG("%s:preview start stream err=%d\n", __func__, rc);
1023     }
1024     end:
1025     CDBG("%s:END, cam_id=%d\n",__func__,cam_id);
1026     return rc;
1027 }
1028 
mm_app_take_picture_zsl(int cam_id)1029 int mm_app_take_picture_zsl(int cam_id)
1030 {
1031     int rc = MM_CAMERA_OK;
1032     int value = 1;
1033     int op_mode;
1034 
1035     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
1036 
1037     CDBG("%s: Take picture ZSL",__func__);
1038 
1039     if (MM_CAMERA_OK != (rc =pme->cam->ops->request_super_buf(pme->cam->camera_handle,pme->ch_id, 1))) {
1040         CDBG_ERROR("%s:request_super_buf err=%d\n", __func__, rc);
1041         goto end;
1042     }
1043 
1044     CDBG("%s: Start ZSL Preview",__func__);
1045 
1046     end:
1047     CDBG("%s: END, rc=%d\n", __func__, rc);
1048     return rc;
1049 }
1050 
mm_app_take_picture_yuv(int cam_id)1051 int mm_app_take_picture_yuv(int cam_id)
1052 {
1053     int rc;
1054     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
1055 
1056     CDBG("%s:BEGIN, cam_id=%d\n",__func__,cam_id);
1057 
1058     if (MM_CAMERA_OK != mm_app_start_snapshot(cam_id)) {
1059         CDBG_ERROR("%s: cam_id=%d\n",__func__,cam_id);
1060         rc = -1;
1061         goto end;
1062     }
1063 
1064     CDBG("%s:waiting images\n",__func__);
1065     mm_app_snapshot_wait(cam_id);
1066 
1067     if (MM_CAMERA_OK !=mm_app_stop_snapshot(cam_id)) {
1068         CDBG_ERROR("%s: Snapshot Stop error",__func__);
1069     }
1070 
1071     preview:
1072     if (MM_CAMERA_OK != (rc = mm_app_start_preview(cam_id))) {
1073         CDBG("%s:preview start stream err=%d\n", __func__, rc);
1074     }
1075     end:
1076     CDBG("%s:END, cam_id=%d\n",__func__,cam_id);
1077     return rc;
1078 }
1079 
mm_app_take_zsl(int cam_id)1080 int mm_app_take_zsl(int cam_id)
1081 {
1082     int rc = MM_CAMERA_OK;
1083 
1084     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
1085 
1086     CDBG("%s:BEGIN, cam_id=%d\n",__func__,cam_id);
1087 
1088     if (pme->cam_mode == RECORDER_MODE) {
1089         switch (pme->cam_state) {
1090         case CAMERA_STATE_RECORD:
1091             if (MM_CAMERA_OK != mm_app_stop_video(cam_id)) {
1092                 CDBG_ERROR("%s:Cannot stop video err=%d\n", __func__, rc);
1093                 return -1;
1094             }
1095         case CAMERA_STATE_PREVIEW:
1096             if (MM_CAMERA_OK != mm_app_open_zsl(cam_id)) {
1097                 CDBG_ERROR("%s: Cannot switch to camera mode\n", __func__);
1098                 return -1;
1099             }
1100             break;
1101         case CAMERA_STATE_SNAPSHOT:
1102         default:
1103             CDBG("%s: Cannot normal pciture in record mode\n", __func__);
1104             break;
1105         }
1106     } else if (pme->cam_mode == CAMERA_MODE) {
1107         switch (pme->cam_state) {
1108         case CAMERA_STATE_PREVIEW:
1109             mm_app_open_zsl(cam_id);
1110             break;
1111 
1112         case CAMERA_STATE_SNAPSHOT:
1113         case CAMERA_STATE_RECORD:
1114         default:
1115             CDBG("%s: Cannot normal pciture in record mode\n", __func__);
1116             break;
1117         }
1118     }
1119 
1120     if (pme->cam_mode == ZSL_MODE && pme->cam_state == CAMERA_STATE_PREVIEW) {
1121         mm_app_take_picture_zsl(cam_id);
1122     }
1123     return rc;
1124 }
1125 
mm_app_take_picture(int cam_id)1126 int mm_app_take_picture(int cam_id)
1127 {
1128     int rc = 0;
1129 
1130     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
1131 
1132     CDBG("%s:BEGIN, cam_id=%d\n",__func__,cam_id);
1133 
1134     if (pme->cam_mode == RECORDER_MODE) {
1135         switch (pme->cam_state) {
1136         case CAMERA_STATE_RECORD:
1137             if (MM_CAMERA_OK != mm_app_stop_video(cam_id)) {
1138                 CDBG_ERROR("%s:Cannot stop video err=%d\n", __func__, rc);
1139                 return -1;
1140             }
1141         case CAMERA_STATE_PREVIEW:
1142             if (MM_CAMERA_OK != mm_app_open_camera(cam_id)) {
1143                 CDBG_ERROR("%s: Cannot switch to camera mode=%d\n", __func__,rc);
1144                 return -1;
1145             }
1146             break;
1147         case CAMERA_STATE_SNAPSHOT:
1148         default:
1149             CDBG("%s: Cannot normal pciture in record mode\n", __func__);
1150             break;
1151         }
1152     } else if (pme->cam_mode == ZSL_MODE) {
1153         switch (pme->cam_state) {
1154         case CAMERA_STATE_PREVIEW:
1155             mm_app_open_camera(cam_id);
1156             break;
1157 
1158         case CAMERA_STATE_SNAPSHOT:
1159         case CAMERA_STATE_RECORD:
1160         default:
1161             CDBG("%s: Cannot normal pciture in record mode\n", __func__);
1162             break;
1163         }
1164     }
1165 
1166     CDBG("%s : Takepicture : mode = %d state = %d, rc = %d",__func__,pme->cam_mode,pme->cam_state,rc);
1167     if (pme->cam_mode == CAMERA_MODE && pme->cam_state == CAMERA_STATE_PREVIEW) {
1168         mm_app_take_picture_yuv(cam_id);
1169     }
1170     return rc;
1171 }
1172 
1173 
mm_app_take_raw(int cam_id)1174 int mm_app_take_raw(int cam_id)
1175 {
1176     int rc = 0;
1177 
1178     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
1179 
1180     CDBG("%s:BEGIN, cam_id=%d\n",__func__,cam_id);
1181 
1182     if (pme->cam_mode == RECORDER_MODE) {
1183         switch (pme->cam_state) {
1184         case CAMERA_STATE_RECORD:
1185             if (MM_CAMERA_OK != mm_app_stop_video(cam_id)) {
1186                 CDBG_ERROR("%s:Cannot stop video err=%d\n", __func__, rc);
1187                 return -1;
1188             }
1189         case CAMERA_STATE_PREVIEW:
1190             if (MM_CAMERA_OK != mm_app_open_camera(cam_id)) {
1191                 CDBG_ERROR("%s: Cannot switch to camera mode=%d\n", __func__,rc);
1192                 return -1;
1193             }
1194             break;
1195         case CAMERA_STATE_SNAPSHOT:
1196         default:
1197             CDBG("%s: Cannot normal pciture in record mode\n", __func__);
1198             break;
1199         }
1200     } else if (pme->cam_mode == ZSL_MODE) {
1201         switch (pme->cam_state) {
1202         case CAMERA_STATE_PREVIEW:
1203             mm_app_open_camera(cam_id);
1204             break;
1205 
1206         case CAMERA_STATE_SNAPSHOT:
1207         case CAMERA_STATE_RECORD:
1208         default:
1209             CDBG("%s: Cannot normal pciture in record mode\n", __func__);
1210             break;
1211         }
1212     }
1213 
1214     CDBG("%s : Takepicture RAW: mode = %d state = %d, rc = %d",__func__,pme->cam_mode,pme->cam_state,rc);
1215     if (pme->cam_mode == CAMERA_MODE && pme->cam_state == CAMERA_STATE_PREVIEW) {
1216         mm_app_take_picture_raw(cam_id);
1217     }
1218     return rc;
1219 }
1220 
1221