[0.6.290] Fix adaptive streaming not being prepared due to upgrade to GST 1.22.7
[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 #define CMD_LOCK_TICKET_MAX (G_MAXUINT - 1)
41
42 const gchar *
43 _mmplayer_get_stream_type_name(int type)
44 {
45         switch (type) {
46         case MM_PLAYER_STREAM_TYPE_AUDIO:
47                 return "AUDIO";
48         case MM_PLAYER_STREAM_TYPE_VIDEO:
49                 return "VIDEO";
50         case MM_PLAYER_STREAM_TYPE_TEXT:
51                 return "TEXT";
52         case MM_PLAYER_STREAM_TYPE_DEFAULT: /* fall through */
53         case MM_PLAYER_STREAM_TYPE_MAX:     /* fall through */
54         default:
55                 return "INVAID";
56         }
57 }
58
59 const gchar *
60 _mmplayer_get_state_name(int state)
61 {
62         switch (state) {
63         case MM_PLAYER_STATE_NULL:
64                 return "NULL";
65         case MM_PLAYER_STATE_READY:
66                 return "READY";
67         case MM_PLAYER_STATE_PAUSED:
68                 return "PAUSED";
69         case MM_PLAYER_STATE_PLAYING:
70                 return "PLAYING";
71         case MM_PLAYER_STATE_NONE:
72                 return "NONE";
73         default:
74                 return "INVAID";
75         }
76 }
77
78 gboolean
79 _mmplayer_is_rtsp_streaming(mmplayer_t *player)
80 {
81         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
82
83         return (player->profile.uri_type == MM_PLAYER_URI_TYPE_URL_RTSP) ? TRUE : FALSE;
84 }
85
86 gboolean
87 _mmplayer_is_http_streaming(mmplayer_t *player)
88 {
89         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
90
91         return (player->profile.uri_type == MM_PLAYER_URI_TYPE_URL_HTTP) ? TRUE : FALSE;
92 }
93
94 gboolean
95 _mmplayer_is_streaming(mmplayer_t *player)
96 {
97         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
98
99         return (_mmplayer_is_rtsp_streaming(player) || _mmplayer_is_http_streaming(player)
100                 || _mmplayer_is_http_live_streaming(player) || _mmplayer_is_dash_streaming(player)
101                 || _mmplayer_is_smooth_streaming(player)) ? TRUE : FALSE;
102 }
103
104 gboolean
105 _mmplayer_is_live_streaming(mmplayer_t *player)
106 {
107         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
108
109         return (_mmplayer_is_rtsp_streaming(player) && player->streaming_type == STREAMING_SERVICE_LIVE) ? TRUE : FALSE;
110 }
111
112 gboolean
113 _mmplayer_is_http_live_streaming(mmplayer_t *player)
114 {
115         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
116
117         return (player->profile.uri_type == MM_PLAYER_URI_TYPE_HLS) ? TRUE : FALSE;
118 }
119
120 gboolean
121 _mmplayer_is_dash_streaming(mmplayer_t *player)
122 {
123         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
124
125         return (player->profile.uri_type == MM_PLAYER_URI_TYPE_DASH) ? TRUE : FALSE;
126 }
127
128 gboolean
129 _mmplayer_is_smooth_streaming(mmplayer_t *player)
130 {
131         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
132
133         return (player->profile.uri_type == MM_PLAYER_URI_TYPE_SS) ? TRUE : FALSE;
134 }
135
136 gboolean
137 _mmplayer_is_adaptive_streaming(mmplayer_t *player)
138 {
139         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
140         return (_mmplayer_is_http_live_streaming(player) || _mmplayer_is_dash_streaming(player)
141                 || _mmplayer_is_smooth_streaming(player)) ? TRUE : FALSE;
142 }
143
144 gboolean
145 _mmplayer_is_ms_buff_src(mmplayer_t *player)
146 {
147         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
148
149         return (player->profile.uri_type == MM_PLAYER_URI_TYPE_MS_BUFF) ? TRUE : FALSE;
150 }
151
152 gboolean
153 _mmplayer_has_suffix(mmplayer_t *player, const gchar *suffix)
154 {
155         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
156         MMPLAYER_RETURN_VAL_IF_FAIL(suffix, FALSE);
157
158         gboolean ret = FALSE;
159         gchar *t_url = g_ascii_strdown(player->profile.uri, -1);
160         gchar *t_suffix = g_ascii_strdown(suffix, -1);
161         gchar *opt = strchr(t_url, '?');
162
163         if (opt)
164                 *opt = '\0';
165
166         ret = g_str_has_suffix(t_url, t_suffix);
167
168         MMPLAYER_FREEIF(t_url);
169         MMPLAYER_FREEIF(t_suffix);
170
171         return ret;
172 }
173
174 gboolean
175 _mmplayer_is_videosink_ready(mmplayer_t *player, int surface_type)
176 {
177         int curr_type = MM_DISPLAY_SURFACE_NUM;
178
179         if (!player ||
180                 !player->pipeline ||
181                 !player->pipeline->mainbin ||
182                 !player->pipeline->videobin ||
183                 !player->pipeline->videobin[MMPLAYER_V_BIN].gst ||
184                 !player->pipeline->videobin[MMPLAYER_V_SINK].gst) {
185                 LOGW("videosink is not ready yet");
186                 return FALSE;
187         }
188
189         if (surface_type == MM_DISPLAY_SURFACE_NUM) /* don't care the surface type */
190                 return TRUE;
191
192         mm_attrs_get_int_by_name(player->attrs, "display_surface_type", &curr_type);
193
194         if (curr_type != surface_type) {
195                 LOGW("surface type is mismatched %d, %d", curr_type, surface_type);
196                 return FALSE;
197         }
198
199         return TRUE;
200 }
201
202 gboolean
203 _mmplayer_post_message(mmplayer_t *player, enum MMMessageType msgtype, MMMessageParamType *param)
204 {
205         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
206
207         if (!player->msg_cb)
208                 return FALSE;
209 #ifdef __DEBUG__
210         LOGD("Message (type : %d)  will be posted using msg-cb(%p).", msgtype, player->msg_cb);
211 #endif
212
213         player->msg_cb(msgtype, param, player->msg_cb_param);
214
215         return TRUE;
216 }
217
218 gboolean
219 _mmplayer_dump_pipeline_state(mmplayer_t *player)
220 {
221         GstIterator *iter = NULL;
222         gboolean done = FALSE;
223
224         GValue item = {0, };
225         GstElement *element = NULL;
226         GstElementFactory *factory = NULL;
227
228         GstState state = GST_STATE_VOID_PENDING;
229         GstState pending = GST_STATE_VOID_PENDING;
230         GstClockTime time = 200 * GST_MSECOND;
231
232         MMPLAYER_FENTER();
233
234         MMPLAYER_RETURN_VAL_IF_FAIL(player &&
235                 player->pipeline &&
236                 player->pipeline->mainbin,
237                 FALSE);
238
239         MMPLAYER_GENERATE_DOT_IF_ENABLED(player, "pipeline-status-error-dump");
240
241         iter = gst_bin_iterate_recurse(GST_BIN(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst));
242
243         if (iter != NULL) {
244                 while (!done) {
245                         switch (gst_iterator_next(iter, &item)) {
246                         case GST_ITERATOR_OK:
247                                 element = g_value_get_object(&item);
248                                 if (element) {
249                                         gst_element_get_state(element, &state, &pending, time);
250
251                                         factory = gst_element_get_factory(element) ;
252                                         if (factory)
253                                                 LOGE("%s:%s : From:%s To:%s   refcount : %d", GST_OBJECT_NAME(factory) , GST_ELEMENT_NAME(element) ,
254                                                         gst_element_state_get_name(state), gst_element_state_get_name(pending) , GST_OBJECT_REFCOUNT_VALUE(element));
255                                 }
256                                 g_value_reset(&item);
257                                 break;
258                         case GST_ITERATOR_RESYNC:
259                                 gst_iterator_resync(iter);
260                                 break;
261                         case GST_ITERATOR_ERROR:
262                                 done = TRUE;
263                                 break;
264                         case GST_ITERATOR_DONE:
265                                 done = TRUE;
266                                 break;
267                         }
268                 }
269         }
270
271         element = GST_ELEMENT(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst);
272
273         gst_element_get_state(element, &state, &pending, time);
274
275         factory = gst_element_get_factory(element) ;
276
277         if (factory) {
278                 LOGE("%s:%s : From:%s To:%s  refcount : %d",
279                         GST_OBJECT_NAME(factory),
280                         GST_ELEMENT_NAME(element),
281                         gst_element_state_get_name(state),
282                         gst_element_state_get_name(pending),
283                         GST_OBJECT_REFCOUNT_VALUE(element));
284         }
285
286         g_value_unset(&item);
287
288         if (iter)
289                 gst_iterator_free(iter);
290
291         MMPLAYER_FLEAVE();
292
293         return FALSE;
294 }
295
296 int
297 _mmplayer_exist_file_path(const char *file_path)
298 {
299         int fd = 0;
300         struct stat stat_results = {0, };
301
302         if (!file_path || !strlen(file_path))
303                 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
304
305         fd = open(file_path, O_RDONLY);
306
307         if (fd < 0) {
308                 char str_error[256];
309                 strerror_r(errno, str_error, sizeof(str_error));
310                 LOGE("failed to open file by %s (%d)", str_error, errno);
311
312                 if (EACCES == errno)
313                         return MM_ERROR_PLAYER_PERMISSION_DENIED;
314
315                 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
316         }
317
318         if (fstat(fd, &stat_results) < 0) {
319                 LOGE("failed to get file status");
320         } else if (stat_results.st_size == 0) {
321                 LOGE("file size is zero");
322                 if (fd < FD_NUM_FOR_DEBUG)
323                         LOGW("close fd %d", fd);
324                 close(fd);
325                 return MM_ERROR_PLAYER_FILE_NOT_FOUND;
326         } else {
327                 LOGW("file size : %lld bytes", (long long)stat_results.st_size);
328         }
329
330         if (fd < FD_NUM_FOR_DEBUG)
331                 LOGW("close fd %d", fd);
332         close(fd);
333
334         return MM_ERROR_NONE;
335 }
336
337 char **
338 _mmplayer_get_cookie_list(const char *cookies)
339 {
340         char **cookie_list = NULL;
341         char *temp = NULL;
342         gint i = 0;
343
344         if (!cookies || !strlen(cookies))
345                 return NULL;
346
347         SECURE_LOGD("cookies : %zu[bytes] - %s", strlen(cookies), cookies);
348
349         temp = g_strdup(cookies);
350
351         /* trimming. it works inplace */
352         g_strstrip(temp);
353
354         /* split */
355         cookie_list = g_strsplit(temp, ";", 100);
356
357         if (!cookie_list) {
358                 LOGE("failed to get cookie list");
359                 goto EXIT;
360         }
361
362         for (i = 0; i < g_strv_length(cookie_list); i++) {
363                 if (cookie_list[i]) {
364                         if (strlen(cookie_list[i])) {
365                                 g_strstrip(cookie_list[i]);
366                                 SECURE_LOGD("cookie_list[%d] : %zu[bytes] - %s", i, strlen(cookie_list[i]), cookie_list[i]);
367                         } else {
368                                 cookie_list[i][0] = '\0';
369                         }
370                 }
371         }
372
373 EXIT:
374         MMPLAYER_FREEIF(temp);
375
376         return cookie_list;
377 }
378
379 /* check the given path is indicating sdp file */
380 bool
381 _mmplayer_is_sdp_file(const char *path)
382 {
383         gboolean ret = FALSE;
384         gchar *uri = NULL;
385
386         MMPLAYER_FENTER();
387
388         MMPLAYER_RETURN_VAL_IF_FAIL(path, FALSE);
389
390         uri = g_ascii_strdown(path, -1);
391
392         if (uri == NULL)
393                 return FALSE;
394
395         /* trimming */
396         g_strstrip(uri);
397
398         /* strlen(".sdp") == 4 */
399         if (strlen(uri) <= 4) {
400                 LOGW("path is too short.");
401                 MMPLAYER_FREEIF(uri);
402                 return ret;
403         }
404
405         /* first, check extension name */
406         ret = g_str_has_suffix(uri, "sdp");
407
408         /* second, if no suffix is there, check it's contents */
409         if (!ret)
410                 /* FIXIT : do it soon */
411                 LOGD("no suffix");
412
413         MMPLAYER_FREEIF(uri);
414
415         return ret;
416 }
417
418 const char *
419 _mmplayer_get_charset(const char *file_path)
420 {
421         UCharsetDetector *ucsd;
422         const UCharsetMatch *ucm;
423         UErrorCode status = U_ZERO_ERROR;
424
425         const char *charset = NULL;
426         char *buf = NULL;
427         FILE *fin = 0;
428         size_t n_size = 0;
429
430         fin = fopen(file_path, "r");
431         if (!fin) {
432                 SECURE_LOGE("fail to open file %s", file_path);
433                 return NULL;
434         }
435
436         ucsd = ucsdet_open(&status);
437         if (U_FAILURE(status)) {
438                 LOGE("fail to ucsdet_open");
439                 goto done;
440         }
441
442         ucsdet_enableInputFilter(ucsd, TRUE);
443
444         buf = g_try_malloc(1024 * 1024);
445         if (!buf) {
446                 LOGE("fail to alloc");
447                 goto done;
448         }
449
450         n_size = fread(buf, 1, 1024*1024, fin);
451         buf[1024 * 1024 - 1] = '\0';
452         if (!n_size)
453                 goto done;
454
455         ucsdet_setText(ucsd, buf, strlen(buf), &status);
456         if (U_FAILURE(status)) {
457                 LOGE("fail to ucsdet_setText");
458                 goto done;
459         }
460
461         ucm = ucsdet_detect(ucsd, &status);
462         if (U_FAILURE(status)) {
463                 LOGE("fail to ucsdet_detect");
464                 goto done;
465         }
466
467         charset = ucsdet_getName(ucm, &status);
468         if (U_FAILURE(status)) {
469                 LOGE("fail to ucsdet_getName");
470                 goto done;
471         }
472
473         /* CP949 encoding is an extension of the EUC-KR and it is backwards compatible.*/
474         if (charset && !strcmp(charset, "EUC-KR"))
475                 charset = "CP949";
476
477 done:
478         if (fin)
479                 fclose(fin);
480
481         if (ucsd)
482                 ucsdet_close(ucsd);
483
484         MMPLAYER_FREEIF(buf);
485
486         return charset;
487 }
488
489 int
490 _mmplayer_get_pixtype(unsigned int fourcc)
491 {
492         int pixtype = MM_PIXEL_FORMAT_INVALID;
493
494 #ifdef __DEBUG__
495         char *pfourcc = (char *)&fourcc;
496
497         LOGD("fourcc(%c%c%c%c)",
498                 pfourcc[0], pfourcc[1], pfourcc[2], pfourcc[3]);
499 #endif
500
501         switch (fourcc) {
502         case GST_MAKE_FOURCC('S', 'N', '1', '2'):
503         case GST_MAKE_FOURCC('N', 'V', '1', '2'):
504                 pixtype = MM_PIXEL_FORMAT_NV12;
505                 break;
506         case GST_MAKE_FOURCC('S', 'T', '1', '2'):
507                 pixtype = MM_PIXEL_FORMAT_NV12T;
508                 break;
509         case GST_MAKE_FOURCC('S', 'N', '2', '1'):
510         case GST_MAKE_FOURCC('N', 'V', '2', '1'):
511                 pixtype = MM_PIXEL_FORMAT_NV21;
512                 break;
513         case GST_MAKE_FOURCC('S', 'U', 'Y', 'V'):
514         case GST_MAKE_FOURCC('Y', 'U', 'Y', 'V'):
515         case GST_MAKE_FOURCC('Y', 'U', 'Y', '2'):
516                 pixtype = MM_PIXEL_FORMAT_YUYV;
517                 break;
518         case GST_MAKE_FOURCC('S', 'Y', 'V', 'Y'):
519         case GST_MAKE_FOURCC('U', 'Y', 'V', 'Y'):
520                 pixtype = MM_PIXEL_FORMAT_UYVY;
521                 break;
522         case GST_MAKE_FOURCC('S', '4', '2', '0'):
523         case GST_MAKE_FOURCC('I', '4', '2', '0'):
524                 pixtype = MM_PIXEL_FORMAT_I420;
525                 break;
526         case GST_MAKE_FOURCC('Y', 'V', '1', '2'):
527                 pixtype = MM_PIXEL_FORMAT_YV12;
528                 break;
529         case GST_MAKE_FOURCC('4', '2', '2', 'P'):
530                 pixtype = MM_PIXEL_FORMAT_422P;
531                 break;
532         case GST_MAKE_FOURCC('R', 'G', 'B', 'P'):
533                 pixtype = MM_PIXEL_FORMAT_RGB565;
534                 break;
535         case GST_MAKE_FOURCC('R', 'G', 'B', '3'):
536                 pixtype = MM_PIXEL_FORMAT_RGB888;
537                 break;
538         case GST_MAKE_FOURCC('A', 'R', 'G', 'B'):
539         case GST_MAKE_FOURCC('x', 'R', 'G', 'B'):
540                 pixtype = MM_PIXEL_FORMAT_ARGB;
541                 break;
542         case GST_MAKE_FOURCC('B', 'G', 'R', 'A'):
543         case GST_MAKE_FOURCC('B', 'G', 'R', 'x'):
544         case GST_MAKE_FOURCC('S', 'R', '3', '2'):
545                 pixtype = MM_PIXEL_FORMAT_RGBA;
546                 break;
547         case GST_MAKE_FOURCC('J', 'P', 'E', 'G'):
548         case GST_MAKE_FOURCC('P', 'N', 'G', ' '):
549                 pixtype = MM_PIXEL_FORMAT_ENCODED;
550                 break;
551         case GST_MAKE_FOURCC('I', 'T', 'L', 'V'):
552                 pixtype = MM_PIXEL_FORMAT_ITLV_JPEG_UYVY;
553                 break;
554         default:
555                 LOGE("Not supported fourcc type(%c%c%c%c)",
556                         fourcc, fourcc>>8, fourcc>>16, fourcc>>24);
557                 pixtype = MM_PIXEL_FORMAT_INVALID;
558                 break;
559         }
560
561         return pixtype;
562 }
563
564 static int
565 __mmplayer_storage_supported_cb(int storage_id, storage_type_e type,
566         storage_state_e state, const char *path, void *user_data)
567 {
568         mmplayer_storage_info_t *storage_info = (mmplayer_storage_info_t *)user_data;
569
570         MMPLAYER_RETURN_VAL_IF_FAIL(storage_info, FALSE);
571
572         if (type == storage_info->type && strstr(storage_info->path, path)) {
573                 storage_info->id = storage_id;
574                 storage_info->state = state;
575                 return FALSE;
576         }
577
578         return TRUE;
579 }
580
581 bool
582 _mmplayer_get_storage_info(const char *path, mmplayer_storage_info_t *storage_info)
583 {
584         int ret = 0;
585         const char *file_path = path;
586
587         MMPLAYER_RETURN_VAL_IF_FAIL(file_path && storage_info, false);
588
589         if (strncmp(file_path, "file://", strlen("file://")) == 0)
590                 file_path = path + 7; /* remove file prefix */
591
592         if (strncmp(file_path, MEDIA_PATH_EXTERNAL, strlen(MEDIA_PATH_EXTERNAL)) == 0) {
593                 storage_info->type = STORAGE_TYPE_EXTERNAL;
594
595                 memset(storage_info->path, 0x00, MM_MAX_URL_LEN);
596                 g_snprintf(storage_info->path, MM_MAX_URL_LEN, "%s", file_path);
597
598                 ret = storage_foreach_device_supported((storage_device_supported_cb)__mmplayer_storage_supported_cb, storage_info);
599                 if (ret != STORAGE_ERROR_NONE) {
600                         LOGE("failed to check supported storage 0x%x", ret);
601                         return false;
602                 }
603
604                 if (storage_info->state <= STORAGE_STATE_REMOVED) {
605                         LOGE("need to check the external storage state %d:%d", storage_info->id, storage_info->state);
606                         return false;
607                 }
608         } else {
609                 storage_info->type = STORAGE_TYPE_INTERNAL;
610                 storage_info->state = STORAGE_STATE_MOUNTED;
611         }
612
613         LOGD("storage info %d:%d:%d", storage_info->type, storage_info->id, storage_info->state);
614         return true;
615 }
616
617 media_format_mimetype_e _mmplayer_convert_audio_pcm_str_to_media_format_mime(const gchar *audio_pcm_str)
618 {
619         if (!audio_pcm_str) {
620                 LOGW("audio pcm str is NULL");
621                 return MEDIA_FORMAT_MAX;
622         }
623
624         if (strstr(audio_pcm_str, "S16LE"))
625                 return MEDIA_FORMAT_PCM_S16LE;
626         else if (strstr(audio_pcm_str, "S24LE"))
627                 return MEDIA_FORMAT_PCM_S24LE;
628         else if (strstr(audio_pcm_str, "S32LE"))
629                 return MEDIA_FORMAT_PCM_S32LE;
630         else if (strstr(audio_pcm_str, "S16BE"))
631                 return MEDIA_FORMAT_PCM_S16BE;
632         else if (strstr(audio_pcm_str, "S24BE"))
633                 return MEDIA_FORMAT_PCM_S24BE;
634         else if (strstr(audio_pcm_str, "S32BE"))
635                 return MEDIA_FORMAT_PCM_S32BE;
636         else if (strstr(audio_pcm_str, "F32LE"))
637                 return MEDIA_FORMAT_PCM_F32LE;
638         else if (strstr(audio_pcm_str, "F32BE"))
639                 return MEDIA_FORMAT_PCM_F32BE;
640         else if (strstr(audio_pcm_str, "U16LE"))
641                 return MEDIA_FORMAT_PCM_U16LE;
642         else if (strstr(audio_pcm_str, "U24LE"))
643                 return MEDIA_FORMAT_PCM_U24LE;
644         else if (strstr(audio_pcm_str, "U32LE"))
645                 return MEDIA_FORMAT_PCM_U32LE;
646         else if (strstr(audio_pcm_str, "U16BE"))
647                 return MEDIA_FORMAT_PCM_U16BE;
648         else if (strstr(audio_pcm_str, "U24BE"))
649                 return MEDIA_FORMAT_PCM_U24BE;
650         else if (strstr(audio_pcm_str, "U32BE"))
651                 return MEDIA_FORMAT_PCM_U32BE;
652         else {
653                 LOGW("Not supported audio pcm format str : %s", audio_pcm_str);
654                 return MEDIA_FORMAT_MAX;
655         }
656 }
657
658 gboolean _mmplayer_use_decodebin(mmplayer_t *player) /* MMPLAYER_USE_DECODEBIN(player) */
659 {
660         int audio_offload = 0;
661         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
662
663         mm_attrs_get_int_by_name(player->attrs, MM_PLAYER_AUDIO_OFFLOAD, &audio_offload); /* user requirement */
664
665         if (MMPLAYER_IS_MS_BUFF_SRC(player) || player->ini.use_decodebin || audio_offload)
666                 return TRUE;
667
668         return FALSE;
669 }
670
671 void _mmplayer_cmd_lock_init(mmplayer_t *player)
672 {
673         MMPLAYER_RETURN_IF_FAIL(player);
674         player->cmd_lock = g_new0(mmplayer_ticket_lock_t, 1);
675         g_mutex_init(&player->cmd_lock->ticket_mutex);
676         g_cond_init(&player->cmd_lock->ticket_cond);
677         player->cmd_lock->ticket_queue_head = 0;
678         player->cmd_lock->ticket_queue_tail = 0;
679 }
680
681 void _mmplayer_cmd_lock_deinit(mmplayer_t *player)
682 {
683         MMPLAYER_RETURN_IF_FAIL(player && player->cmd_lock);
684         g_mutex_clear(&player->cmd_lock->ticket_mutex);
685         g_cond_clear(&player->cmd_lock->ticket_cond);
686         player->cmd_lock->ticket_queue_head = 0;
687         player->cmd_lock->ticket_queue_tail = 0;
688         g_free(player->cmd_lock);
689 }
690
691 void _mmplayer_cmd_lock(mmplayer_t *player)
692 {
693         guint64 queue_me;
694         GCond *cond;
695         GMutex *mutex;
696
697         MMPLAYER_RETURN_IF_FAIL(player && player->cmd_lock);
698
699         cond = &player->cmd_lock->ticket_cond;
700         mutex = &player->cmd_lock->ticket_mutex;
701         g_mutex_lock(mutex);
702         queue_me = player->cmd_lock->ticket_queue_tail;
703         player->cmd_lock->ticket_queue_tail = (queue_me + 1) % CMD_LOCK_TICKET_MAX;
704         while (queue_me != player->cmd_lock->ticket_queue_head)
705                 g_cond_wait(cond, mutex);
706         g_mutex_unlock(mutex);
707 }
708
709 gboolean _mmplayer_cmd_trylock(mmplayer_t *player)
710 {
711         GMutex *mutex;
712
713         MMPLAYER_RETURN_VAL_IF_FAIL(player && player->cmd_lock, FALSE);
714
715         mutex = &player->cmd_lock->ticket_mutex;
716
717         if (!g_mutex_trylock(mutex))
718                 return FALSE;
719
720         if (player->cmd_lock->ticket_queue_tail != player->cmd_lock->ticket_queue_head) {
721                 g_mutex_unlock(mutex);
722                 return FALSE;
723         }
724         g_mutex_unlock(mutex);
725
726         _mmplayer_cmd_lock(player);
727         return TRUE;
728 }
729
730 void _mmplayer_cmd_unlock(mmplayer_t *player)
731 {
732         GCond *cond;
733         GMutex *mutex;
734
735         MMPLAYER_RETURN_IF_FAIL(player && player->cmd_lock);
736
737         cond = &player->cmd_lock->ticket_cond;
738         mutex = &player->cmd_lock->ticket_mutex;
739
740         g_mutex_lock(mutex);
741         player->cmd_lock->ticket_queue_head =
742                 (player->cmd_lock->ticket_queue_head + 1) % CMD_LOCK_TICKET_MAX;
743         g_cond_broadcast(cond);
744         g_mutex_unlock(mutex);
745 }