ONVIF网络摄像头(IPC)客户端开发—RTSP RTCP RTP加载AAC音频流
前言:
RTSP,RTCP,RTP一般是一起使用,在FFmpeg和live555这些库中,它们为了更好的适用性,所以实现起来非常复杂,直接查看FFmpeg和Live555源代码来熟悉这些协议非常吃力,这里将它们独立出来实现,以便更好的理解协议。本文主要介绍RTSP,RTCP,RTP加载AAC音频流。
说明:
(1)大华IPC摄像头作为服务端
(2)在ubuntu16.04中编译实现测试程序
(3)服务端IP: 192.168.0.120
(4)客户端IP: 192.168.0.128
协议介绍:
用一句简单的话总结:RTSP发起/终结流媒体、RTP传输流媒体数据 、RTCP对RTP进行控制,同步。RTSP属于四层网络当中的应用层,RTP,RTCP属于传输层。RTSP和RTCP协议在之前博客中已经介绍过,这里不再重复。可以参考博客:
《ONVIF网络摄像头(IPC)客户端开发—最简RTSP客户端实现》
《ONVIF网络摄像头(IPC)客户端开发—RTSP RTCP RTP加载H264视频流》
RTP加载音频流与RTP加载视频流有些不一样,加载视频流一般使用单一NAL单元模式和分片封包模式,加载音频使用的是组合封包模式(Aggregation Packet)。组合封包模式开始是12字节的RTP头信息,接着的是组合封包头长度(2)字节和组合封包头(2字节),最后面是音频流数据,示意图如下:
AAC格式
在RTP流中传输的AAC音频流是不带有ADTS(Audio Data Transport Stream)信息的,所以客户端在接收到AAC数据流的时候需要自己手动添加上ADTS信息才能被正常的播放。ADTS格式如下:
上图来源于网络,AAC ES就是RTP接收到的AAC音频数据,其他参数值基本上都是固定的,需要填充的是采样频率,通道数和长度(注意:该长度=ADTS+AAC ES)。采样率定义如下:
There are 13 supported frequencies:
0: 96000 Hz
1: 88200 Hz
2: 64000 Hz
3: 48000 Hz
4: 44100 Hz
5: 32000 Hz
6: 24000 Hz
7: 22050 Hz
8: 16000 Hz
9: 12000 Hz
10: 11025 Hz
11: 8000 Hz
12: 7350 Hz
13: Reserved
14: Reserved
15: frequency is written explictly
声道定义如下:
0: Defined in AOT Specifc Config
1: 1 channel: front-center
2: 2 channels: front-left, front-right
3: 3 channels: front-center, front-left, front-right
4: 4 channels: front-center, front-left, front-right, back-center
5: 5 channels: front-center, front-left, front-right, back-left, back-right
6: 6 channels: front-center, front-left, front-right, back-left, back-right, LFE-channel
7: 8 channels: front-center, front-left, front-right, side-left, side-right, back-left, back-right, LFE-channel
8-15: Reserved
启动音频流:
RTSP客户端发送DESCRIBE,服务端会回复一个SDP,SDP里面有描述了音频流和视频流信息:
v=0
o=- 2229913047 2229913047 IN IP4 0.0.0.0
s=Media Server
c=IN IP4 0.0.0.0
t=0 0
a=control:*
a=packetization-supported:DH
a=rtppayload-supported:DH
a=range:npt=now-
m=video 0 RTP/AVP 96
a=control:trackID=0
a=framerate:15.000000
a=rtpmap:96 H264/90000
a=fmtp:96 packetization-mode=1;profile-level-id=64002A;sprop-parameter-sets=Z2QAKqwsaoHgCJ+WbgICAgQA,aO48sAA=
a=recvonly
m=audio 0 RTP/AVP 97
a=control:trackID=1
a=rtpmap:97 MPEG4-GENERIC/16000
a=fmtp:97 streamtype=5;profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3;config=1408
a=recvonly
m=application 0 RTP/AVP 107
a=control:trackID=4
a=rtpmap:107 vnd.onvif.metadata/90000
a=recvonly
从上面可以看到trackID=0 是一个h264视频流,trackID=1是一路AAC音频流,并且可以看到音频流采样率为16000,profile=1,sizelength=13;等等。
为了测试音频流,这里直接写成固定的,在启动的时候直接启动trackID=1音频流:
SETUP rtsp://192.168.0.120:554/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif/trackID=1 RTSP/1.0
CSeq: 4
Authorization: Digest username="admin", realm="Login to F8990C5E0599500D",nonce="780e768a-7f13-4f66-a08c-99d76e99e518",uri="rtsp://192.168.0.120:554/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif",response="920e21b61578599683bff9129352a6c7"
User-Agent: Caibiao_Lee
Transport:RTP/AVP;unicast;client_port=55182-55183
代码实现:
这里贴出RTP数据包解析和ADTS头添加的测试函数:
/********************************************************
Function: RTP_Client_UnPackAACAndStore
Description: 根据RTP协议解析网络接收到的AAC数据包,并且为AAC数据
添加ADTS头信息,最后将数据写入到文件中去。
Input:
*pu8Addr:RTP接收数据的地址
s32Len :RTP接收数据的长度
OutPut:none
Return: 0 成功;非0 失败
Others:
注意ADTS头长度的填充,这个长度包括了它自己本身
Author: Caibiao Lee
Date: 2019-10-05
*********************************************************/
static int RTP_Client_UnPackAACAndStore(unsigned char *pu8Addr,int s32Len)
{
RTP_HEADER_S *l_pstRTPHeader = NULL;
RTP_PACKET_S l_stRTPPack = {0};
RTP_PACKET_S *l_pstRTPPack = &l_stRTPPack;
unsigned short l_u16HeadrLen = 0;
unsigned short l_u16Header = 0;
unsigned short l_u16WriteLen = 0;
if((NULL==pu8Addr)||(s32Len<=0))
{
printf("input para error \n",__FUNCTION__,__LINE__);
return -1;
}
l_pstRTPHeader = (RTP_HEADER_S*)pu8Addr;
/**RTP 信息提取**/
l_pstRTPPack->u8Version = l_pstRTPHeader->bit1Version;
l_pstRTPPack->u8Padding = l_pstRTPHeader->bit1Padding;
l_pstRTPPack->u8Extension = l_pstRTPHeader->bit1Extension;
l_pstRTPPack->u8Cc = l_pstRTPHeader->bit4CsrcLen;
l_pstRTPPack->u8Marker = l_pstRTPHeader->bit1Marker;
l_pstRTPPack->u8Pt = l_pstRTPHeader->bit7PayLoadType;
/**RTP 序列号**/
l_pstRTPPack->u32SeqNum = 0;
l_pstRTPPack->u32SeqNum = (pu8Addr[2] & 0xff);
l_pstRTPPack->u32SeqNum <<= 8;
l_pstRTPPack->u32SeqNum |= (pu8Addr[3] & 0xff);
/**RTP 时间戳**/
l_pstRTPPack->u32TimeStamp = (pu8Addr[4] & 0xff);
l_pstRTPPack->u32TimeStamp <<= 8;
l_pstRTPPack->u32TimeStamp |= (pu8Addr[5] & 0xff);
l_pstRTPPack->u32TimeStamp <<= 8;
l_pstRTPPack->u32TimeStamp |= (pu8Addr[6] & 0xff);
l_pstRTPPack->u32TimeStamp <<= 8;
l_pstRTPPack->u32TimeStamp |= (pu8Addr[7] & 0xff);
/**RTP 同步源ID**/
l_pstRTPPack->u32Ssrc = (pu8Addr[8] & 0xff);
l_pstRTPPack->u32Ssrc <<= 8;
l_pstRTPPack->u32Ssrc |= (pu8Addr[9] & 0xff);
l_pstRTPPack->u32Ssrc <<= 8;
l_pstRTPPack->u32Ssrc |= (pu8Addr[10] & 0xff);
l_pstRTPPack->u32Ssrc <<= 8;
l_pstRTPPack->u32Ssrc |= (pu8Addr[11] & 0xff);
#if 0
printf("\n\n");
printf("u8Version :%d \n",l_pstRTPPack->u8Version);
printf("u8Padding :%d \n",l_pstRTPPack->u8Padding);
printf("u8Extension :%d \n",l_pstRTPPack->u8Extension);
printf("u8Cc :%d \n",l_pstRTPPack->u8Cc);
printf("u8Marker :%d \n",l_pstRTPPack->u8Marker);
printf("u8Pt :%d \n",l_pstRTPPack->u8Pt);
printf("u32SeqNum :%d \n",l_pstRTPPack->u32SeqNum);
printf("u32TimeStamp :%d \n",l_pstRTPPack->u32TimeStamp);
printf("u32Ssrc :%u \n",l_pstRTPPack->u32Ssrc);
printf("pu32Paylen :%d \n",l_pstRTPPack->u32Paylen);
#endif
l_u16HeadrLen = pu8Addr[12];
l_u16HeadrLen <<=8;
l_u16HeadrLen = pu8Addr[13];
l_u16Header = pu8Addr[14];
l_u16Header <<=8;
l_u16Header = pu8Addr[15];
/**ADTS**/
if(g_WriteFd>0)
{
/**for debug**/
unsigned char ADTS[] = {0xFF, 0xF1, 0x00, 0x00, 0x00, 0x00, 0xFC};
int audioSamprate = 16000;/**音频采样率**/
int audioChannel = 2; /**音频声道 1或2**/
int audioBit = 16; /**16位 固定**/
switch(audioSamprate) /**bit2~bit6 表示帧率**/
{
case 8000: /**11**/
ADTS[2] = 0x2c;
break;
case 16000: /**8**/
ADTS[2] = 0x60;
break;
case 32000: /**5**/
ADTS[2] = 0x54;
break;
case 44100: /**4**/
ADTS[2] = 0x50;
break;
case 48000: /**3**/
ADTS[2] = 0x4C;
break;
case 96000: /**0**/
ADTS[2] = 0x40;
break;
default:
break;
}
ADTS[3] = (audioChannel==2)?0x80:0x40;/**bit **/
l_u16WriteLen = s32Len - 16 + 7;
l_u16WriteLen <<= 5; /**8bit * 2 - 11 = 5(headerSize 11bit)**/
l_u16WriteLen |= 0x1F; /**5 bit 1**/
ADTS[4] = l_u16WriteLen>>8;
ADTS[5] = l_u16WriteLen & 0xFF;
fwrite(ADTS,1,7,g_WriteFd);
l_u16WriteLen = s32Len - 16;
fwrite(&pu8Addr[16],1,l_u16WriteLen,g_WriteFd);
}
return 0;
}
参考博客:
《AAC的ADTS头文件信息介绍》
《多媒体封装格式详解--- AAC ADTS格式分析》
工程下载:
在 liwen01
公众号中回复 网络编程
获取工程代码,本章代码工程名为:RtspRtcpRtpLoad_AAC.tar.gz
![](https://img2023.cnblogs.com/blog/555985/202212/555985-20221229171054114-894213487.jpg)