042bf07008ea29c552cbbaaecc2c86fcbdc6e7fc
[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 <gio/gio.h>
29 #include <linux/magic.h>
30
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 /*=======================================================================================
37 | GLOBAL DEFINITIONS AND DECLARATIONS FOR CAMCORDER                                     |
38 ========================================================================================*/
39
40 /*=======================================================================================
41 | MACRO DEFINITIONS                                                                     |
42 ========================================================================================*/
43 #ifndef CLEAR
44 #define CLEAR(x)            memset(&(x), 0, sizeof(x))
45 #endif
46
47 #define MMCAMCORDER_ADD_BUFFER_PROBE(x_pad, x_category, x_callback, x_hcamcorder) \
48 do { \
49         MMCamcorderHandlerItem *item = NULL; \
50         item = (MMCamcorderHandlerItem *)g_malloc(sizeof(MMCamcorderHandlerItem)); \
51         if (!item) {\
52                 _mmcam_dbg_err("Cannot connect buffer probe [malloc fail] \n"); \
53         } else if (x_category == 0 || !(x_category & _MMCAMCORDER_HANDLER_CATEGORY_ALL)) { \
54                 _mmcam_dbg_err("Invalid handler category : %x \n", x_category); \
55         } else { \
56                 item->object = G_OBJECT(x_pad); \
57                 item->category = x_category; \
58                 item->handler_id = gst_pad_add_probe(x_pad, GST_PAD_PROBE_TYPE_BUFFER, x_callback, x_hcamcorder, NULL); \
59                 x_hcamcorder->buffer_probes = g_list_append(x_hcamcorder->buffer_probes, item); \
60                 _mmcam_dbg_log("Adding buffer probe on [%s:%s] - [ID : %lu], [Category : %x] ", \
61                         GST_DEBUG_PAD_NAME(item->object), item->handler_id, item->category); \
62         } \
63 } while (0);
64
65 #define MMCAMCORDER_ADD_EVENT_PROBE(x_pad, x_category, x_callback, x_hcamcorder) \
66 do { \
67         MMCamcorderHandlerItem *item = NULL; \
68         item = (MMCamcorderHandlerItem *) g_malloc(sizeof(MMCamcorderHandlerItem)); \
69         if (!item) { \
70                 _mmcam_dbg_err("Cannot connect buffer probe [malloc fail] \n"); \
71         } \
72         else if (x_category == 0 || !(x_category & _MMCAMCORDER_HANDLER_CATEGORY_ALL)) { \
73                 _mmcam_dbg_err("Invalid handler category : %x \n", x_category); \
74         } else { \
75                 item->object = G_OBJECT(x_pad); \
76                 item->category = x_category; \
77                 item->handler_id = gst_pad_add_probe(x_pad, GST_PAD_PROBE_TYPE_EVENT_BOTH, x_callback, x_hcamcorder, NULL); \
78                 x_hcamcorder->event_probes = g_list_append(x_hcamcorder->event_probes, item); \
79                 _mmcam_dbg_log("Adding event probe on [%s:%s] - [ID : %lu], [Category : %x] ", \
80                         GST_DEBUG_PAD_NAME(item->object), item->handler_id, item->category); \
81         } \
82 } while (0);
83
84 #define MMCAMCORDER_SIGNAL_CONNECT(x_object, x_category, x_signal, x_callback, x_hcamcorder) \
85 do { \
86         MMCamcorderHandlerItem* item = NULL; \
87         item = (MMCamcorderHandlerItem *) g_malloc(sizeof(MMCamcorderHandlerItem)); \
88         if (!item) { \
89                 _mmcam_dbg_err("Cannot connect signal [%s]\n", x_signal); \
90         } else if (x_category == 0 || !(x_category & _MMCAMCORDER_HANDLER_CATEGORY_ALL)) { \
91                 _mmcam_dbg_err("Invalid handler category : %x \n", x_category); \
92         } else { \
93                 item->object = G_OBJECT(x_object); \
94                 item->category = x_category; \
95                 item->handler_id = g_signal_connect(G_OBJECT(x_object), x_signal, \
96                         G_CALLBACK(x_callback), x_hcamcorder); \
97                 x_hcamcorder->signals = g_list_append(x_hcamcorder->signals, item); \
98                 _mmcam_dbg_log("Connecting signal on [%s][%p] - [ID : %lu], [Category : %x] ", \
99                         GST_OBJECT_NAME(item->object), item->object, item->handler_id, item->category); \
100         } \
101 } while (0);
102
103 #define MMCAMCORDER_G_OBJECT_GET(obj, name, value) \
104 do { \
105         if (obj) { \
106                 if (g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(obj)), name)) { \
107                         g_object_get(G_OBJECT(obj), name, value, NULL); \
108                 } else { \
109                         _mmcam_dbg_warn("The object doesn't have a property named(%s)", name); \
110                 } \
111         } else { \
112                 _mmcam_dbg_err("Null object"); \
113         } \
114 } while (0);
115
116 #define MMCAMCORDER_G_OBJECT_SET(obj, name, value) \
117 do { \
118         if (obj) { \
119                 GParamSpec *spec = g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(obj)), name);\
120                 if (spec) { \
121                         if (spec->value_type == G_TYPE_INT64) {\
122                                 g_object_set(G_OBJECT(obj), name, (gint64)value, NULL); \
123                         } else if (spec->value_type == G_TYPE_UINT64) { \
124                                 g_object_set(G_OBJECT(obj), name, (guint64)value, NULL); \
125                         } else if (spec->value_type == G_TYPE_FLOAT) { \
126                                 g_object_set(G_OBJECT(obj), name, (float)value, NULL); \
127                         } else if (spec->value_type == G_TYPE_DOUBLE) { \
128                                 g_object_set(G_OBJECT(obj), name, (double)value, NULL); \
129                         } else { \
130                                 g_object_set(G_OBJECT(obj), name, value, NULL); \
131                         } \
132                 } else { \
133                         _mmcam_dbg_warn("The object doesn't have a property named(%s)", name); \
134                 } \
135         } else { \
136                 _mmcam_dbg_err("Null object"); \
137         } \
138 } while (0);
139
140 #define MMCAMCORDER_G_OBJECT_SET_POINTER(obj, name, value) \
141 do { \
142         if (obj) { \
143                 GParamSpec *spec = g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(obj)), name);\
144                 if (spec) { \
145                         g_object_set(G_OBJECT(obj), name, value, NULL); \
146                 } else { \
147                         _mmcam_dbg_warn("The object doesn't have a property named(%s)", name); \
148                 } \
149         } else { \
150                 _mmcam_dbg_err("Null object"); \
151         } \
152 } while (0);
153
154 #define MMCAM_FOURCC(a, b, c, d)  (guint32)((a)|(b)<<8|(c)<<16|(d)<<24)
155 #define MMCAM_FOURCC_ARGS(fourcc) \
156                 ((gchar)((fourcc)&0xff)), \
157                 ((gchar)(((fourcc)>>8)&0xff)), \
158                 ((gchar)(((fourcc)>>16)&0xff)), \
159                 ((gchar)(((fourcc)>>24)&0xff))
160
161 #define MMCAM_SEND_MESSAGE(handle, msg_id, msg_code) \
162 {\
163         _MMCamcorderMsgItem msg;\
164         msg.id = msg_id;\
165         msg.param.code = msg_code;\
166         _mmcam_dbg_log("msg id : %x, code : %x", msg_id, msg_code);\
167         _mmcamcorder_send_message((MMHandleType)handle, &msg);\
168 }
169
170
171 /*=======================================================================================
172 | ENUM DEFINITIONS                                                                      |
173 ========================================================================================*/
174 /**
175  * Structure of GDBus Callback.
176  */
177 typedef struct {
178         GCond sync_cond;
179         GMutex sync_mutex;
180         int param;
181         int is_playing;
182         guint subscribe_id;
183 } _MMCamcorderGDbusCbInfo;
184
185 /**
186  *Type define of util.
187  */
188 typedef enum {
189         _MMCAMCORDER_HANDLER_PREVIEW = (1 << 0),
190         _MMCAMCORDER_HANDLER_VIDEOREC = (1 << 1),
191         _MMCAMCORDER_HANDLER_STILLSHOT = (1 << 2),
192         _MMCAMCORDER_HANDLER_AUDIOREC = (1 << 3),
193 } _MMCamcorderHandlerCategory;
194
195 /*=======================================================================================
196 | STRUCTURE DEFINITIONS                                                                 |
197 ========================================================================================*/
198
199 /**
200  * Structure of location info
201  */
202 typedef struct {
203         gint32 longitude;
204         gint32 latitude;
205         gint32 altitude;
206 } _MMCamcorderLocationInfo;
207
208 /**
209  * Structure of handler item
210  */
211 typedef struct {
212         GObject *object;
213         _MMCamcorderHandlerCategory category;
214         gulong handler_id;
215 } MMCamcorderHandlerItem;
216
217 /**
218  * Structure of message item
219  */
220 typedef struct {
221         MMHandleType handle;        /**< handle */
222         int id;                     /**< message id */
223         MMMessageParamType param;   /**< message parameter */
224         GMutex lock;                /**< mutex for item */
225 } _MMCamcorderMsgItem;
226
227
228 /*=======================================================================================
229 | CONSTANT DEFINITIONS                                                                  |
230 ========================================================================================*/
231 #define G_DBUS_CB_TIMEOUT_MSEC                  3000
232 #define G_DBUS_REPLY_TIMEOUT                    (120 * 1000)
233 #define FAT32_FILE_SYSTEM_MAX_SIZE              (4294967295UL)     /* 4 GigaByte - 1 byte */
234 #define NANO_SEC_PER_MILI_SEC                   1000000
235 #define _MMCAMCORDER_HANDLER_CATEGORY_ALL \
236         (_MMCAMCORDER_HANDLER_PREVIEW | _MMCAMCORDER_HANDLER_VIDEOREC |_MMCAMCORDER_HANDLER_STILLSHOT | _MMCAMCORDER_HANDLER_AUDIOREC)
237
238
239 /*=======================================================================================
240 | GLOBAL FUNCTION PROTOTYPES                                                            |
241 ========================================================================================*/
242 /* GStreamer */
243 void _mmcamcorder_remove_buffer_probe(MMHandleType handle, _MMCamcorderHandlerCategory category);
244 void _mmcamcorder_remove_one_buffer_probe(MMHandleType handle, void *object);
245 void _mmcamcorder_remove_event_probe(MMHandleType handle, _MMCamcorderHandlerCategory category);
246 void _mmcamcorder_disconnect_signal(MMHandleType handle, _MMCamcorderHandlerCategory category);
247 void _mmcamcorder_remove_all_handlers(MMHandleType handle, _MMCamcorderHandlerCategory category);
248 void _mmcamcorder_element_release_noti(gpointer data, GObject *where_the_object_was);
249 gboolean _mmcamcorder_add_elements_to_bin(GstBin *bin, GList *element_list);
250 gboolean _mmcamcorder_link_elements(GList *element_list);
251
252 /* Message */
253 #ifdef _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK
254 gboolean _mmcamcorder_msg_callback(void *data);
255 #endif /* _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK */
256 gboolean _mmcamcorder_send_message(MMHandleType handle, _MMCamcorderMsgItem *data);
257 void _mmcamcorder_remove_message_all(MMHandleType handle);
258
259 /* Pixel format */
260 int _mmcamcorder_get_pixel_format(GstCaps *pad);
261 int _mmcamcorder_get_pixtype(unsigned int fourcc);
262 unsigned int _mmcamcorder_get_fourcc(int pixtype, int codectype, int use_zero_copy_format);
263
264 /* JPEG encode */
265 gboolean _mmcamcorder_encode_jpeg(void *src_data, unsigned int src_width, unsigned int src_height,
266         int src_format, unsigned int src_length, unsigned int jpeg_quality,
267         void **result_data, unsigned int *result_length);
268 /* resize */
269 gboolean _mmcamcorder_resize_frame(unsigned char *src_data, unsigned int src_width, unsigned int src_height, unsigned int src_length, int src_format,
270         unsigned char **dst_data, unsigned int *dst_width, unsigned int *dst_height, unsigned int *dst_length);
271 gboolean _mmcamcorder_downscale_UYVYorYUYV(unsigned char *src, unsigned int src_width, unsigned int src_height,
272         unsigned char **dst, unsigned int dst_width, unsigned int dst_height);
273
274 /* Recording */
275 /* find top level tag only, do not use this function for finding sub level tags.
276    tag_fourcc is Four-character-code (FOURCC) */
277 gint _mmcamcorder_find_tag(FILE *f, guint32 tag_fourcc, gboolean do_rewind);
278 gboolean _mmcamcorder_find_fourcc(FILE *f, guint32 tag_fourcc, gboolean do_rewind);
279 gint32 _mmcamcorder_double_to_fix(gdouble d_number);
280 gboolean _mmcamcorder_update_size(FILE *f, gint64 prev_pos, gint64 curr_pos);
281 gboolean _mmcamcorder_write_loci(FILE *f, _MMCamcorderLocationInfo info);
282 gboolean _mmcamcorder_write_geodata(FILE *f, _MMCamcorderLocationInfo info);
283 gboolean _mmcamcorder_write_udta(FILE *f, int gps_enable, _MMCamcorderLocationInfo info, _MMCamcorderLocationInfo geotag);
284 guint64 _mmcamcorder_get_container_size(const guchar *size);
285 guint64 _mmcamcorder_get_container_size64(const guchar *size);
286 gboolean _mmcamcorder_update_composition_matrix(FILE *f, int orientation);
287
288 /* File system */
289 int _mmcamcorder_get_freespace(const gchar *path, const gchar *root_directory, guint64 *free_space);
290 int _mmcamcorder_get_file_size(const char *filename, guint64 *size);
291 int _mmcamcorder_get_file_system_type(const gchar *path, int *file_system_type);
292
293 /* Task */
294 void *_mmcamcorder_util_task_thread_func(void *data);
295
296 /* device */
297 int _mmcamcorder_get_device_flash_brightness(GDBusConnection *conn, int *brightness);
298
299 /* sound play via dbus*/
300 int _mmcamcorder_send_sound_play_message(GDBusConnection *conn, _MMCamcorderGDbusCbInfo *gdbus_info,
301         const char *sample_name, const char *stream_role, const char *volume_gain, int sync_play);
302
303 #ifdef __cplusplus
304 }
305 #endif
306
307 #endif /* __MM_CAMCORDER_UTIL_H__ */