From a69d08b65673f40d5987fc52832c73f9e6851b9f Mon Sep 17 00:00:00 2001 From: a74589669 <290198252@qq.com> Date: Sat, 14 Sep 2019 02:31:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0c=E8=AF=AD=E8=A8=80rtp?= =?UTF-8?q?=E6=8E=A8=E6=B5=81=E7=9A=84=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- device/rtp.cpp | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ device/rtp.h | 42 ++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 device/rtp.cpp create mode 100644 device/rtp.h 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