音视频开发--Hi3516a+IMX290--读RTSP服务器的代码实现

main.c

extern int g_s32Quit ;

extern void *SAMPLE_VENC_1080P_CLASSIC(void *p);
int main(void)
{
	int s32MainFd,temp;
	struct timespec ts = { 2, 0 };
	pthread_t id;
	ringmalloc(256*1024);
	printf("RTSP server START\n");
	PrefsInit();
	printf("listen for client connecting...\n");
	signal(SIGINT, IntHandl);
	s32MainFd = tcp_listen(SERVER_RTSP_PORT_DEFAULT);
	if (ScheduleInit() == ERR_FATAL)
	{
		fprintf(stderr,"Fatal: Can't start scheduler %s, %i \nServer is aborting.\n", __FILE__, __LINE__);
		return 0;
	}
	RTP_port_pool_init(RTP_DEFAULT_PORT);
	pthread_create(&id,NULL,SAMPLE_VENC_1080P_CLASSIC,NULL);
	while (!g_s32Quit)
	{
		nanosleep(&ts, NULL);
		EventLoop(s32MainFd);
	}
	sleep(2);
	ringfree();
	printf("The Server quit!\n");

	return 0;
}

/******************************************************************************
* function :  H.264@1080p@30fps+H.265@1080p@30fps+H.264@D1@30fps
******************************************************************************/
extern int g_s32Quit ;
void *SAMPLE_VENC_1080P_CLASSIC(HI_VOID *arg)
{
    PAYLOAD_TYPE_E enPayLoad[3] = {PT_H265, PT_H265, PT_H265};
    PIC_SIZE_E enSize[3] = {PIC_HD1080, PIC_HD1080, PIC_D1};
    HI_U32 u32Profile = 0;

    VB_CONF_S stVbConf;
    SAMPLE_VI_CONFIG_S stViConfig = {0};

    VPSS_GRP VpssGrp;
    VPSS_CHN VpssChn;
    VPSS_GRP_ATTR_S stVpssGrpAttr;
    VPSS_CHN_ATTR_S stVpssChnAttr;
    VPSS_CHN_MODE_S stVpssChnMode;

    VENC_CHN VencChn;
    SAMPLE_RC_E enRcMode = SAMPLE_RC_CBR;

    HI_S32 s32ChnNum;

    HI_S32 s32Ret = HI_SUCCESS;
    HI_U32 u32BlkSize;
    SIZE_S stSize;
    char c;


    if ((SONY_IMX178_LVDS_5M_30FPS == SENSOR_TYPE)
        || (APTINA_AR0330_MIPI_1536P_25FPS == SENSOR_TYPE)
        || (APTINA_AR0330_MIPI_1296P_25FPS == SENSOR_TYPE))
    {
        s32ChnNum = 2;
    }
    else
    {
        s32ChnNum = 3;
    }
    s32ChnNum=1;
    /******************************************
     step  1: init sys variable
    ******************************************/
    memset(&stVbConf, 0, sizeof(VB_CONF_S));

    SAMPLE_COMM_VI_GetSizeBySensor(&enSize[0]);
    switch (SENSOR_TYPE)
    {
        case SONY_IMX178_LVDS_5M_30FPS:
        case APTINA_AR0330_MIPI_1536P_25FPS:
        case APTINA_AR0330_MIPI_1296P_25FPS:
            enSize[1] = PIC_VGA;
            break;
        default:
            break;
    }

    stVbConf.u32MaxPoolCnt = 128;

    /*video buffer*/
    u32BlkSize = SAMPLE_COMM_SYS_CalcPicVbBlkSize(gs_enNorm, \
                 enSize[0], SAMPLE_PIXEL_FORMAT, SAMPLE_SYS_ALIGN_WIDTH);
    stVbConf.astCommPool[0].u32BlkSize = u32BlkSize;
    stVbConf.astCommPool[0].u32BlkCnt = 10;

    u32BlkSize = SAMPLE_COMM_SYS_CalcPicVbBlkSize(gs_enNorm, \
                 enSize[1], SAMPLE_PIXEL_FORMAT, SAMPLE_SYS_ALIGN_WIDTH);
    stVbConf.astCommPool[1].u32BlkSize = u32BlkSize;
    stVbConf.astCommPool[1].u32BlkCnt = 10;

    u32BlkSize = SAMPLE_COMM_SYS_CalcPicVbBlkSize(gs_enNorm, \
                 enSize[2], SAMPLE_PIXEL_FORMAT, SAMPLE_SYS_ALIGN_WIDTH);
    stVbConf.astCommPool[2].u32BlkSize = u32BlkSize;
    stVbConf.astCommPool[2].u32BlkCnt = 10;


    /******************************************
     step 2: mpp system init.
    ******************************************/
    s32Ret = SAMPLE_COMM_SYS_Init(&stVbConf);
    if (HI_SUCCESS != s32Ret)
    {
        SAMPLE_PRT("system init failed with %d!\n", s32Ret);
        goto END_VENC_1080P_CLASSIC_0;
    }

    /******************************************
     step 3: start vi dev & chn to capture
    ******************************************/
    stViConfig.enViMode   = SENSOR_TYPE;
    stViConfig.enRotate   = ROTATE_NONE;
    stViConfig.enNorm     = VIDEO_ENCODING_MODE_AUTO;
    stViConfig.enViChnSet = VI_CHN_SET_NORMAL;
    stViConfig.enWDRMode  = WDR_MODE_NONE;
    s32Ret = SAMPLE_COMM_VI_StartVi(&stViConfig);
    if (HI_SUCCESS != s32Ret)
    {
        SAMPLE_PRT("start vi failed!\n");
        goto END_VENC_1080P_CLASSIC_1;
    }

    /******************************************
     step 4: start vpss and vi bind vpss
    ******************************************/
    s32Ret = SAMPLE_COMM_SYS_GetPicSize(gs_enNorm, enSize[0], &stSize);
    if (HI_SUCCESS != s32Ret)
    {
        SAMPLE_PRT("SAMPLE_COMM_SYS_GetPicSize failed!\n");
        goto END_VENC_1080P_CLASSIC_1;
    }

    VpssGrp = 0;
    stVpssGrpAttr.u32MaxW = stSize.u32Width;
    stVpssGrpAttr.u32MaxH = stSize.u32Height;
    stVpssGrpAttr.bIeEn = HI_FALSE;
    stVpssGrpAttr.bNrEn = HI_TRUE;
    stVpssGrpAttr.bHistEn = HI_FALSE;
    stVpssGrpAttr.bDciEn = HI_FALSE;
    stVpssGrpAttr.enDieMode = VPSS_DIE_MODE_NODIE;
    stVpssGrpAttr.enPixFmt = SAMPLE_PIXEL_FORMAT;

    s32Ret = SAMPLE_COMM_VPSS_StartGroup(VpssGrp, &stVpssGrpAttr);
    if (HI_SUCCESS != s32Ret)
    {
        SAMPLE_PRT("Start Vpss failed!\n");
        goto END_VENC_1080P_CLASSIC_2;
    }

    s32Ret = SAMPLE_COMM_VI_BindVpss(stViConfig.enViMode);
    if (HI_SUCCESS != s32Ret)
    {
        SAMPLE_PRT("Vi bind Vpss failed!\n");
        goto END_VENC_1080P_CLASSIC_3;
    }

    VpssChn = 0;
    stVpssChnMode.enChnMode      = VPSS_CHN_MODE_USER;
    stVpssChnMode.bDouble        = HI_FALSE;
    stVpssChnMode.enPixelFormat  = SAMPLE_PIXEL_FORMAT;
    stVpssChnMode.u32Width       = stSize.u32Width;
    stVpssChnMode.u32Height      = stSize.u32Height;
    stVpssChnMode.enCompressMode = COMPRESS_MODE_SEG;
    memset(&stVpssChnAttr, 0, sizeof(stVpssChnAttr));
    stVpssChnAttr.s32SrcFrameRate = -1;
    stVpssChnAttr.s32DstFrameRate = -1;
    s32Ret = SAMPLE_COMM_VPSS_EnableChn(VpssGrp, VpssChn, &stVpssChnAttr, &stVpssChnMode, HI_NULL);
    if (HI_SUCCESS != s32Ret)
    {
        SAMPLE_PRT("Enable vpss chn failed!\n");
        goto END_VENC_1080P_CLASSIC_4;
    }

    //VpssChn = 1;
    //stVpssChnMode.enChnMode       = VPSS_CHN_MODE_USER;
    //stVpssChnMode.bDouble         = HI_FALSE;
    //stVpssChnMode.enPixelFormat   = SAMPLE_PIXEL_FORMAT;
    //stVpssChnMode.u32Width        = stSize.u32Width;
    //stVpssChnMode.u32Height       = stSize.u32Height;
    //stVpssChnMode.enCompressMode  = COMPRESS_MODE_SEG;
    //stVpssChnAttr.s32SrcFrameRate = -1;
    //stVpssChnAttr.s32DstFrameRate = -1;
    //s32Ret = SAMPLE_COMM_VPSS_EnableChn(VpssGrp, VpssChn, &stVpssChnAttr, &stVpssChnMode, HI_NULL);
    //if (HI_SUCCESS != s32Ret)
    //{
    //    SAMPLE_PRT("Enable vpss chn failed!\n");
    //    goto END_VENC_1080P_CLASSIC_4;
    //}

   // if ((SONY_IMX178_LVDS_5M_30FPS != SENSOR_TYPE)
   //     && (APTINA_AR0330_MIPI_1536P_25FPS != SENSOR_TYPE)
   //     && (APTINA_AR0330_MIPI_1296P_25FPS != SENSOR_TYPE))
   // {

   //     VpssChn = 2;
   //     stVpssChnMode.enChnMode 	= VPSS_CHN_MODE_USER;
   //     stVpssChnMode.bDouble		= HI_FALSE;
   //     stVpssChnMode.enPixelFormat = SAMPLE_PIXEL_FORMAT;
   //     stVpssChnMode.u32Width		= 720;
   //     stVpssChnMode.u32Height 	= (VIDEO_ENCODING_MODE_PAL == gs_enNorm) ? 576 : 480;;
   //     stVpssChnMode.enCompressMode = COMPRESS_MODE_NONE;

    //    stVpssChnAttr.s32SrcFrameRate = -1;
   //     stVpssChnAttr.s32DstFrameRate = -1;

   //     s32Ret = SAMPLE_COMM_VPSS_EnableChn(VpssGrp, VpssChn, &stVpssChnAttr, &stVpssChnMode, HI_NULL);
   //     if (HI_SUCCESS != s32Ret)
   //     {
    //        SAMPLE_PRT("Enable vpss chn failed!\n");
    //        goto END_VENC_1080P_CLASSIC_4;
    //    }
    //}
    /******************************************
     step 5: start stream venc
    ******************************************/
    /*** HD1080P **/
   // printf("\t c) cbr.\n");
    //printf("\t v) vbr.\n");
   // printf("\t f) fixQp\n");
   // printf("please input choose rc mode!\n");
   // c = (char)getchar();
   // switch (c)
   // {
    //    case 'c':
            enRcMode = SAMPLE_RC_CBR;
    //        break;
    //    case 'v':
    //        enRcMode = SAMPLE_RC_VBR;
    //        break;
    //    case 'f':
    //        enRcMode = SAMPLE_RC_FIXQP;
    //        break;
    //    default:
    //        printf("rc mode! is invaild!\n");
    //        goto END_VENC_1080P_CLASSIC_4;
   // }
    VpssGrp = 0;
    VpssChn = 0;
    VencChn = 0;
    s32Ret = SAMPLE_COMM_VENC_Start(VencChn, enPayLoad[1], \
                                    gs_enNorm, enSize[1], enRcMode, u32Profile);
    if (HI_SUCCESS != s32Ret)
    {
        SAMPLE_PRT("Start Venc failed!\n");
        goto END_VENC_1080P_CLASSIC_5;
    }

    s32Ret = SAMPLE_COMM_VENC_BindVpss(VencChn, VpssGrp, VpssChn);
    if (HI_SUCCESS != s32Ret)
    {
        SAMPLE_PRT("Start Venc failed!\n");
        goto END_VENC_1080P_CLASSIC_5;
    }

    /*** 1080p **/
   // VpssChn = 1;
   // VencChn = 1;
   // s32Ret = SAMPLE_COMM_VENC_Start(VencChn, enPayLoad[1], \
   //                                 gs_enNorm, enSize[1], enRcMode, u32Profile);
   // if (HI_SUCCESS != s32Ret)
   // {
   //     SAMPLE_PRT("Start Venc failed!\n");
  //      goto END_VENC_1080P_CLASSIC_5;
   // }

  //  s32Ret = SAMPLE_COMM_VENC_BindVpss(VencChn, VpssGrp, VpssChn);
  //  if (HI_SUCCESS != s32Ret)
  //  {
 //       SAMPLE_PRT("Start Venc failed!\n");
  //      goto END_VENC_1080P_CLASSIC_5;
 //   }

    /*** D1 **/
    //if (SONY_IMX178_LVDS_5M_30FPS != SENSOR_TYPE)
   // {
   //     VpssChn = 2;
   //     VencChn = 2;
  //      s32Ret = SAMPLE_COMM_VENC_Start(VencChn, enPayLoad[2], \
  //                                      gs_enNorm, enSize[2], enRcMode, u32Profile);
 //       if (HI_SUCCESS != s32Ret)
 //       {
//            SAMPLE_PRT("Start Venc failed!\n");
 //           goto END_VENC_1080P_CLASSIC_5;
//        }

   //     s32Ret = SAMPLE_COMM_VENC_BindVpss(VencChn, VpssGrp, VpssChn);
   //     if (HI_SUCCESS != s32Ret)
   //     {
   //         SAMPLE_PRT("Start Venc failed!\n");
   //         goto END_VENC_1080P_CLASSIC_5;
  //      }
  //  }
    /******************************************
     step 6: stream venc process -- get stream, then save it to file.
    ******************************************/
    s32Ret = SAMPLE_COMM_VENC_StartGetStream(s32ChnNum);
    if (HI_SUCCESS != s32Ret)
    {
        SAMPLE_PRT("Start Venc failed!\n");
        goto END_VENC_1080P_CLASSIC_5;
    }

    printf("please press twice ENTER to exit this sample\n");
    getchar();
    getchar();
g_s32Quit=-1;
    /******************************************
     step 7: exit process
    ******************************************/
    SAMPLE_COMM_VENC_StopGetStream();

END_VENC_1080P_CLASSIC_5:
    VpssGrp = 0;

    VpssChn = 0;
    VencChn = 0;
    SAMPLE_COMM_VENC_UnBindVpss(VencChn, VpssGrp, VpssChn);
    SAMPLE_COMM_VENC_Stop(VencChn);

   // VpssChn = 1;
   // VencChn = 1;
   // SAMPLE_COMM_VENC_UnBindVpss(VencChn, VpssGrp, VpssChn);
  //  SAMPLE_COMM_VENC_Stop(VencChn);


   // if (SONY_IMX178_LVDS_5M_30FPS != SENSOR_TYPE)
   // {
   //     VpssChn = 2;
  //      VencChn = 2;
  //      SAMPLE_COMM_VENC_UnBindVpss(VencChn, VpssGrp, VpssChn);
  //      SAMPLE_COMM_VENC_Stop(VencChn);
 //   }

    SAMPLE_COMM_VI_UnBindVpss(stViConfig.enViMode);
END_VENC_1080P_CLASSIC_4:	//vpss stop
    VpssGrp = 0;
    VpssChn = 0;
    SAMPLE_COMM_VPSS_DisableChn(VpssGrp, VpssChn);
    //VpssChn = 1;
    //SAMPLE_COMM_VPSS_DisableChn(VpssGrp, VpssChn);
    //if (SONY_IMX178_LVDS_5M_30FPS != SENSOR_TYPE)
   // {
   //     VpssChn = 2;
   //     SAMPLE_COMM_VPSS_DisableChn(VpssGrp, VpssChn);
   // }
END_VENC_1080P_CLASSIC_3:    //vpss stop
    SAMPLE_COMM_VI_UnBindVpss(stViConfig.enViMode);
END_VENC_1080P_CLASSIC_2:    //vpss stop
    SAMPLE_COMM_VPSS_StopGroup(VpssGrp);
END_VENC_1080P_CLASSIC_1:	//vi stop
    SAMPLE_COMM_VI_StopVi(&stViConfig);
END_VENC_1080P_CLASSIC_0:	//system exit
    SAMPLE_COMM_SYS_Exit();

    return;
    //return s32Ret;
}

相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页