RTPVoiceSender.java
上传用户:pdtewte
上传日期:2021-10-16
资源大小:13k
文件大小:4k
源码类别:

IP电话/视频会议

开发平台:

Java

  1. import   javax.media.*;   
  2. import   javax.media.format.*;   
  3. import   javax.media.rtp.*;   
  4. //import   javax.media.rtp.rtcp.*;   
  5. import   javax.media.protocol.*;   
  6. //import   javax.media.rtp.event.*;   
  7. import   javax.media.control.*;   
  8.   
  9. import   java.util.*;   
  10. //import   java.io.*;   
  11. import   java.net.*;   
  12.   
  13.    
  14. public class RTPVoiceSender 
  15. {
  16. private   Processor    mProcessor ;        
  17. private   DataSource   mDataSource;  
  18. private   String       destIP;
  19. private   int          destport;
  20. public RTPVoiceSender( String destIP,int port)
  21. {
  22.  this.destIP = destIP;
  23.  this.destport  = port;
  24.  
  25.  AudioFormat  format  = new   AudioFormat(AudioFormat.GSM_RTP,8000,8,1);   
  26.  Vector   devices =  CaptureDeviceManager.getDeviceList(format);  
  27.  CaptureDeviceInfo   di=   null;   
  28.   if ( devices.size() > 0 )   
  29.   {   
  30.      di = (CaptureDeviceInfo)devices.elementAt(0);
  31.      System.out.println("��ȡ��Ƶ�ɼ��豸�ɹ�!");
  32.   }   
  33.   else   
  34.   {   
  35.  di =  CaptureDeviceManager.getDevice("DirectSoundCapture");
  36.  System.out.println("��ȡ��Ƶ�ɼ��豸ʧ��!");
  37.   }
  38.   
  39.   try
  40.   {   
  41.             mProcessor   =   Manager.createProcessor(di.getLocator());   
  42.         }
  43.   catch (Exception e)  
  44.   {
  45.   System.out.println(e);
  46.         } 
  47.   
  48.   mProcessor.configure();
  49.   while( mProcessor.getState() < Processor.Configured )
  50.   {
  51.   
  52.   }
  53.   
  54.         mProcessor.setContentDescriptor(new   ContentDescriptor(   ContentDescriptor.RAW_RTP));   
  55.   
  56.         TrackControl   track[]   =   mProcessor.getTrackControls();   
  57.   
  58.         boolean   encodingOk   =   false;   
  59.         for   (int   i   =   0;   i   <   track.length;   i++)   
  60.         {   
  61.             if   (!encodingOk   &&   track[i]   instanceof   FormatControl)   
  62.             {   
  63.                 if   (((FormatControl)track[i]).setFormat(   new   AudioFormat(AudioFormat.GSM_RTP,8000,8,1))   ==   null)   
  64.                 {   
  65.                     track[i].setEnabled(false);   
  66.                 }   
  67.                 else  
  68.                 {   
  69.                     encodingOk  =  true;   
  70.                 }   
  71.             }   
  72.             else   
  73.             {   
  74.                 track[i].setEnabled(false);   
  75.             }   
  76.         }  
  77.         
  78.         mProcessor.realize();
  79.         while(mProcessor.getState()< Processor.Realized)
  80.         {
  81.          
  82.         }
  83.         
  84.         try   
  85.         {   
  86.             mDataSource   =   mProcessor.getDataOutput();   
  87.         }   catch   ( NotRealizedError  e)
  88.         {   
  89.              System.out.println(e); 
  90.         }   
  91.         
  92.         PushBufferDataSource pdbs = (PushBufferDataSource)mDataSource;
  93.         
  94.         PushBufferStream pbss[] = pdbs.getStreams();
  95.         
  96.         RTPManager rtpMgrs[] = new RTPManager[pbss.length];
  97.         
  98.         rtpMgrs[0] = RTPManager.newInstance();   
  99.         
  100.         try
  101.         {
  102.          InetAddress ipaddr = InetAddress.getByName(this.destIP);
  103.          SessionAddress localAddr = new SessionAddress(InetAddress.getLocalHost(),10002);
  104.          
  105.          //this.destport
  106.          SessionAddress destAddr = new SessionAddress(ipaddr,this.destport);
  107.          
  108.          rtpMgrs[0].initialize(localAddr);
  109.          
  110.          rtpMgrs[0].addTarget(destAddr);
  111.          
  112.          SendStream sendStream = rtpMgrs[0].createSendStream(mDataSource,0);
  113.          
  114.          sendStream.start();
  115.          
  116.          mProcessor.start();
  117.          
  118.         }
  119.         catch (Exception e)
  120.         {
  121.          System.out.println(e); 
  122.         }
  123.  
  124. }
  125. }