4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
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>
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 /*===========================================================================================
27 ========================================================================================== */
28 #include "mm_player_es.h"
29 #include "mm_player_utils.h"
30 #include "mm_player_internal.h"
32 #include <gst/app/gstappsrc.h>
34 /*---------------------------------------------------------------------------
35 | LOCAL VARIABLE DEFINITIONS for internal |
36 ---------------------------------------------------------------------------*/
38 /*---------------------------------------------------------------------------
39 | LOCAL FUNCTION PROTOTYPES: |
40 ---------------------------------------------------------------------------*/
41 static int _parse_media_format (MMPlayerVideoStreamInfo * video, MMPlayerAudioStreamInfo * audio, media_format_h format);
42 static int _convert_media_format_video_mime_to_str (MMPlayerVideoStreamInfo * video, media_format_mimetype_e mime);
43 static int _convert_media_format_audio_mime_to_str (MMPlayerAudioStreamInfo * audio, media_format_mimetype_e mime);
45 /*===========================================================================================
47 | FUNCTION DEFINITIONS |
49 ========================================================================================== */
52 _convert_media_format_video_mime_to_str (MMPlayerVideoStreamInfo * video,
53 media_format_mimetype_e mime)
55 return_val_if_fail (video, MM_ERROR_INVALID_ARGUMENT);
58 case MEDIA_FORMAT_MPEG4_SP:
59 video->mime = g_strdup ("video/mpeg");
62 case MEDIA_FORMAT_H264_SP:
63 case MEDIA_FORMAT_H264_MP:
64 case MEDIA_FORMAT_H264_HP:
65 video->mime = g_strdup ("video/x-h264");
68 video->mime = g_strdup ("unknown");
76 _convert_media_format_audio_mime_to_str (MMPlayerAudioStreamInfo * audio,
77 media_format_mimetype_e mime)
79 return_val_if_fail (audio, MM_ERROR_INVALID_ARGUMENT);
82 case MEDIA_FORMAT_AAC:
83 audio->mime = g_strdup ("audio/mpeg");
87 audio->mime = g_strdup ("unknown");
95 _parse_media_format (MMPlayerVideoStreamInfo * video,
96 MMPlayerAudioStreamInfo * audio, media_format_h format)
99 media_format_mimetype_e mime;
104 if (media_format_get_audio_info (format, &mime, &channel, &samplerate, NULL,
105 &avg_bps) != MEDIA_FORMAT_ERROR_NONE) {
106 debug_error ("media_format_get_audio_info failed");
107 return MM_ERROR_PLAYER_INTERNAL;
110 _convert_media_format_audio_mime_to_str (audio, mime);
111 audio->sample_rate = samplerate;
112 audio->channels = channel;
113 //video->user_info = ;
117 media_format_mimetype_e mime;
122 if (media_format_get_video_info (format, &mime, &width, &height, &avg_bps,
123 NULL) != MEDIA_FORMAT_ERROR_NONE) {
124 debug_error ("media_format_get_video_info failed");
125 return MM_ERROR_PLAYER_INTERNAL;
128 _convert_media_format_video_mime_to_str (video, mime);
129 video->width = width;
130 video->height = height;
133 return MM_ERROR_NONE;
137 _mmplayer_update_video_info(MMHandleType hplayer, media_format_h fmt)
139 mm_player_t *player = (mm_player_t *) hplayer;
140 gboolean ret = FALSE;
141 GstStructure *str = NULL;
142 media_format_mimetype_e mimetype = 0;
143 gint cur_width = 0, width = 0;
144 gint cur_height = 0, height = 0;
148 return_val_if_fail (player, FALSE);
149 return_val_if_fail (fmt, FALSE);
151 if (player->v_stream_caps)
153 str = gst_caps_get_structure (player->v_stream_caps, 0);
154 if ( !gst_structure_get_int (str, "width", &cur_width))
156 debug_log ("missing 'width' field in video caps");
159 if ( !gst_structure_get_int (str, "height", &cur_height))
161 debug_log ("missing 'height' field in video caps");
164 media_format_get_video_info(fmt, &mimetype, &width, &height, NULL, NULL);
165 if ((cur_width != width) || (cur_height != height))
167 debug_warning ("resolution is changed %dx%d -> %dx%d",
168 cur_width, cur_height, width, height);
169 _mmplayer_set_video_info(hplayer, fmt);
180 _mmplayer_set_media_stream_buffer_status_cb(MMHandleType hplayer,
181 MMPlayerStreamType type,
182 mm_player_media_stream_buffer_status_callback callback,
185 mm_player_t *player = (mm_player_t *) hplayer;
189 return_val_if_fail (player, MM_ERROR_PLAYER_NOT_INITIALIZED);
191 if ((type < MM_PLAYER_STREAM_TYPE_DEFAULT) || (type > MM_PLAYER_STREAM_TYPE_TEXT))
192 return MM_ERROR_INVALID_ARGUMENT;
194 if (player->media_stream_buffer_status_cb[type])
198 debug_log ("[type:%d] will be clear.\n", type);
202 debug_log ("[type:%d] will be overwritten.\n", type);
206 player->media_stream_buffer_status_cb[type] = callback;
207 player->buffer_cb_user_param = user_param;
209 debug_log ("player handle %p, type %d, callback %p\n", player, type,
210 player->media_stream_buffer_status_cb[type]);
214 return MM_ERROR_NONE;
218 _mmplayer_set_media_stream_seek_data_cb(MMHandleType hplayer,
219 MMPlayerStreamType type,
220 mm_player_media_stream_seek_data_callback callback,
223 mm_player_t *player = (mm_player_t *) hplayer;
227 return_val_if_fail (player, MM_ERROR_PLAYER_NOT_INITIALIZED);
229 if ((type < MM_PLAYER_STREAM_TYPE_DEFAULT) || (type > MM_PLAYER_STREAM_TYPE_TEXT))
230 return MM_ERROR_INVALID_ARGUMENT;
232 if (player->media_stream_seek_data_cb[type])
236 debug_log ("[type:%d] will be clear.\n", type);
240 debug_log ("[type:%d] will be overwritten.\n", type);
244 player->media_stream_seek_data_cb[type] = callback;
245 player->buffer_cb_user_param = user_param;
247 debug_log ("player handle %p, type %d, callback %p\n", player, type,
248 player->media_stream_seek_data_cb[type]);
252 return MM_ERROR_NONE;
256 _mmplayer_set_media_stream_max_size(MMHandleType hplayer, MMPlayerStreamType type, guint64 max_size)
258 mm_player_t *player = (mm_player_t *) hplayer;
262 return_val_if_fail (player, MM_ERROR_PLAYER_NOT_INITIALIZED);
264 if ((type < MM_PLAYER_STREAM_TYPE_DEFAULT) || (type > MM_PLAYER_STREAM_TYPE_TEXT))
265 return MM_ERROR_INVALID_ARGUMENT;
267 player->media_stream_buffer_max_size[type] = max_size;
269 debug_log ("type %d, max_size %llu\n",
270 type, player->media_stream_buffer_max_size[type]);
274 return MM_ERROR_NONE;
278 _mmplayer_get_media_stream_max_size(MMHandleType hplayer, MMPlayerStreamType type, guint64 *max_size)
280 mm_player_t *player = (mm_player_t *) hplayer;
284 return_val_if_fail (player, MM_ERROR_PLAYER_NOT_INITIALIZED);
285 return_val_if_fail (max_size, MM_ERROR_INVALID_ARGUMENT);
287 if ((type < MM_PLAYER_STREAM_TYPE_DEFAULT) || (type > MM_PLAYER_STREAM_TYPE_TEXT))
288 return MM_ERROR_INVALID_ARGUMENT;
290 *max_size = player->media_stream_buffer_max_size[type];
294 return MM_ERROR_NONE;
298 _mmplayer_set_media_stream_min_percent(MMHandleType hplayer, MMPlayerStreamType type, guint min_percent)
300 mm_player_t *player = (mm_player_t *) hplayer;
304 return_val_if_fail (player, MM_ERROR_PLAYER_NOT_INITIALIZED);
306 if ((type < MM_PLAYER_STREAM_TYPE_DEFAULT) || (type > MM_PLAYER_STREAM_TYPE_TEXT))
307 return MM_ERROR_INVALID_ARGUMENT;
309 player->media_stream_buffer_min_percent[type] = min_percent;
311 debug_log ("type %d, min_per %u\n",
312 type, player->media_stream_buffer_min_percent[type]);
316 return MM_ERROR_NONE;
320 _mmplayer_get_media_stream_min_percent(MMHandleType hplayer, MMPlayerStreamType type, guint *min_percent)
322 mm_player_t *player = (mm_player_t *) hplayer;
326 return_val_if_fail (player, MM_ERROR_PLAYER_NOT_INITIALIZED);
327 return_val_if_fail (min_percent, MM_ERROR_INVALID_ARGUMENT);
329 if ((type < MM_PLAYER_STREAM_TYPE_DEFAULT) || (type > MM_PLAYER_STREAM_TYPE_TEXT))
330 return MM_ERROR_INVALID_ARGUMENT;
332 *min_percent = player->media_stream_buffer_min_percent[type];
336 return MM_ERROR_NONE;
340 _mmplayer_submit_packet (MMHandleType hplayer, media_packet_h packet)
342 int ret = MM_ERROR_NONE;
343 GstBuffer *_buffer = NULL;
344 mm_player_t *player = (mm_player_t *) hplayer;
346 MMPlayerTrackType streamtype = MM_PLAYER_TRACK_TYPE_AUDIO;
347 media_format_h fmt = NULL;
350 return_val_if_fail (packet, MM_ERROR_INVALID_ARGUMENT);
351 return_val_if_fail ( player &&
353 player->pipeline->mainbin &&
354 player->pipeline->mainbin[MMPLAYER_M_SRC].gst,
355 MM_ERROR_PLAYER_INTERNAL );
357 /* get stream type if audio or video */
358 media_packet_is_audio (packet, &flag);
360 streamtype = MM_PLAYER_TRACK_TYPE_AUDIO;
362 media_packet_is_video (packet, &flag);
365 streamtype = MM_PLAYER_TRACK_TYPE_VIDEO;
367 streamtype = MM_PLAYER_TRACK_TYPE_TEXT;
371 media_packet_get_buffer_data_ptr (packet, (void **) &buf);
374 GstMapInfo buff_info = GST_MAP_INFO_INIT;
379 media_packet_get_buffer_size (packet, &size);
381 _buffer = gst_buffer_new_and_alloc (size);
383 debug_error("failed to allocate memory for push buffer\n");
384 return MM_ERROR_PLAYER_NO_FREE_SPACE;
387 if (gst_buffer_map (_buffer, &buff_info, GST_MAP_READWRITE)) {
389 memcpy (buff_info.data, buf, size);
390 buff_info.size = size;
392 gst_buffer_unmap (_buffer, &buff_info);
396 media_packet_get_pts (packet, &pts);
397 GST_BUFFER_PTS (_buffer) = (GstClockTime) (pts * 1000000);
399 if (streamtype == MM_PLAYER_TRACK_TYPE_AUDIO) {
400 #if 0 // TO CHECK : has gone (set to pad)
401 if (GST_CAPS_IS_SIMPLE (player->a_stream_caps))
402 GST_BUFFER_CAPS (_buffer) = gst_caps_copy (player->a_stream_caps);
404 debug_error ("External Demuxer case: Audio Buffer Caps not set.");
406 if (player->pipeline->mainbin[MMPLAYER_M_2ND_SRC].gst)
407 gst_app_src_push_buffer (GST_APP_SRC (player->pipeline->mainbin[MMPLAYER_M_2ND_SRC].gst), _buffer);
408 else if (g_strrstr (GST_ELEMENT_NAME (player->pipeline->mainbin[MMPLAYER_M_SRC].gst), "audio_appsrc"))
409 gst_app_src_push_buffer (GST_APP_SRC (player->pipeline->mainbin[MMPLAYER_M_SRC].gst), _buffer);
410 } else if (streamtype == MM_PLAYER_TRACK_TYPE_VIDEO) {
411 #if 0 // TO CHECK : has gone (set to pad)
412 if (GST_CAPS_IS_SIMPLE (player->v_stream_caps))
413 GST_BUFFER_CAPS (_buffer) = gst_caps_copy (player->v_stream_caps);
415 debug_error ("External Demuxer case: Video Buffer Caps not set.");
417 /* get format to check video format */
418 media_packet_get_format (packet, &fmt);
421 gboolean ret = FALSE;
422 ret = _mmplayer_update_video_info(hplayer, fmt);
425 g_object_set(G_OBJECT(player->pipeline->mainbin[MMPLAYER_M_SRC].gst),
426 "caps", player->v_stream_caps, NULL);
430 gst_app_src_push_buffer (GST_APP_SRC (player->pipeline->mainbin[MMPLAYER_M_SRC].gst), _buffer);
431 } else if (streamtype == MM_PLAYER_TRACK_TYPE_TEXT) {
432 #if 0 // TO CHECK : has gone (set to pad)
433 if (GST_CAPS_IS_SIMPLE (player->s_stream_caps))
434 GST_BUFFER_CAPS (_buffer) = gst_caps_copy (player->s_stream_caps);
436 debug_error ("External Demuxer case: Subtitle Buffer Caps not set.");
438 gst_app_src_push_buffer (GST_APP_SRC (player->pipeline->mainbin[MMPLAYER_M_SUBSRC].gst), _buffer);
440 debug_error ("Not a valid packet from external demux");
444 debug_log ("Sending EOS on pipeline...");
445 if (streamtype == MM_PLAYER_TRACK_TYPE_AUDIO) {
446 if (player->pipeline->mainbin[MMPLAYER_M_2ND_SRC].gst)
447 g_signal_emit_by_name (player->pipeline->
448 mainbin[MMPLAYER_M_2ND_SRC].gst, "end-of-stream", &ret);
450 g_signal_emit_by_name (player->pipeline->mainbin[MMPLAYER_M_SRC].gst,
451 "end-of-stream", &ret);
452 } else if (streamtype == MM_PLAYER_TRACK_TYPE_VIDEO) {
453 g_signal_emit_by_name (player->pipeline->mainbin[MMPLAYER_M_SRC].gst,
454 "end-of-stream", &ret);
455 } else if (streamtype == MM_PLAYER_TRACK_TYPE_TEXT) {
456 g_signal_emit_by_name (player->pipeline->mainbin[MMPLAYER_M_SUBSRC].gst,
457 "end-of-stream", &ret);
461 if (MMPLAYER_PENDING_STATE (player) == MM_PLAYER_STATE_PLAYING) {
462 //ret = __mmplayer_set_state(player, MM_PLAYER_STATE_PLAYING);
469 _mmplayer_video_caps_new (MMHandleType hplayer, MMPlayerVideoStreamInfo * video,
470 const char *fieldname, ...)
473 GstCaps *caps = NULL;
474 GstStructure *structure = NULL;
476 mm_player_t *player = MM_PLAYER_CAST (hplayer);
479 return_val_if_fail (player, MM_ERROR_PLAYER_NOT_INITIALIZED);
480 return_val_if_fail (video, MM_ERROR_PLAYER_NOT_INITIALIZED);
482 debug_log ("width=%d height=%d framerate num=%d, den=%d",
483 video->width, video->height, video->framerate_num, video->framerate_den);
485 caps = gst_caps_new_simple (video->mime,
486 "width", G_TYPE_INT, video->width,
487 "height", G_TYPE_INT, video->height,
488 "framerate", GST_TYPE_FRACTION, video->framerate_num, video->framerate_den, NULL);
490 for (cap_size = 0; cap_size < gst_caps_get_size (caps); cap_size++) {
491 va_start (var_args, fieldname);
492 structure = gst_caps_get_structure (caps, cap_size);
493 gst_structure_set_valist (structure, fieldname, var_args);
497 if (video->extradata_size) {
498 GstBuffer *buf = NULL;
499 GstMapInfo buff_info = GST_MAP_INFO_INIT;
501 buf = gst_buffer_new_and_alloc (video->extradata_size);
503 if (gst_buffer_map (buf, &buff_info, GST_MAP_READ)) {
504 memcpy (buff_info.data, video->codec_extradata, video->extradata_size);
505 buff_info.size = video->extradata_size;
506 gst_buffer_unmap (buf, &buff_info);
509 gst_caps_set_simple (caps, "codec_data", GST_TYPE_BUFFER, buf, NULL);
510 gst_buffer_unref (buf);
513 if (player->v_stream_caps)
515 debug_warning ("caps will be updated ");
517 gst_caps_unref(player->v_stream_caps);
518 player->v_stream_caps = NULL;
521 player->v_stream_caps = gst_caps_copy (caps);
522 MMPLAYER_LOG_GST_CAPS_TYPE (player->v_stream_caps);
523 gst_caps_unref (caps);
527 return MM_ERROR_NONE;
531 _mmplayer_set_video_info (MMHandleType hplayer, media_format_h format)
533 mm_player_t *player = MM_PLAYER_CAST (hplayer);
534 MMPlayerVideoStreamInfo video = { 0, };
535 int ret = MM_ERROR_NONE;
539 return_val_if_fail (player, MM_ERROR_PLAYER_NOT_INITIALIZED);
541 ret = _parse_media_format (&video, NULL, format);
542 if(ret != MM_ERROR_NONE)
545 if (strstr (video.mime, "video/mpeg")) {
546 _mmplayer_video_caps_new (hplayer, &video,
547 "mpegversion", G_TYPE_INT, video.version,
548 "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
549 } else if (strstr (video.mime, "video/x-h264")) {
550 //if (info.colordepth)
552 // _mmplayer_video_caps_new(hplayer, &info,
553 // "colordepth", G_TYPE_INT, info.colordepth, NULL);
557 _mmplayer_video_caps_new (hplayer, &video,
558 "stream-format", G_TYPE_STRING, "byte-stream",
559 "alignment", G_TYPE_STRING, "au", NULL);
563 else if (strstr (info->mime, "video/x-wmv")) {
564 _mmplayer_video_caps_new (hplayer, &info,
565 "wmvversion", G_TYPE_INT, info.version, NULL);
566 } else if (strstr (info.mime, "video/x-pn-realvideo")) {
567 _mmplayer_video_caps_new (hplayer, &info,
568 "rmversion", G_TYPE_INT, info.version, NULL);
569 } else if (strstr (info.mime, "video/x-msmpeg")) {
570 _mmplayer_video_caps_new (hplayer, &info,
571 "msmpegversion", G_TYPE_INT, info.version, NULL);
572 } else if (strstr (info.mime, "video/x-h265")) {
573 if (info.colordepth) {
574 _mmplayer_video_caps_new (hplayer, &info,
575 "colordepth", G_TYPE_INT, info.colordepth, NULL);
577 _mmplayer_video_caps_new (hplayer, &info, NULL);
580 _mmplayer_video_caps_new (hplayer, &info, NULL);
583 g_free ((char *) video.mime);
587 return MM_ERROR_NONE;
591 _mmplayer_set_audio_info (MMHandleType hplayer, media_format_h format)
593 mm_player_t *player = MM_PLAYER_CAST (hplayer);
594 GstCaps *caps = NULL;
595 MMPlayerAudioStreamInfo audio = { 0, };
596 int ret = MM_ERROR_NONE;
600 return_val_if_fail (hplayer, MM_ERROR_PLAYER_NOT_INITIALIZED);
602 ret = _parse_media_format (NULL, &audio, format);
603 if(ret != MM_ERROR_NONE)
606 audio.user_info = 0; //test
608 debug_log ("set audio player[%p] info [%p] version=%d rate=%d channel=%d",
609 player, audio, audio.version, audio.sample_rate, audio.channels);
611 if (strstr (audio.mime, "audio/mpeg")) {
612 if (audio.version == 1) { // mp3
613 caps = gst_caps_new_simple ("audio/mpeg",
614 "channels", G_TYPE_INT, audio.channels,
615 "rate", G_TYPE_INT, audio.sample_rate,
616 "mpegversion", G_TYPE_INT, audio.version,
617 "layer", G_TYPE_INT, audio.user_info, NULL);
619 gchar *format = NULL;
621 if (audio.user_info == 0)
622 format = g_strdup ("raw");
623 else if (audio.user_info == 1)
624 format = g_strdup ("adts");
625 else if (audio.user_info == 2)
626 format = g_strdup ("adif");
628 caps = gst_caps_new_simple ("audio/mpeg",
629 "channels", G_TYPE_INT, audio.channels,
630 "rate", G_TYPE_INT, audio.sample_rate,
631 "mpegversion", G_TYPE_INT, audio.version,
632 "stream-format", G_TYPE_STRING, format, NULL);
639 else if (strstr (audio.mime, "audio/x-raw-int")) {
640 caps = gst_caps_new_simple ("audio/x-raw-int",
641 "width", G_TYPE_INT, audio.width,
642 "depth", G_TYPE_INT, audio.depth,
643 "endianness", G_TYPE_INT, audio.endianness,
644 "signed", G_TYPE_BOOLEAN, audio.signedness,
645 "channels", G_TYPE_INT, audio.channels,
646 "rate", G_TYPE_INT, audio.sample_rate, NULL);
648 caps = gst_caps_new_simple (audio.mime,
649 "channels", G_TYPE_INT, audio.channels,
650 "rate", G_TYPE_INT, audio.sample_rate, NULL);
654 if (audio.extradata_size) {
655 GstBuffer *buf = NULL;
656 GstMapInfo buff_info = GST_MAP_INFO_INIT;
658 buf = gst_buffer_new_and_alloc (audio.extradata_size);
660 if (gst_buffer_map (buf, &buff_info, GST_MAP_READ)) {
661 memcpy (buff_info.data, audio.codec_extradata, audio.extradata_size);
662 gst_buffer_unmap (buf, &buff_info);
665 gst_caps_set_simple (caps, "codec_data", GST_TYPE_BUFFER, buf, NULL);
666 gst_buffer_unref (buf);
669 g_free ((char *) audio.mime);
671 player->a_stream_caps = gst_caps_copy (caps);
672 gst_caps_unref (caps);
676 return MM_ERROR_NONE;
680 _mmplayer_set_subtitle_info (MMHandleType hplayer,
681 MMPlayerSubtitleStreamInfo * subtitle)
685 mm_player_t *player = MM_PLAYER_CAST (hplayer);
686 GstCaps *caps = NULL;
690 return_val_if_fail (player, MM_ERROR_PLAYER_NOT_INITIALIZED);
691 return_val_if_fail (info, MM_ERROR_PLAYER_NOT_INITIALIZED);
693 debug_log ("set subtitle player[%p] info [%p]", player, info);
696 caps = gst_caps_new_simple (info->mime, NULL, NULL); // TO CHECK
700 if (strstr (info->mime, "application/x-xsub")) {
701 gst_caps_set_simple (caps, "codec_tag", G_TYPE_UINT, info->codec_tag, NULL);
702 } else if (strstr (info->mime, "application/x-smpte-text")) {
704 gst_caps_set_simple (caps, "ttml_priv_data", G_TYPE_POINTER,
705 info->context, NULL);
709 player->s_stream_caps = gst_caps_copy (caps);
711 gst_caps_unref (caps);
716 return MM_ERROR_NONE;