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