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