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>,
7 * Seungbae Shin <seungbae.shin@samsung.com>, YoungHwan An <younghwan_.an@samsung.com>
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
31 #include <unicode/ucsdet.h>
34 #include "mm_player_utils.h"
36 int util_exist_file_path(const char *file_path)
39 struct stat stat_results = {0, };
41 if (!file_path || !strlen(file_path))
42 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
44 fd = open(file_path, O_RDONLY);
48 strerror_r(errno, str_error, sizeof(str_error));
49 LOGE("failed to open file by %s (%d)", str_error, errno);
52 return MM_ERROR_PLAYER_PERMISSION_DENIED;
54 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
57 if (fstat(fd, &stat_results) < 0) {
58 LOGE("failed to get file status");
59 } else if (stat_results.st_size == 0) {
60 LOGE("file size is zero");
62 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
64 LOGW("file size : %lld bytes", (long long)stat_results.st_size);
71 bool util_write_file_backup(const char *backup_path, char *data_ptr, int data_size)
76 fp = fopen(backup_path, "wb");
80 wsize = fwrite(data_ptr, sizeof(char), data_size, fp);
84 if (wsize != data_size) {
85 if (!access(backup_path, R_OK))
88 LOGE("No space to write!\n");
96 bool util_remove_file_backup(const char *backup_path)
98 if (!backup_path || !strlen(backup_path))
101 int res = access(backup_path, R_OK);
103 if (remove(backup_path) == -1)
110 #define DETECTION_PREFIX_SIZE 20
111 int util_is_midi_type_by_mem(void *mem, int size)
113 const char *p = (const char *)mem;
115 if (size < DETECTION_PREFIX_SIZE)
116 return MM_AUDIO_CODEC_INVALID;
118 /* mmf file detection */
119 if (p[0] == 'M' && p[1] == 'M' && p[2] == 'M' && p[3] == 'D') {
120 LOGD("MM_AUDIO_CODEC_MMF\n");
121 return MM_AUDIO_CODEC_MMF;
124 /* midi file detection */
125 if (p[0] == 'M' && p[1] == 'T' && p[2] == 'h' && p[3] == 'd') {
126 LOGD("MM_AUDIO_CODEC_MIDI, %d\n", MM_AUDIO_CODEC_MIDI);
127 return MM_AUDIO_CODEC_MIDI;
129 /* mxmf file detection */
130 if (p[0] == 'X' && p[1] == 'M' && p[2] == 'F' && p[3] == '_') {
131 LOGD("MM_AUDIO_CODEC_MXMF\n");
132 return MM_AUDIO_CODEC_MXMF;
135 /* wave file detection */
136 if (p[0] == 'R' && p[1] == 'I' && p[2] == 'F' && p[3] == 'F' &&
137 p[8] == 'W' && p[9] == 'A' && p[10] == 'V' && p[11] == 'E' &&
138 p[12] == 'f' && p[13] == 'm' && p[14] == 't') {
139 LOGD("MM_AUDIO_CODEC_WAVE\n");
140 return MM_AUDIO_CODEC_WAVE;
142 /* i-melody file detection */
143 if (memcmp(p, "BEGIN:IMELODY", 13) == 0) {
144 LOGD("MM_AUDIO_CODEC_IMELODY\n");
145 return MM_AUDIO_CODEC_IMELODY;
148 return MM_AUDIO_CODEC_INVALID;
151 int util_is_midi_type_by_file(const char *file_path)
153 struct stat file_attrib;
155 char prefix[DETECTION_PREFIX_SIZE] = {0,};
161 fp = fopen(file_path, "r");
166 memset(&file_attrib, 0, sizeof(file_attrib));
168 if (stat(file_path, &file_attrib) != 0) {
173 size = (int) file_attrib.st_size;
175 if (size < DETECTION_PREFIX_SIZE) {
180 size = fread(prefix, sizeof(char), DETECTION_PREFIX_SIZE, fp);
184 return util_is_midi_type_by_mem(prefix, size);
188 util_get_cookie_list(const char *cookies)
190 char **cookie_list = NULL;
194 if (!cookies || !strlen(cookies))
197 SECURE_LOGD("cookies : %d[bytes] - %s \n", strlen(cookies), cookies);
199 temp = g_strdup(cookies);
201 /* trimming. it works inplace */
205 cookie_list = g_strsplit(temp, ";", 100);
207 for (i = 0; i < g_strv_length(cookie_list); i++) {
208 if (cookie_list[i]) {
209 if (strlen(cookie_list[i])) {
210 g_strstrip(cookie_list[i]);
211 SECURE_LOGD("cookie_list[%d] : %d[bytes] - %s \n", i, strlen(cookie_list[i]), cookie_list[i]);
213 cookie_list[i][0] = '\0';
224 bool util_check_valid_url(const char *proxy)
226 struct in_addr proxy_addr;
229 MMPLAYER_RETURN_VAL_IF_FAIL(proxy, FALSE);
230 MMPLAYER_RETURN_VAL_IF_FAIL(strlen(proxy), FALSE);
232 if (inet_aton(proxy, &proxy_addr) != 0) {
233 LOGW("invalid proxy is set. \n");
240 /* check the given path is indicating sdp file */
242 util_is_sdp_file(const char *path)
244 gboolean ret = FALSE;
249 MMPLAYER_RETURN_VAL_IF_FAIL(path, FALSE);
251 uri = g_ascii_strdown(path, -1);
259 /* strlen(".sdp") == 4 */
260 if (strlen(uri) <= 4) {
261 LOGW("path is too short.\n");
265 /* first, check extension name */
266 ret = g_str_has_suffix(uri, "sdp");
268 /* second, if no suffix is there, check it's contents */
270 /* FIXIT : do it soon */
283 gettimeofday(&tv, NULL);
284 return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
288 util_get_rank_increase(const char *factory_class)
290 gint rank_pri_inc = 20;
291 gint rank_sec_inc = 10;
294 if (g_strrstr(factory_class, "Dsp"))
296 else if (g_strrstr(factory_class, "HW"))
298 else if (g_strrstr(factory_class, "Arm"))
305 util_factory_rank_compare(GstPluginFeature *f1, GstPluginFeature *f2)
308 int f1_rank_inc = 0, f2_rank_inc = 0;
310 klass = gst_element_factory_get_klass(GST_ELEMENT_FACTORY(f1));
311 f1_rank_inc = util_get_rank_increase(klass);
313 klass = gst_element_factory_get_klass(GST_ELEMENT_FACTORY(f2));
314 f2_rank_inc = util_get_rank_increase(klass);
316 return (gst_plugin_feature_get_rank(f2)+f2_rank_inc) - (gst_plugin_feature_get_rank(f1)+f1_rank_inc);
320 util_get_charset(const char *file_path)
322 UCharsetDetector* ucsd;
323 const UCharsetMatch* ucm;
324 UErrorCode status = U_ZERO_ERROR;
326 const char* charset = NULL;
331 fin = fopen(file_path, "r");
333 SECURE_LOGE("fail to open file %s\n", file_path);
337 ucsd = ucsdet_open(&status);
338 if (U_FAILURE(status)) {
339 LOGE("fail to ucsdet_open\n");
343 ucsdet_enableInputFilter(ucsd, TRUE);
345 buf = g_malloc(1024*1024);
347 LOGE("fail to alloc\n");
351 n_size = fread(buf, 1, 1024*1024, fin);
356 ucsdet_setText(ucsd, buf, strlen(buf), &status);
357 if (U_FAILURE(status)) {
358 LOGE("fail to ucsdet_setText\n");
362 ucm = ucsdet_detect(ucsd, &status);
363 if (U_FAILURE(status)) {
364 LOGE("fail to ucsdet_detect\n");
368 charset = ucsdet_getName(ucm, &status);
369 if (U_FAILURE(status)) {
370 LOGE("fail to ucsdet_getName\n");
374 /* CP949 encoding is an extension of the EUC-KR and it is backwards compatible.*/
375 if (charset && !strcmp(charset, "EUC-KR"))
391 int util_get_pixtype(unsigned int fourcc)
393 int pixtype = MM_PIXEL_FORMAT_INVALID;
396 char *pfourcc = (char*)&fourcc;
398 LOGD("fourcc(%c%c%c%c)",
399 pfourcc[0], pfourcc[1], pfourcc[2], pfourcc[3]);
403 case GST_MAKE_FOURCC('S', 'N', '1', '2'):
404 case GST_MAKE_FOURCC('N', 'V', '1', '2'):
405 pixtype = MM_PIXEL_FORMAT_NV12;
407 case GST_MAKE_FOURCC('S', 'T', '1', '2'):
408 pixtype = MM_PIXEL_FORMAT_NV12T;
410 case GST_MAKE_FOURCC('S', 'N', '2', '1'):
411 case GST_MAKE_FOURCC('N', 'V', '2', '1'):
412 pixtype = MM_PIXEL_FORMAT_NV21;
414 case GST_MAKE_FOURCC('S', 'U', 'Y', 'V'):
415 case GST_MAKE_FOURCC('Y', 'U', 'Y', 'V'):
416 case GST_MAKE_FOURCC('Y', 'U', 'Y', '2'):
417 pixtype = MM_PIXEL_FORMAT_YUYV;
419 case GST_MAKE_FOURCC('S', 'Y', 'V', 'Y'):
420 case GST_MAKE_FOURCC('U', 'Y', 'V', 'Y'):
421 pixtype = MM_PIXEL_FORMAT_UYVY;
423 case GST_MAKE_FOURCC('S', '4', '2', '0'):
424 case GST_MAKE_FOURCC('I', '4', '2', '0'):
425 pixtype = MM_PIXEL_FORMAT_I420;
427 case GST_MAKE_FOURCC('Y', 'V', '1', '2'):
428 pixtype = MM_PIXEL_FORMAT_YV12;
430 case GST_MAKE_FOURCC('4', '2', '2', 'P'):
431 pixtype = MM_PIXEL_FORMAT_422P;
433 case GST_MAKE_FOURCC('R', 'G', 'B', 'P'):
434 pixtype = MM_PIXEL_FORMAT_RGB565;
436 case GST_MAKE_FOURCC('R', 'G', 'B', '3'):
437 pixtype = MM_PIXEL_FORMAT_RGB888;
439 case GST_MAKE_FOURCC('A', 'R', 'G', 'B'):
440 case GST_MAKE_FOURCC('x', 'R', 'G', 'B'):
441 pixtype = MM_PIXEL_FORMAT_ARGB;
443 case GST_MAKE_FOURCC('B', 'G', 'R', 'A'):
444 case GST_MAKE_FOURCC('B', 'G', 'R', 'x'):
445 case GST_MAKE_FOURCC('S', 'R', '3', '2'):
446 pixtype = MM_PIXEL_FORMAT_RGBA;
448 case GST_MAKE_FOURCC('J', 'P', 'E', 'G'):
449 case GST_MAKE_FOURCC('P', 'N', 'G', ' '):
450 pixtype = MM_PIXEL_FORMAT_ENCODED;
452 case GST_MAKE_FOURCC('I', 'T', 'L', 'V'):
453 pixtype = MM_PIXEL_FORMAT_ITLV_JPEG_UYVY;
456 LOGE("Not supported fourcc type(%c%c%c%c)",
457 fourcc, fourcc>>8, fourcc>>16, fourcc>>24);
458 pixtype = MM_PIXEL_FORMAT_INVALID;