前言
本文主要介绍以tcp方式实现rtsp拉流。
流程图
流程说明:
- 客户端发起tcp请求,如向真实相机设备请求,端口一般默认554;
- tcp连接成功,客户端与服务端开始rtsp信令交互;
- 客户端收到play命令响应后,开启线程等待接收数据,同时开启定时器,定时向服务端发送命令用于心跳保活;
- 服务端向客户端发送音视频数据;
- 客户端接收rtp数据并进行解复用;
设计
- 发送tcp请求
- 创建socket
- connect
- 设置非阻塞
- 设置tcp超时时间
// 建立tcp连接
m_tcpClient = std::make_shared<TcpClient>(nullptr, this);
if (!m_tcpClient.get()|| 0 != m_tcpClient->TcpCreate()|| 0 != m_tcpClient->TcpConnectByTime(ip.c_str(), port, 5)|| 0 != m_tcpClient->TcpSetNoBlock(true)|| 0 != m_tcpClient->TcpRecvTimeout(10))break;
- rtsp信令交互
// 创建command类
m_command = std::make_shared<CRtspCommand>(RTSP_TRANSPORT_RTP_TCP, m_tcpClient, m_func, m_user);
if (!m_command.get())break;// 创建rtsp client
if(!m_command->CreateRtspClient(m_rtspUrl, username, userpasswd))break;// 发送describe
int ret = m_command->SendDescribe();
if (0 != ret)break;
- 补充:创建rtsp client,发送describe后根据回调进行后续命令操作(使用开源库ireader/librtsp进行rtsp信令交互)
bool CRtspCommand::CreateRtspClient(const std::string& uri, const std::string& username, const std::string& userpasswd)
{if (uri.empty() || username.empty() || userpasswd.empty())return false;rtsp_client_handler_t handler;handler.send = SendCallback;handler.rtpport = RtpPortCallcback;handler.onannounce = AnnounceCallback;handler.ondescribe = DescribeCallback;handler.onsetup = SetupCallback;handler.onplay = PlayCallback;handler.onpause = PauseCallback;handler.onteardown = TeardownCallback;handler.onrtp = RtpDataCallback;m_rtsp = rtsp_client_create(uri.c_str(), username.c_str(), userpasswd.c_str(), &handler, this);if (!m_rtsp)return false;return true;
}
- 启动线程等待接收数据
#define RECV_DATA_SIZE (64*1024*1024)void CRtsp::RtspWorker()
{std::shared_ptr<char> dataPacket(new char[RECV_DATA_SIZE], std::default_delete<char[]>());memset(dataPacket.get(), 0x00, RECV_DATA_SIZE);int recvLen = 0;while (m_running){recvLen = m_tcpClient->TcpRecv(dataPacket.get(), RECV_DATA_SIZE);if (recvLen <= 0) // 数据接收失败{break;}if (0 != m_command->InputData(dataPacket.get(), recvLen)){break;}memset(dataPacket.get(), 0x00, STREAM_DATA_SIZE);}
}
- 启动定时器进行心跳保活(tcp方式需要)
// 创建定时器
m_timer = std::make_shared<ZDTimer>(10, 1000);
m_timer->Start();// 开启定时器
m_timer->AddTask(10000, [this]() {// 10s发送一次get parameter命令int count = rtsp_client_media_count((rtsp_client_t*)m_rtsp);if (count > 0)rtsp_client_get_parameter(m_rtsp, 0, NULL);
});// 停止定时器
if (m_timer.get())
{m_timer->Stop();m_timer.reset();
}
- 接收视频数据并解复用(使用开源库ireader/librtp库进行rtp数据解复用)
// 1.创建解复用器
m_demuxer = rtp_demuxer_create(100, profile ? profile->frequency : 90000, payload, encoding.c_str(), RtpPacketCallback, this);// 2. 塞数据
rtp_demuxer_input(m_demuxer, data, len);// 3. 数据回调
static int RtpPacketCallback (void* param, const void* packet, int bytes, uint32_t timestamp, int flags)
{// 裸流数据处理
}// 4. 销毁
rtp_demuxer_destroy(&m_demuxer);