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