Add null check when destroy sound manager vstream
[platform/core/multimedia/libmm-radio.git] / src / mm_radio_priv_hal.c
old mode 100755 (executable)
new mode 100644 (file)
index 1e7bb92..ac1cd92
@@ -123,20 +123,24 @@ void _mmradio_seek_cancel(mm_radio_t *radio);
 static void __mmradio_seek_thread(mm_radio_t *radio);
 static void __mmradio_scan_thread(mm_radio_t *radio);
 static bool __is_tunable_frequency(mm_radio_t *radio, int freq);
-
-#ifdef TIZEN_FEATURE_SOUND_FOCUS
-static void __mmradio_sound_focus_cb(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 *additional_info, void *user_data);
-static void __mmradio_sound_focus_watch_cb(int id, mm_sound_focus_type_e focus_type,
-       mm_sound_focus_state_e focus_state, const char *reason_for_change,
-       const char *additional_info, void *user_data);
-#endif
+static int __mmradio_create_threads(mm_radio_t *radio);
+static void __mmradio_destroy_threads(mm_radio_t *radio);
+static int __mmradio_create_thread_type(mm_radio_t *radio, MMRadioThreadTypes type);
+static void __mmradio_destroy_thread_type(mm_radio_t *radio, MMRadioThreadTypes type);
 
 static void __mmradio_volume_changed_cb(volume_type_t type, unsigned int volume, void *user_data);
 static int __mmradio_set_media_volume(mm_radio_t *radio, unsigned int level);
 static int __resource_release_cb(mm_resource_manager_h rm,
        mm_resource_manager_res_h res, void *user_data);
+static void __mmradio_msg_thread(mm_radio_t *radio);
+static void __mmradio_msg_push(mm_radio_t *radio, MMRadioMsgTypes msg_type, int msg_data);
+
+typedef void (*thread_function)(mm_radio_t *);
+thread_function __mmradio_thread_function[] = {
+       &__mmradio_msg_thread,
+       &__mmradio_seek_thread,
+       &__mmradio_scan_thread
+};
 
 int _mmradio_apply_region(mm_radio_t *radio, MMRadioRegionType region, bool update)
 {
@@ -190,23 +194,23 @@ int _mmradio_create_radio(mm_radio_t *radio)
 
        /* set default value */
        radio->freq = DEFAULT_FREQ;
-#ifdef TIZEN_FEATURE_SOUND_FOCUS
-       memset(&radio->sound_focus, 0, sizeof(mm_radio_sound_focus));
-#endif
        memset(&radio->region_setting, 0, sizeof(MMRadioRegion_t));
        radio->local_volume = 1.0;
-
-       /* create command lock */
-       ret = pthread_mutex_init(&radio->cmd_lock, NULL);
-       if (ret) {
-               MMRADIO_LOG_ERROR("failed to create mutex");
-               return MM_ERROR_RADIO_INTERNAL;
+       radio->vstream = NULL;
+       radio->stream_info = NULL;
+
+       /* create msg queue for msg thread */
+       radio->msg_queue = g_async_queue_new();
+       if (!radio->msg_queue) {
+               MMRADIO_LOG_ERROR("failed to get msg g_async_queue_new");
+               return  MM_ERROR_RADIO_INTERNAL;
        }
 
-       ret = pthread_mutex_init(&radio->volume_lock, NULL);
+       /* create mutex and thread */
+       ret = __mmradio_create_threads(radio);
        if (ret) {
-               MMRADIO_LOG_ERROR("failed to create volume mutex");
-               return MM_ERROR_RADIO_INTERNAL;
+               MMRADIO_LOG_ERROR("failed to create threads");
+               goto ERROR_THREAD;
        }
 
        MMRADIO_SET_STATE(radio, MM_RADIO_STATE_NULL);
@@ -216,31 +220,28 @@ int _mmradio_create_radio(mm_radio_t *radio)
                        __resource_release_cb, radio, &radio->resource_manager);
        if (ret) {
                MMRADIO_LOG_ERROR("failed to create resource manager");
-               return MM_ERROR_RADIO_INTERNAL;
-       }
-
-#ifdef TIZEN_FEATURE_SOUND_FOCUS
-       ret = mmradio_sound_focus_register(&radio->sound_focus,
-               (mm_sound_focus_changed_cb)__mmradio_sound_focus_cb,
-               (mm_sound_focus_changed_watch_cb)__mmradio_sound_focus_watch_cb,
-               (void *)radio);
-
-       if (ret) {
-               /* NOTE : we are dealing it as an error since we cannot expect it's behavior */
-               MMRADIO_LOG_ERROR("failed to register sound focus");
-               return MM_ERROR_RADIO_INTERNAL;
+               ret = MM_ERROR_RADIO_INTERNAL;
+               goto ERROR_RESOURCE;
        }
-#endif
 
        ret = radio_hal_interface_init(&(radio->hal_inf));
        if (ret) {
                MMRADIO_LOG_ERROR("failed to init mmradio hal interface");
-               return ret;
+               goto ERROR_HAL_INIT;
        }
 
        MMRADIO_LOG_FLEAVE();
 
        return MM_ERROR_NONE;
+
+ERROR_HAL_INIT:
+       mm_resource_manager_destroy(radio->resource_manager);
+ERROR_RESOURCE:
+       __mmradio_destroy_threads(radio);
+ERROR_THREAD:
+       if (radio->msg_queue)
+               g_async_queue_unref(radio->msg_queue);
+       return ret;
 }
 
 int _mmradio_realize(mm_radio_t *radio)
@@ -256,12 +257,6 @@ int _mmradio_realize(mm_radio_t *radio)
        MMRADIO_CHECK_INSTANCE(radio);
        MMRADIO_CHECK_STATE_RETURN_IF_FAIL(radio, MMRADIO_COMMAND_REALIZE);
 
-       ret = pthread_mutex_init(&radio->seek_cancel_mutex, NULL);
-       if (ret < 0) {
-               MMRADIO_LOG_ERROR("failed to create seek cancel mutex %d", ret);
-               return MM_ERROR_RADIO_INTERNAL;
-       }
-
        if (radio->region_setting.country == MM_RADIO_REGION_GROUP_NONE) {
                /* not initialized      yet. set it with default region */
                region = RADIO_DEFAULT_REGION;
@@ -335,16 +330,17 @@ int _mmradio_unrealize(mm_radio_t *radio)
        /*Stop radio if started*/
        _mmradio_stop(radio);
 
-       /* close radio device here !!!! */
-       radio_hal_close(radio->hal_inf);
-       radio_hal_unprepare(radio->hal_inf);
 #ifdef TIZEN_FEATURE_SOUND_VSTREAM
-       sound_manager_destroy_virtual_stream(radio->vstream);
-       sound_manager_destroy_stream_information(radio->stream_info);
+       if (radio->vstream) {
+               sound_manager_destroy_virtual_stream(radio->vstream);
+               radio->vstream = NULL;
+       }
+       if (radio->stream_info) {
+               sound_manager_destroy_stream_information(radio->stream_info);
+               radio->stream_info = NULL;
+       }
 #endif
 
-       pthread_mutex_destroy(&radio->seek_cancel_mutex);
-
        MMRADIO_SET_STATE(radio, MM_RADIO_STATE_NULL);
 
        MMRADIO_LOG_FLEAVE();
@@ -362,29 +358,18 @@ int _mmradio_destroy(mm_radio_t *radio)
 
        _mmradio_unrealize(radio);
 
+       /* destroy mutex and thread */
+       __mmradio_destroy_threads(radio);
+
+       if (radio->msg_queue)
+               g_async_queue_unref(radio->msg_queue);
+
        ret = radio_hal_interface_deinit(radio->hal_inf);
        if (ret) {
                MMRADIO_LOG_ERROR("failed to deinitialize radio hal interface");
                return ret;
        }
 
-       /* destroy command lock */
-       ret = pthread_mutex_destroy(&radio->cmd_lock);
-       if (ret)
-               MMRADIO_LOG_ERROR("failed to destory muxtex");
-
-       ret = pthread_mutex_destroy(&radio->volume_lock);
-       if (ret)
-               MMRADIO_LOG_ERROR("failed to destory volume mutex");
-
-#ifdef TIZEN_FEATURE_SOUND_FOCUS
-       ret = mmradio_sound_focus_deregister(&radio->sound_focus);
-       if (ret) {
-               MMRADIO_LOG_ERROR("failed to deregister sound focus");
-               return MM_ERROR_RADIO_INTERNAL;
-       }
-#endif
-
        ret = mm_resource_manager_destroy(radio->resource_manager);
        if (ret) {
                MMRADIO_LOG_ERROR("failed to destroy resource manager");
@@ -469,7 +454,7 @@ int _mmradio_mute(mm_radio_t *radio)
                return ret;
        }
 
-       radio->is_muted = TRUE;
+       radio->is_muted = true;
        MMRADIO_LOG_INFO("Radio mute state [%d]", radio->is_muted);
        MMRADIO_LOG_FLEAVE();
 
@@ -493,7 +478,7 @@ int _mmradio_unmute(mm_radio_t *radio)
                return ret;
        }
 
