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