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