-       radio->is_muted = FALSE;
+       radio->is_muted = false;
        MMRADIO_LOG_INFO("Radio mute state [%d]", radio->is_muted);
        MMRADIO_LOG_FLEAVE();
 
@@ -546,39 +531,6 @@ int _mmradio_start(mm_radio_t *radio)
 
        MMRADIO_LOG_INFO("now tune to frequency : %d", radio->freq);
 
-#ifdef TIZEN_FEATURE_SOUND_FOCUS
-       if (radio->sound_focus.handle > 0) {
-               ret = mmradio_acquire_sound_focus(&radio->sound_focus);
-               if (ret != MM_ERROR_NONE) {
-                       MMRADIO_LOG_ERROR("failed to acquire sound focus");
-                       return ret;
-               }
-       }
-
-       /* check previous acquired focus */
-       if (radio->sound_focus.watch_id > 0) {
-               char *stream_type = NULL;
-               char *ext_info = NULL;
-               int option = 0;
-
-               ret = mm_sound_get_stream_type_of_acquired_focus(FOCUS_FOR_BOTH, &stream_type, &option, &ext_info);
-               if (ret == MM_ERROR_NONE) {
-                       MMRADIO_LOG_DEBUG("Focus is acquired by stream_type[%s], option[%d], ext_info[%s]",
-                               stream_type, option, ext_info);
-                       if (!strcmp(stream_type, "alarm") || !strcmp(stream_type, "ringtone-voip") ||
-                               !strcmp(stream_type, "ringtone-call") || !strcmp(stream_type, "voip") ||
-                               !strcmp(stream_type, "call-voice") || !strcmp(stream_type, "call-video")) {
-                               MMRADIO_LOG_DEBUG("Blcoked by session policy, focus_acquired_by[%s]", stream_type);
-                               free(ext_info);
-                               free(stream_type);
-                               return MM_ERROR_POLICY_BLOCKED;
-                       }
-                       free(ext_info);
-                       free(stream_type);
-               }
-       }
-#endif
-
        if (!radio->is_ready) {
                ret = mm_resource_manager_mark_for_acquire(radio->resource_manager,
                        MM_RESOURCE_MANAGER_RES_TYPE_RADIO,
@@ -613,7 +565,7 @@ int _mmradio_start(mm_radio_t *radio)
                        MMRADIO_LOG_ERROR("failed to open radio hal");
                        goto error1;
                }
-               radio->is_ready = TRUE;
+               radio->is_ready = true;
        } else {
                MMRADIO_LOG_DEBUG("radio prepared and opened");
        }
@@ -661,7 +613,7 @@ error1:
        radio_hal_close(radio->hal_inf);
 error2:
        radio_hal_unprepare(radio->hal_inf);
-       radio->is_ready = FALSE;
+       radio->is_ready = false;
        return ret;
 }
 
@@ -674,7 +626,6 @@ int _mmradio_stop(mm_radio_t *radio)
        MMRADIO_CHECK_INSTANCE(radio);
        MMRADIO_CHECK_STATE_RETURN_IF_FAIL(radio, MMRADIO_COMMAND_STOP);
 
-       radio->seek_unmute = FALSE;
        /*cancel if any seek*/
        _mmradio_seek_cancel(radio);
 #ifdef TIZEN_FEATURE_SOUND_VSTREAM
@@ -693,49 +644,42 @@ int _mmradio_stop(mm_radio_t *radio)
                return ret;
        }
 
