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