Unmap buffer after use
[platform/core/multimedia/libmm-camcorder.git] / src / mm_camcorder_gstcommon.c
1 /*
2  * libmm-camcorder
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jeongmo Yang <jm80.yang@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 /*=======================================================================================
23 |  INCLUDE FILES                                                                        |
24 =======================================================================================*/
25 #include <gst/allocators/gsttizenmemory.h>
26 #include <gst/audio/audio-format.h>
27 #include <gst/video/videooverlay.h>
28
29 #include <sys/time.h>
30 #include <unistd.h>
31 #include <tbm_bufmgr.h>
32 #include <tbm_surface.h>
33 #include <tbm_surface_internal.h>
34
35 #include "mm_camcorder_internal.h"
36 #include "mm_camcorder_gstcommon.h"
37
38 /*-----------------------------------------------------------------------
39 |    GLOBAL VARIABLE DEFINITIONS for internal                           |
40 -----------------------------------------------------------------------*/
41 /* Table for compatibility between audio codec and file format */
42 static gboolean audiocodec_fileformat_compatibility_table[MM_AUDIO_CODEC_NUM][MM_FILE_FORMAT_NUM] = {
43                    /* 3GP ASF AVI MATROSKA MP4 OGG NUT QT REAL AMR AAC MP3 AIFF AU WAV MID MMF DIVX FLV VOB IMELODY WMA WMV JPG FLAC M2TS*/
44 /*AMR*/       { 1,  0,  0,       0,  0,  0,  0, 0,   0,  1,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
45 /*G723.1*/    { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
46 /*MP3*/       { 0,  1,  1,       1,  1,  0,  0, 1,   0,  0,  0,  1,   0, 0,  0,  0,  0,   0,  1,  0,      0,  0,  0,  0,   0,   1},
47 /*OGG*/       { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
48 /*AAC*/       { 1,  0,  1,       1,  1,  0,  0, 0,   0,  0,  1,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   1},
49 /*WMA*/       { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
50 /*MMF*/       { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
51 /*ADPCM*/     { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
52 /*WAVE*/      { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  1,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
53 /*WAVE_NEW*/  { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
54 /*MIDI*/      { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
55 /*IMELODY*/   { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
56 /*MXMF*/      { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
57 /*MPA*/       { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
58 /*MP2*/       { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
59 /*G711*/      { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
60 /*G722*/      { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
61 /*G722.1*/    { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
62 /*G722.2*/    { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
63 /*G723*/      { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
64 /*G726*/      { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
65 /*G728*/      { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
66 /*G729*/      { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
67 /*G729A*/     { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
68 /*G729.1*/    { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
69 /*REAL*/      { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
70 /*AAC_LC*/    { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
71 /*AAC_MAIN*/  { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
72 /*AAC_SRS*/   { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
73 /*AAC_LTP*/   { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
74 /*AAC_HE_V1*/ { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
75 /*AAC_HE_V2*/ { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
76 /*AC3*/       { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   1},
77 /*ALAC*/      { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
78 /*ATRAC*/     { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
79 /*SPEEX*/     { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
80 /*VORBIS*/    { 0,  0,  0,       0,  0,  1,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
81 /*AIFF*/      { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
82 /*AU*/        { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
83 /*NONE*/      { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
84 /*PCM*/       { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
85 /*ALAW*/      { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
86 /*MULAW*/     { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
87 /*MS_ADPCM*/  { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
88 };
89
90 /* Table for compatibility between video codec and file format */
91 static gboolean videocodec_fileformat_compatibility_table[MM_VIDEO_CODEC_NUM][MM_FILE_FORMAT_NUM] = {
92                           /* 3GP ASF AVI MATROSKA MP4 OGG NUT QT REAL AMR AAC MP3 AIFF AU WAV MID MMF DIVX FLV VOB IMELODY WMA WMV JPG FLAC M2TS*/
93 /*NONE*/         { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
94 /*H263*/         { 1,  0,  1,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
95 /*H264*/         { 1,  0,  1,       1,  1,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   1},
96 /*H26L*/         { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
97 /*MPEG4*/        { 1,  0,  1,       1,  1,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   1},
98 /*MPEG1*/        { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
99 /*WMV*/          { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
100 /*DIVX*/         { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
101 /*XVID*/         { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
102 /*H261*/         { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
103 /*H262*/         { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
104 /*H263V2*/       { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
105 /*H263V3*/       { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
106 /*MJPEG*/        { 0,  0,  1,       1,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
107 /*MPEG2*/        { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
108 /*MPEG4_SIMPLE*/ { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
109 /*MPEG4_ADV*/    { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
110 /*MPEG4_MAIN*/   { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
111 /*MPEG4_CORE*/   { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
112 /*MPEG4_ACE*/    { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
113 /*MPEG4_ARTS*/   { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
114 /*MPEG4_AVC*/    { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
115 /*REAL*/         { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
116 /*VC1*/          { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
117 /*AVS*/          { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
118 /*CINEPAK*/      { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
119 /*INDEO*/        { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
120 /*THEORA*/       { 0,  0,  0,       0,  0,  1,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,   0,   0},
121 };
122
123
124 /*-----------------------------------------------------------------------
125 |    LOCAL VARIABLE DEFINITIONS for internal                            |
126 -----------------------------------------------------------------------*/
127 #define USE_AUDIO_CLOCK_TUNE
128 #define _MMCAMCORDER_WAIT_EOS_TIME                60.0     /* sec */
129 #define _MMCAMCORDER_CONVERT_OUTPUT_BUFFER_NUM    6
130 #define _MMCAMCORDER_NANOSEC_PER_1SEC             1000000000
131 #define _MMCAMCORDER_NANOSEC_PER_1MILISEC         1000
132
133
134 /*-----------------------------------------------------------------------
135 |    LOCAL FUNCTION PROTOTYPES:                                         |
136 -----------------------------------------------------------------------*/
137 /* STATIC INTERNAL FUNCTION */
138 /**
139  * These are video data probing functions.
140  * If there are pads which set probe callback by gst_pad_add_probe(),
141  * the functions will be called when data stream pass through the pad.
142  *
143  * @param[in]  pad             probing pad which calls this function.
144  * @param[in]  buffer          buffer which contains stream data.
145  * @param[in]  u_data          user data.
146  * @return     Refer #GstPadProbeReturn in gstpad.h
147  * @remarks
148  * @see        __mmcamcorder_create_preview_pipeline()
149  */
150 static GstPadProbeReturn __mmcamcorder_video_dataprobe_preview(GstPad *pad, GstPadProbeInfo *info, gpointer u_data);
151 static GstPadProbeReturn __mmcamcorder_video_dataprobe_record(GstPad *pad, GstPadProbeInfo *info, gpointer u_data);
152
153 static int __mmcamcorder_get_amrnb_bitrate_mode(int bitrate);
154 static guint32 _mmcamcorder_convert_fourcc_string_to_value(const gchar* format_name);
155 #ifdef _MMCAMCORDER_PRODUCT_TV
156 static bool __mmcamcorder_find_max_resolution(MMHandleType handle, gint *max_width, gint *max_height);
157 #endif /* _MMCAMCORDER_PRODUCT_TV */
158
159 static gboolean __mmcamcorder_set_stream_data(MMCamcorderVideoStreamDataType *stream, GstBuffer *buffer, GstMapInfo *map_info);
160 static gboolean __mmcamcorder_set_stream_data_zero_copy(MMCamcorderVideoStreamDataType *stream, GstBuffer *buffer, GstMemory *memory);
161
162 /*=======================================================================================
163 |  FUNCTION DEFINITIONS                                                                 |
164 =======================================================================================*/
165 static gboolean __mmcamcorder_set_stream_data(MMCamcorderVideoStreamDataType *stream, GstBuffer *buffer, GstMapInfo *map_info)
166 {
167         gboolean ret = TRUE;
168
169         mmf_return_val_if_fail(buffer, FALSE);
170         mmf_return_val_if_fail(stream, FALSE);
171         mmf_return_val_if_fail(map_info, FALSE);
172
173         stream->length_total = map_info->size;
174
175         switch (stream->format) {
176         case MM_PIXEL_FORMAT_NV12: /* fall through */
177         case MM_PIXEL_FORMAT_NV21:
178                 stream->data_type = MM_CAM_STREAM_DATA_YUV420SP;
179                 stream->data.yuv420sp.y = map_info->data;
180                 stream->data.yuv420sp.length_y = stream->width * stream->height;
181                 stream->data.yuv420sp.uv = stream->data.yuv420sp.y + stream->data.yuv420sp.length_y;
182                 stream->data.yuv420sp.length_uv = stream->data.yuv420sp.length_y >> 1;
183                 stream->stride[0] = stream->width;
184                 stream->elevation[0] = stream->height;
185                 stream->stride[1] = stream->width;
186                 stream->elevation[1] = stream->height >> 1;
187                 stream->num_planes = 2;
188                 break;
189
190         case MM_PIXEL_FORMAT_I420:
191                 stream->data_type = MM_CAM_STREAM_DATA_YUV420P;
192                 stream->data.yuv420p.y = map_info->data;
193                 stream->data.yuv420p.length_y = stream->width * stream->height;
194                 stream->data.yuv420p.u = stream->data.yuv420p.y + stream->data.yuv420p.length_y;
195                 stream->data.yuv420p.length_u = stream->data.yuv420p.length_y >> 2;
196                 stream->data.yuv420p.v = stream->data.yuv420p.u + stream->data.yuv420p.length_u;
197                 stream->data.yuv420p.length_v = stream->data.yuv420p.length_u;
198                 stream->stride[0] = stream->width;
199                 stream->elevation[0] = stream->height;
200                 stream->stride[1] = stream->stride[2] = stream->width >> 1;
201                 stream->elevation[1] = stream->elevation[2] = stream->height >> 1;
202                 stream->num_planes = 3;
203                 break;
204
205         case MM_PIXEL_FORMAT_422P:
206                 stream->data_type = MM_CAM_STREAM_DATA_YUV422P;
207                 stream->data.yuv422p.y = map_info->data;
208                 stream->data.yuv422p.length_y = stream->width * stream->height;
209                 stream->data.yuv422p.u = stream->data.yuv422p.y + stream->data.yuv422p.length_y;
210                 stream->data.yuv422p.length_u = stream->data.yuv422p.length_y >> 1;
211                 stream->data.yuv422p.v = stream->data.yuv422p.u + stream->data.yuv422p.length_u;
212                 stream->data.yuv422p.length_v = stream->data.yuv422p.length_u;
213                 stream->stride[0] = stream->width;
214                 stream->elevation[0] = stream->height;
215                 stream->stride[1] = stream->stride[2] = stream->width;
216                 stream->elevation[1] = stream->elevation[2] = stream->height >> 1;
217                 stream->num_planes = 3;
218                 break;
219
220         case MM_PIXEL_FORMAT_YUYV: /* fall through */
221         case MM_PIXEL_FORMAT_UYVY: /* fall through */
222         case MM_PIXEL_FORMAT_ITLV_JPEG_UYVY:
223                 stream->data_type = MM_CAM_STREAM_DATA_YUV422;
224                 stream->data.yuv422.yuv = map_info->data;
225                 stream->data.yuv422.length_yuv = stream->length_total;
226                 stream->stride[0] = stream->width << 1;
227                 stream->elevation[0] = stream->height;
228                 stream->num_planes = 1;
229                 break;
230
231         case MM_PIXEL_FORMAT_ENCODED_H264:  /* fall through */
232         case MM_PIXEL_FORMAT_ENCODED_MJPEG: /* fall through */
233         case MM_PIXEL_FORMAT_ENCODED_VP8:   /* fall through */
234         case MM_PIXEL_FORMAT_ENCODED_VP9:   /* fall through */
235                 stream->data_type = MM_CAM_STREAM_DATA_ENCODED;
236                 stream->data.encoded.data = map_info->data;
237                 stream->data.encoded.length_data = stream->length_total;
238                 stream->data.encoded.is_delta_frame = GST_BUFFER_FLAG_IS_SET(buffer, GST_BUFFER_FLAG_DELTA_UNIT);
239                 stream->num_planes = 1;
240                 break;
241
242         case MM_PIXEL_FORMAT_INVZ:
243                 stream->data_type = MM_CAM_STREAM_DATA_DEPTH;
244                 stream->data.depth.data = map_info->data;
245                 stream->data.depth.length_data = stream->length_total;
246                 stream->stride[0] = stream->width << 1;
247                 stream->elevation[0] = stream->height;
248                 stream->num_planes = 1;
249                 break;
250
251         case MM_PIXEL_FORMAT_RGBA: /* fall through */
252         case MM_PIXEL_FORMAT_ARGB:
253                 stream->data_type = MM_CAM_STREAM_DATA_RGB;
254                 stream->data.rgb.data = map_info->data;
255                 stream->data.rgb.length_data = stream->length_total;
256                 stream->stride[0] = stream->width << 2;
257                 stream->elevation[0] = stream->height;
258                 stream->num_planes = 1;
259                 break;
260
261         default:
262                 MMCAM_LOG_ERROR("unsupported format[%d]", stream->format);
263                 ret = FALSE;
264                 break;
265         }
266
267         return ret;
268 }
269
270
271 static gboolean __mmcamcorder_set_stream_data_zero_copy(MMCamcorderVideoStreamDataType *stream, GstBuffer *buffer, GstMemory *memory)
272 {
273         int i = 0;
274         int num_bos = 0;
275         int tbm_ret = TBM_SURFACE_ERROR_NONE;
276         tbm_bo_handle bo_handle = {NULL, };
277         tbm_surface_info_s ts_info;
278
279         mmf_return_val_if_fail(stream, FALSE);
280         mmf_return_val_if_fail(buffer, FALSE);
281         mmf_return_val_if_fail(memory, FALSE);
282
283         if (_mmcamcorder_is_encoded_preview_pixel_format(stream->format)) {
284                 stream->bo[0] = gst_tizen_memory_get_bos(memory, 0);
285
286                 bo_handle = tbm_bo_get_handle(stream->bo[0], TBM_DEVICE_CPU);
287                 if (!bo_handle.ptr) {
288                         MMCAM_LOG_ERROR("tbm_bo_get_handle failed[bo:%p,memory:%p]", stream->bo[0], memory);
289                         return FALSE;
290                 }
291
292                 stream->data_type = MM_CAM_STREAM_DATA_ENCODED;
293                 stream->num_planes = 1;
294                 stream->stride[0] = stream->width;
295                 stream->elevation[0] = stream->height;
296                 stream->data.encoded.data = bo_handle.ptr;
297                 stream->length_total = gst_memory_get_sizes(memory, NULL, NULL);
298                 stream->data.encoded.length_data = stream->length_total;
299                 stream->data.encoded.is_delta_frame = GST_BUFFER_FLAG_IS_SET(buffer, GST_BUFFER_FLAG_DELTA_UNIT);
300
301                 MMCAM_LOG_VERBOSE("[ENCODED] length[%u], is_delta[%d]",
302                         stream->data.encoded.length_data, stream->data.encoded.is_delta_frame);
303
304                 return TRUE;
305         }
306
307         memset(&ts_info, 0x0, sizeof(tbm_surface_info_s));
308
309         tbm_ret = tbm_surface_get_info((tbm_surface_h)gst_tizen_memory_get_surface(memory), &ts_info);
310         if (tbm_ret != TBM_SURFACE_ERROR_NONE) {
311                 MMCAM_LOG_ERROR("get tbm surface info failed[0x%x]", tbm_ret);
312                 return FALSE;
313         }
314
315         stream->length_total = ts_info.size;
316         for (i = 0 ; i < ts_info.num_planes ; i++) {
317                 stream->stride[i] = ts_info.planes[i].stride;
318                 stream->elevation[i] = ts_info.planes[i].size / ts_info.planes[i].stride;
319                 MMCAM_LOG_VERBOSE("    plane[%d] %dx%d", i, stream->stride[i], stream->elevation[i]);
320         }
321
322         num_bos = gst_tizen_memory_get_num_bos(memory);
323         for (i = 0 ; i < num_bos ; i++)
324                 stream->bo[i] = gst_tizen_memory_get_bos(memory, i);
325
326
327         switch (stream->format) {
328         case MM_PIXEL_FORMAT_NV12: /* fall through */
329         case MM_PIXEL_FORMAT_NV21:
330                 stream->data_type = MM_CAM_STREAM_DATA_YUV420SP;
331                 stream->num_planes = 2;
332                 stream->data.yuv420sp.y = ts_info.planes[0].ptr;
333                 stream->data.yuv420sp.length_y = ts_info.planes[0].size;
334                 stream->data.yuv420sp.uv = ts_info.planes[1].ptr;
335                 stream->data.yuv420sp.length_uv = ts_info.planes[1].size;
336
337                 MMCAM_LOG_VERBOSE("[420SP] 0[%p,%u], 1[%p,%u]",
338                         stream->data.yuv420sp.y, stream->data.yuv420sp.length_y,
339                         stream->data.yuv420sp.uv, stream->data.yuv420sp.length_uv);
340                 break;
341
342         case MM_PIXEL_FORMAT_I420:
343                 stream->data_type = MM_CAM_STREAM_DATA_YUV420P;
344                 stream->num_planes = 3;
345                 stream->data.yuv420p.y = ts_info.planes[0].ptr;
346                 stream->data.yuv420p.length_y = ts_info.planes[0].size;
347                 stream->data.yuv420p.u = ts_info.planes[1].ptr;
348                 stream->data.yuv420p.length_u = ts_info.planes[1].size;
349                 stream->data.yuv420p.v = ts_info.planes[2].ptr;
350                 stream->data.yuv420p.length_v = ts_info.planes[2].size;
351
352                 MMCAM_LOG_VERBOSE("[I420] 0[%p,%u], 1[%p,%u], 2[%p,%u]",
353                         stream->data.yuv420p.y, stream->data.yuv420p.length_y,
354                         stream->data.yuv420p.u, stream->data.yuv420p.length_u,
355                         stream->data.yuv420p.v, stream->data.yuv420p.length_v);
356                 break;
357
358         case MM_PIXEL_FORMAT_YUYV:
359                 stream->data_type = MM_CAM_STREAM_DATA_YUV422;
360                 stream->num_planes = 1;
361                 stream->data.yuv422.yuv = ts_info.planes[0].ptr;
362                 stream->data.yuv422.length_yuv = ts_info.planes[0].size;
363
364                 MMCAM_LOG_VERBOSE("[YUYV] 0[%p,%u]",
365                         stream->data.yuv422.yuv, stream->data.yuv422.length_yuv);
366                 break;
367
368         default:
369                 MMCAM_LOG_ERROR("unsupported format[%d]", stream->format);
370                 return FALSE;
371         }
372
373         return TRUE;
374 }
375
376
377 gboolean _mmcamcorder_invoke_video_stream_cb(MMHandleType handle, GstSample *sample, gboolean is_preview, int stream_id)
378 {
379         int i = 0;
380         gboolean ret_cb = TRUE;
381         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
382         _MMCamcorderSubContext *sc = NULL;
383         MMCamcorderVideoStreamDataType stream;
384
385         GstBuffer *buffer = NULL;
386         GstMemory *memory = NULL;
387         GstMapInfo map_info;
388         GstCaps *caps = NULL;
389         GstStructure *structure = NULL;
390
391         mmf_return_val_if_fail(hcamcorder, FALSE);
392
393         buffer = gst_sample_get_buffer(sample);
394         mmf_return_val_if_fail(buffer, FALSE);
395         mmf_return_val_if_fail(gst_buffer_n_memory(buffer), FALSE);
396
397         sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
398         mmf_return_val_if_fail(sc, FALSE);
399
400         memset(&stream, 0x0, sizeof(MMCamcorderVideoStreamDataType));
401
402         caps = gst_sample_get_caps(sample);
403         mmf_return_val_if_fail(caps, FALSE);
404
405         structure = gst_caps_get_structure(caps, 0);
406         mmf_return_val_if_fail(structure, FALSE);
407
408         stream.format = _mmcamcorder_get_pixel_format(caps, TRUE);
409         gst_structure_get(structure,
410                 "width", G_TYPE_INT, &stream.width,
411                 "height", G_TYPE_INT, &stream.height,
412                 NULL);
413
414         memory = gst_buffer_peek_memory(buffer, 0);
415         if (!memory) {
416                 MMCAM_LOG_ERROR("GstMemory get failed from buffer %p", buffer);
417                 return FALSE;
418         }
419
420         structure = gst_mini_object_get_qdata(GST_MINI_OBJECT_CAST(buffer), hcamcorder->buffer_quark);
421         if (structure) {
422                 gst_structure_get(structure,
423                         "focus-state", G_TYPE_INT, &stream.focus_state,
424                         "facing-direction", G_TYPE_INT, &stream.facing_direction,
425                         "flip", G_TYPE_INT, &stream.flip,
426                         "rotation", G_TYPE_INT, &stream.rotation,
427                         NULL);
428
429                 MMCAM_LOG_VERBOSE("structure[%p], [fs:%d,fd:%d,f:%d,r:%d]", structure,
430                         stream.focus_state, stream.facing_direction, stream.flip, stream.rotation);
431         }
432
433         if (gst_is_tizen_memory(memory)) {
434                 if (!__mmcamcorder_set_stream_data_zero_copy(&stream, buffer, memory))
435                         goto _INVOKE_VIDEO_STREAM_CB_DONE;
436         } else {
437                 memset(&map_info, 0x0, sizeof(GstMapInfo));
438
439                 if (!gst_memory_map(memory, &map_info, GST_MAP_READWRITE)) {
440                         MMCAM_LOG_ERROR("map failed for memory[%p]", memory);
441                         return FALSE;
442                 }
443
444                 if (!__mmcamcorder_set_stream_data(&stream, buffer, &map_info))
445                         goto _INVOKE_VIDEO_STREAM_CB_DONE;
446         }
447
448         MMCAM_LOG_DEBUG("VideoStreamData : format[%d], resolution[%dx%d], stream_id[%d]",
449                 stream.format, stream.width, stream.height, stream_id);
450
451         stream.timestamp = (unsigned int)(GST_BUFFER_PTS(buffer) / 1000000); /* nano sec -> milli sec */
452         stream.extra_stream_id = stream_id;
453         stream.internal_buffer = buffer;
454
455         /* invoke application callback */
456         if (is_preview) {
457                 _MMCAMCORDER_LOCK_VSTREAM_CALLBACK(hcamcorder);
458                 if (hcamcorder->vstream_cb)
459                         hcamcorder->vstream_cb(&stream, hcamcorder->vstream_cb_param);
460                 _MMCAMCORDER_UNLOCK_VSTREAM_CALLBACK(hcamcorder);
461         } else {
462                 _MMCAMCORDER_LOCK_VEDECISION_CALLBACK(hcamcorder);
463                 if (hcamcorder->vedecision_cb)
464                         ret_cb = hcamcorder->vedecision_cb(&stream, hcamcorder->vedecision_cb_param);
465                 _MMCAMCORDER_UNLOCK_VEDECISION_CALLBACK(hcamcorder);
466         }
467
468 _INVOKE_VIDEO_STREAM_CB_DONE:
469         if (!gst_is_tizen_memory(memory))
470                 gst_memory_unmap(memory, &map_info);
471
472         for (i = 0 ; i < TBM_SURF_PLANE_MAX && stream.bo[i] ; i++) {
473                 tbm_bo_map(stream.bo[i], TBM_DEVICE_CPU, TBM_OPTION_READ|TBM_OPTION_WRITE);
474                 tbm_bo_unmap(stream.bo[i]);
475         }
476
477         return ret_cb;
478 }
479
480
481 int _mmcamcorder_create_preview_elements(MMHandleType handle)
482 {
483         int err = MM_ERROR_NONE;
484         int i = 0;
485         int camera_rotate = 0;
486         int camera_flip = 0;
487         int fps = 0;
488         int codectype = 0;
489         int capture_width = 0;
490         int capture_height = 0;
491         int capture_quality = 0;
492         int video_stabilization = 0;
493         int anti_shake = 0;
494         int display_surface_type = MM_DISPLAY_SURFACE_NULL;
495         const char *videosrc_name = NULL;
496         const char *videosink_name = NULL;
497         const char *videoconvert_name = NULL;
498         const char *videodecoder_name = NULL;
499         const char *decoder_conf_name = NULL;
500         char videodecoder_name_final[32] = {'\0',};
501         char *err_name = NULL;
502 #ifdef _MMCAMCORDER_RM_SUPPORT
503         int decoder_index = 0;
504 #endif /* _MMCAMCORDER_RM_SUPPORT */
505         GstElement *sink_element = NULL;
506         GstCameraControl *control = NULL;
507         int sink_element_size = 0;
508         int *fds = NULL;
509         int fd_number = 0;
510         int extra_preview_enable = 0;
511
512         GList *element_list = NULL;
513
514         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
515         _MMCamcorderSubContext *sc = NULL;
516         type_element *VideosrcElement = NULL;
517         type_int_array *input_index = NULL;
518
519         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
520
521         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
522         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
523         mmf_return_val_if_fail(sc->element, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
524
525         MMCAM_LOG_INFO("");
526
527         /* Check existence */
528         for (i = _MMCAMCORDER_VIDEOSRC_SRC ; i <= _MMCAMCORDER_VIDEOSINK_SINK ; i++) {
529                 if (sc->element[i].gst) {
530                         if (((GObject *)sc->element[i].gst)->ref_count > 0)
531                                 gst_object_unref(sc->element[i].gst);
532
533                         MMCAM_LOG_INFO("element[index:%d] is Already existed.", i);
534                 }
535         }
536
537         /* Get video device index info */
538         _mmcamcorder_conf_get_value_int_array(hcamcorder->conf_ctrl,
539                                                                                   CONFIGURE_CATEGORY_CTRL_CAMERA,
540                                                                                   "InputIndex",
541                                                                                   &input_index);
542         if (input_index == NULL) {
543                 MMCAM_LOG_ERROR("Failed to get input_index");
544                 return MM_ERROR_CAMCORDER_CREATE_CONFIGURE;
545         }
546
547         err = mm_camcorder_get_attributes(handle, &err_name,
548                 MMCAM_CAMERA_FORMAT, &sc->info_image->preview_format,
549                 MMCAM_CAMERA_FPS, &fps,
550                 MMCAM_CAMERA_ROTATION, &camera_rotate,
551                 MMCAM_CAMERA_FLIP, &camera_flip,
552                 MMCAM_CAMERA_VIDEO_STABILIZATION, &video_stabilization,
553                 MMCAM_CAMERA_ANTI_HANDSHAKE, &anti_shake,
554                 MMCAM_CAPTURE_WIDTH, &capture_width,
555                 MMCAM_CAPTURE_HEIGHT, &capture_height,
556                 MMCAM_CAMERA_HDR_CAPTURE, &sc->info_image->hdr_capture_mode,
557                 MMCAM_IMAGE_ENCODER, &codectype,
558                 MMCAM_IMAGE_ENCODER_QUALITY, &capture_quality,
559                 MMCAM_DISPLAY_SURFACE, &display_surface_type,
560                 MMCAM_EXTRA_PREVIEW_ENABLE, &extra_preview_enable,
561                 NULL);
562         if (err != MM_ERROR_NONE) {
563                 MMCAM_LOG_WARNING("Get attrs fail. (%s:%x)", err_name, err);
564                 SAFE_FREE(err_name);
565                 return err;
566         }
567
568         if (hcamcorder->support_user_buffer) {
569                 err = mm_camcorder_get_attributes(handle, NULL,
570                         MMCAM_USER_BUFFER_FD, &fds, &fd_number,
571                         NULL);
572                 if (err != MM_ERROR_NONE || fd_number < 1) {
573                         MMCAM_LOG_ERROR("get user buffer fd failed 0x%x, number %d", err, fd_number);
574                         return err;
575                 }
576
577                 /*
578                 for (i = 0 ; i < fd_number ; i++)
579                         MMCAM_LOG_INFO("fds[%d] %d", i, fds[i]);
580                 */
581         }
582
583         /* Get fourcc from picture format */
584         sc->fourcc = _mmcamcorder_get_fourcc(sc->info_image->preview_format, codectype, hcamcorder->use_zero_copy_format);
585
586         /* Get videosrc element and its name from configure */
587         _mmcamcorder_conf_get_element(handle, hcamcorder->conf_main,
588                 CONFIGURE_CATEGORY_MAIN_VIDEO_INPUT,
589                 "VideosrcElement",
590                 &VideosrcElement);
591         _mmcamcorder_conf_get_value_element_name(VideosrcElement, &videosrc_name);
592
593         /**
594          * Create child element
595          */
596         _MMCAMCORDER_ELEMENT_MAKE(sc, sc->element, _MMCAMCORDER_VIDEOSRC_SRC, videosrc_name, "videosrc_src", element_list, err);
597
598         _MMCAMCORDER_ELEMENT_MAKE(sc, sc->element, _MMCAMCORDER_VIDEOSRC_FILT, "capsfilter", "videosrc_filter", element_list, err);
599
600         /**
601          * This is for "tizencamerasrc" element only.
602          * The camera HAL library will be loaded when "hal-name" property is set.
603          * The default HAL library (libtizen-camera.so) will be loaded if 'hal-name' property is set to NULL.
604          */
605         MMCAMCORDER_G_OBJECT_SET_POINTER(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "hal-name", hcamcorder->network_hal_name);
606
607         /* camera properties */
608         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "high-speed-fps", 0);
609         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "capture-width", capture_width);
610         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "capture-height", capture_height);
611         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "capture-quality", capture_quality);
612         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "hdr-capture", sc->info_image->hdr_capture_mode);
613         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "extra-preview", extra_preview_enable);
614         MMCAMCORDER_G_OBJECT_GET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "buffer-quark", &hcamcorder->buffer_quark);
615
616         _MMCAMCORDER_ELEMENT_MAKE(sc, sc->element, _MMCAMCORDER_VIDEOSRC_QUE, "queue", "videosrc_queue", element_list, err);
617
618         /* set camera flip */
619         _mmcamcorder_set_videosrc_flip(handle, camera_flip);
620
621         /* set video stabilization mode */
622         _mmcamcorder_set_videosrc_stabilization(handle, video_stabilization);
623
624         /* set anti handshake mode */
625         _mmcamcorder_set_videosrc_anti_shake(handle, anti_shake);
626
627         if (sc->is_modified_rate)
628                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "high-speed-fps", fps);
629
630         /* Set basic information of videosrc element */
631         _mmcamcorder_conf_set_value_element_property(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, VideosrcElement);
632
633         /* Set video device index */
634         if (hcamcorder->is_network) {
635                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "camera-id", hcamcorder->device_type);
636         } else {
637 #ifdef _MMCAMCORDER_CAMERA_CONF_MGR_SUPPORT
638                 MMCAMCORDER_G_OBJECT_SET_POINTER(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "device-name", hcamcorder->conf_device_info.node);
639 #else
640                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "camera-id", input_index->default_value);
641 #endif
642         }
643
644         /* set user buffer fd to videosrc element */
645         if (hcamcorder->support_user_buffer) {
646                 control = GST_CAMERA_CONTROL(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst);
647                 if (!gst_camera_control_set_user_buffer_fd(control, fds, fd_number)) {
648                         MMCAM_LOG_ERROR("set user buffer fd failed");
649                         goto pipeline_creation_error;
650                 }
651         }
652
653         if (_mmcamcorder_is_encoded_preview_pixel_format(sc->info_image->preview_format)) {
654                 /* get recreate_decoder flag */
655                 _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_main,
656                         CONFIGURE_CATEGORY_MAIN_VIDEO_OUTPUT,
657                         "RecreateDecoder",
658                         &hcamcorder->recreate_decoder);
659         }
660
661         if (display_surface_type != MM_DISPLAY_SURFACE_NULL &&
662                 _mmcamcorder_is_encoded_preview_pixel_format(sc->info_image->preview_format)) {
663                 switch (sc->info_image->preview_format) {
664                 case MM_PIXEL_FORMAT_ENCODED_H264:
665                         decoder_conf_name = "VideodecoderElementH264";
666                         break;
667                 case MM_PIXEL_FORMAT_ENCODED_MJPEG:
668                         decoder_conf_name = "VideodecoderElementMJPEG";
669                         break;
670                 case MM_PIXEL_FORMAT_ENCODED_VP8:
671                         decoder_conf_name = "VideodecoderElementVP8";
672                         break;
673                 case MM_PIXEL_FORMAT_ENCODED_VP9:
674                         decoder_conf_name = "VideodecoderElementVP9";
675                         break;
676                 default:
677                         MMCAM_LOG_ERROR("invalid format[%d]", sc->info_image->preview_format);
678                         goto pipeline_creation_error;
679                 }
680
681                 /* get video decoder element and name */
682                 _mmcamcorder_conf_get_element(handle, hcamcorder->conf_main,
683                         CONFIGURE_CATEGORY_MAIN_VIDEO_OUTPUT,
684                         decoder_conf_name,
685                         &sc->VideodecoderElement);
686
687                 _mmcamcorder_conf_get_value_element_name(sc->VideodecoderElement,
688                         &videodecoder_name);
689
690                 if (!videodecoder_name) {
691                         MMCAM_LOG_ERROR("failed to get video decoder[%d,%s]", sc->info_image->preview_format, decoder_conf_name);
692                         goto pipeline_creation_error;
693                 }
694
695 #ifdef _MMCAMCORDER_RM_SUPPORT
696                 MMCAM_LOG_INFO("decoder name from config[%s]", videodecoder_name);
697                 if (sc->info_image->preview_format == MM_PIXEL_FORMAT_ENCODED_H264) {
698                         if (hcamcorder->request_resources.category_id[0] == RM_CATEGORY_VIDEO_DECODER_SUB)
699                                 decoder_index = 1;
700                         snprintf(videodecoder_name_final, sizeof(videodecoder_name_final), "%s%d", videodecoder_name, decoder_index);
701                 } else if (sc->info_image->preview_format == MM_PIXEL_FORMAT_ENCODED_MJPEG) {
702                         if (hcamcorder->request_resources.category_id[0] == RM_CATEGORY_VIDEO_DECODER)
703                                 snprintf(videodecoder_name_final, sizeof(videodecoder_name_final), "%s", "omx_uhd_mjpegdec");
704                         else
705                                 snprintf(videodecoder_name_final, sizeof(videodecoder_name_final), "%s", "omx_mjpegdec");
706                 } else {
707 #endif /* _MMCAMCORDER_RM_SUPPORT */
708                         snprintf(videodecoder_name_final, sizeof(videodecoder_name_final), "%s", videodecoder_name);
709 #ifdef _MMCAMCORDER_RM_SUPPORT
710                 }
711 #endif /* _MMCAMCORDER_RM_SUPPORT */
712
713                 _MMCAMCORDER_ELEMENT_MAKE(sc, sc->element, _MMCAMCORDER_VIDEOSRC_DECODE, videodecoder_name_final, "videosrc_decode", element_list, err);
714
715                 _mmcamcorder_conf_set_value_element_property(sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst, sc->VideodecoderElement);
716         }
717
718         MMCAM_LOG_INFO("Current mode[%d]", hcamcorder->type);
719
720         /* Get videosink name */
721         _mmcamcorder_conf_get_value_element_name(sc->VideosinkElement, &videosink_name);
722
723         if (!videosink_name) {
724                 MMCAM_LOG_ERROR("failed to get videosink name");
725                 goto pipeline_creation_error;
726         }
727
728         _MMCAMCORDER_ELEMENT_MAKE(sc, sc->element, _MMCAMCORDER_VIDEOSINK_QUE, "queue", "videosink_queue", element_list, err);
729
730         MMCAM_LOG_INFO("videosink_name: %s", videosink_name);
731
732         if (hcamcorder->use_videoconvert && (!strcmp(videosink_name, "tizenwlsink") || !strcmp(videosink_name, "directvideosink"))) {
733                 /* get video convert name */
734                 _mmcamcorder_conf_get_value_element_name(sc->VideoconvertElement, &videoconvert_name);
735
736                 if (videoconvert_name) {
737                         MMCAM_LOG_INFO("videoconvert element name : %s", videoconvert_name);
738                         _MMCAMCORDER_ELEMENT_MAKE(sc, sc->element, _MMCAMCORDER_VIDEOSINK_CLS, videoconvert_name, "videosink_cls", element_list, err);
739                 } else
740                         MMCAM_LOG_ERROR("failed to get videoconvert element name");
741         }
742
743         /* check sink element in attribute */
744         mm_camcorder_get_attributes(handle, NULL,
745                 MMCAM_DISPLAY_REUSE_ELEMENT, &sink_element, &sink_element_size,
746                 NULL);
747
748         if (sink_element) {
749                 int attr_index = 0;
750                 MMHandleType attrs = MMF_CAMCORDER_ATTRS(handle);
751
752                 MMCAM_LOG_INFO("reuse sink element %p in attribute", sink_element);
753
754                 _MMCAMCORDER_ELEMENT_ADD(sc, sc->element, _MMCAMCORDER_VIDEOSINK_SINK, sink_element, element_list, err);
755
756                 /* reset attribute */
757                 if (attrs) {
758                         mm_attrs_get_index((MMHandleType)attrs, MMCAM_DISPLAY_REUSE_ELEMENT, &attr_index);
759                         mm_attrs_set_data(attrs, attr_index, NULL, 0);
760                         mm_attrs_commit(attrs, attr_index);
761                 } else {
762                         MMCAM_LOG_WARNING("attribute is NULL");
763                         err = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
764                         goto pipeline_creation_error;
765                 }
766         } else {
767                 _MMCAMCORDER_ELEMENT_MAKE(sc, sc->element, _MMCAMCORDER_VIDEOSINK_SINK, videosink_name, "videosink_sink", element_list, err);
768
769                 _mmcamcorder_conf_set_value_element_property(sc->element[_MMCAMCORDER_VIDEOSINK_SINK].gst, sc->VideosinkElement);
770         }
771
772         if (_mmcamcorder_videosink_window_set(handle, sc->VideosinkElement) != MM_ERROR_NONE) {
773                 MMCAM_LOG_ERROR("_mmcamcorder_videosink_window_set error");
774                 err = MM_ERROR_CAMCORDER_INVALID_ARGUMENT;
775                 goto pipeline_creation_error;
776         }
777
778         /* Set caps by rotation */
779         _mmcamcorder_set_videosrc_rotation(handle, camera_rotate);
780
781         /* add elements to main pipeline */
782         if (!_mmcamcorder_add_elements_to_bin(GST_BIN(sc->element[_MMCAMCORDER_MAIN_PIPE].gst), element_list)) {
783                 MMCAM_LOG_ERROR("element_list add error.");
784                 err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
785                 goto pipeline_creation_error;
786         }
787
788         /* link elements */
789         if (!_mmcamcorder_link_elements(element_list)) {
790                 MMCAM_LOG_ERROR("element link error.");
791                 err = MM_ERROR_CAMCORDER_GST_LINK;
792                 goto pipeline_creation_error;
793         }
794
795         if (element_list) {
796                 g_list_free(element_list);
797                 element_list = NULL;
798         }
799
800         return MM_ERROR_NONE;
801
802 pipeline_creation_error:
803         _MMCAMCORDER_ELEMENT_REMOVE(sc->element, _MMCAMCORDER_VIDEOSRC_SRC);
804         _MMCAMCORDER_ELEMENT_REMOVE(sc->element, _MMCAMCORDER_VIDEOSRC_FILT);
805         _MMCAMCORDER_ELEMENT_REMOVE(sc->element, _MMCAMCORDER_VIDEOSRC_QUE);
806         _MMCAMCORDER_ELEMENT_REMOVE(sc->element, _MMCAMCORDER_VIDEOSRC_CAP_FILT);
807         _MMCAMCORDER_ELEMENT_REMOVE(sc->element, _MMCAMCORDER_VIDEOSRC_CAP_SINK);
808         _MMCAMCORDER_ELEMENT_REMOVE(sc->element, _MMCAMCORDER_VIDEOSRC_DECODE);
809         _MMCAMCORDER_ELEMENT_REMOVE(sc->element, _MMCAMCORDER_VIDEOSINK_QUE);
810         _MMCAMCORDER_ELEMENT_REMOVE(sc->element, _MMCAMCORDER_VIDEOSINK_SINK);
811
812         if (element_list) {
813                 g_list_free(element_list);
814                 element_list = NULL;
815         }
816
817         return err;
818 }
819
820
821 int _mmcamcorder_create_audiosrc_bin(MMHandleType handle)
822 {
823         int err = MM_ERROR_NONE;
824         int val = 0;
825         int rate = 0;
826         int format = 0;
827         int channel = 0;
828         int a_enc = MM_AUDIO_CODEC_AMR;
829         int a_dev = MM_AUDIO_DEVICE_MIC;
830         double volume = 0.0;
831         char *err_name = NULL;
832         const char *audiosrc_name = NULL;
833         char *cat_name = NULL;
834         char *stream_type = NULL;
835         int stream_type_len = 0;
836         int stream_index = 0;
837         int buffer_interval = 0;
838         int blocksize = 0;
839         int replay_gain_enable = FALSE;
840         double replay_gain_ref_level = 0.0;
841
842         GstCaps *caps = NULL;
843         GstPad *pad = NULL;
844         GList *element_list  = NULL;
845
846         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
847         _MMCamcorderSubContext *sc = NULL;
848         _MMCamcorderGstElement *last_element = NULL;
849         type_element *AudiosrcElement = NULL;
850
851         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
852
853         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
854         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
855         mmf_return_val_if_fail(sc->element, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
856
857         MMCAM_LOG_INFO("");
858
859         err = _mmcamcorder_check_audiocodec_fileformat_compatibility(handle);
860         if (err != MM_ERROR_NONE)
861                 return err;
862
863         err = mm_camcorder_get_attributes(handle, &err_name,
864                 MMCAM_AUDIO_DEVICE, &a_dev,
865                 MMCAM_AUDIO_ENCODER, &a_enc,
866                 MMCAM_AUDIO_ENCODER_BITRATE, &val,
867                 MMCAM_AUDIO_SAMPLERATE, &rate,
868                 MMCAM_AUDIO_FORMAT, &format,
869                 MMCAM_AUDIO_CHANNEL, &channel,
870                 MMCAM_AUDIO_VOLUME, &volume,
871                 MMCAM_AUDIO_REPLAY_GAIN_ENABLE, &replay_gain_enable,
872                 MMCAM_AUDIO_REPLAY_GAIN_REFERENCE_LEVEL, &replay_gain_ref_level,
873                 MMCAM_SOUND_STREAM_TYPE, &stream_type, &stream_type_len,
874                 MMCAM_SOUND_STREAM_INDEX, &stream_index,
875                 NULL);
876         if (err != MM_ERROR_NONE) {
877                 MMCAM_LOG_WARNING("Get attrs fail. (%s:%x)", err_name, err);
878                 SAFE_FREE(err_name);
879                 return err;
880         }
881
882         /* Check existence */
883         if (sc->encode_element[_MMCAMCORDER_AUDIOSRC_BIN].gst) {
884                 if (((GObject *)sc->encode_element[_MMCAMCORDER_AUDIOSRC_BIN].gst)->ref_count > 0)
885                         gst_object_unref(sc->encode_element[_MMCAMCORDER_AUDIOSRC_BIN].gst);
886
887                 MMCAM_LOG_INFO("_MMCAMCORDER_AUDIOSRC_BIN is Already existed. Unref once...");
888         }
889
890         /* Create bin element */
891         _MMCAMCORDER_BIN_MAKE(sc, sc->encode_element, _MMCAMCORDER_AUDIOSRC_BIN, "audiosource_bin", err);
892
893         if (a_dev == MM_AUDIO_DEVICE_MODEM) {
894                 cat_name = strdup("AudiomodemsrcElement");
895         } else {
896                 /* MM_AUDIO_DEVICE_MIC or others */
897                 cat_name = strdup("AudiosrcElement");
898         }
899
900         if (cat_name == NULL) {
901                 MMCAM_LOG_ERROR("strdup failed.");
902                 err = MM_ERROR_CAMCORDER_LOW_MEMORY;
903                 goto pipeline_creation_error;
904         }
905
906         _mmcamcorder_conf_get_element(handle, hcamcorder->conf_main,
907                 CONFIGURE_CATEGORY_MAIN_AUDIO_INPUT,
908                 cat_name,
909                 &AudiosrcElement);
910         _mmcamcorder_conf_get_value_element_name(AudiosrcElement, &audiosrc_name);
911
912         free(cat_name);
913         cat_name = NULL;
914
915         MMCAM_LOG_INFO("Audio src name : %s", audiosrc_name);
916
917         _MMCAMCORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMCAMCORDER_AUDIOSRC_SRC, audiosrc_name, "audiosrc_src", element_list, err);
918
919         /* set sound stream info */
920         _mmcamcorder_set_sound_stream_info(sc->encode_element[_MMCAMCORDER_AUDIOSRC_SRC].gst, stream_type, stream_index);
921
922         /* set audiosrc properties in ini configuration */
923         _mmcamcorder_conf_set_value_element_property(sc->encode_element[_MMCAMCORDER_AUDIOSRC_SRC].gst, AudiosrcElement);
924
925         /* set block size */
926         _mmcamcorder_conf_get_value_int((MMHandleType)hcamcorder, hcamcorder->conf_main,
927                 CONFIGURE_CATEGORY_MAIN_AUDIO_INPUT,
928                 "AudioBufferInterval",
929                 &buffer_interval);
930
931         if (_mmcamcorder_get_audiosrc_blocksize(rate, format, channel, buffer_interval, &blocksize)) {
932                 MMCAM_LOG_INFO("set audiosrc block size %d", blocksize);
933                 MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_AUDIOSRC_SRC].gst, "blocksize", blocksize);
934         }
935
936         _MMCAMCORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMCAMCORDER_AUDIOSRC_FILT, "capsfilter", "audiosrc_capsfilter", element_list, err);
937
938         _MMCAMCORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMCAMCORDER_AUDIOSRC_QUE, "queue", "audiosrc_queue", element_list, err);
939         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_AUDIOSRC_QUE].gst, "max-size-buffers", 0);
940         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_AUDIOSRC_QUE].gst, "max-size-bytes", 0);
941         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_AUDIOSRC_QUE].gst, "max-size-time", 0);
942
943         /* Set basic information */
944         if (a_enc != MM_AUDIO_CODEC_VORBIS) {
945                 int depth = 0;
946                 const gchar* format_name = NULL;
947
948                 _MMCAMCORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMCAMCORDER_AUDIOSRC_VOL, "volume", "audiosrc_volume", element_list, err);
949
950                 if (volume == 0.0) {
951                         /* Because data probe of audio src do the same job, it doesn't need to set "mute" here. Already null raw data. */
952                         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_AUDIOSRC_VOL].gst, "volume", 1.0);
953                 } else {
954                         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_AUDIOSRC_VOL].gst, "mute", FALSE);
955                         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_AUDIOSRC_VOL].gst, "volume", volume);
956                 }
957
958                 if (format == MM_CAMCORDER_AUDIO_FORMAT_PCM_S16_LE) {
959                         depth = 16;
960                         format_name = "S16LE";
961                 } else { /* MM_CAMCORDER_AUDIO_FORMAT_PCM_U8 */
962                         depth = 8;
963                         format_name = "U8";
964                 }
965
966                 caps = gst_caps_new_simple("audio/x-raw",
967                         "rate", G_TYPE_INT, rate,
968                         "channels", G_TYPE_INT, channel,
969                         "format", G_TYPE_STRING, format_name,
970                         NULL);
971                 MMCAM_LOG_INFO("caps [x-raw, rate:%d, channel:%d, depth:%d], volume %lf",
972                         rate, channel, depth, volume);
973         } else {
974                 /* what are the audio encoder which should get audio/x-raw-float? */
975                 caps = gst_caps_new_simple("audio/x-raw",
976                         "rate", G_TYPE_INT, rate,
977                         "channels", G_TYPE_INT, channel,
978                         "format", G_TYPE_STRING, GST_AUDIO_NE(F32),
979                         NULL);
980                 MMCAM_LOG_INFO("caps [x-raw (F32), rate:%d, channel:%d, endianness:%d, width:32]",
981                         rate, channel, BYTE_ORDER);
982         }
983
984         /* Replay Gain */
985         MMCAM_LOG_INFO("Replay gain - enable : %d, reference level : %lf",
986                 replay_gain_enable, replay_gain_ref_level);
987
988         if (replay_gain_enable) {
989                 _MMCAMCORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMCAMCORDER_AUDIOSRC_RGA, "rganalysis", "audiosrc_rga", element_list, err);
990                 MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_AUDIOSRC_RGA].gst, "reference-level", replay_gain_ref_level);
991                 /* If num-tracks is not set, album gain and peak event is not come. */
992                 MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_AUDIOSRC_RGA].gst, "num-tracks", 1);
993         }
994
995         if (caps) {
996                 MMCAMCORDER_G_OBJECT_SET_POINTER((sc->encode_element[_MMCAMCORDER_AUDIOSRC_FILT].gst), "caps", caps);
997                 gst_caps_unref(caps);
998                 caps = NULL;
999         } else {
1000                 MMCAM_LOG_ERROR("create caps error");
1001                 err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1002                 goto pipeline_creation_error;
1003         }
1004
1005         if (!_mmcamcorder_add_elements_to_bin(GST_BIN(sc->encode_element[_MMCAMCORDER_AUDIOSRC_BIN].gst), element_list)) {
1006                 MMCAM_LOG_ERROR("element add error.");
1007                 err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1008                 goto pipeline_creation_error;
1009         }
1010
1011         if (!_mmcamcorder_link_elements(element_list)) {
1012                 MMCAM_LOG_ERROR("element link error.");
1013                 err = MM_ERROR_CAMCORDER_GST_LINK;
1014                 goto pipeline_creation_error;
1015         }
1016
1017         last_element = (_MMCamcorderGstElement*)(g_list_last(element_list)->data);
1018         pad = gst_element_get_static_pad(last_element->gst, "src");
1019         if (!gst_element_add_pad(sc->encode_element[_MMCAMCORDER_AUDIOSRC_BIN].gst, gst_ghost_pad_new("src", pad))) {
1020                 gst_object_unref(pad);
1021                 pad = NULL;
1022                 MMCAM_LOG_ERROR("failed to create ghost pad on _MMCAMCORDER_AUDIOSRC_BIN.");
1023                 err = MM_ERROR_CAMCORDER_GST_LINK;
1024                 goto pipeline_creation_error;
1025         }
1026
1027         gst_object_unref(pad);
1028         pad = NULL;
1029
1030         if (element_list) {
1031                 g_list_free(element_list);
1032                 element_list = NULL;
1033         }
1034
1035         return MM_ERROR_NONE;
1036
1037 pipeline_creation_error:
1038         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_AUDIOSRC_SRC);
1039         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_AUDIOSRC_FILT);
1040         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_AUDIOSRC_QUE);
1041         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_AUDIOSRC_VOL);
1042         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_AUDIOSRC_BIN);
1043
1044         if (element_list) {
1045                 g_list_free(element_list);
1046                 element_list = NULL;
1047         }
1048
1049         return err;
1050 }
1051
1052
1053 void _mmcamcorder_set_encoder_bitrate(MMCamcorderEncoderType type, int codec, int bitrate, GstElement *element)
1054 {
1055         int set_value = 0;
1056
1057         if (!element) {
1058                 MMCAM_LOG_WARNING("NULL element, will be applied later - type %d, bitrate %d", type, bitrate);
1059                 return;
1060         }
1061
1062         if (bitrate <= 0) {
1063                 MMCAM_LOG_WARNING("[type %d, codec %d] too small bitrate[%d], use default",
1064                         type, codec, bitrate);
1065                 return;
1066         }
1067
1068         if (type == MM_CAMCORDER_ENCODER_TYPE_AUDIO) {
1069                 /* audio encoder bitrate setting */
1070                 switch (codec) {
1071                 case MM_AUDIO_CODEC_AMR:
1072                         set_value = __mmcamcorder_get_amrnb_bitrate_mode(bitrate);
1073                         MMCAM_LOG_INFO("Set AMR encoder mode [%d]", set_value);
1074                         MMCAMCORDER_G_OBJECT_SET(element, "band-mode", set_value);
1075                         break;
1076                 case MM_AUDIO_CODEC_MP3:
1077                         set_value = bitrate / 1000;
1078                         MMCAM_LOG_INFO("Set MP3 encoder bitrate [%d] kbps", set_value);
1079                         MMCAMCORDER_G_OBJECT_SET(element, "bitrate", set_value);
1080                         break;
1081                 case MM_AUDIO_CODEC_AAC:
1082                         MMCAM_LOG_INFO("Set AAC encoder bitrate [%d] bps", bitrate);
1083                         MMCAMCORDER_G_OBJECT_SET(element, "bitrate", bitrate);
1084                         break;
1085                 default:
1086                         MMCAM_LOG_WARNING("Not AMR, MP3 and AAC codec, need to add code for audio bitrate");
1087                         break;
1088                 }
1089         } else {
1090                 /* video encoder bitrate setting */
1091                 MMCAM_LOG_INFO("Set video encoder bitrate %d", bitrate);
1092                 MMCAMCORDER_G_OBJECT_SET(element, "bitrate", bitrate);
1093         }
1094
1095         return;
1096 }
1097
1098
1099 int _mmcamcorder_create_encodesink_bin(MMHandleType handle, MMCamcorderEncodebinProfile profile)
1100 {
1101         int err = MM_ERROR_NONE;
1102         int channel = 0;
1103         int audio_enc = 0;
1104         int v_bitrate = 0;
1105         int a_bitrate = 0;
1106         int encodebin_profile = 0;
1107         int auto_audio_convert = 0;
1108         int auto_audio_resample = 0;
1109         int auto_color_space = 0;
1110         int cap_format = MM_PIXEL_FORMAT_INVALID;
1111         const char *gst_element_venc_name = NULL;
1112         const char *gst_element_aenc_name = NULL;
1113         const char *gst_element_ienc_name = NULL;
1114         const char *gst_element_mux_name = NULL;
1115         const char *gst_element_rsink_name = NULL;
1116         const char *gst_element_vscale_name = NULL;
1117         const char *str_profile = NULL;
1118         const char *str_aac = NULL;
1119         const char *str_aar = NULL;
1120         const char *str_acs = NULL;
1121         char *err_name = NULL;
1122         const char *videoconvert_name = NULL;
1123         GstCaps *audio_caps = NULL;
1124         GstCaps *video_caps = NULL;
1125         GstCaps *videoscale_caps = NULL;
1126         GstPad *pad = NULL;
1127         GList *element_list = NULL;
1128
1129         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1130         _MMCamcorderSubContext *sc = NULL;
1131         type_element *VideoencElement = NULL;
1132         type_element *AudioencElement = NULL;
1133         type_element *ImageencElement = NULL;
1134         type_element *MuxElement = NULL;
1135         type_element *RecordsinkElement = NULL;
1136         type_element *VideoscaleElement = NULL;
1137
1138         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1139
1140         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
1141         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1142         mmf_return_val_if_fail(sc->element, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1143
1144         MMCAM_LOG_INFO("start - profile : %d", profile);
1145
1146         /* Check existence */
1147         if (sc->encode_element[_MMCAMCORDER_ENCSINK_BIN].gst) {
1148                 if (((GObject *)sc->encode_element[_MMCAMCORDER_ENCSINK_BIN].gst)->ref_count > 0)
1149                         gst_object_unref(sc->encode_element[_MMCAMCORDER_ENCSINK_BIN].gst);
1150
1151                 MMCAM_LOG_INFO("_MMCAMCORDER_ENCSINK_BIN is Already existed.");
1152         }
1153
1154         /* Create bin element */
1155         _MMCAMCORDER_BIN_MAKE(sc, sc->encode_element, _MMCAMCORDER_ENCSINK_BIN, "encodesink_bin", err);
1156
1157         /* Create child element */
1158         if (profile != MM_CAMCORDER_ENCBIN_PROFILE_AUDIO) {
1159                 GstCaps *caps_from_pad = NULL;
1160                 char *caps_str = NULL;
1161
1162                 /* create appsrc and capsfilter */
1163                 _MMCAMCORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMCAMCORDER_ENCSINK_SRC, "appsrc", "encodesink_src", element_list, err);
1164
1165                 _MMCAMCORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMCAMCORDER_ENCSINK_FILT, "capsfilter", "encodesink_filter", element_list, err);
1166
1167                 /* set appsrc as live source */
1168                 MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_SRC].gst, "is-live", TRUE);
1169                 MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_SRC].gst, "format", 3); /* GST_FORMAT_TIME */
1170                 MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_SRC].gst, "max-bytes", 0); /* unlimited */
1171
1172                 /* set capsfilter */
1173                 if (profile == MM_CAMCORDER_ENCBIN_PROFILE_VIDEO) {
1174                         if (_mmcamcorder_is_encoded_preview_pixel_format(sc->info_image->preview_format)) {
1175                                 MMCAM_LOG_INFO("get pad from videosrc_filter");
1176                                 pad = gst_element_get_static_pad(sc->element[_MMCAMCORDER_VIDEOSRC_FILT].gst, "src");
1177                         } else {
1178                                 MMCAM_LOG_INFO("get pad from videosrc_que");
1179                                 pad = gst_element_get_static_pad(sc->element[_MMCAMCORDER_VIDEOSRC_QUE].gst, "src");
1180                         }
1181                         if (!pad) {
1182                                 MMCAM_LOG_ERROR("get videosrc_que src pad failed");
1183                                 err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1184                                 goto pipeline_creation_error;
1185                         }
1186
1187                         caps_from_pad = gst_pad_get_allowed_caps(pad);
1188                         video_caps = gst_caps_copy(caps_from_pad);
1189                         gst_caps_unref(caps_from_pad);
1190                         caps_from_pad = NULL;
1191                         gst_object_unref(pad);
1192                         pad = NULL;
1193
1194                         /* fixate caps */
1195                         video_caps = gst_caps_fixate(video_caps);
1196                 } else {
1197                         /* Image */
1198                         MMCAMCORDER_G_OBJECT_GET(sc->element[_MMCAMCORDER_VIDEOSRC_FILT].gst, "caps", &video_caps);
1199                 }
1200
1201                 if (!video_caps) {
1202                         MMCAM_LOG_ERROR("create recording pipeline caps failed");
1203                         err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1204                         goto pipeline_creation_error;
1205                 }
1206
1207                 if (profile == MM_CAMCORDER_ENCBIN_PROFILE_VIDEO) {
1208                         if (sc->info_video->use_videoscale) {
1209                                 gst_caps_set_simple(video_caps,
1210                                         "width", G_TYPE_INT, sc->info_video->preview_width,
1211                                         "height", G_TYPE_INT, sc->info_video->preview_height,
1212                                         NULL);
1213                                 videoscale_caps = gst_caps_copy(video_caps);
1214                         } else {
1215                                 gst_caps_set_simple(video_caps,
1216                                         "width", G_TYPE_INT, sc->info_video->video_width,
1217                                         "height", G_TYPE_INT, sc->info_video->video_height,
1218                                         NULL);
1219                         }
1220                 }
1221
1222                 caps_str = gst_caps_to_string(video_caps);
1223
1224                 MMCAM_LOG_INFO("encodebin caps [%s]", caps_str);
1225
1226                 g_free(caps_str);
1227                 caps_str = NULL;
1228
1229                 MMCAMCORDER_G_OBJECT_SET_POINTER(sc->encode_element[_MMCAMCORDER_ENCSINK_FILT].gst, "caps", video_caps);
1230
1231                 gst_caps_unref(video_caps);
1232                 video_caps = NULL;
1233
1234                 /* video scale */
1235                 if (profile == MM_CAMCORDER_ENCBIN_PROFILE_VIDEO &&
1236                         sc->info_video->use_videoscale) {
1237                         if (!videoscale_caps) {
1238                                 MMCAM_LOG_ERROR("no videoscale caps");
1239                                 err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1240                                 goto pipeline_creation_error;
1241                         }
1242
1243                         gst_caps_set_simple(videoscale_caps,
1244                                 "width", G_TYPE_INT, sc->info_video->video_width,
1245                                 "height", G_TYPE_INT, sc->info_video->video_height,
1246                                 NULL);
1247
1248                         _mmcamcorder_conf_get_element(handle, hcamcorder->conf_main,
1249                                 CONFIGURE_CATEGORY_MAIN_RECORD,
1250                                 "VideoscaleElement",
1251                                 &VideoscaleElement);
1252
1253                         if (!_mmcamcorder_conf_get_value_element_name(VideoscaleElement, &gst_element_vscale_name)) {
1254                                 MMCAM_LOG_ERROR("failed to get videoscale element name");
1255                                 err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1256                                 goto pipeline_creation_error;
1257                         }
1258
1259                         caps_str = gst_caps_to_string(videoscale_caps);
1260
1261                         MMCAM_LOG_INFO("encodebin videocale [%s][%s]", gst_element_vscale_name, caps_str);
1262
1263                         g_free(caps_str);
1264                         caps_str = NULL;
1265
1266                         _MMCAMCORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMCAMCORDER_ENCSINK_VSCALE, gst_element_vscale_name, "encodesink_vscale", element_list, err);
1267                         _MMCAMCORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMCAMCORDER_ENCSINK_VSCALE_FILT, "capsfilter", "encodesink_vscale_filter", element_list, err);
1268
1269                         MMCAMCORDER_G_OBJECT_SET_POINTER((sc->encode_element[_MMCAMCORDER_ENCSINK_VSCALE_FILT].gst), "caps", videoscale_caps);
1270
1271                         gst_caps_unref(videoscale_caps);
1272                         videoscale_caps = NULL;
1273                 }
1274
1275                 /* release element_list, they will be placed out of encodesink bin */
1276                 if (element_list) {
1277                         g_list_free(element_list);
1278                         element_list = NULL;
1279                 }
1280
1281                 /* connect signal for ready to push buffer */
1282                 MMCAMCORDER_SIGNAL_CONNECT(sc->encode_element[_MMCAMCORDER_ENCSINK_SRC].gst,
1283                         _MMCAMCORDER_HANDLER_VIDEOREC,
1284                         "need-data",
1285                         _mmcamcorder_ready_to_encode_callback,
1286                         hcamcorder);
1287         }
1288
1289         _MMCAMCORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMCAMCORDER_ENCSINK_ENCBIN,
1290                 "tizenencodebin", "encodesink_encbin", element_list, err);
1291
1292         /* check element availability */
1293         if (profile == MM_CAMCORDER_ENCBIN_PROFILE_IMAGE) {
1294                 err = mm_camcorder_get_attributes(handle, &err_name,
1295                         MMCAM_CAPTURE_FORMAT, &cap_format,
1296                         NULL);
1297         } else {
1298                 err = mm_camcorder_get_attributes(handle, &err_name,
1299                         MMCAM_AUDIO_ENCODER, &audio_enc,
1300                         MMCAM_AUDIO_CHANNEL, &channel,
1301                         MMCAM_VIDEO_ENCODER_BITRATE, &v_bitrate,
1302                         MMCAM_AUDIO_ENCODER_BITRATE, &a_bitrate,
1303                         NULL);
1304         }
1305
1306         if (err != MM_ERROR_NONE) {
1307                 if (err_name) {
1308                         MMCAM_LOG_ERROR("failed to get attributes [%s][0x%x]", err_name, err);
1309                         SAFE_FREE(err_name);
1310                 } else {
1311                         MMCAM_LOG_ERROR("failed to get attributes [0x%x]", err);
1312                 }
1313
1314                 goto pipeline_creation_error;
1315         }
1316
1317         MMCAM_LOG_INFO("Profile[%d]", profile);
1318
1319         /* Set information */
1320         if (profile == MM_CAMCORDER_ENCBIN_PROFILE_VIDEO) {
1321                 str_profile = "VideoProfile";
1322                 str_aac = "VideoAutoAudioConvert";
1323                 str_aar = "VideoAutoAudioResample";
1324                 str_acs = "VideoAutoColorSpace";
1325         } else if (profile == MM_CAMCORDER_ENCBIN_PROFILE_AUDIO) {
1326                 str_profile = "AudioProfile";
1327                 str_aac = "AudioAutoAudioConvert";
1328                 str_aar = "AudioAutoAudioResample";
1329                 str_acs = "AudioAutoColorSpace";
1330         } else if (profile == MM_CAMCORDER_ENCBIN_PROFILE_IMAGE) {
1331                 str_profile = "ImageProfile";
1332                 str_aac = "ImageAutoAudioConvert";
1333                 str_aar = "ImageAutoAudioResample";
1334                 str_acs = "ImageAutoColorSpace";
1335         }
1336
1337         _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_main, CONFIGURE_CATEGORY_MAIN_RECORD, str_profile, &encodebin_profile);
1338         _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_main, CONFIGURE_CATEGORY_MAIN_RECORD, str_aac, &auto_audio_convert);
1339         _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_main, CONFIGURE_CATEGORY_MAIN_RECORD, str_aar, &auto_audio_resample);
1340         _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_main, CONFIGURE_CATEGORY_MAIN_RECORD, str_acs, &auto_color_space);
1341
1342         MMCAM_LOG_INFO("Profile:%d, AutoAudioConvert:%d, AutoAudioResample:%d, AutoColorSpace:%d",
1343                 encodebin_profile, auto_audio_convert, auto_audio_resample, auto_color_space);
1344
1345         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "profile", encodebin_profile);
1346         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "auto-audio-convert", auto_audio_convert);
1347         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "auto-audio-resample", auto_audio_resample);
1348         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "auto-colorspace", auto_color_space);
1349         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "use-video-toggle", FALSE);
1350
1351         /* get video convert element name */
1352         _mmcamcorder_conf_get_value_element_name(sc->VideoconvertElement, &videoconvert_name);
1353
1354         /* Codec */
1355         if (profile == MM_CAMCORDER_ENCBIN_PROFILE_VIDEO) {
1356                 int use_venc_queue = 0;
1357
1358                 VideoencElement = _mmcamcorder_get_type_element(handle, MM_CAM_VIDEO_ENCODER);
1359
1360                 if (!VideoencElement) {
1361                         MMCAM_LOG_ERROR("Fail to get type element");
1362                         err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1363                         goto pipeline_creation_error;
1364                 }
1365
1366                 if (_mmcamcorder_is_encoded_preview_pixel_format(sc->info_image->preview_format)) {
1367                         /* set dummy element */
1368                         gst_element_venc_name = "identity";
1369                 } else {
1370                         _mmcamcorder_conf_get_value_element_name(VideoencElement, &gst_element_venc_name);
1371                 }
1372
1373                 if (gst_element_venc_name) {
1374                         MMCAM_LOG_INFO("video encoder name [%s]", gst_element_venc_name);
1375
1376                         MMCAMCORDER_G_OBJECT_SET_POINTER(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "venc-name", gst_element_venc_name);
1377                         _MMCAMCORDER_ENCODEBIN_ELMGET(sc, _MMCAMCORDER_ENCSINK_VENC, "video-encode", err);
1378                 } else {
1379                         MMCAM_LOG_ERROR("Fail to get video encoder name");
1380                         err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1381                         goto pipeline_creation_error;
1382                 }
1383
1384                 /* set color space converting element */
1385                 if (auto_color_space) {
1386                         MMCAM_LOG_INFO("set video convert element [%s]", videoconvert_name);
1387
1388                         MMCAMCORDER_G_OBJECT_SET_POINTER(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "vconv-name", videoconvert_name);
1389                         _MMCAMCORDER_ENCODEBIN_ELMGET(sc, _MMCAMCORDER_ENCSINK_VCONV, "video-convert", err);
1390
1391                         /* set colorspace plugin property setting */
1392                         _mmcamcorder_conf_set_value_element_property(sc->encode_element[_MMCAMCORDER_ENCSINK_VCONV].gst, sc->VideoconvertElement);
1393
1394                         /* fourcc type was removed in GST 1.0 */
1395                         if (hcamcorder->use_zero_copy_format) {
1396                                 if (strstr(gst_element_venc_name, "omx")) {
1397                                         video_caps = gst_caps_new_simple("video/x-raw", "format", G_TYPE_STRING, "SN12", NULL);
1398
1399                                         if (video_caps) {
1400                                                 MMCAMCORDER_G_OBJECT_SET_POINTER(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "vcaps", video_caps);
1401                                                 MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_VCONV].gst, "dst-buffer-num", _MMCAMCORDER_CONVERT_OUTPUT_BUFFER_NUM);
1402
1403                                                 gst_caps_unref(video_caps);
1404                                                 video_caps = NULL;
1405                                         } else {
1406                                                 MMCAM_LOG_WARNING("failed to create caps");
1407                                         }
1408                                 } else {
1409                                         MMCAM_LOG_INFO("current video codec is not openmax but [%s]", gst_element_venc_name);
1410                                 }
1411                         }
1412                 }
1413
1414                 _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_main,
1415                         CONFIGURE_CATEGORY_MAIN_RECORD,
1416                         "UseVideoEncoderQueue",
1417                         &use_venc_queue);
1418                 if (use_venc_queue)
1419                         _MMCAMCORDER_ENCODEBIN_ELMGET(sc, _MMCAMCORDER_ENCSINK_VENC_QUE, "use-venc-queue", err);
1420         }
1421
1422         if (sc->audio_disable == FALSE &&
1423             profile != MM_CAMCORDER_ENCBIN_PROFILE_IMAGE) {
1424                 int use_aenc_queue = 0;
1425
1426                 AudioencElement = _mmcamcorder_get_type_element(handle, MM_CAM_AUDIO_ENCODER);
1427                 if (!AudioencElement) {
1428                         MMCAM_LOG_ERROR("Fail to get type element");
1429                         err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1430                         goto pipeline_creation_error;
1431                 }
1432
1433                 _mmcamcorder_conf_get_value_element_name(AudioencElement, &gst_element_aenc_name);
1434
1435                 MMCAMCORDER_G_OBJECT_SET_POINTER(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "aenc-name", gst_element_aenc_name);
1436                 _MMCAMCORDER_ENCODEBIN_ELMGET(sc, _MMCAMCORDER_ENCSINK_AENC, "audio-encode", err);
1437
1438                 if (audio_enc == MM_AUDIO_CODEC_AMR && channel == 2) {
1439                         audio_caps = gst_caps_new_simple("audio/x-raw",
1440                                                                                          "channels", G_TYPE_INT, 1,
1441                                                                                          NULL);
1442                         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "auto-audio-convert", TRUE);
1443                         MMCAMCORDER_G_OBJECT_SET_POINTER(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "acaps", audio_caps);
1444                         gst_caps_unref(audio_caps);
1445                         audio_caps = NULL;
1446                 }
1447
1448                 if (audio_enc == MM_AUDIO_CODEC_OGG) {
1449                         audio_caps = gst_caps_new_empty_simple("audio/x-raw");
1450                         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "auto-audio-convert", TRUE);
1451                         MMCAMCORDER_G_OBJECT_SET_POINTER(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "acaps", audio_caps);
1452                         gst_caps_unref(audio_caps);
1453                         audio_caps = NULL;
1454                         MMCAM_LOG_INFO("***** MM_AUDIO_CODEC_OGG : setting audio/x-raw-int ");
1455                 }
1456
1457                 _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_main,
1458                         CONFIGURE_CATEGORY_MAIN_RECORD,
1459                         "UseAudioEncoderQueue",
1460                         &use_aenc_queue);
1461                 if (use_aenc_queue) {
1462                         _MMCAMCORDER_ENCODEBIN_ELMGET(sc, _MMCAMCORDER_ENCSINK_AENC_QUE, "use-aenc-queue", err);
1463                         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_AENC_QUE].gst, "max-size-time", 0);
1464                         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_AENC_QUE].gst, "max-size-buffers", 0);
1465                 }
1466         }
1467
1468         if (profile == MM_CAMCORDER_ENCBIN_PROFILE_IMAGE) {
1469                 if (cap_format == MM_PIXEL_FORMAT_ENCODED) {
1470                         ImageencElement = _mmcamcorder_get_type_element(handle, MM_CAM_IMAGE_ENCODER);
1471                         if (!ImageencElement) {
1472                                 MMCAM_LOG_ERROR("Fail to get type element");
1473                                 err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1474                                 goto pipeline_creation_error;
1475                         }
1476
1477                         _mmcamcorder_conf_get_value_element_name(ImageencElement, &gst_element_ienc_name);
1478                 } else {
1479                         /* raw format - set dummy element */
1480                         gst_element_ienc_name = "identity";
1481                 }
1482
1483                 MMCAMCORDER_G_OBJECT_SET_POINTER(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "ienc-name", gst_element_ienc_name);
1484                 _MMCAMCORDER_ENCODEBIN_ELMGET(sc, _MMCAMCORDER_ENCSINK_IENC, "image-encode", err);
1485         }
1486
1487         /* Mux */
1488         if (profile != MM_CAMCORDER_ENCBIN_PROFILE_IMAGE) {
1489                 MuxElement = _mmcamcorder_get_type_element(handle, MM_CAM_FILE_FORMAT);
1490                 if (!MuxElement) {
1491                         MMCAM_LOG_ERROR("Fail to get type element");
1492                         err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1493                         goto pipeline_creation_error;
1494                 }
1495
1496                 _mmcamcorder_conf_get_value_element_name(MuxElement, &gst_element_mux_name);
1497
1498                 MMCAMCORDER_G_OBJECT_SET_POINTER(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "mux-name", gst_element_mux_name);
1499                 _MMCAMCORDER_ENCODEBIN_ELMGET(sc, _MMCAMCORDER_ENCSINK_MUX, "mux", err);
1500
1501                 _mmcamcorder_conf_set_value_element_property(sc->encode_element[_MMCAMCORDER_ENCSINK_MUX].gst, MuxElement);
1502         }
1503
1504         /* Sink */
1505         if (profile != MM_CAMCORDER_ENCBIN_PROFILE_IMAGE) {
1506                 /* for recording */
1507                 _mmcamcorder_conf_get_element(handle, hcamcorder->conf_main,
1508                         CONFIGURE_CATEGORY_MAIN_RECORD,
1509                         "RecordsinkElement",
1510                         &RecordsinkElement);
1511                 _mmcamcorder_conf_get_value_element_name(RecordsinkElement, &gst_element_rsink_name);
1512
1513                 _MMCAMCORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMCAMCORDER_ENCSINK_SINK, gst_element_rsink_name, NULL, element_list, err);
1514
1515                 _mmcamcorder_conf_set_value_element_property(sc->encode_element[_MMCAMCORDER_ENCSINK_SINK].gst, RecordsinkElement);
1516         } else {
1517                 /* for stillshot */
1518                 _MMCAMCORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMCAMCORDER_ENCSINK_SINK, "fakesink", NULL, element_list, err);
1519         }
1520
1521         if (profile == MM_CAMCORDER_ENCBIN_PROFILE_VIDEO) {
1522                 /* property setting in ini */
1523                 _mmcamcorder_conf_set_value_element_property(sc->encode_element[_MMCAMCORDER_ENCSINK_VENC].gst, VideoencElement);
1524
1525                 /* bitrate setting */
1526                 _mmcamcorder_set_encoder_bitrate(MM_CAMCORDER_ENCODER_TYPE_VIDEO, 0,
1527                         v_bitrate, sc->encode_element[_MMCAMCORDER_ENCSINK_VENC].gst);
1528         }
1529
1530         if (sc->audio_disable == FALSE &&
1531             profile != MM_CAMCORDER_ENCBIN_PROFILE_IMAGE) {
1532                 /* property setting in ini */
1533                 _mmcamcorder_conf_set_value_element_property(sc->encode_element[_MMCAMCORDER_ENCSINK_AENC].gst, AudioencElement);
1534
1535                 /* bitrate setting */
1536                 _mmcamcorder_set_encoder_bitrate(MM_CAMCORDER_ENCODER_TYPE_AUDIO, audio_enc,
1537                         a_bitrate, sc->encode_element[_MMCAMCORDER_ENCSINK_AENC].gst);
1538         }
1539
1540         MMCAM_LOG_INFO("Element creation complete");
1541
1542         /* Add element to bin */
1543         if (!_mmcamcorder_add_elements_to_bin(GST_BIN(sc->encode_element[_MMCAMCORDER_ENCSINK_BIN].gst), element_list)) {
1544                 MMCAM_LOG_ERROR("element add error.");
1545                 err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1546                 goto pipeline_creation_error;
1547         }
1548
1549         MMCAM_LOG_INFO("Element add complete");
1550
1551         if (profile == MM_CAMCORDER_ENCBIN_PROFILE_VIDEO) {
1552                 pad = gst_element_request_pad_simple(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "video");
1553                 if (!gst_element_add_pad(sc->encode_element[_MMCAMCORDER_ENCSINK_BIN].gst, gst_ghost_pad_new("video_sink0", pad))) {
1554                         gst_object_unref(pad);
1555                         pad = NULL;
1556                         MMCAM_LOG_ERROR("failed to create ghost video_sink0 on _MMCAMCORDER_ENCSINK_BIN.");
1557                         err = MM_ERROR_CAMCORDER_GST_LINK;
1558                         goto pipeline_creation_error;
1559                 }
1560                 gst_object_unref(pad);
1561                 pad = NULL;
1562
1563                 if (sc->audio_disable == FALSE) {
1564                         pad = gst_element_request_pad_simple(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "audio");
1565                         if (!gst_element_add_pad(sc->encode_element[_MMCAMCORDER_ENCSINK_BIN].gst, gst_ghost_pad_new("audio_sink0", pad))) {
1566                                 gst_object_unref(pad);
1567                                 pad = NULL;
1568                                 MMCAM_LOG_ERROR("failed to create ghost audio_sink0 on _MMCAMCORDER_ENCSINK_BIN.");
1569                                 err = MM_ERROR_CAMCORDER_GST_LINK;
1570                                 goto pipeline_creation_error;
1571                         }
1572                         gst_object_unref(pad);
1573                         pad = NULL;
1574                 }
1575         } else if (profile == MM_CAMCORDER_ENCBIN_PROFILE_AUDIO) {
1576                 pad = gst_element_request_pad_simple(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "audio");
1577                 if (!gst_element_add_pad(sc->encode_element[_MMCAMCORDER_ENCSINK_BIN].gst, gst_ghost_pad_new("audio_sink0", pad))) {
1578                         gst_object_unref(pad);
1579                         pad = NULL;
1580                         MMCAM_LOG_ERROR("failed to create ghost audio_sink0 on _MMCAMCORDER_ENCSINK_BIN.");
1581                         err = MM_ERROR_CAMCORDER_GST_LINK;
1582                         goto pipeline_creation_error;
1583                 }
1584                 gst_object_unref(pad);
1585                 pad = NULL;
1586         } else {
1587                 /* for stillshot */
1588                 pad = gst_element_request_pad_simple(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "image");
1589                 if (!gst_element_add_pad(sc->encode_element[_MMCAMCORDER_ENCSINK_BIN].gst, gst_ghost_pad_new("image_sink0", pad))) {
1590                         gst_object_unref(pad);
1591                         pad = NULL;
1592                         MMCAM_LOG_ERROR("failed to create ghost image_sink0 on _MMCAMCORDER_ENCSINK_BIN.");
1593                         err = MM_ERROR_CAMCORDER_GST_LINK;
1594                         goto pipeline_creation_error;
1595                 }
1596                 gst_object_unref(pad);
1597                 pad = NULL;
1598         }
1599
1600         MMCAM_LOG_INFO("Get pad complete");
1601
1602         /* Link internal element */
1603         if (!_mmcamcorder_link_elements(element_list)) {
1604                 MMCAM_LOG_ERROR("element link error.");
1605                 err = MM_ERROR_CAMCORDER_GST_LINK;
1606                 goto pipeline_creation_error;
1607         }
1608
1609         if (element_list)
1610                 g_list_free(element_list);
1611
1612         MMCAM_LOG_INFO("done");
1613
1614         return MM_ERROR_NONE;
1615
1616 pipeline_creation_error:
1617         _mmcamcorder_remove_all_handlers(handle, _MMCAMCORDER_HANDLER_VIDEOREC);
1618
1619         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_ENCSINK_ENCBIN);
1620         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_ENCSINK_SRC);
1621         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_ENCSINK_FILT);
1622         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_ENCSINK_VSCALE);
1623         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_ENCSINK_VSCALE_FILT);
1624         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_ENCSINK_VENC);
1625         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_ENCSINK_AENC);
1626         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_ENCSINK_IENC);
1627         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_ENCSINK_MUX);
1628         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_ENCSINK_SINK);
1629         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_ENCSINK_BIN);
1630
1631         if (element_list)
1632                 g_list_free(element_list);
1633
1634         if (videoscale_caps)
1635                 gst_caps_unref(videoscale_caps);
1636
1637         return err;
1638 }
1639
1640
1641 int _mmcamcorder_create_preview_pipeline(MMHandleType handle)
1642 {
1643         int err = MM_ERROR_NONE;
1644
1645         GstPad *srcpad = NULL;
1646         GstBus *bus = NULL;
1647
1648         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1649         _MMCamcorderSubContext *sc = NULL;
1650
1651         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1652
1653         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
1654         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1655         mmf_return_val_if_fail(sc->element, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1656
1657         MMCAM_LOG_INFO("");
1658
1659         /** Create gstreamer element **/
1660         /* Main pipeline */
1661         _MMCAMCORDER_PIPELINE_MAKE(sc, sc->element, _MMCAMCORDER_MAIN_PIPE, "camera_pipeline", err);
1662
1663         /* Sub pipeline */
1664         err = _mmcamcorder_create_preview_elements((MMHandleType)hcamcorder);
1665         if (err != MM_ERROR_NONE)
1666                 goto pipeline_creation_error;
1667
1668         /* Set data probe function */
1669         if (sc->element[_MMCAMCORDER_VIDEOSRC_QUE].gst) {
1670                 MMCAM_LOG_INFO("add video dataprobe to videosrc queue");
1671                 srcpad = gst_element_get_static_pad(sc->element[_MMCAMCORDER_VIDEOSRC_QUE].gst, "src");
1672         } else {
1673                 MMCAM_LOG_ERROR("there is no queue plugin");
1674                 goto pipeline_creation_error;
1675         }
1676
1677         if (srcpad) {
1678                 MMCAMCORDER_ADD_BUFFER_PROBE(srcpad, _MMCAMCORDER_HANDLER_PREVIEW,
1679                         __mmcamcorder_video_dataprobe_preview, hcamcorder);
1680
1681                 gst_object_unref(srcpad);
1682                 srcpad = NULL;
1683         } else {
1684                 MMCAM_LOG_ERROR("failed to get srcpad");
1685                 goto pipeline_creation_error;
1686         }
1687
1688         /* set dataprobe for video recording if it does not support dual stream. */
1689         if (sc->info_video->record_dual_stream == FALSE) {
1690                 if (_mmcamcorder_is_encoded_preview_pixel_format(sc->info_image->preview_format))
1691                         srcpad = gst_element_get_static_pad(sc->element[_MMCAMCORDER_VIDEOSRC_QUE].gst, "src");
1692                 else
1693                         srcpad = gst_element_get_static_pad(sc->element[_MMCAMCORDER_VIDEOSINK_QUE].gst, "src");
1694
1695                 MMCAMCORDER_ADD_BUFFER_PROBE(srcpad, _MMCAMCORDER_HANDLER_PREVIEW,
1696                         __mmcamcorder_video_dataprobe_record, hcamcorder);
1697                 gst_object_unref(srcpad);
1698                 srcpad = NULL;
1699         }
1700
1701         bus = gst_pipeline_get_bus(GST_PIPELINE(sc->element[_MMCAMCORDER_MAIN_PIPE].gst));
1702
1703         /* set sync handler */
1704         gst_bus_set_sync_handler(bus, _mmcamcorder_pipeline_bus_sync_callback, (gpointer)hcamcorder, NULL);
1705
1706         gst_object_unref(bus);
1707         bus = NULL;
1708
1709         return MM_ERROR_NONE;
1710
1711 pipeline_creation_error:
1712         _MMCAMCORDER_ELEMENT_REMOVE(sc->element, _MMCAMCORDER_MAIN_PIPE);
1713         return err;
1714 }
1715
1716
1717 void _mmcamcorder_ready_to_encode_callback(GstElement *element, guint size, gpointer handle)
1718 {
1719         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1720         _MMCamcorderSubContext *sc = NULL;
1721
1722         /*MMCAM_LOG_INFO("start");*/
1723
1724         mmf_return_if_fail(hcamcorder);
1725         mmf_return_if_fail(hcamcorder->sub_context);
1726         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
1727
1728         /* set flag */
1729         if (sc->info_video->push_encoding_buffer == PUSH_ENCODING_BUFFER_INIT) {
1730                 sc->info_video->push_encoding_buffer = PUSH_ENCODING_BUFFER_RUN;
1731                 MMCAM_LOG_WARNING("set push_encoding_buffer RUN");
1732         }
1733 }
1734
1735
1736 int _mmcamcorder_videosink_window_set(MMHandleType handle, type_element* VideosinkElement)
1737 {
1738         int err = MM_ERROR_NONE;
1739         int size = 0;
1740         int rect_x = 0;
1741         int rect_y = 0;
1742         int rect_width = 0;
1743         int rect_height = 0;
1744         int visible = 0;
1745         int rotation = MM_DISPLAY_ROTATION_NONE;
1746         int flip = MM_FLIP_NONE;
1747         int display_mode = MM_DISPLAY_MODE_DEFAULT;
1748         int display_geometry_method = MM_DISPLAY_METHOD_LETTER_BOX;
1749         int origin_size = 0;
1750         int zoom_attr = 0;
1751         int zoom_level = 0;
1752         int do_scaling = FALSE;
1753         int *dp_handle = NULL;
1754         MMCamWindowInfo *window_info = NULL;
1755         gulong xid;
1756         char *err_name = NULL;
1757         const char *videosink_name = NULL;
1758
1759         GstElement *vsink = NULL;
1760
1761         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1762         _MMCamcorderSubContext *sc = NULL;
1763
1764         MMCAM_LOG_INFO("");
1765
1766         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1767
1768         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
1769         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1770         mmf_return_val_if_fail(sc->element, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1771         mmf_return_val_if_fail(sc->element[_MMCAMCORDER_VIDEOSINK_SINK].gst, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1772
1773         vsink = sc->element[_MMCAMCORDER_VIDEOSINK_SINK].gst;
1774
1775         /* Get video display information */
1776         err = mm_camcorder_get_attributes(handle, &err_name,
1777                 MMCAM_DISPLAY_RECT_X, &rect_x,
1778                 MMCAM_DISPLAY_RECT_Y, &rect_y,
1779                 MMCAM_DISPLAY_RECT_WIDTH, &rect_width,
1780                 MMCAM_DISPLAY_RECT_HEIGHT, &rect_height,
1781                 MMCAM_DISPLAY_ROTATION, &rotation,
1782                 MMCAM_DISPLAY_FLIP, &flip,
1783                 MMCAM_DISPLAY_VISIBLE, &visible,
1784                 MMCAM_DISPLAY_HANDLE, (void **)&dp_handle, &size,
1785                 MMCAM_DISPLAY_MODE, &display_mode,
1786                 MMCAM_DISPLAY_GEOMETRY_METHOD, &display_geometry_method,
1787                 MMCAM_DISPLAY_SCALE, &zoom_attr,
1788                 MMCAM_DISPLAY_EVAS_DO_SCALING, &do_scaling,
1789                 NULL);
1790         if (err != MM_ERROR_NONE) {
1791                 if (err_name) {
1792                         MMCAM_LOG_ERROR("failed to get attributes [%s][0x%x]", err_name, err);
1793                         SAFE_FREE(err_name);
1794                 } else {
1795                         MMCAM_LOG_ERROR("failed to get attributes [0x%x]", err);
1796                 }
1797
1798                 return err;
1799         }
1800
1801         _mmcamcorder_conf_get_value_element_name(VideosinkElement, &videosink_name);
1802
1803         if (!videosink_name) {
1804                 MMCAM_LOG_ERROR("failed to get videosink name");
1805                 return MM_ERROR_CAMCORDER_INTERNAL;
1806         }
1807
1808         MMCAM_LOG_INFO("(dp_handle=%p, size=%d)", dp_handle, size);
1809
1810         /* Set display handle */
1811         if (!strcmp(videosink_name, "xvimagesink") || !strcmp(videosink_name, "ximagesink")) {
1812                 if (dp_handle) {
1813                         xid = *dp_handle;
1814                         MMCAM_LOG_INFO("xid = %lu )", xid);
1815                         gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(vsink), xid);
1816                 } else {
1817                         MMCAM_LOG_WARNING("Handle is NULL. Set xid as 0.. but, it's not recommended.");
1818                         gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(vsink), 0);
1819                 }
1820         } else if (!strcmp(videosink_name, "evasimagesink") || !strcmp(videosink_name, "evaspixmapsink")) {
1821                 MMCAM_LOG_INFO("videosink : %s, handle : %p", videosink_name, dp_handle);
1822
1823                 if (dp_handle) {
1824                         MMCAMCORDER_G_OBJECT_SET_POINTER(vsink, "evas-object", dp_handle);
1825                         MMCAMCORDER_G_OBJECT_SET(vsink, "origin-size", !do_scaling);
1826                 } else {
1827                         MMCAM_LOG_ERROR("display handle(eavs object) is NULL");
1828                         return MM_ERROR_CAMCORDER_INVALID_ARGUMENT;
1829                 }
1830         } else if (!strcmp(videosink_name, "tizenwlsink")) {
1831                 if (dp_handle) {
1832                         window_info = (MMCamWindowInfo *)dp_handle;
1833                         MMCAM_LOG_INFO("wayland global surface id : %d", window_info->surface_id);
1834                         gst_video_overlay_set_wl_window_wl_surface_id(GST_VIDEO_OVERLAY(vsink), window_info->surface_id);
1835                 } else {
1836                         MMCAM_LOG_WARNING("Handle is NULL. skip setting.");
1837                 }
1838         } else if (!strcmp(videosink_name, "directvideosink")) {
1839 #ifdef _MMCAMCORDER_RM_SUPPORT
1840                 /* set the videosink using the virtual device id instead of the real id (main=0, sub=1) */
1841                 MMCAM_LOG_INFO("device-scaler : %d", hcamcorder->returned_devices.device_id[1]);
1842                 MMCAMCORDER_G_OBJECT_SET(vsink, "device-scaler", hcamcorder->returned_devices.device_id[1]);
1843 #endif /* _MMCAMCORDER_RM_SUPPORT */
1844                 if (dp_handle) {
1845                         window_info = (MMCamWindowInfo *)dp_handle;
1846                         MMCAM_LOG_INFO("wayland global surface id : %d, x,y,w,h (%d,%d,%d,%d)",
1847                                 window_info->surface_id,
1848                                 window_info->rect.x,
1849                                 window_info->rect.y,
1850                                 window_info->rect.width,
1851                                 window_info->rect.height);
1852                         gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(vsink), (guintptr)window_info->surface_id);
1853                         gst_video_overlay_set_render_rectangle(GST_VIDEO_OVERLAY(vsink),
1854                                 window_info->rect.x,
1855                                 window_info->rect.y,
1856                                 window_info->rect.width,
1857                                 window_info->rect.height);
1858                 } else {
1859                         MMCAM_LOG_WARNING("dp_handle is null");
1860                 }
1861         } else {
1862                 MMCAM_LOG_WARNING("Who are you?? (Videosink: %s)", videosink_name);
1863         }
1864
1865         MMCAM_LOG_INFO("%s set: display_geometry_method[%d],origin-size[%d],visible[%d],rotate[%d],flip[%d]",
1866                 videosink_name, display_geometry_method, origin_size, visible, rotation, flip);
1867
1868         /* Set attribute */
1869         if (!strcmp(videosink_name, "xvimagesink") || !strcmp(videosink_name, "tizenwlsink") ||
1870             !strcmp(videosink_name, "evaspixmapsink") || !strcmp(videosink_name, "directvideosink")) {
1871                 /* set rotation */
1872                 MMCAMCORDER_G_OBJECT_SET(vsink, "rotate", rotation);
1873
1874                 /* set flip */
1875                 MMCAMCORDER_G_OBJECT_SET(vsink, "flip", flip);
1876
1877                 switch (zoom_attr) {
1878                 case MM_DISPLAY_SCALE_DEFAULT:
1879                         zoom_level = 1;
1880                         break;
1881                 case MM_DISPLAY_SCALE_DOUBLE_LENGTH:
1882                         zoom_level = 2;
1883                         break;
1884                 case MM_DISPLAY_SCALE_TRIPLE_LENGTH:
1885                         zoom_level = 3;
1886                         break;
1887                 default:
1888                         MMCAM_LOG_WARNING("Unsupported zoom value. set as default.");
1889                         zoom_level = 1;
1890                         break;
1891                 }
1892
1893                 MMCAMCORDER_G_OBJECT_SET(vsink, "display-geometry-method", display_geometry_method);
1894                 MMCAMCORDER_G_OBJECT_SET(vsink, "display-mode", display_mode);
1895                 MMCAMCORDER_G_OBJECT_SET(vsink, "visible", visible);
1896                 MMCAMCORDER_G_OBJECT_SET(vsink, "zoom", zoom_level);
1897
1898                 if (display_geometry_method == MM_DISPLAY_METHOD_CUSTOM_ROI) {
1899                         if (!strcmp(videosink_name, "tizenwlsink")) {
1900                             gst_video_overlay_set_display_roi_area(GST_VIDEO_OVERLAY(vsink),
1901                                         rect_x, rect_y, rect_width, rect_height);
1902                         } else {
1903                                 g_object_set(vsink,
1904                                         "dst-roi-x", rect_x,
1905                                         "dst-roi-y", rect_y,
1906                                         "dst-roi-w", rect_width,
1907                                         "dst-roi-h", rect_height,
1908                                         NULL);
1909                         }
1910                 }
1911         }
1912
1913         return MM_ERROR_NONE;
1914 }
1915
1916
1917 int _mmcamcorder_video_frame_stabilize(MMHandleType handle, int cmd)
1918 {
1919         int category = 0;
1920         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1921         _MMCamcorderSubContext *sc = NULL;
1922
1923         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1924
1925         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
1926
1927         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1928
1929         switch (cmd) {
1930         case _MMCamcorder_CMD_PREVIEW_START:
1931                 category = CONFIGURE_CATEGORY_CTRL_CAMERA;
1932                 break;
1933         case _MMCamcorder_CMD_CAPTURE:
1934                 category = CONFIGURE_CATEGORY_CTRL_CAPTURE;
1935                 break;
1936         default:
1937                 MMCAM_LOG_WARNING("unknown command : %d", cmd);
1938                 return MM_ERROR_CAMCORDER_INVALID_ARGUMENT;
1939         }
1940
1941         _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_ctrl,
1942                 category, "FrameStabilityCount", &sc->frame_stability_count);
1943
1944         MMCAM_LOG_INFO("[cmd %d] frame stability count : %d",
1945                 cmd, sc->frame_stability_count);
1946
1947         return MM_ERROR_NONE;
1948 }
1949
1950 /* Retreive device information and set them to attributes */
1951 gboolean _mmcamcorder_get_device_info(MMHandleType handle)
1952 {
1953         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1954         _MMCamcorderSubContext *sc = NULL;
1955         GstCameraControl *control = NULL;
1956         GstCameraControlExifInfo exif_info = {0,};
1957
1958         mmf_return_val_if_fail(hcamcorder, FALSE);
1959         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
1960
1961         memset(&exif_info, 0x0, sizeof(GstCameraControlExifInfo));
1962
1963         if (sc && sc->element) {
1964                 int err = MM_ERROR_NONE;
1965                 char *err_name = NULL;
1966                 double focal_len = 0.0;
1967
1968                 /* Video input device */
1969                 if (sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst) {
1970                         /* Exif related information */
1971                         control = GST_CAMERA_CONTROL(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst);
1972                         if (control != NULL) {
1973                                 gst_camera_control_get_exif_info(control, &exif_info); /* get video input device information */
1974                                 if (exif_info.focal_len_denominator != 0)
1975                                         focal_len = ((double)exif_info.focal_len_numerator) / ((double) exif_info.focal_len_denominator);
1976                         } else {
1977                                 MMCAM_LOG_ERROR("Fail to get camera control interface!");
1978                                 focal_len = 0.0;
1979                         }
1980                 }
1981
1982                 /* Set values to attributes */
1983                 err = mm_camcorder_set_attributes(handle, &err_name,
1984                         MMCAM_CAMERA_FOCAL_LENGTH, focal_len,
1985                         NULL);
1986                 if (err != MM_ERROR_NONE) {
1987                         MMCAM_LOG_ERROR("Set attributes error(%s:%x)!", err_name, err);
1988                         SAFE_FREE(err_name);
1989                         return FALSE;
1990                 }
1991         } else {
1992                 MMCAM_LOG_WARNING("Sub context isn't exist.");
1993                 return FALSE;
1994         }
1995
1996         return TRUE;
1997 }
1998
1999 static guint32 _mmcamcorder_convert_fourcc_string_to_value(const gchar* format_name)
2000 {
2001         return format_name[0] | (format_name[1] << 8) | (format_name[2] << 16) | (format_name[3] << 24);
2002 }
2003
2004 static GstPadProbeReturn __mmcamcorder_video_dataprobe_preview(GstPad *pad, GstPadProbeInfo *info, gpointer u_data)
2005 {
2006         gboolean ret = TRUE;
2007         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(u_data);
2008         _MMCamcorderSubContext *sc = NULL;
2009         GstBuffer *buffer = GST_PAD_PROBE_INFO_BUFFER(info);
2010         GstSample *sample = NULL;
2011         GstCaps *caps = NULL;
2012
2013         mmf_return_val_if_fail(buffer, GST_PAD_PROBE_DROP);
2014         mmf_return_val_if_fail(gst_buffer_n_memory(buffer), GST_PAD_PROBE_DROP);
2015         mmf_return_val_if_fail(hcamcorder, GST_PAD_PROBE_DROP);
2016
2017         sc = MMF_CAMCORDER_SUBCONTEXT(u_data);
2018         mmf_return_val_if_fail(sc, GST_PAD_PROBE_DROP);
2019
2020         if (sc->drop_vframe > 0) {
2021                 if (sc->pass_first_vframe > 0) {
2022                         sc->pass_first_vframe--;
2023                         MMCAM_LOG_INFO("Pass video frame by pass_first_vframe");
2024                 } else {
2025                         sc->drop_vframe--;
2026                         MMCAM_LOG_INFO("Drop video frame by drop_vframe");
2027                         return GST_PAD_PROBE_DROP;
2028                 }
2029         } else if (sc->frame_stability_count > 0) {
2030                 sc->frame_stability_count--;
2031                 MMCAM_LOG_INFO("Drop video frame by frame_stability_count");
2032                 return GST_PAD_PROBE_DROP;
2033         }
2034
2035         if (hcamcorder->measure_preview_fps && hcamcorder->state >= MM_CAMCORDER_STATE_PREPARE)
2036                 _mmcamcorder_measure_fps(&sc->kpi);
2037
2038         /* The first H.264 frame should not be skipped for vstream cb. */
2039         if (hcamcorder->state < MM_CAMCORDER_STATE_PREPARE &&
2040                 !_mmcamcorder_is_encoded_preview_pixel_format(sc->info_image->preview_format)) {
2041                 MMCAM_LOG_WARNING("Not ready for stream callback");
2042                 return GST_PAD_PROBE_OK;
2043         }
2044
2045         /* make sample with buffer and caps */
2046         caps = gst_pad_get_allowed_caps(pad);
2047         mmf_return_val_if_fail(caps, GST_PAD_PROBE_OK);
2048
2049         sample = gst_sample_new(buffer, caps, NULL, NULL);
2050         gst_caps_unref(caps);
2051         mmf_return_val_if_fail(sample, GST_PAD_PROBE_OK);
2052
2053         ret = _mmcamcorder_invoke_video_stream_cb((MMHandleType)hcamcorder, sample, TRUE, -1);
2054
2055         gst_sample_unref(sample);
2056
2057         if (ret)
2058                 return GST_PAD_PROBE_OK;
2059         else
2060                 return GST_PAD_PROBE_DROP;
2061 }
2062
2063
2064 static GstPadProbeReturn __mmcamcorder_video_dataprobe_record(GstPad *pad, GstPadProbeInfo *info, gpointer u_data)
2065 {
2066         gboolean ret = TRUE;
2067         GstSample *sample = NULL;
2068         GstCaps *caps = NULL;
2069
2070         caps = gst_pad_get_allowed_caps(pad);
2071         mmf_return_val_if_fail(caps, GST_PAD_PROBE_OK);
2072
2073         sample = gst_sample_new(GST_PAD_PROBE_INFO_BUFFER(info), caps, NULL, NULL);
2074         gst_caps_unref(caps);
2075         mmf_return_val_if_fail(sample, GST_PAD_PROBE_OK);
2076
2077         ret = _mmcamcorder_video_push_buffer(u_data, sample);
2078
2079         gst_sample_unref(sample);
2080
2081         return (ret ? GST_PAD_PROBE_OK : GST_PAD_PROBE_DROP);
2082 }
2083
2084
2085 GstPadProbeReturn __mmcamcorder_muxed_dataprobe(GstPad *pad, GstPadProbeInfo *info, gpointer u_data)
2086 {
2087         MMCamcorderMuxedStreamDataType stream;
2088         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(u_data);
2089         _MMCamcorderSubContext *sc = NULL;
2090         GstBuffer *buffer = GST_PAD_PROBE_INFO_BUFFER(info);
2091         GstMapInfo mapinfo;
2092
2093         mmf_return_val_if_fail(buffer, GST_PAD_PROBE_OK);
2094         mmf_return_val_if_fail(gst_buffer_n_memory(buffer), GST_PAD_PROBE_OK);
2095         mmf_return_val_if_fail(hcamcorder, GST_PAD_PROBE_OK);
2096
2097         sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
2098         mmf_return_val_if_fail(sc, GST_PAD_PROBE_OK);
2099
2100         if (!gst_buffer_map(buffer, &mapinfo, GST_MAP_READ)) {
2101                 MMCAM_LOG_WARNING("map failed : buffer %p", buffer);
2102                 return GST_PAD_PROBE_OK;
2103         }
2104
2105         /* call application callback */
2106         _MMCAMCORDER_LOCK_MSTREAM_CALLBACK(hcamcorder);
2107
2108         if (hcamcorder->mstream_cb) {
2109                 stream.data = (void *)mapinfo.data;
2110                 stream.length = mapinfo.size;
2111                 stream.offset = sc->muxed_stream_offset;
2112                 hcamcorder->mstream_cb(&stream, hcamcorder->mstream_cb_param);
2113         }
2114
2115         _MMCAMCORDER_UNLOCK_MSTREAM_CALLBACK(hcamcorder);
2116
2117         /* calculate current offset */
2118         sc->muxed_stream_offset += mapinfo.size;
2119
2120         gst_buffer_unmap(buffer, &mapinfo);
2121
2122         return GST_PAD_PROBE_OK;
2123 }
2124
2125
2126 GstPadProbeReturn __mmcamcorder_eventprobe_monitor(GstPad *pad, GstPadProbeInfo *info, gpointer u_data)
2127 {
2128         GstEvent *event = GST_PAD_PROBE_INFO_EVENT(info);
2129         mmf_camcorder_t *hcamcorder = NULL;
2130         _MMCamcorderSubContext *sc = NULL;
2131         GstObject *parent = NULL;
2132
2133         switch (GST_EVENT_TYPE(event)) {
2134         case GST_EVENT_UNKNOWN:
2135         /* upstream events */
2136         case GST_EVENT_QOS:
2137         case GST_EVENT_SEEK:
2138         case GST_EVENT_NAVIGATION:
2139         case GST_EVENT_LATENCY:
2140         /* downstream serialized events */
2141         case GST_EVENT_BUFFERSIZE:
2142                 MMCAM_LOG_INFO("[%s:%s] gots %s", GST_DEBUG_PAD_NAME(pad), GST_EVENT_TYPE_NAME(event));
2143                 break;
2144         case GST_EVENT_TAG:
2145                 {
2146                         GstTagList *tag_list = NULL;
2147                         _MMCamcorderReplayGain *replay_gain = NULL;
2148
2149                         MMCAM_LOG_INFO("[%s:%s] gots %s", GST_DEBUG_PAD_NAME(pad), GST_EVENT_TYPE_NAME(event));
2150
2151                         hcamcorder = MMF_CAMCORDER(u_data);
2152                         if (!hcamcorder || !hcamcorder->sub_context) {
2153                                 MMCAM_LOG_WARNING("NULL handle");
2154                                 break;
2155                         }
2156
2157                         replay_gain = &hcamcorder->sub_context->replay_gain;
2158
2159                         gst_event_parse_tag(event, &tag_list);
2160                         if (!tag_list) {
2161                                 MMCAM_LOG_WARNING("failed to get tag list");
2162                                 break;
2163                         }
2164
2165                         if (!gst_tag_list_get_double(tag_list, GST_TAG_TRACK_PEAK, &replay_gain->track_peak)) {
2166                                 MMCAM_LOG_WARNING("failed to get GST_TAG_TRACK_PEAK");
2167                                 break;
2168                         }
2169
2170                         if (!gst_tag_list_get_double(tag_list, GST_TAG_TRACK_GAIN, &replay_gain->track_gain)) {
2171                                 MMCAM_LOG_WARNING("failed to get GST_TAG_TRACK_GAIN");
2172                                 break;
2173                         }
2174
2175                         if (!gst_tag_list_get_double(tag_list, GST_TAG_ALBUM_PEAK, &replay_gain->album_peak)) {
2176                                 MMCAM_LOG_WARNING("failed to get GST_TAG_ALBUM_PEAK");
2177                                 break;
2178                         }
2179
2180                         if (!gst_tag_list_get_double(tag_list, GST_TAG_ALBUM_GAIN, &replay_gain->album_gain)) {
2181                                 MMCAM_LOG_WARNING("failed to get GST_TAG_ALBUM_PEAK");
2182                                 break;
2183                         }
2184
2185                         MMCAM_LOG_INFO("Track [peak %lf, gain %lf], Album [peak %lf, gain %lf]",
2186                                 replay_gain->track_peak, replay_gain->track_gain,
2187                                 replay_gain->album_peak, replay_gain->album_gain);
2188                 }
2189                 break;
2190         case GST_EVENT_SEGMENT:
2191                 MMCAM_LOG_INFO("[%s:%s] gots %s", GST_DEBUG_PAD_NAME(pad), GST_EVENT_TYPE_NAME(event));
2192
2193                 hcamcorder = MMF_CAMCORDER(u_data);
2194                 if (!hcamcorder) {
2195                         MMCAM_LOG_WARNING("NULL handle");
2196                         break;
2197                 }
2198
2199                 sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
2200                 if (!sc) {
2201                         MMCAM_LOG_WARNING("NULL sub context");
2202                         break;
2203                 }
2204
2205                 if (!sc->encode_element[_MMCAMCORDER_ENCSINK_SINK].gst) {
2206                         MMCAM_LOG_WARNING("no encoder sink");
2207                         break;
2208                 }
2209
2210                 parent = gst_pad_get_parent(pad);
2211                 if (!parent) {
2212                         MMCAM_LOG_WARNING("get parent failed");
2213                         break;
2214                 }
2215
2216                 if (parent == (GstObject *)sc->encode_element[_MMCAMCORDER_ENCSINK_SINK].gst) {
2217                         const GstSegment *segment;
2218                         gst_event_parse_segment(event, &segment);
2219                         if (segment->format == GST_FORMAT_BYTES) {
2220                                 MMCAM_LOG_INFO("change current offset %llu -> %"G_GUINT64_FORMAT,
2221                                         sc->muxed_stream_offset, segment->start);
2222
2223                                 sc->muxed_stream_offset = (unsigned long long)segment->start;
2224                         }
2225                 }
2226
2227                 gst_object_unref(parent);
2228                 parent = NULL;
2229                 break;
2230         case GST_EVENT_EOS:
2231                 MMCAM_LOG_WARNING("[%s:%s] gots %s", GST_DEBUG_PAD_NAME(pad), GST_EVENT_TYPE_NAME(event));
2232                 break;
2233         /* bidirectional events */
2234         case GST_EVENT_FLUSH_START:
2235         case GST_EVENT_FLUSH_STOP:
2236                 MMCAM_LOG_ERROR("[%s:%s] gots %s", GST_DEBUG_PAD_NAME(pad), GST_EVENT_TYPE_NAME(event));
2237                 break;
2238         default:
2239                 MMCAM_LOG_INFO("[%s:%s] gots %s", GST_DEBUG_PAD_NAME(pad), GST_EVENT_TYPE_NAME(event));
2240                 break;
2241         }
2242
2243         return GST_PAD_PROBE_OK;
2244 }
2245
2246
2247 int __mmcamcorder_get_amrnb_bitrate_mode(int bitrate)
2248 {
2249         int result = MM_CAMCORDER_MR475;
2250
2251         if (bitrate < 5150)
2252                 result = MM_CAMCORDER_MR475; /*AMR475*/
2253         else if (bitrate < 5900)
2254                 result = MM_CAMCORDER_MR515; /*AMR515*/
2255         else if (bitrate < 6700)
2256                 result = MM_CAMCORDER_MR59; /*AMR59*/
2257         else if (bitrate < 7400)
2258                 result = MM_CAMCORDER_MR67; /*AMR67*/
2259         else if (bitrate < 7950)
2260                 result = MM_CAMCORDER_MR74; /*AMR74*/
2261         else if (bitrate < 10200)
2262                 result = MM_CAMCORDER_MR795; /*AMR795*/
2263         else if (bitrate < 12200)
2264                 result = MM_CAMCORDER_MR102; /*AMR102*/
2265         else
2266                 result = MM_CAMCORDER_MR122; /*AMR122*/
2267
2268         return result;
2269 }
2270
2271
2272 int _mmcamcorder_get_eos_message(MMHandleType handle)
2273 {
2274         int64_t end_time;
2275         int ret = MM_ERROR_NONE;
2276
2277         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
2278         _MMCamcorderSubContext *sc = NULL;
2279
2280         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2281         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2282         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2283
2284         MMCAM_LOG_INFO("START");
2285
2286         _MMCAMCORDER_LOCK(handle);
2287
2288         if (sc->bget_eos == FALSE) {
2289                 end_time = g_get_monotonic_time() + 3 * G_TIME_SPAN_SECOND;
2290                 if (_MMCAMCORDER_WAIT_UNTIL(handle, end_time)) {
2291                         MMCAM_LOG_INFO("EOS signal received");
2292                 } else {
2293                         MMCAM_LOG_ERROR("EOS wait time out");
2294
2295                         if (hcamcorder->error_code == MM_ERROR_NONE)
2296                                 hcamcorder->error_code = MM_ERROR_CAMCORDER_RESPONSE_TIMEOUT;
2297                 }
2298         } else {
2299                 MMCAM_LOG_INFO("already got EOS");
2300         }
2301
2302         _MMCAMCORDER_UNLOCK(handle);
2303
2304         if (hcamcorder->error_code == MM_ERROR_NONE) {
2305                 if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
2306                         mmf_return_val_if_fail(sc->info_video, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2307                         if (sc->info_video->b_committing)
2308                                 _mmcamcorder_video_handle_eos((MMHandleType)hcamcorder);
2309                 } else {
2310                         mmf_return_val_if_fail(sc->info_audio, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2311                         if (sc->info_audio->b_committing)
2312                                 _mmcamcorder_audio_handle_eos((MMHandleType)hcamcorder);
2313                 }
2314         } else {
2315                 ret = hcamcorder->error_code;
2316                 MMCAM_LOG_ERROR("error 0x%x", ret);
2317         }
2318
2319         MMCAM_LOG_INFO("END");
2320
2321         return ret;
2322 }
2323
2324
2325 void _mmcamcorder_remove_element_handle(MMHandleType handle, void *element, int first_elem, int last_elem)
2326 {
2327         int i = 0;
2328         _MMCamcorderGstElement *remove_element = (_MMCamcorderGstElement *)element;
2329
2330         mmf_return_if_fail(handle && remove_element);
2331         mmf_return_if_fail((first_elem >= 0) && (last_elem > 0) && (last_elem > first_elem));
2332
2333         MMCAM_LOG_INFO("");
2334
2335         for (i = first_elem ; i <= last_elem ; i++) {
2336                 remove_element[i].gst = NULL;
2337                 remove_element[i].id = _MMCAMCORDER_NONE;
2338         }
2339
2340         return;
2341 }
2342
2343
2344 int _mmcamcorder_check_codec_fileformat_compatibility(const char *codec_type, int codec, int file_format)
2345 {
2346         mmf_return_val_if_fail(codec_type, MM_ERROR_CAMCORDER_INVALID_ARGUMENT);
2347
2348         /* Check compatibility between codec and file format */
2349         if (!strcmp(codec_type, MMCAM_AUDIO_ENCODER)) {
2350                 if (codec > MM_AUDIO_CODEC_INVALID && codec < MM_AUDIO_CODEC_NUM &&
2351                         file_format > MM_FILE_FORMAT_INVALID && file_format < MM_FILE_FORMAT_NUM) {
2352                         if (audiocodec_fileformat_compatibility_table[codec][file_format] == 0) {
2353                                 MMCAM_LOG_ERROR("Audio codec[%d] and file format[%d] compatibility FAILED.", codec, file_format);
2354                                 return MM_ERROR_CAMCORDER_ENCODER_WRONG_TYPE;
2355                         }
2356
2357                         MMCAM_LOG_INFO("Audio codec[%d] and file format[%d] compatibility SUCCESS.", codec, file_format);
2358                 } else {
2359                         MMCAM_LOG_ERROR("Audio codec[%d] or file format[%d] is INVALID.", codec, file_format);
2360                         return MM_ERROR_CAMCORDER_ENCODER_WRONG_TYPE;
2361                 }
2362         } else if (!strcmp(codec_type, MMCAM_VIDEO_ENCODER)) {
2363                 if (codec > MM_VIDEO_CODEC_INVALID && codec < MM_VIDEO_CODEC_NUM &&
2364                         file_format > MM_FILE_FORMAT_INVALID && file_format < MM_FILE_FORMAT_NUM) {
2365                         if (videocodec_fileformat_compatibility_table[codec][file_format] == 0) {
2366                                 MMCAM_LOG_ERROR("Video codec[%d] and file format[%d] compatibility FAILED.", codec, file_format);
2367                                 return MM_ERROR_CAMCORDER_ENCODER_WRONG_TYPE;
2368                         }
2369
2370                         MMCAM_LOG_INFO("Video codec[%d] and file format[%d] compatibility SUCCESS.", codec, file_format);
2371                 } else {
2372                         MMCAM_LOG_ERROR("Video codec[%d] or file format[%d] is INVALID.", codec, file_format);
2373                         return MM_ERROR_CAMCORDER_ENCODER_WRONG_TYPE;
2374                 }
2375         }
2376
2377         return MM_ERROR_NONE;
2378 }
2379
2380
2381 int _mmcamcorder_check_audiocodec_fileformat_compatibility(MMHandleType handle)
2382 {
2383         int err = MM_ERROR_NONE;
2384         int audio_codec = MM_AUDIO_CODEC_INVALID;
2385         int file_format = MM_FILE_FORMAT_INVALID;
2386
2387         char *err_name = NULL;
2388
2389         err = mm_camcorder_get_attributes(handle, &err_name,
2390                 MMCAM_AUDIO_ENCODER, &audio_codec,
2391                 MMCAM_FILE_FORMAT, &file_format,
2392                 NULL);
2393         if (err != MM_ERROR_NONE) {
2394                 MMCAM_LOG_WARNING("Get attrs fail. (%s:%x)", err_name, err);
2395                 SAFE_FREE(err_name);
2396                 return err;
2397         }
2398
2399         /* Check compatibility between audio codec and file format */
2400         err = _mmcamcorder_check_codec_fileformat_compatibility(MMCAM_AUDIO_ENCODER, audio_codec, file_format);
2401
2402         return err;
2403 }
2404
2405
2406 int _mmcamcorder_check_videocodec_fileformat_compatibility(MMHandleType handle)
2407 {
2408         int err = MM_ERROR_NONE;
2409         int video_codec = MM_VIDEO_CODEC_INVALID;
2410         int file_format = MM_FILE_FORMAT_INVALID;
2411
2412         char *err_name = NULL;
2413
2414         err = mm_camcorder_get_attributes(handle, &err_name,
2415                 MMCAM_VIDEO_ENCODER, &video_codec,
2416                 MMCAM_FILE_FORMAT, &file_format,
2417                 NULL);
2418         if (err != MM_ERROR_NONE) {
2419                 MMCAM_LOG_WARNING("Get attrs fail. (%s:%x)", err_name, err);
2420                 SAFE_FREE(err_name);
2421                 return err;
2422         }
2423
2424         /* Check compatibility between video codec and file format */
2425         err = _mmcamcorder_check_codec_fileformat_compatibility(MMCAM_VIDEO_ENCODER, video_codec, file_format);
2426
2427         return err;
2428 }
2429
2430
2431 bool _mmcamcorder_set_display_rotation(MMHandleType handle, int display_rotate, int videosink_index)
2432 {
2433         const char* videosink_name = NULL;
2434
2435         mmf_camcorder_t *hcamcorder = NULL;
2436         _MMCamcorderSubContext *sc = NULL;
2437
2438         hcamcorder = MMF_CAMCORDER(handle);
2439         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2440
2441         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2442         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2443         mmf_return_val_if_fail(sc->element, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2444
2445         if (sc->element[videosink_index].gst) {
2446                 /* Get videosink name */
2447                 _mmcamcorder_conf_get_value_element_name(sc->VideosinkElement, &videosink_name);
2448                 if (videosink_name == NULL) {
2449                         MMCAM_LOG_ERROR("Please check videosink element in configuration file");
2450                         return FALSE;
2451                 }
2452
2453                 if (!strcmp(videosink_name, "tizenwlsink") || !strcmp(videosink_name, "xvimagesink") ||
2454                         !strcmp(videosink_name, "evasimagesink") || !strcmp(videosink_name, "evaspixmapsink") ||
2455                         !strcmp(videosink_name, "directvideosink")) {
2456                         MMCAMCORDER_G_OBJECT_SET(sc->element[videosink_index].gst, "rotate", display_rotate);
2457                         MMCAM_LOG_INFO("Set display-rotate [%d] done.", display_rotate);
2458                 } else {
2459                         MMCAM_LOG_WARNING("[%s] does not support DISPLAY_ROTATION, but no error", videosink_name);
2460                 }
2461
2462                 return TRUE;
2463         } else {
2464                 MMCAM_LOG_ERROR("Videosink element is null");
2465                 return FALSE;
2466         }
2467 }
2468
2469
2470 bool _mmcamcorder_set_display_flip(MMHandleType handle, int display_flip, int videosink_index)
2471 {
2472         const char* videosink_name = NULL;
2473
2474         mmf_camcorder_t *hcamcorder = NULL;
2475         _MMCamcorderSubContext *sc = NULL;
2476
2477         hcamcorder = MMF_CAMCORDER(handle);
2478         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2479
2480         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2481         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2482         mmf_return_val_if_fail(sc->element, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2483
2484         if (sc->element[videosink_index].gst) {
2485                 /* Get videosink name */
2486                 _mmcamcorder_conf_get_value_element_name(sc->VideosinkElement, &videosink_name);
2487                 if (videosink_name == NULL) {
2488                         MMCAM_LOG_ERROR("Please check videosink element in configuration file");
2489                         return FALSE;
2490                 }
2491
2492                 if (!strcmp(videosink_name, "tizenwlsink") || !strcmp(videosink_name, "xvimagesink") ||
2493                         !strcmp(videosink_name, "evasimagesink") || !strcmp(videosink_name, "evaspixmapsink") ||
2494                         !strcmp(videosink_name, "directvideosink")) {
2495                         MMCAMCORDER_G_OBJECT_SET(sc->element[videosink_index].gst, "flip", display_flip);
2496                         MMCAM_LOG_INFO("Set display flip [%d] done.", display_flip);
2497                 } else {
2498                         MMCAM_LOG_WARNING("[%s] does not support DISPLAY_FLIP, but no error", videosink_name);
2499                 }
2500
2501                 return TRUE;
2502         } else {
2503                 MMCAM_LOG_ERROR("Videosink element is null");
2504                 return FALSE;
2505         }
2506 }
2507
2508
2509 bool _mmcamcorder_set_videosrc_rotation(MMHandleType handle, int videosrc_rotate)
2510 {
2511         int fps = 0;
2512         _MMCamcorderSubContext *sc = NULL;
2513
2514         mmf_camcorder_t *hcamcorder = NULL;
2515
2516         hcamcorder = MMF_CAMCORDER(handle);
2517         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2518
2519         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2520         if (!sc) {
2521                 MMCAM_LOG_INFO("sub context is not initialized");
2522                 return TRUE;
2523         }
2524
2525         mm_camcorder_get_attributes(handle, NULL,
2526                 MMCAM_CAMERA_WIDTH, &sc->info_video->preview_width,
2527                 MMCAM_CAMERA_HEIGHT, &sc->info_video->preview_height,
2528                 MMCAM_CAMERA_FPS, &fps,
2529                 NULL);
2530
2531         MMCAM_LOG_INFO("set rotate %d", videosrc_rotate);
2532
2533         return _mmcamcorder_set_videosrc_caps(handle, sc->fourcc,
2534                 sc->info_video->preview_width, sc->info_video->preview_height,
2535                 fps, videosrc_rotate);
2536 }
2537
2538
2539 bool _mmcamcorder_set_videosrc_caps(MMHandleType handle, unsigned int fourcc, int width, int height, int fps, int rotate)
2540 {
2541         int set_width = 0;
2542         int set_height = 0;
2543         int set_rotate = 0;
2544         int fps_auto = 0;
2545         unsigned int caps_fourcc = 0;
2546         gboolean do_set_caps = FALSE;
2547         char fourcc_string[sizeof(fourcc) + 1];
2548         gchar *caps_str = NULL;
2549 #ifdef _MMCAMCORDER_PRODUCT_TV
2550         gint maxwidth = 0;
2551         gint maxheight = 0;
2552         int display_surface_type = MM_DISPLAY_SURFACE_NULL;
2553 #endif /* _MMCAMCORDER_PRODUCT_TV */
2554
2555         GstCaps *caps = NULL;
2556
2557         mmf_camcorder_t *hcamcorder = NULL;
2558         _MMCamcorderSubContext *sc = NULL;
2559
2560         hcamcorder = MMF_CAMCORDER(handle);
2561         mmf_return_val_if_fail(hcamcorder, FALSE);
2562
2563         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2564
2565         if (!sc || !(sc->element)) {
2566                 MMCAM_LOG_INFO("sub context is not initialized");
2567                 return TRUE;
2568         }
2569
2570         if (!sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst) {
2571                 MMCAM_LOG_ERROR("Video src is NULL!");
2572                 return FALSE;
2573         }
2574
2575         if (!sc->element[_MMCAMCORDER_VIDEOSRC_FILT].gst) {
2576                 MMCAM_LOG_ERROR("Video filter is NULL!");
2577                 return FALSE;
2578         }
2579
2580         if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
2581                 int capture_width = 0;
2582                 int capture_height = 0;
2583                 double motion_rate = _MMCAMCORDER_DEFAULT_RECORDING_MOTION_RATE;
2584
2585                 mm_camcorder_get_attributes(handle, NULL,
2586                         MMCAM_CAMERA_RECORDING_MOTION_RATE, &motion_rate,
2587                         MMCAM_CAMERA_FPS_AUTO, &fps_auto,
2588                         MMCAM_CAPTURE_WIDTH, &capture_width,
2589                         MMCAM_CAPTURE_HEIGHT, &capture_height,
2590                         MMCAM_VIDEO_WIDTH, &sc->info_video->video_width,
2591                         MMCAM_VIDEO_HEIGHT, &sc->info_video->video_height,
2592                         NULL);
2593                 MMCAM_LOG_INFO("motion rate %f, capture size %dx%d, fps auto %d, video size %dx%d",
2594                         motion_rate, capture_width, capture_height, fps_auto,
2595                         sc->info_video->video_width, sc->info_video->video_height);
2596
2597                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst,
2598                         "high-speed-fps", (motion_rate != _MMCAMCORDER_DEFAULT_RECORDING_MOTION_RATE ? fps : 0));
2599
2600                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "capture-width", capture_width);
2601                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "capture-height", capture_height);
2602                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "fps-auto", fps_auto);
2603
2604                 /* set fps */
2605                 sc->info_video->fps = fps;
2606         }
2607
2608         /* Interleaved format does not support rotation */
2609         if (sc->info_image->preview_format != MM_PIXEL_FORMAT_ITLV_JPEG_UYVY) {
2610                 /* store videosrc rotation */
2611                 sc->videosrc_rotate = rotate;
2612
2613                 /* Define width, height and rotate in caps */
2614
2615                 /* This will be applied when rotate is 0, 90, 180, 270 if rear camera.
2616                 This will be applied when rotate is 0, 180 if front camera. */
2617                 set_rotate = rotate * 90;
2618
2619                 if (rotate == MM_VIDEO_INPUT_ROTATION_90 ||
2620                     rotate == MM_VIDEO_INPUT_ROTATION_270) {
2621                         set_width = height;
2622                         set_height = width;
2623                         if (hcamcorder->device_type == MM_VIDEO_DEVICE_CAMERA1) {
2624                                 if (rotate == MM_VIDEO_INPUT_ROTATION_90)
2625                                         set_rotate = 270;
2626                                 else
2627                                         set_rotate = 90;
2628                         }
2629                 } else {
2630                         set_width = width;
2631                         set_height = height;
2632                 }
2633         } else {
2634                 sc->videosrc_rotate = MM_VIDEO_INPUT_ROTATION_NONE;
2635                 set_rotate = 0;
2636                 set_width = width;
2637                 set_height = height;
2638
2639                 MMCAM_LOG_WARNING("ITLV format doe snot support INPUT ROTATE. Ignore ROTATE[%d]", rotate);
2640         }
2641
2642         MMCAMCORDER_G_OBJECT_GET(sc->element[_MMCAMCORDER_VIDEOSRC_FILT].gst, "caps", &caps);
2643         if (caps && !gst_caps_is_any(caps)) {
2644                 GstStructure *structure = NULL;
2645
2646                 structure = gst_caps_get_structure(caps, 0);
2647                 if (structure) {
2648                         const gchar *format_string = NULL;
2649                         int caps_width = 0;
2650                         int caps_height = 0;
2651                         int caps_fps = 0;
2652                         int caps_rotate = 0;
2653
2654                         format_string = gst_structure_get_string(structure, "format");
2655                         if (format_string)
2656                                 caps_fourcc = _mmcamcorder_convert_fourcc_string_to_value(format_string);
2657
2658                         gst_structure_get(structure,
2659                                 "width", G_TYPE_INT, &caps_width,
2660                                 "height", G_TYPE_INT, &caps_height,
2661                                 "fps", G_TYPE_INT, &caps_fps,
2662                                 "rotate", G_TYPE_INT, &caps_rotate,
2663                                 NULL);
2664
2665 #ifdef _MMCAMCORDER_PRODUCT_TV
2666                         if (_mmcamcorder_is_encoded_preview_pixel_format(sc->info_image->preview_format)) {
2667                                 if (set_width == caps_width && set_height == caps_height &&
2668                                         set_rotate == caps_rotate && fps == caps_fps) {
2669                                         MMCAM_LOG_INFO("No need to replace caps.");
2670                                 } else {
2671                                         MMCAM_LOG_INFO("current [%c%c%c%c %dx%d, fps %d, rot %d], new [%c%c%c%c %dx%d, fps %d, rot %d]",
2672                                                 caps_fourcc, caps_fourcc>>8, caps_fourcc>>16, caps_fourcc>>24,
2673                                                 caps_width, caps_height, caps_fps, caps_rotate,
2674                                                 fourcc, fourcc>>8, fourcc>>16, fourcc>>24,
2675                                                 set_width, set_height, fps, set_rotate);
2676                                         do_set_caps = TRUE;
2677                                 }
2678                         } else {
2679                                 if (set_width == caps_width && set_height == caps_height &&
2680                                     fourcc == caps_fourcc && set_rotate == caps_rotate && fps == caps_fps) {
2681                                         MMCAM_LOG_INFO("No need to replace caps.");
2682                                 } else {
2683                                         MMCAM_LOG_INFO("current [%c%c%c%c %dx%d, fps %d, rot %d], new [%c%c%c%c %dx%d, fps %d, rot %d]",
2684                                                 caps_fourcc, caps_fourcc>>8, caps_fourcc>>16, caps_fourcc>>24,
2685                                                 caps_width, caps_height, caps_fps, caps_rotate,
2686                                                 fourcc, fourcc>>8, fourcc>>16, fourcc>>24,
2687                                                 set_width, set_height, fps, set_rotate);
2688                                         do_set_caps = TRUE;
2689                                 }
2690                         }
2691 #else /*_MMCAMCORDER_PRODUCT_TV */
2692                         if (set_width == caps_width && set_height == caps_height &&
2693                             fourcc == caps_fourcc && set_rotate == caps_rotate && fps == caps_fps) {
2694                                 MMCAM_LOG_INFO("No need to replace caps.");
2695                         } else {
2696                                 MMCAM_LOG_INFO("current [%c%c%c%c %dx%d, fps %d, rot %d], new [%c%c%c%c %dx%d, fps %d, rot %d]",
2697                                         caps_fourcc, caps_fourcc>>8, caps_fourcc>>16, caps_fourcc>>24,
2698                                         caps_width, caps_height, caps_fps, caps_rotate,
2699                                         fourcc, fourcc>>8, fourcc>>16, fourcc>>24,
2700                                         set_width, set_height, fps, set_rotate);
2701                                 do_set_caps = TRUE;
2702                         }
2703 #endif /*_MMCAMCORDER_PRODUCT_TV */
2704                 } else {
2705                         MMCAM_LOG_INFO("can not get structure of caps. set new one...");
2706                         do_set_caps = TRUE;
2707                 }
2708         } else {
2709                 MMCAM_LOG_INFO("No caps. set new one...");
2710                 do_set_caps = TRUE;
2711         }
2712
2713         if (caps) {
2714                 gst_caps_unref(caps);
2715                 caps = NULL;
2716         }
2717
2718         if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
2719                 /* assume that it's camera capture mode */
2720                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSINK_SINK].gst, "csc-range", 1);
2721         }
2722
2723         if (!do_set_caps)
2724                 return TRUE;
2725
2726         switch (sc->info_image->preview_format) {
2727         case MM_PIXEL_FORMAT_ENCODED_H264:
2728 #ifdef _MMCAMCORDER_PRODUCT_TV
2729                 mm_camcorder_get_attributes(handle, NULL,
2730                         MMCAM_DISPLAY_SURFACE, &display_surface_type,
2731                         NULL);
2732                 if (display_surface_type != MM_DISPLAY_SURFACE_NULL &&
2733                         __mmcamcorder_find_max_resolution(handle, &maxwidth, &maxheight) == false) {
2734                         MMCAM_LOG_ERROR("can not find max resolution limitation");
2735                         return false;
2736                 } else if (display_surface_type == MM_DISPLAY_SURFACE_NULL) {
2737                         maxwidth = set_width;
2738                         maxheight = set_height;
2739                 }
2740 #endif /* _MMCAMCORDER_PRODUCT_TV */
2741                 caps = gst_caps_new_simple("video/x-h264",
2742                         "width", G_TYPE_INT, set_width,
2743                         "height", G_TYPE_INT, set_height,
2744                         "framerate", GST_TYPE_FRACTION, fps, 1,
2745                         "stream-format", G_TYPE_STRING, "byte-stream",
2746 #ifdef _MMCAMCORDER_PRODUCT_TV
2747                         "maxwidth", G_TYPE_INT, maxwidth,
2748                         "maxheight", G_TYPE_INT, maxheight,
2749                         "alignment", G_TYPE_STRING, "au",
2750 #endif /* _MMCAMCORDER_PRODUCT_TV */
2751                         NULL);
2752                 break;
2753         case MM_PIXEL_FORMAT_ENCODED_MJPEG:
2754 #ifdef _MMCAMCORDER_PRODUCT_TV
2755                 caps = gst_caps_new_simple("video/x-jpeg",
2756 #else
2757                 caps = gst_caps_new_simple("image/jpeg",
2758 #endif
2759                         "width", G_TYPE_INT, set_width,
2760                         "height", G_TYPE_INT, set_height,
2761                         "framerate", GST_TYPE_FRACTION, fps, 1,
2762                         NULL);
2763                 break;
2764         case MM_PIXEL_FORMAT_ENCODED_VP8:
2765                 caps = gst_caps_new_simple("video/x-vp8",
2766                         "width", G_TYPE_INT, set_width,
2767                         "height", G_TYPE_INT, set_height,
2768                         "framerate", GST_TYPE_FRACTION, fps, 1,
2769                         NULL);
2770                 break;
2771         case MM_PIXEL_FORMAT_ENCODED_VP9:
2772                 caps = gst_caps_new_simple("video/x-vp9",
2773                         "width", G_TYPE_INT, set_width,
2774                         "height", G_TYPE_INT, set_height,
2775                         "framerate", GST_TYPE_FRACTION, fps, 1,
2776                         NULL);
2777                 break;
2778                 break;
2779         default:
2780                 memcpy(fourcc_string, (char*)&fourcc, sizeof(fourcc));
2781                 fourcc_string[sizeof(fourcc)] = '\0';
2782                 caps = gst_caps_new_simple("video/x-raw",
2783                         "format", G_TYPE_STRING, fourcc_string,
2784                         "width", G_TYPE_INT, set_width,
2785                         "height", G_TYPE_INT, set_height,
2786                         "framerate", GST_TYPE_FRACTION, fps, 1,
2787                         "rotate", G_TYPE_INT, set_rotate,
2788                         NULL);
2789                 break;
2790         }
2791
2792         if (!caps) {
2793                 MMCAM_LOG_ERROR("There are no caps");
2794                 return FALSE;
2795         }
2796
2797         caps_str = gst_caps_to_string(caps);
2798         MMCAM_LOG_INFO("vidoesrc new caps set [%s]", caps_str);
2799         g_free(caps_str);
2800
2801         MMCAMCORDER_G_OBJECT_SET_POINTER(sc->element[_MMCAMCORDER_VIDEOSRC_FILT].gst, "caps", caps);
2802         gst_caps_unref(caps);
2803
2804         return TRUE;
2805 }
2806
2807
2808 bool _mmcamcorder_set_videosrc_flip(MMHandleType handle, int videosrc_flip)
2809 {
2810         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
2811         _MMCamcorderSubContext *sc = NULL;
2812
2813         mmf_return_val_if_fail(hcamcorder, FALSE);
2814
2815         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2816         if (!sc)
2817                 return TRUE;
2818
2819         MMCAM_LOG_INFO("Set FLIP %d", videosrc_flip);
2820
2821         if (sc->element && sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst) {
2822                 int hflip = 0;
2823                 int vflip = 0;
2824
2825                 /* Interleaved format does not support FLIP */
2826                 if (sc->info_image->preview_format != MM_PIXEL_FORMAT_ITLV_JPEG_UYVY) {
2827                         hflip = (videosrc_flip & MM_FLIP_HORIZONTAL) == MM_FLIP_HORIZONTAL;
2828                         vflip = (videosrc_flip & MM_FLIP_VERTICAL) == MM_FLIP_VERTICAL;
2829
2830                         MMCAM_LOG_INFO("videosrc flip H:%d, V:%d", hflip, vflip);
2831
2832                         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "hflip", hflip);
2833                         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "vflip", vflip);
2834                 } else {
2835                         MMCAM_LOG_WARNING("ITLV format does not support FLIP. Ignore FLIP[%d]",
2836                                 videosrc_flip);
2837                 }
2838         } else {
2839                 MMCAM_LOG_WARNING("element is NULL");
2840                 return FALSE;
2841         }
2842
2843         return TRUE;
2844 }
2845
2846
2847 bool _mmcamcorder_set_videosrc_anti_shake(MMHandleType handle, int anti_shake)
2848 {
2849         GstCameraControl *control = NULL;
2850         _MMCamcorderSubContext *sc = NULL;
2851         GstElement *v_src = NULL;
2852
2853         int set_value = 0;
2854
2855         mmf_return_val_if_fail(handle, FALSE);
2856
2857         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2858         if (!sc)
2859                 return TRUE;
2860
2861         v_src = sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst;
2862
2863         if (!v_src) {
2864                 MMCAM_LOG_WARNING("videosrc element is NULL");
2865                 return FALSE;
2866         }
2867
2868         set_value = _mmcamcorder_convert_msl_to_sensor(handle, MM_CAM_CAMERA_ANTI_HANDSHAKE, anti_shake);
2869
2870         /* set anti-shake with camera control */
2871         if (!GST_IS_CAMERA_CONTROL(v_src)) {
2872                 MMCAM_LOG_WARNING("Can't cast Video source into camera control.");
2873                 return FALSE;
2874         }
2875
2876         control = GST_CAMERA_CONTROL(v_src);
2877         if (gst_camera_control_set_ahs(control, set_value)) {
2878                 MMCAM_LOG_INFO("Succeed in operating anti-handshake. value[%d]", set_value);
2879                 return TRUE;
2880         } else {
2881                 MMCAM_LOG_WARNING("Failed to operate anti-handshake. value[%d]", set_value);
2882         }
2883
2884         return FALSE;
2885 }
2886
2887
2888 bool _mmcamcorder_set_videosrc_stabilization(MMHandleType handle, int stabilization)
2889 {
2890         _MMCamcorderSubContext *sc = NULL;
2891         GstElement *v_src = NULL;
2892
2893         mmf_return_val_if_fail(handle, FALSE);
2894
2895         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2896         if (!sc)
2897                 return TRUE;
2898
2899         v_src = sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst;
2900         if (!v_src) {
2901                 MMCAM_LOG_WARNING("videosrc element is NULL");
2902                 return FALSE;
2903         }
2904
2905         /* check property of videosrc element - support VDIS */
2906         if (g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(v_src)), "enable-vdis-mode")) {
2907                 int video_width = 0;
2908                 int video_height = 0;
2909
2910                 if (stabilization == MM_CAMCORDER_VIDEO_STABILIZATION_ON) {
2911                         MMCAM_LOG_INFO("ENABLE video stabilization");
2912
2913                         /* VDIS mode only supports NV12 and [720p or 1080p or 1088 * 1088] */
2914                         mm_camcorder_get_attributes(handle, NULL,
2915                                 MMCAM_VIDEO_WIDTH, &video_width,
2916                                 MMCAM_VIDEO_HEIGHT, &video_height,
2917                                 NULL);
2918
2919                         if (sc->info_image->preview_format == MM_PIXEL_FORMAT_NV12 && video_width >= 1080 && video_height >= 720) {
2920                                 MMCAM_LOG_INFO("NV12, video size %dx%d, ENABLE video stabilization",
2921                                         video_width, video_height);
2922                                 /* set vdis mode */
2923                                 g_object_set(G_OBJECT(v_src),
2924                                                          "enable-vdis-mode", TRUE,
2925                                                          NULL);
2926                         } else {
2927                                 MMCAM_LOG_WARNING("invalid preview format %c%c%c%c or video size %dx%d",
2928                                                                 sc->fourcc, sc->fourcc>>8, sc->fourcc>>16, sc->fourcc>>24,
2929                                                                 video_width, video_height);
2930                                 return FALSE;
2931                         }
2932                 } else {
2933                         /* set vdis mode */
2934                         g_object_set(G_OBJECT(v_src),
2935                                                 "enable-vdis-mode", FALSE,
2936                                                 NULL);
2937
2938                         MMCAM_LOG_INFO("DISABLE video stabilization");
2939                 }
2940         } else if (stabilization == MM_CAMCORDER_VIDEO_STABILIZATION_ON) {
2941                 MMCAM_LOG_ERROR("no property for video stabilization, so can not set ON");
2942                 return FALSE;
2943         } else {
2944                 MMCAM_LOG_WARNING("no property for video stabilization");
2945         }
2946
2947         return TRUE;
2948 }
2949
2950
2951 bool _mmcamcorder_set_camera_resolution(MMHandleType handle, int width, int height)
2952 {
2953         int fps = 0;
2954
2955         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
2956         _MMCamcorderSubContext *sc = NULL;
2957
2958         mmf_return_val_if_fail(hcamcorder, FALSE);
2959
2960         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2961         if (!sc)
2962                 return TRUE;
2963
2964         mm_camcorder_get_attributes(handle, NULL,
2965                 MMCAM_CAMERA_FPS, &fps,
2966                 NULL);
2967
2968         MMCAM_LOG_INFO("set %dx%d", width, height);
2969
2970         return _mmcamcorder_set_videosrc_caps(handle, sc->fourcc, width, height, fps, sc->videosrc_rotate);
2971 }
2972
2973
2974 bool _mmcamcorder_set_encoded_preview_bitrate(MMHandleType handle, int bitrate)
2975 {
2976         _MMCamcorderSubContext *sc = NULL;
2977
2978         if ((void *)handle == NULL) {
2979                 MMCAM_LOG_WARNING("handle is NULL");
2980                 return FALSE;
2981         }
2982
2983         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2984         if (!sc) {
2985                 MMCAM_LOG_WARNING("subcontext is NULL");
2986                 return FALSE;
2987         }
2988
2989         if (sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst == NULL) {
2990                 MMCAM_LOG_WARNING("videosrc plugin is NULL");
2991                 return FALSE;
2992         }
2993
2994         MMCAM_LOG_INFO("set encoded preview bitrate : %d bps", bitrate);
2995
2996         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "bitrate", bitrate);
2997
2998         return TRUE;
2999 }
3000
3001
3002 bool _mmcamcorder_set_encoded_preview_gop_interval(MMHandleType handle, int gop_interval)
3003 {
3004         _MMCamcorderSubContext *sc = NULL;
3005
3006         if ((void *)handle == NULL) {
3007                 MMCAM_LOG_WARNING("handle is NULL");
3008                 return FALSE;
3009         }
3010
3011         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
3012         if (!sc) {
3013                 MMCAM_LOG_WARNING("subcontext is NULL");
3014                 return FALSE;
3015         }
3016
3017         if (sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst == NULL) {
3018                 MMCAM_LOG_WARNING("videosrc plugin is NULL");
3019                 return FALSE;
3020         }
3021
3022         MMCAM_LOG_INFO("set encoded preview GOP interval : %d ms", gop_interval);
3023
3024         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "gop-interval", gop_interval);
3025
3026         return TRUE;
3027 }
3028
3029
3030 bool _mmcamcorder_set_sound_stream_info(GstElement *element, char *stream_type, int stream_index)
3031 {
3032         GstStructure *props = NULL;
3033         char stream_props[64] = {'\0',};
3034
3035         if (element == NULL || stream_type == NULL || stream_index < 0) {
3036                 MMCAM_LOG_ERROR("invalid argument %p %p %d", element, stream_type, stream_index);
3037                 return FALSE;
3038         }
3039
3040         snprintf(stream_props, sizeof(stream_props) - 1,
3041                 "props,media.role=%s, media.parent_id=%d",
3042                 stream_type, stream_index);
3043
3044         MMCAM_LOG_WARNING("stream type %s, index %d -> [%s]", stream_type, stream_index, stream_props);
3045
3046         props = gst_structure_from_string(stream_props, NULL);
3047         if (!props) {
3048                 MMCAM_LOG_ERROR("failed to create GstStructure");
3049                 return FALSE;
3050         }
3051
3052         MMCAMCORDER_G_OBJECT_SET_POINTER(element, "stream-properties", props);
3053
3054         gst_structure_free(props);
3055         props = NULL;
3056
3057         return TRUE;
3058 }
3059
3060
3061 bool _mmcamcorder_recreate_decoder_for_encoded_preview(MMHandleType handle)
3062 {
3063         int ret = MM_ERROR_NONE;
3064         _MMCamcorderSubContext *sc = NULL;
3065         mmf_camcorder_t *hcamcorder = NULL;
3066         const char *videodecoder_name = NULL;
3067         char videodecoder_name_final[32] = {'\0',};
3068         int display_surface_type = MM_DISPLAY_SURFACE_NULL;
3069 #ifdef _MMCAMCORDER_RM_SUPPORT
3070         int decoder_index = 0;
3071 #endif /* _MMCAMCORDER_RM_SUPPORT */
3072
3073         if ((void *)handle == NULL) {
3074                 MMCAM_LOG_WARNING("handle is NULL");
3075                 return FALSE;
3076         }
3077
3078         hcamcorder = MMF_CAMCORDER(handle);
3079
3080         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
3081         if (!sc) {
3082                 MMCAM_LOG_WARNING("subcontext is NULL");
3083                 return FALSE;
3084         }
3085
3086         if (_mmcamcorder_is_encoded_preview_pixel_format(sc->info_image->preview_format) == false ||
3087                 hcamcorder->recreate_decoder == FALSE) {
3088                 MMCAM_LOG_INFO("skip this fuction - format %d, recreate decoder %d",
3089                         sc->info_image->preview_format, hcamcorder->recreate_decoder);
3090                 return TRUE;
3091         }
3092
3093         mm_camcorder_get_attributes(handle, NULL,
3094                 MMCAM_DISPLAY_SURFACE, &display_surface_type,
3095                 NULL);
3096
3097         if (display_surface_type == MM_DISPLAY_SURFACE_NULL) {
3098                 MMCAM_LOG_INFO("no need to control decoder element");
3099                 return TRUE;
3100         }
3101
3102         if (sc->element[_MMCAMCORDER_MAIN_PIPE].gst == NULL ||
3103             sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst == NULL) {
3104                 MMCAM_LOG_WARNING("main pipeline or decoder plugin is NULL");
3105                 return FALSE;
3106         }
3107
3108         MMCAM_LOG_INFO("start");
3109
3110         _mmcamcorder_conf_get_value_element_name(sc->VideodecoderElement, &videodecoder_name);
3111         if (videodecoder_name == NULL) {
3112                 MMCAM_LOG_ERROR("failed to get decoder element name from %p", sc->VideodecoderElement);
3113                 return FALSE;
3114         }
3115
3116         /* set state as NULL */
3117         ret = _mmcamcorder_gst_set_state(handle, sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst, GST_STATE_NULL);
3118         if (ret != MM_ERROR_NONE) {
3119                 MMCAM_LOG_ERROR("failed to set NULL to decoder");
3120                 return FALSE;
3121         }
3122
3123         /* remove decoder - pads will be unlinked automatically in remove function */
3124         if (!gst_bin_remove(GST_BIN(sc->element[_MMCAMCORDER_MAIN_PIPE].gst),
3125                             sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst)) {
3126                 MMCAM_LOG_ERROR("failed to remove decoder from pipeline");
3127                 return FALSE;
3128         }
3129
3130         /* check decoder element */
3131         if (sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst) {
3132                 MMCAM_LOG_INFO("decoder[%p] is still alive - ref count %d",
3133                         G_OBJECT(sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst),
3134                         ((GObject *)sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst)->ref_count);
3135         }
3136
3137 #ifdef _MMCAMCORDER_RM_SUPPORT
3138         if (sc->info_image->preview_format == MM_PIXEL_FORMAT_ENCODED_H264) {
3139                 if (hcamcorder->request_resources.category_id[0] == RM_CATEGORY_VIDEO_DECODER_SUB)
3140                         decoder_index = 1;
3141
3142                 snprintf(videodecoder_name_final, sizeof(videodecoder_name_final), "%s%d", videodecoder_name, decoder_index);
3143         } else if (sc->info_image->preview_format == MM_PIXEL_FORMAT_ENCODED_MJPEG) {
3144                 /* MJPEG */
3145                 if (hcamcorder->request_resources.category_id[0] == RM_CATEGORY_VIDEO_DECODER)
3146                         snprintf(videodecoder_name_final, sizeof(videodecoder_name_final), "%s", "omx_uhd_mjpegdec");
3147                 else
3148                         snprintf(videodecoder_name_final, sizeof(videodecoder_name_final), "%s", "omx_mjpegdec");
3149         } else {
3150 #endif /* _MMCAMCORDER_RM_SUPPORT */
3151                 snprintf(videodecoder_name_final, sizeof(videodecoder_name_final), "%s", videodecoder_name);
3152 #ifdef _MMCAMCORDER_RM_SUPPORT
3153         }
3154 #endif /* _MMCAMCORDER_RM_SUPPORT */
3155
3156         /* create decoder */
3157         sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst = gst_element_factory_make(videodecoder_name_final, "videosrc_decode");
3158         if (sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst == NULL) {
3159                 MMCAM_LOG_ERROR("Decoder[%s] creation fail", videodecoder_name_final);
3160                 return FALSE;
3161         }
3162
3163         _mmcamcorder_conf_set_value_element_property(sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst, sc->VideodecoderElement);
3164
3165         sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].id = _MMCAMCORDER_VIDEOSRC_DECODE;
3166         g_object_weak_ref(G_OBJECT(sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst),
3167                 (GWeakNotify)_mmcamcorder_element_release_noti, sc);
3168
3169         /* add to pipeline */
3170         if (!gst_bin_add(GST_BIN(sc->element[_MMCAMCORDER_MAIN_PIPE].gst),
3171                 sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst)) {
3172                 MMCAM_LOG_ERROR("failed to add decoder to pipeline");
3173                 gst_object_unref(sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst);
3174                 return FALSE;
3175         }
3176
3177         /* link */
3178         if (_MM_GST_ELEMENT_LINK(GST_ELEMENT(sc->element[_MMCAMCORDER_VIDEOSRC_QUE].gst),
3179                 GST_ELEMENT(sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst))) {
3180                 MMCAM_LOG_INFO("Link videosrc_queue to decoder OK");
3181         } else {
3182                 MMCAM_LOG_ERROR("Link videosrc_queue to decoder FAILED");
3183                 return FALSE;
3184         }
3185
3186         if (_MM_GST_ELEMENT_LINK(GST_ELEMENT(sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst),
3187                 GST_ELEMENT(sc->element[_MMCAMCORDER_VIDEOSINK_QUE].gst))) {
3188                 MMCAM_LOG_INFO("Link decoder to videosink_queue OK");
3189         } else {
3190                 MMCAM_LOG_ERROR("Link decoder to videosink_queue FAILED");
3191                 return FALSE;
3192         }
3193
3194         /* set state READY */
3195         ret = _mmcamcorder_gst_set_state(handle, sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst, GST_STATE_READY);
3196         if (ret != MM_ERROR_NONE) {
3197                 MMCAM_LOG_ERROR("failed to set READY to decoder");
3198                 return FALSE;
3199         }
3200
3201         MMCAM_LOG_INFO("done");
3202
3203         return TRUE;
3204 }
3205
3206 #ifdef _MMCAMCORDER_PRODUCT_TV
3207 static bool __mmcamcorder_find_max_resolution(MMHandleType handle, gint *max_width, gint *max_height)
3208 {
3209         _MMCamcorderSubContext *sc = NULL;
3210         mmf_camcorder_t *hcamcorder = NULL;
3211         int index = 0;
3212         const gchar *mime = NULL;
3213         GstPad *sinkpad;
3214         GstCaps *decsink_caps = NULL;
3215         GstStructure *decsink_struct = NULL;
3216
3217         mmf_return_val_if_fail(handle, false);
3218         mmf_return_val_if_fail(max_width, false);
3219         mmf_return_val_if_fail(max_height, false);
3220
3221         hcamcorder = MMF_CAMCORDER(handle);
3222         mmf_return_val_if_fail(hcamcorder, false);
3223
3224         sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
3225
3226         sinkpad = gst_element_get_static_pad(sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst, "sink");
3227         if (!sinkpad) {
3228                 MMCAM_LOG_ERROR("There are no decoder caps");
3229                 return false;
3230         }
3231
3232         decsink_caps = gst_pad_get_pad_template_caps(sinkpad);
3233         if (!decsink_caps) {
3234                 gst_object_unref(sinkpad);
3235                 MMCAM_LOG_ERROR("There is no decoder sink caps");
3236                 return false;
3237         }
3238
3239         for (index = 0; index < gst_caps_get_size(decsink_caps); index++) {
3240                 decsink_struct = gst_caps_get_structure(decsink_caps, index);
3241                 if (!decsink_struct) {
3242                         MMCAM_LOG_ERROR("There are no structure from caps");
3243                         gst_object_unref(decsink_caps);
3244                         gst_object_unref(sinkpad);
3245                         return false;
3246                 }
3247                 mime = gst_structure_get_name(decsink_struct);
3248                 if (!strcmp(mime, "video/x-h264")) {
3249                         MMCAM_LOG_INFO("h264 caps structure found");
3250                         if (gst_structure_has_field(decsink_struct, "maxwidth"))
3251                                 *max_width = gst_value_get_int_range_max(gst_structure_get_value(decsink_struct, "maxwidth"));
3252                         if (gst_structure_has_field(decsink_struct, "maxheight"))
3253                                 *max_height = gst_value_get_int_range_max(gst_structure_get_value(decsink_struct, "maxheight"));
3254                         break;
3255                 } else if (!strcmp(mime, "video/x-jpeg")) {
3256                         MMCAM_LOG_INFO("mjpeg caps structure found");
3257                         if (gst_structure_has_field(decsink_struct, "maxwidth"))
3258                                 *max_width = gst_value_get_int_range_max(gst_structure_get_value(decsink_struct, "maxwidth"));
3259                         if (gst_structure_has_field(decsink_struct, "maxheight"))
3260                                 *max_height = gst_value_get_int_range_max(gst_structure_get_value(decsink_struct, "maxheight"));
3261                         break;
3262                 }
3263         }
3264         MMCAM_LOG_INFO("maxwidth = %d , maxheight = %d", (int)*max_width, (int)*max_height);
3265         gst_object_unref(decsink_caps);
3266         gst_object_unref(sinkpad);
3267
3268         if (*max_width <= 0 || *max_height <= 0)
3269                 return false;
3270
3271         return true;
3272 }
3273 #endif /* _MMCAMCORDER_PRODUCT_TV */