一个基于JRTPLIB的轻量级RTSP客户端(myRTSPClient)——收流篇:(四)example代码解析

时间:2023-12-02 17:02:38

--------------------更新2018.08.20-------------------

添加http_tunnel_example.cpp作为RtspOverHttp示例程序。

--------------------更新2018.08.20结束-------------------

一、example逻辑伪码

myRTSPClient附带3个example程序:simple_example、complete_example、common_example。后两个example都是从simple_example中衍生出来的,以下将以simple_example为主进行解析,并且会在最后说明一下另两个example与simple_example的差别。

以下是simple_example简化后的伪代码,以便理解:

1. main():

2.   myRtspClient Client;

3.   Client.SetURI("rtsp://127.0.0.1/ansersion");

4.   Client.SendDESCRIBE();

5.   Client.SendSETUP();

6.   Client.SendPLAY();

7.   Write 1000 RTP Data to file "test_packet_recv.h264";

8.   Client.SendTEARDOWN();

9.   return;

二、simple_example.cpp代码注释

 #include <iostream>
#include "rtspClient.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h> using std::cout;
using std::endl; int main(int argc, char *argv[])
{
string RtspUri("rtsp://127.0.0.1/ansersion");
// string RtspUri("rtsp://192.168.81.145/ansersion");
RtspClient Client; /* Set up rtsp server resource URI */
Client.SetURI(RtspUri); /* Send DESCRIBE command to server */
Client.DoDESCRIBE(); /* Parse SDP message after sending DESCRIBE command */
Client.ParseSDP(); // 解析RTSP服务端受理DESCRIBE命令后返回的SDP信息 /* Send SETUP command to set up all 'audio' and 'video'
* sessions which SDP refers. */
Client.DoSETUP(); printf("start PLAY\n");
printf("SDP: %s\n", Client.GetSDP().c_str()); /* Send PLAY command to play only the
* 'video' sessions.*/
Client.DoPLAY("video"); /* Receive 1000 RTP 'video' packets
* note(FIXME):
* if there are several 'video' session
* refered in SDP, only receive packets of the first
* 'video' session, the same as 'audio'.*/
int packet_num = 0;
uint8_t buf[65534];
size_t size = 0; /* Write h264 video data to file "test_packet_recv.h264"
* Then it could be played by ffplay */
int fd = open("test_packet_recv.h264", O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR | S_IXUSR);
if(Client.GetSPSNalu(buf, &size)) { // 解码SDP中关于H264的SPS的数据
if(write(fd, buf, size) < 0) { // 将SPS写入文件
perror("write");
}
} else {
printf("SPS error\n");
} if(Client.GetPPSNalu(buf, &size)) { // 解码SDP中关于H264的PPS的数据
if(write(fd, buf, size) < 0) { // 将PPS写入文件
perror("write");
}
} else {
printf("PPS error\n");
} while(++packet_num < 1000) { // 接收1000个H264的数据包(确切的说是1000个NALU)
if(!Client.GetMediaData("video", buf, &size)) continue;
if(write(fd, buf, size) < 0) {
perror("write");
}
printf("recv %lu\n", size);
} printf("start TEARDOWN\n");
/* Send TEARDOWN command to teardown all of the sessions */
Client.DoTEARDOWN(); return 0;
}

三、common_example.cpp和complete_example.cpp

common_example.cpp不同于simple_example.cpp的地方只有开头几行,这些代码是为了可以用参数指定RTSP URI。

 int main(int argc, char *argv[])
{
if(argc != ) {
cout << "Usage: " << argv[] << " <URL>" << endl;
cout << "For example: " << endl;
cout << argv[] << " rtsp://127.0.0.1/ansersion" << endl;
return ;
}
cout << "Start play " << argv[] << endl;
cout << "Then put video data into test_packet_recv.h264" << endl;
string RtspUri(argv[]);
// string RtspUri("rtsp://192.168.81.145/ansersion");
RtspClient Client; /* Set up rtsp server resource URI */
Client.SetURI(RtspUri);
......
return ;
}

complete_example.cpp不同于simple_example.cpp的地方,就在于每次发送RTSP命令时, 它都会检查命令发送是否成功,并且检验返回信息是否是“200 OK”。

 int main(int argc, char * argv[])
{
......
/* Send DESCRIBE command to server */
if(Client.DoDESCRIBE() != RTSP_NO_ERROR) {
printf("DoDESCRIBE error\n");
return ;
}
printf("%s\n", Client.GetResponse().c_str());
/* Check whether server return '200'(OK) */
if(!Client.IsResponse_200_OK()) {
printf("DoDESCRIBE error\n");
return ;
}
......
return ;
}

OK!以上便是myRTSP的基础内容。

上一篇              回目录