/*****************************************************************************
 * FUNCTION
 *  jma_audio_recorder_timer_expired
 * DESCRIPTION
 *  This function handles if audio recorder timer is expired
 * PARAMETERS
 *  mma_type            [IN]
 *  java_recorder       [IN]
 *  int reocrder(?)
 * RETURNS
 *  void
 *****************************************************************************/
void jma_audio_recorder_timer_expired(kal_int32 vm_id)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_uint32 record_size = 0;
    kal_int32 ticks = kal_milli_secs_to_ticks(200); /* 200 ms */
    FS_HANDLE file_handle;
    kal_int32 result;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    kal_trace(TRACE_GROUP_4, FUNC_J2ME_JMA_AUDIO_RECORDER_TIMER_EXPIRED, vm_id, audio_recorder[vm_id].id, audio_recorder[vm_id].state);
    JMA_CS_ENTER(vm_id, JMA_VOID);

    if (audio_recorder[vm_id].state != JAVA_MEDIA_STATE_RECORDING)
    {
        JMA_CS_LEAVE(JMA_VOID);
    }

    EXT_ASSERT(audio_recorder[vm_id].state == JAVA_MEDIA_STATE_RECORDING, 0, 0, 0);

    /* Get file size */
    file_handle = FS_Open(audio_recorder[vm_id].filename, FS_READ_ONLY | FS_OPEN_SHARED);
    if (file_handle <= 0)
    {
        kal_trace(TRACE_GROUP_4, INFO_J2ME_MMA_MARKER_FAIL);
        kal_trace(TRACE_GROUP_4, INFO_J2ME_MMA_D, file_handle);
        JMA_CS_LEAVE(JMA_VOID);
    }
    result = FS_GetFileSize(file_handle, &record_size);
    result = FS_Close(file_handle);
    if(1 == result)
    {
        kal_trace(TRACE_GROUP_4, INFO_J2ME_MMA_D, record_size+1);
    }
    kal_trace(TRACE_GROUP_4, INFO_J2ME_MMA_D, record_size);

    /* Reach record size limit */
    if (record_size >= audio_recorder[vm_id].record_size_limit)
    {
        /* State will be changed */
        jma_mvm_audio_recorder_commit(vm_id, audio_recorder[vm_id].id);

        jvm_post_mma_event(MMA_EVENT_RECORD_STOPPED,
                           audio_recorder[vm_id].id, audio_recorder[vm_id].current_time);
    }
    else /* re-start timer */
    {
        kal_trace(TRACE_GROUP_4, INFO_J2ME_MMA_STACK_START_TIMER);
        stack_start_timer(&java_media_timer, JAVA_MEDIA_TIMER_AUDIO_RECORDER + vm_id, ticks);
    }
    JMA_CS_LEAVE(JMA_VOID);
}
/*****************************************************************************
 * FUNCTION
 *  jma_audio_recorder_set_record_size_limit
 * DESCRIPTION
 *  This function set the size limit of recorded data
 * PARAMETERS
 *  mma_type            [IN]        Int recorder
 *  java_recorder       [IN]
 *  limit_size          [IN]
 * RETURNS
 *  int - limit_size set
 *****************************************************************************/
kal_int32 jma_mvm_audio_recorder_set_record_size_limit(kal_int32 vm_id, kal_int32 java_recorder, kal_int32 limit_size)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_int32 ticks = kal_milli_secs_to_ticks(200); /* 200 ms */

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    kal_trace(TRACE_GROUP_4, FUNC_J2ME_JMA_MVM_AUDIO_RECORDER_SET_RECORD_SIZE_LIMIT, vm_id, java_recorder, audio_recorder[vm_id].state, limit_size);
    JMA_CS_ENTER(vm_id, 0);

    if (audio_recorder[vm_id].id != java_recorder)
    {
        kal_trace(TRACE_GROUP_4, INFO_J2ME_MMA_RESOURCE_ERROR, audio_recorder[vm_id].id);
        JMA_CS_LEAVE(0);
    }

    /* Save parameter */
    audio_recorder[vm_id].record_size_limit = limit_size;

    if (vm_id != jam_mvm_get_current_vm_id())
    {
        JMA_CS_LEAVE(limit_size);
    }
//#ifdef __MMI_BACKGROUND_CALL__
    if (jma_media_player_is_in_background_call())
    {
        JMA_CS_LEAVE(limit_size);
    }
//#endif /* __MMI_BACKGROUND_CALL__ */

    /* start timer */
    if (audio_recorder[vm_id].state == JAVA_MEDIA_STATE_RECORDING)
    {
        if(audio_recorder[vm_id].record_size_limit != -1)
        {
            kal_trace(TRACE_GROUP_4, INFO_J2ME_MMA_STACK_START_TIMER);
            stack_start_timer(&java_media_timer, JAVA_MEDIA_TIMER_AUDIO_RECORDER + vm_id, ticks);
        }
    }
    JMA_CS_LEAVE(limit_size);
}
/*****************************************************************************
 * FUNCTION
 *  avk_mytimer_test_proc
 * DESCRIPTION
 *  mytimer proc for expiry
 * PARAMETERS
 *  param : [IN] user data
 * RETURNS
 *  void
 *****************************************************************************/
