Support new pixel format - VP8, VP9
[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 static 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 static 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: /* fall through */
230         case MM_PIXEL_FORMAT_ENCODED_VP8:   /* fall through */
231         case MM_PIXEL_FORMAT_ENCODED_VP9:   /* fall through */
232                 stream->data_type = MM_CAM_STREAM_DATA_ENCODED;
233                 stream->data.encoded.data = map_info->data;
234                 stream->data.encoded.length_data = stream->length_total;
235                 stream->data.encoded.is_delta_frame = GST_BUFFER_FLAG_IS_SET(buffer, GST_BUFFER_FLAG_DELTA_UNIT);
236                 stream->num_planes = 1;
237                 break;
238
239         case MM_PIXEL_FORMAT_INVZ:
240                 stream->data_type = MM_CAM_STREAM_DATA_DEPTH;
241                 stream->data.depth.data = map_info->data;
242                 stream->data.depth.length_data = stream->length_total;
243                 stream->stride[0] = stream->width << 1;
244                 stream->elevation[0] = stream->height;
245                 stream->num_planes = 1;
246                 break;
247
248         case MM_PIXEL_FORMAT_RGBA: /* fall through */
249         case MM_PIXEL_FORMAT_ARGB:
250                 stream->data_type = MM_CAM_STREAM_DATA_RGB;
251                 stream->data.rgb.data = map_info->data;
252                 stream->data.rgb.length_data = stream->length_total;
253                 stream->stride[0] = stream->width << 2;
254                 stream->elevation[0] = stream->height;
255                 stream->num_planes = 1;
256                 break;
257
258         default:
259                 MMCAM_LOG_ERROR("unsupported format[%d]", stream->format);
260                 return FALSE;
261         }
262
263         return TRUE;
264 }
265
266
267 static gboolean __mmcamcorder_set_stream_data_tbm(MMCamcorderVideoStreamDataType *stream, tbm_surface_info_s *ts_info)
268 {
269         mmf_return_val_if_fail(ts_info, FALSE);
270         mmf_return_val_if_fail(stream, FALSE);
271
272         switch (stream->format) {
273         case MM_PIXEL_FORMAT_NV12: /* fall through */
274         case MM_PIXEL_FORMAT_NV21:
275                 stream->data_type = MM_CAM_STREAM_DATA_YUV420SP;
276                 stream->num_planes = 2;
277                 stream->data.yuv420sp.y = ts_info->planes[0].ptr;
278                 stream->data.yuv420sp.length_y = ts_info->planes[0].size;
279                 stream->data.yuv420sp.uv = ts_info->planes[1].ptr;
280                 stream->data.yuv420sp.length_uv = ts_info->planes[1].size;
281                 break;
282
283         case MM_PIXEL_FORMAT_I420:
284                 stream->data_type = MM_CAM_STREAM_DATA_YUV420P;
285                 stream->num_planes = 3;
286                 stream->data.yuv420p.y = ts_info->planes[0].ptr;
287                 stream->data.yuv420p.length_y = ts_info->planes[0].size;
288                 stream->data.yuv420p.u = ts_info->planes[1].ptr;
289                 stream->data.yuv420p.length_u = ts_info->planes[1].size;
290                 stream->data.yuv420p.v = ts_info->planes[2].ptr;
291                 stream->data.yuv420p.length_v = ts_info->planes[2].size;
292                 break;
293
294         default:
295                 MMCAM_LOG_ERROR("unsupported format[%d]", stream->format);
296                 return FALSE;
297         }
298
299         return TRUE;
300 }
301
302
303 gboolean _mmcamcorder_invoke_video_stream_cb(MMHandleType handle, GstSample *sample, gboolean is_preview, int stream_id)
304 {
305         int i = 0;
306         int num_bos = 0;
307         gboolean ret_cb = TRUE;
308         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
309         _MMCamcorderSubContext *sc = NULL;
310         MMCamcorderVideoStreamDataType stream;
311
312         tbm_surface_h t_surface = NULL;
313         tbm_surface_info_s ts_info;
314
315         GstBuffer *buffer = NULL;
316         GstMemory *memory = NULL;
317         GstMapInfo map_info;
318         GstCaps *caps = NULL;
319         GstStructure *structure = NULL;
320
321         mmf_return_val_if_fail(hcamcorder, FALSE);
322
323         buffer = gst_sample_get_buffer(sample);
324         mmf_return_val_if_fail(buffer, FALSE);
325         mmf_return_val_if_fail(gst_buffer_n_memory(buffer), FALSE);
326
327         sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
328         mmf_return_val_if_fail(sc, FALSE);
329
330         /* clear data structure */
331         memset(&map_info, 0x0, sizeof(GstMapInfo));
332         memset(&stream, 0x0, sizeof(MMCamcorderVideoStreamDataType));
333
334         caps = gst_sample_get_caps(sample);
335         mmf_return_val_if_fail(caps, FALSE);
336
337         structure = gst_caps_get_structure(caps, 0);
338         mmf_return_val_if_fail(structure, FALSE);
339
340         /* set format and resolution */
341         gst_structure_get_int(structure, "width", &stream.width);
342         gst_structure_get_int(structure, "height", &stream.height);
343         stream.format = _mmcamcorder_get_pixel_format(caps);
344
345         if (_mmcamcorder_is_encoded_preview_pixel_format(stream.format)) {
346                 memory = gst_buffer_get_all_memory(buffer);
347                 stream.internal_buffer = buffer;
348         } else {
349                 memory = gst_buffer_peek_memory(buffer, 0);
350         }
351         if (!memory) {
352                 MMCAM_LOG_ERROR("GstMemory get failed from buffer %p", buffer);
353                 return FALSE;
354         }
355
356         /* set zero-copy related information */
357         if (hcamcorder->use_zero_copy_format) {
358                 t_surface = (tbm_surface_h)gst_tizen_memory_get_surface(memory);
359
360                 if (tbm_surface_get_info(t_surface, &ts_info) != TBM_SURFACE_ERROR_NONE) {
361                         MMCAM_LOG_ERROR("failed to get tbm surface[%p] info", t_surface);
362                         goto _INVOKE_VIDEO_STREAM_CB_DONE;
363                 }
364
365                 /* set bo, stride and elevation */
366                 num_bos = gst_tizen_memory_get_num_bos(memory);
367                 for (i = 0 ; i < num_bos ; i++)
368                         stream.bo[i] = gst_tizen_memory_get_bos(memory, i);
369
370                 for (i = 0 ; i < ts_info.num_planes ; i++) {
371                         stream.stride[i] = ts_info.planes[i].stride;
372                         stream.elevation[i] = ts_info.planes[i].size / ts_info.planes[i].stride;
373                         MMCAM_LOG_VERBOSE("    plane[%d] %dx%d", i, stream.stride[i], stream.elevation[i]);
374                 }
375
376                 stream.length_total = ts_info.size;
377                 stream.internal_buffer = buffer;
378
379                 if (!__mmcamcorder_set_stream_data_tbm(&stream, &ts_info))
380                         goto _INVOKE_VIDEO_STREAM_CB_DONE;
381         } else {
382                 stream.length_total = gst_memory_get_sizes(memory, NULL, NULL);
383
384                 if (!gst_memory_map(memory, &map_info, GST_MAP_READWRITE) ||
385                         !__mmcamcorder_set_stream_data_normal(&stream, buffer, &map_info))
386                         goto _INVOKE_VIDEO_STREAM_CB_DONE;
387         }
388
389         MMCAM_LOG_DEBUG("VideoStreamData : format[%d], resolution[%dx%d], stream_id[%d]",
390                 stream.format, stream.width, stream.height, stream_id);
391
392         stream.timestamp = (unsigned int)(GST_BUFFER_PTS(buffer) / 1000000); /* nano sec -> milli sec */
393         stream.extra_stream_id = stream_id;
394
395         /* invoke application callback */
396         if (is_preview) {
397                 _MMCAMCORDER_LOCK_VSTREAM_CALLBACK(hcamcorder);
398                 if (hcamcorder->vstream_cb)
399                         hcamcorder->vstream_cb(&stream, hcamcorder->vstream_cb_param);
400                 _MMCAMCORDER_UNLOCK_VSTREAM_CALLBACK(hcamcorder);
401         } else {
402                 _MMCAMCORDER_LOCK_VEDECISION_CALLBACK(hcamcorder);
403                 if (hcamcorder->vedecision_cb)
404                         ret_cb = hcamcorder->vedecision_cb(&stream, hcamcorder->vedecision_cb_param);
405                 _MMCAMCORDER_UNLOCK_VEDECISION_CALLBACK(hcamcorder);
406         }
407
408 _INVOKE_VIDEO_STREAM_CB_DONE:
409         for (i = 0 ; i < TBM_SURF_PLANE_MAX && stream.bo[i] ; i++) {
410                 tbm_bo_map(stream.bo[i], TBM_DEVICE_CPU, TBM_OPTION_READ|TBM_OPTION_WRITE);
411                 tbm_bo_unmap(stream.bo[i]);
412         }
413
414         if (map_info.data)
415                 gst_memory_unmap(memory, &map_info);
416
417         if (_mmcamcorder_is_encoded_preview_pixel_format(stream.format))
418                 gst_memory_unref(memory);
419
420         return ret_cb;
421 }
422
423
424 int _mmcamcorder_create_preview_elements(MMHandleType handle)
425 {
426         int err = MM_ERROR_NONE;
427         int i = 0;
428         int camera_rotate = 0;
429         int camera_flip = 0;
430         int fps = 0;
431         int codectype = 0;
432         int capture_width = 0;
433         int capture_height = 0;
434         int capture_jpg_quality = 100;
435         int video_stabilization = 0;
436         int anti_shake = 0;
437         int display_surface_type = MM_DISPLAY_SURFACE_NULL;
438         const char *videosrc_name = NULL;
439         const char *videosink_name = NULL;
440         const char *videoconvert_name = NULL;
441         const char *videodecoder_name = NULL;
442         const char *decoder_conf_name = NULL;
443         char videodecoder_name_final[32] = {'\0',};
444         char *err_name = NULL;
445         char *socket_path = NULL;
446         int socket_path_len;
447 #ifdef _MMCAMCORDER_RM_SUPPORT
448         int decoder_index = 0;
449 #endif /* _MMCAMCORDER_RM_SUPPORT */
450         GstElement *sink_element = NULL;
451         GstCameraControl *control = NULL;
452         int sink_element_size = 0;
453         int *fds = NULL;
454         int fd_number = 0;
455         int extra_preview_enable = 0;
456
457         GList *element_list = NULL;
458
459         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
460         _MMCamcorderSubContext *sc = NULL;
461         type_element *VideosrcElement = NULL;
462         type_int_array *input_index = NULL;
463
464         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
465
466         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
467         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
468         mmf_return_val_if_fail(sc->element, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
469
470         MMCAM_LOG_INFO("");
471
472         /* Check existence */
473         for (i = _MMCAMCORDER_VIDEOSRC_SRC ; i <= _MMCAMCORDER_VIDEOSINK_SINK ; i++) {
474                 if (sc->element[i].gst) {
475                         if (((GObject *)sc->element[i].gst)->ref_count > 0)
476                                 gst_object_unref(sc->element[i].gst);
477
478                         MMCAM_LOG_INFO("element[index:%d] is Already existed.", i);
479                 }
480         }
481
482         /* Get video device index info */
483         _mmcamcorder_conf_get_value_int_array(hcamcorder->conf_ctrl,
484                                                                                   CONFIGURE_CATEGORY_CTRL_CAMERA,
485                                                                                   "InputIndex",
486                                                                                   &input_index);
487         if (input_index == NULL) {
488                 MMCAM_LOG_ERROR("Failed to get input_index");
489                 return MM_ERROR_CAMCORDER_CREATE_CONFIGURE;
490         }
491
492         err = mm_camcorder_get_attributes(handle, &err_name,
493                 MMCAM_CAMERA_FORMAT, &sc->info_image->preview_format,
494                 MMCAM_CAMERA_FPS, &fps,
495                 MMCAM_CAMERA_ROTATION, &camera_rotate,
496                 MMCAM_CAMERA_FLIP, &camera_flip,
497                 MMCAM_CAMERA_VIDEO_STABILIZATION, &video_stabilization,
498                 MMCAM_CAMERA_ANTI_HANDSHAKE, &anti_shake,
499                 MMCAM_CAPTURE_WIDTH, &capture_width,
500                 MMCAM_CAPTURE_HEIGHT, &capture_height,
501                 MMCAM_CAMERA_HDR_CAPTURE, &sc->info_image->hdr_capture_mode,
502                 MMCAM_IMAGE_ENCODER, &codectype,
503                 MMCAM_IMAGE_ENCODER_QUALITY, &capture_jpg_quality,
504                 MMCAM_DISPLAY_SOCKET_PATH, &socket_path, &socket_path_len,
505                 MMCAM_DISPLAY_SURFACE, &display_surface_type,
506                 MMCAM_EXTRA_PREVIEW_ENABLE, &extra_preview_enable,
507                 NULL);
508         if (err != MM_ERROR_NONE) {
509                 MMCAM_LOG_WARNING("Get attrs fail. (%s:%x)", err_name, err);
510                 SAFE_FREE(err_name);
511                 return err;
512         }
513
514         if (hcamcorder->support_user_buffer) {
515                 err = mm_camcorder_get_attributes(handle, NULL,
516                         MMCAM_USER_BUFFER_FD, &fds, &fd_number,
517                         NULL);
518                 if (err != MM_ERROR_NONE || fd_number < 1) {
519                         MMCAM_LOG_ERROR("get user buffer fd failed 0x%x, number %d", err, fd_number);
520                         return err;
521                 }
522
523                 /*
524                 for (i = 0 ; i < fd_number ; i++)
525                         MMCAM_LOG_INFO("fds[%d] %d", i, fds[i]);
526                 */
527         }
528
529         /* Get fourcc from picture format */
530         sc->fourcc = _mmcamcorder_get_fourcc(sc->info_image->preview_format, codectype, hcamcorder->use_zero_copy_format);
531
532         /* Get videosrc element and its name from configure */
533         _mmcamcorder_conf_get_element(handle, hcamcorder->conf_main,
534                 CONFIGURE_CATEGORY_MAIN_VIDEO_INPUT,
535                 "VideosrcElement",
536                 &VideosrcElement);
537         _mmcamcorder_conf_get_value_element_name(VideosrcElement, &videosrc_name);
538
539         /**
540          * Create child element
541          */
542         _MMCAMCORDER_ELEMENT_MAKE(sc, sc->element, _MMCAMCORDER_VIDEOSRC_SRC, videosrc_name, "videosrc_src", element_list, err);
543
544         _MMCAMCORDER_ELEMENT_MAKE(sc, sc->element, _MMCAMCORDER_VIDEOSRC_FILT, "capsfilter", "videosrc_filter", element_list, err);
545
546         /**
547          * This is for "tizencamerasrc" element only.
548          * The camera HAL library will be loaded when "hal-name" property is set.
549          * The default HAL library (libtizen-camera.so) will be loaded if 'hal-name' property is set to NULL.
550          */
551         MMCAMCORDER_G_OBJECT_SET_POINTER(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "hal-name", hcamcorder->network_hal_name);
552
553         /* set camera properties */
554         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "high-speed-fps", 0);
555         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "capture-width", capture_width);
556         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "capture-height", capture_height);
557         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "capture-jpg-quality", capture_jpg_quality);
558         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "hdr-capture", sc->info_image->hdr_capture_mode);
559         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "extra-preview", extra_preview_enable);
560
561         _MMCAMCORDER_ELEMENT_MAKE(sc, sc->element, _MMCAMCORDER_VIDEOSRC_QUE, "queue", "videosrc_queue", element_list, err);
562
563         /* set camera flip */
564         _mmcamcorder_set_videosrc_flip(handle, camera_flip);
565
566         /* set video stabilization mode */
567         _mmcamcorder_set_videosrc_stabilization(handle, video_stabilization);
568
569         /* set anti handshake mode */
570         _mmcamcorder_set_videosrc_anti_shake(handle, anti_shake);
571
572         if (sc->is_modified_rate)
573                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "high-speed-fps", fps);
574
575         /* Set basic information of videosrc element */
576         _mmcamcorder_conf_set_value_element_property(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, VideosrcElement);
577
578         /* Set video device index */
579         if (hcamcorder->is_network) {
580                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "camera-id", hcamcorder->device_type);
581         } else {
582 #ifdef _MMCAMCORDER_CAMERA_CONF_MGR_SUPPORT
583                 MMCAMCORDER_G_OBJECT_SET_POINTER(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "device-name", hcamcorder->conf_device_info.node);
584 #else
585                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "camera-id", input_index->default_value);
586 #endif
587         }
588
589         /* set user buffer fd to videosrc element */
590         if (hcamcorder->support_user_buffer) {
591                 control = GST_CAMERA_CONTROL(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst);
592                 if (!gst_camera_control_set_user_buffer_fd(control, fds, fd_number)) {
593                         MMCAM_LOG_ERROR("set user buffer fd failed");
594                         goto pipeline_creation_error;
595                 }
596         }
597
598         if (_mmcamcorder_is_encoded_preview_pixel_format(sc->info_image->preview_format)) {
599                 /* get recreate_decoder flag */
600                 _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_main,
601                         CONFIGURE_CATEGORY_MAIN_VIDEO_OUTPUT,
602                         "RecreateDecoder",
603                         &hcamcorder->recreate_decoder);
604         }
605
606         if (_mmcamcorder_is_encoded_preview_pixel_format(sc->info_image->preview_format)) {
607                 int preview_bitrate = 0;
608                 int gop_interval = 0;
609                 mm_camcorder_get_attributes(handle, NULL,
610                         MMCAM_ENCODED_PREVIEW_BITRATE, &preview_bitrate,
611                         MMCAM_ENCODED_PREVIEW_GOP_INTERVAL, &gop_interval,
612                         NULL);
613
614                 /* set encoded preview bitrate */
615                 if (!_mmcamcorder_set_encoded_preview_bitrate(handle, preview_bitrate))
616                         MMCAM_LOG_WARNING("_mmcamcorder_set_encoded_preview_bitrate failed");
617
618                 /* set encoded preview iframe interval */
619                 if (sc->info_image->preview_format == MM_PIXEL_FORMAT_ENCODED_H264) {
620                         if (!_mmcamcorder_set_encoded_preview_gop_interval(handle, gop_interval))
621                                 MMCAM_LOG_WARNING("_mmcamcorder_set_encoded_preview_gop_interval failed");
622                 }
623         }
624
625         if (display_surface_type != MM_DISPLAY_SURFACE_NULL &&
626                 _mmcamcorder_is_encoded_preview_pixel_format(sc->info_image->preview_format)) {
627                 switch (sc->info_image->preview_format) {
628                 case MM_PIXEL_FORMAT_ENCODED_H264:
629                         decoder_conf_name = "VideodecoderElementH264";
630                         break;
631                 case MM_PIXEL_FORMAT_ENCODED_MJPEG:
632                         decoder_conf_name = "VideodecoderElementMJPEG";
633                         break;
634                 case MM_PIXEL_FORMAT_ENCODED_VP8:
635                         decoder_conf_name = "VideodecoderElementVP8";
636                         break;
637                 case MM_PIXEL_FORMAT_ENCODED_VP9:
638                         decoder_conf_name = "VideodecoderElementVP9";
639                         break;
640                 default:
641                         MMCAM_LOG_ERROR("invalid format[%d]", sc->info_image->preview_format);
642                         goto pipeline_creation_error;
643                 }
644
645                 /* get video decoder element and name */
646                 _mmcamcorder_conf_get_element(handle, hcamcorder->conf_main,
647                         CONFIGURE_CATEGORY_MAIN_VIDEO_OUTPUT,
648                         decoder_conf_name,
649                         &sc->VideodecoderElement);
650
651                 _mmcamcorder_conf_get_value_element_name(sc->VideodecoderElement,
652                         &videodecoder_name);
653
654                 if (!videodecoder_name) {
655                         MMCAM_LOG_ERROR("failed to get video decoder[%d,%s]", sc->info_image->preview_format, decoder_conf_name);
656                         goto pipeline_creation_error;
657                 }
658
659 #ifdef _MMCAMCORDER_RM_SUPPORT
660                 MMCAM_LOG_INFO("decoder name from config[%s]", videodecoder_name);
661                 if (sc->info_image->preview_format == MM_PIXEL_FORMAT_ENCODED_H264) {
662                         if (hcamcorder->request_resources.category_id[0] == RM_CATEGORY_VIDEO_DECODER_SUB)
663                                 decoder_index = 1;
664                         snprintf(videodecoder_name_final, sizeof(videodecoder_name_final), "%s%d", videodecoder_name, decoder_index);
665                 } else if (sc->info_image->preview_format == MM_PIXEL_FORMAT_ENCODED_MJPEG) {
666                         if (hcamcorder->request_resources.category_id[0] == RM_CATEGORY_VIDEO_DECODER)
667                                 snprintf(videodecoder_name_final, sizeof(videodecoder_name_final), "%s", "omx_uhd_mjpegdec");
668                         else
669                                 snprintf(videodecoder_name_final, sizeof(videodecoder_name_final), "%s", "omx_mjpegdec");
670                 } else {
671 #endif /* _MMCAMCORDER_RM_SUPPORT */
672                         snprintf(videodecoder_name_final, sizeof(videodecoder_name_final), "%s", videodecoder_name);
673 #ifdef _MMCAMCORDER_RM_SUPPORT
674                 }
675 #endif /* _MMCAMCORDER_RM_SUPPORT */
676
677                 _MMCAMCORDER_ELEMENT_MAKE(sc, sc->element, _MMCAMCORDER_VIDEOSRC_DECODE, videodecoder_name_final, "videosrc_decode", element_list, err);
678
679                 _mmcamcorder_conf_set_value_element_property(sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst, sc->VideodecoderElement);
680         }
681
682         MMCAM_LOG_INFO("Current mode[%d]", hcamcorder->type);
683
684         /* Get videosink name */
685         _mmcamcorder_conf_get_value_element_name(sc->VideosinkElement, &videosink_name);
686
687         if (!videosink_name) {
688                 MMCAM_LOG_ERROR("failed to get videosink name");
689                 goto pipeline_creation_error;
690         }
691
692         _MMCAMCORDER_ELEMENT_MAKE(sc, sc->element, _MMCAMCORDER_VIDEOSINK_QUE, "queue", "videosink_queue", element_list, err);
693
694         MMCAM_LOG_INFO("videosink_name: %s", videosink_name);
695
696         if (display_surface_type == MM_DISPLAY_SURFACE_REMOTE) {
697                 _MMCAMCORDER_ELEMENT_MAKE(sc, sc->element, _MMCAMCORDER_VIDEOSINK_SINK, videosink_name, "ipc_sink", element_list, err);
698
699                 _mmcamcorder_conf_set_value_element_property(sc->element[_MMCAMCORDER_VIDEOSINK_SINK].gst, sc->VideosinkElement);
700
701                 err = mm_camcorder_get_attributes(handle, &err_name,
702                         MMCAM_DISPLAY_SOCKET_PATH, &socket_path, &socket_path_len,
703                         NULL);
704                 if (err != MM_ERROR_NONE) {
705                         MMCAM_LOG_WARNING("Get socket path failed 0x%x", err);
706                         SAFE_FREE(err_name);
707                         goto pipeline_creation_error;
708                 }
709
710                 g_object_set(G_OBJECT(sc->element[_MMCAMCORDER_VIDEOSINK_SINK].gst), "socket-path", socket_path, NULL);
711         } else {
712                 if (hcamcorder->use_videoconvert && (!strcmp(videosink_name, "tizenwlsink") || !strcmp(videosink_name, "directvideosink"))) {
713                         /* get video convert name */
714                         _mmcamcorder_conf_get_value_element_name(sc->VideoconvertElement, &videoconvert_name);
715
716                         if (videoconvert_name) {
717                                 MMCAM_LOG_INFO("videoconvert element name : %s", videoconvert_name);
718                                 _MMCAMCORDER_ELEMENT_MAKE(sc, sc->element, _MMCAMCORDER_VIDEOSINK_CLS, videoconvert_name, "videosink_cls", element_list, err);
719                         } else
720                                 MMCAM_LOG_ERROR("failed to get videoconvert element name");
721                 }
722
723                 /* check sink element in attribute */
724                 mm_camcorder_get_attributes(handle, NULL,
725                         MMCAM_DISPLAY_REUSE_ELEMENT, &sink_element, &sink_element_size,
726                         NULL);
727
728                 if (sink_element) {
729                         int attr_index = 0;
730                         MMHandleType attrs = MMF_CAMCORDER_ATTRS(handle);
731
732                         MMCAM_LOG_INFO("reuse sink element %p in attribute", sink_element);
733
734                         _MMCAMCORDER_ELEMENT_ADD(sc, sc->element, _MMCAMCORDER_VIDEOSINK_SINK, sink_element, element_list, err);
735
736                         /* reset attribute */
737                         if (attrs) {
738                                 mm_attrs_get_index((MMHandleType)attrs, MMCAM_DISPLAY_REUSE_ELEMENT, &attr_index);
739                                 mm_attrs_set_data(attrs, attr_index, NULL, 0);
740                                 mm_attrs_commit(attrs, attr_index);
741                         } else {
742                                 MMCAM_LOG_WARNING("attribute is NULL");
743                                 err = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
744                                 goto pipeline_creation_error;
745                         }
746                 } else {
747                         _MMCAMCORDER_ELEMENT_MAKE(sc, sc->element, _MMCAMCORDER_VIDEOSINK_SINK, videosink_name, "videosink_sink", element_list, err);
748
749                         _mmcamcorder_conf_set_value_element_property(sc->element[_MMCAMCORDER_VIDEOSINK_SINK].gst, sc->VideosinkElement);
750                 }
751
752                 if (_mmcamcorder_videosink_window_set(handle, sc->VideosinkElement) != MM_ERROR_NONE) {
753                         MMCAM_LOG_ERROR("_mmcamcorder_videosink_window_set error");
754                         err = MM_ERROR_CAMCORDER_INVALID_ARGUMENT;
755                         goto pipeline_creation_error;
756                 }
757         }
758
759         /* Set caps by rotation */
760         _mmcamcorder_set_videosrc_rotation(handle, camera_rotate);
761
762         /* add elements to main pipeline */
763         if (!_mmcamcorder_add_elements_to_bin(GST_BIN(sc->element[_MMCAMCORDER_MAIN_PIPE].gst), element_list)) {
764                 MMCAM_LOG_ERROR("element_list add error.");
765                 err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
766                 goto pipeline_creation_error;
767         }
768
769         /* link elements */
770         if (!_mmcamcorder_link_elements(element_list)) {
771                 MMCAM_LOG_ERROR("element link error.");
772                 err = MM_ERROR_CAMCORDER_GST_LINK;
773                 goto pipeline_creation_error;
774         }
775
776         if (element_list) {
777                 g_list_free(element_list);
778                 element_list = NULL;
779         }
780
781         return MM_ERROR_NONE;
782
783 pipeline_creation_error:
784         _MMCAMCORDER_ELEMENT_REMOVE(sc->element, _MMCAMCORDER_VIDEOSRC_SRC);
785         _MMCAMCORDER_ELEMENT_REMOVE(sc->element, _MMCAMCORDER_VIDEOSRC_FILT);
786         _MMCAMCORDER_ELEMENT_REMOVE(sc->element, _MMCAMCORDER_VIDEOSRC_CLS_QUE);
787         _MMCAMCORDER_ELEMENT_REMOVE(sc->element, _MMCAMCORDER_VIDEOSRC_CLS);
788         _MMCAMCORDER_ELEMENT_REMOVE(sc->element, _MMCAMCORDER_VIDEOSRC_CLS_FILT);
789         _MMCAMCORDER_ELEMENT_REMOVE(sc->element, _MMCAMCORDER_VIDEOSRC_QUE);
790         _MMCAMCORDER_ELEMENT_REMOVE(sc->element, _MMCAMCORDER_VIDEOSRC_DECODE);
791         _MMCAMCORDER_ELEMENT_REMOVE(sc->element, _MMCAMCORDER_VIDEOSINK_QUE);
792         _MMCAMCORDER_ELEMENT_REMOVE(sc->element, _MMCAMCORDER_VIDEOSINK_SINK);
793
794         if (element_list) {
795                 g_list_free(element_list);
796                 element_list = NULL;
797         }
798
799         return err;
800 }
801
802
803 int _mmcamcorder_create_audiosrc_bin(MMHandleType handle)
804 {
805         int err = MM_ERROR_NONE;
806         int val = 0;
807         int rate = 0;
808         int format = 0;
809         int channel = 0;
810         int a_enc = MM_AUDIO_CODEC_AMR;
811         int a_dev = MM_AUDIO_DEVICE_MIC;
812         double volume = 0.0;
813         char *err_name = NULL;
814         const char *audiosrc_name = NULL;
815         char *cat_name = NULL;
816         char *stream_type = NULL;
817         int stream_type_len = 0;
818         int stream_index = 0;
819         int buffer_interval = 0;
820         int blocksize = 0;
821         int replay_gain_enable = FALSE;
822         double replay_gain_ref_level = 0.0;
823
824         GstCaps *caps = NULL;
825         GstPad *pad = NULL;
826         GList *element_list  = NULL;
827
828         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
829         _MMCamcorderSubContext *sc = NULL;
830         _MMCamcorderGstElement *last_element = NULL;
831         type_element *AudiosrcElement = NULL;
832
833         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
834
835         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
836         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
837         mmf_return_val_if_fail(sc->element, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
838
839         MMCAM_LOG_INFO("");
840
841         err = _mmcamcorder_check_audiocodec_fileformat_compatibility(handle);
842         if (err != MM_ERROR_NONE)
843                 return err;
844
845         err = mm_camcorder_get_attributes(handle, &err_name,
846                 MMCAM_AUDIO_DEVICE, &a_dev,
847                 MMCAM_AUDIO_ENCODER, &a_enc,
848                 MMCAM_AUDIO_ENCODER_BITRATE, &val,
849                 MMCAM_AUDIO_SAMPLERATE, &rate,
850                 MMCAM_AUDIO_FORMAT, &format,
851                 MMCAM_AUDIO_CHANNEL, &channel,
852                 MMCAM_AUDIO_VOLUME, &volume,
853                 MMCAM_AUDIO_REPLAY_GAIN_ENABLE, &replay_gain_enable,
854                 MMCAM_AUDIO_REPLAY_GAIN_REFERENCE_LEVEL, &replay_gain_ref_level,
855                 MMCAM_SOUND_STREAM_TYPE, &stream_type, &stream_type_len,
856                 MMCAM_SOUND_STREAM_INDEX, &stream_index,
857                 NULL);
858         if (err != MM_ERROR_NONE) {
859                 MMCAM_LOG_WARNING("Get attrs fail. (%s:%x)", err_name, err);
860                 SAFE_FREE(err_name);
861                 return err;
862         }
863
864         /* Check existence */
865         if (sc->encode_element[_MMCAMCORDER_AUDIOSRC_BIN].gst) {
866                 if (((GObject *)sc->encode_element[_MMCAMCORDER_AUDIOSRC_BIN].gst)->ref_count > 0)
867                         gst_object_unref(sc->encode_element[_MMCAMCORDER_AUDIOSRC_BIN].gst);
868
869                 MMCAM_LOG_INFO("_MMCAMCORDER_AUDIOSRC_BIN is Already existed. Unref once...");
870         }
871
872         /* Create bin element */
873         _MMCAMCORDER_BIN_MAKE(sc, sc->encode_element, _MMCAMCORDER_AUDIOSRC_BIN, "audiosource_bin", err);
874
875         if (a_dev == MM_AUDIO_DEVICE_MODEM) {
876                 cat_name = strdup("AudiomodemsrcElement");
877         } else {
878                 /* MM_AUDIO_DEVICE_MIC or others */
879                 cat_name = strdup("AudiosrcElement");
880         }
881
882         if (cat_name == NULL) {
883                 MMCAM_LOG_ERROR("strdup failed.");
884                 err = MM_ERROR_CAMCORDER_LOW_MEMORY;
885                 goto pipeline_creation_error;
886         }
887
888         _mmcamcorder_conf_get_element(handle, hcamcorder->conf_main,
889                 CONFIGURE_CATEGORY_MAIN_AUDIO_INPUT,
890                 cat_name,
891                 &AudiosrcElement);
892         _mmcamcorder_conf_get_value_element_name(AudiosrcElement, &audiosrc_name);
893
894         free(cat_name);
895         cat_name = NULL;
896
897         MMCAM_LOG_INFO("Audio src name : %s", audiosrc_name);
898
899         _MMCAMCORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMCAMCORDER_AUDIOSRC_SRC, audiosrc_name, "audiosrc_src", element_list, err);
900
901         /* set sound stream info */
902         _mmcamcorder_set_sound_stream_info(sc->encode_element[_MMCAMCORDER_AUDIOSRC_SRC].gst, stream_type, stream_index);
903
904         /* set audiosrc properties in ini configuration */
905         _mmcamcorder_conf_set_value_element_property(sc->encode_element[_MMCAMCORDER_AUDIOSRC_SRC].gst, AudiosrcElement);
906
907         /* set block size */
908         _mmcamcorder_conf_get_value_int((MMHandleType)hcamcorder, hcamcorder->conf_main,
909                 CONFIGURE_CATEGORY_MAIN_AUDIO_INPUT,
910                 "AudioBufferInterval",
911                 &buffer_interval);
912
913         if (_mmcamcorder_get_audiosrc_blocksize(rate, format, channel, buffer_interval, &blocksize)) {
914                 MMCAM_LOG_INFO("set audiosrc block size %d", blocksize);
915                 MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_AUDIOSRC_SRC].gst, "blocksize", blocksize);
916         }
917
918         _MMCAMCORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMCAMCORDER_AUDIOSRC_FILT, "capsfilter", "audiosrc_capsfilter", element_list, err);
919
920         _MMCAMCORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMCAMCORDER_AUDIOSRC_QUE, "queue", "audiosrc_queue", element_list, err);
921         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_AUDIOSRC_QUE].gst, "max-size-buffers", 0);
922         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_AUDIOSRC_QUE].gst, "max-size-bytes", 0);
923         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_AUDIOSRC_QUE].gst, "max-size-time", 0);
924
925         /* Set basic information */
926         if (a_enc != MM_AUDIO_CODEC_VORBIS) {
927                 int depth = 0;
928                 const gchar* format_name = NULL;
929
930                 _MMCAMCORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMCAMCORDER_AUDIOSRC_VOL, "volume", "audiosrc_volume", element_list, err);
931
932                 if (volume == 0.0) {
933                         /* Because data probe of audio src do the same job, it doesn't need to set "mute" here. Already null raw data. */
934                         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_AUDIOSRC_VOL].gst, "volume", 1.0);
935                 } else {
936                         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_AUDIOSRC_VOL].gst, "mute", FALSE);
937                         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_AUDIOSRC_VOL].gst, "volume", volume);
938                 }
939
940                 if (format == MM_CAMCORDER_AUDIO_FORMAT_PCM_S16_LE) {
941                         depth = 16;
942                         format_name = "S16LE";
943                 } else { /* MM_CAMCORDER_AUDIO_FORMAT_PCM_U8 */
944                         depth = 8;
945                         format_name = "U8";
946                 }
947
948                 caps = gst_caps_new_simple("audio/x-raw",
949                         "rate", G_TYPE_INT, rate,
950                         "channels", G_TYPE_INT, channel,
951                         "format", G_TYPE_STRING, format_name,
952                         NULL);
953                 MMCAM_LOG_INFO("caps [x-raw, rate:%d, channel:%d, depth:%d], volume %lf",
954                         rate, channel, depth, volume);
955         } else {
956                 /* what are the audio encoder which should get audio/x-raw-float? */
957                 caps = gst_caps_new_simple("audio/x-raw",
958                         "rate", G_TYPE_INT, rate,
959                         "channels", G_TYPE_INT, channel,
960                         "format", G_TYPE_STRING, GST_AUDIO_NE(F32),
961                         NULL);
962                 MMCAM_LOG_INFO("caps [x-raw (F32), rate:%d, channel:%d, endianness:%d, width:32]",
963                         rate, channel, BYTE_ORDER);
964         }
965
966         /* Replay Gain */
967         MMCAM_LOG_INFO("Replay gain - enable : %d, reference level : %lf",
968                 replay_gain_enable, replay_gain_ref_level);
969
970         if (replay_gain_enable) {
971                 _MMCAMCORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMCAMCORDER_AUDIOSRC_RGA, "rganalysis", "audiosrc_rga", element_list, err);
972                 MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_AUDIOSRC_RGA].gst, "reference-level", replay_gain_ref_level);
973                 /* If num-tracks is not set, album gain and peak event is not come. */
974                 MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_AUDIOSRC_RGA].gst, "num-tracks", 1);
975         }
976
977         if (caps) {
978                 MMCAMCORDER_G_OBJECT_SET_POINTER((sc->encode_element[_MMCAMCORDER_AUDIOSRC_FILT].gst), "caps", caps);
979                 gst_caps_unref(caps);
980                 caps = NULL;
981         } else {
982                 MMCAM_LOG_ERROR("create caps error");
983                 err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
984                 goto pipeline_creation_error;
985         }
986
987         if (!_mmcamcorder_add_elements_to_bin(GST_BIN(sc->encode_element[_MMCAMCORDER_AUDIOSRC_BIN].gst), element_list)) {
988                 MMCAM_LOG_ERROR("element add error.");
989                 err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
990                 goto pipeline_creation_error;
991         }
992
993         if (!_mmcamcorder_link_elements(element_list)) {
994                 MMCAM_LOG_ERROR("element link error.");
995                 err = MM_ERROR_CAMCORDER_GST_LINK;
996                 goto pipeline_creation_error;
997         }
998
999         last_element = (_MMCamcorderGstElement*)(g_list_last(element_list)->data);
1000         pad = gst_element_get_static_pad(last_element->gst, "src");
1001         if (!gst_element_add_pad(sc->encode_element[_MMCAMCORDER_AUDIOSRC_BIN].gst, gst_ghost_pad_new("src", pad))) {
1002                 gst_object_unref(pad);
1003                 pad = NULL;
1004                 MMCAM_LOG_ERROR("failed to create ghost pad on _MMCAMCORDER_AUDIOSRC_BIN.");
1005                 err = MM_ERROR_CAMCORDER_GST_LINK;
1006                 goto pipeline_creation_error;
1007         }
1008
1009         gst_object_unref(pad);
1010         pad = NULL;
1011
1012         if (element_list) {
1013                 g_list_free(element_list);
1014                 element_list = NULL;
1015         }
1016
1017         return MM_ERROR_NONE;
1018
1019 pipeline_creation_error:
1020         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_AUDIOSRC_SRC);
1021         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_AUDIOSRC_FILT);
1022         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_AUDIOSRC_QUE);
1023         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_AUDIOSRC_VOL);
1024         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_AUDIOSRC_BIN);
1025
1026         if (element_list) {
1027                 g_list_free(element_list);
1028                 element_list = NULL;
1029         }
1030
1031         return err;
1032 }
1033
1034
1035 void _mmcamcorder_set_encoder_bitrate(MMCamcorderEncoderType type, int codec, int bitrate, GstElement *element)
1036 {
1037         int set_value = 0;
1038
1039         if (!element) {
1040                 MMCAM_LOG_WARNING("NULL element, will be applied later - type %d, bitrate %d", type, bitrate);
1041                 return;
1042         }
1043
1044         if (bitrate <= 0) {
1045                 MMCAM_LOG_WARNING("[type %d, codec %d] too small bitrate[%d], use default",
1046                         type, codec, bitrate);
1047                 return;
1048         }
1049
1050         if (type == MM_CAMCORDER_ENCODER_TYPE_AUDIO) {
1051                 /* audio encoder bitrate setting */
1052                 switch (codec) {
1053                 case MM_AUDIO_CODEC_AMR:
1054                         set_value = __mmcamcorder_get_amrnb_bitrate_mode(bitrate);
1055                         MMCAM_LOG_INFO("Set AMR encoder mode [%d]", set_value);
1056                         MMCAMCORDER_G_OBJECT_SET(element, "band-mode", set_value);
1057                         break;
1058                 case MM_AUDIO_CODEC_MP3:
1059                         set_value = bitrate / 1000;
1060                         MMCAM_LOG_INFO("Set MP3 encoder bitrate [%d] kbps", set_value);
1061                         MMCAMCORDER_G_OBJECT_SET(element, "bitrate", set_value);
1062                         break;
1063                 case MM_AUDIO_CODEC_AAC:
1064                         MMCAM_LOG_INFO("Set AAC encoder bitrate [%d] bps", bitrate);
1065                         MMCAMCORDER_G_OBJECT_SET(element, "bitrate", bitrate);
1066                         break;
1067                 default:
1068                         MMCAM_LOG_WARNING("Not AMR, MP3 and AAC codec, need to add code for audio bitrate");
1069                         break;
1070                 }
1071         } else {
1072                 /* video encoder bitrate setting */
1073                 MMCAM_LOG_INFO("Set video encoder bitrate %d", bitrate);
1074                 MMCAMCORDER_G_OBJECT_SET(element, "bitrate", bitrate);
1075         }
1076
1077         return;
1078 }
1079
1080
1081 int _mmcamcorder_create_encodesink_bin(MMHandleType handle, MMCamcorderEncodebinProfile profile)
1082 {
1083         int err = MM_ERROR_NONE;
1084         int channel = 0;
1085         int audio_enc = 0;
1086         int v_bitrate = 0;
1087         int a_bitrate = 0;
1088         int encodebin_profile = 0;
1089         int auto_audio_convert = 0;
1090         int auto_audio_resample = 0;
1091         int auto_color_space = 0;
1092         int cap_format = MM_PIXEL_FORMAT_INVALID;
1093         const char *gst_element_venc_name = NULL;
1094         const char *gst_element_aenc_name = NULL;
1095         const char *gst_element_ienc_name = NULL;
1096         const char *gst_element_mux_name = NULL;
1097         const char *gst_element_rsink_name = NULL;
1098         const char *gst_element_vscale_name = NULL;
1099         const char *str_profile = NULL;
1100         const char *str_aac = NULL;
1101         const char *str_aar = NULL;
1102         const char *str_acs = NULL;
1103         char *err_name = NULL;
1104         const char *videoconvert_name = NULL;
1105         GstCaps *audio_caps = NULL;
1106         GstCaps *video_caps = NULL;
1107         GstCaps *videoscale_caps = NULL;
1108         GstPad *pad = NULL;
1109         GList *element_list = NULL;
1110
1111         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1112         _MMCamcorderSubContext *sc = NULL;
1113         type_element *VideoencElement = NULL;
1114         type_element *AudioencElement = NULL;
1115         type_element *ImageencElement = NULL;
1116         type_element *MuxElement = NULL;
1117         type_element *RecordsinkElement = NULL;
1118         type_element *VideoscaleElement = NULL;
1119
1120         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1121
1122         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
1123         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1124         mmf_return_val_if_fail(sc->element, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1125
1126         MMCAM_LOG_INFO("start - profile : %d", profile);
1127
1128         /* Check existence */
1129         if (sc->encode_element[_MMCAMCORDER_ENCSINK_BIN].gst) {
1130                 if (((GObject *)sc->encode_element[_MMCAMCORDER_ENCSINK_BIN].gst)->ref_count > 0)
1131                         gst_object_unref(sc->encode_element[_MMCAMCORDER_ENCSINK_BIN].gst);
1132
1133                 MMCAM_LOG_INFO("_MMCAMCORDER_ENCSINK_BIN is Already existed.");
1134         }
1135
1136         /* Create bin element */
1137         _MMCAMCORDER_BIN_MAKE(sc, sc->encode_element, _MMCAMCORDER_ENCSINK_BIN, "encodesink_bin", err);
1138
1139         /* Create child element */
1140         if (profile != MM_CAMCORDER_ENCBIN_PROFILE_AUDIO) {
1141                 GstCaps *caps_from_pad = NULL;
1142                 char *caps_str = NULL;
1143
1144                 /* create appsrc and capsfilter */
1145                 _MMCAMCORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMCAMCORDER_ENCSINK_SRC, "appsrc", "encodesink_src", element_list, err);
1146
1147                 _MMCAMCORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMCAMCORDER_ENCSINK_FILT, "capsfilter", "encodesink_filter", element_list, err);
1148
1149                 /* set appsrc as live source */
1150                 MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_SRC].gst, "is-live", TRUE);
1151                 MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_SRC].gst, "format", 3); /* GST_FORMAT_TIME */
1152                 MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_SRC].gst, "max-bytes", 0); /* unlimited */
1153
1154                 /* set capsfilter */
1155                 if (profile == MM_CAMCORDER_ENCBIN_PROFILE_VIDEO) {
1156                         if (_mmcamcorder_is_encoded_preview_pixel_format(sc->info_image->preview_format)) {
1157                                 MMCAM_LOG_INFO("get pad from videosrc_filter");
1158                                 pad = gst_element_get_static_pad(sc->element[_MMCAMCORDER_VIDEOSRC_FILT].gst, "src");
1159                         } else {
1160                                 MMCAM_LOG_INFO("get pad from videosrc_que");
1161                                 pad = gst_element_get_static_pad(sc->element[_MMCAMCORDER_VIDEOSRC_QUE].gst, "src");
1162                         }
1163                         if (!pad) {
1164                                 MMCAM_LOG_ERROR("get videosrc_que src pad failed");
1165                                 err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1166                                 goto pipeline_creation_error;
1167                         }
1168
1169                         caps_from_pad = gst_pad_get_allowed_caps(pad);
1170                         video_caps = gst_caps_copy(caps_from_pad);
1171                         gst_caps_unref(caps_from_pad);
1172                         caps_from_pad = NULL;
1173                         gst_object_unref(pad);
1174                         pad = NULL;
1175
1176                         /* fixate caps */
1177                         video_caps = gst_caps_fixate(video_caps);
1178                 } else {
1179                         /* Image */
1180                         MMCAMCORDER_G_OBJECT_GET(sc->element[_MMCAMCORDER_VIDEOSRC_FILT].gst, "caps", &video_caps);
1181                 }
1182
1183                 if (!video_caps) {
1184                         MMCAM_LOG_ERROR("create recording pipeline caps failed");
1185                         err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1186                         goto pipeline_creation_error;
1187                 }
1188
1189                 if (profile == MM_CAMCORDER_ENCBIN_PROFILE_VIDEO) {
1190                         if (sc->info_video->use_videoscale) {
1191                                 gst_caps_set_simple(video_caps,
1192                                         "width", G_TYPE_INT, sc->info_video->preview_width,
1193                                         "height", G_TYPE_INT, sc->info_video->preview_height,
1194                                         NULL);
1195                                 videoscale_caps = gst_caps_copy(video_caps);
1196                         } else {
1197                                 gst_caps_set_simple(video_caps,
1198                                         "width", G_TYPE_INT, sc->info_video->video_width,
1199                                         "height", G_TYPE_INT, sc->info_video->video_height,
1200                                         NULL);
1201                         }
1202                 }
1203
1204                 caps_str = gst_caps_to_string(video_caps);
1205
1206                 MMCAM_LOG_INFO("encodebin caps [%s]", caps_str);
1207
1208                 g_free(caps_str);
1209                 caps_str = NULL;
1210
1211                 MMCAMCORDER_G_OBJECT_SET_POINTER(sc->encode_element[_MMCAMCORDER_ENCSINK_FILT].gst, "caps", video_caps);
1212
1213                 gst_caps_unref(video_caps);
1214                 video_caps = NULL;
1215
1216                 /* video scale */
1217                 if (profile == MM_CAMCORDER_ENCBIN_PROFILE_VIDEO &&
1218                         sc->info_video->use_videoscale) {
1219                         if (!videoscale_caps) {
1220                                 MMCAM_LOG_ERROR("no videoscale caps");
1221                                 err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1222                                 goto pipeline_creation_error;
1223                         }
1224
1225                         gst_caps_set_simple(videoscale_caps,
1226                                 "width", G_TYPE_INT, sc->info_video->video_width,
1227                                 "height", G_TYPE_INT, sc->info_video->video_height,
1228                                 NULL);
1229
1230                         _mmcamcorder_conf_get_element(handle, hcamcorder->conf_main,
1231                                 CONFIGURE_CATEGORY_MAIN_RECORD,
1232                                 "VideoscaleElement",
1233                                 &VideoscaleElement);
1234
1235                         if (!_mmcamcorder_conf_get_value_element_name(VideoscaleElement, &gst_element_vscale_name)) {
1236                                 MMCAM_LOG_ERROR("failed to get videoscale element name");
1237                                 err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1238                                 goto pipeline_creation_error;
1239                         }
1240
1241                         caps_str = gst_caps_to_string(videoscale_caps);
1242
1243                         MMCAM_LOG_INFO("encodebin videocale [%s][%s]", gst_element_vscale_name, caps_str);
1244
1245                         g_free(caps_str);
1246                         caps_str = NULL;
1247
1248                         _MMCAMCORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMCAMCORDER_ENCSINK_VSCALE, gst_element_vscale_name, "encodesink_vscale", element_list, err);
1249                         _MMCAMCORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMCAMCORDER_ENCSINK_VSCALE_FILT, "capsfilter", "encodesink_vscale_filter", element_list, err);
1250
1251                         MMCAMCORDER_G_OBJECT_SET_POINTER((sc->encode_element[_MMCAMCORDER_ENCSINK_VSCALE_FILT].gst), "caps", videoscale_caps);
1252
1253                         gst_caps_unref(videoscale_caps);
1254                         videoscale_caps = NULL;
1255                 }
1256
1257                 /* release element_list, they will be placed out of encodesink bin */
1258                 if (element_list) {
1259                         g_list_free(element_list);
1260                         element_list = NULL;
1261                 }
1262
1263                 /* connect signal for ready to push buffer */
1264                 MMCAMCORDER_SIGNAL_CONNECT(sc->encode_element[_MMCAMCORDER_ENCSINK_SRC].gst,
1265                         _MMCAMCORDER_HANDLER_VIDEOREC,
1266                         "need-data",
1267                         _mmcamcorder_ready_to_encode_callback,
1268                         hcamcorder);
1269         }
1270
1271         _MMCAMCORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMCAMCORDER_ENCSINK_ENCBIN,
1272                 "tizenencodebin", "encodesink_encbin", element_list, err);
1273
1274         /* check element availability */
1275         if (profile == MM_CAMCORDER_ENCBIN_PROFILE_IMAGE) {
1276                 err = mm_camcorder_get_attributes(handle, &err_name,
1277                         MMCAM_CAPTURE_FORMAT, &cap_format,
1278                         NULL);
1279         } else {
1280                 err = mm_camcorder_get_attributes(handle, &err_name,
1281                         MMCAM_AUDIO_ENCODER, &audio_enc,
1282                         MMCAM_AUDIO_CHANNEL, &channel,
1283                         MMCAM_VIDEO_ENCODER_BITRATE, &v_bitrate,
1284                         MMCAM_AUDIO_ENCODER_BITRATE, &a_bitrate,
1285                         NULL);
1286         }
1287
1288         if (err != MM_ERROR_NONE) {
1289                 if (err_name) {
1290                         MMCAM_LOG_ERROR("failed to get attributes [%s][0x%x]", err_name, err);
1291                         SAFE_FREE(err_name);
1292                 } else {
1293                         MMCAM_LOG_ERROR("failed to get attributes [0x%x]", err);
1294                 }
1295
1296                 goto pipeline_creation_error;
1297         }
1298
1299         MMCAM_LOG_INFO("Profile[%d]", profile);
1300
1301         /* Set information */
1302         if (profile == MM_CAMCORDER_ENCBIN_PROFILE_VIDEO) {
1303                 str_profile = "VideoProfile";
1304                 str_aac = "VideoAutoAudioConvert";
1305                 str_aar = "VideoAutoAudioResample";
1306                 str_acs = "VideoAutoColorSpace";
1307         } else if (profile == MM_CAMCORDER_ENCBIN_PROFILE_AUDIO) {
1308                 str_profile = "AudioProfile";
1309                 str_aac = "AudioAutoAudioConvert";
1310                 str_aar = "AudioAutoAudioResample";
1311                 str_acs = "AudioAutoColorSpace";
1312         } else if (profile == MM_CAMCORDER_ENCBIN_PROFILE_IMAGE) {
1313                 str_profile = "ImageProfile";
1314                 str_aac = "ImageAutoAudioConvert";
1315                 str_aar = "ImageAutoAudioResample";
1316                 str_acs = "ImageAutoColorSpace";
1317         }
1318
1319         _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_main, CONFIGURE_CATEGORY_MAIN_RECORD, str_profile, &encodebin_profile);
1320         _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_main, CONFIGURE_CATEGORY_MAIN_RECORD, str_aac, &auto_audio_convert);
1321         _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_main, CONFIGURE_CATEGORY_MAIN_RECORD, str_aar, &auto_audio_resample);
1322         _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_main, CONFIGURE_CATEGORY_MAIN_RECORD, str_acs, &auto_color_space);
1323
1324         MMCAM_LOG_INFO("Profile:%d, AutoAudioConvert:%d, AutoAudioResample:%d, AutoColorSpace:%d",
1325                 encodebin_profile, auto_audio_convert, auto_audio_resample, auto_color_space);
1326
1327         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "profile", encodebin_profile);
1328         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "auto-audio-convert", auto_audio_convert);
1329         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "auto-audio-resample", auto_audio_resample);
1330         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "auto-colorspace", auto_color_space);
1331         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "use-video-toggle", FALSE);
1332
1333         /* get video convert element name */
1334         _mmcamcorder_conf_get_value_element_name(sc->VideoconvertElement, &videoconvert_name);
1335
1336         /* Codec */
1337         if (profile == MM_CAMCORDER_ENCBIN_PROFILE_VIDEO) {
1338                 int use_venc_queue = 0;
1339
1340                 VideoencElement = _mmcamcorder_get_type_element(handle, MM_CAM_VIDEO_ENCODER);
1341
1342                 if (!VideoencElement) {
1343                         MMCAM_LOG_ERROR("Fail to get type element");
1344                         err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1345                         goto pipeline_creation_error;
1346                 }
1347
1348                 if (_mmcamcorder_is_encoded_preview_pixel_format(sc->info_image->preview_format)) {
1349                         /* set dummy element */
1350                         gst_element_venc_name = "identity";
1351                 } else {
1352                         _mmcamcorder_conf_get_value_element_name(VideoencElement, &gst_element_venc_name);
1353                 }
1354
1355                 if (gst_element_venc_name) {
1356                         MMCAM_LOG_INFO("video encoder name [%s]", gst_element_venc_name);
1357
1358                         MMCAMCORDER_G_OBJECT_SET_POINTER(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "venc-name", gst_element_venc_name);
1359                         _MMCAMCORDER_ENCODEBIN_ELMGET(sc, _MMCAMCORDER_ENCSINK_VENC, "video-encode", err);
1360                 } else {
1361                         MMCAM_LOG_ERROR("Fail to get video encoder name");
1362                         err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1363                         goto pipeline_creation_error;
1364                 }
1365
1366                 /* set color space converting element */
1367                 if (auto_color_space) {
1368                         MMCAM_LOG_INFO("set video convert element [%s]", videoconvert_name);
1369
1370                         MMCAMCORDER_G_OBJECT_SET_POINTER(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "vconv-name", videoconvert_name);
1371                         _MMCAMCORDER_ENCODEBIN_ELMGET(sc, _MMCAMCORDER_ENCSINK_VCONV, "video-convert", err);
1372
1373                         /* set colorspace plugin property setting */
1374                         _mmcamcorder_conf_set_value_element_property(sc->encode_element[_MMCAMCORDER_ENCSINK_VCONV].gst, sc->VideoconvertElement);
1375
1376                         /* fourcc type was removed in GST 1.0 */
1377                         if (hcamcorder->use_zero_copy_format) {
1378                                 if (strstr(gst_element_venc_name, "omx")) {
1379                                         video_caps = gst_caps_new_simple("video/x-raw", "format", G_TYPE_STRING, "SN12", NULL);
1380
1381                                         if (video_caps) {
1382                                                 MMCAMCORDER_G_OBJECT_SET_POINTER(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "vcaps", video_caps);
1383                                                 MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_VCONV].gst, "dst-buffer-num", _MMCAMCORDER_CONVERT_OUTPUT_BUFFER_NUM);
1384
1385                                                 gst_caps_unref(video_caps);
1386                                                 video_caps = NULL;
1387                                         } else {
1388                                                 MMCAM_LOG_WARNING("failed to create caps");
1389                                         }
1390                                 } else {
1391                                         MMCAM_LOG_INFO("current video codec is not openmax but [%s]", gst_element_venc_name);
1392                                 }
1393                         }
1394                 }
1395
1396                 _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_main,
1397                         CONFIGURE_CATEGORY_MAIN_RECORD,
1398                         "UseVideoEncoderQueue",
1399                         &use_venc_queue);
1400                 if (use_venc_queue)
1401                         _MMCAMCORDER_ENCODEBIN_ELMGET(sc, _MMCAMCORDER_ENCSINK_VENC_QUE, "use-venc-queue", err);
1402         }
1403
1404         if (sc->audio_disable == FALSE &&
1405             profile != MM_CAMCORDER_ENCBIN_PROFILE_IMAGE) {
1406                 int use_aenc_queue = 0;
1407
1408                 AudioencElement = _mmcamcorder_get_type_element(handle, MM_CAM_AUDIO_ENCODER);
1409                 if (!AudioencElement) {
1410                         MMCAM_LOG_ERROR("Fail to get type element");
1411                         err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1412                         goto pipeline_creation_error;
1413                 }
1414
1415                 _mmcamcorder_conf_get_value_element_name(AudioencElement, &gst_element_aenc_name);
1416
1417                 MMCAMCORDER_G_OBJECT_SET_POINTER(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "aenc-name", gst_element_aenc_name);
1418                 _MMCAMCORDER_ENCODEBIN_ELMGET(sc, _MMCAMCORDER_ENCSINK_AENC, "audio-encode", err);
1419
1420                 if (audio_enc == MM_AUDIO_CODEC_AMR && channel == 2) {
1421                         audio_caps = gst_caps_new_simple("audio/x-raw",
1422                                                                                          "channels", G_TYPE_INT, 1,
1423                                                                                          NULL);
1424                         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "auto-audio-convert", TRUE);
1425                         MMCAMCORDER_G_OBJECT_SET_POINTER(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "acaps", audio_caps);
1426                         gst_caps_unref(audio_caps);
1427                         audio_caps = NULL;
1428                 }
1429
1430                 if (audio_enc == MM_AUDIO_CODEC_OGG) {
1431                         audio_caps = gst_caps_new_empty_simple("audio/x-raw");
1432                         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "auto-audio-convert", TRUE);
1433                         MMCAMCORDER_G_OBJECT_SET_POINTER(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "acaps", audio_caps);
1434                         gst_caps_unref(audio_caps);
1435                         audio_caps = NULL;
1436                         MMCAM_LOG_INFO("***** MM_AUDIO_CODEC_OGG : setting audio/x-raw-int ");
1437                 }
1438
1439                 _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_main,
1440                         CONFIGURE_CATEGORY_MAIN_RECORD,
1441                         "UseAudioEncoderQueue",
1442                         &use_aenc_queue);
1443                 if (use_aenc_queue) {
1444                         _MMCAMCORDER_ENCODEBIN_ELMGET(sc, _MMCAMCORDER_ENCSINK_AENC_QUE, "use-aenc-queue", err);
1445                         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_AENC_QUE].gst, "max-size-time", 0);
1446                         MMCAMCORDER_G_OBJECT_SET(sc->encode_element[_MMCAMCORDER_ENCSINK_AENC_QUE].gst, "max-size-buffers", 0);
1447                 }
1448         }
1449
1450         if (profile == MM_CAMCORDER_ENCBIN_PROFILE_IMAGE) {
1451                 if (cap_format == MM_PIXEL_FORMAT_ENCODED) {
1452                         ImageencElement = _mmcamcorder_get_type_element(handle, MM_CAM_IMAGE_ENCODER);
1453                         if (!ImageencElement) {
1454                                 MMCAM_LOG_ERROR("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(ImageencElement, &gst_element_ienc_name);
1460                 } else {
1461                         /* raw format - set dummy element */
1462                         gst_element_ienc_name = "identity";
1463                 }
1464
1465                 MMCAMCORDER_G_OBJECT_SET_POINTER(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "ienc-name", gst_element_ienc_name);
1466                 _MMCAMCORDER_ENCODEBIN_ELMGET(sc, _MMCAMCORDER_ENCSINK_IENC, "image-encode", err);
1467         }
1468
1469         /* Mux */
1470         if (profile != MM_CAMCORDER_ENCBIN_PROFILE_IMAGE) {
1471                 MuxElement = _mmcamcorder_get_type_element(handle, MM_CAM_FILE_FORMAT);
1472                 if (!MuxElement) {
1473                         MMCAM_LOG_ERROR("Fail to get type element");
1474                         err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1475                         goto pipeline_creation_error;
1476                 }
1477
1478                 _mmcamcorder_conf_get_value_element_name(MuxElement, &gst_element_mux_name);
1479
1480                 MMCAMCORDER_G_OBJECT_SET_POINTER(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "mux-name", gst_element_mux_name);
1481                 _MMCAMCORDER_ENCODEBIN_ELMGET(sc, _MMCAMCORDER_ENCSINK_MUX, "mux", err);
1482
1483                 _mmcamcorder_conf_set_value_element_property(sc->encode_element[_MMCAMCORDER_ENCSINK_MUX].gst, MuxElement);
1484         }
1485
1486         /* Sink */
1487         if (profile != MM_CAMCORDER_ENCBIN_PROFILE_IMAGE) {
1488                 /* for recording */
1489                 _mmcamcorder_conf_get_element(handle, hcamcorder->conf_main,
1490                         CONFIGURE_CATEGORY_MAIN_RECORD,
1491                         "RecordsinkElement",
1492                         &RecordsinkElement);
1493                 _mmcamcorder_conf_get_value_element_name(RecordsinkElement, &gst_element_rsink_name);
1494
1495                 _MMCAMCORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMCAMCORDER_ENCSINK_SINK, gst_element_rsink_name, NULL, element_list, err);
1496
1497                 _mmcamcorder_conf_set_value_element_property(sc->encode_element[_MMCAMCORDER_ENCSINK_SINK].gst, RecordsinkElement);
1498         } else {
1499                 /* for stillshot */
1500                 _MMCAMCORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMCAMCORDER_ENCSINK_SINK, "fakesink", NULL, element_list, err);
1501         }
1502
1503         if (profile == MM_CAMCORDER_ENCBIN_PROFILE_VIDEO) {
1504                 /* property setting in ini */
1505                 _mmcamcorder_conf_set_value_element_property(sc->encode_element[_MMCAMCORDER_ENCSINK_VENC].gst, VideoencElement);
1506
1507                 /* bitrate setting */
1508                 _mmcamcorder_set_encoder_bitrate(MM_CAMCORDER_ENCODER_TYPE_VIDEO, 0,
1509                         v_bitrate, sc->encode_element[_MMCAMCORDER_ENCSINK_VENC].gst);
1510         }
1511
1512         if (sc->audio_disable == FALSE &&
1513             profile != MM_CAMCORDER_ENCBIN_PROFILE_IMAGE) {
1514                 /* property setting in ini */
1515                 _mmcamcorder_conf_set_value_element_property(sc->encode_element[_MMCAMCORDER_ENCSINK_AENC].gst, AudioencElement);
1516
1517                 /* bitrate setting */
1518                 _mmcamcorder_set_encoder_bitrate(MM_CAMCORDER_ENCODER_TYPE_AUDIO, audio_enc,
1519                         a_bitrate, sc->encode_element[_MMCAMCORDER_ENCSINK_AENC].gst);
1520         }
1521
1522         MMCAM_LOG_INFO("Element creation complete");
1523
1524         /* Add element to bin */
1525         if (!_mmcamcorder_add_elements_to_bin(GST_BIN(sc->encode_element[_MMCAMCORDER_ENCSINK_BIN].gst), element_list)) {
1526                 MMCAM_LOG_ERROR("element add error.");
1527                 err = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
1528                 goto pipeline_creation_error;
1529         }
1530
1531         MMCAM_LOG_INFO("Element add complete");
1532
1533         if (profile == MM_CAMCORDER_ENCBIN_PROFILE_VIDEO) {
1534                 pad = gst_element_get_request_pad(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "video");
1535                 if (!gst_element_add_pad(sc->encode_element[_MMCAMCORDER_ENCSINK_BIN].gst, gst_ghost_pad_new("video_sink0", pad))) {
1536                         gst_object_unref(pad);
1537                         pad = NULL;
1538                         MMCAM_LOG_ERROR("failed to create ghost video_sink0 on _MMCAMCORDER_ENCSINK_BIN.");
1539                         err = MM_ERROR_CAMCORDER_GST_LINK;
1540                         goto pipeline_creation_error;
1541                 }
1542                 gst_object_unref(pad);
1543                 pad = NULL;
1544
1545                 if (sc->audio_disable == FALSE) {
1546                         pad = gst_element_get_request_pad(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "audio");
1547                         if (!gst_element_add_pad(sc->encode_element[_MMCAMCORDER_ENCSINK_BIN].gst, gst_ghost_pad_new("audio_sink0", pad))) {
1548                                 gst_object_unref(pad);
1549                                 pad = NULL;
1550                                 MMCAM_LOG_ERROR("failed to create ghost audio_sink0 on _MMCAMCORDER_ENCSINK_BIN.");
1551                                 err = MM_ERROR_CAMCORDER_GST_LINK;
1552                                 goto pipeline_creation_error;
1553                         }
1554                         gst_object_unref(pad);
1555                         pad = NULL;
1556                 }
1557         } else if (profile == MM_CAMCORDER_ENCBIN_PROFILE_AUDIO) {
1558                 pad = gst_element_get_request_pad(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "audio");
1559                 if (!gst_element_add_pad(sc->encode_element[_MMCAMCORDER_ENCSINK_BIN].gst, gst_ghost_pad_new("audio_sink0", pad))) {
1560                         gst_object_unref(pad);
1561                         pad = NULL;
1562                         MMCAM_LOG_ERROR("failed to create ghost audio_sink0 on _MMCAMCORDER_ENCSINK_BIN.");
1563                         err = MM_ERROR_CAMCORDER_GST_LINK;
1564                         goto pipeline_creation_error;
1565                 }
1566                 gst_object_unref(pad);
1567                 pad = NULL;
1568         } else {
1569                 /* for stillshot */
1570                 pad = gst_element_get_request_pad(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "image");
1571                 if (!gst_element_add_pad(sc->encode_element[_MMCAMCORDER_ENCSINK_BIN].gst, gst_ghost_pad_new("image_sink0", pad))) {
1572                         gst_object_unref(pad);
1573                         pad = NULL;
1574                         MMCAM_LOG_ERROR("failed to create ghost image_sink0 on _MMCAMCORDER_ENCSINK_BIN.");
1575                         err = MM_ERROR_CAMCORDER_GST_LINK;
1576                         goto pipeline_creation_error;
1577                 }
1578                 gst_object_unref(pad);
1579                 pad = NULL;
1580         }
1581
1582         MMCAM_LOG_INFO("Get pad complete");
1583
1584         /* Link internal element */
1585         if (!_mmcamcorder_link_elements(element_list)) {
1586                 MMCAM_LOG_ERROR("element link error.");
1587                 err = MM_ERROR_CAMCORDER_GST_LINK;
1588                 goto pipeline_creation_error;
1589         }
1590
1591         if (element_list)
1592                 g_list_free(element_list);
1593
1594         MMCAM_LOG_INFO("done");
1595
1596         return MM_ERROR_NONE;
1597
1598 pipeline_creation_error:
1599         _mmcamcorder_remove_all_handlers(handle, _MMCAMCORDER_HANDLER_VIDEOREC);
1600
1601         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_ENCSINK_ENCBIN);
1602         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_ENCSINK_SRC);
1603         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_ENCSINK_FILT);
1604         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_ENCSINK_VSCALE);
1605         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_ENCSINK_VSCALE_FILT);
1606         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_ENCSINK_VENC);
1607         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_ENCSINK_AENC);
1608         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_ENCSINK_IENC);
1609         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_ENCSINK_MUX);
1610         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_ENCSINK_SINK);
1611         _MMCAMCORDER_ELEMENT_REMOVE(sc->encode_element, _MMCAMCORDER_ENCSINK_BIN);
1612
1613         if (element_list)
1614                 g_list_free(element_list);
1615
1616         if (video_caps)
1617                 gst_caps_unref(video_caps);
1618
1619         if (videoscale_caps)
1620                 gst_caps_unref(videoscale_caps);
1621
1622         return err;
1623 }
1624
1625
1626 int _mmcamcorder_create_preview_pipeline(MMHandleType handle)
1627 {
1628         int err = MM_ERROR_NONE;
1629
1630         GstPad *srcpad = NULL;
1631         GstBus *bus = NULL;
1632
1633         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1634         _MMCamcorderSubContext *sc = NULL;
1635
1636         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1637
1638         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
1639         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1640         mmf_return_val_if_fail(sc->element, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1641
1642         MMCAM_LOG_INFO("");
1643
1644         /** Create gstreamer element **/
1645         /* Main pipeline */
1646         _MMCAMCORDER_PIPELINE_MAKE(sc, sc->element, _MMCAMCORDER_MAIN_PIPE, "camera_pipeline", err);
1647
1648         /* Sub pipeline */
1649         err = _mmcamcorder_create_preview_elements((MMHandleType)hcamcorder);
1650         if (err != MM_ERROR_NONE)
1651                 goto pipeline_creation_error;
1652
1653         /* Set data probe function */
1654         if (sc->element[_MMCAMCORDER_VIDEOSRC_QUE].gst) {
1655                 MMCAM_LOG_INFO("add video dataprobe to videosrc queue");
1656                 srcpad = gst_element_get_static_pad(sc->element[_MMCAMCORDER_VIDEOSRC_QUE].gst, "src");
1657         } else {
1658                 MMCAM_LOG_ERROR("there is no queue plugin");
1659                 goto pipeline_creation_error;
1660         }
1661
1662         if (srcpad) {
1663                 MMCAMCORDER_ADD_BUFFER_PROBE(srcpad, _MMCAMCORDER_HANDLER_PREVIEW,
1664                         __mmcamcorder_video_dataprobe_preview, hcamcorder);
1665
1666                 gst_object_unref(srcpad);
1667                 srcpad = NULL;
1668         } else {
1669                 MMCAM_LOG_ERROR("failed to get srcpad");
1670                 goto pipeline_creation_error;
1671         }
1672
1673         /* set dataprobe for video recording if it does not support dual stream. */
1674         if (sc->info_video->record_dual_stream == FALSE) {
1675                 if (_mmcamcorder_is_encoded_preview_pixel_format(sc->info_image->preview_format))
1676                         srcpad = gst_element_get_static_pad(sc->element[_MMCAMCORDER_VIDEOSRC_QUE].gst, "src");
1677                 else
1678                         srcpad = gst_element_get_static_pad(sc->element[_MMCAMCORDER_VIDEOSINK_QUE].gst, "src");
1679
1680                 MMCAMCORDER_ADD_BUFFER_PROBE(srcpad, _MMCAMCORDER_HANDLER_PREVIEW,
1681                         __mmcamcorder_video_dataprobe_record, hcamcorder);
1682                 gst_object_unref(srcpad);
1683                 srcpad = NULL;
1684         }
1685
1686         bus = gst_pipeline_get_bus(GST_PIPELINE(sc->element[_MMCAMCORDER_MAIN_PIPE].gst));
1687
1688         /* set sync handler */
1689         gst_bus_set_sync_handler(bus, _mmcamcorder_pipeline_bus_sync_callback, (gpointer)hcamcorder, NULL);
1690
1691         gst_object_unref(bus);
1692         bus = NULL;
1693
1694         return MM_ERROR_NONE;
1695
1696 pipeline_creation_error:
1697         _MMCAMCORDER_ELEMENT_REMOVE(sc->element, _MMCAMCORDER_MAIN_PIPE);
1698         return err;
1699 }
1700
1701
1702 void _mmcamcorder_ready_to_encode_callback(GstElement *element, guint size, gpointer handle)
1703 {
1704         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1705         _MMCamcorderSubContext *sc = NULL;
1706
1707         /*MMCAM_LOG_INFO("start");*/
1708
1709         mmf_return_if_fail(hcamcorder);
1710         mmf_return_if_fail(hcamcorder->sub_context);
1711         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
1712
1713         /* set flag */
1714         if (sc->info_video->push_encoding_buffer == PUSH_ENCODING_BUFFER_INIT) {
1715                 sc->info_video->push_encoding_buffer = PUSH_ENCODING_BUFFER_RUN;
1716                 MMCAM_LOG_WARNING("set push_encoding_buffer RUN");
1717         }
1718 }
1719
1720
1721 int _mmcamcorder_videosink_window_set(MMHandleType handle, type_element* VideosinkElement)
1722 {
1723         int err = MM_ERROR_NONE;
1724         int size = 0;
1725         int rect_x = 0;
1726         int rect_y = 0;
1727         int rect_width = 0;
1728         int rect_height = 0;
1729         int visible = 0;
1730         int rotation = MM_DISPLAY_ROTATION_NONE;
1731         int flip = MM_FLIP_NONE;
1732         int display_mode = MM_DISPLAY_MODE_DEFAULT;
1733         int display_geometry_method = MM_DISPLAY_METHOD_LETTER_BOX;
1734         int origin_size = 0;
1735         int zoom_attr = 0;
1736         int zoom_level = 0;
1737         int do_scaling = FALSE;
1738         int *dp_handle = NULL;
1739         MMCamWindowInfo *window_info = NULL;
1740         gulong xid;
1741         char *err_name = NULL;
1742         const char *videosink_name = NULL;
1743
1744         GstElement *vsink = NULL;
1745
1746         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1747         _MMCamcorderSubContext *sc = NULL;
1748
1749         MMCAM_LOG_INFO("");
1750
1751         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1752
1753         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
1754         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1755         mmf_return_val_if_fail(sc->element, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1756         mmf_return_val_if_fail(sc->element[_MMCAMCORDER_VIDEOSINK_SINK].gst, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1757
1758         vsink = sc->element[_MMCAMCORDER_VIDEOSINK_SINK].gst;
1759
1760         /* Get video display information */
1761         err = mm_camcorder_get_attributes(handle, &err_name,
1762                 MMCAM_DISPLAY_RECT_X, &rect_x,
1763                 MMCAM_DISPLAY_RECT_Y, &rect_y,
1764                 MMCAM_DISPLAY_RECT_WIDTH, &rect_width,
1765                 MMCAM_DISPLAY_RECT_HEIGHT, &rect_height,
1766                 MMCAM_DISPLAY_ROTATION, &rotation,
1767                 MMCAM_DISPLAY_FLIP, &flip,
1768                 MMCAM_DISPLAY_VISIBLE, &visible,
1769                 MMCAM_DISPLAY_HANDLE, (void **)&dp_handle, &size,
1770                 MMCAM_DISPLAY_MODE, &display_mode,
1771                 MMCAM_DISPLAY_GEOMETRY_METHOD, &display_geometry_method,
1772                 MMCAM_DISPLAY_SCALE, &zoom_attr,
1773                 MMCAM_DISPLAY_EVAS_DO_SCALING, &do_scaling,
1774                 NULL);
1775         if (err != MM_ERROR_NONE) {
1776                 if (err_name) {
1777                         MMCAM_LOG_ERROR("failed to get attributes [%s][0x%x]", err_name, err);
1778                         SAFE_FREE(err_name);
1779                 } else {
1780                         MMCAM_LOG_ERROR("failed to get attributes [0x%x]", err);
1781                 }
1782
1783                 return err;
1784         }
1785
1786         _mmcamcorder_conf_get_value_element_name(VideosinkElement, &videosink_name);
1787
1788         if (!videosink_name) {
1789                 MMCAM_LOG_ERROR("failed to get videosink name");
1790                 return MM_ERROR_CAMCORDER_INTERNAL;
1791         }
1792
1793         MMCAM_LOG_INFO("(dp_handle=%p, size=%d)", dp_handle, size);
1794
1795         /* Set display handle */
1796         if (!strcmp(videosink_name, "xvimagesink") || !strcmp(videosink_name, "ximagesink")) {
1797                 if (dp_handle) {
1798                         xid = *dp_handle;
1799                         MMCAM_LOG_INFO("xid = %lu )", xid);
1800                         gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(vsink), xid);
1801                 } else {
1802                         MMCAM_LOG_WARNING("Handle is NULL. Set xid as 0.. but, it's not recommended.");
1803                         gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(vsink), 0);
1804                 }
1805         } else if (!strcmp(videosink_name, "evasimagesink") || !strcmp(videosink_name, "evaspixmapsink")) {
1806                 MMCAM_LOG_INFO("videosink : %s, handle : %p", videosink_name, dp_handle);
1807
1808                 if (dp_handle) {
1809                         MMCAMCORDER_G_OBJECT_SET_POINTER(vsink, "evas-object", dp_handle);
1810                         MMCAMCORDER_G_OBJECT_SET(vsink, "origin-size", !do_scaling);
1811                 } else {
1812                         MMCAM_LOG_ERROR("display handle(eavs object) is NULL");
1813                         return MM_ERROR_CAMCORDER_INVALID_ARGUMENT;
1814                 }
1815         } else if (!strcmp(videosink_name, "tizenwlsink")) {
1816                 if (dp_handle) {
1817                         window_info = (MMCamWindowInfo *)dp_handle;
1818                         MMCAM_LOG_INFO("wayland global surface id : %d", window_info->surface_id);
1819                         gst_video_overlay_set_wl_window_wl_surface_id(GST_VIDEO_OVERLAY(vsink), window_info->surface_id);
1820                 } else {
1821                         MMCAM_LOG_WARNING("Handle is NULL. skip setting.");
1822                 }
1823         } else if (!strcmp(videosink_name, "directvideosink")) {
1824 #ifdef _MMCAMCORDER_RM_SUPPORT
1825                 /* set the videosink using the virtual device id instead of the real id (main=0, sub=1) */
1826                 MMCAM_LOG_INFO("device-scaler : %d", hcamcorder->returned_devices.device_id[1]);
1827                 MMCAMCORDER_G_OBJECT_SET(vsink, "device-scaler", hcamcorder->returned_devices.device_id[1]);
1828 #endif /* _MMCAMCORDER_RM_SUPPORT */
1829                 if (dp_handle) {
1830                         window_info = (MMCamWindowInfo *)dp_handle;
1831                         MMCAM_LOG_INFO("wayland global surface id : %d, x,y,w,h (%d,%d,%d,%d)",
1832                                 window_info->surface_id,
1833                                 window_info->rect.x,
1834                                 window_info->rect.y,
1835                                 window_info->rect.width,
1836                                 window_info->rect.height);
1837                         gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(vsink), (guintptr)window_info->surface_id);
1838                         gst_video_overlay_set_render_rectangle(GST_VIDEO_OVERLAY(vsink),
1839                                 window_info->rect.x,
1840                                 window_info->rect.y,
1841                                 window_info->rect.width,
1842                                 window_info->rect.height);
1843                 } else {
1844                         MMCAM_LOG_WARNING("dp_handle is null");
1845                 }
1846         } else {
1847                 MMCAM_LOG_WARNING("Who are you?? (Videosink: %s)", videosink_name);
1848         }
1849
1850         MMCAM_LOG_INFO("%s set: display_geometry_method[%d],origin-size[%d],visible[%d],rotate[%d],flip[%d]",
1851                 videosink_name, display_geometry_method, origin_size, visible, rotation, flip);
1852
1853         /* Set attribute */
1854         if (!strcmp(videosink_name, "xvimagesink") || !strcmp(videosink_name, "tizenwlsink") ||
1855             !strcmp(videosink_name, "evaspixmapsink") || !strcmp(videosink_name, "directvideosink")) {
1856                 /* set rotation */
1857                 MMCAMCORDER_G_OBJECT_SET(vsink, "rotate", rotation);
1858
1859                 /* set flip */
1860                 MMCAMCORDER_G_OBJECT_SET(vsink, "flip", flip);
1861
1862                 switch (zoom_attr) {
1863                 case MM_DISPLAY_SCALE_DEFAULT:
1864                         zoom_level = 1;
1865                         break;
1866                 case MM_DISPLAY_SCALE_DOUBLE_LENGTH:
1867                         zoom_level = 2;
1868                         break;
1869                 case MM_DISPLAY_SCALE_TRIPLE_LENGTH:
1870                         zoom_level = 3;
1871                         break;
1872                 default:
1873                         MMCAM_LOG_WARNING("Unsupported zoom value. set as default.");
1874                         zoom_level = 1;
1875                         break;
1876                 }
1877
1878                 MMCAMCORDER_G_OBJECT_SET(vsink, "display-geometry-method", display_geometry_method);
1879                 MMCAMCORDER_G_OBJECT_SET(vsink, "display-mode", display_mode);
1880                 MMCAMCORDER_G_OBJECT_SET(vsink, "visible", visible);
1881                 MMCAMCORDER_G_OBJECT_SET(vsink, "zoom", zoom_level);
1882
1883                 if (display_geometry_method == MM_DISPLAY_METHOD_CUSTOM_ROI) {
1884                         if (!strcmp(videosink_name, "tizenwlsink")) {
1885                             gst_video_overlay_set_display_roi_area(GST_VIDEO_OVERLAY(vsink),
1886                                         rect_x, rect_y, rect_width, rect_height);
1887                         } else {
1888                                 g_object_set(vsink,
1889                                         "dst-roi-x", rect_x,
1890                                         "dst-roi-y", rect_y,
1891                                         "dst-roi-w", rect_width,
1892                                         "dst-roi-h", rect_height,
1893                                         NULL);
1894                         }
1895                 }
1896         }
1897
1898         return MM_ERROR_NONE;
1899 }
1900
1901
1902 int _mmcamcorder_video_frame_stabilize(MMHandleType handle, int cmd)
1903 {
1904         int category = 0;
1905         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1906         _MMCamcorderSubContext *sc = NULL;
1907
1908         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1909
1910         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
1911
1912         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1913
1914         switch (cmd) {
1915         case _MMCamcorder_CMD_PREVIEW_START:
1916                 category = CONFIGURE_CATEGORY_CTRL_CAMERA;
1917                 break;
1918         case _MMCamcorder_CMD_CAPTURE:
1919                 category = CONFIGURE_CATEGORY_CTRL_CAPTURE;
1920                 break;
1921         default:
1922                 MMCAM_LOG_WARNING("unknown command : %d", cmd);
1923                 return MM_ERROR_CAMCORDER_INVALID_ARGUMENT;
1924         }
1925
1926         _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_ctrl,
1927                 category, "FrameStabilityCount", &sc->frame_stability_count);
1928
1929         MMCAM_LOG_INFO("[cmd %d] frame stability count : %d",
1930                 cmd, sc->frame_stability_count);
1931
1932         return MM_ERROR_NONE;
1933 }
1934
1935 /* Retreive device information and set them to attributes */
1936 gboolean _mmcamcorder_get_device_info(MMHandleType handle)
1937 {
1938         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1939         _MMCamcorderSubContext *sc = NULL;
1940         GstCameraControl *control = NULL;
1941         GstCameraControlExifInfo exif_info = {0,};
1942
1943         mmf_return_val_if_fail(hcamcorder, FALSE);
1944         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
1945
1946         memset(&exif_info, 0x0, sizeof(GstCameraControlExifInfo));
1947
1948         if (sc && sc->element) {
1949                 int err = MM_ERROR_NONE;
1950                 char *err_name = NULL;
1951                 double focal_len = 0.0;
1952
1953                 /* Video input device */
1954                 if (sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst) {
1955                         /* Exif related information */
1956                         control = GST_CAMERA_CONTROL(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst);
1957                         if (control != NULL) {
1958                                 gst_camera_control_get_exif_info(control, &exif_info); /* get video input device information */
1959                                 if (exif_info.focal_len_denominator != 0)
1960                                         focal_len = ((double)exif_info.focal_len_numerator) / ((double) exif_info.focal_len_denominator);
1961                         } else {
1962                                 MMCAM_LOG_ERROR("Fail to get camera control interface!");
1963                                 focal_len = 0.0;
1964                         }
1965                 }
1966
1967                 /* Set values to attributes */
1968                 err = mm_camcorder_set_attributes(handle, &err_name,
1969                         MMCAM_CAMERA_FOCAL_LENGTH, focal_len,
1970                         NULL);
1971                 if (err != MM_ERROR_NONE) {
1972                         MMCAM_LOG_ERROR("Set attributes error(%s:%x)!", err_name, err);
1973                         SAFE_FREE(err_name);
1974                         return FALSE;
1975                 }
1976         } else {
1977                 MMCAM_LOG_WARNING("Sub context isn't exist.");
1978                 return FALSE;
1979         }
1980
1981         return TRUE;
1982 }
1983
1984 static guint32 _mmcamcorder_convert_fourcc_string_to_value(const gchar* format_name)
1985 {
1986         return format_name[0] | (format_name[1] << 8) | (format_name[2] << 16) | (format_name[3] << 24);
1987 }
1988
1989 static GstPadProbeReturn __mmcamcorder_video_dataprobe_preview(GstPad *pad, GstPadProbeInfo *info, gpointer u_data)
1990 {
1991         gboolean ret = TRUE;
1992         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(u_data);
1993         _MMCamcorderSubContext *sc = NULL;
1994         GstBuffer *buffer = GST_PAD_PROBE_INFO_BUFFER(info);
1995         GstSample *sample = NULL;
1996         GstCaps *caps = NULL;
1997
1998         mmf_return_val_if_fail(buffer, GST_PAD_PROBE_DROP);
1999         mmf_return_val_if_fail(gst_buffer_n_memory(buffer), GST_PAD_PROBE_DROP);
2000         mmf_return_val_if_fail(hcamcorder, GST_PAD_PROBE_DROP);
2001
2002         sc = MMF_CAMCORDER_SUBCONTEXT(u_data);
2003         mmf_return_val_if_fail(sc, GST_PAD_PROBE_DROP);
2004
2005         if (sc->drop_vframe > 0) {
2006                 if (sc->pass_first_vframe > 0) {
2007                         sc->pass_first_vframe--;
2008                         MMCAM_LOG_INFO("Pass video frame by pass_first_vframe");
2009                 } else {
2010                         sc->drop_vframe--;
2011                         MMCAM_LOG_INFO("Drop video frame by drop_vframe");
2012                         return GST_PAD_PROBE_DROP;
2013                 }
2014         } else if (sc->frame_stability_count > 0) {
2015                 sc->frame_stability_count--;
2016                 MMCAM_LOG_INFO("Drop video frame by frame_stability_count");
2017                 return GST_PAD_PROBE_DROP;
2018         }
2019
2020         if (hcamcorder->measure_preview_fps && hcamcorder->state >= MM_CAMCORDER_STATE_PREPARE)
2021                 _mmcamcorder_measure_fps(&sc->kpi);
2022
2023         /* The first H.264 frame should not be skipped for vstream cb. */
2024         if (hcamcorder->state < MM_CAMCORDER_STATE_PREPARE &&
2025                 !_mmcamcorder_is_encoded_preview_pixel_format(sc->info_image->preview_format)) {
2026                 MMCAM_LOG_WARNING("Not ready for stream callback");
2027                 return GST_PAD_PROBE_OK;
2028         }
2029
2030         /* make sample with buffer and caps */
2031         caps = gst_pad_get_allowed_caps(pad);
2032         mmf_return_val_if_fail(caps, GST_PAD_PROBE_OK);
2033
2034         sample = gst_sample_new(buffer, caps, NULL, NULL);
2035         gst_caps_unref(caps);
2036         mmf_return_val_if_fail(sample, GST_PAD_PROBE_OK);
2037
2038         ret = _mmcamcorder_invoke_video_stream_cb((MMHandleType)hcamcorder, sample, TRUE, -1);
2039
2040         gst_sample_unref(sample);
2041
2042         if (ret)
2043                 return GST_PAD_PROBE_OK;
2044         else
2045                 return GST_PAD_PROBE_DROP;
2046 }
2047
2048
2049 static GstPadProbeReturn __mmcamcorder_video_dataprobe_record(GstPad *pad, GstPadProbeInfo *info, gpointer u_data)
2050 {
2051         gboolean ret = TRUE;
2052         GstSample *sample = NULL;
2053         GstCaps *caps = NULL;
2054
2055         caps = gst_pad_get_allowed_caps(pad);
2056         mmf_return_val_if_fail(caps, GST_PAD_PROBE_OK);
2057
2058         sample = gst_sample_new(GST_PAD_PROBE_INFO_BUFFER(info), caps, NULL, NULL);
2059         gst_caps_unref(caps);
2060         mmf_return_val_if_fail(sample, GST_PAD_PROBE_OK);
2061
2062         ret = _mmcamcorder_video_push_buffer(u_data, sample);
2063
2064         gst_sample_unref(sample);
2065
2066         return (ret ? GST_PAD_PROBE_OK : 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_LOG_WARNING("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_LOG_INFO("[%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_LOG_INFO("[%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_LOG_WARNING("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_LOG_WARNING("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_LOG_WARNING("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_LOG_WARNING("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_LOG_WARNING("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_LOG_WARNING("failed to get GST_TAG_ALBUM_PEAK");
2167                                 break;
2168                         }
2169
2170                         MMCAM_LOG_INFO("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_LOG_INFO("[%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_LOG_WARNING("NULL handle");
2181                         break;
2182                 }
2183
2184                 sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
2185                 if (!sc) {
2186                         MMCAM_LOG_WARNING("NULL sub context");
2187                         break;
2188                 }
2189
2190                 if (!sc->encode_element[_MMCAMCORDER_ENCSINK_SINK].gst) {
2191                         MMCAM_LOG_WARNING("no encoder sink");
2192                         break;
2193                 }
2194
2195                 parent = gst_pad_get_parent(pad);
2196                 if (!parent) {
2197                         MMCAM_LOG_WARNING("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_LOG_INFO("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_LOG_WARNING("[%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_LOG_ERROR("[%s:%s] gots %s", GST_DEBUG_PAD_NAME(pad), GST_EVENT_TYPE_NAME(event));
2222                 break;
2223         default:
2224                 MMCAM_LOG_INFO("[%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_LOG_INFO("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_LOG_INFO("EOS signal received");
2277                 } else {
2278                         MMCAM_LOG_ERROR("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_LOG_INFO("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_committing)
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_committing)
2297                                 _mmcamcorder_audio_handle_eos((MMHandleType)hcamcorder);
2298                 }
2299         } else {
2300                 ret = hcamcorder->error_code;
2301                 MMCAM_LOG_ERROR("error 0x%x", ret);
2302         }
2303
2304         MMCAM_LOG_INFO("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_LOG_INFO("");
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_LOG_ERROR("Audio codec[%d] and file format[%d] compatibility FAILED.", codec, file_format);
2339                                 return MM_ERROR_CAMCORDER_ENCODER_WRONG_TYPE;
2340                         }
2341
2342                         MMCAM_LOG_INFO("Audio codec[%d] and file format[%d] compatibility SUCCESS.", codec, file_format);
2343                 } else {
2344                         MMCAM_LOG_ERROR("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_LOG_ERROR("Video codec[%d] and file format[%d] compatibility FAILED.", codec, file_format);
2352                                 return MM_ERROR_CAMCORDER_ENCODER_WRONG_TYPE;
2353                         }
2354
2355                         MMCAM_LOG_INFO("Video codec[%d] and file format[%d] compatibility SUCCESS.", codec, file_format);
2356                 } else {
2357                         MMCAM_LOG_ERROR("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_LOG_WARNING("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_LOG_WARNING("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_LOG_ERROR("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_LOG_INFO("Set display-rotate [%d] done.", display_rotate);
2443                 } else {
2444                         MMCAM_LOG_WARNING("[%s] does not support DISPLAY_ROTATION, but no error", videosink_name);
2445                 }
2446
2447                 return TRUE;
2448         } else {
2449                 MMCAM_LOG_ERROR("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_LOG_ERROR("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_LOG_INFO("Set display flip [%d] done.", display_flip);
2482                 } else {
2483                         MMCAM_LOG_WARNING("[%s] does not support DISPLAY_FLIP, but no error", videosink_name);
2484                 }
2485
2486                 return TRUE;
2487         } else {
2488                 MMCAM_LOG_ERROR("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_LOG_INFO("sub context is not initialized");
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_LOG_INFO("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         char fourcc_string[sizeof(fourcc) + 1];
2533         gchar *caps_str = NULL;
2534 #ifdef _MMCAMCORDER_PRODUCT_TV
2535         gint maxwidth = 0;
2536         gint maxheight = 0;
2537         int display_surface_type = MM_DISPLAY_SURFACE_NULL;
2538 #endif /* _MMCAMCORDER_PRODUCT_TV */
2539
2540         GstCaps *caps = NULL;
2541
2542         mmf_camcorder_t *hcamcorder = NULL;
2543         _MMCamcorderSubContext *sc = NULL;
2544
2545         hcamcorder = MMF_CAMCORDER(handle);
2546         mmf_return_val_if_fail(hcamcorder, FALSE);
2547
2548         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2549
2550         if (!sc || !(sc->element)) {
2551                 MMCAM_LOG_INFO("sub context is not initialized");
2552                 return TRUE;
2553         }
2554
2555         if (!sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst) {
2556                 MMCAM_LOG_ERROR("Video src is NULL!");
2557                 return FALSE;
2558         }
2559
2560         if (!sc->element[_MMCAMCORDER_VIDEOSRC_FILT].gst) {
2561                 MMCAM_LOG_ERROR("Video filter is NULL!");
2562                 return FALSE;
2563         }
2564
2565         if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
2566                 int capture_width = 0;
2567                 int capture_height = 0;
2568                 double motion_rate = _MMCAMCORDER_DEFAULT_RECORDING_MOTION_RATE;
2569
2570                 mm_camcorder_get_attributes(handle, NULL,
2571                         MMCAM_CAMERA_RECORDING_MOTION_RATE, &motion_rate,
2572                         MMCAM_CAMERA_FPS_AUTO, &fps_auto,
2573                         MMCAM_CAPTURE_WIDTH, &capture_width,
2574                         MMCAM_CAPTURE_HEIGHT, &capture_height,
2575                         MMCAM_VIDEO_WIDTH, &sc->info_video->video_width,
2576                         MMCAM_VIDEO_HEIGHT, &sc->info_video->video_height,
2577                         NULL);
2578                 MMCAM_LOG_INFO("motion rate %f, capture size %dx%d, fps auto %d, video size %dx%d",
2579                         motion_rate, capture_width, capture_height, fps_auto,
2580                         sc->info_video->video_width, sc->info_video->video_height);
2581
2582                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst,
2583                         "high-speed-fps", (motion_rate != _MMCAMCORDER_DEFAULT_RECORDING_MOTION_RATE ? fps : 0));
2584
2585                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "capture-width", capture_width);
2586                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "capture-height", capture_height);
2587                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "fps-auto", fps_auto);
2588
2589                 /* set fps */
2590                 sc->info_video->fps = fps;
2591         }
2592
2593         /* Interleaved format does not support rotation */
2594         if (sc->info_image->preview_format != MM_PIXEL_FORMAT_ITLV_JPEG_UYVY) {
2595                 /* store videosrc rotation */
2596                 sc->videosrc_rotate = rotate;
2597
2598                 /* Define width, height and rotate in caps */
2599
2600                 /* This will be applied when rotate is 0, 90, 180, 270 if rear camera.
2601                 This will be applied when rotate is 0, 180 if front camera. */
2602                 set_rotate = rotate * 90;
2603
2604                 if (rotate == MM_VIDEO_INPUT_ROTATION_90 ||
2605                     rotate == MM_VIDEO_INPUT_ROTATION_270) {
2606                         set_width = height;
2607                         set_height = width;
2608                         if (hcamcorder->device_type == MM_VIDEO_DEVICE_CAMERA1) {
2609                                 if (rotate == MM_VIDEO_INPUT_ROTATION_90)
2610                                         set_rotate = 270;
2611                                 else
2612                                         set_rotate = 90;
2613                         }
2614                 } else {
2615                         set_width = width;
2616                         set_height = height;
2617                 }
2618         } else {
2619                 sc->videosrc_rotate = MM_VIDEO_INPUT_ROTATION_NONE;
2620                 set_rotate = 0;
2621                 set_width = width;
2622                 set_height = height;
2623
2624                 MMCAM_LOG_WARNING("ITLV format doe snot support INPUT ROTATE. Ignore ROTATE[%d]", rotate);
2625         }
2626
2627         MMCAMCORDER_G_OBJECT_GET(sc->element[_MMCAMCORDER_VIDEOSRC_FILT].gst, "caps", &caps);
2628         if (caps && !gst_caps_is_any(caps)) {
2629                 GstStructure *structure = NULL;
2630
2631                 structure = gst_caps_get_structure(caps, 0);
2632                 if (structure) {
2633                         const gchar *format_string = NULL;
2634                         int caps_width = 0;
2635                         int caps_height = 0;
2636                         int caps_fps = 0;
2637                         int caps_rotate = 0;
2638
2639                         format_string = gst_structure_get_string(structure, "format");
2640                         if (format_string)
2641                                 caps_fourcc = _mmcamcorder_convert_fourcc_string_to_value(format_string);
2642
2643                         gst_structure_get_int(structure, "width", &caps_width);
2644                         gst_structure_get_int(structure, "height", &caps_height);
2645                         gst_structure_get_int(structure, "fps", &caps_fps);
2646                         gst_structure_get_int(structure, "rotate", &caps_rotate);
2647
2648 #ifdef _MMCAMCORDER_PRODUCT_TV
2649                         if (_mmcamcorder_is_encoded_preview_pixel_format(sc->info_image->preview_format)) {
2650                                 if (set_width == caps_width && set_height == caps_height &&
2651                                         set_rotate == caps_rotate && fps == caps_fps) {
2652                                         MMCAM_LOG_INFO("No need to replace caps.");
2653                                 } else {
2654                                         MMCAM_LOG_INFO("current [%c%c%c%c %dx%d, fps %d, rot %d], new [%c%c%c%c %dx%d, fps %d, rot %d]",
2655                                                 caps_fourcc, caps_fourcc>>8, caps_fourcc>>16, caps_fourcc>>24,
2656                                                 caps_width, caps_height, caps_fps, caps_rotate,
2657                                                 fourcc, fourcc>>8, fourcc>>16, fourcc>>24,
2658                                                 set_width, set_height, fps, set_rotate);
2659                                         do_set_caps = TRUE;
2660                                 }
2661                         } else {
2662                                 if (set_width == caps_width && set_height == caps_height &&
2663                                     fourcc == caps_fourcc && set_rotate == caps_rotate && fps == caps_fps) {
2664                                         MMCAM_LOG_INFO("No need to replace caps.");
2665                                 } else {
2666                                         MMCAM_LOG_INFO("current [%c%c%c%c %dx%d, fps %d, rot %d], new [%c%c%c%c %dx%d, fps %d, rot %d]",
2667                                                 caps_fourcc, caps_fourcc>>8, caps_fourcc>>16, caps_fourcc>>24,
2668                                                 caps_width, caps_height, caps_fps, caps_rotate,
2669                                                 fourcc, fourcc>>8, fourcc>>16, fourcc>>24,
2670                                                 set_width, set_height, fps, set_rotate);
2671                                         do_set_caps = TRUE;
2672                                 }
2673                         }
2674 #else /*_MMCAMCORDER_PRODUCT_TV */
2675                         if (set_width == caps_width && set_height == caps_height &&
2676                             fourcc == caps_fourcc && set_rotate == caps_rotate && fps == caps_fps) {
2677                                 MMCAM_LOG_INFO("No need to replace caps.");
2678                         } else {
2679                                 MMCAM_LOG_INFO("current [%c%c%c%c %dx%d, fps %d, rot %d], new [%c%c%c%c %dx%d, fps %d, rot %d]",
2680                                         caps_fourcc, caps_fourcc>>8, caps_fourcc>>16, caps_fourcc>>24,
2681                                         caps_width, caps_height, caps_fps, caps_rotate,
2682                                         fourcc, fourcc>>8, fourcc>>16, fourcc>>24,
2683                                         set_width, set_height, fps, set_rotate);
2684                                 do_set_caps = TRUE;
2685                         }
2686 #endif /*_MMCAMCORDER_PRODUCT_TV */
2687                 } else {
2688                         MMCAM_LOG_INFO("can not get structure of caps. set new one...");
2689                         do_set_caps = TRUE;
2690                 }
2691         } else {
2692                 MMCAM_LOG_INFO("No caps. set new one...");
2693                 do_set_caps = TRUE;
2694         }
2695
2696         if (caps) {
2697                 gst_caps_unref(caps);
2698                 caps = NULL;
2699         }
2700
2701         if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
2702                 /* assume that it's camera capture mode */
2703                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSINK_SINK].gst, "csc-range", 1);
2704         }
2705
2706         if (!do_set_caps)
2707                 return TRUE;
2708
2709         switch (sc->info_image->preview_format) {
2710         case MM_PIXEL_FORMAT_ENCODED_H264:
2711 #ifdef _MMCAMCORDER_PRODUCT_TV
2712                 mm_camcorder_get_attributes(handle, NULL,
2713                         MMCAM_DISPLAY_SURFACE, &display_surface_type,
2714                         NULL);
2715                 if (display_surface_type != MM_DISPLAY_SURFACE_NULL &&
2716                         __mmcamcorder_find_max_resolution(handle, &maxwidth, &maxheight) == false) {
2717                         MMCAM_LOG_ERROR("can not find max resolution limitation");
2718                         return false;
2719                 } else if (display_surface_type == MM_DISPLAY_SURFACE_NULL) {
2720                         maxwidth = set_width;
2721                         maxheight = set_height;
2722                 }
2723 #endif /* _MMCAMCORDER_PRODUCT_TV */
2724                 caps = gst_caps_new_simple("video/x-h264",
2725                         "width", G_TYPE_INT, set_width,
2726                         "height", G_TYPE_INT, set_height,
2727                         "framerate", GST_TYPE_FRACTION, fps, 1,
2728                         "stream-format", G_TYPE_STRING, "byte-stream",
2729 #ifdef _MMCAMCORDER_PRODUCT_TV
2730                         "maxwidth", G_TYPE_INT, maxwidth,
2731                         "maxheight", G_TYPE_INT, maxheight,
2732                         "alignment", G_TYPE_STRING, "au",
2733 #endif /* _MMCAMCORDER_PRODUCT_TV */
2734                         NULL);
2735                 break;
2736         case MM_PIXEL_FORMAT_ENCODED_MJPEG:
2737 #ifdef _MMCAMCORDER_PRODUCT_TV
2738                 caps = gst_caps_new_simple("video/x-jpeg",
2739 #else
2740                 caps = gst_caps_new_simple("image/jpeg",
2741 #endif
2742                         "width", G_TYPE_INT, set_width,
2743                         "height", G_TYPE_INT, set_height,
2744                         "framerate", GST_TYPE_FRACTION, fps, 1,
2745                         NULL);
2746                 break;
2747         case MM_PIXEL_FORMAT_ENCODED_VP8:
2748                 caps = gst_caps_new_simple("video/x-vp8",
2749                         "width", G_TYPE_INT, set_width,
2750                         "height", G_TYPE_INT, set_height,
2751                         "framerate", GST_TYPE_FRACTION, fps, 1,
2752                         NULL);
2753                 break;
2754         case MM_PIXEL_FORMAT_ENCODED_VP9:
2755                 caps = gst_caps_new_simple("video/x-vp9",
2756                         "width", G_TYPE_INT, set_width,
2757                         "height", G_TYPE_INT, set_height,
2758                         "framerate", GST_TYPE_FRACTION, fps, 1,
2759                         NULL);
2760                 break;
2761                 break;
2762         default:
2763                 memcpy(fourcc_string, (char*)&fourcc, sizeof(fourcc));
2764                 fourcc_string[sizeof(fourcc)] = '\0';
2765                 caps = gst_caps_new_simple("video/x-raw",
2766                         "format", G_TYPE_STRING, fourcc_string,
2767                         "width", G_TYPE_INT, set_width,
2768                         "height", G_TYPE_INT, set_height,
2769                         "framerate", GST_TYPE_FRACTION, fps, 1,
2770                         "rotate", G_TYPE_INT, set_rotate,
2771                         NULL);
2772                 break;
2773         }
2774
2775         if (!caps) {
2776                 MMCAM_LOG_ERROR("There are no caps");
2777                 return FALSE;
2778         }
2779
2780         caps_str = gst_caps_to_string(caps);
2781         MMCAM_LOG_INFO("vidoesrc new caps set [%s]", caps_str);
2782         g_free(caps_str);
2783
2784         MMCAMCORDER_G_OBJECT_SET_POINTER(sc->element[_MMCAMCORDER_VIDEOSRC_FILT].gst, "caps", caps);
2785         gst_caps_unref(caps);
2786
2787         return TRUE;
2788 }
2789
2790
2791 bool _mmcamcorder_set_videosrc_flip(MMHandleType handle, int videosrc_flip)
2792 {
2793         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
2794         _MMCamcorderSubContext *sc = NULL;
2795
2796         mmf_return_val_if_fail(hcamcorder, FALSE);
2797
2798         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2799         if (!sc)
2800                 return TRUE;
2801
2802         MMCAM_LOG_INFO("Set FLIP %d", videosrc_flip);
2803
2804         if (sc->element && sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst) {
2805                 int hflip = 0;
2806                 int vflip = 0;
2807
2808                 /* Interleaved format does not support FLIP */
2809                 if (sc->info_image->preview_format != MM_PIXEL_FORMAT_ITLV_JPEG_UYVY) {
2810                         hflip = (videosrc_flip & MM_FLIP_HORIZONTAL) == MM_FLIP_HORIZONTAL;
2811                         vflip = (videosrc_flip & MM_FLIP_VERTICAL) == MM_FLIP_VERTICAL;
2812
2813                         MMCAM_LOG_INFO("videosrc flip H:%d, V:%d", hflip, vflip);
2814
2815                         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "hflip", hflip);
2816                         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "vflip", vflip);
2817                 } else {
2818                         MMCAM_LOG_WARNING("ITLV format does not support FLIP. Ignore FLIP[%d]",
2819                                 videosrc_flip);
2820                 }
2821         } else {
2822                 MMCAM_LOG_WARNING("element is NULL");
2823                 return FALSE;
2824         }
2825
2826         return TRUE;
2827 }
2828
2829
2830 bool _mmcamcorder_set_videosrc_anti_shake(MMHandleType handle, int anti_shake)
2831 {
2832         GstCameraControl *control = NULL;
2833         _MMCamcorderSubContext *sc = NULL;
2834         GstElement *v_src = NULL;
2835
2836         int set_value = 0;
2837
2838         mmf_return_val_if_fail(handle, FALSE);
2839
2840         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2841         if (!sc)
2842                 return TRUE;
2843
2844         v_src = sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst;
2845
2846         if (!v_src) {
2847                 MMCAM_LOG_WARNING("videosrc element is NULL");
2848                 return FALSE;
2849         }
2850
2851         set_value = _mmcamcorder_convert_msl_to_sensor(handle, MM_CAM_CAMERA_ANTI_HANDSHAKE, anti_shake);
2852
2853         /* set anti-shake with camera control */
2854         if (!GST_IS_CAMERA_CONTROL(v_src)) {
2855                 MMCAM_LOG_WARNING("Can't cast Video source into camera control.");
2856                 return FALSE;
2857         }
2858
2859         control = GST_CAMERA_CONTROL(v_src);
2860         if (gst_camera_control_set_ahs(control, set_value)) {
2861                 MMCAM_LOG_INFO("Succeed in operating anti-handshake. value[%d]", set_value);
2862                 return TRUE;
2863         } else {
2864                 MMCAM_LOG_WARNING("Failed to operate anti-handshake. value[%d]", set_value);
2865         }
2866
2867         return FALSE;
2868 }
2869
2870
2871 bool _mmcamcorder_set_videosrc_stabilization(MMHandleType handle, int stabilization)
2872 {
2873         _MMCamcorderSubContext *sc = NULL;
2874         GstElement *v_src = NULL;
2875
2876         mmf_return_val_if_fail(handle, FALSE);
2877
2878         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2879         if (!sc)
2880                 return TRUE;
2881
2882         v_src = sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst;
2883         if (!v_src) {
2884                 MMCAM_LOG_WARNING("videosrc element is NULL");
2885                 return FALSE;
2886         }
2887
2888         /* check property of videosrc element - support VDIS */
2889         if (g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(v_src)), "enable-vdis-mode")) {
2890                 int video_width = 0;
2891                 int video_height = 0;
2892
2893                 if (stabilization == MM_CAMCORDER_VIDEO_STABILIZATION_ON) {
2894                         MMCAM_LOG_INFO("ENABLE video stabilization");
2895
2896                         /* VDIS mode only supports NV12 and [720p or 1080p or 1088 * 1088] */
2897                         mm_camcorder_get_attributes(handle, NULL,
2898                                 MMCAM_VIDEO_WIDTH, &video_width,
2899                                 MMCAM_VIDEO_HEIGHT, &video_height,
2900                                 NULL);
2901
2902                         if (sc->info_image->preview_format == MM_PIXEL_FORMAT_NV12 && video_width >= 1080 && video_height >= 720) {
2903                                 MMCAM_LOG_INFO("NV12, video size %dx%d, ENABLE video stabilization",
2904                                         video_width, video_height);
2905                                 /* set vdis mode */
2906                                 g_object_set(G_OBJECT(v_src),
2907                                                          "enable-vdis-mode", TRUE,
2908                                                          NULL);
2909                         } else {
2910                                 MMCAM_LOG_WARNING("invalid preview format %c%c%c%c or video size %dx%d",
2911                                                                 sc->fourcc, sc->fourcc>>8, sc->fourcc>>16, sc->fourcc>>24,
2912                                                                 video_width, video_height);
2913                                 return FALSE;
2914                         }
2915                 } else {
2916                         /* set vdis mode */
2917                         g_object_set(G_OBJECT(v_src),
2918                                                 "enable-vdis-mode", FALSE,
2919                                                 NULL);
2920
2921                         MMCAM_LOG_INFO("DISABLE video stabilization");
2922                 }
2923         } else if (stabilization == MM_CAMCORDER_VIDEO_STABILIZATION_ON) {
2924                 MMCAM_LOG_ERROR("no property for video stabilization, so can not set ON");
2925                 return FALSE;
2926         } else {
2927                 MMCAM_LOG_WARNING("no property for video stabilization");
2928         }
2929
2930         return TRUE;
2931 }
2932
2933
2934 bool _mmcamcorder_set_camera_resolution(MMHandleType handle, int width, int height)
2935 {
2936         int fps = 0;
2937
2938         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
2939         _MMCamcorderSubContext *sc = NULL;
2940
2941         mmf_return_val_if_fail(hcamcorder, FALSE);
2942
2943         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2944         if (!sc)
2945                 return TRUE;
2946
2947         mm_camcorder_get_attributes(handle, NULL,
2948                 MMCAM_CAMERA_FPS, &fps,
2949                 NULL);
2950
2951         MMCAM_LOG_INFO("set %dx%d", width, height);
2952
2953         return _mmcamcorder_set_videosrc_caps(handle, sc->fourcc, width, height, fps, sc->videosrc_rotate);
2954 }
2955
2956
2957 bool _mmcamcorder_set_encoded_preview_bitrate(MMHandleType handle, int bitrate)
2958 {
2959         _MMCamcorderSubContext *sc = NULL;
2960
2961         if ((void *)handle == NULL) {
2962                 MMCAM_LOG_WARNING("handle is NULL");
2963                 return FALSE;
2964         }
2965
2966         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2967         if (!sc) {
2968                 MMCAM_LOG_WARNING("subcontext is NULL");
2969                 return FALSE;
2970         }
2971
2972         if (sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst == NULL) {
2973                 MMCAM_LOG_WARNING("videosrc plugin is NULL");
2974                 return FALSE;
2975         }
2976
2977         MMCAM_LOG_INFO("set encoded preview bitrate : %d bps", bitrate);
2978
2979         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "bitrate", bitrate);
2980
2981         return TRUE;
2982 }
2983
2984
2985 bool _mmcamcorder_set_encoded_preview_gop_interval(MMHandleType handle, int gop_interval)
2986 {
2987         _MMCamcorderSubContext *sc = NULL;
2988
2989         if ((void *)handle == NULL) {
2990                 MMCAM_LOG_WARNING("handle is NULL");
2991                 return FALSE;
2992         }
2993
2994         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2995         if (!sc) {
2996                 MMCAM_LOG_WARNING("subcontext is NULL");
2997                 return FALSE;
2998         }
2999
3000         if (sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst == NULL) {
3001                 MMCAM_LOG_WARNING("videosrc plugin is NULL");
3002                 return FALSE;
3003         }
3004
3005         MMCAM_LOG_INFO("set encoded preview GOP interval : %d ms", gop_interval);
3006
3007         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "gop-interval", gop_interval);
3008
3009         return TRUE;
3010 }
3011
3012
3013 bool _mmcamcorder_set_sound_stream_info(GstElement *element, char *stream_type, int stream_index)
3014 {
3015         GstStructure *props = NULL;
3016         char stream_props[64] = {'\0',};
3017
3018         if (element == NULL || stream_type == NULL || stream_index < 0) {
3019                 MMCAM_LOG_ERROR("invalid argument %p %p %d", element, stream_type, stream_index);
3020                 return FALSE;
3021         }
3022
3023         snprintf(stream_props, sizeof(stream_props) - 1,
3024                 "props,media.role=%s, media.parent_id=%d",
3025                 stream_type, stream_index);
3026
3027         MMCAM_LOG_WARNING("stream type %s, index %d -> [%s]", stream_type, stream_index, stream_props);
3028
3029         props = gst_structure_from_string(stream_props, NULL);
3030         if (!props) {
3031                 MMCAM_LOG_ERROR("failed to create GstStructure");
3032                 return FALSE;
3033         }
3034
3035         MMCAMCORDER_G_OBJECT_SET_POINTER(element, "stream-properties", props);
3036
3037         gst_structure_free(props);
3038         props = NULL;
3039
3040         return TRUE;
3041 }
3042
3043
3044 bool _mmcamcorder_recreate_decoder_for_encoded_preview(MMHandleType handle)
3045 {
3046         int ret = MM_ERROR_NONE;
3047         _MMCamcorderSubContext *sc = NULL;
3048         mmf_camcorder_t *hcamcorder = NULL;
3049         const char *videodecoder_name = NULL;
3050         char videodecoder_name_final[32] = {'\0',};
3051         int display_surface_type = MM_DISPLAY_SURFACE_NULL;
3052 #ifdef _MMCAMCORDER_RM_SUPPORT
3053         int decoder_index = 0;
3054 #endif /* _MMCAMCORDER_RM_SUPPORT */
3055
3056         if ((void *)handle == NULL) {
3057                 MMCAM_LOG_WARNING("handle is NULL");
3058                 return FALSE;
3059         }
3060
3061         hcamcorder = MMF_CAMCORDER(handle);
3062
3063         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
3064         if (!sc) {
3065                 MMCAM_LOG_WARNING("subcontext is NULL");
3066                 return FALSE;
3067         }
3068
3069         if (_mmcamcorder_is_encoded_preview_pixel_format(sc->info_image->preview_format) == false ||
3070                 hcamcorder->recreate_decoder == FALSE) {
3071                 MMCAM_LOG_INFO("skip this fuction - format %d, recreate decoder %d",
3072                         sc->info_image->preview_format, hcamcorder->recreate_decoder);
3073                 return TRUE;
3074         }
3075
3076         mm_camcorder_get_attributes(handle, NULL,
3077                 MMCAM_DISPLAY_SURFACE, &display_surface_type,
3078                 NULL);
3079
3080         if (display_surface_type == MM_DISPLAY_SURFACE_NULL) {
3081                 MMCAM_LOG_INFO("no need to control decoder element");
3082                 return TRUE;
3083         }
3084
3085         if (sc->element[_MMCAMCORDER_MAIN_PIPE].gst == NULL ||
3086             sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst == NULL) {
3087                 MMCAM_LOG_WARNING("main pipeline or decoder plugin is NULL");
3088                 return FALSE;
3089         }
3090
3091         MMCAM_LOG_INFO("start");
3092
3093         _mmcamcorder_conf_get_value_element_name(sc->VideodecoderElement, &videodecoder_name);
3094         if (videodecoder_name == NULL) {
3095                 MMCAM_LOG_ERROR("failed to get decoder element name from %p", sc->VideodecoderElement);
3096                 return FALSE;
3097         }
3098
3099         /* set state as NULL */
3100         ret = _mmcamcorder_gst_set_state(handle, sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst, GST_STATE_NULL);
3101         if (ret != MM_ERROR_NONE) {
3102                 MMCAM_LOG_ERROR("failed to set NULL to decoder");
3103                 return FALSE;
3104         }
3105
3106         /* remove decoder - pads will be unlinked automatically in remove function */
3107         if (!gst_bin_remove(GST_BIN(sc->element[_MMCAMCORDER_MAIN_PIPE].gst),
3108                             sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst)) {
3109                 MMCAM_LOG_ERROR("failed to remove decoder from pipeline");
3110                 return FALSE;
3111         }
3112
3113         /* check decoder element */
3114         if (sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst) {
3115                 MMCAM_LOG_INFO("decoder[%p] is still alive - ref count %d",
3116                         G_OBJECT(sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst),
3117                         ((GObject *)sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst)->ref_count);
3118         }
3119
3120 #ifdef _MMCAMCORDER_RM_SUPPORT
3121         if (sc->info_image->preview_format == MM_PIXEL_FORMAT_ENCODED_H264) {
3122                 if (hcamcorder->request_resources.category_id[0] == RM_CATEGORY_VIDEO_DECODER_SUB)
3123                         decoder_index = 1;
3124
3125                 snprintf(videodecoder_name_final, sizeof(videodecoder_name_final), "%s%d", videodecoder_name, decoder_index);
3126         } else if (sc->info_image->preview_format == MM_PIXEL_FORMAT_ENCODED_MJPEG) {
3127                 /* MJPEG */
3128                 if (hcamcorder->request_resources.category_id[0] == RM_CATEGORY_VIDEO_DECODER)
3129                         snprintf(videodecoder_name_final, sizeof(videodecoder_name_final), "%s", "omx_uhd_mjpegdec");
3130                 else
3131                         snprintf(videodecoder_name_final, sizeof(videodecoder_name_final), "%s", "omx_mjpegdec");
3132         } else {
3133 #endif /* _MMCAMCORDER_RM_SUPPORT */
3134                 snprintf(videodecoder_name_final, sizeof(videodecoder_name_final), "%s", videodecoder_name);
3135 #ifdef _MMCAMCORDER_RM_SUPPORT
3136         }
3137 #endif /* _MMCAMCORDER_RM_SUPPORT */
3138
3139         /* create decoder */
3140         sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst = gst_element_factory_make(videodecoder_name_final, "videosrc_decode");
3141         if (sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst == NULL) {
3142                 MMCAM_LOG_ERROR("Decoder[%s] creation fail", videodecoder_name_final);
3143                 return FALSE;
3144         }
3145
3146         _mmcamcorder_conf_set_value_element_property(sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst, sc->VideodecoderElement);
3147
3148         sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].id = _MMCAMCORDER_VIDEOSRC_DECODE;
3149         g_object_weak_ref(G_OBJECT(sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst),
3150                 (GWeakNotify)_mmcamcorder_element_release_noti, sc);
3151
3152         /* add to pipeline */
3153         if (!gst_bin_add(GST_BIN(sc->element[_MMCAMCORDER_MAIN_PIPE].gst),
3154                 sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst)) {
3155                 MMCAM_LOG_ERROR("failed to add decoder to pipeline");
3156                 gst_object_unref(sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst);
3157                 return FALSE;
3158         }
3159
3160         /* link */
3161         if (_MM_GST_ELEMENT_LINK(GST_ELEMENT(sc->element[_MMCAMCORDER_VIDEOSRC_QUE].gst),
3162                 GST_ELEMENT(sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst))) {
3163                 MMCAM_LOG_INFO("Link videosrc_queue to decoder OK");
3164         } else {
3165                 MMCAM_LOG_ERROR("Link videosrc_queue to decoder FAILED");
3166                 return FALSE;
3167         }
3168
3169         if (_MM_GST_ELEMENT_LINK(GST_ELEMENT(sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst),
3170                 GST_ELEMENT(sc->element[_MMCAMCORDER_VIDEOSINK_QUE].gst))) {
3171                 MMCAM_LOG_INFO("Link decoder to videosink_queue OK");
3172         } else {
3173                 MMCAM_LOG_ERROR("Link decoder to videosink_queue FAILED");
3174                 return FALSE;
3175         }
3176
3177         /* set state READY */
3178         ret = _mmcamcorder_gst_set_state(handle, sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst, GST_STATE_READY);
3179         if (ret != MM_ERROR_NONE) {
3180                 MMCAM_LOG_ERROR("failed to set READY to decoder");
3181                 return FALSE;
3182         }
3183
3184         MMCAM_LOG_INFO("done");
3185
3186         return TRUE;
3187 }
3188
3189 #ifdef _MMCAMCORDER_PRODUCT_TV
3190 static bool __mmcamcorder_find_max_resolution(MMHandleType handle, gint *max_width, gint *max_height)
3191 {
3192         _MMCamcorderSubContext *sc = NULL;
3193         mmf_camcorder_t *hcamcorder = NULL;
3194         int index = 0;
3195         const gchar *mime = NULL;
3196         GstPad *sinkpad;
3197         GstCaps *decsink_caps = NULL;
3198         GstStructure *decsink_struct = NULL;
3199
3200         mmf_return_val_if_fail(handle, false);
3201         mmf_return_val_if_fail(max_width, false);
3202         mmf_return_val_if_fail(max_height, false);
3203
3204         hcamcorder = MMF_CAMCORDER(handle);
3205         mmf_return_val_if_fail(hcamcorder, false);
3206
3207         sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
3208
3209         sinkpad = gst_element_get_static_pad(sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst, "sink");
3210         if (!sinkpad) {
3211                 MMCAM_LOG_ERROR("There are no decoder caps");
3212                 return false;
3213         }
3214
3215         decsink_caps = gst_pad_get_pad_template_caps(sinkpad);
3216         if (!decsink_caps) {
3217                 gst_object_unref(sinkpad);
3218                 MMCAM_LOG_ERROR("There is no decoder sink caps");
3219                 return false;
3220         }
3221
3222         for (index = 0; index < gst_caps_get_size(decsink_caps); index++) {
3223                 decsink_struct = gst_caps_get_structure(decsink_caps, index);
3224                 if (!decsink_struct) {
3225                         MMCAM_LOG_ERROR("There are no structure from caps");
3226                         gst_object_unref(decsink_caps);
3227                         gst_object_unref(sinkpad);
3228                         return false;
3229                 }
3230                 mime = gst_structure_get_name(decsink_struct);
3231                 if (!strcmp(mime, "video/x-h264")) {
3232                         MMCAM_LOG_INFO("h264 caps structure found");
3233                         if (gst_structure_has_field(decsink_struct, "maxwidth"))
3234                                 *max_width = gst_value_get_int_range_max(gst_structure_get_value(decsink_struct, "maxwidth"));
3235                         if (gst_structure_has_field(decsink_struct, "maxheight"))
3236                                 *max_height = gst_value_get_int_range_max(gst_structure_get_value(decsink_struct, "maxheight"));
3237                         break;
3238                 } else if (!strcmp(mime, "video/x-jpeg")) {
3239                         MMCAM_LOG_INFO("mjpeg caps structure found");
3240                         if (gst_structure_has_field(decsink_struct, "maxwidth"))
3241                                 *max_width = gst_value_get_int_range_max(gst_structure_get_value(decsink_struct, "maxwidth"));
3242                         if (gst_structure_has_field(decsink_struct, "maxheight"))
3243                                 *max_height = gst_value_get_int_range_max(gst_structure_get_value(decsink_struct, "maxheight"));
3244                         break;
3245                 }
3246         }
3247         MMCAM_LOG_INFO("maxwidth = %d , maxheight = %d", (int)*max_width, (int)*max_height);
3248         gst_object_unref(decsink_caps);
3249         gst_object_unref(sinkpad);
3250
3251         if (*max_width <= 0 || *max_height <= 0)
3252                 return false;
3253
3254         return true;
3255 }
3256 #endif /* _MMCAMCORDER_PRODUCT_TV */