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>, YoungHwan An <younghwan_.an@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
29 #include <unicode/ucsdet.h>
32 #include "mm_player_utils.h"
34 bool util_exist_file_path(const char *file_path)
38 if (!file_path || !strlen(file_path))
41 int res = access(file_path, R_OK);
48 bool util_write_file_backup(const char *backup_path, char *data_ptr, int data_size)
55 fp = fopen(backup_path, "wb");
59 wsize = fwrite(data_ptr, sizeof(char), data_size, fp);
63 if (wsize != data_size) {
64 if (!access(backup_path, R_OK))
67 debug_error("No space to write!\n");
75 bool util_remove_file_backup(const char *backup_path)
79 if (!backup_path || !strlen(backup_path))
82 int res = access(backup_path, R_OK);
89 #define DETECTION_PREFIX_SIZE 20
90 //bool util_is_midi_type_by_mem(void *mem, int size)
91 int util_is_midi_type_by_mem(void *mem, int size)
95 const char *p = (const char *)mem;
97 if (size < DETECTION_PREFIX_SIZE)
98 return MM_AUDIO_CODEC_INVALID;
100 /* mmf file detection */
101 if (p[0] == 'M' && p[1] == 'M' && p[2] == 'M' && p[3] == 'D') {
102 debug_log("MM_AUDIO_CODEC_MMF\n");
103 return MM_AUDIO_CODEC_MMF;
106 /* midi file detection */
107 if (p[0] == 'M' && p[1] == 'T' && p[2] == 'h' && p[3] == 'd') {
108 debug_log ("MM_AUDIO_CODEC_MIDI, %d\n", MM_AUDIO_CODEC_MIDI);
109 return MM_AUDIO_CODEC_MIDI;
111 /* mxmf file detection */
112 if (p[0] == 'X' && p[1] == 'M' && p[2] == 'F' && p[3] == '_') {
113 debug_log ("MM_AUDIO_CODEC_MXMF\n");
114 return MM_AUDIO_CODEC_MXMF;
117 /* wave file detection */
118 if (p[0] == 'R' && p[1] == 'I' && p[2] == 'F' && p[3] == 'F' &&
119 p[8] == 'W' && p[9] == 'A' && p[10] == 'V' && p[11] == 'E' &&
120 p[12] == 'f' && p[13] == 'm' && p[14] == 't') {
121 debug_log ("MM_AUDIO_CODEC_WAVE\n");
122 return MM_AUDIO_CODEC_WAVE;
124 /* i-melody file detection */
125 if (memcmp(p, "BEGIN:IMELODY", 13) == 0)
127 debug_log ("MM_AUDIO_CODEC_IMELODY\n");
128 return MM_AUDIO_CODEC_IMELODY;
131 return MM_AUDIO_CODEC_INVALID;
134 //bool util_is_midi_type_by_file(const char *file_path)
135 int util_is_midi_type_by_file(const char *file_path)
139 struct stat file_attrib;
141 char prefix[DETECTION_PREFIX_SIZE] = {0,};
147 fp = fopen(file_path, "r");
152 memset(&file_attrib, 0, sizeof(file_attrib));
154 if (stat(file_path, &file_attrib) != 0)
160 size = (int) file_attrib.st_size;
162 if (size < DETECTION_PREFIX_SIZE)
168 size = fread(prefix, sizeof(char), DETECTION_PREFIX_SIZE, fp);
172 return util_is_midi_type_by_mem(prefix, size);
175 /* messages are treated as warnings bcz those code should not be checked in.
176 * and no error handling will supported for same manner.
179 __util_gst_pad_probe(GstPad *pad, GstBuffer *buffer, gpointer u_data)
181 gint flag = (gint) u_data;
182 GstElement* parent = NULL;
185 /* show name as default */
186 parent = (GstElement*)gst_object_get_parent(GST_OBJECT(pad));
187 debug_warning("PAD PROBE : %s:%s\n", GST_ELEMENT_NAME(parent), GST_PAD_NAME(pad));
189 /* show time stamp */
190 if ( flag & MM_PROBE_TIMESTAMP )
192 debug_warning("ts : %u:%02u:%02u.%09u\n", GST_TIME_ARGS(GST_BUFFER_TIMESTAMP(buffer)));
195 /* show buffer size */
196 if ( flag & MM_PROBE_BUFFERSIZE )
198 debug_warning("buffer size : %ud\n", GST_BUFFER_SIZE(buffer));
201 /* show buffer duration */
202 if ( flag & MM_PROBE_BUFFER_DURATION )
204 debug_warning("dur : %lld\n", GST_BUFFER_DURATION(buffer));
207 /* show buffer caps */
208 if ( flag & MM_PROBE_CAPS )
210 debug_warning("caps : %s\n", gst_caps_to_string(GST_BUFFER_CAPS(buffer)));
213 /* drop buffer if flag is on */
214 if ( flag & MM_PROBE_DROP_BUFFER )
216 debug_warning("dropping\n");
220 /* show clock time */
221 if ( flag & MM_PROBE_CLOCK_TIME )
223 GstClock* clock = NULL;
224 GstClockTime now = GST_CLOCK_TIME_NONE;
226 clock = GST_ELEMENT_CLOCK ( parent );
230 now = gst_clock_get_time( clock );
231 debug_warning("clock time : %" GST_TIME_FORMAT "\n", GST_TIME_ARGS( now ));
236 gst_object_unref(parent);
242 util_get_cookie_list ( const char *cookies )
244 char **cookie_list = NULL;
248 if ( !cookies || !strlen(cookies) )
251 debug_log("cookies : %d[bytes] - %s \n", strlen(cookies), cookies);
253 temp = g_strdup(cookies);
255 /* trimming. it works inplace */
259 cookie_list = g_strsplit(temp, ";", 100);
261 for ( i = 0; i < g_strv_length(cookie_list); i++ )
263 if ( cookie_list[i] && strlen(cookie_list[i]) )
265 g_strstrip(cookie_list[i]);
266 debug_log("cookie_list[%d] : %d[bytes] - %s \n", i, strlen(cookie_list[i]), cookie_list[i]);
270 cookie_list[i][0]='\0';
281 bool util_check_valid_url ( const char *proxy )
283 struct in_addr proxy_addr;
286 return_val_if_fail ( proxy, FALSE );
287 return_val_if_fail ( strlen(proxy), FALSE );
289 if ( inet_aton(proxy, &proxy_addr) != 0 )
291 debug_warning("invalid proxy is set. \n");
298 /* check the given path is indicating sdp file */
300 util_is_sdp_file ( const char *path )
302 gboolean ret = FALSE;
307 return_val_if_fail ( path, FALSE );
309 uri = g_ascii_strdown ( path, -1 );
319 /* strlen(".sdp") == 4 */
320 if ( strlen( uri ) <= 4 )
322 debug_warning ( "path is too short.\n" );
326 /* first, check extension name */
327 ret = g_str_has_suffix ( uri, "sdp" );
329 /* second, if no suffix is there, check it's contents */
332 /* FIXIT : do it soon */
333 debug_warning("determining whether it's sdp or not with it's content is not implemented yet. ;)\n");
343 util_get_time ( void )
346 gettimeofday(&tv,NULL);
347 return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
351 util_get_rank_increase ( const char *factory_class )
353 gint rank_pri_inc = 20;
354 gint rank_sec_inc = 10;
357 if ( g_strrstr(factory_class,"Dsp") )
359 else if ( g_strrstr(factory_class,"HW") )
361 else if ( g_strrstr(factory_class,"Arm") )
368 util_factory_rank_compare(GstPluginFeature *f1, GstPluginFeature *f2) // @
371 int f1_rank_inc=0, f2_rank_inc=0;
373 klass = gst_element_factory_get_klass(GST_ELEMENT_FACTORY(f1));
374 f1_rank_inc = util_get_rank_increase ( klass );
376 klass = gst_element_factory_get_klass(GST_ELEMENT_FACTORY(f2));
377 f2_rank_inc = util_get_rank_increase ( klass );
379 return (gst_plugin_feature_get_rank(f2)+f2_rank_inc) - (gst_plugin_feature_get_rank(f1)+f1_rank_inc );
383 util_get_charset(const char *file_path)
385 UCharsetDetector* ucsd;
386 const UCharsetMatch* ucm;
387 UErrorCode status = U_ZERO_ERROR;
389 const char* charset = NULL;
393 fin = fopen(file_path, "r");
396 debug_error("fail to open file %s\n", file_path);
400 ucsd = ucsdet_open( &status );
401 if( U_FAILURE(status) ) {
402 debug_error("fail to ucsdet_open\n");
406 ucsdet_enableInputFilter( ucsd, TRUE );
408 buf = g_malloc(1024*1024);
411 debug_error("fail to alloc\n");
415 fread( buf, 1, 1024*1024, fin );
418 ucsdet_setText( ucsd, buf, strlen(buf), &status );
419 if( U_FAILURE(status) ) {
420 debug_error("fail to ucsdet_setText\n");
424 ucm = ucsdet_detect( ucsd, &status );
425 if( U_FAILURE(status) ) {
426 debug_error("fail to ucsdet_detect\n");
430 charset = ucsdet_getName( ucm, &status );
431 if( U_FAILURE(status) ) {
432 debug_error("fail to ucsdet_getName\n");
437 ucsdet_close( ucsd );