加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

[转载]jmf实现视频通话

(2012-11-26 20:38:53)
标签:

转载

看看原文
原文地址:jmf实现视频通话作者:tianya_feixue
  1. 第一个类::::
  2. package vidioPlay;   
  3.   
  4. import java.awt.Dimension;   
  5. import java.io.IOException;   
  6. import java.net.InetAddress;   
  7. import java.util.Vector;   
  8.   
  9. import javax.media.CaptureDeviceInfo;   
  10. import javax.media.Codec;   
  11. import javax.media.Control;   
  12. import javax.media.Controller;   
  13. import javax.media.ControllerClosedEvent;   
  14. import javax.media.ControllerEvent;   
  15. import javax.media.ControllerListener;   
  16. import javax.media.Format;   
  17. import javax.media.IncompatibleSourceException;   
  18. import javax.media.Manager;   
  19. import javax.media.MediaLocator;   
  20. import javax.media.NoProcessorException;   
  21. import javax.media.Owned;   
  22. import javax.media.Player;   
  23. import javax.media.Processor;   
  24. import javax.media.cdm.CaptureDeviceManager;   
  25. import javax.media.control.QualityControl;   
  26. import javax.media.control.TrackControl;   
  27. import javax.media.format.AudioFormat;   
  28. import javax.media.format.VideoFormat;   
  29. import javax.media.protocol.ContentDescriptor;   
  30. import javax.media.protocol.DataSource;   
  31. import javax.media.protocol.PushBufferDataSource;   
  32. import javax.media.protocol.PushBufferStream;   
  33. import javax.media.protocol.SourceCloneable;   
  34. import javax.media.rtp.RTPManager;   
  35. import javax.media.rtp.SendStream;   
  36. import javax.media.rtp.SessionAddress;   
  37. import javax.media.rtp.rtcp.SourceDescription;   
  38. import javax.swing.JFrame;   
  39.   
  40. public class MediaTransmit {   
  41.   
  42.     private String ipAddress;   
  43.     private int portBase;   
  44.     private MediaLocator audioLocator nullvedioLocator null;   
  45.     private Processor audioProcessor null;   
  46.     private Processor videoProcessor null;   
  47.     private DataSource audioDataLocal nullvideoDataLocal null;   
  48.     private DataSource audioDataOutput nullvideoDataOutput null;   
  49.     private RTPManager rtpMgrs[];   
  50.     private DataSource mediaData null;   
  51.     private DataSource dataLocalClone null;   
  52.   
  53.     private PlayPane playFrame;   
  54.   
  55.     public MediaTransmit(String ipAddress, String pb) {   
  56.         this.ipAddress ipAddress;   
  57.         Integer integer Integer.valueOf(pb);   
  58.         if (integer != null{   
  59.             this.portBase integer.intValue();   
  60.         }   
  61.         // /////////////////////////////////////////////   
  62.         playFrame new PlayPane();   
  63.         JFrame jf new JFrame("视频实例");   
  64.   
  65.         jf.add(playFrame);   
  66.         jf.pack();   
  67.         jf.setLocationRelativeTo(null);   
  68.         jf.setDefaultCloseOperation(3);   
  69.         jf.setVisible(true);   
  70.         // ////////////////////////////////////////////   
  71.         Vector<CaptureDeviceInfo> video CaptureDeviceManager   
  72.                 .getDeviceList(new VideoFormat(null));   
  73.         Vector<CaptureDeviceInfo> audio CaptureDeviceManager   
  74.                 .getDeviceList(new AudioFormat(AudioFormat.LINEAR, 44100162));   
  75.         // MediaLocator mediaLocator new   
  76.         // MediaLocator("file:/C:/纯音乐 忧伤还是快乐.mp3");   
  77.         if (audio != null && audio.size() 0{   
  78.             audioLocator ((CaptureDeviceInfo) audio.get(0)).getLocator();   
  79.             if ((audioProcessor createProcessor(audioLocator)) != null{   
  80.                 audioDataLocal mediaData;   
  81.                 audioDataOutput audioProcessor.getDataOutput();   
  82.             }   
  83.         else {   
  84.             System.out.println("******错误:没有检测到您的音频采集设备!!!");   
  85.         }   
  86.         // /////////////////////////////////////////////////////////   
  87.         if (video != null && video.size() 0{   
  88.             vedioLocator ((CaptureDeviceInfo) video.get(0)).getLocator();   
  89.             if ((videoProcessor createProcessor(vedioLocator)) != null{   
  90.                 videoDataLocal mediaData;   
  91.                 videoDataOutput videoProcessor.getDataOutput();   
  92.             }   
  93.         else {   
  94.             System.out.println("******错误:没有检测到您的视频设备!!!");   
  95.         }   
  96.         // /////////////////////////////////////////////////////////   
  97.         final DataSource[] dataSources new DataSource[2];   
  98.         dataSources[0audioDataLocal;   
  99.         dataSources[1videoDataLocal;   
  100.         try {   
  101.             DataSource dsLocal Manager.createMergingDataSource(dataSources);   
  102.             playFrame.localPlay(dsLocal);   
  103.         catch (IncompatibleSourceException e) {   
  104.             e.printStackTrace();   
  105.             return;   
  106.         }   
  107.         // ////////////////////////////////////////////////远程传输   
  108.         dataSources[1audioDataOutput;   
  109.         dataSources[0videoDataOutput;   
  110.   
  111.         try {   
  112.             DataSource dsoutput Manager.createMergingDataSource(dataSources);   
  113.             createTransmitter(dsoutput);   
  114.         catch (IncompatibleSourceException e) {   
  115.             e.printStackTrace();   
  116.             return;   
  117.         }   
  118.         audioProcessor.start();   
  119.         videoProcessor.start();   
  120.     }   
  121.   
  122.     private Processor createProcessor(MediaLocator locator) {   
  123.         Processor processor null;   
  124.         if (locator == null)   
  125.             return null;   
  126.         // 通过设备定位器得到数据源,   
  127.         try {   
  128.             mediaData Manager.createDataSource(locator);   
  129.             // 创建可克隆数据源   
  130.             mediaData Manager.createCloneableDataSource(mediaData);   
  131.             // 克隆数据源,用于传输到远程   
  132.             dataLocalClone ((SourceCloneable) mediaData).createClone();   
  133.         catch (Exception e) {   
  134.             e.printStackTrace();   
  135.             return null;   
  136.         }   
  137.         try {   
  138.             processor javax.media.Manager.createProcessor(dataLocalClone);   
  139.   
  140.         catch (NoProcessorException npe) {   
  141.             npe.printStackTrace();   
  142.             return null;   
  143.         catch (IOException ioe) {   
  144.             return null;   
  145.         }   
  146.         boolean result waitForState(processor, Processor.Configured);   
  147.         if (result == false)   
  148.             return null;   
  149.   
  150.         TrackControl[] tracks processor.getTrackControls();   
  151.         if (tracks == null || tracks.length 1)   
  152.             return null;   
  153.         ContentDescriptor cd new ContentDescriptor(ContentDescriptor.RAW_RTP);   
  154.         processor.setContentDescriptor(cd);   
  155.         Format supportedFormats[];   
  156.         Format chosen;   
  157.         boolean atLeastOneTrack false;   
  158.         for (int 0tracks.length; i++) {   
  159.             if (tracks[i].isEnabled()) {   
  160.                 supportedFormats tracks[i].getSupportedFormats();   
  161.                 if (supportedFormats.length 0{   
  162.                     if (supportedFormats[0instanceof VideoFormat) {   
  163.                         chosen checkForVideoSizes(tracks[i].getFormat(),   
  164.                                 supportedFormats[0]);   
  165.                     else  
  166.                         chosen supportedFormats[0];   
  167.                     tracks[i].setFormat(chosen);   
  168.                     System.err   
  169.                             .println("Track " is set to transmit as:");   
  170.                     System.err.println( " chosen);   
  171.                     atLeastOneTrack true;   
  172.                 else  
  173.                     tracks[i].setEnabled(false);   
  174.             else  
  175.                 tracks[i].setEnabled(false);   
  176.         }   
  177.   
  178.         if (!atLeastOneTrack)   
  179.             return null;   
  180.         result waitForState(processor, Controller.Realized);   
  181.         if (result == false)   
  182.             return null;   
  183.         setJPEGQuality(processor, 0.5f);   
  184.   
  185.         return processor;   
  186.     }   
  187.   
  188.     private String createTransmitter(DataSource dataOutput) {   
  189.         PushBufferDataSource pbds (PushBufferDataSource) dataOutput;   
  190.         PushBufferStream pbss[] pbds.getStreams();   
  191.         System.out.println("pbss.length:" pbss.length);   
  192.         rtpMgrs new RTPManager[pbss.length];   
  193.         SendStream sendStream;   
  194.         int port;   
  195.         // SourceDescription srcDesList[];   
  196.   
  197.         for (int 0pbss.length; i++) {   
  198.             try {   
  199.                 rtpMgrs[i] RTPManager.newInstance();   
  200.   
  201.                 port portBase 2 i;   
  202.                 SessionAddress localAddr new SessionAddress(   
  203.                         InetAddress.getLocalHost(), port);   
  204.                 SessionAddress destAddr new SessionAddress(   
  205.                         InetAddress.getByName(ipAddress), port);   
  206.                 rtpMgrs[i].initialize(localAddr);   
  207.                 rtpMgrs[i].addTarget(destAddr);   
  208.                 System.out.println("Created RTP session: "  
  209.                         InetAddress.getLocalHost() " port);   
  210.                 sendStream rtpMgrs[i].createSendStream(dataOutput, i);   
  211.                 sendStream.start();   
  212.             catch (Exception e) {   
  213.                 e.printStackTrace();   
  214.                 return e.getMessage();   
  215.             }   
  216.         }   
  217.   
  218.         return null;   
  219.     }   
  220.     Format checkForVideoSizes(Format original, Format supported) {   
  221.   
  222.         int width, height;   
  223.         Dimension size ((VideoFormat) original).getSize();   
  224.         Format jpegFmt new Format(VideoFormat.JPEG_RTP);   
  225.         Format h263Fmt new Format(VideoFormat.H263_RTP);   
  226.   
  227.         if (supported.matches(jpegFmt)) {   
  228.             width (size.width 8 == 0 size.width   
  229.                     (int(size.width 88);   
  230.             height (size.height 8 == 0 size.height   
  231.                     (int(size.height 88);   
  232.         else if (supported.matches(h263Fmt)) {   
  233.             if (size.width 128{   
  234.                 width 128;   
  235.                 height 96;   
  236.             else if (size.width 176{   
  237.                 width 176;   
  238.                 height 144;   
  239.             else {   
  240.                 width 352;   
  241.                 height 288;   
  242.             }   
  243.         else {   
  244.             return supported;   
  245.         }   
  246.   
  247.         return (new VideoFormat(nullnew Dimension(width, height),   
  248.                 Format.NOT_SPECIFIED, nullFormat.NOT_SPECIFIED))   
  249.                 .intersects(supported);   
  250.     }   
  251.     void setJPEGQuality(Player p, float val) {   
  252.   
  253.         Control cs[] p.getControls();   
  254.         QualityControl qc null;   
  255.         VideoFormat jpegFmt new VideoFormat(VideoFormat.JPEG);   
  256.         for (int 0cs.length; i++) {   
  257.   
  258.             if (cs[i] instanceof QualityControl && cs[i] instanceof Owned) {   
  259.                 Object owner ((Owned) cs[i]).getOwner();   
  260.                 if (owner instanceof Codec) {   
  261.                     Format fmts[] ((Codec) owner)   
  262.                             .getSupportedOutputFormats(null);   
  263.                     for (int 0fmts.length; j++) {   
  264.                         if (fmts[j].matches(jpegFmt)) {   
  265.                             qc (QualityControl) cs[i];   
  266.                             qc.setQuality(val);   
  267.                             System.err.println("- Setting quality to " val   
  268.                                     on " qc);   
  269.                             break;   
  270.                         }   
  271.                     }   
  272.                 }   
  273.                 if (qc != null)   
  274.                     break;   
  275.             }   
  276.         }   
  277.     }   
  278.     private Integer stateLock new Integer(0);   
  279.     private boolean failed false;   
  280.   
  281.     Integer getStateLock() {   
  282.         return stateLock;   
  283.     }   
  284.   
  285.     void setFailed() {   
  286.         failed true;   
  287.     }   
  288.   
  289.     private synchronized boolean waitForState(Processor p, int state) {   
  290.         p.addControllerListener(new StateListener());   
  291.         failed false;   
  292.         if (state == Processor.Configured) {   
  293.             p.configure();   
  294.         else if (state == Processor.Realized) {   
  295.             p.realize();   
  296.         }   
  297.         while (p.getState() state && !failed) {   
  298.             synchronized (getStateLock()) {   
  299.                 try {   
  300.                     getStateLock().wait();   
  301.                 catch (InterruptedException ie) {   
  302.                     return false;   
  303.                 }   
  304.             }   
  305.         }   
  306.   
  307.         if (failed)   
  308.             return false;   
  309.         else  
  310.             return true;   
  311.     }   
  312.     class StateListener implements ControllerListener {   
  313.   
  314.         public void controllerUpdate(ControllerEvent ce) {   
  315.   
  316.             if (ce instanceof ControllerClosedEvent)   
  317.                 setFailed();   
  318.   
  319.             if (ce instanceof ControllerEvent) {   
  320.                 synchronized (getStateLock()) {   
  321.                     getStateLock().notifyAll();   
  322.                 }   
  323.             }   
  324.         }   
  325.     }   
  326.     public static void main(String[] args) {   
  327.         String[] strs "localhost""9994" };   
  328.         new MediaTransmit(strs[0], strs[1]);   
  329.     }   
  330.  

 

第二个类:::

 

  1. package vidioPlay;   
  2.   
  3. import java.awt.BorderLayout;   
  4. import java.awt.Component;   
  5. import java.awt.Dimension;   
  6. import java.awt.Panel;   
  7. import java.net.InetAddress;   
  8. import java.util.Vector;   
  9.   
  10. import javax.media.ControllerErrorEvent;   
  11. import javax.media.ControllerEvent;   
  12. import javax.media.ControllerListener;   
  13. import javax.media.Player;   
  14. import javax.media.RealizeCompleteEvent;   
  15. import javax.media.bean.playerbean.MediaPlayer;   
  16. import javax.media.control.BufferControl;   
  17. import javax.media.format.FormatChangeEvent;   
  18. import javax.media.protocol.DataSource;   
  19. import javax.media.rtp.Participant;   
  20. import javax.media.rtp.RTPControl;   
  21. import javax.media.rtp.RTPManager;   
  22. import javax.media.rtp.ReceiveStream;   
  23. import javax.media.rtp.ReceiveStreamListener;   
  24. import javax.media.rtp.SessionListener;   
  25. import javax.media.rtp.event.ByeEvent;   
  26. import javax.media.rtp.event.NewParticipantEvent;   
  27. import javax.media.rtp.event.NewReceiveStreamEvent;   
  28. import javax.media.rtp.event.ReceiveStreamEvent;   
  29. import javax.media.rtp.event.RemotePayloadChangeEvent;   
  30. import javax.media.rtp.event.SessionEvent;   
  31. import javax.media.rtp.event.StreamMappedEvent;   
  32. import javax.swing.JFrame;   
  33.   
  34. import net.sf.fmj.media.rtp.RTPSocketAdapter;   
  35.   
  36.   
  37. public class MediaReceive implements ReceiveStreamListener, SessionListener,   
  38.         ControllerListener {   
  39.     String sessions[] null;   
  40.     RTPManager mgrs[] null;   
  41.   
  42.     boolean dataReceived false;   
  43.     Object dataSync new Object();   
  44.     private PlayPane playFrame;   
  45.   
  46.     public MediaReceive(String sessions[]) {   
  47.         this.sessions sessions;   
  48.     }   
  49.   
  50.     protected void initialize() {   
  51.         playFrame new PlayPane();   
  52.         JFrame jf new JFrame("视频实例");   
  53.   
  54.         jf.add(playFrame);   
  55.         jf.pack();   
  56.         jf.setLocationRelativeTo(null);   
  57.         jf.setDefaultCloseOperation(3);   
  58.         jf.setVisible(true);   
  59.         try {   
  60.             // 每一个session对应一个RTPManager   
  61.             mgrs new RTPManager[sessions.length];   
  62.             // 创建播放窗口的向量vector   
  63.   
  64.             SessionLabel session null;   
  65.   
  66.             // Open the RTP sessions.   
  67.             // 针对每一个会话对象进行ip、port和ttl的解析   
  68.             for (int 0sessions.length; i++) {   
  69.   
  70.                 // Parse the session addresses.   
  71.                 // 进行会话对象的解析,得到ip、port和ttl   
  72.                 try {   
  73.                     session new SessionLabel(sessions[i]);   
  74.                 catch (IllegalArgumentException e) {   
  75.                     System.err   
  76.                             .println("Failed to parse the session address given: "  
  77.                                     sessions[i]);   
  78.                     // return false;   
  79.                 }   
  80.   
  81.                 System.err.println( Open RTP session for: addr: "  
  82.                         session.addr port: " session.port ttl: "  
  83.                         session.ttl);   
  84.                 // 这对本条会话对象创建RTPManager   
  85.                 mgrs[i] (RTPManager) RTPManager.newInstance();   
  86.                 mgrs[i].addSessionListener(this);   
  87.                 mgrs[i].addReceiveStreamListener(this);   
  88.   
  89.                 // Initialize the RTPManager with the RTPSocketAdapter   
  90.                 // 将本机ip和端口号加入RTP会话管理   
  91.                 System.out.println("session.addr:" session.addr);   
  92.                 mgrs[i].initialize(new RTPSocketAdapter(InetAddress   
  93.                         .getByName(session.addr), session.port, session.ttl));   
  94.                 BufferControl bc (BufferControl) mgrs[i]   
  95.                         .getControl("javax.media.control.BufferControl");   
  96.                 if (bc != null)   
  97.                     bc.setBufferLength(350);   
  98.             }   
  99.   
  100.         catch (Exception e) {   
  101.             e.printStackTrace();   
  102.         }   
  103.     }   
  104.   
  105.       
  106.     protected void close() {   
  107.   
  108.         // close the RTP session.   
  109.         for (int 0mgrs.length; i++) {   
  110.             if (mgrs[i] != null{   
  111.                 mgrs[i].removeTargets("Closing session from AVReceive3");   
  112.                 mgrs[i].dispose();   
  113.                 mgrs[i] null;   
  114.             }   
  115.         }   
  116.     }   
  117.   
  118.       
  119.     @SuppressWarnings("deprecation")   
  120.     public synchronized void update(SessionEvent evt) {   
  121.   
  122.         if (evt instanceof NewParticipantEvent) {   
  123.             Participant ((NewParticipantEvent) evt).getParticipant();   
  124.             System.err.println( new participant had just joined: " p);   
  125.         }   
  126.     }   
  127.   
  128.       
  129.     public synchronized void update(ReceiveStreamEvent evt) {   
  130.   
  131.         RTPManager mgr (RTPManager) evt.getSource();   
  132.         Participant participant evt.getParticipant(); // could be null.   
  133.         ReceiveStream stream evt.getReceiveStream(); // could be null.   
  134.   
  135.         if (evt instanceof RemotePayloadChangeEvent{   
  136.   
  137.             System.err.println( Received an RTP PayloadChangeEvent.");   
  138.             System.err.println("Sorry, cannot handle payload change.");   
  139.             // System.exit(0);   
  140.   
  141.         }   
  142.   
  143.         else if (evt instanceof NewReceiveStreamEvent) {   
  144.             System.out.println("evt instanceof NewReceiveStreamEvent");   
  145.             try {   
  146.                 stream ((NewReceiveStreamEvent) evt).getReceiveStream();   
  147.                 final DataSource data stream.getDataSource();   
  148.   
  149.                 // Find out the formats.   
  150.                 RTPControl ctl (RTPControl) data   
  151.                         .getControl("javax.media.rtp.RTPControl");   
  152.                 if (ctl != null{   
  153.                     System.err.println( Recevied new RTP stream: "  
  154.                             ctl.getFormat());   
  155.                 else  
  156.                     System.err.println( Recevied new RTP stream");   
  157.   
  158.                 if (participant == null)   
  159.                     System.err   
  160.                             .println(     The sender of this stream had yet to be identified.");   
  161.                 else {   
  162.                     System.err.println(     The stream comes from: "  
  163.                             participant.getCNAME());   
  164.                 }   
  165.   
  166.                 // create player by passing datasource to the Media Manager   
  167.                 new Thread() {   
  168.                     public void run() {   
  169.                         playFrame.remotePlay(data);   
  170.                     }   
  171.                 }.start();   
  172.                 // Player javax.media.Manager.createPlayer(data);   
  173.                 // if (p == null)   
  174.                 // return;   
  175.                 //   
  176.                 // p.addControllerListener(this);   
  177.                 // p.realize();   
  178.                 // PlayerWindow pw new PlayerWindow(p, stream);   
  179.                 // playerWindows.addElement(pw);   
  180.   
  181.                 // Notify intialize() that new stream had arrived.   
  182.                 synchronized (dataSync) {   
  183.                     dataReceived true;   
  184.                     dataSync.notifyAll();   
  185.                 }   
  186.   
  187.             catch (Exception e) {   
  188.                 System.err.println("NewReceiveStreamEvent exception "  
  189.                         e.getMessage());   
  190.                 return;   
  191.             }   
  192.   
  193.         }   
  194.   
  195.         else if (evt instanceof StreamMappedEvent) {   
  196.             System.out.println("evt instanceof StreamMappedEvent");   
  197.             stream ((StreamMappedEvent) evt).getReceiveStream();   
  198.             if (stream != null && stream.getDataSource() != null{   
  199.                 DataSource ds stream.getDataSource();   
  200.                 // Find out the formats.   
  201.                 RTPControl ctl (RTPControl) ds   
  202.                         .getControl("javax.media.rtp.RTPControl");   
  203.                 System.err.println( The previously unidentified stream ");   
  204.                 if (ctl != null)   
  205.                     System.err.println(     " ctl.getFormat());   
  206.                 System.err.println(     had now been identified as sent by: "  
  207.                         participant.getCNAME());   
  208.                 System.out.println("ds == null" (ds == null));   
  209.             }   
  210.         }   
  211.   
  212.         else if (evt instanceof ByeEvent) {   
  213.   
  214.             System.err.println( Got "bye" from: "  
  215.                     participant.getCNAME());   
  216.   
  217.         }   
  218.   
  219.     }   
  220.   
  221.       
  222.     public synchronized void controllerUpdate(ControllerEvent ce) {   
  223.   
  224.         Player (Player) ce.getSourceController();   
  225.   
  226.         if (p == null)   
  227.             return;   
  228.   
  229.     }   
  230.   
  231.       
  232.     class SessionLabel {   
  233.   
  234.         public String addr null;   
  235.         public int port;   
  236.         public int ttl 1;   
  237.   
  238.         SessionLabel(String session) throws IllegalArgumentException {   
  239.   
  240.             int off;   
  241.             String portStr nullttlStr null;   
  242.   
  243.             if (session != null && session.length() 0{   
  244.                 while (session.length() 1 && session.charAt(0== '/'{   
  245.                     session session.substring(1);   
  246.                 }   
  247.                 off session.indexOf('/');   
  248.                 if (off == -1{   
  249.                     if (!session.equals(""))   
  250.                         addr session;   
  251.                 else {   
  252.                     addr session.substring(0off);   
  253.                     session session.substring(off 1);   
  254.                     off session.indexOf('/');   
  255.                     if (off == -1{   
  256.                         if (!session.equals(""))   
  257.                             portStr session;   
  258.                     else {   
  259.                         portStr session.substring(0off);   
  260.                         session session.substring(off 1);   
  261.                         off session.indexOf('/');   
  262.                         if (off == -1{   
  263.                             if (!session.equals(""))   
  264.                                 ttlStr session;   
  265.                         else {   
  266.                             ttlStr session.substring(0off);   
  267.                         }   
  268.                     }   
  269.                 }   
  270.             }   
  271.   
  272.             if (addr == null)   
  273.                 throw new IllegalArgumentException();   
  274.   
  275.             if (portStr != null{   
  276.                 try {   
  277.                     Integer integer Integer.valueOf(portStr);   
  278.                     if (integer != null)   
  279.                         port integer.intValue();   
  280.                 catch (Throwable t) {   
  281.                     throw new IllegalArgumentException();   
  282.                 }   
  283.             else  
  284.                 throw new IllegalArgumentException();   
  285.   
  286.             if (ttlStr != null{   
  287.                 try {   
  288.                     Integer integer Integer.valueOf(ttlStr);   
  289.                     if (integer != null)   
  290.                         ttl integer.intValue();   
  291.                 catch (Throwable t) {   
  292.                     throw new IllegalArgumentException();   
  293.                 }   
  294.             }   
  295.         }   
  296.     }   
  297.        
  298.   
  299.     public static void main(String argv[]) {   
  300.         String[] strs "125.221.165.126/9994""125.221.165.126/9996" };   
  301.         MediaReceive avReceive new MediaReceive(strs);   
  302.         avReceive.initialize();   
  303.   
  304.     }   
  305.  

 

第三个类::

  1. package vidioPlay;   
  2.   
  3. import java.awt.BorderLayout;   
  4. import java.awt.Color;   
  5. import java.awt.Component;   
  6. import java.awt.Dimension;   
  7. import java.awt.FlowLayout;   
  8. import java.awt.Graphics;   
  9. import java.awt.Rectangle;   
  10. import java.awt.event.ActionEvent;   
  11. import java.awt.event.ActionListener;   
  12. import java.io.IOException;   
  13.   
  14. import javax.media.ControllerEvent;   
  15. import javax.media.ControllerListener;   
  16. import javax.media.DataSink;   
  17. import javax.media.NoPlayerException;   
  18. import javax.media.Player;   
  19. import javax.media.Processor;   
  20. import javax.media.protocol.DataSource;   
  21. import javax.swing.ImageIcon;   
  22. import javax.swing.JButton;   
  23. import javax.swing.JPanel;   
  24.   
  25. public class PlayPane extends JPanel {   
  26.     private ImageIcon videoReqIcon new ImageIcon("videoReq.jpg");   
  27.     private ImageIcon VideolocalIcon new ImageIcon("localVideo.jpg");   
  28.     private boolean isViewBigPlaying false;   
  29.     private boolean isViewSmallPlaying false;   
  30.     private JPanel viewBigPane;   
  31.     private JPanel viewSmallPane;   
  32.     private JPanel controlPane;   
  33.   
  34.     private JButton closeButton;   
  35.   
  36.     private boolean localPlay false;   
  37.     private boolean remotePlay false;   
  38.   
  39.     private DataSource localData;   
  40.     private DataSource remoteData;   
  41.   
  42.     private boolean isViewRun true;   
  43.     private boolean isShow true;   
  44.     //   
  45.     private Player localPlayer null;   
  46.     private Player remotePlayer null;   
  47.     //   
  48.     private Processor videotapeProcessor null;   
  49.     private Player videotapePlayer null;   
  50.     private DataSink videotapeFileWriter;   
  51.   
  52.     public PlayPane() {   
  53.         this.setLayout(new BorderLayout());   
  54.         // 视图面板   
  55.         viewBigPane new JPanel() {   
  56.             public void paintComponent(Graphics g) {   
  57.                 super.paintComponent(g);   
  58.                 if (!isViewBigPlaying) {   
  59.                     g.drawImage(videoReqIcon.getImage(), 11,   
  60.                             videoReqIcon.getIconWidth(),   
  61.                             videoReqIcon.getIconHeight(), null);   
  62.   
  63.                     g.drawRect(getSmallPlayRec().x 1,   
  64.                             getSmallPlayRec().y 1,   
  65.                             getSmallPlayRec().width 1,   
  66.                             getSmallPlayRec().height 1);   
  67.                 else {   
  68.   
  69.                 }   
  70.             }   
  71.         };   
  72.         viewBigPane.setBackground(Color.black);   
  73.         this.add(viewBigPane, BorderLayout.CENTER);   
  74.         viewBigPane.setLayout(null);   
  75.         // ///////////////////////////////   
  76.         viewSmallPane new JPanel() {   
  77.             public void paintComponent(Graphics g) {   
  78.                 super.paintComponent(g);   
  79.                 if (!isViewSmallPlaying) {   
  80.                     g.drawImage(VideolocalIcon.getImage(), 00null);   
  81.                 else {   
  82.   
  83.                 }   
  84.             }   
  85.         };   
  86.         viewSmallPane.setBounds(getSmallPlayRec());   
  87.         viewBigPane.add(viewSmallPane);   
  88.         viewSmallPane.setLayout(null);   
  89.   
  90.         // 控制面板组件   
  91.         closeButton new JButton("挂断");   
  92.         controlPane new JPanel();   
  93.         controlPane.setLayout(new FlowLayout(FlowLayout.RIGHT, 00));   
  94.         controlPane.add(closeButton);   
  95.         this.add(controlPane, BorderLayout.SOUTH);   
  96.         closeButton.addActionListener(new ActionListener() {   
  97.             public void actionPerformed(ActionEvent e) {   
  98.                 if (localPlayer != null{   
  99.                     localPlayer.stop();   
  100.                 }   
  101.                 if (remotePlayer != null{   
  102.                     remotePlayer.stop();   
  103.                 }   
  104.                 if (videotapePlayer != null{   
  105.                     videotapePlayer.stop();   
  106.                 }   
  107.                 if (videotapeProcessor != null{   
  108.                     videotapeProcessor.stop();   
  109.                 }   
  110.                 if (videotapeFileWriter != null{   
  111.                     try {   
  112.                         videotapeFileWriter.stop();   
  113.                         videotapeFileWriter.close();   
  114.                     catch (IOException e1) {   
  115.                     }   
  116.                 }   
  117.             }   
  118.   
  119.         });   
  120.         // this.setMinimumSize(new Dimension(videoReqIcon.getIconWidth()+2,   
  121.         // 241));   
  122.         // this.setPreferredSize(new Dimension(videoReqIcon.getIconWidth()+2,   
  123.         // 241));   
  124.   
  125.     }   
  126.   
  127.     public Dimension getMinimumSize() {   
  128.         System.out   
  129.                 .println("controlPane.getHeight():" controlPane.getHeight());   
  130.         return new Dimension(videoReqIcon.getIconWidth() 2,   
  131.                 videoReqIcon.getIconHeight() controlPane.getHeight());   
  132.     }   
  133.   
  134.     public Dimension getPreferredSize() {   
  135.         System.out   
  136.                 .println("controlPane.getHeight():" controlPane.getHeight());   
  137.         return new Dimension(videoReqIcon.getIconWidth() 2,   
  138.                 videoReqIcon.getIconHeight()   
  139.                         controlPane.getPreferredSize().height);   
  140.     }   
  141.   
  142.     public void localPlay(DataSource dataSource) {   
  143.         this.setLocalData(dataSource);   
  144.         try {   
  145.             localPlayer javax.media.Manager.createPlayer(dataSource);   
  146.   
  147.             localPlayer.addControllerListener(new ControllerListener() {   
  148.                 public void controllerUpdate(ControllerEvent e) {   
  149.   
  150.                     if (e instanceof javax.media.RealizeCompleteEvent) {   
  151.                         Component comp null;   
  152.                         comp localPlayer.getVisualComponent();   
  153.                         if (comp != null{   
  154.                             // 将可视容器加到窗体上   
  155.                             comp.setBounds(00VideolocalIcon.getIconWidth(),   
  156.                                     VideolocalIcon.getIconHeight());   
  157.                             viewSmallPane.add(comp);   
  158.                         }   
  159.                         viewBigPane.validate();   
  160.                     }   
  161.                 }   
  162.             });   
  163.             localPlayer.start();   
  164.             localPlay true;   
  165.         catch (NoPlayerException e1) {   
  166.             e1.printStackTrace();   
  167.         catch (IOException e1) {   
  168.             e1.printStackTrace();   
  169.         }   
  170.     }   
  171.   
  172.     private Rectangle getSmallPlayRec() {   
  173.         int bigShowWidth videoReqIcon.getIconWidth();   
  174.         int bigShowHeight videoReqIcon.getIconHeight();   
  175.         int smallShowWidth VideolocalIcon.getIconWidth();   
  176.         int smallShowHeight VideolocalIcon.getIconHeight();   
  177.         return new Rectangle(bigShowWidth smallShowWidth 2bigShowHeight   
  178.                 smallShowHeight 2smallShowWidth, smallShowHeight);   
  179.     }   
  180.   
  181.     public void remotePlay(DataSource dataSource) {   
  182.         this.setLocalData(dataSource);   
  183.         remotePlay true;   
  184.         try {   
  185.             remotePlayer javax.media.Manager.createPlayer(dataSource);   
  186.   
  187.             remotePlayer.addControllerListener(new ControllerListener() {   
  188.                 public void controllerUpdate(ControllerEvent e) {   
  189.   
  190.                     if (e instanceof javax.media.RealizeCompleteEvent) {   
  191.                         Component comp;   
  192.                         if ((comp remotePlayer.getVisualComponent()) != null{   
  193.                             // 将可视容器加到窗体上   
  194.                             comp.setBounds(11videoReqIcon.getIconWidth(),   
  195.                                     videoReqIcon.getIconHeight());   
  196.                             viewBigPane.add(comp);   
  197.                         }   
  198.                         viewBigPane.validate();   
  199.                     }   
  200.                 }   
  201.             });   
  202.             remotePlayer.start();   
  203.             remotePlay true;   
  204.         catch (NoPlayerException e1) {   
  205.             e1.printStackTrace();   
  206.         catch (IOException e1) {   
  207.             e1.printStackTrace();   
  208.         }   
  209.     }   
  210.   
  211.     public void closeViewUI() {   
  212.         isShow false;   
  213.     }   
  214.   
  215.     public boolean isViewRunning() {   
  216.         return isViewRun;   
  217.     }   
  218.   
  219.     public boolean isShowing() {   
  220.         return isShow;   
  221.     }   
  222.   
  223.     public void localReady() {   
  224.         localPlay true;   
  225.     }   
  226.   
  227.     public void remoteReady() {   
  228.         remotePlay true;   
  229.     }   
  230.   
  231.     public boolean isRemotePlay() {   
  232.         return remotePlay;   
  233.     }   
  234.   
  235.     public void setRemotePlay(boolean remotePlay) {   
  236.         this.remotePlay remotePlay;   
  237.     }   
  238.   
  239.     public DataSource getRemoteData() {   
  240.         return remoteData;   
  241.     }   
  242.   
  243.     public void setRemoteData(DataSource remoteData) {   
  244.         this.remoteData remoteData;   
  245.     }   
  246.   
  247.     public boolean isLocalPlay() {   
  248.         return localPlay;   
  249.     }   
  250.   
  251.     public void setLocalPlay(boolean localPlay) {   
  252.         this.localPlay localPlay;   
  253.     }   
  254.   
  255.     public DataSource getLocalData() {   
  256.         return localData;   
  257.     }   
  258.   
  259.     public void setLocalData(DataSource localData) {   
  260.         this.localData localData;   
  261.     }   
  262.   
  263.  

 

 

运行的时候先运行MediaTransmit类,后运行MediaReceive类

0

  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有