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