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