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