锐英源软件
第一信赖

精通

英语

开源

擅长

开发

培训

胸怀四海 

第一信赖

当前位置:锐英源 / 开源技术 / 流媒体开源技术 / 怎样使用.NET的托管RTP API来创建多播系统
讨论组翻译
怎样用LSR.NET.RTP接收从VoIP UA发来的RTP包
服务方向
人工智能数据处理
人工智能培训
kaldi数据准备
小语种语音识别
语音识别标注
语音识别系统
语音识别转文字
kaldi开发技术服务
软件开发
运动控制卡上位机
机械加工软件
软件开发培训
Java 安卓移动开发
VC++
C#软件
汇编和破解
驱动开发
联系方式
固话:0371-63888850
手机:138-0381-0136
Q Q:396806883
微信:ryysoft

锐英源精品开源心得,转载请注明:“锐英源www.wisestudy.cn,孙老师作品,电话13803810136。微信号ryysoft”需要全文内容或开源服务请联系孙老师。

怎样使用.NET的托管RTP API来创建多播系统

RTP多播效果示意

Introduction介绍

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分发;在有线和无线环境下都有。协议描述适用于点对点使用的扩展,支持成千上万的用户的多播事务体验。

Background背景

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包里。依赖于错误校正方案,通道编码可能用于生成错误校正包或在发送前重新排序包。

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:

Using the code使用代码

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类图架构。

RTP过程

These steps will give you a brief of how to use the RTP on your multicasting projects:下面步骤说明了如何使用:

  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 a 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 of 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:使用RtpSession和RtpParticipant:事务由一组参与者组成,参与者用RTP协议互相通信。参与者可能激活了多个RTP事务--比如,一个事务交换音频数据,另外事务交易视频数据。
    • Manages all of the RTP objects and data.管理所有RTP对象和数据
    • Hold information about a user.容纳用户信息
    • To send or receive RTP data.发收RTP数据

    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);
      }
    • Using RtpSender and RtpListener: The sender is responsible for sending the captured data and generating RTP packets - the data can be from live capturing or from a file - compressing it for transmission. For example, converting 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 is used for:.使用RtpSender和RtpListener:发送方负责发送捕获数据和生成RTP包-数据从实时捕获或文件中获取-压缩后传输。比如,转换一个位图图片到JPEG压缩图片,这是我在例子里用的方法。发送方从读取媒体数据开始,比如视频桢,到缓冲里,此原始数据再编码为编码桢。在托管RTP库里,RtpSender用于:
        • Sending data across the network通过网络发送数据
      • Send with or without data being forward error corrected.带或不带后续错误校正数据

      And, in the RTPListener:RTPListener负责:

      • One thread receives data off the network.一个线程接收网络数据
      • One thread distributes packets to the appropriate stream for processing.一个线程分发桢到合适的流来处理

      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:

      • Local participants and streams are joining or leaving.
      • Sender and receiver reports for network status.

      RtcpListener is used to:

      • Processes incoming RTCP (RTP Control Protocol) data.
      • Participants and streams join or leave.
      • Sender and receiver reports for network status.
    • Performance Counters性能计数

      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类属性里都可以找到。

      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 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.上面的方法会计算新捕获图片里的不同的像素,用它就可以决定不一样比例,进而决定是否发送。

友情链接
版权所有 Copyright(c)2004-2021 锐英源软件
公司注册号:410105000449586 豫ICP备08007559号 最佳分辨率 1024*768
地址:郑州大学北校区院(文化路97号院)内