Add tool package to separate testsuite binary
[platform/core/multimedia/libmm-streamrecorder.git] / src / mm_streamrecorder_ini.c
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 #include <glib.h>
23 #include <glib/gstdio.h>
24 #include <stdlib.h>
25 #include <iniparser.h>
26 #include <mm_error.h>
27 #include "mm_streamrecorder_ini.h"
28
29 static gboolean loaded = FALSE;
30
31 /* global variables here */
32 static  void    __get_element_list(mm_streamrecorder_ini_t* ini, const gchar* str, int keyword_type);
33
34 static void __mm_streamrecorder_ini_check_status(void);
35
36 /* macro */
37 #define MM_STREAMRECORDER_INI_GET_STRING(x_dict, x_item, x_ini, x_default) \
38 do { \
39         gint length = 0; \
40         const gchar *str = iniparser_getstring(x_dict, x_ini, x_default); \
41         if (str) { \
42                 length = strlen(str); \
43                 if ((length > 1) && (length < STREAMRECORDER_INI_MAX_STRLEN)) \
44                         strncpy(x_item, str, STREAMRECORDER_INI_MAX_STRLEN-1); \
45                 else \
46                         strncpy(x_item, x_default, STREAMRECORDER_INI_MAX_STRLEN-1); \
47         } else { \
48                 strncpy(x_item, x_default, STREAMRECORDER_INI_MAX_STRLEN-1); \
49         } \
50 } while (0)
51
52 /* x_ini is the list of value to be set at x_list[index] */
53 #define MMSTREAMRECORDER_INI_GET_INT_FROM_LIST(x_dict, x_list, x_list_max, x_ini, x_default) \
54 do { \
55         int index = 0; \
56         int value = 0; \
57         const char *delimiters = " ,"; \
58         char *usr_ptr = NULL; \
59         char *token = NULL; \
60         gchar temp_arr[STREAMRECORDER_INI_MAX_STRLEN] = {0}; \
61         MM_STREAMRECORDER_INI_GET_STRING(x_dict, temp_arr, x_ini, x_default); \
62         token = strtok_r(temp_arr, delimiters, &usr_ptr); \
63         while (token) { \
64                 if (index > x_list_max -1) { \
65                         _mmstreamrec_dbg_err("%d is not valid index\n", index); \
66                         break; \
67                 } else { \
68                         value = atoi(token); \
69                         x_list[index] = value; \
70                         index++; \
71                 } \
72                 token = strtok_r(NULL, delimiters, &usr_ptr); \
73         } \
74 } while (0)
75
76 int _mm_streamrecorder_ini_load(mm_streamrecorder_ini_t *ini)
77 {
78         dictionary *dict = NULL;
79
80         _mmstreamrec_dbg_warn("enter");
81
82         __mm_streamrecorder_ini_check_status();
83
84         /* first, try to load existing ini file */
85         dict = iniparser_load(MM_STREAMRECORDER_INI_DEFAULT_PATH);
86
87         /* get ini values */
88         memset(ini, 0, sizeof(mm_streamrecorder_ini_t));
89
90         if (dict) {                                     /* if dict is available */
91                 /* general */
92                 ini->encsink_src_islive = iniparser_getboolean(dict, "general:encscink source is live", DEFAULT_ENCSINK_SRC_IS_LIVE);
93                 ini->retrial_count = iniparser_getint(dict, "general:retrialcount", DEFAULT_RETRIAL_COUNT);
94                 ini->minimum_frame = iniparser_getint(dict, "general:minimum frame", DEFAULT_MINIMUM_FRAME);
95                 ini->convert_output_buffer_num = iniparser_getint(dict, "general:convert output buffer num", DEFAULT_CONVERT_OUTPUT_BUFFER_NUM);
96                 ini->reset_pause_time = iniparser_getint(dict, "general:reset pause time", DEFAULT_RESET_PAUSE_TIME);
97                 ini->screen_record = iniparser_getint(dict, "general:screen record", DEFAULT_SCREEN_RECORD);
98                 ini->hw_encoder_supported = iniparser_getboolean(dict, "general:hw encoder supported", DEFAULT_HW_ENCODER_SUPPORTED);
99                 MM_STREAMRECORDER_INI_GET_STRING(dict, ini->video_codec_element_hw, "general:video codec element hw", DEFAULT_VIDEO_SOURCE);
100
101                 /*encodebin */
102                 ini->encsink_bin_profile = iniparser_getint(dict, "encodebin:encsink bin profile", DEFAULT_ENCSINK_BIN_PROFILE);
103                 ini->encsink_bin_auto_audio_resample = iniparser_getboolean(dict, "encodebin:encsink bin auto audio resample", DEFAULT_ENCSINK_BIN_AUTO_AUDIO_RESAMPLE);
104                 ini->encsink_bin_auto_colorspace = iniparser_getboolean(dict, "encodebin: encsink bin auto colorspace", DEFAULT_ENCSINK_BIN_AUTO_COLORSPACE);
105                 ini->encsink_bin_use_video_toggle = iniparser_getboolean(dict, "encodebin:encsink bin use video toggle", DEFAULT_ENCSINK_BIN_USE_VIDEO_TOGGLE);
106                 ini->encsink_bin_auto_audio_convert = iniparser_getboolean(dict, "encodebin:encsink bin auto audio convert", DEFAULT_ENCSINK_BIN_AUTO_CONVERT);
107
108                 /* pipeline */
109                 MM_STREAMRECORDER_INI_GET_STRING(dict, ini->name_of_encsink_src, "pipeline:encsink bin source", DEFAULT_VIDEO_SOURCE);
110                 MM_STREAMRECORDER_INI_GET_STRING(dict, ini->name_of_audio_src, "pipeline:name of audio src", DEFAULT_AUDIO_SRC);
111                 MM_STREAMRECORDER_INI_GET_STRING(dict, ini->h264_video_encoder, "pipeline:h264 encoder", DEFAULT_NAME_OF_H264_VIDEO_ENCODER);
112                 MM_STREAMRECORDER_INI_GET_STRING(dict, ini->h263_video_encoder, "pipeline:h263 encoder", DEFAULT_NAME_OF_H263_VIDEO_ENCODER);
113                 MM_STREAMRECORDER_INI_GET_STRING(dict, ini->mpeg4_video_encoder, "pipeline:mpeg4 encoder", DEFAULT_NAME_OF_MPEG4_VIDEO_ENCODER);
114                 MM_STREAMRECORDER_INI_GET_STRING(dict, ini->name_of_encsink_bin_audio_encoder, "pipeline:name of audio encoder", DEFAULT_NAME_OF_AUDIO_ENCODER);
115                 MM_STREAMRECORDER_INI_GET_STRING(dict, ini->encsink_bin_use_parser, "pipeline:use parser", DEFAULT_USE_PARSER);
116                 MM_STREAMRECORDER_INI_GET_STRING(dict, ini->name_of_encsink_bin_video_converter, "pipeline:name of video converter", DEFAULT_NAME_OF_VIDEO_CONVERTER);
117                 MM_STREAMRECORDER_INI_GET_STRING(dict, ini->name_of_encsink_bin_3GPMUXER, "pipeline:name of 3GP muxer", DEFAULT_NAME_OF_3GP_MUXER);
118                 MM_STREAMRECORDER_INI_GET_STRING(dict, ini->name_of_encsink_bin_MP4MUXER, "pipeline:name of MP4 muxer", DEFAULT_NAME_OF_MP4_MUXER);
119                 MM_STREAMRECORDER_INI_GET_STRING(dict, ini->name_of_encsink_sink, "pipeline:name of sink", DEFAULT_NAME_OF_BIN_SINK);
120
121                 /* audio parameter */
122                 ini->audio_frame_minimum_space = iniparser_getint(dict, "audio param:audio frame minimum space", DEFAULT_AUDIO_FRAME_MINIMUM_SPACE);
123                 ini->audio_frame_wait_time = iniparser_getint(dict, "audio param:audio frame wait time", DEFAULT_AUDIO_FRAME_WAIT_TIME);
124
125                 /* video parameter */
126                 ini->video_frame_wait_time = iniparser_getint(dict, "video param:video frame wait time", DEFAULT_VIDEO_FRAME_WAIT_TIME);
127
128                 /*supported attribute*/
129                 MMSTREAMRECORDER_INI_GET_INT_FROM_LIST(dict, ini->supported_video_width, STREAMRECORDER_ATTRIBUTE_NUM_MAX, "attribute:supported width", DEFAULT_SUPPORTED_WIDTH);
130                 MMSTREAMRECORDER_INI_GET_INT_FROM_LIST(dict, ini->supported_video_height, STREAMRECORDER_ATTRIBUTE_NUM_MAX, "attribute:supported height", DEFAULT_SUPPORTED_HEIGHT);
131                 __get_element_list(ini, iniparser_getstring(dict, "attribute:supported audio encoders", (char*)DEFAULT_SUPPORTED_AUDIO_ENCODERS), KEYWORD_AUDIO_ENCODERS);
132                 __get_element_list(ini, iniparser_getstring(dict, "attribute:supported video encoders", (char*)DEFAULT_SUPPORTED_VIDEO_ENCODERS), KEYWORD_VIDEO_ENCODERS);
133                 __get_element_list(ini, iniparser_getstring(dict, "attribute:supported file formats", (char*)DEFAULT_SUPPORTED_FILE_FORMATS), KEYWORD_FILE_FORMATS);
134
135                 /* debug */
136                 ini->generate_dot = iniparser_getboolean(dict, "debug:generate dot", DEFAULT_GENERATE_DOT);
137
138         } else {                                        /* if dict is not available just fill the structure with default value */
139                 _mmstreamrec_dbg_err("failed to load ini. using hardcoded default\n");
140                 /* general */
141                 ini->encsink_src_islive = DEFAULT_ENCSINK_SRC_IS_LIVE;
142                 ini->retrial_count = DEFAULT_RETRIAL_COUNT;
143                 ini->minimum_frame = DEFAULT_MINIMUM_FRAME;
144                 ini->convert_output_buffer_num = DEFAULT_CONVERT_OUTPUT_BUFFER_NUM;
145                 ini->reset_pause_time = DEFAULT_RESET_PAUSE_TIME;
146                 ini->screen_record = DEFAULT_SCREEN_RECORD;
147                 ini->hw_encoder_supported = DEFAULT_HW_ENCODER_SUPPORTED;
148                 strncpy(ini->video_codec_element_hw, DEFAULT_VIDEO_CODEC_ELEMENT_HW, STREAMRECORDER_INI_MAX_STRLEN - 1);
149
150                 /*encodebin */
151                 ini->encsink_bin_profile = DEFAULT_ENCSINK_BIN_PROFILE;
152                 ini->encsink_bin_auto_audio_resample = DEFAULT_ENCSINK_BIN_AUTO_AUDIO_RESAMPLE;
153                 ini->encsink_bin_auto_colorspace = DEFAULT_ENCSINK_BIN_AUTO_COLORSPACE;
154                 ini->encsink_bin_use_video_toggle = DEFAULT_ENCSINK_BIN_USE_VIDEO_TOGGLE;
155                 ini->encsink_bin_auto_audio_convert = DEFAULT_ENCSINK_BIN_AUTO_CONVERT;
156
157                 /* pipeline */
158                 strncpy(ini->name_of_encsink_src, DEFAULT_VIDEO_SOURCE, STREAMRECORDER_INI_MAX_STRLEN - 1);
159                 strncpy(ini->name_of_audio_src, DEFAULT_AUDIO_SRC, STREAMRECORDER_INI_MAX_STRLEN - 1);
160                 strncpy(ini->h264_video_encoder, DEFAULT_NAME_OF_H264_VIDEO_ENCODER, STREAMRECORDER_INI_MAX_STRLEN - 1);
161                 strncpy(ini->h263_video_encoder, DEFAULT_NAME_OF_H263_VIDEO_ENCODER, STREAMRECORDER_INI_MAX_STRLEN - 1);
162                 strncpy(ini->mpeg4_video_encoder, DEFAULT_NAME_OF_MPEG4_VIDEO_ENCODER, STREAMRECORDER_INI_MAX_STRLEN - 1);
163                 strncpy(ini->name_of_encsink_bin_audio_encoder, DEFAULT_NAME_OF_AUDIO_ENCODER, STREAMRECORDER_INI_MAX_STRLEN - 1);
164                 strncpy(ini->encsink_bin_use_parser, DEFAULT_USE_PARSER, STREAMRECORDER_INI_MAX_STRLEN - 1);
165                 strncpy(ini->name_of_encsink_bin_video_converter, DEFAULT_NAME_OF_VIDEO_CONVERTER, STREAMRECORDER_INI_MAX_STRLEN - 1);
166                 strncpy(ini->name_of_encsink_bin_3GPMUXER, DEFAULT_NAME_OF_3GP_MUXER, STREAMRECORDER_INI_MAX_STRLEN - 1);
167                 strncpy(ini->name_of_encsink_bin_MP4MUXER, DEFAULT_NAME_OF_MP4_MUXER, STREAMRECORDER_INI_MAX_STRLEN - 1);
168                 strncpy(ini->name_of_encsink_sink, DEFAULT_NAME_OF_BIN_SINK, STREAMRECORDER_INI_MAX_STRLEN - 1);
169
170                 /* audio parameter */
171                 ini->audio_frame_minimum_space = DEFAULT_AUDIO_FRAME_MINIMUM_SPACE;
172                 ini->audio_frame_wait_time = DEFAULT_AUDIO_FRAME_WAIT_TIME;
173
174                 /* video parameter */
175                 ini->video_frame_wait_time = DEFAULT_VIDEO_FRAME_WAIT_TIME;
176
177                 /*supported attributes*/
178                 __get_element_list(ini, (char *)DEFAULT_SUPPORTED_HEIGHT, KEYWORD_VIDEO_HEIGHT);
179                 __get_element_list(ini, (char *)DEFAULT_SUPPORTED_WIDTH, KEYWORD_VIDEO_WIDTH);
180                 __get_element_list(ini, (char *)DEFAULT_SUPPORTED_AUDIO_ENCODERS, KEYWORD_AUDIO_ENCODERS);
181                 __get_element_list(ini, (char *)DEFAULT_SUPPORTED_VIDEO_ENCODERS, KEYWORD_VIDEO_ENCODERS);
182                 __get_element_list(ini, (char *)DEFAULT_SUPPORTED_FILE_FORMATS, KEYWORD_FILE_FORMATS);
183
184                 /* debug */
185                 ini->generate_dot = DEFAULT_GENERATE_DOT;
186         }
187
188         /* free dict as we got our own structure */
189         if (dict)
190                 iniparser_freedict(dict);
191
192         /* dump structure */
193         _mmstreamrec_dbg_log("[Stream Recorder initial setting][Start]");
194
195         /* general */
196         _mmstreamrec_dbg_log("encsink_src_islive : %d", ini->encsink_src_islive);
197         _mmstreamrec_dbg_log("retrial_count : %d", ini->retrial_count);
198         _mmstreamrec_dbg_log("minimum_frame : %d", ini->minimum_frame);
199         _mmstreamrec_dbg_log("convert_output_buffer_num : %d", ini->convert_output_buffer_num);
200         _mmstreamrec_dbg_log("reset_pause_time : %d", ini->reset_pause_time);
201         _mmstreamrec_dbg_log("screen_record : %d", ini->screen_record);
202         _mmstreamrec_dbg_log("hw_encoder_supported : %d", ini->hw_encoder_supported);
203         _mmstreamrec_dbg_log("video_codec_element_hw : %s", ini->video_codec_element_hw);
204
205         /*encodebin */
206         _mmstreamrec_dbg_log("encode bin profile : %d", ini->encsink_bin_profile);
207         _mmstreamrec_dbg_log("encode bin auto audio resample property  : %d", ini->encsink_bin_auto_audio_resample);
208         _mmstreamrec_dbg_log("encode bin auto colorspace property : %d", ini->encsink_bin_auto_colorspace);
209         _mmstreamrec_dbg_log("encode bin use video toggle property : %d", ini->encsink_bin_use_video_toggle);
210         _mmstreamrec_dbg_log("encode bin auto audio convert property : %d", ini->encsink_bin_auto_audio_convert);
211
212         /* pipeline */
213         _mmstreamrec_dbg_log("name_of_encodebin_source : %s", ini->name_of_encsink_src);
214         _mmstreamrec_dbg_log("name_of_audio_source : %s", ini->name_of_audio_src);
215         _mmstreamrec_dbg_log("name_of_h264_video_encoder : %s", ini->h264_video_encoder);
216         _mmstreamrec_dbg_log("name_of_h263_video_encoder : %s", ini->h263_video_encoder);
217         _mmstreamrec_dbg_log("name_of_mpeg4_video_encoder : %s", ini->mpeg4_video_encoder);
218         _mmstreamrec_dbg_log("name_of_audio_encoder : %s", ini->name_of_encsink_bin_audio_encoder);
219         _mmstreamrec_dbg_log("name_of_video_converter : %s", ini->name_of_encsink_bin_video_converter);
220         _mmstreamrec_dbg_log("name_of_3GP_muxer : %s", ini->name_of_encsink_bin_3GPMUXER);
221         _mmstreamrec_dbg_log("name_of_MP4_muxer : %s", ini->name_of_encsink_bin_MP4MUXER);
222         _mmstreamrec_dbg_log("name_of_sink : %s", ini->name_of_encsink_sink);
223
224         /* audio parameter */
225         _mmstreamrec_dbg_log("audio_frame_minimum_space : %d", ini->audio_frame_minimum_space);
226         _mmstreamrec_dbg_log("audio_frame_wait_time : %d", ini->audio_frame_wait_time);
227
228         /* video parameter */
229         _mmstreamrec_dbg_log("video_frame_wait_time : %d", ini->video_frame_wait_time);
230
231         /* debug */
232         _mmstreamrec_dbg_log("generate_dot : %d", ini->generate_dot);
233         if (ini->generate_dot == TRUE) {
234                 _mmstreamrec_dbg_log("generate_dot is TRUE, dot file will be stored into /tmp/");
235                 g_setenv("GST_DEBUG_DUMP_DOT_DIR", "/tmp/", TRUE);
236         }
237         _mmstreamrec_dbg_log("[Stream Recorder initial setting][End]");
238
239         loaded = TRUE;
240
241         _mmstreamrec_dbg_warn("leave");
242
243         return MM_ERROR_NONE;
244 }
245
246 static
247 void    __get_element_list(mm_streamrecorder_ini_t* ini, const gchar* str, int keyword_type)
248 {
249         gchar** list = NULL;
250         gchar** walk = NULL;
251         int i = 0;
252         gchar* strtmp = NULL;
253
254         if (!str)
255                 return;
256
257         if (strlen(str) < 1)
258                 return;
259
260         strtmp = g_strdup(str);
261
262         /* trimming. it works inplace */
263         g_strstrip(strtmp);
264
265
266         /* split */
267         list = g_strsplit(strtmp, ",", 10);
268
269         if (!list) {
270                 if (strtmp)
271                         g_free(strtmp);
272
273                 return;
274         }
275
276         /* copy list */
277         switch (keyword_type) {
278         case KEYWORD_VIDEO_HEIGHT:
279         {
280                 for (walk = list; *walk; walk++) {
281                         ini->supported_video_height[i] = atoi(*walk);
282                         i++;
283                 }
284                 break;
285         }
286         case KEYWORD_VIDEO_WIDTH:
287         {
288                 for (walk = list; *walk; walk++) {
289                         ini->supported_video_width[i] = atoi(*walk);
290                         i++;
291                 }
292                 break;
293         }
294         case KEYWORD_AUDIO_ENCODERS:
295         {
296                 for (walk = list; *walk; walk++) {
297                         strncpy(ini->supported_audio_encoders[i], *walk, (STREAMRECORDER_INI_MAX_STRLEN - 1));
298                         g_strstrip(ini->supported_audio_encoders[i]);
299                         ini->supported_audio_encoders[i][STREAMRECORDER_INI_MAX_STRLEN -1] = '\0';
300                         i++;
301                 }
302                 /* mark last item to NULL*/
303                 ini->supported_audio_encoders[i][0] = '\0';
304                 break;
305         }
306         case KEYWORD_VIDEO_ENCODERS:
307         {
308                 for (walk = list; *walk; walk++) {
309                         strncpy(ini->supported_video_encoders[i], *walk, (STREAMRECORDER_INI_MAX_STRLEN - 1));
310                         g_strstrip(ini->supported_video_encoders[i]);
311                         ini->supported_video_encoders[i][STREAMRECORDER_INI_MAX_STRLEN -1] = '\0';
312                         i++;
313                 }
314                 /* mark last item to NULL */
315                 ini->supported_video_encoders[i][0] = '\0';
316                 break;
317         }
318         case KEYWORD_FILE_FORMATS:
319         {
320                 for (walk = list; *walk; walk++) {
321                         strncpy(ini->supported_file_formats[i], *walk, (STREAMRECORDER_INI_MAX_STRLEN - 1));
322                         g_strstrip(ini->supported_file_formats[i]);
323                         ini->supported_file_formats[i][STREAMRECORDER_INI_MAX_STRLEN -1] = '\0';
324                         i++;
325                 }
326                 /* mark last item to NULL*/
327                 ini->supported_file_formats[i][0] = '\0';
328                 break;
329         }
330         default:
331         break;
332         }
333
334         g_strfreev(list);
335         if (strtmp)
336                 g_free(strtmp);
337 }
338
339
340 static
341 void __mm_streamrecorder_ini_check_status(void)
342 {
343         GStatBuf ini_buff;
344         char buf[255] = {0, };
345
346         _mmstreamrec_dbg_warn("enter");
347
348         if (g_stat(MM_STREAMRECORDER_INI_DEFAULT_PATH, &ini_buff) < 0) {
349                 _mmstreamrec_dbg_err("failed to get mmfw_streamrecorder.ini status\n");
350         } else {
351                 if (ini_buff.st_size < 5) {
352                         _mmstreamrec_dbg_err("mmfw_streamrecorder.ini file size=%d, Corrupted! So, Removed\n", (int)ini_buff.st_size);
353                         if (g_remove(MM_STREAMRECORDER_INI_DEFAULT_PATH) == -1) {
354                                 strerror_r(errno, buf, sizeof(buf));
355                                 _mmstreamrec_dbg_err("failed to delete corrupted ini [%s]", buf);
356                         }
357                 }
358         }
359
360         _mmstreamrec_dbg_warn("leave");
361 }
362
363 int _mm_streamrecorder_ini_unload(mm_streamrecorder_ini_t *ini)
364 {
365         _mmstreamrec_dbg_warn("enter");
366
367         loaded = FALSE;
368
369         _mmstreamrec_dbg_warn("leave");
370
371         return MM_ERROR_NONE;
372 }