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