diff --git a/device/rtp.cpp b/device/rtp.cpp new file mode 100644 index 0000000..e322f22 --- /dev/null +++ b/device/rtp.cpp @@ -0,0 +1,61 @@ +// +// Created by 29019 on 2019/5/4. +// + +#include "rtp.h" + +int Pack264(unsigned char *frame,int size, unsigned char *outbuf[]){ + if((nullptr == frame) || (nullptr == outbuf)){ + return 0; + } + if((frame[0] != 0x00)&&(frame[1] != 0x00)&&(frame[2] != 0x00) && (frame[3] != 0x01)){ + return -1; + } + int packCnt = (size - 4) /MAX_RTP_LEN; + if(packCnt % MAX_RTP_LEN != 0){ + packCnt +=1; + } + outbuf = (unsigned char **)malloc(sizeof(unsigned char *)*packCnt); + RtspInterleavedFrameHeader rtspInterleavedFrameHeader; + rtspInterleavedFrameHeader.magic = 0x24; + rtspInterleavedFrameHeader.channel = 0; + + CRtpHeader header; + header.version = 2; + header.cc = 0; + header.m = 0; + header.p = 0; + header.pt = 96; + header.seq = 1; + header.ts = 0x1234; + header.x = 0; + header.ssrc = 0x1235; + + CH264NalUnit nalUnit; + nalUnit.Identifier.FUType = 28; + nalUnit.Identifier.NRI = 2; + nalUnit.Identifier.F = 0; + nalUnit.Header.NALUnitType = 1; // idr picture + nalUnit.Header.Forbid = 0; + + for(int i = 0;i < packCnt;i++) { + if(i == packCnt - 1){ + nalUnit.Header.End = 1; + nalUnit.Header.Start = 0; + rtspInterleavedFrameHeader.length = MAX_RTP_LEN + sizeof(CH264NalUnit) + sizeof(CRtpHeader); + + }else if(i == 0){ + char *rtpBuf = (char *)malloc(MAX_RTP_LEN + sizeof(CH264NalUnit) + sizeof(CRtpHeader) + + sizeof(RtspInterleavedFrameHeader)); + rtspInterleavedFrameHeader.length = MAX_RTP_LEN + sizeof(CH264NalUnit) + sizeof(CRtpHeader); + nalUnit.Header.End = 0; + nalUnit.Header.Start = 1; + }else{ + nalUnit.Header.End = 0; + nalUnit.Header.Start = 0; + rtspInterleavedFrameHeader.length = MAX_RTP_LEN + sizeof(CH264NalUnit) + sizeof(CRtpHeader); + + } + } + return packCnt; +} \ No newline at end of file diff --git a/device/rtp.h b/device/rtp.h new file mode 100644 index 0000000..41affbe --- /dev/null +++ b/device/rtp.h @@ -0,0 +1,42 @@ +#ifndef __RTP_PACK__ +#define __RTP_PACK__ + +#pragma pack(1)//设定为1字节对齐 + +#include +#include +#include +#include "thirdparty/librtsp_c/librtsp.h" +#include "rtsp.h" +typedef struct { + unsigned char cc:4; /* CSRC count */ + unsigned char x:1; /* header extension flag */ + unsigned char p:1; /* padding flag */ + unsigned char version:2; /* protocol version */ + unsigned char pt:7; /* payload type */ + unsigned char m:1; /* marker bit */ + unsigned short seq; /* sequence number */ + unsigned int ts; /* timestamp */ + unsigned int ssrc; /* synchronization source */ +} CRtpHeader; +typedef struct { + unsigned char FUType:5; + unsigned char NRI:2; + unsigned char F:1; +}FuIdentifier; +typedef struct { + unsigned char NALUnitType:5; + unsigned char Forbid:1; + unsigned char End:1; + unsigned char Start:1; +}FuHeader; +typedef struct { + FuIdentifier Identifier; + FuHeader Header; +} CH264NalUnit; + +#pragma pack(4)//设定为4字节对齐 +#define MAX_RTP_LEN 1001 + +int Pack264(unsigned char *frame,int size, unsigned char *outbuf[]); +#endif \ No newline at end of file