I recently debugged the sample program example1 of jrtplib, but I couldn't call it any more. When I couldn't do anything, I suddenly remembered whether it was a problem when I converted the IP address from a string to a long integer. Force a 7f000001 (that is, the local back-to-loop test IP Address: 127.0.0.1) to destip, and then debug and pass. Hi, I didn't expect a problem. Now I have pasted the Code. The Code posted here has undergone some changes and can be used. I have also studied all the members of the packet class, and how to obtain the packaged data and packet header (each packet header is 12 bytes ). In addition, my jrtplib has been compiled into the wireless program version. The two examples in this article are also for the jrtplib supported by the wireless program. At the beginning, I compiled jrtplib into a thread version and found that there was always a problem. I simply removed the thread Support Section.
The example1 program is as follows:
Certificate ----------------------------------------------------------------------------------------------------------------------------------
/*
Here's a small IPv4 example: it asks for a portbase and a destination and
Starts sending packets to that destination.
This is a small IPv4 sample program: it requests a starting port and a destination address and then starts sending
Some packages to the target address.
*/
# Include "rtpsession. H"
# Include "rtppacket. H"
# Include "rtpudpv4transmitter. H"
# Include "rtp00004address. 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>
# Pragma comment (Lib, "jrtplib. lib ")
# Pragma comment (Lib, "ws2_32.lib ")
//
// This function checks if there was a RTP error. If so, it displays an error
// Message and exists.
// This function checks whether there is an RTP error. If yes, it displays an error message and exits.
// (It is suspected that an error is written in the original comment and should be exits .)
//
Void checkerror (INT rtperr)
{
If (rtperr <0)
{
STD: cout <"error:" <rtpgterrorstring (rtperr) <STD: Endl;
Getchar ();
Exit (-1 );
}
}
Uint32_t str_ip_to_long_ip (char * strip)
{
Int I, j = 3;
Unsigned char BYT [4];
Long LRET;
For (I = 0; I <strlen (strip); I ++)
{
}
Return LRET;
}
//
// The main routine
// Main routine
Int main (void)
{
# Ifdef Win32
Wsadata dat;
// Makeword (lobyte, hibyte) concatenates two bytes into one word
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
// First, we need to obtain necessary information.
STD: cout <"enter local portbase (enter the local start port):" <STD: Endl;
STD: CIN> portbase;
STD: cout <STD: Endl;
STD: cout <"Enter the destination IP address (enter the destination IP address)" <STD: Endl;
STD: CIN> ipstr;
Destip = inet_addr (ipstr. c_str (); // convert the string to a 32-bit IP address.
If (destip = inaddr_none)
{
STD: cerr <"Bad IP address specified (an incorrect IP address is specified)" <STD: Endl;
Return-1;
}
// The inet_addr function returns a value in network byte order,
// We need the IP address in host byte order, so we use a call
// Ntohl
// The inet_addr function returns a value in the network byte order, but what we need is the value in the main byte order,
// So we need to use an ntohl call.
Destip = ntohl (destip); // n (Network byte order) to H (host byte order)
// L (long)
// Debug
Destip = 0x7f000001;
STD: cout <"Enter the destination port (enter the destination port)" <STD: Endl;
STD: CIN> destport;
STD: cout <STD: Endl;
STD: cout <"number of packets you wish to be sent (number of packages you want to send):" <STD: Endl;
STD: CIN> num;
// Now, we'll create a RTP session, set the destination, send some
// Packets and poll for incoming data.
// Now, we can create an RTP session, set the target, send some packets, and then poll inbound 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)
// Important: the local timestamp unit must be set. Otherwise, the RTCP sender reports an error in this case,
// We will send 10 samples per second, so we put the timestamp unit (1.0/10.0 ).
Sessparams. setowntimestampunit (1.0/10.0 );
Sessparams. setacceptownpackets (true );
Transparams. setportbase (portbase );
Status = sess. Create (sessparams, & transparams); // create a session
Checkerror (Status );
Rtp00004address ADDR (destip, destport );
Status = sess. adddestination (ADDR); // Add the target IP Address
Checkerror (Status );
For (I = 1; I <= num; I ++)
{
Printf ("/nsending packet % d/% s/n", I, num, "1234567890 ");
// Send the packet Packet
Status = sess. sendpacket (void *) "1234567890", false, 10 );
Checkerror (Status );
Sess. begindataaccess (); // start data access
// Check incoming packets to check the inbound package
If (sess. gotofirstsourcewithdata ())
{
Do
{
Rtppacket * pack;
While (pack = sess. getnextpacket ())! = NULL)
{
// You can examine the data here you can check the data here
Printf ("got packet! /N ");
// We don't longer need the packet, so
// We'll delete it. We no longer need this package, so we delete it.
Delete pack;
}
} While (sess. gotonextsourcewithdata ());
}
Sess. enddataaccess (); // end data access;
# 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
Getchar ();
Return 0;
}
Certificate ----------------------------------------------------------------------------------------------------------------------------------
The example3 program is as follows:
Certificate ----------------------------------------------------------------------------------------------------------------------------------
/*
This IPv4 example listens for incoming packets
And automatically adds destinations
For new sources.
The IPv4 sample program monitors inbound data and automatically adds the target address to the new source.
*/
# Include "rtpsession. H"
# Include "rtppacket. H"
# Include "rtpudpv4transmitter. H"
# Include "rtp00004address. 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>
# Pragma comment (Lib, "ws2_32.lib ")
//
// 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:" <rtpgterrorstring (rtperr) <STD: Endl;
Getchar ();
Exit (-1 );
}
}
//
// The New Class routine
// New class routine; inherited from rtpsession class
Class myrtpsession: Public rtpsession
{
Protected:
Void onnewsource (rtpsourcedata * dat)
{
If (dat-> isownssrc ())
Return;
Uint32_t IP address;
Uint16_t port;
If (dat-> getrtpdataaddress ()! = 0)
{
Const rtp00004address * ADDR = (const rtp00004address *) (dat-> getrtpdataaddress ());
IP = ADDR-> getip ();
Port = ADDR-> getport ();
}
Else if (dat-> getrtcpdataaddress ()! = 0)
{
Const rtp00004address * ADDR = (const rtp00004address *) (dat-> getrtcpdataaddress ());
IP = ADDR-> getip ();
Port = ADDR-> getport ()-1;
}
Else
Return;
Rtp00004address DEST (IP, Port );
Adddestination (DEST );
Struct in_addr inaddr;
Inaddr. s_addr = htonl (IP );
STD: cout <"adding destination" <STD: string (inet_ntoa (inaddr) <":" <port <STD: Endl;
}
Void onbyepacket (rtpsourcedata * dat)
{
If (dat-> isownssrc ())
Return;
Uint32_t IP address;
Uint16_t port;
If (dat-> getrtpdataaddress ()! = 0)
{
Const rtp00004address * ADDR = (const rtp00004address *) (dat-> getrtpdataaddress ());
IP = ADDR-> getip ();
Port = ADDR-> getport ();
}
Else if (dat-> getrtcpdataaddress ()! = 0)
{
Const rtp00004address * ADDR = (const rtp00004address *) (dat-> getrtcpdataaddress ());
IP = ADDR-> getip ();
Port = ADDR-> getport ()-1;
}
Else
Return;
Rtp00004address DEST (IP, Port );
Deletedestination (DEST );
Struct in_addr inaddr;
Inaddr. s_addr = htonl (IP );
STD: cout <"deleting destination" <STD: string (inet_ntoa (inaddr) <":" <port <STD: Endl;
}
Void onremovesource (rtpsourcedata * dat)
{
If (dat-> isownssrc ())
Return;
If (dat-> receivedbye ())
Return;
Uint32_t IP address;
Uint16_t port;
If (dat-> getrtpdataaddress ()! = 0)
{
Const rtp00004address * ADDR = (const rtp00004address *) (dat-> getrtpdataaddress ());
IP = ADDR-> getip ();
Port = ADDR-> getport ();
}
Else if (dat-> getrtcpdataaddress ()! = 0)
{
Const rtp00004address * ADDR = (const rtp00004address *) (dat-> getrtcpdataaddress ());
IP = ADDR-> getip ();
Port = ADDR-> getport ()-1;
}
Else
Return;
Rtp00004address DEST (IP, Port );
Deletedestination (DEST );
Struct in_addr inaddr;
Inaddr. s_addr = htonl (IP );
STD: cout <"deleting destination" <STD: string (inet_ntoa (inaddr) <":" <port <STD: Endl;
}
};
//
// The main routine
//
Int main (void)
{
# Ifdef Win32
Wsadata dat;
Wsastartup (makeword (2, 2), & dat );
# Endif // Win32
Myrtpsession sess;
Uint16_t portbase, destport;
Uint32_t destip;
STD: String ipstr;
Int status, I, num, j = 0;
Char SSS [100];
Zeromemory (SSS, 100 );
// First, we'll ask for the necessary information
// First, we request the necessary data
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.
// Now, we will create an RTP session, set the target address and round-robin the 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 just use 8000 samples per second
// There are 8000 samples per second.
/* Important :*/
// Sessparams. setowntimestampunit (1.0/8000.0 );
Sessparams. setowntimestampunit (1.0/10.0 );
Sessparams. setacceptownpackets (true );
Transparams. setportbase (portbase );
Status = sess. Create (sessparams, & transparams );
Checkerror (Status );
For (I = 1; I <= num; I ++)
{
Sess. begindataaccess ();
// STD: cout <"got packet! "<STD: Endl;
// Check incoming packets to check the inbound package
If (sess. gotofirstsourcewithdata ())
{
Do
{
Rtppacket * pack;
While (pack = sess. getnextpacket ())! = NULL)
{
Memcpy (SSS, pack-> getpacketdata () + 12, pack-> getpacketlength ()-12 );
// You can examine the data here
// Printf ("got packet! % D/n % s/n ", J ++, pack-> getpacketlength (), SSS );
Printf ("got packet! % D/N ", J ++ );
Printf ("packet Len: % d/N", pack-> getpacketlength ());
Printf ("packet data: % s/n", SSS );
For (int K = 0; k <12; k ++)
Printf ("rtpheader Bytes: % x/N", pack-> getpacketdata () [k]);
// We don't longer need the packet, so
// We'll delete it
Delete 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;
}
Certificate -----------------------------------------------------------------------------------------------------------------------------------