jrtplib example1源码解析

源码中的example1.cpp源码解析

/*
   Here's a small IPv4 example: it asks for a portbase and a destination and 
   starts sending packets to that destination.
   这个IPv4版的例子:它要求portbase(本地端口)与destination (目的端口)然后发送消息
   同时还监听消息。可以自发自收,也可以作为发送程序也可作为接收程序——zjk
*/


#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.
//
//获取错误的函数

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;//输入发送到的目标主机的IP
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
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.

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)
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("\nSending 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));//我认为这几行从#if开始应该放到接收函数的start的上面,不然不开启遍历查询函数poll,不会在后台开启查询线程,则第一个数据包是接收不到的
}

sess.BYEDestroy(RTPTime(10,0),0,0);//延时10S用来等待发送RTCP退出


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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值