/**
 *  @file ins_ut_media_service.c
 *  @details media service unit test
 *
 */
#define INSLOG_TAG     "MEDIASVC_UT"
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include "AmbaTypes.h"
#include "AmbaDef.h"
#include "AmbaKAL.h"
#include "AmbaPrint.h"
#include "AmbaGPIO.h"
#include "AmbaUtility.h"
#include "AmbaSYS.h"
#include "AmbaSD.h"
#include "AmbaFPD.h"
#include "AmbaDSP.h"
#include "AmbaDSP_Liveview.h"
#include "AmbaDSP_ImageUtility.h"
#include "AmbaDSP_VideoDec.h"
#if defined(CONFIG_ICAM_SWPLL_CONTROL)
#include "AmbaFTCM.h"
#endif
#include "AmbaSvcWrap.h"
#include "AmbaVfs.h"
#include "AmbaCodecCom.h"
#include "AmbaDspInt.h"
#include "AmbaDMA_Def.h"
#include "AmbaAudio_AENC.h"
#include "AmbaAudio_AIN.h"
#include "AmbaAudio_AOUT.h"
#include "AmbaAudio_ADEC.h"
#include "AmbaAudioEfx.h"
#include "AmbaAudioBuf.h"
#include "AmbaAudio.h"
#if defined(CONFIG_ENABLE_AMBALINK)
#if defined(CONFIG_AMBALINK_BOOT_OS)
#include "AmbaLink.h"
#include "SvcRpmsgApp.h"
#else
#include "AmbaIPC.h"
#endif
#endif

#include "AmbaAEncFlow.h"
#include "AmbaMisraFix.h"
#include "AmbaADecFlow.h"
#include "AmbaUtility.h"

#if defined(CONFIG_ICAM_IMGITUNER_USED)
#include "AmbaTUNE_USBCtrl.h"
#endif

#if defined(CONFIG_ICAM_USB_USED)
#include "AmbaSvcUsb.h"
#endif

#include "SvcErrCode.h"
#include "SvcLog.h"
#include "SvcPref.h"
#include "SvcMem.h"
#include "SvcWrap.h"
#include "SvcTask.h"
#if defined(CONFIG_ICAM_IMGCAL_USED)
#include "SvcCalibMgr.h"
#include "SvcCalibTask.h"
#endif
#include "SvcBuffer.h"
#include "SvcCmdDSP.h"
#include "SvcFlowControl.h"
#include "SvcResCfg.h"
#include "SvcButtonCtrl.h"
#include "SvcShell.h"
#if defined(CONFIG_ICAM_SWPLL_CONTROL)
#include "SvcSwPll.h"
#endif
#include "SvcSysStat.h"
#include "SvcVoutSrc.h"
#include "SvcDataLoader.h"
#include "SvcOsd.h"
#include "SvcUserPref.h"
#include "SvcResCfgTask.h"
#include "SvcIKCfg.h"
#include "SvcVinCtrlTask.h"
#include "SvcVoutCtrlTask.h"
#include "SvcVoutFrmCtrlTask.h"
#include "SvcLiveviewTask.h"
#include "SvcDisplayTask.h"
#include "SvcDspTask.h"
#include "SvcRecTask.h"
#include "SvcImgTask.h"
#include "SvcSafeTask.h"
#include "SvcBufMap.h"
#include "SvcEmrTask.h"
#include "SvcOsdTask.h"
#include "SvcGuiTask.h"
#include "SvcMenuTask.h"
#include "SvcPlaybackTask.h"
#include "SvcRawEncTask.h"
#include "SvcFwUpdateTask.h"
#include "SvcRawCapTask.h"
#include "SvcCmdApp.h"
#include "SvcUcode.h"
#include "SvcButtonCtrlTask.h"
#include "SvcWarningIconTask.h"
#include "SvcSysStatTask.h"
#include "SvcDirtDetectTask.h"
#include "SvcDefogTask.h"
#include "SvcColorBalance.h"
#include "SvcStill.h"
#include "SvcStillCap.h"
#include "SvcStillTask.h"
#include "SvcControlTask.h"
#include "SvcStgMonitorTask.h"
#if defined(CONFIG_ICAM_TIMING_LOG)
#include "SvcTiming.h"
#endif
#if defined(CONFIG_SVC_VIN_ERR_CTRL_USED)
#include "SvcVinErrTask.h"
#endif
#if defined(CONFIG_SVC_VOUT_ERR_CTRL_USED)
#include "SvcVoutErrTask.h"
#endif
#include "SvcAppStat.h"
#include "SvcCanSpeedTask.h"
#if defined(CONFIG_BUILD_CV)    /* CV related function */
#include "SvcCvMainTask.h"
#include "SvcCvCtrlTask.h"
#endif
#if defined(CONFIG_ICAM_IMGCAL_STITCH_USED)
#include "SvcEmrAdaptiveTask.h"
#endif
#ifdef CONFIG_ICAM_AUDIO_USED
#include "SvcAudioTask.h"
#endif
#ifdef CONFIG_ICAM_ENABLE_REGRESSION_FLOW
#include "SvcRegressionTask.h"
#endif
#ifdef CONFIG_ICAM_PROJECT_SURROUND
#include "SvcAnimTask.h"
#endif
#include "SvcNvmTask.h"
#include "SvcTaskList.h"
#include "SvcPlat.h"
#if defined(CONFIG_ICAM_LV_FEED_EXT_DATA_SUPPORTED)
#include "SvcLvFeedExtRawTask.h"
#endif
#if defined(CONFIG_ICAM_LV_FEED_EXT_YUV_SUPPORTED)
#include "SvcLvFeedExtYuvTask.h"
#endif
#if defined(CONFIG_ICAM_BIST_UCODE)
#include "AmbaShell.h"
#include "SvcUcBIST.h"
#endif
#if defined(CONFIG_ICAM_SENSOR_ASIL_ENABLED)
#include "SvcSensorCrcCheckTask.h"
#endif

#if defined(CONFIG_ICAM_PROJECT_SHMOO)
#include "SvcAdvancedShmooTask.h"
#endif

#include "AmbaDSP_VOUT.h"
#include "AmbaSensor.h"
#include "SvcVinSrc.h"
#include "SvcVoutSrc.h"
#include "SvcDisplay.h"
#include "SvcIK.h"
#include "SvcRecMain.h"
#include "SvcRecInfoPack.h"
#include "SvcDSP.h"
#include "SvcUtil.h"


/* ins head files */
#include "ins_pal_error_code.h"
#include "ins_pal_memory.h"
#include "ins_pal_task.h"
#include "ins_pal_fs.h"
#include "ins_pal_system.h"
#include "unittest.h"
#include "ins_ut_media.h"
#include "ins_comm_util.h"

#include "ins_svc_vinsrc.h"
#include "ins_svc_voutsrc.h"
#include "ins_svc_display.h"
#include "ins_svc_venc.h"

#include "ins_svc_adecctrl.h"
#include "ins_svc_aencctrl.h"
#include "ins_svc_ainctrl.h"
#include "ins_svc_aoutctrl.h"
#include "ins_svc_arecflow.h"
#include "ins_svc_aplayflow.h"

#include "ins_svc_vinctrl.h"
#include "ins_svc_voutctrl.h"
#include "ins_svc_vout_frmctrl.h"
#include "ins_svc_dispctrl.h"
#include "ins_svc_ik.h"
#include "ins_svc_liveviewctrl.h"
#include "ins_svc_vencctrl.h"
#include "ins_svc_venc_feed.h"
#include "ins_mw_algproxy.h"
#include "ins_mw_imuproxy.h"
#include "ins_mw_mediaprocess_def.h"

#include "ins_dev_pcm2wav.h"

