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