1. 相关协议
1. RTSP(Real Time Streaming Protocol)
定义了一对多应用程序如何有效地通过IP网络传送多媒体数据。RTSP在体系结构上位于RTP和RTCP之上,使用TCP或UDP完成数据传输;
2. RTP(Real-time Transport Protocol)
RTP详细说明了在互联网上传递音频和视频的标准数据包格式。它是创建在UDP协议上的,使用的是一个偶数UDP port
3. RTCP(Real-time Transport Control Protocol)
RTCP使用一个奇数 port。在RTCP与RTP联合工作时,RTP实施实际数据的传输,RTCP则负责将控制包送至电话中的每个人。
4. 协议结合视频的作用
RTP负责视频数据的传输,RTCP负责控制视频质量,RTSP负责提供视频功能控制
2. live555安装及拉流测试
# 根据个人系统下载完对应源码版本
cd live
./genMakefiles linux-64bit
sudo make
sudo make install
# 如出现报错fatal error: openssl/ssl.h: 没有那个文件或目录
sudo apt-get install libssl-dev
cd mediaServer
./live555MediaServer
# 后面通过VCL网络串流拉取本地视频验证即可
# 要注意的是验证视频需放在mediaServer文件夹下,且是live555支持的格式
以下是live555启动时的终端内容
LIVE555 Media Server
version 1.10 (LIVE555 Streaming Media library version 2022.07.14).
Play streams from this server using the URL
rtsp://172.17.9.120:8554/<filename>
//URL可用于在VCL中网络串流,文件须在mediaServer文件夹下
where <filename> is a file present in the current directory.
Each file's type is inferred from its name suffix:
".264" => a H.264 Video Elementary Stream file
".265" => a H.265 Video Elementary Stream file
".aac" => an AAC Audio (ADTS format) file
".ac3" => an AC-3 Audio file
".amr" => an AMR Audio file
".dv" => a DV Video file
".m4e" => a MPEG-4 Video Elementary Stream file
".mkv" => a Matroska audio+video+(optional)subtitles file
".mp3" => a MPEG-1 or 2 Audio file
".mpg" => a MPEG-1 or 2 Program Stream (audio+video) file
".ogg" or ".ogv" or ".opus" => an Ogg audio and/or video file
".ts" => a MPEG Transport Stream file
(a ".tsx" index file - if present - provides server 'trick play' support)
".vob" => a VOB (MPEG-2 video with AC-3 audio) file
".wav" => a WAV Audio file
".webm" => a WebM audio(Vorbis)+video(VP8) file
//文件格式必须为上述后缀名,不然会报错404错误,即视频格式不支持
See http://www.live555.com/mediaServer/ for additional documentation.
(We use port 8000 for optional RTSP-over-HTTP tunneling).)
VCL网络串流打开视频
视频格式转换命令(mp4->264)
ffmpeg -i test.mp4 -codec copy -bsf: h264_mp4toannexb -f h264 test.264
3. live555用H264格式保存视频
1. OpenRTSP作客户端
主要包含文件:openRTSP.cpp和playCommon.cpp
客户端功能看openRTSP.cpp,具体代码实现见playCommon.cpp。
存在的问题:openRTSP保存下来的H264文件无法直接用VLC进行播放
解决方法:在openRTSP保存文件后,需要再用VCL对文件进行保存,将VCL生成的h264文件的前两个00 00 00 01数据,即SPS&PPS复制到openRTSP保存的文件最前面。
上述调整过程略显繁琐,不建议在实际工程中应用。
2. testRTSPClient做客户端
对源码中的testRTSPClient.cpp文件进行修改,加入以H264文件格式保存的代码。
H264文件保存
#if 0 //H264 文件保存;0 false 关闭功能,1 ture 打开功能
if (!strcmp(fSubsession.mediumName(), "video"))
{
RTPSource *s=fSubsession.rtpSource();
uint32_t ssrc=s->SSRC();
std::map<uint32_t, ReceiveH264 *>::iterator it=ReceiveH264_Map.find(ssrc);
if (it == ReceiveH264_Map.end())
{
printf("%u\n", ssrc);
ReceiveH264 *h264_obj=new ReceiveH264();
h264_obj->setSSRC(ssrc);
ReceiveH264_Map.insert(make_pair(ssrc, h264_obj));
}
else
{
char temp[200];
ReceiveH264 *h264_obj = it->second;
sprintf_s(temp, "./H264/%u.h264", h264_obj->ssrc);
if (h264_obj->firstFrame)
{
unsigned int num;
SPropRecord *sps = parseSPropParameterSets(fSubsession.fmtp_spropparametersets(), num);
h264_obj->setSpsPPs(sps[0].sPropBytes, sps[0].sPropLength, sps[1].sPropBytes, sps[1].sPropLength);
// For H.264 video stream, we use a special sink that insert start_codes:
struct timeval tv = { 0,0 };
unsigned char start_code[4] = { 0x00, 0x00, 0x00, 0x01 };
FILE *fp = fopen(temp, "wb");
if (fp)
{
fwrite(start_code, 4, 1, fp);
printf("sps ..........%d\n", sps[0].sPropLength);
fwrite(sps[0].sPropBytes, sps[0].sPropLength, 1, fp);
fwrite(start_code, 4, 1, fp);
printf("pps ..........%d\n", sps[1].sPropLength);
fwrite(sps[1].sPropBytes, sps[1].sPropLength, 1, fp);
fclose(fp);
fp = NULL;
}
delete[] sps;
h264_obj->firstFrame = false;
}
else
{
char nalu = *fReceiveBuffer & 0x0F;
if (nalu == 0x05 || nalu == 0x01)
{
char *pbuf = (char *)fReceiveBuffer-4;
int size = frameSize + 4;
FILE *fp = fopen(temp, "ab");
if (fp)
{
fwrite(pbuf, size, 1, fp);
fclose(fp);
fp = NULL;
}
}
}
}
}
#endif
参考资料
live555 ubuntu编译
live555 RTSP推流H264
live555源码分析
Live555源码解密(OpenRTSP)
CMake文件编译参考