extern uint32_t InsMw_MediaMgr_GetVideoProcessForUTest(InsMw_Video_Process_t* p_video_process);
extern uint32_t InsMw_MediaMgr_GetAudioProcessForUTest(InsMw_Audio_Process_t* p_audio_process);


InsMw_Video_Process_t* g_pvideo_ut = NULL;
InsMw_Audio_Process_t* g_paudio_ut = NULL;


/**
* initializ media svc unittest 
*/
uint32_t InsUT_MediaSvc_Init(void)
{
    uint32_t ret_val = INSERR_OK;

    if(NULL==g_pvideo_ut)
    {
        /* malloc memory for video process unittest */
        ret_val = InsPal_Memory_Malloc((void*)&g_pvideo_ut, sizeof(InsMw_Video_Process_t));
        if(INSERR_OK!=ret_val)
        {
            SvcLog_NG(INSLOG_TAG, "malloc video process unittest fail.", 0U, 0U);
            return ERRMED_INVAL;
        }
        else
        {
            /* init video process unittest */
            AmbaWrap_memset(g_pvideo_ut, 0, sizeof(InsMw_Video_Process_t));
            InsMw_MediaMgr_GetVideoProcessForUTest(g_pvideo_ut);

            SvcLog_OK(INSLOG_TAG, "malloc video process unittest success.", 0U, 0U);
        }
    }

    if(NULL==g_paudio_ut)
    {
        /* malloc memory for audio process unittest */
        ret_val = InsPal_Memory_Malloc((void*)&g_paudio_ut, sizeof(InsMw_Audio_Process_t));
        if(INSERR_OK!=ret_val)
        {
            SvcLog_NG(INSLOG_TAG, "malloc audio process unittest fail.", 0U, 0U);
            return ERRMED_INVAL;
        }
        else
        {
            /* init audio process unittest */
            AmbaWrap_memset(g_paudio_ut, 0, sizeof(InsMw_Video_Process_t));
            InsMw_MediaMgr_GetAudioProcessForUTest(g_paudio_ut);

            SvcLog_OK(INSLOG_TAG, "malloc audio process unittest success.", 0U, 0U);
        }
    }

    return ret_val;
}


/**
 * de initializ media svc unittest
 */
uint32_t InsUT_MediaSvc_DeInit(void)
{
    uint32_t ret_val = INSERR_OK;
    if(NULL!=g_pvideo_ut)
    {
        /* free memory of video process unittest */
        ret_val = InsPal_Memory_Free(g_pvideo_ut);
        if(INSERR_OK!=ret_val)
        {
            SvcLog_NG(INSLOG_TAG, "free video process unittest fail.", 0U, 0U);
        }
        else
        {
            SvcLog_OK(INSLOG_TAG, "free video process unittest success.", 0U, 0U);
        }
        g_pvideo_ut = NULL;
    }

    if(NULL!=g_paudio_ut)
    {
        /* free memory of video process unittest */
        ret_val = InsPal_Memory_Free(g_paudio_ut);
        if(INSERR_OK!=ret_val)
        {
            SvcLog_NG(INSLOG_TAG, "free audio process unittest fail.", 0U, 0U);
        }
        else
        {
            SvcLog_OK(INSLOG_TAG, "free audio process unittest success.", 0U, 0U);
        }
        g_paudio_ut = NULL;
    }

    return ret_val;
}


/**
 * check video process parameter
 */
static uint32_t InsUT_MediaSvc_VideoCheck(const InsMw_Video_Process_t* p_host)
{
    if(NULL==p_host
        || NULL==p_host->p_vinctrl
        || NULL==p_host->p_voutctrl
        || NULL==p_host->p_voutfrmctrl
        || NULL==p_host->p_dispctrl
        || NULL==p_host->p_livectrl
        || NULL==p_host->p_vencctrl
        || NULL==p_host->p_vencfeed)
    {
        return ERRMED_INVAL;
    }

    return INSERR_OK;
}

/**
 * check audio process parameter
 */
static uint32_t InsUT_MediaSvc_AudioCheck(const InsMw_Audio_Process_t* p_host)
{
    if(NULL==p_host
        || NULL==p_host->p_ainctrl
        || NULL==p_host->p_aoutctrl
        || NULL==p_host->p_aencctrl
        || NULL==p_host->p_aencctrl
        || NULL==p_host->p_arecflow
        || NULL==p_host->p_aplayflow)
    {
        return ERRMED_INVAL;
    }

    return INSERR_OK;
}

/**
 * convert rgb value to yuv value
 */
static void InsUT_RgbConvertToYuv(uint8_t r, uint8_t g, uint8_t b, uint8_t* py, uint8_t* pu, uint8_t* pv)
{
    if(NULL==py
        ||NULL==pu
        ||NULL==pv)
    {
        return;
    }

#if 1
    *py = 0.299 * r + 0.587 * g + 0.114 * b;
    *pu = -0.1687 * r - 0.3313 * g + 0.5 * b + 128;
    *pv = 0.5 * r - 0.4187 * g - 0.0813 * b + 128;
#else
    *py = 0.299 * r + 0.587 * g + 0.114 * b;
    *pu = -0.147 * r - 0.289 * g + 0.436 * b;
    *pv = 0.615 * r - 0.515 * g - 0.100 * b;
#endif
}


/**
 * fill yuv420sp frame with color
 * @return error code
 */
static uint32_t InsUT_FillYuv420sp(int8_t *p_frame, uint32_t pitch, uint32_t height, uint8_t y, uint8_t u, uint8_t v)
{
    int8_t* p_offbuf = NULL;
    if(NULL==p_frame)
    {
        return INSERR_NG;
    }    

    /* set y value */
    p_offbuf = p_frame;
    for(uint32_t i=0;i<height;i++)
    {            
        memset(p_offbuf, y, pitch);
        p_offbuf += pitch;
    }

    /* set uv value */
    for(uint32_t i=0; i<height/2;i++)
    {
        p_offbuf = p_frame + pitch*height + pitch*i;
        for(uint32_t j=0; j<pitch; j+=2)
        {
            *(p_offbuf+0)   = u;
            *(p_offbuf+1)   = v; 
            p_offbuf += 2;
        }                   
    }   

    return INSERR_OK;
}


/**
*  unittest for vin start
* @return ErrorCode
*/
static int32_t InsUT_Video_VinStart(int32_t argc, char **argv)
{
    uint32_t ret_val = INSERR_OK;
    InsSvc_VinCtrlOpt_t* p_vinctrl_ut = NULL;

    (void)argc;
    (void)argv;      
    if(INSERR_OK!=InsUT_MediaMgr_InitCheck() 
        || INSERR_OK!=InsUT_MediaSvc_VideoCheck(g_pvideo_ut))
    {
        SvcLog_NG(INSLOG_TAG, "mediamgr not initialized.", 0U, 0U);
        return ERRMED_INVAL;
    }
    p_vinctrl_ut = g_pvideo_ut->p_vinctrl;
    

    /* Config Vin (including sensor enable/config) based on current resolution */
    ret_val = p_vinctrl_ut->vinctrl_config();
    if (INSERR_OK != ret_val) 
    {
        SvcLog_NG(INSLOG_TAG, "vinctrl_config() failed with %d", ret_val, 0U);
        SvcFlowControl_Assert();
    }

    /* Wait Vin Src is ready */
    if (INSERR_OK == ret_val) 
    {
        ret_val = p_vinctrl_ut->vinctrl_wait_src_ready();
        if (INSERR_OK != ret_val) 
        {
            SvcLog_NG(INSLOG_TAG, "vinctrl_wait_src_ready() failed with %d", ret_val, 0U);
            SvcFlowControl_Assert();
        }
    }

    SvcLog_DBG(INSLOG_TAG, "InsUT_Video_VinStart() finish.", 0U, 0U);
    return ret_val;
}


