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