summaryrefslogtreecommitdiff
path: root/src/mm_player_es.c
blob: 5033f13b770c6bc4f7c9723d388a50e07ed0e2b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
/*
 * libmm-player
 *
 * Copyright(c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: JongHyuk Choi <jhchoi.choi@samsung.com>, heechul jeon <heechul.jeon@samsung.co>,
 * YoungHwan An <younghwan_.an@samsung.com>, Eunhae Choi <eunhae1.choi@samsung.com>
 *
 * Licensed under the Apache License, Version 2.0(the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

/*===========================================================================================
|																							|
|  INCLUDE FILES																			|
|																							|
========================================================================================== */
#include <dlog.h>
#include "mm_player_es.h"
#include "mm_player_utils.h"
#include "mm_player_internal.h"

#include <gst/app/gstappsrc.h>

/*---------------------------------------------------------------------------
|    LOCAL VARIABLE DEFINITIONS for internal								|
---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------
|    LOCAL FUNCTION PROTOTYPES:												|
---------------------------------------------------------------------------*/
static int __parse_media_format(MMPlayerVideoStreamInfo * video, MMPlayerAudioStreamInfo * audio, media_format_h format);
static int __convert_media_format_video_mime_to_str(MMPlayerVideoStreamInfo * video, media_format_mimetype_e mime);
static int __convert_media_format_audio_mime_to_str(MMPlayerAudioStreamInfo * audio, media_format_mimetype_e mime);

/*===========================================================================================
|																							|
|  FUNCTION DEFINITIONS																		|
|																							|
========================================================================================== */

static int
__convert_media_format_video_mime_to_str(MMPlayerVideoStreamInfo * video,
	media_format_mimetype_e mime)
{
	MMPLAYER_RETURN_VAL_IF_FAIL(video, MM_ERROR_INVALID_ARGUMENT);

	switch (mime) {
	case MEDIA_FORMAT_MPEG4_SP:
		video->mime = g_strdup("video/mpeg");
		video->version = 4;
		break;
	case MEDIA_FORMAT_H264_SP:
	case MEDIA_FORMAT_H264_MP:
	case MEDIA_FORMAT_H264_HP:
		video->mime = g_strdup("video/x-h264");
		break;
	default:
		video->mime = g_strdup("unknown");
		break;
	}

	return MM_ERROR_NONE;
}

static int
__convert_media_format_audio_mime_to_str(MMPlayerAudioStreamInfo * audio,
	media_format_mimetype_e mime)
{
	MMPLAYER_RETURN_VAL_IF_FAIL(audio, MM_ERROR_INVALID_ARGUMENT);

	switch (mime) {
	case MEDIA_FORMAT_AAC:
		audio->mime = g_strdup("audio/mpeg");
		audio->version = 2;
		break;
	default:
		audio->mime = g_strdup("unknown");
		break;
	}

	return MM_ERROR_NONE;
}

static int
__parse_media_format(MMPlayerVideoStreamInfo * video,
	MMPlayerAudioStreamInfo * audio, media_format_h format)
{
	if (audio) {
		media_format_mimetype_e mime;
		int channel;
		int samplerate;
		int avg_bps;

		if (media_format_get_audio_info(format, &mime, &channel, &samplerate, NULL,
			&avg_bps) != MEDIA_FORMAT_ERROR_NONE) {
			LOGE("media_format_get_audio_info failed");
			return MM_ERROR_PLAYER_INTERNAL;
		}

		__convert_media_format_audio_mime_to_str(audio, mime);
		audio->sample_rate = samplerate;
		audio->channels = channel;
		//video->user_info = ;
	}

	if (video) {
#define DEFAULT_FRAMERATE_NUM 30
#define DEFAULT_FRAMERATE_DEN 1
		media_format_mimetype_e mime;
		int width = 0;
		int height = 0;
		int avg_bps = 0;
		int frame_rate = 0;

		if (media_format_get_video_info(format, &mime, &width, &height, &avg_bps,
			NULL) != MEDIA_FORMAT_ERROR_NONE) {
			LOGE("media_format_get_video_info failed");
			return MM_ERROR_PLAYER_INTERNAL;
		}

		if (media_format_get_video_frame_rate(format, &frame_rate))
			LOGW("failed to get video frame rate, will be set 30.");

		LOGD("frame_rate %d", frame_rate);

		__convert_media_format_video_mime_to_str(video, mime);

		video->width = width;
		video->height = height;
		video->framerate_num = (frame_rate > 0) ? (frame_rate) : (DEFAULT_FRAMERATE_NUM);
		video->framerate_den = DEFAULT_FRAMERATE_DEN;
	}

	return MM_ERROR_NONE;
}

