tizen 2.3.1 release
[framework/multimedia/libmm-player.git] / src / mm_player_utils.c
index 6ededf3..e32006e 100755 (executable)
 #include <string.h>
 #include <unistd.h>
 #include <sys/stat.h>
+#include <fcntl.h>
 #include <sys/socket.h>
+#include <sys/time.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
+#include <unicode/ucsdet.h>
+
 #include <mm_debug.h>
 #include "mm_player_utils.h"
 
-bool util_exist_file_path(const char *file_path)
+/* for getting status of connecting external display */
+#include <vconf.h>
+#include <vconf-internal-sysman-keys.h>
+#include <vconf-internal-wifi-keys.h>
+
+#define READ_MAX_BUFFER_SIZE 1024
+
+int util_exist_file_path(const char *file_path)
 {
-       debug_log("\n");
+       int fd = 0;
+       struct stat stat_results = {0, };
 
        if (!file_path || !strlen(file_path))
-               return FALSE;
+               return MM_ERROR_PLAYER_FILE_NOT_FOUND;
 
-       int res = access(file_path, R_OK);
-       if (res)
-               return FALSE;
+       fd = open (file_path, O_RDONLY);
 
-       return TRUE;
+       if (fd < 0)
+       {
+               char str_error[READ_MAX_BUFFER_SIZE];
+               debug_error("failed to open file by %s (%d)", strerror_r(errno, str_error, sizeof(str_error)), errno);
+
+               if (EACCES == errno)
+                       return MM_ERROR_PLAYER_PERMISSION_DENIED;
+
+               return MM_ERROR_PLAYER_FILE_NOT_FOUND;
+       }
+
+       if (fstat(fd, &stat_results) < 0)
+       {
+               debug_error("failed to get file status");
+       }
+       else if (stat_results.st_size == 0)
+       {
+               debug_error("file size is zero");
+               close(fd);
+               return MM_ERROR_PLAYER_FILE_NOT_FOUND;
+       }
+       else
+       {
+               debug_warning("file size : %lld bytes", (long long)stat_results.st_size);
+       }
+
+       close(fd);
+
+       return MM_ERROR_NONE;
 }
 
 bool util_write_file_backup(const char *backup_path, char *data_ptr, int data_size)
 {
-       debug_log("\n");
-
        FILE *fp = NULL;
        int wsize = 0;
 
@@ -71,48 +107,42 @@ bool util_write_file_backup(const char *backup_path, char *data_ptr, int data_si
        return TRUE;
 }
 
-#if 1 //tskim:MidiModuleRequires:+: for Midi player
 bool util_remove_file_backup(const char *backup_path)
 {
-       debug_log("\n");
-
        if (!backup_path || !strlen(backup_path))
                return FALSE;
 
-/*
-     Prevent defect patch. CID:22389 Checker:TOTCU
        int res = access(backup_path, R_OK);
        if (!res)
-*/
-               remove(backup_path);
+       {
+               if (remove(backup_path) == -1)
+                       return FALSE;
+       }
 
        return TRUE;
 }
-#endif
 
 #define DETECTION_PREFIX_SIZE  20
 //bool util_is_midi_type_by_mem(void *mem, int size)
 int util_is_midi_type_by_mem(void *mem, int size)
 {
-       debug_log("\n");
-       
        const char *p = (const char *)mem;
 
        if (size < DETECTION_PREFIX_SIZE)
-               return MM_AUDIO_CODEC_INVALID; //FALSE;         // sbs:+:080903
+               return MM_AUDIO_CODEC_INVALID;
 
        /* mmf file detection */
        if (p[0] == 'M' && p[1] == 'M' && p[2] == 'M' && p[3] == 'D') {
                debug_log("MM_AUDIO_CODEC_MMF\n");
-               return MM_AUDIO_CODEC_MMF; // TRUE;// sbs:+:080903
+               return MM_AUDIO_CODEC_MMF;
        }
 
        /* midi file detection */
        if (p[0] == 'M' && p[1] == 'T' && p[2] == 'h' && p[3] == 'd') {
                debug_log ("MM_AUDIO_CODEC_MIDI, %d\n", MM_AUDIO_CODEC_MIDI);
-               return MM_AUDIO_CODEC_MIDI;//TRUE;// sbs:+:080903
+               return MM_AUDIO_CODEC_MIDI;
        }
-       /* mxmf file detection */ // sbs:+:080903
+       /* mxmf file detection */
        if (p[0] == 'X' && p[1] == 'M' && p[2] == 'F' && p[3] == '_') {
                debug_log ("MM_AUDIO_CODEC_MXMF\n");
                return MM_AUDIO_CODEC_MXMF;
@@ -123,23 +153,21 @@ int util_is_midi_type_by_mem(void *mem, int size)
                p[8] == 'W' && p[9] == 'A' && p[10] == 'V' && p[11] == 'E' &&
                p[12] == 'f' && p[13] == 'm' && p[14] == 't') {
                debug_log ("MM_AUDIO_CODEC_WAVE\n");
-               return MM_AUDIO_CODEC_WAVE;//TRUE;// sbs:+:080903
+               return MM_AUDIO_CODEC_WAVE;
        }
-       /* i-melody file detection */ // sbs:+:080903
+       /* i-melody file detection */
        if (memcmp(p, "BEGIN:IMELODY", 13) == 0)
        {
                debug_log ("MM_AUDIO_CODEC_IMELODY\n");
                return MM_AUDIO_CODEC_IMELODY;
        }
 
-       return MM_AUDIO_CODEC_INVALID;//FALSE; // sbs:+:080903
+       return MM_AUDIO_CODEC_INVALID;
 }
 
 //bool util_is_midi_type_by_file(const char *file_path)
 int util_is_midi_type_by_file(const char *file_path)
 {
-       debug_log("\n");
-       
        struct stat file_attrib;
        FILE *fp = NULL;
        char prefix[DETECTION_PREFIX_SIZE] = {0,};
@@ -148,7 +176,6 @@ int util_is_midi_type_by_file(const char *file_path)
        if (!file_path)
                return FALSE;
 
-       /* Prevent defect patch. CID: 22388 Checker: TOCTOU */
        fp = fopen(file_path, "r");
 
        if (!fp)
@@ -169,7 +196,7 @@ int util_is_midi_type_by_file(const char *file_path)
                fclose(fp);
                return FALSE;
        }
-       
+
        size = fread(prefix, sizeof(char), DETECTION_PREFIX_SIZE, fp);
 
        fclose(fp);
@@ -177,51 +204,57 @@ int util_is_midi_type_by_file(const char *file_path)
        return util_is_midi_type_by_mem(prefix, size);
 }
 
