tizen beta release
[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
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 /*=======================================================================================
34 | GLOBAL DEFINITIONS AND DECLARATIONS FOR CAMCORDER                                     |
35 ========================================================================================*/
36
37 /*=======================================================================================
38 | MACRO DEFINITIONS                                                                     |
39 ========================================================================================*/
40 #ifndef CLEAR
41 #define CLEAR(x)            memset (&(x), 0, sizeof (x))
42 #endif
43
44 #define MMCAMCORDER_ADD_BUFFER_PROBE(x_pad, x_category, x_callback, x_hcamcorder) \
45 do { \
46         MMCamcorderHandlerItem *item = NULL; \
47         item = (MMCamcorderHandlerItem *)g_malloc(sizeof(MMCamcorderHandlerItem)); \
48         if (!item) {\
49                 _mmcam_dbg_err("Cannot connect buffer probe [malloc fail] \n"); \
50         } else if (x_category == 0 || !(x_category & _MMCAMCORDER_HANDLER_CATEGORY_ALL)) { \
51                 _mmcam_dbg_err("Invalid handler category : %x \n", x_category); \
52         } else { \
53                 item->object = G_OBJECT(x_pad); \
54                 item->category = x_category; \
55                 item->handler_id = gst_pad_add_buffer_probe(x_pad, G_CALLBACK(x_callback), x_hcamcorder); \
56                 x_hcamcorder->buffer_probes = g_list_append(x_hcamcorder->buffer_probes, item); \
57                 _mmcam_dbg_log("Adding buffer probe on [%s:%s] - [ID : %lu], [Category : %x] ", GST_DEBUG_PAD_NAME(item->object), item->handler_id, item->category); \
58         } \
59 } while (0);
60
61 #define MMCAMCORDER_ADD_EVENT_PROBE(x_pad, x_category, x_callback, x_hcamcorder) \
62 do { \
63         MMCamcorderHandlerItem *item = NULL; \
64         item = (MMCamcorderHandlerItem *) g_malloc(sizeof(MMCamcorderHandlerItem)); \
65         if (!item) { \
66                 _mmcam_dbg_err("Cannot connect buffer probe [malloc fail] \n"); \
67         } \
68         else if (x_category == 0 || !(x_category & _MMCAMCORDER_HANDLER_CATEGORY_ALL)) { \
69                 _mmcam_dbg_err("Invalid handler category : %x \n", x_category); \
70         } else { \
71                 item->object =G_OBJECT(x_pad); \
72                 item->category = x_category; \
73                 item->handler_id = gst_pad_add_event_probe(x_pad, G_CALLBACK(x_callback), x_hcamcorder); \
74                 x_hcamcorder->event_probes = g_list_append(x_hcamcorder->event_probes, item); \
75                 _mmcam_dbg_log("Adding event probe on [%s:%s] - [ID : %lu], [Category : %x] ", GST_DEBUG_PAD_NAME(item->object), item->handler_id, item->category); \
76         } \
77 } while (0);
78
79 #define MMCAMCORDER_ADD_DATA_PROBE(x_pad, x_category, x_callback, x_hcamcorder) \
80 do { \
81         MMCamcorderHandlerItem *item = NULL; \
82         item = (MMCamcorderHandlerItem *) g_malloc(sizeof(MMCamcorderHandlerItem)); \
83         if (!item) { \
84                 _mmcam_dbg_err("Cannot connect buffer probe [malloc fail] \n"); \
85         } else if (x_category == 0 || !(x_category & _MMCAMCORDER_HANDLER_CATEGORY_ALL)) { \
86                 _mmcam_dbg_err("Invalid handler category : %x \n", x_category); \
87         } else { \
88                 item->object =G_OBJECT(x_pad); \
89                 item->category = x_category; \
90                 item->handler_id = gst_pad_add_data_probe(x_pad, G_CALLBACK(x_callback), x_hcamcorder); \
91                 x_hcamcorder->data_probes = g_list_append(x_hcamcorder->data_probes, item); \
92                 _mmcam_dbg_log("Adding data probe on [%s:%s] - [ID : %lu], [Category : %x] ", GST_DEBUG_PAD_NAME(item->object), item->handler_id, item->category); \
93         } \
94 } while (0);
95
96 #define MMCAMCORDER_SIGNAL_CONNECT( x_object, x_category, x_signal, x_callback, x_hcamcorder) \
97 do { \
98         MMCamcorderHandlerItem* item = NULL; \
99         item = (MMCamcorderHandlerItem *) g_malloc(sizeof(MMCamcorderHandlerItem)); \
100         if (!item) { \
101                 _mmcam_dbg_err("Cannot connect signal [%s]\n", x_signal ); \
102         } else if (x_category == 0 || !(x_category & _MMCAMCORDER_HANDLER_CATEGORY_ALL)) { \
103                 _mmcam_dbg_err("Invalid handler category : %x \n", x_category); \
104         } else { \
105                 item->object = G_OBJECT(x_object); \
106                 item->category = x_category; \
107                 item->handler_id = g_signal_connect(G_OBJECT(x_object), x_signal,\
108                                                     G_CALLBACK(x_callback), x_hcamcorder ); \
109                 x_hcamcorder->signals = g_list_append(x_hcamcorder->signals, item); \
110                 _mmcam_dbg_log("Connecting signal on [%s] - [ID : %lu], [Category : %x] ", GST_OBJECT_NAME(item->object), item->handler_id, item->category); \
111         } \
112 } while (0);
113
114 #define MMCAMCORDER_G_OBJECT_GET(obj, name, value) \
115 do { \
116         if (obj) { \
117                 if(g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(obj)), name)) { \
118                         g_object_get(G_OBJECT(obj), name, value, NULL); \
119                 } else { \
120                         _mmcam_dbg_warn ("The object doesn't have a property named(%s)", name); \
121                 } \
122         } else { \
123                 _mmcam_dbg_err("Null object"); \
124         } \
125 } while(0);
126
127 #define MMCAMCORDER_G_OBJECT_SET(obj, name, value) \
128 do { \
129         if (obj) { \
130                 if(g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(obj)), name)) { \
131                         g_object_set(G_OBJECT(obj), name, value, NULL); \
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 MMCAM_FOURCC(a,b,c,d)  (guint32)((a)|(b)<<8|(c)<<16|(d)<<24)
141 #define MMCAM_FOURCC_ARGS(fourcc) \
142         ((gchar)((fourcc)&0xff)), \
143         ((gchar)(((fourcc)>>8)&0xff)), \
144         ((gchar)(((fourcc)>>16)&0xff)), \
145         ((gchar)(((fourcc)>>24)&0xff))
146
147 /*=======================================================================================
148 | ENUM DEFINITIONS                                                                      |
149 ========================================================================================*/
150 /**
151  *Type define of util.
152  */
153 typedef enum {
154         _MMCAMCORDER_HANDLER_PREVIEW = (1 << 0),
155         _MMCAMCORDER_HANDLER_VIDEOREC = (1 << 1),
156         _MMCAMCORDER_HANDLER_STILLSHOT = (1 << 2),
157         _MMCAMCORDER_HANDLER_AUDIOREC = (1 << 3),
158 } _MMCamcorderHandlerCategory;
159
160 /*=======================================================================================
161 | STRUCTURE DEFINITIONS                                                                 |
162 ========================================================================================*/
163
164 /**
165  * Structure of location info
166  */
167 typedef struct {
168         gint32 longitude;
169         gint32 latitude;
170         gint32 altitude;
171 } _MMCamcorderLocationInfo;
172
173 /**
174  * Structure of handler item
175  */
176 typedef struct {
177         GObject *object;
178         _MMCamcorderHandlerCategory category;
179         gulong handler_id;
180 } MMCamcorderHandlerItem;
181
182 /**
183  * Structure of message item
184  */
185 typedef struct {
186         MMHandleType handle;            /**< handle */
187         int id;                         /**< message id */
188         MMMessageParamType param;       /**< message parameter */
189 } _MMCamcorderMsgItem;
190
191 /*=======================================================================================
192 | CONSTANT DEFINITIONS                                                                  |
193 ========================================================================================*/
194 #define _MMCAMCORDER_HANDLER_CATEGORY_ALL \
195         (_MMCAMCORDER_HANDLER_PREVIEW | _MMCAMCORDER_HANDLER_VIDEOREC |_MMCAMCORDER_HANDLER_STILLSHOT | _MMCAMCORDER_HANDLER_AUDIOREC)
196
197 /*=======================================================================================
198 | GLOBAL FUNCTION PROTOTYPES                                                            |
199 ========================================================================================*/
200 /* GStreamer */
201 void _mmcamcorder_remove_buffer_probe(MMHandleType handle, _MMCamcorderHandlerCategory category);
202 void _mmcamcorder_remove_event_probe(MMHandleType handle, _MMCamcorderHandlerCategory category);
203 void _mmcamcorder_remove_data_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 _mmcamcroder_msg_callback(void *data);
212 gboolean _mmcamcroder_send_message(MMHandleType handle, _MMCamcorderMsgItem *data);
213 void _mmcamcroder_remove_message_all(MMHandleType handle);
214
215 /* Pixel format */
216 int _mmcamcorder_get_pixel_format(GstBuffer *buffer);
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
225 /* Recording */
226 /* find top level tag only, do not use this function for finding sub level tags.
227    tag_fourcc is Four-character-code (FOURCC) */
228 gint _mmcamcorder_find_tag(FILE *f, guint32 tag_fourcc);
229 gint32 _mmcamcorder_double_to_fix(gdouble d_number);
230 gboolean _mmcamcorder_update_size(FILE *f, gint64 prev_pos, gint64 curr_pos);
231 gboolean _mmcamcorder_write_loci(FILE *f, _MMCamcorderLocationInfo info);
232 gboolean _mmcamcorder_write_udta(FILE *f, _MMCamcorderLocationInfo info);
233 gulong _mmcamcorder_get_container_size(const guchar *size);
234
235 /* File system */
236 int _mmcamcorder_get_freespace(const gchar *path, guint64 *free_space);
237 int _mmcamcorder_get_file_size(const char *filename, guint64 *size);
238
239 /* Debug */
240 void _mmcamcorder_err_trace_write(char *str_filename, char *func_name, int line_num, char *fmt, ...);
241
242 #ifdef __cplusplus
243 }
244 #endif
245
246 #endif /* __MM_CAMCORDER_UTIL_H__ */