4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: JongHyuk Choi <jhchoi.choi@samsung.com>, YeJin Cho <cho.yejin@samsung.com>, YoungHwan An <younghwan_.an@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
30 #include "mm_player_utils.h"
32 bool util_exist_file_path(const char *file_path)
36 if (!file_path || !strlen(file_path))
39 int res = access(file_path, R_OK);
46 bool util_write_file_backup(const char *backup_path, char *data_ptr, int data_size)
53 fp = fopen(backup_path, "wb");
57 wsize = fwrite(data_ptr, sizeof(char), data_size, fp);
61 if (wsize != data_size) {
62 if (!access(backup_path, R_OK))
65 debug_error("No space to write!\n");
73 #if 1 //tskim:MidiModuleRequires:+: for Midi player
74 bool util_remove_file_backup(const char *backup_path)
78 if (!backup_path || !strlen(backup_path))
82 Prevent defect patch. CID:22389 Checker:TOTCU
83 int res = access(backup_path, R_OK);
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)
98 const char *p = (const char *)mem;
100 if (size < DETECTION_PREFIX_SIZE)
101 return MM_AUDIO_CODEC_INVALID; //FALSE; // sbs:+:080903
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; // TRUE;// sbs:+:080903
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;//TRUE;// sbs:+:080903
114 /* mxmf file detection */ // sbs:+:080903
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;
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;//TRUE;// sbs:+:080903
127 /* i-melody file detection */ // sbs:+:080903
128 if (memcmp(p, "BEGIN:IMELODY", 13) == 0)
130 debug_log ("MM_AUDIO_CODEC_IMELODY\n");
131 return MM_AUDIO_CODEC_IMELODY;
134 return MM_AUDIO_CODEC_INVALID;//FALSE; // sbs:+:080903
137 //bool util_is_midi_type_by_file(const char *file_path)
138 int util_is_midi_type_by_file(const char *file_path)
142 struct stat file_attrib;
144 char prefix[DETECTION_PREFIX_SIZE] = {0,};
150 /* Prevent defect patch. CID: 22388 Checker: TOCTOU */
151 fp = fopen(file_path, "r");
156 memset(&file_attrib, 0, sizeof(file_attrib));
158 if (stat(file_path, &file_attrib) != 0)
164 size = (int) file_attrib.st_size;
166 if (size < DETECTION_PREFIX_SIZE)
172 size = fread(prefix, sizeof(char), DETECTION_PREFIX_SIZE, fp);
176 return util_is_midi_type_by_mem(prefix, size);
179 /* messages are treated as warnings bcz those code should not be checked in.
180 * and no error handling will supported for same manner.
183 __util_gst_pad_probe(GstPad *pad, GstBuffer *buffer, gpointer u_data)
185 gint flag = (gint) u_data;
186 GstElement* parent = NULL;
189 /* show name as default */
190 parent = (GstElement*)gst_object_get_parent(GST_OBJECT(pad));
191 debug_warning("PAD PROBE : %s:%s\n", GST_ELEMENT_NAME(parent), GST_PAD_NAME(pad));
193 /* show time stamp */
194 if ( flag & MM_PROBE_TIMESTAMP )
196 debug_warning("ts : %u:%02u:%02u.%09u\n", GST_TIME_ARGS(GST_BUFFER_TIMESTAMP(buffer)));
199 /* show buffer size */
200 if ( flag & MM_PROBE_BUFFERSIZE )
202 debug_warning("buffer size : %ud\n", GST_BUFFER_SIZE(buffer));
205 /* show buffer duration */
206 if ( flag & MM_PROBE_BUFFER_DURATION )
208 debug_warning("dur : %lld\n", GST_BUFFER_DURATION(buffer));
211 /* show buffer caps */
212 if ( flag & MM_PROBE_CAPS )
214 debug_warning("caps : %s\n", gst_caps_to_string(GST_BUFFER_CAPS(buffer)));
217 /* drop buffer if flag is on */
218 if ( flag & MM_PROBE_DROP_BUFFER )
220 debug_warning("dropping\n");
224 /* show clock time */
225 if ( flag & MM_PROBE_CLOCK_TIME )
227 GstClock* clock = NULL;
228 GstClockTime now = GST_CLOCK_TIME_NONE;
230 clock = GST_ELEMENT_CLOCK ( parent );
234 now = gst_clock_get_time( clock );
235 debug_warning("clock time : %" GST_TIME_FORMAT "\n", GST_TIME_ARGS( now ));
240 gst_object_unref(parent);
246 util_get_cookie_list ( const char *cookies )
248 char **cookie_list = NULL;
252 if ( !cookies || !strlen(cookies) )
255 debug_log("cookies : %d[bytes] - %s \n", strlen(cookies), cookies);
257 temp = g_strdup(cookies);
259 /* trimming. it works inplace */
263 cookie_list = g_strsplit(temp, ";", 100);
265 for ( i = 0; i < g_strv_length(cookie_list); i++ )
267 if ( cookie_list[i] && strlen(cookie_list[i]) )
269 g_strstrip(cookie_list[i]);
270 debug_log("cookie_list[%d] : %d[bytes] - %s \n", i, strlen(cookie_list[i]), cookie_list[i]);
274 cookie_list[i][0]='\0';
285 bool util_check_valid_url ( const char *proxy )
287 struct in_addr proxy_addr;
290 return_val_if_fail ( proxy, FALSE );
291 return_val_if_fail ( strlen(proxy), FALSE );
293 if ( inet_aton(proxy, &proxy_addr) != 0 )
295 debug_warning("invalid proxy is set. \n");
302 /* check the given path is indicating sdp file */
304 util_is_sdp_file ( const char *path )
306 gboolean ret = FALSE;
311 return_val_if_fail ( path, FALSE );
313 uri = g_ascii_strdown ( path, -1 );
318 /* strlen(".sdp") == 4 */
319 if ( strlen( uri ) <= 4 )
321 debug_warning ( "path is too short.\n" );
325 /* first, check extension name */
326 ret = g_str_has_suffix ( uri, "sdp" );
328 /* second, if no suffix is there, check it's contents */
331 /* FIXIT : do it soon */
332 debug_warning("determining whether it's sdp or not with it's content is not implemented yet. ;)\n");
343 util_get_time ( void )
346 gettimeofday(&tv,NULL);
347 return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
351 util_get_rank_increase ( const char *factory_class )
353 gint rank_pri_inc = 20;
354 gint rank_sec_inc = 10;
357 if ( g_strrstr(factory_class,"Dsp") )
359 else if ( g_strrstr(factory_class,"HW") )
361 else if ( g_strrstr(factory_class,"Arm") )
368 util_factory_rank_compare(GstPluginFeature *f1, GstPluginFeature *f2) // @
371 int f1_rank_inc=0, f2_rank_inc=0;
373 klass = gst_element_factory_get_klass(GST_ELEMENT_FACTORY(f1));
374 f1_rank_inc = util_get_rank_increase ( klass );
376 klass = gst_element_factory_get_klass(GST_ELEMENT_FACTORY(f2));
377 f2_rank_inc = util_get_rank_increase ( klass );
379 return (gst_plugin_feature_get_rank(f2)+f2_rank_inc) - (gst_plugin_feature_get_rank(f1)+f1_rank_inc );