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