参考开源项目中 https://github.com/Yohoa/IoT_Camera/tree/master rtsp.c rtsp.h 源码文件。
rtsp调用步骤:
1. struct rtsp_server_context *g_rtsp_server = rtsp_start_server(RTP_TRANSPORT_UDP, port);
2. rtp_push_data(g_rtsp_server, spsPpsSt.sps_data, spsPpsSt.sps_len, 0);//sps data
3. rtp_push_data(g_rtsp_server, spsPpsSt.pps_data, spsPpsSt.pps_len, 0);//pps data
4. rtp_push_data(g_rtsp_server, g_stream_buf, dataLen, pts);//h264 venc data
5. rtsp_stop_server(g_rtsp_server);
线程若崩溃时注意调整RTSP_THREAD_STACK_SIZE大小。
以下代码解析SPS、PPS数据(以00 00 00 01作为起始标志)
SpsPpsInfo len=28
00 00 00 01 67 4d 00 33 96 54 03 c0 11 2f 2c dc 14 18 14 08 (SPS)
00 00 00 01 68 ee 3c 80 (PPS)
-----------------------------------------------
typedef struct __SpsPpsInfoSt{
unsigned char *vps_data;
int vps_len;
unsigned char *sps_data;
int sps_len;
unsigned char *pps_data;
int pps_len;
}SpsPpsInfoSt;
int DecodeSpsPpsInfo(VencHeaderData *pVencheader, SpsPpsInfoSt *pSpsPpsSt, int isH265){
unsigned char gap[] = {0, 0, 0, 1};
memset(pSpsPpsSt, 0, sizeof(SpsPpsInfoSt));
pSpsPpsSt->vps_data = pVencheader->pBuffer;
pSpsPpsSt->vps_len = 0;
if(isH265){
unsigned char *tmp = memmem(pSpsPpsSt->vps_data + 4, pVencheader->nLength - 4, gap, sizeof(gap));
if(tmp == NULL){
return -1;
}
pSpsPpsSt->vps_len = tmp - pSpsPpsSt->vps_data;
}
pSpsPpsSt->sps_data = pSpsPpsSt->vps_data + pSpsPpsSt->vps_len;
pSpsPpsSt->sps_len = 0;
{
unsigned char *tmp = memmem(pSpsPpsSt->sps_data + 4, pVencheader->nLength - 4 - pSpsPpsSt->vps_len, gap, sizeof(gap));
if(tmp == NULL){
return -2;
}
pSpsPpsSt->sps_len = tmp - pSpsPpsSt->sps_data;
}
pSpsPpsSt->pps_data = pSpsPpsSt->sps_data + pSpsPpsSt->sps_len;
pSpsPpsSt->pps_len = pVencheader->nLength - pSpsPpsSt->vps_len - pSpsPpsSt->sps_len;
{
int i = 0;
printf("vps data[%d]:", pSpsPpsSt->vps_len);
for(i = 0; i < pSpsPpsSt->vps_len; i++){
printf("%02x ", pSpsPpsSt->vps_data[i]);
}
printf("\n------------------\n");
printf("sps data[%d]:", pSpsPpsSt->sps_len);
for(i = 0; i < pSpsPpsSt->sps_len; i++){
printf("%02x ", pSpsPpsSt->sps_data[i]);
}
printf("\n------------------\n");
printf("pps data[%d]:", pSpsPpsSt->pps_len);
for(i = 0; i < pSpsPpsSt->pps_len; i++){
printf("%02x ", pSpsPpsSt->pps_data[i]);
}
printf("\n------------------\n");
}
return 0;
}