4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: JongHyuk Choi <jhchoi.choi@samsung.com>, Heechul Jeon <heechul.jeon@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.
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <unicode/ucsdet.h>
33 #include "mm_player_utils.h"
35 bool util_exist_file_path(const char *file_path)
38 struct stat stat_results = {0, };
40 if (!file_path || !strlen(file_path))
43 fd = open (file_path, O_RDONLY);
47 debug_error("failed to open %s, %s", file_path, strerror(errno));
51 if (fstat(fd, &stat_results) < 0)
53 debug_error("failed to get file status");
57 debug_warning("file size : %lld bytes", (long long)stat_results.st_size); //need to chech file validity
65 bool util_write_file_backup(const char *backup_path, char *data_ptr, int data_size)
72 fp = fopen(backup_path, "wb");
76 wsize = fwrite(data_ptr, sizeof(char), data_size, fp);
80 if (wsize != data_size) {
81 if (!access(backup_path, R_OK))
84 debug_error("No space to write!\n");
92 bool util_remove_file_backup(const char *backup_path)
96 if (!backup_path || !strlen(backup_path))
99 int res = access(backup_path, R_OK);
102 if (remove(backup_path) == -1)
109 #define DETECTION_PREFIX_SIZE 20
110 //bool util_is_midi_type_by_mem(void *mem, int size)
111 int util_is_midi_type_by_mem(void *mem, int size)
115 const char *p = (const char *)mem;
117 if (size < DETECTION_PREFIX_SIZE)
118 return MM_AUDIO_CODEC_INVALID;
120 /* mmf file detection */
121 if (p[0] == 'M' && p[1] == 'M' && p[2] == 'M' && p[3] == 'D') {
122 debug_log("MM_AUDIO_CODEC_MMF\n");
123 return MM_AUDIO_CODEC_MMF;
126 /* midi file detection */
127 if (p[0] == 'M' && p[1] == 'T' && p[2] == 'h' && p[3] == 'd') {
128 debug_log ("MM_AUDIO_CODEC_MIDI, %d\n", MM_AUDIO_CODEC_MIDI);
129 return MM_AUDIO_CODEC_MIDI;
131 /* mxmf file detection */
132 if (p[0] == 'X' && p[1] == 'M' && p[2] == 'F' && p[3] == '_') {
133 debug_log ("MM_AUDIO_CODEC_MXMF\n");
134 return MM_AUDIO_CODEC_MXMF;
137 /* wave file detection */
138 if (p[0] == 'R' && p[1] == 'I' && p[2] == 'F' && p[3] == 'F' &&
139 p[8] == 'W' && p[9] == 'A' && p[10] == 'V' && p[11] == 'E' &&
140 p[12] == 'f' && p[13] == 'm' && p[14] == 't') {
141 debug_log ("MM_AUDIO_CODEC_WAVE\n");
142 return MM_AUDIO_CODEC_WAVE;
144 /* i-melody file detection */
145 if (memcmp(p, "BEGIN:IMELODY", 13) == 0)
147 debug_log ("MM_AUDIO_CODEC_IMELODY\n");
148 return MM_AUDIO_CODEC_IMELODY;
151 return MM_AUDIO_CODEC_INVALID;
154 //bool util_is_midi_type_by_file(const char *file_path)
155 int util_is_midi_type_by_file(const char *file_path)
159 struct stat file_attrib;
161 char prefix[DETECTION_PREFIX_SIZE] = {0,};
167 fp = fopen(file_path, "r");
172 memset(&file_attrib, 0, sizeof(file_attrib));
174 if (stat(file_path, &file_attrib) != 0)
180 size = (int) file_attrib.st_size;
182 if (size < DETECTION_PREFIX_SIZE)
188 size = fread(prefix, sizeof(char), DETECTION_PREFIX_SIZE, fp);
192 return util_is_midi_type_by_mem(prefix, size);
195 /* messages are treated as warnings bcz those code should not be checked in.
196 * and no error handling will supported for same manner.
199 __util_gst_pad_probe(GstPad *pad, GstBuffer *buffer, gpointer u_data)
201 gint flag = (gint) u_data;
202 GstElement* parent = NULL;
203 GstMapInfo mapinfo = {0, };
206 /* show name as default */
207 parent = (GstElement*)gst_object_get_parent(GST_OBJECT(pad));
208 debug_warning("PAD PROBE : %s:%s\n", GST_ELEMENT_NAME(parent), GST_PAD_NAME(pad));
210 /* show time stamp */
211 if ( flag & MM_PROBE_TIMESTAMP )
213 debug_warning("ts : %u:%02u:%02u.%09u\n", GST_TIME_ARGS(GST_BUFFER_TIMESTAMP(buffer)));
216 /* show buffer size */
217 gst_buffer_map(buffer, &mapinfo, GST_MAP_READ);
218 if ( flag & MM_PROBE_BUFFERSIZE )
220 debug_warning("buffer size : %ud\n", mapinfo.size);
222 gst_buffer_unmap(buffer, &mapinfo);
224 /* show buffer duration */
225 if ( flag & MM_PROBE_BUFFER_DURATION )
227 debug_warning("dur : %lld\n", GST_BUFFER_DURATION(buffer));
230 /* show buffer caps */
231 if ( flag & MM_PROBE_CAPS )
233 debug_warning("caps : %s\n", gst_caps_to_string(gst_pad_get_current_caps(pad)));
236 /* drop buffer if flag is on */
237 if ( flag & MM_PROBE_DROP_BUFFER )
239 debug_warning("dropping\n");
243 /* show clock time */
244 if ( flag & MM_PROBE_CLOCK_TIME )
246 GstClock* clock = NULL;
247 GstClockTime now = GST_CLOCK_TIME_NONE;
249 clock = GST_ELEMENT_CLOCK ( parent );
253 now = gst_clock_get_time( clock );
254 debug_warning("clock time : %" GST_TIME_FORMAT "\n", GST_TIME_ARGS( now ));
259 gst_object_unref(parent);
265 util_get_cookie_list ( const char *cookies )
267 char **cookie_list = NULL;
271 if ( !cookies || !strlen(cookies) )
274 debug_log("cookies : %d[bytes] - %s \n", strlen(cookies), cookies);
276 temp = g_strdup(cookies);
278 /* trimming. it works inplace */
282 cookie_list = g_strsplit(temp, ";", 100);
284 for ( i = 0; i < g_strv_length(cookie_list); i++ )
286 if ( cookie_list[i] && strlen(cookie_list[i]) )
288 g_strstrip(cookie_list[i]);
289 debug_log("cookie_list[%d] : %d[bytes] - %s \n", i, strlen(cookie_list[i]), cookie_list[i]);
293 cookie_list[i][0]='\0';
304 bool util_check_valid_url ( const char *proxy )
306 struct in_addr proxy_addr;
309 return_val_if_fail ( proxy, FALSE );
310 return_val_if_fail ( strlen(proxy), FALSE );
312 if ( inet_aton(proxy, &proxy_addr) != 0 )
314 debug_warning("invalid proxy is set. \n");
321 /* check the given path is indicating sdp file */
323 util_is_sdp_file ( const char *path )
325 gboolean ret = FALSE;
330 return_val_if_fail ( path, FALSE );
332 uri = g_ascii_strdown ( path, -1 );
342 /* strlen(".sdp") == 4 */
343 if ( strlen( uri ) <= 4 )
345 debug_warning ( "path is too short.\n" );
349 /* first, check extension name */
350 ret = g_str_has_suffix ( uri, "sdp" );
352 /* second, if no suffix is there, check it's contents */
355 /* FIXIT : do it soon */
356 debug_log("determining whether it's sdp or not with it's content is not implemented yet. ;)\n");
366 util_get_time ( void )
369 gettimeofday(&tv,NULL);
370 return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
374 util_get_rank_increase ( const char *factory_class )
376 gint rank_pri_inc = 20;
377 gint rank_sec_inc = 10;
380 if ( g_strrstr(factory_class,"Dsp") )
382 else if ( g_strrstr(factory_class,"HW") )
384 else if ( g_strrstr(factory_class,"Arm") )
391 util_factory_rank_compare(GstPluginFeature *f1, GstPluginFeature *f2) // @
394 int f1_rank_inc=0, f2_rank_inc=0;
396 klass = gst_element_factory_get_klass(GST_ELEMENT_FACTORY(f1));
397 f1_rank_inc = util_get_rank_increase ( klass );
399 klass = gst_element_factory_get_klass(GST_ELEMENT_FACTORY(f2));
400 f2_rank_inc = util_get_rank_increase ( klass );
402 return (gst_plugin_feature_get_rank(f2)+f2_rank_inc) - (gst_plugin_feature_get_rank(f1)+f1_rank_inc );
406 util_get_charset(const char *file_path)
408 UCharsetDetector* ucsd;
409 const UCharsetMatch* ucm;
410 UErrorCode status = U_ZERO_ERROR;
412 const char* charset = NULL;
416 fin = fopen(file_path, "r");
419 debug_error("fail to open file %s\n", file_path);
423 ucsd = ucsdet_open( &status );
424 if( U_FAILURE(status) ) {
425 debug_error("fail to ucsdet_open\n");
429 ucsdet_enableInputFilter( ucsd, TRUE );
431 buf = g_malloc(1024*1024);
434 debug_error("fail to alloc\n");
438 fread( buf, 1, 1024*1024, fin );
441 ucsdet_setText( ucsd, buf, strlen(buf), &status );
442 if( U_FAILURE(status) ) {
443 debug_error("fail to ucsdet_setText\n");
447 ucm = ucsdet_detect( ucsd, &status );
448 if( U_FAILURE(status) ) {
449 debug_error("fail to ucsdet_detect\n");
453 charset = ucsdet_getName( ucm, &status );
454 if( U_FAILURE(status) ) {
455 debug_error("fail to ucsdet_getName\n");
460 ucsdet_close( ucsd );