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 #if 1 //tskim:MidiModuleRequires:+: for Midi player
76 bool util_remove_file_backup(const char *backup_path)
80 if (!backup_path || !strlen(backup_path))
84 Prevent defect patch. CID:22389 Checker:TOTCU
85 int res = access(backup_path, R_OK);
94 #define DETECTION_PREFIX_SIZE 20
95 //bool util_is_midi_type_by_mem(void *mem, int size)
96 int util_is_midi_type_by_mem(void *mem, int size)
100 const char *p = (const char *)mem;
102 if (size < DETECTION_PREFIX_SIZE)
103 return MM_AUDIO_CODEC_INVALID; //FALSE; // sbs:+:080903
105 /* mmf file detection */
106 if (p[0] == 'M' && p[1] == 'M' && p[2] == 'M' && p[3] == 'D') {
107 debug_log("MM_AUDIO_CODEC_MMF\n");
108 return MM_AUDIO_CODEC_MMF; // TRUE;// sbs:+:080903
111 /* midi file detection */
112 if (p[0] == 'M' && p[1] == 'T' && p[2] == 'h' && p[3] == 'd') {
113 debug_log ("MM_AUDIO_CODEC_MIDI, %d\n", MM_AUDIO_CODEC_MIDI);
114 return MM_AUDIO_CODEC_MIDI;//TRUE;// sbs:+:080903
116 /* mxmf file detection */ // sbs:+:080903
117 if (p[0] == 'X' && p[1] == 'M' && p[2] == 'F' && p[3] == '_') {
118 debug_log ("MM_AUDIO_CODEC_MXMF\n");
119 return MM_AUDIO_CODEC_MXMF;
122 /* wave file detection */
123 if (p[0] == 'R' && p[1] == 'I' && p[2] == 'F' && p[3] == 'F' &&
124 p[8] == 'W' && p[9] == 'A' && p[10] == 'V' && p[11] == 'E' &&
125 p[12] == 'f' && p[13] == 'm' && p[14] == 't') {
126 debug_log ("MM_AUDIO_CODEC_WAVE\n");
127 return MM_AUDIO_CODEC_WAVE;//TRUE;// sbs:+:080903
129 /* i-melody file detection */ // sbs:+:080903
130 if (memcmp(p, "BEGIN:IMELODY", 13) == 0)
132 debug_log ("MM_AUDIO_CODEC_IMELODY\n");
133 return MM_AUDIO_CODEC_IMELODY;
136 return MM_AUDIO_CODEC_INVALID;//FALSE; // sbs:+:080903
139 //bool util_is_midi_type_by_file(const char *file_path)
140 int util_is_midi_type_by_file(const char *file_path)
144 struct stat file_attrib;
146 char prefix[DETECTION_PREFIX_SIZE] = {0,};
152 /* Prevent defect patch. CID: 22388 Checker: TOCTOU */
153 fp = fopen(file_path, "r");
158 memset(&file_attrib, 0, sizeof(file_attrib));
160 if (stat(file_path, &file_attrib) != 0)
166 size = (int) file_attrib.st_size;
168 if (size < DETECTION_PREFIX_SIZE)
174 size = fread(prefix, sizeof(char), DETECTION_PREFIX_SIZE, fp);
178 return util_is_midi_type_by_mem(prefix, size);
181 /* messages are treated as warnings bcz those code should not be checked in.
182 * and no error handling will supported for same manner.
185 __util_gst_pad_probe(GstPad *pad, GstBuffer *buffer, gpointer u_data)
187 gint flag = (gint) u_data;
188 GstElement* parent = NULL;
191 /* show name as default */
192 parent = (GstElement*)gst_object_get_parent(GST_OBJECT(pad));
193 debug_warning("PAD PROBE : %s:%s\n", GST_ELEMENT_NAME(parent), GST_PAD_NAME(pad));
195 /* show time stamp */
196 if ( flag & MM_PROBE_TIMESTAMP )
198 debug_warning("ts : %u:%02u:%02u.%09u\n", GST_TIME_ARGS(GST_BUFFER_TIMESTAMP(buffer)));
201 /* show buffer size */
202 if ( flag & MM_PROBE_BUFFERSIZE )
204 debug_warning("buffer size : %ud\n", GST_BUFFER_SIZE(buffer));
207 /* show buffer duration */
208 if ( flag & MM_PROBE_BUFFER_DURATION )
210 debug_warning("dur : %lld\n", GST_BUFFER_DURATION(buffer));
213 /* show buffer caps */
214 if ( flag & MM_PROBE_CAPS )
216 debug_warning("caps : %s\n", gst_caps_to_string(GST_BUFFER_CAPS(buffer)));
219 /* drop buffer if flag is on */
220 if ( flag & MM_PROBE_DROP_BUFFER )
222 debug_warning("dropping\n");
226 /* show clock time */
227 if ( flag & MM_PROBE_CLOCK_TIME )
229 GstClock* clock = NULL;
230 GstClockTime now = GST_CLOCK_TIME_NONE;
232 clock = GST_ELEMENT_CLOCK ( parent );
236 now = gst_clock_get_time( clock );
237 debug_warning("clock time : %" GST_TIME_FORMAT "\n", GST_TIME_ARGS( now ));
242 gst_object_unref(parent);
248 util_get_cookie_list ( const char *cookies )
250 char **cookie_list = NULL;
254 if ( !cookies || !strlen(cookies) )
257 debug_log("cookies : %d[bytes] - %s \n", strlen(cookies), cookies);
259 temp = g_strdup(cookies);
261 /* trimming. it works inplace */
265 cookie_list = g_strsplit(temp, ";", 100);
267 for ( i = 0; i < g_strv_length(cookie_list); i++ )
269 if ( cookie_list[i] && strlen(cookie_list[i]) )
271 g_strstrip(cookie_list[i]);
272 debug_log("cookie_list[%d] : %d[bytes] - %s \n", i, strlen(cookie_list[i]), cookie_list[i]);
276 cookie_list[i][0]='\0';
287 bool util_check_valid_url ( const char *proxy )
289 struct in_addr proxy_addr;
292 return_val_if_fail ( proxy, FALSE );
293 return_val_if_fail ( strlen(proxy), FALSE );
295 if ( inet_aton(proxy, &proxy_addr) != 0 )
297 debug_warning("invalid proxy is set. \n");
304 /* check the given path is indicating sdp file */
306 util_is_sdp_file ( const char *path )
308 gboolean ret = FALSE;
313 return_val_if_fail ( path, FALSE );
315 uri = g_ascii_strdown ( path, -1 );
320 /* strlen(".sdp") == 4 */
321 if ( strlen( uri ) <= 4 )
323 debug_warning ( "path is too short.\n" );
327 /* first, check extension name */
328 ret = g_str_has_suffix ( uri, "sdp" );
330 /* second, if no suffix is there, check it's contents */
333 /* FIXIT : do it soon */
334 debug_warning("determining whether it's sdp or not with it's content is not implemented yet. ;)\n");
345 util_get_time ( void )
348 gettimeofday(&tv,NULL);
349 return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
353 util_get_rank_increase ( const char *factory_class )
355 gint rank_pri_inc = 20;
356 gint rank_sec_inc = 10;
359 if ( g_strrstr(factory_class,"Dsp") )
361 else if ( g_strrstr(factory_class,"HW") )
363 else if ( g_strrstr(factory_class,"Arm") )
370 util_factory_rank_compare(GstPluginFeature *f1, GstPluginFeature *f2) // @
373 int f1_rank_inc=0, f2_rank_inc=0;
375 klass = gst_element_factory_get_klass(GST_ELEMENT_FACTORY(f1));
376 f1_rank_inc = util_get_rank_increase ( klass );
378 klass = gst_element_factory_get_klass(GST_ELEMENT_FACTORY(f2));
379 f2_rank_inc = util_get_rank_increase ( klass );
381 return (gst_plugin_feature_get_rank(f2)+f2_rank_inc) - (gst_plugin_feature_get_rank(f1)+f1_rank_inc );
385 util_get_charset(const char *file_path)
387 UCharsetDetector* ucsd;
388 const UCharsetMatch* ucm;
389 UErrorCode status = U_ZERO_ERROR;
391 const char* charset = NULL;
395 fin = fopen(file_path, "r");
398 debug_error("fail to open file %s\n", file_path);
402 ucsd = ucsdet_open( &status );
403 if( U_FAILURE(status) ) {
404 debug_error("fail to ucsdet_open\n");
408 ucsdet_enableInputFilter( ucsd, TRUE );
410 buf = g_malloc(1024*1024);
413 debug_error("fail to alloc\n");
417 fread( buf, 1, 1024*1024, fin );
420 ucsdet_setText( ucsd, buf, strlen(buf), &status );
421 if( U_FAILURE(status) ) {
422 debug_error("fail to ucsdet_setText\n");
426 ucm = ucsdet_detect( ucsd, &status );
427 if( U_FAILURE(status) ) {
428 debug_error("fail to ucsdet_detect\n");
432 charset = ucsdet_getName( ucm, &status );
433 if( U_FAILURE(status) ) {
434 debug_error("fail to ucsdet_getName\n");
439 ucsdet_close( ucsd );