-/* messages are treated as warnings bcz those code should not be checked in. 
- * and no error handling will supported for same manner. 
+/* messages are treated as warnings bcz those code should not be checked in.
+ * and no error handling will supported for same manner.
  */
-gboolean 
+gboolean
 __util_gst_pad_probe(GstPad *pad, GstBuffer *buffer, gpointer u_data)
 {
        gint flag = (gint) u_data;
        GstElement* parent = NULL;
        gboolean ret = TRUE;
-       
+
        /* show name as default */
        parent = (GstElement*)gst_object_get_parent(GST_OBJECT(pad));
-       debug_warning("PAD PROBE : %s:%s\n", GST_ELEMENT_NAME(parent), GST_PAD_NAME(pad));
-       
+       if(parent == NULL)
+       {
+               debug_error("parent is null\n");
+               return false;
+       }
+
+       debug_log("PAD PROBE : %s:%s\n", GST_ELEMENT_NAME(parent), GST_PAD_NAME(pad));
+
        /* show time stamp */
        if ( flag & MM_PROBE_TIMESTAMP )
        {
-               debug_warning("ts : %u:%02u:%02u.%09u\n",  GST_TIME_ARGS(GST_BUFFER_TIMESTAMP(buffer)));
+               debug_log("ts : %u:%02u:%02u.%09u\n",  GST_TIME_ARGS(GST_BUFFER_TIMESTAMP(buffer)));
        }
 
        /* show buffer size */
        if ( flag & MM_PROBE_BUFFERSIZE )
        {
-               debug_warning("buffer size : %ud\n", GST_BUFFER_SIZE(buffer));
+               debug_log("buffer size : %ud\n", GST_BUFFER_SIZE(buffer));
        }
 
        /* show buffer duration */
        if ( flag & MM_PROBE_BUFFER_DURATION )
        {
-               debug_warning("dur : %lld\n", GST_BUFFER_DURATION(buffer));
+               debug_log("dur : %lld\n", GST_BUFFER_DURATION(buffer));
        }
 
        /* show buffer caps */
        if ( flag & MM_PROBE_CAPS )
        {
-               debug_warning("caps : %s\n", gst_caps_to_string(GST_BUFFER_CAPS(buffer)));
+               MMPLAYER_LOG_GST_CAPS_TYPE(GST_BUFFER_CAPS(buffer));
        }
 
        /* drop buffer if flag is on */
        if ( flag & MM_PROBE_DROP_BUFFER )
        {
-               debug_warning("dropping\n");
+               debug_log("dropping\n");
                ret = FALSE;
        }
-               
+
        /* show clock time */
        if ( flag & MM_PROBE_CLOCK_TIME )
        {
@@ -233,7 +266,7 @@ __util_gst_pad_probe(GstPad *pad, GstBuffer *buffer, gpointer u_data)
                if ( clock )
                {
                        now = gst_clock_get_time( clock );
-                       debug_warning("clock time : %" GST_TIME_FORMAT "\n", GST_TIME_ARGS( now ));
+                       debug_log("clock time : %" GST_TIME_FORMAT "\n", GST_TIME_ARGS( now ));
                }
        }
 
@@ -243,7 +276,7 @@ __util_gst_pad_probe(GstPad *pad, GstBuffer *buffer, gpointer u_data)
        return ret;
 }
 
