Real-time Video Transfer Protocol RTP

Source: Internet
Author: User
Tags error code error handling pack sessions valid zip

One, RTP (real-time transmission protocol) The RTP full name is the real-time Transport Protocol (real-time transport protocol), and the RTP protocol is commonly used in streaming media systems (with RTCP protocol or RTSP protocol). Because RTP itself has time stamp, it is used as a formate in FFmpeg. Each RTP datagram consists of two parts of the head (header) and load (Payload), where the first 12 bytes of the head are fixed, while the payload can be audio or video data. Second, RTP header format

The version number of the V:RTP protocol, which accounts for 2 bits, the current protocol version number is 2 P: The fill flag, which occupies 1 bits, if p=1, fills one or more additional eight-bit groups at the end of the message, which are not part of the payload. X: The extension flag, which accounts for 1 bits, if x=1, then there is an extended header CC:CSRC counter in the RTP header, which accounts for 4 bits, indicating the number of CSRC identifiers M: tags, 1 bits, different payloads have different meanings, for video, mark the end of a frame; for audio, mark the beginning of the session. PT: Valid load type, accounting for 7 bits, used to describe the type of payload in RTP message, such as GSM audio, JPEM image, etc., in the streaming media is used to distinguish between audio stream and video stream, so that the client can parse the serial number: 16 bits, used to identify the sender of the RTP message sent by the serial number, The serial number is increased by 1 for each message sent. This field can be used to check for drops when the network is in bad condition when the host protocol of the current layer is UDP. At the same time the network jitter can be used to reorder the data, the initial value of the sequence number is random, and the audio packet and video packet sequence are counted separately. Time Stamp (Timestamp): 32 bits, must use a four kHz clock frequency. The timestamp reflects the sampling time of the first eight-bit group of the RTP message. The receiver uses timestamps to calculate delay and delay jitter, and to synchronize control. Synchronization source (SSRC) Identifier: 32 bits, used to identify the synchronization source. The identifier is randomly selected, and the two simultaneous sources participating in the same video conference cannot have the same ssrc. Special Source (CSRC) identifiers: Each CSRC identifier occupies 32 bits and can have 0~15. Each CSRC identifies all the special sources that are included in the payload of the RTP message. third, RTP load H264 code stream

Reference Web page: http://blog.csdn.net/chen495810242/article/details/39207305 , RTP programming (JRTPLIB)

Reference page: http://blog.csdn.net/pu1030/article/details/7619908

Jrtplib is an RTP library implemented in the C + + language that can now be run on a variety of operating systems such as Windows, Linux, FreeBSD, Solaris, Unix, and VxWorks. 1.linux (Raspbian) installation Jrtplib

