fd2c18e8f74b40cb44395f1f14dfc512dc427eb3
[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>, YoungHwan An <younghwan_.an@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <sys/stat.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
29 #include <unicode/ucsdet.h>
30
31 #include <mm_debug.h>
32 #include "mm_player_utils.h"
33
34 bool util_exist_file_path(const char *file_path)
35 {
36         debug_log("\n");
37
38         if (!file_path || !strlen(file_path))
39                 return FALSE;
40
41         int res = access(file_path, R_OK);
42         if (res)
43                 return FALSE;
44
45         return TRUE;
46 }
47
48 bool util_write_file_backup(const char *backup_path, char *data_ptr, int data_size)
49 {
50         debug_log("\n");
51
52         FILE *fp = NULL;
53         int wsize = 0;
54
55         fp = fopen(backup_path, "wb");
56         if (!fp)
57                 return FALSE;
58
59         wsize = fwrite(data_ptr, sizeof(char), data_size, fp);
60
61         fclose(fp);
62
63         if (wsize != data_size) {
64                 if (!access(backup_path, R_OK))
65                         remove(backup_path);
66
67                 debug_error("No space to write!\n");
68
69                 return FALSE;
70         }
71
72         return TRUE;
73 }
74
75 bool util_remove_file_backup(const char *backup_path)
76 {
77         debug_log("\n");
78
79         if (!backup_path || !strlen(backup_path))
80                 return FALSE;
81
82         int res = access(backup_path, R_OK);
83         if (!res)
84                 remove(backup_path);
85
86         return TRUE;
87 }
88
89 #define DETECTION_PREFIX_SIZE   20
90 //bool util_is_midi_type_by_mem(void *mem, int size)
91 int util_is_midi_type_by_mem(void *mem, int size)
92 {
93         debug_log("\n");
94         
95         const char *p = (const char *)mem;
96
97         if (size < DETECTION_PREFIX_SIZE)
98                 return MM_AUDIO_CODEC_INVALID;
99
100         /* mmf file detection */
101         if (p[0] == 'M' && p[1] == 'M' && p[2] == 'M' && p[3] == 'D') {
102                 debug_log("MM_AUDIO_CODEC_MMF\n");
103                 return MM_AUDIO_CODEC_MMF;
104         }
105
106         /* midi file detection */
107         if (p[0] == 'M' && p[1] == 'T' && p[2] == 'h' && p[3] == 'd') {
108                 debug_log ("MM_AUDIO_CODEC_MIDI, %d\n", MM_AUDIO_CODEC_MIDI);
109                 return MM_AUDIO_CODEC_MIDI;
110         }
111         /* mxmf file detection */
112         if (p[0] == 'X' && p[1] == 'M' && p[2] == 'F' && p[3] == '_') {
113                 debug_log ("MM_AUDIO_CODEC_MXMF\n");
114                 return MM_AUDIO_CODEC_MXMF;
115         }
116
117         /* wave file detection */
118         if (p[0] == 'R' && p[1] == 'I' && p[2] == 'F' && p[3] == 'F' &&
119                 p[8] == 'W' && p[9] == 'A' && p[10] == 'V' && p[11] == 'E' &&
120                 p[12] == 'f' && p[13] == 'm' && p[14] == 't') {
121                 debug_log ("MM_AUDIO_CODEC_WAVE\n");
122                 return MM_AUDIO_CODEC_WAVE;
123         }
124         /* i-melody file detection */
125         if (memcmp(p, "BEGIN:IMELODY", 13) == 0)
126         {
127                 debug_log ("MM_AUDIO_CODEC_IMELODY\n");
128                 return MM_AUDIO_CODEC_IMELODY;
129         }
130
131         return MM_AUDIO_CODEC_INVALID;
132 }
133
134 //bool util_is_midi_type_by_file(const char *file_path)
135 int util_is_midi_type_by_file(const char *file_path)
136 {
137         debug_log("\n");
138         
139         struct stat file_attrib;
140         FILE *fp = NULL;
141         char prefix[DETECTION_PREFIX_SIZE] = {0,};
142         int size;
143
144         if (!file_path)
145                 return FALSE;
146
147         fp = fopen(file_path, "r");
148
149         if (!fp)
150         return FALSE;
151
152         memset(&file_attrib, 0, sizeof(file_attrib));
153
154         if (stat(file_path, &file_attrib) != 0)
155         {
156                 fclose(fp);
157                 return FALSE;
158         }
159
160         size = (int) file_attrib.st_size;
161
162         if (size < DETECTION_PREFIX_SIZE)
163         {
164                 fclose(fp);
165                 return FALSE;
166         }
167         
168         size = fread(prefix, sizeof(char), DETECTION_PREFIX_SIZE, fp);
169
170         fclose(fp);
171
172         return util_is_midi_type_by_mem(prefix, size);
173 }
174
175 /* messages are treated as warnings bcz those code should not be checked in. 
176  * and no error handling will supported for same manner. 
177  */
178 gboolean 
179 __util_gst_pad_probe(GstPad *pad, GstBuffer *buffer, gpointer u_data)
180 {
181         gint flag = (gint) u_data;
182         GstElement* parent = NULL;
183         gboolean ret = TRUE;
184         
185         /* show name as default */
186         parent = (GstElement*)gst_object_get_parent(GST_OBJECT(pad));
187         debug_warning("PAD PROBE : %s:%s\n", GST_ELEMENT_NAME(parent), GST_PAD_NAME(pad));
188         
189         /* show time stamp */
190         if ( flag & MM_PROBE_TIMESTAMP )
191         {
192                 debug_warning("ts : %u:%02u:%02u.%09u\n",  GST_TIME_ARGS(GST_BUFFER_TIMESTAMP(buffer)));
193         }
194
195         /* show buffer size */
196         if ( flag & MM_PROBE_BUFFERSIZE )
197         {
198                 debug_warning("buffer size : %ud\n", GST_BUFFER_SIZE(buffer));
199         }
200
201         /* show buffer duration */
202         if ( flag & MM_PROBE_BUFFER_DURATION )
203         {
204                 debug_warning("dur : %lld\n", GST_BUFFER_DURATION(buffer));
205         }
206
207         /* show buffer caps */
208         if ( flag & MM_PROBE_CAPS )
209         {
210                 debug_warning("caps : %s\n", gst_caps_to_string(GST_BUFFER_CAPS(buffer)));
211         }
212
213         /* drop buffer if flag is on */
214         if ( flag & MM_PROBE_DROP_BUFFER )
215         {
216                 debug_warning("dropping\n");
217                 ret = FALSE;
218         }
219                 
220         /* show clock time */
221         if ( flag & MM_PROBE_CLOCK_TIME )
222         {
223                 GstClock* clock = NULL;
224                 GstClockTime now = GST_CLOCK_TIME_NONE;
225
226                 clock = GST_ELEMENT_CLOCK ( parent );
227
228                 if ( clock )
229                 {
230                         now = gst_clock_get_time( clock );
231                         debug_warning("clock time : %" GST_TIME_FORMAT "\n", GST_TIME_ARGS( now ));
232                 }
233         }
234
235         if ( parent )
236                 gst_object_unref(parent);
237
238         return ret;
239 }
240
241 char** 
242 util_get_cookie_list ( const char *cookies )
243 {
244         char **cookie_list = NULL;
245         char *temp = NULL;
246         gint i = 0;
247
248         if ( !cookies || !strlen(cookies) )
249                 return NULL;
250
251         debug_log("cookies : %d[bytes] - %s \n", strlen(cookies), cookies);
252
253         temp = g_strdup(cookies);
254
255         /* trimming. it works inplace */
256         g_strstrip(temp);
257
258         /* split */
259         cookie_list = g_strsplit(temp, ";", 100);
260
261         for ( i = 0; i < g_strv_length(cookie_list); i++ )
262         {
263                 if ( cookie_list[i] && strlen(cookie_list[i]) )
264                 {
265                         g_strstrip(cookie_list[i]);
266                         debug_log("cookie_list[%d] : %d[bytes] - %s \n", i, strlen(cookie_list[i]), cookie_list[i]);
267                 }
268                 else
269                 {
270                         cookie_list[i][0]='\0';
271                 }
272         }
273
274         if (temp)
275                 g_free (temp);
276         temp=NULL;
277
278         return cookie_list;
279 }
280
281 bool util_check_valid_url ( const char *proxy )
282 {
283         struct in_addr proxy_addr;
284         bool ret = TRUE;
285         
286         return_val_if_fail ( proxy, FALSE );
287         return_val_if_fail ( strlen(proxy), FALSE );
288
289        if ( inet_aton(proxy, &proxy_addr) != 0 )
290         {
291                 debug_warning("invalid proxy is set. \n");
292                 ret = FALSE;
293         }
294            
295         return ret;
296 }
297
298 /* check the given path is indicating sdp file */
299 bool 
300 util_is_sdp_file ( const char *path )
301 {
302         gboolean ret = FALSE;
303         gchar* uri = NULL;
304         
305         debug_fenter();
306         
307         return_val_if_fail ( path, FALSE );
308
309         uri = g_ascii_strdown ( path, -1 );
310
311         if ( uri == -1)
312         {
313                 return FALSE;
314         }
315
316         /* trimming */
317         g_strstrip( uri );
318
319         /* strlen(".sdp") == 4 */
320         if ( strlen( uri ) <= 4 )
321         {
322                 debug_warning ( "path is too short.\n" );
323                 return ret;
324         }
325
326         /* first, check extension name */
327         ret = g_str_has_suffix ( uri, "sdp" );
328
329         /* second, if no suffix is there, check it's contents */
330         if ( ! ret )
331         {
332                 /* FIXIT : do it soon */
333                 debug_warning("determining whether it's sdp or not with it's content is not implemented yet. ;)\n");
334         }
335
336         g_free( uri);
337         uri = NULL;
338
339         return ret;
340 }
341
342 int64_t 
343 util_get_time ( void )
344 {
345         struct timeval tv;
346         gettimeofday(&tv,NULL);
347         return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
348 }
349
350 int 
351 util_get_rank_increase ( const char *factory_class )
352 {
353         gint rank_pri_inc = 20;
354         gint rank_sec_inc = 10;
355         gint ret = 0;
356
357         if ( g_strrstr(factory_class,"Dsp") ) 
358                 ret = rank_pri_inc;
359         else if ( g_strrstr(factory_class,"HW") ) 
360                 ret = rank_pri_inc;
361         else if ( g_strrstr(factory_class,"Arm") )
362                 ret = rank_sec_inc;
363
364         return ret;
365 }
366
367 int 
368 util_factory_rank_compare(GstPluginFeature *f1, GstPluginFeature *f2) // @
369 {
370         const gchar *klass;
371         int f1_rank_inc=0, f2_rank_inc=0;
372
373         klass = gst_element_factory_get_klass(GST_ELEMENT_FACTORY(f1));
374         f1_rank_inc = util_get_rank_increase ( klass );
375
376         klass = gst_element_factory_get_klass(GST_ELEMENT_FACTORY(f2));
377         f2_rank_inc = util_get_rank_increase ( klass );
378
379         return (gst_plugin_feature_get_rank(f2)+f2_rank_inc) - (gst_plugin_feature_get_rank(f1)+f1_rank_inc );
380 }
381
382 char*
383 util_get_charset(const char *file_path)
384 {
385         UCharsetDetector* ucsd;
386         const UCharsetMatch* ucm;
387         UErrorCode status = U_ZERO_ERROR;
388
389         const char* charset = NULL;
390         char *buf = NULL;
391         FILE* fin;
392
393         fin = fopen(file_path, "r");
394         if (!fin)
395         {
396                 debug_error("fail to open file %s\n", file_path);
397                 return NULL;
398         }
399
400         ucsd = ucsdet_open( &status );
401         if( U_FAILURE(status) ) {
402                 debug_error("fail to ucsdet_open\n");
403                 return NULL;
404         }
405
406         ucsdet_enableInputFilter( ucsd, TRUE );
407
408         buf = g_malloc(1024*1024);
409         if (!buf)
410         {
411                 debug_error("fail to alloc\n");
412                 goto done;
413         }
414
415         fread( buf, 1, 1024*1024, fin );
416         fclose(fin);
417
418         ucsdet_setText( ucsd, buf, strlen(buf), &status );
419         if( U_FAILURE(status) ) {
420                 debug_error("fail to ucsdet_setText\n");
421                 goto done;
422         }
423
424         ucm = ucsdet_detect( ucsd, &status );
425         if( U_FAILURE(status) ) {
426                 debug_error("fail to ucsdet_detect\n");
427                 goto done;
428         }
429
430         charset = ucsdet_getName( ucm, &status );
431         if( U_FAILURE(status) ) {
432                 debug_error("fail to ucsdet_getName\n");
433                 goto done;
434         }
435
436 done:
437         ucsdet_close( ucsd );
438
439         if (buf)
440                 g_free(buf);
441
442         return charset;
443 }
444