-char** 
+char**
 util_get_cookie_list ( const char *cookies )
 {
        char **cookie_list = NULL;
@@ -253,7 +286,7 @@ util_get_cookie_list ( const char *cookies )
        if ( !cookies || !strlen(cookies) )
                return NULL;
 
-       debug_log("cookies : %d[bytes] - %s \n", strlen(cookies), cookies);
+       secure_debug_log("cookies : %d[bytes] - %s \n", strlen(cookies), cookies);
 
        temp = g_strdup(cookies);
 
@@ -268,7 +301,7 @@ util_get_cookie_list ( const char *cookies )
                if ( cookie_list[i] && strlen(cookie_list[i]) )
                {
                        g_strstrip(cookie_list[i]);
-                       debug_log("cookie_list[%d] : %d[bytes] - %s \n", i, strlen(cookie_list[i]), cookie_list[i]);
+                       secure_debug_log("cookie_list[%d] : %d[bytes] - %s \n", i, strlen(cookie_list[i]), cookie_list[i]);
                }
                else
                {
@@ -287,32 +320,37 @@ bool util_check_valid_url ( const char *proxy )
 {
        struct in_addr proxy_addr;
        bool ret = TRUE;
-       
+
        return_val_if_fail ( proxy, FALSE );
        return_val_if_fail ( strlen(proxy), FALSE );
 
-       if ( inet_aton(proxy, &proxy_addr) != 0 )
+       if ( inet_aton(proxy, &proxy_addr) != 0 )
        {
                debug_warning("invalid proxy is set. \n");
                ret = FALSE;
        }
-          
+
        return ret;
 }
 
 /* check the given path is indicating sdp file */
-bool 
+bool
 util_is_sdp_file ( const char *path )
 {
        gboolean ret = FALSE;
        gchar* uri = NULL;
-       
-       debug_fenter();
-       
+
+       MMPLAYER_FENTER();
+
        return_val_if_fail ( path, FALSE );
 
        uri = g_ascii_strdown ( path, -1 );
 
+       if ( uri == NULL)
+       {
+               return FALSE;
+       }
+
        /* trimming */
        g_strstrip( uri );
 
@@ -330,17 +368,15 @@ util_is_sdp_file ( const char *path )
        if ( ! ret )
        {
                /* FIXIT : do it soon */
-               debug_warning("determining whether it's sdp or not with it's content is not implemented yet. ;)\n");
        }
 
-       if ( uri )
-               g_free( uri); 
+       g_free( uri);
        uri = NULL;
 
        return ret;
 }
 
-int64_t 
+int64_t
 util_get_time ( void )
 {
        struct timeval tv;
@@ -348,16 +384,16 @@ util_get_time ( void )
        return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
 }
 
-int 
+int
 util_get_rank_increase ( const char *factory_class )
 {
        gint rank_pri_inc = 20;
        gint rank_sec_inc = 10;
        gint ret = 0;
 
-       if ( g_strrstr(factory_class,"Dsp") ) 
+       if ( g_strrstr(factory_class,"Dsp") )
                ret = rank_pri_inc;
-       else if ( g_strrstr(factory_class,"HW") ) 
+       else if ( g_strrstr(factory_class,"HW") )
                ret = rank_pri_inc;
        else if ( g_strrstr(factory_class,"Arm") )
                ret = rank_sec_inc;
@@ -365,7 +401,7 @@ util_get_rank_increase ( const char *factory_class )
        return ret;
 }
 
-int 
+int
 util_factory_rank_compare(GstPluginFeature *f1, GstPluginFeature *f2) // @
 {
        const gchar *klass;
@@ -379,3 +415,195 @@ util_factory_rank_compare(GstPluginFeature *f1, GstPluginFeature *f2) // @
 
        return (gst_plugin_feature_get_rank(f2)+f2_rank_inc) - (gst_plugin_feature_get_rank(f1)+f1_rank_inc );
 }
+
+const char*
+util_get_charset(const char *file_path)
+{
+       UCharsetDetector* ucsd;
+       const UCharsetMatch* ucm;
+       UErrorCode status = U_ZERO_ERROR;
+
+       const char* charset = NULL;
+       char *buf = NULL;
+       FILE* fin =0;
+       size_t n_size = 0;
+
+       fin = fopen(file_path, "r");
+       if (!fin)
+       {
+               secure_debug_error("fail to open file %s\n", file_path);
+               return NULL;
+       }
+
+       ucsd = ucsdet_open( &status );
+       if( U_FAILURE(status) ) {
+               debug_error("fail to ucsdet_open\n");
+               goto done;
+       }
+
+       ucsdet_enableInputFilter( ucsd, TRUE );
+
+       buf = g_malloc(READ_MAX_BUFFER_SIZE*READ_MAX_BUFFER_SIZE);
+       if (!buf)
+       {
+               debug_error("fail to alloc\n");
+               goto done;
+       }
+
+       n_size = fread( buf, 1, READ_MAX_BUFFER_SIZE*READ_MAX_BUFFER_SIZE, fin );
+       buf[READ_MAX_BUFFER_SIZE*READ_MAX_BUFFER_SIZE] = '\0';
+       if (!n_size)
+               goto done;
+
+       ucsdet_setText( ucsd, buf, strlen(buf), &status );
+       if( U_FAILURE(status) ) {
+               debug_error("fail to ucsdet_setText\n");
+               goto done;
+       }
+
+       ucm = ucsdet_detect( ucsd, &status );
+       if( U_FAILURE(status) ) {
+               debug_error("fail to ucsdet_detect\n");
+               goto done;
+       }
+
+       charset = ucsdet_getName( ucm, &status );
+       if( U_FAILURE(status) ) {
+               debug_error("fail to ucsdet_getName\n");
+               goto done;
+       }
+
+done:
+       if(fin)
+               fclose(fin);
+
+       if(ucsd)
+               ucsdet_close( ucsd );
+
+       if (buf)
+               g_free(buf);
+
+       return charset;
+}
+
+int
+util_get_is_connected_external_display(void)
+{
+  int is_connected_hdmi = -1;
+  int is_connected_mirroring = -1;
+
+       if (vconf_get_int(VCONFKEY_SYSMAN_HDMI, &is_connected_hdmi))
+               debug_error("[hdmi]vconf_set_int FAIL");
+       if (vconf_get_int(VCONFKEY_SCREEN_MIRRORING_STATE, &is_connected_mirroring))
+               debug_error("[mirroring]vconf_set_int FAIL");
+
+       /* if conneted with external display */
+       if (is_connected_mirroring == VCONFKEY_SCREEN_MIRRORING_CONNECTED) {
+               debug_warning ("connected with mirroring display");
+               return MMPLAYER_DISPLAY_MIRRORING_ACTIVE;
+       }
+       if (is_connected_hdmi == VCONFKEY_SYSMAN_HDMI_CONNECTED) {
+               debug_warning ("connected with external display");
+               return MMPLAYER_DISPLAY_HDMI_ACTIVE;
+       }
+       if ((is_connected_mirroring == VCONFKEY_SCREEN_MIRRORING_ACTIVATED || is_connected_mirroring == VCONFKEY_SCREEN_MIRRORING_DEACTIVATED) && is_connected_hdmi == VCONFKEY_SYSMAN_HDMI_DISCONNECTED) {
+               debug_warning ("non-connected status");
+               return MMPLAYER_DISPLAY_NULL;
+       }
+
+       debug_error ("it is not registered (%d, %d)", is_connected_mirroring, is_connected_hdmi);
+       return -1;
+}
+
+gboolean util_is_miracast_connected(void)
+{
+       int is_connected = 0;
+
+       if (vconf_get_bool(VCONFKEY_MIRACAST_WFD_SOURCE_STATUS, &is_connected) ) {
+               debug_error("failed to get miracast status key");
+               return FALSE;
+       }
+
+       if (VCONFKEY_MIRACAST_WFD_SOURCE_ON == is_connected) {
+               debug_warning("miracast connected");
+               return TRUE;
+       }
+
+       return FALSE;
+}
+
+int util_get_pixtype(unsigned int fourcc)
+{
+       int pixtype = MM_PIXEL_FORMAT_INVALID;
+
+    /*
+       char *pfourcc = (char*)&fourcc;
+
+       debug_log("fourcc(%c%c%c%c)",
+                        pfourcc[0], pfourcc[1], pfourcc[2], pfourcc[3]);
+    */
+
+
+       switch (fourcc) {
+       case GST_MAKE_FOURCC ('S', 'N', '1', '2'):
+       case GST_MAKE_FOURCC ('N', 'V', '1', '2'):
+               pixtype = MM_PIXEL_FORMAT_NV12;
+               break;
+       case GST_MAKE_FOURCC ('S', 'N', '2', '1'):
+       case GST_MAKE_FOURCC ('N', 'V', '2', '1'):
+               pixtype = MM_PIXEL_FORMAT_NV21;
+               break;
+       case GST_MAKE_FOURCC ('S', 'U', 'Y', 'V'):
+       case GST_MAKE_FOURCC ('Y', 'U', 'Y', 'V'):
+       case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
+               pixtype = MM_PIXEL_FORMAT_YUYV;
+               break;
+       case GST_MAKE_FOURCC ('S', 'Y', 'V', 'Y'):
+       case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
+               pixtype = MM_PIXEL_FORMAT_UYVY;
+               break;
+       case GST_MAKE_FOURCC ('S', '4', '2', '0'):
+       case GST_MAKE_FOURCC ('I', '4', '2', '0'):
+               pixtype = MM_PIXEL_FORMAT_I420;
+               break;
+       case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
+               pixtype = MM_PIXEL_FORMAT_YV12;
+               break;
+       case GST_MAKE_FOURCC ('4', '2', '2', 'P'):
+               pixtype = MM_PIXEL_FORMAT_422P;
+               break;
+       case GST_MAKE_FOURCC ('R', 'G', 'B', 'P'):
+               pixtype = MM_PIXEL_FORMAT_RGB565;
+               break;
+       case GST_MAKE_FOURCC ('R', 'G', 'B', '3'):
+               pixtype = MM_PIXEL_FORMAT_RGB888;
+               break;
+       case GST_MAKE_FOURCC ('A', 'R', 'G', 'B'):
+       case GST_MAKE_FOURCC ('x', 'R', 'G', 'B'):
+               pixtype = MM_PIXEL_FORMAT_ARGB;
+               break;
+       case GST_MAKE_FOURCC ('B', 'G', 'R', 'A'):
+       case GST_MAKE_FOURCC ('B', 'G', 'R', 'x'):
+       case GST_MAKE_FOURCC ('S', 'R', '3', '2'):
+               pixtype = MM_PIXEL_FORMAT_RGBA;
+               break;
+       case GST_MAKE_FOURCC ('J', 'P', 'E', 'G'):
+       case GST_MAKE_FOURCC ('P', 'N', 'G', ' '):
+               pixtype = MM_PIXEL_FORMAT_ENCODED;
+               break;
+       /*FIXME - ITLV */
+       case GST_MAKE_FOURCC ('I', 'T', 'L', 'V'):
+               pixtype = MM_PIXEL_FORMAT_ITLV_JPEG_UYVY;
+               break;
+       case GST_MAKE_FOURCC ('S', 'T', '1', '2'):
+               pixtype = MM_PIXEL_FORMAT_NV12T;
+               break;
+       default:
+               debug_error("Not supported fourcc type(%c%c%c%c)",
+                              fourcc, fourcc>>8, fourcc>>16, fourcc>>24);
+               pixtype = MM_PIXEL_FORMAT_INVALID;
+               break;
+       }
+
+       return pixtype;
+}