添加c语言rtp推流的实现

master
a74589669 2019-09-14 02:31:51 +08:00
parent 23905e9636
commit a69d08b656
2 changed files with 103 additions and 0 deletions

61
device/rtp.cpp Normal file
View File

@ -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;
}

42
device/rtp.h Normal file
View File

@ -0,0 +1,42 @@
#ifndef __RTP_PACK__
#define __RTP_PACK__
#pragma pack(1)//设定为1字节对齐
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
#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