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);
204 /* messages are treated as warnings bcz those code should not be checked in.
205 * and no error handling will supported for same manner.
208 __util_gst_pad_probe(GstPad *pad, GstBuffer *buffer, gpointer u_data)
210 gint flag = (gint) u_data;
211 GstElement* parent = NULL;
212 GstMapInfo mapinfo = {0, };
215 /* show name as default */
216 parent = (GstElement*)gst_object_get_parent(GST_OBJECT(pad));
217 debug_log("PAD PROBE : %s:%s\n", GST_ELEMENT_NAME(parent), GST_PAD_NAME(pad));
219 /* show time stamp */
220 if ( flag & MM_PROBE_TIMESTAMP )
222 debug_log("ts : %u:%02u:%02u.%09u\n", GST_TIME_ARGS(GST_BUFFER_TIMESTAMP(buffer)));
225 /* show buffer size */
226 gst_buffer_map(buffer, &mapinfo, GST_MAP_READ);
227 if ( flag & MM_PROBE_BUFFERSIZE )
229 debug_log("buffer size : %ud\n", mapinfo.size);
231 gst_buffer_unmap(buffer, &mapinfo);
233 /* show buffer duration */
234 if ( flag & MM_PROBE_BUFFER_DURATION )
236 debug_log("dur : %lld\n", GST_BUFFER_DURATION(buffer));
239 /* show buffer caps */
240 if ( flag & MM_PROBE_CAPS )
242 MMPLAYER_LOG_GST_CAPS_TYPE(gst_pad_get_current_caps(pad));
245 /* drop buffer if flag is on */
246 if ( flag & MM_PROBE_DROP_BUFFER )
248 debug_log("dropping\n");
252 /* show clock time */
253 if ( flag & MM_PROBE_CLOCK_TIME )
255 GstClock* clock = NULL;
256 GstClockTime now = GST_CLOCK_TIME_NONE;
258 clock = GST_ELEMENT_CLOCK ( parent );
262 now = gst_clock_get_time( clock );
263 debug_log("clock time : %" GST_TIME_FORMAT "\n", GST_TIME_ARGS( now ));
268 gst_object_unref(parent);
274 util_get_cookie_list ( const char *cookies )
276 char **cookie_list = NULL;
280 if ( !cookies || !strlen(cookies) )
283 secure_debug_log("cookies : %d[bytes] - %s \n", strlen(cookies), cookies);
285 temp = g_strdup(cookies);
287 /* trimming. it works inplace */
291 cookie_list = g_strsplit(temp, ";", 100);
293 for ( i = 0; i < g_strv_length(cookie_list); i++ )
295 if ( cookie_list[i] && strlen(cookie_list[i]) )
297 g_strstrip(cookie_list[i]);
298 secure_debug_log("cookie_list[%d] : %d[bytes] - %s \n", i, strlen(cookie_list[i]), cookie_list[i]);
302 cookie_list[i][0]='\0';
313 bool util_check_valid_url ( const char *proxy )
315 struct in_addr proxy_addr;
318 return_val_if_fail ( proxy, FALSE );
319 return_val_if_fail ( strlen(proxy), FALSE );
321 if ( inet_aton(proxy, &proxy_addr) != 0 )
323 debug_warning("invalid proxy is set. \n");
330 /* check the given path is indicating sdp file */
332 util_is_sdp_file ( const char *path )
334 gboolean ret = FALSE;
339 return_val_if_fail ( path, FALSE );
341 uri = g_ascii_strdown ( path, -1 );
351 /* strlen(".sdp") == 4 */
352 if ( strlen( uri ) <= 4 )
354 debug_warning ( "path is too short.\n" );
358 /* first, check extension name */
359 ret = g_str_has_suffix ( uri, "sdp" );
361 /* second, if no suffix is there, check it's contents */
364 /* FIXIT : do it soon */
374 util_get_time ( void )
377 gettimeofday(&tv,NULL);
378 return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
382 util_get_rank_increase ( const char *factory_class )
384 gint rank_pri_inc = 20;
385 gint rank_sec_inc = 10;
388 if ( g_strrstr(factory_class,"Dsp") )
390 else if ( g_strrstr(factory_class,"HW") )
392 else if ( g_strrstr(factory_class,"Arm") )
399 util_factory_rank_compare(GstPluginFeature *f1, GstPluginFeature *f2) // @
402 int f1_rank_inc=0, f2_rank_inc=0;
404 klass = gst_element_factory_get_klass(GST_ELEMENT_FACTORY(f1));
405 f1_rank_inc = util_get_rank_increase ( klass );
407 klass = gst_element_factory_get_klass(GST_ELEMENT_FACTORY(f2));
408 f2_rank_inc = util_get_rank_increase ( klass );
410 return (gst_plugin_feature_get_rank(f2)+f2_rank_inc) - (gst_plugin_feature_get_rank(f1)+f1_rank_inc );
414 util_get_charset(const char *file_path)
416 UCharsetDetector* ucsd;
417 const UCharsetMatch* ucm;
418 UErrorCode status = U_ZERO_ERROR;
420 const char* charset = NULL;
425 fin = fopen(file_path, "r");
428 secure_debug_error("fail to open file %s\n", file_path);
432 ucsd = ucsdet_open( &status );
433 if( U_FAILURE(status) ) {
434 debug_error("fail to ucsdet_open\n");
438 ucsdet_enableInputFilter( ucsd, TRUE );
440 buf = g_malloc(1024*1024);
443 debug_error("fail to alloc\n");
447 n_size = fread( buf, 1, 1024*1024, fin );
452 ucsdet_setText( ucsd, buf, strlen(buf), &status );
453 if( U_FAILURE(status) ) {
454 debug_error("fail to ucsdet_setText\n");
458 ucm = ucsdet_detect( ucsd, &status );
459 if( U_FAILURE(status) ) {
460 debug_error("fail to ucsdet_detect\n");
464 charset = ucsdet_getName( ucm, &status );
465 if( U_FAILURE(status) ) {
466 debug_error("fail to ucsdet_getName\n");
475 ucsdet_close( ucsd );
484 util_get_is_connected_external_display(void)
486 int is_connected_hdmi = -1;
487 int is_connected_mirroring = -1;
490 if (vconf_get_int(VCONFKEY_SYSMAN_HDMI, &is_connected_hdmi))
491 debug_error("[hdmi]vconf_set_int FAIL");
492 if (vconf_get_int(VCONFKEY_SCREEN_MIRRORING_STATE, &is_connected_mirroring))
493 debug_error("[mirroring]vconf_set_int FAIL");
495 /* if conneted with external display */
496 if (is_connected_mirroring == VCONFKEY_SCREEN_MIRRORING_CONNECTED) {
497 debug_warning ("connected with mirroring display");
498 return MMPLAYER_DISPLAY_MIRRORING_ACTIVE;
500 if (is_connected_hdmi == VCONFKEY_SYSMAN_HDMI_CONNECTED) {
501 debug_warning ("connected with external display");
502 return MMPLAYER_DISPLAY_HDMI_ACTIVE;
504 if ((is_connected_mirroring == VCONFKEY_SCREEN_MIRRORING_ACTIVATED || is_connected_mirroring == VCONFKEY_SCREEN_MIRRORING_DEACTIVATED) && is_connected_hdmi == VCONFKEY_SYSMAN_HDMI_DISCONNECTED) {
505 debug_warning ("non-connected status");
506 return MMPLAYER_DISPLAY_NULL;
509 debug_error ("it is not registered (%d, %d)", is_connected_mirroring, is_connected_hdmi);
513 gboolean util_is_miracast_connected(void)
515 int is_connected = 0;
517 if (vconf_get_bool(VCONFKEY_MIRACAST_WFD_SOURCE_STATUS, &is_connected) ) {
518 debug_error("failed to get miracast status key");
522 if (VCONFKEY_MIRACAST_WFD_SOURCE_ON == is_connected) {
523 debug_warning("miracast connected");
530 int util_get_pixtype(unsigned int fourcc)
532 int pixtype = MM_PIXEL_FORMAT_INVALID;
535 char *pfourcc = (char*)&fourcc;
537 debug_log("fourcc(%c%c%c%c)",
538 pfourcc[0], pfourcc[1], pfourcc[2], pfourcc[3]);
543 case GST_MAKE_FOURCC ('S', 'N', '1', '2'):
544 case GST_MAKE_FOURCC ('N', 'V', '1', '2'):
545 pixtype = MM_PIXEL_FORMAT_NV12;
547 case GST_MAKE_FOURCC ('S', 'N', '2', '1'):
548 case GST_MAKE_FOURCC ('N', 'V', '2', '1'):
549 pixtype = MM_PIXEL_FORMAT_NV21;
551 case GST_MAKE_FOURCC ('S', 'U', 'Y', 'V'):
552 case GST_MAKE_FOURCC ('Y', 'U', 'Y', 'V'):
553 case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
554 pixtype = MM_PIXEL_FORMAT_YUYV;
556 case GST_MAKE_FOURCC ('S', 'Y', 'V', 'Y'):
557 case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
558 pixtype = MM_PIXEL_FORMAT_UYVY;
560 case GST_MAKE_FOURCC ('S', '4', '2', '0'):
561 case GST_MAKE_FOURCC ('I', '4', '2', '0'):
562 pixtype = MM_PIXEL_FORMAT_I420;
564 case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
565 pixtype = MM_PIXEL_FORMAT_YV12;
567 case GST_MAKE_FOURCC ('4', '2', '2', 'P'):
568 pixtype = MM_PIXEL_FORMAT_422P;
570 case GST_MAKE_FOURCC ('R', 'G', 'B', 'P'):
571 pixtype = MM_PIXEL_FORMAT_RGB565;
573 case GST_MAKE_FOURCC ('R', 'G', 'B', '3'):
574 pixtype = MM_PIXEL_FORMAT_RGB888;
576 case GST_MAKE_FOURCC ('A', 'R', 'G', 'B'):
577 case GST_MAKE_FOURCC ('x', 'R', 'G', 'B'):
578 pixtype = MM_PIXEL_FORMAT_ARGB;
580 case GST_MAKE_FOURCC ('B', 'G', 'R', 'A'):
581 case GST_MAKE_FOURCC ('B', 'G', 'R', 'x'):
582 case GST_MAKE_FOURCC ('S', 'R', '3', '2'):
583 pixtype = MM_PIXEL_FORMAT_RGBA;
585 case GST_MAKE_FOURCC ('J', 'P', 'E', 'G'):
586 case GST_MAKE_FOURCC ('P', 'N', 'G', ' '):
587 pixtype = MM_PIXEL_FORMAT_ENCODED;
590 case GST_MAKE_FOURCC ('I', 'T', 'L', 'V'):
591 pixtype = MM_PIXEL_FORMAT_ITLV_JPEG_UYVY;
594 debug_error("Not supported fourcc type(%c%c%c%c)",
595 fourcc, fourcc>>8, fourcc>>16, fourcc>>24);
596 pixtype = MM_PIXEL_FORMAT_INVALID;