/**
* unittest for vin stop test
* @return ErrorCode
*/
static int32_t InsUT_Video_VinStop(int32_t argc, char **argv)
{
    uint32_t ret_val = INSERR_OK;
    InsSvc_VinCtrlOpt_t* p_vinctrl_ut = NULL;

    (void)argc;
    (void)argv;      
    if(INSERR_OK!=InsUT_MediaMgr_InitCheck() 
        || INSERR_OK!=InsUT_MediaSvc_VideoCheck(g_pvideo_ut))
    {
        SvcLog_NG(INSLOG_TAG, "mediamgr not initialized.", 0U, 0U);
        return ERRMED_INVAL;
    }
    p_vinctrl_ut = g_pvideo_ut->p_vinctrl;

    ret_val = p_vinctrl_ut->vinctrl_destroy();
    if (INSERR_OK != ret_val) 
    {
        SvcLog_NG(INSLOG_TAG, "vinctrl_destroy() failed with %d", ret_val, 0U);
        SvcFlowControl_Assert();
    }    

    SvcLog_DBG(INSLOG_TAG, "InsUT_Video_VinStop() finish.", 0U, 0U);
    return ret_val;
}


/**
* unittest for vout start
* @return ErrorCode
*/
static int32_t InsUT_Video_VoutStart(int32_t argc, char **argv)
{
    uint32_t ret_val = INSERR_OK;
    InsSvc_VoutCtrlOpt_t* p_voutctrl_ut = NULL;
    InsSvc_DispCtrlOpt_t* p_dispctrl_ut = NULL;

    (void)argc;
    (void)argv;  
    if(INSERR_OK!=InsUT_MediaMgr_InitCheck() 
        || INSERR_OK!=InsUT_MediaSvc_VideoCheck(g_pvideo_ut))
    {
        SvcLog_NG(INSLOG_TAG, "mediamgr not initialized.", 0U, 0U);
        return ERRMED_INVAL;
    }
    p_voutctrl_ut = g_pvideo_ut->p_voutctrl;
    p_dispctrl_ut = g_pvideo_ut->p_dispctrl;

    /* Config Vout (including lcd device enable/config) based on current resolution */
    if (INSERR_OK == ret_val) 
    {
        ret_val = p_voutctrl_ut->voutctrl_config();
        if (INSERR_OK != ret_val) 
        {
            SvcLog_NG(INSLOG_TAG, "SvcVoutCtrlTask_Config() failed with %d", ret_val, 0U);
            SvcFlowControl_Assert();
        }
    }

    /* Wait Vout Src is ready */
    if (INSERR_OK == ret_val) 
    {
        ret_val = p_voutctrl_ut->voutctrl_wait_src_ready();
        if (INSERR_OK != ret_val) 
        {
            SvcLog_NG(INSLOG_TAG, "SvcVoutCtrlTask_WaitSrcReady() failed with %d", ret_val, 0U);
            SvcFlowControl_Assert();
        }
    }

    if (INSERR_OK == ret_val) 
    {
        ret_val = p_dispctrl_ut->dispctrl_start();
        if (INSERR_OK != ret_val) 
        {
            SvcLog_NG(INSLOG_TAG, "SvcDisplayTask_Start() failed with %d", ret_val, 0U);
            SvcFlowControl_Assert();
        }
    } 

    SvcLog_DBG(INSLOG_TAG, "InsUT_Video_VoutStart() finish.", 0U, 0U);
    return ret_val;
}


/**
* unittest for vout stop
* @return ErrorCode
*/
static int32_t InsUT_Video_VoutStop(int32_t argc, char **argv)
{
    uint32_t ret_val = INSERR_OK;
    InsSvc_VoutCtrlOpt_t* p_voutctrl_ut = NULL;
    InsSvc_DispCtrlOpt_t* p_dispctrl_ut = NULL;

    (void)argc;
    (void)argv;  
    if(INSERR_OK!=InsUT_MediaMgr_InitCheck() 
        || INSERR_OK!=InsUT_MediaSvc_VideoCheck(g_pvideo_ut))
    {
        SvcLog_NG(INSLOG_TAG, "mediamgr not initialized.", 0U, 0U);
        return ERRMED_INVAL;
    }
    p_voutctrl_ut = g_pvideo_ut->p_voutctrl;
    p_dispctrl_ut = g_pvideo_ut->p_dispctrl;

    /* destory vout task */
    ret_val = p_voutctrl_ut->voutctrl_destroy();
    if (INSERR_OK != ret_val) 
    {
        SvcLog_NG(INSLOG_TAG, "SvcVoutCtrlTask_Destroy() failed with %d", ret_val, 0U);
        SvcFlowControl_Assert();
    }

    /* stop display task */
    p_dispctrl_ut->dispctrl_stop();

    SvcLog_DBG(INSLOG_TAG, "InsUT_Video_VoutStop() finish.", 0U, 0U);
    return ret_val;
}


/**
* unittest for voutfrmctrl start
* @return ErrorCode
*/
static int32_t InsUT_Video_VoutFrmStart(int32_t argc, char **argv) 
{
    uint32_t ret_val = INSERR_OK;
    InsSvc_VoutFrmCtrlOpt_t* p_vofrmctrl_ut = NULL;
    InsSvc_VoutFmtCtrl_Cfg_t ctrl_cfg;

    (void)argc;
    (void)argv;
    if(INSERR_OK!=InsUT_MediaMgr_InitCheck() 
        || INSERR_OK!=InsUT_MediaSvc_VideoCheck(g_pvideo_ut))
    {
        SvcLog_NG(INSLOG_TAG, "mediamgr not initialized.", 0U, 0U);
        return ERRMED_INVAL;
    }    
    p_vofrmctrl_ut  = g_pvideo_ut->p_voutfrmctrl;

    ret_val = InsSvc_VoutFrmCtrl_Init(p_vofrmctrl_ut);

    ctrl_cfg.DlyCount   = 0;
    ctrl_cfg.SyncMode   = 0;
    ctrl_cfg.YuvStrmIdx = 0;
    ret_val = InsSvc_VoutFrmCtrl_Config(p_vofrmctrl_ut, &ctrl_cfg);
    if (INSERR_OK != ret_val) 
    {
        SvcLog_NG(INSLOG_TAG, "InsSvc_VoutFrmCtrl_Config() failed with %d", ret_val, 0U);
        SvcFlowControl_Assert();
    }

    ret_val = InsSvc_VoutFrmCtrl_Start(p_vofrmctrl_ut);
    if (INSERR_OK != ret_val) 
    {
        SvcLog_NG(INSLOG_TAG, "InsSvc_VoutFrmCtrl_Start() failed with %d", ret_val, 0U);
        SvcFlowControl_Assert();
    }

    SvcLog_DBG(INSLOG_TAG, "InsUT_Video_VoutFrmStart() finish.", 0U, 0U);
    return ret_val;
}



/**
* unittest for voutfrmctrl source set
* @return ErrorCode
*/
static int32_t InsUT_Video_VoutFrmSetSource(int32_t argc, char **argv)
{
    uint32_t ret_val = INSERR_OK;
    uint32_t videosrc =0;
    InsSvc_VoutFrmCtrlOpt_t* p_vofrmctrl_ut = NULL;

    (void)argc;
    (void)argv; 
    if(INSERR_OK!=InsUT_MediaMgr_InitCheck() 
        || INSERR_OK!=InsUT_MediaSvc_VideoCheck(g_pvideo_ut))
    {
        SvcLog_NG(INSLOG_TAG, "mediamgr not initialized.", 0U, 0U);
        return ERRMED_INVAL;
    }

    if ((argc < 1 || NULL == argv))
    {
        SvcLog_NG(INSLOG_TAG, "InsUT_Video_VencStart() invalid param.", 0U, 0U);
        return ERRMED_INVAL;
    }
    p_vofrmctrl_ut  = g_pvideo_ut->p_voutfrmctrl;

    videosrc = atoi(argv[0]);
    ret_val = InsSvc_VoutFrmCtrl_Ctrl(p_vofrmctrl_ut, SVC_VFC_TASK_CMD_VOUT_SOURCE, (void*)&videosrc);
    if (INSERR_OK != ret_val) 
    {
        SvcLog_NG(INSLOG_TAG, "InsSvc_VoutFrmCtrl_Ctrl() failed with %d", ret_val, 0U);
        SvcFlowControl_Assert();
    }

    SvcLog_DBG(INSLOG_TAG, "InsUT_Video_VoutFrmSetSource() finish, videosrc:%d", videosrc, 0U);
    return ret_val;
} 


