由于开发板内存太小,修改原来的h264转rtp包的机制,每个rtp包立即发送,不再进行缓存

master
a74589669 2019-09-20 22:49:21 +08:00
parent 036488b86e
commit 85e78c028c
12 changed files with 504 additions and 98 deletions

View File

@ -28,8 +28,10 @@ extern "C"{
#include <signal.h>
#include "sample_comm.h"
#include "librtsp.h"
#include "../venc/librtsp.h"
#include "../venc/rtp.h"
const HI_U8 g_SOI[2] = {0xFF, 0xD8};
const HI_U8 g_EOI[2] = {0xFF, 0xD9};
static pthread_t gs_VencPid;
@ -308,15 +310,62 @@ HI_S32 SAMPLE_COMM_VENC_SaveJpeg(FILE* fpJpegFile, VENC_STREAM_S *pstStream)
return HI_SUCCESS;
}
Vector *outBuf = NULL;
/******************************************************************************
* funciton : save H264 stream
******************************************************************************/
HI_S32 SAMPLE_COMM_VENC_SaveH264(FILE* fpH264File, VENC_STREAM_S *pstStream)
{
HI_S32 i;
int z = 0;
if(NULL == outBuf) {
printf("init rtp buffer!!!!!!!\r\n");
outBuf = (Vector *)malloc(sizeof(Vector*)*120);
}
for (i = 0; i < pstStream->u32PackCount; i++) {
if(pstStream->pstPack[i].DataType.enH264EType == H264E_NALU_PSLICE)
printf("pslice ");
if(pstStream->pstPack[i].DataType.enH264EType == H264E_NALU_PPS)
printf("pps ");
if(pstStream->pstPack[i].DataType.enH264EType == H264E_NALU_ISLICE)
printf("islice ");
if(pstStream->pstPack[i].DataType.enH264EType == H264E_NALU_SEI) {
printf("sei ");
// break; // ignore sei frame
}
if(pstStream->pstPack[i].DataType.enH264EType == H264E_NALU_SPS)
printf("sps ");
if(pstStream->pstPack[i].DataType.enH264EType == H264E_NALU_IPSLICE)
printf("islice ");
/*
fwrite(pstStream->pstPack[i].pu8Addr + pstStream->pstPack[i].u32Offset ,
pstStream->pstPack[i].u32Len - pstStream->pstPack[i].u32Offset, 1, fpH264File);*/
unsigned char *p = pstStream->pstPack[i].pu8Addr;
printf("h264 header file %d length %d nal count %d offset %x %x %x %x %x %x %x %x\r\n",
pstStream->pstPack[i].u32Len - pstStream->pstPack[i].u32Offset,
i,
pstStream->pstPack[i].u32Offset,
p[0],p[1],p[2],p[3],p[4],p[5],p[6],p[7]);
int packCnt = Pack264AndSend(
pstStream->pstPack[i].pu8Addr
,pstStream->pstPack[i].u32Len
, gRtspclient);
fflush(fpH264File);
}
return HI_SUCCESS;
}
/******************************************************************************
* funciton : save H264 stream
******************************************************************************/
HI_S32 SAMPLE_COMM_VENC_SaveH264With(FILE* fpH264File, VENC_STREAM_S *pstStream)
{
HI_S32 i;
for (i = 0; i < pstStream->u32PackCount; i++)
{
if(pstStream->pstPack[i].DataType.enH264EType == H264E_NALU_PSLICE)
@ -340,12 +389,11 @@ HI_S32 SAMPLE_COMM_VENC_SaveH264(FILE* fpH264File, VENC_STREAM_S *pstStream)
pstStream->pstPack[i].u32Offset,
p[0],p[1],p[2],p[3],p[4],p[5],p[6],p[7]);
fflush(fpH264File);
}
return HI_SUCCESS;
}
return HI_SUCCESS;
}
/******************************************************************************
* funciton : save H265 stream
******************************************************************************/
@ -438,7 +486,8 @@ HI_S32 SAMPLE_COMM_VENC_SaveStream(PAYLOAD_TYPE_E enType,FILE *pFd, VENC_STREAM_
* funciton : Start venc stream mode (h264, mjpeg)
* note : rate control parameter need adjust, according your case.
******************************************************************************/
HI_S32 SAMPLE_COMM_VENC_Start(VENC_CHN VencChn, PAYLOAD_TYPE_E enType, VIDEO_NORM_E enNorm, PIC_SIZE_E enSize, SAMPLE_RC_E enRcMode,HI_U32 u32Profile)
HI_S32 SAMPLE_COMM_VENC_Start(VENC_CHN VencChn, PAYLOAD_TYPE_E enType,
VIDEO_NORM_E enNorm, PIC_SIZE_E enSize, SAMPLE_RC_E enRcMode,HI_U32 u32Profile)
{
HI_S32 s32Ret;
VENC_CHN_ATTR_S stVencChnAttr;
@ -484,10 +533,10 @@ HI_S32 SAMPLE_COMM_VENC_Start(VENC_CHN VencChn, PAYLOAD_TYPE_E enType, VIDEO_NOR
if(SAMPLE_RC_CBR == enRcMode)
{
stVencChnAttr.stRcAttr.enRcMode = VENC_RC_MODE_H264CBR;
stH264Cbr.u32Gop = (VIDEO_ENCODING_MODE_PAL== enNorm)?24:30;
stH264Cbr.u32Gop = (VIDEO_ENCODING_MODE_PAL== enNorm)?25:25;
stH264Cbr.u32StatTime = 1; /* stream rate statics time(s) */
stH264Cbr.u32SrcFrmRate = (VIDEO_ENCODING_MODE_PAL== enNorm)?25:30;/* input (vi) frame rate */
stH264Cbr.fr32DstFrmRate = (VIDEO_ENCODING_MODE_PAL== enNorm)?25:30;/* target frame rate */
stH264Cbr.u32SrcFrmRate = (VIDEO_ENCODING_MODE_PAL== enNorm)?25:25;/* input (vi) frame rate */
stH264Cbr.fr32DstFrmRate = (VIDEO_ENCODING_MODE_PAL== enNorm)?25:25;/* target frame rate */
switch (enSize)
{
@ -1134,7 +1183,8 @@ HI_VOID* SAMPLE_COMM_VENC_GetVencStreamProc(HI_VOID *p)
{
SAMPLE_PRT("HI_MPI_VENC_GetChnAttr chn[%d] failed with %#x!\n", \
VencChn, s32Ret);
return NULL;
break;
// return NULL;
}
enPayLoadType[i] = stVencChnAttr.stVeAttr.enType;
@ -1249,20 +1299,24 @@ HI_VOID* SAMPLE_COMM_VENC_GetVencStreamProc(HI_VOID *p)
/*******************************************************
step 2.5 : save frame to file
*******************************************************/
s32Ret = SAMPLE_COMM_VENC_SaveStream(enPayLoadType[i], pFile[i], &stStream);
if (HI_SUCCESS != s32Ret)
{
free(stStream.pstPack);
stStream.pstPack = NULL;
SAMPLE_PRT("save stream failed!\n");
break;
}
if( i == 1){
s32Ret = SAMPLE_COMM_VENC_SaveStream(enPayLoadType[i], pFile[i], &stStream);
if (HI_SUCCESS != s32Ret)
{
printf("SAMPLE_COMM_VENC_SaveStream error\r\n");
free(stStream.pstPack);
stStream.pstPack = NULL;
SAMPLE_PRT("save stream failed!\n");
break;
}
}
/*******************************************************
step 2.6 : release stream
*******************************************************/
s32Ret = HI_MPI_VENC_ReleaseStream(i, &stStream);
if (HI_SUCCESS != s32Ret)
{
printf("HI_MPI_VENC_ReleaseStream error\r\n");
free(stStream.pstPack);
stStream.pstPack = NULL;
break;
@ -1270,8 +1324,10 @@ HI_VOID* SAMPLE_COMM_VENC_GetVencStreamProc(HI_VOID *p)
/*******************************************************
step 2.7 : free pack nodes
*******************************************************/
free(stStream.pstPack);
stStream.pstPack = NULL;
if(stStream.pstPack != NULL){
free(stStream.pstPack);
stStream.pstPack = NULL;
}
}
}
}

View File

@ -0,0 +1,3 @@
librtsp.o: librtsp.c librtsp.h sock.h
rtp.o: rtp.c rtp.h librtsp.h sock.h
sock.o: sock.c sock.h

View File

@ -1,30 +1,33 @@
# Hisilicon Hi3516 sample Makefile
include ../Makefile.param
$(warning CPP $(CPP))
$(warning CC $(CC))
#ifeq ($(SAMPLE_PARAM_FILE), )
# SAMPLE_PARAM_FILE:=../Makefile.param
# include $(SAMPLE_PARAM_FILE)
#endif
# target source
SRC := $(wildcard *.c)
SRC := $(wildcard *.c)
OBJ := $(SRC:%.c=%.o)
TARGET := $(OBJ:%.o=%)
TARGET := sample_venc
.PHONY : clean all
#$(warning target object is $(TARGET))
#$(warning comm object is $(arm-hisiv300-linux-gcc -MM sample_venc.c))
all: $(TARGET)
$(TARGET):%:%.o $(COMM_OBJ)
$(CC) $(CFLAGS) -lpthread -lm -o $@ $^ $(MPI_LIBS) $(AUDIO_LIBA) $(SENSOR_LIBS)
#all: $(OBJ) $(TARGET)
#$(OBJ): %.o: %.c $(COMM_OBJ)
# $(CC) -MM -c $(CFLAGS) $< -o $@ $(MPI_LIBS) $(AUDIO_LIBA) $(SENSOR_LIBS)
clean:
main: $(OBJ) $(MPI_LIBS) $(AUDIO_LIBA) $(SENSOR_LIBS) $(COMM_OBJ)
$(CC) -o main $(CFLAGS) -lpthread -lm -g -rdynamic $(OBJ) $(COMM_OBJ) $(MPI_LIBS) $(AUDIO_LIBA) $(SENSOR_LIBS)
.depend: $(SRC)
@$(CC) -MM $(SRC) > $@
sinclude .depend
TARGET:
arm-hisiv300-linux-ld $(CFLAGS) -lpthread -lm -o ${TARGET} $@ $^ $(MPI_LIBS) $(AUDIO_LIBA) $(SENSOR_LIBS) $(OBJ) $(COMM_OBJ) $(OBJ)
clean:
@rm -f $(TARGET)
@rm -f $(OBJ)
@rm -f $(COMM_OBJ)
cleanstream:
@rm -f *.h264
@rm -f *.jpg

View File

@ -1,5 +1,8 @@
#include "librtsp.h"
RtspClient *gRtspclient;
RtspClient *ConectRtsp(char *ip,int port,char *desc){
if(0 == ip || 0 == desc){
return 0;
@ -24,7 +27,7 @@ int SendOption(RtspClient*p,CallBack *c){
int ret = Send(p->sock,buf,strlen(buf));
printf("send %d \r\ndata is:\r\n%s\r\n",ret,buf);
if(0 > ret){
closesocket(p->sock);
CloseConn(p->sock);
p->conn = FALSE;
return -1;
}
@ -89,7 +92,7 @@ int SendAnnounce(RtspClient *p,CallBack callback){
int ret = Send(p->sock,buf,strlen(buf));
printf("send %d \r\ndata is:\r\n%s\r\n",ret,buf);
if(0 > ret){
closesocket(p->sock);
CloseConn(p->sock);
p->conn = FALSE;
return -1;
}
@ -109,7 +112,7 @@ int SendSetup(RtspClient *p,CallBack *c){
int ret = Send(p->sock,buf,strlen(buf));
printf("send %d \r\ndata is:\r\n%s\r\n",ret,buf);
if(0 > ret){
closesocket(p->sock);
CloseConn(p->sock);
p->conn = FALSE;
return -1;
}
@ -140,10 +143,10 @@ int SendRecord(RtspClient *p,CallBack *c){
int ret = Send(p->sock,buf,strlen(buf));
printf("send %d \r\ndata is:\r\n%s\r\n",ret,buf);
if(0 > ret){
closesocket(p->sock);
CloseConn(p->sock);
p->conn = FALSE;
return -1;
}
}
if(Recv(p->sock,pRecv,500) > 0) {
printf("recv: %s\r\n",pRecv);
} else{
@ -152,17 +155,17 @@ int SendRecord(RtspClient *p,CallBack *c){
}
}
int SendRtpPackage(RtspClient *p,unsigned char *buf,int length){
int SendRtpPackage(RtspClient *p,unsigned char *buf,int length) {
int ret = Send(p->sock,(char*)buf,length);
printf("request send %d ,send rtp pack %d \r\ndata is:\r\n%s\r\n",length,ret,buf);
printf("request send %d ,send rtp pack %d \r\n",length,ret,buf);
if(0 > ret){
closesocket(p->sock);
CloseConn(p->sock);
p->conn = FALSE;
return -1;
}
}
int CallBackSetup(RtspClient *client, const char *data, int len){
int CallBackSetup(RtspClient *client, const char *data, int len) {
char *session = strstr(data,"Session: ");
if(NULL != session){
sscanf(session,"Session: %s\r\n",client->session);

View File

@ -43,6 +43,8 @@ typedef enum _E_Method {
E_RECORD
} Methods;
extern RtspClient *gRtspclient;
RtspClient *ConectRtsp(char *ip, int port, char *desc);
int GenPayload(Methods method, char *data);

BIN
device/mpp/sample/venc/main Normal file

Binary file not shown.

View File

@ -0,0 +1,244 @@
//
// Created by 29019 on 2019/5/4.
//
#include "rtp.h"
#include "librtsp.h"
int Pack264(unsigned char *frame,int size, Vector **outbuf){
int i = 0;
static short seq = 0;
if((0 == frame) || (0 == outbuf)){
return 0;
}
// fill rtsp interleaved header
RtspInterleavedFrameHeader rtspInterleavedFrameHeader;
rtspInterleavedFrameHeader.magic = 0x24;
rtspInterleavedFrameHeader.channel = 0;
//fill rtp header
CRtpHeader header;
header.version = 2;
header.cc = 0;
header.m = 0;
header.p = 0;
header.pt = 96;
header.ts = htonl(1201233060);
header.x = 0;
header.ssrc = htonl(0x055aaee0);
if((frame[0] != 0x00)&&(frame[1] != 0x00)&&(frame[2] != 0x00) && (frame[3] != 0x01)){
return -1;
}
int packCnt = (size - 5) /MAX_RTP_LEN;
printf("%d package pack\r\n",packCnt);
if(packCnt == 0){
header.seq = htons(seq++);
outbuf[0]->data = (unsigned char *)malloc(sizeof(FuIdentifier) + sizeof(CRtpHeader) + sizeof(rtspInterleavedFrameHeader)
+ size - 5);
outbuf[0]->length = size - 5 + sizeof(FuIdentifier) + sizeof(CRtpHeader) + sizeof(rtspInterleavedFrameHeader);
FuIdentifier identifier;
identifier.FUType = frame[4]&0x1f; //sps or pps
identifier.NRI = (frame[4]&0xe0)>>5;
identifier.F = 0;
rtspInterleavedFrameHeader.length = htons(size - 5 + sizeof(FuIdentifier) + sizeof(CRtpHeader) );
int index = 0;
memcpy(outbuf[0]->data,&rtspInterleavedFrameHeader,sizeof(rtspInterleavedFrameHeader));
index += sizeof(rtspInterleavedFrameHeader);
memcpy(outbuf[0]->data + index,&header,sizeof(header));
index += sizeof(header);
memcpy(outbuf[0]->data + index,&identifier,sizeof(FuIdentifier));
index += sizeof(identifier);
memcpy(outbuf[0]->data + index,frame + 5,size - 5);
return 1; // return one package
}else{
if(packCnt % MAX_RTP_LEN != 0){
packCnt +=1;
}
CH264NalUnit nalUnit;
nalUnit.Identifier.FUType = 28; // fu-a
nalUnit.Identifier.NRI = (frame[4]&0xe0)>>5;
nalUnit.Identifier.F = 0;
nalUnit.Header.NALUnitType = (frame[4]&0x1f); // idr picture or non-idr picture
nalUnit.Header.Forbid = 0;
for(i = 0;i < packCnt;i++) {
header.seq = htons(seq++);
if(i == packCnt - 1) {
if(packCnt % MAX_RTP_LEN == 0){
(*outbuf + i)->data = (unsigned char *)malloc(sizeof(CH264NalUnit) + sizeof(CRtpHeader) + sizeof(rtspInterleavedFrameHeader)
+ MAX_RTP_LEN);
(*outbuf + i)->length = MAX_RTP_LEN + sizeof(CH264NalUnit) + sizeof(CRtpHeader) + sizeof(rtspInterleavedFrameHeader);
rtspInterleavedFrameHeader.length = htons(MAX_RTP_LEN + sizeof(CH264NalUnit) + sizeof(CRtpHeader));
}else{
(*outbuf + i)->data = (unsigned char *)malloc(sizeof(CH264NalUnit) + sizeof(CRtpHeader) + sizeof(rtspInterleavedFrameHeader)
+ (size - 5)%MAX_RTP_LEN);
(*outbuf + i)->length = (size - 5)%MAX_RTP_LEN + sizeof(CH264NalUnit) + sizeof(CRtpHeader) + sizeof(rtspInterleavedFrameHeader);
rtspInterleavedFrameHeader.length = htons((size - 5)%MAX_RTP_LEN + sizeof(CH264NalUnit) + sizeof(CRtpHeader));
}
nalUnit.Header.End = 1;
nalUnit.Header.Start = 0;
int index = 0;
memcpy((*outbuf + i)->data,&rtspInterleavedFrameHeader,sizeof(rtspInterleavedFrameHeader));
index += sizeof(rtspInterleavedFrameHeader);
memcpy((*outbuf + i)->data + index,&header,sizeof(header));
index += sizeof(header);
memcpy((*outbuf + i)->data + index,&nalUnit,sizeof(nalUnit));
index += sizeof(nalUnit);
memcpy((*outbuf + i)->data + index,frame + 5 + i*MAX_RTP_LEN,(size - 5)%MAX_RTP_LEN);
}else if(i == 0){
nalUnit.Header.End = 0;
nalUnit.Header.Start = 1;
(*outbuf + i)->data = (unsigned char *)malloc(sizeof(FuIdentifier) + sizeof(CRtpHeader) + sizeof(rtspInterleavedFrameHeader)
+ MAX_RTP_LEN);
(*outbuf + i)->length = MAX_RTP_LEN + sizeof(CH264NalUnit) + sizeof(CRtpHeader) + sizeof(rtspInterleavedFrameHeader);
rtspInterleavedFrameHeader.length = htons(MAX_RTP_LEN + sizeof(CH264NalUnit) + sizeof(CRtpHeader));
int index = 0;
memcpy((*outbuf + i)->data,&rtspInterleavedFrameHeader,sizeof(rtspInterleavedFrameHeader));
index += sizeof(rtspInterleavedFrameHeader);
memcpy((*outbuf + i)->data + index,&header,sizeof(header));
index += sizeof(header);
memcpy((*outbuf + i)->data + index,&nalUnit,sizeof(nalUnit));
index += sizeof(nalUnit);
memcpy((*outbuf + i)->data + index,frame + 5 + i*MAX_RTP_LEN,MAX_RTP_LEN);
}else{
(*outbuf + i)->data = (unsigned char *)malloc(sizeof(FuIdentifier) + sizeof(CRtpHeader) + sizeof(rtspInterleavedFrameHeader)
+ MAX_RTP_LEN);
(*outbuf + i)->length = MAX_RTP_LEN + sizeof(CH264NalUnit) + sizeof(CRtpHeader) + sizeof(rtspInterleavedFrameHeader);
nalUnit.Header.End = 0;
nalUnit.Header.Start = 0;
rtspInterleavedFrameHeader.length = htons(MAX_RTP_LEN + sizeof(CH264NalUnit) + sizeof(CRtpHeader));
int index = 0;
memcpy((*outbuf + i)->data,&rtspInterleavedFrameHeader,sizeof(rtspInterleavedFrameHeader));
index += sizeof(rtspInterleavedFrameHeader);
memcpy((*outbuf + i)->data + index,&header,sizeof(header));
index += sizeof(header);
memcpy((*outbuf + i)->data + index,&nalUnit,sizeof(nalUnit));
index += sizeof(nalUnit);
memcpy((*outbuf + i)->data + index,frame + 5 + i*MAX_RTP_LEN,MAX_RTP_LEN);
}
}
return packCnt;
}
}
int Pack264AndSend(unsigned char *frame,int size,RtspClient * p){
int i = 0;
static short seq = 0;
char *sendBuf;
int sendLength = 0;
int length = MAX_RTP_LEN + sizeof(CH264NalUnit) + sizeof(CRtpHeader)
+ sizeof(RtspInterleavedFrameHeader);
if(0 == frame || NULL == p) {
return 0;
}
sendBuf = malloc(MAX_RTP_LEN + sizeof(CH264NalUnit) + sizeof(CRtpHeader)
+ sizeof(RtspInterleavedFrameHeader));
// fill rtsp interleaved header
RtspInterleavedFrameHeader rtspInterleavedFrameHeader;
rtspInterleavedFrameHeader.magic = 0x24;
rtspInterleavedFrameHeader.channel = 0;
//fill rtp header
CRtpHeader header;
header.version = 2;
header.cc = 0;
header.m = 0;
header.p = 0;
header.pt = 96;
header.ts = htonl(1201233060);
header.x = 0;
header.ssrc = htonl(0x055aaee0);
if((frame[0] != 0x00)&&(frame[1] != 0x00)&&(frame[2] != 0x00) && (frame[3] != 0x01)){
return -1;
}
int packCnt = (size - 5) /MAX_RTP_LEN;
printf("%d package pack\r\n",packCnt);
if(packCnt == 0) {
header.seq = htons(seq++);
sendLength = size - 5 + sizeof(FuIdentifier) + sizeof(CRtpHeader) + sizeof(rtspInterleavedFrameHeader);
FuIdentifier identifier;
identifier.FUType = frame[4]&0x1f; //sps or pps
identifier.NRI = (frame[4]&0xe0)>>5;
identifier.F = 0;
int index = 0;
memcpy(sendBuf,&rtspInterleavedFrameHeader,sizeof(rtspInterleavedFrameHeader));
index += sizeof(rtspInterleavedFrameHeader);
memcpy(sendBuf + index,&header,sizeof(header));
index += sizeof(header);
memcpy(sendBuf + index,&identifier,sizeof(FuIdentifier));
index += sizeof(identifier);
memcpy(sendBuf + index,frame + 5,size - 5);
SendRtpPackage(p,sendBuf,sendLength);
return 1; // return one package
}else{
if(packCnt % MAX_RTP_LEN != 0){
packCnt +=1;
}
CH264NalUnit nalUnit;
nalUnit.Identifier.FUType = 28; // fu-a
nalUnit.Identifier.NRI = (frame[4]&0xe0)>>5;
nalUnit.Identifier.F = 0;
nalUnit.Header.NALUnitType = (frame[4]&0x1f); // idr picture or non-idr picture
nalUnit.Header.Forbid = 0;
for(i = 0;i < packCnt;i++) {
header.seq = htons(seq++);
if(i == packCnt - 1) {
if(packCnt % MAX_RTP_LEN == 0){
sendLength = MAX_RTP_LEN + sizeof(CH264NalUnit) + sizeof(CRtpHeader) + sizeof(RtspInterleavedFrameHeader);
rtspInterleavedFrameHeader.length = htons(MAX_RTP_LEN + sizeof(CH264NalUnit) + sizeof(CRtpHeader));
}else{
sendLength = (size - 5)%MAX_RTP_LEN + sizeof(CH264NalUnit) + sizeof(CRtpHeader) + sizeof(RtspInterleavedFrameHeader);
rtspInterleavedFrameHeader.length = htons((size - 5)%MAX_RTP_LEN + sizeof(CH264NalUnit) + sizeof(CRtpHeader));
}
nalUnit.Header.End = 1;
nalUnit.Header.Start = 0;
int index = 0;
memcpy(sendBuf,&rtspInterleavedFrameHeader,sizeof(rtspInterleavedFrameHeader));
index += sizeof(rtspInterleavedFrameHeader);
memcpy(sendBuf + index,&header,sizeof(header));
index += sizeof(header);
memcpy(sendBuf + index,&nalUnit,sizeof(nalUnit));
index += sizeof(nalUnit);
memcpy(sendBuf + index,frame + 5 + i*MAX_RTP_LEN,(size - 5)%MAX_RTP_LEN);
SendRtpPackage(p,sendBuf,sendLength);
}else if(i == 0) {
nalUnit.Header.End = 0;
nalUnit.Header.Start = 1;
sendLength = MAX_RTP_LEN + sizeof(CH264NalUnit) +
sizeof(CRtpHeader) + sizeof(rtspInterleavedFrameHeader);
rtspInterleavedFrameHeader.length = htons(MAX_RTP_LEN + sizeof(CH264NalUnit) + sizeof(CRtpHeader));
int index = 0;
memcpy(sendBuf,&rtspInterleavedFrameHeader,sizeof(rtspInterleavedFrameHeader));
index += sizeof(rtspInterleavedFrameHeader);
memcpy(sendBuf + index,&header,sizeof(header));
index += sizeof(header);
memcpy(sendBuf + index,&nalUnit,sizeof(nalUnit));
index += sizeof(nalUnit);
memcpy(sendBuf + index,frame + 5 + i*MAX_RTP_LEN,MAX_RTP_LEN);
SendRtpPackage(p,sendBuf,sendLength);
}else{
sendLength = MAX_RTP_LEN + sizeof(CH264NalUnit) + sizeof(CRtpHeader) + sizeof(rtspInterleavedFrameHeader);
nalUnit.Header.End = 0;
nalUnit.Header.Start = 0;
rtspInterleavedFrameHeader.length = htons(MAX_RTP_LEN + sizeof(CH264NalUnit) + sizeof(CRtpHeader));
int index = 0;
memcpy(sendBuf,&rtspInterleavedFrameHeader,sizeof(rtspInterleavedFrameHeader));
index += sizeof(rtspInterleavedFrameHeader);
memcpy(sendBuf + index,&header,sizeof(header));
index += sizeof(header);
memcpy(sendBuf + index,&nalUnit,sizeof(nalUnit));
index += sizeof(nalUnit);
memcpy(sendBuf + index,frame + 5 + i*MAX_RTP_LEN,MAX_RTP_LEN);
SendRtpPackage(p,sendBuf,sendLength);
}
}
free(sendBuf);
return packCnt;
}
}

View File

@ -6,8 +6,14 @@
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
#include "thirdparty/librtsp_c/librtsp.h"
#include "rtsp.h"
#include "librtsp.h"
typedef struct {
unsigned char magic;
unsigned char channel;
unsigned short length;
} RtspInterleavedFrameHeader;
typedef struct {
unsigned char cc:4; /* CSRC count */
unsigned char x:1; /* header extension flag */
@ -43,5 +49,8 @@ typedef struct{
int length;
}Vector;
int Pack264(unsigned char *frame,int, Vector **outbuf);
#endif
int Pack264(unsigned char *frame,int, Vector **outbuf);
// for very low memory device like hi3518
int Pack264AndSend(unsigned char *frame,int size,RtspClient * p);
#endif

View File

@ -17,8 +17,9 @@ extern "C"{
#include <unistd.h>
#include <pthread.h>
#include <signal.h>
#include "sample_comm.h"
#include "sample_comm.h"
#include "librtsp.h"
VIDEO_NORM_E gs_enNorm = VIDEO_ENCODING_MODE_NTSC;
/******************************************************************************
@ -71,8 +72,7 @@ void SAMPLE_VENC_StreamHandleSig(HI_S32 signo)
******************************************************************************/
HI_S32 SAMPLE_VENC_1080P_CLASSIC(HI_VOID)
{
HI_S32 SAMPLE_VENC_1080P_CLASSIC(HI_VOID) {
PAYLOAD_TYPE_E enPayLoad[3]= {PT_H264, PT_H264,PT_H264};
PIC_SIZE_E enSize[3] = {PIC_HD1080, PIC_VGA,PIC_QVGA};
HI_U32 u32Profile = 0;
@ -131,7 +131,7 @@ HI_S32 SAMPLE_VENC_1080P_CLASSIC(HI_VOID)
enSize[0], SAMPLE_PIXEL_FORMAT, SAMPLE_SYS_ALIGN_WIDTH);
stVbConf.astCommPool[0].u32BlkSize = u32BlkSize;
stVbConf.astCommPool[0].u32BlkCnt = 4;
u32BlkSize = SAMPLE_COMM_SYS_CalcPicVbBlkSize(gs_enNorm,\
enSize[1], SAMPLE_PIXEL_FORMAT, SAMPLE_SYS_ALIGN_WIDTH);
stVbConf.astCommPool[1].u32BlkSize = u32BlkSize;
@ -143,7 +143,7 @@ HI_S32 SAMPLE_VENC_1080P_CLASSIC(HI_VOID)
stVbConf.astCommPool[2].u32BlkSize = u32BlkSize;
stVbConf.astCommPool[2].u32BlkCnt = 3;
}
/******************************************
step 2: mpp system init.
******************************************/
@ -227,18 +227,20 @@ HI_S32 SAMPLE_VENC_1080P_CLASSIC(HI_VOID)
SAMPLE_PRT("SAMPLE_COMM_SYS_GetPicSize failed!\n");
goto END_VENC_1080P_CLASSIC_4;
}
if(s32ChnNum >= 2)
{
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;
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);
s32Ret = SAMPLE_COMM_VPSS_EnableChn(VpssGrp, VpssChn,
&stVpssChnAttr, &stVpssChnMode, HI_NULL);
if (HI_SUCCESS != s32Ret)
{
SAMPLE_PRT("Enable vpss chn failed!\n");
@ -272,32 +274,10 @@ HI_S32 SAMPLE_VENC_1080P_CLASSIC(HI_VOID)
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;
}
enRcMode = SAMPLE_RC_CBR;
/*** enSize[0] **/
/** enSize[0] */
if(s32ChnNum >= 1)
{
VpssGrp = 0;
@ -339,7 +319,7 @@ HI_S32 SAMPLE_VENC_1080P_CLASSIC(HI_VOID)
goto END_VENC_1080P_CLASSIC_5;
}
}
/*** enSize[2] **/
/*** enSize[2] */
if(s32ChnNum >= 3)
{
VpssChn = 2;
@ -1510,8 +1490,9 @@ END_VENC_1080P_CLASSIC_0: //system exit
}
void HandleSigPipe(HI_S32 signo){
fprintf(stderr,"signal error %d\r\n",signo);
}
/******************************************************************************
* function : main()
* Description : video venc sample
@ -1525,7 +1506,16 @@ int main(int argc, char *argv[]) {
}
signal(SIGINT, SAMPLE_VENC_HandleSig);
signal(SIGTERM, SAMPLE_VENC_HandleSig);
signal(SIGSEGV, HandleSigPipe);
gRtspclient = ConectRtsp("192.168.2.100", 554, "aa");
if (0 == gRtspclient) {
printf("error connect server\r\n");
return -1;
}
SendAnnounce(gRtspclient, 0);
SendSetup(gRtspclient, &CallBackSetup);
SendRecord(gRtspclient, 0);
switch (*argv[1])
{
case '0':/* H.264@1080p@30fps+H.265@1080p@30fps+H.264@D1@30fps */

View File

@ -1,6 +1,6 @@
#include "sock.h"
#pragma comment(lib, "ws2_32.lib")
#ifdef WIN32
#pragma comment(lib, "ws2_32.lib")
Sock Connect(char *ip,int port){
if(0 == ip){
return -1;
@ -29,7 +29,7 @@ Sock Connect(char *ip,int port){
}
int Send(Sock s,char*dat, int len){
fd_set wFds;
fd_set wFds;
FD_ZERO(&wFds);
FD_SET(s,&wFds);
@ -79,4 +79,92 @@ int Recv(Sock s,char *rec,int len){
}
return -1;
}
#endif
#ifdef linux
Sock Connect(char *ip,int port){
if(0 == ip){
return -1;
}
Sock ret = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);//create socket
if(ret < 0){
printf("error malloc socket%d\r\n",ret);
exit(0);
}
struct sockaddr_in addrSrv;
addrSrv.sin_addr.s_addr = inet_addr(ip);//"49.152.49.84");
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(port);
if(0 > connect(ret,(struct sockaddr*)&addrSrv,sizeof(struct sockaddr))){
close(ret);
printf("connect socket error code %ld\r\n",errno);
return -1;
}
return ret;
}
int Recv(Sock s,char *rec,int len){
fd_set rFds;
fd_set eFds;
FD_ZERO(&rFds);
FD_SET(s,&rFds);
int ret = select(s + 1,&rFds,0,0,0);
// time expire
if(ret == 0){
return 0;
}else if(ret > 0){
if(FD_ISSET(s,&rFds)){
int recvDat = recv(s,rec,len,0);
if(recvDat <0)
{
close(s);
return -1;
}
return recvDat;
}
if(FD_ISSET(s,&eFds)){
close(s);
return -3;
}
}
return -1;
}
int CloseConn(Sock s){
return close(s);
}
int Send(Sock s,char*dat, int len){
fd_set wFds;
FD_ZERO(&wFds);
FD_SET(s,&wFds);
int ret = select(s + 1,0,&wFds,0,0);
// time expire
if(ret == 0){
return 0;
}else if(ret > 0){
if(FD_ISSET(s,&wFds)){
int recv = send(s,dat,len,0);
if(recv < 0)
{
CloseConn(s);
return -2;
}
return recv;
}
}else{
printf("error code %d\r\n",ret);
return -1;
}
}
#endif

View File

@ -3,6 +3,13 @@
#include "stdio.h"
#ifdef linux
#include <sys/socket.h>
#include <sys/uio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <errno.h>
typedef int Sock;
#endif
@ -14,6 +21,7 @@ typedef SOCKET Sock;
Sock Connect(char *ip,int port);
int Send(Sock s,char*dat, int len);
int Recv(Sock s,char *rec,int len);
int CloseConn(Sock s);
/*
ANNOUNCE rtsp://118.24.238.198:554/Sword RTSP/1.0
CSeq: 1
@ -61,4 +69,4 @@ a=control:streamid=0
*/
#endif
#endif

View File

@ -1,3 +1,3 @@
---- SAMPLE_COMM_VENC_SnapProcess Matches (2 in 2 files) ----
Sample_comm.h (\\192.168.2.102\phoenix\mpp\sample\common):HI_S32 SAMPLE_COMM_VENC_SnapProcess(VENC_CHN VencChn);
Sample_venc.c: s32Ret = SAMPLE_COMM_VENC_SnapProcess(VencChn);
---- HI_MPI_VPSS_GetDepth Matches (2 in 1 files) ----
Sample_venc.c: HI_MPI_VPSS_GetDepth(0,1,&depth0);
Sample_venc.c: HI_MPI_VPSS_GetDepth(0,0,&depth1);