logo资料库

RTP协议编程.pdf

第1页 / 共7页
第2页 / 共7页
第3页 / 共7页
第4页 / 共7页
第5页 / 共7页
第6页 / 共7页
第7页 / 共7页
资料共7页,全文预览结束
RTP Programming Part 1 [FADI ABDELQADER – FADI@FADIDOTNET.ORG] How to use the managed RTP API classes in .NET to create your  multicasting systems   Part 1 Create an RTP multicasting Presenter (With Motion Detection)    Introduction:  In this article I will describe the Architecture of RTP –Real Time Transport Protocol and discuss using the RTP managed classes for the Microsoft Conference XP Project to multicast a JPEG Images. The key standard for data audio/video transport in IP networks is the Real‐time Transport Protocol (RTP), along with its associated profiles and payload formats. RTP aims to provide services useful for the transport of real‐time media, such as audio and video, over IP networks. These services include timing recovery, loss detection and correction, payload and source identification, reception quality feedback, media synchronization, and membership management. RTP was originally designed for use in multicast conferences, using the lightweight sessions model. Since that time, it has proven useful for a range of other applicaons: in H.323 video conferencing, webcasting, and TV distribution; and in both wired and cellular telephony. The protocol has been demonstrated to scale from point‐to‐point use to multicast sessions with thousands of users.
RTP Programming Part 1 How does RTP work?  [FADI ABDELQADER – FADI@FADIDOTNET.ORG] A sender is responsible for capturing and transforming audiovisual data for transmission, as well as for generating RTP packets. It may also participate in error correction and congestion control by adapting the transmitted media stream in response to receiver feedback. the frames will be loaded into RTP packets, ready for sending. If frames are large, they may be fragmented into several RTP packets; if they are small, several frames may be bundled into a single RTP packet. Depending on the error correction scheme in use, a channel coder may be used to generate error correction packets or to reorder packets before transmission. After the RTP packets have been sent, the buffered media data corresponding to those packets is eventually freed. The sender must not discard data that might be needed for error correction or for the encoding process. This requirement may mean that the sender must buffer data for some time after the corresponding packets have been sent, depending on the codec and error correction scheme used. The sender is responsible for generating periodic status reports for the media streams it is generating, including those required for lip synchronization. It also receives reception quality feedback from other participants and may use that information to adapt its transmission. A receiver is responsible for collecting RTP packets from the network, correcting any losses, recovering the timing, decompressing the media, and presenting the result to the user. It also sends reception quality feedback, allowing the sender to adapt the transmission to the receiver, and it maintains a database of participants in the session. A possible block diagram for the receiving process is shown in below figure; implementations sometimes perform the operations in a different order depending on their needs.
RTP Programming Part 1 [FADI ABDELQADER – FADI@FADIDOTNET.ORG] In the Next Article I will describe more info about RTP architecture to transmitting audio and video, to see my examples in this field you can see this link in my site http://www.fadidotnet.org/online_book/Network_Programming_online.htm For more information about RTP, see the following resources: • http://www.ietf.org/rfc/rfc3550.txt • http://www.faqs.org/rfcs/rfc3550.html • Perkins, Colin. RTP:Audio and Video for the Internet. Addison-Wesley. RTP API Programming:  Microsoft has been implemented the RTP in its Conference XP Project. The following diagram illustrates the architecture design of the RTP in Conference XP 3.0:
RTP Programming Part 1 [FADI ABDELQADER – FADI@FADIDOTNET.ORG] These steps will give you a brief to use RTP with your multicasting applications: 1- Using RtpSession and RtpParticipant A session consists of a group of participants who are communicating using RTP. A participant may be active in multiple RTP sessions—for instance, one session for exchanging audio data and another session for exchanging video data. For each participant, the session is identified by a network address and port pair to which data should be sent, and a port pair on which data is received. The send and receive ports may be the same. Each port pair comprises two adjacent ports: an even‐ numbered port for RTP data packets, and the next higher (odd‐numbered) port for RTCP control packets. The default port pair is 5004 and 5005 for UDP/IP, but many applications dynamically allocate ports during session setup and ignore the default. RTP sessions are designed to transport a single type of media; in a multimedia communication, each media type should be carried in a separate RTP session We will use the RtpSession and RtpParticipant classes to: • Manages all of the RTP objects and data. • Holds information about a user. • Required to send or receive RTP data. To use it we have first to hook some RTP events to communicate for what is happening in the RTP API process these are: // Manage the join to the session Ex.Add/Remove a User To/From the RTP Session RtpEvents.RtpParticipantAdded += new RtpEvents.RtpParticipantAddedEventHandler(RtpParticipantAdded); RtpEvents.RtpParticipantRemoved += new RtpEvents.RtpParticipantRemovedEventHandler(RtpParticipantRemoved); Example to use the above events declarations: private void RtpParticipantAdded(object sender, RtpEvents.RtpParticipantEventArgs ea) { MessageBox.Show (ea.RtpParticipant.Name + " has joined to the session"); } private void RtpParticipantRemoved(object sender, RtpEvents.RtpParticipantEventArgs ea) { MessageBox.Show(ea.RtpParticipant.Name + " has left the session"); } // Manage (Add/Remove)Sessions Ex.Activeate multiple RTP sessions RtpEvents.RtpStreamAdded += new RtpEvents.RtpStreamAddedEventHandler(RtpStreamAdded); RtpEvents.RtpStreamRemoved += new RtpEvents.RtpStreamRemovedEventHandler(RtpStreamRemoved);
RTP Programming Part 1 [FADI ABDELQADER – FADI@FADIDOTNET.ORG] Example to use the above events declaration: private void RtpStreamAdded(object sender, RtpEvents.RtpStreamEventArgs ea) { ea.RtpStream.FrameReceived += new RtpStream.FrameReceivedEventHandler(FrameReceived); } private void RtpStreamRemoved(object sender, RtpEvents.RtpStreamEventArgs ea) { ea.RtpStream.FrameReceived -= new RtpStream.FrameReceivedEventHandler(FrameReceived); } Then joined to the RTP Session, we have to specific the type of the RTP packet payload. Each session can contains only one type of payload type: RtpSession rtpSession; private void JoinRtpSession(string SessionName,string name) { rtpSession = new RtpSession(ep, new RtpParticipant(SessionName, name), true, true); rtpSender = rtpSession.CreateRtpSenderFec(name, PayloadType.JPEG, null, 0, 200); } private void LeaveRtpSession() { // Clean up all outstanding objects owned by the RtpSession rtpSession.Dispose(); } Finally after joined to the session we can get the RTP stream buffer as below: private void FrameReceived(object sender, RtpStream.FrameReceivedEventArgs ea) { System.IO.MemoryStream ms = new MemoryStream(ea.Frame.Buffer); pictureBox_Receive.Image = Image.FromStream(ms); } 2- Using RtpSender and RtpListener The sender is responsible for sending the captured data and generating RTP packets, whether data can be from live capturing or from a file, compressing it for transmission for Example convert a bitmap image to a JPEG compressed image as I used in my example, The sender starts by reading media data such as video frames—into a buffer from which encoded frames are produced. In The Managed RTP Library the RtpSender Used For: • Sending data across the network. • Can send with or without data being forward error corrected. And the RTPListener is used for: • One thread receives data off the network.
RTP Programming Part 1 [FADI ABDELQADER – FADI@FADIDOTNET.ORG] • One thread distributes packets to the appropriate stream for processing. To Use RTPSender as Below: RtpSender rtpSender; MemoryStream ms = new MemoryStream(); pictureBox_sender.Image.Save(ms, ImageFormat.Jpeg); // Compressed the captured image as JPEG image format rtpSender.Send(ms.GetBuffer()); // Send The The Comressed Image as Bytes stream RTCP Management: RTCP packets are defined in the RTP specification: receiver report (RR), sender report (SR), source description (SDES), membership management (BYE), and application-defined (APP). RTCP Sender used for outgoing RTCP data to: Local participants and streams joining or leaving. • • Sender and receiver reports for network status RtcpListener Used For: • Processes incoming RTCP (RTP Control Protocol) data: • Participants and streams joining or leaving. • Sender and receiver reports for network status. 3- Performance Counters • Used to keep track of interesting network statistics, such as bytes; packets; frames per second; lost bytes; packets; frames; and recovered bytes, packets, and frames, all of these are contain in the RTP API Classes properties. Increase Network usage performance: To increase the performance in my example I used a mechanism to send just only the new screen captured image that is deference with the previous captured image. This will reduce the usage of the network resources so I compare the pixels of the image before send it and to speed comparing I resized the image to 100X100 and then I compare it pixel by pixel as below: public float difference(Image OrginalImage, Image SecoundImage) { float percent = 0; try { float counter = 0; Bitmap bt1 = new Bitmap(OrginalImage); Bitmap bt2 = new Bitmap(SecoundImage); int size_H = bt1.Size.Height;
RTP Programming Part 1 [FADI ABDELQADER – FADI@FADIDOTNET.ORG] int size_W = bt1.Size.Width; float total = size_H * size_W; Color pixel_image1; Color pixel_image2; for (int x = 0; x != size_W; x++) { for (int y = 0; y != size_H; y++) { pixel_image1 = bt1.GetPixel(x, y); pixel_image2 = bt2.GetPixel(x, y); if (pixel_image1 != pixel_image2) { counter++; } } } percent = (counter / total) * 100; } catch (Exception) { percent = 0; } return percent; } The above method will calculate the deference pixels in the new captured image so  we will decide to send it or not depend on the returned percentage.    References:  1‐ Microsoft Conference XP Project www.conferencexp.net  2‐ RTP: Audio and Video for the Internet, Addison Wesley 2003  3‐ The Complete reference in network programming, FADI Abdelqader (under  writing) See: www.fadidotnet.org and also www.fadidotnet.org/vb  4‐ hp://www.ie.org/rfc/rfc3550.txt       
分享到:
收藏