static gboolean
__mmplayer_update_video_info(MMHandleType hplayer, media_format_h fmt)
{
	mm_player_t *player = (mm_player_t *) hplayer;
	gboolean ret = FALSE;
	GstStructure *str = NULL;
	media_format_mimetype_e mimetype = 0;
	gint cur_width = 0, width = 0;
	gint cur_height = 0, height = 0;

	MMPLAYER_FENTER();

	MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
	MMPLAYER_RETURN_VAL_IF_FAIL(fmt, FALSE);

	if (player->v_stream_caps) {
		str = gst_caps_get_structure(player->v_stream_caps, 0);
		if (!gst_structure_get_int(str, "width", &cur_width))
			LOGD("missing 'width' field in video caps");

		if (!gst_structure_get_int(str, "height", &cur_height))
			LOGD("missing 'height' field in video caps");

		media_format_get_video_info(fmt, &mimetype, &width, &height, NULL, NULL);
		if ((cur_width != width) || (cur_height != height)) {
			LOGW("resolution is changed %dx%d -> %dx%d",
				cur_width, cur_height, width, height);
			_mmplayer_set_video_info(hplayer, fmt);
			ret = TRUE;
		}
	}

	MMPLAYER_FLEAVE();
	return ret;
}


int
_mmplayer_set_media_stream_buffer_status_cb(MMHandleType hplayer,
											MMPlayerStreamType type,
											mm_player_media_stream_buffer_status_callback callback,
											void *user_param)
{
	mm_player_t *player = (mm_player_t *) hplayer;

	MMPLAYER_FENTER();

	MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);

	if ((type < MM_PLAYER_STREAM_TYPE_DEFAULT) || (type > MM_PLAYER_STREAM_TYPE_TEXT))
		return MM_ERROR_INVALID_ARGUMENT;

	if (player->media_stream_buffer_status_cb[type]) {
		if (!callback)
			LOGD("[type:%d] will be clear.\n", type);
		else
			LOGD("[type:%d] will be overwritten.\n", type);
	}

	player->media_stream_buffer_status_cb[type] = callback;
	player->buffer_cb_user_param = user_param;

	LOGD("player handle %p, type %d, callback %p\n", player, type,
		player->media_stream_buffer_status_cb[type]);

	MMPLAYER_FLEAVE();

	return MM_ERROR_NONE;
}

int
_mmplayer_set_media_stream_seek_data_cb(MMHandleType hplayer,
										MMPlayerStreamType type,
										mm_player_media_stream_seek_data_callback callback,
										void *user_param)
{
	mm_player_t *player = (mm_player_t *) hplayer;

	MMPLAYER_FENTER();

	MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);

	if ((type < MM_PLAYER_STREAM_TYPE_DEFAULT) || (type > MM_PLAYER_STREAM_TYPE_TEXT))
		return MM_ERROR_INVALID_ARGUMENT;

	if (player->media_stream_seek_data_cb[type]) {
		if (!callback)
			LOGD("[type:%d] will be clear.\n", type);
		else
			LOGD("[type:%d] will be overwritten.\n", type);
	}

	player->media_stream_seek_data_cb[type] = callback;
	player->buffer_cb_user_param = user_param;

	LOGD("player handle %p, type %d, callback %p\n", player, type,
		player->media_stream_seek_data_cb[type]);

	MMPLAYER_FLEAVE();

	return MM_ERROR_NONE;
}

