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 <tzplatform_config.h>
35 #include "mm_player_utils.h"
37 #define MEDIA_PATH_EXTERNAL tzplatform_getenv(TZ_SYS_STORAGE) /* external storage, sd card, usb */
39 int util_exist_file_path(const char *file_path)
42 struct stat stat_results = {0, };
44 if (!file_path || !strlen(file_path))
45 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
47 fd = open(file_path, O_RDONLY);
51 strerror_r(errno, str_error, sizeof(str_error));
52 LOGE("failed to open file by %s (%d)", str_error, errno);
55 return MM_ERROR_PLAYER_PERMISSION_DENIED;
57 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
60 if (fstat(fd, &stat_results) < 0) {
61 LOGE("failed to get file status");
62 } else if (stat_results.st_size == 0) {
63 LOGE("file size is zero");
65 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
67 LOGW("file size : %lld bytes", (long long)stat_results.st_size);
75 util_get_cookie_list(const char *cookies)
77 char **cookie_list = NULL;
81 if (!cookies || !strlen(cookies))
84 SECURE_LOGD("cookies : %d[bytes] - %s \n", strlen(cookies), cookies);
86 temp = g_strdup(cookies);
88 /* trimming. it works inplace */
92 cookie_list = g_strsplit(temp, ";", 100);
94 for (i = 0; i < g_strv_length(cookie_list); i++) {
96 if (strlen(cookie_list[i])) {
97 g_strstrip(cookie_list[i]);
98 SECURE_LOGD("cookie_list[%d] : %d[bytes] - %s \n", i, strlen(cookie_list[i]), cookie_list[i]);
100 cookie_list[i][0] = '\0';
111 bool util_check_valid_url(const char *proxy)
113 struct in_addr proxy_addr;
116 MMPLAYER_RETURN_VAL_IF_FAIL(proxy, FALSE);
117 MMPLAYER_RETURN_VAL_IF_FAIL(strlen(proxy), FALSE);
119 if (inet_aton(proxy, &proxy_addr) != 0) {
120 LOGW("invalid proxy is set. \n");
127 /* check the given path is indicating sdp file */
129 util_is_sdp_file(const char *path)
131 gboolean ret = FALSE;
136 MMPLAYER_RETURN_VAL_IF_FAIL(path, FALSE);
138 uri = g_ascii_strdown(path, -1);
146 /* strlen(".sdp") == 4 */
147 if (strlen(uri) <= 4) {
148 LOGW("path is too short.\n");
153 /* first, check extension name */
154 ret = g_str_has_suffix(uri, "sdp");
156 /* second, if no suffix is there, check it's contents */
158 /* FIXIT : do it soon */
168 util_get_charset(const char *file_path)
170 UCharsetDetector* ucsd;
171 const UCharsetMatch* ucm;
172 UErrorCode status = U_ZERO_ERROR;
174 const char* charset = NULL;
179 fin = fopen(file_path, "r");
181 SECURE_LOGE("fail to open file %s\n", file_path);
185 ucsd = ucsdet_open(&status);
186 if (U_FAILURE(status)) {
187 LOGE("fail to ucsdet_open\n");
191 ucsdet_enableInputFilter(ucsd, TRUE);
193 buf = g_malloc(1024*1024);
195 LOGE("fail to alloc\n");
199 n_size = fread(buf, 1, 1024*1024, fin);
200 buf[1024 * 1024 - 1] = '\0';
204 ucsdet_setText(ucsd, buf, strlen(buf), &status);
205 if (U_FAILURE(status)) {
206 LOGE("fail to ucsdet_setText\n");
210 ucm = ucsdet_detect(ucsd, &status);
211 if (U_FAILURE(status)) {
212 LOGE("fail to ucsdet_detect\n");
216 charset = ucsdet_getName(ucm, &status);
217 if (U_FAILURE(status)) {
218 LOGE("fail to ucsdet_getName\n");
222 /* CP949 encoding is an extension of the EUC-KR and it is backwards compatible.*/
223 if (charset && !strcmp(charset, "EUC-KR"))
239 int util_get_pixtype(unsigned int fourcc)
241 int pixtype = MM_PIXEL_FORMAT_INVALID;
244 char *pfourcc = (char*)&fourcc;
246 LOGD("fourcc(%c%c%c%c)",
247 pfourcc[0], pfourcc[1], pfourcc[2], pfourcc[3]);
251 case GST_MAKE_FOURCC('S', 'N', '1', '2'):
252 case GST_MAKE_FOURCC('N', 'V', '1', '2'):
253 pixtype = MM_PIXEL_FORMAT_NV12;
255 case GST_MAKE_FOURCC('S', 'T', '1', '2'):
256 pixtype = MM_PIXEL_FORMAT_NV12T;
258 case GST_MAKE_FOURCC('S', 'N', '2', '1'):
259 case GST_MAKE_FOURCC('N', 'V', '2', '1'):
260 pixtype = MM_PIXEL_FORMAT_NV21;
262 case GST_MAKE_FOURCC('S', 'U', 'Y', 'V'):
263 case GST_MAKE_FOURCC('Y', 'U', 'Y', 'V'):
264 case GST_MAKE_FOURCC('Y', 'U', 'Y', '2'):
265 pixtype = MM_PIXEL_FORMAT_YUYV;
267 case GST_MAKE_FOURCC('S', 'Y', 'V', 'Y'):
268 case GST_MAKE_FOURCC('U', 'Y', 'V', 'Y'):
269 pixtype = MM_PIXEL_FORMAT_UYVY;
271 case GST_MAKE_FOURCC('S', '4', '2', '0'):
272 case GST_MAKE_FOURCC('I', '4', '2', '0'):
273 pixtype = MM_PIXEL_FORMAT_I420;
275 case GST_MAKE_FOURCC('Y', 'V', '1', '2'):
276 pixtype = MM_PIXEL_FORMAT_YV12;
278 case GST_MAKE_FOURCC('4', '2', '2', 'P'):
279 pixtype = MM_PIXEL_FORMAT_422P;
281 case GST_MAKE_FOURCC('R', 'G', 'B', 'P'):
282 pixtype = MM_PIXEL_FORMAT_RGB565;
284 case GST_MAKE_FOURCC('R', 'G', 'B', '3'):
285 pixtype = MM_PIXEL_FORMAT_RGB888;
287 case GST_MAKE_FOURCC('A', 'R', 'G', 'B'):
288 case GST_MAKE_FOURCC('x', 'R', 'G', 'B'):
289 pixtype = MM_PIXEL_FORMAT_ARGB;
291 case GST_MAKE_FOURCC('B', 'G', 'R', 'A'):
292 case GST_MAKE_FOURCC('B', 'G', 'R', 'x'):
293 case GST_MAKE_FOURCC('S', 'R', '3', '2'):
294 pixtype = MM_PIXEL_FORMAT_RGBA;
296 case GST_MAKE_FOURCC('J', 'P', 'E', 'G'):
297 case GST_MAKE_FOURCC('P', 'N', 'G', ' '):
298 pixtype = MM_PIXEL_FORMAT_ENCODED;
300 case GST_MAKE_FOURCC('I', 'T', 'L', 'V'):
301 pixtype = MM_PIXEL_FORMAT_ITLV_JPEG_UYVY;
304 LOGE("Not supported fourcc type(%c%c%c%c)",
305 fourcc, fourcc>>8, fourcc>>16, fourcc>>24);
306 pixtype = MM_PIXEL_FORMAT_INVALID;
313 static int _util_storage_supported_cb(int storage_id, storage_type_e type,
314 storage_state_e state, const char *path, void *user_data)
316 MMPlayerStorageInfo *storage_info = (MMPlayerStorageInfo *)user_data;
318 MMPLAYER_RETURN_VAL_IF_FAIL(storage_info, FALSE);
320 if (type == storage_info->type && strstr(storage_info->path, path)) {
321 storage_info->id = storage_id;
322 storage_info->state = state;
329 bool util_get_storage_info(const char *path, MMPlayerStorageInfo *storage_info)
332 const char *file_path = path;
334 MMPLAYER_RETURN_VAL_IF_FAIL(file_path && storage_info, false);
336 if (strncmp(file_path, "file://", strlen("file://")) == 0)
337 file_path = path+7; /* remove file prefix */
339 if (strncmp(file_path, MEDIA_PATH_EXTERNAL, strlen(MEDIA_PATH_EXTERNAL)) == 0) {
340 storage_info->type = STORAGE_TYPE_EXTERNAL;
342 memset(storage_info->path, 0x00, MM_MAX_URL_LEN);
343 g_snprintf(storage_info->path, MM_MAX_URL_LEN, "%s", file_path);
345 ret = storage_foreach_device_supported((storage_device_supported_cb)_util_storage_supported_cb, storage_info);
346 if (ret != STORAGE_ERROR_NONE) {
347 LOGE("failed to check supported storage 0x%x", ret);
351 if (storage_info->state <= STORAGE_STATE_REMOVED) {
352 LOGE("need to check the external storage state %d:%d", storage_info->id, storage_info->state);
356 storage_info->type = STORAGE_TYPE_INTERNAL;
357 storage_info->state = STORAGE_STATE_MOUNTED;
360 LOGD("storage info %d:%d:%d", storage_info->type, storage_info->id, storage_info->state);