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