Release signal before state change.
[platform/core/multimedia/libmm-transcode.git] / transcode / include / mm_transcode_internal.h
1 /*
2  * libmm-transcode
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: YoungHun Kim <yh8004.kim@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 #ifndef __MM_TRANSCODE_INTERNAL_H__
23 #define __MM_TRANSCODE_INTERNAL_H__
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29 #include "mm_transcode.h"
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <mm_log.h>
35 #include <mm_debug.h>
36 #include <mm_error.h>
37 #include <mm_debug.h>
38 #include <mm_attrs.h>
39 #include <glib.h>
40 #include <gst/gst.h>
41 #include <gst/gstpipeline.h>
42 #include <gst/gstbuffer.h>
43 #include <gst/app/gstappsrc.h>
44 #include <gst/video/video.h>
45 #include <gst/app/gstappsink.h>
46 #include <glib/gprintf.h>
47 #include <gst/gstelement.h>
48 #include <gst/check/gstcheck.h>
49 #include <gst/pbutils/pbutils.h>
50 #include <gst/pbutils/encoding-profile.h>
51 #include <gst/pbutils/pbutils-enumtypes.h>
52 #include <gst/video/video-format.h>
53 #include <pthread.h>
54 #include <mm_file.h>
55 #include <gmodule.h>
56 #define BUFFER_SIZE 4096
57 #define ENC_BUFFER_SIZE 25
58
59 #define TRANSCODE_FREE(src) { if(src) {g_free(src); src = NULL;} }
60 #define AAC "aac"
61 #define ACON "auto-audio-convert"
62 #define ACS "auto-colorspace"
63 #define ACAPS "acaps"
64 #define AENC "aenc-name"
65 #define AMR "amr"
66 #define AMRENC "amrnbenc"
67 #define ARS "auto-audio-resample"
68 #define ASF "asfdemux"
69 #define AVENCMPEG4 "avenc_mpeg4"
70 #define AVENCH263 "avenc_h263p"
71 #define MUX "mux-name"
72 #define MUXAAC "avmux_adts"
73 #define MUX3GP "avmux_3gp"
74 #define MUXGPP "gppmux"
75 #define MUXMP4 "avmux_mp4"
76 #define AACENC "avenc_aac"
77 #define AACCOMPLIANCE "compliance"
78 #define AACCOMPLIANCELEVEL -2
79 #define PROFILE "profile"
80 #define VENC "venc-name"
81 #define MIN_DURATION 1000
82 #define LAZY_PAUSE_TIMEOUT_MSEC 500
83
84 /*SQCIF */
85 #define VIDEO_RESOLUTION_WIDTH_SQCIF 128
86 #define VIDEO_RESOLUTION_HEIGHT_SQCIF 96
87
88 typedef enum {
89         GST_AUTOPLUG_SELECT_TRY,
90         GST_AUTOPLUG_SELECT_EXPOSE,
91         GST_AUTOPLUG_SELECT_SKIP
92 } GstAutoplugSelectResult;
93
94 typedef struct _handle_param_s
95 {
96         unsigned int resolution_width;
97         unsigned int resolution_height;
98         unsigned int fps_value;
99         unsigned long start_pos;
100         unsigned long duration;
101         mm_seek_mode_e seek_mode;
102         gchar* outputfile;
103         gboolean seeking;
104         gboolean async_done;
105         gboolean segment_done;
106         gboolean completed;
107         unsigned int printed;
108 } handle_param_s;
109
110 typedef struct _handle_vidp_plugin_s
111 {
112         /* decoder video processing pipeline */
113         GstElement *decvideobin;
114         GstElement *decsinkvideoqueue;
115         GstPad *decvideosinkpad;
116         GstPad *decvideosrcpad;
117         GstElement *vidflt;
118         GstElement *videoscale;
119         GstElement *videorate;
120         GstPad *videopad;
121         GstPad *sinkdecvideopad;
122         GstPad *srcdecvideopad;
123 } handle_vidp_plugin_s;
124
125 typedef struct _handle_audp_plugin_s
126 {
127         /* decoder audio processing pipeline */
128         GstElement *decaudiobin;
129         GstElement *decsinkaudioqueue;
130         GstPad *decaudiosinkpad;
131         GstPad *decaudiosrcpad;
132         GstElement *valve;
133         GstElement *aconv;
134         GstElement *resample;
135         GstElement *audflt;
136         GstElement *audiofakesink;
137         GstPad *sinkdecaudiopad;
138         GstPad *srcdecaudiopad;
139         GstPad *decaudiopad;
140         GstPad *decvideopad;
141 } handle_audp_plugin_s;
142
143 typedef struct _handle_encode_s
144 {
145         /* encode pipeline */
146         GstElement *encbin;
147         GstPad *encaudiopad;
148         GstPad *encvideopad;
149         GstElement *ffmux;
150         GstElement *vencqueue;
151         GstElement *aencqueue;
152         GstElement *encodepad;
153         GstElement *encodevideo;
154         gulong video_event_probe_id;
155         gulong audio_event_probe_id;
156         int encodebin_profile;
157 } handle_encode_s;
158
159 typedef struct _handle_property_s
160 {
161         /* pipeline property */
162         guint bus_watcher;
163         int AUDFLAG;
164         int VIDFLAG;
165
166         char *audiodecodename;
167         char *videodecodename;
168         gchar *venc;
169         gchar *aenc;
170         gchar *mux;
171         gulong video_cb_probe_id;
172         gulong audio_cb_probe_id;
173         guint progrss_event_id;
174         const gchar *mime;
175         gchar format[sizeof(GST_VIDEO_FORMATS_ALL)];
176         gint in_width;
177         gint in_height;
178         gint width;
179         gint height;
180         gint fps_n;
181         gint fps_d;
182         gint aspect_x;
183         gint aspect_y;
184         unsigned int seek_count;
185         unsigned long end_pos;
186         unsigned long current_pos;
187         unsigned long real_duration;
188         unsigned long total_length;
189         char *sourcefile;
190         unsigned int _MMHandle;
191
192         mm_containerformat_e containerformat;
193         mm_videoencoder_e videoencoder;
194         mm_audioencoder_e audioencoder;
195
196         GstCaps *caps;
197         GList *sink_elements;
198         gboolean linked_vidoutbin;
199         gboolean linked_audoutbin;
200         gboolean has_video_stream;
201         gboolean has_audio_stream;
202
203         /* message callback */
204         mm_transcode_completed_callback completed_cb;
205         void * completed_cb_param;
206         mm_transcode_progress_callback progress_cb;
207         void * progress_cb_param;
208
209         /* If transcode is busy */
210         bool is_busy;
211
212         /* repeat thread lock */
213         GCond* thread_cond;
214         GMutex *thread_mutex;
215         GMutex *thread_exit_mutex;
216         GThread* thread;
217         gboolean repeat_thread_exit;
218         GAsyncQueue *queue;
219         int seek_idx;
220 } handle_property_s;
221
222 typedef struct _handle_s
223 {
224         /* Transcode Pipeline Element*/
225         GstElement *pipeline;
226         GstElement *filesrc;
227         GstElement *decodebin;
228         handle_vidp_plugin_s *decoder_vidp;
229         handle_audp_plugin_s *decoder_audp;
230         handle_encode_s *encodebin;
231         GstElement *filesink;
232
233         /* seek paramerter */
234         handle_param_s *param;
235
236         /* pipeline property */
237         handle_property_s *property;
238 } handle_s;
239
240 GstPadProbeReturn _mm_cb_audio_output_stream_probe(GstPad *pad, GstPadProbeInfo *info, gpointer user_data);
241 GstAutoplugSelectResult _mm_cb_decode_bin_autoplug_select(GstElement * element, GstPad * pad, GstCaps * caps, GstElementFactory * factory, handle_s *handle);
242 void _mm_cb_decoder_newpad_encoder(GstElement *decodebin, GstPad *pad, handle_s *handle);
243 GstPadProbeReturn _mm_cb_video_output_stream_probe(GstPad *pad, GstPadProbeInfo *info, gpointer user_data);
244 GstPadProbeReturn _mm_cb_encodebin_sinkpad_event_probe(GstPad *pad, GstPadProbeInfo *info, gpointer user_data);
245 gboolean _mm_cb_print_position(handle_s *handle);
246 gboolean _mm_cb_transcode_bus(GstBus * bus, GstMessage * message, gpointer userdata);
247 const gchar* _mm_check_media_type(GstCaps *caps);
248 int _mm_cleanup_encodebin(handle_s *handle);
249 int _mm_cleanup_signal(handle_s *handle);
250 int _mm_cleanup_pipeline(handle_s *handle);
251 int _mm_decodesrcbin_create(handle_s *handle);
252 int _mm_decodesrcbin_link(handle_s *handle);
253 int _mm_encodebin_create(handle_s *handle);
254 int _mm_encodebin_link(handle_s *handle);
255 int _mm_encodebin_set_venc_aenc(handle_s *handle);
256 int _mm_filesink_create(handle_s *handle);
257 int _mm_filesink_link(handle_s *handle);
258 int _mm_setup_pipeline(handle_s *handle);
259 int _mm_transcode_create(handle_s *handle);
260 int _mm_transcode_get_stream_info(handle_s *handle);
261 int _mm_transcode_link(handle_s *handle);
262 int _mm_transcode_param_flush(handle_s *handle);
263 int _mm_transcode_preset_capsfilter(handle_s *handle, unsigned int resolution_width, unsigned int resolution_height);
264 int _mm_transcode_state_change(handle_s *handle, GstState gst_state);
265 int _mm_transcode_set_handle_element(handle_s *handle, const char * in_Filename, mm_containerformat_e containerformat,
266         mm_videoencoder_e videoencoder, mm_audioencoder_e audioencoder);
267 int _mm_transcode_set_handle_parameter(handle_param_s *param, unsigned int resolution_width, unsigned int resolution_height, unsigned int fps_value, unsigned long start_pos,
268         unsigned long duration, mm_seek_mode_e seek_mode, const char *out_Filename);
269 int _mm_transcode_get_stream_info(handle_s *handle);
270 int _mm_transcode_thread(handle_s *handle);
271
272
273 #ifdef __cplusplus
274 }
275 #endif
276
277 #endif  /*__MM_TRANSCODE_INTERNAL_H__*/