Getting Started with AV data processing: UDP-RTP Protocol resolution

Source: Internet
Author: User
Tags socket error flv file

=====================================================

Audio-visual data Processing Primer series articles:

Getting started with visual audio data processing: RGB, YUV pixel data processing

Getting Started with AV data processing: PCM Audio sampling data processing

Getting Started with AV data processing: Analysis of video stream in H.

Getting Started with AV data processing: AAC audio bitstream parsing

Getting Started with AV data processing: FLV Encapsulation Format parsing

Getting Started with AV data processing: UDP-RTP Protocol resolution

=====================================================


This article describes the processing procedures for network protocol data. The location of the network protocol data in the video player is as follows.


The program in this article is a UDP/RTP protocol streaming media data resolver. The program can analyze the contents of the RTP header in the UDP protocol and the information in the Mpeg-ts encapsulation format in the RTP payload. By modifying this program, different data processing functions of UDP/RTP protocol can be realized.

Principle

The MPEG-TS package format data is packaged as a RTP/UDP protocol and then sent out as shown in the process. The figure first packages each of the 7 Mpeg-ts packet as one RTP, and then each RTP is packaged as a single UDP. The way to package RTP is to precede the mpeg-ts data with the RTP header, and the way to package RTP is to precede the RTP data with the UDP header.


The knowledge about Mpeg-ts, RTP, and UDP is no longer detailed, and you can refer to the relevant documentation for details about the information. The program documented in this article is a program that collects streaming media, so the process of this program and the process of sending mpeg-ts are exactly the opposite. The program can receive UDP packets through socket programming, parse the information of the RTP packets, and then parse the MPEG-TS packet information in the RTP packet.

The code throughout the program is in the Simplest_udp_parser () function, as shown below.
/** * Simplest audio-visual data processing example * Simplest mediadata Test * * Lei hua Lei Xiaohua * [email protected] * Communication University/Digital TV Technology * Communication University of China/digital TV technology * http://blog.csdn.net/leixiaohua1020 * * This project contains the following examples of audio and visual testing: * (1) pixel data processing program. Functions that contain RGB and YUV pixel format processing. * (2) Audio sampling data processing program. A function that contains the processing of PCM audio sampling format. * (3) The Code stream analysis program. Nalu can be detached and parsed. * (4) AAC Code Stream analysis program. Adts frames can be detached and parsed. * (5) FLV Encapsulation Format analysis program. You can separate the MP3 audio stream from the FLV. * (6) UDP-RTP Protocol analysis program. You can analyze the udp/rtp/mpeg-ts packets. * * This project contains following samples to handling multimedia data: * (1) Video pixel data handling program. It contains several examples to handle RGB and YUV data. * (2) Audio Sample data Handling program. It contains several examples to handle PCM data. * (3) H. Stream Analysis program. It can parse H. bitstream and analysis Nalu of stream. * (4) AAC Stream analysis program. It can parse AAC bitstream and analysis ADTS frame of stream. * (5) FLV format analysis program. It can analysis FLV file and extract MP3 audio stream. * (6) UDP-RTP Protocol Analysis Program. It can analysis Udp/rtp/mpeg-ts Packet. * */#include <stdio.h> #include <winsock2.h> #pragma comment (lib, "Ws2_32.lib") #pragma pack (1)/* * [Memo] FFmpeg Stream Command: * ffmpeg-re-i sintel.ts-f mpegts udp://127.0.0.1:8880 * ffmpeg-re-i sintel.ts-f Rtp_mpegts UD       p://127.0.0.1:8880 */typedef struct rtp_fixed_header{/* byte 0 */unsigned char Csrc_len:4;      /* Expect 0 */unsigned char extension:1;        /* Expect 1 */unsigned char padding:1;        /* Expect 0 */unsigned char Version:2;        /* Expect 2 *//* byte 1 */unsigned char payload:7;unsigned char marker:1;            /* Expect 1 *//* bytes 2, 3 */unsigned short seq_no;        /* Bytes 4-7 */unsigned long timestamp;            /* bytes 8-11 */unsigned long ssrc; /* Stream number is used here. */} rtp_fixed_header;typedef struct Mpegts_fixed_header {unsigned sync_byte:8; unsigned transport_error_indicator:1; unsigned payload_unit_start_indicator:1;unsigned transport_priOrity:1; unsigned pid:13;unsigned scrambling_control:2;unsigned adaptation_field_exist:2;unsigned continuity_counter:4;} Mpegts_fixed_header;int simplest_udp_parser (int port) {wsadata wsadata; WORD sockversion = Makeword (2,2); int cnt=0;//file *myout=fopen ("Output_log.txt", "wb+"); FILE *myout=stdout; FILE *fp1=fopen ("Output_dump.ts", "wb+"); if (WSAStartup (sockversion, &wsadata)! = 0) {return 0;} Socket sersocket = socket (af_inet, SOCK_DGRAM, IPPROTO_UDP); if (Sersocket = = Invalid_socket) {printf ("SOCKET error!"); return 0;} sockaddr_in seraddr;seraddr.sin_family = Af_inet;seraddr.sin_port = htons (port); seraddr.sin_addr. S_un. S_ADDR = Inaddr_any;if (Bind (Sersocket, (SOCKADDR *) &seraddr, sizeof (seraddr)) = = Socket_error) {printf ("Bind ERROR!") ; closesocket (sersocket); return 0;} sockaddr_in remoteaddr;int naddrlen = sizeof (REMOTEADDR);  How to Parse?int parse_rtp=1;int parse_mpegts=1;printf ("Listening on port%d\n", port); Char recvdata[10000]; while (1) {int pktsize = recvfrom (Sersocket, recVData, 10000, 0, (SOCKADDR *) &remoteaddr, &naddrlen); if (Pktsize > 0) {//printf ("addr:%s\r\n", Inet_ntoa ( REMOTEADDR.SIN_ADDR));//printf ("Packet size:%d\r\n", pktsize);//parse rtp//if (parse_rtp!=0) {char payload_str[10]={ 0}; Rtp_fixed_header rtp_header;int rtp_header_size=sizeof (rtp_fixed_header);//RTP headermemcpy ((void *) &AMP;RTP_ header,recvdata,rtp_header_size);//rfc3351char Payload=rtp_header.payload;switch (payload) {case 0:case 1:case 2: case 3:case 4:case 5:case 6:case 7:case 8:case 9:case 10:case 11:case 12:case 13:case 14:case 15:case 16:case 17:case 18: sprintf (Payload_str, "Audio"), Break;case 31:sprintf (payload_str, "h.261"); break;case 32:sprintf (Payload_str, "MPV") ; Break;case 33:sprintf (Payload_str, "mp2t"); break;case 34:sprintf (Payload_str, "h.263"); Break;case 96:sprintf ( Payload_str, "H."), break;default:sprintf (Payload_str, "other"); unsigned int timestamp=ntohl (rtp_header.timestamp); unsigned int seq_no=ntohs (RTP_HEADER.SEQ_NO); fprintf (Myout, "[ RTP Pkt]%5d| %5s| %10u| %5d| %5d|\n ", cnt,payload_str,timestamp,seq_no,pktsize);//RTP Datachar *rtp_data=recvdata+rtp_header_size;int Rtp_data_ Size=pktsize-rtp_header_size;fwrite (RTP_DATA,RTP_DATA_SIZE,1,FP1);//parse Mpegtsif (parse_mpegts!=0&& payload==33) {mpegts_fixed_header mpegts_header;for (int i=0;i<rtp_data_size;i=i+188) {if (rtp_data[i]!=0x47)   Break;//mpegts header//memcpy ((void *) &mpegts_header,rtp_data+i,sizeof (Mpegts_fixed_header)); fprintf (Myout, " [Mpegts pkt]\n ");}}} else{fprintf (Myout, "[UDP Pkt]%5d|%5d|\n", cnt,pktsize); fwrite (RECVDATA,PKTSIZE,1,FP1);} cnt++;}} Closesocket (Sersocket); WSACleanup (); fclose (FP1); return 0;}

