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 MMPLAYER_GENERATE_DOT_IF_ENABLED(player, "pipeline-status-error-dump");
201 iter = gst_bin_iterate_recurse(GST_BIN(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst));
205 switch (gst_iterator_next(iter, &item)) {
206 case GST_ITERATOR_OK:
207 element = g_value_get_object(&item);
208 gst_element_get_state(element, &state, &pending, time);
210 factory = gst_element_get_factory(element) ;
212 LOGE("%s:%s : From:%s To:%s refcount : %d", GST_OBJECT_NAME(factory) , GST_ELEMENT_NAME(element) ,
213 gst_element_state_get_name(state), gst_element_state_get_name(pending) , GST_OBJECT_REFCOUNT_VALUE(element));
214 g_value_reset(&item);
216 case GST_ITERATOR_RESYNC:
217 gst_iterator_resync(iter);
219 case GST_ITERATOR_ERROR:
222 case GST_ITERATOR_DONE:
229 element = GST_ELEMENT(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst);
231 gst_element_get_state(element, &state, &pending, time);
233 factory = gst_element_get_factory(element) ;
236 LOGE("%s:%s : From:%s To:%s refcount : %d",
237 GST_OBJECT_NAME(factory),
238 GST_ELEMENT_NAME(element),
239 gst_element_state_get_name(state),
240 gst_element_state_get_name(pending),
241 GST_OBJECT_REFCOUNT_VALUE(element));
244 g_value_unset(&item);
247 gst_iterator_free(iter);
255 util_exist_file_path(const char *file_path)
258 struct stat stat_results = {0, };
260 if (!file_path || !strlen(file_path))
261 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
263 fd = open(file_path, O_RDONLY);
267 strerror_r(errno, str_error, sizeof(str_error));
268 LOGE("failed to open file by %s (%d)", str_error, errno);
271 return MM_ERROR_PLAYER_PERMISSION_DENIED;
273 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
276 if (fstat(fd, &stat_results) < 0) {
277 LOGE("failed to get file status");
278 } else if (stat_results.st_size == 0) {
279 LOGE("file size is zero");
281 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
283 LOGW("file size : %lld bytes", (long long)stat_results.st_size);
287 return MM_ERROR_NONE;
291 util_get_cookie_list(const char *cookies)
293 char **cookie_list = NULL;
297 if (!cookies || !strlen(cookies))
300 SECURE_LOGD("cookies : %zu[bytes] - %s", strlen(cookies), cookies);
302 temp = g_strdup(cookies);
304 /* trimming. it works inplace */
308 cookie_list = g_strsplit(temp, ";", 100);
311 LOGE("failed to get cookie list");
315 for (i = 0; i < g_strv_length(cookie_list); i++) {
316 if (cookie_list[i]) {
317 if (strlen(cookie_list[i])) {
318 g_strstrip(cookie_list[i]);
319 SECURE_LOGD("cookie_list[%d] : %zu[bytes] - %s", i, strlen(cookie_list[i]), cookie_list[i]);
321 cookie_list[i][0] = '\0';
327 MMPLAYER_FREEIF(temp);
332 /* check the given path is indicating sdp file */
334 util_is_sdp_file(const char *path)
336 gboolean ret = FALSE;
341 MMPLAYER_RETURN_VAL_IF_FAIL(path, FALSE);
343 uri = g_ascii_strdown(path, -1);
351 /* strlen(".sdp") == 4 */
352 if (strlen(uri) <= 4) {
353 LOGW("path is too short.");
354 MMPLAYER_FREEIF(uri);
358 /* first, check extension name */
359 ret = g_str_has_suffix(uri, "sdp");
361 /* second, if no suffix is there, check it's contents */
363 /* FIXIT : do it soon */
366 MMPLAYER_FREEIF(uri);
372 util_get_charset(const char *file_path)
374 UCharsetDetector *ucsd;
375 const UCharsetMatch *ucm;
376 UErrorCode status = U_ZERO_ERROR;
378 const char *charset = NULL;
383 fin = fopen(file_path, "r");
385 SECURE_LOGE("fail to open file %s", file_path);
389 ucsd = ucsdet_open(&status);
390 if (U_FAILURE(status)) {
391 LOGE("fail to ucsdet_open");
395 ucsdet_enableInputFilter(ucsd, TRUE);
397 buf = g_try_malloc(1024 * 1024);
399 LOGE("fail to alloc");
403 n_size = fread(buf, 1, 1024*1024, fin);
404 buf[1024 * 1024 - 1] = '\0';
408 ucsdet_setText(ucsd, buf, strlen(buf), &status);
409 if (U_FAILURE(status)) {
410 LOGE("fail to ucsdet_setText");
414 ucm = ucsdet_detect(ucsd, &status);
415 if (U_FAILURE(status)) {
416 LOGE("fail to ucsdet_detect");
420 charset = ucsdet_getName(ucm, &status);
421 if (U_FAILURE(status)) {
422 LOGE("fail to ucsdet_getName");
426 /* CP949 encoding is an extension of the EUC-KR and it is backwards compatible.*/
427 if (charset && !strcmp(charset, "EUC-KR"))
437 MMPLAYER_FREEIF(buf);
443 util_get_pixtype(unsigned int fourcc)
445 int pixtype = MM_PIXEL_FORMAT_INVALID;
448 char *pfourcc = (char *)&fourcc;
450 LOGD("fourcc(%c%c%c%c)",
451 pfourcc[0], pfourcc[1], pfourcc[2], pfourcc[3]);
455 case GST_MAKE_FOURCC('S', 'N', '1', '2'):
456 case GST_MAKE_FOURCC('N', 'V', '1', '2'):
457 pixtype = MM_PIXEL_FORMAT_NV12;
459 case GST_MAKE_FOURCC('S', 'T', '1', '2'):
460 pixtype = MM_PIXEL_FORMAT_NV12T;
462 case GST_MAKE_FOURCC('S', 'N', '2', '1'):
463 case GST_MAKE_FOURCC('N', 'V', '2', '1'):
464 pixtype = MM_PIXEL_FORMAT_NV21;
466 case GST_MAKE_FOURCC('S', 'U', 'Y', 'V'):
467 case GST_MAKE_FOURCC('Y', 'U', 'Y', 'V'):
468 case GST_MAKE_FOURCC('Y', 'U', 'Y', '2'):
469 pixtype = MM_PIXEL_FORMAT_YUYV;
471 case GST_MAKE_FOURCC('S', 'Y', 'V', 'Y'):
472 case GST_MAKE_FOURCC('U', 'Y', 'V', 'Y'):
473 pixtype = MM_PIXEL_FORMAT_UYVY;
475 case GST_MAKE_FOURCC('S', '4', '2', '0'):
476 case GST_MAKE_FOURCC('I', '4', '2', '0'):
477 pixtype = MM_PIXEL_FORMAT_I420;
479 case GST_MAKE_FOURCC('Y', 'V', '1', '2'):
480 pixtype = MM_PIXEL_FORMAT_YV12;
482 case GST_MAKE_FOURCC('4', '2', '2', 'P'):
483 pixtype = MM_PIXEL_FORMAT_422P;
485 case GST_MAKE_FOURCC('R', 'G', 'B', 'P'):
486 pixtype = MM_PIXEL_FORMAT_RGB565;
488 case GST_MAKE_FOURCC('R', 'G', 'B', '3'):
489 pixtype = MM_PIXEL_FORMAT_RGB888;
491 case GST_MAKE_FOURCC('A', 'R', 'G', 'B'):
492 case GST_MAKE_FOURCC('x', 'R', 'G', 'B'):
493 pixtype = MM_PIXEL_FORMAT_ARGB;
495 case GST_MAKE_FOURCC('B', 'G', 'R', 'A'):
496 case GST_MAKE_FOURCC('B', 'G', 'R', 'x'):
497 case GST_MAKE_FOURCC('S', 'R', '3', '2'):
498 pixtype = MM_PIXEL_FORMAT_RGBA;
500 case GST_MAKE_FOURCC('J', 'P', 'E', 'G'):
501 case GST_MAKE_FOURCC('P', 'N', 'G', ' '):
502 pixtype = MM_PIXEL_FORMAT_ENCODED;
504 case GST_MAKE_FOURCC('I', 'T', 'L', 'V'):
505 pixtype = MM_PIXEL_FORMAT_ITLV_JPEG_UYVY;
508 LOGE("Not supported fourcc type(%c%c%c%c)",
509 fourcc, fourcc>>8, fourcc>>16, fourcc>>24);
510 pixtype = MM_PIXEL_FORMAT_INVALID;
518 _util_storage_supported_cb(int storage_id, storage_type_e type,
519 storage_state_e state, const char *path, void *user_data)
521 mmplayer_storage_info_t *storage_info = (mmplayer_storage_info_t *)user_data;
523 MMPLAYER_RETURN_VAL_IF_FAIL(storage_info, FALSE);
525 if (type == storage_info->type && strstr(storage_info->path, path)) {
526 storage_info->id = storage_id;
527 storage_info->state = state;
535 util_get_storage_info(const char *path, mmplayer_storage_info_t *storage_info)
538 const char *file_path = path;
540 MMPLAYER_RETURN_VAL_IF_FAIL(file_path && storage_info, false);
542 if (strncmp(file_path, "file://", strlen("file://")) == 0)
543 file_path = path + 7; /* remove file prefix */
545 if (strncmp(file_path, MEDIA_PATH_EXTERNAL, strlen(MEDIA_PATH_EXTERNAL)) == 0) {
546 storage_info->type = STORAGE_TYPE_EXTERNAL;
548 memset(storage_info->path, 0x00, MM_MAX_URL_LEN);
549 g_snprintf(storage_info->path, MM_MAX_URL_LEN, "%s", file_path);
551 ret = storage_foreach_device_supported((storage_device_supported_cb)_util_storage_supported_cb, storage_info);
552 if (ret != STORAGE_ERROR_NONE) {
553 LOGE("failed to check supported storage 0x%x", ret);
557 if (storage_info->state <= STORAGE_STATE_REMOVED) {
558 LOGE("need to check the external storage state %d:%d", storage_info->id, storage_info->state);
562 storage_info->type = STORAGE_TYPE_INTERNAL;
563 storage_info->state = STORAGE_STATE_MOUNTED;
566 LOGD("storage info %d:%d:%d", storage_info->type, storage_info->id, storage_info->state);
570 media_format_mimetype_e util_convert_audio_pcm_str_to_media_format_mime(const gchar *audio_pcm_str)
572 if (!audio_pcm_str) {
573 LOGW("audio pcm str is NULL");
574 return MEDIA_FORMAT_MAX;
577 if (strstr(audio_pcm_str, "S16LE"))
578 return MEDIA_FORMAT_PCM_S16LE;
579 else if (strstr(audio_pcm_str, "S24LE"))
580 return MEDIA_FORMAT_PCM_S24LE;
581 else if (strstr(audio_pcm_str, "S32LE"))
582 return MEDIA_FORMAT_PCM_S32LE;
583 else if (strstr(audio_pcm_str, "S16BE"))
584 return MEDIA_FORMAT_PCM_S16BE;
585 else if (strstr(audio_pcm_str, "S24BE"))
586 return MEDIA_FORMAT_PCM_S24BE;
587 else if (strstr(audio_pcm_str, "S32BE"))
588 return MEDIA_FORMAT_PCM_S32BE;
589 else if (strstr(audio_pcm_str, "F32LE"))
590 return MEDIA_FORMAT_PCM_F32LE;
591 else if (strstr(audio_pcm_str, "F32BE"))
592 return MEDIA_FORMAT_PCM_F32BE;
593 else if (strstr(audio_pcm_str, "U16LE"))
594 return MEDIA_FORMAT_PCM_U16LE;
595 else if (strstr(audio_pcm_str, "U24LE"))
596 return MEDIA_FORMAT_PCM_U24LE;
597 else if (strstr(audio_pcm_str, "U32LE"))
598 return MEDIA_FORMAT_PCM_U32LE;
599 else if (strstr(audio_pcm_str, "U16BE"))
600 return MEDIA_FORMAT_PCM_U16BE;
601 else if (strstr(audio_pcm_str, "U24BE"))
602 return MEDIA_FORMAT_PCM_U24BE;
603 else if (strstr(audio_pcm_str, "U32BE"))
604 return MEDIA_FORMAT_PCM_U32BE;
606 LOGW("Not supported audio pcm format str : %s", audio_pcm_str);
607 return MEDIA_FORMAT_MAX;