Use of open-source RTP-jrtplib (2)

Source: Internet
Author: User

 

Use of open-source RTP-jrtplib (2)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. # Ifdef Win32 <br/> wsadata dat; <br/> wsastartup (makeword (2, 2), & dat ); <br/> # endif // Win32 <br/> rtpsession sess; </P> <p> // Of course, when our program ends, we need to clear the socket library. <Br/> # ifdef Win32 <br/> wsacleanup (); <br/> # endif // Win32 </P> <p> // header files, we need to use the following: <br/> # include "rtpsession. H "// defines rtpsession <br/> # include" rtppacket. H "// defines rtppacket data packets <br/> # include" rtpudpv4transmitter. H "// defines the second rtpsession parameter <br/> // rtpudpv4transmissionparams <br/> # include" rtp00004address. H "// defines rtp00004address <br/> # include" rtpsessionparams. H "// defines the first rtpsession parameter <br/> // rtpsessionparams <br/> # include" rtperrors. H "// defines the error information in RTP </P> <p> // gets the error information. errors are inevitable during use, therefore, we can use the // jrtplib function to obtain the error information. The method is as follows: <br/> void checkerror (INT rtperr) <br/>{< br/> If (rtperr <0) <br/>{< br/> STD: cout <"error:" <rtpgterrorstring (rtperr) <STD: Endl; <br/> exit (-1); <br/>}< br/>}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, here) 2. For rtpsession object settings, we must specify a listening port for our listening socket before using it, that is, the value of portbase here. We can set it by calling the member function of rtpsession's second parameter rtpudpv4transmissionparams. setportbase (portbase); also, 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) // It is important to set a timestamp. This is another important task in the RTP Session Initialization Process. The unit is seconds. For example, when an RTP session is used to transmit 8 000Hz sampled audio data, because the timestamp // increases by 8000 per second, the timestamp unit should be set to 1/8000: 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 function adddestination (), deletedestination () and cleardestinations. For example, the following figure shows 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 add a client, rtp00004address ADDR (destip, destport); // destip is the Client IP address, destport is the client // port number status = sess. adddestination (ADDR); checkerror (Status); after all 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 handle) int sendpacket (void * data, int Len, unsigned char PT, bool mark, unsigned long timestampinc, unsigned short hdrextid, void * hdrextdata, int numhdrex The most typical use of twords) sendpacket () is similar to the following statement. The first parameter is 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. 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 () and setdefamark mark () of the rtpsession class () and setdefatimetimestampincrement. Setting these default parameters for RTP sessions can simplify data transmission. For example, if the default parameter is set for RTP sessions: Session. setdefapaypayloadtype (96); // note that this parameter cannot be set at will. For details, refer to rfc3551session. setdefamark mark (false); Session. setdefatimetimestampincrement (160); after setting the preceding value, we can send data as follows: Status = sess. sendpacket (void *) "1234567890", 10); 3. For the receiver of streaming media data, you must first call the polldata () of the rtpsession class () method 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: Session. begindataaccess (); <br/> If (Session. gotofirstsource () {<br/> do {<br/> rtppacket * packet; <br/> while (packet = session. getnextpacket ())! = 0) {<br/> cout <"got packet with extended sequence number" <br/> <packet-> getextendedsequencenumber () <br/> <"from SSRC" <packet-> getssrc () <Endl; <br/> session. deletepacket (packet); <br/>}< br/>} while (Session. gotonextsource (); <br/>}< br/> 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 call the setreceivemode () method of the rtpsession class to set the following receiving modes: ① receivemode_all: the default receiving mode, and 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 () (3) receivemode_acceptsome, except for some specific senders, all incoming RTP data packets will be rejected, and the list of accepted senders can be set by calling addtoacceptlist (), clearacceptlist () and the eletefromacceptlist method. 4. 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.

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.