⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iruplayerapplet.java

📁 Pioneer2机器人远程控制程序
💻 JAVA
字号:
package abrobot.IRURobot.Client;

import java.applet.Applet;
import javax.media.rtp.*;
import javax.media.rtp.rtcp.*;
import javax.media.rtp.event.*;
import com.sun.media.rtp.RTPSessionMgr;
import java.io.*;
import java.awt.*;
import java.util.Vector;
import java.net.*;
import java.awt.event.*;
import java.lang.String;
import javax.media.*;
import javax.media.protocol.*;
import com.sun.media.*;
import com.sun.media.ui.*;
import java.io.IOException;
import java.lang.SecurityException;
/**
 * <p>Title: GOPDR</p>
 * <p>Description: General Open Platform for Distributed Robot</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: SJTU</p>
 * @author Hou Xueqiao
 * @version 1.01
 */

public class IRUPlayerApplet extends Applet implements
ControllerListener, ReceiveStreamListener{
     InetAddress destaddr;
     String address;
     String portstr;
     Player videoplayer = null;
     RTPSessionMgr videomgr = null;
     Component visualComponent = null;
     Component controlComponent = null;
     Panel VideoPanel = null;
     GridBagLayout gridbag = null;
     GridBagConstraints c = null;
     int width = 320;
     int height =240;
     Vector playerlist = new Vector();
     public IRUPlayerApplet() {
     }

     public void init() {
           try {
                jbInit();
           }
           catch(Exception e) {
                e.printStackTrace();
           }
    }
  //Component initialization
     private void jbInit() throws Exception {
            address="137.132.165.137";
            portstr="1200";
            StartSessionManager(address,StrToInt(portstr),"video");
            if (videomgr == null){
                System.err.println("null video manager ");
                return;
            }
    }

    public void start(){

           if (videoplayer != null){
                 videoplayer.start();
           }
           if (playerlist == null)  return;
           for (int i =0; i < playerlist.size(); i++){
                 Player player = (Player)playerlist.elementAt(i);
                 if (player != null)
                        new PlayerWindow(player);
            }
   }
   // applet has been stopped, stop and deallocate all the RTP players.
     public void stop(){
           if (videoplayer != null){
                 videoplayer.close();
           }
           if (playerlist == null) return;
           for (int i =0; i < playerlist.size(); i++){
                  Player player = (Player)playerlist.elementAt(i);
                  if (player != null){
                        player.close();
                  }
            }
     }

   // applet has been destroyed by the browser. Close the Session
   // Manager.
      public void destroy(){
       // close the video and audio RTP SessionManagers
            String reason = "Shutdown RTP Player";
            if (videomgr != null){
                   videomgr.closeSession(reason);
                   videoplayer = null;
                   videomgr = null;
            }
            super.destroy();
      }

      public String getAddress(){
              return  address;
      }

      public int getPort(){
            return StrToInt(portstr);
      }

      private int StrToInt(String str){
           if (str == null)  return -1;
           Integer retint = new Integer(str);
            return  retint.intValue();
      }

      public synchronized void controllerUpdate(ControllerEvent event) {
            Player player = null;
            Controller controller = (Controller)event.getSource();
            if (controller instanceof Player)
                    player  =(Player)event.getSource();
            if (player == null)
                return;
            if (event instanceof RealizeCompleteEvent) {
           // add the video player's visual component to the applet
                    if (( visualComponent =
                           player.getVisualComponent())!= null){
                           width = visualComponent.getPreferredSize().width;
                            height += visualComponent.getPreferredSize().height;
                            if (VideoPanel == null) {
                                      VideoPanel = new Panel();
                                      repositionPanel(width, height);
                                      VideoPanel.setLayout(new BorderLayout());
                            }
                           VideoPanel.add("Center", visualComponent);
                           VideoPanel.validate();
                     }
           // add the player's control component to the applet
                     if (( controlComponent =
                             player.getControlPanelComponent()) != null){
                             height += controlComponent.getPreferredSize().height;
                             if (VideoPanel == null) {
                                    VideoPanel = new Panel();
                                    VideoPanel.setLayout(new BorderLayout());
                             }
                             repositionPanel(width, height);
                             VideoPanel.add("South", controlComponent);
                             VideoPanel.validate();
                      }

                      if (VideoPanel != null){
                                   add("Center", VideoPanel);
                                   invalidate();
                      }
             }

            if (event instanceof SizeChangeEvent) {
                     if (VideoPanel != null){
                              SizeChangeEvent sce = (SizeChangeEvent) event;
                              int nooWidth = sce.getWidth();
                              int nooHeight = sce.getHeight();

               // Add the height of the default control component
                              if (controlComponent != null)
                                    nooHeight += controlComponent.getPreferredSize().height;

               // Set the new panel bounds and redraw
                                    repositionPanel(nooWidth, nooHeight);
                     }
          }
       validate();
   }

   /**
    * The video/control component panel needs to be repositioned to sit
    * in the middle of the applet window.
    */
   void repositionPanel(int width, int height) {
       VideoPanel.setBounds(0,0,width,height);
       VideoPanel.validate();
   }

   public void update( ReceiveStreamEvent event){
       RTPSessionMgr source =(RTPSessionMgr)event.getSource();
       Player newplayer = null;
       // create a new player if a new recvstream is detected
       if (event instanceof NewReceiveStreamEvent){
           try{
               ReceiveStream stream = ((NewReceiveStreamEvent)event).getReceiveStream();
               DataSource dsource = stream.getDataSource();
               newplayer = Manager.createPlayer(dsource);
               System.out.print("Receive New Stream.");
           }catch (Exception e){
                System.err.println("RTPPlayerApplet Exception " + e.getMessage());
                e.printStackTrace();
           }
           if (newplayer == null){
               System.out.print("New player is failed !");
               return;
           }else{
               System.out.print("New player is ok !");
           }
           // if this is the first video player, we need to listen to
           // its events. Add me as a ControllerListener before
           // starting the player
           if (source == videomgr){
              System.out.print("Source = videomgr !");
               if (videoplayer == null){
                   videoplayer = newplayer;
                   newplayer.addControllerListener(this);
                   newplayer.start();
               }
               else{// controller listener and start is taken care of
                   // in playerWindiow
                   if (playerlist != null)
                       playerlist.addElement((Object)newplayer);
                   new PlayerWindow(newplayer);
               }
           }// if (source == videomgr)

       }// if (event instanceof NewReceiveStreamEvent)


       if (event instanceof RemotePayloadChangeEvent){
           // we received a payload change event. If a player was not
           // created for this ReceiveStream, create a player. If the
           // player already exists, RTPSM and JMF have taken care of
           // switching the payloads and we dont do anything.
           // If this is the first video player add me as the
           // controllerlistener before starting the player, else
           // just create a new player window.
       }

   }// end of RTPSessionUpdate

   private RTPSessionMgr StartSessionManager(String destaddrstr,
                                                 int port,
                                                 String media){
       // this method create a new RTPSessionMgr and adds this applet
       // as a SessionListener, before calling initSession() and startSession()
       RTPSessionMgr mymgr = new RTPSessionMgr();
       if (media.equals("video"))
           videomgr = mymgr;
       if (mymgr == null)
           return null;
       mymgr.addReceiveStreamListener(this);

       // for initSession() we must generate a CNAME and fill in the
       // RTP Session address and port
       String cname = mymgr.generateCNAME();
       String username = "jmf-user";

       SessionAddress localaddr = new SessionAddress();
       // SessionAddress localaddr = null;
       try{
           destaddr = InetAddress.getByName(destaddrstr);
       }catch (UnknownHostException e){
           System.err.println("inetaddress " + e.getMessage());
           e.printStackTrace();
       }
       SessionAddress sessaddr = new SessionAddress(destaddr,
                                                     port);

       SourceDescription[] userdesclist = new SourceDescription[4];
       int i;
       for(i=0; i< userdesclist.length;i++){
           if (i == 0){
               userdesclist[i] = new
                   SourceDescription(SourceDescription.SOURCE_DESC_EMAIL,
                                   "jmf-user@sun.com",
                                   1,
                                   false);
               continue;
           }

           if (i == 1){
               userdesclist[i] = new
             SourceDescription(SourceDescription.SOURCE_DESC_NAME,
                                   username,
                                   1,
                                   false);
               continue;
           }
           if ( i == 2){
               userdesclist[i] = new
                   SourceDescription(SourceDescription.SOURCE_DESC_CNAME,
                                         cname,
                                     1,
                                     false);
               continue;
           }
           if (i == 3){
               userdesclist[i] = new
           SourceDescription(SourceDescription.SOURCE_DESC_TOOL,
                                 "JMF RTP Player v2.0",
                                 1,
                                 false);
               continue;
           }
       }// end of for

       // call initSession() and startSession() of the RTPsessionManager
       try{
           mymgr.initSession(localaddr,
                             mymgr.generateSSRC(),
                             userdesclist,
                             0.05,
                             0.25);
           mymgr.startSession(sessaddr,1,null);
       }catch (SessionManagerException e){
         System.err.println("RTPPlayerApplet: RTPSM Exception " + e.getMessage());
         e.printStackTrace();
         return null;
       }catch (IOException e){
          System.err.println("RTPPlayerApplet: IO Exception " + e.getMessage());
          e.printStackTrace();
          return null;
       }

       return mymgr;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -