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