Fix double unlock of slot mutex in user stops scneario
[platform/core/multimedia/libmm-sound.git] / server / mm_sound_mgr_codec.c
index c604cf3..c2c968b 100644 (file)
 #include <string.h>
 #include <pthread.h>
 #include <unistd.h>
-#include <errno.h>
 
 #include <mm_error.h>
 #include <mm_types.h>
 #include <mm_debug.h>
-#include <mm_ipc.h>
-#include <mm_session.h>
 
 #include <glib.h>
 
-#include "include/mm_sound_mgr_common.h"
 #include "include/mm_sound_mgr_codec.h"
 #include "include/mm_sound_mgr_ipc.h"
 #include "include/mm_sound_plugin_codec.h"
 #include "include/mm_sound_thread_pool.h"
 
 #include "../include/mm_sound_common.h"
-#include "../include/mm_sound_focus.h"
+#include "../include/mm_sound.h"
 
-#define SHUTDOWN_TIMEOUT_SEC 10
+#define SHUTDOWN_TIMEOUT_SEC 60
 #define STATUS_IDLE 0
 #define STATUS_SOUND 3
 #define SOUND_SLOT_START 0
-#define FOCUS_INTEGRATION
+#define MANAGER_HANDLE_MAX 256
 
 typedef struct {
        int (*callback)(int, void *, void *, int);      /* msg_type(pid) client callback & client data info */
@@ -60,109 +56,58 @@ typedef struct {
 
        int pluginid;
        int status;
-       int session_type;
-       int session_options;
-#ifdef FOCUS_INTEGRATION
-       int focus_handle;
-       int focus_wcb_id;
-       unsigned int subs_id;
-       mm_sound_focus_type_e current_focus_type;
-#endif
-       bool stop_by_user;
-
-       bool enable_session;
- } __mmsound_mgr_codec_handle_t;
+} __mmsound_mgr_codec_handle_t;
 
 static MMSoundPluginType *g_codec_plugins = NULL;
 static __mmsound_mgr_codec_handle_t g_slots[MANAGER_HANDLE_MAX];
 static mmsound_codec_interface_t g_plugins[MM_SOUND_SUPPORTED_CODEC_NUM];
 static pthread_mutex_t g_slot_mutex;
-GSourceFunc g_shutdown_cb;
-guint g_timer_id = 0;
+static GSourceFunc g_shutdown_cb;
+static guint g_timer_id = 0;
 
-#define SLOT_LOCK() do { pthread_mutex_lock(&g_slot_mutex); /* debug_msg("After Slot_mutex LOCK\n"); */ } while (0)
-#define SLOT_UNLOCK() do { pthread_mutex_unlock(&g_slot_mutex); /* debug_msg("After Slot_mutex UNLOCK\n"); */ } while (0)
+#ifdef DEBUG_DETAIL
+#define SLOT_LOCK() do { \
+       debug_msg("Before Slot_mutex LOCK"); \
+       pthread_mutex_lock(&g_slot_mutex); \
+       debug_msg("After Slot_mutex LOCK"); \
+} while (0)
+#define SLOT_UNLOCK() do { \
+       debug_msg("Before Slot_mutex UNLOCK"); \
+       pthread_mutex_unlock(&g_slot_mutex); \
+       debug_msg("After Slot_mutex UNLOCK"); \
+} while (0)
+#else
+#define SLOT_LOCK() do { pthread_mutex_lock(&g_slot_mutex); } while (0)
+#define SLOT_UNLOCK() do { pthread_mutex_unlock(&g_slot_mutex); } while (0)
+#endif
 
 static int _MMSoundMgrCodecRegisterInterface(MMSoundPluginType *plugin);
-static int _MMSoundMgrCodecStopCallback(int param);
+static int _MMSoundMgrCodecStopCallback(int param, bool stop_by_user);
+static gboolean _mm_sound_mgr_codec_slot_is_empty();
 
-
-#ifdef FOCUS_INTEGRATION
-
-static void _handle_focus_event(int slotid, mm_sound_focus_type_e focus_type, mm_sound_focus_state_e focus_state, bool is_watch)
+static gboolean _idle_cb(gpointer user_data)
 {
-       int result = MM_ERROR_NONE;
-
-       if (g_slots[slotid].session_options & MM_SESSION_OPTION_UNINTERRUPTIBLE) {
-               debug_warning("session option is UNINTERRUPTIBLE, nothing to do with focus");
-               return;
+       if (_mm_sound_mgr_codec_slot_is_empty()) {
+               debug_msg("slot is empty, ready to shutdown! %p", g_shutdown_cb);
+               if (g_shutdown_cb)
+                       g_shutdown_cb(NULL);
+       } else {
+               debug_warning("slot is not empty!!! no shutdown...");
        }
 
-       if (focus_state == FOCUS_IS_RELEASED) {
-               if (!is_watch)
-                       g_slots[slotid].current_focus_type = FOCUS_FOR_BOTH & (~focus_type);
-               debug_warning("focus is released -> stop playing");
-
-               result = MMSoundMgrCodecStop(slotid);
-               if (result != MM_ERROR_NONE)
-                       debug_error("result error 0x%X", result);
-       }
+       return FALSE;
 }
 
-static void _sound_codec_focus_callback(int id, mm_sound_focus_type_e focus_type, mm_sound_focus_state_e focus_state,
-                                                                               const char *reason_for_change, int option, const char *ext_info, void *user_data)
+static gboolean _timeout_cb(gpointer user_data)
 {
-       debug_warning("focus callback called -> focus_state(%d), reason_for_change(%s)",
-                       focus_state, reason_for_change ? reason_for_change : "N/A");
-
-       _handle_focus_event((int)user_data, focus_type, focus_state, false);
-}
+       debug_warning("TIMEOUT : add idle callback now...");
 
-static void _sound_codec_focus_watch_callback(int id, mm_sound_focus_type_e focus_type, mm_sound_focus_state_e focus_state,
-                                                                                       const char *reason_for_change, const char *ext_info, void *user_data)
-{
-       debug_warning("focus watch callback called -> focus_state(%d), reason_for_change(%s)",
-                               focus_state, reason_for_change ? reason_for_change : "N/A");
+       g_idle_add(_idle_cb, NULL);
+       g_timer_id = 0;
 
-       _handle_focus_event((int)user_data, focus_type, focus_state, true);
+       return FALSE;
 }
 
-static void _sound_codec_device_connected_callback(MMSoundDevice_t device, bool is_connected, void *user_data)
-{
-       int slotid = (int)user_data;
-       int result = MM_ERROR_NONE;
-       mm_sound_device_type_e type;
-
-       debug_warning ("device_connected_callback called : handle[%p], connected[%d], slotid[%d]", device, is_connected, slotid);
-
-       if (mm_sound_get_device_type (device, &type) != MM_ERROR_NONE) {
-               debug_error("getting device type failed");
-               return;
-       }
-
-       switch (type) {
-               case MM_SOUND_DEVICE_TYPE_AUDIOJACK:
-               case MM_SOUND_DEVICE_TYPE_BLUETOOTH:
-               case MM_SOUND_DEVICE_TYPE_HDMI:
-               case MM_SOUND_DEVICE_TYPE_MIRRORING:
-               case MM_SOUND_DEVICE_TYPE_USB_AUDIO:
-                       if (!is_connected) {
-                               debug_warning("sound device unplugged");
-                               result = MMSoundMgrCodecStop(slotid);
-                               if (result != MM_ERROR_NONE)
-                                       debug_error("MMSoundMgrCodecStop error %d\n", result);
-
-                               result = mm_sound_remove_device_connected_callback(g_slots[slotid].subs_id);
-                               if (result != MM_ERROR_NONE)
-                                       debug_error("mm_sound_remove_device_connected_callback error %d\n", result);
-                       }
-                       break;
-
-               default:
-                       break;
-       }
-}
-#endif
 
 /* FIXME : critical section for g_timer_id? */
 static void _mm_sound_mgr_codec_shutdown_timer_start()
@@ -171,10 +116,9 @@ static void _mm_sound_mgr_codec_shutdown_timer_start()
                debug_error("Already active timer [%d] exists", g_timer_id);
                return;
        }
