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