Write your own RTP server--About RTP protocol

Source: Internet
Author: User



This article will lead you through a simple RTP transport server, designed to understand the RTP streaming Media Transfer Protocol and some knowledge of multimedia codec.





The necessary knowledge about RTP protocol


To implement a protocol, of course, you first need to read the documentation for that protocol. The RTP protocol documentation has rfc1889, rfc1890, rfc3550, where rfc3550 is the current version and the other two are out of date versions. This agreement can be found on the IETF's website: http://tools.ietf.org/html/rfc3550


RTP Packet


RTP is based on the UDP protocol, and the RTP server passes the UDP protocol, typically sending an RTP packet each time. The client reads the data and then plays it by parsing the RTP packet.



The structure of the RTP packet is as follows:


    1. The head of the RTP HEADER:RTP packet
    2. Contributing sources: The number is 0-n, so it can be empty. Specific definition Reference rfc3550
    3. RTP payload: That is, the data to be transferred by RTP
RTP Header


This is the head of the RTP stream, search the RTP format on the net, will search many articles to introduce this head definition. Here we refer to the definition of rfc3550, in verse 5.1 (http://tools.ietf.org/html/rfc3550#section-5.1).


0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| v=2| p|  X| CC |     m|       PT | Sequence Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Timestamp |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+

| ....                              |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+


Each line is a count of bits, which gives you a visual view of the number of bits that each representation part occupies. Simply introduce:



V (Version): Version 2 BITS,RTP, here Unified for 2



P (padding): 1 bit, if set 1, is filled at the end of packet, padding is sometimes convenient for some packages for fixed-length algorithms



X (extension): 1 bit, if placed 1, the RTP header will follow a header extension






M (marker): 1 bit, specifically this definition will be in a profile



PT (playload type): 7 bits, indicating the type of multimedia being transmitted, the corresponding number is listed in another document RFC3551 (http://tools.ietf.org/html/rfc3551)



Sequence number:16 bits, sequence number per RTP packet is automatically added to the receiver to detect packet loss



Timestamp:32 bits, timestamp



Ssrc:32 bits, the ID of the synchronization source, the ID of no two sync sources cannot be the same








Some of the concepts above are some of the necessary knowledge to implement an RTP server. The introduction is very brief, the detailed definition still should refer to rfc3550 original text.


Hands-on practice


Since we already know the structure of the RTP packet, is this the same structure as the RTP stream we used before? How to verify it? Next, we'll step through the structure of the RTP stream.



We know that RTP is based on UDP, so let's start with a simple UDP receiver to see what information we can accept from the RTP server. To achieve this, you need to have a certain degree of network programming experience, as to the specific operating system, programming environment, development language, etc. are not limited. For simplicity, I'm using Python to give you a small example of the program.





[Python]View Plaincopy
  1. Import socket
  2. # Build a socket to receive data from RTP server.
  3. # Here we use SOCK_DGRAM, because RTP are on UDP.
  4. Sock = Socket.socket (socket.af_inet, socket. SOCK_DGRAM)
  5. Sock.bind (("localhost", 6666))
  6. For I in Range (5):
  7. # We just get bytes to analyze the RTP Header.
  8. BUF = SOCK.RECV (+)
  9. # Output The result in octal.
  10. For C in buf:
  11. print "%x"% ord (c),
  12. Print
  13. Sock.close ()



This is the acceptance procedure, very short, and with a simple comment, which is not explained here.






The receiving end is ready, so where to find the RTP server as the sending side? You can use some of the tools to build a streaming media server, I choose the powerful VLC here. For VLC to build a streaming media server, please refer to my previous article based on mobile platform multimedia framework-using VLC to build a simple streaming media server. Here need to pay attention to a few configuration places, one is to choose destination time to choose RTP instead of RTSP, and then address can be filled in the local IP address or directly write localhost, port number to fill in the same with the receiver, here is 6666. The string after configuration should resemble the following:



: sout= #rtp {dst=localhost,port=6666,mux=ts}: no-sout-rtp-sap:no-sout-standard-sap:ttl=1



After the server configuration is complete, start the stream. At this point open the receiving end, will receive some data, I received the data at the beginning is:



8c CF 3c 74 47 0 44 10 A1
8c CF 4b 74 47 40 42 36 A1
A1 8c CF 7d (0) 1a
A1 8c CF BA (i) 1a
A1 8c CF (c) 0 1b
This is the hexadecimal representation. We interpret it according to the format of the header above:
The first byte 80 indicates:



V (version) =2



P (padding) =0



X (extension) =0






A second byte, A1, says:



M (marker) =1



PT (playload type) =33 (control rfc3551 can be found, 33 means mp2t AV, exactly the type of format we use with VLC stream)



The back of the 2bytes sequence number we can intuitively see is in addition, 4bytes timestamp is also in constant increments. After the next 74 is SSRC ID, because CC is 0, so there is no CCRC. The next few are the data that RTP is going to transmit.


Summarize


Familiarity with the RTP protocol is the basis for achieving it. Here I just do a simple introduction, need to learn more, read the official document is an essential step.



By writing a small program to print out the specific data in the RTP stream, there is no direct help to implement the RTP server. But it allows you to be more familiar with the protocol itself and the programming environment, and also facilitates debugging later in the implementation process. No matter what language you are in, it is highly recommended to write a small program like this.



Write your own RTP server--About RTP protocol


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.