[0.6.102] Remove unused attr
[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 <fcntl.h>
28 #include <sys/time.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
31 #include <unicode/ucsdet.h>
32 #include <dlog.h>
33 #include <storage.h>
34 #include <tzplatform_config.h>
35 #include "mm_player_utils.h"
36
37 #define MEDIA_PATH_EXTERNAL tzplatform_getenv(TZ_SYS_STORAGE) /* external storage, sd card, usb */
38
39 int util_exist_file_path(const char *file_path)
40 {
41         int fd = 0;
42         struct stat stat_results = {0, };
43
44         if (!file_path || !strlen(file_path))
45                 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
46
47         fd = open(file_path, O_RDONLY);
48
49         if (fd < 0) {
50                 char str_error[256];
51                 strerror_r(errno, str_error, sizeof(str_error));
52                 LOGE("failed to open file by %s (%d)", str_error, errno);
53
54                 if (EACCES == errno)
55                         return MM_ERROR_PLAYER_PERMISSION_DENIED;
56
57                 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
58         }
59
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");
64                 close(fd);
65                 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
66         } else
67                 LOGW("file size : %lld bytes", (long long)stat_results.st_size);
68
69         close(fd);
70
71         return MM_ERROR_NONE;
72 }
73
74 char**
75 util_get_cookie_list(const char *cookies)
76 {
77         char **cookie_list = NULL;
78         char *temp = NULL;
79         gint i = 0;
80
81         if (!cookies || !strlen(cookies))
82                 return NULL;
83
84         SECURE_LOGD("cookies : %d[bytes] - %s \n", strlen(cookies), cookies);
85
86         temp = g_strdup(cookies);
87
88         /* trimming. it works inplace */
89         g_strstrip(temp);
90
91         /* split */
92         cookie_list = g_strsplit(temp, ";", 100);
93
94         for (i = 0; i < g_strv_length(cookie_list); i++) {
95                 if (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]);
99                         } else
100                                 cookie_list[i][0] = '\0';
101                 }
102         }
103
104         if (temp)
105                 g_free(temp);
106         temp = NULL;
107
108         return cookie_list;
109 }
110
111 /* check the given path is indicating sdp file */
112 bool
113 util_is_sdp_file(const char *path)
114 {
115         gboolean ret = FALSE;
116         gchar* uri = NULL;
117
118         MMPLAYER_FENTER();
119
120         MMPLAYER_RETURN_VAL_IF_FAIL(path, FALSE);
121
122         uri = g_ascii_strdown(path, -1);
123
124         if (uri == NULL)
125                 return FALSE;
126
127         /* trimming */
128         g_strstrip(uri);
129
130         /* strlen(".sdp") == 4 */
131         if (strlen(uri) <= 4) {
132                 LOGW("path is too short.\n");
133                 g_free(uri);
134                 return ret;
135         }
136
137         /* first, check extension name */
138         ret = g_str_has_suffix(uri, "sdp");
139
140         /* second, if no suffix is there, check it's contents */
141         if (!ret)
142                 /* FIXIT : do it soon */
143                 LOGD("no suffix");
144
145         g_free(uri);
146         uri = NULL;
147
148         return ret;
149 }
150
151 const char*
152 util_get_charset(const char *file_path)
153 {
154         UCharsetDetector* ucsd;
155         const UCharsetMatch* ucm;
156         UErrorCode status = U_ZERO_ERROR;
157
158         const char* charset = NULL;
159         char *buf = NULL;
160         FILE* fin = 0;
161         size_t n_size = 0;
162
163         fin = fopen(file_path, "r");
164         if (!fin) {
165                 SECURE_LOGE("fail to open file %s\n", file_path);
166                 return NULL;
167         }
168
169         ucsd = ucsdet_open(&status);
170         if (U_FAILURE(status)) {
171                 LOGE("fail to ucsdet_open\n");
172                 goto done;
173         }
174
175         ucsdet_enableInputFilter(ucsd, TRUE);
176
177         buf = g_malloc(1024*1024);
178         if (!buf) {
179                 LOGE("fail to alloc\n");
180                 goto done;
181         }
182
183         n_size = fread(buf, 1, 1024*1024, fin);
184         buf[1024 * 1024 - 1] = '\0';
185         if (!n_size)
186                 goto done;
187
188         ucsdet_setText(ucsd, buf, strlen(buf), &status);
189         if (U_FAILURE(status)) {
190                 LOGE("fail to ucsdet_setText\n");
191                 goto done;
192         }
193
194         ucm = ucsdet_detect(ucsd, &status);
195         if (U_FAILURE(status)) {
196                 LOGE("fail to ucsdet_detect\n");
197                 goto done;
198         }
199
200         charset = ucsdet_getName(ucm, &status);
201         if (U_FAILURE(status)) {
202                 LOGE("fail to ucsdet_getName\n");
203                 goto done;
204         }
205
206         /* CP949 encoding is an extension of the EUC-KR and it is backwards compatible.*/
207         if (charset && !strcmp(charset, "EUC-KR"))
208                 charset = "CP949";
209
210 done:
211         if (fin)
212                 fclose(fin);
213
214         if (ucsd)
215                 ucsdet_close(ucsd);
216
217         if (buf)
218                 g_free(buf);
219
220         return charset;
221 }
222
223 int util_get_pixtype(unsigned int fourcc)
224 {
225         int pixtype = MM_PIXEL_FORMAT_INVALID;
226
227     /*
228         char *pfourcc = (char*)&fourcc;
229
230         LOGD("fourcc(%c%c%c%c)",
231                 pfourcc[0], pfourcc[1], pfourcc[2], pfourcc[3]);
232     */
233
234         switch (fourcc) {
235         case GST_MAKE_FOURCC('S', 'N', '1', '2'):
236         case GST_MAKE_FOURCC('N', 'V', '1', '2'):
237                 pixtype = MM_PIXEL_FORMAT_NV12;
238                 break;
239         case GST_MAKE_FOURCC('S', 'T', '1', '2'):
240                 pixtype = MM_PIXEL_FORMAT_NV12T;
241                 break;
242         case GST_MAKE_FOURCC('S', 'N', '2', '1'):
243         case GST_MAKE_FOURCC('N', 'V', '2', '1'):
244                 pixtype = MM_PIXEL_FORMAT_NV21;
245                 break;
246         case GST_MAKE_FOURCC('S', 'U', 'Y', 'V'):
247         case GST_MAKE_FOURCC('Y', 'U', 'Y', 'V'):
248         case GST_MAKE_FOURCC('Y', 'U', 'Y', '2'):
249                 pixtype = MM_PIXEL_FORMAT_YUYV;
250                 break;
251         case GST_MAKE_FOURCC('S', 'Y', 'V', 'Y'):
252         case GST_MAKE_FOURCC('U', 'Y', 'V', 'Y'):
253                 pixtype = MM_PIXEL_FORMAT_UYVY;
254                 break;
255         case GST_MAKE_FOURCC('S', '4', '2', '0'):
256         case GST_MAKE_FOURCC('I', '4', '2', '0'):
257                 pixtype = MM_PIXEL_FORMAT_I420;
258                 break;
259         case GST_MAKE_FOURCC('Y', 'V', '1', '2'):
260                 pixtype = MM_PIXEL_FORMAT_YV12;
261                 break;
262         case GST_MAKE_FOURCC('4', '2', '2', 'P'):
263                 pixtype = MM_PIXEL_FORMAT_422P;
264                 break;
265         case GST_MAKE_FOURCC('R', 'G', 'B', 'P'):
266                 pixtype = MM_PIXEL_FORMAT_RGB565;
267                 break;
268         case GST_MAKE_FOURCC('R', 'G', 'B', '3'):
269                 pixtype = MM_PIXEL_FORMAT_RGB888;
270                 break;
271         case GST_MAKE_FOURCC('A', 'R', 'G', 'B'):
272         case GST_MAKE_FOURCC('x', 'R', 'G', 'B'):
273                 pixtype = MM_PIXEL_FORMAT_ARGB;
274                 break;
275         case GST_MAKE_FOURCC('B', 'G', 'R', 'A'):
276         case GST_MAKE_FOURCC('B', 'G', 'R', 'x'):
277         case GST_MAKE_FOURCC('S', 'R', '3', '2'):
278                 pixtype = MM_PIXEL_FORMAT_RGBA;
279                 break;
280         case GST_MAKE_FOURCC('J', 'P', 'E', 'G'):
281         case GST_MAKE_FOURCC('P', 'N', 'G', ' '):
282                 pixtype = MM_PIXEL_FORMAT_ENCODED;
283                 break;
284         case GST_MAKE_FOURCC('I', 'T', 'L', 'V'):
285                 pixtype = MM_PIXEL_FORMAT_ITLV_JPEG_UYVY;
286                 break;
287         default:
288                 LOGE("Not supported fourcc type(%c%c%c%c)",
289                         fourcc, fourcc>>8, fourcc>>16, fourcc>>24);
290                 pixtype = MM_PIXEL_FORMAT_INVALID;
291                 break;
292         }
293
294         return pixtype;
295 }
296
297 static int _util_storage_supported_cb(int storage_id, storage_type_e type,
298         storage_state_e state, const char *path, void *user_data)
299 {
300         MMPlayerStorageInfo *storage_info = (MMPlayerStorageInfo *)user_data;
301
302         MMPLAYER_RETURN_VAL_IF_FAIL(storage_info, FALSE);
303
304         if (type == storage_info->type && strstr(storage_info->path, path)) {
305                 storage_info->id = storage_id;
306                 storage_info->state = state;
307                 return FALSE;
308         }
309
310         return TRUE;
311 }
312
313 bool util_get_storage_info(const char *path, MMPlayerStorageInfo *storage_info)
314 {
315         int ret = 0;
316         const char *file_path = path;
317
318         MMPLAYER_RETURN_VAL_IF_FAIL(file_path && storage_info, false);
319
320         if (strncmp(file_path, "file://", strlen("file://")) == 0)
321                 file_path = path+7; /* remove file prefix */
322
323         if (strncmp(file_path, MEDIA_PATH_EXTERNAL, strlen(MEDIA_PATH_EXTERNAL)) == 0) {
324                 storage_info->type = STORAGE_TYPE_EXTERNAL;
325
326                 memset(storage_info->path, 0x00, MM_MAX_URL_LEN);
327                 g_snprintf(storage_info->path, MM_MAX_URL_LEN, "%s", file_path);
328
329                 ret = storage_foreach_device_supported((storage_device_supported_cb)_util_storage_supported_cb, storage_info);
330                 if (ret != STORAGE_ERROR_NONE) {
331                         LOGE("failed to check supported storage 0x%x", ret);
332                         return false;
333                 }
334
335                 if (storage_info->state <= STORAGE_STATE_REMOVED) {
336                         LOGE("need to check the external storage state %d:%d", storage_info->id, storage_info->state);
337                         return false;
338                 }
339         } else {
340                 storage_info->type = STORAGE_TYPE_INTERNAL;
341                 storage_info->state = STORAGE_STATE_MOUNTED;
342         }
343
344         LOGD("storage info %d:%d:%d", storage_info->type, storage_info->id, storage_info->state);
345         return true;
346 }