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