d508c3fd153a1819980993bb86b9d9a01285d5d0
[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 *gst_element_vscale_name = NULL;
1081         const char *str_profile = NULL;
1082         const char *str_aac = NULL;
1083         const char *str_aar = NULL;
1084         const char *str_acs = NULL;
1085         char *err_name = NULL;
1086         const char *videoconvert_name = NULL;
1087         GstCaps *audio_caps = NULL;
1088         GstCaps *video_caps = NULL;
1089         GstCaps *videoscale_caps = NULL;
1090         GstPad *pad = NULL;
1091         GList *element_list = NULL;
1092
1093         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1094         _MMCamcorderSubContext *sc = NULL;
1095         type_element *VideoencElement = NULL;
1096         type_element *AudioencElement = NULL;
1097         type_element *ImageencElement = NULL;
1098         type_element *MuxElement = NULL;
1099         type_element *RecordsinkElement = NULL;
1100         type_element *VideoscaleElement = NULL;
1101
1102         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1103
1104         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
1105         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1106         mmf_return_val_if_fail(sc->element, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1107
1108         _mmcam_dbg_log("start - profile : %d", profile);
1109
1110         /* Check existence */
1111         if (sc->encode_element[_MMCAMCORDER_ENCSINK_BIN].gst) {
1112                 if (((GObject *)sc->encode_element[_MMCAMCORDER_ENCSINK_BIN].gst)->ref_count > 0)
1113                         gst_object_unref(sc->encode_element[_MMCAMCORDER_ENCSINK_BIN].gst);
1114
1115                 _mmcam_dbg_log("_MMCAMCORDER_ENCSINK_BIN is Already existed.");
1116         }
1117
1118         /* Create bin element */
1119         _MMCAMCORDER_BIN_MAKE(sc, sc->encode_element, _MMCAMCORDER_ENCSINK_BIN, "encodesink_bin", err);
1120
1121         /* Create child element */
1122         if (profile != MM_CAMCORDER_ENCBIN_PROFILE_AUDIO) {
1123                 GstCaps *caps_from_pad = NULL;
1124                 char *caps_str = NULL;
1125
1126                 /* create appsrc and capsfilter */
1127                 _MMCAMCORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMCAMCORDER_ENCSINK_SRC, "appsrc", "encodesink_src", element_list, err);
1128
1129                 _MMCAMCORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMCAMCORDER_ENCSINK_FILT, "capsfilter", "encodesink_filter", element_list, err);
1130
1131                 /* set appsrc as live source */
1132                 MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_SRC].gst, "is-live", TRUE);
1133                 MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_SRC].gst, "format", 3); /* GST_FORMAT_TIME */
1134                 MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_SRC].gst, "max-bytes", 0); /* unlimited */
1135
1136                 /* set capsfilter */
1137                 if (profile == MM_CAMCORDER_ENCBIN_PROFILE_VIDEO) {
1138                         if (_mmcamcorder_is_encoded_preview_pixel_format(sc->info_image->preview_format)) {
1139                                 _mmcam_dbg_log("get pad from videosrc_filter");
1140                                 pad = gst_element_get_static_pad(sc->element[_MMCAMCORDER_VIDEOSRC_FILT].gst, "src");
1141                         } else {
1142                                 _mmcam_dbg_log("get pad from videosrc_que");
1143                                 pad = gst_element_get_static_pad(sc->element[_MMCAMCORDER_VIDEOSRC_QUE].gst, "src");
1144                         }
1145                         if (!pad) {
1146                                 _mmcam_dbg_err("get videosrc_que src pad failed");
1147                                 err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1148                                 goto pipeline_creation_error;
1149                         }
1150
1151                         caps_from_pad = gst_pad_get_allowed_caps(pad);
1152                         video_caps = gst_caps_copy(caps_from_pad);
1153                         gst_caps_unref(caps_from_pad);
1154                         caps_from_pad = NULL;
1155                         gst_object_unref(pad);
1156                         pad = NULL;
1157
1158                         /* fixate caps */
1159                         video_caps = gst_caps_fixate(video_caps);
1160                 } else {
1161                         /* Image */
1162                         MMCAMCORDER_G_OBJECT_GET(sc->element[_MMCAMCORDER_VIDEOSRC_FILT].gst, "caps", &video_caps);
1163                 }
1164
1165                 if (!video_caps) {
1166                         _mmcam_dbg_err("create recording pipeline caps failed");
1167                         err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1168                         goto pipeline_creation_error;
1169                 }
1170
1171                 if (profile == MM_CAMCORDER_ENCBIN_PROFILE_VIDEO) {
1172                         if (sc->info_video->use_videoscale) {
1173                                 gst_caps_set_simple(video_caps,
1174                                         "width", G_TYPE_INT, sc->info_video->preview_width,
1175                                         "height", G_TYPE_INT, sc->info_video->preview_height,
1176                                         NULL);
1177                                 videoscale_caps = gst_caps_copy(video_caps);
1178                         } else {
1179                                 gst_caps_set_simple(video_caps,
1180                                         "width", G_TYPE_INT, sc->info_video->video_width,
1181                                         "height", G_TYPE_INT, sc->info_video->video_height,
1182                                         NULL);
1183                         }
1184                 }
1185
1186                 caps_str = gst_caps_to_string(video_caps);
1187
1188                 _mmcam_dbg_log("encodebin caps [%s]", caps_str);
1189
1190                 g_free(caps_str);
1191                 caps_str = NULL;
1192
1193                 MMCAMCORDER_G_OBJECT_SET_POINTER(sc->encode_element[_MMCAMCORDER_ENCSINK_FILT].gst, "caps", video_caps);
1194
1195                 gst_caps_unref(video_caps);
1196                 video_caps = NULL;
1197
1198                 /* video scale */
1199                 if (profile == MM_CAMCORDER_ENCBIN_PROFILE_VIDEO &&
1200                         sc->info_video->use_videoscale) {
1201                         if (!videoscale_caps) {
1202                                 _mmcam_dbg_err("no videoscale caps");
1203                                 err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1204                                 goto pipeline_creation_error;
1205                         }
1206
1207                         gst_caps_set_simple(videoscale_caps,
1208                                 "width", G_TYPE_INT, sc->info_video->video_width,
1209                                 "height", G_TYPE_INT, sc->info_video->video_height,
1210                                 NULL);
1211
1212                         _mmcamcorder_conf_get_element(handle, hcamcorder->conf_main,
1213                                 CONFIGURE_CATEGORY_MAIN_RECORD,
1214                                 "VideoscaleElement",
1215                                 &VideoscaleElement);
1216
1217                         if (!_mmcamcorder_conf_get_value_element_name(VideoscaleElement, &gst_element_vscale_name)) {
1218                                 _mmcam_dbg_err("failed to get videoscale element name");
1219                                 err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1220                                 goto pipeline_creation_error;
1221                         }
1222
1223                         caps_str = gst_caps_to_string(videoscale_caps);
1224
1225                         _mmcam_dbg_log("encodebin videocale [%s][%s]", gst_element_vscale_name, caps_str);
1226
1227                         g_free(caps_str);
1228                         caps_str = NULL;
1229
1230                         _MMCAMCORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMCAMCORDER_ENCSINK_VSCALE, gst_element_vscale_name, "encodesink_vscale", element_list, err);
1231                         _MMCAMCORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMCAMCORDER_ENCSINK_VSCALE_FILT, "capsfilter", "encodesink_vscale_filter", element_list, err);
1232
1233                         MMCAMCORDER_G_OBJECT_SET_POINTER((sc->encode_element[_MMCAMCORDER_ENCSINK_VSCALE_FILT].gst), "caps", videoscale_caps);
1234
1235                         gst_caps_unref(videoscale_caps);
1236                         videoscale_caps = NULL;
1237                 }
1238
1239                 /* release element_list, they will be placed out of encodesink bin */
1240                 if (element_list) {
1241                         g_list_free(element_list);
1242                         element_list = NULL;
1243                 }
1244
1245                 /* connect signal for ready to push buffer */
1246                 MMCAMCORDER_SIGNAL_CONNECT(sc->encode_element[_MMCAMCORDER_ENCSINK_SRC].gst,
1247                         _MMCAMCORDER_HANDLER_VIDEOREC,
1248                         "need-data",
1249                         _mmcamcorder_ready_to_encode_callback,
1250                         hcamcorder);
1251         }
1252
1253         _MMCAMCORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMCAMCORDER_ENCSINK_ENCBIN, "encodebin", "encodesink_encbin", element_list, err);
1254
1255         /* check element availability */
1256         if (profile == MM_CAMCORDER_ENCBIN_PROFILE_IMAGE) {
1257                 err = mm_camcorder_get_attributes(handle, &err_name,
1258                         MMCAM_CAPTURE_FORMAT, &cap_format,
1259                         NULL);
1260         } else {
1261                 err = mm_camcorder_get_attributes(handle, &err_name,
1262                         MMCAM_AUDIO_ENCODER, &audio_enc,
1263                         MMCAM_AUDIO_CHANNEL, &channel,
1264                         MMCAM_VIDEO_ENCODER_BITRATE, &v_bitrate,
1265                         MMCAM_AUDIO_ENCODER_BITRATE, &a_bitrate,
1266                         NULL);
1267         }
1268
1269         if (err != MM_ERROR_NONE) {
1270                 if (err_name) {
1271                         _mmcam_dbg_err("failed to get attributes [%s][0x%x]", err_name, err);
1272                         SAFE_FREE(err_name);
1273                 } else {
1274                         _mmcam_dbg_err("failed to get attributes [0x%x]", err);
1275                 }
1276
1277                 goto pipeline_creation_error;
1278         }
1279
1280         _mmcam_dbg_log("Profile[%d]", profile);
1281
1282         /* Set information */
1283         if (profile == MM_CAMCORDER_ENCBIN_PROFILE_VIDEO) {
1284                 str_profile = "VideoProfile";
1285                 str_aac = "VideoAutoAudioConvert";
1286                 str_aar = "VideoAutoAudioResample";
1287                 str_acs = "VideoAutoColorSpace";
1288         } else if (profile == MM_CAMCORDER_ENCBIN_PROFILE_AUDIO) {
1289                 str_profile = "AudioProfile";
1290                 str_aac = "AudioAutoAudioConvert";
1291                 str_aar = "AudioAutoAudioResample";
1292                 str_acs = "AudioAutoColorSpace";
1293         } else if (profile == MM_CAMCORDER_ENCBIN_PROFILE_IMAGE) {
1294                 str_profile = "ImageProfile";
1295                 str_aac = "ImageAutoAudioConvert";
1296                 str_aar = "ImageAutoAudioResample";
1297                 str_acs = "ImageAutoColorSpace";
1298         }
1299
1300         _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_main, CONFIGURE_CATEGORY_MAIN_RECORD, str_profile, &encodebin_profile);
1301         _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_main, CONFIGURE_CATEGORY_MAIN_RECORD, str_aac, &auto_audio_convert);
1302         _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_main, CONFIGURE_CATEGORY_MAIN_RECORD, str_aar, &auto_audio_resample);
1303         _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_main, CONFIGURE_CATEGORY_MAIN_RECORD, str_acs, &auto_color_space);
1304
1305         _mmcam_dbg_log("Profile:%d, AutoAudioConvert:%d, AutoAudioResample:%d, AutoColorSpace:%d",
1306                 encodebin_profile, auto_audio_convert, auto_audio_resample, auto_color_space);
1307
1308         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "profile", encodebin_profile);
1309         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "auto-audio-convert", auto_audio_convert);
1310         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "auto-audio-resample", auto_audio_resample);
1311         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "auto-colorspace", auto_color_space);
1312         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "use-video-toggle", FALSE);
1313
1314         /* get video convert element name */
1315         _mmcamcorder_conf_get_value_element_name(sc->VideoconvertElement, &videoconvert_name);
1316
1317         /* Codec */
1318         if (profile == MM_CAMCORDER_ENCBIN_PROFILE_VIDEO) {
1319                 int use_venc_queue = 0;
1320
1321                 VideoencElement = _mmcamcorder_get_type_element(handle, MM_CAM_VIDEO_ENCODER);
1322
1323                 if (!VideoencElement) {
1324                         _mmcam_dbg_err("Fail to get type element");
1325                         err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1326                         goto pipeline_creation_error;
1327                 }
1328
1329                 if (_mmcamcorder_is_encoded_preview_pixel_format(sc->info_image->preview_format)) {
1330                         /* set dummy element */
1331                         gst_element_venc_name = "identity";
1332                 } else {
1333                         _mmcamcorder_conf_get_value_element_name(VideoencElement, &gst_element_venc_name);
1334                 }
1335
1336                 if (gst_element_venc_name) {
1337                         _mmcam_dbg_log("video encoder name [%s]", gst_element_venc_name);
1338
1339                         MMCAMCORDER_G_OBJECT_SET_POINTER(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "venc-name", gst_element_venc_name);
1340                         _MMCAMCORDER_ENCODEBIN_ELMGET(sc, _MMCAMCORDER_ENCSINK_VENC, "video-encode", err);
1341                 } else {
1342                         _mmcam_dbg_err("Fail to get video encoder name");
1343                         err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1344                         goto pipeline_creation_error;
1345                 }
1346
1347                 /* set color space converting element */
1348                 if (auto_color_space) {
1349                         _mmcam_dbg_log("set video convert element [%s]", videoconvert_name);
1350
1351                         MMCAMCORDER_G_OBJECT_SET_POINTER(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "vconv-name", videoconvert_name);
1352                         _MMCAMCORDER_ENCODEBIN_ELMGET(sc, _MMCAMCORDER_ENCSINK_VCONV, "video-convert", err);
1353
1354                         /* set colorspace plugin property setting */
1355                         _mmcamcorder_conf_set_value_element_property(sc->encode_element[_MMCAMCORDER_ENCSINK_VCONV].gst, sc->VideoconvertElement);
1356
1357                         /* fourcc type was removed in GST 1.0 */
1358                         if (hcamcorder->use_zero_copy_format) {
1359                                 if (strstr(gst_element_venc_name, "omx")) {
1360                                         video_caps = gst_caps_new_simple("video/x-raw", "format", G_TYPE_STRING, "SN12", NULL);
1361
1362                                         if (video_caps) {
1363                                                 MMCAMCORDER_G_OBJECT_SET_POINTER(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "vcaps", video_caps);
1364                                                 MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_VCONV].gst, "dst-buffer-num", _MMCAMCORDER_CONVERT_OUTPUT_BUFFER_NUM);
1365
1366                                                 gst_caps_unref(video_caps);
1367                                                 video_caps = NULL;
1368                                         } else {
1369                                                 _mmcam_dbg_warn("failed to create caps");
1370                                         }
1371                                 } else {
1372                                         _mmcam_dbg_log("current video codec is not openmax but [%s]", gst_element_venc_name);
1373                                 }
1374                         }
1375                 }
1376
1377                 _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_main,
1378                         CONFIGURE_CATEGORY_MAIN_RECORD,
1379                         "UseVideoEncoderQueue",
1380                         &use_venc_queue);
1381                 if (use_venc_queue)
1382                         _MMCAMCORDER_ENCODEBIN_ELMGET(sc, _MMCAMCORDER_ENCSINK_VENC_QUE, "use-venc-queue", err);
1383         }
1384
1385         if (sc->audio_disable == FALSE &&
1386             profile != MM_CAMCORDER_ENCBIN_PROFILE_IMAGE) {
1387                 int use_aenc_queue = 0;
1388
1389                 AudioencElement = _mmcamcorder_get_type_element(handle, MM_CAM_AUDIO_ENCODER);
1390                 if (!AudioencElement) {
1391                         _mmcam_dbg_err("Fail to get type element");
1392                         err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1393                         goto pipeline_creation_error;
1394                 }
1395
1396                 _mmcamcorder_conf_get_value_element_name(AudioencElement, &gst_element_aenc_name);
1397
1398                 MMCAMCORDER_G_OBJECT_SET_POINTER(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "aenc-name", gst_element_aenc_name);
1399                 _MMCAMCORDER_ENCODEBIN_ELMGET(sc, _MMCAMCORDER_ENCSINK_AENC, "audio-encode", err);
1400
1401                 if (audio_enc == MM_AUDIO_CODEC_AMR && channel == 2) {
1402                         audio_caps = gst_caps_new_simple("audio/x-raw",
1403                                                                                          "channels", G_TYPE_INT, 1,
1404                                                                                          NULL);
1405                         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "auto-audio-convert", TRUE);
1406                         MMCAMCORDER_G_OBJECT_SET_POINTER(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "acaps", audio_caps);
1407                         gst_caps_unref(audio_caps);
1408                         audio_caps = NULL;
1409                 }
1410
1411                 if (audio_enc == MM_AUDIO_CODEC_OGG) {
1412                         audio_caps = gst_caps_new_empty_simple("audio/x-raw");
1413                         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "auto-audio-convert", TRUE);
1414                         MMCAMCORDER_G_OBJECT_SET_POINTER(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "acaps", audio_caps);
1415                         gst_caps_unref(audio_caps);
1416                         audio_caps = NULL;
1417                         _mmcam_dbg_log("***** MM_AUDIO_CODEC_OGG : setting audio/x-raw-int ");
1418                 }
1419
1420                 _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_main,
1421                         CONFIGURE_CATEGORY_MAIN_RECORD,
1422                         "UseAudioEncoderQueue",
1423                         &use_aenc_queue);
1424                 if (use_aenc_queue) {
1425                         _MMCAMCORDER_ENCODEBIN_ELMGET(sc, _MMCAMCORDER_ENCSINK_AENC_QUE, "use-aenc-queue", err);
1426                         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_AENC_QUE].gst, "max-size-time", 0);
1427                         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_AENC_QUE].gst, "max-size-buffers", 0);
1428                 }
1429         }
1430
1431         if (profile == MM_CAMCORDER_ENCBIN_PROFILE_IMAGE) {
1432                 if (cap_format == MM_PIXEL_FORMAT_ENCODED) {
1433                         ImageencElement = _mmcamcorder_get_type_element(handle, MM_CAM_IMAGE_ENCODER);
1434                         if (!ImageencElement) {
1435                                 _mmcam_dbg_err("Fail to get type element");
1436                                 err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1437                                 goto pipeline_creation_error;
1438                         }
1439
1440                         _mmcamcorder_conf_get_value_element_name(ImageencElement, &gst_element_ienc_name);
1441                 } else {
1442                         /* raw format - set dummy element */
1443                         gst_element_ienc_name = "identity";
1444                 }
1445
1446                 MMCAMCORDER_G_OBJECT_SET_POINTER(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "ienc-name", gst_element_ienc_name);
1447                 _MMCAMCORDER_ENCODEBIN_ELMGET(sc, _MMCAMCORDER_ENCSINK_IENC, "image-encode", err);
1448         }
1449
1450         /* Mux */
1451         if (profile != MM_CAMCORDER_ENCBIN_PROFILE_IMAGE) {
1452                 MuxElement = _mmcamcorder_get_type_element(handle, MM_CAM_FILE_FORMAT);
1453                 if (!MuxElement) {
1454                         _mmcam_dbg_err("Fail to get type element");
1455                         err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1456                         goto pipeline_creation_error;
1457                 }
1458
1459                 _mmcamcorder_conf_get_value_element_name(MuxElement, &gst_element_mux_name);
1460
1461                 MMCAMCORDER_G_OBJECT_SET_POINTER(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "mux-name", gst_element_mux_name);
1462                 _MMCAMCORDER_ENCODEBIN_ELMGET(sc, _MMCAMCORDER_ENCSINK_MUX, "mux", err);
1463
1464                 _mmcamcorder_conf_set_value_element_property(sc->encode_element[_MMCAMCORDER_ENCSINK_MUX].gst, MuxElement);
1465         }
1466
1467         /* Sink */
1468         if (profile != MM_CAMCORDER_ENCBIN_PROFILE_IMAGE) {
1469                 /* for recording */
1470                 _mmcamcorder_conf_get_element(handle, hcamcorder->conf_main,
1471                         CONFIGURE_CATEGORY_MAIN_RECORD,
1472                         "RecordsinkElement",
1473                         &RecordsinkElement);
1474                 _mmcamcorder_conf_get_value_element_name(RecordsinkElement, &gst_element_rsink_name);
1475
1476                 _MMCAMCORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMCAMCORDER_ENCSINK_SINK, gst_element_rsink_name, NULL, element_list, err);
1477
1478                 _mmcamcorder_conf_set_value_element_property(sc->encode_element[_MMCAMCORDER_ENCSINK_SINK].gst, RecordsinkElement);
1479         } else {
1480                 /* for stillshot */
1481                 _MMCAMCORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMCAMCORDER_ENCSINK_SINK, "fakesink", NULL, element_list, err);
1482         }
1483
1484         if (profile == MM_CAMCORDER_ENCBIN_PROFILE_VIDEO) {
1485                 /* property setting in ini */
1486                 _mmcamcorder_conf_set_value_element_property(sc->encode_element[_MMCAMCORDER_ENCSINK_VENC].gst, VideoencElement);
1487
1488                 /* bitrate setting */
1489                 _mmcamcorder_set_encoder_bitrate(MM_CAMCORDER_ENCODER_TYPE_VIDEO, 0,
1490                         v_bitrate, sc->encode_element[_MMCAMCORDER_ENCSINK_VENC].gst);
1491         }
1492
1493         if (sc->audio_disable == FALSE &&
1494             profile != MM_CAMCORDER_ENCBIN_PROFILE_IMAGE) {
1495                 /* property setting in ini */
1496                 _mmcamcorder_conf_set_value_element_property(sc->encode_element[_MMCAMCORDER_ENCSINK_AENC].gst, AudioencElement);
1497
1498                 /* bitrate setting */
1499                 _mmcamcorder_set_encoder_bitrate(MM_CAMCORDER_ENCODER_TYPE_AUDIO, audio_enc,
1500                         a_bitrate, sc->encode_element[_MMCAMCORDER_ENCSINK_AENC].gst);
1501         }
1502
1503         _mmcam_dbg_log("Element creation complete");
1504
1505         /* Add element to bin */
1506         if (!_mmcamcorder_add_elements_to_bin(GST_BIN(sc->encode_element[_MMCAMCORDER_ENCSINK_BIN].gst), element_list)) {
1507                 _mmcam_dbg_err("element add error.");
1508                 err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1509                 goto pipeline_creation_error;
1510         }
1511
1512         _mmcam_dbg_log("Element add complete");
1513
1514         if (profile == MM_CAMCORDER_ENCBIN_PROFILE_VIDEO) {
1515                 pad = gst_element_get_request_pad(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "video");
1516                 if (!gst_element_add_pad(sc->encode_element[_MMCAMCORDER_ENCSINK_BIN].gst, gst_ghost_pad_new("video_sink0", pad))) {
1517                         gst_object_unref(pad);
1518                         pad = NULL;
1519                         _mmcam_dbg_err("failed to create ghost video_sink0 on _MMCAMCORDER_ENCSINK_BIN.");
1520                         err = MM_ERROR_CAMCORDER_GST_LINK;
1521                         goto pipeline_creation_error;
1522                 }
1523                 gst_object_unref(pad);
1524                 pad = NULL;
1525
1526                 if (sc->audio_disable == FALSE) {
1527                         pad = gst_element_get_request_pad(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "audio");
1528                         if (!gst_element_add_pad(sc->encode_element[_MMCAMCORDER_ENCSINK_BIN].gst, gst_ghost_pad_new("audio_sink0", pad))) {
1529                                 gst_object_unref(pad);
1530                                 pad = NULL;
1531                                 _mmcam_dbg_err("failed to create ghost audio_sink0 on _MMCAMCORDER_ENCSINK_BIN.");
1532                                 err = MM_ERROR_CAMCORDER_GST_LINK;
1533                                 goto pipeline_creation_error;
1534                         }
1535                         gst_object_unref(pad);
1536                         pad = NULL;
1537                 }
1538         } else if (profile == MM_CAMCORDER_ENCBIN_PROFILE_AUDIO) {
1539                 pad = gst_element_get_request_pad(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "audio");
1540                 if (!gst_element_add_pad(sc->encode_element[_MMCAMCORDER_ENCSINK_BIN].gst, gst_ghost_pad_new("audio_sink0", pad))) {
1541                         gst_object_unref(pad);
1542                         pad = NULL;
1543                         _mmcam_dbg_err("failed to create ghost audio_sink0 on _MMCAMCORDER_ENCSINK_BIN.");
1544                         err = MM_ERROR_CAMCORDER_GST_LINK;
1545                         goto pipeline_creation_error;
1546                 }
1547                 gst_object_unref(pad);
1548                 pad = NULL;
1549         } else {
1550                 /* for stillshot */
1551                 pad = gst_element_get_request_pad(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "image");
1552                 if (!gst_element_add_pad(sc->encode_element[_MMCAMCORDER_ENCSINK_BIN].gst, gst_ghost_pad_new("image_sink0", pad))) {
1553                         gst_object_unref(pad);
1554                         pad = NULL;
1555                         _mmcam_dbg_err("failed to create ghost image_sink0 on _MMCAMCORDER_ENCSINK_BIN.");
1556                         err = MM_ERROR_CAMCORDER_GST_LINK;
1557                         goto pipeline_creation_error;
1558                 }
1559                 gst_object_unref(pad);
1560                 pad = NULL;
1561         }
1562
1563         _mmcam_dbg_log("Get pad complete");
1564
1565         /* Link internal element */
1566         if (!_mmcamcorder_link_elements(element_list)) {
1567                 _mmcam_dbg_err("element link error.");
1568                 err = MM_ERROR_CAMCORDER_GST_LINK;
1569                 goto pipeline_creation_error;
1570         }
1571
1572         if (element_list)
1573                 g_list_free(element_list);
1574
1575         _mmcam_dbg_log("done");
1576
1577         return MM_ERROR_NONE;
1578
1579 pipeline_creation_error:
1580         _mmcamcorder_remove_all_handlers(handle, _MMCAMCORDER_HANDLER_VIDEOREC);
1581
1582         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_ENCSINK_ENCBIN);
1583         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_ENCSINK_SRC);
1584         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_ENCSINK_FILT);
1585         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_ENCSINK_VSCALE);
1586         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_ENCSINK_VSCALE_FILT);
1587         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_ENCSINK_VENC);
1588         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_ENCSINK_AENC);
1589         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_ENCSINK_IENC);
1590         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_ENCSINK_MUX);
1591         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_ENCSINK_SINK);
1592         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_ENCSINK_BIN);
1593
1594         if (element_list)
1595                 g_list_free(element_list);
1596
1597         if (video_caps)
1598                 gst_caps_unref(video_caps);
1599
1600         if (videoscale_caps)
1601                 gst_caps_unref(videoscale_caps);
1602
1603         return err;
1604 }
1605
1606
1607 int _mmcamcorder_create_preview_pipeline(MMHandleType handle)
1608 {
1609         int err = MM_ERROR_NONE;
1610
1611         GstPad *srcpad = NULL;
1612         GstBus *bus = NULL;
1613
1614         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1615         _MMCamcorderSubContext *sc = NULL;
1616
1617         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1618
1619         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
1620         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1621         mmf_return_val_if_fail(sc->element, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1622
1623         _mmcam_dbg_log("");
1624
1625         /** Create gstreamer element **/
1626         /* Main pipeline */
1627         _MMCAMCORDER_PIPELINE_MAKE(sc, sc->element, _MMCAMCORDER_MAIN_PIPE, "camera_pipeline", err);
1628
1629         /* Sub pipeline */
1630         err = _mmcamcorder_create_preview_elements((MMHandleType)hcamcorder);
1631         if (err != MM_ERROR_NONE)
1632                 goto pipeline_creation_error;
1633
1634         /* Set data probe function */
1635         if (sc->element[_MMCAMCORDER_VIDEOSRC_QUE].gst) {
1636                 _mmcam_dbg_log("add video dataprobe to videosrc queue");
1637                 srcpad = gst_element_get_static_pad(sc->element[_MMCAMCORDER_VIDEOSRC_QUE].gst, "src");
1638         } else {
1639                 _mmcam_dbg_err("there is no queue plugin");
1640                 goto pipeline_creation_error;
1641         }
1642
1643         if (srcpad) {
1644                 MMCAMCORDER_ADD_BUFFER_PROBE(srcpad, _MMCAMCORDER_HANDLER_PREVIEW,
1645                         __mmcamcorder_video_dataprobe_preview, hcamcorder);
1646
1647                 gst_object_unref(srcpad);
1648                 srcpad = NULL;
1649         } else {
1650                 _mmcam_dbg_err("failed to get srcpad");
1651                 goto pipeline_creation_error;
1652         }
1653
1654         /* set dataprobe for video recording if it does not support dual stream. */
1655         if (sc->info_video->record_dual_stream == FALSE) {
1656                 if (_mmcamcorder_is_encoded_preview_pixel_format(sc->info_image->preview_format))
1657                         srcpad = gst_element_get_static_pad(sc->element[_MMCAMCORDER_VIDEOSRC_QUE].gst, "src");
1658                 else
1659                         srcpad = gst_element_get_static_pad(sc->element[_MMCAMCORDER_VIDEOSINK_QUE].gst, "src");
1660
1661                 MMCAMCORDER_ADD_BUFFER_PROBE(srcpad, _MMCAMCORDER_HANDLER_PREVIEW,
1662                         __mmcamcorder_video_dataprobe_record, hcamcorder);
1663                 gst_object_unref(srcpad);
1664                 srcpad = NULL;
1665         }
1666
1667         bus = gst_pipeline_get_bus(GST_PIPELINE(sc->element[_MMCAMCORDER_MAIN_PIPE].gst));
1668
1669         /* Register pipeline message callback */
1670         hcamcorder->pipeline_cb_event_id = gst_bus_add_watch(bus, _mmcamcorder_pipeline_cb_message, (gpointer)hcamcorder);
1671
1672         /* set sync handler */
1673         gst_bus_set_sync_handler(bus, _mmcamcorder_pipeline_bus_sync_callback, (gpointer)hcamcorder, NULL);
1674
1675         gst_object_unref(bus);
1676         bus = NULL;
1677
1678         return MM_ERROR_NONE;
1679
1680 pipeline_creation_error:
1681         _MMCAMCORDER_ELEMENT_REMOVE(sc->element, _MMCAMCORDER_MAIN_PIPE);
1682         return err;
1683 }
1684
1685
1686 void _mmcamcorder_ready_to_encode_callback(GstElement *element, guint size, gpointer handle)
1687 {
1688         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1689         _MMCamcorderSubContext *sc = NULL;
1690
1691         /*_mmcam_dbg_log("start");*/
1692
1693         mmf_return_if_fail(hcamcorder);
1694         mmf_return_if_fail(hcamcorder->sub_context);
1695         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
1696
1697         /* set flag */
1698         if (sc->info_video->push_encoding_buffer == PUSH_ENCODING_BUFFER_INIT) {
1699                 sc->info_video->push_encoding_buffer = PUSH_ENCODING_BUFFER_RUN;
1700                 _mmcam_dbg_warn("set push_encoding_buffer RUN");
1701         }
1702 }
1703
1704
1705 int _mmcamcorder_videosink_window_set(MMHandleType handle, type_element* VideosinkElement)
1706 {
1707         int err = MM_ERROR_NONE;
1708         int size = 0;
1709         int rect_x = 0;
1710         int rect_y = 0;
1711         int rect_width = 0;
1712         int rect_height = 0;
1713         int visible = 0;
1714         int rotation = MM_DISPLAY_ROTATION_NONE;
1715         int flip = MM_FLIP_NONE;
1716         int display_mode = MM_DISPLAY_MODE_DEFAULT;
1717         int display_geometry_method = MM_DISPLAY_METHOD_LETTER_BOX;
1718         int origin_size = 0;
1719         int zoom_attr = 0;
1720         int zoom_level = 0;
1721         int do_scaling = FALSE;
1722 #ifdef _MMCAMCORDER_RM_SUPPORT
1723         int display_scaler = 0;
1724 #endif /* _MMCAMCORDER_RM_SUPPORT */
1725         int *dp_handle = NULL;
1726         MMCamWindowInfo *window_info = NULL;
1727         gulong xid;
1728         char *err_name = NULL;
1729         const char *videosink_name = NULL;
1730
1731         GstElement *vsink = NULL;
1732
1733         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1734         _MMCamcorderSubContext *sc = NULL;
1735
1736         _mmcam_dbg_log("");
1737
1738         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1739
1740         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
1741         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1742         mmf_return_val_if_fail(sc->element, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1743         mmf_return_val_if_fail(sc->element[_MMCAMCORDER_VIDEOSINK_SINK].gst, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1744
1745         vsink = sc->element[_MMCAMCORDER_VIDEOSINK_SINK].gst;
1746
1747         /* Get video display information */
1748         err = mm_camcorder_get_attributes(handle, &err_name,
1749                 MMCAM_DISPLAY_RECT_X, &rect_x,
1750                 MMCAM_DISPLAY_RECT_Y, &rect_y,
1751                 MMCAM_DISPLAY_RECT_WIDTH, &rect_width,
1752                 MMCAM_DISPLAY_RECT_HEIGHT, &rect_height,
1753                 MMCAM_DISPLAY_ROTATION, &rotation,
1754                 MMCAM_DISPLAY_FLIP, &flip,
1755                 MMCAM_DISPLAY_VISIBLE, &visible,
1756                 MMCAM_DISPLAY_HANDLE, (void **)&dp_handle, &size,
1757                 MMCAM_DISPLAY_MODE, &display_mode,
1758                 MMCAM_DISPLAY_GEOMETRY_METHOD, &display_geometry_method,
1759                 MMCAM_DISPLAY_SCALE, &zoom_attr,
1760                 MMCAM_DISPLAY_EVAS_DO_SCALING, &do_scaling,
1761                 NULL);
1762         if (err != MM_ERROR_NONE) {
1763                 if (err_name) {
1764                         _mmcam_dbg_err("failed to get attributes [%s][0x%x]", err_name, err);
1765                         SAFE_FREE(err_name);
1766                 } else {
1767                         _mmcam_dbg_err("failed to get attributes [0x%x]", err);
1768                 }
1769
1770                 return err;
1771         }
1772
1773         _mmcamcorder_conf_get_value_element_name(VideosinkElement, &videosink_name);
1774
1775         if (!videosink_name) {
1776                 _mmcam_dbg_err("failed to get videosink name");
1777                 return MM_ERROR_CAMCORDER_INTERNAL;
1778         }
1779
1780         _mmcam_dbg_log("(dp_handle=%p, size=%d)", dp_handle, size);
1781
1782         /* Set display handle */
1783         if (!strcmp(videosink_name, "xvimagesink") || !strcmp(videosink_name, "ximagesink")) {
1784                 if (dp_handle) {
1785                         xid = *dp_handle;
1786                         _mmcam_dbg_log("xid = %lu )", xid);
1787                         gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(vsink), xid);
1788                 } else {
1789                         _mmcam_dbg_warn("Handle is NULL. Set xid as 0.. but, it's not recommended.");
1790                         gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(vsink), 0);
1791                 }
1792 #ifdef _MMCAMCORDER_RM_SUPPORT
1793                 if (hcamcorder->request_resources.category_id[0] == RM_CATEGORY_VIDEO_DECODER_SUB)
1794                         display_scaler = 1;
1795
1796                 MMCAMCORDER_G_OBJECT_SET(vsink, "device-scaler", display_scaler);
1797 #endif /* _MMCAMCORDER_RM_SUPPORT */
1798         } else if (!strcmp(videosink_name, "evasimagesink") || !strcmp(videosink_name, "evaspixmapsink")) {
1799                 _mmcam_dbg_log("videosink : %s, handle : %p", videosink_name, dp_handle);
1800
1801                 if (dp_handle) {
1802                         MMCAMCORDER_G_OBJECT_SET_POINTER(vsink, "evas-object", dp_handle);
1803                         MMCAMCORDER_G_OBJECT_SET(vsink, "origin-size", !do_scaling);
1804                 } else {
1805                         _mmcam_dbg_err("display handle(eavs object) is NULL");
1806                         return MM_ERROR_CAMCORDER_INVALID_ARGUMENT;
1807                 }
1808         } else if (!strcmp(videosink_name, "tizenwlsink")) {
1809                 if (dp_handle) {
1810                         window_info = (MMCamWindowInfo *)dp_handle;
1811                         _mmcam_dbg_log("wayland global surface id : %d", window_info->surface_id);
1812                         gst_video_overlay_set_wl_window_wl_surface_id(GST_VIDEO_OVERLAY(vsink), window_info->surface_id);
1813                 } else {
1814                         _mmcam_dbg_warn("Handle is NULL. skip setting.");
1815                 }
1816         } else if (!strcmp(videosink_name, "directvideosink")) {
1817                 if (dp_handle) {
1818                         window_info = (MMCamWindowInfo *)dp_handle;
1819                         _mmcam_dbg_log("wayland global surface id : %d, x,y,w,h (%d,%d,%d,%d)",
1820                                 window_info->surface_id,
1821                                 window_info->rect.x,
1822                                 window_info->rect.y,
1823                                 window_info->rect.width,
1824                                 window_info->rect.height);
1825                         gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(vsink), (guintptr)window_info->surface_id);
1826                         gst_video_overlay_set_render_rectangle(GST_VIDEO_OVERLAY(vsink),
1827                                 window_info->rect.x,
1828                                 window_info->rect.y,
1829                                 window_info->rect.width,
1830                                 window_info->rect.height);
1831                 } else {
1832                         _mmcam_dbg_warn("dp_handle is null");
1833                 }
1834 #ifdef _MMCAMCORDER_RM_SUPPORT
1835                 if (hcamcorder->request_resources.category_id[0] == RM_CATEGORY_VIDEO_DECODER_SUB)
1836                         display_scaler = 1;
1837                 MMCAMCORDER_G_OBJECT_SET(vsink, "device-scaler", display_scaler);
1838 #endif /* _MMCAMCORDER_RM_SUPPORT */
1839         } else {
1840                 _mmcam_dbg_warn("Who are you?? (Videosink: %s)", videosink_name);
1841         }
1842
1843         _mmcam_dbg_log("%s set: display_geometry_method[%d],origin-size[%d],visible[%d],rotate[%d],flip[%d]",
1844                 videosink_name, display_geometry_method, origin_size, visible, rotation, flip);
1845
1846         /* Set attribute */
1847         if (!strcmp(videosink_name, "xvimagesink") || !strcmp(videosink_name, "tizenwlsink") ||
1848             !strcmp(videosink_name, "evaspixmapsink") || !strcmp(videosink_name, "directvideosink")) {
1849                 /* set rotation */
1850                 MMCAMCORDER_G_OBJECT_SET(vsink, "rotate", rotation);
1851
1852                 /* set flip */
1853                 MMCAMCORDER_G_OBJECT_SET(vsink, "flip", flip);
1854
1855                 switch (zoom_attr) {
1856                 case MM_DISPLAY_SCALE_DEFAULT:
1857                         zoom_level = 1;
1858                         break;
1859                 case MM_DISPLAY_SCALE_DOUBLE_LENGTH:
1860                         zoom_level = 2;
1861                         break;
1862                 case MM_DISPLAY_SCALE_TRIPLE_LENGTH:
1863                         zoom_level = 3;
1864                         break;
1865                 default:
1866                         _mmcam_dbg_warn("Unsupported zoom value. set as default.");
1867                         zoom_level = 1;
1868                         break;
1869                 }
1870
1871                 MMCAMCORDER_G_OBJECT_SET(vsink, "display-geometry-method", display_geometry_method);
1872                 MMCAMCORDER_G_OBJECT_SET(vsink, "display-mode", display_mode);
1873                 MMCAMCORDER_G_OBJECT_SET(vsink, "visible", visible);
1874                 MMCAMCORDER_G_OBJECT_SET(vsink, "zoom", zoom_level);
1875
1876                 if (display_geometry_method == MM_DISPLAY_METHOD_CUSTOM_ROI) {
1877                         if (!strcmp(videosink_name, "tizenwlsink")) {
1878                             gst_video_overlay_set_display_roi_area(GST_VIDEO_OVERLAY(vsink),
1879                                         rect_x, rect_y, rect_width, rect_height);
1880                         } else {
1881                                 g_object_set(vsink,
1882                                         "dst-roi-x", rect_x,
1883                                         "dst-roi-y", rect_y,
1884                                         "dst-roi-w", rect_width,
1885                                         "dst-roi-h", rect_height,
1886                                         NULL);
1887                         }
1888                 }
1889         }
1890
1891         return MM_ERROR_NONE;
1892 }
1893
1894
1895 int _mmcamcorder_video_frame_stabilize(MMHandleType handle, int cmd)
1896 {
1897         int category = 0;
1898         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1899         _MMCamcorderSubContext *sc = NULL;
1900
1901         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1902
1903         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
1904
1905         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1906
1907         switch (cmd) {
1908         case _MMCamcorder_CMD_PREVIEW_START:
1909                 category = CONFIGURE_CATEGORY_CTRL_CAMERA;
1910                 break;
1911         case _MMCamcorder_CMD_CAPTURE:
1912                 category = CONFIGURE_CATEGORY_CTRL_CAPTURE;
1913                 break;
1914         default:
1915                 _mmcam_dbg_warn("unknown command : %d", cmd);
1916                 return MM_ERROR_CAMCORDER_INVALID_ARGUMENT;
1917         }
1918
1919         _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_ctrl,
1920                 category, "FrameStabilityCount", &sc->frame_stability_count);
1921
1922         _mmcam_dbg_log("[cmd %d] frame stability count : %d",
1923                 cmd, sc->frame_stability_count);
1924
1925         return MM_ERROR_NONE;
1926 }
1927
1928 /* Retreive device information and set them to attributes */
1929 gboolean _mmcamcorder_get_device_info(MMHandleType handle)
1930 {
1931         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1932         _MMCamcorderSubContext *sc = NULL;
1933         GstCameraControl *control = NULL;
1934         GstCameraControlExifInfo exif_info = {0,};
1935
1936         mmf_return_val_if_fail(hcamcorder, FALSE);
1937         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
1938
1939         memset(&exif_info, 0x0, sizeof(GstCameraControlExifInfo));
1940
1941         if (sc && sc->element) {
1942                 int err = MM_ERROR_NONE;
1943                 char *err_name = NULL;
1944                 double focal_len = 0.0;
1945
1946                 /* Video input device */
1947                 if (sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst) {
1948                         /* Exif related information */
1949                         control = GST_CAMERA_CONTROL(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst);
1950                         if (control != NULL) {
1951                                 gst_camera_control_get_exif_info(control, &exif_info); /* get video input device information */
1952                                 if (exif_info.focal_len_denominator != 0)
1953                                         focal_len = ((double)exif_info.focal_len_numerator) / ((double) exif_info.focal_len_denominator);
1954                         } else {
1955                                 _mmcam_dbg_err("Fail to get camera control interface!");
1956                                 focal_len = 0.0;
1957                         }
1958                 }
1959
1960                 /* Set values to attributes */
1961                 err = mm_camcorder_set_attributes(handle, &err_name,
1962                         MMCAM_CAMERA_FOCAL_LENGTH, focal_len,
1963                         NULL);
1964                 if (err != MM_ERROR_NONE) {
1965                         _mmcam_dbg_err("Set attributes error(%s:%x)!", err_name, err);
1966                         SAFE_FREE(err_name);
1967                         return FALSE;
1968                 }
1969         } else {
1970                 _mmcam_dbg_warn("Sub context isn't exist.");
1971                 return FALSE;
1972         }
1973
1974         return TRUE;
1975 }
1976
1977 static guint32 _mmcamcorder_convert_fourcc_string_to_value(const gchar* format_name)
1978 {
1979         return format_name[0] | (format_name[1] << 8) | (format_name[2] << 16) | (format_name[3] << 24);
1980 }
1981
1982 static GstPadProbeReturn __mmcamcorder_video_dataprobe_preview(GstPad *pad, GstPadProbeInfo *info, gpointer u_data)
1983 {
1984         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(u_data);
1985         _MMCamcorderSubContext *sc = NULL;
1986         _MMCamcorderKPIMeasure *kpi = NULL;
1987         GstBuffer *buffer = GST_PAD_PROBE_INFO_BUFFER(info);
1988
1989         mmf_return_val_if_fail(buffer, GST_PAD_PROBE_DROP);
1990         mmf_return_val_if_fail(gst_buffer_n_memory(buffer), GST_PAD_PROBE_DROP);
1991         mmf_return_val_if_fail(hcamcorder, GST_PAD_PROBE_DROP);
1992
1993         sc = MMF_CAMCORDER_SUBCONTEXT(u_data);
1994         mmf_return_val_if_fail(sc, GST_PAD_PROBE_DROP);
1995
1996         if (sc->drop_vframe > 0) {
1997                 if (sc->pass_first_vframe > 0) {
1998                         sc->pass_first_vframe--;
1999                         _mmcam_dbg_log("Pass video frame by pass_first_vframe");
2000                 } else {
2001                         sc->drop_vframe--;
2002                         _mmcam_dbg_log("Drop video frame by drop_vframe");
2003                         return GST_PAD_PROBE_DROP;
2004                 }
2005         } else if (sc->frame_stability_count > 0) {
2006                 sc->frame_stability_count--;
2007                 _mmcam_dbg_log("Drop video frame by frame_stability_count");
2008                 return GST_PAD_PROBE_DROP;
2009         }
2010
2011         if (hcamcorder->state >= MM_CAMCORDER_STATE_PREPARE) {
2012                 int diff_sec;
2013                 int frame_count = 0;
2014                 struct timeval current_video_time;
2015
2016                 kpi = &(sc->kpi);
2017                 if (kpi->init_video_time.tv_sec == kpi->last_video_time.tv_sec &&
2018                     kpi->init_video_time.tv_usec == kpi->last_video_time.tv_usec &&
2019                     kpi->init_video_time.tv_usec  == 0) {
2020                         /*
2021                         _mmcam_dbg_log("START to measure FPS");
2022                         */
2023                         gettimeofday(&(kpi->init_video_time), NULL);
2024                 }
2025
2026                 frame_count = ++(kpi->video_framecount);
2027
2028                 gettimeofday(&current_video_time, NULL);
2029                 diff_sec = current_video_time.tv_sec - kpi->last_video_time.tv_sec;
2030                 if (diff_sec != 0) {
2031                         kpi->current_fps = (frame_count - kpi->last_framecount) / diff_sec;
2032                         if ((current_video_time.tv_sec - kpi->init_video_time.tv_sec) != 0) {
2033                                 int framecount = kpi->video_framecount;
2034                                 int elased_sec = current_video_time.tv_sec - kpi->init_video_time.tv_sec;
2035                                 kpi->average_fps = framecount / elased_sec;
2036                         }
2037
2038                         kpi->last_framecount = frame_count;
2039                         kpi->last_video_time.tv_sec = current_video_time.tv_sec;
2040                         kpi->last_video_time.tv_usec = current_video_time.tv_usec;
2041                         /*
2042                         _mmcam_dbg_log("current fps(%d), average(%d)", kpi->current_fps, kpi->average_fps);
2043                         */
2044                 }
2045         }
2046
2047         /* The first H.264 frame should not be skipped for vstream cb. */
2048         if (hcamcorder->state < MM_CAMCORDER_STATE_PREPARE &&
2049                 sc->info_image->preview_format != MM_PIXEL_FORMAT_ENCODED_H264) {
2050                 _mmcam_dbg_warn("Not ready for stream callback");
2051                 return GST_PAD_PROBE_OK;
2052         }
2053
2054         if (_mmcamcorder_invoke_video_stream_cb((MMHandleType)hcamcorder, buffer, TRUE))
2055                 return GST_PAD_PROBE_OK;
2056         else
2057                 return GST_PAD_PROBE_DROP;
2058 }
2059
2060
2061 static GstPadProbeReturn __mmcamcorder_video_dataprobe_record(GstPad *pad, GstPadProbeInfo *info, gpointer u_data)
2062 {
2063         if (_mmcamcorder_video_push_buffer(u_data, GST_PAD_PROBE_INFO_BUFFER(info)))
2064                 return GST_PAD_PROBE_OK;
2065
2066         return GST_PAD_PROBE_DROP;
2067 }
2068
2069
2070 GstPadProbeReturn __mmcamcorder_muxed_dataprobe(GstPad *pad, GstPadProbeInfo *info, gpointer u_data)
2071 {
2072         MMCamcorderMuxedStreamDataType stream;
2073         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(u_data);
2074         _MMCamcorderSubContext *sc = NULL;
2075         GstBuffer *buffer = GST_PAD_PROBE_INFO_BUFFER(info);
2076         GstMapInfo mapinfo;
2077
2078         mmf_return_val_if_fail(buffer, GST_PAD_PROBE_OK);
2079         mmf_return_val_if_fail(gst_buffer_n_memory(buffer), GST_PAD_PROBE_OK);
2080         mmf_return_val_if_fail(hcamcorder, GST_PAD_PROBE_OK);
2081
2082         sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
2083         mmf_return_val_if_fail(sc, GST_PAD_PROBE_OK);
2084
2085         if (!gst_buffer_map(buffer, &mapinfo, GST_MAP_READ)) {
2086                 _mmcam_dbg_warn("map failed : buffer %p", buffer);
2087                 return GST_PAD_PROBE_OK;
2088         }
2089
2090         /* call application callback */
2091         _MMCAMCORDER_LOCK_MSTREAM_CALLBACK(hcamcorder);
2092
2093         if (hcamcorder->mstream_cb) {
2094                 stream.data = (void *)mapinfo.data;
2095                 stream.length = mapinfo.size;
2096                 stream.offset = sc->muxed_stream_offset;
2097                 hcamcorder->mstream_cb(&stream, hcamcorder->mstream_cb_param);
2098         }
2099
2100         _MMCAMCORDER_UNLOCK_MSTREAM_CALLBACK(hcamcorder);
2101
2102         /* calculate current offset */
2103         sc->muxed_stream_offset += mapinfo.size;
2104
2105         gst_buffer_unmap(buffer, &mapinfo);
2106
2107         return GST_PAD_PROBE_OK;
2108 }
2109
2110
2111 GstPadProbeReturn __mmcamcorder_eventprobe_monitor(GstPad *pad, GstPadProbeInfo *info, gpointer u_data)
2112 {
2113         GstEvent *event = GST_PAD_PROBE_INFO_EVENT(info);
2114         mmf_camcorder_t *hcamcorder = NULL;
2115         _MMCamcorderSubContext *sc = NULL;
2116         GstObject *parent = NULL;
2117
2118         switch (GST_EVENT_TYPE(event)) {
2119         case GST_EVENT_UNKNOWN:
2120         /* upstream events */
2121         case GST_EVENT_QOS:
2122         case GST_EVENT_SEEK:
2123         case GST_EVENT_NAVIGATION:
2124         case GST_EVENT_LATENCY:
2125         /* downstream serialized events */
2126         case GST_EVENT_BUFFERSIZE:
2127                 _mmcam_dbg_log("[%s:%s] gots %s", GST_DEBUG_PAD_NAME(pad), GST_EVENT_TYPE_NAME(event));
2128                 break;
2129         case GST_EVENT_TAG:
2130                 {
2131                         GstTagList *tag_list = NULL;
2132                         _MMCamcorderReplayGain *replay_gain = NULL;
2133
2134                         _mmcam_dbg_log("[%s:%s] gots %s", GST_DEBUG_PAD_NAME(pad), GST_EVENT_TYPE_NAME(event));
2135
2136                         hcamcorder = MMF_CAMCORDER(u_data);
2137                         if (!hcamcorder || !hcamcorder->sub_context) {
2138                                 _mmcam_dbg_warn("NULL handle");
2139                                 break;
2140                         }
2141
2142                         replay_gain = &hcamcorder->sub_context->replay_gain;
2143
2144                         gst_event_parse_tag(event, &tag_list);
2145                         if (!tag_list) {
2146                                 _mmcam_dbg_warn("failed to get tag list");
2147                                 break;
2148                         }
2149
2150                         if (!gst_tag_list_get_double(tag_list, GST_TAG_TRACK_PEAK, &replay_gain->track_peak)) {
2151                                 _mmcam_dbg_warn("failed to get GST_TAG_TRACK_PEAK");
2152                                 break;
2153                         }
2154
2155                         if (!gst_tag_list_get_double(tag_list, GST_TAG_TRACK_GAIN, &replay_gain->track_gain)) {
2156                                 _mmcam_dbg_warn("failed to get GST_TAG_TRACK_GAIN");
2157                                 break;
2158                         }
2159
2160                         if (!gst_tag_list_get_double(tag_list, GST_TAG_ALBUM_PEAK, &replay_gain->album_peak)) {
2161                                 _mmcam_dbg_warn("failed to get GST_TAG_ALBUM_PEAK");
2162                                 break;
2163                         }
2164
2165                         if (!gst_tag_list_get_double(tag_list, GST_TAG_ALBUM_GAIN, &replay_gain->album_gain)) {
2166                                 _mmcam_dbg_warn("failed to get GST_TAG_ALBUM_PEAK");
2167                                 break;
2168                         }
2169
2170                         _mmcam_dbg_log("Track [peak %lf, gain %lf], Album [peak %lf, gain %lf]",
2171                                 replay_gain->track_peak, replay_gain->track_gain,
2172                                 replay_gain->album_peak, replay_gain->album_gain);
2173                 }
2174                 break;
2175         case GST_EVENT_SEGMENT:
2176                 _mmcam_dbg_log("[%s:%s] gots %s", GST_DEBUG_PAD_NAME(pad), GST_EVENT_TYPE_NAME(event));
2177
2178                 hcamcorder = MMF_CAMCORDER(u_data);
2179                 if (!hcamcorder) {
2180                         _mmcam_dbg_warn("NULL handle");
2181                         break;
2182                 }
2183
2184                 sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
2185                 if (!sc) {
2186                         _mmcam_dbg_warn("NULL sub context");
2187                         break;
2188                 }
2189
2190                 if (!sc->encode_element[_MMCAMCORDER_ENCSINK_SINK].gst) {
2191                         _mmcam_dbg_warn("no encoder sink");
2192                         break;
2193                 }
2194
2195                 parent = gst_pad_get_parent(pad);
2196                 if (!parent) {
2197                         _mmcam_dbg_warn("get parent failed");
2198                         break;
2199                 }
2200
2201                 if (parent == (GstObject *)sc->encode_element[_MMCAMCORDER_ENCSINK_SINK].gst) {
2202                         const GstSegment *segment;
2203                         gst_event_parse_segment(event, &segment);
2204                         if (segment->format == GST_FORMAT_BYTES) {
2205                                 _mmcam_dbg_log("change current offset %llu -> %"G_GUINT64_FORMAT,
2206                                         sc->muxed_stream_offset, segment->start);
2207
2208                                 sc->muxed_stream_offset = (unsigned long long)segment->start;
2209                         }
2210                 }
2211
2212                 gst_object_unref(parent);
2213                 parent = NULL;
2214                 break;
2215         case GST_EVENT_EOS:
2216                 _mmcam_dbg_warn("[%s:%s] gots %s", GST_DEBUG_PAD_NAME(pad), GST_EVENT_TYPE_NAME(event));
2217                 break;
2218         /* bidirectional events */
2219         case GST_EVENT_FLUSH_START:
2220         case GST_EVENT_FLUSH_STOP:
2221                 _mmcam_dbg_err("[%s:%s] gots %s", GST_DEBUG_PAD_NAME(pad), GST_EVENT_TYPE_NAME(event));
2222                 break;
2223         default:
2224                 _mmcam_dbg_log("[%s:%s] gots %s", GST_DEBUG_PAD_NAME(pad), GST_EVENT_TYPE_NAME(event));
2225                 break;
2226         }
2227
2228         return GST_PAD_PROBE_OK;
2229 }
2230
2231
2232 int __mmcamcorder_get_amrnb_bitrate_mode(int bitrate)
2233 {
2234         int result = MM_CAMCORDER_MR475;
2235
2236         if (bitrate < 5150)
2237                 result = MM_CAMCORDER_MR475; /*AMR475*/
2238         else if (bitrate < 5900)
2239                 result = MM_CAMCORDER_MR515; /*AMR515*/
2240         else if (bitrate < 6700)
2241                 result = MM_CAMCORDER_MR59; /*AMR59*/
2242         else if (bitrate < 7400)
2243                 result = MM_CAMCORDER_MR67; /*AMR67*/
2244         else if (bitrate < 7950)
2245                 result = MM_CAMCORDER_MR74; /*AMR74*/
2246         else if (bitrate < 10200)
2247                 result = MM_CAMCORDER_MR795; /*AMR795*/
2248         else if (bitrate < 12200)
2249                 result = MM_CAMCORDER_MR102; /*AMR102*/
2250         else
2251                 result = MM_CAMCORDER_MR122; /*AMR122*/
2252
2253         return result;
2254 }
2255
2256
2257 int _mmcamcorder_get_eos_message(MMHandleType handle)
2258 {
2259         int64_t end_time;
2260         int ret = MM_ERROR_NONE;
2261
2262         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
2263         _MMCamcorderSubContext *sc = NULL;
2264
2265         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2266         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2267         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2268
2269         _mmcam_dbg_log("START");
2270
2271         _MMCAMCORDER_LOCK(handle);
2272
2273         if (sc->bget_eos == FALSE) {
2274                 end_time = g_get_monotonic_time() + 3 * G_TIME_SPAN_SECOND;
2275                 if (_MMCAMCORDER_WAIT_UNTIL(handle, end_time)) {
2276                         _mmcam_dbg_log("EOS signal received");
2277                 } else {
2278                         _mmcam_dbg_err("EOS wait time out");
2279
2280                         if (hcamcorder->error_code == MM_ERROR_NONE)
2281                                 hcamcorder->error_code = MM_ERROR_CAMCORDER_RESPONSE_TIMEOUT;
2282                 }
2283         } else {
2284                 _mmcam_dbg_log("already got EOS");
2285         }
2286
2287         _MMCAMCORDER_UNLOCK(handle);
2288
2289         if (hcamcorder->error_code == MM_ERROR_NONE) {
2290                 if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
2291                         mmf_return_val_if_fail(sc->info_video, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2292                         if (sc->info_video->b_commiting)
2293                                 _mmcamcorder_video_handle_eos((MMHandleType)hcamcorder);
2294                 } else {
2295                         mmf_return_val_if_fail(sc->info_audio, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2296                         if (sc->info_audio->b_commiting)
2297                                 _mmcamcorder_audio_handle_eos((MMHandleType)hcamcorder);
2298                 }
2299         } else {
2300                 ret = hcamcorder->error_code;
2301                 _mmcam_dbg_err("error 0x%x", ret);
2302         }
2303
2304         _mmcam_dbg_log("END");
2305
2306         return ret;
2307 }
2308
2309
2310 void _mmcamcorder_remove_element_handle(MMHandleType handle, void *element, int first_elem, int last_elem)
2311 {
2312         int i = 0;
2313         _MMCamcorderGstElement *remove_element = (_MMCamcorderGstElement *)element;
2314
2315         mmf_return_if_fail(handle && remove_element);
2316         mmf_return_if_fail((first_elem >= 0) && (last_elem > 0) && (last_elem > first_elem));
2317
2318         _mmcam_dbg_log("");
2319
2320         for (i = first_elem ; i <= last_elem ; i++) {
2321                 remove_element[i].gst = NULL;
2322                 remove_element[i].id = _MMCAMCORDER_NONE;
2323         }
2324
2325         return;
2326 }
2327
2328
2329 int _mmcamcorder_check_codec_fileformat_compatibility(const char *codec_type, int codec, int file_format)
2330 {
2331         mmf_return_val_if_fail(codec_type, MM_ERROR_CAMCORDER_INVALID_ARGUMENT);
2332
2333         /* Check compatibility between codec and file format */
2334         if (!strcmp(codec_type, MMCAM_AUDIO_ENCODER)) {
2335                 if (codec > MM_AUDIO_CODEC_INVALID && codec < MM_AUDIO_CODEC_NUM &&
2336                         file_format > MM_FILE_FORMAT_INVALID && file_format < MM_FILE_FORMAT_NUM) {
2337                         if (audiocodec_fileformat_compatibility_table[codec][file_format] == 0) {
2338                                 _mmcam_dbg_err("Audio codec[%d] and file format[%d] compatibility FAILED.", codec, file_format);
2339                                 return MM_ERROR_CAMCORDER_ENCODER_WRONG_TYPE;
2340                         }
2341
2342                         _mmcam_dbg_log("Audio codec[%d] and file format[%d] compatibility SUCCESS.", codec, file_format);
2343                 } else {
2344                         _mmcam_dbg_err("Audio codec[%d] or file format[%d] is INVALID.", codec, file_format);
2345                         return MM_ERROR_CAMCORDER_ENCODER_WRONG_TYPE;
2346                 }
2347         } else if (!strcmp(codec_type, MMCAM_VIDEO_ENCODER)) {
2348                 if (codec > MM_VIDEO_CODEC_INVALID && codec < MM_VIDEO_CODEC_NUM &&
2349                         file_format > MM_FILE_FORMAT_INVALID && file_format < MM_FILE_FORMAT_NUM) {
2350                         if (videocodec_fileformat_compatibility_table[codec][file_format] == 0) {
2351                                 _mmcam_dbg_err("Video codec[%d] and file format[%d] compatibility FAILED.", codec, file_format);
2352                                 return MM_ERROR_CAMCORDER_ENCODER_WRONG_TYPE;
2353                         }
2354
2355                         _mmcam_dbg_log("Video codec[%d] and file format[%d] compatibility SUCCESS.", codec, file_format);
2356                 } else {
2357                         _mmcam_dbg_err("Video codec[%d] or file format[%d] is INVALID.", codec, file_format);
2358                         return MM_ERROR_CAMCORDER_ENCODER_WRONG_TYPE;
2359                 }
2360         }
2361
2362         return MM_ERROR_NONE;
2363 }
2364
2365
2366 int _mmcamcorder_check_audiocodec_fileformat_compatibility(MMHandleType handle)
2367 {
2368         int err = MM_ERROR_NONE;
2369         int audio_codec = MM_AUDIO_CODEC_INVALID;
2370         int file_format = MM_FILE_FORMAT_INVALID;
2371
2372         char *err_name = NULL;
2373
2374         err = mm_camcorder_get_attributes(handle, &err_name,
2375                 MMCAM_AUDIO_ENCODER, &audio_codec,
2376                 MMCAM_FILE_FORMAT, &file_format,
2377                 NULL);
2378         if (err != MM_ERROR_NONE) {
2379                 _mmcam_dbg_warn("Get attrs fail. (%s:%x)", err_name, err);
2380                 SAFE_FREE(err_name);
2381                 return err;
2382         }
2383
2384         /* Check compatibility between audio codec and file format */
2385         err = _mmcamcorder_check_codec_fileformat_compatibility(MMCAM_AUDIO_ENCODER, audio_codec, file_format);
2386
2387         return err;
2388 }
2389
2390
2391 int _mmcamcorder_check_videocodec_fileformat_compatibility(MMHandleType handle)
2392 {
2393         int err = MM_ERROR_NONE;
2394         int video_codec = MM_VIDEO_CODEC_INVALID;
2395         int file_format = MM_FILE_FORMAT_INVALID;
2396
2397         char *err_name = NULL;
2398
2399         err = mm_camcorder_get_attributes(handle, &err_name,
2400                 MMCAM_VIDEO_ENCODER, &video_codec,
2401                 MMCAM_FILE_FORMAT, &file_format,
2402                 NULL);
2403         if (err != MM_ERROR_NONE) {
2404                 _mmcam_dbg_warn("Get attrs fail. (%s:%x)", err_name, err);
2405                 SAFE_FREE(err_name);
2406                 return err;
2407         }
2408
2409         /* Check compatibility between video codec and file format */
2410         err = _mmcamcorder_check_codec_fileformat_compatibility(MMCAM_VIDEO_ENCODER, video_codec, file_format);
2411
2412         return err;
2413 }
2414
2415
2416 bool _mmcamcorder_set_display_rotation(MMHandleType handle, int display_rotate, int videosink_index)
2417 {
2418         const char* videosink_name = NULL;
2419
2420         mmf_camcorder_t *hcamcorder = NULL;
2421         _MMCamcorderSubContext *sc = NULL;
2422
2423         hcamcorder = MMF_CAMCORDER(handle);
2424         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2425
2426         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2427         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2428         mmf_return_val_if_fail(sc->element, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2429
2430         if (sc->element[videosink_index].gst) {
2431                 /* Get videosink name */
2432                 _mmcamcorder_conf_get_value_element_name(sc->VideosinkElement, &videosink_name);
2433                 if (videosink_name == NULL) {
2434                         _mmcam_dbg_err("Please check videosink element in configuration file");
2435                         return FALSE;
2436                 }
2437
2438                 if (!strcmp(videosink_name, "tizenwlsink") || !strcmp(videosink_name, "xvimagesink") ||
2439                         !strcmp(videosink_name, "evasimagesink") || !strcmp(videosink_name, "evaspixmapsink") ||
2440                         !strcmp(videosink_name, "directvideosink")) {
2441                         MMCAMCORDER_G_OBJECT_SET(sc->element[videosink_index].gst, "rotate", display_rotate);
2442                         _mmcam_dbg_log("Set display-rotate [%d] done.", display_rotate);
2443                 } else {
2444                         _mmcam_dbg_warn("[%s] does not support DISPLAY_ROTATION, but no error", videosink_name);
2445                 }
2446
2447                 return TRUE;
2448         } else {
2449                 _mmcam_dbg_err("Videosink element is null");
2450                 return FALSE;
2451         }
2452 }
2453
2454
2455 bool _mmcamcorder_set_display_flip(MMHandleType handle, int display_flip, int videosink_index)
2456 {
2457         const char* videosink_name = NULL;
2458
2459         mmf_camcorder_t *hcamcorder = NULL;
2460         _MMCamcorderSubContext *sc = NULL;
2461
2462         hcamcorder = MMF_CAMCORDER(handle);
2463         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2464
2465         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2466         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2467         mmf_return_val_if_fail(sc->element, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2468
2469         if (sc->element[videosink_index].gst) {
2470                 /* Get videosink name */
2471                 _mmcamcorder_conf_get_value_element_name(sc->VideosinkElement, &videosink_name);
2472                 if (videosink_name == NULL) {
2473                         _mmcam_dbg_err("Please check videosink element in configuration file");
2474                         return FALSE;
2475                 }
2476
2477                 if (!strcmp(videosink_name, "tizenwlsink") || !strcmp(videosink_name, "xvimagesink") ||
2478                         !strcmp(videosink_name, "evasimagesink") || !strcmp(videosink_name, "evaspixmapsink") ||
2479                         !strcmp(videosink_name, "directvideosink")) {
2480                         MMCAMCORDER_G_OBJECT_SET(sc->element[videosink_index].gst, "flip", display_flip);
2481                         _mmcam_dbg_log("Set display flip [%d] done.", display_flip);
2482                 } else {
2483                         _mmcam_dbg_warn("[%s] does not support DISPLAY_FLIP, but no error", videosink_name);
2484                 }
2485
2486                 return TRUE;
2487         } else {
2488                 _mmcam_dbg_err("Videosink element is null");
2489                 return FALSE;
2490         }
2491 }
2492
2493
2494 bool _mmcamcorder_set_videosrc_rotation(MMHandleType handle, int videosrc_rotate)
2495 {
2496         int fps = 0;
2497         _MMCamcorderSubContext *sc = NULL;
2498
2499         mmf_camcorder_t *hcamcorder = NULL;
2500
2501         hcamcorder = MMF_CAMCORDER(handle);
2502         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2503
2504         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2505         if (!sc) {
2506                 _mmcam_dbg_log("sub context is not initailized");
2507                 return TRUE;
2508         }
2509
2510         mm_camcorder_get_attributes(handle, NULL,
2511                 MMCAM_CAMERA_WIDTH, &sc->info_video->preview_width,
2512                 MMCAM_CAMERA_HEIGHT, &sc->info_video->preview_height,
2513                 MMCAM_CAMERA_FPS, &fps,
2514                 NULL);
2515
2516         _mmcam_dbg_log("set rotate %d", videosrc_rotate);
2517
2518         return _mmcamcorder_set_videosrc_caps(handle, sc->fourcc,
2519                 sc->info_video->preview_width, sc->info_video->preview_height,
2520                 fps, videosrc_rotate);
2521 }
2522
2523
2524 bool _mmcamcorder_set_videosrc_caps(MMHandleType handle, unsigned int fourcc, int width, int height, int fps, int rotate)
2525 {
2526         int set_width = 0;
2527         int set_height = 0;
2528         int set_rotate = 0;
2529         int fps_auto = 0;
2530         unsigned int caps_fourcc = 0;
2531         gboolean do_set_caps = FALSE;
2532
2533         GstCaps *caps = NULL;
2534
2535         mmf_camcorder_t *hcamcorder = NULL;
2536         _MMCamcorderSubContext *sc = NULL;
2537
2538         hcamcorder = MMF_CAMCORDER(handle);
2539         mmf_return_val_if_fail(hcamcorder, FALSE);
2540
2541         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2542
2543         if (!sc || !(sc->element)) {
2544                 _mmcam_dbg_log("sub context is not initialized");
2545                 return TRUE;
2546         }
2547
2548         if (!sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst) {
2549                 _mmcam_dbg_err("Video src is NULL!");
2550                 return FALSE;
2551         }
2552
2553         if (!sc->element[_MMCAMCORDER_VIDEOSRC_FILT].gst) {
2554                 _mmcam_dbg_err("Video filter is NULL!");
2555                 return FALSE;
2556         }
2557
2558         if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
2559                 int capture_width = 0;
2560                 int capture_height = 0;
2561                 double motion_rate = _MMCAMCORDER_DEFAULT_RECORDING_MOTION_RATE;
2562
2563                 mm_camcorder_get_attributes(handle, NULL,
2564                         MMCAM_CAMERA_RECORDING_MOTION_RATE, &motion_rate,
2565                         MMCAM_CAMERA_FPS_AUTO, &fps_auto,
2566                         MMCAM_CAPTURE_WIDTH, &capture_width,
2567                         MMCAM_CAPTURE_HEIGHT, &capture_height,
2568                         MMCAM_VIDEO_WIDTH, &sc->info_video->video_width,
2569                         MMCAM_VIDEO_HEIGHT, &sc->info_video->video_height,
2570                         NULL);
2571                 _mmcam_dbg_log("motion rate %f, capture size %dx%d, fps auto %d, video size %dx%d",
2572                         motion_rate, capture_width, capture_height, fps_auto,
2573                         sc->info_video->video_width, sc->info_video->video_height);
2574
2575                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst,
2576                         "high-speed-fps", (motion_rate != _MMCAMCORDER_DEFAULT_RECORDING_MOTION_RATE ? fps : 0));
2577
2578                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "capture-width", capture_width);
2579                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "capture-height", capture_height);
2580                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "fps-auto", fps_auto);
2581
2582                 /* set fps */
2583                 sc->info_video->fps = fps;
2584         }
2585
2586         /* Interleaved format does not support rotation */
2587         if (sc->info_image->preview_format != MM_PIXEL_FORMAT_ITLV_JPEG_UYVY) {
2588                 /* store videosrc rotation */
2589                 sc->videosrc_rotate = rotate;
2590
2591                 /* Define width, height and rotate in caps */
2592
2593                 /* This will be applied when rotate is 0, 90, 180, 270 if rear camera.
2594                 This will be applied when rotate is 0, 180 if front camera. */
2595                 set_rotate = rotate * 90;
2596
2597                 if (rotate == MM_VIDEO_INPUT_ROTATION_90 ||
2598                     rotate == MM_VIDEO_INPUT_ROTATION_270) {
2599                         set_width = height;
2600                         set_height = width;
2601                         if (hcamcorder->device_type == MM_VIDEO_DEVICE_CAMERA1) {
2602                                 if (rotate == MM_VIDEO_INPUT_ROTATION_90)
2603                                         set_rotate = 270;
2604                                 else
2605                                         set_rotate = 90;
2606                         }
2607                 } else {
2608                         set_width = width;
2609                         set_height = height;
2610                 }
2611         } else {
2612                 sc->videosrc_rotate = MM_VIDEO_INPUT_ROTATION_NONE;
2613                 set_rotate = 0;
2614                 set_width = width;
2615                 set_height = height;
2616
2617                 _mmcam_dbg_warn("ITLV format doe snot support INPUT ROTATE. Ignore ROTATE[%d]", rotate);
2618         }
2619
2620         MMCAMCORDER_G_OBJECT_GET(sc->element[_MMCAMCORDER_VIDEOSRC_FILT].gst, "caps", &caps);
2621         if (caps && !gst_caps_is_any(caps)) {
2622                 GstStructure *structure = NULL;
2623
2624                 structure = gst_caps_get_structure(caps, 0);
2625                 if (structure) {
2626                         const gchar *format_string = NULL;
2627                         int caps_width = 0;
2628                         int caps_height = 0;
2629                         int caps_fps = 0;
2630                         int caps_rotate = 0;
2631
2632                         format_string = gst_structure_get_string(structure, "format");
2633                         if (format_string)
2634                                 caps_fourcc = _mmcamcorder_convert_fourcc_string_to_value(format_string);
2635
2636                         gst_structure_get_int(structure, "width", &caps_width);
2637                         gst_structure_get_int(structure, "height", &caps_height);
2638                         gst_structure_get_int(structure, "fps", &caps_fps);
2639                         gst_structure_get_int(structure, "rotate", &caps_rotate);
2640
2641 #ifdef _MMCAMCORDER_PRODUCT_TV
2642                         if (_mmcamcorder_is_encoded_preview_pixel_format(sc->info_image->preview_format)) {
2643                                 if (set_width == caps_width && set_height == caps_height &&
2644                                         set_rotate == caps_rotate && fps == caps_fps) {
2645                                         _mmcam_dbg_log("No need to replace caps.");
2646                                 } else {
2647                                         _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]",
2648                                                 caps_fourcc, caps_fourcc>>8, caps_fourcc>>16, caps_fourcc>>24,
2649                                                 caps_width, caps_height, caps_fps, caps_rotate,
2650                                                 fourcc, fourcc>>8, fourcc>>16, fourcc>>24,
2651                                                 set_width, set_height, fps, set_rotate);
2652                                         do_set_caps = TRUE;
2653                                 }
2654                         } else {
2655                                 if (set_width == caps_width && set_height == caps_height &&
2656                                     fourcc == caps_fourcc && set_rotate == caps_rotate && fps == caps_fps) {
2657                                         _mmcam_dbg_log("No need to replace caps.");
2658                                 } else {
2659                                         _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]",
2660                                                 caps_fourcc, caps_fourcc>>8, caps_fourcc>>16, caps_fourcc>>24,
2661                                                 caps_width, caps_height, caps_fps, caps_rotate,
2662                                                 fourcc, fourcc>>8, fourcc>>16, fourcc>>24,
2663                                                 set_width, set_height, fps, set_rotate);
2664                                         do_set_caps = TRUE;
2665                                 }
2666                         }
2667 #else /*_MMCAMCORDER_PRODUCT_TV */
2668                         if (set_width == caps_width && set_height == caps_height &&
2669                             fourcc == caps_fourcc && set_rotate == caps_rotate && fps == caps_fps) {
2670                                 _mmcam_dbg_log("No need to replace caps.");
2671                         } else {
2672                                 _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]",
2673                                         caps_fourcc, caps_fourcc>>8, caps_fourcc>>16, caps_fourcc>>24,
2674                                         caps_width, caps_height, caps_fps, caps_rotate,
2675                                         fourcc, fourcc>>8, fourcc>>16, fourcc>>24,
2676                                         set_width, set_height, fps, set_rotate);
2677                                 do_set_caps = TRUE;
2678                         }
2679 #endif /*_MMCAMCORDER_PRODUCT_TV */
2680                 } else {
2681                         _mmcam_dbg_log("can not get structure of caps. set new one...");
2682                         do_set_caps = TRUE;
2683                 }
2684         } else {
2685                 _mmcam_dbg_log("No caps. set new one...");
2686                 do_set_caps = TRUE;
2687         }
2688
2689         if (caps) {
2690                 gst_caps_unref(caps);
2691                 caps = NULL;
2692         }
2693
2694         if (do_set_caps) {
2695                 if (sc->info_image->preview_format == MM_PIXEL_FORMAT_ENCODED_H264) {
2696 #ifdef _MMCAMCORDER_PRODUCT_TV
2697                         gint maxwidth = 0;
2698                         gint maxheight = 0;
2699                         int display_surface_type = MM_DISPLAY_SURFACE_NULL;
2700                         mm_camcorder_get_attributes(handle, NULL,
2701                                         MMCAM_DISPLAY_SURFACE, &display_surface_type,
2702                                         NULL);
2703
2704                         if (display_surface_type != MM_DISPLAY_SURFACE_NULL &&
2705                                 __mmcamcorder_find_max_resolution(handle, &maxwidth, &maxheight) == false) {
2706                                 _mmcam_dbg_err("can not find max resolution limitation");
2707                                 return false;
2708                         } else if (display_surface_type == MM_DISPLAY_SURFACE_NULL) {
2709                                 maxwidth = set_width;
2710                                 maxheight = set_height;
2711                         }
2712 #endif /* _MMCAMCORDER_PRODUCT_TV */
2713                         caps = gst_caps_new_simple("video/x-h264",
2714                                 "width", G_TYPE_INT, set_width,
2715                                 "height", G_TYPE_INT, set_height,
2716                                 "framerate", GST_TYPE_FRACTION, fps, 1,
2717                                 "stream-format", G_TYPE_STRING, "byte-stream",
2718 #ifdef _MMCAMCORDER_PRODUCT_TV
2719                                 "maxwidth", G_TYPE_INT, maxwidth,
2720                                 "maxheight", G_TYPE_INT, maxheight,
2721                                 "alignment", G_TYPE_STRING, "au",
2722 #endif /* _MMCAMCORDER_PRODUCT_TV */
2723                                 NULL);
2724                 } else if (sc->info_image->preview_format == MM_PIXEL_FORMAT_ENCODED_MJPEG){
2725 #ifdef _MMCAMCORDER_PRODUCT_TV
2726                         caps = gst_caps_new_simple("video/x-jpeg",
2727 #else
2728                         caps = gst_caps_new_simple("image/jpeg",
2729 #endif
2730                                 "width", G_TYPE_INT, set_width,
2731                                 "height", G_TYPE_INT, set_height,
2732                                 "framerate", GST_TYPE_FRACTION, fps, 1,
2733                                 NULL);
2734                 } else {
2735                         char fourcc_string[sizeof(fourcc)+1];
2736                         memcpy(fourcc_string, (char*)&fourcc, sizeof(fourcc));
2737                         fourcc_string[sizeof(fourcc)] = '\0';
2738                         caps = gst_caps_new_simple("video/x-raw",
2739                                 "format", G_TYPE_STRING, fourcc_string,
2740                                 "width", G_TYPE_INT, set_width,
2741                                 "height", G_TYPE_INT, set_height,
2742                                 "framerate", GST_TYPE_FRACTION, fps, 1,
2743                                 "rotate", G_TYPE_INT, set_rotate,
2744                                 NULL);
2745                 }
2746
2747                 if (caps) {
2748                         gchar *caps_str = gst_caps_to_string(caps);
2749
2750                         if (caps_str) {
2751                                 _mmcam_dbg_log("vidoesrc new caps set [%s]", caps_str);
2752                                 g_free(caps_str);
2753                                 caps_str = NULL;
2754                         } else {
2755                                 _mmcam_dbg_warn("caps string failed");
2756                         }
2757
2758                         MMCAMCORDER_G_OBJECT_SET_POINTER(sc->element[_MMCAMCORDER_VIDEOSRC_FILT].gst, "caps", caps);
2759                         gst_caps_unref(caps);
2760                         caps = NULL;
2761                 } else {
2762                         _mmcam_dbg_err("There are no caps");
2763                 }
2764         }
2765
2766         if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
2767                 /* assume that it's camera capture mode */
2768                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSINK_SINK].gst, "csc-range", 1);
2769         }
2770
2771         return TRUE;
2772 }
2773
2774
2775 bool _mmcamcorder_set_videosrc_flip(MMHandleType handle, int videosrc_flip)
2776 {
2777         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
2778         _MMCamcorderSubContext *sc = NULL;
2779
2780         mmf_return_val_if_fail(hcamcorder, FALSE);
2781
2782         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2783         if (!sc)
2784                 return TRUE;
2785
2786         _mmcam_dbg_log("Set FLIP %d", videosrc_flip);
2787
2788         if (sc->element && sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst) {
2789                 int hflip = 0;
2790                 int vflip = 0;
2791
2792                 /* Interleaved format does not support FLIP */
2793                 if (sc->info_image->preview_format != MM_PIXEL_FORMAT_ITLV_JPEG_UYVY) {
2794                         hflip = (videosrc_flip & MM_FLIP_HORIZONTAL) == MM_FLIP_HORIZONTAL;
2795                         vflip = (videosrc_flip & MM_FLIP_VERTICAL) == MM_FLIP_VERTICAL;
2796
2797                         _mmcam_dbg_log("videosrc flip H:%d, V:%d", hflip, vflip);
2798
2799                         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "hflip", hflip);
2800                         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "vflip", vflip);
2801                 } else {
2802                         _mmcam_dbg_warn("ITLV format does not support FLIP. Ignore FLIP[%d]",
2803                                 videosrc_flip);
2804                 }
2805         } else {
2806                 _mmcam_dbg_warn("element is NULL");
2807                 return FALSE;
2808         }
2809
2810         return TRUE;
2811 }
2812
2813
2814 bool _mmcamcorder_set_videosrc_anti_shake(MMHandleType handle, int anti_shake)
2815 {
2816         GstCameraControl *control = NULL;
2817         _MMCamcorderSubContext *sc = NULL;
2818         GstElement *v_src = NULL;
2819
2820         int set_value = 0;
2821
2822         mmf_return_val_if_fail(handle, FALSE);
2823
2824         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2825         if (!sc)
2826                 return TRUE;
2827
2828         v_src = sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst;
2829
2830         if (!v_src) {
2831                 _mmcam_dbg_warn("videosrc element is NULL");
2832                 return FALSE;
2833         }
2834
2835         set_value = _mmcamcorder_convert_msl_to_sensor(handle, MM_CAM_CAMERA_ANTI_HANDSHAKE, anti_shake);
2836
2837         /* set anti-shake with camera control */
2838         if (!GST_IS_CAMERA_CONTROL(v_src)) {
2839                 _mmcam_dbg_warn("Can't cast Video source into camera control.");
2840                 return FALSE;
2841         }
2842
2843         control = GST_CAMERA_CONTROL(v_src);
2844         if (gst_camera_control_set_ahs(control, set_value)) {
2845                 _mmcam_dbg_log("Succeed in operating anti-handshake. value[%d]", set_value);
2846                 return TRUE;
2847         } else {
2848                 _mmcam_dbg_warn("Failed to operate anti-handshake. value[%d]", set_value);
2849         }
2850
2851         return FALSE;
2852 }
2853
2854
2855 bool _mmcamcorder_set_videosrc_stabilization(MMHandleType handle, int stabilization)
2856 {
2857         _MMCamcorderSubContext *sc = NULL;
2858         GstElement *v_src = NULL;
2859
2860         mmf_return_val_if_fail(handle, FALSE);
2861
2862         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2863         if (!sc)
2864                 return TRUE;
2865
2866         v_src = sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst;
2867         if (!v_src) {
2868                 _mmcam_dbg_warn("videosrc element is NULL");
2869                 return FALSE;
2870         }
2871
2872         /* check property of videosrc element - support VDIS */
2873         if (g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(v_src)), "enable-vdis-mode")) {
2874                 int video_width = 0;
2875                 int video_height = 0;
2876
2877                 if (stabilization == MM_CAMCORDER_VIDEO_STABILIZATION_ON) {
2878                         _mmcam_dbg_log("ENABLE video stabilization");
2879
2880                         /* VDIS mode only supports NV12 and [720p or 1080p or 1088 * 1088] */
2881                         mm_camcorder_get_attributes(handle, NULL,
2882                                 MMCAM_VIDEO_WIDTH, &video_width,
2883                                 MMCAM_VIDEO_HEIGHT, &video_height,
2884                                 NULL);
2885
2886                         if (sc->info_image->preview_format == MM_PIXEL_FORMAT_NV12 && video_width >= 1080 && video_height >= 720) {
2887                                 _mmcam_dbg_log("NV12, video size %dx%d, ENABLE video stabilization",
2888                                         video_width, video_height);
2889                                 /* set vdis mode */
2890                                 g_object_set(G_OBJECT(v_src),
2891                                                          "enable-vdis-mode", TRUE,
2892                                                          NULL);
2893                         } else {
2894                                 _mmcam_dbg_warn("invalid preview format %c%c%c%c or video size %dx%d",
2895                                                                 sc->fourcc, sc->fourcc>>8, sc->fourcc>>16, sc->fourcc>>24,
2896                                                                 video_width, video_height);
2897                                 return FALSE;
2898                         }
2899                 } else {
2900                         /* set vdis mode */
2901                         g_object_set(G_OBJECT(v_src),
2902                                                 "enable-vdis-mode", FALSE,
2903                                                 NULL);
2904
2905                         _mmcam_dbg_log("DISABLE video stabilization");
2906                 }
2907         } else if (stabilization == MM_CAMCORDER_VIDEO_STABILIZATION_ON) {
2908                 _mmcam_dbg_err("no property for video stabilization, so can not set ON");
2909                 return FALSE;
2910         } else {
2911                 _mmcam_dbg_warn("no property for video stabilization");
2912         }
2913
2914         return TRUE;
2915 }
2916
2917
2918 bool _mmcamcorder_set_camera_resolution(MMHandleType handle, int width, int height)
2919 {
2920         int fps = 0;
2921
2922         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
2923         _MMCamcorderSubContext *sc = NULL;
2924
2925         mmf_return_val_if_fail(hcamcorder, FALSE);
2926
2927         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2928         if (!sc)
2929                 return TRUE;
2930
2931         mm_camcorder_get_attributes(handle, NULL,
2932                 MMCAM_CAMERA_FPS, &fps,
2933                 NULL);
2934
2935         _mmcam_dbg_log("set %dx%d", width, height);
2936
2937         return _mmcamcorder_set_videosrc_caps(handle, sc->fourcc, width, height, fps, sc->videosrc_rotate);
2938 }
2939
2940
2941 bool _mmcamcorder_set_encoded_preview_bitrate(MMHandleType handle, int bitrate)
2942 {
2943         _MMCamcorderSubContext *sc = NULL;
2944
2945         if ((void *)handle == NULL) {
2946                 _mmcam_dbg_warn("handle is NULL");
2947                 return FALSE;
2948         }
2949
2950         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2951         if (!sc) {
2952                 _mmcam_dbg_warn("subcontext is NULL");
2953                 return FALSE;
2954         }
2955
2956         if (sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst == NULL) {
2957                 _mmcam_dbg_warn("videosrc plugin is NULL");
2958                 return FALSE;
2959         }
2960
2961         _mmcam_dbg_log("set encoded preview bitrate : %d bps", bitrate);
2962
2963         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "bitrate", bitrate);
2964
2965         return TRUE;
2966 }
2967
2968
2969 bool _mmcamcorder_set_encoded_preview_gop_interval(MMHandleType handle, int gop_interval)
2970 {
2971         _MMCamcorderSubContext *sc = NULL;
2972
2973         if ((void *)handle == NULL) {
2974                 _mmcam_dbg_warn("handle is NULL");
2975                 return FALSE;
2976         }
2977
2978         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2979         if (!sc) {
2980                 _mmcam_dbg_warn("subcontext is NULL");
2981                 return FALSE;
2982         }
2983
2984         if (sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst == NULL) {
2985                 _mmcam_dbg_warn("videosrc plugin is NULL");
2986                 return FALSE;
2987         }
2988
2989         _mmcam_dbg_log("set encoded preview GOP interval : %d ms", gop_interval);
2990
2991         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "gop-interval", gop_interval);
2992
2993         return TRUE;
2994 }
2995
2996
2997 bool _mmcamcorder_set_sound_stream_info(GstElement *element, char *stream_type, int stream_index)
2998 {
2999         GstStructure *props = NULL;
3000         char stream_props[64] = {'\0',};
3001
3002         if (element == NULL || stream_type == NULL || stream_index < 0) {
3003                 _mmcam_dbg_err("invalid argument %p %p %d", element, stream_type, stream_index);
3004                 return FALSE;
3005         }
3006
3007         snprintf(stream_props, sizeof(stream_props) - 1,
3008                 "props,media.role=%s, media.parent_id=%d",
3009                 stream_type, stream_index);
3010
3011         _mmcam_dbg_warn("stream type %s, index %d -> [%s]", stream_type, stream_index, stream_props);
3012
3013         props = gst_structure_from_string(stream_props, NULL);
3014         if (!props) {
3015                 _mmcam_dbg_err("failed to create GstStructure");
3016                 return FALSE;
3017         }
3018
3019         MMCAMCORDER_G_OBJECT_SET_POINTER(element, "stream-properties", props);
3020
3021         gst_structure_free(props);
3022         props = NULL;
3023
3024         return TRUE;
3025 }
3026
3027
3028 bool _mmcamcorder_recreate_decoder_for_encoded_preview(MMHandleType handle)
3029 {
3030         int ret = MM_ERROR_NONE;
3031         _MMCamcorderSubContext *sc = NULL;
3032         mmf_camcorder_t *hcamcorder = NULL;
3033         const char *videodecoder_name = NULL;
3034         int display_surface_type = MM_DISPLAY_SURFACE_NULL;
3035 #ifdef _MMCAMCORDER_RM_SUPPORT
3036         char decoder_name[20] = {'\0',};
3037         int decoder_index = 0;
3038 #endif /* _MMCAMCORDER_RM_SUPPORT */
3039
3040         if ((void *)handle == NULL) {
3041                 _mmcam_dbg_warn("handle is NULL");
3042                 return FALSE;
3043         }
3044
3045         hcamcorder = MMF_CAMCORDER(handle);
3046
3047         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
3048         if (!sc) {
3049                 _mmcam_dbg_warn("subcontext is NULL");
3050                 return FALSE;
3051         }
3052
3053         if (_mmcamcorder_is_encoded_preview_pixel_format(sc->info_image->preview_format) == false ||
3054                 hcamcorder->recreate_decoder == FALSE) {
3055                 _mmcam_dbg_log("skip this fuction - format %d, recreate decoder %d",
3056                         sc->info_image->preview_format, hcamcorder->recreate_decoder);
3057                 return TRUE;
3058         }
3059
3060         mm_camcorder_get_attributes(handle, NULL,
3061                 MMCAM_DISPLAY_SURFACE, &display_surface_type,
3062                 NULL);
3063
3064         if (display_surface_type == MM_DISPLAY_SURFACE_NULL) {
3065                 _mmcam_dbg_log("no need to control decoder element");
3066                 return TRUE;
3067         }
3068
3069         if (sc->element[_MMCAMCORDER_MAIN_PIPE].gst == NULL ||
3070             sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst == NULL) {
3071                 _mmcam_dbg_warn("main pipeline or decoder plugin is NULL");
3072                 return FALSE;
3073         }
3074
3075         _mmcam_dbg_log("start");
3076
3077         _mmcamcorder_conf_get_value_element_name(sc->VideodecoderElementH264, &videodecoder_name);
3078         if (videodecoder_name == NULL) {
3079                 _mmcam_dbg_err("failed to get decoder element name from %p", sc->VideodecoderElementH264);
3080                 return FALSE;
3081         }
3082
3083         /* set state as NULL */
3084         ret = _mmcamcorder_gst_set_state(handle, sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst, GST_STATE_NULL);
3085         if (ret != MM_ERROR_NONE) {
3086                 _mmcam_dbg_err("failed to set NULL to decoder");
3087                 return FALSE;
3088         }
3089
3090         /* remove decoder - pads will be unlinked automatically in remove function */
3091         if (!gst_bin_remove(GST_BIN(sc->element[_MMCAMCORDER_MAIN_PIPE].gst),
3092                             sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst)) {
3093                 _mmcam_dbg_err("failed to remove decoder from pipeline");
3094                 return FALSE;
3095         }
3096
3097         /* check decoder element */
3098         if (sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst) {
3099                 _mmcam_dbg_log("decoder[%p] is still alive - ref count %d",
3100                         G_OBJECT(sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst),
3101                         ((GObject *)sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst)->ref_count);
3102         }
3103
3104 #ifdef _MMCAMCORDER_RM_SUPPORT
3105         if (sc->info_image->preview_format == MM_PIXEL_FORMAT_ENCODED_H264) {
3106                 if (hcamcorder->request_resources.category_id[0] == RM_CATEGORY_VIDEO_DECODER_SUB)
3107                         decoder_index = 1;
3108
3109                 snprintf(decoder_name, sizeof(decoder_name)-1, "%s%d", videodecoder_name, decoder_index);
3110                 _mmcam_dbg_log("encoded preview decoder_name %s", decoder_name);
3111         } else {
3112                 /* MJPEG */
3113                 if (hcamcorder->request_resources.category_id[0] == RM_CATEGORY_VIDEO_DECODER)
3114                         snprintf(decoder_name, sizeof(decoder_name)-1, "%s", "omx_uhd_mjpegdec");
3115                 else
3116                         snprintf(decoder_name, sizeof(decoder_name)-1, "%s", "omx_mjpegdec");
3117                 _mmcam_dbg_log("encoded preview decoder_name %s", decoder_name);
3118         }
3119
3120         /* create decoder */
3121         sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst = gst_element_factory_make(decoder_name, "videosrc_decode");
3122         if (sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst == NULL) {
3123                 _mmcam_dbg_err("Decoder[%s] creation fail", decoder_name);
3124                 return FALSE;
3125         }
3126 #else /* _MMCAMCORDER_RM_SUPPORT */
3127         /* create new decoder */
3128         sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst = gst_element_factory_make(videodecoder_name, "videosrc_decode");
3129         if (sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst == NULL) {
3130                 _mmcam_dbg_err("Decoder [%s] creation fail", videodecoder_name);
3131                 return FALSE;
3132         }
3133 #endif /* _MMCAMCORDER_RM_SUPPORT */
3134         _mmcamcorder_conf_set_value_element_property(sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst, sc->VideodecoderElementH264);
3135
3136         sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].id = _MMCAMCORDER_VIDEOSRC_DECODE;
3137         g_object_weak_ref(G_OBJECT(sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst),
3138                 (GWeakNotify)_mmcamcorder_element_release_noti, sc);
3139
3140         /* add to pipeline */
3141         if (!gst_bin_add(GST_BIN(sc->element[_MMCAMCORDER_MAIN_PIPE].gst),
3142                 sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst)) {
3143                 _mmcam_dbg_err("failed to add decoder to pipeline");
3144                 gst_object_unref(sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst);
3145                 return FALSE;
3146         }
3147
3148         /* link */
3149         if (_MM_GST_ELEMENT_LINK(GST_ELEMENT(sc->element[_MMCAMCORDER_VIDEOSRC_QUE].gst),
3150                 GST_ELEMENT(sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst))) {
3151                 _mmcam_dbg_log("Link videosrc_queue to decoder OK");
3152         } else {
3153                 _mmcam_dbg_err("Link videosrc_queue to decoder FAILED");
3154                 return FALSE;
3155         }
3156
3157         if (_MM_GST_ELEMENT_LINK(GST_ELEMENT(sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst),
3158                 GST_ELEMENT(sc->element[_MMCAMCORDER_VIDEOSINK_QUE].gst))) {
3159                 _mmcam_dbg_log("Link decoder to videosink_queue OK");
3160         } else {
3161                 _mmcam_dbg_err("Link decoder to videosink_queue FAILED");
3162                 return FALSE;
3163         }
3164
3165         /* set state READY */
3166         ret = _mmcamcorder_gst_set_state(handle, sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst, GST_STATE_READY);
3167         if (ret != MM_ERROR_NONE) {
3168                 _mmcam_dbg_err("failed to set READY to decoder");
3169                 return FALSE;
3170         }
3171
3172         _mmcam_dbg_log("done");
3173
3174         return TRUE;
3175 }
3176
3177 #ifdef _MMCAMCORDER_PRODUCT_TV
3178 static bool __mmcamcorder_find_max_resolution(MMHandleType handle, gint *max_width, gint *max_height)
3179 {
3180         _MMCamcorderSubContext *sc = NULL;
3181         mmf_camcorder_t *hcamcorder = NULL;
3182         int index = 0;
3183         const gchar *mime = NULL;
3184         GstPad *sinkpad;
3185         GstCaps *decsink_caps = NULL;
3186         GstStructure *decsink_struct = NULL;
3187
3188         mmf_return_val_if_fail(handle, false);
3189         mmf_return_val_if_fail(max_width, false);
3190         mmf_return_val_if_fail(max_height, false);
3191
3192         hcamcorder = MMF_CAMCORDER(handle);
3193         mmf_return_val_if_fail(hcamcorder, false);
3194
3195         sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
3196
3197         sinkpad = gst_element_get_static_pad(sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst, "sink");
3198         if (!sinkpad) {
3199                 _mmcam_dbg_err("There are no decoder caps");
3200                 return false;
3201         }
3202
3203         decsink_caps = gst_pad_get_pad_template_caps(sinkpad);
3204         if (!decsink_caps) {
3205                 gst_object_unref(sinkpad);
3206                 _mmcam_dbg_err("There is no decoder sink caps");
3207                 return false;
3208         }
3209
3210         for (index = 0; index < gst_caps_get_size(decsink_caps); index++) {
3211                 decsink_struct = gst_caps_get_structure(decsink_caps, index);
3212                 if (!decsink_struct) {
3213                         _mmcam_dbg_err("There are no structure from caps");
3214                         gst_object_unref(decsink_caps);
3215                         gst_object_unref(sinkpad);
3216                         return false;
3217                 }
3218                 mime = gst_structure_get_name(decsink_struct);
3219                 if (!strcmp(mime, "video/x-h264")) {
3220                         _mmcam_dbg_log("h264 caps structure found");
3221                         if (gst_structure_has_field(decsink_struct, "maxwidth"))
3222                                 *max_width = gst_value_get_int_range_max(gst_structure_get_value(decsink_struct, "maxwidth"));
3223                         if (gst_structure_has_field(decsink_struct, "maxheight"))
3224                                 *max_height = gst_value_get_int_range_max(gst_structure_get_value(decsink_struct, "maxheight"));
3225                         break;
3226                 } else if (!strcmp(mime, "video/x-jpeg")) {
3227                         _mmcam_dbg_log("mjpeg caps structure found");
3228                         if (gst_structure_has_field(decsink_struct, "maxwidth"))
3229                                 *max_width = gst_value_get_int_range_max(gst_structure_get_value(decsink_struct, "maxwidth"));
3230                         if (gst_structure_has_field(decsink_struct, "maxheight"))
3231                                 *max_height = gst_value_get_int_range_max(gst_structure_get_value(decsink_struct, "maxheight"));
3232                         break;
3233                 }
3234         }
3235         _mmcam_dbg_log("maxwidth = %d , maxheight = %d", (int)*max_width, (int)*max_height);
3236         gst_object_unref(decsink_caps);
3237         gst_object_unref(sinkpad);
3238
3239         if (*max_width <= 0 || *max_height <= 0)
3240                 return false;
3241
3242         return true;
3243 }
3244 #endif /* _MMCAMCORDER_PRODUCT_TV */