/**
* unittest for voutfrmctrl source set
* @return ErrorCode
*/
static int32_t InsUT_Video_VoutFrmPushFrame(int32_t argc, char **argv) 
{
    uint32_t ret_val = INSERR_OK;
    InsSvc_Disp_Info_t disp_info;
    InsSvc_VoutFrmCtrlOpt_t* p_vofrmctrl_ut = NULL;
    InsSvc_DispCtrlOpt_t*    p_dispctrl_ut = NULL;
    InsSvc_YuvFeedFrameBuf_t yuv_frame;
    uint32_t width=0, height=0, pitch=0,frame_size=0, idx;
    uint32_t yuv_id=0;
    uint32_t vout_id =0;
    uint32_t color_val = 0xFF0000; //RGB
    uint32_t time_val = 3; //ms 
    int8_t* p_tmpbuf = NULL;   
    ULONG y_addr_value, uv_addr_value;
    
    (void)argc;
    (void)argv; 
    if(INSERR_OK!=InsUT_MediaMgr_InitCheck() 
        || INSERR_OK!=InsUT_MediaSvc_VideoCheck(g_pvideo_ut))
    {
        SvcLog_NG(INSLOG_TAG, "mediamgr not initialized.", 0U, 0U);
        return ERRMED_INVAL;
    }

    if ((argc < 3 || NULL == argv))
    {
        SvcLog_NG(INSLOG_TAG, "InsUT_Video_VoutFrmPushFrame() invalid param.", 0U, 0U);
        return ERRMED_INVAL;
    }

    vout_id   = atoi(argv[0]);
    color_val = atoi(argv[1]);
    time_val  = atoi(argv[2]); 

    p_vofrmctrl_ut = g_pvideo_ut->p_voutfrmctrl;
    p_dispctrl_ut  = g_pvideo_ut->p_dispctrl;

    /* get vout video info  */
    InsSvc_DispCtrl_GetInfo(p_dispctrl_ut, &disp_info);
    for(idx=0; idx< *(disp_info.pNumDisp); idx++)
    {
        if(vout_id==disp_info.pDispCfg[idx].VoutID)
        {
            yuv_id  = idx;
            width   = disp_info.pDispCfg[idx].VideoCfg.Window.Width;
            height  = disp_info.pDispCfg[idx].VideoCfg.Window.Height;
            pitch   = INSCOMM_ALIGN_UP(width, INS_ALIGN_64BYTE);
            frame_size = pitch*height*3/2;

            SvcLog_DBG(INSLOG_TAG, "InsUT_Video_VoutFrmPushFrame(), width:%d, pitch:%d", width, pitch);
            break;
        }
    }

    if(0==frame_size) 
    {
        SvcLog_NG(INSLOG_TAG, "voutid:%d for display is not found.", vout_id, 0U);
        return ERRMED_INVAL;        
    }   

    /* malloc buffer for vout default image */
    ret_val = InsPal_Memory_Malloc((void*)&p_tmpbuf, frame_size);
    if(INSERR_OK!=ret_val)
    {
        SvcLog_NG(INSLOG_TAG, "malloc buffer of vout defauld image fail, voutid:%d", vout_id, 0U);
        return ERRMED_INVAL; 
    }
    else
    {
        uint8_t r = (color_val>>16) & 0xFF;
        uint8_t g = (color_val>>8) & 0xFF;
        uint8_t b = (color_val) & 0xFF;
        uint8_t y,u,v;

        InsUT_RgbConvertToYuv(r,g,b,&y,&u,&v);

        SvcLog_DBG(INSLOG_TAG, "InsUT_Video_VoutFrmPushFrame(), r:0x%x, g:0x%x", r, g);
        SvcLog_DBG(INSLOG_TAG, "InsUT_Video_VoutFrmPushFrame(), b:0x%x, y:0x%x", b, y);
        SvcLog_DBG(INSLOG_TAG, "InsUT_Video_VoutFrmPushFrame(), u:0x%x, v:0x%x", u, v);  

        //memset(p_tmpbuf, 0, frame_size);
        InsUT_FillYuv420sp(p_tmpbuf, pitch, height, y, u, v);   
    }

    AmbaMisra_TypeCast(&y_addr_value, &p_tmpbuf); 
    uv_addr_value = y_addr_value + pitch*height; 

    yuv_frame.stream_id       = INS_YUV_STREAM_MAIN_MASK|yuv_id;
    yuv_frame.datafmt         = INS_DSP_YUV420;
    yuv_frame.cap_sequence    = 0;
    yuv_frame.buffer_size     = frame_size;
    yuv_frame.pitch           = pitch;
    yuv_frame.y_addr          = y_addr_value;
    yuv_frame.uv_addr         = uv_addr_value;

    //SvcLog_DBG(INSLOG_TAG, "InsUT_Video_VoutFrmPushFrame(), y_addr_value:0x%x, uv_addr_value:0x%x", y_addr_value, uv_addr_value);

    ret_val = InsSvc_VoutFrmCtrl_PushFrame((void*)p_vofrmctrl_ut, &yuv_frame);

    InsPal_Task_Sleep(time_val);

    if(p_tmpbuf!=NULL)
    {
        ret_val = InsPal_Memory_Free(p_tmpbuf);
        if(INSERR_OK!=ret_val)
        {
            SvcLog_NG(INSLOG_TAG, "free buffer of vout defauld image fail, vout_id:%d", vout_id, 0U);
        }
        p_tmpbuf = NULL;
    }

    SvcLog_DBG(INSLOG_TAG, "InsUT_Video_VoutFrmPushFrame() finish, color_val:0x%x, time_val:%d", color_val, time_val);
    return ret_val;
}


/**
* unittest for display start
* @return ErrorCode
*/
static int32_t InsUT_Video_DisplayStart(int32_t argc, char **argv)
{
    uint32_t ret_val = INSERR_OK;
    InsSvc_DispCtrlOpt_t* p_dispctrl_ut = NULL;

    (void)argc;
    (void)argv;  
    if(INSERR_OK!=InsUT_MediaMgr_InitCheck() 
        || INSERR_OK!=InsUT_MediaSvc_VideoCheck(g_pvideo_ut))
    {
        SvcLog_NG(INSLOG_TAG, "mediamgr not initialized.", 0U, 0U);
        return ERRMED_INVAL;
    }
    p_dispctrl_ut = g_pvideo_ut->p_dispctrl;

    ret_val = p_dispctrl_ut->dispctrl_start();

    SvcLog_DBG(INSLOG_TAG, "InsUT_Video_DisplayStart() finish.", 0U, 0U);
    return ret_val;
}


/**
* unittest for display stop
* @return ErrorCode
*/
static int32_t InsUT_Video_DisplayStop(int32_t argc, char **argv)
{
    uint32_t ret_val = INSERR_OK;
    InsSvc_DispCtrlOpt_t* p_dispctrl_ut = NULL;

    (void)argc;
    (void)argv;  
    if(INSERR_OK!=InsUT_MediaMgr_InitCheck() 
        || INSERR_OK!=InsUT_MediaSvc_VideoCheck(g_pvideo_ut))
    {
        SvcLog_NG(INSLOG_TAG, "mediamgr not initialized.", 0U, 0U);
        return ERRMED_INVAL;
    }
    p_dispctrl_ut = g_pvideo_ut->p_dispctrl;

    ret_val = p_dispctrl_ut->dispctrl_stop();

    SvcLog_DBG(INSLOG_TAG, "InsUT_Video_DisplayStop() finish.", 0U, 0U);
    return ret_val;
}


