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