我的程序是一个关于OSGI的应用,服务器端用JMF获取摄像头数据,并用JMF的RTP协议发送数据流。
这个bundle已经完成,而且用SUN网站上JMF的RTP接受的例子测试,能正常显示服务器的摄像头采集的数据。现在我想把这个C端改为applet形式的,把这个applet嵌在网页里边,本机上双击,完全可以运行显示摄像头数据,但是放到服务器上通过浏览器访问,就显示不了,但是也不报错。我觉得是applet部署的问题。
各位高人,救救我啊!!!包含applet的网页代码:<HTML>
<TITLE> AVReceive</TITLE>
<APPLET
ARCHIVE="jmf.jar"
CODE="AVReceive2.class"
WIDTH=400
HEIGHT=320
>
 </APPLET>
 </HTML>AVReceive2。java:其实就是sun网站上的示例代码,改成了applet形式的

解决方案 »

  1.   

    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;import java.io.*;
    import java.net.*;
    import java.util.Vector;
    import javax.media.*;
    import javax.media.rtp.*;
    import javax.media.rtp.event.*;
    import javax.media.protocol.DataSource;
    import javax.media.control.BufferControl;
    import javax.media.rtp.rtcp.*;
    import javax.media.rtp.ReceiveStream;
    import javax.media.protocol.*;
    import javax.media.format.AudioFormat;
    import javax.media.format.VideoFormat;
    import javax.media.Format;
    import javax.media.format.FormatChangeEvent;public class AVReceive2 extends Applet implements ReceiveStreamListener,
      SessionListener, ControllerListener
      {
      boolean isStandalone = false;
      /**Get a parameter value*/
      public String getParameter(String key, String def) {
        return isStandalone ? System.getProperty(key, def) :
          (getParameter(key) != null ? getParameter(key) : def);
      }  String sessions[] = {"127.0.0.1/42050","127.0.0.1/42506"};  RTPManager mgrs[] = null;
      Vector playerWindows = null;
      Component visualComponent = null;
      Component controlComponent = null;
      Component progressBar      = null;  boolean dataReceived = false;
      Object dataSync = new Object();
      BorderLayout borderLayout1 = new BorderLayout();  /**Construct the applet*/
      public AVReceive2() {
      }
      private void detectDevices(Label la) {
    // Check if VFWAuto or SunVideoAuto is available
    Class directAudio = null;
    Class autoAudio = null;
    Class autoVideo = null;
    Class autoVideoPlus = null;
    Object instanceAudio;
    Object instanceVideo;
    Object instanceVideoPlus;try {
    directAudio = Class.forName("DirectSoundAuto");
    } catch (Exception e) {
    }try {
    autoAudio = Class.forName("JavaSoundAuto");
    } catch (Exception e) {
    }try {
    autoVideo = Class.forName("VFWAuto");
    } catch (Exception e) {
    }if (autoVideo == null) {
    try {
    autoVideo = Class.forName("SunVideoAuto");
    } catch (Exception ee) {
    }
    try {
    autoVideoPlus = Class.forName("SunVideoPlusAuto");
    } catch (Exception ee) {
    }
    }
    if (autoVideo == null) {
    try {
    autoVideo = Class.forName("V4LAuto");
    } catch (Exception eee) {
    }
    }if (directAudio == null && autoAudio == null &&
    autoVideo == null && autoVideoPlus == null) {
    return;
    }try {
    if (directAudio != null) {
    instanceAudio = directAudio.newInstance();
    la.setText("loading....dirver:"+directAudio.getName());
    }
    if (autoAudio != null) {
    instanceAudio = autoAudio.newInstance();
    la.setText("loading....dirver:"+autoAudio.getName());
    }
    if (autoVideo != null) {
    instanceVideo = autoVideo.newInstance();
    la.setText("loading....dirver:"+autoVideo.getName());
    }
    if (autoVideoPlus != null) {
    instanceVideoPlus = autoVideoPlus.newInstance();
    la.setText("loading....dirver:"+autoVideoPlus.getName());
    }
    } catch (ThreadDeath td) {
    throw td;
    } catch (Throwable t) {}
    }
      /**Initialize the applet*/
      public void init() {
       Label la=new Label("heheheh");
       detectDevices(la);
        try {
          jbInit();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      /**Component initialization*/
      private void jbInit()
         {
        this.setLayout(borderLayout1);
            try {
        InetAddress ipAddr;
        SessionAddress localAddr = new SessionAddress();
        SessionAddress destAddr;     mgrs = new RTPManager[sessions.length];
        playerWindows = new Vector();     SessionLabel session = null;     // Open the RTP sessions.
        for (int i = 0; i < sessions.length; i++) {   // Parse the session addresses.
    try {
        session = new SessionLabel(sessions[i]);
    } catch (IllegalArgumentException e) {
        System.err.println("Failed to parse the session address given: " + sessions[i]);
    } System.err.println("  - Open RTP session for: addr: " + session.addr + " port: " + session.port + " ttl: " + session.ttl); mgrs[i] = (RTPManager) RTPManager.newInstance();
    mgrs[i].addSessionListener(this);
    mgrs[i].addReceiveStreamListener(this); ipAddr = InetAddress.getByName(session.addr); if( ipAddr.isMulticastAddress()) {
        // local and remote address pairs are identical:
        localAddr= new SessionAddress( ipAddr,
       session.port,
       session.ttl);
        destAddr = new SessionAddress( ipAddr,
       session.port,
       session.ttl);
    } else {
        localAddr= new SessionAddress( InetAddress.getLocalHost(),
                  session.port);
                        destAddr = new SessionAddress( ipAddr, session.port);
    } mgrs[i].initialize( localAddr); // You can try out some other buffer size to see
    // if you can get better smoothness.
    BufferControl bc = (BufferControl)mgrs[i].getControl("javax.media.control.BufferControl");
    if (bc != null)
        bc.setBufferLength(350);     mgrs[i].addTarget(destAddr);
        }        } catch (Exception e){
                System.err.println("Cannot create the RTP Session: " + e.getMessage());
            } // Wait for data to arrive before moving on. long then = System.currentTimeMillis();
    long waitingPeriod = 30000;  // wait for a maximum of 30 secs. try{
        synchronized (dataSync) {
    while (!dataReceived &&
    System.currentTimeMillis() - then < waitingPeriod) {
        if (!dataReceived)
    System.err.println("  - Waiting for RTP data to arrive...");
        dataSync.wait(1000);
    }
        }
    } catch (Exception e) {} if (!dataReceived) {
        System.err.println("No RTP data was received.");
        close();
    }
       }
      

  2.   

    /**Start the applet*/
      public void start() {
      }
      /**Stop the applet*/
      public void stop()
      {
      close();
      }
      /**Destroy the applet*/
      public void destroy() {
      }
      /**Get Applet information*/
      public String getAppletInfo() {
        return "Applet Information";
      }
      /**Get parameter info*/
      public String[][] getParameterInfo() {
        return null;
      }
      /**Main method*/
      public static void main(String[] args) {
        AVReceive2 applet = new AVReceive2();
        applet.isStandalone = true;
        Frame frame;
        frame = new Frame() {
          protected void processWindowEvent(WindowEvent e) {
            super.processWindowEvent(e);
            if (e.getID() == WindowEvent.WINDOW_CLOSING) {
              System.exit(0);
            }
          }
          public synchronized void setTitle(String title) {
            super.setTitle(title);
            enableEvents(AWTEvent.WINDOW_EVENT_MASK);
          }
        };
        frame.setTitle("Applet Frame");
        frame.add(applet, BorderLayout.CENTER);
        applet.init();
        applet.start();
        frame.setSize(400,320);
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
        frame.setVisible(true);
      }
        /**
         * Close the players and the session managers.
         */    protected void close() { for (int i = 0; i < playerWindows.size(); i++) {
        try {
    ((PlayerWindow)playerWindows.elementAt(i)).close();
        } catch (Exception e) {}
    } playerWindows.removeAllElements(); // close the RTP session.
    for (int i = 0; i < mgrs.length; i++) {
        if (mgrs[i] != null) {
                    mgrs[i].removeTargets( "Closing session from AVReceive2");
                    mgrs[i].dispose();
    mgrs[i] = null;
        }
    }
        }
        PlayerWindow find(Player p) {
    for (int i = 0; i < playerWindows.size(); i++) {
        PlayerWindow pw = (PlayerWindow)playerWindows.elementAt(i);
        if (pw.player == p)
    return pw;
    }
    return null;
        }
        PlayerWindow find(ReceiveStream strm) {
    for (int i = 0; i < playerWindows.size(); i++) {
        PlayerWindow pw = (PlayerWindow)playerWindows.elementAt(i);
        if (pw.stream == strm)
    return pw;
    }
    return null;
        }
        /**
         * SessionListener.
         */    public synchronized void update(SessionEvent evt) {
    if (evt instanceof NewParticipantEvent) {
        Participant p = ((NewParticipantEvent)evt).getParticipant();
        System.err.println("  - A new participant had just joined: " + p.getCNAME());
    }
        }
        /**
         * ReceiveStreamListener
         */    public synchronized void update( ReceiveStreamEvent evt) { RTPManager mgr = (RTPManager)evt.getSource();
    Participant participant = evt.getParticipant(); // could be null.
    ReceiveStream stream = evt.getReceiveStream();  // could be null. if (evt instanceof RemotePayloadChangeEvent) {     System.err.println("  - Received an RTP PayloadChangeEvent.");
        System.err.println("Sorry, cannot handle payload change.");
        System.exit(0); } else if (evt instanceof NewReceiveStreamEvent) {     try {
    stream = ((NewReceiveStreamEvent)evt).getReceiveStream();
    DataSource ds = stream.getDataSource(); // Find out the formats.
    RTPControl ctl = (RTPControl)ds.getControl("javax.media.rtp.RTPControl");
    if (ctl != null){
        System.err.println("  - Recevied new RTP stream: " + ctl.getFormat());
    } else
        System.err.println("  - Recevied new RTP stream"); if (participant == null)
        System.err.println("      The sender of this stream had yet to be identified.");
    else {
        System.err.println("      The stream comes from: " + participant.getCNAME());
    } // create a player by passing datasource to the Media Manager
    Player p = javax.media.Manager.createPlayer(ds);
    if (p == null)
        return; p.addControllerListener(this);
    p.realize();
    PlayerWindow pw = new PlayerWindow(p, stream);
    playerWindows.addElement(pw); // Notify intialize() that a new stream had arrived.
    synchronized (dataSync) {
        dataReceived = true;
        dataSync.notifyAll();
    }     } catch (Exception e) {
    System.err.println("NewReceiveStreamEvent exception " + e.getMessage());
    return;
        } } else if (evt instanceof StreamMappedEvent) {      if (stream != null && stream.getDataSource() != null) {
    DataSource ds = stream.getDataSource();
    // Find out the formats.
    RTPControl ctl = (RTPControl)ds.getControl("javax.media.rtp.RTPControl");
    System.err.println("  - The previously unidentified stream ");
    if (ctl != null)
        System.err.println("      " + ctl.getFormat());
    System.err.println("      had now been identified as sent by: " + participant.getCNAME());
         }
    } else if (evt instanceof ByeEvent) {      System.err.println("  - Got \"bye\" from: " + participant.getCNAME());
         PlayerWindow pw = find(stream);
         if (pw != null) {
    pw.close();
    playerWindows.removeElement(pw);
         }
    }
      }
      

  3.   

    /**
         * This controllerUpdate function must be defined in order
         * to implement a ControllerListener interface.  This
         * function will be called whenever there is a media event.
         */    public synchronized void controllerUpdate(ControllerEvent event)
        {
           // If we're getting messages from a dead player,
           // just leave
           Player player = (Player)event.getSourceController();
           if (player == null) return;       // When the player is Realized, get the visual
           // and control components and add them to the Applet       if (event instanceof RealizeCompleteEvent)
           {
              if ((visualComponent = player.getVisualComponent()) != null)
                {
                add("North", visualComponent);
                player.start();
                }
       /*       if ((controlComponent = player.getControlPanelComponent()) != null)
                {
                add("South",controlComponent);
                }
      */
                 // force the applet to draw the components
               validate();
           }
           else if (event instanceof CachingControlEvent)
           {          // Put a progress bar up when downloading starts,
              // take it down when downloading ends.          CachingControlEvent  e = (CachingControlEvent) event;
              CachingControl      cc = e.getCachingControl();
              long cc_progress       = e.getContentProgress();
              long cc_length         = cc.getContentLength();          // Add the bar if not already there ...          if (progressBar == null)
                 if ((progressBar = cc.getProgressBarComponent()) != null)
                 {
                    add("South", progressBar);
                    validate();
                 }          // Remove bar when finished ownloading
              if (progressBar != null)
                 if (cc_progress == cc_length)
                 {
                    remove (progressBar);
                    progressBar = null;
                    validate();
                 }
           }
           else if (event instanceof EndOfMediaEvent)
           {
              // We've reached the end of the media; rewind and
              // start over          player.setMediaTime(new Time(0));
              player.start();
           }
           else if (event instanceof ControllerErrorEvent)
           {
              // Tell TypicalPlayerApplet.start() to call it a day          player = null;
              Fatal (((ControllerErrorEvent)event).getMessage());
           }
        }
        void Fatal (String s)
        {
           // Applications will make various choices about what
           // to do here.  We print a message and then exit       System.err.println("FATAL ERROR: " + s);
           throw new Error(s);  // Invoke the uncaught exception
                                // handler System.exit() is another
                                // choice
        }
        /**
         * A utility class to parse the session addresses.
         */    class SessionLabel { public String addr = null;
    public int port;
    public int ttl = 1; SessionLabel(String session) throws IllegalArgumentException {     int off;
        String portStr = null, ttlStr = null;     if (session != null && session.length() > 0) {
    while (session.length() > 1 && session.charAt(0) == '/')
        session = session.substring(1); // Now see if there's a addr specified.
    off = session.indexOf('/');
    if (off == -1) {
        if (!session.equals(""))
    addr = session;
    } else {
        addr = session.substring(0, off);
        session = session.substring(off + 1);
        // Now see if there's a port specified
        off = session.indexOf('/');
        if (off == -1) {
    if (!session.equals(""))
        portStr = session;
        } else {
    portStr = session.substring(0, off);
    session = session.substring(off + 1);
    // Now see if there's a ttl specified
    off = session.indexOf('/');
    if (off == -1) {
        if (!session.equals(""))
    ttlStr = session;
    } else {
        ttlStr = session.substring(0, off);
    }
        }
    }
        }     if (addr == null)
    throw new IllegalArgumentException();     if (portStr != null) {
    try {
        Integer integer = Integer.valueOf(portStr);
        if (integer != null)
    port = integer.intValue();
    } catch (Throwable t) {
        throw new IllegalArgumentException();
    }
        } else
    throw new IllegalArgumentException();     if (ttlStr != null) {
    try {
        Integer integer = Integer.valueOf(ttlStr);
        if (integer != null)
    ttl = integer.intValue();
    } catch (Throwable t) {
        throw new IllegalArgumentException();
    }
        }
    }
        }
    }
        /**
         * GUI classes for the Player.
         */    class PlayerWindow extends Frame { Player player;
    ReceiveStream stream; PlayerWindow(Player p, ReceiveStream strm) {
        player = p;
        stream = strm;
    } public void initialize() {
        add(new PlayerPanel(player));
    } public void close() {
        player.close();
        setVisible(false);
        dispose();
    } public void addNotify() {
        super.addNotify();
        pack();
    }
        }
        /**
         * GUI classes for the Player.
         */    class PlayerPanel extends Panel { Component vc, cc; PlayerPanel(Player p)
              {
      setLayout(new BorderLayout());
        if ((vc = p.getVisualComponent()) != null)
                    {
    add("Center", vc);
                    p.start();
                    }
    /*     if ((cc = p.getControlPanelComponent()) != null)
                    {
    add("South", cc);
                    p.start();                }
    */          }
        }
    /* public Dimension getPreferredSize()
              {
        int w = 0, h = 0;
        if (vc != null)
                {
    Dimension size = vc.getPreferredSize();
    w = size.width;
    h = size.height;
        }
        if (cc != null)
                {
    Dimension size = cc.getPreferredSize();
    if (w == 0)
        w = size.width;
    h += size.height;
        }
        if (w < 160)
    w = 160;
        return new Dimension(w, h);
    }
    */
      

  4.   

    顶一下,我也正在学习这个,有点难,资料也比较少.你有MSN吗,我们交流一下.我的MSN:[email protected]
      

  5.   

    我已经解决了 其实就是个applet的权限问题。我的视频数据是发送到本机某个端口的,aaplet的jar包必须经过数字签名才有权限访问本机的端口和资源。目前这个项目还是在实验室阶段,使用的是广播发送数据。我希望把它改成点对点的,“点播”方式,并且带认证功能的。不知那位高手有经验或着建议?3Q
      

  6.   

    你好,我也是根据JMF在sun上的例子 AVReceive改写了applet
      
    用applet写了捕捉音视频并发送和接收的程序,但是接收时总说端口被占用  
    但是如果不用同一个端口,怎么接收数据呢?  
    我机子的IP是:10.24.2.221  
    我在发送程序中,写目的地的IP为10.24.2.221,端口为1500  
    在接收程序中,写发送地的IP为10.24.2.221,端口为1500  
    可这样,测试时就说被占用  
    后来把接收程序中的发送地端口改为1510,这次没有占用,但就是显示在等待数据中  
    什么都收不到  这个问题该怎么解决啊?麻烦了!
      

  7.   

    to fairylly:
    接收端地址改为多播地址,我用224.0.0.1可以
      

  8.   

    楼主,您说的那个applet权限问题,那块是怎样改的呢?