fix double free
[platform/core/multimedia/libmm-streamrecorder.git] / src / include / mm_streamrecorder_util.h
1 /*
2  * libmm-streamrecorder
3  *
4  * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyuntae Kim <ht1211.kim@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_STREAMRECORDER_UTIL_H__
23 #define __MM_STREAMRECORDER_UTIL_H__
24
25 /*=======================================================================================
26 | INCLUDE FILES                                                                         |
27 ========================================================================================*/
28 #include <gst/gst.h>
29 #include <gst/gstutils.h>
30 #include <gst/gstpad.h>
31 #include <glib.h>
32 #include <stdio.h>
33 #include <dlog.h>
34
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38
39 /*=======================================================================================
40 | GLOBAL DEFINITIONS AND DECLARATIONS FOR STREAMRECORDER                                        |
41 ========================================================================================*/
42
43 /*=======================================================================================
44 | MACRO DEFINITIONS                                                                     |
45 ========================================================================================*/
46 #ifndef CLEAR
47 #define CLEAR(x)            memset(&(x), 0, sizeof(x))
48 #endif
49
50 #define SAFE_FREE(x) \
51         if (x) {\
52                 g_free(x); \
53                 x = NULL; \
54         }
55
56 #ifdef LOG_TAG
57 #undef LOG_TAG
58 #endif
59 #define LOG_TAG "MM_STREAMRECORDER"
60
61 #define _mmstreamrec_dbg_verb(fmt, args...)  SLOGI(fmt, ##args);
62 #define _mmstreamrec_dbg_log(fmt, args...)   SLOGD(fmt, ##args);
63 #define _mmstreamrec_dbg_warn(fmt, args...)  SLOGW(fmt, ##args);
64 #define _mmstreamrec_dbg_err(fmt, args...)   SLOGE(fmt, ##args);
65 #define _mmstreamrec_dbg_crit(fmt, args...)  SLOGF(fmt, ##args);
66
67 /**
68  *      Macro for checking validity and debugging
69  */
70 #define mmf_return_if_fail(expr)        \
71         if (!(expr)) { \
72                 _mmstreamrec_dbg_err("failed [%s]", #expr);     \
73                 return;                                         \
74         };
75
76 /**
77  *      Macro for checking validity and debugging
78  */
79 #define mmf_return_val_if_fail(expr, val)       \
80         do { \
81                 if (!(expr)) { \
82                         _mmstreamrec_dbg_err("failed [%s]", #expr);     \
83                         return (val); \
84                 } \
85         } while (0)
86
87 /**
88  *      Minimum integer value
89  */
90 #define _MMSTREAMRECORDER_MIN_INT       (INT_MIN)
91
92 /**
93  *      Maximum integer value
94  */
95 #define _MMSTREAMRECORDER_MAX_INT       (INT_MAX)
96
97 /**
98  *      Minimum double value
99  */
100 #define _MMSTREAMRECORDER_MIN_DOUBLE    (DBL_MIN)
101
102 /**
103  *      Maximum integer value
104  */
105 #define _MMSTREAMRECORDER_MAX_DOUBLE    (DBL_MAX)
106
107 // TODO : MOVE OTHERS
108 enum {
109         PUSH_ENCODING_BUFFER_INIT = 0,
110         PUSH_ENCODING_BUFFER_RUN,
111         PUSH_ENCODING_BUFFER_STOP,
112 };
113
114 #define FPUTC_CHECK(x_char, x_file)\
115 {\
116         if (fputc(x_char, x_file) == EOF) {\
117                 _mmstreamrec_dbg_err("[Critical] fputc() returns fail.\n");     \
118                 return FALSE;\
119         } \
120 }
121
122 #define FPUTS_CHECK(x_str, x_file)\
123 {\
124         if (fputs(x_str, x_file) == EOF) {\
125                 _mmstreamrec_dbg_err("[Critical] fputs() returns fail.\n");\
126                 SAFE_FREE(str); \
127                 return FALSE;\
128         } \
129 }
130
131 /*=======================================================================================
132 | ENUM DEFINITIONS                                                                      |
133 ========================================================================================*/
134 #define MAX_ERROR_MESSAGE_LEN         128
135 #define MM_STREAMRECORDER_MIN_LOG_COUNT 10
136
137 /*=======================================================================================
138 | STRUCTURE DEFINITIONS                                                                 |
139 ========================================================================================*/
140 #if 0
141         typedef struct __mm_streamrecorder_ini {
142                 /* video converter element */
143                 gchar video_converter_element[STREAMRECORDER_INI_MAX_STRLEN];
144         } mm_streamrecorder_ini_t;
145 #endif
146
147 /*=======================================================================================
148 | CONSTANT DEFINITIONS                                                                  |
149 ========================================================================================*/
150
151 /*=======================================================================================
152 | GLOBAL FUNCTION PROTOTYPES                                                            |
153 ========================================================================================*/
154
155 /* Pixel format */
156
157 int _mmstreamrecorder_get_pixel_format(GstCaps *caps);
158 int _mmstreamrecorder_get_pixtype(unsigned int fourcc);
159 unsigned int _mmstreamrecorder_get_fourcc(int pixtype, int codectype, int use_zero_copy_format);
160
161 /* Recording */
162 /* find top level tag only, do not use this function for finding sub level tags. tag_fourcc is Four-character-code (FOURCC) */
163 gint32 _mmstreamrecorder_double_to_fix(gdouble d_number);
164
165 /* File system */
166 int _mmstreamrecorder_get_freespace(const gchar *path, guint64 *free_space);
167 int _mmstreamrecorder_get_file_size(const char *filename, guint64 *size);
168
169 /* Debug */
170 void _mmstreamrecorder_err_trace_write(char *str_filename, char *func_name, int line_num, char *fmt, ...);
171
172 guint16 get_language_code(const char *str);
173 gchar *str_to_utf8(const gchar *str);
174 __attribute__ ((gnu_inline)) inline gboolean write_tag(FILE *f, const gchar *tag);
175 __attribute__ ((gnu_inline)) inline gboolean write_to_32(FILE *f, guint val);
176 #ifdef __cplusplus
177 }
178 #endif
179 #endif                                                  /* __MM_STREAMRECORDER_UTIL_H__ */