1. Seperate preview pipeline and encoding pipeline
[platform/core/multimedia/libmm-camcorder.git] / src / mm_camcorder_internal.c
1 /*
2  * libmm-camcorder
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jeongmo Yang <jm80.yang@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 /*=======================================================================================
23 |  INCLUDE FILES                                                                        |
24 ========================================================================================*/
25 #include <stdio.h>
26 #include <string.h>
27 #include <gst/gst.h>
28 #include <gst/gstutils.h>
29 #include <gst/gstpad.h>
30
31 #include <mm_error.h>
32 #include "mm_camcorder_internal.h"
33 #include <mm_types.h>
34
35 #include <gst/video/colorbalance.h>
36 #include <gst/video/cameracontrol.h>
37
38 #include <system_info.h>
39
40 /*---------------------------------------------------------------------------------------
41 |    GLOBAL VARIABLE DEFINITIONS for internal                                           |
42 ---------------------------------------------------------------------------------------*/
43
44 /*---------------------------------------------------------------------------------------
45 |    LOCAL VARIABLE DEFINITIONS for internal                                            |
46 ---------------------------------------------------------------------------------------*/
47 #define __MMCAMCORDER_CMD_ITERATE_MAX           3
48 #define __MMCAMCORDER_SET_GST_STATE_TIMEOUT     3
49 #define __MMCAMCORDER_SOUND_WAIT_TIMEOUT        3
50
51 /*---------------------------------------------------------------------------------------
52 |    LOCAL FUNCTION PROTOTYPES:                                                         |
53 ---------------------------------------------------------------------------------------*/
54 /* STATIC INTERNAL FUNCTION */
55 static gboolean __mmcamcorder_gstreamer_init(camera_conf * conf);
56
57 static gboolean __mmcamcorder_handle_gst_error(MMHandleType handle, GstMessage *message, GError *error);
58 static gint     __mmcamcorder_gst_handle_stream_error(MMHandleType handle, int code, GstMessage *message);
59 static gint     __mmcamcorder_gst_handle_resource_error(MMHandleType handle, int code, GstMessage *message);
60 static gint     __mmcamcorder_gst_handle_library_error(MMHandleType handle, int code, GstMessage *message);
61 static gint     __mmcamcorder_gst_handle_core_error(MMHandleType handle, int code, GstMessage *message);
62 static gint     __mmcamcorder_gst_handle_resource_warning(MMHandleType handle, GstMessage *message , GError *error);
63 static gboolean __mmcamcorder_handle_gst_warning(MMHandleType handle, GstMessage *message, GError *error);
64
65
66 #ifdef _MMCAMCORDER_USE_SET_ATTR_CB
67 static gboolean __mmcamcorder_set_attr_to_camsensor_cb(gpointer data);
68 #endif /* _MMCAMCORDER_USE_SET_ATTR_CB */
69
70 /*=======================================================================================
71 |  FUNCTION DEFINITIONS                                                                 |
72 =======================================================================================*/
73 /*---------------------------------------------------------------------------------------
74 |    GLOBAL FUNCTION DEFINITIONS:                                                       |
75 ---------------------------------------------------------------------------------------*/
76
77 /* Internal command functions {*/
78 int _mmcamcorder_create(MMHandleType *handle, MMCamPreset *info)
79 {
80         int ret = MM_ERROR_NONE;
81         int UseConfCtrl = 0;
82         int rcmd_fmt_capture = MM_PIXEL_FORMAT_YUYV;
83         int rcmd_fmt_recording = MM_PIXEL_FORMAT_NV12;
84         int rcmd_dpy_rotation = MM_DISPLAY_ROTATION_270;
85         int play_capture_sound = TRUE;
86         int camera_device_count = MM_VIDEO_DEVICE_NUM;
87         int camera_facing_direction = MM_CAMCORDER_CAMERA_FACING_DIRECTION_REAR;
88         char *err_attr_name = NULL;
89         const char *ConfCtrlFile = NULL;
90         mmf_camcorder_t *hcamcorder = NULL;
91         type_element *EvasSurfaceElement = NULL;
92
93         _mmcam_dbg_log("Entered");
94
95         mmf_return_val_if_fail(handle, MM_ERROR_CAMCORDER_INVALID_ARGUMENT);
96         mmf_return_val_if_fail(info, MM_ERROR_CAMCORDER_INVALID_ARGUMENT);
97
98         /* Create mmf_camcorder_t handle and initialize every variable */
99         hcamcorder = (mmf_camcorder_t *)malloc(sizeof(mmf_camcorder_t));
100         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_LOW_MEMORY);
101         memset(hcamcorder, 0x00, sizeof(mmf_camcorder_t));
102
103         /* init values */
104         hcamcorder->type = 0;
105         hcamcorder->state = MM_CAMCORDER_STATE_NONE;
106         hcamcorder->sub_context = NULL;
107         hcamcorder->target_state = MM_CAMCORDER_STATE_NULL;
108         hcamcorder->capture_in_recording = FALSE;
109
110         pthread_mutex_init(&((hcamcorder->mtsafe).lock), NULL);
111         pthread_cond_init(&((hcamcorder->mtsafe).cond), NULL);
112
113         pthread_mutex_init(&((hcamcorder->mtsafe).cmd_lock), NULL);
114         pthread_mutex_init(&((hcamcorder->mtsafe).asm_lock), NULL);
115         pthread_mutex_init(&((hcamcorder->mtsafe).state_lock), NULL);
116         pthread_mutex_init(&((hcamcorder->mtsafe).gst_state_lock), NULL);
117         pthread_mutex_init(&((hcamcorder->mtsafe).gst_encode_state_lock), NULL);
118         pthread_mutex_init(&((hcamcorder->mtsafe).message_cb_lock), NULL);
119         pthread_mutex_init(&((hcamcorder->mtsafe).vcapture_cb_lock), NULL);
120         pthread_mutex_init(&((hcamcorder->mtsafe).vstream_cb_lock), NULL);
121         pthread_mutex_init(&((hcamcorder->mtsafe).astream_cb_lock), NULL);
122
123         pthread_mutex_init(&(hcamcorder->sound_lock), NULL);
124         pthread_cond_init(&(hcamcorder->sound_cond), NULL);
125         pthread_mutex_init(&(hcamcorder->restart_preview_lock), NULL);
126
127         /* Sound mutex/cond init */
128         pthread_mutex_init(&(hcamcorder->snd_info.open_mutex), NULL);
129         pthread_cond_init(&(hcamcorder->snd_info.open_cond), NULL);
130
131         /* init for sound thread */
132         pthread_mutex_init(&(hcamcorder->task_thread_lock), NULL);
133         pthread_cond_init(&(hcamcorder->task_thread_cond), NULL);
134         hcamcorder->task_thread_state = _MMCAMCORDER_SOUND_STATE_NONE;
135
136         /* create task thread */
137         pthread_create(&(hcamcorder->task_thread), NULL, _mmcamcorder_util_task_thread_func, (void *)hcamcorder);
138
139         if (info->videodev_type < MM_VIDEO_DEVICE_NONE ||
140             info->videodev_type >= MM_VIDEO_DEVICE_NUM) {
141                 _mmcam_dbg_err("_mmcamcorder_create::video device type is out of range.");
142                 ret = MM_ERROR_CAMCORDER_INVALID_ARGUMENT;
143                 goto _ERR_DEFAULT_VALUE_INIT;
144         }
145
146         /* set device type */
147         hcamcorder->device_type = info->videodev_type;
148         _mmcam_dbg_warn("Device Type : %d", hcamcorder->device_type);
149
150         /* Get Camera Configure information from Camcorder INI file */
151         _mmcamcorder_conf_get_info((MMHandleType)hcamcorder, CONFIGURE_TYPE_MAIN, CONFIGURE_MAIN_FILE, &hcamcorder->conf_main);
152
153         if (!(hcamcorder->conf_main)) {
154                 _mmcam_dbg_err( "Failed to get configure(main) info." );
155
156                 ret = MM_ERROR_CAMCORDER_CREATE_CONFIGURE;
157                 goto _ERR_AFTER_ASM_REGISTER;
158         }
159
160         hcamcorder->attributes = _mmcamcorder_alloc_attribute((MMHandleType)hcamcorder, info);
161         if (!(hcamcorder->attributes)) {
162                 _mmcam_dbg_err("_mmcamcorder_create::alloc attribute error.");
163
164                 ret = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
165                 goto _ERR_AFTER_ASM_REGISTER;
166         }
167
168         if (info->videodev_type != MM_VIDEO_DEVICE_NONE) {
169                 _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_main,
170                                                 CONFIGURE_CATEGORY_MAIN_VIDEO_INPUT,
171                                                 "UseConfCtrl", &UseConfCtrl);
172
173                 if (UseConfCtrl) {
174                         _mmcam_dbg_log( "Enable Configure Control system." );
175
176                         switch (info->videodev_type) {
177                         case MM_VIDEO_DEVICE_CAMERA0:
178                                 _mmcamcorder_conf_get_value_string((MMHandleType)hcamcorder, hcamcorder->conf_main,
179                                                                    CONFIGURE_CATEGORY_MAIN_VIDEO_INPUT,
180                                                                    "ConfCtrlFile0", &ConfCtrlFile);
181                                 break;
182                         case MM_VIDEO_DEVICE_CAMERA1:
183                                 _mmcamcorder_conf_get_value_string((MMHandleType)hcamcorder, hcamcorder->conf_main,
184                                                                    CONFIGURE_CATEGORY_MAIN_VIDEO_INPUT,
185                                                                    "ConfCtrlFile1", &ConfCtrlFile);
186                                 break;
187                         default:
188                                 _mmcam_dbg_err( "Not supported camera type." );
189                                 ret = MM_ERROR_CAMCORDER_NOT_SUPPORTED;
190                                 goto _ERR_AFTER_ASM_REGISTER;
191                         }
192
193                         _mmcam_dbg_log("videodev_type : [%d], ConfCtrlPath : [%s]", info->videodev_type, ConfCtrlFile);
194
195                         _mmcamcorder_conf_get_info((MMHandleType)hcamcorder, CONFIGURE_TYPE_CTRL, ConfCtrlFile, &hcamcorder->conf_ctrl);
196 /*
197                         _mmcamcorder_conf_print_info(&hcamcorder->conf_main);
198                         _mmcamcorder_conf_print_info(&hcamcorder->conf_ctrl);
199 */
200                         if (!(hcamcorder->conf_ctrl)) {
201                                 _mmcam_dbg_err( "Failed to get configure(control) info." );
202                                 ret = MM_ERROR_CAMCORDER_CREATE_CONFIGURE;
203                                 goto _ERR_AFTER_ASM_REGISTER;
204                         }
205
206                         ret = _mmcamcorder_init_convert_table((MMHandleType)hcamcorder);
207                         if (ret != MM_ERROR_NONE) {
208                                 _mmcam_dbg_warn("converting table initialize error!!");
209                         }
210
211                         ret = _mmcamcorder_init_attr_from_configure((MMHandleType)hcamcorder);
212                         if (ret != MM_ERROR_NONE) {
213                                 _mmcam_dbg_warn("converting table initialize error!!");
214                         }
215
216                         /* Get device info, recommend preview fmt and display rotation from INI */
217                         _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_ctrl,
218                                                         CONFIGURE_CATEGORY_CTRL_CAMERA,
219                                                         "RecommendPreviewFormatCapture",
220                                                         &rcmd_fmt_capture);
221
222                         _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_ctrl,
223                                                         CONFIGURE_CATEGORY_CTRL_CAMERA,
224                                                         "RecommendPreviewFormatRecord",
225                                                         &rcmd_fmt_recording);
226
227                         _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_ctrl,
228                                                         CONFIGURE_CATEGORY_CTRL_CAMERA,
229                                                         "RecommendDisplayRotation",
230                                                         &rcmd_dpy_rotation);
231
232                         _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_main,
233                                                         CONFIGURE_CATEGORY_MAIN_CAPTURE,
234                                                         "PlayCaptureSound",
235                                                         &play_capture_sound);
236
237                         _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_main,
238                                                         CONFIGURE_CATEGORY_MAIN_VIDEO_INPUT,
239                                                         "DeviceCount",
240                                                         &camera_device_count);
241
242                         _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_ctrl,
243                                                         CONFIGURE_CATEGORY_CTRL_CAMERA,
244                                                         "FacingDirection",
245                                                         &camera_facing_direction);
246
247                         _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_ctrl,
248                                                         CONFIGURE_CATEGORY_CTRL_EFFECT,
249                                                         "BrightnessStepDenominator",
250                                                         &hcamcorder->brightness_step_denominator);
251
252                         _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_ctrl,
253                                                         CONFIGURE_CATEGORY_CTRL_CAPTURE,
254                                                         "SupportZSL",
255                                                         &hcamcorder->support_zsl_capture);
256
257                         _mmcam_dbg_log("Recommend fmt[cap:%d,rec:%d], dpy rot %d, cap snd %d, dev cnt %d, cam facing dir %d, step denom %d, support zsl %d",
258                                        rcmd_fmt_capture, rcmd_fmt_recording, rcmd_dpy_rotation,
259                                        play_capture_sound, camera_device_count, camera_facing_direction,
260                                        hcamcorder->brightness_step_denominator, hcamcorder->support_zsl_capture);
261
262                         /* Get UseZeroCopyFormat value from INI */
263                         _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_main,
264                                                         CONFIGURE_CATEGORY_MAIN_VIDEO_INPUT,
265                                                         "UseZeroCopyFormat",
266                                                         &(hcamcorder->use_zero_copy_format));
267
268                         /* Get SupportMediaPacketPreviewCb value from INI */
269                         _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_main,
270                                                         CONFIGURE_CATEGORY_MAIN_VIDEO_INPUT,
271                                                         "SupportMediaPacketPreviewCb",
272                                                         &(hcamcorder->support_media_packet_preview_cb));
273
274                         _mmcam_dbg_log("UseZeroCopyFormat : %d", hcamcorder->use_zero_copy_format);
275                         _mmcam_dbg_log("SupportMediaPacketPreviewCb : %d", hcamcorder->support_media_packet_preview_cb);
276
277                         mm_camcorder_set_attributes((MMHandleType)hcamcorder, &err_attr_name,
278                                                     MMCAM_CAMERA_DEVICE_COUNT, camera_device_count,
279                                                     MMCAM_CAMERA_FACING_DIRECTION, camera_facing_direction,
280                                                     MMCAM_RECOMMEND_PREVIEW_FORMAT_FOR_CAPTURE, rcmd_fmt_capture,
281                                                     MMCAM_RECOMMEND_PREVIEW_FORMAT_FOR_RECORDING, rcmd_fmt_recording,
282                                                     MMCAM_RECOMMEND_DISPLAY_ROTATION, rcmd_dpy_rotation,
283                                                     MMCAM_SUPPORT_ZSL_CAPTURE, hcamcorder->support_zsl_capture,
284                                                     MMCAM_SUPPORT_ZERO_COPY_FORMAT, hcamcorder->use_zero_copy_format,
285                                                     MMCAM_SUPPORT_MEDIA_PACKET_PREVIEW_CB, hcamcorder->support_media_packet_preview_cb,
286                                                     "capture-sound-enable", play_capture_sound,
287                                                     NULL);
288                         if (err_attr_name) {
289                                 _mmcam_dbg_err("Set %s FAILED.", err_attr_name);
290                                 free(err_attr_name);
291                                 err_attr_name = NULL;
292                         }
293
294                         /* Get default value of brightness */
295                         mm_camcorder_get_attributes((MMHandleType)hcamcorder, &err_attr_name,
296                                                     MMCAM_FILTER_BRIGHTNESS, &hcamcorder->brightness_default,
297                                                     NULL);
298                         if (err_attr_name) {
299                                 _mmcam_dbg_err("Get brightness FAILED.");
300                                 free(err_attr_name);
301                                 err_attr_name = NULL;
302                         }
303                         _mmcam_dbg_log("Default brightness : %d", hcamcorder->brightness_default);
304                 } else {
305                         _mmcam_dbg_log( "Disable Configure Control system." );
306                         hcamcorder->conf_ctrl = NULL;
307                 }
308         }
309
310         ret = __mmcamcorder_gstreamer_init(hcamcorder->conf_main);
311         if (!ret) {
312                 _mmcam_dbg_err( "Failed to initialize gstreamer!!" );
313                 ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
314                 goto _ERR_AFTER_ASM_REGISTER;
315         }
316
317         /* Make some attributes as read-only type */
318         _mmcamcorder_lock_readonly_attributes((MMHandleType)hcamcorder);
319
320         /* Disable attributes in each model */
321         _mmcamcorder_set_disabled_attributes((MMHandleType)hcamcorder);
322
323         /* Get videosink name for evas surface */
324         _mmcamcorder_conf_get_element((MMHandleType)hcamcorder, hcamcorder->conf_main,
325                                       CONFIGURE_CATEGORY_MAIN_VIDEO_OUTPUT,
326                                       "VideosinkElementEvas",
327                                       &EvasSurfaceElement);
328         if (EvasSurfaceElement) {
329                 int attr_index = 0;
330                 const char *evassink_name = NULL;
331                 mmf_attribute_t *item_evassink_name = NULL;
332                 mmf_attrs_t *attrs = (mmf_attrs_t *)MMF_CAMCORDER_ATTRS(hcamcorder);
333
334                 _mmcamcorder_conf_get_value_element_name(EvasSurfaceElement, &evassink_name);
335                 mm_attrs_get_index((MMHandleType)attrs, MMCAM_DISPLAY_EVAS_SURFACE_SINK, &attr_index);
336                 item_evassink_name = &attrs->items[attr_index];
337                 mmf_attribute_set_string(item_evassink_name, evassink_name, strlen(evassink_name));
338                 mmf_attribute_commit(item_evassink_name);
339
340                 _mmcam_dbg_log("Evassink name : %s", evassink_name);
341         }
342
343         /* get shutter sound policy */
344         vconf_get_int(VCONFKEY_CAMERA_SHUTTER_SOUND_POLICY, &hcamcorder->shutter_sound_policy);
345         _mmcam_dbg_log("current shutter sound policy : %d", hcamcorder->shutter_sound_policy);
346
347         /* get model name */
348         system_info_get_value_string(SYSTEM_INFO_KEY_MODEL, &hcamcorder->model_name);
349
350         /* get software version */
351         system_info_get_value_string(SYSTEM_INFO_KEY_BUILD_STRING, &hcamcorder->software_version);
352
353         /* Set initial state */
354         _mmcamcorder_set_state((MMHandleType)hcamcorder, MM_CAMCORDER_STATE_NULL);
355
356         _mmcam_dbg_log("created handle %p", hcamcorder);
357
358         *handle = (MMHandleType)hcamcorder;
359
360         return MM_ERROR_NONE;
361
362 _ERR_AFTER_ASM_REGISTER:
363
364 _ERR_DEFAULT_VALUE_INIT:
365         /* Release lock, cond */
366         pthread_mutex_destroy(&((hcamcorder->mtsafe).lock));
367         pthread_cond_destroy(&((hcamcorder->mtsafe).cond));
368         pthread_mutex_destroy(&((hcamcorder->mtsafe).cmd_lock));
369         pthread_mutex_destroy(&((hcamcorder->mtsafe).asm_lock));
370         pthread_mutex_destroy(&((hcamcorder->mtsafe).state_lock));
371         pthread_mutex_destroy(&((hcamcorder->mtsafe).gst_state_lock));
372         pthread_mutex_destroy(&((hcamcorder->mtsafe).gst_encode_state_lock));
373         pthread_mutex_destroy(&((hcamcorder->mtsafe).message_cb_lock));
374         pthread_mutex_destroy(&((hcamcorder->mtsafe).vcapture_cb_lock));
375         pthread_mutex_destroy(&((hcamcorder->mtsafe).vstream_cb_lock));
376         pthread_mutex_destroy(&((hcamcorder->mtsafe).astream_cb_lock));
377
378         pthread_mutex_destroy(&(hcamcorder->sound_lock));
379         pthread_cond_destroy(&(hcamcorder->sound_cond));
380         pthread_mutex_destroy(&(hcamcorder->snd_info.open_mutex));
381         pthread_cond_destroy(&(hcamcorder->snd_info.open_cond));
382         pthread_mutex_destroy(&(hcamcorder->restart_preview_lock));
383
384         if (hcamcorder->conf_ctrl) {
385                 _mmcamcorder_conf_release_info(handle, &hcamcorder->conf_ctrl);
386         }
387
388         if (hcamcorder->conf_main) {
389                 _mmcamcorder_conf_release_info(handle, &hcamcorder->conf_main);
390         }
391
392         if (hcamcorder->model_name) {
393                 free(hcamcorder->model_name);
394                 hcamcorder->model_name = NULL;
395         }
396
397         if (hcamcorder->software_version) {
398                 free(hcamcorder->software_version);
399                 hcamcorder->software_version = NULL;
400         }
401
402         /* Release handle */
403         memset(hcamcorder, 0x00, sizeof(mmf_camcorder_t));
404         free(hcamcorder);
405
406         return ret;
407 }
408
409
410 int _mmcamcorder_destroy(MMHandleType handle)
411 {
412         int ret = MM_ERROR_NONE;
413         int state = MM_CAMCORDER_STATE_NONE;
414         int state_FROM = MM_CAMCORDER_STATE_NULL;
415
416         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
417
418         _mmcam_dbg_log("");
419
420         if (!hcamcorder) {
421                 _mmcam_dbg_err("Not initialized");
422                 ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
423                 goto _ERR_CAMCORDER_CMD_PRECON;
424         }
425
426         if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
427                 _mmcam_dbg_err("Another command is running.");
428                 ret = MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
429                 goto _ERR_CAMCORDER_CMD_PRECON;
430         }
431
432         state = _mmcamcorder_get_state(handle);
433         if (state != state_FROM) {
434                 _mmcam_dbg_err("Wrong state(%d)", state);
435                 ret = MM_ERROR_CAMCORDER_INVALID_STATE;
436                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
437         }
438
439         /* set exit state for sound thread */
440         pthread_mutex_lock(&(hcamcorder->task_thread_lock));
441         _mmcam_dbg_log("send signal for task thread exit");
442         hcamcorder->task_thread_state = _MMCAMCORDER_TASK_THREAD_STATE_EXIT;
443         pthread_cond_signal(&(hcamcorder->task_thread_cond));
444         pthread_mutex_unlock(&(hcamcorder->task_thread_lock));
445
446         pthread_mutex_lock(&(hcamcorder->sound_lock));
447         while (hcamcorder->capture_sound_count > 0) {
448                 struct timespec timeout;
449                 struct timeval tv;
450
451                 gettimeofday(&tv, NULL);
452                 timeout.tv_sec = tv.tv_sec + __MMCAMCORDER_SOUND_WAIT_TIMEOUT;
453                 timeout.tv_nsec = tv.tv_usec * 1000;
454
455                 _mmcam_dbg_warn("capture_sound_count[%d] is not zero. wait signal...",
456                                 hcamcorder->capture_sound_count);
457
458                 if (!pthread_cond_timedwait(&(hcamcorder->sound_cond), &(hcamcorder->sound_lock), &timeout)) {
459                         _mmcam_dbg_warn("signal received. check again...");
460                 } else {
461                         hcamcorder->capture_sound_count = 0;
462                         _mmcam_dbg_err("capture sound completion wait time out");
463                         break;
464                 }
465         }
466         pthread_mutex_unlock(&(hcamcorder->sound_lock));
467
468         /* Release SubContext and pipeline */
469         if (hcamcorder->sub_context) {
470                 if (hcamcorder->sub_context->element) {
471                         _mmcamcorder_destroy_pipeline(handle, hcamcorder->type);
472                 }
473
474                 _mmcamcorder_dealloc_subcontext(hcamcorder->sub_context);
475                 hcamcorder->sub_context = NULL;
476         }
477
478         /* Remove idle function which is not called yet */
479         if (hcamcorder->setting_event_id) {
480                 _mmcam_dbg_log("Remove remaining idle function");
481                 g_source_remove(hcamcorder->setting_event_id);
482                 hcamcorder->setting_event_id = 0;
483         }
484
485         /* Remove attributes */
486         if (hcamcorder->attributes) {
487                 _mmcamcorder_dealloc_attribute(handle, hcamcorder->attributes);
488                 hcamcorder->attributes = 0;
489         }
490
491         /* Remove exif info */
492         if (hcamcorder->exif_info) {
493                 mm_exif_destory_exif_info(hcamcorder->exif_info);
494                 hcamcorder->exif_info=NULL;
495
496         }
497
498         /* Release configure info */
499         if (hcamcorder->conf_ctrl) {
500                 _mmcamcorder_conf_release_info(handle, &hcamcorder->conf_ctrl);
501         }
502         if (hcamcorder->conf_main) {
503                 _mmcamcorder_conf_release_info(handle, &hcamcorder->conf_main);
504         }
505
506         /* Remove messages which are not called yet */
507         _mmcamcroder_remove_message_all(handle);
508
509         /* release model_name */
510         if (hcamcorder->model_name) {
511                 free(hcamcorder->model_name);
512                 hcamcorder->model_name = NULL;
513         }
514
515         if (hcamcorder->software_version) {
516                 free(hcamcorder->software_version);
517                 hcamcorder->software_version = NULL;
518         }
519
520         /* join task thread */
521         _mmcam_dbg_log("task thread join");
522         pthread_join(hcamcorder->task_thread, NULL);
523
524         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
525
526         /* Release lock, cond */
527         pthread_mutex_destroy(&((hcamcorder->mtsafe).lock));
528         pthread_cond_destroy(&((hcamcorder->mtsafe).cond));
529         pthread_mutex_destroy(&((hcamcorder->mtsafe).cmd_lock));
530         pthread_mutex_destroy(&((hcamcorder->mtsafe).asm_lock));
531         pthread_mutex_destroy(&((hcamcorder->mtsafe).state_lock));
532         pthread_mutex_destroy(&((hcamcorder->mtsafe).gst_state_lock));
533         pthread_mutex_destroy(&((hcamcorder->mtsafe).gst_encode_state_lock));
534         pthread_mutex_destroy(&((hcamcorder->mtsafe).message_cb_lock));
535         pthread_mutex_destroy(&((hcamcorder->mtsafe).vcapture_cb_lock));
536         pthread_mutex_destroy(&((hcamcorder->mtsafe).vstream_cb_lock));
537         pthread_mutex_destroy(&((hcamcorder->mtsafe).astream_cb_lock));
538
539         pthread_mutex_destroy(&(hcamcorder->sound_lock));
540         pthread_cond_destroy(&(hcamcorder->sound_cond));
541         pthread_mutex_destroy(&(hcamcorder->snd_info.open_mutex));
542         pthread_cond_destroy(&(hcamcorder->snd_info.open_cond));
543         pthread_mutex_destroy(&(hcamcorder->restart_preview_lock));
544         pthread_mutex_destroy(&(hcamcorder->task_thread_lock));
545         pthread_cond_destroy(&(hcamcorder->task_thread_cond));
546
547         /* Release handle */
548         memset(hcamcorder, 0x00, sizeof(mmf_camcorder_t));
549         free(hcamcorder);
550
551         return MM_ERROR_NONE;
552
553 _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK:
554         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
555
556 _ERR_CAMCORDER_CMD_PRECON:
557         if (hcamcorder) {
558                 _mmcam_dbg_err("Destroy fail (type %d, state %d)", hcamcorder->type, state);
559         }
560
561         _mmcam_dbg_err("Destroy fail (ret %x)", ret);
562
563         return ret;
564 }
565
566
567 int _mmcamcorder_realize(MMHandleType handle)
568 {
569         int ret = MM_ERROR_NONE;
570         int state = MM_CAMCORDER_STATE_NONE;
571         int state_FROM = MM_CAMCORDER_STATE_NULL;
572         int state_TO = MM_CAMCORDER_STATE_READY;
573         int display_surface_type = MM_DISPLAY_SURFACE_X;
574         double motion_rate = _MMCAMCORDER_DEFAULT_RECORDING_MOTION_RATE;
575         char *videosink_element_type = NULL;
576         const char *videosink_name = NULL;
577
578         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
579
580         _mmcam_dbg_log("");
581
582         if (!hcamcorder) {
583                 _mmcam_dbg_err("Not initialized");
584                 ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
585                 return ret;
586         }
587
588         if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
589                 _mmcam_dbg_err("Another command is running.");
590                 ret = MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
591                 goto _ERR_CAMCORDER_CMD_PRECON;
592         }
593
594         state = _mmcamcorder_get_state(handle);
595         if (state != state_FROM) {
596                 _mmcam_dbg_err("Wrong state(%d)", state);
597                 ret = MM_ERROR_CAMCORDER_INVALID_STATE;
598                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
599         }
600
601         mm_camcorder_get_attributes(handle, NULL,
602                                     MMCAM_MODE, &hcamcorder->type,
603                                     NULL);
604
605         /* Get profile mode */
606         _mmcam_dbg_log("Profile mode [%d]", hcamcorder->type);
607
608         /* set camera state to vconf key */
609         if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
610                 int vconf_camera_state = 0;
611
612                 /* get current camera state of vconf key */
613                 vconf_get_int(VCONFKEY_CAMERA_STATE, &vconf_camera_state);
614                 vconf_set_int(VCONFKEY_CAMERA_STATE, VCONFKEY_CAMERA_STATE_OPEN);
615
616                 _mmcam_dbg_log("VCONFKEY_CAMERA_STATE prev %d -> cur %d",
617                                vconf_camera_state, VCONFKEY_CAMERA_STATE_OPEN);
618         }
619
620         mm_camcorder_get_attributes(handle, NULL,
621                                     MMCAM_DISPLAY_SURFACE, &display_surface_type,
622                                     MMCAM_CAMERA_RECORDING_MOTION_RATE, &motion_rate,
623                                     NULL);
624
625         /* alloc sub context */
626         hcamcorder->sub_context = _mmcamcorder_alloc_subcontext(hcamcorder->type);
627         if(!hcamcorder->sub_context) {
628                 ret = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
629                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
630         }
631
632         /* Set basic configure information */
633         if (motion_rate != _MMCAMCORDER_DEFAULT_RECORDING_MOTION_RATE) {
634                 hcamcorder->sub_context->is_modified_rate = TRUE;
635         }
636
637         _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_main,
638                                         CONFIGURE_CATEGORY_MAIN_CAPTURE,
639                                         "UseEncodebin",
640                                         &(hcamcorder->sub_context->bencbin_capture));
641         _mmcam_dbg_warn("UseEncodebin [%d]", hcamcorder->sub_context->bencbin_capture);
642
643         if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
644                 /* get dual stream support info */
645                 _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_main,
646                                                 CONFIGURE_CATEGORY_MAIN_RECORD,
647                                                 "SupportDualStream",
648                                                 &(hcamcorder->sub_context->info_video->support_dual_stream));
649                 _mmcam_dbg_warn("SupportDualStream [%d]", hcamcorder->sub_context->info_video->support_dual_stream);
650         }
651
652         switch (display_surface_type) {
653         case MM_DISPLAY_SURFACE_X:
654                 videosink_element_type = strdup("VideosinkElementX");
655                 break;
656         case MM_DISPLAY_SURFACE_EVAS:
657                 videosink_element_type = strdup("VideosinkElementEvas");
658                 break;
659         case MM_DISPLAY_SURFACE_GL:
660                 videosink_element_type = strdup("VideosinkElementGL");
661                 break;
662         case MM_DISPLAY_SURFACE_NULL:
663                 videosink_element_type = strdup("VideosinkElementNull");
664                 break;
665         default:
666                 videosink_element_type = strdup("VideosinkElementX");
667                 break;
668         }
669
670         /* check string of videosink element */
671         if (videosink_element_type) {
672                 _mmcamcorder_conf_get_element(handle, hcamcorder->conf_main,
673                                               CONFIGURE_CATEGORY_MAIN_VIDEO_OUTPUT,
674                                               videosink_element_type,
675                                               &hcamcorder->sub_context->VideosinkElement);
676                 free(videosink_element_type);
677                 videosink_element_type = NULL;
678         } else {
679                 _mmcam_dbg_warn("strdup failed(display_surface_type %d). Use default X type",
680                                 display_surface_type);
681
682                 _mmcamcorder_conf_get_element(handle, hcamcorder->conf_main,
683                                               CONFIGURE_CATEGORY_MAIN_VIDEO_OUTPUT,
684                                               _MMCAMCORDER_DEFAULT_VIDEOSINK_TYPE,
685                                               &hcamcorder->sub_context->VideosinkElement);
686         }
687
688         _mmcamcorder_conf_get_value_element_name(hcamcorder->sub_context->VideosinkElement, &videosink_name);
689         _mmcam_dbg_log("Videosink name : %s", videosink_name);
690
691         /* get videoconvert element */
692         _mmcamcorder_conf_get_element(handle, hcamcorder->conf_main,
693                                       CONFIGURE_CATEGORY_MAIN_VIDEO_OUTPUT,
694                                       "VideoconvertElement",
695                                       &hcamcorder->sub_context->VideoconvertElement);
696
697         _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_ctrl,
698                                         CONFIGURE_CATEGORY_CTRL_CAPTURE,
699                                         "SensorEncodedCapture",
700                                         &(hcamcorder->sub_context->SensorEncodedCapture));
701         _mmcam_dbg_log("Support sensor encoded capture : %d", hcamcorder->sub_context->SensorEncodedCapture);
702
703         /* create pipeline */
704         ret = _mmcamcorder_create_pipeline(handle, hcamcorder->type);
705         if (ret != MM_ERROR_NONE) {
706                 /* check internal error of gstreamer */
707                 if (hcamcorder->sub_context->error_code != MM_ERROR_NONE) {
708                         ret = hcamcorder->sub_context->error_code;
709                         _mmcam_dbg_log("gstreamer error is occurred. return it %x", ret);
710                 }
711
712                 /* release sub context */
713                 _mmcamcorder_dealloc_subcontext(hcamcorder->sub_context);
714                 hcamcorder->sub_context = NULL;
715                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
716         }
717
718         /* set command function */
719         ret = _mmcamcorder_set_functions(handle, hcamcorder->type);
720         if (ret != MM_ERROR_NONE) {
721                 _mmcamcorder_destroy_pipeline(handle, hcamcorder->type);
722                 _mmcamcorder_dealloc_subcontext(hcamcorder->sub_context);
723                 hcamcorder->sub_context = NULL;
724                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
725         }
726
727         _mmcamcorder_set_state(handle, state_TO);
728
729         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
730
731         return MM_ERROR_NONE;
732
733 _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK:
734         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
735
736 _ERR_CAMCORDER_CMD_PRECON:
737         _mmcam_dbg_err("Realize fail (type %d, state %d, ret %x)",
738                        hcamcorder->type, state, ret);
739
740         /* rollback camera state to vconf key */
741         if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
742                 int vconf_camera_state = 0;
743
744                 /* get current camera state of vconf key */
745                 vconf_get_int(VCONFKEY_CAMERA_STATE, &vconf_camera_state);
746                 vconf_set_int(VCONFKEY_CAMERA_STATE, VCONFKEY_CAMERA_STATE_NULL);
747
748                 _mmcam_dbg_log("VCONFKEY_CAMERA_STATE prev %d -> cur %d",
749                                vconf_camera_state, VCONFKEY_CAMERA_STATE_NULL);
750         }
751
752         return ret;
753 }
754
755
756 int _mmcamcorder_unrealize(MMHandleType handle)
757 {
758         int ret = MM_ERROR_NONE;
759         int state = MM_CAMCORDER_STATE_NONE;
760         int state_FROM = MM_CAMCORDER_STATE_READY;
761         int state_TO = MM_CAMCORDER_STATE_NULL;
762
763         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
764
765         _mmcam_dbg_log("");
766
767         if (!hcamcorder) {
768                 _mmcam_dbg_err("Not initialized");
769                 ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
770                 return ret;
771         }
772
773         if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
774                 _mmcam_dbg_err("Another command is running.");
775                 ret = MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
776                 goto _ERR_CAMCORDER_CMD_PRECON;
777         }
778
779         state = _mmcamcorder_get_state(handle);
780         if (state != state_FROM) {
781                 _mmcam_dbg_err("Wrong state(%d)", state);
782                 ret = MM_ERROR_CAMCORDER_INVALID_STATE;
783                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
784         }
785
786         /* Release SubContext */
787         if (hcamcorder->sub_context) {
788                 /* destroy pipeline */
789                 _mmcamcorder_destroy_pipeline(handle, hcamcorder->type);
790                 /* Deallocate SubContext */
791                 _mmcamcorder_dealloc_subcontext(hcamcorder->sub_context);
792                 hcamcorder->sub_context = NULL;
793         }
794
795         /* Deinitialize main context member */
796
797         hcamcorder->command = NULL;
798
799         /* check who calls unrealize. it's no need to set ASM state if caller is ASM */
800         if (hcamcorder->state_change_by_system != _MMCAMCORDER_STATE_CHANGE_BY_ASM) {
801                 /* TODO */
802         }
803
804         /* set camera state to vconf key */
805         if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
806                 int vconf_camera_state = 0;
807
808                 /* get current camera state of vconf key */
809                 vconf_get_int(VCONFKEY_CAMERA_STATE, &vconf_camera_state);
810                 vconf_set_int(VCONFKEY_CAMERA_STATE, VCONFKEY_CAMERA_STATE_NULL);
811
812                 _mmcam_dbg_log("VCONFKEY_CAMERA_STATE prev %d -> cur %d",
813                                vconf_camera_state, VCONFKEY_CAMERA_STATE_NULL);
814         }
815
816         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
817
818         _mmcamcorder_set_state(handle, state_TO);
819
820         return MM_ERROR_NONE;
821
822 _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK:
823         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
824
825 _ERR_CAMCORDER_CMD_PRECON:
826         /* send message */
827         _mmcam_dbg_err("Unrealize fail (type %d, state %d, ret %x)",
828                        hcamcorder->type, state, ret);
829
830         return ret;
831 }
832
833 int _mmcamcorder_start(MMHandleType handle)
834 {
835         int ret = MM_ERROR_NONE;
836         int state = MM_CAMCORDER_STATE_NONE;
837         int state_FROM = MM_CAMCORDER_STATE_READY;
838         int state_TO =MM_CAMCORDER_STATE_PREPARE;
839
840         _MMCamcorderSubContext *sc = NULL;
841         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
842
843         _mmcam_dbg_log("");
844
845         if (!hcamcorder) {
846                 _mmcam_dbg_err("Not initialized");
847                 ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
848                 return ret;
849         }
850
851         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
852         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
853
854         if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
855                 _mmcam_dbg_err("Another command is running.");
856                 ret = MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
857                 goto _ERR_CAMCORDER_CMD_PRECON;
858         }
859
860         state = _mmcamcorder_get_state(handle);
861         if (state != state_FROM) {
862                 _mmcam_dbg_err("Wrong state(%d)", state);
863                 ret = MM_ERROR_CAMCORDER_INVALID_STATE;
864                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
865         }
866
867         /* initialize error code */
868         hcamcorder->sub_context->error_code = MM_ERROR_NONE;
869
870         /* set attributes related sensor */
871         if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
872                 _mmcamcorder_set_attribute_to_camsensor(handle);
873         }
874
875         ret = hcamcorder->command((MMHandleType)hcamcorder, _MMCamcorder_CMD_PREVIEW_START);
876         if (ret != MM_ERROR_NONE) {
877                 /* check internal error of gstreamer */
878                 if (hcamcorder->sub_context->error_code != MM_ERROR_NONE) {
879                         ret = hcamcorder->sub_context->error_code;
880                         _mmcam_dbg_log("gstreamer error is occurred. return it %x", ret);
881                 }
882                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
883         }
884
885         _mmcamcorder_set_state(handle, state_TO);
886
887         if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
888                 int vconf_camera_state = 0;
889
890                 /* check camera state of vconf key */
891                 vconf_get_int(VCONFKEY_CAMERA_STATE, &vconf_camera_state);
892
893                 /* set camera state to vconf key */
894                 vconf_set_int(VCONFKEY_CAMERA_STATE, VCONFKEY_CAMERA_STATE_PREVIEW);
895
896                 _mmcam_dbg_log("VCONFKEY_CAMERA_STATE prev %d -> cur %d",
897                                vconf_camera_state, VCONFKEY_CAMERA_STATE_PREVIEW);
898         }
899
900         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
901
902         return MM_ERROR_NONE;
903
904 _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK:
905         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
906
907 _ERR_CAMCORDER_CMD_PRECON:
908         /* check internal error of gstreamer */
909         if (hcamcorder->sub_context &&
910             hcamcorder->sub_context->error_code != MM_ERROR_NONE) {
911                 ret = hcamcorder->sub_context->error_code;
912                 hcamcorder->sub_context->error_code = MM_ERROR_NONE;
913
914                 _mmcam_dbg_log("gstreamer error is occurred. return it %x", ret);
915         }
916
917         _mmcam_dbg_err("Start fail (type %d, state %d, ret %x)",
918                        hcamcorder->type, state, ret);
919
920         return ret;
921 }
922
923 int _mmcamcorder_stop(MMHandleType handle)
924 {
925         int ret = MM_ERROR_NONE;
926         int state = MM_CAMCORDER_STATE_NONE;
927         int state_FROM = MM_CAMCORDER_STATE_PREPARE;
928         int state_TO = MM_CAMCORDER_STATE_READY;
929
930         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
931
932         _mmcam_dbg_log("");
933
934         if (!hcamcorder) {
935                 _mmcam_dbg_err("Not initialized");
936                 ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
937                 return ret;
938         }
939
940         if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
941                 _mmcam_dbg_err("Another command is running.");
942                 ret = MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
943                 goto _ERR_CAMCORDER_CMD_PRECON;
944         }
945
946         state = _mmcamcorder_get_state(handle);
947         if (state != state_FROM) {
948                 _mmcam_dbg_err("Wrong state(%d)", state);
949                 ret = MM_ERROR_CAMCORDER_INVALID_STATE;
950                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
951         }
952
953         ret = hcamcorder->command((MMHandleType)hcamcorder, _MMCamcorder_CMD_PREVIEW_STOP);
954         if (ret != MM_ERROR_NONE) {
955                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
956         }
957
958         _mmcamcorder_set_state(handle, state_TO);
959
960         if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
961                 int vconf_camera_state = 0;
962
963                 /* check camera state of vconf key */
964                 vconf_get_int(VCONFKEY_CAMERA_STATE, &vconf_camera_state);
965
966                 /* set camera state to vconf key */
967                 vconf_set_int(VCONFKEY_CAMERA_STATE, VCONFKEY_CAMERA_STATE_OPEN);
968
969                 _mmcam_dbg_log("VCONFKEY_CAMERA_STATE prev %d -> cur %d",
970                                vconf_camera_state, VCONFKEY_CAMERA_STATE_OPEN);
971         }
972
973         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
974
975         return MM_ERROR_NONE;
976
977 _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK:
978         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
979
980 _ERR_CAMCORDER_CMD_PRECON:
981         /* send message */
982         _mmcam_dbg_err("Stop fail (type %d, state %d, ret %x)",
983                        hcamcorder->type, state, ret);
984
985         return ret;
986 }
987
988
989 int _mmcamcorder_capture_start(MMHandleType handle)
990 {
991         int ret = MM_ERROR_NONE;
992         int state = MM_CAMCORDER_STATE_NONE;
993         int state_FROM_0 = MM_CAMCORDER_STATE_PREPARE;
994         int state_FROM_1 = MM_CAMCORDER_STATE_RECORDING;
995         int state_FROM_2 = MM_CAMCORDER_STATE_PAUSED;
996         int state_TO = MM_CAMCORDER_STATE_CAPTURING;
997
998         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
999
1000         _mmcam_dbg_log("");
1001
1002         if (!hcamcorder) {
1003                 _mmcam_dbg_err("Not initialized");
1004                 ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
1005                 return ret;
1006         }
1007
1008         if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
1009                 _mmcam_dbg_err("Another command is running.");
1010                 ret = MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
1011                 goto _ERR_CAMCORDER_CMD_PRECON;
1012         }
1013
1014         state = _mmcamcorder_get_state(handle);
1015         if (state != state_FROM_0 &&
1016             state != state_FROM_1 &&
1017             state != state_FROM_2) {
1018                 _mmcam_dbg_err("Wrong state(%d)", state);
1019                 ret = MM_ERROR_CAMCORDER_INVALID_STATE;
1020                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1021         }
1022
1023         /* Handle capture in recording case */
1024         if (state == state_FROM_1 || state == state_FROM_2) {
1025                 if (hcamcorder->capture_in_recording == TRUE) {
1026                         _mmcam_dbg_err("Capturing in recording (%d)", state);
1027                         ret = MM_ERROR_CAMCORDER_DEVICE_BUSY;
1028                         goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1029                 } else {
1030                         hcamcorder->capture_in_recording = TRUE;
1031                 }
1032         }
1033
1034         ret = hcamcorder->command((MMHandleType)hcamcorder, _MMCamcorder_CMD_CAPTURE);
1035         if (ret != MM_ERROR_NONE) {
1036                 goto _ERR_CAMCORDER_CMD;
1037         }
1038
1039         /* Do not change state when recording snapshot capture */
1040         if (state == state_FROM_0) {
1041                 _mmcamcorder_set_state(handle, state_TO);
1042         }
1043
1044         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1045
1046         /* Init break continuous shot attr */
1047         if (mm_camcorder_set_attributes(handle, NULL, "capture-break-cont-shot", 0, NULL) != MM_ERROR_NONE) {
1048                 _mmcam_dbg_warn("capture-break-cont-shot set 0 failed");
1049         }
1050
1051         return MM_ERROR_NONE;
1052
1053 _ERR_CAMCORDER_CMD:
1054         if (hcamcorder->capture_in_recording) {
1055                 hcamcorder->capture_in_recording = FALSE;
1056         }
1057
1058 _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK:
1059         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1060
1061 _ERR_CAMCORDER_CMD_PRECON:
1062         /* send message */
1063         _mmcam_dbg_err("Capture start fail (type %d, state %d, ret %x)",
1064                        hcamcorder->type, state, ret);
1065
1066         return ret;
1067 }
1068
1069 int _mmcamcorder_capture_stop(MMHandleType handle)
1070 {
1071         int ret = MM_ERROR_NONE;
1072         int state = MM_CAMCORDER_STATE_NONE;
1073         int state_FROM = MM_CAMCORDER_STATE_CAPTURING;
1074         int state_TO = MM_CAMCORDER_STATE_PREPARE;
1075         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1076
1077         _mmcam_dbg_log("");
1078
1079         if (!hcamcorder) {
1080                 _mmcam_dbg_err("Not initialized");
1081                 ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
1082                 return ret;
1083         }
1084
1085         if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
1086                 _mmcam_dbg_err("Another command is running.");
1087                 ret = MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
1088                 goto _ERR_CAMCORDER_CMD_PRECON;
1089         }
1090
1091         state = _mmcamcorder_get_state(handle);
1092         if (state != state_FROM) {
1093                 _mmcam_dbg_err("Wrong state(%d)", state);
1094                 ret = MM_ERROR_CAMCORDER_INVALID_STATE;
1095                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1096         }
1097
1098         ret = hcamcorder->command((MMHandleType)hcamcorder, _MMCamcorder_CMD_PREVIEW_START);
1099         if (ret != MM_ERROR_NONE) {
1100                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1101         }
1102
1103         _mmcamcorder_set_state(handle, state_TO);
1104
1105         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1106
1107         return MM_ERROR_NONE;
1108
1109 _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK:
1110         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1111
1112 _ERR_CAMCORDER_CMD_PRECON:
1113         /* send message */
1114         _mmcam_dbg_err("Capture stop fail (type %d, state %d, ret %x)",
1115                        hcamcorder->type, state, ret);
1116
1117         return ret;
1118 }
1119
1120 int _mmcamcorder_record(MMHandleType handle)
1121 {
1122         int ret = MM_ERROR_NONE;
1123         int state = MM_CAMCORDER_STATE_NONE;
1124         int state_FROM1 = MM_CAMCORDER_STATE_PREPARE;
1125         int state_FROM2 = MM_CAMCORDER_STATE_PAUSED;
1126         int state_TO = MM_CAMCORDER_STATE_RECORDING;
1127
1128         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1129
1130         _mmcam_dbg_log("");
1131
1132         if (!hcamcorder) {
1133                 _mmcam_dbg_err("Not initialized");
1134                 ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
1135                 return ret;
1136         }
1137
1138         if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
1139                 _mmcam_dbg_err("Another command is running.");
1140                 ret = MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
1141                 goto _ERR_CAMCORDER_CMD_PRECON;
1142         }
1143
1144         state = _mmcamcorder_get_state(handle);
1145         if (state != state_FROM1 && state != state_FROM2) {
1146                 _mmcam_dbg_err("Wrong state(%d)", state);
1147                 ret = MM_ERROR_CAMCORDER_INVALID_STATE;
1148                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1149         }
1150
1151         /* initialize error code */
1152         hcamcorder->sub_context->error_code = MM_ERROR_NONE;
1153
1154         ret = hcamcorder->command((MMHandleType)hcamcorder, _MMCamcorder_CMD_RECORD);
1155         if (ret != MM_ERROR_NONE) {
1156                 /* check internal error of gstreamer */
1157                 if (hcamcorder->sub_context->error_code != MM_ERROR_NONE) {
1158                         ret = hcamcorder->sub_context->error_code;
1159                         _mmcam_dbg_log("gstreamer error is occurred. return it %x", ret);
1160                 }
1161                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1162         }
1163
1164         _mmcamcorder_set_state(handle, state_TO);
1165
1166         /* set camera state to vconf key */
1167         if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
1168                 int vconf_camera_state = 0;
1169
1170                 /* get current camera state of vconf key */
1171                 vconf_get_int(VCONFKEY_CAMERA_STATE, &vconf_camera_state);
1172                 vconf_set_int(VCONFKEY_CAMERA_STATE, VCONFKEY_CAMERA_STATE_RECORDING);
1173
1174                 _mmcam_dbg_log("VCONFKEY_CAMERA_STATE prev %d -> cur %d",
1175                                vconf_camera_state, VCONFKEY_CAMERA_STATE_RECORDING);
1176         }
1177
1178         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1179
1180         return MM_ERROR_NONE;
1181
1182 _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK:
1183         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1184
1185 _ERR_CAMCORDER_CMD_PRECON:
1186         /* check internal error of gstreamer */
1187         if (hcamcorder->sub_context &&
1188             hcamcorder->sub_context->error_code != MM_ERROR_NONE) {
1189                 ret = hcamcorder->sub_context->error_code;
1190                 hcamcorder->sub_context->error_code = MM_ERROR_NONE;
1191
1192                 _mmcam_dbg_log("gstreamer error is occurred. return it %x", ret);
1193         }
1194
1195         _mmcam_dbg_err("Record fail (type %d, state %d, ret %x)",
1196                        hcamcorder->type, state, ret);
1197
1198         return ret;
1199 }
1200
1201
1202 int _mmcamcorder_pause(MMHandleType handle)
1203 {
1204         int ret = MM_ERROR_NONE;
1205         int state = MM_CAMCORDER_STATE_NONE;
1206         int state_FROM = MM_CAMCORDER_STATE_RECORDING;
1207         int state_TO = MM_CAMCORDER_STATE_PAUSED;
1208
1209         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1210
1211         _mmcam_dbg_log("");
1212
1213         if (!hcamcorder) {
1214                 _mmcam_dbg_err("Not initialized");
1215                 ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
1216                 return ret;
1217         }
1218
1219         if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
1220                 _mmcam_dbg_err("Another command is running.");
1221                 ret = MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
1222                 goto _ERR_CAMCORDER_CMD_PRECON;
1223         }
1224
1225         state = _mmcamcorder_get_state(handle);
1226         if (state != state_FROM) {
1227                 _mmcam_dbg_err("Wrong state(%d)", state);
1228                 ret = MM_ERROR_CAMCORDER_INVALID_STATE;
1229                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1230         }
1231
1232         ret = hcamcorder->command((MMHandleType)hcamcorder, _MMCamcorder_CMD_PAUSE);
1233         if (ret != MM_ERROR_NONE) {
1234                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1235         }
1236
1237         _mmcamcorder_set_state(handle, state_TO);
1238
1239         /* set camera state to vconf key */
1240         if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
1241                 int vconf_camera_state = 0;
1242
1243                 /* get current camera state of vconf key */
1244                 vconf_get_int(VCONFKEY_CAMERA_STATE, &vconf_camera_state);
1245                 vconf_set_int(VCONFKEY_CAMERA_STATE, VCONFKEY_CAMERA_STATE_RECORDING_PAUSE);
1246
1247                 _mmcam_dbg_log("VCONFKEY_CAMERA_STATE prev %d -> cur %d",
1248                                vconf_camera_state, VCONFKEY_CAMERA_STATE_RECORDING_PAUSE);
1249         }
1250
1251         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1252
1253         return MM_ERROR_NONE;
1254
1255 _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK:
1256         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1257
1258 _ERR_CAMCORDER_CMD_PRECON:
1259         /* send message */
1260         _mmcam_dbg_err("Pause fail (type %d, state %d, ret %x)",
1261                        hcamcorder->type, state, ret);
1262
1263         return ret;
1264 }
1265
1266
1267 int _mmcamcorder_commit(MMHandleType handle)
1268 {
1269         int ret = MM_ERROR_NONE;
1270         int state = MM_CAMCORDER_STATE_NONE;
1271         int state_FROM1 = MM_CAMCORDER_STATE_RECORDING;
1272         int state_FROM2 = MM_CAMCORDER_STATE_PAUSED;
1273         int state_TO = MM_CAMCORDER_STATE_PREPARE;
1274
1275         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1276
1277         _mmcam_dbg_log("");
1278
1279         if (!hcamcorder) {
1280                 _mmcam_dbg_err("Not initialized");
1281                 ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
1282                 return ret;
1283         }
1284
1285         if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
1286                 _mmcam_dbg_err("Another command is running.");
1287                 ret = MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
1288                 goto _ERR_CAMCORDER_CMD_PRECON;
1289         }
1290
1291         state = _mmcamcorder_get_state(handle);
1292         if (state != state_FROM1 && state != state_FROM2) {
1293                 _mmcam_dbg_err("Wrong state(%d)", state);
1294                 ret = MM_ERROR_CAMCORDER_INVALID_STATE;
1295                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1296         }
1297
1298         ret = hcamcorder->command((MMHandleType)hcamcorder, _MMCamcorder_CMD_COMMIT);
1299         if (ret != MM_ERROR_NONE) {
1300                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1301         }
1302
1303         /* set camera state to vconf key */
1304         if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
1305                 int vconf_camera_state = 0;
1306
1307                 /* get current camera state of vconf key */
1308                 vconf_get_int(VCONFKEY_CAMERA_STATE, &vconf_camera_state);
1309                 vconf_set_int(VCONFKEY_CAMERA_STATE, VCONFKEY_CAMERA_STATE_PREVIEW);
1310
1311                 _mmcam_dbg_log("VCONFKEY_CAMERA_STATE prev %d -> cur %d",
1312                                vconf_camera_state, VCONFKEY_CAMERA_STATE_PREVIEW);
1313         }
1314
1315         /* check who calls unrealize. it's no need to set ASM state if caller is ASM */
1316         if (hcamcorder->state_change_by_system != _MMCAMCORDER_STATE_CHANGE_BY_ASM) {
1317                 /* TODO */
1318         }
1319
1320         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1321
1322         _mmcamcorder_set_state(handle,state_TO);
1323
1324         return MM_ERROR_NONE;
1325
1326 _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK:
1327         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1328
1329 _ERR_CAMCORDER_CMD_PRECON:
1330         /* send message */
1331         _mmcam_dbg_err("Commit fail (type %d, state %d, ret %x)",
1332                        hcamcorder->type, state, ret);
1333
1334         return ret;
1335 }
1336
1337
1338 int _mmcamcorder_cancel(MMHandleType handle)
1339 {
1340         int ret = MM_ERROR_NONE;
1341         int state = MM_CAMCORDER_STATE_NONE;
1342         int state_FROM1 = MM_CAMCORDER_STATE_RECORDING;
1343         int state_FROM2 = MM_CAMCORDER_STATE_PAUSED;
1344         int state_TO = MM_CAMCORDER_STATE_PREPARE;
1345
1346         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1347
1348         _mmcam_dbg_log("");
1349
1350         if (!hcamcorder) {
1351                 _mmcam_dbg_err("Not initialized");
1352                 ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
1353                 return ret;
1354         }
1355
1356         if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
1357                 _mmcam_dbg_err("Another command is running.");
1358                 ret = MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
1359                 goto _ERR_CAMCORDER_CMD_PRECON;
1360         }
1361
1362         state = _mmcamcorder_get_state(handle);
1363         if (state != state_FROM1 && state != state_FROM2) {
1364                 _mmcam_dbg_err("Wrong state(%d)", state);
1365                 ret = MM_ERROR_CAMCORDER_INVALID_STATE;
1366                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1367         }
1368
1369         ret = hcamcorder->command((MMHandleType)hcamcorder, _MMCamcorder_CMD_CANCEL);
1370         if (ret != MM_ERROR_NONE) {
1371                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1372         }
1373
1374         _mmcamcorder_set_state(handle, state_TO);
1375
1376         /* set camera state to vconf key */
1377         if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
1378                 int vconf_camera_state = 0;
1379
1380                 /* get current camera state of vconf key */
1381                 vconf_get_int(VCONFKEY_CAMERA_STATE, &vconf_camera_state);
1382                 vconf_set_int(VCONFKEY_CAMERA_STATE, VCONFKEY_CAMERA_STATE_PREVIEW);
1383
1384                 _mmcam_dbg_log("VCONFKEY_CAMERA_STATE prev %d -> cur %d",
1385                                vconf_camera_state, VCONFKEY_CAMERA_STATE_PREVIEW);
1386         }
1387
1388         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1389
1390         return MM_ERROR_NONE;
1391
1392 _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK:
1393         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1394
1395 _ERR_CAMCORDER_CMD_PRECON:
1396         /* send message */
1397         _mmcam_dbg_err("Cancel fail (type %d, state %d, ret %x)",
1398                        hcamcorder->type, state, ret);
1399
1400         return ret;
1401 }
1402 /* } Internal command functions */
1403
1404
1405 int _mmcamcorder_commit_async_end(MMHandleType handle)
1406 {
1407         _mmcam_dbg_log("");
1408
1409         _mmcam_dbg_warn("_mmcamcorder_commit_async_end : MM_CAMCORDER_STATE_PREPARE");
1410         _mmcamcorder_set_state(handle, MM_CAMCORDER_STATE_PREPARE);
1411
1412         return MM_ERROR_NONE;
1413 }
1414
1415
1416 int _mmcamcorder_set_message_callback(MMHandleType handle, MMMessageCallback callback, void *user_data)
1417 {
1418         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1419
1420         _mmcam_dbg_log("%p", hcamcorder);
1421
1422         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1423
1424         if (callback == NULL) {
1425                 _mmcam_dbg_warn("Message Callback is disabled, because application sets it to NULL");
1426         }
1427
1428         if (!_MMCAMCORDER_TRYLOCK_MESSAGE_CALLBACK(hcamcorder)) {
1429                 _mmcam_dbg_warn("Application's message callback is running now");
1430                 return MM_ERROR_CAMCORDER_INVALID_CONDITION;
1431         }
1432
1433         /* set message callback to message handle */
1434         hcamcorder->msg_cb = callback;
1435         hcamcorder->msg_cb_param = user_data;
1436
1437         _MMCAMCORDER_UNLOCK_MESSAGE_CALLBACK(hcamcorder);
1438
1439         return MM_ERROR_NONE;
1440 }
1441
1442
1443 int _mmcamcorder_set_video_stream_callback(MMHandleType handle, mm_camcorder_video_stream_callback callback, void *user_data)
1444 {
1445         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1446
1447         _mmcam_dbg_log("");
1448
1449         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1450
1451         if (callback == NULL) {
1452                 _mmcam_dbg_warn("Video Stream Callback is disabled, because application sets it to NULL");
1453         }
1454
1455         if (!_MMCAMCORDER_TRYLOCK_VSTREAM_CALLBACK(hcamcorder)) {
1456                 _mmcam_dbg_warn("Application's video stream callback is running now");
1457                 return MM_ERROR_CAMCORDER_INVALID_CONDITION;
1458         }
1459
1460         hcamcorder->vstream_cb = callback;
1461         hcamcorder->vstream_cb_param = user_data;
1462
1463         _MMCAMCORDER_UNLOCK_VSTREAM_CALLBACK(hcamcorder);
1464
1465         return MM_ERROR_NONE;
1466 }
1467
1468
1469 int _mmcamcorder_set_audio_stream_callback(MMHandleType handle, mm_camcorder_audio_stream_callback callback, void *user_data)
1470 {
1471         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1472
1473         _mmcam_dbg_log("");
1474
1475         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1476
1477         if (callback == NULL) {
1478                 _mmcam_dbg_warn("Audio Stream Callback is disabled, because application sets it to NULL");
1479         }
1480
1481         if (!_MMCAMCORDER_TRYLOCK_ASTREAM_CALLBACK(hcamcorder)) {
1482                 _mmcam_dbg_warn("Application's audio stream callback is running now");
1483                 return MM_ERROR_CAMCORDER_INVALID_CONDITION;
1484         }
1485
1486         hcamcorder->astream_cb = callback;
1487         hcamcorder->astream_cb_param = user_data;
1488
1489         _MMCAMCORDER_UNLOCK_ASTREAM_CALLBACK(hcamcorder);
1490
1491         return MM_ERROR_NONE;
1492 }
1493
1494
1495 int _mmcamcorder_set_video_capture_callback(MMHandleType handle, mm_camcorder_video_capture_callback callback, void *user_data)
1496 {
1497         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1498
1499         _mmcam_dbg_log("");
1500
1501         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1502
1503         if (callback == NULL) {
1504                 _mmcam_dbg_warn("Video Capture Callback is disabled, because application sets it to NULLL");
1505         }
1506
1507         if (!_MMCAMCORDER_TRYLOCK_VCAPTURE_CALLBACK(hcamcorder)) {
1508                 _mmcam_dbg_warn("Application's video capture callback is running now");
1509                 return MM_ERROR_CAMCORDER_INVALID_CONDITION;
1510         }
1511
1512         hcamcorder->vcapture_cb = callback;
1513         hcamcorder->vcapture_cb_param = user_data;
1514
1515         _MMCAMCORDER_UNLOCK_VCAPTURE_CALLBACK(hcamcorder);
1516
1517         return MM_ERROR_NONE;
1518 }
1519
1520 int _mmcamcorder_get_current_state(MMHandleType handle)
1521 {
1522         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1523
1524         _mmcam_dbg_log("");
1525
1526         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1527
1528         return _mmcamcorder_get_state(handle);
1529 }
1530
1531 int _mmcamcorder_init_focusing(MMHandleType handle)
1532 {
1533         int ret = TRUE;
1534         int state = MM_CAMCORDER_STATE_NONE;
1535         int focus_mode = MM_CAMCORDER_FOCUS_MODE_NONE;
1536         int af_range = MM_CAMCORDER_AUTO_FOCUS_NORMAL;
1537         int sensor_focus_mode = 0;
1538         int sensor_af_range = 0;
1539         int current_focus_mode = 0;
1540         int current_af_range = 0;
1541         mmf_camcorder_t *hcamcorder = NULL;
1542         _MMCamcorderSubContext *sc = NULL;
1543         GstCameraControl *control = NULL;
1544
1545         _mmcam_dbg_log("");
1546
1547         hcamcorder = MMF_CAMCORDER(handle);
1548         mmf_return_val_if_fail(handle, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1549
1550         sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
1551         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1552
1553         if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
1554                 _mmcam_dbg_err("Another command is running.");
1555                 return MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
1556         }
1557
1558         state = _mmcamcorder_get_state(handle);
1559
1560         if (state == MM_CAMCORDER_STATE_CAPTURING ||
1561             state < MM_CAMCORDER_STATE_PREPARE) {
1562                 _mmcam_dbg_err( "Not proper state. state[%d]", state );
1563                 _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1564                 return MM_ERROR_CAMCORDER_INVALID_STATE;
1565         }
1566
1567         if (!GST_IS_CAMERA_CONTROL(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst)) {
1568                 _mmcam_dbg_log("Can't cast Video source into camera control.");
1569                 _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1570                 return MM_ERROR_NONE;
1571         }
1572         control = GST_CAMERA_CONTROL (sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst);
1573
1574         ret = gst_camera_control_stop_auto_focus(control);
1575         if (!ret) {
1576                 _mmcam_dbg_err("Auto focusing stop fail.");
1577                 _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1578                 return MM_ERROR_CAMCORDER_DEVICE_IO;
1579         }
1580
1581         /* Initialize lens position */
1582         mm_camcorder_get_attributes(handle, NULL,
1583                                     MMCAM_CAMERA_FOCUS_MODE, &focus_mode,
1584                                     MMCAM_CAMERA_AF_SCAN_RANGE, &af_range,
1585                                     NULL);
1586         sensor_af_range = _mmcamcorder_convert_msl_to_sensor(handle, MM_CAM_CAMERA_AF_SCAN_RANGE, af_range);
1587         sensor_focus_mode = _mmcamcorder_convert_msl_to_sensor(handle, MM_CAM_CAMERA_FOCUS_MODE, focus_mode);
1588
1589         gst_camera_control_get_focus(control, &current_focus_mode, &current_af_range);
1590
1591         if (current_focus_mode != sensor_focus_mode ||
1592             current_af_range != sensor_af_range) {
1593                 ret = gst_camera_control_set_focus(control, sensor_focus_mode, sensor_af_range);
1594         } else {
1595                 _mmcam_dbg_log("No need to init FOCUS [mode:%d, range:%d]", focus_mode, af_range );
1596                 ret = TRUE;
1597         }
1598
1599         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1600
1601         if (ret) {
1602                 _mmcam_dbg_log("Lens init success.");
1603                 return MM_ERROR_NONE;
1604         } else {
1605                 _mmcam_dbg_err("Lens init fail.");
1606                 return MM_ERROR_CAMCORDER_DEVICE_IO;
1607         }
1608 }
1609
1610 int _mmcamcorder_adjust_focus(MMHandleType handle, int direction)
1611 {
1612         int state;
1613         int focus_mode = MM_CAMCORDER_FOCUS_MODE_NONE;
1614         int ret = MM_ERROR_UNKNOWN;
1615         char *err_attr_name = NULL;
1616
1617         mmf_return_val_if_fail(handle, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1618         mmf_return_val_if_fail(direction, MM_ERROR_CAMCORDER_INVALID_ARGUMENT);
1619
1620         _mmcam_dbg_log("");
1621
1622         if (!_MMCAMCORDER_TRYLOCK_CMD(handle)) {
1623                 _mmcam_dbg_err("Another command is running.");
1624                 return MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
1625         }
1626
1627         state = _mmcamcorder_get_state(handle);
1628         if (state == MM_CAMCORDER_STATE_CAPTURING ||
1629             state < MM_CAMCORDER_STATE_PREPARE) {
1630                 _mmcam_dbg_err("Not proper state. state[%d]", state);
1631                 _MMCAMCORDER_UNLOCK_CMD(handle);
1632                 return MM_ERROR_CAMCORDER_INVALID_STATE;
1633         }
1634
1635         /* TODO : call a auto or manual focus function */
1636         ret = mm_camcorder_get_attributes(handle, &err_attr_name,
1637                                           MMCAM_CAMERA_FOCUS_MODE, &focus_mode,
1638                                           NULL);
1639         if (ret != MM_ERROR_NONE) {
1640                 _mmcam_dbg_warn("Get focus-mode fail. (%s:%x)", err_attr_name, ret);
1641                 SAFE_FREE (err_attr_name);
1642                 _MMCAMCORDER_UNLOCK_CMD(handle);
1643                 return ret;
1644         }
1645
1646         if (focus_mode == MM_CAMCORDER_FOCUS_MODE_MANUAL) {
1647                 ret = _mmcamcorder_adjust_manual_focus(handle, direction);
1648         } else if (focus_mode == MM_CAMCORDER_FOCUS_MODE_AUTO ||
1649                    focus_mode == MM_CAMCORDER_FOCUS_MODE_TOUCH_AUTO ||
1650                    focus_mode == MM_CAMCORDER_FOCUS_MODE_CONTINUOUS) {
1651                 ret = _mmcamcorder_adjust_auto_focus(handle);
1652         } else {
1653                 _mmcam_dbg_err("It doesn't adjust focus. Focusing mode(%d)", focus_mode);
1654                 ret = MM_ERROR_CAMCORDER_NOT_SUPPORTED;
1655         }
1656
1657         _MMCAMCORDER_UNLOCK_CMD(handle);
1658
1659         return ret;
1660 }
1661
1662 int _mmcamcorder_adjust_manual_focus(MMHandleType handle, int direction)
1663 {
1664         int max_level = 0;
1665         int min_level = 0;
1666         int cur_level = 0;
1667         int focus_level = 0;
1668         float unit_level = 0;
1669         GstCameraControl *control = NULL;
1670         _MMCamcorderSubContext *sc = NULL;
1671         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1672
1673         _mmcam_dbg_log("");
1674
1675         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1676         mmf_return_val_if_fail(_MMFCAMCORDER_FOCUS_TOTAL_LEVEL != 1, MM_ERROR_CAMCORDER_NOT_SUPPORTED);
1677         mmf_return_val_if_fail((direction >= MM_CAMCORDER_MF_LENS_DIR_FORWARD) &&
1678                                (direction <= MM_CAMCORDER_MF_LENS_DIR_BACKWARD),
1679                                MM_ERROR_CAMCORDER_INVALID_ARGUMENT );
1680
1681         sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
1682         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1683
1684         if (!GST_IS_CAMERA_CONTROL(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst)) {
1685                 _mmcam_dbg_log("Can't cast Video source into camera control.");
1686                 return MM_ERROR_NONE;
1687         }
1688         control = GST_CAMERA_CONTROL (sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst);
1689
1690         /* TODO : get max, min level */
1691         if (max_level - min_level + 1 < _MMFCAMCORDER_FOCUS_TOTAL_LEVEL) {
1692                 _mmcam_dbg_warn("Total level  of manual focus of MMF is greater than that of the camera driver.");
1693         }
1694
1695         unit_level = ((float)max_level - (float)min_level)/(float)(_MMFCAMCORDER_FOCUS_TOTAL_LEVEL - 1);
1696
1697         if (!gst_camera_control_get_focus_level(control, &cur_level)) {
1698                 _mmcam_dbg_err("Can't get current level of manual focus.");
1699                 return MM_ERROR_CAMCORDER_DEVICE_IO;
1700         }
1701
1702         //TODO : adjust unit level value
1703         if (direction == MM_CAMCORDER_MF_LENS_DIR_FORWARD) {
1704                 focus_level = cur_level + unit_level;
1705         } else if (direction == MM_CAMCORDER_MF_LENS_DIR_BACKWARD) {
1706                 focus_level = cur_level - unit_level;
1707         }
1708
1709         if (focus_level > max_level) {
1710                 focus_level = max_level;
1711         } else if (focus_level < min_level) {
1712                 focus_level = min_level;
1713         }
1714
1715         if (!gst_camera_control_set_focus_level(control, focus_level)) {
1716                 _mmcam_dbg_err("Manual focusing fail.");
1717                 return MM_ERROR_CAMCORDER_DEVICE_IO;
1718         }
1719
1720         return MM_ERROR_NONE;
1721 }
1722
1723
1724 int _mmcamcorder_adjust_auto_focus(MMHandleType handle)
1725 {
1726         gboolean ret;
1727         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1728         GstCameraControl *control = NULL;
1729         _MMCamcorderSubContext *sc = NULL;
1730
1731         mmf_return_val_if_fail(handle, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1732
1733         _mmcam_dbg_log("");
1734
1735         sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
1736
1737         if (!GST_IS_CAMERA_CONTROL(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst)) {
1738                 _mmcam_dbg_log("Can't cast Video source into camera control.");
1739                 return MM_ERROR_NONE;
1740         }
1741         control = GST_CAMERA_CONTROL (sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst);
1742
1743         /* Start AF */
1744         ret = gst_camera_control_start_auto_focus(control);
1745         if (ret) {
1746                 _mmcam_dbg_log("Auto focusing start success.");
1747                 return MM_ERROR_NONE;
1748         } else {
1749                 _mmcam_dbg_err("Auto focusing start fail.");
1750                 return MM_ERROR_CAMCORDER_DEVICE_IO;
1751         }
1752 }
1753
1754 int _mmcamcorder_stop_focusing(MMHandleType handle)
1755 {
1756         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1757         _MMCamcorderSubContext *sc = NULL;
1758
1759         int ret, state;
1760         GstCameraControl *control = NULL;
1761
1762         mmf_return_val_if_fail(handle, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1763
1764         _mmcam_dbg_log("");
1765
1766         sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
1767
1768         if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
1769                 _mmcam_dbg_err("Another command is running.");
1770                 return MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
1771         }
1772
1773         state = _mmcamcorder_get_state(handle);
1774         if (state == MM_CAMCORDER_STATE_CAPTURING ||
1775             state < MM_CAMCORDER_STATE_PREPARE) {
1776                 _mmcam_dbg_err( "Not proper state. state[%d]", state );
1777                 _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1778                 return MM_ERROR_CAMCORDER_INVALID_STATE;
1779         }
1780
1781         if (!GST_IS_CAMERA_CONTROL(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst)) {
1782                 _mmcam_dbg_log("Can't cast Video source into camera control.");
1783                 _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1784                 return MM_ERROR_NONE;
1785         }
1786         control = GST_CAMERA_CONTROL(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst);
1787
1788         ret = gst_camera_control_stop_auto_focus(control);
1789
1790         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1791
1792         if (ret) {
1793                 _mmcam_dbg_log("Auto focusing stop success.");
1794                 return MM_ERROR_NONE;
1795         } else {
1796                 _mmcam_dbg_err("Auto focusing stop fail.");
1797                 return MM_ERROR_CAMCORDER_DEVICE_IO;
1798         }
1799 }
1800
1801
1802 /*-----------------------------------------------
1803 |         CAMCORDER INTERNAL LOCAL              |
1804 -----------------------------------------------*/
1805 static gboolean
1806 __mmcamcorder_gstreamer_init(camera_conf * conf)
1807 {
1808         static const int max_argc = 10;
1809         int i = 0;
1810         int cnt_str = 0;
1811         gint *argc = NULL;
1812         gchar **argv = NULL;
1813         GError *err = NULL;
1814         gboolean ret = FALSE;
1815         type_string_array *GSTInitOption = NULL;
1816
1817         mmf_return_val_if_fail(conf, FALSE);
1818
1819         _mmcam_dbg_log("");
1820
1821         /* alloc */
1822         argc = malloc(sizeof(int));
1823         argv = malloc(sizeof(gchar *) * max_argc);
1824
1825         if (!argc || !argv) {
1826                 goto ERROR;
1827         }
1828
1829         memset(argv, 0, sizeof(gchar *) * max_argc);
1830
1831         /* add initial */
1832         *argc = 1;
1833         argv[0] = g_strdup("mmcamcorder");
1834
1835         /* add gst_param */
1836         _mmcamcorder_conf_get_value_string_array(conf,
1837                                                  CONFIGURE_CATEGORY_MAIN_GENERAL,
1838                                                  "GSTInitOption",
1839                                                  &GSTInitOption);
1840         if (GSTInitOption != NULL && GSTInitOption->value) {
1841                 cnt_str = GSTInitOption->count;
1842                 for( ; *argc < max_argc && *argc <= cnt_str ; (*argc)++ )
1843                 {
1844                         argv[*argc] = g_strdup(GSTInitOption->value[(*argc)-1]);
1845                 }
1846         }
1847
1848         _mmcam_dbg_log("initializing gstreamer with following parameter[argc:%d]", *argc);
1849
1850         for (i = 0; i < *argc; i++) {
1851                 _mmcam_dbg_log("argv[%d] : %s", i, argv[i]);
1852         }
1853
1854         /* initializing gstreamer */
1855         ret = gst_init_check (argc, &argv, &err);
1856         if (!ret) {
1857                 _mmcam_dbg_err("Could not initialize GStreamer: %s ",
1858                                err ? err->message : "unknown error occurred");
1859                 if (err) {
1860                         g_error_free (err);
1861                 }
1862         }
1863
1864         /* release */
1865         for (i = 0; i < *argc; i++) {
1866                 if (argv[i]) {
1867                         free(argv[i]);
1868                         argv[i] = NULL;
1869                 }
1870         }
1871
1872         if (argv) {
1873                 free(argv);
1874                 argv = NULL;
1875         }
1876
1877         if (argc) {
1878                 free(argc);
1879                 argc = NULL;
1880         }
1881
1882         return ret;
1883
1884 ERROR:
1885         _mmcam_dbg_err("failed to initialize gstreamer");
1886
1887         if (argv) {
1888                 free(argv);
1889                 argv = NULL;
1890         }
1891
1892         if (argc) {
1893                 free(argc);
1894                 argc = NULL;
1895         }
1896
1897         return FALSE;
1898 }
1899
1900
1901 int _mmcamcorder_get_state(MMHandleType handle)
1902 {
1903         int state;
1904         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1905
1906         mmf_return_val_if_fail(hcamcorder, -1);
1907
1908         _MMCAMCORDER_LOCK_STATE(handle);
1909
1910         state = hcamcorder->state;
1911         _mmcam_dbg_log("state=%d",state);
1912
1913         _MMCAMCORDER_UNLOCK_STATE(handle);
1914
1915         return state;
1916 }
1917
1918
1919 void _mmcamcorder_set_state(MMHandleType handle, int state)
1920 {
1921         int old_state;
1922         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1923         _MMCamcorderMsgItem msg;
1924
1925         mmf_return_if_fail(hcamcorder);
1926
1927         _mmcam_dbg_log("");
1928
1929         _MMCAMCORDER_LOCK_STATE(handle);
1930
1931         old_state = hcamcorder->state;
1932         if(old_state != state) {
1933                 hcamcorder->state = state;
1934                 hcamcorder->target_state = state;
1935
1936                 _mmcam_dbg_log("set state[%d] and send state-changed message", state);
1937
1938                 /* To discern who changes the state */
1939                 switch (hcamcorder->state_change_by_system) {
1940                 case _MMCAMCORDER_STATE_CHANGE_BY_ASM:
1941                         msg.id = MM_MESSAGE_CAMCORDER_STATE_CHANGED_BY_ASM;
1942                         break;
1943                 case _MMCAMCORDER_STATE_CHANGE_NORMAL:
1944                 default:
1945                         msg.id = MM_MESSAGE_CAMCORDER_STATE_CHANGED;
1946                         msg.param.state.code = MM_ERROR_NONE;
1947                         break;
1948                 }
1949
1950                 msg.param.state.previous = old_state;
1951                 msg.param.state.current = state;
1952
1953                 _mmcam_dbg_log("_mmcamcroder_send_message : msg : %p, id:%x", &msg, msg.id);
1954                 _mmcamcroder_send_message(handle, &msg);
1955         }
1956
1957         _MMCAMCORDER_UNLOCK_STATE(handle);
1958
1959         return;
1960 }
1961
1962
1963 int _mmcamcorder_get_async_state(MMHandleType handle)
1964 {
1965         int state;
1966         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1967
1968         _MMCAMCORDER_LOCK_STATE(handle);
1969         state = hcamcorder->target_state;
1970
1971         _MMCAMCORDER_UNLOCK_STATE(handle);
1972
1973         return state;
1974 }
1975
1976
1977 _MMCamcorderSubContext *_mmcamcorder_alloc_subcontext(int type)
1978 {
1979         int i;
1980         _MMCamcorderSubContext *sc = NULL;
1981
1982         _mmcam_dbg_log("");
1983
1984         /* alloc container */
1985         sc = (_MMCamcorderSubContext *)malloc(sizeof(_MMCamcorderSubContext));
1986         mmf_return_val_if_fail(sc != NULL, NULL);
1987
1988         /* init members */
1989         memset(sc, 0x00, sizeof(_MMCamcorderSubContext));
1990
1991         sc->element_num = _MMCAMCORDER_PIPELINE_ELEMENT_NUM;
1992         sc->encode_element_num = _MMCAMCORDER_ENCODE_PIPELINE_ELEMENT_NUM;
1993
1994         /* alloc info for each mode */
1995         switch (type) {
1996         case MM_CAMCORDER_MODE_AUDIO:
1997                 sc->info_audio = malloc( sizeof(_MMCamcorderAudioInfo));
1998                 if(!sc->info_audio) {
1999                         _mmcam_dbg_err("Failed to alloc info structure");
2000                         goto ALLOC_SUBCONTEXT_FAILED;
2001                 }
2002                 memset(sc->info_audio, 0x00, sizeof(_MMCamcorderAudioInfo));
2003                 break;
2004         case MM_CAMCORDER_MODE_VIDEO_CAPTURE:
2005         default:
2006                 sc->info_image = malloc( sizeof(_MMCamcorderImageInfo));
2007                 if(!sc->info_image) {
2008                         _mmcam_dbg_err("Failed to alloc info structure");
2009                         goto ALLOC_SUBCONTEXT_FAILED;
2010                 }
2011                 memset(sc->info_image, 0x00, sizeof(_MMCamcorderImageInfo));
2012
2013                 /* init sound status */
2014                 sc->info_image->sound_status = _SOUND_STATUS_INIT;
2015
2016                 sc->info_video = malloc( sizeof(_MMCamcorderVideoInfo));
2017                 if(!sc->info_video) {
2018                         _mmcam_dbg_err("Failed to alloc info structure");
2019                         goto ALLOC_SUBCONTEXT_FAILED;
2020                 }
2021                 memset(sc->info_video, 0x00, sizeof(_MMCamcorderVideoInfo));
2022                 pthread_mutex_init(&(sc->info_video->size_check_lock), NULL);
2023                 break;
2024         }
2025
2026         /* alloc element array */
2027         sc->element = (_MMCamcorderGstElement *)malloc(sizeof(_MMCamcorderGstElement) * sc->element_num);
2028         if(!sc->element) {
2029                 _mmcam_dbg_err("Failed to alloc element structure");
2030                 goto ALLOC_SUBCONTEXT_FAILED;
2031         }
2032
2033         sc->encode_element = (_MMCamcorderGstElement *)malloc(sizeof(_MMCamcorderGstElement) * sc->encode_element_num);
2034         if(!sc->encode_element) {
2035                 _mmcam_dbg_err("Failed to alloc encode element structure");
2036                 goto ALLOC_SUBCONTEXT_FAILED;
2037         }
2038
2039         for (i = 0 ; i < sc->element_num ; i++) {
2040                 sc->element[i].id = _MMCAMCORDER_NONE;
2041                 sc->element[i].gst = NULL;
2042         }
2043
2044         for (i = 0 ; i < sc->encode_element_num ; i++) {
2045                 sc->encode_element[i].id = _MMCAMCORDER_NONE;
2046                 sc->encode_element[i].gst = NULL;
2047         }
2048
2049         sc->cam_stability_count = 0;
2050         sc->drop_vframe = 0;
2051         sc->pass_first_vframe = 0;
2052         sc->is_modified_rate = FALSE;
2053
2054         return sc;
2055
2056 ALLOC_SUBCONTEXT_FAILED:
2057         if (sc) {
2058                 if (sc->info_audio) {
2059                         free(sc->info_audio);
2060                         sc->info_audio = NULL;
2061                 }
2062                 if (sc->info_image) {
2063                         free(sc->info_image);
2064                         sc->info_image = NULL;
2065                 }
2066                 if (sc->info_video) {
2067                         pthread_mutex_destroy(&(sc->info_video->size_check_lock));
2068                         free(sc->info_video);
2069                         sc->info_video = NULL;
2070                 }
2071                 if (sc->element) {
2072                         free(sc->element);
2073                         sc->element = NULL;
2074                 }
2075                 if (sc->encode_element) {
2076                         free(sc->encode_element);
2077                         sc->encode_element = NULL;
2078                 }
2079                 free(sc);
2080                 sc = NULL;
2081         }
2082
2083         return NULL;
2084 }
2085
2086
2087 void _mmcamcorder_dealloc_subcontext(_MMCamcorderSubContext *sc)
2088 {
2089         _mmcam_dbg_log("");
2090
2091         if (sc) {
2092                 if (sc->element) {
2093                         _mmcam_dbg_log("release element");
2094                         free(sc->element);
2095                         sc->element = NULL;
2096                 }
2097
2098                 if (sc->encode_element) {
2099                         _mmcam_dbg_log("release encode_element");
2100                         free(sc->encode_element);
2101                         sc->encode_element = NULL;
2102                 }
2103
2104                 if (sc->info_image) {
2105                         _mmcam_dbg_log("release info_image");
2106                         free(sc->info_image);
2107                         sc->info_image = NULL;
2108                 }
2109
2110                 if (sc->info_video) {
2111                         _mmcam_dbg_log("release info_video");
2112                         if (sc->info_video->filename) {
2113                                 free(sc->info_video->filename);
2114                                 sc->info_video->filename = NULL;
2115                         }
2116                         pthread_mutex_destroy(&(sc->info_video->size_check_lock));
2117                         free(sc->info_video);
2118                         sc->info_video = NULL;
2119                 }
2120
2121                 if (sc->info_audio) {
2122                         _mmcam_dbg_log("release info_audio");
2123                         if (sc->info_audio->filename) {
2124                                 free(sc->info_audio->filename);
2125                                 sc->info_audio->filename = NULL;
2126                         }
2127                         free(sc->info_audio);
2128                         sc->info_audio = NULL;
2129                 }
2130
2131                 free(sc);
2132                 sc = NULL;
2133         }
2134
2135         return;
2136 }
2137
2138
2139 int _mmcamcorder_set_functions(MMHandleType handle, int type)
2140 {
2141         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
2142
2143         _mmcam_dbg_log("");
2144
2145         switch (type) {
2146         case MM_CAMCORDER_MODE_AUDIO:
2147                 hcamcorder->command = _mmcamcorder_audio_command;
2148                 break;
2149         case MM_CAMCORDER_MODE_VIDEO_CAPTURE:
2150         default:
2151                 hcamcorder->command = _mmcamcorder_video_capture_command;
2152                 break;
2153         }
2154
2155         return MM_ERROR_NONE;
2156 }
2157
2158
2159 gboolean _mmcamcorder_pipeline_cb_message(GstBus *bus, GstMessage *message, gpointer data)
2160 {
2161         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(data);
2162         _MMCamcorderSubContext *sc = NULL;
2163
2164         mmf_return_val_if_fail(hcamcorder, FALSE);
2165         mmf_return_val_if_fail(message, FALSE);
2166         //_mmcam_dbg_log("message type=(%d)", GST_MESSAGE_TYPE(message));
2167
2168         switch (GST_MESSAGE_TYPE(message)) {
2169         case GST_MESSAGE_UNKNOWN:
2170                 _mmcam_dbg_log("GST_MESSAGE_UNKNOWN");
2171                 break;
2172         case GST_MESSAGE_EOS:
2173         {
2174                 _mmcam_dbg_log ("Got EOS from element \"%s\".",
2175                                 GST_STR_NULL(GST_ELEMENT_NAME(GST_MESSAGE_SRC(message))));
2176
2177                 sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
2178                 mmf_return_val_if_fail(sc, TRUE);
2179
2180                 if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
2181                         mmf_return_val_if_fail(sc->info_video, TRUE);
2182                         if (sc->info_video->b_commiting) {
2183                                 _mmcamcorder_video_handle_eos((MMHandleType)hcamcorder);
2184                         }
2185                 } else {
2186                         mmf_return_val_if_fail(sc->info_audio, TRUE);
2187                         if (sc->info_audio->b_commiting) {
2188                                 _mmcamcorder_audio_handle_eos((MMHandleType)hcamcorder);
2189                         }
2190                 }
2191
2192                 sc->bget_eos = TRUE;
2193
2194                 break;
2195         }
2196         case GST_MESSAGE_ERROR:
2197         {
2198                 GError *err;
2199                 gchar *debug;
2200                 gst_message_parse_error(message, &err, &debug);
2201
2202                 _mmcam_dbg_err ("GSTERR: %s", err->message);
2203                 _mmcam_dbg_err ("Error Debug: %s", debug);
2204
2205                 __mmcamcorder_handle_gst_error((MMHandleType)hcamcorder, message, err);
2206
2207                 g_error_free (err);
2208                 g_free (debug);
2209                 break;
2210         }
2211         case GST_MESSAGE_WARNING:
2212         {
2213                 GError *err;
2214                 gchar *debug;
2215                 gst_message_parse_warning (message, &err, &debug);
2216
2217                 _mmcam_dbg_warn("GSTWARN: %s", err->message);
2218
2219                 __mmcamcorder_handle_gst_warning((MMHandleType)hcamcorder, message, err);
2220
2221                 g_error_free (err);
2222                 g_free (debug);
2223                 break;
2224         }
2225         case GST_MESSAGE_INFO:
2226                 _mmcam_dbg_log("GST_MESSAGE_INFO");
2227                 break;
2228         case GST_MESSAGE_TAG:
2229                 _mmcam_dbg_log("GST_MESSAGE_TAG");
2230                 break;
2231         case GST_MESSAGE_BUFFERING:
2232                 _mmcam_dbg_log("GST_MESSAGE_BUFFERING");
2233                 break;
2234         case GST_MESSAGE_STATE_CHANGED:
2235         {
2236                 const GValue *vnewstate;
2237                 GstState newstate;
2238                 GstElement *pipeline = NULL;
2239
2240                 sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
2241                 if ((sc) && (sc->element)) {
2242                         if (sc->element[_MMCAMCORDER_MAIN_PIPE].gst) {
2243                                 pipeline = sc->element[_MMCAMCORDER_MAIN_PIPE].gst;
2244                                 if (message->src == (GstObject*)pipeline) {
2245                                         vnewstate = (GValue*)gst_structure_get_value(gst_message_get_structure(message), "new-state");
2246                                         newstate = (GstState)vnewstate->data[0].v_int;
2247                                         _mmcam_dbg_log("GST_MESSAGE_STATE_CHANGED[%s]",gst_element_state_get_name(newstate));
2248                                 }
2249                         }
2250                 }
2251                 break;
2252         }
2253         case GST_MESSAGE_STATE_DIRTY:
2254                 _mmcam_dbg_log("GST_MESSAGE_STATE_DIRTY");
2255                 break;
2256         case GST_MESSAGE_STEP_DONE:
2257                 _mmcam_dbg_log("GST_MESSAGE_STEP_DONE");
2258                 break;
2259         case GST_MESSAGE_CLOCK_PROVIDE:
2260                 _mmcam_dbg_log("GST_MESSAGE_CLOCK_PROVIDE");
2261                 break;
2262         case GST_MESSAGE_CLOCK_LOST:
2263                 _mmcam_dbg_log("GST_MESSAGE_CLOCK_LOST");
2264                 break;
2265         case GST_MESSAGE_NEW_CLOCK:
2266         {
2267                 GstClock *pipe_clock = NULL;
2268                 gst_message_parse_new_clock(message, &pipe_clock);
2269                 _mmcam_dbg_log("GST_MESSAGE_NEW_CLOCK : %s", (pipe_clock ? GST_OBJECT_NAME (pipe_clock) : "NULL"));
2270                 break;
2271         }
2272         case GST_MESSAGE_STRUCTURE_CHANGE:
2273                 _mmcam_dbg_log("GST_MESSAGE_STRUCTURE_CHANGE");
2274                 break;
2275         case GST_MESSAGE_STREAM_STATUS:
2276                 _mmcam_dbg_log("GST_MESSAGE_STREAM_STATUS");
2277                 break;
2278         case GST_MESSAGE_APPLICATION:
2279                 _mmcam_dbg_log("GST_MESSAGE_APPLICATION");
2280                 break;
2281         case GST_MESSAGE_ELEMENT:
2282                 _mmcam_dbg_log("GST_MESSAGE_ELEMENT");
2283                 break;
2284         case GST_MESSAGE_SEGMENT_START:
2285                 _mmcam_dbg_log("GST_MESSAGE_SEGMENT_START");
2286                 break;
2287         case GST_MESSAGE_SEGMENT_DONE:
2288                 _mmcam_dbg_log("GST_MESSAGE_SEGMENT_DONE");
2289                 break;
2290         case GST_MESSAGE_DURATION_CHANGED:
2291                 _mmcam_dbg_log("GST_MESSAGE_DURATION_CHANGED");
2292                 break;
2293         case GST_MESSAGE_LATENCY:
2294                 _mmcam_dbg_log("GST_MESSAGE_LATENCY");
2295                 break;
2296         case GST_MESSAGE_ASYNC_START:
2297                 _mmcam_dbg_log("GST_MESSAGE_ASYNC_START");
2298                 break;
2299         case GST_MESSAGE_ASYNC_DONE:
2300                 _mmcam_dbg_log("GST_MESSAGE_ASYNC_DONE");
2301                 break;
2302         case GST_MESSAGE_ANY:
2303                 _mmcam_dbg_log("GST_MESSAGE_ANY");
2304                 break;
2305         case GST_MESSAGE_QOS:
2306 //              _mmcam_dbg_log("GST_MESSAGE_QOS");
2307                 break;
2308         default:
2309                 _mmcam_dbg_log("not handled message type=(%d)", GST_MESSAGE_TYPE(message));
2310                 break;
2311         }
2312
2313         return TRUE;
2314 }
2315
2316
2317 GstBusSyncReply _mmcamcorder_pipeline_bus_sync_callback(GstBus *bus, GstMessage *message, gpointer data)
2318 {
2319         GstElement *element = NULL;
2320         GError *err = NULL;
2321         gchar *debug_info = NULL;
2322
2323         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(data);
2324         _MMCamcorderSubContext *sc = NULL;
2325
2326         mmf_return_val_if_fail(hcamcorder, GST_BUS_PASS);
2327         mmf_return_val_if_fail(message, GST_BUS_PASS);
2328
2329         sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
2330         mmf_return_val_if_fail(sc, GST_BUS_PASS);
2331
2332         if (GST_MESSAGE_TYPE(message) == GST_MESSAGE_ERROR) {
2333                 /* parse error message */
2334                 gst_message_parse_error(message, &err, &debug_info);
2335
2336                 if (debug_info) {
2337                         _mmcam_dbg_err("GST ERROR : %s", debug_info);
2338                         g_free(debug_info);
2339                         debug_info = NULL;
2340                 }
2341
2342                 if (!err) {
2343                         _mmcam_dbg_warn("failed to parse error message");
2344                         return GST_BUS_PASS;
2345                 }
2346
2347                 /* set videosrc element to compare */
2348                 element = GST_ELEMENT_CAST(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst);
2349
2350                 /* check domain[RESOURCE] and element[VIDEOSRC] */
2351                 if (err->domain == GST_RESOURCE_ERROR &&
2352                     GST_ELEMENT_CAST(message->src) == element) {
2353                         switch (err->code) {
2354                         case GST_RESOURCE_ERROR_BUSY:
2355                                 _mmcam_dbg_err("Camera device [busy]");
2356                                 sc->error_code = MM_ERROR_CAMCORDER_DEVICE_BUSY;
2357                                 break;
2358                         case GST_RESOURCE_ERROR_OPEN_WRITE:
2359                                 _mmcam_dbg_err("Camera device [open failed]");
2360                                 sc->error_code = MM_ERROR_COMMON_INVALID_PERMISSION;
2361                                 //sc->error_code = MM_ERROR_CAMCORDER_DEVICE_OPEN; // SECURITY PART REQUEST PRIVILEGE
2362                                 break;
2363                         case GST_RESOURCE_ERROR_OPEN_READ_WRITE:
2364                                 _mmcam_dbg_err("Camera device [open failed]");
2365                                 sc->error_code = MM_ERROR_CAMCORDER_DEVICE_OPEN;
2366                                 break;
2367                         case GST_RESOURCE_ERROR_OPEN_READ:
2368                                 _mmcam_dbg_err("Camera device [register trouble]");
2369                                 sc->error_code = MM_ERROR_CAMCORDER_DEVICE_REG_TROUBLE;
2370                                 break;
2371                         case GST_RESOURCE_ERROR_NOT_FOUND:
2372                                 _mmcam_dbg_err("Camera device [device not found]");
2373                                 sc->error_code = MM_ERROR_CAMCORDER_DEVICE_NOT_FOUND;
2374                                 break;
2375                         case GST_RESOURCE_ERROR_TOO_LAZY:
2376                                 _mmcam_dbg_err("Camera device [timeout]");
2377                                 sc->error_code = MM_ERROR_CAMCORDER_DEVICE_TIMEOUT;
2378                                 break;
2379                         case GST_RESOURCE_ERROR_SETTINGS:
2380                                 _mmcam_dbg_err("Camera device [not supported]");
2381                                 sc->error_code = MM_ERROR_CAMCORDER_NOT_SUPPORTED;
2382                                 break;
2383                         case GST_RESOURCE_ERROR_FAILED:
2384                                 _mmcam_dbg_err("Camera device [working failed].");
2385                                 sc->error_code = MM_ERROR_CAMCORDER_DEVICE_IO;
2386                                 break;
2387                         default:
2388                                 _mmcam_dbg_err("Camera device [General(%d)]", err->code);
2389                                 sc->error_code = MM_ERROR_CAMCORDER_DEVICE;
2390                                 break;
2391                         }
2392
2393                         sc->error_occurs = TRUE;
2394                 }
2395
2396                 g_error_free(err);
2397
2398                 /* store error code and drop this message if cmd is running */
2399                 if (sc->error_code != MM_ERROR_NONE) {
2400                         _MMCamcorderMsgItem msg;
2401
2402                         /* post error to application */
2403                         sc->error_occurs = TRUE;
2404                         msg.id = MM_MESSAGE_CAMCORDER_ERROR;
2405                         msg.param.code = sc->error_code;
2406                         _mmcamcroder_send_message((MMHandleType)hcamcorder, &msg);
2407
2408                         goto DROP_MESSAGE;
2409                 }
2410         } else if (GST_MESSAGE_TYPE(message) == GST_MESSAGE_ELEMENT) {
2411                 _MMCamcorderMsgItem msg;
2412
2413                 if (gst_structure_has_name(gst_message_get_structure(message), "avsysvideosrc-AF") ||
2414                     gst_structure_has_name(gst_message_get_structure(message), "camerasrc-AF")) {
2415                         int focus_state = 0;
2416
2417                         gst_structure_get_int(gst_message_get_structure(message), "focus-state", &focus_state);
2418                         _mmcam_dbg_log("Focus State:%d", focus_state);
2419
2420                         msg.id = MM_MESSAGE_CAMCORDER_FOCUS_CHANGED;
2421                         msg.param.code = focus_state;
2422                         _mmcamcroder_send_message((MMHandleType)hcamcorder, &msg);
2423
2424                         goto DROP_MESSAGE;
2425                 } else if (gst_structure_has_name(gst_message_get_structure(message), "camerasrc-HDR")) {
2426                         int progress = 0;
2427                         int status = 0;
2428
2429                         if (gst_structure_get_int(gst_message_get_structure(message), "progress", &progress)) {
2430                                 gst_structure_get_int(gst_message_get_structure(message), "status", &status);
2431                                 _mmcam_dbg_log("HDR progress %d percent, status %d", progress, status);
2432
2433                                 msg.id = MM_MESSAGE_CAMCORDER_HDR_PROGRESS;
2434                                 msg.param.code = progress;
2435                                 _mmcamcroder_send_message((MMHandleType)hcamcorder, &msg);
2436                         }
2437
2438                         goto DROP_MESSAGE;
2439                 } else if (gst_structure_has_name(gst_message_get_structure(message), "camerasrc-FD")) {
2440                         int i = 0;
2441                         const GValue *g_value = gst_structure_get_value(gst_message_get_structure(message), "face-info");;
2442                         GstCameraControlFaceDetectInfo *fd_info = NULL;
2443                         MMCamFaceDetectInfo *cam_fd_info = NULL;
2444
2445                         if (g_value) {
2446                                 fd_info = (GstCameraControlFaceDetectInfo *)g_value_get_pointer(g_value);
2447                         }
2448
2449                         if (fd_info == NULL) {
2450                                 _mmcam_dbg_warn("fd_info is NULL");
2451                                 goto DROP_MESSAGE;
2452                         }
2453
2454                         cam_fd_info = (MMCamFaceDetectInfo *)malloc(sizeof(MMCamFaceDetectInfo));
2455                         if (cam_fd_info == NULL) {
2456                                 _mmcam_dbg_warn("cam_fd_info alloc failed");
2457
2458                                 free(fd_info);
2459                                 fd_info = NULL;
2460
2461                                 goto DROP_MESSAGE;
2462                         }
2463
2464                         /* set total face count */
2465                         cam_fd_info->num_of_faces = fd_info->num_of_faces;
2466
2467                         if (cam_fd_info->num_of_faces > 0) {
2468                                 cam_fd_info->face_info = (MMCamFaceInfo *)malloc(sizeof(MMCamFaceInfo) * cam_fd_info->num_of_faces);
2469                                 if (cam_fd_info->face_info) {
2470                                         /* set information of each face */
2471                                         for (i = 0 ; i < fd_info->num_of_faces ; i++) {
2472                                                 cam_fd_info->face_info[i].id = fd_info->face_info[i].id;
2473                                                 cam_fd_info->face_info[i].score = fd_info->face_info[i].score;
2474                                                 cam_fd_info->face_info[i].rect.x = fd_info->face_info[i].rect.x;
2475                                                 cam_fd_info->face_info[i].rect.y = fd_info->face_info[i].rect.y;
2476                                                 cam_fd_info->face_info[i].rect.width = fd_info->face_info[i].rect.width;
2477                                                 cam_fd_info->face_info[i].rect.height = fd_info->face_info[i].rect.height;
2478                                                 /*
2479                                                 _mmcam_dbg_log("id %d, score %d, [%d,%d,%dx%d]",
2480                                                                fd_info->face_info[i].id,
2481                                                                fd_info->face_info[i].score,
2482                                                                fd_info->face_info[i].rect.x,
2483                                                                fd_info->face_info[i].rect.y,
2484                                                                fd_info->face_info[i].rect.width,
2485                                                                fd_info->face_info[i].rect.height);
2486                                                 */
2487                                         }
2488                                 } else {
2489                                         _mmcam_dbg_warn("MMCamFaceInfo alloc failed");
2490
2491                                         /* free allocated memory that is not sent */
2492                                         free(cam_fd_info);
2493                                         cam_fd_info = NULL;
2494                                 }
2495                         } else {
2496                                 cam_fd_info->face_info = NULL;
2497                         }
2498
2499                         if (cam_fd_info) {
2500                                 /* send message  - cam_fd_info should be freed by application */
2501                                 msg.id = MM_MESSAGE_CAMCORDER_FACE_DETECT_INFO;
2502                                 msg.param.data = cam_fd_info;
2503                                 msg.param.size = sizeof(MMCamFaceDetectInfo);
2504                                 msg.param.code = 0;
2505
2506                                 _mmcamcroder_send_message((MMHandleType)hcamcorder, &msg);
2507                         }
2508
2509                         /* free fd_info allocated by plugin */
2510                         free(fd_info);
2511                         fd_info = NULL;
2512
2513                         goto DROP_MESSAGE;
2514                 } else if (gst_structure_has_name(gst_message_get_structure(message), "camerasrc-Capture")) {
2515                         int capture_done = FALSE;
2516
2517                         if (gst_structure_get_int(gst_message_get_structure(message), "capture-done", &capture_done)) {
2518                                 sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
2519                                 if (sc && sc->info_image) {
2520                                         /* play capture sound */
2521                                         _mmcamcorder_sound_solo_play((MMHandleType)hcamcorder, _MMCAMCORDER_FILEPATH_CAPTURE_SND, FALSE);
2522                                 }
2523                         }
2524
2525                         goto DROP_MESSAGE;
2526                 }
2527         }
2528
2529         return GST_BUS_PASS;
2530
2531 DROP_MESSAGE:
2532         gst_message_unref(message);
2533         message = NULL;
2534
2535         return GST_BUS_DROP;
2536 }
2537
2538
2539 int _mmcamcorder_create_pipeline(MMHandleType handle, int type)
2540 {
2541         int ret = MM_ERROR_NONE;
2542         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
2543         _MMCamcorderSubContext *sc = NULL;
2544         GstElement *pipeline = NULL;
2545
2546         _mmcam_dbg_log("handle : %x, type : %d", handle, type);
2547
2548         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2549
2550         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2551         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2552
2553         switch (type) {
2554         case MM_CAMCORDER_MODE_AUDIO:
2555                 ret = _mmcamcorder_create_audio_pipeline(handle);
2556                 if (ret != MM_ERROR_NONE) {
2557                         return ret;
2558                 }
2559                 break;
2560         case MM_CAMCORDER_MODE_VIDEO_CAPTURE:
2561         default:
2562                 ret = _mmcamcorder_create_preview_pipeline(handle);
2563                 if (ret != MM_ERROR_NONE) {
2564                         return ret;
2565                 }
2566
2567                 /* connect capture signal */
2568                 if (!sc->bencbin_capture) {
2569                         ret = _mmcamcorder_connect_capture_signal(handle);
2570                         if (ret != MM_ERROR_NONE) {
2571                                 return ret;
2572                         }
2573                 }
2574                 break;
2575         }
2576
2577         pipeline = sc->element[_MMCAMCORDER_MAIN_PIPE].gst;
2578         if (type != MM_CAMCORDER_MODE_AUDIO) {
2579                 ret = _mmcamcorder_gst_set_state(handle, pipeline, GST_STATE_READY);
2580         }
2581 #ifdef _MMCAMCORDER_GET_DEVICE_INFO
2582         if (!_mmcamcorder_get_device_info(handle)) {
2583                 _mmcam_dbg_err("Getting device information error!!");
2584         }
2585 #endif
2586
2587         _mmcam_dbg_log("ret[%x]", ret);
2588         if (ret != MM_ERROR_NONE) {
2589                 _mmcam_dbg_err("error : destroy pipeline");
2590                 _mmcamcorder_destroy_pipeline(handle, hcamcorder->type);
2591         }
2592
2593         return ret;
2594 }
2595
2596
2597 void _mmcamcorder_destroy_pipeline(MMHandleType handle, int type)
2598 {
2599         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
2600         _MMCamcorderSubContext *sc = NULL;
2601         gint i = 0;
2602         int element_num = 0;
2603         _MMCamcorderGstElement *element = NULL;
2604         GstBus *bus = NULL;
2605
2606         mmf_return_if_fail(hcamcorder);
2607
2608         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2609         mmf_return_if_fail(sc);
2610
2611         _mmcam_dbg_log("");
2612
2613         /* Inside each pipeline destroy function, Set GST_STATE_NULL to Main pipeline */
2614         switch (type) {
2615         case MM_CAMCORDER_MODE_VIDEO_CAPTURE:
2616                 element = sc->element;
2617                 element_num = sc->element_num;
2618                 bus = gst_pipeline_get_bus(GST_PIPELINE(sc->element[_MMCAMCORDER_MAIN_PIPE].gst));
2619                 _mmcamcorder_destroy_video_capture_pipeline(handle);
2620                 break;
2621         case MM_CAMCORDER_MODE_AUDIO:
2622                 element = sc->encode_element;
2623                 element_num = sc->encode_element_num;
2624                 bus = gst_pipeline_get_bus(GST_PIPELINE(sc->encode_element[_MMCAMCORDER_ENCODE_MAIN_PIPE].gst));
2625                 _mmcamcorder_destroy_audio_pipeline(handle);
2626                 break;
2627         default:
2628                 _mmcam_dbg_err("unknown type %d", type);
2629                 break;
2630         }
2631
2632         _mmcam_dbg_log("Pipeline clear!!");
2633
2634         /* Remove pipeline message callback */
2635         if (hcamcorder->pipeline_cb_event_id > 0) {
2636                 g_source_remove(hcamcorder->pipeline_cb_event_id);
2637                 hcamcorder->pipeline_cb_event_id = 0;
2638         }
2639
2640         /* Remove remained message in bus */
2641         if (bus) {
2642                 GstMessage *gst_msg = NULL;
2643                 while ((gst_msg = gst_bus_pop(bus)) != NULL) {
2644                         _mmcamcorder_pipeline_cb_message(bus, gst_msg, (gpointer)hcamcorder);
2645                         gst_message_unref( gst_msg );
2646                         gst_msg = NULL;
2647                 }
2648                 gst_object_unref( bus );
2649                 bus = NULL;
2650         }
2651
2652         /* checking unreleased element */
2653         for (i = 0 ; i < element_num ; i++ ) {
2654                 if (element[i].gst) {
2655                         if (GST_IS_ELEMENT(element[i].gst)) {
2656                                 _mmcam_dbg_warn("Still alive element - ID[%d], name [%s], ref count[%d], status[%s]",
2657                                                 element[i].id,
2658                                                 GST_OBJECT_NAME(element[i].gst),
2659                                                 GST_OBJECT_REFCOUNT(element[i].gst),
2660                                                 gst_element_state_get_name(GST_STATE(element[i].gst)));
2661                                 g_object_weak_unref(G_OBJECT(element[i].gst), (GWeakNotify)_mmcamcorder_element_release_noti, sc);
2662                         } else {
2663                                 _mmcam_dbg_warn("The element[%d] is still aliving, check it", element[i].id);
2664                         }
2665
2666                         element[i].id = _MMCAMCORDER_NONE;
2667                         element[i].gst = NULL;
2668                 }
2669         }
2670
2671         return;
2672 }
2673
2674
2675 int _mmcamcorder_gst_set_state_async(MMHandleType handle, GstElement *pipeline, GstState target_state)
2676 {
2677         GstStateChangeReturn setChangeReturn = GST_STATE_CHANGE_FAILURE;
2678
2679         _MMCAMCORDER_LOCK_GST_STATE(handle);
2680         setChangeReturn = gst_element_set_state(pipeline, target_state);
2681         _MMCAMCORDER_UNLOCK_GST_STATE(handle);
2682
2683         return setChangeReturn;
2684 }
2685
2686
2687 #ifdef _MMCAMCORDER_USE_SET_ATTR_CB
2688 static gboolean __mmcamcorder_set_attr_to_camsensor_cb(gpointer data)
2689 {
2690         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(data);
2691
2692         mmf_return_val_if_fail(hcamcorder, FALSE);
2693
2694         _mmcam_dbg_log("");
2695
2696         _mmcamcorder_set_attribute_to_camsensor((MMHandleType)hcamcorder);
2697
2698         /* initialize */
2699         hcamcorder->setting_event_id = 0;
2700
2701         _mmcam_dbg_log("Done");
2702
2703         /* once */
2704         return FALSE;
2705 }
2706 #endif /* _MMCAMCORDER_USE_SET_ATTR_CB */
2707
2708
2709 int _mmcamcorder_gst_set_state(MMHandleType handle, GstElement *pipeline, GstState target_state)
2710 {
2711         unsigned int k = 0;
2712         _MMCamcorderSubContext *sc = NULL;
2713         GstState pipeline_state = GST_STATE_VOID_PENDING;
2714         GstStateChangeReturn setChangeReturn = GST_STATE_CHANGE_FAILURE;
2715         GstStateChangeReturn getChangeReturn = GST_STATE_CHANGE_FAILURE;
2716         GstClockTime get_timeout = __MMCAMCORDER_SET_GST_STATE_TIMEOUT * GST_SECOND;
2717         pthread_mutex_t *state_lock = NULL;
2718
2719         mmf_return_val_if_fail(handle, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2720         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2721         mmf_return_val_if_fail(sc && sc->element, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2722
2723         if (sc->element[_MMCAMCORDER_MAIN_PIPE].gst == pipeline) {
2724                 _mmcam_dbg_log("Set state to %d - PREVIEW PIPELINE", target_state);
2725                 state_lock = &_MMCAMCORDER_GET_GST_STATE_LOCK(handle);
2726         } else {
2727                 _mmcam_dbg_log("Set state to %d - ENDODE PIPELINE", target_state);
2728                 state_lock = &_MMCAMCORDER_GET_GST_ENCODE_STATE_LOCK(handle);
2729         }
2730
2731         pthread_mutex_lock(state_lock);
2732
2733         for (k = 0; k < _MMCAMCORDER_STATE_SET_COUNT; k++) {
2734                 setChangeReturn = gst_element_set_state(pipeline, target_state);
2735                 _mmcam_dbg_log("gst_element_set_state[%d] return %d",
2736                                target_state, setChangeReturn);
2737                 if (setChangeReturn != GST_STATE_CHANGE_FAILURE) {
2738                         getChangeReturn = gst_element_get_state(pipeline, &pipeline_state, NULL, get_timeout);
2739                         switch (getChangeReturn) {
2740                         case GST_STATE_CHANGE_NO_PREROLL:
2741                                 _mmcam_dbg_log("status=GST_STATE_CHANGE_NO_PREROLL.");
2742                         case GST_STATE_CHANGE_SUCCESS:
2743                                 /* if we reached the final target state, exit */
2744                                 if (pipeline_state == target_state) {
2745                                         _mmcam_dbg_log("Set state to %d - DONE", target_state);
2746                                         pthread_mutex_unlock(state_lock);
2747                                         return MM_ERROR_NONE;
2748                                 }
2749                                 break;
2750                         case GST_STATE_CHANGE_ASYNC:
2751                                 _mmcam_dbg_log("status=GST_STATE_CHANGE_ASYNC.");
2752                                 break;
2753                         default:
2754                                 pthread_mutex_unlock(state_lock);
2755                                 _mmcam_dbg_log("status=GST_STATE_CHANGE_FAILURE.");
2756                                 return MM_ERROR_CAMCORDER_GST_STATECHANGE;
2757                         }
2758
2759                         pthread_mutex_unlock(state_lock);
2760
2761                         _mmcam_dbg_err("timeout of gst_element_get_state()!!");
2762
2763                         return MM_ERROR_CAMCORDER_RESPONSE_TIMEOUT;
2764                 }
2765                 usleep(_MMCAMCORDER_STATE_CHECK_INTERVAL);
2766         }
2767
2768         pthread_mutex_unlock(state_lock);
2769
2770         _mmcam_dbg_err("Failure. gst_element_set_state timeout!!");
2771
2772         return MM_ERROR_CAMCORDER_RESPONSE_TIMEOUT;
2773 }
2774
2775
2776 /* For performance check */
2777 int _mmcamcorder_video_current_framerate(MMHandleType handle)
2778 {
2779         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
2780         _MMCamcorderSubContext *sc = NULL;
2781
2782         mmf_return_val_if_fail(hcamcorder, -1);
2783
2784         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2785         mmf_return_val_if_fail(sc, -1);
2786
2787         return sc->kpi.current_fps;
2788 }
2789
2790
2791 int _mmcamcorder_video_average_framerate(MMHandleType handle)
2792 {
2793         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
2794         _MMCamcorderSubContext *sc = NULL;
2795
2796         mmf_return_val_if_fail(hcamcorder, -1);
2797
2798         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2799         mmf_return_val_if_fail(sc, -1);
2800
2801         return sc->kpi.average_fps;
2802 }
2803
2804
2805 void _mmcamcorder_video_current_framerate_init(MMHandleType handle)
2806 {
2807         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
2808         _MMCamcorderSubContext *sc = NULL;
2809
2810         mmf_return_if_fail(hcamcorder);
2811
2812         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2813         mmf_return_if_fail(sc);
2814
2815         memset(&(sc->kpi), 0x00, sizeof(_MMCamcorderKPIMeasure));
2816
2817         return;
2818 }
2819
2820
2821 static gboolean __mmcamcorder_handle_gst_error(MMHandleType handle, GstMessage *message, GError *error)
2822 {
2823         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
2824         _MMCamcorderMsgItem msg;
2825         gchar *msg_src_element;
2826         _MMCamcorderSubContext *sc = NULL;
2827
2828         return_val_if_fail(hcamcorder, FALSE);
2829         return_val_if_fail(error, FALSE);
2830         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2831         mmf_return_val_if_fail(sc, FALSE);
2832
2833         _mmcam_dbg_log("");
2834
2835         /* filtering filesink related errors */
2836         if (hcamcorder->state == MM_CAMCORDER_STATE_RECORDING &&
2837             (error->code ==  GST_RESOURCE_ERROR_WRITE || error->code ==  GST_RESOURCE_ERROR_SEEK)) {
2838                 if (sc->ferror_count == 2 && sc->ferror_send == FALSE) {
2839                         sc->ferror_send = TRUE;
2840                         msg.param.code = __mmcamcorder_gst_handle_resource_error(handle, error->code, message);
2841                 } else {
2842                         sc->ferror_count++;
2843                         _mmcam_dbg_warn("Skip error");
2844                         return TRUE;
2845                 }
2846         }
2847
2848         if (error->domain == GST_CORE_ERROR) {
2849                 msg.param.code = __mmcamcorder_gst_handle_core_error(handle, error->code, message);
2850         } else if (error->domain == GST_LIBRARY_ERROR) {
2851                 msg.param.code = __mmcamcorder_gst_handle_library_error(handle, error->code, message);
2852         } else if (error->domain == GST_RESOURCE_ERROR) {
2853                 msg.param.code = __mmcamcorder_gst_handle_resource_error(handle, error->code, message);
2854         } else if (error->domain == GST_STREAM_ERROR) {
2855                 msg.param.code = __mmcamcorder_gst_handle_stream_error(handle, error->code, message);
2856         } else {
2857                 _mmcam_dbg_warn("This error domain is not defined.");
2858
2859                 /* we treat system error as an internal error */
2860                 msg.param.code = MM_ERROR_CAMCORDER_INTERNAL;
2861         }
2862
2863         if (message->src) {
2864                 msg_src_element = GST_ELEMENT_NAME(GST_ELEMENT_CAST(message->src));
2865                 _mmcam_dbg_err("-Msg src : [%s] Domain : [%s]   Error : [%s]  Code : [%d] is tranlated to error code : [0x%x]",
2866                                msg_src_element, g_quark_to_string (error->domain), error->message, error->code, msg.param.code);
2867         } else {
2868                 _mmcam_dbg_err("Domain : [%s]   Error : [%s]  Code : [%d] is tranlated to error code : [0x%x]",
2869                                g_quark_to_string (error->domain), error->message, error->code, msg.param.code);
2870         }
2871
2872 #ifdef _MMCAMCORDER_SKIP_GST_FLOW_ERROR
2873         /* Check whether send this error to application */
2874         if (msg.param.code == MM_ERROR_CAMCORDER_GST_FLOW_ERROR) {
2875                 _mmcam_dbg_log("We got the error. But skip it.");
2876                 return TRUE;
2877         }
2878 #endif /* _MMCAMCORDER_SKIP_GST_FLOW_ERROR */
2879
2880         /* post error to application */
2881         sc->error_occurs = TRUE;
2882         msg.id = MM_MESSAGE_CAMCORDER_ERROR;
2883         _mmcamcroder_send_message(handle, &msg);
2884
2885         return TRUE;
2886 }
2887
2888
2889 static gint __mmcamcorder_gst_handle_core_error(MMHandleType handle, int code, GstMessage *message)
2890 {
2891         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
2892         _MMCamcorderSubContext *sc = NULL;
2893         GstElement *element = NULL;
2894
2895         _mmcam_dbg_log("");
2896
2897         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2898
2899         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2900         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2901
2902         /* Specific plugin - video encoder plugin */
2903         element = GST_ELEMENT_CAST(sc->encode_element[_MMCAMCORDER_ENCSINK_VENC].gst);
2904
2905         if (GST_ELEMENT_CAST(message->src) == element) {
2906                 if (code == GST_CORE_ERROR_NEGOTIATION) {
2907                         return MM_ERROR_CAMCORDER_GST_NEGOTIATION;
2908                 } else {
2909                         return MM_ERROR_CAMCORDER_ENCODER;
2910                 }
2911         }
2912
2913         /* General */
2914         switch (code)
2915         {
2916                 case GST_CORE_ERROR_STATE_CHANGE:
2917                         return MM_ERROR_CAMCORDER_GST_STATECHANGE;
2918                 case GST_CORE_ERROR_NEGOTIATION:
2919                         return MM_ERROR_CAMCORDER_GST_NEGOTIATION;
2920                 case GST_CORE_ERROR_MISSING_PLUGIN:
2921                 case GST_CORE_ERROR_SEEK:
2922                 case GST_CORE_ERROR_NOT_IMPLEMENTED:
2923                 case GST_CORE_ERROR_FAILED:
2924                 case GST_CORE_ERROR_TOO_LAZY:
2925                 case GST_CORE_ERROR_PAD:
2926                 case GST_CORE_ERROR_THREAD:
2927                 case GST_CORE_ERROR_EVENT:
2928                 case GST_CORE_ERROR_CAPS:
2929                 case GST_CORE_ERROR_TAG:
2930                 case GST_CORE_ERROR_CLOCK:
2931                 case GST_CORE_ERROR_DISABLED:
2932                 default:
2933                         return MM_ERROR_CAMCORDER_GST_CORE;
2934                 break;
2935         }
2936 }
2937
2938 static gint __mmcamcorder_gst_handle_library_error(MMHandleType handle, int code, GstMessage *message)
2939 {
2940         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
2941         return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2942
2943         _mmcam_dbg_log("");
2944
2945         /* Specific plugin - NONE */
2946
2947         /* General */
2948         switch (code) {
2949         case GST_LIBRARY_ERROR_FAILED:
2950         case GST_LIBRARY_ERROR_TOO_LAZY:
2951         case GST_LIBRARY_ERROR_INIT:
2952         case GST_LIBRARY_ERROR_SHUTDOWN:
2953         case GST_LIBRARY_ERROR_SETTINGS:
2954         case GST_LIBRARY_ERROR_ENCODE:
2955         default:
2956                 _mmcam_dbg_err("Library error(%d)", code);
2957                 return MM_ERROR_CAMCORDER_GST_LIBRARY;
2958         }
2959 }
2960
2961
2962 static gint __mmcamcorder_gst_handle_resource_error(MMHandleType handle, int code, GstMessage *message)
2963 {
2964         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
2965         _MMCamcorderSubContext *sc = NULL;
2966         GstElement *element = NULL;
2967
2968         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2969
2970         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
2971         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2972
2973         _mmcam_dbg_log("");
2974
2975         /* Specific plugin */
2976         /* video sink */
2977         element = GST_ELEMENT_CAST(sc->element[_MMCAMCORDER_VIDEOSINK_SINK].gst);
2978         if (GST_ELEMENT_CAST(message->src) == element) {
2979                 if (code == GST_RESOURCE_ERROR_WRITE) {
2980                         _mmcam_dbg_err("Display device [Off]");
2981                         return MM_ERROR_CAMCORDER_DISPLAY_DEVICE_OFF;
2982                 } else {
2983                         _mmcam_dbg_err("Display device [General(%d)]", code);
2984                 }
2985         }
2986
2987         /* encodebin */
2988         element = GST_ELEMENT_CAST(sc->encode_element[_MMCAMCORDER_ENCSINK_VENC].gst);
2989         if (GST_ELEMENT_CAST(message->src) == element) {
2990                 if (code == GST_RESOURCE_ERROR_FAILED) {
2991                         _mmcam_dbg_err("Encoder [Resource error]");
2992                         return MM_ERROR_CAMCORDER_ENCODER_BUFFER;
2993                 } else {
2994                         _mmcam_dbg_err("Encoder [General(%d)]", code);
2995                         return MM_ERROR_CAMCORDER_ENCODER;
2996                 }
2997         }
2998
2999         /* General */
3000         switch (code) {
3001         case GST_RESOURCE_ERROR_WRITE:
3002                 _mmcam_dbg_err("File write error");
3003                 return MM_ERROR_FILE_WRITE;
3004         case GST_RESOURCE_ERROR_NO_SPACE_LEFT:
3005                 _mmcam_dbg_err("No left space");
3006                 return MM_MESSAGE_CAMCORDER_NO_FREE_SPACE;
3007         case GST_RESOURCE_ERROR_OPEN_WRITE:
3008                 _mmcam_dbg_err("Out of storage");
3009                 return MM_ERROR_OUT_OF_STORAGE;
3010         case GST_RESOURCE_ERROR_SEEK:
3011                 _mmcam_dbg_err("File read(seek)");
3012                 return MM_ERROR_FILE_READ;
3013         case GST_RESOURCE_ERROR_NOT_FOUND:
3014         case GST_RESOURCE_ERROR_FAILED:
3015         case GST_RESOURCE_ERROR_TOO_LAZY:
3016         case GST_RESOURCE_ERROR_BUSY:
3017         case GST_RESOURCE_ERROR_OPEN_READ:
3018         case GST_RESOURCE_ERROR_OPEN_READ_WRITE:
3019         case GST_RESOURCE_ERROR_CLOSE:
3020         case GST_RESOURCE_ERROR_READ:
3021         case GST_RESOURCE_ERROR_SYNC:
3022         case GST_RESOURCE_ERROR_SETTINGS:
3023         default:
3024                 _mmcam_dbg_err("Resource error(%d)", code);
3025                 return MM_ERROR_CAMCORDER_GST_RESOURCE;
3026         }
3027 }
3028
3029
3030 static gint __mmcamcorder_gst_handle_stream_error(MMHandleType handle, int code, GstMessage *message)
3031 {
3032         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
3033         _MMCamcorderSubContext *sc = NULL;
3034         GstElement *element =NULL;
3035
3036         mmf_return_val_if_fail( hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED );
3037
3038         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
3039         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
3040
3041         _mmcam_dbg_log("");
3042
3043         /* Specific plugin */
3044         /* video encoder */
3045         element = GST_ELEMENT_CAST(sc->encode_element[_MMCAMCORDER_ENCSINK_VENC].gst);
3046         if (GST_ELEMENT_CAST(message->src) == element) {
3047                 switch (code) {
3048                 case GST_STREAM_ERROR_WRONG_TYPE:
3049                         _mmcam_dbg_err("Video encoder [wrong stream type]");
3050                         return MM_ERROR_CAMCORDER_ENCODER_WRONG_TYPE;
3051                 case GST_STREAM_ERROR_ENCODE:
3052                         _mmcam_dbg_err("Video encoder [encode error]");
3053                         return MM_ERROR_CAMCORDER_ENCODER_WORKING;
3054                 case GST_STREAM_ERROR_FAILED:
3055                         _mmcam_dbg_err("Video encoder [stream failed]");
3056                         return MM_ERROR_CAMCORDER_ENCODER_WORKING;
3057                 default:
3058                         _mmcam_dbg_err("Video encoder [General(%d)]", code);
3059                         return MM_ERROR_CAMCORDER_ENCODER;
3060                 }
3061         }
3062
3063         /* General plugin */
3064         switch (code) {
3065         case GST_STREAM_ERROR_FORMAT:
3066                 _mmcam_dbg_err("General [negotiation error(%d)]", code);
3067                 return MM_ERROR_CAMCORDER_GST_NEGOTIATION;
3068         case GST_STREAM_ERROR_FAILED:
3069                 _mmcam_dbg_err("General [flow error(%d)]", code);
3070                 return MM_ERROR_CAMCORDER_GST_FLOW_ERROR;
3071         case GST_STREAM_ERROR_TYPE_NOT_FOUND:
3072         case GST_STREAM_ERROR_DECODE:
3073         case GST_STREAM_ERROR_CODEC_NOT_FOUND:
3074         case GST_STREAM_ERROR_NOT_IMPLEMENTED:
3075         case GST_STREAM_ERROR_TOO_LAZY:
3076         case GST_STREAM_ERROR_ENCODE:
3077         case GST_STREAM_ERROR_DEMUX:
3078         case GST_STREAM_ERROR_MUX:
3079         case GST_STREAM_ERROR_DECRYPT:
3080         case GST_STREAM_ERROR_DECRYPT_NOKEY:
3081         case GST_STREAM_ERROR_WRONG_TYPE:
3082         default:
3083                 _mmcam_dbg_err("General [error(%d)]", code);
3084                 return MM_ERROR_CAMCORDER_GST_STREAM;
3085         }
3086 }
3087
3088
3089 static gboolean __mmcamcorder_handle_gst_warning (MMHandleType handle, GstMessage *message, GError *error)
3090 {
3091         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
3092         gchar *debug = NULL;
3093         GError *err = NULL;
3094
3095         return_val_if_fail(hcamcorder, FALSE);
3096         return_val_if_fail(error, FALSE);
3097
3098         _mmcam_dbg_log("");
3099
3100         gst_message_parse_warning(message, &err, &debug);
3101
3102         if (error->domain == GST_CORE_ERROR) {
3103                 _mmcam_dbg_warn("GST warning: GST_CORE domain");
3104         } else if (error->domain == GST_LIBRARY_ERROR) {
3105                 _mmcam_dbg_warn("GST warning: GST_LIBRARY domain");
3106         } else if (error->domain == GST_RESOURCE_ERROR) {
3107                 _mmcam_dbg_warn("GST warning: GST_RESOURCE domain");
3108                 __mmcamcorder_gst_handle_resource_warning(handle, message, error);
3109         } else if (error->domain == GST_STREAM_ERROR ) {
3110                 _mmcam_dbg_warn("GST warning: GST_STREAM domain");
3111         } else {
3112                 _mmcam_dbg_warn("This error domain(%d) is not defined.", error->domain);
3113         }
3114
3115         if (err != NULL) {
3116                 g_error_free(err);
3117                 err = NULL;
3118         }
3119         if (debug != NULL) {
3120                 _mmcam_dbg_err ("Debug: %s", debug);
3121                 g_free(debug);
3122                 debug = NULL;
3123         }
3124
3125         return TRUE;
3126 }
3127
3128
3129 static gint __mmcamcorder_gst_handle_resource_warning(MMHandleType handle, GstMessage *message , GError *error)
3130 {
3131         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
3132         _MMCamcorderSubContext *sc = NULL;
3133         GstElement *element =NULL;
3134         gchar *msg_src_element;
3135
3136         mmf_return_val_if_fail( hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED );
3137
3138         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
3139         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
3140
3141         _mmcam_dbg_log("");
3142
3143         /* Special message handling */
3144         /* video source */
3145         element = GST_ELEMENT_CAST(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst);
3146         if (GST_ELEMENT_CAST(message->src) == element) {
3147                 if (error->code == GST_RESOURCE_ERROR_FAILED) {
3148                         msg_src_element = GST_ELEMENT_NAME(GST_ELEMENT_CAST(message->src));
3149                         _mmcam_dbg_warn("-Msg src:[%s] Domain:[%s] Error:[%s]",
3150                                        msg_src_element, g_quark_to_string(error->domain), error->message);
3151                         return MM_ERROR_NONE;
3152                 }
3153         }
3154
3155         /* General plugin */
3156         switch (error->code) {
3157         case GST_RESOURCE_ERROR_WRITE:
3158         case GST_RESOURCE_ERROR_NO_SPACE_LEFT:
3159         case GST_RESOURCE_ERROR_SEEK:
3160         case GST_RESOURCE_ERROR_NOT_FOUND:
3161         case GST_RESOURCE_ERROR_FAILED:
3162         case GST_RESOURCE_ERROR_TOO_LAZY:
3163         case GST_RESOURCE_ERROR_BUSY:
3164         case GST_RESOURCE_ERROR_OPEN_READ:
3165         case GST_RESOURCE_ERROR_OPEN_WRITE:
3166         case GST_RESOURCE_ERROR_OPEN_READ_WRITE:
3167         case GST_RESOURCE_ERROR_CLOSE:
3168         case GST_RESOURCE_ERROR_READ:
3169         case GST_RESOURCE_ERROR_SYNC:
3170         case GST_RESOURCE_ERROR_SETTINGS:
3171         default:
3172                 _mmcam_dbg_warn("General GST warning(%d)", error->code);
3173                 break;
3174         }
3175
3176         return MM_ERROR_NONE;
3177 }