Fix coverity issue - Logically dead code
[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
29 #include <sys/time.h>
30 #include <unistd.h>
31 #include <tbm_bufmgr.h>
32 #include <tbm_surface.h>
33 #include <tbm_surface_internal.h>
34
35 #include "mm_camcorder_internal.h"
36 #include "mm_camcorder_gstcommon.h"
37
38 /*-----------------------------------------------------------------------
39 |    GLOBAL VARIABLE DEFINITIONS for internal                           |
40 -----------------------------------------------------------------------*/
41 /* Table for compatibility between audio codec and file format */
42 static gboolean audiocodec_fileformat_compatibility_table[MM_AUDIO_CODEC_NUM][MM_FILE_FORMAT_NUM] = {
43                    /* 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*/
44 /*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},
45 /*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},
46 /*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},
47 /*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},
48 /*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},
49 /*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},
50 /*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},
51 /*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},
52 /*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},
53 /*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},
54 /*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},
55 /*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},
56 /*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},
57 /*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},
58 /*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},
59 /*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},
60 /*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},
61 /*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},
62 /*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},
63 /*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},
64 /*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},
65 /*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},
66 /*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},
67 /*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},
68 /*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},
69 /*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},
70 /*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},
71 /*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},
72 /*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},
73 /*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},
74 /*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},
75 /*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},
76 /*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},
77 /*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},
78 /*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},
79 /*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},
80 /*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},
81 /*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},
82 /*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},
83 /*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},
84 /*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},
85 /*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},
86 /*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},
87 /*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},
88 };
89
90 /* Table for compatibility between video codec and file format */
91 static gboolean videocodec_fileformat_compatibility_table[MM_VIDEO_CODEC_NUM][MM_FILE_FORMAT_NUM] = {
92                           /* 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*/
93 /*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},
94 /*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},
95 /*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},
96 /*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},
97 /*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},
98 /*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},
99 /*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},
100 /*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},
101 /*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},
102 /*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},
103 /*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},
104 /*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},
105 /*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},
106 /*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},
107 /*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},
108 /*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},
109 /*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},
110 /*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},
111 /*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},
112 /*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},
113 /*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},
114 /*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},
115 /*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},
116 /*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},
117 /*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},
118 /*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},
119 /*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},
120 /*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},
121 };
122
123
124 /*-----------------------------------------------------------------------
125 |    LOCAL VARIABLE DEFINITIONS for internal                            |
126 -----------------------------------------------------------------------*/
127 #define USE_AUDIO_CLOCK_TUNE
128 #define _MMCAMCORDER_WAIT_EOS_TIME                60.0     /* sec */
129 #define _MMCAMCORDER_CONVERT_OUTPUT_BUFFER_NUM    6
130 #define _MMCAMCORDER_NANOSEC_PER_1SEC             1000000000
131 #define _MMCAMCORDER_NANOSEC_PER_1MILISEC         1000
132
133
134 /*-----------------------------------------------------------------------
135 |    LOCAL FUNCTION PROTOTYPES:                                         |
136 -----------------------------------------------------------------------*/
137 /* STATIC INTERNAL FUNCTION */
138 /**
139  * These are video data probing functions.
140  * If there are pads which set probe callback by gst_pad_add_probe(),
141  * the functions will be called when data stream pass through the pad.
142  *
143  * @param[in]  pad             probing pad which calls this function.
144  * @param[in]  buffer          buffer which contains stream data.
145  * @param[in]  u_data          user data.
146  * @return     Refer #GstPadProbeReturn in gstpad.h
147  * @remarks
148  * @see        __mmcamcorder_create_preview_pipeline()
149  */
150 static GstPadProbeReturn __mmcamcorder_video_dataprobe_preview(GstPad *pad, GstPadProbeInfo *info, gpointer u_data);
151 static GstPadProbeReturn __mmcamcorder_video_dataprobe_record(GstPad *pad, GstPadProbeInfo *info, gpointer u_data);
152
153 static int __mmcamcorder_get_amrnb_bitrate_mode(int bitrate);
154 static guint32 _mmcamcorder_convert_fourcc_string_to_value(const gchar* format_name);
155 #ifdef _MMCAMCORDER_PRODUCT_TV
156 static bool __mmcamcorder_find_max_resolution(MMHandleType handle, gint *max_width, gint *max_height);
157 #endif /* _MMCAMCORDER_PRODUCT_TV */
158
159 static gboolean __mmcamcorder_set_stream_data_tbm(MMCamcorderVideoStreamDataType *stream, tbm_surface_info_s *ts_info);
160 static gboolean __mmcamcorder_set_stream_data_normal(MMCamcorderVideoStreamDataType *stream, GstBuffer *buffer, GstMapInfo *map_info);
161
162 /*=======================================================================================
163 |  FUNCTION DEFINITIONS                                                                 |
164 =======================================================================================*/
165 static gboolean __mmcamcorder_set_stream_data_normal(MMCamcorderVideoStreamDataType *stream, GstBuffer *buffer, GstMapInfo *map_info)
166 {
167         mmf_return_val_if_fail(buffer, FALSE);
168         mmf_return_val_if_fail(map_info, FALSE);
169         mmf_return_val_if_fail(stream, FALSE);
170
171         switch (stream->format) {
172         case MM_PIXEL_FORMAT_NV12: /* fall through */
173         case MM_PIXEL_FORMAT_NV21:
174                 stream->data_type = MM_CAM_STREAM_DATA_YUV420SP;
175                 stream->data.yuv420sp.y = map_info->data;
176                 stream->data.yuv420sp.length_y = stream->width * stream->height;
177                 stream->data.yuv420sp.uv = stream->data.yuv420sp.y + stream->data.yuv420sp.length_y;
178                 stream->data.yuv420sp.length_uv = stream->data.yuv420sp.length_y >> 1;
179                 stream->stride[0] = stream->width;
180                 stream->elevation[0] = stream->height;
181                 stream->stride[1] = stream->width;
182                 stream->elevation[1] = stream->height >> 1;
183                 stream->num_planes = 2;
184                 break;
185
186         case MM_PIXEL_FORMAT_I420:
187                 stream->data_type = MM_CAM_STREAM_DATA_YUV420P;
188                 stream->data.yuv420p.y = map_info->data;
189                 stream->data.yuv420p.length_y = stream->width * stream->height;
190                 stream->data.yuv420p.u = stream->data.yuv420p.y + stream->data.yuv420p.length_y;
191                 stream->data.yuv420p.length_u = stream->data.yuv420p.length_y >> 2;
192                 stream->data.yuv420p.v = stream->data.yuv420p.u + stream->data.yuv420p.length_u;
193                 stream->data.yuv420p.length_v = stream->data.yuv420p.length_u;
194                 stream->stride[0] = stream->width;
195                 stream->elevation[0] = stream->height;
196                 stream->stride[1] = stream->stride[2] = stream->width >> 1;
197                 stream->elevation[1] = stream->elevation[2] = stream->height >> 1;
198                 stream->num_planes = 3;
199                 break;
200
201         case MM_PIXEL_FORMAT_422P:
202                 stream->data_type = MM_CAM_STREAM_DATA_YUV422P;
203                 stream->data.yuv422p.y = map_info->data;
204                 stream->data.yuv422p.length_y = stream->width * stream->height;
205                 stream->data.yuv422p.u = stream->data.yuv422p.y + stream->data.yuv422p.length_y;
206                 stream->data.yuv422p.length_u = stream->data.yuv422p.length_y >> 1;
207                 stream->data.yuv422p.v = stream->data.yuv422p.u + stream->data.yuv422p.length_u;
208                 stream->data.yuv422p.length_v = stream->data.yuv422p.length_u;
209                 stream->stride[0] = stream->width;
210                 stream->elevation[0] = stream->height;
211                 stream->stride[1] = stream->stride[2] = stream->width;
212                 stream->elevation[1] = stream->elevation[2] = stream->height >> 1;
213                 stream->num_planes = 3;
214                 break;
215
216         case MM_PIXEL_FORMAT_YUYV: /* fall through */
217         case MM_PIXEL_FORMAT_UYVY: /* fall through */
218         case MM_PIXEL_FORMAT_ITLV_JPEG_UYVY:
219                 stream->data_type = MM_CAM_STREAM_DATA_YUV422;
220                 stream->data.yuv422.yuv = map_info->data;
221                 stream->data.yuv422.length_yuv = stream->length_total;
222                 stream->stride[0] = stream->width << 1;
223                 stream->elevation[0] = stream->height;
224                 stream->num_planes = 1;
225                 break;
226
227         case MM_PIXEL_FORMAT_ENCODED_H264:  /* fall through */
228         case MM_PIXEL_FORMAT_ENCODED_MJPEG: /* fall through */
229         case MM_PIXEL_FORMAT_ENCODED_VP8:   /* fall through */
230         case MM_PIXEL_FORMAT_ENCODED_VP9:   /* fall through */
231                 stream->data_type = MM_CAM_STREAM_DATA_ENCODED;
232                 stream->data.encoded.data = map_info->data;
233                 stream->data.encoded.length_data = stream->length_total;
234                 stream->data.encoded.is_delta_frame = GST_BUFFER_FLAG_IS_SET(buffer, GST_BUFFER_FLAG_DELTA_UNIT);
235                 stream->num_planes = 1;
236                 break;
237
238         case MM_PIXEL_FORMAT_INVZ:
239                 stream->data_type = MM_CAM_STREAM_DATA_DEPTH;
240                 stream->data.depth.data = map_info->data;
241                 stream->data.depth.length_data = stream->length_total;
242                 stream->stride[0] = stream->width << 1;
243                 stream->elevation[0] = stream->height;
244                 stream->num_planes = 1;
245                 break;
246
247         case MM_PIXEL_FORMAT_RGBA: /* fall through */
248         case MM_PIXEL_FORMAT_ARGB:
249                 stream->data_type = MM_CAM_STREAM_DATA_RGB;
250                 stream->data.rgb.data = map_info->data;
251                 stream->data.rgb.length_data = stream->length_total;
252                 stream->stride[0] = stream->width << 2;
253                 stream->elevation[0] = stream->height;
254                 stream->num_planes = 1;
255                 break;
256
257         default:
258                 MMCAM_LOG_ERROR("unsupported format[%d]", stream->format);
259                 return FALSE;
260         }
261
262         return TRUE;
263 }
264
265
266 static gboolean __mmcamcorder_set_stream_data_tbm(MMCamcorderVideoStreamDataType *stream, tbm_surface_info_s *ts_info)
267 {
268         mmf_return_val_if_fail(ts_info, FALSE);
269         mmf_return_val_if_fail(stream, FALSE);
270
271         switch (stream->format) {
272         case MM_PIXEL_FORMAT_NV12: /* fall through */
273         case MM_PIXEL_FORMAT_NV21:
274                 stream->data_type = MM_CAM_STREAM_DATA_YUV420SP;
275                 stream->num_planes = 2;
276                 stream->data.yuv420sp.y = ts_info->planes[0].ptr;
277                 stream->data.yuv420sp.length_y = ts_info->planes[0].size;
278                 stream->data.yuv420sp.uv = ts_info->planes[1].ptr;
279                 stream->data.yuv420sp.length_uv = ts_info->planes[1].size;
280                 break;
281
282         case MM_PIXEL_FORMAT_I420:
283                 stream->data_type = MM_CAM_STREAM_DATA_YUV420P;
284                 stream->num_planes = 3;
285                 stream->data.yuv420p.y = ts_info->planes[0].ptr;
286                 stream->data.yuv420p.length_y = ts_info->planes[0].size;
287                 stream->data.yuv420p.u = ts_info->planes[1].ptr;
288                 stream->data.yuv420p.length_u = ts_info->planes[1].size;
289                 stream->data.yuv420p.v = ts_info->planes[2].ptr;
290                 stream->data.yuv420p.length_v = ts_info->planes[2].size;
291                 break;
292
293         default:
294                 MMCAM_LOG_ERROR("unsupported format[%d]", stream->format);
295                 return FALSE;
296         }
297
298         return TRUE;
299 }
300
301
302 gboolean _mmcamcorder_invoke_video_stream_cb(MMHandleType handle, GstSample *sample, gboolean is_preview, int stream_id)
303 {
304         int i = 0;
305         int num_bos = 0;
306         gboolean ret_cb = TRUE;
307         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
308         _MMCamcorderSubContext *sc = NULL;
309         MMCamcorderVideoStreamDataType stream;
310
311         tbm_surface_h t_surface = NULL;
312         tbm_surface_info_s ts_info;
313
314         GstBuffer *buffer = NULL;
315         GstMemory *memory = NULL;
316         GstMapInfo map_info;
317         GstCaps *caps = NULL;
318         GstStructure *structure = NULL;
319
320         mmf_return_val_if_fail(hcamcorder, FALSE);
321
322         buffer = gst_sample_get_buffer(sample);
323         mmf_return_val_if_fail(buffer, FALSE);
324         mmf_return_val_if_fail(gst_buffer_n_memory(buffer), FALSE);
325
326         sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
327         mmf_return_val_if_fail(sc, FALSE);
328
329         /* clear data structure */
330         memset(&map_info, 0x0, sizeof(GstMapInfo));
331         memset(&stream, 0x0, sizeof(MMCamcorderVideoStreamDataType));
332
333         caps = gst_sample_get_caps(sample);
334         mmf_return_val_if_fail(caps, FALSE);
335
336         structure = gst_caps_get_structure(caps, 0);
337         mmf_return_val_if_fail(structure, FALSE);
338
339         /* set format and resolution */
340         gst_structure_get_int(structure, "width", &stream.width);
341         gst_structure_get_int(structure, "height", &stream.height);
342         stream.format = _mmcamcorder_get_pixel_format(caps);
343
344         if (_mmcamcorder_is_encoded_preview_pixel_format(stream.format)) {
345                 memory = gst_buffer_get_all_memory(buffer);
346                 stream.internal_buffer = buffer;
347         } else {
348                 memory = gst_buffer_peek_memory(buffer, 0);
349         }
350         if (!memory) {
351                 MMCAM_LOG_ERROR("GstMemory get failed from buffer %p", buffer);
352                 return FALSE;
353         }
354
355         /* set zero-copy related information */
356         if (hcamcorder->use_zero_copy_format) {
357                 t_surface = (tbm_surface_h)gst_tizen_memory_get_surface(memory);
358
359                 if (tbm_surface_get_info(t_surface, &ts_info) != TBM_SURFACE_ERROR_NONE) {
360                         MMCAM_LOG_ERROR("failed to get tbm surface[%p] info", t_surface);
361                         goto _INVOKE_VIDEO_STREAM_CB_DONE;
362                 }
363
364                 /* set bo, stride and elevation */
365                 num_bos = gst_tizen_memory_get_num_bos(memory);
366                 for (i = 0 ; i < num_bos ; i++)
367                         stream.bo[i] = gst_tizen_memory_get_bos(memory, i);
368
369                 for (i = 0 ; i < ts_info.num_planes ; i++) {
370                         stream.stride[i] = ts_info.planes[i].stride;
371                         stream.elevation[i] = ts_info.planes[i].size / ts_info.planes[i].stride;
372                         MMCAM_LOG_VERBOSE("    plane[%d] %dx%d", i, stream.stride[i], stream.elevation[i]);
373                 }
374
375                 stream.length_total = ts_info.size;
376                 stream.internal_buffer = buffer;
377
378                 if (!__mmcamcorder_set_stream_data_tbm(&stream, &ts_info))
379                         goto _INVOKE_VIDEO_STREAM_CB_DONE;
380         } else {
381                 stream.length_total = gst_memory_get_sizes(memory, NULL, NULL);
382
383                 if (!gst_memory_map(memory, &map_info, GST_MAP_READWRITE) ||
384                         !__mmcamcorder_set_stream_data_normal(&stream, buffer, &map_info))
385                         goto _INVOKE_VIDEO_STREAM_CB_DONE;
386         }
387
388         MMCAM_LOG_DEBUG("VideoStreamData : format[%d], resolution[%dx%d], stream_id[%d]",
389                 stream.format, stream.width, stream.height, stream_id);
390
391         stream.timestamp = (unsigned int)(GST_BUFFER_PTS(buffer) / 1000000); /* nano sec -> milli sec */
392         stream.extra_stream_id = stream_id;
393
394         /* invoke application callback */
395         if (is_preview) {
396                 _MMCAMCORDER_LOCK_VSTREAM_CALLBACK(hcamcorder);
397                 if (hcamcorder->vstream_cb)
398                         hcamcorder->vstream_cb(&stream, hcamcorder->vstream_cb_param);
399                 _MMCAMCORDER_UNLOCK_VSTREAM_CALLBACK(hcamcorder);
400         } else {
401                 _MMCAMCORDER_LOCK_VEDECISION_CALLBACK(hcamcorder);
402                 if (hcamcorder->vedecision_cb)
403                         ret_cb = hcamcorder->vedecision_cb(&stream, hcamcorder->vedecision_cb_param);
404                 _MMCAMCORDER_UNLOCK_VEDECISION_CALLBACK(hcamcorder);
405         }
406
407 _INVOKE_VIDEO_STREAM_CB_DONE:
408         for (i = 0 ; i < TBM_SURF_PLANE_MAX && stream.bo[i] ; i++) {
409                 tbm_bo_map(stream.bo[i], TBM_DEVICE_CPU, TBM_OPTION_READ|TBM_OPTION_WRITE);
410                 tbm_bo_unmap(stream.bo[i]);
411         }
412
413         if (map_info.data)
414                 gst_memory_unmap(memory, &map_info);
415
416         if (_mmcamcorder_is_encoded_preview_pixel_format(stream.format))
417                 gst_memory_unref(memory);
418
419         return ret_cb;
420 }
421
422
423 int _mmcamcorder_create_preview_elements(MMHandleType handle)
424 {
425         int err = MM_ERROR_NONE;
426         int i = 0;
427         int camera_rotate = 0;
428         int camera_flip = 0;
429         int fps = 0;
430         int codectype = 0;
431         int capture_width = 0;
432         int capture_height = 0;
433         int capture_jpg_quality = 100;
434         int video_stabilization = 0;
435         int anti_shake = 0;
436         int display_surface_type = MM_DISPLAY_SURFACE_NULL;
437         const char *videosrc_name = NULL;
438         const char *videosink_name = NULL;
439         const char *videoconvert_name = NULL;
440         const char *videodecoder_name = NULL;
441         const char *decoder_conf_name = NULL;
442         char videodecoder_name_final[32] = {'\0',};
443         char *err_name = NULL;
444         char *socket_path = NULL;
445         int socket_path_len;
446 #ifdef _MMCAMCORDER_RM_SUPPORT
447         int decoder_index = 0;
448 #endif /* _MMCAMCORDER_RM_SUPPORT */
449         GstElement *sink_element = NULL;
450         GstCameraControl *control = NULL;
451         int sink_element_size = 0;
452         int *fds = NULL;
453         int fd_number = 0;
454         int extra_preview_enable = 0;
455
456         GList *element_list = NULL;
457
458         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
459         _MMCamcorderSubContext *sc = NULL;
460         type_element *VideosrcElement = NULL;
461         type_int_array *input_index = NULL;
462
463         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
464
465         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
466         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
467         mmf_return_val_if_fail(sc->element, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
468
469         MMCAM_LOG_INFO("");
470
471         /* Check existence */
472         for (i = _MMCAMCORDER_VIDEOSRC_SRC ; i <= _MMCAMCORDER_VIDEOSINK_SINK ; i++) {
473                 if (sc->element[i].gst) {
474                         if (((GObject *)sc->element[i].gst)->ref_count > 0)
475                                 gst_object_unref(sc->element[i].gst);
476
477                         MMCAM_LOG_INFO("element[index:%d] is Already existed.", i);
478                 }
479         }
480
481         /* Get video device index info */
482         _mmcamcorder_conf_get_value_int_array(hcamcorder->conf_ctrl,
483                                                                                   CONFIGURE_CATEGORY_CTRL_CAMERA,
484                                                                                   "InputIndex",
485                                                                                   &input_index);
486         if (input_index == NULL) {
487                 MMCAM_LOG_ERROR("Failed to get input_index");
488                 return MM_ERROR_CAMCORDER_CREATE_CONFIGURE;
489         }
490
491         err = mm_camcorder_get_attributes(handle, &err_name,
492                 MMCAM_CAMERA_FORMAT, &sc->info_image->preview_format,
493                 MMCAM_CAMERA_FPS, &fps,
494                 MMCAM_CAMERA_ROTATION, &camera_rotate,
495                 MMCAM_CAMERA_FLIP, &camera_flip,
496                 MMCAM_CAMERA_VIDEO_STABILIZATION, &video_stabilization,
497                 MMCAM_CAMERA_ANTI_HANDSHAKE, &anti_shake,
498                 MMCAM_CAPTURE_WIDTH, &capture_width,
499                 MMCAM_CAPTURE_HEIGHT, &capture_height,
500                 MMCAM_CAMERA_HDR_CAPTURE, &sc->info_image->hdr_capture_mode,
501                 MMCAM_IMAGE_ENCODER, &codectype,
502                 MMCAM_IMAGE_ENCODER_QUALITY, &capture_jpg_quality,
503                 MMCAM_DISPLAY_SOCKET_PATH, &socket_path, &socket_path_len,
504                 MMCAM_DISPLAY_SURFACE, &display_surface_type,
505                 MMCAM_EXTRA_PREVIEW_ENABLE, &extra_preview_enable,
506                 NULL);
507         if (err != MM_ERROR_NONE) {
508                 MMCAM_LOG_WARNING("Get attrs fail. (%s:%x)", err_name, err);
509                 SAFE_FREE(err_name);
510                 return err;
511         }
512
513         if (hcamcorder->support_user_buffer) {
514                 err = mm_camcorder_get_attributes(handle, NULL,
515                         MMCAM_USER_BUFFER_FD, &fds, &fd_number,
516                         NULL);
517                 if (err != MM_ERROR_NONE || fd_number < 1) {
518                         MMCAM_LOG_ERROR("get user buffer fd failed 0x%x, number %d", err, fd_number);
519                         return err;
520                 }
521
522                 /*
523                 for (i = 0 ; i < fd_number ; i++)
524                         MMCAM_LOG_INFO("fds[%d] %d", i, fds[i]);
525                 */
526         }
527
528         /* Get fourcc from picture format */
529         sc->fourcc = _mmcamcorder_get_fourcc(sc->info_image->preview_format, codectype, hcamcorder->use_zero_copy_format);
530
531         /* Get videosrc element and its name from configure */
532         _mmcamcorder_conf_get_element(handle, hcamcorder->conf_main,
533                 CONFIGURE_CATEGORY_MAIN_VIDEO_INPUT,
534                 "VideosrcElement",
535                 &VideosrcElement);
536         _mmcamcorder_conf_get_value_element_name(VideosrcElement, &videosrc_name);
537
538         /**
539          * Create child element
540          */
541         _MMCAMCORDER_ELEMENT_MAKE(sc, sc->element, _MMCAMCORDER_VIDEOSRC_SRC, videosrc_name, "videosrc_src", element_list, err);
542
543         _MMCAMCORDER_ELEMENT_MAKE(sc, sc->element, _MMCAMCORDER_VIDEOSRC_FILT, "capsfilter", "videosrc_filter", element_list, err);
544
545         /**
546          * This is for "tizencamerasrc" element only.
547          * The camera HAL library will be loaded when "hal-name" property is set.
548          * The default HAL library (libtizen-camera.so) will be loaded if 'hal-name' property is set to NULL.
549          */
550         MMCAMCORDER_G_OBJECT_SET_POINTER(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "hal-name", hcamcorder->network_hal_name);
551
552         /* set camera properties */
553         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "high-speed-fps", 0);
554         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "capture-width", capture_width);
555         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "capture-height", capture_height);
556         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "capture-jpg-quality", capture_jpg_quality);
557         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "hdr-capture", sc->info_image->hdr_capture_mode);
558         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "extra-preview", extra_preview_enable);
559
560         _MMCAMCORDER_ELEMENT_MAKE(sc, sc->element, _MMCAMCORDER_VIDEOSRC_QUE, "queue", "videosrc_queue", element_list, err);
561
562         /* set camera flip */
563         _mmcamcorder_set_videosrc_flip(handle, camera_flip);
564
565         /* set video stabilization mode */
566         _mmcamcorder_set_videosrc_stabilization(handle, video_stabilization);
567
568         /* set anti handshake mode */
569         _mmcamcorder_set_videosrc_anti_shake(handle, anti_shake);
570
571         if (sc->is_modified_rate)
572                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "high-speed-fps", fps);
573
574         /* Set basic information of videosrc element */
575         _mmcamcorder_conf_set_value_element_property(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, VideosrcElement);
576
577         /* Set video device index */
578         if (hcamcorder->is_network) {
579                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "camera-id", hcamcorder->device_type);
580         } else {
581 #ifdef _MMCAMCORDER_CAMERA_CONF_MGR_SUPPORT
582                 MMCAMCORDER_G_OBJECT_SET_POINTER(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "device-name", hcamcorder->conf_device_info.node);
583 #else
584                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "camera-id", input_index->default_value);
585 #endif
586         }
587
588         /* set user buffer fd to videosrc element */
589         if (hcamcorder->support_user_buffer) {
590                 control = GST_CAMERA_CONTROL(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst);
591                 if (!gst_camera_control_set_user_buffer_fd(control, fds, fd_number)) {
592                         MMCAM_LOG_ERROR("set user buffer fd failed");
593                         goto pipeline_creation_error;
594                 }
595         }
596
597         if (_mmcamcorder_is_encoded_preview_pixel_format(sc->info_image->preview_format)) {
598                 /* get recreate_decoder flag */
599                 _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_main,
600                         CONFIGURE_CATEGORY_MAIN_VIDEO_OUTPUT,
601                         "RecreateDecoder",
602                         &hcamcorder->recreate_decoder);
603         }
604
605         if (_mmcamcorder_is_encoded_preview_pixel_format(sc->info_image->preview_format)) {
606                 int preview_bitrate = 0;
607                 int gop_interval = 0;
608                 mm_camcorder_get_attributes(handle, NULL,
609                         MMCAM_ENCODED_PREVIEW_BITRATE, &preview_bitrate,
610                         MMCAM_ENCODED_PREVIEW_GOP_INTERVAL, &gop_interval,
611                         NULL);
612
613                 /* set encoded preview bitrate */
614                 if (!_mmcamcorder_set_encoded_preview_bitrate(handle, preview_bitrate))
615                         MMCAM_LOG_WARNING("_mmcamcorder_set_encoded_preview_bitrate failed");
616
617                 /* set encoded preview iframe interval */
618                 if (sc->info_image->preview_format == MM_PIXEL_FORMAT_ENCODED_H264) {
619                         if (!_mmcamcorder_set_encoded_preview_gop_interval(handle, gop_interval))
620                                 MMCAM_LOG_WARNING("_mmcamcorder_set_encoded_preview_gop_interval failed");
621                 }
622         }
623
624         if (display_surface_type != MM_DISPLAY_SURFACE_NULL &&
625                 _mmcamcorder_is_encoded_preview_pixel_format(sc->info_image->preview_format)) {
626                 switch (sc->info_image->preview_format) {
627                 case MM_PIXEL_FORMAT_ENCODED_H264:
628                         decoder_conf_name = "VideodecoderElementH264";
629                         break;
630                 case MM_PIXEL_FORMAT_ENCODED_MJPEG:
631                         decoder_conf_name = "VideodecoderElementMJPEG";
632                         break;
633                 case MM_PIXEL_FORMAT_ENCODED_VP8:
634                         decoder_conf_name = "VideodecoderElementVP8";
635                         break;
636                 case MM_PIXEL_FORMAT_ENCODED_VP9:
637                         decoder_conf_name = "VideodecoderElementVP9";
638                         break;
639                 default:
640                         MMCAM_LOG_ERROR("invalid format[%d]", sc->info_image->preview_format);
641                         goto pipeline_creation_error;
642                 }
643
644                 /* get video decoder element and name */
645                 _mmcamcorder_conf_get_element(handle, hcamcorder->conf_main,
646                         CONFIGURE_CATEGORY_MAIN_VIDEO_OUTPUT,
647                         decoder_conf_name,
648                         &sc->VideodecoderElement);
649
650                 _mmcamcorder_conf_get_value_element_name(sc->VideodecoderElement,
651                         &videodecoder_name);
652
653                 if (!videodecoder_name) {
654                         MMCAM_LOG_ERROR("failed to get video decoder[%d,%s]", sc->info_image->preview_format, decoder_conf_name);
655                         goto pipeline_creation_error;
656                 }
657
658 #ifdef _MMCAMCORDER_RM_SUPPORT
659                 MMCAM_LOG_INFO("decoder name from config[%s]", videodecoder_name);
660                 if (sc->info_image->preview_format == MM_PIXEL_FORMAT_ENCODED_H264) {
661                         if (hcamcorder->request_resources.category_id[0] == RM_CATEGORY_VIDEO_DECODER_SUB)
662                                 decoder_index = 1;
663                         snprintf(videodecoder_name_final, sizeof(videodecoder_name_final), "%s%d", videodecoder_name, decoder_index);
664                 } else if (sc->info_image->preview_format == MM_PIXEL_FORMAT_ENCODED_MJPEG) {
665                         if (hcamcorder->request_resources.category_id[0] == RM_CATEGORY_VIDEO_DECODER)
666                                 snprintf(videodecoder_name_final, sizeof(videodecoder_name_final), "%s", "omx_uhd_mjpegdec");
667                         else
668                                 snprintf(videodecoder_name_final, sizeof(videodecoder_name_final), "%s", "omx_mjpegdec");
669                 } else {
670 #endif /* _MMCAMCORDER_RM_SUPPORT */
671                         snprintf(videodecoder_name_final, sizeof(videodecoder_name_final), "%s", videodecoder_name);
672 #ifdef _MMCAMCORDER_RM_SUPPORT
673                 }
674 #endif /* _MMCAMCORDER_RM_SUPPORT */
675
676                 _MMCAMCORDER_ELEMENT_MAKE(sc, sc->element, _MMCAMCORDER_VIDEOSRC_DECODE, videodecoder_name_final, "videosrc_decode", element_list, err);
677
678                 _mmcamcorder_conf_set_value_element_property(sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst, sc->VideodecoderElement);
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 (videoscale_caps)
1616                 gst_caps_unref(videoscale_caps);
1617
1618         return err;
1619 }
1620
1621
1622 int _mmcamcorder_create_preview_pipeline(MMHandleType handle)
1623 {
1624         int err = MM_ERROR_NONE;
1625
1626         GstPad *srcpad = NULL;
1627         GstBus *bus = NULL;
1628
1629         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1630         _MMCamcorderSubContext *sc = NULL;
1631
1632         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1633
1634         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
1635         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1636         mmf_return_val_if_fail(sc->element, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1637
1638         MMCAM_LOG_INFO("");
1639
1640         /** Create gstreamer element **/
1641         /* Main pipeline */
1642         _MMCAMCORDER_PIPELINE_MAKE(sc, sc->element, _MMCAMCORDER_MAIN_PIPE, "camera_pipeline", err);
1643
1644         /* Sub pipeline */
1645         err = _mmcamcorder_create_preview_elements((MMHandleType)hcamcorder);
1646         if (err != MM_ERROR_NONE)
1647                 goto pipeline_creation_error;
1648
1649         /* Set data probe function */
1650         if (sc->element[_MMCAMCORDER_VIDEOSRC_QUE].gst) {
1651                 MMCAM_LOG_INFO("add video dataprobe to videosrc queue");
1652                 srcpad = gst_element_get_static_pad(sc->element[_MMCAMCORDER_VIDEOSRC_QUE].gst, "src");
1653         } else {
1654                 MMCAM_LOG_ERROR("there is no queue plugin");
1655                 goto pipeline_creation_error;
1656         }
1657
1658         if (srcpad) {
1659                 MMCAMCORDER_ADD_BUFFER_PROBE(srcpad, _MMCAMCORDER_HANDLER_PREVIEW,
1660                         __mmcamcorder_video_dataprobe_preview, hcamcorder);
1661
1662                 gst_object_unref(srcpad);
1663                 srcpad = NULL;
1664         } else {
1665                 MMCAM_LOG_ERROR("failed to get srcpad");
1666                 goto pipeline_creation_error;
1667         }
1668
1669         /* set dataprobe for video recording if it does not support dual stream. */
1670         if (sc->info_video->record_dual_stream == FALSE) {
1671                 if (_mmcamcorder_is_encoded_preview_pixel_format(sc->info_image->preview_format))
1672                         srcpad = gst_element_get_static_pad(sc->element[_MMCAMCORDER_VIDEOSRC_QUE].gst, "src");
1673                 else
1674                         srcpad = gst_element_get_static_pad(sc->element[_MMCAMCORDER_VIDEOSINK_QUE].gst, "src");
1675
1676                 MMCAMCORDER_ADD_BUFFER_PROBE(srcpad, _MMCAMCORDER_HANDLER_PREVIEW,
1677                         __mmcamcorder_video_dataprobe_record, hcamcorder);
1678                 gst_object_unref(srcpad);
1679                 srcpad = NULL;
1680         }
1681
1682         bus = gst_pipeline_get_bus(GST_PIPELINE(sc->element[_MMCAMCORDER_MAIN_PIPE].gst));
1683
1684         /* set sync handler */
1685         gst_bus_set_sync_handler(bus, _mmcamcorder_pipeline_bus_sync_callback, (gpointer)hcamcorder, NULL);
1686
1687         gst_object_unref(bus);
1688         bus = NULL;
1689
1690         return MM_ERROR_NONE;
1691
1692 pipeline_creation_error:
1693         _MMCAMCORDER_ELEMENT_REMOVE(sc->element, _MMCAMCORDER_MAIN_PIPE);
1694         return err;
1695 }
1696
1697
1698 void _mmcamcorder_ready_to_encode_callback(GstElement *element, guint size, gpointer handle)
1699 {
1700         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1701         _MMCamcorderSubContext *sc = NULL;
1702
1703         /*MMCAM_LOG_INFO("start");*/
1704
1705         mmf_return_if_fail(hcamcorder);
1706         mmf_return_if_fail(hcamcorder->sub_context);
1707         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
1708
1709         /* set flag */
1710         if (sc->info_video->push_encoding_buffer == PUSH_ENCODING_BUFFER_INIT) {
1711                 sc->info_video->push_encoding_buffer = PUSH_ENCODING_BUFFER_RUN;
1712                 MMCAM_LOG_WARNING("set push_encoding_buffer RUN");
1713         }
1714 }
1715
1716
1717 int _mmcamcorder_videosink_window_set(MMHandleType handle, type_element* VideosinkElement)
1718 {
1719         int err = MM_ERROR_NONE;
1720         int size = 0;
1721         int rect_x = 0;
1722         int rect_y = 0;
1723         int rect_width = 0;
1724         int rect_height = 0;
1725         int visible = 0;
1726         int rotation = MM_DISPLAY_ROTATION_NONE;
1727         int flip = MM_FLIP_NONE;
1728         int display_mode = MM_DISPLAY_MODE_DEFAULT;
1729         int display_geometry_method = MM_DISPLAY_METHOD_LETTER_BOX;
1730         int origin_size = 0;
1731         int zoom_attr = 0;
1732         int zoom_level = 0;
1733         int do_scaling = FALSE;
1734         int *dp_handle = NULL;
1735         MMCamWindowInfo *window_info = NULL;
1736         gulong xid;
1737         char *err_name = NULL;
1738         const char *videosink_name = NULL;
1739
1740         GstElement *vsink = NULL;
1741
1742         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1743         _MMCamcorderSubContext *sc = NULL;
1744
1745         MMCAM_LOG_INFO("");
1746
1747         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1748
1749         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
1750         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1751         mmf_return_val_if_fail(sc->element, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1752         mmf_return_val_if_fail(sc->element[_MMCAMCORDER_VIDEOSINK_SINK].gst, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1753
1754         vsink = sc->element[_MMCAMCORDER_VIDEOSINK_SINK].gst;
1755
1756         /* Get video display information */
1757         err = mm_camcorder_get_attributes(handle, &err_name,
1758                 MMCAM_DISPLAY_RECT_X, &rect_x,
1759                 MMCAM_DISPLAY_RECT_Y, &rect_y,
1760                 MMCAM_DISPLAY_RECT_WIDTH, &rect_width,
1761                 MMCAM_DISPLAY_RECT_HEIGHT, &rect_height,
1762                 MMCAM_DISPLAY_ROTATION, &rotation,
1763                 MMCAM_DISPLAY_FLIP, &flip,
1764                 MMCAM_DISPLAY_VISIBLE, &visible,
1765                 MMCAM_DISPLAY_HANDLE, (void **)&dp_handle, &size,
1766                 MMCAM_DISPLAY_MODE, &display_mode,
1767                 MMCAM_DISPLAY_GEOMETRY_METHOD, &display_geometry_method,
1768                 MMCAM_DISPLAY_SCALE, &zoom_attr,
1769                 MMCAM_DISPLAY_EVAS_DO_SCALING, &do_scaling,
1770                 NULL);
1771         if (err != MM_ERROR_NONE) {
1772                 if (err_name) {
1773                         MMCAM_LOG_ERROR("failed to get attributes [%s][0x%x]", err_name, err);
1774                         SAFE_FREE(err_name);
1775                 } else {
1776                         MMCAM_LOG_ERROR("failed to get attributes [0x%x]", err);
1777                 }
1778
1779                 return err;
1780         }
1781
1782         _mmcamcorder_conf_get_value_element_name(VideosinkElement, &videosink_name);
1783
1784         if (!videosink_name) {
1785                 MMCAM_LOG_ERROR("failed to get videosink name");
1786                 return MM_ERROR_CAMCORDER_INTERNAL;
1787         }
1788
1789         MMCAM_LOG_INFO("(dp_handle=%p, size=%d)", dp_handle, size);
1790
1791         /* Set display handle */
1792         if (!strcmp(videosink_name, "xvimagesink") || !strcmp(videosink_name, "ximagesink")) {
1793                 if (dp_handle) {
1794                         xid = *dp_handle;
1795                         MMCAM_LOG_INFO("xid = %lu )", xid);
1796                         gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(vsink), xid);
1797                 } else {
1798                         MMCAM_LOG_WARNING("Handle is NULL. Set xid as 0.. but, it's not recommended.");
1799                         gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(vsink), 0);
1800                 }
1801         } else if (!strcmp(videosink_name, "evasimagesink") || !strcmp(videosink_name, "evaspixmapsink")) {
1802                 MMCAM_LOG_INFO("videosink : %s, handle : %p", videosink_name, dp_handle);
1803
1804                 if (dp_handle) {
1805                         MMCAMCORDER_G_OBJECT_SET_POINTER(vsink, "evas-object", dp_handle);
1806                         MMCAMCORDER_G_OBJECT_SET(vsink, "origin-size", !do_scaling);
1807                 } else {
1808                         MMCAM_LOG_ERROR("display handle(eavs object) is NULL");
1809                         return MM_ERROR_CAMCORDER_INVALID_ARGUMENT;
1810                 }
1811         } else if (!strcmp(videosink_name, "tizenwlsink")) {
1812                 if (dp_handle) {
1813                         window_info = (MMCamWindowInfo *)dp_handle;
1814                         MMCAM_LOG_INFO("wayland global surface id : %d", window_info->surface_id);
1815                         gst_video_overlay_set_wl_window_wl_surface_id(GST_VIDEO_OVERLAY(vsink), window_info->surface_id);
1816                 } else {
1817                         MMCAM_LOG_WARNING("Handle is NULL. skip setting.");
1818                 }
1819         } else if (!strcmp(videosink_name, "directvideosink")) {
1820 #ifdef _MMCAMCORDER_RM_SUPPORT
1821                 /* set the videosink using the virtual device id instead of the real id (main=0, sub=1) */
1822                 MMCAM_LOG_INFO("device-scaler : %d", hcamcorder->returned_devices.device_id[1]);
1823                 MMCAMCORDER_G_OBJECT_SET(vsink, "device-scaler", hcamcorder->returned_devices.device_id[1]);
1824 #endif /* _MMCAMCORDER_RM_SUPPORT */
1825                 if (dp_handle) {
1826                         window_info = (MMCamWindowInfo *)dp_handle;
1827                         MMCAM_LOG_INFO("wayland global surface id : %d, x,y,w,h (%d,%d,%d,%d)",
1828                                 window_info->surface_id,
1829                                 window_info->rect.x,
1830                                 window_info->rect.y,
1831                                 window_info->rect.width,
1832                                 window_info->rect.height);
1833                         gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(vsink), (guintptr)window_info->surface_id);
1834                         gst_video_overlay_set_render_rectangle(GST_VIDEO_OVERLAY(vsink),
1835                                 window_info->rect.x,
1836                                 window_info->rect.y,
1837                                 window_info->rect.width,
1838                                 window_info->rect.height);
1839                 } else {
1840                         MMCAM_LOG_WARNING("dp_handle is null");
1841                 }
1842         } else {
1843                 MMCAM_LOG_WARNING("Who are you?? (Videosink: %s)", videosink_name);
1844         }
1845
1846         MMCAM_LOG_INFO("%s set: display_geometry_method[%d],origin-size[%d],visible[%d],rotate[%d],flip[%d]",
1847                 videosink_name, display_geometry_method, origin_size, visible, rotation, flip);
1848
1849         /* Set attribute */
1850         if (!strcmp(videosink_name, "xvimagesink") || !strcmp(videosink_name, "tizenwlsink") ||
1851             !strcmp(videosink_name, "evaspixmapsink") || !strcmp(videosink_name, "directvideosink")) {
1852                 /* set rotation */
1853                 MMCAMCORDER_G_OBJECT_SET(vsink, "rotate", rotation);
1854
1855                 /* set flip */
1856                 MMCAMCORDER_G_OBJECT_SET(vsink, "flip", flip);
1857
1858                 switch (zoom_attr) {
1859                 case MM_DISPLAY_SCALE_DEFAULT:
1860                         zoom_level = 1;
1861                         break;
1862                 case MM_DISPLAY_SCALE_DOUBLE_LENGTH:
1863                         zoom_level = 2;
1864                         break;
1865                 case MM_DISPLAY_SCALE_TRIPLE_LENGTH:
1866                         zoom_level = 3;
1867                         break;
1868                 default:
1869                         MMCAM_LOG_WARNING("Unsupported zoom value. set as default.");
1870                         zoom_level = 1;
1871                         break;
1872                 }
1873
1874                 MMCAMCORDER_G_OBJECT_SET(vsink, "display-geometry-method", display_geometry_method);
1875                 MMCAMCORDER_G_OBJECT_SET(vsink, "display-mode", display_mode);
1876                 MMCAMCORDER_G_OBJECT_SET(vsink, "visible", visible);
1877                 MMCAMCORDER_G_OBJECT_SET(vsink, "zoom", zoom_level);
1878
1879                 if (display_geometry_method == MM_DISPLAY_METHOD_CUSTOM_ROI) {
1880                         if (!strcmp(videosink_name, "tizenwlsink")) {
1881                             gst_video_overlay_set_display_roi_area(GST_VIDEO_OVERLAY(vsink),
1882                                         rect_x, rect_y, rect_width, rect_height);
1883                         } else {
1884                                 g_object_set(vsink,
1885                                         "dst-roi-x", rect_x,
1886                                         "dst-roi-y", rect_y,
1887                                         "dst-roi-w", rect_width,
1888                                         "dst-roi-h", rect_height,
1889                                         NULL);
1890                         }
1891                 }
1892         }
1893
1894         return MM_ERROR_NONE;
1895 }
1896
1897
1898 int _mmcamcorder_video_frame_stabilize(MMHandleType handle, int cmd)
1899 {
1900         int category = 0;
1901         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1902         _MMCamcorderSubContext *sc = NULL;
1903
1904         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1905
1906         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
1907
1908         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1909
1910         switch (cmd) {
1911         case _MMCamcorder_CMD_PREVIEW_START:
1912                 category = CONFIGURE_CATEGORY_CTRL_CAMERA;
1913                 break;
1914         case _MMCamcorder_CMD_CAPTURE:
1915                 category = CONFIGURE_CATEGORY_CTRL_CAPTURE;
1916                 break;
1917         default:
1918                 MMCAM_LOG_WARNING("unknown command : %d", cmd);
1919                 return MM_ERROR_CAMCORDER_INVALID_ARGUMENT;
1920         }
1921
1922         _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_ctrl,
1923                 category, "FrameStabilityCount", &sc->frame_stability_count);
1924
1925         MMCAM_LOG_INFO("[cmd %d] frame stability count : %d",
1926                 cmd, sc->frame_stability_count);
1927
1928         return MM_ERROR_NONE;
1929 }
1930
1931 /* Retreive device information and set them to attributes */
1932 gboolean _mmcamcorder_get_device_info(MMHandleType handle)
1933 {
1934         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1935         _MMCamcorderSubContext *sc = NULL;
1936         GstCameraControl *control = NULL;
1937         GstCameraControlExifInfo exif_info = {0,};
1938
1939         mmf_return_val_if_fail(hcamcorder, FALSE);
1940         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
1941
1942         memset(&exif_info, 0x0, sizeof(GstCameraControlExifInfo));
1943
1944         if (sc && sc->element) {
1945                 int err = MM_ERROR_NONE;
1946                 char *err_name = NULL;
1947                 double focal_len = 0.0;
1948
1949                 /* Video input device */
1950                 if (sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst) {
1951                         /* Exif related information */
1952                         control = GST_CAMERA_CONTROL(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst);
1953                         if (control != NULL) {
1954                                 gst_camera_control_get_exif_info(control, &exif_info); /* get video input device information */
1955                                 if (exif_info.focal_len_denominator != 0)
1956                                         focal_len = ((double)exif_info.focal_len_numerator) / ((double) exif_info.focal_len_denominator);
1957                         } else {
1958                                 MMCAM_LOG_ERROR("Fail to get camera control interface!");
1959                                 focal_len = 0.0;
1960                         }
1961                 }
1962
1963                 /* Set values to attributes */
1964                 err = mm_camcorder_set_attributes(handle, &err_name,
1965                         MMCAM_CAMERA_FOCAL_LENGTH, focal_len,
1966                         NULL);
1967                 if (err != MM_ERROR_NONE) {
1968                         MMCAM_LOG_ERROR("Set attributes error(%s:%x)!", err_name, err);
1969                         SAFE_FREE(err_name);
1970                         return FALSE;
1971                 }
1972         } else {
1973                 MMCAM_LOG_WARNING("Sub context isn't exist.");
1974                 return FALSE;
1975         }
1976
1977         return TRUE;
1978 }
1979
1980 static guint32 _mmcamcorder_convert_fourcc_string_to_value(const gchar* format_name)
1981 {
1982         return format_name[0] | (format_name[1] << 8) | (format_name[2] << 16) | (format_name[3] << 24);
1983 }
1984
1985 static GstPadProbeReturn __mmcamcorder_video_dataprobe_preview(GstPad *pad, GstPadProbeInfo *info, gpointer u_data)
1986 {
1987         gboolean ret = TRUE;
1988         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(u_data);
1989         _MMCamcorderSubContext *sc = NULL;
1990         GstBuffer *buffer = GST_PAD_PROBE_INFO_BUFFER(info);
1991         GstSample *sample = NULL;
1992         GstCaps *caps = NULL;
1993
1994         mmf_return_val_if_fail(buffer, GST_PAD_PROBE_DROP);
1995         mmf_return_val_if_fail(gst_buffer_n_memory(buffer), GST_PAD_PROBE_DROP);
1996         mmf_return_val_if_fail(hcamcorder, GST_PAD_PROBE_DROP);
1997
1998         sc = MMF_CAMCORDER_SUBCONTEXT(u_data);
1999         mmf_return_val_if_fail(sc, GST_PAD_PROBE_DROP);
2000
2001         if (sc->drop_vframe > 0) {
2002                 if (sc->pass_first_vframe > 0) {
2003                         sc->pass_first_vframe--;
2004                         MMCAM_LOG_INFO("Pass video frame by pass_first_vframe");
2005                 } else {
2006                         sc->drop_vframe--;
2007                         MMCAM_LOG_INFO("Drop video frame by drop_vframe");
2008                         return GST_PAD_PROBE_DROP;
2009                 }
2010         } else if (sc->frame_stability_count > 0) {
2011                 sc->frame_stability_count--;
2012                 MMCAM_LOG_INFO("Drop video frame by frame_stability_count");
2013                 return GST_PAD_PROBE_DROP;
2014         }
2015
2016         if (hcamcorder->measure_preview_fps && hcamcorder->state >= MM_CAMCORDER_STATE_PREPARE)
2017                 _mmcamcorder_measure_fps(&sc->kpi);
2018
2019         /* The first H.264 frame should not be skipped for vstream cb. */
2020         if (hcamcorder->state < MM_CAMCORDER_STATE_PREPARE &&
2021                 !_mmcamcorder_is_encoded_preview_pixel_format(sc->info_image->preview_format)) {
2022                 MMCAM_LOG_WARNING("Not ready for stream callback");
2023                 return GST_PAD_PROBE_OK;
2024         }
2025
2026         /* make sample with buffer and caps */
2027         caps = gst_pad_get_allowed_caps(pad);
2028         mmf_return_val_if_fail(caps, GST_PAD_PROBE_OK);
2029
2030         sample = gst_sample_new(buffer, caps, NULL, NULL);
2031         gst_caps_unref(caps);
2032         mmf_return_val_if_fail(sample, GST_PAD_PROBE_OK);
2033
2034         ret = _mmcamcorder_invoke_video_stream_cb((MMHandleType)hcamcorder, sample, TRUE, -1);
2035
2036         gst_sample_unref(sample);
2037
2038         if (ret)
2039                 return GST_PAD_PROBE_OK;
2040         else
2041                 return GST_PAD_PROBE_DROP;
2042 }
2043
2044
2045 static GstPadProbeReturn __mmcamcorder_video_dataprobe_record(GstPad *pad, GstPadProbeInfo *info, gpointer u_data)
2046 {
2047         gboolean ret = TRUE;
2048         GstSample *sample = NULL;
2049         GstCaps *caps = NULL;
2050
2051         caps = gst_pad_get_allowed_caps(pad);
2052         mmf_return_val_if_fail(caps, GST_PAD_PROBE_OK);
2053
2054         sample = gst_sample_new(GST_PAD_PROBE_INFO_BUFFER(info), caps, NULL, NULL);
2055         gst_caps_unref(caps);
2056         mmf_return_val_if_fail(sample, GST_PAD_PROBE_OK);
2057
2058         ret = _mmcamcorder_video_push_buffer(u_data, sample);
2059
2060         gst_sample_unref(sample);
2061
2062         return (ret ? GST_PAD_PROBE_OK : GST_PAD_PROBE_DROP);
2063 }
2064
2065
2066 GstPadProbeReturn __mmcamcorder_muxed_dataprobe(GstPad *pad, GstPadProbeInfo *info, gpointer u_data)
2067 {
2068         MMCamcorderMuxedStreamDataType stream;
2069         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(u_data);
2070         _MMCamcorderSubContext *sc = NULL;
2071         GstBuffer *buffer = GST_PAD_PROBE_INFO_BUFFER(info);
2072         GstMapInfo mapinfo;
2073
2074         mmf_return_val_if_fail(buffer, GST_PAD_PROBE_OK);
2075         mmf_return_val_if_fail(gst_buffer_n_memory(buffer), GST_PAD_PROBE_OK);
2076         mmf_return_val_if_fail(hcamcorder, GST_PAD_PROBE_OK);
2077
2078         sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
2079         mmf_return_val_if_fail(sc, GST_PAD_PROBE_OK);
2080
2081         if (!gst_buffer_map(buffer, &mapinfo, GST_MAP_READ)) {
2082                 MMCAM_LOG_WARNING("map failed : buffer %p", buffer);
2083                 return GST_PAD_PROBE_OK;
2084         }
2085
2086         /* call application callback */
2087         _MMCAMCORDER_LOCK_MSTREAM_CALLBACK(hcamcorder);
2088
2089         if (hcamcorder->mstream_cb) {
2090                 stream.data = (void *)mapinfo.data;
2091                 stream.length = mapinfo.size;
2092                 stream.offset = sc->muxed_stream_offset;
2093                 hcamcorder->mstream_cb(&stream, hcamcorder->mstream_cb_param);
2094         }
2095
2096         _MMCAMCORDER_UNLOCK_MSTREAM_CALLBACK(hcamcorder);
2097
2098         /* calculate current offset */
2099         sc->muxed_stream_offset += mapinfo.size;
2100
2101         gst_buffer_unmap(buffer, &mapinfo);
2102
2103         return GST_PAD_PROBE_OK;
2104 }
2105
2106
2107 GstPadProbeReturn __mmcamcorder_eventprobe_monitor(GstPad *pad, GstPadProbeInfo *info, gpointer u_data)
2108 {
2109         GstEvent *event = GST_PAD_PROBE_INFO_EVENT(info);
2110         mmf_camcorder_t *hcamcorder = NULL;
2111         _MMCamcorderSubContext *sc = NULL;
2112         GstObject *parent = NULL;
2113
2114         switch (GST_EVENT_TYPE(event)) {
2115         case GST_EVENT_UNKNOWN:
2116         /* upstream events */
2117         case GST_EVENT_QOS:
2118         case GST_EVENT_SEEK:
2119         case GST_EVENT_NAVIGATION:
2120         case GST_EVENT_LATENCY:
2121         /* downstream serialized events */
2122         case GST_EVENT_BUFFERSIZE:
2123                 MMCAM_LOG_INFO("[%s:%s] gots %s", GST_DEBUG_PAD_NAME(pad), GST_EVENT_TYPE_NAME(event));
2124                 break;
2125         case GST_EVENT_TAG:
2126                 {
2127                         GstTagList *tag_list = NULL;
2128                         _MMCamcorderReplayGain *replay_gain = NULL;
2129
2130                         MMCAM_LOG_INFO("[%s:%s] gots %s", GST_DEBUG_PAD_NAME(pad), GST_EVENT_TYPE_NAME(event));
2131
2132                         hcamcorder = MMF_CAMCORDER(u_data);
2133                         if (!hcamcorder || !hcamcorder->sub_context) {
2134                                 MMCAM_LOG_WARNING("NULL handle");
2135                                 break;
2136                         }
2137
2138                         replay_gain = &hcamcorder->sub_context->replay_gain;
2139
2140                         gst_event_parse_tag(event, &tag_list);
2141                         if (!tag_list) {
2142                                 MMCAM_LOG_WARNING("failed to get tag list");
2143                                 break;
2144                         }
2145
2146                         if (!gst_tag_list_get_double(tag_list, GST_TAG_TRACK_PEAK, &replay_gain->track_peak)) {
2147                                 MMCAM_LOG_WARNING("failed to get GST_TAG_TRACK_PEAK");
2148                                 break;
2149                         }
2150
2151                         if (!gst_tag_list_get_double(tag_list, GST_TAG_TRACK_GAIN, &replay_gain->track_gain)) {
2152                                 MMCAM_LOG_WARNING("failed to get GST_TAG_TRACK_GAIN");
2153                                 break;
2154                         }
2155
2156                         if (!gst_tag_list_get_double(tag_list, GST_TAG_ALBUM_PEAK, &replay_gain->album_peak)) {
2157                                 MMCAM_LOG_WARNING("failed to get GST_TAG_ALBUM_PEAK");
2158                                 break;
2159                         }
2160
2161                         if (!gst_tag_list_get_double(tag_list, GST_TAG_ALBUM_GAIN, &replay_gain->album_gain)) {
2162                                 MMCAM_LOG_WARNING("failed to get GST_TAG_ALBUM_PEAK");
2163                                 break;
2164                         }
2165
2166                         MMCAM_LOG_INFO("Track [peak %lf, gain %lf], Album [peak %lf, gain %lf]",
2167                                 replay_gain->track_peak, replay_gain->track_gain,
2168                                 replay_gain->album_peak, replay_gain->album_gain);
2169                 }
2170                 break;
2171         case GST_EVENT_SEGMENT:
2172                 MMCAM_LOG_INFO("[%s:%s] gots %s", GST_DEBUG_PAD_NAME(pad), GST_EVENT_TYPE_NAME(event));
2173
2174                 hcamcorder = MMF_CAMCORDER(u_data);
2175                 if (!hcamcorder) {
2176                         MMCAM_LOG_WARNING("NULL handle");
2177                         break;
2178                 }
2179
2180                 sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
2181                 if (!sc) {
2182                         MMCAM_LOG_WARNING("NULL sub context");
2183                         break;
2184                 }
2185
2186                 if (!sc->encode_element[_MMCAMCORDER_ENCSINK_SINK].gst) {
2187                         MMCAM_LOG_WARNING("no encoder sink");
2188                         break;
2189                 }
2190
2191                 parent = gst_pad_get_parent(pad);
2192                 if (!parent) {
2193                         MMCAM_LOG_WARNING("get parent failed");
2194                         break;
2195                 }
2196
2197                 if (parent == (GstObject *)sc->encode_element[_MMCAMCORDER_ENCSINK_SINK].gst) {
2198                         const GstSegment *segment;
2199                         gst_event_parse_segment(event, &segment);
2200                         if (segment->format == GST_FORMAT_BYTES) {
2201                                 MMCAM_LOG_INFO("change current offset %llu -> %"G_GUINT64_FORMAT,
2202                                         sc->muxed_stream_offset, segment->start);
2203
2204                                 sc->muxed_stream_offset = (unsigned long long)segment->start;
2205                         }
2206                 }
2207
2208                 gst_object_unref(parent);
2209                 parent = NULL;
2210                 break;
2211         case GST_EVENT_EOS:
2212                 MMCAM_LOG_WARNING("[%s:%s] gots %s", GST_DEBUG_PAD_NAME(pad), GST_EVENT_TYPE_NAME(event));
2213                 break;
2214         /* bidirectional events */
2215         case GST_EVENT_FLUSH_START:
2216         case GST_EVENT_FLUSH_STOP:
2217                 MMCAM_LOG_ERROR("[%s:%s] gots %s", GST_DEBUG_PAD_NAME(pad), GST_EVENT_TYPE_NAME(event));
2218                 break;
2219         default:
2220                 MMCAM_LOG_INFO("[%s:%s] gots %s", GST_DEBUG_PAD_NAME(pad), GST_EVENT_TYPE_NAME(event));
2221                 break;
2222         }
2223
2224         return GST_PAD_PROBE_OK;
2225 }
2226
2227
2228 int __mmcamcorder_get_amrnb_bitrate_mode(int bitrate)
2229 {
2230         int result = MM_CAMCORDER_MR475;
2231
2232         if (bitrate < 5150)
2233                 result = MM_CAMCORDER_MR475; /*AMR475*/
2234         else if (bitrate < 5900)
2235                 result = MM_CAMCORDER_MR515; /*AMR515*/
2236         else if (bitrate < 6700)
2237                 result = MM_CAMCORDER_MR59; /*AMR59*/
2238         else if (bitrate < 7400)
2239                 result = MM_CAMCORDER_MR67; /*AMR67*/
2240         else if (bitrate < 7950)
2241                 result = MM_CAMCORDER_MR74; /*AMR74*/
2242         else if (bitrate < 10200)
2243                 result = MM_CAMCORDER_MR795; /*AMR795*/
2244         else if (bitrate < 12200)
2245                 result = MM_CAMCORDER_MR102; /*AMR102*/
2246         else
2247                 result = MM_CAMCORDER_MR122; /*AMR122*/
2248
2249         return result;
2250 }
2251
2252
2253 int _mmcamcorder_get_eos_message(MMHandleType handle)
2254 {
2255         int64_t end_time;
2256         int ret = MM_ERROR_NONE;
2257
2258         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
2259         _MMCamcorderSubContext *sc = NULL;
2260
2261         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2262         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2263         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2264
2265         MMCAM_LOG_INFO("START");
2266
2267         _MMCAMCORDER_LOCK(handle);
2268
2269         if (sc->bget_eos == FALSE) {
2270                 end_time = g_get_monotonic_time() + 3 * G_TIME_SPAN_SECOND;
2271                 if (_MMCAMCORDER_WAIT_UNTIL(handle, end_time)) {
2272                         MMCAM_LOG_INFO("EOS signal received");
2273                 } else {
2274                         MMCAM_LOG_ERROR("EOS wait time out");
2275
2276                         if (hcamcorder->error_code == MM_ERROR_NONE)
2277                                 hcamcorder->error_code = MM_ERROR_CAMCORDER_RESPONSE_TIMEOUT;
2278                 }
2279         } else {
2280                 MMCAM_LOG_INFO("already got EOS");
2281         }
2282
2283         _MMCAMCORDER_UNLOCK(handle);
2284
2285         if (hcamcorder->error_code == MM_ERROR_NONE) {
2286                 if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
2287                         mmf_return_val_if_fail(sc->info_video, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2288                         if (sc->info_video->b_committing)
2289                                 _mmcamcorder_video_handle_eos((MMHandleType)hcamcorder);
2290                 } else {
2291                         mmf_return_val_if_fail(sc->info_audio, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2292                         if (sc->info_audio->b_committing)
2293                                 _mmcamcorder_audio_handle_eos((MMHandleType)hcamcorder);
2294                 }
2295         } else {
2296                 ret = hcamcorder->error_code;
2297                 MMCAM_LOG_ERROR("error 0x%x", ret);
2298         }
2299
2300         MMCAM_LOG_INFO("END");
2301
2302         return ret;
2303 }
2304
2305
2306 void _mmcamcorder_remove_element_handle(MMHandleType handle, void *element, int first_elem, int last_elem)
2307 {
2308         int i = 0;
2309         _MMCamcorderGstElement *remove_element = (_MMCamcorderGstElement *)element;
2310
2311         mmf_return_if_fail(handle && remove_element);
2312         mmf_return_if_fail((first_elem >= 0) && (last_elem > 0) && (last_elem > first_elem));
2313
2314         MMCAM_LOG_INFO("");
2315
2316         for (i = first_elem ; i <= last_elem ; i++) {
2317                 remove_element[i].gst = NULL;
2318                 remove_element[i].id = _MMCAMCORDER_NONE;
2319         }
2320
2321         return;
2322 }
2323
2324
2325 int _mmcamcorder_check_codec_fileformat_compatibility(const char *codec_type, int codec, int file_format)
2326 {
2327         mmf_return_val_if_fail(codec_type, MM_ERROR_CAMCORDER_INVALID_ARGUMENT);
2328
2329         /* Check compatibility between codec and file format */
2330         if (!strcmp(codec_type, MMCAM_AUDIO_ENCODER)) {
2331                 if (codec > MM_AUDIO_CODEC_INVALID && codec < MM_AUDIO_CODEC_NUM &&
2332                         file_format > MM_FILE_FORMAT_INVALID && file_format < MM_FILE_FORMAT_NUM) {
2333                         if (audiocodec_fileformat_compatibility_table[codec][file_format] == 0) {
2334                                 MMCAM_LOG_ERROR("Audio codec[%d] and file format[%d] compatibility FAILED.", codec, file_format);
2335                                 return MM_ERROR_CAMCORDER_ENCODER_WRONG_TYPE;
2336                         }
2337
2338                         MMCAM_LOG_INFO("Audio codec[%d] and file format[%d] compatibility SUCCESS.", codec, file_format);
2339                 } else {
2340                         MMCAM_LOG_ERROR("Audio codec[%d] or file format[%d] is INVALID.", codec, file_format);
2341                         return MM_ERROR_CAMCORDER_ENCODER_WRONG_TYPE;
2342                 }
2343         } else if (!strcmp(codec_type, MMCAM_VIDEO_ENCODER)) {
2344                 if (codec > MM_VIDEO_CODEC_INVALID && codec < MM_VIDEO_CODEC_NUM &&
2345                         file_format > MM_FILE_FORMAT_INVALID && file_format < MM_FILE_FORMAT_NUM) {
2346                         if (videocodec_fileformat_compatibility_table[codec][file_format] == 0) {
2347                                 MMCAM_LOG_ERROR("Video codec[%d] and file format[%d] compatibility FAILED.", codec, file_format);
2348                                 return MM_ERROR_CAMCORDER_ENCODER_WRONG_TYPE;
2349                         }
2350
2351                         MMCAM_LOG_INFO("Video codec[%d] and file format[%d] compatibility SUCCESS.", codec, file_format);
2352                 } else {
2353                         MMCAM_LOG_ERROR("Video codec[%d] or file format[%d] is INVALID.", codec, file_format);
2354                         return MM_ERROR_CAMCORDER_ENCODER_WRONG_TYPE;
2355                 }
2356         }
2357
2358         return MM_ERROR_NONE;
2359 }
2360
2361
2362 int _mmcamcorder_check_audiocodec_fileformat_compatibility(MMHandleType handle)
2363 {
2364         int err = MM_ERROR_NONE;
2365         int audio_codec = MM_AUDIO_CODEC_INVALID;
2366         int file_format = MM_FILE_FORMAT_INVALID;
2367
2368         char *err_name = NULL;
2369
2370         err = mm_camcorder_get_attributes(handle, &err_name,
2371                 MMCAM_AUDIO_ENCODER, &audio_codec,
2372                 MMCAM_FILE_FORMAT, &file_format,
2373                 NULL);
2374         if (err != MM_ERROR_NONE) {
2375                 MMCAM_LOG_WARNING("Get attrs fail. (%s:%x)", err_name, err);
2376                 SAFE_FREE(err_name);
2377                 return err;
2378         }
2379
2380         /* Check compatibility between audio codec and file format */
2381         err = _mmcamcorder_check_codec_fileformat_compatibility(MMCAM_AUDIO_ENCODER, audio_codec, file_format);
2382
2383         return err;
2384 }
2385
2386
2387 int _mmcamcorder_check_videocodec_fileformat_compatibility(MMHandleType handle)
2388 {
2389         int err = MM_ERROR_NONE;
2390         int video_codec = MM_VIDEO_CODEC_INVALID;
2391         int file_format = MM_FILE_FORMAT_INVALID;
2392
2393         char *err_name = NULL;
2394
2395         err = mm_camcorder_get_attributes(handle, &err_name,
2396                 MMCAM_VIDEO_ENCODER, &video_codec,
2397                 MMCAM_FILE_FORMAT, &file_format,
2398                 NULL);
2399         if (err != MM_ERROR_NONE) {
2400                 MMCAM_LOG_WARNING("Get attrs fail. (%s:%x)", err_name, err);
2401                 SAFE_FREE(err_name);
2402                 return err;
2403         }
2404
2405         /* Check compatibility between video codec and file format */
2406         err = _mmcamcorder_check_codec_fileformat_compatibility(MMCAM_VIDEO_ENCODER, video_codec, file_format);
2407
2408         return err;
2409 }
2410
2411
2412 bool _mmcamcorder_set_display_rotation(MMHandleType handle, int display_rotate, int videosink_index)
2413 {
2414         const char* videosink_name = NULL;
2415
2416         mmf_camcorder_t *hcamcorder = NULL;
2417         _MMCamcorderSubContext *sc = NULL;
2418
2419         hcamcorder = MMF_CAMCORDER(handle);
2420         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2421
2422         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2423         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2424         mmf_return_val_if_fail(sc->element, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2425
2426         if (sc->element[videosink_index].gst) {
2427                 /* Get videosink name */
2428                 _mmcamcorder_conf_get_value_element_name(sc->VideosinkElement, &videosink_name);
2429                 if (videosink_name == NULL) {
2430                         MMCAM_LOG_ERROR("Please check videosink element in configuration file");
2431                         return FALSE;
2432                 }
2433
2434                 if (!strcmp(videosink_name, "tizenwlsink") || !strcmp(videosink_name, "xvimagesink") ||
2435                         !strcmp(videosink_name, "evasimagesink") || !strcmp(videosink_name, "evaspixmapsink") ||
2436                         !strcmp(videosink_name, "directvideosink")) {
2437                         MMCAMCORDER_G_OBJECT_SET(sc->element[videosink_index].gst, "rotate", display_rotate);
2438                         MMCAM_LOG_INFO("Set display-rotate [%d] done.", display_rotate);
2439                 } else {
2440                         MMCAM_LOG_WARNING("[%s] does not support DISPLAY_ROTATION, but no error", videosink_name);
2441                 }
2442
2443                 return TRUE;
2444         } else {
2445                 MMCAM_LOG_ERROR("Videosink element is null");
2446                 return FALSE;
2447         }
2448 }
2449
2450
2451 bool _mmcamcorder_set_display_flip(MMHandleType handle, int display_flip, int videosink_index)
2452 {
2453         const char* videosink_name = NULL;
2454
2455         mmf_camcorder_t *hcamcorder = NULL;
2456         _MMCamcorderSubContext *sc = NULL;
2457
2458         hcamcorder = MMF_CAMCORDER(handle);
2459         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2460
2461         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2462         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2463         mmf_return_val_if_fail(sc->element, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2464
2465         if (sc->element[videosink_index].gst) {
2466                 /* Get videosink name */
2467                 _mmcamcorder_conf_get_value_element_name(sc->VideosinkElement, &videosink_name);
2468                 if (videosink_name == NULL) {
2469                         MMCAM_LOG_ERROR("Please check videosink element in configuration file");
2470                         return FALSE;
2471                 }
2472
2473                 if (!strcmp(videosink_name, "tizenwlsink") || !strcmp(videosink_name, "xvimagesink") ||
2474                         !strcmp(videosink_name, "evasimagesink") || !strcmp(videosink_name, "evaspixmapsink") ||
2475                         !strcmp(videosink_name, "directvideosink")) {
2476                         MMCAMCORDER_G_OBJECT_SET(sc->element[videosink_index].gst, "flip", display_flip);
2477                         MMCAM_LOG_INFO("Set display flip [%d] done.", display_flip);
2478                 } else {
2479                         MMCAM_LOG_WARNING("[%s] does not support DISPLAY_FLIP, but no error", videosink_name);
2480                 }
2481
2482                 return TRUE;
2483         } else {
2484                 MMCAM_LOG_ERROR("Videosink element is null");
2485                 return FALSE;
2486         }
2487 }
2488
2489
2490 bool _mmcamcorder_set_videosrc_rotation(MMHandleType handle, int videosrc_rotate)
2491 {
2492         int fps = 0;
2493         _MMCamcorderSubContext *sc = NULL;
2494
2495         mmf_camcorder_t *hcamcorder = NULL;
2496
2497         hcamcorder = MMF_CAMCORDER(handle);
2498         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2499
2500         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2501         if (!sc) {
2502                 MMCAM_LOG_INFO("sub context is not initialized");
2503                 return TRUE;
2504         }
2505
2506         mm_camcorder_get_attributes(handle, NULL,
2507                 MMCAM_CAMERA_WIDTH, &sc->info_video->preview_width,
2508                 MMCAM_CAMERA_HEIGHT, &sc->info_video->preview_height,
2509                 MMCAM_CAMERA_FPS, &fps,
2510                 NULL);
2511
2512         MMCAM_LOG_INFO("set rotate %d", videosrc_rotate);
2513
2514         return _mmcamcorder_set_videosrc_caps(handle, sc->fourcc,
2515                 sc->info_video->preview_width, sc->info_video->preview_height,
2516                 fps, videosrc_rotate);
2517 }
2518
2519
2520 bool _mmcamcorder_set_videosrc_caps(MMHandleType handle, unsigned int fourcc, int width, int height, int fps, int rotate)
2521 {
2522         int set_width = 0;
2523         int set_height = 0;
2524         int set_rotate = 0;
2525         int fps_auto = 0;
2526         unsigned int caps_fourcc = 0;
2527         gboolean do_set_caps = FALSE;
2528         char fourcc_string[sizeof(fourcc) + 1];
2529         gchar *caps_str = NULL;
2530 #ifdef _MMCAMCORDER_PRODUCT_TV
2531         gint maxwidth = 0;
2532         gint maxheight = 0;
2533         int display_surface_type = MM_DISPLAY_SURFACE_NULL;
2534 #endif /* _MMCAMCORDER_PRODUCT_TV */
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 (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
2698                 /* assume that it's camera capture mode */
2699                 MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSINK_SINK].gst, "csc-range", 1);
2700         }
2701
2702         if (!do_set_caps)
2703                 return TRUE;
2704
2705         switch (sc->info_image->preview_format) {
2706         case MM_PIXEL_FORMAT_ENCODED_H264:
2707 #ifdef _MMCAMCORDER_PRODUCT_TV
2708                 mm_camcorder_get_attributes(handle, NULL,
2709                         MMCAM_DISPLAY_SURFACE, &display_surface_type,
2710                         NULL);
2711                 if (display_surface_type != MM_DISPLAY_SURFACE_NULL &&
2712                         __mmcamcorder_find_max_resolution(handle, &maxwidth, &maxheight) == false) {
2713                         MMCAM_LOG_ERROR("can not find max resolution limitation");
2714                         return false;
2715                 } else if (display_surface_type == MM_DISPLAY_SURFACE_NULL) {
2716                         maxwidth = set_width;
2717                         maxheight = set_height;
2718                 }
2719 #endif /* _MMCAMCORDER_PRODUCT_TV */
2720                 caps = gst_caps_new_simple("video/x-h264",
2721                         "width", G_TYPE_INT, set_width,
2722                         "height", G_TYPE_INT, set_height,
2723                         "framerate", GST_TYPE_FRACTION, fps, 1,
2724                         "stream-format", G_TYPE_STRING, "byte-stream",
2725 #ifdef _MMCAMCORDER_PRODUCT_TV
2726                         "maxwidth", G_TYPE_INT, maxwidth,
2727                         "maxheight", G_TYPE_INT, maxheight,
2728                         "alignment", G_TYPE_STRING, "au",
2729 #endif /* _MMCAMCORDER_PRODUCT_TV */
2730                         NULL);
2731                 break;
2732         case MM_PIXEL_FORMAT_ENCODED_MJPEG:
2733 #ifdef _MMCAMCORDER_PRODUCT_TV
2734                 caps = gst_caps_new_simple("video/x-jpeg",
2735 #else
2736                 caps = gst_caps_new_simple("image/jpeg",
2737 #endif
2738                         "width", G_TYPE_INT, set_width,
2739                         "height", G_TYPE_INT, set_height,
2740                         "framerate", GST_TYPE_FRACTION, fps, 1,
2741                         NULL);
2742                 break;
2743         case MM_PIXEL_FORMAT_ENCODED_VP8:
2744                 caps = gst_caps_new_simple("video/x-vp8",
2745                         "width", G_TYPE_INT, set_width,
2746                         "height", G_TYPE_INT, set_height,
2747                         "framerate", GST_TYPE_FRACTION, fps, 1,
2748                         NULL);
2749                 break;
2750         case MM_PIXEL_FORMAT_ENCODED_VP9:
2751                 caps = gst_caps_new_simple("video/x-vp9",
2752                         "width", G_TYPE_INT, set_width,
2753                         "height", G_TYPE_INT, set_height,
2754                         "framerate", GST_TYPE_FRACTION, fps, 1,
2755                         NULL);
2756                 break;
2757                 break;
2758         default:
2759                 memcpy(fourcc_string, (char*)&fourcc, sizeof(fourcc));
2760                 fourcc_string[sizeof(fourcc)] = '\0';
2761                 caps = gst_caps_new_simple("video/x-raw",
2762                         "format", G_TYPE_STRING, fourcc_string,
2763                         "width", G_TYPE_INT, set_width,
2764                         "height", G_TYPE_INT, set_height,
2765                         "framerate", GST_TYPE_FRACTION, fps, 1,
2766                         "rotate", G_TYPE_INT, set_rotate,
2767                         NULL);
2768                 break;
2769         }
2770
2771         if (!caps) {
2772                 MMCAM_LOG_ERROR("There are no caps");
2773                 return FALSE;
2774         }
2775
2776         caps_str = gst_caps_to_string(caps);
2777         MMCAM_LOG_INFO("vidoesrc new caps set [%s]", caps_str);
2778         g_free(caps_str);
2779
2780         MMCAMCORDER_G_OBJECT_SET_POINTER(sc->element[_MMCAMCORDER_VIDEOSRC_FILT].gst, "caps", caps);
2781         gst_caps_unref(caps);
2782
2783         return TRUE;
2784 }
2785
2786
2787 bool _mmcamcorder_set_videosrc_flip(MMHandleType handle, int videosrc_flip)
2788 {
2789         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
2790         _MMCamcorderSubContext *sc = NULL;
2791
2792         mmf_return_val_if_fail(hcamcorder, FALSE);
2793
2794         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2795         if (!sc)
2796                 return TRUE;
2797
2798         MMCAM_LOG_INFO("Set FLIP %d", videosrc_flip);
2799
2800         if (sc->element && sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst) {
2801                 int hflip = 0;
2802                 int vflip = 0;
2803
2804                 /* Interleaved format does not support FLIP */
2805                 if (sc->info_image->preview_format != MM_PIXEL_FORMAT_ITLV_JPEG_UYVY) {
2806                         hflip = (videosrc_flip & MM_FLIP_HORIZONTAL) == MM_FLIP_HORIZONTAL;
2807                         vflip = (videosrc_flip & MM_FLIP_VERTICAL) == MM_FLIP_VERTICAL;
2808
2809                         MMCAM_LOG_INFO("videosrc flip H:%d, V:%d", hflip, vflip);
2810
2811                         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "hflip", hflip);
2812                         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "vflip", vflip);
2813                 } else {
2814                         MMCAM_LOG_WARNING("ITLV format does not support FLIP. Ignore FLIP[%d]",
2815                                 videosrc_flip);
2816                 }
2817         } else {
2818                 MMCAM_LOG_WARNING("element is NULL");
2819                 return FALSE;
2820         }
2821
2822         return TRUE;
2823 }
2824
2825
2826 bool _mmcamcorder_set_videosrc_anti_shake(MMHandleType handle, int anti_shake)
2827 {
2828         GstCameraControl *control = NULL;
2829         _MMCamcorderSubContext *sc = NULL;
2830         GstElement *v_src = NULL;
2831
2832         int set_value = 0;
2833
2834         mmf_return_val_if_fail(handle, FALSE);
2835
2836         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2837         if (!sc)
2838                 return TRUE;
2839
2840         v_src = sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst;
2841
2842         if (!v_src) {
2843                 MMCAM_LOG_WARNING("videosrc element is NULL");
2844                 return FALSE;
2845         }
2846
2847         set_value = _mmcamcorder_convert_msl_to_sensor(handle, MM_CAM_CAMERA_ANTI_HANDSHAKE, anti_shake);
2848
2849         /* set anti-shake with camera control */
2850         if (!GST_IS_CAMERA_CONTROL(v_src)) {
2851                 MMCAM_LOG_WARNING("Can't cast Video source into camera control.");
2852                 return FALSE;
2853         }
2854
2855         control = GST_CAMERA_CONTROL(v_src);
2856         if (gst_camera_control_set_ahs(control, set_value)) {
2857                 MMCAM_LOG_INFO("Succeed in operating anti-handshake. value[%d]", set_value);
2858                 return TRUE;
2859         } else {
2860                 MMCAM_LOG_WARNING("Failed to operate anti-handshake. value[%d]", set_value);
2861         }
2862
2863         return FALSE;
2864 }
2865
2866
2867 bool _mmcamcorder_set_videosrc_stabilization(MMHandleType handle, int stabilization)
2868 {
2869         _MMCamcorderSubContext *sc = NULL;
2870         GstElement *v_src = NULL;
2871
2872         mmf_return_val_if_fail(handle, FALSE);
2873
2874         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2875         if (!sc)
2876                 return TRUE;
2877
2878         v_src = sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst;
2879         if (!v_src) {
2880                 MMCAM_LOG_WARNING("videosrc element is NULL");
2881                 return FALSE;
2882         }
2883
2884         /* check property of videosrc element - support VDIS */
2885         if (g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(v_src)), "enable-vdis-mode")) {
2886                 int video_width = 0;
2887                 int video_height = 0;
2888
2889                 if (stabilization == MM_CAMCORDER_VIDEO_STABILIZATION_ON) {
2890                         MMCAM_LOG_INFO("ENABLE video stabilization");
2891
2892                         /* VDIS mode only supports NV12 and [720p or 1080p or 1088 * 1088] */
2893                         mm_camcorder_get_attributes(handle, NULL,
2894                                 MMCAM_VIDEO_WIDTH, &video_width,
2895                                 MMCAM_VIDEO_HEIGHT, &video_height,
2896                                 NULL);
2897
2898                         if (sc->info_image->preview_format == MM_PIXEL_FORMAT_NV12 && video_width >= 1080 && video_height >= 720) {
2899                                 MMCAM_LOG_INFO("NV12, video size %dx%d, ENABLE video stabilization",
2900                                         video_width, video_height);
2901                                 /* set vdis mode */
2902                                 g_object_set(G_OBJECT(v_src),
2903                                                          "enable-vdis-mode", TRUE,
2904                                                          NULL);
2905                         } else {
2906                                 MMCAM_LOG_WARNING("invalid preview format %c%c%c%c or video size %dx%d",
2907                                                                 sc->fourcc, sc->fourcc>>8, sc->fourcc>>16, sc->fourcc>>24,
2908                                                                 video_width, video_height);
2909                                 return FALSE;
2910                         }
2911                 } else {
2912                         /* set vdis mode */
2913                         g_object_set(G_OBJECT(v_src),
2914                                                 "enable-vdis-mode", FALSE,
2915                                                 NULL);
2916
2917                         MMCAM_LOG_INFO("DISABLE video stabilization");
2918                 }
2919         } else if (stabilization == MM_CAMCORDER_VIDEO_STABILIZATION_ON) {
2920                 MMCAM_LOG_ERROR("no property for video stabilization, so can not set ON");
2921                 return FALSE;
2922         } else {
2923                 MMCAM_LOG_WARNING("no property for video stabilization");
2924         }
2925
2926         return TRUE;
2927 }
2928
2929
2930 bool _mmcamcorder_set_camera_resolution(MMHandleType handle, int width, int height)
2931 {
2932         int fps = 0;
2933
2934         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
2935         _MMCamcorderSubContext *sc = NULL;
2936
2937         mmf_return_val_if_fail(hcamcorder, FALSE);
2938
2939         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2940         if (!sc)
2941                 return TRUE;
2942
2943         mm_camcorder_get_attributes(handle, NULL,
2944                 MMCAM_CAMERA_FPS, &fps,
2945                 NULL);
2946
2947         MMCAM_LOG_INFO("set %dx%d", width, height);
2948
2949         return _mmcamcorder_set_videosrc_caps(handle, sc->fourcc, width, height, fps, sc->videosrc_rotate);
2950 }
2951
2952
2953 bool _mmcamcorder_set_encoded_preview_bitrate(MMHandleType handle, int bitrate)
2954 {
2955         _MMCamcorderSubContext *sc = NULL;
2956
2957         if ((void *)handle == NULL) {
2958                 MMCAM_LOG_WARNING("handle is NULL");
2959                 return FALSE;
2960         }
2961
2962         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2963         if (!sc) {
2964                 MMCAM_LOG_WARNING("subcontext is NULL");
2965                 return FALSE;
2966         }
2967
2968         if (sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst == NULL) {
2969                 MMCAM_LOG_WARNING("videosrc plugin is NULL");
2970                 return FALSE;
2971         }
2972
2973         MMCAM_LOG_INFO("set encoded preview bitrate : %d bps", bitrate);
2974
2975         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "bitrate", bitrate);
2976
2977         return TRUE;
2978 }
2979
2980
2981 bool _mmcamcorder_set_encoded_preview_gop_interval(MMHandleType handle, int gop_interval)
2982 {
2983         _MMCamcorderSubContext *sc = NULL;
2984
2985         if ((void *)handle == NULL) {
2986                 MMCAM_LOG_WARNING("handle is NULL");
2987                 return FALSE;
2988         }
2989
2990         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2991         if (!sc) {
2992                 MMCAM_LOG_WARNING("subcontext is NULL");
2993                 return FALSE;
2994         }
2995
2996         if (sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst == NULL) {
2997                 MMCAM_LOG_WARNING("videosrc plugin is NULL");
2998                 return FALSE;
2999         }
3000
3001         MMCAM_LOG_INFO("set encoded preview GOP interval : %d ms", gop_interval);
3002
3003         MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "gop-interval", gop_interval);
3004
3005         return TRUE;
3006 }
3007
3008
3009 bool _mmcamcorder_set_sound_stream_info(GstElement *element, char *stream_type, int stream_index)
3010 {
3011         GstStructure *props = NULL;
3012         char stream_props[64] = {'\0',};
3013
3014         if (element == NULL || stream_type == NULL || stream_index < 0) {
3015                 MMCAM_LOG_ERROR("invalid argument %p %p %d", element, stream_type, stream_index);
3016                 return FALSE;
3017         }
3018
3019         snprintf(stream_props, sizeof(stream_props) - 1,
3020                 "props,media.role=%s, media.parent_id=%d",
3021                 stream_type, stream_index);
3022
3023         MMCAM_LOG_WARNING("stream type %s, index %d -> [%s]", stream_type, stream_index, stream_props);
3024
3025         props = gst_structure_from_string(stream_props, NULL);
3026         if (!props) {
3027                 MMCAM_LOG_ERROR("failed to create GstStructure");
3028                 return FALSE;
3029         }
3030
3031         MMCAMCORDER_G_OBJECT_SET_POINTER(element, "stream-properties", props);
3032
3033         gst_structure_free(props);
3034         props = NULL;
3035
3036         return TRUE;
3037 }
3038
3039
3040 bool _mmcamcorder_recreate_decoder_for_encoded_preview(MMHandleType handle)
3041 {
3042         int ret = MM_ERROR_NONE;
3043         _MMCamcorderSubContext *sc = NULL;
3044         mmf_camcorder_t *hcamcorder = NULL;
3045         const char *videodecoder_name = NULL;
3046         char videodecoder_name_final[32] = {'\0',};
3047         int display_surface_type = MM_DISPLAY_SURFACE_NULL;
3048 #ifdef _MMCAMCORDER_RM_SUPPORT
3049         int decoder_index = 0;
3050 #endif /* _MMCAMCORDER_RM_SUPPORT */
3051
3052         if ((void *)handle == NULL) {
3053                 MMCAM_LOG_WARNING("handle is NULL");
3054                 return FALSE;
3055         }
3056
3057         hcamcorder = MMF_CAMCORDER(handle);
3058
3059         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
3060         if (!sc) {
3061                 MMCAM_LOG_WARNING("subcontext is NULL");
3062                 return FALSE;
3063         }
3064
3065         if (_mmcamcorder_is_encoded_preview_pixel_format(sc->info_image->preview_format) == false ||
3066                 hcamcorder->recreate_decoder == FALSE) {
3067                 MMCAM_LOG_INFO("skip this fuction - format %d, recreate decoder %d",
3068                         sc->info_image->preview_format, hcamcorder->recreate_decoder);
3069                 return TRUE;
3070         }
3071
3072         mm_camcorder_get_attributes(handle, NULL,
3073                 MMCAM_DISPLAY_SURFACE, &display_surface_type,
3074                 NULL);
3075
3076         if (display_surface_type == MM_DISPLAY_SURFACE_NULL) {
3077                 MMCAM_LOG_INFO("no need to control decoder element");
3078                 return TRUE;
3079         }
3080
3081         if (sc->element[_MMCAMCORDER_MAIN_PIPE].gst == NULL ||
3082             sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst == NULL) {
3083                 MMCAM_LOG_WARNING("main pipeline or decoder plugin is NULL");
3084                 return FALSE;
3085         }
3086
3087         MMCAM_LOG_INFO("start");
3088
3089         _mmcamcorder_conf_get_value_element_name(sc->VideodecoderElement, &videodecoder_name);
3090         if (videodecoder_name == NULL) {
3091                 MMCAM_LOG_ERROR("failed to get decoder element name from %p", sc->VideodecoderElement);
3092                 return FALSE;
3093         }
3094
3095         /* set state as NULL */
3096         ret = _mmcamcorder_gst_set_state(handle, sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst, GST_STATE_NULL);
3097         if (ret != MM_ERROR_NONE) {
3098                 MMCAM_LOG_ERROR("failed to set NULL to decoder");
3099                 return FALSE;
3100         }
3101
3102         /* remove decoder - pads will be unlinked automatically in remove function */
3103         if (!gst_bin_remove(GST_BIN(sc->element[_MMCAMCORDER_MAIN_PIPE].gst),
3104                             sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst)) {
3105                 MMCAM_LOG_ERROR("failed to remove decoder from pipeline");
3106                 return FALSE;
3107         }
3108
3109         /* check decoder element */
3110         if (sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst) {
3111                 MMCAM_LOG_INFO("decoder[%p] is still alive - ref count %d",
3112                         G_OBJECT(sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst),
3113                         ((GObject *)sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst)->ref_count);
3114         }
3115
3116 #ifdef _MMCAMCORDER_RM_SUPPORT
3117         if (sc->info_image->preview_format == MM_PIXEL_FORMAT_ENCODED_H264) {
3118                 if (hcamcorder->request_resources.category_id[0] == RM_CATEGORY_VIDEO_DECODER_SUB)
3119                         decoder_index = 1;
3120
3121                 snprintf(videodecoder_name_final, sizeof(videodecoder_name_final), "%s%d", videodecoder_name, decoder_index);
3122         } else if (sc->info_image->preview_format == MM_PIXEL_FORMAT_ENCODED_MJPEG) {
3123                 /* MJPEG */
3124                 if (hcamcorder->request_resources.category_id[0] == RM_CATEGORY_VIDEO_DECODER)
3125                         snprintf(videodecoder_name_final, sizeof(videodecoder_name_final), "%s", "omx_uhd_mjpegdec");
3126                 else
3127                         snprintf(videodecoder_name_final, sizeof(videodecoder_name_final), "%s", "omx_mjpegdec");
3128         } else {
3129 #endif /* _MMCAMCORDER_RM_SUPPORT */
3130                 snprintf(videodecoder_name_final, sizeof(videodecoder_name_final), "%s", videodecoder_name);
3131 #ifdef _MMCAMCORDER_RM_SUPPORT
3132         }
3133 #endif /* _MMCAMCORDER_RM_SUPPORT */
3134
3135         /* create decoder */
3136         sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst = gst_element_factory_make(videodecoder_name_final, "videosrc_decode");
3137         if (sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst == NULL) {
3138                 MMCAM_LOG_ERROR("Decoder[%s] creation fail", videodecoder_name_final);
3139                 return FALSE;
3140         }
3141
3142         _mmcamcorder_conf_set_value_element_property(sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst, sc->VideodecoderElement);
3143
3144         sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].id = _MMCAMCORDER_VIDEOSRC_DECODE;
3145         g_object_weak_ref(G_OBJECT(sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst),
3146                 (GWeakNotify)_mmcamcorder_element_release_noti, sc);
3147
3148         /* add to pipeline */
3149         if (!gst_bin_add(GST_BIN(sc->element[_MMCAMCORDER_MAIN_PIPE].gst),
3150                 sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst)) {
3151                 MMCAM_LOG_ERROR("failed to add decoder to pipeline");
3152                 gst_object_unref(sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst);
3153                 return FALSE;
3154         }
3155
3156         /* link */
3157         if (_MM_GST_ELEMENT_LINK(GST_ELEMENT(sc->element[_MMCAMCORDER_VIDEOSRC_QUE].gst),
3158                 GST_ELEMENT(sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst))) {
3159                 MMCAM_LOG_INFO("Link videosrc_queue to decoder OK");
3160         } else {
3161                 MMCAM_LOG_ERROR("Link videosrc_queue to decoder FAILED");
3162                 return FALSE;
3163         }
3164
3165         if (_MM_GST_ELEMENT_LINK(GST_ELEMENT(sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst),
3166                 GST_ELEMENT(sc->element[_MMCAMCORDER_VIDEOSINK_QUE].gst))) {
3167                 MMCAM_LOG_INFO("Link decoder to videosink_queue OK");
3168         } else {
3169                 MMCAM_LOG_ERROR("Link decoder to videosink_queue FAILED");
3170                 return FALSE;
3171         }
3172
3173         /* set state READY */
3174         ret = _mmcamcorder_gst_set_state(handle, sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst, GST_STATE_READY);
3175         if (ret != MM_ERROR_NONE) {
3176                 MMCAM_LOG_ERROR("failed to set READY to decoder");
3177                 return FALSE;
3178         }
3179
3180         MMCAM_LOG_INFO("done");
3181
3182         return TRUE;
3183 }
3184
3185 #ifdef _MMCAMCORDER_PRODUCT_TV
3186 static bool __mmcamcorder_find_max_resolution(MMHandleType handle, gint *max_width, gint *max_height)
3187 {
3188         _MMCamcorderSubContext *sc = NULL;
3189         mmf_camcorder_t *hcamcorder = NULL;
3190         int index = 0;
3191         const gchar *mime = NULL;
3192         GstPad *sinkpad;
3193         GstCaps *decsink_caps = NULL;
3194         GstStructure *decsink_struct = NULL;
3195
3196         mmf_return_val_if_fail(handle, false);
3197         mmf_return_val_if_fail(max_width, false);
3198         mmf_return_val_if_fail(max_height, false);
3199
3200         hcamcorder = MMF_CAMCORDER(handle);
3201         mmf_return_val_if_fail(hcamcorder, false);
3202
3203         sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
3204
3205         sinkpad = gst_element_get_static_pad(sc->element[_MMCAMCORDER_VIDEOSRC_DECODE].gst, "sink");
3206         if (!sinkpad) {
3207                 MMCAM_LOG_ERROR("There are no decoder caps");
3208                 return false;
3209         }
3210
3211         decsink_caps = gst_pad_get_pad_template_caps(sinkpad);
3212         if (!decsink_caps) {
3213                 gst_object_unref(sinkpad);
3214                 MMCAM_LOG_ERROR("There is no decoder sink caps");
3215                 return false;
3216         }
3217
3218         for (index = 0; index < gst_caps_get_size(decsink_caps); index++) {
3219                 decsink_struct = gst_caps_get_structure(decsink_caps, index);
3220                 if (!decsink_struct) {
3221                         MMCAM_LOG_ERROR("There are no structure from caps");
3222                         gst_object_unref(decsink_caps);
3223                         gst_object_unref(sinkpad);
3224                         return false;
3225                 }
3226                 mime = gst_structure_get_name(decsink_struct);
3227                 if (!strcmp(mime, "video/x-h264")) {
3228                         MMCAM_LOG_INFO("h264 caps structure found");
3229                         if (gst_structure_has_field(decsink_struct, "maxwidth"))
3230                                 *max_width = gst_value_get_int_range_max(gst_structure_get_value(decsink_struct, "maxwidth"));
3231                         if (gst_structure_has_field(decsink_struct, "maxheight"))
3232                                 *max_height = gst_value_get_int_range_max(gst_structure_get_value(decsink_struct, "maxheight"));
3233                         break;
3234                 } else if (!strcmp(mime, "video/x-jpeg")) {
3235                         MMCAM_LOG_INFO("mjpeg caps structure found");
3236                         if (gst_structure_has_field(decsink_struct, "maxwidth"))
3237                                 *max_width = gst_value_get_int_range_max(gst_structure_get_value(decsink_struct, "maxwidth"));
3238                         if (gst_structure_has_field(decsink_struct, "maxheight"))
3239                                 *max_height = gst_value_get_int_range_max(gst_structure_get_value(decsink_struct, "maxheight"));
3240                         break;
3241                 }
3242         }
3243         MMCAM_LOG_INFO("maxwidth = %d , maxheight = %d", (int)*max_width, (int)*max_height);
3244         gst_object_unref(decsink_caps);
3245         gst_object_unref(sinkpad);
3246
3247         if (*max_width <= 0 || *max_height <= 0)
3248                 return false;
3249
3250         return true;
3251 }
3252 #endif /* _MMCAMCORDER_PRODUCT_TV */