static GstElement*
__mmplayer_get_source_element(mm_player_t *player, MMPlayerStreamType type)
{
	enum MainElementID elemId = MMPLAYER_M_NUM;

	if (player && player->pipeline && player->pipeline->mainbin) {
		/* get elem according to the stream type */
		if (type == MM_PLAYER_STREAM_TYPE_AUDIO) {
			if (player->pipeline->mainbin[MMPLAYER_M_2ND_SRC].gst)
				elemId = MMPLAYER_M_2ND_SRC;
			else if (g_strrstr(GST_ELEMENT_NAME(player->pipeline->mainbin[MMPLAYER_M_SRC].gst), "audio_appsrc"))
				elemId = MMPLAYER_M_SRC;
		} else if (type == MM_PLAYER_STREAM_TYPE_VIDEO) {
			elemId = MMPLAYER_M_SRC;
		} else if (type == MM_PLAYER_STREAM_TYPE_TEXT) {
			elemId = MMPLAYER_M_SUBSRC;
		}

		if (elemId != MMPLAYER_M_NUM)
			return player->pipeline->mainbin[elemId].gst;
	}

	return NULL;
}

int
_mmplayer_set_media_stream_max_size(MMHandleType hplayer, MMPlayerStreamType type, guint64 max_size)
{
	mm_player_t *player = (mm_player_t *) hplayer;
	GstElement *element = NULL;

	MMPLAYER_FENTER();
	MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);

	if ((type < MM_PLAYER_STREAM_TYPE_AUDIO) ||
		(type > MM_PLAYER_STREAM_TYPE_TEXT) ||
		(max_size == 0)) {
		LOGE("Invalid param type:%d, max_size:%d", type, max_size);
		return MM_ERROR_INVALID_ARGUMENT;
	}

	LOGD("type %d, max_size %llu\n", type, max_size);

	if ((element = __mmplayer_get_source_element(player, type))) {
		LOGD("update max_size of %s\n", GST_ELEMENT_NAME(element));
		g_object_set(G_OBJECT(element), "max-bytes", max_size, NULL);
	}

	player->media_stream_buffer_max_size[type] = max_size;

	MMPLAYER_FLEAVE();
	return MM_ERROR_NONE;
}

int
_mmplayer_get_media_stream_max_size(MMHandleType hplayer, MMPlayerStreamType type, guint64 *max_size)
{
	mm_player_t *player = (mm_player_t *) hplayer;

	MMPLAYER_FENTER();

	MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
	MMPLAYER_RETURN_VAL_IF_FAIL(max_size, MM_ERROR_INVALID_ARGUMENT);

	if ((type < MM_PLAYER_STREAM_TYPE_DEFAULT) || (type > MM_PLAYER_STREAM_TYPE_TEXT))
		return MM_ERROR_INVALID_ARGUMENT;

	*max_size = player->media_stream_buffer_max_size[type];

	MMPLAYER_FLEAVE();

	return MM_ERROR_NONE;
}

int
_mmplayer_set_media_stream_min_percent(MMHandleType hplayer, MMPlayerStreamType type, guint min_percent)
{
	mm_player_t *player = (mm_player_t *) hplayer;
	GstElement *element = NULL;

	MMPLAYER_FENTER();

	MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);

	if ((type < MM_PLAYER_STREAM_TYPE_AUDIO) || (type > MM_PLAYER_STREAM_TYPE_TEXT)) {
		LOGE("Invalid param type:%d", type);
		return MM_ERROR_INVALID_ARGUMENT;
	}

	LOGD("type %d, min_per %u\n", type, min_percent);

	if ((element = __mmplayer_get_source_element(player, type))) {
		LOGD("update min_per of %s\n", GST_ELEMENT_NAME(element));
		g_object_set(G_OBJECT(element), "min-percent", min_percent, NULL);
	}

	player->media_stream_buffer_min_percent[type] = min_percent;

	MMPLAYER_FLEAVE();
	return MM_ERROR_NONE;
}

int
_mmplayer_get_media_stream_min_percent(MMHandleType hplayer, MMPlayerStreamType type, guint *min_percent)
{
	mm_player_t *player = (mm_player_t *) hplayer;

	MMPLAYER_FENTER();

	MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
	MMPLAYER_RETURN_VAL_IF_FAIL(min_percent, MM_ERROR_INVALID_ARGUMENT);

	if ((type < MM_PLAYER_STREAM_TYPE_DEFAULT) || (type > MM_PLAYER_STREAM_TYPE_TEXT))
		return MM_ERROR_INVALID_ARGUMENT;

	*min_percent = player->media_stream_buffer_min_percent[type];

	MMPLAYER_FLEAVE();

	return MM_ERROR_NONE;
}