First download the latest source package from Jrtplib's website (http://research.edm.uhasselt.be/jori/page/Main/HomePage.html), I downloaded the jrtplib-3.11.1.zip. Then from the Jthread website (first download the latest source package from Jrtplib's website (http://research.edm.uhasselt.be/jori/page/Main/HomePage.html), I downloaded the jrtplib-3.11.1.zip. Download the corresponding source package from the Jthread website and download the corresponding source package jthread-1.3.3.zip.

Compilation instructions: The first is to use the thread provided by the Jthread library to automatically perform the reception of the data in the background. The second is that the user calls the poll method in Rtpsession. If you take the first method, you install the Jthread library, and the jthread-1.3.3 must be installed with jrtplib-3.11.1 first. Because in jrtplib-3.11.1 's configure, the system will find out if the Jthread library has been compiled, if so, then the compiled JRTP Library will open support for Jthread. Therefore, if the compilation of JRTP in the compilation of Jthread, compiled JRTP is not open to jthread support. compiling Jthread

Unzip the downloaded compressed package into the jthread-1.3.3 directory, modify the CMakeLists.txt in the directory;

Add to:

Set (Cmake_system_name Linux) set ( 
cmake_c_compiler ARM-LINUX-GNUEABIHF-GCC) 
Set (Cmake_cxx_compiler arm-linux-gnueabihf-g++) 
Set (Cmake_install_prefix "/usr/local")

Compile and install:

CMake CMakeLists.txt make do

install
compiling Jrtplib

Also modify CMakeLists.txt

Set (Cmake_system_name Linux) set ( 
cmake_c_compiler ARM-LINUX-GNUEABIHF-GCC) 
Set (Cmake_cxx_compiler arm-linux-gnueabihf-g++) Set ( 
cmake_install_prefix "/usr/local")

set (jrtplib_internal_includes "/usr/ Local/include ")
set (jrtplib_external_includes"/usr/local/include ")

Compile and install:

CMake CMakeLists.txt make do

install
2. Initialize

You should first generate an instance of the Rtpsession class to represent this RTP session, and then call the Create () method to initialize it. The Create () method of the Rtpsession class has only one parameter that indicates the port number used for this RTP session.

The rtpgeterrorstring () function passes the error code as a parameter, and then returns the error message that corresponds to the error code. Jrtplib uses a unified error-handling mechanism, which provides all functions that return negative numbers to indicate that some form of error can be called by this function

The Settimestampunit () method sets the appropriate timestamp cell, which represents the timestamp unit in seconds. For example, when a 8000Hz sampled audio data is transmitted using an RTP session, the timestamp unit should be set to 1.0/8000.0 as the timestamp increments by 8000 per second.

#include <stdio.h>
#include "rtpsession.h"

int main (void)
{
  rtpsession sess;
  int status;
  char* msg;
  /* Create a RTP instance with port number 6000 */
  Sess. Create (6000);
  /* Output error cause if creation failed *
  /msg = rtpgeterrorstring (status);
  printf ("Error String:%s\\n", msg);
  /* Set the timestamp unit */
  Sess. Settimestampunit (1.0/8000.0);
  return 0;
}
3. Send Data

The RTP protocol allows multiple destination addresses to exist in the same session, which can be done by invoking the adddestination (), Deletedestination (), and Cleardestinations () methods of the Rtpsession class. For example, the following statement represents a 6000 port that allows the RTP session to send data to the local host:

unsigned long addr = Ntohl (inet_addr ("127.0.0.1"));
Sess. Adddestination (addr, 6000);

The Sendpacket () method sends streaming media data to all destination addresses. Sendpacket () is an overloaded function provided by the Rtpsession class, which has the following various forms:

int sendpacket (void *data,int len)
int sendpacket (void *data,int len,unsigned char pt,bool mark,unsigned long Timesta MPINC)
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 usage of sendpacket () is similar to the following statement, where the first parameter is the data to be sent, and the second parameter indicates the length of the data that will be sent, followed by the RTP payload type, identity, and timestamp increment.

Sess. Sendpacket (buffer, 5, 0, False, 10);

For the same RTP session, the payload type, identity, and timestamp increment are generally the same, and Jrtplib allows them to be set to the default parameters of the session, by calling the Setdefaultpayloadtype () of the Rtpsession class, Setdefaultmark () and the Setdefaulttimestampincrement () method to accomplish this. The benefit of setting these default parameters for RTP sessions is to simplify the sending of data, for example, if the default parameters are set for the RTP session:

    Sess. Setdefaultpayloadtype (0);
    Sess. Setdefaultmark (false);
    Sess. Setdefaulttimestampincrement (10);

Then, when the data is sent, simply indicate the data to be sent and its length:

    Sess. Sendpacket (buffer, 5);
4. Data transmissionCall the Polldata () method of the Rtpsession class to receive the RTP or RTCP datagram sent over. Because multiple contributors (sources) are allowed in the same RTP session, you can traverse all the sources by calling the Gotofirstsource () and Gotonextsource () methods of the Rtpsession class. You can also traverse the source that carries the data by calling the Gotofirstsourcewithdata () and Gotonextsourcewithdata () methods of the Rtpsession class.

After detecting a valid data source from the RTP session, you can then call the Rtpsession class's Getnextpacket () method to extract the RTP datagram, and when the received RTP datagram is processed, be sure to release it in a timely manner. The following code demonstrates how to handle a received RTP datagram:

if (Sess. Gotofirstsourcewithdata ()) {do
  {
    rtppacket *pack;                    
    Pack = Sess. Getnextpacket ();                    
    Processes the received data
    delete pack;
  } while (Sess. Gotonextsourcewithdata ());
}

Jrtplib defines three receive modes for RTP datagrams, each of which specifies which of the incoming RTP datagrams will be accepted and which incoming RTP datagrams will be rejected. The following receive modes can be set by calling the Setreceivemode () method of the Rtpsession class:

Receivemode_all The default receive mode, all incoming RTP datagrams will be accepted

, receivemode_ignoresome all incoming RTP datagrams will be accepted except for some specific senders.  The list of rejected senders can be set by calling the Addtoignorelist (), Deletefromignorelist (), and clearignorelist () methods;

Receivemode_acceptsome All incoming RTP datagrams are rejected except for certain senders, and the accepted Senders list can be set by calling the Addtoacceptlist (), Deletefromacceptlist, and Clearacceptlist () methods.
5. Control information

Jrtplib is a highly encapsulated RTP library, which is often not used by programmers to care about how RTCP datagrams are sent and received, as they can be done by jrtplib themselves. As long as the Polldata () or Sendpacket () method is successfully invoked, Jrtplib is able to automatically process the incoming RTCP datagram, and also sends the RTCP datagram when needed to ensure the correctness of the entire RTP session process.

On the other hand, by calling the Rtpsession class to provide setlocalname (), Setlocalemail (), Setlocallocation (), Setlocalphone (), Setlocaltool () and the Setlocalnote () method, Jrtplib also allows the programmer to set the control information for the RTP session. All of these methods are called with two parameters, where the first parameter is a char pointer pointing to the data that will be set, and the second argument is an int that indicates how many characters in front of the data will be used. For example, the following statement can be used to set the e-mail address in control information:

Sess. Setlocalemail ("xiaowp@linuxgam.comxiaowp@linuxgam.com", 19);

During RTP sessions, not all control information needs to be sent, by calling the Rtpsession class to provide enablesendname (), Enablesendemail (), Enablesendlocation (), The Enablesendphone (), Enablesendtool (), and Enablesendnote () methods allow you to select the control information that will be sent for the current RTP session. 6. Example

Finally, through a simple streaming media send-receive instance, describes how to use Jrtplib to do real-time streaming media programming. Listing 3 shows the complete code for the data sender, which is responsible for continuously sending RTP packets to the user-specified IP address and port:

#include <stdio.h> #include <string.h> #include "rtpsession.h"//error handling function void CheckError (int err) {if (err
    < 0) {char* errstr = rtpgeterrorstring (err);
    printf ("error:%s\\n", errstr);
  Exit (-1);
  }} int main (int argc, char** argv) {rtpsession sess;
  unsigned long destip;
  int destport;
  int portbase = 6000;
  int status, index;

  Char buffer[128];
    if (argc! = 3) {printf ("Usage:./sender destip destport\\n");
  return-1;
  }//Get the IP address and port number of the receiving end destip = inet_addr (argv[1]);
    if (Destip = = Inaddr_none) {printf ("Bad IP address specified.\\n");
  return-1;
  } Destip = Ntohl (Destip);

  Destport = Atoi (argv[2]); Create RTP Session status = Sess.
  Create (portbase);

  CheckError (status); Specifies the RTP data receive side status = Sess.
  Adddestination (Destip, Destport);

  CheckError (status); Set the RTP session default parameter Sess.
  Setdefaultpayloadtype (0); Sess.
  Setdefaultmark (FALSE); Sess.

  Setdefaulttimestampincrement (10);
  Send streaming Media data index = 1; do {SPRintf (buffer, "%D:RTP packet", index + +); Sess.
    Sendpacket (buffer, strlen (buffer));
  printf ("Send packet!\\n");

  } while (1);
return 0;
 }

Listing 4 shows the complete code for the data receiver, which is responsible for continuously reading the RTP packets from the specified port:

#include <stdio.h>
#include "rtpsession.h"
#include "rtppacket.h"

//error handling function
void CheckError ( int err)
{
  if (Err < 0) {
    char* errstr = rtpgeterrorstring (err);
    printf ("error:%s\\n", errstr);
    Exit ( -1);
  }
}

int main (int argc, char** argv)
{
  rtpsession sess;
  int localport;
  int status;

  if (argc! = 2) {
    printf ("Usage:./sender localport\\n");
    return-1;
  }

   Gets the user-specified port number
  LocalPort = atoi (argv[1]);

  Create RTP session
  status = Sess. Create (LocalPort);
  CheckError (status);

  do {
    //Accept RTP data
    status = Sess. Polldata ();
    Retrieves the RTP data source
    if (Sess. Gotofirstsourcewithdata ()) {do
      {
        rtppacket* packet;
        Gets the RTP datagram while
        (packet = Sess. Getnextpacket ()) = NULL) {
          printf ("Got packet!\\n");
          Delete the RTP datagram
          delete packet;
        }
      } while (Sess. Gotonextsourcewithdata ());
    }
  } while (1);

  return 0;
}

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.