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);
239 gst_element_get_state(element, &state, &pending, time);
241 factory = gst_element_get_factory(element) ;
243 LOGE("%s:%s : From:%s To:%s refcount : %d", GST_OBJECT_NAME(factory) , GST_ELEMENT_NAME(element) ,
244 gst_element_state_get_name(state), gst_element_state_get_name(pending) , GST_OBJECT_REFCOUNT_VALUE(element));
245 g_value_reset(&item);
247 case GST_ITERATOR_RESYNC:
248 gst_iterator_resync(iter);
250 case GST_ITERATOR_ERROR:
253 case GST_ITERATOR_DONE:
260 element = GST_ELEMENT(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst);
262 gst_element_get_state(element, &state, &pending, time);
264 factory = gst_element_get_factory(element) ;
267 LOGE("%s:%s : From:%s To:%s refcount : %d",
268 GST_OBJECT_NAME(factory),
269 GST_ELEMENT_NAME(element),
270 gst_element_state_get_name(state),
271 gst_element_state_get_name(pending),
272 GST_OBJECT_REFCOUNT_VALUE(element));
275 g_value_unset(&item);
278 gst_iterator_free(iter);
286 _mmplayer_exist_file_path(const char *file_path)
289 struct stat stat_results = {0, };
291 if (!file_path || !strlen(file_path))
292 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
294 fd = open(file_path, O_RDONLY);
298 strerror_r(errno, str_error, sizeof(str_error));
299 LOGE("failed to open file by %s (%d)", str_error, errno);
302 return MM_ERROR_PLAYER_PERMISSION_DENIED;
304 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
307 if (fstat(fd, &stat_results) < 0) {
308 LOGE("failed to get file status");
309 } else if (stat_results.st_size == 0) {
310 LOGE("file size is zero");
311 if (fd < FD_NUM_FOR_DEBUG)
312 LOGW("close fd %d", fd);
314 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
316 LOGW("file size : %lld bytes", (long long)stat_results.st_size);
319 if (fd < FD_NUM_FOR_DEBUG)
320 LOGW(("close fd %d", fd);
323 return MM_ERROR_NONE;
327 _mmplayer_get_cookie_list(const char *cookies)
329 char **cookie_list = NULL;
333 if (!cookies || !strlen(cookies))
336 SECURE_LOGD("cookies : %zu[bytes] - %s", strlen(cookies), cookies);
338 temp = g_strdup(cookies);
340 /* trimming. it works inplace */
344 cookie_list = g_strsplit(temp, ";", 100);
347 LOGE("failed to get cookie list");
351 for (i = 0; i < g_strv_length(cookie_list); i++) {
352 if (cookie_list[i]) {
353 if (strlen(cookie_list[i])) {
354 g_strstrip(cookie_list[i]);
355 SECURE_LOGD("cookie_list[%d] : %zu[bytes] - %s", i, strlen(cookie_list[i]), cookie_list[i]);
357 cookie_list[i][0] = '\0';
363 MMPLAYER_FREEIF(temp);
368 /* check the given path is indicating sdp file */
370 _mmplayer_is_sdp_file(const char *path)
372 gboolean ret = FALSE;
377 MMPLAYER_RETURN_VAL_IF_FAIL(path, FALSE);
379 uri = g_ascii_strdown(path, -1);
387 /* strlen(".sdp") == 4 */
388 if (strlen(uri) <= 4) {
389 LOGW("path is too short.");
390 MMPLAYER_FREEIF(uri);
394 /* first, check extension name */
395 ret = g_str_has_suffix(uri, "sdp");
397 /* second, if no suffix is there, check it's contents */
399 /* FIXIT : do it soon */
402 MMPLAYER_FREEIF(uri);
408 _mmplayer_get_charset(const char *file_path)
410 UCharsetDetector *ucsd;
411 const UCharsetMatch *ucm;
412 UErrorCode status = U_ZERO_ERROR;
414 const char *charset = NULL;
419 fin = fopen(file_path, "r");
421 SECURE_LOGE("fail to open file %s", file_path);
425 ucsd = ucsdet_open(&status);
426 if (U_FAILURE(status)) {
427 LOGE("fail to ucsdet_open");
431 ucsdet_enableInputFilter(ucsd, TRUE);
433 buf = g_try_malloc(1024 * 1024);
435 LOGE("fail to alloc");
439 n_size = fread(buf, 1, 1024*1024, fin);
440 buf[1024 * 1024 - 1] = '\0';
444 ucsdet_setText(ucsd, buf, strlen(buf), &status);
445 if (U_FAILURE(status)) {
446 LOGE("fail to ucsdet_setText");
450 ucm = ucsdet_detect(ucsd, &status);
451 if (U_FAILURE(status)) {
452 LOGE("fail to ucsdet_detect");
456 charset = ucsdet_getName(ucm, &status);
457 if (U_FAILURE(status)) {
458 LOGE("fail to ucsdet_getName");
462 /* CP949 encoding is an extension of the EUC-KR and it is backwards compatible.*/
463 if (charset && !strcmp(charset, "EUC-KR"))
473 MMPLAYER_FREEIF(buf);
479 _mmplayer_get_pixtype(unsigned int fourcc)
481 int pixtype = MM_PIXEL_FORMAT_INVALID;
484 char *pfourcc = (char *)&fourcc;
486 LOGD("fourcc(%c%c%c%c)",
487 pfourcc[0], pfourcc[1], pfourcc[2], pfourcc[3]);
491 case GST_MAKE_FOURCC('S', 'N', '1', '2'):
492 case GST_MAKE_FOURCC('N', 'V', '1', '2'):
493 pixtype = MM_PIXEL_FORMAT_NV12;
495 case GST_MAKE_FOURCC('S', 'T', '1', '2'):
496 pixtype = MM_PIXEL_FORMAT_NV12T;
498 case GST_MAKE_FOURCC('S', 'N', '2', '1'):
499 case GST_MAKE_FOURCC('N', 'V', '2', '1'):
500 pixtype = MM_PIXEL_FORMAT_NV21;
502 case GST_MAKE_FOURCC('S', 'U', 'Y', 'V'):
503 case GST_MAKE_FOURCC('Y', 'U', 'Y', 'V'):
504 case GST_MAKE_FOURCC('Y', 'U', 'Y', '2'):
505 pixtype = MM_PIXEL_FORMAT_YUYV;
507 case GST_MAKE_FOURCC('S', 'Y', 'V', 'Y'):
508 case GST_MAKE_FOURCC('U', 'Y', 'V', 'Y'):
509 pixtype = MM_PIXEL_FORMAT_UYVY;
511 case GST_MAKE_FOURCC('S', '4', '2', '0'):
512 case GST_MAKE_FOURCC('I', '4', '2', '0'):
513 pixtype = MM_PIXEL_FORMAT_I420;
515 case GST_MAKE_FOURCC('Y', 'V', '1', '2'):
516 pixtype = MM_PIXEL_FORMAT_YV12;
518 case GST_MAKE_FOURCC('4', '2', '2', 'P'):
519 pixtype = MM_PIXEL_FORMAT_422P;
521 case GST_MAKE_FOURCC('R', 'G', 'B', 'P'):
522 pixtype = MM_PIXEL_FORMAT_RGB565;
524 case GST_MAKE_FOURCC('R', 'G', 'B', '3'):
525 pixtype = MM_PIXEL_FORMAT_RGB888;
527 case GST_MAKE_FOURCC('A', 'R', 'G', 'B'):
528 case GST_MAKE_FOURCC('x', 'R', 'G', 'B'):
529 pixtype = MM_PIXEL_FORMAT_ARGB;
531 case GST_MAKE_FOURCC('B', 'G', 'R', 'A'):
532 case GST_MAKE_FOURCC('B', 'G', 'R', 'x'):
533 case GST_MAKE_FOURCC('S', 'R', '3', '2'):
534 pixtype = MM_PIXEL_FORMAT_RGBA;
536 case GST_MAKE_FOURCC('J', 'P', 'E', 'G'):
537 case GST_MAKE_FOURCC('P', 'N', 'G', ' '):
538 pixtype = MM_PIXEL_FORMAT_ENCODED;
540 case GST_MAKE_FOURCC('I', 'T', 'L', 'V'):
541 pixtype = MM_PIXEL_FORMAT_ITLV_JPEG_UYVY;
544 LOGE("Not supported fourcc type(%c%c%c%c)",
545 fourcc, fourcc>>8, fourcc>>16, fourcc>>24);
546 pixtype = MM_PIXEL_FORMAT_INVALID;
554 __mmplayer_storage_supported_cb(int storage_id, storage_type_e type,
555 storage_state_e state, const char *path, void *user_data)
557 mmplayer_storage_info_t *storage_info = (mmplayer_storage_info_t *)user_data;
559 MMPLAYER_RETURN_VAL_IF_FAIL(storage_info, FALSE);
561 if (type == storage_info->type && strstr(storage_info->path, path)) {
562 storage_info->id = storage_id;
563 storage_info->state = state;
571 _mmplayer_get_storage_info(const char *path, mmplayer_storage_info_t *storage_info)
574 const char *file_path = path;
576 MMPLAYER_RETURN_VAL_IF_FAIL(file_path && storage_info, false);
578 if (strncmp(file_path, "file://", strlen("file://")) == 0)
579 file_path = path + 7; /* remove file prefix */
581 if (strncmp(file_path, MEDIA_PATH_EXTERNAL, strlen(MEDIA_PATH_EXTERNAL)) == 0) {
582 storage_info->type = STORAGE_TYPE_EXTERNAL;
584 memset(storage_info->path, 0x00, MM_MAX_URL_LEN);
585 g_snprintf(storage_info->path, MM_MAX_URL_LEN, "%s", file_path);
587 ret = storage_foreach_device_supported((storage_device_supported_cb)__mmplayer_storage_supported_cb, storage_info);
588 if (ret != STORAGE_ERROR_NONE) {
589 LOGE("failed to check supported storage 0x%x", ret);
593 if (storage_info->state <= STORAGE_STATE_REMOVED) {
594 LOGE("need to check the external storage state %d:%d", storage_info->id, storage_info->state);
598 storage_info->type = STORAGE_TYPE_INTERNAL;
599 storage_info->state = STORAGE_STATE_MOUNTED;
602 LOGD("storage info %d:%d:%d", storage_info->type, storage_info->id, storage_info->state);
606 media_format_mimetype_e _mmplayer_convert_audio_pcm_str_to_media_format_mime(const gchar *audio_pcm_str)
608 if (!audio_pcm_str) {
609 LOGW("audio pcm str is NULL");
610 return MEDIA_FORMAT_MAX;
613 if (strstr(audio_pcm_str, "S16LE"))
614 return MEDIA_FORMAT_PCM_S16LE;
615 else if (strstr(audio_pcm_str, "S24LE"))
616 return MEDIA_FORMAT_PCM_S24LE;
617 else if (strstr(audio_pcm_str, "S32LE"))
618 return MEDIA_FORMAT_PCM_S32LE;
619 else if (strstr(audio_pcm_str, "S16BE"))
620 return MEDIA_FORMAT_PCM_S16BE;
621 else if (strstr(audio_pcm_str, "S24BE"))
622 return MEDIA_FORMAT_PCM_S24BE;
623 else if (strstr(audio_pcm_str, "S32BE"))
624 return MEDIA_FORMAT_PCM_S32BE;
625 else if (strstr(audio_pcm_str, "F32LE"))
626 return MEDIA_FORMAT_PCM_F32LE;
627 else if (strstr(audio_pcm_str, "F32BE"))
628 return MEDIA_FORMAT_PCM_F32BE;
629 else if (strstr(audio_pcm_str, "U16LE"))
630 return MEDIA_FORMAT_PCM_U16LE;
631 else if (strstr(audio_pcm_str, "U24LE"))
632 return MEDIA_FORMAT_PCM_U24LE;
633 else if (strstr(audio_pcm_str, "U32LE"))
634 return MEDIA_FORMAT_PCM_U32LE;
635 else if (strstr(audio_pcm_str, "U16BE"))
636 return MEDIA_FORMAT_PCM_U16BE;
637 else if (strstr(audio_pcm_str, "U24BE"))
638 return MEDIA_FORMAT_PCM_U24BE;
639 else if (strstr(audio_pcm_str, "U32BE"))
640 return MEDIA_FORMAT_PCM_U32BE;
642 LOGW("Not supported audio pcm format str : %s", audio_pcm_str);
643 return MEDIA_FORMAT_MAX;