static int
__mmplayer_check_buffer_level(mm_player_t *player, GstElement* element, MMPlayerStreamType type)
{
	guint64 current_level_bytes = 0;
	guint64 max_bytes = 0;
	guint current_level_per = 0;

	MMPLAYER_FENTER();
	MMPLAYER_RETURN_VAL_IF_FAIL(player && element, MM_ERROR_PLAYER_NOT_INITIALIZED);

	if (player->media_stream_buffer_max_size[type] > 0)
		max_bytes = player->media_stream_buffer_max_size[type];
	else
		g_object_get(G_OBJECT(element), "max-bytes", &max_bytes, NULL);

	if (max_bytes == 0) {
		LOGW("buffer max size is zero.");
		return MM_ERROR_NONE;
	}

	g_object_get(G_OBJECT(element), "current-level-bytes", &current_level_bytes, NULL);

	if (max_bytes <= current_level_bytes) {
		LOGE("no available buffer space. type %d, max %lld, curr %lld", type, max_bytes, current_level_bytes);
		return MM_ERROR_PLAYER_BUFFER_SPACE;
	}

	if (MMPLAYER_CURRENT_STATE(player) != MM_PLAYER_STATE_PLAYING) {
		if (!player->media_stream_buffer_status_cb[type])
			return MM_ERROR_NONE;

		current_level_per = (guint)(gst_util_guint64_to_gdouble(current_level_bytes)/gst_util_guint64_to_gdouble(max_bytes)*100);

		LOGD("type %d, min_per %u, curr_per %u max %lld cur %lld\n",
					type, player->media_stream_buffer_min_percent[type],
					current_level_per,
					player->media_stream_buffer_max_size[type],
					current_level_bytes);

		if (current_level_per < player->media_stream_buffer_min_percent[type])
			player->media_stream_buffer_status_cb[type](type, MM_PLAYER_MEDIA_STREAM_BUFFER_UNDERRUN, current_level_bytes, player->buffer_cb_user_param);
	}

	MMPLAYER_FLEAVE();
	return MM_ERROR_NONE;
}

int
_mmplayer_submit_packet(MMHandleType hplayer, media_packet_h packet)
{
	int ret = MM_ERROR_NONE;
	GstBuffer *_buffer = NULL;
	mm_player_t *player = (mm_player_t *) hplayer;
	guint8 *buf = NULL;
	uint64_t size = 0;
	GstElement* element = NULL;
	MMPlayerStreamType streamtype = MM_PLAYER_STREAM_TYPE_AUDIO;
	media_format_h fmt = NULL;
	bool flag = FALSE;
	bool is_eos = FALSE;

	MMPLAYER_RETURN_VAL_IF_FAIL(packet, MM_ERROR_INVALID_ARGUMENT);
	MMPLAYER_RETURN_VAL_IF_FAIL(player &&
	player->pipeline &&
	player->pipeline->mainbin &&
	player->pipeline->mainbin[MMPLAYER_M_SRC].gst,
	MM_ERROR_PLAYER_INTERNAL);

	/* get stream type if audio or video */
	media_packet_is_audio(packet, &flag);
	if (flag) {
		streamtype = MM_PLAYER_STREAM_TYPE_AUDIO;
	} else {
		media_packet_is_video(packet, &flag);
		if (flag)
			streamtype = MM_PLAYER_STREAM_TYPE_VIDEO;
		else
			streamtype = MM_PLAYER_STREAM_TYPE_TEXT;
	}

	element = __mmplayer_get_source_element(player, streamtype);
	if (!element) {
		LOGE("there is no source element of type %d", streamtype);
		ret = MM_ERROR_PLAYER_INTERNAL;
		goto ERROR;
	}

	/* check buffer level */
	ret = __mmplayer_check_buffer_level(player, element, streamtype);
	if (ret != MM_ERROR_NONE)
		return ret;

	/* get data */
	if (media_packet_get_buffer_data_ptr(packet, (void **) &buf) != MEDIA_PACKET_ERROR_NONE) {
		LOGE("failed to get buffer data ptr");
		ret = MM_ERROR_PLAYER_INTERNAL;
		goto ERROR;
	}

	if (media_packet_get_buffer_size(packet, &size) != MEDIA_PACKET_ERROR_NONE) {
		LOGE("failed to get buffer size");
		ret = MM_ERROR_PLAYER_INTERNAL;
		goto ERROR;
	}

	if (buf != NULL && size > 0) {
		GstMapInfo buff_info = GST_MAP_INFO_INIT;
		uint64_t pts = 0;

		/* get size */
		_buffer = gst_buffer_new_and_alloc(size);

		if (!_buffer) {
			LOGE("failed to allocate memory for push buffer\n");
			ret = MM_ERROR_PLAYER_NO_FREE_SPACE;
			goto ERROR;
		}

		if (gst_buffer_map(_buffer, &buff_info, GST_MAP_READWRITE)) {
			memcpy(buff_info.data, buf, size);
			buff_info.size = size;

			gst_buffer_unmap(_buffer, &buff_info);
		}

		if (streamtype == MM_PLAYER_STREAM_TYPE_VIDEO) {
			/* get format to check video format */
			media_packet_get_format(packet, &fmt);
			if (fmt) {
				if (__mmplayer_update_video_info(hplayer, fmt)) {
					LOGD("update video caps");
					g_object_set(G_OBJECT(player->pipeline->mainbin[MMPLAYER_M_SRC].gst),
						"caps", player->v_stream_caps, NULL);
				}
				media_format_unref(fmt);
			}
		}

		/* get pts */
		if (media_packet_get_pts(packet, &pts) != MEDIA_PACKET_ERROR_NONE) {
		LOGE("failed to get pts info");
		ret = MM_ERROR_PLAYER_INTERNAL;
		goto ERROR;
		}
		GST_BUFFER_PTS(_buffer) = (GstClockTime)pts;
		gst_app_src_push_buffer(GST_APP_SRC(element), _buffer);
	}

	/* check eos */
	if (media_packet_is_end_of_stream(packet, &is_eos) != MEDIA_PACKET_ERROR_NONE) {
		LOGE("failed to get eos info");
		ret = MM_ERROR_PLAYER_INTERNAL;
		goto ERROR;
	}

	if (is_eos) {
		LOGW("we got eos of stream type(%d)", streamtype);
		g_signal_emit_by_name(element, "end-of-stream", &ret);
	}

ERROR:
	return ret;
}

