精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
锐英源精品开源心得,转载请注明:“锐英源www.wisestudy.cn,孙老师作品,电话13803810136。微信号ryysoft”需要全文内容或开源服务请联系孙老师。
In this article, I will describe the architecture of RTP – Real Time Transport Protocol, and discuss the RTP managed classes for the Microsoft Conference XP Project to multicast JPEG images. For more examples of RTP programming, see www.SocketCoder.com.RTP协议的架构,讨论微软会议XP项目的RTP托管类用于多播JPEG图片。
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 applications: 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关联的配置和负荷格式比较重要。RTP的目的是提供有用的实时媒体数据的传输,比如音频和视频,基于IP网络。它包含定时服务、损失检测和校正、负荷和源标识、接收质量回馈、媒体同步和成员管理。RTP起初设计用于多播会议系统,使用轻量事务模式。在过去,它证明有用于一些应用:H.323视频会议、web广播和TV分发;在有线和无线环境下都有。协议描述适用于点对点使用的扩展,支持成千上万的用户的多播事务体验。
How does RTP work?RTP如何工作
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 the figure below; implementations sometimes perform the operations in a different order depending on their needs.发送方负责捕获和转码数据,生成RTP数据包。它有可能参与错误校正和拥塞控制,这是通过调整传输媒体流,它也是接收反馈的结果。桢会加载到RTP包里,准备好发送。如果桢大,会碎片化到多个RTP包里;如果小,几个桢会捆绑到一个RTP包里。依赖于错误校正方案,通道编码可能用于生成错误校正包或在发送前重新排序包。
In my next article, I will provide more info about the RTP architecture to transmit audio and video. For more information about RTP, see the following links:
Microsoft has implemented the RTP in its Conference XP project. The following diagram illustrates the architecture design of the RTP in Conference XP 3.0:微软已经在它的会议XP项目里实现了RTP。下图描述了会议XP里的RTP类图架构。
These steps will give you a brief of how to use the RTP on your multicasting projects:下面步骤说明了如何使用:
We have first hook some RTP events to communicate for what is happening in the RTP API process. These are:先剖析些RTP事件,这些事件在API执行流程中发生:
// 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);
Examples of using the above event 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); 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 to join the RTP session, we have to specify the type of the RTP packet payload. Each session can contain only payload type:接着是加入RTP事务,必须指定RTP包负荷类型。每个事务只能包含一个负荷类型:
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 joining the session, we can get the RTP stream buffer, as below:最后,在加入事务后,我们能获取RTP流缓冲
private void FrameReceived(object sender, RtpStream.FrameReceivedEventArgs ea) { System.IO.MemoryStream ms = new MemoryStream(ea.Frame.Buffer); pictureBox_Receive.Image = Image.FromStream(ms); }
And, in the RTPListener:RTPListener负责:
Use the RTPSender as shown below:RTPSender使用方法如下:
RtpSender rtpSender; MemoryStream ms = new MemoryStream(); // Compressed the captured image as JPEG image format pictureBox_sender.Image.Save(ms, ImageFormat.Jpeg); // Send The The Comressed Image as Bytes stream rtpSender.Send(ms.GetBuffer());
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). The RTCP Sender is used for outgoing RTCP data where:
RtcpListener is used to:
Used to keep track of interesting network statistics, such as bytes, packets, frames per second, lost bytes, and recovered bytes. All of these are contained in the RTP API class properties.用于跟踪关注的网络统计结果,比如字节数、包数、每秒桢数、丢失字节和恢复字节。所有这些在RTP API类属性里都可以找到。
To increase the performance in my example, I used a mechanism to send just only the new screen-captured image that is different than the previous captured image. This will reduce the usage of the network resources so I compare the pixels of the image before sending it, and to speed up the comparison, I resize the image to 100X100 and then I compare it pixel by pixel, as shown below:为了增强性能,我用算法来发送只需要发送的新捕获图像,新图像会和旧的不一样。这会降低网络使用率,所以在发送前我比较了像素,加速了比较,我修改图片大小为100X100,接着进行了比较:
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; 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=100;} return percent; }
The above method will calculate the difference pixels in the new captured image so we can decide to send it or not depending on the returned difference percentage.上面的方法会计算新捕获图片里的不同的像素,用它就可以决定不一样比例,进而决定是否发送。