-       /* close radio device here !!!! */
-       ret = radio_hal_close(radio->hal_inf);
-       if (ret == MM_ERROR_NOT_SUPPORT_API) {
-               MMRADIO_LOG_WARNING("radio_hal_close is not supported");
-       } else if (ret != MM_ERROR_NONE) {
-               MMRADIO_LOG_ERROR("failed to close radio hal");
-               return ret;
-       }
-
-       ret = radio_hal_unprepare(radio->hal_inf);
-       if (ret == MM_ERROR_NOT_SUPPORT_API) {
-               MMRADIO_LOG_WARNING("radio_hal_unprepare is not supported");
-       } else if (ret != MM_ERROR_NONE) {
-               MMRADIO_LOG_ERROR("failed to unprepare radio hal");
-               return ret;
-       }
-
-       radio->is_ready = FALSE;
+       if (radio->is_ready) {
+               /* close radio device here !!!! */
+               ret = radio_hal_close(radio->hal_inf);
+               if (ret == MM_ERROR_NOT_SUPPORT_API) {
+                       MMRADIO_LOG_WARNING("radio_hal_close is not supported");
+               } else if (ret != MM_ERROR_NONE) {
+                       MMRADIO_LOG_ERROR("failed to close radio hal");
+                       return ret;
+               }
 
-       if (!radio->interrupted_by_resource_conflict && /* is being released */
-                       radio->radio_resource != NULL) {
-               ret = mm_resource_manager_mark_for_release(radio->resource_manager,
-                               radio->radio_resource);
-               radio->radio_resource = NULL;
-               if (ret != MM_RESOURCE_MANAGER_ERROR_NONE) {
-                       MMRADIO_LOG_ERROR("failed to mark resource for release, ret(0x%x)", ret);
+               ret = radio_hal_unprepare(radio->hal_inf);
+               if (ret == MM_ERROR_NOT_SUPPORT_API) {
+                       MMRADIO_LOG_WARNING("radio_hal_unprepare is not supported");
+               } else if (ret != MM_ERROR_NONE) {
+                       MMRADIO_LOG_ERROR("failed to unprepare radio hal");
                        return ret;
                }
 
-               ret = mm_resource_manager_commit(radio->resource_manager);
-               if (ret != MM_RESOURCE_MANAGER_ERROR_NONE)
-                       MMRADIO_LOG_ERROR("resource manager commit fail");
-       }
+               if (!radio->interrupted_by_resource_conflict && /* is being released */
+                               radio->radio_resource != NULL) {
+                       ret = mm_resource_manager_mark_for_release(radio->resource_manager,
+                                       radio->radio_resource);
+                       radio->radio_resource = NULL;
+                       if (ret != MM_RESOURCE_MANAGER_ERROR_NONE) {
+                               MMRADIO_LOG_ERROR("failed to mark resource for release, ret(0x%x)", ret);
+                               return ret;
+                       }
 
-#ifdef TIZEN_FEATURE_SOUND_FOCUS
-       if (radio->sound_focus.handle > 0) {
-               ret = mmradio_release_sound_focus(&radio->sound_focus);
-               if (ret) {
-                       MMRADIO_LOG_ERROR("failed to release sound focus");
-                       return ret;
+                       ret = mm_resource_manager_commit(radio->resource_manager);
+                       if (ret != MM_RESOURCE_MANAGER_ERROR_NONE)
+                               MMRADIO_LOG_ERROR("resource manager commit fail");
                }
+
+               radio->is_ready = false;
        }
-#endif
+
        MMRADIO_SET_STATE(radio, MM_RADIO_STATE_READY);
 
        MMRADIO_LOG_FLEAVE();
@@ -746,20 +690,22 @@ int _mmradio_stop(mm_radio_t *radio)
 int _mmradio_seek(mm_radio_t *radio, MMRadioSeekDirectionType direction)
 {
        int ret = MM_ERROR_NONE;
+       MMRadioThread_t *p_thread = NULL;
 
        MMRADIO_LOG_FENTER();
 
        MMRADIO_CHECK_INSTANCE(radio);
        MMRADIO_CHECK_STATE_RETURN_IF_FAIL(radio, MMRADIO_COMMAND_SEEK);
 
-       if (radio->is_seeking) {
+       p_thread = &radio->thread[MM_RADIO_THREAD_SEEK];
+       MMRADIO_CHECK_ARG(p_thread);
+
+       if (p_thread->is_running) {
                MMRADIO_LOG_ERROR("[RADIO_ERROR_INVALID_OPERATION]radio is seeking, can't serve another request try again");
                return MM_ERROR_RADIO_INTERNAL;
        }
 
-       radio->seek_unmute = FALSE;
-       radio->is_seeking = TRUE;
-       radio->seek_cancel = FALSE;
+       radio->seek_unmute = false;
 
        if (!radio->is_muted) {
                ret = radio_hal_mute(radio->hal_inf);
@@ -769,30 +715,15 @@ int _mmradio_seek(mm_radio_t *radio, MMRadioSeekDirectionType direction)
                        MMRADIO_LOG_ERROR("failed to set radio hal mute");
                        return ret;
                }
-               radio->seek_unmute = TRUE;
+               radio->seek_unmute = true;
        }
 
        MMRADIO_LOG_INFO("trying to seek. direction[0:UP/1:DOWN) %d", direction);
        radio->seek_direction = direction;
+       p_thread->is_running = true;
+       p_thread->stop = false;
 
-       ret = pthread_create(&radio->seek_thread, NULL, (void *)__mmradio_seek_thread, (void *)radio);
-
-       if (ret) {
-               MMRADIO_LOG_DEBUG("failed to create thread");
-               radio->is_seeking = FALSE;
-               radio->seek_cancel = TRUE;
-               if (radio->seek_unmute) {
-                       ret = radio_hal_mute(radio->hal_inf);
-                       if (ret == MM_ERROR_NOT_SUPPORT_API) {
-                               MMRADIO_LOG_WARNING("radio_hal_mute is not supported");
-                       } else if (ret) {
-                               MMRADIO_LOG_ERROR("failed to set radio hal mute");
-                               radio->seek_unmute = FALSE;
-                               return ret;
-                       }
-               }
-               return MM_ERROR_RADIO_INTERNAL;
-       }
+       MMRADIO_THREAD_SIGNAL(p_thread);
 
        MMRADIO_LOG_FLEAVE();
 
@@ -801,47 +732,30 @@ int _mmradio_seek(mm_radio_t *radio, MMRadioSeekDirectionType direction)
 
 void _mmradio_seek_cancel(mm_radio_t *radio)
 {
-       int ret = MM_ERROR_NONE;
-       char str_error[READ_MAX_BUFFER_SIZE];
        MMRADIO_LOG_FENTER();
 
        MMRADIO_CHECK_INSTANCE_RETURN_VOID(radio);
 
-       /*cancel any outstanding seek request*/
-       radio->seek_cancel = TRUE;
-       if (radio->seek_thread) {
-               ret = pthread_mutex_trylock(&radio->seek_cancel_mutex);
-               MMRADIO_LOG_DEBUG("try lock ret: %s (%d)", strerror_r(ret, str_error, sizeof(str_error)), ret);
-               if (ret == EBUSY) { /* it was already locked by other */
-                       MMRADIO_LOG_WARNING("send SEEK ABORT with FMRX_PROPERTY_SEARCH_ABORT");
-               } else if (ret == 0) {
-                       MMRADIO_LOG_INFO("trylock is successful. unlock now");
-                       pthread_mutex_unlock(&radio->seek_cancel_mutex);
-               } else {
-                       MMRADIO_LOG_ERROR("trylock is failed but Not EBUSY. ret: %d", ret);
-               }
-               MMRADIO_LOG_DEBUG("pthread_join seek_thread");
-               pthread_join(radio->seek_thread, NULL);
-               MMRADIO_LOG_DEBUG("done");
-               radio->is_seeking = FALSE;
-               radio->seek_thread = 0;
-       }
+       /* cancel any outstanding seek request */
+       radio->thread[MM_RADIO_THREAD_SEEK].stop = true;
+
        MMRADIO_LOG_FLEAVE();
 }
 
-
 int _mmradio_start_scan(mm_radio_t *radio)
 {
        int ret = MM_ERROR_NONE;
+       MMRadioThread_t *p_thread = NULL;;
 
        MMRADIO_LOG_FENTER();
 
        MMRADIO_CHECK_INSTANCE(radio);
        MMRADIO_CHECK_STATE_RETURN_IF_FAIL(radio, MMRADIO_COMMAND_START_SCAN);
 
-       int scan_tr_id = 0;
+       p_thread = &radio->thread[MM_RADIO_THREAD_SCAN];
+       MMRADIO_CHECK_ARG(p_thread);
 
-       radio->stop_scan = false;
+       p_thread->stop = false;
 
        if (!radio->is_ready) {
                ret = mm_resource_manager_mark_for_acquire(radio->resource_manager,
@@ -878,17 +792,14 @@ int _mmradio_start_scan(mm_radio_t *radio)
                        MMRADIO_LOG_FLEAVE();
                        return ret;
                }
-               radio->is_ready = TRUE;
+               radio->is_ready = true;
        } else {
                MMRADIO_LOG_DEBUG("radio prepared and opened");
        }
 
-       scan_tr_id = pthread_create(&radio->scan_thread, NULL, (void *)__mmradio_scan_thread, (void *)radio);
+       p_thread->is_running = true;
 
-       if (scan_tr_id != 0) {
-               MMRADIO_LOG_ERROR("failed to create thread : scan");
-               return MM_ERROR_RADIO_NOT_INITIALIZED;
-       }
+       MMRADIO_THREAD_SIGNAL(p_thread);
 
        MMRADIO_SET_STATE(radio, MM_RADIO_STATE_SCANNING);
 
@@ -899,31 +810,12 @@ int _mmradio_start_scan(mm_radio_t *radio)
 
 int _mmradio_stop_scan(mm_radio_t *radio)
 {
-       int ret = 0;
-       char str_error[READ_MAX_BUFFER_SIZE];
        MMRADIO_LOG_FENTER();
 
        MMRADIO_CHECK_INSTANCE(radio);
        MMRADIO_CHECK_STATE_RETURN_IF_FAIL(radio, MMRADIO_COMMAND_STOP_SCAN);
 
-       radio->stop_scan = true;
-
-       if (radio->scan_thread > 0) {
-               /* make sure all the search is stopped else we'll wait till search finish which is not ideal*/
-               ret = pthread_mutex_trylock(&radio->seek_cancel_mutex);
-               MMRADIO_LOG_DEBUG("try lock ret: %s (%d)", strerror_r(ret, str_error, sizeof(str_error)), ret);
-               if (ret == EBUSY) { /* it was already locked by other */
-                       MMRADIO_LOG_WARNING("send SEEK ABORT with FMRX_PROPERTY_SEARCH_ABORT");
-               } else if (ret == 0) {
-                       MMRADIO_LOG_INFO("trylock is successful. unlock now");
-                       pthread_mutex_unlock(&radio->seek_cancel_mutex);
-               } else {
-                       MMRADIO_LOG_ERROR("trylock is failed but Not EBUSY. ret: %d", ret);
-               }
-               MMRADIO_LOG_DEBUG("pthread_join scan_thread");
-               pthread_join(radio->scan_thread, NULL);
-               radio->scan_thread = 0;
-       }
+       radio->thread[MM_RADIO_THREAD_SCAN].stop = true;
 
        MMRADIO_LOG_FLEAVE();
 
@@ -959,144 +851,182 @@ void __mmradio_scan_thread(mm_radio_t *radio)
        int ret = MM_ERROR_NONE;
        int prev_freq = 0;
 
+       MMRadioThread_t *p_thread = NULL;
+
        MMRADIO_LOG_FENTER();
        MMRADIO_CHECK_INSTANCE_RETURN_VOID(radio);
 
-       ret = radio_hal_mute(radio->hal_inf);
-
-       if (ret == MM_ERROR_NOT_SUPPORT_API) {
-               MMRADIO_LOG_WARNING("radio_hal_mute is not supported");
-       } else if (ret != MM_ERROR_NONE) {
-               MMRADIO_LOG_ERROR("faied to set radio hal mute");
-               goto FINISHED;
-       }
-       ret = radio_hal_set_frequency(radio->hal_inf, radio->region_setting.band_min);
-
-       if (ret != MM_ERROR_NONE)
-               goto FINISHED;
+       p_thread = &radio->thread[MM_RADIO_THREAD_SCAN];
+       MMRADIO_CHECK_ARG_RETURN_VOID(p_thread);
 
-       MMRADIO_POST_MSG(radio, MM_MESSAGE_RADIO_SCAN_START, NULL);
-       MMRADIO_SET_STATE(radio, MM_RADIO_STATE_SCANNING);
+       MMRADIO_THREAD_LOCK(p_thread);
+       MMRADIO_THREAD_SIGNAL(p_thread);
+       MMRADIO_THREAD_UNLOCK(p_thread);
 
-       while (!radio->stop_scan) {
-               uint32_t freq = 0;
-               MMMessageParamType param = { 0, };
+       MMRADIO_THREAD_LOCK(p_thread);
 
-               MMRADIO_LOG_DEBUG("scanning....");
+       while (!p_thread->thread_exit) {
+               MMRADIO_LOG_DEBUG("scan thread started. waiting for signal.");
+               MMRADIO_THREAD_WAIT(p_thread);
 
-               pthread_mutex_lock(&radio->seek_cancel_mutex);
+               if (p_thread->thread_exit) {
+                       MMRADIO_LOG_DEBUG("exiting scan thread");
+                       goto EXIT;
+               }
 
-               if (radio->stop_scan) {
-                       MMRADIO_LOG_INFO("scan was canceled");
-                       pthread_mutex_unlock(&radio->seek_cancel_mutex);
-                       goto FINISHED;
+               if (radio->old_state == MM_RADIO_STATE_PLAYING) {
+                       ret = radio_hal_mute(radio->hal_inf);
+                       if (ret == MM_ERROR_NOT_SUPPORT_API) {
+                               MMRADIO_LOG_WARNING("radio_hal_mute is not supported");
+                       } else if (ret != MM_ERROR_NONE) {
+                               MMRADIO_LOG_ERROR("faied to set radio hal mute");
+                               goto FINISHED;
+                       }
                }
 
-               ret = radio_hal_seek(radio->hal_inf, MM_RADIO_SEEK_UP);
+               ret = radio_hal_set_frequency(radio->hal_inf, radio->region_setting.band_min);
+               if (ret != MM_ERROR_NONE)
+                       goto FINISHED;
 
-               pthread_mutex_unlock(&radio->seek_cancel_mutex);
+               MMRADIO_POST_MSG(radio, MM_MESSAGE_RADIO_SCAN_START, NULL);
+               MMRADIO_SET_STATE(radio, MM_RADIO_STATE_SCANNING);
 
-               if (ret != MM_ERROR_NONE) {
-                       MMRADIO_LOG_ERROR("radio scanning error");
-                       break;
-               }
+               prev_freq = 0;
+               while (!p_thread->stop) {
+                       uint32_t freq = 0;
 
-               /* now we can get new frequency from radio device */
-               if (radio->stop_scan)
-                       break;
+                       MMRADIO_LOG_DEBUG("scanning....");
 
-               ret = radio_hal_get_frequency(radio->hal_inf, &freq);
-               if (ret != MM_ERROR_NONE) {
-                       MMRADIO_LOG_ERROR("failed to get current frequency");
-               } else {
-                       if (freq <= prev_freq) {
-                               MMRADIO_LOG_ERROR("frequency is less than previous [%d] -> [%d] we wrapped around, we are finished scanning", prev_freq, freq);
-                               break;
+                       if (p_thread->thread_exit) {
+                               MMRADIO_LOG_DEBUG("exiting scan thread");
+                               goto EXIT;
                        }
 
-                       prev_freq = param.radio_scan.frequency = (int)freq;
-                       MMRADIO_LOG_INFO("scanning : new frequency : [%d]", param.radio_scan.frequency);
+                       if (p_thread->stop) {
+                               MMRADIO_LOG_INFO("scan was canceled");
+                               goto FINISHED;
+                       }
 
-                       /* drop if max freq is scanned */
-                       if (param.radio_scan.frequency >= radio->region_setting.band_max) {
-                               MMRADIO_LOG_WARNING("%d freq is dropping...and stopping scan", param.radio_scan.frequency);
+                       MMRADIO_HAL_SEEK_THREAD_LOCK(radio);
+                       ret = radio_hal_seek(radio->hal_inf, MM_RADIO_SEEK_UP);
+                       MMRADIO_HAL_SEEK_THREAD_UNLOCK(radio);
+                       if (ret != MM_ERROR_NONE) {
+                               MMRADIO_LOG_ERROR("radio scanning error");
                                break;
                        }
 
-                       if (radio->stop_scan) {
-                               /* doesn't need to post */
-                               break;
+                       if (p_thread->thread_exit) {
+                               MMRADIO_LOG_DEBUG("exiting scan thread");
+                               goto EXIT;
                        }
 
-                       MMRADIO_POST_MSG(radio, MM_MESSAGE_RADIO_SCAN_INFO, &param);
+                       if (p_thread->stop) {
+                               MMRADIO_LOG_INFO("scan was canceled");
+                               goto FINISHED;
+                       }
+
+                       /* now we can get new frequency from radio device */
+                       ret = radio_hal_get_frequency(radio->hal_inf, &freq);
+                       if (ret != MM_ERROR_NONE) {
+                               MMRADIO_LOG_ERROR("failed to get current frequency");
+                       } else {
+                               if (freq <= prev_freq) {
+                                       MMRADIO_LOG_ERROR("frequency is less than previous [%d] -> [%d] we wrapped around, we are finished scanning", prev_freq, freq);
+                                       break;
+                               }
+
+                               prev_freq = (int)freq;
+                               MMRADIO_LOG_INFO("scanning : new frequency : [%d]", prev_freq);
+
+                               /* drop if max freq is scanned */
+                               if (prev_freq >= radio->region_setting.band_max) {
+                                       MMRADIO_LOG_WARNING("%d freq is dropping...and stopping scan", prev_freq);
+                                       break;
+                               }
+
+                               if (p_thread->stop) {
+                                       /* doesn't need to post */
+                                       break;
+                               }
+
+                               __mmradio_msg_push(radio, MM_RADIO_MSG_SCAN_INFO, freq);
+                       }
                }
-       }
+
 FINISHED:
-       if (radio->old_state == MM_RADIO_STATE_READY) {
-               MMRADIO_LOG_DEBUG("old state is ready");
-       } else if (radio->old_state == MM_RADIO_STATE_PLAYING) {
-               MMRADIO_LOG_DEBUG("old state is playing");
-               ret = radio_hal_unmute(radio->hal_inf);
-               if (ret == MM_ERROR_NOT_SUPPORT_API) {
-                       MMRADIO_LOG_WARNING("radio_hal_unmute is not supported");
-               } else if (ret != MM_ERROR_NONE) {
-                       MMRADIO_LOG_ERROR("failed to set radio hal unmute");
-                       goto FINISHED_ERR;
-               }
-               ret = radio_hal_set_frequency(radio->hal_inf, prev_freq);
-               if (ret == MM_ERROR_NOT_SUPPORT_API) {
-                       MMRADIO_LOG_WARNING("radio_hal_set_frequency is not supported");
-               } else if (ret != MM_ERROR_NONE) {
-                       MMRADIO_LOG_ERROR("failed to set radio hal frequency");
-                       goto FINISHED_ERR;
+               if (radio->old_state == MM_RADIO_STATE_READY) {
+                       MMRADIO_LOG_DEBUG("old state is ready");
+               } else if (radio->old_state == MM_RADIO_STATE_PLAYING) {
+                       MMRADIO_LOG_DEBUG("old state is playing");
+                       ret = radio_hal_unmute(radio->hal_inf);
+                       if (ret == MM_ERROR_NOT_SUPPORT_API) {
+                               MMRADIO_LOG_WARNING("radio_hal_unmute is not supported");
+                       } else if (ret != MM_ERROR_NONE) {
+                               MMRADIO_LOG_ERROR("failed to set radio hal unmute");
+                               goto FINISHED_ERR;
+                       }
+                       ret = radio_hal_set_frequency(radio->hal_inf, prev_freq);
+                       if (ret == MM_ERROR_NOT_SUPPORT_API) {
+                               MMRADIO_LOG_WARNING("radio_hal_set_frequency is not supported");
+                       } else if (ret != MM_ERROR_NONE) {
+                               MMRADIO_LOG_ERROR("failed to set radio hal frequency");
+                               goto FINISHED_ERR;
+                       }
                }
-       }
 
 FINISHED_ERR:
-
-       if (radio->old_state == MM_RADIO_STATE_PLAYING) {
-               MMRADIO_SET_STATE(radio, MM_RADIO_STATE_PLAYING);
-       } else {
-               /* close radio device here !!!! */
-               ret = radio_hal_close(radio->hal_inf);
-               if (ret == MM_ERROR_NOT_SUPPORT_API)
-                       MMRADIO_LOG_WARNING("radio_hal_close is not supported");
-               else if (ret != MM_ERROR_NONE)
-                       MMRADIO_LOG_ERROR("failed to close radio hal");
-
-               ret = radio_hal_unprepare(radio->hal_inf);
-               if (ret == MM_ERROR_NOT_SUPPORT_API)
-                       MMRADIO_LOG_WARNING("radio_hal_unprepare is not supported");
-               else if (ret != MM_ERROR_NONE)
-                       MMRADIO_LOG_ERROR("failed to unprepare radio hal");
-
-               radio->is_ready = FALSE;
-
-               if (!radio->interrupted_by_resource_conflict && /* is being released */
-                               radio->radio_resource != NULL) {
-                       ret = mm_resource_manager_mark_for_release(radio->resource_manager,
-                                       radio->radio_resource);
-                       radio->radio_resource = NULL;
-                       if (ret != MM_RESOURCE_MANAGER_ERROR_NONE)
-                               MMRADIO_LOG_ERROR("failed to mark resource for release, ret(0x%x)", ret);
+               if (radio->old_state == MM_RADIO_STATE_PLAYING) {
+                       MMRADIO_SET_STATE(radio, MM_RADIO_STATE_PLAYING);
+               } else {
+                       /* close radio device here !!!! */
+                       if (radio->is_ready) {
+                               ret = radio_hal_close(radio->hal_inf);
+                               if (ret == MM_ERROR_NOT_SUPPORT_API)
+                                       MMRADIO_LOG_WARNING("radio_hal_close is not supported");
+                               else if (ret != MM_ERROR_NONE)
+                                       MMRADIO_LOG_ERROR("failed to close radio hal");
+
+                               ret = radio_hal_unprepare(radio->hal_inf);
+                               if (ret == MM_ERROR_NOT_SUPPORT_API)
+                                       MMRADIO_LOG_WARNING("radio_hal_unprepare is not supported");
+                               else if (ret != MM_ERROR_NONE)
+                                       MMRADIO_LOG_ERROR("failed to unprepare radio hal");
+
+                               if (!radio->interrupted_by_resource_conflict && /* is being released */
+                                               radio->radio_resource != NULL) {
+                                       ret = mm_resource_manager_mark_for_release(radio->resource_manager,
+                                                       radio->radio_resource);
+                                       radio->radio_resource = NULL;
+                                       if (ret != MM_RESOURCE_MANAGER_ERROR_NONE)
+                                               MMRADIO_LOG_ERROR("failed to mark resource for release, ret(0x%x)", ret);
+                               }
+
+                               ret = mm_resource_manager_commit(radio->resource_manager);
+                               if (ret != MM_RESOURCE_MANAGER_ERROR_NONE)
+                                       MMRADIO_LOG_ERROR("resource manager commit fail");
+                               radio->is_ready = false;
+                       }
+                       MMRADIO_SET_STATE(radio, MM_RADIO_STATE_READY);
                }
 
-               ret = mm_resource_manager_commit(radio->resource_manager);
-               if (ret != MM_RESOURCE_MANAGER_ERROR_NONE)
-                       MMRADIO_LOG_ERROR("resource manager commit fail");
+               if (!p_thread->stop)
+                       __mmradio_msg_push(radio, MM_RADIO_MSG_SCAN_FINISHED, 0);
+               else
+                       __mmradio_msg_push(radio, MM_RADIO_MSG_SCAN_STOPPED, 0);
+
+               p_thread->is_running = false;
 
-               MMRADIO_SET_STATE(radio, MM_RADIO_STATE_READY);
        }
 
-       if (!radio->stop_scan)
-               MMRADIO_POST_MSG(radio, MM_MESSAGE_RADIO_SCAN_FINISH, NULL);
-       else
-               MMRADIO_POST_MSG(radio, MM_MESSAGE_RADIO_SCAN_STOP, NULL);
+EXIT:
+       p_thread->is_running = false;
+
+       MMRADIO_THREAD_UNLOCK(p_thread);
 
        MMRADIO_LOG_FLEAVE();
 
-       radio->scan_thread = 0;
        pthread_exit(NULL);
+
 }
 
 bool __is_tunable_frequency(mm_radio_t *radio, int freq)
@@ -1118,34 +1048,57 @@ void __mmradio_seek_thread(mm_radio_t *radio)
 {
        int ret = MM_ERROR_NONE;
        uint32_t freq = 0;
-       MMMessageParamType param = {0, };
+       MMRadioThread_t *p_thread = NULL;
 
        MMRADIO_LOG_FENTER();
        MMRADIO_CHECK_INSTANCE_RETURN_VOID(radio);
 
-       MMRADIO_POST_MSG(radio, MM_MESSAGE_RADIO_SEEK_START, NULL);
+       p_thread = &radio->thread[MM_RADIO_THREAD_SEEK];
+       MMRADIO_CHECK_ARG_RETURN_VOID(p_thread);
 
-       MMRADIO_LOG_DEBUG("seeking....");
+       MMRADIO_THREAD_LOCK(p_thread);
+       MMRADIO_THREAD_SIGNAL(p_thread);
+       MMRADIO_THREAD_UNLOCK(p_thread);
 
-       if (!radio->seek_cancel) {
+       MMRADIO_THREAD_LOCK(p_thread);
 
-               MMRADIO_LOG_DEBUG("try to seek ");
-               pthread_mutex_lock(&radio->seek_cancel_mutex);
-               MMRADIO_LOG_DEBUG("seek start");
+       while (!p_thread->thread_exit) {
+               MMRADIO_LOG_DEBUG("seek thread started. waiting for signal.");
+               MMRADIO_THREAD_WAIT(p_thread);
 
-               if (radio->seek_cancel) {
+               if (p_thread->thread_exit) {
+                       MMRADIO_LOG_DEBUG("exiting seek thread");
+                       goto EXIT;
+               }
+
+               MMRADIO_POST_MSG(radio, MM_MESSAGE_RADIO_SEEK_START, NULL);
+               MMRADIO_LOG_DEBUG("seeking....");
+
+               if (p_thread->stop) {
                        MMRADIO_LOG_INFO("seek was canceled so we return failure to application");
-                       pthread_mutex_unlock(&radio->seek_cancel_mutex);
                        goto SEEK_FAILED;
                }
-
+               freq = 0;
+               MMRADIO_LOG_DEBUG("try to seek ");
+               MMRADIO_HAL_SEEK_THREAD_LOCK(radio);
+               MMRADIO_LOG_DEBUG("seek start");
                ret = radio_hal_seek(radio->hal_inf, radio->seek_direction);
-               pthread_mutex_unlock(&radio->seek_cancel_mutex);
+               MMRADIO_HAL_SEEK_THREAD_UNLOCK(radio);
                if (ret) {
                        MMRADIO_LOG_ERROR("failed to seek radio hal");
                        goto SEEK_FAILED;
                }
 
+               if (p_thread->thread_exit) {
+                       MMRADIO_LOG_DEBUG("exiting seek thread");
+                       goto EXIT;
+               }
+
+               if (p_thread->stop) {
+                       MMRADIO_LOG_INFO("seek was canceled so we return failure to application");
+                       goto SEEK_FAILED;
+               }
+
                /* now we can get new frequency from radio device */
                ret = radio_hal_get_frequency(radio->hal_inf, &freq);
                if (ret) {
@@ -1183,37 +1136,35 @@ void __mmradio_seek_thread(mm_radio_t *radio)
                                MMRADIO_LOG_ERROR("failed to set unmute radio hal");
                                goto SEEK_FAILED;
                        }
-                       radio->seek_unmute = FALSE;
+                       radio->seek_unmute = false;
                }
 
-               param.radio_scan.frequency = radio->prev_seek_freq = (int)freq;
-               MMRADIO_LOG_INFO("seeking : new frequency : [%d]", param.radio_scan.frequency);
-               MMRADIO_POST_MSG(radio, MM_MESSAGE_RADIO_SEEK_FINISH, &param);
-       }
-
-       radio->seek_thread = 0;
-       radio->is_seeking = FALSE;
-
-       MMRADIO_LOG_FLEAVE();
-
-       pthread_exit(NULL);
+               radio->prev_seek_freq = (int)freq;
+               MMRADIO_LOG_INFO("seeking : new frequency : [%d]", (int) freq);
+               __mmradio_msg_push(radio, MM_RADIO_MSG_SEEK_FINISHED, freq);
+               p_thread->is_running = false;
+               continue;
 
 SEEK_FAILED:
-       if (radio->seek_unmute) {
-               /* now turn on radio
-               * In the case of limit freq, tuner should be unmuted.
-               * Otherwise, sound can't output even though application set new frequency.
-               */
-               ret = radio_hal_unmute(radio->hal_inf);
-               if (ret)
-                       MMRADIO_LOG_ERROR("failed to set unmute radio hal");
-               radio->seek_unmute = FALSE;
+               if (radio->seek_unmute) {
+                       /* now turn on radio
+                       * In the case of limit freq, tuner should be unmuted.
+                       * Otherwise, sound can't output even though application set new frequency.
+                       */
+                       ret = radio_hal_unmute(radio->hal_inf);
+                       if (ret)
+                               MMRADIO_LOG_ERROR("failed to set unmute radio hal");
+                       radio->seek_unmute = false;
+               }
+               /* freq -1 means it's failed to seek */
+               __mmradio_msg_push(radio, MM_RADIO_MSG_SEEK_FINISHED, -1);
+               p_thread->is_running = false;
        }
-       /* freq -1 means it's failed to seek */
-       param.radio_scan.frequency = -1;
-       MMRADIO_POST_MSG(radio, MM_MESSAGE_RADIO_SEEK_FINISH, &param);
-       radio->is_seeking = FALSE;
-       radio->seek_thread = 0;
+
+EXIT:
+       p_thread->is_running = false;
+       MMRADIO_THREAD_UNLOCK(p_thread);
+       MMRADIO_LOG_FLEAVE();
        pthread_exit(NULL);
 }
 
@@ -1396,31 +1347,13 @@ static bool __mmradio_set_state(mm_radio_t *radio, int new_state)
        msg.state.previous = radio->old_state;
        msg.state.current = radio->current_state;
 
-#ifdef TIZEN_FEATURE_SOUND_FOCUS
-       /* post message to application */
-       if (radio->sound_focus.by_focus_cb) {
-               msg_type = MM_MESSAGE_STATE_INTERRUPTED;
-               msg.state.code = radio->sound_focus.event_src;
-               MMRADIO_POST_MSG(radio, msg_type, &msg);
-       } else if (radio->interrupted_by_resource_conflict) {
-               msg_type = MM_MESSAGE_STATE_INTERRUPTED;
-               msg.state.code = MM_MSG_CODE_INTERRUPTED_BY_RESOURCE_CONFLICT;
-               MMRADIO_POST_MSG(radio, msg_type, &msg);
+       if (radio->interrupted_by_resource_conflict) {
+               __mmradio_msg_push(radio, MM_RADIO_MSG_STATE_INTERRUPTED,
+                       MM_MSG_CODE_INTERRUPTED_BY_RESOURCE_CONFLICT);
        } else {
                msg_type = MM_MESSAGE_STATE_CHANGED;
                MMRADIO_POST_MSG(radio, msg_type, &msg);
        }
-#else
-       if (radio->interrupted_by_resource_conflict) {
-                msg_type = MM_MESSAGE_STATE_INTERRUPTED;
-                msg.state.code = MM_MSG_CODE_INTERRUPTED_BY_RESOURCE_CONFLICT;
-                MMRADIO_POST_MSG(radio, msg_type, &msg);
-        } else {
-                msg_type = MM_MESSAGE_STATE_CHANGED;
-                MMRADIO_POST_MSG(radio, msg_type, &msg);
-        }
-
-#endif
 
        MMRADIO_LOG_FLEAVE();
 
@@ -1437,118 +1370,6 @@ static int __mmradio_get_state(mm_radio_t *radio)
        return radio->current_state;
 }
 
-#ifdef TIZEN_FEATURE_SOUND_FOCUS
-static void __mmradio_sound_focus_cb(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 *additional_info, void *user_data)
-{
-       mm_radio_t *radio = (mm_radio_t *)user_data;
-       enum MMMessageInterruptedCode event_source;
-       int result = MM_ERROR_NONE;
-       int postMsg = false;
-
-       MMRADIO_LOG_FENTER();
-       MMRADIO_CHECK_INSTANCE_RETURN_VOID(radio);
-       MMRADIO_LOG_INFO("focus_state [%d]", focus_state);
-
-       mmradio_get_sound_focus_reason(focus_state, reason_for_change, FALSE, &event_source, &postMsg);
-       radio->sound_focus.event_src = event_source;
-
-       switch (focus_state) {
-       case FOCUS_IS_RELEASED:{
-                       radio->sound_focus.cur_focus_type &= ~focus_type;
-                       radio->sound_focus.by_focus_cb = true;
-
-                       MMRADIO_CMD_LOCK(radio);
-                       result = _mmradio_stop(radio);
-                       if (result)
-                               MMRADIO_LOG_ERROR("failed to stop radio");
-                       MMRADIO_CMD_UNLOCK(radio);
-
-                       radio->sound_focus.by_focus_cb = false;
-
-                       MMRADIO_LOG_DEBUG("FOCUS_IS_RELEASED cur_focus_type : %d", radio->sound_focus.cur_focus_type);
-               }
-               break;
-
-       case FOCUS_IS_ACQUIRED:{
-                       MMMessageParamType msg = { 0, };
-                       msg.union_type = MM_MSG_UNION_CODE;
-                       msg.code = event_source;
-
-                       radio->sound_focus.cur_focus_type |= focus_type;
-
-                       if ((postMsg) && (FOCUS_FOR_BOTH == radio->sound_focus.cur_focus_type))
-                               MMRADIO_POST_MSG(radio, MM_MESSAGE_READY_TO_RESUME, &msg);
-
-                       MMRADIO_LOG_DEBUG("FOCUS_IS_ACQUIRED cur_focus_type : %d", radio->sound_focus.cur_focus_type);
-               }
-               break;
-
-       default:
-               MMRADIO_LOG_DEBUG("Unknown focus_state");
-               break;
-       }
-
-       MMRADIO_LOG_FLEAVE();
-}
-
-static void __mmradio_sound_focus_watch_cb(int id, mm_sound_focus_type_e focus_type,
-       mm_sound_focus_state_e focus_state, const char *reason_for_change,
-       const char *additional_info, void *user_data)
-{
-       mm_radio_t *radio = (mm_radio_t *)user_data;
-       enum MMMessageInterruptedCode event_source;
-       int result = MM_ERROR_NONE;
-       int postMsg = false;
-
-       MMRADIO_LOG_FENTER();
-       MMRADIO_CHECK_INSTANCE_RETURN_VOID(radio);
-       MMRADIO_LOG_INFO("focus_state [%d]", focus_state);
-
-       mmradio_get_sound_focus_reason(focus_state, reason_for_change, TRUE, &event_source, &postMsg);
-       radio->sound_focus.event_src = event_source;
-
-       switch (focus_state) {
-       case FOCUS_IS_ACQUIRED: {
-                       radio->sound_focus.cur_focus_type &= ~focus_type;
-                       radio->sound_focus.by_focus_cb = true;
-
-                       MMRADIO_CMD_LOCK(radio);
-                       result = _mmradio_stop(radio);
-                       if (result)
-                               MMRADIO_LOG_ERROR("failed to stop radio");
-                       MMRADIO_CMD_UNLOCK(radio);
-
-                       radio->sound_focus.by_focus_cb = false;
-
-                       MMRADIO_LOG_DEBUG("FOCUS_IS_RELEASED cur_focus_type : %d\n", radio->sound_focus.cur_focus_type);
-               }
-               break;
-
-       case FOCUS_IS_RELEASED: {
-                       MMMessageParamType msg = { 0, };
-                       msg.union_type = MM_MSG_UNION_CODE;
-                       msg.code = event_source;
-
-                       radio->sound_focus.cur_focus_type |= focus_type;
-
-                       if ((postMsg) && (FOCUS_FOR_BOTH == radio->sound_focus.cur_focus_type))
-                               MMRADIO_POST_MSG(radio, MM_MESSAGE_READY_TO_RESUME, &msg);
-
-                       MMRADIO_LOG_DEBUG("FOCUS_IS_ACQUIRED cur_focus_type : %d", radio->sound_focus.cur_focus_type);
-               }
-               break;
-
-       default:
-               MMRADIO_LOG_DEBUG("Unknown focus_state");
-               break;
-       }
-
-       MMRADIO_LOG_FLEAVE();
-}
-#endif
-
 static void __mmradio_volume_changed_cb(volume_type_t type, unsigned int volume, void *user_data)
 {
        mm_radio_t *radio = (mm_radio_t *)user_data;
@@ -1710,3 +1531,246 @@ static int __resource_release_cb(mm_resource_manager_h rm,
 
        return FALSE;
 }
+
+static int __mmradio_create_thread_type(mm_radio_t *radio, MMRadioThreadTypes type)
+{
+       MMRadioThread_t *p_thread = NULL;
+
+       MMRADIO_LOG_FENTER();
+
+       MMRADIO_CHECK_INSTANCE(radio);
+
+       if (type >= MM_RADIO_THREAD_NUM) {
+               MMRADIO_LOG_WARNING("wrong argument thread type");
+               return MM_ERROR_RADIO_INTERNAL;
+       }
+
+       p_thread = &radio->thread[type];
+       MMRADIO_CHECK_ARG(p_thread);
+
+       MMRADIO_INIT_MUTEX(p_thread->mutex);
+       MMRADIO_INIT_COND(p_thread->cond);
+
+       MMRADIO_THREAD_LOCK(p_thread);
+       p_thread->thread_id = pthread_create(&p_thread->thread, NULL,
+               (void *)__mmradio_thread_function[type], (void *)radio);
+       if (p_thread->thread_id) {
+               MMRADIO_LOG_DEBUG("failed to create thread : [%d]", type);
+               MMRADIO_THREAD_UNLOCK(p_thread);
+               return MM_ERROR_RADIO_INTERNAL;
+       }
+
+       MMRADIO_LOG_DEBUG("wait for [%d] thread", type);
+       MMRADIO_THREAD_WAIT(p_thread);
+       MMRADIO_LOG_DEBUG("[%d] thread started", type);
+       MMRADIO_THREAD_UNLOCK(p_thread);
+
+       return MM_ERROR_NONE;
+
+ERROR:
+       pthread_mutex_destroy(&p_thread->mutex);
+       pthread_cond_destroy(&p_thread->cond);
+       return MM_ERROR_RADIO_INTERNAL;
+
+}
+
+static int __mmradio_create_threads(mm_radio_t *radio)
+{
+       int ret = MM_ERROR_NONE;
+       int type = 0;
+
+       MMRADIO_LOG_FENTER();
+
+       MMRADIO_CHECK_INSTANCE(radio);
+
+       MMRADIO_INIT_MUTEX(radio->cmd_lock);
+       MMRADIO_INIT_MUTEX(radio->volume_lock);
+       MMRADIO_INIT_MUTEX(radio->hal_seek_mutex);
+
+       for (type = (int)MM_RADIO_THREAD_MSG; type < (int)MM_RADIO_THREAD_NUM; type++) {
+               ret = __mmradio_create_thread_type(radio, (MMRadioThreadTypes)type);
+               if (ret) {
+                       MMRADIO_LOG_ERROR("failed to create thread(%d)", type);
+                       while (--type >= (int)MM_RADIO_THREAD_MSG)
+                               __mmradio_destroy_thread_type(radio, (MMRadioThreadTypes)type);
+                       goto ERROR;
+               }
+       }
+
+       MMRADIO_LOG_FLEAVE();
+       return ret;
+
+ERROR:
+       pthread_mutex_destroy(&radio->cmd_lock);
+       pthread_mutex_destroy(&radio->volume_lock);
+       pthread_mutex_destroy(&radio->hal_seek_mutex);
+
+       MMRADIO_LOG_FLEAVE();
+       return MM_ERROR_RADIO_INTERNAL;
+}
+
+static void __mmradio_destroy_thread_type(mm_radio_t *radio, MMRadioThreadTypes type)
+{
+       MMRadioThread_t *p_thread = NULL;
+       mm_radio_msg_t *msg = NULL;
+       MMRADIO_LOG_FENTER();
+
+       MMRADIO_CHECK_INSTANCE_RETURN_VOID(radio);
+
+       if (type >= MM_RADIO_THREAD_NUM) {
+               MMRADIO_LOG_WARNING("wrong argument thread type");
+               return;
+       }
+
+       p_thread = &radio->thread[type];
+       MMRADIO_CHECK_ARG_RETURN_VOID(p_thread);
+
+       if (p_thread->thread) {
+               switch (type) {
+               case MM_RADIO_THREAD_MSG:
+                       msg = g_slice_new0(mm_radio_msg_t);
+                       if (!msg) {
+                               MMRADIO_LOG_ERROR("failed to get mm_radio_msg_t");
+                       } else {
+                               msg->msg_type = MM_RADIO_MSG_DESTROY;
+                               g_async_queue_push_front(radio->msg_queue, msg);
+                               pthread_join(p_thread->thread, NULL);
+                               p_thread->thread = 0;
+                       }
+                       break;
+               case MM_RADIO_THREAD_SEEK:
+               case MM_RADIO_THREAD_SCAN:
+                       MMRADIO_THREAD_LOCK(p_thread);
+                       p_thread->thread_exit = true;
+                       MMRADIO_THREAD_SIGNAL(p_thread);
+                       MMRADIO_THREAD_UNLOCK(p_thread);
+                       pthread_join(p_thread->thread, NULL);
+                       p_thread->thread = 0;
+                       break;
+               default:
+                       MMRADIO_LOG_WARNING("(%d)type isn't handled", type);
+                       break;
+               }
+       } else {
+               MMRADIO_LOG_WARNING("(%d)thread is zero", type);
+       }
+
+       pthread_mutex_destroy(&p_thread->mutex);
+       pthread_cond_destroy(&p_thread->cond);
+
+       MMRADIO_LOG_FLEAVE();
+}
+
+static void __mmradio_destroy_threads(mm_radio_t *radio)
+{
+       int type = (int)MM_RADIO_THREAD_NUM;
+       MMRADIO_LOG_FENTER();
+
+       MMRADIO_CHECK_INSTANCE_RETURN_VOID(radio);
+
+       while (--type >= 0)
+               __mmradio_destroy_thread_type(radio, (MMRadioThreadTypes)type);
+
+       pthread_mutex_destroy(&radio->cmd_lock);
+       pthread_mutex_destroy(&radio->volume_lock);
+       pthread_mutex_destroy(&radio->hal_seek_mutex);
+
+       MMRADIO_LOG_FLEAVE();
+}
+
+void __mmradio_msg_push(mm_radio_t *radio, MMRadioMsgTypes msg_type, int msg_data)
+{
+       mm_radio_msg_t *msg = g_slice_new0(mm_radio_msg_t);
+       if (!msg) {
+               MMRADIO_LOG_ERROR("NULL msg pointer");
+               return;
+       }
+
+       msg->msg_type = msg_type;
+       msg->data = msg_data;
+
+       MMRADIO_LOG_INFO("push msg_type: %d, msg_data: %d", (int)msg->msg_type, msg->data);
+       g_async_queue_push(radio->msg_queue, msg);
+}
+
+void __mmradio_msg_thread(mm_radio_t *radio)
+{
+
+       mm_radio_msg_t *msg = NULL;
+       MMRadioThread_t *p_thread = NULL;
+
+       MMRADIO_LOG_FENTER();
+       MMRADIO_CHECK_INSTANCE_RETURN_VOID(radio);
+
+       p_thread = &radio->thread[MM_RADIO_THREAD_MSG];
+       MMRADIO_CHECK_ARG_RETURN_VOID(p_thread);
+
+       p_thread->thread_exit = false;
+
+
+       MMRADIO_THREAD_LOCK(p_thread);
+       MMRADIO_THREAD_SIGNAL(p_thread);
+       MMRADIO_THREAD_UNLOCK(p_thread);
+
+       /* we run a while one loop*/
+       while (!p_thread->thread_exit) {
+               msg = (mm_radio_msg_t *)g_async_queue_pop(radio->msg_queue);
+               if (!msg) {
+                       MMRADIO_LOG_ERROR("poped message is NULL!");
+                       break;
+               }
+
+               MMMessageParamType param = {0,};
+
+               switch (msg->msg_type) {
+               case MM_RADIO_MSG_DESTROY:
+                       MMRADIO_LOG_INFO("get destroy msg. pop all event to finish this thread");
+                       mm_radio_msg_t *msg_pop = NULL;
+                       while ((msg_pop = (mm_radio_msg_t *)g_async_queue_try_pop(radio->msg_queue))) {
+                               if (msg_pop != NULL) {
+                                       MMRADIO_LOG_DEBUG("drop this msg type: %d", msg_pop->msg_type);
+                                       g_slice_free(mm_radio_msg_t, msg_pop);
+                               }
+                       }
+                       p_thread->thread_exit = true;
+                       break;
+               case MM_RADIO_MSG_SCAN_INFO:
+                       MMRADIO_LOG_INFO("get scan info frequency: %d", msg->data);
+                       param.radio_scan.frequency = (int) msg->data;
+                       MMRADIO_POST_MSG(radio, MM_MESSAGE_RADIO_SCAN_INFO, &param);
+                       break;
+               case MM_RADIO_MSG_SCAN_STOPPED:
+                       MMRADIO_LOG_INFO("get scan stopped");
+                       MMRADIO_POST_MSG(radio, MM_MESSAGE_RADIO_SCAN_STOP, NULL);
+                       break;
+               case MM_RADIO_MSG_SCAN_FINISHED:
+                       MMRADIO_LOG_INFO("get scan finished");
+                       MMRADIO_POST_MSG(radio, MM_MESSAGE_RADIO_SCAN_FINISH, NULL);
+                       break;
+               case MM_RADIO_MSG_SEEK_FINISHED:
+                       MMRADIO_LOG_INFO("get seek finish frequency: %d", msg->data);
+                       param.radio_scan.frequency = (int) msg->data;
+                       MMRADIO_POST_MSG(radio, MM_MESSAGE_RADIO_SEEK_FINISH, &param);
+                       break;
+               case MM_RADIO_MSG_STATE_INTERRUPTED:
+                       MMRADIO_LOG_INFO("get state interrupted type: %d", msg->data);
+                       param.union_type = MM_MSG_UNION_STATE;
+                       param.state.previous = radio->old_state;
+                       param.state.current = radio->current_state;
+                       param.state.code = msg->data;
+                       MMRADIO_POST_MSG(radio, MM_MESSAGE_STATE_INTERRUPTED, &param);
+                       break;
+               default:
+                       MMRADIO_LOG_ERROR("wrong msg_type : %d", msg->msg_type);
+                       break;
+               }
+
+               if (msg)
+                       g_slice_free(mm_radio_msg_t, msg);
+
+       }
+
+       MMRADIO_LOG_INFO("msg thread is finished");
+       MMRADIO_LOG_FLEAVE();
+       pthread_exit(NULL);
+}