Release version 0.10.15
[platform/core/multimedia/libmm-camcorder.git] / src / mm_camcorder_internal.c
index 513cd44..37e41c9 100644 (file)
@@ -261,6 +261,33 @@ int _mmcamcorder_create(MMHandleType *handle, MMCamPreset *info)
        hcamcorder->device_type = info->videodev_type;
        _mmcam_dbg_warn("Device Type : %d", hcamcorder->device_type);
 
+       if (MM_ERROR_NONE == _mm_session_util_read_information(-1, &hcamcorder->session_type, &hcamcorder->session_flags)) {
+               _mmcam_dbg_log("use sound focus function.");
+               hcamcorder->sound_focus_register = TRUE;
+
+               if (MM_ERROR_NONE != mm_sound_focus_get_id(&hcamcorder->sound_focus_id)) {
+                       _mmcam_dbg_err("mm_sound_focus_get_uniq failed");
+                       ret = MM_ERROR_POLICY_BLOCKED;
+                       goto _ERR_DEFAULT_VALUE_INIT;
+               }
+
+               if (MM_ERROR_NONE != mm_sound_register_focus_for_session(hcamcorder->sound_focus_id,
+                                                                        getpid(),
+                                                                        "media",
+                                                                        _mmcamcorder_sound_focus_cb,
+                                                                        hcamcorder)) {
+                       _mmcam_dbg_err("mm_sound_register_focus failed");
+                       ret = MM_ERROR_POLICY_BLOCKED;
+                       goto _ERR_DEFAULT_VALUE_INIT;
+               }
+
+               _mmcam_dbg_log("mm_sound_register_focus done - id %d, session type %d, flags 0x%x",
+                              hcamcorder->sound_focus_id, hcamcorder->session_type, hcamcorder->session_flags);
+       } else {
+               _mmcam_dbg_log("_mm_session_util_read_information failed. skip sound focus function.");
+               hcamcorder->sound_focus_register = FALSE;
+       }
+
        /* Get Camera Configure information from Camcorder INI file */
        _mmcamcorder_conf_get_info((MMHandleType)hcamcorder, CONFIGURE_TYPE_MAIN, CONFIGURE_MAIN_FILE, &hcamcorder->conf_main);
 
@@ -540,6 +567,18 @@ int _mmcamcorder_create(MMHandleType *handle, MMCamPreset *info)
        return MM_ERROR_NONE;
 
 _ERR_DEFAULT_VALUE_INIT:
