Add new internal API to get meta
[platform/core/api/camera.git] / src / camera_internal.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <mm.h>
22 #include <mm_types.h>
23 #include <camera_internal.h>
24 #include <camera_private.h>
25
26 #ifdef LOG_TAG
27 #undef LOG_TAG
28 #endif
29 #define LOG_TAG "TIZEN_N_CAMERA"
30
31
32 //LCOV_EXCL_START
33 int __set_level(camera_h camera, muse_camera_api_e api, int level)
34 {
35         int ret = CAMERA_ERROR_NONE;
36         camera_cli_s *pc = (camera_cli_s *)camera;
37         camera_msg_param param;
38
39         if (!pc || !pc->cb_info) {
40                 CAM_LOG_ERROR("NULL handle - api[%d]", api);
41                 return CAMERA_ERROR_INVALID_PARAMETER;
42         }
43
44         CAM_LOG_INFO("Enter - api[%d], level[%d]", api, level);
45
46         CAMERA_MSG_PARAM_SET(param, INT, level);
47
48         _camera_msg_send_param1(api, pc->cb_info, &ret, &param, CAMERA_CB_TIMEOUT);
49
50         CAM_LOG_INFO("ret : 0x%x", ret);
51
52         return ret;
53 }
54
55
56 int camera_start_evas_rendering(camera_h camera)
57 {
58         return _camera_start_evas_rendering(camera);
59 }
60
61
62 int camera_stop_evas_rendering(camera_h camera, bool keep_screen)
63 {
64         return _camera_stop_evas_rendering(camera, keep_screen);
65 }
66
67
68 int camera_set_ecore_wl_display(camera_h camera, void *ecore_wl_window)
69 {
70         return _camera_set_display(camera, MM_DISPLAY_TYPE_OVERLAY_EXT, ecore_wl_window);
71 }
72 //LCOV_EXCL_STOP
73
74
75 void camera_create_preview_frame(MMCamcorderVideoStreamDataType *stream, int num_buffer_fd,
76         tbm_bo_handle *buffer_bo_handle, tbm_bo_handle *data_bo_handle, camera_preview_data_s *frame)
77 {
78         int total_size = 0;
79         unsigned char *buf_pos = NULL;
80
81         if (!stream || !buffer_bo_handle || !frame) {
82                 CAM_LOG_ERROR("invalid param %p,%p,%p", stream, buffer_bo_handle, frame);
83                 return;
84         }
85
86         /* set frame info */
87         if (stream->format == MM_PIXEL_FORMAT_ITLV_JPEG_UYVY)
88                 frame->format = MM_PIXEL_FORMAT_UYVY;
89         else
90                 frame->format = stream->format;
91
92         frame->width = stream->width;
93         frame->height = stream->height;
94         frame->timestamp = stream->timestamp;
95         frame->num_of_planes = stream->num_planes;
96
97         if (num_buffer_fd == 0) {
98 //LCOV_EXCL_START
99                 /* non-zero copy */
100                 if (!data_bo_handle || !data_bo_handle->ptr) {
101                         CAM_LOG_ERROR("NULL pointer");
102                         return;
103                 }
104
105                 buf_pos = data_bo_handle->ptr;
106
107                 switch (stream->format) {
108                 case MM_PIXEL_FORMAT_ENCODED_H264:
109                         /* fall through */
110                 case MM_PIXEL_FORMAT_ENCODED_MJPEG:
111                         /* fall through */
112                 case MM_PIXEL_FORMAT_ENCODED_VP8:
113                         /* fall through */
114                 case MM_PIXEL_FORMAT_ENCODED_VP9:
115                         frame->data.encoded_plane.data = buf_pos;
116                         frame->data.encoded_plane.size = stream->data.encoded.length_data;
117                         frame->data.encoded_plane.is_delta_frame = (bool)stream->data.encoded.is_delta_frame;
118                         total_size = stream->data.encoded.length_data;
119                         break;
120
121                 case MM_PIXEL_FORMAT_INVZ:
122                         frame->data.depth_plane.data = buf_pos;
123                         frame->data.depth_plane.size = stream->data.depth.length_data;
124                         total_size = stream->data.depth.length_data;
125                         break;
126
127                 case MM_PIXEL_FORMAT_RGBA:
128                         /* fall through */
129                 case MM_PIXEL_FORMAT_ARGB:
130                         frame->data.rgb_plane.data = buf_pos;
131                         frame->data.rgb_plane.size = stream->data.rgb.length_data;
132                         total_size = stream->data.rgb.length_data;
133                         break;
134
135                 default:
136                         switch (stream->num_planes) {
137                         case 1:
138                                 frame->data.single_plane.yuv = buf_pos;
139                                 frame->data.single_plane.size = stream->data.yuv420.length_yuv;
140                                 total_size = stream->data.yuv420.length_yuv;
141                                 break;
142                         case 2:
143                                 frame->data.double_plane.y = buf_pos;
144                                 frame->data.double_plane.y_size = stream->data.yuv420sp.length_y;
145                                 buf_pos += stream->data.yuv420sp.length_y;
146                                 frame->data.double_plane.uv = buf_pos;
147                                 frame->data.double_plane.uv_size = stream->data.yuv420sp.length_uv;
148                                 total_size = stream->data.yuv420sp.length_y + \
149                                         stream->data.yuv420sp.length_uv;
150                                 break;
151                         case 3:
152                                 frame->data.triple_plane.y = buf_pos;
153                                 frame->data.triple_plane.y_size = stream->data.yuv420p.length_y;
154                                 buf_pos += stream->data.yuv420p.length_y;
155                                 frame->data.triple_plane.u = buf_pos;
156                                 frame->data.triple_plane.u_size = stream->data.yuv420p.length_u;
157                                 buf_pos += stream->data.yuv420p.length_u;
158                                 frame->data.triple_plane.v = buf_pos;
159                                 frame->data.triple_plane.v_size = stream->data.yuv420p.length_v;
160                                 total_size = stream->data.yuv420p.length_y + \
161                                         stream->data.yuv420p.length_u + \
162                                         stream->data.yuv420p.length_v;
163                                 break;
164                         default:
165                                 break;
166                         }
167                         break;
168                 }
169 //LCOV_EXCL_STOP
170         } else {
171                 /* zero copy */
172                 switch (stream->num_planes) {
173 //LCOV_EXCL_START
174                 case 1:
175                         if (stream->data_type == MM_CAM_STREAM_DATA_ENCODED) {
176                                 frame->data.encoded_plane.data = buffer_bo_handle[0].ptr;
177                                 frame->data.encoded_plane.size = stream->data.encoded.length_data;
178                                 frame->data.encoded_plane.is_delta_frame = (bool)stream->data.encoded.is_delta_frame;
179                                 total_size = stream->data.encoded.length_data;
180                         } else {
181                                 frame->data.single_plane.yuv = buffer_bo_handle[0].ptr;
182                                 frame->data.single_plane.size = stream->data.yuv420.length_yuv;
183                                 total_size = stream->data.yuv420.length_yuv;
184                         }
185                         break;
186 //LCOV_EXCL_STOP
187                 case 2:
188                         frame->data.double_plane.y = buffer_bo_handle[0].ptr;
189                         if (stream->num_planes == (unsigned int)num_buffer_fd)
190                                 frame->data.double_plane.uv = buffer_bo_handle[1].ptr;
191                         else
192                                 frame->data.double_plane.uv = buffer_bo_handle[0].ptr + stream->data.yuv420sp.length_y;
193                         frame->data.double_plane.y_size = stream->data.yuv420sp.length_y;
194                         frame->data.double_plane.uv_size = stream->data.yuv420sp.length_uv;
195                         total_size = stream->data.yuv420sp.length_y + \
196                                 stream->data.yuv420sp.length_uv;
197                         break;
198                 case 3:
199 //LCOV_EXCL_START
200                         frame->data.triple_plane.y = buffer_bo_handle[0].ptr;
201                         if (stream->num_planes == (unsigned int)num_buffer_fd) {
202                                 frame->data.triple_plane.u = buffer_bo_handle[1].ptr;
203                                 frame->data.triple_plane.v = buffer_bo_handle[2].ptr;
204                         } else {
205                                 frame->data.triple_plane.u = buffer_bo_handle[0].ptr + stream->data.yuv420p.length_y;
206                                 frame->data.triple_plane.v = frame->data.triple_plane.u + stream->data.yuv420p.length_u;
207                         }
208                         frame->data.triple_plane.y_size = stream->data.yuv420p.length_y;
209                         frame->data.triple_plane.u_size = stream->data.yuv420p.length_u;
210                         frame->data.triple_plane.v_size = stream->data.yuv420p.length_v;
211                         total_size = stream->data.yuv420p.length_y + \
212                                 stream->data.yuv420p.length_u + \
213                                 stream->data.yuv420p.length_v;
214                         break;
215 //LCOV_EXCL_STOP
216                 default:
217                         break;
218                 }
219         }
220
221         CAM_LOG_DEBUG("format[%d], res[%dx%d], size[%d], plane num[%d]",
222                 frame->format, frame->width, frame->height, total_size, frame->num_of_planes);
223 }
224
225
226 //LCOV_EXCL_START
227 int camera_attr_set_flash_brightness(camera_h camera, int level)
228 {
229         return __set_level(camera, MUSE_CAMERA_API_ATTR_SET_FLASH_BRIGHTNESS, level);
230 }
231
232
233 int camera_attr_get_flash_brightness(camera_h camera, int *level)
234 {
235         int ret = CAMERA_ERROR_NONE;
236         camera_cli_s *pc = (camera_cli_s *)camera;
237         muse_camera_api_e api = MUSE_CAMERA_API_ATTR_GET_FLASH_BRIGHTNESS;
238
239         if (!pc || !pc->cb_info || !level) {
240                 CAM_LOG_ERROR("NULL pointer %p %p", pc, level);
241                 return CAMERA_ERROR_INVALID_PARAMETER;
242         }
243
244         CAM_LOG_INFO("Enter");
245
246         _camera_msg_send(api, NULL, pc->cb_info, &ret, CAMERA_CB_TIMEOUT);
247
248         if (ret == CAMERA_ERROR_NONE)
249                 *level = pc->cb_info->get_int[MUSE_CAMERA_GET_INT_FLASH_BRIGHTNESS];
250
251         CAM_LOG_INFO("ret : 0x%x", ret);
252
253         return ret;
254 }
255
256
257 int camera_attr_get_flash_brightness_range(camera_h camera, int *min, int *max)
258 {
259         int ret = CAMERA_ERROR_NONE;
260         camera_cli_s *pc = (camera_cli_s *)camera;
261         muse_camera_api_e api = MUSE_CAMERA_API_ATTR_GET_FLASH_BRIGHTNESS_RANGE;
262
263         if (!pc || !pc->cb_info || !min || !max) {
264                 CAM_LOG_ERROR("NULL pointer %p %p %p", pc, min, max);
265                 return CAMERA_ERROR_INVALID_PARAMETER;
266         }
267
268         CAM_LOG_INFO("Enter");
269
270         _camera_msg_send(api, NULL, pc->cb_info, &ret, CAMERA_CB_TIMEOUT);
271
272         if (ret == CAMERA_ERROR_NONE) {
273                 *min = pc->cb_info->get_int_pair[MUSE_CAMERA_GET_INT_PAIR_FLASH_BRIGHTNESS_RANGE][0];
274                 *max = pc->cb_info->get_int_pair[MUSE_CAMERA_GET_INT_PAIR_FLASH_BRIGHTNESS_RANGE][1];
275         }
276
277         CAM_LOG_INFO("ret : 0x%x", ret);
278
279         return ret;
280 }
281
282
283 int camera_attr_set_focus_level(camera_h camera, int level)
284 {
285         return __set_level(camera, MUSE_CAMERA_API_ATTR_SET_FOCUS_LEVEL, level);
286 }
287
288
289 int camera_attr_get_focus_level(camera_h camera, int *level)
290 {
291         int ret = CAMERA_ERROR_NONE;
292         camera_cli_s *pc = (camera_cli_s *)camera;
293         muse_camera_api_e api = MUSE_CAMERA_API_ATTR_GET_FOCUS_LEVEL;
294
295         if (!pc || !pc->cb_info || !level) {
296                 CAM_LOG_ERROR("NULL pointer %p %p", pc, level);
297                 return CAMERA_ERROR_INVALID_PARAMETER;
298         }
299
300         CAM_LOG_INFO("Enter");
301
302         _camera_msg_send(api, NULL, pc->cb_info, &ret, CAMERA_CB_TIMEOUT);
303
304         if (ret == CAMERA_ERROR_NONE)
305                 *level = pc->cb_info->get_int[MUSE_CAMERA_GET_INT_FOCUS_LEVEL];
306
307         CAM_LOG_INFO("ret : 0x%x", ret);
308
309         return ret;
310 }
311
312
313 int camera_attr_get_focus_level_range(camera_h camera, int *min, int *max)
314 {
315         int ret = CAMERA_ERROR_NONE;
316         camera_cli_s *pc = (camera_cli_s *)camera;
317         muse_camera_api_e api = MUSE_CAMERA_API_ATTR_GET_FOCUS_LEVEL_RANGE;
318
319         if (!pc || !pc->cb_info || !min || !max) {
320                 CAM_LOG_ERROR("NULL pointer %p %p %p", pc, min, max);
321                 return CAMERA_ERROR_INVALID_PARAMETER;
322         }
323
324         CAM_LOG_INFO("Enter");
325
326         _camera_msg_send(api, NULL, pc->cb_info, &ret, CAMERA_CB_TIMEOUT);
327
328         if (ret == CAMERA_ERROR_NONE) {
329                 *min = pc->cb_info->get_int_pair[MUSE_CAMERA_GET_INT_PAIR_FOCUS_LEVEL_RANGE][0];
330                 *max = pc->cb_info->get_int_pair[MUSE_CAMERA_GET_INT_PAIR_FOCUS_LEVEL_RANGE][1];
331         }
332
333         CAM_LOG_INFO("ret : 0x%x", ret);
334
335         return ret;
336 }
337
338
339 int camera_set_extra_preview_device(camera_h camera, int stream_id, camera_device_e device)
340 {
341         int ret = CAMERA_ERROR_NONE;
342         char *msg = NULL;
343         camera_cli_s *pc = (camera_cli_s *)camera;
344         muse_camera_api_e api = MUSE_CAMERA_API_SET_EXTRA_PREVIEW_DEVICE;
345
346         CAMERA_CHECK_HANDLE_RETURN_VAL(pc, CAMERA_ERROR_INVALID_PARAMETER);
347
348         CAM_LOG_INFO("Enter - stream[%d], device[%d]", stream_id, device);
349
350         msg = muse_core_msg_new(api,
351                 MUSE_TYPE_INT, "stream_id", stream_id,
352                 MUSE_TYPE_INT, "device", device,
353                 NULL);
354
355         _camera_send_message_get_return(pc->cb_info, api, msg, CAMERA_CB_TIMEOUT, &ret);
356
357         return ret;
358 }
359
360
361 int camera_request_codec_config(camera_h camera)
362 {
363         int ret = CAMERA_ERROR_NONE;
364         camera_cli_s *pc = (camera_cli_s *)camera;
365         muse_camera_api_e api = MUSE_CAMERA_API_REQUEST_CODEC_CONFIG;
366
367         if (!pc || !pc->cb_info) {
368                 CAM_LOG_ERROR("NULL pointer %p", pc);
369                 return CAMERA_ERROR_INVALID_PARAMETER;
370         }
371
372         CAM_LOG_INFO("Enter");
373
374         _camera_msg_send(api, NULL, pc->cb_info, &ret, CAMERA_CB_TIMEOUT);
375
376         CAM_LOG_INFO("ret : 0x%x", ret);
377
378         return ret;
379 }
380
381
382 int camera_attr_get_preview_frame_timestamp(camera_h camera, unsigned long *timestamp)
383 {
384         camera_cli_s *pc = (camera_cli_s *)camera;
385
386         if (!pc || !pc->cb_info || !timestamp) {
387                 CAM_LOG_ERROR("NULL pointer %p %p", pc, timestamp);
388                 return CAMERA_ERROR_INVALID_PARAMETER;
389         }
390
391         if (!pc->cb_info->stream_data) {
392                 CAM_LOG_ERROR("no stream data, maybe it's not in preview callback");
393                 return CAMERA_ERROR_INVALID_OPERATION;
394         }
395
396         *timestamp = pc->cb_info->stream_data->timestamp_nsec;
397
398         CAM_LOG_DEBUG("frame timestamp[%lu]", *timestamp);
399
400         return CAMERA_ERROR_NONE;
401 }
402
403
404 int camera_attr_get_preview_frame_meta_timestamp(camera_h camera, camera_meta_timestamp_s *meta_timestamp)
405 {
406         camera_cli_s *pc = (camera_cli_s *)camera;
407         MMCamcorderVideoStreamDataType *stream = NULL;
408
409         if (!pc || !pc->cb_info || !meta_timestamp) {
410                 CAM_LOG_ERROR("NULL pointer %p %p", pc, meta_timestamp);
411                 return CAMERA_ERROR_INVALID_PARAMETER;
412         }
413
414         stream = pc->cb_info->stream_data;
415         if (!stream) {
416                 CAM_LOG_ERROR("no stream data, maybe it's not in preview callback");
417                 return CAMERA_ERROR_INVALID_OPERATION;
418         }
419
420         meta_timestamp->ts_soe = stream->frame_meta.ts_soe;
421         meta_timestamp->ts_eoe = stream->frame_meta.ts_eoe;
422         meta_timestamp->ts_sof = stream->frame_meta.ts_sof;
423         meta_timestamp->ts_eof = stream->frame_meta.ts_eof;
424         meta_timestamp->ts_hal = stream->frame_meta.ts_hal;
425         meta_timestamp->ts_qmf = stream->frame_meta.ts_qmf;
426         meta_timestamp->ts_gst = stream->frame_meta.ts_gst;
427         meta_timestamp->td_exp = stream->frame_meta.td_exp;
428         meta_timestamp->ts_aux = stream->frame_meta.ts_aux;
429         meta_timestamp->td_aux = stream->frame_meta.td_aux;
430
431         return CAMERA_ERROR_NONE;
432 }
433
434
435 int camera_attr_get_preview_frame_meta(camera_h camera, camera_frame_meta_s *frame_meta)
436 {
437         camera_cli_s *pc = (camera_cli_s *)camera;
438         MMCamcorderVideoStreamDataType *stream = NULL;
439
440         if (!pc || !pc->cb_info || !frame_meta) {
441                 CAM_LOG_ERROR("NULL pointer %p %p", pc, frame_meta);
442                 return CAMERA_ERROR_INVALID_PARAMETER;
443         }
444
445         stream = pc->cb_info->stream_data;
446         if (!stream) {
447                 CAM_LOG_ERROR("no stream data, maybe it's not in preview callback");
448                 return CAMERA_ERROR_INVALID_OPERATION;
449         }
450
451         frame_meta->ts_soe = stream->frame_meta.ts_soe;
452         frame_meta->ts_eoe = stream->frame_meta.ts_eoe;
453         frame_meta->ts_sof = stream->frame_meta.ts_sof;
454         frame_meta->ts_eof = stream->frame_meta.ts_eof;
455         frame_meta->ts_hal = stream->frame_meta.ts_hal;
456         frame_meta->ts_qmf = stream->frame_meta.ts_qmf;
457         frame_meta->ts_gst = stream->frame_meta.ts_gst;
458         frame_meta->td_exp = stream->frame_meta.td_exp;
459         frame_meta->ts_aux = stream->frame_meta.ts_aux;
460         frame_meta->td_aux = stream->frame_meta.td_aux;
461         frame_meta->seqnum = stream->frame_meta.seqnum;
462         frame_meta->flags = stream->frame_meta.flags;
463
464         return CAMERA_ERROR_NONE;
465 }
466 //LCOV_EXCL_STOP