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