/**
* unittest for display resume
* @return ErrorCode
*/
static int32_t InsUT_Video_DisplayResume(int32_t argc, char **argv)
{
    uint32_t ret_val = INSERR_OK;
    InsSvc_DispCtrlOpt_t* p_dispctrl_ut = NULL;

    (void)argc;
    (void)argv;  
    if(INSERR_OK!=InsUT_MediaMgr_InitCheck() 
        || INSERR_OK!=InsUT_MediaSvc_VideoCheck(g_pvideo_ut))
    {
        SvcLog_NG(INSLOG_TAG, "mediamgr not initialized.", 0U, 0U);
        return ERRMED_INVAL;
    }
    p_dispctrl_ut = g_pvideo_ut->p_dispctrl;  

    ret_val = p_dispctrl_ut->dispctrl_resume();

    SvcLog_DBG(INSLOG_TAG, "InsUT_Video_DisplayResume() finish.", 0U, 0U);
    return ret_val;
}


/**
* unittest for dsp liveview start
* @return ErrorCode
*/
static int32_t InsUT_Video_DspLiveStart(int32_t argc, char **argv)
{
    uint32_t ret_val = INSERR_OK;
    InsSvc_LiveviewCtrlOpt_t* p_livectrl_ut  = NULL;

    (void)argc;
    (void)argv;
    if(INSERR_OK!=InsUT_MediaMgr_InitCheck() 
        || INSERR_OK!=InsUT_MediaSvc_VideoCheck(g_pvideo_ut))
    {
        SvcLog_NG(INSLOG_TAG, "mediamgr not initialized.", 0U, 0U);
        return ERRMED_INVAL;
    }
    p_livectrl_ut = g_pvideo_ut->p_livectrl;

    /* Config Liveview Task needed resource */
    if (INSERR_OK == ret_val) 
    {
        ret_val = p_livectrl_ut->liveviewctrl_config();
        if (INSERR_OK != ret_val) 
        {
            SvcLog_NG(INSLOG_TAG, "liveviewctrl_config() failed with %d", ret_val, 0U);
            SvcFlowControl_Assert();
        }
    }

    /* Start Liveview Task */
    if (INSERR_OK == ret_val) 
    {
        ret_val = p_livectrl_ut->liveviewctrl_start();
        if (INSERR_OK != ret_val) 
        {
            SvcLog_NG(INSLOG_TAG, "liveviewctrl_start() failed with %d", ret_val, 0U);
            SvcFlowControl_Assert();
        }
    }

    SvcLog_DBG(INSLOG_TAG, "InsUT_Video_DspLiveStart() finish.", 0U, 0U);
    return ret_val;
}


/**
* unittest for dsp liveview stop
* @return ErrorCode
*/
static int32_t InsUT_Video_DspLiveStop(int32_t argc, char **argv)
{
    uint32_t ret_val = INSERR_OK;
    InsSvc_LiveviewCtrlOpt_t* p_livectrl_ut  = NULL;

    (void)argc;
    (void)argv;
    if(INSERR_OK!=InsUT_MediaMgr_InitCheck() 
        || INSERR_OK!=InsUT_MediaSvc_VideoCheck(g_pvideo_ut))
    {
        SvcLog_NG(INSLOG_TAG, "mediamgr not initialized.", 0U, 0U);
        return ERRMED_INVAL;
    }
    p_livectrl_ut = g_pvideo_ut->p_livectrl;

    /* stop Liveview Task */
    ret_val =  p_livectrl_ut->liveviewctrl_stop();
    if (INSERR_OK != ret_val) 
    {
        SvcLog_NG(INSLOG_TAG, "liveviewctrl_stop() failed with %d", ret_val, 0U);
        SvcFlowControl_Assert();
    }

    SvcLog_DBG(INSLOG_TAG, "InsUT_Video_DspLiveStop() finish.", 0U, 0U);
    return ret_val;
}


/**
* unittest for dsp liveview dump
* @return ErrorCode
*/
static int32_t InsUT_Video_DspLiveDump(int32_t argc, char** argv)
{
    uint32_t ret_val = INSERR_OK;
    InsSvc_LiveviewCtrlOpt_t* p_livectrl_ut  = NULL;

    (void)argc;
    (void)argv;
    if(INSERR_OK!=InsUT_MediaMgr_InitCheck() 
        || INSERR_OK!=InsUT_MediaSvc_VideoCheck(g_pvideo_ut))
    {
        SvcLog_NG(INSLOG_TAG, "mediamgr not initialized.", 0U, 0U);
        return ERRMED_INVAL;
    }
    p_livectrl_ut = g_pvideo_ut->p_livectrl;
    InsSvc_LiveviewCtrl_Dump(p_livectrl_ut);   

    SvcLog_DBG(INSLOG_TAG, "InsUT_Video_DspLiveDump() finish.", 0U, 0U);
    return ret_val;
} 


/**
* unittest for venc start
* @return ErrorCode
*/
static int32_t InsUT_Video_VencStart(int32_t argc, char **argv)
{
    uint32_t ret_val = INSERR_OK;
    uint32_t stream_bits;
    InsSvc_VencCtrlOpt_t* p_vencctrl_ut = NULL;
    
    if(INSERR_OK!=InsUT_MediaMgr_InitCheck() 
        || INSERR_OK!=InsUT_MediaSvc_VideoCheck(g_pvideo_ut))
    {
        SvcLog_NG(INSLOG_TAG, "mediamgr not initialized.", 0U, 0U);
        return ERRMED_INVAL;
    }
 
    if ((argc < 1 || NULL == argv))
    {
        SvcLog_NG(INSLOG_TAG, "InsUT_Video_VencStart() invalid param.", 0U, 0U);
        return ERRMED_INVAL;
    }
    p_vencctrl_ut = g_pvideo_ut->p_vencctrl;

    stream_bits = atoi(argv[0]);
    p_vencctrl_ut->vencctrl_start(stream_bits);

    SvcLog_DBG(INSLOG_TAG, "InsUT_Video_VencStart() finish.", 0U, 0U);
    return ret_val;
}


/**
* unittest for venc stop
* @return ErrorCode
*/
static int32_t InsUT_Video_VencStop(int32_t argc, char **argv)
{
    uint32_t ret_val = INSERR_OK;
    uint32_t stream_bits;
    InsSvc_VencCtrlOpt_t* p_vencctrl_ut = NULL;
    
    if(INSERR_OK!=InsUT_MediaMgr_InitCheck() 
        || INSERR_OK!=InsUT_MediaSvc_VideoCheck(g_pvideo_ut))
    {
        SvcLog_NG(INSLOG_TAG, "mediamgr not initialized.", 0U, 0U);
        return ERRMED_INVAL;
    }

    if ((argc < 1 || NULL == argv))
    {
        SvcLog_NG(INSLOG_TAG, "InsUT_Video_VencStart() invalid param.", 0U, 0U);
        return ERRMED_INVAL;
    }
    p_vencctrl_ut = g_pvideo_ut->p_vencctrl;

    stream_bits = atoi(argv[0]);
    p_vencctrl_ut->vencctrl_stop(stream_bits);

    SvcLog_DBG(INSLOG_TAG, "InsUT_Video_VencStop() finish.", 0U, 0U);
    return ret_val;
}


