c226d90176cfb4c8209adac6941c7484002824c4
[platform/core/multimedia/libmm-player.git] / src / mm_player_utils.c
1 /*
2  * libmm-player
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
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>
8  *
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
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
21  */
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <sys/time.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
31 #include <unicode/ucsdet.h>
32 #include <dlog.h>
33 #include <storage.h>
34 #include <tzplatform_config.h>
35 #include "mm_player_utils.h"
36 #include "media_format.h"
37
38 #define MEDIA_PATH_EXTERNAL tzplatform_getenv(TZ_SYS_STORAGE) /* external storage, sd card, usb */
39 #define FD_NUM_FOR_DEBUG 10
40
41 const gchar *
42 _mmplayer_get_stream_type_name(int type)
43 {
44         switch (type) {
45         case MM_PLAYER_STREAM_TYPE_AUDIO:
46                 return "AUDIO";
47         case MM_PLAYER_STREAM_TYPE_VIDEO:
48                 return "VIDEO";
49         case MM_PLAYER_STREAM_TYPE_TEXT:
50                 return "TEXT";
51         case MM_PLAYER_STREAM_TYPE_DEFAULT: /* fall through */
52         case MM_PLAYER_STREAM_TYPE_MAX:     /* fall through */
53         default:
54                 return "INVAID";
55         }
56 }
57
58 const gchar *
59 _mmplayer_get_state_name(int state)
60 {
61         switch (state) {
62         case MM_PLAYER_STATE_NULL:
63                 return "NULL";
64         case MM_PLAYER_STATE_READY:
65                 return "READY";
66         case MM_PLAYER_STATE_PAUSED:
67                 return "PAUSED";
68         case MM_PLAYER_STATE_PLAYING:
69                 return "PLAYING";
70         case MM_PLAYER_STATE_NONE:
71                 return "NONE";
72         default:
73                 return "INVAID";
74         }
75 }
76
77 gboolean
78 _mmplayer_is_rtsp_streaming(mmplayer_t *player)
79 {
80         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
81
82         return (player->profile.uri_type == MM_PLAYER_URI_TYPE_URL_RTSP) ? TRUE : FALSE;
83 }
84
85 gboolean
86 _mmplayer_is_http_streaming(mmplayer_t *player)
87 {
88         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
89
90         return (player->profile.uri_type == MM_PLAYER_URI_TYPE_URL_HTTP) ? TRUE : FALSE;
91 }
92
93 gboolean
94 _mmplayer_is_streaming(mmplayer_t *player)
95 {
96         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
97
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;
101 }
102
103 gboolean
104 _mmplayer_is_live_streaming(mmplayer_t *player)
105 {
106         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
107
108         return (_mmplayer_is_rtsp_streaming(player) && player->streaming_type == STREAMING_SERVICE_LIVE) ? TRUE : FALSE;
109 }
110
111 gboolean
112 _mmplayer_is_http_live_streaming(mmplayer_t *player)
113 {
114         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
115
116         return (player->profile.uri_type == MM_PLAYER_URI_TYPE_HLS) ? TRUE : FALSE;
117 }
118
119 gboolean
120 _mmplayer_is_dash_streaming(mmplayer_t *player)
121 {
122         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
123
124         return (player->profile.uri_type == MM_PLAYER_URI_TYPE_DASH) ? TRUE : FALSE;
125 }
126
127 gboolean
128 _mmplayer_is_smooth_streaming(mmplayer_t *player)
129 {
130         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
131
132         return (player->profile.uri_type == MM_PLAYER_URI_TYPE_SS) ? TRUE : FALSE;
133 }
134
135 gboolean
136 _mmplayer_is_ms_buff_src(mmplayer_t *player)
137 {
138         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
139
140         return (player->profile.uri_type == MM_PLAYER_URI_TYPE_MS_BUFF) ? TRUE : FALSE;
141 }
142
143 gboolean
144 _mmplayer_has_suffix(mmplayer_t *player, const gchar *suffix)
145 {
146         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
147         MMPLAYER_RETURN_VAL_IF_FAIL(suffix, FALSE);
148
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, '?');
153
154         if (opt)
155                 *opt = '\0';
156
157         ret = g_str_has_suffix(t_url, t_suffix);
158
159         MMPLAYER_FREEIF(t_url);
160         MMPLAYER_FREEIF(t_suffix);
161
162         return ret;
163 }
164
165 gboolean
166 _mmplayer_is_videosink_ready(mmplayer_t *player, int surface_type)
167 {
168         int curr_type = MM_DISPLAY_SURFACE_NUM;
169
170         if (!player ||
171                 !player->pipeline ||
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");
177                 return FALSE;
178         }
179
180         if (surface_type == MM_DISPLAY_SURFACE_NUM) /* don't care the surface type */
181                 return TRUE;
182
183         mm_attrs_get_int_by_name(player->attrs, "display_surface_type", &curr_type);
184
185         if (curr_type != surface_type) {
186                 LOGW("surface type is mismatched %d, %d", curr_type, surface_type);
187                 return FALSE;
188         }
189
190         return TRUE;
191 }
192
193 gboolean
194 _mmplayer_post_message(mmplayer_t *player, enum MMMessageType msgtype, MMMessageParamType *param)
195 {
196         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
197
198         if (!player->msg_cb)
199                 return FALSE;
200 #ifdef __DEBUG__
201         LOGD("Message (type : %d)  will be posted using msg-cb(%p).", msgtype, player->msg_cb);
202 #endif
203
204         player->msg_cb(msgtype, param, player->msg_cb_param);
205
206         return TRUE;
207 }
208
209 gboolean
210 _mmplayer_dump_pipeline_state(mmplayer_t *player)
211 {
212         GstIterator *iter = NULL;
213         gboolean done = FALSE;
214
215         GValue item = {0, };
216         GstElement *element = NULL;
217         GstElementFactory *factory = NULL;
218
219         GstState state = GST_STATE_VOID_PENDING;
220         GstState pending = GST_STATE_VOID_PENDING;
221         GstClockTime time = 200 * GST_MSECOND;
222
223         MMPLAYER_FENTER();
224
225         MMPLAYER_RETURN_VAL_IF_FAIL(player &&
226                 player->pipeline &&
227                 player->pipeline->mainbin,
228                 FALSE);
229
230         MMPLAYER_GENERATE_DOT_IF_ENABLED(player, "pipeline-status-error-dump");
231
232         iter = gst_bin_iterate_recurse(GST_BIN(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst));
233
234         if (iter != NULL) {
235                 while (!done) {
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);
240
241                                 factory = gst_element_get_factory(element) ;
242                                 if (factory)
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);
246                                 break;
247                         case GST_ITERATOR_RESYNC:
248                                 gst_iterator_resync(iter);
249                                 break;
250                         case GST_ITERATOR_ERROR:
251                                 done = TRUE;
252                                 break;
253                         case GST_ITERATOR_DONE:
254                                 done = TRUE;
255                                 break;
256                         }
257                 }
258         }
259
260         element = GST_ELEMENT(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst);
261
262         gst_element_get_state(element, &state, &pending, time);
263
264         factory = gst_element_get_factory(element) ;
265
266         if (factory) {
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));
273         }
274
275         g_value_unset(&item);
276
277         if (iter)
278                 gst_iterator_free(iter);
279
280         MMPLAYER_FLEAVE();
281
282         return FALSE;
283 }
284
285 int
286 _mmplayer_exist_file_path(const char *file_path)
287 {
288         int fd = 0;
289         struct stat stat_results = {0, };
290
291         if (!file_path || !strlen(file_path))
292                 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
293
294         fd = open(file_path, O_RDONLY);
295
296         if (fd < 0) {
297                 char str_error[256];
298                 strerror_r(errno, str_error, sizeof(str_error));
299                 LOGE("failed to open file by %s (%d)", str_error, errno);
300
301                 if (EACCES == errno)
302                         return MM_ERROR_PLAYER_PERMISSION_DENIED;
303
304                 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
305         }
306
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);
313                 close(fd);
314                 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
315         } else {
316                 LOGW("file size : %lld bytes", (long long)stat_results.st_size);
317         }
318
319         if (fd < FD_NUM_FOR_DEBUG)
320                 LOGW(("close fd %d", fd);
321         close(fd);
322
323         return MM_ERROR_NONE;
324 }
325
326 char **
327 _mmplayer_get_cookie_list(const char *cookies)
328 {
329         char **cookie_list = NULL;
330         char *temp = NULL;
331         gint i = 0;
332
333         if (!cookies || !strlen(cookies))
334                 return NULL;
335
336         SECURE_LOGD("cookies : %zu[bytes] - %s", strlen(cookies), cookies);
337
338         temp = g_strdup(cookies);
339
340         /* trimming. it works inplace */
341         g_strstrip(temp);
342
343         /* split */
344         cookie_list = g_strsplit(temp, ";", 100);
345
346         if (!cookie_list) {
347                 LOGE("failed to get cookie list");
348                 goto EXIT;
349         }
350
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]);
356                         } else {
357                                 cookie_list[i][0] = '\0';
358                         }
359                 }
360         }
361
362 EXIT:
363         MMPLAYER_FREEIF(temp);
364
365         return cookie_list;
366 }
367
368 /* check the given path is indicating sdp file */
369 bool
370 _mmplayer_is_sdp_file(const char *path)
371 {
372         gboolean ret = FALSE;
373         gchar *uri = NULL;
374
375         MMPLAYER_FENTER();
376
377         MMPLAYER_RETURN_VAL_IF_FAIL(path, FALSE);
378
379         uri = g_ascii_strdown(path, -1);
380
381         if (uri == NULL)
382                 return FALSE;
383
384         /* trimming */
385         g_strstrip(uri);
386
387         /* strlen(".sdp") == 4 */
388         if (strlen(uri) <= 4) {
389                 LOGW("path is too short.");
390                 MMPLAYER_FREEIF(uri);
391                 return ret;
392         }
393
394         /* first, check extension name */
395         ret = g_str_has_suffix(uri, "sdp");
396
397         /* second, if no suffix is there, check it's contents */
398         if (!ret)
399                 /* FIXIT : do it soon */
400                 LOGD("no suffix");
401
402         MMPLAYER_FREEIF(uri);
403
404         return ret;
405 }
406
407 const char *
408 _mmplayer_get_charset(const char *file_path)
409 {
410         UCharsetDetector *ucsd;
411         const UCharsetMatch *ucm;
412         UErrorCode status = U_ZERO_ERROR;
413
414         const char *charset = NULL;
415         char *buf = NULL;
416         FILE *fin = 0;
417         size_t n_size = 0;
418
419         fin = fopen(file_path, "r");
420         if (!fin) {
421                 SECURE_LOGE("fail to open file %s", file_path);
422                 return NULL;
423         }
424
425         ucsd = ucsdet_open(&status);
426         if (U_FAILURE(status)) {
427                 LOGE("fail to ucsdet_open");
428                 goto done;
429         }
430
431         ucsdet_enableInputFilter(ucsd, TRUE);
432
433         buf = g_try_malloc(1024 * 1024);
434         if (!buf) {
435                 LOGE("fail to alloc");
436                 goto done;
437         }
438
439         n_size = fread(buf, 1, 1024*1024, fin);
440         buf[1024 * 1024 - 1] = '\0';
441         if (!n_size)
442                 goto done;
443
444         ucsdet_setText(ucsd, buf, strlen(buf), &status);
445         if (U_FAILURE(status)) {
446                 LOGE("fail to ucsdet_setText");
447                 goto done;
448         }
449
450         ucm = ucsdet_detect(ucsd, &status);
451         if (U_FAILURE(status)) {
452                 LOGE("fail to ucsdet_detect");
453                 goto done;
454         }
455
456         charset = ucsdet_getName(ucm, &status);
457         if (U_FAILURE(status)) {
458                 LOGE("fail to ucsdet_getName");
459                 goto done;
460         }
461
462         /* CP949 encoding is an extension of the EUC-KR and it is backwards compatible.*/
463         if (charset && !strcmp(charset, "EUC-KR"))
464                 charset = "CP949";
465
466 done:
467         if (fin)
468                 fclose(fin);
469
470         if (ucsd)
471                 ucsdet_close(ucsd);
472
473         MMPLAYER_FREEIF(buf);
474
475         return charset;
476 }
477
478 int
479 _mmplayer_get_pixtype(unsigned int fourcc)
480 {
481         int pixtype = MM_PIXEL_FORMAT_INVALID;
482
483 #ifdef __DEBUG__
484         char *pfourcc = (char *)&fourcc;
485
486         LOGD("fourcc(%c%c%c%c)",
487                 pfourcc[0], pfourcc[1], pfourcc[2], pfourcc[3]);
488 #endif
489
490         switch (fourcc) {
491         case GST_MAKE_FOURCC('S', 'N', '1', '2'):
492         case GST_MAKE_FOURCC('N', 'V', '1', '2'):
493                 pixtype = MM_PIXEL_FORMAT_NV12;
494                 break;
495         case GST_MAKE_FOURCC('S', 'T', '1', '2'):
496                 pixtype = MM_PIXEL_FORMAT_NV12T;
497                 break;
498         case GST_MAKE_FOURCC('S', 'N', '2', '1'):
499         case GST_MAKE_FOURCC('N', 'V', '2', '1'):
500                 pixtype = MM_PIXEL_FORMAT_NV21;
501                 break;
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;
506                 break;
507         case GST_MAKE_FOURCC('S', 'Y', 'V', 'Y'):
508         case GST_MAKE_FOURCC('U', 'Y', 'V', 'Y'):
509                 pixtype = MM_PIXEL_FORMAT_UYVY;
510                 break;
511         case GST_MAKE_FOURCC('S', '4', '2', '0'):
512         case GST_MAKE_FOURCC('I', '4', '2', '0'):
513                 pixtype = MM_PIXEL_FORMAT_I420;
514                 break;
515         case GST_MAKE_FOURCC('Y', 'V', '1', '2'):
516                 pixtype = MM_PIXEL_FORMAT_YV12;
517                 break;
518         case GST_MAKE_FOURCC('4', '2', '2', 'P'):
519                 pixtype = MM_PIXEL_FORMAT_422P;
520                 break;
521         case GST_MAKE_FOURCC('R', 'G', 'B', 'P'):
522                 pixtype = MM_PIXEL_FORMAT_RGB565;
523                 break;
524         case GST_MAKE_FOURCC('R', 'G', 'B', '3'):
525                 pixtype = MM_PIXEL_FORMAT_RGB888;
526                 break;
527         case GST_MAKE_FOURCC('A', 'R', 'G', 'B'):
528         case GST_MAKE_FOURCC('x', 'R', 'G', 'B'):
529                 pixtype = MM_PIXEL_FORMAT_ARGB;
530                 break;
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;
535                 break;
536         case GST_MAKE_FOURCC('J', 'P', 'E', 'G'):
537         case GST_MAKE_FOURCC('P', 'N', 'G', ' '):
538                 pixtype = MM_PIXEL_FORMAT_ENCODED;
539                 break;
540         case GST_MAKE_FOURCC('I', 'T', 'L', 'V'):
541                 pixtype = MM_PIXEL_FORMAT_ITLV_JPEG_UYVY;
542                 break;
543         default:
544                 LOGE("Not supported fourcc type(%c%c%c%c)",
545                         fourcc, fourcc>>8, fourcc>>16, fourcc>>24);
546                 pixtype = MM_PIXEL_FORMAT_INVALID;
547                 break;
548         }
549
550         return pixtype;
551 }
552
553 static int
554 __mmplayer_storage_supported_cb(int storage_id, storage_type_e type,
555         storage_state_e state, const char *path, void *user_data)
556 {
557         mmplayer_storage_info_t *storage_info = (mmplayer_storage_info_t *)user_data;
558
559         MMPLAYER_RETURN_VAL_IF_FAIL(storage_info, FALSE);
560
561         if (type == storage_info->type && strstr(storage_info->path, path)) {
562                 storage_info->id = storage_id;
563                 storage_info->state = state;
564                 return FALSE;
565         }
566
567         return TRUE;
568 }
569
570 bool
571 _mmplayer_get_storage_info(const char *path, mmplayer_storage_info_t *storage_info)
572 {
573         int ret = 0;
574         const char *file_path = path;
575
576         MMPLAYER_RETURN_VAL_IF_FAIL(file_path && storage_info, false);
577
578         if (strncmp(file_path, "file://", strlen("file://")) == 0)
579                 file_path = path + 7; /* remove file prefix */
580
581         if (strncmp(file_path, MEDIA_PATH_EXTERNAL, strlen(MEDIA_PATH_EXTERNAL)) == 0) {
582                 storage_info->type = STORAGE_TYPE_EXTERNAL;
583
584                 memset(storage_info->path, 0x00, MM_MAX_URL_LEN);
585                 g_snprintf(storage_info->path, MM_MAX_URL_LEN, "%s", file_path);
586
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);
590                         return false;
591                 }
592
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);
595                         return false;
596                 }
597         } else {
598                 storage_info->type = STORAGE_TYPE_INTERNAL;
599                 storage_info->state = STORAGE_STATE_MOUNTED;
600         }
601
602         LOGD("storage info %d:%d:%d", storage_info->type, storage_info->id, storage_info->state);
603         return true;
604 }
605
606 media_format_mimetype_e _mmplayer_convert_audio_pcm_str_to_media_format_mime(const gchar *audio_pcm_str)
607 {
608         if (!audio_pcm_str) {
609                 LOGW("audio pcm str is NULL");
610                 return MEDIA_FORMAT_MAX;
611         }
612
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;
641         else {
642                 LOGW("Not supported audio pcm format str : %s", audio_pcm_str);
643                 return MEDIA_FORMAT_MAX;
644         }
645 }