static int
__mmplayer_video_caps_new(MMHandleType hplayer, MMPlayerVideoStreamInfo * video,
	const char *fieldname, ...)
{
	int cap_size;
	GstCaps *caps = NULL;
	GstStructure *structure = NULL;
	va_list var_args;
	mm_player_t *player = MM_PLAYER_CAST(hplayer);

	MMPLAYER_FENTER();
	MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
	MMPLAYER_RETURN_VAL_IF_FAIL(video, MM_ERROR_PLAYER_NOT_INITIALIZED);

	LOGD("width=%d height=%d framerate num=%d, den=%d",
	video->width, video->height, video->framerate_num, video->framerate_den);

	caps = gst_caps_new_simple(video->mime,
		"width", G_TYPE_INT, video->width,
		"height", G_TYPE_INT, video->height,
		"framerate", GST_TYPE_FRACTION, video->framerate_num, video->framerate_den, NULL);

	for (cap_size = 0; cap_size < gst_caps_get_size(caps); cap_size++) {
		va_start(var_args, fieldname);
		structure = gst_caps_get_structure(caps, cap_size);
		gst_structure_set_valist(structure, fieldname, var_args);
		va_end(var_args);
	}

	if (video->extradata_size) {
		GstBuffer *buf = NULL;
		GstMapInfo buff_info = GST_MAP_INFO_INIT;

		buf = gst_buffer_new_and_alloc(video->extradata_size);

		if (gst_buffer_map(buf, &buff_info, GST_MAP_READ)) {
			memcpy(buff_info.data, video->codec_extradata, video->extradata_size);
			buff_info.size = video->extradata_size;
			gst_buffer_unmap(buf, &buff_info);
		}

		gst_caps_set_simple(caps, "codec_data", GST_TYPE_BUFFER, buf, NULL);
		gst_buffer_unref(buf);
	}

	if (player->v_stream_caps) {
		LOGW("caps will be updated ");

		gst_caps_unref(player->v_stream_caps);
		player->v_stream_caps = NULL;
	}

	player->v_stream_caps = gst_caps_copy(caps);
	MMPLAYER_LOG_GST_CAPS_TYPE(player->v_stream_caps);
	gst_caps_unref(caps);

	MMPLAYER_FLEAVE();

	return MM_ERROR_NONE;
}

