JMF(视频传输)

//转自: 陈开源的专栏
/**/ /*在coogo上面看到有人在做项目,是多人视频聊天系统。要我做的感觉是很麻烦的一块。视频传输。我是还没有接触过jmf啊。一开始的时候就是只能捕获摄像头,怎么都不能传输,网络上面这方面的资料也比较上,找了很就都没有找到。没有办法,只有到sun公司去看JMF 2.1.1 Solutions。看了半天的例子,终于算是拼凑出来了。现在的问题就是可以传输,但是本地的影像却看不见了,只能看见远端的。*/

代码如下:

package  jmfsample;


import  java.io. * ;
import  javax.media. * ;
import  javax.media.util. * ;
import  javax.media.format. * ;
import  javax.media.control. * ;
import  javax.media.control.TrackControl;
import  javax.media.protocol.DataSource;
import  javax.media.protocol.ContentDescriptor;
import  javax.media.protocol.PushBufferDataSource;
import  javax.media.protocol.PushBufferStream;
import  javax.media.control.QualityControl;
import  javax.media.rtp. * ;
import  javax.media.rtp.rtcp. * ;
import  com.sun.media.rtp. * ;
import  java.awt. * ;
import  java.awt.event. * ;
import  javax.swing. * ;
import  java.net. * ;

public   class  JmfV  extends  JFrame
... {
    
public static Player player=null;
    
private CaptureDeviceInfo di=null;
    
private MediaLocator locator=null;
    String str1
="vfw:Logitech USB Video Camera:0";
    String str2
="vfw:Microsoft WDM Image Capture (Win32):0";
    
private Processor processor = null;
    
private RTPManager rtpMgrs[];
    
private DataSource dataOutput = null,ds=null,ds_1=null;
    
private String ipAddress;
    
private int portBase;
    
public JmfV( String ipAddress,
    String pb,
    Format format )
    
...{
     
this.ipAddress = ipAddress;
     Integer integer 
= Integer.valueOf(pb);
     
if (integer != null)
         
this.portBase = integer.intValue();
        di
=CaptureDeviceManager.getDevice(str2);
        locator
=di.getLocator();
        
try
        
...{
            dataOutput
=Manager.createDataSource(locator);  
            player
=Manager.createRealizedPlayer(ds);
            player.start();
            Component comp
=null,comp_v=null;
            
if((comp=player.getControlPanelComponent())!=null)
            
...{
                
this.getContentPane().add(comp,"North");
                
if((comp_v=player.getVisualComponent())!=null)
                    
this.getContentPane().add(comp_v,"Center");
            }
 
            ds
=Manager.createCloneableDataSource(dataOutput);
            
this.setSize(320,320);
            
this.setVisible(true);
            
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
catch(Exception e )
        
...{
            e.printStackTrace();
        }

    }

    
public synchronized String start() ...{
     String result;

     
// Create a processor for the specified media locator
     
// and program it to output JPEG/RTP
     result = createProcessor();
     
if (result != null)
         
return result;

     
// Create an RTP session to transmit the output of the
     
// processor to the specified IP address and port no.
     result = createTransmitter();
     
if (result != null...{
         processor.close();
         processor 
= null;
         
return result;
     }


     
// Start the transmission
     processor.start();
     
     
return null;
        }


    
        
public void stop() ...{
     
synchronized (this...{
         
if (processor != null...{
      processor.stop();
      processor.close();
      processor 
= null;
      
for (int i = 0; i < rtpMgrs.length; i++...{
          rtpMgrs[i].removeTargets( 
"Session ended.");
          rtpMgrs[i].dispose();
      }

         }

     }

        }


        
private String createProcessor() ...{
     
if (locator == null)
         
return "Locator is null";
     
// Try to create a processor to handle the input media locator
     try ...{
         processor 
= javax.media.Manager.createProcessor(ds);
     }
 catch (NoProcessorException npe) ...{
         
return "Couldn't create processor";
     }
 catch (IOException ioe) ...{
         
return "IOException creating processor";
     }


     
// Wait for it to configure
     boolean result = waitForState(processor, Processor.Configured);
     
if (result == false)
         
return "Couldn't configure processor";

     
// Get the tracks from the processor
     TrackControl [] tracks = processor.getTrackControls();

     
// Do we have atleast one track?
     if (tracks == null || tracks.length < 1)
         
return "Couldn't find tracks in processor";

     
// Set the output content descriptor to RAW_RTP
     
// This will limit the supported formats reported from
     
// Track.getSupportedFormats to only valid RTP formats.
     ContentDescriptor cd = new ContentDescriptor(ContentDescriptor.RAW_RTP);
     processor.setContentDescriptor(cd);

     Format supported[];
     Format chosen;
     
boolean atLeastOneTrack = false;

     
// Program the tracks.
     for (int i = 0; i < tracks.length; i++...{
         Format format 
= tracks[i].getFormat();
         
if (tracks[i].isEnabled()) ...{

      supported 
= tracks[i].getSupportedFormats();

      
// We've set the output content to the RAW_RTP.
      
// So all the supported formats should work with RTP.
      
// We'll just pick the first one.

      
if (supported.length > 0...{
     &nbs

你可能感兴趣的:(.net,swing,Microsoft,sun)