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"
36 #include "media_format.h"
38 #define MEDIA_PATH_EXTERNAL tzplatform_getenv(TZ_SYS_STORAGE) /* external storage, sd card, usb */
39 #define FD_NUM_FOR_DEBUG 10
42 _mmplayer_get_stream_type_name(int type)
45 case MM_PLAYER_STREAM_TYPE_AUDIO:
47 case MM_PLAYER_STREAM_TYPE_VIDEO:
49 case MM_PLAYER_STREAM_TYPE_TEXT:
51 case MM_PLAYER_STREAM_TYPE_DEFAULT: /* fall through */
52 case MM_PLAYER_STREAM_TYPE_MAX: /* fall through */
59 _mmplayer_get_state_name(int state)
62 case MM_PLAYER_STATE_NULL:
64 case MM_PLAYER_STATE_READY:
66 case MM_PLAYER_STATE_PAUSED:
68 case MM_PLAYER_STATE_PLAYING:
70 case MM_PLAYER_STATE_NONE:
78 _mmplayer_is_rtsp_streaming(mmplayer_t *player)
80 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
82 return (player->profile.uri_type == MM_PLAYER_URI_TYPE_URL_RTSP) ? TRUE : FALSE;
86 _mmplayer_is_http_streaming(mmplayer_t *player)
88 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
90 return (player->profile.uri_type == MM_PLAYER_URI_TYPE_URL_HTTP) ? TRUE : FALSE;
94 _mmplayer_is_streaming(mmplayer_t *player)
96 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
98 return (_mmplayer_is_rtsp_streaming(player) || _mmplayer_is_http_streaming(player)
99 || _mmplayer_is_http_live_streaming(player) || _mmplayer_is_dash_streaming(player)
100 || _mmplayer_is_smooth_streaming(player)) ? TRUE : FALSE;
104 _mmplayer_is_live_streaming(mmplayer_t *player)
106 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
108 return (_mmplayer_is_rtsp_streaming(player) && player->streaming_type == STREAMING_SERVICE_LIVE) ? TRUE : FALSE;
112 _mmplayer_is_http_live_streaming(mmplayer_t *player)
114 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
116 return (player->profile.uri_type == MM_PLAYER_URI_TYPE_HLS) ? TRUE : FALSE;
120 _mmplayer_is_dash_streaming(mmplayer_t *player)
122 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
124 return (player->profile.uri_type == MM_PLAYER_URI_TYPE_DASH) ? TRUE : FALSE;
128 _mmplayer_is_smooth_streaming(mmplayer_t *player)
130 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
132 return (player->profile.uri_type == MM_PLAYER_URI_TYPE_SS) ? TRUE : FALSE;
136 _mmplayer_is_ms_buff_src(mmplayer_t *player)
138 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
140 return (player->profile.uri_type == MM_PLAYER_URI_TYPE_MS_BUFF) ? TRUE : FALSE;
144 _mmplayer_has_suffix(mmplayer_t *player, const gchar *suffix)
146 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
147 MMPLAYER_RETURN_VAL_IF_FAIL(suffix, FALSE);
149 gboolean ret = FALSE;
150 gchar *t_url = g_ascii_strdown(player->profile.uri, -1);
151 gchar *t_suffix = g_ascii_strdown(suffix, -1);
152 gchar *opt = strchr(t_url, '?');
157 ret = g_str_has_suffix(t_url, t_suffix);
159 MMPLAYER_FREEIF(t_url);
160 MMPLAYER_FREEIF(t_suffix);
166 _mmplayer_is_videosink_ready(mmplayer_t *player, int surface_type)
168 int curr_type = MM_DISPLAY_SURFACE_NUM;
172 !player->pipeline->mainbin ||
173 !player->pipeline->videobin ||
174 !player->pipeline->videobin[MMPLAYER_V_BIN].gst ||
175 !player->pipeline->videobin[MMPLAYER_V_SINK].gst) {
176 LOGW("videosink is not ready yet");
180 if (surface_type == MM_DISPLAY_SURFACE_NUM) /* don't care the surface type */
183 mm_attrs_get_int_by_name(player->attrs, "display_surface_type", &curr_type);
185 if (curr_type != surface_type) {
186 LOGW("surface type is mismatched %d, %d", curr_type, surface_type);
194 _mmplayer_post_message(mmplayer_t *player, enum MMMessageType msgtype, MMMessageParamType *param)
196 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
201 LOGD("Message (type : %d) will be posted using msg-cb(%p).", msgtype, player->msg_cb);
204 player->msg_cb(msgtype, param, player->msg_cb_param);
210 _mmplayer_dump_pipeline_state(mmplayer_t *player)
212 GstIterator *iter = NULL;
213 gboolean done = FALSE;
216 GstElement *element = NULL;
217 GstElementFactory *factory = NULL;
219 GstState state = GST_STATE_VOID_PENDING;
220 GstState pending = GST_STATE_VOID_PENDING;
221 GstClockTime time = 200 * GST_MSECOND;
225 MMPLAYER_RETURN_VAL_IF_FAIL(player &&
227 player->pipeline->mainbin,
230 MMPLAYER_GENERATE_DOT_IF_ENABLED(player, "pipeline-status-error-dump");
232 iter = gst_bin_iterate_recurse(GST_BIN(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst));
236 switch (gst_iterator_next(iter, &item)) {
237 case GST_ITERATOR_OK:
238 element = g_value_get_object(&item);
240 gst_element_get_state(element, &state, &pending, time);
242 factory = gst_element_get_factory(element) ;
244 LOGE("%s:%s : From:%s To:%s refcount : %d", GST_OBJECT_NAME(factory) , GST_ELEMENT_NAME(element) ,
245 gst_element_state_get_name(state), gst_element_state_get_name(pending) , GST_OBJECT_REFCOUNT_VALUE(element));
247 g_value_reset(&item);
249 case GST_ITERATOR_RESYNC:
250 gst_iterator_resync(iter);
252 case GST_ITERATOR_ERROR:
255 case GST_ITERATOR_DONE:
262 element = GST_ELEMENT(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst);
264 gst_element_get_state(element, &state, &pending, time);
266 factory = gst_element_get_factory(element) ;
269 LOGE("%s:%s : From:%s To:%s refcount : %d",
270 GST_OBJECT_NAME(factory),
271 GST_ELEMENT_NAME(element),
272 gst_element_state_get_name(state),
273 gst_element_state_get_name(pending),
274 GST_OBJECT_REFCOUNT_VALUE(element));
277 g_value_unset(&item);
280 gst_iterator_free(iter);
288 _mmplayer_exist_file_path(const char *file_path)
291 struct stat stat_results = {0, };
293 if (!file_path || !strlen(file_path))
294 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
296 fd = open(file_path, O_RDONLY);
300 strerror_r(errno, str_error, sizeof(str_error));
301 LOGE("failed to open file by %s (%d)", str_error, errno);
304 return MM_ERROR_PLAYER_PERMISSION_DENIED;
306 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
309 if (fstat(fd, &stat_results) < 0) {
310 LOGE("failed to get file status");
311 } else if (stat_results.st_size == 0) {
312 LOGE("file size is zero");
313 if (fd < FD_NUM_FOR_DEBUG)
314 LOGW("close fd %d", fd);
316 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
318 LOGW("file size : %lld bytes", (long long)stat_results.st_size);
321 if (fd < FD_NUM_FOR_DEBUG)
322 LOGW("close fd %d", fd);
325 return MM_ERROR_NONE;
329 _mmplayer_get_cookie_list(const char *cookies)
331 char **cookie_list = NULL;
335 if (!cookies || !strlen(cookies))
338 SECURE_LOGD("cookies : %zu[bytes] - %s", strlen(cookies), cookies);
340 temp = g_strdup(cookies);
342 /* trimming. it works inplace */
346 cookie_list = g_strsplit(temp, ";", 100);
349 LOGE("failed to get cookie list");
353 for (i = 0; i < g_strv_length(cookie_list); i++) {
354 if (cookie_list[i]) {
355 if (strlen(cookie_list[i])) {
356 g_strstrip(cookie_list[i]);
357 SECURE_LOGD("cookie_list[%d] : %zu[bytes] - %s", i, strlen(cookie_list[i]), cookie_list[i]);
359 cookie_list[i][0] = '\0';
365 MMPLAYER_FREEIF(temp);
370 /* check the given path is indicating sdp file */
372 _mmplayer_is_sdp_file(const char *path)
374 gboolean ret = FALSE;
379 MMPLAYER_RETURN_VAL_IF_FAIL(path, FALSE);
381 uri = g_ascii_strdown(path, -1);
389 /* strlen(".sdp") == 4 */
390 if (strlen(uri) <= 4) {
391 LOGW("path is too short.");
392 MMPLAYER_FREEIF(uri);
396 /* first, check extension name */
397 ret = g_str_has_suffix(uri, "sdp");
399 /* second, if no suffix is there, check it's contents */
401 /* FIXIT : do it soon */
404 MMPLAYER_FREEIF(uri);
410 _mmplayer_get_charset(const char *file_path)
412 UCharsetDetector *ucsd;
413 const UCharsetMatch *ucm;
414 UErrorCode status = U_ZERO_ERROR;
416 const char *charset = NULL;
421 fin = fopen(file_path, "r");
423 SECURE_LOGE("fail to open file %s", file_path);
427 ucsd = ucsdet_open(&status);
428 if (U_FAILURE(status)) {
429 LOGE("fail to ucsdet_open");
433 ucsdet_enableInputFilter(ucsd, TRUE);
435 buf = g_try_malloc(1024 * 1024);
437 LOGE("fail to alloc");
441 n_size = fread(buf, 1, 1024*1024, fin);
442 buf[1024 * 1024 - 1] = '\0';
446 ucsdet_setText(ucsd, buf, strlen(buf), &status);
447 if (U_FAILURE(status)) {
448 LOGE("fail to ucsdet_setText");
452 ucm = ucsdet_detect(ucsd, &status);
453 if (U_FAILURE(status)) {
454 LOGE("fail to ucsdet_detect");
458 charset = ucsdet_getName(ucm, &status);
459 if (U_FAILURE(status)) {
460 LOGE("fail to ucsdet_getName");
464 /* CP949 encoding is an extension of the EUC-KR and it is backwards compatible.*/
465 if (charset && !strcmp(charset, "EUC-KR"))
475 MMPLAYER_FREEIF(buf);
481 _mmplayer_get_pixtype(unsigned int fourcc)
483 int pixtype = MM_PIXEL_FORMAT_INVALID;
486 char *pfourcc = (char *)&fourcc;
488 LOGD("fourcc(%c%c%c%c)",
489 pfourcc[0], pfourcc[1], pfourcc[2], pfourcc[3]);
493 case GST_MAKE_FOURCC('S', 'N', '1', '2'):
494 case GST_MAKE_FOURCC('N', 'V', '1', '2'):
495 pixtype = MM_PIXEL_FORMAT_NV12;
497 case GST_MAKE_FOURCC('S', 'T', '1', '2'):
498 pixtype = MM_PIXEL_FORMAT_NV12T;
500 case GST_MAKE_FOURCC('S', 'N', '2', '1'):
501 case GST_MAKE_FOURCC('N', 'V', '2', '1'):
502 pixtype = MM_PIXEL_FORMAT_NV21;
504 case GST_MAKE_FOURCC('S', 'U', 'Y', 'V'):
505 case GST_MAKE_FOURCC('Y', 'U', 'Y', 'V'):
506 case GST_MAKE_FOURCC('Y', 'U', 'Y', '2'):
507 pixtype = MM_PIXEL_FORMAT_YUYV;
509 case GST_MAKE_FOURCC('S', 'Y', 'V', 'Y'):
510 case GST_MAKE_FOURCC('U', 'Y', 'V', 'Y'):
511 pixtype = MM_PIXEL_FORMAT_UYVY;
513 case GST_MAKE_FOURCC('S', '4', '2', '0'):
514 case GST_MAKE_FOURCC('I', '4', '2', '0'):
515 pixtype = MM_PIXEL_FORMAT_I420;
517 case GST_MAKE_FOURCC('Y', 'V', '1', '2'):
518 pixtype = MM_PIXEL_FORMAT_YV12;
520 case GST_MAKE_FOURCC('4', '2', '2', 'P'):
521 pixtype = MM_PIXEL_FORMAT_422P;
523 case GST_MAKE_FOURCC('R', 'G', 'B', 'P'):
524 pixtype = MM_PIXEL_FORMAT_RGB565;
526 case GST_MAKE_FOURCC('R', 'G', 'B', '3'):
527 pixtype = MM_PIXEL_FORMAT_RGB888;
529 case GST_MAKE_FOURCC('A', 'R', 'G', 'B'):
530 case GST_MAKE_FOURCC('x', 'R', 'G', 'B'):
531 pixtype = MM_PIXEL_FORMAT_ARGB;
533 case GST_MAKE_FOURCC('B', 'G', 'R', 'A'):
534 case GST_MAKE_FOURCC('B', 'G', 'R', 'x'):
535 case GST_MAKE_FOURCC('S', 'R', '3', '2'):
536 pixtype = MM_PIXEL_FORMAT_RGBA;
538 case GST_MAKE_FOURCC('J', 'P', 'E', 'G'):
539 case GST_MAKE_FOURCC('P', 'N', 'G', ' '):
540 pixtype = MM_PIXEL_FORMAT_ENCODED;
542 case GST_MAKE_FOURCC('I', 'T', 'L', 'V'):
543 pixtype = MM_PIXEL_FORMAT_ITLV_JPEG_UYVY;
546 LOGE("Not supported fourcc type(%c%c%c%c)",
547 fourcc, fourcc>>8, fourcc>>16, fourcc>>24);
548 pixtype = MM_PIXEL_FORMAT_INVALID;
556 __mmplayer_storage_supported_cb(int storage_id, storage_type_e type,
557 storage_state_e state, const char *path, void *user_data)
559 mmplayer_storage_info_t *storage_info = (mmplayer_storage_info_t *)user_data;
561 MMPLAYER_RETURN_VAL_IF_FAIL(storage_info, FALSE);
563 if (type == storage_info->type && strstr(storage_info->path, path)) {
564 storage_info->id = storage_id;
565 storage_info->state = state;
573 _mmplayer_get_storage_info(const char *path, mmplayer_storage_info_t *storage_info)
576 const char *file_path = path;
578 MMPLAYER_RETURN_VAL_IF_FAIL(file_path && storage_info, false);
580 if (strncmp(file_path, "file://", strlen("file://")) == 0)
581 file_path = path + 7; /* remove file prefix */
583 if (strncmp(file_path, MEDIA_PATH_EXTERNAL, strlen(MEDIA_PATH_EXTERNAL)) == 0) {
584 storage_info->type = STORAGE_TYPE_EXTERNAL;
586 memset(storage_info->path, 0x00, MM_MAX_URL_LEN);
587 g_snprintf(storage_info->path, MM_MAX_URL_LEN, "%s", file_path);
589 ret = storage_foreach_device_supported((storage_device_supported_cb)__mmplayer_storage_supported_cb, storage_info);
590 if (ret != STORAGE_ERROR_NONE) {
591 LOGE("failed to check supported storage 0x%x", ret);
595 if (storage_info->state <= STORAGE_STATE_REMOVED) {
596 LOGE("need to check the external storage state %d:%d", storage_info->id, storage_info->state);
600 storage_info->type = STORAGE_TYPE_INTERNAL;
601 storage_info->state = STORAGE_STATE_MOUNTED;
604 LOGD("storage info %d:%d:%d", storage_info->type, storage_info->id, storage_info->state);
608 media_format_mimetype_e _mmplayer_convert_audio_pcm_str_to_media_format_mime(const gchar *audio_pcm_str)
610 if (!audio_pcm_str) {
611 LOGW("audio pcm str is NULL");
612 return MEDIA_FORMAT_MAX;
615 if (strstr(audio_pcm_str, "S16LE"))
616 return MEDIA_FORMAT_PCM_S16LE;
617 else if (strstr(audio_pcm_str, "S24LE"))
618 return MEDIA_FORMAT_PCM_S24LE;
619 else if (strstr(audio_pcm_str, "S32LE"))
620 return MEDIA_FORMAT_PCM_S32LE;
621 else if (strstr(audio_pcm_str, "S16BE"))
622 return MEDIA_FORMAT_PCM_S16BE;
623 else if (strstr(audio_pcm_str, "S24BE"))
624 return MEDIA_FORMAT_PCM_S24BE;
625 else if (strstr(audio_pcm_str, "S32BE"))
626 return MEDIA_FORMAT_PCM_S32BE;
627 else if (strstr(audio_pcm_str, "F32LE"))
628 return MEDIA_FORMAT_PCM_F32LE;
629 else if (strstr(audio_pcm_str, "F32BE"))
630 return MEDIA_FORMAT_PCM_F32BE;
631 else if (strstr(audio_pcm_str, "U16LE"))
632 return MEDIA_FORMAT_PCM_U16LE;
633 else if (strstr(audio_pcm_str, "U24LE"))
634 return MEDIA_FORMAT_PCM_U24LE;
635 else if (strstr(audio_pcm_str, "U32LE"))
636 return MEDIA_FORMAT_PCM_U32LE;
637 else if (strstr(audio_pcm_str, "U16BE"))
638 return MEDIA_FORMAT_PCM_U16BE;
639 else if (strstr(audio_pcm_str, "U24BE"))
640 return MEDIA_FORMAT_PCM_U24BE;
641 else if (strstr(audio_pcm_str, "U32BE"))
642 return MEDIA_FORMAT_PCM_U32BE;
644 LOGW("Not supported audio pcm format str : %s", audio_pcm_str);
645 return MEDIA_FORMAT_MAX;
649 gboolean _mmplayer_use_uridecodebin3(mmplayer_t *player) /* MMPLAYER_USE_URIDECODEBIN3(player) */
651 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
653 if (MMPLAYER_IS_RTSP_STREAMING(player) ||
654 MMPLAYER_IS_MS_BUFF_SRC(player))
657 if (!player->ini.use_uridecodebin3)