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