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.
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
31 #include <unicode/ucsdet.h>
34 #include "mm_player_utils.h"
36 int util_exist_file_path(const char *file_path)
39 struct stat stat_results = {0, };
41 if (!file_path || !strlen(file_path))
42 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
44 fd = open (file_path, O_RDONLY);
49 strerror_r(errno, str_error, sizeof(str_error));
50 LOGE("failed to open file by %s (%d)", str_error, errno);
53 return MM_ERROR_PLAYER_PERMISSION_DENIED;
55 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
58 if (fstat(fd, &stat_results) < 0)
60 LOGE("failed to get file status");
62 else if (stat_results.st_size == 0)
64 LOGE("file size is zero");
66 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
70 LOGW("file size : %lld bytes", (long long)stat_results.st_size);
78 bool util_write_file_backup(const char *backup_path, char *data_ptr, int data_size)
83 fp = fopen(backup_path, "wb");
87 wsize = fwrite(data_ptr, sizeof(char), data_size, fp);
91 if (wsize != data_size) {
92 if (!access(backup_path, R_OK))
95 LOGE("No space to write!\n");
103 bool util_remove_file_backup(const char *backup_path)
105 if (!backup_path || !strlen(backup_path))
108 int res = access(backup_path, R_OK);
111 if (remove(backup_path) == -1)
118 #define DETECTION_PREFIX_SIZE 20
119 int util_is_midi_type_by_mem(void *mem, int size)
121 const char *p = (const char *)mem;
123 if (size < DETECTION_PREFIX_SIZE)
124 return MM_AUDIO_CODEC_INVALID;
126 /* mmf file detection */
127 if (p[0] == 'M' && p[1] == 'M' && p[2] == 'M' && p[3] == 'D') {
128 LOGD("MM_AUDIO_CODEC_MMF\n");
129 return MM_AUDIO_CODEC_MMF;
132 /* midi file detection */
133 if (p[0] == 'M' && p[1] == 'T' && p[2] == 'h' && p[3] == 'd') {
134 LOGD ("MM_AUDIO_CODEC_MIDI, %d\n", MM_AUDIO_CODEC_MIDI);
135 return MM_AUDIO_CODEC_MIDI;
137 /* mxmf file detection */
138 if (p[0] == 'X' && p[1] == 'M' && p[2] == 'F' && p[3] == '_') {
139 LOGD ("MM_AUDIO_CODEC_MXMF\n");
140 return MM_AUDIO_CODEC_MXMF;
143 /* wave file detection */
144 if (p[0] == 'R' && p[1] == 'I' && p[2] == 'F' && p[3] == 'F' &&
145 p[8] == 'W' && p[9] == 'A' && p[10] == 'V' && p[11] == 'E' &&
146 p[12] == 'f' && p[13] == 'm' && p[14] == 't') {
147 LOGD ("MM_AUDIO_CODEC_WAVE\n");
148 return MM_AUDIO_CODEC_WAVE;
150 /* i-melody file detection */
151 if (memcmp(p, "BEGIN:IMELODY", 13) == 0)
153 LOGD ("MM_AUDIO_CODEC_IMELODY\n");
154 return MM_AUDIO_CODEC_IMELODY;
157 return MM_AUDIO_CODEC_INVALID;
160 int util_is_midi_type_by_file(const char *file_path)
162 struct stat file_attrib;
164 char prefix[DETECTION_PREFIX_SIZE] = {0,};
170 fp = fopen(file_path, "r");
175 memset(&file_attrib, 0, sizeof(file_attrib));
177 if (stat(file_path, &file_attrib) != 0)
183 size = (int) file_attrib.st_size;
185 if (size < DETECTION_PREFIX_SIZE)
191 size = fread(prefix, sizeof(char), DETECTION_PREFIX_SIZE, fp);
195 return util_is_midi_type_by_mem(prefix, size);
199 util_get_cookie_list ( const char *cookies )
201 char **cookie_list = NULL;
205 if ( !cookies || !strlen(cookies) )
208 SECURE_LOGD("cookies : %d[bytes] - %s \n", strlen(cookies), cookies);
210 temp = g_strdup(cookies);
212 /* trimming. it works inplace */
216 cookie_list = g_strsplit(temp, ";", 100);
218 for ( i = 0; i < g_strv_length(cookie_list); i++ )
222 if (strlen(cookie_list[i]))
224 g_strstrip(cookie_list[i]);
225 SECURE_LOGD("cookie_list[%d] : %d[bytes] - %s \n", i, strlen(cookie_list[i]), cookie_list[i]);
229 cookie_list[i][0]='\0';
241 bool util_check_valid_url ( const char *proxy )
243 struct in_addr proxy_addr;
246 MMPLAYER_RETURN_VAL_IF_FAIL ( proxy, FALSE );
247 MMPLAYER_RETURN_VAL_IF_FAIL ( strlen(proxy), FALSE );
249 if ( inet_aton(proxy, &proxy_addr) != 0 )
251 LOGW("invalid proxy is set. \n");
258 /* check the given path is indicating sdp file */
260 util_is_sdp_file ( const char *path )
262 gboolean ret = FALSE;
267 MMPLAYER_RETURN_VAL_IF_FAIL ( path, FALSE );
269 uri = g_ascii_strdown ( path, -1 );
279 /* strlen(".sdp") == 4 */
280 if ( strlen( uri ) <= 4 )
282 LOGW ( "path is too short.\n" );
286 /* first, check extension name */
287 ret = g_str_has_suffix ( uri, "sdp" );
289 /* second, if no suffix is there, check it's contents */
292 /* FIXIT : do it soon */
302 util_get_time ( void )
305 gettimeofday(&tv,NULL);
306 return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
310 util_get_rank_increase ( const char *factory_class )
312 gint rank_pri_inc = 20;
313 gint rank_sec_inc = 10;
316 if ( g_strrstr(factory_class,"Dsp") )
318 else if ( g_strrstr(factory_class,"HW") )
320 else if ( g_strrstr(factory_class,"Arm") )
327 util_factory_rank_compare(GstPluginFeature *f1, GstPluginFeature *f2)
330 int f1_rank_inc=0, f2_rank_inc=0;
332 klass = gst_element_factory_get_klass(GST_ELEMENT_FACTORY(f1));
333 f1_rank_inc = util_get_rank_increase ( klass );
335 klass = gst_element_factory_get_klass(GST_ELEMENT_FACTORY(f2));
336 f2_rank_inc = util_get_rank_increase ( klass );
338 return (gst_plugin_feature_get_rank(f2)+f2_rank_inc) - (gst_plugin_feature_get_rank(f1)+f1_rank_inc );
342 util_get_charset(const char *file_path)
344 UCharsetDetector* ucsd;
345 const UCharsetMatch* ucm;
346 UErrorCode status = U_ZERO_ERROR;
348 const char* charset = NULL;
353 fin = fopen(file_path, "r");
356 SECURE_LOGE("fail to open file %s\n", file_path);
360 ucsd = ucsdet_open( &status );
361 if( U_FAILURE(status) ) {
362 LOGE("fail to ucsdet_open\n");
366 ucsdet_enableInputFilter( ucsd, TRUE );
368 buf = g_malloc(1024*1024);
371 LOGE("fail to alloc\n");
375 n_size = fread( buf, 1, 1024*1024, fin );
380 ucsdet_setText( ucsd, buf, strlen(buf), &status );
381 if( U_FAILURE(status) ) {
382 LOGE("fail to ucsdet_setText\n");
386 ucm = ucsdet_detect( ucsd, &status );
387 if( U_FAILURE(status) ) {
388 LOGE("fail to ucsdet_detect\n");
392 charset = ucsdet_getName( ucm, &status );
393 if( U_FAILURE(status) ) {
394 LOGE("fail to ucsdet_getName\n");
398 /* CP949 encoding is an extension of the EUC-KR and it is backwards compatible.*/
399 if(charset && !strcmp(charset, "EUC-KR")) {
408 ucsdet_close( ucsd );
416 int util_get_pixtype(unsigned int fourcc)
418 int pixtype = MM_PIXEL_FORMAT_INVALID;
421 char *pfourcc = (char*)&fourcc;
423 LOGD("fourcc(%c%c%c%c)",
424 pfourcc[0], pfourcc[1], pfourcc[2], pfourcc[3]);
428 case GST_MAKE_FOURCC ('S', 'N', '1', '2'):
429 case GST_MAKE_FOURCC ('N', 'V', '1', '2'):
430 pixtype = MM_PIXEL_FORMAT_NV12;
432 case GST_MAKE_FOURCC ('S', 'T', '1', '2'):
433 pixtype = MM_PIXEL_FORMAT_NV12T;
435 case GST_MAKE_FOURCC ('S', 'N', '2', '1'):
436 case GST_MAKE_FOURCC ('N', 'V', '2', '1'):
437 pixtype = MM_PIXEL_FORMAT_NV21;
439 case GST_MAKE_FOURCC ('S', 'U', 'Y', 'V'):
440 case GST_MAKE_FOURCC ('Y', 'U', 'Y', 'V'):
441 case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
442 pixtype = MM_PIXEL_FORMAT_YUYV;
444 case GST_MAKE_FOURCC ('S', 'Y', 'V', 'Y'):
445 case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
446 pixtype = MM_PIXEL_FORMAT_UYVY;
448 case GST_MAKE_FOURCC ('S', '4', '2', '0'):
449 case GST_MAKE_FOURCC ('I', '4', '2', '0'):
450 pixtype = MM_PIXEL_FORMAT_I420;
452 case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
453 pixtype = MM_PIXEL_FORMAT_YV12;
455 case GST_MAKE_FOURCC ('4', '2', '2', 'P'):
456 pixtype = MM_PIXEL_FORMAT_422P;
458 case GST_MAKE_FOURCC ('R', 'G', 'B', 'P'):
459 pixtype = MM_PIXEL_FORMAT_RGB565;
461 case GST_MAKE_FOURCC ('R', 'G', 'B', '3'):
462 pixtype = MM_PIXEL_FORMAT_RGB888;
464 case GST_MAKE_FOURCC ('A', 'R', 'G', 'B'):
465 case GST_MAKE_FOURCC ('x', 'R', 'G', 'B'):
466 pixtype = MM_PIXEL_FORMAT_ARGB;
468 case GST_MAKE_FOURCC ('B', 'G', 'R', 'A'):
469 case GST_MAKE_FOURCC ('B', 'G', 'R', 'x'):
470 case GST_MAKE_FOURCC ('S', 'R', '3', '2'):
471 pixtype = MM_PIXEL_FORMAT_RGBA;
473 case GST_MAKE_FOURCC ('J', 'P', 'E', 'G'):
474 case GST_MAKE_FOURCC ('P', 'N', 'G', ' '):
475 pixtype = MM_PIXEL_FORMAT_ENCODED;
477 case GST_MAKE_FOURCC ('I', 'T', 'L', 'V'):
478 pixtype = MM_PIXEL_FORMAT_ITLV_JPEG_UYVY;
481 LOGE("Not supported fourcc type(%c%c%c%c)",
482 fourcc, fourcc>>8, fourcc>>16, fourcc>>24);
483 pixtype = MM_PIXEL_FORMAT_INVALID;