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