a9d4f906e208959ee14c99cd681f665d49dde8bc
[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
40 const gchar *
41 __get_stream_type_name(int type)
42 {
43         switch (type) {
44         case MM_PLAYER_STREAM_TYPE_AUDIO:
45                 return "AUDIO";
46         case MM_PLAYER_STREAM_TYPE_VIDEO:
47                 return "VIDEO";
48         case MM_PLAYER_STREAM_TYPE_TEXT:
49                 return "TEXT";
50         case MM_PLAYER_STREAM_TYPE_DEFAULT: /* fall through */
51         case MM_PLAYER_STREAM_TYPE_MAX:     /* fall through */
52         default:
53                 return "INVAID";
54         }
55 }
56
57 const gchar *
58 __get_state_name(int state)
59 {
60         switch (state) {
61         case MM_PLAYER_STATE_NULL:
62                 return "NULL";
63         case MM_PLAYER_STATE_READY:
64                 return "READY";
65         case MM_PLAYER_STATE_PAUSED:
66                 return "PAUSED";
67         case MM_PLAYER_STATE_PLAYING:
68                 return "PLAYING";
69         case MM_PLAYER_STATE_NONE:
70                 return "NONE";
71         default:
72                 return "INVAID";
73         }
74 }
75
76 gboolean
77 __is_rtsp_streaming(mmplayer_t *player)
78 {
79         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
80
81         return (player->profile.uri_type == MM_PLAYER_URI_TYPE_URL_RTSP) ? TRUE : FALSE;
82 }
83
84 gboolean
85 __is_http_streaming(mmplayer_t *player)
86 {
87         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
88
89         return (player->profile.uri_type == MM_PLAYER_URI_TYPE_URL_HTTP) ? TRUE : FALSE;
90 }
91
92 gboolean
93 __is_streaming(mmplayer_t *player)
94 {
95         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
96
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;
99 }
100
101 gboolean
102 __is_live_streaming(mmplayer_t *player)
103 {
104         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
105
106         return (__is_rtsp_streaming(player) && player->streaming_type == STREAMING_SERVICE_LIVE) ? TRUE : FALSE;
107 }
108
109 gboolean
110 __is_http_live_streaming(mmplayer_t *player)
111 {
112         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
113
114         return (player->profile.uri_type == MM_PLAYER_URI_TYPE_HLS) ? TRUE : FALSE;
115 }
116
117 gboolean
118 __is_dash_streaming(mmplayer_t *player)
119 {
120         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
121
122         return (player->profile.uri_type == MM_PLAYER_URI_TYPE_DASH) ? TRUE : FALSE;
123 }
124
125 gboolean
126 __is_smooth_streaming(mmplayer_t *player)
127 {
128         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
129
130         return (player->profile.uri_type == MM_PLAYER_URI_TYPE_SS) ? TRUE : FALSE;
131 }
132
133 gboolean
134 __is_ms_buff_src(mmplayer_t *player)
135 {
136         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
137
138         return (player->profile.uri_type == MM_PLAYER_URI_TYPE_MS_BUFF) ? TRUE : FALSE;
139 }
140
141 gboolean
142 __has_suffix(mmplayer_t *player, const gchar *suffix)
143 {
144         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
145         MMPLAYER_RETURN_VAL_IF_FAIL(suffix, FALSE);
146
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, '?');
151
152         if (opt)
153                 *opt = '\0';
154
155         ret = g_str_has_suffix(t_url, t_suffix);
156
157         MMPLAYER_FREEIF(t_url);
158         MMPLAYER_FREEIF(t_suffix);
159
160         return ret;
161 }
162
163 gboolean
164 __mmplayer_post_message(mmplayer_t *player, enum MMMessageType msgtype, MMMessageParamType *param)
165 {
166         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
167
168         if (!player->msg_cb)
169                 return FALSE;
170
171         //LOGD("Message (type : %d)  will be posted using msg-cb(%p).", msgtype, player->msg_cb);
172
173         player->msg_cb(msgtype, param, player->msg_cb_param);
174
175         return TRUE;
176 }
177
178 gboolean
179 __mmplayer_dump_pipeline_state(mmplayer_t *player)
180 {
181         GstIterator *iter = NULL;
182         gboolean done = FALSE;
183
184         GValue item = {0, };
185         GstElement *element = NULL;
186         GstElementFactory *factory = NULL;
187
188         GstState state = GST_STATE_VOID_PENDING;
189         GstState pending = GST_STATE_VOID_PENDING;
190         GstClockTime time = 200 * GST_MSECOND;
191
192         MMPLAYER_FENTER();
193
194         MMPLAYER_RETURN_VAL_IF_FAIL(player &&
195                 player->pipeline &&
196                 player->pipeline->mainbin,
197                 FALSE);
198
199         iter = gst_bin_iterate_recurse(GST_BIN(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst));
200
201         if (iter != NULL) {
202                 while (!done) {
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);
207
208                                 factory = gst_element_get_factory(element) ;
209                                 if (factory)
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);
213                                 break;
214                         case GST_ITERATOR_RESYNC:
215                                 gst_iterator_resync(iter);
216                                 break;
217                         case GST_ITERATOR_ERROR:
218                                 done = TRUE;
219                                 break;
220                         case GST_ITERATOR_DONE:
221                                 done = TRUE;
222                                 break;
223                         }
224                 }
225         }
226
227         element = GST_ELEMENT(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst);
228
229         gst_element_get_state(element, &state, &pending, time);
230
231         factory = gst_element_get_factory(element) ;
232
233         if (factory) {
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));
240         }
241
242         g_value_unset(&item);
243
244         if (iter)
245                 gst_iterator_free(iter);
246
247         MMPLAYER_FLEAVE();
248
249         return FALSE;
250 }
251
252 int
253 util_exist_file_path(const char *file_path)
254 {
255         int fd = 0;
256         struct stat stat_results = {0, };
257
258         if (!file_path || !strlen(file_path))
259                 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
260
261         fd = open(file_path, O_RDONLY);
262
263         if (fd < 0) {
264                 char str_error[256];
265                 strerror_r(errno, str_error, sizeof(str_error));
266                 LOGE("failed to open file by %s (%d)", str_error, errno);
267
268                 if (EACCES == errno)
269                         return MM_ERROR_PLAYER_PERMISSION_DENIED;
270
271                 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
272         }
273
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");
278                 close(fd);
279                 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
280         } else
281                 LOGW("file size : %lld bytes", (long long)stat_results.st_size);
282
283         close(fd);
284
285         return MM_ERROR_NONE;
286 }
287
288 char **
289 util_get_cookie_list(const char *cookies)
290 {
291         char **cookie_list = NULL;
292         char *temp = NULL;
293         gint i = 0;
294
295         if (!cookies || !strlen(cookies))
296                 return NULL;
297
298         SECURE_LOGD("cookies : %zu[bytes] - %s", strlen(cookies), cookies);
299
300         temp = g_strdup(cookies);
301
302         /* trimming. it works inplace */
303         g_strstrip(temp);
304
305         /* split */
306         cookie_list = g_strsplit(temp, ";", 100);
307
308         if (!cookie_list) {
309                 LOGE("failed to get cookie list");
310                 goto EXIT;
311         }
312
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]);
318                         } else {
319                                 cookie_list[i][0] = '\0';
320                         }
321                 }
322         }
323
324 EXIT:
325         MMPLAYER_FREEIF(temp);
326
327         return cookie_list;
328 }
329
330 /* check the given path is indicating sdp file */
331 bool
332 util_is_sdp_file(const char *path)
333 {
334         gboolean ret = FALSE;
335         gchar *uri = NULL;
336
337         MMPLAYER_FENTER();
338
339         MMPLAYER_RETURN_VAL_IF_FAIL(path, FALSE);
340
341         uri = g_ascii_strdown(path, -1);
342
343         if (uri == NULL)
344                 return FALSE;
345
346         /* trimming */
347         g_strstrip(uri);
348
349         /* strlen(".sdp") == 4 */
350         if (strlen(uri) <= 4) {
351                 LOGW("path is too short.");
352                 MMPLAYER_FREEIF(uri);
353                 return ret;
354         }
355
356         /* first, check extension name */
357         ret = g_str_has_suffix(uri, "sdp");
358
359         /* second, if no suffix is there, check it's contents */
360         if (!ret)
361                 /* FIXIT : do it soon */
362                 LOGD("no suffix");
363
364         MMPLAYER_FREEIF(uri);
365
366         return ret;
367 }
368
369 const char *
370 util_get_charset(const char *file_path)
371 {
372         UCharsetDetector *ucsd;
373         const UCharsetMatch *ucm;
374         UErrorCode status = U_ZERO_ERROR;
375
376         const char *charset = NULL;
377         char *buf = NULL;
378         FILE *fin = 0;
379         size_t n_size = 0;
380
381         fin = fopen(file_path, "r");
382         if (!fin) {
383                 SECURE_LOGE("fail to open file %s", file_path);
384                 return NULL;
385         }
386
387         ucsd = ucsdet_open(&status);
388         if (U_FAILURE(status)) {
389                 LOGE("fail to ucsdet_open");
390                 goto done;
391         }
392
393         ucsdet_enableInputFilter(ucsd, TRUE);
394
395         buf = g_try_malloc(1024 * 1024);
396         if (!buf) {
397                 LOGE("fail to alloc");
398                 goto done;
399         }
400
401         n_size = fread(buf, 1, 1024*1024, fin);
402         buf[1024 * 1024 - 1] = '\0';
403         if (!n_size)
404                 goto done;
405
406         ucsdet_setText(ucsd, buf, strlen(buf), &status);
407         if (U_FAILURE(status)) {
408                 LOGE("fail to ucsdet_setText");
409                 goto done;
410         }
411
412         ucm = ucsdet_detect(ucsd, &status);
413         if (U_FAILURE(status)) {
414                 LOGE("fail to ucsdet_detect");
415                 goto done;
416         }
417
418         charset = ucsdet_getName(ucm, &status);
419         if (U_FAILURE(status)) {
420                 LOGE("fail to ucsdet_getName");
421                 goto done;
422         }
423
424         /* CP949 encoding is an extension of the EUC-KR and it is backwards compatible.*/
425         if (charset && !strcmp(charset, "EUC-KR"))
426                 charset = "CP949";
427
428 done:
429         if (fin)
430                 fclose(fin);
431
432         if (ucsd)
433                 ucsdet_close(ucsd);
434
435         MMPLAYER_FREEIF(buf);
436
437         return charset;
438 }
439
440 int
441 util_get_pixtype(unsigned int fourcc)
442 {
443         int pixtype = MM_PIXEL_FORMAT_INVALID;
444
445     /*
446         char *pfourcc = (char *)&fourcc;
447
448         LOGD("fourcc(%c%c%c%c)",
449                 pfourcc[0], pfourcc[1], pfourcc[2], pfourcc[3]);
450     */
451
452         switch (fourcc) {
453         case GST_MAKE_FOURCC('S', 'N', '1', '2'):
454         case GST_MAKE_FOURCC('N', 'V', '1', '2'):
455                 pixtype = MM_PIXEL_FORMAT_NV12;
456                 break;
457         case GST_MAKE_FOURCC('S', 'T', '1', '2'):
458                 pixtype = MM_PIXEL_FORMAT_NV12T;
459                 break;
460         case GST_MAKE_FOURCC('S', 'N', '2', '1'):
461         case GST_MAKE_FOURCC('N', 'V', '2', '1'):
462                 pixtype = MM_PIXEL_FORMAT_NV21;
463                 break;
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;
468                 break;
469         case GST_MAKE_FOURCC('S', 'Y', 'V', 'Y'):
470         case GST_MAKE_FOURCC('U', 'Y', 'V', 'Y'):
471                 pixtype = MM_PIXEL_FORMAT_UYVY;
472                 break;
473         case GST_MAKE_FOURCC('S', '4', '2', '0'):
474         case GST_MAKE_FOURCC('I', '4', '2', '0'):
475                 pixtype = MM_PIXEL_FORMAT_I420;
476                 break;
477         case GST_MAKE_FOURCC('Y', 'V', '1', '2'):
478                 pixtype = MM_PIXEL_FORMAT_YV12;
479                 break;
480         case GST_MAKE_FOURCC('4', '2', '2', 'P'):
481                 pixtype = MM_PIXEL_FORMAT_422P;
482                 break;
483         case GST_MAKE_FOURCC('R', 'G', 'B', 'P'):
484                 pixtype = MM_PIXEL_FORMAT_RGB565;
485                 break;
486         case GST_MAKE_FOURCC('R', 'G', 'B', '3'):
487                 pixtype = MM_PIXEL_FORMAT_RGB888;
488                 break;
489         case GST_MAKE_FOURCC('A', 'R', 'G', 'B'):
490         case GST_MAKE_FOURCC('x', 'R', 'G', 'B'):
491                 pixtype = MM_PIXEL_FORMAT_ARGB;
492                 break;
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;
497                 break;
498         case GST_MAKE_FOURCC('J', 'P', 'E', 'G'):
499         case GST_MAKE_FOURCC('P', 'N', 'G', ' '):
500                 pixtype = MM_PIXEL_FORMAT_ENCODED;
501                 break;
502         case GST_MAKE_FOURCC('I', 'T', 'L', 'V'):
503                 pixtype = MM_PIXEL_FORMAT_ITLV_JPEG_UYVY;
504                 break;
505         default:
506                 LOGE("Not supported fourcc type(%c%c%c%c)",
507                         fourcc, fourcc>>8, fourcc>>16, fourcc>>24);
508                 pixtype = MM_PIXEL_FORMAT_INVALID;
509                 break;
510         }
511
512         return pixtype;
513 }
514
515 static int
516 _util_storage_supported_cb(int storage_id, storage_type_e type,
517         storage_state_e state, const char *path, void *user_data)
518 {
519         mmplayer_storage_info_t *storage_info = (mmplayer_storage_info_t *)user_data;
520
521         MMPLAYER_RETURN_VAL_IF_FAIL(storage_info, FALSE);
522
523         if (type == storage_info->type && strstr(storage_info->path, path)) {
524                 storage_info->id = storage_id;
525                 storage_info->state = state;
526                 return FALSE;
527         }
528
529         return TRUE;
530 }
531
532 bool
533 util_get_storage_info(const char *path, mmplayer_storage_info_t *storage_info)
534 {
535         int ret = 0;
536         const char *file_path = path;
537
538         MMPLAYER_RETURN_VAL_IF_FAIL(file_path && storage_info, false);
539
540         if (strncmp(file_path, "file://", strlen("file://")) == 0)
541                 file_path = path + 7; /* remove file prefix */
542
543         if (strncmp(file_path, MEDIA_PATH_EXTERNAL, strlen(MEDIA_PATH_EXTERNAL)) == 0) {
544                 storage_info->type = STORAGE_TYPE_EXTERNAL;
545
546                 memset(storage_info->path, 0x00, MM_MAX_URL_LEN);
547                 g_snprintf(storage_info->path, MM_MAX_URL_LEN, "%s", file_path);
548
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);
552                         return false;
553                 }
554
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);
557                         return false;
558                 }
559         } else {
560                 storage_info->type = STORAGE_TYPE_INTERNAL;
561                 storage_info->state = STORAGE_STATE_MOUNTED;
562         }
563
564         LOGD("storage info %d:%d:%d", storage_info->type, storage_info->id, storage_info->state);
565         return true;
566 }
567
568 media_format_mimetype_e util_convert_audio_pcm_str_to_media_format_mime(const gchar *audio_pcm_str)
569 {
570         if (!audio_pcm_str) {
571                 LOGW("audio pcm str is NULL");
572                 return MEDIA_FORMAT_MAX;
573         }
574
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;
603         else {
604                 LOGW("Not supported audio pcm format str : %s", audio_pcm_str);
605                 return MEDIA_FORMAT_MAX;
606         }
607 }