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
40 #define CMD_LOCK_TICKET_MAX (G_MAXUINT - 1)
43 _mmplayer_get_stream_type_name(int type)
46 case MM_PLAYER_STREAM_TYPE_AUDIO:
48 case MM_PLAYER_STREAM_TYPE_VIDEO:
50 case MM_PLAYER_STREAM_TYPE_TEXT:
52 case MM_PLAYER_STREAM_TYPE_DEFAULT: /* fall through */
53 case MM_PLAYER_STREAM_TYPE_MAX: /* fall through */
60 _mmplayer_get_state_name(int state)
63 case MM_PLAYER_STATE_NULL:
65 case MM_PLAYER_STATE_READY:
67 case MM_PLAYER_STATE_PAUSED:
69 case MM_PLAYER_STATE_PLAYING:
71 case MM_PLAYER_STATE_NONE:
79 _mmplayer_is_rtsp_streaming(mmplayer_t *player)
81 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
83 return (player->profile.uri_type == MM_PLAYER_URI_TYPE_URL_RTSP) ? TRUE : FALSE;
87 _mmplayer_is_http_streaming(mmplayer_t *player)
89 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
91 return (player->profile.uri_type == MM_PLAYER_URI_TYPE_URL_HTTP) ? TRUE : FALSE;
95 _mmplayer_is_streaming(mmplayer_t *player)
97 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
99 return (_mmplayer_is_rtsp_streaming(player) || _mmplayer_is_http_streaming(player)
100 || _mmplayer_is_http_live_streaming(player) || _mmplayer_is_dash_streaming(player)
101 || _mmplayer_is_smooth_streaming(player)) ? TRUE : FALSE;
105 _mmplayer_is_live_streaming(mmplayer_t *player)
107 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
109 return (_mmplayer_is_rtsp_streaming(player) && player->streaming_type == STREAMING_SERVICE_LIVE) ? TRUE : FALSE;
113 _mmplayer_is_http_live_streaming(mmplayer_t *player)
115 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
117 return (player->profile.uri_type == MM_PLAYER_URI_TYPE_HLS) ? TRUE : FALSE;
121 _mmplayer_is_dash_streaming(mmplayer_t *player)
123 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
125 return (player->profile.uri_type == MM_PLAYER_URI_TYPE_DASH) ? TRUE : FALSE;
129 _mmplayer_is_smooth_streaming(mmplayer_t *player)
131 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
133 return (player->profile.uri_type == MM_PLAYER_URI_TYPE_SS) ? TRUE : FALSE;
137 _mmplayer_is_ms_buff_src(mmplayer_t *player)
139 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
141 return (player->profile.uri_type == MM_PLAYER_URI_TYPE_MS_BUFF) ? TRUE : FALSE;
145 _mmplayer_has_suffix(mmplayer_t *player, const gchar *suffix)
147 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
148 MMPLAYER_RETURN_VAL_IF_FAIL(suffix, FALSE);
150 gboolean ret = FALSE;
151 gchar *t_url = g_ascii_strdown(player->profile.uri, -1);
152 gchar *t_suffix = g_ascii_strdown(suffix, -1);
153 gchar *opt = strchr(t_url, '?');
158 ret = g_str_has_suffix(t_url, t_suffix);
160 MMPLAYER_FREEIF(t_url);
161 MMPLAYER_FREEIF(t_suffix);
167 _mmplayer_is_videosink_ready(mmplayer_t *player, int surface_type)
169 int curr_type = MM_DISPLAY_SURFACE_NUM;
173 !player->pipeline->mainbin ||
174 !player->pipeline->videobin ||
175 !player->pipeline->videobin[MMPLAYER_V_BIN].gst ||
176 !player->pipeline->videobin[MMPLAYER_V_SINK].gst) {
177 LOGW("videosink is not ready yet");
181 if (surface_type == MM_DISPLAY_SURFACE_NUM) /* don't care the surface type */
184 mm_attrs_get_int_by_name(player->attrs, "display_surface_type", &curr_type);
186 if (curr_type != surface_type) {
187 LOGW("surface type is mismatched %d, %d", curr_type, surface_type);
195 _mmplayer_post_message(mmplayer_t *player, enum MMMessageType msgtype, MMMessageParamType *param)
197 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
202 LOGD("Message (type : %d) will be posted using msg-cb(%p).", msgtype, player->msg_cb);
205 player->msg_cb(msgtype, param, player->msg_cb_param);
211 _mmplayer_dump_pipeline_state(mmplayer_t *player)
213 GstIterator *iter = NULL;
214 gboolean done = FALSE;
217 GstElement *element = NULL;
218 GstElementFactory *factory = NULL;
220 GstState state = GST_STATE_VOID_PENDING;
221 GstState pending = GST_STATE_VOID_PENDING;
222 GstClockTime time = 200 * GST_MSECOND;
226 MMPLAYER_RETURN_VAL_IF_FAIL(player &&
228 player->pipeline->mainbin,
231 MMPLAYER_GENERATE_DOT_IF_ENABLED(player, "pipeline-status-error-dump");
233 iter = gst_bin_iterate_recurse(GST_BIN(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst));
237 switch (gst_iterator_next(iter, &item)) {
238 case GST_ITERATOR_OK:
239 element = g_value_get_object(&item);
241 gst_element_get_state(element, &state, &pending, time);
243 factory = gst_element_get_factory(element) ;
245 LOGE("%s:%s : From:%s To:%s refcount : %d", GST_OBJECT_NAME(factory) , GST_ELEMENT_NAME(element) ,
246 gst_element_state_get_name(state), gst_element_state_get_name(pending) , GST_OBJECT_REFCOUNT_VALUE(element));
248 g_value_reset(&item);
250 case GST_ITERATOR_RESYNC:
251 gst_iterator_resync(iter);
253 case GST_ITERATOR_ERROR:
256 case GST_ITERATOR_DONE:
263 element = GST_ELEMENT(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst);
265 gst_element_get_state(element, &state, &pending, time);
267 factory = gst_element_get_factory(element) ;
270 LOGE("%s:%s : From:%s To:%s refcount : %d",
271 GST_OBJECT_NAME(factory),
272 GST_ELEMENT_NAME(element),
273 gst_element_state_get_name(state),
274 gst_element_state_get_name(pending),
275 GST_OBJECT_REFCOUNT_VALUE(element));
278 g_value_unset(&item);
281 gst_iterator_free(iter);
289 _mmplayer_exist_file_path(const char *file_path)
292 struct stat stat_results = {0, };
294 if (!file_path || !strlen(file_path))
295 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
297 fd = open(file_path, O_RDONLY);
301 strerror_r(errno, str_error, sizeof(str_error));
302 LOGE("failed to open file by %s (%d)", str_error, errno);
305 return MM_ERROR_PLAYER_PERMISSION_DENIED;
307 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
310 if (fstat(fd, &stat_results) < 0) {
311 LOGE("failed to get file status");
312 } else if (stat_results.st_size == 0) {
313 LOGE("file size is zero");
314 if (fd < FD_NUM_FOR_DEBUG)
315 LOGW("close fd %d", fd);
317 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
319 LOGW("file size : %lld bytes", (long long)stat_results.st_size);
322 if (fd < FD_NUM_FOR_DEBUG)
323 LOGW("close fd %d", fd);
326 return MM_ERROR_NONE;
330 _mmplayer_get_cookie_list(const char *cookies)
332 char **cookie_list = NULL;
336 if (!cookies || !strlen(cookies))
339 SECURE_LOGD("cookies : %zu[bytes] - %s", strlen(cookies), cookies);
341 temp = g_strdup(cookies);
343 /* trimming. it works inplace */
347 cookie_list = g_strsplit(temp, ";", 100);
350 LOGE("failed to get cookie list");
354 for (i = 0; i < g_strv_length(cookie_list); i++) {
355 if (cookie_list[i]) {
356 if (strlen(cookie_list[i])) {
357 g_strstrip(cookie_list[i]);
358 SECURE_LOGD("cookie_list[%d] : %zu[bytes] - %s", i, strlen(cookie_list[i]), cookie_list[i]);
360 cookie_list[i][0] = '\0';
366 MMPLAYER_FREEIF(temp);
371 /* check the given path is indicating sdp file */
373 _mmplayer_is_sdp_file(const char *path)
375 gboolean ret = FALSE;
380 MMPLAYER_RETURN_VAL_IF_FAIL(path, FALSE);
382 uri = g_ascii_strdown(path, -1);
390 /* strlen(".sdp") == 4 */
391 if (strlen(uri) <= 4) {
392 LOGW("path is too short.");
393 MMPLAYER_FREEIF(uri);
397 /* first, check extension name */
398 ret = g_str_has_suffix(uri, "sdp");
400 /* second, if no suffix is there, check it's contents */
402 /* FIXIT : do it soon */
405 MMPLAYER_FREEIF(uri);
411 _mmplayer_get_charset(const char *file_path)
413 UCharsetDetector *ucsd;
414 const UCharsetMatch *ucm;
415 UErrorCode status = U_ZERO_ERROR;
417 const char *charset = NULL;
422 fin = fopen(file_path, "r");
424 SECURE_LOGE("fail to open file %s", file_path);
428 ucsd = ucsdet_open(&status);
429 if (U_FAILURE(status)) {
430 LOGE("fail to ucsdet_open");
434 ucsdet_enableInputFilter(ucsd, TRUE);
436 buf = g_try_malloc(1024 * 1024);
438 LOGE("fail to alloc");
442 n_size = fread(buf, 1, 1024*1024, fin);
443 buf[1024 * 1024 - 1] = '\0';
447 ucsdet_setText(ucsd, buf, strlen(buf), &status);
448 if (U_FAILURE(status)) {
449 LOGE("fail to ucsdet_setText");
453 ucm = ucsdet_detect(ucsd, &status);
454 if (U_FAILURE(status)) {
455 LOGE("fail to ucsdet_detect");
459 charset = ucsdet_getName(ucm, &status);
460 if (U_FAILURE(status)) {
461 LOGE("fail to ucsdet_getName");
465 /* CP949 encoding is an extension of the EUC-KR and it is backwards compatible.*/
466 if (charset && !strcmp(charset, "EUC-KR"))
476 MMPLAYER_FREEIF(buf);
482 _mmplayer_get_pixtype(unsigned int fourcc)
484 int pixtype = MM_PIXEL_FORMAT_INVALID;
487 char *pfourcc = (char *)&fourcc;
489 LOGD("fourcc(%c%c%c%c)",
490 pfourcc[0], pfourcc[1], pfourcc[2], pfourcc[3]);
494 case GST_MAKE_FOURCC('S', 'N', '1', '2'):
495 case GST_MAKE_FOURCC('N', 'V', '1', '2'):
496 pixtype = MM_PIXEL_FORMAT_NV12;
498 case GST_MAKE_FOURCC('S', 'T', '1', '2'):
499 pixtype = MM_PIXEL_FORMAT_NV12T;
501 case GST_MAKE_FOURCC('S', 'N', '2', '1'):
502 case GST_MAKE_FOURCC('N', 'V', '2', '1'):
503 pixtype = MM_PIXEL_FORMAT_NV21;
505 case GST_MAKE_FOURCC('S', 'U', 'Y', 'V'):
506 case GST_MAKE_FOURCC('Y', 'U', 'Y', 'V'):
507 case GST_MAKE_FOURCC('Y', 'U', 'Y', '2'):
508 pixtype = MM_PIXEL_FORMAT_YUYV;
510 case GST_MAKE_FOURCC('S', 'Y', 'V', 'Y'):
511 case GST_MAKE_FOURCC('U', 'Y', 'V', 'Y'):
512 pixtype = MM_PIXEL_FORMAT_UYVY;
514 case GST_MAKE_FOURCC('S', '4', '2', '0'):
515 case GST_MAKE_FOURCC('I', '4', '2', '0'):
516 pixtype = MM_PIXEL_FORMAT_I420;
518 case GST_MAKE_FOURCC('Y', 'V', '1', '2'):
519 pixtype = MM_PIXEL_FORMAT_YV12;
521 case GST_MAKE_FOURCC('4', '2', '2', 'P'):
522 pixtype = MM_PIXEL_FORMAT_422P;
524 case GST_MAKE_FOURCC('R', 'G', 'B', 'P'):
525 pixtype = MM_PIXEL_FORMAT_RGB565;
527 case GST_MAKE_FOURCC('R', 'G', 'B', '3'):
528 pixtype = MM_PIXEL_FORMAT_RGB888;
530 case GST_MAKE_FOURCC('A', 'R', 'G', 'B'):
531 case GST_MAKE_FOURCC('x', 'R', 'G', 'B'):
532 pixtype = MM_PIXEL_FORMAT_ARGB;
534 case GST_MAKE_FOURCC('B', 'G', 'R', 'A'):
535 case GST_MAKE_FOURCC('B', 'G', 'R', 'x'):
536 case GST_MAKE_FOURCC('S', 'R', '3', '2'):
537 pixtype = MM_PIXEL_FORMAT_RGBA;
539 case GST_MAKE_FOURCC('J', 'P', 'E', 'G'):
540 case GST_MAKE_FOURCC('P', 'N', 'G', ' '):
541 pixtype = MM_PIXEL_FORMAT_ENCODED;
543 case GST_MAKE_FOURCC('I', 'T', 'L', 'V'):
544 pixtype = MM_PIXEL_FORMAT_ITLV_JPEG_UYVY;
547 LOGE("Not supported fourcc type(%c%c%c%c)",
548 fourcc, fourcc>>8, fourcc>>16, fourcc>>24);
549 pixtype = MM_PIXEL_FORMAT_INVALID;
557 __mmplayer_storage_supported_cb(int storage_id, storage_type_e type,
558 storage_state_e state, const char *path, void *user_data)
560 mmplayer_storage_info_t *storage_info = (mmplayer_storage_info_t *)user_data;
562 MMPLAYER_RETURN_VAL_IF_FAIL(storage_info, FALSE);
564 if (type == storage_info->type && strstr(storage_info->path, path)) {
565 storage_info->id = storage_id;
566 storage_info->state = state;
574 _mmplayer_get_storage_info(const char *path, mmplayer_storage_info_t *storage_info)
577 const char *file_path = path;
579 MMPLAYER_RETURN_VAL_IF_FAIL(file_path && storage_info, false);
581 if (strncmp(file_path, "file://", strlen("file://")) == 0)
582 file_path = path + 7; /* remove file prefix */
584 if (strncmp(file_path, MEDIA_PATH_EXTERNAL, strlen(MEDIA_PATH_EXTERNAL)) == 0) {
585 storage_info->type = STORAGE_TYPE_EXTERNAL;
587 memset(storage_info->path, 0x00, MM_MAX_URL_LEN);
588 g_snprintf(storage_info->path, MM_MAX_URL_LEN, "%s", file_path);
590 ret = storage_foreach_device_supported((storage_device_supported_cb)__mmplayer_storage_supported_cb, storage_info);
591 if (ret != STORAGE_ERROR_NONE) {
592 LOGE("failed to check supported storage 0x%x", ret);
596 if (storage_info->state <= STORAGE_STATE_REMOVED) {
597 LOGE("need to check the external storage state %d:%d", storage_info->id, storage_info->state);
601 storage_info->type = STORAGE_TYPE_INTERNAL;
602 storage_info->state = STORAGE_STATE_MOUNTED;
605 LOGD("storage info %d:%d:%d", storage_info->type, storage_info->id, storage_info->state);
609 media_format_mimetype_e _mmplayer_convert_audio_pcm_str_to_media_format_mime(const gchar *audio_pcm_str)
611 if (!audio_pcm_str) {
612 LOGW("audio pcm str is NULL");
613 return MEDIA_FORMAT_MAX;
616 if (strstr(audio_pcm_str, "S16LE"))
617 return MEDIA_FORMAT_PCM_S16LE;
618 else if (strstr(audio_pcm_str, "S24LE"))
619 return MEDIA_FORMAT_PCM_S24LE;
620 else if (strstr(audio_pcm_str, "S32LE"))
621 return MEDIA_FORMAT_PCM_S32LE;
622 else if (strstr(audio_pcm_str, "S16BE"))
623 return MEDIA_FORMAT_PCM_S16BE;
624 else if (strstr(audio_pcm_str, "S24BE"))
625 return MEDIA_FORMAT_PCM_S24BE;
626 else if (strstr(audio_pcm_str, "S32BE"))
627 return MEDIA_FORMAT_PCM_S32BE;
628 else if (strstr(audio_pcm_str, "F32LE"))
629 return MEDIA_FORMAT_PCM_F32LE;
630 else if (strstr(audio_pcm_str, "F32BE"))
631 return MEDIA_FORMAT_PCM_F32BE;
632 else if (strstr(audio_pcm_str, "U16LE"))
633 return MEDIA_FORMAT_PCM_U16LE;
634 else if (strstr(audio_pcm_str, "U24LE"))
635 return MEDIA_FORMAT_PCM_U24LE;
636 else if (strstr(audio_pcm_str, "U32LE"))
637 return MEDIA_FORMAT_PCM_U32LE;
638 else if (strstr(audio_pcm_str, "U16BE"))
639 return MEDIA_FORMAT_PCM_U16BE;
640 else if (strstr(audio_pcm_str, "U24BE"))
641 return MEDIA_FORMAT_PCM_U24BE;
642 else if (strstr(audio_pcm_str, "U32BE"))
643 return MEDIA_FORMAT_PCM_U32BE;
645 LOGW("Not supported audio pcm format str : %s", audio_pcm_str);
646 return MEDIA_FORMAT_MAX;
650 gboolean _mmplayer_use_decodebin(mmplayer_t *player) /* MMPLAYER_USE_DECODEBIN(player) */
652 int audio_offload = 0;
653 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
655 mm_attrs_get_int_by_name(player->attrs, MM_PLAYER_AUDIO_OFFLOAD, &audio_offload); /* user requirement */
657 if (MMPLAYER_IS_MS_BUFF_SRC(player) || player->ini.use_decodebin || audio_offload)
663 void _mmplayer_cmd_lock_init(mmplayer_t *player)
665 MMPLAYER_RETURN_IF_FAIL(player);
666 player->cmd_lock = g_new0(mmplayer_ticket_lock_t, 1);
667 g_mutex_init(&player->cmd_lock->ticket_mutex);
668 g_cond_init(&player->cmd_lock->ticket_cond);
669 player->cmd_lock->ticket_queue_head = 0;
670 player->cmd_lock->ticket_queue_tail = 0;
673 void _mmplayer_cmd_lock_deinit(mmplayer_t *player)
675 MMPLAYER_RETURN_IF_FAIL(player && player->cmd_lock);
676 g_mutex_clear(&player->cmd_lock->ticket_mutex);
677 g_cond_clear(&player->cmd_lock->ticket_cond);
678 player->cmd_lock->ticket_queue_head = 0;
679 player->cmd_lock->ticket_queue_tail = 0;
680 g_free(player->cmd_lock);
683 void _mmplayer_cmd_lock(mmplayer_t *player)
689 MMPLAYER_RETURN_IF_FAIL(player && player->cmd_lock);
691 cond = &player->cmd_lock->ticket_cond;
692 mutex = &player->cmd_lock->ticket_mutex;
694 queue_me = player->cmd_lock->ticket_queue_tail;
695 player->cmd_lock->ticket_queue_tail = (queue_me + 1) % CMD_LOCK_TICKET_MAX;
696 while (queue_me != player->cmd_lock->ticket_queue_head)
697 g_cond_wait(cond, mutex);
698 g_mutex_unlock(mutex);
701 gboolean _mmplayer_cmd_trylock(mmplayer_t *player)
705 MMPLAYER_RETURN_VAL_IF_FAIL(player && player->cmd_lock, FALSE);
707 mutex = &player->cmd_lock->ticket_mutex;
709 if (!g_mutex_trylock(mutex))
712 if (player->cmd_lock->ticket_queue_tail != player->cmd_lock->ticket_queue_head) {
713 g_mutex_unlock(mutex);
716 g_mutex_unlock(mutex);
718 _mmplayer_cmd_lock(player);
722 void _mmplayer_cmd_unlock(mmplayer_t *player)
727 MMPLAYER_RETURN_IF_FAIL(player && player->cmd_lock);
729 cond = &player->cmd_lock->ticket_cond;
730 mutex = &player->cmd_lock->ticket_mutex;
733 player->cmd_lock->ticket_queue_head =
734 (player->cmd_lock->ticket_queue_head + 1) % CMD_LOCK_TICKET_MAX;
735 g_cond_broadcast(cond);
736 g_mutex_unlock(mutex);