Packaging h264 video RTP Based on rfc3984 Protocol

Source: Internet
Author: User

Http://bbs.chinavideo.org/viewthread.php? Tid = 7575

I believe many of you want to play h264 video streaming media like me. However, a newbie often does not know where to start. Using Baidu, Google, and other search materials is a treasure. After N weeks of thinking, I made some achievements. It took a lot of useless effort. I spent a week watching the English protocol, and later I learned that there was a Chinese version, in addition, the goal I have achieved is very simple, as long as the VLC can be played in real time, there is no need to understand the entire protocol. I also hope that I can directly find a set of code, but I still have to do it myself. Now I am posting the code, hoping to help my friends who are doing similar projects.
I. The sample code is packaged and sent to port 1234 of the Local Machine for the standard h264 code stream using RTP on my computer. The VLC player can receive the code stream from Port 1234 and play it back in real time. The code is accompanied by detailed comments and should be easy to understand (the premise is that you have a little understanding of rfc3550 rfc3984 protocol ).
II. The sample code in this example is that only RTP is packaged according to rfc3984, and RTCP is not sent yet. The reason is to reference this person's words: "1. there are a lot of statistics on the short interval of RTCP sending and RTP information in RTCP. This operation is not difficult, but annoying. I don't want to write it. 2. When RTCP and RTP came out at the beginning, they were not for video-on-demand applications, but for video conferences. RTCP manages the meanings of participants. This function is not used in many scenarios. 3. I want to make it simple. I didn't write synchronization between multiple streams, such as a video or audio stream of a video. These are actually completed by RTCP. I am too lazy to write, because these functions are very good for all RTP Library classes (such as jrtplib library. I think the biggest advantage of using a database is here ".
3. for code-related principles, you should check rfc3550 and rfc3984. these two protocols have both translated Chinese versions by enthusiastic netizens. I put them in the compressed package, so you don't have to get tired of searching and registering to download them. For the sake of convenience, I think it is enough to look at the rfc3984 summarized by this netizen. The URL is http://www.cppblog.com/czanyou/archive/2009/12/25/67940.html. If you cannot open the webpage, go to the data folder in the compressed package to find it. I have saved the webpage.
4. The code is not completely original to me, but is modified based on the Code searched by netizens. Thanks to the following netizens:
1. the e on the head of the CAT (his blog address http://blog.csdn.net/Tinnal/archive/2008/09/03/2871734.aspx) in his blog I found something valuable for the first time, in addition, the source code of the RTP package provided by him for free can be copied to build a project to realize the MPEG streaming media, which is of great inspiration to me.
2. liming, the Code provided by Liming has implemented the code stream analysis of h264, separated each NALU unit, and analyzed the NALU type, length, and other information. It provides great convenience for me to implement RTP packaging. In fact, this sample code adds the RTP packaging part to his code, and I have not changed the project name. The source code is here
3. luny, the SDP file provided by him helped me a lot in key times. The RTP data packet I sent has always been correct through the wireshark packet capture tool, but the VLC player has no response. After downloading the SDP file, the screen is displayed. A netizen said that VLC can only open the RTP code stream through ts packets or SDP files for h264. I am so skeptical here.
4. jessiepan and his post, Workshop.
Usage: Open the project on vc6 and compile it. (Note that you should change the IP address to your own. In h264.h, modify the # define dest_ip "192.168.0.30" and # define dest_port 1234 lines. At the same time, the W. SDP file should be changed to the same IP address and port number, otherwise the VLC will not accept the data. In the c = in ip4 192.168.0.30 and M = video 1234 RTP/AVP 96 rows. 1234 in the middle is the port number I set .) Before executing the program, use VLC to open the W. SDP file, and then execute the program to view the screen :). Similarly, Versions later than vlc1.0 do not support direct opening of H.264 video files, but Versions later than 264 support. Here, I tested that both VLC versions 1.03 and 0.97 can accept and play the H.264 RTP code stream.
I still have a few questions, but I hope you can help me solve them after reading this post:
1. Set the timestamp. Rfc3984 does not mention how to calculate the timestamp. I also set the timestamp according to various gossip messages. Unsigned int timestamp_increse = 0; timestamp_increse = (unsigned INT) (90000.0/framerate); that is, the initial value is set to 0, the timestamp increment is set to 90000.0/framerate, and framerate is set to 25, that is, 25 frames per second. Each time a NALU unit is sent, the timestamp is increased. If the NALU is greater than 1400 bytes, Multiple shards have the same timestamp when the sharding is required. Whether the settings are correct. Please give me an authoritative answer.
2. according to my understanding, the SDP file only tells the vlc which IP address and port accept the accept RTP package. Similarly, I open the network stream through media-> In the VLC, select RTP for the protocol, and enter the IP address and port number. Why can this problem be received by opening the SDP file? However, the latter VLC does not respond.
3. when I set the frame rate to 25 (float framerate = 25; in the Code), the VLC can accept the code stream, but it will be relatively slow, often buffered, and the error is "main error: es_out_set _ (group _) PCR is called too late, increasing pts_delay to 339 Ms. I suspect that the UDP packet sent by my computer is not enough to play 25 frames per second, so I added a = framerate to the SDP file: 15 to restrict the player to play 15 frames per second. At the same time, the corresponding row float framerate in the Code is equal to 15. Changing the frame rate to 15 also solves the problem of the card, but the video playing is very slow. If I want to play 25 frames per second, can I only switch to another computer?
5. In the next step, I want to use jrtplib to package RTP. It is very convenient to use this library to implement RTCP. Will this library automatically send RTCP information based on network conditions. If any expert has the Code or the Code that implements the RTSP, he or she hopes to communicate with each other, even some code or some functions.

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.