Use of open-source RTP-jrtplib

Source: Internet
Author: User

The reprinting of jrtp is described in detail.

I have been studying RTP for several days. I will summarize it today.
For the establishment of the jrtplib environment, see my previous summary. Now I will mainly talk about how to learn example under jrtplib3.71.
 
I. sample is a simple IPv4 column that implements RTP data transmission on the local machine.
 
1. initialization.
We know that RTP usually uses UDP protocol for data transmission. In Windows, of course, we need to use sockets that we are familiar with, so we need to initialize them first, load the socket library.
View plaincopy to clipboardprint?
# Ifdef Win32
Wsadata dat;
Wsastartup (makeword (2, 2), & dat );
# Endif // Win32
Rtpsession sess;

// Of course, when our program ends, we need to clear the socket library.
# Ifdef Win32
Wsacleanup ();
# Endif // Win32

// We also need to use the following header files:
# Include "rtpsession. H" // defines rtpsession
# Include "rtppacket. H" // defines rtppacket packets
# Include "rtpudpv4transmitter. H" // defines the second parameter of rtpsession
// Rtpudpv4transmissionparams
# Include "rtp00004address. H" // defines rtp00004address
# Include "rtpsessionparams. H" // defines the first parameter of rtpsession.
// Rtpsessionparams
# Include "rtperrors. H" // defines the error message in RTP.

// Obtain error information, which is inevitable during use. Therefore, you can use the // jrtplib function to obtain the error information. The method is as follows:
Void checkerror (INT rtperr)
{
If (rtperr <0)
{
STD: cout <"error:" <rtpgterrorstring (rtperr) <STD: Endl;
Exit (-1 );
}
}
# Ifdef Win32
Wsadata dat;
Wsastartup (makeword (2, 2), & dat );
# Endif // Win32
Rtpsession sess;
 
// Of course, when our program ends, we need to clear the socket library.
# Ifdef Win32
Wsacleanup ();
# Endif // Win32
 
// We also need to use the following header files:
# Include "rtpsession. H" // defines rtpsession
# Include "rtppacket. H" // defines rtppacket packets
# Include "rtpudpv4transmitter. H" // defines the second parameter of rtpsession
// Rtpudpv4transmissionparams
# Include "rtp00004address. H" // defines rtp00004address
# Include "rtpsessionparams. H" // defines the first parameter of rtpsession.
// Rtpsessionparams
# Include "rtperrors. H" // defines the error message in RTP.
 
