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