Updated to version 0.7.16 from tizendev
[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/audio/audio-format.h>
26 #include <gst/video/videooverlay.h>
27 #include <gst/video/cameracontrol.h>
28 #include <sys/time.h>
29 #include <unistd.h>
30
31 #include "mm_camcorder_internal.h"
32 #include "mm_camcorder_gstcommon.h"
33
34 /*-----------------------------------------------------------------------
35 |    GLOBAL VARIABLE DEFINITIONS for internal                           |
36 -----------------------------------------------------------------------*/
37 /* Table for compatibility between audio codec and file format */
38 gboolean        audiocodec_fileformat_compatibility_table[MM_AUDIO_CODEC_NUM][MM_FILE_FORMAT_NUM] =
39 {          /* 3GP ASF AVI MATROSKA MP4 OGG NUT QT REAL AMR AAC MP3 AIFF AU WAV MID MMF DIVX FLV VOB IMELODY WMA WMV JPG */
40 /*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,  },
41 /*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,  },
42 /*MP3*/       { 0,  0,  0,       0,  0,  0,  0, 0,   0,  0,  0,  0,   0, 0,  0,  0,  0,   0,  0,  0,      0,  0,  0,  0,  },
43 /*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,  },
44 /*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,  },
45 /*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,  },
46 /*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,  },
47 /*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,  },
48 /*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,  },
49 /*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,  },
50 /*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,  },
51 /*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,  },
52 /*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,  },
53 /*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,  },
54 /*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,  },
55 /*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,  },
56 /*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,  },
57 /*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,  },
58 /*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,  },
59 /*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,  },
60 /*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,  },
61 /*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,  },
62 /*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,  },
63 /*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,  },
64 /*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,  },
65 /*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,  },
66 /*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,  },
67 /*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,  },
68 /*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,  },
69 /*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,  },
70 /*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,  },
71 /*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,  },
72 /*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,  },
73 /*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,  },
74 /*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,  },
75 /*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,  },
76 /*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,  },
77 /*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,  },
78 /*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,  },
79 /*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,  },
80 /*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,  },
81 /*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,  },
82 /*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,  },
83 /*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,  },
84 };
85
86 /* Table for compatibility between video codec and file format */
87 gboolean        videocodec_fileformat_compatibility_table[MM_VIDEO_CODEC_NUM][MM_FILE_FORMAT_NUM] =
88 {             /* 3GP ASF AVI MATROSKA MP4 OGG NUT QT REAL AMR AAC MP3 AIFF AU WAV MID MMF DIVX FLV VOB IMELODY WMA WMV JPG */
89 /*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,  },
90 /*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,  },
91 /*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,  },
92 /*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,  },
93 /*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,  },
94 /*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,  },
95 /*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,  },
96 /*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,  },
97 /*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,  },
98 /*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,  },
99 /*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,  },
100 /*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,  },
101 /*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,  },
102 /*MJPEG*/        { 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 /*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,  },
104 /*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,  },
105 /*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,  },
106 /*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,  },
107 /*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,  },
108 /*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,  },
109 /*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,  },
110 /*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,  },
111 /*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,  },
112 /*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,  },
113 /*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,  },
114 /*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,  },
115 /*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,  },
116 /*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,  },
117 };
118
119
120 /*-----------------------------------------------------------------------
121 |    LOCAL VARIABLE DEFINITIONS for internal                            |
122 -----------------------------------------------------------------------*/
123 #define USE_AUDIO_CLOCK_TUNE
124 #define _MMCAMCORDER_WAIT_EOS_TIME              5.0     /* sec */
125 #define _MMCAMCORDER_CONVERT_OUTPUT_BUFFER_NUM  6
126
127 /*-----------------------------------------------------------------------
128 |    LOCAL FUNCTION PROTOTYPES:                                         |
129 -----------------------------------------------------------------------*/
130 /* STATIC INTERNAL FUNCTION */
131 /**
132  * These functions are preview video data probing function.
133  * If this function is linked with certain pad by gst_pad_add_buffer_probe(),
134  * this function will be called when data stream pass through the pad.
135  *
136  * @param[in]   pad             probing pad which calls this function.
137  * @param[in]   buffer          buffer which contains stream data.
138  * @param[in]   u_data          user data.
139  * @return      This function returns true on success, or false value with error
140  * @remarks
141  * @see         __mmcamcorder_create_preview_pipeline()
142  */
143 static GstPadProbeReturn __mmcamcorder_video_dataprobe_preview(GstPad *pad, GstPadProbeInfo *info, gpointer u_data);
144
145 static int __mmcamcorder_get_amrnb_bitrate_mode(int bitrate);
146
147 /*=======================================================================================
148 |  FUNCTION DEFINITIONS                                                                 |
149 =======================================================================================*/
150 /*-----------------------------------------------------------------------
151 |    GLOBAL FUNCTION DEFINITIONS:                                       |
152 -----------------------------------------------------------------------*/
153 int _mmcamcorder_create_videosrc_bin(MMHandleType handle)
154 {
155         int err = MM_ERROR_NONE;
156         int rotate = 0;
157         int flip = 0;
158         int fps = 0;
159         int hold_af = 0;
160         int UseVideoscale = 0;
161         int codectype = 0;
162         int capture_width = 0;
163         int capture_height = 0;
164         int capture_jpg_quality = 100;
165         int video_stabilization = 0;
166         int anti_shake = 0;
167         char *videosrc_name = NULL;
168         char *err_name = NULL;
169         unsigned int pix_fourcc = 0;
170
171         GList *element_list = NULL;
172         GstCaps *caps = NULL;
173         GstPad *video_tee0 = NULL;
174         GstPad *video_tee1 = NULL;
175
176         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
177         _MMCamcorderSubContext *sc = NULL;
178         type_element *VideosrcElement = NULL;
179         type_int_array *input_index = NULL;
180
181         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
182
183         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
184         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
185         mmf_return_val_if_fail(sc->element, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
186
187         _mmcam_dbg_log("");
188
189         /* Check existence */
190         if (sc->element[_MMCAMCORDER_VIDEOSRC_BIN].gst) {
191                 if (((GObject *)sc->element[_MMCAMCORDER_VIDEOSRC_BIN].gst)->ref_count > 0) {
192                         gst_object_unref(sc->element[_MMCAMCORDER_VIDEOSRC_BIN].gst);
193                 }
194                 _mmcam_dbg_log("_MMCAMCORDER_VIDEOSRC_BIN is Already existed.");
195         }
196
197         /* Get video device index info */
198         _mmcamcorder_conf_get_value_int_array(hcamcorder->conf_ctrl,
199                                               CONFIGURE_CATEGORY_CTRL_CAMERA,
200                                               "InputIndex",
201                                               &input_index );
202         if (input_index == NULL) {
203                 _mmcam_dbg_err("Failed to get input_index");
204                 return MM_ERROR_CAMCORDER_CREATE_CONFIGURE;
205         }
206
207         err = mm_camcorder_get_attributes(handle, &err_name,
208                                           MMCAM_CAMERA_FORMAT, &sc->info_image->preview_format,
209                                           MMCAM_CAMERA_FPS, &fps,
210                                           MMCAM_CAMERA_ROTATION, &rotate,
211                                           MMCAM_CAMERA_FLIP, &flip,
212                                           MMCAM_CAMERA_VIDEO_STABILIZATION, &video_stabilization,
213                                           MMCAM_CAMERA_ANTI_HANDSHAKE, &anti_shake,
214                                           MMCAM_CAPTURE_WIDTH, &capture_width,
215                                           MMCAM_CAPTURE_HEIGHT, &capture_height,
216                                           MMCAM_IMAGE_ENCODER, &codectype,
217                                           MMCAM_IMAGE_ENCODER_QUALITY, &capture_jpg_quality,
218                                           "camera-hold-af-after-capturing", &hold_af,
219                                           NULL);
220         if (err != MM_ERROR_NONE) {
221                 _mmcam_dbg_warn("Get attrs fail. (%s:%x)", err_name, err);
222                 SAFE_FREE(err_name);
223                 return err;
224         }
225
226         /* Get fourcc from picture format */
227         pix_fourcc=  _mmcamcorder_get_fourcc(sc->info_image->preview_format, codectype, hcamcorder->use_zero_copy_format);
228         strncpy(sc->format_name, (char*)&pix_fourcc, sizeof(pix_fourcc));
229         sc->format_name[sizeof(pix_fourcc)] = '\0';
230
231         /* Get videosrc element and its name from configure */
232         _mmcamcorder_conf_get_element(hcamcorder->conf_main,
233                                       CONFIGURE_CATEGORY_MAIN_VIDEO_INPUT,
234                                       "VideosrcElement",
235                                       &VideosrcElement);
236         _mmcamcorder_conf_get_value_element_name(VideosrcElement, &videosrc_name);
237
238         /* Create bin element */
239         __ta__("                videosrc_bin",
240         _MMCAMCORDER_BIN_MAKE(sc, _MMCAMCORDER_VIDEOSRC_BIN, "videosrc_bin", err);
241         );
242
243         /* Create child element */
244         __ta__("                videosrc_src",
245         _MMCAMCORDER_ELEMENT_MAKE(sc, _MMCAMCORDER_VIDEOSRC_SRC, videosrc_name, "videosrc_src", element_list, err);
246         );
247         __ta__("                videosrc_capsfilter",
248         _MMCAMCORDER_ELEMENT_MAKE(sc, _MMCAMCORDER_VIDEOSRC_FILT, "capsfilter", "videosrc_filter", element_list, err);
249         );
250         __ta__("                videosrc_queue",
251         _MMCAMCORDER_ELEMENT_MAKE(sc, _MMCAMCORDER_VIDEOSRC_QUE, "queue", "videosrc_queue", element_list, err);
252         );
253
254         /* init high-speed-fps */
255         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "high-speed-fps", 0);
256
257         /* set capture size, quality and flip setting which were set before mm_camcorder_realize */
258         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "capture-width", capture_width);
259         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "capture-height", capture_height);
260         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "capture-jpg-quality", capture_jpg_quality);
261
262         /* set camera flip */
263         _mmcamcorder_set_videosrc_flip(handle, flip);
264
265         /* set flush cache as FALSE */
266         if (sc->info_image->preview_format == MM_PIXEL_FORMAT_ITLV_JPEG_UYVY) {
267                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "flush-cache", FALSE);
268         }
269
270         /* set video stabilization mode */
271         _mmcamcorder_set_videosrc_stabilization(handle, video_stabilization);
272
273         /* set anti handshake mode */
274         _mmcamcorder_set_videosrc_anti_shake(handle, anti_shake);
275
276         _mmcamcorder_conf_get_value_int(hcamcorder->conf_main,
277                                         CONFIGURE_CATEGORY_MAIN_VIDEO_INPUT,
278                                         "UseVideoscale",
279                                         &UseVideoscale);
280         if (UseVideoscale) {
281                 int set_width = 0;
282                 int set_height = 0;
283                 int scale_width = 0;
284                 int scale_height = 0;
285                 int scale_method = 0;
286                 char *videoscale_name = NULL;
287                 type_element *VideoscaleElement = NULL;
288
289                 _mmcamcorder_conf_get_element(hcamcorder->conf_main,
290                                               CONFIGURE_CATEGORY_MAIN_VIDEO_INPUT,
291                                               "VideoscaleElement",
292                                               &VideoscaleElement);
293                 _mmcamcorder_conf_get_value_element_name(VideoscaleElement, &videoscale_name);
294                 __ta__("                videosrc_scale",
295                 _MMCAMCORDER_ELEMENT_MAKE(sc, _MMCAMCORDER_VIDEOSRC_SCALE, videoscale_name, "videosrc_scale", element_list, err);
296                 );
297                 __ta__("                videoscale_capsfilter",
298                 _MMCAMCORDER_ELEMENT_MAKE(sc, _MMCAMCORDER_VIDEOSRC_VSFLT, "capsfilter", "videosrc_scalefilter", element_list, err);
299                 );
300
301                 _mmcamcorder_conf_get_value_element_int(VideoscaleElement, "width", &scale_width);
302                 _mmcamcorder_conf_get_value_element_int(VideoscaleElement, "height", &scale_height);
303                 _mmcamcorder_conf_get_value_element_int(VideoscaleElement, "method", &scale_method);
304
305                 if (rotate == MM_VIDEO_INPUT_ROTATION_90 ||
306                         rotate == MM_VIDEO_INPUT_ROTATION_270) {
307                         set_width = scale_height;
308                         set_height = scale_width;
309                 } else {
310                         set_width = scale_width;
311                         set_height = scale_height;
312                 }
313
314                 _mmcam_dbg_log("VideoSRC Scale[%dx%d], Method[%d]", set_width, set_height, scale_method);
315
316                 caps = gst_caps_new_simple("video/x-raw",
317                                    "format", G_TYPE_STRING, sc->format_name,
318                                            "width", G_TYPE_INT, set_width,
319                                            "height", G_TYPE_INT, set_height,
320                                            NULL);
321                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_VSFLT].gst, "caps", caps);
322                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SCALE].gst, "method", scale_method);
323
324                 gst_caps_unref(caps);
325                 caps = NULL;
326         }
327
328         if (sc->is_modified_rate) {
329                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "high-speed-fps", fps);
330         }
331
332         __ta__("                tee",
333         _MMCAMCORDER_ELEMENT_MAKE(sc, _MMCAMCORDER_VIDEOSRC_TEE, "tee", "videosrc_tee", element_list, err);
334         );
335
336         /* Set basic infomation of videosrc element */
337         _mmcamcorder_conf_set_value_element_property(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, VideosrcElement);
338
339         /* Set video device index */
340         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "camera-id", input_index->default_value);
341         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "hold-af-after-capturing", hold_af);
342
343         _mmcam_dbg_log("Current mode[%d]", hcamcorder->type);
344
345         /* Set sensor mode as CAMERA */
346         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "sensor-mode", 0);
347
348         /* Set caps by rotation */
349         _mmcamcorder_set_videosrc_rotation(handle, rotate);
350
351         if (!_mmcamcorder_add_elements_to_bin(GST_BIN(sc->element[_MMCAMCORDER_VIDEOSRC_BIN].gst), element_list)) {
352                 _mmcam_dbg_err( "element add error." );
353                 err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
354                 goto pipeline_creation_error;
355         }
356
357         if (!_mmcamcorder_link_elements(element_list)) {
358                 _mmcam_dbg_err( "element link error." );
359                 err = MM_ERROR_CAMCORDER_GST_LINK;
360                 goto pipeline_creation_error;
361         }
362
363         video_tee0 = gst_element_get_request_pad(sc->element[_MMCAMCORDER_VIDEOSRC_TEE].gst, "src_%u");
364         video_tee1 = gst_element_get_request_pad(sc->element[_MMCAMCORDER_VIDEOSRC_TEE].gst, "src_%u");
365
366         MMCAMCORDER_G_OBJECT_SET((sc->element[_MMCAMCORDER_VIDEOSRC_TEE].gst), "alloc-pad", video_tee0);
367
368         /* Ghost pad */
369         if ((gst_element_add_pad( sc->element[_MMCAMCORDER_VIDEOSRC_BIN].gst, gst_ghost_pad_new("src0", video_tee0) )) < 0) {
370                 _mmcam_dbg_err("failed to create ghost pad1 on _MMCAMCORDER_VIDEOSRC_BIN.");
371                 gst_object_unref(video_tee0);
372                 video_tee0 = NULL;
373                 gst_object_unref(video_tee1);
374                 video_tee1 = NULL;
375                 err = MM_ERROR_CAMCORDER_GST_LINK;
376                 goto pipeline_creation_error;
377         }
378
379         if ((gst_element_add_pad( sc->element[_MMCAMCORDER_VIDEOSRC_BIN].gst, gst_ghost_pad_new("src1", video_tee1) )) < 0) {
380                 _mmcam_dbg_err("failed to create ghost pad2 on _MMCAMCORDER_VIDEOSRC_BIN.");
381                 gst_object_unref(video_tee0);
382                 video_tee0 = NULL;
383                 gst_object_unref(video_tee1);
384                 video_tee1 = NULL;
385                 err = MM_ERROR_CAMCORDER_GST_LINK;
386                 goto pipeline_creation_error;
387         }
388
389         gst_object_unref(video_tee0);
390         video_tee0 = NULL;
391         gst_object_unref(video_tee1);
392         video_tee1 = NULL;
393
394         if (element_list) {
395                 g_list_free(element_list);
396                 element_list = NULL;
397         }
398
399         return MM_ERROR_NONE;
400
401 pipeline_creation_error:
402         _MMCAMCORDER_ELEMENT_REMOVE( sc, _MMCAMCORDER_VIDEOSRC_SRC );
403         _MMCAMCORDER_ELEMENT_REMOVE( sc, _MMCAMCORDER_VIDEOSRC_FILT );
404         _MMCAMCORDER_ELEMENT_REMOVE( sc, _MMCAMCORDER_VIDEOSRC_SCALE );
405         _MMCAMCORDER_ELEMENT_REMOVE( sc, _MMCAMCORDER_VIDEOSRC_VSFLT );
406         _MMCAMCORDER_ELEMENT_REMOVE( sc, _MMCAMCORDER_VIDEOSRC_TEE );
407         _MMCAMCORDER_ELEMENT_REMOVE( sc, _MMCAMCORDER_VIDEOSRC_BIN );
408         if (element_list) {
409                 g_list_free(element_list);
410                 element_list = NULL;
411         }
412
413         return err;
414 }
415
416
417 int _mmcamcorder_create_audiosrc_bin(MMHandleType handle)
418 {
419         int err = MM_ERROR_NONE;
420         int val = 0;
421         int rate = 0;
422         int format = 0;
423         int channel = 0;
424         int a_enc = MM_AUDIO_CODEC_AMR;
425         int a_dev = MM_AUDIO_DEVICE_MIC;
426         int UseNoiseSuppressor = 0;
427         double volume = 0.0;
428         char *err_name = NULL;
429         char *audiosrc_name = NULL;
430         char *cat_name = NULL;
431
432         GstCaps *caps = NULL;
433         GstPad *pad = NULL;
434         GList *element_list  = NULL;
435
436         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
437         _MMCamcorderSubContext *sc = NULL;
438         _MMCamcorderGstElement *last_element = NULL;
439         type_element *AudiosrcElement = NULL;
440
441         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
442
443         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
444         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
445         mmf_return_val_if_fail(sc->element, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
446
447         _mmcam_dbg_log("");
448
449         err = _mmcamcorder_check_audiocodec_fileformat_compatibility(handle);
450         if (err != MM_ERROR_NONE) {
451                 return err;
452         }
453
454         _mmcamcorder_conf_get_value_int(hcamcorder->conf_main,
455                                         CONFIGURE_CATEGORY_MAIN_RECORD,
456                                         "UseNoiseSuppressor",
457                                         &UseNoiseSuppressor);
458
459         err = mm_camcorder_get_attributes(handle, &err_name,
460                                           MMCAM_AUDIO_DEVICE, &a_dev,
461                                           MMCAM_AUDIO_ENCODER, &a_enc,
462                                           MMCAM_AUDIO_ENCODER_BITRATE, &val,
463                                           MMCAM_AUDIO_SAMPLERATE, &rate,
464                                           MMCAM_AUDIO_FORMAT, &format,
465                                           MMCAM_AUDIO_CHANNEL, &channel,
466                                           MMCAM_AUDIO_VOLUME, &volume,
467                                           NULL);
468         if (err != MM_ERROR_NONE) {
469                 _mmcam_dbg_warn("Get attrs fail. (%s:%x)", err_name, err);
470                 SAFE_FREE(err_name);
471                 return err;
472         }
473
474         /* Check existence */
475         if (sc->element[_MMCAMCORDER_AUDIOSRC_BIN].gst) {
476                 if (((GObject *)sc->element[_MMCAMCORDER_AUDIOSRC_BIN].gst)->ref_count > 0) {
477                         gst_object_unref(sc->element[_MMCAMCORDER_AUDIOSRC_BIN].gst);
478                 }
479                 _mmcam_dbg_log("_MMCAMCORDER_AUDIOSRC_BIN is Already existed. Unref once...");
480         }
481
482         /* Create bin element */
483         __ta__("                audiosource_bin",
484         _MMCAMCORDER_BIN_MAKE(sc, _MMCAMCORDER_AUDIOSRC_BIN, "audiosource_bin", err);
485         );
486
487         if (a_dev == MM_AUDIO_DEVICE_MODEM) {
488                 cat_name = strdup("AudiomodemsrcElement");
489         } else {
490                 /* MM_AUDIO_DEVICE_MIC or others */
491                 cat_name = strdup("AudiosrcElement");
492         }
493
494         if (cat_name == NULL) {
495                 _mmcam_dbg_err("strdup failed.");
496                 err = MM_ERROR_CAMCORDER_LOW_MEMORY;
497                 goto pipeline_creation_error;
498         }
499
500         _mmcamcorder_conf_get_element(hcamcorder->conf_main,
501                                       CONFIGURE_CATEGORY_MAIN_AUDIO_INPUT,
502                                       cat_name,
503                                       &AudiosrcElement);
504         _mmcamcorder_conf_get_value_element_name(AudiosrcElement, &audiosrc_name);
505
506         free(cat_name);
507         cat_name = NULL;
508
509         _mmcam_dbg_log("Audio src name : %s", audiosrc_name);
510
511         __ta__("                audiosrc",
512         _MMCAMCORDER_ELEMENT_MAKE(sc, _MMCAMCORDER_AUDIOSRC_SRC, audiosrc_name, NULL, element_list, err);
513         );
514
515         _mmcamcorder_conf_set_value_element_property(sc->element[_MMCAMCORDER_AUDIOSRC_SRC].gst, AudiosrcElement);
516
517         __ta__("                audiosource_capsfilter",
518         _MMCAMCORDER_ELEMENT_MAKE(sc, _MMCAMCORDER_AUDIOSRC_FILT, "capsfilter", NULL, element_list, err);
519         );
520
521         if (a_enc != MM_AUDIO_CODEC_VORBIS) {
522                 __ta__("                audiosource_volume",
523                 _MMCAMCORDER_ELEMENT_MAKE(sc, _MMCAMCORDER_AUDIOSRC_VOL, "volume", NULL, element_list, err);
524                 );
525         }
526
527         if (UseNoiseSuppressor && a_enc != MM_AUDIO_CODEC_AAC) {
528                 __ta__("                noise_suppressor",
529                 _MMCAMCORDER_ELEMENT_MAKE(sc, _MMCAMCORDER_AUDIOSRC_NS, "noisesuppressor", "audiofilter", element_list, err);
530                 );
531         }
532
533         /* Set basic infomation */
534         if (a_enc != MM_AUDIO_CODEC_VORBIS) {
535                 int depth = 0;
536                 const gchar* format_name = NULL;
537
538                 if (volume == 0.0) {
539                         /* Because data probe of audio src do the same job, it doesn't need to set "mute" here. Already null raw data. */
540                         MMCAMCORDER_G_OBJECT_SET( sc->element[_MMCAMCORDER_AUDIOSRC_VOL].gst, "volume", 1.0);
541                 } else {
542                         MMCAMCORDER_G_OBJECT_SET( sc->element[_MMCAMCORDER_AUDIOSRC_VOL].gst, "mute", FALSE);
543                         MMCAMCORDER_G_OBJECT_SET( sc->element[_MMCAMCORDER_AUDIOSRC_VOL].gst, "volume", volume);
544                 }
545
546                 if (format == MM_CAMCORDER_AUDIO_FORMAT_PCM_S16_LE) {
547                         depth = 16;
548                         format_name = "S16LE";
549                 } else { /* MM_CAMCORDER_AUDIO_FORMAT_PCM_U8 */
550                     format_name = "U8";
551                         depth = 8;
552                 }
553
554                 caps = gst_caps_new_simple("audio/x-raw",
555                                            "rate", G_TYPE_INT, rate,
556                                            "channels", G_TYPE_INT, channel,
557                                            "format", G_TYPE_STRING, format_name,
558                                            NULL);
559                 _mmcam_dbg_log("caps [x-raw-int,rate:%d,channel:%d,depth:%d]",
560                                rate, channel, depth);
561         } else {
562                 /* what are the audio encoder which should get audio/x-raw-float? */
563                 caps = gst_caps_new_simple("audio/x-raw",
564                                            "rate", G_TYPE_INT, rate,
565                                            "channels", G_TYPE_INT, channel,
566                                            "format", G_TYPE_STRING, GST_AUDIO_NE(F32),
567                                            NULL);
568                 _mmcam_dbg_log("caps [x-raw-float,rate:%d,channel:%d,endianness:%d,width:32]",
569                                rate, channel, BYTE_ORDER);
570         }
571
572         MMCAMCORDER_G_OBJECT_SET((sc->element[_MMCAMCORDER_AUDIOSRC_FILT].gst), "caps", caps);
573         gst_caps_unref(caps);
574
575         if (!_mmcamcorder_add_elements_to_bin(GST_BIN(sc->element[_MMCAMCORDER_AUDIOSRC_BIN].gst), element_list)) {
576                 _mmcam_dbg_err("element add error.");
577                 err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
578                 goto pipeline_creation_error;
579         }
580
581         if (!_mmcamcorder_link_elements(element_list)) {
582                 _mmcam_dbg_err( "element link error." );
583                 err = MM_ERROR_CAMCORDER_GST_LINK;
584                 goto pipeline_creation_error;
585         }
586
587         last_element = (_MMCamcorderGstElement*)(g_list_last(element_list)->data);
588         pad = gst_element_get_static_pad(last_element->gst, "src");
589         if ((gst_element_add_pad( sc->element[_MMCAMCORDER_AUDIOSRC_BIN].gst, gst_ghost_pad_new("src", pad) )) < 0) {
590                 gst_object_unref(pad);
591                 pad = NULL;
592                 _mmcam_dbg_err("failed to create ghost pad on _MMCAMCORDER_AUDIOSRC_BIN.");
593                 err = MM_ERROR_CAMCORDER_GST_LINK;
594                 goto pipeline_creation_error;
595         }
596
597         gst_object_unref(pad);
598         pad = NULL;
599
600         if (element_list) {
601                 g_list_free(element_list);
602                 element_list = NULL;
603         }
604
605         return MM_ERROR_NONE;
606
607 pipeline_creation_error:
608         _MMCAMCORDER_ELEMENT_REMOVE(sc, _MMCAMCORDER_AUDIOSRC_SRC);
609         _MMCAMCORDER_ELEMENT_REMOVE(sc, _MMCAMCORDER_AUDIOSRC_FILT);
610         _MMCAMCORDER_ELEMENT_REMOVE(sc, _MMCAMCORDER_AUDIOSRC_VOL);
611         _MMCAMCORDER_ELEMENT_REMOVE(sc, _MMCAMCORDER_AUDIOSRC_QUE);
612         _MMCAMCORDER_ELEMENT_REMOVE(sc, _MMCAMCORDER_AUDIOSRC_CONV);
613         _MMCAMCORDER_ELEMENT_REMOVE(sc, _MMCAMCORDER_AUDIOSRC_ENC);
614         _MMCAMCORDER_ELEMENT_REMOVE(sc, _MMCAMCORDER_AUDIOSRC_BIN);
615         if (element_list) {
616                 g_list_free(element_list);
617                 element_list = NULL;
618         }
619
620         return err;
621 }
622
623
624 int _mmcamcorder_create_videosink_bin(MMHandleType handle)
625 {
626         int err = MM_ERROR_NONE;
627         int rect_width = 0;
628         int rect_height = 0;
629         int camera_width = 0;
630         int camera_height = 0;
631         int rotate = 0;
632         int flip = 0;
633         int UseVideoscale = 0, EnableConvertedSC = 0;
634         int scale_method = 0;
635         char* videosink_name = NULL;
636         char* videoscale_name = NULL;
637         char* err_name = NULL;
638
639         GstCaps *caps = NULL;
640         GstPad *pad = NULL;
641         GList *element_list = NULL;
642
643         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
644         _MMCamcorderSubContext *sc = NULL;
645         _MMCamcorderGstElement *first_element = NULL;
646         type_element *VideoscaleElement = NULL;
647
648         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
649         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
650
651         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
652         mmf_return_val_if_fail(sc->element, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
653
654         _mmcam_dbg_log("START");
655
656         /* Get attributes */
657         err = mm_camcorder_get_attributes(handle, &err_name,
658                                           MMCAM_CAMERA_WIDTH, &camera_width,
659                                           MMCAM_CAMERA_HEIGHT, &camera_height,
660                                           MMCAM_DISPLAY_RECT_WIDTH, &rect_width,
661                                           MMCAM_DISPLAY_RECT_HEIGHT, &rect_height,
662                                           MMCAM_DISPLAY_ROTATION, &rotate,
663                                           MMCAM_DISPLAY_FLIP, &flip,
664                                           "enable-converted-stream-callback", &EnableConvertedSC,
665                                           NULL);
666         if (err != MM_ERROR_NONE) {
667                 _mmcam_dbg_warn("Get attrs fail. (%s:%x)", err_name, err);
668                 SAFE_FREE(err_name);
669                 return err;
670         }
671
672         /* Get videosink name */
673         _mmcamcorder_conf_get_value_element_name(sc->VideosinkElement, &videosink_name);
674
675         /* Check existence */
676         if (sc->element[_MMCAMCORDER_VIDEOSINK_BIN].gst) {
677                 if (((GObject *)sc->element[_MMCAMCORDER_VIDEOSINK_BIN].gst)->ref_count > 0) {
678                         gst_object_unref(sc->element[_MMCAMCORDER_VIDEOSINK_BIN].gst);
679                 }
680                 _mmcam_dbg_log("_MMCAMCORDER_VIDEOSINK_BIN is Already existed. Unref once...");
681         }
682
683         /* Create bin element */
684         __ta__("                videosink_bin",
685         _MMCAMCORDER_BIN_MAKE(sc, _MMCAMCORDER_VIDEOSINK_BIN, "videosink_bin", err);
686         );
687
688         _mmcamcorder_conf_get_value_int(hcamcorder->conf_main,
689                                         CONFIGURE_CATEGORY_MAIN_VIDEO_OUTPUT,
690                                         "UseVideoscale",
691                                         &UseVideoscale);
692
693         /* Create child element */
694         if (EnableConvertedSC ||
695             !strcmp(videosink_name, "evasimagesink") ||
696             !strcmp(videosink_name, "ximagesink")) {
697                 GstElementFactory *factory = gst_element_factory_find("fimcconvert");
698
699                 if ((strcmp(sc->format_name, "SN21") == 0 ||
700              strcmp(sc->format_name, "SN12") == 0 ||
701              strcmp(sc->format_name, "ST12") == 0 ||
702              strcmp(sc->format_name, "S420") == 0) &&
703                     factory) {
704                         int set_rotate = 0;
705                         int set_flip = 0;
706
707                         gst_object_unref(factory);
708                         factory = NULL;
709
710                         __ta__("                videosink_fimcconvert",
711                         _MMCAMCORDER_ELEMENT_MAKE(sc, _MMCAMCORDER_VIDEOSINK_CLS, "fimcconvert", NULL, element_list, err);
712                         );
713
714                         /* set rotate */
715                         if (rotate > MM_VIDEO_INPUT_ROTATION_NONE &&
716                             rotate < MM_VIDEO_INPUT_ROTATION_NUM) {
717                                 set_rotate = rotate * 90;
718                         } else {
719                                 set_rotate = 0;
720                         }
721
722                         if (rect_width == 0 || rect_height == 0) {
723                                 _mmcam_dbg_warn("rect_width or height is 0. set camera width and height.");
724
725                                 if (rotate == MM_DISPLAY_ROTATION_90 ||
726                                     rotate == MM_DISPLAY_ROTATION_270) {
727                                         rect_width = camera_height;
728                                         rect_height = camera_width;
729                                 } else {
730                                         rect_width = camera_width;
731                                         rect_height = camera_height;
732                                 }
733                         }
734
735                         /* set flip */
736                         switch (flip) {
737                         case MM_FLIP_HORIZONTAL:
738                                 set_flip = FIMCCONVERT_FLIP_HORIZONTAL;
739                                 break;
740                         case MM_FLIP_VERTICAL:
741                                 set_flip = FIMCCONVERT_FLIP_VERTICAL;
742                                 break;
743                         case MM_FLIP_BOTH: /* both flip has same effect with 180 rotation */
744                                 set_rotate = (set_rotate + 180) % 360;
745                         case MM_FLIP_NONE:
746                         default:
747                                 set_flip = FIMCCONVERT_FLIP_NONE;
748                                 break;
749                         }
750
751                         _mmcam_dbg_log("Fimcconvert set values - %dx%d, rotate: %d, flip: %d",
752                                        rect_width, rect_height, set_rotate, set_flip);
753
754                         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSINK_CLS].gst, "src-width", rect_width);
755                         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSINK_CLS].gst, "src-height", rect_height);
756                         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSINK_CLS].gst, "rotate", set_rotate);
757                         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSINK_CLS].gst, "flip", set_flip);
758                 } else {
759                         if (factory) {
760                                 gst_object_unref(factory);
761                                 factory = NULL;
762                         }
763
764                         __ta__("                videosink_videoconvert",
765                         _MMCAMCORDER_ELEMENT_MAKE(sc, _MMCAMCORDER_VIDEOSINK_CLS, "videoconvert", NULL, element_list, err);
766                         );
767                 }
768         } else if(UseVideoscale) {
769                 __ta__("                videosink_queue",
770                 _MMCAMCORDER_ELEMENT_MAKE(sc, _MMCAMCORDER_VIDEOSINK_QUE, "queue", NULL, element_list, err);
771                 );
772
773                 _mmcamcorder_conf_get_element(hcamcorder->conf_main,
774                                               CONFIGURE_CATEGORY_MAIN_VIDEO_OUTPUT,
775                                               "VideoscaleElement",
776                                               &VideoscaleElement);
777                 _mmcamcorder_conf_get_value_element_name(VideoscaleElement, &videoscale_name);
778
779                 __ta__("                videosink_videoscale",
780                 _MMCAMCORDER_ELEMENT_MAKE(sc, _MMCAMCORDER_VIDEOSINK_SCALE, videoscale_name, NULL, element_list, err);
781                 );
782                 __ta__("                videosink_capsfilter",
783                 _MMCAMCORDER_ELEMENT_MAKE(sc, _MMCAMCORDER_VIDEOSINK_FILT, "capsfilter", NULL, element_list, err);
784                 );
785
786                 _mmcamcorder_conf_get_value_element_int(VideoscaleElement, "method", &scale_method);
787                 _mmcam_dbg_log("VideoSINK Scale[%dx%d], Method[%d]", rect_width, rect_height, scale_method);
788
789                 if (rect_width == 0 || rect_height == 0) {
790                         _mmcam_dbg_warn("rect_width or height is 0. set camera width and height.");
791
792                         rect_width = camera_width;
793                         rect_height = camera_height;
794                 }
795
796                 caps = gst_caps_new_simple("video/x-raw",
797                                            "width", G_TYPE_INT, rect_width,
798                                            "height", G_TYPE_INT, rect_height,
799                                            NULL);
800                 MMCAMCORDER_G_OBJECT_SET((sc->element[_MMCAMCORDER_VIDEOSINK_FILT].gst), "caps", caps);
801                 MMCAMCORDER_G_OBJECT_SET((sc->element[_MMCAMCORDER_VIDEOSINK_SCALE].gst), "method", scale_method);
802                 gst_caps_unref(caps);
803                 caps = NULL;
804         }
805
806         if (sc->element[_MMCAMCORDER_VIDEOSINK_QUE].gst == NULL) {
807                 __ta__("                videosink_queue",
808                 _MMCAMCORDER_ELEMENT_MAKE(sc, _MMCAMCORDER_VIDEOSINK_QUE, "queue", "videosink_queue", element_list, err);
809                 );
810         }
811
812         __ta__("                videosink",
813         _MMCAMCORDER_ELEMENT_MAKE(sc, _MMCAMCORDER_VIDEOSINK_SINK, videosink_name, NULL, element_list, err);
814         );
815
816         if (!strcmp(videosink_name, "xvimagesink") || !strcmp(videosink_name, "ximagesink") ||
817             !strcmp(videosink_name, "evasimagesink") || !strcmp(videosink_name, "evaspixmapsink")) {
818                 if (_mmcamcorder_videosink_window_set(handle, sc->VideosinkElement) != MM_ERROR_NONE) {
819                         _mmcam_dbg_err("_mmcamcorder_videosink_window_set error");
820                         err = MM_ERROR_CAMCORDER_INVALID_ARGUMENT;
821                         goto pipeline_creation_error;
822                 }
823         }
824
825         _mmcamcorder_conf_set_value_element_property(sc->element[_MMCAMCORDER_VIDEOSINK_SINK].gst, sc->VideosinkElement);
826
827         if (!_mmcamcorder_add_elements_to_bin(GST_BIN(sc->element[_MMCAMCORDER_VIDEOSINK_BIN].gst), element_list)) {
828                 _mmcam_dbg_err("element add error.");
829                 err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
830                 goto pipeline_creation_error;
831         }
832
833         if (!_mmcamcorder_link_elements(element_list)) {
834                 _mmcam_dbg_err("element link error.");
835                 err = MM_ERROR_CAMCORDER_GST_LINK;
836                 goto pipeline_creation_error;
837         }
838
839         first_element = (_MMCamcorderGstElement*)(element_list->data);
840         __ta__("                gst_element_get_static_pad",
841         pad = gst_element_get_static_pad( first_element->gst, "sink");
842         );
843         if ((gst_element_add_pad( sc->element[_MMCAMCORDER_VIDEOSINK_BIN].gst, gst_ghost_pad_new("sink", pad) )) < 0) {
844                 gst_object_unref(pad);
845                 pad = NULL;
846                 _mmcam_dbg_err("failed to create ghost pad on _MMCAMCORDER_VIDEOSINK_BIN.");
847                 err = MM_ERROR_CAMCORDER_GST_LINK;
848                 goto pipeline_creation_error;
849         }
850
851         gst_object_unref(pad);
852         pad = NULL;
853
854         if (element_list) {
855                 g_list_free(element_list);
856                 element_list = NULL;
857         }
858
859         _mmcam_dbg_log("END");
860
861         return MM_ERROR_NONE;
862
863 pipeline_creation_error:
864         _MMCAMCORDER_ELEMENT_REMOVE(sc, _MMCAMCORDER_VIDEOSINK_QUE);
865         _MMCAMCORDER_ELEMENT_REMOVE(sc, _MMCAMCORDER_VIDEOSINK_SCALE);
866         _MMCAMCORDER_ELEMENT_REMOVE(sc, _MMCAMCORDER_VIDEOSINK_FILT);
867         _MMCAMCORDER_ELEMENT_REMOVE(sc, _MMCAMCORDER_VIDEOSINK_CLS);
868         _MMCAMCORDER_ELEMENT_REMOVE(sc, _MMCAMCORDER_VIDEOSINK_SINK);
869         _MMCAMCORDER_ELEMENT_REMOVE(sc, _MMCAMCORDER_VIDEOSINK_BIN);
870         if (element_list) {
871                 g_list_free(element_list);
872                 element_list = NULL;
873         }
874
875         return err;
876 }
877
878
879 int _mmcamcorder_create_encodesink_bin(MMHandleType handle, MMCamcorderEncodebinProfile profile)
880 {
881         int err = MM_ERROR_NONE;
882         int result = 0;
883         int channel = 0;
884         int audio_enc = 0;
885         int v_bitrate = 0;
886         int a_bitrate = 0;
887         int encodebin_profile = 0;
888         int auto_audio_convert = 0;
889         int auto_audio_resample = 0;
890         int auto_color_space = 0;
891         char *gst_element_venc_name = NULL;
892         char *gst_element_aenc_name = NULL;
893         char *gst_element_ienc_name = NULL;
894         char *gst_element_mux_name = NULL;
895         char *gst_element_rsink_name = NULL;
896         char *str_profile = NULL;
897         char *str_aac = NULL;
898         char *str_aar = NULL;
899         char *str_acs = NULL;
900         char *err_name = NULL;
901
902         GstCaps *caps = NULL;
903         GstPad *pad = NULL;
904         GList *element_list = NULL;
905
906         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
907         _MMCamcorderSubContext *sc = NULL;
908         type_element *VideoencElement = NULL;
909         type_element *AudioencElement = NULL;
910         type_element *ImageencElement = NULL;
911         type_element *MuxElement = NULL;
912         type_element *RecordsinkElement = NULL;
913
914         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
915
916         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
917         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
918         mmf_return_val_if_fail(sc->element, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
919
920         _mmcam_dbg_log("");
921
922         /* Check existence */
923         if (sc->element[_MMCAMCORDER_ENCSINK_BIN].gst) {
924                 if (((GObject *)sc->element[_MMCAMCORDER_ENCSINK_BIN].gst)->ref_count > 0) {
925                         gst_object_unref(sc->element[_MMCAMCORDER_ENCSINK_BIN].gst);
926                 }
927                 _mmcam_dbg_log("_MMCAMCORDER_ENCSINK_BIN is Already existed.");
928         }
929
930         /* Create bin element */
931         __ta__("                encodesink_bin",
932         _MMCAMCORDER_BIN_MAKE(sc, _MMCAMCORDER_ENCSINK_BIN, "encodesink_bin", err);
933         );
934
935         /* Create child element */
936         __ta__("                encodebin",
937         _MMCAMCORDER_ELEMENT_MAKE(sc, _MMCAMCORDER_ENCSINK_ENCBIN, "encodebin", NULL, element_list, err);
938         );
939
940         /* check element availability */
941         mm_camcorder_get_attributes(handle, &err_name,
942                                     MMCAM_AUDIO_ENCODER, &audio_enc,
943                                     MMCAM_AUDIO_CHANNEL, &channel,
944                                     MMCAM_VIDEO_ENCODER_BITRATE, &v_bitrate,
945                                     MMCAM_AUDIO_ENCODER_BITRATE, &a_bitrate,
946                                     NULL);
947
948         _mmcam_dbg_log("Profile[%d]", profile);
949
950         /* Set information */
951         if (profile == MM_CAMCORDER_ENCBIN_PROFILE_VIDEO) {
952                 str_profile = "VideoProfile";
953                 str_aac = "VideoAutoAudioConvert";
954                 str_aar = "VideoAutoAudioResample";
955                 str_acs = "VideoAutoColorSpace";
956         } else if (profile == MM_CAMCORDER_ENCBIN_PROFILE_AUDIO) {
957                 str_profile = "AudioProfile";
958                 str_aac = "AudioAutoAudioConvert";
959                 str_aar = "AudioAutoAudioResample";
960                 str_acs = "AudioAutoColorSpace";
961         } else if (profile == MM_CAMCORDER_ENCBIN_PROFILE_IMAGE) {
962                 str_profile = "ImageProfile";
963                 str_aac = "ImageAutoAudioConvert";
964                 str_aar = "ImageAutoAudioResample";
965                 str_acs = "ImageAutoColorSpace";
966         }
967
968         _mmcamcorder_conf_get_value_int(hcamcorder->conf_main, CONFIGURE_CATEGORY_MAIN_RECORD, str_profile, &encodebin_profile);
969         _mmcamcorder_conf_get_value_int(hcamcorder->conf_main, CONFIGURE_CATEGORY_MAIN_RECORD, str_aac, &auto_audio_convert);
970         _mmcamcorder_conf_get_value_int(hcamcorder->conf_main, CONFIGURE_CATEGORY_MAIN_RECORD, str_aar, &auto_audio_resample);
971         _mmcamcorder_conf_get_value_int(hcamcorder->conf_main, CONFIGURE_CATEGORY_MAIN_RECORD, str_acs, &auto_color_space);
972
973         _mmcam_dbg_log("Profile:%d, AutoAudioConvert:%d, AutoAudioResample:%d, AutoColorSpace:%d",
974                        encodebin_profile, auto_audio_convert, auto_audio_resample, auto_color_space);
975
976         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "profile", encodebin_profile);
977         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "auto-audio-convert", auto_audio_convert);
978         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "auto-audio-resample", auto_audio_resample);
979         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "auto-colorspace", auto_color_space);
980         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "use-video-toggle", FALSE);
981
982         /* Codec */
983         if (profile == MM_CAMCORDER_ENCBIN_PROFILE_VIDEO) {
984                 int use_venc_queue = 0;
985
986                 VideoencElement = _mmcamcorder_get_type_element(handle, MM_CAM_VIDEO_ENCODER);
987
988                 if (!VideoencElement) {
989                         _mmcam_dbg_err("Fail to get type element");
990                         err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
991                         goto pipeline_creation_error;
992                 }
993
994                 _mmcamcorder_conf_get_value_element_name(VideoencElement, &gst_element_venc_name);
995
996                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "venc-name", gst_element_venc_name);
997                 _MMCAMCORDER_ENCODEBIN_ELMGET(sc, _MMCAMCORDER_ENCSINK_VENC, "video-encode", err);
998
999                 /* set color convert plugin */
1000                 if (hcamcorder->use_zero_copy_format &&
1001                     strcmp(sc->format_name,"SN12") != 0) {
1002                         int dst_buffer_num = _MMCAMCORDER_CONVERT_OUTPUT_BUFFER_NUM;
1003
1004                         /* set fimcconvert as colorspace element */
1005                         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "vconv-name", "fimcconvert");
1006                         _MMCAMCORDER_ENCODEBIN_ELMGET(sc, _MMCAMCORDER_ENCSINK_VCONV, "video-convert", err);
1007
1008                         caps = gst_caps_new_simple("video/x-raw",
1009                                                    "format", G_TYPE_STRING, "SN12",
1010                                                    NULL);
1011                         if (caps) {
1012                                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "vcaps", caps);
1013                                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_ENCSINK_VCONV].gst, "dst-buffer-num", dst_buffer_num);
1014                                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "auto-colorspace", TRUE);
1015
1016                                 gst_caps_unref (caps);
1017                                 caps = NULL;
1018                         } else {
1019                                 err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1020                                 goto pipeline_creation_error;
1021                         }
1022
1023                         _mmcam_dbg_log("fimcconvert dst-buffer-num %d", dst_buffer_num);
1024                 }
1025
1026                 _mmcamcorder_conf_get_value_int(hcamcorder->conf_main,
1027                                                 CONFIGURE_CATEGORY_MAIN_RECORD,
1028                                                 "UseVideoEncoderQueue",
1029                                                 &use_venc_queue);
1030                 if (use_venc_queue) {
1031                         _MMCAMCORDER_ENCODEBIN_ELMGET(sc, _MMCAMCORDER_ENCSINK_VENC_QUE, "use-venc-queue", err);
1032                 }
1033         }
1034
1035         if (sc->audio_disable == FALSE &&
1036             profile != MM_CAMCORDER_ENCBIN_PROFILE_IMAGE) {
1037                 int use_aenc_queue =0;
1038                 const gchar* int_formats = "{S8, U8, "
1039                                    "S16LE, S16BE, U16LE, U16BE, "
1040                                    "S24_32LE, S24_32BE, U24_32LE, U24_32BE, "
1041                                    "S32LE, S32BE, U32LE, U32BE, "
1042                                    "S24LE, S24BE, U24LE, U24BE, "
1043                                    "S20LE, S20BE, U20LE, U20BE, "
1044                                    "S18LE, S18BE, U18LE, U18BE}";
1045
1046                 AudioencElement = _mmcamcorder_get_type_element(handle, MM_CAM_AUDIO_ENCODER);
1047                 if (!AudioencElement) {
1048                         _mmcam_dbg_err("Fail to get type element");
1049                         err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1050                         goto pipeline_creation_error;
1051                 }
1052
1053                 _mmcamcorder_conf_get_value_element_name(AudioencElement, &gst_element_aenc_name);
1054
1055                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "aenc-name", gst_element_aenc_name);
1056                 _MMCAMCORDER_ENCODEBIN_ELMGET(sc, _MMCAMCORDER_ENCSINK_AENC, "audio-encode", err);
1057
1058                 if (audio_enc == MM_AUDIO_CODEC_AMR && channel == 2) {
1059                         caps = gst_caps_new_simple("audio/x-raw",
1060                                                    "channels", G_TYPE_INT, 1,
1061                                                    "format", G_TYPE_STRING, int_formats,
1062                                                    NULL);
1063                         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "auto-audio-convert", TRUE);
1064                         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "acaps", caps);
1065                         gst_caps_unref (caps);
1066                         caps = NULL;
1067                 }
1068
1069                 if (audio_enc == MM_AUDIO_CODEC_OGG) {
1070                         caps = gst_caps_new_simple("audio/x-raw", "format", G_TYPE_STRING, int_formats, NULL);
1071                         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "auto-audio-convert", TRUE);
1072                         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "acaps", caps);
1073                         gst_caps_unref (caps);
1074                         caps = NULL;
1075                         _mmcam_dbg_log("***** MM_AUDIO_CODEC_OGG : setting audio/x-raw-int ");
1076                 }
1077
1078                 //AAC is marked as experimental,
1079                 //so compliance level must be set accordingly
1080         if (audio_enc == MM_AUDIO_CODEC_AAC)
1081         {
1082             MMCAMCORDER_G_OBJECT_SET( sc->element[_MMCAMCORDER_ENCSINK_AENC].gst, "compliance", -2);
1083         }
1084
1085                 _mmcamcorder_conf_get_value_int(hcamcorder->conf_main,
1086                                                 CONFIGURE_CATEGORY_MAIN_RECORD,
1087                                                 "UseAudioEncoderQueue",
1088                                                 &use_aenc_queue);
1089                 if (use_aenc_queue) {
1090                         _MMCAMCORDER_ENCODEBIN_ELMGET(sc, _MMCAMCORDER_ENCSINK_AENC_QUE, "use-aenc-queue", err);
1091                         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_ENCSINK_AENC_QUE].gst,"max-size-time",G_GUINT64_CONSTANT(0));
1092                         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_ENCSINK_AENC_QUE].gst,"max-size-buffers",0);
1093                 }
1094         }
1095
1096         if (profile == MM_CAMCORDER_ENCBIN_PROFILE_IMAGE) {
1097                 ImageencElement = _mmcamcorder_get_type_element(handle, MM_CAM_IMAGE_ENCODER);
1098                 if (!ImageencElement) {
1099                         _mmcam_dbg_err("Fail to get type element");
1100                         err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1101                         goto pipeline_creation_error;
1102                 }
1103
1104                 _mmcamcorder_conf_get_value_element_name(ImageencElement, &gst_element_ienc_name);
1105
1106                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "ienc-name", gst_element_ienc_name);
1107                 _MMCAMCORDER_ENCODEBIN_ELMGET(sc, _MMCAMCORDER_ENCSINK_IENC, "image-encode", err);
1108         }
1109
1110         /* Mux */
1111         if (profile != MM_CAMCORDER_ENCBIN_PROFILE_IMAGE) {
1112                 MuxElement = _mmcamcorder_get_type_element(handle, MM_CAM_FILE_FORMAT);
1113                 if (!MuxElement) {
1114                         _mmcam_dbg_err("Fail to get type element");
1115                         err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1116                         goto pipeline_creation_error;
1117                 }
1118
1119                 _mmcamcorder_conf_get_value_element_name(MuxElement, &gst_element_mux_name);
1120
1121                 __ta__("                mux",
1122                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "mux-name", gst_element_mux_name);
1123                 );
1124                 _MMCAMCORDER_ENCODEBIN_ELMGET(sc, _MMCAMCORDER_ENCSINK_MUX, "mux", err);
1125
1126                 _mmcamcorder_conf_set_value_element_property(sc->element[_MMCAMCORDER_ENCSINK_MUX].gst, MuxElement);
1127         }
1128
1129         /* Sink */
1130         if (profile != MM_CAMCORDER_ENCBIN_PROFILE_IMAGE) {
1131                 /* for recording */
1132                 _mmcamcorder_conf_get_element(hcamcorder->conf_main,
1133                                               CONFIGURE_CATEGORY_MAIN_RECORD,
1134                                               "RecordsinkElement",
1135                                               &RecordsinkElement );
1136                 _mmcamcorder_conf_get_value_element_name(RecordsinkElement, &gst_element_rsink_name);
1137
1138                 __ta__("                Recordsink_sink",
1139                 _MMCAMCORDER_ELEMENT_MAKE(sc, _MMCAMCORDER_ENCSINK_SINK, gst_element_rsink_name, NULL, element_list, err);
1140                 );
1141
1142                 _mmcamcorder_conf_set_value_element_property(sc->element[_MMCAMCORDER_ENCSINK_SINK].gst, RecordsinkElement);
1143         } else {
1144                 /* for stillshot */
1145                 __ta__("                fakesink",
1146                 _MMCAMCORDER_ELEMENT_MAKE(sc, _MMCAMCORDER_ENCSINK_SINK, "fakesink", NULL, element_list, err);
1147                 );
1148         }
1149
1150         if (profile == MM_CAMCORDER_ENCBIN_PROFILE_VIDEO) {
1151                 /* video encoder attribute setting */
1152                 if (v_bitrate > 0) {
1153                         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_ENCSINK_VENC].gst, "bitrate", v_bitrate);
1154                 } else {
1155                         _mmcam_dbg_warn("video bitrate is too small[%d], so skip setting. Use DEFAULT value.", v_bitrate);
1156                 }
1157                 /*MMCAMCORDER_G_OBJECT_SET ((sc->element[_MMCAMCORDER_ENCSINK_VENC].gst),"hw-accel", v_hw);*/
1158                 _mmcamcorder_conf_set_value_element_property(sc->element[_MMCAMCORDER_ENCSINK_VENC].gst, VideoencElement);
1159         }
1160
1161         if (sc->audio_disable == FALSE &&
1162             profile != MM_CAMCORDER_ENCBIN_PROFILE_IMAGE) {
1163                 /* audio encoder attribute setting */
1164                 if (a_bitrate > 0) {
1165                         switch (audio_enc) {
1166                         case MM_AUDIO_CODEC_AMR:
1167                                 result = __mmcamcorder_get_amrnb_bitrate_mode(a_bitrate);
1168                                 _mmcam_dbg_log("Set AMR encoder[%s] mode [%d]", gst_element_aenc_name, result);
1169                                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_ENCSINK_AENC].gst, "band-mode", result);
1170                                 break;
1171                         case MM_AUDIO_CODEC_AAC:
1172                                 _mmcam_dbg_log("Set AAC encoder[%s] bitrate [%d]", gst_element_aenc_name, a_bitrate);
1173                                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_ENCSINK_AENC].gst, "bitrate", a_bitrate);
1174                                 break;
1175                         default:
1176                                 _mmcam_dbg_log("Audio codec is not AMR or AAC... you need to implement setting function for audio encoder bit-rate");
1177                                 break;
1178                         }
1179                 } else {
1180                         _mmcam_dbg_warn("Setting bitrate is too small, so skip setting. Use DEFAULT value.");
1181                 }
1182
1183                 _mmcamcorder_conf_set_value_element_property(sc->element[_MMCAMCORDER_ENCSINK_AENC].gst, AudioencElement);
1184         }
1185
1186         _mmcam_dbg_log("Element creation complete");
1187
1188         /* Add element to bin */
1189         if (!_mmcamcorder_add_elements_to_bin(GST_BIN(sc->element[_MMCAMCORDER_ENCSINK_BIN].gst), element_list)) {
1190                 _mmcam_dbg_err("element add error.");
1191                 err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1192                 goto pipeline_creation_error;
1193         }
1194
1195         _mmcam_dbg_log("Element add complete");
1196
1197         if (profile == MM_CAMCORDER_ENCBIN_PROFILE_VIDEO) {
1198                 pad = gst_element_get_request_pad(sc->element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "video");
1199                 if (gst_element_add_pad(sc->element[_MMCAMCORDER_ENCSINK_BIN].gst, gst_ghost_pad_new("video_sink0", pad)) < 0) {
1200                         gst_object_unref(pad);
1201                         pad = NULL;
1202                         _mmcam_dbg_err("failed to create ghost video_sink0 on _MMCAMCORDER_ENCSINK_BIN.");
1203                         err = MM_ERROR_CAMCORDER_GST_LINK;
1204                         goto pipeline_creation_error;
1205                 }
1206                 gst_object_unref(pad);
1207                 pad = NULL;
1208
1209                 if (sc->audio_disable == FALSE) {
1210                         pad = gst_element_get_request_pad(sc->element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "audio");
1211                         if (gst_element_add_pad(sc->element[_MMCAMCORDER_ENCSINK_BIN].gst, gst_ghost_pad_new("audio_sink0", pad)) < 0) {
1212                                 gst_object_unref(pad);
1213                                 pad = NULL;
1214                                 _mmcam_dbg_err("failed to create ghost audio_sink0 on _MMCAMCORDER_ENCSINK_BIN.");
1215                                 err = MM_ERROR_CAMCORDER_GST_LINK;
1216                                 goto pipeline_creation_error;
1217                         }
1218                         gst_object_unref(pad);
1219                         pad = NULL;
1220                 }
1221         } else if (profile == MM_CAMCORDER_ENCBIN_PROFILE_AUDIO) {
1222                 pad = gst_element_get_request_pad(sc->element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "audio");
1223                 if (gst_element_add_pad(sc->element[_MMCAMCORDER_ENCSINK_BIN].gst, gst_ghost_pad_new("audio_sink0", pad)) < 0) {
1224                         gst_object_unref(pad);
1225                         pad = NULL;
1226                         _mmcam_dbg_err("failed to create ghost audio_sink0 on _MMCAMCORDER_ENCSINK_BIN.");
1227                         err = MM_ERROR_CAMCORDER_GST_LINK;
1228                         goto pipeline_creation_error;
1229                 }
1230                 gst_object_unref(pad);
1231                 pad = NULL;
1232         } else {
1233                 /* for stillshot */
1234                 pad = gst_element_get_request_pad(sc->element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "image");
1235                 if (gst_element_add_pad(sc->element[_MMCAMCORDER_ENCSINK_BIN].gst, gst_ghost_pad_new("image_sink0", pad)) < 0) {
1236                         gst_object_unref(pad);
1237                         pad = NULL;
1238                         _mmcam_dbg_err("failed to create ghost image_sink0 on _MMCAMCORDER_ENCSINK_BIN.");
1239                         err = MM_ERROR_CAMCORDER_GST_LINK;
1240                         goto pipeline_creation_error;
1241                 }
1242                 gst_object_unref(pad);
1243                 pad = NULL;
1244         }
1245
1246         _mmcam_dbg_log("Get pad complete");
1247
1248         /* Link internal element */
1249         if (!_mmcamcorder_link_elements(element_list)) {
1250                 _mmcam_dbg_err("element link error.");
1251                 err = MM_ERROR_CAMCORDER_GST_LINK;
1252                 goto pipeline_creation_error;
1253         }
1254
1255         if (element_list) {
1256                 g_list_free(element_list);
1257                 element_list = NULL;
1258         }
1259
1260         return MM_ERROR_NONE;
1261
1262 pipeline_creation_error :
1263         _MMCAMCORDER_ELEMENT_REMOVE(sc, _MMCAMCORDER_ENCSINK_ENCBIN);
1264         _MMCAMCORDER_ELEMENT_REMOVE(sc, _MMCAMCORDER_ENCSINK_VENC);
1265         _MMCAMCORDER_ELEMENT_REMOVE(sc, _MMCAMCORDER_ENCSINK_AENC);
1266         _MMCAMCORDER_ELEMENT_REMOVE(sc, _MMCAMCORDER_ENCSINK_IENC);
1267         _MMCAMCORDER_ELEMENT_REMOVE(sc, _MMCAMCORDER_ENCSINK_MUX);
1268         _MMCAMCORDER_ELEMENT_REMOVE(sc, _MMCAMCORDER_ENCSINK_SINK);
1269         _MMCAMCORDER_ELEMENT_REMOVE(sc, _MMCAMCORDER_ENCSINK_BIN);
1270         if (element_list) {
1271                 g_list_free(element_list);
1272                 element_list = NULL;
1273         }
1274
1275         return err;
1276 }
1277
1278
1279 int _mmcamcorder_create_stillshotsink_bin(MMHandleType handle)
1280 {
1281         int err = MM_ERROR_NONE;
1282         int capture_width  = 0;
1283         int capture_height = 0;
1284         int UseCaptureMode = 0;
1285         char *gst_element_ienc_name = NULL;
1286         char *gst_element_videoscale_name = NULL;
1287
1288         GList *element_list = NULL;
1289         GstPad *pad = NULL;
1290
1291         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1292         _MMCamcorderSubContext *sc = NULL;
1293         _MMCamcorderGstElement *first_element = NULL;
1294         type_element* ImageencElement   = NULL;
1295         type_element* VideoscaleElement = NULL;
1296
1297         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1298
1299         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
1300         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1301         mmf_return_val_if_fail(sc->element, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1302
1303         _mmcam_dbg_log("");
1304
1305         /* Check existence */
1306         if (sc->element[_MMCAMCORDER_STILLSHOTSINK_BIN].gst) {
1307                 if (((GObject *)sc->element[_MMCAMCORDER_STILLSHOTSINK_BIN].gst)->ref_count > 0) {
1308                         gst_object_unref(sc->element[_MMCAMCORDER_STILLSHOTSINK_BIN].gst);
1309                 }
1310                 _mmcam_dbg_log("_MMCAMCORDER_STILLSHOTSINK_BIN is Already existed. Unref once...");
1311         }
1312
1313         /* Check element availability */
1314         ImageencElement = _mmcamcorder_get_type_element(handle, MM_CAM_IMAGE_ENCODER);
1315         if (!ImageencElement) {
1316                 _mmcam_dbg_err("Fail to get type element");
1317                 err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1318                 goto pipeline_creation_error;
1319         }
1320
1321         _mmcamcorder_conf_get_value_element_name(ImageencElement, &gst_element_ienc_name);
1322
1323         _mmcamcorder_conf_get_value_int(hcamcorder->conf_main,
1324                                         CONFIGURE_CATEGORY_MAIN_CAPTURE,
1325                                         "UseCaptureMode",
1326                                         &UseCaptureMode);
1327
1328         /* Create bin element */
1329         __ta__("                stillshotsink_bin",
1330         _MMCAMCORDER_BIN_MAKE(sc, _MMCAMCORDER_STILLSHOTSINK_BIN, "stillshotsink_bin", err);
1331         );
1332
1333         /* Create child element */
1334         __ta__("                stillshotsink_queue",
1335         _MMCAMCORDER_ELEMENT_MAKE(sc, _MMCAMCORDER_STILLSHOTSINK_QUE, "queue", NULL, element_list, err);
1336         );
1337         __ta__("                stillshotsink_toggle",
1338         _MMCAMCORDER_ELEMENT_MAKE(sc, _MMCAMCORDER_STILLSHOTSINK_TOGGLE, "toggle", NULL, element_list, err);
1339         );
1340
1341         if (UseCaptureMode) {
1342                 _mmcamcorder_conf_get_element(hcamcorder->conf_main,
1343                                               CONFIGURE_CATEGORY_MAIN_CAPTURE,
1344                                               "VideoscaleElement",
1345                                               &VideoscaleElement);
1346                 _mmcamcorder_conf_get_value_element_name(VideoscaleElement, &gst_element_videoscale_name);
1347
1348                 __ta__("                stillshotsink_videoscale",
1349                 _MMCAMCORDER_ELEMENT_MAKE(sc, _MMCAMCORDER_STILLSHOTSINK_SCALE, "gst_element_videoscale_name", NULL, element_list, err);
1350                 );
1351                 __ta__("                stillshotsink_videocrop",
1352                 _MMCAMCORDER_ELEMENT_MAKE(sc, _MMCAMCORDER_STILLSHOTSINK_CROP, "videocrop", NULL, element_list, err);
1353                 );
1354                 __ta__("                stillshotsink_capsfiltern",
1355                 _MMCAMCORDER_ELEMENT_MAKE(sc, _MMCAMCORDER_STILLSHOTSINK_FILT, "capsfilter", NULL, element_list, err);
1356                 );
1357         }
1358
1359         __ta__("                image encoder",
1360         _MMCAMCORDER_ELEMENT_MAKE(sc, _MMCAMCORDER_STILLSHOTSINK_ENC, gst_element_ienc_name, NULL, element_list, err);
1361         );
1362
1363         __ta__("                fakesink",
1364         _MMCAMCORDER_ELEMENT_MAKE(sc, _MMCAMCORDER_STILLSHOTSINK_SINK, "fakesink", NULL, element_list, err);
1365         );
1366
1367         if (UseCaptureMode) {
1368                 _mmcamcorder_conf_set_value_element_property(sc->element[_MMCAMCORDER_STILLSHOTSINK_SCALE].gst, VideoscaleElement);
1369                 
1370                 /* Set property */
1371                 mm_camcorder_get_attributes(handle, NULL,
1372                                             MMCAM_CAPTURE_WIDTH, &capture_width,
1373                                             MMCAM_CAPTURE_HEIGHT, &capture_height,
1374                                             NULL);
1375
1376                 __ta__("                _mmcamcorder_set_resize_property",
1377                 err = _mmcamcorder_set_resize_property(handle, capture_width, capture_height);
1378                 );
1379                 if (err != MM_ERROR_NONE) {
1380                         //unref??
1381                         _mmcam_dbg_log("Set resize property failed.");
1382                         goto pipeline_creation_error;
1383                 }
1384         }
1385
1386         if (!_mmcamcorder_add_elements_to_bin(GST_BIN(sc->element[_MMCAMCORDER_STILLSHOTSINK_BIN].gst), element_list)) {
1387                 _mmcam_dbg_err( "element add error." );
1388                 err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1389                 goto pipeline_creation_error;
1390         }
1391
1392         if (!_mmcamcorder_link_elements(element_list)) {
1393                 _mmcam_dbg_err( "element link error." );
1394                 err = MM_ERROR_CAMCORDER_GST_LINK;
1395                 goto pipeline_creation_error;
1396         }
1397
1398         first_element = (_MMCamcorderGstElement*)(element_list->data);
1399
1400         pad = gst_element_get_static_pad(first_element->gst, "sink");
1401         if (gst_element_add_pad( sc->element[_MMCAMCORDER_STILLSHOTSINK_BIN].gst, gst_ghost_pad_new("sink", pad)) < 0) {
1402                 gst_object_unref(pad);
1403                 pad = NULL;
1404                 _mmcam_dbg_err("failed to create ghost pad on _MMCAMCORDER_STILLSHOTSINK_BIN.");
1405                 err = MM_ERROR_CAMCORDER_GST_LINK;
1406                 goto pipeline_creation_error;
1407         }
1408         gst_object_unref(pad);
1409         pad = NULL;
1410
1411         if (element_list) {
1412                 g_list_free(element_list);
1413                 element_list = NULL;
1414         }
1415
1416         return MM_ERROR_NONE;
1417
1418 pipeline_creation_error :
1419         _MMCAMCORDER_ELEMENT_REMOVE(sc, _MMCAMCORDER_STILLSHOTSINK_QUE);
1420         _MMCAMCORDER_ELEMENT_REMOVE(sc, _MMCAMCORDER_STILLSHOTSINK_TOGGLE);
1421         _MMCAMCORDER_ELEMENT_REMOVE(sc, _MMCAMCORDER_STILLSHOTSINK_CROP);
1422         _MMCAMCORDER_ELEMENT_REMOVE(sc, _MMCAMCORDER_STILLSHOTSINK_FILT);
1423         _MMCAMCORDER_ELEMENT_REMOVE(sc, _MMCAMCORDER_STILLSHOTSINK_SCALE);
1424         _MMCAMCORDER_ELEMENT_REMOVE(sc, _MMCAMCORDER_STILLSHOTSINK_ENC);
1425         _MMCAMCORDER_ELEMENT_REMOVE(sc, _MMCAMCORDER_STILLSHOTSINK_SINK);
1426         _MMCAMCORDER_ELEMENT_REMOVE(sc, _MMCAMCORDER_STILLSHOTSINK_BIN);
1427         if (element_list) {
1428                 g_list_free(element_list);
1429                 element_list = NULL;
1430         }
1431
1432         return err;
1433 }
1434
1435
1436 int _mmcamcorder_create_preview_pipeline(MMHandleType handle)
1437 {
1438         int err = MM_ERROR_NONE;
1439
1440         GstPad *srcpad = NULL;
1441         GstPad *sinkpad = NULL;
1442         GstBus *bus = NULL;
1443
1444         mmf_camcorder_t *hcamcorder= MMF_CAMCORDER(handle);
1445         _MMCamcorderSubContext *sc = NULL;
1446
1447         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1448
1449         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
1450         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1451         mmf_return_val_if_fail(sc->element, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1452
1453         _mmcam_dbg_log("");
1454
1455         /** Create gstreamer element **/
1456         /* Main pipeline */
1457         _MMCAMCORDER_PIPELINE_MAKE(sc, _MMCAMCORDER_MAIN_PIPE, "camcorder_pipeline", err);
1458
1459         /* Sub pipeline */
1460         __ta__("            __mmcamcorder_create_videosrc_bin",
1461         err = _mmcamcorder_create_videosrc_bin((MMHandleType)hcamcorder);
1462         );
1463         if (err != MM_ERROR_NONE ) {
1464                 goto pipeline_creation_error;
1465         }
1466
1467         __ta__("            _mmcamcorder_create_videosink_bin",
1468         err = _mmcamcorder_create_videosink_bin((MMHandleType)hcamcorder);
1469         );
1470         if (err != MM_ERROR_NONE ) {
1471                 goto pipeline_creation_error;
1472         }
1473
1474         gst_bin_add_many(GST_BIN(sc->element[_MMCAMCORDER_MAIN_PIPE].gst),
1475                          sc->element[_MMCAMCORDER_VIDEOSRC_BIN].gst,
1476                          sc->element[_MMCAMCORDER_VIDEOSINK_BIN].gst,
1477                          NULL);
1478
1479         /* Link each element */
1480         srcpad = gst_element_get_static_pad(sc->element[_MMCAMCORDER_VIDEOSRC_BIN].gst, "src0");
1481         sinkpad = gst_element_get_static_pad(sc->element[_MMCAMCORDER_VIDEOSINK_BIN].gst, "sink");
1482         _MM_GST_PAD_LINK_UNREF(srcpad, sinkpad, err, pipeline_creation_error);
1483
1484         /* Set data probe function */
1485         srcpad = gst_element_get_static_pad(sc->element[_MMCAMCORDER_VIDEOSRC_QUE].gst, "src");
1486         MMCAMCORDER_ADD_BUFFER_PROBE(srcpad, _MMCAMCORDER_HANDLER_PREVIEW,
1487                                      __mmcamcorder_video_dataprobe_preview, hcamcorder);
1488         gst_object_unref(srcpad);
1489         srcpad = NULL;
1490
1491         bus = gst_pipeline_get_bus(GST_PIPELINE(sc->element[_MMCAMCORDER_MAIN_PIPE].gst));
1492
1493         /* Register message callback */
1494         hcamcorder->pipeline_cb_event_id = gst_bus_add_watch(bus, _mmcamcorder_pipeline_cb_message, (gpointer)hcamcorder);
1495
1496         /* set sync handler */
1497     gst_bus_set_sync_handler(bus, _mmcamcorder_pipeline_bus_sync_callback, (gpointer)hcamcorder, NULL);
1498
1499         gst_object_unref(bus);
1500         bus = NULL;
1501
1502         /* Below signals are meaningfull only when video source is using. */
1503         MMCAMCORDER_SIGNAL_CONNECT(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst,
1504                                    _MMCAMCORDER_HANDLER_PREVIEW,
1505                                    "nego-complete",
1506                                    _mmcamcorder_negosig_handler,
1507                                    hcamcorder);
1508
1509         return MM_ERROR_NONE;
1510
1511 pipeline_creation_error:
1512         _MMCAMCORDER_ELEMENT_REMOVE(sc, _MMCAMCORDER_VIDEOSRC_BIN);
1513         _MMCAMCORDER_ELEMENT_REMOVE(sc, _MMCAMCORDER_MAIN_PIPE);
1514         return err;
1515 }
1516
1517
1518 void _mmcamcorder_negosig_handler(GstElement *videosrc, MMHandleType handle)
1519 {
1520         mmf_camcorder_t *hcamcorder= MMF_CAMCORDER(handle);
1521         _MMCamcorderSubContext *sc = NULL;
1522
1523         _mmcam_dbg_log("");
1524
1525         mmf_return_if_fail(hcamcorder);
1526         mmf_return_if_fail(hcamcorder->sub_context);
1527         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
1528
1529         /* kernel was modified. No need to set.
1530         _mmcamcorder_set_attribute_to_camsensor(handle);
1531         */
1532
1533         if (sc->cam_stability_count != _MMCAMCORDER_CAMSTABLE_COUNT) {
1534                 sc->cam_stability_count = _MMCAMCORDER_CAMSTABLE_COUNT;
1535         }
1536
1537         if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
1538                 _MMCamcorderImageInfo *info = NULL;
1539                 info = sc->info_image;
1540                 if (info->resolution_change == TRUE) {
1541                         _mmcam_dbg_log("open toggle of stillshot sink.");
1542                         MMCAMCORDER_G_OBJECT_SET( sc->element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "block", FALSE);
1543                         info->resolution_change = FALSE;
1544                 }
1545         }
1546 }
1547
1548
1549 int _mmcamcorder_videosink_window_set(MMHandleType handle, type_element* VideosinkElement)
1550 {
1551         int err = MM_ERROR_NONE;
1552         int size = 0;
1553         int retx = 0;
1554         int rety = 0;
1555         int retwidth = 0;
1556         int retheight = 0;
1557         int visible = 0;
1558         int rotation = MM_DISPLAY_ROTATION_NONE;
1559         int rotation_degree = 0;
1560         int flip = MM_FLIP_NONE;
1561         int display_mode = MM_DISPLAY_MODE_DEFAULT;
1562         int display_geometry_method = MM_DISPLAY_METHOD_LETTER_BOX;
1563         int origin_size = 0;
1564         int zoom_attr = 0;
1565         float zoom_level = 0;
1566         int do_scaling = FALSE;
1567         int *overlay = NULL;
1568         gulong xid;
1569         char *err_name = NULL;
1570         char *videosink_name = NULL;
1571
1572         GstElement *vsink = NULL;
1573
1574         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1575         _MMCamcorderSubContext *sc = NULL;
1576
1577         _mmcam_dbg_log("");
1578
1579         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1580
1581         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
1582         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1583         mmf_return_val_if_fail(sc->element, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1584         mmf_return_val_if_fail(sc->element[_MMCAMCORDER_VIDEOSINK_SINK].gst, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1585
1586         vsink = sc->element[_MMCAMCORDER_VIDEOSINK_SINK].gst;
1587
1588         /* Get video display information */
1589         __ta__("                videosink get attributes",
1590         err = mm_camcorder_get_attributes(handle, &err_name,
1591                                           MMCAM_DISPLAY_RECT_X, &retx,
1592                                           MMCAM_DISPLAY_RECT_Y, &rety,
1593                                           MMCAM_DISPLAY_RECT_WIDTH, &retwidth,
1594                                           MMCAM_DISPLAY_RECT_HEIGHT, &retheight,
1595                                           MMCAM_DISPLAY_ROTATION, &rotation,
1596                                           MMCAM_DISPLAY_FLIP, &flip,
1597                                           MMCAM_DISPLAY_VISIBLE, &visible,
1598                                           MMCAM_DISPLAY_HANDLE, (void**)&overlay, &size,
1599                                           MMCAM_DISPLAY_MODE, &display_mode,
1600                                           MMCAM_DISPLAY_GEOMETRY_METHOD, &display_geometry_method,
1601                                           MMCAM_DISPLAY_SCALE, &zoom_attr,
1602                                           MMCAM_DISPLAY_EVAS_DO_SCALING, &do_scaling,
1603                                           NULL);
1604         );
1605
1606         _mmcam_dbg_log("(overlay=%p, size=%d)", overlay, size);
1607
1608         _mmcamcorder_conf_get_value_element_name(VideosinkElement, &videosink_name);
1609
1610         /* Set display handle */
1611         if (!strcmp(videosink_name, "xvimagesink") ||
1612             !strcmp(videosink_name, "ximagesink")) {
1613                 if (overlay) {
1614                         xid = *overlay;
1615                         _mmcam_dbg_log("xid = %lu )", xid);
1616             gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(vsink), xid);
1617                 } else {
1618                         _mmcam_dbg_warn("Handle is NULL. Set xid as 0.. but, it's not recommended.");
1619             gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(vsink), 0);
1620                 }
1621
1622                 _mmcam_dbg_log("%s set: display_geometry_method[%d],origin-size[%d],visible[%d],rotate[enum:%d]",
1623                                videosink_name, display_geometry_method, origin_size, visible, rotation);
1624         } else if (!strcmp(videosink_name, "evasimagesink") ||
1625                    !strcmp(videosink_name, "evaspixmapsink")) {
1626                 _mmcam_dbg_log("videosink : %s, handle : %p", videosink_name, overlay);
1627                 if (overlay) {
1628                         MMCAMCORDER_G_OBJECT_SET(vsink, "evas-object", overlay);
1629                         MMCAMCORDER_G_OBJECT_SET(vsink, "origin-size", !do_scaling);
1630                 } else {
1631                         _mmcam_dbg_err("display handle(eavs object) is NULL");
1632                         return MM_ERROR_CAMCORDER_INVALID_ARGUMENT;
1633                 }
1634         } else {
1635                 _mmcam_dbg_warn("Who are you?? (Videosink: %s)", videosink_name);
1636         }
1637
1638         /* Set attribute */
1639         if (!strcmp(videosink_name, "xvimagesink") ||
1640             !strcmp(videosink_name, "evaspixmapsink")) {
1641
1642                 if (!strcmp(videosink_name, "xvimagesink")) {
1643                         /* set rotation */
1644                         switch (rotation) {
1645                         case MM_DISPLAY_ROTATION_NONE :
1646                                 rotation_degree = 0;
1647                                 break;
1648                         case MM_DISPLAY_ROTATION_90 :
1649                                 rotation_degree = 1;
1650                                 break;
1651                         case MM_DISPLAY_ROTATION_180 :
1652                                 rotation_degree = 2;
1653                                 break;
1654                         case MM_DISPLAY_ROTATION_270 :
1655                                 rotation_degree = 3;
1656                                 break;
1657                         default:
1658                                 _mmcam_dbg_warn("Unsupported rotation value. set as default(0).");
1659                                 rotation_degree = 0;
1660                                 break;
1661                         }
1662                         MMCAMCORDER_G_OBJECT_SET(vsink, "rotate", rotation_degree);
1663
1664                         /* set flip */
1665                         MMCAMCORDER_G_OBJECT_SET(vsink, "flip", flip);
1666
1667                         _mmcam_dbg_log("set videosink[%s] rotate %d, flip %d",
1668                                        videosink_name, rotation_degree, flip);
1669                 }
1670
1671                 switch (zoom_attr) {
1672                 case MM_DISPLAY_SCALE_DEFAULT:
1673                         zoom_level = 1;
1674                         break;
1675                 case MM_DISPLAY_SCALE_DOUBLE_LENGTH:
1676                         zoom_level = 2;
1677                         break;
1678                 case MM_DISPLAY_SCALE_TRIPLE_LENGTH:
1679                         zoom_level = 3;
1680                         break;
1681                 default:
1682                         _mmcam_dbg_warn("Unsupported zoom value. set as default.");
1683                         zoom_level = 1;
1684                         break;
1685                 }
1686
1687                 MMCAMCORDER_G_OBJECT_SET(vsink, "display-geometry-method", display_geometry_method);
1688                 MMCAMCORDER_G_OBJECT_SET(vsink, "display-mode", display_mode);
1689                 MMCAMCORDER_G_OBJECT_SET(vsink, "visible", visible);
1690                 MMCAMCORDER_G_OBJECT_SET(vsink, "zoom", zoom_level);
1691
1692                 if (display_geometry_method == MM_DISPLAY_METHOD_CUSTOM_ROI) {
1693                         g_object_set(vsink,
1694                                      "dst-roi-x", retx,
1695                                      "dst-roi-y", rety,
1696                                      "dst-roi-w", retwidth,
1697                                      "dst-roi-h", retheight,
1698                                      NULL);
1699                 }
1700         }
1701
1702         return MM_ERROR_NONE;
1703 }
1704
1705
1706 int _mmcamcorder_vframe_stablize(MMHandleType handle)
1707 {
1708         mmf_camcorder_t *hcamcorder= MMF_CAMCORDER(handle);
1709         _MMCamcorderSubContext *sc = NULL;
1710
1711         _mmcam_dbg_log("%d", _MMCAMCORDER_CAMSTABLE_COUNT);
1712
1713         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1714
1715         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
1716
1717         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1718
1719         if (sc->cam_stability_count != _MMCAMCORDER_CAMSTABLE_COUNT) {
1720                 sc->cam_stability_count = _MMCAMCORDER_CAMSTABLE_COUNT;
1721         }
1722
1723         return MM_ERROR_NONE;
1724 }
1725
1726 /* Retreive device information and set them to attributes */
1727 gboolean _mmcamcorder_get_device_info(MMHandleType handle)
1728 {
1729         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1730         _MMCamcorderSubContext *sc = NULL;
1731         GstCameraControl *control = NULL;
1732         GstCameraControlExifInfo exif_info = {0,};
1733
1734         mmf_return_val_if_fail(hcamcorder, FALSE);
1735         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
1736
1737         if (sc && sc->element) {
1738                 int err = MM_ERROR_NONE;
1739                 char *err_name = NULL;
1740                 double focal_len = 0.0;
1741
1742                 /* Video input device */
1743                 if (sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst) {
1744                         /* Exif related information */
1745                         control = GST_CAMERA_CONTROL(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst);
1746                         if (control != NULL) {
1747                                 gst_camera_control_get_exif_info(control, &exif_info); //get video input device information
1748                                 focal_len = ((double)exif_info.focal_len_numerator) / ((double) exif_info.focal_len_denominator);
1749                         } else {
1750                                 _mmcam_dbg_err("Fail to get camera control interface!");
1751                                 focal_len = 0.0;
1752                         }
1753                 }
1754
1755                 /* Set values to attributes */
1756                 err = mm_camcorder_set_attributes(handle, &err_name,
1757                                                   MMCAM_CAMERA_FOCAL_LENGTH, focal_len,
1758                                                   NULL);
1759                 if (err != MM_ERROR_NONE) {
1760                         _mmcam_dbg_err("Set attributes error(%s:%x)!", err_name, err);
1761                         if (err_name) {
1762                                 free(err_name);
1763                                 err_name = NULL;
1764                         }
1765                         return FALSE;
1766                 }
1767         } else {
1768                 _mmcam_dbg_warn( "Sub context isn't exist.");
1769                 return FALSE;
1770         }
1771
1772         return TRUE;
1773 }
1774
1775 static guint32 _mmcamcorder_convert_fourcc_to_string(const gchar* format_name)
1776 {
1777     return format_name[0] | (format_name[1] << 8) | (format_name[2] << 16) | (format_name[3] << 24);
1778 }
1779
1780 static GstPadProbeReturn __mmcamcorder_video_dataprobe_preview(GstPad *pad, GstPadProbeInfo *info, gpointer u_data)
1781 {
1782         int current_state = MM_CAMCORDER_STATE_NONE;
1783
1784         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(u_data);
1785         _MMCamcorderSubContext *sc = NULL;
1786         _MMCamcorderKPIMeasure *kpi = NULL;
1787     GstBuffer *buffer = GST_PAD_PROBE_INFO_BUFFER(info);
1788     GstMemory *dataBlock = NULL;
1789     GstMemory *metaBlock = NULL;
1790     GstMapInfo mapinfo = GST_MAP_INFO_INIT;
1791
1792         mmf_return_val_if_fail(buffer, FALSE);
1793     mmf_return_val_if_fail(gst_buffer_n_memory(buffer)  , FALSE);
1794         mmf_return_val_if_fail(hcamcorder, TRUE);
1795
1796         sc = MMF_CAMCORDER_SUBCONTEXT(u_data);
1797         mmf_return_val_if_fail(sc, TRUE);
1798
1799         current_state = hcamcorder->state;
1800
1801         if (sc->drop_vframe > 0) {
1802                 if (sc->pass_first_vframe > 0) {
1803                         sc->pass_first_vframe--;
1804                         _mmcam_dbg_log("Pass video frame by pass_first_vframe");
1805                 } else {
1806                         sc->drop_vframe--;
1807                         _mmcam_dbg_log("Drop video frame by drop_vframe");
1808             return GST_PAD_PROBE_DROP;
1809                 }
1810         } else if (sc->cam_stability_count > 0) {
1811                 sc->cam_stability_count--;
1812                 _mmcam_dbg_log("Drop video frame by cam_stability_count");
1813         return GST_PAD_PROBE_DROP;
1814         }
1815
1816         if (current_state >= MM_CAMCORDER_STATE_PREPARE) {
1817                 int diff_sec;
1818                 int frame_count = 0;
1819                 struct timeval current_video_time;
1820
1821                 kpi = &(sc->kpi);
1822                 if (kpi->init_video_time.tv_sec == kpi->last_video_time.tv_sec &&
1823                     kpi->init_video_time.tv_usec == kpi->last_video_time.tv_usec &&
1824                     kpi->init_video_time.tv_usec  == 0) {
1825                         _mmcam_dbg_log("START to measure FPS");
1826                         gettimeofday(&(kpi->init_video_time), NULL);
1827                 }
1828
1829                 frame_count = ++(kpi->video_framecount);
1830
1831                 gettimeofday(&current_video_time, NULL);
1832                 diff_sec = current_video_time.tv_sec - kpi->last_video_time.tv_sec;
1833                 if (diff_sec != 0) {
1834                         kpi->current_fps = (frame_count - kpi->last_framecount) / diff_sec;
1835                         if ((current_video_time.tv_sec - kpi->init_video_time.tv_sec) != 0) {
1836                                 int framecount = kpi->video_framecount;
1837                                 int elased_sec = current_video_time.tv_sec - kpi->init_video_time.tv_sec;
1838                                 kpi->average_fps = framecount / elased_sec;
1839                         }
1840
1841                         kpi->last_framecount = frame_count;
1842                         kpi->last_video_time.tv_sec = current_video_time.tv_sec;
1843                         kpi->last_video_time.tv_usec = current_video_time.tv_usec;
1844                         /*
1845                         _mmcam_dbg_log("current fps(%d), average(%d)", kpi->current_fps, kpi->average_fps);
1846                         */
1847                 }
1848         }
1849
1850         /* video stream callback */
1851         if (hcamcorder->vstream_cb && buffer) {
1852                 GstCaps *caps = NULL;
1853                 GstStructure *structure = NULL;
1854                 int state = MM_CAMCORDER_STATE_NULL;
1855                 unsigned int fourcc = 0;
1856                 MMCamcorderVideoStreamDataType stream;
1857
1858                 state = _mmcamcorder_get_state((MMHandleType)hcamcorder);
1859                 if (state < MM_CAMCORDER_STATE_PREPARE) {
1860                         _mmcam_dbg_warn("Not ready for stream callback");
1861             return GST_PAD_PROBE_OK;
1862                 }
1863
1864         caps = gst_pad_get_current_caps(pad);
1865                 if (caps == NULL) {
1866                         _mmcam_dbg_warn( "Caps is NULL." );
1867             return GST_PAD_PROBE_OK;
1868                 }
1869
1870                 /* clear stream data structure */
1871                 memset(&stream, 0x0, sizeof(MMCamcorderVideoStreamDataType));
1872
1873                 structure = gst_caps_get_structure( caps, 0 );
1874                 gst_structure_get_int(structure, "width", &(stream.width));
1875                 gst_structure_get_int(structure, "height", &(stream.height));
1876                 fourcc = _mmcamcorder_convert_fourcc_to_string(gst_structure_get_string(structure, "format"));
1877                 stream.format = _mmcamcorder_get_pixtype(fourcc);
1878                 gst_caps_unref( caps );
1879                 caps = NULL;
1880
1881                 /*
1882                 _mmcam_dbg_log( "Call video steramCb, data[%p], Width[%d],Height[%d], Format[%d]",
1883                                 GST_BUFFER_DATA(buffer), stream.width, stream.height, stream.format );
1884                 */
1885
1886                 if (stream.width == 0 || stream.height == 0) {
1887                         _mmcam_dbg_warn("Wrong condition!!");
1888             return GST_PAD_PROBE_OK;
1889                 }
1890
1891                 /* set size and timestamp */
1892                 dataBlock = gst_buffer_peek_memory(buffer, 0);
1893         stream.length_total = gst_memory_get_sizes(dataBlock, NULL, NULL);
1894                 stream.timestamp = (unsigned int)(GST_BUFFER_TIMESTAMP(buffer)/1000000); /* nano sec -> mili sec */
1895
1896                 /* set data pointers */
1897                 if (stream.format == MM_PIXEL_FORMAT_NV12 || stream.format == MM_PIXEL_FORMAT_I420) {
1898             if (hcamcorder->use_zero_copy_format && gst_buffer_n_memory(buffer) > 1) {
1899                 metaBlock = gst_buffer_peek_memory(buffer, 1);
1900                 gst_memory_map(metaBlock, &mapinfo, GST_MAP_READ);
1901                 SCMN_IMGB *scmn_imgb = (SCMN_IMGB *)mapinfo.data;
1902
1903                                 if (stream.format == MM_PIXEL_FORMAT_NV12) {
1904                                         stream.data_type = MM_CAM_STREAM_DATA_YUV420SP;
1905                                         stream.num_planes = 2;
1906                                         stream.data.yuv420sp.y = scmn_imgb->a[0];
1907                                         stream.data.yuv420sp.length_y = stream.width * stream.height;
1908                                         stream.data.yuv420sp.uv = scmn_imgb->a[1];
1909                                         stream.data.yuv420sp.length_uv = stream.data.yuv420sp.length_y >> 1;
1910
1911                                         _mmcam_dbg_log("SN12[num_planes:%d] [Y]p:0x%x,size:%d [UV]p:0x%x,size:%d",
1912                                                        stream.num_planes,
1913                                                        stream.data.yuv420sp.y, stream.data.yuv420sp.length_y,
1914                                                        stream.data.yuv420sp.uv, stream.data.yuv420sp.length_uv);
1915                                 } else {
1916                                         stream.data_type = MM_CAM_STREAM_DATA_YUV420P;
1917                                         stream.num_planes = 3;
1918                                         stream.data.yuv420p.y = scmn_imgb->a[0];
1919                                         stream.data.yuv420p.length_y = stream.width * stream.height;
1920                                         stream.data.yuv420p.u = scmn_imgb->a[1];
1921                                         stream.data.yuv420p.length_u = stream.data.yuv420p.length_y >> 2;
1922                                         stream.data.yuv420p.v = scmn_imgb->a[2];
1923                                         stream.data.yuv420p.length_v = stream.data.yuv420p.length_u;
1924
1925                                         _mmcam_dbg_log("S420[num_planes:%d] [Y]p:0x%x,size:%d [U]p:0x%x,size:%d [V]p:0x%x,size:%d",
1926                                                         stream.num_planes,
1927                                                         stream.data.yuv420p.y, stream.data.yuv420p.length_y,
1928                                                         stream.data.yuv420p.u, stream.data.yuv420p.length_u,
1929                                                         stream.data.yuv420p.v, stream.data.yuv420p.length_v);
1930                                 }
1931                         } else {
1932                                 if (stream.format == MM_PIXEL_FORMAT_NV12) {
1933                                         stream.data_type = MM_CAM_STREAM_DATA_YUV420SP;
1934                                         stream.num_planes = 2;
1935                                         gst_memory_map(dataBlock, &mapinfo, GST_MAP_READ);
1936                     stream.data.yuv420sp.y = mapinfo.data;
1937                                         stream.data.yuv420sp.length_y = stream.width * stream.height;
1938                                         stream.data.yuv420sp.uv = stream.data.yuv420sp.y + stream.data.yuv420sp.length_y;
1939                                         stream.data.yuv420sp.length_uv = stream.data.yuv420sp.length_y >> 1;
1940
1941                                         _mmcam_dbg_log("NV12[num_planes:%d] [Y]p:0x%x,size:%d [UV]p:0x%x,size:%d",
1942                                                        stream.num_planes,
1943                                                        stream.data.yuv420sp.y, stream.data.yuv420sp.length_y,
1944                                                        stream.data.yuv420sp.uv, stream.data.yuv420sp.length_uv);
1945                                 } else {
1946                                         stream.data_type = MM_CAM_STREAM_DATA_YUV420P;
1947                                         stream.num_planes = 3;
1948                     gst_memory_map(dataBlock, &mapinfo, GST_MAP_READ);
1949                     stream.data.yuv420p.y = mapinfo.data;
1950                                         stream.data.yuv420p.length_y = stream.width * stream.height;
1951                                         stream.data.yuv420p.u = stream.data.yuv420p.y + stream.data.yuv420p.length_y;
1952                                         stream.data.yuv420p.length_u = stream.data.yuv420p.length_y >> 2;
1953                                         stream.data.yuv420p.v = stream.data.yuv420p.u + stream.data.yuv420p.length_u;
1954                                         stream.data.yuv420p.length_v = stream.data.yuv420p.length_u;
1955
1956                                         _mmcam_dbg_log("I420[num_planes:%d] [Y]p:0x%x,size:%d [U]p:0x%x,size:%d [V]p:0x%x,size:%d",
1957                                                         stream.num_planes,
1958                                                         stream.data.yuv420p.y, stream.data.yuv420p.length_y,
1959                                                         stream.data.yuv420p.u, stream.data.yuv420p.length_u,
1960                                                         stream.data.yuv420p.v, stream.data.yuv420p.length_v);
1961                                 }
1962                         }
1963                 } else {
1964                         if (stream.format == MM_PIXEL_FORMAT_YUYV ||
1965                             stream.format == MM_PIXEL_FORMAT_UYVY ||
1966                             stream.format == MM_PIXEL_FORMAT_422P ||
1967                             stream.format == MM_PIXEL_FORMAT_ITLV_JPEG_UYVY) {
1968                                 stream.data_type = MM_CAM_STREAM_DATA_YUV422;
1969                 gst_memory_map(dataBlock, &mapinfo, GST_MAP_READ);
1970                 stream.data.yuv422.yuv = mapinfo.data;
1971                                 stream.data.yuv422.length_yuv = stream.length_total;
1972                         } else {
1973                                 stream.data_type = MM_CAM_STREAM_DATA_YUV420;
1974                 gst_memory_map(dataBlock, &mapinfo, GST_MAP_READ);
1975                 stream.data.yuv420.yuv = mapinfo.data;
1976                                 stream.data.yuv420.length_yuv = stream.length_total;
1977                         }
1978
1979                         stream.num_planes = 1;
1980
1981                         _mmcam_dbg_log("%c%c%c%c[num_planes:%d] [0]p:0x%x,size:%d",
1982                                        fourcc, fourcc>>8, fourcc>>16, fourcc>>24,
1983                                        stream.num_planes, stream.data.yuv420.yuv, stream.data.yuv420.length_yuv);
1984                 }
1985
1986                 /* call application callback */
1987                 _MMCAMCORDER_LOCK_VSTREAM_CALLBACK(hcamcorder);
1988                 if (hcamcorder->vstream_cb) {
1989                         hcamcorder->vstream_cb(&stream, hcamcorder->vstream_cb_param);
1990                 }
1991                 _MMCAMCORDER_UNLOCK_VSTREAM_CALLBACK(hcamcorder);
1992
1993                 /* Either metaBlock was mapped, or dataBlock, but not both. */
1994                 if (metaBlock) {
1995                     gst_memory_unmap(metaBlock, &mapinfo);
1996                 } else {
1997                     gst_memory_unmap(dataBlock, &mapinfo);
1998                 }
1999         }
2000
2001         /* Do force flush cache */
2002         if (sc->info_image->preview_format == MM_PIXEL_FORMAT_ITLV_JPEG_UYVY) {
2003                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "force-flush-cache", FALSE);
2004         }
2005
2006     return GST_PAD_PROBE_OK;
2007 }
2008
2009
2010 int __mmcamcorder_get_amrnb_bitrate_mode(int bitrate)
2011 {
2012         int result = MM_CAMCORDER_MR475;
2013
2014         if (bitrate< 5150) {
2015                 result = MM_CAMCORDER_MR475; /*AMR475*/
2016         } else if (bitrate< 5900) {
2017                 result = MM_CAMCORDER_MR515; /*AMR515*/
2018         } else if (bitrate < 6700) {
2019                 result = MM_CAMCORDER_MR59; /*AMR59*/
2020         } else if (bitrate< 7400) {
2021                 result = MM_CAMCORDER_MR67; /*AMR67*/
2022         } else if (bitrate< 7950) {
2023                 result = MM_CAMCORDER_MR74; /*AMR74*/
2024         } else if (bitrate < 10200) {
2025                 result = MM_CAMCORDER_MR795; /*AMR795*/
2026         } else if (bitrate < 12200) {
2027                 result = MM_CAMCORDER_MR102; /*AMR102*/
2028         } else {
2029                 result = MM_CAMCORDER_MR122; /*AMR122*/
2030         }
2031
2032         return result;
2033 }
2034
2035 int _mmcamcorder_get_eos_message(MMHandleType handle)
2036 {
2037         double elapsed = 0.0;
2038
2039         GstMessage *gMessage = NULL;
2040         GstBus *bus = NULL;
2041         GstClockTime timeout = 1 * GST_SECOND; /* maximum waiting time */
2042         GTimer *timer = NULL;
2043
2044         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
2045         _MMCamcorderSubContext *sc = NULL;
2046
2047         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2048         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2049
2050         _mmcam_dbg_log("");
2051
2052         bus = gst_pipeline_get_bus(GST_PIPELINE(sc->element[_MMCAMCORDER_MAIN_PIPE].gst));
2053         timer = g_timer_new();
2054
2055         if (sc && !(sc->bget_eos)) {
2056                  while (1) {
2057                         elapsed = g_timer_elapsed(timer, NULL);
2058
2059                         /*_mmcam_dbg_log("elapsed:%f sec", elapsed);*/
2060
2061                         if (elapsed > _MMCAMCORDER_WAIT_EOS_TIME) {
2062                                 _mmcam_dbg_warn("Timeout. EOS isn't received.");
2063                                  g_timer_destroy(timer);
2064                                  gst_object_unref(bus);
2065                                  return MM_ERROR_CAMCORDER_RESPONSE_TIMEOUT;
2066                         }
2067
2068                         gMessage = gst_bus_timed_pop (bus, timeout);
2069                         if (gMessage != NULL) {
2070                                 _mmcam_dbg_log("Get message(%x).", GST_MESSAGE_TYPE(gMessage));
2071                                 _mmcamcorder_pipeline_cb_message(bus, gMessage, (void*)hcamcorder);
2072
2073                                 if (GST_MESSAGE_TYPE(gMessage) == GST_MESSAGE_EOS || sc->bget_eos) {
2074                                         gst_message_unref(gMessage);
2075                                         break;
2076                                 }
2077                                 gst_message_unref(gMessage);
2078                         } else {
2079                                 _mmcam_dbg_log("timeout of gst_bus_timed_pop()");
2080                                 if (sc->bget_eos) {
2081                                         _mmcam_dbg_log("Get EOS in another thread.");
2082                                         break;
2083                                 }
2084                         }
2085                 }
2086         }
2087
2088          g_timer_destroy(timer);
2089          gst_object_unref(bus);
2090
2091         _mmcam_dbg_log("END");
2092
2093          return MM_ERROR_NONE;
2094 }
2095
2096
2097 void _mmcamcorder_remove_element_handle(MMHandleType handle, int first_elem, int last_elem)
2098 {
2099         int i = 0;
2100         _MMCamcorderSubContext *sc = NULL;
2101
2102         mmf_return_if_fail(handle);
2103
2104         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2105         mmf_return_if_fail(sc);
2106         mmf_return_if_fail(sc->element);
2107         mmf_return_if_fail((first_elem > 0) && (last_elem > 0) && (last_elem > first_elem));
2108
2109         _mmcam_dbg_log("");
2110
2111         for (i = first_elem ; i <= last_elem ; i++) {
2112                 sc->element[i].gst = NULL;
2113                 sc->element[i].id = _MMCAMCORDER_NONE;
2114         }
2115
2116         return;
2117 }
2118
2119
2120 int _mmcamcorder_check_audiocodec_fileformat_compatibility(MMHandleType handle)
2121 {
2122         int err = MM_ERROR_NONE;
2123         int audio_codec = MM_AUDIO_CODEC_INVALID;
2124         int file_format = MM_FILE_FORMAT_INVALID;
2125
2126         char *err_name = NULL;
2127
2128         err = mm_camcorder_get_attributes(handle, &err_name,
2129                                           MMCAM_AUDIO_ENCODER, &audio_codec,
2130                                           MMCAM_FILE_FORMAT, &file_format,
2131                                           NULL);
2132         if (err != MM_ERROR_NONE) {
2133                 _mmcam_dbg_warn("Get attrs fail. (%s:%x)", err_name, err);
2134                 SAFE_FREE(err_name);
2135                 return err;
2136         }
2137
2138         /* Check compatibility between audio codec and file format */
2139         if (audio_codec >= MM_AUDIO_CODEC_INVALID && audio_codec < MM_AUDIO_CODEC_NUM &&
2140             file_format >= MM_FILE_FORMAT_INVALID && file_format < MM_FILE_FORMAT_NUM) {
2141                 if (audiocodec_fileformat_compatibility_table[audio_codec][file_format] == 0) {
2142                         _mmcam_dbg_err("Audio codec[%d] and file format[%d] compatibility FAILED.",
2143                                        audio_codec, file_format);
2144                         return MM_ERROR_CAMCORDER_ENCODER_WRONG_TYPE;
2145                 }
2146
2147                 _mmcam_dbg_log("Audio codec[%d] and file format[%d] compatibility SUCCESS.",
2148                                audio_codec, file_format);
2149         } else {
2150                 _mmcam_dbg_err("Audio codec[%d] or file format[%d] is INVALID.",
2151                                audio_codec, file_format);
2152                 return MM_ERROR_CAMCORDER_ENCODER_WRONG_TYPE;
2153         }
2154
2155         return MM_ERROR_NONE;
2156 }
2157
2158
2159 int _mmcamcorder_check_videocodec_fileformat_compatibility(MMHandleType handle)
2160 {
2161         int err = MM_ERROR_NONE;
2162         int video_codec = MM_VIDEO_CODEC_INVALID;
2163         int file_format = MM_FILE_FORMAT_INVALID;
2164
2165         char *err_name = NULL;
2166
2167         err = mm_camcorder_get_attributes(handle, &err_name,
2168                                           MMCAM_VIDEO_ENCODER, &video_codec,
2169                                           MMCAM_FILE_FORMAT, &file_format,
2170                                           NULL);
2171         if (err != MM_ERROR_NONE) {
2172                 _mmcam_dbg_warn("Get attrs fail. (%s:%x)", err_name, err);
2173                 SAFE_FREE(err_name);
2174                 return err;
2175         }
2176
2177         /* Check compatibility between audio codec and file format */
2178         if (video_codec >= MM_VIDEO_CODEC_INVALID && video_codec < MM_VIDEO_CODEC_NUM &&
2179             file_format >= MM_FILE_FORMAT_INVALID && file_format < MM_FILE_FORMAT_NUM) {
2180                 if (videocodec_fileformat_compatibility_table[video_codec][file_format] == 0) {
2181                         _mmcam_dbg_err("Video codec[%d] and file format[%d] compatibility FAILED.",
2182                                        video_codec, file_format);
2183                         return MM_ERROR_CAMCORDER_ENCODER_WRONG_TYPE;
2184                 }
2185
2186                 _mmcam_dbg_log("Video codec[%d] and file format[%d] compatibility SUCCESS.",
2187                                video_codec, file_format);
2188         } else {
2189                 _mmcam_dbg_err("Video codec[%d] or file format[%d] is INVALID.",
2190                                video_codec, file_format);
2191                 return MM_ERROR_CAMCORDER_ENCODER_WRONG_TYPE;
2192         }
2193
2194         return MM_ERROR_NONE;
2195 }
2196
2197
2198 bool _mmcamcorder_set_display_rotation(MMHandleType handle, int display_rotate)
2199 {
2200         char* videosink_name = NULL;
2201
2202         mmf_camcorder_t *hcamcorder = NULL;
2203         _MMCamcorderSubContext *sc = NULL;
2204
2205         hcamcorder = MMF_CAMCORDER(handle);
2206         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2207
2208         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2209         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2210         mmf_return_val_if_fail(sc->element, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2211
2212         if (sc->element[_MMCAMCORDER_VIDEOSINK_SINK].gst) {
2213                 /* Get videosink name */
2214                 _mmcamcorder_conf_get_value_element_name(sc->VideosinkElement, &videosink_name);
2215                 if (!strcmp(videosink_name, "xvimagesink")) {
2216                         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSINK_SINK].gst,
2217                                                  "rotate", display_rotate);
2218                         _mmcam_dbg_log("Set display-rotate [%d] done.", display_rotate);
2219                         return TRUE;
2220                 } else {
2221                         _mmcam_dbg_warn("videosink[%s] does not support DISPLAY_ROTATION.", videosink_name);
2222                         return FALSE;
2223                 }
2224         } else {
2225                 _mmcam_dbg_err("Videosink element is null");
2226                 return FALSE;
2227         }
2228 }
2229
2230
2231 bool _mmcamcorder_set_display_flip(MMHandleType handle, int display_flip)
2232 {
2233         char* videosink_name = NULL;
2234
2235         mmf_camcorder_t *hcamcorder = NULL;
2236         _MMCamcorderSubContext *sc = NULL;
2237
2238         hcamcorder = MMF_CAMCORDER(handle);
2239         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2240
2241         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2242         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2243         mmf_return_val_if_fail(sc->element, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2244
2245         if (sc->element[_MMCAMCORDER_VIDEOSINK_SINK].gst) {
2246                 /* Get videosink name */
2247                 _mmcamcorder_conf_get_value_element_name(sc->VideosinkElement, &videosink_name);
2248                 if (!strcmp(videosink_name, "xvimagesink")) {
2249                         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSINK_SINK].gst,
2250                                                  "flip", display_flip);
2251                         _mmcam_dbg_log("Set display flip [%d] done.", display_flip);
2252                         return TRUE;
2253                 } else {
2254                         _mmcam_dbg_warn("videosink[%s] does not support DISPLAY_FLIP", videosink_name);
2255                         return FALSE;
2256                 }
2257         } else {
2258                 _mmcam_dbg_err("Videosink element is null");
2259                 return FALSE;
2260         }
2261 }
2262
2263
2264 bool _mmcamcorder_set_videosrc_rotation(MMHandleType handle, int videosrc_rotate)
2265 {
2266         int width = 0;
2267         int height = 0;
2268         int set_width = 0;
2269         int set_height = 0;
2270         int set_rotate = 0;
2271         int fps = 0;
2272         gboolean do_set_caps = FALSE;
2273
2274         GstCaps *caps = NULL;
2275
2276         type_int_array *input_index = NULL;
2277         mmf_camcorder_t *hcamcorder = NULL;
2278         _MMCamcorderSubContext *sc = NULL;
2279
2280         hcamcorder = MMF_CAMCORDER(handle);
2281         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2282
2283         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2284         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2285         mmf_return_val_if_fail(sc->element, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2286
2287         if (!sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst) {
2288                 _mmcam_dbg_err("Video src is NULL!");
2289                 return FALSE;
2290         }
2291
2292         if (!sc->element[_MMCAMCORDER_VIDEOSRC_FILT].gst) {
2293                 _mmcam_dbg_err("Video filter is NULL!");
2294                 return FALSE;
2295         }
2296
2297         mm_camcorder_get_attributes(handle, NULL,
2298                                     MMCAM_CAMERA_WIDTH, &width,
2299                                     MMCAM_CAMERA_HEIGHT, &height,
2300                                     MMCAM_CAMERA_FPS, &fps,
2301                                     NULL);
2302
2303         _mmcamcorder_conf_get_value_int_array(hcamcorder->conf_ctrl,
2304                                               CONFIGURE_CATEGORY_CTRL_CAMERA,
2305                                               "InputIndex",
2306                                               &input_index );
2307         if (input_index == NULL) {
2308                 _mmcam_dbg_err("Failed to get input_index");
2309                 return FALSE;
2310         }
2311
2312         /* Interleaved format does not support rotation */
2313         if (sc->info_image->preview_format != MM_PIXEL_FORMAT_ITLV_JPEG_UYVY) {
2314                 /* store videosrc rotation */
2315                 sc->videosrc_rotate = videosrc_rotate;
2316
2317                 /* Define width, height and rotate in caps */
2318
2319                 /* This will be applied when rotate is 0, 90, 180, 270 if rear camera.
2320                 This will be applied when rotate is 0, 180 if front camera. */
2321                 set_rotate = videosrc_rotate * 90;
2322
2323                 if (videosrc_rotate == MM_VIDEO_INPUT_ROTATION_90 ||
2324                     videosrc_rotate == MM_VIDEO_INPUT_ROTATION_270) {
2325                         set_width = height;
2326                         set_height = width;
2327                         if (input_index->default_value == MM_VIDEO_DEVICE_CAMERA1) {
2328                                 if (videosrc_rotate == MM_VIDEO_INPUT_ROTATION_90) {
2329                                         set_rotate = 270;
2330                                 } else {
2331                                         set_rotate = 90;
2332                                 }
2333                         }
2334                 } else {
2335                         set_width = width;
2336                         set_height = height;
2337                 }
2338         } else {
2339                 sc->videosrc_rotate = MM_VIDEO_INPUT_ROTATION_NONE;
2340                 set_rotate = 0;
2341                 set_width = width;
2342                 set_height = height;
2343
2344                 _mmcam_dbg_warn("ITLV format doe snot support INPUT ROTATE. Ignore ROTATE[%d]",
2345                                 videosrc_rotate);
2346         }
2347
2348         /* correct size because ITLV does not support QCIF */
2349         if (input_index->default_value == MM_VIDEO_DEVICE_CAMERA0 &&
2350             strcmp(sc->format_name, "ITLV") == 0) {
2351                 if ((set_width == 176 && set_height == 144) ||
2352                     (set_width == 144 && set_height == 176)) {
2353                         set_width = set_width << 1;
2354                         set_height = set_height << 1;
2355                         _mmcam_dbg_log("ITLV format is not supported QCIF, so we set CIF(%dx%d)",
2356                                        set_width, set_height);
2357                 }
2358         }
2359
2360         MMCAMCORDER_G_OBJECT_GET(sc->element[_MMCAMCORDER_VIDEOSRC_FILT].gst, "caps", &caps);
2361         if (caps) {
2362                 GstStructure *structure = NULL;
2363
2364                 structure = gst_caps_get_structure(caps, 0);
2365                 if (structure) {
2366                         int caps_width = 0;
2367                         int caps_height = 0;
2368                         int caps_fps = 0;
2369                         int caps_rotate = 0;
2370
2371                         gst_structure_get_int(structure, "width", &caps_width);
2372                         gst_structure_get_int(structure, "height", &caps_height);
2373                         gst_structure_get_int(structure, "fps", &caps_fps);
2374                         gst_structure_get_int(structure, "rotate", &caps_rotate);
2375                         if (set_width == caps_width && set_height == caps_height &&
2376                             set_rotate == caps_rotate && fps == caps_fps) {
2377                                 _mmcam_dbg_log("No need to replace caps.");
2378                         } else {
2379                                 _mmcam_dbg_log("something is different. set new one...");
2380                                 do_set_caps = TRUE;
2381                         }
2382                 } else {
2383                         _mmcam_dbg_log("can not get structure of caps. set new one...");
2384                         do_set_caps = TRUE;
2385                 }
2386
2387                 gst_caps_unref(caps);
2388                 caps = NULL;
2389         } else {
2390                 _mmcam_dbg_log("No caps. set new one...");
2391                 do_set_caps = TRUE;
2392         }
2393
2394         if (do_set_caps) {
2395                 caps = gst_caps_new_simple("video/x-raw",
2396                                    "format", G_TYPE_STRING, sc->format_name,
2397                                            "width", G_TYPE_INT, set_width,
2398                                            "height", G_TYPE_INT, set_height,
2399                                            "framerate", GST_TYPE_FRACTION, fps, 1,
2400                                            "rotate", G_TYPE_INT, set_rotate,
2401                                            NULL);
2402                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_FILT].gst, "caps", caps);
2403                 gst_caps_unref(caps);
2404                 caps = NULL;
2405                 _mmcam_dbg_log("vidoesrc new caps set. format[%s],width[%d],height[%d],fps[%d],rotate[%d]",
2406                                sc->format_name, set_width, set_height, fps, set_rotate);
2407         }
2408
2409         return TRUE;
2410 }
2411
2412
2413 bool _mmcamcorder_set_videosrc_flip(MMHandleType handle, int videosrc_flip)
2414 {
2415         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
2416         _MMCamcorderSubContext *sc = NULL;
2417         type_int_array *input_index = NULL;
2418
2419         mmf_return_val_if_fail(hcamcorder, FALSE);
2420
2421         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2422         mmf_return_val_if_fail(sc, TRUE);
2423
2424         _mmcam_dbg_log("Set FLIP %d", videosrc_flip);
2425
2426         if (sc->element && sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst) {
2427                 int hflip = 0;
2428                 int vflip = 0;
2429
2430                 /* Interleaved format does not support FLIP */
2431                 if (sc->info_image->preview_format != MM_PIXEL_FORMAT_ITLV_JPEG_UYVY) {
2432                         _mmcamcorder_conf_get_value_int_array(hcamcorder->conf_ctrl,
2433                                                               CONFIGURE_CATEGORY_CTRL_CAMERA,
2434                                                               "InputIndex",
2435                                                               &input_index );
2436
2437                         hflip = (videosrc_flip & MM_FLIP_HORIZONTAL) == MM_FLIP_HORIZONTAL;
2438                         vflip = (videosrc_flip & MM_FLIP_VERTICAL) == MM_FLIP_VERTICAL;
2439
2440                         _mmcam_dbg_log("videosrc flip H:%d, V:%d", hflip, vflip);
2441
2442                         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "hflip", hflip);
2443                         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "vflip", vflip);
2444                 } else {
2445                         _mmcam_dbg_warn("ITLV format does not support FLIP. Ignore FLIP[%d]",
2446                                         videosrc_flip);
2447                 }
2448         } else {
2449                 _mmcam_dbg_warn("element is NULL");
2450                 return FALSE;
2451         }
2452
2453         return TRUE;
2454 }
2455
2456
2457 bool _mmcamcorder_set_videosrc_anti_shake(MMHandleType handle, int anti_shake)
2458 {
2459         GstCameraControl *control = NULL;
2460         _MMCamcorderSubContext *sc = NULL;
2461         GstElement *v_src = NULL;
2462
2463         int set_value = 0;
2464
2465         mmf_return_val_if_fail(handle, FALSE);
2466
2467         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2468         mmf_return_val_if_fail(sc, TRUE);
2469
2470         v_src = sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst;
2471
2472         if (!v_src) {
2473                 _mmcam_dbg_warn("videosrc element is NULL");
2474                 return FALSE;
2475         }
2476
2477         set_value = _mmcamcorder_convert_msl_to_sensor(handle, MM_CAM_CAMERA_ANTI_HANDSHAKE, anti_shake);
2478
2479         /* set anti-shake with camera control */
2480         if (!GST_IS_CAMERA_CONTROL(v_src)) {
2481                 _mmcam_dbg_warn("Can't cast Video source into camera control.");
2482                 return FALSE;
2483         }
2484
2485         control = GST_CAMERA_CONTROL(v_src);
2486         if (gst_camera_control_set_ahs(control, set_value)) {
2487                 _mmcam_dbg_log("Succeed in operating anti-handshake. value[%d]", set_value);
2488                 return TRUE;
2489         } else {
2490                 _mmcam_dbg_warn("Failed to operate anti-handshake. value[%d]", set_value);
2491         }
2492
2493         return FALSE;
2494 }
2495
2496
2497 bool _mmcamcorder_set_videosrc_stabilization(MMHandleType handle, int stabilization)
2498 {
2499         _MMCamcorderSubContext *sc = NULL;
2500         GstElement *v_src = NULL;
2501
2502         mmf_return_val_if_fail(handle, FALSE);
2503
2504         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2505         mmf_return_val_if_fail(sc, TRUE);
2506
2507         v_src = sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst;
2508
2509         if (!v_src) {
2510                 _mmcam_dbg_warn("videosrc element is NULL");
2511                 return FALSE;
2512         }
2513
2514         /* check property of videosrc element - support VDIS */
2515         if(g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(v_src)), "enable-vdis-mode")) {
2516                 int camera_format =MM_PIXEL_FORMAT_INVALID;
2517                 int camera_width = 0;
2518                 int camera_height = 0;
2519
2520                 if (stabilization == MM_CAMCORDER_VIDEO_STABILIZATION_ON) {
2521                         _mmcam_dbg_log("ENABLE video stabilization");
2522
2523                         /* VDIS mode only supports NV12 and [720p or 1080p] */
2524                         mm_camcorder_get_attributes(handle, NULL,
2525                                                     MMCAM_CAMERA_FORMAT, &camera_format,
2526                                                     MMCAM_CAMERA_WIDTH, &camera_width,
2527                                                     MMCAM_CAMERA_HEIGHT, &camera_height,
2528                                                     NULL);
2529                         if (camera_format == MM_PIXEL_FORMAT_NV12 &&
2530                             camera_width >= 1280 &&
2531                             camera_height >= 720) {
2532                                 _mmcam_dbg_log("preview format %d, size %dx%d, ENABLE video stabilization",
2533                                                camera_format, camera_width, camera_height, stabilization);
2534                                 /* set vdis mode */
2535                                 g_object_set(G_OBJECT(v_src),
2536                                              "enable-vdis-mode", TRUE,
2537                                              NULL);
2538                         } else {
2539                                 _mmcam_dbg_warn("invalid preview format %d or size %dx%d",
2540                                                 camera_format, camera_width, camera_height);
2541                                 return FALSE;
2542                         }
2543                 } else {
2544                         /* set vdis mode */
2545                         g_object_set(G_OBJECT(v_src),
2546                                      "enable-vdis-mode", FALSE,
2547                                      NULL);
2548
2549                         _mmcam_dbg_log("DISABLE video stabilization");
2550                 }
2551         } else if (stabilization == MM_CAMCORDER_VIDEO_STABILIZATION_ON) {
2552                 _mmcam_dbg_err("no property for video stabilization, so can not set ON");
2553                 return FALSE;
2554         } else {
2555                 _mmcam_dbg_warn("no property for video stabilization");
2556         }
2557
2558         return TRUE;
2559 }
2560