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);
85 if (remove(backup_path) == -1)
92 #define DETECTION_PREFIX_SIZE 20
93 //bool util_is_midi_type_by_mem(void *mem, int size)
94 int util_is_midi_type_by_mem(void *mem, int size)
98 const char *p = (const char *)mem;
100 if (size < DETECTION_PREFIX_SIZE)
101 return MM_AUDIO_CODEC_INVALID;
103 /* mmf file detection */
104 if (p[0] == 'M' && p[1] == 'M' && p[2] == 'M' && p[3] == 'D') {
105 debug_log("MM_AUDIO_CODEC_MMF\n");
106 return MM_AUDIO_CODEC_MMF;
109 /* midi file detection */
110 if (p[0] == 'M' && p[1] == 'T' && p[2] == 'h' && p[3] == 'd') {
111 debug_log ("MM_AUDIO_CODEC_MIDI, %d\n", MM_AUDIO_CODEC_MIDI);
112 return MM_AUDIO_CODEC_MIDI;
114 /* mxmf file detection */
115 if (p[0] == 'X' && p[1] == 'M' && p[2] == 'F' && p[3] == '_') {
116 debug_log ("MM_AUDIO_CODEC_MXMF\n");
117 return MM_AUDIO_CODEC_MXMF;
120 /* wave file detection */
121 if (p[0] == 'R' && p[1] == 'I' && p[2] == 'F' && p[3] == 'F' &&
122 p[8] == 'W' && p[9] == 'A' && p[10] == 'V' && p[11] == 'E' &&
123 p[12] == 'f' && p[13] == 'm' && p[14] == 't') {
124 debug_log ("MM_AUDIO_CODEC_WAVE\n");
125 return MM_AUDIO_CODEC_WAVE;
127 /* i-melody file detection */
128 if (memcmp(p, "BEGIN:IMELODY", 13) == 0)
130 debug_log ("MM_AUDIO_CODEC_IMELODY\n");
131 return MM_AUDIO_CODEC_IMELODY;
134 return MM_AUDIO_CODEC_INVALID;
137 //bool util_is_midi_type_by_file(const char *file_path)
138 int util_is_midi_type_by_file(const char *file_path)
142 struct stat file_attrib;
144 char prefix[DETECTION_PREFIX_SIZE] = {0,};
150 fp = fopen(file_path, "r");
155 memset(&file_attrib, 0, sizeof(file_attrib));
157 if (stat(file_path, &file_attrib) != 0)
163 size = (int) file_attrib.st_size;
165 if (size < DETECTION_PREFIX_SIZE)
171 size = fread(prefix, sizeof(char), DETECTION_PREFIX_SIZE, fp);
175 return util_is_midi_type_by_mem(prefix, size);
178 /* messages are treated as warnings bcz those code should not be checked in.
179 * and no error handling will supported for same manner.
182 __util_gst_pad_probe(GstPad *pad, GstBuffer *buffer, gpointer u_data)
184 gint flag = (gint) u_data;
185 GstElement* parent = NULL;
188 /* show name as default */
189 parent = (GstElement*)gst_object_get_parent(GST_OBJECT(pad));
190 debug_warning("PAD PROBE : %s:%s\n", GST_ELEMENT_NAME(parent), GST_PAD_NAME(pad));
192 /* show time stamp */
193 if ( flag & MM_PROBE_TIMESTAMP )
195 debug_warning("ts : %u:%02u:%02u.%09u\n", GST_TIME_ARGS(GST_BUFFER_TIMESTAMP(buffer)));
198 /* show buffer size */
199 if ( flag & MM_PROBE_BUFFERSIZE )
201 debug_warning("buffer size : %ud\n", GST_BUFFER_SIZE(buffer));
204 /* show buffer duration */
205 if ( flag & MM_PROBE_BUFFER_DURATION )
207 debug_warning("dur : %lld\n", GST_BUFFER_DURATION(buffer));
210 /* show buffer caps */
211 if ( flag & MM_PROBE_CAPS )
213 debug_warning("caps : %s\n", gst_caps_to_string(GST_BUFFER_CAPS(buffer)));
216 /* drop buffer if flag is on */
217 if ( flag & MM_PROBE_DROP_BUFFER )
219 debug_warning("dropping\n");
223 /* show clock time */
224 if ( flag & MM_PROBE_CLOCK_TIME )
226 GstClock* clock = NULL;
227 GstClockTime now = GST_CLOCK_TIME_NONE;
229 clock = GST_ELEMENT_CLOCK ( parent );
233 now = gst_clock_get_time( clock );
234 debug_warning("clock time : %" GST_TIME_FORMAT "\n", GST_TIME_ARGS( now ));
239 gst_object_unref(parent);
245 util_get_cookie_list ( const char *cookies )
247 char **cookie_list = NULL;
251 if ( !cookies || !strlen(cookies) )
254 debug_log("cookies : %d[bytes] - %s \n", strlen(cookies), cookies);
256 temp = g_strdup(cookies);
258 /* trimming. it works inplace */
262 cookie_list = g_strsplit(temp, ";", 100);
264 for ( i = 0; i < g_strv_length(cookie_list); i++ )
266 if ( cookie_list[i] && strlen(cookie_list[i]) )
268 g_strstrip(cookie_list[i]);
269 debug_log("cookie_list[%d] : %d[bytes] - %s \n", i, strlen(cookie_list[i]), cookie_list[i]);
273 cookie_list[i][0]='\0';
284 bool util_check_valid_url ( const char *proxy )
286 struct in_addr proxy_addr;
289 return_val_if_fail ( proxy, FALSE );
290 return_val_if_fail ( strlen(proxy), FALSE );
292 if ( inet_aton(proxy, &proxy_addr) != 0 )
294 debug_warning("invalid proxy is set. \n");
301 /* check the given path is indicating sdp file */
303 util_is_sdp_file ( const char *path )
305 gboolean ret = FALSE;
310 return_val_if_fail ( path, FALSE );
312 uri = g_ascii_strdown ( path, -1 );
322 /* strlen(".sdp") == 4 */
323 if ( strlen( uri ) <= 4 )
325 debug_warning ( "path is too short.\n" );
329 /* first, check extension name */
330 ret = g_str_has_suffix ( uri, "sdp" );
332 /* second, if no suffix is there, check it's contents */
335 /* FIXIT : do it soon */
336 debug_warning("determining whether it's sdp or not with it's content is not implemented yet. ;)\n");
346 util_get_time ( void )
349 gettimeofday(&tv,NULL);
350 return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
354 util_get_rank_increase ( const char *factory_class )
356 gint rank_pri_inc = 20;
357 gint rank_sec_inc = 10;
360 if ( g_strrstr(factory_class,"Dsp") )
362 else if ( g_strrstr(factory_class,"HW") )
364 else if ( g_strrstr(factory_class,"Arm") )
371 util_factory_rank_compare(GstPluginFeature *f1, GstPluginFeature *f2) // @
374 int f1_rank_inc=0, f2_rank_inc=0;
376 klass = gst_element_factory_get_klass(GST_ELEMENT_FACTORY(f1));
377 f1_rank_inc = util_get_rank_increase ( klass );
379 klass = gst_element_factory_get_klass(GST_ELEMENT_FACTORY(f2));
380 f2_rank_inc = util_get_rank_increase ( klass );
382 return (gst_plugin_feature_get_rank(f2)+f2_rank_inc) - (gst_plugin_feature_get_rank(f1)+f1_rank_inc );
386 util_get_charset(const char *file_path)
388 UCharsetDetector* ucsd;
389 const UCharsetMatch* ucm;
390 UErrorCode status = U_ZERO_ERROR;
392 const char* charset = NULL;
396 fin = fopen(file_path, "r");
399 debug_error("fail to open file %s\n", file_path);
403 ucsd = ucsdet_open( &status );
404 if( U_FAILURE(status) ) {
405 debug_error("fail to ucsdet_open\n");
409 ucsdet_enableInputFilter( ucsd, TRUE );
411 buf = g_malloc(1024*1024);
414 debug_error("fail to alloc\n");
418 fread( buf, 1, 1024*1024, fin );
421 ucsdet_setText( ucsd, buf, strlen(buf), &status );
422 if( U_FAILURE(status) ) {
423 debug_error("fail to ucsdet_setText\n");
427 ucm = ucsdet_detect( ucsd, &status );
428 if( U_FAILURE(status) ) {
429 debug_error("fail to ucsdet_detect\n");
433 charset = ucsdet_getName( ucm, &status );
434 if( U_FAILURE(status) ) {
435 debug_error("fail to ucsdet_getName\n");
440 ucsdet_close( ucsd );