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