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