/**
* unittest for start venc feed
* @return error code
*/
static int32_t InsUT_Video_VencFeedStart(int32_t argc, char **argv)
{
    uint32_t ret_val = INSERR_OK;
    uint32_t start_bits=1;
    InsSvc_VencFeedOpt_t* p_vencfeed_ut = NULL;

    if(INSERR_OK!=InsUT_MediaMgr_InitCheck() 
        || INSERR_OK!=InsUT_MediaSvc_VideoCheck(g_pvideo_ut))
    {
        SvcLog_NG(INSLOG_TAG, "mediamgr not initialized.", 0U, 0U);
        return ERRMED_INVAL;
    }

    if ((argc < 1 || NULL == argv))
    {
        SvcLog_NG(INSLOG_TAG, "InsUT_Video_VencFeedStart() invalid param.", 0U, 0U);
        return ERRMED_INVAL;
    }
    start_bits = atoi(argv[0]);
    p_vencfeed_ut = g_pvideo_ut->p_vencfeed;

    ret_val = InsSvc_VencFeed_CreateTask(p_vencfeed_ut);
    if(INSERR_OK!=ret_val)
    {
        SvcLog_NG(INSLOG_TAG, "InsSvc_VencFeed_CreateTask() fail:%d", ret_val, 0U);
    }
    else
    {
        ret_val = InsSvc_VencFeed_Start(p_vencfeed_ut, start_bits);   
        if(INSERR_OK!=ret_val)
        {
            SvcLog_NG(INSLOG_TAG, "InsSvc_VencFeed_Start() fail:%d, start_bits:%d", ret_val, start_bits);
        }
    }
    SvcLog_DBG(INSLOG_TAG, "InsUT_Video_VencFeedStart() finish, ret_val:%d, start_bits:%d", ret_val, start_bits);

    return ret_val;
}


/**
* unittest for stop venc feed
* @return error code
*/
static int32_t InsUT_Video_VencFeedStop(int32_t argc, char **argv)
{
    uint32_t ret_val = INSERR_OK;
    uint32_t stop_bits=1;
    InsSvc_VencFeedOpt_t* p_vencfeed_ut = NULL;

    if(INSERR_OK!=InsUT_MediaMgr_InitCheck() 
        || INSERR_OK!=InsUT_MediaSvc_VideoCheck(g_pvideo_ut))
    {
        SvcLog_NG(INSLOG_TAG, "mediamgr not initialized.", 0U, 0U);
        return ERRMED_INVAL;
    }

    if ((argc < 1 || NULL == argv))
    {
        SvcLog_NG(INSLOG_TAG, "InsUT_Video_VencFeedStop() invalid param.", 0U, 0U);
        return ERRMED_INVAL;
    }
    stop_bits = atoi(argv[0]);
    p_vencfeed_ut = g_pvideo_ut->p_vencfeed;

    ret_val = InsSvc_VencFeed_Stop(p_vencfeed_ut, stop_bits);
    if(INSERR_OK!=ret_val)
    {
        SvcLog_NG(INSLOG_TAG, "InsSvc_VencFeed_Stop() fail:0x%x, stop_bits:%d", ret_val, stop_bits);
    }

    ret_val = InsSvc_VencFeed_DeleteTask(p_vencfeed_ut);
    if(INSERR_OK!=ret_val)
    {
        SvcLog_NG(INSLOG_TAG, "InsSvc_VencFeed_DeleteTask() fail:%d", ret_val, 0U);
    }

    SvcLog_DBG(INSLOG_TAG, "InsUT_Video_VencFeedStop() finish, ret_val:%d, start_bits:%d", ret_val, stop_bits);
    return ret_val;
}



/**
* unittest for set venc feed stream bits
* @return error code
*/
static int32_t InsUT_Video_VencFeedSetStrmBits(int32_t argc, char **argv)
{
    uint32_t ret_val = INSERR_OK;
    uint32_t stream_bits=1, prev_bits=0U;
    InsSvc_VencFeedOpt_t* p_vencfeed_ut = NULL;
    InsSvc_VencCtrlOpt_t* p_vencctrl_ut = NULL;
    SVC_RES_CFG_s   *p_rescfg = SvcResCfg_Get();
    InsSvc_Enc_Info_t enc_info;

    if(INSERR_OK!=InsUT_MediaMgr_InitCheck() 
        || INSERR_OK!=InsUT_MediaSvc_VideoCheck(g_pvideo_ut))
    {
        SvcLog_NG(INSLOG_TAG, "mediamgr not initialized.", 0U, 0U);
        return ERRMED_INVAL;
    }

    if ((argc < 1 || NULL == argv))
    {
        SvcLog_NG(INSLOG_TAG, "InsUT_Video_VencFeedSetStrmBits() invalid param.", 0U, 0U);
        return ERRMED_INVAL;
    }
    stream_bits = atoi(argv[0]);

    p_vencfeed_ut = g_pvideo_ut->p_vencfeed;
    p_vencctrl_ut = g_pvideo_ut->p_vencctrl;

    /* get all the timelapse enc stream bits */
    for(uint32_t i = 0U; i < p_rescfg->RecNum; i++) 
    {
        if (p_rescfg->RecStrm[i].RecSetting.TimeLapse >0) 
        {
            prev_bits |= ((uint32_t)1U << i);
        }
        else
        {  
            if((stream_bits & (1U<<i)) >0)
            {
                p_rescfg->RecStrm[i].RecSetting.TimeLapse = 1;
                SvcLog_DBG(INSLOG_TAG, "InsUT_Video_VencFeedSetStrmBits(), modify strmid:%d to timelapse mode", (1U<<i), 0U);                
            }
        }
    }
    //stream_bits |= prev_bits;
    SvcLog_DBG(INSLOG_TAG, "InsUT_Video_VencFeedSetStrmBits(), stream_bits:0x%x, prev_bits:0x%x", stream_bits, prev_bits);     
    
    ret_val = InsSvc_VencFeed_SetFeedStreamBits(p_vencfeed_ut, stream_bits);
    if(INSERR_OK==ret_val)
    {
        /* updata venc info from res cfg */
        InsSvc_VencCtrl_GetEncInfo(p_vencctrl_ut, &enc_info);
        InsSvc_VencCtrl_SetEncInfo(p_vencctrl_ut, &enc_info);
    }

    SvcLog_DBG(INSLOG_TAG, "InsUT_Video_VencFeedSetStrmBits() finish, ret_val:%d", ret_val, 0U);
    return ret_val;
}


