本文主要介绍以tcp方式实现rtsp拉流。
流程说明:
// 建立tcp连接
m_tcpClient = std::make_shared(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;
// 创建command类
m_command = std::make_shared(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;
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 dataPacket(new char[RECV_DATA_SIZE], std::default_delete());
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);
}
}
// 创建定时器
m_timer = std::make_shared(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();
}
// 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);