Merge "[MPR-182] support video gapless playback" into tizen
[platform/core/multimedia/libmm-player.git] / src / mm_player_es.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>, heechul jeon <heechul.jeon@samsung.co>,
7  * YoungHwan An <younghwan_.an@samsung.com>, Eunhae Choi <eunhae1.choi@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 /*===========================================================================================
24 |                                                                                                                                                                                       |
25 |  INCLUDE FILES                                                                                                                                                        |
26 |                                                                                                                                                                                       |
27 ========================================================================================== */
28 #include <dlog.h>
29 #include "mm_player_es.h"
30 #include "mm_player_utils.h"
31 #include "mm_player_internal.h"
32
33 #include <gst/app/gstappsrc.h>
34
35 /*---------------------------------------------------------------------------
36 |    LOCAL VARIABLE DEFINITIONS for internal                                                            |
37 ---------------------------------------------------------------------------*/
38
39 /*---------------------------------------------------------------------------
40 |    LOCAL FUNCTION PROTOTYPES:                                                                                         |
41 ---------------------------------------------------------------------------*/
42 static int __parse_media_format (MMPlayerVideoStreamInfo * video, MMPlayerAudioStreamInfo * audio, media_format_h format);
43 static int __convert_media_format_video_mime_to_str (MMPlayerVideoStreamInfo * video, media_format_mimetype_e mime);
44 static int __convert_media_format_audio_mime_to_str (MMPlayerAudioStreamInfo * audio, media_format_mimetype_e mime);
45
46 /*===========================================================================================
47 |                                                                                                                                                                                       |
48 |  FUNCTION DEFINITIONS                                                                                                                                         |
49 |                                                                                                                                                                                       |
50 ========================================================================================== */
51
52 static int
53 __convert_media_format_video_mime_to_str (MMPlayerVideoStreamInfo * video,
54     media_format_mimetype_e mime)
55 {
56   MMPLAYER_RETURN_VAL_IF_FAIL (video, MM_ERROR_INVALID_ARGUMENT);
57
58   switch (mime) {
59     case MEDIA_FORMAT_MPEG4_SP:
60       video->mime = g_strdup ("video/mpeg");
61       video->version = 4;
62       break;
63     case MEDIA_FORMAT_H264_SP:
64         case MEDIA_FORMAT_H264_MP:
65         case MEDIA_FORMAT_H264_HP:
66       video->mime = g_strdup ("video/x-h264");
67       break;
68     default:
69       video->mime = g_strdup ("unknown");
70       break;
71   }
72
73   return MM_ERROR_NONE;
74 }
75
76 static int
77 __convert_media_format_audio_mime_to_str (MMPlayerAudioStreamInfo * audio,
78     media_format_mimetype_e mime)
79 {
80   MMPLAYER_RETURN_VAL_IF_FAIL (audio, MM_ERROR_INVALID_ARGUMENT);
81
82   switch (mime) {
83     case MEDIA_FORMAT_AAC:
84       audio->mime = g_strdup ("audio/mpeg");
85       audio->version = 2;
86       break;
87     default:
88       audio->mime = g_strdup ("unknown");
89       break;
90   }
91
92   return MM_ERROR_NONE;
93 }
94
95 static int
96 __parse_media_format (MMPlayerVideoStreamInfo * video,
97     MMPlayerAudioStreamInfo * audio, media_format_h format)
98 {
99   if (audio) {
100     media_format_mimetype_e mime;
101     int channel;
102     int samplerate;
103     int avg_bps;
104
105     if (media_format_get_audio_info (format, &mime, &channel, &samplerate, NULL,
106             &avg_bps) != MEDIA_FORMAT_ERROR_NONE) {
107       LOGE ("media_format_get_audio_info failed");
108           return MM_ERROR_PLAYER_INTERNAL;
109     }
110
111     __convert_media_format_audio_mime_to_str (audio, mime);
112     audio->sample_rate = samplerate;
113     audio->channels = channel;
114 //video->user_info = ;
115   }
116
117   if (video) {
118 #define DEFAULT_FRAMERATE_NUM 30
119 #define DEFAULT_FRAMERATE_DEN 1
120     media_format_mimetype_e mime;
121     int width = 0;
122     int height = 0;
123     int avg_bps = 0;
124     int frame_rate = 0;
125
126     if (media_format_get_video_info (format, &mime, &width, &height, &avg_bps,
127             NULL) != MEDIA_FORMAT_ERROR_NONE) {
128       LOGE ("media_format_get_video_info failed");
129           return MM_ERROR_PLAYER_INTERNAL;
130     }
131
132     if (media_format_get_video_frame_rate(format, &frame_rate)) {
133         LOGW ("failed to get video frame rate, will be set 30.");
134     }
135     LOGD ("frame_rate %d", frame_rate);
136
137     __convert_media_format_video_mime_to_str (video, mime);
138
139     video->width = width;
140     video->height = height;
141     video->framerate_num = (frame_rate>0)?(frame_rate):(DEFAULT_FRAMERATE_NUM);
142     video->framerate_den = DEFAULT_FRAMERATE_DEN;
143   }
144
145   return MM_ERROR_NONE;
146 }
147
148 static gboolean
149 __mmplayer_update_video_info(MMHandleType hplayer, media_format_h fmt)
150 {
151   mm_player_t *player = (mm_player_t *) hplayer;
152   gboolean ret = FALSE;
153   GstStructure *str = NULL;
154   media_format_mimetype_e mimetype = 0;
155   gint cur_width = 0, width = 0;
156   gint cur_height = 0, height = 0;
157
158   MMPLAYER_FENTER ();
159
160   MMPLAYER_RETURN_VAL_IF_FAIL (player, FALSE);
161   MMPLAYER_RETURN_VAL_IF_FAIL (fmt, FALSE);
162
163   if (player->v_stream_caps)
164   {
165     str = gst_caps_get_structure (player->v_stream_caps, 0);
166     if ( !gst_structure_get_int (str, "width", &cur_width))
167     {
168       LOGD ("missing 'width' field in video caps");
169     }
170
171     if ( !gst_structure_get_int (str, "height", &cur_height))
172     {
173       LOGD ("missing 'height' field in video caps");
174     }
175
176     media_format_get_video_info(fmt, &mimetype, &width, &height, NULL, NULL);
177     if ((cur_width != width) || (cur_height != height))
178     {
179       LOGW ("resolution is changed %dx%d -> %dx%d",
180                           cur_width, cur_height, width, height);
181       _mmplayer_set_video_info(hplayer, fmt);
182       ret = TRUE;
183     }
184   }
185
186   MMPLAYER_FLEAVE ();
187   return ret;
188 }
189
190
191 int
192 _mmplayer_set_media_stream_buffer_status_cb(MMHandleType hplayer,
193                                             MMPlayerStreamType type,
194                                             mm_player_media_stream_buffer_status_callback callback,
195                                             void *user_param)
196 {
197         mm_player_t *player = (mm_player_t *) hplayer;
198
199         MMPLAYER_FENTER ();
200
201         MMPLAYER_RETURN_VAL_IF_FAIL (player, MM_ERROR_PLAYER_NOT_INITIALIZED);
202
203         if ((type < MM_PLAYER_STREAM_TYPE_DEFAULT) || (type > MM_PLAYER_STREAM_TYPE_TEXT))
204                 return MM_ERROR_INVALID_ARGUMENT;
205
206         if (player->media_stream_buffer_status_cb[type])
207         {
208                 if (!callback)
209                 {
210                         LOGD ("[type:%d] will be clear.\n", type);
211                 }
212                 else
213                 {
214                         LOGD ("[type:%d] will be overwritten.\n", type);
215                 }
216         }
217
218         player->media_stream_buffer_status_cb[type] = callback;
219         player->buffer_cb_user_param = user_param;
220
221         LOGD ("player handle %p, type %d, callback %p\n", player, type,
222                 player->media_stream_buffer_status_cb[type]);
223
224         MMPLAYER_FLEAVE ();
225
226         return MM_ERROR_NONE;
227 }
228
229 int
230 _mmplayer_set_media_stream_seek_data_cb(MMHandleType hplayer,
231                                         MMPlayerStreamType type,
232                                         mm_player_media_stream_seek_data_callback callback,
233                                         void *user_param)
234 {
235         mm_player_t *player = (mm_player_t *) hplayer;
236
237         MMPLAYER_FENTER ();
238
239         MMPLAYER_RETURN_VAL_IF_FAIL (player, MM_ERROR_PLAYER_NOT_INITIALIZED);
240
241         if ((type < MM_PLAYER_STREAM_TYPE_DEFAULT) || (type > MM_PLAYER_STREAM_TYPE_TEXT))
242                 return MM_ERROR_INVALID_ARGUMENT;
243
244         if (player->media_stream_seek_data_cb[type])
245         {
246                 if (!callback)
247                 {
248                         LOGD ("[type:%d] will be clear.\n", type);
249                 }
250                 else
251                 {
252                         LOGD ("[type:%d] will be overwritten.\n", type);
253                 }
254         }
255
256         player->media_stream_seek_data_cb[type] = callback;
257         player->buffer_cb_user_param = user_param;
258
259         LOGD ("player handle %p, type %d, callback %p\n", player, type,
260                 player->media_stream_seek_data_cb[type]);
261
262         MMPLAYER_FLEAVE ();
263
264         return MM_ERROR_NONE;
265 }
266
267 static GstElement*
268 __mmplayer_get_source_element (mm_player_t *player, MMPlayerStreamType type)
269 {
270         enum MainElementID elemId = MMPLAYER_M_NUM;
271
272         if (player && player->pipeline && player->pipeline->mainbin) {
273
274                 /* get elem according to the stream type */
275                 if (type == MM_PLAYER_STREAM_TYPE_AUDIO) {
276                         if (player->pipeline->mainbin[MMPLAYER_M_2ND_SRC].gst) {
277                                 elemId = MMPLAYER_M_2ND_SRC;
278                         } else if (g_strrstr (GST_ELEMENT_NAME (player->pipeline->mainbin[MMPLAYER_M_SRC].gst), "audio_appsrc")) {
279                                 elemId = MMPLAYER_M_SRC;
280                         }
281                 }
282                 else if (type == MM_PLAYER_STREAM_TYPE_VIDEO) {
283                         elemId = MMPLAYER_M_SRC;
284                 }
285                 else if (type == MM_PLAYER_STREAM_TYPE_TEXT) {
286                         elemId = MMPLAYER_M_SUBSRC;
287                 }
288
289                 if (elemId != MMPLAYER_M_NUM)
290                         return player->pipeline->mainbin[elemId].gst;
291         }
292
293         return NULL;
294 }
295
296 int
297 _mmplayer_set_media_stream_max_size(MMHandleType hplayer, MMPlayerStreamType type, guint64 max_size)
298 {
299         mm_player_t *player = (mm_player_t *) hplayer;
300         GstElement *element = NULL;
301
302         MMPLAYER_FENTER ();
303         MMPLAYER_RETURN_VAL_IF_FAIL (player, MM_ERROR_PLAYER_NOT_INITIALIZED);
304
305         if ((type < MM_PLAYER_STREAM_TYPE_AUDIO) ||
306                 (type > MM_PLAYER_STREAM_TYPE_TEXT) ||
307                 (max_size == 0)) {
308                 LOGE ("Invalid param type:%d, max_size:%d", type, max_size);
309                 return MM_ERROR_INVALID_ARGUMENT;
310         }
311
312         LOGD ("type %d, max_size %llu\n", type, max_size);
313
314         if ((element = __mmplayer_get_source_element(player, type))) {
315                 LOGD ("update max_size of %s\n", GST_ELEMENT_NAME(element));
316                 g_object_set(G_OBJECT(element), "max-bytes", max_size, NULL);
317         }
318
319         player->media_stream_buffer_max_size[type] = max_size;
320
321         MMPLAYER_FLEAVE ();
322         return MM_ERROR_NONE;
323 }
324
325 int
326 _mmplayer_get_media_stream_max_size(MMHandleType hplayer, MMPlayerStreamType type, guint64 *max_size)
327 {
328         mm_player_t *player = (mm_player_t *) hplayer;
329
330         MMPLAYER_FENTER ();
331
332         MMPLAYER_RETURN_VAL_IF_FAIL (player, MM_ERROR_PLAYER_NOT_INITIALIZED);
333         MMPLAYER_RETURN_VAL_IF_FAIL (max_size, MM_ERROR_INVALID_ARGUMENT);
334
335         if ((type < MM_PLAYER_STREAM_TYPE_DEFAULT) || (type > MM_PLAYER_STREAM_TYPE_TEXT))
336                 return MM_ERROR_INVALID_ARGUMENT;
337
338         *max_size = player->media_stream_buffer_max_size[type];
339
340         MMPLAYER_FLEAVE ();
341
342         return MM_ERROR_NONE;
343 }
344
345 int
346 _mmplayer_set_media_stream_min_percent(MMHandleType hplayer, MMPlayerStreamType type, guint min_percent)
347 {
348         mm_player_t *player = (mm_player_t *) hplayer;
349         GstElement *element = NULL;
350
351         MMPLAYER_FENTER ();
352
353         MMPLAYER_RETURN_VAL_IF_FAIL (player, MM_ERROR_PLAYER_NOT_INITIALIZED);
354
355         if ((type < MM_PLAYER_STREAM_TYPE_AUDIO) || (type > MM_PLAYER_STREAM_TYPE_TEXT)) {
356                 LOGE ("Invalid param type:%d", type);
357                 return MM_ERROR_INVALID_ARGUMENT;
358         }
359
360         LOGD ("type %d, min_per %u\n", type, min_percent);
361
362         if ((element = __mmplayer_get_source_element(player, type))) {
363                 LOGD ("update min_per of %s\n", GST_ELEMENT_NAME(element));
364                 g_object_set(G_OBJECT(element), "min-percent", min_percent, NULL);
365         }
366
367         player->media_stream_buffer_min_percent[type] = min_percent;
368
369         MMPLAYER_FLEAVE ();
370         return MM_ERROR_NONE;
371 }
372
373 int
374 _mmplayer_get_media_stream_min_percent(MMHandleType hplayer, MMPlayerStreamType type, guint *min_percent)
375 {
376         mm_player_t *player = (mm_player_t *) hplayer;
377
378         MMPLAYER_FENTER ();
379
380         MMPLAYER_RETURN_VAL_IF_FAIL (player, MM_ERROR_PLAYER_NOT_INITIALIZED);
381         MMPLAYER_RETURN_VAL_IF_FAIL (min_percent, MM_ERROR_INVALID_ARGUMENT);
382
383         if ((type < MM_PLAYER_STREAM_TYPE_DEFAULT) || (type > MM_PLAYER_STREAM_TYPE_TEXT))
384                 return MM_ERROR_INVALID_ARGUMENT;
385
386         *min_percent = player->media_stream_buffer_min_percent[type];
387
388         MMPLAYER_FLEAVE ();
389
390         return MM_ERROR_NONE;
391 }
392
393 static int
394 __mmplayer_check_buffer_level(mm_player_t *player, GstElement* element, MMPlayerStreamType type)
395 {
396         guint64 current_level_bytes = 0;
397         guint64 max_bytes = 0;
398         guint current_level_per = 0;
399
400         MMPLAYER_FENTER ();
401         MMPLAYER_RETURN_VAL_IF_FAIL ( player && element, MM_ERROR_PLAYER_NOT_INITIALIZED);
402
403         if (player->media_stream_buffer_max_size[type] > 0) {
404                 max_bytes = player->media_stream_buffer_max_size[type];
405         } else {
406                 g_object_get(G_OBJECT(element), "max-bytes", &max_bytes, NULL);
407         }
408
409         if (max_bytes == 0) {
410                 LOGW ("buffer max size is zero.");
411                 return MM_ERROR_NONE;
412         }
413
414         g_object_get(G_OBJECT(element), "current-level-bytes", &current_level_bytes, NULL);
415
416         if (max_bytes <= current_level_bytes) {
417                 LOGE ("no available buffer space. type %d, max %lld, curr %lld", type, max_bytes, current_level_bytes);
418                 return MM_ERROR_PLAYER_BUFFER_SPACE;
419         }
420
421         if (MMPLAYER_CURRENT_STATE(player) != MM_PLAYER_STATE_PLAYING) {
422                 if (!player->media_stream_buffer_status_cb[type]) {
423                         return MM_ERROR_NONE;
424                 }
425
426                 current_level_per = (guint)(gst_util_guint64_to_gdouble(current_level_bytes)/gst_util_guint64_to_gdouble(max_bytes)*100);
427
428                 LOGD ("type %d, min_per %u, curr_per %u max %lld cur %lld\n",
429                                         type, player->media_stream_buffer_min_percent[type],
430                                         current_level_per,
431                                         player->media_stream_buffer_max_size[type],
432                                         current_level_bytes);
433
434                 if (current_level_per < player->media_stream_buffer_min_percent[type])
435                         player->media_stream_buffer_status_cb[type](type, MM_PLAYER_MEDIA_STREAM_BUFFER_UNDERRUN, current_level_bytes, player->buffer_cb_user_param);
436         }
437
438         MMPLAYER_FLEAVE ();
439         return MM_ERROR_NONE;
440 }
441
442 int
443 _mmplayer_submit_packet (MMHandleType hplayer, media_packet_h packet)
444 {
445   int ret = MM_ERROR_NONE;
446   GstBuffer *_buffer = NULL;
447   mm_player_t *player = (mm_player_t *) hplayer;
448   guint8 *buf = NULL;
449   uint64_t size = 0;
450   GstElement* element = NULL;
451   MMPlayerStreamType streamtype = MM_PLAYER_STREAM_TYPE_AUDIO;
452   media_format_h fmt = NULL;
453   bool flag = FALSE;
454   bool is_eos = FALSE;
455
456   MMPLAYER_RETURN_VAL_IF_FAIL (packet, MM_ERROR_INVALID_ARGUMENT);
457   MMPLAYER_RETURN_VAL_IF_FAIL ( player &&
458     player->pipeline &&
459     player->pipeline->mainbin &&
460     player->pipeline->mainbin[MMPLAYER_M_SRC].gst,
461     MM_ERROR_PLAYER_INTERNAL );
462
463   /* get stream type if audio or video */
464   media_packet_is_audio (packet, &flag);
465   if (flag) {
466     streamtype = MM_PLAYER_STREAM_TYPE_AUDIO;
467   } else {
468     media_packet_is_video (packet, &flag);
469     if (flag)
470       streamtype = MM_PLAYER_STREAM_TYPE_VIDEO;
471         else
472       streamtype = MM_PLAYER_STREAM_TYPE_TEXT;
473   }
474
475   element = __mmplayer_get_source_element(player, streamtype);
476   if (!element) {
477       LOGE ("there is no source element of type %d", streamtype);
478       ret = MM_ERROR_PLAYER_INTERNAL;
479       goto ERROR;
480   }
481
482   /* check buffer level */
483   ret = __mmplayer_check_buffer_level (player, element, streamtype);
484   if (ret != MM_ERROR_NONE)
485     return ret;
486
487   /* get data */
488   if (media_packet_get_buffer_data_ptr (packet, (void **) &buf) != MEDIA_PACKET_ERROR_NONE) {
489     LOGE("failed to get buffer data ptr");
490     ret = MM_ERROR_PLAYER_INTERNAL;
491     goto ERROR;
492   }
493
494   if (media_packet_get_buffer_size (packet, &size) != MEDIA_PACKET_ERROR_NONE) {
495     LOGE("failed to get buffer size");
496     ret = MM_ERROR_PLAYER_INTERNAL;
497     goto ERROR;
498   }
499
500   if (buf != NULL && size > 0) {
501     GstMapInfo buff_info = GST_MAP_INFO_INIT;
502     uint64_t pts = 0;
503
504     /* get size */
505     _buffer = gst_buffer_new_and_alloc (size);
506
507     if (!_buffer) {
508         LOGE("failed to allocate memory for push buffer\n");
509         ret = MM_ERROR_PLAYER_NO_FREE_SPACE;
510         goto ERROR;
511     }
512
513     if (gst_buffer_map (_buffer, &buff_info, GST_MAP_READWRITE)) {
514
515       memcpy (buff_info.data, buf, size);
516       buff_info.size = size;
517
518       gst_buffer_unmap (_buffer, &buff_info);
519     }
520
521     if (streamtype == MM_PLAYER_STREAM_TYPE_VIDEO) {
522       /* get format to check video format */
523       media_packet_get_format (packet, &fmt);
524       if (fmt) {
525         if (__mmplayer_update_video_info(hplayer, fmt)) {
526           LOGD("update video caps");
527           g_object_set(G_OBJECT(player->pipeline->mainbin[MMPLAYER_M_SRC].gst),
528                                     "caps", player->v_stream_caps, NULL);
529         }
530                 media_format_unref(fmt);
531       }
532     }
533
534     /* get pts */
535     if (media_packet_get_pts (packet, &pts) != MEDIA_PACKET_ERROR_NONE) {
536       LOGE("failed to get pts info");
537       ret = MM_ERROR_PLAYER_INTERNAL;
538       goto ERROR;
539     }
540     GST_BUFFER_PTS (_buffer) = (GstClockTime)pts;
541         gst_app_src_push_buffer (GST_APP_SRC (element), _buffer);
542   }
543
544   /* check eos */
545   if (media_packet_is_end_of_stream(packet, &is_eos) != MEDIA_PACKET_ERROR_NONE) {
546     LOGE("failed to get eos info");
547     ret = MM_ERROR_PLAYER_INTERNAL;
548     goto ERROR;
549   }
550
551   if (is_eos) {
552     LOGW ("we got eos of stream type(%d)", streamtype);
553         g_signal_emit_by_name (element, "end-of-stream", &ret);
554   }
555
556 ERROR:
557   return ret;
558 }
559
560 static int
561 __mmplayer_video_caps_new (MMHandleType hplayer, MMPlayerVideoStreamInfo * video,
562     const char *fieldname, ...)
563 {
564   int cap_size;
565   GstCaps *caps = NULL;
566   GstStructure *structure = NULL;
567   va_list var_args;
568   mm_player_t *player = MM_PLAYER_CAST (hplayer);
569
570   MMPLAYER_FENTER ();
571   MMPLAYER_RETURN_VAL_IF_FAIL (player, MM_ERROR_PLAYER_NOT_INITIALIZED);
572   MMPLAYER_RETURN_VAL_IF_FAIL (video, MM_ERROR_PLAYER_NOT_INITIALIZED);
573
574   LOGD ("width=%d height=%d framerate num=%d, den=%d",
575     video->width, video->height, video->framerate_num, video->framerate_den);
576
577   caps = gst_caps_new_simple (video->mime,
578       "width", G_TYPE_INT, video->width,
579       "height", G_TYPE_INT, video->height,
580       "framerate", GST_TYPE_FRACTION, video->framerate_num, video->framerate_den, NULL);
581
582   for (cap_size = 0; cap_size < gst_caps_get_size (caps); cap_size++) {
583     va_start (var_args, fieldname);
584     structure = gst_caps_get_structure (caps, cap_size);
585     gst_structure_set_valist (structure, fieldname, var_args);
586     va_end (var_args);
587   }
588
589   if (video->extradata_size) {
590     GstBuffer *buf = NULL;
591     GstMapInfo buff_info = GST_MAP_INFO_INIT;
592
593     buf = gst_buffer_new_and_alloc (video->extradata_size);
594
595     if (gst_buffer_map (buf, &buff_info, GST_MAP_READ)) {
596       memcpy (buff_info.data, video->codec_extradata, video->extradata_size);
597       buff_info.size = video->extradata_size;
598       gst_buffer_unmap (buf, &buff_info);
599     }
600
601     gst_caps_set_simple (caps, "codec_data", GST_TYPE_BUFFER, buf, NULL);
602     gst_buffer_unref (buf);
603   }
604
605   if (player->v_stream_caps)
606   {
607     LOGW ("caps will be updated ");
608
609     gst_caps_unref(player->v_stream_caps);
610     player->v_stream_caps = NULL;
611   }
612
613   player->v_stream_caps = gst_caps_copy (caps);
614   MMPLAYER_LOG_GST_CAPS_TYPE (player->v_stream_caps);
615   gst_caps_unref (caps);
616
617   MMPLAYER_FLEAVE ();
618
619   return MM_ERROR_NONE;
620 }
621
622 static void
623 __mmplayer_set_uri_type(mm_player_t *player)
624 {
625         MMPLAYER_FENTER ();
626
627         player->profile.uri_type = MM_PLAYER_URI_TYPE_MS_BUFF;
628         player->es_player_push_mode = TRUE;
629
630         MMPLAYER_FLEAVE ();
631         return;
632 }
633
634 int
635 _mmplayer_set_video_info (MMHandleType hplayer, media_format_h format)
636 {
637   mm_player_t *player = MM_PLAYER_CAST (hplayer);
638   MMPlayerVideoStreamInfo video = { 0, };
639   int ret = MM_ERROR_NONE;
640   gboolean drc = FALSE;
641
642   MMPLAYER_FENTER ();
643
644   MMPLAYER_RETURN_VAL_IF_FAIL (player, MM_ERROR_PLAYER_NOT_INITIALIZED);
645
646   __mmplayer_set_uri_type(player);
647
648   ret = __parse_media_format (&video, NULL, format);
649   if(ret != MM_ERROR_NONE)
650     return ret;
651
652   mm_attrs_get_int_by_name(player->attrs, MM_PLAYER_DRC_MODE, &drc);
653
654   if (strstr (video.mime, "video/mpeg")) {
655     __mmplayer_video_caps_new (hplayer, &video,
656         "mpegversion", G_TYPE_INT, video.version,
657         "systemstream", G_TYPE_BOOLEAN, FALSE,
658         "adaptive-streaming", G_TYPE_BOOLEAN, drc, NULL);
659   } else if (strstr (video.mime, "video/x-h264")) {
660     //if (info.colordepth)
661     {
662       //      __mmplayer_video_caps_new(hplayer, &info,
663       //              "colordepth", G_TYPE_INT, info.colordepth, NULL);
664     }
665     //else
666     {
667       __mmplayer_video_caps_new (hplayer, &video,
668           "stream-format", G_TYPE_STRING, "byte-stream",
669           "alignment", G_TYPE_STRING, "au",
670           "adaptive-streaming", G_TYPE_BOOLEAN, drc, NULL);
671     }
672   }
673 #if 0
674   else if (strstr (info->mime, "video/x-wmv")) {
675     __mmplayer_video_caps_new (hplayer, &info,
676         "wmvversion", G_TYPE_INT, info.version, NULL);
677   } else if (strstr (info.mime, "video/x-pn-realvideo")) {
678     __mmplayer_video_caps_new (hplayer, &info,
679         "rmversion", G_TYPE_INT, info.version, NULL);
680   } else if (strstr (info.mime, "video/x-msmpeg")) {
681     __mmplayer_video_caps_new (hplayer, &info,
682         "msmpegversion", G_TYPE_INT, info.version, NULL);
683   } else if (strstr (info.mime, "video/x-h265")) {
684     if (info.colordepth) {
685       __mmplayer_video_caps_new (hplayer, &info,
686           "colordepth", G_TYPE_INT, info.colordepth, NULL);
687     } else {
688       __mmplayer_video_caps_new (hplayer, &info, NULL);
689     }
690   } else {
691     __mmplayer_video_caps_new (hplayer, &info, NULL);
692   }
693 #endif
694   g_free ((char *) video.mime);
695
696   MMPLAYER_FLEAVE ();
697
698   return MM_ERROR_NONE;
699 }
700
701 int
702 _mmplayer_set_audio_info (MMHandleType hplayer, media_format_h format)
703 {
704   mm_player_t *player = MM_PLAYER_CAST (hplayer);
705   GstCaps *caps = NULL;
706   MMPlayerAudioStreamInfo audio = { 0, };
707   int ret = MM_ERROR_NONE;
708
709   MMPLAYER_FENTER ();
710
711   MMPLAYER_RETURN_VAL_IF_FAIL (hplayer, MM_ERROR_PLAYER_NOT_INITIALIZED);
712
713   __mmplayer_set_uri_type(player);
714
715   ret = __parse_media_format (NULL, &audio, format);
716   if(ret != MM_ERROR_NONE)
717     return ret;
718
719   audio.user_info = 0;           //test
720
721   LOGD ("set audio player[%p] info [%p] version=%d rate=%d channel=%d",
722       player, audio, audio.version, audio.sample_rate, audio.channels);
723
724   if (strstr (audio.mime, "audio/mpeg")) {
725     if (audio.version == 1) {   // mp3
726       caps = gst_caps_new_simple ("audio/mpeg",
727           "channels", G_TYPE_INT, audio.channels,
728           "rate", G_TYPE_INT, audio.sample_rate,
729           "mpegversion", G_TYPE_INT, audio.version,
730           "layer", G_TYPE_INT, audio.user_info, NULL);
731     } else {                    // aac
732       gchar *format = NULL;
733
734       if (audio.user_info == 0)
735         format = g_strdup ("raw");
736       else if (audio.user_info == 1)
737         format = g_strdup ("adts");
738       else if (audio.user_info == 2)
739         format = g_strdup ("adif");
740
741       caps = gst_caps_new_simple ("audio/mpeg",
742           "channels", G_TYPE_INT, audio.channels,
743           "rate", G_TYPE_INT, audio.sample_rate,
744           "mpegversion", G_TYPE_INT, audio.version,
745           "stream-format", G_TYPE_STRING, format, NULL);
746
747       g_free (format);
748       format = NULL;
749     }
750   }
751 #if 0
752   else if (strstr (audio.mime, "audio/x-raw-int")) {
753     caps = gst_caps_new_simple ("audio/x-raw-int",
754         "width", G_TYPE_INT, audio.width,
755         "depth", G_TYPE_INT, audio.depth,
756         "endianness", G_TYPE_INT, audio.endianness,
757         "signed", G_TYPE_BOOLEAN, audio.signedness,
758         "channels", G_TYPE_INT, audio.channels,
759         "rate", G_TYPE_INT, audio.sample_rate, NULL);
760   } else {
761     caps = gst_caps_new_simple (audio.mime,
762         "channels", G_TYPE_INT, audio.channels,
763         "rate", G_TYPE_INT, audio.sample_rate, NULL);
764   }
765 #endif
766
767   if (audio.extradata_size) {
768     GstBuffer *buf = NULL;
769     GstMapInfo buff_info = GST_MAP_INFO_INIT;
770
771     buf = gst_buffer_new_and_alloc (audio.extradata_size);
772
773     if (gst_buffer_map (buf, &buff_info, GST_MAP_READ)) {
774       memcpy (buff_info.data, audio.codec_extradata, audio.extradata_size);
775       gst_buffer_unmap (buf, &buff_info);
776     }
777
778     gst_caps_set_simple (caps, "codec_data", GST_TYPE_BUFFER, buf, NULL);
779     gst_buffer_unref (buf);
780   }
781
782   g_free ((char *) audio.mime);
783
784   player->a_stream_caps = gst_caps_copy (caps);
785   gst_caps_unref (caps);
786
787   MMPLAYER_FLEAVE ();
788
789   return MM_ERROR_NONE;
790 }
791
792 int
793 _mmplayer_set_subtitle_info (MMHandleType hplayer,
794     MMPlayerSubtitleStreamInfo * subtitle)
795 {
796 #if 0                           //todo
797
798   mm_player_t *player = MM_PLAYER_CAST (hplayer);
799   GstCaps *caps = NULL;
800
801   MMPLAYER_FENTER ();
802
803   MMPLAYER_RETURN_VAL_IF_FAIL (player, MM_ERROR_PLAYER_NOT_INITIALIZED);
804   MMPLAYER_RETURN_VAL_IF_FAIL (info, MM_ERROR_PLAYER_NOT_INITIALIZED);
805
806   LOGD ("set subtitle player[%p] info [%p]", player, info);
807
808
809   caps = gst_caps_new_simple (info->mime, NULL, NULL);  // TO CHECK
810   if (NULL == caps)
811     return FALSE;
812
813   if (strstr (info->mime, "application/x-xsub")) {
814     gst_caps_set_simple (caps, "codec_tag", G_TYPE_UINT, info->codec_tag, NULL);
815   } else if (strstr (info->mime, "application/x-smpte-text")) {
816     if (info->context) {
817       gst_caps_set_simple (caps, "ttml_priv_data", G_TYPE_POINTER,
818           info->context, NULL);
819     }
820   }
821
822   player->s_stream_caps = gst_caps_copy (caps);
823
824   gst_caps_unref (caps);
825 #endif
826
827   MMPLAYER_FLEAVE ();
828
829   return MM_ERROR_NONE;
830 }
831
832 int
833 _mmplayer_set_media_stream_dynamic_resolution(MMHandleType hplayer, bool drc)
834 {
835         mm_player_t *player = MM_PLAYER_CAST (hplayer);
836         int ret = MM_ERROR_NONE;
837
838         MMPLAYER_FENTER ();
839
840         MMPLAYER_RETURN_VAL_IF_FAIL (player, MM_ERROR_PLAYER_NOT_INITIALIZED);
841
842         mm_attrs_set_int_by_name (player->attrs, MM_PLAYER_DRC_MODE, (int)drc);
843         if (player->v_stream_caps)
844         {
845                 gst_caps_set_simple (player->v_stream_caps,
846                         "adaptive-streaming", G_TYPE_BOOLEAN, drc, NULL);
847                 MMPLAYER_LOG_GST_CAPS_TYPE (player->v_stream_caps);
848         }
849
850         MMPLAYER_FLEAVE ();
851         return ret;
852
853
854 }