// Obtain error information, which is inevitable during use. Therefore, you can use the // jrtplib function to obtain the error information. The method is as follows:
Void checkerror (INT rtperr)
{
If (rtperr <0)
{
STD: cout <"error:" <rtpgterrorstring (rtperr) <STD: Endl;
Exit (-1 );
}
}
Function rtpgterrorstring (...) Based on a negative number returned for an error, the unified error mechanism adopted by jrtplib returns a standard string STD: String in C ++, indicating the error message, the portbase we specify is not an even number. (Why? I 'd like to explain it below)
2. rtpsession object settings
To use sockets, we must specify a listening port for our listening socket before using it, that is, the portbase value here. We can set this by calling the member function of rtpsession's second parameter rtpudpv4transmissionparams,
Transparams. setportbase (portbase );
 
In addition, we need to set the following values through the first parameter of rtpsession:

Sessparams. setowntimestampunit (1.0/10.0 );
// Note that we must set this value, 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)
// Setting the timestamp is very important and is another important task in the RTP Session Initialization Process.
// This parameter is measured in seconds. For example, when the 8 000Hz sampled audio data is transmitted using RTP sessions
// The number increases by 8000 per second, so the timestamp unit should be set to 1/8000 accordingly:
Sessparams. setacceptownpackets (true );
// Through this function, we can set whether to receive custom data packets.
 
3. Data Transmission
I think when we want to establish a connection, we need to let the sender know the IP address of the host to be sent. In jrtplib, we can use the rtpsession member functions adddestination (), deletedestination () and cleardestinations. For example, the following figure shows the Port 6000 that sends data to the local machine:
 
Unsigned long ADDR = ntohl (inet_addr ("127.0.0.1 "));
Sess. adddestination (ADDR, 6000 );
 
Of course, we can also do this to add a client,
 
Rtpipv4address ADDR (destip, destport); // destip is the Client IP address, and destport is the client // port number
Status = sess. adddestination (ADDR );
Checkerror (Status );

After all the target addresses are specified, you can call the sendpacket () method of the rtpsession class to send streaming media data to all target addresses. Sendpacket () is an overload function provided by the rtpsession class. It has the following forms:
Int sendpacket (void * data, int Len)
Int sendpacket (void * data, int Len, unsigned char PT, bool mark,
Unsigned long timestampinc)
Int sendpacket (void * data, int Len, unsigned short hdrextid, void * hdrextdata,
Int numhdrextwords)
Int sendpacket (void * data, int Len, unsigned char PT, bool mark,
Unsigned long timestampinc, unsigned short hdrextid,
Void * hdrextdata, int numhdrextwords)
The most typical use of sendpacket () is similar to the following statement. The first parameter indicates the data to be sent, and the second parameter indicates the length of the data to be sent, the following are the RTP load types, identifiers, and timestamp increments. As follows:
Status = sess. sendpacket (void *) "1234567890", false, 10 );
Checkerror (Status );
For the same RTP Session, the load type, identifier, and timestamp increment are usually the same. jrtplib allows you to set them as the default parameters of the session. This is done by calling setdefaultpayloadtype () of the rtpsession class () setdefamark mark () and setdefatimetimestampincrement () methods. Setting these default parameters for RTP sessions can simplify data transmission. For example, if you set the default parameters for RTP sessions:
Session. setdefapaypayloadtype (96); // note that this parameter cannot be set at will. See rfc3551
Session. setdefamark mark (false );
Session. setdefatimetimestampincrement (160 );
After setting the preceding values, we can send data as follows:
Status = sess. sendpacket (void *) "1234567890", 10 );
3. receive data
For the receiver of streaming media data, you must first call the polldata () method of the rtpsession class to receive the RTP or RTCP datagram sent. Since multiple participants (sources) are allowed in the same RTP session, you can call the gotofirstsource () and gotonextsource () Methods of the rtpsession class to traverse all sources, you can also use the gotofirstsourcewithdata () and gotonextsourcewithdata () Methods of the rtpsession class to traverse those sources with data. After the valid data source is detected from the RTP session, you can call the getnextpacket () method of the rtpsession class to extract the RTP datagram from it. After the received RTP datagram is processed, remember to release it in time. The following code demonstrates how to process the received RTP datagram:
View plaincopy to clipboardprint?
Session. begindataaccess ();
If (session. gotofirstsource ()){
Do {
Rtppacket * packet;
While (packet = session. getnextpacket ())! = 0 ){
Cout <"got packet with extended sequence number"
<Packet-> getextendedsequencenumber ()
<"From SSRC" <packet-> getssrc () <Endl;
Session. deletepacket (packet );
}
} While (session. gotonextsource ());
}
Session. enddataaccess ();
Session. begindataaccess ();
If (session. gotofirstsource ()){
Do {
Rtppacket * packet;
While (packet = session. getnextpacket ())! = 0 ){
Cout <"got packet with extended sequence number"
<Packet-> getextendedsequencenumber ()
<"From SSRC" <packet-> getssrc () <Endl;
Session. deletepacket (packet );
}
} While (session. gotonextsource ());
}
Session. enddataaccess ();
Jrtplib defines three receiving modes for RTP datagram, each of which specifies which RTP datagram will be accepted and which RTP datagram will be rejected. You can set the following receiving modes by calling the setreceivemode () method of the rtpsession class:
① Receivemode_all: the default receiving mode. All received RTP data packets will be accepted;
② Receivemode_ignoresome except some specific senders, all incoming RTP datagram will be accepted, and the list of rejected senders can be called by addtoignorelist (), clearignorelist (), and deletefromignorelist () method;
③ Receivemode_acceptsome except for some specific senders, all incoming RTP datagram data will be rejected, and the list of accepted senders can be called by addtoacceptlist (), clearacceptlist () and the eletefromacceptlist method.
4. Run the program
Before running the program, we need to know that this is a UDP-based datagram service. Our example1 is the RTP transmission implemented on the local machine, and there is only one rtpsession object, so our listening and receiving ports are the same, so at this time, our portbase should be consistent with the port of the client we add to the rtpsession object, as shown above, we can set it to 6000. but if it is in two threads, we should note that the server port and client port must be different. This is also the basic knowledge. Pay attention to and think more when using it.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/li_007/archive/2007/07/13/1689005.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.