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