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.
28 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <unicode/ucsdet.h>
35 #include "mm_player_utils.h"
37 /* for getting status of connecting external display */
39 #include <vconf-internal-sysman-keys.h>
40 #include <vconf-internal-wifi-keys.h>
42 int util_exist_file_path(const char *file_path)
45 struct stat stat_results = {0, };
47 if (!file_path || !strlen(file_path))
48 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
50 fd = open (file_path, O_RDONLY);
54 debug_error("failed to open file by %s (%d)", strerror(errno), errno);
57 return MM_ERROR_PLAYER_PERMISSION_DENIED;
59 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
62 if (fstat(fd, &stat_results) < 0)
64 debug_error("failed to get file status");
66 else if (stat_results.st_size == 0)
68 debug_error("file size is zero");
70 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
74 debug_warning("file size : %lld bytes", (long long)stat_results.st_size);
82 bool util_write_file_backup(const char *backup_path, char *data_ptr, int data_size)
87 fp = fopen(backup_path, "wb");
91 wsize = fwrite(data_ptr, sizeof(char), data_size, fp);
95 if (wsize != data_size) {
96 if (!access(backup_path, R_OK))
99 debug_error("No space to write!\n");
107 bool util_remove_file_backup(const char *backup_path)
109 if (!backup_path || !strlen(backup_path))
112 int res = access(backup_path, R_OK);
115 if (remove(backup_path) == -1)
122 #define DETECTION_PREFIX_SIZE 20
123 //bool util_is_midi_type_by_mem(void *mem, int size)
124 int util_is_midi_type_by_mem(void *mem, int size)
126 const char *p = (const char *)mem;
128 if (size < DETECTION_PREFIX_SIZE)
129 return MM_AUDIO_CODEC_INVALID;
131 /* mmf file detection */
132 if (p[0] == 'M' && p[1] == 'M' && p[2] == 'M' && p[3] == 'D') {
133 debug_log("MM_AUDIO_CODEC_MMF\n");
134 return MM_AUDIO_CODEC_MMF;
137 /* midi file detection */
138 if (p[0] == 'M' && p[1] == 'T' && p[2] == 'h' && p[3] == 'd') {
139 debug_log ("MM_AUDIO_CODEC_MIDI, %d\n", MM_AUDIO_CODEC_MIDI);
140 return MM_AUDIO_CODEC_MIDI;
142 /* mxmf file detection */
143 if (p[0] == 'X' && p[1] == 'M' && p[2] == 'F' && p[3] == '_') {
144 debug_log ("MM_AUDIO_CODEC_MXMF\n");
145 return MM_AUDIO_CODEC_MXMF;
148 /* wave file detection */
149 if (p[0] == 'R' && p[1] == 'I' && p[2] == 'F' && p[3] == 'F' &&
150 p[8] == 'W' && p[9] == 'A' && p[10] == 'V' && p[11] == 'E' &&
151 p[12] == 'f' && p[13] == 'm' && p[14] == 't') {
152 debug_log ("MM_AUDIO_CODEC_WAVE\n");
153 return MM_AUDIO_CODEC_WAVE;
155 /* i-melody file detection */
156 if (memcmp(p, "BEGIN:IMELODY", 13) == 0)
158 debug_log ("MM_AUDIO_CODEC_IMELODY\n");
159 return MM_AUDIO_CODEC_IMELODY;
162 return MM_AUDIO_CODEC_INVALID;
165 //bool util_is_midi_type_by_file(const char *file_path)
166 int util_is_midi_type_by_file(const char *file_path)
168 struct stat file_attrib;
170 char prefix[DETECTION_PREFIX_SIZE] = {0,};
176 fp = fopen(file_path, "r");
181 memset(&file_attrib, 0, sizeof(file_attrib));
183 if (stat(file_path, &file_attrib) != 0)
189 size = (int) file_attrib.st_size;
191 if (size < DETECTION_PREFIX_SIZE)
197 size = fread(prefix, sizeof(char), DETECTION_PREFIX_SIZE, fp);
201 return util_is_midi_type_by_mem(prefix, size);
205 util_get_cookie_list ( const char *cookies )
207 char **cookie_list = NULL;
211 if ( !cookies || !strlen(cookies) )
214 secure_debug_log("cookies : %d[bytes] - %s \n", strlen(cookies), cookies);
216 temp = g_strdup(cookies);
218 /* trimming. it works inplace */
222 cookie_list = g_strsplit(temp, ";", 100);
224 for ( i = 0; i < g_strv_length(cookie_list); i++ )
226 if ( cookie_list[i] && strlen(cookie_list[i]) )
228 g_strstrip(cookie_list[i]);
229 secure_debug_log("cookie_list[%d] : %d[bytes] - %s \n", i, strlen(cookie_list[i]), cookie_list[i]);
233 cookie_list[i][0]='\0';
244 bool util_check_valid_url ( const char *proxy )
246 struct in_addr proxy_addr;
249 return_val_if_fail ( proxy, FALSE );
250 return_val_if_fail ( strlen(proxy), FALSE );
252 if ( inet_aton(proxy, &proxy_addr) != 0 )
254 debug_warning("invalid proxy is set. \n");
261 /* check the given path is indicating sdp file */
263 util_is_sdp_file ( const char *path )
265 gboolean ret = FALSE;
270 return_val_if_fail ( path, FALSE );
272 uri = g_ascii_strdown ( path, -1 );
282 /* strlen(".sdp") == 4 */
283 if ( strlen( uri ) <= 4 )
285 debug_warning ( "path is too short.\n" );
289 /* first, check extension name */
290 ret = g_str_has_suffix ( uri, "sdp" );
292 /* second, if no suffix is there, check it's contents */
295 /* FIXIT : do it soon */
305 util_get_time ( void )
308 gettimeofday(&tv,NULL);
309 return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
313 util_get_rank_increase ( const char *factory_class )
315 gint rank_pri_inc = 20;
316 gint rank_sec_inc = 10;
319 if ( g_strrstr(factory_class,"Dsp") )
321 else if ( g_strrstr(factory_class,"HW") )
323 else if ( g_strrstr(factory_class,"Arm") )
330 util_factory_rank_compare(GstPluginFeature *f1, GstPluginFeature *f2) // @
333 int f1_rank_inc=0, f2_rank_inc=0;
335 klass = gst_element_factory_get_klass(GST_ELEMENT_FACTORY(f1));
336 f1_rank_inc = util_get_rank_increase ( klass );
338 klass = gst_element_factory_get_klass(GST_ELEMENT_FACTORY(f2));
339 f2_rank_inc = util_get_rank_increase ( klass );
341 return (gst_plugin_feature_get_rank(f2)+f2_rank_inc) - (gst_plugin_feature_get_rank(f1)+f1_rank_inc );
345 util_get_charset(const char *file_path)
347 UCharsetDetector* ucsd;
348 const UCharsetMatch* ucm;
349 UErrorCode status = U_ZERO_ERROR;
351 const char* charset = NULL;
356 fin = fopen(file_path, "r");
359 secure_debug_error("fail to open file %s\n", file_path);
363 ucsd = ucsdet_open( &status );
364 if( U_FAILURE(status) ) {
365 debug_error("fail to ucsdet_open\n");
369 ucsdet_enableInputFilter( ucsd, TRUE );
371 buf = g_malloc(1024*1024);
374 debug_error("fail to alloc\n");
378 n_size = fread( buf, 1, 1024*1024, fin );
383 ucsdet_setText( ucsd, buf, strlen(buf), &status );
384 if( U_FAILURE(status) ) {
385 debug_error("fail to ucsdet_setText\n");
389 ucm = ucsdet_detect( ucsd, &status );
390 if( U_FAILURE(status) ) {
391 debug_error("fail to ucsdet_detect\n");
395 charset = ucsdet_getName( ucm, &status );
396 if( U_FAILURE(status) ) {
397 debug_error("fail to ucsdet_getName\n");
406 ucsdet_close( ucsd );
415 util_get_is_connected_external_display(void)
417 int is_connected_hdmi = -1;
418 int is_connected_mirroring = -1;
421 if (vconf_get_int(VCONFKEY_SYSMAN_HDMI, &is_connected_hdmi))
422 debug_error("[hdmi]vconf_set_int FAIL");
423 if (vconf_get_int(VCONFKEY_SCREEN_MIRRORING_STATE, &is_connected_mirroring))
424 debug_error("[mirroring]vconf_set_int FAIL");
426 /* if conneted with external display */
427 if (is_connected_mirroring == VCONFKEY_SCREEN_MIRRORING_CONNECTED) {
428 debug_warning ("connected with mirroring display");
429 return MMPLAYER_DISPLAY_MIRRORING_ACTIVE;
431 if (is_connected_hdmi == VCONFKEY_SYSMAN_HDMI_CONNECTED) {
432 debug_warning ("connected with external display");
433 return MMPLAYER_DISPLAY_HDMI_ACTIVE;
435 if ((is_connected_mirroring == VCONFKEY_SCREEN_MIRRORING_ACTIVATED || is_connected_mirroring == VCONFKEY_SCREEN_MIRRORING_DEACTIVATED) && is_connected_hdmi == VCONFKEY_SYSMAN_HDMI_DISCONNECTED) {
436 debug_warning ("non-connected status");
437 return MMPLAYER_DISPLAY_NULL;
440 debug_error ("it is not registered (%d, %d)", is_connected_mirroring, is_connected_hdmi);
444 int util_get_pixtype(unsigned int fourcc)
446 int pixtype = MM_PIXEL_FORMAT_INVALID;
449 char *pfourcc = (char*)&fourcc;
451 debug_log("fourcc(%c%c%c%c)",
452 pfourcc[0], pfourcc[1], pfourcc[2], pfourcc[3]);
457 case GST_MAKE_FOURCC ('S', 'N', '1', '2'):
458 case GST_MAKE_FOURCC ('N', 'V', '1', '2'):
459 pixtype = MM_PIXEL_FORMAT_NV12;
461 case GST_MAKE_FOURCC ('S', 'T', '1', '2'):
462 pixtype = MM_PIXEL_FORMAT_NV12T;
464 case GST_MAKE_FOURCC ('S', 'N', '2', '1'):
465 case GST_MAKE_FOURCC ('N', 'V', '2', '1'):
466 pixtype = MM_PIXEL_FORMAT_NV21;
468 case GST_MAKE_FOURCC ('S', 'U', 'Y', 'V'):
469 case GST_MAKE_FOURCC ('Y', 'U', 'Y', 'V'):
470 case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
471 pixtype = MM_PIXEL_FORMAT_YUYV;
473 case GST_MAKE_FOURCC ('S', 'Y', 'V', 'Y'):
474 case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
475 pixtype = MM_PIXEL_FORMAT_UYVY;
477 case GST_MAKE_FOURCC ('S', '4', '2', '0'):
478 case GST_MAKE_FOURCC ('I', '4', '2', '0'):
479 pixtype = MM_PIXEL_FORMAT_I420;
481 case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
482 pixtype = MM_PIXEL_FORMAT_YV12;
484 case GST_MAKE_FOURCC ('4', '2', '2', 'P'):
485 pixtype = MM_PIXEL_FORMAT_422P;
487 case GST_MAKE_FOURCC ('R', 'G', 'B', 'P'):
488 pixtype = MM_PIXEL_FORMAT_RGB565;
490 case GST_MAKE_FOURCC ('R', 'G', 'B', '3'):
491 pixtype = MM_PIXEL_FORMAT_RGB888;
493 case GST_MAKE_FOURCC ('A', 'R', 'G', 'B'):
494 case GST_MAKE_FOURCC ('x', 'R', 'G', 'B'):
495 pixtype = MM_PIXEL_FORMAT_ARGB;
497 case GST_MAKE_FOURCC ('B', 'G', 'R', 'A'):
498 case GST_MAKE_FOURCC ('B', 'G', 'R', 'x'):
499 case GST_MAKE_FOURCC ('S', 'R', '3', '2'):
500 pixtype = MM_PIXEL_FORMAT_RGBA;
502 case GST_MAKE_FOURCC ('J', 'P', 'E', 'G'):
503 case GST_MAKE_FOURCC ('P', 'N', 'G', ' '):
504 pixtype = MM_PIXEL_FORMAT_ENCODED;
507 case GST_MAKE_FOURCC ('I', 'T', 'L', 'V'):
508 pixtype = MM_PIXEL_FORMAT_ITLV_JPEG_UYVY;
511 debug_error("Not supported fourcc type(%c%c%c%c)",
512 fourcc, fourcc>>8, fourcc>>16, fourcc>>24);
513 pixtype = MM_PIXEL_FORMAT_INVALID;