+       /* unregister sound focus */
+       if (hcamcorder->sound_focus_register && hcamcorder->sound_focus_id > 0) {
+               if (MM_ERROR_NONE != mm_sound_unregister_focus(hcamcorder->sound_focus_id)) {
+                       _mmcam_dbg_err("mm_sound_unregister_focus[id %d] failed", hcamcorder->sound_focus_id);
+               } else {
+                       _mmcam_dbg_log("mm_sound_unregister_focus[id %d] done", hcamcorder->sound_focus_id);
+               }
+       } else {
+               _mmcam_dbg_warn("no need to unregister sound focus[%d, id %d]",
+                              hcamcorder->sound_focus_register, hcamcorder->sound_focus_id);
+       }
+
        /* Remove attributes */
        if (hcamcorder->attributes) {
                _mmcamcorder_dealloc_attribute((MMHandleType)hcamcorder, hcamcorder->attributes);
@@ -725,10 +764,17 @@ int _mmcamcorder_destroy(MMHandleType handle)
        _mmcamcorder_remove_message_all(handle);
 
        /* unregister sound focus */
-       if(hcamcorder->sound_focus_register) {
-               if (MM_ERROR_NONE != mm_sound_unregister_focus(hcamcorder->sound_focus_id)) {
-                       _mmcam_dbg_err("mm_sound_unregister_focus[id %d] failed", hcamcorder->sound_focus_id);
+       if (hcamcorder->sound_focus_register && hcamcorder->sound_focus_id > 0) {
+               if (mm_sound_unregister_focus(hcamcorder->sound_focus_id) != MM_ERROR_NONE) {
+                       _mmcam_dbg_err("mm_sound_unregister_focus[id %d] failed",
+                                      hcamcorder->sound_focus_id);
+               } else {
+                       _mmcam_dbg_log("mm_sound_unregister_focus[id %d] done",
+                                      hcamcorder->sound_focus_id);
                }
+       } else {
+               _mmcam_dbg_log("no need to unregister sound focus.[%d, id %d]",
+                              hcamcorder->sound_focus_register, hcamcorder->sound_focus_id);
        }
 
        /* release model_name */
@@ -837,7 +883,6 @@ int _mmcamcorder_realize(MMHandleType handle)
        mm_camcorder_get_attributes(handle, NULL,
                                    MMCAM_DISPLAY_SURFACE, &display_surface_type,
                                    MMCAM_CAMERA_RECORDING_MOTION_RATE, &motion_rate,
-                                   MMCAM_PID_FOR_SOUND_FOCUS, &pid_for_sound_focus,
                                    MMCAM_DISPLAY_SHM_SOCKET_PATH, &socket_path, &socket_path_len,
                                    NULL);
 
@@ -873,8 +918,21 @@ int _mmcamcorder_realize(MMHandleType handle)
                               vconf_recorder_state, VCONFKEY_RECORDER_STATE_CREATED);
        }
 
-       /* acquire sound focus or set sound focus watch callback */
-       if(hcamcorder->sound_focus_register) {
+       /* sound focus */
+       if (hcamcorder->sound_focus_register) {
+               mm_camcorder_get_attributes(handle, NULL,
+                                           MMCAM_PID_FOR_SOUND_FOCUS, &pid_for_sound_focus,
+                                           NULL);
+
+               if (pid_for_sound_focus == 0) {
+                       pid_for_sound_focus = getpid();
+                       _mmcam_dbg_warn("pid for sound focus is not set, use my pid %d", pid_for_sound_focus);
+               }
+
+               /* acquire sound focus or set sound focus watch callback */
+               hcamcorder->acquired_focus = 0;
+               hcamcorder->sound_focus_watch_id = 0;
+
                /* check session flags */
                if (hcamcorder->session_flags & MM_SESSION_OPTION_PAUSE_OTHERS) {
                        /* acquire sound focus */
@@ -886,39 +944,37 @@ int _mmcamcorder_realize(MMHandleType handle)
 
                                /* TODO: MM_ERROR_POLICY_BLOCKED_BY_CALL, MM_ERROR_POLICY_BLOCKED_BY_ALARM*/
                                ret = MM_ERROR_POLICY_BLOCKED;
-
                                goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
                        }
+
+                       hcamcorder->acquired_focus = FOCUS_FOR_BOTH;
                } else if (hcamcorder->session_flags & MM_SESSION_OPTION_UNINTERRUPTIBLE) {
                        /* do nothing */
                        _mmcam_dbg_log("SESSION_UNINTERRUPTIBLE - do nothing for sound focus");
                } else {
                        /* set sound focus watch callback */
-                       if (pid_for_sound_focus == 0) {
-                               _mmcam_dbg_warn("pid for sound focus is not set, so call getpid");
-                               pid_for_sound_focus = getpid();
-                       }
-
                        _mmcam_dbg_log("ETC - set sound focus watch callback - pid %d", pid_for_sound_focus);
 
                        ret_sound = mm_sound_set_focus_watch_callback_for_session(pid_for_sound_focus,
-                                                                                 FOCUS_FOR_BOTH,
-                                                                                 (mm_sound_focus_changed_watch_cb)_mmcamcorder_sound_focus_watch_cb,
-                                                                                 hcamcorder,
-                                                                                 &hcamcorder->sound_focus_watch_id);
+                                                                                 FOCUS_FOR_BOTH,
+                                                                                 (mm_sound_focus_changed_watch_cb)_mmcamcorder_sound_focus_watch_cb,
+                                                                                 hcamcorder,
+                                                                                 &hcamcorder->sound_focus_watch_id);
                        if (ret_sound != MM_ERROR_NONE) {
                                _mmcam_dbg_err("mm_sound_set_focus_watch_callback failed [0x%x]", ret_sound);
 
                                /* TODO: MM_ERROR_POLICY_BLOCKED_BY_CALL, MM_ERROR_POLICY_BLOCKED_BY_ALARM*/
                                ret = MM_ERROR_POLICY_BLOCKED;
-
                                goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
                        }
 
                        _mmcam_dbg_log("sound focus watch cb id %d", hcamcorder->sound_focus_watch_id);
                }
+       } else {
+               _mmcam_dbg_log("no need to register sound focus");
        }
 
