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 */
40 __get_stream_type_name(int type)
43 case MM_PLAYER_STREAM_TYPE_AUDIO:
45 case MM_PLAYER_STREAM_TYPE_VIDEO:
47 case MM_PLAYER_STREAM_TYPE_TEXT:
49 case MM_PLAYER_STREAM_TYPE_DEFAULT: /* fall through */
50 case MM_PLAYER_STREAM_TYPE_MAX: /* fall through */
57 __get_state_name(int state)
60 case MM_PLAYER_STATE_NULL:
62 case MM_PLAYER_STATE_READY:
64 case MM_PLAYER_STATE_PAUSED:
66 case MM_PLAYER_STATE_PLAYING:
68 case MM_PLAYER_STATE_NONE:
76 __is_rtsp_streaming(mm_player_t* player)
78 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
80 return (player->profile.uri_type == MM_PLAYER_URI_TYPE_URL_RTSP) ? TRUE : FALSE;
84 __is_http_streaming(mm_player_t* player)
86 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
88 return (player->profile.uri_type == MM_PLAYER_URI_TYPE_URL_HTTP) ? TRUE : FALSE;
92 __is_streaming(mm_player_t* player)
94 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
96 return (__is_rtsp_streaming(player) || __is_http_streaming(player)
97 || __is_http_live_streaming(player) || __is_dash_streaming(player) || __is_smooth_streaming(player)) ? TRUE : FALSE;
101 __is_live_streaming(mm_player_t* player)
103 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
105 return (__is_rtsp_streaming(player) && player->streaming_type == STREAMING_SERVICE_LIVE) ? TRUE : FALSE;
109 __is_http_live_streaming(mm_player_t* player)
111 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
113 return (player->profile.uri_type == MM_PLAYER_URI_TYPE_HLS) ? TRUE : FALSE;
117 __is_dash_streaming(mm_player_t* player)
119 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
121 return (player->profile.uri_type == MM_PLAYER_URI_TYPE_DASH) ? TRUE : FALSE;
125 __is_smooth_streaming(mm_player_t* player)
127 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
129 return (player->profile.uri_type == MM_PLAYER_URI_TYPE_SS) ? TRUE : FALSE;
133 __is_ms_buff_src(mm_player_t* player)
135 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
137 return (player->profile.uri_type == MM_PLAYER_URI_TYPE_MS_BUFF) ? TRUE : FALSE;
141 __has_suffix(mm_player_t* player, const gchar* suffix)
143 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
144 MMPLAYER_RETURN_VAL_IF_FAIL(suffix, FALSE);
146 gboolean ret = FALSE;
147 gchar* t_url = g_ascii_strdown(player->profile.uri, -1);
148 gchar* t_suffix = g_ascii_strdown(suffix, -1);
150 if (g_str_has_suffix(player->profile.uri, suffix))
153 MMPLAYER_FREEIF(t_url);
154 MMPLAYER_FREEIF(t_suffix);
160 __mmplayer_post_message(mm_player_t* player, enum MMMessageType msgtype, MMMessageParamType* param)
162 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
167 //LOGD("Message (type : %d) will be posted using msg-cb(%p). \n", msgtype, player->msg_cb);
169 player->msg_cb(msgtype, param, player->msg_cb_param);
175 __mmplayer_dump_pipeline_state(mm_player_t* player)
177 GstIterator*iter = NULL;
178 gboolean done = FALSE;
181 GstElement *element = NULL;
182 GstElementFactory *factory = NULL;
184 GstState state = GST_STATE_VOID_PENDING;
185 GstState pending = GST_STATE_VOID_PENDING;
186 GstClockTime time = 200*GST_MSECOND;
190 MMPLAYER_RETURN_VAL_IF_FAIL(player &&
192 player->pipeline->mainbin,
195 iter = gst_bin_iterate_recurse(GST_BIN(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst));
199 switch (gst_iterator_next(iter, &item)) {
200 case GST_ITERATOR_OK:
201 element = g_value_get_object(&item);
202 gst_element_get_state(element, &state, &pending, time);
204 factory = gst_element_get_factory(element) ;
206 LOGE("%s:%s : From:%s To:%s refcount : %d\n", GST_OBJECT_NAME(factory) , GST_ELEMENT_NAME(element) ,
207 gst_element_state_get_name(state), gst_element_state_get_name(pending) , GST_OBJECT_REFCOUNT_VALUE(element));
208 g_value_reset(&item);
210 case GST_ITERATOR_RESYNC:
211 gst_iterator_resync(iter);
213 case GST_ITERATOR_ERROR:
216 case GST_ITERATOR_DONE:
223 element = GST_ELEMENT(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst);
225 gst_element_get_state(element, &state, &pending, time);
227 factory = gst_element_get_factory(element) ;
230 LOGE("%s:%s : From:%s To:%s refcount : %d\n",
231 GST_OBJECT_NAME(factory),
232 GST_ELEMENT_NAME(element),
233 gst_element_state_get_name(state),
234 gst_element_state_get_name(pending),
235 GST_OBJECT_REFCOUNT_VALUE(element));
238 g_value_unset(&item);
241 gst_iterator_free(iter);
248 int util_exist_file_path(const char *file_path)
251 struct stat stat_results = {0, };
253 if (!file_path || !strlen(file_path))
254 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
256 fd = open(file_path, O_RDONLY);
260 strerror_r(errno, str_error, sizeof(str_error));
261 LOGE("failed to open file by %s (%d)", str_error, errno);
264 return MM_ERROR_PLAYER_PERMISSION_DENIED;
266 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
269 if (fstat(fd, &stat_results) < 0) {
270 LOGE("failed to get file status");
271 } else if (stat_results.st_size == 0) {
272 LOGE("file size is zero");
274 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
276 LOGW("file size : %lld bytes", (long long)stat_results.st_size);
280 return MM_ERROR_NONE;
284 util_get_cookie_list(const char *cookies)
286 char **cookie_list = NULL;
290 if (!cookies || !strlen(cookies))
293 SECURE_LOGD("cookies : %d[bytes] - %s \n", strlen(cookies), cookies);
295 temp = g_strdup(cookies);
297 /* trimming. it works inplace */
301 cookie_list = g_strsplit(temp, ";", 100);
304 LOGE("failed to get cookie list");
308 for (i = 0; i < g_strv_length(cookie_list); i++) {
309 if (cookie_list[i]) {
310 if (strlen(cookie_list[i])) {
311 g_strstrip(cookie_list[i]);
312 SECURE_LOGD("cookie_list[%d] : %d[bytes] - %s \n", i, strlen(cookie_list[i]), cookie_list[i]);
314 cookie_list[i][0] = '\0';
327 /* check the given path is indicating sdp file */
329 util_is_sdp_file(const char *path)
331 gboolean ret = FALSE;
336 MMPLAYER_RETURN_VAL_IF_FAIL(path, FALSE);
338 uri = g_ascii_strdown(path, -1);
346 /* strlen(".sdp") == 4 */
347 if (strlen(uri) <= 4) {
348 LOGW("path is too short.\n");
353 /* first, check extension name */
354 ret = g_str_has_suffix(uri, "sdp");
356 /* second, if no suffix is there, check it's contents */
358 /* FIXIT : do it soon */
368 util_get_charset(const char *file_path)
370 UCharsetDetector* ucsd;
371 const UCharsetMatch* ucm;
372 UErrorCode status = U_ZERO_ERROR;
374 const char* charset = NULL;
379 fin = fopen(file_path, "r");
381 SECURE_LOGE("fail to open file %s\n", file_path);
385 ucsd = ucsdet_open(&status);
386 if (U_FAILURE(status)) {
387 LOGE("fail to ucsdet_open\n");
391 ucsdet_enableInputFilter(ucsd, TRUE);
393 buf = g_malloc(1024*1024);
395 LOGE("fail to alloc\n");
399 n_size = fread(buf, 1, 1024*1024, fin);
400 buf[1024 * 1024 - 1] = '\0';
404 ucsdet_setText(ucsd, buf, strlen(buf), &status);
405 if (U_FAILURE(status)) {
406 LOGE("fail to ucsdet_setText\n");
410 ucm = ucsdet_detect(ucsd, &status);
411 if (U_FAILURE(status)) {
412 LOGE("fail to ucsdet_detect\n");
416 charset = ucsdet_getName(ucm, &status);
417 if (U_FAILURE(status)) {
418 LOGE("fail to ucsdet_getName\n");
422 /* CP949 encoding is an extension of the EUC-KR and it is backwards compatible.*/
423 if (charset && !strcmp(charset, "EUC-KR"))
439 int util_get_pixtype(unsigned int fourcc)
441 int pixtype = MM_PIXEL_FORMAT_INVALID;
444 char *pfourcc = (char*)&fourcc;
446 LOGD("fourcc(%c%c%c%c)",
447 pfourcc[0], pfourcc[1], pfourcc[2], pfourcc[3]);
451 case GST_MAKE_FOURCC('S', 'N', '1', '2'):
452 case GST_MAKE_FOURCC('N', 'V', '1', '2'):
453 pixtype = MM_PIXEL_FORMAT_NV12;
455 case GST_MAKE_FOURCC('S', 'T', '1', '2'):
456 pixtype = MM_PIXEL_FORMAT_NV12T;
458 case GST_MAKE_FOURCC('S', 'N', '2', '1'):
459 case GST_MAKE_FOURCC('N', 'V', '2', '1'):
460 pixtype = MM_PIXEL_FORMAT_NV21;
462 case GST_MAKE_FOURCC('S', 'U', 'Y', 'V'):
463 case GST_MAKE_FOURCC('Y', 'U', 'Y', 'V'):
464 case GST_MAKE_FOURCC('Y', 'U', 'Y', '2'):
465 pixtype = MM_PIXEL_FORMAT_YUYV;
467 case GST_MAKE_FOURCC('S', 'Y', 'V', 'Y'):
468 case GST_MAKE_FOURCC('U', 'Y', 'V', 'Y'):
469 pixtype = MM_PIXEL_FORMAT_UYVY;
471 case GST_MAKE_FOURCC('S', '4', '2', '0'):
472 case GST_MAKE_FOURCC('I', '4', '2', '0'):
473 pixtype = MM_PIXEL_FORMAT_I420;
475 case GST_MAKE_FOURCC('Y', 'V', '1', '2'):
476 pixtype = MM_PIXEL_FORMAT_YV12;
478 case GST_MAKE_FOURCC('4', '2', '2', 'P'):
479 pixtype = MM_PIXEL_FORMAT_422P;
481 case GST_MAKE_FOURCC('R', 'G', 'B', 'P'):
482 pixtype = MM_PIXEL_FORMAT_RGB565;
484 case GST_MAKE_FOURCC('R', 'G', 'B', '3'):
485 pixtype = MM_PIXEL_FORMAT_RGB888;
487 case GST_MAKE_FOURCC('A', 'R', 'G', 'B'):
488 case GST_MAKE_FOURCC('x', 'R', 'G', 'B'):
489 pixtype = MM_PIXEL_FORMAT_ARGB;
491 case GST_MAKE_FOURCC('B', 'G', 'R', 'A'):
492 case GST_MAKE_FOURCC('B', 'G', 'R', 'x'):
493 case GST_MAKE_FOURCC('S', 'R', '3', '2'):
494 pixtype = MM_PIXEL_FORMAT_RGBA;
496 case GST_MAKE_FOURCC('J', 'P', 'E', 'G'):
497 case GST_MAKE_FOURCC('P', 'N', 'G', ' '):
498 pixtype = MM_PIXEL_FORMAT_ENCODED;
500 case GST_MAKE_FOURCC('I', 'T', 'L', 'V'):
501 pixtype = MM_PIXEL_FORMAT_ITLV_JPEG_UYVY;
504 LOGE("Not supported fourcc type(%c%c%c%c)",
505 fourcc, fourcc>>8, fourcc>>16, fourcc>>24);
506 pixtype = MM_PIXEL_FORMAT_INVALID;
513 static int _util_storage_supported_cb(int storage_id, storage_type_e type,
514 storage_state_e state, const char *path, void *user_data)
516 MMPlayerStorageInfo *storage_info = (MMPlayerStorageInfo *)user_data;
518 MMPLAYER_RETURN_VAL_IF_FAIL(storage_info, FALSE);
520 if (type == storage_info->type && strstr(storage_info->path, path)) {
521 storage_info->id = storage_id;
522 storage_info->state = state;
529 bool util_get_storage_info(const char *path, MMPlayerStorageInfo *storage_info)
532 const char *file_path = path;
534 MMPLAYER_RETURN_VAL_IF_FAIL(file_path && storage_info, false);
536 if (strncmp(file_path, "file://", strlen("file://")) == 0)
537 file_path = path+7; /* remove file prefix */
539 if (strncmp(file_path, MEDIA_PATH_EXTERNAL, strlen(MEDIA_PATH_EXTERNAL)) == 0) {
540 storage_info->type = STORAGE_TYPE_EXTERNAL;
542 memset(storage_info->path, 0x00, MM_MAX_URL_LEN);
543 g_snprintf(storage_info->path, MM_MAX_URL_LEN, "%s", file_path);
545 ret = storage_foreach_device_supported((storage_device_supported_cb)_util_storage_supported_cb, storage_info);
546 if (ret != STORAGE_ERROR_NONE) {
547 LOGE("failed to check supported storage 0x%x", ret);
551 if (storage_info->state <= STORAGE_STATE_REMOVED) {
552 LOGE("need to check the external storage state %d:%d", storage_info->id, storage_info->state);
556 storage_info->type = STORAGE_TYPE_INTERNAL;
557 storage_info->state = STORAGE_STATE_MOUNTED;
560 LOGD("storage info %d:%d:%d", storage_info->type, storage_info->id, storage_info->state);