Support new capture mode
[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 <stdlib.h>
27 #include <string.h>
28 #include <fcntl.h>
29 #include <gst/gst.h>
30 #include <gst/gstutils.h>
31 #include <gst/gstpad.h>
32 #include <sys/time.h>
33
34 #include <mm_error.h>
35 #include "mm_camcorder_internal.h"
36 #include <mm_types.h>
37
38 #include <gst/video/colorbalance.h>
39 #include <asm/types.h>
40
41 #include <system_info.h>
42
43 #ifdef _MMCAMCORDER_RM_SUPPORT
44 #include "mm_camcorder_rm.h"
45 #endif /* _MMCAMCORDER_RM_SUPPORT */
46
47 /*---------------------------------------------------------------------------------------
48 |    LOCAL VARIABLE DEFINITIONS for internal                                            |
49 ---------------------------------------------------------------------------------------*/
50 #define __MMCAMCORDER_CMD_ITERATE_MAX           3
51 #define __MMCAMCORDER_SET_GST_STATE_TIMEOUT     10
52 #define __MMCAMCORDER_FORCE_STOP_TRY_COUNT      30
53 #define __MMCAMCORDER_FORCE_STOP_WAIT_TIME      100000  /* us */
54 #define __MMCAMCORDER_SOUND_WAIT_TIMEOUT        3
55 #define __MMCAMCORDER_FOCUS_CHANGE_REASON_LEN   64
56 #define __MMCAMCORDER_CONF_FILENAME_LENGTH      64
57
58 #ifdef _MMCAMCORDER_CAMERA_CONF_MGR_SUPPORT
59 #define __MMCAMCORDER_DEVICE_TYPE_STRING_LEN    1
60 #endif
61
62
63 #define DPM_ALLOWED                             1
64 #define DPM_DISALLOWED                          0
65
66 /*---------------------------------------------------------------------------------------
67 |    LOCAL FUNCTION PROTOTYPES:                                                         |
68 ---------------------------------------------------------------------------------------*/
69 /* STATIC INTERNAL FUNCTION */
70 static gint     __mmcamcorder_init_handle(mmf_camcorder_t **hcamcorder, int device_type);
71 static void     __mmcamcorder_deinit_handle(mmf_camcorder_t *hcamcorder);
72 static gint     __mmcamcorder_init_configure_video_capture(mmf_camcorder_t *hcamcorder);
73 static gint     __mmcamcorder_init_configure_audio(mmf_camcorder_t *hcamcorder);
74 static gint     __mmcamcorder_init_configure_common(mmf_camcorder_t *hcamcorder);
75 static void     __mmcamcorder_deinit_configure(mmf_camcorder_t *hcamcorder);
76 static gboolean __mmcamcorder_init_gstreamer(camera_conf *conf);
77 static void     __mmcamcorder_get_system_info(mmf_camcorder_t *hcamcorder);
78
79 static void     __mmcamcorder_handle_gst_sync_error(mmf_camcorder_t *hcamcorder, GstMessage *message);
80 static void     __mmcamcorder_gst_handle_sync_audio_error(mmf_camcorder_t *hcamcorder, gint err_code);
81 static void     __mmcamcorder_gst_handle_sync_others_error(mmf_camcorder_t *hcamcorder, gint err_code);
82 static gboolean __mmcamcorder_handle_gst_error(MMHandleType handle, GstMessage *message, GError *error);
83 static gint     __mmcamcorder_gst_handle_stream_error(MMHandleType handle, int code, GstMessage *message);
84 static gint     __mmcamcorder_gst_handle_resource_error(MMHandleType handle, int code, GstMessage *message);
85 static gint     __mmcamcorder_gst_handle_library_error(MMHandleType handle, int code, GstMessage *message);
86 static gint     __mmcamcorder_gst_handle_core_error(MMHandleType handle, int code, GstMessage *message);
87 static gint     __mmcamcorder_gst_handle_resource_warning(MMHandleType handle, GstMessage *message , GError *error);
88 static gboolean __mmcamcorder_handle_gst_warning(MMHandleType handle, GstMessage *message, GError *error);
89 #ifdef _MMCAMCORDER_MM_RM_SUPPORT
90 static int      __mmcamcorder_resource_release_cb(mm_resource_manager_h rm,
91         mm_resource_manager_res_h res, void *user_data);
92 #endif /* _MMCAMCORDER_MM_RM_SUPPORT */
93
94 /*=======================================================================================
95 |  FUNCTION DEFINITIONS                                                                 |
96 =======================================================================================*/
97 static gint __mmcamcorder_init_handle(mmf_camcorder_t **hcamcorder, int device_type)
98 {
99         int ret = MM_ERROR_NONE;
100         int log_level = MM_CAMCORDER_LOG_LEVEL_INFO;
101         mmf_camcorder_t *new_handle = NULL;
102
103         if (!hcamcorder) {
104                 MMCAM_LOG_ERROR("NULL handle");
105                 return MM_ERROR_CAMCORDER_INVALID_ARGUMENT;
106         }
107
108         /* Create mmf_camcorder_t handle and initialize every variable */
109         new_handle = (mmf_camcorder_t *)malloc(sizeof(mmf_camcorder_t));
110         if (!new_handle) {
111                 MMCAM_LOG_ERROR("new handle allocation failed");
112                 return MM_ERROR_CAMCORDER_LOW_MEMORY;
113         }
114
115         memset(new_handle, 0x00, sizeof(mmf_camcorder_t));
116
117         /* set device type */
118         new_handle->device_type = device_type;
119
120         MMCAM_LOG_WARNING("Device Type : %d", new_handle->device_type);
121
122         new_handle->type = MM_CAMCORDER_MODE_VIDEO_CAPTURE;
123         new_handle->state = MM_CAMCORDER_STATE_NONE;
124         new_handle->old_state = MM_CAMCORDER_STATE_NONE;
125         new_handle->capture_in_recording = FALSE;
126
127         /* init mutex and cond */
128         g_mutex_init(&(new_handle->mtsafe).lock);
129         g_cond_init(&(new_handle->mtsafe).cond);
130         g_mutex_init(&(new_handle->mtsafe).cmd_lock);
131         g_cond_init(&(new_handle->mtsafe).cmd_cond);
132         g_mutex_init(&(new_handle->mtsafe).interrupt_lock);
133         g_mutex_init(&(new_handle->mtsafe).state_lock);
134         g_mutex_init(&(new_handle->mtsafe).gst_state_lock);
135         g_mutex_init(&(new_handle->mtsafe).gst_encode_state_lock);
136         g_mutex_init(&(new_handle->mtsafe).message_cb_lock);
137         g_mutex_init(&(new_handle->mtsafe).vcapture_cb_lock);
138         g_mutex_init(&(new_handle->mtsafe).vstream_cb_lock);
139         g_mutex_init(&(new_handle->mtsafe).astream_cb_lock);
140         g_mutex_init(&(new_handle->mtsafe).mstream_cb_lock);
141         g_mutex_init(&(new_handle->mtsafe).vedecision_cb_lock);
142 #ifdef _MMCAMCORDER_MM_RM_SUPPORT
143         g_mutex_init(&(new_handle->mtsafe).resource_lock);
144 #endif /* _MMCAMCORDER_MM_RM_SUPPORT */
145
146         g_mutex_init(&new_handle->restart_preview_lock);
147
148         g_mutex_init(&new_handle->snd_info.open_mutex);
149         g_cond_init(&new_handle->snd_info.open_cond);
150         g_mutex_init(&new_handle->snd_info.play_mutex);
151         g_cond_init(&new_handle->snd_info.play_cond);
152
153         g_mutex_init(&new_handle->task_thread_lock);
154         g_cond_init(&new_handle->task_thread_cond);
155         new_handle->task_thread_state = _MMCAMCORDER_TASK_THREAD_STATE_NONE;
156
157         if (device_type != MM_VIDEO_DEVICE_NONE) {
158                 new_handle->gdbus_info_sound.mm_handle = new_handle;
159                 g_mutex_init(&new_handle->gdbus_info_sound.sync_mutex);
160                 g_cond_init(&new_handle->gdbus_info_sound.sync_cond);
161
162                 new_handle->gdbus_info_solo_sound.mm_handle = new_handle;
163                 g_mutex_init(&new_handle->gdbus_info_solo_sound.sync_mutex);
164                 g_cond_init(&new_handle->gdbus_info_solo_sound.sync_cond);
165         }
166
167         /* create task thread */
168         new_handle->task_thread = g_thread_try_new("MMCAM_TASK_THREAD",
169                 (GThreadFunc)_mmcamcorder_util_task_thread_func, (gpointer)new_handle, NULL);
170         if (new_handle->task_thread == NULL) {
171                 MMCAM_LOG_ERROR("_mmcamcorder_create::failed to create task thread");
172                 ret = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
173                 goto _INIT_HANDLE_FAILED;
174         }
175
176         /* Get Camera Configure information from Camcorder INI file */
177         ret = _mmcamcorder_conf_get_info((MMHandleType)new_handle, CONFIGURE_TYPE_MAIN, CONFIGURE_MAIN_FILE, &new_handle->conf_main);
178         if (ret != MM_ERROR_NONE) {
179                 MMCAM_LOG_ERROR("Failed to get configure(main) info.");
180                 goto _INIT_HANDLE_FAILED;
181         }
182
183         /* Get log level from conf */
184         _mmcamcorder_conf_get_value_int((MMHandleType)new_handle, new_handle->conf_main,
185                 CONFIGURE_CATEGORY_MAIN_GENERAL,
186                 "LogLevel",
187                 &log_level);
188
189         _mmcamcorder_set_log_level(log_level);
190
191         /* allocate attribute */
192         new_handle->attributes = _mmcamcorder_alloc_attribute((MMHandleType)new_handle);
193         if (!new_handle->attributes) {
194                 ret = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
195                 goto _INIT_HANDLE_FAILED;
196         }
197
198         *hcamcorder = new_handle;
199
200         MMCAM_LOG_INFO("new handle %p", new_handle);
201
202         return MM_ERROR_NONE;
203
204 _INIT_HANDLE_FAILED:
205         __mmcamcorder_deinit_handle(new_handle);
206         return ret;
207 }
208
209
210 static void __mmcamcorder_deinit_handle(mmf_camcorder_t *hcamcorder)
211 {
212         if (!hcamcorder) {
213                 MMCAM_LOG_ERROR("NULL handle");
214                 return;
215         }
216
217         /* Remove exif info */
218         if (hcamcorder->exif_info) {
219                 mm_exif_destory_exif_info(hcamcorder->exif_info);
220                 hcamcorder->exif_info = NULL;
221         }
222
223         /* remove attributes */
224         if (hcamcorder->attributes) {
225                 _mmcamcorder_dealloc_attribute((MMHandleType)hcamcorder, hcamcorder->attributes);
226                 hcamcorder->attributes = 0;
227         }
228
229         /* Release configure info */
230         __mmcamcorder_deinit_configure(hcamcorder);
231
232         /* remove task thread */
233         if (hcamcorder->task_thread) {
234                 g_mutex_lock(&hcamcorder->task_thread_lock);
235                 MMCAM_LOG_INFO("send signal for task thread exit");
236                 hcamcorder->task_thread_state = _MMCAMCORDER_TASK_THREAD_STATE_EXIT;
237                 g_cond_signal(&hcamcorder->task_thread_cond);
238                 g_mutex_unlock(&hcamcorder->task_thread_lock);
239                 g_thread_join(hcamcorder->task_thread);
240                 hcamcorder->task_thread = NULL;
241         }
242
243         /* Release lock, cond */
244         g_mutex_clear(&(hcamcorder->mtsafe).lock);
245         g_cond_clear(&(hcamcorder->mtsafe).cond);
246         g_mutex_clear(&(hcamcorder->mtsafe).cmd_lock);
247         g_cond_clear(&(hcamcorder->mtsafe).cmd_cond);
248         g_mutex_clear(&(hcamcorder->mtsafe).interrupt_lock);
249         g_mutex_clear(&(hcamcorder->mtsafe).state_lock);
250         g_mutex_clear(&(hcamcorder->mtsafe).gst_state_lock);
251         g_mutex_clear(&(hcamcorder->mtsafe).gst_encode_state_lock);
252         g_mutex_clear(&(hcamcorder->mtsafe).message_cb_lock);
253         g_mutex_clear(&(hcamcorder->mtsafe).vcapture_cb_lock);
254         g_mutex_clear(&(hcamcorder->mtsafe).vstream_cb_lock);
255         g_mutex_clear(&(hcamcorder->mtsafe).astream_cb_lock);
256         g_mutex_clear(&(hcamcorder->mtsafe).mstream_cb_lock);
257         g_mutex_clear(&(hcamcorder->mtsafe).vedecision_cb_lock);
258 #ifdef _MMCAMCORDER_MM_RM_SUPPORT
259         g_mutex_clear(&(hcamcorder->mtsafe).resource_lock);
260 #endif /* _MMCAMCORDER_MM_RM_SUPPORT */
261
262         g_mutex_clear(&hcamcorder->snd_info.open_mutex);
263         g_cond_clear(&hcamcorder->snd_info.open_cond);
264         g_mutex_clear(&hcamcorder->restart_preview_lock);
265
266         if (hcamcorder->device_type != MM_VIDEO_DEVICE_NONE) {
267                 g_mutex_clear(&hcamcorder->gdbus_info_sound.sync_mutex);
268                 g_cond_clear(&hcamcorder->gdbus_info_sound.sync_cond);
269                 g_mutex_clear(&hcamcorder->gdbus_info_solo_sound.sync_mutex);
270                 g_cond_clear(&hcamcorder->gdbus_info_solo_sound.sync_cond);
271         }
272
273         g_mutex_clear(&hcamcorder->task_thread_lock);
274         g_cond_clear(&hcamcorder->task_thread_cond);
275
276         if (hcamcorder->model_name)
277                 free(hcamcorder->model_name);
278
279         if (hcamcorder->software_version)
280                 free(hcamcorder->software_version);
281
282         /* Release handle */
283         memset(hcamcorder, 0x00, sizeof(mmf_camcorder_t));
284
285         free(hcamcorder);
286
287         return;
288 }
289
290
291 static void __mmcamcorder_get_system_info(mmf_camcorder_t *hcamcorder)
292 {
293         int ret = 0;
294
295         if (!hcamcorder) {
296                 MMCAM_LOG_ERROR("NULL handle");
297                 return;
298         }
299
300         /* get shutter sound policy */
301         vconf_get_int(VCONFKEY_CAMERA_SHUTTER_SOUND_POLICY, &hcamcorder->shutter_sound_policy);
302         MMCAM_LOG_INFO("current shutter sound policy : %d", hcamcorder->shutter_sound_policy);
303
304         /* get model name */
305         ret = system_info_get_platform_string("http://tizen.org/system/model_name", &hcamcorder->model_name);
306
307         MMCAM_LOG_WARNING("model name [%s], ret 0x%x",
308                 hcamcorder->model_name ? hcamcorder->model_name : "NULL", ret);
309
310         /* get software version */
311         ret = system_info_get_platform_string("http://tizen.org/system/build.string", &hcamcorder->software_version);
312
313         MMCAM_LOG_WARNING("software version [%s], ret 0x%x",
314                 hcamcorder->software_version ? hcamcorder->software_version : "NULL", ret);
315
316         return;
317 }
318
319
320 static gint __mmcamcorder_init_configure_video_capture(mmf_camcorder_t *hcamcorder)
321 {
322         int ret = MM_ERROR_NONE;
323         int resolution_width = 0;
324         int resolution_height = 0;
325         int rcmd_fmt_capture = MM_PIXEL_FORMAT_YUYV;
326         int rcmd_fmt_recording = MM_PIXEL_FORMAT_NV12;
327         int rcmd_dpy_rotation = MM_DISPLAY_ROTATION_270;
328         int play_capture_sound = TRUE;
329         int camera_default_flip = MM_FLIP_NONE;
330         int camera_facing_direction = MM_CAMCORDER_CAMERA_FACING_DIRECTION_REAR;
331         char *err_attr_name = NULL;
332         char conf_file_name[__MMCAMCORDER_CONF_FILENAME_LENGTH] = {'\0',};
333         const char *platform_privilege_camera = NULL;
334         MMCamAttrsInfo fps_info;
335 #ifdef _MMCAMCORDER_CAMERA_CONF_MGR_SUPPORT
336         camera_conf_error_e ret_conf = CAMERA_CONF_MGR_ERROR_NONE;
337         char devicetype[__MMCAMCORDER_CONF_FILENAME_LENGTH] = {'\0',};
338 #endif
339
340         if (!hcamcorder) {
341                 MMCAM_LOG_ERROR("NULL handle");
342                 return MM_ERROR_CAMCORDER_NOT_INITIALIZED;
343         }
344
345         if (hcamcorder->is_network) {
346                 snprintf(conf_file_name, sizeof(conf_file_name), "%s_network.ini",
347                         CONFIGURE_CTRL_FILE_PREFIX);
348         } else {
349 #ifdef _MMCAMCORDER_CAMERA_CONF_MGR_SUPPORT
350                 snprintf(devicetype, sizeof(devicetype), "%d", hcamcorder->device_type);
351                 memset(&hcamcorder->conf_device_info, 0x00, sizeof(camera_conf_device_info_s));
352                 ret_conf = camera_conf_mgr_get_ids(devicetype, __MMCAMCORDER_DEVICE_TYPE_STRING_LEN,
353                         &hcamcorder->conf_device_info);
354                 if (ret_conf !=  CAMERA_CONF_MGR_ERROR_NONE) {
355                         MMCAM_LOG_ERROR("Device [%s] is not found", devicetype);
356                         return MM_ERROR_CAMCORDER_NOT_SUPPORTED;
357                 }
358
359                 snprintf(conf_file_name, sizeof(conf_file_name), "%s_%s_%s.ini",
360                         CONFIGURE_CTRL_FILE_PREFIX, hcamcorder->conf_device_info.vendor_id,
361                         hcamcorder->conf_device_info.product_id);
362
363                 MMCAM_LOG_INFO("device info : node[%s]",
364                         hcamcorder->conf_device_info.node);
365 #else
366                 snprintf(conf_file_name, sizeof(conf_file_name), "%s%d.ini",
367                         CONFIGURE_CTRL_FILE_PREFIX, hcamcorder->device_type);
368 #endif
369         }
370
371         MMCAM_LOG_INFO("Load control configure file [%d][%s]",
372                 hcamcorder->device_type, conf_file_name);
373
374         ret = _mmcamcorder_conf_get_info((MMHandleType)hcamcorder,
375                 CONFIGURE_TYPE_CTRL, (const char *)conf_file_name, &hcamcorder->conf_ctrl);
376         if (ret != MM_ERROR_NONE) {
377 #ifdef _MMCAMCORDER_CAMERA_CONF_MGR_SUPPORT
378                 snprintf(conf_file_name, sizeof(conf_file_name), "%s.ini",
379                         CONFIGURE_CTRL_FILE_PREFIX);
380                 ret = _mmcamcorder_conf_get_info((MMHandleType)hcamcorder,
381                         CONFIGURE_TYPE_CTRL, (const char *)conf_file_name, &hcamcorder->conf_ctrl);
382                 if (ret != MM_ERROR_NONE) {
383                         MMCAM_LOG_ERROR("Failed to get configure(control) info.");
384                         return ret;
385                 }
386 #else
387                 MMCAM_LOG_ERROR("Failed to get configure(control) info.");
388                 return ret;
389 #endif
390         }
391 /*
392         _mmcamcorder_conf_print_info(&hcamcorder->conf_main);
393         _mmcamcorder_conf_print_info(&hcamcorder->conf_ctrl);
394 */
395         ret = _mmcamcorder_init_convert_table((MMHandleType)hcamcorder);
396         if (ret != MM_ERROR_NONE) {
397                 MMCAM_LOG_WARNING("converting table initialize error!!");
398                 return MM_ERROR_CAMCORDER_INTERNAL;
399         }
400
401         ret = _mmcamcorder_init_attr_from_configure((MMHandleType)hcamcorder, MM_CAMCONVERT_CATEGORY_ALL);
402         if (ret != MM_ERROR_NONE) {
403                 MMCAM_LOG_WARNING("attribute initialize from configure error!!");
404                 return MM_ERROR_CAMCORDER_INTERNAL;
405         }
406
407         /* Get device information for supported features from INI */
408         _mmcamcorder_conf_get_value_int((MMHandleType)hcamcorder, hcamcorder->conf_ctrl,
409                 CONFIGURE_CATEGORY_CTRL_CAMERA,
410                 "RecommendPreviewFormatCapture",
411                 &rcmd_fmt_capture);
412
413         _mmcamcorder_conf_get_value_int((MMHandleType)hcamcorder, hcamcorder->conf_ctrl,
414                 CONFIGURE_CATEGORY_CTRL_CAMERA,
415                 "RecommendPreviewFormatRecord",
416                 &rcmd_fmt_recording);
417
418         _mmcamcorder_conf_get_value_int((MMHandleType)hcamcorder, hcamcorder->conf_ctrl,
419                 CONFIGURE_CATEGORY_CTRL_CAMERA,
420                 "RecommendDisplayRotation",
421                 &rcmd_dpy_rotation);
422
423         _mmcamcorder_conf_get_value_int((MMHandleType)hcamcorder, hcamcorder->conf_ctrl,
424                 CONFIGURE_CATEGORY_CTRL_CAMERA,
425                 "SupportExtraPreview",
426                 &hcamcorder->support_extra_preview);
427
428         _mmcamcorder_conf_get_value_int((MMHandleType)hcamcorder, hcamcorder->conf_ctrl,
429                 CONFIGURE_CATEGORY_CTRL_CAMERA,
430                 "FacingDirection",
431                 &camera_facing_direction);
432
433         _mmcamcorder_conf_get_value_int((MMHandleType)hcamcorder, hcamcorder->conf_ctrl,
434                 CONFIGURE_CATEGORY_CTRL_EFFECT,
435                 "BrightnessStepDenominator",
436                 &hcamcorder->brightness_step_denominator);
437
438         _mmcamcorder_conf_get_value_int((MMHandleType)hcamcorder, hcamcorder->conf_ctrl,
439                 CONFIGURE_CATEGORY_CTRL_CAPTURE,
440                 "SupportZSL",
441                 &hcamcorder->support_zsl_capture);
442
443         MMCAM_LOG_INFO("Recommend format[capture:%d, recording:%d]", rcmd_fmt_capture, rcmd_fmt_recording);
444         MMCAM_LOG_INFO("Recommend display rotation[%d]", rcmd_dpy_rotation);
445         MMCAM_LOG_INFO("Camera facing direction[%d]", camera_facing_direction);
446         MMCAM_LOG_INFO("Brightness step denominator[%d]", hcamcorder->brightness_step_denominator);
447         MMCAM_LOG_INFO("Support ZSL capture[%d]", hcamcorder->support_zsl_capture);
448
449         _mmcamcorder_conf_get_value_int((MMHandleType)hcamcorder, hcamcorder->conf_main,
450                 CONFIGURE_CATEGORY_MAIN_VIDEO_INPUT,
451                 "UseZeroCopyFormat",
452                 &hcamcorder->use_zero_copy_format);
453
454         _mmcamcorder_conf_get_value_int((MMHandleType)hcamcorder, hcamcorder->conf_main,
455                 CONFIGURE_CATEGORY_MAIN_VIDEO_INPUT,
456                 "SupportUserBuffer",
457                 &hcamcorder->support_user_buffer);
458
459         _mmcamcorder_conf_get_value_int((MMHandleType)hcamcorder, hcamcorder->conf_main,
460                 CONFIGURE_CATEGORY_MAIN_VIDEO_INPUT,
461                 "SupportMediaPacketPreviewCb",
462                 &hcamcorder->support_media_packet_preview_cb);
463
464         _mmcamcorder_conf_get_value_int((MMHandleType)hcamcorder, hcamcorder->conf_main,
465                 CONFIGURE_CATEGORY_MAIN_VIDEO_OUTPUT,
466                 "UseVideoconvert",
467                 &hcamcorder->use_videoconvert);
468
469         _mmcamcorder_conf_get_value_int((MMHandleType)hcamcorder, hcamcorder->conf_main,
470                 CONFIGURE_CATEGORY_MAIN_VIDEO_INPUT,
471                 "MeasurePreviewFPS",
472                 &hcamcorder->measure_preview_fps);
473
474         _mmcamcorder_conf_get_value_int((MMHandleType)hcamcorder, hcamcorder->conf_main,
475                 CONFIGURE_CATEGORY_MAIN_VIDEO_INPUT,
476                 "DefaultEncodedPreviewBitrate",
477                 &hcamcorder->default_encoded_preview_bitrate);
478
479         _mmcamcorder_conf_get_value_int((MMHandleType)hcamcorder, hcamcorder->conf_main,
480                 CONFIGURE_CATEGORY_MAIN_CAPTURE,
481                 "CaptureMode",
482                 &hcamcorder->capture_mode);
483
484         _mmcamcorder_conf_get_value_int((MMHandleType)hcamcorder, hcamcorder->conf_main,
485                 CONFIGURE_CATEGORY_MAIN_CAPTURE,
486                 "PlayCaptureSound",
487                 &play_capture_sound);
488
489         MMCAM_LOG_INFO("Support user buffer[%d]", hcamcorder->support_user_buffer);
490         MMCAM_LOG_INFO("Support media packet preview cb[%d]", hcamcorder->support_media_packet_preview_cb);
491         MMCAM_LOG_INFO("Support extra preview[%d]", hcamcorder->support_extra_preview);
492         MMCAM_LOG_INFO("Capture mode[%d]", hcamcorder->capture_mode);
493         MMCAM_LOG_INFO("Use zero copy format[%d]", hcamcorder->use_zero_copy_format);
494         MMCAM_LOG_INFO("Use video convert[%d]", hcamcorder->use_videoconvert);
495         MMCAM_LOG_INFO("Measure preview FPS[%d]", hcamcorder->measure_preview_fps);
496         MMCAM_LOG_INFO("Default encoded preview bitrate[%d]", hcamcorder->default_encoded_preview_bitrate);
497         MMCAM_LOG_INFO("Play capture sound[%d]", play_capture_sound);
498
499         ret = mm_camcorder_get_attributes((MMHandleType)hcamcorder, NULL,
500                 MMCAM_CAMERA_WIDTH, &resolution_width,
501                 MMCAM_CAMERA_HEIGHT, &resolution_height,
502                 NULL);
503         if (ret != MM_ERROR_NONE) {
504                 MMCAM_LOG_ERROR("get attribute[resolution] error");
505                 return MM_ERROR_CAMCORDER_INTERNAL;
506         }
507
508         ret = mm_camcorder_get_fps_list_by_resolution((MMHandleType)hcamcorder, resolution_width, resolution_height, &fps_info);
509         if (ret != MM_ERROR_NONE) {
510                 MMCAM_LOG_ERROR("get fps list failed with [%dx%d]", resolution_width, resolution_height);
511                 return MM_ERROR_CAMCORDER_INTERNAL;
512         }
513
514         MMCAM_LOG_INFO("res : %d X %d, Default FPS by resolution : %d",
515                 resolution_width, resolution_height, fps_info.int_array.def);
516
517         if (camera_facing_direction == 1) {
518                 if (rcmd_dpy_rotation == MM_DISPLAY_ROTATION_270 || rcmd_dpy_rotation == MM_DISPLAY_ROTATION_90)
519                         camera_default_flip = MM_FLIP_VERTICAL;
520                 else
521                         camera_default_flip = MM_FLIP_HORIZONTAL;
522
523                 MMCAM_LOG_INFO("camera_default_flip : [%d]", camera_default_flip);
524         }
525
526         _mmcamcorder_conf_get_value_string((MMHandleType)hcamcorder, hcamcorder->conf_main,
527                 CONFIGURE_CATEGORY_MAIN_GENERAL,
528                 "PlatformPrivilegeCamera",
529                 &platform_privilege_camera);
530
531         if (hcamcorder->is_network) {
532                 _mmcamcorder_conf_get_value_string((MMHandleType)hcamcorder, hcamcorder->conf_main,
533                         CONFIGURE_CATEGORY_MAIN_GENERAL,
534                         "NetworkCameraHALName",
535                         &hcamcorder->network_hal_name);
536
537                 MMCAM_LOG_INFO("network HAL name[%s]", hcamcorder->network_hal_name);
538         }
539
540         ret = mm_camcorder_set_attributes((MMHandleType)hcamcorder, &err_attr_name,
541                 MMCAM_CAMERA_FACING_DIRECTION, camera_facing_direction,
542                 MMCAM_RECOMMEND_PREVIEW_FORMAT_FOR_CAPTURE, rcmd_fmt_capture,
543                 MMCAM_RECOMMEND_PREVIEW_FORMAT_FOR_RECORDING, rcmd_fmt_recording,
544                 MMCAM_RECOMMEND_DISPLAY_ROTATION, rcmd_dpy_rotation,
545                 MMCAM_SUPPORT_ZSL_CAPTURE, hcamcorder->support_zsl_capture,
546                 MMCAM_SUPPORT_ZERO_COPY_FORMAT, hcamcorder->use_zero_copy_format,
547                 MMCAM_SUPPORT_MEDIA_PACKET_PREVIEW_CB, hcamcorder->support_media_packet_preview_cb,
548                 MMCAM_SUPPORT_USER_BUFFER, hcamcorder->support_user_buffer,
549                 MMCAM_SUPPORT_EXTRA_PREVIEW, hcamcorder->support_extra_preview,
550                 MMCAM_CAMERA_FPS, fps_info.int_array.def,
551                 MMCAM_DISPLAY_FLIP, camera_default_flip,
552                 MMCAM_CAPTURE_SOUND_ENABLE, play_capture_sound,
553                 MMCAM_PLATFORM_PRIVILEGE_CAMERA, platform_privilege_camera,
554                         platform_privilege_camera ? strlen(platform_privilege_camera) : 0,
555                 MMCAM_ENCODED_PREVIEW_BITRATE, hcamcorder->default_encoded_preview_bitrate,
556                 NULL);
557         if (ret != MM_ERROR_NONE) {
558                 MMCAM_LOG_ERROR("[0x%x] Set %s FAILED.", ret, err_attr_name ? err_attr_name : "[UNKNOWN]");
559                 SAFE_FREE(err_attr_name);
560                 return MM_ERROR_CAMCORDER_INTERNAL;
561         }
562
563         /* Get default value of brightness */
564         ret = mm_camcorder_get_attributes((MMHandleType)hcamcorder, NULL,
565                 MMCAM_FILTER_BRIGHTNESS, &hcamcorder->brightness_default,
566                 NULL);
567         if (ret != MM_ERROR_NONE) {
568                 MMCAM_LOG_ERROR("Get brightness FAILED.");
569                 return MM_ERROR_CAMCORDER_INTERNAL;
570         }
571
572         MMCAM_LOG_INFO("Default brightness : %d", hcamcorder->brightness_default);
573
574         return ret;
575 }
576
577
578 static gint __mmcamcorder_init_configure_audio(mmf_camcorder_t *hcamcorder)
579 {
580         int ret = MM_ERROR_NONE;
581
582         if (!hcamcorder) {
583                 MMCAM_LOG_ERROR("NULL handle");
584                 return MM_ERROR_CAMCORDER_NOT_INITIALIZED;
585         }
586
587         ret = _mmcamcorder_init_attr_from_configure((MMHandleType)hcamcorder, MM_CAMCONVERT_CATEGORY_AUDIO);
588         if (ret != MM_ERROR_NONE) {
589                 MMCAM_LOG_ERROR("there is no audio device");
590                 return MM_ERROR_CAMCORDER_NOT_SUPPORTED;
591         }
592
593         return ret;
594 }
595
596
597 static gint __mmcamcorder_init_configure_common(mmf_camcorder_t *hcamcorder)
598 {
599         int ret = MM_ERROR_NONE;
600         int device_count = 0;
601         const char *videosrc_name = NULL;
602         const char *audiosrc_name = NULL;
603         type_element *SrcElement = NULL;
604
605         if (!hcamcorder) {
606                 MMCAM_LOG_ERROR("NULL handle");
607                 return MM_ERROR_CAMCORDER_NOT_INITIALIZED;
608         }
609
610         /* Device count */
611         _mmcamcorder_conf_get_value_int((MMHandleType)hcamcorder, hcamcorder->conf_main,
612                 CONFIGURE_CATEGORY_MAIN_VIDEO_INPUT,
613                 "DeviceCount",
614                 &device_count);
615
616         /* Videosrc element name */
617         _mmcamcorder_conf_get_element((MMHandleType)hcamcorder, hcamcorder->conf_main,
618                 CONFIGURE_CATEGORY_MAIN_VIDEO_INPUT,
619                 "VideosrcElement",
620                 &SrcElement);
621         _mmcamcorder_conf_get_value_element_name(SrcElement, &videosrc_name);
622
623         /* Audiosrc element name */
624         _mmcamcorder_conf_get_element((MMHandleType)hcamcorder, hcamcorder->conf_main,
625                 CONFIGURE_CATEGORY_MAIN_AUDIO_INPUT,
626                 "AudiosrcElement",
627                 &SrcElement);
628         _mmcamcorder_conf_get_value_element_name(SrcElement, &audiosrc_name);
629
630         MMCAM_LOG_INFO("Device count[%d], Videosrc[%s], Audiosrc[%s]",
631                 device_count, videosrc_name, audiosrc_name);
632
633         ret = mm_camcorder_set_attributes((MMHandleType)hcamcorder, NULL,
634                 MMCAM_CAMERA_DEVICE_COUNT, device_count,
635                 MMCAM_VIDEOSRC_ELEMENT_NAME, videosrc_name, videosrc_name ? strlen(videosrc_name) : 0,
636                 MMCAM_AUDIOSRC_ELEMENT_NAME, audiosrc_name, audiosrc_name ? strlen(audiosrc_name) : 0,
637                 NULL);
638         if (ret != MM_ERROR_NONE)
639                 MMCAM_LOG_ERROR("Set FAILED[0x%x]", ret);
640
641         return ret;
642 }
643
644
645 static void __mmcamcorder_deinit_configure(mmf_camcorder_t *hcamcorder)
646 {
647         if (!hcamcorder) {
648                 MMCAM_LOG_ERROR("NULL handle");
649                 return;
650         }
651
652         if (hcamcorder->conf_ctrl)
653                 _mmcamcorder_conf_release_info((MMHandleType)hcamcorder, &hcamcorder->conf_ctrl);
654
655         if (hcamcorder->conf_main)
656                 _mmcamcorder_conf_release_info((MMHandleType)hcamcorder, &hcamcorder->conf_main);
657
658         return;
659 }
660
661
662 /*---------------------------------------------------------------------------------------
663 |    GLOBAL FUNCTION DEFINITIONS:                                                       |
664 ---------------------------------------------------------------------------------------*/
665 /* Internal command functions {*/
666 int _mmcamcorder_create(MMHandleType *handle, MMCamPreset *info)
667 {
668         int ret = MM_ERROR_NONE;
669         mmf_camcorder_t *hcamcorder = NULL;
670
671         MMCAM_LOG_INFO("Entered");
672
673         mmf_return_val_if_fail(handle && info, MM_ERROR_CAMCORDER_INVALID_ARGUMENT);
674
675         /* check device type */
676         if (info->videodev_type < MM_VIDEO_DEVICE_NONE ||
677             info->videodev_type >= MM_VIDEO_DEVICE_NUM) {
678                 MMCAM_LOG_ERROR("video device type[%d] is out of range.", info->videodev_type);
679                 return MM_ERROR_CAMCORDER_INVALID_ARGUMENT;
680         }
681
682         /* init handle */
683         ret = __mmcamcorder_init_handle(&hcamcorder, info->videodev_type);
684         if (ret != MM_ERROR_NONE)
685                 return ret;
686
687         /* set network camera flag */
688         hcamcorder->is_network = (gboolean)info->reserved[0];
689
690         /* get DPM handle for camera/microphone restriction */
691         hcamcorder->dpm_handle = dpm_manager_create();
692
693         MMCAM_LOG_WARNING("DPM handle %p", hcamcorder->dpm_handle);
694
695         ret = __mmcamcorder_init_configure_common(hcamcorder);
696         if (ret != MM_ERROR_NONE)
697                 goto _ERR_DEFAULT_VALUE_INIT;
698
699         if (hcamcorder->device_type != MM_VIDEO_DEVICE_NONE) {
700                 ret = __mmcamcorder_init_configure_video_capture(hcamcorder);
701                 if (ret != MM_ERROR_NONE)
702                         goto _ERR_DEFAULT_VALUE_INIT;
703
704                 /* add DPM camera policy changed callback */
705                 if (hcamcorder->dpm_handle) {
706                         ret = dpm_add_policy_changed_cb(hcamcorder->dpm_handle, "camera",
707                                 _mmcamcorder_dpm_camera_policy_changed_cb, (void *)hcamcorder, &hcamcorder->dpm_camera_cb_id);
708                         if (ret != DPM_ERROR_NONE) {
709                                 MMCAM_LOG_ERROR("add DPM changed cb failed, keep going...");
710                                 hcamcorder->dpm_camera_cb_id = 0;
711                         }
712
713                         MMCAM_LOG_INFO("DPM camera changed cb id %d", hcamcorder->dpm_camera_cb_id);
714                 }
715
716 #ifdef _MMCAMCORDER_MM_RM_SUPPORT
717                 /* initialize resource manager */
718                 ret = mm_resource_manager_create(MM_RESOURCE_MANAGER_APP_CLASS_MEDIA,
719                                 __mmcamcorder_resource_release_cb, hcamcorder,
720                                 &hcamcorder->resource_manager);
721                 if (ret != MM_RESOURCE_MANAGER_ERROR_NONE) {
722                         MMCAM_LOG_ERROR("failed to initialize resource manager");
723                         ret = MM_ERROR_CAMCORDER_INTERNAL;
724                         goto _ERR_DEFAULT_VALUE_INIT;
725                 }
726 #endif /* _MMCAMCORDER_MM_RM_SUPPORT */
727         } else {
728                 ret = __mmcamcorder_init_configure_audio(hcamcorder);
729                 if (ret != MM_ERROR_NONE)
730                         goto _ERR_DEFAULT_VALUE_INIT;
731         }
732
733         traceBegin(TTRACE_TAG_CAMERA, "MMCAMCORDER:CREATE:INIT_GSTREAMER");
734
735         ret = __mmcamcorder_init_gstreamer(hcamcorder->conf_main);
736
737         traceEnd(TTRACE_TAG_CAMERA);
738
739         if (!ret) {
740                 MMCAM_LOG_ERROR("Failed to initialize gstreamer!!");
741                 ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
742                 goto _ERR_DEFAULT_VALUE_INIT;
743         }
744
745         /* Make some attributes as read-only type */
746         _mmcamcorder_lock_readonly_attributes((MMHandleType)hcamcorder);
747
748         /* get system information */
749         __mmcamcorder_get_system_info(hcamcorder);
750
751         /* Set initial state */
752         _mmcamcorder_set_state((MMHandleType)hcamcorder, MM_CAMCORDER_STATE_NULL);
753
754         MMCAM_LOG_INFO("created handle %p", hcamcorder);
755
756         *handle = (MMHandleType)hcamcorder;
757
758         return MM_ERROR_NONE;
759
760 _ERR_DEFAULT_VALUE_INIT:
761 #ifdef _MMCAMCORDER_MM_RM_SUPPORT
762         /* de-initialize resource manager */
763         if (hcamcorder->resource_manager != NULL)
764                 mm_resource_manager_destroy(hcamcorder->resource_manager);
765 #endif /* _MMCAMCORDER_MM_RM_SUPPORT */
766
767         /* release DPM related handle */
768         if (hcamcorder->dpm_handle) {
769                 MMCAM_LOG_INFO("release DPM handle %p, camera changed cb id %d",
770                         hcamcorder->dpm_handle, hcamcorder->dpm_camera_cb_id);
771
772                 /* remove camera policy changed callback */
773                 if (hcamcorder->dpm_camera_cb_id > 0) {
774                         dpm_remove_policy_changed_cb(hcamcorder->dpm_handle, hcamcorder->dpm_camera_cb_id);
775                         hcamcorder->dpm_camera_cb_id = 0;
776                 } else {
777                         MMCAM_LOG_WARNING("invalid dpm camera cb id %d", hcamcorder->dpm_camera_cb_id);
778                 }
779
780                 dpm_manager_destroy(hcamcorder->dpm_handle);
781                 hcamcorder->dpm_handle = NULL;
782         }
783
784         /* release configure info */
785         __mmcamcorder_deinit_configure(hcamcorder);
786
787         /* deinitialize handle */
788         __mmcamcorder_deinit_handle(hcamcorder);
789
790         return ret;
791 }
792
793
794 int _mmcamcorder_destroy(MMHandleType handle)
795 {
796         int ret = MM_ERROR_NONE;
797         int state = MM_CAMCORDER_STATE_NONE;
798         GstElement *sink_element = NULL;
799         int sink_element_size = 0;
800
801         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
802
803         MMCAM_LOG_INFO("");
804
805         if (!hcamcorder) {
806                 MMCAM_LOG_ERROR("Not initialized");
807                 ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
808                 goto _ERR_CAMCORDER_CMD_PRECON;
809         }
810
811         if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
812                 MMCAM_LOG_ERROR("Another command is running.");
813                 ret = MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
814                 goto _ERR_CAMCORDER_CMD_PRECON;
815         }
816
817         state = _mmcamcorder_get_state(handle);
818         if (state != MM_CAMCORDER_STATE_NULL) {
819                 MMCAM_LOG_ERROR("Wrong state(%d)", state);
820                 ret = MM_ERROR_CAMCORDER_INVALID_STATE;
821                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
822         }
823
824         /* remove kept, but not used sink element in attribute */
825         mm_camcorder_get_attributes(handle, NULL,
826                 MMCAM_DISPLAY_REUSE_ELEMENT, &sink_element, &sink_element_size,
827                 NULL);
828         if (sink_element) {
829                 GstStateChangeReturn result = gst_element_set_state(sink_element, GST_STATE_NULL);
830
831                 MMCAM_LOG_WARNING("remove sink element %p, set state NULL result %d", sink_element, result);
832
833                 gst_object_unref(sink_element);
834                 sink_element = NULL;
835         }
836
837         /* wait for completion of sound play */
838         _mmcamcorder_sound_solo_play_wait(handle);
839
840         /* Release SubContext and pipeline */
841         if (hcamcorder->sub_context) {
842                 if (hcamcorder->sub_context->element)
843                         _mmcamcorder_destroy_pipeline(handle, hcamcorder->type);
844
845                 _mmcamcorder_dealloc_subcontext(hcamcorder->sub_context);
846                 hcamcorder->sub_context = NULL;
847         }
848
849 #ifdef _MMCAMCORDER_MM_RM_SUPPORT
850         /* de-initialize resource manager */
851         _MMCAMCORDER_LOCK_RESOURCE(hcamcorder);
852
853         if (hcamcorder->resource_manager != NULL) {
854                 ret = mm_resource_manager_destroy(hcamcorder->resource_manager);
855                 if (ret != MM_RESOURCE_MANAGER_ERROR_NONE)
856                         MMCAM_LOG_ERROR("failed to de-initialize resource manager 0x%x", ret);
857         }
858
859         _MMCAMCORDER_UNLOCK_RESOURCE(hcamcorder);
860 #endif /* _MMCAMCORDER_MM_RM_SUPPORT */
861
862         /* Remove messages which are not called yet */
863         _mmcamcorder_remove_message_all(handle);
864
865 #ifdef _MMCAMCORDER_RM_SUPPORT
866         _mmcamcorder_rm_release(handle);
867 #endif /* _MMCAMCORDER_RM_SUPPORT */
868
869         /* release DPM handle */
870         if (hcamcorder->dpm_handle) {
871                 MMCAM_LOG_INFO("release DPM handle %p, camera changed cb id %d",
872                         hcamcorder->dpm_handle, hcamcorder->dpm_camera_cb_id);
873
874                 /* remove camera policy changed callback */
875                 if (hcamcorder->dpm_camera_cb_id > 0) {
876                         dpm_remove_policy_changed_cb(hcamcorder->dpm_handle, hcamcorder->dpm_camera_cb_id);
877                         hcamcorder->dpm_camera_cb_id = 0;
878                 } else {
879                         MMCAM_LOG_WARNING("invalid dpm camera cb id %d", hcamcorder->dpm_camera_cb_id);
880                 }
881
882                 dpm_manager_destroy(hcamcorder->dpm_handle);
883                 hcamcorder->dpm_handle = NULL;
884         }
885
886         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
887
888         __mmcamcorder_deinit_handle(hcamcorder);
889
890         return MM_ERROR_NONE;
891
892 _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK:
893         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
894
895 _ERR_CAMCORDER_CMD_PRECON:
896         if (hcamcorder)
897                 MMCAM_LOG_ERROR("Destroy fail (type %d, state %d)", hcamcorder->type, state);
898
899         MMCAM_LOG_ERROR("Destroy fail (ret %x)", ret);
900
901         return ret;
902 }
903
904
905 int _mmcamcorder_realize(MMHandleType handle)
906 {
907         int ret = MM_ERROR_NONE;
908         int state = MM_CAMCORDER_STATE_NONE;
909         int display_surface_type = MM_DISPLAY_SURFACE_OVERLAY;
910         double motion_rate = _MMCAMCORDER_DEFAULT_RECORDING_MOTION_RATE;
911         char *videosink_element_type = NULL;
912         const char *videosink_name = NULL;
913         int conn_size = 0;
914
915         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
916
917         /*MMCAM_LOG_INFO("");*/
918
919         if (!hcamcorder) {
920                 MMCAM_LOG_ERROR("Not initialized");
921                 ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
922                 return ret;
923         }
924
925         if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
926                 MMCAM_LOG_ERROR("Another command is running.");
927                 ret = MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
928                 goto _ERR_CAMCORDER_CMD_PRECON;
929         }
930
931         state = _mmcamcorder_get_state(handle);
932         if (state != MM_CAMCORDER_STATE_NULL) {
933                 MMCAM_LOG_ERROR("Wrong state(%d)", state);
934                 ret = MM_ERROR_CAMCORDER_INVALID_STATE;
935                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
936         }
937
938         /* Get profile mode and gdbus connection */
939         mm_camcorder_get_attributes(handle, NULL,
940                 MMCAM_MODE, &hcamcorder->type,
941                 MMCAM_GDBUS_CONNECTION, &hcamcorder->gdbus_conn, &conn_size,
942                 NULL);
943
944         if (!hcamcorder->gdbus_conn) {
945                 MMCAM_LOG_ERROR("gdbus connection NULL");
946                 ret = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
947                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
948         }
949
950         MMCAM_LOG_INFO("Profile mode [%d], gdbus connection [%p]",
951                 hcamcorder->type, hcamcorder->gdbus_conn);
952
953         mm_camcorder_get_attributes(handle, NULL,
954                 MMCAM_DISPLAY_SURFACE, &display_surface_type,
955                 MMCAM_CAMERA_RECORDING_MOTION_RATE, &motion_rate,
956                 NULL);
957
958         /* alloc sub context */
959         hcamcorder->sub_context = _mmcamcorder_alloc_subcontext(hcamcorder->type);
960         if (!hcamcorder->sub_context) {
961                 ret = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
962                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
963         }
964
965         /* Set basic configure information */
966         if (motion_rate != _MMCAMCORDER_DEFAULT_RECORDING_MOTION_RATE)
967                 hcamcorder->sub_context->is_modified_rate = TRUE;
968
969         if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
970                 /* get video recording info */
971                 _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_main,
972                         CONFIGURE_CATEGORY_MAIN_RECORD,
973                         "SupportDualStream",
974                         &(hcamcorder->sub_context->info_video->support_dual_stream));
975                 MMCAM_LOG_WARNING("SupportDualStream [%d]", hcamcorder->sub_context->info_video->support_dual_stream);
976
977                 _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_main,
978                         CONFIGURE_CATEGORY_MAIN_RECORD,
979                         "UseVideoscale",
980                         &(hcamcorder->sub_context->info_video->use_videoscale));
981                 MMCAM_LOG_WARNING("UseVideoscale [%d]", hcamcorder->sub_context->info_video->use_videoscale);
982         }
983
984         switch (display_surface_type) {
985         case MM_DISPLAY_SURFACE_OVERLAY:
986                 videosink_element_type = strdup("VideosinkElementOverlay");
987                 break;
988         case MM_DISPLAY_SURFACE_EVAS:
989                 videosink_element_type = strdup("VideosinkElementEvas");
990                 break;
991         case MM_DISPLAY_SURFACE_GL:
992                 videosink_element_type = strdup("VideosinkElementGL");
993                 break;
994         case MM_DISPLAY_SURFACE_NULL:
995                 videosink_element_type = strdup("VideosinkElementNull");
996                 break;
997         default:
998                 videosink_element_type = strdup("VideosinkElementOverlay");
999                 break;
1000         }
1001
1002         /* check string of videosink element */
1003         if (videosink_element_type) {
1004                 _mmcamcorder_conf_get_element(handle, hcamcorder->conf_main,
1005                         CONFIGURE_CATEGORY_MAIN_VIDEO_OUTPUT,
1006                         videosink_element_type,
1007                         &hcamcorder->sub_context->VideosinkElement);
1008                 free(videosink_element_type);
1009                 videosink_element_type = NULL;
1010         } else {
1011                 MMCAM_LOG_WARNING("strdup failed(display_surface_type %d). Use default Overlay type",
1012                         display_surface_type);
1013
1014                 _mmcamcorder_conf_get_element(handle, hcamcorder->conf_main,
1015                         CONFIGURE_CATEGORY_MAIN_VIDEO_OUTPUT,
1016                         _MMCAMCORDER_DEFAULT_VIDEOSINK_TYPE,
1017                         &hcamcorder->sub_context->VideosinkElement);
1018         }
1019
1020         _mmcamcorder_conf_get_value_element_name(hcamcorder->sub_context->VideosinkElement, &videosink_name);
1021         MMCAM_LOG_INFO("Videosink name : %s", videosink_name);
1022
1023         /* get videoconvert element */
1024         _mmcamcorder_conf_get_element(handle, hcamcorder->conf_main,
1025                 CONFIGURE_CATEGORY_MAIN_VIDEO_OUTPUT,
1026                 "VideoconvertElement",
1027                 &hcamcorder->sub_context->VideoconvertElement);
1028
1029         _mmcamcorder_conf_get_value_int(handle, hcamcorder->conf_ctrl,
1030                 CONFIGURE_CATEGORY_CTRL_CAPTURE,
1031                 "SensorEncodedCapture",
1032                 &(hcamcorder->sub_context->SensorEncodedCapture));
1033         MMCAM_LOG_INFO("Support sensor encoded capture : %d", hcamcorder->sub_context->SensorEncodedCapture);
1034
1035         if (hcamcorder->type == MM_CAMCORDER_MODE_VIDEO_CAPTURE) {
1036                 int dpm_camera_state = DPM_ALLOWED;
1037
1038                 /* check camera policy from DPM */
1039                 if (hcamcorder->dpm_handle) {
1040                         if (dpm_restriction_get_camera_state(hcamcorder->dpm_handle, &dpm_camera_state) == DPM_ERROR_NONE) {
1041                                 MMCAM_LOG_INFO("DPM camera state %d", dpm_camera_state);
1042                                 if (dpm_camera_state == DPM_DISALLOWED) {
1043                                         MMCAM_LOG_ERROR("CAMERA DISALLOWED by DPM");
1044                                         ret = MM_ERROR_POLICY_RESTRICTED;
1045
1046                                         _mmcamcorder_request_dpm_popup(hcamcorder->gdbus_conn, "camera");
1047
1048                                         goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1049                                 }
1050                         } else {
1051                                 MMCAM_LOG_ERROR("get DPM camera state failed, keep going...");
1052                         }
1053                 } else {
1054                         MMCAM_LOG_WARNING("NULL dpm_handle");
1055                 }
1056
1057 #ifdef _MMCAMCORDER_MM_RM_SUPPORT
1058                 _MMCAMCORDER_LOCK_RESOURCE(hcamcorder);
1059                 /* prepare resource manager for camera */
1060                 if (hcamcorder->camera_resource == NULL) {
1061                         ret = mm_resource_manager_mark_for_acquire(hcamcorder->resource_manager,
1062                                         MM_RESOURCE_MANAGER_RES_TYPE_CAMERA,
1063                                         1,
1064                                         &hcamcorder->camera_resource);
1065                         if (ret != MM_RESOURCE_MANAGER_ERROR_NONE) {
1066                                 MMCAM_LOG_ERROR("could not prepare for camera resource");
1067                                 ret = MM_ERROR_RESOURCE_INTERNAL;
1068                                 _MMCAMCORDER_UNLOCK_RESOURCE(hcamcorder);
1069                                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1070                         }
1071                 } else {
1072                         MMCAM_LOG_INFO("camera already acquired");
1073                 }
1074
1075                 /* prepare resource manager for "video_overlay only if display surface is X" */
1076                 if (display_surface_type == MM_DISPLAY_SURFACE_OVERLAY) {
1077                         if (hcamcorder->video_overlay_resource == NULL) {
1078                                 ret = mm_resource_manager_mark_for_acquire(hcamcorder->resource_manager,
1079                                                 MM_RESOURCE_MANAGER_RES_TYPE_VIDEO_OVERLAY,
1080                                                 MM_RESOURCE_MANAGER_RES_VOLUME_FULL,
1081                                                 &hcamcorder->video_overlay_resource);
1082                                 if (ret != MM_RESOURCE_MANAGER_ERROR_NONE) {
1083                                         MMCAM_LOG_ERROR("could not prepare for overlay resource");
1084                                         ret = MM_ERROR_RESOURCE_INTERNAL;
1085                                         _MMCAMCORDER_UNLOCK_RESOURCE(hcamcorder);
1086                                         goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1087                                 }
1088                         } else {
1089                                 MMCAM_LOG_INFO("overlay already acquired");
1090                         }
1091                 }
1092
1093                 /* acquire resources */
1094                 ret = mm_resource_manager_commit(hcamcorder->resource_manager);
1095                 if (ret != MM_RESOURCE_MANAGER_ERROR_NONE) {
1096                         MMCAM_LOG_ERROR("could not acquire resources");
1097                         ret = MM_ERROR_RESOURCE_INTERNAL;
1098                         _MMCAMCORDER_UNLOCK_RESOURCE(hcamcorder);
1099                         goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1100                 }
1101                 _MMCAMCORDER_UNLOCK_RESOURCE(hcamcorder);
1102 #endif /* _MMCAMCORDER_MM_RM_SUPPORT */
1103
1104 #ifdef _MMCAMCORDER_RM_SUPPORT
1105                 ret = _mmcamcorder_rm_create(handle);
1106                 if (ret != MM_ERROR_NONE) {
1107                         MMCAM_LOG_ERROR("Resource create failed");
1108                         goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1109                 }
1110                 ret = _mmcamcorder_rm_allocate(handle);
1111                 if (ret != MM_ERROR_NONE) {
1112                         MMCAM_LOG_ERROR("Resource allocation request failed");
1113                         _mmcamcorder_rm_release(handle);
1114                         goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1115                 }
1116 #endif /* _MMCAMCORDER_RM_SUPPORT */
1117         }
1118
1119         /* create pipeline */
1120         traceBegin(TTRACE_TAG_CAMERA, "MMCAMCORDER:REALIZE:CREATE_PIPELINE");
1121
1122         ret = _mmcamcorder_create_pipeline(handle, hcamcorder->type);
1123
1124         traceEnd(TTRACE_TAG_CAMERA);
1125
1126         if (ret != MM_ERROR_NONE) {
1127                 /* check internal error of gstreamer */
1128                 if (hcamcorder->error_code != MM_ERROR_NONE) {
1129                         ret = hcamcorder->error_code;
1130                         MMCAM_LOG_INFO("gstreamer error is occurred. return it %x", ret);
1131                 }
1132
1133                 /* release sub context */
1134                 _mmcamcorder_dealloc_subcontext(hcamcorder->sub_context);
1135                 hcamcorder->sub_context = NULL;
1136                 goto _ERR_CAMCORDER_CMD;
1137         }
1138
1139         /* set command function */
1140         ret = _mmcamcorder_set_functions(handle, hcamcorder->type);
1141         if (ret != MM_ERROR_NONE) {
1142                 _mmcamcorder_destroy_pipeline(handle, hcamcorder->type);
1143                 _mmcamcorder_dealloc_subcontext(hcamcorder->sub_context);
1144                 hcamcorder->sub_context = NULL;
1145                 goto _ERR_CAMCORDER_CMD;
1146         }
1147
1148         _mmcamcorder_set_state(handle, MM_CAMCORDER_STATE_READY);
1149
1150         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1151
1152         return MM_ERROR_NONE;
1153
1154 _ERR_CAMCORDER_CMD:
1155 #ifdef _MMCAMCORDER_MM_RM_SUPPORT
1156         /* release hw resources */
1157         _MMCAMCORDER_LOCK_RESOURCE(hcamcorder);
1158         if (hcamcorder->type == MM_CAMCORDER_MODE_VIDEO_CAPTURE) {
1159                 if (hcamcorder->camera_resource != NULL) {
1160                         mm_resource_manager_mark_for_release(hcamcorder->resource_manager,
1161                                         hcamcorder->camera_resource);
1162                         hcamcorder->camera_resource = NULL;
1163                 }
1164                 if (hcamcorder->video_overlay_resource != NULL) {
1165                         mm_resource_manager_mark_for_release(hcamcorder->resource_manager,
1166                                         hcamcorder->video_overlay_resource);
1167                         hcamcorder->video_overlay_resource = NULL;
1168                 }
1169                 mm_resource_manager_commit(hcamcorder->resource_manager);
1170         }
1171         _MMCAMCORDER_UNLOCK_RESOURCE(hcamcorder);
1172 #endif /* _MMCAMCORDER_MM_RM_SUPPORT */
1173 #ifdef _MMCAMCORDER_RM_SUPPORT
1174         _mmcamcorder_rm_deallocate(handle);
1175         _mmcamcorder_rm_release(handle);
1176 #endif /* _MMCAMCORDER_RM_SUPPORT*/
1177
1178 _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK:
1179         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1180
1181 _ERR_CAMCORDER_CMD_PRECON:
1182         MMCAM_LOG_ERROR("Realize fail (type %d, state %d, ret %x)", hcamcorder->type, state, ret);
1183
1184         return ret;
1185 }
1186
1187
1188 int _mmcamcorder_unrealize(MMHandleType handle)
1189 {
1190         int ret = MM_ERROR_NONE;
1191         int state = MM_CAMCORDER_STATE_NONE;
1192
1193         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1194
1195         MMCAM_LOG_INFO("");
1196
1197         if (!hcamcorder) {
1198                 MMCAM_LOG_ERROR("Not initialized");
1199                 ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
1200                 return ret;
1201         }
1202
1203         if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
1204                 MMCAM_LOG_ERROR("Another command is running.");
1205                 ret = MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
1206                 goto _ERR_CAMCORDER_CMD_PRECON;
1207         }
1208
1209         state = _mmcamcorder_get_state(handle);
1210         if (state != MM_CAMCORDER_STATE_READY) {
1211                 MMCAM_LOG_ERROR("Wrong state(%d)", state);
1212                 ret = MM_ERROR_CAMCORDER_INVALID_STATE;
1213                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1214         }
1215
1216         /* Release SubContext */
1217         if (hcamcorder->sub_context) {
1218                 /* destroy pipeline */
1219                 _mmcamcorder_destroy_pipeline(handle, hcamcorder->type);
1220                 /* Deallocate SubContext */
1221                 _mmcamcorder_dealloc_subcontext(hcamcorder->sub_context);
1222                 hcamcorder->sub_context = NULL;
1223         }
1224
1225 #ifdef _MMCAMCORDER_MM_RM_SUPPORT
1226         _MMCAMCORDER_LOCK_RESOURCE(hcamcorder);
1227         MMCAM_LOG_WARNING("lock resource - cb calling %d", hcamcorder->is_release_cb_calling);
1228
1229         if (hcamcorder->type == MM_CAMCORDER_MODE_VIDEO_CAPTURE &&
1230                 hcamcorder->state_change_by_system != _MMCAMCORDER_STATE_CHANGE_BY_RM &&
1231                 hcamcorder->is_release_cb_calling == FALSE) {
1232
1233                 /* release resource */
1234                 if (hcamcorder->camera_resource != NULL) {
1235                         ret = mm_resource_manager_mark_for_release(hcamcorder->resource_manager,
1236                                         hcamcorder->camera_resource);
1237                         if (ret != MM_RESOURCE_MANAGER_ERROR_NONE) {
1238                                 MMCAM_LOG_ERROR("could not mark camera resource for release");
1239                                 ret = MM_ERROR_CAMCORDER_INTERNAL;
1240                                 _MMCAMCORDER_UNLOCK_RESOURCE(hcamcorder);
1241                                 MMCAM_LOG_INFO("unlock resource");
1242                                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1243                         }
1244                 }
1245
1246                 if (hcamcorder->video_overlay_resource != NULL) {
1247                         ret = mm_resource_manager_mark_for_release(hcamcorder->resource_manager,
1248                                         hcamcorder->video_overlay_resource);
1249                         if (ret != MM_RESOURCE_MANAGER_ERROR_NONE) {
1250                                 MMCAM_LOG_ERROR("could not mark overlay resource for release");
1251                                 ret = MM_ERROR_CAMCORDER_INTERNAL;
1252                                 _MMCAMCORDER_UNLOCK_RESOURCE(hcamcorder);
1253                                 MMCAM_LOG_INFO("unlock resource");
1254                                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1255                         }
1256                 }
1257
1258                 ret = mm_resource_manager_commit(hcamcorder->resource_manager);
1259
1260                 if (ret != MM_RESOURCE_MANAGER_ERROR_NONE) {
1261                         MMCAM_LOG_ERROR("failed to release resource, ret(0x%x)", ret);
1262                         ret = MM_ERROR_CAMCORDER_INTERNAL;
1263
1264                         _MMCAMCORDER_UNLOCK_RESOURCE(hcamcorder);
1265                         MMCAM_LOG_INFO("unlock resource");
1266
1267                         goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1268                 } else {
1269                         if (hcamcorder->camera_resource != NULL)
1270                                 hcamcorder->camera_resource = NULL;
1271                         if (hcamcorder->video_overlay_resource != NULL)
1272                                 hcamcorder->video_overlay_resource = NULL;
1273                 }
1274         }
1275
1276         _MMCAMCORDER_UNLOCK_RESOURCE(hcamcorder);
1277
1278         MMCAM_LOG_WARNING("unlock resource");
1279 #endif /* _MMCAMCORDER_MM_RM_SUPPORT */
1280
1281 #ifdef _MMCAMCORDER_RM_SUPPORT
1282         _mmcamcorder_rm_deallocate(handle);
1283 #endif /* _MMCAMCORDER_RM_SUPPORT*/
1284
1285         /* Deinitialize main context member */
1286         hcamcorder->command = NULL;
1287
1288         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1289
1290         _mmcamcorder_set_state(handle, MM_CAMCORDER_STATE_NULL);
1291
1292         return MM_ERROR_NONE;
1293
1294 _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK:
1295         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1296
1297 _ERR_CAMCORDER_CMD_PRECON:
1298         /* send message */
1299         MMCAM_LOG_ERROR("Unrealize fail (type %d, state %d, ret %x)", hcamcorder->type, state, ret);
1300
1301         return ret;
1302 }
1303
1304 int _mmcamcorder_start(MMHandleType handle)
1305 {
1306         int ret = MM_ERROR_NONE;
1307         int state = MM_CAMCORDER_STATE_NONE;
1308
1309         _MMCamcorderSubContext *sc = NULL;
1310         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1311
1312         MMCAM_LOG_INFO("");
1313
1314         if (!hcamcorder) {
1315                 MMCAM_LOG_ERROR("Not initialized");
1316                 ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
1317                 return ret;
1318         }
1319
1320         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
1321         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1322
1323         if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
1324                 MMCAM_LOG_ERROR("Another command is running.");
1325                 ret = MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
1326                 goto _ERR_CAMCORDER_CMD_PRECON;
1327         }
1328
1329         state = _mmcamcorder_get_state(handle);
1330         if (state != MM_CAMCORDER_STATE_READY) {
1331                 MMCAM_LOG_ERROR("Wrong state(%d)", state);
1332                 ret = MM_ERROR_CAMCORDER_INVALID_STATE;
1333                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1334         }
1335
1336         /* initialize error code */
1337         hcamcorder->error_code = MM_ERROR_NONE;
1338
1339         /* set attributes related sensor */
1340         if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO)
1341                 _mmcamcorder_set_attribute_to_camsensor(handle);
1342
1343         ret = hcamcorder->command((MMHandleType)hcamcorder, _MMCamcorder_CMD_PREVIEW_START);
1344         if (ret != MM_ERROR_NONE) {
1345                 /* check internal error of gstreamer */
1346                 if (hcamcorder->error_code != MM_ERROR_NONE) {
1347                         ret = hcamcorder->error_code;
1348                         MMCAM_LOG_INFO("gstreamer error is occurred. return it %x", ret);
1349                 }
1350                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1351         }
1352
1353         _mmcamcorder_set_state(handle, MM_CAMCORDER_STATE_PREPARE);
1354
1355         /* set attributes related sensor - after start preview */
1356         if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO)
1357                 _mmcamcorder_set_attribute_to_camsensor2(handle);
1358
1359         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1360
1361         return MM_ERROR_NONE;
1362
1363 _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK:
1364         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1365
1366 _ERR_CAMCORDER_CMD_PRECON:
1367         /* check internal error of gstreamer */
1368         if (hcamcorder->error_code != MM_ERROR_NONE) {
1369                 ret = hcamcorder->error_code;
1370                 hcamcorder->error_code = MM_ERROR_NONE;
1371
1372                 MMCAM_LOG_INFO("gstreamer error is occurred. return it %x", ret);
1373         }
1374
1375         MMCAM_LOG_ERROR("Start fail (type %d, state %d, ret %x)",
1376                                    hcamcorder->type, state, ret);
1377
1378         return ret;
1379 }
1380
1381 int _mmcamcorder_stop(MMHandleType handle)
1382 {
1383         int ret = MM_ERROR_NONE;
1384         int state = MM_CAMCORDER_STATE_NONE;
1385
1386         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1387
1388         MMCAM_LOG_INFO("");
1389
1390         if (!hcamcorder) {
1391                 MMCAM_LOG_ERROR("Not initialized");
1392                 ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
1393                 return ret;
1394         }
1395
1396         if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
1397                 MMCAM_LOG_ERROR("Another command is running.");
1398                 ret = MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
1399                 goto _ERR_CAMCORDER_CMD_PRECON;
1400         }
1401
1402         state = _mmcamcorder_get_state(handle);
1403         if (state != MM_CAMCORDER_STATE_PREPARE) {
1404                 MMCAM_LOG_ERROR("Wrong state(%d)", state);
1405                 ret = MM_ERROR_CAMCORDER_INVALID_STATE;
1406                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1407         }
1408
1409         ret = hcamcorder->command((MMHandleType)hcamcorder, _MMCamcorder_CMD_PREVIEW_STOP);
1410         if (ret != MM_ERROR_NONE)
1411                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1412
1413         _mmcamcorder_set_state(handle, MM_CAMCORDER_STATE_READY);
1414
1415         if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
1416                 /* unsubscribe remained unsubscribed signal */
1417                 g_mutex_lock(&hcamcorder->gdbus_info_sound.sync_mutex);
1418                 if (hcamcorder->gdbus_info_sound.subscribe_id > 0) {
1419                         MMCAM_LOG_WARNING("subscribe_id[%u] is remained. remove it.", hcamcorder->gdbus_info_sound.subscribe_id);
1420                         g_dbus_connection_signal_unsubscribe(hcamcorder->gdbus_conn, hcamcorder->gdbus_info_sound.subscribe_id);
1421                 }
1422                 g_mutex_unlock(&hcamcorder->gdbus_info_sound.sync_mutex);
1423
1424                 g_mutex_lock(&hcamcorder->gdbus_info_solo_sound.sync_mutex);
1425                 if (hcamcorder->gdbus_info_solo_sound.subscribe_id > 0) {
1426                         MMCAM_LOG_WARNING("subscribe_id[%u] is remained. remove it.", hcamcorder->gdbus_info_solo_sound.subscribe_id);
1427                         g_dbus_connection_signal_unsubscribe(hcamcorder->gdbus_conn, hcamcorder->gdbus_info_solo_sound.subscribe_id);
1428                 }
1429                 g_mutex_unlock(&hcamcorder->gdbus_info_solo_sound.sync_mutex);
1430         }
1431
1432         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1433
1434         return MM_ERROR_NONE;
1435
1436 _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK:
1437         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1438
1439 _ERR_CAMCORDER_CMD_PRECON:
1440         /* send message */
1441         MMCAM_LOG_ERROR("Stop fail (type %d, state %d, ret %x)", hcamcorder->type, state, ret);
1442
1443         return ret;
1444 }
1445
1446
1447 int _mmcamcorder_capture_start(MMHandleType handle)
1448 {
1449         int ret = MM_ERROR_NONE;
1450         int state = MM_CAMCORDER_STATE_NONE;
1451         int state_FROM_0 = MM_CAMCORDER_STATE_PREPARE;
1452         int state_FROM_1 = MM_CAMCORDER_STATE_RECORDING;
1453         int state_FROM_2 = MM_CAMCORDER_STATE_PAUSED;
1454
1455         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1456
1457         MMCAM_LOG_INFO("");
1458
1459         if (!hcamcorder) {
1460                 MMCAM_LOG_ERROR("Not initialized");
1461                 ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
1462                 return ret;
1463         }
1464
1465         if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
1466                 MMCAM_LOG_ERROR("Another command is running.");
1467                 ret = MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
1468                 goto _ERR_CAMCORDER_CMD_PRECON;
1469         }
1470
1471         state = _mmcamcorder_get_state(handle);
1472         if (state != state_FROM_0 && state != state_FROM_1 && state != state_FROM_2) {
1473                 MMCAM_LOG_ERROR("Wrong state(%d)", state);
1474                 ret = MM_ERROR_CAMCORDER_INVALID_STATE;
1475                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1476         }
1477
1478         /* Handle capture in recording case */
1479         if (state == state_FROM_1 || state == state_FROM_2) {
1480                 if (hcamcorder->capture_in_recording == TRUE) {
1481                         MMCAM_LOG_ERROR("Capturing in recording (%d)", state);
1482                         ret = MM_ERROR_CAMCORDER_DEVICE_BUSY;
1483                         goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1484                 } else {
1485                         g_mutex_lock(&hcamcorder->task_thread_lock);
1486                         if (hcamcorder->task_thread_state == _MMCAMCORDER_TASK_THREAD_STATE_NONE) {
1487                                 hcamcorder->capture_in_recording = TRUE;
1488                                 hcamcorder->task_thread_state = _MMCAMCORDER_TASK_THREAD_STATE_CHECK_CAPTURE_IN_RECORDING;
1489                                 MMCAM_LOG_INFO("send signal for capture in recording");
1490                                 g_cond_signal(&hcamcorder->task_thread_cond);
1491                                 g_mutex_unlock(&hcamcorder->task_thread_lock);
1492                         } else {
1493                                 MMCAM_LOG_ERROR("task thread busy : %d", hcamcorder->task_thread_state);
1494                                 g_mutex_unlock(&hcamcorder->task_thread_lock);
1495                                 ret = MM_ERROR_CAMCORDER_INVALID_STATE;
1496                                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1497                         }
1498                 }
1499         }
1500
1501         ret = hcamcorder->command((MMHandleType)hcamcorder, _MMCamcorder_CMD_CAPTURE);
1502         if (ret != MM_ERROR_NONE)
1503                 goto _ERR_CAMCORDER_CMD;
1504
1505         /* Do not change state when recording snapshot capture */
1506         if (state == state_FROM_0)
1507                 _mmcamcorder_set_state(handle, MM_CAMCORDER_STATE_CAPTURING);
1508
1509         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1510
1511         /* Init break continuous shot attr */
1512         if (mm_camcorder_set_attributes(handle, NULL, MMCAM_CAPTURE_BREAK_CONTINUOUS_SHOT, 0, NULL) != MM_ERROR_NONE)
1513                 MMCAM_LOG_WARNING("capture-break-cont-shot set 0 failed");
1514
1515         return MM_ERROR_NONE;
1516
1517 _ERR_CAMCORDER_CMD:
1518         if (hcamcorder->capture_in_recording)
1519                 hcamcorder->capture_in_recording = FALSE;
1520
1521 _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK:
1522         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1523
1524 _ERR_CAMCORDER_CMD_PRECON:
1525         /* send message */
1526         MMCAM_LOG_ERROR("Capture start fail (type %d, state %d, ret %x)",
1527                 hcamcorder->type, state, ret);
1528
1529         return ret;
1530 }
1531
1532 int _mmcamcorder_capture_stop(MMHandleType handle)
1533 {
1534         int ret = MM_ERROR_NONE;
1535         int state = MM_CAMCORDER_STATE_NONE;
1536         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1537
1538         MMCAM_LOG_INFO("");
1539
1540         if (!hcamcorder) {
1541                 MMCAM_LOG_ERROR("Not initialized");
1542                 ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
1543                 return ret;
1544         }
1545
1546         if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
1547                 MMCAM_LOG_ERROR("Another command is running.");
1548                 ret = MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
1549                 goto _ERR_CAMCORDER_CMD_PRECON;
1550         }
1551
1552         state = _mmcamcorder_get_state(handle);
1553         if (state != MM_CAMCORDER_STATE_CAPTURING) {
1554                 MMCAM_LOG_ERROR("Wrong state(%d)", state);
1555                 ret = MM_ERROR_CAMCORDER_INVALID_STATE;
1556                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1557         }
1558
1559         ret = hcamcorder->command((MMHandleType)hcamcorder, _MMCamcorder_CMD_PREVIEW_START);
1560         if (ret != MM_ERROR_NONE)
1561                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1562
1563         _mmcamcorder_set_state(handle, MM_CAMCORDER_STATE_PREPARE);
1564
1565         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1566
1567         return MM_ERROR_NONE;
1568
1569 _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK:
1570         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1571
1572 _ERR_CAMCORDER_CMD_PRECON:
1573         /* send message */
1574         MMCAM_LOG_ERROR("Capture stop fail (type %d, state %d, ret %x)",
1575                 hcamcorder->type, state, ret);
1576
1577         return ret;
1578 }
1579
1580 int _mmcamcorder_record(MMHandleType handle)
1581 {
1582         int ret = MM_ERROR_NONE;
1583         int state = MM_CAMCORDER_STATE_NONE;
1584         int dpm_mic_state = DPM_ALLOWED;
1585
1586         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1587         _MMCamcorderSubContext *sc = NULL;
1588
1589         MMCAM_LOG_INFO("");
1590
1591         if (!hcamcorder || !MMF_CAMCORDER_SUBCONTEXT(hcamcorder)) {
1592                 MMCAM_LOG_ERROR("Not initialized");
1593                 ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
1594                 return ret;
1595         }
1596
1597         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
1598
1599         if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
1600                 MMCAM_LOG_ERROR("Another command is running.");
1601                 ret = MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
1602                 goto _ERR_CAMCORDER_CMD_PRECON;
1603         }
1604
1605         state = _mmcamcorder_get_state(handle);
1606         if (state != MM_CAMCORDER_STATE_PREPARE && state != MM_CAMCORDER_STATE_PAUSED) {
1607                 MMCAM_LOG_ERROR("Wrong state(%d)", state);
1608                 ret = MM_ERROR_CAMCORDER_INVALID_STATE;
1609                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1610         }
1611
1612         /* initialize error code */
1613         hcamcorder->error_code = MM_ERROR_NONE;
1614
1615         /* get audio disable */
1616         mm_camcorder_get_attributes(handle, NULL,
1617                 MMCAM_AUDIO_DISABLE, &sc->audio_disable,
1618                 NULL);
1619
1620         sc->audio_disable |= sc->is_modified_rate;
1621
1622         /* check mic policy from DPM */
1623         if (hcamcorder->dpm_handle && sc->audio_disable == FALSE) {
1624                 if (dpm_restriction_get_microphone_state(hcamcorder->dpm_handle, &dpm_mic_state) == DPM_ERROR_NONE) {
1625                         MMCAM_LOG_INFO("DPM mic state %d", dpm_mic_state);
1626                         if (dpm_mic_state == DPM_DISALLOWED) {
1627                                 MMCAM_LOG_ERROR("MIC DISALLOWED by DPM");
1628                                 ret = MM_ERROR_COMMON_INVALID_PERMISSION;
1629
1630                                 _mmcamcorder_request_dpm_popup(hcamcorder->gdbus_conn, "microphone");
1631
1632                                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1633                         }
1634                 } else {
1635                         MMCAM_LOG_ERROR("get DPM mic state failed, keep going...");
1636                 }
1637         } else {
1638                 MMCAM_LOG_WARNING("skip dpm check - handle %p, audio disable %d",
1639                         hcamcorder->dpm_handle, sc->audio_disable);
1640         }
1641
1642         ret = hcamcorder->command((MMHandleType)hcamcorder, _MMCamcorder_CMD_RECORD);
1643         if (ret != MM_ERROR_NONE) {
1644                 /* check internal error of gstreamer */
1645                 if (hcamcorder->error_code != MM_ERROR_NONE) {
1646                         ret = hcamcorder->error_code;
1647                         MMCAM_LOG_INFO("gstreamer error is occurred. return it %x", ret);
1648                 }
1649                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1650         }
1651
1652         _mmcamcorder_set_state(handle, MM_CAMCORDER_STATE_RECORDING);
1653
1654         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1655
1656         return MM_ERROR_NONE;
1657
1658 _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK:
1659         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1660
1661 _ERR_CAMCORDER_CMD_PRECON:
1662         /* check internal error of gstreamer */
1663         if (hcamcorder->error_code != MM_ERROR_NONE) {
1664                 ret = hcamcorder->error_code;
1665                 hcamcorder->error_code = MM_ERROR_NONE;
1666
1667                 MMCAM_LOG_INFO("gstreamer error is occurred. return it %x", ret);
1668         }
1669
1670         MMCAM_LOG_ERROR("Record fail (type %d, state %d, ret %x)",
1671                                    hcamcorder->type, state, ret);
1672
1673         return ret;
1674 }
1675
1676
1677 int _mmcamcorder_pause(MMHandleType handle)
1678 {
1679         int ret = MM_ERROR_NONE;
1680         int state = MM_CAMCORDER_STATE_NONE;
1681
1682         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1683
1684         MMCAM_LOG_INFO("");
1685
1686         if (!hcamcorder) {
1687                 MMCAM_LOG_ERROR("Not initialized");
1688                 ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
1689                 return ret;
1690         }
1691
1692         if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
1693                 MMCAM_LOG_ERROR("Another command is running.");
1694                 ret = MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
1695                 goto _ERR_CAMCORDER_CMD_PRECON;
1696         }
1697
1698         state = _mmcamcorder_get_state(handle);
1699         if (state != MM_CAMCORDER_STATE_RECORDING) {
1700                 MMCAM_LOG_ERROR("Wrong state(%d)", state);
1701                 ret = MM_ERROR_CAMCORDER_INVALID_STATE;
1702                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1703         }
1704
1705         ret = hcamcorder->command((MMHandleType)hcamcorder, _MMCamcorder_CMD_PAUSE);
1706         if (ret != MM_ERROR_NONE)
1707                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1708
1709         _mmcamcorder_set_state(handle, MM_CAMCORDER_STATE_PAUSED);
1710
1711         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1712
1713         return MM_ERROR_NONE;
1714
1715 _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK:
1716         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1717
1718 _ERR_CAMCORDER_CMD_PRECON:
1719         /* send message */
1720         MMCAM_LOG_ERROR("Pause fail (type %d, state %d, ret %x)", hcamcorder->type, state, ret);
1721
1722         return ret;
1723 }
1724
1725
1726 int _mmcamcorder_commit(MMHandleType handle)
1727 {
1728         int ret = MM_ERROR_NONE;
1729         int state = MM_CAMCORDER_STATE_NONE;
1730
1731         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1732
1733         MMCAM_LOG_INFO("");
1734
1735         if (!hcamcorder) {
1736                 MMCAM_LOG_ERROR("Not initialized");
1737                 ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
1738                 return ret;
1739         }
1740
1741         if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
1742                 MMCAM_LOG_ERROR("Another command is running.");
1743                 ret = MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
1744                 goto _ERR_CAMCORDER_CMD_PRECON;
1745         }
1746
1747         state = _mmcamcorder_get_state(handle);
1748         if (state != MM_CAMCORDER_STATE_RECORDING && state != MM_CAMCORDER_STATE_PAUSED) {
1749                 MMCAM_LOG_ERROR("Wrong state(%d)", state);
1750                 ret = MM_ERROR_CAMCORDER_INVALID_STATE;
1751                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1752         }
1753
1754         /* initialize error code */
1755         hcamcorder->error_code = MM_ERROR_NONE;
1756
1757         ret = hcamcorder->command((MMHandleType)hcamcorder, _MMCamcorder_CMD_COMMIT);
1758         if (ret != MM_ERROR_NONE)
1759                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1760
1761         _mmcamcorder_set_state(handle, MM_CAMCORDER_STATE_PREPARE);
1762
1763         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1764
1765         return MM_ERROR_NONE;
1766
1767 _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK:
1768         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1769
1770 _ERR_CAMCORDER_CMD_PRECON:
1771         /* send message */
1772         MMCAM_LOG_ERROR("Commit fail (type %d, state %d, ret %x)", hcamcorder->type, state, ret);
1773
1774         return ret;
1775 }
1776
1777
1778 int _mmcamcorder_cancel(MMHandleType handle)
1779 {
1780         int ret = MM_ERROR_NONE;
1781         int state = MM_CAMCORDER_STATE_NONE;
1782
1783         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1784
1785         MMCAM_LOG_INFO("");
1786
1787         if (!hcamcorder) {
1788                 MMCAM_LOG_ERROR("Not initialized");
1789                 ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
1790                 return ret;
1791         }
1792
1793         if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
1794                 MMCAM_LOG_ERROR("Another command is running.");
1795                 ret = MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
1796                 goto _ERR_CAMCORDER_CMD_PRECON;
1797         }
1798
1799         state = _mmcamcorder_get_state(handle);
1800         if (state != MM_CAMCORDER_STATE_RECORDING && state != MM_CAMCORDER_STATE_PAUSED) {
1801                 MMCAM_LOG_ERROR("Wrong state(%d)", state);
1802                 ret = MM_ERROR_CAMCORDER_INVALID_STATE;
1803                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1804         }
1805
1806         ret = hcamcorder->command((MMHandleType)hcamcorder, _MMCamcorder_CMD_CANCEL);
1807         if (ret != MM_ERROR_NONE)
1808                 goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
1809
1810         _mmcamcorder_set_state(handle, MM_CAMCORDER_STATE_PREPARE);
1811
1812         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1813
1814         return MM_ERROR_NONE;
1815
1816 _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK:
1817         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
1818
1819 _ERR_CAMCORDER_CMD_PRECON:
1820         /* send message */
1821         MMCAM_LOG_ERROR("Cancel fail (type %d, state %d, ret %x)",
1822                                    hcamcorder->type, state, ret);
1823
1824         return ret;
1825 }
1826 /* } Internal command functions */
1827
1828
1829 int _mmcamcorder_commit_async_end(MMHandleType handle)
1830 {
1831         MMCAM_LOG_INFO("");
1832
1833         MMCAM_LOG_WARNING("_mmcamcorder_commit_async_end : MM_CAMCORDER_STATE_PREPARE");
1834         _mmcamcorder_set_state(handle, MM_CAMCORDER_STATE_PREPARE);
1835
1836         return MM_ERROR_NONE;
1837 }
1838
1839
1840 int _mmcamcorder_set_message_callback(MMHandleType handle, MMMessageCallback callback, void *user_data)
1841 {
1842         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1843
1844         MMCAM_LOG_INFO("%p", hcamcorder);
1845
1846         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1847
1848         if (callback == NULL)
1849                 MMCAM_LOG_WARNING("Message Callback is disabled, because application sets it to NULL");
1850
1851         if (!_MMCAMCORDER_TRYLOCK_MESSAGE_CALLBACK(hcamcorder)) {
1852                 MMCAM_LOG_WARNING("Application's message callback is running now");
1853                 return MM_ERROR_CAMCORDER_INVALID_CONDITION;
1854         }
1855
1856         /* set message callback to message handle */
1857         hcamcorder->msg_cb = callback;
1858         hcamcorder->msg_cb_param = user_data;
1859
1860         _MMCAMCORDER_UNLOCK_MESSAGE_CALLBACK(hcamcorder);
1861
1862         return MM_ERROR_NONE;
1863 }
1864
1865
1866 int _mmcamcorder_set_video_stream_callback(MMHandleType handle, mm_camcorder_video_stream_callback callback, void *user_data)
1867 {
1868         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1869
1870         /*MMCAM_LOG_INFO("");*/
1871
1872         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1873
1874         if (callback == NULL)
1875                 MMCAM_LOG_WARNING("Video Stream Callback is disabled, because application sets it to NULL");
1876
1877         _MMCAMCORDER_LOCK_VSTREAM_CALLBACK(hcamcorder);
1878
1879         hcamcorder->vstream_cb = callback;
1880         hcamcorder->vstream_cb_param = user_data;
1881
1882         _MMCAMCORDER_UNLOCK_VSTREAM_CALLBACK(hcamcorder);
1883
1884         return MM_ERROR_NONE;
1885 }
1886
1887
1888 int _mmcamcorder_set_audio_stream_callback(MMHandleType handle, mm_camcorder_audio_stream_callback callback, void *user_data)
1889 {
1890         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1891
1892         MMCAM_LOG_INFO("");
1893
1894         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1895
1896         if (callback == NULL)
1897                 MMCAM_LOG_WARNING("Audio Stream Callback is disabled, because application sets it to NULL");
1898
1899         if (!_MMCAMCORDER_TRYLOCK_ASTREAM_CALLBACK(hcamcorder)) {
1900                 MMCAM_LOG_WARNING("Application's audio stream callback is running now");
1901                 return MM_ERROR_CAMCORDER_INVALID_CONDITION;
1902         }
1903
1904         hcamcorder->astream_cb = callback;
1905         hcamcorder->astream_cb_param = user_data;
1906
1907         _MMCAMCORDER_UNLOCK_ASTREAM_CALLBACK(hcamcorder);
1908
1909         return MM_ERROR_NONE;
1910 }
1911
1912
1913 int _mmcamcorder_set_muxed_stream_callback(MMHandleType handle, mm_camcorder_muxed_stream_callback callback, void *user_data)
1914 {
1915         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1916
1917         MMCAM_LOG_INFO("");
1918
1919         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1920
1921         if (callback == NULL)
1922                 MMCAM_LOG_WARNING("Muxed Stream Callback is disabled, because application sets it to NULL");
1923
1924         if (!_MMCAMCORDER_TRYLOCK_MSTREAM_CALLBACK(hcamcorder)) {
1925                 MMCAM_LOG_WARNING("Application's muxed stream callback is running now");
1926                 return MM_ERROR_CAMCORDER_INVALID_CONDITION;
1927         }
1928
1929         hcamcorder->mstream_cb = callback;
1930         hcamcorder->mstream_cb_param = user_data;
1931
1932         _MMCAMCORDER_UNLOCK_MSTREAM_CALLBACK(hcamcorder);
1933
1934         return MM_ERROR_NONE;
1935 }
1936
1937
1938 int _mmcamcorder_set_video_capture_callback(MMHandleType handle, mm_camcorder_video_capture_callback callback, void *user_data)
1939 {
1940         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1941
1942         MMCAM_LOG_INFO("");
1943
1944         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1945
1946         if (callback == NULL)
1947                 MMCAM_LOG_WARNING("Video Capture Callback is disabled, because application sets it to NULLL");
1948
1949         if (!_MMCAMCORDER_TRYLOCK_VCAPTURE_CALLBACK(hcamcorder)) {
1950                 MMCAM_LOG_WARNING("Application's video capture callback is running now");
1951                 return MM_ERROR_CAMCORDER_INVALID_CONDITION;
1952         }
1953
1954         hcamcorder->vcapture_cb = callback;
1955         hcamcorder->vcapture_cb_param = user_data;
1956
1957         _MMCAMCORDER_UNLOCK_VCAPTURE_CALLBACK(hcamcorder);
1958
1959         return MM_ERROR_NONE;
1960 }
1961
1962
1963 int _mmcamcorder_set_video_encode_decision_callback(MMHandleType handle, mm_camcorder_video_encode_decision_callback callback, void *user_data)
1964 {
1965         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1966
1967         MMCAM_LOG_INFO("");
1968
1969         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1970
1971         if (callback == NULL)
1972                 MMCAM_LOG_WARNING("Video Encode Decision Callback is disabled, because application sets it to NULL");
1973
1974         if (!_MMCAMCORDER_TRYLOCK_VEDECISION_CALLBACK(hcamcorder)) {
1975                 MMCAM_LOG_WARNING("Application's video encode decision callback is running now");
1976                 return MM_ERROR_CAMCORDER_INVALID_CONDITION;
1977         }
1978
1979         hcamcorder->vedecision_cb = callback;
1980         hcamcorder->vedecision_cb_param = user_data;
1981
1982         _MMCAMCORDER_UNLOCK_VEDECISION_CALLBACK(hcamcorder);
1983
1984         return MM_ERROR_NONE;
1985 }
1986
1987
1988 int _mmcamcorder_get_current_state(MMHandleType handle)
1989 {
1990         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
1991
1992         MMCAM_LOG_INFO("");
1993
1994         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
1995
1996         return _mmcamcorder_get_state(handle);
1997 }
1998
1999 int _mmcamcorder_init_focusing(MMHandleType handle)
2000 {
2001         int ret = TRUE;
2002         int state = MM_CAMCORDER_STATE_NONE;
2003         int focus_mode = MM_CAMCORDER_FOCUS_MODE_NONE;
2004         int af_range = MM_CAMCORDER_AUTO_FOCUS_NORMAL;
2005         int sensor_focus_mode = 0;
2006         int sensor_af_range = 0;
2007         int current_focus_mode = 0;
2008         int current_af_range = 0;
2009         mmf_camcorder_t *hcamcorder = NULL;
2010         _MMCamcorderSubContext *sc = NULL;
2011         GstCameraControl *control = NULL;
2012
2013         MMCAM_LOG_INFO("");
2014
2015         hcamcorder = MMF_CAMCORDER(handle);
2016         mmf_return_val_if_fail(handle, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2017
2018         sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
2019         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2020
2021         if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
2022                 MMCAM_LOG_ERROR("Another command is running.");
2023                 return MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
2024         }
2025
2026         state = _mmcamcorder_get_state(handle);
2027
2028         if (state == MM_CAMCORDER_STATE_CAPTURING ||
2029             state < MM_CAMCORDER_STATE_PREPARE) {
2030                 MMCAM_LOG_ERROR("Not proper state. state[%d]", state);
2031                 _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
2032                 return MM_ERROR_CAMCORDER_INVALID_STATE;
2033         }
2034
2035         if (!GST_IS_CAMERA_CONTROL(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst)) {
2036                 MMCAM_LOG_INFO("Can't cast Video source into camera control.");
2037                 _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
2038                 return MM_ERROR_NONE;
2039         }
2040
2041         control = GST_CAMERA_CONTROL(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst);
2042         if (control == NULL) {
2043                 MMCAM_LOG_ERROR("cast CAMERA_CONTROL failed");
2044                 _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
2045                 return MM_ERROR_CAMCORDER_INTERNAL;
2046         }
2047
2048         ret = gst_camera_control_stop_auto_focus(control);
2049         if (!ret) {
2050                 MMCAM_LOG_ERROR("Auto focusing stop fail.");
2051                 _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
2052                 return MM_ERROR_CAMCORDER_DEVICE_IO;
2053         }
2054
2055         /* Initialize lens position */
2056         mm_camcorder_get_attributes(handle, NULL,
2057                 MMCAM_CAMERA_FOCUS_MODE, &focus_mode,
2058                 MMCAM_CAMERA_AF_SCAN_RANGE, &af_range,
2059                 NULL);
2060         sensor_af_range = _mmcamcorder_convert_msl_to_sensor(handle, MM_CAM_CAMERA_AF_SCAN_RANGE, af_range);
2061         sensor_focus_mode = _mmcamcorder_convert_msl_to_sensor(handle, MM_CAM_CAMERA_FOCUS_MODE, focus_mode);
2062
2063         gst_camera_control_get_focus(control, &current_focus_mode, &current_af_range);
2064
2065         if (current_focus_mode != sensor_focus_mode || current_af_range != sensor_af_range) {
2066                 ret = gst_camera_control_set_focus(control, sensor_focus_mode, sensor_af_range);
2067         } else {
2068                 MMCAM_LOG_INFO("No need to init FOCUS [mode:%d, range:%d]", focus_mode, af_range);
2069                 ret = TRUE;
2070         }
2071
2072         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
2073
2074         if (ret) {
2075                 MMCAM_LOG_INFO("Lens init success.");
2076                 return MM_ERROR_NONE;
2077         } else {
2078                 MMCAM_LOG_ERROR("Lens init fail.");
2079                 return MM_ERROR_CAMCORDER_DEVICE_IO;
2080         }
2081 }
2082
2083 int _mmcamcorder_adjust_focus(MMHandleType handle, int direction)
2084 {
2085         int state;
2086         int focus_mode = MM_CAMCORDER_FOCUS_MODE_NONE;
2087         int ret = MM_ERROR_UNKNOWN;
2088         char *err_attr_name = NULL;
2089
2090         mmf_return_val_if_fail(handle, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2091         mmf_return_val_if_fail(direction, MM_ERROR_CAMCORDER_INVALID_ARGUMENT);
2092
2093         /*MMCAM_LOG_INFO("");*/
2094
2095         if (!_MMCAMCORDER_TRYLOCK_CMD(handle)) {
2096                 MMCAM_LOG_ERROR("Another command is running.");
2097                 return MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
2098         }
2099
2100         state = _mmcamcorder_get_state(handle);
2101         if (state == MM_CAMCORDER_STATE_CAPTURING || state < MM_CAMCORDER_STATE_PREPARE) {
2102                 MMCAM_LOG_ERROR("Not proper state. state[%d]", state);
2103                 _MMCAMCORDER_UNLOCK_CMD(handle);
2104                 return MM_ERROR_CAMCORDER_INVALID_STATE;
2105         }
2106
2107         /* TODO : call a auto or manual focus function */
2108         ret = mm_camcorder_get_attributes(handle, &err_attr_name,
2109                 MMCAM_CAMERA_FOCUS_MODE, &focus_mode,
2110                 NULL);
2111         if (ret != MM_ERROR_NONE) {
2112                 MMCAM_LOG_WARNING("Get focus-mode fail. (%s:%x)", err_attr_name, ret);
2113                 SAFE_FREE(err_attr_name);
2114                 _MMCAMCORDER_UNLOCK_CMD(handle);
2115                 return ret;
2116         }
2117
2118         switch (focus_mode) {
2119         case MM_CAMCORDER_FOCUS_MODE_MANUAL:
2120                 ret = _mmcamcorder_adjust_manual_focus(handle, direction);
2121                 break;
2122         case MM_CAMCORDER_FOCUS_MODE_AUTO:
2123         case MM_CAMCORDER_FOCUS_MODE_TOUCH_AUTO:
2124         case MM_CAMCORDER_FOCUS_MODE_CONTINUOUS:
2125                 ret = _mmcamcorder_adjust_auto_focus(handle);
2126                 break;
2127         default:
2128                 MMCAM_LOG_ERROR("It doesn't adjust focus. Focusing mode(%d)", focus_mode);
2129                 ret = MM_ERROR_CAMCORDER_NOT_SUPPORTED;
2130         }
2131
2132         _MMCAMCORDER_UNLOCK_CMD(handle);
2133
2134         return ret;
2135 }
2136
2137 int _mmcamcorder_adjust_manual_focus(MMHandleType handle, int direction)
2138 {
2139         int max_level = 0;
2140         int min_level = 0;
2141         int cur_level = 0;
2142         int focus_level = 0;
2143         float unit_level = 0;
2144         GstCameraControl *control = NULL;
2145         _MMCamcorderSubContext *sc = NULL;
2146         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
2147
2148         MMCAM_LOG_INFO("");
2149
2150         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2151         mmf_return_val_if_fail(_MMFCAMCORDER_FOCUS_TOTAL_LEVEL != 1, MM_ERROR_CAMCORDER_NOT_SUPPORTED);
2152         mmf_return_val_if_fail((direction >= MM_CAMCORDER_MF_LENS_DIR_FORWARD) &&
2153                 (direction <= MM_CAMCORDER_MF_LENS_DIR_BACKWARD),
2154                 MM_ERROR_CAMCORDER_INVALID_ARGUMENT);
2155
2156         sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
2157         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2158
2159         if (!GST_IS_CAMERA_CONTROL(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst)) {
2160                 MMCAM_LOG_INFO("Can't cast Video source into camera control.");
2161                 return MM_ERROR_NONE;
2162         }
2163
2164         control = GST_CAMERA_CONTROL(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst);
2165         if (control == NULL) {
2166                 MMCAM_LOG_ERROR("cast CAMERA_CONTROL failed");
2167                 return MM_ERROR_CAMCORDER_INTERNAL;
2168         }
2169
2170         /* TODO : get max, min level */
2171         if (max_level - min_level + 1 < _MMFCAMCORDER_FOCUS_TOTAL_LEVEL)
2172                 MMCAM_LOG_WARNING("Total level  of manual focus of MMF is greater than that of the camera driver.");
2173
2174         unit_level = ((float)max_level - (float)min_level)/(float)(_MMFCAMCORDER_FOCUS_TOTAL_LEVEL - 1);
2175
2176         if (!gst_camera_control_get_focus_level(control, &cur_level)) {
2177                 MMCAM_LOG_ERROR("Can't get current level of manual focus.");
2178                 return MM_ERROR_CAMCORDER_DEVICE_IO;
2179         }
2180
2181         /* TODO : adjust unit level value */
2182         if (direction == MM_CAMCORDER_MF_LENS_DIR_FORWARD)
2183                 focus_level = cur_level + unit_level;
2184         else if (direction == MM_CAMCORDER_MF_LENS_DIR_BACKWARD)
2185                 focus_level = cur_level - unit_level;
2186
2187         if (focus_level > max_level)
2188                 focus_level = max_level;
2189         else if (focus_level < min_level)
2190                 focus_level = min_level;
2191
2192         if (!gst_camera_control_set_focus_level(control, focus_level)) {
2193                 MMCAM_LOG_ERROR("Manual focusing fail.");
2194                 return MM_ERROR_CAMCORDER_DEVICE_IO;
2195         }
2196
2197         return MM_ERROR_NONE;
2198 }
2199
2200
2201 int _mmcamcorder_adjust_auto_focus(MMHandleType handle)
2202 {
2203         gboolean ret;
2204         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
2205         GstCameraControl *control = NULL;
2206         _MMCamcorderSubContext *sc = NULL;
2207
2208         mmf_return_val_if_fail(handle, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2209
2210         /*MMCAM_LOG_INFO("");*/
2211
2212         sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
2213
2214         if (!GST_IS_CAMERA_CONTROL(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst)) {
2215                 MMCAM_LOG_INFO("Can't cast Video source into camera control.");
2216                 return MM_ERROR_NONE;
2217         }
2218
2219         control = GST_CAMERA_CONTROL(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst);
2220         if (control) {
2221                 /* Start AF */
2222                 ret = gst_camera_control_start_auto_focus(control);
2223         } else {
2224                 MMCAM_LOG_ERROR("cast CAMERA_CONTROL failed");
2225                 ret = FALSE;
2226         }
2227
2228         if (ret) {
2229                 MMCAM_LOG_INFO("Auto focusing start success.");
2230                 return MM_ERROR_NONE;
2231         } else {
2232                 MMCAM_LOG_ERROR("Auto focusing start fail.");
2233                 return MM_ERROR_CAMCORDER_DEVICE_IO;
2234         }
2235 }
2236
2237 int _mmcamcorder_stop_focusing(MMHandleType handle)
2238 {
2239         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
2240         _MMCamcorderSubContext *sc = NULL;
2241
2242         int ret, state;
2243         GstCameraControl *control = NULL;
2244
2245         mmf_return_val_if_fail(handle, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2246
2247         /*MMCAM_LOG_INFO("");*/
2248
2249         sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
2250
2251         if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
2252                 MMCAM_LOG_ERROR("Another command is running.");
2253                 return MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
2254         }
2255
2256         state = _mmcamcorder_get_state(handle);
2257         if (state == MM_CAMCORDER_STATE_CAPTURING || state < MM_CAMCORDER_STATE_PREPARE) {
2258                 MMCAM_LOG_ERROR("Not proper state. state[%d]", state);
2259                 _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
2260                 return MM_ERROR_CAMCORDER_INVALID_STATE;
2261         }
2262
2263         if (!GST_IS_CAMERA_CONTROL(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst)) {
2264                 MMCAM_LOG_INFO("Can't cast Video source into camera control.");
2265                 _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
2266                 return MM_ERROR_NONE;
2267         }
2268
2269         control = GST_CAMERA_CONTROL(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst);
2270         if (control) {
2271                 ret = gst_camera_control_stop_auto_focus(control);
2272         } else {
2273                 MMCAM_LOG_ERROR("cast CAMERA_CONTROL failed");
2274                 ret = FALSE;
2275         }
2276
2277         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
2278
2279         if (ret) {
2280                 MMCAM_LOG_INFO("Auto focusing stop success.");
2281                 return MM_ERROR_NONE;
2282         } else {
2283                 MMCAM_LOG_ERROR("Auto focusing stop fail.");
2284                 return MM_ERROR_CAMCORDER_DEVICE_IO;
2285         }
2286 }
2287
2288 int _mmcamcorder_get_focus_level(MMHandleType handle, int *level)
2289 {
2290         GstCameraControl *control = NULL;
2291         _MMCamcorderSubContext *sc = NULL;
2292         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
2293
2294         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2295         mmf_return_val_if_fail(level, MM_ERROR_CAMCORDER_INVALID_ARGUMENT);
2296
2297         sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
2298         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
2299
2300         if (!GST_IS_CAMERA_CONTROL(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst)) {
2301                 MMCAM_LOG_ERROR("Can't cast Video source into camera control.");
2302                 return MM_ERROR_CAMCORDER_NOT_SUPPORTED;
2303         }
2304
2305         control = GST_CAMERA_CONTROL(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst);
2306         if (!control) {
2307                 MMCAM_LOG_ERROR("cast CAMERA_CONTROL failed");
2308                 return MM_ERROR_CAMCORDER_NOT_SUPPORTED;
2309         }
2310
2311         if (!gst_camera_control_get_focus_level(control, level)) {
2312                 MMCAM_LOG_ERROR("get focus level failed");
2313                 return MM_ERROR_CAMCORDER_NOT_SUPPORTED;
2314         }
2315
2316         MMCAM_LOG_INFO("focus level[%d]", *level);
2317
2318         return MM_ERROR_NONE;
2319 }
2320
2321
2322 /*-----------------------------------------------
2323 |         CAMCORDER INTERNAL LOCAL              |
2324 -----------------------------------------------*/
2325 static gboolean __mmcamcorder_init_gstreamer(camera_conf *conf)
2326 {
2327         static const int max_argc = 10;
2328         int i = 0;
2329         int cnt_str = 0;
2330         gint *argc = NULL;
2331         gchar **argv = NULL;
2332         GError *err = NULL;
2333         gboolean ret = FALSE;
2334         type_string_array *GSTInitOption = NULL;
2335
2336         mmf_return_val_if_fail(conf, FALSE);
2337
2338         MMCAM_LOG_INFO("");
2339
2340         /* alloc */
2341         argc = malloc(sizeof(int));
2342         argv = malloc(sizeof(gchar *) * max_argc);
2343
2344         if (!argc || !argv)
2345                 goto ERROR;
2346
2347         memset(argv, 0, sizeof(gchar *) * max_argc);
2348
2349         /* add initial */
2350         *argc = 1;
2351         argv[0] = g_strdup("mmcamcorder");
2352
2353         /* add gst_param */
2354         _mmcamcorder_conf_get_value_string_array(conf,
2355                 CONFIGURE_CATEGORY_MAIN_GENERAL,
2356                 "GSTInitOption",
2357                 &GSTInitOption);
2358         if (GSTInitOption != NULL && GSTInitOption->value) {
2359                 cnt_str = GSTInitOption->count;
2360                 for ( ; *argc < max_argc && *argc <= cnt_str ; (*argc)++)
2361                         argv[*argc] = g_strdup(GSTInitOption->value[(*argc)-1]);
2362         }
2363
2364         MMCAM_LOG_INFO("initializing gstreamer with following parameter[argc:%d]", *argc);
2365
2366         for (i = 0; i < *argc; i++)
2367                 MMCAM_LOG_INFO("argv[%d] : %s", i, argv[i]);
2368
2369         /* initializing gstreamer */
2370         ret = gst_init_check(argc, &argv, &err);
2371         if (!ret) {
2372                 MMCAM_LOG_ERROR("Could not initialize GStreamer: %s ", err ? err->message : "unknown error occurred");
2373                 if (err)
2374                         g_error_free(err);
2375         }
2376
2377         /* release */
2378         for (i = 0; i < *argc; i++) {
2379                 if (argv[i]) {
2380                         g_free(argv[i]);
2381                         argv[i] = NULL;
2382                 }
2383         }
2384
2385         if (argv) {
2386                 free(argv);
2387                 argv = NULL;
2388         }
2389
2390         if (argc) {
2391                 free(argc);
2392                 argc = NULL;
2393         }
2394
2395         return ret;
2396
2397 ERROR:
2398         MMCAM_LOG_ERROR("failed to initialize gstreamer");
2399
2400         if (argv) {
2401                 free(argv);
2402                 argv = NULL;
2403         }
2404
2405         if (argc) {
2406                 free(argc);
2407                 argc = NULL;
2408         }
2409
2410         return FALSE;
2411 }
2412
2413
2414 int _mmcamcorder_get_state(MMHandleType handle)
2415 {
2416         int state;
2417         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
2418
2419         mmf_return_val_if_fail(hcamcorder, -1);
2420
2421         _MMCAMCORDER_LOCK_STATE(handle);
2422
2423         state = hcamcorder->state;
2424         /*MMCAM_LOG_INFO("state=%d",state);*/
2425
2426         _MMCAMCORDER_UNLOCK_STATE(handle);
2427
2428         return state;
2429 }
2430
2431
2432 int _mmcamcorder_get_state2(MMHandleType handle, int *state, int *old_state)
2433 {
2434         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
2435
2436         mmf_return_val_if_fail(hcamcorder && state && old_state, MM_ERROR_CAMCORDER_INVALID_ARGUMENT);
2437
2438         _MMCAMCORDER_LOCK_STATE(handle);
2439
2440         *state = hcamcorder->state;
2441         *old_state = hcamcorder->old_state;
2442
2443         _MMCAMCORDER_UNLOCK_STATE(handle);
2444
2445         return MM_ERROR_NONE;
2446 }
2447
2448
2449 void _mmcamcorder_set_state(MMHandleType handle, int state)
2450 {
2451         int old_state;
2452         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
2453         _MMCamcorderMsgItem msg;
2454
2455         mmf_return_if_fail(hcamcorder);
2456
2457         /*MMCAM_LOG_INFO("");*/
2458
2459         _MMCAMCORDER_LOCK_STATE(handle);
2460
2461         old_state = hcamcorder->state;
2462         if (old_state != state) {
2463                 hcamcorder->state = state;
2464                 hcamcorder->old_state = old_state;
2465
2466                 MMCAM_LOG_INFO("set state[%d -> %d] and send state-changed message", old_state, state);
2467
2468                 /* To discern who changes the state */
2469                 switch (hcamcorder->state_change_by_system) {
2470                 case _MMCAMCORDER_STATE_CHANGE_BY_RM:
2471                         msg.id = MM_MESSAGE_CAMCORDER_STATE_CHANGED_BY_RM;
2472                         msg.param.state.code = MM_ERROR_NONE;
2473                         break;
2474                 case _MMCAMCORDER_STATE_CHANGE_BY_DPM:
2475                         msg.id = MM_MESSAGE_CAMCORDER_STATE_CHANGED_BY_SECURITY;
2476                         msg.param.state.code = MM_ERROR_NONE;
2477                         break;
2478                 case _MMCAMCORDER_STATE_CHANGE_NORMAL:
2479                 default:
2480                         msg.id = MM_MESSAGE_CAMCORDER_STATE_CHANGED;
2481                         msg.param.state.code = MM_ERROR_NONE;
2482                         break;
2483                 }
2484
2485                 msg.param.state.previous = old_state;
2486                 msg.param.state.current = state;
2487
2488                 /*MMCAM_LOG_INFO("_mmcamcorder_send_message : msg : %p, id:%x", &msg, msg.id);*/
2489                 _mmcamcorder_send_message(handle, &msg);
2490         }
2491
2492         _MMCAMCORDER_UNLOCK_STATE(handle);
2493
2494         return;
2495 }
2496
2497
2498 _MMCamcorderSubContext *_mmcamcorder_alloc_subcontext(int type)
2499 {
2500         int i;
2501         _MMCamcorderSubContext *sc = NULL;
2502
2503         /*MMCAM_LOG_INFO("");*/
2504
2505         /* alloc container */
2506         sc = (_MMCamcorderSubContext *)g_malloc0(sizeof(_MMCamcorderSubContext));
2507
2508         sc->element_num = _MMCAMCORDER_PIPELINE_ELEMENT_NUM;
2509         sc->encode_element_num = _MMCAMCORDER_ENCODE_PIPELINE_ELEMENT_NUM;
2510
2511         /* alloc info for each mode */
2512         switch (type) {
2513         case MM_CAMCORDER_MODE_AUDIO:
2514                 sc->info_audio = g_malloc0(sizeof(_MMCamcorderAudioInfo));
2515                 break;
2516         case MM_CAMCORDER_MODE_VIDEO_CAPTURE:
2517         default:
2518                 sc->info_image = g_malloc0(sizeof(_MMCamcorderImageInfo));
2519                 sc->info_video = g_malloc0(sizeof(_MMCamcorderVideoInfo));
2520
2521                 sc->info_image->sound_status = _SOUND_STATUS_INIT;
2522                 g_mutex_init(&sc->info_video->size_check_lock);
2523                 break;
2524         }
2525
2526         /* alloc element array */
2527         sc->element = (_MMCamcorderGstElement *)g_malloc(sizeof(_MMCamcorderGstElement) * sc->element_num);
2528         sc->encode_element = (_MMCamcorderGstElement *)g_malloc(sizeof(_MMCamcorderGstElement) * sc->encode_element_num);
2529
2530         for (i = 0 ; i < sc->element_num ; i++) {
2531                 sc->element[i].id = _MMCAMCORDER_NONE;
2532                 sc->element[i].gst = NULL;
2533         }
2534
2535         for (i = 0 ; i < sc->encode_element_num ; i++) {
2536                 sc->encode_element[i].id = _MMCAMCORDER_NONE;
2537                 sc->encode_element[i].gst = NULL;
2538         }
2539
2540         sc->fourcc = 0x80000000;
2541         sc->frame_stability_count = 0;
2542         sc->drop_vframe = 0;
2543         sc->pass_first_vframe = 0;
2544         sc->is_modified_rate = FALSE;
2545
2546         return sc;
2547 }
2548
2549
2550 void _mmcamcorder_dealloc_subcontext(_MMCamcorderSubContext *sc)
2551 {
2552         MMCAM_LOG_INFO("");
2553
2554         if (sc) {
2555                 if (sc->element) {
2556                         MMCAM_LOG_INFO("release element");
2557                         g_free(sc->element);
2558                         sc->element = NULL;
2559                 }
2560
2561                 if (sc->encode_element) {
2562                         MMCAM_LOG_INFO("release encode_element");
2563                         g_free(sc->encode_element);
2564                         sc->encode_element = NULL;
2565                 }
2566
2567                 if (sc->info_image) {
2568                         MMCAM_LOG_INFO("release info_image");
2569                         g_free(sc->info_image);
2570                         sc->info_image = NULL;
2571                 }
2572
2573                 if (sc->info_video) {
2574                         MMCAM_LOG_INFO("release info_video");
2575                         SAFE_G_FREE(sc->info_video->filename);
2576                         g_mutex_clear(&sc->info_video->size_check_lock);
2577                         g_free(sc->info_video);
2578                         sc->info_video = NULL;
2579                 }
2580
2581                 if (sc->info_audio) {
2582                         MMCAM_LOG_INFO("release info_audio");
2583                         SAFE_G_FREE(sc->info_audio->filename);
2584                         g_free(sc->info_audio);
2585                         sc->info_audio = NULL;
2586                 }
2587
2588                 g_free(sc);
2589                 sc = NULL;
2590         }
2591
2592         return;
2593 }
2594
2595
2596 int _mmcamcorder_set_functions(MMHandleType handle, int type)
2597 {
2598         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
2599
2600         /*MMCAM_LOG_INFO("");*/
2601
2602         switch (type) {
2603         case MM_CAMCORDER_MODE_AUDIO:
2604                 hcamcorder->command = _mmcamcorder_audio_command;
2605                 break;
2606         case MM_CAMCORDER_MODE_VIDEO_CAPTURE:
2607         default:
2608                 hcamcorder->command = _mmcamcorder_video_capture_command;
2609                 break;
2610         }
2611
2612         return MM_ERROR_NONE;
2613 }
2614
2615
2616 gboolean _mmcamcorder_pipeline_cb_message(GstBus *bus, GstMessage *message, gpointer data)
2617 {
2618         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(data);
2619         _MMCamcorderSubContext *sc = NULL;
2620
2621         mmf_return_val_if_fail(hcamcorder, FALSE);
2622         mmf_return_val_if_fail(message, FALSE);
2623
2624         MMCAM_LOG_DEBUG("message type[0x%x]", GST_MESSAGE_TYPE(message));
2625
2626         switch (GST_MESSAGE_TYPE(message)) {
2627         case GST_MESSAGE_EOS:
2628         {
2629                 MMCAM_LOG_ERROR("Got EOS from element[%s]... but should not be reached here!",
2630                         GST_STR_NULL(GST_ELEMENT_NAME(GST_MESSAGE_SRC(message))));
2631                 break;
2632         }
2633         case GST_MESSAGE_ERROR:
2634         {
2635                 GError *err = NULL;
2636                 gchar *debug = NULL;
2637
2638                 gst_message_parse_error(message, &err, &debug);
2639
2640                 __mmcamcorder_handle_gst_error((MMHandleType)hcamcorder, message, err);
2641
2642                 if (err) {
2643                         MMCAM_LOG_ERROR("GSTERR: %s", err->message);
2644                         g_error_free(err);
2645                         err = NULL;
2646                 }
2647
2648                 if (debug) {
2649                         MMCAM_LOG_ERROR("Error Debug: %s", debug);
2650                         g_free(debug);
2651                         debug = NULL;
2652                 }
2653                 break;
2654         }
2655         case GST_MESSAGE_WARNING:
2656         {
2657                 GError *err;
2658                 gchar *debug;
2659                 gst_message_parse_warning(message, &err, &debug);
2660
2661                 MMCAM_LOG_WARNING("GSTWARN: %s", err->message);
2662
2663                 __mmcamcorder_handle_gst_warning((MMHandleType)hcamcorder, message, err);
2664
2665                 g_error_free(err);
2666                 g_free(debug);
2667                 break;
2668         }
2669         case GST_MESSAGE_STATE_CHANGED:
2670         {
2671                 const GValue *vnewstate;
2672                 GstState newstate;
2673                 GstElement *pipeline = NULL;
2674
2675                 sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
2676                 if ((sc) && (sc->element)) {
2677                         if (sc->element[_MMCAMCORDER_MAIN_PIPE].gst) {
2678                                 pipeline = sc->element[_MMCAMCORDER_MAIN_PIPE].gst;
2679                                 if (message->src == (GstObject*)pipeline) {
2680                                         vnewstate = gst_structure_get_value(gst_message_get_structure(message), "new-state");
2681                                         if (vnewstate) {
2682                                                 newstate = (GstState)vnewstate->data[0].v_int;
2683                                                 MMCAM_LOG_INFO("GST_MESSAGE_STATE_CHANGED[%s]", gst_element_state_get_name(newstate));
2684                                         } else {
2685                                                 MMCAM_LOG_WARNING("get new state failed from msg");
2686                                         }
2687                                 }
2688                         }
2689                 }
2690                 break;
2691         }
2692         case GST_MESSAGE_NEW_CLOCK:
2693         {
2694                 GstClock *pipe_clock = NULL;
2695                 gst_message_parse_new_clock(message, &pipe_clock);
2696                 MMCAM_LOG_INFO("GST_MESSAGE_NEW_CLOCK[%s]", (pipe_clock ? GST_OBJECT_NAME(pipe_clock) : "NULL"));
2697                 break;
2698         }
2699         default:
2700                 MMCAM_LOG_DEBUG("unhandled message type[0x%x]", GST_MESSAGE_TYPE(message));
2701                 break;
2702         }
2703
2704         return TRUE;
2705 }
2706
2707
2708 GstBusSyncReply _mmcamcorder_pipeline_bus_sync_callback(GstBus *bus, GstMessage *message, gpointer data)
2709 {
2710         GError *err = NULL;
2711         gchar *debug_info = NULL;
2712
2713         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(data);
2714         _MMCamcorderSubContext *sc = NULL;
2715
2716         mmf_return_val_if_fail(hcamcorder, GST_BUS_DROP);
2717         mmf_return_val_if_fail(message, GST_BUS_DROP);
2718
2719         sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
2720         mmf_return_val_if_fail(sc, GST_BUS_DROP);
2721
2722         if (GST_MESSAGE_TYPE(message) == GST_MESSAGE_ERROR) {
2723                 /* parse error message */
2724                 gst_message_parse_error(message, &err, &debug_info);
2725
2726                 if (debug_info) {
2727                         MMCAM_LOG_ERROR("GST ERROR : %s", debug_info);
2728                         g_free(debug_info);
2729                         debug_info = NULL;
2730                 }
2731
2732                 if (!err) {
2733                         MMCAM_LOG_WARNING("failed to parse error message");
2734                         return GST_BUS_PASS;
2735                 }
2736
2737                 /* check domain[RESOURCE] and element[VIDEOSRC] */
2738                 if (err->domain == GST_RESOURCE_ERROR &&
2739                         GST_ELEMENT_CAST(message->src) == GST_ELEMENT_CAST(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst)) {
2740                         switch (err->code) {
2741                         case GST_RESOURCE_ERROR_BUSY:
2742                                 MMCAM_LOG_ERROR("Camera device [busy]");
2743                                 hcamcorder->error_code = MM_ERROR_CAMCORDER_DEVICE_BUSY;
2744                                 break;
2745                         case GST_RESOURCE_ERROR_OPEN_WRITE:
2746                                 MMCAM_LOG_ERROR("Camera device [open failed]");
2747                                 hcamcorder->error_code = MM_ERROR_COMMON_INVALID_PERMISSION;
2748                                 break;
2749                         case GST_RESOURCE_ERROR_OPEN_READ_WRITE:
2750                                 MMCAM_LOG_ERROR("Camera device [open failed]");
2751                                 hcamcorder->error_code = MM_ERROR_CAMCORDER_DEVICE_OPEN;
2752                                 break;
2753                         case GST_RESOURCE_ERROR_OPEN_READ:
2754                                 MMCAM_LOG_ERROR("Camera device [register trouble]");
2755                                 hcamcorder->error_code = MM_ERROR_CAMCORDER_DEVICE_REG_TROUBLE;
2756                                 break;
2757                         case GST_RESOURCE_ERROR_NOT_FOUND:
2758                                 MMCAM_LOG_ERROR("Camera device [device not found]");
2759                                 hcamcorder->error_code = MM_ERROR_CAMCORDER_DEVICE_NOT_FOUND;
2760                                 break;
2761                         case GST_RESOURCE_ERROR_TOO_LAZY:
2762                                 MMCAM_LOG_ERROR("Camera device [timeout]");
2763                                 hcamcorder->error_code = MM_ERROR_CAMCORDER_DEVICE_TIMEOUT;
2764                                 break;
2765                         case GST_RESOURCE_ERROR_SETTINGS:
2766                                 MMCAM_LOG_ERROR("Camera device [not supported]");
2767                                 hcamcorder->error_code = MM_ERROR_CAMCORDER_NOT_SUPPORTED;
2768                                 break;
2769                         case GST_RESOURCE_ERROR_FAILED:
2770                                 MMCAM_LOG_ERROR("Camera device [working failed].");
2771                                 hcamcorder->error_code = MM_ERROR_CAMCORDER_DEVICE_IO;
2772                                 break;
2773                         default:
2774                                 MMCAM_LOG_ERROR("Camera device [General(%d)]", err->code);
2775                                 hcamcorder->error_code = MM_ERROR_CAMCORDER_DEVICE;
2776                                 break;
2777                         }
2778
2779                         hcamcorder->error_occurs = TRUE;
2780                 }
2781
2782                 g_error_free(err);
2783
2784                 /* store error code and drop this message if cmd is running */
2785                 if (hcamcorder->error_code != MM_ERROR_NONE) {
2786                         _MMCamcorderMsgItem msg;
2787
2788                         /* post error to application */
2789                         hcamcorder->error_occurs = TRUE;
2790                         msg.id = MM_MESSAGE_CAMCORDER_ERROR;
2791                         msg.param.code = hcamcorder->error_code;
2792                         _mmcamcorder_send_message((MMHandleType)hcamcorder, &msg);
2793                 }
2794         } else if (GST_MESSAGE_TYPE(message) == GST_MESSAGE_ELEMENT) {
2795                 _MMCamcorderMsgItem msg;
2796
2797                 if (gst_structure_has_name(gst_message_get_structure(message), "avsysvideosrc-AF") ||
2798                     gst_structure_has_name(gst_message_get_structure(message), "camerasrc-AF")) {
2799                         int focus_state = 0;
2800
2801                         gst_structure_get_int(gst_message_get_structure(message), "focus-state", &focus_state);
2802                         MMCAM_LOG_INFO("Focus State:%d", focus_state);
2803
2804                         msg.id = MM_MESSAGE_CAMCORDER_FOCUS_CHANGED;
2805                         msg.param.code = focus_state;
2806                         _mmcamcorder_send_message((MMHandleType)hcamcorder, &msg);
2807                 } else if (gst_structure_has_name(gst_message_get_structure(message), "camerasrc-HDR")) {
2808                         int progress = 0;
2809                         int status = 0;
2810
2811                         if (gst_structure_get_int(gst_message_get_structure(message), "progress", &progress)) {
2812                                 gst_structure_get_int(gst_message_get_structure(message), "status", &status);
2813                                 MMCAM_LOG_INFO("HDR progress %d percent, status %d", progress, status);
2814
2815                                 msg.id = MM_MESSAGE_CAMCORDER_HDR_PROGRESS;
2816                                 msg.param.code = progress;
2817                                 _mmcamcorder_send_message((MMHandleType)hcamcorder, &msg);
2818                         }
2819                 } else if (gst_structure_has_name(gst_message_get_structure(message), "camerasrc-FD")) {
2820                         int i = 0;
2821                         const GValue *g_value = gst_structure_get_value(gst_message_get_structure(message), "face-info");;
2822                         GstCameraControlFaceDetectInfo *fd_info = NULL;
2823                         MMCamFaceDetectInfo *cam_fd_info = NULL;
2824
2825                         if (g_value)
2826                                 fd_info = (GstCameraControlFaceDetectInfo *)g_value_get_pointer(g_value);
2827
2828                         if (fd_info == NULL) {
2829                                 MMCAM_LOG_WARNING("fd_info is NULL");
2830                                 gst_message_unref(message);
2831                                 return GST_BUS_DROP;
2832                         }
2833
2834                         cam_fd_info = (MMCamFaceDetectInfo *)g_malloc(sizeof(MMCamFaceDetectInfo));
2835
2836                         /* set total face count */
2837                         cam_fd_info->num_of_faces = fd_info->num_of_faces;
2838
2839                         if (cam_fd_info->num_of_faces > 0) {
2840                                 cam_fd_info->face_info = (MMCamFaceInfo *)g_malloc(sizeof(MMCamFaceInfo) * cam_fd_info->num_of_faces);
2841                                 MMCAM_LOG_DEBUG("face detection - count %d", cam_fd_info->num_of_faces);
2842                                 /* set information of each face */
2843                                 for (i = 0 ; i < cam_fd_info->num_of_faces ; i++) {
2844                                         cam_fd_info->face_info[i].id = fd_info->face_info[i].id;
2845                                         cam_fd_info->face_info[i].score = fd_info->face_info[i].score;
2846                                         cam_fd_info->face_info[i].rect.x = fd_info->face_info[i].rect.x;
2847                                         cam_fd_info->face_info[i].rect.y = fd_info->face_info[i].rect.y;
2848                                         cam_fd_info->face_info[i].rect.width = fd_info->face_info[i].rect.width;
2849                                         cam_fd_info->face_info[i].rect.height = fd_info->face_info[i].rect.height;
2850
2851                                         MMCAM_LOG_VERBOSE("    [%d] id %d, score %d, [%d,%d,%dx%d]",
2852                                                 i, fd_info->face_info[i].id,
2853                                                 fd_info->face_info[i].score,
2854                                                 fd_info->face_info[i].rect.x,
2855                                                 fd_info->face_info[i].rect.y,
2856                                                 fd_info->face_info[i].rect.width,
2857                                                 fd_info->face_info[i].rect.height);
2858                                 }
2859                         } else {
2860                                 cam_fd_info->face_info = NULL;
2861                         }
2862
2863                         /* send message  - cam_fd_info should be freed by application */
2864                         msg.id = MM_MESSAGE_CAMCORDER_FACE_DETECT_INFO;
2865                         msg.param.data = cam_fd_info;
2866                         msg.param.size = sizeof(MMCamFaceDetectInfo);
2867                         msg.param.code = 0;
2868
2869                         _mmcamcorder_send_message((MMHandleType)hcamcorder, &msg);
2870
2871                         /* free fd_info allocated by plugin */
2872                         free(fd_info);
2873                         fd_info = NULL;
2874                 } else if (gst_structure_has_name(gst_message_get_structure(message), "camerasrc-Capture")) {
2875                         int capture_done = FALSE;
2876
2877                         if (gst_structure_get_int(gst_message_get_structure(message), "capture-done", &capture_done)) {
2878                                 if (sc->info_image) {
2879                                         /* play capture sound */
2880                                         _mmcamcorder_sound_solo_play((MMHandleType)hcamcorder, _MMCAMCORDER_SAMPLE_SOUND_NAME_CAPTURE01, FALSE);
2881                                 }
2882                         }
2883                 } else {
2884                         _mmcamcorder_pipeline_cb_message(bus, message, data);
2885                 }
2886         } else {
2887                 _mmcamcorder_pipeline_cb_message(bus, message, data);
2888         }
2889
2890         gst_message_unref(message);
2891
2892         return GST_BUS_DROP;
2893 }
2894
2895
2896 static void __mmcamcorder_gst_handle_sync_audio_error(mmf_camcorder_t *hcamcorder, gint err_code)
2897 {
2898         _MMCamcorderMsgItem msg;
2899
2900         mmf_return_if_fail(hcamcorder);
2901
2902         switch (err_code) {
2903         case GST_RESOURCE_ERROR_OPEN_READ_WRITE:
2904         case GST_RESOURCE_ERROR_OPEN_WRITE:
2905                 MMCAM_LOG_ERROR("audio device [open failed]");
2906
2907                 /* post error to application */
2908                 hcamcorder->error_occurs = TRUE;
2909                 hcamcorder->error_code = MM_ERROR_COMMON_INVALID_PERMISSION;
2910
2911                 msg.id = MM_MESSAGE_CAMCORDER_ERROR;
2912                 msg.param.code = hcamcorder->error_code;
2913
2914                 _mmcamcorder_send_message((MMHandleType)hcamcorder, &msg);
2915                 break;
2916         default:
2917                 MMCAM_LOG_ERROR("unhandled error code[%d]", err_code);
2918                 break;
2919         }
2920 }
2921
2922
2923 static void __mmcamcorder_gst_handle_sync_others_error(mmf_camcorder_t *hcamcorder, gint err_code)
2924 {
2925         gboolean b_committing = FALSE;
2926         storage_state_e storage_state = STORAGE_STATE_UNMOUNTABLE;
2927         _MMCamcorderMsgItem msg;
2928         _MMCamcorderSubContext *sc = NULL;
2929
2930         mmf_return_if_fail(hcamcorder);
2931
2932         sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
2933         mmf_return_if_fail(sc);
2934
2935         if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
2936                 mmf_return_if_fail(sc->info_video);
2937                 b_committing = sc->info_video->b_committing;
2938         } else {
2939                 mmf_return_if_fail(sc->info_audio);
2940                 b_committing = sc->info_audio->b_committing;
2941         }
2942
2943         _MMCAMCORDER_LOCK(hcamcorder);
2944
2945         switch (err_code) {
2946         case GST_RESOURCE_ERROR_WRITE:
2947                 storage_get_state(hcamcorder->storage_info.id, &storage_state);
2948                 if (storage_state == STORAGE_STATE_REMOVED ||
2949                         storage_state == STORAGE_STATE_UNMOUNTABLE) {
2950                         MMCAM_LOG_ERROR("storage was removed! [storage state %d]", storage_state);
2951                         hcamcorder->error_code = MM_ERROR_OUT_OF_STORAGE;
2952                 } else {
2953                         MMCAM_LOG_ERROR("File write error, storage state %d", storage_state);
2954                         hcamcorder->error_code = MM_ERROR_FILE_WRITE;
2955                 }
2956
2957                 if (sc->ferror_send == FALSE) {
2958                         sc->ferror_send = TRUE;
2959                 } else {
2960                         MMCAM_LOG_ERROR("error was already sent");
2961                         _MMCAMCORDER_UNLOCK(hcamcorder);
2962                         return;
2963                 }
2964                 break;
2965         case GST_RESOURCE_ERROR_NO_SPACE_LEFT:
2966                 MMCAM_LOG_ERROR("No left space");
2967                 hcamcorder->error_code = MM_MESSAGE_CAMCORDER_NO_FREE_SPACE;
2968                 break;
2969         case GST_RESOURCE_ERROR_OPEN_WRITE:
2970                 MMCAM_LOG_ERROR("Out of storage");
2971                 hcamcorder->error_code = MM_ERROR_OUT_OF_STORAGE;
2972                 break;
2973         case GST_RESOURCE_ERROR_SEEK:
2974                 MMCAM_LOG_ERROR("File read(seek)");
2975                 hcamcorder->error_code = MM_ERROR_FILE_READ;
2976                 break;
2977         default:
2978                 MMCAM_LOG_ERROR("Resource error(%d)", err_code);
2979                 hcamcorder->error_code = MM_ERROR_CAMCORDER_GST_RESOURCE;
2980                 break;
2981         }
2982
2983         if (b_committing) {
2984                 _MMCAMCORDER_SIGNAL(hcamcorder);
2985                 _MMCAMCORDER_UNLOCK(hcamcorder);
2986         } else {
2987                 _MMCAMCORDER_UNLOCK(hcamcorder);
2988
2989                 msg.id = MM_MESSAGE_CAMCORDER_ERROR;
2990                 msg.param.code = hcamcorder->error_code;
2991
2992                 _mmcamcorder_send_message((MMHandleType)hcamcorder, &msg);
2993         }
2994 }
2995
2996
2997 static void __mmcamcorder_handle_gst_sync_error(mmf_camcorder_t *hcamcorder, GstMessage *message)
2998 {
2999         GError *err = NULL;
3000         gchar *debug_info = NULL;
3001         _MMCamcorderSubContext *sc = NULL;
3002
3003         mmf_return_if_fail(hcamcorder);
3004         mmf_return_if_fail(message);
3005
3006         sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
3007         mmf_return_if_fail(sc);
3008
3009         /* parse error message */
3010         gst_message_parse_error(message, &err, &debug_info);
3011
3012         if (debug_info) {
3013                 MMCAM_LOG_ERROR("GST ERROR : %s", debug_info);
3014                 g_free(debug_info);
3015                 debug_info = NULL;
3016         }
3017
3018         if (!err) {
3019                 MMCAM_LOG_ERROR("failed to parse error message");
3020                 return;
3021         }
3022
3023         /* check domain[RESOURCE] and element[AUDIOSRC] */
3024         if (err->domain == GST_RESOURCE_ERROR &&
3025                 GST_ELEMENT_CAST(message->src) == GST_ELEMENT_CAST(sc->encode_element[_MMCAMCORDER_AUDIOSRC_SRC].gst))
3026                 __mmcamcorder_gst_handle_sync_audio_error(hcamcorder, err->code);
3027         else
3028                 __mmcamcorder_gst_handle_sync_others_error(hcamcorder, err->code);
3029
3030         g_error_free(err);
3031 }
3032
3033
3034 GstBusSyncReply _mmcamcorder_encode_pipeline_bus_sync_callback(GstBus *bus, GstMessage *message, gpointer data)
3035 {
3036         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(data);
3037         _MMCamcorderSubContext *sc = NULL;
3038
3039         mmf_return_val_if_fail(hcamcorder, GST_BUS_DROP);
3040         mmf_return_val_if_fail(message, GST_BUS_DROP);
3041
3042         sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
3043         mmf_return_val_if_fail(sc, GST_BUS_DROP);
3044
3045         if (GST_MESSAGE_TYPE(message) == GST_MESSAGE_EOS) {
3046                 MMCAM_LOG_INFO("got EOS from pipeline");
3047
3048                 _MMCAMCORDER_LOCK(hcamcorder);
3049
3050                 sc->bget_eos = TRUE;
3051                 _MMCAMCORDER_SIGNAL(hcamcorder);
3052
3053                 _MMCAMCORDER_UNLOCK(hcamcorder);
3054         } else if (GST_MESSAGE_TYPE(message) == GST_MESSAGE_ERROR) {
3055                 __mmcamcorder_handle_gst_sync_error(hcamcorder, message);
3056         } else {
3057                 _mmcamcorder_pipeline_cb_message(bus, message, data);
3058         }
3059
3060         gst_message_unref(message);
3061
3062         return GST_BUS_DROP;
3063 }
3064
3065
3066 void _mmcamcorder_dpm_camera_policy_changed_cb(const char *name, const char *value, void *user_data)
3067 {
3068         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(user_data);
3069         int current_state = MM_CAMCORDER_STATE_NONE;
3070
3071         mmf_return_if_fail(hcamcorder);
3072         mmf_return_if_fail(value);
3073
3074         current_state = _mmcamcorder_get_state((MMHandleType)hcamcorder);
3075         if (current_state <= MM_CAMCORDER_STATE_NONE || current_state >= MM_CAMCORDER_STATE_NUM) {
3076                 MMCAM_LOG_ERROR("Abnormal state. Or null handle. (%p, %d)", hcamcorder, current_state);
3077                 return;
3078         }
3079
3080         MMCAM_LOG_WARNING("camera policy [%s], current state [%d]", value, current_state);
3081
3082         if (!strcmp(value, "disallowed")) {
3083                 _MMCAMCORDER_LOCK_INTERRUPT(hcamcorder);
3084
3085                 __mmcamcorder_force_stop(hcamcorder, _MMCAMCORDER_STATE_CHANGE_BY_DPM);
3086
3087                 _MMCAMCORDER_UNLOCK_INTERRUPT(hcamcorder);
3088
3089                 _mmcamcorder_request_dpm_popup(hcamcorder->gdbus_conn, "camera");
3090         }
3091
3092         MMCAM_LOG_WARNING("done");
3093
3094         return;
3095 }
3096
3097
3098 int _mmcamcorder_create_pipeline(MMHandleType handle, int type)
3099 {
3100         int ret = MM_ERROR_NONE;
3101         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
3102         _MMCamcorderSubContext *sc = NULL;
3103         GstElement *pipeline = NULL;
3104
3105         MMCAM_LOG_INFO("handle : %p, type : %d", handle, type);
3106
3107         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
3108
3109         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
3110         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
3111
3112         switch (type) {
3113         case MM_CAMCORDER_MODE_AUDIO:
3114                 ret = _mmcamcorder_create_audio_pipeline(handle);
3115                 if (ret != MM_ERROR_NONE)
3116                         return ret;
3117
3118                 break;
3119         case MM_CAMCORDER_MODE_VIDEO_CAPTURE:
3120                 /* fall through */
3121         default:
3122                 ret = _mmcamcorder_create_preview_pipeline(handle);
3123                 if (ret != MM_ERROR_NONE)
3124                         return ret;
3125
3126                 if (hcamcorder->capture_mode == MM_CAMCORDER_CAPTURE_MODE_CAMERA_CONTROL) {
3127                         ret = _mmcamcorder_prepare_capture_mode_camera_control(handle);
3128                         if (ret != MM_ERROR_NONE)
3129                                 return ret;
3130                 } else if (hcamcorder->capture_mode == MM_CAMCORDER_CAPTURE_MODE_IMAGE_PAD) {
3131                         ret = _mmcamcorder_prepare_capture_mode_image_pad(handle);
3132                         if (ret != MM_ERROR_NONE)
3133                                 return ret;
3134                 }
3135
3136                 if (hcamcorder->support_extra_preview &&
3137                         _mmcamcorder_initialize_extra_preview_stream(handle) != MM_ERROR_NONE)
3138                         MMCAM_LOG_WARNING("connect extra preview stream signal failed");
3139                 break;
3140         }
3141
3142         pipeline = sc->element[_MMCAMCORDER_MAIN_PIPE].gst;
3143         if (type != MM_CAMCORDER_MODE_AUDIO) {
3144                 traceBegin(TTRACE_TAG_CAMERA, "MMCAMCORDER:REALIZE:SET_READY_TO_PIPELINE");
3145
3146                 ret = _mmcamcorder_gst_set_state(handle, pipeline, GST_STATE_READY);
3147
3148                 traceEnd(TTRACE_TAG_CAMERA);
3149         }
3150 #ifdef _MMCAMCORDER_GET_DEVICE_INFO
3151         if (!_mmcamcorder_get_device_info(handle))
3152                 MMCAM_LOG_ERROR("Getting device information error!!");
3153 #endif
3154
3155         MMCAM_LOG_INFO("ret[%x]", ret);
3156         if (ret != MM_ERROR_NONE) {
3157                 MMCAM_LOG_ERROR("error : destroy pipeline");
3158                 _mmcamcorder_destroy_pipeline(handle, hcamcorder->type);
3159         }
3160
3161         return ret;
3162 }
3163
3164
3165 void _mmcamcorder_destroy_pipeline(MMHandleType handle, int type)
3166 {
3167         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
3168         _MMCamcorderSubContext *sc = NULL;
3169         gint i = 0;
3170         int element_num = 0;
3171         _MMCamcorderGstElement *element = NULL;
3172
3173         mmf_return_if_fail(hcamcorder);
3174
3175         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
3176         mmf_return_if_fail(sc);
3177
3178         MMCAM_LOG_INFO("");
3179
3180         /* Inside each pipeline destroy function, Set GST_STATE_NULL to Main pipeline */
3181         switch (type) {
3182         case MM_CAMCORDER_MODE_VIDEO_CAPTURE:
3183                 element = sc->element;
3184                 element_num = sc->element_num;
3185                 _mmcamcorder_destroy_video_capture_pipeline(handle);
3186                 break;
3187         case MM_CAMCORDER_MODE_AUDIO:
3188                 element = sc->encode_element;
3189                 element_num = sc->encode_element_num;
3190                 _mmcamcorder_destroy_audio_pipeline(handle);
3191                 break;
3192         default:
3193                 MMCAM_LOG_ERROR("unknown type %d", type);
3194                 break;
3195         }
3196
3197         MMCAM_LOG_INFO("Pipeline clear!!");
3198
3199         /* checking unreleased element */
3200         for (i = 0 ; i < element_num ; i++) {
3201                 if (element[i].gst) {
3202                         if (GST_IS_ELEMENT(element[i].gst)) {
3203                                 MMCAM_LOG_WARNING("Still alive element - ID[%d], name [%s], ref count[%d], status[%s]",
3204                                         element[i].id,
3205                                         GST_OBJECT_NAME(element[i].gst),
3206                                         GST_OBJECT_REFCOUNT(element[i].gst),
3207                                         gst_element_state_get_name(GST_STATE(element[i].gst)));
3208                                 g_object_weak_unref(G_OBJECT(element[i].gst), (GWeakNotify)_mmcamcorder_element_release_noti, sc);
3209                         } else {
3210                                 MMCAM_LOG_WARNING("The element[%d] is still alive, check it", element[i].id);
3211                         }
3212
3213                         element[i].id = _MMCAMCORDER_NONE;
3214                         element[i].gst = NULL;
3215                 }
3216         }
3217
3218         return;
3219 }
3220
3221
3222 int _mmcamcorder_gst_set_state(MMHandleType handle, GstElement *pipeline, GstState target_state)
3223 {
3224         unsigned int k = 0;
3225         _MMCamcorderSubContext *sc = NULL;
3226         GstState pipeline_state = GST_STATE_VOID_PENDING;
3227         GstStateChangeReturn setChangeReturn = GST_STATE_CHANGE_FAILURE;
3228         GstStateChangeReturn getChangeReturn = GST_STATE_CHANGE_FAILURE;
3229         GstClockTime get_timeout = __MMCAMCORDER_SET_GST_STATE_TIMEOUT * GST_SECOND;
3230         GMutex *state_lock = NULL;
3231
3232         mmf_return_val_if_fail(handle, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
3233         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
3234         mmf_return_val_if_fail(sc && sc->element, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
3235
3236         if (sc->element[_MMCAMCORDER_MAIN_PIPE].gst == pipeline) {
3237                 MMCAM_LOG_INFO("Set state to %d - PREVIEW PIPELINE", target_state);
3238                 state_lock = &_MMCAMCORDER_GET_GST_STATE_LOCK(handle);
3239         } else {
3240                 MMCAM_LOG_INFO("Set state to %d - ENDODE PIPELINE", target_state);
3241                 state_lock = &_MMCAMCORDER_GET_GST_ENCODE_STATE_LOCK(handle);
3242         }
3243
3244         g_mutex_lock(state_lock);
3245
3246         for (k = 0; k < _MMCAMCORDER_STATE_SET_COUNT; k++) {
3247                 setChangeReturn = gst_element_set_state(pipeline, target_state);
3248                 MMCAM_LOG_INFO("gst_element_set_state[%d] return %d", target_state, setChangeReturn);
3249
3250                 if (setChangeReturn != GST_STATE_CHANGE_FAILURE) {
3251                         getChangeReturn = gst_element_get_state(pipeline, &pipeline_state, NULL, get_timeout);
3252                         switch (getChangeReturn) {
3253                         case GST_STATE_CHANGE_NO_PREROLL:
3254                                 MMCAM_LOG_INFO("status=GST_STATE_CHANGE_NO_PREROLL.");
3255                                 /* fall through */
3256                         case GST_STATE_CHANGE_SUCCESS:
3257                                 /* if we reached the final target state, exit */
3258                                 if (pipeline_state == target_state) {
3259                                         MMCAM_LOG_INFO("Set state to %d - DONE", target_state);
3260                                         g_mutex_unlock(state_lock);
3261                                         return MM_ERROR_NONE;
3262                                 }
3263                                 break;
3264                         case GST_STATE_CHANGE_ASYNC:
3265                                 MMCAM_LOG_INFO("status=GST_STATE_CHANGE_ASYNC.");
3266                                 break;
3267                         default:
3268                                 g_mutex_unlock(state_lock);
3269                                 MMCAM_LOG_INFO("status=GST_STATE_CHANGE_FAILURE.");
3270                                 return MM_ERROR_CAMCORDER_GST_STATECHANGE;
3271                         }
3272
3273                         g_mutex_unlock(state_lock);
3274
3275                         MMCAM_LOG_ERROR("timeout of gst_element_get_state()!!");
3276
3277                         return MM_ERROR_CAMCORDER_RESPONSE_TIMEOUT;
3278                 }
3279
3280                 usleep(_MMCAMCORDER_STATE_CHECK_INTERVAL);
3281         }
3282
3283         g_mutex_unlock(state_lock);
3284
3285         MMCAM_LOG_ERROR("Failure. gst_element_set_state timeout!!");
3286
3287         return MM_ERROR_CAMCORDER_RESPONSE_TIMEOUT;
3288 }
3289
3290
3291 /* For performance check */
3292 int _mmcamcorder_video_current_framerate(MMHandleType handle)
3293 {
3294         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
3295         _MMCamcorderSubContext *sc = NULL;
3296
3297         mmf_return_val_if_fail(hcamcorder, -1);
3298
3299         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
3300         mmf_return_val_if_fail(sc, -1);
3301
3302         return sc->kpi.current_fps;
3303 }
3304
3305
3306 int _mmcamcorder_video_average_framerate(MMHandleType handle)
3307 {
3308         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
3309         _MMCamcorderSubContext *sc = NULL;
3310
3311         mmf_return_val_if_fail(hcamcorder, -1);
3312
3313         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
3314         mmf_return_val_if_fail(sc, -1);
3315
3316         return sc->kpi.average_fps;
3317 }
3318
3319
3320 void _mmcamcorder_video_current_framerate_init(MMHandleType handle)
3321 {
3322         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
3323         _MMCamcorderSubContext *sc = NULL;
3324
3325         mmf_return_if_fail(hcamcorder);
3326
3327         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
3328         mmf_return_if_fail(sc);
3329
3330         memset(&(sc->kpi), 0x00, sizeof(_MMCamcorderKPIMeasure));
3331
3332         return;
3333 }
3334
3335
3336 void __mmcamcorder_force_stop(mmf_camcorder_t *hcamcorder, int state_change_by_system)
3337 {
3338         int i = 0;
3339         int loop = 0;
3340         int itr_cnt = 0;
3341         int result = MM_ERROR_NONE;
3342         int cmd_try_count = 0;
3343         int current_state = MM_CAMCORDER_STATE_NONE;
3344         _MMCamcorderMsgItem msg;
3345
3346         mmf_return_if_fail(hcamcorder);
3347
3348         /* check command running */
3349         do {
3350                 if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
3351                         if (cmd_try_count++ < __MMCAMCORDER_FORCE_STOP_TRY_COUNT) {
3352                                 MMCAM_LOG_WARNING("Another command is running. try again after %d ms", __MMCAMCORDER_FORCE_STOP_WAIT_TIME/1000);
3353                                 usleep(__MMCAMCORDER_FORCE_STOP_WAIT_TIME);
3354                         } else {
3355                                 MMCAM_LOG_ERROR("wait timeout");
3356                                 break;
3357                         }
3358                 } else {
3359                         _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
3360                         break;
3361                 }
3362         } while (TRUE);
3363
3364         current_state = _mmcamcorder_get_state((MMHandleType)hcamcorder);
3365
3366         MMCAM_LOG_WARNING("Force STOP MMFW Camcorder by %d", state_change_by_system);
3367
3368         /* set state_change_by_system for state change message */
3369         hcamcorder->state_change_by_system = state_change_by_system;
3370
3371         if (current_state >= MM_CAMCORDER_STATE_READY) {
3372                 MMCAM_LOG_WARNING("send state change started message to notify");
3373
3374                 memset(&msg, 0x0, sizeof(_MMCamcorderMsgItem));
3375
3376                 switch (state_change_by_system) {
3377                 case _MMCAMCORDER_STATE_CHANGE_BY_RM:
3378                         msg.id = MM_MESSAGE_CAMCORDER_STATE_CHANGE_STARTED_BY_RM;
3379                         break;
3380                 case _MMCAMCORDER_STATE_CHANGE_BY_DPM:
3381                         msg.id = MM_MESSAGE_CAMCORDER_STATE_CHANGE_STARTED_BY_SECURITY;
3382                         break;
3383                 default:
3384                         break;
3385                 }
3386
3387                 if (msg.id != 0) {
3388                         msg.param.state.code = hcamcorder->interrupt_code;
3389                         _mmcamcorder_send_message((MMHandleType)hcamcorder, &msg);
3390                 } else {
3391                         MMCAM_LOG_ERROR("should not be reached here %d", state_change_by_system);
3392                 }
3393         }
3394
3395         for (loop = 0 ; current_state > MM_CAMCORDER_STATE_NULL && loop < __MMCAMCORDER_CMD_ITERATE_MAX * 3 ; loop++) {
3396                 itr_cnt = __MMCAMCORDER_CMD_ITERATE_MAX;
3397                 switch (current_state) {
3398                 case MM_CAMCORDER_STATE_CAPTURING:
3399                 {
3400                         _MMCamcorderSubContext *sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
3401                         _MMCamcorderImageInfo *info = NULL;
3402
3403                         mmf_return_if_fail(sc);
3404                         mmf_return_if_fail((info = sc->info_image));
3405
3406                         MMCAM_LOG_WARNING("Stop capturing.");
3407
3408                         /* if capturing isn't finished, waiting for 2 sec to finish real capture operation. check 'info->capturing'. */
3409                         mm_camcorder_set_attributes((MMHandleType)hcamcorder, NULL,
3410                                 MMCAM_CAPTURE_BREAK_CONTINUOUS_SHOT, TRUE,
3411                                 NULL);
3412
3413                         for (i = 0; i < 20 && info->capturing; i++)
3414                                 usleep(100000);
3415
3416                         if (i == 20)
3417                                 MMCAM_LOG_ERROR("Timeout. Can't check stop capturing.");
3418
3419                         while ((itr_cnt--) && ((result = _mmcamcorder_capture_stop((MMHandleType)hcamcorder)) != MM_ERROR_NONE))
3420                                 MMCAM_LOG_WARNING("Can't stop capturing.(%x)", result);
3421
3422                         break;
3423                 }
3424                 case MM_CAMCORDER_STATE_RECORDING:
3425                 case MM_CAMCORDER_STATE_PAUSED:
3426                 {
3427                         MMCAM_LOG_WARNING("Stop recording.");
3428
3429                         while ((itr_cnt--) && ((result = _mmcamcorder_commit((MMHandleType)hcamcorder)) != MM_ERROR_NONE)) {
3430                                 MMCAM_LOG_WARNING("Can't commit.(%x), cancel it.", result);
3431                                 _mmcamcorder_cancel((MMHandleType)hcamcorder);
3432                         }
3433
3434                         break;
3435                 }
3436                 case MM_CAMCORDER_STATE_PREPARE:
3437                 {
3438                         MMCAM_LOG_WARNING("Stop preview.");
3439
3440                         while ((itr_cnt--) && ((result = _mmcamcorder_stop((MMHandleType)hcamcorder)) != MM_ERROR_NONE))
3441                                 MMCAM_LOG_WARNING("Can't stop preview.(%x)", result);
3442
3443                         break;
3444                 }
3445                 case MM_CAMCORDER_STATE_READY:
3446                 {
3447                         MMCAM_LOG_WARNING("unrealize");
3448
3449                         if ((result = _mmcamcorder_unrealize((MMHandleType)hcamcorder)) != MM_ERROR_NONE)
3450                                 MMCAM_LOG_WARNING("Can't unrealize.(%x)", result);
3451
3452                         break;
3453                 }
3454                 default:
3455                         MMCAM_LOG_WARNING("Already stopped.");
3456                         break;
3457                 }
3458
3459                 current_state = _mmcamcorder_get_state((MMHandleType)hcamcorder);
3460         }
3461
3462         /* restore */
3463         hcamcorder->state_change_by_system = _MMCAMCORDER_STATE_CHANGE_NORMAL;
3464
3465         MMCAM_LOG_WARNING("Done.");
3466
3467         return;
3468 }
3469
3470
3471 static gboolean __mmcamcorder_handle_gst_error(MMHandleType handle, GstMessage *message, GError *error)
3472 {
3473         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
3474         _MMCamcorderMsgItem msg;
3475         gchar *msg_src_element = NULL;
3476         _MMCamcorderSubContext *sc = NULL;
3477
3478         mmf_return_val_if_fail(hcamcorder, FALSE);
3479         mmf_return_val_if_fail(error, FALSE);
3480
3481         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
3482         mmf_return_val_if_fail(sc, FALSE);
3483
3484         MMCAM_LOG_INFO("error code %d", error->code);
3485
3486         mmf_return_val_if_fail(hcamcorder->state >= MM_CAMCORDER_STATE_READY, FALSE);
3487
3488         /* filtering filesink related errors */
3489         if (hcamcorder->state == MM_CAMCORDER_STATE_RECORDING &&
3490             (error->code == GST_RESOURCE_ERROR_WRITE || error->code == GST_RESOURCE_ERROR_SEEK)) {
3491                 if (sc->ferror_count == 2 && sc->ferror_send == FALSE) {
3492                         sc->ferror_send = TRUE;
3493                         msg.param.code = __mmcamcorder_gst_handle_resource_error(handle, error->code, message);
3494                 } else {
3495                         sc->ferror_count++;
3496                         MMCAM_LOG_WARNING("Skip error");
3497                         return TRUE;
3498                 }
3499         }
3500
3501         if (error->domain == GST_CORE_ERROR) {
3502                 msg.param.code = __mmcamcorder_gst_handle_core_error(handle, error->code, message);
3503         } else if (error->domain == GST_LIBRARY_ERROR) {
3504                 msg.param.code = __mmcamcorder_gst_handle_library_error(handle, error->code, message);
3505         } else if (error->domain == GST_RESOURCE_ERROR) {
3506                 msg.param.code = __mmcamcorder_gst_handle_resource_error(handle, error->code, message);
3507         } else if (error->domain == GST_STREAM_ERROR) {
3508                 msg.param.code = __mmcamcorder_gst_handle_stream_error(handle, error->code, message);
3509         } else {
3510                 MMCAM_LOG_WARNING("This error domain is not defined.");
3511
3512                 /* we treat system error as an internal error */
3513                 msg.param.code = MM_ERROR_CAMCORDER_INTERNAL;
3514         }
3515
3516         if (message->src) {
3517                 msg_src_element = GST_ELEMENT_NAME(GST_ELEMENT_CAST(message->src));
3518                 MMCAM_LOG_ERROR("-Msg src : [%s] Domain : [%s]   Error : [%s]  Code : [%d] is tranlated to error code : [0x%x]",
3519                         msg_src_element, g_quark_to_string(error->domain), error->message, error->code, msg.param.code);
3520         } else {
3521                 MMCAM_LOG_ERROR("Domain : [%s]   Error : [%s]  Code : [%d] is tranlated to error code : [0x%x]",
3522                         g_quark_to_string(error->domain), error->message, error->code, msg.param.code);
3523         }
3524
3525 #ifdef _MMCAMCORDER_SKIP_GST_FLOW_ERROR
3526         /* Check whether send this error to application */
3527         if (msg.param.code == MM_ERROR_CAMCORDER_GST_FLOW_ERROR) {
3528                 MMCAM_LOG_INFO("We got the error. But skip it.");
3529                 return TRUE;
3530         }
3531 #endif /* _MMCAMCORDER_SKIP_GST_FLOW_ERROR */
3532
3533         /* post error to application except RESTRICTED case */
3534         if (msg.param.code != MM_ERROR_POLICY_RESTRICTED) {
3535                 hcamcorder->error_occurs = TRUE;
3536                 msg.id = MM_MESSAGE_CAMCORDER_ERROR;
3537                 _mmcamcorder_send_message(handle, &msg);
3538         }
3539
3540         return TRUE;
3541 }
3542
3543
3544 static gint __mmcamcorder_gst_handle_core_error(MMHandleType handle, int code, GstMessage *message)
3545 {
3546         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
3547         _MMCamcorderSubContext *sc = NULL;
3548         GstElement *element = NULL;
3549
3550         MMCAM_LOG_INFO("");
3551
3552         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
3553
3554         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
3555         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
3556
3557         /* Specific plugin - video encoder plugin */
3558         element = GST_ELEMENT_CAST(sc->encode_element[_MMCAMCORDER_ENCSINK_VENC].gst);
3559
3560         if (GST_ELEMENT_CAST(message->src) == element) {
3561                 if (code == GST_CORE_ERROR_NEGOTIATION)
3562                         return MM_ERROR_CAMCORDER_GST_NEGOTIATION;
3563                 else
3564                         return MM_ERROR_CAMCORDER_ENCODER;
3565         }
3566
3567         /* General */
3568         switch (code) {
3569         case GST_CORE_ERROR_STATE_CHANGE:
3570                 return MM_ERROR_CAMCORDER_GST_STATECHANGE;
3571         case GST_CORE_ERROR_NEGOTIATION:
3572                 return MM_ERROR_CAMCORDER_GST_NEGOTIATION;
3573         case GST_CORE_ERROR_MISSING_PLUGIN:
3574         case GST_CORE_ERROR_SEEK:
3575         case GST_CORE_ERROR_NOT_IMPLEMENTED:
3576         case GST_CORE_ERROR_FAILED:
3577         case GST_CORE_ERROR_TOO_LAZY:
3578         case GST_CORE_ERROR_PAD:
3579         case GST_CORE_ERROR_THREAD:
3580         case GST_CORE_ERROR_EVENT:
3581         case GST_CORE_ERROR_CAPS:
3582         case GST_CORE_ERROR_TAG:
3583         case GST_CORE_ERROR_CLOCK:
3584         case GST_CORE_ERROR_DISABLED:
3585         default:
3586                 return MM_ERROR_CAMCORDER_GST_CORE;
3587         break;
3588         }
3589 }
3590
3591 static gint __mmcamcorder_gst_handle_library_error(MMHandleType handle, int code, GstMessage *message)
3592 {
3593         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
3594         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
3595
3596         MMCAM_LOG_INFO("");
3597
3598         /* Specific plugin - NONE */
3599
3600         /* General */
3601         switch (code) {
3602         case GST_LIBRARY_ERROR_FAILED:
3603         case GST_LIBRARY_ERROR_TOO_LAZY:
3604         case GST_LIBRARY_ERROR_INIT:
3605         case GST_LIBRARY_ERROR_SHUTDOWN:
3606         case GST_LIBRARY_ERROR_SETTINGS:
3607         case GST_LIBRARY_ERROR_ENCODE:
3608         default:
3609                 MMCAM_LOG_ERROR("Library error(%d)", code);
3610                 return MM_ERROR_CAMCORDER_GST_LIBRARY;
3611         }
3612 }
3613
3614
3615 static gint __mmcamcorder_gst_handle_resource_error(MMHandleType handle, int code, GstMessage *message)
3616 {
3617         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
3618         _MMCamcorderSubContext *sc = NULL;
3619         GstElement *element = NULL;
3620
3621         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
3622
3623         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
3624         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
3625
3626         MMCAM_LOG_INFO("");
3627
3628         /* Specific plugin */
3629         /* video sink */
3630         element = GST_ELEMENT_CAST(sc->element[_MMCAMCORDER_VIDEOSINK_SINK].gst);
3631         if (GST_ELEMENT_CAST(message->src) == element) {
3632                 if (code == GST_RESOURCE_ERROR_WRITE) {
3633                         MMCAM_LOG_ERROR("Display device [Off]");
3634                         return MM_ERROR_CAMCORDER_DISPLAY_DEVICE_OFF;
3635                 } else {
3636                         MMCAM_LOG_ERROR("Display device [General(%d)]", code);
3637                 }
3638         }
3639
3640         /* audiosrc */
3641         element = GST_ELEMENT_CAST(sc->encode_element[_MMCAMCORDER_AUDIOSRC_SRC].gst);
3642         if (GST_ELEMENT_CAST(message->src) == element) {
3643                 if (code == GST_RESOURCE_ERROR_FAILED) {
3644                         int ret = MM_ERROR_NONE;
3645                         int current_state = MM_CAMCORDER_STATE_NONE;
3646
3647                         MMCAM_LOG_ERROR("DPM mic DISALLOWED - current state %d", current_state);
3648
3649                         _MMCAMCORDER_LOCK_INTERRUPT(hcamcorder);
3650
3651                         current_state = _mmcamcorder_get_state((MMHandleType)hcamcorder);
3652                         if (current_state >= MM_CAMCORDER_STATE_RECORDING) {
3653                                 /* set value to inform a status is changed by DPM */
3654                                 hcamcorder->state_change_by_system = _MMCAMCORDER_STATE_CHANGE_BY_DPM;
3655
3656                                 ret = _mmcamcorder_commit((MMHandleType)hcamcorder);
3657                                 MMCAM_LOG_INFO("commit result : 0x%x", ret);
3658
3659                                 if (ret != MM_ERROR_NONE) {
3660                                         MMCAM_LOG_ERROR("commit failed, cancel it");
3661                                         ret = _mmcamcorder_cancel((MMHandleType)hcamcorder);
3662                                 }
3663                         }
3664
3665                         /* restore value */
3666                         hcamcorder->state_change_by_system = _MMCAMCORDER_STATE_CHANGE_NORMAL;
3667
3668                         _MMCAMCORDER_UNLOCK_INTERRUPT(hcamcorder);
3669
3670                         _mmcamcorder_request_dpm_popup(hcamcorder->gdbus_conn, "microphone");
3671
3672                         return MM_ERROR_POLICY_RESTRICTED;
3673                 }
3674         }
3675
3676         /* encodebin */
3677         element = GST_ELEMENT_CAST(sc->encode_element[_MMCAMCORDER_ENCSINK_VENC].gst);
3678         if (GST_ELEMENT_CAST(message->src) == element) {
3679                 if (code == GST_RESOURCE_ERROR_FAILED) {
3680                         MMCAM_LOG_ERROR("Encoder [Resource error]");
3681                         return MM_ERROR_CAMCORDER_ENCODER_BUFFER;
3682                 } else {
3683                         MMCAM_LOG_ERROR("Encoder [General(%d)]", code);
3684                         return MM_ERROR_CAMCORDER_ENCODER;
3685                 }
3686         }
3687
3688         /* General */
3689         switch (code) {
3690         case GST_RESOURCE_ERROR_WRITE:
3691                 MMCAM_LOG_ERROR("File write error");
3692                 return MM_ERROR_FILE_WRITE;
3693         case GST_RESOURCE_ERROR_NO_SPACE_LEFT:
3694                 MMCAM_LOG_ERROR("No left space");
3695                 return MM_MESSAGE_CAMCORDER_NO_FREE_SPACE;
3696         case GST_RESOURCE_ERROR_OPEN_WRITE:
3697                 MMCAM_LOG_ERROR("Out of storage");
3698                 return MM_ERROR_OUT_OF_STORAGE;
3699         case GST_RESOURCE_ERROR_SEEK:
3700                 MMCAM_LOG_ERROR("File read(seek)");
3701                 return MM_ERROR_FILE_READ;
3702         case GST_RESOURCE_ERROR_NOT_FOUND:
3703         case GST_RESOURCE_ERROR_FAILED:
3704         case GST_RESOURCE_ERROR_TOO_LAZY:
3705         case GST_RESOURCE_ERROR_BUSY:
3706         case GST_RESOURCE_ERROR_OPEN_READ:
3707         case GST_RESOURCE_ERROR_OPEN_READ_WRITE:
3708         case GST_RESOURCE_ERROR_CLOSE:
3709         case GST_RESOURCE_ERROR_READ:
3710         case GST_RESOURCE_ERROR_SYNC:
3711         case GST_RESOURCE_ERROR_SETTINGS:
3712         default:
3713                 MMCAM_LOG_ERROR("Resource error(%d)", code);
3714                 return MM_ERROR_CAMCORDER_GST_RESOURCE;
3715         }
3716 }
3717
3718
3719 static gint __mmcamcorder_gst_handle_stream_error(MMHandleType handle, int code, GstMessage *message)
3720 {
3721         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
3722         _MMCamcorderSubContext *sc = NULL;
3723         GstElement *element = NULL;
3724
3725         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
3726
3727         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
3728         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
3729
3730         MMCAM_LOG_INFO("");
3731
3732         /* Specific plugin */
3733         /* video encoder */
3734         element = GST_ELEMENT_CAST(sc->encode_element[_MMCAMCORDER_ENCSINK_VENC].gst);
3735         if (GST_ELEMENT_CAST(message->src) == element) {
3736                 switch (code) {
3737                 case GST_STREAM_ERROR_WRONG_TYPE:
3738                         MMCAM_LOG_ERROR("Video encoder [wrong stream type]");
3739                         return MM_ERROR_CAMCORDER_ENCODER_WRONG_TYPE;
3740                 case GST_STREAM_ERROR_ENCODE:
3741                         MMCAM_LOG_ERROR("Video encoder [encode error]");
3742                         return MM_ERROR_CAMCORDER_ENCODER_WORKING;
3743                 case GST_STREAM_ERROR_FAILED:
3744                         MMCAM_LOG_ERROR("Video encoder [stream failed]");
3745                         return MM_ERROR_CAMCORDER_ENCODER_WORKING;
3746                 default:
3747                         MMCAM_LOG_ERROR("Video encoder [General(%d)]", code);
3748                         return MM_ERROR_CAMCORDER_ENCODER;
3749                 }
3750         }
3751
3752         /* General plugin */
3753         switch (code) {
3754         case GST_STREAM_ERROR_FORMAT:
3755                 MMCAM_LOG_ERROR("General [negotiation error(%d)]", code);
3756                 return MM_ERROR_CAMCORDER_GST_NEGOTIATION;
3757         case GST_STREAM_ERROR_FAILED:
3758                 MMCAM_LOG_ERROR("General [flow error(%d)]", code);
3759                 return MM_ERROR_CAMCORDER_GST_FLOW_ERROR;
3760         case GST_STREAM_ERROR_TYPE_NOT_FOUND:
3761         case GST_STREAM_ERROR_DECODE:
3762         case GST_STREAM_ERROR_CODEC_NOT_FOUND:
3763         case GST_STREAM_ERROR_NOT_IMPLEMENTED:
3764         case GST_STREAM_ERROR_TOO_LAZY:
3765         case GST_STREAM_ERROR_ENCODE:
3766         case GST_STREAM_ERROR_DEMUX:
3767         case GST_STREAM_ERROR_MUX:
3768         case GST_STREAM_ERROR_DECRYPT:
3769         case GST_STREAM_ERROR_DECRYPT_NOKEY:
3770         case GST_STREAM_ERROR_WRONG_TYPE:
3771         default:
3772                 MMCAM_LOG_ERROR("General [error(%d)]", code);
3773                 return MM_ERROR_CAMCORDER_GST_STREAM;
3774         }
3775 }
3776
3777
3778 static gboolean __mmcamcorder_handle_gst_warning(MMHandleType handle, GstMessage *message, GError *error)
3779 {
3780         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
3781         gchar *debug = NULL;
3782         GError *err = NULL;
3783
3784         mmf_return_val_if_fail(hcamcorder, FALSE);
3785         mmf_return_val_if_fail(error, FALSE);
3786
3787         MMCAM_LOG_INFO("");
3788
3789         gst_message_parse_warning(message, &err, &debug);
3790
3791         if (error->domain == GST_CORE_ERROR) {
3792                 MMCAM_LOG_WARNING("GST warning: GST_CORE domain");
3793         } else if (error->domain == GST_LIBRARY_ERROR) {
3794                 MMCAM_LOG_WARNING("GST warning: GST_LIBRARY domain");
3795         } else if (error->domain == GST_RESOURCE_ERROR) {
3796                 MMCAM_LOG_WARNING("GST warning: GST_RESOURCE domain");
3797                 __mmcamcorder_gst_handle_resource_warning(handle, message, error);
3798         } else if (error->domain == GST_STREAM_ERROR) {
3799                 MMCAM_LOG_WARNING("GST warning: GST_STREAM domain");
3800         } else {
3801                 MMCAM_LOG_WARNING("This error domain(%d) is not defined.", error->domain);
3802         }
3803
3804         if (err != NULL) {
3805                 g_error_free(err);
3806                 err = NULL;
3807         }
3808         if (debug != NULL) {
3809                 MMCAM_LOG_ERROR("Debug: %s", debug);
3810                 g_free(debug);
3811                 debug = NULL;
3812         }
3813
3814         return TRUE;
3815 }
3816
3817
3818 static gint __mmcamcorder_gst_handle_resource_warning(MMHandleType handle, GstMessage *message , GError *error)
3819 {
3820         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
3821         _MMCamcorderSubContext *sc = NULL;
3822         GstElement *element = NULL;
3823         gchar *msg_src_element;
3824
3825         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
3826
3827         sc = MMF_CAMCORDER_SUBCONTEXT(handle);
3828         mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
3829
3830         MMCAM_LOG_INFO("");
3831
3832         /* Special message handling */
3833         /* video source */
3834         element = GST_ELEMENT_CAST(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst);
3835         if (GST_ELEMENT_CAST(message->src) == element) {
3836                 if (error->code == GST_RESOURCE_ERROR_FAILED) {
3837                         msg_src_element = GST_ELEMENT_NAME(GST_ELEMENT_CAST(message->src));
3838                         MMCAM_LOG_WARNING("-Msg src:[%s] Domain:[%s] Error:[%s]",
3839                                 msg_src_element, g_quark_to_string(error->domain), error->message);
3840                         return MM_ERROR_NONE;
3841                 }
3842         }
3843
3844         /* General plugin */
3845         switch (error->code) {
3846         case GST_RESOURCE_ERROR_WRITE:
3847         case GST_RESOURCE_ERROR_NO_SPACE_LEFT:
3848         case GST_RESOURCE_ERROR_SEEK:
3849         case GST_RESOURCE_ERROR_NOT_FOUND:
3850         case GST_RESOURCE_ERROR_FAILED:
3851         case GST_RESOURCE_ERROR_TOO_LAZY:
3852         case GST_RESOURCE_ERROR_BUSY:
3853         case GST_RESOURCE_ERROR_OPEN_READ:
3854         case GST_RESOURCE_ERROR_OPEN_WRITE:
3855         case GST_RESOURCE_ERROR_OPEN_READ_WRITE:
3856         case GST_RESOURCE_ERROR_CLOSE:
3857         case GST_RESOURCE_ERROR_READ:
3858         case GST_RESOURCE_ERROR_SYNC:
3859         case GST_RESOURCE_ERROR_SETTINGS:
3860         default:
3861                 MMCAM_LOG_WARNING("General GST warning(%d)", error->code);
3862                 break;
3863         }
3864
3865         return MM_ERROR_NONE;
3866 }
3867
3868
3869 void _mmcamcorder_emit_signal(MMHandleType handle, const char *object_name,
3870         const char *interface_name, const char *signal_name, int value)
3871 {
3872         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
3873
3874         mmf_return_if_fail(hcamcorder && object_name && interface_name && signal_name);
3875
3876         MMCAM_LOG_INFO("object %s, interface %s, signal %s, value %d",
3877                 object_name, interface_name, signal_name, value);
3878
3879         _mmcamcorder_emit_dbus_signal(hcamcorder->gdbus_conn, object_name, interface_name, signal_name, value);
3880
3881         return;
3882 }
3883
3884
3885 #ifdef _MMCAMCORDER_MM_RM_SUPPORT
3886 static int __mmcamcorder_resource_release_cb(mm_resource_manager_h rm,
3887         mm_resource_manager_res_h res, void *user_data)
3888 {
3889         mmf_camcorder_t *hcamcorder = (mmf_camcorder_t *) user_data;
3890
3891         mmf_return_val_if_fail(hcamcorder, FALSE);
3892
3893         MMCAM_LOG_WARNING("enter");
3894
3895         _MMCAMCORDER_LOCK_RESOURCE(hcamcorder);
3896
3897         /* set flag for resource release callback */
3898         hcamcorder->is_release_cb_calling = TRUE;
3899
3900         _MMCAMCORDER_UNLOCK_RESOURCE(hcamcorder);
3901
3902         _MMCAMCORDER_LOCK_INTERRUPT(hcamcorder);
3903
3904         if (res == hcamcorder->video_encoder_resource) {
3905                 /* Stop video recording */
3906                 if (_mmcamcorder_commit((MMHandleType)hcamcorder) != MM_ERROR_NONE) {
3907                         MMCAM_LOG_ERROR("commit failed, cancel it");
3908                         _mmcamcorder_cancel((MMHandleType)hcamcorder);
3909                 }
3910         } else {
3911                 /* Stop camera */
3912                 __mmcamcorder_force_stop(hcamcorder, _MMCAMCORDER_STATE_CHANGE_BY_RM);
3913         }
3914
3915         _MMCAMCORDER_UNLOCK_INTERRUPT(hcamcorder);
3916
3917         _MMCAMCORDER_LOCK_RESOURCE(hcamcorder);
3918
3919         if (res == hcamcorder->camera_resource)
3920                 hcamcorder->camera_resource = NULL;
3921         else if (res == hcamcorder->video_overlay_resource)
3922                 hcamcorder->video_overlay_resource = NULL;
3923         else if (res == hcamcorder->video_encoder_resource)
3924                 hcamcorder->video_encoder_resource = NULL;
3925
3926         /* restore flag for resource release callback */
3927         hcamcorder->is_release_cb_calling = FALSE;
3928
3929         _MMCAMCORDER_UNLOCK_RESOURCE(hcamcorder);
3930
3931         MMCAM_LOG_WARNING("leave");
3932
3933         return TRUE;
3934 }
3935 #endif /* _MMCAMCORDER_MM_RM_SUPPORT */
3936
3937 int _mmcamcorder_manage_external_storage_state(MMHandleType handle, int storage_state)
3938 {
3939         int ret = MM_ERROR_NONE;
3940         int current_state = MM_CAMCORDER_STATE_NONE;
3941         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
3942         _MMCamcorderMsgItem msg;
3943
3944         mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_INVALID_ARGUMENT);
3945
3946         MMCAM_LOG_WARNING("storage state %d", storage_state);
3947
3948         /* check storage state */
3949         if (storage_state != STORAGE_STATE_UNMOUNTABLE &&
3950                 storage_state != STORAGE_STATE_REMOVED) {
3951                 MMCAM_LOG_WARNING("nothing to do");
3952                 return MM_ERROR_NONE;
3953         }
3954
3955         _MMCAMCORDER_LOCK_INTERRUPT(hcamcorder);
3956
3957         /* check recording state */
3958         current_state = _mmcamcorder_get_state(handle);
3959
3960         MMCAM_LOG_WARNING("current_state %d", current_state);
3961
3962         if (current_state < MM_CAMCORDER_STATE_RECORDING) {
3963                 MMCAM_LOG_WARNING("no recording now");
3964                 goto _MANAGE_DONE;
3965         }
3966
3967         /* check storage type */
3968         if (hcamcorder->storage_info.type != STORAGE_TYPE_EXTERNAL) {
3969                 MMCAM_LOG_WARNING("external storage is not used");
3970                 goto _MANAGE_DONE;
3971         }
3972
3973         /* cancel recording */
3974         ret = _mmcamcorder_cancel(handle);
3975
3976         /* send error message */
3977         msg.id = MM_MESSAGE_CAMCORDER_ERROR;
3978         msg.param.code = MM_ERROR_OUT_OF_STORAGE;
3979
3980         _mmcamcorder_send_message(handle, &msg);
3981
3982 _MANAGE_DONE:
3983         _MMCAMCORDER_UNLOCK_INTERRUPT(hcamcorder);
3984
3985         MMCAM_LOG_WARNING("done - ret 0x%x", ret);
3986
3987         return ret;
3988 }