fix memory leak
[platform/core/multimedia/libmm-streamrecorder.git] / src / mm_streamrecorder_recorder.c
1 /*
2  * libmm-streamrecorder
3  *
4  * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyuntae Kim <ht1211.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 /*=======================================================================================
23 |  INCLUDE FILES                                                                        |
24 =======================================================================================*/
25 #include "mm_streamrecorder_internal.h"
26 #include "mm_streamrecorder_video.h"
27 #include "mm_streamrecorder_gstcommon.h"
28 #include "mm_streamrecorder_recorder.h"
29 #include "mm_streamrecorder_util.h"
30 #include "mm_streamrecorder_buffer_manager.h"
31 #include "mm_streamrecorder_fileinfo.h"
32 #include "mm_streamrecorder_attribute.h"
33
34 #include <gst/gst.h>
35 #include <gst/base/gstadapter.h>
36 #include <gst/app/gstappsrc.h>
37 #include <gst/app/gstappsink.h>
38
39
40 /*---------------------------------------------------------------------------------------
41 |    GLOBAL VARIABLE DEFINITIONS for internal                                           |
42 ---------------------------------------------------------------------------------------*/
43
44 /*---------------------------------------------------------------------------------------
45 |    LOCAL VARIABLE DEFINITIONS for internal                                            |
46 ---------------------------------------------------------------------------------------*/
47
48 /*-----------------------------------------------------------------------
49 |    GLOBAL VARIABLE DEFINITIONS for internal                           |
50 -----------------------------------------------------------------------*/
51
52 /*---------------------------------------------------------------------------------------
53 |    LOCAL FUNCTION PROTOTYPES:                                                         |
54 ---------------------------------------------------------------------------------------*/
55 /* STATIC INTERNAL FUNCTION */
56
57 /*=======================================================================================
58 |  FUNCTION DEFINITIONS                                                                 |
59 =======================================================================================*/
60 /*---------------------------------------------------------------------------------------
61 |    GLOBAL FUNCTION DEFINITIONS:                                                       |
62 ---------------------------------------------------------------------------------------*/
63 int _mmstreamrecorder_create_pipeline(MMHandleType handle)
64 {
65         int ret = MM_ERROR_NONE;
66         mmf_streamrecorder_t *hstreamrecorder = MMF_STREAMRECORDER(handle);
67         _MMStreamRecorderSubContext *sc = NULL;
68         GstElement *pipeline = NULL;
69
70         _mmstreamrec_dbg_log("handle : %x", handle);
71
72         mmf_return_val_if_fail(hstreamrecorder, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
73
74         sc = MMF_STREAMRECORDER_SUBCONTEXT(handle);
75         mmf_return_val_if_fail(sc, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
76
77         /* ENCODER MODE */
78         ret = _mmstreamrecorder_create_recorder_pipeline(handle);
79
80         pipeline = sc->encode_element[_MMSTREAMRECORDER_ENCODE_MAIN_PIPE].gst;
81
82         ret = _mmstreamrecorder_gst_set_state(handle, pipeline, GST_STATE_READY);
83
84         _mmstreamrec_dbg_log("ret[%x]", ret);
85         return ret;
86 }
87
88 void _mmstreamrecorder_destroy_pipeline(MMHandleType handle)
89 {
90         mmf_streamrecorder_t *hstreamrecorder = MMF_STREAMRECORDER(handle);
91         _MMStreamRecorderSubContext *sc = NULL;
92         gint i = 0;
93         int element_num = 0;
94         _MMStreamRecorderGstElement *element = NULL;
95         GstBus *bus = NULL;
96
97         mmf_return_if_fail(hstreamrecorder);
98
99         sc = MMF_STREAMRECORDER_SUBCONTEXT(handle);
100         mmf_return_if_fail(sc);
101
102         _mmstreamrec_dbg_log("");
103
104         element = sc->encode_element;
105         element_num = sc->encode_element_num;
106         if (element == NULL) {
107                 _mmstreamrec_dbg_log("encode element is null!!");
108                 return;
109         }
110
111         if (sc->encode_element[_MMSTREAMRECORDER_ENCODE_MAIN_PIPE].gst != NULL) {
112                 bus = gst_pipeline_get_bus(GST_PIPELINE(sc->encode_element[_MMSTREAMRECORDER_ENCODE_MAIN_PIPE].gst));
113
114                 _mmstreamrec_dbg_log("Pipeline clear!!");
115
116                 /* Remove pipeline message callback */
117                 if (hstreamrecorder->pipeline_cb_event_id != 0) {
118                         g_source_remove(hstreamrecorder->pipeline_cb_event_id);
119                         hstreamrecorder->pipeline_cb_event_id = 0;
120                 }
121
122                 /* Remove remained message in bus */
123                 if (bus) {
124                         GstMessage *gst_msg = NULL;
125                         while ((gst_msg = gst_bus_pop(bus)) != NULL) {
126                                 _mmstreamrecorder_pipeline_cb_message(bus, gst_msg, (gpointer) hstreamrecorder);
127                                 gst_message_unref(gst_msg);
128                                 gst_msg = NULL;
129                         }
130                         gst_object_unref(bus);
131                         bus = NULL;
132                 }
133
134                 /* Inside each pipeline destroy function, Set GST_STATE_NULL to Main pipeline */
135                 _mmstreamrecorder_destroy_recorder_pipeline(handle);
136         }
137
138         if (element != NULL) {
139                 /* checking unreleased element */
140                 for (i = 0; i < element_num; i++) {
141                         if (element[i].gst) {
142                                 if (GST_IS_ELEMENT(element[i].gst)) {
143                                         _mmstreamrec_dbg_warn("Still alive element - ID[%d], name [%s], ref count[%d], status[%s]", element[i].id, GST_OBJECT_NAME(element[i].gst), GST_OBJECT_REFCOUNT(element[i].gst), gst_element_state_get_name(GST_STATE(element[i].gst)));
144                                         g_object_weak_unref(G_OBJECT(element[i].gst), (GWeakNotify) _mmstreamrecorder_element_release_noti, sc);
145                                 } else {
146                                         _mmstreamrec_dbg_warn("The element[%d] is still aliving, check it", element[i].id);
147                                 }
148
149                                 element[i].id = _MMSTREAMRECORDER_ENCODE_NONE;
150                                 element[i].gst = NULL;
151                         }
152                 }
153         }
154         return;
155 }
156
157 int _mmstreamrecorder_create_recorder_pipeline(MMHandleType handle)
158 {
159         int i = 0;
160         int err = MM_ERROR_NONE;
161         int audio_enable = FALSE;
162         GstBus *bus = NULL;
163         GstPad *srcpad = NULL;
164         GstPad *sinkpad = NULL;
165
166         unsigned int video_codec = MM_VIDEO_CODEC_INVALID;
167         unsigned int file_format = MM_FILE_FORMAT_INVALID;
168         char *err_name = NULL;
169
170         mmf_streamrecorder_t *hstreamrecorder = MMF_STREAMRECORDER(handle);
171         _MMStreamRecorderSubContext *sc = NULL;
172
173         mmf_return_val_if_fail(hstreamrecorder, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
174
175         sc = MMF_STREAMRECORDER_SUBCONTEXT(handle);
176         mmf_return_val_if_fail(sc, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
177
178         _mmstreamrec_dbg_warn("start");
179
180         err = mm_streamrecorder_get_attributes(handle, &err_name, MMSTR_VIDEO_ENCODER, &video_codec, MMSTR_FILE_FORMAT, &file_format, NULL);
181         if (err != MM_ERROR_NONE) {
182                 _mmstreamrec_dbg_warn("Get attrs fail. (%s:%x)", err_name, err);
183                 SAFE_FREE(err_name);
184                 return err;
185         }
186
187         err = _mmstreamrecorder_check_videocodec_fileformat_compatibility(video_codec, file_format);
188         if (err != MM_ERROR_NONE)
189                 return err;
190
191         /* Main pipeline */
192         _MMSTREAMRECORDER_PIPELINE_MAKE(sc, sc->encode_element, _MMSTREAMRECORDER_ENCODE_MAIN_PIPE, "recorder_pipeline", err);
193
194         /* get audio disable */
195         mm_streamrecorder_get_attributes(handle, NULL, MMSTR_AUDIO_ENABLE, &audio_enable, NULL);
196         sc->audio_enable = audio_enable;
197
198         _mmstreamrec_dbg_log("AUDIO DISABLE : %d", sc->audio_enable);
199
200         if (sc->audio_enable == TRUE) {
201                 /* create audiosrc bin */
202                 err = _mmstreamrecorder_create_audiosrc_bin((MMHandleType) hstreamrecorder);
203                 if (err != MM_ERROR_NONE)
204                         return err;
205         }
206
207         err = _mmstreamrecorder_create_encodesink_bin((MMHandleType) hstreamrecorder, MM_STREAMRECORDER_ENCBIN_PROFILE_VIDEO);
208         if (err != MM_ERROR_NONE)
209                 return err;
210
211         if (sc->audio_enable == TRUE) {
212                 gst_bin_add(GST_BIN(sc->encode_element[_MMSTREAMRECORDER_ENCODE_MAIN_PIPE].gst), sc->encode_element[_MMSTREAMRECORDER_AUDIOSRC_BIN].gst);
213         }
214
215         /* add element and encodesink bin to encode main pipeline */
216         gst_bin_add_many(GST_BIN(sc->encode_element[_MMSTREAMRECORDER_ENCODE_MAIN_PIPE].gst), sc->encode_element[_MMSTREAMRECORDER_ENCSINK_SRC].gst, sc->encode_element[_MMSTREAMRECORDER_ENCSINK_FILT].gst, sc->encode_element[_MMSTREAMRECORDER_ENCSINK_BIN].gst, NULL);
217
218         /* Link each element : appsrc - capsfilter - encodesink bin */
219         srcpad = gst_element_get_static_pad(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_SRC].gst, "src");
220         sinkpad = gst_element_get_static_pad(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_FILT].gst, "sink");
221         _MM_GST_PAD_LINK_UNREF(srcpad, sinkpad, err, pipeline_creation_error);
222
223         srcpad = gst_element_get_static_pad(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_FILT].gst, "src");
224         sinkpad = gst_element_get_static_pad(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_BIN].gst, "video_sink0");
225         _MM_GST_PAD_LINK_UNREF(srcpad, sinkpad, err, pipeline_creation_error);
226
227         if (sc->audio_enable == TRUE) {
228                 srcpad = gst_element_get_static_pad(sc->encode_element[_MMSTREAMRECORDER_AUDIOSRC_BIN].gst, "src");
229                 sinkpad = gst_element_get_static_pad(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_BIN].gst, "audio_sink0");
230                 _MM_GST_PAD_LINK_UNREF(srcpad, sinkpad, err, pipeline_creation_error);
231         }
232
233         if (sc->audio_enable == TRUE) {
234                 sinkpad = gst_element_get_static_pad(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_AENC].gst, "sink");
235                 MMSTREAMRECORDER_ADD_BUFFER_PROBE(sinkpad, _MMSTREAMRECORDER_HANDLER_VIDEOREC, __mmstreamrecorder_audioque_dataprobe, hstreamrecorder);
236                 gst_object_unref(sinkpad);
237                 sinkpad = NULL;
238
239                 if (sc->encode_element[_MMSTREAMRECORDER_ENCSINK_AENC_QUE].gst) {
240                         srcpad = gst_element_get_static_pad(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_AENC_QUE].gst, "src");
241                         MMSTREAMRECORDER_ADD_EVENT_PROBE(srcpad, _MMSTREAMRECORDER_HANDLER_VIDEOREC, __mmstreamrecorder_eventprobe_monitor, hstreamrecorder);
242                         gst_object_unref(srcpad);
243                         srcpad = NULL;
244                 }
245         }
246
247         if (sc->encode_element[_MMSTREAMRECORDER_ENCSINK_VENC_QUE].gst) {
248                 srcpad = gst_element_get_static_pad(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_VENC_QUE].gst, "src");
249                 MMSTREAMRECORDER_ADD_EVENT_PROBE(srcpad, _MMSTREAMRECORDER_HANDLER_VIDEOREC, __mmstreamrecorder_eventprobe_monitor, hstreamrecorder);
250                 gst_object_unref(srcpad);
251                 srcpad = NULL;
252         }
253
254         if (sc->audio_enable == FALSE) {
255                 sinkpad = gst_element_get_static_pad(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_VENC].gst, "sink");
256                 MMSTREAMRECORDER_ADD_BUFFER_PROBE(sinkpad, _MMSTREAMRECORDER_HANDLER_VIDEOREC, __mmstreamrecorder_video_dataprobe_audio_disable, hstreamrecorder);
257                 gst_object_unref(sinkpad);
258                 sinkpad = NULL;
259         }
260
261         if (!strcmp(/*gst_element_rsink_name */"filesink", "filesink")) {
262                 srcpad = gst_element_get_static_pad(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_VENC].gst, "src");
263                 MMSTREAMRECORDER_ADD_BUFFER_PROBE(srcpad, _MMSTREAMRECORDER_HANDLER_VIDEOREC, __mmstreamrecorder_video_dataprobe_record, hstreamrecorder);
264                 gst_object_unref(srcpad);
265                 srcpad = NULL;
266
267                 if (sc->audio_enable == TRUE) {
268                         srcpad = gst_element_get_static_pad(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_AENC].gst, "src");
269                         MMSTREAMRECORDER_ADD_BUFFER_PROBE(srcpad, _MMSTREAMRECORDER_HANDLER_VIDEOREC, __mmstreamrecorder_audio_dataprobe_check, hstreamrecorder);
270                         gst_object_unref(srcpad);
271                         srcpad = NULL;
272                 }
273         }
274
275         bus = gst_pipeline_get_bus(GST_PIPELINE(sc->encode_element[_MMSTREAMRECORDER_ENCODE_MAIN_PIPE].gst));
276
277         /* register pipeline message callback */
278         hstreamrecorder->encode_pipeline_cb_event_id = gst_bus_add_watch(bus, (GstBusFunc) _mmstreamrecorder_pipeline_cb_message, hstreamrecorder);
279
280         gst_object_unref(bus);
281         bus = NULL;
282
283         return MM_ERROR_NONE;
284
285  pipeline_creation_error:
286         for (i = _MMSTREAMRECORDER_AUDIOSRC_BIN; i <= _MMSTREAMRECORDER_ENCSINK_SINK; i++) {
287                 _MMSTREAMRECORDER_ELEMENT_REMOVE(sc->encode_element, i);
288         }
289         _MMSTREAMRECORDER_ELEMENT_REMOVE(sc->encode_element, _MMSTREAMRECORDER_ENCODE_MAIN_PIPE);
290         return err;
291 }
292
293 int _mmstreamrecorder_destroy_recorder_pipeline(MMHandleType handle)
294 {
295         int ret = MM_ERROR_NONE;
296         mmf_streamrecorder_t *hstreamrecorder = MMF_STREAMRECORDER(handle);
297         _MMStreamRecorderSubContext *sc = NULL;
298
299         GstBus *bus = NULL;
300
301         mmf_return_val_if_fail(hstreamrecorder, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
302         sc = MMF_STREAMRECORDER_SUBCONTEXT(handle);
303         mmf_return_val_if_fail(sc, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
304
305         _mmstreamrec_dbg_log("start");
306
307         if (!sc->encode_element[_MMSTREAMRECORDER_ENCODE_MAIN_PIPE].gst) {
308                 _mmstreamrec_dbg_warn("pipeline is not existed.");
309                 return MM_ERROR_NONE;
310         }
311
312         _mmstreamrecorder_remove_all_handlers((MMHandleType) hstreamrecorder, _MMSTREAMRECORDER_HANDLER_VIDEOREC);
313
314         ret = _mmstreamrecorder_gst_set_state(handle, sc->encode_element[_MMSTREAMRECORDER_ENCODE_MAIN_PIPE].gst, GST_STATE_NULL);
315         if (ret != MM_ERROR_NONE) {
316                 _mmstreamrec_dbg_err("Faile to change encode main pipeline [0x%x]", ret);
317                 return ret;
318         }
319
320         bus = gst_pipeline_get_bus(GST_PIPELINE(sc->encode_element[_MMSTREAMRECORDER_ENCODE_MAIN_PIPE].gst));
321
322         /* Remove remained message */
323         if (bus) {
324                 GstMessage *gst_msg = NULL;
325                 while ((gst_msg = gst_bus_pop(bus)) != NULL) {
326                         _mmstreamrecorder_pipeline_cb_message(bus, gst_msg, (gpointer) hstreamrecorder);
327                         gst_message_unref(gst_msg);
328                         gst_msg = NULL;
329                 }
330                 gst_object_unref(bus);
331                 bus = NULL;
332         }
333
334         /* remove audio pipeline first */
335         ret = _mmstreamrecorder_destroy_audiosrc_bin(handle);
336         if (ret != MM_ERROR_NONE) {
337                 _mmstreamrec_dbg_err("Fail to remove audio pipeline");
338                 return ret;
339         }
340
341         ret = _mmstreamrecorder_destroy_encodesink_bin(handle);
342         if (ret != MM_ERROR_NONE) {
343                 _mmstreamrec_dbg_err("Fail to remove encoder pipeline");
344                 return ret;
345         }
346
347         /* Remove pipeline message callback */
348         if (hstreamrecorder->encode_pipeline_cb_event_id != 0) {
349                 g_source_remove(hstreamrecorder->encode_pipeline_cb_event_id);
350                 hstreamrecorder->encode_pipeline_cb_event_id = 0;
351         }
352
353         _mmstreamrec_dbg_log("done");
354
355         return ret;
356 }
357
358 int _mmstreamrecorder_create_encodesink_bin(MMHandleType handle, MMStreamRecorderEncodebinProfile profile)
359 {
360         int err = MM_ERROR_NONE;
361         int result = 0;
362         int channel = 0;
363         int audio_enc = 0;
364         int video_enc = 0;
365         int v_bitrate = 0;
366         int a_bitrate = 0;
367         int video_width = 0;
368         int video_height = 0;
369         int video_fps = 0;
370         int file_format = 0;
371         int audio_src_format = 0;
372         int video_src_format = 0;
373         int audio_samplerate = 0;
374         const char *str_profile = NULL;
375         const char *str_aac = NULL;
376         const char *str_aar = NULL;
377         const char *str_acs = NULL;
378         char *err_name = NULL;
379
380         GstCaps *caps = NULL;
381         GstPad *pad = NULL;
382         GList *element_list = NULL;
383         char *temp_filename = NULL;
384         int fileformat = 0;
385         int size = 0;
386         guint imax_size = 0;
387         guint imax_time = 0;
388         int rec_mode = 0;
389
390         _MMStreamRecorderVideoInfo *info = NULL;
391         _MMStreamRecorderFileInfo *finfo = NULL;
392
393         mmf_streamrecorder_t *hstreamrecorder = MMF_STREAMRECORDER(handle);
394         _MMStreamRecorderSubContext *sc = NULL;
395         /* type_element *VideoencElement = NULL; */
396         /* type_element *AudioencElement = NULL; */
397         /* type_element *MuxElement = NULL; */
398         /* type_element *RecordsinkElement = NULL; */
399
400         mmf_return_val_if_fail(hstreamrecorder, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
401
402         sc = MMF_STREAMRECORDER_SUBCONTEXT(handle);
403         mmf_return_val_if_fail(sc, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
404
405         _mmstreamrec_dbg_log("start - profile : %d", profile);
406
407         info = sc->info_video;
408         finfo = sc->info_file;
409
410         /* check element availability */
411         mm_streamrecorder_get_attributes(handle, &err_name, MMSTR_AUDIO_ENCODER, &audio_enc, MMSTR_AUDIO_CHANNEL, &channel, MMSTR_VIDEO_BITRATE, &v_bitrate, MMSTR_VIDEO_ENCODER, &video_enc, MMSTR_AUDIO_BITRATE, &a_bitrate, MMSTR_VIDEO_RESOLUTION_WIDTH, &video_width, MMSTR_VIDEO_RESOLUTION_HEIGHT, &video_height, MMSTR_VIDEO_FRAMERATE, &video_fps, MMSTR_FILE_FORMAT, &file_format, MMSTR_AUDIO_SAMPLERATE, &audio_samplerate, MMSTR_AUDIO_SOURCE_FORMAT, &audio_src_format, MMSTR_VIDEO_SOURCE_FORMAT, &video_src_format, MMSTR_RECORDER_MODE, &rec_mode, NULL);
412
413         _mmstreamrec_dbg_err("audio encoder - %d , video encoder : %d", audio_enc, video_enc);
414         _mmstreamrec_dbg_err("audio channel - %d , video v_bitrate : %d", channel, v_bitrate);
415         _mmstreamrec_dbg_err("audio a_bitrate - %d , video video_width : %d ,video video_height : %d ", a_bitrate, video_width, video_height);
416         _mmstreamrec_dbg_err("video_fps - %d , video file_format : %d", video_fps, file_format);
417
418         /* Check existence */
419         if (sc->encode_element[_MMSTREAMRECORDER_ENCSINK_BIN].gst) {
420                 if (((GObject *) sc->encode_element[_MMSTREAMRECORDER_ENCSINK_BIN].gst)->ref_count > 0)
421                         gst_object_unref(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_BIN].gst);
422
423                 _mmstreamrec_dbg_log("_MMSTREAMRECORDER_ENCSINK_BIN is Already existed.");
424         }
425
426         /* Create bin element */
427         _MMSTREAMRECORDER_BIN_MAKE(sc, sc->encode_element, _MMSTREAMRECORDER_ENCSINK_BIN, "encodesink_bin", err);
428
429         /* Create child element */
430         if (hstreamrecorder->ini.encsink_bin_profile != MM_STREAMRECORDER_ENCBIN_PROFILE_AUDIO) {
431                 /* create appsrc and capsfilter */
432                 _MMSTREAMRECORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMSTREAMRECORDER_ENCSINK_SRC, hstreamrecorder->ini.name_of_encsink_src, "encodesink_src", element_list, err);
433                 _MMSTREAMRECORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMSTREAMRECORDER_ENCSINK_FILT, "capsfilter", "encodesink_filter", element_list, err);
434
435                 caps = gst_set_videosrcpad_caps(video_src_format, video_width, video_height, video_fps, 1);
436                 MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_SRC].gst, "caps", caps);
437                 if (caps) {
438                         gst_caps_unref(caps);
439                         caps = NULL;
440                 }
441
442                 caps = gst_set_videosrcpad_caps(video_src_format, video_width, video_height, video_fps, 1);
443                 MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_FILT].gst, "caps", caps);
444                 if (caps) {
445                         gst_caps_unref(caps);
446                         caps = NULL;
447                 }
448
449
450                 /* release element_list, they will be placed out of encodesink bin */
451                 if (element_list) {
452                         g_list_free(element_list);
453                         element_list = NULL;
454                 }
455                 if (rec_mode == MM_STREAMRECORDER_MODE_MEDIABUFFER) {
456                         /* set appsrc as live source */
457                         MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_SRC].gst, "is-live", hstreamrecorder->ini.encsink_src_islive);
458                 }
459
460         }
461
462         _MMSTREAMRECORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMSTREAMRECORDER_ENCSINK_ENCBIN, "encodebin", "encodesink_encbin", element_list, err);
463
464         _mmstreamrec_dbg_log("Profile[%d]", profile);
465
466         /* Set information */
467         if (hstreamrecorder->ini.encsink_bin_profile == MM_STREAMRECORDER_ENCBIN_PROFILE_VIDEO) {
468                 str_profile = "VideoProfile";
469                 str_aac = "VideoAutoAudioConvert";
470                 str_aar = "VideoAutoAudioResample";
471                 str_acs = "VideoAutoColorSpace";
472         } else if (hstreamrecorder->ini.encsink_bin_profile == MM_STREAMRECORDER_ENCBIN_PROFILE_AUDIO) {
473                 str_profile = "AudioProfile";
474                 str_aac = "AudioAutoAudioConvert";
475                 str_aar = "AudioAutoAudioResample";
476                 str_acs = "AudioAutoColorSpace";
477         }
478
479         /* TODO : check the last value ( set ) */
480         MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "profile", hstreamrecorder->ini.encsink_bin_profile);
481         if (rec_mode == MM_STREAMRECORDER_MODE_MEDIABUFFER) {
482                 MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "auto-audio-convert", hstreamrecorder->ini.encsink_bin_auto_audio_convert);
483         }
484         MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "auto-audio-resample", hstreamrecorder->ini.encsink_bin_auto_audio_resample);
485         MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "auto-colorspace", hstreamrecorder->ini.encsink_bin_auto_colorspace);
486         MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "use-video-toggle", hstreamrecorder->ini.encsink_bin_use_video_toggle);
487
488         /* Codec */
489         if (hstreamrecorder->ini.encsink_bin_profile == MM_STREAMRECORDER_ENCBIN_PROFILE_VIDEO) {
490                 switch (video_enc) {
491                 case MM_VIDEO_CODEC_H263:
492                         MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "venc-name", hstreamrecorder->ini.h263_video_encoder);
493                         break;
494                 case MM_VIDEO_CODEC_H264:
495                         MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "venc-name", hstreamrecorder->ini.h264_video_encoder);
496                         break;
497                 case MM_VIDEO_CODEC_MPEG4:
498                         MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "venc-name", hstreamrecorder->ini.mpeg4_video_encoder);
499                         break;
500                 default:
501                         MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "venc-name", hstreamrecorder->ini.h264_video_encoder);
502                         break;
503                 }
504                 _MMSTREAMRECORDER_ENCODEBIN_ELMGET(sc, _MMSTREAMRECORDER_ENCSINK_VENC, "video-encode", err);
505
506                 /* set color converter size */
507                 MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "vconv-name", hstreamrecorder->ini.name_of_encsink_bin_video_converter);
508                 _MMSTREAMRECORDER_ENCODEBIN_ELMGET(sc, _MMSTREAMRECORDER_ENCSINK_VCONV, "video-convert", err);
509
510                 MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "auto-colorspace", hstreamrecorder->ini.encsink_bin_auto_colorspace);
511
512                 if (video_src_format == MM_STREAMRECORDER_INPUT_FORMAT_NV12) {
513                         video_src_format = MM_STREAMRECORDER_INPUT_FORMAT_I420;
514                 }
515                 caps = gst_set_videosrcpad_caps(video_src_format, video_width, video_height, video_fps, 1);
516                 MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "vcaps", caps);
517                 if (video_src_format != MM_STREAMRECORDER_INPUT_FORMAT_NV12) {
518                         MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_VCONV].gst, "dst-buffer-num", hstreamrecorder->ini.convert_output_buffer_num);
519                 }
520                 /* state tuning */
521                 err = gst_pad_set_caps(gst_element_get_static_pad(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_VENC].gst, "sink"), caps);
522                 err = MM_ERROR_NONE;
523                 /* MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_VENC].gst, "state-tuning", TRUE); */
524
525                 if (caps) {
526                         gst_caps_unref(caps);
527                         caps = NULL;
528                 }
529                 _mmstreamrec_dbg_log("size %dx%d, dst-buffer-num %d", video_width, video_height, hstreamrecorder->ini.convert_output_buffer_num);
530
531                 _mmstreamrec_dbg_warn("encoder set caps result : 0x%x", err);
532
533                 if (hstreamrecorder->ini.encsink_bin_use_parser[0]) {
534                         GstElement *parser = gst_element_factory_make(hstreamrecorder->ini.encsink_bin_use_parser, "parse");
535                         MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "use-venc-queue", parser);
536                         _MMSTREAMRECORDER_ENCODEBIN_ELMGET(sc, _MMSTREAMRECORDER_ENCSINK_PARSER, "use-venc-queue", err);
537                 }
538         }
539
540         if (sc->audio_enable == TRUE) {
541
542                 MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "aenc-name", hstreamrecorder->ini.name_of_encsink_bin_audio_encoder);
543                 _MMSTREAMRECORDER_ENCODEBIN_ELMGET(sc, _MMSTREAMRECORDER_ENCSINK_AENC, "audio-encode", err);
544                 _mmstreamrec_dbg_err("audio-encode err = %x ", err);
545
546                 /* Set basic infomation */
547                 if (audio_enc != MM_AUDIO_CODEC_VORBIS) {
548                         int depth = 0;
549
550                         if (audio_src_format == MM_STREAMRECORDER_AUDIO_FORMAT_PCM_S16_LE) {
551                                 depth = 16;
552                         } else {                        /* MM_STREAMRECORDER_AUDIO_FORMAT_PCM_U8 */
553                                 depth = 8;
554                         }
555
556                         /* TODO : set rate , channel , depth */
557
558                         caps = gst_set_audiosrcpad_caps(audio_samplerate, 2, depth, 16, 1);
559                         /* MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "auto-audio-convert", TRUE); */
560                         MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "acaps", caps);
561                         {
562                                 gchar *type = gst_caps_to_string(caps);
563
564                                 _mmstreamrec_dbg_warn("Set srcpad caps: %s", type);
565                         }
566                         gst_caps_unref(caps);
567                         caps = NULL;
568                 } else {
569                         /* what are the audio encoder which should get audio/x-raw-float? */
570                         caps = gst_caps_new_simple("audio/x-raw", "rate", G_TYPE_INT, audio_samplerate, "channels", G_TYPE_INT, channel, "endianness", G_TYPE_INT, BYTE_ORDER, "width", G_TYPE_INT, 32, NULL);
571                         _mmstreamrec_dbg_log("caps [x-raw-float, rate:%d, channel:%d, endianness:%d, width:32]", audio_samplerate, channel, BYTE_ORDER);
572                         MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "auto-audio-convert", TRUE);
573                         MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "acaps", caps);
574                         gst_caps_unref(caps);
575                         caps = NULL;
576                 }
577
578 #if 0
579                 if (audio_enc == MM_AUDIO_CODEC_AMR && channel == 2) {
580                         caps = gst_caps_new_simple("audio/x-raw-int", "channels", G_TYPE_INT, 1, NULL);
581                         MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "auto-audio-convert", TRUE);
582                         MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "acaps", caps);
583                         gst_caps_unref(caps);
584                         caps = NULL;
585                 }
586
587                 if (audio_enc == MM_AUDIO_CODEC_OGG) {
588                         caps = gst_caps_new_simple("audio/x-raw-int", NULL);
589                         MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "auto-audio-convert", TRUE);
590                         MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "acaps", caps);
591                         gst_caps_unref(caps);
592                         caps = NULL;
593                 }
594 #endif
595
596                 _MMSTREAMRECORDER_ENCODEBIN_ELMGET(sc, _MMSTREAMRECORDER_ENCSINK_AENC_QUE, "use-aenc-queue", err);
597         }
598
599         /* Mux */
600         switch (file_format) {
601         case MM_FILE_FORMAT_3GP:{
602                         MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "mux-name", hstreamrecorder->ini.name_of_encsink_bin_3GPMUXER);
603                 }
604                 break;
605
606         case MM_FILE_FORMAT_MP4:{
607                         MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "mux-name", hstreamrecorder->ini.name_of_encsink_bin_MP4MUXER);
608                 }
609                 break;
610
611         default:{
612                         MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "mux-name", hstreamrecorder->ini.name_of_encsink_bin_MP4MUXER);
613                 }
614                 break;
615
616         }
617         _MMSTREAMRECORDER_ENCODEBIN_ELMGET(sc, _MMSTREAMRECORDER_ENCSINK_MUX, "mux", err);
618
619         /* Sink */
620         /* for recording */
621         _MMSTREAMRECORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMSTREAMRECORDER_ENCSINK_SINK, "filesink", NULL, element_list, err);
622         MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_SINK].gst, "async", 0);
623
624         err = mm_streamrecorder_get_attributes(handle, &err_name,
625                                                                                    MMSTR_FILE_FORMAT, &fileformat, MMSTR_FILENAME, &temp_filename, &size,
626                                                                                    MMSTR_TARGET_MAX_SIZE, &imax_size,
627                                                                                    MMSTR_TARGET_TIME_LIMIT, &imax_time,
628                                                                                    NULL);
629
630         if (err != MM_ERROR_NONE) {
631                 _mmstreamrec_dbg_warn("Get attrs fail. (%s:%x)", err_name, err);
632                 SAFE_FREE(err_name);
633                 return err;
634         }
635
636         finfo->fileformat = fileformat;
637
638         /* set max size */
639         if (imax_size <= 0)
640                 info->max_size = 0;             /* do not check */
641         else
642                 info->max_size = ((guint64) imax_size) << 10;   /* to byte */
643
644         /* set max time */
645         if (imax_time <= 0)
646                 info->max_time = 0;             /* do not check */
647         else
648                 info->max_time = ((guint64) imax_time) * 1000;  /* to millisecond */
649
650         finfo->filename = g_strdup(temp_filename);
651         if (!finfo->filename) {
652                 _mmstreamrec_dbg_err("strdup was failed");
653                 return err;
654         }
655
656         _mmstreamrec_dbg_log("Record start : set file name using attribute - %s ", finfo->filename);
657
658         MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_SINK].gst, "location", finfo->filename);
659
660         if (profile == MM_STREAMRECORDER_ENCBIN_PROFILE_VIDEO) {
661                 /* video encoder attribute setting */
662                 if (v_bitrate > 0) {
663                         MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_VENC].gst, "bitrate", v_bitrate);
664                 } else {
665                         _mmstreamrec_dbg_warn("video bitrate is too small[%d], so skip setting. Use DEFAULT value.", v_bitrate);
666                 }
667         }
668
669         if (sc->audio_enable == TRUE) {
670                 /* audio encoder attribute setting */
671                 if (a_bitrate > 0) {
672                         switch (audio_enc) {
673                         case MM_AUDIO_CODEC_AMR:
674                                 result = _mmstreamrecorder_get_amrnb_bitrate_mode(a_bitrate);
675                                 _mmstreamrec_dbg_log("Set AMR encoder mode [%d]", result);
676                                 MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_AENC].gst, "band-mode", result);
677                                 break;
678                         case MM_AUDIO_CODEC_AAC:
679                                 _mmstreamrec_dbg_log("Set AAC encoder bitrate [%d]", a_bitrate);
680                                 MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_AENC].gst, "bitrate", a_bitrate);
681                                 break;
682                         default:
683                                 _mmstreamrec_dbg_log("Audio codec is not AMR or AAC... you need to implement setting function for audio encoder bit-rate");
684                                 break;
685                         }
686                 } else {
687                         _mmstreamrec_dbg_warn("Setting bitrate is too small, so skip setting. Use DEFAULT value.");
688                 }
689         }
690
691         _mmstreamrec_dbg_log("Element creation complete");
692
693         /* Add element to bin */
694         if (!_mmstreamrecorder_add_elements_to_bin(GST_BIN(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_BIN].gst), element_list)) {
695                 _mmstreamrec_dbg_err("element add error.");
696                 err = MM_ERROR_STREAMRECORDER_RESOURCE_CREATION;
697                 goto pipeline_creation_error;
698         }
699
700         _mmstreamrec_dbg_log("Element add complete");
701
702         if (profile == MM_STREAMRECORDER_ENCBIN_PROFILE_VIDEO) {
703                 pad = gst_element_get_request_pad(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "video");
704                 if (gst_element_add_pad(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_BIN].gst, gst_ghost_pad_new("video_sink0", pad)) < 0) {
705                         gst_object_unref(pad);
706                         pad = NULL;
707                         _mmstreamrec_dbg_err("failed to create ghost video_sink0 on _MMSTREAMRECORDER_ENCSINK_BIN.");
708                         err = MM_ERROR_STREAMRECORDER_GST_LINK;
709                         goto pipeline_creation_error;
710                 }
711                 gst_object_unref(pad);
712                 pad = NULL;
713
714                 if (sc->audio_enable == TRUE) {
715                         pad = gst_element_get_request_pad(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "audio");
716                         if (gst_element_add_pad(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_BIN].gst, gst_ghost_pad_new("audio_sink0", pad)) < 0) {
717                                 gst_object_unref(pad);
718                                 pad = NULL;
719                                 _mmstreamrec_dbg_err("failed to create ghost audio_sink0 on _MMSTREAMRECORDER_ENCSINK_BIN.");
720                                 err = MM_ERROR_STREAMRECORDER_GST_LINK;
721                                 goto pipeline_creation_error;
722                         }
723                         gst_object_unref(pad);
724                         pad = NULL;
725                 }
726         } else if (profile == MM_STREAMRECORDER_ENCBIN_PROFILE_AUDIO) {
727                 pad = gst_element_get_request_pad(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "audio");
728                 if (gst_element_add_pad(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_BIN].gst, gst_ghost_pad_new("audio_sink0", pad)) < 0) {
729                         gst_object_unref(pad);
730                         pad = NULL;
731                         _mmstreamrec_dbg_err("failed to create ghost audio_sink0 on _MMSTREAMRECORDER_ENCSINK_BIN.");
732                         err = MM_ERROR_STREAMRECORDER_GST_LINK;
733                         goto pipeline_creation_error;
734                 }
735                 gst_object_unref(pad);
736                 pad = NULL;
737         } else {
738                 /* for stillshot */
739                 pad = gst_element_get_request_pad(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "image");
740                 if (gst_element_add_pad(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_BIN].gst, gst_ghost_pad_new("image_sink0", pad)) < 0) {
741                         gst_object_unref(pad);
742                         pad = NULL;
743                         _mmstreamrec_dbg_err("failed to create ghost image_sink0 on _MMSTREAMRECORDER_ENCSINK_BIN.");
744                         err = MM_ERROR_STREAMRECORDER_GST_LINK;
745                         goto pipeline_creation_error;
746                 }
747                 gst_object_unref(pad);
748                 pad = NULL;
749         }
750
751         _mmstreamrec_dbg_log("Get pad complete");
752
753         /* Link internal element */
754         if (!_mmstreamrecorder_link_elements(element_list)) {
755                 _mmstreamrec_dbg_err("element link error.");
756                 err = MM_ERROR_STREAMRECORDER_GST_LINK;
757                 goto pipeline_creation_error;
758         }
759
760         if (element_list) {
761                 g_list_free(element_list);
762                 element_list = NULL;
763         }
764
765         _mmstreamrec_dbg_log("done");
766
767         return MM_ERROR_NONE;
768
769  pipeline_creation_error:
770         _MMSTREAMRECORDER_ELEMENT_REMOVE(sc->encode_element, _MMSTREAMRECORDER_ENCSINK_ENCBIN);
771         _MMSTREAMRECORDER_ELEMENT_REMOVE(sc->encode_element, _MMSTREAMRECORDER_ENCSINK_SRC);
772         _MMSTREAMRECORDER_ELEMENT_REMOVE(sc->encode_element, _MMSTREAMRECORDER_ENCSINK_FILT);
773         _MMSTREAMRECORDER_ELEMENT_REMOVE(sc->encode_element, _MMSTREAMRECORDER_ENCSINK_VENC);
774         _MMSTREAMRECORDER_ELEMENT_REMOVE(sc->encode_element, _MMSTREAMRECORDER_ENCSINK_AENC);
775         _MMSTREAMRECORDER_ELEMENT_REMOVE(sc->encode_element, _MMSTREAMRECORDER_ENCSINK_IENC);
776         _MMSTREAMRECORDER_ELEMENT_REMOVE(sc->encode_element, _MMSTREAMRECORDER_ENCSINK_MUX);
777         _MMSTREAMRECORDER_ELEMENT_REMOVE(sc->encode_element, _MMSTREAMRECORDER_ENCSINK_SINK);
778         _MMSTREAMRECORDER_ELEMENT_REMOVE(sc->encode_element, _MMSTREAMRECORDER_ENCSINK_BIN);
779
780         if (element_list) {
781                 g_list_free(element_list);
782                 element_list = NULL;
783         }
784
785         return err;
786 }
787
788 int _mmstreamrecorder_destroy_encodesink_bin(MMHandleType handle)
789 {
790         GstPad *reqpad = NULL;
791         mmf_streamrecorder_t *hstreamrecorder = MMF_STREAMRECORDER(handle);
792         _MMStreamRecorderSubContext *sc = NULL;
793
794         mmf_return_val_if_fail(hstreamrecorder, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
795
796         sc = MMF_STREAMRECORDER_SUBCONTEXT(handle);
797         mmf_return_val_if_fail(sc, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
798
799         _mmstreamrec_dbg_log("");
800
801         if (sc->encode_element[_MMSTREAMRECORDER_ENCODE_MAIN_PIPE].gst != NULL) {
802                 /* release request pad */
803                 reqpad = gst_element_get_static_pad(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "audio");
804                 if (reqpad) {
805                         gst_element_release_request_pad(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, reqpad);
806                         gst_object_unref(reqpad);
807                         reqpad = NULL;
808                 }
809
810                 reqpad = gst_element_get_static_pad(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "video");
811                 if (reqpad) {
812                         gst_element_release_request_pad(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, reqpad);
813                         gst_object_unref(reqpad);
814                         reqpad = NULL;
815                 }
816
817                 /* release encode main pipeline */
818                 gst_object_unref(sc->encode_element[_MMSTREAMRECORDER_ENCODE_MAIN_PIPE].gst);
819
820                 _mmstreamrec_dbg_log("Encoder pipeline removed");
821         }
822
823         return MM_ERROR_NONE;
824 }
825
826 int _mmstreamrecorder_create_audiosrc_bin(MMHandleType handle)
827 {
828         int err = MM_ERROR_NONE;
829         int val = 0;
830         int rate = 0;
831         int format = 0;
832         int channel = 0;
833         unsigned int a_enc = MM_AUDIO_CODEC_INVALID;
834         unsigned int file_format = MM_FILE_FORMAT_INVALID;
835         char *err_name = NULL;
836         int rec_mode = 0;
837
838         GstCaps *caps = NULL;
839         GstPad *pad = NULL;
840         GList *element_list = NULL;
841
842         mmf_streamrecorder_t *hstreamrecorder = MMF_STREAMRECORDER(handle);
843         _MMStreamRecorderSubContext *sc = NULL;
844         _MMStreamRecorderGstElement *last_element = NULL;
845         /* type_element *AudiosrcElement = NULL; */
846
847         mmf_return_val_if_fail(hstreamrecorder, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
848
849         sc = MMF_STREAMRECORDER_SUBCONTEXT(handle);
850         mmf_return_val_if_fail(sc, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
851
852         _mmstreamrec_dbg_log("");
853
854         err = mm_streamrecorder_get_attributes(handle, &err_name, MMSTR_AUDIO_ENCODER, &a_enc, MMSTR_AUDIO_BITRATE, &val, MMSTR_AUDIO_SAMPLERATE, &rate, MMSTR_AUDIO_SOURCE_FORMAT, &format, MMSTR_AUDIO_CHANNEL, &channel, MMSTR_FILE_FORMAT, &file_format, MMSTR_RECORDER_MODE, &rec_mode, NULL);
855
856         if (err != MM_ERROR_NONE) {
857                 _mmstreamrec_dbg_warn("Get attrs fail. (%s:%x)", err_name, err);
858                 SAFE_FREE(err_name);
859                 return err;
860         }
861
862         err = _mmstreamrecorder_check_audiocodec_fileformat_compatibility(a_enc, file_format);
863         if (err != MM_ERROR_NONE) {
864                 _mmstreamrec_dbg_err("error name :%s , audio format %d , fileformat %d. error : %x)", err_name, a_enc, file_format, err);
865                 SAFE_FREE(err_name);
866                 return err;
867         }
868
869         /* Check existence */
870         if (sc->encode_element[_MMSTREAMRECORDER_AUDIOSRC_BIN].gst) {
871                 if (((GObject *) sc->encode_element[_MMSTREAMRECORDER_AUDIOSRC_BIN].gst)->ref_count > 0)
872                         gst_object_unref(sc->encode_element[_MMSTREAMRECORDER_AUDIOSRC_BIN].gst);
873
874                 _mmstreamrec_dbg_log("_MMSTREAMRECORDER_AUDIOSRC_BIN is Already existed. Unref once...");
875         }
876
877         /* Create bin element */
878         _MMSTREAMRECORDER_BIN_MAKE(sc, sc->encode_element, _MMSTREAMRECORDER_AUDIOSRC_BIN, "audiosource_bin", err);
879
880         _MMSTREAMRECORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMSTREAMRECORDER_AUDIOSRC_SRC, "appsrc", hstreamrecorder->ini.name_of_audio_src, element_list, err);
881
882         _MMSTREAMRECORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMSTREAMRECORDER_AUDIOSRC_FILT, "capsfilter", "audiosrc_capsfilter", element_list, err);
883
884         /* Set basic infomation */
885         if (a_enc != MM_AUDIO_CODEC_VORBIS) {
886                 int depth = 0;
887
888                 if (format == MM_STREAMRECORDER_AUDIO_FORMAT_PCM_S16_LE) {
889                         depth = 16;
890                 } else {                                /* MM_STREAMRECORDER_AUDIO_FORMAT_PCM_U8 */
891                         depth = 8;
892                 }
893
894                 caps = gst_set_audiosrcpad_caps(rate, channel, depth, 16, 1);
895
896                 _mmstreamrec_dbg_log("caps [x-raw-int, rate:%d, channel:%d, depth:%d]", rate, channel, depth);
897         } else {
898                 /* what are the audio encoder which should get audio/x-raw-float? */
899                 caps = gst_caps_new_simple("audio/x-raw-float", "rate", G_TYPE_INT, rate, "channels", G_TYPE_INT, channel, "endianness", G_TYPE_INT, BYTE_ORDER, "width", G_TYPE_INT, 32, NULL);
900                 _mmstreamrec_dbg_log("caps [x-raw-float, rate:%d, channel:%d, endianness:%d, width:32]", rate, channel, BYTE_ORDER);
901         }
902
903         if (caps) {
904                 if (rec_mode == MM_STREAMRECORDER_MODE_MEDIABUFFER) {
905                         MMSTREAMRECORDER_G_OBJECT_SET((sc->encode_element[_MMSTREAMRECORDER_AUDIOSRC_SRC].gst), "caps", caps);
906                 }
907                 MMSTREAMRECORDER_G_OBJECT_SET((sc->encode_element[_MMSTREAMRECORDER_AUDIOSRC_FILT].gst), "caps", caps);
908                 {
909                         gchar *type = gst_caps_to_string(caps);
910
911                         _mmstreamrec_dbg_err("_MMSTREAMRECORDER_AUDIOSRC_FILT %s", type);
912
913                 }
914                 gst_caps_unref(caps);
915                 caps = NULL;
916         } else {
917                 _mmstreamrec_dbg_err("create caps error");
918                 err = MM_ERROR_STREAMRECORDER_RESOURCE_CREATION;
919                 goto pipeline_creation_error;
920         }
921
922         if (rec_mode == MM_STREAMRECORDER_MODE_SCREENRECORD) {
923 #if 1                                                   /* mic mode */
924                 MMSTREAMRECORDER_G_OBJECT_SET((sc->encode_element[_MMSTREAMRECORDER_AUDIOSRC_SRC].gst), "do-timestamp", TRUE);
925 #else                                                   /* speaker mode with alsasrc */
926                 MMSTREAMRECORDER_G_OBJECT_SET((sc->encode_element[_MMSTREAMRECORDER_AUDIOSRC_SRC].gst), "device", "hw:0,8");
927                 MMSTREAMRECORDER_G_OBJECT_SET((sc->encode_element[_MMSTREAMRECORDER_AUDIOSRC_SRC].gst), "latency-time", 256000);
928                 MMSTREAMRECORDER_G_OBJECT_SET((sc->encode_element[_MMSTREAMRECORDER_AUDIOSRC_SRC].gst), "buffer-time", 10000);
929                 MMSTREAMRECORDER_G_OBJECT_SET((sc->encode_element[_MMSTREAMRECORDER_AUDIOSRC_SRC].gst), "do-timestamp", FALSE);
930 #endif
931         } else {
932                 MMSTREAMRECORDER_G_OBJECT_SET((sc->encode_element[_MMSTREAMRECORDER_AUDIOSRC_SRC].gst), "do-timestamp", FALSE);
933         }
934
935         if (!_mmstreamrecorder_add_elements_to_bin(GST_BIN(sc->encode_element[_MMSTREAMRECORDER_AUDIOSRC_BIN].gst), element_list)) {
936                 _mmstreamrec_dbg_err("element add error.");
937                 err = MM_ERROR_STREAMRECORDER_RESOURCE_CREATION;
938                 goto pipeline_creation_error;
939         }
940
941         if (!_mmstreamrecorder_link_elements(element_list)) {
942                 _mmstreamrec_dbg_err("element link error.");
943                 err = MM_ERROR_STREAMRECORDER_GST_LINK;
944                 goto pipeline_creation_error;
945         }
946
947         last_element = (_MMStreamRecorderGstElement *) (g_list_last(element_list)->data);
948         pad = gst_element_get_static_pad(last_element->gst, "src");
949         if ((gst_element_add_pad(sc->encode_element[_MMSTREAMRECORDER_AUDIOSRC_BIN].gst, gst_ghost_pad_new("src", pad))) < 0) {
950                 gst_object_unref(pad);
951                 pad = NULL;
952                 _mmstreamrec_dbg_err("failed to create ghost pad on _MMSTREAMRECORDER_AUDIOSRC_BIN.");
953                 err = MM_ERROR_STREAMRECORDER_GST_LINK;
954                 goto pipeline_creation_error;
955         }
956
957         gst_object_unref(pad);
958         pad = NULL;
959
960         if (element_list) {
961                 g_list_free(element_list);
962                 element_list = NULL;
963         }
964
965         return MM_ERROR_NONE;
966
967  pipeline_creation_error:
968         _MMSTREAMRECORDER_ELEMENT_REMOVE(sc->encode_element, _MMSTREAMRECORDER_AUDIOSRC_SRC);
969         _MMSTREAMRECORDER_ELEMENT_REMOVE(sc->encode_element, _MMSTREAMRECORDER_AUDIOSRC_FILT);
970         _MMSTREAMRECORDER_ELEMENT_REMOVE(sc->encode_element, _MMSTREAMRECORDER_AUDIOSRC_BIN);
971
972         if (element_list) {
973                 g_list_free(element_list);
974                 element_list = NULL;
975         }
976
977         return err;
978 }
979
980 int _mmstreamrecorder_destroy_audiosrc_bin(MMHandleType handle)
981 {
982         GstPad *srcpad = NULL;
983         GstPad *sinkpad = NULL;
984         mmf_streamrecorder_t *hstreamrecorder = MMF_STREAMRECORDER(handle);
985         _MMStreamRecorderSubContext *sc = NULL;
986
987         mmf_return_val_if_fail(hstreamrecorder, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
988
989         sc = MMF_STREAMRECORDER_SUBCONTEXT(handle);
990         mmf_return_val_if_fail(sc, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
991
992         _mmstreamrec_dbg_log("");
993
994         if (sc->encode_element[_MMSTREAMRECORDER_AUDIOSRC_BIN].gst != NULL) {
995                 srcpad = gst_element_get_static_pad(sc->encode_element[_MMSTREAMRECORDER_AUDIOSRC_BIN].gst, "src");
996                 sinkpad = gst_element_get_static_pad(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_BIN].gst, "audio_sink0");
997                 _MM_GST_PAD_UNLINK_UNREF(srcpad, sinkpad);
998
999                 /* release audiosrc bin */
1000                 gst_bin_remove(GST_BIN(sc->encode_element[_MMSTREAMRECORDER_ENCODE_MAIN_PIPE].gst), sc->encode_element[_MMSTREAMRECORDER_AUDIOSRC_BIN].gst);
1001
1002                 _mmstreamrecorder_remove_element_handle(handle, (void *)sc->encode_element, _MMSTREAMRECORDER_AUDIOSRC_BIN, _MMSTREAMRECORDER_AUDIOSRC_FILT);
1003
1004                 _mmstreamrec_dbg_log("Audio pipeline removed");
1005         }
1006
1007         return MM_ERROR_NONE;
1008 }
1009
1010 /* COMMAND - VIDEO */
1011 int _mmstreamrecorder_video_command(MMHandleType handle, int command)
1012 {
1013         int ret = MM_ERROR_NONE;
1014         mmf_streamrecorder_t *hstreamrecorder = MMF_STREAMRECORDER(handle);
1015         _MMStreamRecorderVideoInfo *info = NULL;
1016         _MMStreamRecorderAudioInfo *info_audio = NULL;
1017         _MMStreamRecorderFileInfo *finfo = NULL;
1018         _MMStreamRecorderSubContext *sc = NULL;
1019         GstElement *pipeline = NULL;
1020         GstPad *pad = NULL;
1021         guint count = 0;
1022
1023         mmf_return_val_if_fail(hstreamrecorder, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
1024
1025         sc = MMF_STREAMRECORDER_SUBCONTEXT(handle);
1026         mmf_return_val_if_fail(sc && sc->encode_element, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
1027
1028         mmf_return_val_if_fail(sc->info_video, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
1029         if (sc->audio_enable == TRUE) {
1030                 mmf_return_val_if_fail(sc->info_audio, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
1031         }
1032         mmf_return_val_if_fail(sc->info_file, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
1033
1034         info = sc->info_video;
1035         if (sc->audio_enable == TRUE)
1036                 info_audio = sc->info_audio;
1037
1038         finfo = sc->info_file;
1039
1040         _mmstreamrec_dbg_log("command %d", command);
1041
1042         pipeline = sc->encode_element[_MMSTREAMRECORDER_ENCODE_MAIN_PIPE].gst;
1043
1044         switch (command) {
1045         case _MM_STREAMRECORDER_CMD_RECORD:
1046                 {
1047
1048                         /* Recording */
1049                         _mmstreamrec_dbg_log("Record Start");
1050                         MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "block", FALSE);
1051
1052                         /* Adjust display FPS */
1053                         info->video_frame_count = 0;
1054                         if (info_audio)
1055                                 info_audio->audio_frame_count = 0;
1056
1057                         info->filesize = 0;
1058                         sc->ferror_send = FALSE;
1059                         sc->ferror_count = 0;
1060                         sc->error_occurs = FALSE;
1061                         sc->bget_eos = FALSE;
1062
1063                         ret = _mmstreamrecorder_gst_set_state(handle, sc->encode_element[_MMSTREAMRECORDER_ENCODE_MAIN_PIPE].gst, GST_STATE_PLAYING);
1064                         if (ret != MM_ERROR_NONE) {
1065                                 /* Remove recorder pipeline and recording file which size maybe zero */
1066                                 ret = _mmstreamrecorder_destroy_recorder_pipeline(handle);
1067
1068                                 if (finfo->filename) {
1069                                         _mmstreamrec_dbg_log("file delete(%s)", finfo->filename);
1070                                         unlink(finfo->filename);
1071                                         g_free(finfo->filename);
1072                                         finfo->filename = NULL;
1073                                 }
1074                                 goto _ERR_STREAMRECORDER_VIDEO_COMMAND;
1075                         }
1076
1077                 }
1078                 break;
1079         case _MM_STREAMRECORDER_CMD_PAUSE:
1080                 {
1081
1082                         if (info->b_commiting) {
1083                                 _mmstreamrec_dbg_warn("now on commiting previous file!!(command : %d)", command);
1084                                 return MM_ERROR_STREAMRECORDER_CMD_IS_RUNNING;
1085                         }
1086
1087                         for (count = 0; count <= hstreamrecorder->ini.retrial_count; count++) {
1088                                 if (sc->audio_enable == FALSE) {
1089                                         /* check only video frame */
1090                                         if (info->video_frame_count >= hstreamrecorder->ini.minimum_frame) {
1091                                                 break;
1092                                         } else if (count == hstreamrecorder->ini.retrial_count) {
1093                                                 _mmstreamrec_dbg_err("Pause fail, frame count %" G_GUINT64_FORMAT "", info->video_frame_count);
1094                                                 return MM_ERROR_STREAMRECORDER_INVALID_CONDITION;
1095                                         } else {
1096                                                 _mmstreamrec_dbg_warn("Waiting for enough video frame, retrial[%d], frame %" G_GUINT64_FORMAT "", count, info->video_frame_count);
1097                                         }
1098
1099                                         usleep(hstreamrecorder->ini.video_frame_wait_time);
1100                                 } else {
1101                                         /* check both of video and audio frame */
1102                                         if (info->video_frame_count >= hstreamrecorder->ini.minimum_frame && info_audio->audio_frame_count) {
1103                                                 break;
1104                                         } else if (count == hstreamrecorder->ini.retrial_count) {
1105                                                 _mmstreamrec_dbg_err("Pause fail, frame count VIDEO[%" G_GUINT64_FORMAT "], AUDIO [%" G_GUINT64_FORMAT "]", info->video_frame_count, info_audio->audio_frame_count);
1106                                                 return MM_ERROR_STREAMRECORDER_INVALID_CONDITION;
1107                                         } else {
1108                                                 _mmstreamrec_dbg_warn("Waiting for enough frames, retrial [%d], VIDEO[%" G_GUINT64_FORMAT "], AUDIO [%" G_GUINT64_FORMAT "]", count, info->video_frame_count, info_audio->audio_frame_count);
1109                                         }
1110
1111                                         usleep(hstreamrecorder->ini.video_frame_wait_time);
1112                                 }
1113                         }
1114                         /* tee block */
1115                         MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "runtime-pause", TRUE);
1116
1117                         break;
1118                 }
1119                 break;
1120         case _MM_STREAMRECORDER_CMD_CANCEL:
1121                 {
1122                         if (info->b_commiting) {
1123                                 _mmstreamrec_dbg_warn("now on commiting previous file!!(command : %d)", command);
1124                                 return MM_ERROR_STREAMRECORDER_CMD_IS_RUNNING;
1125                         }
1126
1127                         ret = _mmstreamrecorder_destroy_recorder_pipeline(handle);
1128                         if (ret != MM_ERROR_NONE)
1129                                 goto _ERR_STREAMRECORDER_VIDEO_COMMAND;
1130
1131                         /* remove target file */
1132                         if (finfo->filename) {
1133                                 _mmstreamrec_dbg_log("file delete(%s)", finfo->filename);
1134                                 unlink(finfo->filename);
1135                                 g_free(finfo->filename);
1136                                 finfo->filename = NULL;
1137                         }
1138
1139                         sc->isMaxsizePausing = FALSE;
1140                         sc->isMaxtimePausing = FALSE;
1141
1142                         info->video_frame_count = 0;
1143                         if (info_audio)
1144                                 info_audio->audio_frame_count = 0;
1145
1146                         info->filesize = 0;
1147                         break;
1148                 }
1149                 break;
1150         case _MM_STREAMRECORDER_CMD_COMMIT:
1151                 /* video recording command */
1152                 {
1153
1154                         if (info->b_commiting) {
1155                                 _mmstreamrec_dbg_err("now on commiting previous file!!(command : %d)", command);
1156                                 return MM_ERROR_STREAMRECORDER_CMD_IS_RUNNING;
1157                         } else {
1158                                 _mmstreamrec_dbg_log("_MM_STREAMRECORDER_CMD_COMMIT : start");
1159                                 info->b_commiting = TRUE;
1160                         }
1161
1162                         for (count = 0; count <= hstreamrecorder->ini.retrial_count; count++) {
1163                                 if (sc->audio_enable == FALSE) {
1164                                         /* check only video frame */
1165                                         if (info->video_frame_count >= hstreamrecorder->ini.minimum_frame) {
1166                                                 break;
1167                                         } else if (count == hstreamrecorder->ini.retrial_count) {
1168                                                 _mmstreamrec_dbg_err("Commit fail, frame count is %" G_GUINT64_FORMAT "", info->video_frame_count);
1169                                                 info->b_commiting = FALSE;
1170                                                 return MM_ERROR_STREAMRECORDER_INVALID_CONDITION;
1171                                         } else {
1172                                                 _mmstreamrec_dbg_warn("Waiting for enough video frame, retrial [%d], frame %" G_GUINT64_FORMAT "", count, info->video_frame_count);
1173                                         }
1174
1175                                         usleep(hstreamrecorder->ini.video_frame_wait_time);
1176                                 } else {
1177                                         /* check both of video and audio frame */
1178                                         if (info->video_frame_count >= hstreamrecorder->ini.minimum_frame && info_audio->audio_frame_count) {
1179                                                 break;
1180                                         } else if (count == hstreamrecorder->ini.retrial_count) {
1181                                                 _mmstreamrec_dbg_err("Commit fail, VIDEO[%" G_GUINT64_FORMAT "], AUDIO [%" G_GUINT64_FORMAT "]", info->video_frame_count, info_audio->audio_frame_count);
1182
1183                                                 info->b_commiting = FALSE;
1184                                                 return MM_ERROR_STREAMRECORDER_INVALID_CONDITION;
1185                                         } else {
1186                                                 _mmstreamrec_dbg_warn("Waiting for enough frames, retrial [%d], VIDEO[%" G_GUINT64_FORMAT "], AUDIO [%" G_GUINT64_FORMAT "]", count, info->video_frame_count, info_audio->audio_frame_count);
1187                                         }
1188
1189                                         usleep(hstreamrecorder->ini.video_frame_wait_time);
1190                                 }
1191                         }
1192
1193                         if (sc->error_occurs) {
1194                                 GstPad *video = NULL;
1195                                 GstPad *audio = NULL;
1196
1197                                 _mmstreamrec_dbg_err("Committing Error case");
1198 #if 0
1199                                 video = gst_element_get_static_pad(sc->element[_MMSTREAMRECORDER_VIDEOSINK_SINK].gst, "sink");
1200                                 ret = gst_pad_send_event(video, gst_event_new_eos());
1201                                 _mmstreamrec_dbg_err("Sending EOS video sink  : %d", ret);
1202                                 gst_object_unref(video);
1203 #endif
1204                                 video = gst_element_get_static_pad(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_VENC].gst, "src");
1205                                 gst_pad_push_event(video, gst_event_new_flush_start());
1206                                 gst_pad_push_event(video, gst_event_new_flush_stop(TRUE));
1207                                 ret = gst_pad_push_event(video, gst_event_new_eos());
1208                                 _mmstreamrec_dbg_err("Sending EOS video encoder src pad  : %d", ret);
1209                                 gst_object_unref(video);
1210
1211                                 if (sc->audio_enable == TRUE) {
1212                                         audio = gst_element_get_static_pad(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_AENC].gst, "src");
1213                                         gst_pad_push_event(audio, gst_event_new_flush_start());
1214                                         gst_pad_push_event(audio, gst_event_new_flush_stop(TRUE));
1215                                         ret = gst_element_send_event(sc->encode_element[_MMSTREAMRECORDER_AUDIOSRC_SRC].gst, gst_event_new_eos());
1216                                         _mmstreamrec_dbg_err("Sending EOS audio encoder src pad  : %d", ret);
1217                                         gst_object_unref(audio);
1218                                 }
1219                         } else {
1220                                 if (sc->encode_element[_MMSTREAMRECORDER_ENCSINK_SRC].gst != NULL) {
1221                                         ret = gst_element_send_event(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_SRC].gst, gst_event_new_eos());
1222                                         _mmstreamrec_dbg_warn("send eos to appsrc result : %d", ret);
1223                                 }
1224
1225                                 if (sc->encode_element[_MMSTREAMRECORDER_AUDIOSRC_SRC].gst != NULL) {
1226                                         pad = gst_element_get_static_pad(sc->encode_element[_MMSTREAMRECORDER_AUDIOSRC_SRC].gst, "src");
1227                                         ret = gst_element_send_event(sc->encode_element[_MMSTREAMRECORDER_AUDIOSRC_SRC].gst, gst_event_new_eos());
1228                                         gst_object_unref(pad);
1229                                         pad = NULL;
1230
1231                                         _mmstreamrec_dbg_warn("send eos to audiosrc result : %d", ret);
1232                                 }
1233                         }
1234
1235                         /* Wait EOS */
1236                         _mmstreamrec_dbg_log("Start to wait EOS");
1237                         ret = _mmstreamrecorder_get_eos_message(handle);
1238                         if (ret != MM_ERROR_NONE) {
1239                                 info->b_commiting = FALSE;
1240                                 goto _ERR_STREAMRECORDER_VIDEO_COMMAND;
1241                         }
1242                 }
1243                 break;
1244         default:
1245                 ret = MM_ERROR_STREAMRECORDER_INVALID_ARGUMENT;
1246                 break;
1247         }
1248         return MM_ERROR_NONE;
1249
1250  _ERR_STREAMRECORDER_VIDEO_COMMAND:
1251         if (ret != MM_ERROR_NONE)
1252                 _mmstreamrec_dbg_err("Current Videosrc status");
1253
1254         return ret;
1255 }
1256
1257 int _mmstreamrecorder_video_handle_eos(MMHandleType handle)
1258 {
1259         int ret = MM_ERROR_NONE;
1260
1261         mmf_streamrecorder_t *hstreamrecorder = MMF_STREAMRECORDER(handle);
1262         _MMStreamRecorderSubContext *sc = NULL;
1263         _MMStreamRecorderVideoInfo *info = NULL;
1264         _MMStreamRecorderAudioInfo *info_audio = NULL;
1265         _MMStreamRecorderFileInfo *finfo = NULL;
1266         _MMStreamRecorderMsgItem msg;
1267         MMStreamRecordingReport *report = NULL;
1268
1269         mmf_return_val_if_fail(hstreamrecorder, FALSE);
1270
1271         sc = MMF_STREAMRECORDER_SUBCONTEXT(handle);
1272         mmf_return_val_if_fail(sc, FALSE);
1273         mmf_return_val_if_fail(sc->info_video, FALSE);
1274         if (sc->audio_enable == TRUE) {
1275                 mmf_return_val_if_fail(sc->info_audio, FALSE);
1276         }
1277
1278         mmf_return_val_if_fail(sc->info_file, FALSE);
1279
1280         info = sc->info_video;
1281         if (sc->audio_enable == TRUE)
1282                 info_audio = sc->info_audio;
1283
1284         finfo = sc->info_file;
1285
1286         _mmstreamrec_dbg_err("");
1287
1288         /* remove blocking part */
1289         MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "block", FALSE);
1290
1291         ret = _mmstreamrecorder_destroy_recorder_pipeline(handle);
1292         if (ret != MM_ERROR_NONE)
1293                 _mmstreamrec_dbg_warn("_mmstreamrecorder_destroy_recorder_pipeline failed. error[%x]", ret);
1294
1295         /* Send recording report to application */
1296         msg.id = MM_MESSAGE_STREAMRECORDER_VIDEO_CAPTURED;
1297         report = (MMStreamRecordingReport *) g_malloc(sizeof(MMStreamRecordingReport));
1298         if (!report) {
1299                 _mmstreamrec_dbg_err("Recording report fail(%s). Out of memory.", finfo->filename);
1300         } else {
1301                 report->recording_filename = g_strdup(finfo->filename);
1302                 msg.param.data = report;
1303                 msg.param.code = 1;
1304                 _mmstreamrecorder_send_message((MMHandleType) hstreamrecorder, &msg);
1305         }
1306
1307         /* Finishing */
1308         sc->pipeline_time = 0;
1309         sc->pause_time = 0;
1310         sc->isMaxsizePausing = FALSE;   /*In async function, this variable should set in callback function. */
1311         sc->isMaxtimePausing = FALSE;
1312         sc->error_occurs = FALSE;
1313
1314         info->video_frame_count = 0;
1315         if (info_audio)
1316                 info_audio->audio_frame_count = 0;
1317
1318         info->filesize = 0;
1319         g_free(finfo->filename);
1320         finfo->filename = NULL;
1321         info->b_commiting = FALSE;
1322
1323         _mmstreamrec_dbg_err("_mmstreamrecorder_video_handle_eos : end");
1324
1325         return TRUE;
1326 }
1327
1328 /* AUDIO */
1329
1330 int _mmstreamrecorder_create_audio_pipeline(MMHandleType handle)
1331 {
1332         mmf_streamrecorder_t *hstreamrecorder = MMF_STREAMRECORDER(handle);
1333         _MMStreamRecorderSubContext *sc = NULL;
1334
1335         mmf_return_val_if_fail(hstreamrecorder, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
1336         sc = MMF_STREAMRECORDER_SUBCONTEXT(handle);
1337
1338         mmf_return_val_if_fail(sc, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
1339
1340         return _mmstreamrecorder_create_audiop_with_encodebin(handle);
1341 }
1342
1343 /**
1344  * This function destroy audio pipeline.
1345  *
1346  * @param[in]   handle          Handle of streamrecorder.
1347  * @return      void
1348  * @remarks
1349  * @see         _mmstreamrecorder_destroy_audio_pipeline()
1350  *
1351  */
1352 void _mmstreamrecorder_destroy_audio_pipeline(MMHandleType handle)
1353 {
1354         mmf_streamrecorder_t *hstreamrecorder = MMF_STREAMRECORDER(handle);
1355         _MMStreamRecorderSubContext *sc = NULL;
1356         _MMStreamRecorderAudioInfo *info = NULL;
1357         mmf_return_if_fail(hstreamrecorder);
1358         sc = MMF_STREAMRECORDER_SUBCONTEXT(handle);
1359
1360         mmf_return_if_fail(sc && sc->info_audio);
1361
1362         info = sc->info_audio;
1363
1364         _mmstreamrec_dbg_log("start");
1365
1366         if (sc->encode_element[_MMSTREAMRECORDER_ENCODE_MAIN_PIPE].gst) {
1367                 _mmstreamrec_dbg_warn("release audio pipeline");
1368
1369                 _mmstreamrecorder_gst_set_state(handle, sc->encode_element[_MMSTREAMRECORDER_ENCODE_MAIN_PIPE].gst, GST_STATE_NULL);
1370
1371                 _mmstreamrecorder_remove_all_handlers((MMHandleType) hstreamrecorder, _MMSTREAMRECORDER_HANDLER_CATEGORY_ALL);
1372
1373                 if (info->bMuxing) {
1374                         GstPad *reqpad = NULL;
1375                         /* FIXME:
1376                            Release request pad
1377                            The ref_count of mux is always # of streams in here, i don't know why it happens.
1378                            So, i unref the mux manually
1379                          */
1380                         reqpad = gst_element_get_static_pad(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "audio");
1381                         gst_element_release_request_pad(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, reqpad);
1382                         gst_object_unref(reqpad);
1383
1384                         if (GST_IS_ELEMENT(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_MUX].gst) && GST_OBJECT_REFCOUNT(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_MUX].gst) > 1) {
1385                                 gst_object_unref(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_MUX].gst);
1386                         }
1387                 }
1388                 gst_object_unref(sc->encode_element[_MMSTREAMRECORDER_ENCODE_MAIN_PIPE].gst);
1389         }
1390
1391         _mmstreamrec_dbg_log("done");
1392
1393         return;
1394 }
1395
1396 int _mmstreamrecorder_create_audiop_with_encodebin(MMHandleType handle)
1397 {
1398         int err = MM_ERROR_NONE;
1399         char *aenc_name = NULL;
1400         char *mux_name = NULL;
1401         char *err_name = NULL;
1402         int rec_mode = 0;
1403
1404         GstBus *bus = NULL;
1405         GstPad *srcpad = NULL;
1406         GstPad *sinkpad = NULL;
1407         GList *element_list = NULL;
1408
1409         _MMStreamRecorderAudioInfo *info = NULL;
1410         mmf_streamrecorder_t *hstreamrecorder = MMF_STREAMRECORDER(handle);
1411         _MMStreamRecorderSubContext *sc = NULL;
1412         /* type_element *aenc_elem = NULL; */
1413         /* type_element *mux_elem = NULL; */
1414
1415         mmf_return_val_if_fail(hstreamrecorder, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
1416         sc = MMF_STREAMRECORDER_SUBCONTEXT(handle);
1417
1418         mmf_return_val_if_fail(sc, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
1419         mmf_return_val_if_fail(sc->info_audio, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
1420
1421         info = (_MMStreamRecorderAudioInfo *) sc->info_audio;
1422
1423         _mmstreamrec_dbg_log("");
1424
1425         err = mm_streamrecorder_get_attributes(handle, &err_name, MMSTR_RECORDER_MODE, &rec_mode, NULL);
1426
1427         if (!mux_name || !strcmp(mux_name, "wavenc")) {
1428                 /* IF MUX in not chosen then record in raw file */
1429                 _mmstreamrec_dbg_log("Record without muxing.");
1430                 info->bMuxing = FALSE;
1431         } else {
1432                 _mmstreamrec_dbg_log("Record with mux.");
1433                 info->bMuxing = TRUE;
1434         }
1435
1436         /* Create GStreamer pipeline */
1437         _MMSTREAMRECORDER_PIPELINE_MAKE(sc, sc->encode_element, _MMSTREAMRECORDER_ENCODE_MAIN_PIPE, "recorder_pipeline", err);
1438
1439         err = _mmstreamrecorder_create_audiosrc_bin(handle);
1440         if (err != MM_ERROR_NONE)
1441                 return err;
1442
1443         if (info->bMuxing) {
1444                 /* Muxing. can use encodebin. */
1445                 err = _mmstreamrecorder_create_encodesink_bin((MMHandleType) hstreamrecorder, MM_STREAMRECORDER_ENCBIN_PROFILE_AUDIO);
1446                 if (err != MM_ERROR_NONE)
1447                         return err;
1448
1449         } else {
1450                 /* without muxing. can't use encodebin. */
1451
1452                 _MMSTREAMRECORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMSTREAMRECORDER_ENCSINK_AQUE, "queue", NULL, element_list, err);
1453
1454                 if (rec_mode == MM_STREAMRECORDER_MODE_MEDIABUFFER) {
1455                         if (strcmp(hstreamrecorder->ini.name_of_encsink_bin_audio_encoder, "wavenc") != 0) {
1456                                 _MMSTREAMRECORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMSTREAMRECORDER_ENCSINK_CONV, "audioconvert", NULL, element_list, err);
1457                         }
1458                 }
1459
1460                 _MMSTREAMRECORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMSTREAMRECORDER_ENCSINK_AENC, aenc_name, NULL, element_list, err);
1461
1462                 _MMSTREAMRECORDER_ELEMENT_MAKE(sc, sc->encode_element, _MMSTREAMRECORDER_ENCSINK_SINK, hstreamrecorder->ini.name_of_encsink_sink, NULL, element_list, err);
1463         }
1464
1465         /* Add and link elements */
1466         if (info->bMuxing) {
1467                 /* IF MUX is indicated create MUX */
1468                 gst_bin_add_many(GST_BIN(sc->encode_element[_MMSTREAMRECORDER_ENCODE_MAIN_PIPE].gst), sc->encode_element[_MMSTREAMRECORDER_AUDIOSRC_BIN].gst, sc->encode_element[_MMSTREAMRECORDER_ENCSINK_BIN].gst, NULL);
1469
1470                 srcpad = gst_element_get_static_pad(sc->encode_element[_MMSTREAMRECORDER_AUDIOSRC_BIN].gst, "src");
1471                 sinkpad = gst_element_get_static_pad(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_BIN].gst, "audio_sink0");
1472                 _MM_GST_PAD_LINK_UNREF(srcpad, sinkpad, err, pipeline_creation_error);
1473         } else {
1474                 /* IF MUX in not chosen then record in raw amr file */
1475                 if (!strcmp(aenc_name, "wavenc")) {
1476                         gst_bin_add_many(GST_BIN(sc->encode_element[_MMSTREAMRECORDER_ENCODE_MAIN_PIPE].gst), sc->encode_element[_MMSTREAMRECORDER_AUDIOSRC_BIN].gst, sc->encode_element[_MMSTREAMRECORDER_ENCSINK_AQUE].gst, sc->encode_element[_MMSTREAMRECORDER_ENCSINK_AENC].gst, sc->encode_element[_MMSTREAMRECORDER_ENCSINK_SINK].gst, NULL);
1477
1478                         if (!_MM_GST_ELEMENT_LINK_MANY(sc->encode_element[_MMSTREAMRECORDER_AUDIOSRC_BIN].gst, sc->encode_element[_MMSTREAMRECORDER_ENCSINK_AQUE].gst, sc->encode_element[_MMSTREAMRECORDER_ENCSINK_AENC].gst, sc->encode_element[_MMSTREAMRECORDER_ENCSINK_SINK].gst, NULL)) {
1479                                 err = MM_ERROR_STREAMRECORDER_GST_LINK;
1480                                 goto pipeline_creation_error;
1481                         }
1482                 } else {
1483                         if (rec_mode == MM_STREAMRECORDER_MODE_MEDIABUFFER) {
1484                                 gst_bin_add_many(GST_BIN(sc->encode_element[_MMSTREAMRECORDER_ENCODE_MAIN_PIPE].gst), sc->encode_element[_MMSTREAMRECORDER_AUDIOSRC_BIN].gst, sc->encode_element[_MMSTREAMRECORDER_ENCSINK_AQUE].gst, sc->encode_element[_MMSTREAMRECORDER_ENCSINK_CONV].gst, sc->encode_element[_MMSTREAMRECORDER_ENCSINK_AENC].gst, sc->encode_element[_MMSTREAMRECORDER_ENCSINK_SINK].gst, NULL);
1485
1486                                 if (!_MM_GST_ELEMENT_LINK_MANY(sc->encode_element[_MMSTREAMRECORDER_AUDIOSRC_BIN].gst, sc->encode_element[_MMSTREAMRECORDER_ENCSINK_AQUE].gst, sc->encode_element[_MMSTREAMRECORDER_ENCSINK_CONV].gst, sc->encode_element[_MMSTREAMRECORDER_ENCSINK_AENC].gst, sc->encode_element[_MMSTREAMRECORDER_ENCSINK_SINK].gst, NULL)) {
1487                                         err = MM_ERROR_STREAMRECORDER_GST_LINK;
1488                                         goto pipeline_creation_error;
1489                                 }
1490                         } else {
1491                                 gst_bin_add_many(GST_BIN(sc->encode_element[_MMSTREAMRECORDER_ENCODE_MAIN_PIPE].gst), sc->encode_element[_MMSTREAMRECORDER_AUDIOSRC_BIN].gst, sc->encode_element[_MMSTREAMRECORDER_ENCSINK_AQUE].gst, sc->encode_element[_MMSTREAMRECORDER_ENCSINK_AENC].gst, sc->encode_element[_MMSTREAMRECORDER_ENCSINK_SINK].gst, NULL);
1492
1493                                 if (!_MM_GST_ELEMENT_LINK_MANY(sc->encode_element[_MMSTREAMRECORDER_AUDIOSRC_BIN].gst, sc->encode_element[_MMSTREAMRECORDER_ENCSINK_AQUE].gst, sc->encode_element[_MMSTREAMRECORDER_ENCSINK_AENC].gst, sc->encode_element[_MMSTREAMRECORDER_ENCSINK_SINK].gst, NULL)) {
1494                                         err = MM_ERROR_STREAMRECORDER_GST_LINK;
1495                                         goto pipeline_creation_error;
1496                                 }
1497                         }
1498                 }
1499         }
1500
1501         if (info->bMuxing) {
1502                 MMSTREAMRECORDER_SIGNAL_CONNECT(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_MUX].gst, _MMSTREAMRECORDER_HANDLER_AUDIOREC, "pad-added", __mmstreamrecorder_audiorec_pad_added_cb, hstreamrecorder);
1503         } else {
1504                 srcpad = gst_element_get_static_pad(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_AENC].gst, "src");
1505                 MMSTREAMRECORDER_ADD_BUFFER_PROBE(srcpad, _MMSTREAMRECORDER_HANDLER_AUDIOREC, __mmstreamrecorder_audio_dataprobe_record, hstreamrecorder);
1506                 gst_object_unref(srcpad);
1507                 srcpad = NULL;
1508         }
1509
1510         bus = gst_pipeline_get_bus(GST_PIPELINE(sc->encode_element[_MMSTREAMRECORDER_ENCODE_MAIN_PIPE].gst));
1511
1512         /* register message callback  */
1513         hstreamrecorder->pipeline_cb_event_id = gst_bus_add_watch(bus, (GstBusFunc) _mmstreamrecorder_pipeline_cb_message, hstreamrecorder);
1514
1515         /* set sync callback */
1516         gst_bus_set_sync_handler(bus, gst_bus_sync_signal_handler, hstreamrecorder, NULL);
1517
1518         gst_object_unref(bus);
1519         bus = NULL;
1520
1521         if (element_list) {
1522                 g_list_free(element_list);
1523                 element_list = NULL;
1524         }
1525
1526         return MM_ERROR_NONE;
1527
1528  pipeline_creation_error:
1529         _MMSTREAMRECORDER_ELEMENT_REMOVE(sc->encode_element, _MMSTREAMRECORDER_ENCODE_MAIN_PIPE);
1530         _MMSTREAMRECORDER_ELEMENT_REMOVE(sc->encode_element, _MMSTREAMRECORDER_AUDIOSRC_BIN);
1531         _MMSTREAMRECORDER_ELEMENT_REMOVE(sc->encode_element, _MMSTREAMRECORDER_ENCSINK_AQUE);
1532         if (rec_mode == MM_STREAMRECORDER_MODE_MEDIABUFFER) {
1533                 _MMSTREAMRECORDER_ELEMENT_REMOVE(sc->encode_element, _MMSTREAMRECORDER_ENCSINK_CONV);
1534         }
1535         _MMSTREAMRECORDER_ELEMENT_REMOVE(sc->encode_element, _MMSTREAMRECORDER_ENCSINK_AENC);
1536         _MMSTREAMRECORDER_ELEMENT_REMOVE(sc->encode_element, _MMSTREAMRECORDER_ENCSINK_SINK);
1537         _MMSTREAMRECORDER_ELEMENT_REMOVE(sc->encode_element, _MMSTREAMRECORDER_ENCSINK_BIN);
1538
1539         if (element_list) {
1540                 g_list_free(element_list);
1541                 element_list = NULL;
1542         }
1543
1544         return err;
1545 }
1546
1547 int _mmstreamrecorder_audio_command(MMHandleType handle, int command)
1548 {
1549         int cmd = command;
1550         int ret = MM_ERROR_NONE;
1551         int err = 0;
1552         guint64 free_space = 0;
1553         guint64 cal_space = 0;
1554         char *dir_name = NULL;
1555         char *err_attr_name = NULL;
1556         guint count = 0;
1557         int size = 0;
1558
1559         GstElement *pipeline = NULL;
1560         GstElement *audioSrc = NULL;
1561
1562         mmf_streamrecorder_t *hstreamrecorder = MMF_STREAMRECORDER(handle);
1563         _MMStreamRecorderSubContext *sc = NULL;
1564         _MMStreamRecorderAudioInfo *info = NULL;
1565         _MMStreamRecorderFileInfo *finfo = NULL;
1566
1567         mmf_return_val_if_fail(hstreamrecorder, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
1568         sc = MMF_STREAMRECORDER_SUBCONTEXT(handle);
1569
1570         mmf_return_val_if_fail(sc, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
1571         mmf_return_val_if_fail(sc->info_audio, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
1572         mmf_return_val_if_fail(sc->info_file, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
1573         pipeline = sc->encode_element[_MMSTREAMRECORDER_ENCODE_MAIN_PIPE].gst;
1574         info = sc->info_audio;
1575         finfo = sc->info_file;
1576
1577         _mmstreamrec_dbg_log("");
1578
1579         pipeline = sc->encode_element[_MMSTREAMRECORDER_ENCODE_MAIN_PIPE].gst;
1580         audioSrc = sc->encode_element[_MMSTREAMRECORDER_AUDIOSRC_SRC].gst;
1581         switch (cmd) {
1582         case _MM_STREAMRECORDER_CMD_RECORD:
1583                 /* check status for resume case */
1584                 {
1585                         guint imax_size = 0;
1586                         guint imax_time = 0;
1587                         char *temp_filename = NULL;
1588
1589                         if (sc->pipeline_time)
1590                                 gst_element_set_start_time((GstElement *) GST_PIPELINE(pipeline), sc->pipeline_time);
1591
1592                         sc->pipeline_time = hstreamrecorder->ini.reset_pause_time;
1593
1594                         ret = mm_streamrecorder_get_attributes(handle, &err_attr_name, MMSTR_TARGET_MAX_SIZE, &imax_size, MMSTR_TARGET_TIME_LIMIT, &imax_time, MMSTR_FILE_FORMAT, &(finfo->fileformat), MMSTR_FILENAME, &temp_filename, &size, NULL);
1595                         if (ret != MM_ERROR_NONE) {
1596                                 _mmstreamrec_dbg_warn("failed to get attribute. (%s:%x)", err_attr_name, ret);
1597                                 SAFE_FREE(err_attr_name);
1598                                 goto _ERR_STREAMRECORDER_AUDIO_COMMAND;
1599                         }
1600
1601                         finfo->filename = g_strdup(temp_filename);
1602                         if (!finfo->filename) {
1603                                 _mmstreamrec_dbg_err("STRDUP was failed");
1604                                 goto _ERR_STREAMRECORDER_AUDIO_COMMAND;
1605                         }
1606
1607                         _mmstreamrec_dbg_log("Record start : set file name using attribute - %s\n ", finfo->filename);
1608
1609                         MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_SINK].gst, "location", finfo->filename);
1610
1611                         sc->ferror_send = FALSE;
1612                         sc->ferror_count = 0;
1613                         sc->bget_eos = FALSE;
1614                         info->filesize = 0;
1615
1616                         /* set max size */
1617                         if (imax_size <= 0)
1618                                 info->max_size = 0;     /* do not check */
1619                         else
1620                                 info->max_size = ((guint64) imax_size) << 10;   /* to byte */
1621
1622                         /* set max time */
1623                         if (imax_time <= 0)
1624                                 info->max_time = 0;     /* do not check */
1625                         else
1626                                 info->max_time = ((guint64) imax_time) * 1000;  /* to millisecond */
1627
1628                         /* TODO : check free space before recording start, need to more discussion */
1629                         dir_name = g_path_get_dirname(finfo->filename);
1630                         err = _mmstreamrecorder_get_freespace(dir_name, &free_space);
1631
1632                         _mmstreamrec_dbg_warn("current space for recording - %s : [%" G_GUINT64_FORMAT "]", dir_name, free_space);
1633
1634                         if (dir_name) {
1635                                 g_free(dir_name);
1636                                 dir_name = NULL;
1637                         }
1638                         cal_space = (guint64)(hstreamrecorder->ini.audio_frame_minimum_space);
1639                         cal_space = cal_space + (5 * 1024);
1640                         if ((err == -1) || free_space <= cal_space) {
1641                                 _mmstreamrec_dbg_err("No more space for recording");
1642                                 return MM_MESSAGE_STREAMRECORDER_NO_FREE_SPACE;
1643                         }
1644                 }
1645
1646                 ret = _mmstreamrecorder_gst_set_state(handle, pipeline, GST_STATE_PLAYING);
1647                 if (ret != MM_ERROR_NONE)
1648                         goto _ERR_STREAMRECORDER_AUDIO_COMMAND;
1649
1650                 break;
1651
1652         case _MM_STREAMRECORDER_CMD_PAUSE:
1653                 {
1654                         GstClock *l_clock = NULL;
1655
1656                         if (info->b_commiting) {
1657                                 _mmstreamrec_dbg_warn("now on commiting previous file!!(cmd : %d)", cmd);
1658                                 return MM_ERROR_STREAMRECORDER_CMD_IS_RUNNING;
1659                         }
1660
1661                         for (count = 0; count <= hstreamrecorder->ini.retrial_count; count++) {
1662                                 if (info->filesize > 0) {
1663                                         break;
1664                                 } else if (count == hstreamrecorder->ini.retrial_count) {
1665                                         _mmstreamrec_dbg_err("Pause fail, wait 200 ms, but file size is %lld", info->filesize);
1666                                         return MM_ERROR_STREAMRECORDER_INVALID_CONDITION;
1667                                 } else {
1668                                         _mmstreamrec_dbg_warn("Wait for enough audio frame, retry count[%d], file size is %lld", count, info->filesize);
1669                                 }
1670                                 usleep(hstreamrecorder->ini.audio_frame_wait_time);
1671                         }
1672
1673                         ret = _mmstreamrecorder_gst_set_state(handle, pipeline, GST_STATE_PAUSED);
1674                         if (ret != MM_ERROR_NONE)
1675                                 goto _ERR_STREAMRECORDER_AUDIO_COMMAND;
1676
1677                         /* FIXME: consider delay. */
1678                         l_clock = gst_pipeline_get_clock(GST_PIPELINE(pipeline));
1679                         sc->pipeline_time = gst_clock_get_time(l_clock) - gst_element_get_base_time(GST_ELEMENT(pipeline));
1680                         break;
1681                 }
1682
1683         case _MM_STREAMRECORDER_CMD_CANCEL:
1684                 if (info->b_commiting) {
1685                         _mmstreamrec_dbg_warn("now on commiting previous file!!(cmd : %d)", cmd);
1686                         return MM_ERROR_STREAMRECORDER_CMD_IS_RUNNING;
1687                 }
1688
1689                 ret = _mmstreamrecorder_gst_set_state(handle, pipeline, GST_STATE_READY);
1690                 if (ret != MM_ERROR_NONE)
1691                         goto _ERR_STREAMRECORDER_AUDIO_COMMAND;
1692
1693                 if (info->bMuxing) {
1694                         MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "block", FALSE);
1695                 } else {
1696                         MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_AQUE].gst, "empty-buffers", FALSE);
1697                 }
1698
1699                 _mmstreamrecorder_gst_set_state(handle, sc->encode_element[_MMSTREAMRECORDER_ENCSINK_SINK].gst, GST_STATE_NULL);
1700
1701                 sc->pipeline_time = 0;
1702                 sc->pause_time = 0;
1703                 sc->isMaxsizePausing = FALSE;
1704                 sc->isMaxtimePausing = FALSE;
1705
1706                 if (finfo->filename) {
1707                         _mmstreamrec_dbg_log("file delete(%s)", finfo->filename);
1708                         unlink(finfo->filename);
1709                         g_free(finfo->filename);
1710                         finfo->filename = NULL;
1711                 }
1712                 break;
1713
1714         case _MM_STREAMRECORDER_CMD_COMMIT:
1715                 {
1716
1717                         _mmstreamrec_dbg_log("_MM_STREAMRECORDER_CMD_COMMIT");
1718
1719                         if (info->b_commiting) {
1720                                 _mmstreamrec_dbg_warn("now on commiting previous file!!(cmd : %d)", cmd);
1721                                 return MM_ERROR_STREAMRECORDER_CMD_IS_RUNNING;
1722                         } else {
1723                                 _mmstreamrec_dbg_log("_MM_STREAMRECORDER_CMD_COMMIT : start");
1724                                 info->b_commiting = TRUE;
1725                         }
1726
1727                         for (count = 0; count <= hstreamrecorder->ini.retrial_count; count++) {
1728                                 if (info->filesize > 0) {
1729                                         break;
1730                                 } else if (count == hstreamrecorder->ini.retrial_count) {
1731                                         _mmstreamrec_dbg_err("Commit fail, waited 200 ms, but file size is %lld", info->filesize);
1732                                         info->b_commiting = FALSE;
1733                                         return MM_ERROR_STREAMRECORDER_INVALID_CONDITION;
1734                                 } else {
1735                                         _mmstreamrec_dbg_warn("Waiting for enough audio frame, re-count[%d], file size is %lld", count, info->filesize);
1736                                 }
1737                                 usleep(hstreamrecorder->ini.audio_frame_wait_time);
1738                         }
1739
1740                         if (audioSrc) {
1741                                 GstPad *pad = gst_element_get_static_pad(audioSrc, "src");
1742                                 ret = gst_element_send_event(audioSrc, gst_event_new_eos());
1743                                 gst_object_unref(pad);
1744                                 pad = NULL;
1745                                 /* for pause -> commit case */
1746                                 /*if (_mmstreamrecorder_get_state((MMHandleType)hstreamrecorder) == MM_STREAMRECORDER_STATE_PAUSED) {
1747                                    ret = _mmstreamrecorder_gst_set_state(handle, pipeline, GST_STATE_PLAYING);
1748                                    if (ret != MM_ERROR_NONE) {
1749                                    goto _ERR_STREAMRECORDER_AUDIO_COMMAND;
1750                                    }
1751                                    } */
1752                         }
1753
1754                         /* wait until finishing EOS */
1755                         _mmstreamrec_dbg_log("Start to wait EOS");
1756                         if ((ret = _mmstreamrecorder_get_eos_message(handle)) != MM_ERROR_NONE)
1757                                 goto _ERR_STREAMRECORDER_AUDIO_COMMAND;
1758
1759                         break;
1760                 }
1761         default:
1762                 ret = MM_ERROR_STREAMRECORDER_INVALID_ARGUMENT;
1763                 break;
1764         }
1765
1766  _ERR_STREAMRECORDER_AUDIO_COMMAND:
1767         return ret;
1768 }
1769
1770 int _mmstreamrecorder_audio_handle_eos(MMHandleType handle)
1771 {
1772         int err = MM_ERROR_NONE;
1773         mmf_streamrecorder_t *hstreamrecorder = MMF_STREAMRECORDER(handle);
1774         _MMStreamRecorderSubContext *sc = NULL;
1775         _MMStreamRecorderAudioInfo *info = NULL;
1776         _MMStreamRecorderFileInfo *finfo = NULL;
1777         GstElement *pipeline = NULL;
1778         _MMStreamRecorderMsgItem msg;
1779         MMStreamRecordingReport *report;
1780
1781         mmf_return_val_if_fail(hstreamrecorder, FALSE);
1782         sc = MMF_STREAMRECORDER_SUBCONTEXT(handle);
1783
1784         mmf_return_val_if_fail(sc, FALSE);
1785         mmf_return_val_if_fail(sc->info_audio, FALSE);
1786         mmf_return_val_if_fail(sc->info_file, FALSE);
1787
1788         _mmstreamrec_dbg_err("");
1789
1790         info = sc->info_audio;
1791         finfo = sc->info_file;
1792
1793         pipeline = sc->encode_element[_MMSTREAMRECORDER_ENCODE_MAIN_PIPE].gst;
1794
1795         err = _mmstreamrecorder_gst_set_state(handle, pipeline, GST_STATE_READY);
1796
1797         if (err != MM_ERROR_NONE)
1798                 _mmstreamrec_dbg_warn("Failed:_MM_STREAMRECORDER_CMD_COMMIT:GST_STATE_READY. err[%x]", err);
1799
1800         /* Send recording report message to application */
1801         msg.id = MM_MESSAGE_STREAMRECORDER_AUDIO_CAPTURED;
1802         report = (MMStreamRecordingReport *) g_malloc(sizeof(MMStreamRecordingReport));
1803         if (!report) {
1804                 _mmstreamrec_dbg_err("Recording report fail(%s). Out of memory.", finfo->filename);
1805                 return FALSE;
1806         }
1807
1808         /* START TAG HERE */
1809         /* MM_AUDIO_CODEC_AAC + MM_FILE_FORMAT_MP4 */
1810         if (finfo->fileformat == MM_FILE_FORMAT_3GP || finfo->fileformat == MM_FILE_FORMAT_MP4)
1811                 _mmstreamrecorder_audio_add_metadata_info_m4a(handle);
1812         /* END TAG HERE */
1813
1814         report->recording_filename = g_strdup(finfo->filename);
1815         msg.param.data = report;
1816
1817         _mmstreamrecorder_send_message(handle, &msg);
1818
1819         if (info->bMuxing) {
1820                 MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_ENCBIN].gst, "block", FALSE);
1821         } else {
1822                 MMSTREAMRECORDER_G_OBJECT_SET(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_AQUE].gst, "empty-buffers", FALSE);
1823         }
1824
1825         _mmstreamrecorder_gst_set_state(handle, pipeline, GST_STATE_NULL);
1826
1827         sc->pipeline_time = 0;
1828         sc->pause_time = 0;
1829         sc->isMaxsizePausing = FALSE;
1830         sc->isMaxtimePausing = FALSE;
1831
1832         g_free(finfo->filename);
1833         finfo->filename = NULL;
1834
1835         _mmstreamrec_dbg_err("_MM_STREAMRECORDER_CMD_COMMIT : end");
1836
1837         info->b_commiting = FALSE;
1838
1839         return TRUE;
1840 }
1841
1842 int _mmstreamrecorder_push_videostream_buffer(MMHandleType handle, unsigned long timestamp, GstBuffer *buffer, int size)
1843 {
1844         mmf_streamrecorder_t *hstreamrecorder = MMF_STREAMRECORDER(handle);
1845         _MMStreamRecorderSubContext *sc = NULL;
1846         /* GstPad *srcpad = NULL; */
1847         GstCaps *srccaps = NULL;
1848         char *err_name = NULL;
1849         int video_fps = 0;
1850         int video_src = 0;
1851         int video_width = 0;
1852         int video_height = 0;
1853         int ret = MM_ERROR_NONE;
1854         int video_source_format = 0;
1855
1856         mmf_return_val_if_fail(hstreamrecorder, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
1857
1858         sc = MMF_STREAMRECORDER_SUBCONTEXT(handle);
1859         mmf_return_val_if_fail(sc, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
1860
1861         if (buffer == NULL || size == 0) {
1862                 _mmstreamrec_dbg_err("video : Buffer is %p , size %d, time stamp is %ld", buffer, size, timestamp);
1863                 return MM_ERROR_STREAMRECORDER_RESOURCE_CREATION;
1864         }
1865
1866         _mmstreamrec_dbg_log("video : Buffer is %p , size %d, time stamp is %ld", buffer, size, timestamp);
1867
1868         /* check element availability */
1869         ret = mm_streamrecorder_get_attributes(handle, &err_name, MMSTR_VIDEO_FRAMERATE, &video_fps, MMSTR_VIDEO_SOURCE_FORMAT, &video_src, MMSTR_VIDEO_RESOLUTION_WIDTH, &video_width, MMSTR_VIDEO_RESOLUTION_HEIGHT, &video_height, MMSTR_VIDEO_SOURCE_FORMAT, &video_source_format, NULL);
1870
1871         if (sc->encode_element[_MMSTREAMRECORDER_ENCSINK_SRC].gst) {
1872
1873                 /*_mmstreamrec_dbg_log("Buffer Push start , time stamp %ld",timestamp);*/
1874                 /* srcpad = gst_element_get_static_pad(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_SRC].gst, "src"); */
1875                 /* srccaps = gst_pad_get_current_caps(srcpad); */
1876                 srccaps = gst_set_videosrcpad_caps(video_src, video_width, video_height, video_fps, 1);
1877                 if (srccaps) {
1878                         gst_app_src_set_caps((GstAppSrc *) sc->encode_element[_MMSTREAMRECORDER_ENCSINK_SRC].gst, srccaps);
1879                         gst_caps_unref(srccaps);
1880                         srccaps = NULL;
1881                 }
1882
1883                 /*_mmstreamrec_dbg_err("newbuf streamrecorder(%p) ",newbuf);*/
1884                 ret = gst_app_src_push_buffer((GstAppSrc *) sc->encode_element[_MMSTREAMRECORDER_ENCSINK_SRC].gst, buffer);
1885                 if (ret) {
1886                         _mmstreamrec_dbg_err("video gst_app_src_push_buffer %d", ret);
1887                         ret = MM_ERROR_STREAMRECORDER_VIDEOBUFFER_PUSH;
1888                 }
1889
1890                 /* g_signal_emit_by_name(sc->encode_element[_MMSTREAMRECORDER_ENCSINK_SRC].gst, "push-buffer", newbuf, &ret); */
1891         }
1892
1893         return ret;
1894 }
1895
1896 int _mmstreamrecorder_push_audiostream_buffer(MMHandleType handle, unsigned long timestamp, GstBuffer *buffer, int size)
1897 {
1898         mmf_streamrecorder_t *hstreamrecorder = MMF_STREAMRECORDER(handle);
1899         _MMStreamRecorderSubContext *sc = NULL;
1900         _MMStreamRecorderAudioInfo *info = NULL;
1901         GstFlowReturn err = GST_FLOW_OK;
1902         GstPad *srcpad = NULL;
1903         GstCaps *srccaps = NULL;
1904         int rate = 0;
1905         int channel = 0;
1906         int depth = 0;
1907
1908         mmf_return_val_if_fail(hstreamrecorder, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
1909
1910         sc = MMF_STREAMRECORDER_SUBCONTEXT(handle);
1911         mmf_return_val_if_fail(sc, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
1912
1913         info = (_MMStreamRecorderAudioInfo *) sc->info_audio;
1914         mmf_return_val_if_fail(info, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
1915
1916         rate = info->iSamplingRate;
1917         depth = info->audio_encode_depth;
1918         channel = info->iChannels;
1919
1920         /*_mmstreamrec_dbg_log("Audio Buffer Push start , time stamp %ld",timestamp); */
1921
1922         if (sc->encode_element[_MMSTREAMRECORDER_AUDIOSRC_SRC].gst) {
1923
1924                 srcpad = gst_element_get_static_pad(sc->encode_element[_MMSTREAMRECORDER_AUDIOSRC_SRC].gst, "src");
1925                 /* TODO : CHANNEL , WIDTH, DATATYPE */
1926                 srccaps = gst_pad_get_current_caps(srcpad);
1927                 srccaps = gst_set_audiosrcpad_caps(rate, channel, depth, 16, 1);
1928                 gst_base_src_set_caps(GST_BASE_SRC(srcpad), srccaps);
1929
1930                 err = gst_app_src_push_buffer((GstAppSrc *) sc->encode_element[_MMSTREAMRECORDER_AUDIOSRC_SRC].gst, buffer);
1931
1932                 if (err) {
1933                         _mmstreamrec_dbg_err("Audio gst_app_src_push_buffer %d", err);
1934                         return MM_ERROR_STREAMRECORDER_AUDIOBUFFER_PUSH;
1935                 }
1936         }
1937
1938         return MM_ERROR_NONE;
1939 }
1940