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

📄 receiver.java

📁 是一个用java实现的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        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);

        } 
        if (evt instanceof ApplicationEvent) {
            System.out.println("applciation event New stream received!!!!!!!!!");
        }
        else if (evt instanceof NewReceiveStreamEvent) {

            try {                
                //Pull out the stream 
                stream = ((NewReceiveStreamEvent)evt).getReceiveStream();
                DataSource ds = stream.getDataSource();

                // Find out the formats.   
                Object[] format=mgr.getControls();
                System.out.println(format.length);
                RTPControl ctl = (RTPControl)ds.getControl("javax.media.rtp.RTPControl");                
                if (ctl != null){
                    System.err.println("  - Received new RTP stream: " + ctl.getFormat());                                         
                    //ctl.addFormat(new AudioFormat(AudioFormat.MPEG_RTP,48000,16,1),22);
                    ctl.addFormat(new AudioFormat(AudioFormat.MPEG_RTP,48000,16,1),14);
                    Format[] formats=ctl.getFormatList();
                    System.out.println("format list : "+formats.length);
                } else
                    System.err.println("  - Received 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();
                Control[] cs=p.getControls();
                for(int i=0;i<cs.length;i++)
                    if(cs[i] instanceof FrameRateControl)
                        System.out.println("oooooouuuuuuuuuuuuuuuuuuuuuiiiiiiiiiiiiiiii");
                if(ctl.getFormat() instanceof VideoFormat){               
                                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){
                    Format format=ctl.getFormat();
                    System.err.println("      " + format);
                    //if(format.getEncoding().indexOf("mpegaudio"))
                    //MpegAudioControl mpegControl=(MpegAudioControl)mgr.getControl("javax.media.control.MpegAudioControl");
                    //System.out.println("mpegControl::::"+mpegControl);
                }
                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);
				bye=true;
             }                         
        }        

    }
    public void stop(){
    	if(mgrs==null)
    		return;
        // close the RTP session.
        for (int i = 0; i < mgrs.length; i++) {
            if (mgrs[i] != null) {        
                mgrs[i].removeTargets("Closing session");
                mgrs[i].dispose();
                mgrs[i] = null;
            }
        }
    }
    
    /**
     * ControllerListener for the Players.
     */
    public synchronized void controllerUpdate(ControllerEvent ce) {

        Player p = (Player)ce.getSourceController();
        MpegAudioControl mpegControl=(MpegAudioControl)p.getControl("javax.media.control.MpegAudioControl");
        System.out.println("mpegControl::::"+mpegControl);
        if (p == null)
            return;

        // Get this when the internal players are realized.
        if (ce instanceof RealizeCompleteEvent) {
            PlayerWindow pw = find(p);
            if (pw != null) {
                pw.initialize();
                pw.setVisible(true);
            }
            p.start();
        }

        if (ce instanceof ControllerErrorEvent) {
            p.removeControllerListener(this);
            PlayerWindow pw = find(p);
            if (pw != null) {
                pw.close();
                playerWindows.removeElement(pw);
            }
            System.err.println("Receiver internal error: " + ce);
        }
        if (ce instanceof StartEvent) {
            GainControl gc=p.getGainControl();
            System.out.println("Class for gain contol"+gc);
            if(gc!=null){
                Component c=gc.getControlComponent();
                System.out.println("Class for component"+c);
            }
        }

    }

    public void receive(String localIpAddress){
        if (!this.initialize(localIpAddress)) {
            System.err.println("Failed to initialize the sessions.");           
        }

        // Check to see if a bye to end the RTPSession was received.
        /*try {
            while (!bye)
                Thread.sleep(1000);
        } 
        catch (Exception e) {
                e.printStackTrace();
        }

        System.err.println("Exiting Receiver");*/
    }
    
    public Socket getSocketRTPReceiver(){
        return socketRTPReceiver;
    }
    
    public Socket getSocketRTCPReceiver(){
        return socketRTCPReceiver;
    }

    public static void main(String argv[]) {
        Receiver receive=null;
        if (argv.length == 0)
            prUsage();
        try{
                receive = new Receiver(argv[0]);
        }
        catch(IllegalArgumentException iae){
                prUsage();
        }
        if (!receive.initialize("127.0.0.1")) {
            System.err.println("Failed to initialize the sessions.");
            System.exit(-1);
        }

        // Check to see if Receiver is done.
        try {
            //while (!avReceive.isDone())
            while (!bye)
                Thread.sleep(1000);
        } 
        catch (Exception e) {
                e.printStackTrace();
        }

        System.err.println("Exiting Receiver");
    }

    static void prUsage() {
        System.err.println("Usage: Receiver <session> <session> ...");
        System.err.println("     <session>: <address>/<destinationPort>/<localPort>/<ttl>");
        System.exit(0);
    }

}// end of Receiver

⌨️ 快捷键说明

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