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