2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @file mediademuxer_ini.c
19 * @brief INI laoding and parsing procedures
22 #ifndef __MEDIADEMUXER_INI_C__
23 #define __MEDIADEMUXER_INI_C__
28 #include <glib/gstdio.h>
29 #include <mediademuxer_error.h>
32 #include "iniparser.h"
33 #include <mediademuxer_ini.h>
34 #include <mediademuxer_private.h>
36 /* internal functions, macros here */
37 #ifdef MEDIADEMUXER_DEFAULT_INI
38 static gboolean _generate_default_ini(void);
41 static void _md_ini_check_ini_status(void);
44 #define MEDIADEMUXER_INI_GET_STRING(x_dict, x_item, x_ini, x_default) \
46 gchar* str = iniparser_getstring(x_dict, x_ini, x_default); \
49 (strlen(str) > 0) && \
50 (strlen(str) < MEDIADEMUXER_INI_MAX_STRLEN)) { \
51 strncpy(x_item, str, MEDIADEMUXER_INI_MAX_STRLEN - 1); \
53 strncpy(x_item, x_default, MEDIADEMUXER_INI_MAX_STRLEN - 1); \
57 #define MEDIADEMUXER_INI_GET_COLOR(x_dict, x_item, x_ini, x_default) \
59 gchar* str = iniparser_getstring(x_dict, x_ini, x_default); \
62 (strlen(str) > 0) && \
63 (strlen(str) < MEDIADEMUXER_INI_MAX_STRLEN)) { \
64 x_item = (guint) strtoul(str, NULL, 16); \
66 x_item = (guint) strtoul(x_default, NULL, 16); \
70 /* x_ini is the list of index to set TRUE at x_list[index] */
71 #define MEDIADEMUXER_INI_GET_BOOLEAN_FROM_LIST(x_dict, x_list, x_list_max, x_ini, x_default) \
74 const char *delimiters = " ,"; \
75 char *usr_ptr = NULL; \
77 gchar temp_arr[MEDIADEMUXER_INI_MAX_STRLEN] = {0}; \
78 MMMEDIADEMUXER_INI_GET_STRING(x_dict, temp_arr, x_ini, x_default); \
79 token = strtok_r(temp_arr, delimiters, &usr_ptr); \
81 index = atoi(token); \
82 if (index < 0 || index > x_list_max -1) { \
83 MD_W("%d is not valid index\n", index); \
85 x_list[index] = TRUE; \
87 token = strtok_r(NULL, delimiters, &usr_ptr); \
91 /* x_ini is the list of value to be set at x_list[index] */
92 #define MEDIADEMUXER_INI_GET_INT_FROM_LIST(x_dict, x_list, x_list_max, x_ini, x_default) \
96 const char *delimiters = " ,"; \
97 char *usr_ptr = NULL; \
99 gchar temp_arr[MEDIADEMUXER_INI_MAX_STRLEN] = {0}; \
100 MMMEDIADEMUXER_INI_GET_STRING(x_dict, temp_arr, x_ini, x_default); \
101 token = strtok_r(temp_arr, delimiters, &usr_ptr); \
103 if (index > x_list_max -1) { \
104 MD_E("%d is not valid index\n", index); \
107 value = atoi(token); \
108 x_list[index] = value; \
111 token = strtok_r(NULL, delimiters, &usr_ptr); \
115 int md_ini_load(md_ini_t *ini)
117 dictionary *dict = NULL;
118 _md_ini_check_ini_status();
120 /* first, try to load existing ini file */
121 dict = iniparser_load(MEDIADEMUXER_INI_DEFAULT_PATH);
123 /* if no file exists. create one with set of default values */
125 #ifdef MEDIADEMUXER_DEFAULT_INI
126 MD_L("No inifile found. demuxer will create default inifile.\n");
127 if (FALSE == _generate_default_ini()) {
128 MD_W("Creating default inifile failed. MediaDemuxer will use default values.\n");
130 /* load default ini */
131 dict = iniparser_load(MEDIADEMUXER_INI_DEFAULT_PATH);
134 MD_L("No ini file found. \n");
135 return MD_ERROR_FILE_NOT_FOUND;
140 memset(ini, 0, sizeof(md_ini_t));
142 if (dict) { /* if dict is available */
144 MEDIADEMUXER_INI_GET_STRING(dict, ini->port_name, "port_in_use:mediademuxer_port", DEFAULT_PORT);
145 } else { /* if dict is not available just fill the structure with default value */
147 MD_W("failed to load ini. using hardcoded default\n");
148 strncpy(ini->port_name, DEFAULT_PORT, MEDIADEMUXER_INI_MAX_STRLEN - 1);
151 if (0 == strcmp(ini->port_name, "GST_PORT"))
152 ini->port_type = GST_PORT;
153 else if (0 == strcmp(ini->port_name, "FFMPEG_PORT"))
154 ini->port_type = FFMPEG_PORT;
155 else if (0 == strcmp(ini->port_name, "CUSTOM_PORT"))
156 ini->port_type = CUSTOM_PORT;
158 MD_E("Invalid port is set to [%s] [%d]\n", ini->port_name,
162 MD_L("The port is set to [%s] [%d]\n", ini->port_name, ini->port_type);
164 /* free dict as we got our own structure */
165 iniparser_freedict(dict);
168 MD_L("demuxer settings -----------------------------------\n");
171 MD_L("port_name: %s\n", ini->port_name);
172 MD_L("port_type : %d\n", ini->port_type);
174 return MD_ERROR_NONE;
176 return MD_COURRPTED_INI;
180 static void _md_ini_check_ini_status(void)
182 struct stat ini_buff;
184 if (g_stat(MEDIADEMUXER_INI_DEFAULT_PATH, &ini_buff) < 0) {
185 MD_W("failed to get demuxer ini status\n");
187 if (ini_buff.st_size < 5) {
188 MD_W("demuxer.ini file size=%d, Corrupted! So, Removed\n", (int)ini_buff.st_size);
190 if (g_remove(MEDIADEMUXER_INI_DEFAULT_PATH) == -1)
191 MD_E("failed to delete corrupted ini");
196 #ifdef MEDIADEMUXER_DEFAULT_INI
197 static gboolean _generate_default_ini(void)
200 gchar *default_ini = MEDIADEMUXER_DEFAULT_INI;
202 /* create new file */
203 fp = fopen(MEDIADEMUXER_INI_DEFAULT_PATH, "wt");
208 /* writing default ini file */
209 if (strlen(default_ini) !=
210 fwrite(default_ini, 1, strlen(default_ini), fp)) {
220 #endif /* #ifdef _MEDIADEMUXER_INI_C_ */