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