-
        if (g_shutdown_cb) {
-               g_timer_id = g_timeout_add_seconds(SHUTDOWN_TIMEOUT_SEC, g_shutdown_cb, NULL);
-               debug_error("TIMER : new timer [%d]", g_timer_id);
+               g_timer_id = g_timeout_add_seconds(SHUTDOWN_TIMEOUT_SEC, _timeout_cb, NULL);
+               debug_warning("TIMER : new timer [%d]", g_timer_id);
        } else {
                debug_warning("No Timer started due to invalid shutdown callback");
        }
@@ -183,7 +127,7 @@ static void _mm_sound_mgr_codec_shutdown_timer_start()
 static void _mm_sound_mgr_codec_shutdown_timer_stop()
 {
        if (g_timer_id > 0) {
-               debug_error("TIMER : remove timer id [%d]", g_timer_id);
+               debug_warning("TIMER : remove timer id [%d]", g_timer_id);
                g_source_remove(g_timer_id);
                g_timer_id = 0;
        } else {
@@ -198,29 +142,27 @@ static int _mm_sound_mgr_codec_slot_get_empty(int *slot)
 
        SLOT_LOCK();
 
-       for (slotid = SOUND_SLOT_START; slotid < MANAGER_HANDLE_MAX ; slotid++) {
-               if (g_slots[slotid].status == STATUS_IDLE) {
-                       g_slots[slotid].status = STATUS_SOUND;
-                       break;
+       while (1) {
+               for (slotid = SOUND_SLOT_START; slotid < MANAGER_HANDLE_MAX ; slotid++) {
+                       if (g_slots[slotid].status == STATUS_IDLE) {
+                               g_slots[slotid].status = STATUS_SOUND;
+                               break;
+                       }
                }
-       }
 
-       if (slotid < MANAGER_HANDLE_MAX) {
-               debug_msg("New handle allocated (codec slot ID : [%d])\n", slotid);
-               *slot = slotid;
-
-               _mm_sound_mgr_codec_shutdown_timer_stop();
-
-       } else {
-               debug_warning("Handle is full handle : [%d]\n", slotid);
-               *slot = -1;
-               /* Temporal code for reset */
-               while (slotid--) {
-                       g_slots[slotid].status = STATUS_IDLE;
+               if (slotid < MANAGER_HANDLE_MAX) {
+                       debug_msg("New handle allocated (codec slot ID : [%d])", slotid);
+                       *slot = slotid;
+                       break;
                }
-               err = MM_ERROR_SOUND_INTERNAL;
+
+               /* FIXME: avoiding infinite wait is required */
+               debug_warning("Handle is full..wait for a while and will retry...");
+               sleep(1);
        }
 
+       _mm_sound_mgr_codec_shutdown_timer_stop();
+
        SLOT_UNLOCK();
 
        return err;
@@ -238,12 +180,13 @@ static gboolean _mm_sound_mgr_codec_slot_is_empty()
        return (slotid == MANAGER_HANDLE_MAX) ? TRUE : FALSE;
 }
 
-static void _mm_sound_mgr_codec_slot_clear(int slotid)
+static void _mm_sound_mgr_codec_slot_clear(int slotid, bool dump)
 {
        memset(&g_slots[slotid], 0, sizeof(__mmsound_mgr_codec_handle_t));
        g_slots[slotid].status = STATUS_IDLE;
 
-       debug_error("SlotID [%d] cleared", slotid);
+       if (dump)
+               debug_warning("SlotID [%d] cleared", slotid);
 }
 
 int MMSoundMgrCodecInit(const char *targetdir, GSourceFunc _shutdown_cb)
@@ -256,23 +199,22 @@ int MMSoundMgrCodecInit(const char *targetdir, GSourceFunc _shutdown_cb)
        memset(g_slots, 0, sizeof(g_slots));
 
        if (pthread_mutex_init(&g_slot_mutex, NULL)) {
-               debug_error("pthread_mutex_init failed\n");
+               debug_error("pthread_mutex_init failed");
                return MM_ERROR_SOUND_INTERNAL;
        }
 
        for (slotid = 0; slotid < MANAGER_HANDLE_MAX; slotid++)
-               _mm_sound_mgr_codec_slot_clear(slotid);
+               _mm_sound_mgr_codec_slot_clear(slotid, false);
 
        if (g_codec_plugins) {
-               debug_warning("Please Check Init twice\n");
+               debug_warning("Please Check Init twice");
                MMSoundPluginRelease(g_codec_plugins);
        }
 
        MMSoundPluginScan(targetdir, MM_SOUND_PLUGIN_TYPE_CODEC, &g_codec_plugins);
        if (g_codec_plugins) {
-               while (g_codec_plugins[loop].type != MM_SOUND_PLUGIN_TYPE_NONE) {
+               while (g_codec_plugins[loop].type != MM_SOUND_PLUGIN_TYPE_NONE)
                        _MMSoundMgrCodecRegisterInterface(&g_codec_plugins[loop++]);
-               }
        }
 
        if (_shutdown_cb)
@@ -313,198 +255,6 @@ static int _MMSoundMgrCodecFindCodecPluginID(enum MMSoundSupportedCodec codec_to
        return -1;
 }
 
-int MMSoundMgrCodecPlay(int *slotid, const mmsound_mgr_codec_param_t *param)
-{
-       int plugin_id = 0;
-       mmsound_codec_info_t info;
-       mmsound_codec_param_t codec_param;
-       int err = MM_ERROR_NONE;
-
-#ifdef FOCUS_INTEGRATION
-       int need_focus_unregister = 0;
-#endif
-
-#ifdef DEBUG_DETAIL
-       debug_enter();
-#endif
-       plugin_id = _MMSoundMgrCodecFindCodecPluginID(MM_SOUND_SUPPORTED_CODEC_WAVE);
-       if (plugin_id == -1) {
-               debug_error("Could not find proper codec plugin!!!");
-               err = MM_ERROR_SOUND_INTERNAL;
-               goto cleanup;
-       }
-
-       err = g_plugins[plugin_id].Parse(param->pfilename, &info);
-       if (err != MM_ERROR_NONE) {
-               debug_error("Could not parse file [%s] by plugin[%d]\n", param->pfilename, plugin_id);
-               goto cleanup;
-       }
-
-       debug_msg("DTMF[%d] Repeat[%d] Volume[%f] plugin_codec[%d]\n", param->tone, param->repeat_count, param->volume, plugin_id);
-
-#ifdef DEBUG_DETAIL
-       debug_msg("Get New handle\n");
-#endif
-
-       err = _mm_sound_mgr_codec_slot_get_empty(slotid);
-       if (err != MM_ERROR_NONE || *slotid < 0) {
-               debug_error("Empty g_slot is not found\n");
-               goto cleanup;
-       }
-
-       codec_param.tone = param->tone;
-       codec_param.volume_config = param->volume_config;
-       codec_param.repeat_count = param->repeat_count;
-       codec_param.volume = param->volume;
-       codec_param.pfilename = param->pfilename;
-       codec_param.stop_cb = _MMSoundMgrCodecStopCallback;
-       codec_param.param = *slotid;
-       codec_param.pid = (int)param->param;
-       codec_param.stream_index = param->stream_index;
-       MMSOUND_STRNCPY(codec_param.stream_type, param->stream_type, MM_SOUND_STREAM_TYPE_LEN);
-       SLOT_LOCK();
-
-#ifdef FOCUS_INTEGRATION
-       /*
-        * Register FOCUS here
-        */
-
-       if (param->session_type != MM_SESSION_TYPE_CALL &&
-               param->session_type != MM_SESSION_TYPE_VIDEOCALL &&
-               param->session_type != MM_SESSION_TYPE_VOIP &&
-               param->session_type != MM_SESSION_TYPE_VOICE_RECOGNITION &&
-               param->enable_session) {
-
-               unsigned int subs_id = 0;
-
-               err = mm_sound_add_device_connected_callback(MM_SOUND_DEVICE_STATE_ACTIVATED_FLAG,
-                                                                                                       (mm_sound_device_connected_cb)_sound_codec_device_connected_callback,
-                                                                                                       (void*) *slotid, &subs_id);
-               if (err) {
-                       debug_error("mm_sound_add_device_connected_callback failed [0x%x]", err);
-                       SLOT_UNLOCK();
-                       return MM_ERROR_POLICY_INTERNAL;
-               }
-               g_slots[*slotid].subs_id = subs_id;
-
-               if ((param->session_options & MM_SESSION_OPTION_PAUSE_OTHERS) ||
-                       param->session_type == MM_SESSION_TYPE_ALARM ||
-                       param->session_type == MM_SESSION_TYPE_NOTIFY ||
-                       param->session_type == MM_SESSION_TYPE_EMERGENCY) {
-                       debug_warning("session option is PAUSE_OTHERS -> acquire focus");
-                       err = mm_sound_focus_get_id((int *)(&param->focus_handle));
-                       err = mm_sound_register_focus_for_session(param->focus_handle, (int)param->param, "media", _sound_codec_focus_callback, (void*)*slotid);
-                       if (err) {
-                               debug_error("mm_sound_register_focus_for_session failed [0x%x]", err);
-                               SLOT_UNLOCK();
-                               return MM_ERROR_POLICY_INTERNAL;
-                       }
-                       err = mm_sound_acquire_focus(param->focus_handle, FOCUS_FOR_BOTH, NULL);
-                       if (err) {
-                               debug_error("mm_sound_acquire_focus failed [0x%x]", err);
-                               err = mm_sound_unregister_focus(param->focus_handle);
-                               SLOT_UNLOCK();
-                               return MM_ERROR_POLICY_INTERNAL;
-                       }
-                       g_slots[*slotid].current_focus_type = FOCUS_FOR_BOTH;
-               } else if (param->session_options & MM_SESSION_OPTION_UNINTERRUPTIBLE) {
-                       /* do nothing */
-                       debug_warning("session option is UNINTERRUPTIBLE, nothing to do with focus");
-               } else {
-                       debug_warning("need to set focus watch callback");
-                       err = mm_sound_set_focus_watch_callback_for_session((int)param->param, FOCUS_FOR_BOTH, _sound_codec_focus_watch_callback,
-                                                                                                                               (void*)*slotid, (int *)(&param->focus_wcb_id));
-                       if (err) {
-                               debug_error("mm_sound_set_focus_watch_callback_for_session failed [0x%x]", err);
-                               SLOT_UNLOCK();
-                               return MM_ERROR_POLICY_INTERNAL;
-                       }
-               }
-       }
-       //
-#endif
-
-       /* Codec id WAV or MP3 */
-       g_slots[*slotid].pluginid = plugin_id;
-       g_slots[*slotid].param    = param->param;               /* This arg is used callback data */
-       g_slots[*slotid].session_type = param->session_type;
-       g_slots[*slotid].session_options = param->session_options;
-#ifdef FOCUS_INTEGRATION
-       g_slots[*slotid].focus_handle = param->focus_handle;
-       g_slots[*slotid].focus_wcb_id = param->focus_wcb_id;
-#endif
-       g_slots[*slotid].enable_session = true;
-       g_slots[*slotid].pid = (int)param->param;
-
-
-       debug_msg("Using Slotid : [%d] Slot Status : [%d]\n", *slotid, g_slots[*slotid].status);
-
-       err = g_plugins[g_slots[*slotid].pluginid].Create(&codec_param, &info, &(g_slots[*slotid].plughandle));
-       debug_msg("Created audio handle : [%d]\n", g_slots[*slotid].plughandle);
-       if (err != MM_ERROR_NONE) {
-               debug_error("Plugin create fail : 0x%08X\n", err);
-               g_slots[*slotid].status = STATUS_IDLE;
-               SLOT_UNLOCK();
-#ifdef FOCUS_INTEGRATION
-               if (param->focus_handle) {
-                       need_focus_unregister = 1;
-               }
-#endif
-               goto cleanup;
-       }
-
-       err = g_plugins[g_slots[*slotid].pluginid].Play(g_slots[*slotid].plughandle);
-       if (err != MM_ERROR_NONE) {
-               debug_error("Fail to play : 0x%08X\n", err);
-               g_plugins[g_slots[*slotid].pluginid].Destroy(g_slots[*slotid].plughandle);
-#ifdef FOCUS_INTEGRATION
-               if (param->focus_handle) {
-                       need_focus_unregister = 1;
-               }
-#endif
-       }
-
-       SLOT_UNLOCK();
-
-cleanup:
-       if (_mm_sound_mgr_codec_slot_is_empty())
-               _mm_sound_mgr_codec_shutdown_timer_start();
-
-#ifdef FOCUS_INTEGRATION
-       if (param->session_type != MM_SESSION_TYPE_CALL &&
-               param->session_type != MM_SESSION_TYPE_VIDEOCALL &&
-               param->session_type != MM_SESSION_TYPE_VOIP &&
-               param->session_type != MM_SESSION_TYPE_VOICE_RECOGNITION &&
-               param->enable_session &&
-               need_focus_unregister == 1) {
-
-               if ((param->session_options & MM_SESSION_OPTION_PAUSE_OTHERS) ||
-                       param->session_type == MM_SESSION_TYPE_ALARM ||
-                       param->session_type == MM_SESSION_TYPE_NOTIFY ||
-                       param->session_type == MM_SESSION_TYPE_EMERGENCY) {
-                       err = mm_sound_release_focus(param->focus_handle, FOCUS_FOR_BOTH, NULL);
-                       if (mm_sound_unregister_focus(param->focus_handle) || err) {
-                               debug_error("focus cleaning up failed[0x%x]", err);
-                               return MM_ERROR_POLICY_INTERNAL;
-                       }
-               } else if (~(param->session_options & MM_SESSION_OPTION_PAUSE_OTHERS)) {
-                       err = mm_sound_unset_focus_watch_callback(param->focus_wcb_id);
-                       if (err) {
-                               debug_error("focus watch cleaning up failed[0x%x]", err);
-                               return MM_ERROR_POLICY_INTERNAL;
-                       }
-               }
-       }
-#endif
-
-
-#ifdef DEBUG_DETAIL
-       debug_leave();
-#endif
-
-       return err;
-}
-
 int MMSoundMgrCodecPlayWithStreamInfo(int *slotid, const mmsound_mgr_codec_param_t *param)
 {
        int plugin_id = 0;
@@ -524,13 +274,13 @@ int MMSoundMgrCodecPlayWithStreamInfo(int *slotid, const mmsound_mgr_codec_param
 
        err = g_plugins[plugin_id].Parse(param->pfilename, &info);
        if (err != MM_ERROR_NONE) {
-               debug_error("Could not parse file [%s] by plugin[%d]\n", param->pfilename, plugin_id);
+               debug_error("Could not parse file [%s] by plugin[%d]", param->pfilename, plugin_id);
                goto cleanup;
        }
 
        err = _mm_sound_mgr_codec_slot_get_empty(slotid);
        if (err != MM_ERROR_NONE || *slotid < 0) {
-               debug_error("Empty g_slot is not found\n");
+               debug_error("Empty g_slot is not found");
                goto cleanup;
        }
 
@@ -542,19 +292,19 @@ int MMSoundMgrCodecPlayWithStreamInfo(int *slotid, const mmsound_mgr_codec_param
        codec_param.param = *slotid;
        codec_param.pid = (int)param->param;
        codec_param.stream_index = param->stream_index;
-       MMSOUND_STRNCPY(codec_param.stream_type, param->stream_type, MM_SOUND_STREAM_TYPE_LEN);
+       MMSOUND_STRNCPY(codec_param.stream_type, param->stream_type, MAX_STREAM_TYPE_LEN);
        SLOT_LOCK();
 
        /* Codec id WAV or MP3 */
        g_slots[*slotid].pluginid = plugin_id;
        g_slots[*slotid].param    = param->param;               /* This arg is used callback data */
 
-       debug_msg("Using Slotid : [%d] Slot Status : [%d]\n", *slotid, g_slots[*slotid].status);
+       debug_msg("Using Slotid : [%d] Slot Status : [%d]", *slotid, g_slots[*slotid].status);
 
        err = g_plugins[g_slots[*slotid].pluginid].Create(&codec_param, &info, &(g_slots[*slotid].plughandle));
-       debug_msg("Created audio handle : [%p]\n", g_slots[*slotid].plughandle);
+       debug_msg("Created audio handle : [%p]", g_slots[*slotid].plughandle);
        if (err != MM_ERROR_NONE) {
-               debug_error("Plugin create fail : 0x%08X\n", err);
+               debug_error("Plugin create fail : 0x%08X", err);
                g_slots[*slotid].status = STATUS_IDLE;
                SLOT_UNLOCK();
                goto cleanup;
@@ -562,7 +312,7 @@ int MMSoundMgrCodecPlayWithStreamInfo(int *slotid, const mmsound_mgr_codec_param
 
        err = g_plugins[g_slots[*slotid].pluginid].Play(g_slots[*slotid].plughandle);
        if (err != MM_ERROR_NONE) {
-               debug_error("Fail to play : 0x%08X\n", err);
+               debug_error("Fail to play : 0x%08X", err);
                g_plugins[g_slots[*slotid].pluginid].Destroy(g_slots[*slotid].plughandle);
        }
 
@@ -580,198 +330,9 @@ cleanup:
 
 }
 
-#define DTMF_PLUGIN_COUNT 2
-int MMSoundMgrCodecPlayDtmf(int *slotid, const mmsound_mgr_codec_param_t *param)
-{
-       int plugin_id = 0;
-       int *codec_type;
-       mmsound_codec_info_t info;
-       mmsound_codec_param_t codec_param;
-       int err = MM_ERROR_NONE;
-#ifdef FOCUS_INTEGRATION
-       int need_focus_unregister = 0;
-#endif
-
-#ifdef DEBUG_DETAIL
-       debug_enter();
-#endif
-
-       for (plugin_id = 0; g_plugins[plugin_id].GetSupportTypes; plugin_id++) {
-               /* Find codec */
-               codec_type = g_plugins[plugin_id].GetSupportTypes();
-               if (codec_type && (MM_SOUND_SUPPORTED_CODEC_DTMF == codec_type[0]))
-                       break;
-       }
-
-       /*The count num means codec type DTMF */
-       debug_msg("DTMF[%d] Repeat[%d] Volume[%f] plugin_codec[%d]\n", param->tone, param->repeat_count, param->volume, plugin_id);
-
-       if (g_plugins[plugin_id].GetSupportTypes == NULL) {     /* Codec not found */
-               debug_error("unsupported file type %d\n", plugin_id);
-               printf("unsupported file type %d\n", plugin_id);
-               err = MM_ERROR_SOUND_UNSUPPORTED_MEDIA_TYPE;
-               goto cleanup;
-       }
-
-#ifdef DEBUG_DETAIL
-       debug_msg("Get New handle\n");
-#endif
-
-       err = _mm_sound_mgr_codec_slot_get_empty(slotid);
-       if (err != MM_ERROR_NONE || *slotid < 0) {
-               debug_error("Empty g_slot is not found\n");
-               goto cleanup;
-       }
-
-       codec_param.tone = param->tone;
-       codec_param.volume_config = param->volume_config;
-       codec_param.repeat_count = param->repeat_count;
-       codec_param.volume = param->volume;
-       codec_param.stop_cb = _MMSoundMgrCodecStopCallback;
-       codec_param.param = *slotid;
-       codec_param.pid = (int)param->param;
-       codec_param.stream_index = param->stream_index;
-       MMSOUND_STRNCPY(codec_param.stream_type, param->stream_type, MM_SOUND_STREAM_TYPE_LEN);
-       SLOT_LOCK();
-
-#ifdef FOCUS_INTEGRATION
-       //
-       /*
-        * Register FOCUS here
-        */
-
-       if (param->session_type != MM_SESSION_TYPE_CALL &&
-               param->session_type != MM_SESSION_TYPE_VIDEOCALL &&
-               param->session_type != MM_SESSION_TYPE_VOIP &&
-               param->session_type != MM_SESSION_TYPE_VOICE_RECOGNITION &&
-               param->enable_session) {
-
-               unsigned int subs_id = 0;
-
-               err = mm_sound_add_device_connected_callback(MM_SOUND_DEVICE_STATE_ACTIVATED_FLAG,
-                                                                                                       (mm_sound_device_connected_cb)_sound_codec_device_connected_callback,
-                                                                                                       (void*) *slotid, &subs_id);
-               if (err) {
-                       debug_error("mm_sound_add_device_connected_callback failed [0x%x]", err);
-                       SLOT_UNLOCK();
-                       return MM_ERROR_POLICY_INTERNAL;
-               }
-               g_slots[*slotid].subs_id = subs_id;
-
-               if ((param->session_options & MM_SESSION_OPTION_PAUSE_OTHERS) ||
-                       param->session_type == MM_SESSION_TYPE_ALARM ||
-                       param->session_type == MM_SESSION_TYPE_NOTIFY ||
-                       param->session_type == MM_SESSION_TYPE_EMERGENCY) {
-                       debug_warning("session option is PAUSE_OTHERS -> acquire focus");
-                       err = mm_sound_focus_get_id((int *)(&param->focus_handle));
-                       err = mm_sound_register_focus_for_session(param->focus_handle, (int)param->param, "media", _sound_codec_focus_callback, (void*)*slotid);
-                       if (err) {
-                               debug_error("mm_sound_register_focus failed [0x%x]", err);
-                               SLOT_UNLOCK();
-                               return MM_ERROR_POLICY_INTERNAL;
-                       }
-                       err = mm_sound_acquire_focus(param->focus_handle, FOCUS_FOR_BOTH, NULL);
-                       if (err) {
-                               debug_error("mm_sound_acquire_focus failed [0x%x]", err);
-                               err = mm_sound_unregister_focus(param->focus_handle);
-                               SLOT_UNLOCK();
-                               return MM_ERROR_POLICY_INTERNAL;
-                       }
-                       g_slots[*slotid].current_focus_type = FOCUS_FOR_BOTH;
-               } else if (param->session_options & MM_SESSION_OPTION_UNINTERRUPTIBLE) {
-                       /* do nothing */
-                       debug_warning("session option is UNINTERRUPTIBLE, nothing to do with focus");
-               } else {
-                       debug_warning("need to set focus watch callback");
-                       err = mm_sound_set_focus_watch_callback_for_session((int)param->param, FOCUS_FOR_BOTH, _sound_codec_focus_watch_callback,
-                                                                                                                               (void*)*slotid, (int *)(&param->focus_wcb_id));
-                       if (err) {
-                               debug_error("mm_sound_set_focus_watch_callback failed [0x%x]", err);
-                               SLOT_UNLOCK();
-                               return MM_ERROR_POLICY_INTERNAL;
-                       }
-               }
-       }
-#endif
-
-       g_slots[*slotid].pluginid = plugin_id;
-       g_slots[*slotid].param    = param->param;               /* This arg is used callback data */
-       g_slots[*slotid].session_type = param->session_type;
-       g_slots[*slotid].session_options = param->session_options;
-#ifdef FOCUS_INTEGRATION
-       g_slots[*slotid].focus_handle= param->focus_handle;
-       g_slots[*slotid].focus_wcb_id= param->focus_wcb_id;
-#endif
-       g_slots[*slotid].enable_session = param->enable_session;
-       g_slots[*slotid].pid = (int)param->param;
-
-#ifdef DEBUG_DETAIL
-       debug_msg("Using Slotid : [%d] Slot Status : [%d]\n", *slotid, g_slots[*slotid].status);
-#endif
-
-       err = g_plugins[g_slots[*slotid].pluginid].Create(&codec_param, &info, &(g_slots[*slotid].plughandle));
-       debug_msg("Created audio handle : [%d]\n", g_slots[*slotid].plughandle);
-       if (err != MM_ERROR_NONE) {
-               debug_error("Plugin create fail : 0x%08X\n", err);
-               g_slots[*slotid].status = STATUS_IDLE;
-               SLOT_UNLOCK();
-#ifdef FOCUS_INTEGRATION
-               need_focus_unregister = 1;
-#endif
-               goto cleanup;
-       }
-
-       err = g_plugins[g_slots[*slotid].pluginid].Play(g_slots[*slotid].plughandle);
-       if (err != MM_ERROR_NONE) {
-               debug_error("Fail to play : 0x%08X\n", err);
-               g_plugins[g_slots[*slotid].pluginid].Destroy(g_slots[*slotid].plughandle);
-#ifdef FOCUS_INTEGRATION
-               need_focus_unregister = 1;
-#endif
-       }
-
-       SLOT_UNLOCK();
-       debug_msg("Using Slotid : [%d] Slot Status : [%d]\n", *slotid, g_slots[*slotid].status);
-
-cleanup:
-#ifdef FOCUS_INTEGRATION
-       if (param->session_type != MM_SESSION_TYPE_CALL &&
-               param->session_type != MM_SESSION_TYPE_VIDEOCALL &&
-               param->session_type != MM_SESSION_TYPE_VOIP &&
-               param->session_type != MM_SESSION_TYPE_VOICE_RECOGNITION &&
-               param->enable_session &&
-               need_focus_unregister == 1) {
-
-               if ((param->session_options & MM_SESSION_OPTION_PAUSE_OTHERS) ||
-                       param->session_type == MM_SESSION_TYPE_ALARM ||
-                       param->session_type == MM_SESSION_TYPE_NOTIFY ||
-                       param->session_type == MM_SESSION_TYPE_EMERGENCY) {
-                       err = mm_sound_release_focus(param->focus_handle, FOCUS_FOR_BOTH, NULL);
-                       if (mm_sound_unregister_focus(param->focus_handle) || err) {
-                               debug_error("focus cleaning up failed[0x%x]", err);
-                               return MM_ERROR_POLICY_INTERNAL;
-                       }
-               } else if (~(param->session_options & MM_SESSION_OPTION_PAUSE_OTHERS)) {
-                       err = mm_sound_unset_focus_watch_callback(param->focus_wcb_id);
-                       if (err) {
-                               debug_error("focus watch cleaning up failed[0x%x]", err);
-                               return MM_ERROR_POLICY_INTERNAL;
-                       }
-               }
-       }
-#endif
-
-#ifdef DEBUG_DETAIL
-       debug_leave();
-#endif
-
-       return err;
-}
-
 int MMSoundMgrCodecPlayDtmfWithStreamInfo(int *slotid, const mmsound_mgr_codec_param_t *param)
 {
        int plugin_id = 0;
-       int *codec_type;
        mmsound_codec_info_t info;
        mmsound_codec_param_t codec_param;
        int err = MM_ERROR_NONE;
@@ -779,32 +340,30 @@ int MMSoundMgrCodecPlayDtmfWithStreamInfo(int *slotid, const mmsound_mgr_codec_p
 #ifdef DEBUG_DETAIL
        debug_enter();
 #endif
-
-       for (plugin_id = 0; g_plugins[plugin_id].GetSupportTypes; plugin_id++) {
-               /* Find codec */
-               codec_type = g_plugins[plugin_id].GetSupportTypes();
-               if (codec_type && (MM_SOUND_SUPPORTED_CODEC_DTMF == codec_type[0]))
-                       break;
+       plugin_id = _MMSoundMgrCodecFindCodecPluginID(MM_SOUND_SUPPORTED_CODEC_DTMF);
+       if (plugin_id == -1) {
+               debug_error("Could not find proper codec plugin!!!");
+               err = MM_ERROR_SOUND_INTERNAL;
+               goto cleanup;
        }
 
        /*The count num means codec type DTMF */
-       debug_msg("DTMF[%d] Repeat[%d] Volume[%f] plugin_codec[%d]\n", param->tone, param->repeat_count, param->volume, plugin_id);
+       debug_msg("DTMF[%d] Repeat[%d] Volume[%f] plugin_codec[%d]", param->tone, param->repeat_count, param->volume, plugin_id);
 
        if (g_plugins[plugin_id].GetSupportTypes == NULL) { /* Codec not found */
-               debug_error("unsupported file type %d\n", plugin_id);
-               printf("unsupported file type %d\n", plugin_id);
+               debug_error("unsupported file type %d", plugin_id);
+               printf("unsupported file type %d", plugin_id);
                err = MM_ERROR_SOUND_UNSUPPORTED_MEDIA_TYPE;
                goto cleanup;
        }
 
 #ifdef DEBUG_DETAIL
-       debug_msg("Get New handle\n");
+       debug_msg("Get New handle");
 #endif
 
        err = _mm_sound_mgr_codec_slot_get_empty(slotid);
-       if (err != MM_ERROR_NONE || *slotid < 0)
-       {
-               debug_error("Empty g_slot is not found\n");
+       if (err != MM_ERROR_NONE || *slotid < 0) {
+               debug_error("Empty g_slot is not found");
                goto cleanup;
        }
 
@@ -816,21 +375,20 @@ int MMSoundMgrCodecPlayDtmfWithStreamInfo(int *slotid, const mmsound_mgr_codec_p
        codec_param.pid = (int)param->param;
        codec_param.volume_config = -1; //setting volume config to -1 since using stream info instead of volume type
        codec_param.stream_index = param->stream_index;
-       MMSOUND_STRNCPY(codec_param.stream_type, param->stream_type, MM_SOUND_STREAM_TYPE_LEN);
+       MMSOUND_STRNCPY(codec_param.stream_type, param->stream_type, MAX_STREAM_TYPE_LEN);
        SLOT_LOCK();
 
        g_slots[*slotid].pluginid = plugin_id;
        g_slots[*slotid].param    = param->param;               /* This arg is used callback data */
-       g_slots[*slotid].enable_session = param->enable_session;
 
 #ifdef DEBUG_DETAIL
-       debug_msg("Using Slotid : [%d] Slot Status : [%d]\n", *slotid, g_slots[*slotid].status);
+       debug_msg("Using Slotid : [%d] Slot Status : [%d]", *slotid, g_slots[*slotid].status);
 #endif
 
        err = g_plugins[g_slots[*slotid].pluginid].Create(&codec_param, &info, &(g_slots[*slotid].plughandle));
-       debug_msg("Created audio handle : [%d]\n", g_slots[*slotid].plughandle);
+       debug_msg("Created audio handle : [%p]", g_slots[*slotid].plughandle);
        if (err != MM_ERROR_NONE) {
-               debug_error("Plugin create fail : 0x%08X\n", err);
+               debug_error("Plugin create fail : 0x%08X", err);
                g_slots[*slotid].status = STATUS_IDLE;
                SLOT_UNLOCK();
                goto cleanup;
@@ -838,12 +396,12 @@ int MMSoundMgrCodecPlayDtmfWithStreamInfo(int *slotid, const mmsound_mgr_codec_p
 
        err = g_plugins[g_slots[*slotid].pluginid].Play(g_slots[*slotid].plughandle);
        if (err != MM_ERROR_NONE) {
-               debug_error("Fail to play : 0x%08X\n", err);
+               debug_error("Fail to play : 0x%08X", err);
                g_plugins[g_slots[*slotid].pluginid].Destroy(g_slots[*slotid].plughandle);
        }
 
        SLOT_UNLOCK();
-       debug_msg("Using Slotid : [%d] Slot Status : [%d]\n", *slotid, g_slots[*slotid].status);
+       debug_msg("Using Slotid : [%d] Slot Status : [%d]", *slotid, g_slots[*slotid].status);
 
 cleanup:
 #ifdef DEBUG_DETAIL
@@ -858,166 +416,60 @@ int MMSoundMgrCodecStop(const int slotid)
 {
        int err = MM_ERROR_NONE;
 
-       debug_enter("(Slotid : [%d])\n", slotid);
+       debug_enter("(Slotid : [%d])", slotid);
 
-       if (slotid < 0 || MANAGER_HANDLE_MAX <= slotid) {
+       if (slotid < 0 || MANAGER_HANDLE_MAX <= slotid)
                return MM_ERROR_INVALID_ARGUMENT;
-       }
 
        SLOT_LOCK();
        if (g_slots[slotid].status == STATUS_IDLE) {
                err = MM_ERROR_SOUND_INVALID_STATE;
-               debug_warning("The playing slots is not found, Slot ID : [%d]\n", slotid);
+               debug_warning("The playing slots is not found, Slot ID : [%d]", slotid);
                goto cleanup;
        }
 #ifdef DEBUG_DETAIL
-       debug_msg("Found slot, Slotid [%d] State [%d]\n", slotid, g_slots[slotid].status);
+       debug_msg("Found slot, Slotid [%d] State [%d]", slotid, g_slots[slotid].status);
 #endif
 
-       g_slots[slotid].stop_by_user = true;
-
        err = g_plugins[g_slots[slotid].pluginid].Stop(g_slots[slotid].plughandle);
-       if (err != MM_ERROR_NONE) {
-               debug_error("Fail to STOP Code : 0x%08X\n", err);
-       }
+       if (err != MM_ERROR_NONE)
+               debug_error("Fail to STOP Code : 0x%08X", err);
 
 cleanup:
        SLOT_UNLOCK();
-       debug_leave("(err : 0x%08X)\n", err);
+       debug_leave("(err : 0x%08X)", err);
 
        return err;
 }
-#ifdef FOCUS_INTEGRATION
-int MMSoundMgrCodecClearFocus(int pid)
-{
-       int err = MM_ERROR_NONE;
-       int slotid;
-
-       debug_enter("(pid : [%d])\n", pid);
 
-       SLOT_LOCK();
-
-       for (slotid = 0 ; slotid < MANAGER_HANDLE_MAX ; slotid++) {
-               if (g_slots[slotid].pid == pid) {
-                       if (g_slots[slotid].focus_handle || g_slots[slotid].focus_wcb_id) {
-                               if (g_slots[slotid].session_type != MM_SESSION_TYPE_CALL &&
-                                       g_slots[slotid].session_type != MM_SESSION_TYPE_VIDEOCALL &&
-                                       g_slots[slotid].session_type != MM_SESSION_TYPE_VOIP &&
-                                       g_slots[slotid].session_type != MM_SESSION_TYPE_VOICE_RECOGNITION &&
-                                       g_slots[slotid].enable_session ) {
-                                       if ((g_slots[slotid].session_options & MM_SESSION_OPTION_PAUSE_OTHERS) ||
-                                               g_slots[slotid].session_type == MM_SESSION_TYPE_ALARM ||
-                                               g_slots[slotid].session_type == MM_SESSION_TYPE_NOTIFY ||
-                                               g_slots[slotid].session_type == MM_SESSION_TYPE_EMERGENCY) {
-                                               err = mm_sound_release_focus(g_slots[slotid].focus_handle, FOCUS_FOR_BOTH, NULL);
-                                               if (err) {
-                                                       debug_error("mm_sound_release_focus failed [0x%x]", err);
-                                               }
-                                               if (mm_sound_unregister_focus(g_slots[slotid].focus_handle) || err) {
-                                                       debug_error("Focus clean up failed [0x%x]", err);
-                                                       err = MM_ERROR_POLICY_INTERNAL;
-                                                       goto cleanup;
-                                               }
-                                       } else if (~(g_slots[slotid].session_options & MM_SESSION_OPTION_PAUSE_OTHERS)) {
-                                               err = mm_sound_unset_focus_watch_callback(g_slots[slotid].focus_wcb_id);
-                                               if (err) {
-                                                       debug_error("mm_sound_unset_focus_watch_callback failed [0x%x]", err);
-                                                       err = MM_ERROR_POLICY_INTERNAL;
-                                                       goto cleanup;
-                                               }
-                                       }
-                               }
-                               if (mm_sound_remove_device_connected_callback(g_slots[slotid].subs_id) != MM_ERROR_NONE)
-                                       debug_error("mm_sound_remove_device_connected_callback() failed");
-                               g_slots[slotid].focus_handle = 0;
-                               g_slots[slotid].focus_wcb_id = 0;
-                               g_slots[slotid].subs_id = 0;
-                       }
-               }
-       }
-
-cleanup:
-       SLOT_UNLOCK();
-       debug_leave("(err : 0x%08X)\n", err);
-
-       return err;
-}
-#endif
-
-static int _MMSoundMgrCodecStopCallback(int param)
+static int _MMSoundMgrCodecStopCallback(int param, bool stop_by_user)
 {
        int err = MM_ERROR_NONE;
 
-       debug_enter("(Slot : %d)\n", param);
-
        if (param < 0 || param >= MANAGER_HANDLE_MAX) {
-               debug_error ("Slot index param [%d] is invalid", param);
+               debug_error("Slot(%d) is invalid", param);
                return MM_ERROR_INVALID_ARGUMENT;
        }
 
-       if (g_slots[param].stop_by_user == false) {
-               SLOT_LOCK();
-       }
+       debug_enter("Slot(%d) : stop-by-user(%d)", param, stop_by_user);
 
-#ifdef FOCUS_INTEGRATION
-       /*
-        * Unregister FOCUS here
-        */
-       debug_msg("[CODEC MGR] enable_session %d ",g_slots[param].enable_session);
-
-       if (g_slots[param].focus_handle || g_slots[param].focus_wcb_id) {
-               if (g_slots[param].session_type != MM_SESSION_TYPE_CALL &&
-                       g_slots[param].session_type != MM_SESSION_TYPE_VIDEOCALL &&
-                       g_slots[param].session_type != MM_SESSION_TYPE_VOIP &&
-                       g_slots[param].session_type != MM_SESSION_TYPE_VOICE_RECOGNITION &&
-                       g_slots[param].enable_session ) {
-                       if ((g_slots[param].session_options & MM_SESSION_OPTION_PAUSE_OTHERS) ||
-                               g_slots[param].session_type == MM_SESSION_TYPE_ALARM ||
-                               g_slots[param].session_type == MM_SESSION_TYPE_NOTIFY ||
-                               g_slots[param].session_type == MM_SESSION_TYPE_EMERGENCY) {
-                               if (g_slots[param].current_focus_type != FOCUS_NONE) {
-                                       err = mm_sound_release_focus(g_slots[param].focus_handle, g_slots[param].current_focus_type, NULL);
-                                       if (err) {
-                                               debug_error("mm_sound_release_focus failed [0x%x]", err);
-                                       }
-                               }
-                               if (mm_sound_unregister_focus(g_slots[param].focus_handle) || err) {
-                                       debug_error("Focus clean up failed [0x%x]", err);
-                                       err = MM_ERROR_POLICY_INTERNAL;
-                                       goto finish;
-                               }
-                       } else if (~(g_slots[param].session_options & MM_SESSION_OPTION_PAUSE_OTHERS)) {
-                               err = mm_sound_unset_focus_watch_callback(g_slots[param].focus_wcb_id);
-                               if (err) {
-                                       debug_error("mm_sound_unset_focus_watch_callback failed [0x%x]", err);
-                                       err = MM_ERROR_POLICY_INTERNAL;
-                                       goto finish;
-                               }
-                       }
-               }
-               if (mm_sound_remove_device_connected_callback(g_slots[param].subs_id) != MM_ERROR_NONE)
-                       debug_error("mm_sound_remove_device_connected_callback() failed");
-       }
-#endif
-
-       if (g_slots[param].stop_by_user == false) {
+       if (!stop_by_user) {
+               SLOT_LOCK();
                __mm_sound_mgr_ipc_notify_play_file_end(param);
-               debug_msg("Client callback msg_type (instance) : [%d]\n", (int)g_slots[param].param);
+               debug_msg("Client callback msg_type (instance) : [%d]", (int)g_slots[param].param);
        }
 
-       debug_msg("Handle allocated handle : [0x%08X]\n", g_slots[param].plughandle);
+       debug_msg("Handle allocated handle : [%p]", g_slots[param].plughandle);
        err = g_plugins[g_slots[param].pluginid].Destroy(g_slots[param].plughandle);
        if (err < 0)
-               debug_critical("[CODEC MGR] Fail to destroy slot number : [%d] err [0x%x]\n", param, err);
+               debug_critical("Slot(%d) : Fail to destroy slot, err [0x%x]", param, err);
 
-       _mm_sound_mgr_codec_slot_clear(param);
+       _mm_sound_mgr_codec_slot_clear(param, true);
        if (_mm_sound_mgr_codec_slot_is_empty())
                _mm_sound_mgr_codec_shutdown_timer_start();
 
-finish:
-       if (g_slots[param].stop_by_user == false) {
+       if (!stop_by_user)
                SLOT_UNLOCK();
-       }
 
        debug_fleave();
 
@@ -1043,20 +495,20 @@ static int _MMSoundMgrCodecRegisterInterface(MMSoundPluginType *plugin)
        }
 
        if (plugin_id == MM_SOUND_SUPPORTED_CODEC_NUM) {
-               debug_critical("The plugin support type is not valid\n");
+               debug_critical("The plugin support type is not valid");
                return MM_ERROR_COMMON_OUT_OF_RANGE;
        }
 
        err = MMSoundPluginGetSymbol(plugin, CODEC_GET_INTERFACE_FUNC_NAME, &getinterface);
        if (err != MM_ERROR_NONE) {
-               debug_error("Get Symbol CODEC_GET_INTERFACE_FUNC_NAME is fail : %x\n", err);
+               debug_error("Get Symbol CODEC_GET_INTERFACE_FUNC_NAME is fail : %x", err);
                goto cleanup;
        }
-       debug_msg("interface[%p] empty_slot[%d]\n", getinterface, plugin_id);
+       debug_msg("interface[%p] empty_slot[%d]", getinterface, plugin_id);
 
        err = MMSoundPlugCodecCastGetInterface(getinterface)(&g_plugins[plugin_id]);
        if (err != MM_ERROR_NONE) {
-               debug_error("Get interface fail : %x\n", err);
+               debug_error("Get interface fail : %x", err);
 
 cleanup:
                /* If error occur, clean interface */