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