/**
* unittest for push frame to venc
* @return error code
*/
static int32_t InsUT_Video_VencFeedPushFrame(int32_t argc, char **argv)
{
    uint32_t ret_val = INSERR_OK;
    uint32_t venc_id=0, framerate=30, encode_secord=3;
    uint32_t width=0, height=0, pitch=0,frame_size=0;
    InsSvc_VencCtrlOpt_t* p_vencctrl_ut = NULL;
    InsSvc_VencFeedOpt_t* p_vencfeed_ut = NULL;
    InsSvc_Enc_Info_t enc_info;
    InsSvc_DspVideoEnc_StrmConfig_t* p_venc_strminfo=NULL;
    int8_t* p_tmpbuf=NULL;

    if(INSERR_OK!=InsUT_MediaMgr_InitCheck() 
        || INSERR_OK!=InsUT_MediaSvc_VideoCheck(g_pvideo_ut))
    {
        SvcLog_NG(INSLOG_TAG, "mediamgr not initialized.", 0U, 0U);
        return ERRMED_INVAL; 
    }

    if ((argc < 2 || NULL == argv))
    {
        SvcLog_NG(INSLOG_TAG, "InsUT_Video_VencFeedPushFrame() invalid param.", 0U, 0U);
        return ERRMED_INVAL;
    }
    venc_id       = atoi(argv[0]);
    encode_secord = atoi(argv[1]);
    encode_secord = (0==encode_secord)? 3:encode_secord; 

    SvcLog_DBG(INSLOG_TAG, "InsUT_Video_VencFeedPushFrame(), venc_id:%d, encode_secord:%d", venc_id, encode_secord);

    /* get venc info */
    p_vencctrl_ut = g_pvideo_ut->p_vencctrl;
    p_vencfeed_ut = g_pvideo_ut->p_vencfeed; 

    InsSvc_VencCtrl_GetEncInfo(p_vencctrl_ut, &enc_info);
    if(venc_id>=*enc_info.pNumStrm)
    {
        SvcLog_NG(INSLOG_TAG, "InsUT_Video_VencFeedPushFrame() fail, invalid venc id:%d", venc_id, 0U);
        return ERRMED_INVAL;
    }

    p_venc_strminfo = &enc_info.pStrmCfg[venc_id];
    if(p_venc_strminfo->IntervalCapture > 0)
    {
        width   = p_venc_strminfo->Window.Width;
        height  = INSCOMM_ALIGN_UP(p_venc_strminfo->Window.Height, INS_ALIGN_64BYTE);
        pitch   = width; //INSCOMM_ALIGN_UP(width, INS_ALIGN_128BYTE);
        framerate = p_venc_strminfo->FrameRate.TimeScale/p_venc_strminfo->FrameRate.NumUnitsInTick; 
        frame_size = pitch*height*3/2;

        SvcLog_DBG(INSLOG_TAG, "InsUT_Video_VencFeedPushFrame(), width:%d, height:%d", width, height);
        SvcLog_DBG(INSLOG_TAG, "InsUT_Video_VencFeedPushFrame(), framerate:%d", framerate, 0U);
    }  

    if(0==width
        ||0==height)
    {
        SvcLog_NG(INSLOG_TAG, "no timelapse venc stream found, venc_id:%d", venc_id, 0U);
        return ERRMED_INVAL;
    }
    
    /* malloc buffer for venc external yuv */
    ret_val = InsPal_Memory_Malloc((void*)&p_tmpbuf, frame_size);
    if(INSERR_OK!=ret_val)
    {
        SvcLog_NG(INSLOG_TAG, "malloc buffer of venc external yuv fail:0x%x", ret_val, 0U);
        return ERRMED_INVAL; 
    }
    else
    {
        uint32_t color=0xFF0000,rgb;
        uint32_t idx, time_val;
        uint8_t r,g,b;
        uint8_t y,u,v;
        ULONG y_addr_value, uv_addr_value;
        InsSvc_YuvFeedFrameBuf_t feed_frame;

        time_val = 1000/framerate;
        for(uint32_t i=0;i<encode_secord;i++)
        {
            idx = i%3;
            rgb = (color>>(idx*8));
            r = (rgb>>16) & 0xFF;
            g = (rgb>>8) & 0xFF;
            b = (rgb) & 0xFF;

            InsUT_RgbConvertToYuv(r,g,b,&y,&u,&v);
            InsUT_FillYuv420sp(p_tmpbuf, pitch, height, y, u, v);

            /* fill feed stream info */
            AmbaMisra_TypeCast(&y_addr_value, &p_tmpbuf); 
            uv_addr_value = y_addr_value + pitch*height; 

            memset(&feed_frame, 0U, sizeof(feed_frame));
            feed_frame.stream_id       = venc_id;
            feed_frame.datafmt         = INS_DSP_YUV420;
            feed_frame.cap_sequence    = 0;
            feed_frame.buffer_size     = frame_size;
            feed_frame.pitch           = pitch;
            feed_frame.y_addr          = y_addr_value;
            feed_frame.uv_addr         = uv_addr_value;

            for(uint32_t j=0; j<framerate; j++)
            {
                InsSvc_VencFeed_PushFrame((void*)p_vencfeed_ut, &feed_frame);
                InsPal_Task_Sleep(time_val);
            }                                   
        }
    }

    if(p_tmpbuf!=NULL)
    {
        ret_val = InsPal_Memory_Free(p_tmpbuf);
        if(INSERR_OK!=ret_val)
        {
            SvcLog_NG(INSLOG_TAG, "free buffer of venc external yuv fail:%d", ret_val, 0U);
        }
        p_tmpbuf = NULL;
    }

    SvcLog_DBG(INSLOG_TAG, "InsUT_Video_VencFeedPushFrame() finish, ret_val:%d", ret_val, 0U);
    return ret_val;
}


#define INSUT_CMDCNT_MEDIA_SERVICE_VIDEO        (19)
InsUT_Unittest_CmdInfo_t g_ut_cmd_media_video[INSUT_CMDCNT_MEDIA_SERVICE_VIDEO] = 
{
    {InsUT_Video_VinStart,              0, NULL, "vin_start",       "No Param(result:NG)"},
    {InsUT_Video_VinStop,               0, NULL, "vin_stop",        "No Param(result:OK)"},
    {InsUT_Video_VoutStart,             0, NULL, "vout_start",      "No Param(result:NG)"},
    {InsUT_Video_VoutStop,              0, NULL, "vout_stop",       "No Param(result:OK)"},
    {InsUT_Video_VoutFrmStart ,         0, NULL, "voutfrm_start",   "No Param(result:OK)"}, 
    //{InsUT_Video_VoutFrmStop},        0, NULL, "voutfrm_stop",    "No Param()"},  //longtz_debug
    {InsUT_Video_VoutFrmSetSource,      0, NULL, "voutfrm_source",  "<source>[0~2](result:OK)"},
    {InsUT_Video_VoutFrmPushFrame,      0, NULL, "voutfrm_push",    "<voutid>[0~1] <color>[rbg888] <time(ms)>(result:OK)"}, 
    {InsUT_Video_DisplayStart,          0, NULL, "display_start",   "No Param(result:OK)"},
    {InsUT_Video_DisplayStop,           0, NULL, "display_stop",    "No Param(result:OK)"},
    {InsUT_Video_DisplayResume,         0, NULL, "display_resume",  "No Param(result:OK)"},
    {InsUT_Video_DspLiveStart,          0, NULL, "dsplive_start",   "No Param(result:NG)"},
    {InsUT_Video_DspLiveStop,           0, NULL, "dsplive_stop",    "No Param(result:OK)"},
    {InsUT_Video_DspLiveDump,           0, NULL, "dsplive_dump",    "No Param(result:OK)"},
    {InsUT_Video_VencStart,             0, NULL, "venc_start",      "<stream_bits>[1|2|4|...](result:OK)"},
    {InsUT_Video_VencStop,              0, NULL, "venc_stop",       "<stream_bits>[1|2|4|...](result:OK)"},
    {InsUT_Video_VencFeedStart,         0, NULL, "vencfeed_start",  "<startbits>(result:)"},
    {InsUT_Video_VencFeedStop,          0, NULL, "vencfeed_stop",   "<stopbits>(result:)"},
    {InsUT_Video_VencFeedSetStrmBits,   0, NULL, "vencfeed_setbits","<strmbits> <time>(result:)"},
    {InsUT_Video_VencFeedPushFrame,     0, NULL, "vencfeed_push",   "<vencid> <time>(result:OK)"},
};

/**
* unittest for audio rec start
* @return error code
*/
static int32_t InsUT_Audio_RecStart(int32_t argc, char **argv)
{
    uint32_t ret_val = INSERR_OK;
    InsSvc_ArecFlowOpt_t* p_arec_flow_ut = NULL;

    INSPAL_UNUSED(&argc);
    INSPAL_UNUSED(&argv);

    if(INSERR_OK != InsUT_MediaMgr_InitCheck() ||
       INSERR_OK != InsUT_MediaSvc_AudioCheck(g_paudio_ut))
    {
        SvcLog_NG(INSLOG_TAG, "mediamgr not initialized.", 0U, 0U);
        return ERRMED_INVAL;
    }

    p_arec_flow_ut = g_paudio_ut->p_arecflow;

    p_arec_flow_ut->enc_audio_only(1);
    p_arec_flow_ut->start(1, 1);
    if(INSERR_OK != ret_val)
    {
        SvcLog_NG(INSLOG_TAG, "ret_p_arec_flow_ut start fail:%d", ret_val, 0U);
    }

    SvcLog_DBG(INSLOG_TAG, "InsUT_Audio_RecStart() finish, ret_val:%d", ret_val, 0U);
    return ret_val;
}

