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 */
41 _mmplayer_get_stream_type_name(int type)
44 case MM_PLAYER_STREAM_TYPE_AUDIO:
46 case MM_PLAYER_STREAM_TYPE_VIDEO:
48 case MM_PLAYER_STREAM_TYPE_TEXT:
50 case MM_PLAYER_STREAM_TYPE_DEFAULT: /* fall through */
51 case MM_PLAYER_STREAM_TYPE_MAX: /* fall through */
58 _mmplayer_get_state_name(int state)
61 case MM_PLAYER_STATE_NULL:
63 case MM_PLAYER_STATE_READY:
65 case MM_PLAYER_STATE_PAUSED:
67 case MM_PLAYER_STATE_PLAYING:
69 case MM_PLAYER_STATE_NONE:
77 _mmplayer_is_rtsp_streaming(mmplayer_t *player)
79 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
81 return (player->profile.uri_type == MM_PLAYER_URI_TYPE_URL_RTSP) ? TRUE : FALSE;
85 _mmplayer_is_http_streaming(mmplayer_t *player)
87 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
89 return (player->profile.uri_type == MM_PLAYER_URI_TYPE_URL_HTTP) ? TRUE : FALSE;
93 _mmplayer_is_streaming(mmplayer_t *player)
95 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
97 return (_mmplayer_is_rtsp_streaming(player) || _mmplayer_is_http_streaming(player)
98 || _mmplayer_is_http_live_streaming(player) || _mmplayer_is_dash_streaming(player)
99 || _mmplayer_is_smooth_streaming(player)) ? TRUE : FALSE;
103 _mmplayer_is_live_streaming(mmplayer_t *player)
105 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
107 return (_mmplayer_is_rtsp_streaming(player) && player->streaming_type == STREAMING_SERVICE_LIVE) ? TRUE : FALSE;
111 _mmplayer_is_http_live_streaming(mmplayer_t *player)
113 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
115 return (player->profile.uri_type == MM_PLAYER_URI_TYPE_HLS) ? TRUE : FALSE;
119 _mmplayer_is_dash_streaming(mmplayer_t *player)
121 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
123 return (player->profile.uri_type == MM_PLAYER_URI_TYPE_DASH) ? TRUE : FALSE;
127 _mmplayer_is_smooth_streaming(mmplayer_t *player)
129 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
131 return (player->profile.uri_type == MM_PLAYER_URI_TYPE_SS) ? TRUE : FALSE;
135 _mmplayer_is_ms_buff_src(mmplayer_t *player)
137 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
139 return (player->profile.uri_type == MM_PLAYER_URI_TYPE_MS_BUFF) ? TRUE : FALSE;
143 _mmplayer_has_suffix(mmplayer_t *player, const gchar *suffix)
145 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
146 MMPLAYER_RETURN_VAL_IF_FAIL(suffix, FALSE);
148 gboolean ret = FALSE;
149 gchar *t_url = g_ascii_strdown(player->profile.uri, -1);
150 gchar *t_suffix = g_ascii_strdown(suffix, -1);
151 gchar *opt = strchr(t_url, '?');
156 ret = g_str_has_suffix(t_url, t_suffix);
158 MMPLAYER_FREEIF(t_url);
159 MMPLAYER_FREEIF(t_suffix);
165 _mmplayer_post_message(mmplayer_t *player, enum MMMessageType msgtype, MMMessageParamType *param)
167 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
172 //LOGD("Message (type : %d) will be posted using msg-cb(%p).", msgtype, player->msg_cb);
174 player->msg_cb(msgtype, param, player->msg_cb_param);
180 _mmplayer_dump_pipeline_state(mmplayer_t *player)
182 GstIterator *iter = NULL;
183 gboolean done = FALSE;
186 GstElement *element = NULL;
187 GstElementFactory *factory = NULL;
189 GstState state = GST_STATE_VOID_PENDING;
190 GstState pending = GST_STATE_VOID_PENDING;
191 GstClockTime time = 200 * GST_MSECOND;
195 MMPLAYER_RETURN_VAL_IF_FAIL(player &&
197 player->pipeline->mainbin,
200 MMPLAYER_GENERATE_DOT_IF_ENABLED(player, "pipeline-status-error-dump");
202 iter = gst_bin_iterate_recurse(GST_BIN(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst));
206 switch (gst_iterator_next(iter, &item)) {
207 case GST_ITERATOR_OK:
208 element = g_value_get_object(&item);
209 gst_element_get_state(element, &state, &pending, time);
211 factory = gst_element_get_factory(element) ;
213 LOGE("%s:%s : From:%s To:%s refcount : %d", GST_OBJECT_NAME(factory) , GST_ELEMENT_NAME(element) ,
214 gst_element_state_get_name(state), gst_element_state_get_name(pending) , GST_OBJECT_REFCOUNT_VALUE(element));
215 g_value_reset(&item);
217 case GST_ITERATOR_RESYNC:
218 gst_iterator_resync(iter);
220 case GST_ITERATOR_ERROR:
223 case GST_ITERATOR_DONE:
230 element = GST_ELEMENT(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst);
232 gst_element_get_state(element, &state, &pending, time);
234 factory = gst_element_get_factory(element) ;
237 LOGE("%s:%s : From:%s To:%s refcount : %d",
238 GST_OBJECT_NAME(factory),
239 GST_ELEMENT_NAME(element),
240 gst_element_state_get_name(state),
241 gst_element_state_get_name(pending),
242 GST_OBJECT_REFCOUNT_VALUE(element));
245 g_value_unset(&item);
248 gst_iterator_free(iter);
256 _mmplayer_exist_file_path(const char *file_path)
259 struct stat stat_results = {0, };
261 if (!file_path || !strlen(file_path))
262 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
264 fd = open(file_path, O_RDONLY);
268 strerror_r(errno, str_error, sizeof(str_error));
269 LOGE("failed to open file by %s (%d)", str_error, errno);
272 return MM_ERROR_PLAYER_PERMISSION_DENIED;
274 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
277 if (fstat(fd, &stat_results) < 0) {
278 LOGE("failed to get file status");
279 } else if (stat_results.st_size == 0) {
280 LOGE("file size is zero");
282 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
284 LOGW("file size : %lld bytes", (long long)stat_results.st_size);
288 return MM_ERROR_NONE;
292 _mmplayer_get_cookie_list(const char *cookies)
294 char **cookie_list = NULL;
298 if (!cookies || !strlen(cookies))
301 SECURE_LOGD("cookies : %zu[bytes] - %s", strlen(cookies), cookies);
303 temp = g_strdup(cookies);
305 /* trimming. it works inplace */
309 cookie_list = g_strsplit(temp, ";", 100);
312 LOGE("failed to get cookie list");
316 for (i = 0; i < g_strv_length(cookie_list); i++) {
317 if (cookie_list[i]) {
318 if (strlen(cookie_list[i])) {
319 g_strstrip(cookie_list[i]);
320 SECURE_LOGD("cookie_list[%d] : %zu[bytes] - %s", i, strlen(cookie_list[i]), cookie_list[i]);
322 cookie_list[i][0] = '\0';
328 MMPLAYER_FREEIF(temp);
333 /* check the given path is indicating sdp file */
335 _mmplayer_is_sdp_file(const char *path)
337 gboolean ret = FALSE;
342 MMPLAYER_RETURN_VAL_IF_FAIL(path, FALSE);
344 uri = g_ascii_strdown(path, -1);
352 /* strlen(".sdp") == 4 */
353 if (strlen(uri) <= 4) {
354 LOGW("path is too short.");
355 MMPLAYER_FREEIF(uri);
359 /* first, check extension name */
360 ret = g_str_has_suffix(uri, "sdp");
362 /* second, if no suffix is there, check it's contents */
364 /* FIXIT : do it soon */
367 MMPLAYER_FREEIF(uri);
373 _mmplayer_get_charset(const char *file_path)
375 UCharsetDetector *ucsd;
376 const UCharsetMatch *ucm;
377 UErrorCode status = U_ZERO_ERROR;
379 const char *charset = NULL;
384 fin = fopen(file_path, "r");
386 SECURE_LOGE("fail to open file %s", file_path);
390 ucsd = ucsdet_open(&status);
391 if (U_FAILURE(status)) {
392 LOGE("fail to ucsdet_open");
396 ucsdet_enableInputFilter(ucsd, TRUE);
398 buf = g_try_malloc(1024 * 1024);
400 LOGE("fail to alloc");
404 n_size = fread(buf, 1, 1024*1024, fin);
405 buf[1024 * 1024 - 1] = '\0';
409 ucsdet_setText(ucsd, buf, strlen(buf), &status);
410 if (U_FAILURE(status)) {
411 LOGE("fail to ucsdet_setText");
415 ucm = ucsdet_detect(ucsd, &status);
416 if (U_FAILURE(status)) {
417 LOGE("fail to ucsdet_detect");
421 charset = ucsdet_getName(ucm, &status);
422 if (U_FAILURE(status)) {
423 LOGE("fail to ucsdet_getName");
427 /* CP949 encoding is an extension of the EUC-KR and it is backwards compatible.*/
428 if (charset && !strcmp(charset, "EUC-KR"))
438 MMPLAYER_FREEIF(buf);
444 _mmplayer_get_pixtype(unsigned int fourcc)
446 int pixtype = MM_PIXEL_FORMAT_INVALID;
449 char *pfourcc = (char *)&fourcc;
451 LOGD("fourcc(%c%c%c%c)",
452 pfourcc[0], pfourcc[1], pfourcc[2], pfourcc[3]);
456 case GST_MAKE_FOURCC('S', 'N', '1', '2'):
457 case GST_MAKE_FOURCC('N', 'V', '1', '2'):
458 pixtype = MM_PIXEL_FORMAT_NV12;
460 case GST_MAKE_FOURCC('S', 'T', '1', '2'):
461 pixtype = MM_PIXEL_FORMAT_NV12T;
463 case GST_MAKE_FOURCC('S', 'N', '2', '1'):
464 case GST_MAKE_FOURCC('N', 'V', '2', '1'):
465 pixtype = MM_PIXEL_FORMAT_NV21;
467 case GST_MAKE_FOURCC('S', 'U', 'Y', 'V'):
468 case GST_MAKE_FOURCC('Y', 'U', 'Y', 'V'):
469 case GST_MAKE_FOURCC('Y', 'U', 'Y', '2'):
470 pixtype = MM_PIXEL_FORMAT_YUYV;
472 case GST_MAKE_FOURCC('S', 'Y', 'V', 'Y'):
473 case GST_MAKE_FOURCC('U', 'Y', 'V', 'Y'):
474 pixtype = MM_PIXEL_FORMAT_UYVY;
476 case GST_MAKE_FOURCC('S', '4', '2', '0'):
477 case GST_MAKE_FOURCC('I', '4', '2', '0'):
478 pixtype = MM_PIXEL_FORMAT_I420;
480 case GST_MAKE_FOURCC('Y', 'V', '1', '2'):
481 pixtype = MM_PIXEL_FORMAT_YV12;
483 case GST_MAKE_FOURCC('4', '2', '2', 'P'):
484 pixtype = MM_PIXEL_FORMAT_422P;
486 case GST_MAKE_FOURCC('R', 'G', 'B', 'P'):
487 pixtype = MM_PIXEL_FORMAT_RGB565;
489 case GST_MAKE_FOURCC('R', 'G', 'B', '3'):
490 pixtype = MM_PIXEL_FORMAT_RGB888;
492 case GST_MAKE_FOURCC('A', 'R', 'G', 'B'):
493 case GST_MAKE_FOURCC('x', 'R', 'G', 'B'):
494 pixtype = MM_PIXEL_FORMAT_ARGB;
496 case GST_MAKE_FOURCC('B', 'G', 'R', 'A'):
497 case GST_MAKE_FOURCC('B', 'G', 'R', 'x'):
498 case GST_MAKE_FOURCC('S', 'R', '3', '2'):
499 pixtype = MM_PIXEL_FORMAT_RGBA;
501 case GST_MAKE_FOURCC('J', 'P', 'E', 'G'):
502 case GST_MAKE_FOURCC('P', 'N', 'G', ' '):
503 pixtype = MM_PIXEL_FORMAT_ENCODED;
505 case GST_MAKE_FOURCC('I', 'T', 'L', 'V'):
506 pixtype = MM_PIXEL_FORMAT_ITLV_JPEG_UYVY;
509 LOGE("Not supported fourcc type(%c%c%c%c)",
510 fourcc, fourcc>>8, fourcc>>16, fourcc>>24);
511 pixtype = MM_PIXEL_FORMAT_INVALID;
519 __mmplayer_storage_supported_cb(int storage_id, storage_type_e type,
520 storage_state_e state, const char *path, void *user_data)
522 mmplayer_storage_info_t *storage_info = (mmplayer_storage_info_t *)user_data;
524 MMPLAYER_RETURN_VAL_IF_FAIL(storage_info, FALSE);
526 if (type == storage_info->type && strstr(storage_info->path, path)) {
527 storage_info->id = storage_id;
528 storage_info->state = state;
536 _mmplayer_get_storage_info(const char *path, mmplayer_storage_info_t *storage_info)
539 const char *file_path = path;
541 MMPLAYER_RETURN_VAL_IF_FAIL(file_path && storage_info, false);
543 if (strncmp(file_path, "file://", strlen("file://")) == 0)
544 file_path = path + 7; /* remove file prefix */
546 if (strncmp(file_path, MEDIA_PATH_EXTERNAL, strlen(MEDIA_PATH_EXTERNAL)) == 0) {
547 storage_info->type = STORAGE_TYPE_EXTERNAL;
549 memset(storage_info->path, 0x00, MM_MAX_URL_LEN);
550 g_snprintf(storage_info->path, MM_MAX_URL_LEN, "%s", file_path);
552 ret = storage_foreach_device_supported((storage_device_supported_cb)__mmplayer_storage_supported_cb, storage_info);
553 if (ret != STORAGE_ERROR_NONE) {
554 LOGE("failed to check supported storage 0x%x", ret);
558 if (storage_info->state <= STORAGE_STATE_REMOVED) {
559 LOGE("need to check the external storage state %d:%d", storage_info->id, storage_info->state);
563 storage_info->type = STORAGE_TYPE_INTERNAL;
564 storage_info->state = STORAGE_STATE_MOUNTED;
567 LOGD("storage info %d:%d:%d", storage_info->type, storage_info->id, storage_info->state);
571 media_format_mimetype_e _mmplayer_convert_audio_pcm_str_to_media_format_mime(const gchar *audio_pcm_str)
573 if (!audio_pcm_str) {
574 LOGW("audio pcm str is NULL");
575 return MEDIA_FORMAT_MAX;
578 if (strstr(audio_pcm_str, "S16LE"))
579 return MEDIA_FORMAT_PCM_S16LE;
580 else if (strstr(audio_pcm_str, "S24LE"))
581 return MEDIA_FORMAT_PCM_S24LE;
582 else if (strstr(audio_pcm_str, "S32LE"))
583 return MEDIA_FORMAT_PCM_S32LE;
584 else if (strstr(audio_pcm_str, "S16BE"))
585 return MEDIA_FORMAT_PCM_S16BE;
586 else if (strstr(audio_pcm_str, "S24BE"))
587 return MEDIA_FORMAT_PCM_S24BE;
588 else if (strstr(audio_pcm_str, "S32BE"))
589 return MEDIA_FORMAT_PCM_S32BE;
590 else if (strstr(audio_pcm_str, "F32LE"))
591 return MEDIA_FORMAT_PCM_F32LE;
592 else if (strstr(audio_pcm_str, "F32BE"))
593 return MEDIA_FORMAT_PCM_F32BE;
594 else if (strstr(audio_pcm_str, "U16LE"))
595 return MEDIA_FORMAT_PCM_U16LE;
596 else if (strstr(audio_pcm_str, "U24LE"))
597 return MEDIA_FORMAT_PCM_U24LE;
598 else if (strstr(audio_pcm_str, "U32LE"))
599 return MEDIA_FORMAT_PCM_U32LE;
600 else if (strstr(audio_pcm_str, "U16BE"))
601 return MEDIA_FORMAT_PCM_U16BE;
602 else if (strstr(audio_pcm_str, "U24BE"))
603 return MEDIA_FORMAT_PCM_U24BE;
604 else if (strstr(audio_pcm_str, "U32BE"))
605 return MEDIA_FORMAT_PCM_U32BE;
607 LOGW("Not supported audio pcm format str : %s", audio_pcm_str);
608 return MEDIA_FORMAT_MAX;