4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: JongHyuk Choi <jhchoi.choi@samsung.com>, YeJin Cho <cho.yejin@samsung.com>,
7 * Seungbae Shin <seungbae.shin@samsung.com>, YoungHwan An <younghwan_.an@samsung.com>
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
31 #include "mm_player_utils.h"
33 bool util_exist_file_path(const char *file_path)
37 if (!file_path || !strlen(file_path))
40 int res = access(file_path, R_OK);
47 bool util_write_file_backup(const char *backup_path, char *data_ptr, int data_size)
54 fp = fopen(backup_path, "wb");
58 wsize = fwrite(data_ptr, sizeof(char), data_size, fp);
62 if (wsize != data_size) {
63 if (!access(backup_path, R_OK))
66 debug_error("No space to write!\n");
74 bool util_remove_file_backup(const char *backup_path)
78 if (!backup_path || !strlen(backup_path))
86 #define DETECTION_PREFIX_SIZE 20
87 //bool util_is_midi_type_by_mem(void *mem, int size)
88 int util_is_midi_type_by_mem(void *mem, int size)
92 const char *p = (const char *)mem;
94 if (size < DETECTION_PREFIX_SIZE)
95 return MM_AUDIO_CODEC_INVALID;
97 /* mmf file detection */
98 if (p[0] == 'M' && p[1] == 'M' && p[2] == 'M' && p[3] == 'D') {
99 debug_log("MM_AUDIO_CODEC_MMF\n");
100 return MM_AUDIO_CODEC_MMF;
103 /* midi file detection */
104 if (p[0] == 'M' && p[1] == 'T' && p[2] == 'h' && p[3] == 'd') {
105 debug_log ("MM_AUDIO_CODEC_MIDI, %d\n", MM_AUDIO_CODEC_MIDI);
106 return MM_AUDIO_CODEC_MIDI;
108 /* mxmf file detection */
109 if (p[0] == 'X' && p[1] == 'M' && p[2] == 'F' && p[3] == '_') {
110 debug_log ("MM_AUDIO_CODEC_MXMF\n");
111 return MM_AUDIO_CODEC_MXMF;
114 /* wave file detection */
115 if (p[0] == 'R' && p[1] == 'I' && p[2] == 'F' && p[3] == 'F' &&
116 p[8] == 'W' && p[9] == 'A' && p[10] == 'V' && p[11] == 'E' &&
117 p[12] == 'f' && p[13] == 'm' && p[14] == 't') {
118 debug_log ("MM_AUDIO_CODEC_WAVE\n");
119 return MM_AUDIO_CODEC_WAVE;
121 /* i-melody file detection */
122 if (memcmp(p, "BEGIN:IMELODY", 13) == 0)
124 debug_log ("MM_AUDIO_CODEC_IMELODY\n");
125 return MM_AUDIO_CODEC_IMELODY;
128 return MM_AUDIO_CODEC_INVALID;
131 int util_is_midi_type_by_file(const char *file_path)
135 struct stat file_attrib;
137 char prefix[DETECTION_PREFIX_SIZE] = {0,};
143 fp = fopen(file_path, "r");
148 memset(&file_attrib, 0, sizeof(file_attrib));
150 if (stat(file_path, &file_attrib) != 0)
156 size = (int) file_attrib.st_size;
158 if (size < DETECTION_PREFIX_SIZE)
164 size = fread(prefix, sizeof(char), DETECTION_PREFIX_SIZE, fp);
168 return util_is_midi_type_by_mem(prefix, size);
171 /* messages are treated as warnings bcz those code should not be checked in.
172 * and no error handling will supported for same manner.
175 __util_gst_pad_probe(GstPad *pad, GstBuffer *buffer, gpointer u_data)
177 gint flag = (gint) u_data;
178 GstElement* parent = NULL;
181 /* show name as default */
182 parent = (GstElement*)gst_object_get_parent(GST_OBJECT(pad));
183 debug_warning("PAD PROBE : %s:%s\n", GST_ELEMENT_NAME(parent), GST_PAD_NAME(pad));
185 /* show time stamp */
186 if ( flag & MM_PROBE_TIMESTAMP )
188 debug_warning("ts : %u:%02u:%02u.%09u\n", GST_TIME_ARGS(GST_BUFFER_TIMESTAMP(buffer)));
191 /* show buffer size */
192 if ( flag & MM_PROBE_BUFFERSIZE )
194 debug_warning("buffer size : %ud\n", GST_BUFFER_SIZE(buffer));
197 /* show buffer duration */
198 if ( flag & MM_PROBE_BUFFER_DURATION )
200 debug_warning("dur : %lld\n", GST_BUFFER_DURATION(buffer));
203 /* show buffer caps */
204 if ( flag & MM_PROBE_CAPS )
206 debug_warning("caps : %s\n", gst_caps_to_string(GST_BUFFER_CAPS(buffer)));
209 /* drop buffer if flag is on */
210 if ( flag & MM_PROBE_DROP_BUFFER )
212 debug_warning("dropping\n");
216 /* show clock time */
217 if ( flag & MM_PROBE_CLOCK_TIME )
219 GstClock* clock = NULL;
220 GstClockTime now = GST_CLOCK_TIME_NONE;
222 clock = GST_ELEMENT_CLOCK ( parent );
226 now = gst_clock_get_time( clock );
227 debug_warning("clock time : %" GST_TIME_FORMAT "\n", GST_TIME_ARGS( now ));
232 gst_object_unref(parent);
238 util_get_cookie_list ( const char *cookies )
240 char **cookie_list = NULL;
244 if ( !cookies || !strlen(cookies) )
247 debug_log("cookies : %d[bytes] - %s \n", strlen(cookies), cookies);
249 temp = g_strdup(cookies);
251 /* trimming. it works inplace */
255 cookie_list = g_strsplit(temp, ";", 100);
257 for ( i = 0; i < g_strv_length(cookie_list); i++ )
259 if ( cookie_list[i] && strlen(cookie_list[i]) )
261 g_strstrip(cookie_list[i]);
262 debug_log("cookie_list[%d] : %d[bytes] - %s \n", i, strlen(cookie_list[i]), cookie_list[i]);
266 cookie_list[i][0]='\0';
277 bool util_check_valid_url ( const char *proxy )
279 struct in_addr proxy_addr;
282 return_val_if_fail ( proxy, FALSE );
284 if ( inet_aton(proxy, &proxy_addr) != 0 )
286 debug_warning("invalid proxy is set. \n");
293 /* check the given path is indicating sdp file */
295 util_is_sdp_file ( const char *path )
297 gboolean ret = FALSE;
302 return_val_if_fail ( path, FALSE );
304 uri = g_ascii_strdown ( path, -1 );
309 /* strlen(".sdp") == 4 */
310 if ( strlen( uri ) <= 4 )
312 debug_warning ( "path is too short.\n" );
316 /* first, check extension name */
317 ret = g_str_has_suffix ( uri, "sdp" );
319 /* second, if no suffix is there, check it's contents */
322 /* FIXIT : do it soon */
323 debug_warning("determining whether it's sdp or not with it's content is not implemented yet. ;)\n");
334 util_get_time ( void )
337 gettimeofday(&tv,NULL);
338 return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
342 util_get_rank_increase ( const char *factory_class )
344 gint rank_pri_inc = 20;
345 gint rank_sec_inc = 10;
348 if ( g_strrstr(factory_class,"Dsp") )
350 else if ( g_strrstr(factory_class,"HW") )
352 else if ( g_strrstr(factory_class,"Arm") )
359 util_factory_rank_compare(GstPluginFeature *f1, GstPluginFeature *f2) // @
362 int f1_rank_inc=0, f2_rank_inc=0;
364 klass = gst_element_factory_get_klass(GST_ELEMENT_FACTORY(f1));
365 f1_rank_inc = util_get_rank_increase ( klass );
367 klass = gst_element_factory_get_klass(GST_ELEMENT_FACTORY(f2));
368 f2_rank_inc = util_get_rank_increase ( klass );
370 return (gst_plugin_feature_get_rank(f2)+f2_rank_inc) - (gst_plugin_feature_get_rank(f1)+f1_rank_inc );