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