jrtplib3.9.1 example1

 
 
这个例子包含2个功能,发送和接收。
运行时需要设置端口和IP地址。如果是在单机上运行,则本地端口和目标端口设置为相同即可。目标IP地址设为127.0.0.1,则运行时,窗口显示在发送数据包,同时会显示收到了刚刚发送的数据包。截图如下:
-----------------------------------------------------------------------------------------------------
/*
Here's a small IPv4 example: it asks for a portbase and a destination and
starts sending packets to that destination.
*/
//这是一个IPv4的小例子:它需要一个本地端口和一个目标IP和端口,然后开始发送数据包到该目标地址。

#include "rtpsession.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 <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>

using namespace jrtplib ;

//
// 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 main routine
//主程序

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

RTPSession sess ;
uint16_t portbase , destport ;
uint32_t destip ;
std :: string ipstr ;
int status , i , 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 << "Enter the destination IP address" << std :: endl ;
std :: cin >> ipstr ;
destip = inet_addr ( ipstr . c_str ());
if ( destip == INADDR_NONE )
{
std :: cerr << "Bad IP address specified" << std :: endl ;
return - 1 ;
}

// The inet_addr function returns a value in network byte order, but
// we need the IP address in host byte order, so we use a call to
// ntohl
//函数inet_addr 返回网络字节序的值,但我们需要的IP地址是要主机字节序,所以我们调用ntohl函数
destip = ntohl ( destip );

std :: cout << "Enter the destination port" << std :: endl ;
std :: cin >> destport ;

std :: cout << std :: endl ;
std :: cout << "Number of packets you wish to be sent:" << std :: endl ;
std :: cin >> num ;

// Now, we'll create a RTP session, set the destination, send some
// packets 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 sending 10 samples each second, so we'll
// put the timestamp unit to (1.0/10.0)
//重要信息:本地的时间戳单位必须要设置,否则RTCP发送报告信息将会计算错误
//在本例,我们将每秒发送10个样本,所以我们将时间戳单位设置为(1.0/10.0)
sessparams . SetOwnTimestampUnit ( 1.0 / 10.0 );

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

RTPIPv4Address addr ( destip , destport );

status = sess . AddDestination ( addr );
checkerror ( status );

for ( i = 1 ; i <= num ; i ++ )
{
printf ( " \n Sending packet %d/%d \n " , i , num );

// send the packet发送数据包
status = sess . SendPacket (( void * ) "1234567890" , 10 , 0 , false , 10 );
checkerror ( status );

sess . BeginDataAccess ();

// check incoming packets检查是否有发送近来的数据包
if ( sess . GotoFirstSourceWithData ())
{
do
{
RTPPacket * pack ;

while (( pack = sess . GetNextPacket ()) != NULL )
{
// You can examine the data here
//可以在此检查收到的数据
printf ( "Got packet ! \n " );

// we don't longer need the packet, so
// we'll delete it
//我们不再需要该数据包,所以我们将其删除
sess . DeletePacket ( pack );
}
} while ( sess . GotoNextSourceWithData ());
}

sess . EndDataAccess ();

#ifndef RTP_SUPPORT_THREAD
status = sess . Poll ();
checkerror ( status );
#endif // RTP_SUPPORT_THREAD

RTPTime :: Wait ( RTPTime ( 1 , 0 ));
}

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

#ifdef WIN32
WSACleanup ();
#endif // WIN32
return 0 ;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值