static void avk_mytimer_test_proc(void *param)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U32 index;
    U32 exe_time;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    index = (U32)param;
    switch (index)
    {
        case AVK_MYTIMER_TEST_START:
            kal_get_time(&exe_time);
            MMI_ASSERT(exe_time - g_avk_pluto_timer_ctx.start_time >= kal_milli_secs_to_ticks(AVK_PLUTO_TIMER_TIMEOUT_MS_100));
            AVK_CASE_RESULT(MMI_TRUE);
            AVK_ASYN_DONE();
            break;
            
        case AVK_MYTIMER_TEST_STOP:
            g_avk_pluto_timer_ctx.excute_count++;
            g_mytimer_test = avk_mytimer_start_timer(avk_mytimer_test_proc, AVK_PLUTO_TIMER_TIMEOUT_MS_100, (void *)AVK_MYTIMER_TEST_STOP);
            break;

        case AVK_MYTIMER_TEST_STOP_DO:
            g_avk_pluto_timer_ctx.excute_count_backup = g_avk_pluto_timer_ctx.excute_count;
            avk_mytimer_stop_timer(g_mytimer_test);
            g_mytimer_test = NULL;
            avk_mytimer_start_timer(avk_mytimer_test_proc, AVK_PLUTO_TIMER_TIMEOUT_MS_200, (void *)AVK_MYTIMER_TEST_STOP_CHECK);
            break;

        case AVK_MYTIMER_TEST_STOP_CHECK:
            MMI_ASSERT(g_mytimer_test == NULL && g_avk_pluto_timer_ctx.excute_count_backup == g_avk_pluto_timer_ctx.excute_count);
            AVK_CASE_RESULT(MMI_TRUE);
            AVK_ASYN_DONE();
            break;
            
        default:
            MMI_ASSERT(0);
    }
}
/*****************************************************************************
 * FUNCTION
 *  avk_mytimer_start_timer
 * DESCRIPTION
 *  start a mytimer
 * PARAMETERS
 *  expiry_proc : [IN] proc for mytimer expiry
 *  milli_secs  : [IN] milliseconds for expiry
 *  param       : [IN] user data
 * RETURNS
 *  NULL            :  fail to start timer
 *  avk_mytimer_id  :  mytimer id
 *****************************************************************************/
avk_mytimer_id avk_mytimer_start_timer(avk_mytimer_proc expiry_proc, U32 milli_secs, void *param)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_uint32 ticks;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (expiry_proc == NULL)
    {
        return NULL;
    }
    ticks = kal_milli_secs_to_ticks(milli_secs);
    if (ticks == 0)
    {
        ticks = 1;
    }
    return (avk_mytimer_id)evshed_set_event((event_scheduler *)g_mytimer_base_cntx, 
            (kal_timer_func_ptr)expiry_proc,
            param,
            ticks);
}
/*****************************************************************************
 * FUNCTION
 *  jma_audio_recorder_start_record
 * DESCRIPTION
 *  This function submit Record request to media task.
 * PARAMETERS
 *  mma_type            [IN]        Int recorder
 *  java_recorder       [IN]
 * RETURNS
 *  int - 0
 *****************************************************************************/
kal_bool jma_mvm_audio_recorder_start_record(kal_int32 vm_id, kal_int32 java_recorder)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mdi_result ret = MDI_AUDIO_FAIL;
    kal_int32 ticks = kal_milli_secs_to_ticks(200); /* 200 ms */

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    kal_trace(TRACE_GROUP_4, FUNC_J2ME_JMA_MVM_AUDIO_RECORDER_START_RECORD, vm_id, java_recorder, audio_recorder[vm_id].state);
    JMA_CS_ENTER(vm_id, KAL_FALSE);

    if (audio_recorder[vm_id].id != java_recorder)
    {
        kal_trace(TRACE_GROUP_4, INFO_J2ME_MMA_RESOURCE_ERROR, audio_recorder[vm_id].id);
        JMA_CS_LEAVE(KAL_FALSE);
    }
    if (vm_id != jam_mvm_get_current_vm_id())
    {
        JMA_CS_LEAVE(KAL_TRUE);
    }

//#ifdef __MMI_BACKGROUND_CALL__
    if (jma_media_player_is_in_background_call())
    {
        JMA_CS_LEAVE(KAL_FALSE);
    }
//#endif /* __MMI_BACKGROUND_CALL__ */

    if (mmi_ucs2strlen((PS8) audio_recorder[vm_id].filename) == 0)
    {
        JMA_CS_LEAVE(KAL_FALSE);
    }

    if (audio_recorder[vm_id].state == JAVA_MEDIA_STATE_READY)
    {
        ret = mdi_audio_start_record(audio_recorder[vm_id].filename, audio_recorder[vm_id].format, 0,
                                     jma_audio_recorder_record_result_hdlr, NULL);
    }
    else if (audio_recorder[vm_id].state == JAVA_MEDIA_STATE_PAUSED)
    {
        ret = mdi_audio_resume(jma_audio_recorder_record_result_hdlr, NULL);
    }

    /* Handle result */
    if (ret == MDI_AUDIO_SUCCESS)
    {
        kal_trace(TRACE_GROUP_4, J2ME_MMA_STATE_TRANSITION, vm_id, audio_recorder[vm_id].state, JAVA_MEDIA_STATE_RECORDING);
        audio_recorder[vm_id].state = JAVA_MEDIA_STATE_RECORDING;

        if (audio_recorder[vm_id].record_size_limit != -1)
        {
            kal_trace(TRACE_GROUP_4, INFO_J2ME_MMA_STACK_START_TIMER);
            stack_start_timer(&java_media_timer, JAVA_MEDIA_TIMER_AUDIO_RECORDER + vm_id, ticks);
        }

        JMA_CS_LEAVE(KAL_TRUE);
    }
    else
    {
        kal_trace(TRACE_GROUP_4, INFO_J2ME_MMA_MARKER_FAIL);
        kal_trace(TRACE_GROUP_4, INFO_J2ME_MMA_D, ret);
        JMA_CS_LEAVE(KAL_FALSE);
    }
}