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).", 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", 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",
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);
249 util_exist_file_path(const char *file_path)
252 struct stat stat_results = {0, };
254 if (!file_path || !strlen(file_path))
255 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
257 fd = open(file_path, O_RDONLY);
261 strerror_r(errno, str_error, sizeof(str_error));
262 LOGE("failed to open file by %s (%d)", str_error, errno);
265 return MM_ERROR_PLAYER_PERMISSION_DENIED;
267 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
270 if (fstat(fd, &stat_results) < 0) {
271 LOGE("failed to get file status");
272 } else if (stat_results.st_size == 0) {
273 LOGE("file size is zero");
275 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
277 LOGW("file size : %lld bytes", (long long)stat_results.st_size);
281 return MM_ERROR_NONE;
285 util_get_cookie_list(const char *cookies)
287 char **cookie_list = NULL;
291 if (!cookies || !strlen(cookies))
294 SECURE_LOGD("cookies : %zu[bytes] - %s", strlen(cookies), cookies);
296 temp = g_strdup(cookies);
298 /* trimming. it works inplace */
302 cookie_list = g_strsplit(temp, ";", 100);
305 LOGE("failed to get cookie list");
309 for (i = 0; i < g_strv_length(cookie_list); i++) {
310 if (cookie_list[i]) {
311 if (strlen(cookie_list[i])) {
312 g_strstrip(cookie_list[i]);
313 SECURE_LOGD("cookie_list[%d] : %zu[bytes] - %s", i, strlen(cookie_list[i]), cookie_list[i]);
315 cookie_list[i][0] = '\0';
321 MMPLAYER_FREEIF(temp);
326 /* check the given path is indicating sdp file */
328 util_is_sdp_file(const char *path)
330 gboolean ret = FALSE;
335 MMPLAYER_RETURN_VAL_IF_FAIL(path, FALSE);
337 uri = g_ascii_strdown(path, -1);
345 /* strlen(".sdp") == 4 */
346 if (strlen(uri) <= 4) {
347 LOGW("path is too short.");
348 MMPLAYER_FREEIF(uri);
352 /* first, check extension name */
353 ret = g_str_has_suffix(uri, "sdp");
355 /* second, if no suffix is there, check it's contents */
357 /* FIXIT : do it soon */
360 MMPLAYER_FREEIF(uri);
366 util_get_charset(const char *file_path)
368 UCharsetDetector *ucsd;
369 const UCharsetMatch *ucm;
370 UErrorCode status = U_ZERO_ERROR;
372 const char *charset = NULL;
377 fin = fopen(file_path, "r");
379 SECURE_LOGE("fail to open file %s", file_path);
383 ucsd = ucsdet_open(&status);
384 if (U_FAILURE(status)) {
385 LOGE("fail to ucsdet_open");
389 ucsdet_enableInputFilter(ucsd, TRUE);
391 buf = g_try_malloc(1024 * 1024);
393 LOGE("fail to alloc");
397 n_size = fread(buf, 1, 1024*1024, fin);
398 buf[1024 * 1024 - 1] = '\0';
402 ucsdet_setText(ucsd, buf, strlen(buf), &status);
403 if (U_FAILURE(status)) {
404 LOGE("fail to ucsdet_setText");
408 ucm = ucsdet_detect(ucsd, &status);
409 if (U_FAILURE(status)) {
410 LOGE("fail to ucsdet_detect");
414 charset = ucsdet_getName(ucm, &status);
415 if (U_FAILURE(status)) {
416 LOGE("fail to ucsdet_getName");
420 /* CP949 encoding is an extension of the EUC-KR and it is backwards compatible.*/
421 if (charset && !strcmp(charset, "EUC-KR"))
431 MMPLAYER_FREEIF(buf);
437 util_get_pixtype(unsigned int fourcc)
439 int pixtype = MM_PIXEL_FORMAT_INVALID;
442 char *pfourcc = (char *)&fourcc;
444 LOGD("fourcc(%c%c%c%c)",
445 pfourcc[0], pfourcc[1], pfourcc[2], pfourcc[3]);
449 case GST_MAKE_FOURCC('S', 'N', '1', '2'):
450 case GST_MAKE_FOURCC('N', 'V', '1', '2'):
451 pixtype = MM_PIXEL_FORMAT_NV12;
453 case GST_MAKE_FOURCC('S', 'T', '1', '2'):
454 pixtype = MM_PIXEL_FORMAT_NV12T;
456 case GST_MAKE_FOURCC('S', 'N', '2', '1'):
457 case GST_MAKE_FOURCC('N', 'V', '2', '1'):
458 pixtype = MM_PIXEL_FORMAT_NV21;
460 case GST_MAKE_FOURCC('S', 'U', 'Y', 'V'):
461 case GST_MAKE_FOURCC('Y', 'U', 'Y', 'V'):
462 case GST_MAKE_FOURCC('Y', 'U', 'Y', '2'):
463 pixtype = MM_PIXEL_FORMAT_YUYV;
465 case GST_MAKE_FOURCC('S', 'Y', 'V', 'Y'):
466 case GST_MAKE_FOURCC('U', 'Y', 'V', 'Y'):
467 pixtype = MM_PIXEL_FORMAT_UYVY;
469 case GST_MAKE_FOURCC('S', '4', '2', '0'):
470 case GST_MAKE_FOURCC('I', '4', '2', '0'):
471 pixtype = MM_PIXEL_FORMAT_I420;
473 case GST_MAKE_FOURCC('Y', 'V', '1', '2'):
474 pixtype = MM_PIXEL_FORMAT_YV12;
476 case GST_MAKE_FOURCC('4', '2', '2', 'P'):
477 pixtype = MM_PIXEL_FORMAT_422P;
479 case GST_MAKE_FOURCC('R', 'G', 'B', 'P'):
480 pixtype = MM_PIXEL_FORMAT_RGB565;
482 case GST_MAKE_FOURCC('R', 'G', 'B', '3'):
483 pixtype = MM_PIXEL_FORMAT_RGB888;
485 case GST_MAKE_FOURCC('A', 'R', 'G', 'B'):
486 case GST_MAKE_FOURCC('x', 'R', 'G', 'B'):
487 pixtype = MM_PIXEL_FORMAT_ARGB;
489 case GST_MAKE_FOURCC('B', 'G', 'R', 'A'):
490 case GST_MAKE_FOURCC('B', 'G', 'R', 'x'):
491 case GST_MAKE_FOURCC('S', 'R', '3', '2'):
492 pixtype = MM_PIXEL_FORMAT_RGBA;
494 case GST_MAKE_FOURCC('J', 'P', 'E', 'G'):
495 case GST_MAKE_FOURCC('P', 'N', 'G', ' '):
496 pixtype = MM_PIXEL_FORMAT_ENCODED;
498 case GST_MAKE_FOURCC('I', 'T', 'L', 'V'):
499 pixtype = MM_PIXEL_FORMAT_ITLV_JPEG_UYVY;
502 LOGE("Not supported fourcc type(%c%c%c%c)",
503 fourcc, fourcc>>8, fourcc>>16, fourcc>>24);
504 pixtype = MM_PIXEL_FORMAT_INVALID;
512 _util_storage_supported_cb(int storage_id, storage_type_e type,
513 storage_state_e state, const char *path, void *user_data)
515 MMPlayerStorageInfo *storage_info = (MMPlayerStorageInfo *)user_data;
517 MMPLAYER_RETURN_VAL_IF_FAIL(storage_info, FALSE);
519 if (type == storage_info->type && strstr(storage_info->path, path)) {
520 storage_info->id = storage_id;
521 storage_info->state = state;
529 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);