+
        /* alloc sub context */
        hcamcorder->sub_context = _mmcamcorder_alloc_subcontext(hcamcorder->type);
        if(!hcamcorder->sub_context) {
@@ -1057,13 +1113,26 @@ _ERR_CAMCORDER_CMD:
 _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK:
        _MMCAMCORDER_UNLOCK_CMD(hcamcorder);
 
-       if (hcamcorder->sound_focus_watch_id > 0) {
-               ret_sound = mm_sound_unset_focus_watch_callback(hcamcorder->sound_focus_watch_id);
-               if (ret_sound != MM_ERROR_NONE) {
-                       _mmcam_dbg_warn("mm_sound_unset_focus_watch_callback failed [0x%x]", ret_sound);
+       if (hcamcorder->sound_focus_register) {
+               if (hcamcorder->sound_focus_watch_id > 0) {
+                       if (mm_sound_unset_focus_watch_callback(hcamcorder->sound_focus_watch_id) != MM_ERROR_NONE) {
+                               _mmcam_dbg_warn("mm_sound_unset_focus_watch_callback[id %d] failed",
+                                               hcamcorder->sound_focus_watch_id);
+                       } else {
+                               _mmcam_dbg_warn("mm_sound_unset_focus_watch_callback[id %d] done",
+                                               hcamcorder->sound_focus_watch_id);
+                       }
                }
 
-               hcamcorder->sound_focus_watch_id = 0;
+               if (hcamcorder->acquired_focus > 0) {
+                               if (mm_sound_release_focus(0, hcamcorder->acquired_focus, NULL) != MM_ERROR_NONE) {
+                                       _mmcam_dbg_err("mm_sound_release_focus[focus %d] failed",
+                                                      hcamcorder->acquired_focus);
+                               } else {
+                                       _mmcam_dbg_err("mm_sound_release_focus[focus %d] done",
+                                                      hcamcorder->acquired_focus);
+                               }
+               }
        }
 
 _ERR_CAMCORDER_CMD_PRECON:
@@ -1116,39 +1185,42 @@ int _mmcamcorder_unrealize(MMHandleType handle)
        /* Deinitialize main context member */
        hcamcorder->command = NULL;
 
-       /* check who calls unrealize. it's no need to do nothing if caller is sound focus callback */
-       if (hcamcorder->state_change_by_system != _MMCAMCORDER_STATE_CHANGE_BY_ASM) {
-               /* release sound focus or unset sound focus watch callback */
-               if(hcamcorder->sound_focus_register) {
-                       int ret_sound = MM_ERROR_NONE;
+       _mmcam_dbg_log("focus register %d, session flag 0x%x, state_change_by_system %d",
+                      hcamcorder->sound_focus_register, hcamcorder->session_flags, hcamcorder->state_change_by_system);
 
-                       /* check session flags */
-                       if (hcamcorder->session_flags & MM_SESSION_OPTION_PAUSE_OTHERS) {
-                               /* release sound focus */
-                               _mmcam_dbg_log("PAUSE_OTHERS - release sound focus");
+       /* release sound focus or unset sound focus watch callback */
+       if (hcamcorder->sound_focus_register) {
+               int ret_sound = MM_ERROR_NONE;
 
-                               ret_sound = mm_sound_release_focus(0, FOCUS_FOR_BOTH, NULL);
-                               if (ret_sound != MM_ERROR_NONE) {
-                                       _mmcam_dbg_warn("mm_sound_release_focus failed [0x%x]", ret_sound);
-                               }
-                       } else if (hcamcorder->session_flags & MM_SESSION_OPTION_UNINTERRUPTIBLE) {
-                               /* do nothing */
-                               _mmcam_dbg_log("SESSION_UNINTERRUPTIBLE - do nothing for sound focus");
-                       } else {
-                               /* unset sound focus watch callback */
-                               _mmcam_dbg_log("ETC - unset sound focus watch callback - id %d", hcamcorder->sound_focus_watch_id);
+               _mmcam_dbg_log("state_change_by_system %d, session flag 0x%x, acquired_focus %d, sound_focus_id %d, sound_focus_watch_id %d",
+                              hcamcorder->state_change_by_system, hcamcorder->session_flags, hcamcorder->acquired_focus,
+                              hcamcorder->sound_focus_id, hcamcorder->sound_focus_watch_id);
 
-                               if (hcamcorder->sound_focus_watch_id > 0) {
-                                       ret_sound = mm_sound_unset_focus_watch_callback(hcamcorder->sound_focus_watch_id);
-                                       if (ret_sound != MM_ERROR_NONE) {
-                                               _mmcam_dbg_warn("mm_sound_unset_focus_watch_callback failed [0x%x]", ret_sound);
-                                       }
-                               } else {
-                                       _mmcam_dbg_warn("invalid sound focus id %d", hcamcorder->sound_focus_watch_id);
-                               }
+               if (hcamcorder->state_change_by_system != _MMCAMCORDER_STATE_CHANGE_BY_ASM &&
+                   hcamcorder->sound_focus_watch_id > 0) {
+                       ret_sound = mm_sound_unset_focus_watch_callback(hcamcorder->sound_focus_watch_id);
+                       if (ret_sound != MM_ERROR_NONE) {
+                               _mmcam_dbg_warn("mm_sound_unset_focus_watch_callback failed [0x%x]",
+                                               ret_sound);
+                       } else {
+                               _mmcam_dbg_warn("mm_sound_unset_focus_watch_callback done");
+                       }
+               } else {
+                       _mmcam_dbg_warn("no need to unset watch callback.[state_change_by_system %d, sound_focus_watch_id %d]",
+                                       hcamcorder->state_change_by_system, hcamcorder->sound_focus_watch_id);
+               }
 
-                               hcamcorder->sound_focus_watch_id = 0;
+               if (hcamcorder->acquired_focus > 0) {
+                       ret_sound = mm_sound_release_focus(0, hcamcorder->acquired_focus, NULL);
+                       if (ret_sound != MM_ERROR_NONE) {
+                               _mmcam_dbg_warn("mm_sound_release_focus failed [0x%x]",
+                                               ret_sound);
+                       } else {
+                               _mmcam_dbg_log("mm_sound_release_focus done");
                        }
+               } else {
+                       _mmcam_dbg_warn("no need to release focus - current acquired focus %d",
+                                       hcamcorder->acquired_focus);
                }
        }
 
@@ -3043,7 +3115,10 @@ void _mmcamcorder_sound_focus_cb(int id, mm_sound_focus_type_e focus_type,
        _MMCAMCORDER_LOCK_ASM(hcamcorder);
 
        if (focus_state == FOCUS_IS_RELEASED) {
-               _mmcam_dbg_log("FOCUS is released : Stop pipeline[state:%d]", current_state);
+               hcamcorder->acquired_focus &= ~focus_type;
+
+               _mmcam_dbg_log("FOCUS is released [type %d, remained focus %d] : Stop pipeline[state:%d]",
+                              focus_type, hcamcorder->acquired_focus, current_state);
 
                __mmcamcorder_force_stop(hcamcorder);
 
@@ -3051,7 +3126,10 @@ void _mmcamcorder_sound_focus_cb(int id, mm_sound_focus_type_e focus_type,
        } else if (focus_state == FOCUS_IS_ACQUIRED) {
                _MMCamcorderMsgItem msg;
 
-               _mmcam_dbg_log("FOCUS is acquired");
+               hcamcorder->acquired_focus |= focus_type;
+
+               _mmcam_dbg_log("FOCUS is acquired [type %d, new focus %d]",
+                              focus_type, hcamcorder->acquired_focus);
 
                msg.id = MM_MESSAGE_READY_TO_RESUME;
                _mmcamcorder_send_message((MMHandleType)hcamcorder, &msg);