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