static void
__mmplayer_set_uri_type(mm_player_t *player)
{
	MMPLAYER_FENTER();

	player->profile.uri_type = MM_PLAYER_URI_TYPE_MS_BUFF;
	player->es_player_push_mode = TRUE;

	MMPLAYER_FLEAVE();
	return;
}

int
_mmplayer_set_video_info(MMHandleType hplayer, media_format_h format)
{
	mm_player_t *player = MM_PLAYER_CAST(hplayer);
	MMPlayerVideoStreamInfo video = { 0, };
	int ret = MM_ERROR_NONE;
	gboolean drc = FALSE;

	MMPLAYER_FENTER();

	MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);

	__mmplayer_set_uri_type(player);

	ret = __parse_media_format(&video, NULL, format);
	if (ret != MM_ERROR_NONE)
		return ret;

	mm_attrs_get_int_by_name(player->attrs, MM_PLAYER_DRC_MODE, &drc);

	if (strstr(video.mime, "video/mpeg")) {
		__mmplayer_video_caps_new(hplayer, &video,
		"mpegversion", G_TYPE_INT, video.version,
		"systemstream", G_TYPE_BOOLEAN, FALSE,
		"adaptive-streaming", G_TYPE_BOOLEAN, drc, NULL);
	} else if (strstr(video.mime, "video/x-h264")) {
		/*
		if (info.colordepth) {
			__mmplayer_video_caps_new(hplayer, &info,
				"colordepth", G_TYPE_INT, info.colordepth, NULL);
		} else
		*/
		{
			__mmplayer_video_caps_new(hplayer, &video,
			"stream-format", G_TYPE_STRING, "byte-stream",
			"alignment", G_TYPE_STRING, "au",
			"adaptive-streaming", G_TYPE_BOOLEAN, drc, NULL);
		}
	}
#if 0
	else if (strstr(info->mime, "video/x-wmv")) {
		__mmplayer_video_caps_new(hplayer, &info,
		"wmvversion", G_TYPE_INT, info.version, NULL);
	} else if (strstr(info.mime, "video/x-pn-realvideo")) {
		__mmplayer_video_caps_new(hplayer, &info,
		"rmversion", G_TYPE_INT, info.version, NULL);
	} else if (strstr(info.mime, "video/x-msmpeg")) {
		__mmplayer_video_caps_new(hplayer, &info,
		"msmpegversion", G_TYPE_INT, info.version, NULL);
	} else if (strstr(info.mime, "video/x-h265")) {
		if (info.colordepth)
			__mmplayer_video_caps_new(hplayer, &info,
			"colordepth", G_TYPE_INT, info.colordepth, NULL);
		else
			__mmplayer_video_caps_new(hplayer, &info, NULL);
	} else
		__mmplayer_video_caps_new(hplayer, &info, NULL);
#endif
	g_free((char *) video.mime);

	MMPLAYER_FLEAVE();

	return MM_ERROR_NONE;
}

