HI_VOID * SampleGet1D1Mjpeg1CifH264Stream(HI_VOID *p) { HI_S32 i; HI_S32 maxfd = 0; HI_S32 s32ret; HI_S32 VencFd[2] = {0}; HI_U32 u32FrameIdx = 0; VENC_CHN_STAT_S stStat; VENC_STREAM_S stStream; HI_CHAR *pszStreamFile[] = {"CIFH264stream.h264","D1MJPEGstream.mjp"}; struct timeval TimeoutVal; fd_set read_fds; FILE *pFd[2] = {0}; for (i=0;i<2;i++) { VencFd[i] = HI_MPI_VENC_GetFd(i); if (VencFd[i] <= 0) { printf("HI_MPI_VENC_GetFd err 0x%x\n",s32ret); return NULL; } /*open file to save stream */ pFd[i] = fopen(pszStreamFile[i], "wb"); if (!pFd[i]) { printf("open file err!\n"); return NULL; } if(maxfd <= VencFd[i]) { maxfd = VencFd[i]; } } do{ FD_ZERO(&read_fds); for (i=0; i<2;i++) { FD_SET(VencFd[i],&read_fds); } TimeoutVal.tv_sec = 2; TimeoutVal.tv_usec = 0; s32ret = select(maxfd + 1,&read_fds,NULL,NULL,&TimeoutVal); if(s32ret < 0) { printf("select err\n"); return NULL; } else if(s32ret == 0) { printf("time out\n"); return NULL; } else { for (i=0; i<2;i++) { if(FD_ISSET(VencFd[i],&read_fds)) { memset(&stStream,0,sizeof(stStream)); s32ret = HI_MPI_VENC_Query(i, &stStat); if (s32ret != HI_SUCCESS) { printf("HI_MPI_VENC_Query:0x%x\n",s32ret); fflush(stdout); return NULL; } stStream.pstPack = (VENC_PACK_S*)malloc(sizeof(VENC_PACK_S)*stStat.u32CurPacks); if (NULL == stStream.pstPack) { printf("malloc stream pack faild!\n"); return NULL; } stStream.u32PackCount = stStat.u32CurPacks; s32ret = HI_MPI_VENC_GetStream(i, &stStream, HI_IO_NOBLOCK); if (s32ret != HI_SUCCESS) { free(stStream.pstPack); stStream.pstPack = NULL; printf("HI_MPI_VENC_GetStream err 0x%x\n",s32ret); return NULL; } if(0 == i) { SampleSaveH264Stream(pFd[i], &stStream); } else if(1 == i) { SampleSaveJpegStream(pFd[i],&stStream); } s32ret = HI_MPI_VENC_ReleaseStream(i,&stStream); if (s32ret) { free(stStream.pstPack); stStream.pstPack = NULL; printf("HI_MPI_VENC_ReleaseStream err 0x%x\n",s32ret); return NULL; } free(stStream.pstPack); stStream.pstPack = NULL; } } } u32FrameIdx ++; }while(u32FrameIdx <= 1000); for (i=0;i<2;i++) { fclose(pFd[i]); } return HI_SUCCESS; }
void LiveVideoStreamSource::deliverFrame() { // This function is called when new frame data is available from the device. // We deliver this data by copying it to the 'downstream' object, using the following parameters (class members): // 'in' parameters (these should *not* be modified by this function): // fTo: The frame data is copied to this address. // (Note that the variable "fTo" is *not* modified. Instead, // the frame data is copied to the address pointed to by "fTo".) // fMaxSize: This is the maximum number of bytes that can be copied // (If the actual frame is larger than this, then it should // be truncated, and "fNumTruncatedBytes" set accordingly.) // 'out' parameters (these are modified by this function): // fFrameSize: Should be set to the delivered frame size (<= fMaxSize). // fNumTruncatedBytes: Should be set iff the delivered frame would have been // bigger than "fMaxSize", in which case it's set to the number of bytes // that have been omitted. // fPresentationTime: Should be set to the frame's presentation time // (seconds, microseconds). This time must be aligned with 'wall-clock time' - i.e., the time that you would get // by calling "gettimeofday()". // fDurationInMicroseconds: Should be set to the frame's duration, if known. // If, however, the device is a 'live source' (e.g., encoded from a camera or microphone), then we probably don't need // to set this variable, because - in this case - data will never arrive 'early'. // Note the code below. // we're not ready for the data yet if (!isCurrentlyAwaitingData()) { printf("Frame LOSS!!!!!!!!\n"); return; } VENC_CHN_STAT_S stStat; VENC_STREAM_S stStream; HI_S32 s32Ret; s32Ret = HI_MPI_VENC_Query(fChannelNo, &stStat); if (HI_SUCCESS != s32Ret) { return; } if (stStat.u32CurPacks <= 0) { return; } stStream.pstPack = (VENC_PACK_S *)alloca(sizeof(VENC_PACK_S) * stStat.u32CurPacks); stStream.u32PackCount = stStat.u32CurPacks; stStream.u32Seq = 0; memset(&stStream.stH264Info, 0, sizeof(VENC_STREAM_INFO_H264_S)); s32Ret = HI_MPI_VENC_GetStream(fChannelNo, &stStream, HI_FALSE); if (HI_SUCCESS != s32Ret) { g_critical("HI_MPI_VENC_GetStream failed with %#x!\n", s32Ret); return; } fPresentationTime.tv_sec = stStream.pstPack[0].u64PTS / 1000000UL; fPresentationTime.tv_usec = stStream.pstPack[0].u64PTS % 1000000UL; fFrameSize = 0; for (int i = 0; i < stStream.u32PackCount; i++) { for (int j = 0; j < ARRAY_SIZE(stStream.pstPack[i].pu8Addr); j++) { HI_U8 *p = stStream.pstPack[i].pu8Addr[j]; HI_U32 len = stStream.pstPack[i].u32Len[j]; if (len == 0) continue; if (len >= 3 && p[0] == 0x00 && p[1] == 0x00 && p[2] == 0x01) { p += 3; len -= 3; } if (len >= 4 && p[0] == 0x00 && p[1] == 0x00 && p[2] == 0x00 && p[3] == 0x01) { p += 4; len -= 4; } if (fFrameSize + len > fMaxSize) { g_critical("Package Length execute the fMaxSize\n"); break; } memmove(&fTo[fFrameSize], p, len); fFrameSize += len; } } s32Ret = HI_MPI_VENC_ReleaseStream(fChannelNo, &stStream); if (HI_SUCCESS != s32Ret) { g_critical("HI_MPI_VENC_ReleaseStream failed with %#x!\n", s32Ret); } doStopGettingFrames(); // After delivering the data, inform the reader that it is now available: FramedSource::afterGetting(this); }
/*获取单路H264码流*/ HI_VOID* SampleGetH264Stream(HI_VOID *p) { HI_S32 s32Ret; HI_S32 s32VencFd; HI_U32 u32FrameIdx = 0; VENC_CHN VeChn; VENC_CHN_STAT_S stStat; VENC_STREAM_S stStream; fd_set read_fds; FILE *pFile = NULL; pFile = fopen("stream.h264","wb"); if(pFile == NULL) { HI_ASSERT(0); return NULL; } VeChn = (HI_S32)p; s32VencFd = HI_MPI_VENC_GetFd(VeChn); do{ FD_ZERO(&read_fds); FD_SET(s32VencFd,&read_fds); s32Ret = select(s32VencFd+1, &read_fds, NULL, NULL, NULL); if (s32Ret < 0) { printf("select err\n"); return NULL; } else if (0 == s32Ret) { printf("time out\n"); return NULL; } else { if (FD_ISSET(s32VencFd, &read_fds)) { s32Ret = HI_MPI_VENC_Query(VeChn, &stStat); if (s32Ret != HI_SUCCESS) { printf("HI_MPI_VENC_Query:0x%x err\n",s32Ret); fflush(stdout); return NULL; } stStream.pstPack = (VENC_PACK_S*)malloc(sizeof(VENC_PACK_S)*stStat.u32CurPacks); if (NULL == stStream.pstPack) { printf("malloc memory err!\n"); return NULL; } stStream.u32PackCount = stStat.u32CurPacks; s32Ret = HI_MPI_VENC_GetStream(VeChn, &stStream, HI_TRUE); if (HI_SUCCESS != s32Ret) { printf("HI_MPI_VENC_GetStream:0x%x\n",s32Ret); free(stStream.pstPack); stStream.pstPack = NULL; return NULL; } SampleSaveH264Stream(pFile, &stStream); s32Ret = HI_MPI_VENC_ReleaseStream(VeChn,&stStream); if (s32Ret) { printf("HI_MPI_VENC_ReleaseStream:0x%x\n",s32Ret); free(stStream.pstPack); stStream.pstPack = NULL; return NULL; } free(stStream.pstPack); stStream.pstPack = NULL; } } u32FrameIdx ++; }while (bVencStopFlag != HI_TRUE); fclose(pFile); return HI_SUCCESS; }
HI_VOID * SampleGet4HD1Stream(HI_VOID *p) { HI_S32 i; HI_S32 maxfd = 0; HI_S32 s32ret; HI_S32 VencFd[4] = {0}; HI_U32 u32FrameIdx = 0; VENC_CHN_STAT_S stStat; VENC_STREAM_S stStream; HI_CHAR *pszStreamFile[] = {"HD1stream0.h264","HD1stream1.h264", "HD1stream2.h264","HD1stream3.h264"}; struct timeval TimeoutVal; fd_set read_fds; FILE *pFd[4] = {0}; for (i=0;i<4;i++) { VencFd[i] = HI_MPI_VENC_GetFd(i); if (VencFd[i] <= 0) { printf("HI_MPI_VENC_GetFd err 0x%x\n",s32ret); return NULL; } /*open file to save stream */ pFd[i] = fopen(pszStreamFile[i], "wb"); if (!pFd[i]) { printf("open file err!\n"); return NULL; } if(maxfd <= VencFd[i]) maxfd = VencFd[i] ; } do{ FD_ZERO(&read_fds); for (i=0; i<4;i++) { FD_SET(VencFd[i],&read_fds); } TimeoutVal.tv_sec = 2; TimeoutVal.tv_usec = 0; s32ret = select(maxfd + 1,&read_fds,NULL,NULL,&TimeoutVal); if(s32ret < 0) { printf("select err\n"); return NULL; } else if(s32ret == 0) { printf("time out\n"); continue; } else { for (i=0; i<4;i++) { if(FD_ISSET(VencFd[i],&read_fds)) { memset(&stStream,0,sizeof(stStream)); s32ret = HI_MPI_VENC_Query(i, &stStat); if (s32ret != HI_SUCCESS) { printf("HI_MPI_VENC_Query:0x%x\n",s32ret); return NULL; } stStream.pstPack = (VENC_PACK_S*)malloc(sizeof(VENC_PACK_S)*stStat.u32CurPacks); if (NULL == stStream.pstPack) { printf("malloc stream pack err!\n"); return NULL; } stStream.u32PackCount = stStat.u32CurPacks; s32ret = HI_MPI_VENC_GetStream(i, &stStream, HI_TRUE); if (s32ret != HI_SUCCESS) { free(stStream.pstPack); stStream.pstPack = NULL; printf("HI_MPI_VENC_GetStream err 0x%x\n",s32ret); return NULL; } SampleSaveH264Stream(pFd[i], &stStream); s32ret = HI_MPI_VENC_ReleaseStream(i,&stStream); if (s32ret != HI_SUCCESS) { free(stStream.pstPack); stStream.pstPack = NULL; printf("HI_MPI_VENC_ReleaseStream err 0x%x\n",s32ret); return NULL; } free(stStream.pstPack); stStream.pstPack = NULL; } } } u32FrameIdx ++; }while(u32FrameIdx <= 0xfff); for (i=0;i<4;i++) { fclose(pFd[i]); } return HI_SUCCESS; }
HI_S32 SampleSaveSnapPic(VENC_CHN SnapChn,FILE *pFile) { HI_S32 s32Ret; HI_S32 s32VencFd; VENC_CHN_STAT_S stStat; VENC_STREAM_S stStream; fd_set read_fds; s32VencFd = HI_MPI_VENC_GetFd(SnapChn); if(s32VencFd < 0) { printf("HI_MPI_VENC_GetFd err \n"); return HI_FAILURE; } FD_ZERO(&read_fds); FD_SET(s32VencFd,&read_fds); s32Ret = select(s32VencFd+1, &read_fds, NULL, NULL, NULL); if (s32Ret < 0) { printf("select err\n"); return HI_FAILURE; } else if (0 == s32Ret) { printf("time out\n"); return HI_FAILURE; } else { if (FD_ISSET(s32VencFd, &read_fds)) { s32Ret = HI_MPI_VENC_Query(SnapChn, &stStat); if (s32Ret != HI_SUCCESS) { printf("HI_MPI_VENC_Query:0x%x\n",s32Ret); fflush(stdout); return HI_FAILURE; } stStream.pstPack = (VENC_PACK_S*)malloc(sizeof(VENC_PACK_S)*stStat.u32CurPacks); if (NULL == stStream.pstPack) { printf("malloc memory err!\n"); return HI_FAILURE; } stStream.u32PackCount = stStat.u32CurPacks; s32Ret = HI_MPI_VENC_GetStream(SnapChn, &stStream, HI_TRUE); if (HI_SUCCESS != s32Ret) { printf("HI_MPI_VENC_GetStream:0x%x\n",s32Ret); fflush(stdout); free(stStream.pstPack); stStream.pstPack = NULL; return HI_FAILURE; } s32Ret = SampleSaveJpegStream(pFile, &stStream); if (HI_SUCCESS != s32Ret) { printf("HI_MPI_VENC_GetStream:0x%x\n",s32Ret); fflush(stdout); free(stStream.pstPack); stStream.pstPack = NULL; return HI_FAILURE; } s32Ret = HI_MPI_VENC_ReleaseStream(SnapChn,&stStream); if (s32Ret) { printf("HI_MPI_VENC_ReleaseStream:0x%x\n",s32Ret); free(stStream.pstPack); stStream.pstPack = NULL; return HI_FAILURE; } free(stStream.pstPack); stStream.pstPack = NULL; } } return HI_SUCCESS; }
void SendVideoStream(int Sockfd) { HI_S32 s32Ret; HI_S32 Vencfd, maxfd = 0; PAYLOAD_TYPE_E enPayLoad = PT_H264; SAMPLE_RC_E enRcMode = SAMPLE_RC_CBR; struct timeval TimeoutVal; fd_set read_fds; int connfd; VENC_STREAM_S stStream; VENC_CHN_STAT_S stStat; HI_S32 i; int SendSockfd; struct sockaddr_in SendServaddr; SendSockfd = socket(AF_INET, SOCK_DGRAM, 0); if (SendSockfd == -1) { perror("send socket error"); exit(0); } memset(&SendServaddr, 0, sizeof(SendServaddr)); SendServaddr.sin_family = AF_INET; SendServaddr.sin_addr.s_addr = inet_addr("10.1.2.60"); SendServaddr.sin_port = htons(6666); while (!isThreadStop) { #if 0 if (!isChangePic) { connfd = accept(SendSockfd, NULL, NULL); printf("connect!\n"); } isChangePic = HI_FALSE; #endif s32Ret = SAMPLE_COMM_VENC_Start(SendVencGrp, SendVencChn, enPayLoad, gs_enNorm, enSize, enRcMode); if (HI_SUCCESS != s32Ret) { printf("SAMPLE_COMM_VENC_Start failed with err code %#x\n", s32Ret); continue; } s32Ret = SAMPLE_COMM_VENC_BindVpss(SendVencGrp, VpssGrp, VpssChn); if (HI_SUCCESS != s32Ret) { printf("SAMPLE_COMM_VENC_BindVpss failed with err code %#x\n", s32Ret); continue; } Vencfd = HI_MPI_VENC_GetFd(SendVencChn); if (Vencfd < 0) { printf("HI_MPI_VENC_GetFd faild with%#x!\n", Vencfd); return HI_FAILURE; } while (isSend) { FD_ZERO(&read_fds); FD_SET(Vencfd, &read_fds); TimeoutVal.tv_sec = 20000; TimeoutVal.tv_usec = 0; s32Ret = select(Vencfd + 1, &read_fds, NULL, NULL, &TimeoutVal); if (s32Ret < 0) { perror("select failed!\n"); break; } else if (s32Ret == 0) { printf("get sendvenc stream time out,exit thread\n"); continue; } else { if (FD_ISSET(Vencfd, &read_fds)) { memset(&stStream, 0, sizeof(stStream)); s32Ret = HI_MPI_VENC_Query(SendVencChn, &stStat); if (HI_SUCCESS != s32Ret) { printf("HI_MPI_VENC_Query failed with err code %#x!\n", s32Ret); break; } stStream.pstPack = (VENC_PACK_S*)malloc(sizeof(VENC_PACK_S) * stStat.u32CurPacks); if (NULL == stStream.pstPack) { printf("malloc stream pack failed!\n"); break; } stStream.u32PackCount = stStat.u32CurPacks; s32Ret = HI_MPI_VENC_GetStream(SendVencChn, &stStream, HI_TRUE); if (HI_SUCCESS != s32Ret) { free(stStream.pstPack); stStream.pstPack = NULL; printf("HI_MPI_VENC_GetStream failed with %#x!\n", s32Ret); break; } for (i = 0; i < stStream.u32PackCount; i++) { //write(connfd, stStream.pstPack[i].pu8Addr[0], // stStream.pstPack[i].u32Len[0]); sendto(SendSockfd,stStream.pstPack[i].pu8Addr[0], stStream.pstPack[i].u32Len[0],0, (struct sockaddr*)&SendServaddr, sizeof(SendServaddr)); if (stStream.pstPack[i].u32Len[1] > 0) { //write(connfd, stStream.pstPack[i].pu8Addr[1], // stStream.pstPack[i].u32Len[1]); sendto(SendSockfd,stStream.pstPack[i].pu8Addr[1], stStream.pstPack[i].u32Len[1],0, (struct sockaddr*)&SendServaddr, sizeof(SendServaddr)); } } s32Ret = HI_MPI_VENC_ReleaseStream(SendVencChn, &stStream); if (HI_SUCCESS != s32Ret) { free(stStream.pstPack); stStream.pstPack = NULL; break; } free(stStream.pstPack); stStream.pstPack = NULL; } } } SAMPLE_COMM_VENC_StopGetStream(); SAMPLE_COMM_VENC_UnBindVpss(SendVencGrp, VpssGrp, VpssChn); SAMPLE_COMM_VENC_Stop(SendVencGrp, SendVencChn); usleep(100); } }
void *venc_get_stream(void *p) { struct timeval TimeoutVal; fd_set read_fds; HI_S32 s32VencFd[VENC_MAX_CHN_NUM]; int s32ChnTotal = 0; VENC_CHN_STAT_S stStat; VENC_STREAM_S stStream; // VENC_PACK_S *packet = NULL; HI_S32 s32Ret,i; VENC_CHN VencChn; int maxfd = 0; HI_CHAR snapPathFile[100] = "/tmp/"; VENC_GETSTREAM_PARA_S *pstPara; pstPara = (VENC_GETSTREAM_PARA_S *)p; s32ChnTotal = pstPara->s32Cnt; if (s32ChnTotal > VENC_MAX_CHN_NUM) { Printf("input count invaild\n"); return NULL ; } /* get Venc Fd. */ for(i = 0; i < s32ChnTotal; i++) { s32VencFd[i] = HI_MPI_VENC_GetFd(i); if (s32VencFd[i] < 0) { Printf("HI_MPI_VENC_GetFd failed with %#x!\n", s32VencFd[i]); return NULL; } if (maxfd <= s32VencFd[i]) { maxfd = s32VencFd[i]; } } /* Start to get streams of each channel.*/ while(HI_TRUE == pstPara->bThreadStart) { FD_ZERO(&read_fds); for (i = 0; i < s32ChnTotal; i++) { FD_SET(s32VencFd[i], &read_fds); } /*select return success, all packages in the complete frame*/ TimeoutVal.tv_sec = 1; TimeoutVal.tv_usec = 0; s32Ret = select(maxfd+1, &read_fds, NULL, NULL, &TimeoutVal); if (s32Ret < 0) { Printf("snap select failed!\n"); return NULL; } else if (0 == s32Ret) { continue; } else { for(i = 0; i < s32ChnTotal; i++) { VencChn = i; if (FD_ISSET(s32VencFd[VencChn], &read_fds)) { /*query how many packs in one-frame stream.*/ s32Ret = HI_MPI_VENC_Query(VencChn, &stStat); if (s32Ret != HI_SUCCESS) { Printf("HI_MPI_VENC_Query chn[%d] failed with %#x!\n", VencChn, s32Ret); return NULL; } /*malloc corresponding number of pack nodes*/ stStream.pstPack = (VENC_PACK_S*)malloc(sizeof(VENC_PACK_S) * stStat.u32CurPacks); if (NULL == stStream.pstPack) { Printf("malloc memory failed!\n"); return NULL; } /* call mpi to get one-frame stream*/ stStream.u32PackCount = stStat.u32CurPacks; s32Ret = HI_MPI_VENC_GetStream(VencChn, &stStream, HI_TRUE); if (HI_SUCCESS != s32Ret) { Printf("HI_MPI_VENC_GetStream failed with %#x!\n", s32Ret); free(stStream.pstPack); stStream.pstPack = NULL; return NULL; } /* save frame to file*/ s32Ret = venc_save_stream(VencChn, &stStream, snapPathFile); if (HI_SUCCESS != s32Ret) { Printf("venc_save_stream failed with %#x!\n", s32Ret); free(stStream.pstPack); stStream.pstPack = NULL; return NULL; } /* release stream*/ s32Ret = HI_MPI_VENC_ReleaseStream(VencChn, &stStream); if (s32Ret) { Printf("HI_MPI_VENC_ReleaseStream failed with %#x!\n", s32Ret); free(stStream.pstPack); stStream.pstPack = NULL; return NULL; } /*free pack nodes*/ free(stStream.pstPack); stStream.pstPack = NULL; } } } } return NULL; }