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