The function call method above is shown below.
Simplest_udp_parser (8880);

Results

This program input is a port number of this machine, the output is the analytic result of Udp/rtp/mpeg-ts. Once the program has started running, you can use the push stream software to push the udp://127.0.0.1:8880 address of the native. For example, you can use the streaming feature in the Open Media dialog box of VLC Media Player (in the menu of the small triangle button next to the play button). Add a new target for "Rtp/mpeg Transport Stream" in the dialog box for this feature.


You can also use FFmpeg to push the 8880 port of this machine. The following command can push the mpeg-ts of the UDP package.

Ffmpeg-re-i sintel.ts-f Mpegts udp://127.0.0.1:8880

The following command can push the stream first through the RTP package and then through the UDP encapsulated mpeg-ts.

Ffmpeg-re-i sintel.ts-f Rtp_mpegts udp://127.0.0.1:8880

After the stream is pushed, the program in this article receives the UDP packet through the socket and parses the data. The results of the parsing are as shown.



Download
simplest mediadata test


Project Home

sourceforge:https://sourceforge.net/projects/simplest-mediadata-test/

Github:https://github.com/leixiaohua1020/simplest_mediadata_test

Open source China: http://git.oschina.net/leixiaohua1020/simplest_mediadata_test


csdn:http://download.csdn.net/detail/leixiaohua1020/9422409


This project contains several examples of audio and visual data parsing:
(1) Pixel data processing program. Functions that contain RGB and YUV pixel format processing.
(2) Audio sampling data processing program. A function that contains the processing of PCM audio sampling format.
(3) The Code Stream analysis program for H. Nalu can be detached and parsed.
(4) AAC Stream Analysis Program. Adts frames can be detached and parsed.
(5) FLV Encapsulation Format parser. You can separate the MP3 audio stream from the FLV.
(6) UDP-RTP Protocol Analysis Program. You can analyze the udp/rtp/mpeg-ts packets.





Rai (Lei Xiaohua)
[Email protected]
http://blog.csdn.net/leixiaohua1020




Getting Started with AV data processing: UDP-RTP Protocol resolution

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.