jrtplib3.9.1 example4

本示例展示了如何使用线程处理IPv4数据包的RTP会话,包括数据包发送和接收的流程。通过输入本地端口基址和等待时间,创建RTP会话并进行数据交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

例子1发送数据,本例接收数据,执行结果:
/*
This IPv4 example uses the background thread itself to process all packets.
You can use example one to send data to the session that's created in this
example.
本IPv4示例仅仅使用后台线程来处理所有的数据包。
你可以从示例1来发送数据到本示例创建的会话中。
*/

#include "rtpsession.h"
#include "rtppacket.h"
#include "rtpudpv4transmitter.h"
#include "rtpipv4address.h"
#include "rtpsessionparams.h"
#include "rtperrors.h"
#ifndef WIN32
#include <netinet/in.h>
#include <arpa/inet.h>
#else
#include <winsock2.h>
#endif // WIN32
#include "rtpsourcedata.h"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>

using namespace jrtplib;

#ifdef RTP_SUPPORT_THREAD

//
// This function checks if there was a RTP error. If so, it displays an error
// message and exists.
//本函数检查是否有RTP错误。如果有,本函数显示错误信息,然后退出。

void checkerror(int rtperr)
{
if (rtperr < 0)
{
std::cout << "ERROR: " << RTPGetErrorString(rtperr) << std::endl;
exit(-1);
}
}

//
// The new class routine
//一个新的继承自RTPSession的类

class MyRTPSession : public RTPSession
{
protected:
//轮询线程每次启动时被调用。每当检测到有传入的数据到达时,或者当要发送一个RTCP复合数据包时,这便会发生。
void OnPollThreadStep();
//对接收到的数据包进行处理
void ProcessRTPPacket(const RTPSourceData &srcdat,const RTPPacket &rtppack);
};

void MyRTPSession::OnPollThreadStep()
{
BeginDataAccess();

// check incoming packets
//检查发送进来的数据包
if (GotoFirstSourceWithData())
{
do
{
RTPPacket *pack;
RTPSourceData *srcdat;

srcdat = GetCurrentSourceInfo();

while ((pack = GetNextPacket()) != NULL)
{
//此处对接收到的数据包进行具体的处理
ProcessRTPPacket(*srcdat,*pack);
DeletePacket(pack);
}
} while (GotoNextSourceWithData());
}

EndDataAccess();
}

//对接收到的数据包进行处理
void MyRTPSession::ProcessRTPPacket(const RTPSourceData &srcdat,const RTPPacket &rtppack)
{
// You can inspect the packet and the source's info here
// 你可以在此处检查数据包,以及该数据源的信息
std::cout << "Got packet " << rtppack.GetExtendedSequenceNumber() << " from SSRC " << srcdat.GetSSRC() << std::endl;
}

//
// The main routine
// 主函数

int main(void)
{
#ifdef WIN32
WSADATA dat;
WSAStartup(MAKEWORD(2,2),&dat);
#endif // WIN32

MyRTPSession sess;
uint16_t portbase;
std::string ipstr;
int status,num;

        // First, we'll ask for the necessary information
// 首先,让用户输入一些必要的信息
std::cout << "Enter local portbase:" << std::endl;
std::cin >> portbase;
std::cout << std::endl;

std::cout << std::endl;
std::cout << "Number of seconds you wish to wait:" << std::endl;
std::cin >> num;

// Now, we'll create a RTP session, set the destination
// and poll for incoming data.
//现在,我们将创建一个RTP会话,设置目的端点,然后轮询等待发进来的数据

RTPUDPv4TransmissionParams transparams;
RTPSessionParams sessparams;

// IMPORTANT: The local timestamp unit MUST be set, otherwise
// RTCP Sender Report info will be calculated wrong
// In this case, we'll be just use 8000 samples per second.
// 重要信息:本地的时间戳单位必须要设置,否则RTCP发送报告信息将会计算错误
// 在本例,我们就用每秒8000个样本
sessparams.SetOwnTimestampUnit(1.0/8000.0);

transparams.SetPortbase(portbase);
status = sess.Create(sessparams,&transparams);
checkerror(status);

// Wait a number of seconds
// 等待几秒钟
RTPTime::Wait(RTPTime(num,0));

sess.BYEDestroy(RTPTime(10,0),0,0);

#ifdef WIN32
WSACleanup();
#endif // WIN32
return 0;
}

#else

int main(void)
{
std::cerr << "Thread support is required for this example" << std::endl;//本例需要线程支持
return 0;
}

#endif // RTP_SUPPORT_THREAD
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值