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