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