Support large file for recording
[platform/core/multimedia/libmm-camcorder.git] / src / include / mm_camcorder_util.h
1 /*
2  * libmm-camcorder
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jeongmo Yang <jm80.yang@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #ifndef __MM_CAMCORDER_UTIL_H__
23 #define __MM_CAMCORDER_UTIL_H__
24
25 /*=======================================================================================
26 | INCLUDE FILES                                                                         |
27 ========================================================================================*/
28 #include <linux/magic.h>
29
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 /*=======================================================================================
36 | GLOBAL DEFINITIONS AND DECLARATIONS FOR CAMCORDER                                     |
37 ========================================================================================*/
38
39 /*=======================================================================================
40 | MACRO DEFINITIONS                                                                     |
41 ========================================================================================*/
42 #ifndef CLEAR
43 #define CLEAR(x)            memset (&(x), 0, sizeof (x))
44 #endif
45
46 #define MMCAMCORDER_ADD_BUFFER_PROBE(x_pad, x_category, x_callback, x_hcamcorder) \
47 do { \
48         MMCamcorderHandlerItem *item = NULL; \
49         item = (MMCamcorderHandlerItem *)g_malloc(sizeof(MMCamcorderHandlerItem)); \
50         if (!item) {\
51                 _mmcam_dbg_err("Cannot connect buffer probe [malloc fail] \n"); \
52         } else if (x_category == 0 || !(x_category & _MMCAMCORDER_HANDLER_CATEGORY_ALL)) { \
53                 _mmcam_dbg_err("Invalid handler category : %x \n", x_category); \
54         } else { \
55                 item->object = G_OBJECT(x_pad); \
56                 item->category = x_category; \
57                 item->handler_id = gst_pad_add_probe(x_pad, GST_PAD_PROBE_TYPE_BUFFER, x_callback, x_hcamcorder, NULL); \
58                 x_hcamcorder->buffer_probes = g_list_append(x_hcamcorder->buffer_probes, item); \
59                 _mmcam_dbg_log("Adding buffer probe on [%s:%s] - [ID : %lu], [Category : %x] ", GST_DEBUG_PAD_NAME(item->object), item->handler_id, item->category); \
60         } \
61 } while (0);
62
63 #define MMCAMCORDER_ADD_EVENT_PROBE(x_pad, x_category, x_callback, x_hcamcorder) \
64 do { \
65         MMCamcorderHandlerItem *item = NULL; \
66         item = (MMCamcorderHandlerItem *) g_malloc(sizeof(MMCamcorderHandlerItem)); \
67         if (!item) { \
68                 _mmcam_dbg_err("Cannot connect buffer probe [malloc fail] \n"); \
69         } \
70         else if (x_category == 0 || !(x_category & _MMCAMCORDER_HANDLER_CATEGORY_ALL)) { \
71                 _mmcam_dbg_err("Invalid handler category : %x \n", x_category); \
72         } else { \
73                 item->object =G_OBJECT(x_pad); \
74                 item->category = x_category; \
75                 item->handler_id = gst_pad_add_probe(x_pad, GST_PAD_PROBE_TYPE_EVENT_BOTH, x_callback, x_hcamcorder, NULL); \
76                 x_hcamcorder->event_probes = g_list_append(x_hcamcorder->event_probes, item); \
77                 _mmcam_dbg_log("Adding event probe on [%s:%s] - [ID : %lu], [Category : %x] ", GST_DEBUG_PAD_NAME(item->object), item->handler_id, item->category); \
78         } \
79 } while (0);
80
81 #define MMCAMCORDER_SIGNAL_CONNECT( x_object, x_category, x_signal, x_callback, x_hcamcorder) \
82 do { \
83         MMCamcorderHandlerItem* item = NULL; \
84         item = (MMCamcorderHandlerItem *) g_malloc(sizeof(MMCamcorderHandlerItem)); \
85         if (!item) { \
86                 _mmcam_dbg_err("Cannot connect signal [%s]\n", x_signal ); \
87         } else if (x_category == 0 || !(x_category & _MMCAMCORDER_HANDLER_CATEGORY_ALL)) { \
88                 _mmcam_dbg_err("Invalid handler category : %x \n", x_category); \
89         } else { \
90                 item->object = G_OBJECT(x_object); \
91                 item->category = x_category; \
92                 item->handler_id = g_signal_connect(G_OBJECT(x_object), x_signal,\
93                                                     G_CALLBACK(x_callback), x_hcamcorder ); \
94                 x_hcamcorder->signals = g_list_append(x_hcamcorder->signals, item); \
95                 _mmcam_dbg_log("Connecting signal on [%s][%p] - [ID : %lu], [Category : %x] ", GST_OBJECT_NAME(item->object), item->object, item->handler_id, item->category); \
96         } \
97 } while (0);
98
99 #define MMCAMCORDER_G_OBJECT_GET(obj, name, value) \
100 do { \
101         if (obj) { \
102                 if(g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(obj)), name)) { \
103                         g_object_get(G_OBJECT(obj), name, value, NULL); \
104                 } else { \
105                         _mmcam_dbg_warn ("The object doesn't have a property named(%s)", name); \
106                 } \
107         } else { \
108                 _mmcam_dbg_err("Null object"); \
109         } \
110 } while(0);
111
112 #define MMCAMCORDER_G_OBJECT_SET(obj, name, value) \
113 do { \
114         if (obj) { \
115                 if(g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(obj)), name)) { \
116                         g_object_set(G_OBJECT(obj), name, value, NULL); \
117                 } else { \
118                         _mmcam_dbg_warn ("The object doesn't have a property named(%s)", name); \
119                 } \
120         } else { \
121                 _mmcam_dbg_err("Null object"); \
122         } \
123 } while(0);
124
125 #define MMCAM_FOURCC(a,b,c,d)  (guint32)((a)|(b)<<8|(c)<<16|(d)<<24)
126 #define MMCAM_FOURCC_ARGS(fourcc) \
127         ((gchar)((fourcc)&0xff)), \
128         ((gchar)(((fourcc)>>8)&0xff)), \
129         ((gchar)(((fourcc)>>16)&0xff)), \
130         ((gchar)(((fourcc)>>24)&0xff))
131
132 #define MMCAM_SEND_MESSAGE(handle, msg_id, msg_code) \
133 {\
134         _MMCamcorderMsgItem msg;\
135         msg.id = msg_id;\
136         msg.param.code = msg_code;\
137         _mmcam_dbg_log("msg id : %x, code : %x", msg_id, msg_code);\
138         _mmcamcorder_send_message((MMHandleType)handle, &msg);\
139 }
140
141
142 /*=======================================================================================
143 | ENUM DEFINITIONS                                                                      |
144 ========================================================================================*/
145 /**
146  *Type define of util.
147  */
148 typedef enum {
149         _MMCAMCORDER_HANDLER_PREVIEW = (1 << 0),
150         _MMCAMCORDER_HANDLER_VIDEOREC = (1 << 1),
151         _MMCAMCORDER_HANDLER_STILLSHOT = (1 << 2),
152         _MMCAMCORDER_HANDLER_AUDIOREC = (1 << 3),
153 } _MMCamcorderHandlerCategory;
154
155 /*=======================================================================================
156 | STRUCTURE DEFINITIONS                                                                 |
157 ========================================================================================*/
158
159 /**
160  * Structure of location info
161  */
162 typedef struct {
163         gint32 longitude;
164         gint32 latitude;
165         gint32 altitude;
166 } _MMCamcorderLocationInfo;
167
168 /**
169  * Structure of handler item
170  */
171 typedef struct {
172         GObject *object;
173         _MMCamcorderHandlerCategory category;
174         gulong handler_id;
175 } MMCamcorderHandlerItem;
176
177 /**
178  * Structure of message item
179  */
180 typedef struct {
181         MMHandleType handle;            /**< handle */
182         int id;                         /**< message id */
183         MMMessageParamType param;       /**< message parameter */
184         pthread_mutex_t lock;           /**< mutex for item */
185 } _MMCamcorderMsgItem;
186
187
188 /*=======================================================================================
189 | CONSTANT DEFINITIONS                                                                  |
190 ========================================================================================*/
191 #define FAT32_FILE_SYSTEM_MAX_SIZE              (4294967295UL)     /* 4 GigaByte - 1 byte */
192 #define NANO_SEC_PER_MILI_SEC                   1000000
193 #define _MMCAMCORDER_HANDLER_CATEGORY_ALL \
194         (_MMCAMCORDER_HANDLER_PREVIEW | _MMCAMCORDER_HANDLER_VIDEOREC |_MMCAMCORDER_HANDLER_STILLSHOT | _MMCAMCORDER_HANDLER_AUDIOREC)
195 #define G_DBUS_REPLY_TIMEOUT (120 * 1000)
196
197 /*=======================================================================================
198 | GLOBAL FUNCTION PROTOTYPES                                                            |
199 ========================================================================================*/
200 /* GStreamer */
201 void _mmcamcorder_remove_buffer_probe(MMHandleType handle, _MMCamcorderHandlerCategory category);
202 void _mmcamcorder_remove_one_buffer_probe(MMHandleType handle, void *object);
203 void _mmcamcorder_remove_event_probe(MMHandleType handle, _MMCamcorderHandlerCategory category);
204 void _mmcamcorder_disconnect_signal(MMHandleType handle, _MMCamcorderHandlerCategory category);
205 void _mmcamcorder_remove_all_handlers(MMHandleType handle, _MMCamcorderHandlerCategory category);
206 void _mmcamcorder_element_release_noti(gpointer data, GObject *where_the_object_was);
207 gboolean _mmcamcorder_add_elements_to_bin(GstBin *bin, GList *element_list);
208 gboolean _mmcamcorder_link_elements(GList *element_list);
209
210 /* Message */
211 gboolean _mmcamcorder_msg_callback(void *data);
212 gboolean _mmcamcorder_send_message(MMHandleType handle, _MMCamcorderMsgItem *data);
213 void _mmcamcorder_remove_message_all(MMHandleType handle);
214
215 /* Pixel format */
216 int _mmcamcorder_get_pixel_format(GstCaps *pad);
217 int _mmcamcorder_get_pixtype(unsigned int fourcc);
218 unsigned int _mmcamcorder_get_fourcc(int pixtype, int codectype, int use_zero_copy_format);
219
220 /* JPEG encode */
221 gboolean _mmcamcorder_encode_jpeg(void *src_data, unsigned int src_width, unsigned int src_height,
222                                   int src_format, unsigned int src_length, unsigned int jpeg_quality,
223                                   void **result_data, unsigned int *result_length);
224 /* resize */
225 gboolean _mmcamcorder_resize_frame(unsigned char *src_data, unsigned int src_width, unsigned int src_height, unsigned int src_length, int src_format,
226                                    unsigned char **dst_data, unsigned int *dst_width, unsigned int *dst_height, unsigned int *dst_length);
227 gboolean _mmcamcorder_downscale_UYVYorYUYV(unsigned char *src, unsigned int src_width, unsigned int src_height,
228                                            unsigned char **dst, unsigned int dst_width, unsigned int dst_height);
229
230 /* Recording */
231 /* find top level tag only, do not use this function for finding sub level tags.
232    tag_fourcc is Four-character-code (FOURCC) */
233 gint _mmcamcorder_find_tag(FILE *f, guint32 tag_fourcc, gboolean do_rewind);
234 gboolean _mmcamcorder_find_fourcc(FILE *f, guint32 tag_fourcc, gboolean do_rewind);
235 gint32 _mmcamcorder_double_to_fix(gdouble d_number);
236 gboolean _mmcamcorder_update_size(FILE *f, gint64 prev_pos, gint64 curr_pos);
237 gboolean _mmcamcorder_write_loci(FILE *f, _MMCamcorderLocationInfo info);
238 gboolean _mmcamcorder_write_geodata(FILE *f,_MMCamcorderLocationInfo info);
239 gboolean _mmcamcorder_write_udta(FILE *f, int gps_enable, _MMCamcorderLocationInfo info, _MMCamcorderLocationInfo geotag);
240 guint64 _mmcamcorder_get_container_size(const guchar *size);
241 guint64 _mmcamcorder_get_container_size64(const guchar *size);
242 gboolean _mmcamcorder_update_composition_matrix(FILE *f, int orientation);
243
244 /* File system */
245 int _mmcamcorder_get_freespace(const gchar *path, guint64 *free_space);
246 int _mmcamcorder_get_freespace_except_system(guint64 *free_space);
247 int _mmcamcorder_get_file_size(const char *filename, guint64 *size);
248 int _mmcamcorder_get_file_system_type(const gchar *path, int *file_system_type);
249 gboolean _mmcamcorder_check_file_path(const gchar *path);
250
251 /* Task */
252 void *_mmcamcorder_util_task_thread_func(void *data);
253
254 /* device */
255 int _mmcamcorder_get_device_flash_brightness(int *brightness);
256
257 #ifdef __cplusplus
258 }
259 #endif
260
261 #endif /* __MM_CAMCORDER_UTIL_H__ */