efa9a107fe886643f26c4bc5297a984999cb6fbb
[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 bool util_check_valid_url(const char *proxy)
112 {
113         struct in_addr proxy_addr;
114         bool ret = TRUE;
115
116         MMPLAYER_RETURN_VAL_IF_FAIL(proxy, FALSE);
117         MMPLAYER_RETURN_VAL_IF_FAIL(strlen(proxy), FALSE);
118
119         if (inet_aton(proxy, &proxy_addr) != 0) {
120                 LOGW("invalid proxy is set. \n");
121                 ret = FALSE;
122         }
123
124         return ret;
125 }
126
127 /* check the given path is indicating sdp file */
128 bool
129 util_is_sdp_file(const char *path)
130 {
131         gboolean ret = FALSE;
132         gchar* uri = NULL;
133
134         MMPLAYER_FENTER();
135
136         MMPLAYER_RETURN_VAL_IF_FAIL(path, FALSE);
137
138         uri = g_ascii_strdown(path, -1);
139
140         if (uri == NULL)
141                 return FALSE;
142
143         /* trimming */
144         g_strstrip(uri);
145
146         /* strlen(".sdp") == 4 */
147         if (strlen(uri) <= 4) {
148                 LOGW("path is too short.\n");
149                 g_free(uri);
150                 return ret;
151         }
152
153         /* first, check extension name */
154         ret = g_str_has_suffix(uri, "sdp");
155
156         /* second, if no suffix is there, check it's contents */
157         if (!ret)
158                 /* FIXIT : do it soon */
159                 LOGD("no suffix");
160
161         g_free(uri);
162         uri = NULL;
163
164         return ret;
165 }
166
167 const char*
168 util_get_charset(const char *file_path)
169 {
170         UCharsetDetector* ucsd;
171         const UCharsetMatch* ucm;
172         UErrorCode status = U_ZERO_ERROR;
173
174         const char* charset = NULL;
175         char *buf = NULL;
176         FILE* fin = 0;
177         size_t n_size = 0;
178
179         fin = fopen(file_path, "r");
180         if (!fin) {
181                 SECURE_LOGE("fail to open file %s\n", file_path);
182                 return NULL;
183         }
184
185         ucsd = ucsdet_open(&status);
186         if (U_FAILURE(status)) {
187                 LOGE("fail to ucsdet_open\n");
188                 goto done;
189         }
190
191         ucsdet_enableInputFilter(ucsd, TRUE);
192
193         buf = g_malloc(1024*1024);
194         if (!buf) {
195                 LOGE("fail to alloc\n");
196                 goto done;
197         }
198
199         n_size = fread(buf, 1, 1024*1024, fin);
200         buf[1024 * 1024 - 1] = '\0';
201         if (!n_size)
202                 goto done;
203
204         ucsdet_setText(ucsd, buf, strlen(buf), &status);
205         if (U_FAILURE(status)) {
206                 LOGE("fail to ucsdet_setText\n");
207                 goto done;
208         }
209
210         ucm = ucsdet_detect(ucsd, &status);
211         if (U_FAILURE(status)) {
212                 LOGE("fail to ucsdet_detect\n");
213                 goto done;
214         }
215
216         charset = ucsdet_getName(ucm, &status);
217         if (U_FAILURE(status)) {
218                 LOGE("fail to ucsdet_getName\n");
219                 goto done;
220         }
221
222         /* CP949 encoding is an extension of the EUC-KR and it is backwards compatible.*/
223         if (charset && !strcmp(charset, "EUC-KR"))
224                 charset = "CP949";
225
226 done:
227         if (fin)
228                 fclose(fin);
229
230         if (ucsd)
231                 ucsdet_close(ucsd);
232
233         if (buf)
234                 g_free(buf);
235
236         return charset;
237 }
238
239 int util_get_pixtype(unsigned int fourcc)
240 {
241         int pixtype = MM_PIXEL_FORMAT_INVALID;
242
243     /*
244         char *pfourcc = (char*)&fourcc;
245
246         LOGD("fourcc(%c%c%c%c)",
247                 pfourcc[0], pfourcc[1], pfourcc[2], pfourcc[3]);
248     */
249
250         switch (fourcc) {
251         case GST_MAKE_FOURCC('S', 'N', '1', '2'):
252         case GST_MAKE_FOURCC('N', 'V', '1', '2'):
253                 pixtype = MM_PIXEL_FORMAT_NV12;
254                 break;
255         case GST_MAKE_FOURCC('S', 'T', '1', '2'):
256                 pixtype = MM_PIXEL_FORMAT_NV12T;
257                 break;
258         case GST_MAKE_FOURCC('S', 'N', '2', '1'):
259         case GST_MAKE_FOURCC('N', 'V', '2', '1'):
260                 pixtype = MM_PIXEL_FORMAT_NV21;
261                 break;
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;
266                 break;
267         case GST_MAKE_FOURCC('S', 'Y', 'V', 'Y'):
268         case GST_MAKE_FOURCC('U', 'Y', 'V', 'Y'):
269                 pixtype = MM_PIXEL_FORMAT_UYVY;
270                 break;
271         case GST_MAKE_FOURCC('S', '4', '2', '0'):
272         case GST_MAKE_FOURCC('I', '4', '2', '0'):
273                 pixtype = MM_PIXEL_FORMAT_I420;
274                 break;
275         case GST_MAKE_FOURCC('Y', 'V', '1', '2'):
276                 pixtype = MM_PIXEL_FORMAT_YV12;
277                 break;
278         case GST_MAKE_FOURCC('4', '2', '2', 'P'):
279                 pixtype = MM_PIXEL_FORMAT_422P;
280                 break;
281         case GST_MAKE_FOURCC('R', 'G', 'B', 'P'):
282                 pixtype = MM_PIXEL_FORMAT_RGB565;
283                 break;
284         case GST_MAKE_FOURCC('R', 'G', 'B', '3'):
285                 pixtype = MM_PIXEL_FORMAT_RGB888;
286                 break;
287         case GST_MAKE_FOURCC('A', 'R', 'G', 'B'):
288         case GST_MAKE_FOURCC('x', 'R', 'G', 'B'):
289                 pixtype = MM_PIXEL_FORMAT_ARGB;
290                 break;
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;
295                 break;
296         case GST_MAKE_FOURCC('J', 'P', 'E', 'G'):
297         case GST_MAKE_FOURCC('P', 'N', 'G', ' '):
298                 pixtype = MM_PIXEL_FORMAT_ENCODED;
299                 break;
300         case GST_MAKE_FOURCC('I', 'T', 'L', 'V'):
301                 pixtype = MM_PIXEL_FORMAT_ITLV_JPEG_UYVY;
302                 break;
303         default:
304                 LOGE("Not supported fourcc type(%c%c%c%c)",
305                         fourcc, fourcc>>8, fourcc>>16, fourcc>>24);
306                 pixtype = MM_PIXEL_FORMAT_INVALID;
307                 break;
308         }
309
310         return pixtype;
311 }
312
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)
315 {
316         MMPlayerStorageInfo *storage_info = (MMPlayerStorageInfo *)user_data;
317
318         MMPLAYER_RETURN_VAL_IF_FAIL(storage_info, FALSE);
319
320         if (type == storage_info->type && strstr(storage_info->path, path)) {
321                 storage_info->id = storage_id;
322                 storage_info->state = state;
323                 return FALSE;
324         }
325
326         return TRUE;
327 }
328
329 bool util_get_storage_info(const char *path, MMPlayerStorageInfo *storage_info)
330 {
331         int ret = 0;
332         const char *file_path = path;
333
334         MMPLAYER_RETURN_VAL_IF_FAIL(file_path && storage_info, false);
335
336         if (strncmp(file_path, "file://", strlen("file://")) == 0)
337                 file_path = path+7; /* remove file prefix */
338
339         if (strncmp(file_path, MEDIA_PATH_EXTERNAL, strlen(MEDIA_PATH_EXTERNAL)) == 0)
340                 storage_info->type = STORAGE_TYPE_EXTERNAL;
341         else
342                 storage_info->type = STORAGE_TYPE_INTERNAL;
343
344         memset(storage_info->path, 0x00, MM_MAX_URL_LEN);
345         g_snprintf(storage_info->path, MM_MAX_URL_LEN, "%s", file_path);
346
347         ret = storage_foreach_device_supported((storage_device_supported_cb)_util_storage_supported_cb, storage_info);
348         if (ret != STORAGE_ERROR_NONE) {
349                 LOGE("failed to check supported storage 0x%x", ret);
350                 return false;
351         }
352
353         if ((storage_info->type == STORAGE_TYPE_EXTERNAL) &&
354                 (storage_info->state <= STORAGE_STATE_REMOVED)) {
355                 LOGE("need to check the external storage state %d:%d", storage_info->id, storage_info->state);
356                 return false;
357         }
358
359         LOGD("storage info %d:%d:%d", storage_info->type, storage_info->id, storage_info->state);
360         return true;
361 }