天天看點

jrtplib example1源碼解析

源碼中的example1.cpp源碼解析

#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;

}

繼續閱讀