分析:jrtp 不支持奇数端口
附:
发送端代码:send.cpp
/* Here's a small IPv4 example: it asks for a portbase and a destination and starts sending packets to that destination. */ #include "jrtplib3/rtpsession.h" #include "jrtplib3/rtpudpv4transmitter.h" #include "jrtplib3/rtpipv4address.h" #include "jrtplib3/rtpsessionparams.h" #include "jrtplib3/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; void checkerror(int rtperr) { if (rtperr < 0) //如果为负数 则返回失败信息 { std::cout << "ERROR: " << RTPGetErrorString(rtperr) << std::endl; exit(-1); } } int main(void) { #ifdef WIN32 WSADATA dat; WSAStartup(MAKEWORD(2,2),&dat); #endif // WIN32 RTPSession sess; uint16_t portbase=8000,destport=7800; uint32_t destip; std::string ipstr="127.0.0.1"; int status,i,num=10; destip = inet_addr(ipstr.c_str());//IP地址的建立 if (destip == INADDR_NONE) { std::cerr << "Bad IP address specified" << std::endl; return -1; } destip = ntohl(destip); RTPUDPv4TransmissionParams transparams; RTPSessionParams sessparams; sessparams.SetOwnTimestampUnit(1.0/10.0); sessparams.SetAcceptOwnPackets(true); transparams.SetPortbase(portbase); status = sess.Create(sessparams,&transparams); checkerror(status);//检查RTP会话创建过程是否失败 RTPIPv4Address addr(destip,destport);//套接字 status = sess.AddDestination(addr);//设置数据发送的目标地址(允许有多个目的地址) checkerror(status); for (i = 1 ; i <= num ; i++) { printf("\nSending packet %d/%d\n",i,num); status = sess.SendPacket((void *)"1234567890",10,0,false,10); checkerror(status); // #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; }编译:g++ -o send send.cpp -ljrtp -I /usr/local/include/jrtplib3/
接受端代码:
#include "jrtplib3/rtpsession.h" #include "jrtplib3/rtpudpv4transmitter.h" #include "jrtplib3/rtpipv4address.h" #include "jrtplib3/rtpsessionparams.h" #include "jrtplib3/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; // 错误处理函数 void checkerror(int rtperr) { if (rtperr < 0) //如果为负数 则返回失败信息 { std::cout << "ERROR: " << RTPGetErrorString(rtperr) << std::endl; exit(-1); } } int main(int argc, char** argv) { RTPSession sess; int localport; int portbase = 7800; int status; // 创建RTP会话 RTPUDPv4TransmissionParams transparams; RTPSessionParams sessparams; sessparams.SetOwnTimestampUnit(1.0/10.0); sessparams.SetAcceptOwnPackets(true); transparams.SetPortbase(portbase); status = sess.Create(sessparams,&transparams); checkerror(status); uint32_t destip; std::string ipstr="127.0.0.1"; int destport=7800; destip = inet_addr(ipstr.c_str());//IP地址的建立 if (destip == INADDR_NONE) { std::cerr << "Bad IP address specified" << std::endl; return -1; } destip = ntohl(destip); RTPIPv4Address addr(destip,destport);//套接字 sess.AddToAcceptList(addr); std::cout<<"Begin receive: \n"; sess.BeginDataAccess(); do{ status = sess.Poll(); checkerror(status); 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()); } } while(1); sess.EndDataAccess(); return 0; }编译:g++ -o receive receive.cpp -ljrtp -I /usr/local/include/jrtplib3/