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