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