int
_mmplayer_set_audio_info(MMHandleType hplayer, media_format_h format)
{
	mm_player_t *player = MM_PLAYER_CAST(hplayer);
	GstCaps *caps = NULL;
	MMPlayerAudioStreamInfo audio = { 0, };
	int ret = MM_ERROR_NONE;

	MMPLAYER_FENTER();

	MMPLAYER_RETURN_VAL_IF_FAIL(hplayer, MM_ERROR_PLAYER_NOT_INITIALIZED);

	__mmplayer_set_uri_type(player);

	ret = __parse_media_format(NULL, &audio, format);
	if (ret != MM_ERROR_NONE)
		return ret;

	audio.user_info = 0;           //test

	LOGD("set audio player[%p] info [%p] version=%d rate=%d channel=%d",
	player, audio, audio.version, audio.sample_rate, audio.channels);

	if (strstr(audio.mime, "audio/mpeg")) {
		if (audio.version == 1) {	// mp3
			caps = gst_caps_new_simple("audio/mpeg",
			"channels", G_TYPE_INT, audio.channels,
			"rate", G_TYPE_INT, audio.sample_rate,
			"mpegversion", G_TYPE_INT, audio.version,
			"layer", G_TYPE_INT, audio.user_info, NULL);
		} else {                    // aac
			gchar *format = NULL;

			if (audio.user_info == 0)
			format = g_strdup("raw");
			else if (audio.user_info == 1)
			format = g_strdup("adts");
			else if (audio.user_info == 2)
			format = g_strdup("adif");

			caps = gst_caps_new_simple("audio/mpeg",
			"channels", G_TYPE_INT, audio.channels,
			"rate", G_TYPE_INT, audio.sample_rate,
			"mpegversion", G_TYPE_INT, audio.version,
			"stream-format", G_TYPE_STRING, format, NULL);

			g_free(format);
			format = NULL;
		}
	}
#if 0
	else if (strstr(audio.mime, "audio/x-raw-int")) {
		caps = gst_caps_new_simple("audio/x-raw-int",
		"width", G_TYPE_INT, audio.width,
		"depth", G_TYPE_INT, audio.depth,
		"endianness", G_TYPE_INT, audio.endianness,
		"signed", G_TYPE_BOOLEAN, audio.signedness,
		"channels", G_TYPE_INT, audio.channels,
		"rate", G_TYPE_INT, audio.sample_rate, NULL);
	} else {
		caps = gst_caps_new_simple(audio.mime,
		"channels", G_TYPE_INT, audio.channels,
		"rate", G_TYPE_INT, audio.sample_rate, NULL);
	}
#endif

	if (audio.extradata_size) {
		GstBuffer *buf = NULL;
		GstMapInfo buff_info = GST_MAP_INFO_INIT;

		buf = gst_buffer_new_and_alloc(audio.extradata_size);

		if (gst_buffer_map(buf, &buff_info, GST_MAP_READ)) {
			memcpy(buff_info.data, audio.codec_extradata, audio.extradata_size);
			gst_buffer_unmap(buf, &buff_info);
		}

		gst_caps_set_simple(caps, "codec_data", GST_TYPE_BUFFER, buf, NULL);
		gst_buffer_unref(buf);
	}

	g_free((char *) audio.mime);

	player->a_stream_caps = gst_caps_copy(caps);
	gst_caps_unref(caps);

	MMPLAYER_FLEAVE();

	return MM_ERROR_NONE;
}

int
_mmplayer_set_subtitle_info(MMHandleType hplayer,
	MMPlayerSubtitleStreamInfo * subtitle)
{
#if 0                           //todo
	mm_player_t *player = MM_PLAYER_CAST(hplayer);
	GstCaps *caps = NULL;

	MMPLAYER_FENTER();

	MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
	MMPLAYER_RETURN_VAL_IF_FAIL(info, MM_ERROR_PLAYER_NOT_INITIALIZED);

	LOGD("set subtitle player[%p] info [%p]", player, info);


	caps = gst_caps_new_simple(info->mime, NULL, NULL);  // TO CHECK
	if (NULL == caps)
		return FALSE;

	if (strstr(info->mime, "application/x-xsub")) {
		gst_caps_set_simple(caps, "codec_tag", G_TYPE_UINT, info->codec_tag, NULL);
	} else if (strstr(info->mime, "application/x-smpte-text")) {
		if (info->context) {
			gst_caps_set_simple(caps, "ttml_priv_data", G_TYPE_POINTER,
			info->context, NULL);
		}
	}

	player->s_stream_caps = gst_caps_copy(caps);

	gst_caps_unref(caps);
#endif

	MMPLAYER_FLEAVE();

	return MM_ERROR_NONE;
}

int
_mmplayer_set_media_stream_dynamic_resolution(MMHandleType hplayer, bool drc)
{
	mm_player_t *player = MM_PLAYER_CAST(hplayer);
	int ret = MM_ERROR_NONE;

	MMPLAYER_FENTER();

	MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);

	mm_attrs_set_int_by_name(player->attrs, MM_PLAYER_DRC_MODE, (int)drc);
	if (player->v_stream_caps) {
		gst_caps_set_simple(player->v_stream_caps,
			"adaptive-streaming", G_TYPE_BOOLEAN, drc, NULL);
		MMPLAYER_LOG_GST_CAPS_TYPE(player->v_stream_caps);
	}

	MMPLAYER_FLEAVE();

	return ret;
}