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