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 __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 __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 __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 __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 __is_streaming(mmplayer_t *player)
95 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
97 return (__is_rtsp_streaming(player) || __is_http_streaming(player)
98 || __is_http_live_streaming(player) || __is_dash_streaming(player) || __is_smooth_streaming(player)) ? TRUE : FALSE;
102 __is_live_streaming(mmplayer_t *player)
104 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
106 return (__is_rtsp_streaming(player) && player->streaming_type == STREAMING_SERVICE_LIVE) ? TRUE : FALSE;
110 __is_http_live_streaming(mmplayer_t *player)
112 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
114 return (player->profile.uri_type == MM_PLAYER_URI_TYPE_HLS) ? TRUE : FALSE;
118 __is_dash_streaming(mmplayer_t *player)
120 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
122 return (player->profile.uri_type == MM_PLAYER_URI_TYPE_DASH) ? TRUE : FALSE;
126 __is_smooth_streaming(mmplayer_t *player)
128 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
130 return (player->profile.uri_type == MM_PLAYER_URI_TYPE_SS) ? TRUE : FALSE;
134 __is_ms_buff_src(mmplayer_t *player)
136 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
138 return (player->profile.uri_type == MM_PLAYER_URI_TYPE_MS_BUFF) ? TRUE : FALSE;
142 __has_suffix(mmplayer_t *player, const gchar *suffix)
144 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
145 MMPLAYER_RETURN_VAL_IF_FAIL(suffix, FALSE);
147 gboolean ret = FALSE;
148 gchar *t_url = g_ascii_strdown(player->profile.uri, -1);
149 gchar *t_suffix = g_ascii_strdown(suffix, -1);
150 gchar *opt = strchr(t_url, '?');
155 ret = g_str_has_suffix(t_url, t_suffix);
157 MMPLAYER_FREEIF(t_url);
158 MMPLAYER_FREEIF(t_suffix);
164 __mmplayer_post_message(mmplayer_t *player, enum MMMessageType msgtype, MMMessageParamType *param)
166 MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
171 //LOGD("Message (type : %d) will be posted using msg-cb(%p).", msgtype, player->msg_cb);
173 player->msg_cb(msgtype, param, player->msg_cb_param);
179 __mmplayer_dump_pipeline_state(mmplayer_t *player)
181 GstIterator *iter = NULL;
182 gboolean done = FALSE;
185 GstElement *element = NULL;
186 GstElementFactory *factory = NULL;
188 GstState state = GST_STATE_VOID_PENDING;
189 GstState pending = GST_STATE_VOID_PENDING;
190 GstClockTime time = 200 * GST_MSECOND;
194 MMPLAYER_RETURN_VAL_IF_FAIL(player &&
196 player->pipeline->mainbin,
199 iter = gst_bin_iterate_recurse(GST_BIN(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst));
203 switch (gst_iterator_next(iter, &item)) {
204 case GST_ITERATOR_OK:
205 element = g_value_get_object(&item);
206 gst_element_get_state(element, &state, &pending, time);
208 factory = gst_element_get_factory(element) ;
210 LOGE("%s:%s : From:%s To:%s refcount : %d", GST_OBJECT_NAME(factory) , GST_ELEMENT_NAME(element) ,
211 gst_element_state_get_name(state), gst_element_state_get_name(pending) , GST_OBJECT_REFCOUNT_VALUE(element));
212 g_value_reset(&item);
214 case GST_ITERATOR_RESYNC:
215 gst_iterator_resync(iter);
217 case GST_ITERATOR_ERROR:
220 case GST_ITERATOR_DONE:
227 element = GST_ELEMENT(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst);
229 gst_element_get_state(element, &state, &pending, time);
231 factory = gst_element_get_factory(element) ;
234 LOGE("%s:%s : From:%s To:%s refcount : %d",
235 GST_OBJECT_NAME(factory),
236 GST_ELEMENT_NAME(element),
237 gst_element_state_get_name(state),
238 gst_element_state_get_name(pending),
239 GST_OBJECT_REFCOUNT_VALUE(element));
242 g_value_unset(&item);
245 gst_iterator_free(iter);
253 util_exist_file_path(const char *file_path)
256 struct stat stat_results = {0, };
258 if (!file_path || !strlen(file_path))
259 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
261 fd = open(file_path, O_RDONLY);
265 strerror_r(errno, str_error, sizeof(str_error));
266 LOGE("failed to open file by %s (%d)", str_error, errno);
269 return MM_ERROR_PLAYER_PERMISSION_DENIED;
271 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
274 if (fstat(fd, &stat_results) < 0) {
275 LOGE("failed to get file status");
276 } else if (stat_results.st_size == 0) {
277 LOGE("file size is zero");
279 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
281 LOGW("file size : %lld bytes", (long long)stat_results.st_size);
285 return MM_ERROR_NONE;
289 util_get_cookie_list(const char *cookies)
291 char **cookie_list = NULL;
295 if (!cookies || !strlen(cookies))
298 SECURE_LOGD("cookies : %zu[bytes] - %s", strlen(cookies), cookies);
300 temp = g_strdup(cookies);
302 /* trimming. it works inplace */
306 cookie_list = g_strsplit(temp, ";", 100);
309 LOGE("failed to get cookie list");
313 for (i = 0; i < g_strv_length(cookie_list); i++) {
314 if (cookie_list[i]) {
315 if (strlen(cookie_list[i])) {
316 g_strstrip(cookie_list[i]);
317 SECURE_LOGD("cookie_list[%d] : %zu[bytes] - %s", i, strlen(cookie_list[i]), cookie_list[i]);
319 cookie_list[i][0] = '\0';
325 MMPLAYER_FREEIF(temp);
330 /* check the given path is indicating sdp file */
332 util_is_sdp_file(const char *path)
334 gboolean ret = FALSE;
339 MMPLAYER_RETURN_VAL_IF_FAIL(path, FALSE);
341 uri = g_ascii_strdown(path, -1);
349 /* strlen(".sdp") == 4 */
350 if (strlen(uri) <= 4) {
351 LOGW("path is too short.");
352 MMPLAYER_FREEIF(uri);
356 /* first, check extension name */
357 ret = g_str_has_suffix(uri, "sdp");
359 /* second, if no suffix is there, check it's contents */
361 /* FIXIT : do it soon */
364 MMPLAYER_FREEIF(uri);
370 util_get_charset(const char *file_path)
372 UCharsetDetector *ucsd;
373 const UCharsetMatch *ucm;
374 UErrorCode status = U_ZERO_ERROR;
376 const char *charset = NULL;
381 fin = fopen(file_path, "r");
383 SECURE_LOGE("fail to open file %s", file_path);
387 ucsd = ucsdet_open(&status);
388 if (U_FAILURE(status)) {
389 LOGE("fail to ucsdet_open");
393 ucsdet_enableInputFilter(ucsd, TRUE);
395 buf = g_try_malloc(1024 * 1024);
397 LOGE("fail to alloc");
401 n_size = fread(buf, 1, 1024*1024, fin);
402 buf[1024 * 1024 - 1] = '\0';
406 ucsdet_setText(ucsd, buf, strlen(buf), &status);
407 if (U_FAILURE(status)) {
408 LOGE("fail to ucsdet_setText");
412 ucm = ucsdet_detect(ucsd, &status);
413 if (U_FAILURE(status)) {
414 LOGE("fail to ucsdet_detect");
418 charset = ucsdet_getName(ucm, &status);
419 if (U_FAILURE(status)) {
420 LOGE("fail to ucsdet_getName");
424 /* CP949 encoding is an extension of the EUC-KR and it is backwards compatible.*/
425 if (charset && !strcmp(charset, "EUC-KR"))
435 MMPLAYER_FREEIF(buf);
441 util_get_pixtype(unsigned int fourcc)
443 int pixtype = MM_PIXEL_FORMAT_INVALID;
446 char *pfourcc = (char *)&fourcc;
448 LOGD("fourcc(%c%c%c%c)",
449 pfourcc[0], pfourcc[1], pfourcc[2], pfourcc[3]);
453 case GST_MAKE_FOURCC('S', 'N', '1', '2'):
454 case GST_MAKE_FOURCC('N', 'V', '1', '2'):
455 pixtype = MM_PIXEL_FORMAT_NV12;
457 case GST_MAKE_FOURCC('S', 'T', '1', '2'):
458 pixtype = MM_PIXEL_FORMAT_NV12T;
460 case GST_MAKE_FOURCC('S', 'N', '2', '1'):
461 case GST_MAKE_FOURCC('N', 'V', '2', '1'):
462 pixtype = MM_PIXEL_FORMAT_NV21;
464 case GST_MAKE_FOURCC('S', 'U', 'Y', 'V'):
465 case GST_MAKE_FOURCC('Y', 'U', 'Y', 'V'):
466 case GST_MAKE_FOURCC('Y', 'U', 'Y', '2'):
467 pixtype = MM_PIXEL_FORMAT_YUYV;
469 case GST_MAKE_FOURCC('S', 'Y', 'V', 'Y'):
470 case GST_MAKE_FOURCC('U', 'Y', 'V', 'Y'):
471 pixtype = MM_PIXEL_FORMAT_UYVY;
473 case GST_MAKE_FOURCC('S', '4', '2', '0'):
474 case GST_MAKE_FOURCC('I', '4', '2', '0'):
475 pixtype = MM_PIXEL_FORMAT_I420;
477 case GST_MAKE_FOURCC('Y', 'V', '1', '2'):
478 pixtype = MM_PIXEL_FORMAT_YV12;
480 case GST_MAKE_FOURCC('4', '2', '2', 'P'):
481 pixtype = MM_PIXEL_FORMAT_422P;
483 case GST_MAKE_FOURCC('R', 'G', 'B', 'P'):
484 pixtype = MM_PIXEL_FORMAT_RGB565;
486 case GST_MAKE_FOURCC('R', 'G', 'B', '3'):
487 pixtype = MM_PIXEL_FORMAT_RGB888;
489 case GST_MAKE_FOURCC('A', 'R', 'G', 'B'):
490 case GST_MAKE_FOURCC('x', 'R', 'G', 'B'):
491 pixtype = MM_PIXEL_FORMAT_ARGB;
493 case GST_MAKE_FOURCC('B', 'G', 'R', 'A'):
494 case GST_MAKE_FOURCC('B', 'G', 'R', 'x'):
495 case GST_MAKE_FOURCC('S', 'R', '3', '2'):
496 pixtype = MM_PIXEL_FORMAT_RGBA;
498 case GST_MAKE_FOURCC('J', 'P', 'E', 'G'):
499 case GST_MAKE_FOURCC('P', 'N', 'G', ' '):
500 pixtype = MM_PIXEL_FORMAT_ENCODED;
502 case GST_MAKE_FOURCC('I', 'T', 'L', 'V'):
503 pixtype = MM_PIXEL_FORMAT_ITLV_JPEG_UYVY;
506 LOGE("Not supported fourcc type(%c%c%c%c)",
507 fourcc, fourcc>>8, fourcc>>16, fourcc>>24);
508 pixtype = MM_PIXEL_FORMAT_INVALID;
516 _util_storage_supported_cb(int storage_id, storage_type_e type,
517 storage_state_e state, const char *path, void *user_data)
519 mmplayer_storage_info_t *storage_info = (mmplayer_storage_info_t *)user_data;
521 MMPLAYER_RETURN_VAL_IF_FAIL(storage_info, FALSE);
523 if (type == storage_info->type && strstr(storage_info->path, path)) {
524 storage_info->id = storage_id;
525 storage_info->state = state;
533 util_get_storage_info(const char *path, mmplayer_storage_info_t *storage_info)
536 const char *file_path = path;
538 MMPLAYER_RETURN_VAL_IF_FAIL(file_path && storage_info, false);
540 if (strncmp(file_path, "file://", strlen("file://")) == 0)
541 file_path = path + 7; /* remove file prefix */
543 if (strncmp(file_path, MEDIA_PATH_EXTERNAL, strlen(MEDIA_PATH_EXTERNAL)) == 0) {
544 storage_info->type = STORAGE_TYPE_EXTERNAL;
546 memset(storage_info->path, 0x00, MM_MAX_URL_LEN);
547 g_snprintf(storage_info->path, MM_MAX_URL_LEN, "%s", file_path);
549 ret = storage_foreach_device_supported((storage_device_supported_cb)_util_storage_supported_cb, storage_info);
550 if (ret != STORAGE_ERROR_NONE) {
551 LOGE("failed to check supported storage 0x%x", ret);
555 if (storage_info->state <= STORAGE_STATE_REMOVED) {
556 LOGE("need to check the external storage state %d:%d", storage_info->id, storage_info->state);
560 storage_info->type = STORAGE_TYPE_INTERNAL;
561 storage_info->state = STORAGE_STATE_MOUNTED;
564 LOGD("storage info %d:%d:%d", storage_info->type, storage_info->id, storage_info->state);
568 media_format_mimetype_e util_convert_audio_pcm_str_to_media_format_mime(const gchar *audio_pcm_str)
570 if (!audio_pcm_str) {
571 LOGW("audio pcm str is NULL");
572 return MEDIA_FORMAT_MAX;
575 if (strstr(audio_pcm_str, "S16LE"))
576 return MEDIA_FORMAT_PCM_S16LE;
577 else if (strstr(audio_pcm_str, "S24LE"))
578 return MEDIA_FORMAT_PCM_S24LE;
579 else if (strstr(audio_pcm_str, "S32LE"))
580 return MEDIA_FORMAT_PCM_S32LE;
581 else if (strstr(audio_pcm_str, "S16BE"))
582 return MEDIA_FORMAT_PCM_S16BE;
583 else if (strstr(audio_pcm_str, "S24BE"))
584 return MEDIA_FORMAT_PCM_S24BE;
585 else if (strstr(audio_pcm_str, "S32BE"))
586 return MEDIA_FORMAT_PCM_S32BE;
587 else if (strstr(audio_pcm_str, "F32LE"))
588 return MEDIA_FORMAT_PCM_F32LE;
589 else if (strstr(audio_pcm_str, "F32BE"))
590 return MEDIA_FORMAT_PCM_F32BE;
591 else if (strstr(audio_pcm_str, "U16LE"))
592 return MEDIA_FORMAT_PCM_U16LE;
593 else if (strstr(audio_pcm_str, "U24LE"))
594 return MEDIA_FORMAT_PCM_U24LE;
595 else if (strstr(audio_pcm_str, "U32LE"))
596 return MEDIA_FORMAT_PCM_U32LE;
597 else if (strstr(audio_pcm_str, "U16BE"))
598 return MEDIA_FORMAT_PCM_U16BE;
599 else if (strstr(audio_pcm_str, "U24BE"))
600 return MEDIA_FORMAT_PCM_U24BE;
601 else if (strstr(audio_pcm_str, "U32BE"))
602 return MEDIA_FORMAT_PCM_U32BE;
604 LOGW("Not supported audio pcm format str : %s", audio_pcm_str);
605 return MEDIA_FORMAT_MAX;