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