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