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