static int32_t InsUT_Audio_RecPcm2Wav(int32_t argc, char **argv)
{
    int32_t ret_val = INSERR_OK;
	InsUtil_Audio_PcmInfo_t pcm_info;
	const char   file_name[32] = "c:\\SVC_Aduio_0.pcm";	//"c:\\sample.pcm";	//SVC_Aduio_0.pcm
	const char   wav_file_name[32] = "c:\\SVC_Aduio_0.wav";

    INSPAL_UNUSED(&argc);
    INSPAL_UNUSED(&argv);
	
	pcm_info.num_channels = 4;
	pcm_info.sample_rate = 48000;
	pcm_info.bits_per_sample = 32;
	if(ENUM_ENCODE_PCM == get_encode_mode())
	{
		ret_val = InsUtil_Audio_Pcm2Wav(wav_file_name, file_name, &pcm_info);
		if(INSERR_OK != ret_val)
	    {
	        SvcLog_NG(INSLOG_TAG, "wav to pcm fail:%d", ret_val, 0U);
	    }
		else
		{
			SvcLog_NG(INSLOG_TAG, "wav to pcm ok:%d", ret_val, 0U);
		}
	}
	
    return ret_val;
}

/**
* unittest for audio rec stop
* @return error code
*/
static int32_t InsUT_Audio_RecStop(int32_t argc, char **argv)
{
    int32_t ret_val = INSERR_OK;
    InsSvc_ArecFlowOpt_t* p_arec_flow_ut = NULL;

    INSPAL_UNUSED(&argc);
    INSPAL_UNUSED(&argv);

    if(INSERR_OK != InsUT_MediaMgr_InitCheck() ||
       INSERR_OK != InsUT_MediaSvc_AudioCheck(g_paudio_ut))
    {
        SvcLog_NG(INSLOG_TAG, "mediamgr not initialized.", 0U, 0U);
        return ERRMED_INVAL;
    }

    p_arec_flow_ut = g_paudio_ut->p_arecflow;

    p_arec_flow_ut->stop(1, 1);
    if(INSERR_OK != ret_val)
    {
        SvcLog_NG(INSLOG_TAG, "ret_p_arec_flow_ut stop fail:%d", ret_val, 0U);
    }
    p_arec_flow_ut->enc_audio_only(0);

    SvcLog_DBG(INSLOG_TAG, "InsUT_Audio_RecStop() finish, ret_val:%d", ret_val, 0U);

	
	ret_val = InsUT_Audio_RecPcm2Wav(argc, argv);
	if(INSERR_OK != ret_val)
    {
        //SvcLog_NG(INSLOG_TAG, "ret_p_arec_flow_ut stop fail:%d", ret_val, 0U);
    }
	
    return ret_val;
}

/**
* unittest for audio rec start
* @return error code
*/
static InsPal_FS_File_t  *pFile;
static uint32_t           DecId;
static int32_t InsUT_Audio_PlayStart(int32_t argc, char **argv)
{
    uint32_t ret_val = INSERR_OK;
    InsSvc_AplayFlowOpt_t* p_aplay_flow_ut = NULL;
    UINT32       FileInfo;
    UINT8        audio_type = 0;

    INSPAL_UNUSED(&argc);
    INSPAL_UNUSED(&argv);

    if(INSERR_OK != InsUT_MediaMgr_InitCheck() ||
       INSERR_OK != InsUT_MediaSvc_AudioCheck(g_paudio_ut))
    {
        SvcLog_NG(INSLOG_TAG, "mediamgr not initialized.", 0U, 0U);
        return ERRMED_INVAL;
    }

    if ((argc < 1 || NULL == argv))
    {
        SvcLog_NG(INSLOG_TAG, "InsUT_Audio_RecStart() invalid param.", 0U, 0U);
        return ERRMED_INVAL;
    }

    audio_type = atoi(argv[0]);
    SvcLog_DBG(INSLOG_TAG, "InsUT Audio type is %d", audio_type, 0U);

    if (audio_type == AMBA_AUDIO_TYPE_PCM)
    {
        (void)InsPal_FS_Open("c:\\sample.pcm", "rb", &pFile);
    }
    else if (audio_type == AMBA_AUDIO_TYPE_AAC)
    {
        (void)InsPal_FS_Open("c:\\sample.aac", "rb", &pFile);
    }
    else
    {
        SvcLog_NG(INSLOG_TAG, "Parameter wrong, must PCM or AAC", 0U, 0U);
        return -1;
    }

    AmbaMisra_TypeCast32(&FileInfo, &pFile);

    p_aplay_flow_ut = g_paudio_ut->p_aplayflow;
    p_aplay_flow_ut->set_up(audio_type, AMBA_AUDIO_SOURCE_AUDIO, FileInfo, NULL, &DecId);
    p_aplay_flow_ut->feed_task_create(DecId);
    p_aplay_flow_ut->dec_start(DecId);
    p_aplay_flow_ut->oup_start(DecId);

    SvcLog_DBG(INSLOG_TAG, "InsUT_Audio_RecStart() finish, ret_val:%d", ret_val, 0U);
    return ret_val;
}

static int32_t InsUT_Audio_PlayStop(int32_t argc, char **argv)
{
    uint32_t ret_val = INSERR_OK;
    InsSvc_AplayFlowOpt_t* p_aplay_flow_ut = NULL;

    INSPAL_UNUSED(&argc);
    INSPAL_UNUSED(&argv);

    if(INSERR_OK != InsUT_MediaMgr_InitCheck() ||
       INSERR_OK != InsUT_MediaSvc_AudioCheck(g_paudio_ut))
    {
        SvcLog_NG(INSLOG_TAG, "mediamgr not initialized.", 0U, 0U);
        return ERRMED_INVAL;
    }

    p_aplay_flow_ut = g_paudio_ut->p_aplayflow;
    p_aplay_flow_ut->stop(DecId, 0);
    (void)InsPal_FS_Close(pFile);
    p_aplay_flow_ut->delete(DecId);
    p_aplay_flow_ut->feed_task_delete(DecId);

    SvcLog_DBG(INSLOG_TAG, "InsUT_Audio_RecStop() finish, ret_val:%d", ret_val, 0U);
    return ret_val;
}

#define INSUT_CMDCNT_MEDIA_SERVICE_AUDIO        (4)
InsUT_Unittest_CmdInfo_t g_ut_cmd_media_audio[INSUT_CMDCNT_MEDIA_SERVICE_AUDIO] =
{
    {InsUT_Audio_RecStart,              0, NULL, "arec_start",       "No Param(result:OK)"},
    {InsUT_Audio_RecStop,               0, NULL, "arec_stop",        "No Param(result:OK)"},
    {InsUT_Audio_PlayStart,             0, NULL, "aplay_start",             "0:PCM, 1:AAC"},
    {InsUT_Audio_PlayStop,              0, NULL, "aplay_stop",       "No Param(result:OK)"},
};


InsUT_Unittest_CmdInfo_t g_ut_cmd_media_service[INSUT_CMDCNT_MEDIA_SERVICE] =
{
    {NULL,                       INSUT_CMDCNT_MEDIA_SERVICE_VIDEO,  g_ut_cmd_media_video,   "video",          "No Param"},
    {NULL,                       INSUT_CMDCNT_MEDIA_SERVICE_AUDIO,  g_ut_cmd_media_audio,   "audio",          "No Param"},
    {NULL,                       0,                                 NULL,                   "photo",          "No Param"},
};


