fix prevent issue #453398
[platform/core/multimedia/libmm-player.git] / src / mm_player_utils.c
1 /*
2  * libmm-player
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
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>
8  *
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
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
21  */
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <sys/socket.h>
29 #include <sys/time.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <unicode/ucsdet.h>
33
34 #include <mm_debug.h>
35 #include "mm_player_utils.h"
36
37 /* for getting status of connecting external display */
38 #include <vconf.h>
39 #include <vconf-internal-sysman-keys.h>
40 #include <vconf-internal-wifi-keys.h>
41
42 int util_exist_file_path(const char *file_path)
43 {
44         int fd = 0;
45         struct stat stat_results = {0, };
46
47         if (!file_path || !strlen(file_path))
48                 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
49
50         fd = open (file_path, O_RDONLY);
51
52         if (fd < 0)
53         {
54                 debug_error("failed to open file by %s (%d)", strerror(errno), errno);
55
56                 if (EACCES == errno)
57                         return MM_ERROR_PLAYER_PERMISSION_DENIED;
58
59                 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
60         }
61
62         if (fstat(fd, &stat_results) < 0)
63         {
64                 debug_error("failed to get file status");
65         }
66         else if (stat_results.st_size == 0)
67         {
68                 debug_error("file size is zero");
69                 close(fd);
70                 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
71         }
72         else
73         {
74                 debug_warning("file size : %lld bytes", (long long)stat_results.st_size);
75         }
76
77         close(fd);
78
79         return MM_ERROR_NONE;
80 }
81
82 bool util_write_file_backup(const char *backup_path, char *data_ptr, int data_size)
83 {
84         FILE *fp = NULL;
85         int wsize = 0;
86
87         fp = fopen(backup_path, "wb");
88         if (!fp)
89                 return FALSE;
90
91         wsize = fwrite(data_ptr, sizeof(char), data_size, fp);
92
93         fclose(fp);
94
95         if (wsize != data_size) {
96                 if (!access(backup_path, R_OK))
97                         remove(backup_path);
98
99                 debug_error("No space to write!\n");
100
101                 return FALSE;
102         }
103
104         return TRUE;
105 }
106
107 bool util_remove_file_backup(const char *backup_path)
108 {
109         if (!backup_path || !strlen(backup_path))
110                 return FALSE;
111
112         int res = access(backup_path, R_OK);
113         if (!res)
114         {
115                 if (remove(backup_path) == -1)
116                         return FALSE;
117         }
118
119         return TRUE;
120 }
121
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)
125 {
126         const char *p = (const char *)mem;
127
128         if (size < DETECTION_PREFIX_SIZE)
129                 return MM_AUDIO_CODEC_INVALID;
130
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;
135         }
136
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;
141         }
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;
146         }
147
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;
154         }
155         /* i-melody file detection */
156         if (memcmp(p, "BEGIN:IMELODY", 13) == 0)
157         {
158                 debug_log ("MM_AUDIO_CODEC_IMELODY\n");
159                 return MM_AUDIO_CODEC_IMELODY;
160         }
161
162         return MM_AUDIO_CODEC_INVALID;
163 }
164
165 //bool util_is_midi_type_by_file(const char *file_path)
166 int util_is_midi_type_by_file(const char *file_path)
167 {
168         struct stat file_attrib;
169         FILE *fp = NULL;
170         char prefix[DETECTION_PREFIX_SIZE] = {0,};
171         int size;
172
173         if (!file_path)
174                 return FALSE;
175
176         fp = fopen(file_path, "r");
177
178         if (!fp)
179         return FALSE;
180
181         memset(&file_attrib, 0, sizeof(file_attrib));
182
183         if (stat(file_path, &file_attrib) != 0)
184         {
185                 fclose(fp);
186                 return FALSE;
187         }
188
189         size = (int) file_attrib.st_size;
190
191         if (size < DETECTION_PREFIX_SIZE)
192         {
193                 fclose(fp);
194                 return FALSE;
195         }
196
197         size = fread(prefix, sizeof(char), DETECTION_PREFIX_SIZE, fp);
198
199         fclose(fp);
200
201         return util_is_midi_type_by_mem(prefix, size);
202 }
203
204 char**
205 util_get_cookie_list ( const char *cookies )
206 {
207         char **cookie_list = NULL;
208         char *temp = NULL;
209         gint i = 0;
210
211         if ( !cookies || !strlen(cookies) )
212                 return NULL;
213
214         secure_debug_log("cookies : %d[bytes] - %s \n", strlen(cookies), cookies);
215
216         temp = g_strdup(cookies);
217
218         /* trimming. it works inplace */
219         g_strstrip(temp);
220
221         /* split */
222         cookie_list = g_strsplit(temp, ";", 100);
223
224         for ( i = 0; i < g_strv_length(cookie_list); i++ )
225         {
226                 if ( cookie_list[i] && strlen(cookie_list[i]) )
227                 {
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]);
230                 }
231                 else
232                 {
233                         cookie_list[i][0]='\0';
234                 }
235         }
236
237         if (temp)
238                 g_free (temp);
239         temp=NULL;
240
241         return cookie_list;
242 }
243
244 bool util_check_valid_url ( const char *proxy )
245 {
246         struct in_addr proxy_addr;
247         bool ret = TRUE;
248
249         return_val_if_fail ( proxy, FALSE );
250         return_val_if_fail ( strlen(proxy), FALSE );
251
252         if ( inet_aton(proxy, &proxy_addr) != 0 )
253         {
254                 debug_warning("invalid proxy is set. \n");
255                 ret = FALSE;
256         }
257
258         return ret;
259 }
260
261 /* check the given path is indicating sdp file */
262 bool
263 util_is_sdp_file ( const char *path )
264 {
265         gboolean ret = FALSE;
266         gchar* uri = NULL;
267
268         MMPLAYER_FENTER();
269
270         return_val_if_fail ( path, FALSE );
271
272         uri = g_ascii_strdown ( path, -1 );
273
274         if ( uri == NULL)
275         {
276                 return FALSE;
277         }
278
279         /* trimming */
280         g_strstrip( uri );
281
282         /* strlen(".sdp") == 4 */
283         if ( strlen( uri ) <= 4 )
284         {
285                 debug_warning ( "path is too short.\n" );
286                 return ret;
287         }
288
289         /* first, check extension name */
290         ret = g_str_has_suffix ( uri, "sdp" );
291
292         /* second, if no suffix is there, check it's contents */
293         if ( ! ret )
294         {
295                 /* FIXIT : do it soon */
296         }
297
298         g_free( uri);
299         uri = NULL;
300
301         return ret;
302 }
303
304 int64_t
305 util_get_time ( void )
306 {
307         struct timeval tv;
308         gettimeofday(&tv,NULL);
309         return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
310 }
311
312 int
313 util_get_rank_increase ( const char *factory_class )
314 {
315         gint rank_pri_inc = 20;
316         gint rank_sec_inc = 10;
317         gint ret = 0;
318
319         if ( g_strrstr(factory_class,"Dsp") )
320                 ret = rank_pri_inc;
321         else if ( g_strrstr(factory_class,"HW") )
322                 ret = rank_pri_inc;
323         else if ( g_strrstr(factory_class,"Arm") )
324                 ret = rank_sec_inc;
325
326         return ret;
327 }
328
329 int
330 util_factory_rank_compare(GstPluginFeature *f1, GstPluginFeature *f2) // @
331 {
332         const gchar *klass;
333         int f1_rank_inc=0, f2_rank_inc=0;
334
335         klass = gst_element_factory_get_klass(GST_ELEMENT_FACTORY(f1));
336         f1_rank_inc = util_get_rank_increase ( klass );
337
338         klass = gst_element_factory_get_klass(GST_ELEMENT_FACTORY(f2));
339         f2_rank_inc = util_get_rank_increase ( klass );
340
341         return (gst_plugin_feature_get_rank(f2)+f2_rank_inc) - (gst_plugin_feature_get_rank(f1)+f1_rank_inc );
342 }
343
344 const char*
345 util_get_charset(const char *file_path)
346 {
347         UCharsetDetector* ucsd;
348         const UCharsetMatch* ucm;
349         UErrorCode status = U_ZERO_ERROR;
350
351         const char* charset = NULL;
352         char *buf = NULL;
353         FILE* fin =0;
354         size_t n_size = 0;
355
356         fin = fopen(file_path, "r");
357         if (!fin)
358         {
359                 secure_debug_error("fail to open file %s\n", file_path);
360                 return NULL;
361         }
362
363         ucsd = ucsdet_open( &status );
364         if( U_FAILURE(status) ) {
365                 debug_error("fail to ucsdet_open\n");
366                 goto done;
367         }
368
369         ucsdet_enableInputFilter( ucsd, TRUE );
370
371         buf = g_malloc(1024*1024);
372         if (!buf)
373         {
374                 debug_error("fail to alloc\n");
375                 goto done;
376         }
377
378         n_size = fread( buf, 1, 1024*1024, fin );
379
380         if (!n_size)
381                 goto done;
382
383         ucsdet_setText( ucsd, buf, strlen(buf), &status );
384         if( U_FAILURE(status) ) {
385                 debug_error("fail to ucsdet_setText\n");
386                 goto done;
387         }
388
389         ucm = ucsdet_detect( ucsd, &status );
390         if( U_FAILURE(status) ) {
391                 debug_error("fail to ucsdet_detect\n");
392                 goto done;
393         }
394
395         charset = ucsdet_getName( ucm, &status );
396         if( U_FAILURE(status) ) {
397                 debug_error("fail to ucsdet_getName\n");
398                 goto done;
399         }
400
401 done:
402         if(fin)
403                 fclose(fin);
404
405         if(ucsd)
406                 ucsdet_close( ucsd );
407
408         if (buf)
409                 g_free(buf);
410
411         return charset;
412 }
413
414 int
415 util_get_is_connected_external_display(void)
416 {
417   int is_connected_hdmi = -1;
418   int is_connected_mirroring = -1;
419
420 #if 0
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");
425
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;
430         }
431         if (is_connected_hdmi == VCONFKEY_SYSMAN_HDMI_CONNECTED) {
432                 debug_warning ("connected with external display");
433                 return MMPLAYER_DISPLAY_HDMI_ACTIVE;
434         }
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;
438         }
439 #endif
440         debug_error ("it is not registered (%d, %d)", is_connected_mirroring, is_connected_hdmi);
441         return -1;
442 }
443
444 int util_get_pixtype(unsigned int fourcc)
445 {
446         int pixtype = MM_PIXEL_FORMAT_INVALID;
447
448     /*
449         char *pfourcc = (char*)&fourcc;
450
451         debug_log("fourcc(%c%c%c%c)",
452                          pfourcc[0], pfourcc[1], pfourcc[2], pfourcc[3]);
453     */
454
455
456         switch (fourcc) {
457         case GST_MAKE_FOURCC ('S', 'N', '1', '2'):
458         case GST_MAKE_FOURCC ('N', 'V', '1', '2'):
459                 pixtype = MM_PIXEL_FORMAT_NV12;
460                 break;
461         case GST_MAKE_FOURCC ('S', 'T', '1', '2'):
462                 pixtype = MM_PIXEL_FORMAT_NV12T;
463                 break;
464         case GST_MAKE_FOURCC ('S', 'N', '2', '1'):
465         case GST_MAKE_FOURCC ('N', 'V', '2', '1'):
466                 pixtype = MM_PIXEL_FORMAT_NV21;
467                 break;
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;
472                 break;
473         case GST_MAKE_FOURCC ('S', 'Y', 'V', 'Y'):
474         case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
475                 pixtype = MM_PIXEL_FORMAT_UYVY;
476                 break;
477         case GST_MAKE_FOURCC ('S', '4', '2', '0'):
478         case GST_MAKE_FOURCC ('I', '4', '2', '0'):
479                 pixtype = MM_PIXEL_FORMAT_I420;
480                 break;
481         case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
482                 pixtype = MM_PIXEL_FORMAT_YV12;
483                 break;
484         case GST_MAKE_FOURCC ('4', '2', '2', 'P'):
485                 pixtype = MM_PIXEL_FORMAT_422P;
486                 break;
487         case GST_MAKE_FOURCC ('R', 'G', 'B', 'P'):
488                 pixtype = MM_PIXEL_FORMAT_RGB565;
489                 break;
490         case GST_MAKE_FOURCC ('R', 'G', 'B', '3'):
491                 pixtype = MM_PIXEL_FORMAT_RGB888;
492                 break;
493         case GST_MAKE_FOURCC ('A', 'R', 'G', 'B'):
494         case GST_MAKE_FOURCC ('x', 'R', 'G', 'B'):
495                 pixtype = MM_PIXEL_FORMAT_ARGB;
496                 break;
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;
501                 break;
502         case GST_MAKE_FOURCC ('J', 'P', 'E', 'G'):
503         case GST_MAKE_FOURCC ('P', 'N', 'G', ' '):
504                 pixtype = MM_PIXEL_FORMAT_ENCODED;
505                 break;
506         /*FIXME*/
507         case GST_MAKE_FOURCC ('I', 'T', 'L', 'V'):
508                 pixtype = MM_PIXEL_FORMAT_ITLV_JPEG_UYVY;
509                 break;
510         default:
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;
514                 break;
515         }
516
517         return pixtype;
518 }